Optimize memcpy.
[seabios.git] / src / util.c
1 // Misc utility functions.
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
7 #include "util.h" // call16
8 #include "bregs.h" // struct bregs
9 #include "farptr.h" // GET_FLATPTR
10 #include "biosvar.h" // get_ebda_seg
11
12 // Call a function with a specified register state.  Note that on
13 // return, the interrupt enable/disable flag may be altered.
14 inline void
15 call16(struct bregs *callregs)
16 {
17     asm volatile(
18 #if MODE16 == 1
19         "calll __call16\n"
20         "cli\n"
21         "cld\n"
22 #else
23         "calll __call16_from32\n"
24 #endif
25         : "+a" (callregs), "+m" (*callregs)
26         :
27         : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
28 }
29
30 inline void
31 call16big(struct bregs *callregs)
32 {
33     extern void __force_link_error__call16big_only_in_32bit_mode();
34     if (MODE16)
35         __force_link_error__call16big_only_in_32bit_mode();
36
37     asm volatile(
38         "calll __call16big_from32\n"
39         : "+a" (callregs), "+m" (*callregs)
40         :
41         : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
42 }
43
44 inline void
45 __call16_int(struct bregs *callregs, u16 offset)
46 {
47     if (MODE16)
48         callregs->cs = GET_SEG(CS);
49     else
50         callregs->cs = SEG_BIOS;
51     callregs->ip = offset;
52     call16(callregs);
53 }
54
55 inline void
56 call16_simpint(int nr, u32 *eax, u32 *flags)
57 {
58     extern void __force_link_error__call16_simpint_only_in_16bit_mode();
59     if (!MODE16)
60         __force_link_error__call16_simpint_only_in_16bit_mode();
61
62     asm volatile(
63         "stc\n"
64         "int %2\n"
65         "pushfl\n"
66         "popl %1\n"
67         "cli\n"
68         "cld\n"
69         : "+a"(*eax), "=r"(*flags)
70         : "i"(nr)
71         : "cc", "memory");
72 }
73
74 // Switch to the extra stack in ebda and call a function.
75 inline u32
76 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
77 {
78     extern void __force_link_error__stack_hop_only_in_16bit_mode();
79     if (!MODE16)
80         __force_link_error__stack_hop_only_in_16bit_mode();
81
82     u16 ebda_seg = get_ebda_seg(), bkup_ss;
83     u32 bkup_esp;
84     asm volatile(
85         // Backup current %ss/%esp values.
86         "movw %%ss, %w3\n"
87         "movl %%esp, %4\n"
88         // Copy ebda seg to %ds/%ss and set %esp
89         "movw %w6, %%ds\n"
90         "movw %w6, %%ss\n"
91         "movl %5, %%esp\n"
92         // Call func
93         "calll %7\n"
94         // Restore segments and stack
95         "movw %w3, %%ds\n"
96         "movw %w3, %%ss\n"
97         "movl %4, %%esp\n"
98         : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
99         : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
100         : "cc", "memory");
101     return eax;
102 }
103
104 // Sum the bytes in the specified area.
105 u8
106 checksum_far(u16 buf_seg, void *buf_far, u32 len)
107 {
108     SET_SEG(ES, buf_seg);
109     u32 i;
110     u8 sum = 0;
111     for (i=0; i<len; i++)
112         sum += GET_VAR(ES, ((u8*)buf_far)[i]);
113     return sum;
114 }
115
116 u8
117 checksum(void *buf, u32 len)
118 {
119     return checksum_far(GET_SEG(SS), buf, len);
120 }
121
122 size_t
123 strlen(const char *s)
124 {
125     if (__builtin_constant_p(s))
126         return __builtin_strlen(s);
127     const char *p = s;
128     while (*p)
129         p++;
130     return p-s;
131 }
132
133 // Compare two areas of memory.
134 int
135 memcmp(const void *s1, const void *s2, size_t n)
136 {
137     while (n) {
138         if (*(u8*)s1 != *(u8*)s2)
139             return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
140         s1++;
141         s2++;
142         n--;
143     }
144     return 0;
145 }
146
147 // Compare two strings.
148 int
149 strcmp(const char *s1, const char *s2)
150 {
151     for (;;) {
152         if (*s1 != *s2)
153             return *s1 < *s2 ? -1 : 1;
154         if (! *s1)
155             return 0;
156         s1++;
157         s2++;
158     }
159 }
160
161 void *
162 memset(void *s, int c, size_t n)
163 {
164     while (n)
165         ((char *)s)[--n] = c;
166     return s;
167 }
168
169 inline void
170 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
171 {
172     SET_SEG(ES, d_seg);
173     u16 bkup_ds;
174     asm volatile(
175         "movw %%ds, %w0\n"
176         "movw %w4, %%ds\n"
177         "rep movsb (%%si),%%es:(%%di)\n"
178         "movw %w0, %%ds\n"
179         : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
180         : "r"(s_seg)
181         : "cc", "memory");
182 }
183
184 noinline void *
185 __memcpy(void *d1, const void *s1, size_t len)
186 {
187     void *d = d1;
188     if (((u32)d1 | (u32)s1 | len) & 3) {
189         // non-aligned memcpy
190         asm volatile(
191             "rep movsb (%%esi),%%es:(%%edi)\n"
192             : "+c"(len), "+S"(s1), "+D"(d)
193             : : "cc", "memory");
194         return d1;
195     }
196     // Common case - use 4-byte copy
197     len /= 4;
198     asm volatile(
199         "rep movsl (%%esi),%%es:(%%edi)\n"
200         : "+c"(len), "+S"(s1), "+D"(d)
201         : : "cc", "memory");
202     return d1;
203 }
204
205 void *
206 memmove(void *d, const void *s, size_t len)
207 {
208     if (s >= d)
209         return memcpy(d, s, len);
210
211     d += len-1;
212     s += len-1;
213     while (len--) {
214         *(char*)d = *(char*)s;
215         d--;
216         s--;
217     }
218
219     return d;
220 }
221
222 // Copy a string - truncating it if necessary.
223 char *
224 strtcpy(char *dest, const char *src, size_t len)
225 {
226     char *d = dest;
227     while (len-- && *src != '\0')
228         *d++ = *src++;
229     *d = '\0';
230     return dest;
231 }
232
233 // Wait for 'usec' microseconds with irqs enabled.
234 void
235 usleep(u32 usec)
236 {
237     struct bregs br;
238     memset(&br, 0, sizeof(br));
239     br.ah = 0x86;
240     br.cx = usec >> 16;
241     br.dx = usec;
242     call16_int(0x15, &br);
243 }
244
245 // See if a keystroke is pending in the keyboard buffer.
246 static int
247 check_for_keystroke()
248 {
249     struct bregs br;
250     memset(&br, 0, sizeof(br));
251     br.ah = 1;
252     call16_int(0x16, &br);
253     return !(br.flags & F_ZF);
254 }
255
256 // Return a keystroke - waiting forever if necessary.
257 static int
258 get_raw_keystroke()
259 {
260     struct bregs br;
261     memset(&br, 0, sizeof(br));
262     call16_int(0x16, &br);
263     return br.ah;
264 }
265
266 // Read a keystroke - waiting up to 'msec' milliseconds.
267 int
268 get_keystroke(int msec)
269 {
270     for (;;) {
271         if (check_for_keystroke())
272             return get_raw_keystroke();
273         if (msec <= 0)
274             return -1;
275         usleep(50*1000);
276         msec -= 50;
277     }
278 }