github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/runtime/os_darwin.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime 6 7 import "unsafe" 8 9 type mOS struct { 10 machport uint32 // return address for mach ipc 11 waitsema uint32 // semaphore for parking on locks 12 } 13 14 func bsdthread_create(stk, arg unsafe.Pointer, fn uintptr) int32 15 func bsdthread_register() int32 16 17 //go:noescape 18 func mach_msg_trap(h unsafe.Pointer, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32 19 20 func mach_reply_port() uint32 21 func mach_task_self() uint32 22 func mach_thread_self() uint32 23 24 //go:noescape 25 func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32 26 27 func unimplemented(name string) { 28 println(name, "not implemented") 29 *(*int)(unsafe.Pointer(uintptr(1231))) = 1231 30 } 31 32 //go:nosplit 33 func semawakeup(mp *m) { 34 mach_semrelease(mp.waitsema) 35 } 36 37 //go:nosplit 38 func semacreate(mp *m) { 39 if mp.waitsema != 0 { 40 return 41 } 42 systemstack(func() { 43 mp.waitsema = mach_semcreate() 44 }) 45 } 46 47 // BSD interface for threading. 48 func osinit() { 49 // bsdthread_register delayed until end of goenvs so that we 50 // can look at the environment first. 51 52 ncpu = getncpu() 53 54 physPageSize = getPageSize() 55 } 56 57 const ( 58 _CTL_HW = 6 59 _HW_NCPU = 3 60 _HW_PAGESIZE = 7 61 ) 62 63 func getncpu() int32 { 64 // Use sysctl to fetch hw.ncpu. 65 mib := [2]uint32{_CTL_HW, _HW_NCPU} 66 out := uint32(0) 67 nout := unsafe.Sizeof(out) 68 ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) 69 if ret >= 0 && int32(out) > 0 { 70 return int32(out) 71 } 72 return 1 73 } 74 75 func getPageSize() uintptr { 76 // Use sysctl to fetch hw.pagesize. 77 mib := [2]uint32{_CTL_HW, _HW_PAGESIZE} 78 out := uint32(0) 79 nout := unsafe.Sizeof(out) 80 ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) 81 if ret >= 0 && int32(out) > 0 { 82 return uintptr(out) 83 } 84 return 0 85 } 86 87 var urandom_dev = []byte("/dev/urandom\x00") 88 89 //go:nosplit 90 func getRandomData(r []byte) { 91 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) 92 n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) 93 closefd(fd) 94 extendRandom(r, int(n)) 95 } 96 97 func goenvs() { 98 goenvs_unix() 99 100 // Register our thread-creation callback (see sys_darwin_{amd64,386}.s) 101 // but only if we're not using cgo. If we are using cgo we need 102 // to let the C pthread library install its own thread-creation callback. 103 if !iscgo { 104 if bsdthread_register() != 0 { 105 if gogetenv("DYLD_INSERT_LIBRARIES") != "" { 106 throw("runtime: bsdthread_register error (unset DYLD_INSERT_LIBRARIES)") 107 } 108 throw("runtime: bsdthread_register error") 109 } 110 } 111 } 112 113 // May run with m.p==nil, so write barriers are not allowed. 114 //go:nowritebarrier 115 func newosproc(mp *m, stk unsafe.Pointer) { 116 if false { 117 print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n") 118 } 119 120 var oset sigset 121 sigprocmask(_SIG_SETMASK, &sigset_all, &oset) 122 errno := bsdthread_create(stk, unsafe.Pointer(mp), funcPC(mstart)) 123 sigprocmask(_SIG_SETMASK, &oset, nil) 124 125 if errno < 0 { 126 print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -errno, ")\n") 127 throw("runtime.newosproc") 128 } 129 } 130 131 // newosproc0 is a version of newosproc that can be called before the runtime 132 // is initialized. 133 // 134 // As Go uses bsdthread_register when running without cgo, this function is 135 // not safe to use after initialization as it does not pass an M as fnarg. 136 // 137 //go:nosplit 138 func newosproc0(stacksize uintptr, fn unsafe.Pointer, fnarg uintptr) { 139 stack := sysAlloc(stacksize, &memstats.stacks_sys) 140 if stack == nil { 141 write(2, unsafe.Pointer(&failallocatestack[0]), int32(len(failallocatestack))) 142 exit(1) 143 } 144 stk := unsafe.Pointer(uintptr(stack) + stacksize) 145 146 var oset sigset 147 sigprocmask(_SIG_SETMASK, &sigset_all, &oset) 148 errno := bsdthread_create(stk, fn, fnarg) 149 sigprocmask(_SIG_SETMASK, &oset, nil) 150 151 if errno < 0 { 152 write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate))) 153 exit(1) 154 } 155 } 156 157 var failallocatestack = []byte("runtime: failed to allocate stack for the new OS thread\n") 158 var failthreadcreate = []byte("runtime: failed to create new OS thread\n") 159 160 // Called to do synchronous initialization of Go code built with 161 // -buildmode=c-archive or -buildmode=c-shared. 162 // None of the Go runtime is initialized. 163 //go:nosplit 164 //go:nowritebarrierrec 165 func libpreinit() { 166 initsig(true) 167 } 168 169 // Called to initialize a new m (including the bootstrap m). 170 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 171 func mpreinit(mp *m) { 172 mp.gsignal = malg(32 * 1024) // OS X wants >= 8K 173 mp.gsignal.m = mp 174 } 175 176 //go:nosplit 177 func msigsave(mp *m) { 178 sigprocmask(_SIG_SETMASK, nil, &mp.sigmask) 179 } 180 181 //go:nosplit 182 func msigrestore(sigmask sigset) { 183 sigprocmask(_SIG_SETMASK, &sigmask, nil) 184 } 185 186 //go:nosplit 187 func sigblock() { 188 sigprocmask(_SIG_SETMASK, &sigset_all, nil) 189 } 190 191 // Called to initialize a new m (including the bootstrap m). 192 // Called on the new thread, cannot allocate memory. 193 func minit() { 194 // Initialize signal handling. 195 _g_ := getg() 196 197 // The alternate signal stack is buggy on arm and arm64. 198 // The signal handler handles it directly. 199 // The sigaltstack assembly function does nothing. 200 if GOARCH != "arm" && GOARCH != "arm64" { 201 var st stackt 202 sigaltstack(nil, &st) 203 if st.ss_flags&_SS_DISABLE != 0 { 204 signalstack(&_g_.m.gsignal.stack) 205 _g_.m.newSigstack = true 206 } else { 207 // Use existing signal stack. 208 stsp := uintptr(unsafe.Pointer(st.ss_sp)) 209 _g_.m.gsignal.stack.lo = stsp 210 _g_.m.gsignal.stack.hi = stsp + st.ss_size 211 _g_.m.gsignal.stackguard0 = stsp + _StackGuard 212 _g_.m.gsignal.stackguard1 = stsp + _StackGuard 213 _g_.m.gsignal.stackAlloc = st.ss_size 214 _g_.m.newSigstack = false 215 } 216 } 217 218 // restore signal mask from m.sigmask and unblock essential signals 219 nmask := _g_.m.sigmask 220 for i := range sigtable { 221 if sigtable[i].flags&_SigUnblock != 0 { 222 nmask &^= 1 << (uint32(i) - 1) 223 } 224 } 225 sigprocmask(_SIG_SETMASK, &nmask, nil) 226 } 227 228 // Called from dropm to undo the effect of an minit. 229 //go:nosplit 230 func unminit() { 231 if getg().m.newSigstack { 232 signalstack(nil) 233 } 234 } 235 236 // Mach IPC, to get at semaphores 237 // Definitions are in /usr/include/mach on a Mac. 238 239 func macherror(r int32, fn string) { 240 print("mach error ", fn, ": ", r, "\n") 241 throw("mach error") 242 } 243 244 const _DebugMach = false 245 246 var zerondr machndr 247 248 func mach_msgh_bits(a, b uint32) uint32 { 249 return a | b<<8 250 } 251 252 func mach_msg(h *machheader, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32 { 253 // TODO: Loop on interrupt. 254 return mach_msg_trap(unsafe.Pointer(h), op, send_size, rcv_size, rcv_name, timeout, notify) 255 } 256 257 // Mach RPC (MIG) 258 const ( 259 _MinMachMsg = 48 260 _MachReply = 100 261 ) 262 263 type codemsg struct { 264 h machheader 265 ndr machndr 266 code int32 267 } 268 269 func machcall(h *machheader, maxsize int32, rxsize int32) int32 { 270 _g_ := getg() 271 port := _g_.m.machport 272 if port == 0 { 273 port = mach_reply_port() 274 _g_.m.machport = port 275 } 276 277 h.msgh_bits |= mach_msgh_bits(_MACH_MSG_TYPE_COPY_SEND, _MACH_MSG_TYPE_MAKE_SEND_ONCE) 278 h.msgh_local_port = port 279 h.msgh_reserved = 0 280 id := h.msgh_id 281 282 if _DebugMach { 283 p := (*[10000]unsafe.Pointer)(unsafe.Pointer(h)) 284 print("send:\t") 285 var i uint32 286 for i = 0; i < h.msgh_size/uint32(unsafe.Sizeof(p[0])); i++ { 287 print(" ", p[i]) 288 if i%8 == 7 { 289 print("\n\t") 290 } 291 } 292 if i%8 != 0 { 293 print("\n") 294 } 295 } 296 ret := mach_msg(h, _MACH_SEND_MSG|_MACH_RCV_MSG, h.msgh_size, uint32(maxsize), port, 0, 0) 297 if ret != 0 { 298 if _DebugMach { 299 print("mach_msg error ", ret, "\n") 300 } 301 return ret 302 } 303 if _DebugMach { 304 p := (*[10000]unsafe.Pointer)(unsafe.Pointer(h)) 305 var i uint32 306 for i = 0; i < h.msgh_size/uint32(unsafe.Sizeof(p[0])); i++ { 307 print(" ", p[i]) 308 if i%8 == 7 { 309 print("\n\t") 310 } 311 } 312 if i%8 != 0 { 313 print("\n") 314 } 315 } 316 if h.msgh_id != id+_MachReply { 317 if _DebugMach { 318 print("mach_msg _MachReply id mismatch ", h.msgh_id, " != ", id+_MachReply, "\n") 319 } 320 return -303 // MIG_REPLY_MISMATCH 321 } 322 // Look for a response giving the return value. 323 // Any call can send this back with an error, 324 // and some calls only have return values so they 325 // send it back on success too. I don't quite see how 326 // you know it's one of these and not the full response 327 // format, so just look if the message is right. 328 c := (*codemsg)(unsafe.Pointer(h)) 329 if uintptr(h.msgh_size) == unsafe.Sizeof(*c) && h.msgh_bits&_MACH_MSGH_BITS_COMPLEX == 0 { 330 if _DebugMach { 331 print("mig result ", c.code, "\n") 332 } 333 return c.code 334 } 335 if h.msgh_size != uint32(rxsize) { 336 if _DebugMach { 337 print("mach_msg _MachReply size mismatch ", h.msgh_size, " != ", rxsize, "\n") 338 } 339 return -307 // MIG_ARRAY_TOO_LARGE 340 } 341 return 0 342 } 343 344 // Semaphores! 345 346 const ( 347 tmach_semcreate = 3418 348 rmach_semcreate = tmach_semcreate + _MachReply 349 350 tmach_semdestroy = 3419 351 rmach_semdestroy = tmach_semdestroy + _MachReply 352 353 _KERN_ABORTED = 14 354 _KERN_OPERATION_TIMED_OUT = 49 355 ) 356 357 type tmach_semcreatemsg struct { 358 h machheader 359 ndr machndr 360 policy int32 361 value int32 362 } 363 364 type rmach_semcreatemsg struct { 365 h machheader 366 body machbody 367 semaphore machport 368 } 369 370 type tmach_semdestroymsg struct { 371 h machheader 372 body machbody 373 semaphore machport 374 } 375 376 func mach_semcreate() uint32 { 377 var m [256]uint8 378 tx := (*tmach_semcreatemsg)(unsafe.Pointer(&m)) 379 rx := (*rmach_semcreatemsg)(unsafe.Pointer(&m)) 380 381 tx.h.msgh_bits = 0 382 tx.h.msgh_size = uint32(unsafe.Sizeof(*tx)) 383 tx.h.msgh_remote_port = mach_task_self() 384 tx.h.msgh_id = tmach_semcreate 385 tx.ndr = zerondr 386 387 tx.policy = 0 // 0 = SYNC_POLICY_FIFO 388 tx.value = 0 389 390 for { 391 r := machcall(&tx.h, int32(unsafe.Sizeof(m)), int32(unsafe.Sizeof(*rx))) 392 if r == 0 { 393 break 394 } 395 if r == _KERN_ABORTED { // interrupted 396 continue 397 } 398 macherror(r, "semaphore_create") 399 } 400 if rx.body.msgh_descriptor_count != 1 { 401 unimplemented("mach_semcreate desc count") 402 } 403 return rx.semaphore.name 404 } 405 406 func mach_semdestroy(sem uint32) { 407 var m [256]uint8 408 tx := (*tmach_semdestroymsg)(unsafe.Pointer(&m)) 409 410 tx.h.msgh_bits = _MACH_MSGH_BITS_COMPLEX 411 tx.h.msgh_size = uint32(unsafe.Sizeof(*tx)) 412 tx.h.msgh_remote_port = mach_task_self() 413 tx.h.msgh_id = tmach_semdestroy 414 tx.body.msgh_descriptor_count = 1 415 tx.semaphore.name = sem 416 tx.semaphore.disposition = _MACH_MSG_TYPE_MOVE_SEND 417 tx.semaphore._type = 0 418 419 for { 420 r := machcall(&tx.h, int32(unsafe.Sizeof(m)), 0) 421 if r == 0 { 422 break 423 } 424 if r == _KERN_ABORTED { // interrupted 425 continue 426 } 427 macherror(r, "semaphore_destroy") 428 } 429 } 430 431 // The other calls have simple system call traps in sys_darwin_{amd64,386}.s 432 433 func mach_semaphore_wait(sema uint32) int32 434 func mach_semaphore_timedwait(sema, sec, nsec uint32) int32 435 func mach_semaphore_signal(sema uint32) int32 436 func mach_semaphore_signal_all(sema uint32) int32 437 438 func semasleep1(ns int64) int32 { 439 _g_ := getg() 440 441 if ns >= 0 { 442 var nsecs int32 443 secs := timediv(ns, 1000000000, &nsecs) 444 r := mach_semaphore_timedwait(_g_.m.waitsema, uint32(secs), uint32(nsecs)) 445 if r == _KERN_ABORTED || r == _KERN_OPERATION_TIMED_OUT { 446 return -1 447 } 448 if r != 0 { 449 macherror(r, "semaphore_wait") 450 } 451 return 0 452 } 453 454 for { 455 r := mach_semaphore_wait(_g_.m.waitsema) 456 if r == 0 { 457 break 458 } 459 if r == _KERN_ABORTED { // interrupted 460 continue 461 } 462 macherror(r, "semaphore_wait") 463 } 464 return 0 465 } 466 467 //go:nosplit 468 func semasleep(ns int64) int32 { 469 var r int32 470 systemstack(func() { 471 r = semasleep1(ns) 472 }) 473 return r 474 } 475 476 //go:nosplit 477 func mach_semrelease(sem uint32) { 478 for { 479 r := mach_semaphore_signal(sem) 480 if r == 0 { 481 break 482 } 483 if r == _KERN_ABORTED { // interrupted 484 continue 485 } 486 487 // mach_semrelease must be completely nosplit, 488 // because it is called from Go code. 489 // If we're going to die, start that process on the system stack 490 // to avoid a Go stack split. 491 systemstack(func() { macherror(r, "semaphore_signal") }) 492 } 493 } 494 495 //go:nosplit 496 func osyield() { 497 usleep(1) 498 } 499 500 func memlimit() uintptr { 501 // NOTE(rsc): Could use getrlimit here, 502 // like on FreeBSD or Linux, but Darwin doesn't enforce 503 // ulimit -v, so it's unclear why we'd try to stay within 504 // the limit. 505 return 0 506 } 507 508 const ( 509 _NSIG = 32 510 _SI_USER = 0 /* empirically true, but not what headers say */ 511 _SIG_BLOCK = 1 512 _SIG_UNBLOCK = 2 513 _SIG_SETMASK = 3 514 _SS_DISABLE = 4 515 ) 516 517 //go:noescape 518 func sigprocmask(how uint32, new, old *sigset) 519 520 //go:noescape 521 func sigaction(mode uint32, new *sigactiont, old *usigactiont) 522 523 //go:noescape 524 func sigaltstack(new, old *stackt) 525 526 func sigtramp() 527 528 //go:noescape 529 func setitimer(mode int32, new, old *itimerval) 530 531 func raise(sig int32) 532 func raiseproc(int32) 533 534 //extern SigTabTT runtimeĀ·sigtab[]; 535 536 type sigset uint32 537 538 var sigset_all = ^sigset(0) 539 540 //go:nosplit 541 //go:nowritebarrierrec 542 func setsig(i int32, fn uintptr, restart bool) { 543 var sa sigactiont 544 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK 545 if restart { 546 sa.sa_flags |= _SA_RESTART 547 } 548 sa.sa_mask = ^uint32(0) 549 sa.sa_tramp = unsafe.Pointer(funcPC(sigtramp)) // runtimeĀ·sigtramp's job is to call into real handler 550 *(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) = fn 551 sigaction(uint32(i), &sa, nil) 552 } 553 554 //go:nosplit 555 //go:nowritebarrierrec 556 func setsigstack(i int32) { 557 var osa usigactiont 558 sigaction(uint32(i), nil, &osa) 559 handler := *(*uintptr)(unsafe.Pointer(&osa.__sigaction_u)) 560 if handler == 0 || handler == _SIG_DFL || handler == _SIG_IGN || osa.sa_flags&_SA_ONSTACK != 0 { 561 return 562 } 563 var sa sigactiont 564 *(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) = handler 565 sa.sa_tramp = unsafe.Pointer(funcPC(sigtramp)) 566 sa.sa_mask = osa.sa_mask 567 sa.sa_flags = osa.sa_flags | _SA_ONSTACK 568 sigaction(uint32(i), &sa, nil) 569 } 570 571 //go:nosplit 572 //go:nowritebarrierrec 573 func getsig(i int32) uintptr { 574 var sa usigactiont 575 sigaction(uint32(i), nil, &sa) 576 return *(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) 577 } 578 579 //go:nosplit 580 func signalstack(s *stack) { 581 var st stackt 582 if s == nil { 583 st.ss_flags = _SS_DISABLE 584 } else { 585 st.ss_sp = (*byte)(unsafe.Pointer(s.lo)) 586 st.ss_size = s.hi - s.lo 587 st.ss_flags = 0 588 } 589 sigaltstack(&st, nil) 590 } 591 592 //go:nosplit 593 //go:nowritebarrierrec 594 func updatesigmask(m sigmask) { 595 s := sigset(m[0]) 596 sigprocmask(_SIG_SETMASK, &s, nil) 597 } 598 599 func unblocksig(sig int32) { 600 mask := sigset(1) << (uint32(sig) - 1) 601 sigprocmask(_SIG_UNBLOCK, &mask, nil) 602 }