1 // Misc utility functions.
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "util.h" // call16
8 #include "bregs.h" // struct bregs
9 #include "farptr.h" // GET_FLATPTR
10 #include "biosvar.h" // get_ebda_seg
12 // Call a function with a specified register state. Note that on
13 // return, the interrupt enable/disable flag may be altered.
15 call16(struct bregs *callregs)
23 "calll __call16_from32"
25 : "+a" (callregs), "+m" (*callregs)
27 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
31 call16big(struct bregs *callregs)
35 "calll __call16big_from32"
36 : "+a" (callregs), "+m" (*callregs)
38 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
42 __call16_int(struct bregs *callregs, u16 offset)
45 callregs->cs = GET_SEG(CS);
47 callregs->cs = SEG_BIOS;
48 callregs->ip = offset;
52 // Switch to the extra stack in ebda and call a function.
54 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
57 u16 ebda_seg = get_ebda_seg(), bkup_ss;
60 // Backup current %ss/%esp values.
63 // Copy ebda seg to %ds/%ss and set %esp
69 // Restore segments and stack
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)
79 // Sum the bytes in the specified area.
81 checksum_far(u16 buf_seg, void *buf_far, u32 len)
87 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
92 checksum(void *buf, u32 len)
94 return checksum_far(GET_SEG(SS), buf, len);
100 if (__builtin_constant_p(s))
101 return __builtin_strlen(s);
108 // Compare two areas of memory.
110 memcmp(const void *s1, const void *s2, size_t n)
113 if (*(u8*)s1 != *(u8*)s2)
114 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
122 // Compare two strings.
124 strcmp(const char *s1, const char *s2)
128 return *s1 < *s2 ? -1 : 1;
137 memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
141 "rep stosb %%es:(%%di)"
142 : "+c"(len), "+D"(d_far)
148 memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
153 "rep stosw %%es:(%%di)"
154 : "+c"(len), "+D"(d_far)
160 memset(void *s, int c, size_t n)
163 ((char *)s)[--n] = c;
168 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
175 "rep movsb (%%si),%%es:(%%di)\n"
177 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
184 memcpy(void *d1, const void *s1, size_t len)
186 #define memcpy __builtin_memcpy
189 SET_SEG(ES, GET_SEG(SS));
191 if (((u32)d1 | (u32)s1 | len) & 3) {
192 // non-aligned memcpy
194 "rep movsb (%%esi),%%es:(%%edi)"
195 : "+c"(len), "+S"(s1), "+D"(d)
199 // Common case - use 4-byte copy
202 "rep movsl (%%esi),%%es:(%%edi)"
203 : "+c"(len), "+S"(s1), "+D"(d)
209 memmove(void *d, const void *s, size_t len)
212 return memcpy(d, s, len);
217 *(char*)d = *(char*)s;
225 // Copy a string - truncating it if necessary.
227 strtcpy(char *dest, const char *src, size_t len)
230 while (len-- && *src != '\0')
236 // Wait for 'usec' microseconds with irqs enabled.
241 memset(&br, 0, sizeof(br));
245 call16_int(0x15, &br);
248 // See if a keystroke is pending in the keyboard buffer.
250 check_for_keystroke()
253 memset(&br, 0, sizeof(br));
255 call16_int(0x16, &br);
256 return !(br.flags & F_ZF);
259 // Return a keystroke - waiting forever if necessary.
264 memset(&br, 0, sizeof(br));
265 call16_int(0x16, &br);
269 // Read a keystroke - waiting up to 'msec' milliseconds.
271 get_keystroke(int msec)
274 if (check_for_keystroke())
275 return get_raw_keystroke();