kgdb.c 38 KB

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