blob: cf98857f69d0d354d70bb40b33781842e67781c6 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Basic x86 asm functions and function defs.
2//
Kevin O'Connor5b199ac2009-05-06 23:23:01 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor786502d2008-02-27 10:41:41 -05006#ifndef __UTIL_H
7#define __UTIL_H
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "types.h" // u32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050010
Kevin O'Connor786502d2008-02-27 10:41:41 -050011static inline void irq_disable(void)
12{
13 asm volatile("cli": : :"memory");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050014}
15
Kevin O'Connor786502d2008-02-27 10:41:41 -050016static inline void irq_enable(void)
17{
18 asm volatile("sti": : :"memory");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050019}
20
21static inline unsigned long irq_save(void)
22{
23 unsigned long flags;
24 asm volatile("pushfl ; popl %0" : "=g" (flags));
25 irq_disable();
26 return flags;
27}
28
29static inline void irq_restore(unsigned long flags)
30{
31 asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
32}
33
Kevin O'Connor06ad44e2008-04-05 19:30:02 -040034static inline void cpu_relax(void)
35{
36 asm volatile("rep ; nop": : :"memory");
37}
38
Kevin O'Connoree2efa72009-09-20 15:33:08 -040039// Atomically enable irqs and sleep until an irq; then re-disable irqs.
40static inline void wait_irq(void)
41{
42 asm volatile("sti ; hlt ; cli ; cld": : :"memory");
43}
44
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050045static inline void nop(void)
46{
47 asm volatile("nop");
48}
49
Kevin O'Connor95b2f782008-03-05 19:52:06 -050050static inline void hlt(void)
51{
52 asm volatile("hlt");
53}
54
Kevin O'Connorda4a6482008-06-08 13:48:06 -040055static inline void wbinvd(void)
56{
57 asm volatile("wbinvd");
58}
59
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040060#define CPUID_MSR (1 << 5)
61#define CPUID_APIC (1 << 9)
62#define CPUID_MTRR (1 << 12)
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040063static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
64{
65 asm("cpuid"
66 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
67 : "0" (index));
68}
69
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040070static inline u64 rdmsr(u32 index)
71{
72 u64 ret;
73 asm ("rdmsr" : "=A"(ret) : "c"(index));
74 return ret;
75}
76
77static inline void wrmsr(u32 index, u64 val)
78{
79 asm volatile ("wrmsr" : : "c"(index), "A"(val));
80}
81
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050082static inline u64 rdtscll(void)
83{
84 u64 val;
85 asm volatile("rdtsc" : "=A" (val));
86 return val;
87}
88
Kevin O'Connor0bf92702009-08-01 11:45:37 -040089static inline u32 __ffs(u32 word)
90{
91 asm("bsf %1,%0"
92 : "=r" (word)
93 : "rm" (word));
94 return word;
95}
Kevin O'Connor86916ce2009-11-14 13:34:27 -050096static inline u32 __fls(u32 word)
97{
98 asm("bsr %1,%0"
99 : "=r" (word)
100 : "rm" (word));
101 return word;
102}
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400103
Kevin O'Connor91031ed2009-10-12 09:49:27 -0400104static inline void writel(void *addr, u32 val) {
105 *(volatile u32 *)addr = val;
106}
107static inline void writew(void *addr, u16 val) {
108 *(volatile u16 *)addr = val;
109}
110static inline void writeb(void *addr, u8 val) {
111 *(volatile u8 *)addr = val;
112}
113static inline u32 readl(const void *addr) {
114 return *(volatile const u32 *)addr;
115}
116static inline u16 readw(const void *addr) {
117 return *(volatile const u16 *)addr;
118}
119static inline u8 readb(const void *addr) {
120 return *(volatile const u8 *)addr;
121}
122
Kevin O'Connoreaa2e552009-08-10 00:03:04 -0400123// GDT bit manipulation
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400124#define GDT_BASE(v) ((((u64)(v) & 0xff000000) << 32) \
125 | (((u64)(v) & 0x00ffffff) << 16))
126#define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32) \
127 | (((u64)(v) & 0x0000ffff) << 0))
Kevin O'Connoreaa2e552009-08-10 00:03:04 -0400128#define GDT_CODE (0x9bULL << 40) // Code segment - P,R,A bits also set
129#define GDT_DATA (0x93ULL << 40) // Data segment - W,A bits also set
130#define GDT_B (0x1ULL << 54) // Big flag
131#define GDT_G (0x1ULL << 55) // Granularity flag
132
Kevin O'Connor942d4952009-06-10 22:44:06 -0400133#define call16_simpint(nr, peax, pflags) do { \
Kevin O'Connor0b60a062009-06-15 23:03:05 -0400134 ASSERT16(); \
Kevin O'Connor942d4952009-06-10 22:44:06 -0400135 asm volatile( \
136 "stc\n" \
137 "int %2\n" \
138 "pushfl\n" \
139 "popl %1\n" \
140 "cli\n" \
141 "cld" \
142 : "+a"(*peax), "=r"(*pflags) \
143 : "i"(nr) \
144 : "cc", "memory"); \
145 } while (0)
146
Kevin O'Connora83ff552009-01-01 21:00:59 -0500147// util.c
148inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
Kevin O'Connorc604f2f2009-10-24 19:56:11 -0400149extern struct thread_info MainThread;
Kevin O'Connore9a67bf2009-12-09 20:04:54 -0500150void thread_setup();
Kevin O'Connorc604f2f2009-10-24 19:56:11 -0400151struct thread_info *getCurThread();
Kevin O'Connora5826b52009-10-24 17:57:29 -0400152void run_thread(void (*func)(void*), void *data);
153void wait_threads();
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500154u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
155u8 checksum(void *buf, u32 len);
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400156int memcmp(const void *s1, const void *s2, size_t n);
Kevin O'Connor67823442009-04-13 14:14:51 -0400157size_t strlen(const char *s);
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400158int strcmp(const char *s1, const char *s2);
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400159inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
160inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400161void *memset(void *s, int c, size_t n);
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400162void *memcpy(void *d1, const void *s1, size_t len);
163#if MODE16 == 0
Kevin O'Connor942d4952009-06-10 22:44:06 -0400164#define memcpy __builtin_memcpy
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400165#endif
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500166inline void memcpy_far(u16 d_seg, void *d_far
167 , u16 s_seg, const void *s_far, size_t len);
Kevin O'Connor34036962009-12-05 18:51:53 -0500168void iomemcpy(void *d, const void *s, u32 len);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400169void *memmove(void *d, const void *s, size_t len);
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500170char *strtcpy(char *dest, const char *src, size_t len);
Kevin O'Connor9521e262008-07-04 13:04:29 -0400171struct bregs;
172inline void call16(struct bregs *callregs);
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -0500173inline void call16big(struct bregs *callregs);
Kevin O'Connor9521e262008-07-04 13:04:29 -0400174inline void __call16_int(struct bregs *callregs, u16 offset);
Kevin O'Connor3a47a312008-03-01 14:46:37 -0500175#define call16_int(nr, callregs) do { \
176 extern void irq_trampoline_ ##nr (); \
Kevin O'Connor117fc212008-04-13 18:17:02 -0400177 __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500178 } while (0)
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400179void yield();
180void biosusleep(u32 usec);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500181int get_keystroke(int msec);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500182
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500183// output.c
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400184void debug_serial_setup();
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500185void panic(const char *fmt, ...)
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400186 __attribute__ ((format (printf, 1, 2)))
187 __attribute__ ((noreturn));
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400188void printf(const char *fmt, ...)
189 __attribute__ ((format (printf, 1, 2)));
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400190void __dprintf(const char *fmt, ...)
191 __attribute__ ((format (printf, 1, 2)));
Kevin O'Connor2be312c2009-11-24 09:37:53 -0500192int snprintf(char *str, size_t size, const char *fmt, ...)
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400193 __attribute__ ((format (printf, 3, 4)));
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400194#define dprintf(lvl, fmt, args...) do { \
195 if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \
196 __dprintf((fmt) , ##args ); \
197 } while (0)
Kevin O'Connor05600342009-01-02 13:10:58 -0500198void __debug_enter(struct bregs *regs, const char *fname);
199void __debug_stub(struct bregs *regs, int lineno, const char *fname);
Kevin O'Connored128492008-03-11 11:14:59 -0400200void __debug_isr(const char *fname);
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400201#define debug_enter(regs, lvl) do { \
202 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
Kevin O'Connor05600342009-01-02 13:10:58 -0500203 __debug_enter((regs), __func__); \
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400204 } while (0)
205#define debug_isr(lvl) do { \
206 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
207 __debug_isr(__func__); \
208 } while (0)
Kevin O'Connor05600342009-01-02 13:10:58 -0500209#define debug_stub(regs) \
210 __debug_stub((regs), __LINE__, __func__)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400211void hexdump(const void *d, int len);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500212
213// kbd.c
Kevin O'Connorefda97d2008-07-04 13:10:12 -0400214void kbd_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500215void handle_15c2(struct bregs *regs);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400216void process_key(u8 key);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500217
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400218// mouse.c
219void mouse_setup();
220
221// system.c
Kevin O'Connore7916362008-12-28 22:03:17 -0500222extern u32 RamSize;
223extern u64 RamSizeOver4G;
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400224void mathcp_setup();
225
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400226// serial.c
227void serial_setup();
228void lpt_setup();
229
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500230// clock.c
Kevin O'Connor89eb6242009-10-22 22:30:37 -0400231static inline int check_time(u64 end) {
232 return (s64)(rdtscll() - end) > 0;
233}
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400234void timer_setup();
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500235void ndelay(u32 count);
236void udelay(u32 count);
237void mdelay(u32 count);
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400238void nsleep(u32 count);
239void usleep(u32 count);
240void msleep(u32 count);
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500241u64 calc_future_tsc(u32 msecs);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400242u64 calc_future_tsc_usec(u32 usecs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500243void handle_1583(struct bregs *regs);
Kevin O'Connor5be04902008-05-18 17:12:06 -0400244void handle_1586(struct bregs *regs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500245
Kevin O'Connor95b2f782008-03-05 19:52:06 -0500246// apm.c
Kevin O'Connor44d65302008-03-08 11:34:46 -0500247void VISIBLE16 handle_1553(struct bregs *regs);
Kevin O'Connor95b2f782008-03-05 19:52:06 -0500248
Kevin O'Connora0dc2962008-03-16 14:29:32 -0400249// pcibios.c
250void handle_1ab1(struct bregs *regs);
251
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400252// shadow.c
253void make_bios_writable();
254void make_bios_readonly();
255
Kevin O'Connor0525d292008-07-04 06:18:30 -0400256// pciinit.c
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400257void pci_setup(void);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500258
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400259// smm.c
260void smm_init();
261
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400262// smp.c
Kevin O'Connor603bfc32009-06-22 20:04:56 -0400263extern u32 CountCPUs;
Kevin O'Connor84705852009-10-08 22:13:15 -0400264extern u32 MaxCountCPUs;
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400265void wrmsr_smp(u32 index, u64 val);
266void smp_probe(void);
Kevin O'Connoracf13742008-11-29 11:19:19 -0500267void smp_probe_setup(void);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400268
Kevin O'Connorc7812932008-06-08 23:08:12 -0400269// coreboot.c
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400270struct cbfs_file;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400271struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
Kevin O'Connor00882592009-08-18 22:21:10 -0400272u32 cbfs_datasize(struct cbfs_file *file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400273const char *cbfs_filename(struct cbfs_file *file);
Kevin O'Connor00882592009-08-18 22:21:10 -0400274int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400275int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
276void cbfs_run_payload(struct cbfs_file *file);
277
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400278void coreboot_copy_biostable();
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400279void coreboot_setup();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400280
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400281// vgahooks.c
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400282extern int VGAbdf;
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400283void handle_155f();
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400284void vgahook_setup(const char *vendor, const char *part);
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400285
Kevin O'Connor714325c2008-11-01 20:32:27 -0400286// optionroms.c
Kevin O'Connor0a924122009-02-08 19:43:47 -0500287void call_bcv(u16 seg, u16 ip);
Kevin O'Connor714325c2008-11-01 20:32:27 -0400288void optionrom_setup();
Kevin O'Connore8f00ee2009-07-04 04:04:36 -0400289void vga_setup();
Kevin O'Connord282af72009-07-04 04:10:32 -0400290void s3_resume_vga_init();
Kevin O'Connore7739302009-07-26 19:16:09 -0400291extern u32 RomEnd;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400292
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500293// resume.c
294void init_dma();
295
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500296// pnpbios.c
297#define PNP_SIGNATURE 0x506e5024 // $PnP
298u16 get_pnp_offset();
299void pnp_setup();
300
Kevin O'Connore54ee382009-07-26 19:33:13 -0400301// pmm.c
Kevin O'Connor415d4292009-08-30 19:19:31 -0400302extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400303void malloc_setup();
304void malloc_finalize();
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400305void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
306int pmm_free(void *data);
Kevin O'Connore54ee382009-07-26 19:33:13 -0400307void pmm_setup();
308void pmm_finalize();
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400309#define PMM_DEFAULT_HANDLE 0xFFFFFFFF
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400310// Minimum alignment of malloc'd memory
311#define MALLOC_MIN_ALIGN 16
Kevin O'Connor415d4292009-08-30 19:19:31 -0400312// Helper functions for memory allocation.
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400313static inline void *malloc_low(u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400314 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400315}
Kevin O'Connor415d4292009-08-30 19:19:31 -0400316static inline void *malloc_high(u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400317 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
Kevin O'Connor415d4292009-08-30 19:19:31 -0400318}
319static inline void *malloc_fseg(u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400320 return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
Kevin O'Connor415d4292009-08-30 19:19:31 -0400321}
Kevin O'Connor114592f2009-09-28 21:32:08 -0400322static inline void *malloc_tmphigh(u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400323 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400324}
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400325static inline void *memalign_low(u32 align, u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400326 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
Kevin O'Connor415d4292009-08-30 19:19:31 -0400327}
328static inline void *memalign_high(u32 align, u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400329 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
Kevin O'Connor415d4292009-08-30 19:19:31 -0400330}
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400331static inline void *memalign_tmphigh(u32 align, u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400332 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
333}
334static inline void free(void *data) {
335 pmm_free(data);
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400336}
Kevin O'Connore54ee382009-07-26 19:33:13 -0400337
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500338// mtrr.c
339void mtrr_setup(void);
340
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500341// romlayout.S
342void reset_vector() __attribute__ ((noreturn));
343
Kevin O'Connor30853762009-01-17 18:49:20 -0500344// misc.c
345extern u8 BiosChecksum;
346
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400347// version (auto generated file out/version.c)
348extern const char VERSION[];
349
Kevin O'Connor786502d2008-02-27 10:41:41 -0500350#endif // util.h