All of lore.kernel.org
 help / color / mirror / Atom feed
* Remote debugging GRUB over a serial line
@ 2006-05-22  8:51 Lubomir Kundrak
  2006-05-22 14:00 ` Marco Gerards
  0 siblings, 1 reply; 4+ messages in thread
From: Lubomir Kundrak @ 2006-05-22  8:51 UTC (permalink / raw)
  To: grub-devel

Hi!

For those who are interested, here's gdb remote stub
for debugging over serial line.

http://netbsd.sk/~lkundrak/misc/grub2-gdb/grub2-gdb-stub.diff

This week I'll try to clean up the code, so don't
expect it to be particularly good yet.

I also added a paragraph on remote debugging to the howto
http://netbsd.sk/~lkundrak/misc/grub2-gdb/howto.html
 
-- 
  o	Lubomir Kundrak
 *O*	<lkundrak@skosi.org>, http://skosi.org/~lkundrak/
 (_)	SKOSI -- Slovak OpenSource Initiative



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Remote debugging GRUB over a serial line
  2006-05-22  8:51 Remote debugging GRUB over a serial line Lubomir Kundrak
@ 2006-05-22 14:00 ` Marco Gerards
  2006-05-23 10:45   ` Lubomir Kundrak
  0 siblings, 1 reply; 4+ messages in thread
From: Marco Gerards @ 2006-05-22 14:00 UTC (permalink / raw)
  To: The development of GRUB 2

Lubomir Kundrak <lkundrak@skosi.org> writes:

Hi Lubomir,

> For those who are interested, here's gdb remote stub
> for debugging over serial line.
>
> http://netbsd.sk/~lkundrak/misc/grub2-gdb/grub2-gdb-stub.diff
>
> This week I'll try to clean up the code, so don't
> expect it to be particularly good yet.

When it is finished, could you please send the patch to the list
(inline) and can you include a changelog entry?  That will make it
easier to get the patch applied.

Thanks,
Marco




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Remote debugging GRUB over a serial line
  2006-05-22 14:00 ` Marco Gerards
@ 2006-05-23 10:45   ` Lubomir Kundrak
  2006-05-25 15:23     ` Lubomir Kundrak
  0 siblings, 1 reply; 4+ messages in thread
From: Lubomir Kundrak @ 2006-05-23 10:45 UTC (permalink / raw)
  To: The development of GRUB 2

On Mon, May 22, 2006 at 04:00:01PM +0200, Marco Gerards wrote:
> From: Marco Gerards <mgerards@xs4all.nl>
> Date: Mon, 22 May 2006 16:00:01 +0200
> In-Reply-To: <20060522085146.GC70384@meta.morph.sk> (Lubomir Kundrak's message
> 	of "Mon, 22 May 2006 10:51:46 +0200")
> Message-ID: <87odxqma72.fsf@xs4all.nl>
> Subject: Re: Remote debugging GRUB over a serial line
> 
> Lubomir Kundrak <lkundrak@skosi.org> writes:
> 
> Hi Lubomir,
> 
> > For those who are interested, here's gdb remote stub
> > for debugging over serial line.
> >
> > http://netbsd.sk/~lkundrak/misc/grub2-gdb/grub2-gdb-stub.diff
> >
> > This week I'll try to clean up the code, so don't
> > expect it to be particularly good yet.
> 
> When it is finished, could you please send the patch to the list
> (inline) and can you include a changelog entry?  That will make it
> easier to get the patch applied.
> 
> Thanks,
> Marco

So okay, I have tidied the code up a little, trying to comply
with GNU coding standards & wrote a changelog entry. Here is it:

2006-05-23:  Lubomir Kundrak  <lkundrak@skosi.org>

	* conf/i386-pc.rmk: Add serial and gdb modules to the list of
	modules to be built.

	* term/i386/pc/serial.c (serial_hw_put): Let some useful serial
	driver functions be exported.
	(grub_serial_getkey): Likewise

	* kern/dl.c (grub_dl_resolve_symbols): Let the trapvec symbol be
	resolved correctly (instead of 0).

	* gdb/cstub.c: New file.
	* gdb/gdb.c:  New file.
	* gdb/i386/idt.c: New file.
	* gdb/i386/signal.c: New file.
	* gdb/i386/machdep.S: New file.
	* include/grub/gdb.h: New file.
	* include/grub/i386/gdb.h: New file.
	* include/grub/i386/pc/kernel.h: New file.
	* kern/i386/pc/startup.S: New file.

