kgdb.c 38 KB

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