[SeaBIOS] [PATCH 04/13] Move farcall16 code from util.c to stacks.c and reorg stacks.c.

Kevin O'Connor kevin at koconnor.net
Tue May 29 06:07:31 CEST 2012


No code changes - just code movement.

Signed-off-by: Kevin O'Connor <kevin at koconnor.net>
---
 src/stacks.c |  237 ++++++++++++++++++++++++++++++++++------------------------
 src/util.c   |   49 ------------
 src/util.h   |   20 +++---
 3 files changed, 150 insertions(+), 156 deletions(-)

diff --git a/src/stacks.c b/src/stacks.c
index 48512b0..da5daa9 100644
--- a/src/stacks.c
+++ b/src/stacks.c
@@ -8,20 +8,44 @@
 #include "util.h" // dprintf
 #include "bregs.h" // CR0_PE
 
-// Thread info - stored at bottom of each thread stack - don't change
-// without also updating the inline assembler below.
-struct thread_info {
-    struct thread_info *next;
-    void *stackpos;
-    struct thread_info **pprev;
-};
-struct thread_info VAR32FLATVISIBLE MainThread = {
-    &MainThread, NULL, &MainThread.next
-};
+
+/****************************************************************
+ * Extra 16bit stack
+ ****************************************************************/
+
+// Space for a stack for 16bit code.
+u8 ExtraStack[BUILD_EXTRA_STACK_SIZE+1] VARLOW __aligned(8);
+
+// Switch to the extra stack and call a function.
+inline u32
+stack_hop(u32 eax, u32 edx, void *func)
+{
+    ASSERT16();
+    u16 stack_seg = SEG_LOW, bkup_ss;
+    u32 bkup_esp;
+    asm volatile(
+        // Backup current %ss/%esp values.
+        "movw %%ss, %w3\n"
+        "movl %%esp, %4\n"
+        // Copy stack seg to %ds/%ss and set %esp
+        "movw %w6, %%ds\n"
+        "movw %w6, %%ss\n"
+        "movl %5, %%esp\n"
+        // Call func
+        "calll *%2\n"
+        // Restore segments and stack
+        "movw %w3, %%ds\n"
+        "movw %w3, %%ss\n"
+        "movl %4, %%esp"
+        : "+a" (eax), "+d" (edx), "+c" (func), "=&r" (bkup_ss), "=&r" (bkup_esp)
+        : "i" (&ExtraStack[BUILD_EXTRA_STACK_SIZE]), "r" (stack_seg)
+        : "cc", "memory");
+    return eax;
+}
 
 
 /****************************************************************
- * Low level helpers
+ * 16bit / 32bit calling
  ****************************************************************/
 
 static inline void sgdt(struct descloc_s *desc) {
@@ -88,98 +112,48 @@ call32(void *func, u32 eax, u32 errret)
     return eax;
 }
 
