root / linux-user / main.c @ 0ecfa993
History | View | Annotate | Download (4.9 kB)
1 |
/*
|
---|---|
2 |
* emu main
|
3 |
*
|
4 |
* Copyright (c) 2003 Fabrice Bellard
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation; either version 2 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
19 |
*/
|
20 |
#include <stdlib.h> |
21 |
#include <stdio.h> |
22 |
#include <stdarg.h> |
23 |
#include <errno.h> |
24 |
#include <unistd.h> |
25 |
|
26 |
#include "gemu.h" |
27 |
|
28 |
#include "cpu-i386.h" |
29 |
|
30 |
unsigned long x86_stack_size; |
31 |
unsigned long stktop; |
32 |
|
33 |
void gemu_log(const char *fmt, ...) |
34 |
{ |
35 |
va_list ap; |
36 |
|
37 |
va_start(ap, fmt); |
38 |
vfprintf(stderr, fmt, ap); |
39 |
va_end(ap); |
40 |
} |
41 |
|
42 |
/***********************************************************/
|
43 |
/* CPUX86 core interface */
|
44 |
|
45 |
void cpu_x86_outb(int addr, int val) |
46 |
{ |
47 |
fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
|
48 |
} |
49 |
|
50 |
void cpu_x86_outw(int addr, int val) |
51 |
{ |
52 |
fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
|
53 |
} |
54 |
|
55 |
void cpu_x86_outl(int addr, int val) |
56 |
{ |
57 |
fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
|
58 |
} |
59 |
|
60 |
int cpu_x86_inb(int addr) |
61 |
{ |
62 |
fprintf(stderr, "inb: port=0x%04x\n", addr);
|
63 |
return 0; |
64 |
} |
65 |
|
66 |
int cpu_x86_inw(int addr) |
67 |
{ |
68 |
fprintf(stderr, "inw: port=0x%04x\n", addr);
|
69 |
return 0; |
70 |
} |
71 |
|
72 |
int cpu_x86_inl(int addr) |
73 |
{ |
74 |
fprintf(stderr, "inl: port=0x%04x\n", addr);
|
75 |
return 0; |
76 |
} |
77 |
|
78 |
|
79 |
/* XXX: currently we use LDT entries */
|
80 |
#define __USER_CS (0x23|4) |
81 |
#define __USER_DS (0x2B|4) |
82 |
|
83 |
void usage(void) |
84 |
{ |
85 |
printf("gemu version 0.1, Copyright (c) 2003 Fabrice Bellard\n"
|
86 |
"usage: gemu program [arguments...]\n"
|
87 |
"Linux x86 emulator\n"
|
88 |
); |
89 |
exit(1);
|
90 |
} |
91 |
|
92 |
int main(int argc, char **argv) |
93 |
{ |
94 |
const char *filename; |
95 |
struct target_pt_regs regs1, *regs = ®s1;
|
96 |
struct image_info info1, *info = &info1;
|
97 |
CPUX86State *env; |
98 |
|
99 |
if (argc <= 1) |
100 |
usage(); |
101 |
|
102 |
filename = argv[1];
|
103 |
|
104 |
/* Zero out regs */
|
105 |
memset(regs, 0, sizeof(struct target_pt_regs)); |
106 |
|
107 |
/* Zero out image_info */
|
108 |
memset(info, 0, sizeof(struct image_info)); |
109 |
|
110 |
if(elf_exec(filename, argv+1, environ, regs, info) != 0) { |
111 |
printf("Error loading %s\n", filename);
|
112 |
exit(1);
|
113 |
} |
114 |
|
115 |
#if 0
|
116 |
printf("start_brk 0x%08lx\n" , info->start_brk);
|
117 |
printf("end_code 0x%08lx\n" , info->end_code);
|
118 |
printf("start_code 0x%08lx\n" , info->start_code);
|
119 |
printf("end_data 0x%08lx\n" , info->end_data);
|
120 |
printf("start_stack 0x%08lx\n" , info->start_stack);
|
121 |
printf("brk 0x%08lx\n" , info->brk);
|
122 |
printf("esp 0x%08lx\n" , regs->esp);
|
123 |
printf("eip 0x%08lx\n" , regs->eip);
|
124 |
#endif
|
125 |
|
126 |
target_set_brk((char *)info->brk);
|
127 |
syscall_init(); |
128 |
|
129 |
env = cpu_x86_init(); |
130 |
|
131 |
env->regs[R_EAX] = regs->eax; |
132 |
env->regs[R_EBX] = regs->ebx; |
133 |
env->regs[R_ECX] = regs->ecx; |
134 |
env->regs[R_EDX] = regs->edx; |
135 |
env->regs[R_ESI] = regs->esi; |
136 |
env->regs[R_EDI] = regs->edi; |
137 |
env->regs[R_EBP] = regs->ebp; |
138 |
env->regs[R_ESP] = regs->esp; |
139 |
env->segs[R_CS] = __USER_CS; |
140 |
env->segs[R_DS] = __USER_DS; |
141 |
env->segs[R_ES] = __USER_DS; |
142 |
env->segs[R_SS] = __USER_DS; |
143 |
env->segs[R_FS] = __USER_DS; |
144 |
env->segs[R_GS] = __USER_DS; |
145 |
env->pc = regs->eip; |
146 |
|
147 |
#if 0
|
148 |
LDT[__USER_CS >> 3].w86Flags = DF_PRESENT | DF_PAGES | DF_32;
|
149 |
LDT[__USER_CS >> 3].dwSelLimit = 0xfffff;
|
150 |
LDT[__USER_CS >> 3].lpSelBase = NULL;
|
151 |
|
152 |
LDT[__USER_DS >> 3].w86Flags = DF_PRESENT | DF_PAGES | DF_32;
|
153 |
LDT[__USER_DS >> 3].dwSelLimit = 0xfffff;
|
154 |
LDT[__USER_DS >> 3].lpSelBase = NULL;
|
155 |
#endif
|
156 |
|
157 |
for(;;) {
|
158 |
int err;
|
159 |
uint8_t *pc; |
160 |
|
161 |
err = cpu_x86_exec(env); |
162 |
switch(err) {
|
163 |
case EXCP0D_GPF:
|
164 |
pc = (uint8_t *)env->pc; |
165 |
if (pc[0] == 0xcd && pc[1] == 0x80) { |
166 |
/* syscall */
|
167 |
env->pc += 2;
|
168 |
env->regs[R_EAX] = do_syscall(env->regs[R_EAX], |
169 |
env->regs[R_EBX], |
170 |
env->regs[R_ECX], |
171 |
env->regs[R_EDX], |
172 |
env->regs[R_ESI], |
173 |
env->regs[R_EDI], |
174 |
env->regs[R_EBP]); |
175 |
} else {
|
176 |
goto trap_error;
|
177 |
} |
178 |
break;
|
179 |
default:
|
180 |
trap_error:
|
181 |
fprintf(stderr, "0x%08lx: Unknown exception %d, aborting\n",
|
182 |
(long)env->pc, err);
|
183 |
abort(); |
184 |
} |
185 |
} |
186 |
return 0; |
187 |
} |