Skip to contentSkip to conversation

Centralizing understanding of far pointers

PATCH In review Javier Martín 3 participants 4 messages Jul 25, 2009 – Jul 29, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Centralizing understanding of far pointers

This patch modifies the machine-specific memory.h (currently, just the i386-specific file), adding a new type grub_machine_farptr and two functions to convert between such far pointers and normal C pointers.

The code performing the mapping between realmode and pmode addresses is simple, and thus is repeated in many source files like drivemap, vbe*, mmap, etc. However, as simple as it is, its ad-hoc application tends to be quite unwieldy, generating long code that is verges close to the tag of write-only code.

The i386 farptr type has been implemented as an union between the uint32 that was used until now (.raw_bits) and a structure separating the uint16 segment and offset parts for easy access and debug printing.

This post has three attachments: the patch to memory.h itself, a patch to the i386 mmap.c and its helper asm file, to show the impact of this patch (I've also taken the liberty of adding an offset macro just like in drivemap, and make the changed code more elegant in my opinion), and another patch doing the same for drivemap.

NOTE: this patch depends on the PTR_TO_UINT macro added by another patch still on discussion, [1].

[1] http://lists.gnu.org/archive/html/grub-devel/2009-07/msg00398.html

Signature · 1 line
-- Lazy, Oblivious, Recurrent Disaster -- Habbit
grub/i386/pc/memory.h +26 -0
Index: include/grub/i386/pc/memory.h
===================================================================
@@ -126,6 +126,32 @@
 }
 #endif
 
+typedef union
+{
+  struct
+  {
+    /* Given that i386 is little-endian, this order matches the in-memory
+       format of CPU realmode far pointers.  */
+    grub_uint16_t offset;
+    grub_uint16_t segment;
+  } __attribute__ ((packed));
+  grub_uint32_t raw_bits;
+} grub_machine_farptr;
+
+static inline void *
+grub_machine_real2linear (grub_machine_farptr ptr)
+{
+  return UINT_TO_PTR ((((grub_uint32_t) ptr.segment) << 4) + ptr.offset);
+}
+
+static inline grub_machine_farptr
+grub_machine_linear2real (void *addr)
+{
+  grub_machine_farptr ret = { { PTR_TO_UINT32 (addr) & 0x0000000F,
+			       (PTR_TO_UINT32 (addr) & 0x000FFFF0) >> 4 } };
+  return ret;
+}
+
 #endif
 
 #endif /* ! GRUB_MEMORY_MACHINE_HEADER */
i386/pc/mmap_helper.S +1 -2
Index: mmap/i386/pc/mmap_helper.S
===================================================================
@@ -48,9 +48,8 @@
 	popf
 	/* ljmp */
 	.byte	0xea
-VARIABLE (grub_machine_mmaphook_int15offset)
+VARIABLE (grub_machine_mmaphook_oldint15)
 	.word	0
-VARIABLE (grub_machine_mmaphook_int15segment)
 	.word	0
 
 e801:
i386/pc/mmap.c +27 -29

99 lines not on this page · raw message

i386/pc/drivemap.c +8 -9
Index: commands/i386/pc/drivemap.c
===================================================================
@@ -29,7 +29,7 @@
 
 
 /* Real mode IVT slot (seg:off far pointer) for interrupt 0x13.  */
-static grub_uint32_t *const int13slot = UINT_TO_PTR (4 * 0x13);
+static grub_machine_farptr *const int13slot = UINT_TO_PTR (4 * 0x13);
 
 /* Remember to update enum opt_idxs accordingly.  */
 static const struct grub_arg_option options[] = {
@@ -48,7 +48,7 @@
 };
 
 /* Realmode far ptr (2 * 16b) to the previous INT13h handler.  */
-extern grub_uint32_t grub_drivemap_oldhandler;
+extern grub_machine_farptr grub_drivemap_oldhandler;
 
 /* The type "void" is used for imported assembly labels, takes no storage and
    serves just to take the address with &label.  */
@@ -295,8 +295,7 @@
   /* Save the pointer to the old handler.  */
   grub_drivemap_oldhandler = *int13slot;
   grub_dprintf ("drivemap", "Original int13 handler: %04x:%04x\n",
-		(grub_drivemap_oldhandler >> 16) & 0x0ffff,
-		grub_drivemap_oldhandler & 0x0ffff);
+		grub_drivemap_oldhandler.segment, grub_drivemap_oldhandler.offset);
 
   /* Find a rmode-segment-aligned zone in conventional memory big
      enough to hold the handler and its data.  */
