VirtualBox

source: vbox/trunk/src/recompiler/tests/runcom.c@ 76686

Last change on this file since 76686 was 69465, checked in by vboxsync, 7 years ago

recompiler: scm updates

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette