1
// Copyright 2009 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.
6
#include "defs_GOOS_GOARCH.h"
8
#include "signal_unix.h"
11
extern SigTab runtime·sigtab[];
13
static Sigset sigset_none;
14
static Sigset sigset_all = { ~(uint32)0, ~(uint32)0 };
18
// futexsleep(uint32 *addr, uint32 val)
19
// futexwakeup(uint32 *addr)
21
// Futexsleep atomically checks if *addr == val and if so, sleeps on addr.
22
// Futexwakeup wakes up threads sleeping on addr.
23
// Futexsleep is allowed to wake up spuriously.
32
// if(*addr == val) sleep
33
// Might be woken up spuriously; that's allowed.
34
// Don't sleep longer than ns; ns < 0 means forever.
36
runtime·futexsleep(uint32 *addr, uint32 val, int64 ns)
44
secs = ns/1000000000LL;
49
ts.tv_nsec = ns%1000000000LL;
53
// Some Linux kernels have a bug where futex of
54
// FUTEX_WAIT returns an internal error code
55
// as an errno. Libpthread ignores the return value
56
// here, and so can we: as it says a few lines up,
57
// spurious wakeups are allowed.
58
runtime·futex(addr, FUTEX_WAIT, val, tsp, nil, 0);
61
// If any procs are sleeping on addr, wake up at most cnt.
63
runtime·futexwakeup(uint32 *addr, uint32 cnt)
67
ret = runtime·futex(addr, FUTEX_WAKE, cnt, nil, nil, 0);
72
// I don't know that futex wakeup can return
73
// EAGAIN or EINTR, but if it does, it would be
74
// safe to loop and call futex again.
75
runtime·printf("futexwakeup addr=%p returned %D\n", addr, ret);
76
*(int32*)0x1006 = 0x1006;
79
extern runtime·sched_getaffinity(uintptr pid, uintptr len, uintptr *buf);
87
r = runtime·sched_getaffinity(0, sizeof(buf), buf);
89
for(i = 0; i < r/sizeof(buf[0]); i++) {
91
t = t - ((t >> 1) & 0x5555555555555555ULL);
92
t = (t & 0x3333333333333333ULL) + ((t >> 2) & 0x3333333333333333ULL);
93
cnt += (int32)((((t + (t >> 4)) & 0xF0F0F0F0F0F0F0FULL) * 0x101010101010101ULL) >> 56);
99
// Clone, the Linux rfork.
105
CLONE_SIGHAND = 0x800,
106
CLONE_PTRACE = 0x2000,
107
CLONE_VFORK = 0x4000,
108
CLONE_PARENT = 0x8000,
109
CLONE_THREAD = 0x10000,
110
CLONE_NEWNS = 0x20000,
111
CLONE_SYSVSEM = 0x40000,
112
CLONE_SETTLS = 0x80000,
113
CLONE_PARENT_SETTID = 0x100000,
114
CLONE_CHILD_CLEARTID = 0x200000,
115
CLONE_UNTRACED = 0x800000,
116
CLONE_CHILD_SETTID = 0x1000000,
117
CLONE_STOPPED = 0x2000000,
118
CLONE_NEWUTS = 0x4000000,
119
CLONE_NEWIPC = 0x8000000,
123
runtime·newosproc(M *mp, void *stk)
130
* note: strace gets confused if we use CLONE_PTRACE here.
132
flags = CLONE_VM /* share memory */
133
| CLONE_FS /* share cwd, etc */
134
| CLONE_FILES /* share fd table */
135
| CLONE_SIGHAND /* share sig handler table */
136
| CLONE_THREAD /* revisit - okay for now */
139
mp->tls[0] = mp->id; // so 386 asm can find it
141
runtime·printf("newosproc stk=%p m=%p g=%p clone=%p id=%d/%d ostk=%p\n",
142
stk, mp, mp->g0, runtime·clone, mp->id, (int32)mp->tls[0], &mp);
145
// Disable signals during clone, so that the new thread starts
146
// with signals disabled. It will enable them in minit.
147
runtime·rtsigprocmask(SIG_SETMASK, &sigset_all, &oset, sizeof oset);
148
ret = runtime·clone(flags, stk, mp, mp->g0, runtime·mstart);
149
runtime·rtsigprocmask(SIG_SETMASK, &oset, nil, sizeof oset);
152
runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), -ret);
153
runtime·throw("runtime.newosproc");
160
runtime·ncpu = getproccount();
163
// Random bytes initialized at startup. These come
164
// from the ELF AT_RANDOM auxiliary vector (vdso_linux_amd64.c).
165
byte* runtime·startup_random_data;
166
uint32 runtime·startup_random_data_len;
169
runtime·get_random_data(byte **rnd, int32 *rnd_len)
171
if(runtime·startup_random_data != nil) {
172
*rnd = runtime·startup_random_data;
173
*rnd_len = runtime·startup_random_data_len;
175
static byte urandom_data[HashRandomBytes];
177
fd = runtime·open("/dev/urandom", 0 /* O_RDONLY */, 0);
178
if(runtime·read(fd, urandom_data, HashRandomBytes) == HashRandomBytes) {
180
*rnd_len = HashRandomBytes;
192
runtime·goenvs_unix();
195
// Called to initialize a new m (including the bootstrap m).
196
// Called on the parent thread (main thread in case of bootstrap), can allocate memory.
198
runtime·mpreinit(M *mp)
200
mp->gsignal = runtime·malg(32*1024); // OS X wants >=8K, Linux >=2K
203
// Called to initialize a new m (including the bootstrap m).
204
// Called on the new thread, can not allocate memory.
208
// Initialize signal handling.
209
runtime·signalstack((byte*)m->gsignal->stackguard - StackGuard, 32*1024);
210
runtime·rtsigprocmask(SIG_SETMASK, &sigset_none, nil, sizeof(Sigset));
213
// Called from dropm to undo the effect of an minit.
215
runtime·unminit(void)
217
runtime·signalstack(nil, 0);
221
runtime·sigpanic(void)
225
if(g->sigcode0 == BUS_ADRERR && g->sigcode1 < 0x1000) {
227
runtime·panicstring("call of nil func value");
228
runtime·panicstring("invalid memory address or nil pointer dereference");
230
runtime·printf("unexpected fault address %p\n", g->sigcode1);
231
runtime·throw("fault");
233
if((g->sigcode0 == 0 || g->sigcode0 == SEGV_MAPERR || g->sigcode0 == SEGV_ACCERR) && g->sigcode1 < 0x1000) {
235
runtime·panicstring("call of nil func value");
236
runtime·panicstring("invalid memory address or nil pointer dereference");
238
runtime·printf("unexpected fault address %p\n", g->sigcode1);
239
runtime·throw("fault");
241
switch(g->sigcode0) {
243
runtime·panicstring("integer divide by zero");
245
runtime·panicstring("integer overflow");
247
runtime·panicstring("floating point error");
249
runtime·panicstring(runtime·sigtab[g->sig].name);
253
runtime·memlimit(void)
256
extern byte text[], end[];
259
if(runtime·getrlimit(RLIMIT_AS, &rl) != 0)
261
if(rl.rlim_cur >= 0x7fffffff)
264
// Estimate our VM footprint excluding the heap.
265
// Not an exact science: use size of binary plus
266
// some room for thread stacks.
267
used = end - text + (64<<20);
268
if(used >= rl.rlim_cur)
271
// If there's not at least 16 MB left, we're probably
272
// not going to be able to do much. Treat as no limit.
274
if(rl.rlim_cur < (16<<20))
277
return rl.rlim_cur - used;
281
runtime·setprof(bool on)
286
static int8 badcallback[] = "runtime: cgo callback on thread not created by Go.\n";
288
// This runs on a foreign stack, without an m or a g. No stack split.
291
runtime·badcallback(void)
293
runtime·write(2, badcallback, sizeof badcallback - 1);
296
static int8 badsignal[] = "runtime: signal received on thread not created by Go: ";
298
// This runs on a foreign stack, without an m or a g. No stack split.
301
runtime·badsignal(int32 sig)
305
if (sig == SIGPROF) {
306
return; // Ignore SIGPROFs intended for a non-Go thread.
308
runtime·write(2, badsignal, sizeof badsignal - 1);
309
if (0 <= sig && sig < NSIG) {
310
// Can't call findnull() because it will split stack.
311
for(len = 0; runtime·sigtab[sig].name[len]; len++)
313
runtime·write(2, runtime·sigtab[sig].name, len);
315
runtime·write(2, "\n", 1);
320
#define sa_handler k_sa_handler
324
* This assembler routine takes the args from registers, puts them on the stack,
325
* and calls sighandler().
327
extern void runtime·sigtramp(void);
328
extern void runtime·sigreturn(void); // calls runtime·sigreturn
331
runtime·setsig(int32 i, GoSighandler *fn, bool restart)
335
runtime·memclr((byte*)&sa, sizeof sa);
336
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
338
sa.sa_flags |= SA_RESTART;
340
// TODO(adonovan): Linux manpage says "sa_restorer element is
341
// obsolete and should not be used". Avoid it here, and test.
342
sa.sa_restorer = (void*)runtime·sigreturn;
343
if(fn == runtime·sighandler)
344
fn = (void*)runtime·sigtramp;
346
if(runtime·rt_sigaction(i, &sa, nil, sizeof(sa.sa_mask)) != 0)
347
runtime·throw("rt_sigaction failure");
351
runtime·getsig(int32 i)
355
runtime·memclr((byte*)&sa, sizeof sa);
356
if(runtime·rt_sigaction(i, nil, &sa, sizeof(sa.sa_mask)) != 0)
357
runtime·throw("rt_sigaction read failure");
358
if((void*)sa.sa_handler == runtime·sigtramp)
359
return runtime·sighandler;
360
return (void*)sa.sa_handler;
364
runtime·signalstack(byte *p, int32 n)
372
st.ss_flags = SS_DISABLE;
373
runtime·sigaltstack(&st, nil);