gdbstub.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel Debug Core
  4. *
  5. * Maintainer: Jason Wessel <[email protected]>
  6. *
  7. * Copyright (C) 2000-2001 VERITAS Software Corporation.
  8. * Copyright (C) 2002-2004 Timesys Corporation
  9. * Copyright (C) 2003-2004 Amit S. Kale <[email protected]>
  10. * Copyright (C) 2004 Pavel Machek <[email protected]>
  11. * Copyright (C) 2004-2006 Tom Rini <[email protected]>
  12. * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
  13. * Copyright (C) 2005-2009 Wind River Systems, Inc.
  14. * Copyright (C) 2007 MontaVista Software, Inc.
  15. * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <[email protected]>
  16. *
  17. * Contributors at various stages not listed above:
  18. * Jason Wessel ( [email protected] )
  19. * George Anzinger <[email protected]>
  20. * Anurekh Saxena ([email protected])
  21. * Lake Stevens Instrument Division (Glenn Engel)
  22. * Jim Kingdon, Cygnus Support.
  23. *
  24. * Original KGDB stub: David Grothe <[email protected]>,
  25. * Tigran Aivazian <[email protected]>
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/sched/signal.h>
  29. #include <linux/kgdb.h>
  30. #include <linux/kdb.h>
  31. #include <linux/serial_core.h>
  32. #include <linux/reboot.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/unaligned.h>
  36. #include "debug_core.h"
  37. #define KGDB_MAX_THREAD_QUERY 17
  38. /* Our I/O buffers. */
  39. static char remcom_in_buffer[BUFMAX];
  40. static char remcom_out_buffer[BUFMAX];
  41. static int gdbstub_use_prev_in_buf;
  42. static int gdbstub_prev_in_buf_pos;
  43. /* Storage for the registers, in GDB format. */
  44. static unsigned long gdb_regs[(NUMREGBYTES +
  45. sizeof(unsigned long) - 1) /
  46. sizeof(unsigned long)];
  47. /*
  48. * GDB remote protocol parser:
  49. */
  50. #ifdef CONFIG_KGDB_KDB
  51. static int gdbstub_read_wait(void)
  52. {
  53. int ret = -1;
  54. int i;
  55. if (unlikely(gdbstub_use_prev_in_buf)) {
  56. if (gdbstub_prev_in_buf_pos < gdbstub_use_prev_in_buf)
  57. return remcom_in_buffer[gdbstub_prev_in_buf_pos++];
  58. else
  59. gdbstub_use_prev_in_buf = 0;
  60. }
  61. /* poll any additional I/O interfaces that are defined */
  62. while (ret < 0)
  63. for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
  64. ret = kdb_poll_funcs[i]();
  65. if (ret > 0)
  66. break;
  67. }
  68. return ret;
  69. }
  70. #else
  71. static int gdbstub_read_wait(void)
  72. {
  73. int ret = dbg_io_ops->read_char();
  74. while (ret == NO_POLL_CHAR)
  75. ret = dbg_io_ops->read_char();
  76. return ret;
  77. }
  78. #endif
  79. /* scan for the sequence $<data>#<checksum> */
  80. static void get_packet(char *buffer)
  81. {
  82. unsigned char checksum;
  83. unsigned char xmitcsum;
  84. int count;
  85. char ch;
  86. do {
  87. /*
  88. * Spin and wait around for the start character, ignore all
  89. * other characters:
  90. */
  91. while ((ch = (gdbstub_read_wait())) != '$')
  92. /* nothing */;
  93. kgdb_connected = 1;
  94. checksum = 0;
  95. xmitcsum = -1;
  96. count = 0;
  97. /*
  98. * now, read until a # or end of buffer is found:
  99. */
  100. while (count < (BUFMAX - 1)) {
  101. ch = gdbstub_read_wait();
  102. if (ch == '#')
  103. break;
  104. checksum = checksum + ch;
  105. buffer[count] = ch;
  106. count = count + 1;
  107. }
  108. if (ch == '#') {
  109. xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4;
  110. xmitcsum += hex_to_bin(gdbstub_read_wait());
  111. if (checksum != xmitcsum)
  112. /* failed checksum */
  113. dbg_io_ops->write_char('-');
  114. else
  115. /* successful transfer */
  116. dbg_io_ops->write_char('+');
  117. if (dbg_io_ops->flush)
  118. dbg_io_ops->flush();
  119. }
  120. buffer[count] = 0;
  121. } while (checksum != xmitcsum);
  122. }
  123. /*
  124. * Send the packet in buffer.
  125. * Check for gdb connection if asked for.
  126. */
  127. static void put_packet(char *buffer)
  128. {
  129. unsigned char checksum;
  130. int count;
  131. char ch;
  132. /*
  133. * $<packet info>#<checksum>.
  134. */
  135. while (1) {
  136. dbg_io_ops->write_char('$');
  137. checksum = 0;
  138. count = 0;
  139. while ((ch = buffer[count])) {
  140. dbg_io_ops->write_char(ch);
  141. checksum += ch;
  142. count++;
  143. }
  144. dbg_io_ops->write_char('#');
  145. dbg_io_ops->write_char(hex_asc_hi(checksum));
  146. dbg_io_ops->write_char(hex_asc_lo(checksum));
  147. if (dbg_io_ops->flush)
  148. dbg_io_ops->flush();
  149. /* Now see what we get in reply. */
  150. ch = gdbstub_read_wait();
  151. if (ch == 3)
  152. ch = gdbstub_read_wait();
  153. /* If we get an ACK, we are done. */
  154. if (ch == '+')
  155. return;
  156. /*
  157. * If we get the start of another packet, this means
  158. * that GDB is attempting to reconnect. We will NAK
  159. * the packet being sent, and stop trying to send this
  160. * packet.
  161. */
  162. if (ch == '$') {
  163. dbg_io_ops->write_char('-');
  164. if (dbg_io_ops->flush)
  165. dbg_io_ops->flush();
  166. return;
  167. }
  168. }
  169. }
  170. static char gdbmsgbuf[BUFMAX + 1];
  171. void gdbstub_msg_write(const char *s, int len)
  172. {
  173. char *bufptr;
  174. int wcount;
  175. int i;
  176. if (len == 0)
  177. len = strlen(s);
  178. /* 'O'utput */
  179. gdbmsgbuf[0] = 'O';
  180. /* Fill and send buffers... */
  181. while (len > 0) {
  182. bufptr = gdbmsgbuf + 1;
  183. /* Calculate how many this time */
  184. if ((len << 1) > (BUFMAX - 2))
  185. wcount = (BUFMAX - 2) >> 1;
  186. else
  187. wcount = len;
  188. /* Pack in hex chars */
  189. for (i = 0; i < wcount; i++)
  190. bufptr = hex_byte_pack(bufptr, s[i]);
  191. *bufptr = '\0';
  192. /* Move up */
  193. s += wcount;
  194. len -= wcount;
  195. /* Write packet */
  196. put_packet(gdbmsgbuf);
  197. }
  198. }
  199. /*
  200. * Convert the memory pointed to by mem into hex, placing result in
  201. * buf. Return a pointer to the last char put in buf (null). May
  202. * return an error.
  203. */
  204. char *kgdb_mem2hex(char *mem, char *buf, int count)
  205. {
  206. char *tmp;
  207. int err;
  208. /*
  209. * We use the upper half of buf as an intermediate buffer for the
  210. * raw memory copy. Hex conversion will work against this one.
  211. */
  212. tmp = buf + count;
  213. err = copy_from_kernel_nofault(tmp, mem, count);
  214. if (err)
  215. return NULL;
  216. while (count > 0) {
  217. buf = hex_byte_pack(buf, *tmp);
  218. tmp++;
  219. count--;
  220. }
  221. *buf = 0;
  222. return buf;
  223. }
  224. /*
  225. * Convert the hex array pointed to by buf into binary to be placed in
  226. * mem. Return a pointer to the character AFTER the last byte
  227. * written. May return an error.
  228. */
  229. int kgdb_hex2mem(char *buf, char *mem, int count)
  230. {
  231. char *tmp_raw;
  232. char *tmp_hex;
  233. /*
  234. * We use the upper half of buf as an intermediate buffer for the
  235. * raw memory that is converted from hex.
  236. */
  237. tmp_raw = buf + count * 2;
  238. tmp_hex = tmp_raw - 1;
  239. while (tmp_hex >= buf) {
  240. tmp_raw--;
  241. *tmp_raw = hex_to_bin(*tmp_hex--);
  242. *tmp_raw |= hex_to_bin(*tmp_hex--) << 4;
  243. }
  244. return copy_to_kernel_nofault(mem, tmp_raw, count);
  245. }
  246. /*
  247. * While we find nice hex chars, build a long_val.
  248. * Return number of chars processed.
  249. */
  250. int kgdb_hex2long(char **ptr, unsigned long *long_val)
  251. {
  252. int hex_val;
  253. int num = 0;
  254. int negate = 0;
  255. *long_val = 0;
  256. if (**ptr == '-') {
  257. negate = 1;
  258. (*ptr)++;
  259. }
  260. while (**ptr) {
  261. hex_val = hex_to_bin(**ptr);
  262. if (hex_val < 0)
  263. break;
  264. *long_val = (*long_val << 4) | hex_val;
  265. num++;
  266. (*ptr)++;
  267. }
  268. if (negate)
  269. *long_val = -*long_val;
  270. return num;
  271. }
  272. /*
  273. * Copy the binary array pointed to by buf into mem. Fix $, #, and
  274. * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
  275. * The input buf is overwritten with the result to write to mem.
  276. */
  277. static int kgdb_ebin2mem(char *buf, char *mem, int count)
  278. {
  279. int size = 0;
  280. char *c = buf;
  281. while (count-- > 0) {
  282. c[size] = *buf++;
  283. if (c[size] == 0x7d)
  284. c[size] = *buf++ ^ 0x20;
  285. size++;
  286. }
  287. return copy_to_kernel_nofault(mem, c, size);
  288. }
  289. #if DBG_MAX_REG_NUM > 0
  290. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  291. {
  292. int i;
  293. int idx = 0;
  294. char *ptr = (char *)gdb_regs;
  295. for (i = 0; i < DBG_MAX_REG_NUM; i++) {
  296. dbg_get_reg(i, ptr + idx, regs);
  297. idx += dbg_reg_def[i].size;
  298. }
  299. }
  300. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  301. {
  302. int i;
  303. int idx = 0;
  304. char *ptr = (char *)gdb_regs;
  305. for (i = 0; i < DBG_MAX_REG_NUM; i++) {
  306. dbg_set_reg(i, ptr + idx, regs);
  307. idx += dbg_reg_def[i].size;
  308. }
  309. }
  310. #endif /* DBG_MAX_REG_NUM > 0 */
  311. /* Write memory due to an 'M' or 'X' packet. */
  312. static int write_mem_msg(int binary)
  313. {
  314. char *ptr = &remcom_in_buffer[1];
  315. unsigned long addr;
  316. unsigned long length;
  317. int err;
  318. if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
  319. kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
  320. if (binary)
  321. err = kgdb_ebin2mem(ptr, (char *)addr, length);
  322. else
  323. err = kgdb_hex2mem(ptr, (char *)addr, length);
  324. if (err)
  325. return err;
  326. if (CACHE_FLUSH_IS_SAFE)
  327. flush_icache_range(addr, addr + length);
  328. return 0;
  329. }
  330. return -EINVAL;
  331. }
  332. static void error_packet(char *pkt, int error)
  333. {
  334. error = -error;
  335. pkt[0] = 'E';
  336. pkt[1] = hex_asc[(error / 10)];
  337. pkt[2] = hex_asc[(error % 10)];
  338. pkt[3] = '\0';
  339. }
  340. /*
  341. * Thread ID accessors. We represent a flat TID space to GDB, where
  342. * the per CPU idle threads (which under Linux all have PID 0) are
  343. * remapped to negative TIDs.
  344. */
  345. #define BUF_THREAD_ID_SIZE 8
  346. static char *pack_threadid(char *pkt, unsigned char *id)
  347. {
  348. unsigned char *limit;
  349. int lzero = 1;
  350. limit = id + (BUF_THREAD_ID_SIZE / 2);
  351. while (id < limit) {
  352. if (!lzero || *id != 0) {
  353. pkt = hex_byte_pack(pkt, *id);
  354. lzero = 0;
  355. }
  356. id++;
  357. }
  358. if (lzero)
  359. pkt = hex_byte_pack(pkt, 0);
  360. return pkt;
  361. }
  362. static void int_to_threadref(unsigned char *id, int value)
  363. {
  364. put_unaligned_be32(value, id);
  365. }
  366. static struct task_struct *getthread(struct pt_regs *regs, int tid)
  367. {
  368. /*
  369. * Non-positive TIDs are remapped to the cpu shadow information
  370. */
  371. if (tid == 0 || tid == -1)
  372. tid = -atomic_read(&kgdb_active) - 2;
  373. if (tid < -1 && tid > -NR_CPUS - 2) {
  374. if (kgdb_info[-tid - 2].task)
  375. return kgdb_info[-tid - 2].task;
  376. else
  377. return idle_task(-tid - 2);
  378. }
  379. if (tid <= 0) {
  380. printk(KERN_ERR "KGDB: Internal thread select error\n");
  381. dump_stack();
  382. return NULL;
  383. }
  384. /*
  385. * find_task_by_pid_ns() does not take the tasklist lock anymore
  386. * but is nicely RCU locked - hence is a pretty resilient
  387. * thing to use:
  388. */
  389. return find_task_by_pid_ns(tid, &init_pid_ns);
  390. }
  391. /*
  392. * Remap normal tasks to their real PID,
  393. * CPU shadow threads are mapped to -CPU - 2
  394. */
  395. static inline int shadow_pid(int realpid)
  396. {
  397. if (realpid)
  398. return realpid;
  399. return -raw_smp_processor_id() - 2;
  400. }
  401. /*
  402. * All the functions that start with gdb_cmd are the various
  403. * operations to implement the handlers for the gdbserial protocol
  404. * where KGDB is communicating with an external debugger
  405. */
  406. /* Handle the '?' status packets */
  407. static void gdb_cmd_status(struct kgdb_state *ks)
  408. {
  409. /*
  410. * We know that this packet is only sent
  411. * during initial connect. So to be safe,
  412. * we clear out our breakpoints now in case
  413. * GDB is reconnecting.
  414. */
  415. dbg_remove_all_break();
  416. remcom_out_buffer[0] = 'S';
  417. hex_byte_pack(&remcom_out_buffer[1], ks->signo);
  418. }
  419. static void gdb_get_regs_helper(struct kgdb_state *ks)
  420. {
  421. struct task_struct *thread;
  422. void *local_debuggerinfo;
  423. int i;
  424. thread = kgdb_usethread;
  425. if (!thread) {
  426. thread = kgdb_info[ks->cpu].task;
  427. local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
  428. } else {
  429. local_debuggerinfo = NULL;
  430. for_each_online_cpu(i) {
  431. /*
  432. * Try to find the task on some other
  433. * or possibly this node if we do not
  434. * find the matching task then we try
  435. * to approximate the results.
  436. */
  437. if (thread == kgdb_info[i].task)
  438. local_debuggerinfo = kgdb_info[i].debuggerinfo;
  439. }
  440. }
  441. /*
  442. * All threads that don't have debuggerinfo should be
  443. * in schedule() sleeping, since all other CPUs
  444. * are in kgdb_wait, and thus have debuggerinfo.
  445. */
  446. if (local_debuggerinfo) {
  447. pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
  448. } else {
  449. /*
  450. * Pull stuff saved during switch_to; nothing
  451. * else is accessible (or even particularly
  452. * relevant).
  453. *
  454. * This should be enough for a stack trace.
  455. */
  456. sleeping_thread_to_gdb_regs(gdb_regs, thread);
  457. }
  458. }
  459. /* Handle the 'g' get registers request */
  460. static void gdb_cmd_getregs(struct kgdb_state *ks)
  461. {
  462. gdb_get_regs_helper(ks);
  463. kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
  464. }
  465. /* Handle the 'G' set registers request */
  466. static void gdb_cmd_setregs(struct kgdb_state *ks)
  467. {
  468. kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
  469. if (kgdb_usethread && kgdb_usethread != current) {
  470. error_packet(remcom_out_buffer, -EINVAL);
  471. } else {
  472. gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
  473. strcpy(remcom_out_buffer, "OK");
  474. }
  475. }
  476. /* Handle the 'm' memory read bytes */
  477. static void gdb_cmd_memread(struct kgdb_state *ks)
  478. {
  479. char *ptr = &remcom_in_buffer[1];
  480. unsigned long length;
  481. unsigned long addr;
  482. char *err;
  483. if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
  484. kgdb_hex2long(&ptr, &length) > 0) {
  485. err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
  486. if (!err)
  487. error_packet(remcom_out_buffer, -EINVAL);
  488. } else {
  489. error_packet(remcom_out_buffer, -EINVAL);
  490. }
  491. }
  492. /* Handle the 'M' memory write bytes */
  493. static void gdb_cmd_memwrite(struct kgdb_state *ks)
  494. {
  495. int err = write_mem_msg(0);
  496. if (err)
  497. error_packet(remcom_out_buffer, err);
  498. else
  499. strcpy(remcom_out_buffer, "OK");
  500. }
  501. #if DBG_MAX_REG_NUM > 0
  502. static char *gdb_hex_reg_helper(int regnum, char *out)
  503. {
  504. int i;
  505. int offset = 0;
  506. for (i = 0; i < regnum; i++)
  507. offset += dbg_reg_def[i].size;
  508. return kgdb_mem2hex((char *)gdb_regs + offset, out,
  509. dbg_reg_def[i].size);
  510. }
  511. /* Handle the 'p' individual register get */
  512. static void gdb_cmd_reg_get(struct kgdb_state *ks)
  513. {
  514. unsigned long regnum;
  515. char *ptr = &remcom_in_buffer[1];
  516. kgdb_hex2long(&ptr, &regnum);
  517. if (regnum >= DBG_MAX_REG_NUM) {
  518. error_packet(remcom_out_buffer, -EINVAL);
  519. return;
  520. }
  521. gdb_get_regs_helper(ks);
  522. gdb_hex_reg_helper(regnum, remcom_out_buffer);
  523. }
  524. /* Handle the 'P' individual register set */
  525. static void gdb_cmd_reg_set(struct kgdb_state *ks)
  526. {
  527. unsigned long regnum;
  528. char *ptr = &remcom_in_buffer[1];
  529. int i = 0;
  530. kgdb_hex2long(&ptr, &regnum);
  531. if (*ptr++ != '=' ||
  532. !(!kgdb_usethread || kgdb_usethread == current) ||
  533. !dbg_get_reg(regnum, gdb_regs, ks->linux_regs)) {
  534. error_packet(remcom_out_buffer, -EINVAL);
  535. return;
  536. }
  537. memset(gdb_regs, 0, sizeof(gdb_regs));
  538. while (i < sizeof(gdb_regs) * 2)
  539. if (hex_to_bin(ptr[i]) >= 0)
  540. i++;
  541. else
  542. break;
  543. i = i / 2;
  544. kgdb_hex2mem(ptr, (char *)gdb_regs, i);
  545. dbg_set_reg(regnum, gdb_regs, ks->linux_regs);
  546. strcpy(remcom_out_buffer, "OK");
  547. }
  548. #endif /* DBG_MAX_REG_NUM > 0 */
  549. /* Handle the 'X' memory binary write bytes */
  550. static void gdb_cmd_binwrite(struct kgdb_state *ks)
  551. {
  552. int err = write_mem_msg(1);
  553. if (err)
  554. error_packet(remcom_out_buffer, err);
  555. else
  556. strcpy(remcom_out_buffer, "OK");
  557. }
  558. /* Handle the 'D' or 'k', detach or kill packets */
  559. static void gdb_cmd_detachkill(struct kgdb_state *ks)
  560. {
  561. int error;
  562. /* The detach case */
  563. if (remcom_in_buffer[0] == 'D') {
  564. error = dbg_remove_all_break();
  565. if (error < 0) {
  566. error_packet(remcom_out_buffer, error);
  567. } else {
  568. strcpy(remcom_out_buffer, "OK");
  569. kgdb_connected = 0;
  570. }
  571. put_packet(remcom_out_buffer);
  572. } else {
  573. /*
  574. * Assume the kill case, with no exit code checking,
  575. * trying to force detach the debugger:
  576. */
  577. dbg_remove_all_break();
  578. kgdb_connected = 0;
  579. }
  580. }
  581. /* Handle the 'R' reboot packets */
  582. static int gdb_cmd_reboot(struct kgdb_state *ks)
  583. {
  584. /* For now, only honor R0 */
  585. if (strcmp(remcom_in_buffer, "R0") == 0) {
  586. printk(KERN_CRIT "Executing emergency reboot\n");
  587. strcpy(remcom_out_buffer, "OK");
  588. put_packet(remcom_out_buffer);
  589. /*
  590. * Execution should not return from
  591. * machine_emergency_restart()
  592. */
  593. machine_emergency_restart();
  594. kgdb_connected = 0;
  595. return 1;
  596. }
  597. return 0;
  598. }
  599. /* Handle the 'q' query packets */
  600. static void gdb_cmd_query(struct kgdb_state *ks)
  601. {
  602. struct task_struct *g;
  603. struct task_struct *p;
  604. unsigned char thref[BUF_THREAD_ID_SIZE];
  605. char *ptr;
  606. int i;
  607. int cpu;
  608. int finished = 0;
  609. switch (remcom_in_buffer[1]) {
  610. case 's':
  611. case 'f':
  612. if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10))
  613. break;
  614. i = 0;
  615. remcom_out_buffer[0] = 'm';
  616. ptr = remcom_out_buffer + 1;
  617. if (remcom_in_buffer[1] == 'f') {
  618. /* Each cpu is a shadow thread */
  619. for_each_online_cpu(cpu) {
  620. ks->thr_query = 0;
  621. int_to_threadref(thref, -cpu - 2);
  622. ptr = pack_threadid(ptr, thref);
  623. *(ptr++) = ',';
  624. i++;
  625. }
  626. }
  627. for_each_process_thread(g, p) {
  628. if (i >= ks->thr_query && !finished) {
  629. int_to_threadref(thref, p->pid);
  630. ptr = pack_threadid(ptr, thref);
  631. *(ptr++) = ',';
  632. ks->thr_query++;
  633. if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
  634. finished = 1;
  635. }
  636. i++;
  637. }
  638. *(--ptr) = '\0';
  639. break;
  640. case 'C':
  641. /* Current thread id */
  642. strcpy(remcom_out_buffer, "QC");
  643. ks->threadid = shadow_pid(current->pid);
  644. int_to_threadref(thref, ks->threadid);
  645. pack_threadid(remcom_out_buffer + 2, thref);
  646. break;
  647. case 'T':
  648. if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16))
  649. break;
  650. ks->threadid = 0;
  651. ptr = remcom_in_buffer + 17;
  652. kgdb_hex2long(&ptr, &ks->threadid);
  653. if (!getthread(ks->linux_regs, ks->threadid)) {
  654. error_packet(remcom_out_buffer, -EINVAL);
  655. break;
  656. }
  657. if ((int)ks->threadid > 0) {
  658. kgdb_mem2hex(getthread(ks->linux_regs,
  659. ks->threadid)->comm,
  660. remcom_out_buffer, 16);
  661. } else {
  662. static char tmpstr[23 + BUF_THREAD_ID_SIZE];
  663. sprintf(tmpstr, "shadowCPU%d",
  664. (int)(-ks->threadid - 2));
  665. kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
  666. }
  667. break;
  668. #ifdef CONFIG_KGDB_KDB
  669. case 'R':
  670. if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
  671. int len = strlen(remcom_in_buffer + 6);
  672. if ((len % 2) != 0) {
  673. strcpy(remcom_out_buffer, "E01");
  674. break;
  675. }
  676. kgdb_hex2mem(remcom_in_buffer + 6,
  677. remcom_out_buffer, len);
  678. len = len / 2;
  679. remcom_out_buffer[len++] = 0;
  680. kdb_common_init_state(ks);
  681. kdb_parse(remcom_out_buffer);
  682. kdb_common_deinit_state();
  683. strcpy(remcom_out_buffer, "OK");
  684. }
  685. break;
  686. #endif
  687. #ifdef CONFIG_HAVE_ARCH_KGDB_QXFER_PKT
  688. case 'S':
  689. if (!strncmp(remcom_in_buffer, "qSupported:", 11))
  690. strcpy(remcom_out_buffer, kgdb_arch_gdb_stub_feature);
  691. break;
  692. case 'X':
  693. if (!strncmp(remcom_in_buffer, "qXfer:", 6))
  694. kgdb_arch_handle_qxfer_pkt(remcom_in_buffer,
  695. remcom_out_buffer);
  696. break;
  697. #endif
  698. default:
  699. break;
  700. }
  701. }
  702. /* Handle the 'H' task query packets */
  703. static void gdb_cmd_task(struct kgdb_state *ks)
  704. {
  705. struct task_struct *thread;
  706. char *ptr;
  707. switch (remcom_in_buffer[1]) {
  708. case 'g':
  709. ptr = &remcom_in_buffer[2];
  710. kgdb_hex2long(&ptr, &ks->threadid);
  711. thread = getthread(ks->linux_regs, ks->threadid);
  712. if (!thread && ks->threadid > 0) {
  713. error_packet(remcom_out_buffer, -EINVAL);
  714. break;
  715. }
  716. kgdb_usethread = thread;
  717. ks->kgdb_usethreadid = ks->threadid;
  718. strcpy(remcom_out_buffer, "OK");
  719. break;
  720. case 'c':
  721. ptr = &remcom_in_buffer[2];
  722. kgdb_hex2long(&ptr, &ks->threadid);
  723. if (!ks->threadid) {
  724. kgdb_contthread = NULL;
  725. } else {
  726. thread = getthread(ks->linux_regs, ks->threadid);
  727. if (!thread && ks->threadid > 0) {
  728. error_packet(remcom_out_buffer, -EINVAL);
  729. break;
  730. }
  731. kgdb_contthread = thread;
  732. }
  733. strcpy(remcom_out_buffer, "OK");
  734. break;
  735. }
  736. }
  737. /* Handle the 'T' thread query packets */
  738. static void gdb_cmd_thread(struct kgdb_state *ks)
  739. {
  740. char *ptr = &remcom_in_buffer[1];
  741. struct task_struct *thread;
  742. kgdb_hex2long(&ptr, &ks->threadid);
  743. thread = getthread(ks->linux_regs, ks->threadid);
  744. if (thread)
  745. strcpy(remcom_out_buffer, "OK");
  746. else
  747. error_packet(remcom_out_buffer, -EINVAL);
  748. }
  749. /* Handle the 'z' or 'Z' breakpoint remove or set packets */
  750. static void gdb_cmd_break(struct kgdb_state *ks)
  751. {
  752. /*
  753. * Since GDB-5.3, it's been drafted that '0' is a software
  754. * breakpoint, '1' is a hardware breakpoint, so let's do that.
  755. */
  756. char *bpt_type = &remcom_in_buffer[1];
  757. char *ptr = &remcom_in_buffer[2];
  758. unsigned long addr;
  759. unsigned long length;
  760. int error = 0;
  761. if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
  762. /* Unsupported */
  763. if (*bpt_type > '4')
  764. return;
  765. } else {
  766. if (*bpt_type != '0' && *bpt_type != '1')
  767. /* Unsupported. */
  768. return;
  769. }
  770. /*
  771. * Test if this is a hardware breakpoint, and
  772. * if we support it:
  773. */
  774. if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
  775. /* Unsupported. */
  776. return;
  777. if (*(ptr++) != ',') {
  778. error_packet(remcom_out_buffer, -EINVAL);
  779. return;
  780. }
  781. if (!kgdb_hex2long(&ptr, &addr)) {
  782. error_packet(remcom_out_buffer, -EINVAL);
  783. return;
  784. }
  785. if (*(ptr++) != ',' ||
  786. !kgdb_hex2long(&ptr, &length)) {
  787. error_packet(remcom_out_buffer, -EINVAL);
  788. return;
  789. }
  790. if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
  791. error = dbg_set_sw_break(addr);
  792. else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
  793. error = dbg_remove_sw_break(addr);
  794. else if (remcom_in_buffer[0] == 'Z')
  795. error = arch_kgdb_ops.set_hw_breakpoint(addr,
  796. (int)length, *bpt_type - '0');
  797. else if (remcom_in_buffer[0] == 'z')
  798. error = arch_kgdb_ops.remove_hw_breakpoint(addr,
  799. (int) length, *bpt_type - '0');
  800. if (error == 0)
  801. strcpy(remcom_out_buffer, "OK");
  802. else
  803. error_packet(remcom_out_buffer, error);
  804. }
  805. /* Handle the 'C' signal / exception passing packets */
  806. static int gdb_cmd_exception_pass(struct kgdb_state *ks)
  807. {
  808. /* C09 == pass exception
  809. * C15 == detach kgdb, pass exception
  810. */
  811. if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
  812. ks->pass_exception = 1;
  813. remcom_in_buffer[0] = 'c';
  814. } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
  815. ks->pass_exception = 1;
  816. remcom_in_buffer[0] = 'D';
  817. dbg_remove_all_break();
  818. kgdb_connected = 0;
  819. return 1;
  820. } else {
  821. gdbstub_msg_write("KGDB only knows signal 9 (pass)"
  822. " and 15 (pass and disconnect)\n"
  823. "Executing a continue without signal passing\n", 0);
  824. remcom_in_buffer[0] = 'c';
  825. }
  826. /* Indicate fall through */
  827. return -1;
  828. }
  829. /*
  830. * This function performs all gdbserial command processing
  831. */
  832. int gdb_serial_stub(struct kgdb_state *ks)
  833. {
  834. int error = 0;
  835. int tmp;
  836. /* Initialize comm buffer and globals. */
  837. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  838. kgdb_usethread = kgdb_info[ks->cpu].task;
  839. ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
  840. ks->pass_exception = 0;
  841. if (kgdb_connected) {
  842. unsigned char thref[BUF_THREAD_ID_SIZE];
  843. char *ptr;
  844. /* Reply to host that an exception has occurred */
  845. ptr = remcom_out_buffer;
  846. *ptr++ = 'T';
  847. ptr = hex_byte_pack(ptr, ks->signo);
  848. ptr += strlen(strcpy(ptr, "thread:"));
  849. int_to_threadref(thref, shadow_pid(current->pid));
  850. ptr = pack_threadid(ptr, thref);
  851. *ptr++ = ';';
  852. put_packet(remcom_out_buffer);
  853. }
  854. while (1) {
  855. error = 0;
  856. /* Clear the out buffer. */
  857. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  858. get_packet(remcom_in_buffer);
  859. switch (remcom_in_buffer[0]) {
  860. case '?': /* gdbserial status */
  861. gdb_cmd_status(ks);
  862. break;
  863. case 'g': /* return the value of the CPU registers */
  864. gdb_cmd_getregs(ks);
  865. break;
  866. case 'G': /* set the value of the CPU registers - return OK */
  867. gdb_cmd_setregs(ks);
  868. break;
  869. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  870. gdb_cmd_memread(ks);
  871. break;
  872. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  873. gdb_cmd_memwrite(ks);
  874. break;
  875. #if DBG_MAX_REG_NUM > 0
  876. case 'p': /* pXX Return gdb register XX (in hex) */
  877. gdb_cmd_reg_get(ks);
  878. break;
  879. case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */
  880. gdb_cmd_reg_set(ks);
  881. break;
  882. #endif /* DBG_MAX_REG_NUM > 0 */
  883. case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  884. gdb_cmd_binwrite(ks);
  885. break;
  886. /* kill or detach. KGDB should treat this like a
  887. * continue.
  888. */
  889. case 'D': /* Debugger detach */
  890. case 'k': /* Debugger detach via kill */
  891. gdb_cmd_detachkill(ks);
  892. goto default_handle;
  893. case 'R': /* Reboot */
  894. if (gdb_cmd_reboot(ks))
  895. goto default_handle;
  896. break;
  897. case 'q': /* query command */
  898. gdb_cmd_query(ks);
  899. break;
  900. case 'H': /* task related */
  901. gdb_cmd_task(ks);
  902. break;
  903. case 'T': /* Query thread status */
  904. gdb_cmd_thread(ks);
  905. break;
  906. case 'z': /* Break point remove */
  907. case 'Z': /* Break point set */
  908. gdb_cmd_break(ks);
  909. break;
  910. #ifdef CONFIG_KGDB_KDB
  911. case '3': /* Escape into back into kdb */
  912. if (remcom_in_buffer[1] == '\0') {
  913. gdb_cmd_detachkill(ks);
  914. return DBG_PASS_EVENT;
  915. }
  916. fallthrough;
  917. #endif
  918. case 'C': /* Exception passing */
  919. tmp = gdb_cmd_exception_pass(ks);
  920. if (tmp > 0)
  921. goto default_handle;
  922. if (tmp == 0)
  923. break;
  924. fallthrough; /* on tmp < 0 */
  925. case 'c': /* Continue packet */
  926. case 's': /* Single step packet */
  927. if (kgdb_contthread && kgdb_contthread != current) {
  928. /* Can't switch threads in kgdb */
  929. error_packet(remcom_out_buffer, -EINVAL);
  930. break;
  931. }
  932. fallthrough; /* to default processing */
  933. default:
  934. default_handle:
  935. error = kgdb_arch_handle_exception(ks->ex_vector,
  936. ks->signo,
  937. ks->err_code,
  938. remcom_in_buffer,
  939. remcom_out_buffer,
  940. ks->linux_regs);
  941. /*
  942. * Leave cmd processing on error, detach,
  943. * kill, continue, or single step.
  944. */
  945. if (error >= 0 || remcom_in_buffer[0] == 'D' ||
  946. remcom_in_buffer[0] == 'k') {
  947. error = 0;
  948. goto kgdb_exit;
  949. }
  950. }
  951. /* reply to the request */
  952. put_packet(remcom_out_buffer);
  953. }
  954. kgdb_exit:
  955. if (ks->pass_exception)
  956. error = 1;
  957. return error;
  958. }
  959. int gdbstub_state(struct kgdb_state *ks, char *cmd)
  960. {
  961. int error;
  962. switch (cmd[0]) {
  963. case 'e':
  964. error = kgdb_arch_handle_exception(ks->ex_vector,
  965. ks->signo,
  966. ks->err_code,
  967. remcom_in_buffer,
  968. remcom_out_buffer,
  969. ks->linux_regs);
  970. return error;
  971. case 's':
  972. case 'c':
  973. strscpy(remcom_in_buffer, cmd, sizeof(remcom_in_buffer));
  974. return 0;
  975. case '$':
  976. strscpy(remcom_in_buffer, cmd, sizeof(remcom_in_buffer));
  977. gdbstub_use_prev_in_buf = strlen(remcom_in_buffer);
  978. gdbstub_prev_in_buf_pos = 0;
  979. return 0;
  980. }
  981. dbg_io_ops->write_char('+');
  982. put_packet(remcom_out_buffer);
  983. return 0;
  984. }
  985. /**
  986. * gdbstub_exit - Send an exit message to GDB
  987. * @status: The exit code to report.
  988. */
  989. void gdbstub_exit(int status)
  990. {
  991. unsigned char checksum, ch, buffer[3];
  992. int loop;
  993. if (!kgdb_connected)
  994. return;
  995. kgdb_connected = 0;
  996. if (!dbg_io_ops || dbg_kdb_mode)
  997. return;
  998. buffer[0] = 'W';
  999. buffer[1] = hex_asc_hi(status);
  1000. buffer[2] = hex_asc_lo(status);
  1001. dbg_io_ops->write_char('$');
  1002. checksum = 0;
  1003. for (loop = 0; loop < 3; loop++) {
  1004. ch = buffer[loop];
  1005. checksum += ch;
  1006. dbg_io_ops->write_char(ch);
  1007. }
  1008. dbg_io_ops->write_char('#');
  1009. dbg_io_ops->write_char(hex_asc_hi(checksum));
  1010. dbg_io_ops->write_char(hex_asc_lo(checksum));
  1011. /* make sure the output is flushed, lest the bootloader clobber it */
  1012. if (dbg_io_ops->flush)
  1013. dbg_io_ops->flush();
  1014. }