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