be_x86_64.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * Debugger x86_64 specific functions
  3. *
  4. * Copyright 2004 Vincent Béron
  5. * Copyright 2009 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. #define NONAMELESSSTRUCT
  22. #define NONAMELESSUNION
  23. #include "debugger.h"
  24. #include "wine/debug.h"
  25. #if defined(__x86_64__)
  26. WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
  27. #define STEP_FLAG 0x00000100 /* single step flag */
  28. static BOOL be_x86_64_get_addr(HANDLE hThread, const dbg_ctx_t *ctx,
  29. enum be_cpu_addr bca, ADDRESS64* addr)
  30. {
  31. addr->Mode = AddrModeFlat;
  32. switch (bca)
  33. {
  34. case be_cpu_addr_pc:
  35. addr->Segment = ctx->ctx.SegCs;
  36. addr->Offset = ctx->ctx.Rip;
  37. return TRUE;
  38. case be_cpu_addr_stack:
  39. addr->Segment = ctx->ctx.SegSs;
  40. addr->Offset = ctx->ctx.Rsp;
  41. return TRUE;
  42. case be_cpu_addr_frame:
  43. addr->Segment = ctx->ctx.SegSs;
  44. addr->Offset = ctx->ctx.Rbp;
  45. return TRUE;
  46. default:
  47. addr->Mode = -1;
  48. return FALSE;
  49. }
  50. }
  51. static BOOL be_x86_64_get_register_info(int regno, enum be_cpu_addr* kind)
  52. {
  53. /* this is true when running in 32bit mode... and wrong in 64 :-/ */
  54. switch (regno)
  55. {
  56. case CV_AMD64_RIP: *kind = be_cpu_addr_pc; return TRUE;
  57. case CV_AMD64_EBP: *kind = be_cpu_addr_frame; return TRUE;
  58. case CV_AMD64_ESP: *kind = be_cpu_addr_stack; return TRUE;
  59. }
  60. return FALSE;
  61. }
  62. static void be_x86_64_single_step(dbg_ctx_t *ctx, BOOL enable)
  63. {
  64. if (enable) ctx->ctx.EFlags |= STEP_FLAG;
  65. else ctx->ctx.EFlags &= ~STEP_FLAG;
  66. }
  67. static void be_x86_64_print_context(HANDLE hThread, const dbg_ctx_t *pctx,
  68. int all_regs)
  69. {
  70. static const char mxcsr_flags[16][4] = { "IE", "DE", "ZE", "OE", "UE", "PE", "DAZ", "IM",
  71. "DM", "ZM", "OM", "UM", "PM", "R-", "R+", "FZ" };
  72. static const char flags[] = "aVR-N--ODITSZ-A-P-C";
  73. const CONTEXT *ctx = &pctx->ctx;
  74. char buf[33];
  75. int i;
  76. strcpy(buf, flags);
  77. for (i = 0; buf[i]; i++)
  78. if (buf[i] != '-' && !(ctx->EFlags & (1 << (sizeof(flags) - 2 - i))))
  79. buf[i] = ' ';
  80. dbg_printf("Register dump:\n");
  81. dbg_printf(" rip:%016I64x rsp:%016I64x rbp:%016I64x eflags:%08lx (%s)\n",
  82. ctx->Rip, ctx->Rsp, ctx->Rbp, ctx->EFlags, buf);
  83. dbg_printf(" rax:%016I64x rbx:%016I64x rcx:%016I64x rdx:%016I64x\n",
  84. ctx->Rax, ctx->Rbx, ctx->Rcx, ctx->Rdx);
  85. dbg_printf(" rsi:%016I64x rdi:%016I64x r8:%016I64x r9:%016I64x r10:%016I64x\n",
  86. ctx->Rsi, ctx->Rdi, ctx->R8, ctx->R9, ctx->R10 );
  87. dbg_printf(" r11:%016I64x r12:%016I64x r13:%016I64x r14:%016I64x r15:%016I64x\n",
  88. ctx->R11, ctx->R12, ctx->R13, ctx->R14, ctx->R15 );
  89. if (!all_regs) return;
  90. dbg_printf(" cs:%04x ds:%04x es:%04x fs:%04x gs:%04x ss:%04x\n",
  91. ctx->SegCs, ctx->SegDs, ctx->SegEs, ctx->SegFs, ctx->SegGs, ctx->SegSs );
  92. dbg_printf("Debug:\n");
  93. dbg_printf(" dr0:%016I64x dr1:%016I64x dr2:%016I64x dr3:%016I64x\n",
  94. ctx->Dr0, ctx->Dr1, ctx->Dr2, ctx->Dr3 );
  95. dbg_printf(" dr6:%016I64x dr7:%016I64x\n", ctx->Dr6, ctx->Dr7 );
  96. dbg_printf("Floating point:\n");
  97. dbg_printf(" flcw:%04x ", LOWORD(ctx->u.FltSave.ControlWord));
  98. dbg_printf(" fltw:%04x ", LOWORD(ctx->u.FltSave.TagWord));
  99. dbg_printf(" flsw:%04x", LOWORD(ctx->u.FltSave.StatusWord));
  100. dbg_printf("(cc:%d%d%d%d", (ctx->u.FltSave.StatusWord & 0x00004000) >> 14,
  101. (ctx->u.FltSave.StatusWord & 0x00000400) >> 10,
  102. (ctx->u.FltSave.StatusWord & 0x00000200) >> 9,
  103. (ctx->u.FltSave.StatusWord & 0x00000100) >> 8);
  104. dbg_printf(" top:%01x", (unsigned int) (ctx->u.FltSave.StatusWord & 0x00003800) >> 11);
  105. if (ctx->u.FltSave.StatusWord & 0x00000001) /* Invalid Fl OP */
  106. {
  107. if (ctx->u.FltSave.StatusWord & 0x00000040) /* Stack Fault */
  108. {
  109. if (ctx->u.FltSave.StatusWord & 0x00000200) /* C1 says Overflow */
  110. dbg_printf(" #IE(Stack Overflow)");
  111. else
  112. dbg_printf(" #IE(Stack Underflow)"); /* Underflow */
  113. }
  114. else dbg_printf(" #IE(Arithmetic error)"); /* Invalid Fl OP */
  115. }
  116. if (ctx->u.FltSave.StatusWord & 0x00000002) dbg_printf(" #DE"); /* Denormalised OP */
  117. if (ctx->u.FltSave.StatusWord & 0x00000004) dbg_printf(" #ZE"); /* Zero Divide */
  118. if (ctx->u.FltSave.StatusWord & 0x00000008) dbg_printf(" #OE"); /* Overflow */
  119. if (ctx->u.FltSave.StatusWord & 0x00000010) dbg_printf(" #UE"); /* Underflow */
  120. if (ctx->u.FltSave.StatusWord & 0x00000020) dbg_printf(" #PE"); /* Precision error */
  121. if (ctx->u.FltSave.StatusWord & 0x00000040)
  122. if (!(ctx->u.FltSave.StatusWord & 0x00000001))
  123. dbg_printf(" #SE"); /* Stack Fault (don't think this can occur) */
  124. if (ctx->u.FltSave.StatusWord & 0x00000080) dbg_printf(" #ES"); /* Error Summary */
  125. if (ctx->u.FltSave.StatusWord & 0x00008000) dbg_printf(" #FB"); /* FPU Busy */
  126. dbg_printf(")\n");
  127. dbg_printf(" flerr:%04x:%08lx fldata:%04x:%08lx\n",
  128. ctx->u.FltSave.ErrorSelector, ctx->u.FltSave.ErrorOffset,
  129. ctx->u.FltSave.DataSelector, ctx->u.FltSave.DataOffset );
  130. for (i = 0; i < 8; i++)
  131. {
  132. M128A reg = ctx->u.FltSave.FloatRegisters[i];
  133. if (i == 4) dbg_printf("\n");
  134. dbg_printf(" ST%u:%016I64x%16I64x ", i, reg.High, reg.Low );
  135. }
  136. dbg_printf("\n");
  137. dbg_printf(" mxcsr: %04lx (", ctx->u.FltSave.MxCsr );
  138. for (i = 0; i < 16; i++)
  139. if (ctx->u.FltSave.MxCsr & (1 << i)) dbg_printf( " %s", mxcsr_flags[i] );
  140. dbg_printf(" )\n");
  141. for (i = 0; i < 16; i++)
  142. {
  143. dbg_printf( " %sxmm%u: uint=%016I64x%016I64x", (i > 9) ? "" : " ", i,
  144. ctx->u.FltSave.XmmRegisters[i].High, ctx->u.FltSave.XmmRegisters[i].Low );
  145. dbg_printf( " double={%g; %g}", *(double *)&ctx->u.FltSave.XmmRegisters[i].Low,
  146. *(double *)&ctx->u.FltSave.XmmRegisters[i].High );
  147. dbg_printf( " float={%g; %g; %g; %g}\n",
  148. (double)*((float *)&ctx->u.FltSave.XmmRegisters[i] + 0),
  149. (double)*((float *)&ctx->u.FltSave.XmmRegisters[i] + 1),
  150. (double)*((float *)&ctx->u.FltSave.XmmRegisters[i] + 2),
  151. (double)*((float *)&ctx->u.FltSave.XmmRegisters[i] + 3) );
  152. }
  153. }
  154. static void be_x86_64_print_segment_info(HANDLE hThread, const dbg_ctx_t *ctx)
  155. {
  156. }
  157. static struct dbg_internal_var be_x86_64_ctx[] =
  158. {
  159. {CV_AMD64_AL, "AL", (void*)FIELD_OFFSET(CONTEXT, Rax), dbg_itype_unsigned_char_int},
  160. {CV_AMD64_BL, "BL", (void*)FIELD_OFFSET(CONTEXT, Rbx), dbg_itype_unsigned_char_int},
  161. {CV_AMD64_CL, "CL", (void*)FIELD_OFFSET(CONTEXT, Rcx), dbg_itype_unsigned_char_int},
  162. {CV_AMD64_DL, "DL", (void*)FIELD_OFFSET(CONTEXT, Rdx), dbg_itype_unsigned_char_int},
  163. {CV_AMD64_AH, "AH", (void*)(FIELD_OFFSET(CONTEXT, Rax)+1), dbg_itype_unsigned_char_int},
  164. {CV_AMD64_BH, "BH", (void*)(FIELD_OFFSET(CONTEXT, Rbx)+1), dbg_itype_unsigned_char_int},
  165. {CV_AMD64_CH, "CH", (void*)(FIELD_OFFSET(CONTEXT, Rcx)+1), dbg_itype_unsigned_char_int},
  166. {CV_AMD64_DH, "DH", (void*)(FIELD_OFFSET(CONTEXT, Rdx)+1), dbg_itype_unsigned_char_int},
  167. {CV_AMD64_AX, "AX", (void*)FIELD_OFFSET(CONTEXT, Rax), dbg_itype_unsigned_short_int},
  168. {CV_AMD64_BX, "BX", (void*)FIELD_OFFSET(CONTEXT, Rbx), dbg_itype_unsigned_short_int},
  169. {CV_AMD64_CX, "CX", (void*)FIELD_OFFSET(CONTEXT, Rcx), dbg_itype_unsigned_short_int},
  170. {CV_AMD64_DX, "DX", (void*)FIELD_OFFSET(CONTEXT, Rdx), dbg_itype_unsigned_short_int},
  171. {CV_AMD64_SP, "SP", (void*)FIELD_OFFSET(CONTEXT, Rsp), dbg_itype_unsigned_short_int},
  172. {CV_AMD64_BP, "BP", (void*)FIELD_OFFSET(CONTEXT, Rbp), dbg_itype_unsigned_short_int},
  173. {CV_AMD64_SI, "SI", (void*)FIELD_OFFSET(CONTEXT, Rsi), dbg_itype_unsigned_short_int},
  174. {CV_AMD64_DI, "DI", (void*)FIELD_OFFSET(CONTEXT, Rdi), dbg_itype_unsigned_short_int},
  175. {CV_AMD64_EAX, "EAX", (void*)FIELD_OFFSET(CONTEXT, Rax), dbg_itype_unsigned_int},
  176. {CV_AMD64_EBX, "EBX", (void*)FIELD_OFFSET(CONTEXT, Rbx), dbg_itype_unsigned_int},
  177. {CV_AMD64_ECX, "ECX", (void*)FIELD_OFFSET(CONTEXT, Rcx), dbg_itype_unsigned_int},
  178. {CV_AMD64_EDX, "EDX", (void*)FIELD_OFFSET(CONTEXT, Rdx), dbg_itype_unsigned_int},
  179. {CV_AMD64_ESP, "ESP", (void*)FIELD_OFFSET(CONTEXT, Rsp), dbg_itype_unsigned_int},
  180. {CV_AMD64_EBP, "EBP", (void*)FIELD_OFFSET(CONTEXT, Rbp), dbg_itype_unsigned_int},
  181. {CV_AMD64_ESI, "ESI", (void*)FIELD_OFFSET(CONTEXT, Rsi), dbg_itype_unsigned_int},
  182. {CV_AMD64_EDI, "EDI", (void*)FIELD_OFFSET(CONTEXT, Rdi), dbg_itype_unsigned_int},
  183. {CV_AMD64_ES, "ES", (void*)FIELD_OFFSET(CONTEXT, SegEs), dbg_itype_unsigned_short_int},
  184. {CV_AMD64_CS, "CS", (void*)FIELD_OFFSET(CONTEXT, SegCs), dbg_itype_unsigned_short_int},
  185. {CV_AMD64_SS, "SS", (void*)FIELD_OFFSET(CONTEXT, SegSs), dbg_itype_unsigned_short_int},
  186. {CV_AMD64_DS, "DS", (void*)FIELD_OFFSET(CONTEXT, SegDs), dbg_itype_unsigned_short_int},
  187. {CV_AMD64_FS, "FS", (void*)FIELD_OFFSET(CONTEXT, SegFs), dbg_itype_unsigned_short_int},
  188. {CV_AMD64_GS, "GS", (void*)FIELD_OFFSET(CONTEXT, SegGs), dbg_itype_unsigned_short_int},
  189. {CV_AMD64_FLAGS, "FLAGS", (void*)FIELD_OFFSET(CONTEXT, EFlags), dbg_itype_unsigned_short_int},
  190. {CV_AMD64_EFLAGS, "EFLAGS", (void*)FIELD_OFFSET(CONTEXT, EFlags), dbg_itype_unsigned_int},
  191. {CV_AMD64_RIP, "RIP", (void*)FIELD_OFFSET(CONTEXT, Rip), dbg_itype_unsigned_long_int},
  192. {CV_AMD64_RAX, "RAX", (void*)FIELD_OFFSET(CONTEXT, Rax), dbg_itype_unsigned_long_int},
  193. {CV_AMD64_RBX, "RBX", (void*)FIELD_OFFSET(CONTEXT, Rbx), dbg_itype_unsigned_long_int},
  194. {CV_AMD64_RCX, "RCX", (void*)FIELD_OFFSET(CONTEXT, Rcx), dbg_itype_unsigned_long_int},
  195. {CV_AMD64_RDX, "RDX", (void*)FIELD_OFFSET(CONTEXT, Rdx), dbg_itype_unsigned_long_int},
  196. {CV_AMD64_RSP, "RSP", (void*)FIELD_OFFSET(CONTEXT, Rsp), dbg_itype_unsigned_long_int},
  197. {CV_AMD64_RBP, "RBP", (void*)FIELD_OFFSET(CONTEXT, Rbp), dbg_itype_unsigned_long_int},
  198. {CV_AMD64_RSI, "RSI", (void*)FIELD_OFFSET(CONTEXT, Rsi), dbg_itype_unsigned_long_int},
  199. {CV_AMD64_RDI, "RDI", (void*)FIELD_OFFSET(CONTEXT, Rdi), dbg_itype_unsigned_long_int},
  200. {CV_AMD64_R8, "R8", (void*)FIELD_OFFSET(CONTEXT, R8), dbg_itype_unsigned_long_int},
  201. {CV_AMD64_R9, "R9", (void*)FIELD_OFFSET(CONTEXT, R9), dbg_itype_unsigned_long_int},
  202. {CV_AMD64_R10, "R10", (void*)FIELD_OFFSET(CONTEXT, R10), dbg_itype_unsigned_long_int},
  203. {CV_AMD64_R11, "R11", (void*)FIELD_OFFSET(CONTEXT, R11), dbg_itype_unsigned_long_int},
  204. {CV_AMD64_R12, "R12", (void*)FIELD_OFFSET(CONTEXT, R12), dbg_itype_unsigned_long_int},
  205. {CV_AMD64_R13, "R13", (void*)FIELD_OFFSET(CONTEXT, R13), dbg_itype_unsigned_long_int},
  206. {CV_AMD64_R14, "R14", (void*)FIELD_OFFSET(CONTEXT, R14), dbg_itype_unsigned_long_int},
  207. {CV_AMD64_R15, "R15", (void*)FIELD_OFFSET(CONTEXT, R15), dbg_itype_unsigned_long_int},
  208. {CV_AMD64_ST0, "ST0", (void*)FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[0]), dbg_itype_long_real},
  209. {CV_AMD64_ST0+1, "ST1", (void*)FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[1]), dbg_itype_long_real},
  210. {CV_AMD64_ST0+2, "ST2", (void*)FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[2]), dbg_itype_long_real},
  211. {CV_AMD64_ST0+3, "ST3", (void*)FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[3]), dbg_itype_long_real},
  212. {CV_AMD64_ST0+4, "ST4", (void*)FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[4]), dbg_itype_long_real},
  213. {CV_AMD64_ST0+5, "ST5", (void*)FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[5]), dbg_itype_long_real},
  214. {CV_AMD64_ST0+6, "ST6", (void*)FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[6]), dbg_itype_long_real},
  215. {CV_AMD64_ST0+7, "ST7", (void*)FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[7]), dbg_itype_long_real},
  216. {CV_AMD64_XMM0, "XMM0", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm0), dbg_itype_m128a},
  217. {CV_AMD64_XMM0+1, "XMM1", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm1), dbg_itype_m128a},
  218. {CV_AMD64_XMM0+2, "XMM2", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm2), dbg_itype_m128a},
  219. {CV_AMD64_XMM0+3, "XMM3", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm3), dbg_itype_m128a},
  220. {CV_AMD64_XMM0+4, "XMM4", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm4), dbg_itype_m128a},
  221. {CV_AMD64_XMM0+5, "XMM5", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm5), dbg_itype_m128a},
  222. {CV_AMD64_XMM0+6, "XMM6", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm6), dbg_itype_m128a},
  223. {CV_AMD64_XMM0+7, "XMM7", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm7), dbg_itype_m128a},
  224. {CV_AMD64_XMM8, "XMM8", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm8), dbg_itype_m128a},
  225. {CV_AMD64_XMM8+1, "XMM9", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm9), dbg_itype_m128a},
  226. {CV_AMD64_XMM8+2, "XMM10", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm10), dbg_itype_m128a},
  227. {CV_AMD64_XMM8+3, "XMM11", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm11), dbg_itype_m128a},
  228. {CV_AMD64_XMM8+4, "XMM12", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm12), dbg_itype_m128a},
  229. {CV_AMD64_XMM8+5, "XMM13", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm13), dbg_itype_m128a},
  230. {CV_AMD64_XMM8+6, "XMM14", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm14), dbg_itype_m128a},
  231. {CV_AMD64_XMM8+7, "XMM15", (void*)FIELD_OFFSET(CONTEXT, u.s.Xmm15), dbg_itype_m128a},
  232. {0, NULL, 0, dbg_itype_none}
  233. };
  234. #define f_mod(b) ((b)>>6)
  235. #define f_reg(b) (((b)>>3)&0x7)
  236. #define f_rm(b) ((b)&0x7)
  237. #define f_sib_b(b) ((b)&0x7)
  238. #define f_sib_i(b) (((b)>>3)&0x7)
  239. #define f_sib_s(b) ((b)>>6)
  240. static BOOL be_x86_64_is_step_over_insn(const void* insn)
  241. {
  242. BYTE ch;
  243. for (;;)
  244. {
  245. if (!dbg_read_memory(insn, &ch, sizeof(ch))) return FALSE;
  246. switch (ch)
  247. {
  248. /* Skip all prefixes */
  249. case 0x2e: /* cs: */
  250. case 0x36: /* ss: */
  251. case 0x3e: /* ds: */
  252. case 0x26: /* es: */
  253. case 0x64: /* fs: */
  254. case 0x65: /* gs: */
  255. case 0x66: /* opcode size prefix */
  256. case 0x67: /* addr size prefix */
  257. case 0xf0: /* lock */
  258. case 0xf2: /* repne */
  259. case 0xf3: /* repe */
  260. insn = (const char*)insn + 1;
  261. continue;
  262. /* Handle call instructions */
  263. case 0xcd: /* int <intno> */
  264. case 0xe8: /* call <offset> */
  265. case 0x9a: /* lcall <seg>:<off> */
  266. return TRUE;
  267. case 0xff: /* call <regmodrm> */
  268. if (!dbg_read_memory((const char*)insn + 1, &ch, sizeof(ch)))
  269. return FALSE;
  270. return (((ch & 0x38) == 0x10) || ((ch & 0x38) == 0x18));
  271. /* Handle string instructions */
  272. case 0x6c: /* insb */
  273. case 0x6d: /* insw */
  274. case 0x6e: /* outsb */
  275. case 0x6f: /* outsw */
  276. case 0xa4: /* movsb */
  277. case 0xa5: /* movsw */
  278. case 0xa6: /* cmpsb */
  279. case 0xa7: /* cmpsw */
  280. case 0xaa: /* stosb */
  281. case 0xab: /* stosw */
  282. case 0xac: /* lodsb */
  283. case 0xad: /* lodsw */
  284. case 0xae: /* scasb */
  285. case 0xaf: /* scasw */
  286. return TRUE;
  287. default:
  288. return FALSE;
  289. }
  290. }
  291. }
  292. static BOOL be_x86_64_is_function_return(const void* insn)
  293. {
  294. BYTE c;
  295. /* sigh... amd64 for prefetch optimization requires 'rep ret' in some cases */
  296. if (!dbg_read_memory(insn, &c, sizeof(c))) return FALSE;
  297. if (c == 0xF3) /* REP */
  298. {
  299. insn = (const char*)insn + 1;
  300. if (!dbg_read_memory(insn, &c, sizeof(c))) return FALSE;
  301. }
  302. return c == 0xC2 /* ret */ || c == 0xC3 /* ret NN */;
  303. }
  304. static BOOL be_x86_64_is_break_insn(const void* insn)
  305. {
  306. BYTE c;
  307. return dbg_read_memory(insn, &c, sizeof(c)) && c == 0xCC;
  308. }
  309. static BOOL fetch_value(const char* addr, unsigned sz, LONG* value)
  310. {
  311. char value8;
  312. short value16;
  313. switch (sz)
  314. {
  315. case 1:
  316. if (!dbg_read_memory(addr, &value8, sizeof(value8))) return FALSE;
  317. *value = value8;
  318. break;
  319. case 2:
  320. if (!dbg_read_memory(addr, &value16, sizeof(value16))) return FALSE;
  321. *value = value16;
  322. case 4:
  323. if (!dbg_read_memory(addr, value, sizeof(*value))) return FALSE;
  324. break;
  325. default: return FALSE;
  326. }
  327. return TRUE;
  328. }
  329. static BOOL add_fixed_displacement(const void* insn, BYTE mod, DWORD64* addr)
  330. {
  331. LONG delta = 0;
  332. if (mod == 1)
  333. {
  334. if (!fetch_value(insn, 1, &delta))
  335. return FALSE;
  336. }
  337. else if (mod == 2)
  338. {
  339. if (!fetch_value(insn, sizeof(delta), &delta))
  340. return FALSE;
  341. }
  342. *addr += delta;
  343. return TRUE;
  344. }
  345. static BOOL evaluate_sib_address(const void* insn, BYTE mod, DWORD64* addr)
  346. {
  347. BYTE ch;
  348. BYTE scale;
  349. DWORD64 loc;
  350. if (!dbg_read_memory(insn, &ch, sizeof(ch))) return FALSE;
  351. switch (f_sib_b(ch))
  352. {
  353. case 0x00: loc = dbg_context.ctx.Rax; break;
  354. case 0x01: loc = dbg_context.ctx.Rcx; break;
  355. case 0x02: loc = dbg_context.ctx.Rdx; break;
  356. case 0x03: loc = dbg_context.ctx.Rbx; break;
  357. case 0x04: loc = dbg_context.ctx.Rsp; break;
  358. case 0x05:
  359. loc = dbg_context.ctx.Rbp;
  360. if (mod == 0)
  361. {
  362. loc = 0;
  363. mod = 2;
  364. }
  365. break;
  366. case 0x06: loc = dbg_context.ctx.Rsi; break;
  367. case 0x07: loc = dbg_context.ctx.Rdi; break;
  368. }
  369. scale = f_sib_s(ch);
  370. switch (f_sib_i(ch))
  371. {
  372. case 0x00: loc += dbg_context.ctx.Rax << scale; break;
  373. case 0x01: loc += dbg_context.ctx.Rcx << scale; break;
  374. case 0x02: loc += dbg_context.ctx.Rdx << scale; break;
  375. case 0x03: loc += dbg_context.ctx.Rbx << scale; break;
  376. case 0x04: break;
  377. case 0x05: loc += dbg_context.ctx.Rbp << scale; break;
  378. case 0x06: loc += dbg_context.ctx.Rsi << scale; break;
  379. case 0x07: loc += dbg_context.ctx.Rdi << scale; break;
  380. }
  381. if (!add_fixed_displacement((const char*)insn + 1, mod, &loc))
  382. return FALSE;
  383. *addr = loc;
  384. return TRUE;
  385. }
  386. static BOOL load_indirect_target(DWORD64* dst)
  387. {
  388. ADDRESS64 addr;
  389. addr.Mode = AddrModeFlat;
  390. addr.Segment = dbg_context.ctx.SegDs;
  391. addr.Offset = *dst;
  392. return dbg_read_memory(memory_to_linear_addr(&addr), &dst, sizeof(dst));
  393. }
  394. static BOOL be_x86_64_is_func_call(const void* insn, ADDRESS64* callee)
  395. {
  396. BYTE ch;
  397. LONG delta;
  398. unsigned op_size = 32, rex = 0;
  399. DWORD64 dst;
  400. /* we assume 64bit mode all over the place */
  401. for (;;)
  402. {
  403. if (!dbg_read_memory(insn, &ch, sizeof(ch))) return FALSE;
  404. if (ch == 0x66) op_size = 16;
  405. else if (ch == 0x67) WINE_FIXME("prefix not supported %x\n", ch);
  406. else if (ch >= 0x40 && ch <= 0x4f) rex = ch & 0xf;
  407. else break;
  408. insn = (const char*)insn + 1;
  409. } while (0);
  410. /* that's the only mode we support anyway */
  411. callee->Mode = AddrModeFlat;
  412. callee->Segment = dbg_context.ctx.SegCs;
  413. switch (ch)
  414. {
  415. case 0xe8: /* relative near call */
  416. assert(op_size == 32);
  417. if (!fetch_value((const char*)insn + 1, sizeof(delta), &delta))
  418. return FALSE;
  419. callee->Offset = (DWORD_PTR)insn + 1 + 4 + delta;
  420. return TRUE;
  421. case 0xff:
  422. if (!dbg_read_memory((const char*)insn + 1, &ch, sizeof(ch)))
  423. return FALSE;
  424. WINE_TRACE("Got 0xFF %x (&C7=%x) with rex=%x\n", ch, ch & 0xC7, rex);
  425. /* keep only the CALL and LCALL insn:s */
  426. switch (f_reg(ch))
  427. {
  428. case 0x02:
  429. break;
  430. default: return FALSE;
  431. }
  432. if (rex == 0) switch (ch & 0xC7) /* keep Mod R/M only (skip reg) */
  433. {
  434. case 0x04:
  435. case 0x44:
  436. case 0x84:
  437. {
  438. evaluate_sib_address((const char*)insn + 2, f_mod(ch), &dst);
  439. if (!load_indirect_target(&dst)) return FALSE;
  440. callee->Offset = dst;
  441. return TRUE;
  442. }
  443. case 0x05: /* addr32 */
  444. if (f_reg(ch) == 0x2)
  445. {
  446. /* rip-relative to next insn */
  447. if (!dbg_read_memory((const char*)insn + 2, &delta, sizeof(delta)) ||
  448. !dbg_read_memory((const char*)insn + 6 + delta, &dst, sizeof(dst)))
  449. return FALSE;
  450. callee->Offset = dst;
  451. return TRUE;
  452. }
  453. WINE_FIXME("Unsupported yet call insn (0xFF 0x%02x) at %p\n", ch, insn);
  454. return FALSE;
  455. default:
  456. switch (f_rm(ch))
  457. {
  458. case 0x00: dst = dbg_context.ctx.Rax; break;
  459. case 0x01: dst = dbg_context.ctx.Rcx; break;
  460. case 0x02: dst = dbg_context.ctx.Rdx; break;
  461. case 0x03: dst = dbg_context.ctx.Rbx; break;
  462. case 0x04: dst = dbg_context.ctx.Rsp; break;
  463. case 0x05: dst = dbg_context.ctx.Rbp; break;
  464. case 0x06: dst = dbg_context.ctx.Rsi; break;
  465. case 0x07: dst = dbg_context.ctx.Rdi; break;
  466. }
  467. if (f_mod(ch) != 0x03)
  468. {
  469. if (!add_fixed_displacement((const char*)insn + 2, f_mod(ch), &dst))
  470. return FALSE;
  471. if (!load_indirect_target(&dst)) return FALSE;
  472. }
  473. callee->Offset = dst;
  474. return TRUE;
  475. }
  476. else
  477. WINE_FIXME("Unsupported yet call insn (rex=0x%02x 0xFF 0x%02x) at %p\n", rex, ch, insn);
  478. return FALSE;
  479. default:
  480. return FALSE;
  481. }
  482. }
  483. static BOOL be_x86_64_is_jump(const void* insn, ADDRESS64* jumpee)
  484. {
  485. return FALSE;
  486. }
  487. extern void be_x86_64_disasm_one_insn(ADDRESS64* addr, int display);
  488. #define DR7_CONTROL_SHIFT 16
  489. #define DR7_CONTROL_SIZE 4
  490. #define DR7_RW_EXECUTE (0x0)
  491. #define DR7_RW_WRITE (0x1)
  492. #define DR7_RW_READ (0x3)
  493. #define DR7_LEN_1 (0x0)
  494. #define DR7_LEN_2 (0x4)
  495. #define DR7_LEN_4 (0xC)
  496. #define DR7_LEN_8 (0x8)
  497. #define DR7_LOCAL_ENABLE_SHIFT 0
  498. #define DR7_GLOBAL_ENABLE_SHIFT 1
  499. #define DR7_ENABLE_SIZE 2
  500. #define DR7_LOCAL_ENABLE_MASK (0x55)
  501. #define DR7_GLOBAL_ENABLE_MASK (0xAA)
  502. #define DR7_CONTROL_RESERVED (0xFC00)
  503. #define DR7_LOCAL_SLOWDOWN (0x100)
  504. #define DR7_GLOBAL_SLOWDOWN (0x200)
  505. #define DR7_ENABLE_MASK(dr) (1<<(DR7_LOCAL_ENABLE_SHIFT+DR7_ENABLE_SIZE*(dr)))
  506. #define IS_DR7_SET(ctrl,dr) ((ctrl)&DR7_ENABLE_MASK(dr))
  507. static inline int be_x86_64_get_unused_DR(dbg_ctx_t *pctx, DWORD64** r)
  508. {
  509. CONTEXT *ctx = &pctx->ctx;
  510. if (!IS_DR7_SET(ctx->Dr7, 0))
  511. {
  512. *r = &ctx->Dr0;
  513. return 0;
  514. }
  515. if (!IS_DR7_SET(ctx->Dr7, 1))
  516. {
  517. *r = &ctx->Dr1;
  518. return 1;
  519. }
  520. if (!IS_DR7_SET(ctx->Dr7, 2))
  521. {
  522. *r = &ctx->Dr2;
  523. return 2;
  524. }
  525. if (!IS_DR7_SET(ctx->Dr7, 3))
  526. {
  527. *r = &ctx->Dr3;
  528. return 3;
  529. }
  530. dbg_printf("All hardware registers have been used\n");
  531. return -1;
  532. }
  533. static BOOL be_x86_64_insert_Xpoint(HANDLE hProcess, const struct be_process_io* pio,
  534. dbg_ctx_t *ctx, enum be_xpoint_type type,
  535. void* addr, unsigned *val, unsigned size)
  536. {
  537. unsigned char ch;
  538. SIZE_T sz;
  539. DWORD64 *pr;
  540. int reg;
  541. unsigned int bits;
  542. switch (type)
  543. {
  544. case be_xpoint_break:
  545. if (size != 0) return FALSE;
  546. if (!pio->read(hProcess, addr, &ch, 1, &sz) || sz != 1) return FALSE;
  547. *val = ch;
  548. ch = 0xcc;
  549. if (!pio->write(hProcess, addr, &ch, 1, &sz) || sz != 1) return FALSE;
  550. break;
  551. case be_xpoint_watch_exec:
  552. bits = DR7_RW_EXECUTE;
  553. goto hw_bp;
  554. case be_xpoint_watch_read:
  555. bits = DR7_RW_READ;
  556. goto hw_bp;
  557. case be_xpoint_watch_write:
  558. bits = DR7_RW_WRITE;
  559. hw_bp:
  560. if ((reg = be_x86_64_get_unused_DR(ctx, &pr)) == -1) return FALSE;
  561. *pr = (DWORD64)addr;
  562. if (type != be_xpoint_watch_exec) switch (size)
  563. {
  564. case 8: bits |= DR7_LEN_8; break;
  565. case 4: bits |= DR7_LEN_4; break;
  566. case 2: bits |= DR7_LEN_2; break;
  567. case 1: bits |= DR7_LEN_1; break;
  568. default: WINE_FIXME("Unsupported xpoint_watch of size %d\n", size); return FALSE;
  569. }
  570. *val = reg;
  571. /* clear old values */
  572. ctx->ctx.Dr7 &= ~(0x0F << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg));
  573. /* set the correct ones */
  574. ctx->ctx.Dr7 |= bits << (DR7_CONTROL_SHIFT + DR7_CONTROL_SIZE * reg);
  575. ctx->ctx.Dr7 |= DR7_ENABLE_MASK(reg) | DR7_LOCAL_SLOWDOWN;
  576. break;
  577. default:
  578. dbg_printf("Unknown bp type %c\n", type);
  579. return FALSE;
  580. }
  581. return TRUE;
  582. }
  583. static BOOL be_x86_64_remove_Xpoint(HANDLE hProcess, const struct be_process_io* pio,
  584. dbg_ctx_t *ctx, enum be_xpoint_type type,
  585. void* addr, unsigned val, unsigned size)
  586. {
  587. SIZE_T sz;
  588. unsigned char ch;
  589. switch (type)
  590. {
  591. case be_xpoint_break:
  592. if (size != 0) return FALSE;
  593. if (!pio->read(hProcess, addr, &ch, 1, &sz) || sz != 1) return FALSE;
  594. if (ch != (unsigned char)0xCC)
  595. WINE_FIXME("Cannot get back %02x instead of 0xCC at %p\n", ch, addr);
  596. ch = (unsigned char)val;
  597. if (!pio->write(hProcess, addr, &ch, 1, &sz) || sz != 1) return FALSE;
  598. break;
  599. case be_xpoint_watch_exec:
  600. case be_xpoint_watch_read:
  601. case be_xpoint_watch_write:
  602. /* simply disable the entry */
  603. ctx->ctx.Dr7 &= ~DR7_ENABLE_MASK(val);
  604. break;
  605. default:
  606. dbg_printf("Unknown bp type %c\n", type);
  607. return FALSE;
  608. }
  609. return TRUE;
  610. }
  611. static BOOL be_x86_64_is_watchpoint_set(const dbg_ctx_t *ctx, unsigned idx)
  612. {
  613. return ctx->ctx.Dr6 & (1 << idx);
  614. }
  615. static void be_x86_64_clear_watchpoint(dbg_ctx_t *ctx, unsigned idx)
  616. {
  617. ctx->ctx.Dr6 &= ~(1 << idx);
  618. }
  619. static int be_x86_64_adjust_pc_for_break(dbg_ctx_t *ctx, BOOL way)
  620. {
  621. if (way)
  622. {
  623. ctx->ctx.Rip--;
  624. return -1;
  625. }
  626. ctx->ctx.Rip++;
  627. return 1;
  628. }
  629. static BOOL be_x86_64_get_context(HANDLE thread, dbg_ctx_t *ctx)
  630. {
  631. ctx->ctx.ContextFlags = CONTEXT_ALL;
  632. return GetThreadContext(thread, &ctx->ctx);
  633. }
  634. static BOOL be_x86_64_set_context(HANDLE thread, const dbg_ctx_t *ctx)
  635. {
  636. return SetThreadContext(thread, &ctx->ctx);
  637. }
  638. #define REG(f,n,t,r) {f, n, t, FIELD_OFFSET(CONTEXT, r), sizeof(((CONTEXT*)NULL)->r)}
  639. static struct gdb_register be_x86_64_gdb_register_map[] = {
  640. REG("core", "rax", NULL, Rax),
  641. REG(NULL, "rbx", NULL, Rbx),
  642. REG(NULL, "rcx", NULL, Rcx),
  643. REG(NULL, "rdx", NULL, Rdx),
  644. REG(NULL, "rsi", NULL, Rsi),
  645. REG(NULL, "rdi", NULL, Rdi),
  646. REG(NULL, "rbp", "data_ptr", Rbp),
  647. REG(NULL, "rsp", "data_ptr", Rsp),
  648. REG(NULL, "r8", NULL, R8),
  649. REG(NULL, "r9", NULL, R9),
  650. REG(NULL, "r10", NULL, R10),
  651. REG(NULL, "r11", NULL, R11),
  652. REG(NULL, "r12", NULL, R12),
  653. REG(NULL, "r13", NULL, R13),
  654. REG(NULL, "r14", NULL, R14),
  655. REG(NULL, "r15", NULL, R15),
  656. REG(NULL, "rip", "code_ptr", Rip),
  657. REG(NULL, "eflags", "i386_eflags", EFlags),
  658. REG(NULL, "cs", NULL, SegCs),
  659. REG(NULL, "ss", NULL, SegSs),
  660. REG(NULL, "ds", NULL, SegDs),
  661. REG(NULL, "es", NULL, SegEs),
  662. REG(NULL, "fs", NULL, SegFs),
  663. REG(NULL, "gs", NULL, SegGs),
  664. { NULL, "st0", "i387_ext", FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[ 0]), 10},
  665. { NULL, "st1", "i387_ext", FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[ 1]), 10},
  666. { NULL, "st2", "i387_ext", FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[ 2]), 10},
  667. { NULL, "st3", "i387_ext", FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[ 3]), 10},
  668. { NULL, "st4", "i387_ext", FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[ 4]), 10},
  669. { NULL, "st5", "i387_ext", FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[ 5]), 10},
  670. { NULL, "st6", "i387_ext", FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[ 6]), 10},
  671. { NULL, "st7", "i387_ext", FIELD_OFFSET(CONTEXT, u.FltSave.FloatRegisters[ 7]), 10},
  672. REG(NULL, "fctrl", NULL, u.FltSave.ControlWord),
  673. REG(NULL, "fstat", NULL, u.FltSave.StatusWord),
  674. REG(NULL, "ftag", NULL, u.FltSave.TagWord),
  675. REG(NULL, "fiseg", NULL, u.FltSave.ErrorSelector),
  676. REG(NULL, "fioff", NULL, u.FltSave.ErrorOffset),
  677. REG(NULL, "foseg", NULL, u.FltSave.DataSelector),
  678. REG(NULL, "fooff", NULL, u.FltSave.DataOffset),
  679. REG(NULL, "fop", NULL, u.FltSave.ErrorOpcode),
  680. REG("sse", "xmm0", "vec128", u.s.Xmm0),
  681. REG(NULL, "xmm1", "vec128", u.s.Xmm1),
  682. REG(NULL, "xmm2", "vec128", u.s.Xmm2),
  683. REG(NULL, "xmm3", "vec128", u.s.Xmm3),
  684. REG(NULL, "xmm4", "vec128", u.s.Xmm4),
  685. REG(NULL, "xmm5", "vec128", u.s.Xmm5),
  686. REG(NULL, "xmm6", "vec128", u.s.Xmm6),
  687. REG(NULL, "xmm7", "vec128", u.s.Xmm7),
  688. REG(NULL, "xmm8", "vec128", u.s.Xmm8),
  689. REG(NULL, "xmm9", "vec128", u.s.Xmm9),
  690. REG(NULL, "xmm10", "vec128", u.s.Xmm10),
  691. REG(NULL, "xmm11", "vec128", u.s.Xmm11),
  692. REG(NULL, "xmm12", "vec128", u.s.Xmm12),
  693. REG(NULL, "xmm13", "vec128", u.s.Xmm13),
  694. REG(NULL, "xmm14", "vec128", u.s.Xmm14),
  695. REG(NULL, "xmm15", "vec128", u.s.Xmm15),
  696. REG(NULL, "mxcsr", "i386_mxcsr", u.FltSave.MxCsr),
  697. };
  698. struct backend_cpu be_x86_64 =
  699. {
  700. IMAGE_FILE_MACHINE_AMD64,
  701. 8,
  702. be_cpu_linearize,
  703. be_cpu_build_addr,
  704. be_x86_64_get_addr,
  705. be_x86_64_get_register_info,
  706. be_x86_64_single_step,
  707. be_x86_64_print_context,
  708. be_x86_64_print_segment_info,
  709. be_x86_64_ctx,
  710. be_x86_64_is_step_over_insn,
  711. be_x86_64_is_function_return,
  712. be_x86_64_is_break_insn,
  713. be_x86_64_is_func_call,
  714. be_x86_64_is_jump,
  715. be_x86_64_disasm_one_insn,
  716. be_x86_64_insert_Xpoint,
  717. be_x86_64_remove_Xpoint,
  718. be_x86_64_is_watchpoint_set,
  719. be_x86_64_clear_watchpoint,
  720. be_x86_64_adjust_pc_for_break,
  721. be_x86_64_get_context,
  722. be_x86_64_set_context,
  723. be_x86_64_gdb_register_map,
  724. ARRAY_SIZE(be_x86_64_gdb_register_map),
  725. };
  726. #endif