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