Bug fixes; get mouse working.
[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 #define BX_PANIC(fmt, args...) bprintf(0, fmt , ##args)
41 #define BX_INFO(fmt, args...) bprintf(0, fmt , ##args)
42
43 static inline void
44 memset(void *s, int c, size_t n)
45 {
46     while (n)
47         ((char *)s)[--n] = c;
48 }
49
50 static inline void
51 eoi_master_pic()
52 {
53     outb(PIC1_IRQ5, PORT_PIC1);
54 }
55
56 static inline void
57 eoi_both_pics()
58 {
59     outb(PIC2_IRQ13, PORT_PIC2);
60     eoi_master_pic();
61 }
62
63 static inline
64 void call16(struct bregs *callregs)
65 {
66     asm volatile(
67         "pushl %%ebp\n" // Save state
68         "pushfl\n"
69 #ifdef MODE16
70         "calll __call16\n"
71 #else
72         "calll __call16_from32\n"
73 #endif
74         "popfl\n"       // Restore state
75         "popl %%ebp\n"
76         : "+a" (callregs), "+m" (*callregs)
77         :
78         : "ebx", "ecx", "edx", "esi", "edi");
79 }
80
81 static inline
82 void __call16_int(struct bregs *callregs, u16 offset)
83 {
84     callregs->cs = SEG_BIOS;
85     callregs->ip = offset;
86     call16(callregs);
87 }
88
89 #ifdef MODE16
90 #define call16_int(nr, callregs) do {                           \
91         extern void irq_trampoline_ ##nr ();                    \
92         __call16_int((callregs), (u16)&irq_trampoline_ ##nr );  \
93     } while (0)
94 #else
95 #include "../out/rom16.offset.auto.h"
96 #define call16_int(nr, callregs)                                \
97     __call16_int((callregs), OFFSET_irq_trampoline_ ##nr )
98 #endif
99
100 // output.c
101 void bprintf(u16 action, const char *fmt, ...)
102     __attribute__ ((format (printf, 2, 3)));
103 void __debug_enter(const char *fname, struct bregs *regs);
104 void __debug_exit(const char *fname, struct bregs *regs);
105 void __debug_stub(const char *fname, struct bregs *regs);
106 void __debug_isr(const char *fname, struct bregs *regs);
107 #define debug_enter(regs) \
108     __debug_enter(__func__, regs)
109 #define debug_exit(regs) \
110     __debug_exit(__func__, regs)
111 #define debug_stub(regs) \
112     __debug_stub(__func__, regs)
113 #define debug_isr(regs) \
114     __debug_isr(__func__, regs)
115 #define printf(fmt, args...)                     \
116     bprintf(1, fmt , ##args )
117
118 // kbd.c
119 void handle_15c2(struct bregs *regs);
120
121 // clock.c
122 void handle_1583(struct bregs *regs);
123
124 // Frequent bios return helper
125 #define RET_EUNSUPPORTED 0x86
126 static inline void
127 handle_ret(struct bregs *regs, u8 code)
128 {
129     regs->ah = code;
130     set_cf(regs, code);
131 }
132
133 #endif // util.h