irq0override provided by qemu.
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6 #ifndef __UTIL_H
7 #define __UTIL_H
8
9 #include "types.h" // u32
10
11 static inline void irq_disable(void)
12 {
13     asm volatile("cli": : :"memory");
14 }
15
16 static inline void irq_enable(void)
17 {
18     asm volatile("sti": : :"memory");
19 }
20
21 static inline unsigned long irq_save(void)
22 {
23     unsigned long flags;
24     asm volatile("pushfl ; popl %0" : "=g" (flags));
25     irq_disable();
26     return flags;
27 }
28
29 static inline void irq_restore(unsigned long flags)
30 {
31     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
32 }
33
34 static inline void cpu_relax(void)
35 {
36     asm volatile("rep ; nop": : :"memory");
37 }
38
39 // Atomically enable irqs and sleep until an irq; then re-disable irqs.
40 static inline void wait_irq(void)
41 {
42     asm volatile("sti ; hlt ; cli ; cld": : :"memory");
43 }
44
45 static inline void nop(void)
46 {
47     asm volatile("nop");
48 }
49
50 static inline void hlt(void)
51 {
52     asm volatile("hlt");
53 }
54
55 static inline void wbinvd(void)
56 {
57     asm volatile("wbinvd");
58 }
59
60 #define CPUID_MSR (1 << 5)
61 #define CPUID_APIC (1 << 9)
62 #define CPUID_MTRR (1 << 12)
63 static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
64 {
65     asm("cpuid"
66         : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
67         : "0" (index));
68 }
69
70 static inline u64 rdmsr(u32 index)
71 {
72     u64 ret;
73     asm ("rdmsr" : "=A"(ret) : "c"(index));
74     return ret;
75 }
76
77 static inline void wrmsr(u32 index, u64 val)
78 {
79     asm volatile ("wrmsr" : : "c"(index), "A"(val));
80 }
81
82 static inline u64 rdtscll(void)
83 {
84     u64 val;
85     asm volatile("rdtsc" : "=A" (val));
86     return val;
87 }
88
89 static inline u32 __ffs(u32 word)
90 {
91     asm("bsf %1,%0"
92         : "=r" (word)
93         : "rm" (word));
94     return word;
95 }
96
97 // GDT bit manipulation
98 #define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
99                       | (((u64)(v) & 0x00ffffff) << 16))
100 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
101                       | (((u64)(v) & 0x0000ffff) << 0))
102 #define GDT_CODE     (0x9bULL << 40) // Code segment - P,R,A bits also set
103 #define GDT_DATA     (0x93ULL << 40) // Data segment - W,A bits also set
104 #define GDT_B        (0x1ULL << 54)  // Big flag
105 #define GDT_G        (0x1ULL << 55)  // Granularity flag
106
107 #define call16_simpint(nr, peax, pflags) do {                           \
108         ASSERT16();                                                     \
109         asm volatile(                                                   \
110             "stc\n"                                                     \
111             "int %2\n"                                                  \
112             "pushfl\n"                                                  \
113             "popl %1\n"                                                 \
114             "cli\n"                                                     \
115             "cld"                                                       \
116             : "+a"(*peax), "=r"(*pflags)                                \
117             : "i"(nr)                                                   \
118             : "cc", "memory");                                          \
119     } while (0)
120
121 // util.c
122 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
123 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
124 u8 checksum(void *buf, u32 len);
125 int memcmp(const void *s1, const void *s2, size_t n);
126 size_t strlen(const char *s);
127 int strcmp(const char *s1, const char *s2);
128 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
129 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
130 void *memset(void *s, int c, size_t n);
131 void *memcpy(void *d1, const void *s1, size_t len);
132 #if MODE16 == 0
133 #define memcpy __builtin_memcpy
134 #endif
135 inline void memcpy_far(u16 d_seg, void *d_far
136                        , u16 s_seg, const void *s_far, size_t len);
137 void *memmove(void *d, const void *s, size_t len);
138 char *strtcpy(char *dest, const char *src, size_t len);
139 struct bregs;
140 inline void call16(struct bregs *callregs);
141 inline void call16big(struct bregs *callregs);
142 inline void __call16_int(struct bregs *callregs, u16 offset);
143 #define call16_int(nr, callregs) do {                           \
144         extern void irq_trampoline_ ##nr ();                    \
145         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
146     } while (0)
147 void usleep(u32 usec);
148 int get_keystroke(int msec);
149
150 // output.c
151 void debug_serial_setup();
152 void panic(const char *fmt, ...)
153     __attribute__ ((format (printf, 1, 2)))
154     __attribute__ ((noreturn));
155 void printf(const char *fmt, ...)
156     __attribute__ ((format (printf, 1, 2)));
157 void __dprintf(const char *fmt, ...)
158     __attribute__ ((format (printf, 1, 2)));
159 void snprintf(char *str, size_t size, const char *fmt, ...)
160     __attribute__ ((format (printf, 3, 4)));
161 #define dprintf(lvl, fmt, args...) do {                         \
162         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
163             __dprintf((fmt) , ##args );                         \
164     } while (0)
165 void __debug_enter(struct bregs *regs, const char *fname);
166 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
167 void __debug_isr(const char *fname);
168 #define debug_enter(regs, lvl) do {                     \
169         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
170             __debug_enter((regs), __func__);            \
171     } while (0)
172 #define debug_isr(lvl) do {                             \
173         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
174             __debug_isr(__func__);                      \
175     } while (0)
176 #define debug_stub(regs)                        \
177     __debug_stub((regs), __LINE__, __func__)
178 void hexdump(const void *d, int len);
179
180 // kbd.c
181 void kbd_setup();
182 void handle_15c2(struct bregs *regs);
183 void process_key(u8 key);
184
185 // mouse.c
186 void mouse_setup();
187
188 // system.c
189 extern u32 RamSize;
190 extern u64 RamSizeOver4G;
191 void mathcp_setup();
192
193 // serial.c
194 void serial_setup();
195 void lpt_setup();
196
197 // clock.c
198 void timer_setup();
199 void ndelay(u32 count);
200 void udelay(u32 count);
201 void mdelay(u32 count);
202 u64 calc_future_tsc(u32 msecs);
203 void handle_1583(struct bregs *regs);
204 void handle_1586(struct bregs *regs);
205
206 // apm.c
207 void VISIBLE16 handle_1553(struct bregs *regs);
208
209 // pcibios.c
210 void handle_1ab1(struct bregs *regs);
211
212 // shadow.c
213 void make_bios_writable();
214 void make_bios_readonly();
215
216 // pciinit.c
217 void pci_setup(void);
218
219 // smm.c
220 void smm_init();
221
222 // smp.c
223 extern u32 CountCPUs;
224 void wrmsr_smp(u32 index, u64 val);
225 void smp_probe(void);
226 void smp_probe_setup(void);
227
228 // smbios.c
229 void smbios_init(void);
230
231 // coreboot.c
232 struct cbfs_file;
233 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
234 u32 cbfs_datasize(struct cbfs_file *file);
235 const char *cbfs_filename(struct cbfs_file *file);
236 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
237 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
238 void cbfs_run_payload(struct cbfs_file *file);
239
240 void coreboot_copy_biostable();
241 void coreboot_setup();
242
243 // vgahooks.c
244 extern int VGAbdf;
245 void handle_155f();
246 void vgahook_setup(const char *vendor, const char *part);
247
248 // optionroms.c
249 void call_bcv(u16 seg, u16 ip);
250 void optionrom_setup();
251 void vga_setup();
252 void s3_resume_vga_init();
253 extern u32 RomEnd;
254
255 // resume.c
256 void init_dma();
257
258 // pnpbios.c
259 #define PNP_SIGNATURE 0x506e5024 // $PnP
260 u16 get_pnp_offset();
261 void pnp_setup();
262
263 // pmm.c
264 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
265 void *zone_malloc(struct zone_s *zone, u32 size, u32 align);
266 void *zone_malloc_low(u32 size, u32 align);
267 void malloc_setup();
268 void malloc_finalize();
269 void pmm_setup();
270 void pmm_finalize();
271 // Minimum alignment of malloc'd memory
272 #define MALLOC_MIN_ALIGN 16
273 // Helper functions for memory allocation.
274 static inline void *malloc_low(u32 size) {
275     return zone_malloc_low(size, MALLOC_MIN_ALIGN);
276 }
277 static inline void *malloc_high(u32 size) {
278     return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
279 }
280 static inline void *malloc_fseg(u32 size) {
281     return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
282 }
283 static inline void *malloc_tmphigh(u32 size) {
284     return zone_malloc(&ZoneTmpHigh, size, MALLOC_MIN_ALIGN);
285 }
286 static inline void *memalign_low(u32 align, u32 size) {
287     return zone_malloc_low(size, align);
288 }
289 static inline void *memalign_high(u32 align, u32 size) {
290     return zone_malloc(&ZoneHigh, size, align);
291 }
292 static inline void *memalign_tmphigh(u32 align, u32 size) {
293     return zone_malloc(&ZoneTmpHigh, size, align);
294 }
295
296 // mtrr.c
297 void mtrr_setup(void);
298
299 // romlayout.S
300 void reset_vector() __attribute__ ((noreturn));
301
302 // misc.c
303 extern u8 BiosChecksum;
304
305 // version (auto generated file out/version.c)
306 extern const char VERSION[];
307
308 #endif // util.h