@@ -335,9 +334,9 @@
   grub_dprintf ("drivemap", "\t#%d: 0x00 <- 0x00 (end)\n", i);
 
   /* Install our function as the int13h handler in the IVT.  */
-  *int13slot = ((grub_uint32_t) handler_base) << 12;	/* Segment address.  */
+  *int13slot = grub_machine_linear2real(handler_base);
   grub_dprintf ("drivemap", "New int13 handler: %04x:%04x\n",
-		(*int13slot >> 16) & 0x0ffff, *int13slot & 0x0ffff);
+		int13slot->segment, int13slot->offset);
 
   return GRUB_ERR_NONE;
 }
@@ -345,14 +344,14 @@
 static grub_err_t
 uninstall_int13_handler (void)
 {
-  if (! grub_drivemap_oldhandler)
+  if (! grub_drivemap_oldhandler.raw_bits)
     return GRUB_ERR_NONE;
 
   *int13slot = grub_drivemap_oldhandler;
   grub_mmap_free_and_unregister (drivemap_mmap);
-  grub_drivemap_oldhandler = 0;
+  grub_drivemap_oldhandler.raw_bits = 0;
   grub_dprintf ("drivemap", "Restored int13 handler: %04x:%04x\n",
-		(*int13slot >> 16) & 0x0ffff, *int13slot & 0x0ffff);
+		int13slot->segment, int13slot->offset);
 
   return GRUB_ERR_NONE;
 }
#1 lore raw
Quoted 14 lines · Javier Martín

This patch modifies the machine-specific memory.h (currently, just the i386-specific file), adding a new type grub_machine_farptr and two functions to convert between such far pointers and normal C pointers.

The code performing the mapping between realmode and pmode addresses is simple, and thus is repeated in many source files like drivemap, vbe*, mmap, etc. However, as simple as it is, its ad-hoc application tends to be quite unwieldy, generating long code that is verges close to the tag of write-only code.

The i386 farptr type has been implemented as an union between the uint32 that was used until now (.raw_bits) and a structure separating the uint16 segment and offset parts for easy access and debug printing.

Quoted 8 lines

