Optimize memcpy.
[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 LGPLv3 license.
6 #ifndef __UTIL_H
7 #define __UTIL_H
8
9 #include "types.h" // u32
10
11 static inline void irq_disable(void)
12 {
13     asm volatile("cli": : :"memory");
14 }
15
16 static inline void irq_enable(void)
17 {
18     asm volatile("sti": : :"memory");
19 }
20
21 static inline unsigned long irq_save(void)
22 {
23     unsigned long flags;
24     asm volatile("pushfl ; popl %0" : "=g" (flags));
25     irq_disable();
26     return flags;
27 }
28
29 static inline void irq_restore(unsigned long flags)
30 {
31     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
32 }
33
34 static inline void cpu_relax(void)
35 {
36     asm volatile("rep ; nop": : :"memory");
37 }
38
39 static inline void nop(void)
40 {
41     asm volatile("nop");
42 }
43
44 static inline void hlt(void)
45 {
46     asm volatile("hlt");
47 }
48
49 static inline void wbinvd(void)
50 {
51     asm volatile("wbinvd");
52 }
53
54 static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
55 {
56     asm("cpuid"
57         : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
58         : "0" (index));
59 }
60
61 static inline u64 rdtscll(void)
62 {
63     u64 val;
64     asm volatile("rdtsc" : "=A" (val));
65     return val;
66 }
67
68 // util.c
69 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
70 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
71 u8 checksum(void *buf, u32 len);
72 int memcmp(const void *s1, const void *s2, size_t n);
73 size_t strlen(const char *s);
74 int strcmp(const char *s1, const char *s2);
75 void *memset(void *s, int c, size_t n);
76 void *__memcpy(void *d1, const void *s1, size_t len);
77 #define memcpy(d1, s1, len) (                           \
78         (__builtin_constant_p(len) && (len) <= 20)      \
79         ? __builtin_memcpy((d1), (s1), (len))           \
80         :  __memcpy((d1), (s1), (len)))
81 inline void memcpy_far(u16 d_seg, void *d_far
82                        , u16 s_seg, const void *s_far, size_t len);
83 void *memmove(void *d, const void *s, size_t len);
84 char *strtcpy(char *dest, const char *src, size_t len);
85 struct bregs;
86 inline void call16(struct bregs *callregs);
87 inline void call16big(struct bregs *callregs);
88 inline void __call16_int(struct bregs *callregs, u16 offset);
89 #define call16_int(nr, callregs) do {                           \
90         extern void irq_trampoline_ ##nr ();                    \
91         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
92     } while (0)
93 inline void call16_simpint(int nr, u32 *eax, u32 *flags);
94 void usleep(u32 usec);
95 int get_keystroke(int msec);
96
97 // output.c
98 void debug_serial_setup();
99 void panic(const char *fmt, ...)
100     __attribute__ ((format (printf, 1, 2)))
101     __attribute__ ((noreturn));
102 void printf(const char *fmt, ...)
103     __attribute__ ((format (printf, 1, 2)));
104 void __dprintf(const char *fmt, ...)
105     __attribute__ ((format (printf, 1, 2)));
106 #define dprintf(lvl, fmt, args...) do {                         \
107         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
108             __dprintf((fmt) , ##args );                         \
109     } while (0)
110 void __debug_enter(struct bregs *regs, const char *fname);
111 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
112 void __debug_isr(const char *fname);
113 #define debug_enter(regs, lvl) do {                     \
114         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
115             __debug_enter((regs), __func__);            \
116     } while (0)
117 #define debug_isr(lvl) do {                             \
118         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
119             __debug_isr(__func__);                      \
120     } while (0)
121 #define debug_stub(regs)                        \
122     __debug_stub((regs), __LINE__, __func__)
123 void hexdump(void *d, int len);
124
125 // kbd.c
126 void kbd_setup();
127 void handle_15c2(struct bregs *regs);
128
129 // mouse.c
130 void mouse_setup();
131
132 // system.c
133 extern u32 RamSize;
134 extern u64 RamSizeOver4G;
135 void mathcp_setup();
136
137 // serial.c
138 void serial_setup();
139 void lpt_setup();
140
141 // clock.c
142 void timer_setup();
143 void ndelay(u32 count);
144 void udelay(u32 count);
145 void mdelay(u32 count);
146 u64 calc_future_tsc(u32 msecs);
147 void handle_1583(struct bregs *regs);
148 void handle_1586(struct bregs *regs);
149
150 // apm.c
151 void VISIBLE16 handle_1553(struct bregs *regs);
152
153 // pcibios.c
154 void handle_1ab1(struct bregs *regs);
155
156 // shadow.c
157 void make_bios_writable();
158 void make_bios_readonly();
159
160 // pciinit.c
161 void pci_bios_setup(void);
162
163 // smm.c
164 void smm_init();
165
166 // smpdetect.c
167 int smp_probe(void);
168 void smp_probe_setup(void);
169
170 // smbios.c
171 void smbios_init(void);
172
173 // coreboot.c
174 const char *cbfs_findNprefix(const char *prefix, int n);
175 void *cb_find_optionrom(u32 vendev);
176 void cbfs_run_payload(const char *filename);
177 void coreboot_setup();
178
179 // vgahooks.c
180 void handle_155f();
181
182 // optionroms.c
183 void call_bcv(u16 seg, u16 ip);
184 void vga_setup();
185 void optionrom_setup();
186
187 // resume.c
188 void init_dma();
189
190 // pnpbios.c
191 #define PNP_SIGNATURE 0x506e5024 // $PnP
192 u16 get_pnp_offset();
193 void pnp_setup();
194
195 // mtrr.c
196 void mtrr_setup(void);
197
198 // romlayout.S
199 void reset_vector() __attribute__ ((noreturn));
200
201 // misc.c
202 extern u8 BiosChecksum;
203
204 #endif // util.h