be_arm64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Debugger ARM64 specific functions
  3. *
  4. * Copyright 2010-2013 André Hentschel
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "debugger.h"
  21. #if defined(__aarch64__) && !defined(__AARCH64EB__)
  22. static BOOL be_arm64_get_addr(HANDLE hThread, const dbg_ctx_t *ctx,
  23. enum be_cpu_addr bca, ADDRESS64* addr)
  24. {
  25. switch (bca)
  26. {
  27. case be_cpu_addr_pc:
  28. return be_cpu_build_addr(hThread, ctx, addr, 0, ctx->ctx.Pc);
  29. case be_cpu_addr_stack:
  30. return be_cpu_build_addr(hThread, ctx, addr, 0, ctx->ctx.Sp);
  31. case be_cpu_addr_frame:
  32. return be_cpu_build_addr(hThread, ctx, addr, 0, ctx->ctx.u.s.Fp);
  33. break;
  34. }
  35. return FALSE;
  36. }
  37. static BOOL be_arm64_get_register_info(int regno, enum be_cpu_addr* kind)
  38. {
  39. switch (regno)
  40. {
  41. case CV_ARM64_PC: *kind = be_cpu_addr_pc; return TRUE;
  42. case CV_ARM64_SP: *kind = be_cpu_addr_stack; return TRUE;
  43. case CV_ARM64_FP: *kind = be_cpu_addr_frame; return TRUE;
  44. }
  45. return FALSE;
  46. }
  47. static void be_arm64_single_step(dbg_ctx_t *ctx, BOOL enable)
  48. {
  49. dbg_printf("be_arm64_single_step: not done\n");
  50. }
  51. static void be_arm64_print_context(HANDLE hThread, const dbg_ctx_t *ctx, int all_regs)
  52. {
  53. static const char condflags[] = "NZCV";
  54. int i;
  55. char buf[8];
  56. switch (ctx->ctx.Cpsr & 0x0f)
  57. {
  58. case 0: strcpy(buf, "EL0t"); break;
  59. case 4: strcpy(buf, "EL1t"); break;
  60. case 5: strcpy(buf, "EL1t"); break;
  61. case 8: strcpy(buf, "EL2t"); break;
  62. case 9: strcpy(buf, "EL2t"); break;
  63. case 12: strcpy(buf, "EL3t"); break;
  64. case 13: strcpy(buf, "EL3t"); break;
  65. default: strcpy(buf, "UNKNWN"); break;
  66. }
  67. dbg_printf("Register dump:\n");
  68. dbg_printf("%s %s Mode\n", (ctx->ctx.Cpsr & 0x10) ? "ARM" : "ARM64", buf);
  69. strcpy(buf, condflags);
  70. for (i = 0; buf[i]; i++)
  71. if (!((ctx->ctx.Cpsr >> 26) & (1 << (sizeof(condflags) - i))))
  72. buf[i] = '-';
  73. dbg_printf(" Pc:%016I64x Sp:%016I64x Lr:%016I64x Cpsr:%08x(%s)\n",
  74. ctx->ctx.Pc, ctx->ctx.Sp, ctx->ctx.u.s.Lr, ctx->ctx.Cpsr, buf);
  75. dbg_printf(" x0: %016I64x x1: %016I64x x2: %016I64x x3: %016I64x x4: %016I64x\n",
  76. ctx->ctx.u.s.X0, ctx->ctx.u.s.X1, ctx->ctx.u.s.X2, ctx->ctx.u.s.X3, ctx->ctx.u.s.X4);
  77. dbg_printf(" x5: %016I64x x6: %016I64x x7: %016I64x x8: %016I64x x9: %016I64x\n",
  78. ctx->ctx.u.s.X5, ctx->ctx.u.s.X6, ctx->ctx.u.s.X7, ctx->ctx.u.s.X8, ctx->ctx.u.s.X9);
  79. dbg_printf(" x10:%016I64x x11:%016I64x x12:%016I64x x13:%016I64x x14:%016I64x\n",
  80. ctx->ctx.u.s.X10, ctx->ctx.u.s.X11, ctx->ctx.u.s.X12, ctx->ctx.u.s.X13, ctx->ctx.u.s.X14);
  81. dbg_printf(" x15:%016I64x ip0:%016I64x ip1:%016I64x x18:%016I64x x19:%016I64x\n",
  82. ctx->ctx.u.s.X15, ctx->ctx.u.s.X16, ctx->ctx.u.s.X17, ctx->ctx.u.s.X18, ctx->ctx.u.s.X19);
  83. dbg_printf(" x20:%016I64x x21:%016I64x x22:%016I64x x23:%016I64x x24:%016I64x\n",
  84. ctx->ctx.u.s.X20, ctx->ctx.u.s.X21, ctx->ctx.u.s.X22, ctx->ctx.u.s.X23, ctx->ctx.u.s.X24);
  85. dbg_printf(" x25:%016I64x x26:%016I64x x27:%016I64x x28:%016I64x Fp:%016I64x\n",
  86. ctx->ctx.u.s.X25, ctx->ctx.u.s.X26, ctx->ctx.u.s.X27, ctx->ctx.u.s.X28, ctx->ctx.u.s.Fp);
  87. if (all_regs) dbg_printf( "Floating point ARM64 dump not implemented\n" );
  88. }
  89. static void be_arm64_print_segment_info(HANDLE hThread, const dbg_ctx_t *ctx)
  90. {
  91. }
  92. static struct dbg_internal_var be_arm64_ctx[] =
  93. {
  94. {CV_ARM64_PSTATE, "cpsr", (void*)FIELD_OFFSET(CONTEXT, Cpsr), dbg_itype_unsigned_int},
  95. {CV_ARM64_X0 + 0, "x0", (void*)FIELD_OFFSET(CONTEXT, u.s.X0), dbg_itype_unsigned_long_int},
  96. {CV_ARM64_X0 + 1, "x1", (void*)FIELD_OFFSET(CONTEXT, u.s.X1), dbg_itype_unsigned_long_int},
  97. {CV_ARM64_X0 + 2, "x2", (void*)FIELD_OFFSET(CONTEXT, u.s.X2), dbg_itype_unsigned_long_int},
  98. {CV_ARM64_X0 + 3, "x3", (void*)FIELD_OFFSET(CONTEXT, u.s.X3), dbg_itype_unsigned_long_int},
  99. {CV_ARM64_X0 + 4, "x4", (void*)FIELD_OFFSET(CONTEXT, u.s.X4), dbg_itype_unsigned_long_int},
  100. {CV_ARM64_X0 + 5, "x5", (void*)FIELD_OFFSET(CONTEXT, u.s.X5), dbg_itype_unsigned_long_int},
  101. {CV_ARM64_X0 + 6, "x6", (void*)FIELD_OFFSET(CONTEXT, u.s.X6), dbg_itype_unsigned_long_int},
  102. {CV_ARM64_X0 + 7, "x7", (void*)FIELD_OFFSET(CONTEXT, u.s.X7), dbg_itype_unsigned_long_int},
  103. {CV_ARM64_X0 + 8, "x8", (void*)FIELD_OFFSET(CONTEXT, u.s.X8), dbg_itype_unsigned_long_int},
  104. {CV_ARM64_X0 + 9, "x9", (void*)FIELD_OFFSET(CONTEXT, u.s.X9), dbg_itype_unsigned_long_int},
  105. {CV_ARM64_X0 + 10, "x10", (void*)FIELD_OFFSET(CONTEXT, u.s.X10), dbg_itype_unsigned_long_int},
  106. {CV_ARM64_X0 + 11, "x11", (void*)FIELD_OFFSET(CONTEXT, u.s.X11), dbg_itype_unsigned_long_int},
  107. {CV_ARM64_X0 + 12, "x12", (void*)FIELD_OFFSET(CONTEXT, u.s.X12), dbg_itype_unsigned_long_int},
  108. {CV_ARM64_X0 + 13, "x13", (void*)FIELD_OFFSET(CONTEXT, u.s.X13), dbg_itype_unsigned_long_int},
  109. {CV_ARM64_X0 + 14, "x14", (void*)FIELD_OFFSET(CONTEXT, u.s.X14), dbg_itype_unsigned_long_int},
  110. {CV_ARM64_X0 + 15, "x15", (void*)FIELD_OFFSET(CONTEXT, u.s.X15), dbg_itype_unsigned_long_int},
  111. {CV_ARM64_X0 + 16, "x16", (void*)FIELD_OFFSET(CONTEXT, u.s.X16), dbg_itype_unsigned_long_int},
  112. {CV_ARM64_X0 + 17, "x17", (void*)FIELD_OFFSET(CONTEXT, u.s.X17), dbg_itype_unsigned_long_int},
  113. {CV_ARM64_X0 + 18, "x18", (void*)FIELD_OFFSET(CONTEXT, u.s.X18), dbg_itype_unsigned_long_int},
  114. {CV_ARM64_X0 + 19, "x19", (void*)FIELD_OFFSET(CONTEXT, u.s.X19), dbg_itype_unsigned_long_int},
  115. {CV_ARM64_X0 + 20, "x20", (void*)FIELD_OFFSET(CONTEXT, u.s.X20), dbg_itype_unsigned_long_int},
  116. {CV_ARM64_X0 + 21, "x21", (void*)FIELD_OFFSET(CONTEXT, u.s.X21), dbg_itype_unsigned_long_int},
  117. {CV_ARM64_X0 + 22, "x22", (void*)FIELD_OFFSET(CONTEXT, u.s.X22), dbg_itype_unsigned_long_int},
  118. {CV_ARM64_X0 + 23, "x23", (void*)FIELD_OFFSET(CONTEXT, u.s.X23), dbg_itype_unsigned_long_int},
  119. {CV_ARM64_X0 + 24, "x24", (void*)FIELD_OFFSET(CONTEXT, u.s.X24), dbg_itype_unsigned_long_int},
  120. {CV_ARM64_X0 + 25, "x25", (void*)FIELD_OFFSET(CONTEXT, u.s.X25), dbg_itype_unsigned_long_int},
  121. {CV_ARM64_X0 + 26, "x26", (void*)FIELD_OFFSET(CONTEXT, u.s.X26), dbg_itype_unsigned_long_int},
  122. {CV_ARM64_X0 + 27, "x27", (void*)FIELD_OFFSET(CONTEXT, u.s.X27), dbg_itype_unsigned_long_int},
  123. {CV_ARM64_X0 + 28, "x28", (void*)FIELD_OFFSET(CONTEXT, u.s.X28), dbg_itype_unsigned_long_int},
  124. {CV_ARM64_FP, "fp", (void*)FIELD_OFFSET(CONTEXT, u.s.Fp), dbg_itype_unsigned_long_int},
  125. {CV_ARM64_LR, "lr", (void*)FIELD_OFFSET(CONTEXT, u.s.Lr), dbg_itype_unsigned_long_int},
  126. {CV_ARM64_SP, "sp", (void*)FIELD_OFFSET(CONTEXT, Sp), dbg_itype_unsigned_long_int},
  127. {CV_ARM64_PC, "pc", (void*)FIELD_OFFSET(CONTEXT, Pc), dbg_itype_unsigned_long_int},
  128. {0, NULL, 0, dbg_itype_none}
  129. };
  130. static BOOL be_arm64_is_step_over_insn(const void* insn)
  131. {
  132. dbg_printf("be_arm64_is_step_over_insn: not done\n");
  133. return FALSE;
  134. }
  135. static BOOL be_arm64_is_function_return(const void* insn)
  136. {
  137. dbg_printf("be_arm64_is_function_return: not done\n");
  138. return FALSE;
  139. }
  140. static BOOL be_arm64_is_break_insn(const void* insn)
  141. {
  142. dbg_printf("be_arm64_is_break_insn: not done\n");
  143. return FALSE;
  144. }
  145. static BOOL be_arm64_is_func_call(const void* insn, ADDRESS64* callee)
  146. {
  147. return FALSE;
  148. }
  149. static BOOL be_arm64_is_jump(const void* insn, ADDRESS64* jumpee)
  150. {
  151. return FALSE;
  152. }
  153. static BOOL be_arm64_insert_Xpoint(HANDLE hProcess, const struct be_process_io* pio,
  154. dbg_ctx_t *ctx, enum be_xpoint_type type,
  155. void* addr, unsigned *val, unsigned size)
  156. {
  157. SIZE_T sz;
  158. switch (type)
  159. {
  160. case be_xpoint_break:
  161. if (!size) return FALSE;
  162. if (!pio->read(hProcess, addr, val, 4, &sz) || sz != 4) return FALSE;
  163. default:
  164. dbg_printf("Unknown/unsupported bp type %c\n", type);
  165. return FALSE;
  166. }
  167. return TRUE;
  168. }
  169. static BOOL be_arm64_remove_Xpoint(HANDLE hProcess, const struct be_process_io* pio,
  170. dbg_ctx_t *ctx, enum be_xpoint_type type,
  171. void* addr, unsigned val, unsigned size)
  172. {
  173. SIZE_T sz;
  174. switch (type)
  175. {
  176. case be_xpoint_break:
  177. if (!size) return FALSE;
  178. if (!pio->write(hProcess, addr, &val, 4, &sz) || sz == 4) return FALSE;
  179. break;
  180. default:
  181. dbg_printf("Unknown/unsupported bp type %c\n", type);
  182. return FALSE;
  183. }
  184. return TRUE;
  185. }
  186. static BOOL be_arm64_is_watchpoint_set(const dbg_ctx_t *ctx, unsigned idx)
  187. {
  188. dbg_printf("be_arm64_is_watchpoint_set: not done\n");
  189. return FALSE;
  190. }
  191. static void be_arm64_clear_watchpoint(dbg_ctx_t *ctx, unsigned idx)
  192. {
  193. dbg_printf("be_arm64_clear_watchpoint: not done\n");
  194. }
  195. static int be_arm64_adjust_pc_for_break(dbg_ctx_t *ctx, BOOL way)
  196. {
  197. if (way)
  198. {
  199. ctx->ctx.Pc -= 4;
  200. return -4;
  201. }
  202. ctx->ctx.Pc += 4;
  203. return 4;
  204. }
  205. void be_arm64_disasm_one_insn(ADDRESS64 *addr, int display)
  206. {
  207. dbg_printf("be_arm64_disasm_one_insn: not done\n");
  208. }
  209. static BOOL be_arm64_get_context(HANDLE thread, dbg_ctx_t *ctx)
  210. {
  211. ctx->ctx.ContextFlags = CONTEXT_ALL;
  212. return GetThreadContext(thread, &ctx->ctx);
  213. }
  214. static BOOL be_arm64_set_context(HANDLE thread, const dbg_ctx_t *ctx)
  215. {
  216. return SetThreadContext(thread, &ctx->ctx);
  217. }
  218. #define REG(f,n,t,r) {f, n, t, FIELD_OFFSET(CONTEXT, r), sizeof(((CONTEXT*)NULL)->r)}
  219. static struct gdb_register be_arm64_gdb_register_map[] = {
  220. REG("core", "x0", NULL, u.s.X0),
  221. REG(NULL, "x1", NULL, u.s.X1),
  222. REG(NULL, "x2", NULL, u.s.X2),
  223. REG(NULL, "x3", NULL, u.s.X3),
  224. REG(NULL, "x4", NULL, u.s.X4),
  225. REG(NULL, "x5", NULL, u.s.X5),
  226. REG(NULL, "x6", NULL, u.s.X6),
  227. REG(NULL, "x7", NULL, u.s.X7),
  228. REG(NULL, "x8", NULL, u.s.X8),
  229. REG(NULL, "x9", NULL, u.s.X9),
  230. REG(NULL, "x10", NULL, u.s.X10),
  231. REG(NULL, "x11", NULL, u.s.X11),
  232. REG(NULL, "x12", NULL, u.s.X12),
  233. REG(NULL, "x13", NULL, u.s.X13),
  234. REG(NULL, "x14", NULL, u.s.X14),
  235. REG(NULL, "x15", NULL, u.s.X15),
  236. REG(NULL, "x16", NULL, u.s.X16),
  237. REG(NULL, "x17", NULL, u.s.X17),
  238. REG(NULL, "x18", NULL, u.s.X18),
  239. REG(NULL, "x19", NULL, u.s.X19),
  240. REG(NULL, "x20", NULL, u.s.X20),
  241. REG(NULL, "x21", NULL, u.s.X21),
  242. REG(NULL, "x22", NULL, u.s.X22),
  243. REG(NULL, "x23", NULL, u.s.X23),
  244. REG(NULL, "x24", NULL, u.s.X24),
  245. REG(NULL, "x25", NULL, u.s.X25),
  246. REG(NULL, "x26", NULL, u.s.X26),
  247. REG(NULL, "x27", NULL, u.s.X27),
  248. REG(NULL, "x28", NULL, u.s.X28),
  249. REG(NULL, "x29", NULL, u.s.Fp),
  250. REG(NULL, "x30", NULL, u.s.Lr),
  251. REG(NULL, "sp", "data_ptr", Sp),
  252. REG(NULL, "pc", "code_ptr", Pc),
  253. REG(NULL, "cpsr", "cpsr_flags", Cpsr),
  254. };
  255. struct backend_cpu be_arm64 =
  256. {
  257. IMAGE_FILE_MACHINE_ARM64,
  258. 8,
  259. be_cpu_linearize,
  260. be_cpu_build_addr,
  261. be_arm64_get_addr,
  262. be_arm64_get_register_info,
  263. be_arm64_single_step,
  264. be_arm64_print_context,
  265. be_arm64_print_segment_info,
  266. be_arm64_ctx,
  267. be_arm64_is_step_over_insn,
  268. be_arm64_is_function_return,
  269. be_arm64_is_break_insn,
  270. be_arm64_is_func_call,
  271. be_arm64_is_jump,
  272. be_arm64_disasm_one_insn,
  273. be_arm64_insert_Xpoint,
  274. be_arm64_remove_Xpoint,
  275. be_arm64_is_watchpoint_set,
  276. be_arm64_clear_watchpoint,
  277. be_arm64_adjust_pc_for_break,
  278. be_arm64_get_context,
  279. be_arm64_set_context,
  280. be_arm64_gdb_register_map,
  281. ARRAY_SIZE(be_arm64_gdb_register_map),
  282. };
  283. #endif