Add support for gcc v3.x compilers.
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6 #ifndef __UTIL_H
7 #define __UTIL_H
8
9 #include "types.h" // u32
10
11 static inline void irq_disable(void)
12 {
13     asm volatile("cli": : :"memory");
14 }
15
16 static inline void irq_enable(void)
17 {
18     asm volatile("sti": : :"memory");
19 }
20
21 static 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
29 static inline void irq_restore(unsigned long flags)
30 {
31     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
32 }
33
34 static inline void cpu_relax(void)
35 {
36     asm volatile("rep ; nop": : :"memory");
37 }
38
39 static inline void nop(void)
40 {
41     asm volatile("nop");
42 }
43
44 static inline void hlt(void)
45 {
46     asm volatile("hlt");
47 }
48
49 static inline void wbinvd(void)
50 {
51     asm volatile("wbinvd");
52 }
53
54 static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
55 {
56     asm("cpuid"
57         : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
58         : "0" (index));
59 }
60
61 static inline u64 rdtscll(void)
62 {
63     u64 val;
64     asm volatile("rdtsc" : "=A" (val));
65     return val;
66 }
67
68 #define call16_simpint(nr, peax, pflags) do {                           \
69         extern void __force_link_error__call16_simpint_only_in_16bit_mode(); \
70         if (!MODE16)                                                    \
71             __force_link_error__call16_simpint_only_in_16bit_mode();    \
72                                                                         \
73         asm volatile(                                                   \
74             "stc\n"                                                     \
75             "int %2\n"                                                  \
76             "pushfl\n"                                                  \
77             "popl %1\n"                                                 \
78             "cli\n"                                                     \
79             "cld"                                                       \
80             : "+a"(*peax), "=r"(*pflags)                                \
81             : "i"(nr)                                                   \
82             : "cc", "memory");                                          \
83     } while (0)
84
85 // util.c
86 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
87 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
88 u8 checksum(void *buf, u32 len);
89 int memcmp(const void *s1, const void *s2, size_t n);
90 size_t strlen(const char *s);
91 int strcmp(const char *s1, const char *s2);
92 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
93 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
94 void *memset(void *s, int c, size_t n);
95 #define memcpy __builtin_memcpy
96 inline void memcpy_far(u16 d_seg, void *d_far
97                        , u16 s_seg, const void *s_far, size_t len);
98 void *memmove(void *d, const void *s, size_t len);
99 char *strtcpy(char *dest, const char *src, size_t len);
100 struct bregs;
101 inline void call16(struct bregs *callregs);
102 inline void call16big(struct bregs *callregs);
103 inline void __call16_int(struct bregs *callregs, u16 offset);
104 #define call16_int(nr, callregs) do {                           \
105         extern void irq_trampoline_ ##nr ();                    \
106         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
107     } while (0)
108 void usleep(u32 usec);
109 int get_keystroke(int msec);
110
111 // output.c
112 void debug_serial_setup();
113 void panic(const char *fmt, ...)
114     __attribute__ ((format (printf, 1, 2)))
115     __attribute__ ((noreturn));
116 void printf(const char *fmt, ...)
117     __attribute__ ((format (printf, 1, 2)));
118 void __dprintf(const char *fmt, ...)
119     __attribute__ ((format (printf, 1, 2)));
120 #define dprintf(lvl, fmt, args...) do {                         \
121         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
122             __dprintf((fmt) , ##args );                         \
123     } while (0)
124 void __debug_enter(struct bregs *regs, const char *fname);
125 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
126 void __debug_isr(const char *fname);
127 #define debug_enter(regs, lvl) do {                     \
128         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
129             __debug_enter((regs), __func__);            \
130     } while (0)
131 #define debug_isr(lvl) do {                             \
132         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
133             __debug_isr(__func__);                      \
134     } while (0)
135 #define debug_stub(regs)                        \
136     __debug_stub((regs), __LINE__, __func__)
137 void hexdump(void *d, int len);
138
139 // kbd.c
140 void kbd_setup();
141 void handle_15c2(struct bregs *regs);
142
143 // mouse.c
144 void mouse_setup();
145
146 // system.c
147 extern u32 RamSize;
148 extern u64 RamSizeOver4G;
149 void mathcp_setup();
150
151 // serial.c
152 void serial_setup();
153 void lpt_setup();
154
155 // clock.c
156 void timer_setup();
157 void ndelay(u32 count);
158 void udelay(u32 count);
159 void mdelay(u32 count);
160 u64 calc_future_tsc(u32 msecs);
161 void handle_1583(struct bregs *regs);
162 void handle_1586(struct bregs *regs);
163
164 // apm.c
165 void VISIBLE16 handle_1553(struct bregs *regs);
166
167 // pcibios.c
168 void handle_1ab1(struct bregs *regs);
169
170 // shadow.c
171 void make_bios_writable();
172 void make_bios_readonly();
173
174 // pciinit.c
175 void pci_bios_setup(void);
176
177 // smm.c
178 void smm_init();
179
180 // smpdetect.c
181 int smp_probe(void);
182 void smp_probe_setup(void);
183
184 // smbios.c
185 void smbios_init(void);
186
187 // coreboot.c
188 const char *cbfs_findNprefix(const char *prefix, int n);
189 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
190 void cbfs_run_payload(const char *filename);
191 struct cbfs_file;
192 struct cbfs_file *cbfs_copy_gen_optionrom(void *dst, u32 maxlen
193                                           , struct cbfs_file *file);
194 void coreboot_setup();
195
196 // vgahooks.c
197 void handle_155f();
198
199 // optionroms.c
200 void call_bcv(u16 seg, u16 ip);
201 void vga_setup();
202 void optionrom_setup();
203
204 // resume.c
205 void init_dma();
206
207 // pnpbios.c
208 #define PNP_SIGNATURE 0x506e5024 // $PnP
209 u16 get_pnp_offset();
210 void pnp_setup();
211
212 // mtrr.c
213 void mtrr_setup(void);
214
215 // romlayout.S
216 void reset_vector() __attribute__ ((noreturn));
217
218 // misc.c
219 extern u8 BiosChecksum;
220
221 #endif // util.h