kgdb.c 39 KB

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