1 // Basic x86 asm functions and function defs.
3 // Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
9 #include "types.h" // u32
11 static inline void irq_disable(void)
13 asm volatile("cli": : :"memory");
16 static inline void irq_enable(void)
18 asm volatile("sti": : :"memory");
21 static inline u32 save_flags(void)
24 asm volatile("pushfl ; popl %0" : "=rm" (flags));
28 static inline void restore_flags(u32 flags)
30 asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
33 static inline void cpu_relax(void)
35 asm volatile("rep ; nop": : :"memory");
38 static inline void nop(void)
43 static inline void hlt(void)
45 asm volatile("hlt": : :"memory");
48 static inline void wbinvd(void)
50 asm volatile("wbinvd": : :"memory");
53 #define CPUID_TSC (1 << 4)
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)
60 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
64 static inline u32 getcr0(void) {
66 asm("movl %%cr0, %0" : "=r"(cr0));
69 static inline void setcr0(u32 cr0) {
70 asm("movl %0, %%cr0" : : "r"(cr0));
73 static inline u64 rdmsr(u32 index)
76 asm ("rdmsr" : "=A"(ret) : "c"(index));
80 static inline void wrmsr(u32 index, u64 val)
82 asm volatile ("wrmsr" : : "c"(index), "A"(val));
85 static inline u64 rdtscll(void)
88 asm volatile("rdtsc" : "=A" (val));
92 static inline u32 __ffs(u32 word)
99 static inline u32 __fls(u32 word)
107 static inline u16 __htons_constant(u16 val) {
108 return (val<<8) | (val>>8);
110 static inline u32 __htonl_constant(u32 val) {
111 return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
113 static inline u32 __htonl(u32 val) {
114 asm("bswapl %0" : "+r"(val));
117 #define htonl(x) (__builtin_constant_p((u32)(x)) ? __htonl_constant(x) : __htonl(x))
118 #define ntohl(x) htonl(x)
119 #define htons(x) __htons_constant(x)
120 #define ntohs(x) htons(x)
122 static inline u16 cpu_to_le16(u16 x)
127 static inline u32 cpu_to_le32(u32 x)
132 static inline u32 le32_to_cpu(u32 x)
137 static inline u32 getesp(void) {
139 asm("movl %%esp, %0" : "=rm"(esp));
143 static inline void writel(void *addr, u32 val) {
144 *(volatile u32 *)addr = val;
146 static inline void writew(void *addr, u16 val) {
147 *(volatile u16 *)addr = val;
149 static inline void writeb(void *addr, u8 val) {
150 *(volatile u8 *)addr = val;
152 static inline u32 readl(const void *addr) {
153 return *(volatile const u32 *)addr;
155 static inline u16 readw(const void *addr) {
156 return *(volatile const u16 *)addr;
158 static inline u8 readb(const void *addr) {
159 return *(volatile const u8 *)addr;
162 #define call16_simpint(nr, peax, pflags) do { \
174 : "+a"(*peax), "=c"(*pflags) \
176 : "ebx", "edx", "esi", "edi", "cc", "memory"); \
180 #define GDT_CODE (0x9bULL << 40) // Code segment - P,R,A bits also set
181 #define GDT_DATA (0x93ULL << 40) // Data segment - W,A bits also set
182 #define GDT_B (0x1ULL << 54) // Big flag
183 #define GDT_G (0x1ULL << 55) // Granularity flag
184 // GDT bits for segment base
185 #define GDT_BASE(v) ((((u64)(v) & 0xff000000) << 32) \
186 | (((u64)(v) & 0x00ffffff) << 16))
187 // GDT bits for segment limit (0-1Meg)
188 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32) \
189 | (((u64)(v) & 0x0000ffff) << 0))
190 // GDT bits for segment limit (0-4Gig in 4K chunks)
191 #define GDT_GRANLIMIT(v) (GDT_G | GDT_LIMIT((v) >> 12))
199 void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
201 inline void call16(struct bregs *callregs);
202 inline void call16big(struct bregs *callregs);
203 inline void __call16_int(struct bregs *callregs, u16 offset);
204 #define call16_int(nr, callregs) do { \
205 extern void irq_trampoline_ ##nr (); \
206 __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \
208 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
209 u8 checksum(void *buf, u32 len);
210 size_t strlen(const char *s);
211 int memcmp(const void *s1, const void *s2, size_t n);
212 int strcmp(const char *s1, const char *s2);
213 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
214 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
215 void *memset(void *s, int c, size_t n);
216 void memset_fl(void *ptr, u8 val, size_t size);
217 inline void memcpy_far(u16 d_seg, void *d_far
218 , u16 s_seg, const void *s_far, size_t len);
219 void memcpy_fl(void *d_fl, const void *s_fl, size_t len);
220 void *memcpy(void *d1, const void *s1, size_t len);
222 #define memcpy __builtin_memcpy
224 void iomemcpy(void *d, const void *s, u32 len);
225 void *memmove(void *d, const void *s, size_t len);
226 char *strtcpy(char *dest, const char *src, size_t len);
227 char *strchr(const char *s, int c);
228 void nullTrailingSpace(char *buf);
229 int get_keystroke(int msec);
232 u32 call32(void *func, u32 eax, u32 errret);
233 inline u32 stack_hop(u32 eax, u32 edx, void *func);
234 extern struct thread_info MainThread;
235 extern int CanPreempt;
236 struct thread_info *getCurThread(void);
239 void run_thread(void (*func)(void*), void *data);
240 void wait_threads(void);
241 struct mutex_s { u32 isLocked; };
242 void mutex_lock(struct mutex_s *mutex);
243 void mutex_unlock(struct mutex_s *mutex);
244 void start_preempt(void);
245 void finish_preempt(void);
246 int wait_preempt(void);
247 void check_preempt(void);
250 void debug_serial_setup(void);
251 void panic(const char *fmt, ...)
252 __attribute__ ((format (printf, 1, 2))) __noreturn;
253 void printf(const char *fmt, ...)
254 __attribute__ ((format (printf, 1, 2)));
255 int snprintf(char *str, size_t size, const char *fmt, ...)
256 __attribute__ ((format (printf, 3, 4)));
257 char * znprintf(size_t size, const char *fmt, ...)
258 __attribute__ ((format (printf, 2, 3)));
259 void __dprintf(const char *fmt, ...)
260 __attribute__ ((format (printf, 1, 2)));
261 void __debug_enter(struct bregs *regs, const char *fname);
262 void __debug_isr(const char *fname);
263 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
264 void __warn_invalid(struct bregs *regs, int lineno, const char *fname);
265 void __warn_unimplemented(struct bregs *regs, int lineno, const char *fname);
266 void __warn_internalerror(int lineno, const char *fname);
267 void __warn_noalloc(int lineno, const char *fname);
268 void __warn_timeout(int lineno, const char *fname);
269 void __set_invalid(struct bregs *regs, int lineno, const char *fname);
270 void __set_unimplemented(struct bregs *regs, int lineno, const char *fname);
271 void __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname);
272 void __set_code_unimplemented(struct bregs *regs, u32 linecode
273 , const char *fname);
274 void hexdump(const void *d, int len);
276 #define dprintf(lvl, fmt, args...) do { \
277 if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \
278 __dprintf((fmt) , ##args ); \
280 #define debug_enter(regs, lvl) do { \
281 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
282 __debug_enter((regs), __func__); \
284 #define debug_isr(lvl) do { \
285 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
286 __debug_isr(__func__); \
288 #define debug_stub(regs) \
289 __debug_stub((regs), __LINE__, __func__)
290 #define warn_invalid(regs) \
291 __warn_invalid((regs), __LINE__, __func__)
292 #define warn_unimplemented(regs) \
293 __warn_unimplemented((regs), __LINE__, __func__)
294 #define warn_internalerror() \
295 __warn_internalerror(__LINE__, __func__)
296 #define warn_noalloc() \
297 __warn_noalloc(__LINE__, __func__)
298 #define warn_timeout() \
299 __warn_timeout(__LINE__, __func__)
300 #define set_invalid(regs) \
301 __set_invalid((regs), __LINE__, __func__)
302 #define set_code_invalid(regs, code) \
303 __set_code_invalid((regs), (code) | (__LINE__ << 8), __func__)
304 #define set_unimplemented(regs) \
305 __set_unimplemented((regs), __LINE__, __func__)
306 #define set_code_unimplemented(regs, code) \
307 __set_code_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
310 void kbd_setup(void);
311 void handle_15c2(struct bregs *regs);
312 void process_key(u8 key);
315 void mouse_setup(void);
316 void process_mouse(u8 data);
320 extern u64 RamSizeOver4G;
321 void mathcp_setup(void);
324 void serial_setup(void);
325 void lpt_setup(void);
328 #define PIT_TICK_RATE 1193180 // Underlying HZ of PIT
329 #define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
330 int check_tsc(u64 end);
331 void timer_setup(void);
332 void ndelay(u32 count);
333 void udelay(u32 count);
334 void mdelay(u32 count);
335 void nsleep(u32 count);
336 void usleep(u32 count);
337 void msleep(u32 count);
338 u64 calc_future_tsc(u32 msecs);
339 u64 calc_future_tsc_usec(u32 usecs);
340 u32 calc_future_timer_ticks(u32 count);
341 u32 calc_future_timer(u32 msecs);
342 int check_timer(u32 end);
343 void handle_1583(struct bregs *regs);
344 void handle_1586(struct bregs *regs);
346 void releaseRTC(void);
349 void apm_shutdown(void);
350 void handle_1553(struct bregs *regs);
353 void handle_1ab1(struct bregs *regs);
354 void bios32_setup(void);
357 void make_bios_writable(void);
358 void make_bios_readonly(void);
359 void qemu_prep_reset(void);
362 extern const u8 pci_irqs[4];
363 void pci_setup(void);
369 extern u32 CountCPUs;
370 extern u32 MaxCountCPUs;
371 void wrmsr_smp(u32 index, u64 val);
372 void smp_probe(void);
375 extern const char *CBvendor, *CBpart;
377 struct cbfs_file *cbfs_finddatafile(const char *fname);
378 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
379 u32 cbfs_datasize(struct cbfs_file *file);
380 const char *cbfs_filename(struct cbfs_file *file);
381 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
382 void cbfs_run_payload(struct cbfs_file *file);
383 void coreboot_copy_biostable(void);
384 void cbfs_payload_setup(void);
385 void coreboot_setup(void);
388 void copy_pir(void *pos);
389 void copy_mptable(void *pos);
390 void copy_acpi_rsdp(void *pos);
391 void copy_smbios(void *pos);
394 void handle_155f(struct bregs *regs);
396 void vgahook_setup(struct pci_device *pci);
399 void call_bcv(u16 seg, u16 ip);
400 void optionrom_setup(void);
401 void vga_setup(void);
402 void s3_resume_vga_init(void);
404 extern int ScreenAndDebug;
407 void enable_vga_console(void);
408 void enable_bootsplash(void);
409 void disable_bootsplash(void);
412 extern int HaveRunPost;
416 #define PNP_SIGNATURE 0x506e5024 // $PnP
417 u16 get_pnp_offset(void);
418 void pnp_setup(void);
421 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
422 void malloc_setup(void);
423 void malloc_fixupreloc(void);
424 void malloc_finalize(void);
425 void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
426 int pmm_free(void *data);
427 void pmm_setup(void);
428 void pmm_finalize(void);
429 #define PMM_DEFAULT_HANDLE 0xFFFFFFFF
430 // Minimum alignment of malloc'd memory
431 #define MALLOC_MIN_ALIGN 16
432 // Helper functions for memory allocation.
433 static inline void *malloc_low(u32 size) {
434 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
436 static inline void *malloc_high(u32 size) {
437 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
439 static inline void *malloc_fseg(u32 size) {
440 return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
442 static inline void *malloc_tmplow(u32 size) {
443 return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
445 static inline void *malloc_tmphigh(u32 size) {
446 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
448 static inline void *malloc_tmp(u32 size) {
449 void *ret = malloc_tmphigh(size);
452 return malloc_tmplow(size);
454 static inline void *memalign_low(u32 align, u32 size) {
455 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
457 static inline void *memalign_high(u32 align, u32 size) {
458 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
460 static inline void *memalign_tmplow(u32 align, u32 size) {
461 return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, align);
463 static inline void *memalign_tmphigh(u32 align, u32 size) {
464 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
466 static inline void *memalign_tmp(u32 align, u32 size) {
467 void *ret = memalign_tmphigh(align, size);
470 return memalign_tmplow(align, size);
472 static inline void free(void *data) {
477 void mtrr_setup(void);
480 void reset_vector(void) __noreturn;
483 extern u8 BiosChecksum;
485 // version (auto generated file out/version.c)
486 extern const char VERSION[];