github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/src/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) 16 17 //go:noescape 18 func sigaltstack(new, old *stackt) 19 20 //go:noescape 21 func sigaction(sig uint32, new, old *sigactiont) 22 23 //go:noescape 24 func sigprocmask(how int32, new, old *sigset) 25 26 //go:noescape 27 func setitimer(mode int32, new, old *itimerval) 28 29 //go:noescape 30 func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32 31 32 //go:noescape 33 func getrlimit(kind int32, limit unsafe.Pointer) int32 34 func raise(sig uint32) 35 func raiseproc(sig uint32) 36 37 //go:noescape 38 func sys_umtx_op(addr *uint32, mode int32, val uint32, uaddr1 uintptr, ut *umtx_time) int32 39 40 func osyield() 41 42 // From FreeBSD's <sys/sysctl.h> 43 const ( 44 _CTL_HW = 6 45 _HW_PAGESIZE = 7 46 ) 47 48 var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}} 49 50 // Undocumented numbers from FreeBSD's lib/libc/gen/sysctlnametomib.c. 51 const ( 52 _CTL_QUERY = 0 53 _CTL_QUERY_MIB = 3 54 ) 55 56 // sysctlnametomib fill mib with dynamically assigned sysctl entries of name, 57 // return count of effected mib slots, return 0 on error. 58 func sysctlnametomib(name []byte, mib *[_CTL_MAXNAME]uint32) uint32 { 59 oid := [2]uint32{_CTL_QUERY, _CTL_QUERY_MIB} 60 miblen := uintptr(_CTL_MAXNAME) 61 if sysctl(&oid[0], 2, (*byte)(unsafe.Pointer(mib)), &miblen, (*byte)(unsafe.Pointer(&name[0])), (uintptr)(len(name))) < 0 { 62 return 0 63 } 64 miblen /= unsafe.Sizeof(uint32(0)) 65 if miblen <= 0 { 66 return 0 67 } 68 return uint32(miblen) 69 } 70 71 const ( 72 _CPU_SETSIZE_MAX = 32 // Limited by _MaxGomaxprocs(256) in runtime2.go. 73 _CPU_CURRENT_PID = -1 // Current process ID. 74 ) 75 76 //go:noescape 77 func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32 78 79 func getncpu() int32 { 80 var mask [_CPU_SETSIZE_MAX]byte 81 var mib [_CTL_MAXNAME]uint32 82 83 // According to FreeBSD's /usr/src/sys/kern/kern_cpuset.c, 84 // cpuset_getaffinity return ERANGE when provided buffer size exceed the limits in kernel. 85 // Querying kern.smp.maxcpus to calculate maximum buffer size. 86 // See https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=200802 87 88 // Variable kern.smp.maxcpus introduced at Dec 23 2003, revision 123766, 89 // with dynamically assigned sysctl entries. 90 miblen := sysctlnametomib([]byte("kern.smp.maxcpus"), &mib) 91 if miblen == 0 { 92 return 1 93 } 94 95 // Query kern.smp.maxcpus. 96 dstsize := uintptr(4) 97 maxcpus := uint32(0) 98 if sysctl(&mib[0], miblen, (*byte)(unsafe.Pointer(&maxcpus)), &dstsize, nil, 0) != 0 { 99 return 1 100 } 101 102 size := maxcpus / _NBBY 103 ptrsize := uint32(unsafe.Sizeof(uintptr(0))) 104 if size < ptrsize { 105 size = ptrsize 106 } 107 if size > _CPU_SETSIZE_MAX { 108 return 1 109 } 110 111 if cpuset_getaffinity(_CPU_LEVEL_WHICH, _CPU_WHICH_PID, _CPU_CURRENT_PID, 112 int(size), (*byte)(unsafe.Pointer(&mask[0]))) != 0 { 113 return 1 114 } 115 n := int32(0) 116 for _, v := range mask[:size] { 117 for v != 0 { 118 n += int32(v & 1) 119 v >>= 1 120 } 121 } 122 if n == 0 { 123 return 1 124 } 125 return n 126 } 127 128 func getPageSize() uintptr { 129 mib := [2]uint32{_CTL_HW, _HW_PAGESIZE} 130 out := uint32(0) 131 nout := unsafe.Sizeof(out) 132 ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) 133 if ret >= 0 { 134 return uintptr(out) 135 } 136 return 0 137 } 138 139 // FreeBSD's umtx_op syscall is effectively the same as Linux's futex, and 140 // thus the code is largely similar. See Linux implementation 141 // and lock_futex.go for comments. 142 143 //go:nosplit 144 func futexsleep(addr *uint32, val uint32, ns int64) { 145 systemstack(func() { 146 futexsleep1(addr, val, ns) 147 }) 148 } 149 150 func futexsleep1(addr *uint32, val uint32, ns int64) { 151 var utp *umtx_time 152 if ns >= 0 { 153 var ut umtx_time 154 ut._clockid = _CLOCK_MONOTONIC 155 ut._timeout.set_sec(int64(timediv(ns, 1000000000, (*int32)(unsafe.Pointer(&ut._timeout.tv_nsec))))) 156 utp = &ut 157 } 158 ret := sys_umtx_op(addr, _UMTX_OP_WAIT_UINT_PRIVATE, val, unsafe.Sizeof(*utp), utp) 159 if ret >= 0 || ret == -_EINTR { 160 return 161 } 162 print("umtx_wait addr=", addr, " val=", val, " ret=", ret, "\n") 163 *(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005 164 } 165 166 //go:nosplit 167 func futexwakeup(addr *uint32, cnt uint32) { 168 ret := sys_umtx_op(addr, _UMTX_OP_WAKE_PRIVATE, cnt, 0, nil) 169 if ret >= 0 { 170 return 171 } 172 173 systemstack(func() { 174 print("umtx_wake_addr=", addr, " ret=", ret, "\n") 175 }) 176 } 177 178 func thr_start() 179 180 // May run with m.p==nil, so write barriers are not allowed. 181 //go:nowritebarrier 182 func newosproc(mp *m, stk unsafe.Pointer) { 183 if false { 184 print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " thr_start=", funcPC(thr_start), " id=", mp.id, " ostk=", &mp, "\n") 185 } 186 187 // NOTE(rsc): This code is confused. stackbase is the top of the stack 188 // and is equal to stk. However, it's working, so I'm not changing it. 189 param := thrparam{ 190 start_func: funcPC(thr_start), 191 arg: unsafe.Pointer(mp), 192 stack_base: mp.g0.stack.hi, 193 stack_size: uintptr(stk) - mp.g0.stack.hi, 194 child_tid: unsafe.Pointer(&mp.procid), 195 parent_tid: nil, 196 tls_base: unsafe.Pointer(&mp.tls[0]), 197 tls_size: unsafe.Sizeof(mp.tls), 198 } 199 200 var oset sigset 201 sigprocmask(_SIG_SETMASK, &sigset_all, &oset) 202 // TODO: Check for error. 203 thr_new(¶m, int32(unsafe.Sizeof(param))) 204 sigprocmask(_SIG_SETMASK, &oset, nil) 205 } 206 207 func osinit() { 208 ncpu = getncpu() 209 physPageSize = getPageSize() 210 } 211 212 var urandom_dev = []byte("/dev/urandom\x00") 213 214 //go:nosplit 215 func getRandomData(r []byte) { 216 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) 217 n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) 218 closefd(fd) 219 extendRandom(r, int(n)) 220 } 221 222 func goenvs() { 223 goenvs_unix() 224 } 225 226 // Called to initialize a new m (including the bootstrap m). 227 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 228 func mpreinit(mp *m) { 229 mp.gsignal = malg(32 * 1024) 230 mp.gsignal.m = mp 231 } 232 233 // Called to initialize a new m (including the bootstrap m). 234 // Called on the new thread, cannot allocate memory. 235 func minit() { 236 // m.procid is a uint64, but thr_new writes a uint32 on 32-bit systems. 237 // Fix it up. (Only matters on big-endian, but be clean anyway.) 238 if sys.PtrSize == 4 { 239 _g_ := getg() 240 _g_.m.procid = uint64(*(*uint32)(unsafe.Pointer(&_g_.m.procid))) 241 } 242 243 // On FreeBSD before about April 2017 there was a bug such 244 // that calling execve from a thread other than the main 245 // thread did not reset the signal stack. That would confuse 246 // minitSignals, which calls minitSignalStack, which checks 247 // whether there is currently a signal stack and uses it if 248 // present. To avoid this confusion, explicitly disable the 249 // signal stack on the main thread when not running in a 250 // library. This can be removed when we are confident that all 251 // FreeBSD users are running a patched kernel. See issue #15658. 252 if gp := getg(); !isarchive && !islibrary && gp.m == &m0 && gp == gp.m.g0 { 253 st := stackt{ss_flags: _SS_DISABLE} 254 sigaltstack(&st, nil) 255 } 256 257 minitSignals() 258 } 259 260 // Called from dropm to undo the effect of an minit. 261 //go:nosplit 262 func unminit() { 263 unminitSignals() 264 } 265 266 func memlimit() uintptr { 267 /* 268 TODO: Convert to Go when something actually uses the result. 269 Rlimit rl; 270 extern byte runtime·text[], runtime·end[]; 271 uintptr used; 272 273 if(runtime·getrlimit(RLIMIT_AS, &rl) != 0) 274 return 0; 275 if(rl.rlim_cur >= 0x7fffffff) 276 return 0; 277 278 // Estimate our VM footprint excluding the heap. 279 // Not an exact science: use size of binary plus 280 // some room for thread stacks. 281 used = runtime·end - runtime·text + (64<<20); 282 if(used >= rl.rlim_cur) 283 return 0; 284 285 // If there's not at least 16 MB left, we're probably 286 // not going to be able to do much. Treat as no limit. 287 rl.rlim_cur -= used; 288 if(rl.rlim_cur < (16<<20)) 289 return 0; 290 291 return rl.rlim_cur - used; 292 */ 293 294 return 0 295 } 296 297 func sigtramp() 298 299 type sigactiont struct { 300 sa_handler uintptr 301 sa_flags int32 302 sa_mask sigset 303 } 304 305 //go:nosplit 306 //go:nowritebarrierrec 307 func setsig(i uint32, fn uintptr) { 308 var sa sigactiont 309 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTART 310 sa.sa_mask = sigset_all 311 if fn == funcPC(sighandler) { 312 fn = funcPC(sigtramp) 313 } 314 sa.sa_handler = fn 315 sigaction(i, &sa, nil) 316 } 317 318 //go:nosplit 319 //go:nowritebarrierrec 320 func setsigstack(i uint32) { 321 throw("setsigstack") 322 } 323 324 //go:nosplit 325 //go:nowritebarrierrec 326 func getsig(i uint32) uintptr { 327 var sa sigactiont 328 sigaction(i, nil, &sa) 329 return sa.sa_handler 330 } 331 332 // setSignaltstackSP sets the ss_sp field of a stackt. 333 //go:nosplit 334 func setSignalstackSP(s *stackt, sp uintptr) { 335 s.ss_sp = sp 336 } 337 338 //go:nosplit 339 //go:nowritebarrierrec 340 func sigaddset(mask *sigset, i int) { 341 mask.__bits[(i-1)/32] |= 1 << ((uint32(i) - 1) & 31) 342 } 343 344 func sigdelset(mask *sigset, i int) { 345 mask.__bits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31) 346 } 347 348 func (c *sigctxt) fixsigcode(sig uint32) { 349 }