[0074691a] | 1 | /**************************************************************************** |
---|
| 2 | |
---|
[6128a4a] | 3 | THIS SOFTWARE IS NOT COPYRIGHTED |
---|
| 4 | |
---|
[0074691a] | 5 | HP offers the following for use in the public domain. HP makes no |
---|
[6128a4a] | 6 | warranty with regard to the software or it's performance and the |
---|
[0074691a] | 7 | user accepts the software "AS IS" with all faults. |
---|
| 8 | |
---|
| 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. |
---|
| 12 | |
---|
| 13 | ****************************************************************************/ |
---|
| 14 | |
---|
| 15 | /**************************************************************************** |
---|
[6128a4a] | 16 | * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $ |
---|
[0074691a] | 17 | * |
---|
[6128a4a] | 18 | * Module name: remcom.c $ |
---|
[0074691a] | 19 | * Revision: 1.34 $ |
---|
| 20 | * Date: 91/03/09 12:29:49 $ |
---|
| 21 | * Contributor: Lake Stevens Instrument Division$ |
---|
[6128a4a] | 22 | * |
---|
[0074691a] | 23 | * Description: low level support for gdb debugger. $ |
---|
| 24 | * |
---|
| 25 | * Considerations: only works on target hardware $ |
---|
| 26 | * |
---|
| 27 | * Written by: Glenn Engel $ |
---|
[6128a4a] | 28 | * ModuleState: Experimental $ |
---|
[0074691a] | 29 | * |
---|
| 30 | * NOTES: See Below $ |
---|
[6128a4a] | 31 | * |
---|
[0074691a] | 32 | * To enable debugger support, two things need to happen. One, a |
---|
| 33 | * call to set_debug_traps() is necessary in order to allow any breakpoints |
---|
| 34 | * or error conditions to be properly intercepted and reported to gdb. |
---|
| 35 | * Two, a breakpoint needs to be generated to begin communication. This |
---|
| 36 | * is most easily accomplished by a call to breakpoint(). Breakpoint() |
---|
| 37 | * simulates a breakpoint by executing a trap #1. The breakpoint instruction |
---|
| 38 | * is hardwired to trap #1 because not to do so is a compatibility problem-- |
---|
| 39 | * there either should be a standard breakpoint instruction, or the protocol |
---|
| 40 | * should be extended to provide some means to communicate which breakpoint |
---|
| 41 | * instruction is in use (or have the stub insert the breakpoint). |
---|
[6128a4a] | 42 | * |
---|
[0074691a] | 43 | * Some explanation is probably necessary to explain how exceptions are |
---|
| 44 | * handled. When an exception is encountered the 68000 pushes the current |
---|
| 45 | * program counter and status register onto the supervisor stack and then |
---|
| 46 | * transfers execution to a location specified in it's vector table. |
---|
| 47 | * The handlers for the exception vectors are hardwired to jmp to an address |
---|
[6128a4a] | 48 | * given by the relation: (exception - 256) * 6. These are decending |
---|
[0074691a] | 49 | * addresses starting from -6, -12, -18, ... By allowing 6 bytes for |
---|
[6128a4a] | 50 | * each entry, a jsr, jmp, bsr, ... can be used to enter the exception |
---|
[0074691a] | 51 | * handler. Using a jsr to handle an exception has an added benefit of |
---|
| 52 | * allowing a single handler to service several exceptions and use the |
---|
| 53 | * return address as the key differentiation. The vector number can be |
---|
| 54 | * computed from the return address by [ exception = (addr + 1530) / 6 ]. |
---|
| 55 | * The sole purpose of the routine _catchException is to compute the |
---|
| 56 | * exception number and push it on the stack in place of the return address. |
---|
| 57 | * The external function exceptionHandler() is |
---|
| 58 | * used to attach a specific handler to a specific m68k exception. |
---|
| 59 | * For 68020 machines, the ability to have a return address around just |
---|
| 60 | * so the vector can be determined is not necessary because the '020 pushes an |
---|
| 61 | * extra word onto the stack containing the vector offset |
---|
[6128a4a] | 62 | * |
---|
[0074691a] | 63 | * Because gdb will sometimes write to the stack area to execute function |
---|
| 64 | * calls, this program cannot rely on using the supervisor stack so it |
---|
[6128a4a] | 65 | * uses it's own stack area reserved in the int array remcomStack. |
---|
| 66 | * |
---|
[0074691a] | 67 | ************* |
---|
| 68 | * |
---|
| 69 | * The following gdb commands are supported: |
---|
[6128a4a] | 70 | * |
---|
[0074691a] | 71 | * command function Return value |
---|
[6128a4a] | 72 | * |
---|
[0074691a] | 73 | * g return the value of the CPU registers hex data or ENN |
---|
| 74 | * G set the value of the CPU registers OK or ENN |
---|
[6128a4a] | 75 | * |
---|
[0074691a] | 76 | * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN |
---|
| 77 | * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN |
---|
[6128a4a] | 78 | * |
---|
[0074691a] | 79 | * c Resume at current address SNN ( signal NN) |
---|
| 80 | * cAA..AA Continue at address AA..AA SNN |
---|
[6128a4a] | 81 | * |
---|
[0074691a] | 82 | * s Step one instruction SNN |
---|
| 83 | * sAA..AA Step one instruction from AA..AA SNN |
---|
[6128a4a] | 84 | * |
---|
[0074691a] | 85 | * k kill |
---|
| 86 | * |
---|
| 87 | * ? What was the last sigval ? SNN (signal NN) |
---|
[6128a4a] | 88 | * |
---|
| 89 | * All commands and responses are sent with a packet which includes a |
---|
| 90 | * checksum. A packet consists of |
---|
| 91 | * |
---|
[0074691a] | 92 | * $<packet info>#<checksum>. |
---|
[6128a4a] | 93 | * |
---|
[0074691a] | 94 | * where |
---|
| 95 | * <packet info> :: <characters representing the command or response> |
---|
| 96 | * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>> |
---|
[6128a4a] | 97 | * |
---|
[0074691a] | 98 | * When a packet is received, it is first acknowledged with either '+' or '-'. |
---|
| 99 | * '+' indicates a successful transfer. '-' indicates a failed transfer. |
---|
[6128a4a] | 100 | * |
---|
[0074691a] | 101 | * Example: |
---|
[6128a4a] | 102 | * |
---|
[0074691a] | 103 | * Host: Reply: |
---|
| 104 | * $m0,10#2a +$00010203040506070809101112131415#42 |
---|
[6128a4a] | 105 | * |
---|
[6f9c75c3] | 106 | * $Id$ |
---|
| 107 | * |
---|
[0074691a] | 108 | ****************************************************************************/ |
---|
| 109 | |
---|
| 110 | #include <stdio.h> |
---|
| 111 | #include <string.h> |
---|
| 112 | #include <setjmp.h> |
---|
| 113 | |
---|
| 114 | #include <bsp.h> |
---|
| 115 | #include <debugport.h> |
---|
| 116 | |
---|
| 117 | /************************************************************************ |
---|
| 118 | * |
---|
[6128a4a] | 119 | * external low-level support routines |
---|
[0074691a] | 120 | */ |
---|
| 121 | typedef void (*ExceptionHook)(int); /* pointer to function with int parm */ |
---|
| 122 | typedef void (*Function)(void); /* pointer to a function */ |
---|
| 123 | |
---|
| 124 | /* assign an exception handler */ |
---|
| 125 | Function exceptionHandler(int vector, Function handler); |
---|
| 126 | extern ExceptionHook exceptionHook; /* hook variable for errors/exceptions */ |
---|
| 127 | |
---|
| 128 | int putDebugChar(char ch); |
---|
| 129 | char getDebugChar(void); |
---|
| 130 | |
---|
| 131 | /************************/ |
---|
| 132 | /* FORWARD DECLARATIONS */ |
---|
| 133 | /************************/ |
---|
| 134 | static int hex(char ch); |
---|
| 135 | static void getpacket(char *buffer); |
---|
| 136 | static void putpacket(char *buffer); |
---|
| 137 | static char* mem2hex(char *mem, char *buf, int count); |
---|
| 138 | static char* hex2mem(char *buf, char *mem, int count); |
---|
| 139 | static void handle_buserror(void); |
---|
| 140 | static int computeSignal(int exceptionVector); |
---|
| 141 | static int hexToInt(char **ptr, int *intValue); |
---|
| 142 | void handle_exception(int exceptionVector); |
---|
| 143 | static void initializeRemcomErrorFrame(void); |
---|
| 144 | |
---|
| 145 | void set_debug_traps(void); |
---|
| 146 | void breakpoint(void); |
---|
| 147 | |
---|
| 148 | /************************************************************************/ |
---|
| 149 | /* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/ |
---|
| 150 | /* at least NUMREGBYTES*2 are needed for register packets */ |
---|
| 151 | #define BUFMAX 400 |
---|
| 152 | |
---|
| 153 | static char initialized; /* boolean flag. != 0 means we've been initialized */ |
---|
| 154 | |
---|
| 155 | int remote_debug; |
---|
[6128a4a] | 156 | /* debug > 0 prints ill-formed commands in valid packets & checksum errors */ |
---|
[0074691a] | 157 | |
---|
| 158 | static const char hexchars[]="0123456789abcdef"; |
---|
| 159 | |
---|
| 160 | /* there are 180 bytes of registers on a 68020 w/68881 */ |
---|
| 161 | /* many of the fpa registers are 12 byte (96 bit) registers */ |
---|
| 162 | #define NUMREGBYTES 180 |
---|
[6128a4a] | 163 | enum regnames {D0,D1,D2,D3,D4,D5,D6,D7, |
---|
| 164 | A0,A1,A2,A3,A4,A5,A6,A7, |
---|
[0074691a] | 165 | PS,PC, |
---|
| 166 | FP0,FP1,FP2,FP3,FP4,FP5,FP6,FP7, |
---|
| 167 | FPCONTROL,FPSTATUS,FPIADDR |
---|
| 168 | }; |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | /* We keep a whole frame cache here. "Why?", I hear you cry, "doesn't |
---|
| 172 | GDB handle that sort of thing?" Well, yes, I believe the only |
---|
| 173 | reason for this cache is to save and restore floating point state |
---|
| 174 | (fsave/frestore). A cleaner way to do this would be to make the |
---|
| 175 | fsave data part of the registers which GDB deals with like any |
---|
| 176 | other registers. This should not be a performance problem if the |
---|
| 177 | ability to read individual registers is added to the protocol. */ |
---|
| 178 | |
---|
| 179 | typedef struct FrameStruct |
---|
| 180 | { |
---|
| 181 | struct FrameStruct *previous; |
---|
| 182 | int exceptionPC; /* pc value when this frame created */ |
---|
| 183 | int exceptionVector; /* cpu vector causing exception */ |
---|
| 184 | short frameSize; /* size of cpu frame in words */ |
---|
| 185 | short sr; /* for 68000, this not always sr */ |
---|
| 186 | int pc; |
---|
| 187 | short format; |
---|
| 188 | int fsaveHeader; |
---|
| 189 | int morejunk[0]; /* exception frame, fp save... */ |
---|
| 190 | } Frame; |
---|
| 191 | |
---|
| 192 | #define FRAMESIZE 500 |
---|
| 193 | int gdbFrameStack[FRAMESIZE]; |
---|
| 194 | static Frame *lastFrame; |
---|
| 195 | |
---|
| 196 | /* |
---|
| 197 | * these should not be static cuz they can be used outside this module |
---|
| 198 | */ |
---|
| 199 | int registers[NUMREGBYTES/4]; |
---|
| 200 | int superStack; |
---|
| 201 | |
---|
| 202 | #define STACKSIZE 10000 |
---|
| 203 | int remcomStack[STACKSIZE/sizeof(int)]; |
---|
| 204 | static int* stackPtr = &remcomStack[STACKSIZE/sizeof(int) - 1]; |
---|
| 205 | |
---|
| 206 | /* |
---|
| 207 | * In many cases, the system will want to continue exception processing |
---|
[6128a4a] | 208 | * when a continue command is given. |
---|
[0074691a] | 209 | * oldExceptionHook is a function to invoke in this case. |
---|
| 210 | */ |
---|
| 211 | |
---|
| 212 | static ExceptionHook oldExceptionHook; |
---|
| 213 | |
---|
[46ce08d] | 214 | #if (defined(__mc68020__) && !defined(__mcpu32__)) |
---|
[0074691a] | 215 | /* the size of the exception stack on the 68020 varies with the type of |
---|
| 216 | * exception. The following table is the number of WORDS used |
---|
| 217 | * for each exception format. |
---|
| 218 | */ |
---|
| 219 | const short exceptionSize[] = { 4,4,6,4,4,4,4,4,29,10,16,46,12,4,4,4 }; |
---|
| 220 | #endif |
---|
| 221 | |
---|
| 222 | #if defined(__mc68332__) |
---|
| 223 | static const short exceptionSize[] = { 4,4,6,4,4,4,4,4,4,4,4,4,16,4,4,4 }; |
---|
| 224 | #endif |
---|
| 225 | |
---|
| 226 | /************* jump buffer used for setjmp/longjmp **************************/ |
---|
| 227 | jmp_buf remcomEnv; |
---|
| 228 | |
---|
| 229 | /*************************** ASSEMBLY CODE MACROS *************************/ |
---|
| 230 | /* */ |
---|
| 231 | |
---|
| 232 | #if defined(__HAVE_68881__) |
---|
| 233 | |
---|
| 234 | /* do an fsave, then remember the address to begin a restore from */ |
---|
| 235 | #define SAVE_FP_REGS() \ |
---|
| 236 | asm(" fsave %a0@-"); \ |
---|
| 237 | asm(" fmovemx %fp0-%fp7,registers+72"); \ |
---|
| 238 | asm(" fmoveml %fpcr/%fpsr/%fpi,registers+168"); |
---|
| 239 | |
---|
| 240 | #define RESTORE_FP_REGS() \ |
---|
| 241 | asm(" \n\ |
---|
| 242 | fmoveml registers+168,%fpcr/%fpsr/%fpi \n\ |
---|
| 243 | fmovemx registers+72,%fp0-%fp7 \n\ |
---|
| 244 | cmpl #-1,%a0@ | skip frestore flag set ? \n\ |
---|
| 245 | beq skip_frestore \n\ |
---|
| 246 | frestore %a0@+ \n\ |
---|
| 247 | skip_frestore: \n\ |
---|
| 248 | "); |
---|
| 249 | |
---|
| 250 | #else |
---|
| 251 | |
---|
| 252 | #define SAVE_FP_REGS() |
---|
| 253 | #define RESTORE_FP_REGS() |
---|
| 254 | |
---|
| 255 | #endif /* __HAVE_68881__ */ |
---|
| 256 | |
---|
| 257 | void return_to_super(void); |
---|
| 258 | void return_to_user(void); |
---|
| 259 | |
---|
[f799d21c] | 260 | asm("\n\ |
---|
| 261 | .text\n\ |
---|
| 262 | \n\ |
---|
| 263 | .globl return_to_super\n\ |
---|
| 264 | .align 4\n\ |
---|
| 265 | return_to_super:\n\ |
---|
| 266 | movel registers+60,%sp /* get new stack pointer */ \n\ |
---|
| 267 | movel lastFrame,%a0 /* get last frame info */ \n\ |
---|
| 268 | bra return_to_any\n\ |
---|
| 269 | \n\ |
---|
| 270 | .globl return_to_user\n\ |
---|
| 271 | .align 4\n\ |
---|
| 272 | \n\ |
---|
| 273 | return_to_user:\n\ |
---|
| 274 | movel registers+60,%a0 /* get usp */ \n\ |
---|
| 275 | movel %a0,%usp /* set usp */ \n\ |
---|
| 276 | movel superStack,%sp /* get original stack pointer */ \n\ |
---|
| 277 | \n\ |
---|
| 278 | return_to_any:\n\ |
---|
| 279 | movel lastFrame,%a0 /* get last frame info */ \n\ |
---|
| 280 | movel %a0@+,lastFrame /* link in previous frame */ \n\ |
---|
| 281 | addql #8,%a0 /* skip over pc, vector#*/ \n\ |
---|
| 282 | movew %a0@+,%d0 /* get # of words in cpu frame */ \n\ |
---|
| 283 | addw %d0,%a0 /* point to end of data */ \n\ |
---|
| 284 | addw %d0,%a0 /* point to end of data */ \n\ |
---|
| 285 | movel %a0,%a1 \n\ |
---|
| 286 | # \n\ |
---|
| 287 | # copy the stack frame \n\ |
---|
| 288 | subql #1,%d0\n\ |
---|
| 289 | \n\ |
---|
| 290 | copyUserLoop: \n\ |
---|
| 291 | movew %a1@-,%sp@- \n\ |
---|
| 292 | dbf %d0,copyUserLoop \n\ |
---|
[80277b8] | 293 | "); |
---|
| 294 | RESTORE_FP_REGS() |
---|
[f799d21c] | 295 | asm("\n\ |
---|
| 296 | moveml registers,%d0-%d7/%a0-%a6\n\ |
---|
| 297 | rte /* pop and go! */\n\ |
---|
[0074691a] | 298 | "); |
---|
| 299 | |
---|
| 300 | #define DISABLE_INTERRUPTS() asm(" oriw #0x0700,%sr"); |
---|
| 301 | #define BREAKPOINT() asm(" trap #1"); |
---|
| 302 | |
---|
| 303 | /* this function is called immediately when a level 7 interrupt occurs */ |
---|
| 304 | /* if the previous interrupt level was 7 then we're already servicing */ |
---|
| 305 | /* this interrupt and an rte is in order to return to the debugger. */ |
---|
| 306 | /* For the 68000, the offset for sr is 6 due to the jsr return address */ |
---|
[f799d21c] | 307 | asm("\n\ |
---|
| 308 | .text\n\ |
---|
| 309 | .globl _debug_level7\n\ |
---|
| 310 | .align 4\n\ |
---|
| 311 | \n\ |
---|
| 312 | _debug_level7:\n\ |
---|
| 313 | movew %d0,%sp@-\n\ |
---|
[0074691a] | 314 | "); |
---|
| 315 | |
---|
[46ce08d] | 316 | #if (defined(__mc68020__) && !defined(__mcpu32__)) || defined(__mc68332__) |
---|
[f799d21c] | 317 | asm("\n\ |
---|
| 318 | movew %sp@(2),%d0\n\ |
---|
[0074691a] | 319 | "); |
---|
| 320 | #else |
---|
[f799d21c] | 321 | asm("\n\ |
---|
| 322 | movew %sp@(6),%d0\n\ |
---|
[0074691a] | 323 | "); |
---|
| 324 | #endif |
---|
[f799d21c] | 325 | asm("\n\ |
---|
| 326 | andiw #0x700,%d0\n\ |
---|
| 327 | cmpiw #0x700,%d0\n\ |
---|
| 328 | beq _already7\n\ |
---|
| 329 | movew %sp@+,%d0 \n\ |
---|
| 330 | bra _catchException\n\ |
---|
| 331 | _already7:\n\ |
---|
| 332 | movew %sp@+,%d0\n\ |
---|
[0074691a] | 333 | "); |
---|
[46ce08d] | 334 | #if defined (__mc68000__) && !(defined(__mc68020__) && !defined(__mcpu32__)) |
---|
[f799d21c] | 335 | asm("\n\ |
---|
[80277b8] | 336 | lea %sp@(4),%sp"); /* pull off 68000 return address */ |
---|
[0074691a] | 337 | #endif |
---|
[f799d21c] | 338 | asm("\n\ |
---|
| 339 | rte\n\ |
---|
[0074691a] | 340 | "); |
---|
| 341 | |
---|
| 342 | extern void _catchException(void); |
---|
| 343 | |
---|
[46ce08d] | 344 | #if (defined(__mc68020__) && !defined(__mcpu32__)) || defined(__mc68332__) |
---|
[0074691a] | 345 | /* This function is called when a 68020 exception occurs. It saves |
---|
| 346 | * all the cpu and fpcp regs in the _registers array, creates a frame on a |
---|
| 347 | * linked list of frames which has the cpu and fpcp stack frames needed |
---|
| 348 | * to properly restore the context of these processors, and invokes |
---|
| 349 | * an exception handler (remcom_handler). |
---|
| 350 | * |
---|
| 351 | * stack on entry: stack on exit: |
---|
| 352 | * N bytes of junk exception # MSWord |
---|
| 353 | * Exception Format Word exception # MSWord |
---|
[6128a4a] | 354 | * Program counter LSWord |
---|
| 355 | * Program counter MSWord |
---|
| 356 | * Status Register |
---|
| 357 | * |
---|
| 358 | * |
---|
[0074691a] | 359 | */ |
---|
[f799d21c] | 360 | asm(" \n\ |
---|
| 361 | .text\n\ |
---|
| 362 | \n\ |
---|
| 363 | .globl _catchException\n\ |
---|
| 364 | .align 4\n\ |
---|
| 365 | _catchException:\n\ |
---|
[0074691a] | 366 | "); |
---|
| 367 | |
---|
| 368 | DISABLE_INTERRUPTS(); |
---|
| 369 | |
---|
[f799d21c] | 370 | asm("\n\ |
---|
| 371 | moveml %d0-%d7/%a0-%a6,registers /* save registers */\n\ |
---|
| 372 | movel lastFrame,%a0 /* last frame pointer */\n\ |
---|
[0074691a] | 373 | "); |
---|
[6128a4a] | 374 | SAVE_FP_REGS(); |
---|
[f799d21c] | 375 | asm("\n\ |
---|
| 376 | lea registers,%a5 /* get address of registers */\n\ |
---|
| 377 | movew %sp@,%d1 /* get status register */\n\ |
---|
| 378 | movew %d1,%a5@(66) /* save sr */ \n\ |
---|
| 379 | movel %sp@(2),%a4 /* save pc in %a4 for later use */\n\ |
---|
| 380 | movel %a4,%a5@(68) /* save pc in _regisers[] */\n\ |
---|
| 381 | \n\ |
---|
| 382 | #\n\ |
---|
| 383 | # figure out how many bytes in the stack frame\n\ |
---|
| 384 | #\n\ |
---|
| 385 | movew %sp@(6),%d0 /* get '020 exception format */\n\ |
---|
| 386 | movew %d0,%d2 /* make a copy of format word */\n\ |
---|
| 387 | andiw #0xf000,%d0 /* mask off format type */\n\ |
---|
| 388 | rolw #5,%d0 /* rotate into the low byte *2 */\n\ |
---|
| 389 | lea exceptionSize,%a1 \n\ |
---|
| 390 | addw %d0,%a1 /* index into the table */\n\ |
---|
| 391 | movew %a1@,%d0 /* get number of words in frame */\n\ |
---|
| 392 | movew %d0,%d3 /* save it */\n\ |
---|
| 393 | subw %d0,%a0 /* adjust save pointer */\n\ |
---|
| 394 | subw %d0,%a0 /* adjust save pointer(bytes) */\n\ |
---|
| 395 | movel %a0,%a1 /* copy save pointer */\n\ |
---|
| 396 | subql #1,%d0 /* predecrement loop counter */\n\ |
---|
| 397 | #\n\ |
---|
| 398 | # copy the frame\n\ |
---|
| 399 | #\n\ |
---|
| 400 | saveFrameLoop:\n\ |
---|
| 401 | movew %sp@+,%a1@+\n\ |
---|
| 402 | dbf %d0,saveFrameLoop\n\ |
---|
| 403 | #\n\ |
---|
| 404 | # now that the stack has been clenaed,\n\ |
---|
| 405 | # save the %a7 in use at time of exception\n\ |
---|
| 406 | \n\ |
---|
| 407 | movel %sp,superStack /* save supervisor %sp */\n\ |
---|
| 408 | andiw #0x2000,%d1 /* were we in supervisor mode ? */\n\ |
---|
| 409 | beq userMode \n\ |
---|
| 410 | movel %a7,%a5@(60) /* save %a7 */\n\ |
---|
| 411 | bra a7saveDone\n\ |
---|
| 412 | userMode: \n\ |
---|
| 413 | movel %usp,%a1 \n\ |
---|
| 414 | movel %a1,%a5@(60) /* save user stack pointer */\n\ |
---|
| 415 | a7saveDone:\n\ |
---|
| 416 | \n\ |
---|
| 417 | #\n\ |
---|
| 418 | # save size of frame\n\ |
---|
| 419 | movew %d3,%a0@-\n\ |
---|
| 420 | \n\ |
---|
| 421 | #\n\ |
---|
| 422 | # compute exception number\n\ |
---|
| 423 | andl #0xfff,%d2 /* mask off vector offset */\n\ |
---|
| 424 | lsrw #2,%d2 /* divide by 4 to get vect num */\n\ |
---|
| 425 | movel %d2,%a0@- /* save it */\n\ |
---|
| 426 | #\n\ |
---|
| 427 | # save pc causing exception\n\ |
---|
| 428 | movel %a4,%a0@-\n\ |
---|
| 429 | #\n\ |
---|
| 430 | # save old frame link and set the new value\n\ |
---|
| 431 | movel lastFrame,%a1 /* last frame pointer */\n\ |
---|
| 432 | movel %a1,%a0@- /* save pointer to prev frame */\n\ |
---|
| 433 | movel %a0,lastFrame\n\ |
---|
| 434 | \n\ |
---|
| 435 | movel %d2,%sp@- /* push exception num */\n\ |
---|
| 436 | movel exceptionHook,%a0 /* get address of handler */\n\ |
---|
| 437 | jbsr %a0@ /* and call it */\n\ |
---|
| 438 | clrl %sp@ /* replace exception num parm with frame ptr */\n\ |
---|
| 439 | jbsr _returnFromException /* jbsr, but never returns */\n\ |
---|
| 440 | \n\ |
---|
[0074691a] | 441 | "); |
---|
| 442 | |
---|
| 443 | #else /* mc68000 */ |
---|
| 444 | |
---|
| 445 | /* This function is called when an exception occurs. It translates the |
---|
| 446 | * return address found on the stack into an exception vector # which |
---|
| 447 | * is then handled by either handle_exception or a system handler. |
---|
[6128a4a] | 448 | * _catchException provides a front end for both. |
---|
[0074691a] | 449 | * |
---|
| 450 | * stack on entry: stack on exit: |
---|
[6128a4a] | 451 | * Program counter MSWord exception # MSWord |
---|
[0074691a] | 452 | * Program counter LSWord exception # MSWord |
---|
[6128a4a] | 453 | * Status Register |
---|
| 454 | * Return Address MSWord |
---|
| 455 | * Return Address LSWord |
---|
[0074691a] | 456 | */ |
---|
[80277b8] | 457 | asm("\n\ |
---|
[f799d21c] | 458 | .text\n\ |
---|
| 459 | .globl _catchException\n\ |
---|
| 460 | .align 4\n\ |
---|
| 461 | _catchException:\n\ |
---|
[0074691a] | 462 | "); |
---|
| 463 | DISABLE_INTERRUPTS(); |
---|
[f799d21c] | 464 | asm("\n\ |
---|
| 465 | moveml %d0-%d7/%a0-%a6,registers /* save registers */\n\ |
---|
| 466 | movel lastFrame,%a0 /* last frame pointer */\n\ |
---|
[0074691a] | 467 | "); |
---|
| 468 | |
---|
[6128a4a] | 469 | SAVE_FP_REGS(); |
---|
[f799d21c] | 470 | asm("\n\ |
---|
| 471 | moveq.l #0,%d2\n\ |
---|
| 472 | movew %sp@+,%d2\n\ |
---|
| 473 | lea registers,%a5 /* get address of registers */\n\ |
---|
| 474 | \n\ |
---|
| 475 | moveql #3,%d3 /* assume a three word frame */\n\ |
---|
| 476 | \n\ |
---|
| 477 | cmpiw #3,%d2 /* bus error or address error ? */\n\ |
---|
| 478 | bgt normal /* if >3 then normal error */\n\ |
---|
| 479 | movel %sp@+,%a0@- /* copy error info to frame buff*/\n\ |
---|
| 480 | movel %sp@+,%a0@- /* these are never used */\n\ |
---|
| 481 | moveql #7,%d3 /* this is a 7 word frame */\n\ |
---|
| 482 | \n\ |
---|
| 483 | normal: \n\ |
---|
| 484 | movew %sp@+,%d1 /* pop status register */\n\ |
---|
| 485 | movel %sp@+,%a4 /* pop program counter */\n\ |
---|
| 486 | \n\ |
---|
| 487 | cmpiw #33,%d2 /* trap #1, breakpoint ? */\n\ |
---|
| 488 | bne not_breakpoint\n\ |
---|
| 489 | \n\ |
---|
| 490 | subql #2,%a4 /* trap leaves the pc after the trap */\n\ |
---|
| 491 | \n\ |
---|
| 492 | not_breakpoint:\n\ |
---|
| 493 | movew %d1,%a5@(66) /* save sr */ \n\ |
---|
| 494 | movel %a4,%a5@(68) /* save pc in _regisers[] */\n\ |
---|
| 495 | movel %a4,%a0@- /* copy pc to frame buffer */\n\ |
---|
| 496 | movew %d1,%a0@- /* copy sr to frame buffer */\n\ |
---|
| 497 | \n\ |
---|
| 498 | movel %sp,superStack /* save supervisor %sp */\n\ |
---|
| 499 | \n\ |
---|
| 500 | andiw #0x2000,%d1 /* were we in supervisor mode ? */\n\ |
---|
| 501 | beq userMode \n\ |
---|
| 502 | movel %a7,%a5@(60) /* save %a7 */\n\ |
---|
| 503 | bra saveDone \n\ |
---|
| 504 | userMode:\n\ |
---|
| 505 | movel %usp,%a1 /* save user stack pointer */\n\ |
---|
| 506 | movel %a1,%a5@(60) /* save user stack pointer */\n\ |
---|
| 507 | saveDone:\n\ |
---|
| 508 | \n\ |
---|
| 509 | movew %d3,%a0@- /* push frame size in words */\n\ |
---|
| 510 | movel %d2,%a0@- /* push vector number */\n\ |
---|
| 511 | movel %a4,%a0@- /* push exception pc */\n\ |
---|
| 512 | \n\ |
---|
| 513 | #\n\ |
---|
| 514 | # save old frame link and set the new value\n\ |
---|
| 515 | #\n\ |
---|
| 516 | movel lastFrame,%a1 /* last frame pointer */\n\ |
---|
| 517 | movel %a1,%a0@- /* save pointer to prev frame */\n\ |
---|
| 518 | movel %a0,lastFrame\n\ |
---|
| 519 | \n\ |
---|
| 520 | movel %d2,%sp@- /* push exception num */\n\ |
---|
| 521 | movel exceptionHook,%a0 /* get address of handler */\n\ |
---|
| 522 | \n\ |
---|
| 523 | jbsr %a0@ /* and call it */\n\ |
---|
| 524 | clrl %sp@ /* replace exception num parm with frame ptr */\n\ |
---|
| 525 | jbsr _returnFromException /* jbsr, but never returns */\n\ |
---|
[0074691a] | 526 | "); |
---|
| 527 | #endif |
---|
| 528 | |
---|
| 529 | /* |
---|
| 530 | * remcomHandler is a front end for handle_exception. It moves the |
---|
| 531 | * stack pointer into an area reserved for debugger use in case the |
---|
| 532 | * breakpoint happened in supervisor mode. |
---|
| 533 | */ |
---|
| 534 | asm("remcomHandler:"); |
---|
| 535 | asm(" addl #4,%sp"); /* pop off return address */ |
---|
| 536 | asm(" movel %sp@+,%d0"); /* get the exception number */ |
---|
| 537 | asm(" movel stackPtr,%sp"); /* move to remcom stack area */ |
---|
| 538 | asm(" movel %d0,%sp@-"); /* push exception onto stack */ |
---|
| 539 | asm(" jbsr handle_exception"); /* this never returns */ |
---|
| 540 | asm(" rts"); /* return */ |
---|
| 541 | |
---|
| 542 | void _returnFromException(Frame *frame) |
---|
| 543 | { |
---|
| 544 | /* if no passed in frame, use the last one */ |
---|
| 545 | if (! frame) |
---|
| 546 | { |
---|
| 547 | frame = lastFrame; |
---|
| 548 | frame->frameSize = 4; |
---|
| 549 | frame->format = 0; |
---|
| 550 | frame->fsaveHeader = -1; /* restore regs, but we dont have fsave info*/ |
---|
| 551 | } |
---|
| 552 | |
---|
[46ce08d] | 553 | #if defined(__mc68000__) && !(defined(__mc68020__) && !defined(__mcpu32__)) |
---|
[0074691a] | 554 | /* a 68000 cannot use the internal info pushed onto a bus error |
---|
| 555 | * or address error frame when doing an RTE so don't put this info |
---|
| 556 | * onto the stack or the stack will creep every time this happens. |
---|
| 557 | */ |
---|
| 558 | frame->frameSize=3; |
---|
| 559 | #endif |
---|
| 560 | |
---|
| 561 | /* throw away any frames in the list after this frame */ |
---|
| 562 | lastFrame = frame; |
---|
| 563 | |
---|
| 564 | frame->sr = registers[(int) PS]; |
---|
| 565 | frame->pc = registers[(int) PC]; |
---|
| 566 | |
---|
| 567 | if (registers[(int) PS] & 0x2000) |
---|
[6128a4a] | 568 | { |
---|
[0074691a] | 569 | /* return to supervisor mode... */ |
---|
| 570 | return_to_super(); |
---|
| 571 | } |
---|
| 572 | else |
---|
| 573 | { /* return to user mode */ |
---|
| 574 | return_to_user(); |
---|
| 575 | } |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | int hex(char ch) |
---|
| 579 | { |
---|
| 580 | if ((ch >= 'a') && (ch <= 'f')) return (ch-'a'+10); |
---|
| 581 | if ((ch >= '0') && (ch <= '9')) return (ch-'0'); |
---|
| 582 | if ((ch >= 'A') && (ch <= 'F')) return (ch-'A'+10); |
---|
| 583 | return (-1); |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | /* scan for the sequence $<data>#<checksum> */ |
---|
| 587 | void getpacket(char *buffer) |
---|
| 588 | { |
---|
| 589 | unsigned char checksum; |
---|
| 590 | unsigned char xmitcsum; |
---|
| 591 | int i; |
---|
| 592 | int count; |
---|
| 593 | char ch; |
---|
[6128a4a] | 594 | |
---|
[0074691a] | 595 | do { |
---|
| 596 | /* wait around for the start character, ignore all other characters */ |
---|
[6128a4a] | 597 | while ((ch = (getDebugChar() & 0x7f)) != '$'); |
---|
[0074691a] | 598 | checksum = 0; |
---|
| 599 | xmitcsum = -1; |
---|
[6128a4a] | 600 | |
---|
[0074691a] | 601 | count = 0; |
---|
[6128a4a] | 602 | |
---|
[0074691a] | 603 | /* now, read until a # or end of buffer is found */ |
---|
| 604 | while (count < BUFMAX) { |
---|
| 605 | ch = getDebugChar() & 0x7f; |
---|
| 606 | if (ch == '#') break; |
---|
| 607 | checksum = checksum + ch; |
---|
| 608 | buffer[count] = ch; |
---|
| 609 | count = count + 1; |
---|
| 610 | } |
---|
| 611 | buffer[count] = 0; |
---|
| 612 | |
---|
| 613 | if (ch == '#') { |
---|
| 614 | xmitcsum = hex(getDebugChar() & 0x7f) << 4; |
---|
| 615 | xmitcsum += hex(getDebugChar() & 0x7f); |
---|
| 616 | if ((remote_debug ) && (checksum != xmitcsum)) { |
---|
| 617 | debug_port_printf ("bad checksum. My count = 0x%x, sent=0x%x. buf=%s\n", |
---|
| 618 | checksum,xmitcsum,buffer); |
---|
| 619 | } |
---|
[6128a4a] | 620 | |
---|
| 621 | if (checksum != xmitcsum) putDebugChar('-'); /* failed checksum */ |
---|
[0074691a] | 622 | else { |
---|
| 623 | putDebugChar('+'); /* successful transfer */ |
---|
| 624 | /* if a sequence char is present, reply the sequence ID */ |
---|
| 625 | if (buffer[2] == ':') { |
---|
| 626 | putDebugChar( buffer[0] ); |
---|
| 627 | putDebugChar( buffer[1] ); |
---|
| 628 | /* remove sequence chars from buffer */ |
---|
| 629 | count = strlen(buffer); |
---|
| 630 | for (i=3; i <= count; i++) buffer[i-3] = buffer[i]; |
---|
[6128a4a] | 631 | } |
---|
| 632 | } |
---|
| 633 | } |
---|
| 634 | } while (checksum != xmitcsum); |
---|
[0074691a] | 635 | } |
---|
| 636 | |
---|
[6128a4a] | 637 | /* send the packet in buffer. The host get's one chance to read it. |
---|
[0074691a] | 638 | This routine does not wait for a positive acknowledge. */ |
---|
| 639 | |
---|
| 640 | void |
---|
| 641 | putpacket(char *buffer) |
---|
| 642 | { |
---|
| 643 | unsigned char checksum; |
---|
| 644 | int count; |
---|
| 645 | char ch; |
---|
[6128a4a] | 646 | |
---|
[0074691a] | 647 | /* $<packet info>#<checksum>. */ |
---|
| 648 | do { |
---|
| 649 | putDebugChar('$'); |
---|
| 650 | checksum = 0; |
---|
| 651 | count = 0; |
---|
[6128a4a] | 652 | |
---|
[0074691a] | 653 | while ((ch=buffer[count])) { |
---|
| 654 | if (! putDebugChar(ch)) return; |
---|
| 655 | checksum += ch; |
---|
| 656 | count += 1; |
---|
| 657 | } |
---|
[6128a4a] | 658 | |
---|
[0074691a] | 659 | putDebugChar('#'); |
---|
| 660 | putDebugChar(hexchars[checksum >> 4]); |
---|
| 661 | putDebugChar(hexchars[checksum % 16]); |
---|
| 662 | |
---|
| 663 | } while (1 == 0); /* (getDebugChar() != '+'); */ |
---|
[6128a4a] | 664 | |
---|
[0074691a] | 665 | } |
---|
| 666 | |
---|
| 667 | char remcomInBuffer[BUFMAX]; |
---|
| 668 | char remcomOutBuffer[BUFMAX]; |
---|
| 669 | static short error; |
---|
| 670 | |
---|
| 671 | /* convert the memory pointed to by mem into hex, placing result in buf */ |
---|
| 672 | /* return a pointer to the last char put in buf (null) */ |
---|
| 673 | char *mem2hex(char *mem, char *buf, int count) |
---|
| 674 | { |
---|
| 675 | int i; |
---|
| 676 | unsigned char ch; |
---|
[6128a4a] | 677 | |
---|
[0074691a] | 678 | if (remote_debug) |
---|
[6128a4a] | 679 | debug_port_printf("mem=0x%x, count=0x%x\n", mem, count); |
---|
| 680 | |
---|
[0074691a] | 681 | for (i=0;i<count;i++) { |
---|
| 682 | ch = *mem++; |
---|
| 683 | *buf++ = hexchars[ch >> 4]; |
---|
| 684 | *buf++ = hexchars[ch % 16]; |
---|
| 685 | } |
---|
[6128a4a] | 686 | *buf = 0; |
---|
[0074691a] | 687 | return(buf); |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | /* convert the hex array pointed to by buf into binary to be placed in mem */ |
---|
| 691 | /* return a pointer to the character AFTER the last byte written */ |
---|
| 692 | char *hex2mem(char *buf, char *mem, int count) |
---|
| 693 | { |
---|
| 694 | int i; |
---|
| 695 | unsigned char ch; |
---|
[6128a4a] | 696 | |
---|
[0074691a] | 697 | if (remote_debug) |
---|
[6128a4a] | 698 | debug_port_printf("mem=0x%x, count=0x%x\n", mem, count); |
---|
| 699 | |
---|
[0074691a] | 700 | for (i=0;i<count;i++) { |
---|
| 701 | ch = hex(*buf++) << 4; |
---|
| 702 | ch = ch + hex(*buf++); |
---|
| 703 | *mem++ = ch; |
---|
| 704 | } |
---|
| 705 | return(mem); |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | /* a bus error has occurred, perform a longjmp |
---|
| 709 | to return execution and allow handling of the error */ |
---|
| 710 | |
---|
| 711 | void handle_buserror() |
---|
| 712 | { |
---|
| 713 | longjmp(remcomEnv,1); |
---|
| 714 | } |
---|
| 715 | |
---|
[6128a4a] | 716 | /* this function takes the 68000 exception number and attempts to |
---|
[0074691a] | 717 | translate this number into a unix compatible signal value */ |
---|
| 718 | int computeSignal(int exceptionVector) |
---|
| 719 | { |
---|
| 720 | int sigval; |
---|
| 721 | switch (exceptionVector) { |
---|
| 722 | case 2 : sigval = 10; break; /* bus error */ |
---|
| 723 | case 3 : sigval = 10; break; /* address error */ |
---|
| 724 | case 4 : sigval = 4; break; /* illegal instruction */ |
---|
| 725 | case 5 : sigval = 8; break; /* zero divide */ |
---|
| 726 | case 6 : sigval = 8; break; /* chk instruction */ |
---|
| 727 | case 7 : sigval = 8; break; /* trapv instruction */ |
---|
| 728 | case 8 : sigval = 11; break; /* privilege violation */ |
---|
| 729 | case 9 : sigval = 5; break; /* trace trap */ |
---|
| 730 | case 10: sigval = 4; break; /* line 1010 emulator */ |
---|
| 731 | case 11: sigval = 4; break; /* line 1111 emulator */ |
---|
| 732 | |
---|
| 733 | /* Coprocessor protocol violation. Using a standard MMU or FPU |
---|
| 734 | this cannot be triggered by software. Call it a SIGBUS. */ |
---|
| 735 | case 13: sigval = 10; break; |
---|
| 736 | |
---|
| 737 | case 31: sigval = 2; break; /* interrupt */ |
---|
| 738 | case 33: sigval = 5; break; /* breakpoint */ |
---|
| 739 | |
---|
| 740 | /* This is a trap #8 instruction. Apparently it is someone's software |
---|
| 741 | convention for some sort of SIGFPE condition. Whose? How many |
---|
| 742 | people are being screwed by having this code the way it is? |
---|
| 743 | Is there a clean solution? */ |
---|
| 744 | case 40: sigval = 8; break; /* floating point err */ |
---|
| 745 | |
---|
| 746 | case 48: sigval = 8; break; /* floating point err */ |
---|
| 747 | case 49: sigval = 8; break; /* floating point err */ |
---|
| 748 | case 50: sigval = 8; break; /* zero divide */ |
---|
| 749 | case 51: sigval = 8; break; /* underflow */ |
---|
| 750 | case 52: sigval = 8; break; /* operand error */ |
---|
| 751 | case 53: sigval = 8; break; /* overflow */ |
---|
| 752 | case 54: sigval = 8; break; /* NAN */ |
---|
[6128a4a] | 753 | default: |
---|
[0074691a] | 754 | sigval = 7; /* "software generated"*/ |
---|
| 755 | } |
---|
| 756 | return (sigval); |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | /**********************************************/ |
---|
| 760 | /* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */ |
---|
| 761 | /* RETURN NUMBER OF CHARS PROCESSED */ |
---|
| 762 | /**********************************************/ |
---|
| 763 | int hexToInt(char **ptr, int *intValue) |
---|
| 764 | { |
---|
| 765 | int numChars = 0; |
---|
| 766 | int hexValue; |
---|
[6128a4a] | 767 | |
---|
[0074691a] | 768 | *intValue = 0; |
---|
| 769 | |
---|
| 770 | while (**ptr) |
---|
| 771 | { |
---|
| 772 | hexValue = hex(**ptr); |
---|
| 773 | if (hexValue >=0) |
---|
| 774 | { |
---|
| 775 | *intValue = (*intValue <<4) | hexValue; |
---|
| 776 | numChars ++; |
---|
| 777 | } |
---|
| 778 | else |
---|
| 779 | break; |
---|
[6128a4a] | 780 | |
---|
[0074691a] | 781 | (*ptr)++; |
---|
| 782 | } |
---|
| 783 | |
---|
| 784 | return (numChars); |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | /* |
---|
| 788 | * This function does all command procesing for interfacing to gdb. |
---|
| 789 | */ |
---|
| 790 | void handle_exception(int exceptionVector) |
---|
| 791 | { |
---|
| 792 | int sigval; |
---|
| 793 | int addr, length; |
---|
| 794 | char * ptr; |
---|
| 795 | int newPC; |
---|
| 796 | Frame *frame; |
---|
[6128a4a] | 797 | |
---|
[0074691a] | 798 | if (remote_debug) |
---|
[6128a4a] | 799 | printf("vector=%d, sr=0x%x, pc=0x%x\n", |
---|
[0074691a] | 800 | exceptionVector, |
---|
[6128a4a] | 801 | registers[ PS ], |
---|
[0074691a] | 802 | registers[ PC ]); |
---|
| 803 | |
---|
| 804 | /* reply to host that an exception has occurred */ |
---|
| 805 | sigval = computeSignal( exceptionVector ); |
---|
| 806 | remcomOutBuffer[0] = 'S'; |
---|
| 807 | remcomOutBuffer[1] = hexchars[sigval >> 4]; |
---|
| 808 | remcomOutBuffer[2] = hexchars[sigval % 16]; |
---|
| 809 | remcomOutBuffer[3] = 0; |
---|
| 810 | |
---|
[6128a4a] | 811 | putpacket(remcomOutBuffer); |
---|
[0074691a] | 812 | |
---|
[6128a4a] | 813 | while (1==1) { |
---|
[0074691a] | 814 | error = 0; |
---|
| 815 | remcomOutBuffer[0] = 0; |
---|
| 816 | getpacket(remcomInBuffer); |
---|
| 817 | switch (remcomInBuffer[0]) { |
---|
| 818 | case '?' : remcomOutBuffer[0] = 'S'; |
---|
| 819 | remcomOutBuffer[1] = hexchars[sigval >> 4]; |
---|
| 820 | remcomOutBuffer[2] = hexchars[sigval % 16]; |
---|
| 821 | remcomOutBuffer[3] = 0; |
---|
[6128a4a] | 822 | break; |
---|
[0074691a] | 823 | case 'd' : |
---|
| 824 | remote_debug = !(remote_debug); /* toggle debug flag */ |
---|
| 825 | debug_port_printf("debug mode "); |
---|
| 826 | if (remote_debug) |
---|
[6128a4a] | 827 | { |
---|
[0074691a] | 828 | debug_port_printf("on\n"); |
---|
| 829 | } |
---|
| 830 | else |
---|
[6128a4a] | 831 | { |
---|
[0074691a] | 832 | debug_port_printf("off\n"); |
---|
[6128a4a] | 833 | } |
---|
| 834 | break; |
---|
[0074691a] | 835 | case 'g' : /* return the value of the CPU registers */ |
---|
| 836 | mem2hex((char*) registers, remcomOutBuffer, NUMREGBYTES); |
---|
| 837 | break; |
---|
| 838 | case 'G' : /* set the value of the CPU registers - return OK */ |
---|
| 839 | hex2mem(&remcomInBuffer[1], (char*) registers, NUMREGBYTES); |
---|
| 840 | strcpy(remcomOutBuffer,"OK"); |
---|
| 841 | break; |
---|
[6128a4a] | 842 | |
---|
[0074691a] | 843 | /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ |
---|
[6128a4a] | 844 | case 'm' : |
---|
[0074691a] | 845 | if (setjmp(remcomEnv) == 0) |
---|
| 846 | { |
---|
[6128a4a] | 847 | exceptionHandler(2,handle_buserror); |
---|
[0074691a] | 848 | |
---|
| 849 | /* TRY TO READ %x,%x. IF SUCCEED, SET PTR = 0 */ |
---|
| 850 | ptr = &remcomInBuffer[1]; |
---|
| 851 | if (hexToInt(&ptr,&addr)) |
---|
| 852 | if (*(ptr++) == ',') |
---|
[6128a4a] | 853 | if (hexToInt(&ptr,&length)) |
---|
[0074691a] | 854 | { |
---|
| 855 | ptr = 0; |
---|
| 856 | mem2hex((char*) addr, remcomOutBuffer, length); |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | if (ptr) |
---|
| 860 | { |
---|
| 861 | strcpy(remcomOutBuffer,"E01"); |
---|
| 862 | if (remote_debug) |
---|
| 863 | printf("malformed read memory command: %s",remcomInBuffer); |
---|
[6128a4a] | 864 | } |
---|
| 865 | } |
---|
[0074691a] | 866 | else { |
---|
[6128a4a] | 867 | exceptionHandler(2,_catchException); |
---|
[0074691a] | 868 | strcpy(remcomOutBuffer,"E03"); |
---|
| 869 | if (remote_debug) |
---|
| 870 | printf("bus error"); |
---|
[6128a4a] | 871 | } |
---|
| 872 | |
---|
[0074691a] | 873 | /* restore handler for bus error */ |
---|
[6128a4a] | 874 | exceptionHandler(2,_catchException); |
---|
[0074691a] | 875 | break; |
---|
[6128a4a] | 876 | |
---|
[0074691a] | 877 | /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */ |
---|
[6128a4a] | 878 | case 'M' : |
---|
[0074691a] | 879 | if (setjmp(remcomEnv) == 0) { |
---|
[6128a4a] | 880 | exceptionHandler(2,handle_buserror); |
---|
| 881 | |
---|
[0074691a] | 882 | /* TRY TO READ '%x,%x:'. IF SUCCEED, SET PTR = 0 */ |
---|
| 883 | ptr = &remcomInBuffer[1]; |
---|
| 884 | if (hexToInt(&ptr,&addr)) |
---|
| 885 | if (*(ptr++) == ',') |
---|
| 886 | if (hexToInt(&ptr,&length)) |
---|
| 887 | if (*(ptr++) == ':') |
---|
| 888 | { |
---|
| 889 | hex2mem(ptr, (char*) addr, length); |
---|
| 890 | ptr = 0; |
---|
| 891 | strcpy(remcomOutBuffer,"OK"); |
---|
| 892 | } |
---|
| 893 | if (ptr) |
---|
| 894 | { |
---|
| 895 | strcpy(remcomOutBuffer,"E02"); |
---|
| 896 | if (remote_debug) |
---|
| 897 | printf("malformed write memory command: %s",remcomInBuffer); |
---|
[6128a4a] | 898 | } |
---|
| 899 | } |
---|
[0074691a] | 900 | else { |
---|
[6128a4a] | 901 | exceptionHandler(2,_catchException); |
---|
[0074691a] | 902 | strcpy(remcomOutBuffer,"E03"); |
---|
| 903 | if (remote_debug) |
---|
| 904 | printf("bus error"); |
---|
[6128a4a] | 905 | } |
---|
[0074691a] | 906 | |
---|
| 907 | /* restore handler for bus error */ |
---|
[6128a4a] | 908 | exceptionHandler(2,_catchException); |
---|
[0074691a] | 909 | break; |
---|
[6128a4a] | 910 | |
---|
[0074691a] | 911 | /* cAA..AA Continue at address AA..AA(optional) */ |
---|
| 912 | /* sAA..AA Step one instruction from AA..AA(optional) */ |
---|
[6128a4a] | 913 | case 'c' : |
---|
| 914 | case 's' : |
---|
[0074691a] | 915 | /* try to read optional parameter, pc unchanged if no parm */ |
---|
| 916 | ptr = &remcomInBuffer[1]; |
---|
| 917 | if (hexToInt(&ptr,&addr)) |
---|
| 918 | registers[ PC ] = addr; |
---|
[6128a4a] | 919 | |
---|
[0074691a] | 920 | newPC = registers[ PC]; |
---|
[6128a4a] | 921 | |
---|
[0074691a] | 922 | /* clear the trace bit */ |
---|
| 923 | registers[ PS ] &= 0x7fff; |
---|
[6128a4a] | 924 | |
---|
[0074691a] | 925 | /* set the trace bit if we're stepping */ |
---|
| 926 | if (remcomInBuffer[0] == 's') registers[ PS ] |= 0x8000; |
---|
[6128a4a] | 927 | |
---|
[0074691a] | 928 | /* |
---|
| 929 | * look for newPC in the linked list of exception frames. |
---|
| 930 | * if it is found, use the old frame it. otherwise, |
---|
| 931 | * fake up a dummy frame in returnFromException(). |
---|
| 932 | */ |
---|
| 933 | if (remote_debug) debug_port_printf("new pc = 0x%x\n",newPC); |
---|
[6128a4a] | 934 | |
---|
[0074691a] | 935 | frame = lastFrame; |
---|
| 936 | while (frame) |
---|
| 937 | { |
---|
| 938 | if (remote_debug) |
---|
| 939 | debug_port_printf("frame at 0x%x has pc=0x%x, except#=%d\n", |
---|
| 940 | frame,frame->exceptionPC, |
---|
| 941 | (unsigned int) frame->exceptionVector); |
---|
| 942 | if (frame->exceptionPC == newPC) break; /* bingo! a match */ |
---|
| 943 | /* |
---|
| 944 | * for a breakpoint instruction, the saved pc may |
---|
| 945 | * be off by two due to re-executing the instruction |
---|
| 946 | * replaced by the trap instruction. Check for this. |
---|
| 947 | */ |
---|
| 948 | if ((frame->exceptionVector == 33) && |
---|
| 949 | (frame->exceptionPC == newPC)) break; |
---|
| 950 | if (frame == frame->previous) |
---|
| 951 | { |
---|
[6128a4a] | 952 | frame = 0; /* no match found */ |
---|
| 953 | break; |
---|
[0074691a] | 954 | } |
---|
| 955 | frame = frame->previous; |
---|
| 956 | } |
---|
[6128a4a] | 957 | |
---|
[0074691a] | 958 | /* |
---|
| 959 | * If we found a match for the PC AND we are not returning |
---|
| 960 | * as a result of a breakpoint (33), |
---|
| 961 | * trace exception (9), nmi (31), jmp to |
---|
| 962 | * the old exception handler as if this code never ran. |
---|
| 963 | */ |
---|
[6128a4a] | 964 | if (frame) |
---|
[0074691a] | 965 | { |
---|
[6128a4a] | 966 | if ((frame->exceptionVector != 9) && |
---|
| 967 | (frame->exceptionVector != 31) && |
---|
[0074691a] | 968 | (frame->exceptionVector != 33)) |
---|
[6128a4a] | 969 | { |
---|
[0074691a] | 970 | /* |
---|
| 971 | * invoke the previous handler. |
---|
| 972 | */ |
---|
| 973 | if (oldExceptionHook) |
---|
| 974 | (*oldExceptionHook) (frame->exceptionVector); |
---|
| 975 | newPC = registers[ PC ]; /* pc may have changed */ |
---|
| 976 | if (newPC != frame->exceptionPC) |
---|
| 977 | { |
---|
| 978 | if (remote_debug) |
---|
| 979 | debug_port_printf("frame at 0x%x has pc=0x%x, except#=%d\n", |
---|
| 980 | frame,frame->exceptionPC, |
---|
| 981 | (unsigned int) frame->exceptionVector); |
---|
| 982 | /* re-use the last frame, we're skipping it (longjump?)*/ |
---|
| 983 | frame = (Frame *) 0; |
---|
| 984 | _returnFromException( frame ); /* this is a jump */ |
---|
| 985 | } |
---|
| 986 | } |
---|
[6128a4a] | 987 | } |
---|
[0074691a] | 988 | |
---|
| 989 | /* if we couldn't find a frame, create one */ |
---|
| 990 | if (frame == 0) |
---|
| 991 | { |
---|
| 992 | frame = lastFrame -1 ; |
---|
[6128a4a] | 993 | |
---|
[0074691a] | 994 | /* by using a bunch of print commands with breakpoints, |
---|
| 995 | it's possible for the frame stack to creep down. If it creeps |
---|
| 996 | too far, give up and reset it to the top. Normal use should |
---|
| 997 | not see this happen. |
---|
| 998 | */ |
---|
| 999 | if ((unsigned int) (frame-2) < (unsigned int) &gdbFrameStack) |
---|
| 1000 | { |
---|
| 1001 | initializeRemcomErrorFrame(); |
---|
[6128a4a] | 1002 | frame = lastFrame; |
---|
[0074691a] | 1003 | } |
---|
| 1004 | frame->previous = lastFrame; |
---|
| 1005 | lastFrame = frame; |
---|
[6128a4a] | 1006 | frame = 0; /* null so _return... will properly initialize it */ |
---|
| 1007 | } |
---|
| 1008 | |
---|
[0074691a] | 1009 | _returnFromException( frame ); /* this is a jump */ |
---|
| 1010 | |
---|
| 1011 | break; |
---|
[6128a4a] | 1012 | |
---|
[0074691a] | 1013 | /* kill the program */ |
---|
| 1014 | case 'k' : |
---|
| 1015 | /* reset the board */ |
---|
| 1016 | WATCHDOG_TRIGGER(); |
---|
| 1017 | while (1 == 1); |
---|
| 1018 | break; |
---|
[6128a4a] | 1019 | |
---|
| 1020 | } /* switch */ |
---|
| 1021 | |
---|
[0074691a] | 1022 | /* reply to the request */ |
---|
[6128a4a] | 1023 | putpacket(remcomOutBuffer); |
---|
[0074691a] | 1024 | } |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | void initializeRemcomErrorFrame() |
---|
| 1028 | { |
---|
| 1029 | lastFrame = ((Frame *) &gdbFrameStack[FRAMESIZE-1]) - 1; |
---|
| 1030 | lastFrame->previous = lastFrame; |
---|
| 1031 | } |
---|
| 1032 | |
---|
[6128a4a] | 1033 | /* this function is used to set up exception handlers for tracing and |
---|
[0074691a] | 1034 | breakpoints */ |
---|
| 1035 | void set_debug_traps() |
---|
| 1036 | { |
---|
| 1037 | extern void _debug_level7(void); |
---|
| 1038 | extern void remcomHandler(void); |
---|
[6128a4a] | 1039 | |
---|
[0074691a] | 1040 | int exception; |
---|
| 1041 | |
---|
| 1042 | initializeRemcomErrorFrame(); |
---|
| 1043 | stackPtr = &remcomStack[STACKSIZE/sizeof(int) - 1]; |
---|
| 1044 | |
---|
| 1045 | registers[ PC ] = 0x400; |
---|
| 1046 | registers[ PS ] = 0x2700; |
---|
[6128a4a] | 1047 | |
---|
[0074691a] | 1048 | for (exception = 2; exception <= 30; exception++) |
---|
[6128a4a] | 1049 | exceptionHandler(exception,_catchException); |
---|
[0074691a] | 1050 | |
---|
| 1051 | /* level 7 interrupt */ |
---|
[6128a4a] | 1052 | exceptionHandler(31,_debug_level7); |
---|
| 1053 | |
---|
[0074691a] | 1054 | for (exception = 32; exception <= 47; exception++) |
---|
[6128a4a] | 1055 | exceptionHandler(exception,_catchException); |
---|
[0074691a] | 1056 | |
---|
| 1057 | /* exclude the unassigned, reserved vector locations */ |
---|
[6128a4a] | 1058 | |
---|
[0074691a] | 1059 | for (exception = 64; exception <= 255; exception++) |
---|
[6128a4a] | 1060 | exceptionHandler(exception,_catchException); |
---|
[0074691a] | 1061 | |
---|
| 1062 | if (oldExceptionHook != (ExceptionHook) remcomHandler) |
---|
| 1063 | { |
---|
| 1064 | oldExceptionHook = exceptionHook; |
---|
| 1065 | exceptionHook = (ExceptionHook) remcomHandler; |
---|
| 1066 | } |
---|
[6128a4a] | 1067 | |
---|
[0074691a] | 1068 | initialized = 1; |
---|
| 1069 | |
---|
[6128a4a] | 1070 | #if defined(UPDATE_DISPLAY) |
---|
[0074691a] | 1071 | UPDATE_DISPLAY("gdb "); |
---|
[6128a4a] | 1072 | #endif |
---|
[0074691a] | 1073 | } |
---|
| 1074 | |
---|
| 1075 | /* This function will generate a breakpoint exception. It is used at the |
---|
| 1076 | beginning of a program to sync up with a debugger and can be used |
---|
| 1077 | otherwise as a quick means to stop program execution and "break" into |
---|
| 1078 | the debugger. */ |
---|
[6128a4a] | 1079 | |
---|
[0074691a] | 1080 | void breakpoint() |
---|
| 1081 | { |
---|
| 1082 | if (initialized) BREAKPOINT(); |
---|
| 1083 | } |
---|