This patch adds the BIOS support for SMP, ACPI, PCI, SMM, SMBIOS.
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU GPLv3 license.
6 #ifndef __UTIL_H
7 #define __UTIL_H
8
9 #include "ioport.h" // outb
10 #include "biosvar.h" // struct bregs
11
12 static inline void irq_disable(void)
13 {
14     asm volatile("cli": : :"memory");
15 }
16
17 static inline void irq_enable(void)
18 {
19     asm volatile("sti": : :"memory");
20 }
21
22 static inline unsigned long irq_save(void)
23 {
24     unsigned long flags;
25     asm volatile("pushfl ; popl %0" : "=g" (flags));
26     irq_disable();
27     return flags;
28 }
29
30 static inline void irq_restore(unsigned long flags)
31 {
32     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
33 }
34
35 static inline void nop(void)
36 {
37     asm volatile("nop");
38 }
39
40 static inline void hlt(void)
41 {
42     asm volatile("hlt");
43 }
44
45 #define BX_PANIC(fmt, args...) bprintf(0, fmt , ##args)
46 #define BX_INFO(fmt, args...) bprintf(0, fmt , ##args)
47
48 static inline void
49 memset(void *s, int c, size_t n)
50 {
51     while (n)
52         ((char *)s)[--n] = c;
53 }
54
55 static inline void
56 memcpy(void *d, void *s, size_t n)
57 {
58     while (n) {
59         ((char *)d)[n-1] = ((char *)s)[n-1];
60                 n--;
61         }
62 }
63
64 static inline void
65 eoi_master_pic()
66 {
67     outb(PIC1_IRQ5, PORT_PIC1);
68 }
69
70 static inline void
71 eoi_both_pics()
72 {
73     outb(PIC2_IRQ13, PORT_PIC2);
74     eoi_master_pic();
75 }
76
77 static inline
78 void call16(struct bregs *callregs)
79 {
80     asm volatile(
81         "pushl %%ebp\n" // Save state
82         "pushfl\n"
83 #ifdef MODE16
84         "calll __call16\n"
85 #else
86         "calll __call16_from32\n"
87 #endif
88         "popfl\n"       // Restore state
89         "popl %%ebp\n"
90         : "+a" (callregs), "+m" (*callregs)
91         :
92         : "ebx", "ecx", "edx", "esi", "edi");
93 }
94
95 static inline
96 void __call16_int(struct bregs *callregs, u16 offset)
97 {
98     callregs->cs = SEG_BIOS;
99     callregs->ip = offset;
100     call16(callregs);
101 }
102
103 #ifdef MODE16
104 #define call16_int(nr, callregs) do {                           \
105         extern void irq_trampoline_ ##nr ();                    \
106         __call16_int((callregs), (u16)&irq_trampoline_ ##nr );  \
107     } while (0)
108 #else
109 #include "../out/rom16.offset.auto.h"
110 #define call16_int(nr, callregs)                                \
111     __call16_int((callregs), OFFSET_irq_trampoline_ ##nr )
112 #endif
113
114 // output.c
115 void bprintf(u16 action, const char *fmt, ...)
116     __attribute__ ((format (printf, 2, 3)));
117 void __debug_enter(const char *fname, struct bregs *regs);
118 void __debug_exit(const char *fname, struct bregs *regs);
119 void __debug_stub(const char *fname, struct bregs *regs);
120 void __debug_isr(const char *fname, struct bregs *regs);
121 #define debug_enter(regs) \
122     __debug_enter(__func__, regs)
123 #define debug_exit(regs) \
124     __debug_exit(__func__, regs)
125 #define debug_stub(regs) \
126     __debug_stub(__func__, regs)
127 #define debug_isr(regs) \
128     __debug_isr(__func__, regs)
129 #define printf(fmt, args...)                     \
130     bprintf(1, fmt , ##args )
131
132 // kbd.c
133 void handle_15c2(struct bregs *regs);
134
135 // clock.c
136 void handle_1583(struct bregs *regs);
137
138 // apm.c
139 void handle_1553(struct bregs *regs);
140
141 // Frequent bios return helper
142 #define RET_EUNSUPPORTED 0x86
143 static inline void
144 handle_ret(struct bregs *regs, u8 code)
145 {
146     regs->ah = code;
147     set_cf(regs, code);
148 }
149
150 unsigned long
151 align(unsigned long addr, unsigned long v)
152 {
153     return (addr + v - 1) & ~(v - 1);
154 }
155
156 #endif // util.h