kgdb.c 15 KB

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