kgdb.c 37 KB

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