1 // Copyright 2023 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.
5 //go:build ppc64le && linux
16 type sigContext struct {
20 func sigctxtSetContextRegister(ctxt *sigctxt, x uint64) {
21 ctxt.regs().gpr[11] = x
24 func sigctxtAtTrapInstruction(ctxt *sigctxt) bool {
25 return *(*uint32)(unsafe.Pointer(ctxt.sigpc())) == 0x7fe00008 // Trap
28 func sigctxtStatus(ctxt *sigctxt) uint64 {
32 func (h *debugCallHandler) saveSigContext(ctxt *sigctxt) {
34 sp -= 4 * goarch.PtrSize
36 *(*uint64)(unsafe.Pointer(uintptr(sp))) = ctxt.link() // save the current lr
37 ctxt.set_link(ctxt.pc()) // set new lr to the current pc
38 // Write the argument frame size.
39 *(*uintptr)(unsafe.Pointer(uintptr(sp - 32))) = h.argSize
40 // Save current registers.
41 h.sigCtxt.savedRegs = *ctxt.cregs()
45 func (h *debugCallHandler) debugCallRun(ctxt *sigctxt) {
47 memmove(unsafe.Pointer(uintptr(sp)+32), h.argp, h.argSize)
49 storeRegArgs(ctxt.cregs(), h.regArgs)
51 // Push return PC, which should be the signal PC+4, because
52 // the signal PC is the PC of the trap instruction itself.
53 ctxt.set_link(ctxt.pc() + 4)
54 // Set PC to call and context register.
55 ctxt.set_pc(uint64(h.fv.fn))
56 sigctxtSetContextRegister(ctxt, uint64(uintptr(unsafe.Pointer(h.fv))))
60 func (h *debugCallHandler) debugCallReturn(ctxt *sigctxt) {
62 memmove(h.argp, unsafe.Pointer(uintptr(sp)+32), h.argSize)
64 loadRegArgs(h.regArgs, ctxt.cregs())
66 // Restore the old lr from *sp
67 olr := *(*uint64)(unsafe.Pointer(uintptr(sp)))
70 ctxt.set_pc(pc + 4) // step to next instruction
74 func (h *debugCallHandler) debugCallPanicOut(ctxt *sigctxt) {
76 memmove(unsafe.Pointer(&h.panic), unsafe.Pointer(uintptr(sp)+32), 2*goarch.PtrSize)
77 ctxt.set_pc(ctxt.pc() + 4)
81 func (h *debugCallHandler) debugCallUnsafe(ctxt *sigctxt) {
83 reason := *(*string)(unsafe.Pointer(uintptr(sp) + 40))
84 h.err = plainError(reason)
85 ctxt.set_pc(ctxt.pc() + 4)
89 func (h *debugCallHandler) restoreSigContext(ctxt *sigctxt) {
90 // Restore all registers except for pc and sp
91 pc, sp := ctxt.pc(), ctxt.sp()
92 *ctxt.cregs() = h.sigCtxt.savedRegs
97 // storeRegArgs sets up argument registers in the signal
98 // context state from an abi.RegArgs.
100 // Both src and dst must be non-nil.
101 func storeRegArgs(dst *sigcontext, src *abi.RegArgs) {
102 // Gprs R3..R10, R14..R17 are used to pass int arguments in registers on PPC64
103 for i := 0; i < 12; i++ {
105 dst.gp_regs[i+6] = uint64(src.Ints[i])
107 dst.gp_regs[i+3] = uint64(src.Ints[i])
110 // Fprs F1..F13 are used to pass float arguments in registers on PPC64
111 for i := 0; i < 12; i++ {
112 dst.fp_regs[i+1] = math.Float64frombits(src.Floats[i])
117 func loadRegArgs(dst *abi.RegArgs, src *sigcontext) {
118 // Gprs R3..R10, R14..R17 are used to pass int arguments in registers on PPC64
119 for i, _ := range [12]int{} {
121 dst.Ints[i] = uintptr(src.gp_regs[i+6])
123 dst.Ints[i] = uintptr(src.gp_regs[i+3])
126 // Fprs F1..F13 are used to pass float arguments in registers on PPC64
127 for i, _ := range [12]int{} {
128 dst.Floats[i] = math.Float64bits(src.fp_regs[i+1])