...

Source file src/runtime/export_debug_ppc64le_test.go

Documentation: runtime

     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.
     4  
     5  //go:build ppc64le && linux
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/abi"
    11  	"internal/goarch"
    12  	"math"
    13  	"unsafe"
    14  )
    15  
    16  type sigContext struct {
    17  	savedRegs sigcontext
    18  }
    19  
    20  func sigctxtSetContextRegister(ctxt *sigctxt, x uint64) {
    21  	ctxt.regs().gpr[11] = x
    22  }
    23  
    24  func sigctxtAtTrapInstruction(ctxt *sigctxt) bool {
    25  	return *(*uint32)(unsafe.Pointer(ctxt.sigpc())) == 0x7fe00008 // Trap
    26  }
    27  
    28  func sigctxtStatus(ctxt *sigctxt) uint64 {
    29  	return ctxt.r20()
    30  }
    31  
    32  func (h *debugCallHandler) saveSigContext(ctxt *sigctxt) {
    33  	sp := ctxt.sp()
    34  	sp -= 4 * goarch.PtrSize
    35  	ctxt.set_sp(sp)
    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()
    42  }
    43  
    44  // case 0
    45  func (h *debugCallHandler) debugCallRun(ctxt *sigctxt) {
    46  	sp := ctxt.sp()
    47  	memmove(unsafe.Pointer(uintptr(sp)+32), h.argp, h.argSize)
    48  	if h.regArgs != nil {
    49  		storeRegArgs(ctxt.cregs(), h.regArgs)
    50  	}
    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))))
    57  }
    58  
    59  // case 1
    60  func (h *debugCallHandler) debugCallReturn(ctxt *sigctxt) {
    61  	sp := ctxt.sp()
    62  	memmove(h.argp, unsafe.Pointer(uintptr(sp)+32), h.argSize)
    63  	if h.regArgs != nil {
    64  		loadRegArgs(h.regArgs, ctxt.cregs())
    65  	}
    66  	// Restore the old lr from *sp
    67  	olr := *(*uint64)(unsafe.Pointer(uintptr(sp)))
    68  	ctxt.set_link(olr)
    69  	pc := ctxt.pc()
    70  	ctxt.set_pc(pc + 4) // step to next instruction
    71  }
    72  
    73  // case 2
    74  func (h *debugCallHandler) debugCallPanicOut(ctxt *sigctxt) {
    75  	sp := ctxt.sp()
    76  	memmove(unsafe.Pointer(&h.panic), unsafe.Pointer(uintptr(sp)+32), 2*goarch.PtrSize)
    77  	ctxt.set_pc(ctxt.pc() + 4)
    78  }
    79  
    80  // case 8
    81  func (h *debugCallHandler) debugCallUnsafe(ctxt *sigctxt) {
    82  	sp := ctxt.sp()
    83  	reason := *(*string)(unsafe.Pointer(uintptr(sp) + 40))
    84  	h.err = plainError(reason)
    85  	ctxt.set_pc(ctxt.pc() + 4)
    86  }
    87  
    88  // case 16
    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
    93  	ctxt.set_pc(pc + 4)
    94  	ctxt.set_sp(sp)
    95  }
    96  
    97  // storeRegArgs sets up argument registers in the signal
    98  // context state from an abi.RegArgs.
    99  //
   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++ {
   104  		if i > 7 {
   105  			dst.gp_regs[i+6] = uint64(src.Ints[i])
   106  		} else {
   107  			dst.gp_regs[i+3] = uint64(src.Ints[i])
   108  		}
   109  	}
   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])
   113  	}
   114  
   115  }
   116  
   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{} {
   120  		if i > 7 {
   121  			dst.Ints[i] = uintptr(src.gp_regs[i+6])
   122  		} else {
   123  			dst.Ints[i] = uintptr(src.gp_regs[i+3])
   124  		}
   125  	}
   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])
   129  	}
   130  
   131  }
   132  

View as plain text