Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/kdb/i386-stub.c need to be fixed.

0001 /*                    The Quest Operating System
0002  *  Copyright (C) 2005-2010  Richard West, Boston University
0003  *
0004  *  This program is free software: you can redistribute it and/or modify
0005  *  it under the terms of the GNU General Public License as published by
0006  *  the Free Software Foundation, either version 3 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 /****************************************************************************
0019 
0020         THIS SOFTWARE IS NOT COPYRIGHTED
0021 
0022    HP offers the following for use in the public domain.  HP makes no
0023    warranty with regard to the software or it's performance and the
0024    user accepts the software "AS IS" with all faults.
0025 
0026    HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
0027    TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0028    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
0029 
0030 ****************************************************************************/
0031 
0032 /****************************************************************************
0033  *  Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
0034  *
0035  *  Module name: remcom.c $
0036  *  Revision: 1.34 $
0037  *  Date: 91/03/09 12:29:49 $
0038  *  Contributor:     Lake Stevens Instrument Division$
0039  *
0040  *  Description:     low level support for gdb debugger. $
0041  *
0042  *  Considerations:  only works on target hardware $
0043  *
0044  *  Written by:      Glenn Engel $
0045  *  ModuleState:     Experimental $
0046  *
0047  *  NOTES:           See Below $
0048  *
0049  *  Modified for 386 by Jim Kingdon, Cygnus Support.
0050  *
0051  *  To enable debugger support, two things need to happen.  One, a
0052  *  call to set_debug_traps() is necessary in order to allow any breakpoints
0053  *  or error conditions to be properly intercepted and reported to gdb.
0054  *  Two, a breakpoint needs to be generated to begin communication.  This
0055  *  is most easily accomplished by a call to breakpoint().  Breakpoint()
0056  *  simulates a breakpoint by executing a trap #1.
0057  *
0058  *  The external function exceptionHandler() is
0059  *  used to attach a specific handler to a specific 386 vector number.
0060  *  It should use the same privilege level it runs at.  It should
0061  *  install it as an interrupt gate so that interrupts are masked
0062  *  while the handler runs.
0063  *
0064  *  Because gdb will sometimes write to the stack area to execute function
0065  *  calls, this program cannot rely on using the supervisor stack so it
0066  *  uses it's own stack area reserved in the int array remcomStack.
0067  *
0068  *************
0069  *
0070  *    The following gdb commands are supported:
0071  *
0072  * command          function                               Return value
0073  *
0074  *    g             return the value of the CPU registers  hex data or ENN
0075  *    G             set the value of the CPU registers     OK or ENN
0076  *
0077  *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
0078  *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
0079  *
0080  *    c             Resume at current address              SNN   ( signal NN)
0081  *    cAA..AA       Continue at address AA..AA             SNN
0082  *
0083  *    s             Step one instruction                   SNN
0084  *    sAA..AA       Step one instruction from AA..AA       SNN
0085  *
0086  *    k             kill
0087  *
0088  *    ?             What was the last sigval ?             SNN   (signal NN)
0089  *
0090  * All commands and responses are sent with a packet which includes a
0091  * checksum.  A packet consists of
0092  *
0093  * $<packet info>#<checksum>.
0094  *
0095  * where
0096  * <packet info> :: <characters representing the command or response>
0097  * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
0098  *
0099  * When a packet is received, it is first acknowledged with either '+' or '-'.
0100  * '+' indicates a successful transfer.  '-' indicates a failed transfer.
0101  *
0102  * Example:
0103  *
0104  * Host:                  Reply:
0105  * $m0,10#2a               +$00010203040506070809101112131415#42
0106  *
0107  ****************************************************************************/
0108 
0109 /*                    The Quest Operating System
0110  *  Portions Copyright (C) 2005-2010  Richard West, Boston University
0111  *
0112  *  This program is free software: you can redistribute it and/or modify
0113  *  it under the terms of the GNU General Public License as published by
0114  *  the Free Software Foundation, either version 3 of the License, or
0115  *  (at your option) any later version.
0116  *
0117  *  This program is distributed in the hope that it will be useful,
0118  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0119  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0120  *  GNU General Public License for more details.
0121  *
0122  *  You should have received a copy of the GNU General Public License
0123  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0124  */
0125 
0126 /* Ported to Quest by: Matthew Danish (md) */
0127 
0128 
0129 #include "types.h"
0130 #include "kernel.h"
0131 #include "util/printf.h"
0132 #include "arch/i386.h"
0133 
0134 /************************************************************************
0135  *
0136  * external low-level support routines
0137  */
0138 
0139 extern void putDebugChar(int);  /* write a single character      */
0140 extern int getDebugChar();  /* read and return a single char */
0141 //extern void exceptionHandler();   /* assign an exception handler   */
0142 #define exceptionHandler(n,h) set_idt_descriptor_by_addr (n, h, 0x3);
0143 
0144 /************************************************************************/
0145 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
0146 /* at least NUMREGBYTES*2 are needed for register packets */
0147 #define BUFMAX 400
0148 
0149 static char initialized;  /* boolean flag. != 0 means we've been initialized */
0150 
0151 int     remote_debug;
0152 /*  debug >  0 prints ill-formed commands in valid packets & checksum errors */
0153 
0154 static const char hexchars[]="0123456789abcdef";
0155 
0156 /* Number of registers.  */
0157 #define NUMREGS 16
0158 
0159 /* Number of bytes of registers.  */
0160 #define NUMREGBYTES (NUMREGS * 4)
0161 
0162 enum regnames {EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
0163            PC /* also known as eip */,
0164            PS /* also known as eflags */,
0165            CS, SS, DS, ES, FS, GS};
0166 
0167 /*
0168  * these should not be static cuz they can be used outside this module
0169  */
0170 int _registers[NUMREGS];
0171 
0172 #define STACKSIZE 10000
0173 int remcomStack[STACKSIZE/sizeof(int)];
0174 static int* stackPtr = &remcomStack[STACKSIZE/sizeof(int) - 1];
0175 
0176 /***************************  ASSEMBLY CODE MACROS *************************/
0177 /*                                     */
0178 
0179 extern void
0180 return_to_prog ();
0181 
0182 /* Restore the program's registers (including the stack pointer, which
0183    means we get the right stack and don't have to worry about popping our
0184    return address and any stack frames and so on) and return.  */
0185 asm(".text");
0186 asm(".globl return_to_prog");
0187 asm("return_to_prog:");
0188 asm("        movw _registers+44, %ss");
0189 asm("        movl _registers+16, %esp");
0190 asm("        movl _registers+4, %ecx");
0191 asm("        movl _registers+8, %edx");
0192 asm("        movl _registers+12, %ebx");
0193 asm("        movl _registers+20, %ebp");
0194 asm("        movl _registers+24, %esi");
0195 asm("        movl _registers+28, %edi");
0196 asm("        movw _registers+48, %ds");
0197 asm("        movw _registers+52, %es");
0198 asm("        movw _registers+56, %fs");
0199 asm("        movw _registers+60, %gs");
0200 asm("        movl _registers+36, %eax");
0201 asm("        pushl %eax");  /* saved eflags */
0202 asm("        movl _registers+40, %eax");
0203 asm("        pushl %eax");  /* saved cs */
0204 asm("        movl _registers+32, %eax");
0205 asm("        pushl %eax");  /* saved eip */
0206 asm("        movl _registers, %eax");
0207 /* use iret to restore pc and flags together so
0208    that trace flag works right.  */
0209 asm("        iret");
0210 
0211 /* Put the error code here just in case the user cares.  */
0212 int gdb_i386errcode;
0213 /* Likewise, the vector number here (since GDB only gets the signal
0214    number through the usual means, and that's not very specific).  */
0215 int gdb_i386vector = -1;
0216 
0217 /* GDB stores segment registers in 32-bit words (that's just the way
0218    m-i386v.h is written).  So zero the appropriate areas in registers.  */
0219 #define SAVE_REGISTERS1() \
0220   asm ("movl %eax, _registers");                                      \
0221   asm ("movl %ecx, _registers+4");                       \
0222   asm ("movl %edx, _registers+8");                       \
0223   asm ("movl %ebx, _registers+12");                      \
0224   asm ("movl %ebp, _registers+20");                      \
0225   asm ("movl %esi, _registers+24");                      \
0226   asm ("movl %edi, _registers+28");                      \
0227   asm ("movw $0, %ax");                              \
0228   asm ("movw %ds, _registers+48");                       \
0229   asm ("movw %ax, _registers+50");                       \
0230   asm ("movw %es, _registers+52");                       \
0231   asm ("movw %ax, _registers+54");                       \
0232   asm ("movw %fs, _registers+56");                       \
0233   asm ("movw %ax, _registers+58");                       \
0234   asm ("movw %gs, _registers+60");                       \
0235   asm ("movw %ax, _registers+62");
0236 #define SAVE_ERRCODE() \
0237   asm ("popl %ebx");                                  \
0238   asm ("movl %ebx, gdb_i386errcode");
0239 #define SAVE_REGISTERS2() \
0240   asm ("popl %ebx"); /* old eip */                       \
0241   asm ("movl %ebx, _registers+32");                      \
0242   asm ("popl %ebx");     /* old cs */                        \
0243   asm ("movl %ebx, _registers+40");                      \
0244   asm ("movw %ax, _registers+42");                                           \
0245   asm ("popl %ebx");     /* old eflags */                    \
0246   asm ("movl %ebx, _registers+36");                      \
0247   /* Now that we've done the pops, we can save the stack pointer.");  */   \
0248   asm ("movw %ss, _registers+44");                       \
0249   asm ("movw %ax, _registers+46");                                           \
0250   asm ("movl %esp, _registers+16");
0251 
0252 /* See if mem_fault_routine is set, if so just IRET to that address.  */
0253 #define CHECK_FAULT() \
0254   asm ("cmpl $0, _mem_fault_routine");                     \
0255   asm ("jne mem_fault");
0256 
0257 asm (".text");
0258 asm ("mem_fault:");
0259 /* OK to clobber temp registers; we're just going to end up in set_mem_err.  */
0260 /* Pop error code from the stack and save it.  */
0261 asm ("     popl %eax");
0262 asm ("     movl %eax, gdb_i386errcode");
0263 
0264 asm ("     popl %eax"); /* eip */
0265 /* We don't want to return there, we want to return to the function
0266    pointed to by mem_fault_routine instead.  */
0267 asm ("     movl _mem_fault_routine, %eax");
0268 asm ("     popl %ecx"); /* cs (low 16 bits; junk in hi 16 bits).  */
0269 asm ("     popl %edx"); /* eflags */
0270 
0271 /* Remove this stack frame; when we do the iret, we will be going to
0272    the start of a function, so we want the stack to look just like it
0273    would after a "call" instruction.  */
0274 asm ("     leave");
0275 
0276 /* Push the stuff that iret wants.  */
0277 asm ("     pushl %edx"); /* eflags */
0278 asm ("     pushl %ecx"); /* cs */
0279 asm ("     pushl %eax"); /* eip */
0280 
0281 /* Zero mem_fault_routine.  */
0282 asm ("     movl $0, %eax");
0283 asm ("     movl %eax, _mem_fault_routine");
0284 
0285 asm ("iret");
0286 
0287 #define CALL_HOOK() asm("call _remcomHandler");
0288 
0289 /* This function is called when a i386 exception occurs.  It saves
0290  * all the cpu regs in the _registers array, munges the stack a bit,
0291  * and invokes an exception handler (remcom_handler).
0292  *
0293  * stack on entry:                       stack on exit:
0294  *   old eflags                          vector number
0295  *   old cs (zero-filled to 32 bits)
0296  *   old eip
0297  *
0298  */
0299 extern void __catchException3();
0300 asm(".text");
0301 asm(".globl __catchException3");
0302 asm("__catchException3:");
0303 SAVE_REGISTERS1();
0304 SAVE_REGISTERS2();
0305 asm ("pushl $3");
0306 CALL_HOOK();
0307 
0308 /* Same thing for exception 1.  */
0309 extern void __catchException1();
0310 asm(".text");
0311 asm(".globl __catchException1");
0312 asm("__catchException1:");
0313 SAVE_REGISTERS1();
0314 SAVE_REGISTERS2();
0315 asm ("pushl $1");
0316 CALL_HOOK();
0317 
0318 /* Same thing for exception 0.  */
0319 extern void __catchException0();
0320 asm(".text");
0321 asm(".globl __catchException0");
0322 asm("__catchException0:");
0323 SAVE_REGISTERS1();
0324 SAVE_REGISTERS2();
0325 asm ("pushl $0");
0326 CALL_HOOK();
0327 
0328 /* Same thing for exception 4.  */
0329 extern void __catchException4();
0330 asm(".text");
0331 asm(".globl __catchException4");
0332 asm("__catchException4:");
0333 SAVE_REGISTERS1();
0334 SAVE_REGISTERS2();
0335 asm ("pushl $4");
0336 CALL_HOOK();
0337 
0338 /* Same thing for exception 5.  */
0339 extern void __catchException5();
0340 asm(".text");
0341 asm(".globl __catchException5");
0342 asm("__catchException5:");
0343 SAVE_REGISTERS1();
0344 SAVE_REGISTERS2();
0345 asm ("pushl $5");
0346 CALL_HOOK();
0347 
0348 /* Same thing for exception 6.  */
0349 extern void __catchException6();
0350 asm(".text");
0351 asm(".globl __catchException6");
0352 asm("__catchException6:");
0353 SAVE_REGISTERS1();
0354 SAVE_REGISTERS2();
0355 asm ("pushl $6");
0356 CALL_HOOK();
0357 
0358 /* Same thing for exception 7.  */
0359 extern void __catchException7();
0360 asm(".text");
0361 asm(".globl __catchException7");
0362 asm("__catchException7:");
0363 SAVE_REGISTERS1();
0364 SAVE_REGISTERS2();
0365 asm ("pushl $7");
0366 CALL_HOOK();
0367 
0368 /* Same thing for exception 8.  */
0369 extern void __catchException8();
0370 asm(".text");
0371 asm(".globl __catchException8");
0372 asm("__catchException8:");
0373 SAVE_REGISTERS1();
0374 SAVE_ERRCODE();
0375 SAVE_REGISTERS2();
0376 asm ("pushl $8");
0377 CALL_HOOK();
0378 
0379 /* Same thing for exception 9.  */
0380 extern void __catchException9();
0381 asm(".text");
0382 asm(".globl __catchException9");
0383 asm("__catchException9:");
0384 SAVE_REGISTERS1();
0385 SAVE_REGISTERS2();
0386 asm ("pushl $9");
0387 CALL_HOOK();
0388 
0389 /* Same thing for exception 10.  */
0390 extern void __catchException10();
0391 asm(".text");
0392 asm(".globl __catchException10");
0393 asm("__catchException10:");
0394 SAVE_REGISTERS1();
0395 SAVE_ERRCODE();
0396 SAVE_REGISTERS2();
0397 asm ("pushl $10");
0398 CALL_HOOK();
0399 
0400 /* Same thing for exception 12.  */
0401 extern void __catchException12();
0402 asm(".text");
0403 asm(".globl __catchException12");
0404 asm("__catchException12:");
0405 SAVE_REGISTERS1();
0406 SAVE_ERRCODE();
0407 SAVE_REGISTERS2();
0408 asm ("pushl $12");
0409 CALL_HOOK();
0410 
0411 /* Same thing for exception 16.  */
0412 extern void __catchException16();
0413 asm(".text");
0414 asm(".globl __catchException16");
0415 asm("__catchException16:");
0416 SAVE_REGISTERS1();
0417 SAVE_REGISTERS2();
0418 asm ("pushl $16");
0419 CALL_HOOK();
0420 
0421 /* For 13, 11, and 14 we have to deal with the CHECK_FAULT stuff.  */
0422 
0423 /* Same thing for exception 13.  */
0424 extern void __catchException13 ();
0425 asm (".text");
0426 asm (".globl __catchException13");
0427 asm ("__catchException13:");
0428 CHECK_FAULT();
0429 SAVE_REGISTERS1();
0430 SAVE_ERRCODE();
0431 SAVE_REGISTERS2();
0432 asm ("pushl $13");
0433 CALL_HOOK();
0434 
0435 /* Same thing for exception 11.  */
0436 extern void __catchException11 ();
0437 asm (".text");
0438 asm (".globl __catchException11");
0439 asm ("__catchException11:");
0440 CHECK_FAULT();
0441 SAVE_REGISTERS1();
0442 SAVE_ERRCODE();
0443 SAVE_REGISTERS2();
0444 asm ("pushl $11");
0445 CALL_HOOK();
0446 
0447 /* Same thing for exception 14.  */
0448 extern void __catchException14 ();
0449 asm (".text");
0450 asm (".globl __catchException14");
0451 asm ("__catchException14:");
0452 CHECK_FAULT();
0453 SAVE_REGISTERS1();
0454 SAVE_ERRCODE();
0455 SAVE_REGISTERS2();
0456 asm ("pushl $14");
0457 CALL_HOOK();
0458 
0459 /*
0460  * remcomHandler is a front end for handle_exception.  It moves the
0461  * stack pointer into an area reserved for debugger use.
0462  */
0463 asm("_remcomHandler:");
0464 asm("           popl %eax");        /* pop off return address     */
0465 asm("           popl %eax");      /* get the exception number   */
0466 asm("       movl stackPtr, %esp"); /* move to remcom stack area  */
0467 asm("       pushl %eax");   /* push exception onto stack  */
0468 asm("       call  handle_exception");    /* this never returns */
0469 
0470 void
0471 _returnFromException ()
0472 {
0473   return_to_prog ();
0474 }
0475 
0476 int
0477 hex (ch)
0478      char ch;
0479 {
0480   if ((ch >= 'a') && (ch <= 'f'))
0481     return (ch - 'a' + 10);
0482   if ((ch >= '0') && (ch <= '9'))
0483     return (ch - '0');
0484   if ((ch >= 'A') && (ch <= 'F'))
0485     return (ch - 'A' + 10);
0486   return (-1);
0487 }
0488 
0489 static char remcomInBuffer[BUFMAX];
0490 static char remcomOutBuffer[BUFMAX];
0491 
0492 /* scan for the sequence $<data>#<checksum>     */
0493 
0494 char *
0495 getpacket (void)
0496 {
0497   char *buffer = &remcomInBuffer[0];
0498   unsigned char checksum;
0499   unsigned char xmitcsum;
0500   int count;
0501   char ch;
0502 
0503   while (1)
0504     {
0505       /* wait around for the start character, ignore all other characters */
0506       while ((ch = getDebugChar ()) != '$')
0507     ;
0508 
0509     retry:
0510       checksum = 0;
0511       xmitcsum = -1;
0512       count = 0;
0513 
0514       /* now, read until a # or end of buffer is found */
0515       while (count < BUFMAX - 1)
0516     {
0517       ch = getDebugChar ();
0518       if (ch == '$')
0519         goto retry;
0520       if (ch == '#')
0521         break;
0522       checksum = checksum + ch;
0523       buffer[count] = ch;
0524       count = count + 1;
0525     }
0526       buffer[count] = 0;
0527 
0528       if (ch == '#')
0529     {
0530       ch = getDebugChar ();
0531       xmitcsum = hex (ch) << 4;
0532       ch = getDebugChar ();
0533       xmitcsum += hex (ch);
0534 
0535       if (checksum != xmitcsum)
0536         {
0537           if (remote_debug)
0538         {
0539 #if 0
0540           fprintf (stderr, "bad checksum.  My count = 0x%x, sent=0x%x. buf=%s\n",
0541                checksum, xmitcsum, buffer);
0542 #endif
0543         }
0544           putDebugChar ('-');   /* failed checksum */
0545         }
0546       else
0547         {
0548           putDebugChar ('+');   /* successful transfer */
0549 
0550           /* if a sequence char is present, reply the sequence ID */
0551           if (buffer[2] == ':')
0552         {
0553           putDebugChar (buffer[0]);
0554           putDebugChar (buffer[1]);
0555 
0556           return &buffer[3];
0557         }
0558 
0559           return &buffer[0];
0560         }
0561     }
0562     }
0563 }
0564 
0565 /* send the packet in buffer.  */
0566 
0567 void
0568 putpacket (char *buffer)
0569 {
0570   unsigned char checksum;
0571   int count;
0572   char ch;
0573 
0574   /*  $<packet info>#<checksum>. */
0575   do
0576     {
0577       putDebugChar ('$');
0578       checksum = 0;
0579       count = 0;
0580 
0581       while ((ch = buffer[count]))
0582     {
0583       putDebugChar (ch);
0584       checksum += ch;
0585       count += 1;
0586     }
0587 
0588       putDebugChar ('#');
0589       putDebugChar (hexchars[checksum >> 4]);
0590       putDebugChar (hexchars[checksum % 16]);
0591 
0592     }
0593   while (getDebugChar () != '+');
0594 }
0595 
0596 void
0597 debug_error (format, parm)
0598      char *format;
0599      char *parm;
0600 {
0601 #if 0
0602   if (remote_debug)
0603     fprintf (stderr, format, parm);
0604 #endif
0605 }
0606 
0607 /* Address of a routine to RTE to if we get a memory fault.  */
0608 static void (*volatile _mem_fault_routine) () = NULL;
0609 
0610 /* Indicate to caller of mem2hex or hex2mem that there has been an
0611    error.  */
0612 static volatile int mem_err = 0;
0613 
0614 void
0615 set_mem_err (void)
0616 {
0617   mem_err = 1;
0618 }
0619 
0620 /* These are separate functions so that they are so short and sweet
0621    that the compiler won't save any registers (if there is a fault
0622    to mem_fault, they won't get restored, so there better not be any
0623    saved).  */
0624 int
0625 get_char (char *addr)
0626 {
0627   return *addr;
0628 }
0629 
0630 void
0631 set_char (char *addr, int val)
0632 {
0633   *addr = val;
0634 }
0635 
0636 /* convert the memory pointed to by mem into hex, placing result in buf */
0637 /* return a pointer to the last char put in buf (null) */
0638 /* If MAY_FAULT is non-zero, then we should set mem_err in response to
0639    a fault; if zero treat a fault like any other fault in the stub.  */
0640 char *
0641 mem2hex (mem, buf, count, may_fault)
0642      char *mem;
0643      char *buf;
0644      int count;
0645      int may_fault;
0646 {
0647   int i;
0648   unsigned char ch;
0649   extern char *_start;
0650 
0651   if (mem == (char *)&_start) {
0652     /* Special-case this address because GDB absolutely insists on
0653      * inserting a breakpoint at _start.  For us though it becomes
0654      * inaccessible after boot. */
0655     *buf++ = 'C';
0656     *buf++ = 'C';
0657     *buf = 0;
0658     return buf;
0659   }
0660 
0661   if (may_fault)
0662     _mem_fault_routine = set_mem_err;
0663   for (i = 0; i < count; i++)
0664     {
0665       ch = get_char (mem++);
0666       if (may_fault && mem_err)
0667     return (buf);
0668       *buf++ = hexchars[ch >> 4];
0669       *buf++ = hexchars[ch % 16];
0670     }
0671   *buf = 0;
0672   if (may_fault)
0673     _mem_fault_routine = NULL;
0674   return (buf);
0675 }
0676 
0677 /* convert the hex array pointed to by buf into binary to be placed in mem */
0678 /* return a pointer to the character AFTER the last byte written */
0679 char *
0680 hex2mem (buf, mem, count, may_fault)
0681      char *buf;
0682      char *mem;
0683      int count;
0684      int may_fault;
0685 {
0686   int i;
0687   unsigned char ch;
0688   extern char *_start;
0689 
0690   if (mem == (char *)&_start) return mem + 1;
0691 
0692   if (may_fault)
0693     _mem_fault_routine = set_mem_err;
0694   for (i = 0; i < count; i++)
0695     {
0696       ch = hex (*buf++) << 4;
0697       ch = ch + hex (*buf++);
0698       set_char (mem++, ch);
0699       if (may_fault && mem_err)
0700     return (mem);
0701     }
0702   if (may_fault)
0703     _mem_fault_routine = NULL;
0704   return (mem);
0705 }
0706 
0707 /* this function takes the 386 exception vector and attempts to
0708    translate this number into a unix compatible signal value */
0709 int
0710 computeSignal (int exceptionVector)
0711 {
0712   int sigval;
0713   switch (exceptionVector)
0714     {
0715     case 0:
0716       sigval = 8;
0717       break;            /* divide by zero */
0718     case 1:
0719       sigval = 5;
0720       break;            /* debug exception */
0721     case 3:
0722       sigval = 5;
0723       break;            /* breakpoint */
0724     case 4:
0725       sigval = 16;
0726       break;            /* into instruction (overflow) */
0727     case 5:
0728       sigval = 16;
0729       break;            /* bound instruction */
0730     case 6:
0731       sigval = 4;
0732       break;            /* Invalid opcode */
0733     case 7:
0734       sigval = 8;
0735       break;            /* coprocessor not available */
0736     case 8:
0737       sigval = 7;
0738       break;            /* double fault */
0739     case 9:
0740       sigval = 11;
0741       break;            /* coprocessor segment overrun */
0742     case 10:
0743       sigval = 11;
0744       break;            /* Invalid TSS */
0745     case 11:
0746       sigval = 11;
0747       break;            /* Segment not present */
0748     case 12:
0749       sigval = 11;
0750       break;            /* stack exception */
0751     case 13:
0752       sigval = 11;
0753       break;            /* general protection */
0754     case 14:
0755       sigval = 11;
0756       break;            /* page fault */
0757     case 16:
0758       sigval = 7;
0759       break;            /* coprocessor error */
0760     default:
0761       sigval = 7;       /* "software generated" */
0762     }
0763   return (sigval);
0764 }
0765 
0766 /**********************************************/
0767 /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
0768 /* RETURN NUMBER OF CHARS PROCESSED           */
0769 /**********************************************/
0770 int
0771 hexToInt (char **ptr, int *intValue)
0772 {
0773   int numChars = 0;
0774   int hexValue;
0775 
0776   *intValue = 0;
0777 
0778   while (**ptr)
0779     {
0780       hexValue = hex (**ptr);
0781       if (hexValue >= 0)
0782     {
0783       *intValue = (*intValue << 4) | hexValue;
0784       numChars++;
0785     }
0786       else
0787     break;
0788 
0789       (*ptr)++;
0790     }
0791 
0792   return (numChars);
0793 }
0794 
0795 /*
0796  * This function does all command procesing for interfacing to gdb.
0797  */
0798 void
0799 handle_exception (int exceptionVector)
0800 {
0801   int sigval, stepping;
0802   int addr, length;
0803   char *ptr;
0804   int newPC;
0805 
0806   gdb_i386vector = exceptionVector;
0807 
0808   if (remote_debug)
0809     {
0810       printf ("vector=%d, sr=0x%x, pc=0x%x\n",
0811           exceptionVector, _registers[PS], _registers[PC]);
0812     }
0813 
0814   /* reply to host that an exception has occurred */
0815   sigval = computeSignal (exceptionVector);
0816 
0817   ptr = remcomOutBuffer;
0818 
0819   *ptr++ = 'T';         /* notify gdb with signo, PC, FP and SP */
0820   *ptr++ = hexchars[sigval >> 4];
0821   *ptr++ = hexchars[sigval & 0xf];
0822 
0823   *ptr++ = hexchars[ESP]; 
0824   *ptr++ = ':';
0825   ptr = mem2hex((char *)&_registers[ESP], ptr, 4, 0);   /* SP */
0826   *ptr++ = ';';
0827 
0828   *ptr++ = hexchars[EBP]; 
0829   *ptr++ = ':';
0830   ptr = mem2hex((char *)&_registers[EBP], ptr, 4, 0);   /* FP */
0831   *ptr++ = ';';
0832 
0833   *ptr++ = hexchars[PC]; 
0834   *ptr++ = ':';
0835   ptr = mem2hex((char *)&_registers[PC], ptr, 4, 0);    /* PC */
0836   *ptr++ = ';';
0837 
0838   *ptr = '\0';
0839 
0840   putpacket (remcomOutBuffer);
0841 
0842   stepping = 0;
0843 
0844   while (1 == 1)
0845     {
0846       remcomOutBuffer[0] = 0;
0847       ptr = getpacket ();
0848 
0849       switch (*ptr++)
0850     {
0851     case '?':
0852       remcomOutBuffer[0] = 'S';
0853       remcomOutBuffer[1] = hexchars[sigval >> 4];
0854       remcomOutBuffer[2] = hexchars[sigval % 16];
0855       remcomOutBuffer[3] = 0;
0856       break;
0857     case 'd':
0858       remote_debug = !(remote_debug);   /* toggle debug flag */
0859       break;
0860     case 'g':       /* return the value of the CPU registers */
0861       mem2hex ((char *) _registers, remcomOutBuffer, NUMREGBYTES, 0);
0862       break;
0863     case 'G':       /* set the value of the CPU registers - return OK */
0864       hex2mem (ptr, (char *) _registers, NUMREGBYTES, 0);
0865       strcpy (remcomOutBuffer, "OK");
0866       break;
0867     case 'P':       /* set the value of a single CPU register - return OK */
0868       {
0869         int regno;
0870 
0871         if (hexToInt (&ptr, &regno) && *ptr++ == '=') {
0872           if (regno >= 0 && regno < NUMREGS) {
0873                 hex2mem (ptr, (char *) &_registers[regno], 4, 0);
0874                 strcpy (remcomOutBuffer, "OK");
0875                 break;
0876               } else if (regno == 0x29) {
0877                 /* quick "orig_eax" hack for GDB */
0878                 strcpy (remcomOutBuffer, "OK");
0879                 break;
0880               }
0881             }
0882         strcpy (remcomOutBuffer, "E01");
0883         break;
0884       }
0885 
0886       /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
0887     case 'm':
0888       /* TRY TO READ %x,%x.  IF SUCCEED, SET PTR = 0 */
0889       if (hexToInt (&ptr, &addr))
0890         if (*(ptr++) == ',')
0891           if (hexToInt (&ptr, &length))
0892         {
0893           ptr = 0;
0894           mem_err = 0;
0895           mem2hex ((char *) addr, remcomOutBuffer, length, 1);
0896           if (mem_err)
0897             {
0898               strcpy (remcomOutBuffer, "E03");
0899               debug_error ("memory fault");
0900             }
0901         }
0902 
0903       if (ptr)
0904         {
0905           strcpy (remcomOutBuffer, "E01");
0906         }
0907       break;
0908 
0909       /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
0910     case 'M':
0911       /* TRY TO READ '%x,%x:'.  IF SUCCEED, SET PTR = 0 */
0912       if (hexToInt (&ptr, &addr))
0913         if (*(ptr++) == ',')
0914           if (hexToInt (&ptr, &length))
0915         if (*(ptr++) == ':')
0916           {
0917             mem_err = 0;
0918             hex2mem (ptr, (char *) addr, length, 1);
0919 
0920             if (mem_err)
0921               {
0922             strcpy (remcomOutBuffer, "E03");
0923             debug_error ("memory fault");
0924               }
0925             else
0926               {
0927             strcpy (remcomOutBuffer, "OK");
0928               }
0929 
0930             ptr = 0;
0931           }
0932       if (ptr)
0933         {
0934           strcpy (remcomOutBuffer, "E02");
0935         }
0936       break;
0937 
0938       /* cAA..AA    Continue at address AA..AA(optional) */
0939       /* sAA..AA   Step one instruction from AA..AA(optional) */
0940     case 's':
0941       stepping = 1;
0942     case 'c':
0943       /* try to read optional parameter, pc unchanged if no parm */
0944       if (hexToInt (&ptr, &addr))
0945         _registers[PC] = addr;
0946 
0947       newPC = _registers[PC];
0948 
0949       /* clear the trace bit */
0950       _registers[PS] &= 0xfffffeff;
0951 
0952       /* set the trace bit if we're stepping */
0953       if (stepping)
0954         _registers[PS] |= 0x100;
0955 
0956       _returnFromException ();  /* this is a jump */
0957       break;
0958 
0959       /* kill the program */
0960     case 'k':       /* do nothing */
0961 #if 0
0962       /* Huh? This doesn't look like "nothing".
0963          m68k-stub.c and sparc-stub.c don't have it.  */
0964       BREAKPOINT ();
0965 #endif
0966       break;
0967     }           /* switch */
0968 
0969       /* reply to the request */
0970       putpacket (remcomOutBuffer);
0971     }
0972 }
0973 
0974 /* this function is used to set up exception handlers for tracing and
0975    breakpoints */
0976 void
0977 set_debug_traps (void)
0978 {
0979   char *ptr;
0980   stackPtr = &remcomStack[STACKSIZE / sizeof (int) - 1];
0981 
0982   exceptionHandler (1,  __catchException1);
0983   exceptionHandler (3,  __catchException3);
0984   exceptionHandler (0,  __catchException0);
0985   exceptionHandler (4,  __catchException4);
0986   exceptionHandler (5,  __catchException5);
0987   exceptionHandler (6,  __catchException6);
0988   exceptionHandler (7,  __catchException7);
0989   exceptionHandler (8,  __catchException8);
0990   exceptionHandler (9,  __catchException9);
0991   exceptionHandler (10, __catchException10);
0992   exceptionHandler (11, __catchException11);
0993   exceptionHandler (12, __catchException12);
0994   exceptionHandler (13, __catchException13);
0995   exceptionHandler (14, __catchException14);
0996   exceptionHandler (16, __catchException16);
0997 
0998   ptr = getpacket ();           /* expect qSupported */
0999   printf ("RX GDB packet: %s\n", ptr);
1000   remcomOutBuffer[0] = 0;
1001   putpacket (remcomOutBuffer);
1002 
1003   initialized = 1;
1004 }
1005 
1006 /* This function will generate a breakpoint exception.  It is used at the
1007    beginning of a program to sync up with a debugger and can be used
1008    otherwise as a quick means to stop program execution and "break" into
1009    the debugger. */
1010 
1011 void
1012 breakpoint (void)
1013 {
1014   if (initialized)
1015     BREAKPOINT ();
1016 }