2160b37e38537cd099bbea204f2707bde97f73ae
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
2 //
3 // Copyright (C) 2008-2010  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): :"memory");
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 static inline void nop(void)
40 {
41     asm volatile("nop");
42 }
43
44 static inline void hlt(void)
45 {
46     asm volatile("hlt": : :"memory");
47 }
48
49 static inline void wbinvd(void)
50 {
51     asm volatile("wbinvd": : :"memory");
52 }
53
54 #define CPUID_MSR (1 << 5)
55 #define CPUID_APIC (1 << 9)
56 #define CPUID_MTRR (1 << 12)
57 static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
58 {
59     asm("cpuid"
60         : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
61         : "0" (index));
62 }
63
64 static inline u64 rdmsr(u32 index)
65 {
66     u64 ret;
67     asm ("rdmsr" : "=A"(ret) : "c"(index));
68     return ret;
69 }
70
71 static inline void wrmsr(u32 index, u64 val)
72 {
73     asm volatile ("wrmsr" : : "c"(index), "A"(val));
74 }
75
76 static inline u64 rdtscll(void)
77 {
78     u64 val;
79     asm volatile("rdtsc" : "=A" (val));
80     return val;
81 }
82
83 static inline u32 __ffs(u32 word)
84 {
85     asm("bsf %1,%0"
86         : "=r" (word)
87         : "rm" (word));
88     return word;
89 }
90 static inline u32 __fls(u32 word)
91 {
92     asm("bsr %1,%0"
93         : "=r" (word)
94         : "rm" (word));
95     return word;
96 }
97
98 static inline u16 __htons_constant(u16 val) {
99     return (val<<8) | (val>>8);
100 }
101 static inline u32 __htonl_constant(u32 val) {
102     return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
103 }
104 static inline u32 __htonl(u32 val) {
105     asm("bswapl %0" : "+r"(val));
106     return val;
107 }
108 #define htonl(x) (__builtin_constant_p((u32)(x)) ? __htonl_constant(x) : __htonl(x))
109 #define ntohl(x) htonl(x)
110 #define htons(x) __htons_constant(x)
111 #define ntohs(x) htons(x)
112
113 static inline u16 cpu_to_le16(u16 x)
114 {
115     return x;
116 }
117
118 static inline u32 cpu_to_le32(u32 x)
119 {
120     return x;
121 }
122
123 static inline u32 getesp(void) {
124     u32 esp;
125     asm("movl %%esp, %0" : "=rm"(esp));
126     return esp;
127 }
128
129 static inline void writel(void *addr, u32 val) {
130     *(volatile u32 *)addr = val;
131 }
132 static inline void writew(void *addr, u16 val) {
133     *(volatile u16 *)addr = val;
134 }
135 static inline void writeb(void *addr, u8 val) {
136     *(volatile u8 *)addr = val;
137 }
138 static inline u32 readl(const void *addr) {
139     return *(volatile const u32 *)addr;
140 }
141 static inline u16 readw(const void *addr) {
142     return *(volatile const u16 *)addr;
143 }
144 static inline u8 readb(const void *addr) {
145     return *(volatile const u8 *)addr;
146 }
147
148 #define call16_simpint(nr, peax, pflags) do {                           \
149         ASSERT16();                                                     \
150         asm volatile(                                                   \
151             "pushl %%ebp\n"                                             \
152             "sti\n"                                                     \
153             "stc\n"                                                     \
154             "int %2\n"                                                  \
155             "pushfl\n"                                                  \
156             "popl %1\n"                                                 \
157             "cli\n"                                                     \
158             "cld\n"                                                     \
159             "popl %%ebp"                                                \
160             : "+a"(*peax), "=c"(*pflags)                                \
161             : "i"(nr)                                                   \
162             : "ebx", "edx", "esi", "edi", "cc", "memory");              \
163     } while (0)
164
165 // GDT bits
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 // GDT bits for segment base
171 #define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
172                       | (((u64)(v) & 0x00ffffff) << 16))
173 // GDT bits for segment limit (0-1Meg)
174 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
175                       | (((u64)(v) & 0x0000ffff) << 0))
176 // GDT bits for segment limit (0-4Gig in 4K chunks)
177 #define GDT_GRANLIMIT(v) (GDT_G | GDT_LIMIT((v) >> 12))
178
179 struct descloc_s {
180     u16 length;
181     u32 addr;
182 } PACKED;
183
184 // util.c
185 struct bregs;
186 inline void call16(struct bregs *callregs);
187 inline void call16big(struct bregs *callregs);
188 inline void __call16_int(struct bregs *callregs, u16 offset);
189 #define call16_int(nr, callregs) do {                           \
190         extern void irq_trampoline_ ##nr ();                    \
191         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
192     } while (0)
193 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
194 u8 checksum(void *buf, u32 len);
195 size_t strlen(const char *s);
196 int memcmp(const void *s1, const void *s2, size_t n);
197 int strcmp(const char *s1, const char *s2);
198 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
199 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
200 void *memset(void *s, int c, size_t n);
201 void memset_fl(void *ptr, u8 val, size_t size);
202 inline void memcpy_far(u16 d_seg, void *d_far
203                        , u16 s_seg, const void *s_far, size_t len);
204 void memcpy_fl(void *d_fl, const void *s_fl, size_t len);
205 void *memcpy(void *d1, const void *s1, size_t len);
206 #if MODESEGMENT == 0
207 #define memcpy __builtin_memcpy
208 #endif
209 void iomemcpy(void *d, const void *s, u32 len);
210 void *memmove(void *d, const void *s, size_t len);
211 char *strtcpy(char *dest, const char *src, size_t len);
212 char *strchr(const char *s, int c);
213 void nullTrailingSpace(char *buf);
214 int get_keystroke(int msec);
215
216 // stacks.c
217 u32 call32(void *func, u32 eax, u32 errret);
218 inline u32 stack_hop(u32 eax, u32 edx, void *func);
219 extern struct thread_info MainThread;
220 struct thread_info *getCurThread(void);
221 void yield(void);
222 void wait_irq(void);
223 void run_thread(void (*func)(void*), void *data);
224 void wait_threads(void);
225 struct mutex_s { u32 isLocked; };
226 void mutex_lock(struct mutex_s *mutex);
227 void mutex_unlock(struct mutex_s *mutex);
228 void start_preempt(void);
229 void finish_preempt(void);
230 int wait_preempt(void);
231 void check_preempt(void);
232
233 // output.c
234 void debug_serial_setup(void);
235 void panic(const char *fmt, ...)
236     __attribute__ ((format (printf, 1, 2))) __noreturn;
237 void printf(const char *fmt, ...)
238     __attribute__ ((format (printf, 1, 2)));
239 int snprintf(char *str, size_t size, const char *fmt, ...)
240     __attribute__ ((format (printf, 3, 4)));
241 char * znprintf(size_t size, const char *fmt, ...)
242     __attribute__ ((format (printf, 2, 3)));
243 void __dprintf(const char *fmt, ...)
244     __attribute__ ((format (printf, 1, 2)));
245 void __debug_enter(struct bregs *regs, const char *fname);
246 void __debug_isr(const char *fname);
247 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
248 void __warn_invalid(struct bregs *regs, int lineno, const char *fname);
249 void __warn_unimplemented(struct bregs *regs, int lineno, const char *fname);
250 void __warn_internalerror(int lineno, const char *fname);
251 void __warn_noalloc(int lineno, const char *fname);
252 void __warn_timeout(int lineno, const char *fname);
253 void __set_invalid(struct bregs *regs, int lineno, const char *fname);
254 void __set_unimplemented(struct bregs *regs, int lineno, const char *fname);
255 void __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname);
256 void __set_code_unimplemented(struct bregs *regs, u32 linecode
257                               , const char *fname);
258 void hexdump(const void *d, int len);
259
260 #define dprintf(lvl, fmt, args...) do {                         \
261         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
262             __dprintf((fmt) , ##args );                         \
263     } while (0)
264 #define debug_enter(regs, lvl) do {                     \
265         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
266             __debug_enter((regs), __func__);            \
267     } while (0)
268 #define debug_isr(lvl) do {                             \
269         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
270             __debug_isr(__func__);                      \
271     } while (0)
272 #define debug_stub(regs)                        \
273     __debug_stub((regs), __LINE__, __func__)
274 #define warn_invalid(regs)                      \
275     __warn_invalid((regs), __LINE__, __func__)
276 #define warn_unimplemented(regs)                        \
277     __warn_unimplemented((regs), __LINE__, __func__)
278 #define warn_internalerror()                    \
279     __warn_internalerror(__LINE__, __func__)
280 #define warn_noalloc()                          \
281     __warn_noalloc(__LINE__, __func__)
282 #define warn_timeout()                          \
283     __warn_timeout(__LINE__, __func__)
284 #define set_invalid(regs)                       \
285     __set_invalid((regs), __LINE__, __func__)
286 #define set_code_invalid(regs, code)                                    \
287     __set_code_invalid((regs), (code) | (__LINE__ << 8), __func__)
288 #define set_unimplemented(regs)                         \
289     __set_unimplemented((regs), __LINE__, __func__)
290 #define set_code_unimplemented(regs, code)                              \
291     __set_code_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
292
293 // kbd.c
294 void kbd_setup(void);
295 void handle_15c2(struct bregs *regs);
296 void process_key(u8 key);
297
298 // mouse.c
299 void mouse_setup(void);
300 void process_mouse(u8 data);
301
302 // system.c
303 extern u32 RamSize;
304 extern u64 RamSizeOver4G;
305 void mathcp_setup(void);
306
307 // serial.c
308 void serial_setup(void);
309 void lpt_setup(void);
310
311 // clock.c
312 #define PIT_TICK_RATE 1193180   // Underlying HZ of PIT
313 #define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
314 static inline int check_tsc(u64 end) {
315     return (s64)(rdtscll() - end) > 0;
316 }
317 void timer_setup(void);
318 void ndelay(u32 count);
319 void udelay(u32 count);
320 void mdelay(u32 count);
321 void nsleep(u32 count);
322 void usleep(u32 count);
323 void msleep(u32 count);
324 u64 calc_future_tsc(u32 msecs);
325 u64 calc_future_tsc_usec(u32 usecs);
326 u32 calc_future_timer_ticks(u32 count);
327 u32 calc_future_timer(u32 msecs);
328 int check_timer(u32 end);
329 void handle_1583(struct bregs *regs);
330 void handle_1586(struct bregs *regs);
331 void useRTC(void);
332 void releaseRTC(void);
333
334 // apm.c
335 void apm_shutdown(void);
336 void handle_1553(struct bregs *regs);
337
338 // pcibios.c
339 void handle_1ab1(struct bregs *regs);
340 void bios32_setup(void);
341
342 // shadow.c
343 void make_bios_writable(void);
344 void make_bios_readonly(void);
345 void make_bios_writable_intel(u16 bdf, u32 pam0);
346 void make_bios_readonly_intel(u16 bdf, u32 pam0);
347 void qemu_prep_reset(void);
348
349 // smm.c
350 void smm_save_and_copy(void);
351 void smm_relocate_and_restore(void);
352
353 // pci_region.c
354 // region allocator. pci region allocates the requested region
355 // sequentially with overflow check.
356 struct pci_region {
357     // The region is [first, last].
358     u32 first;
359     u32 last;
360
361     // The next allocation starts from here.
362     // i.e. [start, cur_first) is allocated.
363     // Right after initialization cur_first == first.
364     u32 cur_first;
365 };
366 // initialize the pci_region of [first, last]
367 // last must not be 0xffffffff
368 void pci_region_init(struct pci_region *r, u32 first, u32 last);
369 // allocate the region of size
370 u32 pci_region_alloc(struct pci_region *r, u32 size);
371 // make the next allocation aligned to align
372 u32 pci_region_align(struct pci_region *r, u32 align);
373 // revert the allocation to addr.
374 void pci_region_revert(struct pci_region *r, u32 addr);
375 // make the allocation fail.
376 u32 pci_region_disable(struct pci_region *r);
377 // returns the current allocation point.
378 u32 pci_region_addr(const struct pci_region *r);
379 // returns the region size.
380 u32 pci_region_size(const struct pci_region *r);
381
382 // pciinit.c
383 extern const u8 pci_irqs[4];
384 void pci_bios_allocate_regions(u16 bdf, void *arg);
385 void pci_setup(void);
386
387 // smm.c
388 void smm_init(void);
389
390 // smp.c
391 extern u32 CountCPUs;
392 extern u32 MaxCountCPUs;
393 void wrmsr_smp(u32 index, u64 val);
394 void smp_probe(void);
395
396 // coreboot.c
397 struct cbfs_file;
398 struct cbfs_file *cbfs_finddatafile(const char *fname);
399 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
400 u32 cbfs_datasize(struct cbfs_file *file);
401 const char *cbfs_filename(struct cbfs_file *file);
402 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
403 void cbfs_run_payload(struct cbfs_file *file);
404 void coreboot_copy_biostable(void);
405 void cbfs_payload_setup(void);
406 void coreboot_setup(void);
407
408 // vgahooks.c
409 extern int VGAbdf;
410 void handle_155f(struct bregs *regs);
411 void vgahook_setup(const char *vendor, const char *part);
412
413 // optionroms.c
414 void call_bcv(u16 seg, u16 ip);
415 void optionrom_setup(void);
416 void vga_setup(void);
417 void s3_resume_vga_init(void);
418 extern u32 RomEnd;
419
420 // bootsplash.c
421 void enable_vga_console(void);
422 void enable_bootsplash(void);
423 void disable_bootsplash(void);
424
425 // resume.c
426 void init_dma(void);
427
428 // pnpbios.c
429 #define PNP_SIGNATURE 0x506e5024 // $PnP
430 u16 get_pnp_offset(void);
431 void pnp_setup(void);
432
433 // pmm.c
434 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
435 void malloc_setup(void);
436 void malloc_finalize(void);
437 void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
438 int pmm_free(void *data);
439 void pmm_setup(void);
440 void pmm_finalize(void);
441 #define PMM_DEFAULT_HANDLE 0xFFFFFFFF
442 // Minimum alignment of malloc'd memory
443 #define MALLOC_MIN_ALIGN 16
444 // Helper functions for memory allocation.
445 static inline void *malloc_low(u32 size) {
446     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
447 }
448 static inline void *malloc_high(u32 size) {
449     return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
450 }
451 static inline void *malloc_fseg(u32 size) {
452     return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
453 }
454 static inline void *malloc_tmplow(u32 size) {
455     return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
456 }
457 static inline void *malloc_tmphigh(u32 size) {
458     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
459 }
460 static inline void *malloc_tmp(u32 size) {
461     void *ret = malloc_tmphigh(size);
462     if (ret)
463         return ret;
464     return malloc_tmplow(size);
465 }
466 static inline void *memalign_low(u32 align, u32 size) {
467     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
468 }
469 static inline void *memalign_high(u32 align, u32 size) {
470     return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
471 }
472 static inline void *memalign_tmplow(u32 align, u32 size) {
473     return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, align);
474 }
475 static inline void *memalign_tmphigh(u32 align, u32 size) {
476     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
477 }
478 static inline void *memalign_tmp(u32 align, u32 size) {
479     void *ret = memalign_tmphigh(align, size);
480     if (ret)
481         return ret;
482     return memalign_tmplow(align, size);
483 }
484 static inline void free(void *data) {
485     pmm_free(data);
486 }
487
488 // mtrr.c
489 void mtrr_setup(void);
490
491 // romlayout.S
492 void reset_vector(void) __noreturn;
493
494 // misc.c
495 extern u8 BiosChecksum;
496
497 // version (auto generated file out/version.c)
498 extern const char VERSION[];
499
500 #endif // util.h