diff -EbwBruN grub2.orig/conf/i386-pc.rmk grub2/conf/i386-pc.rmk
--- grub2.orig/conf/i386-pc.rmk	2006-04-26 10:26:53.000000000 +0200
+++ grub2/conf/i386-pc.rmk	2006-05-23 00:01:43.000000000 +0200
@@ -116,7 +116,14 @@
 pkgdata_MODULES = _chain.mod _linux.mod linux.mod normal.mod \
 	_multiboot.mod chain.mod multiboot.mod reboot.mod halt.mod	\
 	vbe.mod vbetest.mod vbeinfo.mod video.mod gfxterm.mod \
-	videotest.mod play.mod
+	videotest.mod play.mod serial.mod gdb.mod
+ 
+# For gdb.mod.
+gdb_mod_SOURCES = gdb/cstub.c gdb/gdb.c gdb/i386/idt.c \
+	gdb/i386/machdep.S gdb/i386/signal.c
+gdb_mod_CFLAGS = $(COMMON_CFLAGS)
+gdb_mod_LDFLAGS = $(COMMON_LDFLAGS)
+gdb_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 # For _chain.mod.
 _chain_mod_SOURCES = loader/i386/pc/chainloader.c
diff -EbwBruN grub2.orig/gdb/cstub.c grub2/gdb/cstub.c
--- grub2.orig/gdb/cstub.c	1970-01-01 01:00:00.000000000 +0100
+++ grub2/gdb/cstub.c	2006-05-22 23:59:24.000000000 +0200
@@ -0,0 +1,362 @@
+/* cstub.c - machine independent portion of remote GDB stub */
+/*
+ *  Copyright (C) 2006  Lubomir Kundrak
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <grub/misc.h>
+#include <grub/cpu/gdb.h>
+#include <grub/gdb.h>
+
+static const char hexchars[] = "0123456789abcdef";
+int grub_gdb_regs[GRUB_MACHINE_NR_REGS];
+
+#define GRUB_GDB_COMBUF_SIZE 400	/* At least sizeof(grub_gdb_regs)*2 are needed for
+					   register packets.  */
+static char grub_gdb_inbuf[GRUB_GDB_COMBUF_SIZE];
+static char grub_gdb_outbuf[GRUB_GDB_COMBUF_SIZE];
+
+int (*grub_gdb_getchar) ();
+void (*grub_gdb_putchar) (int);
+
+static 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);
+}
+
+/* Scan for the sequence $<data>#<checksum>.  */
+static char *
+grub_gdb_getpacket (void)
+{
+  char *buffer = &grub_gdb_inbuf[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 = grub_gdb_getchar ()) != '$');
+
+    retry:
+      checksum = 0;
+      xmitcsum = -1;
+      count = 0;
+
+      /* Now read until a # or end of buffer is found.  */
+      while (count < GRUB_GDB_COMBUF_SIZE)
+	{
+	  ch = grub_gdb_getchar ();
+	  if (ch == '$')
+	    goto retry;
+	  if (ch == '#')
+	    break;
+	  checksum += ch;
+	  buffer[count] = ch;
+	  count = count + 1;
+	}
+      buffer[count] = 0;
+
+      if (ch == '#')
+	{
+	  ch = grub_gdb_getchar ();
+	  xmitcsum = hex (ch) << 4;
+	  ch = grub_gdb_getchar ();
+	  xmitcsum += hex (ch);
+
+	  if (checksum != xmitcsum)
+	    {
+	      grub_dprintf ("gdb",
+			    "bad checksum.  My count = 0x%x, sent=0x%x. buf=%s\n",
+			    checksum, xmitcsum, buffer);
+	      grub_gdb_putchar ('-');	/* Failed checksum.  */
+	    }
+	  else
+	    {
+	      grub_gdb_putchar ('+');	/* Successful transfer.  */
+
+	      /* If a sequence char is present, reply the sequence ID.  */
+	      if (buffer[2] == ':')
+		{
+		  grub_gdb_putchar (buffer[0]);
+		  grub_gdb_putchar (buffer[1]);
+
+		  return &buffer[3];
+		}
+	      return &buffer[0];
+	    }
+	}
+    }
+}
+
+/* Send the packet in buffer.  */
+static void
+grub_gdb_putpacket (char *buffer)
+{
+  unsigned char checksum;
+  int count;
+  char ch;
+
+  /* $<packet info>#<checksum>.  */
+  do
+    {
+      grub_gdb_putchar ('$');
+      checksum = 0;
+      count = 0;
+
+      while ((ch = buffer[count]))
+	{
+	  grub_gdb_putchar (ch);
+	  checksum += ch;
+	  count += 1;
+	}
+
+      grub_gdb_putchar ('#');
+      grub_gdb_putchar (hexchars[checksum >> 4]);
+      grub_gdb_putchar (hexchars[checksum % 16]);
+    }
+  while (grub_gdb_getchar () != '+');
+}
+
+/* Convert the memory pointed to by mem into hex, placing result in buf.
+   Return a pointer to the last char put in buf (NULL).  */
+static char *
+grub_gdb_mem2hex (char *mem, char *buf, int count)
+{
+  int i;
+  unsigned char ch;
+
+  for (i = 0; i < count; i++)
+    {
+      ch = *mem++;
+      *buf++ = hexchars[ch >> 4];
+      *buf++ = hexchars[ch % 16];
+    }
+  *buf = 0;
+  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.  */
+static char *
+grub_gdb_hex2mem (char *buf, char *mem, int count)
+{
+  int i;
+  unsigned char ch;
+
+  for (i = 0; i < count; i++)
+    {
+      ch = hex (*buf++) << 4;
+      ch = ch + hex (*buf++);
+      *mem++ = ch;
+    }
+  return (mem);
+}
+
+/* Convert hex characters to int and return the number of characters
+   processed.  */
+static int
+grub_gdb_hex2int (char **ptr, int *int_value)
+{
+  int num_chars = 0;
+  int hex_value;
+
+  *int_value = 0;
+
+  while (**ptr)
+    {
+      hex_value = hex (**ptr);
+      if (hex_value >= 0)
+	{
+	  *int_value = (*int_value << 4) | hex_value;
+	  num_chars++;
+	}
+      else
+	break;
+
+      (*ptr)++;
+    }
+
+  return (num_chars);
+}
+
+/* This function does all command procesing for interfacing to gdb.  */
+void
+grub_gdb_trap (int trap_no)
+{
+  int sig_no;
+  int stepping;
+  int addr;
+  int length;
+  char *ptr;
+  int newPC;
+
+  sig_no = grub_gdb_trap2sig (trap_no);
+
+  ptr = grub_gdb_outbuf;
+
+  /* Reply to host that an exception has occurred.  */
+
+  *ptr++ = 'T';	/* Notify gdb with signo, PC, FP and SP.  */
+
+  *ptr++ = hexchars[sig_no >> 4];
+  *ptr++ = hexchars[sig_no & 0xf];
+
+  /* Stack pointer.  */
+  *ptr++ = hexchars[SP];
+  *ptr++ = ':';
+  ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[ESP], ptr, 4);
+  *ptr++ = ';';
+
+  /* Frame pointer.  */
+  *ptr++ = hexchars[FP];
+  *ptr++ = ':';
+  ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[EBP], ptr, 4);
+  *ptr++ = ';';
+
+  /* Program counter.  */
+  *ptr++ = hexchars[PC];
+  *ptr++ = ':';
+  ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[PC], ptr, 4);
+  *ptr++ = ';';
+
+  *ptr = '\0';
+
+  grub_gdb_putpacket (grub_gdb_outbuf);
+
+  stepping = 0;
+
+  while (1 == 1)
+    {
+      grub_gdb_outbuf[0] = 0;
+      ptr = grub_gdb_getpacket ();
+
+      switch (*ptr++)
+	{
+	case '?':
+	  grub_gdb_outbuf[0] = 'S';
+	  grub_gdb_outbuf[1] = hexchars[sig_no >> 4];
+	  grub_gdb_outbuf[2] = hexchars[sig_no % 16];
+	  grub_gdb_outbuf[3] = 0;
+	  break;
+
+	/* Return values of the CPU registers.  */
+	case 'g':
+	  grub_gdb_mem2hex ((char *) grub_gdb_regs, grub_gdb_outbuf,
+			    sizeof (grub_gdb_regs));
+	  break;
+
+	/* Set values of the CPU registers -- return OK.  */
+	case 'G':
+	  grub_gdb_hex2mem (ptr, (char *) grub_gdb_regs,
+			    sizeof (grub_gdb_regs));
+	  grub_strcpy (grub_gdb_outbuf, "OK");
+	  break;
+
+	/* Set the value of a single CPU register -- return OK.  */
+	case 'P':
+	  {
+	    int regno;
+
+	    if (grub_gdb_hex2int (&ptr, &regno) && *ptr++ == '=')
+	      if (regno >= 0 && regno < GRUB_MACHINE_NR_REGS)
+		{
+		  grub_gdb_hex2mem (ptr, (char *) &grub_gdb_regs[regno], 4);
+		  grub_strcpy (grub_gdb_outbuf, "OK");
+		  break;
+		}
+	    grub_strcpy (grub_gdb_outbuf, "E01");
+	    break;
+	  }
+
+	/* mAA..AA,LLLL: Read LLLL bytes at address AA..AA.  */
+	case 'm':
+	  /* Try to read %x,%x.  Set ptr = 0 if successful.  */
+	  if (grub_gdb_hex2int (&ptr, &addr))
+	    if (*(ptr++) == ',')
+	      if (grub_gdb_hex2int (&ptr, &length))
+		{
+		  ptr = 0;
+		  grub_gdb_mem2hex ((char *) addr, grub_gdb_outbuf, length);
+		}
+	  if (ptr)
+	    {
+	      grub_strcpy (grub_gdb_outbuf, "E01");
+	    }
+	  break;
+
+	/* MAA..AA,LLLL: Write LLLL bytes at address AA.AA -- return OK.  */
+	case 'M':
+	  /* Try to read %x,%x.  Set ptr = 0 if successful.  */
+	  if (grub_gdb_hex2int (&ptr, &addr))
+	    if (*(ptr++) == ',')
+	      if (grub_gdb_hex2int (&ptr, &length))
+		if (*(ptr++) == ':')
+		  {
+		    grub_gdb_hex2mem (ptr, (char *) addr, length);
+		    grub_strcpy (grub_gdb_outbuf, "OK");
+		    ptr = 0;
+		  }
+	  if (ptr)
+	    {
+	      grub_strcpy (grub_gdb_outbuf, "E02");
+	    }
+	  break;
+
+	/* sAA..AA: Step one instruction from AA..AA(optional).  */
+	case 's':
+	  stepping = 1;
+
+	/* cAA..AA: Continue at address AA..AA(optional).  */
+	case 'c':
+	  /* try to read optional parameter, pc unchanged if no parm */
+	  if (grub_gdb_hex2int (&ptr, &addr))
+	    grub_gdb_regs[PC] = addr;
+
+	  newPC = grub_gdb_regs[PC];
+
+	  /* Clear the trace bit.  */
+	  grub_gdb_regs[PS] &= 0xfffffeff;
+
+	  /* Set the trace bit if we're stepping.  */
+	  if (stepping)
+	    grub_gdb_regs[PS] |= 0x100;
+
+	  return;
+	  break;
+
+	/* Kill the program.  */
+	case 'k':
+	  /* Do nothing.  */
+	  return;
+	  break;
+	}
+
+      /* Reply to the request.  */
+      grub_gdb_putpacket (grub_gdb_outbuf);
+    }
+}
+
diff -EbwBruN grub2.orig/gdb/gdb.c grub2/gdb/gdb.c
--- grub2.orig/gdb/gdb.c	1970-01-01 01:00:00.000000000 +0100
+++ grub2/gdb/gdb.c	2006-05-23 00:01:15.000000000 +0200
@@ -0,0 +1,61 @@
+/* gdb.c - gdb remote stub module */
+/*
+ *  Copyright (C) 2003  Free Software Foundation, Inc.
+ *  Copyright (C) 2006  Lubomir Kundrak
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <grub/types.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/normal.h>
+#include <grub/term.h>
+#include <grub/cpu/gdb.h>
+#include <grub/gdb.h>
+
+extern int grub_serial_getkey ();
+extern void serial_hw_put (int);
+
+static grub_err_t
+grub_cmd_break (struct grub_arg_list *state __attribute__ ((unused)),
+		int argc __attribute__ ((unused)),
+		char **args __attribute__ ((unused)))
+{
+  grub_printf ("Now connect the remote debugger, please.\n");
+  grub_gdb_breakpoint ();
+  return 0;
+}
+
+GRUB_MOD_INIT (gdb)
+{
+  (void) mod;			/* To stop warning.  */
+
+  grub_gdb_getchar = &grub_serial_getkey;
+  grub_gdb_putchar = &serial_hw_put;
+
+  grub_gdb_idtinit ();
+  grub_register_command ("break", grub_cmd_break, GRUB_COMMAND_FLAG_BOTH,
+			 "break", "Break into debugger", 0);
+}
+
+GRUB_MOD_FINI (gdb)
+{
+  grub_unregister_command ("break");
+  /* FIXME: restore old IDTR.  */
+}
+
diff -EbwBruN grub2.orig/gdb/i386/idt.c grub2/gdb/i386/idt.c
--- grub2.orig/gdb/i386/idt.c	1970-01-01 01:00:00.000000000 +0100
+++ grub2/gdb/i386/idt.c	2006-05-23 07:34:50.000000000 +0200
@@ -0,0 +1,61 @@
+/* idt.c - routines for constructing IDT fot the GDB stub */
+/*
+ *  Copyright (C) 2006  Lubomir Kundrak
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <grub/machine/memory.h>
+#include <grub/misc.h>
+#include <grub/cpu/gdb.h>
+#include <grub/gdb.h>
+
+struct gate grub_gdb_idt[GRUB_GDB_LAST_TRAP + 1];
+
+/* Sets up a gate descriptor in the IDT table.  */
+static void
+grub_idt_gate (struct gate *gate, void (*offset) (), int selector,
+               int type, int dpl)
+{
+  gate->offset_lo = (int) offset & 0xffff;
+  gate->selector = selector & 0xffff;
+  gate->xx = 0;
+  gate->type = type & 0x1f;
+  gate->dpl = dpl & 0x3;
+  gate->present = 1;
+  gate->offset_hi = ((int) offset >> 16) & 0xffff;
+}
+
+/* Set up interrupt and trap handler descriptors in IDT.  */
+void
+grub_gdb_idtinit ()
+{
+  int i;
+  struct region r;
+
+  for (i = 0; i <= GRUB_GDB_LAST_TRAP; i++)
+    {
+      grub_idt_gate (&grub_gdb_idt[i],
+		     grub_gdb_trapvec[i],
+		     GRUB_MEMORY_MACHINE_PROT_MODE_CSEG,
+                     GRUB_CPU_TRAP_GATE, 0);
+    }
+
+  r.base = (int) grub_gdb_idt;
+  r.limit = sizeof (grub_gdb_idt) - 1;
+
+  grub_idt_load (&r);
+}
+
diff -EbwBruN grub2.orig/gdb/i386/machdep.S grub2/gdb/i386/machdep.S
--- grub2.orig/gdb/i386/machdep.S	1970-01-01 01:00:00.000000000 +0100
+++ grub2/gdb/i386/machdep.S	2006-05-22 23:25:24.000000000 +0200
@@ -0,0 +1,193 @@
+/* machdep.S - machine dependent assembly routines for the GDB stub */
+/*
+ *  Copyright (C) 2006  Lubomir Kundrak
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#define ASM	1
+#include <grub/cpu/gdb.h>
+
+#define EC_PRESENT	1
+#define EC_ABSENT	0
+
+#define GRUB_GDB_STACKSIZE	40000
+
+/*
+ * The .data index for the address vector.
+ */
+
+#define	VECTOR		1
+
+.globl grub_gdb_trap
+.globl grub_gdb_regs
+
+.data
+.globl grub_gdb_stack
+.space GRUB_GDB_STACKSIZE
+grub_gdb_stack:
+
+/*
+ * Supplemental macros for register saving/restoration
+ * on exception handler entry/leave.
+ */
+
+.macro save32 reg ndx
+	movl \reg, grub_gdb_regs+(\ndx * 4)
+.endm
+
+.macro save16 reg ndx
+	mov $0, %eax
+	movw \reg, grub_gdb_regs+(\ndx * 4)
+	movw %ax, grub_gdb_regs+(\ndx * 4 + 2)
+	movl grub_gdb_regs+(EAX * 4), %eax
+.endm
+
+.macro load32 ndx reg
+	movl grub_gdb_regs+(\ndx * 4), \reg
+.endm
+
+.macro load16 ndx reg
+	movw grub_gdb_regs+(\ndx * 4), \reg
+.endm
+
+.macro save_context
+	save32 %eax EAX
+	save32 %ecx ECX
+	save32 %edx EDX
+	save32 %ebx EBX
+	save32 %ebp EBP
+	save32 %esi ESI
+	save32 %edi EDI
+
+	popl %ebx
+	save32 %ebx EIP
+	popl %ebx
+	save32 %ebx CS
+	popl %ebx
+	save32 %ebx EFLAGS
+
+	save32 %esp ESP
+
+	save16 %ds DS
+	save16 %es ES
+	save16 %fs FS
+	save16 %gs GS
+	save16 %ss SS
+.endm
+
+.macro load_context
+	load16 SS %ss
+	load32 ESP %esp
+
+	load32 EBP %ebp
+	load32 ESI %esi
+	load32 EDI %edi
+
+	load16 DS %ds
+	load16 ES %es
+	load16 FS %fs
+	load16 GS %gs
+
+	load32 EFLAGS %eax
+	pushl %eax
+	load32 CS %eax
+	pushl %eax
+	load32 EIP %eax
+	pushl %eax
+
+	load32 EBX %ebx
+	load32 EDX %edx
+	load32 ECX %ecx
+	load32 EAX %eax
+.endm
+
+/*
+ * This macro creates handlers for a given range of exception numbers
+ * and adds their addresses to the grub_gdb_trapvec array.
+ */
+
+.macro ent ec beg end=0
+
+	/*
+	 * Wrapper body itself.
+	 */
+
+	.text
+1:	
+	.if \ec
+		add $4,%esp
+	.endif
+
+	save_context
+	mov	$grub_gdb_stack, %esp
+	mov	$\beg, %eax	/* trap number */
+	call	grub_gdb_trap
+	load_context
+	iret
+
+	/*
+	 * Address entry in trapvec array.
+	 */
+
+	.data VECTOR
+	.long 1b
+
+	/*
+	 * Next... (recursion).
+	 */
+
+	.if \end-\beg > 0
+		ent \ec "(\beg+1)" \end
+	.endif
+.endm
+
+/*
+ * Here does the actual construction of the address array and handlers
+ * take place.
+ */
+
+.data VECTOR
+.globl grub_gdb_trapvec
+grub_gdb_trapvec:
+	ent EC_ABSENT	0 7
+	ent EC_PRESENT	8
+	ent EC_ABSENT	9
+	ent EC_PRESENT	10 14
+	/*
+	 * You may have to split this further or as(1)
+	 * will complain about nesting being too deep.
+	 */
+	ent EC_ABSENT	15 GRUB_GDB_LAST_TRAP
+
+/*
+ * Random stuff
+ */
+
+.text
+.globl grub_gdb_breakpoint
+grub_gdb_breakpoint:
+	int $3
+	ret
+
+.globl grub_idt_load
+grub_idt_load:
+        lidt    (%eax)
+        ret
+
+.end
+
+OMG, Bunnies!
+
diff -EbwBruN grub2.orig/gdb/i386/signal.c grub2/gdb/i386/signal.c
--- grub2.orig/gdb/i386/signal.c	1970-01-01 01:00:00.000000000 +0100
+++ grub2/gdb/i386/signal.c	2006-05-23 07:36:13.000000000 +0200
@@ -0,0 +1,53 @@
+/* idt.c - routines for constructing IDT fot the GDB stub */
+/*
+ *  Copyright (C) 2006  Lubomir Kundrak
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <grub/machine/memory.h>
+#include <grub/misc.h>
+#include <grub/cpu/gdb.h>
+#include <grub/gdb.h>
+
+/* Converting CPU trap number to UNIX signal number as
+   described in System V ABI i386 Processor Supplement, 3-25.  */
+int
+grub_gdb_trap2sig (int trap_no)
+{
+  int signals[] = {
+    SIGFPE,			/* 0:   Divide error fault              */
+    SIGTRAP,			/* 1:   Single step trap fault          */
+    SIGABRT,			/* 2:   # Nonmaskable interrupt         */
+    SIGTRAP,			/* 3:   Breakpoint trap                 */
+    SIGSEGV,			/* 4:   Overflow trap                   */
+    SIGSEGV,			/* 5:   Bounds check fault              */
+    SIGILL,			/* 6:   Invalid opcode fault            */
+    SIGFPE,			/* 7:   No coprocessor fault            */
+    SIGABRT,			/* 8:   # Double fault abort            */
+    SIGSEGV,			/* 9:   Coprocessor overrun abort       */
+    SIGSEGV,			/* 10:  Invalid TSS fault               */
+    SIGSEGV,			/* 11:  Segment not present fault       */
+    SIGSEGV,			/* 12:  Stack exception fault           */
+    SIGSEGV,			/* 13:  General protection fault abort  */
+    SIGSEGV,			/* 14:  Page fault                      */
+    SIGABRT,			/* 15:  (reserved)                      */
+    SIGFPE,			/* 16:  Coprocessor error fault         */
+    SIGUSR1			/* other                                */
+  };
+
+  return signals[trap_no < 17 ? trap_no : 17];
+}
+
diff -EbwBruN grub2.orig/include/grub/gdb.h grub2/include/grub/gdb.h
--- grub2.orig/include/grub/gdb.h	1970-01-01 01:00:00.000000000 +0100
+++ grub2/include/grub/gdb.h	2006-05-23 00:00:15.000000000 +0200
@@ -0,0 +1,38 @@
+/* gdb.h - Various definitions for the remote GDB stub */
+/*
+ *  Copyright (C) 2006  Lubomir Kundrak
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef GRUB_GDB_HEADER
+#define GRUB_GDB_HEADER		1
+
+#define SIGFPE		8
+#define SIGTRAP		5
+#define SIGABRT		6
+#define SIGSEGV		11
+#define SIGILL		4
+#define SIGUSR1		30
+/* We probably don't need other ones.  */
+
+extern int (*grub_gdb_getchar) ();
+extern void (*grub_gdb_putchar) (int);
+
+void grub_gdb_breakpoint ();
+int grub_gdb_trap2sig (int);
+
+#endif /* ! GRUB_GDB_HEADER */
+
diff -EbwBruN grub2.orig/include/grub/i386/gdb.h grub2/include/grub/i386/gdb.h
--- grub2.orig/include/grub/i386/gdb.h	1970-01-01 01:00:00.000000000 +0100
+++ grub2/include/grub/i386/gdb.h	2006-05-23 07:34:06.000000000 +0200
@@ -0,0 +1,80 @@
+/* i386/gdb.h - i386 specific definitions for the remote GDB stub */
+/*
+ *  Copyright (C) 2006	Lubomir Kundrak
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef GRUB_GDB_CPU_HEADER
+#define GRUB_GDB_CPU_HEADER	1
+
+#define GRUB_GDB_LAST_TRAP	31
+/* You may have to edit the bottom of machdep.S when adjusting
+   GRUB_GDB_LAST_TRAP.	*/
+#define GRUB_MACHINE_NR_REGS	16
+
+#define EAX	0
+#define ECX	1
+#define EDX	2
+#define EBX	3
+#define ESP	4
+#define EBP	5
+#define ESI	6
+#define EDI	7
+#define EIP	8
+#define EFLAGS	9
+#define CS	10
+#define SS	11
+#define DS	12
+#define ES	13
+#define FS	14
+#define GS	15
+
+#define PC	EIP
+#define FP	EBP
+#define SP	ESP
+#define PS	EFLAGS
+
+#ifndef ASM
+
+#include <grub/gdb.h>
+
+#define GRUB_CPU_TRAP_GATE	15
+
+struct gate
+{
+  unsigned offset_lo:16;
+  unsigned selector:16;
+  unsigned xx:8;
+  unsigned type:5;	/* GRUB_CPU_TRAP_GATE */
+  unsigned dpl:2;
+  unsigned present:1;
+  unsigned offset_hi:16;
+} __attribute__ ((packed));
+
+struct region
+{
+  unsigned limit:16;
+  unsigned base:32;
+} __attribute__ ((packed));
+
+extern void (*grub_gdb_trapvec[]) ();
+extern void grub_gdb_breakpoint ();
+extern void grub_idt_load (struct region *);
+extern void grub_gdb_idtinit ();
+
+#endif /* ! ASM */
+#endif /* ! GRUB_GDB_CPU_HEADER */
+
diff -EbwBruN grub2.orig/include/grub/i386/pc/kernel.h grub2/include/grub/i386/pc/kernel.h
--- grub2.orig/include/grub/i386/pc/kernel.h	2006-04-26 10:09:07.000000000 +0200
+++ grub2/include/grub/i386/pc/kernel.h	2006-05-22 07:49:51.000000000 +0200
@@ -39,7 +39,7 @@
 #define GRUB_KERNEL_MACHINE_PREFIX		0x1c
 
 /* The size of the first region which won't be compressed.  */
