From ea6b0e3ed665ffb3aa18fdc535aeb1247eaabacc Mon Sep 17 00:00:00 2001
From: Caleb Battig <caleb.battig@netapp.com>
Date: Tue, 20 Oct 2020 10:29:14 -0600
Subject: [PATCH] Added-x86_64 stub for debugging embedded systems running
 Intel x86_64 architecture.

---
 gdb/stubs/x86_64-stub.c | 1272 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 1272 insertions(+)
 create mode 100644 gdb/stubs/x86_64-stub.c

diff --git a/gdb/stubs/x86_64-stub.c b/gdb/stubs/x86_64-stub.c
new file mode 100644
index 0000000000..eaf523a237
--- /dev/null
+++ b/gdb/stubs/x86_64-stub.c
@@ -0,0 +1,1272 @@
+/****************************************************************************
+
+		THIS SOFTWARE IS NOT COPYRIGHTED
+
+   HP offers the following for use in the public domain.  HP makes no
+   warranty with regard to the software or it's performance and the
+   user accepts the software "AS IS" with all faults.
+
+   HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
+   TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+
+****************************************************************************/
+
+/****************************************************************************
+ *  Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
+ *
+ *  Module name: remcom.c $
+ *  Revision: 1.34 $
+ *  Date: 91/03/09 12:29:49 $
+ *  Contributor:     Lake Stevens Instrument Division$
+ *
+ *  Description:     low level support for gdb debugger. $
+ *
+ *  Considerations:  only works on target hardware $
+ *
+ *  Written by:      Glenn Engel $
+ *  ModuleState:     Experimental $
+ *
+ *  NOTES:           See Below $
+ *
+ *  Modified for 386 by Jim Kingdon, Cygnus Support.
+ *
+ *  To enable debugger support, two things need to happen.  One, a
+ *  call to set_debug_traps() is necessary in order to allow any breakpoints
+ *  or error conditions to be properly intercepted and reported to gdb.
+ *  Two, a breakpoint needs to be generated to begin communication.  This
+ *  is most easily accomplished by a call to breakpoint().  Breakpoint()
+ *  simulates a breakpoint by executing a trap #1.
+ *
+ *  The function exceptionHandler () is
+ *  used to attach a specific handler to a specific 386 vector number.
+ *  It should use the same privilege level it runs at.  It should
+ *  install it as an interrupt gate so that interrupts are masked
+ *  while the handler runs.
+ *
+ *  Because gdb will sometimes write to the stack area to execute function
+ *  calls, this program cannot rely on using the supervisor stack so it
+ *  uses it's own stack area reserved in the int array remcomStack.
+ *
+ *************
+ *
+ *    The following gdb commands are supported:
+ *
+ * command          function                               Return value
+ *
+ *    g             return the value of the CPU _registers  hex data or ENN
+ *    G             set the value of the CPU _registers     OK or ENN
+ *
+ *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
+ *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
+ *
+ *    c             Resume at current address              SNN   ( signal NN)
+ *    cAA..AA       Continue at address AA..AA             SNN
+ *
+ *    s             Step one instruction                   SNN
+ *    sAA..AA       Step one instruction from AA..AA       SNN
+ *
+ *    k             kill
+ *
+ *    ?             What was the last sigval ?             SNN   (signal NN)
+ *
+ * All commands and responses are sent with a packet which includes a
+ * checksum.  A packet consists of
+ *
+ * $<packet info>#<checksum>.
+ *
+ * where
+ * <packet info> :: <characters representing the command or response>
+ * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
+ *
+ * When a packet is received, it is first acknowledged with either '+' or '-'.
+ * '+' indicates a successful transfer.  '-' indicates a failed transfer.
+ *
+ * Example:
+ *
+ * Host:                  Reply:
+ * $m0,10#2a               +$00010203040506070809101112131415#42
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ *
+ * Update 2020/03/05
+ *
+ * This stub is based on the old x86 stub (i386-stub.c) provided by gdb.
+ * It has been updated to support intel x86_64 processor architecture.
+ *
+ ****************************************************************************/
+
+/*
+ * We don't want GCC to compile this file with any optimizations since
+ * it contains so much embedded x64 assembly code.
+ */
+#pragma GCC optimize("-O0")
+
+#include <stdio.h>
+#include <string.h>
+
+/* single threaded target */
+#define THREAD_TID "1"
+
+/************************************************************************
+ *
+ * External low-level support routines.
+ * These functions should have an OS specific implementation provided.
+ * See - https://sourceware.org/gdb/onlinedocs/gdb/Bootstrapping.html#Bootstrapping.
+ *
+ * For convenience, default implementations of these functions are provided at the
+ * end of this file, but they may not apply to your OS. To enable the default
+ * implementations uncomment the below defines:
+ *
+ * #define GDB_USE_SERIAL_PORT_IO              // for the functions putDebugChar() and getDebugChar()
+ * #define GDB_USE_STD_X64_EXCEPTION_HANDLER   // for the function exceptionHandler()
+ */
+extern void putDebugChar (char c); /* write a single character      */
+extern int getDebugChar (void); /* read and return a single char */
+extern void exceptionHandler (int intr_vector, void *intr_handler); /* install intr_handler as handler for exception intr_vector */
+/************************************************************************/
+
+/************************************************************************
+ * Internal support routines
+ */
+static char *gdb_strcpy (char *dest, const char *src); /* essentially strcpy */
+static void gdb_bcopy (void *from, void *to, int length);
+static int gdb_str_starts_with (char *str, char *prefix);
+static size_t gdb_run_length_encode (char *buffer, size_t length);
+/************************************************************************/
+
+/* BUFMAX defines the maximum number of characters in inbound/outbound buffers*/
+/* at least NUMREGBYTES*2 are needed for register packets */
+#define BUFMAX 1024
+
+static char initialized = 0;  /* boolean flag. != 0 means we've been initialized */
+static const char hexchars[] = "0123456789abcdef";
+
+/*
+ * GDB expects the 16-bit segment registers (%cs, %ss, %ds, %es, %fs, %gs)
+ * as well as the 64-bit rflags register to be stored in 32-bit fields.
+ * So we need to separate the registers into two arrays.
+ */
+
+/* Number of non-segment registers. */
+#define NUMREGS	17
+/*  Number of segment registers. */
+#define NUMSEGREGS 7
+
+/* Number of bytes of 64-bit registers. */
+#define NUMREGBYTES (NUMREGS * 8)
+/* Need to store the segment registers in 32-bit fields. */
+#define NUMSEGREGBYTES (NUMSEGREGS * 4)
+
+/* Names of 64-bit registers */
+enum regnames { RAX, RBX, RCX, RDX, RSI, RDI, RBP, RSP,
+                R8,  R9,  R10, R11, R12, R13, R14, R15,
+	              RIP };
+/*
+ * Names of 16-bit segment registers.
+ * rflags is included here because GDB expects it to be stored
+ * in a 32-bit field
+ */
+enum segregnames {  PS /* also known as rflags */, CS, SS, DS, ES, FS, GS };
+
+/*
+ * These should not be static because they can be used outside this module
+ */
+uint64_t _registers[NUMREGS];
+uint32_t _segregisters[NUMSEGREGS];
+
+#define STACKSIZE 10000
+uint64_t remcomStack[STACKSIZE/sizeof(uint64_t)];
+static uint64_t* _stackPtr = &remcomStack[STACKSIZE/sizeof(uint64_t) - 1];
+
+
+/***************************  ASSEMBLY CODE MACROS *************************/
+
+extern void
+_return_to_prog ();
+
+/* Restore the program's _registers (including the stack pointer, which
+   means we get the right stack and don't have to worry about popping our
+   return address and any stack frames and so on) and return.  */
+asm(".text");
+asm(".globl _return_to_prog");
+asm("_return_to_prog:");
+  asm("movw _segregisters+8, %ss");
+  asm("movq _registers+56, %rsp");
+  asm("movq _registers+8,  %rbx");
+  asm("movq _registers+16, %rcx");
+  asm("movq _registers+24, %rdx");
+  asm("movq _registers+32, %rsi");
+  asm("movq _registers+40, %rdi");
+  asm("movq _registers+48, %rbp");
+  asm("movq _registers+64, %r8");
+  asm("movq _registers+72, %r9");
+  asm("movq _registers+80, %r10");
+  asm("movq _registers+88, %r11");
+  asm("movq _registers+96, %r12");
+  asm("movq _registers+104, %r13");
+  asm("movq _registers+112, %r14");
+  asm("movq _registers+120, %r15");
+  asm("movw _segregisters+12, %ds");
+  asm("movw _segregisters+16, %es");
+  asm("movw _segregisters+20, %fs");
+  asm("movw _segregisters+24, %gs");
+  asm("xorq %rax, %rax");
+  asm("movl _segregisters, %eax");
+  asm("pushq %rax");  /* saved rflags */
+  asm("movl _segregisters+4, %eax");
+  asm("pushq %rax");  /* saved cs */
+  asm("movq _registers+128, %rax");
+  asm("pushq %rax");  /* saved rip */
+  asm("movq _registers, %rax");
+  /* use iretq to restore rip and rflags together so
+     that trace flag works right.  */
+  asm("iretq");
+
+#define BREAKPOINT() asm("  int $3")
+
+/* Put the error code here just in case the user cares.  */
+uint64_t _gdb_errcode;
+/* Likewise, the vector number here (since GDB only gets the signal
+   number through the usual means, and that's not very specific).  */
+uint64_t gdb_vector = -1;
+
+/* GDB stores segment _registers in 32-bit words (that's just the way
+   m-i386v.h is written).  So zero the appropriate areas in _segregisters.  */
+#define SAVE_REGISTERS1()                \
+  asm ("movq %rax, _registers");         \
+  asm ("movq %rbx, _registers+8");	     \
+  asm ("movq %rcx, _registers+16");	     \
+  asm ("movq %rdx, _registers+24");	     \
+  asm ("movq %rsi, _registers+32");	     \
+  asm ("movq %rdi, _registers+40");	     \
+  asm ("movq %rbp, _registers+48");	     \
+  asm ("movq %r8,  _registers+64");	     \
+  asm ("movq %r9,  _registers+72");	     \
+  asm ("movq %r10, _registers+80");	     \
+  asm ("movq %r11, _registers+88");	     \
+  asm ("movq %r12, _registers+96");	     \
+  asm ("movq %r13, _registers+104");     \
+  asm ("movq %r14, _registers+112");     \
+  asm ("movq %r15, _registers+120");     \
+  asm ("movw $0, %ax");                  \
+  asm ("movw %ds,  _segregisters+12");   \
+  asm ("movw %ax,  _segregisters+14");   \
+  asm ("movw %es,  _segregisters+16");   \
+  asm ("movw %ax,  _segregisters+18");   \
+  asm ("movw %fs,  _segregisters+20");   \
+  asm ("movw %ax,  _segregisters+22");   \
+  asm ("movw %gs,  _segregisters+24");   \
+  asm ("movw %ax,  _segregisters+26")
+#define SAVE_ERRCODE()                   \
+  asm ("popq %rbx");                     \
+  asm ("movq %rbx, _gdb_errcode")
+#define SAVE_REGISTERS2()                 \
+  asm ("popq %rbx"); /* old rip */        \
+  asm ("movq %rbx, _registers+128");      \
+  asm ("popq %rbx");	 /* old cs */	      \
+  asm ("movl %ebx, _segregisters+4");     \
+  asm ("popq %rbx");	 /* old rflags */   \
+  asm ("movl %ebx, _segregisters");	      \
+  /* Now that we've done the pops, we can save the stack pointer." */   \
+  asm ("movw %ss,  _segregisters+8");	    \
+  asm ("movw %ax,  _segregisters+10");    \
+  asm ("movq %rsp, _registers+56")
+
+/* See if _mem_fault_routine is set, if so just iretq to that address.  */
+#define CHECK_FAULT()                     \
+  asm ("cmpq $0, _mem_fault_routine");    \
+  asm ("jne mem_fault")
+
+asm (".text");
+asm ("mem_fault:");
+  /* OK to clobber temp _registers; we're just going to end up in set_mem_err.  */
+  /* Pop error code from the stack and save it.  */
+  asm ("popq %rax");
+  asm ("movq %rax, _gdb_errcode");
+
+  asm ("popq %rax"); /* rip */
+  /* We don't want to return there, we want to return to the function
+     pointed to by _mem_fault_routine instead.  */
+  asm ("movq _mem_fault_routine, %rax");
+  asm ("popq %rcx"); /* cs (low 16 bits; junk in hi 16 bits).  */
+  asm ("popq %rdx"); /* rflags */
+
+  /* Remove this stack frame; when we do the iretq, we will be going to
+     the start of a function, so we want the stack to look just like it
+     would after a "call" instruction.  */
+  asm ("leave");
+
+  /* Push the stuff that iretq wants.  */
+  asm ("pushq %rdx"); /* rflags */
+  asm ("pushq %rcx"); /* cs */
+  asm ("pushq %rax"); /* rip */
+
+  /* Zero _mem_fault_routine.  */
+  asm ("movq $0, %rax");
+  asm ("movq %rax, _mem_fault_routine");
+
+  asm ("iretq");
+
+#define CALL_HOOK() asm("call _remcomHandler")
+
+/* These functions are called when an exception occurs.  It saves
+ * all the cpu regs in the _registers array, munges the stack a bit,
+ * and invokes an exception handler (remcom_handler).
+ *
+ * stack on entry:                       stack on exit:
+ *   old rflags                          vector number
+ *   old cs (zero-filled to 64 bits)
+ *   old rip
+ *
+ */
+extern void
+_catchException3 (void);
+asm(".text");
+asm(".globl _catchException3");
+asm("_catchException3:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $3");
+  CALL_HOOK();
+
+/* Same thing for exception 1.  */
+extern void
+_catchException1 (void);
+asm(".text");
+asm(".globl _catchException1");
+asm("_catchException1:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $1");
+  CALL_HOOK();
+
+/* Same thing for exception 0.  */
+extern void
+_catchException0 (void);
+asm(".text");
+asm(".globl _catchException0");
+asm("_catchException0:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $0");
+  CALL_HOOK();
+
+/* Same thing for exception 4.  */
+extern void
+_catchException4 (void);
+asm(".text");
+asm(".globl _catchException4");
+asm("_catchException4:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $4");
+  CALL_HOOK();
+
+/* Same thing for exception 5.  */
+extern void
+_catchException5 (void);
+asm(".text");
+asm(".globl _catchException5");
+asm("_catchException5:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $5");
+  CALL_HOOK();
+
+/* Same thing for exception 6.  */
+extern void
+_catchException6 (void);
+asm(".text");
+asm(".globl _catchException6");
+asm("_catchException6:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $6");
+  CALL_HOOK();
+
+/* Same thing for exception 7.  */
+extern void
+_catchException7 (void);
+asm(".text");
+asm(".globl _catchException7");
+asm("_catchException7:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $7");
+  CALL_HOOK();
+
+/* Same thing for exception 8.  */
+extern void
+_catchException8 (void);
+asm(".text");
+asm(".globl _catchException8");
+asm("_catchException8:");
+  SAVE_REGISTERS1();
+  SAVE_ERRCODE();
+  SAVE_REGISTERS2();
+  asm ("pushq $8");
+  CALL_HOOK();
+
+/* Same thing for exception 9.  */
+extern void
+_catchException9 (void);
+asm(".text");
+asm(".globl _catchException9");
+asm("_catchException9:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $9");
+  CALL_HOOK();
+
+/* Same thing for exception 10.  */
+extern void
+_catchException10 (void);
+asm(".text");
+asm(".globl _catchException10");
+asm("_catchException10:");
+  SAVE_REGISTERS1();
+  SAVE_ERRCODE();
+  SAVE_REGISTERS2();
+  asm ("pushq $10");
+  CALL_HOOK();
+
+/* Same thing for exception 12.  */
+extern void
+_catchException12 (void);
+asm(".text");
+asm(".globl _catchException12");
+asm("_catchException12:");
+  SAVE_REGISTERS1();
+  SAVE_ERRCODE();
+  SAVE_REGISTERS2();
+  asm ("pushq $12");
+  CALL_HOOK();
+
+/* Same thing for exception 16.  */
+extern void
+_catchException16 (void);
+asm(".text");
+asm(".globl _catchException16");
+asm("_catchException16:");
+  SAVE_REGISTERS1();
+  SAVE_REGISTERS2();
+  asm ("pushq $16");
+  CALL_HOOK();
+
+/* For 13, 11, and 14 we have to deal with the CHECK_FAULT stuff.  */
+
+/* Same thing for exception 13.  */
+extern void
+_catchException13 (void);
+asm (".text");
+asm (".globl _catchException13");
+asm ("_catchException13:");
+  CHECK_FAULT();
+  SAVE_REGISTERS1();
+  SAVE_ERRCODE();
+  SAVE_REGISTERS2();
+  asm ("pushq $13");
+  CALL_HOOK();
+
+/* Same thing for exception 11.  */
+extern void
+_catchException11 (void);
+asm (".text");
+asm (".globl _catchException11");
+asm ("_catchException11:");
+  CHECK_FAULT();
+  SAVE_REGISTERS1();
+  SAVE_ERRCODE();
+  SAVE_REGISTERS2();
+  asm ("pushq $11");
+  CALL_HOOK();
+
+/* Same thing for exception 14.  */
+extern void
+_catchException14 (void);
+asm (".text");
+asm (".globl _catchException14");
+asm ("_catchException14:");
+  CHECK_FAULT();
+  SAVE_REGISTERS1();
+  SAVE_ERRCODE();
+  SAVE_REGISTERS2();
+  asm ("pushq $14");
+  CALL_HOOK();
+
+/*
+ * remcomHandler is a front end for _handle_exception.  It moves the
+ * stack pointer into an area reserved for debugger use.
+ */
+asm("_remcomHandler:");
+  asm("popq %rax");        /* pop off return address     */
+  asm("popq %rdi");      /* get the exception number in %rdi to pass it as the first argument to  _handle_exception  */
+  asm("movq _stackPtr, %rsp"); /* move to remcom stack area  */
+  asm("call  _handle_exception");    /* this never returns */
+
+void
+_returnFromException ()
+{
+  _return_to_prog ();
+}
+
+void
+debug_error (format, parm)
+     char *format;
+     char *parm;
+{
+  printf (format, parm);
+}
+
+int
+hex (ch)
+     char ch;
+{
+  if ((ch >= 'a') && (ch <= 'f'))
+    return (ch - 'a' + 10);
+  if ((ch >= '0') && (ch <= '9'))
+    return (ch - '0');
+  if ((ch >= 'A') && (ch <= 'F'))
+    return (ch - 'A' + 10);
+  return (-1);
+}
+
+static char remcomInBuffer[BUFMAX];
+static char remcomOutBuffer[BUFMAX];
+
+/* scan for the sequence $<data>#<checksum>     */
+
+char *
+getpacket (void)
+{
+  char *buffer = &remcomInBuffer[0];
+  unsigned char checksum;
+  unsigned char xmitcsum;
+  int count;
+  char ch;
+
+  while (1)
+    {
+      /* wait around for the start character, ignore all other characters */
+      while ((ch = getDebugChar ()) != '$')
+	;
+
+    retry:
+      checksum = 0;
+      xmitcsum = -1;
+      count = 0;
+
+      /* now, read until a # or end of buffer is found */
+      while (count < BUFMAX)
+	{
+	  ch = getDebugChar ();
+	  if (ch == '$')
+	    goto retry;
+	  if (ch == '#')
+	    break;
+	  checksum = checksum + ch;
+	  buffer[count] = ch;
+	  count = count + 1;
+	}
+      buffer[count] = 0;
+
+      if (ch == '#')
+	{
+	  ch = getDebugChar ();
+	  xmitcsum = hex (ch) << 4;
+	  ch = getDebugChar ();
+	  xmitcsum += hex (ch);
+
+	  if (checksum != xmitcsum)
+    {
+      debug_error ("failed checksum");
+      putDebugChar ('-');	/* failed checksum */
+    }
+	  else
+	    {
+	      putDebugChar ('+');	/* successful transfer */
+
+	      /* if a sequence char is present, reply the sequence ID */
+	      if (buffer[2] == ':')
+		{
+		  putDebugChar (buffer[0]);
+		  putDebugChar (buffer[1]);
+
+		  return &buffer[3];
+		}
+
+	      return &buffer[0];
+	    }
+	}
+    }
+}
+
+/* send the packet in buffer.  */
+
+void
+putpacket (char *buffer)
+{
+  unsigned char checksum;
+  int count;
+  char ch;
+
+  /*  $<packet info>#<checksum>. */
+  do
+    {
+      putDebugChar ('$');
+      checksum = 0;
+      count = strlen (buffer);
+
+      count = gdb_run_length_encode (buffer, count);
+      buffer[count] = '\0';
+      count = 0;
+
+      while ((ch = buffer[count]))
+	{
+	  putDebugChar (ch);
+	  checksum += ch;
+	  count += 1;
+	}
+
+      putDebugChar ('#');
+      putDebugChar (hexchars[checksum >> 4]);
+      putDebugChar (hexchars[checksum & 0x0f]);
+
+    }
+  while (getDebugChar () != '+');
+}
+
+/* Address of a routine to RTE to if we get a memory fault.  */
+static void (*volatile _mem_fault_routine) () = NULL;
+
+/* Indicate to caller of mem2hex or hex2mem that there has been an
+   error.  */
+static volatile int mem_err = 0;
+
+void
+set_mem_err (void)
+{
+  mem_err = 1;
+}
+
+/* These are separate functions so that they are so short and sweet
+   that the compiler won't save any _registers (if there is a fault
+   to mem_fault, they won't get restored, so there better not be any
+   saved).  */
+int
+get_char (char *addr)
+{
+  return *addr;
+}
+
+void
+set_char (char *addr, int val)
+{
+  *addr = val;
+}
+
+/* convert the memory pointed to by mem into hex, placing result in buf */
+/* return a pointer to the last char put in buf (null) */
+/* If MAY_FAULT is non-zero, then we should set mem_err in response to
+   a fault; if zero treat a fault like any other fault in the stub.  */
+char *
+mem2hex (char *mem, char * buf, uint32_t count, uint32_t may_fault)
+{
+  int i;
+  unsigned char ch;
+
+  if (may_fault) {
+    _mem_fault_routine = set_mem_err;
+  }
+
+  for (i = 0; i < count; i++)
+  {
+    ch = get_char (mem++);
+    if (may_fault && mem_err) {
+      return (buf);
+    }
+    *buf++ = hexchars[ch >> 4];
+    *buf++ = hexchars[ch & 0x0f];
+  }
+  *buf = 0;
+
+  if (may_fault) {
+    _mem_fault_routine = NULL;
+  }
+
+  return (buf);
+}
+
+/* convert the hex array pointed to by buf into binary to be placed in mem */
+/* return a pointer to the character AFTER the last byte written */
+char *
+hex2mem (char *buf, char *mem, uint32_t count, uint32_t may_fault)
+{
+  int i;
+  unsigned char ch;
+
+  if (may_fault)
+    _mem_fault_routine = set_mem_err;
+  for (i = 0; i < count; i++)
+    {
+      ch = hex (*buf++) << 4;
+      ch = ch + hex (*buf++);
+      set_char (mem++, ch);
+      if (may_fault && mem_err)
+	return (mem);
+    }
+  if (may_fault)
+    _mem_fault_routine = NULL;
+  return (mem);
+}
+
+/* this function takes the x64 exception vector and attempts to
+   translate this number into a unix compatible signal value */
+uint32_t
+computeSignal (uint64_t exceptionVector)
+{
+  uint32_t sigval;
+  switch (exceptionVector)
+    {
+    case 0:
+      sigval = 8;
+      break;			/* divide by zero */
+    case 1:
+      sigval = 5;
+      break;			/* debug exception */
+    case 3:
+      sigval = 5;
+      break;			/* breakpoint */
+    case 4:
+      sigval = 16;
+      break;			/* into instruction (overflow) */
+    case 5:
+      sigval = 16;
+      break;			/* bound instruction */
+    case 6:
+      sigval = 4;
+      break;			/* Invalid opcode */
+    case 7:
+      sigval = 8;
+      break;			/* coprocessor not available */
+    case 8:
+      sigval = 7;
+      break;			/* double fault */
+    case 9:
+      sigval = 11;
+      break;			/* coprocessor segment overrun */
+    case 10:
+      sigval = 11;
+      break;			/* Invalid TSS */
+    case 11:
+      sigval = 11;
+      break;			/* Segment not present */
+    case 12:
+      sigval = 11;
+      break;			/* stack exception */
+    case 13:
+      sigval = 11;
+      break;			/* general protection */
+    case 14:
+      sigval = 11;
+      break;			/* page fault */
+    case 16:
+      sigval = 7;
+      break;			/* coprocessor error */
+    default:
+      sigval = 7;		/* "software generated" */
+    }
+  return (sigval);
+}
+
+/**********************************************/
+/* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
+/* RETURN NUMBER OF CHARS PROCESSED           */
+/**********************************************/
+uint32_t
+hexToInt (char **ptr, int64_t *intValue)
+{
+  uint32_t numChars = 0;
+  int hexValue;
+
+  *intValue = 0;
+
+  while (**ptr)
+    {
+      hexValue = hex (**ptr);
+      if (hexValue >= 0)
+	{
+	  *intValue = (*intValue << 4) | hexValue;
+	  numChars++;
+	}
+      else
+	break;
+
+      (*ptr)++;
+    }
+
+  return (numChars);
+}
+
+/*
+ * This function does all command procesing for interfacing to gdb.
+ */
+void
+_handle_exception (uint64_t exceptionVector)
+{
+  uint32_t sigval, stepping;
+  int64_t addr, length;
+  char *ptr;
+
+  gdb_vector = exceptionVector;
+
+  /* reply to host that an exception has occurred */
+  sigval = computeSignal (exceptionVector);
+
+  ptr = remcomOutBuffer;
+
+  *ptr++ = 'T';			/* notify gdb with signo, RIP, and thread id */
+  *ptr++ = hexchars[sigval >> 4];
+  *ptr++ = hexchars[sigval & 0x0f];
+
+  *ptr++ = hexchars[RIP >> 4];
+  *ptr++ = hexchars[RIP & 0x0f];
+  *ptr++ = ':';
+  ptr = mem2hex ((char *)&_registers[RIP], ptr, 8, 0); /* RIP */
+  *ptr++ = ';';
+
+  gdb_strcpy (ptr, "thread:"THREAD_TID";");
+
+  putpacket (remcomOutBuffer);
+
+  stepping = 0;
+
+  while (1 == 1)
+    {
+      remcomOutBuffer[0] = 0;
+      ptr = getpacket ();
+
+      switch (*ptr++)
+	{
+	case '?':
+	  remcomOutBuffer[0] = 'S';
+	  remcomOutBuffer[1] = hexchars[sigval >> 4];
+	  remcomOutBuffer[2] = hexchars[sigval & 0x0f];
+	  remcomOutBuffer[3] = 0;
+	  break;
+  case 'D': /* detach */
+    gdb_strcpy (remcomOutBuffer, "OK");
+    break;
+	case 'g':		/* return the value of the CPU _registers */
+	  ptr = mem2hex ((char *) _registers, remcomOutBuffer, NUMREGBYTES, 0);
+	  mem2hex ((char *) _segregisters, ptr, NUMSEGREGBYTES, 0);
+    break;
+	case 'G':		/* set the value of the CPU _registers - return OK */
+	  hex2mem (ptr, (char *) _registers, NUMREGBYTES, 0);
+    ptr += (NUMREGBYTES * 2);
+    hex2mem (ptr, (char *) _segregisters, NUMSEGREGBYTES, 0);
+	  gdb_strcpy (remcomOutBuffer, "OK");
+	  break;
+  case 'p':  /* return the value of a single CPU register */
+  {
+    int64_t regno;
+
+    if (hexToInt (&ptr, &regno)) {
+      if (regno >= 0) {
+        if (regno < NUMREGS) {
+          mem2hex ((char *) &_registers[regno], remcomOutBuffer, 8, 0);
+          break;
+        } else if (regno < (NUMREGS + NUMSEGREGS)) {
+          mem2hex ((char *) &_segregisters[regno - NUMREGS], remcomOutBuffer, 4, 0);
+          break;
+        }
+      }
+    }
+
+    gdb_strcpy (remcomOutBuffer, "E01");
+    break;
+  }
+	case 'P':		/* set the value of a single CPU register - return OK */
+  {
+	    int64_t regno;
+
+	    if (hexToInt (&ptr, &regno) && *ptr++ == '=') {
+	      if (regno >= 0) {
+          if (regno < NUMREGS) {
+            hex2mem (ptr, (char *) &_registers[regno], 8, 0);
+            gdb_strcpy (remcomOutBuffer, "OK");
+		        break;
+          } else if (regno < (NUMREGS + NUMSEGREGS)) {
+            hex2mem (ptr, (char *) &_segregisters[regno], 4, 0);
+            gdb_strcpy (remcomOutBuffer, "OK");
+            break;
+          } else {
+            /*
+             * Hack to deal with gdb client asking to set weird registers
+             * such as "orig_rax" - lie and say we set the non-existent register.
+             */
+            gdb_strcpy (remcomOutBuffer, "OK");
+            break;
+          }
+        }
+      }
+      gdb_strcpy (remcomOutBuffer, "E01");
+      break;
+  }
+  /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
+	case 'm':
+	  /* TRY TO READ %x,%x.  IF SUCCEED, SET PTR = 0 */
+	  if (hexToInt (&ptr, &addr))
+	    if (*(ptr++) == ',')
+	      if (hexToInt (&ptr, &length))
+		{
+		  ptr = 0;
+		  mem_err = 0;
+		  mem2hex ((char *) addr, remcomOutBuffer, length, 1);
+		  if (mem_err)
+		    {
+		      gdb_strcpy (remcomOutBuffer, "E03");
+		      debug_error ("memory fault");
+		    }
+		}
+
+	  if (ptr)
+	    {
+	      gdb_strcpy (remcomOutBuffer, "E01");
+	    }
+	  break;
+
+	  /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
+	case 'M':
+	  /* TRY TO READ '%x,%x:'.  IF SUCCEED, SET PTR = 0 */
+	  if (hexToInt (&ptr, &addr))
+	    if (*(ptr++) == ',')
+	      if (hexToInt (&ptr, &length))
+		if (*(ptr++) == ':')
+		  {
+		    mem_err = 0;
+		    hex2mem (ptr, (char *) addr, length, 1);
+
+		    if (mem_err)
+		      {
+			gdb_strcpy (remcomOutBuffer, "E03");
+			debug_error ("memory fault");
+		      }
+		    else
+		      {
+			gdb_strcpy (remcomOutBuffer, "OK");
+		      }
+
+		    ptr = 0;
+		  }
+	  if (ptr)
+	    {
+	      gdb_strcpy (remcomOutBuffer, "E02");
+	    }
+	  break;
+
+	  /* cAA..AA    Continue at address AA..AA(optional) */
+	  /* sAA..AA   Step one instruction from AA..AA(optional) */
+	case 's':
+	  stepping = 1;
+  /* FALLTHROUGH */
+	case 'c':
+	  /* try to read optional parameter,RIP unchanged if no parm */
+	  if (hexToInt (&ptr, &addr)) {
+	    _registers[RIP] = addr;
+    }
+
+	  /* clear the trace bit */
+	  _segregisters[PS] &= 0xfffffeff;
+
+	  /* set the trace bit if we're stepping */
+	  if (stepping) {
+	    _segregisters[PS] |= 0x00000100;
+    }
+
+	  _returnFromException ();	/* this is a jump */
+	  break;
+
+	  /* kill the program */
+	case 'k':
+    /*
+     * We don't actually want to reboot the loader - do nothing.
+     */
+	  break;
+
+  /* Set thread for subsequent operations; ignore this since we are a single threaded target */
+  case 'H':
+    gdb_strcpy (remcomOutBuffer, "OK");
+    break;
+
+  /*
+   * General Query Packets
+   *
+   * See - https://sourceware.org/gdb/current/onlinedocs/gdb/General-Query-Packets.html
+   */
+  case 'q':
+    /* qSupported packet */
+    if (gdb_str_starts_with(ptr, "Supported")) {
+      gdb_strcpy (remcomOutBuffer, "multiprocess-;vContSupported-;QThreadEvents-");
+    }
+    /* qC packet */
+    else if (strcmp(ptr, "C") == 0) {
+      // single threaded target - reply with thread id
+      gdb_strcpy (remcomOutBuffer, "QC"THREAD_TID);
+    }
+    /* qTStatus packet */
+    else if (strcmp (ptr, "TStatus") == 0) {
+      // no trace is running
+      gdb_strcpy (remcomOutBuffer, "T0");
+    }
+    break;
+	}			/* switch */
+
+      /* reply to the request */
+      putpacket (remcomOutBuffer);
+    }
+}
+
+/* this function is used to set up exception handlers for tracing and
+   breakpoints */
+void
+set_debug_traps (void)
+{
+  _stackPtr = &remcomStack[STACKSIZE / sizeof (uint64_t) - 1];
+
+  printf("Setting Debug Traps for GDB.\n\n");
+
+  exceptionHandler (0, _catchException0);
+  exceptionHandler (1, _catchException1);
+  exceptionHandler (3, _catchException3);
+  exceptionHandler (4, _catchException4);
+  exceptionHandler (5, _catchException5);
+  exceptionHandler (6, _catchException6);
+  exceptionHandler (7, _catchException7);
+  exceptionHandler (8, _catchException8);
+  exceptionHandler (9, _catchException9);
+  exceptionHandler (10, _catchException10);
+  exceptionHandler (11, _catchException11);
+  exceptionHandler (12, _catchException12);
+  /*
+   * Don't install GP exception handler
+   */
+  // exceptionHandler (13, _catchException13);
+  exceptionHandler (14, _catchException14);
+  exceptionHandler (16, _catchException16);
+}
+
+
+void
+gdb_init (void)
+{
+	if (!initialized) {
+		set_debug_traps ();
+	}
+    initialized = 1;
+}
+
+/* This function will generate a breakpoint exception.  It is used at the
+   beginning of a program to sync up with a debugger and can be used
+   otherwise as a quick means to stop program execution and "break" into
+   the debugger. */
+
+void
+breakpoint (void)
+{
+	if (!initialized) {
+		gdb_init ();
+	}
+
+	BREAKPOINT ();
+}
+
+static void
+gdb_bcopy (void *from, void *to, int length)
+{
+    char *fp = from;
+    char *tp = to;
+
+    while (length--) *tp++ = *fp++;
+}
+
+#define GDB_RLE_LEN_ENCODE_OFFSET 29
+#define GDB_RLE_MIN_ENCODE_LEN 4
+#define GDB_RLE_MAX_ENCODE_LEN (126 - GDB_RLE_LEN_ENCODE_OFFSET)
+
+static size_t
+gdb_run_length_encode (char *buffer, size_t length)
+{
+    if (length < GDB_RLE_MIN_ENCODE_LEN)
+        return length;
+
+    size_t newLen = length;
+    char *rleStart = &buffer[0];
+    int i;
+
+    for (i = 1; i < newLen; ++i) {
+        // rleBlockLen is the count of identical characters in the block.
+        // The encoding is <char>*<repeat> where repeat is the number of times to repeat
+        // <char>, therefore <repeat> is (rleBlockLen - 1)
+        char rleBlockLen = (&buffer[i] - rleStart) + 1;
+
+        if ((i == (newLen - 1)) || (buffer[i] != *rleStart) || ((rleBlockLen - 1) == GDB_RLE_MAX_ENCODE_LEN)) {
+            // reached an end condition
+            if (buffer[i] != *rleStart) {
+                // in this case buffer[i] is pointing at the first non encodable byte after the block
+                // so adjust rleBlockLen
+                rleBlockLen -= 1;
+            }
+
+            if (rleBlockLen < GDB_RLE_MIN_ENCODE_LEN) {
+                rleStart = &buffer[i];
+                continue;
+            }
+
+            char encodedLength = GDB_RLE_LEN_ENCODE_OFFSET + (rleBlockLen - 1);
+            if ((encodedLength == '+') || (encodedLength == '-') ||
+                (encodedLength == '$') || (encodedLength == '#')) {
+                // don't rle if encoded length is a reserved character
+                // TBD: could still encode by encoding a shorter block, but for now just leave it unencoded
+                rleStart = &buffer[i];
+                continue;
+            }
+
+            // only encode if the encoded length is not a reserved character
+            rleStart[1] = '*';
+            rleStart[2] = encodedLength;
+            if (&rleStart[3] < &buffer[newLen]) {
+                // if there is more data in the buffer, shift it left up to the end of the newly
+                // encoded data
+                size_t numBytesToRemove = rleBlockLen - 3;
+                char *firstByteToRemove = &rleStart[3];
+                char *firstByteToMove = firstByteToRemove + numBytesToRemove;
+                int numBytesToMove = newLen - (firstByteToRemove - buffer) - numBytesToRemove;
+                if (numBytesToMove > 0) {
+                    gdb_bcopy(firstByteToMove, firstByteToRemove, numBytesToMove);
+                }
+                newLen -= numBytesToRemove;
+                // adjust i so that processing will continue on the moved data
+                i = (&rleStart[3] - buffer);
+            }
+            rleStart = &buffer[i];
+        }
+    }
+
+    return newLen;
+}
+
+static char *
+gdb_strcpy (char *dest,const char *src)
+{
+  char *ptr = dest;
+
+  while (*src) *ptr++ = *src++;
+  *ptr = '\0';
+
+  return dest;
+}
+
+/* return 1 if str starts with prefix and 0 otherwise */
+static int
+gdb_str_starts_with (char *str, char *prefix)
+{
+  return (strncmp (str, prefix, strlen (prefix)) == 0);
+}
+
+
+// Default Implementations of OS Specific Functions
+
+#ifdef GDB_USE_SERIAL_PORT_IO
+
+// Implementations of I/O functions - putDebugChar() and getDebugChar()
+
+#define DEBUG_COM_PORT ((volatile unsigned short)0x3f8)
+#define COM_REG_THB   (DEBUG_COM_PORT + 0)
+#define COM_REG_RB    (DEBUG_COM_PORT + 0)
+
+#define COM_REG_LSR   (DEBUG_COM_PORT + 5)
+#define LSR_THB_EMPTY (1<<5)
+#define LSR_RB_READY  (1<<0)
+
+extern void phys_write8 (uint64_t port, uint8_t data);
+extern uint8_t phys_read8 (uint64_t port);
+
+void
+putDebugChar (char c)
+{
+  while(! (phys_read8(COM_REG_LSR) & LSR_THB_EMPTY) ) {
+      // wait for current byte to be written
+  }
+  phys_write8 (COM_REG_THB, c);
+}
+
+int
+getDebugChar (void)
+{
+  char c;
+  while (! (phys_read8 (COM_REG_LSR) & LSR_RB_READY) ) {
+      // wait for a character
+  }
+  c = phys_read8 (COM_REG_RB);
+  return c;
+}
+
+#endif /* GDB_USE_SERIAL_PORT_IO */
+
+#ifdef GDB_USE_STD_X64_EXCEPTION_HANDLER
+
+// Implementation of Interrupt Descriptor Table (IDT) function - exceptionHandler()
+
+typedef struct idtr_table_entry {
+	uint16_t handler_addr_offset1;
+	uint16_t segment_selector;
+	uint16_t flags;
+	uint16_t handler_addr_offset2;
+	uint32_t handler_addr_offset3;
+	uint32_t reserved;
+} __attribute__((packed)) idtr_table_entry_t;
+
+typedef struct idtr_table_addr {
+	uint16_t limit; // the offset (in bytes) of the last valid byte in the idtr
+	idtr_table_entry_t *addr; // the base address of the idtr in memory
+} __attribute__((packed)) idtr_table_addr_t;
+
+/* store the address of the interrupt descrtiptor table in memory */
+void store_idtr (idtr_table_addr_t *idtr_table_addr);
+
+asm(".text");
+asm("store_idtr:");
+  // %rdi is the parameter to this function
+  // a pointer to a 10-byte memory location
+  asm("sidt (%rdi)");
+  asm("retq");
+
+/* return the value of the 16-bit %cs (code segment) register */
+uint16_t get_cs_reg (void);
+
+asm(".text");
+asm("get_cs_reg:");
+  asm("xor %rax, %rax");
+  asm("mov %cs, %rax");
+  asm("retq");
+
+void
+exceptionHandler (int intr_vector, void *intr_handler)
+{
+  idtr_table_addr_t idtr_table_addr;
+  store_idtr (&idtr_table_addr);
+
+  idtr_table_entry_t *idtr_entry = &idtr_table_addr.addr[intr_vector];
+  idtr_entry->segment_selector = get_cs_reg ();
+
+  uint64_t intr_handler_addr = (uint64_t)intr_handler;
+  idtr_entry->handler_addr_offset1 = (uint16_t)intr_handler_addr;
+  idtr_entry->handler_addr_offset2 = (uint16_t)(intr_handler_addr >> 16);
+  idtr_entry->handler_addr_offset3 = (uint32_t)(intr_handler_addr >> 32);
+}
+
+#endif /* GDB_USE_STD_X64_EXCEPTION_HANDLER */
-- 
2.14.1.windows.1

