Add helper macros for defining GDT descriptors.
authorKevin O'Connor <kevin@koconnor.net>
Mon, 10 Aug 2009 04:03:04 +0000 (00:03 -0400)
committerKevin O'Connor <kevin@koconnor.net>
Mon, 10 Aug 2009 04:03:04 +0000 (00:03 -0400)
src/misc.c
src/system.c
src/util.h

index 167dfe5e4c97e170618da270f73ca40fd026ac13..2d702a48198167cdf6cce9b4542f1239d267c317 100644 (file)
@@ -154,17 +154,17 @@ u64 rombios32_gdt[] VAR16_32 __aligned(8) = {
     // First entry can't be used.
     0x0000000000000000LL,
     // 32 bit flat code segment (SEG32_MODE32_CS)
-    0x00cf9b000000ffffLL,
+    GDT_LIMIT(0xfffff) | GDT_CODE | GDT_B | GDT_G,
     // 32 bit flat data segment (SEG32_MODE32_DS)
-    0x00cf93000000ffffLL,
+    GDT_LIMIT(0xfffff) | GDT_DATA | GDT_B | GDT_G,
     // 16 bit code segment base=0xf0000 limit=0xffff (SEG32_MODE16_CS)
-    0x00009b0f0000ffffLL,
+    GDT_LIMIT(0x0ffff) | GDT_CODE | GDT_BASE(0xf0000),
     // 16 bit data segment base=0x0 limit=0xffff (SEG32_MODE16_DS)
-    0x000093000000ffffLL,
+    GDT_LIMIT(0x0ffff) | GDT_DATA,
     // 16 bit code segment base=0 limit=0xffffffff (SEG32_MODE16BIG_CS)
-    0x008f9b000000ffffLL,
+    GDT_LIMIT(0xfffff) | GDT_CODE | GDT_G,
     // 16 bit data segment base=0 limit=0xffffffff (SEG32_MODE16BIG_DS)
-    0x008f93000000ffffLL,
+    GDT_LIMIT(0xfffff) | GDT_DATA | GDT_G,
 };
 
 // GDT descriptor
index 245e9cfb26a75579c0fe91753889d7f42464a495..8236cf0f47afb6b0a6a6f81b93f95a401d77b3aa 100644 (file)
@@ -101,47 +101,27 @@ handle_1587(struct bregs *regs)
     // 20..27   CS      zeros      filled in by BIOS
     // 28..2f   SS      zeros      filled in by BIOS
 
-    //es:si
-    //eeee0
-    //0ssss
-    //-----
-
 // check for access rights of source & dest here
 
     // Initialize GDT descriptor
-    SET_SEG(ES, regs->es);
-    u16 si = regs->si;
-    u16 base15_00 = (regs->es << 4) + si;
-    u16 base23_16 = regs->es >> 12;
-    if (base15_00 < (u16)(regs->es<<4))
-        base23_16++;
-    SET_VAR(ES, *(u16*)(si+0x08+0), 47);       // limit 15:00 = 6 * 8bytes/descriptor
-    SET_VAR(ES, *(u16*)(si+0x08+2), base15_00);// base 15:00
-    SET_VAR(ES, *(u8 *)(si+0x08+4), base23_16);// base 23:16
-    SET_VAR(ES, *(u8 *)(si+0x08+5), 0x93);     // access
-    SET_VAR(ES, *(u16*)(si+0x08+6), 0x0000);   // base 31:24/reserved/limit 19:16
-
+    u32 si = regs->si;
+    u64 *gdt_far = (void*)si;
+    u16 gdt_seg = regs->es;
+    u32 loc = (u32)MAKE_FLATPTR(gdt_seg, gdt_far);
+    SET_FARVAR(gdt_seg, gdt_far[1], GDT_DATA | GDT_LIMIT((6*sizeof(u64))-1)
+               | GDT_BASE(loc));
     // Initialize CS descriptor
