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));
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 // Atomically enable irqs and sleep until an irq; then re-disable irqs.
40 static inline void wait_irq(void)
42 asm volatile("sti ; hlt ; cli ; cld": : :"memory");
45 static inline void nop(void)
50 static inline void hlt(void)
55 static inline void wbinvd(void)
57 asm volatile("wbinvd");
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)
66 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
70 static inline u64 rdmsr(u32 index)
73 asm ("rdmsr" : "=A"(ret) : "c"(index));
77 static inline void wrmsr(u32 index, u64 val)
79 asm volatile ("wrmsr" : : "c"(index), "A"(val));
82 static inline u64 rdtscll(void)
85 asm volatile("rdtsc" : "=A" (val));
89 static inline u32 __ffs(u32 word)
96 static inline u32 __fls(u32 word)
104 static inline u32 getesp() {
106 asm("movl %%esp, %0" : "=rm"(esp));
110 static inline void writel(void *addr, u32 val) {
111 *(volatile u32 *)addr = val;
113 static inline void writew(void *addr, u16 val) {
114 *(volatile u16 *)addr = val;
116 static inline void writeb(void *addr, u8 val) {
117 *(volatile u8 *)addr = val;
119 static inline u32 readl(const void *addr) {
120 return *(volatile const u32 *)addr;
122 static inline u16 readw(const void *addr) {
123 return *(volatile const u16 *)addr;
125 static inline u8 readb(const void *addr) {
126 return *(volatile const u8 *)addr;
129 // GDT bit manipulation
130 #define GDT_BASE(v) ((((u64)(v) & 0xff000000) << 32) \
131 | (((u64)(v) & 0x00ffffff) << 16))
132 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32) \
133 | (((u64)(v) & 0x0000ffff) << 0))
134 #define GDT_CODE (0x9bULL << 40) // Code segment - P,R,A bits also set
135 #define GDT_DATA (0x93ULL << 40) // Data segment - W,A bits also set
136 #define GDT_B (0x1ULL << 54) // Big flag
137 #define GDT_G (0x1ULL << 55) // Granularity flag
139 #define call16_simpint(nr, peax, pflags) do { \
148 : "+a"(*peax), "=r"(*pflags) \
155 inline void call16(struct bregs *callregs);
156 inline void call16big(struct bregs *callregs);
157 inline void __call16_int(struct bregs *callregs, u16 offset);
158 #define call16_int(nr, callregs) do { \
159 extern void irq_trampoline_ ##nr (); \
160 __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \
163 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
164 u8 checksum(void *buf, u32 len);
165 size_t strlen(const char *s);
166 int memcmp(const void *s1, const void *s2, size_t n);
167 int strcmp(const char *s1, const char *s2);
168 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
169 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
170 void *memset(void *s, int c, size_t n);
171 inline void memcpy_far(u16 d_seg, void *d_far
172 , u16 s_seg, const void *s_far, size_t len);
173 void *memcpy(void *d1, const void *s1, size_t len);
175 #define memcpy __builtin_memcpy
177 void iomemcpy(void *d, const void *s, u32 len);
178 void *memmove(void *d, const void *s, size_t len);
179 char *strtcpy(char *dest, const char *src, size_t len);
180 void biosusleep(u32 usec);
181 int get_keystroke(int msec);
184 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
185 extern struct thread_info MainThread;
187 struct thread_info *getCurThread();
189 void run_thread(void (*func)(void*), void *data);
193 void debug_serial_setup();
194 void panic(const char *fmt, ...)
195 __attribute__ ((format (printf, 1, 2)))
196 __attribute__ ((noreturn));
197 void printf(const char *fmt, ...)
198 __attribute__ ((format (printf, 1, 2)));
199 void __dprintf(const char *fmt, ...)
200 __attribute__ ((format (printf, 1, 2)));
201 int snprintf(char *str, size_t size, const char *fmt, ...)
202 __attribute__ ((format (printf, 3, 4)));
203 #define dprintf(lvl, fmt, args...) do { \
204 if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \
205 __dprintf((fmt) , ##args ); \
207 void __debug_enter(struct bregs *regs, const char *fname);
208 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
209 void __debug_isr(const char *fname);
210 #define debug_enter(regs, lvl) do { \
211 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
212 __debug_enter((regs), __func__); \
214 #define debug_isr(lvl) do { \
215 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
216 __debug_isr(__func__); \
218 #define debug_stub(regs) \
219 __debug_stub((regs), __LINE__, __func__)
220 void hexdump(const void *d, int len);
224 void handle_15c2(struct bregs *regs);
225 void process_key(u8 key);
229 void process_mouse(u8 data);
233 extern u64 RamSizeOver4G;
241 static inline int check_time(u64 end) {
242 return (s64)(rdtscll() - end) > 0;
245 void ndelay(u32 count);
246 void udelay(u32 count);
247 void mdelay(u32 count);
248 void nsleep(u32 count);
249 void usleep(u32 count);
250 void msleep(u32 count);
251 u64 calc_future_tsc(u32 msecs);
252 u64 calc_future_tsc_usec(u32 usecs);
253 void handle_1583(struct bregs *regs);
254 void handle_1586(struct bregs *regs);
257 void VISIBLE16 handle_1553(struct bregs *regs);
260 void handle_1ab1(struct bregs *regs);
263 void make_bios_writable();
264 void make_bios_readonly();
267 void pci_setup(void);
273 extern u32 CountCPUs;
274 extern u32 MaxCountCPUs;
275 void wrmsr_smp(u32 index, u64 val);
276 void smp_probe(void);
277 void smp_probe_setup(void);
281 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
282 u32 cbfs_datasize(struct cbfs_file *file);
283 const char *cbfs_filename(struct cbfs_file *file);
284 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
285 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
286 void cbfs_run_payload(struct cbfs_file *file);
288 void coreboot_copy_biostable();
289 void coreboot_setup();
294 void vgahook_setup(const char *vendor, const char *part);
297 void call_bcv(u16 seg, u16 ip);
298 void optionrom_setup();
300 void s3_resume_vga_init();
307 #define PNP_SIGNATURE 0x506e5024 // $PnP
308 u16 get_pnp_offset();
312 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
314 void malloc_finalize();
315 void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
316 int pmm_free(void *data);
319 #define PMM_DEFAULT_HANDLE 0xFFFFFFFF
320 // Minimum alignment of malloc'd memory
321 #define MALLOC_MIN_ALIGN 16
322 // Helper functions for memory allocation.
323 static inline void *malloc_low(u32 size) {
324 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
326 static inline void *malloc_high(u32 size) {
327 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
329 static inline void *malloc_fseg(u32 size) {
330 return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
332 static inline void *malloc_tmphigh(u32 size) {
333 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
335 static inline void *memalign_low(u32 align, u32 size) {
336 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
338 static inline void *memalign_high(u32 align, u32 size) {
339 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
341 static inline void *memalign_tmphigh(u32 align, u32 size) {
342 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
344 static inline void free(void *data) {
349 void mtrr_setup(void);
352 void reset_vector() __attribute__ ((noreturn));
355 extern u8 BiosChecksum;
357 // version (auto generated file out/version.c)
358 extern const char VERSION[];