kgdb.c 40 KB

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