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