| 1 |
// Copyright 2025 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 |
package windows |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/goarch" |
| 9 |
"unsafe" |
| 10 |
) |
| 11 |
|
| 12 |
// NOTE(rsc): CONTEXT_CONTROL is actually 0x400001 and should include PC, SP, and LR. |
| 13 |
// However, empirically, LR doesn't come along on Windows 10 |
| 14 |
// unless you also set CONTEXT_INTEGER (0x400002). |
| 15 |
// Without LR, we skip over the next-to-bottom function in profiles |
| 16 |
// when the bottom function is frameless. |
| 17 |
// So we set both here, to make a working CONTEXT_CONTROL. |
| 18 |
const CONTEXT_CONTROL = 0x400003 |
| 19 |
|
| 20 |
type Neon128 struct { |
| 21 |
Low uint64 |
| 22 |
High int64 |
| 23 |
} |
| 24 |
|
| 25 |
// See https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-arm64_nt_context |
| 26 |
type Context struct { |
| 27 |
ContextFlags uint32 |
| 28 |
Cpsr uint32 |
| 29 |
X [31]uint64 // fp is x[29], lr is x[30] |
| 30 |
XSp uint64 |
| 31 |
Pc uint64 |
| 32 |
V [32]Neon128 |
| 33 |
Fpcr uint32 |
| 34 |
Fpsr uint32 |
| 35 |
Bcr [8]uint32 |
| 36 |
Bvr [8]uint64 |
| 37 |
Wcr [2]uint32 |
| 38 |
Wvr [2]uint64 |
| 39 |
} |
| 40 |
|
| 41 |
func (c *Context) PC() uintptr { return uintptr(c.Pc) } |
| 42 |
func (c *Context) SP() uintptr { return uintptr(c.XSp) } |
| 43 |
func (c *Context) LR() uintptr { return uintptr(c.X[30]) } |
| 44 |
|
| 45 |
func (c *Context) SetPC(x uintptr) { c.Pc = uint64(x) } |
| 46 |
func (c *Context) SetSP(x uintptr) { c.XSp = uint64(x) } |
| 47 |
func (c *Context) SetLR(x uintptr) { c.X[30] = uint64(x) } |
| 48 |
func (c *Context) SetFP(x uintptr) { c.X[29] = uint64(x) } |
| 49 |
|
| 50 |
func (c *Context) PushCall(targetPC, resumePC uintptr) { |
| 51 |
// Push LR. The injected call is responsible |
| 52 |
// for restoring LR. gentraceback is aware of |
| 53 |
// this extra slot. See sigctxt.pushCall in |
| 54 |
// signal_arm64.go. |
| 55 |
sp := c.SP() - goarch.StackAlign |
| 56 |
c.SetSP(sp) |
| 57 |
*(*uint64)(unsafe.Pointer(sp)) = uint64(c.LR()) |
| 58 |
c.SetLR(resumePC) |
| 59 |
c.SetPC(targetPC) |
| 60 |
} |
| 61 |
|
| 62 |
type DISPATCHER_CONTEXT struct { |
| 63 |
ControlPc uint64 |
| 64 |
ImageBase uint64 |
| 65 |
FunctionEntry uintptr |
| 66 |
EstablisherFrame uint64 |
| 67 |
TargetIp uint64 |
| 68 |
Context *Context |
| 69 |
LanguageHandler uintptr |
| 70 |
HandlerData uintptr |
| 71 |
} |
| 72 |
|
| 73 |
func (c *DISPATCHER_CONTEXT) Ctx() *Context { |
| 74 |
return c.Context |
| 75 |
} |