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 GPLv3 license.
7 #include "util.h" // usleep
8 #include "bregs.h" // struct bregs
9 #include "config.h" // SEG_BIOS
10 #include "farptr.h" // GET_FARPTR
11 #include "biosvar.h" // get_ebda_seg
13 // Call a function with a specified register state. Note that on
14 // return, the interrupt enable/disable flag may be altered.
16 call16(struct bregs *callregs)
22 "calll __call16_from32\n"
24 : "+a" (callregs), "+m" (*callregs)
26 : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc", "memory");
30 call16big(struct bregs *callregs)
32 extern void __force_link_error__call16big_only_in_32bit_mode();
34 __force_link_error__call16big_only_in_32bit_mode();
37 "calll __call16big_from32\n"
38 : "+a" (callregs), "+m" (*callregs)
40 : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc", "memory");
44 __call16_int(struct bregs *callregs, u16 offset)
46 callregs->cs = SEG_BIOS;
47 callregs->ip = offset;
51 // Switch to the extra stack in ebda and call a function.
53 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
55 extern void __force_link_error__stack_hop_only_in_16bit_mode();
57 __force_link_error__stack_hop_only_in_16bit_mode();
59 u32 ebda_seg = get_ebda_seg();
62 // Backup current %ss value.
64 // Copy ebda seg to %ss and %ds
67 // Backup %esp and set it to new value
72 // Restore segments and stack
76 : "+a" (eax), "+d" (edx), "+c" (ecx), "+r" (ebda_seg), "=r" (tmp)
77 : "i" (EBDA_OFFSET_TOP_STACK), "m" (*(u8*)func)
82 // Sum the bytes in the specified area.
84 checksum(u8 *far_data, u32 len)
89 sum += GET_FARPTR(far_data[i]);
94 memset(void *s, int c, size_t n)
102 memcpy_far(void *far_d1, const void *far_s1, size_t len)
108 SET_FARPTR(*d, GET_FARPTR(*s));
117 memcpy(void *d1, const void *s1, size_t len)
119 u8 *d = (u8*)d1, *s = (u8*)s1;
126 memmove(void *d, const void *s, size_t len)
129 return memcpy(d, s, len);
134 *(char*)d = *(char*)s;