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 386 by Jim Kingdon, Cygnus Support.
34 * To enable debugger support, two things need to happen. One, a
35 * call to set_debug_traps() is necessary in order to allow any breakpoints
36 * or error conditions to be properly intercepted and reported to gdb.
37 * Two, a breakpoint needs to be generated to begin communication. This
38 * is most easily accomplished by a call to breakpoint(). Breakpoint()
39 * simulates a breakpoint by executing a trap #1.
41 * The external function exceptionHandler() is
42 * used to attach a specific handler to a specific 386 vector number.
43 * It should use the same privilege level it runs at. It should
44 * install it as an interrupt gate so that interrupts are masked
45 * while the handler runs.
46 * Also, need to assign exceptionHook and oldExceptionHook.
48 * Because gdb will sometimes write to the stack area to execute function
49 * calls, this program cannot rely on using the supervisor stack so it
50 * uses it's own stack area reserved in the int array remcomStack.
54 * The following gdb commands are supported:
56 * command function Return value
58 * g return the value of the CPU registers hex data or ENN
59 * G set the value of the CPU registers OK or ENN
61 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
62 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
64 * c Resume at current address SNN ( signal NN)
65 * cAA..AA Continue at address AA..AA SNN
67 * s Step one instruction SNN
68 * sAA..AA Step one instruction from AA..AA SNN
72 * ? What was the last sigval ? SNN (signal NN)
74 * All commands and responses are sent with a packet which includes a
75 * checksum. A packet consists of
77 * $<packet info>#<checksum>.
80 * <packet info> :: <characters representing the command or response>
81 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
83 * When a packet is received, it is first acknowledged with either '+' or '-'.
84 * '+' indicates a successful transfer. '-' indicates a failed transfer.
89 * $m0,10#2a +$00010203040506070809101112131415#42
91 ****************************************************************************/
96 /************************************************************************
98 * external low-level support routines
100 typedef void (*ExceptionHook
)(int); /* pointer to function with int parm */
101 typedef void (*Function
)(); /* pointer to a function */
103 extern void putDebugChar(); /* write a single character */
104 extern int getDebugChar(); /* read and return a single char */
106 extern Function
exceptionHandler(); /* assign an exception handler */
107 extern ExceptionHook exceptionHook
; /* hook variable for errors/exceptions */
109 /************************************************************************/
110 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
111 /* at least NUMREGBYTES*2 are needed for register packets */
114 static char initialized
; /* boolean flag. != 0 means we've been initialized */
117 /* debug > 0 prints ill-formed commands in valid packets & checksum errors */
119 static const char hexchars
[]="0123456789abcdef";
121 /* Number of registers. */
124 /* Number of bytes of registers. */
125 #define NUMREGBYTES (NUMREGS * 4)
127 enum regnames
{EAX
, ECX
, EDX
, EBX
, ESP
, EBP
, ESI
, EDI
,
128 PC
/* also known as eip */,
129 PS
/* also known as eflags */,
130 CS
, SS
, DS
, ES
, FS
, GS
};
133 * these should not be static cuz they can be used outside this module
135 int registers
[NUMREGS
];
137 #define STACKSIZE 10000
138 int remcomStack
[STACKSIZE
/sizeof(int)];
139 static int* stackPtr
= &remcomStack
[STACKSIZE
/sizeof(int) - 1];
142 * In many cases, the system will want to continue exception processing
143 * when a continue command is given.
144 * oldExceptionHook is a function to invoke in this case.
147 static ExceptionHook oldExceptionHook
;
149 /*************************** ASSEMBLY CODE MACROS *************************/
155 /* Restore the program's registers (including the stack pointer, which
156 means we get the right stack and don't have to worry about popping our
157 return address and any stack frames and so on) and return. */
159 asm(".globl _return_to_prog");
160 asm("_return_to_prog:");
161 asm(" movw _registers+44, %ss");
162 asm(" movl _registers+16, %esp");
163 asm(" movl _registers+4, %ecx");
164 asm(" movl _registers+8, %edx");
165 asm(" movl _registers+12, %ebx");
166 asm(" movl _registers+20, %ebp");
167 asm(" movl _registers+24, %esi");
168 asm(" movl _registers+28, %edi");
169 asm(" movw _registers+48, %ds");
170 asm(" movw _registers+52, %es");
171 asm(" movw _registers+56, %fs");
172 asm(" movw _registers+60, %gs");
173 asm(" movl _registers+36, %eax");
174 asm(" pushl %eax"); /* saved eflags */
175 asm(" movl _registers+40, %eax");
176 asm(" pushl %eax"); /* saved cs */
177 asm(" movl _registers+32, %eax");
178 asm(" pushl %eax"); /* saved eip */
179 asm(" movl _registers, %eax");
180 /* use iret to restore pc and flags together so
181 that trace flag works right. */
184 #define BREAKPOINT() asm(" int $3");
186 /* Put the error code here just in case the user cares. */
188 /* Likewise, the vector number here (since GDB only gets the signal
189 number through the usual means, and that's not very specific). */
190 int gdb_i386vector
= -1;
192 /* GDB stores segment registers in 32-bit words (that's just the way
193 m-i386v.h is written). So zero the appropriate areas in registers. */
194 #define SAVE_REGISTERS1() \
195 asm ("movl %eax, _registers"); \
196 asm ("movl %ecx, _registers+4"); \
197 asm ("movl %edx, _registers+8"); \
198 asm ("movl %ebx, _registers+12"); \
199 asm ("movl %ebp, _registers+20"); \
200 asm ("movl %esi, _registers+24"); \
201 asm ("movl %edi, _registers+28"); \
202 asm ("movw $0, %ax"); \
203 asm ("movw %ds, _registers+48"); \
204 asm ("movw %ax, _registers+50"); \
205 asm ("movw %es, _registers+52"); \
206 asm ("movw %ax, _registers+54"); \
207 asm ("movw %fs, _registers+56"); \
208 asm ("movw %ax, _registers+58"); \
209 asm ("movw %gs, _registers+60"); \
210 asm ("movw %ax, _registers+62");
211 #define SAVE_ERRCODE() \
213 asm ("movl %ebx, _gdb_i386errcode");
214 #define SAVE_REGISTERS2() \
215 asm ("popl %ebx"); /* old eip */ \
216 asm ("movl %ebx, _registers+32"); \
217 asm ("popl %ebx"); /* old cs */ \
218 asm ("movl %ebx, _registers+40"); \
219 asm ("movw %ax, _registers+42"); \
220 asm ("popl %ebx"); /* old eflags */ \
221 asm ("movl %ebx, _registers+36"); \
222 /* Now that we've done the pops, we can save the stack pointer."); */ \
223 asm ("movw %ss, _registers+44"); \
224 asm ("movw %ax, _registers+46"); \
225 asm ("movl %esp, _registers+16");
227 /* See if mem_fault_routine is set, if so just IRET to that address. */
228 #define CHECK_FAULT() \
229 asm ("cmpl $0, _mem_fault_routine"); \
230 asm ("jne mem_fault");
234 /* OK to clobber temp registers; we're just going to end up in set_mem_err. */
235 /* Pop error code from the stack and save it. */
237 asm (" movl %eax, _gdb_i386errcode");
239 asm (" popl %eax"); /* eip */
240 /* We don't want to return there, we want to return to the function
241 pointed to by mem_fault_routine instead. */
242 asm (" movl _mem_fault_routine, %eax");
243 asm (" popl %ecx"); /* cs (low 16 bits; junk in hi 16 bits). */
244 asm (" popl %edx"); /* eflags */
246 /* Remove this stack frame; when we do the iret, we will be going to
247 the start of a function, so we want the stack to look just like it
248 would after a "call" instruction. */
251 /* Push the stuff that iret wants. */
252 asm (" pushl %edx"); /* eflags */
253 asm (" pushl %ecx"); /* cs */
254 asm (" pushl %eax"); /* eip */
256 /* Zero mem_fault_routine. */
257 asm (" movl $0, %eax");
258 asm (" movl %eax, _mem_fault_routine");
262 #define CALL_HOOK() asm("call _remcomHandler");
264 /* This function is called when a i386 exception occurs. It saves
265 * all the cpu regs in the _registers array, munges the stack a bit,
266 * and invokes an exception handler (remcom_handler).
268 * stack on entry: stack on exit:
269 * old eflags vector number
270 * old cs (zero-filled to 32 bits)
274 extern void _catchException3();
276 asm(".globl __catchException3");
277 asm("__catchException3:");
283 /* Same thing for exception 1. */
284 extern void _catchException1();
286 asm(".globl __catchException1");
287 asm("__catchException1:");
293 /* Same thing for exception 0. */
294 extern void _catchException0();
296 asm(".globl __catchException0");
297 asm("__catchException0:");
303 /* Same thing for exception 4. */
304 extern void _catchException4();
306 asm(".globl __catchException4");
307 asm("__catchException4:");
313 /* Same thing for exception 5. */
314 extern void _catchException5();
316 asm(".globl __catchException5");
317 asm("__catchException5:");
323 /* Same thing for exception 6. */
324 extern void _catchException6();
326 asm(".globl __catchException6");
327 asm("__catchException6:");
333 /* Same thing for exception 7. */
334 extern void _catchException7();
336 asm(".globl __catchException7");
337 asm("__catchException7:");
343 /* Same thing for exception 8. */
344 extern void _catchException8();
346 asm(".globl __catchException8");
347 asm("__catchException8:");
354 /* Same thing for exception 9. */
355 extern void _catchException9();
357 asm(".globl __catchException9");
358 asm("__catchException9:");
364 /* Same thing for exception 10. */
365 extern void _catchException10();
367 asm(".globl __catchException10");
368 asm("__catchException10:");
375 /* Same thing for exception 12. */
376 extern void _catchException12();
378 asm(".globl __catchException12");
379 asm("__catchException12:");
386 /* Same thing for exception 16. */
387 extern void _catchException16();
389 asm(".globl __catchException16");
390 asm("__catchException16:");
396 /* For 13, 11, and 14 we have to deal with the CHECK_FAULT stuff. */
398 /* Same thing for exception 13. */
399 extern void _catchException13 ();
401 asm (".globl __catchException13");
402 asm ("__catchException13:");
410 /* Same thing for exception 11. */
411 extern void _catchException11 ();
413 asm (".globl __catchException11");
414 asm ("__catchException11:");
422 /* Same thing for exception 14. */
423 extern void _catchException14 ();
425 asm (".globl __catchException14");
426 asm ("__catchException14:");
435 * remcomHandler is a front end for handle_exception. It moves the
436 * stack pointer into an area reserved for debugger use.
438 asm("_remcomHandler:");
439 asm(" popl %eax"); /* pop off return address */
440 asm(" popl %eax"); /* get the exception number */
441 asm(" movl _stackPtr, %esp"); /* move to remcom stack area */
442 asm(" pushl %eax"); /* push exception onto stack */
443 asm(" call _handle_exception"); /* this never returns */
445 void _returnFromException()
453 if ((ch
>= 'a') && (ch
<= 'f')) return (ch
-'a'+10);
454 if ((ch
>= '0') && (ch
<= '9')) return (ch
-'0');
455 if ((ch
>= 'A') && (ch
<= 'F')) return (ch
-'A'+10);
459 /* scan for the sequence $<data>#<checksum> */
463 unsigned char *buffer
;
465 unsigned char checksum
;
466 unsigned char xmitcsum
;
472 /* wait around for the start character, ignore all other characters */
473 while ((ch
= getDebugChar ()) != '$')
481 /* now, read until a # or end of buffer is found */
482 while (count
< BUFMAX
)
484 ch
= getDebugChar ();
489 checksum
= checksum
+ ch
;
497 ch
= getDebugChar ();
498 xmitcsum
= hex (ch
) << 4;
499 ch
= getDebugChar ();
500 xmitcsum
+= hex (ch
);
502 if (checksum
!= xmitcsum
)
507 "bad checksum. My count = 0x%x, sent=0x%x. buf=%s\n",
508 checksum
, xmitcsum
, buffer
);
510 putDebugChar ('-'); /* failed checksum */
514 putDebugChar ('+'); /* successful transfer */
516 /* if a sequence char is present, reply the sequence ID */
517 if (buffer
[2] == ':')
519 putDebugChar (buffer
[0]);
520 putDebugChar (buffer
[1]);
531 /* send the packet in buffer. */
533 void putpacket(buffer
)
534 unsigned char *buffer
;
536 unsigned char checksum
;
540 /* $<packet info>#<checksum>. */
546 while (ch
=buffer
[count
]) {
553 putDebugChar(hexchars
[checksum
>> 4]);
554 putDebugChar(hexchars
[checksum
% 16]);
556 } while (getDebugChar() != '+');
560 char remcomInBuffer
[BUFMAX
];
561 char remcomOutBuffer
[BUFMAX
];
565 void debug_error(format
, parm
)
569 if (remote_debug
) fprintf (stderr
,format
,parm
);
572 /* Address of a routine to RTE to if we get a memory fault. */
573 static void (*volatile mem_fault_routine
)() = NULL
;
575 /* Indicate to caller of mem2hex or hex2mem that there has been an
577 static volatile int mem_err
= 0;
585 /* These are separate functions so that they are so short and sweet
586 that the compiler won't save any registers (if there is a fault
587 to mem_fault, they won't get restored, so there better not be any
604 /* convert the memory pointed to by mem into hex, placing result in buf */
605 /* return a pointer to the last char put in buf (null) */
606 /* If MAY_FAULT is non-zero, then we should set mem_err in response to
607 a fault; if zero treat a fault like any other fault in the stub. */
608 char* mem2hex(mem
, buf
, count
, may_fault
)
618 mem_fault_routine
= set_mem_err
;
619 for (i
=0;i
<count
;i
++) {
620 ch
= get_char (mem
++);
621 if (may_fault
&& mem_err
)
623 *buf
++ = hexchars
[ch
>> 4];
624 *buf
++ = hexchars
[ch
% 16];
628 mem_fault_routine
= NULL
;
632 /* convert the hex array pointed to by buf into binary to be placed in mem */
633 /* return a pointer to the character AFTER the last byte written */
634 char* hex2mem(buf
, mem
, count
, may_fault
)
644 mem_fault_routine
= set_mem_err
;
645 for (i
=0;i
<count
;i
++) {
646 ch
= hex(*buf
++) << 4;
647 ch
= ch
+ hex(*buf
++);
648 set_char (mem
++, ch
);
649 if (may_fault
&& mem_err
)
653 mem_fault_routine
= NULL
;
657 /* this function takes the 386 exception vector and attempts to
658 translate this number into a unix compatible signal value */
659 int computeSignal( exceptionVector
)
663 switch (exceptionVector
) {
664 case 0 : sigval
= 8; break; /* divide by zero */
665 case 1 : sigval
= 5; break; /* debug exception */
666 case 3 : sigval
= 5; break; /* breakpoint */
667 case 4 : sigval
= 16; break; /* into instruction (overflow) */
668 case 5 : sigval
= 16; break; /* bound instruction */
669 case 6 : sigval
= 4; break; /* Invalid opcode */
670 case 7 : sigval
= 8; break; /* coprocessor not available */
671 case 8 : sigval
= 7; break; /* double fault */
672 case 9 : sigval
= 11; break; /* coprocessor segment overrun */
673 case 10 : sigval
= 11; break; /* Invalid TSS */
674 case 11 : sigval
= 11; break; /* Segment not present */
675 case 12 : sigval
= 11; break; /* stack exception */
676 case 13 : sigval
= 11; break; /* general protection */
677 case 14 : sigval
= 11; break; /* page fault */
678 case 16 : sigval
= 7; break; /* coprocessor error */
680 sigval
= 7; /* "software generated"*/
685 /**********************************************/
686 /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
687 /* RETURN NUMBER OF CHARS PROCESSED */
688 /**********************************************/
689 int hexToInt(char **ptr
, int *intValue
)
698 hexValue
= hex(**ptr
);
701 *intValue
= (*intValue
<<4) | hexValue
;
714 * This function does all command procesing for interfacing to gdb.
716 void handle_exception(int exceptionVector
)
718 int sigval
, stepping
;
723 gdb_i386vector
= exceptionVector
;
725 if (remote_debug
) printf("vector=%d, sr=0x%x, pc=0x%x\n",
730 /* reply to host that an exception has occurred */
731 sigval
= computeSignal( exceptionVector
);
732 remcomOutBuffer
[0] = 'S';
733 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
734 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
735 remcomOutBuffer
[3] = 0;
737 putpacket(remcomOutBuffer
);
743 remcomOutBuffer
[0] = 0;
744 ptr
= getpacket(remcomInBuffer
);
747 case '?' : remcomOutBuffer
[0] = 'S';
748 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
749 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
750 remcomOutBuffer
[3] = 0;
752 case 'd' : remote_debug
= !(remote_debug
); /* toggle debug flag */
754 case 'g' : /* return the value of the CPU registers */
755 mem2hex((char*) registers
, remcomOutBuffer
, NUMREGBYTES
, 0);
757 case 'G' : /* set the value of the CPU registers - return OK */
758 hex2mem(ptr
, (char*) registers
, NUMREGBYTES
, 0);
759 strcpy(remcomOutBuffer
,"OK");
761 case 'P' : /* set the value of a single CPU register - return OK */
765 if (hexToInt (&ptr
, ®no
) && *ptr
++ == '=')
766 if (regno
>= 0 && regno
< NUMREGS
)
768 hex2mem (ptr
, (char *)®isters
[regno
], 4, 0);
769 strcpy(remcomOutBuffer
,"OK");
773 strcpy (remcomOutBuffer
, "E01");
777 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
779 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
780 if (hexToInt(&ptr
,&addr
))
782 if (hexToInt(&ptr
,&length
))
786 mem2hex((char*) addr
, remcomOutBuffer
, length
, 1);
788 strcpy (remcomOutBuffer
, "E03");
789 debug_error ("memory fault");
795 strcpy(remcomOutBuffer
,"E01");
799 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
801 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
802 if (hexToInt(&ptr
,&addr
))
804 if (hexToInt(&ptr
,&length
))
808 hex2mem(ptr
, (char*) addr
, length
, 1);
811 strcpy (remcomOutBuffer
, "E03");
812 debug_error ("memory fault");
814 strcpy(remcomOutBuffer
,"OK");
821 strcpy(remcomOutBuffer
,"E02");
825 /* cAA..AA Continue at address AA..AA(optional) */
826 /* sAA..AA Step one instruction from AA..AA(optional) */
830 /* try to read optional parameter, pc unchanged if no parm */
831 if (hexToInt(&ptr
,&addr
))
832 registers
[ PC
] = addr
;
834 newPC
= registers
[ PC
];
836 /* clear the trace bit */
837 registers
[ PS
] &= 0xfffffeff;
839 /* set the trace bit if we're stepping */
840 if (stepping
) registers
[ PS
] |= 0x100;
843 * If we found a match for the PC AND we are not returning
844 * as a result of a breakpoint (33),
845 * trace exception (9), nmi (31), jmp to
846 * the old exception handler as if this code never ran.
849 /* Don't really think we need this, except maybe for protection
852 * invoke the previous handler.
854 if (oldExceptionHook
)
855 (*oldExceptionHook
) (frame
->exceptionVector
);
856 newPC
= registers
[ PC
]; /* pc may have changed */
859 _returnFromException(); /* this is a jump */
863 /* kill the program */
864 case 'k' : /* do nothing */
866 /* Huh? This doesn't look like "nothing".
867 m68k-stub.c and sparc-stub.c don't have it. */
873 /* reply to the request */
874 putpacket(remcomOutBuffer
);
878 /* this function is used to set up exception handlers for tracing and
880 void set_debug_traps()
882 extern void remcomHandler();
885 stackPtr
= &remcomStack
[STACKSIZE
/sizeof(int) - 1];
887 exceptionHandler (0, _catchException0
);
888 exceptionHandler (1, _catchException1
);
889 exceptionHandler (3, _catchException3
);
890 exceptionHandler (4, _catchException4
);
891 exceptionHandler (5, _catchException5
);
892 exceptionHandler (6, _catchException6
);
893 exceptionHandler (7, _catchException7
);
894 exceptionHandler (8, _catchException8
);
895 exceptionHandler (9, _catchException9
);
896 exceptionHandler (10, _catchException10
);
897 exceptionHandler (11, _catchException11
);
898 exceptionHandler (12, _catchException12
);
899 exceptionHandler (13, _catchException13
);
900 exceptionHandler (14, _catchException14
);
901 exceptionHandler (16, _catchException16
);
903 if (exceptionHook
!= remcomHandler
)
905 oldExceptionHook
= exceptionHook
;
906 exceptionHook
= remcomHandler
;
912 /* This function will generate a breakpoint exception. It is used at the
913 beginning of a program to sync up with a debugger and can be used
914 otherwise as a quick means to stop program execution and "break" into