-#define GRUB_KERNEL_MACHINE_RAW_SIZE		0x4A0
+#define GRUB_KERNEL_MACHINE_RAW_SIZE		0x4BC
 
 #ifndef ASM_FILE
 
diff -EbwBruN grub2.orig/kern/dl.c grub2/kern/dl.c
--- grub2.orig/kern/dl.c	2006-04-26 10:09:08.000000000 +0200
+++ grub2/kern/dl.c	2006-05-22 23:53:37.000000000 +0200
@@ -353,16 +353,18 @@
 	{
 	case STT_NOTYPE:
 	  /* Resolve a global symbol.  */
-	  if (sym->st_name != 0 && sym->st_shndx == 0)
+	  if (sym->st_name == 0)
+	    break;
+
+	  if (sym->st_shndx == 0) /* external */
 	    {
 	      sym->st_value = (Elf_Addr) grub_dl_resolve_symbol (name);
 	      if (! sym->st_value)
 		return grub_error (GRUB_ERR_BAD_MODULE,
 				   "the symbol `%s' not found", name);
-	    }
-	  else
-	    sym->st_value = 0;
 	  break;
+	    }
+          /* nonexternal, same as STT_OBJECT */
 
 	case STT_OBJECT:
 	  sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod,
diff -EbwBruN grub2.orig/kern/i386/pc/startup.S grub2/kern/i386/pc/startup.S
--- grub2.orig/kern/i386/pc/startup.S	2006-05-06 22:29:42.000000000 +0200
+++ grub2/kern/i386/pc/startup.S	2006-05-22 07:49:51.000000000 +0200
@@ -332,6 +332,14 @@
 	.word	0x27			/* limit */
 	.long	gdt			/* addr */
 