This post has three attachments: the patch to memory.h itself, a patch to the i386 mmap.c and its helper asm file, to show the impact of this patch (I've also taken the liberty of adding an offset macro just like in drivemap, and make the changed code more elegant in my opinion), and another patch doing the same for drivemap.

NOTE: this patch depends on the PTR_TO_UINT macro added by another patch still on discussion, [1].

The idea seems interesting, and in general such cleanup is welcome. I have some comments, only on the first patch.

First of all, please don't call them far pointers. They're an i8086 legacy cruft, which have nothing to do with far or close really (although we seem to have some code that makes this reference already).

What you're doing is basically the same as real2pm() function we already had. It seems this function should move elsewhere so it can have the generic use you intended (it can be reimplemented as well, if that makes it cleaner).

grub/i386/pc/memory.h line 138

Quoted 10 lines
grub/i386/pc/memory.h +10 -0
+typedef union
+{
+  struct
+  {
+    /* Given that i386 is little-endian, this order matches the in-memory
+       format of CPU realmode far pointers.  */
+    grub_uint16_t offset;
+    grub_uint16_t segment;
+  } __attribute__ ((packed));
+  grub_uint32_t raw_bits;

Is there a usefulness in this `raw_bits' member? It doesn't have any real meaning, as it doesn't correspond to an actual address.

Quoted 1 line

+} grub_machine_farptr;

I admit this is a bit confusing, but the machine' namespace is for things specific to a given firmware. i8086 mode is part of the i386 architecture, so we'd put this in the cpu' namespace instead.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#2 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 3 lines · Robert Millan

First of all, please don't call them far pointers. They're an i8086 legacy cruft, which have nothing to do with far or close really (although we seem to have some code that makes this reference already).

So... how do we call them? I am an utter failure at making up sensible names.

Quoted 4 lines

What you're doing is basically the same as real2pm() function we already had. It seems this function should move elsewhere so it can have the generic use you intended (it can be reimplemented as well, if that makes it cleaner).

Indeed, that's what I thought when I saw that function in some vbe-related file. I remembered that I wrote similar code for drivemap, and then reasoned that mmap would used too, as it modified the IVT. Thus, we are uselessly duplicating code that is simple but at the same time error-prone.

The idea of this patch is to provide a way to handle the abstraction of a pointer that is somehow mangled by the architecture, but the "user" code doesn't need to actually know how. For example, the patched drivemap would just take whatever linear2real(ptr) gives and stores it into the IVT slot. Of course, this is not as "magical" as an ideal world would have it, because the code still has to know the IVT to place it there, but I think it's still a plus.

Quoted 5 lines
Quoted 5 lines
grub/i386/pc/memory.h +5 -0
+typedef union
+{
+  struct
+  {
+    /* Given that i386 is little-endian, this order matches the in-memory
Quoted 8 lines

grub/i386/pc/memory.h line 138

Quoted 5 lines
grub/i386/pc/memory.h +5 -0
+       format of CPU realmode far pointers.  */
+    grub_uint16_t offset;
+    grub_uint16_t segment;
+  } __attribute__ ((packed));
+  grub_uint32_t raw_bits;

Is there a usefulness in this `raw_bits' member? It doesn't have any real meaning, as it doesn't correspond to an actual address.

Mainly, the possibility of checking equality against a particular bit pattern without using another instance of the structure/union. Also, some code might want to still use the raw bits for some reason, instead of either the nearly-opaque handling described above or the more detailed view provided by the inner structure.

Quoted 6 lines
Quoted 2 lines

+} grub_machine_farptr;

I admit this is a bit confusing, but the machine' namespace is for things specific to a given firmware. i8086 mode is part of the i386 architecture, so we'd put this in the cpu' namespace instead.

OK then... That means the code will have to move from i386/pc/memory.h to i386/memory.h. And what about x86_64?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJKb5qBAAoJEKSl+Fbdeo72EfMP/0JVO7mO92Pyk9l5tiGP5IQ+ PbrLJBX1HtAemwMufW5wR+OoQjyb9LGf1JR0ff7o7Xkbtnc3iN7EI8RV4WAzuLp3 9tYpYZkIwLWa86GM8Oy8QWer58vNlHYNtH+Zmrv+341umtFZERRjWk6NwpNNgFtm uX8FoYGqHLIhkzLWvd5vM7V+8S9LJPjq2ws/F1NbS5Sd33wTz81eFz6TNmoALNuD KeIR9Vo6f1zN3lp0M9+CL1fzX7uP3e3dWJ+KmOlkJcEJgpsRJhz76Ux9Byq0Tby9 SLXQmEfBmSNk95HpXnn2AFuiH/o8SYyWExPAEbz//Ttym4ElGGfS+tE3d8b7auga Y2ReU8I9XlbMczIjvnh6MaLOwzGuu6mtPFvec5PHSK9sekJ2TWYCosXpUkMo58TS vXPou5VDAkRb6sbuMx8g7fGws5vgYuEYcIZAkW3ejcJhIkMSGODEYRsT2A8ZREzx Qs/B48Z9Tk4nVxdPrIfDg7eB4TtEpJak84uw/fV6/WclY5ipk/SGf0iyhDfuU7vp 6w+6Kbvny0dOyssMz8cN2MlL7DinAtx2Zzi0jVHBNl253Af/idPJAPw69LDFWroN EHjLHxxnnkF4MOiR7WzH49QRzLCuGmMfgnqKEdEW1JxNFqG5+zE0L3p1Sp/pITs7 q+2CU6+NR3veOdzg3yRh
=dKOS
-----END PGP SIGNATURE-----

#3 lore raw

2009/7/29 Javier Martín <[email protected]>:

Quoted 7 lines
Quoted 3 lines · Robert Millan

First of all, please don't call them far pointers.  They're an i8086 legacy cruft, which have nothing to do with far or close really (although we seem to have some code that makes this reference already).

So... how do we call them? I am an utter failure at making up sensible names.

grub_i386_realmode_ptr_t

Quoted 7 lines
Quoted 2 lines

Is there a usefulness in this `raw_bits' member?  It doesn't have any real meaning, as it doesn't correspond to an actual address.

Mainly, the possibility of checking equality against a particular bit pattern without using another instance of the structure/union. Also, some code might want to still use the raw bits for some reason, instead of either the nearly-opaque handling described above or the more detailed view provided by the inner structure.

raw_bits don't seem very useful and using them is error-prone. E.g. two pointers may reffer to same address even having different raw_bits. So I would prefer not to include them unless there are real examples when it's useful

Quoted 2 lines

OK then... That means the code will have to move from i386/pc/memory.h to i386/memory.h. And what about x86_64?

Put a memory.h in x86_64/memory.h which will include i386/memory.h

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git