Commit 62296fe3510d1f72b219223c36f11f3a4cf23107
1 parent
32f36bce
added runcom test
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@71 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
1 changed file
with
188 additions
and
0 deletions
tests/runcom.c
0 → 100644
| 1 | +/* | |
| 2 | + * Simple example of use of vm86: launch a basic .com DOS executable | |
| 3 | + */ | |
| 4 | +#include <stdlib.h> | |
| 5 | +#include <stdio.h> | |
| 6 | +#include <inttypes.h> | |
| 7 | +#include <unistd.h> | |
| 8 | +#include <fcntl.h> | |
| 9 | +#include <sys/mman.h> | |
| 10 | +#include <signal.h> | |
| 11 | + | |
| 12 | +#include <linux/unistd.h> | |
| 13 | +#include <asm/vm86.h> | |
| 14 | + | |
| 15 | +//#define SIGTEST | |
| 16 | + | |
| 17 | +_syscall2(int, vm86, int, func, struct vm86plus_struct *, v86) | |
| 18 | + | |
| 19 | +#define COM_BASE_ADDR 0x10100 | |
| 20 | + | |
| 21 | +void usage(void) | |
| 22 | +{ | |
| 23 | + printf("runcom version 0.1 (c) 2003 Fabrice Bellard\n" | |
| 24 | + "usage: runcom file.com\n" | |
| 25 | + "VM86 Run simple .com DOS executables (linux vm86 test mode)\n"); | |
| 26 | + exit(1); | |
| 27 | +} | |
| 28 | + | |
| 29 | +static inline void set_bit(uint8_t *a, unsigned int bit) | |
| 30 | +{ | |
| 31 | + a[bit / 8] |= (1 << (bit % 8)); | |
| 32 | +} | |
| 33 | + | |
| 34 | +static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) | |
| 35 | +{ | |
| 36 | + return (uint8_t *)((seg << 4) + (reg & 0xffff)); | |
| 37 | +} | |
| 38 | + | |
| 39 | +static inline void pushw(struct vm86_regs *r, int val) | |
| 40 | +{ | |
| 41 | + r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff); | |
| 42 | + *(uint16_t *)seg_to_linear(r->ss, r->esp) = val; | |
| 43 | +} | |
| 44 | + | |
| 45 | +void dump_regs(struct vm86_regs *r) | |
| 46 | +{ | |
| 47 | + fprintf(stderr, | |
| 48 | + "EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx\n" | |
| 49 | + "ESI=%08lx EDI=%08lx EBP=%08lx ESP=%08lx\n" | |
| 50 | + "EIP=%08lx EFL=%08lx\n" | |
| 51 | + "CS=%04x DS=%04x ES=%04x SS=%04x FS=%04x GS=%04x\n", | |
| 52 | + r->eax, r->ebx, r->ecx, r->edx, r->esi, r->edi, r->ebp, r->esp, | |
| 53 | + r->eip, r->eflags, | |
| 54 | + r->cs, r->ds, r->es, r->ss, r->fs, r->gs); | |
| 55 | +} | |
| 56 | + | |
| 57 | +#ifdef SIGTEST | |
| 58 | +void alarm_handler(int sig) | |
| 59 | +{ | |
| 60 | + fprintf(stderr, "alarm signal=%d\n", sig); | |
| 61 | + alarm(1); | |
| 62 | +} | |
| 63 | +#endif | |
| 64 | + | |
| 65 | +int main(int argc, char **argv) | |
| 66 | +{ | |
| 67 | + uint8_t *vm86_mem; | |
| 68 | + const char *filename; | |
| 69 | + int fd, ret, seg; | |
| 70 | + struct vm86plus_struct ctx; | |
| 71 | + struct vm86_regs *r; | |
| 72 | + | |
| 73 | + if (argc != 2) | |
| 74 | + usage(); | |
| 75 | + filename = argv[1]; | |
| 76 | + | |
| 77 | + vm86_mem = mmap((void *)0x00000000, 0x110000, | |
| 78 | + PROT_WRITE | PROT_READ | PROT_EXEC, | |
| 79 | + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); | |
| 80 | + if (vm86_mem == MAP_FAILED) { | |
| 81 | + perror("mmap"); | |
| 82 | + exit(1); | |
| 83 | + } | |
| 84 | +#ifdef SIGTEST | |
| 85 | + { | |
| 86 | + struct sigaction act; | |
| 87 | + | |
| 88 | + act.sa_handler = alarm_handler; | |
| 89 | + sigemptyset(&act.sa_mask); | |
| 90 | + act.sa_flags = 0; | |
| 91 | + sigaction(SIGALRM, &act, NULL); | |
| 92 | + alarm(1); | |
| 93 | + } | |
| 94 | +#endif | |
| 95 | + | |
| 96 | + /* load the MSDOS .com executable */ | |
| 97 | + fd = open(filename, O_RDONLY); | |
| 98 | + if (fd < 0) { | |
| 99 | + perror(filename); | |
| 100 | + exit(1); | |
| 101 | + } | |
| 102 | + ret = read(fd, vm86_mem + COM_BASE_ADDR, 65536 - 256); | |
| 103 | + if (ret < 0) { | |
| 104 | + perror("read"); | |
| 105 | + exit(1); | |
| 106 | + } | |
| 107 | + close(fd); | |
| 108 | + | |
| 109 | + memset(&ctx, 0, sizeof(ctx)); | |
| 110 | + /* init basic registers */ | |
| 111 | + r = &ctx.regs; | |
| 112 | + r->eip = 0x100; | |
| 113 | + r->esp = 0xfffe; | |
| 114 | + seg = (COM_BASE_ADDR - 0x100) >> 4; | |
| 115 | + r->cs = seg; | |
| 116 | + r->ss = seg; | |
| 117 | + r->ds = seg; | |
| 118 | + r->es = seg; | |
| 119 | + r->fs = seg; | |
| 120 | + r->gs = seg; | |
| 121 | + r->eflags = (IF_MASK | IOPL_MASK); | |
| 122 | + | |
| 123 | + /* put return code */ | |
| 124 | + set_bit((uint8_t *)&ctx.int_revectored, 0x21); | |
| 125 | + *seg_to_linear(r->cs, 0) = 0xb4; /* mov ah, $0 */ | |
| 126 | + *seg_to_linear(r->cs, 1) = 0x00; | |
| 127 | + *seg_to_linear(r->cs, 2) = 0xcd; /* int $0x21 */ | |
| 128 | + *seg_to_linear(r->cs, 3) = 0x21; | |
| 129 | + pushw(&ctx.regs, 0x0000); | |
| 130 | + | |
| 131 | + /* the value of these registers seem to be assumed by pi_10.com */ | |
| 132 | + r->esi = 0x100; | |
| 133 | + r->ecx = 0xff; | |
| 134 | + r->ebp = 0x0900; | |
| 135 | + r->edi = 0xfffe; | |
| 136 | + | |
| 137 | + for(;;) { | |
| 138 | + ret = vm86(VM86_ENTER, &ctx); | |
| 139 | + switch(VM86_TYPE(ret)) { | |
| 140 | + case VM86_INTx: | |
| 141 | + { | |
| 142 | + int int_num, ah; | |
| 143 | + | |
| 144 | + int_num = VM86_ARG(ret); | |
| 145 | + if (int_num != 0x21) | |
| 146 | + goto unknown_int; | |
| 147 | + ah = (r->eax >> 8) & 0xff; | |
| 148 | + switch(ah) { | |
| 149 | + case 0x00: /* exit */ | |
| 150 | + exit(0); | |
| 151 | + case 0x02: /* write char */ | |
| 152 | + { | |
| 153 | + uint8_t c = r->edx; | |
| 154 | + write(1, &c, 1); | |
| 155 | + } | |
| 156 | + break; | |
| 157 | + case 0x09: /* write string */ | |
| 158 | + { | |
| 159 | + uint8_t c; | |
| 160 | + for(;;) { | |
| 161 | + c = *seg_to_linear(r->ds, r->edx); | |
| 162 | + if (c == '$') | |
| 163 | + break; | |
| 164 | + write(1, &c, 1); | |
| 165 | + } | |
| 166 | + r->eax = (r->eax & ~0xff) | '$'; | |
| 167 | + } | |
| 168 | + break; | |
| 169 | + default: | |
| 170 | + unknown_int: | |
| 171 | + fprintf(stderr, "unsupported int 0x%02x\n", int_num); | |
| 172 | + dump_regs(&ctx.regs); | |
| 173 | + // exit(1); | |
| 174 | + } | |
| 175 | + } | |
| 176 | + break; | |
| 177 | + case VM86_SIGNAL: | |
| 178 | + /* a signal came, we just ignore that */ | |
| 179 | + break; | |
| 180 | + case VM86_STI: | |
| 181 | + break; | |
| 182 | + default: | |
| 183 | + fprintf(stderr, "unhandled vm86 return code (0x%x)\n", ret); | |
| 184 | + dump_regs(&ctx.regs); | |
| 185 | + exit(1); | |
| 186 | + } | |
| 187 | + } | |
| 188 | +} | ... | ... |