kgdb.c 38 KB

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