kgdb.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  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. unsigned 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. static 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. int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
  170. {
  171. return 0;
  172. }
  173. void __weak
  174. kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
  175. {
  176. return;
  177. }
  178. /**
  179. * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
  180. * @regs: Current &struct pt_regs.
  181. *
  182. * This function will be called if the particular architecture must
  183. * disable hardware debugging while it is processing gdb packets or
  184. * handling exception.
  185. */
  186. void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
  187. {
  188. }
  189. /*
  190. * GDB remote protocol parser:
  191. */
  192. static const char hexchars[] = "0123456789abcdef";
  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(hexchars[checksum >> 4]);
  270. kgdb_io_ops->write_char(hexchars[checksum & 0xf]);
  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] = hexchars[(error / 10)];
  405. pkt[2] = hexchars[(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. *scan++ = (value >> 24) & 0xff;
  430. *scan++ = (value >> 16) & 0xff;
  431. *scan++ = (value >> 8) & 0xff;
  432. *scan++ = (value & 0xff);
  433. }
  434. static struct task_struct *getthread(struct pt_regs *regs, int tid)
  435. {
  436. /*
  437. * Non-positive TIDs are remapped idle tasks:
  438. */
  439. if (tid <= 0)
  440. return idle_task(-tid);
  441. /*
  442. * find_task_by_pid_ns() does not take the tasklist lock anymore
  443. * but is nicely RCU locked - hence is a pretty resilient
  444. * thing to use:
  445. */
  446. return find_task_by_pid_ns(tid, &init_pid_ns);
  447. }
  448. /*
  449. * CPU debug state control:
  450. */
  451. #ifdef CONFIG_SMP
  452. static void kgdb_wait(struct pt_regs *regs)
  453. {
  454. unsigned long flags;
  455. int cpu;
  456. local_irq_save(flags);
  457. cpu = raw_smp_processor_id();
  458. kgdb_info[cpu].debuggerinfo = regs;
  459. kgdb_info[cpu].task = current;
  460. /*
  461. * Make sure the above info reaches the primary CPU before
  462. * our cpu_in_kgdb[] flag setting does:
  463. */
  464. smp_wmb();
  465. atomic_set(&cpu_in_kgdb[cpu], 1);
  466. /* Wait till primary CPU is done with debugging */
  467. while (atomic_read(&passive_cpu_wait[cpu]))
  468. cpu_relax();
  469. kgdb_info[cpu].debuggerinfo = NULL;
  470. kgdb_info[cpu].task = NULL;
  471. /* fix up hardware debug registers on local cpu */
  472. if (arch_kgdb_ops.correct_hw_break)
  473. arch_kgdb_ops.correct_hw_break();
  474. /* Signal the primary CPU that we are done: */
  475. atomic_set(&cpu_in_kgdb[cpu], 0);
  476. clocksource_touch_watchdog();
  477. local_irq_restore(flags);
  478. }
  479. #endif
  480. /*
  481. * Some architectures need cache flushes when we set/clear a
  482. * breakpoint:
  483. */
  484. static void kgdb_flush_swbreak_addr(unsigned long addr)
  485. {
  486. if (!CACHE_FLUSH_IS_SAFE)
  487. return;
  488. if (current->mm && current->mm->mmap_cache) {
  489. flush_cache_range(current->mm->mmap_cache,
  490. addr, addr + BREAK_INSTR_SIZE);
  491. }
  492. /* Force flush instruction cache if it was outside the mm */
  493. flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
  494. }
  495. /*
  496. * SW breakpoint management:
  497. */
  498. static int kgdb_activate_sw_breakpoints(void)
  499. {
  500. unsigned long addr;
  501. int error = 0;
  502. int i;
  503. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  504. if (kgdb_break[i].state != BP_SET)
  505. continue;
  506. addr = kgdb_break[i].bpt_addr;
  507. error = kgdb_arch_set_breakpoint(addr,
  508. kgdb_break[i].saved_instr);
  509. if (error)
  510. return error;
  511. kgdb_flush_swbreak_addr(addr);
  512. kgdb_break[i].state = BP_ACTIVE;
  513. }
  514. return 0;
  515. }
  516. static int kgdb_set_sw_break(unsigned long addr)
  517. {
  518. int err = kgdb_validate_break_address(addr);
  519. int breakno = -1;
  520. int i;
  521. if (err)
  522. return err;
  523. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  524. if ((kgdb_break[i].state == BP_SET) &&
  525. (kgdb_break[i].bpt_addr == addr))
  526. return -EEXIST;
  527. }
  528. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  529. if (kgdb_break[i].state == BP_REMOVED &&
  530. kgdb_break[i].bpt_addr == addr) {
  531. breakno = i;
  532. break;
  533. }
  534. }
  535. if (breakno == -1) {
  536. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  537. if (kgdb_break[i].state == BP_UNDEFINED) {
  538. breakno = i;
  539. break;
  540. }
  541. }
  542. }
  543. if (breakno == -1)
  544. return -E2BIG;
  545. kgdb_break[breakno].state = BP_SET;
  546. kgdb_break[breakno].type = BP_BREAKPOINT;
  547. kgdb_break[breakno].bpt_addr = addr;
  548. return 0;
  549. }
  550. static int kgdb_deactivate_sw_breakpoints(void)
  551. {
  552. unsigned long addr;
  553. int error = 0;
  554. int i;
  555. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  556. if (kgdb_break[i].state != BP_ACTIVE)
  557. continue;
  558. addr = kgdb_break[i].bpt_addr;
  559. error = kgdb_arch_remove_breakpoint(addr,
  560. kgdb_break[i].saved_instr);
  561. if (error)
  562. return error;
  563. kgdb_flush_swbreak_addr(addr);
  564. kgdb_break[i].state = BP_SET;
  565. }
  566. return 0;
  567. }
  568. static int kgdb_remove_sw_break(unsigned long addr)
  569. {
  570. int i;
  571. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  572. if ((kgdb_break[i].state == BP_SET) &&
  573. (kgdb_break[i].bpt_addr == addr)) {
  574. kgdb_break[i].state = BP_REMOVED;
  575. return 0;
  576. }
  577. }
  578. return -ENOENT;
  579. }
  580. int kgdb_isremovedbreak(unsigned long addr)
  581. {
  582. int i;
  583. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  584. if ((kgdb_break[i].state == BP_REMOVED) &&
  585. (kgdb_break[i].bpt_addr == addr))
  586. return 1;
  587. }
  588. return 0;
  589. }
  590. static int remove_all_break(void)
  591. {
  592. unsigned long addr;
  593. int error;
  594. int i;
  595. /* Clear memory breakpoints. */
  596. for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
  597. if (kgdb_break[i].state != BP_ACTIVE)
  598. goto setundefined;
  599. addr = kgdb_break[i].bpt_addr;
  600. error = kgdb_arch_remove_breakpoint(addr,
  601. kgdb_break[i].saved_instr);
  602. if (error)
  603. printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
  604. addr);
  605. setundefined:
  606. kgdb_break[i].state = BP_UNDEFINED;
  607. }
  608. /* Clear hardware breakpoints. */
  609. if (arch_kgdb_ops.remove_all_hw_break)
  610. arch_kgdb_ops.remove_all_hw_break();
  611. return 0;
  612. }
  613. /*
  614. * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs:
  615. */
  616. static inline int shadow_pid(int realpid)
  617. {
  618. if (realpid)
  619. return realpid;
  620. return -1-raw_smp_processor_id();
  621. }
  622. static char gdbmsgbuf[BUFMAX + 1];
  623. static void kgdb_msg_write(const char *s, int len)
  624. {
  625. char *bufptr;
  626. int wcount;
  627. int i;
  628. /* 'O'utput */
  629. gdbmsgbuf[0] = 'O';
  630. /* Fill and send buffers... */
  631. while (len > 0) {
  632. bufptr = gdbmsgbuf + 1;
  633. /* Calculate how many this time */
  634. if ((len << 1) > (BUFMAX - 2))
  635. wcount = (BUFMAX - 2) >> 1;
  636. else
  637. wcount = len;
  638. /* Pack in hex chars */
  639. for (i = 0; i < wcount; i++)
  640. bufptr = pack_hex_byte(bufptr, s[i]);
  641. *bufptr = '\0';
  642. /* Move up */
  643. s += wcount;
  644. len -= wcount;
  645. /* Write packet */
  646. put_packet(gdbmsgbuf);
  647. }
  648. }
  649. /*
  650. * Return true if there is a valid kgdb I/O module. Also if no
  651. * debugger is attached a message can be printed to the console about
  652. * waiting for the debugger to attach.
  653. *
  654. * The print_wait argument is only to be true when called from inside
  655. * the core kgdb_handle_exception, because it will wait for the
  656. * debugger to attach.
  657. */
  658. static int kgdb_io_ready(int print_wait)
  659. {
  660. if (!kgdb_io_ops)
  661. return 0;
  662. if (kgdb_connected)
  663. return 1;
  664. if (atomic_read(&kgdb_setting_breakpoint))
  665. return 1;
  666. if (print_wait)
  667. printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
  668. return 1;
  669. }
  670. /*
  671. * All the functions that start with gdb_cmd are the various
  672. * operations to implement the handlers for the gdbserial protocol
  673. * where KGDB is communicating with an external debugger
  674. */
  675. /* Handle the '?' status packets */
  676. static void gdb_cmd_status(struct kgdb_state *ks)
  677. {
  678. /*
  679. * We know that this packet is only sent
  680. * during initial connect. So to be safe,
  681. * we clear out our breakpoints now in case
  682. * GDB is reconnecting.
  683. */
  684. remove_all_break();
  685. remcom_out_buffer[0] = 'S';
  686. pack_hex_byte(&remcom_out_buffer[1], ks->signo);
  687. }
  688. /* Handle the 'g' get registers request */
  689. static void gdb_cmd_getregs(struct kgdb_state *ks)
  690. {
  691. struct task_struct *thread;
  692. void *local_debuggerinfo;
  693. int i;
  694. thread = kgdb_usethread;
  695. if (!thread) {
  696. thread = kgdb_info[ks->cpu].task;
  697. local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
  698. } else {
  699. local_debuggerinfo = NULL;
  700. for (i = 0; i < NR_CPUS; i++) {
  701. /*
  702. * Try to find the task on some other
  703. * or possibly this node if we do not
  704. * find the matching task then we try
  705. * to approximate the results.
  706. */
  707. if (thread == kgdb_info[i].task)
  708. local_debuggerinfo = kgdb_info[i].debuggerinfo;
  709. }
  710. }
  711. /*
  712. * All threads that don't have debuggerinfo should be
  713. * in __schedule() sleeping, since all other CPUs
  714. * are in kgdb_wait, and thus have debuggerinfo.
  715. */
  716. if (local_debuggerinfo) {
  717. pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
  718. } else {
  719. /*
  720. * Pull stuff saved during switch_to; nothing
  721. * else is accessible (or even particularly
  722. * relevant).
  723. *
  724. * This should be enough for a stack trace.
  725. */
  726. sleeping_thread_to_gdb_regs(gdb_regs, thread);
  727. }
  728. kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
  729. }
  730. /* Handle the 'G' set registers request */
  731. static void gdb_cmd_setregs(struct kgdb_state *ks)
  732. {
  733. kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
  734. if (kgdb_usethread && kgdb_usethread != current) {
  735. error_packet(remcom_out_buffer, -EINVAL);
  736. } else {
  737. gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
  738. strcpy(remcom_out_buffer, "OK");
  739. }
  740. }
  741. /* Handle the 'm' memory read bytes */
  742. static void gdb_cmd_memread(struct kgdb_state *ks)
  743. {
  744. char *ptr = &remcom_in_buffer[1];
  745. unsigned long length;
  746. unsigned long addr;
  747. int err;
  748. if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
  749. kgdb_hex2long(&ptr, &length) > 0) {
  750. err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
  751. if (err)
  752. error_packet(remcom_out_buffer, err);
  753. } else {
  754. error_packet(remcom_out_buffer, -EINVAL);
  755. }
  756. }
  757. /* Handle the 'M' memory write bytes */
  758. static void gdb_cmd_memwrite(struct kgdb_state *ks)
  759. {
  760. int err = write_mem_msg(0);
  761. if (err)
  762. error_packet(remcom_out_buffer, err);
  763. else
  764. strcpy(remcom_out_buffer, "OK");
  765. }
  766. /* Handle the 'X' memory binary write bytes */
  767. static void gdb_cmd_binwrite(struct kgdb_state *ks)
  768. {
  769. int err = write_mem_msg(1);
  770. if (err)
  771. error_packet(remcom_out_buffer, err);
  772. else
  773. strcpy(remcom_out_buffer, "OK");
  774. }
  775. /* Handle the 'D' or 'k', detach or kill packets */
  776. static void gdb_cmd_detachkill(struct kgdb_state *ks)
  777. {
  778. int error;
  779. /* The detach case */
  780. if (remcom_in_buffer[0] == 'D') {
  781. error = remove_all_break();
  782. if (error < 0) {
  783. error_packet(remcom_out_buffer, error);
  784. } else {
  785. strcpy(remcom_out_buffer, "OK");
  786. kgdb_connected = 0;
  787. }
  788. put_packet(remcom_out_buffer);
  789. } else {
  790. /*
  791. * Assume the kill case, with no exit code checking,
  792. * trying to force detach the debugger:
  793. */
  794. remove_all_break();
  795. kgdb_connected = 0;
  796. }
  797. }
  798. /* Handle the 'R' reboot packets */
  799. static int gdb_cmd_reboot(struct kgdb_state *ks)
  800. {
  801. /* For now, only honor R0 */
  802. if (strcmp(remcom_in_buffer, "R0") == 0) {
  803. printk(KERN_CRIT "Executing emergency reboot\n");
  804. strcpy(remcom_out_buffer, "OK");
  805. put_packet(remcom_out_buffer);
  806. /*
  807. * Execution should not return from
  808. * machine_emergency_restart()
  809. */
  810. machine_emergency_restart();
  811. kgdb_connected = 0;
  812. return 1;
  813. }
  814. return 0;
  815. }
  816. /* Handle the 'q' query packets */
  817. static void gdb_cmd_query(struct kgdb_state *ks)
  818. {
  819. struct task_struct *thread;
  820. unsigned char thref[8];
  821. char *ptr;
  822. int i;
  823. switch (remcom_in_buffer[1]) {
  824. case 's':
  825. case 'f':
  826. if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
  827. error_packet(remcom_out_buffer, -EINVAL);
  828. break;
  829. }
  830. if (remcom_in_buffer[1] == 'f')
  831. ks->threadid = 1;
  832. remcom_out_buffer[0] = 'm';
  833. ptr = remcom_out_buffer + 1;
  834. for (i = 0; i < 17; ks->threadid++) {
  835. thread = getthread(ks->linux_regs, ks->threadid);
  836. if (thread) {
  837. int_to_threadref(thref, ks->threadid);
  838. pack_threadid(ptr, thref);
  839. ptr += BUF_THREAD_ID_SIZE;
  840. *(ptr++) = ',';
  841. i++;
  842. }
  843. }
  844. *(--ptr) = '\0';
  845. break;
  846. case 'C':
  847. /* Current thread id */
  848. strcpy(remcom_out_buffer, "QC");
  849. ks->threadid = shadow_pid(current->pid);
  850. int_to_threadref(thref, ks->threadid);
  851. pack_threadid(remcom_out_buffer + 2, thref);
  852. break;
  853. case 'T':
  854. if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
  855. error_packet(remcom_out_buffer, -EINVAL);
  856. break;
  857. }
  858. ks->threadid = 0;
  859. ptr = remcom_in_buffer + 17;
  860. kgdb_hex2long(&ptr, &ks->threadid);
  861. if (!getthread(ks->linux_regs, ks->threadid)) {
  862. error_packet(remcom_out_buffer, -EINVAL);
  863. break;
  864. }
  865. if (ks->threadid > 0) {
  866. kgdb_mem2hex(getthread(ks->linux_regs,
  867. ks->threadid)->comm,
  868. remcom_out_buffer, 16);
  869. } else {
  870. static char tmpstr[23 + BUF_THREAD_ID_SIZE];
  871. sprintf(tmpstr, "Shadow task %d for pid 0",
  872. (int)(-ks->threadid-1));
  873. kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
  874. }
  875. break;
  876. }
  877. }
  878. /* Handle the 'H' task query packets */
  879. static void gdb_cmd_task(struct kgdb_state *ks)
  880. {
  881. struct task_struct *thread;
  882. char *ptr;
  883. switch (remcom_in_buffer[1]) {
  884. case 'g':
  885. ptr = &remcom_in_buffer[2];
  886. kgdb_hex2long(&ptr, &ks->threadid);
  887. thread = getthread(ks->linux_regs, ks->threadid);
  888. if (!thread && ks->threadid > 0) {
  889. error_packet(remcom_out_buffer, -EINVAL);
  890. break;
  891. }
  892. kgdb_usethread = thread;
  893. ks->kgdb_usethreadid = ks->threadid;
  894. strcpy(remcom_out_buffer, "OK");
  895. break;
  896. case 'c':
  897. ptr = &remcom_in_buffer[2];
  898. kgdb_hex2long(&ptr, &ks->threadid);
  899. if (!ks->threadid) {
  900. kgdb_contthread = NULL;
  901. } else {
  902. thread = getthread(ks->linux_regs, ks->threadid);
  903. if (!thread && ks->threadid > 0) {
  904. error_packet(remcom_out_buffer, -EINVAL);
  905. break;
  906. }
  907. kgdb_contthread = thread;
  908. }
  909. strcpy(remcom_out_buffer, "OK");
  910. break;
  911. }
  912. }
  913. /* Handle the 'T' thread query packets */
  914. static void gdb_cmd_thread(struct kgdb_state *ks)
  915. {
  916. char *ptr = &remcom_in_buffer[1];
  917. struct task_struct *thread;
  918. kgdb_hex2long(&ptr, &ks->threadid);
  919. thread = getthread(ks->linux_regs, ks->threadid);
  920. if (thread)
  921. strcpy(remcom_out_buffer, "OK");
  922. else
  923. error_packet(remcom_out_buffer, -EINVAL);
  924. }
  925. /* Handle the 'z' or 'Z' breakpoint remove or set packets */
  926. static void gdb_cmd_break(struct kgdb_state *ks)
  927. {
  928. /*
  929. * Since GDB-5.3, it's been drafted that '0' is a software
  930. * breakpoint, '1' is a hardware breakpoint, so let's do that.
  931. */
  932. char *bpt_type = &remcom_in_buffer[1];
  933. char *ptr = &remcom_in_buffer[2];
  934. unsigned long addr;
  935. unsigned long length;
  936. int error = 0;
  937. if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
  938. /* Unsupported */
  939. if (*bpt_type > '4')
  940. return;
  941. } else {
  942. if (*bpt_type != '0' && *bpt_type != '1')
  943. /* Unsupported. */
  944. return;
  945. }
  946. /*
  947. * Test if this is a hardware breakpoint, and
  948. * if we support it:
  949. */
  950. if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
  951. /* Unsupported. */
  952. return;
  953. if (*(ptr++) != ',') {
  954. error_packet(remcom_out_buffer, -EINVAL);
  955. return;
  956. }
  957. if (!kgdb_hex2long(&ptr, &addr)) {
  958. error_packet(remcom_out_buffer, -EINVAL);
  959. return;
  960. }
  961. if (*(ptr++) != ',' ||
  962. !kgdb_hex2long(&ptr, &length)) {
  963. error_packet(remcom_out_buffer, -EINVAL);
  964. return;
  965. }
  966. if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
  967. error = kgdb_set_sw_break(addr);
  968. else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
  969. error = kgdb_remove_sw_break(addr);
  970. else if (remcom_in_buffer[0] == 'Z')
  971. error = arch_kgdb_ops.set_hw_breakpoint(addr,
  972. (int)length, *bpt_type - '0');
  973. else if (remcom_in_buffer[0] == 'z')
  974. error = arch_kgdb_ops.remove_hw_breakpoint(addr,
  975. (int) length, *bpt_type - '0');
  976. if (error == 0)
  977. strcpy(remcom_out_buffer, "OK");
  978. else
  979. error_packet(remcom_out_buffer, error);
  980. }
  981. /* Handle the 'C' signal / exception passing packets */
  982. static int gdb_cmd_exception_pass(struct kgdb_state *ks)
  983. {
  984. /* C09 == pass exception
  985. * C15 == detach kgdb, pass exception
  986. */
  987. if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
  988. ks->pass_exception = 1;
  989. remcom_in_buffer[0] = 'c';
  990. } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
  991. ks->pass_exception = 1;
  992. remcom_in_buffer[0] = 'D';
  993. remove_all_break();
  994. kgdb_connected = 0;
  995. return 1;
  996. } else {
  997. error_packet(remcom_out_buffer, -EINVAL);
  998. return 0;
  999. }
  1000. /* Indicate fall through */
  1001. return -1;
  1002. }
  1003. /*
  1004. * This function performs all gdbserial command procesing
  1005. */
  1006. static int gdb_serial_stub(struct kgdb_state *ks)
  1007. {
  1008. int error = 0;
  1009. int tmp;
  1010. /* Clear the out buffer. */
  1011. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  1012. if (kgdb_connected) {
  1013. unsigned char thref[8];
  1014. char *ptr;
  1015. /* Reply to host that an exception has occurred */
  1016. ptr = remcom_out_buffer;
  1017. *ptr++ = 'T';
  1018. ptr = pack_hex_byte(ptr, ks->signo);
  1019. ptr += strlen(strcpy(ptr, "thread:"));
  1020. int_to_threadref(thref, shadow_pid(current->pid));
  1021. ptr = pack_threadid(ptr, thref);
  1022. *ptr++ = ';';
  1023. put_packet(remcom_out_buffer);
  1024. }
  1025. kgdb_usethread = kgdb_info[ks->cpu].task;
  1026. ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
  1027. ks->pass_exception = 0;
  1028. while (1) {
  1029. error = 0;
  1030. /* Clear the out buffer. */
  1031. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  1032. get_packet(remcom_in_buffer);
  1033. switch (remcom_in_buffer[0]) {
  1034. case '?': /* gdbserial status */
  1035. gdb_cmd_status(ks);
  1036. break;
  1037. case 'g': /* return the value of the CPU registers */
  1038. gdb_cmd_getregs(ks);
  1039. break;
  1040. case 'G': /* set the value of the CPU registers - return OK */
  1041. gdb_cmd_setregs(ks);
  1042. break;
  1043. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  1044. gdb_cmd_memread(ks);
  1045. break;
  1046. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  1047. gdb_cmd_memwrite(ks);
  1048. break;
  1049. case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  1050. gdb_cmd_binwrite(ks);
  1051. break;
  1052. /* kill or detach. KGDB should treat this like a
  1053. * continue.
  1054. */
  1055. case 'D': /* Debugger detach */
  1056. case 'k': /* Debugger detach via kill */
  1057. gdb_cmd_detachkill(ks);
  1058. goto default_handle;
  1059. case 'R': /* Reboot */
  1060. if (gdb_cmd_reboot(ks))
  1061. goto default_handle;
  1062. break;
  1063. case 'q': /* query command */
  1064. gdb_cmd_query(ks);
  1065. break;
  1066. case 'H': /* task related */
  1067. gdb_cmd_task(ks);
  1068. break;
  1069. case 'T': /* Query thread status */
  1070. gdb_cmd_thread(ks);
  1071. break;
  1072. case 'z': /* Break point remove */
  1073. case 'Z': /* Break point set */
  1074. gdb_cmd_break(ks);
  1075. break;
  1076. case 'C': /* Exception passing */
  1077. tmp = gdb_cmd_exception_pass(ks);
  1078. if (tmp > 0)
  1079. goto default_handle;
  1080. if (tmp == 0)
  1081. break;
  1082. /* Fall through on tmp < 0 */
  1083. case 'c': /* Continue packet */
  1084. case 's': /* Single step packet */
  1085. if (kgdb_contthread && kgdb_contthread != current) {
  1086. /* Can't switch threads in kgdb */
  1087. error_packet(remcom_out_buffer, -EINVAL);
  1088. break;
  1089. }
  1090. kgdb_activate_sw_breakpoints();
  1091. /* Fall through to default processing */
  1092. default:
  1093. default_handle:
  1094. error = kgdb_arch_handle_exception(ks->ex_vector,
  1095. ks->signo,
  1096. ks->err_code,
  1097. remcom_in_buffer,
  1098. remcom_out_buffer,
  1099. ks->linux_regs);
  1100. /*
  1101. * Leave cmd processing on error, detach,
  1102. * kill, continue, or single step.
  1103. */
  1104. if (error >= 0 || remcom_in_buffer[0] == 'D' ||
  1105. remcom_in_buffer[0] == 'k') {
  1106. error = 0;
  1107. goto kgdb_exit;
  1108. }
  1109. }
  1110. /* reply to the request */
  1111. put_packet(remcom_out_buffer);
  1112. }
  1113. kgdb_exit:
  1114. if (ks->pass_exception)
  1115. error = 1;
  1116. return error;
  1117. }
  1118. static int kgdb_reenter_check(struct kgdb_state *ks)
  1119. {
  1120. unsigned long addr;
  1121. if (atomic_read(&kgdb_active) != raw_smp_processor_id())
  1122. return 0;
  1123. /* Panic on recursive debugger calls: */
  1124. exception_level++;
  1125. addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
  1126. kgdb_deactivate_sw_breakpoints();
  1127. /*
  1128. * If the break point removed ok at the place exception
  1129. * occurred, try to recover and print a warning to the end
  1130. * user because the user planted a breakpoint in a place that
  1131. * KGDB needs in order to function.
  1132. */
  1133. if (kgdb_remove_sw_break(addr) == 0) {
  1134. exception_level = 0;
  1135. kgdb_skipexception(ks->ex_vector, ks->linux_regs);
  1136. kgdb_activate_sw_breakpoints();
  1137. printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
  1138. addr);
  1139. WARN_ON_ONCE(1);
  1140. return 1;
  1141. }
  1142. remove_all_break();
  1143. kgdb_skipexception(ks->ex_vector, ks->linux_regs);
  1144. if (exception_level > 1) {
  1145. dump_stack();
  1146. panic("Recursive entry to debugger");
  1147. }
  1148. printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
  1149. dump_stack();
  1150. panic("Recursive entry to debugger");
  1151. return 1;
  1152. }
  1153. /*
  1154. * kgdb_handle_exception() - main entry point from a kernel exception
  1155. *
  1156. * Locking hierarchy:
  1157. * interface locks, if any (begin_session)
  1158. * kgdb lock (kgdb_active)
  1159. */
  1160. int
  1161. kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
  1162. {
  1163. struct kgdb_state kgdb_var;
  1164. struct kgdb_state *ks = &kgdb_var;
  1165. unsigned long flags;
  1166. int error = 0;
  1167. int i, cpu;
  1168. ks->cpu = raw_smp_processor_id();
  1169. ks->ex_vector = evector;
  1170. ks->signo = signo;
  1171. ks->ex_vector = evector;
  1172. ks->err_code = ecode;
  1173. ks->kgdb_usethreadid = 0;
  1174. ks->linux_regs = regs;
  1175. if (kgdb_reenter_check(ks))
  1176. return 0; /* Ouch, double exception ! */
  1177. acquirelock:
  1178. /*
  1179. * Interrupts will be restored by the 'trap return' code, except when
  1180. * single stepping.
  1181. */
  1182. local_irq_save(flags);
  1183. cpu = raw_smp_processor_id();
  1184. /*
  1185. * Acquire the kgdb_active lock:
  1186. */
  1187. while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
  1188. cpu_relax();
  1189. /*
  1190. * Do not start the debugger connection on this CPU if the last
  1191. * instance of the exception handler wanted to come into the
  1192. * debugger on a different CPU via a single step
  1193. */
  1194. if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
  1195. atomic_read(&kgdb_cpu_doing_single_step) != cpu) {
  1196. atomic_set(&kgdb_active, -1);
  1197. clocksource_touch_watchdog();
  1198. local_irq_restore(flags);
  1199. goto acquirelock;
  1200. }
  1201. if (!kgdb_io_ready(1)) {
  1202. error = 1;
  1203. goto kgdb_restore; /* No I/O connection, so resume the system */
  1204. }
  1205. /*
  1206. * Don't enter if we have hit a removed breakpoint.
  1207. */
  1208. if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
  1209. goto kgdb_restore;
  1210. /* Call the I/O driver's pre_exception routine */
  1211. if (kgdb_io_ops->pre_exception)
  1212. kgdb_io_ops->pre_exception();
  1213. kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
  1214. kgdb_info[ks->cpu].task = current;
  1215. kgdb_disable_hw_debug(ks->linux_regs);
  1216. /*
  1217. * Get the passive CPU lock which will hold all the non-primary
  1218. * CPU in a spin state while the debugger is active
  1219. */
  1220. if (!kgdb_single_step || !kgdb_contthread) {
  1221. for (i = 0; i < NR_CPUS; i++)
  1222. atomic_set(&passive_cpu_wait[i], 1);
  1223. }
  1224. /*
  1225. * spin_lock code is good enough as a barrier so we don't
  1226. * need one here:
  1227. */
  1228. atomic_set(&cpu_in_kgdb[ks->cpu], 1);
  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. * Wait for the other CPUs to be notified and be waiting for us:
  1236. */
  1237. for_each_online_cpu(i) {
  1238. while (!atomic_read(&cpu_in_kgdb[i]))
  1239. cpu_relax();
  1240. }
  1241. /*
  1242. * At this point the primary processor is completely
  1243. * in the debugger and all secondary CPUs are quiescent
  1244. */
  1245. kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
  1246. kgdb_deactivate_sw_breakpoints();
  1247. kgdb_single_step = 0;
  1248. kgdb_contthread = NULL;
  1249. exception_level = 0;
  1250. /* Talk to debugger with gdbserial protocol */
  1251. error = gdb_serial_stub(ks);
  1252. /* Call the I/O driver's post_exception routine */
  1253. if (kgdb_io_ops->post_exception)
  1254. kgdb_io_ops->post_exception();
  1255. kgdb_info[ks->cpu].debuggerinfo = NULL;
  1256. kgdb_info[ks->cpu].task = NULL;
  1257. atomic_set(&cpu_in_kgdb[ks->cpu], 0);
  1258. if (!kgdb_single_step || !kgdb_contthread) {
  1259. for (i = NR_CPUS-1; i >= 0; i--)
  1260. atomic_set(&passive_cpu_wait[i], 0);
  1261. /*
  1262. * Wait till all the CPUs have quit
  1263. * from the debugger.
  1264. */
  1265. for_each_online_cpu(i) {
  1266. while (atomic_read(&cpu_in_kgdb[i]))
  1267. cpu_relax();
  1268. }
  1269. }
  1270. kgdb_restore:
  1271. /* Free kgdb_active */
  1272. atomic_set(&kgdb_active, -1);
  1273. clocksource_touch_watchdog();
  1274. local_irq_restore(flags);
  1275. return error;
  1276. }
  1277. int kgdb_nmicallback(int cpu, void *regs)
  1278. {
  1279. #ifdef CONFIG_SMP
  1280. if (!atomic_read(&cpu_in_kgdb[cpu]) &&
  1281. atomic_read(&kgdb_active) != cpu &&
  1282. atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)])) {
  1283. kgdb_wait((struct pt_regs *)regs);
  1284. return 0;
  1285. }
  1286. #endif
  1287. return 1;
  1288. }
  1289. void kgdb_console_write(struct console *co, const char *s, unsigned count)
  1290. {
  1291. unsigned long flags;
  1292. /* If we're debugging, or KGDB has not connected, don't try
  1293. * and print. */
  1294. if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
  1295. return;
  1296. local_irq_save(flags);
  1297. kgdb_msg_write(s, count);
  1298. local_irq_restore(flags);
  1299. }
  1300. static struct console kgdbcons = {
  1301. .name = "kgdb",
  1302. .write = kgdb_console_write,
  1303. .flags = CON_PRINTBUFFER | CON_ENABLED,
  1304. .index = -1,
  1305. };
  1306. #ifdef CONFIG_MAGIC_SYSRQ
  1307. static void sysrq_handle_gdb(int key, struct tty_struct *tty)
  1308. {
  1309. if (!kgdb_io_ops) {
  1310. printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
  1311. return;
  1312. }
  1313. if (!kgdb_connected)
  1314. printk(KERN_CRIT "Entering KGDB\n");
  1315. kgdb_breakpoint();
  1316. }
  1317. static struct sysrq_key_op sysrq_gdb_op = {
  1318. .handler = sysrq_handle_gdb,
  1319. .help_msg = "Gdb",
  1320. .action_msg = "GDB",
  1321. };
  1322. #endif
  1323. static void kgdb_register_callbacks(void)
  1324. {
  1325. if (!kgdb_io_module_registered) {
  1326. kgdb_io_module_registered = 1;
  1327. kgdb_arch_init();
  1328. #ifdef CONFIG_MAGIC_SYSRQ
  1329. register_sysrq_key('g', &sysrq_gdb_op);
  1330. #endif
  1331. if (kgdb_use_con && !kgdb_con_registered) {
  1332. register_console(&kgdbcons);
  1333. kgdb_con_registered = 1;
  1334. }
  1335. }
  1336. }
  1337. static void kgdb_unregister_callbacks(void)
  1338. {
  1339. /*
  1340. * When this routine is called KGDB should unregister from the
  1341. * panic handler and clean up, making sure it is not handling any
  1342. * break exceptions at the time.
  1343. */
  1344. if (kgdb_io_module_registered) {
  1345. kgdb_io_module_registered = 0;
  1346. kgdb_arch_exit();
  1347. #ifdef CONFIG_MAGIC_SYSRQ
  1348. unregister_sysrq_key('g', &sysrq_gdb_op);
  1349. #endif
  1350. if (kgdb_con_registered) {
  1351. unregister_console(&kgdbcons);
  1352. kgdb_con_registered = 0;
  1353. }
  1354. }
  1355. }
  1356. static void kgdb_initial_breakpoint(void)
  1357. {
  1358. kgdb_break_asap = 0;
  1359. printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
  1360. kgdb_breakpoint();
  1361. }
  1362. /**
  1363. * kgdb_register_io_module - register KGDB IO module
  1364. * @new_kgdb_io_ops: the io ops vector
  1365. *
  1366. * Register it with the KGDB core.
  1367. */
  1368. int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
  1369. {
  1370. int err;
  1371. spin_lock(&kgdb_registration_lock);
  1372. if (kgdb_io_ops) {
  1373. spin_unlock(&kgdb_registration_lock);
  1374. printk(KERN_ERR "kgdb: Another I/O driver is already "
  1375. "registered with KGDB.\n");
  1376. return -EBUSY;
  1377. }
  1378. if (new_kgdb_io_ops->init) {
  1379. err = new_kgdb_io_ops->init();
  1380. if (err) {
  1381. spin_unlock(&kgdb_registration_lock);
  1382. return err;
  1383. }
  1384. }
  1385. kgdb_io_ops = new_kgdb_io_ops;
  1386. spin_unlock(&kgdb_registration_lock);
  1387. printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
  1388. new_kgdb_io_ops->name);
  1389. /* Arm KGDB now. */
  1390. kgdb_register_callbacks();
  1391. if (kgdb_break_asap)
  1392. kgdb_initial_breakpoint();
  1393. return 0;
  1394. }
  1395. EXPORT_SYMBOL_GPL(kgdb_register_io_module);
  1396. /**
  1397. * kkgdb_unregister_io_module - unregister KGDB IO module
  1398. * @old_kgdb_io_ops: the io ops vector
  1399. *
  1400. * Unregister it with the KGDB core.
  1401. */
  1402. void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
  1403. {
  1404. BUG_ON(kgdb_connected);
  1405. /*
  1406. * KGDB is no longer able to communicate out, so
  1407. * unregister our callbacks and reset state.
  1408. */
  1409. kgdb_unregister_callbacks();
  1410. spin_lock(&kgdb_registration_lock);
  1411. WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
  1412. kgdb_io_ops = NULL;
  1413. spin_unlock(&kgdb_registration_lock);
  1414. printk(KERN_INFO
  1415. "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
  1416. old_kgdb_io_ops->name);
  1417. }
  1418. EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
  1419. /**
  1420. * kgdb_breakpoint - generate breakpoint exception
  1421. *
  1422. * This function will generate a breakpoint exception. It is used at the
  1423. * beginning of a program to sync up with a debugger and can be used
  1424. * otherwise as a quick means to stop program execution and "break" into
  1425. * the debugger.
  1426. */
  1427. void kgdb_breakpoint(void)
  1428. {
  1429. atomic_set(&kgdb_setting_breakpoint, 1);
  1430. wmb(); /* Sync point before breakpoint */
  1431. arch_kgdb_breakpoint();
  1432. wmb(); /* Sync point after breakpoint */
  1433. atomic_set(&kgdb_setting_breakpoint, 0);
  1434. }
  1435. EXPORT_SYMBOL_GPL(kgdb_breakpoint);
  1436. static int __init opt_kgdb_wait(char *str)
  1437. {
  1438. kgdb_break_asap = 1;
  1439. if (kgdb_io_module_registered)
  1440. kgdb_initial_breakpoint();
  1441. return 0;
  1442. }
  1443. early_param("kgdbwait", opt_kgdb_wait);