ReactOS  0.4.14-dev-114-gc8cbd56
cpu_arm64.c
Go to the documentation of this file.
1 /*
2  * File cpu_arm64.c
3  *
4  * Copyright (C) 2009 Eric Pouech
5  * Copyright (C) 2010-2013 AndrĂ© Hentschel
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 
22 #include <assert.h>
23 
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "dbghelp_private.h"
29 #include "winternl.h"
30 #include "wine/debug.h"
31 
33 
35  enum cpu_addr ca, ADDRESS64* addr)
36 {
37  addr->Mode = AddrModeFlat;
38  addr->Segment = 0; /* don't need segment */
39  switch (ca)
40  {
41 #ifdef __aarch64__
42  case cpu_addr_pc: addr->Offset = ctx->Pc; return TRUE;
43  case cpu_addr_stack: addr->Offset = ctx->Sp; return TRUE;
44  case cpu_addr_frame: addr->Offset = ctx->u.s.Fp; return TRUE;
45 #endif
46  default: addr->Mode = -1;
47  return FALSE;
48  }
49 }
50 
51 #ifdef __aarch64__
52 enum st_mode {stm_start, stm_arm64, stm_done};
53 
54 /* indexes in Reserved array */
55 #define __CurrentModeCount 0
56 
57 #define curr_mode (frame->Reserved[__CurrentModeCount] & 0x0F)
58 #define curr_count (frame->Reserved[__CurrentModeCount] >> 4)
59 
60 #define set_curr_mode(m) {frame->Reserved[__CurrentModeCount] &= ~0x0F; frame->Reserved[__CurrentModeCount] |= (m & 0x0F);}
61 #define inc_curr_count() (frame->Reserved[__CurrentModeCount] += 0x10)
62 
63 /* fetch_next_frame()
64  *
65  * modify (at least) context.Pc using unwind information
66  * either out of debug info (dwarf), or simple Lr trace
67  */
68 static BOOL fetch_next_frame(struct cpu_stack_walk* csw,
69  CONTEXT* context, DWORD_PTR curr_pc)
70 {
71  DWORD_PTR xframe;
72  DWORD_PTR oldReturn = context->u.s.Lr;
73 
74  if (dwarf2_virtual_unwind(csw, curr_pc, context, &xframe))
75  {
76  context->Sp = xframe;
77  context->Pc = oldReturn;
78  return TRUE;
79  }
80 
81  if (context->Pc == context->u.s.Lr) return FALSE;
82  context->Pc = oldReturn;
83 
84  return TRUE;
85 }
86 
88 {
89  unsigned deltapc = curr_count <= 1 ? 0 : 4;
90 
91  /* sanity check */
92  if (curr_mode >= stm_done) return FALSE;
93 
94  TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s\n",
95  wine_dbgstr_addr(&frame->AddrPC),
96  wine_dbgstr_addr(&frame->AddrFrame),
98  wine_dbgstr_addr(&frame->AddrStack),
99  curr_mode == stm_start ? "start" : "ARM64",
101 
102  if (curr_mode == stm_start)
103  {
104  /* Init done */
105  set_curr_mode(stm_arm64);
106  frame->AddrReturn.Mode = frame->AddrStack.Mode = AddrModeFlat;
107  /* don't set up AddrStack on first call. Either the caller has set it up, or
108  * we will get it in the next frame
109  */
110  memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
111  }
112  else
113  {
114  if (context->Sp != frame->AddrStack.Offset) FIXME("inconsistent Stack Pointer\n");
115  if (context->Pc != frame->AddrPC.Offset) FIXME("inconsistent Program Counter\n");
116 
117  if (frame->AddrReturn.Offset == 0) goto done_err;
118  if (!fetch_next_frame(csw, context, frame->AddrPC.Offset - deltapc))
119  goto done_err;
120  }
121 
122  memset(&frame->Params, 0, sizeof(frame->Params));
123 
124  /* set frame information */
125  frame->AddrStack.Offset = context->Sp;
126  frame->AddrReturn.Offset = context->u.s.Lr;
127  frame->AddrFrame.Offset = context->u.s.Fp;
128  frame->AddrPC.Offset = context->Pc;
129 
130  frame->Far = TRUE;
131  frame->Virtual = TRUE;
132  inc_curr_count();
133 
134  TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s FuncTable=%p\n",
135  wine_dbgstr_addr(&frame->AddrPC),
136  wine_dbgstr_addr(&frame->AddrFrame),
137  wine_dbgstr_addr(&frame->AddrReturn),
138  wine_dbgstr_addr(&frame->AddrStack),
139  curr_mode == stm_start ? "start" : "ARM64",
141  frame->FuncTableEntry);
142 
143  return TRUE;
144 done_err:
146  return FALSE;
147 }
148 #else
150 {
151  return FALSE;
152 }
153 #endif
154 
155 static unsigned arm64_map_dwarf_register(unsigned regno, BOOL eh_frame)
156 {
157  if (regno <= 28) return CV_ARM64_X0 + regno;
158  if (regno == 29) return CV_ARM64_FP;
159  if (regno == 30) return CV_ARM64_LR;
160  if (regno == 31) return CV_ARM64_SP;
161 
162  FIXME("Don't know how to map register %d\n", regno);
163  return CV_ARM64_NOREG;
164 }
165 
166 static void* arm64_fetch_context_reg(CONTEXT* ctx, unsigned regno, unsigned* size)
167 {
168 #ifdef __aarch64__
169  switch (regno)
170  {
171  case CV_ARM64_X0 + 0:
172  case CV_ARM64_X0 + 1:
173  case CV_ARM64_X0 + 2:
174  case CV_ARM64_X0 + 3:
175  case CV_ARM64_X0 + 4:
176  case CV_ARM64_X0 + 5:
177  case CV_ARM64_X0 + 6:
178  case CV_ARM64_X0 + 7:
179  case CV_ARM64_X0 + 8:
180  case CV_ARM64_X0 + 9:
181  case CV_ARM64_X0 + 10:
182  case CV_ARM64_X0 + 11:
183  case CV_ARM64_X0 + 12:
184  case CV_ARM64_X0 + 13:
185  case CV_ARM64_X0 + 14:
186  case CV_ARM64_X0 + 15:
187  case CV_ARM64_X0 + 16:
188  case CV_ARM64_X0 + 17:
189  case CV_ARM64_X0 + 18:
190  case CV_ARM64_X0 + 19:
191  case CV_ARM64_X0 + 20:
192  case CV_ARM64_X0 + 21:
193  case CV_ARM64_X0 + 22:
194  case CV_ARM64_X0 + 23:
195  case CV_ARM64_X0 + 24:
196  case CV_ARM64_X0 + 25:
197  case CV_ARM64_X0 + 26:
198  case CV_ARM64_X0 + 27:
199  case CV_ARM64_X0 + 28: *size = sizeof(ctx->u.X[0]); return &ctx->u.X[regno - CV_ARM64_X0];
200  case CV_ARM64_PSTATE: *size = sizeof(ctx->Cpsr); return &ctx->Cpsr;
201  case CV_ARM64_FP: *size = sizeof(ctx->u.s.Fp); return &ctx->u.s.Fp;
202  case CV_ARM64_LR: *size = sizeof(ctx->u.s.Lr); return &ctx->u.s.Lr;
203  case CV_ARM64_SP: *size = sizeof(ctx->Sp); return &ctx->Sp;
204  case CV_ARM64_PC: *size = sizeof(ctx->Pc); return &ctx->Pc;
205  }
206 #endif
207  FIXME("Unknown register %x\n", regno);
208  return NULL;
209 }
210 
211 static const char* arm64_fetch_regname(unsigned regno)
212 {
213  switch (regno)
214  {
215  case CV_ARM64_PSTATE: return "cpsr";
216  case CV_ARM64_X0 + 0: return "x0";
217  case CV_ARM64_X0 + 1: return "x1";
218  case CV_ARM64_X0 + 2: return "x2";
219  case CV_ARM64_X0 + 3: return "x3";
220  case CV_ARM64_X0 + 4: return "x4";
221  case CV_ARM64_X0 + 5: return "x5";
222  case CV_ARM64_X0 + 6: return "x6";
223  case CV_ARM64_X0 + 7: return "x7";
224  case CV_ARM64_X0 + 8: return "x8";
225  case CV_ARM64_X0 + 9: return "x9";
226  case CV_ARM64_X0 + 10: return "x10";
227  case CV_ARM64_X0 + 11: return "x11";
228  case CV_ARM64_X0 + 12: return "x12";
229  case CV_ARM64_X0 + 13: return "x13";
230  case CV_ARM64_X0 + 14: return "x14";
231  case CV_ARM64_X0 + 15: return "x15";
232  case CV_ARM64_X0 + 16: return "x16";
233  case CV_ARM64_X0 + 17: return "x17";
234  case CV_ARM64_X0 + 18: return "x18";
235  case CV_ARM64_X0 + 19: return "x19";
236  case CV_ARM64_X0 + 20: return "x20";
237  case CV_ARM64_X0 + 21: return "x21";
238  case CV_ARM64_X0 + 22: return "x22";
239  case CV_ARM64_X0 + 23: return "x23";
240  case CV_ARM64_X0 + 24: return "x24";
241  case CV_ARM64_X0 + 25: return "x25";
242  case CV_ARM64_X0 + 26: return "x26";
243  case CV_ARM64_X0 + 27: return "x27";
244  case CV_ARM64_X0 + 28: return "x28";
245 
246  case CV_ARM64_FP: return "fp";
247  case CV_ARM64_LR: return "lr";
248  case CV_ARM64_SP: return "sp";
249  case CV_ARM64_PC: return "pc";
250  }
251  FIXME("Unknown register %x\n", regno);
252  return NULL;
253 }
254 
255 static BOOL arm64_fetch_minidump_thread(struct dump_context* dc, unsigned index, unsigned flags, const CONTEXT* ctx)
256 {
258  {
259  /* FIXME: crop values across module boundaries, */
260 #ifdef __aarch64__
261  ULONG base = ctx->Pc <= 0x80 ? 0 : ctx->Pc - 0x80;
262  minidump_add_memory_block(dc, base, ctx->Pc + 0x80 - base, 0);
263 #endif
264  }
265 
266  return TRUE;
267 }
268 
269 static BOOL arm64_fetch_minidump_module(struct dump_context* dc, unsigned index, unsigned flags)
270 {
271  /* FIXME: actually, we should probably take care of FPO data, unless it's stored in
272  * function table minidump stream
273  */
274  return FALSE;
275 }
276 
279  8,
280  CV_ARM64_FP,
283  NULL,
289 };
#define TRUE
Definition: types.h:120
#define set_curr_mode(m)
Definition: cpu_i386.c:155
#define IMAGE_FILE_MACHINE_ARM64
Definition: compat.h:118
Definition: http.c:6587
HDC dc
Definition: cylfrac.c:34
static BOOL arm64_fetch_minidump_module(struct dump_context *dc, unsigned index, unsigned flags)
Definition: cpu_arm64.c:269
DWORD64 Params[4]
Definition: compat.h:1048
static BOOL arm64_get_addr(HANDLE hThread, const CONTEXT *ctx, enum cpu_addr ca, ADDRESS64 *addr)
Definition: cpu_arm64.c:34
ADDRESS64 AddrBStore
Definition: compat.h:1046
void minidump_add_memory_block(struct dump_context *dc, ULONG64 base, ULONG size, ULONG rva) DECLSPEC_HIDDEN
Definition: minidump.c:338
#define curr_mode
Definition: cpu_i386.c:150
static BOOL arm64_stack_walk(struct cpu_stack_walk *csw, LPSTACKFRAME64 frame, CONTEXT *context)
Definition: cpu_arm64.c:149
DECLSPEC_HIDDEN struct cpu cpu_arm64
Definition: cpu_arm64.c:277
BOOL dwarf2_virtual_unwind(struct cpu_stack_walk *csw, DWORD_PTR ip, CONTEXT *context, ULONG_PTR *cfa) DECLSPEC_HIDDEN
Definition: dwarf.c:3265
unsigned int BOOL
Definition: ntddk_ex.h:94
#define FIXME(fmt,...)
Definition: debug.h:110
cpu_addr
#define inc_curr_count()
Definition: cpu_i386.c:156
smooth NULL
Definition: ftsmooth.c:416
static unsigned arm64_map_dwarf_register(unsigned regno, BOOL eh_frame)
Definition: cpu_arm64.c:155
GLuint index
Definition: glext.h:6031
ULONG ContextFlags
Definition: compat.h:331
WINE_DEFAULT_DEBUG_CHANNEL(dbghelp)
const char * wine_dbgstr_addr(const ADDRESS64 *addr)
Definition: dbghelp.c:123
#define TRACE(s)
Definition: solgame.cpp:4
GLsizeiptr size
Definition: glext.h:5919
DWORD64 Offset
Definition: compat.h:815
ADDRESS64 AddrReturn
Definition: compat.h:1043
static BOOL arm64_fetch_minidump_thread(struct dump_context *dc, unsigned index, unsigned flags, const CONTEXT *ctx)
Definition: cpu_arm64.c:255
GLbitfield flags
Definition: glext.h:7161
PVOID FuncTableEntry
Definition: compat.h:1047
ADDRESS64 AddrStack
Definition: compat.h:1045
GLenum const GLvoid * addr
Definition: glext.h:9621
static const char * arm64_fetch_regname(unsigned regno)
Definition: cpu_arm64.c:211
uint32_t DWORD_PTR
Definition: typedefs.h:63
st_mode
Definition: cpu_i386.c:143
#define DECLSPEC_HIDDEN
Definition: precomp.h:8
ULONG Cpsr
Definition: compat.h:353
ADDRESS64 AddrPC
Definition: compat.h:1042
HANDLE hThread
Definition: wizard.c:27
ULONG Fp
Definition: compat.h:346
unsigned int ULONG
Definition: retypes.h:1
ULONG Sp
Definition: compat.h:350
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:41
#define curr_count
Definition: cpu_i386.c:151
static void * arm64_fetch_context_reg(CONTEXT *ctx, unsigned regno, unsigned *size)
Definition: cpu_arm64.c:166
static const WCHAR ca[]
Definition: main.c:457
ULONG Pc
Definition: compat.h:352
#define memset(x, y, z)
Definition: compat.h:39
ADDRESS_MODE Mode
Definition: compat.h:817
ADDRESS64 AddrFrame
Definition: compat.h:1044
BOOL Virtual
Definition: compat.h:1050
ULONG Lr
Definition: compat.h:351