Replace irq_enable() regions with explicit calls to check for irqs.
[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 static inline void writel(void *addr, u32 val) {
98     *(volatile u32 *)addr = val;
99 }
100 static inline void writew(void *addr, u16 val) {
101     *(volatile u16 *)addr = val;
102 }
103 static inline void writeb(void *addr, u8 val) {
104     *(volatile u8 *)addr = val;
105 }
106 static inline u32 readl(const void *addr) {
107     return *(volatile const u32 *)addr;
108 }
109 static inline u16 readw(const void *addr) {
110     return *(volatile const u16 *)addr;
111 }
112 static inline u8 readb(const void *addr) {
113     return *(volatile const u8 *)addr;
114 }
115
116 // GDT bit manipulation
117 #define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
118                       | (((u64)(v) & 0x00ffffff) << 16))
119 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
120                       | (((u64)(v) & 0x0000ffff) << 0))
121 #define GDT_CODE     (0x9bULL << 40) // Code segment - P,R,A bits also set
122 #define GDT_DATA     (0x93ULL << 40) // Data segment - W,A bits also set
123 #define GDT_B        (0x1ULL << 54)  // Big flag
124 #define GDT_G        (0x1ULL << 55)  // Granularity flag
125
126 #define call16_simpint(nr, peax, pflags) do {                           \
127         ASSERT16();                                                     \
128         asm volatile(                                                   \
129             "stc\n"                                                     \
130             "int %2\n"                                                  \
131             "pushfl\n"                                                  \
132             "popl %1\n"                                                 \
133             "cli\n"                                                     \
134             "cld"                                                       \
135             : "+a"(*peax), "=r"(*pflags)                                \
136             : "i"(nr)                                                   \
137             : "cc", "memory");                                          \
138     } while (0)
139
140 // util.c
141 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
142 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
143 u8 checksum(void *buf, u32 len);
144 int memcmp(const void *s1, const void *s2, size_t n);
145 size_t strlen(const char *s);
146 int strcmp(const char *s1, const char *s2);
147 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
148 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
149 void *memset(void *s, int c, size_t n);
150 void *memcpy(void *d1, const void *s1, size_t len);
151 #if MODE16 == 0
152 #define memcpy __builtin_memcpy
153 #endif
154 inline void memcpy_far(u16 d_seg, void *d_far
155                        , u16 s_seg, const void *s_far, size_t len);
156 void *memmove(void *d, const void *s, size_t len);
157 char *strtcpy(char *dest, const char *src, size_t len);
158 struct bregs;
159 inline void call16(struct bregs *callregs);
160 inline void call16big(struct bregs *callregs);
161 inline void __call16_int(struct bregs *callregs, u16 offset);
162 #define call16_int(nr, callregs) do {                           \
163         extern void irq_trampoline_ ##nr ();                    \
164         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
165     } while (0)
166 void yield();
167 void biosusleep(u32 usec);
168 int get_keystroke(int msec);
169
170 // output.c
171 void debug_serial_setup();
172 void panic(const char *fmt, ...)
173     __attribute__ ((format (printf, 1, 2)))
174     __attribute__ ((noreturn));
175 void printf(const char *fmt, ...)
176     __attribute__ ((format (printf, 1, 2)));
177 void __dprintf(const char *fmt, ...)
178     __attribute__ ((format (printf, 1, 2)));
179 void snprintf(char *str, size_t size, const char *fmt, ...)
180     __attribute__ ((format (printf, 3, 4)));
181 #define dprintf(lvl, fmt, args...) do {                         \
182         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
183             __dprintf((fmt) , ##args );                         \
184     } while (0)
185 void __debug_enter(struct bregs *regs, const char *fname);
186 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
187 void __debug_isr(const char *fname);
188 #define debug_enter(regs, lvl) do {                     \
189         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
190             __debug_enter((regs), __func__);            \
191     } while (0)
192 #define debug_isr(lvl) do {                             \
193         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
194             __debug_isr(__func__);                      \
195     } while (0)
196 #define debug_stub(regs)                        \
197     __debug_stub((regs), __LINE__, __func__)
198 void hexdump(const void *d, int len);
199
200 // kbd.c
201 void kbd_setup();
202 void handle_15c2(struct bregs *regs);
203 void process_key(u8 key);
204
205 // mouse.c
206 void mouse_setup();
207
208 // system.c
209 extern u32 RamSize;
210 extern u64 RamSizeOver4G;
211 void mathcp_setup();
212
213 // serial.c
214 void serial_setup();
215 void lpt_setup();
216
217 // clock.c
218 static inline int check_time(u64 end) {
219     return (s64)(rdtscll() - end) > 0;
220 }
221 void timer_setup();
222 void ndelay(u32 count);
223 void udelay(u32 count);
224 void mdelay(u32 count);
225 void nsleep(u32 count);
226 void usleep(u32 count);
227 void msleep(u32 count);
228 u64 calc_future_tsc(u32 msecs);
229 u64 calc_future_tsc_usec(u32 usecs);
230 void handle_1583(struct bregs *regs);
231 void handle_1586(struct bregs *regs);
232
233 // apm.c
234 void VISIBLE16 handle_1553(struct bregs *regs);
235
236 // pcibios.c
237 void handle_1ab1(struct bregs *regs);
238
239 // shadow.c
240 void make_bios_writable();
241 void make_bios_readonly();
242
243 // pciinit.c
244 void pci_setup(void);
245
246 // smm.c
247 void smm_init();
248
249 // smp.c
250 extern u32 CountCPUs;
251 extern u32 MaxCountCPUs;
252 void wrmsr_smp(u32 index, u64 val);
253 void smp_probe(void);
254 void smp_probe_setup(void);
255
256 // coreboot.c
257 struct cbfs_file;
258 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
259 u32 cbfs_datasize(struct cbfs_file *file);
260 const char *cbfs_filename(struct cbfs_file *file);
261 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
262 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
263 void cbfs_run_payload(struct cbfs_file *file);
264
265 void coreboot_copy_biostable();
266 void coreboot_setup();
267
268 // vgahooks.c
269 extern int VGAbdf;
270 void handle_155f();
271 void vgahook_setup(const char *vendor, const char *part);
272
273 // optionroms.c
274 void call_bcv(u16 seg, u16 ip);
275 void optionrom_setup();
276 void vga_setup();
277 void s3_resume_vga_init();
278 extern u32 RomEnd;
279
280 // resume.c
281 void init_dma();
282
283 // pnpbios.c
284 #define PNP_SIGNATURE 0x506e5024 // $PnP
285 u16 get_pnp_offset();
286 void pnp_setup();
287
288 // pmm.c
289 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
290 void malloc_setup();
291 void malloc_finalize();
292 void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
293 int pmm_free(void *data);
294 void pmm_setup();
295 void pmm_finalize();
296 #define PMM_DEFAULT_HANDLE 0xFFFFFFFF
297 // Minimum alignment of malloc'd memory
298 #define MALLOC_MIN_ALIGN 16
299 // Helper functions for memory allocation.
300 static inline void *malloc_low(u32 size) {
301     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
302 }
303 static inline void *malloc_high(u32 size) {
304     return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
305 }
306 static inline void *malloc_fseg(u32 size) {
307     return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
308 }
309 static inline void *malloc_tmphigh(u32 size) {
310     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
311 }
312 static inline void *memalign_low(u32 align, u32 size) {
313     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
314 }
315 static inline void *memalign_high(u32 align, u32 size) {
316     return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
317 }
318 static inline void *memalign_tmphigh(u32 align, u32 size) {
319     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
320 }
321 static inline void free(void *data) {
322     pmm_free(data);
323 }
324
325 // mtrr.c
326 void mtrr_setup(void);
327
328 // romlayout.S
329 void reset_vector() __attribute__ ((noreturn));
330
331 // misc.c
332 extern u8 BiosChecksum;
333
334 // version (auto generated file out/version.c)
335 extern const char VERSION[];
336
337 #endif // util.h