fa30e08e58cb4f1c412f54e97106d8ca04a25ab3
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU GPLv3 license.
6 #ifndef __UTIL_H
7 #define __UTIL_H
8
9 #include "ioport.h" // outb
10 #include "biosvar.h" // struct bregs
11
12 static inline void irq_disable(void)
13 {
14     asm volatile("cli": : :"memory");
15 }
16
17 static inline void irq_enable(void)
18 {
19     asm volatile("sti": : :"memory");
20 }
21
22 static inline unsigned long irq_save(void)
23 {
24     unsigned long flags;
25     asm volatile("pushfl ; popl %0" : "=g" (flags));
26     irq_disable();
27     return flags;
28 }
29
30 static inline void irq_restore(unsigned long flags)
31 {
32     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
33 }
34
35 static inline void cpu_relax(void)
36 {
37     asm volatile("rep ; nop": : :"memory");
38 }
39
40 static inline void nop(void)
41 {
42     asm volatile("nop");
43 }
44
45 static inline void hlt(void)
46 {
47     asm volatile("hlt");
48 }
49
50 static inline void wbinvd(void)
51 {
52     asm volatile("wbinvd");
53 }
54
55 void *memset(void *s, int c, size_t n);
56 void *memcpy(void *d1, const void *s1, size_t len);
57 void *memmove(void *d, const void *s, size_t len);
58
59 static inline void
60 eoi_master_pic()
61 {
62     outb(PIC1_IRQ5, PORT_PIC1);
63 }
64
65 static inline void
66 eoi_both_pics()
67 {
68     outb(PIC2_IRQ13, PORT_PIC2);
69     eoi_master_pic();
70 }
71
72 // Call a function with a specified register state.  Note that on
73 // return, the interrupt enable/disable flag may be altered.
74 static inline
75 void call16(struct bregs *callregs)
76 {
77     asm volatile(
78 #ifdef MODE16
79         "calll __call16\n"
80 #else
81         "calll __call16_from32\n"
82 #endif
83         : "+a" (callregs), "+m" (*callregs)
84         :
85         : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc");
86 }
87
88 static inline
89 void __call16_int(struct bregs *callregs, u16 offset)
90 {
91     callregs->cs = SEG_BIOS;
92     callregs->ip = offset;
93     call16(callregs);
94 }
95
96 #ifdef MODE16
97 #define call16_int(nr, callregs) do {                           \
98         extern void irq_trampoline_ ##nr ();                    \
99         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
100     } while (0)
101 #else
102 #include "../out/rom16.offset.auto.h"
103 #define call16_int(nr, callregs)                                \
104     __call16_int((callregs), OFFSET_irq_trampoline_ ##nr )
105 #endif
106
107 // output.c
108 void BX_PANIC(const char *fmt, ...)
109     __attribute__ ((format (printf, 1, 2)))
110     __attribute__ ((noreturn));
111 void printf(const char *fmt, ...)
112     __attribute__ ((format (printf, 1, 2)));
113 void __dprintf(const char *fmt, ...)
114     __attribute__ ((format (printf, 1, 2)));
115 #define dprintf(lvl, fmt, args...) do {                         \
116         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
117             __dprintf((fmt) , ##args );                         \
118     } while (0)
119 void __debug_enter(const char *fname, struct bregs *regs);
120 void __debug_fail(const char *fname, struct bregs *regs);
121 void __debug_stub(const char *fname, struct bregs *regs);
122 void __debug_isr(const char *fname);
123 #define debug_enter(regs) \
124     __debug_enter(__func__, regs)
125 #define debug_stub(regs) \
126     __debug_stub(__func__, regs)
127 #define debug_isr(regs) \
128     __debug_isr(__func__)
129
130 // Frequently used return codes
131 #define RET_EUNSUPPORTED 0x86
132 static inline void
133 set_success(struct bregs *regs)
134 {
135     set_cf(regs, 0);
136 }
137
138 static inline void
139 set_code_success(struct bregs *regs)
140 {
141     regs->ah = 0;
142     set_cf(regs, 0);
143 }
144
145 static inline void
146 set_fail_silent(struct bregs *regs)
147 {
148     set_cf(regs, 1);
149 }
150
151 static inline void
152 set_code_fail_silent(struct bregs *regs, u8 code)
153 {
154     regs->ah = code;
155     set_cf(regs, 1);
156 }
157
158 void __set_fail(const char *fname, struct bregs *regs);
159 void __set_code_fail(const char *fname, struct bregs *regs, u8 code);
160
161 #define set_fail(regs) \
162     __set_fail(__func__, (regs))
163 #define set_code_fail(regs, code)               \
164     __set_code_fail(__func__, (regs), (code))
165
166 // kbd.c
167 void handle_15c2(struct bregs *regs);
168
169 // serial.c
170 void serial_setup();
171 void lpt_setup();
172
173 // clock.c
174 void timer_setup();
175 int usleep(u32 count);
176 void handle_1583(struct bregs *regs);
177 void handle_1586(struct bregs *regs);
178
179 // apm.c
180 void VISIBLE16 handle_1553(struct bregs *regs);
181
182 // pcibios.c
183 void handle_1ab1(struct bregs *regs);
184
185 // util.c
186 u8 checksum(u8 *far_data, u32 len);
187
188 // shadow.c
189 void make_bios_writable();
190 void make_bios_readonly();
191
192 // rombios32.c
193 void rombios32_init(void);
194
195 // boot.c
196 void printf_bootdev(u16 bootdev);
197
198 // post_menu.c
199 void interactive_bootmenu();
200
201 // coreboot.c
202 void coreboot_fill_map();
203
204 #endif // util.h