1 /****************************************************************************
3 THIS SOFTWARE IS NOT COPYRIGHTED
5 HP offers the following for use in the public domain. HP makes no
6 warranty with regard to the software or it's performance and the
7 user accepts the software "AS IS" with all faults.
9 HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
10 TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
11 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 ****************************************************************************/
15 /****************************************************************************
16 * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
18 * Module name: remcom.c $
20 * Date: 91/03/09 12:29:49 $
21 * Contributor: Lake Stevens Instrument Division$
23 * Description: low level support for gdb debugger. $
25 * Considerations: only works on target hardware $
27 * Written by: Glenn Engel $
28 * ModuleState: Experimental $
32 * Modified for SPARC by Stu Grossman, Cygnus Support.
34 * This code has been extensively tested on the Fujitsu SPARClite demo board.
36 * To enable debugger support, two things need to happen. One, a
37 * call to set_debug_traps() is necessary in order to allow any breakpoints
38 * or error conditions to be properly intercepted and reported to gdb.
39 * Two, a breakpoint needs to be generated to begin communication. This
40 * is most easily accomplished by a call to breakpoint(). Breakpoint()
41 * simulates a breakpoint by executing a trap #1.
45 * The following gdb commands are supported:
47 * command function Return value
49 * g return the value of the CPU registers hex data or ENN
50 * G set the value of the CPU registers OK or ENN
52 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
53 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
55 * c Resume at current address SNN ( signal NN)
56 * cAA..AA Continue at address AA..AA SNN
58 * s Step one instruction SNN
59 * sAA..AA Step one instruction from AA..AA SNN
63 * ? What was the last sigval ? SNN (signal NN)
65 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
68 * All commands and responses are sent with a packet which includes a
69 * checksum. A packet consists of
71 * $<packet info>#<checksum>.
74 * <packet info> :: <characters representing the command or response>
75 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
77 * When a packet is received, it is first acknowledged with either '+' or '-'.
78 * '+' indicates a successful transfer. '-' indicates a failed transfer.
83 * $m0,10#2a +$00010203040506070809101112131415#42
85 ****************************************************************************/
90 /************************************************************************
92 * external low-level support routines
95 extern void putDebugChar(); /* write a single character */
96 extern int getDebugChar(); /* read and return a single char */
98 /************************************************************************/
99 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
100 /* at least NUMREGBYTES*2 are needed for register packets */
103 static int initialized = 0; /* !0 means we've been initialized */
105 static void set_mem_fault_trap();
107 static const char hexchars[]="0123456789abcdef";
111 /* Number of bytes of registers. */
112 #define NUMREGBYTES (NUMREGS * 4)
113 enum regnames {G0, G1, G2, G3, G4, G5, G6, G7,
114 O0, O1, O2, O3, O4, O5, SP, O7,
115 L0, L1, L2, L3, L4, L5, L6, L7,
116 I0, I1, I2, I3, I4, I5, FP, I7,
118 F0, F1, F2, F3, F4, F5, F6, F7,
119 F8, F9, F10, F11, F12, F13, F14, F15,
120 F16, F17, F18, F19, F20, F21, F22, F23,
121 F24, F25, F26, F27, F28, F29, F30, F31,
122 Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR };
124 /*************************** ASSEMBLY CODE MACROS *************************/
127 extern void trap_low();
130 .reserve trapstack, 1000 * 4, \"bss\", 8
141 ! This function is called when any SPARC trap (except window overflow or
142 ! underflow) occurs. It makes sure that the invalid register window is still
143 ! available before jumping into C code. It will also restore the world if you
144 ! return from handle_exception.
151 srl %l3, %l0, %l4 ! wim >> cwp
153 bne window_fine ! Branch if not in the invalid window
156 ! Handle window overflow
158 mov %g1, %l4 ! Save g1, we use it to hold the wim
159 srl %l3, 1, %g1 ! Rotate wim right
161 bg good_wim ! Branch if new wim is non-zero
164 ! At this point, we need to bring a 1 into the high order bit of the wim.
165 ! Since we don't want to make any assumptions about the number of register
166 ! windows, we figure it out dynamically so as to setup the wim correctly.
168 not %g1 ! Fill g1 with ones
169 mov %g1, %wim ! Fill the wim with ones
173 mov %wim, %g1 ! Read back the wim
174 inc %g1 ! Now g1 has 1 just to left of wim
175 srl %g1, 1, %g1 ! Now put 1 at top of wim
176 mov %g0, %wim ! Clear wim so that subsequent save
182 save %g0, %g0, %g0 ! Slip into next window
183 mov %g1, %wim ! Install the new wim
185 std %l0, [%sp + 0 * 4] ! save L & I registers
186 std %l2, [%sp + 2 * 4]
187 std %l4, [%sp + 4 * 4]
188 std %l6, [%sp + 6 * 4]
190 std %i0, [%sp + 8 * 4]
191 std %i2, [%sp + 10 * 4]
192 std %i4, [%sp + 12 * 4]
193 std %i6, [%sp + 14 * 4]
195 restore ! Go back to trap window.
196 mov %l4, %g1 ! Restore %g1
199 sethi %hi(in_trap_handler), %l4
200 ld [%lo(in_trap_handler) + %l4], %l5
205 set trapstack+1000*4, %sp ! Switch to trap stack
208 st %l5, [%lo(in_trap_handler) + %l4]
209 sub %sp,(16+1+6+1+72)*4,%sp ! Make room for input & locals
210 ! + hidden arg + arg spill
211 ! + doubleword alignment
212 ! + registers[72] local var
214 std %g0, [%sp + (24 + 0) * 4] ! registers[Gx]
215 std %g2, [%sp + (24 + 2) * 4]
216 std %g4, [%sp + (24 + 4) * 4]
217 std %g6, [%sp + (24 + 6) * 4]
219 std %i0, [%sp + (24 + 8) * 4] ! registers[Ox]
220 std %i2, [%sp + (24 + 10) * 4]
221 std %i4, [%sp + (24 + 12) * 4]
222 std %i6, [%sp + (24 + 14) * 4]
223 ! F0->F31 not implemented
226 st %l4, [%sp + (24 + 64) * 4] ! Y
227 st %l0, [%sp + (24 + 65) * 4] ! PSR
228 st %l3, [%sp + (24 + 66) * 4] ! WIM
229 st %l5, [%sp + (24 + 67) * 4] ! TBR
230 st %l1, [%sp + (24 + 68) * 4] ! PC
231 st %l2, [%sp + (24 + 69) * 4] ! NPC
233 ! CPSR and FPSR not impl
236 mov %l4, %psr ! Turn on traps, disable interrupts
238 call _handle_exception
239 add %sp, 24 * 4, %o0 ! Pass address of registers
241 ! Reload all of the registers that aren't on the stack
243 ld [%sp + (24 + 1) * 4], %g1 ! registers[Gx]
244 ldd [%sp + (24 + 2) * 4], %g2
245 ldd [%sp + (24 + 4) * 4], %g4
246 ldd [%sp + (24 + 6) * 4], %g6
248 ldd [%sp + (24 + 8) * 4], %i0 ! registers[Ox]
249 ldd [%sp + (24 + 10) * 4], %i2
250 ldd [%sp + (24 + 12) * 4], %i4
251 ldd [%sp + (24 + 14) * 4], %i6
253 ldd [%sp + (24 + 64) * 4], %l0 ! Y & PSR
254 ldd [%sp + (24 + 68) * 4], %l2 ! PC & NPC
256 restore ! Ensure that previous window is valid
257 save %g0, %g0, %g0 ! by causing a window_underflow trap
260 mov %l1, %psr ! Make sure that traps are disabled
263 sethi %hi(in_trap_handler), %l4
264 ld [%lo(in_trap_handler) + %l4], %l5
266 st %l5, [%lo(in_trap_handler) + %l4]
268 jmpl %l2, %g0 ! Restore old PC
269 rett %l3 ! Restore old nPC
272 /* Convert ch from a hex digit to an int */
278 if (ch >= 'a' && ch <= 'f')
280 if (ch >= '0' && ch <= '9')
282 if (ch >= 'A' && ch <= 'F')
287 /* scan for the sequence $<data>#<checksum> */
293 unsigned char checksum;
294 unsigned char xmitcsum;
301 /* wait around for the start character, ignore all other characters */
302 while ((ch = (getDebugChar() & 0x7f)) != '$') ;
309 /* now, read until a # or end of buffer is found */
310 while (count < BUFMAX)
312 ch = getDebugChar() & 0x7f;
315 checksum = checksum + ch;
327 xmitcsum = hex(getDebugChar() & 0x7f) << 4;
328 xmitcsum |= hex(getDebugChar() & 0x7f);
330 /* Humans shouldn't have to figure out checksums to type to it. */
334 if (checksum != xmitcsum)
335 putDebugChar('-'); /* failed checksum */
338 putDebugChar('+'); /* successful transfer */
339 /* if a sequence char is present, reply the sequence ID */
340 if (buffer[2] == ':')
342 putDebugChar(buffer[0]);
343 putDebugChar(buffer[1]);
344 /* remove sequence chars from buffer */
345 count = strlen(buffer);
346 for (i=3; i <= count; i++)
347 buffer[i-3] = buffer[i];
352 while (checksum != xmitcsum);
355 /* send the packet in buffer. */
359 unsigned char *buffer;
361 unsigned char checksum;
365 /* $<packet info>#<checksum>. */
372 while (ch = buffer[count])
380 putDebugChar(hexchars[checksum >> 4]);
381 putDebugChar(hexchars[checksum & 0xf]);
384 while ((getDebugChar() & 0x7f) != '+');
387 static char remcomInBuffer[BUFMAX];
388 static char remcomOutBuffer[BUFMAX];
390 /* Indicate to caller of mem2hex or hex2mem that there has been an
392 static volatile int mem_err = 0;
394 /* Convert the memory pointed to by mem into hex, placing result in buf.
395 * Return a pointer to the last char put in buf (null), in case of mem fault,
397 * If MAY_FAULT is non-zero, then we will handle memory faults by returning
398 * a 0, else treat a fault like any other fault in the stub.
401 static unsigned char *
402 mem2hex(mem, buf, count, may_fault)
410 set_mem_fault_trap(may_fault);
417 *buf++ = hexchars[ch >> 4];
418 *buf++ = hexchars[ch & 0xf];
423 set_mem_fault_trap(0);
428 /* convert the hex array pointed to by buf into binary to be placed in mem
429 * return a pointer to the character AFTER the last byte written */
432 hex2mem(buf, mem, count, may_fault)
441 set_mem_fault_trap(may_fault);
443 for (i=0; i<count; i++)
445 ch = hex(*buf++) << 4;
452 set_mem_fault_trap(0);
457 /* This table contains the mapping between SPARC hardware trap types, and
458 signals, which are primarily what GDB understands. It also indicates
459 which hardware traps we need to commandeer when initializing the stub. */
461 static struct hard_trap_info
463 unsigned char tt; /* Trap type code for SPARClite */
464 unsigned char signo; /* Signal that we map this trap into */
465 } hard_trap_info[] = {
466 {1, SIGSEGV}, /* instruction access error */
467 {2, SIGILL}, /* privileged instruction */
468 {3, SIGILL}, /* illegal instruction */
469 {4, SIGEMT}, /* fp disabled */
470 {36, SIGEMT}, /* cp disabled */
471 {7, SIGBUS}, /* mem address not aligned */
472 {9, SIGSEGV}, /* data access exception */
473 {10, SIGEMT}, /* tag overflow */
474 {128+1, SIGTRAP}, /* ta 1 - normal breakpoint instruction */
475 {0, 0} /* Must be last */
478 /* Set up exception handlers for tracing and breakpoints */
483 struct hard_trap_info *ht;
485 for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
486 exceptionHandler(ht->tt, trap_low);
488 /* In case GDB is started before us, ack any packets (presumably
489 "$?#xx") sitting there. */
497 ! Trap handler for memory errors. This just sets mem_err to be non-zero. It
498 ! assumes that %l1 is non-zero. This should be safe, as it is doubtful that
499 ! 0 would ever contain code that could mem fault. This routine will skip
500 ! past the faulting instruction after setting mem_err.
506 sethi %hi(_mem_err), %l0
507 st %l1, [%l0 + %lo(_mem_err)]
513 set_mem_fault_trap(enable)
516 extern void fltr_set_mem_err();
520 exceptionHandler(9, fltr_set_mem_err);
522 exceptionHandler(9, trap_low);
525 /* Convert the SPARC hardware trap type code to a unix signal number. */
531 struct hard_trap_info *ht;
533 for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
537 return SIGHUP; /* default for things we don't know about */
541 * While we find nice hex chars, build an int.
542 * Return number of chars processed.
546 hexToInt(char **ptr, int *intValue)
555 hexValue = hex(**ptr);
559 *intValue = (*intValue << 4) | hexValue;
569 * This function does all command procesing for interfacing to gdb. It
570 * returns 1 if you should skip the instruction at the trap address, 0
574 extern void breakinst();
577 handle_exception (registers)
578 unsigned long *registers;
580 int tt; /* Trap type */
587 /* First, we must force all of the windows to be spilled out */
589 asm(" save %sp, -64, %sp
607 if (registers[PC] == (unsigned long)breakinst)
609 registers[PC] = registers[NPC];
613 sp = (unsigned long *)registers[SP];
615 tt = (registers[TBR] >> 4) & 0xff;
617 /* reply to host that an exception has occurred */
618 sigval = computeSignal(tt);
619 ptr = remcomOutBuffer;
622 *ptr++ = hexchars[sigval >> 4];
623 *ptr++ = hexchars[sigval & 0xf];
625 *ptr++ = hexchars[PC >> 4];
626 *ptr++ = hexchars[PC & 0xf];
628 ptr = mem2hex((char *)®isters[PC], ptr, 4, 0);
631 *ptr++ = hexchars[FP >> 4];
632 *ptr++ = hexchars[FP & 0xf];
634 ptr = mem2hex(sp + 8 + 6, ptr, 4, 0); /* FP */
637 *ptr++ = hexchars[SP >> 4];
638 *ptr++ = hexchars[SP & 0xf];
640 ptr = mem2hex((char *)&sp, ptr, 4, 0);
643 *ptr++ = hexchars[NPC >> 4];
644 *ptr++ = hexchars[NPC & 0xf];
646 ptr = mem2hex((char *)®isters[NPC], ptr, 4, 0);
649 *ptr++ = hexchars[O7 >> 4];
650 *ptr++ = hexchars[O7 & 0xf];
652 ptr = mem2hex((char *)®isters[O7], ptr, 4, 0);
657 putpacket(remcomOutBuffer);
661 remcomOutBuffer[0] = 0;
663 getpacket(remcomInBuffer);
664 switch (remcomInBuffer[0])
667 remcomOutBuffer[0] = 'S';
668 remcomOutBuffer[1] = hexchars[sigval >> 4];
669 remcomOutBuffer[2] = hexchars[sigval & 0xf];
670 remcomOutBuffer[3] = 0;
674 /* toggle debug flag */
677 case 'g': /* return the value of the CPU registers */
679 ptr = remcomOutBuffer;
680 ptr = mem2hex((char *)registers, ptr, 16 * 4, 0); /* G & O regs */
681 ptr = mem2hex(sp + 0, ptr, 16 * 4, 0); /* L & I regs */
682 memset(ptr, '0', 32 * 8); /* Floating point */
683 mem2hex((char *)®isters[Y],
686 0); /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
690 case 'G': /* set the value of the CPU registers - return OK */
692 unsigned long *newsp, psr;
694 psr = registers[PSR];
696 ptr = &remcomInBuffer[1];
697 hex2mem(ptr, (char *)registers, 16 * 4, 0); /* G & O regs */
698 hex2mem(ptr + 16 * 4 * 2, sp + 0, 16 * 4, 0); /* L & I regs */
699 hex2mem(ptr + 64 * 4 * 2, (char *)®isters[Y],
700 8 * 4, 0); /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
702 /* See if the stack pointer has moved. If so, then copy the saved
703 locals and ins to the new location. This keeps the window
704 overflow and underflow routines happy. */
706 newsp = (unsigned long *)registers[SP];
708 sp = memcpy(newsp, sp, 16 * 4);
710 /* Don't allow CWP to be modified. */
712 if (psr != registers[PSR])
713 registers[PSR] = (psr & 0x1f) | (registers[PSR] & ~0x1f);
715 strcpy(remcomOutBuffer,"OK");
719 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
720 /* Try to read %x,%x. */
722 ptr = &remcomInBuffer[1];
724 if (hexToInt(&ptr, &addr)
726 && hexToInt(&ptr, &length))
728 if (mem2hex((char *)addr, remcomOutBuffer, length, 1))
731 strcpy (remcomOutBuffer, "E03");
734 strcpy(remcomOutBuffer,"E01");
737 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
738 /* Try to read '%x,%x:'. */
740 ptr = &remcomInBuffer[1];
742 if (hexToInt(&ptr, &addr)
744 && hexToInt(&ptr, &length)
747 if (hex2mem(ptr, (char *)addr, length, 1))
748 strcpy(remcomOutBuffer, "OK");
750 strcpy(remcomOutBuffer, "E03");
753 strcpy(remcomOutBuffer, "E02");
756 case 'c': /* cAA..AA Continue at address AA..AA(optional) */
757 /* try to read optional parameter, pc unchanged if no parm */
759 ptr = &remcomInBuffer[1];
760 if (hexToInt(&ptr, &addr))
762 registers[PC] = addr;
763 registers[NPC] = addr + 4;
766 /* Need to flush the instruction cache here, as we may have deposited a
767 breakpoint, and the icache probably has no way of knowing that a data ref to
768 some location may have changed something that is in the instruction cache.
774 /* kill the program */
775 case 'k' : /* do nothing */
778 case 't': /* Test feature */
779 asm (" std %f30,[%sp]");
782 case 'r': /* Reset */
788 Disabled until we can unscrew this properly
790 case 'b': /* bBB... Set baud rate to BB... */
793 extern void set_timer_3();
795 ptr = &remcomInBuffer[1];
796 if (!hexToInt(&ptr, &baudrate))
798 strcpy(remcomOutBuffer,"B01");
802 /* Convert baud rate to uart clock divider */
815 strcpy(remcomOutBuffer,"B02");
819 putpacket("OK"); /* Ack before changing speed */
820 set_timer_3(baudrate); /* Set it */
826 /* reply to the request */
827 putpacket(remcomOutBuffer);
831 /* This function will generate a breakpoint exception. It is used at the
832 beginning of a program to sync up with a debugger and can be used
833 otherwise as a quick means to stop program execution and "break" into
842 asm(" .globl _breakinst