blob: 31708e2454b111170e0b61cbb6b20804ddc0ca7e [file] [log] [blame]
Dan Willemsen0c157092016-07-08 13:57:52 -07001// Copyright 2011 The Go Authors. All rights reserved.
Colin Cross7bb052a2015-02-03 12:59:37 -08002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package runtime
6
Dan Willemsen0c157092016-07-08 13:57:52 -07007import (
8 "runtime/internal/sys"
9 "unsafe"
10)
11
12type mOS struct{}
Colin Cross7bb052a2015-02-03 12:59:37 -080013
Dan Willemsen6ff23252015-09-15 13:49:18 -070014//go:noescape
15func thr_new(param *thrparam, size int32)
16
17//go:noescape
18func sigaltstack(new, old *stackt)
19
20//go:noescape
Dan Willemsenbbdf6642017-01-13 22:57:23 -080021func sigaction(sig uint32, new, old *sigactiont)
Dan Willemsen6ff23252015-09-15 13:49:18 -070022
23//go:noescape
24func sigprocmask(how int32, new, old *sigset)
25
26//go:noescape
27func setitimer(mode int32, new, old *itimerval)
28
29//go:noescape
Colin Cross7bb052a2015-02-03 12:59:37 -080030func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32
Dan Willemsen6ff23252015-09-15 13:49:18 -070031
32//go:noescape
Colin Cross7bb052a2015-02-03 12:59:37 -080033func getrlimit(kind int32, limit unsafe.Pointer) int32
Dan Willemsenbbdf6642017-01-13 22:57:23 -080034func raise(sig uint32)
35func raiseproc(sig uint32)
Dan Willemsen6ff23252015-09-15 13:49:18 -070036
37//go:noescape
Dan Willemsenbbdf6642017-01-13 22:57:23 -080038func sys_umtx_op(addr *uint32, mode int32, val uint32, uaddr1 uintptr, ut *umtx_time) int32
Dan Willemsen6ff23252015-09-15 13:49:18 -070039
40func osyield()
Dan Willemsen0c157092016-07-08 13:57:52 -070041
42// From FreeBSD's <sys/sysctl.h>
43const (
Dan Willemsenbbdf6642017-01-13 22:57:23 -080044 _CTL_HW = 6
Dan Willemsenbbdf6642017-01-13 22:57:23 -080045 _HW_PAGESIZE = 7
Dan Willemsen0c157092016-07-08 13:57:52 -070046)
47
48var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}}
49
Dan Willemsenc78f7142017-07-26 13:08:14 -070050// Undocumented numbers from FreeBSD's lib/libc/gen/sysctlnametomib.c.
51const (
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.
58func 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 Willemsen0c157092016-07-08 13:57:52 -070063 }
Dan Willemsenc78f7142017-07-26 13:08:14 -070064 miblen /= unsafe.Sizeof(uint32(0))
65 if miblen <= 0 {
66 return 0
67 }
68 return uint32(miblen)
69}
70
71const (
Dan Willemsenc78f7142017-07-26 13:08:14 -070072 _CPU_CURRENT_PID = -1 // Current process ID.
73)
74
75//go:noescape
76func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32
77
Dan Willemsene1b3b182018-02-27 19:36:27 -080078//go:systemstack
Dan Willemsenc78f7142017-07-26 13:08:14 -070079func getncpu() int32 {
Dan Willemsene1b3b182018-02-27 19:36:27 -080080 // 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 Willemsenc78f7142017-07-26 13:08:14 -070085 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 Willemsene1b3b182018-02-27 19:36:27 -0800106 maskSize := int(maxcpus+7) / 8
107 if maskSize < sys.PtrSize {
108 maskSize = sys.PtrSize
Dan Willemsenc78f7142017-07-26 13:08:14 -0700109 }
Dan Willemsene1b3b182018-02-27 19:36:27 -0800110 if maskSize > len(mask) {
111 maskSize = len(mask)
Dan Willemsenc78f7142017-07-26 13:08:14 -0700112 }
113
114 if cpuset_getaffinity(_CPU_LEVEL_WHICH, _CPU_WHICH_PID, _CPU_CURRENT_PID,
Dan Willemsene1b3b182018-02-27 19:36:27 -0800115 maskSize, (*byte)(unsafe.Pointer(&mask[0]))) != 0 {
Dan Willemsenc78f7142017-07-26 13:08:14 -0700116 return 1
117 }
118 n := int32(0)
Dan Willemsene1b3b182018-02-27 19:36:27 -0800119 for _, v := range mask[:maskSize] {
Dan Willemsenc78f7142017-07-26 13:08:14 -0700120 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 Willemsen0c157092016-07-08 13:57:52 -0700129}
130
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800131func 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 Willemsen0c157092016-07-08 13:57:52 -0700142// 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
147func futexsleep(addr *uint32, val uint32, ns int64) {
148 systemstack(func() {
149 futexsleep1(addr, val, ns)
150 })
151}
152
153func futexsleep1(addr *uint32, val uint32, ns int64) {
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800154 var utp *umtx_time
Dan Willemsen0c157092016-07-08 13:57:52 -0700155 if ns >= 0 {
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800156 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 Willemsen0c157092016-07-08 13:57:52 -0700160 }
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800161 ret := sys_umtx_op(addr, _UMTX_OP_WAIT_UINT_PRIVATE, val, unsafe.Sizeof(*utp), utp)
Dan Willemsen0c157092016-07-08 13:57:52 -0700162 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
170func futexwakeup(addr *uint32, cnt uint32) {
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800171 ret := sys_umtx_op(addr, _UMTX_OP_WAKE_PRIVATE, cnt, 0, nil)
Dan Willemsen0c157092016-07-08 13:57:52 -0700172 if ret >= 0 {
173 return
174 }
175
176 systemstack(func() {
177 print("umtx_wake_addr=", addr, " ret=", ret, "\n")
178 })
179}
180
181func thr_start()
182
183// May run with m.p==nil, so write barriers are not allowed.
184//go:nowritebarrier
185func 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(&param, int32(unsafe.Sizeof(param)))
207 sigprocmask(_SIG_SETMASK, &oset, nil)
208}
209
210func osinit() {
211 ncpu = getncpu()
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800212 physPageSize = getPageSize()
Dan Willemsen0c157092016-07-08 13:57:52 -0700213}
214
215var urandom_dev = []byte("/dev/urandom\x00")
216
217//go:nosplit
218func 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
225func 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.
231func mpreinit(mp *m) {
232 mp.gsignal = malg(32 * 1024)
233 mp.gsignal.m = mp
234}
235
Dan Willemsen0c157092016-07-08 13:57:52 -0700236// Called to initialize a new m (including the bootstrap m).
237// Called on the new thread, cannot allocate memory.
238func minit() {
Dan Willemsen0c157092016-07-08 13:57:52 -0700239 // 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 Willemsenbbdf6642017-01-13 22:57:23 -0800242 _g_ := getg()
Dan Willemsen0c157092016-07-08 13:57:52 -0700243 _g_.m.procid = uint64(*(*uint32)(unsafe.Pointer(&_g_.m.procid)))
244 }
245
Dan Willemsenc78f7142017-07-26 13:08:14 -0700246 // 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 Willemsenbbdf6642017-01-13 22:57:23 -0800260 minitSignals()
Dan Willemsen0c157092016-07-08 13:57:52 -0700261}
262
263// Called from dropm to undo the effect of an minit.
264//go:nosplit
265func unminit() {
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800266 unminitSignals()
Dan Willemsen0c157092016-07-08 13:57:52 -0700267}
268
269func 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
300func sigtramp()
301
302type sigactiont struct {
303 sa_handler uintptr
304 sa_flags int32
305 sa_mask sigset
306}
307
308//go:nosplit
309//go:nowritebarrierrec
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800310func setsig(i uint32, fn uintptr) {
Dan Willemsen0c157092016-07-08 13:57:52 -0700311 var sa sigactiont
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800312 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTART
Dan Willemsen0c157092016-07-08 13:57:52 -0700313 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 Willemsenbbdf6642017-01-13 22:57:23 -0800323func setsigstack(i uint32) {
Dan Willemsen0c157092016-07-08 13:57:52 -0700324 throw("setsigstack")
325}
326
327//go:nosplit
328//go:nowritebarrierrec
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800329func getsig(i uint32) uintptr {
Dan Willemsen0c157092016-07-08 13:57:52 -0700330 var sa sigactiont
331 sigaction(i, nil, &sa)
Dan Willemsen0c157092016-07-08 13:57:52 -0700332 return sa.sa_handler
333}
334
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800335// setSignaltstackSP sets the ss_sp field of a stackt.
Dan Willemsen0c157092016-07-08 13:57:52 -0700336//go:nosplit
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800337func setSignalstackSP(s *stackt, sp uintptr) {
338 s.ss_sp = sp
Dan Willemsen0c157092016-07-08 13:57:52 -0700339}
340
341//go:nosplit
342//go:nowritebarrierrec
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800343func sigaddset(mask *sigset, i int) {
344 mask.__bits[(i-1)/32] |= 1 << ((uint32(i) - 1) & 31)
Dan Willemsen0c157092016-07-08 13:57:52 -0700345}
346
Dan Willemsenbbdf6642017-01-13 22:57:23 -0800347func sigdelset(mask *sigset, i int) {
348 mask.__bits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31)
349}
350
351func (c *sigctxt) fixsigcode(sig uint32) {
Dan Willemsen0c157092016-07-08 13:57:52 -0700352}