blob: 21ddf7aeb938ab4e5affc27609718c6efabb40c8 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard93ac68b2003-09-30 20:57:29 +00002 * qemu user main
bellard31e31b82003-02-18 22:55:36 +00003 *
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>
bellard04369ff2003-03-20 22:33:23 +000023#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000024#include <errno.h>
bellard0ecfa992003-03-03 14:32:43 +000025#include <unistd.h>
bellard31e31b82003-02-18 22:55:36 +000026
bellard3ef693a2003-03-23 20:17:16 +000027#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +000028
bellard3ef693a2003-03-23 20:17:16 +000029#define DEBUG_LOGFILE "/tmp/qemu.log"
bellard586314f2003-03-03 15:02:29 +000030
bellard74cd30b2003-04-11 00:13:41 +000031static const char *interp_prefix = CONFIG_QEMU_PREFIX;
bellard586314f2003-03-03 15:02:29 +000032
bellard3a4739d2003-10-28 00:48:22 +000033#if defined(__i386__) && !defined(CONFIG_STATIC)
bellardf801f972003-04-07 21:31:06 +000034/* Force usage of an ELF interpreter even if it is an ELF shared
35 object ! */
36const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
bellard43047632003-05-29 20:05:35 +000037#endif
bellard74cd30b2003-04-11 00:13:41 +000038
bellard93ac68b2003-09-30 20:57:29 +000039/* for recent libc, we add these dummy symbols which are not declared
bellard74cd30b2003-04-11 00:13:41 +000040 when generating a linked object (bug in ld ?) */
41#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
bellardc6981052004-02-16 21:49:03 +000042long __preinit_array_start[0];
43long __preinit_array_end[0];
bellard74cd30b2003-04-11 00:13:41 +000044long __init_array_start[0];
45long __init_array_end[0];
46long __fini_array_start[0];
47long __fini_array_end[0];
48#endif
49
bellard9de5e442003-03-23 16:49:39 +000050/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
51 we allocate a bigger stack. Need a better solution, for example
52 by remapping the process stack directly at the right place */
53unsigned long x86_stack_size = 512 * 1024;
bellard31e31b82003-02-18 22:55:36 +000054
55void gemu_log(const char *fmt, ...)
56{
57 va_list ap;
58
59 va_start(ap, fmt);
60 vfprintf(stderr, fmt, ap);
61 va_end(ap);
62}
63
bellard61190b12004-01-04 23:54:31 +000064void cpu_outb(CPUState *env, int addr, int val)
bellard367e86e2003-03-01 17:13:26 +000065{
66 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
67}
68
bellard61190b12004-01-04 23:54:31 +000069void cpu_outw(CPUState *env, int addr, int val)
bellard367e86e2003-03-01 17:13:26 +000070{
71 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
72}
73
bellard61190b12004-01-04 23:54:31 +000074void cpu_outl(CPUState *env, int addr, int val)
bellard367e86e2003-03-01 17:13:26 +000075{
76 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
77}
78
bellard61190b12004-01-04 23:54:31 +000079int cpu_inb(CPUState *env, int addr)
bellard367e86e2003-03-01 17:13:26 +000080{
81 fprintf(stderr, "inb: port=0x%04x\n", addr);
82 return 0;
83}
84
bellard61190b12004-01-04 23:54:31 +000085int cpu_inw(CPUState *env, int addr)
bellard367e86e2003-03-01 17:13:26 +000086{
87 fprintf(stderr, "inw: port=0x%04x\n", addr);
88 return 0;
89}
90
bellard61190b12004-01-04 23:54:31 +000091int cpu_inl(CPUState *env, int addr)
bellard367e86e2003-03-01 17:13:26 +000092{
93 fprintf(stderr, "inl: port=0x%04x\n", addr);
94 return 0;
95}
96
bellard61190b12004-01-04 23:54:31 +000097#ifdef TARGET_I386
98/***********************************************************/
99/* CPUX86 core interface */
100
101int cpu_x86_get_pic_interrupt(CPUState *env)
bellard92ccca62003-06-24 13:30:31 +0000102{
103 return -1;
104}
105
bellardf4beb512003-05-27 23:28:08 +0000106static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
107 int flags)
bellard6dbad632003-03-16 18:05:05 +0000108{
bellardf4beb512003-05-27 23:28:08 +0000109 unsigned int e1, e2;
bellard6dbad632003-03-16 18:05:05 +0000110 e1 = (addr << 16) | (limit & 0xffff);
111 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
bellardf4beb512003-05-27 23:28:08 +0000112 e2 |= flags;
113 stl((uint8_t *)ptr, e1);
114 stl((uint8_t *)ptr + 4, e2);
115}
116
117static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
118 unsigned long addr, unsigned int sel)
119{
120 unsigned int e1, e2;
121 e1 = (addr & 0xffff) | (sel << 16);
122 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
bellard6dbad632003-03-16 18:05:05 +0000123 stl((uint8_t *)ptr, e1);
124 stl((uint8_t *)ptr + 4, e2);
125}
126
127uint64_t gdt_table[6];
bellardf4beb512003-05-27 23:28:08 +0000128uint64_t idt_table[256];
129
130/* only dpl matters as we do only user space emulation */
131static void set_idt(int n, unsigned int dpl)
132{
133 set_gate(idt_table + n, 0, dpl, 0, 0);
134}
bellard31e31b82003-02-18 22:55:36 +0000135
bellard89e957e2003-05-10 12:33:15 +0000136void cpu_loop(CPUX86State *env)
bellard1b6b0292003-03-22 17:31:38 +0000137{
bellardbc8a22c2003-03-30 21:02:40 +0000138 int trapnr;
bellard9de5e442003-03-23 16:49:39 +0000139 uint8_t *pc;
140 target_siginfo_t info;
bellard851e67a2003-03-29 16:53:14 +0000141
bellard1b6b0292003-03-22 17:31:38 +0000142 for(;;) {
bellardbc8a22c2003-03-30 21:02:40 +0000143 trapnr = cpu_x86_exec(env);
bellardbc8a22c2003-03-30 21:02:40 +0000144 switch(trapnr) {
bellardf4beb512003-05-27 23:28:08 +0000145 case 0x80:
146 /* linux syscall */
147 env->regs[R_EAX] = do_syscall(env,
148 env->regs[R_EAX],
149 env->regs[R_EBX],
150 env->regs[R_ECX],
151 env->regs[R_EDX],
152 env->regs[R_ESI],
153 env->regs[R_EDI],
154 env->regs[R_EBP]);
155 break;
156 case EXCP0B_NOSEG:
157 case EXCP0C_STACK:
158 info.si_signo = SIGBUS;
159 info.si_errno = 0;
160 info.si_code = TARGET_SI_KERNEL;
161 info._sifields._sigfault._addr = 0;
162 queue_signal(info.si_signo, &info);
163 break;
bellard1b6b0292003-03-22 17:31:38 +0000164 case EXCP0D_GPF:
bellard851e67a2003-03-29 16:53:14 +0000165 if (env->eflags & VM_MASK) {
bellard89e957e2003-05-10 12:33:15 +0000166 handle_vm86_fault(env);
bellard1b6b0292003-03-22 17:31:38 +0000167 } else {
bellardf4beb512003-05-27 23:28:08 +0000168 info.si_signo = SIGSEGV;
169 info.si_errno = 0;
170 info.si_code = TARGET_SI_KERNEL;
171 info._sifields._sigfault._addr = 0;
172 queue_signal(info.si_signo, &info);
bellard1b6b0292003-03-22 17:31:38 +0000173 }
174 break;
bellardb689bc52003-05-08 15:33:33 +0000175 case EXCP0E_PAGE:
176 info.si_signo = SIGSEGV;
177 info.si_errno = 0;
178 if (!(env->error_code & 1))
179 info.si_code = TARGET_SEGV_MAPERR;
180 else
181 info.si_code = TARGET_SEGV_ACCERR;
bellard970a87a2003-06-21 13:13:25 +0000182 info._sifields._sigfault._addr = env->cr[2];
bellardb689bc52003-05-08 15:33:33 +0000183 queue_signal(info.si_signo, &info);
184 break;
bellard9de5e442003-03-23 16:49:39 +0000185 case EXCP00_DIVZ:
bellardbc8a22c2003-03-30 21:02:40 +0000186 if (env->eflags & VM_MASK) {
bellard447db212003-05-10 15:10:36 +0000187 handle_vm86_trap(env, trapnr);
bellardbc8a22c2003-03-30 21:02:40 +0000188 } else {
189 /* division by zero */
190 info.si_signo = SIGFPE;
191 info.si_errno = 0;
192 info.si_code = TARGET_FPE_INTDIV;
193 info._sifields._sigfault._addr = env->eip;
194 queue_signal(info.si_signo, &info);
195 }
bellard9de5e442003-03-23 16:49:39 +0000196 break;
bellard447db212003-05-10 15:10:36 +0000197 case EXCP01_SSTP:
198 case EXCP03_INT3:
199 if (env->eflags & VM_MASK) {
200 handle_vm86_trap(env, trapnr);
201 } else {
202 info.si_signo = SIGTRAP;
203 info.si_errno = 0;
204 if (trapnr == EXCP01_SSTP) {
205 info.si_code = TARGET_TRAP_BRKPT;
206 info._sifields._sigfault._addr = env->eip;
207 } else {
208 info.si_code = TARGET_SI_KERNEL;
209 info._sifields._sigfault._addr = 0;
210 }
211 queue_signal(info.si_signo, &info);
212 }
213 break;
bellard9de5e442003-03-23 16:49:39 +0000214 case EXCP04_INTO:
215 case EXCP05_BOUND:
bellardbc8a22c2003-03-30 21:02:40 +0000216 if (env->eflags & VM_MASK) {
bellard447db212003-05-10 15:10:36 +0000217 handle_vm86_trap(env, trapnr);
bellardbc8a22c2003-03-30 21:02:40 +0000218 } else {
219 info.si_signo = SIGSEGV;
220 info.si_errno = 0;
bellardb689bc52003-05-08 15:33:33 +0000221 info.si_code = TARGET_SI_KERNEL;
bellardbc8a22c2003-03-30 21:02:40 +0000222 info._sifields._sigfault._addr = 0;
223 queue_signal(info.si_signo, &info);
224 }
bellard9de5e442003-03-23 16:49:39 +0000225 break;
226 case EXCP06_ILLOP:
227 info.si_signo = SIGILL;
228 info.si_errno = 0;
229 info.si_code = TARGET_ILL_ILLOPN;
230 info._sifields._sigfault._addr = env->eip;
231 queue_signal(info.si_signo, &info);
232 break;
233 case EXCP_INTERRUPT:
234 /* just indicate that signals should be handled asap */
235 break;
bellard1b6b0292003-03-22 17:31:38 +0000236 default:
bellard970a87a2003-06-21 13:13:25 +0000237 pc = env->segs[R_CS].base + env->eip;
bellardbc8a22c2003-03-30 21:02:40 +0000238 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
239 (long)pc, trapnr);
bellard1b6b0292003-03-22 17:31:38 +0000240 abort();
241 }
bellard66fb9762003-03-23 01:06:05 +0000242 process_pending_signals(env);
bellard1b6b0292003-03-22 17:31:38 +0000243 }
244}
bellardb346ff42003-06-15 20:05:50 +0000245#endif
246
247#ifdef TARGET_ARM
248
bellardb346ff42003-06-15 20:05:50 +0000249void cpu_loop(CPUARMState *env)
250{
251 int trapnr;
252 unsigned int n, insn;
253 target_siginfo_t info;
254
255 for(;;) {
256 trapnr = cpu_arm_exec(env);
257 switch(trapnr) {
258 case EXCP_UDEF:
bellardc6981052004-02-16 21:49:03 +0000259 {
260 TaskState *ts = env->opaque;
261 uint32_t opcode;
262
263 /* we handle the FPU emulation here, as Linux */
264 /* we get the opcode */
265 opcode = ldl_raw((uint8_t *)env->regs[15]);
266
267 if (EmulateAll(opcode, &ts->fpa, env->regs) == 0) {
268 info.si_signo = SIGILL;
269 info.si_errno = 0;
270 info.si_code = TARGET_ILL_ILLOPN;
271 info._sifields._sigfault._addr = env->regs[15];
272 queue_signal(info.si_signo, &info);
273 } else {
274 /* increment PC */
275 env->regs[15] += 4;
276 }
277 }
bellardb346ff42003-06-15 20:05:50 +0000278 break;
279 case EXCP_SWI:
280 {
281 /* system call */
282 insn = ldl((void *)(env->regs[15] - 4));
283 n = insn & 0xffffff;
284 if (n >= ARM_SYSCALL_BASE) {
285 /* linux syscall */
286 n -= ARM_SYSCALL_BASE;
287 env->regs[0] = do_syscall(env,
288 n,
289 env->regs[0],
290 env->regs[1],
291 env->regs[2],
292 env->regs[3],
293 env->regs[4],
294 0);
295 } else {
296 goto error;
297 }
298 }
299 break;
bellard43fff232003-07-09 19:31:39 +0000300 case EXCP_INTERRUPT:
301 /* just indicate that signals should be handled asap */
302 break;
bellardb346ff42003-06-15 20:05:50 +0000303 default:
304 error:
305 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
306 trapnr);
307 cpu_arm_dump_state(env, stderr, 0);
308 abort();
309 }
310 process_pending_signals(env);
311 }
312}
313
314#endif
bellard1b6b0292003-03-22 17:31:38 +0000315
bellard93ac68b2003-09-30 20:57:29 +0000316#ifdef TARGET_SPARC
317
bellard060366c2004-01-04 15:50:01 +0000318//#define DEBUG_WIN
319
320/* WARNING: dealing with register windows _is_ complicated */
321static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
322{
323 index = (index + cwp * 16) & (16 * NWINDOWS - 1);
324 /* wrap handling : if cwp is on the last window, then we use the
325 registers 'after' the end */
326 if (index < 8 && env->cwp == (NWINDOWS - 1))
327 index += (16 * NWINDOWS);
328 return index;
329}
330
331static inline void save_window_offset(CPUSPARCState *env, int offset)
332{
333 unsigned int new_wim, i, cwp1;
334 uint32_t *sp_ptr;
335
336 new_wim = ((env->wim >> 1) | (env->wim << (NWINDOWS - 1))) &
337 ((1LL << NWINDOWS) - 1);
338 /* save the window */
339 cwp1 = (env->cwp + offset) & (NWINDOWS - 1);
340 sp_ptr = (uint32_t *)(env->regbase[get_reg_index(env, cwp1, 6)]);
341#if defined(DEBUG_WIN)
342 printf("win_overflow: sp_ptr=0x%x save_cwp=%d\n",
343 (int)sp_ptr, cwp1);
344#endif
345 for(i = 0; i < 16; i++)
346 stl_raw(sp_ptr + i, env->regbase[get_reg_index(env, cwp1, 8 + i)]);
347 env->wim = new_wim;
348}
349
350static void save_window(CPUSPARCState *env)
351{
352 save_window_offset(env, 2);
353}
354
355static void restore_window(CPUSPARCState *env)
356{
357 unsigned int new_wim, i, cwp1;
358 uint32_t *sp_ptr;
359
360 new_wim = ((env->wim << 1) | (env->wim >> (NWINDOWS - 1))) &
361 ((1LL << NWINDOWS) - 1);
362
363 /* restore the invalid window */
364 cwp1 = (env->cwp + 1) & (NWINDOWS - 1);
365 sp_ptr = (uint32_t *)(env->regbase[get_reg_index(env, cwp1, 6)]);
366#if defined(DEBUG_WIN)
367 printf("win_underflow: sp_ptr=0x%x load_cwp=%d\n",
368 (int)sp_ptr, cwp1);
369#endif
370 for(i = 0; i < 16; i++)
371 env->regbase[get_reg_index(env, cwp1, 8 + i)] = ldl_raw(sp_ptr + i);
372 env->wim = new_wim;
373}
374
375static void flush_windows(CPUSPARCState *env)
376{
377 int offset, cwp1;
378#if defined(DEBUG_WIN)
379 printf("flush_windows:\n");
380#endif
381 offset = 2;
382 for(;;) {
383 /* if restore would invoke restore_window(), then we can stop */
384 cwp1 = (env->cwp + 1) & (NWINDOWS - 1);
385 if (env->wim & (1 << cwp1))
386 break;
387#if defined(DEBUG_WIN)
388 printf("offset=%d: ", offset);
389#endif
390 save_window_offset(env, offset);
391 offset++;
392 }
393}
394
bellard93ac68b2003-09-30 20:57:29 +0000395void cpu_loop (CPUSPARCState *env)
396{
bellard060366c2004-01-04 15:50:01 +0000397 int trapnr, ret;
398
399 while (1) {
400 trapnr = cpu_sparc_exec (env);
401
402 switch (trapnr) {
403 case 0x88:
404 case 0x90:
405 ret = do_syscall (env, env->gregs[1],
406 env->regwptr[0], env->regwptr[1],
407 env->regwptr[2], env->regwptr[3],
408 env->regwptr[4], env->regwptr[5]);
409 if ((unsigned int)ret >= (unsigned int)(-515)) {
410 env->psr |= PSR_CARRY;
411 ret = -ret;
412 } else {
413 env->psr &= ~PSR_CARRY;
414 }
415 env->regwptr[0] = ret;
416 /* next instruction */
417 env->pc = env->npc;
418 env->npc = env->npc + 4;
419 break;
420 case 0x83: /* flush windows */
421 // flush_windows(env);
422 /* next instruction */
423 env->pc = env->npc;
424 env->npc = env->npc + 4;
425 break;
426 case TT_WIN_OVF: /* window overflow */
427 save_window(env);
428 break;
429 case TT_WIN_UNF: /* window underflow */
430 restore_window(env);
431 break;
432 default:
433 printf ("Unhandled trap: 0x%x\n", trapnr);
434 cpu_sparc_dump_state(env, stderr, 0);
435 exit (1);
436 }
437 process_pending_signals (env);
438 }
bellard93ac68b2003-09-30 20:57:29 +0000439}
440
441#endif
442
bellard67867302003-11-23 17:05:30 +0000443#ifdef TARGET_PPC
444
445void cpu_loop(CPUPPCState *env)
446{
bellard67867302003-11-23 17:05:30 +0000447 target_siginfo_t info;
bellard61190b12004-01-04 23:54:31 +0000448 int trapnr;
449 uint32_t ret;
bellard67867302003-11-23 17:05:30 +0000450
451 for(;;) {
452 trapnr = cpu_ppc_exec(env);
bellard61190b12004-01-04 23:54:31 +0000453 if (trapnr != EXCP_SYSCALL_USER && trapnr != EXCP_BRANCH &&
454 trapnr != EXCP_TRACE) {
455 if (loglevel > 0) {
456 cpu_ppc_dump_state(env, logfile, 0);
457 }
458 }
bellard67867302003-11-23 17:05:30 +0000459 switch(trapnr) {
460 case EXCP_NONE:
bellard67867302003-11-23 17:05:30 +0000461 break;
bellard61190b12004-01-04 23:54:31 +0000462 case EXCP_SYSCALL_USER:
bellard67867302003-11-23 17:05:30 +0000463 /* system call */
464 /* WARNING:
465 * PPC ABI uses overflow flag in cr0 to signal an error
466 * in syscalls.
467 */
bellard61190b12004-01-04 23:54:31 +0000468#if 0
469 printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0],
470 env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]);
471#endif
bellard67867302003-11-23 17:05:30 +0000472 env->crf[0] &= ~0x1;
473 ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
474 env->gpr[5], env->gpr[6], env->gpr[7],
475 env->gpr[8]);
476 if (ret > (uint32_t)(-515)) {
477 env->crf[0] |= 0x1;
478 ret = -ret;
479 }
480 env->gpr[3] = ret;
bellard61190b12004-01-04 23:54:31 +0000481#if 0
482 printf("syscall returned 0x%08x (%d)\n", ret, ret);
483#endif
bellard67867302003-11-23 17:05:30 +0000484 break;
bellard61190b12004-01-04 23:54:31 +0000485 case EXCP_RESET:
486 /* Should not happen ! */
487 fprintf(stderr, "RESET asked... Stop emulation\n");
488 if (loglevel)
489 fprintf(logfile, "RESET asked... Stop emulation\n");
490 abort();
491 case EXCP_MACHINE_CHECK:
492 fprintf(stderr, "Machine check exeption... Stop emulation\n");
493 if (loglevel)
494 fprintf(logfile, "RESET asked... Stop emulation\n");
495 info.si_signo = TARGET_SIGBUS;
496 info.si_errno = 0;
497 info.si_code = TARGET_BUS_OBJERR;
498 info._sifields._sigfault._addr = env->nip - 4;
499 queue_signal(info.si_signo, &info);
500 case EXCP_DSI:
501 fprintf(stderr, "Invalid data memory access: 0x%08x\n", env->spr[DAR]);
502 if (loglevel) {
503 fprintf(logfile, "Invalid data memory access: 0x%08x\n",
504 env->spr[DAR]);
505 }
506 switch (env->error_code & 0xF) {
507 case EXCP_DSI_TRANSLATE:
508 info.si_signo = TARGET_SIGSEGV;
509 info.si_errno = 0;
510 info.si_code = TARGET_SEGV_MAPERR;
511 break;
512 case EXCP_DSI_NOTSUP:
513 case EXCP_DSI_EXTERNAL:
514 info.si_signo = TARGET_SIGILL;
515 info.si_errno = 0;
516 info.si_code = TARGET_ILL_ILLADR;
517 break;
518 case EXCP_DSI_PROT:
519 info.si_signo = TARGET_SIGSEGV;
520 info.si_errno = 0;
521 info.si_code = TARGET_SEGV_ACCERR;
522 break;
523 case EXCP_DSI_DABR:
524 info.si_signo = TARGET_SIGTRAP;
525 info.si_errno = 0;
526 info.si_code = TARGET_TRAP_BRKPT;
527 break;
528 default:
529 /* Let's send a regular segfault... */
530 fprintf(stderr, "Invalid segfault errno (%02x)\n",
531 env->error_code);
532 if (loglevel) {
533 fprintf(logfile, "Invalid segfault errno (%02x)\n",
534 env->error_code);
535 }
536 info.si_signo = TARGET_SIGSEGV;
537 info.si_errno = 0;
538 info.si_code = TARGET_SEGV_MAPERR;
539 break;
540 }
541 info._sifields._sigfault._addr = env->nip;
542 queue_signal(info.si_signo, &info);
543 break;
544 case EXCP_ISI:
545 fprintf(stderr, "Invalid instruction fetch\n");
546 if (loglevel)
547 fprintf(logfile, "Invalid instruction fetch\n");
548 switch (env->error_code) {
549 case EXCP_ISI_TRANSLATE:
550 info.si_signo = TARGET_SIGSEGV;
551 info.si_errno = 0;
552 info.si_code = TARGET_SEGV_MAPERR;
553 break;
554 case EXCP_ISI_GUARD:
555 info.si_signo = TARGET_SIGILL;
556 info.si_errno = 0;
557 info.si_code = TARGET_ILL_ILLADR;
558 break;
559 case EXCP_ISI_NOEXEC:
560 case EXCP_ISI_PROT:
561 info.si_signo = TARGET_SIGSEGV;
562 info.si_errno = 0;
563 info.si_code = TARGET_SEGV_ACCERR;
564 break;
565 default:
566 /* Let's send a regular segfault... */
567 fprintf(stderr, "Invalid segfault errno (%02x)\n",
568 env->error_code);
569 if (loglevel) {
570 fprintf(logfile, "Invalid segfault errno (%02x)\n",
571 env->error_code);
572 }
573 info.si_signo = TARGET_SIGSEGV;
574 info.si_errno = 0;
575 info.si_code = TARGET_SEGV_MAPERR;
576 break;
577 }
578 info._sifields._sigfault._addr = env->nip - 4;
579 queue_signal(info.si_signo, &info);
580 break;
581 case EXCP_EXTERNAL:
582 /* Should not happen ! */
583 fprintf(stderr, "External interruption... Stop emulation\n");
584 if (loglevel)
585 fprintf(logfile, "External interruption... Stop emulation\n");
586 abort();
587 case EXCP_ALIGN:
588 fprintf(stderr, "Invalid unaligned memory access\n");
589 if (loglevel)
590 fprintf(logfile, "Invalid unaligned memory access\n");
591 info.si_signo = TARGET_SIGBUS;
592 info.si_errno = 0;
593 info.si_code = TARGET_BUS_ADRALN;
594 info._sifields._sigfault._addr = env->nip - 4;
595 queue_signal(info.si_signo, &info);
596 break;
597 case EXCP_PROGRAM:
598 switch (env->error_code & ~0xF) {
599 case EXCP_FP:
600 fprintf(stderr, "Program exception\n");
601 if (loglevel)
602 fprintf(logfile, "Program exception\n");
603 /* Set FX */
604 env->fpscr[7] |= 0x8;
605 /* Finally, update FEX */
606 if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
607 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
608 env->fpscr[7] |= 0x4;
609 info.si_signo = TARGET_SIGFPE;
610 info.si_errno = 0;
611 switch (env->error_code & 0xF) {
612 case EXCP_FP_OX:
613 info.si_code = TARGET_FPE_FLTOVF;
614 break;
615 case EXCP_FP_UX:
616 info.si_code = TARGET_FPE_FLTUND;
617 break;
618 case EXCP_FP_ZX:
619 case EXCP_FP_VXZDZ:
620 info.si_code = TARGET_FPE_FLTDIV;
621 break;
622 case EXCP_FP_XX:
623 info.si_code = TARGET_FPE_FLTRES;
624 break;
625 case EXCP_FP_VXSOFT:
626 info.si_code = TARGET_FPE_FLTINV;
627 break;
628 case EXCP_FP_VXNAN:
629 case EXCP_FP_VXISI:
630 case EXCP_FP_VXIDI:
631 case EXCP_FP_VXIMZ:
632 case EXCP_FP_VXVC:
633 case EXCP_FP_VXSQRT:
634 case EXCP_FP_VXCVI:
635 info.si_code = TARGET_FPE_FLTSUB;
636 break;
637 default:
638 fprintf(stderr, "Unknown floating point exception "
639 "(%02x)\n", env->error_code);
640 if (loglevel) {
641 fprintf(logfile, "Unknown floating point exception "
642 "(%02x)\n", env->error_code & 0xF);
643 }
644 }
645 break;
646 case EXCP_INVAL:
647 fprintf(stderr, "Invalid instruction\n");
648 if (loglevel)
649 fprintf(logfile, "Invalid instruction\n");
650 info.si_signo = TARGET_SIGILL;
651 info.si_errno = 0;
652 switch (env->error_code & 0xF) {
653 case EXCP_INVAL_INVAL:
654 info.si_code = TARGET_ILL_ILLOPC;
655 break;
656 case EXCP_INVAL_LSWX:
657 info.si_code = TARGET_ILL_ILLOPN;
658 break;
659 case EXCP_INVAL_SPR:
660 info.si_code = TARGET_ILL_PRVREG;
661 break;
662 case EXCP_INVAL_FP:
663 info.si_code = TARGET_ILL_COPROC;
664 break;
665 default:
666 fprintf(stderr, "Unknown invalid operation (%02x)\n",
667 env->error_code & 0xF);
668 if (loglevel) {
669 fprintf(logfile, "Unknown invalid operation (%02x)\n",
670 env->error_code & 0xF);
671 }
672 info.si_code = TARGET_ILL_ILLADR;
673 break;
674 }
675 break;
676 case EXCP_PRIV:
677 fprintf(stderr, "Privilege violation\n");
678 if (loglevel)
679 fprintf(logfile, "Privilege violation\n");
680 info.si_signo = TARGET_SIGILL;
681 info.si_errno = 0;
682 switch (env->error_code & 0xF) {
683 case EXCP_PRIV_OPC:
684 info.si_code = TARGET_ILL_PRVOPC;
685 break;
686 case EXCP_PRIV_REG:
687 info.si_code = TARGET_ILL_PRVREG;
688 break;
689 default:
690 fprintf(stderr, "Unknown privilege violation (%02x)\n",
691 env->error_code & 0xF);
692 info.si_code = TARGET_ILL_PRVOPC;
693 break;
694 }
695 break;
696 case EXCP_TRAP:
697 fprintf(stderr, "Tried to call a TRAP\n");
698 if (loglevel)
699 fprintf(logfile, "Tried to call a TRAP\n");
700 abort();
701 default:
702 /* Should not happen ! */
703 fprintf(stderr, "Unknown program exception (%02x)\n",
704 env->error_code);
705 if (loglevel) {
706 fprintf(logfile, "Unknwon program exception (%02x)\n",
707 env->error_code);
708 }
709 abort();
710 }
711 info._sifields._sigfault._addr = env->nip - 4;
712 queue_signal(info.si_signo, &info);
713 break;
714 case EXCP_NO_FP:
715 fprintf(stderr, "No floating point allowed\n");
716 if (loglevel)
717 fprintf(logfile, "No floating point allowed\n");
718 info.si_signo = TARGET_SIGILL;
719 info.si_errno = 0;
720 info.si_code = TARGET_ILL_COPROC;
721 info._sifields._sigfault._addr = env->nip - 4;
722 queue_signal(info.si_signo, &info);
723 break;
724 case EXCP_DECR:
725 /* Should not happen ! */
726 fprintf(stderr, "Decrementer exception\n");
727 if (loglevel)
728 fprintf(logfile, "Decrementer exception\n");
729 abort();
730 case EXCP_RESA: /* Implementation specific */
731 /* Should not happen ! */
732 fprintf(stderr, "RESA exception should never happen !\n");
733 if (loglevel)
734 fprintf(logfile, "RESA exception should never happen !\n");
735 abort();
736 case EXCP_RESB: /* Implementation specific */
737 /* Should not happen ! */
738 fprintf(stderr, "RESB exception should never happen !\n");
739 if (loglevel)
740 fprintf(logfile, "RESB exception should never happen !\n");
741 abort();
742 case EXCP_TRACE:
743 /* Do nothing: we use this to trace execution */
744 break;
745 case EXCP_FP_ASSIST:
746 /* Should not happen ! */
747 fprintf(stderr, "Floating point assist exception\n");
748 if (loglevel)
749 fprintf(logfile, "Floating point assist exception\n");
750 abort();
751 case EXCP_MTMSR:
752 /* We reloaded the msr, just go on */
753 if (msr_pr) {
754 fprintf(stderr, "Tried to go into supervisor mode !\n");
755 if (loglevel)
756 fprintf(logfile, "Tried to go into supervisor mode !\n");
757 abort();
bellard67867302003-11-23 17:05:30 +0000758 }
bellard61190b12004-01-04 23:54:31 +0000759 break;
760 case EXCP_BRANCH:
761 /* We stopped because of a jump... */
762 break;
763 case EXCP_RFI:
764 /* Should not occur: we always are in user mode */
765 fprintf(stderr, "Return from interrupt ?\n");
766 if (loglevel)
767 fprintf(logfile, "Return from interrupt ?\n");
768 abort();
769 case EXCP_INTERRUPT:
770 /* Don't know why this should ever happen... */
771 break;
bellard67867302003-11-23 17:05:30 +0000772 default:
bellard67867302003-11-23 17:05:30 +0000773 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
774 trapnr);
bellard61190b12004-01-04 23:54:31 +0000775 if (loglevel) {
776 fprintf(logfile, "qemu: unhandled CPU exception 0x%02x - "
777 "0x%02x - aborting\n", trapnr, env->error_code);
778 }
bellard67867302003-11-23 17:05:30 +0000779 abort();
780 }
bellard61190b12004-01-04 23:54:31 +0000781 if (trapnr < EXCP_PPC_MAX)
782 env->exceptions &= ~(1 << trapnr);
bellard67867302003-11-23 17:05:30 +0000783 process_pending_signals(env);
bellard61190b12004-01-04 23:54:31 +0000784 if (env->exceptions != 0) {
785 check_exception_state(env);
786 }
bellard67867302003-11-23 17:05:30 +0000787 }
788}
789#endif
790
bellard31e31b82003-02-18 22:55:36 +0000791void usage(void)
792{
bellard93ac68b2003-09-30 20:57:29 +0000793 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
794 "usage: qemu-" TARGET_ARCH " [-h] [-d] [-L path] [-s size] program [arguments...]\n"
bellardb346ff42003-06-15 20:05:50 +0000795 "Linux CPU emulator (compiled for %s emulation)\n"
bellardd691f662003-03-24 21:58:34 +0000796 "\n"
bellard54936002003-05-13 00:25:15 +0000797 "-h print this help\n"
bellardb346ff42003-06-15 20:05:50 +0000798 "-L path set the elf interpreter prefix (default=%s)\n"
799 "-s size set the stack size in bytes (default=%ld)\n"
bellard54936002003-05-13 00:25:15 +0000800 "\n"
801 "debug options:\n"
bellardc6981052004-02-16 21:49:03 +0000802#ifdef USE_CODE_COPY
803 "-no-code-copy disable code copy acceleration\n"
804#endif
bellard54936002003-05-13 00:25:15 +0000805 "-d activate log (logfile=%s)\n"
806 "-p pagesize set the host page size to 'pagesize'\n",
bellardb346ff42003-06-15 20:05:50 +0000807 TARGET_ARCH,
bellardd691f662003-03-24 21:58:34 +0000808 interp_prefix,
bellard54936002003-05-13 00:25:15 +0000809 x86_stack_size,
810 DEBUG_LOGFILE);
bellard74cd30b2003-04-11 00:13:41 +0000811 _exit(1);
bellard31e31b82003-02-18 22:55:36 +0000812}
813
bellard9de5e442003-03-23 16:49:39 +0000814/* XXX: currently only used for async signals (see signal.c) */
bellardb346ff42003-06-15 20:05:50 +0000815CPUState *global_env;
bellard59faf6d2003-06-25 16:18:50 +0000816/* used only if single thread */
817CPUState *cpu_single_env = NULL;
818
bellard851e67a2003-03-29 16:53:14 +0000819/* used to free thread contexts */
820TaskState *first_task_state;
bellard9de5e442003-03-23 16:49:39 +0000821
bellard31e31b82003-02-18 22:55:36 +0000822int main(int argc, char **argv)
823{
824 const char *filename;
bellard01ffc752003-02-18 23:00:51 +0000825 struct target_pt_regs regs1, *regs = &regs1;
bellard31e31b82003-02-18 22:55:36 +0000826 struct image_info info1, *info = &info1;
bellard851e67a2003-03-29 16:53:14 +0000827 TaskState ts1, *ts = &ts1;
bellardb346ff42003-06-15 20:05:50 +0000828 CPUState *env;
bellard586314f2003-03-03 15:02:29 +0000829 int optind;
bellardd691f662003-03-24 21:58:34 +0000830 const char *r;
831
bellard31e31b82003-02-18 22:55:36 +0000832 if (argc <= 1)
833 usage();
bellardf801f972003-04-07 21:31:06 +0000834
bellardcc38b842003-10-27 21:16:14 +0000835 /* init debug */
836 cpu_set_log_filename(DEBUG_LOGFILE);
837
bellard586314f2003-03-03 15:02:29 +0000838 optind = 1;
bellardd691f662003-03-24 21:58:34 +0000839 for(;;) {
840 if (optind >= argc)
841 break;
842 r = argv[optind];
843 if (r[0] != '-')
844 break;
bellard586314f2003-03-03 15:02:29 +0000845 optind++;
bellardd691f662003-03-24 21:58:34 +0000846 r++;
847 if (!strcmp(r, "-")) {
848 break;
849 } else if (!strcmp(r, "d")) {
bellarde19e89a2004-03-21 17:08:23 +0000850 int mask;
851 CPULogItem *item;
852
853 mask = cpu_str_to_log_mask(optarg);
854 if (!mask) {
855 printf("Log items (comma separated):\n");
856 for(item = cpu_log_items; item->mask != 0; item++) {
857 printf("%-10s %s\n", item->name, item->help);
858 }
859 exit(1);
860 }
861 cpu_set_log(mask);
bellardd691f662003-03-24 21:58:34 +0000862 } else if (!strcmp(r, "s")) {
863 r = argv[optind++];
864 x86_stack_size = strtol(r, (char **)&r, 0);
865 if (x86_stack_size <= 0)
866 usage();
867 if (*r == 'M')
868 x86_stack_size *= 1024 * 1024;
869 else if (*r == 'k' || *r == 'K')
870 x86_stack_size *= 1024;
871 } else if (!strcmp(r, "L")) {
872 interp_prefix = argv[optind++];
bellard54936002003-05-13 00:25:15 +0000873 } else if (!strcmp(r, "p")) {
874 host_page_size = atoi(argv[optind++]);
875 if (host_page_size == 0 ||
876 (host_page_size & (host_page_size - 1)) != 0) {
877 fprintf(stderr, "page size must be a power of two\n");
878 exit(1);
879 }
bellardc6981052004-02-16 21:49:03 +0000880 } else
881#ifdef USE_CODE_COPY
882 if (!strcmp(r, "no-code-copy")) {
883 code_copy_enabled = 0;
884 } else
885#endif
886 {
bellardd691f662003-03-24 21:58:34 +0000887 usage();
888 }
bellard586314f2003-03-03 15:02:29 +0000889 }
bellardd691f662003-03-24 21:58:34 +0000890 if (optind >= argc)
891 usage();
bellard586314f2003-03-03 15:02:29 +0000892 filename = argv[optind];
893
bellard31e31b82003-02-18 22:55:36 +0000894 /* Zero out regs */
bellard01ffc752003-02-18 23:00:51 +0000895 memset(regs, 0, sizeof(struct target_pt_regs));
bellard31e31b82003-02-18 22:55:36 +0000896
897 /* Zero out image_info */
898 memset(info, 0, sizeof(struct image_info));
899
bellard74cd30b2003-04-11 00:13:41 +0000900 /* Scan interp_prefix dir for replacement files. */
901 init_paths(interp_prefix);
902
bellard54936002003-05-13 00:25:15 +0000903 /* NOTE: we need to init the CPU at this stage to get the
904 host_page_size */
bellardb346ff42003-06-15 20:05:50 +0000905 env = cpu_init();
bellard92ccca62003-06-24 13:30:31 +0000906
bellard74cd30b2003-04-11 00:13:41 +0000907 if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
bellard31e31b82003-02-18 22:55:36 +0000908 printf("Error loading %s\n", filename);
bellard74cd30b2003-04-11 00:13:41 +0000909 _exit(1);
bellard31e31b82003-02-18 22:55:36 +0000910 }
911
bellard4b74fe12003-03-03 23:23:09 +0000912 if (loglevel) {
bellard54936002003-05-13 00:25:15 +0000913 page_dump(logfile);
914
bellard4b74fe12003-03-03 23:23:09 +0000915 fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
916 fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
917 fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
918 fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
919 fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
920 fprintf(logfile, "brk 0x%08lx\n" , info->brk);
bellardb346ff42003-06-15 20:05:50 +0000921 fprintf(logfile, "entry 0x%08lx\n" , info->entry);
bellard4b74fe12003-03-03 23:23:09 +0000922 }
bellard31e31b82003-02-18 22:55:36 +0000923
924 target_set_brk((char *)info->brk);
925 syscall_init();
bellard66fb9762003-03-23 01:06:05 +0000926 signal_init();
bellard31e31b82003-02-18 22:55:36 +0000927
bellard9de5e442003-03-23 16:49:39 +0000928 global_env = env;
bellard31e31b82003-02-18 22:55:36 +0000929
bellard851e67a2003-03-29 16:53:14 +0000930 /* build Task State */
931 memset(ts, 0, sizeof(TaskState));
932 env->opaque = ts;
933 ts->used = 1;
bellard59faf6d2003-06-25 16:18:50 +0000934 env->user_mode_only = 1;
bellard851e67a2003-03-29 16:53:14 +0000935
bellardb346ff42003-06-15 20:05:50 +0000936#if defined(TARGET_I386)
bellard2e255c62003-08-21 23:25:21 +0000937 cpu_x86_set_cpl(env, 3);
938
bellard3802ce22003-07-26 18:02:28 +0000939 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
bellard75c62152004-01-04 18:08:37 +0000940 env->hflags |= HF_PE_MASK;
bellard3802ce22003-07-26 18:02:28 +0000941
bellard415e5612004-02-03 23:37:12 +0000942 /* flags setup : we activate the IRQs by default as in user mode */
943 env->eflags |= IF_MASK;
944
bellard6dbad632003-03-16 18:05:05 +0000945 /* linux register setup */
bellard0ecfa992003-03-03 14:32:43 +0000946 env->regs[R_EAX] = regs->eax;
947 env->regs[R_EBX] = regs->ebx;
948 env->regs[R_ECX] = regs->ecx;
949 env->regs[R_EDX] = regs->edx;
950 env->regs[R_ESI] = regs->esi;
951 env->regs[R_EDI] = regs->edi;
952 env->regs[R_EBP] = regs->ebp;
953 env->regs[R_ESP] = regs->esp;
bellarddab2ed92003-03-22 15:23:14 +0000954 env->eip = regs->eip;
bellard31e31b82003-02-18 22:55:36 +0000955
bellardf4beb512003-05-27 23:28:08 +0000956 /* linux interrupt setup */
957 env->idt.base = (void *)idt_table;
958 env->idt.limit = sizeof(idt_table) - 1;
959 set_idt(0, 0);
960 set_idt(1, 0);
961 set_idt(2, 0);
962 set_idt(3, 3);
963 set_idt(4, 3);
964 set_idt(5, 3);
965 set_idt(6, 0);
966 set_idt(7, 0);
967 set_idt(8, 0);
968 set_idt(9, 0);
969 set_idt(10, 0);
970 set_idt(11, 0);
971 set_idt(12, 0);
972 set_idt(13, 0);
973 set_idt(14, 0);
974 set_idt(15, 0);
975 set_idt(16, 0);
976 set_idt(17, 0);
977 set_idt(18, 0);
978 set_idt(19, 0);
979 set_idt(0x80, 3);
980
bellard6dbad632003-03-16 18:05:05 +0000981 /* linux segment setup */
982 env->gdt.base = (void *)gdt_table;
983 env->gdt.limit = sizeof(gdt_table) - 1;
bellardf4beb512003-05-27 23:28:08 +0000984 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
985 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
986 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
987 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
988 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
989 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
bellard6dbad632003-03-16 18:05:05 +0000990 cpu_x86_load_seg(env, R_CS, __USER_CS);
991 cpu_x86_load_seg(env, R_DS, __USER_DS);
992 cpu_x86_load_seg(env, R_ES, __USER_DS);
993 cpu_x86_load_seg(env, R_SS, __USER_DS);
994 cpu_x86_load_seg(env, R_FS, __USER_DS);
995 cpu_x86_load_seg(env, R_GS, __USER_DS);
bellard92ccca62003-06-24 13:30:31 +0000996
bellardb346ff42003-06-15 20:05:50 +0000997#elif defined(TARGET_ARM)
998 {
999 int i;
1000 for(i = 0; i < 16; i++) {
1001 env->regs[i] = regs->uregs[i];
1002 }
1003 env->cpsr = regs->uregs[16];
1004 }
bellard93ac68b2003-09-30 20:57:29 +00001005#elif defined(TARGET_SPARC)
bellard060366c2004-01-04 15:50:01 +00001006 {
1007 int i;
1008 env->pc = regs->pc;
1009 env->npc = regs->npc;
1010 env->y = regs->y;
1011 for(i = 0; i < 8; i++)
1012 env->gregs[i] = regs->u_regs[i];
1013 for(i = 0; i < 8; i++)
1014 env->regwptr[i] = regs->u_regs[i + 8];
1015 }
bellard67867302003-11-23 17:05:30 +00001016#elif defined(TARGET_PPC)
1017 {
1018 int i;
bellard61190b12004-01-04 23:54:31 +00001019 for (i = 0; i < 32; i++) {
1020 if (i != 12 && i != 6)
1021 env->msr[i] = (regs->msr >> i) & 1;
1022 }
bellard67867302003-11-23 17:05:30 +00001023 env->nip = regs->nip;
1024 for(i = 0; i < 32; i++) {
1025 env->gpr[i] = regs->gpr[i];
1026 }
1027 }
bellardb346ff42003-06-15 20:05:50 +00001028#else
1029#error unsupported target CPU
1030#endif
bellard31e31b82003-02-18 22:55:36 +00001031
bellard1b6b0292003-03-22 17:31:38 +00001032 cpu_loop(env);
1033 /* never exits */
bellard31e31b82003-02-18 22:55:36 +00001034 return 0;
1035}