add SDHC support in mmc driver
[u-boot-openmoko/mini2440.git] / common / kgdb.c
blobb14898be9214929dbe44b3bb7e100e4f98564f34
1 /* taken from arch/ppc/kernel/ppc-stub.c */
3 /****************************************************************************
5 THIS SOFTWARE IS NOT COPYRIGHTED
7 HP offers the following for use in the public domain. HP makes no
8 warranty with regard to the software or its performance and the
9 user accepts the software "AS IS" with all faults.
11 HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
12 TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
13 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15 ****************************************************************************/
17 /****************************************************************************
18 * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
20 * Module name: remcom.c $
21 * Revision: 1.34 $
22 * Date: 91/03/09 12:29:49 $
23 * Contributor: Lake Stevens Instrument Division$
25 * Description: low level support for gdb debugger. $
27 * Considerations: only works on target hardware $
29 * Written by: Glenn Engel $
30 * ModuleState: Experimental $
32 * NOTES: See Below $
34 * Modified for SPARC by Stu Grossman, Cygnus Support.
36 * This code has been extensively tested on the Fujitsu SPARClite demo board.
38 * To enable debugger support, two things need to happen. One, a
39 * call to set_debug_traps() is necessary in order to allow any breakpoints
40 * or error conditions to be properly intercepted and reported to gdb.
41 * Two, a breakpoint needs to be generated to begin communication. This
42 * is most easily accomplished by a call to breakpoint(). Breakpoint()
43 * simulates a breakpoint by executing a trap #1.
45 *************
47 * The following gdb commands are supported:
49 * command function Return value
51 * g return the value of the CPU registers hex data or ENN
52 * G set the value of the CPU registers OK or ENN
53 * qOffsets Get section offsets. Reply is Text=xxx;Data=yyy;Bss=zzz
55 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
56 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
58 * c Resume at current address SNN ( signal NN)
59 * cAA..AA Continue at address AA..AA SNN
61 * s Step one instruction SNN
62 * sAA..AA Step one instruction from AA..AA SNN
64 * k kill
66 * ? What was the last sigval ? SNN (signal NN)
68 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
69 * baud rate
71 * All commands and responses are sent with a packet which includes a
72 * checksum. A packet consists of
74 * $<packet info>#<checksum>.
76 * where
77 * <packet info> :: <characters representing the command or response>
78 * <checksum> :: <two hex digits computed as modulo 256 sum of <packetinfo>>
80 * When a packet is received, it is first acknowledged with either '+' or '-'.
81 * '+' indicates a successful transfer. '-' indicates a failed transfer.
83 * Example:
85 * Host: Reply:
86 * $m0,10#2a +$00010203040506070809101112131415#42
88 ****************************************************************************/
90 #include <common.h>
92 #include <kgdb.h>
93 #include <command.h>
95 #if defined(CONFIG_CMD_KGDB)
97 #undef KGDB_DEBUG
100 * BUFMAX defines the maximum number of characters in inbound/outbound buffers
102 #define BUFMAX 1024
103 static char remcomInBuffer[BUFMAX];
104 static char remcomOutBuffer[BUFMAX];
105 static char remcomRegBuffer[BUFMAX];
107 static int initialized = 0;
108 static int kgdb_active = 0, first_entry = 1;
109 static struct pt_regs entry_regs;
110 static long error_jmp_buf[BUFMAX/2];
111 static int longjmp_on_fault = 0;
112 #ifdef KGDB_DEBUG
113 static int kdebug = 1;
114 #endif
116 static const char hexchars[]="0123456789abcdef";
118 /* Convert ch from a hex digit to an int */
119 static int
120 hex(unsigned char ch)
122 if (ch >= 'a' && ch <= 'f')
123 return ch-'a'+10;
124 if (ch >= '0' && ch <= '9')
125 return ch-'0';
126 if (ch >= 'A' && ch <= 'F')
127 return ch-'A'+10;
128 return -1;
131 /* Convert the memory pointed to by mem into hex, placing result in buf.
132 * Return a pointer to the last char put in buf (null).
134 static unsigned char *
135 mem2hex(char *mem, char *buf, int count)
137 unsigned char ch;
139 longjmp_on_fault = 1;
140 while (count-- > 0) {
141 ch = *mem++;
142 *buf++ = hexchars[ch >> 4];
143 *buf++ = hexchars[ch & 0xf];
145 *buf = 0;
146 longjmp_on_fault = 0;
147 return (unsigned char *)buf;
150 /* convert the hex array pointed to by buf into binary to be placed in mem
151 * return a pointer to the character AFTER the last byte fetched from buf.
153 static char *
154 hex2mem(char *buf, char *mem, int count)
156 int i, hexValue;
157 unsigned char ch;
158 char *mem_start = mem;
160 longjmp_on_fault = 1;
161 for (i=0; i<count; i++) {
162 if ((hexValue = hex(*buf++)) < 0)
163 kgdb_error(KGDBERR_NOTHEXDIG);
164 ch = hexValue << 4;
165 if ((hexValue = hex(*buf++)) < 0)
166 kgdb_error(KGDBERR_NOTHEXDIG);
167 ch |= hexValue;
168 *mem++ = ch;
170 kgdb_flush_cache_range((void *)mem_start, (void *)(mem - 1));
171 longjmp_on_fault = 0;
173 return buf;
177 * While we find nice hex chars, build an int.
178 * Return number of chars processed.
180 static int
181 hexToInt(char **ptr, int *intValue)
183 int numChars = 0;
184 int hexValue;
186 *intValue = 0;
188 longjmp_on_fault = 1;
189 while (**ptr) {
190 hexValue = hex(**ptr);
191 if (hexValue < 0)
192 break;
194 *intValue = (*intValue << 4) | hexValue;
195 numChars ++;
197 (*ptr)++;
199 longjmp_on_fault = 0;
201 return (numChars);
204 /* scan for the sequence $<data>#<checksum> */
205 static void
206 getpacket(char *buffer)
208 unsigned char checksum;
209 unsigned char xmitcsum;
210 int i;
211 int count;
212 unsigned char ch;
214 do {
215 /* wait around for the start character, ignore all other
216 * characters */
217 while ((ch = (getDebugChar() & 0x7f)) != '$') {
218 #ifdef KGDB_DEBUG
219 if (kdebug)
220 putc(ch);
221 #endif
225 checksum = 0;
226 xmitcsum = -1;
228 count = 0;
230 /* now, read until a # or end of buffer is found */
231 while (count < BUFMAX) {
232 ch = getDebugChar() & 0x7f;
233 if (ch == '#')
234 break;
235 checksum = checksum + ch;
236 buffer[count] = ch;
237 count = count + 1;
240 if (count >= BUFMAX)
241 continue;
243 buffer[count] = 0;
245 if (ch == '#') {
246 xmitcsum = hex(getDebugChar() & 0x7f) << 4;
247 xmitcsum |= hex(getDebugChar() & 0x7f);
248 if (checksum != xmitcsum)
249 putDebugChar('-'); /* failed checksum */
250 else {
251 putDebugChar('+'); /* successful transfer */
252 /* if a sequence char is present, reply the ID */
253 if (buffer[2] == ':') {
254 putDebugChar(buffer[0]);
255 putDebugChar(buffer[1]);
256 /* remove sequence chars from buffer */
257 count = strlen(buffer);
258 for (i=3; i <= count; i++)
259 buffer[i-3] = buffer[i];
263 } while (checksum != xmitcsum);
266 /* send the packet in buffer. */
267 static void
268 putpacket(unsigned char *buffer)
270 unsigned char checksum;
271 int count;
272 unsigned char ch, recv;
274 /* $<packet info>#<checksum>. */
275 do {
276 putDebugChar('$');
277 checksum = 0;
278 count = 0;
280 while ((ch = buffer[count])) {
281 putDebugChar(ch);
282 checksum += ch;
283 count += 1;
286 putDebugChar('#');
287 putDebugChar(hexchars[checksum >> 4]);
288 putDebugChar(hexchars[checksum & 0xf]);
289 recv = getDebugChar();
290 } while ((recv & 0x7f) != '+');
294 * This function does all command processing for interfacing to gdb.
296 static int
297 handle_exception (struct pt_regs *regs)
299 int addr;
300 int length;
301 char *ptr;
302 kgdb_data kd;
303 int i;
305 if (!initialized) {
306 printf("kgdb: exception before kgdb is initialized! huh?\n");
307 return (0);
310 /* probably should check which exception occured as well */
311 if (longjmp_on_fault) {
312 longjmp_on_fault = 0;
313 kgdb_longjmp(error_jmp_buf, KGDBERR_MEMFAULT);
314 panic("kgdb longjump failed!\n");
317 if (kgdb_active) {
318 printf("kgdb: unexpected exception from within kgdb\n");
319 return (0);
321 kgdb_active = 1;
323 kgdb_interruptible(0);
325 printf("kgdb: handle_exception; trap [0x%x]\n", kgdb_trap(regs));
327 if (kgdb_setjmp(error_jmp_buf) != 0)
328 panic("kgdb: error or fault in entry init!\n");
330 kgdb_enter(regs, &kd);
332 if (first_entry) {
334 * the first time we enter kgdb, we save the processor
335 * state so that we can return to the monitor if the
336 * remote end quits gdb (or at least, tells us to quit
337 * with the 'k' packet)
339 entry_regs = *regs;
340 first_entry = 0;
343 ptr = remcomOutBuffer;
345 *ptr++ = 'T';
347 *ptr++ = hexchars[kd.sigval >> 4];
348 *ptr++ = hexchars[kd.sigval & 0xf];
350 for (i = 0; i < kd.nregs; i++) {
351 kgdb_reg *rp = &kd.regs[i];
353 *ptr++ = hexchars[rp->num >> 4];
354 *ptr++ = hexchars[rp->num & 0xf];
355 *ptr++ = ':';
356 ptr = (char *)mem2hex((char *)&rp->val, ptr, 4);
357 *ptr++ = ';';
360 *ptr = 0;
362 #ifdef KGDB_DEBUG
363 if (kdebug)
364 printf("kgdb: remcomOutBuffer: %s\n", remcomOutBuffer);
365 #endif
367 putpacket((unsigned char *)&remcomOutBuffer);
369 while (1) {
370 volatile int errnum;
372 remcomOutBuffer[0] = 0;
374 getpacket(remcomInBuffer);
375 ptr = &remcomInBuffer[1];
377 #ifdef KGDB_DEBUG
378 if (kdebug)
379 printf("kgdb: remcomInBuffer: %s\n", remcomInBuffer);
380 #endif
382 errnum = kgdb_setjmp(error_jmp_buf);
384 if (errnum == 0) switch (remcomInBuffer[0]) {
386 case '?': /* report most recent signal */
387 remcomOutBuffer[0] = 'S';
388 remcomOutBuffer[1] = hexchars[kd.sigval >> 4];
389 remcomOutBuffer[2] = hexchars[kd.sigval & 0xf];
390 remcomOutBuffer[3] = 0;
391 break;
393 #ifdef KGDB_DEBUG
394 case 'd':
395 /* toggle debug flag */
396 kdebug ^= 1;
397 break;
398 #endif
400 case 'g': /* return the value of the CPU registers. */
401 length = kgdb_getregs(regs, remcomRegBuffer, BUFMAX);
402 mem2hex(remcomRegBuffer, remcomOutBuffer, length);
403 break;
405 case 'G': /* set the value of the CPU registers */
406 length = strlen(ptr);
407 if ((length & 1) != 0) kgdb_error(KGDBERR_BADPARAMS);
408 hex2mem(ptr, remcomRegBuffer, length/2);
409 kgdb_putregs(regs, remcomRegBuffer, length/2);
410 strcpy(remcomOutBuffer,"OK");
411 break;
413 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
414 /* Try to read %x,%x. */
416 if (hexToInt(&ptr, &addr)
417 && *ptr++ == ','
418 && hexToInt(&ptr, &length)) {
419 mem2hex((char *)addr, remcomOutBuffer, length);
420 } else {
421 kgdb_error(KGDBERR_BADPARAMS);
423 break;
425 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
426 /* Try to read '%x,%x:'. */
428 if (hexToInt(&ptr, &addr)
429 && *ptr++ == ','
430 && hexToInt(&ptr, &length)
431 && *ptr++ == ':') {
432 hex2mem(ptr, (char *)addr, length);
433 strcpy(remcomOutBuffer, "OK");
434 } else {
435 kgdb_error(KGDBERR_BADPARAMS);
437 break;
440 case 'k': /* kill the program, actually return to monitor */
441 kd.extype = KGDBEXIT_KILL;
442 *regs = entry_regs;
443 first_entry = 1;
444 goto doexit;
446 case 'C': /* CSS continue with signal SS */
447 *ptr = '\0'; /* ignore the signal number for now */
448 /* fall through */
450 case 'c': /* cAA..AA Continue; address AA..AA optional */
451 /* try to read optional parameter, pc unchanged if no parm */
452 kd.extype = KGDBEXIT_CONTINUE;
454 if (hexToInt(&ptr, &addr)) {
455 kd.exaddr = addr;
456 kd.extype |= KGDBEXIT_WITHADDR;
459 goto doexit;
461 case 'S': /* SSS single step with signal SS */
462 *ptr = '\0'; /* ignore the signal number for now */
463 /* fall through */
465 case 's':
466 kd.extype = KGDBEXIT_SINGLE;
468 if (hexToInt(&ptr, &addr)) {
469 kd.exaddr = addr;
470 kd.extype |= KGDBEXIT_WITHADDR;
473 doexit:
474 /* Need to flush the instruction cache here, as we may have deposited a
475 * breakpoint, and the icache probably has no way of knowing that a data ref to
476 * some location may have changed something that is in the instruction cache.
478 kgdb_flush_cache_all();
479 kgdb_exit(regs, &kd);
480 kgdb_active = 0;
481 kgdb_interruptible(1);
482 return (1);
484 case 'r': /* Reset (if user process..exit ???)*/
485 panic("kgdb reset.");
486 break;
488 case 'P': /* Pr=v set reg r to value v (r and v are hex) */
489 if (hexToInt(&ptr, &addr)
490 && *ptr++ == '='
491 && ((length = strlen(ptr)) & 1) == 0) {
492 hex2mem(ptr, remcomRegBuffer, length/2);
493 kgdb_putreg(regs, addr,
494 remcomRegBuffer, length/2);
495 strcpy(remcomOutBuffer,"OK");
496 } else {
497 kgdb_error(KGDBERR_BADPARAMS);
499 break;
500 } /* switch */
502 if (errnum != 0)
503 sprintf(remcomOutBuffer, "E%02d", errnum);
505 #ifdef KGDB_DEBUG
506 if (kdebug)
507 printf("kgdb: remcomOutBuffer: %s\n", remcomOutBuffer);
508 #endif
510 /* reply to the request */
511 putpacket((unsigned char *)&remcomOutBuffer);
513 } /* while(1) */
517 * kgdb_init must be called *after* the
518 * monitor is relocated into ram
520 void
521 kgdb_init(void)
523 kgdb_serial_init();
524 debugger_exception_handler = handle_exception;
525 initialized = 1;
527 putDebugStr("kgdb ready\n");
528 puts("ready\n");
531 void
532 kgdb_error(int errnum)
534 longjmp_on_fault = 0;
535 kgdb_longjmp(error_jmp_buf, errnum);
536 panic("kgdb_error: longjmp failed!\n");
539 /* Output string in GDB O-packet format if GDB has connected. If nothing
540 output, returns 0 (caller must then handle output). */
542 kgdb_output_string (const char* s, unsigned int count)
544 char buffer[512];
546 count = (count <= (sizeof(buffer) / 2 - 2))
547 ? count : (sizeof(buffer) / 2 - 2);
549 buffer[0] = 'O';
550 mem2hex ((char *)s, &buffer[1], count);
551 putpacket((unsigned char *)&buffer);
553 return 1;
556 void
557 breakpoint(void)
559 if (!initialized) {
560 printf("breakpoint() called b4 kgdb init\n");
561 return;
564 kgdb_breakpoint(0, 0);
568 do_kgdb(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
570 printf("Entering KGDB mode via exception handler...\n\n");
571 kgdb_breakpoint(argc - 1, argv + 1);
572 printf("\nReturned from KGDB mode\n");
573 return 0;
576 U_BOOT_CMD(
577 kgdb, CFG_MAXARGS, 1, do_kgdb,
578 "kgdb - enter gdb remote debug mode\n",
579 "[arg0 arg1 .. argN]\n"
580 " - executes a breakpoint so that kgdb mode is\n"
581 " entered via the exception handler. To return\n"
582 " to the monitor, the remote gdb debugger must\n"
583 " execute a \"continue\" or \"quit\" command.\n"
584 "\n"
585 " if a program is loaded by the remote gdb, any args\n"
586 " passed to the kgdb command are given to the loaded\n"
587 " program if it is executed (see the \"hello_world\"\n"
588 " example program in the U-Boot examples directory)."
590 #else
592 int kgdb_not_configured = 1;
594 #endif