Try to hard-reboot on rerun of post even on emulators.
[seabios.git] / src / util.h
index 9f71d65a00434b70f45e17ceeb9658669fc09339..d2003c162cdff4d01b51131c9a2a92de8308387d 100644 (file)
@@ -1,6 +1,6 @@
 // Basic x86 asm functions and function defs.
 //
-// Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2008-2010  Kevin O'Connor <kevin@koconnor.net>
 //
 // This file may be distributed under the terms of the GNU LGPLv3 license.
 #ifndef __UTIL_H
@@ -21,7 +21,7 @@ static inline void irq_enable(void)
 static inline unsigned long irq_save(void)
 {
     unsigned long flags;
-    asm volatile("pushfl ; popl %0" : "=g" (flags));
+    asm volatile("pushfl ; popl %0" : "=g" (flags): :"memory");
     irq_disable();
     return flags;
 }
@@ -36,12 +36,6 @@ static inline void cpu_relax(void)
     asm volatile("rep ; nop": : :"memory");
 }
 
-// Atomically enable irqs and sleep until an irq; then re-disable irqs.
-static inline void wait_irq(void)
-{
-    asm volatile("sti ; hlt ; cli ; cld": : :"memory");
-}
-
 static inline void nop(void)
 {
     asm volatile("nop");
@@ -49,12 +43,12 @@ static inline void nop(void)
 
 static inline void hlt(void)
 {
-    asm volatile("hlt");
+    asm volatile("hlt": : :"memory");
 }
 
 static inline void wbinvd(void)
 {
-    asm volatile("wbinvd");
+    asm volatile("wbinvd": : :"memory");
 }
 
 #define CPUID_MSR (1 << 5)
@@ -116,6 +110,16 @@ static inline u32 __htonl(u32 val) {
 #define htons(x) __htons_constant(x)
 #define ntohs(x) htons(x)
 
+static inline u16 cpu_to_le16(u16 x)
+{
+    return x;
+}
+
+static inline u32 cpu_to_le32(u32 x)
+{
+    return x;
+}
+
 static inline u32 getesp(void) {
     u32 esp;
     asm("movl %%esp, %0" : "=rm"(esp));
@@ -144,26 +148,33 @@ static inline u8 readb(const void *addr) {
 #define call16_simpint(nr, peax, pflags) do {                           \
         ASSERT16();                                                     \
         asm volatile(                                                   \
+            "pushl %%ebp\n"                                             \
+            "sti\n"                                                     \
             "stc\n"                                                     \
             "int %2\n"                                                  \
             "pushfl\n"                                                  \
             "popl %1\n"                                                 \
             "cli\n"                                                     \
-            "cld"                                                       \
-            : "+a"(*peax), "=r"(*pflags)                                \
+            "cld\n"                                                     \
+            "popl %%ebp"                                                \
+            : "+a"(*peax), "=c"(*pflags)                                \
             : "i"(nr)                                                   \
-            : "cc", "memory");                                          \
+            : "ebx", "edx", "esi", "edi", "cc", "memory");              \
     } while (0)
 
-// GDT bit manipulation
-#define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
-                      | (((u64)(v) & 0x00ffffff) << 16))
-#define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
-                      | (((u64)(v) & 0x0000ffff) << 0))
+// GDT bits
 #define GDT_CODE     (0x9bULL << 40) // Code segment - P,R,A bits also set
 #define GDT_DATA     (0x93ULL << 40) // Data segment - W,A bits also set
 #define GDT_B        (0x1ULL << 54)  // Big flag
 #define GDT_G        (0x1ULL << 55)  // Granularity flag
+// GDT bits for segment base
+#define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
+                      | (((u64)(v) & 0x00ffffff) << 16))
+// GDT bits for segment limit (0-1Meg)
+#define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
+                      | (((u64)(v) & 0x0000ffff) << 0))
+// GDT bits for segment limit (0-4Gig in 4K chunks)
+#define GDT_GRANLIMIT(v) (GDT_G | GDT_LIMIT((v) >> 12))
 
 struct descloc_s {
     u16 length;
@@ -179,7 +190,6 @@ inline void __call16_int(struct bregs *callregs, u16 offset);
         extern void irq_trampoline_ ##nr ();                    \
         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
     } while (0)
-void check_irqs(void);
 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
 u8 checksum(void *buf, u32 len);
 size_t strlen(const char *s);
