* Sync up to trunk head (r64921).
[reactos.git] / dll / win32 / dbghelp / cpu_x86_64.c
1 /*
2 * File cpu_x86_64.c
3 *
4 * Copyright (C) 1999, 2005 Alexandre Julliard
5 * Copyright (C) 2009, 2011 Eric Pouech.
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 "dbghelp_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
25
26 /* x86-64 unwind information, for PE modules, as described on MSDN */
27
28 typedef enum _UNWIND_OP_CODES
29 {
30 UWOP_PUSH_NONVOL = 0,
31 UWOP_ALLOC_LARGE,
32 UWOP_ALLOC_SMALL,
33 UWOP_SET_FPREG,
34 UWOP_SAVE_NONVOL,
35 UWOP_SAVE_NONVOL_FAR,
36 UWOP_SAVE_XMM128,
37 UWOP_SAVE_XMM128_FAR,
38 UWOP_PUSH_MACHFRAME
39 } UNWIND_CODE_OPS;
40
41 typedef union _UNWIND_CODE
42 {
43 struct
44 {
45 BYTE CodeOffset;
46 BYTE UnwindOp : 4;
47 BYTE OpInfo : 4;
48 };
49 USHORT FrameOffset;
50 } UNWIND_CODE, *PUNWIND_CODE;
51
52 typedef struct _UNWIND_INFO
53 {
54 BYTE Version : 3;
55 BYTE Flags : 5;
56 BYTE SizeOfProlog;
57 BYTE CountOfCodes;
58 BYTE FrameRegister : 4;
59 BYTE FrameOffset : 4;
60 UNWIND_CODE UnwindCode[1]; /* actually CountOfCodes (aligned) */
61 /*
62 * union
63 * {
64 * OPTIONAL ULONG ExceptionHandler;
65 * OPTIONAL ULONG FunctionEntry;
66 * };
67 * OPTIONAL ULONG ExceptionData[];
68 */
69 } UNWIND_INFO, *PUNWIND_INFO;
70
71 #define GetUnwindCodeEntry(info, index) \
72 ((info)->UnwindCode[index])
73
74 #define GetLanguageSpecificDataPtr(info) \
75 ((PVOID)&GetUnwindCodeEntry((info),((info)->CountOfCodes + 1) & ~1))
76
77 #define GetExceptionHandler(base, info) \
78 ((PEXCEPTION_HANDLER)((base) + *(PULONG)GetLanguageSpecificDataPtr(info)))
79
80 #define GetChainedFunctionEntry(base, info) \
81 ((PRUNTIME_FUNCTION)((base) + *(PULONG)GetLanguageSpecificDataPtr(info)))
82
83 #define GetExceptionDataPtr(info) \
84 ((PVOID)((PULONG)GetLanguageSpecificData(info) + 1)
85
86 static BOOL x86_64_get_addr(HANDLE hThread, const CONTEXT* ctx,
87 enum cpu_addr ca, ADDRESS64* addr)
88 {
89 addr->Mode = AddrModeFlat;
90 switch (ca)
91 {
92 #ifdef __x86_64__
93 case cpu_addr_pc: addr->Segment = ctx->SegCs; addr->Offset = ctx->Rip; return TRUE;
94 case cpu_addr_stack: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rsp; return TRUE;
95 case cpu_addr_frame: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rbp; return TRUE;
96 #endif
97 default: addr->Mode = -1;
98 return FALSE;
99 }
100 }
101
102 enum st_mode {stm_start, stm_64bit, stm_done};
103
104 /* indexes in Reserved array */
105 #define __CurrentMode 0
106 #define __CurrentCount 1
107 /* #define __ 2 (unused) */
108
109 #define curr_mode (frame->Reserved[__CurrentMode])
110 #define curr_count (frame->Reserved[__CurrentCount])
111 /* #define ??? (frame->Reserved[__]) (unused) */
112
113 #ifdef __x86_64__
114 union handler_data
115 {
116 RUNTIME_FUNCTION chain;
117 ULONG handler;
118 };
119
120 static void dump_unwind_info(struct cpu_stack_walk* csw, ULONG64 base, RUNTIME_FUNCTION *function)
121 {
122 static const char * const reg_names[16] =
123 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
124 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
125
126 union handler_data handler_data;
127 char buffer[sizeof(UNWIND_INFO) + 256 * sizeof(UNWIND_CODE)];
128 UNWIND_INFO* info = (UNWIND_INFO*)buffer;
129 unsigned int i, count;
130 RUNTIME_FUNCTION snext;
131 ULONG64 addr;
132
133 TRACE("**** func %x-%x\n", function->BeginAddress, function->EndAddress);
134 for (;;)
135 {
136 if (function->UnwindData & 1)
137 {
138 if (!sw_read_mem(csw, base + function->UnwindData, &snext, sizeof(snext)))
139 {
140 TRACE("Couldn't unwind RUNTIME_INFO at %lx\n", base + function->UnwindData);
141 return;
142 }
143 TRACE("unwind info for function %p-%p chained to function %p-%p\n",
144 (char*)base + function->BeginAddress, (char*)base + function->EndAddress,
145 (char*)base + snext.BeginAddress, (char*)base + snext.EndAddress);
146 function = &snext;
147 continue;
148 }
149 addr = base + function->UnwindData;
150 if (!sw_read_mem(csw, addr, info, FIELD_OFFSET(UNWIND_INFO, UnwindCode)) ||
151 !sw_read_mem(csw, addr + FIELD_OFFSET(UNWIND_INFO, UnwindCode),
152 info->UnwindCode, info->CountOfCodes * sizeof(UNWIND_CODE)))
153 {
154 FIXME("couldn't read memory for UNWIND_INFO at %lx\n", addr);
155 return;
156 }
157 TRACE("unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
158 (char*)addr, info->Flags, info->SizeOfProlog,
159 (char*)base + function->BeginAddress, (char*)base + function->EndAddress);
160
161 if (info->FrameRegister)
162 TRACE(" frame register %s offset 0x%x(%%rsp)\n",
163 reg_names[info->FrameRegister], info->FrameOffset * 16);
164
165 for (i = 0; i < info->CountOfCodes; i++)
166 {
167 TRACE(" 0x%x: ", info->UnwindCode[i].CodeOffset);
168 switch (info->UnwindCode[i].UnwindOp)
169 {
170 case UWOP_PUSH_NONVOL:
171 TRACE("pushq %%%s\n", reg_names[info->UnwindCode[i].OpInfo]);
172 break;
173 case UWOP_ALLOC_LARGE:
174 if (info->UnwindCode[i].OpInfo)
175 {
176 count = *(DWORD*)&info->UnwindCode[i+1];
177 i += 2;
178 }
179 else
180 {
181 count = *(USHORT*)&info->UnwindCode[i+1] * 8;
182 i++;
183 }
184 TRACE("subq $0x%x,%%rsp\n", count);
185 break;
186 case UWOP_ALLOC_SMALL:
187 count = (info->UnwindCode[i].OpInfo + 1) * 8;
188 TRACE("subq $0x%x,%%rsp\n", count);
189 break;
190 case UWOP_SET_FPREG:
191 TRACE("leaq 0x%x(%%rsp),%s\n",
192 info->FrameOffset * 16, reg_names[info->FrameRegister]);
193 break;
194 case UWOP_SAVE_NONVOL:
195 count = *(USHORT*)&info->UnwindCode[i+1] * 8;
196 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names[info->UnwindCode[i].OpInfo], count);
197 i++;
198 break;
199 case UWOP_SAVE_NONVOL_FAR:
200 count = *(DWORD*)&info->UnwindCode[i+1];
201 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names[info->UnwindCode[i].OpInfo], count);
202 i += 2;
203 break;
204 case UWOP_SAVE_XMM128:
205 count = *(USHORT*)&info->UnwindCode[i+1] * 16;
206 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info->UnwindCode[i].OpInfo, count);
207 i++;
208 break;
209 case UWOP_SAVE_XMM128_FAR:
210 count = *(DWORD*)&info->UnwindCode[i+1];
211 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info->UnwindCode[i].OpInfo, count);
212 i += 2;
213 break;
214 case UWOP_PUSH_MACHFRAME:
215 TRACE("PUSH_MACHFRAME %u\n", info->UnwindCode[i].OpInfo);
216 break;
217 default:
218 FIXME("unknown code %u\n", info->UnwindCode[i].UnwindOp);
219 break;
220 }
221 }
222
223 addr += FIELD_OFFSET(UNWIND_INFO, UnwindCode) +
224 ((info->CountOfCodes + 1) & ~1) * sizeof(UNWIND_CODE);
225 if (info->Flags & UNW_FLAG_CHAININFO)
226 {
227 if (!sw_read_mem(csw, addr, &handler_data, sizeof(handler_data.chain)))
228 {
229 FIXME("couldn't read memory for handler_data.chain\n");
230 return;
231 }
232 TRACE(" chained to function %p-%p\n",
233 (char*)base + handler_data.chain.BeginAddress,
234 (char*)base + handler_data.chain.EndAddress);
235 function = &handler_data.chain;
236 continue;
237 }
238 if (info->Flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
239 {
240 if (!sw_read_mem(csw, addr, &handler_data, sizeof(handler_data.handler)))
241 {
242 FIXME("couldn't read memory for handler_data.handler\n");
243 return;
244 }
245 TRACE(" handler %p data at %p\n",
246 (char*)base + handler_data.handler, (char*)addr + sizeof(handler_data.handler));
247 }
248 break;
249 }
250 }
251
252 /* highly derived from dlls/ntdll/signal_x86_64.c */
253 static ULONG64 get_int_reg(CONTEXT *context, int reg)
254 {
255 return *(&context->Rax + reg);
256 }
257
258 static void set_int_reg(CONTEXT *context, int reg, ULONG64 val)
259 {
260 *(&context->Rax + reg) = val;
261 }
262
263 static void set_float_reg(CONTEXT *context, int reg, M128A val)
264 {
265 *(&context->u.s.Xmm0 + reg) = val;
266 }
267
268 static int get_opcode_size(UNWIND_CODE op)
269 {
270 switch (op.UnwindOp)
271 {
272 case UWOP_ALLOC_LARGE:
273 return 2 + (op.OpInfo != 0);
274 case UWOP_SAVE_NONVOL:
275 case UWOP_SAVE_XMM128:
276 return 2;
277 case UWOP_SAVE_NONVOL_FAR:
278 case UWOP_SAVE_XMM128_FAR:
279 return 3;
280 default:
281 return 1;
282 }
283 }
284
285 static BOOL is_inside_epilog(struct cpu_stack_walk* csw, DWORD64 pc,
286 DWORD64 base, const RUNTIME_FUNCTION *function )
287 {
288 BYTE op0, op1, op2;
289 LONG val32;
290
291 if (!sw_read_mem(csw, pc, &op0, 1)) return FALSE;
292
293 /* add or lea must be the first instruction, and it must have a rex.W prefix */
294 if ((op0 & 0xf8) == 0x48)
295 {
296 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
297 if (!sw_read_mem(csw, pc + 2, &op2, 1)) return FALSE;
298 switch (op1)
299 {
300 case 0x81: /* add $nnnn,%rsp */
301 if (op0 == 0x48 && op2 == 0xc4)
302 {
303 pc += 7;
304 break;
305 }
306 return FALSE;
307 case 0x83: /* add $n,%rsp */
308 if (op0 == 0x48 && op2 == 0xc4)
309 {
310 pc += 4;
311 break;
312 }
313 return FALSE;
314 case 0x8d: /* lea n(reg),%rsp */
315 if (op0 & 0x06) return FALSE; /* rex.RX must be cleared */
316 if (((op2 >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
317 if ((op2 & 7) == 4) return FALSE; /* no SIB byte allowed */
318 if ((op2 >> 6) == 1) /* 8-bit offset */
319 {
320 pc += 4;
321 break;
322 }
323 if ((op2 >> 6) == 2) /* 32-bit offset */
324 {
325 pc += 7;
326 break;
327 }
328 return FALSE;
329 }
330 }
331
332 /* now check for various pop instructions */
333 for (;;)
334 {
335 if (!sw_read_mem(csw, pc, &op0, 1)) return FALSE;
336 if ((op0 & 0xf0) == 0x40) /* rex prefix */
337 {
338 if (!sw_read_mem(csw, ++pc, &op0, 1)) return FALSE;
339 }
340
341 switch (op0)
342 {
343 case 0x58: /* pop %rax/%r8 */
344 case 0x59: /* pop %rcx/%r9 */
345 case 0x5a: /* pop %rdx/%r10 */
346 case 0x5b: /* pop %rbx/%r11 */
347 case 0x5c: /* pop %rsp/%r12 */
348 case 0x5d: /* pop %rbp/%r13 */
349 case 0x5e: /* pop %rsi/%r14 */
350 case 0x5f: /* pop %rdi/%r15 */
351 pc++;
352 continue;
353 case 0xc2: /* ret $nn */
354 case 0xc3: /* ret */
355 return TRUE;
356 case 0xe9: /* jmp nnnn */
357 if (!sw_read_mem(csw, pc + 1, &val32, sizeof(LONG))) return FALSE;
358 pc += 5 + val32;
359 if (pc - base >= function->BeginAddress && pc - base < function->EndAddress)
360 continue;
361 break;
362 case 0xeb: /* jmp n */
363 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
364 pc += 2 + (signed char)op1;
365 if (pc - base >= function->BeginAddress && pc - base < function->EndAddress)
366 continue;
367 break;
368 case 0xf3: /* rep; ret (for amd64 prediction bug) */
369 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
370 return op1 == 0xc3;
371 }
372 return FALSE;
373 }
374 }
375
376 static BOOL interpret_epilog(struct cpu_stack_walk* csw, ULONG64 pc, CONTEXT *context )
377 {
378 BYTE insn, val8;
379 WORD val16;
380 LONG val32;
381 DWORD64 val64;
382
383 for (;;)
384 {
385 BYTE rex = 0;
386
387 if (!sw_read_mem(csw, pc, &insn, 1)) return FALSE;
388 if ((insn & 0xf0) == 0x40)
389 {
390 rex = insn & 0x0f; /* rex prefix */
391 if (!sw_read_mem(csw, ++pc, &insn, 1)) return FALSE;
392 }
393
394 switch (insn)
395 {
396 case 0x58: /* pop %rax/r8 */
397 case 0x59: /* pop %rcx/r9 */
398 case 0x5a: /* pop %rdx/r10 */
399 case 0x5b: /* pop %rbx/r11 */
400 case 0x5c: /* pop %rsp/r12 */
401 case 0x5d: /* pop %rbp/r13 */
402 case 0x5e: /* pop %rsi/r14 */
403 case 0x5f: /* pop %rdi/r15 */
404 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
405 set_int_reg(context, insn - 0x58 + (rex & 1) * 8, val64);
406 context->Rsp += sizeof(ULONG64);
407 pc++;
408 continue;
409 case 0x81: /* add $nnnn,%rsp */
410 if (!sw_read_mem(csw, pc + 2, &val32, sizeof(LONG))) return FALSE;
411 context->Rsp += val32;
412 pc += 2 + sizeof(LONG);
413 continue;
414 case 0x83: /* add $n,%rsp */
415 if (!sw_read_mem(csw, pc + 2, &val8, sizeof(BYTE))) return FALSE;
416 context->Rsp += (signed char)val8;
417 pc += 3;
418 continue;
419 case 0x8d:
420 if (!sw_read_mem(csw, pc + 1, &insn, sizeof(BYTE))) return FALSE;
421 if ((insn >> 6) == 1) /* lea n(reg),%rsp */
422 {
423 if (!sw_read_mem(csw, pc + 2, &val8, sizeof(BYTE))) return FALSE;
424 context->Rsp = get_int_reg( context, (insn & 7) + (rex & 1) * 8 ) + (signed char)val8;
425 pc += 3;
426 }
427 else /* lea nnnn(reg),%rsp */
428 {
429 if (!sw_read_mem(csw, pc + 2, &val32, sizeof(LONG))) return FALSE;
430 context->Rsp = get_int_reg( context, (insn & 7) + (rex & 1) * 8 ) + val32;
431 pc += 2 + sizeof(LONG);
432 }
433 continue;
434 case 0xc2: /* ret $nn */
435 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
436 if (!sw_read_mem(csw, pc + 1, &val16, sizeof(WORD))) return FALSE;
437 context->Rip = val64;
438 context->Rsp += sizeof(ULONG64) + val16;
439 return TRUE;
440 case 0xc3: /* ret */
441 case 0xf3: /* rep; ret */
442 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
443 context->Rip = val64;
444 context->Rsp += sizeof(ULONG64);
445 return TRUE;
446 case 0xe9: /* jmp nnnn */
447 if (!sw_read_mem(csw, pc + 1, &val32, sizeof(LONG))) return FALSE;
448 pc += 5 + val32;
449 continue;
450 case 0xeb: /* jmp n */
451 if (!sw_read_mem(csw, pc + 1, &val8, sizeof(BYTE))) return FALSE;
452 pc += 2 + (signed char)val8;
453 continue;
454 }
455 FIXME("unsupported insn %x\n", insn);
456 return FALSE;
457 }
458 }
459
460 static BOOL default_unwind(struct cpu_stack_walk* csw, CONTEXT* context)
461 {
462 if (!sw_read_mem(csw, context->Rsp, &context->Rip, sizeof(DWORD64)))
463 {
464 WARN("Cannot read new frame offset %s\n", wine_dbgstr_longlong(context->Rsp));
465 return FALSE;
466 }
467 context->Rsp += sizeof(DWORD64);
468 return TRUE;
469 }
470
471 static BOOL interpret_function_table_entry(struct cpu_stack_walk* csw,
472 CONTEXT* context, RUNTIME_FUNCTION* function, DWORD64 base)
473 {
474 char buffer[sizeof(UNWIND_INFO) + 256 * sizeof(UNWIND_CODE)];
475 UNWIND_INFO* info = (UNWIND_INFO*)buffer;
476 unsigned i;
477 DWORD64 newframe, prolog_offset, off, value;
478 M128A floatvalue;
479 union handler_data handler_data;
480
481 /* FIXME: we have some assumptions here */
482 assert(context);
483 dump_unwind_info(csw, sw_module_base(csw, context->Rip), function);
484 newframe = context->Rsp;
485 for (;;)
486 {
487 if (!sw_read_mem(csw, base + function->UnwindData, info, sizeof(*info)) ||
488 !sw_read_mem(csw, base + function->UnwindData + FIELD_OFFSET(UNWIND_INFO, UnwindCode),
489 info->UnwindCode, info->CountOfCodes * sizeof(UNWIND_CODE)))
490 {
491 WARN("Couldn't read unwind_code at %lx\n", base + function->UnwindData);
492 return FALSE;
493 }
494
495 if (info->Version != 1)
496 {
497 WARN("unknown unwind info version %u at %lx\n", info->Version, base + function->UnwindData);
498 return FALSE;
499 }
500
501 if (info->FrameRegister)
502 newframe = get_int_reg(context, info->FrameRegister) - info->FrameOffset * 16;
503
504 /* check if in prolog */
505 if (context->Rip >= base + function->BeginAddress &&
506 context->Rip < base + function->BeginAddress + info->SizeOfProlog)
507 {
508 prolog_offset = context->Rip - base - function->BeginAddress;
509 }
510 else
511 {
512 prolog_offset = ~0;
513 if (is_inside_epilog(csw, context->Rip, base, function))
514 {
515 interpret_epilog(csw, context->Rip, context);
516 return TRUE;
517 }
518 }
519
520 for (i = 0; i < info->CountOfCodes; i += get_opcode_size(info->UnwindCode[i]))
521 {
522 if (prolog_offset < info->UnwindCode[i].CodeOffset) continue; /* skip it */
523
524 switch (info->UnwindCode[i].UnwindOp)
525 {
526 case UWOP_PUSH_NONVOL: /* pushq %reg */
527 if (!sw_read_mem(csw, context->Rsp, &value, sizeof(DWORD64))) return FALSE;
528 set_int_reg(context, info->UnwindCode[i].OpInfo, value);
529 context->Rsp += sizeof(ULONG64);
530 break;
531 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
532 if (info->UnwindCode[i].OpInfo) context->Rsp += *(DWORD*)&info->UnwindCode[i+1];
533 else context->Rsp += *(USHORT*)&info->UnwindCode[i+1] * 8;
534 break;
535 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
536 context->Rsp += (info->UnwindCode[i].OpInfo + 1) * 8;
537 break;
538 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
539 context->Rsp = newframe;
540 break;
541 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
542 off = newframe + *(USHORT*)&info->UnwindCode[i+1] * 8;
543 if (!sw_read_mem(csw, off, &value, sizeof(DWORD64))) return FALSE;
544 set_int_reg(context, info->UnwindCode[i].OpInfo, value);
545 break;
546 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
547 off = newframe + *(DWORD*)&info->UnwindCode[i+1];
548 if (!sw_read_mem(csw, off, &value, sizeof(DWORD64))) return FALSE;
549 set_int_reg(context, info->UnwindCode[i].OpInfo, value);
550 break;
551 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
552 off = newframe + *(USHORT*)&info->UnwindCode[i+1] * 16;
553 if (!sw_read_mem(csw, off, &floatvalue, sizeof(M128A))) return FALSE;
554 set_float_reg(context, info->UnwindCode[i].OpInfo, floatvalue);
555 break;
556 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
557 off = newframe + *(DWORD*)&info->UnwindCode[i+1];
558 if (!sw_read_mem(csw, off, &floatvalue, sizeof(M128A))) return FALSE;
559 set_float_reg(context, info->UnwindCode[i].OpInfo, floatvalue);
560 break;
561 case UWOP_PUSH_MACHFRAME:
562 FIXME("PUSH_MACHFRAME %u\n", info->UnwindCode[i].OpInfo);
563 break;
564 default:
565 FIXME("unknown code %u\n", info->UnwindCode[i].UnwindOp);
566 break;
567 }
568 }
569 if (!(info->Flags & UNW_FLAG_CHAININFO)) break;
570 if (!sw_read_mem(csw, base + function->UnwindData + FIELD_OFFSET(UNWIND_INFO, UnwindCode) +
571 ((info->CountOfCodes + 1) & ~1) * sizeof(UNWIND_CODE),
572 &handler_data, sizeof(handler_data))) return FALSE;
573 function = &handler_data.chain; /* restart with the chained info */
574 }
575 return default_unwind(csw, context);
576 }
577
578 /* fetch_next_frame()
579 *
580 * modify (at least) context.{rip, rsp, rbp} using unwind information
581 * either out of PE exception handlers, debug info (dwarf), or simple stack unwind
582 */
583 static BOOL fetch_next_frame(struct cpu_stack_walk* csw, CONTEXT* context,
584 DWORD_PTR curr_pc, void** prtf)
585 {
586 DWORD_PTR cfa;
587 RUNTIME_FUNCTION* rtf;
588 DWORD64 base;
589
590 if (!curr_pc || !(base = sw_module_base(csw, curr_pc))) return FALSE;
591 rtf = sw_table_access(csw, curr_pc);
592 if (prtf) *prtf = rtf;
593 if (rtf)
594 {
595 return interpret_function_table_entry(csw, context, rtf, base);
596 }
597 else if (dwarf2_virtual_unwind(csw, curr_pc, context, &cfa))
598 {
599 context->Rsp = cfa;
600 TRACE("next function rip=%016lx\n", context->Rip);
601 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
602 context->Rax, context->Rbx, context->Rcx, context->Rdx);
603 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
604 context->Rsi, context->Rdi, context->Rbp, context->Rsp);
605 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
606 context->R8, context->R9, context->R10, context->R11);
607 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
608 context->R12, context->R13, context->R14, context->R15);
609 return TRUE;
610 }
611 else
612 return default_unwind(csw, context);
613 }
614
615 static BOOL x86_64_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame, CONTEXT* context)
616 {
617 unsigned deltapc = curr_count <= 1 ? 0 : 1;
618
619 /* sanity check */
620 if (curr_mode >= stm_done) return FALSE;
621 assert(!csw->is32);
622
623 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s\n",
624 wine_dbgstr_addr(&frame->AddrPC),
625 wine_dbgstr_addr(&frame->AddrFrame),
626 wine_dbgstr_addr(&frame->AddrReturn),
627 wine_dbgstr_addr(&frame->AddrStack),
628 curr_mode == stm_start ? "start" : "64bit",
629 wine_dbgstr_longlong(curr_count));
630
631 if (curr_mode == stm_start)
632 {
633 if ((frame->AddrPC.Mode == AddrModeFlat) &&
634 (frame->AddrFrame.Mode != AddrModeFlat))
635 {
636 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
637 goto done_err;
638 }
639
640 /* Init done */
641 curr_mode = stm_64bit;
642 frame->AddrReturn.Mode = frame->AddrStack.Mode = AddrModeFlat;
643 /* don't set up AddrStack on first call. Either the caller has set it up, or
644 * we will get it in the next frame
645 */
646 memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
647 }
648 else
649 {
650 if (context->Rsp != frame->AddrStack.Offset) FIXME("inconsistent Stack Pointer\n");
651 if (context->Rip != frame->AddrPC.Offset) FIXME("inconsistent Instruction Pointer\n");
652
653 if (frame->AddrReturn.Offset == 0) goto done_err;
654 if (!fetch_next_frame(csw, context, frame->AddrPC.Offset - deltapc, &frame->FuncTableEntry))
655 goto done_err;
656 deltapc = 1;
657 }
658
659 memset(&frame->Params, 0, sizeof(frame->Params));
660
661 /* set frame information */
662 frame->AddrStack.Offset = context->Rsp;
663 frame->AddrFrame.Offset = context->Rbp;
664 frame->AddrPC.Offset = context->Rip;
665 if (1)
666 {
667 CONTEXT newctx = *context;
668
669 if (!fetch_next_frame(csw, &newctx, frame->AddrPC.Offset - deltapc, NULL))
670 goto done_err;
671 frame->AddrReturn.Mode = AddrModeFlat;
672 frame->AddrReturn.Offset = newctx.Rip;
673 }
674
675 frame->Far = TRUE;
676 frame->Virtual = TRUE;
677 curr_count++;
678
679 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s FuncTable=%p\n",
680 wine_dbgstr_addr(&frame->AddrPC),
681 wine_dbgstr_addr(&frame->AddrFrame),
682 wine_dbgstr_addr(&frame->AddrReturn),
683 wine_dbgstr_addr(&frame->AddrStack),
684 curr_mode == stm_start ? "start" : "64bit",
685 wine_dbgstr_longlong(curr_count),
686 frame->FuncTableEntry);
687
688 return TRUE;
689 done_err:
690 curr_mode = stm_done;
691 return FALSE;
692 }
693 #else
694 static BOOL x86_64_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame, CONTEXT* context)
695 {
696 return FALSE;
697 }
698 #endif
699
700 static void* x86_64_find_runtime_function(struct module* module, DWORD64 addr)
701 {
702 #ifdef __x86_64__
703 RUNTIME_FUNCTION* rtf;
704 ULONG size;
705 int min, max;
706
707 rtf = (RUNTIME_FUNCTION*)pe_map_directory(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size);
708 if (rtf) for (min = 0, max = size / sizeof(*rtf); min <= max; )
709 {
710 int pos = (min + max) / 2;
711 if (addr < module->module.BaseOfImage + rtf[pos].BeginAddress) max = pos - 1;
712 else if (addr >= module->module.BaseOfImage + rtf[pos].EndAddress) min = pos + 1;
713 else
714 {
715 rtf += pos;
716 while (rtf->UnwindData & 1) /* follow chained entry */
717 {
718 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
719 /* we need to read into the other process */
720 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
721 }
722 return rtf;
723 }
724 }
725 #endif
726 return NULL;
727 }
728
729 static unsigned x86_64_map_dwarf_register(unsigned regno)
730 {
731 unsigned reg;
732
733 if (regno >= 17 && regno <= 24)
734 reg = CV_AMD64_XMM0 + regno - 17;
735 else if (regno >= 25 && regno <= 32)
736 reg = CV_AMD64_XMM8 + regno - 25;
737 else if (regno >= 33 && regno <= 40)
738 reg = CV_AMD64_ST0 + regno - 33;
739 else switch (regno)
740 {
741 case 0: reg = CV_AMD64_RAX; break;
742 case 1: reg = CV_AMD64_RDX; break;
743 case 2: reg = CV_AMD64_RCX; break;
744 case 3: reg = CV_AMD64_RBX; break;
745 case 4: reg = CV_AMD64_RSI; break;
746 case 5: reg = CV_AMD64_RDI; break;
747 case 6: reg = CV_AMD64_RBP; break;
748 case 7: reg = CV_AMD64_RSP; break;
749 case 8: reg = CV_AMD64_R8; break;
750 case 9: reg = CV_AMD64_R9; break;
751 case 10: reg = CV_AMD64_R10; break;
752 case 11: reg = CV_AMD64_R11; break;
753 case 12: reg = CV_AMD64_R12; break;
754 case 13: reg = CV_AMD64_R13; break;
755 case 14: reg = CV_AMD64_R14; break;
756 case 15: reg = CV_AMD64_R15; break;
757 case 16: reg = CV_AMD64_RIP; break;
758 case 49: reg = CV_AMD64_EFLAGS; break;
759 case 50: reg = CV_AMD64_ES; break;
760 case 51: reg = CV_AMD64_CS; break;
761 case 52: reg = CV_AMD64_SS; break;
762 case 53: reg = CV_AMD64_DS; break;
763 case 54: reg = CV_AMD64_FS; break;
764 case 55: reg = CV_AMD64_GS; break;
765 case 62: reg = CV_AMD64_TR; break;
766 case 63: reg = CV_AMD64_LDTR; break;
767 case 64: reg = CV_AMD64_MXCSR; break;
768 case 65: reg = CV_AMD64_CTRL; break;
769 case 66: reg = CV_AMD64_STAT; break;
770 /*
771 * 56-57 reserved
772 * 58 %fs.base
773 * 59 %gs.base
774 * 60-61 reserved
775 */
776 default:
777 FIXME("Don't know how to map register %d\n", regno);
778 return 0;
779 }
780 return reg;
781 }
782
783 static void* x86_64_fetch_context_reg(CONTEXT* ctx, unsigned regno, unsigned* size)
784 {
785 #ifdef __x86_64__
786 switch (regno)
787 {
788 case CV_AMD64_RAX: *size = sizeof(ctx->Rax); return &ctx->Rax;
789 case CV_AMD64_RDX: *size = sizeof(ctx->Rdx); return &ctx->Rdx;
790 case CV_AMD64_RCX: *size = sizeof(ctx->Rcx); return &ctx->Rcx;
791 case CV_AMD64_RBX: *size = sizeof(ctx->Rbx); return &ctx->Rbx;
792 case CV_AMD64_RSI: *size = sizeof(ctx->Rsi); return &ctx->Rsi;
793 case CV_AMD64_RDI: *size = sizeof(ctx->Rdi); return &ctx->Rdi;
794 case CV_AMD64_RBP: *size = sizeof(ctx->Rbp); return &ctx->Rbp;
795 case CV_AMD64_RSP: *size = sizeof(ctx->Rsp); return &ctx->Rsp;
796 case CV_AMD64_R8: *size = sizeof(ctx->R8); return &ctx->R8;
797 case CV_AMD64_R9: *size = sizeof(ctx->R9); return &ctx->R9;
798 case CV_AMD64_R10: *size = sizeof(ctx->R10); return &ctx->R10;
799 case CV_AMD64_R11: *size = sizeof(ctx->R11); return &ctx->R11;
800 case CV_AMD64_R12: *size = sizeof(ctx->R12); return &ctx->R12;
801 case CV_AMD64_R13: *size = sizeof(ctx->R13); return &ctx->R13;
802 case CV_AMD64_R14: *size = sizeof(ctx->R14); return &ctx->R14;
803 case CV_AMD64_R15: *size = sizeof(ctx->R15); return &ctx->R15;
804 case CV_AMD64_RIP: *size = sizeof(ctx->Rip); return &ctx->Rip;
805
806 case CV_AMD64_XMM0 + 0: *size = sizeof(ctx->u.s.Xmm0 ); return &ctx->u.s.Xmm0;
807 case CV_AMD64_XMM0 + 1: *size = sizeof(ctx->u.s.Xmm1 ); return &ctx->u.s.Xmm1;
808 case CV_AMD64_XMM0 + 2: *size = sizeof(ctx->u.s.Xmm2 ); return &ctx->u.s.Xmm2;
809 case CV_AMD64_XMM0 + 3: *size = sizeof(ctx->u.s.Xmm3 ); return &ctx->u.s.Xmm3;
810 case CV_AMD64_XMM0 + 4: *size = sizeof(ctx->u.s.Xmm4 ); return &ctx->u.s.Xmm4;
811 case CV_AMD64_XMM0 + 5: *size = sizeof(ctx->u.s.Xmm5 ); return &ctx->u.s.Xmm5;
812 case CV_AMD64_XMM0 + 6: *size = sizeof(ctx->u.s.Xmm6 ); return &ctx->u.s.Xmm6;
813 case CV_AMD64_XMM0 + 7: *size = sizeof(ctx->u.s.Xmm7 ); return &ctx->u.s.Xmm7;
814 case CV_AMD64_XMM8 + 0: *size = sizeof(ctx->u.s.Xmm8 ); return &ctx->u.s.Xmm8;
815 case CV_AMD64_XMM8 + 1: *size = sizeof(ctx->u.s.Xmm9 ); return &ctx->u.s.Xmm9;
816 case CV_AMD64_XMM8 + 2: *size = sizeof(ctx->u.s.Xmm10); return &ctx->u.s.Xmm10;
817 case CV_AMD64_XMM8 + 3: *size = sizeof(ctx->u.s.Xmm11); return &ctx->u.s.Xmm11;
818 case CV_AMD64_XMM8 + 4: *size = sizeof(ctx->u.s.Xmm12); return &ctx->u.s.Xmm12;
819 case CV_AMD64_XMM8 + 5: *size = sizeof(ctx->u.s.Xmm13); return &ctx->u.s.Xmm13;
820 case CV_AMD64_XMM8 + 6: *size = sizeof(ctx->u.s.Xmm14); return &ctx->u.s.Xmm14;
821 case CV_AMD64_XMM8 + 7: *size = sizeof(ctx->u.s.Xmm15); return &ctx->u.s.Xmm15;
822
823 case CV_AMD64_ST0 + 0: *size = sizeof(ctx->u.s.Legacy[0]); return &ctx->u.s.Legacy[0];
824 case CV_AMD64_ST0 + 1: *size = sizeof(ctx->u.s.Legacy[1]); return &ctx->u.s.Legacy[1];
825 case CV_AMD64_ST0 + 2: *size = sizeof(ctx->u.s.Legacy[2]); return &ctx->u.s.Legacy[2];
826 case CV_AMD64_ST0 + 3: *size = sizeof(ctx->u.s.Legacy[3]); return &ctx->u.s.Legacy[3];
827 case CV_AMD64_ST0 + 4: *size = sizeof(ctx->u.s.Legacy[4]); return &ctx->u.s.Legacy[4];
828 case CV_AMD64_ST0 + 5: *size = sizeof(ctx->u.s.Legacy[5]); return &ctx->u.s.Legacy[5];
829 case CV_AMD64_ST0 + 6: *size = sizeof(ctx->u.s.Legacy[6]); return &ctx->u.s.Legacy[6];
830 case CV_AMD64_ST0 + 7: *size = sizeof(ctx->u.s.Legacy[7]); return &ctx->u.s.Legacy[7];
831
832 case CV_AMD64_EFLAGS: *size = sizeof(ctx->EFlags); return &ctx->EFlags;
833 case CV_AMD64_ES: *size = sizeof(ctx->SegEs); return &ctx->SegEs;
834 case CV_AMD64_CS: *size = sizeof(ctx->SegCs); return &ctx->SegCs;
835 case CV_AMD64_SS: *size = sizeof(ctx->SegSs); return &ctx->SegSs;
836 case CV_AMD64_DS: *size = sizeof(ctx->SegDs); return &ctx->SegDs;
837 case CV_AMD64_FS: *size = sizeof(ctx->SegFs); return &ctx->SegFs;
838 case CV_AMD64_GS: *size = sizeof(ctx->SegGs); return &ctx->SegGs;
839
840 }
841 #endif
842 FIXME("Unknown register %x\n", regno);
843 return NULL;
844 }
845
846 static const char* x86_64_fetch_regname(unsigned regno)
847 {
848 switch (regno)
849 {
850 case CV_AMD64_RAX: return "rax";
851 case CV_AMD64_RDX: return "rdx";
852 case CV_AMD64_RCX: return "rcx";
853 case CV_AMD64_RBX: return "rbx";
854 case CV_AMD64_RSI: return "rsi";
855 case CV_AMD64_RDI: return "rdi";
856 case CV_AMD64_RBP: return "rbp";
857 case CV_AMD64_RSP: return "rsp";
858 case CV_AMD64_R8: return "r8";
859 case CV_AMD64_R9: return "r9";
860 case CV_AMD64_R10: return "r10";
861 case CV_AMD64_R11: return "r11";
862 case CV_AMD64_R12: return "r12";
863 case CV_AMD64_R13: return "r13";
864 case CV_AMD64_R14: return "r14";
865 case CV_AMD64_R15: return "r15";
866 case CV_AMD64_RIP: return "rip";
867
868 case CV_AMD64_XMM0 + 0: return "xmm0";
869 case CV_AMD64_XMM0 + 1: return "xmm1";
870 case CV_AMD64_XMM0 + 2: return "xmm2";
871 case CV_AMD64_XMM0 + 3: return "xmm3";
872 case CV_AMD64_XMM0 + 4: return "xmm4";
873 case CV_AMD64_XMM0 + 5: return "xmm5";
874 case CV_AMD64_XMM0 + 6: return "xmm6";
875 case CV_AMD64_XMM0 + 7: return "xmm7";
876 case CV_AMD64_XMM8 + 0: return "xmm8";
877 case CV_AMD64_XMM8 + 1: return "xmm9";
878 case CV_AMD64_XMM8 + 2: return "xmm10";
879 case CV_AMD64_XMM8 + 3: return "xmm11";
880 case CV_AMD64_XMM8 + 4: return "xmm12";
881 case CV_AMD64_XMM8 + 5: return "xmm13";
882 case CV_AMD64_XMM8 + 6: return "xmm14";
883 case CV_AMD64_XMM8 + 7: return "xmm15";
884
885 case CV_AMD64_ST0 + 0: return "st0";
886 case CV_AMD64_ST0 + 1: return "st1";
887 case CV_AMD64_ST0 + 2: return "st2";
888 case CV_AMD64_ST0 + 3: return "st3";
889 case CV_AMD64_ST0 + 4: return "st4";
890 case CV_AMD64_ST0 + 5: return "st5";
891 case CV_AMD64_ST0 + 6: return "st6";
892 case CV_AMD64_ST0 + 7: return "st7";
893
894 case CV_AMD64_EFLAGS: return "eflags";
895 case CV_AMD64_ES: return "es";
896 case CV_AMD64_CS: return "cs";
897 case CV_AMD64_SS: return "ss";
898 case CV_AMD64_DS: return "ds";
899 case CV_AMD64_FS: return "fs";
900 case CV_AMD64_GS: return "gs";
901 }
902 FIXME("Unknown register %x\n", regno);
903 return NULL;
904 }
905
906 static BOOL x86_64_fetch_minidump_thread(struct dump_context* dc, unsigned index, unsigned flags, const CONTEXT* ctx)
907 {
908 if (ctx->ContextFlags && (flags & ThreadWriteInstructionWindow))
909 {
910 /* FIXME: crop values across module boundaries, */
911 #ifdef __x86_64__
912 ULONG64 base = ctx->Rip <= 0x80 ? 0 : ctx->Rip - 0x80;
913 minidump_add_memory_block(dc, base, ctx->Rip + 0x80 - base, 0);
914 #endif
915 }
916
917 return TRUE;
918 }
919
920 static BOOL x86_64_fetch_minidump_module(struct dump_context* dc, unsigned index, unsigned flags)
921 {
922 /* FIXME: not sure about the flags... */
923 if (1)
924 {
925 /* FIXME: crop values across module boundaries, */
926 #ifdef __x86_64__
927 struct process* pcs;
928 struct module* module;
929 const RUNTIME_FUNCTION* rtf;
930 ULONG size;
931
932 if (!(pcs = process_find_by_handle(dc->hProcess)) ||
933 !(module = module_find_by_addr(pcs, dc->modules[index].base, DMT_UNKNOWN)))
934 return FALSE;
935 rtf = (const RUNTIME_FUNCTION*)pe_map_directory(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size);
936 if (rtf)
937 {
938 const RUNTIME_FUNCTION* end = (const RUNTIME_FUNCTION*)((const char*)rtf + size);
939 UNWIND_INFO ui;
940
941 while (rtf + 1 < end)
942 {
943 while (rtf->UnwindData & 1) /* follow chained entry */
944 {
945 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
946 return FALSE;
947 /* we need to read into the other process */
948 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
949 }
950 if (ReadProcessMemory(dc->hProcess,
951 (void*)(dc->modules[index].base + rtf->UnwindData),
952 &ui, sizeof(ui), NULL))
953 minidump_add_memory_block(dc, dc->modules[index].base + rtf->UnwindData,
954 FIELD_OFFSET(UNWIND_INFO, UnwindCode) + ui.CountOfCodes * sizeof(UNWIND_CODE), 0);
955 rtf++;
956 }
957 }
958 #endif
959 }
960
961 return TRUE;
962 }
963
964 DECLSPEC_HIDDEN struct cpu cpu_x86_64 = {
965 IMAGE_FILE_MACHINE_AMD64,
966 8,
967 CV_AMD64_RSP,
968 x86_64_get_addr,
969 x86_64_stack_walk,
970 x86_64_find_runtime_function,
971 x86_64_map_dwarf_register,
972 x86_64_fetch_context_reg,
973 x86_64_fetch_regname,
974 x86_64_fetch_minidump_thread,
975 x86_64_fetch_minidump_module,
976 };