kgdb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. int reg;
  121. struct pt_regs *thread_regs;
  122. unsigned long *ptr = gdb_regs;
  123. if (task == NULL)
  124. return;
  125. /* Initialize to zero. */
  126. memset(gdb_regs, 0, NUMREGBYTES);
  127. thread_regs = task_pt_regs(task);
  128. for (reg = 0; reg <= TREG_LAST_GPR; reg++)
  129. *(ptr++) = thread_regs->regs[reg];
  130. gdb_regs[TILEGX_PC_REGNUM] = thread_regs->pc;
  131. gdb_regs[TILEGX_FAULTNUM_REGNUM] = thread_regs->faultnum;
  132. }
  133. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  134. {
  135. regs->pc = pc;
  136. }
  137. static void kgdb_call_nmi_hook(void *ignored)
  138. {
  139. kgdb_nmicallback(raw_smp_processor_id(), NULL);
  140. }
  141. void kgdb_roundup_cpus(unsigned long flags)
  142. {
  143. local_irq_enable();
  144. smp_call_function(kgdb_call_nmi_hook, NULL, 0);
  145. local_irq_disable();
  146. }
  147. /*
  148. * Convert a kernel address to the writable kernel text mapping.
  149. */
  150. static unsigned long writable_address(unsigned long addr)
  151. {
  152. unsigned long ret = 0;
  153. if (core_kernel_text(addr))
  154. ret = addr - MEM_SV_START + PAGE_OFFSET;
  155. else if (is_module_text_address(addr))
  156. ret = addr;
  157. else
  158. pr_err("Unknown virtual address 0x%lx\n", addr);
  159. return ret;
  160. }
  161. /*
  162. * Calculate the new address for after a step.
  163. */
  164. static unsigned long get_step_address(struct pt_regs *regs)
  165. {
  166. int src_reg;
  167. int jump_off;
  168. int br_off;
  169. unsigned long addr;
  170. unsigned int opcode;
  171. tile_bundle_bits bundle;
  172. /* Move to the next instruction by default. */
  173. addr = regs->pc + TILEGX_BUNDLE_SIZE_IN_BYTES;
  174. bundle = *(unsigned long *)instruction_pointer(regs);
  175. /* 0: X mode, Otherwise: Y mode. */
  176. if (bundle & TILEGX_BUNDLE_MODE_MASK) {
  177. if (get_Opcode_Y1(bundle) == RRR_1_OPCODE_Y1 &&
  178. get_RRROpcodeExtension_Y1(bundle) ==
  179. UNARY_RRR_1_OPCODE_Y1) {
  180. opcode = get_UnaryOpcodeExtension_Y1(bundle);
  181. switch (opcode) {
  182. case JALR_UNARY_OPCODE_Y1:
  183. case JALRP_UNARY_OPCODE_Y1:
  184. case JR_UNARY_OPCODE_Y1:
  185. case JRP_UNARY_OPCODE_Y1:
  186. src_reg = get_SrcA_Y1(bundle);
  187. dbg_get_reg(src_reg, &addr, regs);
  188. break;
  189. }
  190. }
  191. } else if (get_Opcode_X1(bundle) == RRR_0_OPCODE_X1) {
  192. if (get_RRROpcodeExtension_X1(bundle) ==
  193. UNARY_RRR_0_OPCODE_X1) {
  194. opcode = get_UnaryOpcodeExtension_X1(bundle);
  195. switch (opcode) {
  196. case JALR_UNARY_OPCODE_X1:
  197. case JALRP_UNARY_OPCODE_X1:
  198. case JR_UNARY_OPCODE_X1:
  199. case JRP_UNARY_OPCODE_X1:
  200. src_reg = get_SrcA_X1(bundle);
  201. dbg_get_reg(src_reg, &addr, regs);
  202. break;
  203. }
  204. }
  205. } else if (get_Opcode_X1(bundle) == JUMP_OPCODE_X1) {
  206. opcode = get_JumpOpcodeExtension_X1(bundle);
  207. switch (opcode) {
  208. case JAL_JUMP_OPCODE_X1:
  209. case J_JUMP_OPCODE_X1:
  210. jump_off = sign_extend(get_JumpOff_X1(bundle), 27);
  211. addr = regs->pc +
  212. (jump_off << TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES);
  213. break;
  214. }
  215. } else if (get_Opcode_X1(bundle) == BRANCH_OPCODE_X1) {
  216. br_off = 0;
  217. opcode = get_BrType_X1(bundle);
  218. switch (opcode) {
  219. case BEQZT_BRANCH_OPCODE_X1:
  220. case BEQZ_BRANCH_OPCODE_X1:
  221. if (get_SrcA_X1(bundle) == 0)
  222. br_off = get_BrOff_X1(bundle);
  223. break;
  224. case BGEZT_BRANCH_OPCODE_X1:
  225. case BGEZ_BRANCH_OPCODE_X1:
  226. if (get_SrcA_X1(bundle) >= 0)
  227. br_off = get_BrOff_X1(bundle);
  228. break;
  229. case BGTZT_BRANCH_OPCODE_X1:
  230. case BGTZ_BRANCH_OPCODE_X1:
  231. if (get_SrcA_X1(bundle) > 0)
  232. br_off = get_BrOff_X1(bundle);
  233. break;
  234. case BLBCT_BRANCH_OPCODE_X1:
  235. case BLBC_BRANCH_OPCODE_X1:
  236. if (!(get_SrcA_X1(bundle) & 1))
  237. br_off = get_BrOff_X1(bundle);
  238. break;
  239. case BLBST_BRANCH_OPCODE_X1:
  240. case BLBS_BRANCH_OPCODE_X1:
  241. if (get_SrcA_X1(bundle) & 1)
  242. br_off = get_BrOff_X1(bundle);
  243. break;
  244. case BLEZT_BRANCH_OPCODE_X1:
  245. case BLEZ_BRANCH_OPCODE_X1:
  246. if (get_SrcA_X1(bundle) <= 0)
  247. br_off = get_BrOff_X1(bundle);
  248. break;
  249. case BLTZT_BRANCH_OPCODE_X1:
  250. case BLTZ_BRANCH_OPCODE_X1:
  251. if (get_SrcA_X1(bundle) < 0)
  252. br_off = get_BrOff_X1(bundle);
  253. break;
  254. case BNEZT_BRANCH_OPCODE_X1:
  255. case BNEZ_BRANCH_OPCODE_X1:
  256. if (get_SrcA_X1(bundle) != 0)
  257. br_off = get_BrOff_X1(bundle);
  258. break;
  259. }
  260. if (br_off != 0) {
  261. br_off = sign_extend(br_off, 17);
  262. addr = regs->pc +
  263. (br_off << TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES);
  264. }
  265. }
  266. return addr;
  267. }
  268. /*
  269. * Replace the next instruction after the current instruction with a
  270. * breakpoint instruction.
  271. */
  272. static void do_single_step(struct pt_regs *regs)
  273. {
  274. unsigned long addr_wr;
  275. /* Determine where the target instruction will send us to. */
  276. stepped_addr = get_step_address(regs);
  277. probe_kernel_read((char *)&stepped_instr, (char *)stepped_addr,
  278. BREAK_INSTR_SIZE);
  279. addr_wr = writable_address(stepped_addr);
  280. probe_kernel_write((char *)addr_wr, (char *)&singlestep_insn,
  281. BREAK_INSTR_SIZE);
  282. smp_wmb();
  283. flush_icache_range(stepped_addr, stepped_addr + BREAK_INSTR_SIZE);
  284. }
  285. static void undo_single_step(struct pt_regs *regs)
  286. {
  287. unsigned long addr_wr;
  288. if (stepped_instr == 0)
  289. return;
  290. addr_wr = writable_address(stepped_addr);
  291. probe_kernel_write((char *)addr_wr, (char *)&stepped_instr,
  292. BREAK_INSTR_SIZE);
  293. stepped_instr = 0;
  294. smp_wmb();
  295. flush_icache_range(stepped_addr, stepped_addr + BREAK_INSTR_SIZE);
  296. }
  297. /*
  298. * Calls linux_debug_hook before the kernel dies. If KGDB is enabled,
  299. * then try to fall into the debugger.
  300. */
  301. static int
  302. kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
  303. {
  304. int ret;
  305. unsigned long flags;
  306. struct die_args *args = (struct die_args *)ptr;
  307. struct pt_regs *regs = args->regs;
  308. #ifdef CONFIG_KPROBES
  309. /*
  310. * Return immediately if the kprobes fault notifier has set
  311. * DIE_PAGE_FAULT.
  312. */
  313. if (cmd == DIE_PAGE_FAULT)
  314. return NOTIFY_DONE;
  315. #endif /* CONFIG_KPROBES */
  316. switch (cmd) {
  317. case DIE_BREAK:
  318. case DIE_COMPILED_BPT:
  319. break;
  320. case DIE_SSTEPBP:
  321. local_irq_save(flags);
  322. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  323. local_irq_restore(flags);
  324. return NOTIFY_STOP;
  325. default:
  326. /* Userspace events, ignore. */
  327. if (user_mode(regs))
  328. return NOTIFY_DONE;
  329. }
  330. local_irq_save(flags);
  331. ret = kgdb_handle_exception(args->trapnr, args->signr, args->err, regs);
  332. local_irq_restore(flags);
  333. if (ret)
  334. return NOTIFY_DONE;
  335. return NOTIFY_STOP;
  336. }
  337. static struct notifier_block kgdb_notifier = {
  338. .notifier_call = kgdb_notify,
  339. };
  340. /*
  341. * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
  342. * @vector: The error vector of the exception that happened.
  343. * @signo: The signal number of the exception that happened.
  344. * @err_code: The error code of the exception that happened.
  345. * @remcom_in_buffer: The buffer of the packet we have read.
  346. * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
  347. * @regs: The &struct pt_regs of the current process.
  348. *
  349. * This function MUST handle the 'c' and 's' command packets,
  350. * as well packets to set / remove a hardware breakpoint, if used.
  351. * If there are additional packets which the hardware needs to handle,
  352. * they are handled here. The code should return -1 if it wants to
  353. * process more packets, and a %0 or %1 if it wants to exit from the
  354. * kgdb callback.
  355. */
  356. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  357. char *remcom_in_buffer, char *remcom_out_buffer,
  358. struct pt_regs *regs)
  359. {
  360. char *ptr;
  361. unsigned long address;
  362. /* Undo any stepping we may have done. */
  363. undo_single_step(regs);
  364. switch (remcom_in_buffer[0]) {
  365. case 'c':
  366. case 's':
  367. case 'D':
  368. case 'k':
  369. /*
  370. * Try to read optional parameter, pc unchanged if no parm.
  371. * If this was a compiled-in breakpoint, we need to move
  372. * to the next instruction or we will just breakpoint
  373. * over and over again.
  374. */
  375. ptr = &remcom_in_buffer[1];
  376. if (kgdb_hex2long(&ptr, &address))
  377. regs->pc = address;
  378. else if (*(unsigned long *)regs->pc == compiled_bpt)
  379. regs->pc += BREAK_INSTR_SIZE;
  380. if (remcom_in_buffer[0] == 's') {
  381. do_single_step(regs);
  382. kgdb_single_step = 1;
  383. atomic_set(&kgdb_cpu_doing_single_step,
  384. raw_smp_processor_id());
  385. } else
  386. atomic_set(&kgdb_cpu_doing_single_step, -1);
  387. return 0;
  388. }
  389. return -1; /* this means that we do not want to exit from the handler */
  390. }
  391. struct kgdb_arch arch_kgdb_ops;
  392. /*
  393. * kgdb_arch_init - Perform any architecture specific initalization.
  394. *
  395. * This function will handle the initalization of any architecture
  396. * specific callbacks.
  397. */
  398. int kgdb_arch_init(void)
  399. {
  400. tile_bundle_bits bundle = TILEGX_BPT_BUNDLE;
  401. memcpy(arch_kgdb_ops.gdb_bpt_instr, &bundle, BREAK_INSTR_SIZE);
  402. return register_die_notifier(&kgdb_notifier);
  403. }
  404. /*
  405. * kgdb_arch_exit - Perform any architecture specific uninitalization.
  406. *
  407. * This function will handle the uninitalization of any architecture
  408. * specific callbacks, for dynamic registration and unregistration.
  409. */
  410. void kgdb_arch_exit(void)
  411. {
  412. unregister_die_notifier(&kgdb_notifier);
  413. }
  414. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  415. {
  416. int err;
  417. unsigned long addr_wr = writable_address(bpt->bpt_addr);
  418. if (addr_wr == 0)
  419. return -1;
  420. err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
  421. BREAK_INSTR_SIZE);
  422. if (err)
  423. return err;
  424. err = probe_kernel_write((char *)addr_wr, arch_kgdb_ops.gdb_bpt_instr,
  425. BREAK_INSTR_SIZE);
  426. smp_wmb();
  427. flush_icache_range((unsigned long)bpt->bpt_addr,
  428. (unsigned long)bpt->bpt_addr + BREAK_INSTR_SIZE);
  429. return err;
  430. }
  431. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  432. {
  433. int err;
  434. unsigned long addr_wr = writable_address(bpt->bpt_addr);
  435. if (addr_wr == 0)
  436. return -1;
  437. err = probe_kernel_write((char *)addr_wr, (char *)bpt->saved_instr,
  438. BREAK_INSTR_SIZE);
  439. smp_wmb();
  440. flush_icache_range((unsigned long)bpt->bpt_addr,
  441. (unsigned long)bpt->bpt_addr + BREAK_INSTR_SIZE);
  442. return err;
  443. }