ReactOS  0.4.14-dev-317-g96040ec
cpu_arm.c
Go to the documentation of this file.
1 /*
2  * File cpu_arm.c
3  *
4  * Copyright (C) 2009 Eric Pouech
5  * Copyright (C) 2010, 2011 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 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "dbghelp_private.h"
27 #include "winternl.h"
28 #include "wine/debug.h"
29 
31 
32 static BOOL arm_get_addr(HANDLE hThread, const CONTEXT* ctx,
33  enum cpu_addr ca, ADDRESS64* addr)
34 {
35  addr->Mode = AddrModeFlat;
36  addr->Segment = 0; /* don't need segment */
37  switch (ca)
38  {
39 #ifdef __arm__
40  case cpu_addr_pc: addr->Offset = ctx->Pc; return TRUE;
41  case cpu_addr_stack: addr->Offset = ctx->Sp; return TRUE;
42 #ifdef __REACTOS__
43  case cpu_addr_frame: addr->Offset = ctx->R11; return TRUE;
44 #else
45  case cpu_addr_frame: addr->Offset = ctx->Fp; return TRUE;
46 #endif
47 #endif
48  default: addr->Mode = -1;
49  return FALSE;
50  }
51 }
52 
53 #ifdef __arm__
54 enum st_mode {stm_start, stm_arm, stm_done};
55 
56 /* indexes in Reserved array */
57 #define __CurrentModeCount 0
58 
59 #define curr_mode (frame->Reserved[__CurrentModeCount] & 0x0F)
60 #define curr_count (frame->Reserved[__CurrentModeCount] >> 4)
61 
62 #define set_curr_mode(m) {frame->Reserved[__CurrentModeCount] &= ~0x0F; frame->Reserved[__CurrentModeCount] |= (m & 0x0F);}
63 #define inc_curr_count() (frame->Reserved[__CurrentModeCount] += 0x10)
64 
65 /* fetch_next_frame()
66  *
67  * modify (at least) context.Pc using unwind information
68  * either out of debug info (dwarf), or simple Lr trace
69  */
70 static BOOL fetch_next_frame(struct cpu_stack_walk* csw,
71  CONTEXT* context, DWORD_PTR curr_pc)
72 {
73  DWORD_PTR xframe;
74  DWORD oldReturn = context->Lr;
75 
76  if (dwarf2_virtual_unwind(csw, curr_pc, context, &xframe))
77  {
78  context->Sp = xframe;
79  context->Pc = oldReturn;
80  return TRUE;
81  }
82 
83  if (context->Pc == context->Lr) return FALSE;
84  context->Pc = oldReturn;
85 
86  return TRUE;
87 }
88 
90 {
91  unsigned deltapc = curr_count <= 1 ? 0 : 4;
92 
93  /* sanity check */
94  if (curr_mode >= stm_done) return FALSE;
95 
96  TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s\n",
97  wine_dbgstr_addr(&frame->AddrPC),
98  wine_dbgstr_addr(&frame->AddrFrame),
100  wine_dbgstr_addr(&frame->AddrStack),
101  curr_mode == stm_start ? "start" : "ARM",
103 
104  if (curr_mode == stm_start)
105  {
106  /* Init done */
107  set_curr_mode(stm_arm);
108  frame->AddrReturn.Mode = frame->AddrStack.Mode = AddrModeFlat;
109  /* don't set up AddrStack on first call. Either the caller has set it up, or
110  * we will get it in the next frame
111  */
112  memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
113  }
114  else
115  {
116  if (context->Sp != frame->AddrStack.Offset) FIXME("inconsistent Stack Pointer\n");
117  if (context->Pc != frame->AddrPC.Offset) FIXME("inconsistent Program Counter\n");
118 
119  if (frame->AddrReturn.Offset == 0) goto done_err;
120  if (!fetch_next_frame(csw, context, frame->AddrPC.Offset - deltapc))
121  goto done_err;
122  }
123 
124  memset(&frame->Params, 0, sizeof(frame->Params));
125 
126  /* set frame information */
127  frame->AddrStack.Offset = context->Sp;
128  frame->AddrReturn.Offset = context->Lr;
129 #ifdef __REACTOS__
130  frame->AddrFrame.Offset = context->R11;
131 #else
132  frame->AddrFrame.Offset = context->Fp;
133 #endif
134  frame->AddrPC.Offset = context->Pc;
135 
136  frame->Far = TRUE;
137  frame->Virtual = TRUE;
138  inc_curr_count();
139 
140  TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s FuncTable=%p\n",
141  wine_dbgstr_addr(&frame->AddrPC),
142  wine_dbgstr_addr(&frame->AddrFrame),
143  wine_dbgstr_addr(&frame->AddrReturn),
144  wine_dbgstr_addr(&frame->AddrStack),
145  curr_mode == stm_start ? "start" : "ARM",
147  frame->FuncTableEntry);
148 
149  return TRUE;
150 done_err:
152  return FALSE;
153 }
154 #else
156 {
157  return FALSE;
158 }
159 #endif
160 
161 static unsigned arm_map_dwarf_register(unsigned regno, BOOL eh_frame)
162 {
163  if (regno <= 15) return CV_ARM_R0 + regno;
164  if (regno == 128) return CV_ARM_CPSR;
165 
166  FIXME("Don't know how to map register %d\n", regno);
167  return CV_ARM_NOREG;
168 }
169 
170 static void* arm_fetch_context_reg(CONTEXT* ctx, unsigned regno, unsigned* size)
171 {
172 #ifdef __arm__
173  switch (regno)
174  {
175  case CV_ARM_R0 + 0: *size = sizeof(ctx->R0); return &ctx->R0;
176  case CV_ARM_R0 + 1: *size = sizeof(ctx->R1); return &ctx->R1;
177  case CV_ARM_R0 + 2: *size = sizeof(ctx->R2); return &ctx->R2;
178  case CV_ARM_R0 + 3: *size = sizeof(ctx->R3); return &ctx->R3;
179  case CV_ARM_R0 + 4: *size = sizeof(ctx->R4); return &ctx->R4;
180  case CV_ARM_R0 + 5: *size = sizeof(ctx->R5); return &ctx->R5;
181  case CV_ARM_R0 + 6: *size = sizeof(ctx->R6); return &ctx->R6;
182  case CV_ARM_R0 + 7: *size = sizeof(ctx->R7); return &ctx->R7;
183  case CV_ARM_R0 + 8: *size = sizeof(ctx->R8); return &ctx->R8;
184  case CV_ARM_R0 + 9: *size = sizeof(ctx->R9); return &ctx->R9;
185  case CV_ARM_R0 + 10: *size = sizeof(ctx->R10); return &ctx->R10;
186 #ifdef __REACTOS__
187  case CV_ARM_R0 + 11: *size = sizeof(ctx->R11); return &ctx->R11;
188  case CV_ARM_R0 + 12: *size = sizeof(ctx->R12); return &ctx->R12;
189 #else
190  case CV_ARM_R0 + 11: *size = sizeof(ctx->Fp); return &ctx->Fp;
191  case CV_ARM_R0 + 12: *size = sizeof(ctx->Ip); return &ctx->Ip;
192 #endif
193 
194  case CV_ARM_SP: *size = sizeof(ctx->Sp); return &ctx->Sp;
195  case CV_ARM_LR: *size = sizeof(ctx->Lr); return &ctx->Lr;
196  case CV_ARM_PC: *size = sizeof(ctx->Pc); return &ctx->Pc;
197  case CV_ARM_CPSR: *size = sizeof(ctx->Cpsr); return &ctx->Cpsr;
198  }
199 #endif
200  FIXME("Unknown register %x\n", regno);
201  return NULL;
202 }
203 
204 static const char* arm_fetch_regname(unsigned regno)
205 {
206  switch (regno)
207  {
208  case CV_ARM_R0 + 0: return "r0";
209  case CV_ARM_R0 + 1: return "r1";
210  case CV_ARM_R0 + 2: return "r2";
211  case CV_ARM_R0 + 3: return "r3";
212  case CV_ARM_R0 + 4: return "r4";
213  case CV_ARM_R0 + 5: return "r5";
214  case CV_ARM_R0 + 6: return "r6";
215  case CV_ARM_R0 + 7: return "r7";
216  case CV_ARM_R0 + 8: return "r8";
217  case CV_ARM_R0 + 9: return "r9";
218  case CV_ARM_R0 + 10: return "r10";
219  case CV_ARM_R0 + 11: return "r11";
220  case CV_ARM_R0 + 12: return "r12";
221 
222  case CV_ARM_SP: return "sp";
223  case CV_ARM_LR: return "lr";
224  case CV_ARM_PC: return "pc";
225  case CV_ARM_CPSR: return "cpsr";
226  }
227  FIXME("Unknown register %x\n", regno);
228  return NULL;
229 }
230 
231 static BOOL arm_fetch_minidump_thread(struct dump_context* dc, unsigned index, unsigned flags, const CONTEXT* ctx)
232 {
234  {
235  /* FIXME: crop values across module boundaries, */
236 #ifdef __arm__
237  ULONG base = ctx->Pc <= 0x80 ? 0 : ctx->Pc - 0x80;
238  minidump_add_memory_block(dc, base, ctx->Pc + 0x80 - base, 0);
239 #endif
240  }
241 
242  return TRUE;
243 }
244 
245 static BOOL arm_fetch_minidump_module(struct dump_context* dc, unsigned index, unsigned flags)
246 {
247  /* FIXME: actually, we should probably take care of FPO data, unless it's stored in
248  * function table minidump stream
249  */
250  return FALSE;
251 }
252 
255  4,
256  CV_ARM_R0 + 11,
257  arm_get_addr,
259  NULL,
265 };
static void * arm_fetch_context_reg(CONTEXT *ctx, unsigned regno, unsigned *size)
Definition: cpu_arm.c:170
#define TRUE
Definition: types.h:120
#define set_curr_mode(m)
Definition: cpu_i386.c:155
static unsigned arm_map_dwarf_register(unsigned regno, BOOL eh_frame)
Definition: cpu_arm.c:161
DECLSPEC_HIDDEN struct cpu cpu_arm
Definition: cpu_arm.c:253
Definition: http.c:6587
ULONG R6
Definition: compat.h:341
ULONG R0
Definition: compat.h:335
HDC dc
Definition: cylfrac.c:34
DWORD64 Params[4]
Definition: compat.h:1048
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
ULONG R8
Definition: compat.h:343
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 const char * arm_fetch_regname(unsigned regno)
Definition: cpu_arm.c:204
ULONG R9
Definition: compat.h:344
ULONG R1
Definition: compat.h:336
static BOOL arm_fetch_minidump_module(struct dump_context *dc, unsigned index, unsigned flags)
Definition: cpu_arm.c:245
GLuint index
Definition: glext.h:6031
ULONG R4
Definition: compat.h:339
ULONG ContextFlags
Definition: compat.h:331
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
ULONG R11
Definition: ke.h:266
DWORD64 Offset
Definition: compat.h:815
static BOOL arm_get_addr(HANDLE hThread, const CONTEXT *ctx, enum cpu_addr ca, ADDRESS64 *addr)
Definition: cpu_arm.c:32
ADDRESS64 AddrReturn
Definition: compat.h:1043
unsigned long DWORD
Definition: ntddk_ex.h:95
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
uint32_t DWORD_PTR
Definition: typedefs.h:63
ULONG Ip
Definition: compat.h:347
st_mode
Definition: cpu_i386.c:143
#define DECLSPEC_HIDDEN
Definition: precomp.h:8
WINE_DEFAULT_DEBUG_CHANNEL(dbghelp)
ULONG Cpsr
Definition: compat.h:353
ULONG R5
Definition: compat.h:340
static BOOL arm_fetch_minidump_thread(struct dump_context *dc, unsigned index, unsigned flags, const CONTEXT *ctx)
Definition: cpu_arm.c:231
#define IMAGE_FILE_MACHINE_ARMNT
Definition: compat.h:116
ADDRESS64 AddrPC
Definition: compat.h:1042
ULONG R7
Definition: compat.h:342
ULONG R12
Definition: ke.h:267
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
ULONG R10
Definition: compat.h:345
ULONG R2
Definition: compat.h:337
#define curr_count
Definition: cpu_i386.c:151
static const WCHAR ca[]
Definition: main.c:455
ULONG Pc
Definition: compat.h:352
#define memset(x, y, z)
Definition: compat.h:39
ULONG R3
Definition: compat.h:338
ADDRESS_MODE Mode
Definition: compat.h:817
static BOOL arm_stack_walk(struct cpu_stack_walk *csw, LPSTACKFRAME64 frame, CONTEXT *context)
Definition: cpu_arm.c:155
ADDRESS64 AddrFrame
Definition: compat.h:1044
BOOL Virtual
Definition: compat.h:1050
ULONG Lr
Definition: compat.h:351