-    SET_VAR(ES, *(u16*)(si+0x20+0), 0xffff);// limit 15:00 = normal 64K limit
-    SET_VAR(ES, *(u16*)(si+0x20+2), 0x0000);// base 15:00
-    SET_VAR(ES, *(u8 *)(si+0x20+4), 0x000f);// base 23:16
-    SET_VAR(ES, *(u8 *)(si+0x20+5), 0x9b);  // access
-    SET_VAR(ES, *(u16*)(si+0x20+6), 0x0000);// base 31:24/reserved/limit 19:16
-
+    SET_FARVAR(gdt_seg, gdt_far[4], GDT_CODE | GDT_LIMIT(0x0ffff)
+               | GDT_BASE(0xf0000));
     // Initialize SS descriptor
-    u16 ss = GET_SEG(SS);
-    base15_00 = ss << 4;
-    base23_16 = ss >> 12;
-    SET_VAR(ES, *(u16*)(si+0x28+0), 0xffff);   // limit 15:00 = normal 64K limit
-    SET_VAR(ES, *(u16*)(si+0x28+2), base15_00);// base 15:00
-    SET_VAR(ES, *(u8 *)(si+0x28+4), base23_16);// base 23:16
-    SET_VAR(ES, *(u8 *)(si+0x28+5), 0x93);     // access
-    SET_VAR(ES, *(u16*)(si+0x28+6), 0x0000);   // base 31:24/reserved/limit 19:16
+    loc = (u32)MAKE_FLATPTR(GET_SEG(SS), 0);
+    SET_FARVAR(gdt_seg, gdt_far[5], GDT_DATA | GDT_LIMIT(0x0ffff)
+               | GDT_BASE(loc));
 
     u16 count = regs->cx;
     asm volatile(
         // Load new descriptor tables
-        "lgdtw %%es:0x8(%%si)\n"
+        "lgdtw %%es:(1<<3)(%%si)\n"
         "lidtw %%cs:pmode_IDT_info\n"
 
         // Enable protected mode
@@ -150,13 +130,13 @@ handle_1587(struct bregs *regs)
         "movl %%eax, %%cr0\n"
 
         // far jump to flush CPU queue after transition to protected mode
-        "ljmpw $0x0020, $1f\n"
+        "ljmpw $(4<<3), $1f\n"
         "1:\n"
 
         // GDT points to valid descriptor table, now load DS, ES
-        "movw $0x10, %%ax\n" // 010 000 = 2nd descriptor in table, TI=GDT, RPL=00
+        "movw $(2<<3), %%ax\n" // 2nd descriptor in table, TI=GDT, RPL=00
         "movw %%ax, %%ds\n"
-        "movw $0x18, %%ax\n" // 011 000 = 3rd descriptor in table, TI=GDT, RPL=00
+        "movw $(3<<3), %%ax\n" // 3rd descriptor in table, TI=GDT, RPL=00
         "movw %%ax, %%es\n"
 
         // move CX words from DS:SI to ES:DI
index c2fc6caef74078a2cce82b5df899d481101e02a2..458f8b56758441912e3a4ad15a7e7ef49591c7bf 100644 (file)
@@ -88,6 +88,16 @@ static inline u32 __ffs(u32 word)
     return word;
 }
 
+// GDT bit manipulation
+#define GDT_BASE(v)  (((u64)((v) & 0xff000000) << 56)           \
+                      | ((u64)((v) & 0x00ffffff) << 16))
+#define GDT_LIMIT(v) (((u64)((v) & 0x000f0000) << 48)   \
+                      | ((u64)((v) & 0x0000ffff) << 0))
+#define GDT_CODE     (0x9bULL << 40) // Code segment - P,R,A bits also set
+#define GDT_DATA     (0x93ULL << 40) // Data segment - W,A bits also set
+#define GDT_B        (0x1ULL << 54)  // Big flag
+#define GDT_G        (0x1ULL << 55)  // Granularity flag
+
 #define call16_simpint(nr, peax, pflags) do {                           \
         ASSERT16();                                                     \
         asm volatile(                                                   \