2 * Example of use of user mode libqemu: launch a basic .com DOS
18 CPUState
*cpu_single_env
= NULL
;
20 void cpu_outb(CPUState
*env
, int addr
, int val
)
22 fprintf(stderr
, "outb: port=0x%04x, data=%02x\n", addr
, val
);
25 void cpu_outw(CPUState
*env
, int addr
, int val
)
27 fprintf(stderr
, "outw: port=0x%04x, data=%04x\n", addr
, val
);
30 void cpu_outl(CPUState
*env
, int addr
, int val
)
32 fprintf(stderr
, "outl: port=0x%04x, data=%08x\n", addr
, val
);
35 int cpu_inb(CPUState
*env
, int addr
)
37 fprintf(stderr
, "inb: port=0x%04x\n", addr
);
41 int cpu_inw(CPUState
*env
, int addr
)
43 fprintf(stderr
, "inw: port=0x%04x\n", addr
);
47 int cpu_inl(CPUState
*env
, int addr
)
49 fprintf(stderr
, "inl: port=0x%04x\n", addr
);
53 int cpu_get_pic_interrupt(CPUState
*env
)
58 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
59 unsigned long addr
, unsigned int sel
)
62 e1
= (addr
& 0xffff) | (sel
<< 16);
63 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
64 stl((uint8_t *)ptr
, e1
);
65 stl((uint8_t *)ptr
+ 4, e2
);
68 uint64_t idt_table
[256];
70 /* only dpl matters as we do only user space emulation */
71 static void set_idt(int n
, unsigned int dpl
)
73 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
76 void qemu_free(void *ptr
)
81 void *qemu_malloc(size_t size
)
86 void qemu_printf(const char *fmt
, ...)
94 /* XXX: this is a bug in helper2.c */
97 /**********************************************/
99 #define COM_BASE_ADDR 0x10100
103 printf("qruncom version 0.1 (c) 2003 Fabrice Bellard\n"
104 "usage: qruncom file.com\n"
105 "user mode libqemu demo: run simple .com DOS executables\n");
109 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
111 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
114 static inline void pushw(CPUState
*env
, int val
)
116 env
->regs
[R_ESP
] = (env
->regs
[R_ESP
] & ~0xffff) | ((env
->regs
[R_ESP
] - 2) & 0xffff);
117 *(uint16_t *)seg_to_linear(env
->segs
[R_SS
].selector
, env
->regs
[R_ESP
]) = val
;
120 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
123 if (cpu_signal_handler(host_signum
, info
, puc
)) {
129 int main(int argc
, char **argv
)
132 const char *filename
;
140 vm86_mem
= mmap((void *)0x00000000, 0x110000,
141 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
142 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
143 if (vm86_mem
== MAP_FAILED
) {
148 /* load the MSDOS .com executable */
149 fd
= open(filename
, O_RDONLY
);
154 ret
= read(fd
, vm86_mem
+ COM_BASE_ADDR
, 65536 - 256);
161 /* install exception handler for CPU emulator */
163 struct sigaction act
;
165 sigfillset(&act
.sa_mask
);
166 act
.sa_flags
= SA_SIGINFO
;
167 // act.sa_flags |= SA_ONSTACK;
169 act
.sa_sigaction
= host_segv_handler
;
170 sigaction(SIGSEGV
, &act
, NULL
);
171 sigaction(SIGBUS
, &act
, NULL
);
172 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
173 sigaction(SIGFPE
, &act
, NULL
);
177 // cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_TB_OUT_ASM | CPU_LOG_EXEC);
181 /* disable code copy to simplify debugging */
182 code_copy_enabled
= 0;
184 /* set user mode state (XXX: should be done automatically by
186 env
->user_mode_only
= 1;
188 cpu_x86_set_cpl(env
, 3);
190 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
191 /* NOTE: hflags duplicates some of the virtual CPU state */
192 env
->hflags
|= HF_PE_MASK
| VM_MASK
;
194 /* flags setup : we activate the IRQs by default as in user
195 mode. We also activate the VM86 flag to run DOS code */
196 env
->eflags
|= IF_MASK
| VM_MASK
;
198 /* init basic registers */
200 env
->regs
[R_ESP
] = 0xfffe;
201 seg
= (COM_BASE_ADDR
- 0x100) >> 4;
203 cpu_x86_load_seg_cache(env
, R_CS
, seg
,
204 (uint8_t *)(seg
<< 4), 0xffff, 0);
205 cpu_x86_load_seg_cache(env
, R_SS
, seg
,
206 (uint8_t *)(seg
<< 4), 0xffff, 0);
207 cpu_x86_load_seg_cache(env
, R_DS
, seg
,
208 (uint8_t *)(seg
<< 4), 0xffff, 0);
209 cpu_x86_load_seg_cache(env
, R_ES
, seg
,
210 (uint8_t *)(seg
<< 4), 0xffff, 0);
211 cpu_x86_load_seg_cache(env
, R_FS
, seg
,
212 (uint8_t *)(seg
<< 4), 0xffff, 0);
213 cpu_x86_load_seg_cache(env
, R_GS
, seg
,
214 (uint8_t *)(seg
<< 4), 0xffff, 0);
216 /* exception support */
217 env
->idt
.base
= (void *)idt_table
;
218 env
->idt
.limit
= sizeof(idt_table
) - 1;
240 /* put return code */
241 *seg_to_linear(env
->segs
[R_CS
].selector
, 0) = 0xb4; /* mov ah, $0 */
242 *seg_to_linear(env
->segs
[R_CS
].selector
, 1) = 0x00;
243 *seg_to_linear(env
->segs
[R_CS
].selector
, 2) = 0xcd; /* int $0x21 */
244 *seg_to_linear(env
->segs
[R_CS
].selector
, 3) = 0x21;
247 /* the value of these registers seem to be assumed by pi_10.com */
248 env
->regs
[R_ESI
] = 0x100;
249 env
->regs
[R_ECX
] = 0xff;
250 env
->regs
[R_EBP
] = 0x0900;
251 env
->regs
[R_EDI
] = 0xfffe;
253 /* inform the emulator of the mmaped memory */
254 page_set_flags(0x00000000, 0x110000,
255 PAGE_WRITE
| PAGE_READ
| PAGE_EXEC
| PAGE_VALID
);
258 ret
= cpu_x86_exec(env
);
263 int_num
= *(env
->segs
[R_CS
].base
+ env
->eip
+ 1);
266 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
268 case 0x00: /* exit */
270 case 0x02: /* write char */
272 uint8_t c
= env
->regs
[R_EDX
];
276 case 0x09: /* write string */
280 c
= *seg_to_linear(env
->segs
[R_DS
].selector
, env
->regs
[R_EAX
]);
285 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | '$';
290 fprintf(stderr
, "unsupported int 0x%02x\n", int_num
);
291 cpu_dump_state(env
, stderr
, 0);
298 fprintf(stderr
, "unhandled cpu_exec return code (0x%x)\n", ret
);
299 cpu_dump_state(env
, stderr
, 0);