kgdb.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. /*
  2. * KGDB stub.
  3. *
  4. * Maintainer: Jason Wessel <jason.wessel@windriver.com>
  5. *
  6. * Copyright (C) 2000-2001 VERITAS Software Corporation.
  7. * Copyright (C) 2002-2004 Timesys Corporation
  8. * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
  9. * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
  10. * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
  11. * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
  12. * Copyright (C) 2005-2008 Wind River Systems, Inc.
  13. * Copyright (C) 2007 MontaVista Software, Inc.
  14. * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  15. *
  16. * Contributors at various stages not listed above:
  17. * Jason Wessel ( jason.wessel@windriver.com )
  18. * George Anzinger <george@mvista.com>
  19. * Anurekh Saxena (anurekh.saxena@timesys.com)
  20. * Lake Stevens Instrument Division (Glenn Engel)
  21. * Jim Kingdon, Cygnus Support.
  22. *
  23. * Original KGDB stub: David Grothe <dave@gcom.com>,
  24. * Tigran Aivazian <tigran@sco.com>
  25. *
  26. * This file is licensed under the terms of the GNU General Public License
  27. * version 2. This program is licensed "as is" without any warranty of any
  28. * kind, whether express or implied.
  29. */
  30. #include <linux/pid_namespace.h>
  31. #include <linux/clocksource.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/console.h>
  35. #include <linux/threads.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/reboot.h>
  41. #include <linux/string.h>
  42. #include <linux/delay.h>
  43. #include <linux/sched.h>
  44. #include <linux/sysrq.h>
  45. #include <linux/init.h>
  46. #include <linux/kgdb.h>
  47. #include <linux/pid.h>
  48. #include <linux/smp.h>
  49. #include <linux/mm.h>
  50. #include <asm/cacheflush.h>
  51. #include <asm/byteorder.h>
  52. #include <asm/atomic.h>
  53. #include <asm/system.h>
  54. #include <asm/unaligned.h>
  55. static int kgdb_break_asap;
  56. #define KGDB_MAX_THREAD_QUERY 17
  57. struct kgdb_state {
  58. int ex_vector;
  59. int signo;
  60. int err_code;
  61. int cpu;
  62. int pass_exception;
  63. unsigned long thr_query;
  64. unsigned long threadid;
  65. long kgdb_usethreadid;
  66. struct pt_regs *linux_regs;
  67. };
  68. static struct debuggerinfo_struct {
  69. void *debuggerinfo;
  70. struct task_struct *task;
  71. } kgdb_info[NR_CPUS];
  72. /**
  73. * kgdb_connected - Is a host GDB connected to us?
  74. */
  75. int kgdb_connected;
  76. EXPORT_SYMBOL_GPL(kgdb_connected);
  77. /* All the KGDB handlers are installed */
  78. static int kgdb_io_module_registered;
  79. /* Guard for recursive entry */
  80. static int exception_level;
  81. static struct kgdb_io *kgdb_io_ops;
  82. static DEFINE_SPINLOCK(kgdb_registration_lock);
  83. /* kgdb console driver is loaded */
  84. static int kgdb_con_registered;
  85. /* determine if kgdb console output should be used */
  86. static int kgdb_use_con;
  87. static int __init opt_kgdb_con(char *str)
  88. {
  89. kgdb_use_con = 1;
  90. return 0;
  91. }
  92. early_param("kgdbcon", opt_kgdb_con);
  93. module_param(kgdb_use_con, int, 0644);
  94. /*
  95. * Holds information about breakpoints in a kernel. These breakpoints are
  96. * added and removed by gdb.
  97. */
  98. static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
  99. [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
  100. };
  101. /*
  102. * The CPU# of the active CPU, or -1 if none:
  103. */
  104. atomic_t kgdb_active = ATOMIC_INIT(-1);
  105. /*
  106. * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
  107. * bootup code (which might not have percpu set up yet):
  108. */
  109. static atomic_t passive_cpu_wait[NR_CPUS];
  110. static atomic_t cpu_in_kgdb[NR_CPUS];
  111. atomic_t kgdb_setting_breakpoint;
  112. struct task_struct *kgdb_usethread;
  113. struct task_struct *kgdb_contthread;
  114. int kgdb_single_step;
  115. pid_t kgdb_sstep_pid;
  116. /* Our I/O buffers. */
  117. static char remcom_in_buffer[BUFMAX];
  118. static char remcom_out_buffer[BUFMAX];
  119. /* Storage for the registers, in GDB format. */
  120. static unsigned long gdb_regs[(NUMREGBYTES +
  121. sizeof(unsigned long) - 1) /
  122. sizeof(unsigned long)];
  123. /* to keep track of the CPU which is doing the single stepping*/
  124. atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
  125. /*
  126. * If you are debugging a problem where roundup (the collection of
  127. * all other CPUs) is a problem [this should be extremely rare],
  128. * then use the nokgdbroundup option to avoid roundup. In that case
  129. * the other CPUs might interfere with your debugging context, so
  130. * use this with care:
  131. */
  132. static int kgdb_do_roundup = 1;
  133. static int __init opt_nokgdbroundup(char *str)
  134. {
  135. kgdb_do_roundup = 0;
  136. return 0;
  137. }
  138. early_param("nokgdbroundup", opt_nokgdbroundup);
  139. /*
  140. * Finally, some KGDB code :-)
  141. */
  142. /*
  143. * Weak aliases for breakpoint management,
  144. * can be overriden by architectures when needed:
  145. */
  146. int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
  147. {
  148. int err;
  149. err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE);
  150. if (err)
  151. return err;
  152. return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
  153. BREAK_INSTR_SIZE);
  154. }
  155. int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
  156. {
  157. return probe_kernel_write((char *)addr,
  158. (char *)bundle, BREAK_INSTR_SIZE);
  159. }
  160. int __weak kgdb_validate_break_address(unsigned long addr)
  161. {
  162. char tmp_variable[BREAK_INSTR_SIZE];
  163. int err;
  164. /* Validate setting the breakpoint and then removing it. In the
  165. * remove fails, the kernel needs to emit a bad message because we
  166. * are deep trouble not being able to put things back the way we
  167. * found them.
  168. */
  169. err = kgdb_arch_set_breakpoint(addr, tmp_variable);
  170. if (err)
  171. return err;
  172. err = kgdb_arch_remove_breakpoint(addr, tmp_variable);
  173. if (err)
  174. printk(KERN_ERR "KGDB: Critical breakpoint error, kernel "
  175. "memory destroyed at: %lx", addr);
  176. return err;
  177. }
  178. unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
  179. {
  180. return instruction_pointer(regs);
  181. }
  182. int __weak kgdb_arch_init(void)
  183. {
  184. return 0;
  185. }
  186. int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
  187. {
  188. return 0;
  189. }
  190. void __weak
  191. kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
  192. {
  193. return;
  194. }
  195. /**
  196. * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
  197. * @regs: Current &struct pt_regs.
  198. *
  199. * This function will be called if the particular architecture must
  200. * disable hardware debugging while it is processing gdb packets or
  201. * handling exception.
  202. */
  203. void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
  204. {
  205. }
  206. /*
  207. * GDB remote protocol parser:
  208. */
  209. static int hex(char ch)
  210. {
  211. if ((ch >= 'a') && (ch <= 'f'))
  212. return ch - 'a' + 10;
  213. if ((ch >= '0') && (ch <= '9'))
  214. return ch - '0';
  215. if ((ch >= 'A') && (ch <= 'F'))
  216. return ch - 'A' + 10;
  217. return -1;
  218. }
  219. /* scan for the sequence $<data>#<checksum> */
  220. static void get_packet(char *buffer)
  221. {
  222. unsigned char checksum;
  223. unsigned char xmitcsum;
  224. int count;
  225. char ch;
  226. do {
  227. /*
  228. * Spin and wait around for the start character, ignore all
  229. * other characters:
  230. */
  231. while ((ch = (kgdb_io_ops->read_char())) != '$')
  232. /* nothing */;
  233. kgdb_connected = 1;
  234. checksum = 0;
  235. xmitcsum = -1;
  236. count = 0;
  237. /*
  238. * now, read until a # or end of buffer is found:
  239. */
  240. while (count < (BUFMAX - 1)) {
  241. ch = kgdb_io_ops->read_char();
  242. if (ch == '#')
  243. break;
  244. checksum = checksum + ch;
  245. buffer[count] = ch;
  246. count = count + 1;
  247. }
  248. buffer[count] = 0;
  249. if (ch == '#') {
  250. xmitcsum = hex(kgdb_io_ops->read_char()) << 4;
  251. xmitcsum += hex(kgdb_io_ops->read_char());
  252. if (checksum != xmitcsum)
  253. /* failed checksum */
  254. kgdb_io_ops->write_char('-');
  255. else
  256. /* successful transfer */
  257. kgdb_io_ops->write_char('+');
  258. if (kgdb_io_ops->flush)
  259. kgdb_io_ops->flush();
  260. }
  261. } while (checksum != xmitcsum);
  262. }
  263. /*
  264. * Send the packet in buffer.
  265. * Check for gdb connection if asked for.
  266. */
  267. static void put_packet(char *buffer)
  268. {
  269. unsigned char checksum;
  270. int count;
  271. char ch;
  272. /*
  273. * $<packet info>#<checksum>.
  274. */
  275. while (1) {
  276. kgdb_io_ops->write_char('$');
  277. checksum = 0;
  278. count = 0;
  279. while ((ch = buffer[count])) {
  280. kgdb_io_ops->write_char(ch);
  281. checksum += ch;
  282. count++;
  283. }
  284. kgdb_io_ops->write_char('#');
  285. kgdb_io_ops->write_char(hex_asc_hi(checksum));
  286. kgdb_io_ops->write_char(hex_asc_lo(checksum));
  287. if (kgdb_io_ops->flush)
  288. kgdb_io_ops->flush();
  289. /* Now see what we get in reply. */
  290. ch = kgdb_io_ops->read_char();
  291. if (ch == 3)
  292. ch = kgdb_io_ops->read_char();
  293. /* If we get an ACK, we are done. */
  294. if (ch == '+')
  295. return;
  296. /*
  297. * If we get the start of another packet, this means
  298. * that GDB is attempting to reconnect. We will NAK
  299. * the packet being sent, and stop trying to send this
  300. * packet.
  301. */
  302. if (ch == '$') {
  303. kgdb_io_ops->write_char('-');
  304. if (kgdb_io_ops->flush)
  305. kgdb_io_ops->flush();
  306. return;
  307. }
  308. }
  309. }
  310. /*
  311. * Convert the memory pointed to by mem into hex, placing result in buf.
  312. * Return a pointer to the last char put in buf (null). May return an error.
  313. */
  314. int kgdb_mem2hex(char *mem, char *buf, int count)
  315. {
  316. char *tmp;
  317. int err;
  318. /*
  319. * We use the upper half of buf as an intermediate buffer for the
  320. * raw memory copy. Hex conversion will work against this one.
  321. */
  322. tmp = buf + count;
  323. err = probe_kernel_read(tmp, mem, count);
  324. if (!err) {
  325. while (count > 0) {
  326. buf = pack_hex_byte(buf, *tmp);
  327. tmp++;
  328. count--;
  329. }
  330. *buf = 0;
  331. }
  332. return err;
  333. }
  334. /*
  335. * Copy the binary array pointed to by buf into mem. Fix $, #, and
  336. * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
  337. * The input buf is overwitten with the result to write to mem.
  338. */
  339. static int kgdb_ebin2mem(char *buf, char *mem, int count)
  340. {
  341. int size = 0;
  342. char *c = buf;
  343. while (count-- > 0) {
  344. c[size] = *buf++;
  345. if (c[size] == 0x7d)
  346. c[size] = *buf++ ^ 0x20;
  347. size++;
  348. }
  349. return probe_kernel_write(mem, c, size);
  350. }
  351. /*
  352. * Convert the hex array pointed to by buf into binary to be placed in mem.
  353. * Return a pointer to the character AFTER the last byte written.
  354. * May return an error.
  355. */
  356. int kgdb_hex2mem(char *buf, char *mem, int count)
  357. {
  358. char *tmp_raw;
  359. char *tmp_hex;
  360. /*
  361. * We use the upper half of buf as an intermediate buffer for the
  362. * raw memory that is converted from hex.
  363. */
  364. tmp_raw = buf + count * 2;
  365. tmp_hex = tmp_raw - 1;
  366. while (tmp_hex >= buf) {
  367. tmp_raw--;
  368. *tmp_raw = hex(*tmp_hex--);
  369. *tmp_raw |= hex(*tmp_hex--) << 4;
  370. }
  371. return probe_kernel_write(mem, tmp_raw, count);
  372. }
  373. /*
  374. * While we find nice hex chars, build a long_val.
  375. * Return number of chars processed.
  376. */
  377. int kgdb_hex2long(char **ptr, unsigned long *long_val)
  378. {
  379. int hex_val;
  380. int num = 0;
  381. int negate = 0;
  382. *long_val = 0;
  383. if (**ptr == '-') {
  384. negate = 1;
  385. (*ptr)++;
  386. }
  387. while (**ptr) {
  388. hex_val = hex(**ptr);
  389. if (hex_val < 0)
  390. break;
  391. *long_val = (*long_val << 4) | hex_val;
  392. num++;
  393. (*ptr)++;
  394. }
  395. if (negate)
  396. *long_val = -*long_val;
  397. return num;
  398. }
  399. /* Write memory due to an 'M' or 'X' packet. */
  400. static int write_mem_msg(int binary)
  401. {
  402. char *ptr = &remcom_in_buffer[1];
  403. unsigned long addr;
  404. unsigned long length;
  405. int err;
  406. if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
  407. kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
  408. if (binary)
  409. err = kgdb_ebin2mem(ptr, (char *)addr, length);
  410. else
  411. err = kgdb_hex2mem(ptr, (char *)addr, length);
  412. if (err)
  413. return err;
  414. if (CACHE_FLUSH_IS_SAFE)
  415. flush_icache_range(addr, addr + length);
  416. return 0;
  417. }
  418. return -EINVAL;
  419. }
  420. static void error_packet(char *pkt, int error)
  421. {
  422. error = -error;
  423. pkt[0] = 'E';
  424. pkt[1] = hex_asc[(error / 10)];
  425. pkt[2] = hex_asc[(error % 10)];
  426. pkt[3] = '\0';
  427. }
  428. /*
  429. * Thread ID accessors. We represent a flat TID space to GDB, where
  430. * the per CPU idle threads (which under Linux all have PID 0) are
  431. * remapped to negative TIDs.
  432. */
  433. #define BUF_THREAD_ID_SIZE 16
  434. static char *pack_threadid(char *pkt, unsigned char *id)
  435. {
  436. char *limit;
  437. limit = pkt + BUF_THREAD_ID_SIZE;
  438. while (pkt < limit)
  439. pkt = pack_hex_byte(pkt, *id++);
  440. return pkt;
  441. }
  442. static void int_to_threadref(unsigned char *id, int value)
  443. {
  444. unsigned char *scan;
  445. int i = 4;
  446. scan = (unsigned char *)id;
  447. while (i--)
  448. *scan++ = 0;
  449. put_unaligned_be32(value, scan);
  450. }
  451. static struct task_struct *getthread(struct pt_regs *regs, int tid)
  452. {
  453. /*
  454. * Non-positive TIDs are remapped to the cpu shadow information
  455. */
  456. if (tid == 0 || tid == -1)
  457. tid = -atomic_read(&kgdb_active) - 2;
  458. if (tid < -1 && tid > -NR_CPUS - 2) {
  459. if (kgdb_info[-tid - 2].task)
  460. return kgdb_info[-tid - 2].task;
  461. else
  462. return idle_task(-tid - 2);
  463. }
  464. if (tid <= 0) {
  465. printk(KERN_ERR "KGDB: Internal thread select error\n");
  466. dump_stack();
  467. return NULL;
  468. }
  469. /*
  470. * find_task_by_pid_ns() does not take the tasklist lock anymore
  471. * but is nicely RCU locked - hence is a pretty resilient
  472. * thing to use:
  473. */
  474. return find_task_by_pid_ns(tid, &init_pid_ns);
  475. }
  476. /*
  477. * CPU debug state control:
  478. */
  479. #ifdef CONFIG_SMP
  480. static void kgdb_wait(struct pt_regs *regs)
  481. {
  482. unsigned long flags;
  483. int cpu;
  484. local_irq_save(flags);
  485. cpu = raw_smp_processor_id();
  486. kgdb_info[cpu].debuggerinfo = regs;
  487. kgdb_info[cpu].task = current;
  488. /*
  489. * Make sure the above info reaches the primary CPU before
  490. * our cpu_in_kgdb[] flag setting does:
  491. */
  492. smp_wmb();
  493. atomic_set(&cpu_in_kgdb[cpu], 1);
  494. /* Disable any cpu specific hw breakpoints */
  495. kgdb_disable_hw_debug(regs);
  496. /* Wait till primary CPU is done with debugging */
  497. while (atomic_read(&passive_cpu_wait[cpu]))
  498. cpu_relax();
  499. kgdb_info[cpu].debuggerinfo = NULL;
  500. kgdb_info[cpu].task = NULL;
  501. /* fix up hardware debug registers on local cpu */
  502. if (arch_kgdb_ops.correct_hw_break)
  503. arch_kgdb_ops.correct_hw_break();
  504. /* Signal the primary CPU that we are done: */
  505. atomic_set(&cpu_in_kgdb[cpu], 0);
  506. touch_softlockup_watchdog_sync();
  507. clocksource_touch_watchdog();
  508. local_irq_restore(flags);
  509. }
  510. #endif
  511. /*
  512. * Some architectures need cache flushes when we set/clear a
  513. * breakpoint:
  514. */
  515. static void kgdb_flush_swbreak_addr(unsigned long addr)
  516. {
  517. if (!CACHE_FLUSH_IS_SAFE)
  518. return;
  519. if (current->mm && current->mm->mmap_cache) {
  520. flush_cache_range(current->mm->mmap_cache,
  521. addr, addr + BREAK_INSTR_SIZE);
  522. }
  523. /* Force flush instruction cache if it was outside the mm */
  524. flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
  525. }
  526. /*
  527. * SW breakpoint management:
  528. */
  529. static int kgdb_activate_sw_breakpoints(void)
  530. {
  531. unsigned long addr;
  532. int error;
  533. int ret = 0;
  534. int i;
  535. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  536. if (kgdb_break[i].state != BP_SET)
  537. continue;
  538. addr = kgdb_break[i].bpt_addr;
  539. error = kgdb_arch_set_breakpoint(addr,
  540. kgdb_break[i].saved_instr);
  541. if (error) {
  542. ret = error;
  543. printk(KERN_INFO "KGDB: BP install failed: %lx", addr);
  544. continue;
  545. }
  546. kgdb_flush_swbreak_addr(addr);
  547. kgdb_break[i].state = BP_ACTIVE;
  548. }
  549. return ret;
  550. }
  551. static int kgdb_set_sw_break(unsigned long addr)
  552. {
  553. int err = kgdb_validate_break_address(addr);
  554. int breakno = -1;
  555. int i;
  556. if (err)
  557. return err;
  558. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  559. if ((kgdb_break[i].state == BP_SET) &&
  560. (kgdb_break[i].bpt_addr == addr))
  561. return -EEXIST;
  562. }
  563. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  564. if (kgdb_break[i].state == BP_REMOVED &&
  565. kgdb_break[i].bpt_addr == addr) {
  566. breakno = i;
  567. break;
  568. }
  569. }
  570. if (breakno == -1) {
  571. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  572. if (kgdb_break[i].state == BP_UNDEFINED) {
  573. breakno = i;
  574. break;
  575. }
  576. }
  577. }
  578. if (breakno == -1)
  579. return -E2BIG;
  580. kgdb_break[breakno].state = BP_SET;
  581. kgdb_break[breakno].type = BP_BREAKPOINT;
  582. kgdb_break[breakno].bpt_addr = addr;
  583. return 0;
  584. }
  585. static int kgdb_deactivate_sw_breakpoints(void)
  586. {
  587. unsigned long addr;
  588. int error;
  589. int ret = 0;
  590. int i;
  591. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  592. if (kgdb_break[i].state != BP_ACTIVE)
  593. continue;
  594. addr = kgdb_break[i].bpt_addr;
  595. error = kgdb_arch_remove_breakpoint(addr,
  596. kgdb_break[i].saved_instr);
  597. if (error) {
  598. printk(KERN_INFO "KGDB: BP remove failed: %lx\n", addr);
  599. ret = error;
  600. }
  601. kgdb_flush_swbreak_addr(addr);
  602. kgdb_break[i].state = BP_SET;
  603. }
  604. return ret;
  605. }
  606. static int kgdb_remove_sw_break(unsigned long addr)
  607. {
  608. int i;
  609. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  610. if ((kgdb_break[i].state == BP_SET) &&
  611. (kgdb_break[i].bpt_addr == addr)) {
  612. kgdb_break[i].state = BP_REMOVED;
  613. return 0;
  614. }
  615. }
  616. return -ENOENT;
  617. }
  618. int kgdb_isremovedbreak(unsigned long addr)
  619. {
  620. int i;
  621. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  622. if ((kgdb_break[i].state == BP_REMOVED) &&
  623. (kgdb_break[i].bpt_addr == addr))
  624. return 1;
  625. }
  626. return 0;
  627. }
  628. static int remove_all_break(void)
  629. {
  630. unsigned long addr;
  631. int error;
  632. int i;
  633. /* Clear memory breakpoints. */
  634. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  635. if (kgdb_break[i].state != BP_ACTIVE)
  636. goto setundefined;
  637. addr = kgdb_break[i].bpt_addr;
  638. error = kgdb_arch_remove_breakpoint(addr,
  639. kgdb_break[i].saved_instr);
  640. if (error)
  641. printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
  642. addr);
  643. setundefined:
  644. kgdb_break[i].state = BP_UNDEFINED;
  645. }
  646. /* Clear hardware breakpoints. */
  647. if (arch_kgdb_ops.remove_all_hw_break)
  648. arch_kgdb_ops.remove_all_hw_break();
  649. return 0;
  650. }
  651. /*
  652. * Remap normal tasks to their real PID,
  653. * CPU shadow threads are mapped to -CPU - 2
  654. */
  655. static inline int shadow_pid(int realpid)
  656. {
  657. if (realpid)
  658. return realpid;
  659. return -raw_smp_processor_id() - 2;
  660. }
  661. static char gdbmsgbuf[BUFMAX + 1];
  662. static void kgdb_msg_write(const char *s, int len)
  663. {
  664. char *bufptr;
  665. int wcount;
  666. int i;
  667. /* 'O'utput */
  668. gdbmsgbuf[0] = 'O';
  669. /* Fill and send buffers... */
  670. while (len > 0) {
  671. bufptr = gdbmsgbuf + 1;
  672. /* Calculate how many this time */
  673. if ((len << 1) > (BUFMAX - 2))
  674. wcount = (BUFMAX - 2) >> 1;
  675. else
  676. wcount = len;
  677. /* Pack in hex chars */
  678. for (i = 0; i < wcount; i++)
  679. bufptr = pack_hex_byte(bufptr, s[i]);
  680. *bufptr = '\0';
  681. /* Move up */
  682. s += wcount;
  683. len -= wcount;
  684. /* Write packet */
  685. put_packet(gdbmsgbuf);
  686. }
  687. }
  688. /*
  689. * Return true if there is a valid kgdb I/O module. Also if no
  690. * debugger is attached a message can be printed to the console about
  691. * waiting for the debugger to attach.
  692. *
  693. * The print_wait argument is only to be true when called from inside
  694. * the core kgdb_handle_exception, because it will wait for the
  695. * debugger to attach.
  696. */
  697. static int kgdb_io_ready(int print_wait)
  698. {
  699. if (!kgdb_io_ops)
  700. return 0;
  701. if (kgdb_connected)
  702. return 1;
  703. if (atomic_read(&kgdb_setting_breakpoint))
  704. return 1;
  705. if (print_wait)
  706. printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
  707. return 1;
  708. }
  709. /*
  710. * All the functions that start with gdb_cmd are the various
  711. * operations to implement the handlers for the gdbserial protocol
  712. * where KGDB is communicating with an external debugger
  713. */
  714. /* Handle the '?' status packets */
  715. static void gdb_cmd_status(struct kgdb_state *ks)
  716. {
  717. /*
  718. * We know that this packet is only sent
  719. * during initial connect. So to be safe,
  720. * we clear out our breakpoints now in case
  721. * GDB is reconnecting.
  722. */
  723. remove_all_break();
  724. remcom_out_buffer[0] = 'S';
  725. pack_hex_byte(&remcom_out_buffer[1], ks->signo);
  726. }
  727. /* Handle the 'g' get registers request */
  728. static void gdb_cmd_getregs(struct kgdb_state *ks)
  729. {
  730. struct task_struct *thread;
  731. void *local_debuggerinfo;
  732. int i;
  733. thread = kgdb_usethread;
  734. if (!thread) {
  735. thread = kgdb_info[ks->cpu].task;
  736. local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
  737. } else {
  738. local_debuggerinfo = NULL;
  739. for_each_online_cpu(i) {
  740. /*
  741. * Try to find the task on some other
  742. * or possibly this node if we do not
  743. * find the matching task then we try
  744. * to approximate the results.
  745. */
  746. if (thread == kgdb_info[i].task)
  747. local_debuggerinfo = kgdb_info[i].debuggerinfo;
  748. }
  749. }
  750. /*
  751. * All threads that don't have debuggerinfo should be
  752. * in schedule() sleeping, since all other CPUs
  753. * are in kgdb_wait, and thus have debuggerinfo.
  754. */
  755. if (local_debuggerinfo) {
  756. pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
  757. } else {
  758. /*
  759. * Pull stuff saved during switch_to; nothing
  760. * else is accessible (or even particularly
  761. * relevant).
  762. *
  763. * This should be enough for a stack trace.
  764. */
  765. sleeping_thread_to_gdb_regs(gdb_regs, thread);
  766. }
  767. kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
  768. }
  769. /* Handle the 'G' set registers request */
  770. static void gdb_cmd_setregs(struct kgdb_state *ks)
  771. {
  772. kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
  773. if (kgdb_usethread && kgdb_usethread != current) {
  774. error_packet(remcom_out_buffer, -EINVAL);
  775. } else {
  776. gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
  777. strcpy(remcom_out_buffer, "OK");
  778. }
  779. }
  780. /* Handle the 'm' memory read bytes */
  781. static void gdb_cmd_memread(struct kgdb_state *ks)
  782. {
  783. char *ptr = &remcom_in_buffer[1];
  784. unsigned long length;
  785. unsigned long addr;
  786. int err;
  787. if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
  788. kgdb_hex2long(&ptr, &length) > 0) {
  789. err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
  790. if (err)
  791. error_packet(remcom_out_buffer, err);
  792. } else {
  793. error_packet(remcom_out_buffer, -EINVAL);
  794. }
  795. }
  796. /* Handle the 'M' memory write bytes */
  797. static void gdb_cmd_memwrite(struct kgdb_state *ks)
  798. {
  799. int err = write_mem_msg(0);
  800. if (err)
  801. error_packet(remcom_out_buffer, err);
  802. else
  803. strcpy(remcom_out_buffer, "OK");
  804. }
  805. /* Handle the 'X' memory binary write bytes */
  806. static void gdb_cmd_binwrite(struct kgdb_state *ks)
  807. {
  808. int err = write_mem_msg(1);
  809. if (err)
  810. error_packet(remcom_out_buffer, err);
  811. else
  812. strcpy(remcom_out_buffer, "OK");
  813. }
  814. /* Handle the 'D' or 'k', detach or kill packets */
  815. static void gdb_cmd_detachkill(struct kgdb_state *ks)
  816. {
  817. int error;
  818. /* The detach case */
  819. if (remcom_in_buffer[0] == 'D') {
  820. error = remove_all_break();
  821. if (error < 0) {
  822. error_packet(remcom_out_buffer, error);
  823. } else {
  824. strcpy(remcom_out_buffer, "OK");
  825. kgdb_connected = 0;
  826. }
  827. put_packet(remcom_out_buffer);
  828. } else {
  829. /*
  830. * Assume the kill case, with no exit code checking,
  831. * trying to force detach the debugger:
  832. */
  833. remove_all_break();
  834. kgdb_connected = 0;
  835. }
  836. }
  837. /* Handle the 'R' reboot packets */
  838. static int gdb_cmd_reboot(struct kgdb_state *ks)
  839. {
  840. /* For now, only honor R0 */
  841. if (strcmp(remcom_in_buffer, "R0") == 0) {
  842. printk(KERN_CRIT "Executing emergency reboot\n");
  843. strcpy(remcom_out_buffer, "OK");
  844. put_packet(remcom_out_buffer);
  845. /*
  846. * Execution should not return from
  847. * machine_emergency_restart()
  848. */
  849. machine_emergency_restart();
  850. kgdb_connected = 0;
  851. return 1;
  852. }
  853. return 0;
  854. }
  855. /* Handle the 'q' query packets */
  856. static void gdb_cmd_query(struct kgdb_state *ks)
  857. {
  858. struct task_struct *g;
  859. struct task_struct *p;
  860. unsigned char thref[8];
  861. char *ptr;
  862. int i;
  863. int cpu;
  864. int finished = 0;
  865. switch (remcom_in_buffer[1]) {
  866. case 's':
  867. case 'f':
  868. if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
  869. error_packet(remcom_out_buffer, -EINVAL);
  870. break;
  871. }
  872. i = 0;
  873. remcom_out_buffer[0] = 'm';
  874. ptr = remcom_out_buffer + 1;
  875. if (remcom_in_buffer[1] == 'f') {
  876. /* Each cpu is a shadow thread */
  877. for_each_online_cpu(cpu) {
  878. ks->thr_query = 0;
  879. int_to_threadref(thref, -cpu - 2);
  880. pack_threadid(ptr, thref);
  881. ptr += BUF_THREAD_ID_SIZE;
  882. *(ptr++) = ',';
  883. i++;
  884. }
  885. }
  886. do_each_thread(g, p) {
  887. if (i >= ks->thr_query && !finished) {
  888. int_to_threadref(thref, p->pid);
  889. pack_threadid(ptr, thref);
  890. ptr += BUF_THREAD_ID_SIZE;
  891. *(ptr++) = ',';
  892. ks->thr_query++;
  893. if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
  894. finished = 1;
  895. }
  896. i++;
  897. } while_each_thread(g, p);
  898. *(--ptr) = '\0';
  899. break;
  900. case 'C':
  901. /* Current thread id */
  902. strcpy(remcom_out_buffer, "QC");
  903. ks->threadid = shadow_pid(current->pid);
  904. int_to_threadref(thref, ks->threadid);
  905. pack_threadid(remcom_out_buffer + 2, thref);
  906. break;
  907. case 'T':
  908. if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
  909. error_packet(remcom_out_buffer, -EINVAL);
  910. break;
  911. }
  912. ks->threadid = 0;
  913. ptr = remcom_in_buffer + 17;
  914. kgdb_hex2long(&ptr, &ks->threadid);
  915. if (!getthread(ks->linux_regs, ks->threadid)) {
  916. error_packet(remcom_out_buffer, -EINVAL);
  917. break;
  918. }
  919. if ((int)ks->threadid > 0) {
  920. kgdb_mem2hex(getthread(ks->linux_regs,
  921. ks->threadid)->comm,
  922. remcom_out_buffer, 16);
  923. } else {
  924. static char tmpstr[23 + BUF_THREAD_ID_SIZE];
  925. sprintf(tmpstr, "shadowCPU%d",
  926. (int)(-ks->threadid - 2));
  927. kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
  928. }
  929. break;
  930. }
  931. }
  932. /* Handle the 'H' task query packets */
  933. static void gdb_cmd_task(struct kgdb_state *ks)
  934. {
  935. struct task_struct *thread;
  936. char *ptr;
  937. switch (remcom_in_buffer[1]) {
  938. case 'g':
  939. ptr = &remcom_in_buffer[2];
  940. kgdb_hex2long(&ptr, &ks->threadid);
  941. thread = getthread(ks->linux_regs, ks->threadid);
  942. if (!thread && ks->threadid > 0) {
  943. error_packet(remcom_out_buffer, -EINVAL);
  944. break;
  945. }
  946. kgdb_usethread = thread;
  947. ks->kgdb_usethreadid = ks->threadid;
  948. strcpy(remcom_out_buffer, "OK");
  949. break;
  950. case 'c':
  951. ptr = &remcom_in_buffer[2];
  952. kgdb_hex2long(&ptr, &ks->threadid);
  953. if (!ks->threadid) {
  954. kgdb_contthread = NULL;
  955. } else {
  956. thread = getthread(ks->linux_regs, ks->threadid);
  957. if (!thread && ks->threadid > 0) {
  958. error_packet(remcom_out_buffer, -EINVAL);
  959. break;
  960. }
  961. kgdb_contthread = thread;
  962. }
  963. strcpy(remcom_out_buffer, "OK");
  964. break;
  965. }
  966. }
  967. /* Handle the 'T' thread query packets */
  968. static void gdb_cmd_thread(struct kgdb_state *ks)
  969. {
  970. char *ptr = &remcom_in_buffer[1];
  971. struct task_struct *thread;
  972. kgdb_hex2long(&ptr, &ks->threadid);
  973. thread = getthread(ks->linux_regs, ks->threadid);
  974. if (thread)
  975. strcpy(remcom_out_buffer, "OK");
  976. else
  977. error_packet(remcom_out_buffer, -EINVAL);
  978. }
  979. /* Handle the 'z' or 'Z' breakpoint remove or set packets */
  980. static void gdb_cmd_break(struct kgdb_state *ks)
  981. {
  982. /*
  983. * Since GDB-5.3, it's been drafted that '0' is a software
  984. * breakpoint, '1' is a hardware breakpoint, so let's do that.
  985. */
  986. char *bpt_type = &remcom_in_buffer[1];
  987. char *ptr = &remcom_in_buffer[2];
  988. unsigned long addr;
  989. unsigned long length;
  990. int error = 0;
  991. if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
  992. /* Unsupported */
  993. if (*bpt_type > '4')
  994. return;
  995. } else {
  996. if (*bpt_type != '0' && *bpt_type != '1')
  997. /* Unsupported. */
  998. return;
  999. }
  1000. /*
  1001. * Test if this is a hardware breakpoint, and
  1002. * if we support it:
  1003. */
  1004. if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
  1005. /* Unsupported. */
  1006. return;
  1007. if (*(ptr++) != ',') {
  1008. error_packet(remcom_out_buffer, -EINVAL);
  1009. return;
  1010. }
  1011. if (!kgdb_hex2long(&ptr, &addr)) {
  1012. error_packet(remcom_out_buffer, -EINVAL);
  1013. return;
  1014. }
  1015. if (*(ptr++) != ',' ||
  1016. !kgdb_hex2long(&ptr, &length)) {
  1017. error_packet(remcom_out_buffer, -EINVAL);
  1018. return;
  1019. }
  1020. if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
  1021. error = kgdb_set_sw_break(addr);
  1022. else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
  1023. error = kgdb_remove_sw_break(addr);
  1024. else if (remcom_in_buffer[0] == 'Z')
  1025. error = arch_kgdb_ops.set_hw_breakpoint(addr,
  1026. (int)length, *bpt_type - '0');
  1027. else if (remcom_in_buffer[0] == 'z')
  1028. error = arch_kgdb_ops.remove_hw_breakpoint(addr,
  1029. (int) length, *bpt_type - '0');
  1030. if (error == 0)
  1031. strcpy(remcom_out_buffer, "OK");
  1032. else
  1033. error_packet(remcom_out_buffer, error);
  1034. }
  1035. /* Handle the 'C' signal / exception passing packets */
  1036. static int gdb_cmd_exception_pass(struct kgdb_state *ks)
  1037. {
  1038. /* C09 == pass exception
  1039. * C15 == detach kgdb, pass exception
  1040. */
  1041. if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
  1042. ks->pass_exception = 1;
  1043. remcom_in_buffer[0] = 'c';
  1044. } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
  1045. ks->pass_exception = 1;
  1046. remcom_in_buffer[0] = 'D';
  1047. remove_all_break();
  1048. kgdb_connected = 0;
  1049. return 1;
  1050. } else {
  1051. kgdb_msg_write("KGDB only knows signal 9 (pass)"
  1052. " and 15 (pass and disconnect)\n"
  1053. "Executing a continue without signal passing\n", 0);
  1054. remcom_in_buffer[0] = 'c';
  1055. }
  1056. /* Indicate fall through */
  1057. return -1;
  1058. }
  1059. /*
  1060. * This function performs all gdbserial command procesing
  1061. */
  1062. static int gdb_serial_stub(struct kgdb_state *ks)
  1063. {
  1064. int error = 0;
  1065. int tmp;
  1066. /* Clear the out buffer. */
  1067. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  1068. if (kgdb_connected) {
  1069. unsigned char thref[8];
  1070. char *ptr;
  1071. /* Reply to host that an exception has occurred */
  1072. ptr = remcom_out_buffer;
  1073. *ptr++ = 'T';
  1074. ptr = pack_hex_byte(ptr, ks->signo);
  1075. ptr += strlen(strcpy(ptr, "thread:"));
  1076. int_to_threadref(thref, shadow_pid(current->pid));
  1077. ptr = pack_threadid(ptr, thref);
  1078. *ptr++ = ';';
  1079. put_packet(remcom_out_buffer);
  1080. }
  1081. kgdb_usethread = kgdb_info[ks->cpu].task;
  1082. ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
  1083. ks->pass_exception = 0;
  1084. while (1) {
  1085. error = 0;
  1086. /* Clear the out buffer. */
  1087. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  1088. get_packet(remcom_in_buffer);
  1089. switch (remcom_in_buffer[0]) {
  1090. case '?': /* gdbserial status */
  1091. gdb_cmd_status(ks);
  1092. break;
  1093. case 'g': /* return the value of the CPU registers */
  1094. gdb_cmd_getregs(ks);
  1095. break;
  1096. case 'G': /* set the value of the CPU registers - return OK */
  1097. gdb_cmd_setregs(ks);
  1098. break;
  1099. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  1100. gdb_cmd_memread(ks);
  1101. break;
  1102. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  1103. gdb_cmd_memwrite(ks);
  1104. break;
  1105. case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  1106. gdb_cmd_binwrite(ks);
  1107. break;
  1108. /* kill or detach. KGDB should treat this like a
  1109. * continue.
  1110. */
  1111. case 'D': /* Debugger detach */
  1112. case 'k': /* Debugger detach via kill */
  1113. gdb_cmd_detachkill(ks);
  1114. goto default_handle;
  1115. case 'R': /* Reboot */
  1116. if (gdb_cmd_reboot(ks))
  1117. goto default_handle;
  1118. break;
  1119. case 'q': /* query command */
  1120. gdb_cmd_query(ks);
  1121. break;
  1122. case 'H': /* task related */
  1123. gdb_cmd_task(ks);
  1124. break;
  1125. case 'T': /* Query thread status */
  1126. gdb_cmd_thread(ks);
  1127. break;
  1128. case 'z': /* Break point remove */
  1129. case 'Z': /* Break point set */
  1130. gdb_cmd_break(ks);
  1131. break;
  1132. case 'C': /* Exception passing */
  1133. tmp = gdb_cmd_exception_pass(ks);
  1134. if (tmp > 0)
  1135. goto default_handle;
  1136. if (tmp == 0)
  1137. break;
  1138. /* Fall through on tmp < 0 */
  1139. case 'c': /* Continue packet */
  1140. case 's': /* Single step packet */
  1141. if (kgdb_contthread && kgdb_contthread != current) {
  1142. /* Can't switch threads in kgdb */
  1143. error_packet(remcom_out_buffer, -EINVAL);
  1144. break;
  1145. }
  1146. kgdb_activate_sw_breakpoints();
  1147. /* Fall through to default processing */
  1148. default:
  1149. default_handle:
  1150. error = kgdb_arch_handle_exception(ks->ex_vector,
  1151. ks->signo,
  1152. ks->err_code,
  1153. remcom_in_buffer,
  1154. remcom_out_buffer,
  1155. ks->linux_regs);
  1156. /*
  1157. * Leave cmd processing on error, detach,
  1158. * kill, continue, or single step.
  1159. */
  1160. if (error >= 0 || remcom_in_buffer[0] == 'D' ||
  1161. remcom_in_buffer[0] == 'k') {
  1162. error = 0;
  1163. goto kgdb_exit;
  1164. }
  1165. }
  1166. /* reply to the request */
  1167. put_packet(remcom_out_buffer);
  1168. }
  1169. kgdb_exit:
  1170. if (ks->pass_exception)
  1171. error = 1;
  1172. return error;
  1173. }
  1174. static int kgdb_reenter_check(struct kgdb_state *ks)
  1175. {
  1176. unsigned long addr;
  1177. if (atomic_read(&kgdb_active) != raw_smp_processor_id())
  1178. return 0;
  1179. /* Panic on recursive debugger calls: */
  1180. exception_level++;
  1181. addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
  1182. kgdb_deactivate_sw_breakpoints();
  1183. /*
  1184. * If the break point removed ok at the place exception
  1185. * occurred, try to recover and print a warning to the end
  1186. * user because the user planted a breakpoint in a place that
  1187. * KGDB needs in order to function.
  1188. */
  1189. if (kgdb_remove_sw_break(addr) == 0) {
  1190. exception_level = 0;
  1191. kgdb_skipexception(ks->ex_vector, ks->linux_regs);
  1192. kgdb_activate_sw_breakpoints();
  1193. printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
  1194. addr);
  1195. WARN_ON_ONCE(1);
  1196. return 1;
  1197. }
  1198. remove_all_break();
  1199. kgdb_skipexception(ks->ex_vector, ks->linux_regs);
  1200. if (exception_level > 1) {
  1201. dump_stack();
  1202. panic("Recursive entry to debugger");
  1203. }
  1204. printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
  1205. dump_stack();
  1206. panic("Recursive entry to debugger");
  1207. return 1;
  1208. }
  1209. /*
  1210. * kgdb_handle_exception() - main entry point from a kernel exception
  1211. *
  1212. * Locking hierarchy:
  1213. * interface locks, if any (begin_session)
  1214. * kgdb lock (kgdb_active)
  1215. */
  1216. int
  1217. kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
  1218. {
  1219. struct kgdb_state kgdb_var;
  1220. struct kgdb_state *ks = &kgdb_var;
  1221. unsigned long flags;
  1222. int sstep_tries = 100;
  1223. int error = 0;
  1224. int i, cpu;
  1225. ks->cpu = raw_smp_processor_id();
  1226. ks->ex_vector = evector;
  1227. ks->signo = signo;
  1228. ks->ex_vector = evector;
  1229. ks->err_code = ecode;
  1230. ks->kgdb_usethreadid = 0;
  1231. ks->linux_regs = regs;
  1232. if (kgdb_reenter_check(ks))
  1233. return 0; /* Ouch, double exception ! */
  1234. acquirelock:
  1235. /*
  1236. * Interrupts will be restored by the 'trap return' code, except when
  1237. * single stepping.
  1238. */
  1239. local_irq_save(flags);
  1240. cpu = raw_smp_processor_id();
  1241. /*
  1242. * Acquire the kgdb_active lock:
  1243. */
  1244. while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
  1245. cpu_relax();
  1246. /*
  1247. * For single stepping, try to only enter on the processor
  1248. * that was single stepping. To gaurd against a deadlock, the
  1249. * kernel will only try for the value of sstep_tries before
  1250. * giving up and continuing on.
  1251. */
  1252. if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
  1253. (kgdb_info[cpu].task &&
  1254. kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
  1255. atomic_set(&kgdb_active, -1);
  1256. touch_softlockup_watchdog_sync();
  1257. clocksource_touch_watchdog();
  1258. local_irq_restore(flags);
  1259. goto acquirelock;
  1260. }
  1261. if (!kgdb_io_ready(1)) {
  1262. error = 1;
  1263. goto kgdb_restore; /* No I/O connection, so resume the system */
  1264. }
  1265. /*
  1266. * Don't enter if we have hit a removed breakpoint.
  1267. */
  1268. if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
  1269. goto kgdb_restore;
  1270. /* Call the I/O driver's pre_exception routine */
  1271. if (kgdb_io_ops->pre_exception)
  1272. kgdb_io_ops->pre_exception();
  1273. kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
  1274. kgdb_info[ks->cpu].task = current;
  1275. kgdb_disable_hw_debug(ks->linux_regs);
  1276. /*
  1277. * Get the passive CPU lock which will hold all the non-primary
  1278. * CPU in a spin state while the debugger is active
  1279. */
  1280. if (!kgdb_single_step) {
  1281. for (i = 0; i < NR_CPUS; i++)
  1282. atomic_set(&passive_cpu_wait[i], 1);
  1283. }
  1284. /*
  1285. * spin_lock code is good enough as a barrier so we don't
  1286. * need one here:
  1287. */
  1288. atomic_set(&cpu_in_kgdb[ks->cpu], 1);
  1289. #ifdef CONFIG_SMP
  1290. /* Signal the other CPUs to enter kgdb_wait() */
  1291. if ((!kgdb_single_step) && kgdb_do_roundup)
  1292. kgdb_roundup_cpus(flags);
  1293. #endif
  1294. /*
  1295. * Wait for the other CPUs to be notified and be waiting for us:
  1296. */
  1297. for_each_online_cpu(i) {
  1298. while (!atomic_read(&cpu_in_kgdb[i]))
  1299. cpu_relax();
  1300. }
  1301. /*
  1302. * At this point the primary processor is completely
  1303. * in the debugger and all secondary CPUs are quiescent
  1304. */
  1305. kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
  1306. kgdb_deactivate_sw_breakpoints();
  1307. kgdb_single_step = 0;
  1308. kgdb_contthread = current;
  1309. exception_level = 0;
  1310. /* Talk to debugger with gdbserial protocol */
  1311. error = gdb_serial_stub(ks);
  1312. /* Call the I/O driver's post_exception routine */
  1313. if (kgdb_io_ops->post_exception)
  1314. kgdb_io_ops->post_exception();
  1315. kgdb_info[ks->cpu].debuggerinfo = NULL;
  1316. kgdb_info[ks->cpu].task = NULL;
  1317. atomic_set(&cpu_in_kgdb[ks->cpu], 0);
  1318. if (!kgdb_single_step) {
  1319. for (i = NR_CPUS-1; i >= 0; i--)
  1320. atomic_set(&passive_cpu_wait[i], 0);
  1321. /*
  1322. * Wait till all the CPUs have quit
  1323. * from the debugger.
  1324. */
  1325. for_each_online_cpu(i) {
  1326. while (atomic_read(&cpu_in_kgdb[i]))
  1327. cpu_relax();
  1328. }
  1329. }
  1330. kgdb_restore:
  1331. if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
  1332. int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
  1333. if (kgdb_info[sstep_cpu].task)
  1334. kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
  1335. else
  1336. kgdb_sstep_pid = 0;
  1337. }
  1338. /* Free kgdb_active */
  1339. atomic_set(&kgdb_active, -1);
  1340. touch_softlockup_watchdog_sync();
  1341. clocksource_touch_watchdog();
  1342. local_irq_restore(flags);
  1343. return error;
  1344. }
  1345. int kgdb_nmicallback(int cpu, void *regs)
  1346. {
  1347. #ifdef CONFIG_SMP
  1348. if (!atomic_read(&cpu_in_kgdb[cpu]) &&
  1349. atomic_read(&kgdb_active) != cpu &&
  1350. atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)])) {
  1351. kgdb_wait((struct pt_regs *)regs);
  1352. return 0;
  1353. }
  1354. #endif
  1355. return 1;
  1356. }
  1357. static void kgdb_console_write(struct console *co, const char *s,
  1358. unsigned count)
  1359. {
  1360. unsigned long flags;
  1361. /* If we're debugging, or KGDB has not connected, don't try
  1362. * and print. */
  1363. if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
  1364. return;
  1365. local_irq_save(flags);
  1366. kgdb_msg_write(s, count);
  1367. local_irq_restore(flags);
  1368. }
  1369. static struct console kgdbcons = {
  1370. .name = "kgdb",
  1371. .write = kgdb_console_write,
  1372. .flags = CON_PRINTBUFFER | CON_ENABLED,
  1373. .index = -1,
  1374. };
  1375. #ifdef CONFIG_MAGIC_SYSRQ
  1376. static void sysrq_handle_gdb(int key, struct tty_struct *tty)
  1377. {
  1378. if (!kgdb_io_ops) {
  1379. printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
  1380. return;
  1381. }
  1382. if (!kgdb_connected)
  1383. printk(KERN_CRIT "Entering KGDB\n");
  1384. kgdb_breakpoint();
  1385. }
  1386. static struct sysrq_key_op sysrq_gdb_op = {
  1387. .handler = sysrq_handle_gdb,
  1388. .help_msg = "debug(G)",
  1389. .action_msg = "DEBUG",
  1390. };
  1391. #endif
  1392. static void kgdb_register_callbacks(void)
  1393. {
  1394. if (!kgdb_io_module_registered) {
  1395. kgdb_io_module_registered = 1;
  1396. kgdb_arch_init();
  1397. #ifdef CONFIG_MAGIC_SYSRQ
  1398. register_sysrq_key('g', &sysrq_gdb_op);
  1399. #endif
  1400. if (kgdb_use_con && !kgdb_con_registered) {
  1401. register_console(&kgdbcons);
  1402. kgdb_con_registered = 1;
  1403. }
  1404. }
  1405. }
  1406. static void kgdb_unregister_callbacks(void)
  1407. {
  1408. /*
  1409. * When this routine is called KGDB should unregister from the
  1410. * panic handler and clean up, making sure it is not handling any
  1411. * break exceptions at the time.
  1412. */
  1413. if (kgdb_io_module_registered) {
  1414. kgdb_io_module_registered = 0;
  1415. kgdb_arch_exit();
  1416. #ifdef CONFIG_MAGIC_SYSRQ
  1417. unregister_sysrq_key('g', &sysrq_gdb_op);
  1418. #endif
  1419. if (kgdb_con_registered) {
  1420. unregister_console(&kgdbcons);
  1421. kgdb_con_registered = 0;
  1422. }
  1423. }
  1424. }
  1425. static void kgdb_initial_breakpoint(void)
  1426. {
  1427. kgdb_break_asap = 0;
  1428. printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
  1429. kgdb_breakpoint();
  1430. }
  1431. /**
  1432. * kgdb_register_io_module - register KGDB IO module
  1433. * @new_kgdb_io_ops: the io ops vector
  1434. *
  1435. * Register it with the KGDB core.
  1436. */
  1437. int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
  1438. {
  1439. int err;
  1440. spin_lock(&kgdb_registration_lock);
  1441. if (kgdb_io_ops) {
  1442. spin_unlock(&kgdb_registration_lock);
  1443. printk(KERN_ERR "kgdb: Another I/O driver is already "
  1444. "registered with KGDB.\n");
  1445. return -EBUSY;
  1446. }
  1447. if (new_kgdb_io_ops->init) {
  1448. err = new_kgdb_io_ops->init();
  1449. if (err) {
  1450. spin_unlock(&kgdb_registration_lock);
  1451. return err;
  1452. }
  1453. }
  1454. kgdb_io_ops = new_kgdb_io_ops;
  1455. spin_unlock(&kgdb_registration_lock);
  1456. printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
  1457. new_kgdb_io_ops->name);
  1458. /* Arm KGDB now. */
  1459. kgdb_register_callbacks();
  1460. if (kgdb_break_asap)
  1461. kgdb_initial_breakpoint();
  1462. return 0;
  1463. }
  1464. EXPORT_SYMBOL_GPL(kgdb_register_io_module);
  1465. /**
  1466. * kkgdb_unregister_io_module - unregister KGDB IO module
  1467. * @old_kgdb_io_ops: the io ops vector
  1468. *
  1469. * Unregister it with the KGDB core.
  1470. */
  1471. void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
  1472. {
  1473. BUG_ON(kgdb_connected);
  1474. /*
  1475. * KGDB is no longer able to communicate out, so
  1476. * unregister our callbacks and reset state.
  1477. */
  1478. kgdb_unregister_callbacks();
  1479. spin_lock(&kgdb_registration_lock);
  1480. WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
  1481. kgdb_io_ops = NULL;
  1482. spin_unlock(&kgdb_registration_lock);
  1483. printk(KERN_INFO
  1484. "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
  1485. old_kgdb_io_ops->name);
  1486. }
  1487. EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
  1488. /**
  1489. * kgdb_breakpoint - generate breakpoint exception
  1490. *
  1491. * This function will generate a breakpoint exception. It is used at the
  1492. * beginning of a program to sync up with a debugger and can be used
  1493. * otherwise as a quick means to stop program execution and "break" into
  1494. * the debugger.
  1495. */
  1496. void kgdb_breakpoint(void)
  1497. {
  1498. atomic_set(&kgdb_setting_breakpoint, 1);
  1499. wmb(); /* Sync point before breakpoint */
  1500. arch_kgdb_breakpoint();
  1501. wmb(); /* Sync point after breakpoint */
  1502. atomic_set(&kgdb_setting_breakpoint, 0);
  1503. }
  1504. EXPORT_SYMBOL_GPL(kgdb_breakpoint);
  1505. static int __init opt_kgdb_wait(char *str)
  1506. {
  1507. kgdb_break_asap = 1;
  1508. if (kgdb_io_module_registered)
  1509. kgdb_initial_breakpoint();
  1510. return 0;
  1511. }
  1512. early_param("kgdbwait", opt_kgdb_wait);