kgdb.c 39 KB

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