2 * Example of use of user mode libqemu: launch a basic .com DOS
19 int cpu_get_pic_interrupt(CPUState
*env
)
24 uint64_t cpu_get_tsc(CPUState
*env
)
29 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
30 unsigned long addr
, unsigned int sel
)
33 e1
= (addr
& 0xffff) | (sel
<< 16);
34 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
35 stl((uint8_t *)ptr
, e1
);
36 stl((uint8_t *)ptr
+ 4, e2
);
39 uint64_t idt_table
[256];
41 /* only dpl matters as we do only user space emulation */
42 static void set_idt(int n
, unsigned int dpl
)
44 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
47 void qemu_free(void *ptr
)
52 void *qemu_malloc(size_t size
)
57 void *qemu_mallocz(size_t size
)
60 ptr
= qemu_malloc(size
);
67 void *qemu_vmalloc(size_t size
)
69 return memalign(4096, size
);
72 void qemu_vfree(void *ptr
)
77 void qemu_printf(const char *fmt
, ...)
85 /* XXX: this is a bug in helper2.c */
88 /**********************************************/
90 #define COM_BASE_ADDR 0x10100
92 static void usage(void)
94 printf("qruncom version 0.1 (c) 2003 Fabrice Bellard\n"
95 "usage: qruncom file.com\n"
96 "user mode libqemu demo: run simple .com DOS executables\n");
100 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
102 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
105 static inline void pushw(CPUState
*env
, int val
)
107 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~0xffff) | ((env
->regs
[R_ESP
] - 2) & 0xffff);
108 *(uint16_t *)seg_to_linear(env
->segs
[R_SS
].selector
, env
->regs
[R_ESP
]) = val
;
111 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
114 if (cpu_signal_handler(host_signum
, info
, puc
)) {
120 int main(int argc
, char **argv
)
123 const char *filename
;
131 vm86_mem
= mmap((void *)0x00000000, 0x110000,
132 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
133 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
134 if (vm86_mem
== MAP_FAILED
) {
139 /* load the MSDOS .com executable */
140 fd
= open(filename
, O_RDONLY
);
145 ret
= read(fd
, vm86_mem
+ COM_BASE_ADDR
, 65536 - 256);
152 /* install exception handler for CPU emulator */
154 struct sigaction act
;
156 sigfillset(&act
.sa_mask
);
157 act
.sa_flags
= SA_SIGINFO
;
158 // act.sa_flags |= SA_ONSTACK;
160 act
.sa_sigaction
= host_segv_handler
;
161 sigaction(SIGSEGV
, &act
, NULL
);
162 sigaction(SIGBUS
, &act
, NULL
);
165 // cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_TB_OUT_ASM | CPU_LOG_EXEC);
167 env
= cpu_init("qemu32");
169 cpu_x86_set_cpl(env
, 3);
171 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
172 /* NOTE: hflags duplicates some of the virtual CPU state */
173 env
->hflags
|= HF_PE_MASK
| VM_MASK
;
175 /* flags setup : we activate the IRQs by default as in user
176 mode. We also activate the VM86 flag to run DOS code */
177 env
->eflags
|= IF_MASK
| VM_MASK
;
179 /* init basic registers */
181 env
->regs
[R_ESP
] = 0xfffe;
182 seg
= (COM_BASE_ADDR
- 0x100) >> 4;
184 cpu_x86_load_seg_cache(env
, R_CS
, seg
,
185 (seg
<< 4), 0xffff, 0);
186 cpu_x86_load_seg_cache(env
, R_SS
, seg
,
187 (seg
<< 4), 0xffff, 0);
188 cpu_x86_load_seg_cache(env
, R_DS
, seg
,
189 (seg
<< 4), 0xffff, 0);
190 cpu_x86_load_seg_cache(env
, R_ES
, seg
,
191 (seg
<< 4), 0xffff, 0);
192 cpu_x86_load_seg_cache(env
, R_FS
, seg
,
193 (seg
<< 4), 0xffff, 0);
194 cpu_x86_load_seg_cache(env
, R_GS
, seg
,
195 (seg
<< 4), 0xffff, 0);
197 /* exception support */
198 env
->idt
.base
= (unsigned long)idt_table
;
199 env
->idt
.limit
= sizeof(idt_table
) - 1;
221 /* put return code */
222 *seg_to_linear(env
->segs
[R_CS
].selector
, 0) = 0xb4; /* mov ah, $0 */
223 *seg_to_linear(env
->segs
[R_CS
].selector
, 1) = 0x00;
224 *seg_to_linear(env
->segs
[R_CS
].selector
, 2) = 0xcd; /* int $0x21 */
225 *seg_to_linear(env
->segs
[R_CS
].selector
, 3) = 0x21;
228 /* the value of these registers seem to be assumed by pi_10.com */
229 env
->regs
[R_ESI
] = 0x100;
230 env
->regs
[R_ECX
] = 0xff;
231 env
->regs
[R_EBP
] = 0x0900;
232 env
->regs
[R_EDI
] = 0xfffe;
234 /* inform the emulator of the mmaped memory */
235 page_set_flags(0x00000000, 0x110000,
236 PAGE_WRITE
| PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
239 ret
= cpu_x86_exec(env
);
244 int_num
= *(uint8_t *)(env
->segs
[R_CS
].base
+ env
->eip
+ 1);
247 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
249 case 0x00: /* exit */
251 case 0x02: /* write char */
253 uint8_t c
= env
->regs
[R_EDX
];
257 case 0x09: /* write string */
261 c
= *seg_to_linear(env
->segs
[R_DS
].selector
, env
->regs
[R_EAX
]);
266 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | '$';
271 fprintf(stderr
, "unsupported int 0x%02x\n", int_num
);
272 cpu_dump_state(env
, stderr
, fprintf
, 0);
279 fprintf(stderr
, "unhandled cpu_exec return code (0x%x)\n", ret
);
280 cpu_dump_state(env
, stderr
, fprintf
, 0);