1 // Misc utility functions.
3 // Copyright (C) 2008 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\n"
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\n"
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;
56 call16_simpint(int nr, u32 *eax, u32 *flags)
58 extern void __force_link_error__call16_simpint_only_in_16bit_mode();
60 __force_link_error__call16_simpint_only_in_16bit_mode();
69 : "+a"(*eax), "=r"(*flags)
74 // Switch to the extra stack in ebda and call a function.
76 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
78 extern void __force_link_error__stack_hop_only_in_16bit_mode();
80 __force_link_error__stack_hop_only_in_16bit_mode();
82 u16 ebda_seg = get_ebda_seg(), bkup_ss;
85 // Backup current %ss/%esp values.
88 // Copy ebda seg to %ds/%ss and set %esp
94 // Restore segments and stack
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)
104 // Sum the bytes in the specified area.
106 checksum_far(u16 buf_seg, void *buf_far, u32 len)
108 SET_SEG(ES, buf_seg);
111 for (i=0; i<len; i++)
112 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
117 checksum(void *buf, u32 len)
119 return checksum_far(GET_SEG(SS), buf, len);
123 strlen(const char *s)
125 if (__builtin_constant_p(s))
126 return __builtin_strlen(s);
133 // Compare two areas of memory.
135 memcmp(const void *s1, const void *s2, size_t n)
138 if (*(u8*)s1 != *(u8*)s2)
139 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
147 // Compare two strings.
149 strcmp(const char *s1, const char *s2)
153 return *s1 < *s2 ? -1 : 1;
162 memset(void *s, int c, size_t n)
165 ((char *)s)[--n] = c;
170 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
177 "rep movsb (%%si),%%es:(%%di)\n"
179 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
185 __memcpy(void *d1, const void *s1, size_t len)
188 if (((u32)d1 | (u32)s1 | len) & 3) {
189 // non-aligned memcpy
191 "rep movsb (%%esi),%%es:(%%edi)\n"
192 : "+c"(len), "+S"(s1), "+D"(d)
196 // Common case - use 4-byte copy
199 "rep movsl (%%esi),%%es:(%%edi)\n"
200 : "+c"(len), "+S"(s1), "+D"(d)
206 memmove(void *d, const void *s, size_t len)
209 return memcpy(d, s, len);
214 *(char*)d = *(char*)s;
222 // Copy a string - truncating it if necessary.
224 strtcpy(char *dest, const char *src, size_t len)
227 while (len-- && *src != '\0')
233 // Wait for 'usec' microseconds with irqs enabled.
238 memset(&br, 0, sizeof(br));
242 call16_int(0x15, &br);
245 // See if a keystroke is pending in the keyboard buffer.
247 check_for_keystroke()
250 memset(&br, 0, sizeof(br));
252 call16_int(0x16, &br);
253 return !(br.flags & F_ZF);
256 // Return a keystroke - waiting forever if necessary.
261 memset(&br, 0, sizeof(br));
262 call16_int(0x16, &br);
266 // Read a keystroke - waiting up to 'msec' milliseconds.
268 get_keystroke(int msec)
271 if (check_for_keystroke())
272 return get_raw_keystroke();