36e9a30594e44c00cc70c7156267ead172a71e88
[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 nop(void)
36 {
37     asm volatile("nop");
38 }
39
40 static inline void hlt(void)
41 {
42     asm volatile("hlt");
43 }
44
45 #define BX_PANIC(fmt, args...) bprintf(0, fmt , ##args)
46 #define BX_INFO(fmt, args...) bprintf(0, fmt , ##args)
47
48 static inline void
49 memset(void *s, int c, size_t n)
50 {
51     while (n)
52         ((char *)s)[--n] = c;
53 }
54
55 static inline void *
56 memcpy(void *d1, const void *s1, size_t len)
57 {
58     u8 *d = d1;
59     const u8 *s = s1;
60
61     while (len--) {
62         *d++ = *s++;
63     }
64     return d1;
65 }
66
67 static inline void
68 eoi_master_pic()
69 {
70     outb(PIC1_IRQ5, PORT_PIC1);
71 }
72
73 static inline void
74 eoi_both_pics()
75 {
76     outb(PIC2_IRQ13, PORT_PIC2);
77     eoi_master_pic();
78 }
79
80 static inline
81 void call16(struct bregs *callregs)
82 {
83     asm volatile(
84         "pushl %%ebp\n" // Save state
85         "pushfl\n"
86 #ifdef MODE16
87         "calll __call16\n"
88 #else
89         "calll __call16_from32\n"
90 #endif
91         "popfl\n"       // Restore state
92         "popl %%ebp\n"
93         : "+a" (callregs), "+m" (*callregs)
94         :
95         : "ebx", "ecx", "edx", "esi", "edi");
96 }
97
98 static inline
99 void __call16_int(struct bregs *callregs, u16 offset)
100 {
101     callregs->cs = SEG_BIOS;
102     callregs->ip = offset;
103     call16(callregs);
104 }
105
106 #ifdef MODE16
107 #define call16_int(nr, callregs) do {                           \
108         extern void irq_trampoline_ ##nr ();                    \
109         __call16_int((callregs), (u16)&irq_trampoline_ ##nr );  \
110     } while (0)
111 #else
112 #include "../out/rom16.offset.auto.h"
113 #define call16_int(nr, callregs)                                \
114     __call16_int((callregs), OFFSET_irq_trampoline_ ##nr )
115 #endif
116
117 // output.c
118 void bprintf(u16 action, const char *fmt, ...)
119     __attribute__ ((format (printf, 2, 3)));
120 void __debug_enter(const char *fname, struct bregs *regs);
121 void __debug_fail(const char *fname, struct bregs *regs);
122 void __debug_stub(const char *fname, struct bregs *regs);
123 void __debug_isr(const char *fname, struct bregs *regs);
124 #define debug_enter(regs) \
125     __debug_enter(__func__, regs)
126 #define debug_stub(regs) \
127     __debug_stub(__func__, regs)
128 #define debug_isr(regs) \
129     __debug_isr(__func__, regs)
130 #define printf(fmt, args...)                     \
131     bprintf(1, fmt , ##args )
132
133 // Frequently used return codes
134 #define RET_EUNSUPPORTED 0x86
135 static inline void
136 set_success(struct bregs *regs)
137 {
138     set_cf(regs, 0);
139 }
140
141 static inline void
142 set_code_success(struct bregs *regs)
143 {
144     regs->ah = 0;
145     set_cf(regs, 0);
146 }
147
148 #define set_fail(regs) do {                     \
149         __debug_fail(__func__, (regs));         \
150         set_cf((regs), 1);                      \
151     } while (0)
152
153 #define set_code_fail(regs, code) do {          \
154         set_fail(regs);                         \
155         (regs)->ah = (code);                    \
156     } while (0)
157
158 // kbd.c
159 void handle_15c2(struct bregs *regs);
160
161 // clock.c
162 void handle_1583(struct bregs *regs);
163
164 // apm.c
165 void VISIBLE16 handle_1553(struct bregs *regs);
166
167 // util.c
168 void usleep(u32 count);
169
170 // rombios32.c
171 void rombios32_init(void);
172
173 #endif // util.h