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