]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/main.c
glibc2.2 fixes - more command line options - misc doc fixes
[mirror_qemu.git] / linux-user / main.c
1 /*
2 * qemu 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 <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include "qemu.h"
28
29 #include "cpu-i386.h"
30
31 #define DEBUG_LOGFILE "/tmp/qemu.log"
32
33 FILE *logfile = NULL;
34 int loglevel;
35 const char *interp_prefix = CONFIG_QEMU_PREFIX "/qemu-i386";
36
37 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
38 we allocate a bigger stack. Need a better solution, for example
39 by remapping the process stack directly at the right place */
40 unsigned long x86_stack_size = 512 * 1024;
41 unsigned long stktop;
42
43 void gemu_log(const char *fmt, ...)
44 {
45 va_list ap;
46
47 va_start(ap, fmt);
48 vfprintf(stderr, fmt, ap);
49 va_end(ap);
50 }
51
52 /***********************************************************/
53 /* CPUX86 core interface */
54
55 void cpu_x86_outb(int addr, int val)
56 {
57 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
58 }
59
60 void cpu_x86_outw(int addr, int val)
61 {
62 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
63 }
64
65 void cpu_x86_outl(int addr, int val)
66 {
67 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
68 }
69
70 int cpu_x86_inb(int addr)
71 {
72 fprintf(stderr, "inb: port=0x%04x\n", addr);
73 return 0;
74 }
75
76 int cpu_x86_inw(int addr)
77 {
78 fprintf(stderr, "inw: port=0x%04x\n", addr);
79 return 0;
80 }
81
82 int cpu_x86_inl(int addr)
83 {
84 fprintf(stderr, "inl: port=0x%04x\n", addr);
85 return 0;
86 }
87
88 void write_dt(void *ptr, unsigned long addr, unsigned long limit,
89 int seg32_bit)
90 {
91 unsigned int e1, e2, limit_in_pages;
92 limit_in_pages = 0;
93 if (limit > 0xffff) {
94 limit = limit >> 12;
95 limit_in_pages = 1;
96 }
97 e1 = (addr << 16) | (limit & 0xffff);
98 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
99 e2 |= limit_in_pages << 23; /* byte granularity */
100 e2 |= seg32_bit << 22; /* 32 bit segment */
101 stl((uint8_t *)ptr, e1);
102 stl((uint8_t *)ptr + 4, e2);
103 }
104
105 uint64_t gdt_table[6];
106
107 void cpu_loop(struct CPUX86State *env)
108 {
109 int err;
110 uint8_t *pc;
111 target_siginfo_t info;
112
113 for(;;) {
114 err = cpu_x86_exec(env);
115 pc = env->seg_cache[R_CS].base + env->eip;
116 switch(err) {
117 case EXCP0D_GPF:
118 if (pc[0] == 0xcd && pc[1] == 0x80) {
119 /* syscall */
120 env->eip += 2;
121 env->regs[R_EAX] = do_syscall(env,
122 env->regs[R_EAX],
123 env->regs[R_EBX],
124 env->regs[R_ECX],
125 env->regs[R_EDX],
126 env->regs[R_ESI],
127 env->regs[R_EDI],
128 env->regs[R_EBP]);
129 } else {
130 /* XXX: more precise info */
131 info.si_signo = SIGSEGV;
132 info.si_errno = 0;
133 info.si_code = 0;
134 info._sifields._sigfault._addr = 0;
135 queue_signal(info.si_signo, &info);
136 }
137 break;
138 case EXCP00_DIVZ:
139 /* division by zero */
140 info.si_signo = SIGFPE;
141 info.si_errno = 0;
142 info.si_code = TARGET_FPE_INTDIV;
143 info._sifields._sigfault._addr = env->eip;
144 queue_signal(info.si_signo, &info);
145 break;
146 case EXCP04_INTO:
147 case EXCP05_BOUND:
148 info.si_signo = SIGSEGV;
149 info.si_errno = 0;
150 info.si_code = 0;
151 info._sifields._sigfault._addr = 0;
152 queue_signal(info.si_signo, &info);
153 break;
154 case EXCP06_ILLOP:
155 info.si_signo = SIGILL;
156 info.si_errno = 0;
157 info.si_code = TARGET_ILL_ILLOPN;
158 info._sifields._sigfault._addr = env->eip;
159 queue_signal(info.si_signo, &info);
160 break;
161 case EXCP_INTERRUPT:
162 /* just indicate that signals should be handled asap */
163 break;
164 default:
165 fprintf(stderr, "0x%08lx: Unknown exception CPU %d, aborting\n",
166 (long)pc, err);
167 abort();
168 }
169 process_pending_signals(env);
170 }
171 }
172
173 void usage(void)
174 {
175 printf("qemu version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
176 "usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]\n"
177 "Linux x86 emulator\n"
178 "\n"
179 "-h print this help\n"
180 "-d activate log (logfile=%s)\n"
181 "-L path set the x86 elf interpreter prefix (default=%s)\n"
182 "-s size set the x86 stack size in bytes (default=%ld)\n",
183 DEBUG_LOGFILE,
184 interp_prefix,
185 x86_stack_size);
186 exit(1);
187 }
188
189 /* XXX: currently only used for async signals (see signal.c) */
190 CPUX86State *global_env;
191
192 int main(int argc, char **argv)
193 {
194 const char *filename;
195 struct target_pt_regs regs1, *regs = &regs1;
196 struct image_info info1, *info = &info1;
197 CPUX86State *env;
198 int optind;
199 const char *r;
200
201 if (argc <= 1)
202 usage();
203 loglevel = 0;
204 optind = 1;
205 for(;;) {
206 if (optind >= argc)
207 break;
208 r = argv[optind];
209 if (r[0] != '-')
210 break;
211 optind++;
212 r++;
213 if (!strcmp(r, "-")) {
214 break;
215 } else if (!strcmp(r, "d")) {
216 loglevel = 1;
217 } else if (!strcmp(r, "s")) {
218 r = argv[optind++];
219 x86_stack_size = strtol(r, (char **)&r, 0);
220 if (x86_stack_size <= 0)
221 usage();
222 if (*r == 'M')
223 x86_stack_size *= 1024 * 1024;
224 else if (*r == 'k' || *r == 'K')
225 x86_stack_size *= 1024;
226 } else if (!strcmp(r, "L")) {
227 interp_prefix = argv[optind++];
228 } else {
229 usage();
230 }
231 }
232 if (optind >= argc)
233 usage();
234 filename = argv[optind];
235
236 /* init debug */
237 if (loglevel) {
238 logfile = fopen(DEBUG_LOGFILE, "w");
239 if (!logfile) {
240 perror(DEBUG_LOGFILE);
241 exit(1);
242 }
243 setvbuf(logfile, NULL, _IOLBF, 0);
244 }
245
246 /* Zero out regs */
247 memset(regs, 0, sizeof(struct target_pt_regs));
248
249 /* Zero out image_info */
250 memset(info, 0, sizeof(struct image_info));
251
252 if(elf_exec(interp_prefix, filename, argv+optind, environ, regs, info) != 0) {
253 printf("Error loading %s\n", filename);
254 exit(1);
255 }
256
257 if (loglevel) {
258 fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
259 fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
260 fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
261 fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
262 fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
263 fprintf(logfile, "brk 0x%08lx\n" , info->brk);
264 fprintf(logfile, "esp 0x%08lx\n" , regs->esp);
265 fprintf(logfile, "eip 0x%08lx\n" , regs->eip);
266 }
267
268 target_set_brk((char *)info->brk);
269 syscall_init();
270 signal_init();
271
272 env = cpu_x86_init();
273 global_env = env;
274
275 /* linux register setup */
276 env->regs[R_EAX] = regs->eax;
277 env->regs[R_EBX] = regs->ebx;
278 env->regs[R_ECX] = regs->ecx;
279 env->regs[R_EDX] = regs->edx;
280 env->regs[R_ESI] = regs->esi;
281 env->regs[R_EDI] = regs->edi;
282 env->regs[R_EBP] = regs->ebp;
283 env->regs[R_ESP] = regs->esp;
284 env->eip = regs->eip;
285
286 /* linux segment setup */
287 env->gdt.base = (void *)gdt_table;
288 env->gdt.limit = sizeof(gdt_table) - 1;
289 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1);
290 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1);
291 cpu_x86_load_seg(env, R_CS, __USER_CS);
292 cpu_x86_load_seg(env, R_DS, __USER_DS);
293 cpu_x86_load_seg(env, R_ES, __USER_DS);
294 cpu_x86_load_seg(env, R_SS, __USER_DS);
295 cpu_x86_load_seg(env, R_FS, __USER_DS);
296 cpu_x86_load_seg(env, R_GS, __USER_DS);
297
298 cpu_loop(env);
299 /* never exits */
300 return 0;
301 }