-// 16bit trampoline for enabling irqs from 32bit mode.
-ASM16(
-    "  .global trampoline_checkirqs\n"
-    "trampoline_checkirqs:\n"
-    "  rep ; nop\n"
-    "  lretw"
-    );
-
-static void
-check_irqs(void)
+// Call a function with a specified register state.  Note that on
+// return, the interrupt enable/disable flag may be altered.
+inline void
+farcall16(struct bregs *callregs)
 {
-    if (MODESEGMENT) {
-        asm volatile(
-            "sti\n"
-            "nop\n"
-            "rep ; nop\n"
-            "cli\n"
-            "cld\n"
-            : : :"memory");
-        return;
-    }
-    extern void trampoline_checkirqs();
-    struct bregs br;
-    br.flags = F_IF;
-    br.code.seg = SEG_BIOS;
-    br.code.offset = (u32)&trampoline_checkirqs;
-    farcall16big(&br);
+    if (!MODESEGMENT && getesp() > BUILD_STACK_ADDR)
+        panic("call16 with invalid stack\n");
+    asm volatile(
+#if MODE16 == 1
+        "calll __farcall16\n"
+        "cli\n"
+        "cld"
+#else
+        "calll __farcall16_from32"
+#endif
+        : "+a" (callregs), "+m" (*callregs)
+        :
+        : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
 }
 
-// 16bit trampoline for waiting for an irq from 32bit mode.
-ASM16(
-    "  .global trampoline_waitirq\n"
-    "trampoline_waitirq:\n"
-    "  sti\n"
-    "  hlt\n"
-    "  lretw"
-    );
-
-// Wait for next irq to occur.
-void
-yield_toirq(void)
+inline void
+farcall16big(struct bregs *callregs)
 {
-    if (MODESEGMENT) {
-        asm volatile("sti ; hlt ; cli ; cld": : :"memory");
-        return;
-    }
-    if (CONFIG_THREADS && MainThread.next != &MainThread) {
-        // Threads still active - do a yield instead.
-        yield();
-        return;
-    }
-    extern void trampoline_waitirq();
-    struct bregs br;
-    br.flags = 0;
-    br.code.seg = SEG_BIOS;
-    br.code.offset = (u32)&trampoline_waitirq;
-    farcall16big(&br);
+    ASSERT32FLAT();
+    if (getesp() > BUILD_STACK_ADDR)
+        panic("call16 with invalid stack\n");
+    asm volatile(
+        "calll __farcall16big_from32"
+        : "+a" (callregs), "+m" (*callregs)
+        :
+        : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
 }
 
-
-/****************************************************************
- * Extra 16bit stack
- ****************************************************************/
-
-// Space for a stack for 16bit code.
-u8 ExtraStack[BUILD_EXTRA_STACK_SIZE+1] VARLOW __aligned(8);
-
-// Switch to the extra stack and call a function.
-inline u32
-stack_hop(u32 eax, u32 edx, void *func)
+inline void
+__call16_int(struct bregs *callregs, u16 offset)
 {
-    ASSERT16();
-    u16 stack_seg = SEG_LOW, bkup_ss;
-    u32 bkup_esp;
-    asm volatile(
-        // Backup current %ss/%esp values.
-        "movw %%ss, %w3\n"
-        "movl %%esp, %4\n"
-        // Copy stack seg to %ds/%ss and set %esp
-        "movw %w6, %%ds\n"
-        "movw %w6, %%ss\n"
-        "movl %5, %%esp\n"
-        // Call func
-        "calll *%2\n"
-        // Restore segments and stack
-        "movw %w3, %%ds\n"
-        "movw %w3, %%ss\n"
-        "movl %4, %%esp"
-        : "+a" (eax), "+d" (edx), "+c" (func), "=&r" (bkup_ss), "=&r" (bkup_esp)
-        : "i" (&ExtraStack[BUILD_EXTRA_STACK_SIZE]), "r" (stack_seg)
-        : "cc", "memory");
-    return eax;
+    if (MODESEGMENT)
+        callregs->code.seg = GET_SEG(CS);
+    else
+        callregs->code.seg = SEG_BIOS;
+    callregs->code.offset = offset;
+    farcall16(callregs);
 }
 
 
@@ -187,8 +161,17 @@ stack_hop(u32 eax, u32 edx, void *func)
  * Threads
  ****************************************************************/
 
+// Thread info - stored at bottom of each thread stack - don't change
+// without also updating the inline assembler below.
+struct thread_info {
+    struct thread_info *next;
+    void *stackpos;
+    struct thread_info **pprev;
+};
+struct thread_info VAR32FLATVISIBLE MainThread = {
+    &MainThread, NULL, &MainThread.next
+};
 #define THREADSTACKSIZE 4096
-int VAR16VISIBLE CanPreempt;
 
 // Return the 'struct thread_info' for the currently running thread.
 struct thread_info *
@@ -221,6 +204,35 @@ switch_next(struct thread_info *cur)
         : "ebx", "edx", "esi", "edi", "cc", "memory");
 }
 
+// 16bit trampoline for enabling irqs from 32bit mode.
+ASM16(
+    "  .global trampoline_checkirqs\n"
+    "trampoline_checkirqs:\n"
+    "  rep ; nop\n"
+    "  lretw"
+    );
+
+static void
+check_irqs(void)
+{
+    if (MODESEGMENT) {
+        asm volatile(
+            "sti\n"
+            "nop\n"
+            "rep ; nop\n"
+            "cli\n"
+            "cld\n"
+            : : :"memory");
+        return;
+    }
+    extern void trampoline_checkirqs();
+    struct bregs br;
+    br.flags = F_IF;
+    br.code.seg = SEG_BIOS;
+    br.code.offset = (u32)&trampoline_checkirqs;
+    farcall16big(&br);
+}
+
 // Briefly permit irqs to occur.
 void
 yield(void)
@@ -239,6 +251,36 @@ yield(void)
     switch_next(cur);
 }
 
