Fix compile of smpdetect - last commit missed an include.
[seabios.git] / src / util.c
1 // Misc utility functions.
2 //
3 // Copyright (C) 2008,2009  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"
22 #else
23         "calll __call16_from32"
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     ASSERT32();
34     asm volatile(
35         "calll __call16big_from32"
36         : "+a" (callregs), "+m" (*callregs)
37         :
38         : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
39 }
40
41 inline void
42 __call16_int(struct bregs *callregs, u16 offset)
43 {
44     if (MODE16)
45         callregs->cs = GET_SEG(CS);
46     else
47         callregs->cs = SEG_BIOS;
48     callregs->ip = offset;
49     call16(callregs);
50 }
51
52 // Switch to the extra stack in ebda and call a function.
53 inline u32
54 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
55 {
56     ASSERT16();
57     u16 ebda_seg = get_ebda_seg(), bkup_ss;
58     u32 bkup_esp;
59     asm volatile(
60         // Backup current %ss/%esp values.
61         "movw %%ss, %w3\n"
62         "movl %%esp, %4\n"
63         // Copy ebda seg to %ds/%ss and set %esp
64         "movw %w6, %%ds\n"
65         "movw %w6, %%ss\n"
66         "movl %5, %%esp\n"
67         // Call func
68         "calll %7\n"
69         // Restore segments and stack
70         "movw %w3, %%ds\n"
71         "movw %w3, %%ss\n"
72         "movl %4, %%esp"
73         : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
74         : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
75         : "cc", "memory");
76     return eax;
77 }
78
79 // Sum the bytes in the specified area.
80 u8
81 checksum_far(u16 buf_seg, void *buf_far, u32 len)
82 {
83     SET_SEG(ES, buf_seg);
84     u32 i;
85     u8 sum = 0;
86     for (i=0; i<len; i++)
87         sum += GET_VAR(ES, ((u8*)buf_far)[i]);
88     return sum;
89 }
90
91 u8
92 checksum(void *buf, u32 len)
93 {
94     return checksum_far(GET_SEG(SS), buf, len);
95 }
96
97 size_t
98 strlen(const char *s)
99 {
100     if (__builtin_constant_p(s))
101         return __builtin_strlen(s);
102     const char *p = s;
103     while (*p)
104         p++;
105     return p-s;
106 }
107
108 // Compare two areas of memory.
109 int
110 memcmp(const void *s1, const void *s2, size_t n)
111 {
112     while (n) {
113         if (*(u8*)s1 != *(u8*)s2)
114             return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
115         s1++;
116         s2++;
117         n--;
118     }
119     return 0;
120 }
121
122 // Compare two strings.
123 int
124 strcmp(const char *s1, const char *s2)
125 {
126     for (;;) {
127         if (*s1 != *s2)
128             return *s1 < *s2 ? -1 : 1;
129         if (! *s1)
130             return 0;
131         s1++;
132         s2++;
133     }
134 }
135
136 inline void
137 memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
138 {
139     SET_SEG(ES, d_seg);
140     asm volatile(
141         "rep stosb %%es:(%%di)"
142         : "+c"(len), "+D"(d_far)
143         : "a"(c)
144         : "cc", "memory");
145 }
146
147 inline void
148 memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
149 {
150     len /= 2;
151     SET_SEG(ES, d_seg);
152     asm volatile(
153         "rep stosw %%es:(%%di)"
154         : "+c"(len), "+D"(d_far)
155         : "a"(c)
156         : "cc", "memory");
157 }
158
159 void *
160 memset(void *s, int c, size_t n)
161 {
162     while (n)
163         ((char *)s)[--n] = c;
164     return s;
165 }
166
167 inline void
168 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
169 {
170     SET_SEG(ES, d_seg);
171     u16 bkup_ds;
172     asm volatile(
173         "movw %%ds, %w0\n"
174         "movw %w4, %%ds\n"
175         "rep movsb (%%si),%%es:(%%di)\n"
176         "movw %w0, %%ds"
177         : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
178         : "r"(s_seg)
179         : "cc", "memory");
180 }
181
182 void *
183 #undef memcpy
184 memcpy(void *d1, const void *s1, size_t len)
185 #define memcpy __builtin_memcpy
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)"
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)"
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 }