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 "unsafe" 8 9 // From DragonFly's <sys/sysctl.h> 10 const ( 11 _CTL_HW = 6 12 _HW_NCPU = 3 13 ) 14 15 var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}} 16 17 func getncpu() int32 { 18 mib := [2]uint32{_CTL_HW, _HW_NCPU} 19 out := uint32(0) 20 nout := unsafe.Sizeof(out) 21 ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) 22 if ret >= 0 { 23 return int32(out) 24 } 25 return 1 26 } 27 28 //go:nosplit 29 func futexsleep(addr *uint32, val uint32, ns int64) { 30 systemstack(func() { 31 futexsleep1(addr, val, ns) 32 }) 33 } 34 35 func futexsleep1(addr *uint32, val uint32, ns int64) { 36 var timeout int32 37 if ns >= 0 { 38 // The timeout is specified in microseconds - ensure that we 39 // do not end up dividing to zero, which would put us to sleep 40 // indefinitely... 41 timeout = timediv(ns, 1000, nil) 42 if timeout == 0 { 43 timeout = 1 44 } 45 } 46 47 // sys_umtx_sleep will return EWOULDBLOCK (EAGAIN) when the timeout 48 // expires or EBUSY if the mutex value does not match. 49 ret := sys_umtx_sleep(addr, int32(val), timeout) 50 if ret >= 0 || ret == -_EINTR || ret == -_EAGAIN || ret == -_EBUSY { 51 return 52 } 53 54 print("umtx_sleep addr=", addr, " val=", val, " ret=", ret, "\n") 55 *(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005 56 } 57 58 //go:nosplit 59 func futexwakeup(addr *uint32, cnt uint32) { 60 ret := sys_umtx_wakeup(addr, int32(cnt)) 61 if ret >= 0 { 62 return 63 } 64 65 systemstack(func() { 66 print("umtx_wake_addr=", addr, " ret=", ret, "\n") 67 *(*int32)(unsafe.Pointer(uintptr(0x1006))) = 0x1006 68 }) 69 } 70 71 func lwp_start(uintptr) 72 73 // May run with m.p==nil, so write barriers are not allowed. 74 //go:nowritebarrier 75 func newosproc(mp *m, stk unsafe.Pointer) { 76 if false { 77 print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " lwp_start=", funcPC(lwp_start), " id=", mp.id, "/", mp.tls[0], " ostk=", &mp, "\n") 78 } 79 80 var oset sigset 81 sigprocmask(_SIG_SETMASK, &sigset_all, &oset) 82 83 params := lwpparams{ 84 start_func: funcPC(lwp_start), 85 arg: unsafe.Pointer(mp), 86 stack: uintptr(stk), 87 tid1: unsafe.Pointer(&mp.procid), 88 tid2: nil, 89 } 90 91 mp.tls[0] = uintptr(mp.id) // XXX so 386 asm can find it 92 93 lwp_create(¶ms) 94 sigprocmask(_SIG_SETMASK, &oset, nil) 95 } 96 97 func osinit() { 98 ncpu = getncpu() 99 } 100 101 var urandom_dev = []byte("/dev/urandom\x00") 102 103 //go:nosplit 104 func getRandomData(r []byte) { 105 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) 106 n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) 107 closefd(fd) 108 extendRandom(r, int(n)) 109 } 110 111 func goenvs() { 112 goenvs_unix() 113 } 114 115 // Called to initialize a new m (including the bootstrap m). 116 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 117 func mpreinit(mp *m) { 118 mp.gsignal = malg(32 * 1024) 119 mp.gsignal.m = mp 120 } 121 122 func msigsave(mp *m) { 123 smask := (*sigset)(unsafe.Pointer(&mp.sigmask)) 124 if unsafe.Sizeof(*smask) > unsafe.Sizeof(mp.sigmask) { 125 throw("insufficient storage for signal mask") 126 } 127 sigprocmask(_SIG_SETMASK, nil, smask) 128 } 129 130 // Called to initialize a new m (including the bootstrap m). 131 // Called on the new thread, can not allocate memory. 132 func minit() { 133 _g_ := getg() 134 135 // m.procid is a uint64, but lwp_start writes an int32. Fix it up. 136 _g_.m.procid = uint64(*(*int32)(unsafe.Pointer(&_g_.m.procid))) 137 138 // Initialize signal handling 139 signalstack(&_g_.m.gsignal.stack) 140 141 // restore signal mask from m.sigmask and unblock essential signals 142 nmask := *(*sigset)(unsafe.Pointer(&_g_.m.sigmask)) 143 for i := range sigtable { 144 if sigtable[i].flags&_SigUnblock != 0 { 145 nmask.__bits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31) 146 } 147 } 148 sigprocmask(_SIG_SETMASK, &nmask, nil) 149 } 150 151 // Called from dropm to undo the effect of an minit. 152 func unminit() { 153 _g_ := getg() 154 smask := (*sigset)(unsafe.Pointer(&_g_.m.sigmask)) 155 sigprocmask(_SIG_SETMASK, smask, nil) 156 signalstack(nil) 157 } 158 159 func memlimit() uintptr { 160 /* 161 TODO: Convert to Go when something actually uses the result. 162 163 Rlimit rl; 164 extern byte runtimetext[], runtimeend[]; 165 uintptr used; 166 167 if(runtimegetrlimit(RLIMIT_AS, &rl) != 0) 168 return 0; 169 if(rl.rlim_cur >= 0x7fffffff) 170 return 0; 171 172 // Estimate our VM footprint excluding the heap. 173 // Not an exact science: use size of binary plus 174 // some room for thread stacks. 175 used = runtimeend - runtimetext + (64<<20); 176 if(used >= rl.rlim_cur) 177 return 0; 178 179 // If there's not at least 16 MB left, we're probably 180 // not going to be able to do much. Treat as no limit. 181 rl.rlim_cur -= used; 182 if(rl.rlim_cur < (16<<20)) 183 return 0; 184 185 return rl.rlim_cur - used; 186 */ 187 return 0 188 } 189 190 func sigtramp() 191 192 type sigactiont struct { 193 sa_sigaction uintptr 194 sa_flags int32 195 sa_mask sigset 196 } 197 198 func setsig(i int32, fn uintptr, restart bool) { 199 var sa sigactiont 200 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK 201 if restart { 202 sa.sa_flags |= _SA_RESTART 203 } 204 sa.sa_mask = sigset_all 205 if fn == funcPC(sighandler) { 206 fn = funcPC(sigtramp) 207 } 208 sa.sa_sigaction = fn 209 sigaction(i, &sa, nil) 210 } 211 212 func setsigstack(i int32) { 213 throw("setsigstack") 214 } 215 216 func getsig(i int32) uintptr { 217 var sa sigactiont 218 sigaction(i, nil, &sa) 219 if sa.sa_sigaction == funcPC(sigtramp) { 220 return funcPC(sighandler) 221 } 222 return sa.sa_sigaction 223 } 224 225 func signalstack(s *stack) { 226 var st sigaltstackt 227 if s == nil { 228 st.ss_flags = _SS_DISABLE 229 } else { 230 st.ss_sp = s.lo 231 st.ss_size = s.hi - s.lo 232 st.ss_flags = 0 233 } 234 sigaltstack(&st, nil) 235 } 236 237 func updatesigmask(m sigmask) { 238 var mask sigset 239 copy(mask.__bits[:], m[:]) 240 sigprocmask(_SIG_SETMASK, &mask, nil) 241 } 242 243 func unblocksig(sig int32) { 244 var mask sigset 245 mask.__bits[(sig-1)/32] |= 1 << ((uint32(sig) - 1) & 31) 246 sigprocmask(_SIG_UNBLOCK, &mask, nil) 247 } 248