kgdb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright 2013 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * TILE-Gx KGDB support.
  15. */
  16. #include <linux/ptrace.h>
  17. #include <linux/kgdb.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/module.h>
  21. #include <asm/cacheflush.h>
  22. static tile_bundle_bits singlestep_insn = TILEGX_BPT_BUNDLE | DIE_SSTEPBP;
  23. static unsigned long stepped_addr;
  24. static tile_bundle_bits stepped_instr;
  25. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
  26. { "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[0])},
  27. { "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[1])},
  28. { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[2])},
  29. { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[3])},
  30. { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[4])},
  31. { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[5])},
  32. { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[6])},
  33. { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[7])},
  34. { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[8])},
  35. { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[9])},
  36. { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[10])},
  37. { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[11])},
  38. { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[12])},
  39. { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[13])},
  40. { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[14])},
  41. { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[15])},
  42. { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[16])},
  43. { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[17])},
  44. { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[18])},
  45. { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[19])},
  46. { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[20])},
  47. { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[21])},
  48. { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[22])},
  49. { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[23])},
  50. { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[24])},
  51. { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[25])},
  52. { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[26])},
  53. { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[27])},
  54. { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[28])},
  55. { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[29])},
  56. { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[30])},
  57. { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[31])},
  58. { "r32", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[32])},
  59. { "r33", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[33])},
  60. { "r34", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[34])},
  61. { "r35", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[35])},
  62. { "r36", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[36])},
  63. { "r37", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[37])},
  64. { "r38", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[38])},
  65. { "r39", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[39])},
  66. { "r40", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[40])},
  67. { "r41", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[41])},
  68. { "r42", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[42])},
  69. { "r43", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[43])},
  70. { "r44", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[44])},
  71. { "r45", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[45])},
  72. { "r46", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[46])},
  73. { "r47", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[47])},
  74. { "r48", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[48])},
  75. { "r49", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[49])},
  76. { "r50", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[50])},
  77. { "r51", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[51])},
  78. { "r52", GDB_SIZEOF_REG, offsetof(struct pt_regs, regs[52])},
  79. { "tp", GDB_SIZEOF_REG, offsetof(struct pt_regs, tp)},
  80. { "sp", GDB_SIZEOF_REG, offsetof(struct pt_regs, sp)},
  81. { "lr", GDB_SIZEOF_REG, offsetof(struct pt_regs, lr)},
  82. { "sn", GDB_SIZEOF_REG, -1},
  83. { "idn0", GDB_SIZEOF_REG, -1},
  84. { "idn1", GDB_SIZEOF_REG, -1},
  85. { "udn0", GDB_SIZEOF_REG, -1},
  86. { "udn1", GDB_SIZEOF_REG, -1},
  87. { "udn2", GDB_SIZEOF_REG, -1},
  88. { "udn3", GDB_SIZEOF_REG, -1},
  89. { "zero", GDB_SIZEOF_REG, -1},
  90. { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, pc)},
  91. { "faultnum", GDB_SIZEOF_REG, offsetof(struct pt_regs, faultnum)},
  92. };
  93. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  94. {
  95. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  96. return NULL;
  97. if (dbg_reg_def[regno].offset != -1)
  98. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  99. dbg_reg_def[regno].size);
  100. else
  101. memset(mem, 0, dbg_reg_def[regno].size);
  102. return dbg_reg_def[regno].name;
  103. }
  104. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  105. {
  106. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  107. return -EINVAL;
  108. if (dbg_reg_def[regno].offset != -1)
  109. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  110. dbg_reg_def[regno].size);
  111. return 0;
  112. }
  113. /*
  114. * Similar to pt_regs_to_gdb_regs() except that process is sleeping and so
  115. * we may not be able to get all the info.
  116. */
  117. void
  118. sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
  119. {
  120. struct pt_regs *thread_regs;
  121. if (task == NULL)
  122. return;
  123. /* Initialize to zero. */
  124. memset(gdb_regs, 0, NUMREGBYTES);
  125. thread_regs = task_pt_regs(task);
  126. memcpy(gdb_regs, thread_regs, TREG_LAST_GPR * sizeof(unsigned long));
  127. gdb_regs[TILEGX_PC_REGNUM] = thread_regs->pc;
  128. gdb_regs[TILEGX_FAULTNUM_REGNUM] = thread_regs->faultnum;
  129. }
  130. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  131. {
  132. regs->pc = pc;
  133. }
  134. static void kgdb_call_nmi_hook(void *ignored)
  135. {
  136. kgdb_nmicallback(raw_smp_processor_id(), NULL);
  137. }
  138. void kgdb_roundup_cpus(unsigned long flags)
  139. {
  140. local_irq_enable();
  141. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  142. local_irq_disable();
  143. }
  144. /*
  145. * Convert a kernel address to the writable kernel text mapping.
  146. */
  147. static unsigned long writable_address(unsigned long addr)
  148. {
  149. unsigned long ret = 0;
  150. if (core_kernel_text(addr))
  151. ret = addr - MEM_SV_START + PAGE_OFFSET;
  152. else if (is_module_text_address(addr))
  153. ret = addr;
  154. else
  155. pr_err("Unknown virtual address 0x%lx\n", addr);
  156. return ret;
  157. }
  158. /*
  159. * Calculate the new address for after a step.
  160. */
  161. static unsigned long get_step_address(struct pt_regs *regs)
  162. {
  163. int src_reg;
  164. int jump_off;
  165. int br_off;
  166. unsigned long addr;
  167. unsigned int opcode;
  168. tile_bundle_bits bundle;
  169. /* Move to the next instruction by default. */
  170. addr = regs->pc + TILEGX_BUNDLE_SIZE_IN_BYTES;
  171. bundle = *(unsigned long *)instruction_pointer(regs);
  172. /* 0: X mode, Otherwise: Y mode. */
  173. if (bundle & TILEGX_BUNDLE_MODE_MASK) {
  174. if (get_Opcode_Y1(bundle) == RRR_1_OPCODE_Y1 &&
  175. get_RRROpcodeExtension_Y1(bundle) ==
  176. UNARY_RRR_1_OPCODE_Y1) {
  177. opcode = get_UnaryOpcodeExtension_Y1(bundle);
  178. switch (opcode) {
  179. case JALR_UNARY_OPCODE_Y1:
  180. case JALRP_UNARY_OPCODE_Y1:
  181. case JR_UNARY_OPCODE_Y1:
  182. case JRP_UNARY_OPCODE_Y1:
  183. src_reg = get_SrcA_Y1(bundle);
  184. dbg_get_reg(src_reg, &addr, regs);
  185. break;
  186. }
  187. }
  188. } else if (get_Opcode_X1(bundle) == RRR_0_OPCODE_X1) {
  189. if (get_RRROpcodeExtension_X1(bundle) ==
  190. UNARY_RRR_0_OPCODE_X1) {
  191. opcode = get_UnaryOpcodeExtension_X1(bundle);
  192. switch (opcode) {
  193. case JALR_UNARY_OPCODE_X1:
  194. case JALRP_UNARY_OPCODE_X1:
  195. case JR_UNARY_OPCODE_X1:
  196. case JRP_UNARY_OPCODE_X1:
  197. src_reg = get_SrcA_X1(bundle);
  198. dbg_get_reg(src_reg, &addr, regs);
  199. break;
  200. }
  201. }
  202. } else if (get_Opcode_X1(bundle) == JUMP_OPCODE_X1) {
  203. opcode = get_JumpOpcodeExtension_X1(bundle);
  204. switch (opcode) {
  205. case JAL_JUMP_OPCODE_X1:
  206. case J_JUMP_OPCODE_X1:
  207. jump_off = sign_extend(get_JumpOff_X1(bundle), 27);
  208. addr = regs->pc +
  209. (jump_off << TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES);
  210. break;
  211. }
  212. } else if (get_Opcode_X1(bundle) == BRANCH_OPCODE_X1) {
  213. br_off = 0;
  214. opcode = get_BrType_X1(bundle);
  215. switch (opcode) {
  216. case BEQZT_BRANCH_OPCODE_X1:
  217. case BEQZ_BRANCH_OPCODE_X1:
  218. if (get_SrcA_X1(bundle) == 0)
  219. br_off = get_BrOff_X1(bundle);
  220. break;
  221. case BGEZT_BRANCH_OPCODE_X1:
  222. case BGEZ_BRANCH_OPCODE_X1:
  223. if (get_SrcA_X1(bundle) >= 0)
  224. br_off = get_BrOff_X1(bundle);
  225. break;
  226. case BGTZT_BRANCH_OPCODE_X1:
  227. case BGTZ_BRANCH_OPCODE_X1:
  228. if (get_SrcA_X1(bundle) > 0)
  229. br_off = get_BrOff_X1(bundle);
  230. break;
  231. case BLBCT_BRANCH_OPCODE_X1:
  232. case BLBC_BRANCH_OPCODE_X1:
  233. if (!(get_SrcA_X1(bundle) & 1))
  234. br_off = get_BrOff_X1(bundle);
  235. break;
  236. case BLBST_BRANCH_OPCODE_X1:
  237. case BLBS_BRANCH_OPCODE_X1:
  238. if (get_SrcA_X1(bundle) & 1)
  239. br_off = get_BrOff_X1(bundle);
  240. break;
  241. case BLEZT_BRANCH_OPCODE_X1:
  242. case BLEZ_BRANCH_OPCODE_X1:
  243. if (get_SrcA_X1(bundle) <= 0)
  244. br_off = get_BrOff_X1(bundle);
  245. break;
  246. case BLTZT_BRANCH_OPCODE_X1:
  247. case BLTZ_BRANCH_OPCODE_X1:
  248. if (get_SrcA_X1(bundle) < 0)
  249. br_off = get_BrOff_X1(bundle);
  250. break;
  251. case BNEZT_BRANCH_OPCODE_X1:
  252. case BNEZ_BRANCH_OPCODE_X1:
  253. if (get_SrcA_X1(bundle) != 0)
  254. br_off = get_BrOff_X1(bundle);
  255. break;
  256. }
  257. if (br_off != 0) {
  258. br_off = sign_extend(br_off, 17);
  259. addr = regs->pc +
  260. (br_off << TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES);
  261. }
  262. }
  263. return addr;
  264. }
  265. /*
  266. * Replace the next instruction after the current instruction with a
  267. * breakpoint instruction.
  268. */
  269. static void do_single_step(struct pt_regs *regs)
  270. {
  271. unsigned long addr_wr;
  272. /* Determine where the target instruction will send us to. */
  273. stepped_addr = get_step_address(regs);
  274. probe_kernel_read((char *)&stepped_instr, (char *)stepped_addr,
  275. BREAK_INSTR_SIZE);
  276. addr_wr = writable_address(stepped_addr);
  277. probe_kernel_write((char *)addr_wr, (char *)&singlestep_insn,
  278. BREAK_INSTR_SIZE);
  279. smp_wmb();
  280. flush_icache_range(stepped_addr, stepped_addr + BREAK_INSTR_SIZE);
  281. }
  282. static void undo_single_step(struct pt_regs *regs)
  283. {
  284. unsigned long addr_wr;
  285. if (stepped_instr == 0)
  286. return;
  287. addr_wr = writable_address(stepped_addr);
  288. probe_kernel_write((char *)addr_wr, (char *)&stepped_instr,
  289. BREAK_INSTR_SIZE);
  290. stepped_instr = 0;
  291. smp_wmb();
  292. flush_icache_range(stepped_addr, stepped_addr + BREAK_INSTR_SIZE);
  293. }
  294. /*
  295. * Calls linux_debug_hook before the kernel dies. If KGDB is enabled,
  296. * then try to fall into the debugger.
  297. */
  298. static int
  299. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  300. {
  301. int ret;
  302. unsigned long flags;
  303. struct die_args *args = (struct die_args *)ptr;
  304. struct pt_regs *regs = args->regs;
  305. #ifdef CONFIG_KPROBES
  306. /*
  307. * Return immediately if the kprobes fault notifier has set
  308. * DIE_PAGE_FAULT.
  309. */
  310. if (cmd == DIE_PAGE_FAULT)
  311. return NOTIFY_DONE;
  312. #endif /* CONFIG_KPROBES */
  313. switch (cmd) {
  314. case DIE_BREAK:
  315. case DIE_COMPILED_BPT:
  316. break;
  317. case DIE_SSTEPBP:
  318. local_irq_save(flags);
  319. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  320. local_irq_restore(flags);
  321. return NOTIFY_STOP;
  322. default:
  323. /* Userspace events, ignore. */
  324. if (user_mode(regs))
  325. return NOTIFY_DONE;
  326. }
  327. local_irq_save(flags);
  328. ret = kgdb_handle_exception(args->trapnr, args->signr, args->err, regs);
  329. local_irq_restore(flags);
  330. if (ret)
  331. return NOTIFY_DONE;
  332. return NOTIFY_STOP;
  333. }
  334. static struct notifier_block kgdb_notifier = {
  335. .notifier_call = kgdb_notify,
  336. };
  337. /*
  338. * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
  339. * @vector: The error vector of the exception that happened.
  340. * @signo: The signal number of the exception that happened.
  341. * @err_code: The error code of the exception that happened.
  342. * @remcom_in_buffer: The buffer of the packet we have read.
  343. * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
  344. * @regs: The &struct pt_regs of the current process.
  345. *
  346. * This function MUST handle the 'c' and 's' command packets,
  347. * as well packets to set / remove a hardware breakpoint, if used.
  348. * If there are additional packets which the hardware needs to handle,
  349. * they are handled here. The code should return -1 if it wants to
  350. * process more packets, and a %0 or %1 if it wants to exit from the
  351. * kgdb callback.
  352. */
  353. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  354. char *remcom_in_buffer, char *remcom_out_buffer,
  355. struct pt_regs *regs)
  356. {
  357. char *ptr;
  358. unsigned long address;
  359. /* Undo any stepping we may have done. */
  360. undo_single_step(regs);
  361. switch (remcom_in_buffer[0]) {
  362. case 'c':
  363. case 's':
  364. case 'D':
  365. case 'k':
  366. /*
  367. * Try to read optional parameter, pc unchanged if no parm.
  368. * If this was a compiled-in breakpoint, we need to move
  369. * to the next instruction or we will just breakpoint
  370. * over and over again.
  371. */
  372. ptr = &remcom_in_buffer[1];
  373. if (kgdb_hex2long(&ptr, &address))
  374. regs->pc = address;
  375. else if (*(unsigned long *)regs->pc == compiled_bpt)
  376. regs->pc += BREAK_INSTR_SIZE;
  377. if (remcom_in_buffer[0] == 's') {
  378. do_single_step(regs);
  379. kgdb_single_step = 1;
  380. atomic_set(&kgdb_cpu_doing_single_step,
  381. raw_smp_processor_id());
  382. } else
  383. atomic_set(&kgdb_cpu_doing_single_step, -1);
  384. return 0;
  385. }
  386. return -1; /* this means that we do not want to exit from the handler */
  387. }
  388. struct kgdb_arch arch_kgdb_ops;
  389. /*
  390. * kgdb_arch_init - Perform any architecture specific initalization.
  391. *
  392. * This function will handle the initalization of any architecture
  393. * specific callbacks.
  394. */
  395. int kgdb_arch_init(void)
  396. {
  397. tile_bundle_bits bundle = TILEGX_BPT_BUNDLE;
  398. memcpy(arch_kgdb_ops.gdb_bpt_instr, &bundle, BREAK_INSTR_SIZE);
  399. return register_die_notifier(&kgdb_notifier);
  400. }
  401. /*
  402. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  403. *
  404. * This function will handle the uninitalization of any architecture
  405. * specific callbacks, for dynamic registration and unregistration.
  406. */
  407. void kgdb_arch_exit(void)
  408. {
  409. unregister_die_notifier(&kgdb_notifier);
  410. }
  411. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  412. {
  413. int err;
  414. unsigned long addr_wr = writable_address(bpt->bpt_addr);
  415. if (addr_wr == 0)
  416. return -1;
  417. err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
  418. BREAK_INSTR_SIZE);
  419. if (err)
  420. return err;
  421. err = probe_kernel_write((char *)addr_wr, arch_kgdb_ops.gdb_bpt_instr,
  422. BREAK_INSTR_SIZE);
  423. smp_wmb();
  424. flush_icache_range((unsigned long)bpt->bpt_addr,
  425. (unsigned long)bpt->bpt_addr + BREAK_INSTR_SIZE);
  426. return err;
  427. }
  428. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  429. {
  430. int err;
  431. unsigned long addr_wr = writable_address(bpt->bpt_addr);
  432. if (addr_wr == 0)
  433. return -1;
  434. err = probe_kernel_write((char *)addr_wr, (char *)bpt->saved_instr,
  435. BREAK_INSTR_SIZE);
  436. smp_wmb();
  437. flush_icache_range((unsigned long)bpt->bpt_addr,
  438. (unsigned long)bpt->bpt_addr + BREAK_INSTR_SIZE);
  439. return err;
  440. }