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)
33 extern void __force_link_error__call16big_only_in_32bit_mode();
35 __force_link_error__call16big_only_in_32bit_mode();
38 "calll __call16big_from32"
39 : "+a" (callregs), "+m" (*callregs)
41 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
45 __call16_int(struct bregs *callregs, u16 offset)
48 callregs->cs = GET_SEG(CS);
50 callregs->cs = SEG_BIOS;
51 callregs->ip = offset;
55 // Switch to the extra stack in ebda and call a function.
57 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
59 extern void __force_link_error__stack_hop_only_in_16bit_mode();
61 __force_link_error__stack_hop_only_in_16bit_mode();
63 u16 ebda_seg = get_ebda_seg(), bkup_ss;
66 // Backup current %ss/%esp values.
69 // Copy ebda seg to %ds/%ss and set %esp
75 // Restore segments and stack
79 : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
80 : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
85 // Sum the bytes in the specified area.
87 checksum_far(u16 buf_seg, void *buf_far, u32 len)
93 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
98 checksum(void *buf, u32 len)
100 return checksum_far(GET_SEG(SS), buf, len);
104 strlen(const char *s)
106 if (__builtin_constant_p(s))
107 return __builtin_strlen(s);
114 // Compare two areas of memory.
116 memcmp(const void *s1, const void *s2, size_t n)
119 if (*(u8*)s1 != *(u8*)s2)
120 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
128 // Compare two strings.
130 strcmp(const char *s1, const char *s2)
134 return *s1 < *s2 ? -1 : 1;
143 memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
147 "rep stosb %%es:(%%di)"
148 : "+c"(len), "+D"(d_far)
154 memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
159 "rep stosw %%es:(%%di)"
160 : "+c"(len), "+D"(d_far)
166 memset(void *s, int c, size_t n)
169 ((char *)s)[--n] = c;
174 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
181 "rep movsb (%%si),%%es:(%%di)\n"
183 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
190 memcpy(void *d1, const void *s1, size_t len)
191 #define memcpy __builtin_memcpy
194 if (((u32)d1 | (u32)s1 | len) & 3) {
195 // non-aligned memcpy
197 "rep movsb (%%esi),%%es:(%%edi)"
198 : "+c"(len), "+S"(s1), "+D"(d)
202 // Common case - use 4-byte copy
205 "rep movsl (%%esi),%%es:(%%edi)"
206 : "+c"(len), "+S"(s1), "+D"(d)
212 memmove(void *d, const void *s, size_t len)
215 return memcpy(d, s, len);
220 *(char*)d = *(char*)s;
228 // Copy a string - truncating it if necessary.
230 strtcpy(char *dest, const char *src, size_t len)
233 while (len-- && *src != '\0')
239 // Wait for 'usec' microseconds with irqs enabled.
244 memset(&br, 0, sizeof(br));
248 call16_int(0x15, &br);
251 // See if a keystroke is pending in the keyboard buffer.
253 check_for_keystroke()
256 memset(&br, 0, sizeof(br));
258 call16_int(0x16, &br);
259 return !(br.flags & F_ZF);
262 // Return a keystroke - waiting forever if necessary.
267 memset(&br, 0, sizeof(br));
268 call16_int(0x16, &br);
272 // Read a keystroke - waiting up to 'msec' milliseconds.
274 get_keystroke(int msec)
277 if (check_for_keystroke())
278 return get_raw_keystroke();