kgdb.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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. acquirelock:
  1187. /*
  1188. * Interrupts will be restored by the 'trap return' code, except when
  1189. * single stepping.
  1190. */
  1191. local_irq_save(flags);
  1192. cpu = ks->cpu;
  1193. kgdb_info[cpu].debuggerinfo = regs;
  1194. kgdb_info[cpu].task = current;
  1195. /*
  1196. * Make sure the above info reaches the primary CPU before
  1197. * our cpu_in_kgdb[] flag setting does:
  1198. */
  1199. atomic_inc(&cpu_in_kgdb[cpu]);
  1200. /*
  1201. * CPU will loop if it is a slave or request to become a kgdb
  1202. * master cpu and acquire the kgdb_active lock:
  1203. */
  1204. while (1) {
  1205. if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
  1206. if (atomic_cmpxchg(&kgdb_active, -1, cpu) == cpu)
  1207. break;
  1208. } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
  1209. if (!atomic_read(&passive_cpu_wait[cpu]))
  1210. goto return_normal;
  1211. } else {
  1212. return_normal:
  1213. /* Return to normal operation by executing any
  1214. * hw breakpoint fixup.
  1215. */
  1216. if (arch_kgdb_ops.correct_hw_break)
  1217. arch_kgdb_ops.correct_hw_break();
  1218. atomic_dec(&cpu_in_kgdb[cpu]);
  1219. touch_softlockup_watchdog_sync();
  1220. clocksource_touch_watchdog();
  1221. local_irq_restore(flags);
  1222. return 0;
  1223. }
  1224. cpu_relax();
  1225. }
  1226. /*
  1227. * For single stepping, try to only enter on the processor
  1228. * that was single stepping. To gaurd against a deadlock, the
  1229. * kernel will only try for the value of sstep_tries before
  1230. * giving up and continuing on.
  1231. */
  1232. if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
  1233. (kgdb_info[cpu].task &&
  1234. kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
  1235. atomic_set(&kgdb_active, -1);
  1236. touch_softlockup_watchdog_sync();
  1237. clocksource_touch_watchdog();
  1238. local_irq_restore(flags);
  1239. goto acquirelock;
  1240. }
  1241. if (!kgdb_io_ready(1)) {
  1242. error = 1;
  1243. goto kgdb_restore; /* No I/O connection, so resume the system */
  1244. }
  1245. /*
  1246. * Don't enter if we have hit a removed breakpoint.
  1247. */
  1248. if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
  1249. goto kgdb_restore;
  1250. /* Call the I/O driver's pre_exception routine */
  1251. if (kgdb_io_ops->pre_exception)
  1252. kgdb_io_ops->pre_exception();
  1253. kgdb_disable_hw_debug(ks->linux_regs);
  1254. /*
  1255. * Get the passive CPU lock which will hold all the non-primary
  1256. * CPU in a spin state while the debugger is active
  1257. */
  1258. if (!kgdb_single_step) {
  1259. for (i = 0; i < NR_CPUS; i++)
  1260. atomic_inc(&passive_cpu_wait[i]);
  1261. }
  1262. #ifdef CONFIG_SMP
  1263. /* Signal the other CPUs to enter kgdb_wait() */
  1264. if ((!kgdb_single_step) && kgdb_do_roundup)
  1265. kgdb_roundup_cpus(flags);
  1266. #endif
  1267. /*
  1268. * Wait for the other CPUs to be notified and be waiting for us:
  1269. */
  1270. for_each_online_cpu(i) {
  1271. while (!atomic_read(&cpu_in_kgdb[i]))
  1272. cpu_relax();
  1273. }
  1274. /*
  1275. * At this point the primary processor is completely
  1276. * in the debugger and all secondary CPUs are quiescent
  1277. */
  1278. kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
  1279. kgdb_deactivate_sw_breakpoints();
  1280. kgdb_single_step = 0;
  1281. kgdb_contthread = current;
  1282. exception_level = 0;
  1283. /* Talk to debugger with gdbserial protocol */
  1284. error = gdb_serial_stub(ks);
  1285. /* Call the I/O driver's post_exception routine */
  1286. if (kgdb_io_ops->post_exception)
  1287. kgdb_io_ops->post_exception();
  1288. atomic_dec(&cpu_in_kgdb[ks->cpu]);
  1289. if (!kgdb_single_step) {
  1290. for (i = NR_CPUS-1; i >= 0; i--)
  1291. atomic_dec(&passive_cpu_wait[i]);
  1292. /*
  1293. * Wait till all the CPUs have quit
  1294. * from the debugger.
  1295. */
  1296. for_each_online_cpu(i) {
  1297. while (atomic_read(&cpu_in_kgdb[i]))
  1298. cpu_relax();
  1299. }
  1300. }
  1301. kgdb_restore:
  1302. if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
  1303. int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
  1304. if (kgdb_info[sstep_cpu].task)
  1305. kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
  1306. else
  1307. kgdb_sstep_pid = 0;
  1308. }
  1309. /* Free kgdb_active */
  1310. atomic_set(&kgdb_active, -1);
  1311. touch_softlockup_watchdog_sync();
  1312. clocksource_touch_watchdog();
  1313. local_irq_restore(flags);
  1314. return error;
  1315. }
  1316. /*
  1317. * kgdb_handle_exception() - main entry point from a kernel exception
  1318. *
  1319. * Locking hierarchy:
  1320. * interface locks, if any (begin_session)
  1321. * kgdb lock (kgdb_active)
  1322. */
  1323. int
  1324. kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
  1325. {
  1326. struct kgdb_state kgdb_var;
  1327. struct kgdb_state *ks = &kgdb_var;
  1328. int ret;
  1329. ks->cpu = raw_smp_processor_id();
  1330. ks->ex_vector = evector;
  1331. ks->signo = signo;
  1332. ks->ex_vector = evector;
  1333. ks->err_code = ecode;
  1334. ks->kgdb_usethreadid = 0;
  1335. ks->linux_regs = regs;
  1336. if (kgdb_reenter_check(ks))
  1337. return 0; /* Ouch, double exception ! */
  1338. kgdb_info[ks->cpu].exception_state |= DCPU_WANT_MASTER;
  1339. ret = kgdb_cpu_enter(ks, regs);
  1340. kgdb_info[ks->cpu].exception_state &= ~DCPU_WANT_MASTER;
  1341. return ret;
  1342. }
  1343. int kgdb_nmicallback(int cpu, void *regs)
  1344. {
  1345. #ifdef CONFIG_SMP
  1346. struct kgdb_state kgdb_var;
  1347. struct kgdb_state *ks = &kgdb_var;
  1348. memset(ks, 0, sizeof(struct kgdb_state));
  1349. ks->cpu = cpu;
  1350. ks->linux_regs = regs;
  1351. if (!atomic_read(&cpu_in_kgdb[cpu]) &&
  1352. atomic_read(&kgdb_active) != -1 &&
  1353. atomic_read(&kgdb_active) != cpu) {
  1354. kgdb_info[cpu].exception_state |= DCPU_IS_SLAVE;
  1355. kgdb_cpu_enter(ks, regs);
  1356. kgdb_info[cpu].exception_state &= ~DCPU_IS_SLAVE;
  1357. return 0;
  1358. }
  1359. #endif
  1360. return 1;
  1361. }
  1362. static void kgdb_console_write(struct console *co, const char *s,
  1363. unsigned count)
  1364. {
  1365. unsigned long flags;
  1366. /* If we're debugging, or KGDB has not connected, don't try
  1367. * and print. */
  1368. if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
  1369. return;
  1370. local_irq_save(flags);
  1371. kgdb_msg_write(s, count);
  1372. local_irq_restore(flags);
  1373. }
  1374. static struct console kgdbcons = {
  1375. .name = "kgdb",
  1376. .write = kgdb_console_write,
  1377. .flags = CON_PRINTBUFFER | CON_ENABLED,
  1378. .index = -1,
  1379. };
  1380. #ifdef CONFIG_MAGIC_SYSRQ
  1381. static void sysrq_handle_gdb(int key, struct tty_struct *tty)
  1382. {
  1383. if (!kgdb_io_ops) {
  1384. printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
  1385. return;
  1386. }
  1387. if (!kgdb_connected)
  1388. printk(KERN_CRIT "Entering KGDB\n");
  1389. kgdb_breakpoint();
  1390. }
  1391. static struct sysrq_key_op sysrq_gdb_op = {
  1392. .handler = sysrq_handle_gdb,
  1393. .help_msg = "debug(G)",
  1394. .action_msg = "DEBUG",
  1395. };
  1396. #endif
  1397. static void kgdb_register_callbacks(void)
  1398. {
  1399. if (!kgdb_io_module_registered) {
  1400. kgdb_io_module_registered = 1;
  1401. kgdb_arch_init();
  1402. #ifdef CONFIG_MAGIC_SYSRQ
  1403. register_sysrq_key('g', &sysrq_gdb_op);
  1404. #endif
  1405. if (kgdb_use_con && !kgdb_con_registered) {
  1406. register_console(&kgdbcons);
  1407. kgdb_con_registered = 1;
  1408. }
  1409. }
  1410. }
  1411. static void kgdb_unregister_callbacks(void)
  1412. {
  1413. /*
  1414. * When this routine is called KGDB should unregister from the
  1415. * panic handler and clean up, making sure it is not handling any
  1416. * break exceptions at the time.
  1417. */
  1418. if (kgdb_io_module_registered) {
  1419. kgdb_io_module_registered = 0;
  1420. kgdb_arch_exit();
  1421. #ifdef CONFIG_MAGIC_SYSRQ
  1422. unregister_sysrq_key('g', &sysrq_gdb_op);
  1423. #endif
  1424. if (kgdb_con_registered) {
  1425. unregister_console(&kgdbcons);
  1426. kgdb_con_registered = 0;
  1427. }
  1428. }
  1429. }
  1430. static void kgdb_initial_breakpoint(void)
  1431. {
  1432. kgdb_break_asap = 0;
  1433. printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
  1434. kgdb_breakpoint();
  1435. }
  1436. /**
  1437. * kgdb_register_io_module - register KGDB IO module
  1438. * @new_kgdb_io_ops: the io ops vector
  1439. *
  1440. * Register it with the KGDB core.
  1441. */
  1442. int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
  1443. {
  1444. int err;
  1445. spin_lock(&kgdb_registration_lock);
  1446. if (kgdb_io_ops) {
  1447. spin_unlock(&kgdb_registration_lock);
  1448. printk(KERN_ERR "kgdb: Another I/O driver is already "
  1449. "registered with KGDB.\n");
  1450. return -EBUSY;
  1451. }
  1452. if (new_kgdb_io_ops->init) {
  1453. err = new_kgdb_io_ops->init();
  1454. if (err) {
  1455. spin_unlock(&kgdb_registration_lock);
  1456. return err;
  1457. }
  1458. }
  1459. kgdb_io_ops = new_kgdb_io_ops;
  1460. spin_unlock(&kgdb_registration_lock);
  1461. printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
  1462. new_kgdb_io_ops->name);
  1463. /* Arm KGDB now. */
  1464. kgdb_register_callbacks();
  1465. if (kgdb_break_asap)
  1466. kgdb_initial_breakpoint();
  1467. return 0;
  1468. }
  1469. EXPORT_SYMBOL_GPL(kgdb_register_io_module);
  1470. /**
  1471. * kkgdb_unregister_io_module - unregister KGDB IO module
  1472. * @old_kgdb_io_ops: the io ops vector
  1473. *
  1474. * Unregister it with the KGDB core.
  1475. */
  1476. void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
  1477. {
  1478. BUG_ON(kgdb_connected);
  1479. /*
  1480. * KGDB is no longer able to communicate out, so
  1481. * unregister our callbacks and reset state.
  1482. */
  1483. kgdb_unregister_callbacks();
  1484. spin_lock(&kgdb_registration_lock);
  1485. WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
  1486. kgdb_io_ops = NULL;
  1487. spin_unlock(&kgdb_registration_lock);
  1488. printk(KERN_INFO
  1489. "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
  1490. old_kgdb_io_ops->name);
  1491. }
  1492. EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
  1493. /**
  1494. * kgdb_breakpoint - generate breakpoint exception
  1495. *
  1496. * This function will generate a breakpoint exception. It is used at the
  1497. * beginning of a program to sync up with a debugger and can be used
  1498. * otherwise as a quick means to stop program execution and "break" into
  1499. * the debugger.
  1500. */
  1501. void kgdb_breakpoint(void)
  1502. {
  1503. atomic_inc(&kgdb_setting_breakpoint);
  1504. wmb(); /* Sync point before breakpoint */
  1505. arch_kgdb_breakpoint();
  1506. wmb(); /* Sync point after breakpoint */
  1507. atomic_dec(&kgdb_setting_breakpoint);
  1508. }
  1509. EXPORT_SYMBOL_GPL(kgdb_breakpoint);
  1510. static int __init opt_kgdb_wait(char *str)
  1511. {
  1512. kgdb_break_asap = 1;
  1513. if (kgdb_io_module_registered)
  1514. kgdb_initial_breakpoint();
  1515. return 0;
  1516. }
  1517. early_param("kgdbwait", opt_kgdb_wait);