If an int 1587 call is made from an option rom, stay in bigreal mode.

Modify the int 1587 handler to check if the POST phase is still
running.  If it is, use bigreal mode segment limits so that the caller
remains in bigreal mode when the 1587 handler completes.  This helps
with SeaVGABIOS' use of "direct" framebuffer accesses (an option rom
may attempt to display text during its option rom execution which can
cause SeaVGABIOS to make the int 1587 calls).

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
diff --git a/src/misc.c b/src/misc.c
index 191e707..6712355 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -14,6 +14,15 @@
 
 #define PORT_MATH_CLEAR        0x00f0
 
+// Indicator if POST phase has been started (and if it has completed).
+int HaveRunPost VARFSEG;
+
+int
+in_post(void)
+{
+    return GET_GLOBAL(HaveRunPost) == 1;
+}
+
 
 /****************************************************************
  * Misc 16bit ISRs
diff --git a/src/post.c b/src/post.c
index 5fc1968..0d9e66f 100644
--- a/src/post.c
+++ b/src/post.c
@@ -185,6 +185,8 @@
     malloc_prepboot();
     memmap_prepboot();
 
+    HaveRunPost = 2;
+
     // Setup bios checksum.
     BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
 }
diff --git a/src/resume.c b/src/resume.c
index e2ceef1..1903174 100644
--- a/src/resume.c
+++ b/src/resume.c
@@ -17,9 +17,6 @@
 #include "string.h" // memset
 #include "util.h" // dma_setup
 
-// Indicator if POST phase has been run.
-int HaveRunPost VARFSEG;
-
 // Handler for post calls that look like a resume.
 void VISIBLE16
 handle_resume(void)
diff --git a/src/system.c b/src/system.c
index 5d10cc8..3cb2228 100644
--- a/src/system.c
+++ b/src/system.c
@@ -115,12 +115,13 @@
     SET_FARVAR(gdt_seg, gdt_far[1], GDT_DATA | GDT_LIMIT((6*sizeof(u64))-1)
                | GDT_BASE(loc));
     // Initialize CS descriptor
-    SET_FARVAR(gdt_seg, gdt_far[4], GDT_CODE | GDT_LIMIT(BUILD_BIOS_SIZE-1)
-               | GDT_BASE(BUILD_BIOS_ADDR));
+    u64 lim = GDT_LIMIT(0x0ffff);
+    if (in_post())
+        lim = GDT_GRANLIMIT(0xffffffff);
+    SET_FARVAR(gdt_seg, gdt_far[4], GDT_CODE | lim | GDT_BASE(BUILD_BIOS_ADDR));
     // Initialize SS descriptor
     loc = (u32)MAKE_FLATPTR(GET_SEG(SS), 0);
-    SET_FARVAR(gdt_seg, gdt_far[5], GDT_DATA | GDT_LIMIT(0x0ffff)
-               | GDT_BASE(loc));
+    SET_FARVAR(gdt_seg, gdt_far[5], GDT_DATA | lim | GDT_BASE(loc));
 
     u16 count = regs->cx;
     asm volatile(
diff --git a/src/util.h b/src/util.h
index 9557581..0cbea48 100644
--- a/src/util.h
+++ b/src/util.h
@@ -178,6 +178,7 @@
 // misc.c
 extern struct bios_config_table_s BIOS_CONFIG_TABLE __aligned(1);
 extern u8 BiosChecksum;
+int in_post(void);
 void mathcp_setup(void);
 
 // mouse.c