Introduce MODESEGMENT define; rename VISIBLE32 to VISIBLE32FLAT.
authorKevin O'Connor <kevin@koconnor.net>
Sun, 27 Dec 2009 04:32:57 +0000 (23:32 -0500)
committerKevin O'Connor <kevin@koconnor.net>
Sun, 27 Dec 2009 04:32:57 +0000 (23:32 -0500)
Prepare for support of segmented 32bit code.
Add new MODESEGMENT definition, and clarify existing 32bit mode defs.

15 files changed:
Makefile
src/biosvar.h
src/block.c
src/boot.c
src/farptr.h
src/output.c
src/pmm.c
src/post.c
src/resume.c
src/smm.c
src/smp.c
src/stacks.c
src/types.h
src/util.c
src/util.h

index 45f176e028b4931c3711cef47643e1d3ee9d25b5..26c94615a9fc3399570b7f629a7af439ff662bd1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,7 @@ SRCBOTH=misc.c pmm.c stacks.c output.c util.c block.c floppy.c ata.c mouse.c \
         pnpbios.c pirtable.c vgahooks.c ramdisk.c \
         usb.c usb-uhci.c usb-ohci.c usb-hid.c paravirt.c
 SRC16=$(SRCBOTH) system.c disk.c apm.c pcibios.c font.c
-SRC32=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \
+SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \
       acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \
       lzmadecode.c
 
@@ -34,8 +34,8 @@ COMMONCFLAGS += $(call cc-option,$(CC),-nopie,)
 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector,)
 COMMONCFLAGS += $(call cc-option,$(CC),-fno-stack-protector-all,)
 
-override CFLAGS = $(COMMONCFLAGS) -g -DMODE16=0
-CFLAGS16INC = $(COMMONCFLAGS) -DMODE16=1 -fno-defer-pop \
+override CFLAGS = $(COMMONCFLAGS) -g -DMODE16=0 -DMODESEGMENT=0
+CFLAGS16INC = $(COMMONCFLAGS) -DMODE16=1 -DMODESEGMENT=1 -fno-defer-pop \
               $(call cc-option,$(CC),-fno-jump-tables,-DMANUAL_NO_JUMP_TABLE) \
               $(call cc-option,$(CC),-fno-tree-switch-conversion,) \
               $(call cc-option,$(CC),--param large-stack-frame=4,)
@@ -112,36 +112,36 @@ $(OUT)asm-offsets.h: $(OUT)asm-offsets.s
 
 $(OUT)ccode.16.s: ; $(call whole-compile, $(CFLAGS16) -S, $(addprefix src/, $(SRC16)),$@)
 
-$(OUT)ccode32.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32)),$@)
+$(OUT)ccode32flat.o: ; $(call whole-compile, $(CFLAGS), $(addprefix src/, $(SRC32FLAT)),$@)
 
 $(OUT)code16.o: romlayout.S $(OUT)ccode.16.s $(OUT)asm-offsets.h
        @echo "  Compiling (16bit) $@"
        $(Q)$(CC) $(CFLAGS16INC) -c -D__ASSEMBLY__ $< -o $@
 
-$(OUT)romlayout16.lds $(OUT)romlayout32.lds $(OUT)code32.o: $(OUT)ccode32.o $(OUT)code16.o tools/layoutrom.py
+$(OUT)romlayout16.lds $(OUT)romlayout32flat.lds $(OUT)code32flat.o: $(OUT)ccode32flat.o $(OUT)code16.o tools/layoutrom.py
        @echo "  Building ld scripts (version \"$(VERSION)\")"
        $(Q)echo 'const char VERSION[] = "$(VERSION)";' > $(OUT)version.c
        $(Q)$(CC) $(CFLAGS) -c $(OUT)version.c -o $(OUT)version.o
