Mikio Hara | 4aeb0fc | 2013-03-20 02:40:29 +0900 | [diff] [blame] | 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. |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 4 | |
| 5 | #include "runtime.h" |
Russ Cox | 851f301 | 2011-12-16 15:33:58 -0500 | [diff] [blame] | 6 | #include "defs_GOOS_GOARCH.h" |
| 7 | #include "os_GOOS.h" |
Russ Cox | e9d62a6 | 2013-03-14 11:35:13 -0700 | [diff] [blame] | 8 | #include "signal_unix.h" |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 9 | #include "stack.h" |
Russ Cox | cb040d5 | 2014-09-04 23:05:18 -0400 | [diff] [blame] | 10 | #include "textflag.h" |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 11 | |
| 12 | enum |
| 13 | { |
| 14 | ESRCH = 3, |
| 15 | ENOTSUP = 91, |
| 16 | |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 17 | // From NetBSD's <sys/time.h> |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 18 | CLOCK_REALTIME = 0, |
| 19 | CLOCK_VIRTUAL = 1, |
| 20 | CLOCK_PROF = 2, |
| 21 | CLOCK_MONOTONIC = 3 |
| 22 | }; |
| 23 | |
| 24 | extern SigTab runtime·sigtab[]; |
| 25 | |
Russ Cox | c7f7bbb | 2013-02-15 12:18:33 -0500 | [diff] [blame] | 26 | static Sigset sigset_none; |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 27 | static Sigset sigset_all = { ~(uint32)0, ~(uint32)0, ~(uint32)0, ~(uint32)0, }; |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 28 | |
| 29 | extern void runtime·getcontext(UcontextT *context); |
| 30 | extern int32 runtime·lwp_create(UcontextT *context, uintptr flags, void *lwpid); |
Jingcheng Zhang | 70e967b | 2012-12-19 00:30:29 +0800 | [diff] [blame] | 31 | extern void runtime·lwp_mcontext_init(void *mc, void *stack, M *mp, G *gp, void (*fn)(void)); |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 32 | extern int32 runtime·lwp_park(Timespec *abstime, int32 unpark, void *hint, void *unparkhint); |
| 33 | extern int32 runtime·lwp_unpark(int32 lwp, void *hint); |
Joel Sing | deb93b0 | 2012-05-31 03:27:04 +1000 | [diff] [blame] | 34 | extern int32 runtime·lwp_self(void); |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 35 | |
| 36 | // From NetBSD's <sys/sysctl.h> |
| 37 | #define CTL_HW 6 |
| 38 | #define HW_NCPU 3 |
| 39 | |
| 40 | static int32 |
| 41 | getncpu(void) |
| 42 | { |
| 43 | uint32 mib[2]; |
| 44 | uint32 out; |
| 45 | int32 ret; |
| 46 | uintptr nout; |
| 47 | |
| 48 | // Fetch hw.ncpu via sysctl. |
| 49 | mib[0] = CTL_HW; |
| 50 | mib[1] = HW_NCPU; |
| 51 | nout = sizeof out; |
| 52 | out = 0; |
| 53 | ret = runtime·sysctl(mib, 2, (byte*)&out, &nout, nil, 0); |
| 54 | if(ret >= 0) |
| 55 | return out; |
| 56 | else |
| 57 | return 1; |
| 58 | } |
| 59 | |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 60 | #pragma textflag NOSPLIT |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 61 | uintptr |
| 62 | runtime·semacreate(void) |
| 63 | { |
| 64 | return 1; |
| 65 | } |
| 66 | |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 67 | static void |
| 68 | semasleep(void) |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 69 | { |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 70 | int64 ns; |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 71 | Timespec ts; |
| 72 | |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 73 | ns = (int64)(uint32)g->m->scalararg[0] | (int64)(uint32)g->m->scalararg[1]<<32; |
| 74 | g->m->scalararg[0] = 0; |
| 75 | g->m->scalararg[1] = 0; |
| 76 | |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 77 | // spin-mutex lock |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 78 | while(runtime·xchg(&g->m->waitsemalock, 1)) |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 79 | runtime·osyield(); |
| 80 | |
| 81 | for(;;) { |
| 82 | // lock held |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 83 | if(g->m->waitsemacount == 0) { |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 84 | // sleep until semaphore != 0 or timeout. |
| 85 | // thrsleep unlocks m->waitsemalock. |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 86 | if(ns < 0) { |
| 87 | // TODO(jsing) - potential deadlock! |
| 88 | // |
| 89 | // There is a potential deadlock here since we |
| 90 | // have to release the waitsemalock mutex |
| 91 | // before we call lwp_park() to suspend the |
| 92 | // thread. This allows another thread to |
| 93 | // release the lock and call lwp_unpark() |
| 94 | // before the thread is actually suspended. |
| 95 | // If this occurs the current thread will end |
| 96 | // up sleeping indefinitely. Unfortunately |
| 97 | // the NetBSD kernel does not appear to provide |
| 98 | // a mechanism for unlocking the userspace |
| 99 | // mutex once the thread is actually parked. |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 100 | runtime·atomicstore(&g->m->waitsemalock, 0); |
| 101 | runtime·lwp_park(nil, 0, &g->m->waitsemacount, nil); |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 102 | } else { |
Dmitriy Vyukov | 21315c34 | 2013-08-01 15:19:45 +0400 | [diff] [blame] | 103 | ns = ns + runtime·nanotime(); |
Russ Cox | 98cc58e | 2013-07-29 16:31:42 -0400 | [diff] [blame] | 104 | // NOTE: tv_nsec is int64 on amd64, so this assumes a little-endian system. |
Dmitriy Vyukov | e84d9e1 | 2013-07-29 22:22:34 +0400 | [diff] [blame] | 105 | ts.tv_nsec = 0; |
Russ Cox | 98cc58e | 2013-07-29 16:31:42 -0400 | [diff] [blame] | 106 | ts.tv_sec = runtime·timediv(ns, 1000000000, (int32*)&ts.tv_nsec); |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 107 | // TODO(jsing) - potential deadlock! |
| 108 | // See above for details. |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 109 | runtime·atomicstore(&g->m->waitsemalock, 0); |
| 110 | runtime·lwp_park(&ts, 0, &g->m->waitsemacount, nil); |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 111 | } |
| 112 | // reacquire lock |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 113 | while(runtime·xchg(&g->m->waitsemalock, 1)) |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 114 | runtime·osyield(); |
| 115 | } |
| 116 | |
| 117 | // lock held (again) |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 118 | if(g->m->waitsemacount != 0) { |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 119 | // semaphore is available. |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 120 | g->m->waitsemacount--; |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 121 | // spin-mutex unlock |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 122 | runtime·atomicstore(&g->m->waitsemalock, 0); |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 123 | g->m->scalararg[0] = 0; // semaphore acquired |
| 124 | return; |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // semaphore not available. |
| 128 | // if there is a timeout, stop now. |
| 129 | // otherwise keep trying. |
| 130 | if(ns >= 0) |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | // lock held but giving up |
| 135 | // spin-mutex unlock |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 136 | runtime·atomicstore(&g->m->waitsemalock, 0); |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 137 | g->m->scalararg[0] = -1; |
| 138 | return; |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 139 | } |
| 140 | |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 141 | #pragma textflag NOSPLIT |
| 142 | int32 |
| 143 | runtime·semasleep(int64 ns) |
| 144 | { |
| 145 | int32 r; |
| 146 | void (*fn)(void); |
| 147 | |
| 148 | g->m->scalararg[0] = (uint32)ns; |
| 149 | g->m->scalararg[1] = (uint32)(ns>>32); |
| 150 | fn = semasleep; |
| 151 | runtime·onM(&fn); |
| 152 | r = g->m->scalararg[0]; |
| 153 | g->m->scalararg[0] = 0; |
| 154 | return r; |
| 155 | } |
| 156 | |
| 157 | static void badsemawakeup(void); |
| 158 | |
| 159 | #pragma textflag NOSPLIT |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 160 | void |
| 161 | runtime·semawakeup(M *mp) |
| 162 | { |
| 163 | uint32 ret; |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 164 | void (*fn)(void); |
| 165 | void *oldptr; |
| 166 | uintptr oldscalar; |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 167 | |
| 168 | // spin-mutex lock |
| 169 | while(runtime·xchg(&mp->waitsemalock, 1)) |
| 170 | runtime·osyield(); |
| 171 | mp->waitsemacount++; |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 172 | // TODO(jsing) - potential deadlock, see semasleep() for details. |
| 173 | // Confirm that LWP is parked before unparking... |
| 174 | ret = runtime·lwp_unpark(mp->procid, &mp->waitsemacount); |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 175 | if(ret != 0 && ret != ESRCH) { |
| 176 | // semawakeup can be called on signal stack. |
| 177 | // Save old ptrarg/scalararg so we can restore them. |
| 178 | oldptr = g->m->ptrarg[0]; |
| 179 | oldscalar = g->m->scalararg[0]; |
| 180 | g->m->ptrarg[0] = mp; |
| 181 | g->m->scalararg[0] = ret; |
| 182 | fn = badsemawakeup; |
| 183 | if(g == g->m->gsignal) |
| 184 | fn(); |
| 185 | else |
| 186 | runtime·onM(&fn); |
| 187 | g->m->ptrarg[0] = oldptr; |
| 188 | g->m->scalararg[0] = oldscalar; |
| 189 | } |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 190 | // spin-mutex unlock |
| 191 | runtime·atomicstore(&mp->waitsemalock, 0); |
| 192 | } |
| 193 | |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 194 | static void |
| 195 | badsemawakeup(void) |
| 196 | { |
| 197 | M *mp; |
| 198 | int32 ret; |
| 199 | |
| 200 | mp = g->m->ptrarg[0]; |
| 201 | g->m->ptrarg[0] = nil; |
| 202 | ret = g->m->scalararg[0]; |
| 203 | g->m->scalararg[0] = 0; |
| 204 | |
| 205 | runtime·printf("thrwakeup addr=%p sem=%d ret=%d\n", &mp->waitsemacount, mp->waitsemacount, ret); |
| 206 | } |
| 207 | |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 208 | void |
Russ Cox | e6a3e22 | 2013-03-01 11:44:43 -0500 | [diff] [blame] | 209 | runtime·newosproc(M *mp, void *stk) |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 210 | { |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 211 | UcontextT uc; |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 212 | int32 ret; |
| 213 | |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 214 | if(0) { |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 215 | runtime·printf( |
Russ Cox | e6a3e22 | 2013-03-01 11:44:43 -0500 | [diff] [blame] | 216 | "newosproc stk=%p m=%p g=%p id=%d/%d ostk=%p\n", |
| 217 | stk, mp, mp->g0, mp->id, (int32)mp->tls[0], &mp); |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Jingcheng Zhang | 70e967b | 2012-12-19 00:30:29 +0800 | [diff] [blame] | 220 | mp->tls[0] = mp->id; // so 386 asm can find it |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 221 | |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 222 | runtime·getcontext(&uc); |
| 223 | |
| 224 | uc.uc_flags = _UC_SIGMASK | _UC_CPU; |
| 225 | uc.uc_link = nil; |
| 226 | uc.uc_sigmask = sigset_all; |
| 227 | |
Russ Cox | e6a3e22 | 2013-03-01 11:44:43 -0500 | [diff] [blame] | 228 | runtime·lwp_mcontext_init(&uc.uc_mcontext, stk, mp, mp->g0, runtime·mstart); |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 229 | |
Jingcheng Zhang | 70e967b | 2012-12-19 00:30:29 +0800 | [diff] [blame] | 230 | ret = runtime·lwp_create(&uc, 0, &mp->procid); |
Joel Sing | 5a043de | 2012-05-24 11:33:11 +1000 | [diff] [blame] | 231 | |
| 232 | if(ret < 0) { |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 233 | runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount() - 1, -ret); |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 234 | runtime·throw("runtime.newosproc"); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | runtime·osinit(void) |
| 240 | { |
| 241 | runtime·ncpu = getncpu(); |
| 242 | } |
| 243 | |
Russ Cox | f545b05 | 2014-09-04 15:53:45 -0400 | [diff] [blame] | 244 | #pragma textflag NOSPLIT |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 245 | void |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 246 | runtime·get_random_data(byte **rnd, int32 *rnd_len) |
| 247 | { |
Keith Randall | 548b15d | 2014-05-31 19:21:17 -0400 | [diff] [blame] | 248 | #pragma dataflag NOPTR |
Keith Randall | a5d4024 | 2013-03-12 10:47:44 -0700 | [diff] [blame] | 249 | static byte urandom_data[HashRandomBytes]; |
| 250 | int32 fd; |
| 251 | fd = runtime·open("/dev/urandom", 0 /* O_RDONLY */, 0); |
| 252 | if(runtime·read(fd, urandom_data, HashRandomBytes) == HashRandomBytes) { |
| 253 | *rnd = urandom_data; |
| 254 | *rnd_len = HashRandomBytes; |
| 255 | } else { |
| 256 | *rnd = nil; |
| 257 | *rnd_len = 0; |
| 258 | } |
| 259 | runtime·close(fd); |
| 260 | } |
| 261 | |
| 262 | void |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 263 | runtime·goenvs(void) |
| 264 | { |
| 265 | runtime·goenvs_unix(); |
| 266 | } |
| 267 | |
| 268 | // Called to initialize a new m (including the bootstrap m). |
Dmitriy Vyukov | a0955a2 | 2013-02-21 16:24:38 +0400 | [diff] [blame] | 269 | // Called on the parent thread (main thread in case of bootstrap), can allocate memory. |
| 270 | void |
| 271 | runtime·mpreinit(M *mp) |
| 272 | { |
| 273 | mp->gsignal = runtime·malg(32*1024); |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 274 | mp->gsignal->m = mp; |
Dmitriy Vyukov | a0955a2 | 2013-02-21 16:24:38 +0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // Called to initialize a new m (including the bootstrap m). |
| 278 | // Called on the new thread, can not allocate memory. |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 279 | void |
| 280 | runtime·minit(void) |
| 281 | { |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 282 | g->m->procid = runtime·lwp_self(); |
Joel Sing | deb93b0 | 2012-05-31 03:27:04 +1000 | [diff] [blame] | 283 | |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 284 | // Initialize signal handling |
Russ Cox | 89f185f | 2014-06-26 11:54:39 -0400 | [diff] [blame] | 285 | runtime·signalstack((byte*)g->m->gsignal->stackguard - StackGuard, 32*1024); |
Russ Cox | c7f7bbb | 2013-02-15 12:18:33 -0500 | [diff] [blame] | 286 | runtime·sigprocmask(SIG_SETMASK, &sigset_none, nil); |
Christopher Nielsen | 26089cf | 2011-12-12 18:10:11 -0500 | [diff] [blame] | 287 | } |
| 288 | |
Russ Cox | 6c97639 | 2013-02-20 17:48:23 -0500 | [diff] [blame] | 289 | // Called from dropm to undo the effect of an minit. |
| 290 | void |
| 291 | runtime·unminit(void) |
| 292 | { |
| 293 | runtime·signalstack(nil, 0); |
| 294 | } |
| 295 | |
Russ Cox | 102274a | 2012-02-24 15:28:51 -0500 | [diff] [blame] | 296 | uintptr |
| 297 | runtime·memlimit(void) |
| 298 | { |
| 299 | return 0; |
| 300 | } |
Russ Cox | 6e2ae0a | 2012-02-28 16:18:24 -0500 | [diff] [blame] | 301 | |
Russ Cox | e9d62a6 | 2013-03-14 11:35:13 -0700 | [diff] [blame] | 302 | extern void runtime·sigtramp(void); |
| 303 | |
| 304 | typedef struct sigaction { |
| 305 | union { |
| 306 | void (*_sa_handler)(int32); |
| 307 | void (*_sa_sigaction)(int32, Siginfo*, void *); |
| 308 | } _sa_u; /* signal handler */ |
| 309 | uint32 sa_mask[4]; /* signal mask to apply */ |
| 310 | int32 sa_flags; /* see signal options below */ |
Russ Cox | 9a75c74 | 2014-08-29 16:00:31 -0400 | [diff] [blame] | 311 | } SigactionT; |
Russ Cox | e9d62a6 | 2013-03-14 11:35:13 -0700 | [diff] [blame] | 312 | |
| 313 | void |
| 314 | runtime·setsig(int32 i, GoSighandler *fn, bool restart) |
| 315 | { |
Russ Cox | 9a75c74 | 2014-08-29 16:00:31 -0400 | [diff] [blame] | 316 | SigactionT sa; |
Russ Cox | e9d62a6 | 2013-03-14 11:35:13 -0700 | [diff] [blame] | 317 | |
| 318 | runtime·memclr((byte*)&sa, sizeof sa); |
| 319 | sa.sa_flags = SA_SIGINFO|SA_ONSTACK; |
| 320 | if(restart) |
| 321 | sa.sa_flags |= SA_RESTART; |
| 322 | sa.sa_mask[0] = ~0U; |
| 323 | sa.sa_mask[1] = ~0U; |
| 324 | sa.sa_mask[2] = ~0U; |
| 325 | sa.sa_mask[3] = ~0U; |
| 326 | if (fn == runtime·sighandler) |
| 327 | fn = (void*)runtime·sigtramp; |
| 328 | sa._sa_u._sa_sigaction = (void*)fn; |
| 329 | runtime·sigaction(i, &sa, nil); |
| 330 | } |
| 331 | |
| 332 | GoSighandler* |
| 333 | runtime·getsig(int32 i) |
| 334 | { |
Russ Cox | 9a75c74 | 2014-08-29 16:00:31 -0400 | [diff] [blame] | 335 | SigactionT sa; |
Russ Cox | e9d62a6 | 2013-03-14 11:35:13 -0700 | [diff] [blame] | 336 | |
| 337 | runtime·memclr((byte*)&sa, sizeof sa); |
| 338 | runtime·sigaction(i, nil, &sa); |
| 339 | if((void*)sa._sa_u._sa_sigaction == runtime·sigtramp) |
| 340 | return runtime·sighandler; |
| 341 | return (void*)sa._sa_u._sa_sigaction; |
| 342 | } |
| 343 | |
| 344 | void |
| 345 | runtime·signalstack(byte *p, int32 n) |
| 346 | { |
| 347 | StackT st; |
| 348 | |
| 349 | st.ss_sp = (void*)p; |
| 350 | st.ss_size = n; |
| 351 | st.ss_flags = 0; |
| 352 | if(p == nil) |
| 353 | st.ss_flags = SS_DISABLE; |
| 354 | runtime·sigaltstack(&st, nil); |
| 355 | } |
Shenghou Ma | 0097d30 | 2013-12-19 20:45:05 -0500 | [diff] [blame] | 356 | |
| 357 | void |
| 358 | runtime·unblocksignals(void) |
| 359 | { |
| 360 | runtime·sigprocmask(SIG_SETMASK, &sigset_none, nil); |
| 361 | } |
Russ Cox | c81a0ed | 2014-09-08 14:05:23 -0400 | [diff] [blame^] | 362 | |
| 363 | #pragma textflag NOSPLIT |
| 364 | int8* |
| 365 | runtime·signame(int32 sig) |
| 366 | { |
| 367 | return runtime·sigtab[sig].name; |
| 368 | } |