]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/signal_amd64x.go
runtime: break out system-specific constants into package sys
[gostls13.git] / src / runtime / signal_amd64x.go
1 // Copyright 2013 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 // +build amd64 amd64p32
6 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
7
8 package runtime
9
10 import (
11         "runtime/internal/sys"
12         "unsafe"
13 )
14
15 func dumpregs(c *sigctxt) {
16         print("rax    ", hex(c.rax()), "\n")
17         print("rbx    ", hex(c.rbx()), "\n")
18         print("rcx    ", hex(c.rcx()), "\n")
19         print("rdx    ", hex(c.rdx()), "\n")
20         print("rdi    ", hex(c.rdi()), "\n")
21         print("rsi    ", hex(c.rsi()), "\n")
22         print("rbp    ", hex(c.rbp()), "\n")
23         print("rsp    ", hex(c.rsp()), "\n")
24         print("r8     ", hex(c.r8()), "\n")
25         print("r9     ", hex(c.r9()), "\n")
26         print("r10    ", hex(c.r10()), "\n")
27         print("r11    ", hex(c.r11()), "\n")
28         print("r12    ", hex(c.r12()), "\n")
29         print("r13    ", hex(c.r13()), "\n")
30         print("r14    ", hex(c.r14()), "\n")
31         print("r15    ", hex(c.r15()), "\n")
32         print("rip    ", hex(c.rip()), "\n")
33         print("rflags ", hex(c.rflags()), "\n")
34         print("cs     ", hex(c.cs()), "\n")
35         print("fs     ", hex(c.fs()), "\n")
36         print("gs     ", hex(c.gs()), "\n")
37 }
38
39 var crashing int32
40
41 // May run during STW, so write barriers are not allowed.
42 //go:nowritebarrier
43 func sighandler(sig uint32, info *siginfo, ctxt unsafe.Pointer, gp *g) {
44         _g_ := getg()
45         c := &sigctxt{info, ctxt}
46
47         if sig == _SIGPROF {
48                 sigprof(uintptr(c.rip()), uintptr(c.rsp()), 0, gp, _g_.m)
49                 return
50         }
51
52         if GOOS == "darwin" {
53                 // x86-64 has 48-bit virtual addresses. The top 16 bits must echo bit 47.
54                 // The hardware delivers a different kind of fault for a malformed address
55                 // than it does for an attempt to access a valid but unmapped address.
56                 // OS X 10.9.2 mishandles the malformed address case, making it look like
57                 // a user-generated signal (like someone ran kill -SEGV ourpid).
58                 // We pass user-generated signals to os/signal, or else ignore them.
59                 // Doing that here - and returning to the faulting code - results in an
60                 // infinite loop. It appears the best we can do is rewrite what the kernel
61                 // delivers into something more like the truth. The address used below
62                 // has very little chance of being the one that caused the fault, but it is
63                 // malformed, it is clearly not a real pointer, and if it does get printed
64                 // in real life, people will probably search for it and find this code.
65                 // There are no Google hits for b01dfacedebac1e or 0xb01dfacedebac1e
66                 // as I type this comment.
67                 if sig == _SIGSEGV && c.sigcode() == _SI_USER {
68                         c.set_sigcode(_SI_USER + 1)
69                         c.set_sigaddr(0xb01dfacedebac1e)
70                 }
71         }
72
73         flags := int32(_SigThrow)
74         if sig < uint32(len(sigtable)) {
75                 flags = sigtable[sig].flags
76         }
77         if c.sigcode() != _SI_USER && flags&_SigPanic != 0 {
78                 // Make it look like a call to the signal func.
79                 // Have to pass arguments out of band since
80                 // augmenting the stack frame would break
81                 // the unwinding code.
82                 gp.sig = sig
83                 gp.sigcode0 = uintptr(c.sigcode())
84                 gp.sigcode1 = uintptr(c.sigaddr())
85                 gp.sigpc = uintptr(c.rip())
86
87                 if GOOS == "darwin" {
88                         // Work around Leopard bug that doesn't set FPE_INTDIV.
89                         // Look at instruction to see if it is a divide.
90                         // Not necessary in Snow Leopard (si_code will be != 0).
91                         if sig == _SIGFPE && gp.sigcode0 == 0 {
92                                 pc := (*[4]byte)(unsafe.Pointer(gp.sigpc))
93                                 i := 0
94                                 if pc[i]&0xF0 == 0x40 { // 64-bit REX prefix
95                                         i++
96                                 } else if pc[i] == 0x66 { // 16-bit instruction prefix
97                                         i++
98                                 }
99                                 if pc[i] == 0xF6 || pc[i] == 0xF7 {
100                                         gp.sigcode0 = _FPE_INTDIV
101                                 }
102                         }
103                 }
104
105                 pc := uintptr(c.rip())
106                 sp := uintptr(c.rsp())
107
108                 // If we don't recognize the PC as code
109                 // but we do recognize the top pointer on the stack as code,
110                 // then assume this was a call to non-code and treat like
111                 // pc == 0, to make unwinding show the context.
112                 if pc != 0 && findfunc(pc) == nil && findfunc(*(*uintptr)(unsafe.Pointer(sp))) != nil {
113                         pc = 0
114                 }
115
116                 // Only push runtime.sigpanic if pc != 0.
117                 // If pc == 0, probably panicked because of a
118                 // call to a nil func.  Not pushing that onto sp will
119                 // make the trace look like a call to runtime.sigpanic instead.
120                 // (Otherwise the trace will end at runtime.sigpanic and we
121                 // won't get to see who faulted.)
122                 if pc != 0 {
123                         if sys.RegSize > sys.PtrSize {
124                                 sp -= sys.PtrSize
125                                 *(*uintptr)(unsafe.Pointer(sp)) = 0
126                         }
127                         sp -= sys.PtrSize
128                         *(*uintptr)(unsafe.Pointer(sp)) = pc
129                         c.set_rsp(uint64(sp))
130                 }
131                 c.set_rip(uint64(funcPC(sigpanic)))
132                 return
133         }
134
135         if c.sigcode() == _SI_USER || flags&_SigNotify != 0 {
136                 if sigsend(sig) {
137                         return
138                 }
139         }
140
141         if flags&_SigKill != 0 {
142                 exit(2)
143         }
144
145         if flags&_SigThrow == 0 {
146                 return
147         }
148
149         _g_.m.throwing = 1
150         _g_.m.caughtsig.set(gp)
151
152         if crashing == 0 {
153                 startpanic()
154         }
155
156         if sig < uint32(len(sigtable)) {
157                 print(sigtable[sig].name, "\n")
158         } else {
159                 print("Signal ", sig, "\n")
160         }
161
162         print("PC=", hex(c.rip()), " m=", _g_.m.id, "\n")
163         if _g_.m.lockedg != nil && _g_.m.ncgo > 0 && gp == _g_.m.g0 {
164                 print("signal arrived during cgo execution\n")
165                 gp = _g_.m.lockedg
166         }
167         print("\n")
168
169         level, _, docrash := gotraceback()
170         if level > 0 {
171                 goroutineheader(gp)
172                 tracebacktrap(uintptr(c.rip()), uintptr(c.rsp()), 0, gp)
173                 if crashing > 0 && gp != _g_.m.curg && _g_.m.curg != nil && readgstatus(_g_.m.curg)&^_Gscan == _Grunning {
174                         // tracebackothers on original m skipped this one; trace it now.
175                         goroutineheader(_g_.m.curg)
176                         traceback(^uintptr(0), ^uintptr(0), 0, gp)
177                 } else if crashing == 0 {
178                         tracebackothers(gp)
179                         print("\n")
180                 }
181                 dumpregs(c)
182         }
183
184         if docrash {
185                 crashing++
186                 if crashing < sched.mcount {
187                         // There are other m's that need to dump their stacks.
188                         // Relay SIGQUIT to the next m by sending it to the current process.
189                         // All m's that have already received SIGQUIT have signal masks blocking
190                         // receipt of any signals, so the SIGQUIT will go to an m that hasn't seen it yet.
191                         // When the last m receives the SIGQUIT, it will fall through to the call to
192                         // crash below. Just in case the relaying gets botched, each m involved in
193                         // the relay sleeps for 5 seconds and then does the crash/exit itself.
194                         // In expected operation, the last m has received the SIGQUIT and run
195                         // crash/exit and the process is gone, all long before any of the
196                         // 5-second sleeps have finished.
197                         print("\n-----\n\n")
198                         raiseproc(_SIGQUIT)
199                         usleep(5 * 1000 * 1000)
200                 }
201                 crash()
202         }
203
204         exit(2)
205 }