+/* IDT region descriptors */
+prot_idt:
+	.word	0xffff			/* limit */
+	.long	0x0			/* addr */
+
+real_idt:
+	.word	0xffff			/* limit */
+	.long	0x0			/* addr */
 	
 /*
  *  These next two routines, "real_to_prot" and "prot_to_real" are structured
@@ -381,6 +389,8 @@
 	/* zero %eax */
 	xorl	%eax, %eax
 
+	lidt prot_idt
+
 	/* return on the old (or initialized) stack! */
 	ret
 
@@ -389,6 +399,9 @@
 	/* just in case, set GDT */
 	lgdt	gdtdesc
 
+	sidt prot_idt
+	lidt real_idt
+
 	/* save the protected mode stack */
 	movl	%esp, %eax
 	movl	%eax, protstack
diff -EbwBruN grub2.orig/term/i386/pc/serial.c grub2/term/i386/pc/serial.c
--- grub2.orig/term/i386/pc/serial.c	2006-04-26 10:09:08.000000000 +0200
+++ grub2/term/i386/pc/serial.c	2006-05-22 07:49:51.000000000 +0200
@@ -93,7 +93,7 @@
 }
 
 /* Fetch a key.  */
-static int
+int
 serial_hw_fetch (void)
 {
   if (inb (serial_settings.port + UART_LSR) & UART_DATA_READY)
@@ -103,7 +103,7 @@
 }
 
 /* Put a chararacter.  */
