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