root / tests / qruncom.c @ c68ea704
History | View | Annotate | Download (7.9 kB)
1 |
/*
|
---|---|
2 |
* Example of use of user mode libqemu: launch a basic .com DOS
|
3 |
* executable
|
4 |
*/
|
5 |
#include <stdlib.h> |
6 |
#include <stdio.h> |
7 |
#include <string.h> |
8 |
#include <inttypes.h> |
9 |
#include <unistd.h> |
10 |
#include <fcntl.h> |
11 |
#include <sys/mman.h> |
12 |
#include <signal.h> |
13 |
|
14 |
#include "cpu.h" |
15 |
|
16 |
//#define SIGTEST
|
17 |
|
18 |
void cpu_outb(CPUState *env, int addr, int val) |
19 |
{ |
20 |
fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
|
21 |
} |
22 |
|
23 |
void cpu_outw(CPUState *env, int addr, int val) |
24 |
{ |
25 |
fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
|
26 |
} |
27 |
|
28 |
void cpu_outl(CPUState *env, int addr, int val) |
29 |
{ |
30 |
fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
|
31 |
} |
32 |
|
33 |
int cpu_inb(CPUState *env, int addr) |
34 |
{ |
35 |
fprintf(stderr, "inb: port=0x%04x\n", addr);
|
36 |
return 0; |
37 |
} |
38 |
|
39 |
int cpu_inw(CPUState *env, int addr) |
40 |
{ |
41 |
fprintf(stderr, "inw: port=0x%04x\n", addr);
|
42 |
return 0; |
43 |
} |
44 |
|
45 |
int cpu_inl(CPUState *env, int addr) |
46 |
{ |
47 |
fprintf(stderr, "inl: port=0x%04x\n", addr);
|
48 |
return 0; |
49 |
} |
50 |
|
51 |
int cpu_get_pic_interrupt(CPUState *env)
|
52 |
{ |
53 |
return -1; |
54 |
} |
55 |
|
56 |
uint64_t cpu_get_tsc(CPUState *env) |
57 |
{ |
58 |
return 0; |
59 |
} |
60 |
|
61 |
static void set_gate(void *ptr, unsigned int type, unsigned int dpl, |
62 |
unsigned long addr, unsigned int sel) |
63 |
{ |
64 |
unsigned int e1, e2; |
65 |
e1 = (addr & 0xffff) | (sel << 16); |
66 |
e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); |
67 |
stl((uint8_t *)ptr, e1); |
68 |
stl((uint8_t *)ptr + 4, e2);
|
69 |
} |
70 |
|
71 |
uint64_t idt_table[256];
|
72 |
|
73 |
/* only dpl matters as we do only user space emulation */
|
74 |
static void set_idt(int n, unsigned int dpl) |
75 |
{ |
76 |
set_gate(idt_table + n, 0, dpl, 0, 0); |
77 |
} |
78 |
|
79 |
void qemu_free(void *ptr) |
80 |
{ |
81 |
free(ptr); |
82 |
} |
83 |
|
84 |
void *qemu_malloc(size_t size)
|
85 |
{ |
86 |
return malloc(size);
|
87 |
} |
88 |
|
89 |
void qemu_printf(const char *fmt, ...) |
90 |
{ |
91 |
va_list ap; |
92 |
va_start(ap, fmt); |
93 |
vprintf(fmt, ap); |
94 |
va_end(ap); |
95 |
} |
96 |
|
97 |
/* XXX: this is a bug in helper2.c */
|
98 |
int errno;
|
99 |
|
100 |
/**********************************************/
|
101 |
|
102 |
#define COM_BASE_ADDR 0x10100 |
103 |
|
104 |
void usage(void) |
105 |
{ |
106 |
printf("qruncom version 0.1 (c) 2003 Fabrice Bellard\n"
|
107 |
"usage: qruncom file.com\n"
|
108 |
"user mode libqemu demo: run simple .com DOS executables\n");
|
109 |
exit(1);
|
110 |
} |
111 |
|
112 |
static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) |
113 |
{ |
114 |
return (uint8_t *)((seg << 4) + (reg & 0xffff)); |
115 |
} |
116 |
|
117 |
static inline void pushw(CPUState *env, int val) |
118 |
{ |
119 |
env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | ((env->regs[R_ESP] - 2) & 0xffff); |
120 |
*(uint16_t *)seg_to_linear(env->segs[R_SS].selector, env->regs[R_ESP]) = val; |
121 |
} |
122 |
|
123 |
static void host_segv_handler(int host_signum, siginfo_t *info, |
124 |
void *puc)
|
125 |
{ |
126 |
if (cpu_signal_handler(host_signum, info, puc)) {
|
127 |
return;
|
128 |
} |
129 |
abort(); |
130 |
} |
131 |
|
132 |
int main(int argc, char **argv) |
133 |
{ |
134 |
uint8_t *vm86_mem; |
135 |
const char *filename; |
136 |
int fd, ret, seg;
|
137 |
CPUState *env; |
138 |
|
139 |
if (argc != 2) |
140 |
usage(); |
141 |
filename = argv[1];
|
142 |
|
143 |
vm86_mem = mmap((void *)0x00000000, 0x110000, |
144 |
PROT_WRITE | PROT_READ | PROT_EXEC, |
145 |
MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); |
146 |
if (vm86_mem == MAP_FAILED) {
|
147 |
perror("mmap");
|
148 |
exit(1);
|
149 |
} |
150 |
|
151 |
/* load the MSDOS .com executable */
|
152 |
fd = open(filename, O_RDONLY); |
153 |
if (fd < 0) { |
154 |
perror(filename); |
155 |
exit(1);
|
156 |
} |
157 |
ret = read(fd, vm86_mem + COM_BASE_ADDR, 65536 - 256); |
158 |
if (ret < 0) { |
159 |
perror("read");
|
160 |
exit(1);
|
161 |
} |
162 |
close(fd); |
163 |
|
164 |
/* install exception handler for CPU emulator */
|
165 |
{ |
166 |
struct sigaction act;
|
167 |
|
168 |
sigfillset(&act.sa_mask); |
169 |
act.sa_flags = SA_SIGINFO; |
170 |
// act.sa_flags |= SA_ONSTACK;
|
171 |
|
172 |
act.sa_sigaction = host_segv_handler; |
173 |
sigaction(SIGSEGV, &act, NULL);
|
174 |
sigaction(SIGBUS, &act, NULL);
|
175 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
176 |
sigaction(SIGFPE, &act, NULL);
|
177 |
#endif
|
178 |
} |
179 |
|
180 |
// cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_TB_OUT_ASM | CPU_LOG_EXEC);
|
181 |
|
182 |
env = cpu_init(); |
183 |
|
184 |
/* disable code copy to simplify debugging */
|
185 |
code_copy_enabled = 0;
|
186 |
|
187 |
/* set user mode state (XXX: should be done automatically by
|
188 |
cpu_init ?) */
|
189 |
env->user_mode_only = 1;
|
190 |
|
191 |
cpu_x86_set_cpl(env, 3);
|
192 |
|
193 |
env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
|
194 |
/* NOTE: hflags duplicates some of the virtual CPU state */
|
195 |
env->hflags |= HF_PE_MASK | VM_MASK; |
196 |
|
197 |
/* flags setup : we activate the IRQs by default as in user
|
198 |
mode. We also activate the VM86 flag to run DOS code */
|
199 |
env->eflags |= IF_MASK | VM_MASK; |
200 |
|
201 |
/* init basic registers */
|
202 |
env->eip = 0x100;
|
203 |
env->regs[R_ESP] = 0xfffe;
|
204 |
seg = (COM_BASE_ADDR - 0x100) >> 4; |
205 |
|
206 |
cpu_x86_load_seg_cache(env, R_CS, seg, |
207 |
(uint8_t *)(seg << 4), 0xffff, 0); |
208 |
cpu_x86_load_seg_cache(env, R_SS, seg, |
209 |
(uint8_t *)(seg << 4), 0xffff, 0); |
210 |
cpu_x86_load_seg_cache(env, R_DS, seg, |
211 |
(uint8_t *)(seg << 4), 0xffff, 0); |
212 |
cpu_x86_load_seg_cache(env, R_ES, seg, |
213 |
(uint8_t *)(seg << 4), 0xffff, 0); |
214 |
cpu_x86_load_seg_cache(env, R_FS, seg, |
215 |
(uint8_t *)(seg << 4), 0xffff, 0); |
216 |
cpu_x86_load_seg_cache(env, R_GS, seg, |
217 |
(uint8_t *)(seg << 4), 0xffff, 0); |
218 |
|
219 |
/* exception support */
|
220 |
env->idt.base = (void *)idt_table;
|
221 |
env->idt.limit = sizeof(idt_table) - 1; |
222 |
set_idt(0, 0); |
223 |
set_idt(1, 0); |
224 |
set_idt(2, 0); |
225 |
set_idt(3, 3); |
226 |
set_idt(4, 3); |
227 |
set_idt(5, 3); |
228 |
set_idt(6, 0); |
229 |
set_idt(7, 0); |
230 |
set_idt(8, 0); |
231 |
set_idt(9, 0); |
232 |
set_idt(10, 0); |
233 |
set_idt(11, 0); |
234 |
set_idt(12, 0); |
235 |
set_idt(13, 0); |
236 |
set_idt(14, 0); |
237 |
set_idt(15, 0); |
238 |
set_idt(16, 0); |
239 |
set_idt(17, 0); |
240 |
set_idt(18, 0); |
241 |
set_idt(19, 0); |
242 |
|
243 |
/* put return code */
|
244 |
*seg_to_linear(env->segs[R_CS].selector, 0) = 0xb4; /* mov ah, $0 */ |
245 |
*seg_to_linear(env->segs[R_CS].selector, 1) = 0x00; |
246 |
*seg_to_linear(env->segs[R_CS].selector, 2) = 0xcd; /* int $0x21 */ |
247 |
*seg_to_linear(env->segs[R_CS].selector, 3) = 0x21; |
248 |
pushw(env, 0x0000);
|
249 |
|
250 |
/* the value of these registers seem to be assumed by pi_10.com */
|
251 |
env->regs[R_ESI] = 0x100;
|
252 |
env->regs[R_ECX] = 0xff;
|
253 |
env->regs[R_EBP] = 0x0900;
|
254 |
env->regs[R_EDI] = 0xfffe;
|
255 |
|
256 |
/* inform the emulator of the mmaped memory */
|
257 |
page_set_flags(0x00000000, 0x110000, |
258 |
PAGE_WRITE | PAGE_READ | PAGE_EXEC | PAGE_VALID); |
259 |
|
260 |
for(;;) {
|
261 |
ret = cpu_x86_exec(env); |
262 |
switch(ret) {
|
263 |
case EXCP0D_GPF:
|
264 |
{ |
265 |
int int_num, ah;
|
266 |
int_num = *(env->segs[R_CS].base + env->eip + 1);
|
267 |
if (int_num != 0x21) |
268 |
goto unknown_int;
|
269 |
ah = (env->regs[R_EAX] >> 8) & 0xff; |
270 |
switch(ah) {
|
271 |
case 0x00: /* exit */ |
272 |
exit(0);
|
273 |
case 0x02: /* write char */ |
274 |
{ |
275 |
uint8_t c = env->regs[R_EDX]; |
276 |
write(1, &c, 1); |
277 |
} |
278 |
break;
|
279 |
case 0x09: /* write string */ |
280 |
{ |
281 |
uint8_t c; |
282 |
for(;;) {
|
283 |
c = *seg_to_linear(env->segs[R_DS].selector, env->regs[R_EAX]); |
284 |
if (c == '$') |
285 |
break;
|
286 |
write(1, &c, 1); |
287 |
} |
288 |
env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | '$'; |
289 |
} |
290 |
break;
|
291 |
default:
|
292 |
unknown_int:
|
293 |
fprintf(stderr, "unsupported int 0x%02x\n", int_num);
|
294 |
cpu_dump_state(env, stderr, 0);
|
295 |
// exit(1);
|
296 |
} |
297 |
env->eip += 2;
|
298 |
} |
299 |
break;
|
300 |
default:
|
301 |
fprintf(stderr, "unhandled cpu_exec return code (0x%x)\n", ret);
|
302 |
cpu_dump_state(env, stderr, 0);
|
303 |
exit(1);
|
304 |
} |
305 |
} |
306 |
} |