kgdb.c 38 KB

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