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