-       $(Q)$(LD) -melf_i386 -r $(OUT)ccode32.o $(OUT)version.o -o $(OUT)code32.o
-       $(Q)$(OBJDUMP) -thr $(OUT)code32.o > $(OUT)code32.o.objdump
+       $(Q)$(LD) -melf_i386 -r $(OUT)ccode32flat.o $(OUT)version.o -o $(OUT)code32flat.o
+       $(Q)$(OBJDUMP) -thr $(OUT)code32flat.o > $(OUT)code32flat.o.objdump
        $(Q)$(OBJDUMP) -thr $(OUT)code16.o > $(OUT)code16.o.objdump
-       $(Q)./tools/layoutrom.py $(OUT)code16.o.objdump $(OUT)code32.o.objdump $(OUT)romlayout16.lds $(OUT)romlayout32.lds
+       $(Q)./tools/layoutrom.py $(OUT)code16.o.objdump $(OUT)code32flat.o.objdump $(OUT)romlayout16.lds $(OUT)romlayout32flat.lds
 
 
-$(OUT)rom16.o: $(OUT)code16.o $(OUT)rom32.o $(OUT)romlayout16.lds
+$(OUT)rom16.o: $(OUT)code16.o $(OUT)rom32flat.o $(OUT)romlayout16.lds
        @echo "  Linking (no relocs) $@"
        $(Q)$(LD) -r -T $(OUT)romlayout16.lds $< -o $@
 
-$(OUT)rom32.o: $(OUT)code32.o $(OUT)romlayout32.lds
+$(OUT)rom32flat.o: $(OUT)code32flat.o $(OUT)romlayout32flat.lds
        @echo "  Linking (no relocs) $@"
-       $(Q)$(LD) -r -T $(OUT)romlayout32.lds $< -o $@
+       $(Q)$(LD) -r -T $(OUT)romlayout32flat.lds $< -o $@
 
