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)
97 // GDT bit manipulation
98 #define GDT_BASE(v) ((((u64)(v) & 0xff000000) << 32) \
99 | (((u64)(v) & 0x00ffffff) << 16))
100 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32) \
101 | (((u64)(v) & 0x0000ffff) << 0))
102 #define GDT_CODE (0x9bULL << 40) // Code segment - P,R,A bits also set
103 #define GDT_DATA (0x93ULL << 40) // Data segment - W,A bits also set
104 #define GDT_B (0x1ULL << 54) // Big flag
105 #define GDT_G (0x1ULL << 55) // Granularity flag
107 #define call16_simpint(nr, peax, pflags) do { \
116 : "+a"(*peax), "=r"(*pflags) \
122 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
123 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
124 u8 checksum(void *buf, u32 len);
125 int memcmp(const void *s1, const void *s2, size_t n);
126 size_t strlen(const char *s);
127 int strcmp(const char *s1, const char *s2);
128 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
129 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
130 void *memset(void *s, int c, size_t n);
131 void *memcpy(void *d1, const void *s1, size_t len);
133 #define memcpy __builtin_memcpy
135 inline void memcpy_far(u16 d_seg, void *d_far
136 , u16 s_seg, const void *s_far, size_t len);
137 void *memmove(void *d, const void *s, size_t len);
138 char *strtcpy(char *dest, const char *src, size_t len);
140 inline void call16(struct bregs *callregs);
141 inline void call16big(struct bregs *callregs);
142 inline void __call16_int(struct bregs *callregs, u16 offset);
143 #define call16_int(nr, callregs) do { \
144 extern void irq_trampoline_ ##nr (); \
145 __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \
147 void usleep(u32 usec);
148 int get_keystroke(int msec);
151 void debug_serial_setup();
152 void panic(const char *fmt, ...)
153 __attribute__ ((format (printf, 1, 2)))
154 __attribute__ ((noreturn));
155 void printf(const char *fmt, ...)
156 __attribute__ ((format (printf, 1, 2)));
157 void __dprintf(const char *fmt, ...)
158 __attribute__ ((format (printf, 1, 2)));
159 #define dprintf(lvl, fmt, args...) do { \
160 if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \
161 __dprintf((fmt) , ##args ); \
163 void __debug_enter(struct bregs *regs, const char *fname);
164 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
165 void __debug_isr(const char *fname);
166 #define debug_enter(regs, lvl) do { \
167 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
168 __debug_enter((regs), __func__); \
170 #define debug_isr(lvl) do { \
171 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
172 __debug_isr(__func__); \
174 #define debug_stub(regs) \
175 __debug_stub((regs), __LINE__, __func__)
176 void hexdump(const void *d, int len);
180 void handle_15c2(struct bregs *regs);
181 void process_key(u8 key);
188 extern u64 RamSizeOver4G;
197 void ndelay(u32 count);
198 void udelay(u32 count);
199 void mdelay(u32 count);
200 u64 calc_future_tsc(u32 msecs);
201 void handle_1583(struct bregs *regs);
202 void handle_1586(struct bregs *regs);
205 void VISIBLE16 handle_1553(struct bregs *regs);
208 void handle_1ab1(struct bregs *regs);
211 void make_bios_writable();
212 void make_bios_readonly();
215 void pci_setup(void);
221 extern u32 CountCPUs;
222 void wrmsr_smp(u32 index, u64 val);
223 void smp_probe(void);
224 void smp_probe_setup(void);
227 void smbios_init(void);
231 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
232 u32 cbfs_datasize(struct cbfs_file *file);
233 const char *cbfs_filename(struct cbfs_file *file);
234 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
235 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
236 void cbfs_run_payload(struct cbfs_file *file);
238 void coreboot_copy_biostable();
239 void coreboot_setup();
244 void vgahook_setup(const char *vendor, const char *part);
247 void call_bcv(u16 seg, u16 ip);
248 void optionrom_setup();
250 void s3_resume_vga_init();
257 #define PNP_SIGNATURE 0x506e5024 // $PnP
258 u16 get_pnp_offset();
262 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
263 void *zone_malloc(struct zone_s *zone, u32 size, u32 align);
264 void *zone_malloc_low(u32 size, u32 align);
266 void malloc_finalize();
269 // Minimum alignment of malloc'd memory
270 #define MALLOC_MIN_ALIGN 16
271 // Helper functions for memory allocation.
272 static inline void *malloc_low(u32 size) {
273 return zone_malloc_low(size, MALLOC_MIN_ALIGN);
275 static inline void *malloc_high(u32 size) {
276 return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
278 static inline void *malloc_fseg(u32 size) {
279 return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
281 static inline void *malloc_tmphigh(u32 size) {
282 return zone_malloc(&ZoneTmpHigh, size, MALLOC_MIN_ALIGN);
284 static inline void *memalign_tmphigh(u32 align, u32 size) {
285 return zone_malloc(&ZoneTmpHigh, size, align);
287 static inline void *memalign_high(u32 align, u32 size) {
288 return zone_malloc(&ZoneHigh, size, align);
292 void mtrr_setup(void);
295 void reset_vector() __attribute__ ((noreturn));
298 extern u8 BiosChecksum;
301 extern int irq0override;
303 // version (auto generated file out/version.c)
304 extern const char VERSION[];