Improve support for old 16bit resume handlers.

Detect a non-standard CMOS shutdown code during post and run a
    separate resume handler.
Set aside space in the EBDA for the resume handler stack.
Add support for several of the code supported in bochs bios.
diff --git a/src/asm-offsets.c b/src/asm-offsets.c
index 8f17c04..1e3a293 100644
--- a/src/asm-offsets.c
+++ b/src/asm-offsets.c
@@ -1,13 +1,15 @@
 // Generate assembler offsets.
 
-#include "gen-defs.h"
-#include "bregs.h"
+#include "gen-defs.h" // OFFSET
+#include "bregs.h" // struct bregs
+#include "biosvar.h" // struct bios_data_area_s
 
 /* workaround for a warning with -Wmissing-prototypes */
 void foo(void) VISIBLE16;
 
 void foo(void)
 {
+    COMMENT("BREGS");
     OFFSET(BREGS_es, bregs, es);
     OFFSET(BREGS_ds, bregs, ds);
     OFFSET(BREGS_eax, bregs, eax);
@@ -18,4 +20,12 @@
     OFFSET(BREGS_edi, bregs, edi);
     OFFSET(BREGS_flags, bregs, flags);
     OFFSET(BREGS_ip, bregs, ip);
+
+    COMMENT("BDA");
+    OFFSET(BDA_ebda_seg, bios_data_area_s, ebda_seg);
+
+    COMMENT("EBDA");
+    OFFSET(EBDA_resume_stack_top, extended_bios_data_area_s
+           , resume_stack[FIELD_SIZEOF(struct extended_bios_data_area_s
+                                       , resume_stack)]);
 }
diff --git a/src/biosvar.h b/src/biosvar.h
index 4571d5d..646bd51 100644
--- a/src/biosvar.h
+++ b/src/biosvar.h
@@ -60,7 +60,8 @@
     u16 crtc_address;
     u8 video_msr;
     u8 video_pal;
-    u32 jump_cs_ip;
+    u16 jump_ip;
+    u16 jump_cs;
     u8 other_6b;
     u32 timer_counter;
     // 40:70
@@ -272,6 +273,7 @@
     // 0x5d
     u8 other2[0xC4];
 
+    // 0x121 - Begin custom storage.
     u8 ps2ctr;
 
     // Physical memory available.
@@ -280,13 +282,16 @@
     struct pir_header *pir_loc;
 
     // ATA Driver data
-    struct ata_s   ata;
+    struct ata_s ata;
 
     // El Torito Emulation data
     struct cdemu_s cdemu;
 
     // Initial program load
     struct ipl_s ipl;
+
+    // Resume stack
+    u8 resume_stack[128] __aligned(8);
 } PACKED;
 
 #define EBDA_SIZE DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024)
diff --git a/src/cmos.h b/src/cmos.h
index 4087cfc..f663163 100644
--- a/src/cmos.h
+++ b/src/cmos.h
@@ -6,8 +6,6 @@
 #ifndef __CMOS_H
 #define __CMOS_H
 
-#include "ioport.h" // inb, outb
-
 #define CMOS_RTC_SECONDS         0x00
 #define CMOS_RTC_SECONDS_ALARM   0x01
 #define CMOS_RTC_MINUTES         0x02
@@ -50,6 +48,10 @@
 #define CFD_144MB    4
 #define CFD_288MB    5
 
+#ifndef __ASSEMBLY__
+
+#include "ioport.h" // inb, outb
+
 static inline u8
 inb_cmos(u8 reg)
 {
@@ -64,4 +66,6 @@
     outb(val, PORT_CMOS_DATA);
 }
 
+#endif // !__ASSEMBLY__
+
 #endif // cmos.h
diff --git a/src/kbd.c b/src/kbd.c
index e0f958a..7563733 100644
--- a/src/kbd.c
+++ b/src/kbd.c
@@ -338,9 +338,6 @@
     }
 }
 
-// 16bit reset vector declared in romlayout.S
-void reset_vector() __attribute__ ((noreturn));
-
 #define none 0
 #define MAX_SCAN_CODE 0x58
 
diff --git a/src/post.c b/src/post.c
index 3aec90b..3f86ead 100644
--- a/src/post.c
+++ b/src/post.c
@@ -219,52 +219,11 @@
     optionrom_setup();
 }
 
