xref: /reactos/dll/win32/dbghelp/cpu_arm64.c (revision 58588b76)
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 
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
33 
34 static BOOL arm64_get_addr(HANDLE hThread, const CONTEXT* ctx,
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 
87 static BOOL arm64_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame, CONTEXT* context)
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),
97           wine_dbgstr_addr(&frame->AddrReturn),
98           wine_dbgstr_addr(&frame->AddrStack),
99           curr_mode == stm_start ? "start" : "ARM64",
100           wine_dbgstr_longlong(curr_count));
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",
140           wine_dbgstr_longlong(curr_count),
141           frame->FuncTableEntry);
142 
143     return TRUE;
144 done_err:
145     set_curr_mode(stm_done);
146     return FALSE;
147 }
148 #else
149 static BOOL arm64_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame, CONTEXT* context)
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 {
257     if (ctx->ContextFlags && (flags & ThreadWriteInstructionWindow))
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 
277 DECLSPEC_HIDDEN struct cpu cpu_arm64 = {
278     IMAGE_FILE_MACHINE_ARM64,
279     8,
280     CV_ARM64_FP,
281     arm64_get_addr,
282     arm64_stack_walk,
283     NULL,
284     arm64_map_dwarf_register,
285     arm64_fetch_context_reg,
286     arm64_fetch_regname,
287     arm64_fetch_minidump_thread,
288     arm64_fetch_minidump_module,
289 };
290