-static void
+void
 serial_hw_put (const int c)
 {
   unsigned int timeout = 100000;
@@ -261,7 +261,7 @@
 }
 
 /* The serial version of getkey.  */
-static int
+int
 grub_serial_getkey (void)
 {
   int c;


-- 
  o	Lubomir Kundrak
 *O*	<lkundrak@skosi.org>, http://skosi.org/~lkundrak/
 (_)	SKOSI -- Slovak OpenSource Initiative



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Remote debugging GRUB over a serial line
  2006-05-23 10:45   ` Lubomir Kundrak
@ 2006-05-25 15:23     ` Lubomir Kundrak
  0 siblings, 0 replies; 4+ messages in thread
From: Lubomir Kundrak @ 2006-05-25 15:23 UTC (permalink / raw)
  To: The development of GRUB 2

Uff, I made a mistake -- of course following two files were
wrongly described as newly added in the changelog.
I am sorry for that:

	* include/grub/i386/pc/kernel.h: 
	(GRUB_KERNEL_MACHINE_RAW_SIZE): Portion of uncopressible code
	grew a bit.

	* kern/i386/pc/startup.S: (real_idt, prot_idt)
	(real_to_prot, prot_to_real): Preservation of protected mode
	IDT in real mode calls

-- 
  o	Lubomir Kundrak
 *O*	<lkundrak@skosi.org>, http://skosi.org/~lkundrak/
 (_)	SKOSI -- Slovak OpenSource Initiative



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-05-25 15:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-22  8:51 Remote debugging GRUB over a serial line Lubomir Kundrak
2006-05-22 14:00 ` Marco Gerards
2006-05-23 10:45   ` Lubomir Kundrak
2006-05-25 15:23     ` Lubomir Kundrak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.