Kai Backman | 7943556 | 2009-05-26 11:18:42 -0700 | [diff] [blame] | 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. |
| 4 | |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 5 | #include "runtime.h" |
| 6 | #include "defs.h" |
| 7 | #include "signals.h" |
| 8 | #include "os.h" |
| 9 | |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 10 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 11 | runtime·dumpregs(Sigcontext *r) |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 12 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 13 | runtime·printf("trap %x\n", r->trap_no); |
| 14 | runtime·printf("error %x\n", r->error_code); |
| 15 | runtime·printf("oldmask %x\n", r->oldmask); |
| 16 | runtime·printf("r0 %x\n", r->arm_r0); |
| 17 | runtime·printf("r1 %x\n", r->arm_r1); |
| 18 | runtime·printf("r2 %x\n", r->arm_r2); |
| 19 | runtime·printf("r3 %x\n", r->arm_r3); |
| 20 | runtime·printf("r4 %x\n", r->arm_r4); |
| 21 | runtime·printf("r5 %x\n", r->arm_r5); |
| 22 | runtime·printf("r6 %x\n", r->arm_r6); |
| 23 | runtime·printf("r7 %x\n", r->arm_r7); |
| 24 | runtime·printf("r8 %x\n", r->arm_r8); |
| 25 | runtime·printf("r9 %x\n", r->arm_r9); |
| 26 | runtime·printf("r10 %x\n", r->arm_r10); |
| 27 | runtime·printf("fp %x\n", r->arm_fp); |
| 28 | runtime·printf("ip %x\n", r->arm_ip); |
| 29 | runtime·printf("sp %x\n", r->arm_sp); |
| 30 | runtime·printf("lr %x\n", r->arm_lr); |
| 31 | runtime·printf("pc %x\n", r->arm_pc); |
| 32 | runtime·printf("cpsr %x\n", r->arm_cpsr); |
| 33 | runtime·printf("fault %x\n", r->fault_address); |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 34 | } |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * This assembler routine takes the args from registers, puts them on the stack, |
| 38 | * and calls sighandler(). |
| 39 | */ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 40 | extern void runtime·sigtramp(void); |
| 41 | extern void runtime·sigignore(void); // just returns |
| 42 | extern void runtime·sigreturn(void); // calls runtime·sigreturn |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 43 | |
David Symonds | b586649 | 2009-12-15 18:21:29 -0800 | [diff] [blame] | 44 | String |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 45 | runtime·signame(int32 sig) |
David Symonds | b586649 | 2009-12-15 18:21:29 -0800 | [diff] [blame] | 46 | { |
| 47 | if(sig < 0 || sig >= NSIG) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 48 | return runtime·emptystring; |
| 49 | return runtime·gostringnocopy((byte*)runtime·sigtab[sig].name); |
David Symonds | b586649 | 2009-12-15 18:21:29 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 52 | void |
Russ Cox | 690291a | 2011-02-23 14:47:42 -0500 | [diff] [blame] | 53 | runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 54 | { |
| 55 | Ucontext *uc; |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 56 | Sigcontext *r; |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 57 | |
| 58 | uc = context; |
| 59 | r = &uc->uc_mcontext; |
| 60 | |
Russ Cox | c19b373 | 2011-03-23 11:43:37 -0400 | [diff] [blame] | 61 | if(sig == SIGPROF) { |
| 62 | runtime·sigprof((uint8*)r->arm_pc, (uint8*)r->arm_sp, (uint8*)r->arm_lr, gp); |
| 63 | return; |
| 64 | } |
| 65 | |
Russ Cox | 690291a | 2011-02-23 14:47:42 -0500 | [diff] [blame] | 66 | if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) { |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 67 | // Make it look like a call to the signal func. |
| 68 | // Have to pass arguments out of band since |
| 69 | // augmenting the stack frame would break |
| 70 | // the unwinding code. |
| 71 | gp->sig = sig; |
| 72 | gp->sigcode0 = info->si_code; |
| 73 | gp->sigcode1 = r->fault_address; |
Russ Cox | 1230700 | 2011-01-18 14:15:11 -0500 | [diff] [blame] | 74 | gp->sigpc = r->arm_pc; |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 75 | |
| 76 | // If this is a leaf function, we do smash LR, |
| 77 | // but we're not going back there anyway. |
Russ Cox | c3e54f0 | 2010-04-13 22:31:47 -0700 | [diff] [blame] | 78 | // Don't bother smashing if r->arm_pc is 0, |
| 79 | // which is probably a call to a nil func: the |
| 80 | // old link register is more useful in the stack trace. |
| 81 | if(r->arm_pc != 0) |
| 82 | r->arm_lr = r->arm_pc; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 83 | r->arm_pc = (uintptr)runtime·sigpanic; |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 84 | return; |
| 85 | } |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 86 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 87 | if(runtime·sigtab[sig].flags & SigQueue) { |
| 88 | if(runtime·sigsend(sig) || (runtime·sigtab[sig].flags & SigIgnore)) |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 89 | return; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 90 | runtime·exit(2); // SIGINT, SIGTERM, etc |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 91 | } |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 92 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 93 | if(runtime·panicking) // traceback already printed |
| 94 | runtime·exit(2); |
| 95 | runtime·panicking = 1; |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 96 | |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 97 | if(sig < 0 || sig >= NSIG) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 98 | runtime·printf("Signal %d\n", sig); |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 99 | else |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 100 | runtime·printf("%s\n", runtime·sigtab[sig].name); |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 101 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 102 | runtime·printf("PC=%x\n", r->arm_pc); |
| 103 | runtime·printf("\n"); |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 104 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 105 | if(runtime·gotraceback()){ |
Russ Cox | 690291a | 2011-02-23 14:47:42 -0500 | [diff] [blame] | 106 | runtime·traceback((void*)r->arm_pc, (void*)r->arm_sp, (void*)r->arm_lr, gp); |
| 107 | runtime·tracebackothers(gp); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 108 | runtime·printf("\n"); |
| 109 | runtime·dumpregs(r); |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // breakpoint(); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 113 | runtime·exit(2); |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 114 | } |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 115 | |
| 116 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 117 | runtime·signalstack(byte *p, int32 n) |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 118 | { |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 119 | Sigaltstack st; |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 120 | |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 121 | st.ss_sp = p; |
| 122 | st.ss_size = n; |
| 123 | st.ss_flags = 0; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 124 | runtime·sigaltstack(&st, nil); |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Russ Cox | c19b373 | 2011-03-23 11:43:37 -0400 | [diff] [blame] | 127 | static void |
| 128 | sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart) |
| 129 | { |
| 130 | Sigaction sa; |
| 131 | |
| 132 | runtime·memclr((byte*)&sa, sizeof sa); |
| 133 | sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER; |
| 134 | if(restart) |
| 135 | sa.sa_flags |= SA_RESTART; |
| 136 | sa.sa_mask = ~0ULL; |
| 137 | sa.sa_restorer = (void*)runtime·sigreturn; |
Russ Cox | 6b33571 | 2011-03-27 23:39:42 -0400 | [diff] [blame] | 138 | if(fn == runtime·sighandler) |
| 139 | fn = (void*)runtime·sigtramp; |
Russ Cox | 1f22346 | 2011-03-25 12:30:49 -0400 | [diff] [blame] | 140 | sa.sa_handler = fn; |
Russ Cox | c19b373 | 2011-03-23 11:43:37 -0400 | [diff] [blame] | 141 | runtime·rt_sigaction(i, &sa, nil, 8); |
| 142 | } |
| 143 | |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 144 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 145 | runtime·initsig(int32 queue) |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 146 | { |
Russ Cox | c19b373 | 2011-03-23 11:43:37 -0400 | [diff] [blame] | 147 | int32 i; |
| 148 | void *fn; |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 149 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 150 | runtime·siginit(); |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 151 | |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 152 | for(i = 0; i<NSIG; i++) { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 153 | if(runtime·sigtab[i].flags) { |
| 154 | if((runtime·sigtab[i].flags & SigQueue) != queue) |
Ian Lance Taylor | 807605d | 2010-06-28 17:14:17 -0700 | [diff] [blame] | 155 | continue; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 156 | if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) |
Russ Cox | c19b373 | 2011-03-23 11:43:37 -0400 | [diff] [blame] | 157 | fn = runtime·sighandler; |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 158 | else |
Russ Cox | c19b373 | 2011-03-23 11:43:37 -0400 | [diff] [blame] | 159 | fn = runtime·sigignore; |
| 160 | sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0); |
Russ Cox | 6c19601 | 2010-04-05 12:51:09 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
Kai Backman | 5289195 | 2009-06-10 11:53:07 -0700 | [diff] [blame] | 163 | } |
Russ Cox | c19b373 | 2011-03-23 11:43:37 -0400 | [diff] [blame] | 164 | |
| 165 | void |
| 166 | runtime·resetcpuprofiler(int32 hz) |
| 167 | { |
Russ Cox | c19b373 | 2011-03-23 11:43:37 -0400 | [diff] [blame] | 168 | Itimerval it; |
| 169 | |
| 170 | runtime·memclr((byte*)&it, sizeof it); |
| 171 | if(hz == 0) { |
| 172 | runtime·setitimer(ITIMER_PROF, &it, nil); |
| 173 | sigaction(SIGPROF, SIG_IGN, true); |
| 174 | } else { |
| 175 | sigaction(SIGPROF, runtime·sighandler, true); |
| 176 | it.it_interval.tv_sec = 0; |
| 177 | it.it_interval.tv_usec = 1000000 / hz; |
| 178 | it.it_value = it.it_interval; |
| 179 | runtime·setitimer(ITIMER_PROF, &it, nil); |
| 180 | } |
| 181 | m->profilehz = hz; |
| 182 | } |
Russ Cox | 8698bb6 | 2011-04-25 16:58:00 -0400 | [diff] [blame] | 183 | |
| 184 | void |
| 185 | os·sigpipe(void) |
| 186 | { |
| 187 | sigaction(SIGPIPE, SIG_DFL, false); |
| 188 | runtime·raisesigpipe(); |
| 189 | } |