Be sure to add "void" to all function prototypes that take no args.
[seabios.git] / src / stacks.c
index bd1c75f19da73cabaf8cb60b23606b7bb4b02513..a35ca3de7717a3bafed840689e3fdea40e5c3611 100644 (file)
@@ -8,6 +8,76 @@
 #include "util.h" // dprintf
 #include "bregs.h" // CR0_PE
 
+static inline u32 getcr0(void) {
+    u32 cr0;
+    asm("movl %%cr0, %0" : "=r"(cr0));
+    return cr0;
+}
+static inline void sgdt(struct descloc_s *desc) {
+    asm("sgdtl %0" : "=m"(*desc));
+}
+static inline void lgdt(struct descloc_s *desc) {
+    asm("lgdtl %0" : : "m"(*desc) : "memory");
+}
+
+// Call a 32bit SeaBIOS function from a 16bit SeaBIOS function.
+static inline int
+call32(void *func)
+{
+    ASSERT16();
+    u32 cr0 = getcr0();
+    if (cr0 & CR0_PE)
+        // Called in 16bit protected mode?!
+        return -1;
+
+    // Backup cmos index register and disable nmi
+    u8 cmosindex = inb(PORT_CMOS_INDEX);
+    outb(cmosindex | NMI_DISABLE_BIT, PORT_CMOS_INDEX);
+    inb(PORT_CMOS_DATA);
+
+    // Backup fs/gs and gdt
+    u16 fs = GET_SEG(FS), gs = GET_SEG(GS);
+    struct descloc_s gdt;
+    sgdt(&gdt);
+
+    func -= BUILD_BIOS_ADDR;
+    u32 bkup_ss, bkup_esp;
+    asm volatile(
+        // Backup ss/esp / set esp to flat stack location
+        "  movl %%ss, %0\n"
+        "  movl %%esp, %1\n"
+        "  shll $4, %0\n"
+        "  addl %0, %%esp\n"
+        "  movl %%ss, %0\n"
+
+        // Transition to 32bit mode, call func, return to 16bit
+        "  pushl $(" __stringify(BUILD_BIOS_ADDR) " + 1f)\n"
+        "  jmp transition32\n"
+        "  .code32\n"
+        "1:calll %2\n"
+        "  pushl $2f\n"
+        "  jmp transition16big\n"
+
+        // Restore ds/ss/esp
+        "  .code16gcc\n"
+        "2:movl %0, %%ds\n"
+        "  movl %0, %%ss\n"
+        "  movl %1, %%esp\n"
+        : "=&r" (bkup_ss), "=&r" (bkup_esp)
+        : "m" (*(u8*)func)
+        : "eax", "ecx", "edx", "cc", "memory");
+
+    // Restore gdt and fs/gs
+    lgdt(&gdt);
+    SET_SEG(FS, fs);
+    SET_SEG(GS, gs);
+
+    // Restore cmos index register
+    outb(cmosindex, PORT_CMOS_INDEX);
+    inb(PORT_CMOS_DATA);
+    return 0;
+}
+
 
 /****************************************************************
  * Stack in EBDA
@@ -56,7 +126,7 @@ struct thread_info VAR16VISIBLE MainThread;
 int VAR16VISIBLE CanPreempt;
 
 void
-thread_setup()
+thread_setup(void)
 {
     MainThread.next = &MainThread;
     MainThread.stackpos = NULL;
@@ -65,7 +135,7 @@ thread_setup()
 
 // Return the 'struct thread_info' for the currently running thread.
 struct thread_info *
-getCurThread()
+getCurThread(void)
 {
     u32 esp = getesp();
     if (esp <= BUILD_STACK_ADDR)
@@ -78,6 +148,9 @@ static void
 switch_next(struct thread_info *cur)
 {
     struct thread_info *next = cur->next;
+    if (cur == next)
+        // Nothing to do.
+        return;
     asm volatile(
         "  pushl $1f\n"                 // store return pc
         "  pushl %%ebp\n"               // backup %ebp
@@ -93,9 +166,9 @@ switch_next(struct thread_info *cur)
 
 // Briefly permit irqs to occur.
 void
-yield()
+yield(void)
 {
-    if (MODE16 || !CONFIG_THREADS) {
+    if (MODESEGMENT || !CONFIG_THREADS) {
         // Just directly check irqs.
         check_irqs();
         return;
@@ -125,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;
@@ -166,9 +239,9 @@ fail:
 
 // Wait for all threads (other than the main thread) to complete.
 void
-wait_threads()
+wait_threads(void)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     if (! CONFIG_THREADS)
         return;
     while (MainThread.next != &MainThread)
@@ -184,7 +257,7 @@ static u32 PreemptCount;
 
 // Turn on RTC irqs and arrange for them to check the 32bit threads.
 void
-start_preempt()
+start_preempt(void)
 {
     if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
         return;
@@ -195,7 +268,7 @@ start_preempt()
 
 // Turn off RTC irqs / stop checking for thread execution.
 void
-finish_preempt()
+finish_preempt(void)
 {
     if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
         return;
@@ -204,22 +277,11 @@ finish_preempt()
     dprintf(1, "Done preempt - %d checks\n", PreemptCount);
 }
 
-static inline u32 getcr0() {
-    u32 cr0;
-    asm("movl %%cr0, %0" : "=r"(cr0));
-    return cr0;
-}
-static inline void sgdt(struct descloc_s *desc) {
-    asm("sgdtl %0" : "=m"(*desc));
-}
-static inline void lgdt(struct descloc_s *desc) {
-    asm("lgdtl %0" : : "m"(*desc) : "memory");
-}
-
-#if !MODE16
+extern void yield_preempt(void);
+#if MODESEGMENT == 0
 // Try to execute 32bit threads.
-void VISIBLE32
-yield_preempt()
+void VISIBLE32FLAT
+yield_preempt(void)
 {
     PreemptCount++;
     switch_next(&MainThread);
@@ -228,60 +290,12 @@ yield_preempt()
 
 // 16bit code that checks if threads are pending and executes them if so.
 void
-check_preempt()
+check_preempt(void)
 {
-    ASSERT16();
     if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS
         || !GET_GLOBAL(CanPreempt)
         || GET_GLOBAL(MainThread.next) == &MainThread)
         return;
-    u32 cr0 = getcr0();
-    if (cr0 & CR0_PE)
-        // Called in 16bit protected mode?!
-        return;
-
-    // Backup cmos index register and disable nmi
-    u8 cmosindex = inb(PORT_CMOS_INDEX);
-    outb(cmosindex | NMI_DISABLE_BIT, PORT_CMOS_INDEX);
-    inb(PORT_CMOS_DATA);
 
-    // Backup fs/gs and gdt
-    u16 fs = GET_SEG(FS), gs = GET_SEG(GS);
-    struct descloc_s gdt;
-    sgdt(&gdt);
-
-    u32 bkup_ss, bkup_esp;
-    asm volatile(
-        // Backup ss/esp / set esp to flat stack location
-        "  movl %%ss, %0\n"
-        "  movl %%esp, %1\n"
-        "  shll $4, %0\n"
-        "  addl %0, %%esp\n"
-        "  movl %%ss, %0\n"
-
-        // Transition to 32bit mode, call yield_preempt, return to 16bit
-        "  pushl $(" __stringify(BUILD_BIOS_ADDR) " + 1f)\n"
-        "  jmp transition32\n"
-        "  .code32\n"
-        "1:calll (yield_preempt - " __stringify(BUILD_BIOS_ADDR) ")\n"
-        "  pushl $2f\n"
-        "  jmp transition16big\n"
-        "  .code16gcc\n"
-        "2:\n"
-
-        // Restore ds/ss/esp
-        "  movl %0, %%ds\n"
-        "  movl %0, %%ss\n"
-        "  movl %1, %%esp\n"
-        : "=&r" (bkup_ss), "=&r" (bkup_esp)
-        : : "eax", "ecx", "edx", "cc", "memory");
-
-    // Restore gdt and fs/gs
-    lgdt(&gdt);
-    SET_SEG(FS, fs);
-    SET_SEG(GS, gs);
-
-    // Restore cmos index register
-    outb(cmosindex, PORT_CMOS_INDEX);
-    inb(PORT_CMOS_DATA);
+    call32(yield_preempt);
 }