|
|
1.1 root 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>
1.1.1.2 root 13: #include <malloc.h>
1.1 root 14:
15: #include "cpu.h"
16:
17: //#define SIGTEST
18:
19: int cpu_get_pic_interrupt(CPUState *env)
20: {
21: return -1;
22: }
23:
24: uint64_t cpu_get_tsc(CPUState *env)
25: {
26: return 0;
27: }
28:
1.1.1.3 root 29: static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
1.1 root 30: unsigned long addr, unsigned int sel)
31: {
32: unsigned int e1, e2;
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);
37: }
38:
39: uint64_t idt_table[256];
40:
41: /* only dpl matters as we do only user space emulation */
42: static void set_idt(int n, unsigned int dpl)
43: {
44: set_gate(idt_table + n, 0, dpl, 0, 0);
45: }
46:
1.1.1.7 ! root 47: void g_free(void *ptr)
1.1 root 48: {
49: free(ptr);
50: }
51:
1.1.1.7 ! root 52: void *g_malloc(size_t size)
1.1 root 53: {
54: return malloc(size);
55: }
56:
1.1.1.7 ! root 57: void *g_malloc0(size_t size)
1.1.1.2 root 58: {
59: void *ptr;
1.1.1.7 ! root 60: ptr = g_malloc(size);
1.1.1.2 root 61: if (!ptr)
62: return NULL;
63: memset(ptr, 0, size);
64: return ptr;
65: }
66:
67: void *qemu_vmalloc(size_t size)
68: {
69: return memalign(4096, size);
70: }
71:
72: void qemu_vfree(void *ptr)
73: {
74: free(ptr);
75: }
76:
1.1 root 77: void qemu_printf(const char *fmt, ...)
78: {
79: va_list ap;
80: va_start(ap, fmt);
81: vprintf(fmt, ap);
82: va_end(ap);
83: }
84:
85: /* XXX: this is a bug in helper2.c */
86: int errno;
87:
88: /**********************************************/
89:
90: #define COM_BASE_ADDR 0x10100
91:
1.1.1.6 root 92: static void usage(void)
1.1 root 93: {
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");
97: exit(1);
98: }
99:
100: static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
101: {
102: return (uint8_t *)((seg << 4) + (reg & 0xffff));
103: }
104:
105: static inline void pushw(CPUState *env, int val)
106: {
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;
109: }
110:
1.1.1.3 root 111: static void host_segv_handler(int host_signum, siginfo_t *info,
1.1 root 112: void *puc)
113: {
114: if (cpu_signal_handler(host_signum, info, puc)) {
115: return;
116: }
117: abort();
118: }
119:
120: int main(int argc, char **argv)
121: {
122: uint8_t *vm86_mem;
123: const char *filename;
124: int fd, ret, seg;
125: CPUState *env;
126:
127: if (argc != 2)
128: usage();
129: filename = argv[1];
1.1.1.3 root 130:
131: vm86_mem = mmap((void *)0x00000000, 0x110000,
132: PROT_WRITE | PROT_READ | PROT_EXEC,
1.1 root 133: MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
134: if (vm86_mem == MAP_FAILED) {
135: perror("mmap");
136: exit(1);
137: }
138:
139: /* load the MSDOS .com executable */
140: fd = open(filename, O_RDONLY);
141: if (fd < 0) {
142: perror(filename);
143: exit(1);
144: }
145: ret = read(fd, vm86_mem + COM_BASE_ADDR, 65536 - 256);
146: if (ret < 0) {
147: perror("read");
148: exit(1);
149: }
150: close(fd);
151:
152: /* install exception handler for CPU emulator */
153: {
154: struct sigaction act;
1.1.1.3 root 155:
1.1 root 156: sigfillset(&act.sa_mask);
157: act.sa_flags = SA_SIGINFO;
158: // act.sa_flags |= SA_ONSTACK;
159:
160: act.sa_sigaction = host_segv_handler;
161: sigaction(SIGSEGV, &act, NULL);
162: sigaction(SIGBUS, &act, NULL);
163: }
164:
165: // cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_TB_OUT_ASM | CPU_LOG_EXEC);
166:
1.1.1.3 root 167: env = cpu_init("qemu32");
1.1 root 168:
169: cpu_x86_set_cpl(env, 3);
170:
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;
174:
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;
1.1.1.3 root 178:
1.1 root 179: /* init basic registers */
180: env->eip = 0x100;
181: env->regs[R_ESP] = 0xfffe;
182: seg = (COM_BASE_ADDR - 0x100) >> 4;
183:
1.1.1.3 root 184: cpu_x86_load_seg_cache(env, R_CS, seg,
1.1.1.2 root 185: (seg << 4), 0xffff, 0);
1.1.1.3 root 186: cpu_x86_load_seg_cache(env, R_SS, seg,
1.1.1.2 root 187: (seg << 4), 0xffff, 0);
1.1.1.3 root 188: cpu_x86_load_seg_cache(env, R_DS, seg,
1.1.1.2 root 189: (seg << 4), 0xffff, 0);
1.1.1.3 root 190: cpu_x86_load_seg_cache(env, R_ES, seg,
1.1.1.2 root 191: (seg << 4), 0xffff, 0);
1.1.1.3 root 192: cpu_x86_load_seg_cache(env, R_FS, seg,
1.1.1.2 root 193: (seg << 4), 0xffff, 0);
1.1.1.3 root 194: cpu_x86_load_seg_cache(env, R_GS, seg,
1.1.1.2 root 195: (seg << 4), 0xffff, 0);
1.1 root 196:
197: /* exception support */
1.1.1.2 root 198: env->idt.base = (unsigned long)idt_table;
1.1 root 199: env->idt.limit = sizeof(idt_table) - 1;
200: set_idt(0, 0);
201: set_idt(1, 0);
202: set_idt(2, 0);
203: set_idt(3, 3);
204: set_idt(4, 3);
205: set_idt(5, 3);
206: set_idt(6, 0);
207: set_idt(7, 0);
208: set_idt(8, 0);
209: set_idt(9, 0);
210: set_idt(10, 0);
211: set_idt(11, 0);
212: set_idt(12, 0);
213: set_idt(13, 0);
214: set_idt(14, 0);
215: set_idt(15, 0);
216: set_idt(16, 0);
217: set_idt(17, 0);
218: set_idt(18, 0);
219: set_idt(19, 0);
1.1.1.3 root 220:
1.1 root 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;
226: pushw(env, 0x0000);
227:
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;
233:
234: /* inform the emulator of the mmaped memory */
1.1.1.3 root 235: page_set_flags(0x00000000, 0x110000,
1.1 root 236: PAGE_WRITE | PAGE_READ | PAGE_EXEC | PAGE_VALID);
237:
238: for(;;) {
239: ret = cpu_x86_exec(env);
240: switch(ret) {
241: case EXCP0D_GPF:
242: {
243: int int_num, ah;
1.1.1.2 root 244: int_num = *(uint8_t *)(env->segs[R_CS].base + env->eip + 1);
1.1 root 245: if (int_num != 0x21)
246: goto unknown_int;
247: ah = (env->regs[R_EAX] >> 8) & 0xff;
248: switch(ah) {
249: case 0x00: /* exit */
250: exit(0);
251: case 0x02: /* write char */
252: {
253: uint8_t c = env->regs[R_EDX];
254: write(1, &c, 1);
255: }
256: break;
257: case 0x09: /* write string */
258: {
259: uint8_t c;
260: for(;;) {
261: c = *seg_to_linear(env->segs[R_DS].selector, env->regs[R_EAX]);
262: if (c == '$')
263: break;
264: write(1, &c, 1);
265: }
266: env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | '$';
267: }
268: break;
269: default:
270: unknown_int:
271: fprintf(stderr, "unsupported int 0x%02x\n", int_num);
1.1.1.2 root 272: cpu_dump_state(env, stderr, fprintf, 0);
1.1 root 273: // exit(1);
274: }
275: env->eip += 2;
276: }
277: break;
278: default:
279: fprintf(stderr, "unhandled cpu_exec return code (0x%x)\n", ret);
1.1.1.2 root 280: cpu_dump_state(env, stderr, fprintf, 0);
1.1 root 281: exit(1);
282: }
283: }
284: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.