1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
// Misc utility functions.
//
// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU LGPLv3 license.
#include "util.h" // usleep
#include "bregs.h" // struct bregs
#include "config.h" // SEG_BIOS
#include "farptr.h" // GET_FLATPTR
#include "biosvar.h" // get_ebda_seg
// Call a function with a specified register state. Note that on
// return, the interrupt enable/disable flag may be altered.
inline void
call16(struct bregs *callregs)
{
asm volatile(
#if MODE16 == 1
"calll __call16\n"
#else
"calll __call16_from32\n"
#endif
: "+a" (callregs), "+m" (*callregs)
:
: "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc", "memory");
}
inline void
call16big(struct bregs *callregs)
{
extern void __force_link_error__call16big_only_in_32bit_mode();
if (MODE16)
__force_link_error__call16big_only_in_32bit_mode();
asm volatile(
"calll __call16big_from32\n"
: "+a" (callregs), "+m" (*callregs)
:
: "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc", "memory");
}
inline void
__call16_int(struct bregs *callregs, u16 offset)
{
callregs->cs = SEG_BIOS;
callregs->ip = offset;
call16(callregs);
}
inline void
call16_simpint(int nr, u32 *eax, u32 *flags)
{
extern void __force_link_error__call16_simpint_only_in_16bit_mode();
if (!MODE16)
__force_link_error__call16_simpint_only_in_16bit_mode();
asm volatile(
"stc\n"
"int %2\n"
"pushfl\n"
"popl %1\n"
"cld\n"
"cli\n"
: "+a"(*eax), "=r"(*flags)
: "i"(nr)
: "cc", "memory");
}
// Switch to the extra stack in ebda and call a function.
inline u32
stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
{
extern void __force_link_error__stack_hop_only_in_16bit_mode();
if (!MODE16)
__force_link_error__stack_hop_only_in_16bit_mode();
u16 ebda_seg = get_ebda_seg(), bkup_ss;
u32 bkup_esp;
asm volatile(
// Backup current %ss/%esp values.
"movw %%ss, %w3\n"
"movl %%esp, %4\n"
// Copy ebda seg to %ds/%ss and set %esp
"movw %w6, %%ds\n"
"movw %w6, %%ss\n"
"movl %5, %%esp\n"
// Call func
"calll %7\n"
// Restore segments and stack
"movw %w3, %%ds\n"
"movw %w3, %%ss\n"
"movl %4, %%esp\n"
: "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
: "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
: "cc", "memory");
return eax;
}
// Sum the bytes in the specified area.
u8
checksum(u8 *buf_fl, u32 len)
{
u32 i;
u8 sum = 0;
for (i=0; i<len; i++)
sum += GET_FLATPTR(buf_fl[i]);
return sum;
}
void *
memset(void *s, int c, size_t n)
{
while (n)
((char *)s)[--n] = c;
return s;
}
void *
memcpy_fl(void *d_fl, const void *s_fl, size_t len)
{
u8 *d = d_fl;
u8 *s = (u8*)s_fl;
while (len--) {
SET_FLATPTR(*d, GET_FLATPTR(*s));
d++;
s++;
}
return d_fl;
}
void *
memcpy(void *d1, const void *s1, size_t len)
{
u8 *d = (u8*)d1, *s = (u8*)s1;
while (len--)
*d++ = *s++;
return d1;
}
void *
memmove(void *d, const void *s, size_t len)
{
if (s >= d)
return memcpy(d, s, len);
d += len-1;
s += len-1;
while (len--) {
*(char*)d = *(char*)s;
d--;
s--;
}
return d;
}
|