2 * Example of use of user mode libqemu: launch a basic .com DOS
19 void cpu_outb(CPUState
*env
, int addr
, int val
)
21 fprintf(stderr
, "outb: port=0x%04x, data=%02x\n", addr
, val
);
24 void cpu_outw(CPUState
*env
, int addr
, int val
)
26 fprintf(stderr
, "outw: port=0x%04x, data=%04x\n", addr
, val
);
29 void cpu_outl(CPUState
*env
, int addr
, int val
)
31 fprintf(stderr
, "outl: port=0x%04x, data=%08x\n", addr
, val
);
34 int cpu_inb(CPUState
*env
, int addr
)
36 fprintf(stderr
, "inb: port=0x%04x\n", addr
);
40 int cpu_inw(CPUState
*env
, int addr
)
42 fprintf(stderr
, "inw: port=0x%04x\n", addr
);
46 int cpu_inl(CPUState
*env
, int addr
)
48 fprintf(stderr
, "inl: port=0x%04x\n", addr
);
52 int cpu_get_pic_interrupt(CPUState
*env
)
57 uint64_t cpu_get_tsc(CPUState
*env
)
62 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
63 unsigned long addr
, unsigned int sel
)
66 e1
= (addr
& 0xffff) | (sel
<< 16);
67 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
68 stl((uint8_t *)ptr
, e1
);
69 stl((uint8_t *)ptr
+ 4, e2
);
72 uint64_t idt_table
[256];
74 /* only dpl matters as we do only user space emulation */
75 static void set_idt(int n
, unsigned int dpl
)
77 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
80 void qemu_free(void *ptr
)
85 void *qemu_malloc(size_t size
)
90 void *qemu_mallocz(size_t size
)
93 ptr
= qemu_malloc(size
);
100 void *qemu_vmalloc(size_t size
)
102 return memalign(4096, size
);
105 void qemu_vfree(void *ptr
)
110 void qemu_printf(const char *fmt
, ...)
118 /* XXX: this is a bug in helper2.c */
121 /**********************************************/
123 #define COM_BASE_ADDR 0x10100
127 printf("qruncom version 0.1 (c) 2003 Fabrice Bellard\n"
128 "usage: qruncom file.com\n"
129 "user mode libqemu demo: run simple .com DOS executables\n");
133 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
135 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
138 static inline void pushw(CPUState
*env
, int val
)
140 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~0xffff) | ((env
->regs
[R_ESP
] - 2) & 0xffff);
141 *(uint16_t *)seg_to_linear(env
->segs
[R_SS
].selector
, env
->regs
[R_ESP
]) = val
;
144 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
147 if (cpu_signal_handler(host_signum
, info
, puc
)) {
153 int main(int argc
, char **argv
)
156 const char *filename
;
164 vm86_mem
= mmap((void *)0x00000000, 0x110000,
165 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
166 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
167 if (vm86_mem
== MAP_FAILED
) {
172 /* load the MSDOS .com executable */
173 fd
= open(filename
, O_RDONLY
);
178 ret
= read(fd
, vm86_mem
+ COM_BASE_ADDR
, 65536 - 256);
185 /* install exception handler for CPU emulator */
187 struct sigaction act
;
189 sigfillset(&act
.sa_mask
);
190 act
.sa_flags
= SA_SIGINFO
;
191 // act.sa_flags |= SA_ONSTACK;
193 act
.sa_sigaction
= host_segv_handler
;
194 sigaction(SIGSEGV
, &act
, NULL
);
195 sigaction(SIGBUS
, &act
, NULL
);
196 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
197 sigaction(SIGFPE
, &act
, NULL
);
201 // cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_TB_OUT_ASM | CPU_LOG_EXEC);
205 /* disable code copy to simplify debugging */
206 code_copy_enabled
= 0;
208 /* set user mode state (XXX: should be done automatically by
210 env
->user_mode_only
= 1;
212 cpu_x86_set_cpl(env
, 3);
214 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
215 /* NOTE: hflags duplicates some of the virtual CPU state */
216 env
->hflags
|= HF_PE_MASK
| VM_MASK
;
218 /* flags setup : we activate the IRQs by default as in user
219 mode. We also activate the VM86 flag to run DOS code */
220 env
->eflags
|= IF_MASK
| VM_MASK
;
222 /* init basic registers */
224 env
->regs
[R_ESP
] = 0xfffe;
225 seg
= (COM_BASE_ADDR
- 0x100) >> 4;
227 cpu_x86_load_seg_cache(env
, R_CS
, seg
,
228 (seg
<< 4), 0xffff, 0);
229 cpu_x86_load_seg_cache(env
, R_SS
, seg
,
230 (seg
<< 4), 0xffff, 0);
231 cpu_x86_load_seg_cache(env
, R_DS
, seg
,
232 (seg
<< 4), 0xffff, 0);
233 cpu_x86_load_seg_cache(env
, R_ES
, seg
,
234 (seg
<< 4), 0xffff, 0);
235 cpu_x86_load_seg_cache(env
, R_FS
, seg
,
236 (seg
<< 4), 0xffff, 0);
237 cpu_x86_load_seg_cache(env
, R_GS
, seg
,
238 (seg
<< 4), 0xffff, 0);
240 /* exception support */
241 env
->idt
.base
= (unsigned long)idt_table
;
242 env
->idt
.limit
= sizeof(idt_table
) - 1;
264 /* put return code */
265 *seg_to_linear(env
->segs
[R_CS
].selector
, 0) = 0xb4; /* mov ah, $0 */
266 *seg_to_linear(env
->segs
[R_CS
].selector
, 1) = 0x00;
267 *seg_to_linear(env
->segs
[R_CS
].selector
, 2) = 0xcd; /* int $0x21 */
268 *seg_to_linear(env
->segs
[R_CS
].selector
, 3) = 0x21;
271 /* the value of these registers seem to be assumed by pi_10.com */
272 env
->regs
[R_ESI
] = 0x100;
273 env
->regs
[R_ECX
] = 0xff;
274 env
->regs
[R_EBP
] = 0x0900;
275 env
->regs
[R_EDI
] = 0xfffe;
277 /* inform the emulator of the mmaped memory */
278 page_set_flags(0x00000000, 0x110000,
279 PAGE_WRITE
| PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
282 ret
= cpu_x86_exec(env
);
287 int_num
= *(uint8_t *)(env
->segs
[R_CS
].base
+ env
->eip
+ 1);
290 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
292 case 0x00: /* exit */
294 case 0x02: /* write char */
296 uint8_t c
= env
->regs
[R_EDX
];
300 case 0x09: /* write string */
304 c
= *seg_to_linear(env
->segs
[R_DS
].selector
, env
->regs
[R_EAX
]);
309 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | '$';
314 fprintf(stderr
, "unsupported int 0x%02x\n", int_num
);
315 cpu_dump_state(env
, stderr
, fprintf
, 0);
322 fprintf(stderr
, "unhandled cpu_exec return code (0x%x)\n", ret
);
323 cpu_dump_state(env
, stderr
, fprintf
, 0);