gdb-stub.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. /* gdb-stub.c: FRV GDB stub
  2. *
  3. * Copyright (C) 2003,4 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from Linux/MIPS version, Copyright (C) 1995 Andreas Busse
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. /*
  13. * To enable debugger support, two things need to happen. One, a
  14. * call to set_debug_traps() is necessary in order to allow any breakpoints
  15. * or error conditions to be properly intercepted and reported to gdb.
  16. * Two, a breakpoint needs to be generated to begin communication. This
  17. * is most easily accomplished by a call to breakpoint(). Breakpoint()
  18. * simulates a breakpoint by executing a BREAK instruction.
  19. *
  20. *
  21. * The following gdb commands are supported:
  22. *
  23. * command function Return value
  24. *
  25. * g return the value of the CPU registers hex data or ENN
  26. * G set the value of the CPU registers OK or ENN
  27. *
  28. * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
  29. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
  30. *
  31. * c Resume at current address SNN ( signal NN)
  32. * cAA..AA Continue at address AA..AA SNN
  33. *
  34. * s Step one instruction SNN
  35. * sAA..AA Step one instruction from AA..AA SNN
  36. *
  37. * k kill
  38. *
  39. * ? What was the last sigval ? SNN (signal NN)
  40. *
  41. * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
  42. * baud rate
  43. *
  44. * All commands and responses are sent with a packet which includes a
  45. * checksum. A packet consists of
  46. *
  47. * $<packet info>#<checksum>.
  48. *
  49. * where
  50. * <packet info> :: <characters representing the command or response>
  51. * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
  52. *
  53. * When a packet is received, it is first acknowledged with either '+' or '-'.
  54. * '+' indicates a successful transfer. '-' indicates a failed transfer.
  55. *
  56. * Example:
  57. *
  58. * Host: Reply:
  59. * $m0,10#2a +$00010203040506070809101112131415#42
  60. *
  61. *
  62. * ==============
  63. * MORE EXAMPLES:
  64. * ==============
  65. *
  66. * For reference -- the following are the steps that one
  67. * company took (RidgeRun Inc) to get remote gdb debugging
  68. * going. In this scenario the host machine was a PC and the
  69. * target platform was a Galileo EVB64120A MIPS evaluation
  70. * board.
  71. *
  72. * Step 1:
  73. * First download gdb-5.0.tar.gz from the internet.
  74. * and then build/install the package.
  75. *
  76. * Example:
  77. * $ tar zxf gdb-5.0.tar.gz
  78. * $ cd gdb-5.0
  79. * $ ./configure --target=frv-elf-gdb
  80. * $ make
  81. * $ frv-elf-gdb
  82. *
  83. * Step 2:
  84. * Configure linux for remote debugging and build it.
  85. *
  86. * Example:
  87. * $ cd ~/linux
  88. * $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
  89. * $ make vmlinux
  90. *
  91. * Step 3:
  92. * Download the kernel to the remote target and start
  93. * the kernel running. It will promptly halt and wait
  94. * for the host gdb session to connect. It does this
  95. * since the "Kernel Hacking" option has defined
  96. * CONFIG_REMOTE_DEBUG which in turn enables your calls
  97. * to:
  98. * set_debug_traps();
  99. * breakpoint();
  100. *
  101. * Step 4:
  102. * Start the gdb session on the host.
  103. *
  104. * Example:
  105. * $ frv-elf-gdb vmlinux
  106. * (gdb) set remotebaud 115200
  107. * (gdb) target remote /dev/ttyS1
  108. * ...at this point you are connected to
  109. * the remote target and can use gdb
  110. * in the normal fasion. Setting
  111. * breakpoints, single stepping,
  112. * printing variables, etc.
  113. *
  114. */
  115. #include <linux/string.h>
  116. #include <linux/kernel.h>
  117. #include <linux/signal.h>
  118. #include <linux/sched.h>
  119. #include <linux/mm.h>
  120. #include <linux/console.h>
  121. #include <linux/init.h>
  122. #include <linux/slab.h>
  123. #include <linux/nmi.h>
  124. #include <asm/asm-offsets.h>
  125. #include <asm/pgtable.h>
  126. #include <asm/system.h>
  127. #include <asm/gdb-stub.h>
  128. #define LEDS(x) do { /* *(u32*)0xe1200004 = ~(x); mb(); */ } while(0)
  129. #undef GDBSTUB_DEBUG_PROTOCOL
  130. extern void debug_to_serial(const char *p, int n);
  131. extern void gdbstub_console_write(struct console *co, const char *p, unsigned n);
  132. extern volatile uint32_t __break_error_detect[3]; /* ESFR1, ESR15, EAR15 */
  133. struct __debug_amr {
  134. unsigned long L, P;
  135. } __attribute__((aligned(8)));
  136. struct __debug_mmu {
  137. struct {
  138. unsigned long hsr0, pcsr, esr0, ear0, epcr0;
  139. #ifdef CONFIG_MMU
  140. unsigned long tplr, tppr, tpxr, cxnr;
  141. #endif
  142. } regs;
  143. struct __debug_amr iamr[16];
  144. struct __debug_amr damr[16];
  145. #ifdef CONFIG_MMU
  146. struct __debug_amr tlb[64*2];
  147. #endif
  148. };
  149. static struct __debug_mmu __debug_mmu;
  150. /*
  151. * BUFMAX defines the maximum number of characters in inbound/outbound buffers
  152. * at least NUMREGBYTES*2 are needed for register packets
  153. */
  154. #define BUFMAX 2048
  155. #define BREAK_INSN 0x801000c0 /* use "break" as bkpt */
  156. static const char gdbstub_banner[] = "Linux/FR-V GDB Stub (c) RedHat 2003\n";
  157. volatile u8 gdbstub_rx_buffer[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
  158. volatile u32 gdbstub_rx_inp = 0;
  159. volatile u32 gdbstub_rx_outp = 0;
  160. volatile u8 gdbstub_rx_overflow = 0;
  161. u8 gdbstub_rx_unget = 0;
  162. /* set with GDB whilst running to permit step through exceptions */
  163. extern volatile u32 __attribute__((section(".bss"))) gdbstub_trace_through_exceptions;
  164. static char input_buffer[BUFMAX];
  165. static char output_buffer[BUFMAX];
  166. static const char *regnames[] = {
  167. "PSR ", "ISR ", "CCR ", "CCCR",
  168. "LR ", "LCR ", "PC ", "_stt",
  169. "sys ", "GR8*", "GNE0", "GNE1",
  170. "IACH", "IACL",
  171. "TBR ", "SP ", "FP ", "GR3 ",
  172. "GR4 ", "GR5 ", "GR6 ", "GR7 ",
  173. "GR8 ", "GR9 ", "GR10", "GR11",
  174. "GR12", "GR13", "GR14", "GR15",
  175. "GR16", "GR17", "GR18", "GR19",
  176. "GR20", "GR21", "GR22", "GR23",
  177. "GR24", "GR25", "GR26", "GR27",
  178. "EFRM", "CURR", "GR30", "BFRM"
  179. };
  180. struct gdbstub_bkpt {
  181. unsigned long addr; /* address of breakpoint */
  182. unsigned len; /* size of breakpoint */
  183. uint32_t originsns[7]; /* original instructions */
  184. };
  185. static struct gdbstub_bkpt gdbstub_bkpts[256];
  186. /*
  187. * local prototypes
  188. */
  189. static void gdbstub_recv_packet(char *buffer);
  190. static int gdbstub_send_packet(char *buffer);
  191. static int gdbstub_compute_signal(unsigned long tbr);
  192. static int hex(unsigned char ch);
  193. static int hexToInt(char **ptr, unsigned long *intValue);
  194. static unsigned char *mem2hex(const void *mem, char *buf, int count, int may_fault);
  195. static char *hex2mem(const char *buf, void *_mem, int count);
  196. /*
  197. * Convert ch from a hex digit to an int
  198. */
  199. static int hex(unsigned char ch)
  200. {
  201. if (ch >= 'a' && ch <= 'f')
  202. return ch-'a'+10;
  203. if (ch >= '0' && ch <= '9')
  204. return ch-'0';
  205. if (ch >= 'A' && ch <= 'F')
  206. return ch-'A'+10;
  207. return -1;
  208. }
  209. void gdbstub_printk(const char *fmt, ...)
  210. {
  211. static char buf[1024];
  212. va_list args;
  213. int len;
  214. /* Emit the output into the temporary buffer */
  215. va_start(args, fmt);
  216. len = vsnprintf(buf, sizeof(buf), fmt, args);
  217. va_end(args);
  218. debug_to_serial(buf, len);
  219. }
  220. static inline char *gdbstub_strcpy(char *dst, const char *src)
  221. {
  222. int loop = 0;
  223. while ((dst[loop] = src[loop]))
  224. loop++;
  225. return dst;
  226. }
  227. static void gdbstub_purge_cache(void)
  228. {
  229. asm volatile(" dcef @(gr0,gr0),#1 \n"
  230. " icei @(gr0,gr0),#1 \n"
  231. " membar \n"
  232. " bar \n"
  233. );
  234. }
  235. /*****************************************************************************/
  236. /*
  237. * scan for the sequence $<data>#<checksum>
  238. */
  239. static void gdbstub_recv_packet(char *buffer)
  240. {
  241. unsigned char checksum;
  242. unsigned char xmitcsum;
  243. unsigned char ch;
  244. int count, i, ret, error;
  245. for (;;) {
  246. /* wait around for the start character, ignore all other characters */
  247. do {
  248. gdbstub_rx_char(&ch, 0);
  249. } while (ch != '$');
  250. checksum = 0;
  251. xmitcsum = -1;
  252. count = 0;
  253. error = 0;
  254. /* now, read until a # or end of buffer is found */
  255. while (count < BUFMAX) {
  256. ret = gdbstub_rx_char(&ch, 0);
  257. if (ret < 0)
  258. error = ret;
  259. if (ch == '#')
  260. break;
  261. checksum += ch;
  262. buffer[count] = ch;
  263. count++;
  264. }
  265. if (error == -EIO) {
  266. gdbstub_proto("### GDB Rx Error - Skipping packet ###\n");
  267. gdbstub_proto("### GDB Tx NAK\n");
  268. gdbstub_tx_char('-');
  269. continue;
  270. }
  271. if (count >= BUFMAX || error)
  272. continue;
  273. buffer[count] = 0;
  274. /* read the checksum */
  275. ret = gdbstub_rx_char(&ch, 0);
  276. if (ret < 0)
  277. error = ret;
  278. xmitcsum = hex(ch) << 4;
  279. ret = gdbstub_rx_char(&ch, 0);
  280. if (ret < 0)
  281. error = ret;
  282. xmitcsum |= hex(ch);
  283. if (error) {
  284. if (error == -EIO)
  285. gdbstub_proto("### GDB Rx Error - Skipping packet\n");
  286. gdbstub_proto("### GDB Tx NAK\n");
  287. gdbstub_tx_char('-');
  288. continue;
  289. }
  290. /* check the checksum */
  291. if (checksum != xmitcsum) {
  292. gdbstub_proto("### GDB Tx NAK\n");
  293. gdbstub_tx_char('-'); /* failed checksum */
  294. continue;
  295. }
  296. gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
  297. gdbstub_proto("### GDB Tx ACK\n");
  298. gdbstub_tx_char('+'); /* successful transfer */
  299. /* if a sequence char is present, reply the sequence ID */
  300. if (buffer[2] == ':') {
  301. gdbstub_tx_char(buffer[0]);
  302. gdbstub_tx_char(buffer[1]);
  303. /* remove sequence chars from buffer */
  304. count = 0;
  305. while (buffer[count]) count++;
  306. for (i=3; i <= count; i++)
  307. buffer[i - 3] = buffer[i];
  308. }
  309. break;
  310. }
  311. } /* end gdbstub_recv_packet() */
  312. /*****************************************************************************/
  313. /*
  314. * send the packet in buffer.
  315. * - return 0 if successfully ACK'd
  316. * - return 1 if abandoned due to new incoming packet
  317. */
  318. static int gdbstub_send_packet(char *buffer)
  319. {
  320. unsigned char checksum;
  321. int count;
  322. unsigned char ch;
  323. /* $<packet info>#<checksum> */
  324. gdbstub_proto("### GDB Tx '%s' ###\n", buffer);
  325. do {
  326. gdbstub_tx_char('$');
  327. checksum = 0;
  328. count = 0;
  329. while ((ch = buffer[count]) != 0) {
  330. gdbstub_tx_char(ch);
  331. checksum += ch;
  332. count += 1;
  333. }
  334. gdbstub_tx_char('#');
  335. gdbstub_tx_char(hex_asc_hi(checksum));
  336. gdbstub_tx_char(hex_asc_lo(checksum));
  337. } while (gdbstub_rx_char(&ch,0),
  338. #ifdef GDBSTUB_DEBUG_PROTOCOL
  339. ch=='-' && (gdbstub_proto("### GDB Rx NAK\n"),0),
  340. ch!='-' && ch!='+' && (gdbstub_proto("### GDB Rx ??? %02x\n",ch),0),
  341. #endif
  342. ch!='+' && ch!='$');
  343. if (ch=='+') {
  344. gdbstub_proto("### GDB Rx ACK\n");
  345. return 0;
  346. }
  347. gdbstub_proto("### GDB Tx Abandoned\n");
  348. gdbstub_rx_unget = ch;
  349. return 1;
  350. } /* end gdbstub_send_packet() */
  351. /*
  352. * While we find nice hex chars, build an int.
  353. * Return number of chars processed.
  354. */
  355. static int hexToInt(char **ptr, unsigned long *_value)
  356. {
  357. int count = 0, ch;
  358. *_value = 0;
  359. while (**ptr) {
  360. ch = hex(**ptr);
  361. if (ch < 0)
  362. break;
  363. *_value = (*_value << 4) | ((uint8_t) ch & 0xf);
  364. count++;
  365. (*ptr)++;
  366. }
  367. return count;
  368. }
  369. /*****************************************************************************/
  370. /*
  371. * probe an address to see whether it maps to anything
  372. */
  373. static inline int gdbstub_addr_probe(const void *vaddr)
  374. {
  375. #ifdef CONFIG_MMU
  376. unsigned long paddr;
  377. asm("lrad %1,%0,#1,#0,#0" : "=r"(paddr) : "r"(vaddr));
  378. if (!(paddr & xAMPRx_V))
  379. return 0;
  380. #endif
  381. return 1;
  382. } /* end gdbstub_addr_probe() */
  383. #ifdef CONFIG_MMU
  384. static unsigned long __saved_dampr, __saved_damlr;
  385. static inline unsigned long gdbstub_virt_to_pte(unsigned long vaddr)
  386. {
  387. pgd_t *pgd;
  388. pud_t *pud;
  389. pmd_t *pmd;
  390. pte_t *pte;
  391. unsigned long val, dampr5;
  392. pgd = (pgd_t *) __get_DAMLR(3) + pgd_index(vaddr);
  393. pud = pud_offset(pgd, vaddr);
  394. pmd = pmd_offset(pud, vaddr);
  395. if (pmd_bad(*pmd) || !pmd_present(*pmd))
  396. return 0;
  397. /* make sure dampr5 maps to the correct pmd */
  398. dampr5 = __get_DAMPR(5);
  399. val = pmd_val(*pmd);
  400. __set_DAMPR(5, val | xAMPRx_L | xAMPRx_SS_16Kb | xAMPRx_S | xAMPRx_C | xAMPRx_V);
  401. /* now its safe to access pmd */
  402. pte = (pte_t *)__get_DAMLR(5) + __pte_index(vaddr);
  403. if (pte_present(*pte))
  404. val = pte_val(*pte);
  405. else
  406. val = 0;
  407. /* restore original dampr5 */
  408. __set_DAMPR(5, dampr5);
  409. return val;
  410. }
  411. #endif
  412. static inline int gdbstub_addr_map(const void *vaddr)
  413. {
  414. #ifdef CONFIG_MMU
  415. unsigned long pte;
  416. __saved_dampr = __get_DAMPR(2);
  417. __saved_damlr = __get_DAMLR(2);
  418. #endif
  419. if (gdbstub_addr_probe(vaddr))
  420. return 1;
  421. #ifdef CONFIG_MMU
  422. pte = gdbstub_virt_to_pte((unsigned long) vaddr);
  423. if (pte) {
  424. __set_DAMPR(2, pte);
  425. __set_DAMLR(2, (unsigned long) vaddr & PAGE_MASK);
  426. return 1;
  427. }
  428. #endif
  429. return 0;
  430. }
  431. static inline void gdbstub_addr_unmap(void)
  432. {
  433. #ifdef CONFIG_MMU
  434. __set_DAMPR(2, __saved_dampr);
  435. __set_DAMLR(2, __saved_damlr);
  436. #endif
  437. }
  438. /*
  439. * access potentially dodgy memory through a potentially dodgy pointer
  440. */
  441. static inline int gdbstub_read_dword(const void *addr, uint32_t *_res)
  442. {
  443. unsigned long brr;
  444. uint32_t res;
  445. if (!gdbstub_addr_map(addr))
  446. return 0;
  447. asm volatile(" movgs gr0,brr \n"
  448. " ld%I2 %M2,%0 \n"
  449. " movsg brr,%1 \n"
  450. : "=r"(res), "=r"(brr)
  451. : "m"(*(uint32_t *) addr));
  452. *_res = res;
  453. gdbstub_addr_unmap();
  454. return likely(!brr);
  455. }
  456. static inline int gdbstub_write_dword(void *addr, uint32_t val)
  457. {
  458. unsigned long brr;
  459. if (!gdbstub_addr_map(addr))
  460. return 0;
  461. asm volatile(" movgs gr0,brr \n"
  462. " st%I2 %1,%M2 \n"
  463. " movsg brr,%0 \n"
  464. : "=r"(brr)
  465. : "r"(val), "m"(*(uint32_t *) addr));
  466. gdbstub_addr_unmap();
  467. return likely(!brr);
  468. }
  469. static inline int gdbstub_read_word(const void *addr, uint16_t *_res)
  470. {
  471. unsigned long brr;
  472. uint16_t res;
  473. if (!gdbstub_addr_map(addr))
  474. return 0;
  475. asm volatile(" movgs gr0,brr \n"
  476. " lduh%I2 %M2,%0 \n"
  477. " movsg brr,%1 \n"
  478. : "=r"(res), "=r"(brr)
  479. : "m"(*(uint16_t *) addr));
  480. *_res = res;
  481. gdbstub_addr_unmap();
  482. return likely(!brr);
  483. }
  484. static inline int gdbstub_write_word(void *addr, uint16_t val)
  485. {
  486. unsigned long brr;
  487. if (!gdbstub_addr_map(addr))
  488. return 0;
  489. asm volatile(" movgs gr0,brr \n"
  490. " sth%I2 %1,%M2 \n"
  491. " movsg brr,%0 \n"
  492. : "=r"(brr)
  493. : "r"(val), "m"(*(uint16_t *) addr));
  494. gdbstub_addr_unmap();
  495. return likely(!brr);
  496. }
  497. static inline int gdbstub_read_byte(const void *addr, uint8_t *_res)
  498. {
  499. unsigned long brr;
  500. uint8_t res;
  501. if (!gdbstub_addr_map(addr))
  502. return 0;
  503. asm volatile(" movgs gr0,brr \n"
  504. " ldub%I2 %M2,%0 \n"
  505. " movsg brr,%1 \n"
  506. : "=r"(res), "=r"(brr)
  507. : "m"(*(uint8_t *) addr));
  508. *_res = res;
  509. gdbstub_addr_unmap();
  510. return likely(!brr);
  511. }
  512. static inline int gdbstub_write_byte(void *addr, uint8_t val)
  513. {
  514. unsigned long brr;
  515. if (!gdbstub_addr_map(addr))
  516. return 0;
  517. asm volatile(" movgs gr0,brr \n"
  518. " stb%I2 %1,%M2 \n"
  519. " movsg brr,%0 \n"
  520. : "=r"(brr)
  521. : "r"(val), "m"(*(uint8_t *) addr));
  522. gdbstub_addr_unmap();
  523. return likely(!brr);
  524. }
  525. static void __gdbstub_console_write(struct console *co, const char *p, unsigned n)
  526. {
  527. char outbuf[26];
  528. int qty;
  529. outbuf[0] = 'O';
  530. while (n > 0) {
  531. qty = 1;
  532. while (n > 0 && qty < 20) {
  533. mem2hex(p, outbuf + qty, 2, 0);
  534. qty += 2;
  535. if (*p == 0x0a) {
  536. outbuf[qty++] = '0';
  537. outbuf[qty++] = 'd';
  538. }
  539. p++;
  540. n--;
  541. }
  542. outbuf[qty] = 0;
  543. gdbstub_send_packet(outbuf);
  544. }
  545. }
  546. #if 0
  547. void debug_to_serial(const char *p, int n)
  548. {
  549. gdbstub_console_write(NULL,p,n);
  550. }
  551. #endif
  552. #ifdef CONFIG_GDB_CONSOLE
  553. static struct console gdbstub_console = {
  554. .name = "gdb",
  555. .write = gdbstub_console_write, /* in break.S */
  556. .flags = CON_PRINTBUFFER,
  557. .index = -1,
  558. };
  559. #endif
  560. /*****************************************************************************/
  561. /*
  562. * Convert the memory pointed to by mem into hex, placing result in buf.
  563. * - if successful, return a pointer to the last char put in buf (NUL)
  564. * - in case of mem fault, return NULL
  565. * may_fault is non-zero if we are reading from arbitrary memory, but is currently
  566. * not used.
  567. */
  568. static unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
  569. {
  570. const uint8_t *mem = _mem;
  571. uint8_t ch[4] __attribute__((aligned(4)));
  572. if ((uint32_t)mem&1 && count>=1) {
  573. if (!gdbstub_read_byte(mem,ch))
  574. return NULL;
  575. buf = pack_hex_byte(buf, ch[0]);
  576. mem++;
  577. count--;
  578. }
  579. if ((uint32_t)mem&3 && count>=2) {
  580. if (!gdbstub_read_word(mem,(uint16_t *)ch))
  581. return NULL;
  582. buf = pack_hex_byte(buf, ch[0]);
  583. buf = pack_hex_byte(buf, ch[1]);
  584. mem += 2;
  585. count -= 2;
  586. }
  587. while (count>=4) {
  588. if (!gdbstub_read_dword(mem,(uint32_t *)ch))
  589. return NULL;
  590. buf = pack_hex_byte(buf, ch[0]);
  591. buf = pack_hex_byte(buf, ch[1]);
  592. buf = pack_hex_byte(buf, ch[2]);
  593. buf = pack_hex_byte(buf, ch[3]);
  594. mem += 4;
  595. count -= 4;
  596. }
  597. if (count>=2) {
  598. if (!gdbstub_read_word(mem,(uint16_t *)ch))
  599. return NULL;
  600. buf = pack_hex_byte(buf, ch[0]);
  601. buf = pack_hex_byte(buf, ch[1]);
  602. mem += 2;
  603. count -= 2;
  604. }
  605. if (count>=1) {
  606. if (!gdbstub_read_byte(mem,ch))
  607. return NULL;
  608. buf = pack_hex_byte(buf, ch[0]);
  609. }
  610. *buf = 0;
  611. return buf;
  612. } /* end mem2hex() */
  613. /*****************************************************************************/
  614. /*
  615. * convert the hex array pointed to by buf into binary to be placed in mem
  616. * return a pointer to the character AFTER the last byte of buffer consumed
  617. */
  618. static char *hex2mem(const char *buf, void *_mem, int count)
  619. {
  620. uint8_t *mem = _mem;
  621. union {
  622. uint32_t l;
  623. uint16_t w;
  624. uint8_t b[4];
  625. } ch;
  626. if ((u32)mem&1 && count>=1) {
  627. ch.b[0] = hex(*buf++) << 4;
  628. ch.b[0] |= hex(*buf++);
  629. if (!gdbstub_write_byte(mem,ch.b[0]))
  630. return NULL;
  631. mem++;
  632. count--;
  633. }
  634. if ((u32)mem&3 && count>=2) {
  635. ch.b[0] = hex(*buf++) << 4;
  636. ch.b[0] |= hex(*buf++);
  637. ch.b[1] = hex(*buf++) << 4;
  638. ch.b[1] |= hex(*buf++);
  639. if (!gdbstub_write_word(mem,ch.w))
  640. return NULL;
  641. mem += 2;
  642. count -= 2;
  643. }
  644. while (count>=4) {
  645. ch.b[0] = hex(*buf++) << 4;
  646. ch.b[0] |= hex(*buf++);
  647. ch.b[1] = hex(*buf++) << 4;
  648. ch.b[1] |= hex(*buf++);
  649. ch.b[2] = hex(*buf++) << 4;
  650. ch.b[2] |= hex(*buf++);
  651. ch.b[3] = hex(*buf++) << 4;
  652. ch.b[3] |= hex(*buf++);
  653. if (!gdbstub_write_dword(mem,ch.l))
  654. return NULL;
  655. mem += 4;
  656. count -= 4;
  657. }
  658. if (count>=2) {
  659. ch.b[0] = hex(*buf++) << 4;
  660. ch.b[0] |= hex(*buf++);
  661. ch.b[1] = hex(*buf++) << 4;
  662. ch.b[1] |= hex(*buf++);
  663. if (!gdbstub_write_word(mem,ch.w))
  664. return NULL;
  665. mem += 2;
  666. count -= 2;
  667. }
  668. if (count>=1) {
  669. ch.b[0] = hex(*buf++) << 4;
  670. ch.b[0] |= hex(*buf++);
  671. if (!gdbstub_write_byte(mem,ch.b[0]))
  672. return NULL;
  673. }
  674. return (char *) buf;
  675. } /* end hex2mem() */
  676. /*****************************************************************************/
  677. /*
  678. * This table contains the mapping between FRV TBR.TT exception codes,
  679. * and signals, which are primarily what GDB understands. It also
  680. * indicates which hardware traps we need to commandeer when
  681. * initializing the stub.
  682. */
  683. static const struct brr_to_sig_map {
  684. unsigned long brr_mask; /* BRR bitmask */
  685. unsigned long tbr_tt; /* TBR.TT code (in BRR.EBTT) */
  686. unsigned int signo; /* Signal that we map this into */
  687. } brr_to_sig_map[] = {
  688. { BRR_EB, TBR_TT_INSTR_ACC_ERROR, SIGSEGV },
  689. { BRR_EB, TBR_TT_ILLEGAL_INSTR, SIGILL },
  690. { BRR_EB, TBR_TT_PRIV_INSTR, SIGILL },
  691. { BRR_EB, TBR_TT_MP_EXCEPTION, SIGFPE },
  692. { BRR_EB, TBR_TT_DATA_ACC_ERROR, SIGSEGV },
  693. { BRR_EB, TBR_TT_DATA_STR_ERROR, SIGSEGV },
  694. { BRR_EB, TBR_TT_DIVISION_EXCEP, SIGFPE },
  695. { BRR_EB, TBR_TT_COMPOUND_EXCEP, SIGSEGV },
  696. { BRR_EB, TBR_TT_INTERRUPT_13, SIGALRM }, /* watchdog */
  697. { BRR_EB, TBR_TT_INTERRUPT_14, SIGINT }, /* GDB serial */
  698. { BRR_EB, TBR_TT_INTERRUPT_15, SIGQUIT }, /* NMI */
  699. { BRR_CB, 0, SIGUSR1 },
  700. { BRR_TB, 0, SIGUSR2 },
  701. { BRR_DBNEx, 0, SIGTRAP },
  702. { BRR_DBx, 0, SIGTRAP }, /* h/w watchpoint */
  703. { BRR_IBx, 0, SIGTRAP }, /* h/w breakpoint */
  704. { BRR_CBB, 0, SIGTRAP },
  705. { BRR_SB, 0, SIGTRAP },
  706. { BRR_ST, 0, SIGTRAP }, /* single step */
  707. { 0, 0, SIGHUP } /* default */
  708. };
  709. /*****************************************************************************/
  710. /*
  711. * convert the FRV BRR register contents into a UNIX signal number
  712. */
  713. static inline int gdbstub_compute_signal(unsigned long brr)
  714. {
  715. const struct brr_to_sig_map *map;
  716. unsigned long tbr = (brr & BRR_EBTT) >> 12;
  717. for (map = brr_to_sig_map; map->brr_mask; map++)
  718. if (map->brr_mask & brr)
  719. if (!map->tbr_tt || map->tbr_tt == tbr)
  720. break;
  721. return map->signo;
  722. } /* end gdbstub_compute_signal() */
  723. /*****************************************************************************/
  724. /*
  725. * set a software breakpoint or a hardware breakpoint or watchpoint
  726. */
  727. static int gdbstub_set_breakpoint(unsigned long type, unsigned long addr, unsigned long len)
  728. {
  729. unsigned long tmp;
  730. int bkpt, loop, xloop;
  731. union {
  732. struct {
  733. unsigned long mask0, mask1;
  734. };
  735. uint8_t bytes[8];
  736. } dbmr;
  737. //gdbstub_printk("setbkpt(%ld,%08lx,%ld)\n", type, addr, len);
  738. switch (type) {
  739. /* set software breakpoint */
  740. case 0:
  741. if (addr & 3 || len > 7*4)
  742. return -EINVAL;
  743. for (bkpt = 255; bkpt >= 0; bkpt--)
  744. if (!gdbstub_bkpts[bkpt].addr)
  745. break;
  746. if (bkpt < 0)
  747. return -ENOSPC;
  748. for (loop = 0; loop < len/4; loop++)
  749. if (!gdbstub_read_dword(&((uint32_t *) addr)[loop],
  750. &gdbstub_bkpts[bkpt].originsns[loop]))
  751. return -EFAULT;
  752. for (loop = 0; loop < len/4; loop++)
  753. if (!gdbstub_write_dword(&((uint32_t *) addr)[loop],
  754. BREAK_INSN)
  755. ) {
  756. /* need to undo the changes if possible */
  757. for (xloop = 0; xloop < loop; xloop++)
  758. gdbstub_write_dword(&((uint32_t *) addr)[xloop],
  759. gdbstub_bkpts[bkpt].originsns[xloop]);
  760. return -EFAULT;
  761. }
  762. gdbstub_bkpts[bkpt].addr = addr;
  763. gdbstub_bkpts[bkpt].len = len;
  764. #if 0
  765. gdbstub_printk("Set BKPT[%02x]: %08lx #%d {%04x, %04x} -> { %04x, %04x }\n",
  766. bkpt,
  767. gdbstub_bkpts[bkpt].addr,
  768. gdbstub_bkpts[bkpt].len,
  769. gdbstub_bkpts[bkpt].originsns[0],
  770. gdbstub_bkpts[bkpt].originsns[1],
  771. ((uint32_t *) addr)[0],
  772. ((uint32_t *) addr)[1]
  773. );
  774. #endif
  775. return 0;
  776. /* set hardware breakpoint */
  777. case 1:
  778. if (addr & 3 || len != 4)
  779. return -EINVAL;
  780. if (!(__debug_regs->dcr & DCR_IBE0)) {
  781. //gdbstub_printk("set h/w break 0: %08lx\n", addr);
  782. __debug_regs->dcr |= DCR_IBE0;
  783. __debug_regs->ibar[0] = addr;
  784. asm volatile("movgs %0,ibar0" : : "r"(addr));
  785. return 0;
  786. }
  787. if (!(__debug_regs->dcr & DCR_IBE1)) {
  788. //gdbstub_printk("set h/w break 1: %08lx\n", addr);
  789. __debug_regs->dcr |= DCR_IBE1;
  790. __debug_regs->ibar[1] = addr;
  791. asm volatile("movgs %0,ibar1" : : "r"(addr));
  792. return 0;
  793. }
  794. if (!(__debug_regs->dcr & DCR_IBE2)) {
  795. //gdbstub_printk("set h/w break 2: %08lx\n", addr);
  796. __debug_regs->dcr |= DCR_IBE2;
  797. __debug_regs->ibar[2] = addr;
  798. asm volatile("movgs %0,ibar2" : : "r"(addr));
  799. return 0;
  800. }
  801. if (!(__debug_regs->dcr & DCR_IBE3)) {
  802. //gdbstub_printk("set h/w break 3: %08lx\n", addr);
  803. __debug_regs->dcr |= DCR_IBE3;
  804. __debug_regs->ibar[3] = addr;
  805. asm volatile("movgs %0,ibar3" : : "r"(addr));
  806. return 0;
  807. }
  808. return -ENOSPC;
  809. /* set data read/write/access watchpoint */
  810. case 2:
  811. case 3:
  812. case 4:
  813. if ((addr & ~7) != ((addr + len - 1) & ~7))
  814. return -EINVAL;
  815. tmp = addr & 7;
  816. memset(dbmr.bytes, 0xff, sizeof(dbmr.bytes));
  817. for (loop = 0; loop < len; loop++)
  818. dbmr.bytes[tmp + loop] = 0;
  819. addr &= ~7;
  820. if (!(__debug_regs->dcr & (DCR_DRBE0|DCR_DWBE0))) {
  821. //gdbstub_printk("set h/w watchpoint 0 type %ld: %08lx\n", type, addr);
  822. tmp = type==2 ? DCR_DWBE0 : type==3 ? DCR_DRBE0 : DCR_DRBE0|DCR_DWBE0;
  823. __debug_regs->dcr |= tmp;
  824. __debug_regs->dbar[0] = addr;
  825. __debug_regs->dbmr[0][0] = dbmr.mask0;
  826. __debug_regs->dbmr[0][1] = dbmr.mask1;
  827. __debug_regs->dbdr[0][0] = 0;
  828. __debug_regs->dbdr[0][1] = 0;
  829. asm volatile(" movgs %0,dbar0 \n"
  830. " movgs %1,dbmr00 \n"
  831. " movgs %2,dbmr01 \n"
  832. " movgs gr0,dbdr00 \n"
  833. " movgs gr0,dbdr01 \n"
  834. : : "r"(addr), "r"(dbmr.mask0), "r"(dbmr.mask1));
  835. return 0;
  836. }
  837. if (!(__debug_regs->dcr & (DCR_DRBE1|DCR_DWBE1))) {
  838. //gdbstub_printk("set h/w watchpoint 1 type %ld: %08lx\n", type, addr);
  839. tmp = type==2 ? DCR_DWBE1 : type==3 ? DCR_DRBE1 : DCR_DRBE1|DCR_DWBE1;
  840. __debug_regs->dcr |= tmp;
  841. __debug_regs->dbar[1] = addr;
  842. __debug_regs->dbmr[1][0] = dbmr.mask0;
  843. __debug_regs->dbmr[1][1] = dbmr.mask1;
  844. __debug_regs->dbdr[1][0] = 0;
  845. __debug_regs->dbdr[1][1] = 0;
  846. asm volatile(" movgs %0,dbar1 \n"
  847. " movgs %1,dbmr10 \n"
  848. " movgs %2,dbmr11 \n"
  849. " movgs gr0,dbdr10 \n"
  850. " movgs gr0,dbdr11 \n"
  851. : : "r"(addr), "r"(dbmr.mask0), "r"(dbmr.mask1));
  852. return 0;
  853. }
  854. return -ENOSPC;
  855. default:
  856. return -EINVAL;
  857. }
  858. } /* end gdbstub_set_breakpoint() */
  859. /*****************************************************************************/
  860. /*
  861. * clear a breakpoint or watchpoint
  862. */
  863. int gdbstub_clear_breakpoint(unsigned long type, unsigned long addr, unsigned long len)
  864. {
  865. unsigned long tmp;
  866. int bkpt, loop;
  867. union {
  868. struct {
  869. unsigned long mask0, mask1;
  870. };
  871. uint8_t bytes[8];
  872. } dbmr;
  873. //gdbstub_printk("clearbkpt(%ld,%08lx,%ld)\n", type, addr, len);
  874. switch (type) {
  875. /* clear software breakpoint */
  876. case 0:
  877. for (bkpt = 255; bkpt >= 0; bkpt--)
  878. if (gdbstub_bkpts[bkpt].addr == addr && gdbstub_bkpts[bkpt].len == len)
  879. break;
  880. if (bkpt < 0)
  881. return -ENOENT;
  882. gdbstub_bkpts[bkpt].addr = 0;
  883. for (loop = 0; loop < len/4; loop++)
  884. if (!gdbstub_write_dword(&((uint32_t *) addr)[loop],
  885. gdbstub_bkpts[bkpt].originsns[loop]))
  886. return -EFAULT;
  887. return 0;
  888. /* clear hardware breakpoint */
  889. case 1:
  890. if (addr & 3 || len != 4)
  891. return -EINVAL;
  892. #define __get_ibar(X) ({ unsigned long x; asm volatile("movsg ibar"#X",%0" : "=r"(x)); x; })
  893. if (__debug_regs->dcr & DCR_IBE0 && __get_ibar(0) == addr) {
  894. //gdbstub_printk("clear h/w break 0: %08lx\n", addr);
  895. __debug_regs->dcr &= ~DCR_IBE0;
  896. __debug_regs->ibar[0] = 0;
  897. asm volatile("movgs gr0,ibar0");
  898. return 0;
  899. }
  900. if (__debug_regs->dcr & DCR_IBE1 && __get_ibar(1) == addr) {
  901. //gdbstub_printk("clear h/w break 1: %08lx\n", addr);
  902. __debug_regs->dcr &= ~DCR_IBE1;
  903. __debug_regs->ibar[1] = 0;
  904. asm volatile("movgs gr0,ibar1");
  905. return 0;
  906. }
  907. if (__debug_regs->dcr & DCR_IBE2 && __get_ibar(2) == addr) {
  908. //gdbstub_printk("clear h/w break 2: %08lx\n", addr);
  909. __debug_regs->dcr &= ~DCR_IBE2;
  910. __debug_regs->ibar[2] = 0;
  911. asm volatile("movgs gr0,ibar2");
  912. return 0;
  913. }
  914. if (__debug_regs->dcr & DCR_IBE3 && __get_ibar(3) == addr) {
  915. //gdbstub_printk("clear h/w break 3: %08lx\n", addr);
  916. __debug_regs->dcr &= ~DCR_IBE3;
  917. __debug_regs->ibar[3] = 0;
  918. asm volatile("movgs gr0,ibar3");
  919. return 0;
  920. }
  921. return -EINVAL;
  922. /* clear data read/write/access watchpoint */
  923. case 2:
  924. case 3:
  925. case 4:
  926. if ((addr & ~7) != ((addr + len - 1) & ~7))
  927. return -EINVAL;
  928. tmp = addr & 7;
  929. memset(dbmr.bytes, 0xff, sizeof(dbmr.bytes));
  930. for (loop = 0; loop < len; loop++)
  931. dbmr.bytes[tmp + loop] = 0;
  932. addr &= ~7;
  933. #define __get_dbar(X) ({ unsigned long x; asm volatile("movsg dbar"#X",%0" : "=r"(x)); x; })
  934. #define __get_dbmr0(X) ({ unsigned long x; asm volatile("movsg dbmr"#X"0,%0" : "=r"(x)); x; })
  935. #define __get_dbmr1(X) ({ unsigned long x; asm volatile("movsg dbmr"#X"1,%0" : "=r"(x)); x; })
  936. /* consider DBAR 0 */
  937. tmp = type==2 ? DCR_DWBE0 : type==3 ? DCR_DRBE0 : DCR_DRBE0|DCR_DWBE0;
  938. if ((__debug_regs->dcr & (DCR_DRBE0|DCR_DWBE0)) != tmp ||
  939. __get_dbar(0) != addr ||
  940. __get_dbmr0(0) != dbmr.mask0 ||
  941. __get_dbmr1(0) != dbmr.mask1)
  942. goto skip_dbar0;
  943. //gdbstub_printk("clear h/w watchpoint 0 type %ld: %08lx\n", type, addr);
  944. __debug_regs->dcr &= ~(DCR_DRBE0|DCR_DWBE0);
  945. __debug_regs->dbar[0] = 0;
  946. __debug_regs->dbmr[0][0] = 0;
  947. __debug_regs->dbmr[0][1] = 0;
  948. __debug_regs->dbdr[0][0] = 0;
  949. __debug_regs->dbdr[0][1] = 0;
  950. asm volatile(" movgs gr0,dbar0 \n"
  951. " movgs gr0,dbmr00 \n"
  952. " movgs gr0,dbmr01 \n"
  953. " movgs gr0,dbdr00 \n"
  954. " movgs gr0,dbdr01 \n");
  955. return 0;
  956. skip_dbar0:
  957. /* consider DBAR 0 */
  958. tmp = type==2 ? DCR_DWBE1 : type==3 ? DCR_DRBE1 : DCR_DRBE1|DCR_DWBE1;
  959. if ((__debug_regs->dcr & (DCR_DRBE1|DCR_DWBE1)) != tmp ||
  960. __get_dbar(1) != addr ||
  961. __get_dbmr0(1) != dbmr.mask0 ||
  962. __get_dbmr1(1) != dbmr.mask1)
  963. goto skip_dbar1;
  964. //gdbstub_printk("clear h/w watchpoint 1 type %ld: %08lx\n", type, addr);
  965. __debug_regs->dcr &= ~(DCR_DRBE1|DCR_DWBE1);
  966. __debug_regs->dbar[1] = 0;
  967. __debug_regs->dbmr[1][0] = 0;
  968. __debug_regs->dbmr[1][1] = 0;
  969. __debug_regs->dbdr[1][0] = 0;
  970. __debug_regs->dbdr[1][1] = 0;
  971. asm volatile(" movgs gr0,dbar1 \n"
  972. " movgs gr0,dbmr10 \n"
  973. " movgs gr0,dbmr11 \n"
  974. " movgs gr0,dbdr10 \n"
  975. " movgs gr0,dbdr11 \n");
  976. return 0;
  977. skip_dbar1:
  978. return -ENOSPC;
  979. default:
  980. return -EINVAL;
  981. }
  982. } /* end gdbstub_clear_breakpoint() */
  983. /*****************************************************************************/
  984. /*
  985. * check a for an internal software breakpoint, and wind the PC back if necessary
  986. */
  987. static void gdbstub_check_breakpoint(void)
  988. {
  989. unsigned long addr = __debug_frame->pc - 4;
  990. int bkpt;
  991. for (bkpt = 255; bkpt >= 0; bkpt--)
  992. if (gdbstub_bkpts[bkpt].addr == addr)
  993. break;
  994. if (bkpt >= 0)
  995. __debug_frame->pc = addr;
  996. //gdbstub_printk("alter pc [%d] %08lx\n", bkpt, __debug_frame->pc);
  997. } /* end gdbstub_check_breakpoint() */
  998. /*****************************************************************************/
  999. /*
  1000. *
  1001. */
  1002. static void __maybe_unused gdbstub_show_regs(void)
  1003. {
  1004. unsigned long *reg;
  1005. int loop;
  1006. gdbstub_printk("\n");
  1007. gdbstub_printk("Frame: @%p [%s]\n",
  1008. __debug_frame,
  1009. __debug_frame->psr & PSR_S ? "kernel" : "user");
  1010. reg = (unsigned long *) __debug_frame;
  1011. for (loop = 0; loop < NR_PT_REGS; loop++) {
  1012. printk("%s %08lx", regnames[loop + 0], reg[loop + 0]);
  1013. if (loop == NR_PT_REGS - 1 || loop % 5 == 4)
  1014. printk("\n");
  1015. else
  1016. printk(" | ");
  1017. }
  1018. gdbstub_printk("Process %s (pid: %d)\n", current->comm, current->pid);
  1019. } /* end gdbstub_show_regs() */
  1020. /*****************************************************************************/
  1021. /*
  1022. * dump debugging regs
  1023. */
  1024. static void __maybe_unused gdbstub_dump_debugregs(void)
  1025. {
  1026. gdbstub_printk("DCR %08lx ", __debug_status.dcr);
  1027. gdbstub_printk("BRR %08lx\n", __debug_status.brr);
  1028. gdbstub_printk("IBAR0 %08lx ", __get_ibar(0));
  1029. gdbstub_printk("IBAR1 %08lx ", __get_ibar(1));
  1030. gdbstub_printk("IBAR2 %08lx ", __get_ibar(2));
  1031. gdbstub_printk("IBAR3 %08lx\n", __get_ibar(3));
  1032. gdbstub_printk("DBAR0 %08lx ", __get_dbar(0));
  1033. gdbstub_printk("DBMR00 %08lx ", __get_dbmr0(0));
  1034. gdbstub_printk("DBMR01 %08lx\n", __get_dbmr1(0));
  1035. gdbstub_printk("DBAR1 %08lx ", __get_dbar(1));
  1036. gdbstub_printk("DBMR10 %08lx ", __get_dbmr0(1));
  1037. gdbstub_printk("DBMR11 %08lx\n", __get_dbmr1(1));
  1038. gdbstub_printk("\n");
  1039. } /* end gdbstub_dump_debugregs() */
  1040. /*****************************************************************************/
  1041. /*
  1042. * dump the MMU state into a structure so that it can be accessed with GDB
  1043. */
  1044. void gdbstub_get_mmu_state(void)
  1045. {
  1046. asm volatile("movsg hsr0,%0" : "=r"(__debug_mmu.regs.hsr0));
  1047. asm volatile("movsg pcsr,%0" : "=r"(__debug_mmu.regs.pcsr));
  1048. asm volatile("movsg esr0,%0" : "=r"(__debug_mmu.regs.esr0));
  1049. asm volatile("movsg ear0,%0" : "=r"(__debug_mmu.regs.ear0));
  1050. asm volatile("movsg epcr0,%0" : "=r"(__debug_mmu.regs.epcr0));
  1051. /* read the protection / SAT registers */
  1052. __debug_mmu.iamr[0].L = __get_IAMLR(0);
  1053. __debug_mmu.iamr[0].P = __get_IAMPR(0);
  1054. __debug_mmu.iamr[1].L = __get_IAMLR(1);
  1055. __debug_mmu.iamr[1].P = __get_IAMPR(1);
  1056. __debug_mmu.iamr[2].L = __get_IAMLR(2);
  1057. __debug_mmu.iamr[2].P = __get_IAMPR(2);
  1058. __debug_mmu.iamr[3].L = __get_IAMLR(3);
  1059. __debug_mmu.iamr[3].P = __get_IAMPR(3);
  1060. __debug_mmu.iamr[4].L = __get_IAMLR(4);
  1061. __debug_mmu.iamr[4].P = __get_IAMPR(4);
  1062. __debug_mmu.iamr[5].L = __get_IAMLR(5);
  1063. __debug_mmu.iamr[5].P = __get_IAMPR(5);
  1064. __debug_mmu.iamr[6].L = __get_IAMLR(6);
  1065. __debug_mmu.iamr[6].P = __get_IAMPR(6);
  1066. __debug_mmu.iamr[7].L = __get_IAMLR(7);
  1067. __debug_mmu.iamr[7].P = __get_IAMPR(7);
  1068. __debug_mmu.iamr[8].L = __get_IAMLR(8);
  1069. __debug_mmu.iamr[8].P = __get_IAMPR(8);
  1070. __debug_mmu.iamr[9].L = __get_IAMLR(9);
  1071. __debug_mmu.iamr[9].P = __get_IAMPR(9);
  1072. __debug_mmu.iamr[10].L = __get_IAMLR(10);
  1073. __debug_mmu.iamr[10].P = __get_IAMPR(10);
  1074. __debug_mmu.iamr[11].L = __get_IAMLR(11);
  1075. __debug_mmu.iamr[11].P = __get_IAMPR(11);
  1076. __debug_mmu.iamr[12].L = __get_IAMLR(12);
  1077. __debug_mmu.iamr[12].P = __get_IAMPR(12);
  1078. __debug_mmu.iamr[13].L = __get_IAMLR(13);
  1079. __debug_mmu.iamr[13].P = __get_IAMPR(13);
  1080. __debug_mmu.iamr[14].L = __get_IAMLR(14);
  1081. __debug_mmu.iamr[14].P = __get_IAMPR(14);
  1082. __debug_mmu.iamr[15].L = __get_IAMLR(15);
  1083. __debug_mmu.iamr[15].P = __get_IAMPR(15);
  1084. __debug_mmu.damr[0].L = __get_DAMLR(0);
  1085. __debug_mmu.damr[0].P = __get_DAMPR(0);
  1086. __debug_mmu.damr[1].L = __get_DAMLR(1);
  1087. __debug_mmu.damr[1].P = __get_DAMPR(1);
  1088. __debug_mmu.damr[2].L = __get_DAMLR(2);
  1089. __debug_mmu.damr[2].P = __get_DAMPR(2);
  1090. __debug_mmu.damr[3].L = __get_DAMLR(3);
  1091. __debug_mmu.damr[3].P = __get_DAMPR(3);
  1092. __debug_mmu.damr[4].L = __get_DAMLR(4);
  1093. __debug_mmu.damr[4].P = __get_DAMPR(4);
  1094. __debug_mmu.damr[5].L = __get_DAMLR(5);
  1095. __debug_mmu.damr[5].P = __get_DAMPR(5);
  1096. __debug_mmu.damr[6].L = __get_DAMLR(6);
  1097. __debug_mmu.damr[6].P = __get_DAMPR(6);
  1098. __debug_mmu.damr[7].L = __get_DAMLR(7);
  1099. __debug_mmu.damr[7].P = __get_DAMPR(7);
  1100. __debug_mmu.damr[8].L = __get_DAMLR(8);
  1101. __debug_mmu.damr[8].P = __get_DAMPR(8);
  1102. __debug_mmu.damr[9].L = __get_DAMLR(9);
  1103. __debug_mmu.damr[9].P = __get_DAMPR(9);
  1104. __debug_mmu.damr[10].L = __get_DAMLR(10);
  1105. __debug_mmu.damr[10].P = __get_DAMPR(10);
  1106. __debug_mmu.damr[11].L = __get_DAMLR(11);
  1107. __debug_mmu.damr[11].P = __get_DAMPR(11);
  1108. __debug_mmu.damr[12].L = __get_DAMLR(12);
  1109. __debug_mmu.damr[12].P = __get_DAMPR(12);
  1110. __debug_mmu.damr[13].L = __get_DAMLR(13);
  1111. __debug_mmu.damr[13].P = __get_DAMPR(13);
  1112. __debug_mmu.damr[14].L = __get_DAMLR(14);
  1113. __debug_mmu.damr[14].P = __get_DAMPR(14);
  1114. __debug_mmu.damr[15].L = __get_DAMLR(15);
  1115. __debug_mmu.damr[15].P = __get_DAMPR(15);
  1116. #ifdef CONFIG_MMU
  1117. do {
  1118. /* read the DAT entries from the TLB */
  1119. struct __debug_amr *p;
  1120. int loop;
  1121. asm volatile("movsg tplr,%0" : "=r"(__debug_mmu.regs.tplr));
  1122. asm volatile("movsg tppr,%0" : "=r"(__debug_mmu.regs.tppr));
  1123. asm volatile("movsg tpxr,%0" : "=r"(__debug_mmu.regs.tpxr));
  1124. asm volatile("movsg cxnr,%0" : "=r"(__debug_mmu.regs.cxnr));
  1125. p = __debug_mmu.tlb;
  1126. /* way 0 */
  1127. asm volatile("movgs %0,tpxr" :: "r"(0 << TPXR_WAY_SHIFT));
  1128. for (loop = 0; loop < 64; loop++) {
  1129. asm volatile("tlbpr %0,gr0,#1,#0" :: "r"(loop << PAGE_SHIFT));
  1130. asm volatile("movsg tplr,%0" : "=r"(p->L));
  1131. asm volatile("movsg tppr,%0" : "=r"(p->P));
  1132. p++;
  1133. }
  1134. /* way 1 */
  1135. asm volatile("movgs %0,tpxr" :: "r"(1 << TPXR_WAY_SHIFT));
  1136. for (loop = 0; loop < 64; loop++) {
  1137. asm volatile("tlbpr %0,gr0,#1,#0" :: "r"(loop << PAGE_SHIFT));
  1138. asm volatile("movsg tplr,%0" : "=r"(p->L));
  1139. asm volatile("movsg tppr,%0" : "=r"(p->P));
  1140. p++;
  1141. }
  1142. asm volatile("movgs %0,tplr" :: "r"(__debug_mmu.regs.tplr));
  1143. asm volatile("movgs %0,tppr" :: "r"(__debug_mmu.regs.tppr));
  1144. asm volatile("movgs %0,tpxr" :: "r"(__debug_mmu.regs.tpxr));
  1145. } while(0);
  1146. #endif
  1147. } /* end gdbstub_get_mmu_state() */
  1148. /*
  1149. * handle general query commands of the form 'qXXXXX'
  1150. */
  1151. static void gdbstub_handle_query(void)
  1152. {
  1153. if (strcmp(input_buffer, "qAttached") == 0) {
  1154. /* return current thread ID */
  1155. sprintf(output_buffer, "1");
  1156. return;
  1157. }
  1158. if (strcmp(input_buffer, "qC") == 0) {
  1159. /* return current thread ID */
  1160. sprintf(output_buffer, "QC 0");
  1161. return;
  1162. }
  1163. if (strcmp(input_buffer, "qOffsets") == 0) {
  1164. /* return relocation offset of text and data segments */
  1165. sprintf(output_buffer, "Text=0;Data=0;Bss=0");
  1166. return;
  1167. }
  1168. if (strcmp(input_buffer, "qSymbol::") == 0) {
  1169. sprintf(output_buffer, "OK");
  1170. return;
  1171. }
  1172. if (strcmp(input_buffer, "qSupported") == 0) {
  1173. /* query of supported features */
  1174. sprintf(output_buffer, "PacketSize=%u;ReverseContinue-;ReverseStep-",
  1175. sizeof(input_buffer));
  1176. return;
  1177. }
  1178. gdbstub_strcpy(output_buffer,"E01");
  1179. }
  1180. /*****************************************************************************/
  1181. /*
  1182. * handle event interception and GDB remote protocol processing
  1183. * - on entry:
  1184. * PSR.ET==0, PSR.S==1 and the CPU is in debug mode
  1185. * __debug_frame points to the saved registers
  1186. * __frame points to the kernel mode exception frame, if it was in kernel
  1187. * mode when the break happened
  1188. */
  1189. void gdbstub(int sigval)
  1190. {
  1191. unsigned long addr, length, loop, dbar, temp, temp2, temp3;
  1192. uint32_t zero;
  1193. char *ptr;
  1194. int flush_cache = 0;
  1195. LEDS(0x5000);
  1196. if (sigval < 0) {
  1197. #ifndef CONFIG_GDBSTUB_IMMEDIATE
  1198. /* return immediately if GDB immediate activation option not set */
  1199. return;
  1200. #else
  1201. sigval = SIGINT;
  1202. #endif
  1203. }
  1204. save_user_regs(&__debug_frame0->uc);
  1205. #if 0
  1206. gdbstub_printk("--> gdbstub() %08x %p %08x %08x\n",
  1207. __debug_frame->pc,
  1208. __debug_frame,
  1209. __debug_regs->brr,
  1210. __debug_regs->bpsr);
  1211. // gdbstub_show_regs();
  1212. #endif
  1213. LEDS(0x5001);
  1214. /* if we were interrupted by input on the serial gdbstub serial port,
  1215. * restore the context prior to the interrupt so that we return to that
  1216. * directly
  1217. */
  1218. temp = (unsigned long) __entry_kerneltrap_table;
  1219. temp2 = (unsigned long) __entry_usertrap_table;
  1220. temp3 = __debug_frame->pc & ~15;
  1221. if (temp3 == temp + TBR_TT_INTERRUPT_15 ||
  1222. temp3 == temp2 + TBR_TT_INTERRUPT_15
  1223. ) {
  1224. asm volatile("movsg pcsr,%0" : "=r"(__debug_frame->pc));
  1225. __debug_frame->psr |= PSR_ET;
  1226. __debug_frame->psr &= ~PSR_S;
  1227. if (__debug_frame->psr & PSR_PS)
  1228. __debug_frame->psr |= PSR_S;
  1229. __debug_status.brr = (__debug_frame->tbr & TBR_TT) << 12;
  1230. __debug_status.brr |= BRR_EB;
  1231. sigval = SIGINT;
  1232. }
  1233. /* handle the decrement timer going off (FR451 only) */
  1234. if (temp3 == temp + TBR_TT_DECREMENT_TIMER ||
  1235. temp3 == temp2 + TBR_TT_DECREMENT_TIMER
  1236. ) {
  1237. asm volatile("movgs %0,timerd" :: "r"(10000000));
  1238. asm volatile("movsg pcsr,%0" : "=r"(__debug_frame->pc));
  1239. __debug_frame->psr |= PSR_ET;
  1240. __debug_frame->psr &= ~PSR_S;
  1241. if (__debug_frame->psr & PSR_PS)
  1242. __debug_frame->psr |= PSR_S;
  1243. __debug_status.brr = (__debug_frame->tbr & TBR_TT) << 12;
  1244. __debug_status.brr |= BRR_EB;
  1245. sigval = SIGXCPU;
  1246. }
  1247. LEDS(0x5002);
  1248. /* after a BREAK insn, the PC lands on the far side of it */
  1249. if (__debug_status.brr & BRR_SB)
  1250. gdbstub_check_breakpoint();
  1251. LEDS(0x5003);
  1252. /* handle attempts to write console data via GDB "O" commands */
  1253. if (__debug_frame->pc == (unsigned long) gdbstub_console_write + 4) {
  1254. __gdbstub_console_write((struct console *) __debug_frame->gr8,
  1255. (const char *) __debug_frame->gr9,
  1256. (unsigned) __debug_frame->gr10);
  1257. goto done;
  1258. }
  1259. if (gdbstub_rx_unget) {
  1260. sigval = SIGINT;
  1261. goto packet_waiting;
  1262. }
  1263. if (!sigval)
  1264. sigval = gdbstub_compute_signal(__debug_status.brr);
  1265. LEDS(0x5004);
  1266. /* send a message to the debugger's user saying what happened if it may
  1267. * not be clear cut (we can't map exceptions onto signals properly)
  1268. */
  1269. if (sigval != SIGINT && sigval != SIGTRAP && sigval != SIGILL) {
  1270. static const char title[] = "Break ";
  1271. static const char crlf[] = "\r\n";
  1272. unsigned long brr = __debug_status.brr;
  1273. char hx;
  1274. ptr = output_buffer;
  1275. *ptr++ = 'O';
  1276. ptr = mem2hex(title, ptr, sizeof(title) - 1,0);
  1277. hx = hex_asc_hi(brr >> 24);
  1278. ptr = pack_hex_byte(ptr, hx);
  1279. hx = hex_asc_lo(brr >> 24);
  1280. ptr = pack_hex_byte(ptr, hx);
  1281. hx = hex_asc_hi(brr >> 16);
  1282. ptr = pack_hex_byte(ptr, hx);
  1283. hx = hex_asc_lo(brr >> 16);
  1284. ptr = pack_hex_byte(ptr, hx);
  1285. hx = hex_asc_hi(brr >> 8);
  1286. ptr = pack_hex_byte(ptr, hx);
  1287. hx = hex_asc_lo(brr >> 8);
  1288. ptr = pack_hex_byte(ptr, hx);
  1289. hx = hex_asc_hi(brr);
  1290. ptr = pack_hex_byte(ptr, hx);
  1291. hx = hex_asc_lo(brr);
  1292. ptr = pack_hex_byte(ptr, hx);
  1293. ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
  1294. *ptr = 0;
  1295. gdbstub_send_packet(output_buffer); /* send it off... */
  1296. }
  1297. LEDS(0x5005);
  1298. /* tell the debugger that an exception has occurred */
  1299. ptr = output_buffer;
  1300. /* Send trap type (converted to signal) */
  1301. *ptr++ = 'T';
  1302. ptr = pack_hex_byte(ptr, sigval);
  1303. /* Send Error PC */
  1304. ptr = pack_hex_byte(ptr, GDB_REG_PC);
  1305. *ptr++ = ':';
  1306. ptr = mem2hex(&__debug_frame->pc, ptr, 4, 0);
  1307. *ptr++ = ';';
  1308. /*
  1309. * Send frame pointer
  1310. */
  1311. ptr = pack_hex_byte(ptr, GDB_REG_FP);
  1312. *ptr++ = ':';
  1313. ptr = mem2hex(&__debug_frame->fp, ptr, 4, 0);
  1314. *ptr++ = ';';
  1315. /*
  1316. * Send stack pointer
  1317. */
  1318. ptr = pack_hex_byte(ptr, GDB_REG_SP);
  1319. *ptr++ = ':';
  1320. ptr = mem2hex(&__debug_frame->sp, ptr, 4, 0);
  1321. *ptr++ = ';';
  1322. *ptr++ = 0;
  1323. gdbstub_send_packet(output_buffer); /* send it off... */
  1324. LEDS(0x5006);
  1325. packet_waiting:
  1326. gdbstub_get_mmu_state();
  1327. /* wait for input from remote GDB */
  1328. while (1) {
  1329. output_buffer[0] = 0;
  1330. LEDS(0x5007);
  1331. gdbstub_recv_packet(input_buffer);
  1332. LEDS(0x5600 | input_buffer[0]);
  1333. switch (input_buffer[0]) {
  1334. /* request repeat of last signal number */
  1335. case '?':
  1336. output_buffer[0] = 'S';
  1337. output_buffer[1] = hex_asc_hi(sigval);
  1338. output_buffer[2] = hex_asc_lo(sigval);
  1339. output_buffer[3] = 0;
  1340. break;
  1341. case 'd':
  1342. /* toggle debug flag */
  1343. break;
  1344. /* return the value of the CPU registers
  1345. * - GR0, GR1, GR2, GR3, GR4, GR5, GR6, GR7,
  1346. * - GR8, GR9, GR10, GR11, GR12, GR13, GR14, GR15,
  1347. * - GR16, GR17, GR18, GR19, GR20, GR21, GR22, GR23,
  1348. * - GR24, GR25, GR26, GR27, GR28, GR29, GR30, GR31,
  1349. * - GR32, GR33, GR34, GR35, GR36, GR37, GR38, GR39,
  1350. * - GR40, GR41, GR42, GR43, GR44, GR45, GR46, GR47,
  1351. * - GR48, GR49, GR50, GR51, GR52, GR53, GR54, GR55,
  1352. * - GR56, GR57, GR58, GR59, GR60, GR61, GR62, GR63,
  1353. * - FP0, FP1, FP2, FP3, FP4, FP5, FP6, FP7,
  1354. * - FP8, FP9, FP10, FP11, FP12, FP13, FP14, FP15,
  1355. * - FP16, FP17, FP18, FP19, FP20, FP21, FP22, FP23,
  1356. * - FP24, FP25, FP26, FP27, FP28, FP29, FP30, FP31,
  1357. * - FP32, FP33, FP34, FP35, FP36, FP37, FP38, FP39,
  1358. * - FP40, FP41, FP42, FP43, FP44, FP45, FP46, FP47,
  1359. * - FP48, FP49, FP50, FP51, FP52, FP53, FP54, FP55,
  1360. * - FP56, FP57, FP58, FP59, FP60, FP61, FP62, FP63,
  1361. * - PC, PSR, CCR, CCCR,
  1362. * - _X132, _X133, _X134
  1363. * - TBR, BRR, DBAR0, DBAR1, DBAR2, DBAR3,
  1364. * - _X141, _X142, _X143, _X144,
  1365. * - LR, LCR
  1366. */
  1367. case 'g':
  1368. zero = 0;
  1369. ptr = output_buffer;
  1370. /* deal with GR0, GR1-GR27, GR28-GR31, GR32-GR63 */
  1371. ptr = mem2hex(&zero, ptr, 4, 0);
  1372. for (loop = 1; loop <= 27; loop++)
  1373. ptr = mem2hex(&__debug_user_context->i.gr[loop], ptr, 4, 0);
  1374. temp = (unsigned long) __frame;
  1375. ptr = mem2hex(&temp, ptr, 4, 0);
  1376. ptr = mem2hex(&__debug_user_context->i.gr[29], ptr, 4, 0);
  1377. ptr = mem2hex(&__debug_user_context->i.gr[30], ptr, 4, 0);
  1378. #ifdef CONFIG_MMU
  1379. ptr = mem2hex(&__debug_user_context->i.gr[31], ptr, 4, 0);
  1380. #else
  1381. temp = (unsigned long) __debug_frame;
  1382. ptr = mem2hex(&temp, ptr, 4, 0);
  1383. #endif
  1384. for (loop = 32; loop <= 63; loop++)
  1385. ptr = mem2hex(&__debug_user_context->i.gr[loop], ptr, 4, 0);
  1386. /* deal with FR0-FR63 */
  1387. for (loop = 0; loop <= 63; loop++)
  1388. ptr = mem2hex(&__debug_user_context->f.fr[loop], ptr, 4, 0);
  1389. /* deal with special registers */
  1390. ptr = mem2hex(&__debug_frame->pc, ptr, 4, 0);
  1391. ptr = mem2hex(&__debug_frame->psr, ptr, 4, 0);
  1392. ptr = mem2hex(&__debug_frame->ccr, ptr, 4, 0);
  1393. ptr = mem2hex(&__debug_frame->cccr, ptr, 4, 0);
  1394. ptr = mem2hex(&zero, ptr, 4, 0);
  1395. ptr = mem2hex(&zero, ptr, 4, 0);
  1396. ptr = mem2hex(&zero, ptr, 4, 0);
  1397. ptr = mem2hex(&__debug_frame->tbr, ptr, 4, 0);
  1398. ptr = mem2hex(&__debug_status.brr , ptr, 4, 0);
  1399. asm volatile("movsg dbar0,%0" : "=r"(dbar));
  1400. ptr = mem2hex(&dbar, ptr, 4, 0);
  1401. asm volatile("movsg dbar1,%0" : "=r"(dbar));
  1402. ptr = mem2hex(&dbar, ptr, 4, 0);
  1403. asm volatile("movsg dbar2,%0" : "=r"(dbar));
  1404. ptr = mem2hex(&dbar, ptr, 4, 0);
  1405. asm volatile("movsg dbar3,%0" : "=r"(dbar));
  1406. ptr = mem2hex(&dbar, ptr, 4, 0);
  1407. asm volatile("movsg scr0,%0" : "=r"(dbar));
  1408. ptr = mem2hex(&dbar, ptr, 4, 0);
  1409. asm volatile("movsg scr1,%0" : "=r"(dbar));
  1410. ptr = mem2hex(&dbar, ptr, 4, 0);
  1411. asm volatile("movsg scr2,%0" : "=r"(dbar));
  1412. ptr = mem2hex(&dbar, ptr, 4, 0);
  1413. asm volatile("movsg scr3,%0" : "=r"(dbar));
  1414. ptr = mem2hex(&dbar, ptr, 4, 0);
  1415. ptr = mem2hex(&__debug_frame->lr, ptr, 4, 0);
  1416. ptr = mem2hex(&__debug_frame->lcr, ptr, 4, 0);
  1417. ptr = mem2hex(&__debug_frame->iacc0, ptr, 8, 0);
  1418. ptr = mem2hex(&__debug_user_context->f.fsr[0], ptr, 4, 0);
  1419. for (loop = 0; loop <= 7; loop++)
  1420. ptr = mem2hex(&__debug_user_context->f.acc[loop], ptr, 4, 0);
  1421. ptr = mem2hex(&__debug_user_context->f.accg, ptr, 8, 0);
  1422. for (loop = 0; loop <= 1; loop++)
  1423. ptr = mem2hex(&__debug_user_context->f.msr[loop], ptr, 4, 0);
  1424. ptr = mem2hex(&__debug_frame->gner0, ptr, 4, 0);
  1425. ptr = mem2hex(&__debug_frame->gner1, ptr, 4, 0);
  1426. ptr = mem2hex(&__debug_user_context->f.fner[0], ptr, 4, 0);
  1427. ptr = mem2hex(&__debug_user_context->f.fner[1], ptr, 4, 0);
  1428. break;
  1429. /* set the values of the CPU registers */
  1430. case 'G':
  1431. ptr = &input_buffer[1];
  1432. /* deal with GR0, GR1-GR27, GR28-GR31, GR32-GR63 */
  1433. ptr = hex2mem(ptr, &temp, 4);
  1434. for (loop = 1; loop <= 27; loop++)
  1435. ptr = hex2mem(ptr, &__debug_user_context->i.gr[loop], 4);
  1436. ptr = hex2mem(ptr, &temp, 4);
  1437. __frame = (struct pt_regs *) temp;
  1438. ptr = hex2mem(ptr, &__debug_frame->gr29, 4);
  1439. ptr = hex2mem(ptr, &__debug_frame->gr30, 4);
  1440. #ifdef CONFIG_MMU
  1441. ptr = hex2mem(ptr, &__debug_frame->gr31, 4);
  1442. #else
  1443. ptr = hex2mem(ptr, &temp, 4);
  1444. #endif
  1445. for (loop = 32; loop <= 63; loop++)
  1446. ptr = hex2mem(ptr, &__debug_user_context->i.gr[loop], 4);
  1447. /* deal with FR0-FR63 */
  1448. for (loop = 0; loop <= 63; loop++)
  1449. ptr = mem2hex(&__debug_user_context->f.fr[loop], ptr, 4, 0);
  1450. /* deal with special registers */
  1451. ptr = hex2mem(ptr, &__debug_frame->pc, 4);
  1452. ptr = hex2mem(ptr, &__debug_frame->psr, 4);
  1453. ptr = hex2mem(ptr, &__debug_frame->ccr, 4);
  1454. ptr = hex2mem(ptr, &__debug_frame->cccr,4);
  1455. for (loop = 132; loop <= 140; loop++)
  1456. ptr = hex2mem(ptr, &temp, 4);
  1457. ptr = hex2mem(ptr, &temp, 4);
  1458. asm volatile("movgs %0,scr0" :: "r"(temp));
  1459. ptr = hex2mem(ptr, &temp, 4);
  1460. asm volatile("movgs %0,scr1" :: "r"(temp));
  1461. ptr = hex2mem(ptr, &temp, 4);
  1462. asm volatile("movgs %0,scr2" :: "r"(temp));
  1463. ptr = hex2mem(ptr, &temp, 4);
  1464. asm volatile("movgs %0,scr3" :: "r"(temp));
  1465. ptr = hex2mem(ptr, &__debug_frame->lr, 4);
  1466. ptr = hex2mem(ptr, &__debug_frame->lcr, 4);
  1467. ptr = hex2mem(ptr, &__debug_frame->iacc0, 8);
  1468. ptr = hex2mem(ptr, &__debug_user_context->f.fsr[0], 4);
  1469. for (loop = 0; loop <= 7; loop++)
  1470. ptr = hex2mem(ptr, &__debug_user_context->f.acc[loop], 4);
  1471. ptr = hex2mem(ptr, &__debug_user_context->f.accg, 8);
  1472. for (loop = 0; loop <= 1; loop++)
  1473. ptr = hex2mem(ptr, &__debug_user_context->f.msr[loop], 4);
  1474. ptr = hex2mem(ptr, &__debug_frame->gner0, 4);
  1475. ptr = hex2mem(ptr, &__debug_frame->gner1, 4);
  1476. ptr = hex2mem(ptr, &__debug_user_context->f.fner[0], 4);
  1477. ptr = hex2mem(ptr, &__debug_user_context->f.fner[1], 4);
  1478. gdbstub_strcpy(output_buffer,"OK");
  1479. break;
  1480. /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  1481. case 'm':
  1482. ptr = &input_buffer[1];
  1483. if (hexToInt(&ptr, &addr) &&
  1484. *ptr++ == ',' &&
  1485. hexToInt(&ptr, &length)
  1486. ) {
  1487. if (mem2hex((char *)addr, output_buffer, length, 1))
  1488. break;
  1489. gdbstub_strcpy (output_buffer, "E03");
  1490. }
  1491. else {
  1492. gdbstub_strcpy(output_buffer,"E01");
  1493. }
  1494. break;
  1495. /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
  1496. case 'M':
  1497. ptr = &input_buffer[1];
  1498. if (hexToInt(&ptr, &addr) &&
  1499. *ptr++ == ',' &&
  1500. hexToInt(&ptr, &length) &&
  1501. *ptr++ == ':'
  1502. ) {
  1503. if (hex2mem(ptr, (char *)addr, length)) {
  1504. gdbstub_strcpy(output_buffer, "OK");
  1505. }
  1506. else {
  1507. gdbstub_strcpy(output_buffer, "E03");
  1508. }
  1509. }
  1510. else
  1511. gdbstub_strcpy(output_buffer, "E02");
  1512. flush_cache = 1;
  1513. break;
  1514. /* pNN: Read value of reg N and return it */
  1515. case 'p':
  1516. /* return no value, indicating that we don't support
  1517. * this command and that gdb should use 'g' instead */
  1518. break;
  1519. /* PNN,=RRRRRRRR: Write value R to reg N return OK */
  1520. case 'P':
  1521. ptr = &input_buffer[1];
  1522. if (!hexToInt(&ptr, &addr) ||
  1523. *ptr++ != '=' ||
  1524. !hexToInt(&ptr, &temp)
  1525. ) {
  1526. gdbstub_strcpy(output_buffer, "E01");
  1527. break;
  1528. }
  1529. temp2 = 1;
  1530. switch (addr) {
  1531. case GDB_REG_GR(0):
  1532. break;
  1533. case GDB_REG_GR(1) ... GDB_REG_GR(63):
  1534. __debug_user_context->i.gr[addr - GDB_REG_GR(0)] = temp;
  1535. break;
  1536. case GDB_REG_FR(0) ... GDB_REG_FR(63):
  1537. __debug_user_context->f.fr[addr - GDB_REG_FR(0)] = temp;
  1538. break;
  1539. case GDB_REG_PC:
  1540. __debug_user_context->i.pc = temp;
  1541. break;
  1542. case GDB_REG_PSR:
  1543. __debug_user_context->i.psr = temp;
  1544. break;
  1545. case GDB_REG_CCR:
  1546. __debug_user_context->i.ccr = temp;
  1547. break;
  1548. case GDB_REG_CCCR:
  1549. __debug_user_context->i.cccr = temp;
  1550. break;
  1551. case GDB_REG_BRR:
  1552. __debug_status.brr = temp;
  1553. break;
  1554. case GDB_REG_LR:
  1555. __debug_user_context->i.lr = temp;
  1556. break;
  1557. case GDB_REG_LCR:
  1558. __debug_user_context->i.lcr = temp;
  1559. break;
  1560. case GDB_REG_FSR0:
  1561. __debug_user_context->f.fsr[0] = temp;
  1562. break;
  1563. case GDB_REG_ACC(0) ... GDB_REG_ACC(7):
  1564. __debug_user_context->f.acc[addr - GDB_REG_ACC(0)] = temp;
  1565. break;
  1566. case GDB_REG_ACCG(0):
  1567. *(uint32_t *) &__debug_user_context->f.accg[0] = temp;
  1568. break;
  1569. case GDB_REG_ACCG(4):
  1570. *(uint32_t *) &__debug_user_context->f.accg[4] = temp;
  1571. break;
  1572. case GDB_REG_MSR(0) ... GDB_REG_MSR(1):
  1573. __debug_user_context->f.msr[addr - GDB_REG_MSR(0)] = temp;
  1574. break;
  1575. case GDB_REG_GNER(0) ... GDB_REG_GNER(1):
  1576. __debug_user_context->i.gner[addr - GDB_REG_GNER(0)] = temp;
  1577. break;
  1578. case GDB_REG_FNER(0) ... GDB_REG_FNER(1):
  1579. __debug_user_context->f.fner[addr - GDB_REG_FNER(0)] = temp;
  1580. break;
  1581. default:
  1582. temp2 = 0;
  1583. break;
  1584. }
  1585. if (temp2) {
  1586. gdbstub_strcpy(output_buffer, "OK");
  1587. }
  1588. else {
  1589. gdbstub_strcpy(output_buffer, "E02");
  1590. }
  1591. break;
  1592. /* cAA..AA Continue at address AA..AA(optional) */
  1593. case 'c':
  1594. /* try to read optional parameter, pc unchanged if no parm */
  1595. ptr = &input_buffer[1];
  1596. if (hexToInt(&ptr, &addr))
  1597. __debug_frame->pc = addr;
  1598. goto done;
  1599. /* kill the program */
  1600. case 'k' :
  1601. goto done; /* just continue */
  1602. /* detach */
  1603. case 'D':
  1604. gdbstub_strcpy(output_buffer, "OK");
  1605. break;
  1606. /* reset the whole machine (FIXME: system dependent) */
  1607. case 'r':
  1608. break;
  1609. /* step to next instruction */
  1610. case 's':
  1611. __debug_regs->dcr |= DCR_SE;
  1612. __debug_status.dcr |= DCR_SE;
  1613. goto done;
  1614. /* extended command */
  1615. case 'v':
  1616. if (strcmp(input_buffer, "vCont?") == 0) {
  1617. output_buffer[0] = 0;
  1618. break;
  1619. }
  1620. goto unsupported_cmd;
  1621. /* set baud rate (bBB) */
  1622. case 'b':
  1623. ptr = &input_buffer[1];
  1624. if (!hexToInt(&ptr, &temp)) {
  1625. gdbstub_strcpy(output_buffer,"B01");
  1626. break;
  1627. }
  1628. if (temp) {
  1629. /* ack before changing speed */
  1630. gdbstub_send_packet("OK");
  1631. gdbstub_set_baud(temp);
  1632. }
  1633. break;
  1634. /* set breakpoint */
  1635. case 'Z':
  1636. ptr = &input_buffer[1];
  1637. if (!hexToInt(&ptr,&temp) || *ptr++ != ',' ||
  1638. !hexToInt(&ptr,&addr) || *ptr++ != ',' ||
  1639. !hexToInt(&ptr,&length)
  1640. ) {
  1641. gdbstub_strcpy(output_buffer,"E01");
  1642. break;
  1643. }
  1644. if (temp >= 5) {
  1645. gdbstub_strcpy(output_buffer,"E03");
  1646. break;
  1647. }
  1648. if (gdbstub_set_breakpoint(temp, addr, length) < 0) {
  1649. gdbstub_strcpy(output_buffer,"E03");
  1650. break;
  1651. }
  1652. if (temp == 0)
  1653. flush_cache = 1; /* soft bkpt by modified memory */
  1654. gdbstub_strcpy(output_buffer,"OK");
  1655. break;
  1656. /* clear breakpoint */
  1657. case 'z':
  1658. ptr = &input_buffer[1];
  1659. if (!hexToInt(&ptr,&temp) || *ptr++ != ',' ||
  1660. !hexToInt(&ptr,&addr) || *ptr++ != ',' ||
  1661. !hexToInt(&ptr,&length)
  1662. ) {
  1663. gdbstub_strcpy(output_buffer,"E01");
  1664. break;
  1665. }
  1666. if (temp >= 5) {
  1667. gdbstub_strcpy(output_buffer,"E03");
  1668. break;
  1669. }
  1670. if (gdbstub_clear_breakpoint(temp, addr, length) < 0) {
  1671. gdbstub_strcpy(output_buffer,"E03");
  1672. break;
  1673. }
  1674. if (temp == 0)
  1675. flush_cache = 1; /* soft bkpt by modified memory */
  1676. gdbstub_strcpy(output_buffer,"OK");
  1677. break;
  1678. /* Thread-setting packet */
  1679. case 'H':
  1680. gdbstub_strcpy(output_buffer, "OK");
  1681. break;
  1682. case 'q':
  1683. gdbstub_handle_query();
  1684. break;
  1685. default:
  1686. unsupported_cmd:
  1687. gdbstub_proto("### GDB Unsupported Cmd '%s'\n",input_buffer);
  1688. gdbstub_strcpy(output_buffer,"E01");
  1689. break;
  1690. }
  1691. /* reply to the request */
  1692. LEDS(0x5009);
  1693. gdbstub_send_packet(output_buffer);
  1694. }
  1695. done:
  1696. restore_user_regs(&__debug_frame0->uc);
  1697. //gdbstub_dump_debugregs();
  1698. //gdbstub_printk("<-- gdbstub() %08x\n", __debug_frame->pc);
  1699. /* need to flush the instruction cache before resuming, as we may have
  1700. * deposited a breakpoint, and the icache probably has no way of
  1701. * knowing that a data ref to some location may have changed something
  1702. * that is in the instruction cache. NB: We flush both caches, just to
  1703. * be sure...
  1704. */
  1705. /* note: flushing the icache will clobber EAR0 on the FR451 */
  1706. if (flush_cache)
  1707. gdbstub_purge_cache();
  1708. LEDS(0x5666);
  1709. } /* end gdbstub() */
  1710. /*****************************************************************************/
  1711. /*
  1712. * initialise the GDB stub
  1713. */
  1714. void __init gdbstub_init(void)
  1715. {
  1716. #ifdef CONFIG_GDBSTUB_IMMEDIATE
  1717. unsigned char ch;
  1718. int ret;
  1719. #endif
  1720. gdbstub_printk("%s", gdbstub_banner);
  1721. gdbstub_io_init();
  1722. /* try to talk to GDB (or anyone insane enough to want to type GDB protocol by hand) */
  1723. gdbstub_proto("### GDB Tx ACK\n");
  1724. gdbstub_tx_char('+'); /* 'hello world' */
  1725. #ifdef CONFIG_GDBSTUB_IMMEDIATE
  1726. gdbstub_printk("GDB Stub waiting for packet\n");
  1727. /*
  1728. * In case GDB is started before us, ack any packets
  1729. * (presumably "$?#xx") sitting there.
  1730. */
  1731. do { gdbstub_rx_char(&ch, 0); } while (ch != '$');
  1732. do { gdbstub_rx_char(&ch, 0); } while (ch != '#');
  1733. do { ret = gdbstub_rx_char(&ch, 0); } while (ret != 0); /* eat first csum byte */
  1734. do { ret = gdbstub_rx_char(&ch, 0); } while (ret != 0); /* eat second csum byte */
  1735. gdbstub_proto("### GDB Tx NAK\n");
  1736. gdbstub_tx_char('-'); /* nak it */
  1737. #else
  1738. gdbstub_printk("GDB Stub set\n");
  1739. #endif
  1740. #if 0
  1741. /* send banner */
  1742. ptr = output_buffer;
  1743. *ptr++ = 'O';
  1744. ptr = mem2hex(gdbstub_banner, ptr, sizeof(gdbstub_banner) - 1, 0);
  1745. gdbstub_send_packet(output_buffer);
  1746. #endif
  1747. #if defined(CONFIG_GDB_CONSOLE) && defined(CONFIG_GDBSTUB_IMMEDIATE)
  1748. register_console(&gdbstub_console);
  1749. #endif
  1750. } /* end gdbstub_init() */
  1751. /*****************************************************************************/
  1752. /*
  1753. * register the console at a more appropriate time
  1754. */
  1755. #if defined (CONFIG_GDB_CONSOLE) && !defined(CONFIG_GDBSTUB_IMMEDIATE)
  1756. static int __init gdbstub_postinit(void)
  1757. {
  1758. printk("registering console\n");
  1759. register_console(&gdbstub_console);
  1760. return 0;
  1761. } /* end gdbstub_postinit() */
  1762. __initcall(gdbstub_postinit);
  1763. #endif
  1764. /*****************************************************************************/
  1765. /*
  1766. * send an exit message to GDB
  1767. */
  1768. void gdbstub_exit(int status)
  1769. {
  1770. unsigned char checksum;
  1771. int count;
  1772. unsigned char ch;
  1773. sprintf(output_buffer,"W%02x",status&0xff);
  1774. gdbstub_tx_char('$');
  1775. checksum = 0;
  1776. count = 0;
  1777. while ((ch = output_buffer[count]) != 0) {
  1778. gdbstub_tx_char(ch);
  1779. checksum += ch;
  1780. count += 1;
  1781. }
  1782. gdbstub_tx_char('#');
  1783. gdbstub_tx_char(hex_asc_hi(checksum));
  1784. gdbstub_tx_char(hex_asc_lo(checksum));
  1785. /* make sure the output is flushed, or else RedBoot might clobber it */
  1786. gdbstub_tx_char('-');
  1787. gdbstub_tx_flush();
  1788. } /* end gdbstub_exit() */
  1789. /*****************************************************************************/
  1790. /*
  1791. * GDB wants to call malloc() and free() to allocate memory for calling kernel
  1792. * functions directly from its command line
  1793. */
  1794. static void *malloc(size_t size) __maybe_unused;
  1795. static void *malloc(size_t size)
  1796. {
  1797. return kmalloc(size, GFP_ATOMIC);
  1798. }
  1799. static void free(void *p) __maybe_unused;
  1800. static void free(void *p)
  1801. {
  1802. kfree(p);
  1803. }
  1804. static uint32_t ___get_HSR0(void) __maybe_unused;
  1805. static uint32_t ___get_HSR0(void)
  1806. {
  1807. return __get_HSR(0);
  1808. }
  1809. static uint32_t ___set_HSR0(uint32_t x) __maybe_unused;
  1810. static uint32_t ___set_HSR0(uint32_t x)
  1811. {
  1812. __set_HSR(0, x);
  1813. return __get_HSR(0);
  1814. }