@@ -190,6 +200,7 @@ inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
 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_fl(void *d_fl, const void *s_fl, size_t len);
 void *memcpy(void *d1, const void *s1, size_t len);
 #if MODESEGMENT == 0
 #define memcpy __builtin_memcpy
@@ -197,19 +208,23 @@ void *memcpy(void *d1, const void *s1, size_t len);
 void iomemcpy(void *d, const void *s, u32 len);
 void *memmove(void *d, const void *s, size_t len);
 char *strtcpy(char *dest, const char *src, size_t len);
-void biosusleep(u32 usec);
 int get_keystroke(int msec);
 
 // stacks.c
-inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
+inline u32 stack_hop(u32 eax, u32 edx, void *func);
 extern struct thread_info MainThread;
 void thread_setup(void);
 struct thread_info *getCurThread(void);
 void yield(void);
+void wait_irq(void);
 void run_thread(void (*func)(void*), void *data);
 void wait_threads(void);
+struct mutex_s { u32 isLocked; };
+void mutex_lock(struct mutex_s *mutex);
+void mutex_unlock(struct mutex_s *mutex);
 void start_preempt(void);
 void finish_preempt(void);
+int wait_preempt(void);
 void check_preempt(void);
 
 // output.c
@@ -291,7 +306,7 @@ void lpt_setup(void);
 // clock.c
 #define PIT_TICK_RATE 1193180   // Underlying HZ of PIT
 #define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
-static inline int check_time(u64 end) {
+static inline int check_tsc(u64 end) {
     return (s64)(rdtscll() - end) > 0;
 }
 void timer_setup(void);
@@ -303,12 +318,16 @@ void usleep(u32 count);
 void msleep(u32 count);
 u64 calc_future_tsc(u32 msecs);
 u64 calc_future_tsc_usec(u32 usecs);
+u32 calc_future_timer_ticks(u32 count);
+u32 calc_future_timer(u32 msecs);
+int check_timer(u32 end);
 void handle_1583(struct bregs *regs);
 void handle_1586(struct bregs *regs);
 void useRTC(void);
 void releaseRTC(void);
 
 // apm.c
+void apm_shutdown(void);
 void handle_1553(struct bregs *regs);
 
 // pcibios.c
@@ -318,8 +337,17 @@ void bios32_setup(void);
 // shadow.c
 void make_bios_writable(void);
 void make_bios_readonly(void);
+void make_bios_writable_intel(u16 bdf, u32 pam0);
+void make_bios_readonly_intel(u16 bdf, u32 pam0);
+void qemu_prep_reset(void);
+
+// smm.c
+void smm_save_and_copy(void);
+void smm_relocate_and_restore(void);
 
 // pciinit.c
+extern const u8 pci_irqs[4];
+void pci_bios_allocate_regions(u16 bdf, void *arg);
 void pci_setup(void);
 
 // smm.c
@@ -334,13 +362,12 @@ void smp_probe_setup(void);
 
 // coreboot.c
 struct cbfs_file;
+struct cbfs_file *cbfs_finddatafile(const char *fname);
 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
 u32 cbfs_datasize(struct cbfs_file *file);
 const char *cbfs_filename(struct cbfs_file *file);
 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
-int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
 void cbfs_run_payload(struct cbfs_file *file);
-
 void coreboot_copy_biostable(void);
 void coreboot_setup(void);
 
@@ -356,6 +383,11 @@ void vga_setup(void);
 void s3_resume_vga_init(void);
 extern u32 RomEnd;
 
+// bootsplash.c
+void enable_vga_console(void);
+void enable_bootsplash(void);
+void disable_bootsplash(void);
+
 // resume.c
 void init_dma(void);
 
@@ -385,6 +417,9 @@ static inline void *malloc_high(u32 size) {
 static inline void *malloc_fseg(u32 size) {
     return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
 }
+static inline void *malloc_tmplow(u32 size) {
+    return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+}
 static inline void *malloc_tmphigh(u32 size) {
     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
 }
@@ -392,7 +427,7 @@ static inline void *malloc_tmp(u32 size) {
     void *ret = malloc_tmphigh(size);
     if (ret)
         return ret;
-    return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+    return malloc_tmplow(size);
 }
 static inline void *memalign_low(u32 align, u32 size) {
     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);