Commit 039d3da3653a76cc00c67be273799c4f0b007f88
1 parent
a7e61ed4
added user mode libqemu usage example
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@739 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
3 changed files
with
308 additions
and
0 deletions
tests/Makefile
| ... | ... | @@ -62,6 +62,11 @@ speed: sha1 sha1-i386 |
| 62 | 62 | runcom: runcom.c |
| 63 | 63 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< |
| 64 | 64 | |
| 65 | +# NOTE: -fomit-frame-pointer is currently needed : this is a bug in libqemu | |
| 66 | +qruncom: qruncom.c ../i386-user/libqemu.a | |
| 67 | + $(CC) $(CFLAGS) -fomit-frame-pointer $(LDFLAGS) -I../target-i386 -I.. -I../i386-user \ | |
| 68 | + -o $@ $< -L../i386-user -lqemu -lm | |
| 69 | + | |
| 65 | 70 | # arm test |
| 66 | 71 | hello-arm: hello-arm.o |
| 67 | 72 | arm-linux-ld -o $@ $< | ... | ... |
tests/pi_10.com
0 → 100644
No preview for this file type
tests/qruncom.c
0 → 100644
| 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 | +CPUState *cpu_single_env = NULL; | |
| 19 | + | |
| 20 | +void cpu_outb(CPUState *env, int addr, int val) | |
| 21 | +{ | |
| 22 | + fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val); | |
| 23 | +} | |
| 24 | + | |
| 25 | +void cpu_outw(CPUState *env, int addr, int val) | |
| 26 | +{ | |
| 27 | + fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val); | |
| 28 | +} | |
| 29 | + | |
| 30 | +void cpu_outl(CPUState *env, int addr, int val) | |
| 31 | +{ | |
| 32 | + fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val); | |
| 33 | +} | |
| 34 | + | |
| 35 | +int cpu_inb(CPUState *env, int addr) | |
| 36 | +{ | |
| 37 | + fprintf(stderr, "inb: port=0x%04x\n", addr); | |
| 38 | + return 0; | |
| 39 | +} | |
| 40 | + | |
| 41 | +int cpu_inw(CPUState *env, int addr) | |
| 42 | +{ | |
| 43 | + fprintf(stderr, "inw: port=0x%04x\n", addr); | |
| 44 | + return 0; | |
| 45 | +} | |
| 46 | + | |
| 47 | +int cpu_inl(CPUState *env, int addr) | |
| 48 | +{ | |
| 49 | + fprintf(stderr, "inl: port=0x%04x\n", addr); | |
| 50 | + return 0; | |
| 51 | +} | |
| 52 | + | |
| 53 | +int cpu_get_pic_interrupt(CPUState *env) | |
| 54 | +{ | |
| 55 | + return -1; | |
| 56 | +} | |
| 57 | + | |
| 58 | +static void set_gate(void *ptr, unsigned int type, unsigned int dpl, | |
| 59 | + unsigned long addr, unsigned int sel) | |
| 60 | +{ | |
| 61 | + unsigned int e1, e2; | |
| 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); | |
| 66 | +} | |
| 67 | + | |
| 68 | +uint64_t idt_table[256]; | |
| 69 | + | |
| 70 | +/* only dpl matters as we do only user space emulation */ | |
| 71 | +static void set_idt(int n, unsigned int dpl) | |
| 72 | +{ | |
| 73 | + set_gate(idt_table + n, 0, dpl, 0, 0); | |
| 74 | +} | |
| 75 | + | |
| 76 | +void qemu_free(void *ptr) | |
| 77 | +{ | |
| 78 | + free(ptr); | |
| 79 | +} | |
| 80 | + | |
| 81 | +void *qemu_malloc(size_t size) | |
| 82 | +{ | |
| 83 | + return malloc(size); | |
| 84 | +} | |
| 85 | + | |
| 86 | +void qemu_printf(const char *fmt, ...) | |
| 87 | +{ | |
| 88 | + va_list ap; | |
| 89 | + va_start(ap, fmt); | |
| 90 | + vprintf(fmt, ap); | |
| 91 | + va_end(ap); | |
| 92 | +} | |
| 93 | + | |
| 94 | +/* XXX: this is a bug in helper2.c */ | |
| 95 | +int errno; | |
| 96 | + | |
| 97 | +/**********************************************/ | |
| 98 | + | |
| 99 | +#define COM_BASE_ADDR 0x10100 | |
| 100 | + | |
| 101 | +void usage(void) | |
| 102 | +{ | |
| 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"); | |
| 106 | + exit(1); | |
| 107 | +} | |
| 108 | + | |
| 109 | +static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) | |
| 110 | +{ | |
| 111 | + return (uint8_t *)((seg << 4) + (reg & 0xffff)); | |
| 112 | +} | |
| 113 | + | |
| 114 | +static inline void pushw(CPUState *env, int val) | |
| 115 | +{ | |
| 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; | |
| 118 | +} | |
| 119 | + | |
| 120 | +static void host_segv_handler(int host_signum, siginfo_t *info, | |
| 121 | + void *puc) | |
| 122 | +{ | |
| 123 | + if (cpu_signal_handler(host_signum, info, puc)) { | |
| 124 | + return; | |
| 125 | + } | |
| 126 | + abort(); | |
| 127 | +} | |
| 128 | + | |
| 129 | +int main(int argc, char **argv) | |
| 130 | +{ | |
| 131 | + uint8_t *vm86_mem; | |
| 132 | + const char *filename; | |
| 133 | + int fd, ret, seg; | |
| 134 | + CPUState *env; | |
| 135 | + | |
| 136 | + if (argc != 2) | |
| 137 | + usage(); | |
| 138 | + filename = argv[1]; | |
| 139 | + | |
| 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) { | |
| 144 | + perror("mmap"); | |
| 145 | + exit(1); | |
| 146 | + } | |
| 147 | + | |
| 148 | + /* load the MSDOS .com executable */ | |
| 149 | + fd = open(filename, O_RDONLY); | |
| 150 | + if (fd < 0) { | |
| 151 | + perror(filename); | |
| 152 | + exit(1); | |
| 153 | + } | |
| 154 | + ret = read(fd, vm86_mem + COM_BASE_ADDR, 65536 - 256); | |
| 155 | + if (ret < 0) { | |
| 156 | + perror("read"); | |
| 157 | + exit(1); | |
| 158 | + } | |
| 159 | + close(fd); | |
| 160 | + | |
| 161 | + /* install exception handler for CPU emulator */ | |
| 162 | + { | |
| 163 | + struct sigaction act; | |
| 164 | + | |
| 165 | + sigfillset(&act.sa_mask); | |
| 166 | + act.sa_flags = SA_SIGINFO; | |
| 167 | + // act.sa_flags |= SA_ONSTACK; | |
| 168 | + | |
| 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); | |
| 174 | +#endif | |
| 175 | + } | |
| 176 | + | |
| 177 | + // cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_TB_OUT_ASM | CPU_LOG_EXEC); | |
| 178 | + | |
| 179 | + env = cpu_init(); | |
| 180 | + | |
| 181 | + /* disable code copy to simplify debugging */ | |
| 182 | + code_copy_enabled = 0; | |
| 183 | + | |
| 184 | + /* set user mode state (XXX: should be done automatically by | |
| 185 | + cpu_init ?) */ | |
| 186 | + env->user_mode_only = 1; | |
| 187 | + | |
| 188 | + cpu_x86_set_cpl(env, 3); | |
| 189 | + | |
| 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; | |
| 193 | + | |
| 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; | |
| 197 | + | |
| 198 | + /* init basic registers */ | |
| 199 | + env->eip = 0x100; | |
| 200 | + env->regs[R_ESP] = 0xfffe; | |
| 201 | + seg = (COM_BASE_ADDR - 0x100) >> 4; | |
| 202 | + | |
| 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); | |
| 215 | + | |
| 216 | + /* exception support */ | |
| 217 | + env->idt.base = (void *)idt_table; | |
| 218 | + env->idt.limit = sizeof(idt_table) - 1; | |
| 219 | + set_idt(0, 0); | |
| 220 | + set_idt(1, 0); | |
| 221 | + set_idt(2, 0); | |
| 222 | + set_idt(3, 3); | |
| 223 | + set_idt(4, 3); | |
| 224 | + set_idt(5, 3); | |
| 225 | + set_idt(6, 0); | |
| 226 | + set_idt(7, 0); | |
| 227 | + set_idt(8, 0); | |
| 228 | + set_idt(9, 0); | |
| 229 | + set_idt(10, 0); | |
| 230 | + set_idt(11, 0); | |
| 231 | + set_idt(12, 0); | |
| 232 | + set_idt(13, 0); | |
| 233 | + set_idt(14, 0); | |
| 234 | + set_idt(15, 0); | |
| 235 | + set_idt(16, 0); | |
| 236 | + set_idt(17, 0); | |
| 237 | + set_idt(18, 0); | |
| 238 | + set_idt(19, 0); | |
| 239 | + | |
| 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; | |
| 245 | + pushw(env, 0x0000); | |
| 246 | + | |
| 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; | |
| 252 | + | |
| 253 | + /* inform the emulator of the mmaped memory */ | |
| 254 | + page_set_flags(0x00000000, 0x110000, | |
| 255 | + PAGE_WRITE | PAGE_READ | PAGE_EXEC | PAGE_VALID); | |
| 256 | + | |
| 257 | + for(;;) { | |
| 258 | + ret = cpu_x86_exec(env); | |
| 259 | + switch(ret) { | |
| 260 | + case EXCP0D_GPF: | |
| 261 | + { | |
| 262 | + int int_num, ah; | |
| 263 | + int_num = *(env->segs[R_CS].base + env->eip + 1); | |
| 264 | + if (int_num != 0x21) | |
| 265 | + goto unknown_int; | |
| 266 | + ah = (env->regs[R_EAX] >> 8) & 0xff; | |
| 267 | + switch(ah) { | |
| 268 | + case 0x00: /* exit */ | |
| 269 | + exit(0); | |
| 270 | + case 0x02: /* write char */ | |
| 271 | + { | |
| 272 | + uint8_t c = env->regs[R_EDX]; | |
| 273 | + write(1, &c, 1); | |
| 274 | + } | |
| 275 | + break; | |
| 276 | + case 0x09: /* write string */ | |
| 277 | + { | |
| 278 | + uint8_t c; | |
| 279 | + for(;;) { | |
| 280 | + c = *seg_to_linear(env->segs[R_DS].selector, env->regs[R_EAX]); | |
| 281 | + if (c == '$') | |
| 282 | + break; | |
| 283 | + write(1, &c, 1); | |
| 284 | + } | |
| 285 | + env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | '$'; | |
| 286 | + } | |
| 287 | + break; | |
| 288 | + default: | |
| 289 | + unknown_int: | |
| 290 | + fprintf(stderr, "unsupported int 0x%02x\n", int_num); | |
| 291 | + cpu_dump_state(env, stderr, 0); | |
| 292 | + // exit(1); | |
| 293 | + } | |
| 294 | + env->eip += 2; | |
| 295 | + } | |
| 296 | + break; | |
| 297 | + default: | |
| 298 | + fprintf(stderr, "unhandled cpu_exec return code (0x%x)\n", ret); | |
| 299 | + cpu_dump_state(env, stderr, 0); | |
| 300 | + exit(1); | |
| 301 | + } | |
| 302 | + } | |
| 303 | +} | ... | ... |