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);
460 /* scan for the sequence $<data>#<checksum> */
461 void getpacket(buffer
)
464 unsigned char checksum
;
465 unsigned char xmitcsum
;
471 /* wait around for the start character, ignore all other characters */
472 while ((ch
= (getDebugChar() & 0x7f)) != '$');
478 /* now, read until a # or end of buffer is found */
479 while (count
< BUFMAX
) {
480 ch
= getDebugChar() & 0x7f;
481 if (ch
== '#') break;
482 checksum
= checksum
+ ch
;
489 xmitcsum
= hex(getDebugChar() & 0x7f) << 4;
490 xmitcsum
+= hex(getDebugChar() & 0x7f);
491 if ((remote_debug
) && (checksum
!= xmitcsum
)) {
492 fprintf (stderr
,"bad checksum. My count = 0x%x, sent=0x%x. buf=%s\n",
493 checksum
,xmitcsum
,buffer
);
496 if (checksum
!= xmitcsum
) putDebugChar('-'); /* failed checksum */
498 putDebugChar('+'); /* successful transfer */
499 /* if a sequence char is present, reply the sequence ID */
500 if (buffer
[2] == ':') {
501 putDebugChar( buffer
[0] );
502 putDebugChar( buffer
[1] );
503 /* remove sequence chars from buffer */
504 count
= strlen(buffer
);
505 for (i
=3; i
<= count
; i
++) buffer
[i
-3] = buffer
[i
];
509 } while (checksum
!= xmitcsum
);
513 /* send the packet in buffer. */
516 void putpacket(buffer
)
519 unsigned char checksum
;
523 /* $<packet info>#<checksum>. */
529 while (ch
=buffer
[count
]) {
536 putDebugChar(hexchars
[checksum
>> 4]);
537 putDebugChar(hexchars
[checksum
% 16]);
539 } while ((getDebugChar() & 0x7f) != '+');
543 char remcomInBuffer
[BUFMAX
];
544 char remcomOutBuffer
[BUFMAX
];
548 void debug_error(format
, parm
)
552 if (remote_debug
) fprintf (stderr
,format
,parm
);
555 /* Address of a routine to RTE to if we get a memory fault. */
556 static void (*volatile mem_fault_routine
)() = NULL
;
558 /* Indicate to caller of mem2hex or hex2mem that there has been an
560 static volatile int mem_err
= 0;
568 /* These are separate functions so that they are so short and sweet
569 that the compiler won't save any registers (if there is a fault
570 to mem_fault, they won't get restored, so there better not be any
587 /* convert the memory pointed to by mem into hex, placing result in buf */
588 /* return a pointer to the last char put in buf (null) */
589 /* If MAY_FAULT is non-zero, then we should set mem_err in response to
590 a fault; if zero treat a fault like any other fault in the stub. */
591 char* mem2hex(mem
, buf
, count
, may_fault
)
601 mem_fault_routine
= set_mem_err
;
602 for (i
=0;i
<count
;i
++) {
603 ch
= get_char (mem
++);
604 if (may_fault
&& mem_err
)
606 *buf
++ = hexchars
[ch
>> 4];
607 *buf
++ = hexchars
[ch
% 16];
611 mem_fault_routine
= NULL
;
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 written */
617 char* hex2mem(buf
, mem
, count
, may_fault
)
627 mem_fault_routine
= set_mem_err
;
628 for (i
=0;i
<count
;i
++) {
629 ch
= hex(*buf
++) << 4;
630 ch
= ch
+ hex(*buf
++);
631 set_char (mem
++, ch
);
632 if (may_fault
&& mem_err
)
636 mem_fault_routine
= NULL
;
640 /* this function takes the 386 exception vector and attempts to
641 translate this number into a unix compatible signal value */
642 int computeSignal( exceptionVector
)
646 switch (exceptionVector
) {
647 case 0 : sigval
= 8; break; /* divide by zero */
648 case 1 : sigval
= 5; break; /* debug exception */
649 case 3 : sigval
= 5; break; /* breakpoint */
650 case 4 : sigval
= 16; break; /* into instruction (overflow) */
651 case 5 : sigval
= 16; break; /* bound instruction */
652 case 6 : sigval
= 4; break; /* Invalid opcode */
653 case 7 : sigval
= 8; break; /* coprocessor not available */
654 case 8 : sigval
= 7; break; /* double fault */
655 case 9 : sigval
= 11; break; /* coprocessor segment overrun */
656 case 10 : sigval
= 11; break; /* Invalid TSS */
657 case 11 : sigval
= 11; break; /* Segment not present */
658 case 12 : sigval
= 11; break; /* stack exception */
659 case 13 : sigval
= 11; break; /* general protection */
660 case 14 : sigval
= 11; break; /* page fault */
661 case 16 : sigval
= 7; break; /* coprocessor error */
663 sigval
= 7; /* "software generated"*/
668 /**********************************************/
669 /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
670 /* RETURN NUMBER OF CHARS PROCESSED */
671 /**********************************************/
672 int hexToInt(char **ptr
, int *intValue
)
681 hexValue
= hex(**ptr
);
684 *intValue
= (*intValue
<<4) | hexValue
;
697 * This function does all command procesing for interfacing to gdb.
699 void handle_exception(int exceptionVector
)
706 gdb_i386vector
= exceptionVector
;
708 if (remote_debug
) printf("vector=%d, sr=0x%x, pc=0x%x\n",
713 /* reply to host that an exception has occurred */
714 sigval
= computeSignal( exceptionVector
);
715 remcomOutBuffer
[0] = 'S';
716 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
717 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
718 remcomOutBuffer
[3] = 0;
720 putpacket(remcomOutBuffer
);
724 remcomOutBuffer
[0] = 0;
725 getpacket(remcomInBuffer
);
726 switch (remcomInBuffer
[0]) {
727 case '?' : remcomOutBuffer
[0] = 'S';
728 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
729 remcomOutBuffer
[2] = hexchars
[sigval
% 16];
730 remcomOutBuffer
[3] = 0;
732 case 'd' : remote_debug
= !(remote_debug
); /* toggle debug flag */
734 case 'g' : /* return the value of the CPU registers */
735 mem2hex((char*) registers
, remcomOutBuffer
, NUMREGBYTES
, 0);
737 case 'G' : /* set the value of the CPU registers - return OK */
738 hex2mem(&remcomInBuffer
[1], (char*) registers
, NUMREGBYTES
, 0);
739 strcpy(remcomOutBuffer
,"OK");
741 case 'P' : /* set the value of a single CPU register - return OK */
745 ptr
= &remcomInBuffer
[1];
746 if (hexToInt (&ptr
, ®no
) && *ptr
++ == '=')
747 if (regno
>= 0 && regno
< NUMREGS
)
749 hex2mem (ptr
, (char *)®isters
[regno
], 4, 0);
750 strcpy(remcomOutBuffer
,"OK");
754 strcpy (remcomOutBuffer
, "E01");
758 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
760 /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */
761 ptr
= &remcomInBuffer
[1];
762 if (hexToInt(&ptr
,&addr
))
764 if (hexToInt(&ptr
,&length
))
768 mem2hex((char*) addr
, remcomOutBuffer
, length
, 1);
770 strcpy (remcomOutBuffer
, "E03");
771 debug_error ("memory fault");
777 strcpy(remcomOutBuffer
,"E01");
778 debug_error("malformed read memory command: %s",remcomInBuffer
);
782 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
784 /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */
785 ptr
= &remcomInBuffer
[1];
786 if (hexToInt(&ptr
,&addr
))
788 if (hexToInt(&ptr
,&length
))
792 hex2mem(ptr
, (char*) addr
, length
, 1);
795 strcpy (remcomOutBuffer
, "E03");
796 debug_error ("memory fault");
798 strcpy(remcomOutBuffer
,"OK");
805 strcpy(remcomOutBuffer
,"E02");
806 debug_error("malformed write memory command: %s",remcomInBuffer
);
810 /* cAA..AA Continue at address AA..AA(optional) */
811 /* sAA..AA Step one instruction from AA..AA(optional) */
814 /* try to read optional parameter, pc unchanged if no parm */
815 ptr
= &remcomInBuffer
[1];
816 if (hexToInt(&ptr
,&addr
))
817 registers
[ PC
] = addr
;
819 newPC
= registers
[ PC
];
821 /* clear the trace bit */
822 registers
[ PS
] &= 0xfffffeff;
824 /* set the trace bit if we're stepping */
825 if (remcomInBuffer
[0] == 's') registers
[ PS
] |= 0x100;
828 * If we found a match for the PC AND we are not returning
829 * as a result of a breakpoint (33),
830 * trace exception (9), nmi (31), jmp to
831 * the old exception handler as if this code never ran.
834 /* Don't really think we need this, except maybe for protection
837 * invoke the previous handler.
839 if (oldExceptionHook
)
840 (*oldExceptionHook
) (frame
->exceptionVector
);
841 newPC
= registers
[ PC
]; /* pc may have changed */
844 _returnFromException(); /* this is a jump */
848 /* kill the program */
849 case 'k' : /* do nothing */
851 /* Huh? This doesn't look like "nothing".
852 m68k-stub.c and sparc-stub.c don't have it. */
858 /* reply to the request */
859 putpacket(remcomOutBuffer
);
863 /* this function is used to set up exception handlers for tracing and
865 void set_debug_traps()
867 extern void remcomHandler();
870 stackPtr
= &remcomStack
[STACKSIZE
/sizeof(int) - 1];
872 exceptionHandler (0, _catchException0
);
873 exceptionHandler (1, _catchException1
);
874 exceptionHandler (3, _catchException3
);
875 exceptionHandler (4, _catchException4
);
876 exceptionHandler (5, _catchException5
);
877 exceptionHandler (6, _catchException6
);
878 exceptionHandler (7, _catchException7
);
879 exceptionHandler (8, _catchException8
);
880 exceptionHandler (9, _catchException9
);
881 exceptionHandler (10, _catchException10
);
882 exceptionHandler (11, _catchException11
);
883 exceptionHandler (12, _catchException12
);
884 exceptionHandler (13, _catchException13
);
885 exceptionHandler (14, _catchException14
);
886 exceptionHandler (16, _catchException16
);
888 if (exceptionHook
!= remcomHandler
)
890 oldExceptionHook
= exceptionHook
;
891 exceptionHook
= remcomHandler
;
897 /* This function will generate a breakpoint exception. It is used at the
898 beginning of a program to sync up with a debugger and can be used
899 otherwise as a quick means to stop program execution and "break" into