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