ggstd/runtime/os_linux.rs
1// Copyright 2023 The rust-ggstd authors. All rights reserved.
2// Copyright 2009 The Go Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file.
5
6use std::io::Read;
7
8// import (
9// "internal/abi"
10// "internal/goarch"
11// "runtime/internal/atomic"
12// "runtime/internal/syscall"
13// "unsafe"
14// )
15
16// // sigPerThreadSyscall is the same signal (SIGSETXID) used by glibc for
17// // per-thread syscalls on Linux. We use it for the same purpose in non-cgo
18// // binaries.
19// const sigPerThreadSyscall = _SIGRTMIN + 1
20
21// type mOS struct {
22// // profileTimer holds the ID of the POSIX interval timer for profiling CPU
23// // usage on this thread.
24// //
25// // It is valid when the profileTimerValid field is true. A thread
26// // creates and manages its own timer, and these fields are read and written
27// // only by this thread. But because some of the reads on profileTimerValid
28// // are in signal handling code, this field should be atomic type.
29// profileTimer int32
30// profileTimerValid atomic.Bool
31
32// // needPerThreadSyscall indicates that a per-thread syscall is required
33// // for doAllThreadsSyscall.
34// needPerThreadSyscall atomic.Uint8
35// }
36
37// //go:noescape
38// func futex(addr unsafe.Pointer, op int32, val uint32, ts, addr2 unsafe.Pointer, val3 uint32) int32
39
40// // Linux futex.
41// //
42// // futexsleep(uint32 *addr, uint32 val)
43// // futexwakeup(uint32 *addr)
44// //
45// // Futexsleep atomically checks if *addr == val and if so, sleeps on addr.
46// // Futexwakeup wakes up threads sleeping on addr.
47// // Futexsleep is allowed to wake up spuriously.
48
49// const (
50// _FUTEX_PRIVATE_FLAG = 128
51// _FUTEX_WAIT_PRIVATE = 0 | _FUTEX_PRIVATE_FLAG
52// _FUTEX_WAKE_PRIVATE = 1 | _FUTEX_PRIVATE_FLAG
53// )
54
55// // Atomically,
56// //
57// // if(*addr == val) sleep
58// //
59// // Might be woken up spuriously; that's allowed.
60// // Don't sleep longer than ns; ns < 0 means forever.
61// //
62// //go:nosplit
63// func futexsleep(addr *uint32, val uint32, ns int64) {
64// // Some Linux kernels have a bug where futex of
65// // FUTEX_WAIT returns an internal error code
66// // as an errno. Libpthread ignores the return value
67// // here, and so can we: as it says a few lines up,
68// // spurious wakeups are allowed.
69// if ns < 0 {
70// futex(unsafe.Pointer(addr), _FUTEX_WAIT_PRIVATE, val, nil, nil, 0)
71// return
72// }
73
74// var ts timespec
75// ts.setNsec(ns)
76// futex(unsafe.Pointer(addr), _FUTEX_WAIT_PRIVATE, val, unsafe.Pointer(&ts), nil, 0)
77// }
78
79// // If any procs are sleeping on addr, wake up at most cnt.
80// //
81// //go:nosplit
82// func futexwakeup(addr *uint32, cnt uint32) {
83// ret := futex(unsafe.Pointer(addr), _FUTEX_WAKE_PRIVATE, cnt, nil, nil, 0)
84// if ret >= 0 {
85// return
86// }
87
88// // I don't know that futex wakeup can return
89// // EAGAIN or EINTR, but if it does, it would be
90// // safe to loop and call futex again.
91// systemstack(func() {
92// print("futexwakeup addr=", addr, " returned ", ret, "\n")
93// })
94
95// *(*int32)(unsafe.Pointer(uintptr(0x1006))) = 0x1006
96// }
97
98// func getproccount() int32 {
99// // This buffer is huge (8 kB) but we are on the system stack
100// // and there should be plenty of space (64 kB).
101// // Also this is a leaf, so we're not holding up the memory for long.
102// // See golang.org/issue/11823.
103// // The suggested behavior here is to keep trying with ever-larger
104// // buffers, but we don't have a dynamic memory allocator at the
105// // moment, so that's a bit tricky and seems like overkill.
106// const maxCPUs = 64 * 1024
107// var buf [maxCPUs / 8]byte
108// r := sched_getaffinity(0, unsafe.Sizeof(buf), &buf[0])
109// if r < 0 {
110// return 1
111// }
112// n := int32(0)
113// for _, v := range buf[:r] {
114// for v != 0 {
115// n += int32(v & 1)
116// v >>= 1
117// }
118// }
119// if n == 0 {
120// n = 1
121// }
122// return n
123// }
124
125// // Clone, the Linux rfork.
126// const (
127// _CLONE_VM = 0x100
128// _CLONE_FS = 0x200
129// _CLONE_FILES = 0x400
130// _CLONE_SIGHAND = 0x800
131// _CLONE_PTRACE = 0x2000
132// _CLONE_VFORK = 0x4000
133// _CLONE_PARENT = 0x8000
134// _CLONE_THREAD = 0x10000
135// _CLONE_NEWNS = 0x20000
136// _CLONE_SYSVSEM = 0x40000
137// _CLONE_SETTLS = 0x80000
138// _CLONE_PARENT_SETTID = 0x100000
139// _CLONE_CHILD_CLEARTID = 0x200000
140// _CLONE_UNTRACED = 0x800000
141// _CLONE_CHILD_SETTID = 0x1000000
142// _CLONE_STOPPED = 0x2000000
143// _CLONE_NEWUTS = 0x4000000
144// _CLONE_NEWIPC = 0x8000000
145
146// // As of QEMU 2.8.0 (5ea2fc84d), user emulation requires all six of these
147// // flags to be set when creating a thread; attempts to share the other
148// // five but leave SYSVSEM unshared will fail with -EINVAL.
149// //
150// // In non-QEMU environments CLONE_SYSVSEM is inconsequential as we do not
151// // use System V semaphores.
152
153// cloneFlags = _CLONE_VM | /* share memory */
154// _CLONE_FS | /* share cwd, etc */
155// _CLONE_FILES | /* share fd table */
156// _CLONE_SIGHAND | /* share sig handler table */
157// _CLONE_SYSVSEM | /* share SysV semaphore undo lists (see issue #20763) */
158// _CLONE_THREAD /* revisit - okay for now */
159// )
160
161// //go:noescape
162// func clone(flags int32, stk, mp, gp, fn unsafe.Pointer) int32
163
164// // May run with m.p==nil, so write barriers are not allowed.
165// //
166// //go:nowritebarrier
167// func newosproc(mp *m) {
168// stk := unsafe.Pointer(mp.g0.stack.hi)
169// /*
170// * note: strace gets confused if we use CLONE_PTRACE here.
171// */
172// if false {
173// print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " clone=", abi.FuncPCABI0(clone), " id=", mp.id, " ostk=", &mp, "\n")
174// }
175
176// // Disable signals during clone, so that the new thread starts
177// // with signals disabled. It will enable them in minit.
178// var oset sigset
179// sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
180// ret := retryOnEAGAIN(func() int32 {
181// r := clone(cloneFlags, stk, unsafe.Pointer(mp), unsafe.Pointer(mp.g0), unsafe.Pointer(abi.FuncPCABI0(mstart)))
182// // clone returns positive TID, negative errno.
183// // We don't care about the TID.
184// if r >= 0 {
185// return 0
186// }
187// return -r
188// })
189// sigprocmask(_SIG_SETMASK, &oset, nil)
190
191// if ret != 0 {
192// print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", ret, ")\n")
193// if ret == _EAGAIN {
194// println("runtime: may need to increase max user processes (ulimit -u)")
195// }
196// throw("newosproc")
197// }
198// }
199
200// // Version of newosproc that doesn't require a valid G.
201// //
202// //go:nosplit
203// func newosproc0(stacksize uintptr, fn unsafe.Pointer) {
204// stack := sysAlloc(stacksize, &memstats.stacks_sys)
205// if stack == nil {
206// writeErrStr(failallocatestack)
207// exit(1)
208// }
209// ret := clone(cloneFlags, unsafe.Pointer(uintptr(stack)+stacksize), nil, nil, fn)
210// if ret < 0 {
211// writeErrStr(failthreadcreate)
212// exit(1)
213// }
214// }
215
216// const (
217// _AT_NULL = 0 // End of vector
218// _AT_PAGESZ = 6 // System physical page size
219// _AT_HWCAP = 16 // hardware capability bit vector
220// _AT_SECURE = 23 // secure mode boolean
221// _AT_RANDOM = 25 // introduced in 2.6.29
222// _AT_HWCAP2 = 26 // hardware capability bit vector 2
223// )
224
225// var procAuxv = []byte("/proc/self/auxv\x00")
226
227// var addrspace_vec [1]byte
228
229// func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
230
231// func sysargs(argc int32, argv **byte) {
232// n := argc + 1
233
234// // skip over argv, envp to get to auxv
235// for argv_index(argv, n) != nil {
236// n++
237// }
238
239// // skip NULL separator
240// n++
241
242// // now argv+n is auxv
243// auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*goarch.PtrSize))
244// if sysauxv(auxv[:]) != 0 {
245// return
246// }
247// // In some situations we don't get a loader-provided
248// // auxv, such as when loaded as a library on Android.
249// // Fall back to /proc/self/auxv.
250// fd := open(&procAuxv[0], 0 /* O_RDONLY */, 0)
251// if fd < 0 {
252// // On Android, /proc/self/auxv might be unreadable (issue 9229), so we fallback to
253// // try using mincore to detect the physical page size.
254// // mincore should return EINVAL when address is not a multiple of system page size.
255// const size = 256 << 10 // size of memory region to allocate
256// p, err := mmap(nil, size, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
257// if err != 0 {
258// return
259// }
260// var n uintptr
261// for n = 4 << 10; n < size; n <<= 1 {
262// err := mincore(unsafe.Pointer(uintptr(p)+n), 1, &addrspace_vec[0])
263// if err == 0 {
264// physPageSize = n
265// break
266// }
267// }
268// if physPageSize == 0 {
269// physPageSize = size
270// }
271// munmap(p, size)
272// return
273// }
274// var buf [128]uintptr
275// n = read(fd, noescape(unsafe.Pointer(&buf[0])), int32(unsafe.Sizeof(buf)))
276// closefd(fd)
277// if n < 0 {
278// return
279// }
280// // Make sure buf is terminated, even if we didn't read
281// // the whole file.
282// buf[len(buf)-2] = _AT_NULL
283// sysauxv(buf[:])
284// }
285
286// // startupRandomData holds random bytes initialized at startup. These come from
287// // the ELF AT_RANDOM auxiliary vector.
288// var startupRandomData []byte
289
290// // secureMode holds the value of AT_SECURE passed in the auxiliary vector.
291// var secureMode bool
292
293// func sysauxv(auxv []uintptr) int {
294// var i int
295// for ; auxv[i] != _AT_NULL; i += 2 {
296// tag, val := auxv[i], auxv[i+1]
297// switch tag {
298// case _AT_RANDOM:
299// // The kernel provides a pointer to 16-bytes
300// // worth of random data.
301// startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:]
302
303// case _AT_PAGESZ:
304// physPageSize = val
305
306// case _AT_SECURE:
307// secureMode = val == 1
308// }
309
310// archauxv(tag, val)
311// vdsoauxv(tag, val)
312// }
313// return i / 2
314// }
315
316// var sysTHPSizePath = []byte("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size\x00")
317
318// func getHugePageSize() uintptr {
319// var numbuf [20]byte
320// fd := open(&sysTHPSizePath[0], 0 /* O_RDONLY */, 0)
321// if fd < 0 {
322// return 0
323// }
324// ptr := noescape(unsafe.Pointer(&numbuf[0]))
325// n := read(fd, ptr, int32(len(numbuf)))
326// closefd(fd)
327// if n <= 0 {
328// return 0
329// }
330// n-- // remove trailing newline
331// v, ok := atoi(slicebytetostringtmp((*byte)(ptr), int(n)))
332// if !ok || v < 0 {
333// v = 0
334// }
335// if v&(v-1) != 0 {
336// // v is not a power of 2
337// return 0
338// }
339// return uintptr(v)
340// }
341
342// func osinit() {
343// ncpu = getproccount()
344// physHugePageSize = getHugePageSize()
345// if iscgo {
346// // #42494 glibc and musl reserve some signals for
347// // internal use and require they not be blocked by
348// // the rest of a normal C runtime. When the go runtime
349// // blocks...unblocks signals, temporarily, the blocked
350// // interval of time is generally very short. As such,
351// // these expectations of *libc code are mostly met by
352// // the combined go+cgo system of threads. However,
353// // when go causes a thread to exit, via a return from
354// // mstart(), the combined runtime can deadlock if
355// // these signals are blocked. Thus, don't block these
356// // signals when exiting threads.
357// // - glibc: SIGCANCEL (32), SIGSETXID (33)
358// // - musl: SIGTIMER (32), SIGCANCEL (33), SIGSYNCCALL (34)
359// sigdelset(&sigsetAllExiting, 32)
360// sigdelset(&sigsetAllExiting, 33)
361// sigdelset(&sigsetAllExiting, 34)
362// }
363// osArchInit()
364// }
365
366// var urandom_dev = []byte("/dev/urandom\x00")
367
368pub fn get_random_data(r: &mut [u8]) {
369 // if startupRandomData != nil {
370 // n := copy(r, startupRandomData)
371 // extendRandom(r, n)
372 // return
373 // }
374 match &mut std::fs::File::open("/dev/urandom") {
375 Err(err) => panic!("failed to open /dev/urandom: {}", err),
376 Ok(f) => {
377 if let Err(err) = f.read_exact(r) {
378 panic!("failed to open /dev/urandom: {}", err);
379 }
380 }
381 }
382 // fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
383 // n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
384 // closefd(fd)
385 // extendRandom(r, int(n))
386}
387
388// func goenvs() {
389// goenvs_unix()
390// }
391
392// // Called to do synchronous initialization of Go code built with
393// // -buildmode=c-archive or -buildmode=c-shared.
394// // None of the Go runtime is initialized.
395// //
396// //go:nosplit
397// //go:nowritebarrierrec
398// func libpreinit() {
399// initsig(true)
400// }
401
402// // Called to initialize a new m (including the bootstrap m).
403// // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
404// func mpreinit(mp *m) {
405// mp.gsignal = malg(32 * 1024) // Linux wants >= 2K
406// mp.gsignal.m = mp
407// }
408
409// func gettid() uint32
410
411// // Called to initialize a new m (including the bootstrap m).
412// // Called on the new thread, cannot allocate memory.
413// func minit() {
414// minitSignals()
415
416// // Cgo-created threads and the bootstrap m are missing a
417// // procid. We need this for asynchronous preemption and it's
418// // useful in debuggers.
419// getg().m.procid = uint64(gettid())
420// }
421
422// // Called from dropm to undo the effect of an minit.
423// //
424// //go:nosplit
425// func unminit() {
426// unminitSignals()
427// }
428
429// // Called from exitm, but not from drop, to undo the effect of thread-owned
430// // resources in minit, semacreate, or elsewhere. Do not take locks after calling this.
431// func mdestroy(mp *m) {
432// }
433
434// //#ifdef GOARCH_386
435// //#define sa_handler k_sa_handler
436// //#endif
437
438// func sigreturn()
439// func sigtramp() // Called via C ABI
440// func cgoSigtramp()
441
442// //go:noescape
443// func sigaltstack(new, old *stackt)
444
445// //go:noescape
446// func setitimer(mode int32, new, old *itimerval)
447
448// //go:noescape
449// func timer_create(clockid int32, sevp *sigevent, timerid *int32) int32
450
451// //go:noescape
452// func timer_settime(timerid int32, flags int32, new, old *itimerspec) int32
453
454// //go:noescape
455// func timer_delete(timerid int32) int32
456
457// //go:noescape
458// func rtsigprocmask(how int32, new, old *sigset, size int32)
459
460// //go:nosplit
461// //go:nowritebarrierrec
462// func sigprocmask(how int32, new, old *sigset) {
463// rtsigprocmask(how, new, old, int32(unsafe.Sizeof(*new)))
464// }
465
466// func raise(sig uint32)
467// func raiseproc(sig uint32)
468
469// //go:noescape
470// func sched_getaffinity(pid, len uintptr, buf *byte) int32
471// func osyield()
472
473// //go:nosplit
474// func osyield_no_g() {
475// osyield()
476// }
477
478// func pipe2(flags int32) (r, w int32, errno int32)
479
480// //go:nosplit
481// func fcntl(fd, cmd, arg int32) (ret int32, errno int32) {
482// r, _, err := syscall.Syscall6(syscall.SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg), 0, 0, 0)
483// return int32(r), int32(err)
484// }
485
486// const (
487// _si_max_size = 128
488// _sigev_max_size = 64
489// )
490
491// //go:nosplit
492// //go:nowritebarrierrec
493// func setsig(i uint32, fn uintptr) {
494// var sa sigactiont
495// sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTORER | _SA_RESTART
496// sigfillset(&sa.sa_mask)
497// // Although Linux manpage says "sa_restorer element is obsolete and
498// // should not be used". x86_64 kernel requires it. Only use it on
499// // x86.
500// if GOARCH == "386" || GOARCH == "amd64" {
501// sa.sa_restorer = abi.FuncPCABI0(sigreturn)
502// }
503// if fn == abi.FuncPCABIInternal(sighandler) { // abi.FuncPCABIInternal(sighandler) matches the callers in signal_unix.go
504// if iscgo {
505// fn = abi.FuncPCABI0(cgoSigtramp)
506// } else {
507// fn = abi.FuncPCABI0(sigtramp)
508// }
509// }
510// sa.sa_handler = fn
511// sigaction(i, &sa, nil)
512// }
513
514// //go:nosplit
515// //go:nowritebarrierrec
516// func setsigstack(i uint32) {
517// var sa sigactiont
518// sigaction(i, nil, &sa)
519// if sa.sa_flags&_SA_ONSTACK != 0 {
520// return
521// }
522// sa.sa_flags |= _SA_ONSTACK
523// sigaction(i, &sa, nil)
524// }
525
526// //go:nosplit
527// //go:nowritebarrierrec
528// func getsig(i uint32) uintptr {
529// var sa sigactiont
530// sigaction(i, nil, &sa)
531// return sa.sa_handler
532// }
533
534// // setSignalstackSP sets the ss_sp field of a stackt.
535// //
536// //go:nosplit
537// func setSignalstackSP(s *stackt, sp uintptr) {
538// *(*uintptr)(unsafe.Pointer(&s.ss_sp)) = sp
539// }
540
541// //go:nosplit
542// func (c *sigctxt) fixsigcode(sig uint32) {
543// }
544
545// // sysSigaction calls the rt_sigaction system call.
546// //
547// //go:nosplit
548// func sysSigaction(sig uint32, new, old *sigactiont) {
549// if rt_sigaction(uintptr(sig), new, old, unsafe.Sizeof(sigactiont{}.sa_mask)) != 0 {
550// // Workaround for bugs in QEMU user mode emulation.
551// //
552// // QEMU turns calls to the sigaction system call into
553// // calls to the C library sigaction call; the C
554// // library call rejects attempts to call sigaction for
555// // SIGCANCEL (32) or SIGSETXID (33).
556// //
557// // QEMU rejects calling sigaction on SIGRTMAX (64).
558// //
559// // Just ignore the error in these case. There isn't
560// // anything we can do about it anyhow.
561// if sig != 32 && sig != 33 && sig != 64 {
562// // Use system stack to avoid split stack overflow on ppc64/ppc64le.
563// systemstack(func() {
564// throw("sigaction failed")
565// })
566// }
567// }
568// }
569
570// // rt_sigaction is implemented in assembly.
571// //
572// //go:noescape
573// func rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32
574
575// func getpid() int
576// func tgkill(tgid, tid, sig int)
577
578// // signalM sends a signal to mp.
579// func signalM(mp *m, sig int) {
580// tgkill(getpid(), int(mp.procid), sig)
581// }
582
583// // go118UseTimerCreateProfiler enables the per-thread CPU profiler.
584// const go118UseTimerCreateProfiler = true
585
586// // validSIGPROF compares this signal delivery's code against the signal sources
587// // that the profiler uses, returning whether the delivery should be processed.
588// // To be processed, a signal delivery from a known profiling mechanism should
589// // correspond to the best profiling mechanism available to this thread. Signals
590// // from other sources are always considered valid.
591// //
592// //go:nosplit
593// func validSIGPROF(mp *m, c *sigctxt) bool {
594// code := int32(c.sigcode())
595// setitimer := code == _SI_KERNEL
596// timer_create := code == _SI_TIMER
597
598// if !(setitimer || timer_create) {
599// // The signal doesn't correspond to a profiling mechanism that the
600// // runtime enables itself. There's no reason to process it, but there's
601// // no reason to ignore it either.
602// return true
603// }
604
605// if mp == nil {
606// // Since we don't have an M, we can't check if there's an active
607// // per-thread timer for this thread. We don't know how long this thread
608// // has been around, and if it happened to interact with the Go scheduler
609// // at a time when profiling was active (causing it to have a per-thread
610// // timer). But it may have never interacted with the Go scheduler, or
611// // never while profiling was active. To avoid double-counting, process
612// // only signals from setitimer.
613// //
614// // When a custom cgo traceback function has been registered (on
615// // platforms that support runtime.SetCgoTraceback), SIGPROF signals
616// // delivered to a thread that cannot find a matching M do this check in
617// // the assembly implementations of runtime.cgoSigtramp.
618// return setitimer
619// }
620
621// // Having an M means the thread interacts with the Go scheduler, and we can
622// // check whether there's an active per-thread timer for this thread.
623// if mp.profileTimerValid.Load() {
624// // If this M has its own per-thread CPU profiling interval timer, we
625// // should track the SIGPROF signals that come from that timer (for
626// // accurate reporting of its CPU usage; see issue 35057) and ignore any
627// // that it gets from the process-wide setitimer (to not over-count its
628// // CPU consumption).
629// return timer_create
630// }
631
632// // No active per-thread timer means the only valid profiler is setitimer.
633// return setitimer
634// }
635
636// func setProcessCPUProfiler(hz int32) {
637// setProcessCPUProfilerTimer(hz)
638// }
639
640// func setThreadCPUProfiler(hz int32) {
641// mp := getg().m
642// mp.profilehz = hz
643
644// if !go118UseTimerCreateProfiler {
645// return
646// }
647
648// // destroy any active timer
649// if mp.profileTimerValid.Load() {
650// timerid := mp.profileTimer
651// mp.profileTimerValid.Store(false)
652// mp.profileTimer = 0
653
654// ret := timer_delete(timerid)
655// if ret != 0 {
656// print("runtime: failed to disable profiling timer; timer_delete(", timerid, ") errno=", -ret, "\n")
657// throw("timer_delete")
658// }
659// }
660
661// if hz == 0 {
662// // If the goal was to disable profiling for this thread, then the job's done.
663// return
664// }
665
666// // The period of the timer should be 1/Hz. For every "1/Hz" of additional
667// // work, the user should expect one additional sample in the profile.
668// //
669// // But to scale down to very small amounts of application work, to observe
670// // even CPU usage of "one tenth" of the requested period, set the initial
671// // timing delay in a different way: So that "one tenth" of a period of CPU
672// // spend shows up as a 10% chance of one sample (for an expected value of
673// // 0.1 samples), and so that "two and six tenths" periods of CPU spend show
674// // up as a 60% chance of 3 samples and a 40% chance of 2 samples (for an
675// // expected value of 2.6). Set the initial delay to a value in the unifom
676// // random distribution between 0 and the desired period. And because "0"
677// // means "disable timer", add 1 so the half-open interval [0,period) turns
678// // into (0,period].
679// //
680// // Otherwise, this would show up as a bias away from short-lived threads and
681// // from threads that are only occasionally active: for example, when the
682// // garbage collector runs on a mostly-idle system, the additional threads it
683// // activates may do a couple milliseconds of GC-related work and nothing
684// // else in the few seconds that the profiler observes.
685// spec := new(itimerspec)
686// spec.it_value.setNsec(1 + int64(fastrandn(uint32(1e9/hz))))
687// spec.it_interval.setNsec(1e9 / int64(hz))
688
689// var timerid int32
690// var sevp sigevent
691// sevp.notify = _SIGEV_THREAD_ID
692// sevp.signo = _SIGPROF
693// sevp.sigev_notify_thread_id = int32(mp.procid)
694// ret := timer_create(_CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid)
695// if ret != 0 {
696// // If we cannot create a timer for this M, leave profileTimerValid false
697// // to fall back to the process-wide setitimer profiler.
698// return
699// }
700
701// ret = timer_settime(timerid, 0, spec, nil)
702// if ret != 0 {
703// print("runtime: failed to configure profiling timer; timer_settime(", timerid,
704// ", 0, {interval: {",
705// spec.it_interval.tv_sec, "s + ", spec.it_interval.tv_nsec, "ns} value: {",
706// spec.it_value.tv_sec, "s + ", spec.it_value.tv_nsec, "ns}}, nil) errno=", -ret, "\n")
707// throw("timer_settime")
708// }
709
710// mp.profileTimer = timerid
711// mp.profileTimerValid.Store(true)
712// }
713
714// // perThreadSyscallArgs contains the system call number, arguments, and
715// // expected return values for a system call to be executed on all threads.
716// type perThreadSyscallArgs struct {
717// trap uintptr
718// a1 uintptr
719// a2 uintptr
720// a3 uintptr
721// a4 uintptr
722// a5 uintptr
723// a6 uintptr
724// r1 uintptr
725// r2 uintptr
726// }
727
728// // perThreadSyscall is the system call to execute for the ongoing
729// // doAllThreadsSyscall.
730// //
731// // perThreadSyscall may only be written while mp.needPerThreadSyscall == 0 on
732// // all Ms.
733// var perThreadSyscall perThreadSyscallArgs
734
735// // syscall_runtime_doAllThreadsSyscall and executes a specified system call on
736// // all Ms.
737// //
738// // The system call is expected to succeed and return the same value on every
739// // thread. If any threads do not match, the runtime throws.
740// //
741// //go:linkname syscall_runtime_doAllThreadsSyscall syscall.runtime_doAllThreadsSyscall
742// //go:uintptrescapes
743// func syscall_runtime_doAllThreadsSyscall(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
744// if iscgo {
745// // In cgo, we are not aware of threads created in C, so this approach will not work.
746// panic("doAllThreadsSyscall not supported with cgo enabled")
747// }
748
749// // STW to guarantee that user goroutines see an atomic change to thread
750// // state. Without STW, goroutines could migrate Ms while change is in
751// // progress and e.g., see state old -> new -> old -> new.
752// //
753// // N.B. Internally, this function does not depend on STW to
754// // successfully change every thread. It is only needed for user
755// // expectations, per above.
756// stopTheWorld("doAllThreadsSyscall")
757
758// // This function depends on several properties:
759// //
760// // 1. All OS threads that already exist are associated with an M in
761// // allm. i.e., we won't miss any pre-existing threads.
762// // 2. All Ms listed in allm will eventually have an OS thread exist.
763// // i.e., they will set procid and be able to receive signals.
764// // 3. OS threads created after we read allm will clone from a thread
765// // that has executed the system call. i.e., they inherit the
766// // modified state.
767// //
768// // We achieve these through different mechanisms:
769// //
770// // 1. Addition of new Ms to allm in allocm happens before clone of its
771// // OS thread later in newm.
772// // 2. newm does acquirem to avoid being preempted, ensuring that new Ms
773// // created in allocm will eventually reach OS thread clone later in
774// // newm.
775// // 3. We take allocmLock for write here to prevent allocation of new Ms
776// // while this function runs. Per (1), this prevents clone of OS
777// // threads that are not yet in allm.
778// allocmLock.lock()
779
780// // Disable preemption, preventing us from changing Ms, as we handle
781// // this M specially.
782// //
783// // N.B. STW and lock() above do this as well, this is added for extra
784// // clarity.
785// acquirem()
786
787// // N.B. allocmLock also prevents concurrent execution of this function,
788// // serializing use of perThreadSyscall, mp.needPerThreadSyscall, and
789// // ensuring all threads execute system calls from multiple calls in the
790// // same order.
791
792// r1, r2, errno := syscall.Syscall6(trap, a1, a2, a3, a4, a5, a6)
793// if GOARCH == "ppc64" || GOARCH == "ppc64le" {
794// // TODO(https://go.dev/issue/51192 ): ppc64 doesn't use r2.
795// r2 = 0
796// }
797// if errno != 0 {
798// releasem(getg().m)
799// allocmLock.unlock()
800// startTheWorld()
801// return r1, r2, errno
802// }
803
804// perThreadSyscall = perThreadSyscallArgs{
805// trap: trap,
806// a1: a1,
807// a2: a2,
808// a3: a3,
809// a4: a4,
810// a5: a5,
811// a6: a6,
812// r1: r1,
813// r2: r2,
814// }
815
816// // Wait for all threads to start.
817// //
818// // As described above, some Ms have been added to allm prior to
819// // allocmLock, but not yet completed OS clone and set procid.
820// //
821// // At minimum we must wait for a thread to set procid before we can
822// // send it a signal.
823// //
824// // We take this one step further and wait for all threads to start
825// // before sending any signals. This prevents system calls from getting
826// // applied twice: once in the parent and once in the child, like so:
827// //
828// // A B C
829// // add C to allm
830// // doAllThreadsSyscall
831// // allocmLock.lock()
832// // signal B
833// // <receive signal>
834// // execute syscall
835// // <signal return>
836// // clone C
837// // <thread start>
838// // set procid
839// // signal C
840// // <receive signal>
841// // execute syscall
842// // <signal return>
843// //
844// // In this case, thread C inherited the syscall-modified state from
845// // thread B and did not need to execute the syscall, but did anyway
846// // because doAllThreadsSyscall could not be sure whether it was
847// // required.
848// //
849// // Some system calls may not be idempotent, so we ensure each thread
850// // executes the system call exactly once.
851// for mp := allm; mp != nil; mp = mp.alllink {
852// for atomic.Load64(&mp.procid) == 0 {
853// // Thread is starting.
854// osyield()
855// }
856// }
857
858// // Signal every other thread, where they will execute perThreadSyscall
859// // from the signal handler.
860// gp := getg()
861// tid := gp.m.procid
862// for mp := allm; mp != nil; mp = mp.alllink {
863// if atomic.Load64(&mp.procid) == tid {
864// // Our thread already performed the syscall.
865// continue
866// }
867// mp.needPerThreadSyscall.Store(1)
868// signalM(mp, sigPerThreadSyscall)
869// }
870
871// // Wait for all threads to complete.
872// for mp := allm; mp != nil; mp = mp.alllink {
873// if mp.procid == tid {
874// continue
875// }
876// for mp.needPerThreadSyscall.Load() != 0 {
877// osyield()
878// }
879// }
880
881// perThreadSyscall = perThreadSyscallArgs{}
882
883// releasem(getg().m)
884// allocmLock.unlock()
885// startTheWorld()
886
887// return r1, r2, errno
888// }
889
890// // runPerThreadSyscall runs perThreadSyscall for this M if required.
891// //
892// // This function throws if the system call returns with anything other than the
893// // expected values.
894// //
895// //go:nosplit
896// func runPerThreadSyscall() {
897// gp := getg()
898// if gp.m.needPerThreadSyscall.Load() == 0 {
899// return
900// }
901
902// args := perThreadSyscall
903// r1, r2, errno := syscall.Syscall6(args.trap, args.a1, args.a2, args.a3, args.a4, args.a5, args.a6)
904// if GOARCH == "ppc64" || GOARCH == "ppc64le" {
905// // TODO(https://go.dev/issue/51192 ): ppc64 doesn't use r2.
906// r2 = 0
907// }
908// if errno != 0 || r1 != args.r1 || r2 != args.r2 {
909// print("trap:", args.trap, ", a123456=[", args.a1, ",", args.a2, ",", args.a3, ",", args.a4, ",", args.a5, ",", args.a6, "]\n")
910// print("results: got {r1=", r1, ",r2=", r2, ",errno=", errno, "}, want {r1=", args.r1, ",r2=", args.r2, ",errno=0}\n")
911// fatal("AllThreadsSyscall6 results differ between threads; runtime corrupted")
912// }
913
914// gp.m.needPerThreadSyscall.Store(0)
915// }
916
917// const (
918// _SI_USER = 0
919// _SI_TKILL = -6
920// )
921
922// // sigFromUser reports whether the signal was sent because of a call
923// // to kill or tgkill.
924// //
925// //go:nosplit
926// func (c *sigctxt) sigFromUser() bool {
927// code := int32(c.sigcode())
928// return code == _SI_USER || code == _SI_TKILL
929// }