+// 16bit trampoline for waiting for an irq from 32bit mode.
+ASM16(
+    "  .global trampoline_waitirq\n"
+    "trampoline_waitirq:\n"
+    "  sti\n"
+    "  hlt\n"
+    "  lretw"
+    );
+
+// Wait for next irq to occur.
+void
+yield_toirq(void)
+{
+    if (MODESEGMENT) {
+        asm volatile("sti ; hlt ; cli ; cld": : :"memory");
+        return;
+    }
+    if (CONFIG_THREADS && MainThread.next != &MainThread) {
+        // Threads still active - do a yield instead.
+        yield();
+        return;
+    }
+    extern void trampoline_waitirq();
+    struct bregs br;
+    br.flags = 0;
+    br.code.seg = SEG_BIOS;
+    br.code.offset = (u32)&trampoline_waitirq;
+    farcall16big(&br);
+}
+
 // Last thing called from a thread (called on "next" stack).
 static void
 __end_thread(struct thread_info *old)
@@ -332,6 +374,7 @@ mutex_unlock(struct mutex_s *mutex)
  * Thread preemption
  ****************************************************************/
 
+int VAR16VISIBLE CanPreempt;
 static u32 PreemptCount;
 
 // Turn on RTC irqs and arrange for them to check the 32bit threads.
diff --git a/src/util.c b/src/util.c
index c7a22fc..0e69913 100644
--- a/src/util.c
+++ b/src/util.c
@@ -26,55 +26,6 @@ cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
 
 
 /****************************************************************
- * 16bit calls
- ****************************************************************/
-
-// Call a function with a specified register state.  Note that on
-// return, the interrupt enable/disable flag may be altered.
-inline void
-farcall16(struct bregs *callregs)
-{
-    if (!MODESEGMENT && getesp() > BUILD_STACK_ADDR)
-        panic("call16 with invalid stack\n");
-    asm volatile(
-#if MODE16 == 1
-        "calll __farcall16\n"
-        "cli\n"
-        "cld"
-#else
-        "calll __farcall16_from32"
-#endif
-        : "+a" (callregs), "+m" (*callregs)
-        :
-        : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
-}
-
-inline void
-farcall16big(struct bregs *callregs)
-{
-    ASSERT32FLAT();
-    if (getesp() > BUILD_STACK_ADDR)
-        panic("call16 with invalid stack\n");
-    asm volatile(
-        "calll __farcall16big_from32"
-        : "+a" (callregs), "+m" (*callregs)
-        :
-        : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
-}
-
-inline void
-__call16_int(struct bregs *callregs, u16 offset)
-{
-    if (MODESEGMENT)
-        callregs->code.seg = GET_SEG(CS);
-    else
-        callregs->code.seg = SEG_BIOS;
-    callregs->code.offset = offset;
-    farcall16(callregs);
-}
-
-
-/****************************************************************
  * String ops
  ****************************************************************/
 
diff --git a/src/util.h b/src/util.h
index 5357841..f4a5ac8 100644
--- a/src/util.h
+++ b/src/util.h
@@ -197,14 +197,6 @@ struct descloc_s {
 
 // util.c
 void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
-struct bregs;
-inline void farcall16(struct bregs *callregs);
-inline void farcall16big(struct bregs *callregs);
-inline void __call16_int(struct bregs *callregs, u16 offset);
-#define call16_int(nr, callregs) do {                           \
-        extern void irq_trampoline_ ##nr ();                    \
-        __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
-    } while (0)
 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
 u8 checksum(void *buf, u32 len);
 size_t strlen(const char *s);
@@ -229,11 +221,18 @@ void nullTrailingSpace(char *buf);
 int get_keystroke(int msec);
 
 // stacks.c
-u32 call32(void *func, u32 eax, u32 errret);
 extern u8 ExtraStack[];
 inline u32 stack_hop(u32 eax, u32 edx, void *func);
+u32 call32(void *func, u32 eax, u32 errret);
+struct bregs;
+inline void farcall16(struct bregs *callregs);
+inline void farcall16big(struct bregs *callregs);
+inline void __call16_int(struct bregs *callregs, u16 offset);
+#define call16_int(nr, callregs) do {                           \
+        extern void irq_trampoline_ ##nr ();                    \
+        __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
+    } while (0)
 extern struct thread_info MainThread;
-extern int CanPreempt;
 struct thread_info *getCurThread(void);
 void yield(void);
 void yield_toirq(void);
@@ -242,6 +241,7 @@ void wait_threads(void);
 struct mutex_s { u32 isLocked; };
 void mutex_lock(struct mutex_s *mutex);
 void mutex_unlock(struct mutex_s *mutex);
+extern int CanPreempt;
 void start_preempt(void);
 void finish_preempt(void);
 int wait_preempt(void);
-- 
1.7.6.5




More information about the SeaBIOS mailing list