kgdb.c 38 KB

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