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