-$(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32.o $(OUT)rombios16.lds $(OUT)rombios.lds
+$(OUT)rom.o: $(OUT)rom16.o $(OUT)rom32flat.o $(OUT)rombios16.lds $(OUT)rombios.lds
        @echo "  Linking $@"
-       $(Q)$(LD) -T $(OUT)rombios16.lds $(OUT)rom16.o -R $(OUT)rom32.o -o $(OUT)rom16.reloc.o
+       $(Q)$(LD) -T $(OUT)rombios16.lds $(OUT)rom16.o -R $(OUT)rom32flat.o -o $(OUT)rom16.reloc.o
        $(Q)$(STRIP) $(OUT)rom16.reloc.o -o $(OUT)rom16.final.o
        $(Q)$(OBJCOPY) --adjust-vma 0xf0000 $(OUT)rom16.o $(OUT)rom16.moved.o
-       $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.final.o $(OUT)rom32.o -R $(OUT)rom16.moved.o -o $@
+       $(Q)$(LD) -T $(OUT)rombios.lds $(OUT)rom16.final.o $(OUT)rom32flat.o -R $(OUT)rom16.moved.o -o $@
 
 $(OUT)bios.bin.elf $(OUT)bios.bin: $(OUT)rom.o tools/checkrom.py
        @echo "  Prepping $@"
index aeebf0fc5761bda98a4dbd23859bc6adc536fcee..6a789e41a9e216eac664c65765565ec61dcd53b8 100644 (file)
@@ -238,7 +238,7 @@ static inline u16 get_ebda_seg() {
 static inline struct extended_bios_data_area_s *
 get_ebda_ptr()
 {
-    ASSERT32();
+    ASSERT32FLAT();
     return MAKE_FLATPTR(get_ebda_seg(), 0);
 }
 #define GET_EBDA2(eseg, var)                                            \
@@ -267,10 +267,10 @@ static inline u16 get_global_seg() {
 #define GET_GLOBAL(var)                         \
     GET_VAR(GLOBAL_SEGREG, (var))
 #define SET_GLOBAL(var, val) do {               \
-        ASSERT32();                             \
+        ASSERT32FLAT();                         \
         (var) = (val);                          \
     } while (0)
-#if MODE16
+#if MODESEGMENT
 #define ADJUST_GLOBAL_PTR(var) (var)
 #else
 #define ADJUST_GLOBAL_PTR(var) ((typeof(var))((void*)var - BUILD_BIOS_ADDR))
index 1fbfadabb16196181cc9dc0eaefd3dba05f3d111..3fb5e91da9ecb446d6c394f0920339b0d67e0da6 100644 (file)
@@ -283,7 +283,7 @@ map_floppy_drive(struct drive_s *drive_g)
 void
 describe_drive(struct drive_s *drive_g)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     u8 type = GET_GLOBAL(drive_g->type);
     switch (type) {
     case DTYPE_FLOPPY:
index 05de7157f6234a7f0b6f9f59c468621e1edb21f0..c427c00bd333882ed94d9194601add23b2a97970 100644 (file)
@@ -486,7 +486,7 @@ do_boot(u16 seq_nr)
 }
 
 // Boot Failure recovery: try the next device.
-void VISIBLE32
+void VISIBLE32FLAT
 handle_18()
 {
     debug_serial_setup();
@@ -498,7 +498,7 @@ handle_18()
 }
 
 // INT 19h Boot Load Service Entry Point
-void VISIBLE32
+void VISIBLE32FLAT
 handle_19()
 {
     debug_serial_setup();
index acc9d596e9a2da72b8b42015d3c10d38e8f967f8..3cc41706ec4c93a7290fea4ae0a4d81fe1d94fff 100644 (file)
@@ -122,9 +122,9 @@ extern void __force_link_error__unknown_type();
 #define MAKE_FLATPTR(seg,off) ((void*)(((u32)(seg)<<4)+(u32)(off)))
 
 
-#if MODE16 == 1
+#if MODESEGMENT == 1
 
-// Definitions when in 16 bit mode.
+// Definitions when using segmented mode.
 #define GET_FARVAR(seg, var) __GET_FARVAR((seg), (var))
 #define SET_FARVAR(seg, var, val) __SET_FARVAR((seg), (var), (val))
 #define GET_VAR(seg, var) __GET_VAR(seg, (var))
@@ -159,13 +159,9 @@ static inline void outsl_fl(u16 port, void *ptr_fl, u16 count) {
     outsl(port, (u32*)FLATPTR_TO_OFFSET(ptr_fl), count);
 }
 
-extern void __force_link_error__only_in_32bit() __attribute__ ((noreturn));
-#define ASSERT16() do { } while (0)
-#define ASSERT32() __force_link_error__only_in_32bit()
-
 #else
 
-// In 32-bit mode there is no need to mess with the segments.
+// In 32-bit flat mode there is no need to mess with the segments.
 #define GET_FARVAR(seg, var) \
     (*((typeof(&(var)))MAKE_FLATPTR((seg), &(var))))
 #define SET_FARVAR(seg, var, val) \
@@ -184,10 +180,6 @@ extern void __force_link_error__only_in_32bit() __attribute__ ((noreturn));
 #define outsw_fl(port, ptr_fl, count) outsw(port, ptr_fl, count)
 #define outsl_fl(port, ptr_fl, count) outsl(port, ptr_fl, count)
 
-extern void __force_link_error__only_in_16bit() __attribute__ ((noreturn));
-#define ASSERT16() __force_link_error__only_in_16bit()
-#define ASSERT32() do { } while (0)
-
 #endif
 
 // Definition for common 16bit segment/offset pointers.
index e49010da0a1fe2a5f4a403d98ca569f3c5728b14..3b0e0e7ccbfbe60be6b52d51a6b0bc431a10d682 100644 (file)
@@ -84,10 +84,10 @@ putc_debug(struct putcinfo *action, char c)
     debug_serial(c);
 }
 
-// In 16bit mode just need a dummy variable (putc_debug is always used
-// anyway), and in 32bit mode need a pointer to the 32bit instance of
-// putc_debug().
-#if MODE16
+// In segmented mode just need a dummy variable (putc_debug is always
+// used anyway), and in 32bit flat mode need a pointer to the 32bit
+// instance of putc_debug().
+#if MODESEGMENT
 static struct putcinfo debuginfo VAR16;
 #else
 static struct putcinfo debuginfo = { putc_debug };
@@ -132,8 +132,8 @@ static struct putcinfo screeninfo = { putc_screen };
 static void
 putc(struct putcinfo *action, char c)
 {
-    if (MODE16) {
-        // Only debugging output supported in 16bit mode.
+    if (MODESEGMENT) {
+        // Only debugging output supported in segmented mode.
         putc_debug(action, c);
         return;
     }
@@ -325,7 +325,7 @@ panic(const char *fmt, ...)
 void
 __dprintf(const char *fmt, ...)
 {
-    if (!MODE16 && CONFIG_THREADS && CONFIG_DEBUG_LEVEL >= DEBUG_thread
+    if (!MODESEGMENT && CONFIG_THREADS && CONFIG_DEBUG_LEVEL >= DEBUG_thread
         && *fmt != '\\' && *fmt != '/') {
         struct thread_info *cur = getCurThread();
         if (cur != &MainThread) {
@@ -347,7 +347,7 @@ __dprintf(const char *fmt, ...)
 void
 printf(const char *fmt, ...)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     va_list args;
     va_start(args, fmt);
     bvprintf(&screeninfo, fmt, args);
@@ -382,7 +382,7 @@ putc_str(struct putcinfo *info, char c)
 int
 snprintf(char *str, size_t size, const char *fmt, ...)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     if (!size)
         return 0;
     struct snprintfinfo sinfo = { { putc_str }, str, str + size };
index 48a882a394e9e49031d6331577bbecd9941dac72..a72167bb50c518930438c8834317095e0eb5ee01 100644 (file)
--- a/src/pmm.c
+++ b/src/pmm.c
@@ -11,7 +11,7 @@
 #include "biosvar.h" // GET_BDA
 
 
-#if MODE16
+#if MODESEGMENT
 // The 16bit pmm entry points runs in "big real" mode, and can
 // therefore read/write to the 32bit malloc variables.
 #define GET_PMMVAR(var) GET_FARVAR(0, (var))
@@ -26,11 +26,11 @@ struct zone_s {
     u32 top, bottom, cur;
 };
 
-struct zone_s ZoneLow VAR32VISIBLE, ZoneHigh VAR32VISIBLE;
-struct zone_s ZoneFSeg VAR32VISIBLE;
-struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
+struct zone_s ZoneLow VAR32FLATVISIBLE, ZoneHigh VAR32FLATVISIBLE;
+struct zone_s ZoneFSeg VAR32FLATVISIBLE;
+struct zone_s ZoneTmpLow VAR32FLATVISIBLE, ZoneTmpHigh VAR32FLATVISIBLE;
 
-struct zone_s *Zones[] VAR32VISIBLE = {
+struct zone_s *Zones[] VAR32FLATVISIBLE = {
     &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
 };
 
@@ -49,7 +49,7 @@ relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
         return -1;
 
     // Do copy
-    if (MODE16)
+    if (MODESEGMENT)
         memcpy_far(FLATPTR_TO_SEG(newebda)
                    , (void*)FLATPTR_TO_OFFSET(newebda)
                    , FLATPTR_TO_SEG(oldebda)
@@ -172,7 +172,7 @@ struct pmmalloc_s {
     struct pmmalloc_s *next;
 };
 
-struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
+struct pmmalloc_s *PMMAllocs VAR32FLATVISIBLE;
 
 // Allocate memory from the given zone and track it as a PMM allocation
 void *
@@ -279,7 +279,7 @@ pmm_find(u32 handle)
 void
 malloc_setup()
 {
-    ASSERT32();
+    ASSERT32FLAT();
     dprintf(3, "malloc setup\n");
 
     PMMAllocs = NULL;
index f087a27f952c56d6c5d239255c89ece6f1b70816..2f14aa73f07dfdae7b44904fecfd83af96e42d02 100644 (file)
@@ -234,7 +234,7 @@ post()
 }
 
 // 32-bit entry point.
-void VISIBLE32
+void VISIBLE32FLAT
 _start()
 {
     init_dma();
index 097bb6f38f7299745b1451e9e3be50ddf5886742..59bb90195427e01be1525aeb4d4a3f1813f61f69 100644 (file)
@@ -93,8 +93,8 @@ handle_resume(u8 status)
     panic("Unimplemented shutdown status: %02x\n", status);
 }
 
-#if MODE16==0
-void VISIBLE32
+#if MODESEGMENT == 0
+void VISIBLE32FLAT
 s3_resume()
 {
     if (!CONFIG_S3_RESUME)
index 31e56d8c285bac984ed6bcec65407ac88ff1a2c8..be2291bc7e8dc6c42c19d48d904d73acf9dc8360 100644 (file)
--- a/src/smm.c
+++ b/src/smm.c
@@ -11,7 +11,7 @@
 #include "ioport.h" // outb
 #include "pci_ids.h" // PCI_VENDOR_ID_INTEL
 
-ASM32(
+ASM32FLAT(
     ".global smm_relocation_start\n"
     ".global smm_relocation_end\n"
     ".global smm_code_start\n"
index 00cf64bd9dab840489527488c7c562525097875c..913d3a2b6b6e5b5681eef50538055f2a5296aac3 100644 (file)
--- a/src/smp.c
+++ b/src/smp.c
@@ -8,7 +8,6 @@
 #include "util.h" // dprintf
 #include "config.h" // CONFIG_*
 #include "cmos.h" // CMOS_BIOS_SMP_COUNT
-#include "farptr.h" // ASSERT32
 #include "paravirt.h"
 
 #define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
@@ -70,7 +69,7 @@ ASM16(
 void
 smp_probe(void)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     u32 eax, ebx, ecx, cpuid_features;
     cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
     if (! (cpuid_features & CPUID_APIC)) {
index 56d17a1ac8d396d0d321b4d6349f2ac48bdf8cbe..0ddb9a89f64f60809f6b0917b1788afac288138c 100644 (file)
@@ -168,7 +168,7 @@ switch_next(struct thread_info *cur)
 void
 yield()
 {
-    if (MODE16 || !CONFIG_THREADS) {
+    if (MODESEGMENT || !CONFIG_THREADS) {
         // Just directly check irqs.
         check_irqs();
         return;
@@ -198,7 +198,7 @@ __end_thread(struct thread_info *old)
 void
 run_thread(void (*func)(void*), void *data)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     if (! CONFIG_THREADS)
         goto fail;
     struct thread_info *thread;
@@ -241,7 +241,7 @@ fail:
 void
 wait_threads()
 {
-    ASSERT32();
+    ASSERT32FLAT();
     if (! CONFIG_THREADS)
         return;
     while (MainThread.next != &MainThread)
@@ -278,9 +278,9 @@ finish_preempt()
 }
 
 extern void yield_preempt();
-#if !MODE16
+#if MODESEGMENT == 0
 // Try to execute 32bit threads.
-void VISIBLE32
+void VISIBLE32FLAT
 yield_preempt()
 {
     PreemptCount++;
index 8b9fbb12724198290728a8af3786686b82bee93c..b9786e2598d5bd2717a9c1b4aaaf05f39c59ad20 100644 (file)
@@ -38,8 +38,8 @@ union u64_u32_u {
 #if MODE16 == 1
 // Notes a function as externally visible in the 16bit code chunk.
 # define VISIBLE16 __VISIBLE
-// Notes a function as externally visible in the 32bit code chunk.
-# define VISIBLE32
+// Notes a function as externally visible in the 32bit flat code chunk.
+# define VISIBLE32FLAT
 // Designate a variable as (only) visible to 16bit code.
 # define VAR16 __section(".data16." UNIQSEC)
 // Designate a variable as visible to 16bit, 32bit, and assembler code.
@@ -49,21 +49,28 @@ union u64_u32_u {
 // Designate a variable at a specific 16bit address
 # define VAR16FIXED(addr) __aligned(1) __VISIBLE __section(".fixedaddr." __stringify(addr))
 // Designate a 32bit variable also available in 16bit "big real" mode.
-# define VAR32VISIBLE __section(".discard.var32." UNIQSEC) __VISIBLE __weak
+# define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
 // Designate top-level assembler as 16bit only.
 # define ASM16(code) __ASM(code)
-// Designate top-level assembler as 32bit only.
-# define ASM32(code)
+// Designate top-level assembler as 32bit flat only.
+# define ASM32FLAT(code)
+// Compile time check for a given mode.
+extern void __force_link_error__only_in_32bit_flat() __attribute__ ((noreturn));
+#define ASSERT16() do { } while (0)
+#define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
 #else
 # define VISIBLE16
-# define VISIBLE32 __VISIBLE
+# define VISIBLE32FLAT __VISIBLE
 # define VAR16 __section(".discard.var16." UNIQSEC)
 # define VAR16VISIBLE VAR16 __VISIBLE __weak
 # define VAR16EXPORT VAR16VISIBLE
 # define VAR16FIXED(addr) VAR16VISIBLE
-# define VAR32VISIBLE __VISIBLE
+# define VAR32FLATVISIBLE __VISIBLE
 # define ASM16(code)
-# define ASM32(code) __ASM(code)
+# define ASM32FLAT(code) __ASM(code)
+extern void __force_link_error__only_in_16bit() __attribute__ ((noreturn));
+#define ASSERT16() __force_link_error__only_in_16bit()
+#define ASSERT32FLAT() do { } while (0)
 #endif
 
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
index f5ae0e1c3f056c6f18566be851fee89e261a8cdc..552f944bbcb8e130db71b2a0f7364c5482cb7af6 100644 (file)
@@ -18,7 +18,7 @@
 inline void
 call16(struct bregs *callregs)
 {
-    if (!MODE16 && getesp() > BUILD_STACK_ADDR)
+    if (!MODESEGMENT && getesp() > BUILD_STACK_ADDR)
         panic("call16 with invalid stack\n");
     asm volatile(
 #if MODE16 == 1
@@ -36,7 +36,7 @@ call16(struct bregs *callregs)
 inline void
 call16big(struct bregs *callregs)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     if (getesp() > BUILD_STACK_ADDR)
         panic("call16 with invalid stack\n");
     asm volatile(
@@ -49,7 +49,7 @@ call16big(struct bregs *callregs)
 inline void
 __call16_int(struct bregs *callregs, u16 offset)
 {
-    if (MODE16)
+    if (MODESEGMENT)
         callregs->code.seg = GET_SEG(CS);
     else
         callregs->code.seg = SEG_BIOS;
@@ -197,7 +197,7 @@ memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
 void *
 #undef memcpy
 memcpy(void *d1, const void *s1, size_t len)
-#if MODE16 == 0
+#if MODESEGMENT == 0
 #define memcpy __builtin_memcpy
 #endif
 {
index 24e39d1b47ba2391fa91c79e0fac3fefc957484a..7d95cbede40735438ea0dbbdf31aa6ef8bbee318 100644 (file)
@@ -176,7 +176,7 @@ void *memset(void *s, int c, size_t n);
 inline void memcpy_far(u16 d_seg, void *d_far
                        , u16 s_seg, const void *s_far, size_t len);
 void *memcpy(void *d1, const void *s1, size_t len);
-#if MODE16 == 0
+#if MODESEGMENT == 0
 #define memcpy __builtin_memcpy
 #endif
 void iomemcpy(void *d, const void *s, u32 len);