kgdb.c 38 KB

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