kgdb.c 38 KB

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