Force use of indirect function calls in inline assembler.
[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 static inline u32 __fls(u32 word)
97 {
98     asm("bsr %1,%0"
99         : "=r" (word)
100         : "rm" (word));
101     return word;
102 }
103
104 static inline u16 __htons_constant(u16 val) {
105     return (val<<8) | (val>>8);
106 }
107 static inline u32 __htonl_constant(u32 val) {
108     return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
109 }
110 static inline u32 __htonl(u32 val) {
111     asm("bswapl %0" : "+r"(val));
112     return val;
113 }
114 #define htonl(x) (__builtin_constant_p((u32)(x)) ? __htonl_constant(x) : __htonl(x))
115 #define ntohl(x) htonl(x)
116 #define htons(x) __htons_constant(x)
117 #define ntohs(x) htons(x)
118
119 static inline u32 getesp(void) {
120     u32 esp;
121     asm("movl %%esp, %0" : "=rm"(esp));
122     return esp;
123 }
124
125 static inline void writel(void *addr, u32 val) {
126     *(volatile u32 *)addr = val;
127 }
128 static inline void writew(void *addr, u16 val) {
129     *(volatile u16 *)addr = val;
130 }
131 static inline void writeb(void *addr, u8 val) {
132     *(volatile u8 *)addr = val;
133 }
134 static inline u32 readl(const void *addr) {
135     return *(volatile const u32 *)addr;
136 }
137 static inline u16 readw(const void *addr) {
138     return *(volatile const u16 *)addr;
139 }
140 static inline u8 readb(const void *addr) {
141     return *(volatile const u8 *)addr;
142 }
143
144 #define call16_simpint(nr, peax, pflags) do {                           \
145         ASSERT16();                                                     \
146         asm volatile(                                                   \
147             "pushl %%ebp\n"                                             \
148             "sti\n"                                                     \
149             "stc\n"                                                     \
150             "int %2\n"                                                  \
151             "pushfl\n"                                                  \
152             "popl %1\n"                                                 \
153             "cli\n"                                                     \
154             "cld\n"                                                     \
155             "popl %%ebp"                                                \
156             : "+a"(*peax), "=c"(*pflags)                                \
157             : "i"(nr)                                                   \
158             : "ebx", "edx", "esi", "edi", "cc", "memory");              \
159     } while (0)
160
161 // GDT bit manipulation
162 #define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
163                       | (((u64)(v) & 0x00ffffff) << 16))
164 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
165                       | (((u64)(v) & 0x0000ffff) << 0))
166 #define GDT_CODE     (0x9bULL << 40) // Code segment - P,R,A bits also set
167 #define GDT_DATA     (0x93ULL << 40) // Data segment - W,A bits also set
168 #define GDT_B        (0x1ULL << 54)  // Big flag
169 #define GDT_G        (0x1ULL << 55)  // Granularity flag
170
171 struct descloc_s {
172     u16 length;
173     u32 addr;
174 } PACKED;
175
176 // util.c
177 struct bregs;
178 inline void call16(struct bregs *callregs);
179 inline void call16big(struct bregs *callregs);
180 inline void __call16_int(struct bregs *callregs, u16 offset);
181 #define call16_int(nr, callregs) do {                           \
182         extern void irq_trampoline_ ##nr ();                    \
183         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
184     } while (0)
185 void check_irqs(void);
186 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
187 u8 checksum(void *buf, u32 len);
188 size_t strlen(const char *s);
189 int memcmp(const void *s1, const void *s2, size_t n);
190 int strcmp(const char *s1, const char *s2);
191 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
192 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
193 void *memset(void *s, int c, size_t n);
194 inline void memcpy_far(u16 d_seg, void *d_far
195                        , u16 s_seg, const void *s_far, size_t len);
196 void *memcpy(void *d1, const void *s1, size_t len);
197 #if MODESEGMENT == 0
198 #define memcpy __builtin_memcpy
199 #endif
200 void iomemcpy(void *d, const void *s, u32 len);
201 void *memmove(void *d, const void *s, size_t len);
202 char *strtcpy(char *dest, const char *src, size_t len);
203 void biosusleep(u32 usec);
204 int get_keystroke(int msec);
205
206 // stacks.c
207 inline u32 stack_hop(u32 eax, u32 edx, void *func);
208 extern struct thread_info MainThread;
209 void thread_setup(void);
210 struct thread_info *getCurThread(void);
211 void yield(void);
212 void run_thread(void (*func)(void*), void *data);
213 void wait_threads(void);
214 struct mutex_s { u32 isLocked; };
215 void mutex_lock(struct mutex_s *mutex);
216 void mutex_unlock(struct mutex_s *mutex);
217 void start_preempt(void);
218 void finish_preempt(void);
219 int wait_preempt(void);
220 void check_preempt(void);
221
222 // output.c
223 void debug_serial_setup(void);
224 void panic(const char *fmt, ...)
225     __attribute__ ((format (printf, 1, 2))) __noreturn;
226 void printf(const char *fmt, ...)
227     __attribute__ ((format (printf, 1, 2)));
228 int snprintf(char *str, size_t size, const char *fmt, ...)
229     __attribute__ ((format (printf, 3, 4)));
230 void __dprintf(const char *fmt, ...)
231     __attribute__ ((format (printf, 1, 2)));
232 void __debug_enter(struct bregs *regs, const char *fname);
233 void __debug_isr(const char *fname);
234 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
235 void __warn_invalid(struct bregs *regs, int lineno, const char *fname);
236 void __warn_unimplemented(struct bregs *regs, int lineno, const char *fname);
237 void __warn_internalerror(int lineno, const char *fname);
238 void __warn_noalloc(int lineno, const char *fname);
239 void __warn_timeout(int lineno, const char *fname);
240 void __set_invalid(struct bregs *regs, int lineno, const char *fname);
241 void __set_unimplemented(struct bregs *regs, int lineno, const char *fname);
242 void __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname);
243 void __set_code_unimplemented(struct bregs *regs, u32 linecode
244                               , const char *fname);
245 void hexdump(const void *d, int len);
246
247 #define dprintf(lvl, fmt, args...) do {                         \
248         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
249             __dprintf((fmt) , ##args );                         \
250     } while (0)
251 #define debug_enter(regs, lvl) do {                     \
252         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
253             __debug_enter((regs), __func__);            \
254     } while (0)
255 #define debug_isr(lvl) do {                             \
256         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
257             __debug_isr(__func__);                      \
258     } while (0)
259 #define debug_stub(regs)                        \
260     __debug_stub((regs), __LINE__, __func__)
261 #define warn_invalid(regs)                      \
262     __warn_invalid((regs), __LINE__, __func__)
263 #define warn_unimplemented(regs)                        \
264     __warn_unimplemented((regs), __LINE__, __func__)
265 #define warn_internalerror()                    \
266     __warn_internalerror(__LINE__, __func__)
267 #define warn_noalloc()                          \
268     __warn_noalloc(__LINE__, __func__)
269 #define warn_timeout()                          \
270     __warn_timeout(__LINE__, __func__)
271 #define set_invalid(regs)                       \
272     __set_invalid((regs), __LINE__, __func__)
273 #define set_code_invalid(regs, code)                                    \
274     __set_code_invalid((regs), (code) | (__LINE__ << 8), __func__)
275 #define set_unimplemented(regs)                         \
276     __set_unimplemented((regs), __LINE__, __func__)
277 #define set_code_unimplemented(regs, code)                              \
278     __set_code_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
279
280 // kbd.c
281 void kbd_setup(void);
282 void handle_15c2(struct bregs *regs);
283 void process_key(u8 key);
284
285 // mouse.c
286 void mouse_setup(void);
287 void process_mouse(u8 data);
288
289 // system.c
290 extern u32 RamSize;
291 extern u64 RamSizeOver4G;
292 void mathcp_setup(void);
293
294 // serial.c
295 void serial_setup(void);
296 void lpt_setup(void);
297
298 // clock.c
299 #define PIT_TICK_RATE 1193180   // Underlying HZ of PIT
300 #define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
301 static inline int check_time(u64 end) {
302     return (s64)(rdtscll() - end) > 0;
303 }
304 void timer_setup(void);
305 void ndelay(u32 count);
306 void udelay(u32 count);
307 void mdelay(u32 count);
308 void nsleep(u32 count);
309 void usleep(u32 count);
310 void msleep(u32 count);
311 u64 calc_future_tsc(u32 msecs);
312 u64 calc_future_tsc_usec(u32 usecs);
313 void handle_1583(struct bregs *regs);
314 void handle_1586(struct bregs *regs);
315 void useRTC(void);
316 void releaseRTC(void);
317
318 // apm.c
319 void handle_1553(struct bregs *regs);
320
321 // pcibios.c
322 void handle_1ab1(struct bregs *regs);
323 void bios32_setup(void);
324
325 // shadow.c
326 void make_bios_writable(void);
327 void make_bios_readonly(void);
328
329 // pciinit.c
330 void pci_setup(void);
331
332 // smm.c
333 void smm_init(void);
334
335 // smp.c
336 extern u32 CountCPUs;
337 extern u32 MaxCountCPUs;
338 void wrmsr_smp(u32 index, u64 val);
339 void smp_probe(void);
340 void smp_probe_setup(void);
341
342 // coreboot.c
343 struct cbfs_file;
344 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
345 u32 cbfs_datasize(struct cbfs_file *file);
346 const char *cbfs_filename(struct cbfs_file *file);
347 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
348 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
349 void cbfs_run_payload(struct cbfs_file *file);
350
351 void coreboot_copy_biostable(void);
352 void coreboot_setup(void);
353
354 // vgahooks.c
355 extern int VGAbdf;
356 void handle_155f(struct bregs *regs);
357 void vgahook_setup(const char *vendor, const char *part);
358
359 // optionroms.c
360 void call_bcv(u16 seg, u16 ip);
361 void optionrom_setup(void);
362 void vga_setup(void);
363 void s3_resume_vga_init(void);
364 extern u32 RomEnd;
365
366 // resume.c
367 void init_dma(void);
368
369 // pnpbios.c
370 #define PNP_SIGNATURE 0x506e5024 // $PnP
371 u16 get_pnp_offset(void);
372 void pnp_setup(void);
373
374 // pmm.c
375 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
376 void malloc_setup(void);
377 void malloc_finalize(void);
378 void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
379 int pmm_free(void *data);
380 void pmm_setup(void);
381 void pmm_finalize(void);
382 #define PMM_DEFAULT_HANDLE 0xFFFFFFFF
383 // Minimum alignment of malloc'd memory
384 #define MALLOC_MIN_ALIGN 16
385 // Helper functions for memory allocation.
386 static inline void *malloc_low(u32 size) {
387     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
388 }
389 static inline void *malloc_high(u32 size) {
390     return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
391 }
392 static inline void *malloc_fseg(u32 size) {
393     return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
394 }
395 static inline void *malloc_tmphigh(u32 size) {
396     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
397 }
398 static inline void *malloc_tmp(u32 size) {
399     void *ret = malloc_tmphigh(size);
400     if (ret)
401         return ret;
402     return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
403 }
404 static inline void *memalign_low(u32 align, u32 size) {
405     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
406 }
407 static inline void *memalign_high(u32 align, u32 size) {
408     return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
409 }
410 static inline void *memalign_tmphigh(u32 align, u32 size) {
411     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
412 }
413 static inline void free(void *data) {
414     pmm_free(data);
415 }
416
417 // mtrr.c
418 void mtrr_setup(void);
419
420 // romlayout.S
421 void reset_vector(void) __noreturn;
422
423 // misc.c
424 extern u8 BiosChecksum;
425
426 // version (auto generated file out/version.c)
427 extern const char VERSION[];
428
429 #endif // util.h