-// Reset DMA controller
-static void
-init_dma()
-{
-    // first reset the DMA controllers
-    outb(0, PORT_DMA1_MASTER_CLEAR);
-    outb(0, PORT_DMA2_MASTER_CLEAR);
-
-    // then initialize the DMA controllers
-    outb(0xc0, PORT_DMA2_MODE_REG);
-    outb(0x00, PORT_DMA2_MASK_REG);
-}
-
-// Check if the machine was setup with a special restart vector.
-static void
-check_restart_status()
-{
-    // Get and then clear CMOS shutdown status.
-    u8 status = inb_cmos(CMOS_RESET_CODE);
-    outb_cmos(0, CMOS_RESET_CODE);
-
-    if (status == 0x00 || status == 0x09 || status >= 0x0d)
-        // Normal post
-        return;
-
-    if (status != 0x05) {
-        BX_PANIC("Unimplemented shutdown status: %02x\n", status);
-        return;
-    }
-
-    // XXX - this is supposed to jump without changing any memory -
-    // but the stack has been altered by the time the code gets here.
-    eoi_pic2();
-    struct bregs br;
-    memset(&br, 0, sizeof(br));
-    br.cs = GET_BDA(jump_cs_ip) >> 16;
-    br.ip = GET_BDA(jump_cs_ip);
-    call16(&br);
-}
-
 // 32-bit entry point.
 void VISIBLE32
 _start()
 {
     init_dma();
-    check_restart_status();
 
     debug_serial_setup();
     dprintf(1, "Start bios\n");
diff --git a/src/resume.c b/src/resume.c
new file mode 100644
index 0000000..559fa97
--- /dev/null
+++ b/src/resume.c
@@ -0,0 +1,81 @@
+// Code for handling calls to "post" that are resume related.
+//
+// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include "util.h" // dprintf
+#include "ioport.h" // outb
+#include "pic.h" // eoi_pic2
+#include "biosvar.h" // struct bios_data_area_s
+
+// Reset DMA controller
+void
+init_dma()
+{
+    // first reset the DMA controllers
+    outb(0, PORT_DMA1_MASTER_CLEAR);
+    outb(0, PORT_DMA2_MASTER_CLEAR);
+
+    // then initialize the DMA controllers
+    outb(0xc0, PORT_DMA2_MODE_REG);
+    outb(0x00, PORT_DMA2_MASK_REG);
+}
+
+// Handler for post calls that look like a resume.
+void VISIBLE16
+handle_resume(u8 status)
+{
+    init_dma();
+
+    debug_serial_setup();
+    dprintf(1, "In resume (status=%d)\n", status);
+
+    switch (status) {
+    case 0x00:
+    case 0x09:
+    case 0x0d ... 0xff:
+        // Normal post - now that status has been cleared a reset will
+        // run regular boot code..
+        reset_vector();
+        break;
+
+    case 0x05:
+        // flush keyboard (issue EOI) and jump via 40h:0067h
+        eoi_pic2();
+        // NO BREAK
+    case 0x0a:
+        // resume execution by jump via 40h:0067h
+#define bda ((struct bios_data_area_s *)0)
+        asm volatile(
+            "movw %%ax, %%ds\n"
+            "ljmpw *%0\n"
+            : : "m"(bda->jump_ip), "a"(SEG_BDA)
+            );
+        break;
+
+    case 0x0b:
+        // resume execution via IRET via 40h:0067h
+        asm volatile(
+            "movw %%ax, %%ds\n"
+            "movw %0, %%sp\n"
+            "movw %1, %%ss\n"
+            "iretw\n"
+            : : "m"(bda->jump_ip), "m"(bda->jump_cs), "a"(SEG_BDA)
+            );
+        break;
+
+    case 0x0c:
+        // resume execution via RETF via 40h:0067h
+        asm volatile(
+            "movw %%ax, %%ds\n"
+            "movw %0, %%sp\n"
+            "movw %1, %%ss\n"
+            "lretw\n"
+            : : "m"(bda->jump_ip), "m"(bda->jump_cs), "a"(SEG_BDA)
+            );
+        break;
+    }
+
+    BX_PANIC("Unimplemented shutdown status: %02x\n", status);
+}
diff --git a/src/romlayout.S b/src/romlayout.S
index d731072..d0b463c 100644
--- a/src/romlayout.S
+++ b/src/romlayout.S
@@ -8,6 +8,7 @@
 #include "config.h" // CONFIG_*
 #include "ioport.h" // PORT_A20
 #include "bregs.h" // CR0_*
+#include "cmos.h" // CMOS_RESET_CODE
 #include "../out/asm-offsets.h" // BREGS_*
 
 
@@ -129,7 +130,14 @@
         andl $~(CR0_CD|CR0_NW), %eax
         movl %eax, %cr0
 
-        // init the stack pointer
+        // Check for restart indicator.
+        movl $CMOS_RESET_CODE, %eax
+        outb %al, $PORT_CMOS_INDEX
+        inb $PORT_CMOS_DATA, %al
+        cmpb $0x0, %al
+        jnz entry_resume
+
+        // Normal entry point
         RESET_STACK
 
         pushl $_code32__start
@@ -295,6 +303,28 @@
 
         retl
 
+// Entry point when a post call looks like a resume.
+entry_resume:
+        // Save old shutdown status.
+        movl %eax, %ebx
+
+        // Clear shutdown status register.
+        movl $CMOS_RESET_CODE, %eax
+        outb %al, $PORT_CMOS_INDEX
+        xorl %eax, %eax
+        outb %al, $PORT_CMOS_DATA
+
+        // Use a stack in EBDA
+        movw $SEG_BDA, %ax
+        movw %ax, %ds
+        movw BDA_ebda_seg, %ss
+        movl $EBDA_resume_stack_top, %esp
+
+        // Call handler.
+        movl %ebx, %eax
+        cld
+        cli
+        jmp handle_resume
 
 // APM trampolines
         .global apm16protected_entry
diff --git a/src/types.h b/src/types.h
index dfc3f16..8dd1a3b 100644
--- a/src/types.h
+++ b/src/types.h
@@ -42,6 +42,7 @@
 #define NULL ((void *)0)
 
 #define PACKED __attribute__((packed))
+#define __aligned(x) __attribute__((aligned(x)))
 
 #define barrier() __asm__ __volatile__("": : :"memory")
 
diff --git a/src/util.h b/src/util.h
index ef79332..5bbe9e8 100644
--- a/src/util.h
+++ b/src/util.h
@@ -173,4 +173,10 @@
 void vga_setup();
 void optionrom_setup();
 
+// resume.c
+void init_dma();
+
+// romlayout.S
+void reset_vector() __attribute__ ((noreturn));
+
 #endif // util.h