]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/os1_freebsd.go
[dev.garbage] all: merge dev.cc into dev.garbage
[gostls13.git] / src / runtime / os1_freebsd.go
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 FreeBSD'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 // FreeBSD's umtx_op syscall is effectively the same as Linux's futex, and
30 // thus the code is largely similar. See Linux implementation
31 // and lock_futex.c for comments.
32
33 //go:nosplit
34 func futexsleep(addr *uint32, val uint32, ns int64) {
35         systemstack(func() {
36                 futexsleep1(addr, val, ns)
37         })
38 }
39
40 func futexsleep1(addr *uint32, val uint32, ns int64) {
41         var tsp *timespec
42         if ns >= 0 {
43                 var ts timespec
44                 ts.tv_nsec = 0
45                 ts.set_sec(int64(timediv(ns, 1000000000, (*int32)(unsafe.Pointer(&ts.tv_nsec)))))
46                 tsp = &ts
47         }
48         ret := sys_umtx_op(addr, _UMTX_OP_WAIT_UINT_PRIVATE, val, nil, tsp)
49         if ret >= 0 || ret == -_EINTR {
50                 return
51         }
52         print("umtx_wait addr=", addr, " val=", val, " ret=", ret, "\n")
53         *(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005
54 }
55
56 //go:nosplit
57 func futexwakeup(addr *uint32, cnt uint32) {
58         ret := sys_umtx_op(addr, _UMTX_OP_WAKE_PRIVATE, cnt, nil, nil)
59         if ret >= 0 {
60                 return
61         }
62
63         systemstack(func() {
64                 print("umtx_wake_addr=", addr, " ret=", ret, "\n")
65         })
66 }
67
68 func thr_start()
69
70 func newosproc(mp *m, stk unsafe.Pointer) {
71         if false {
72                 print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " thr_start=", funcPC(thr_start), " id=", mp.id, "/", mp.tls[0], " ostk=", &mp, "\n")
73         }
74
75         // NOTE(rsc): This code is confused. stackbase is the top of the stack
76         // and is equal to stk. However, it's working, so I'm not changing it.
77         param := thrparam{
78                 start_func: funcPC(thr_start),
79                 arg:        unsafe.Pointer(mp),
80                 stack_base: mp.g0.stack.hi,
81                 stack_size: uintptr(stk) - mp.g0.stack.hi,
82                 child_tid:  unsafe.Pointer(&mp.procid),
83                 parent_tid: nil,
84                 tls_base:   unsafe.Pointer(&mp.tls[0]),
85                 tls_size:   unsafe.Sizeof(mp.tls),
86         }
87         mp.tls[0] = uintptr(mp.id) // so 386 asm can find it
88
89         var oset sigset
90         sigprocmask(&sigset_all, &oset)
91         thr_new(&param, int32(unsafe.Sizeof(param)))
92         sigprocmask(&oset, nil)
93 }
94
95 func osinit() {
96         ncpu = getncpu()
97 }
98
99 var urandom_data [_HashRandomBytes]byte
100 var urandom_dev = []byte("/dev/random\x00")
101
102 //go:nosplit
103 func get_random_data(rnd *unsafe.Pointer, rnd_len *int32) {
104         fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
105         if read(fd, unsafe.Pointer(&urandom_data), _HashRandomBytes) == _HashRandomBytes {
106                 *rnd = unsafe.Pointer(&urandom_data[0])
107                 *rnd_len = _HashRandomBytes
108         } else {
109                 *rnd = nil
110                 *rnd_len = 0
111         }
112         close(fd)
113 }
114
115 func goenvs() {
116         goenvs_unix()
117 }
118
119 // Called to initialize a new m (including the bootstrap m).
120 // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
121 func mpreinit(mp *m) {
122         mp.gsignal = malg(32 * 1024)
123         mp.gsignal.m = mp
124 }
125
126 // Called to initialize a new m (including the bootstrap m).
127 // Called on the new thread, can not allocate memory.
128 func minit() {
129         _g_ := getg()
130
131         // m.procid is a uint64, but thr_new writes a uint32 on 32-bit systems.
132         // Fix it up. (Only matters on big-endian, but be clean anyway.)
133         if ptrSize == 4 {
134                 _g_.m.procid = uint64(*(*uint32)(unsafe.Pointer(&_g_.m.procid)))
135         }
136
137         // Initialize signal handling.
138         signalstack((*byte)(unsafe.Pointer(_g_.m.gsignal.stack.lo)), 32*1024)
139         sigprocmask(&sigset_none, nil)
140 }
141
142 // Called from dropm to undo the effect of an minit.
143 func unminit() {
144         signalstack(nil, 0)
145 }
146
147 func memlimit() uintptr {
148         /*
149                 TODO: Convert to Go when something actually uses the result.
150                 Rlimit rl;
151                 extern byte runtime·text[], runtime·end[];
152                 uintptr used;
153
154                 if(runtime·getrlimit(RLIMIT_AS, &rl) != 0)
155                         return 0;
156                 if(rl.rlim_cur >= 0x7fffffff)
157                         return 0;
158
159                 // Estimate our VM footprint excluding the heap.
160                 // Not an exact science: use size of binary plus
161                 // some room for thread stacks.
162                 used = runtime·end - runtime·text + (64<<20);
163                 if(used >= rl.rlim_cur)
164                         return 0;
165
166                 // If there's not at least 16 MB left, we're probably
167                 // not going to be able to do much.  Treat as no limit.
168                 rl.rlim_cur -= used;
169                 if(rl.rlim_cur < (16<<20))
170                         return 0;
171
172                 return rl.rlim_cur - used;
173         */
174
175         return 0
176 }
177
178 func sigtramp()
179
180 type sigactiont struct {
181         sa_handler uintptr
182         sa_flags   int32
183         sa_mask    sigset
184 }
185
186 func setsig(i int32, fn uintptr, restart bool) {
187         var sa sigactiont
188         sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK
189         if restart {
190                 sa.sa_flags |= _SA_RESTART
191         }
192         sa.sa_mask = sigset_all
193         if fn == funcPC(sighandler) {
194                 fn = funcPC(sigtramp)
195         }
196         sa.sa_handler = fn
197         sigaction(i, &sa, nil)
198 }
199 func getsig(i int32) uintptr {
200         var sa sigactiont
201         sigaction(i, nil, &sa)
202         if sa.sa_handler == funcPC(sigtramp) {
203                 return funcPC(sighandler)
204         }
205         return sa.sa_handler
206 }
207
208 func signalstack(p *byte, n int32) {
209         var st stackt
210         st.ss_sp = uintptr(unsafe.Pointer(p))
211         st.ss_size = uintptr(n)
212         st.ss_flags = 0
213         if p == nil {
214                 st.ss_flags = _SS_DISABLE
215         }
216         sigaltstack(&st, nil)
217 }
218
219 func unblocksignals() {
220         sigprocmask(&sigset_none, nil)
221 }