kgdb.c 39 KB

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