github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/os_freebsd.go (about) 1 // Copyright 2011 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 ( 8 "runtime/internal/sys" 9 "unsafe" 10 ) 11 12 type mOS struct{} 13 14 //go:noescape 15 func thr_new(param *thrparam, size int32) int32 16 17 //go:noescape 18 func sigaltstack(new, old *stackt) 19 20 //go:noescape 21 func sigprocmask(how int32, new, old *sigset) 22 23 //go:noescape 24 func setitimer(mode int32, new, old *itimerval) 25 26 //go:noescape 27 func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32 28 29 func raiseproc(sig uint32) 30 31 func thr_self() thread 32 func thr_kill(tid thread, sig int) 33 34 //go:noescape 35 func sys_umtx_op(addr *uint32, mode int32, val uint32, uaddr1 uintptr, ut *umtx_time) int32 36 37 func osyield() 38 39 func kqueue() int32 40 41 //go:noescape 42 func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 43 44 func pipe() (r, w int32, errno int32) 45 func pipe2(flags int32) (r, w int32, errno int32) 46 func closeonexec(fd int32) 47 func setNonblock(fd int32) 48 49 // From FreeBSD's <sys/sysctl.h> 50 const ( 51 _CTL_HW = 6 52 _HW_PAGESIZE = 7 53 ) 54 55 var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}} 56 57 // Undocumented numbers from FreeBSD's lib/libc/gen/sysctlnametomib.c. 58 const ( 59 _CTL_QUERY = 0 60 _CTL_QUERY_MIB = 3 61 ) 62 63 // sysctlnametomib fill mib with dynamically assigned sysctl entries of name, 64 // return count of effected mib slots, return 0 on error. 65 func sysctlnametomib(name []byte, mib *[_CTL_MAXNAME]uint32) uint32 { 66 oid := [2]uint32{_CTL_QUERY, _CTL_QUERY_MIB} 67 miblen := uintptr(_CTL_MAXNAME) 68 if sysctl(&oid[0], 2, (*byte)(unsafe.Pointer(mib)), &miblen, (*byte)(unsafe.Pointer(&name[0])), (uintptr)(len(name))) < 0 { 69 return 0 70 } 71 miblen /= unsafe.Sizeof(uint32(0)) 72 if miblen <= 0 { 73 return 0 74 } 75 return uint32(miblen) 76 } 77 78 const ( 79 _CPU_CURRENT_PID = -1 // Current process ID. 80 ) 81 82 //go:noescape 83 func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32 84 85 //go:systemstack 86 func getncpu() int32 { 87 // Use a large buffer for the CPU mask. We're on the system 88 // stack, so this is fine, and we can't allocate memory for a 89 // dynamically-sized buffer at this point. 90 const maxCPUs = 64 * 1024 91 var mask [maxCPUs / 8]byte 92 var mib [_CTL_MAXNAME]uint32 93 94 // According to FreeBSD's /usr/src/sys/kern/kern_cpuset.c, 95 // cpuset_getaffinity return ERANGE when provided buffer size exceed the limits in kernel. 96 // Querying kern.smp.maxcpus to calculate maximum buffer size. 97 // See https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=200802 98 99 // Variable kern.smp.maxcpus introduced at Dec 23 2003, revision 123766, 100 // with dynamically assigned sysctl entries. 101 miblen := sysctlnametomib([]byte("kern.smp.maxcpus"), &mib) 102 if miblen == 0 { 103 return 1 104 } 105 106 // Query kern.smp.maxcpus. 107 dstsize := uintptr(4) 108 maxcpus := uint32(0) 109 if sysctl(&mib[0], miblen, (*byte)(unsafe.Pointer(&maxcpus)), &dstsize, nil, 0) != 0 { 110 return 1 111 } 112 113 maskSize := int(maxcpus+7) / 8 114 if maskSize < sys.PtrSize { 115 maskSize = sys.PtrSize 116 } 117 if maskSize > len(mask) { 118 maskSize = len(mask) 119 } 120 121 if cpuset_getaffinity(_CPU_LEVEL_WHICH, _CPU_WHICH_PID, _CPU_CURRENT_PID, 122 maskSize, (*byte)(unsafe.Pointer(&mask[0]))) != 0 { 123 return 1 124 } 125 n := int32(0) 126 for _, v := range mask[:maskSize] { 127 for v != 0 { 128 n += int32(v & 1) 129 v >>= 1 130 } 131 } 132 if n == 0 { 133 return 1 134 } 135 return n 136 } 137 138 func getPageSize() uintptr { 139 mib := [2]uint32{_CTL_HW, _HW_PAGESIZE} 140 out := uint32(0) 141 nout := unsafe.Sizeof(out) 142 ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) 143 if ret >= 0 { 144 return uintptr(out) 145 } 146 return 0 147 } 148 149 // FreeBSD's umtx_op syscall is effectively the same as Linux's futex, and 150 // thus the code is largely similar. See Linux implementation 151 // and lock_futex.go for comments. 152 153 //go:nosplit 154 func futexsleep(addr *uint32, val uint32, ns int64) { 155 systemstack(func() { 156 futexsleep1(addr, val, ns) 157 }) 158 } 159 160 func futexsleep1(addr *uint32, val uint32, ns int64) { 161 var utp *umtx_time 162 if ns >= 0 { 163 var ut umtx_time 164 ut._clockid = _CLOCK_MONOTONIC 165 ut._timeout.setNsec(ns) 166 utp = &ut 167 } 168 ret := sys_umtx_op(addr, _UMTX_OP_WAIT_UINT_PRIVATE, val, unsafe.Sizeof(*utp), utp) 169 if ret >= 0 || ret == -_EINTR || ret == -_ETIMEDOUT { 170 return 171 } 172 print("umtx_wait addr=", addr, " val=", val, " ret=", ret, "\n") 173 *(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005 174 } 175 176 //go:nosplit 177 func futexwakeup(addr *uint32, cnt uint32) { 178 ret := sys_umtx_op(addr, _UMTX_OP_WAKE_PRIVATE, cnt, 0, nil) 179 if ret >= 0 { 180 return 181 } 182 183 systemstack(func() { 184 print("umtx_wake_addr=", addr, " ret=", ret, "\n") 185 }) 186 } 187 188 func thr_start() 189 190 // May run with m.p==nil, so write barriers are not allowed. 191 //go:nowritebarrier 192 func newosproc(mp *m) { 193 stk := unsafe.Pointer(mp.g0.stack.hi) 194 if false { 195 print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " thr_start=", funcPC(thr_start), " id=", mp.id, " ostk=", &mp, "\n") 196 } 197 198 param := thrparam{ 199 start_func: funcPC(thr_start), 200 arg: unsafe.Pointer(mp), 201 stack_base: mp.g0.stack.lo, 202 stack_size: uintptr(stk) - mp.g0.stack.lo, 203 child_tid: nil, // minit will record tid 204 parent_tid: nil, 205 tls_base: unsafe.Pointer(&mp.tls[0]), 206 tls_size: unsafe.Sizeof(mp.tls), 207 } 208 209 var oset sigset 210 sigprocmask(_SIG_SETMASK, &sigset_all, &oset) 211 ret := thr_new(¶m, int32(unsafe.Sizeof(param))) 212 sigprocmask(_SIG_SETMASK, &oset, nil) 213 if ret < 0 { 214 print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -ret, ")\n") 215 throw("newosproc") 216 } 217 } 218 219 // Version of newosproc that doesn't require a valid G. 220 //go:nosplit 221 func newosproc0(stacksize uintptr, fn unsafe.Pointer) { 222 stack := sysAlloc(stacksize, &memstats.stacks_sys) 223 if stack == nil { 224 write(2, unsafe.Pointer(&failallocatestack[0]), int32(len(failallocatestack))) 225 exit(1) 226 } 227 // This code "knows" it's being called once from the library 228 // initialization code, and so it's using the static m0 for the 229 // tls and procid (thread) pointers. thr_new() requires the tls 230 // pointers, though the tid pointers can be nil. 231 // However, newosproc0 is currently unreachable because builds 232 // utilizing c-shared/c-archive force external linking. 233 param := thrparam{ 234 start_func: funcPC(fn), 235 arg: nil, 236 stack_base: uintptr(stack), //+stacksize? 237 stack_size: stacksize, 238 child_tid: nil, // minit will record tid 239 parent_tid: nil, 240 tls_base: unsafe.Pointer(&m0.tls[0]), 241 tls_size: unsafe.Sizeof(m0.tls), 242 } 243 244 var oset sigset 245 sigprocmask(_SIG_SETMASK, &sigset_all, &oset) 246 ret := thr_new(¶m, int32(unsafe.Sizeof(param))) 247 sigprocmask(_SIG_SETMASK, &oset, nil) 248 if ret < 0 { 249 write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate))) 250 exit(1) 251 } 252 } 253 254 var failallocatestack = []byte("runtime: failed to allocate stack for the new OS thread\n") 255 var failthreadcreate = []byte("runtime: failed to create new OS thread\n") 256 257 // Called to do synchronous initialization of Go code built with 258 // -buildmode=c-archive or -buildmode=c-shared. 259 // None of the Go runtime is initialized. 260 //go:nosplit 261 //go:nowritebarrierrec 262 func libpreinit() { 263 initsig(true) 264 } 265 266 func osinit() { 267 ncpu = getncpu() 268 if physPageSize == 0 { 269 physPageSize = getPageSize() 270 } 271 } 272 273 var urandom_dev = []byte("/dev/urandom\x00") 274 275 //go:nosplit 276 func getRandomData(r []byte) { 277 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) 278 n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) 279 closefd(fd) 280 extendRandom(r, int(n)) 281 } 282 283 func goenvs() { 284 goenvs_unix() 285 } 286 287 // Called to initialize a new m (including the bootstrap m). 288 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 289 func mpreinit(mp *m) { 290 mp.gsignal = malg(32 * 1024) 291 mp.gsignal.m = mp 292 } 293 294 // Called to initialize a new m (including the bootstrap m). 295 // Called on the new thread, cannot allocate memory. 296 func minit() { 297 getg().m.procid = uint64(thr_self()) 298 299 // On FreeBSD before about April 2017 there was a bug such 300 // that calling execve from a thread other than the main 301 // thread did not reset the signal stack. That would confuse 302 // minitSignals, which calls minitSignalStack, which checks 303 // whether there is currently a signal stack and uses it if 304 // present. To avoid this confusion, explicitly disable the 305 // signal stack on the main thread when not running in a 306 // library. This can be removed when we are confident that all 307 // FreeBSD users are running a patched kernel. See issue #15658. 308 if gp := getg(); !isarchive && !islibrary && gp.m == &m0 && gp == gp.m.g0 { 309 st := stackt{ss_flags: _SS_DISABLE} 310 sigaltstack(&st, nil) 311 } 312 313 minitSignals() 314 } 315 316 // Called from dropm to undo the effect of an minit. 317 //go:nosplit 318 func unminit() { 319 unminitSignals() 320 } 321 322 // Called from exitm, but not from drop, to undo the effect of thread-owned 323 // resources in minit, semacreate, or elsewhere. Do not take locks after calling this. 324 func mdestroy(mp *m) { 325 } 326 327 func sigtramp() 328 329 type sigactiont struct { 330 sa_handler uintptr 331 sa_flags int32 332 sa_mask sigset 333 } 334 335 // See os_freebsd2.go, os_freebsd_amd64.go for setsig function 336 337 //go:nosplit 338 //go:nowritebarrierrec 339 func setsigstack(i uint32) { 340 var sa sigactiont 341 sigaction(i, nil, &sa) 342 if sa.sa_flags&_SA_ONSTACK != 0 { 343 return 344 } 345 sa.sa_flags |= _SA_ONSTACK 346 sigaction(i, &sa, nil) 347 } 348 349 //go:nosplit 350 //go:nowritebarrierrec 351 func getsig(i uint32) uintptr { 352 var sa sigactiont 353 sigaction(i, nil, &sa) 354 return sa.sa_handler 355 } 356 357 // setSignaltstackSP sets the ss_sp field of a stackt. 358 //go:nosplit 359 func setSignalstackSP(s *stackt, sp uintptr) { 360 s.ss_sp = sp 361 } 362 363 //go:nosplit 364 //go:nowritebarrierrec 365 func sigaddset(mask *sigset, i int) { 366 mask.__bits[(i-1)/32] |= 1 << ((uint32(i) - 1) & 31) 367 } 368 369 func sigdelset(mask *sigset, i int) { 370 mask.__bits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31) 371 } 372 373 //go:nosplit 374 func (c *sigctxt) fixsigcode(sig uint32) { 375 } 376 377 func sysargs(argc int32, argv **byte) { 378 n := argc + 1 379 380 // skip over argv, envp to get to auxv 381 for argv_index(argv, n) != nil { 382 n++ 383 } 384 385 // skip NULL separator 386 n++ 387 388 // now argv+n is auxv 389 auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize)) 390 sysauxv(auxv[:]) 391 } 392 393 const ( 394 _AT_NULL = 0 // Terminates the vector 395 _AT_PAGESZ = 6 // Page size in bytes 396 _AT_TIMEKEEP = 22 // Pointer to timehands. 397 _AT_HWCAP = 25 // CPU feature flags 398 _AT_HWCAP2 = 26 // CPU feature flags 2 399 ) 400 401 func sysauxv(auxv []uintptr) { 402 for i := 0; auxv[i] != _AT_NULL; i += 2 { 403 tag, val := auxv[i], auxv[i+1] 404 switch tag { 405 // _AT_NCPUS from auxv shouldn't be used due to golang.org/issue/15206 406 case _AT_PAGESZ: 407 physPageSize = val 408 case _AT_TIMEKEEP: 409 timekeepSharedPage = (*vdsoTimekeep)(unsafe.Pointer(val)) 410 } 411 412 archauxv(tag, val) 413 } 414 } 415 416 // sysSigaction calls the sigaction system call. 417 //go:nosplit 418 func sysSigaction(sig uint32, new, old *sigactiont) { 419 // Use system stack to avoid split stack overflow on amd64 420 if asmSigaction(uintptr(sig), new, old) != 0 { 421 systemstack(func() { 422 throw("sigaction failed") 423 }) 424 } 425 } 426 427 // asmSigaction is implemented in assembly. 428 //go:noescape 429 func asmSigaction(sig uintptr, new, old *sigactiont) int32 430 431 // raise sends a signal to the calling thread. 432 // 433 // It must be nosplit because it is used by the signal handler before 434 // it definitely has a Go stack. 435 // 436 //go:nosplit 437 func raise(sig uint32) { 438 thr_kill(thr_self(), int(sig)) 439 } 440 441 func signalM(mp *m, sig int) { 442 thr_kill(thread(mp.procid), sig) 443 }