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 static inline void nop(void)
44 static inline void hlt(void)
49 static inline void wbinvd(void)
51 asm volatile("wbinvd");
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)
91 // GDT bit manipulation
92 #define GDT_BASE(v) ((((u64)(v) & 0xff000000) << 32) \
93 | (((u64)(v) & 0x00ffffff) << 16))
94 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32) \
95 | (((u64)(v) & 0x0000ffff) << 0))
96 #define GDT_CODE (0x9bULL << 40) // Code segment - P,R,A bits also set
97 #define GDT_DATA (0x93ULL << 40) // Data segment - W,A bits also set
98 #define GDT_B (0x1ULL << 54) // Big flag
99 #define GDT_G (0x1ULL << 55) // Granularity flag
101 #define call16_simpint(nr, peax, pflags) do { \
110 : "+a"(*peax), "=r"(*pflags) \
116 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
117 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
118 u8 checksum(void *buf, u32 len);
119 int memcmp(const void *s1, const void *s2, size_t n);
120 size_t strlen(const char *s);
121 int strcmp(const char *s1, const char *s2);
122 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
123 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
124 void *memset(void *s, int c, size_t n);
125 void *memcpy(void *d1, const void *s1, size_t len);
127 #define memcpy __builtin_memcpy
129 inline void memcpy_far(u16 d_seg, void *d_far
130 , u16 s_seg, const void *s_far, size_t len);
131 void *memmove(void *d, const void *s, size_t len);
132 char *strtcpy(char *dest, const char *src, size_t len);
134 inline void call16(struct bregs *callregs);
135 inline void call16big(struct bregs *callregs);
136 inline void __call16_int(struct bregs *callregs, u16 offset);
137 #define call16_int(nr, callregs) do { \
138 extern void irq_trampoline_ ##nr (); \
139 __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \
141 void usleep(u32 usec);
142 int get_keystroke(int msec);
145 void debug_serial_setup();
146 void panic(const char *fmt, ...)
147 __attribute__ ((format (printf, 1, 2)))
148 __attribute__ ((noreturn));
149 void printf(const char *fmt, ...)
150 __attribute__ ((format (printf, 1, 2)));
151 void __dprintf(const char *fmt, ...)
152 __attribute__ ((format (printf, 1, 2)));
153 #define dprintf(lvl, fmt, args...) do { \
154 if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \
155 __dprintf((fmt) , ##args ); \
157 void __debug_enter(struct bregs *regs, const char *fname);
158 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
159 void __debug_isr(const char *fname);
160 #define debug_enter(regs, lvl) do { \
161 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
162 __debug_enter((regs), __func__); \
164 #define debug_isr(lvl) do { \
165 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
166 __debug_isr(__func__); \
168 #define debug_stub(regs) \
169 __debug_stub((regs), __LINE__, __func__)
170 void hexdump(void *d, int len);
174 void handle_15c2(struct bregs *regs);
181 extern u64 RamSizeOver4G;
190 void ndelay(u32 count);
191 void udelay(u32 count);
192 void mdelay(u32 count);
193 u64 calc_future_tsc(u32 msecs);
194 void handle_1583(struct bregs *regs);
195 void handle_1586(struct bregs *regs);
198 void VISIBLE16 handle_1553(struct bregs *regs);
201 void handle_1ab1(struct bregs *regs);
204 void make_bios_writable();
205 void make_bios_readonly();
208 void pci_setup(void);
214 extern u32 CountCPUs;
215 void wrmsr_smp(u32 index, u64 val);
216 void smp_probe(void);
217 void smp_probe_setup(void);
220 void smbios_init(void);
224 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
225 u32 cbfs_datasize(struct cbfs_file *file);
226 const char *cbfs_filename(struct cbfs_file *file);
227 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
228 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
229 void cbfs_run_payload(struct cbfs_file *file);
231 void coreboot_copy_biostable();
232 void coreboot_setup();
237 void vgahook_setup(const char *vendor, const char *part);
240 void call_bcv(u16 seg, u16 ip);
241 void optionrom_setup();
243 void s3_resume_vga_init();
250 #define PNP_SIGNATURE 0x506e5024 // $PnP
251 u16 get_pnp_offset();
255 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
256 void *zone_malloc(struct zone_s *zone, u32 size, u32 align);
258 void malloc_finalize();
261 // Minimum alignment of malloc'd memory
262 #define MALLOC_MIN_ALIGN 16
263 // Helper functions for memory allocation.
264 static inline void *malloc_high(u32 size) {
265 return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
267 static inline void *malloc_fseg(u32 size) {
268 return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
270 static inline void *memalign_tmphigh(u32 align, u32 size) {
271 return zone_malloc(&ZoneTmpHigh, size, align);
273 static inline void *memalign_high(u32 align, u32 size) {
274 return zone_malloc(&ZoneHigh, size, align);
278 void mtrr_setup(void);
281 void reset_vector() __attribute__ ((noreturn));
284 extern u8 BiosChecksum;
287 extern int irq0override;
289 // version (auto generated file out/version.c)
290 extern const char VERSION[];