1 // Basic x86 asm functions and function defs.
3 // Copyright (C) 2008,2009 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 unsigned long irq_save(void)
24 asm volatile("pushfl ; popl %0" : "=g" (flags): :"memory");
29 static inline void irq_restore(unsigned long flags)
31 asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
34 static inline void cpu_relax(void)
36 asm volatile("rep ; nop": : :"memory");
39 static inline void nop(void)
44 static inline void hlt(void)
46 asm volatile("hlt": : :"memory");
49 static inline void wbinvd(void)
51 asm volatile("wbinvd": : :"memory");
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 u64 rdmsr(u32 index)
67 asm ("rdmsr" : "=A"(ret) : "c"(index));
71 static inline void wrmsr(u32 index, u64 val)
73 asm volatile ("wrmsr" : : "c"(index), "A"(val));
76 static inline u64 rdtscll(void)
79 asm volatile("rdtsc" : "=A" (val));
83 static inline u32 __ffs(u32 word)
90 static inline u32 __fls(u32 word)
98 static inline u16 __htons_constant(u16 val) {
99 return (val<<8) | (val>>8);
101 static inline u32 __htonl_constant(u32 val) {
102 return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
104 static inline u32 __htonl(u32 val) {
105 asm("bswapl %0" : "+r"(val));
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)
113 static inline u16 cpu_to_le16(u16 x)
118 static inline u32 cpu_to_le32(u32 x)
123 static inline u32 getesp(void) {
125 asm("movl %%esp, %0" : "=rm"(esp));
129 static inline void writel(void *addr, u32 val) {
130 *(volatile u32 *)addr = val;
132 static inline void writew(void *addr, u16 val) {
133 *(volatile u16 *)addr = val;
135 static inline void writeb(void *addr, u8 val) {
136 *(volatile u8 *)addr = val;
138 static inline u32 readl(const void *addr) {
139 return *(volatile const u32 *)addr;
141 static inline u16 readw(const void *addr) {
142 return *(volatile const u16 *)addr;
144 static inline u8 readb(const void *addr) {
145 return *(volatile const u8 *)addr;
148 #define call16_simpint(nr, peax, pflags) do { \
160 : "+a"(*peax), "=c"(*pflags) \
162 : "ebx", "edx", "esi", "edi", "cc", "memory"); \
165 // GDT bit manipulation
166 #define GDT_BASE(v) ((((u64)(v) & 0xff000000) << 32) \
167 | (((u64)(v) & 0x00ffffff) << 16))
168 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32) \
169 | (((u64)(v) & 0x0000ffff) << 0))
170 #define GDT_CODE (0x9bULL << 40) // Code segment - P,R,A bits also set
171 #define GDT_DATA (0x93ULL << 40) // Data segment - W,A bits also set
172 #define GDT_B (0x1ULL << 54) // Big flag
173 #define GDT_G (0x1ULL << 55) // Granularity flag
182 inline void call16(struct bregs *callregs);
183 inline void call16big(struct bregs *callregs);
184 inline void __call16_int(struct bregs *callregs, u16 offset);
185 #define call16_int(nr, callregs) do { \
186 extern void irq_trampoline_ ##nr (); \
187 __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \
189 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
190 u8 checksum(void *buf, u32 len);
191 size_t strlen(const char *s);
192 int memcmp(const void *s1, const void *s2, size_t n);
193 int strcmp(const char *s1, const char *s2);
194 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
195 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
196 void *memset(void *s, int c, size_t n);
197 inline void memcpy_far(u16 d_seg, void *d_far
198 , u16 s_seg, const void *s_far, size_t len);
199 void memcpy_fl(void *d_fl, const void *s_fl, size_t len);
200 void *memcpy(void *d1, const void *s1, size_t len);
202 #define memcpy __builtin_memcpy
204 void iomemcpy(void *d, const void *s, u32 len);
205 void *memmove(void *d, const void *s, size_t len);
206 char *strtcpy(char *dest, const char *src, size_t len);
207 int get_keystroke(int msec);
210 inline u32 stack_hop(u32 eax, u32 edx, void *func);
211 extern struct thread_info MainThread;
212 void thread_setup(void);
213 struct thread_info *getCurThread(void);
216 void run_thread(void (*func)(void*), void *data);
217 void wait_threads(void);
218 struct mutex_s { u32 isLocked; };
219 void mutex_lock(struct mutex_s *mutex);
220 void mutex_unlock(struct mutex_s *mutex);
221 void start_preempt(void);
222 void finish_preempt(void);
223 int wait_preempt(void);
224 void check_preempt(void);
227 void debug_serial_setup(void);
228 void panic(const char *fmt, ...)
229 __attribute__ ((format (printf, 1, 2))) __noreturn;
230 void printf(const char *fmt, ...)
231 __attribute__ ((format (printf, 1, 2)));
232 int snprintf(char *str, size_t size, const char *fmt, ...)
233 __attribute__ ((format (printf, 3, 4)));
234 void __dprintf(const char *fmt, ...)
235 __attribute__ ((format (printf, 1, 2)));
236 void __debug_enter(struct bregs *regs, const char *fname);
237 void __debug_isr(const char *fname);
238 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
239 void __warn_invalid(struct bregs *regs, int lineno, const char *fname);
240 void __warn_unimplemented(struct bregs *regs, int lineno, const char *fname);
241 void __warn_internalerror(int lineno, const char *fname);
242 void __warn_noalloc(int lineno, const char *fname);
243 void __warn_timeout(int lineno, const char *fname);
244 void __set_invalid(struct bregs *regs, int lineno, const char *fname);
245 void __set_unimplemented(struct bregs *regs, int lineno, const char *fname);
246 void __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname);
247 void __set_code_unimplemented(struct bregs *regs, u32 linecode
248 , const char *fname);
249 void hexdump(const void *d, int len);
251 #define dprintf(lvl, fmt, args...) do { \
252 if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \
253 __dprintf((fmt) , ##args ); \
255 #define debug_enter(regs, lvl) do { \
256 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
257 __debug_enter((regs), __func__); \
259 #define debug_isr(lvl) do { \
260 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
261 __debug_isr(__func__); \
263 #define debug_stub(regs) \
264 __debug_stub((regs), __LINE__, __func__)
265 #define warn_invalid(regs) \
266 __warn_invalid((regs), __LINE__, __func__)
267 #define warn_unimplemented(regs) \
268 __warn_unimplemented((regs), __LINE__, __func__)
269 #define warn_internalerror() \
270 __warn_internalerror(__LINE__, __func__)
271 #define warn_noalloc() \
272 __warn_noalloc(__LINE__, __func__)
273 #define warn_timeout() \
274 __warn_timeout(__LINE__, __func__)
275 #define set_invalid(regs) \
276 __set_invalid((regs), __LINE__, __func__)
277 #define set_code_invalid(regs, code) \
278 __set_code_invalid((regs), (code) | (__LINE__ << 8), __func__)
279 #define set_unimplemented(regs) \
280 __set_unimplemented((regs), __LINE__, __func__)
281 #define set_code_unimplemented(regs, code) \
282 __set_code_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
285 void kbd_setup(void);
286 void handle_15c2(struct bregs *regs);
287 void process_key(u8 key);
290 void mouse_setup(void);
291 void process_mouse(u8 data);
295 extern u64 RamSizeOver4G;
296 void mathcp_setup(void);
299 void serial_setup(void);
300 void lpt_setup(void);
303 #define PIT_TICK_RATE 1193180 // Underlying HZ of PIT
304 #define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
305 static inline int check_tsc(u64 end) {
306 return (s64)(rdtscll() - end) > 0;
308 void timer_setup(void);
309 void ndelay(u32 count);
310 void udelay(u32 count);
311 void mdelay(u32 count);
312 void nsleep(u32 count);
313 void usleep(u32 count);
314 void msleep(u32 count);
315 u64 calc_future_tsc(u32 msecs);
316 u64 calc_future_tsc_usec(u32 usecs);
317 u32 calc_future_timer_ticks(u32 count);
318 u32 calc_future_timer(u32 msecs);
319 int check_timer(u32 end);
320 void handle_1583(struct bregs *regs);
321 void handle_1586(struct bregs *regs);
323 void releaseRTC(void);
326 void handle_1553(struct bregs *regs);
329 void handle_1ab1(struct bregs *regs);
330 void bios32_setup(void);
333 void make_bios_writable(void);
334 void make_bios_readonly(void);
337 extern const u8 pci_irqs[4];
338 void pci_bios_allocate_regions(u16 bdf, void *arg);
339 void pci_setup(void);
345 extern u32 CountCPUs;
346 extern u32 MaxCountCPUs;
347 void wrmsr_smp(u32 index, u64 val);
348 void smp_probe(void);
349 void smp_probe_setup(void);
353 struct cbfs_file *cbfs_finddatafile(const char *fname);
354 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
355 u32 cbfs_datasize(struct cbfs_file *file);
356 const char *cbfs_filename(struct cbfs_file *file);
357 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
358 void cbfs_run_payload(struct cbfs_file *file);
359 void coreboot_copy_biostable(void);
360 void coreboot_setup(void);
364 void handle_155f(struct bregs *regs);
365 void vgahook_setup(const char *vendor, const char *part);
368 void call_bcv(u16 seg, u16 ip);
369 void optionrom_setup(void);
370 void vga_setup(void);
371 void s3_resume_vga_init(void);
375 void enable_vga_console(void);
376 void disable_bootsplash(void);
382 #define PNP_SIGNATURE 0x506e5024 // $PnP
383 u16 get_pnp_offset(void);
384 void pnp_setup(void);
387 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
388 void malloc_setup(void);
389 void malloc_finalize(void);
390 void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
391 int pmm_free(void *data);
392 void pmm_setup(void);
393 void pmm_finalize(void);
394 #define PMM_DEFAULT_HANDLE 0xFFFFFFFF
395 // Minimum alignment of malloc'd memory
396 #define MALLOC_MIN_ALIGN 16
397 // Helper functions for memory allocation.
398 static inline void *malloc_low(u32 size) {
399 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
401 static inline void *malloc_high(u32 size) {
402 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
404 static inline void *malloc_fseg(u32 size) {
405 return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
407 static inline void *malloc_tmphigh(u32 size) {
408 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
410 static inline void *malloc_tmp(u32 size) {
411 void *ret = malloc_tmphigh(size);
414 return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
416 static inline void *memalign_low(u32 align, u32 size) {
417 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
419 static inline void *memalign_high(u32 align, u32 size) {
420 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
422 static inline void *memalign_tmphigh(u32 align, u32 size) {
423 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
425 static inline void free(void *data) {
430 void mtrr_setup(void);
433 void reset_vector(void) __noreturn;
436 extern u8 BiosChecksum;
438 // version (auto generated file out/version.c)
439 extern const char VERSION[];