kgdb.c 37 KB

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