Merge commit 'origin/upstream' into test
[qemu] / linux-user / main.c
1 /*
2  *  qemu user main
3  *
4  *  Copyright (c) 2003-2008 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., 51 Franklin Street - Fifth Floor, Boston,
19  *  MA 02110-1301, USA.
20  */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/syscall.h>
29
30 #include "qemu.h"
31 #include "qemu-common.h"
32 #include "cache-utils.h"
33 /* For tb_lock */
34 #include "exec-all.h"
35
36
37 #include "envlist.h"
38
39 #define DEBUG_LOGFILE "/tmp/qemu.log"
40
41 const char *exec_path;
42
43 #if defined(CONFIG_USE_GUEST_BASE)
44 unsigned long mmap_min_addr = 0;
45 unsigned long guest_base = 0;
46 #endif
47
48 int singlestep;
49
50 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
51 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
52
53 #if defined(__i386__) && !defined(CONFIG_STATIC)
54 /* Force usage of an ELF interpreter even if it is an ELF shared
55    object ! */
56 const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
57 #endif
58
59 /* for recent libc, we add these dummy symbols which are not declared
60    when generating a linked object (bug in ld ?) */
61 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined(CONFIG_STATIC)
62 asm(".globl __preinit_array_start\n"
63     ".globl __preinit_array_end\n"
64     ".globl __init_array_start\n"
65     ".globl __init_array_end\n"
66     ".globl __fini_array_start\n"
67     ".globl __fini_array_end\n"
68     ".section \".rodata\"\n"
69     "__preinit_array_start:\n"
70     "__preinit_array_end:\n"
71     "__init_array_start:\n"
72     "__init_array_end:\n"
73     "__fini_array_start:\n"
74     "__fini_array_end:\n"
75     ".long 0\n"
76     ".previous\n");
77 #endif
78
79 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
80    we allocate a bigger stack. Need a better solution, for example
81    by remapping the process stack directly at the right place */
82 unsigned long x86_stack_size = 512 * 1024;
83
84 void gemu_log(const char *fmt, ...)
85 {
86     va_list ap;
87
88     va_start(ap, fmt);
89     vfprintf(stderr, fmt, ap);
90     va_end(ap);
91 }
92
93 void cpu_outb(CPUState *env, int addr, int val)
94 {
95     fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
96 }
97
98 void cpu_outw(CPUState *env, int addr, int val)
99 {
100     fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
101 }
102
103 void cpu_outl(CPUState *env, int addr, int val)
104 {
105     fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
106 }
107
108 int cpu_inb(CPUState *env, int addr)
109 {
110     fprintf(stderr, "inb: port=0x%04x\n", addr);
111     return 0;
112 }
113
114 int cpu_inw(CPUState *env, int addr)
115 {
116     fprintf(stderr, "inw: port=0x%04x\n", addr);
117     return 0;
118 }
119
120 int cpu_inl(CPUState *env, int addr)
121 {
122     fprintf(stderr, "inl: port=0x%04x\n", addr);
123     return 0;
124 }
125
126 #if defined(TARGET_I386)
127 int cpu_get_pic_interrupt(CPUState *env)
128 {
129     return -1;
130 }
131 #endif
132
133 /* timers for rdtsc */
134
135 #if 0
136
137 static uint64_t emu_time;
138
139 int64_t cpu_get_real_ticks(void)
140 {
141     return emu_time++;
142 }
143
144 #endif
145
146 #if defined(USE_NPTL)
147 /***********************************************************/
148 /* Helper routines for implementing atomic operations.  */
149
150 /* To implement exclusive operations we force all cpus to syncronise.
151    We don't require a full sync, only that no cpus are executing guest code.
152    The alternative is to map target atomic ops onto host equivalents,
153    which requires quite a lot of per host/target work.  */
154 static pthread_mutex_t cpu_list_mutex = PTHREAD_MUTEX_INITIALIZER;
155 static pthread_mutex_t exclusive_lock = PTHREAD_MUTEX_INITIALIZER;
156 static pthread_cond_t exclusive_cond = PTHREAD_COND_INITIALIZER;
157 static pthread_cond_t exclusive_resume = PTHREAD_COND_INITIALIZER;
158 static int pending_cpus;
159
160 /* Make sure everything is in a consistent state for calling fork().  */
161 void fork_start(void)
162 {
163     mmap_fork_start();
164     pthread_mutex_lock(&tb_lock);
165     pthread_mutex_lock(&exclusive_lock);
166 }
167
168 void fork_end(int child)
169 {
170     if (child) {
171         /* Child processes created by fork() only have a single thread.
172            Discard information about the parent threads.  */
173         first_cpu = thread_env;
174         thread_env->next_cpu = NULL;
175         pending_cpus = 0;
176         pthread_mutex_init(&exclusive_lock, NULL);
177         pthread_mutex_init(&cpu_list_mutex, NULL);
178         pthread_cond_init(&exclusive_cond, NULL);
179         pthread_cond_init(&exclusive_resume, NULL);
180         pthread_mutex_init(&tb_lock, NULL);
181         gdbserver_fork(thread_env);
182     } else {
183         pthread_mutex_unlock(&exclusive_lock);
184         pthread_mutex_unlock(&tb_lock);
185     }
186     mmap_fork_end(child);
187 }
188
189 /* Wait for pending exclusive operations to complete.  The exclusive lock
190    must be held.  */
191 static inline void exclusive_idle(void)
192 {
193     while (pending_cpus) {
194         pthread_cond_wait(&exclusive_resume, &exclusive_lock);
195     }
196 }
197
198 /* Start an exclusive operation.
199    Must only be called from outside cpu_arm_exec.   */
200 static inline void start_exclusive(void)
201 {
202     CPUState *other;
203     pthread_mutex_lock(&exclusive_lock);
204     exclusive_idle();
205
206     pending_cpus = 1;
207     /* Make all other cpus stop executing.  */
208     for (other = first_cpu; other; other = other->next_cpu) {
209         if (other->running) {
210             pending_cpus++;
211             cpu_exit(other);
212         }
213     }
214     if (pending_cpus > 1) {
215         pthread_cond_wait(&exclusive_cond, &exclusive_lock);
216     }
217 }
218
219 /* Finish an exclusive operation.  */
220 static inline void end_exclusive(void)
221 {
222     pending_cpus = 0;
223     pthread_cond_broadcast(&exclusive_resume);
224     pthread_mutex_unlock(&exclusive_lock);
225 }
226
227 /* Wait for exclusive ops to finish, and begin cpu execution.  */
228 static inline void cpu_exec_start(CPUState *env)
229 {
230     pthread_mutex_lock(&exclusive_lock);
231     exclusive_idle();
232     env->running = 1;
233     pthread_mutex_unlock(&exclusive_lock);
234 }
235
236 /* Mark cpu as not executing, and release pending exclusive ops.  */
237 static inline void cpu_exec_end(CPUState *env)
238 {
239     pthread_mutex_lock(&exclusive_lock);
240     env->running = 0;
241     if (pending_cpus > 1) {
242         pending_cpus--;
243         if (pending_cpus == 1) {
244             pthread_cond_signal(&exclusive_cond);
245         }
246     }
247     exclusive_idle();
248     pthread_mutex_unlock(&exclusive_lock);
249 }
250
251 void cpu_list_lock(void)
252 {
253     pthread_mutex_lock(&cpu_list_mutex);
254 }
255
256 void cpu_list_unlock(void)
257 {
258     pthread_mutex_unlock(&cpu_list_mutex);
259 }
260 #else /* if !USE_NPTL */
261 /* These are no-ops because we are not threadsafe.  */
262 static inline void cpu_exec_start(CPUState *env)
263 {
264 }
265
266 static inline void cpu_exec_end(CPUState *env)
267 {
268 }
269
270 static inline void start_exclusive(void)
271 {
272 }
273
274 static inline void end_exclusive(void)
275 {
276 }
277
278 void fork_start(void)
279 {
280 }
281
282 void fork_end(int child)
283 {
284     if (child) {
285         gdbserver_fork(thread_env);
286     }
287 }
288
289 void cpu_list_lock(void)
290 {
291 }
292
293 void cpu_list_unlock(void)
294 {
295 }
296 #endif
297
298
299 #ifdef TARGET_I386
300 /***********************************************************/
301 /* CPUX86 core interface */
302
303 void cpu_smm_update(CPUState *env)
304 {
305 }
306
307 uint64_t cpu_get_tsc(CPUX86State *env)
308 {
309     return cpu_get_real_ticks();
310 }
311
312 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
313                      int flags)
314 {
315     unsigned int e1, e2;
316     uint32_t *p;
317     e1 = (addr << 16) | (limit & 0xffff);
318     e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
319     e2 |= flags;
320     p = ptr;
321     p[0] = tswap32(e1);
322     p[1] = tswap32(e2);
323 }
324
325 static uint64_t *idt_table;
326 #ifdef TARGET_X86_64
327 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
328                        uint64_t addr, unsigned int sel)
329 {
330     uint32_t *p, e1, e2;
331     e1 = (addr & 0xffff) | (sel << 16);
332     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
333     p = ptr;
334     p[0] = tswap32(e1);
335     p[1] = tswap32(e2);
336     p[2] = tswap32(addr >> 32);
337     p[3] = 0;
338 }
339 /* only dpl matters as we do only user space emulation */
340 static void set_idt(int n, unsigned int dpl)
341 {
342     set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
343 }
344 #else
345 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
346                      uint32_t addr, unsigned int sel)
347 {
348     uint32_t *p, e1, e2;
349     e1 = (addr & 0xffff) | (sel << 16);
350     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
351     p = ptr;
352     p[0] = tswap32(e1);
353     p[1] = tswap32(e2);
354 }
355
356 /* only dpl matters as we do only user space emulation */
357 static void set_idt(int n, unsigned int dpl)
358 {
359     set_gate(idt_table + n, 0, dpl, 0, 0);
360 }
361 #endif
362
363 void cpu_loop(CPUX86State *env)
364 {
365     int trapnr;
366     abi_ulong pc;
367     target_siginfo_t info;
368
369     for(;;) {
370         trapnr = cpu_x86_exec(env);
371         switch(trapnr) {
372         case 0x80:
373             /* linux syscall from int $0x80 */
374             env->regs[R_EAX] = do_syscall(env,
375                                           env->regs[R_EAX],
376                                           env->regs[R_EBX],
377                                           env->regs[R_ECX],
378                                           env->regs[R_EDX],
379                                           env->regs[R_ESI],
380                                           env->regs[R_EDI],
381                                           env->regs[R_EBP]);
382             break;
383 #ifndef TARGET_ABI32
384         case EXCP_SYSCALL:
385             /* linux syscall from syscall intruction */
386             env->regs[R_EAX] = do_syscall(env,
387                                           env->regs[R_EAX],
388                                           env->regs[R_EDI],
389                                           env->regs[R_ESI],
390                                           env->regs[R_EDX],
391                                           env->regs[10],
392                                           env->regs[8],
393                                           env->regs[9]);
394             env->eip = env->exception_next_eip;
395             break;
396 #endif
397         case EXCP0B_NOSEG:
398         case EXCP0C_STACK:
399             info.si_signo = SIGBUS;
400             info.si_errno = 0;
401             info.si_code = TARGET_SI_KERNEL;
402             info._sifields._sigfault._addr = 0;
403             queue_signal(env, info.si_signo, &info);
404             break;
405         case EXCP0D_GPF:
406             /* XXX: potential problem if ABI32 */
407 #ifndef TARGET_X86_64
408             if (env->eflags & VM_MASK) {
409                 handle_vm86_fault(env);
410             } else
411 #endif
412             {
413                 info.si_signo = SIGSEGV;
414                 info.si_errno = 0;
415                 info.si_code = TARGET_SI_KERNEL;
416                 info._sifields._sigfault._addr = 0;
417                 queue_signal(env, info.si_signo, &info);
418             }
419             break;
420         case EXCP0E_PAGE:
421             info.si_signo = SIGSEGV;
422             info.si_errno = 0;
423             if (!(env->error_code & 1))
424                 info.si_code = TARGET_SEGV_MAPERR;
425             else
426                 info.si_code = TARGET_SEGV_ACCERR;
427             info._sifields._sigfault._addr = env->cr[2];
428             queue_signal(env, info.si_signo, &info);
429             break;
430         case EXCP00_DIVZ:
431 #ifndef TARGET_X86_64
432             if (env->eflags & VM_MASK) {
433                 handle_vm86_trap(env, trapnr);
434             } else
435 #endif
436             {
437                 /* division by zero */
438                 info.si_signo = SIGFPE;
439                 info.si_errno = 0;
440                 info.si_code = TARGET_FPE_INTDIV;
441                 info._sifields._sigfault._addr = env->eip;
442                 queue_signal(env, info.si_signo, &info);
443             }
444             break;
445         case EXCP01_DB:
446         case EXCP03_INT3:
447 #ifndef TARGET_X86_64
448             if (env->eflags & VM_MASK) {
449                 handle_vm86_trap(env, trapnr);
450             } else
451 #endif
452             {
453                 info.si_signo = SIGTRAP;
454                 info.si_errno = 0;
455                 if (trapnr == EXCP01_DB) {
456                     info.si_code = TARGET_TRAP_BRKPT;
457                     info._sifields._sigfault._addr = env->eip;
458                 } else {
459                     info.si_code = TARGET_SI_KERNEL;
460                     info._sifields._sigfault._addr = 0;
461                 }
462                 queue_signal(env, info.si_signo, &info);
463             }
464             break;
465         case EXCP04_INTO:
466         case EXCP05_BOUND:
467 #ifndef TARGET_X86_64
468             if (env->eflags & VM_MASK) {
469                 handle_vm86_trap(env, trapnr);
470             } else
471 #endif
472             {
473                 info.si_signo = SIGSEGV;
474                 info.si_errno = 0;
475                 info.si_code = TARGET_SI_KERNEL;
476                 info._sifields._sigfault._addr = 0;
477                 queue_signal(env, info.si_signo, &info);
478             }
479             break;
480         case EXCP06_ILLOP:
481             info.si_signo = SIGILL;
482             info.si_errno = 0;
483             info.si_code = TARGET_ILL_ILLOPN;
484             info._sifields._sigfault._addr = env->eip;
485             queue_signal(env, info.si_signo, &info);
486             break;
487         case EXCP_INTERRUPT:
488             /* just indicate that signals should be handled asap */
489             break;
490         case EXCP_DEBUG:
491             {
492                 int sig;
493
494                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
495                 if (sig)
496                   {
497                     info.si_signo = sig;
498                     info.si_errno = 0;
499                     info.si_code = TARGET_TRAP_BRKPT;
500                     queue_signal(env, info.si_signo, &info);
501                   }
502             }
503             break;
504         default:
505             pc = env->segs[R_CS].base + env->eip;
506             fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
507                     (long)pc, trapnr);
508             abort();
509         }
510         process_pending_signals(env);
511     }
512 }
513 #endif
514
515 #ifdef TARGET_ARM
516
517 static void arm_cache_flush(abi_ulong start, abi_ulong last)
518 {
519     abi_ulong addr, last1;
520
521     if (last < start)
522         return;
523     addr = start;
524     for(;;) {
525         last1 = ((addr + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK) - 1;
526         if (last1 > last)
527             last1 = last;
528         tb_invalidate_page_range(addr, last1 + 1);
529         if (last1 == last)
530             break;
531         addr = last1 + 1;
532     }
533 }
534
535 /* Handle a jump to the kernel code page.  */
536 static int
537 do_kernel_trap(CPUARMState *env)
538 {
539     uint32_t addr;
540     uint32_t cpsr;
541     uint32_t val;
542
543     switch (env->regs[15]) {
544     case 0xffff0fa0: /* __kernel_memory_barrier */
545         /* ??? No-op. Will need to do better for SMP.  */
546         break;
547     case 0xffff0fc0: /* __kernel_cmpxchg */
548          /* XXX: This only works between threads, not between processes.
549             It's probably possible to implement this with native host
550             operations. However things like ldrex/strex are much harder so
551             there's not much point trying.  */
552         start_exclusive();
553         cpsr = cpsr_read(env);
554         addr = env->regs[2];
555         /* FIXME: This should SEGV if the access fails.  */
556         if (get_user_u32(val, addr))
557             val = ~env->regs[0];
558         if (val == env->regs[0]) {
559             val = env->regs[1];
560             /* FIXME: Check for segfaults.  */
561             put_user_u32(val, addr);
562             env->regs[0] = 0;
563             cpsr |= CPSR_C;
564         } else {
565             env->regs[0] = -1;
566             cpsr &= ~CPSR_C;
567         }
568         cpsr_write(env, cpsr, CPSR_C);
569         end_exclusive();
570         break;
571     case 0xffff0fe0: /* __kernel_get_tls */
572         env->regs[0] = env->cp15.c13_tls2;
573         break;
574     default:
575         return 1;
576     }
577     /* Jump back to the caller.  */
578     addr = env->regs[14];
579     if (addr & 1) {
580         env->thumb = 1;
581         addr &= ~1;
582     }
583     env->regs[15] = addr;
584
585     return 0;
586 }
587
588 void cpu_loop(CPUARMState *env)
589 {
590     int trapnr;
591     unsigned int n, insn;
592     target_siginfo_t info;
593     uint32_t addr;
594
595     for(;;) {
596         cpu_exec_start(env);
597         trapnr = cpu_arm_exec(env);
598         cpu_exec_end(env);
599         switch(trapnr) {
600         case EXCP_UDEF:
601             {
602                 TaskState *ts = env->opaque;
603                 uint32_t opcode;
604                 int rc;
605
606                 /* we handle the FPU emulation here, as Linux */
607                 /* we get the opcode */
608                 /* FIXME - what to do if get_user() fails? */
609                 get_user_u32(opcode, env->regs[15]);
610
611                 rc = EmulateAll(opcode, &ts->fpa, env);
612                 if (rc == 0) { /* illegal instruction */
613                     info.si_signo = SIGILL;
614                     info.si_errno = 0;
615                     info.si_code = TARGET_ILL_ILLOPN;
616                     info._sifields._sigfault._addr = env->regs[15];
617                     queue_signal(env, info.si_signo, &info);
618                 } else if (rc < 0) { /* FP exception */
619                     int arm_fpe=0;
620
621                     /* translate softfloat flags to FPSR flags */
622                     if (-rc & float_flag_invalid)
623                       arm_fpe |= BIT_IOC;
624                     if (-rc & float_flag_divbyzero)
625                       arm_fpe |= BIT_DZC;
626                     if (-rc & float_flag_overflow)
627                       arm_fpe |= BIT_OFC;
628                     if (-rc & float_flag_underflow)
629                       arm_fpe |= BIT_UFC;
630                     if (-rc & float_flag_inexact)
631                       arm_fpe |= BIT_IXC;
632
633                     FPSR fpsr = ts->fpa.fpsr;
634                     //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
635
636                     if (fpsr & (arm_fpe << 16)) { /* exception enabled? */
637                       info.si_signo = SIGFPE;
638                       info.si_errno = 0;
639
640                       /* ordered by priority, least first */
641                       if (arm_fpe & BIT_IXC) info.si_code = TARGET_FPE_FLTRES;
642                       if (arm_fpe & BIT_UFC) info.si_code = TARGET_FPE_FLTUND;
643                       if (arm_fpe & BIT_OFC) info.si_code = TARGET_FPE_FLTOVF;
644                       if (arm_fpe & BIT_DZC) info.si_code = TARGET_FPE_FLTDIV;
645                       if (arm_fpe & BIT_IOC) info.si_code = TARGET_FPE_FLTINV;
646
647                       info._sifields._sigfault._addr = env->regs[15];
648                       queue_signal(env, info.si_signo, &info);
649                     } else {
650                       env->regs[15] += 4;
651                     }
652
653                     /* accumulate unenabled exceptions */
654                     if ((!(fpsr & BIT_IXE)) && (arm_fpe & BIT_IXC))
655                       fpsr |= BIT_IXC;
656                     if ((!(fpsr & BIT_UFE)) && (arm_fpe & BIT_UFC))
657                       fpsr |= BIT_UFC;
658                     if ((!(fpsr & BIT_OFE)) && (arm_fpe & BIT_OFC))
659                       fpsr |= BIT_OFC;
660                     if ((!(fpsr & BIT_DZE)) && (arm_fpe & BIT_DZC))
661                       fpsr |= BIT_DZC;
662                     if ((!(fpsr & BIT_IOE)) && (arm_fpe & BIT_IOC))
663                       fpsr |= BIT_IOC;
664                     ts->fpa.fpsr=fpsr;
665                 } else { /* everything OK */
666                     /* increment PC */
667                     env->regs[15] += 4;
668                 }
669             }
670             break;
671         case EXCP_SWI:
672         case EXCP_BKPT:
673             {
674                 env->eabi = 1;
675                 /* system call */
676                 if (trapnr == EXCP_BKPT) {
677                     if (env->thumb) {
678                         /* FIXME - what to do if get_user() fails? */
679                         get_user_u16(insn, env->regs[15]);
680                         n = insn & 0xff;
681                         env->regs[15] += 2;
682                     } else {
683                         /* FIXME - what to do if get_user() fails? */
684                         get_user_u32(insn, env->regs[15]);
685                         n = (insn & 0xf) | ((insn >> 4) & 0xff0);
686                         env->regs[15] += 4;
687                     }
688                 } else {
689                     if (env->thumb) {
690                         /* FIXME - what to do if get_user() fails? */
691                         get_user_u16(insn, env->regs[15] - 2);
692                         n = insn & 0xff;
693                     } else {
694                         /* FIXME - what to do if get_user() fails? */
695                         get_user_u32(insn, env->regs[15] - 4);
696                         n = insn & 0xffffff;
697                     }
698                 }
699
700                 if (n == ARM_NR_cacheflush) {
701                     arm_cache_flush(env->regs[0], env->regs[1]);
702                 } else if (n == ARM_NR_semihosting
703                            || n == ARM_NR_thumb_semihosting) {
704                     env->regs[0] = do_arm_semihosting (env);
705                 } else if (n == 0 || n >= ARM_SYSCALL_BASE
706                            || (env->thumb && n == ARM_THUMB_SYSCALL)) {
707                     /* linux syscall */
708                     if (env->thumb || n == 0) {
709                         n = env->regs[7];
710                     } else {
711                         n -= ARM_SYSCALL_BASE;
712                         env->eabi = 0;
713                     }
714                     if ( n > ARM_NR_BASE) {
715                         switch (n) {
716                         case ARM_NR_cacheflush:
717                             arm_cache_flush(env->regs[0], env->regs[1]);
718                             break;
719                         case ARM_NR_set_tls:
720                             cpu_set_tls(env, env->regs[0]);
721                             env->regs[0] = 0;
722                             break;
723                         default:
724                             gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
725                                      n);
726                             env->regs[0] = -TARGET_ENOSYS;
727                             break;
728                         }
729                     } else {
730                         env->regs[0] = do_syscall(env,
731                                                   n,
732                                                   env->regs[0],
733                                                   env->regs[1],
734                                                   env->regs[2],
735                                                   env->regs[3],
736                                                   env->regs[4],
737                                                   env->regs[5]);
738                     }
739                 } else {
740                     goto error;
741                 }
742             }
743             break;
744         case EXCP_INTERRUPT:
745             /* just indicate that signals should be handled asap */
746             break;
747         case EXCP_PREFETCH_ABORT:
748             addr = env->cp15.c6_insn;
749             goto do_segv;
750         case EXCP_DATA_ABORT:
751             addr = env->cp15.c6_data;
752             goto do_segv;
753         do_segv:
754             {
755                 info.si_signo = SIGSEGV;
756                 info.si_errno = 0;
757                 /* XXX: check env->error_code */
758                 info.si_code = TARGET_SEGV_MAPERR;
759                 info._sifields._sigfault._addr = addr;
760                 queue_signal(env, info.si_signo, &info);
761             }
762             break;
763         case EXCP_DEBUG:
764             {
765                 int sig;
766
767                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
768                 if (sig)
769                   {
770                     info.si_signo = sig;
771                     info.si_errno = 0;
772                     info.si_code = TARGET_TRAP_BRKPT;
773                     queue_signal(env, info.si_signo, &info);
774                   }
775             }
776             break;
777         case EXCP_KERNEL_TRAP:
778             if (do_kernel_trap(env))
779               goto error;
780             break;
781         default:
782         error:
783             fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
784                     trapnr);
785             cpu_dump_state(env, stderr, fprintf, 0);
786             abort();
787         }
788         process_pending_signals(env);
789     }
790 }
791
792 #endif
793
794 #ifdef TARGET_SPARC
795 #define SPARC64_STACK_BIAS 2047
796
797 //#define DEBUG_WIN
798
799 /* WARNING: dealing with register windows _is_ complicated. More info
800    can be found at http://www.sics.se/~psm/sparcstack.html */
801 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
802 {
803     index = (index + cwp * 16) % (16 * env->nwindows);
804     /* wrap handling : if cwp is on the last window, then we use the
805        registers 'after' the end */
806     if (index < 8 && env->cwp == env->nwindows - 1)
807         index += 16 * env->nwindows;
808     return index;
809 }
810
811 /* save the register window 'cwp1' */
812 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
813 {
814     unsigned int i;
815     abi_ulong sp_ptr;
816
817     sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
818 #ifdef TARGET_SPARC64
819     if (sp_ptr & 3)
820         sp_ptr += SPARC64_STACK_BIAS;
821 #endif
822 #if defined(DEBUG_WIN)
823     printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
824            sp_ptr, cwp1);
825 #endif
826     for(i = 0; i < 16; i++) {
827         /* FIXME - what to do if put_user() fails? */
828         put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
829         sp_ptr += sizeof(abi_ulong);
830     }
831 }
832
833 static void save_window(CPUSPARCState *env)
834 {
835 #ifndef TARGET_SPARC64
836     unsigned int new_wim;
837     new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
838         ((1LL << env->nwindows) - 1);
839     save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
840     env->wim = new_wim;
841 #else
842     save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
843     env->cansave++;
844     env->canrestore--;
845 #endif
846 }
847
848 static void restore_window(CPUSPARCState *env)
849 {
850 #ifndef TARGET_SPARC64
851     unsigned int new_wim;
852 #endif
853     unsigned int i, cwp1;
854     abi_ulong sp_ptr;
855
856 #ifndef TARGET_SPARC64
857     new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
858         ((1LL << env->nwindows) - 1);
859 #endif
860
861     /* restore the invalid window */
862     cwp1 = cpu_cwp_inc(env, env->cwp + 1);
863     sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
864 #ifdef TARGET_SPARC64
865     if (sp_ptr & 3)
866         sp_ptr += SPARC64_STACK_BIAS;
867 #endif
868 #if defined(DEBUG_WIN)
869     printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
870            sp_ptr, cwp1);
871 #endif
872     for(i = 0; i < 16; i++) {
873         /* FIXME - what to do if get_user() fails? */
874         get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
875         sp_ptr += sizeof(abi_ulong);
876     }
877 #ifdef TARGET_SPARC64
878     env->canrestore++;
879     if (env->cleanwin < env->nwindows - 1)
880         env->cleanwin++;
881     env->cansave--;
882 #else
883     env->wim = new_wim;
884 #endif
885 }
886
887 static void flush_windows(CPUSPARCState *env)
888 {
889     int offset, cwp1;
890
891     offset = 1;
892     for(;;) {
893         /* if restore would invoke restore_window(), then we can stop */
894         cwp1 = cpu_cwp_inc(env, env->cwp + offset);
895 #ifndef TARGET_SPARC64
896         if (env->wim & (1 << cwp1))
897             break;
898 #else
899         if (env->canrestore == 0)
900             break;
901         env->cansave++;
902         env->canrestore--;
903 #endif
904         save_window_offset(env, cwp1);
905         offset++;
906     }
907     cwp1 = cpu_cwp_inc(env, env->cwp + 1);
908 #ifndef TARGET_SPARC64
909     /* set wim so that restore will reload the registers */
910     env->wim = 1 << cwp1;
911 #endif
912 #if defined(DEBUG_WIN)
913     printf("flush_windows: nb=%d\n", offset - 1);
914 #endif
915 }
916
917 void cpu_loop (CPUSPARCState *env)
918 {
919     int trapnr, ret;
920     target_siginfo_t info;
921
922     while (1) {
923         trapnr = cpu_sparc_exec (env);
924
925         switch (trapnr) {
926 #ifndef TARGET_SPARC64
927         case 0x88:
928         case 0x90:
929 #else
930         case 0x110:
931         case 0x16d:
932 #endif
933             ret = do_syscall (env, env->gregs[1],
934                               env->regwptr[0], env->regwptr[1],
935                               env->regwptr[2], env->regwptr[3],
936                               env->regwptr[4], env->regwptr[5]);
937             if ((unsigned int)ret >= (unsigned int)(-515)) {
938 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
939                 env->xcc |= PSR_CARRY;
940 #else
941                 env->psr |= PSR_CARRY;
942 #endif
943                 ret = -ret;
944             } else {
945 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
946                 env->xcc &= ~PSR_CARRY;
947 #else
948                 env->psr &= ~PSR_CARRY;
949 #endif
950             }
951             env->regwptr[0] = ret;
952             /* next instruction */
953             env->pc = env->npc;
954             env->npc = env->npc + 4;
955             break;
956         case 0x83: /* flush windows */
957 #ifdef TARGET_ABI32
958         case 0x103:
959 #endif
960             flush_windows(env);
961             /* next instruction */
962             env->pc = env->npc;
963             env->npc = env->npc + 4;
964             break;
965 #ifndef TARGET_SPARC64
966         case TT_WIN_OVF: /* window overflow */
967             save_window(env);
968             break;
969         case TT_WIN_UNF: /* window underflow */
970             restore_window(env);
971             break;
972         case TT_TFAULT:
973         case TT_DFAULT:
974             {
975                 info.si_signo = SIGSEGV;
976                 info.si_errno = 0;
977                 /* XXX: check env->error_code */
978                 info.si_code = TARGET_SEGV_MAPERR;
979                 info._sifields._sigfault._addr = env->mmuregs[4];
980                 queue_signal(env, info.si_signo, &info);
981             }
982             break;
983 #else
984         case TT_SPILL: /* window overflow */
985             save_window(env);
986             break;
987         case TT_FILL: /* window underflow */
988             restore_window(env);
989             break;
990         case TT_TFAULT:
991         case TT_DFAULT:
992             {
993                 info.si_signo = SIGSEGV;
994                 info.si_errno = 0;
995                 /* XXX: check env->error_code */
996                 info.si_code = TARGET_SEGV_MAPERR;
997                 if (trapnr == TT_DFAULT)
998                     info._sifields._sigfault._addr = env->dmmuregs[4];
999                 else
1000                     info._sifields._sigfault._addr = env->tsptr->tpc;
1001                 queue_signal(env, info.si_signo, &info);
1002             }
1003             break;
1004 #ifndef TARGET_ABI32
1005         case 0x16e:
1006             flush_windows(env);
1007             sparc64_get_context(env);
1008             break;
1009         case 0x16f:
1010             flush_windows(env);
1011             sparc64_set_context(env);
1012             break;
1013 #endif
1014 #endif
1015         case EXCP_INTERRUPT:
1016             /* just indicate that signals should be handled asap */
1017             break;
1018         case EXCP_DEBUG:
1019             {
1020                 int sig;
1021
1022                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1023                 if (sig)
1024                   {
1025                     info.si_signo = sig;
1026                     info.si_errno = 0;
1027                     info.si_code = TARGET_TRAP_BRKPT;
1028                     queue_signal(env, info.si_signo, &info);
1029                   }
1030             }
1031             break;
1032         default:
1033             printf ("Unhandled trap: 0x%x\n", trapnr);
1034             cpu_dump_state(env, stderr, fprintf, 0);
1035             _exit (1);
1036         }
1037         process_pending_signals (env);
1038     }
1039 }
1040
1041 #endif
1042
1043 #ifdef TARGET_PPC
1044 static inline uint64_t cpu_ppc_get_tb (CPUState *env)
1045 {
1046     /* TO FIX */
1047     return 0;
1048 }
1049
1050 uint32_t cpu_ppc_load_tbl (CPUState *env)
1051 {
1052     return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
1053 }
1054
1055 uint32_t cpu_ppc_load_tbu (CPUState *env)
1056 {
1057     return cpu_ppc_get_tb(env) >> 32;
1058 }
1059
1060 uint32_t cpu_ppc_load_atbl (CPUState *env)
1061 {
1062     return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
1063 }
1064
1065 uint32_t cpu_ppc_load_atbu (CPUState *env)
1066 {
1067     return cpu_ppc_get_tb(env) >> 32;
1068 }
1069
1070 uint32_t cpu_ppc601_load_rtcu (CPUState *env)
1071 __attribute__ (( alias ("cpu_ppc_load_tbu") ));
1072
1073 uint32_t cpu_ppc601_load_rtcl (CPUState *env)
1074 {
1075     return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
1076 }
1077
1078 /* XXX: to be fixed */
1079 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
1080 {
1081     return -1;
1082 }
1083
1084 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
1085 {
1086     return -1;
1087 }
1088
1089 #define EXCP_DUMP(env, fmt, args...)                                         \
1090 do {                                                                          \
1091     fprintf(stderr, fmt , ##args);                                            \
1092     cpu_dump_state(env, stderr, fprintf, 0);                                  \
1093     qemu_log(fmt, ##args);                                                   \
1094     log_cpu_state(env, 0);                                                      \
1095 } while (0)
1096
1097 void cpu_loop(CPUPPCState *env)
1098 {
1099     target_siginfo_t info;
1100     int trapnr;
1101     uint32_t ret;
1102
1103     for(;;) {
1104         trapnr = cpu_ppc_exec(env);
1105         switch(trapnr) {
1106         case POWERPC_EXCP_NONE:
1107             /* Just go on */
1108             break;
1109         case POWERPC_EXCP_CRITICAL: /* Critical input                        */
1110             cpu_abort(env, "Critical interrupt while in user mode. "
1111                       "Aborting\n");
1112             break;
1113         case POWERPC_EXCP_MCHECK:   /* Machine check exception               */
1114             cpu_abort(env, "Machine check exception while in user mode. "
1115                       "Aborting\n");
1116             break;
1117         case POWERPC_EXCP_DSI:      /* Data storage exception                */
1118             EXCP_DUMP(env, "Invalid data memory access: 0x" ADDRX "\n",
1119                       env->spr[SPR_DAR]);
1120             /* XXX: check this. Seems bugged */
1121             switch (env->error_code & 0xFF000000) {
1122             case 0x40000000:
1123                 info.si_signo = TARGET_SIGSEGV;
1124                 info.si_errno = 0;
1125                 info.si_code = TARGET_SEGV_MAPERR;
1126                 break;
1127             case 0x04000000:
1128                 info.si_signo = TARGET_SIGILL;
1129                 info.si_errno = 0;
1130                 info.si_code = TARGET_ILL_ILLADR;
1131                 break;
1132             case 0x08000000:
1133                 info.si_signo = TARGET_SIGSEGV;
1134                 info.si_errno = 0;
1135                 info.si_code = TARGET_SEGV_ACCERR;
1136                 break;
1137             default:
1138                 /* Let's send a regular segfault... */
1139                 EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
1140                           env->error_code);
1141                 info.si_signo = TARGET_SIGSEGV;
1142                 info.si_errno = 0;
1143                 info.si_code = TARGET_SEGV_MAPERR;
1144                 break;
1145             }
1146             info._sifields._sigfault._addr = env->nip;
1147             queue_signal(env, info.si_signo, &info);
1148             break;
1149         case POWERPC_EXCP_ISI:      /* Instruction storage exception         */
1150             EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" ADDRX "\n",
1151                       env->spr[SPR_SRR0]);
1152             /* XXX: check this */
1153             switch (env->error_code & 0xFF000000) {
1154             case 0x40000000:
1155                 info.si_signo = TARGET_SIGSEGV;
1156             info.si_errno = 0;
1157                 info.si_code = TARGET_SEGV_MAPERR;
1158                 break;
1159             case 0x10000000:
1160             case 0x08000000:
1161                 info.si_signo = TARGET_SIGSEGV;
1162                 info.si_errno = 0;
1163                 info.si_code = TARGET_SEGV_ACCERR;
1164                 break;
1165             default:
1166                 /* Let's send a regular segfault... */
1167                 EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
1168                           env->error_code);
1169                 info.si_signo = TARGET_SIGSEGV;
1170                 info.si_errno = 0;
1171                 info.si_code = TARGET_SEGV_MAPERR;
1172                 break;
1173             }
1174             info._sifields._sigfault._addr = env->nip - 4;
1175             queue_signal(env, info.si_signo, &info);
1176             break;
1177         case POWERPC_EXCP_EXTERNAL: /* External input                        */
1178             cpu_abort(env, "External interrupt while in user mode. "
1179                       "Aborting\n");
1180             break;
1181         case POWERPC_EXCP_ALIGN:    /* Alignment exception                   */
1182             EXCP_DUMP(env, "Unaligned memory access\n");
1183             /* XXX: check this */
1184             info.si_signo = TARGET_SIGBUS;
1185             info.si_errno = 0;
1186             info.si_code = TARGET_BUS_ADRALN;
1187             info._sifields._sigfault._addr = env->nip - 4;
1188             queue_signal(env, info.si_signo, &info);
1189             break;
1190         case POWERPC_EXCP_PROGRAM:  /* Program exception                     */
1191             /* XXX: check this */
1192             switch (env->error_code & ~0xF) {
1193             case POWERPC_EXCP_FP:
1194                 EXCP_DUMP(env, "Floating point program exception\n");
1195                 info.si_signo = TARGET_SIGFPE;
1196                 info.si_errno = 0;
1197                 switch (env->error_code & 0xF) {
1198                 case POWERPC_EXCP_FP_OX:
1199                     info.si_code = TARGET_FPE_FLTOVF;
1200                     break;
1201                 case POWERPC_EXCP_FP_UX:
1202                     info.si_code = TARGET_FPE_FLTUND;
1203                     break;
1204                 case POWERPC_EXCP_FP_ZX:
1205                 case POWERPC_EXCP_FP_VXZDZ:
1206                     info.si_code = TARGET_FPE_FLTDIV;
1207                     break;
1208                 case POWERPC_EXCP_FP_XX:
1209                     info.si_code = TARGET_FPE_FLTRES;
1210                     break;
1211                 case POWERPC_EXCP_FP_VXSOFT:
1212                     info.si_code = TARGET_FPE_FLTINV;
1213                     break;
1214                 case POWERPC_EXCP_FP_VXSNAN:
1215                 case POWERPC_EXCP_FP_VXISI:
1216                 case POWERPC_EXCP_FP_VXIDI:
1217                 case POWERPC_EXCP_FP_VXIMZ:
1218                 case POWERPC_EXCP_FP_VXVC:
1219                 case POWERPC_EXCP_FP_VXSQRT:
1220                 case POWERPC_EXCP_FP_VXCVI:
1221                     info.si_code = TARGET_FPE_FLTSUB;
1222                     break;
1223                 default:
1224                     EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
1225                               env->error_code);
1226                     break;
1227                 }
1228                 break;
1229             case POWERPC_EXCP_INVAL:
1230                 EXCP_DUMP(env, "Invalid instruction\n");
1231                 info.si_signo = TARGET_SIGILL;
1232                 info.si_errno = 0;
1233                 switch (env->error_code & 0xF) {
1234                 case POWERPC_EXCP_INVAL_INVAL:
1235                     info.si_code = TARGET_ILL_ILLOPC;
1236                     break;
1237                 case POWERPC_EXCP_INVAL_LSWX:
1238                     info.si_code = TARGET_ILL_ILLOPN;
1239                     break;
1240                 case POWERPC_EXCP_INVAL_SPR:
1241                     info.si_code = TARGET_ILL_PRVREG;
1242                     break;
1243                 case POWERPC_EXCP_INVAL_FP:
1244                     info.si_code = TARGET_ILL_COPROC;
1245                     break;
1246                 default:
1247                     EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
1248                               env->error_code & 0xF);
1249                     info.si_code = TARGET_ILL_ILLADR;
1250                     break;
1251                 }
1252                 break;
1253             case POWERPC_EXCP_PRIV:
1254                 EXCP_DUMP(env, "Privilege violation\n");
1255                 info.si_signo = TARGET_SIGILL;
1256                 info.si_errno = 0;
1257                 switch (env->error_code & 0xF) {
1258                 case POWERPC_EXCP_PRIV_OPC:
1259                     info.si_code = TARGET_ILL_PRVOPC;
1260                     break;
1261                 case POWERPC_EXCP_PRIV_REG:
1262                     info.si_code = TARGET_ILL_PRVREG;
1263                     break;
1264                 default:
1265                     EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
1266                               env->error_code & 0xF);
1267                     info.si_code = TARGET_ILL_PRVOPC;
1268                     break;
1269                 }
1270                 break;
1271             case POWERPC_EXCP_TRAP:
1272                 cpu_abort(env, "Tried to call a TRAP\n");
1273                 break;
1274             default:
1275                 /* Should not happen ! */
1276                 cpu_abort(env, "Unknown program exception (%02x)\n",
1277                           env->error_code);
1278                 break;
1279             }
1280             info._sifields._sigfault._addr = env->nip - 4;
1281             queue_signal(env, info.si_signo, &info);
1282             break;
1283         case POWERPC_EXCP_FPU:      /* Floating-point unavailable exception  */
1284             EXCP_DUMP(env, "No floating point allowed\n");
1285             info.si_signo = TARGET_SIGILL;
1286             info.si_errno = 0;
1287             info.si_code = TARGET_ILL_COPROC;
1288             info._sifields._sigfault._addr = env->nip - 4;
1289             queue_signal(env, info.si_signo, &info);
1290             break;
1291         case POWERPC_EXCP_SYSCALL:  /* System call exception                 */
1292             cpu_abort(env, "Syscall exception while in user mode. "
1293                       "Aborting\n");
1294             break;
1295         case POWERPC_EXCP_APU:      /* Auxiliary processor unavailable       */
1296             EXCP_DUMP(env, "No APU instruction allowed\n");
1297             info.si_signo = TARGET_SIGILL;
1298             info.si_errno = 0;
1299             info.si_code = TARGET_ILL_COPROC;
1300             info._sifields._sigfault._addr = env->nip - 4;
1301             queue_signal(env, info.si_signo, &info);
1302             break;
1303         case POWERPC_EXCP_DECR:     /* Decrementer exception                 */
1304             cpu_abort(env, "Decrementer interrupt while in user mode. "
1305                       "Aborting\n");
1306             break;
1307         case POWERPC_EXCP_FIT:      /* Fixed-interval timer interrupt        */
1308             cpu_abort(env, "Fix interval timer interrupt while in user mode. "
1309                       "Aborting\n");
1310             break;
1311         case POWERPC_EXCP_WDT:      /* Watchdog timer interrupt              */
1312             cpu_abort(env, "Watchdog timer interrupt while in user mode. "
1313                       "Aborting\n");
1314             break;
1315         case POWERPC_EXCP_DTLB:     /* Data TLB error                        */
1316             cpu_abort(env, "Data TLB exception while in user mode. "
1317                       "Aborting\n");
1318             break;
1319         case POWERPC_EXCP_ITLB:     /* Instruction TLB error                 */
1320             cpu_abort(env, "Instruction TLB exception while in user mode. "
1321                       "Aborting\n");
1322             break;
1323         case POWERPC_EXCP_SPEU:     /* SPE/embedded floating-point unavail.  */
1324             EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
1325             info.si_signo = TARGET_SIGILL;
1326             info.si_errno = 0;
1327             info.si_code = TARGET_ILL_COPROC;
1328             info._sifields._sigfault._addr = env->nip - 4;
1329             queue_signal(env, info.si_signo, &info);
1330             break;
1331         case POWERPC_EXCP_EFPDI:    /* Embedded floating-point data IRQ      */
1332             cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
1333             break;
1334         case POWERPC_EXCP_EFPRI:    /* Embedded floating-point round IRQ     */
1335             cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
1336             break;
1337         case POWERPC_EXCP_EPERFM:   /* Embedded performance monitor IRQ      */
1338             cpu_abort(env, "Performance monitor exception not handled\n");
1339             break;
1340         case POWERPC_EXCP_DOORI:    /* Embedded doorbell interrupt           */
1341             cpu_abort(env, "Doorbell interrupt while in user mode. "
1342                        "Aborting\n");
1343             break;
1344         case POWERPC_EXCP_DOORCI:   /* Embedded doorbell critical interrupt  */
1345             cpu_abort(env, "Doorbell critical interrupt while in user mode. "
1346                       "Aborting\n");
1347             break;
1348         case POWERPC_EXCP_RESET:    /* System reset exception                */
1349             cpu_abort(env, "Reset interrupt while in user mode. "
1350                       "Aborting\n");
1351             break;
1352         case POWERPC_EXCP_DSEG:     /* Data segment exception                */
1353             cpu_abort(env, "Data segment exception while in user mode. "
1354                       "Aborting\n");
1355             break;
1356         case POWERPC_EXCP_ISEG:     /* Instruction segment exception         */
1357             cpu_abort(env, "Instruction segment exception "
1358                       "while in user mode. Aborting\n");
1359             break;
1360         /* PowerPC 64 with hypervisor mode support */
1361         case POWERPC_EXCP_HDECR:    /* Hypervisor decrementer exception      */
1362             cpu_abort(env, "Hypervisor decrementer interrupt "
1363                       "while in user mode. Aborting\n");
1364             break;
1365         case POWERPC_EXCP_TRACE:    /* Trace exception                       */
1366             /* Nothing to do:
1367              * we use this exception to emulate step-by-step execution mode.
1368              */
1369             break;
1370         /* PowerPC 64 with hypervisor mode support */
1371         case POWERPC_EXCP_HDSI:     /* Hypervisor data storage exception     */
1372             cpu_abort(env, "Hypervisor data storage exception "
1373                       "while in user mode. Aborting\n");
1374             break;
1375         case POWERPC_EXCP_HISI:     /* Hypervisor instruction storage excp   */
1376             cpu_abort(env, "Hypervisor instruction storage exception "
1377                       "while in user mode. Aborting\n");
1378             break;
1379         case POWERPC_EXCP_HDSEG:    /* Hypervisor data segment exception     */
1380             cpu_abort(env, "Hypervisor data segment exception "
1381                       "while in user mode. Aborting\n");
1382             break;
1383         case POWERPC_EXCP_HISEG:    /* Hypervisor instruction segment excp   */
1384             cpu_abort(env, "Hypervisor instruction segment exception "
1385                       "while in user mode. Aborting\n");
1386             break;
1387         case POWERPC_EXCP_VPU:      /* Vector unavailable exception          */
1388             EXCP_DUMP(env, "No Altivec instructions allowed\n");
1389             info.si_signo = TARGET_SIGILL;
1390             info.si_errno = 0;
1391             info.si_code = TARGET_ILL_COPROC;
1392             info._sifields._sigfault._addr = env->nip - 4;
1393             queue_signal(env, info.si_signo, &info);
1394             break;
1395         case POWERPC_EXCP_PIT:      /* Programmable interval timer IRQ       */
1396             cpu_abort(env, "Programable interval timer interrupt "
1397                       "while in user mode. Aborting\n");
1398             break;
1399         case POWERPC_EXCP_IO:       /* IO error exception                    */
1400             cpu_abort(env, "IO error exception while in user mode. "
1401                       "Aborting\n");
1402             break;
1403         case POWERPC_EXCP_RUNM:     /* Run mode exception                    */
1404             cpu_abort(env, "Run mode exception while in user mode. "
1405                       "Aborting\n");
1406             break;
1407         case POWERPC_EXCP_EMUL:     /* Emulation trap exception              */
1408             cpu_abort(env, "Emulation trap exception not handled\n");
1409             break;
1410         case POWERPC_EXCP_IFTLB:    /* Instruction fetch TLB error           */
1411             cpu_abort(env, "Instruction fetch TLB exception "
1412                       "while in user-mode. Aborting");
1413             break;
1414         case POWERPC_EXCP_DLTLB:    /* Data load TLB miss                    */
1415             cpu_abort(env, "Data load TLB exception while in user-mode. "
1416                       "Aborting");
1417             break;
1418         case POWERPC_EXCP_DSTLB:    /* Data store TLB miss                   */
1419             cpu_abort(env, "Data store TLB exception while in user-mode. "
1420                       "Aborting");
1421             break;
1422         case POWERPC_EXCP_FPA:      /* Floating-point assist exception       */
1423             cpu_abort(env, "Floating-point assist exception not handled\n");
1424             break;
1425         case POWERPC_EXCP_IABR:     /* Instruction address breakpoint        */
1426             cpu_abort(env, "Instruction address breakpoint exception "
1427                       "not handled\n");
1428             break;
1429         case POWERPC_EXCP_SMI:      /* System management interrupt           */
1430             cpu_abort(env, "System management interrupt while in user mode. "
1431                       "Aborting\n");
1432             break;
1433         case POWERPC_EXCP_THERM:    /* Thermal interrupt                     */
1434             cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
1435                       "Aborting\n");
1436             break;
1437         case POWERPC_EXCP_PERFM:   /* Embedded performance monitor IRQ      */
1438             cpu_abort(env, "Performance monitor exception not handled\n");
1439             break;
1440         case POWERPC_EXCP_VPUA:     /* Vector assist exception               */
1441             cpu_abort(env, "Vector assist exception not handled\n");
1442             break;
1443         case POWERPC_EXCP_SOFTP:    /* Soft patch exception                  */
1444             cpu_abort(env, "Soft patch exception not handled\n");
1445             break;
1446         case POWERPC_EXCP_MAINT:    /* Maintenance exception                 */
1447             cpu_abort(env, "Maintenance exception while in user mode. "
1448                       "Aborting\n");
1449             break;
1450         case POWERPC_EXCP_STOP:     /* stop translation                      */
1451             /* We did invalidate the instruction cache. Go on */
1452             break;
1453         case POWERPC_EXCP_BRANCH:   /* branch instruction:                   */
1454             /* We just stopped because of a branch. Go on */
1455             break;
1456         case POWERPC_EXCP_SYSCALL_USER:
1457             /* system call in user-mode emulation */
1458             /* WARNING:
1459              * PPC ABI uses overflow flag in cr0 to signal an error
1460              * in syscalls.
1461              */
1462 #if 0
1463             printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0],
1464                    env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]);
1465 #endif
1466             env->crf[0] &= ~0x1;
1467             ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
1468                              env->gpr[5], env->gpr[6], env->gpr[7],
1469                              env->gpr[8]);
1470             if (ret > (uint32_t)(-515)) {
1471                 env->crf[0] |= 0x1;
1472                 ret = -ret;
1473             }
1474             env->gpr[3] = ret;
1475 #if 0
1476             printf("syscall returned 0x%08x (%d)\n", ret, ret);
1477 #endif
1478             break;
1479         case EXCP_DEBUG:
1480             {
1481                 int sig;
1482
1483                 sig = gdb_handlesig(env, TARGET_SIGTRAP);
1484                 if (sig) {
1485                     info.si_signo = sig;
1486                     info.si_errno = 0;
1487                     info.si_code = TARGET_TRAP_BRKPT;
1488                     queue_signal(env, info.si_signo, &info);
1489                   }
1490             }
1491             break;
1492         case EXCP_INTERRUPT:
1493             /* just indicate that signals should be handled asap */
1494             break;
1495         default:
1496             cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
1497             break;
1498         }
1499         process_pending_signals(env);
1500     }
1501 }
1502 #endif
1503
1504 #ifdef TARGET_MIPS
1505
1506 #define MIPS_SYS(name, args) args,
1507
1508 static const uint8_t mips_syscall_args[] = {
1509         MIPS_SYS(sys_syscall    , 0)    /* 4000 */
1510         MIPS_SYS(sys_exit       , 1)
1511         MIPS_SYS(sys_fork       , 0)
1512         MIPS_SYS(sys_read       , 3)
1513         MIPS_SYS(sys_write      , 3)
1514         MIPS_SYS(sys_open       , 3)    /* 4005 */
1515         MIPS_SYS(sys_close      , 1)
1516         MIPS_SYS(sys_waitpid    , 3)
1517         MIPS_SYS(sys_creat      , 2)
1518         MIPS_SYS(sys_link       , 2)
1519         MIPS_SYS(sys_unlink     , 1)    /* 4010 */
1520         MIPS_SYS(sys_execve     , 0)
1521         MIPS_SYS(sys_chdir      , 1)
1522         MIPS_SYS(sys_time       , 1)
1523         MIPS_SYS(sys_mknod      , 3)
1524         MIPS_SYS(sys_chmod      , 2)    /* 4015 */
1525         MIPS_SYS(sys_lchown     , 3)
1526         MIPS_SYS(sys_ni_syscall , 0)
1527         MIPS_SYS(sys_ni_syscall , 0)    /* was sys_stat */
1528         MIPS_SYS(sys_lseek      , 3)
1529         MIPS_SYS(sys_getpid     , 0)    /* 4020 */
1530         MIPS_SYS(sys_mount      , 5)
1531         MIPS_SYS(sys_oldumount  , 1)
1532         MIPS_SYS(sys_setuid     , 1)
1533         MIPS_SYS(sys_getuid     , 0)
1534         MIPS_SYS(sys_stime      , 1)    /* 4025 */
1535         MIPS_SYS(sys_ptrace     , 4)
1536         MIPS_SYS(sys_alarm      , 1)
1537         MIPS_SYS(sys_ni_syscall , 0)    /* was sys_fstat */
1538         MIPS_SYS(sys_pause      , 0)
1539         MIPS_SYS(sys_utime      , 2)    /* 4030 */
1540         MIPS_SYS(sys_ni_syscall , 0)
1541         MIPS_SYS(sys_ni_syscall , 0)
1542         MIPS_SYS(sys_access     , 2)
1543         MIPS_SYS(sys_nice       , 1)
1544         MIPS_SYS(sys_ni_syscall , 0)    /* 4035 */
1545         MIPS_SYS(sys_sync       , 0)
1546         MIPS_SYS(sys_kill       , 2)
1547         MIPS_SYS(sys_rename     , 2)
1548         MIPS_SYS(sys_mkdir      , 2)
1549         MIPS_SYS(sys_rmdir      , 1)    /* 4040 */
1550         MIPS_SYS(sys_dup                , 1)
1551         MIPS_SYS(sys_pipe       , 0)
1552         MIPS_SYS(sys_times      , 1)
1553         MIPS_SYS(sys_ni_syscall , 0)
1554         MIPS_SYS(sys_brk                , 1)    /* 4045 */
1555         MIPS_SYS(sys_setgid     , 1)
1556         MIPS_SYS(sys_getgid     , 0)
1557         MIPS_SYS(sys_ni_syscall , 0)    /* was signal(2) */
1558         MIPS_SYS(sys_geteuid    , 0)
1559         MIPS_SYS(sys_getegid    , 0)    /* 4050 */
1560         MIPS_SYS(sys_acct       , 0)
1561         MIPS_SYS(sys_umount     , 2)
1562         MIPS_SYS(sys_ni_syscall , 0)
1563         MIPS_SYS(sys_ioctl      , 3)
1564         MIPS_SYS(sys_fcntl      , 3)    /* 4055 */
1565         MIPS_SYS(sys_ni_syscall , 2)
1566         MIPS_SYS(sys_setpgid    , 2)
1567         MIPS_SYS(sys_ni_syscall , 0)
1568         MIPS_SYS(sys_olduname   , 1)
1569         MIPS_SYS(sys_umask      , 1)    /* 4060 */
1570         MIPS_SYS(sys_chroot     , 1)
1571         MIPS_SYS(sys_ustat      , 2)
1572         MIPS_SYS(sys_dup2       , 2)
1573         MIPS_SYS(sys_getppid    , 0)
1574         MIPS_SYS(sys_getpgrp    , 0)    /* 4065 */
1575         MIPS_SYS(sys_setsid     , 0)
1576         MIPS_SYS(sys_sigaction  , 3)
1577         MIPS_SYS(sys_sgetmask   , 0)
1578         MIPS_SYS(sys_ssetmask   , 1)
1579         MIPS_SYS(sys_setreuid   , 2)    /* 4070 */
1580         MIPS_SYS(sys_setregid   , 2)
1581         MIPS_SYS(sys_sigsuspend , 0)
1582         MIPS_SYS(sys_sigpending , 1)
1583         MIPS_SYS(sys_sethostname        , 2)
1584         MIPS_SYS(sys_setrlimit  , 2)    /* 4075 */
1585         MIPS_SYS(sys_getrlimit  , 2)
1586         MIPS_SYS(sys_getrusage  , 2)
1587         MIPS_SYS(sys_gettimeofday, 2)
1588         MIPS_SYS(sys_settimeofday, 2)
1589         MIPS_SYS(sys_getgroups  , 2)    /* 4080 */
1590         MIPS_SYS(sys_setgroups  , 2)
1591         MIPS_SYS(sys_ni_syscall , 0)    /* old_select */
1592         MIPS_SYS(sys_symlink    , 2)
1593         MIPS_SYS(sys_ni_syscall , 0)    /* was sys_lstat */
1594         MIPS_SYS(sys_readlink   , 3)    /* 4085 */
1595         MIPS_SYS(sys_uselib     , 1)
1596         MIPS_SYS(sys_swapon     , 2)
1597         MIPS_SYS(sys_reboot     , 3)
1598         MIPS_SYS(old_readdir    , 3)
1599         MIPS_SYS(old_mmap       , 6)    /* 4090 */
1600         MIPS_SYS(sys_munmap     , 2)
1601         MIPS_SYS(sys_truncate   , 2)
1602         MIPS_SYS(sys_ftruncate  , 2)
1603         MIPS_SYS(sys_fchmod     , 2)
1604         MIPS_SYS(sys_fchown     , 3)    /* 4095 */
1605         MIPS_SYS(sys_getpriority        , 2)
1606         MIPS_SYS(sys_setpriority        , 3)
1607         MIPS_SYS(sys_ni_syscall , 0)
1608         MIPS_SYS(sys_statfs     , 2)
1609         MIPS_SYS(sys_fstatfs    , 2)    /* 4100 */
1610         MIPS_SYS(sys_ni_syscall , 0)    /* was ioperm(2) */
1611         MIPS_SYS(sys_socketcall , 2)
1612         MIPS_SYS(sys_syslog     , 3)
1613         MIPS_SYS(sys_setitimer  , 3)
1614         MIPS_SYS(sys_getitimer  , 2)    /* 4105 */
1615         MIPS_SYS(sys_newstat    , 2)
1616         MIPS_SYS(sys_newlstat   , 2)
1617         MIPS_SYS(sys_newfstat   , 2)
1618         MIPS_SYS(sys_uname      , 1)
1619         MIPS_SYS(sys_ni_syscall , 0)    /* 4110 was iopl(2) */
1620         MIPS_SYS(sys_vhangup    , 0)
1621         MIPS_SYS(sys_ni_syscall , 0)    /* was sys_idle() */
1622         MIPS_SYS(sys_ni_syscall , 0)    /* was sys_vm86 */
1623         MIPS_SYS(sys_wait4      , 4)
1624         MIPS_SYS(sys_swapoff    , 1)    /* 4115 */
1625         MIPS_SYS(sys_sysinfo    , 1)
1626         MIPS_SYS(sys_ipc                , 6)
1627         MIPS_SYS(sys_fsync      , 1)
1628         MIPS_SYS(sys_sigreturn  , 0)
1629         MIPS_SYS(sys_clone      , 0)    /* 4120 */
1630         MIPS_SYS(sys_setdomainname, 2)
1631         MIPS_SYS(sys_newuname   , 1)
1632         MIPS_SYS(sys_ni_syscall , 0)    /* sys_modify_ldt */
1633         MIPS_SYS(sys_adjtimex   , 1)
1634         MIPS_SYS(sys_mprotect   , 3)    /* 4125 */
1635         MIPS_SYS(sys_sigprocmask        , 3)
1636         MIPS_SYS(sys_ni_syscall , 0)    /* was create_module */
1637         MIPS_SYS(sys_init_module        , 5)
1638         MIPS_SYS(sys_delete_module, 1)
1639         MIPS_SYS(sys_ni_syscall , 0)    /* 4130 was get_kernel_syms */
1640         MIPS_SYS(sys_quotactl   , 0)
1641         MIPS_SYS(sys_getpgid    , 1)
1642         MIPS_SYS(sys_fchdir     , 1)
1643         MIPS_SYS(sys_bdflush    , 2)
1644         MIPS_SYS(sys_sysfs      , 3)    /* 4135 */
1645         MIPS_SYS(sys_personality        , 1)
1646         MIPS_SYS(sys_ni_syscall , 0)    /* for afs_syscall */
1647         MIPS_SYS(sys_setfsuid   , 1)
1648         MIPS_SYS(sys_setfsgid   , 1)
1649         MIPS_SYS(sys_llseek     , 5)    /* 4140 */
1650         MIPS_SYS(sys_getdents   , 3)
1651         MIPS_SYS(sys_select     , 5)
1652         MIPS_SYS(sys_flock      , 2)
1653         MIPS_SYS(sys_msync      , 3)
1654         MIPS_SYS(sys_readv      , 3)    /* 4145 */
1655         MIPS_SYS(sys_writev     , 3)
1656         MIPS_SYS(sys_cacheflush , 3)
1657         MIPS_SYS(sys_cachectl   , 3)
1658         MIPS_SYS(sys_sysmips    , 4)
1659         MIPS_SYS(sys_ni_syscall , 0)    /* 4150 */
1660         MIPS_SYS(sys_getsid     , 1)
1661         MIPS_SYS(sys_fdatasync  , 0)
1662         MIPS_SYS(sys_sysctl     , 1)
1663         MIPS_SYS(sys_mlock      , 2)
1664         MIPS_SYS(sys_munlock    , 2)    /* 4155 */
1665         MIPS_SYS(sys_mlockall   , 1)
1666         MIPS_SYS(sys_munlockall , 0)
1667         MIPS_SYS(sys_sched_setparam, 2)
1668         MIPS_SYS(sys_sched_getparam, 2)
1669         MIPS_SYS(sys_sched_setscheduler, 3)     /* 4160 */
1670         MIPS_SYS(sys_sched_getscheduler, 1)
1671         MIPS_SYS(sys_sched_yield        , 0)
1672         MIPS_SYS(sys_sched_get_priority_max, 1)
1673         MIPS_SYS(sys_sched_get_priority_min, 1)
1674         MIPS_SYS(sys_sched_rr_get_interval, 2)  /* 4165 */
1675         MIPS_SYS(sys_nanosleep, 2)
1676         MIPS_SYS(sys_mremap     , 4)
1677         MIPS_SYS(sys_accept     , 3)
1678         MIPS_SYS(sys_bind       , 3)
1679         MIPS_SYS(sys_connect    , 3)    /* 4170 */
1680         MIPS_SYS(sys_getpeername        , 3)
1681         MIPS_SYS(sys_getsockname        , 3)
1682         MIPS_SYS(sys_getsockopt , 5)
1683         MIPS_SYS(sys_listen     , 2)
1684         MIPS_SYS(sys_recv       , 4)    /* 4175 */
1685         MIPS_SYS(sys_recvfrom   , 6)
1686         MIPS_SYS(sys_recvmsg    , 3)
1687         MIPS_SYS(sys_send       , 4)
1688         MIPS_SYS(sys_sendmsg    , 3)
1689         MIPS_SYS(sys_sendto     , 6)    /* 4180 */
1690         MIPS_SYS(sys_setsockopt , 5)
1691         MIPS_SYS(sys_shutdown   , 2)
1692         MIPS_SYS(sys_socket     , 3)
1693         MIPS_SYS(sys_socketpair , 4)
1694         MIPS_SYS(sys_setresuid  , 3)    /* 4185 */
1695         MIPS_SYS(sys_getresuid  , 3)
1696         MIPS_SYS(sys_ni_syscall , 0)    /* was sys_query_module */
1697         MIPS_SYS(sys_poll       , 3)
1698         MIPS_SYS(sys_nfsservctl , 3)
1699         MIPS_SYS(sys_setresgid  , 3)    /* 4190 */
1700         MIPS_SYS(sys_getresgid  , 3)
1701         MIPS_SYS(sys_prctl      , 5)
1702         MIPS_SYS(sys_rt_sigreturn, 0)
1703         MIPS_SYS(sys_rt_sigaction, 4)
1704         MIPS_SYS(sys_rt_sigprocmask, 4) /* 4195 */
1705         MIPS_SYS(sys_rt_sigpending, 2)
1706         MIPS_SYS(sys_rt_sigtimedwait, 4)
1707         MIPS_SYS(sys_rt_sigqueueinfo, 3)
1708         MIPS_SYS(sys_rt_sigsuspend, 0)
1709         MIPS_SYS(sys_pread64    , 6)    /* 4200 */
1710         MIPS_SYS(sys_pwrite64   , 6)
1711         MIPS_SYS(sys_chown      , 3)
1712         MIPS_SYS(sys_getcwd     , 2)
1713         MIPS_SYS(sys_capget     , 2)
1714         MIPS_SYS(sys_capset     , 2)    /* 4205 */
1715         MIPS_SYS(sys_sigaltstack        , 0)
1716         MIPS_SYS(sys_sendfile   , 4)
1717         MIPS_SYS(sys_ni_syscall , 0)
1718         MIPS_SYS(sys_ni_syscall , 0)
1719         MIPS_SYS(sys_mmap2      , 6)    /* 4210 */
1720         MIPS_SYS(sys_truncate64 , 4)
1721         MIPS_SYS(sys_ftruncate64        , 4)
1722         MIPS_SYS(sys_stat64     , 2)
1723         MIPS_SYS(sys_lstat64    , 2)
1724         MIPS_SYS(sys_fstat64    , 2)    /* 4215 */
1725         MIPS_SYS(sys_pivot_root , 2)
1726         MIPS_SYS(sys_mincore    , 3)
1727         MIPS_SYS(sys_madvise    , 3)
1728         MIPS_SYS(sys_getdents64 , 3)
1729         MIPS_SYS(sys_fcntl64    , 3)    /* 4220 */
1730         MIPS_SYS(sys_ni_syscall , 0)
1731         MIPS_SYS(sys_gettid     , 0)
1732         MIPS_SYS(sys_readahead  , 5)
1733         MIPS_SYS(sys_setxattr   , 5)
1734         MIPS_SYS(sys_lsetxattr  , 5)    /* 4225 */
1735         MIPS_SYS(sys_fsetxattr  , 5)
1736         MIPS_SYS(sys_getxattr   , 4)
1737         MIPS_SYS(sys_lgetxattr  , 4)
1738         MIPS_SYS(sys_fgetxattr  , 4)
1739         MIPS_SYS(sys_listxattr  , 3)    /* 4230 */
1740         MIPS_SYS(sys_llistxattr , 3)
1741         MIPS_SYS(sys_flistxattr , 3)
1742         MIPS_SYS(sys_removexattr        , 2)
1743         MIPS_SYS(sys_lremovexattr, 2)
1744         MIPS_SYS(sys_fremovexattr, 2)   /* 4235 */
1745         MIPS_SYS(sys_tkill      , 2)
1746         MIPS_SYS(sys_sendfile64 , 5)
1747         MIPS_SYS(sys_futex      , 2)
1748         MIPS_SYS(sys_sched_setaffinity, 3)
1749         MIPS_SYS(sys_sched_getaffinity, 3)      /* 4240 */
1750         MIPS_SYS(sys_io_setup   , 2)
1751         MIPS_SYS(sys_io_destroy , 1)
1752         MIPS_SYS(sys_io_getevents, 5)
1753         MIPS_SYS(sys_io_submit  , 3)
1754         MIPS_SYS(sys_io_cancel  , 3)    /* 4245 */
1755         MIPS_SYS(sys_exit_group , 1)
1756         MIPS_SYS(sys_lookup_dcookie, 3)
1757         MIPS_SYS(sys_epoll_create, 1)
1758         MIPS_SYS(sys_epoll_ctl  , 4)
1759         MIPS_SYS(sys_epoll_wait , 3)    /* 4250 */
1760         MIPS_SYS(sys_remap_file_pages, 5)
1761         MIPS_SYS(sys_set_tid_address, 1)
1762         MIPS_SYS(sys_restart_syscall, 0)
1763         MIPS_SYS(sys_fadvise64_64, 7)
1764         MIPS_SYS(sys_statfs64   , 3)    /* 4255 */
1765         MIPS_SYS(sys_fstatfs64  , 2)
1766         MIPS_SYS(sys_timer_create, 3)
1767         MIPS_SYS(sys_timer_settime, 4)
1768         MIPS_SYS(sys_timer_gettime, 2)
1769         MIPS_SYS(sys_timer_getoverrun, 1)       /* 4260 */
1770         MIPS_SYS(sys_timer_delete, 1)
1771         MIPS_SYS(sys_clock_settime, 2)
1772         MIPS_SYS(sys_clock_gettime, 2)
1773         MIPS_SYS(sys_clock_getres, 2)
1774         MIPS_SYS(sys_clock_nanosleep, 4)        /* 4265 */
1775         MIPS_SYS(sys_tgkill     , 3)
1776         MIPS_SYS(sys_utimes     , 2)
1777         MIPS_SYS(sys_mbind      , 4)
1778         MIPS_SYS(sys_ni_syscall , 0)    /* sys_get_mempolicy */
1779         MIPS_SYS(sys_ni_syscall , 0)    /* 4270 sys_set_mempolicy */
1780         MIPS_SYS(sys_mq_open    , 4)
1781         MIPS_SYS(sys_mq_unlink  , 1)
1782         MIPS_SYS(sys_mq_timedsend, 5)
1783         MIPS_SYS(sys_mq_timedreceive, 5)
1784         MIPS_SYS(sys_mq_notify  , 2)    /* 4275 */
1785         MIPS_SYS(sys_mq_getsetattr, 3)
1786         MIPS_SYS(sys_ni_syscall , 0)    /* sys_vserver */
1787         MIPS_SYS(sys_waitid     , 4)
1788         MIPS_SYS(sys_ni_syscall , 0)    /* available, was setaltroot */
1789         MIPS_SYS(sys_add_key    , 5)
1790         MIPS_SYS(sys_request_key, 4)
1791         MIPS_SYS(sys_keyctl     , 5)
1792         MIPS_SYS(sys_set_thread_area, 1)
1793         MIPS_SYS(sys_inotify_init, 0)
1794         MIPS_SYS(sys_inotify_add_watch, 3) /* 4285 */
1795         MIPS_SYS(sys_inotify_rm_watch, 2)
1796         MIPS_SYS(sys_migrate_pages, 4)
1797         MIPS_SYS(sys_openat, 4)
1798         MIPS_SYS(sys_mkdirat, 3)
1799         MIPS_SYS(sys_mknodat, 4)        /* 4290 */
1800         MIPS_SYS(sys_fchownat, 5)
1801         MIPS_SYS(sys_futimesat, 3)
1802         MIPS_SYS(sys_fstatat64, 4)
1803         MIPS_SYS(sys_unlinkat, 3)
1804         MIPS_SYS(sys_renameat, 4)       /* 4295 */
1805         MIPS_SYS(sys_linkat, 5)
1806         MIPS_SYS(sys_symlinkat, 3)
1807         MIPS_SYS(sys_readlinkat, 4)
1808         MIPS_SYS(sys_fchmodat, 3)
1809         MIPS_SYS(sys_faccessat, 3)      /* 4300 */
1810         MIPS_SYS(sys_pselect6, 6)
1811         MIPS_SYS(sys_ppoll, 5)
1812         MIPS_SYS(sys_unshare, 1)
1813         MIPS_SYS(sys_splice, 4)
1814         MIPS_SYS(sys_sync_file_range, 7) /* 4305 */
1815         MIPS_SYS(sys_tee, 4)
1816         MIPS_SYS(sys_vmsplice, 4)
1817         MIPS_SYS(sys_move_pages, 6)
1818         MIPS_SYS(sys_set_robust_list, 2)
1819         MIPS_SYS(sys_get_robust_list, 3) /* 4310 */
1820         MIPS_SYS(sys_kexec_load, 4)
1821         MIPS_SYS(sys_getcpu, 3)
1822         MIPS_SYS(sys_epoll_pwait, 6)
1823         MIPS_SYS(sys_ioprio_set, 3)
1824         MIPS_SYS(sys_ioprio_get, 2)
1825 };
1826
1827 #undef MIPS_SYS
1828
1829 void cpu_loop(CPUMIPSState *env)
1830 {
1831     target_siginfo_t info;
1832     int trapnr, ret;
1833     unsigned int syscall_num;
1834
1835     for(;;) {
1836         trapnr = cpu_mips_exec(env);
1837         switch(trapnr) {
1838         case EXCP_SYSCALL:
1839             syscall_num = env->active_tc.gpr[2] - 4000;
1840             env->active_tc.PC += 4;
1841             if (syscall_num >= sizeof(mips_syscall_args)) {
1842                 ret = -ENOSYS;
1843             } else {
1844                 int nb_args;
1845                 abi_ulong sp_reg;
1846                 abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
1847
1848                 nb_args = mips_syscall_args[syscall_num];
1849                 sp_reg = env->active_tc.gpr[29];
1850                 switch (nb_args) {
1851                 /* these arguments are taken from the stack */
1852                 /* FIXME - what to do if get_user() fails? */
1853                 case 8: get_user_ual(arg8, sp_reg + 28);
1854                 case 7: get_user_ual(arg7, sp_reg + 24);
1855                 case 6: get_user_ual(arg6, sp_reg + 20);
1856                 case 5: get_user_ual(arg5, sp_reg + 16);
1857                 default:
1858                     break;
1859                 }
1860                 ret = do_syscall(env, env->active_tc.gpr[2],
1861                                  env->active_tc.gpr[4],
1862                                  env->active_tc.gpr[5],
1863                                  env->active_tc.gpr[6],
1864                                  env->active_tc.gpr[7],
1865                                  arg5, arg6/*, arg7, arg8*/);
1866             }
1867             if ((unsigned int)ret >= (unsigned int)(-1133)) {
1868                 env->active_tc.gpr[7] = 1; /* error flag */
1869                 ret = -ret;
1870             } else {
1871                 env->active_tc.gpr[7] = 0; /* error flag */
1872             }
1873             env->active_tc.gpr[2] = ret;
1874             break;
1875         case EXCP_TLBL:
1876         case EXCP_TLBS:
1877         case EXCP_CpU:
1878         case EXCP_RI:
1879             info.si_signo = TARGET_SIGILL;
1880             info.si_errno = 0;
1881             info.si_code = 0;
1882             queue_signal(env, info.si_signo, &info);
1883             break;
1884         case EXCP_INTERRUPT:
1885             /* just indicate that signals should be handled asap */
1886             break;
1887         case EXCP_DEBUG:
1888             {
1889                 int sig;
1890
1891                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1892                 if (sig)
1893                   {
1894                     info.si_signo = sig;
1895                     info.si_errno = 0;
1896                     info.si_code = TARGET_TRAP_BRKPT;
1897                     queue_signal(env, info.si_signo, &info);
1898                   }
1899             }
1900             break;
1901         default:
1902             //        error:
1903             fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
1904                     trapnr);
1905             cpu_dump_state(env, stderr, fprintf, 0);
1906             abort();
1907         }
1908         process_pending_signals(env);
1909     }
1910 }
1911 #endif
1912
1913 #ifdef TARGET_SH4
1914 void cpu_loop (CPUState *env)
1915 {
1916     int trapnr, ret;
1917     target_siginfo_t info;
1918
1919     while (1) {
1920         trapnr = cpu_sh4_exec (env);
1921
1922         switch (trapnr) {
1923         case 0x160:
1924             env->pc += 2;
1925             ret = do_syscall(env,
1926                              env->gregs[3],
1927                              env->gregs[4],
1928                              env->gregs[5],
1929                              env->gregs[6],
1930                              env->gregs[7],
1931                              env->gregs[0],
1932                              env->gregs[1]);
1933             env->gregs[0] = ret;
1934             break;
1935         case EXCP_INTERRUPT:
1936             /* just indicate that signals should be handled asap */
1937             break;
1938         case EXCP_DEBUG:
1939             {
1940                 int sig;
1941
1942                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1943                 if (sig)
1944                   {
1945                     info.si_signo = sig;
1946                     info.si_errno = 0;
1947                     info.si_code = TARGET_TRAP_BRKPT;
1948                     queue_signal(env, info.si_signo, &info);
1949                   }
1950             }
1951             break;
1952         case 0xa0:
1953         case 0xc0:
1954             info.si_signo = SIGSEGV;
1955             info.si_errno = 0;
1956             info.si_code = TARGET_SEGV_MAPERR;
1957             info._sifields._sigfault._addr = env->tea;
1958             queue_signal(env, info.si_signo, &info);
1959             break;
1960
1961         default:
1962             printf ("Unhandled trap: 0x%x\n", trapnr);
1963             cpu_dump_state(env, stderr, fprintf, 0);
1964             _exit (1);
1965         }
1966         process_pending_signals (env);
1967     }
1968 }
1969 #endif
1970
1971 #ifdef TARGET_CRIS
1972 void cpu_loop (CPUState *env)
1973 {
1974     int trapnr, ret;
1975     target_siginfo_t info;
1976     
1977     while (1) {
1978         trapnr = cpu_cris_exec (env);
1979         switch (trapnr) {
1980         case 0xaa:
1981             {
1982                 info.si_signo = SIGSEGV;
1983                 info.si_errno = 0;
1984                 /* XXX: check env->error_code */
1985                 info.si_code = TARGET_SEGV_MAPERR;
1986                 info._sifields._sigfault._addr = env->pregs[PR_EDA];
1987                 queue_signal(env, info.si_signo, &info);
1988             }
1989             break;
1990         case EXCP_INTERRUPT:
1991           /* just indicate that signals should be handled asap */
1992           break;
1993         case EXCP_BREAK:
1994             ret = do_syscall(env, 
1995                              env->regs[9], 
1996                              env->regs[10], 
1997                              env->regs[11], 
1998                              env->regs[12], 
1999                              env->regs[13], 
2000                              env->pregs[7], 
2001                              env->pregs[11]);
2002             env->regs[10] = ret;
2003             break;
2004         case EXCP_DEBUG:
2005             {
2006                 int sig;
2007
2008                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2009                 if (sig)
2010                   {
2011                     info.si_signo = sig;
2012                     info.si_errno = 0;
2013                     info.si_code = TARGET_TRAP_BRKPT;
2014                     queue_signal(env, info.si_signo, &info);
2015                   }
2016             }
2017             break;
2018         default:
2019             printf ("Unhandled trap: 0x%x\n", trapnr);
2020             cpu_dump_state(env, stderr, fprintf, 0);
2021             exit (1);
2022         }
2023         process_pending_signals (env);
2024     }
2025 }
2026 #endif
2027
2028 #ifdef TARGET_M68K
2029
2030 void cpu_loop(CPUM68KState *env)
2031 {
2032     int trapnr;
2033     unsigned int n;
2034     target_siginfo_t info;
2035     TaskState *ts = env->opaque;
2036
2037     for(;;) {
2038         trapnr = cpu_m68k_exec(env);
2039         switch(trapnr) {
2040         case EXCP_ILLEGAL:
2041             {
2042                 if (ts->sim_syscalls) {
2043                     uint16_t nr;
2044                     nr = lduw(env->pc + 2);
2045                     env->pc += 4;
2046                     do_m68k_simcall(env, nr);
2047                 } else {
2048                     goto do_sigill;
2049                 }
2050             }
2051             break;
2052         case EXCP_HALT_INSN:
2053             /* Semihosing syscall.  */
2054             env->pc += 4;
2055             do_m68k_semihosting(env, env->dregs[0]);
2056             break;
2057         case EXCP_LINEA:
2058         case EXCP_LINEF:
2059         case EXCP_UNSUPPORTED:
2060         do_sigill:
2061             info.si_signo = SIGILL;
2062             info.si_errno = 0;
2063             info.si_code = TARGET_ILL_ILLOPN;
2064             info._sifields._sigfault._addr = env->pc;
2065             queue_signal(env, info.si_signo, &info);
2066             break;
2067         case EXCP_TRAP0:
2068             {
2069                 ts->sim_syscalls = 0;
2070                 n = env->dregs[0];
2071                 env->pc += 2;
2072                 env->dregs[0] = do_syscall(env,
2073                                           n,
2074                                           env->dregs[1],
2075                                           env->dregs[2],
2076                                           env->dregs[3],
2077                                           env->dregs[4],
2078                                           env->dregs[5],
2079                                           env->aregs[0]);
2080             }
2081             break;
2082         case EXCP_INTERRUPT:
2083             /* just indicate that signals should be handled asap */
2084             break;
2085         case EXCP_ACCESS:
2086             {
2087                 info.si_signo = SIGSEGV;
2088                 info.si_errno = 0;
2089                 /* XXX: check env->error_code */
2090                 info.si_code = TARGET_SEGV_MAPERR;
2091                 info._sifields._sigfault._addr = env->mmu.ar;
2092                 queue_signal(env, info.si_signo, &info);
2093             }
2094             break;
2095         case EXCP_DEBUG:
2096             {
2097                 int sig;
2098
2099                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2100                 if (sig)
2101                   {
2102                     info.si_signo = sig;
2103                     info.si_errno = 0;
2104                     info.si_code = TARGET_TRAP_BRKPT;
2105                     queue_signal(env, info.si_signo, &info);
2106                   }
2107             }
2108             break;
2109         default:
2110             fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
2111                     trapnr);
2112             cpu_dump_state(env, stderr, fprintf, 0);
2113             abort();
2114         }
2115         process_pending_signals(env);
2116     }
2117 }
2118 #endif /* TARGET_M68K */
2119
2120 #ifdef TARGET_ALPHA
2121 void cpu_loop (CPUState *env)
2122 {
2123     int trapnr;
2124     target_siginfo_t info;
2125
2126     while (1) {
2127         trapnr = cpu_alpha_exec (env);
2128
2129         switch (trapnr) {
2130         case EXCP_RESET:
2131             fprintf(stderr, "Reset requested. Exit\n");
2132             exit(1);
2133             break;
2134         case EXCP_MCHK:
2135             fprintf(stderr, "Machine check exception. Exit\n");
2136             exit(1);
2137             break;
2138         case EXCP_ARITH:
2139             fprintf(stderr, "Arithmetic trap.\n");
2140             exit(1);
2141             break;
2142         case EXCP_HW_INTERRUPT:
2143             fprintf(stderr, "External interrupt. Exit\n");
2144             exit(1);
2145             break;
2146         case EXCP_DFAULT:
2147             fprintf(stderr, "MMU data fault\n");
2148             exit(1);
2149             break;
2150         case EXCP_DTB_MISS_PAL:
2151             fprintf(stderr, "MMU data TLB miss in PALcode\n");
2152             exit(1);
2153             break;
2154         case EXCP_ITB_MISS:
2155             fprintf(stderr, "MMU instruction TLB miss\n");
2156             exit(1);
2157             break;
2158         case EXCP_ITB_ACV:
2159             fprintf(stderr, "MMU instruction access violation\n");
2160             exit(1);
2161             break;
2162         case EXCP_DTB_MISS_NATIVE:
2163             fprintf(stderr, "MMU data TLB miss\n");
2164             exit(1);
2165             break;
2166         case EXCP_UNALIGN:
2167             fprintf(stderr, "Unaligned access\n");
2168             exit(1);
2169             break;
2170         case EXCP_OPCDEC:
2171             fprintf(stderr, "Invalid instruction\n");
2172             exit(1);
2173             break;
2174         case EXCP_FEN:
2175             fprintf(stderr, "Floating-point not allowed\n");
2176             exit(1);
2177             break;
2178         case EXCP_CALL_PAL ... (EXCP_CALL_PALP - 1):
2179             call_pal(env, (trapnr >> 6) | 0x80);
2180             break;
2181         case EXCP_CALL_PALP ... (EXCP_CALL_PALE - 1):
2182             fprintf(stderr, "Privileged call to PALcode\n");
2183             exit(1);
2184             break;
2185         case EXCP_DEBUG:
2186             {
2187                 int sig;
2188
2189                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2190                 if (sig)
2191                   {
2192                     info.si_signo = sig;
2193                     info.si_errno = 0;
2194                     info.si_code = TARGET_TRAP_BRKPT;
2195                     queue_signal(env, info.si_signo, &info);
2196                   }
2197             }
2198             break;
2199         default:
2200             printf ("Unhandled trap: 0x%x\n", trapnr);
2201             cpu_dump_state(env, stderr, fprintf, 0);
2202             exit (1);
2203         }
2204         process_pending_signals (env);
2205     }
2206 }
2207 #endif /* TARGET_ALPHA */
2208
2209 static void usage(void)
2210 {
2211     printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
2212            "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
2213            "Linux CPU emulator (compiled for %s emulation)\n"
2214            "\n"
2215            "Standard options:\n"
2216            "-h                print this help\n"
2217            "-g port           wait gdb connection to port\n"
2218            "-L path           set the elf interpreter prefix (default=%s)\n"
2219            "-s size           set the stack size in bytes (default=%ld)\n"
2220            "-cpu model        select CPU (-cpu ? for list)\n"
2221            "-drop-ld-preload  drop LD_PRELOAD for target process\n"
2222            "-E var=value      sets/modifies targets environment variable(s)\n"
2223            "-U var            unsets targets environment variable(s)\n"
2224            "-0 argv0          forces target process argv[0] to be argv0\n"
2225 #if defined(CONFIG_USE_GUEST_BASE)
2226            "-B address        set guest_base address to address\n"
2227 #endif
2228            "\n"
2229            "Debug options:\n"
2230            "-d options   activate log (logfile=%s)\n"
2231            "-p pagesize  set the host page size to 'pagesize'\n"
2232            "-singlestep  always run in singlestep mode\n"
2233            "-strace      log system calls\n"
2234            "\n"
2235            "Environment variables:\n"
2236            "QEMU_STRACE       Print system calls and arguments similar to the\n"
2237            "                  'strace' program.  Enable by setting to any value.\n"
2238            "\n"
2239            "You can use -E and -U options to set/unset environment variables\n"
2240            "for target process.  It is possible to provide several variables\n"
2241            "by repeating the option.  For example:\n"
2242            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
2243            "Note that if you provide several changes to single variable\n"
2244            "last change will stay in effect.\n"
2245 #if defined(CONFIG_USE_GUEST_BASE)
2246            "\n"
2247            "You can use -B option to load target binary into different\n"
2248            "address that is specified in elf headers.  This can be useful\n"
2249            "when target binary would be loaded to low addresses and\n"
2250            "/proc/sys/vm/mmap_min_addr is set to higher.  For example\n"
2251            "     qemu-" TARGET_ARCH " -B 0x100000 ...\n"
2252            "loads target binary starting from the first meg.\n"
2253 #endif
2254            ,
2255            TARGET_ARCH,
2256            interp_prefix,
2257            x86_stack_size,
2258            DEBUG_LOGFILE);
2259     exit(1);
2260 }
2261
2262 THREAD CPUState *thread_env;
2263
2264 void task_settid(TaskState *ts)
2265 {
2266     if (ts->ts_tid == 0) {
2267 #ifdef USE_NPTL
2268         ts->ts_tid = (pid_t)syscall(SYS_gettid);
2269 #else
2270         /* when no threads are used, tid becomes pid */
2271         ts->ts_tid = getpid();
2272 #endif
2273     }
2274 }
2275
2276 void stop_all_tasks(void)
2277 {
2278     /*
2279      * We trust that when using NPTL, start_exclusive()
2280      * handles thread stopping correctly.  Note that there
2281      * is no way out of this state as we don't provide
2282      * any suspend routine.
2283      */
2284     start_exclusive();
2285 }
2286
2287 /* Assumes contents are already zeroed.  */
2288 void init_task_state(TaskState *ts)
2289 {
2290     int i;
2291  
2292     ts->used = 1;
2293     ts->first_free = ts->sigqueue_table;
2294     for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
2295         ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
2296     }
2297     ts->sigqueue_table[i].next = NULL;
2298 }
2299  
2300 int main(int argc, char **argv, char **envp)
2301 {
2302     const char *filename = NULL;
2303     const char *cpu_model;
2304     struct target_pt_regs regs1, *regs = &regs1;
2305     struct image_info info1, *info = &info1;
2306     struct linux_binprm bprm;
2307     TaskState ts1, *ts = &ts1;
2308     CPUState *env;
2309     int optind;
2310     const char *r;
2311     int gdbstub_port = 0;
2312     char **target_environ, **wrk;
2313     char **target_argv;
2314     int target_argc;
2315     int drop_ld_preload = 0;
2316     envlist_t *envlist = NULL;
2317     const char *argv0 = NULL;
2318     int argskip = 0;
2319     int i;
2320
2321     if (argc <= 1)
2322         usage();
2323
2324     qemu_cache_utils_init(envp);
2325
2326     /* init debug */
2327     cpu_set_log_filename(DEBUG_LOGFILE);
2328
2329     if ((envlist = envlist_create()) == NULL) {
2330         (void) fprintf(stderr, "Unable to allocate envlist\n");
2331         exit(1);
2332     }
2333
2334     /* add current environment into the list */
2335     for (wrk = environ; *wrk != NULL; wrk++) {
2336         (void) envlist_setenv(envlist, *wrk);
2337     }
2338
2339     cpu_model = NULL;
2340     optind = 1;
2341     for(;;) {
2342         if (optind >= argc)
2343             break;
2344         r = argv[optind];
2345         if (r[0] != '-')
2346             break;
2347         optind++;
2348         r++;
2349         if (!strcmp(r, "-")) {
2350             break;
2351         } else if (!strcmp(r, "d")) {
2352             int mask;
2353             const CPULogItem *item;
2354
2355             if (optind >= argc)
2356                 break;
2357
2358             r = argv[optind++];
2359             mask = cpu_str_to_log_mask(r);
2360             if (!mask) {
2361                 printf("Log items (comma separated):\n");
2362                 for(item = cpu_log_items; item->mask != 0; item++) {
2363                     printf("%-10s %s\n", item->name, item->help);
2364                 }
2365                 _exit(1);
2366             }
2367             cpu_set_log(mask);
2368         } else if (!strcmp(r, "E")) {
2369             r = argv[optind++];
2370             if (envlist_setenv(envlist, r) != 0)
2371                 usage();
2372         } else if (!strcmp(r, "U")) {
2373             r = argv[optind++];
2374             if (envlist_unsetenv(envlist, r) != 0)
2375                 usage();
2376         } else if (!strcmp(r, "0")) {
2377             r = argv[optind++];
2378             argv0 = r;
2379         } else if (!strcmp(r,"-sbox-call")) {
2380             argskip++;
2381         } else if (!strcmp(r, "s")) {
2382             if (optind >= argc)
2383                 break;
2384             r = argv[optind++];
2385             x86_stack_size = strtol(r, (char **)&r, 0);
2386             if (x86_stack_size <= 0)
2387                 usage();
2388             if (*r == 'M')
2389                 x86_stack_size *= 1024 * 1024;
2390             else if (*r == 'k' || *r == 'K')
2391                 x86_stack_size *= 1024;
2392         } else if (!strcmp(r, "L")) {
2393             interp_prefix = argv[optind++];
2394         } else if (!strcmp(r, "p")) {
2395             if (optind >= argc)
2396                 break;
2397             qemu_host_page_size = atoi(argv[optind++]);
2398             if (qemu_host_page_size == 0 ||
2399                 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
2400                 fprintf(stderr, "page size must be a power of two\n");
2401                 _exit(1);
2402             }
2403         } else if (!strcmp(r, "g")) {
2404             if (optind >= argc)
2405                 break;
2406             gdbstub_port = atoi(argv[optind++]);
2407         } else if (!strcmp(r, "r")) {
2408             qemu_uname_release = argv[optind++];
2409         } else if (!strcmp(r, "cpu")) {
2410             cpu_model = argv[optind++];
2411             if (cpu_model == NULL || strcmp(cpu_model, "?") == 0) {
2412 /* XXX: implement xxx_cpu_list for targets that still miss it */
2413 #if defined(cpu_list)
2414                     cpu_list(stdout, &fprintf);
2415 #endif
2416                 exit(1);
2417             }
2418 #if defined(CONFIG_USE_GUEST_BASE)
2419         } else if (!strcmp(r, "B")) {
2420            guest_base = strtol(argv[optind++], NULL, 0);
2421 #endif
2422         } else if (!strcmp(r, "drop-ld-preload")) {
2423             drop_ld_preload = 1;
2424         } else if (!strcmp(r, "keep-ld-preload")) {
2425             drop_ld_preload = 0;
2426         } else if (!strcmp(r, "singlestep")) {
2427             singlestep = 1;
2428         } else if (!strcmp(r, "strace")) {
2429             do_strace = 1;
2430         } else
2431         {
2432             usage();
2433         }
2434     }
2435     if (optind >= argc)
2436         usage();
2437     if (filename == NULL) {
2438         filename = argv[optind];
2439         exec_path = argv[optind];
2440     } else {
2441         argv0 = argv[optind];
2442     }
2443     if (drop_ld_preload) {
2444         (void) envlist_unsetenv(envlist, "LD_PRELOAD");
2445     }
2446
2447     /* Zero out regs */
2448     memset(regs, 0, sizeof(struct target_pt_regs));
2449
2450     /* Zero out image_info */
2451     memset(info, 0, sizeof(struct image_info));
2452
2453     memset(&bprm, 0, sizeof (bprm));
2454
2455     /* Scan interp_prefix dir for replacement files. */
2456     init_paths(interp_prefix);
2457
2458     if (cpu_model == NULL) {
2459 #if defined(TARGET_I386)
2460 #ifdef TARGET_X86_64
2461         cpu_model = "qemu64";
2462 #else
2463         cpu_model = "qemu32";
2464 #endif
2465 #elif defined(TARGET_ARM)
2466         cpu_model = "any";
2467 #elif defined(TARGET_M68K)
2468         cpu_model = "any";
2469 #elif defined(TARGET_SPARC)
2470 #ifdef TARGET_SPARC64
2471         cpu_model = "TI UltraSparc II";
2472 #else
2473         cpu_model = "Fujitsu MB86904";
2474 #endif
2475 #elif defined(TARGET_MIPS)
2476 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
2477         cpu_model = "20Kc";
2478 #else
2479         cpu_model = "24Kf";
2480 #endif
2481 #elif defined(TARGET_PPC)
2482 #ifdef TARGET_PPC64
2483         cpu_model = "970";
2484 #else
2485         cpu_model = "750";
2486 #endif
2487 #else
2488         cpu_model = "any";
2489 #endif
2490     }
2491     cpu_exec_init_all(0);
2492     /* NOTE: we need to init the CPU at this stage to get
2493        qemu_host_page_size */
2494     env = cpu_init(cpu_model);
2495     if (!env) {
2496         fprintf(stderr, "Unable to find CPU definition\n");
2497         exit(1);
2498     }
2499     thread_env = env;
2500
2501     if (getenv("QEMU_STRACE")) {
2502         do_strace = 1;
2503     }
2504
2505     target_environ = envlist_to_environ(envlist, NULL);
2506     envlist_free(envlist);
2507
2508 #if defined(CONFIG_USE_GUEST_BASE)
2509     /*
2510      * Now that page sizes are configured in cpu_init() we can do
2511      * proper page alignment for guest_base.
2512      */
2513     guest_base = HOST_PAGE_ALIGN(guest_base);
2514
2515     /*
2516      * Read in mmap_min_addr kernel parameter and check
2517      * whether it is set to some value > 0.  This value is used
2518      * later on when doing mmap(2)s to calculate where guest_base
2519      * is to set, if needed.
2520      *
2521      * When user has explicitly set the quest base, we skip this
2522      * test.
2523      */
2524     if (guest_base == 0) {
2525         FILE *fp;
2526
2527         if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
2528             unsigned long tmp;
2529             if (fscanf(fp, "%lu", &tmp) == 1) {
2530                 mmap_min_addr = tmp;
2531                 qemu_log("kernel mmap_min_addr=%lu\n", mmap_min_addr);
2532             }
2533             fclose(fp);
2534         }
2535     }
2536 #endif /* CONFIG_USE_GUEST_BASE */
2537
2538     /*
2539      * Prepare copy of argv vector for target.
2540      */
2541     target_argc = argc - optind;
2542     target_argv = calloc(target_argc + 1, sizeof (char *));
2543     if (target_argv == NULL) {
2544         (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
2545         exit(1);
2546     }
2547
2548     /*
2549      * If argv0 is specified (using '-0' switch) we replace
2550      * argv[0] pointer with the given one.
2551      */
2552     i = 0;
2553     if (argv0 != NULL) {
2554         target_argv[i++] = strdup(argv0);
2555     }
2556     for (; i < target_argc; i++) {
2557         target_argv[i] = strdup(argv[optind + i]);
2558     }
2559     target_argv[target_argc] = NULL;
2560
2561     memset(ts, 0, sizeof(TaskState));
2562     init_task_state(ts);
2563     /* build Task State */
2564     env->opaque = ts;
2565     ts->bprm = &bprm;
2566     ts->info = info;
2567     task_settid(ts);
2568
2569     if (loader_exec(filename, target_argv+argskip, target_environ, regs,
2570         info, &bprm) != 0) {
2571         printf("Error loading %s\n", filename);
2572         _exit(1);
2573     }
2574
2575     for (i = 0; i < target_argc; i++) {
2576         free(target_argv[i]);
2577     }
2578     free(target_argv);
2579
2580     for (wrk = target_environ; *wrk; wrk++) {
2581         free(*wrk);
2582     }
2583
2584     free(target_environ);
2585
2586     if (qemu_log_enabled()) {
2587 #if defined(CONFIG_USE_GUEST_BASE)
2588         if (guest_base > 0) {
2589             qemu_log("guest_base is set to 0x%lx\n", guest_base);
2590             qemu_log(
2591                 "==========================================================\n"
2592                 "Note that all target addresses below are given in target\n"
2593                 "address space which is different from host by guest_base.\n"
2594                 "For example: target address 0x%x becomes 0x%x and so on.\n"
2595                 "==========================================================\n",
2596                 (uintptr_t)0x8000, (uintptr_t)g2h(0x8000));
2597         }
2598 #endif
2599         log_page_dump();
2600
2601         qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
2602         qemu_log("end_code    0x" TARGET_ABI_FMT_lx "\n", info->end_code);
2603         qemu_log("start_code  0x" TARGET_ABI_FMT_lx "\n",
2604                  info->start_code);
2605         qemu_log("start_data  0x" TARGET_ABI_FMT_lx "\n",
2606                  info->start_data);
2607         qemu_log("end_data    0x" TARGET_ABI_FMT_lx "\n", info->end_data);
2608         qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
2609                  info->start_stack);
2610         qemu_log("brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
2611         qemu_log("entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
2612     }
2613
2614     target_set_brk(info->brk);
2615     syscall_init();
2616     signal_init();
2617
2618 #if defined(TARGET_I386)
2619     cpu_x86_set_cpl(env, 3);
2620
2621     env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
2622     env->hflags |= HF_PE_MASK;
2623     if (env->cpuid_features & CPUID_SSE) {
2624         env->cr[4] |= CR4_OSFXSR_MASK;
2625         env->hflags |= HF_OSFXSR_MASK;
2626     }
2627 #ifndef TARGET_ABI32
2628     /* enable 64 bit mode if possible */
2629     if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
2630         fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
2631         exit(1);
2632     }
2633     env->cr[4] |= CR4_PAE_MASK;
2634     env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
2635     env->hflags |= HF_LMA_MASK;
2636 #endif
2637
2638     /* flags setup : we activate the IRQs by default as in user mode */
2639     env->eflags |= IF_MASK;
2640
2641     /* linux register setup */
2642 #ifndef TARGET_ABI32
2643     env->regs[R_EAX] = regs->rax;
2644     env->regs[R_EBX] = regs->rbx;
2645     env->regs[R_ECX] = regs->rcx;
2646     env->regs[R_EDX] = regs->rdx;
2647     env->regs[R_ESI] = regs->rsi;
2648     env->regs[R_EDI] = regs->rdi;
2649     env->regs[R_EBP] = regs->rbp;
2650     env->regs[R_ESP] = regs->rsp;
2651     env->eip = regs->rip;
2652 #else
2653     env->regs[R_EAX] = regs->eax;
2654     env->regs[R_EBX] = regs->ebx;
2655     env->regs[R_ECX] = regs->ecx;
2656     env->regs[R_EDX] = regs->edx;
2657     env->regs[R_ESI] = regs->esi;
2658     env->regs[R_EDI] = regs->edi;
2659     env->regs[R_EBP] = regs->ebp;
2660     env->regs[R_ESP] = regs->esp;
2661     env->eip = regs->eip;
2662 #endif
2663
2664     /* linux interrupt setup */
2665 #ifndef TARGET_ABI32
2666     env->idt.limit = 511;
2667 #else
2668     env->idt.limit = 255;
2669 #endif
2670     env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
2671                                 PROT_READ|PROT_WRITE,
2672                                 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
2673     idt_table = g2h(env->idt.base);
2674     set_idt(0, 0);
2675     set_idt(1, 0);
2676     set_idt(2, 0);
2677     set_idt(3, 3);
2678     set_idt(4, 3);
2679     set_idt(5, 0);
2680     set_idt(6, 0);
2681     set_idt(7, 0);
2682     set_idt(8, 0);
2683     set_idt(9, 0);
2684     set_idt(10, 0);
2685     set_idt(11, 0);
2686     set_idt(12, 0);
2687     set_idt(13, 0);
2688     set_idt(14, 0);
2689     set_idt(15, 0);
2690     set_idt(16, 0);
2691     set_idt(17, 0);
2692     set_idt(18, 0);
2693     set_idt(19, 0);
2694     set_idt(0x80, 3);
2695
2696     /* linux segment setup */
2697     {
2698         uint64_t *gdt_table;
2699         env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
2700                                     PROT_READ|PROT_WRITE,
2701                                     MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
2702         env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
2703         gdt_table = g2h(env->gdt.base);
2704 #ifdef TARGET_ABI32
2705         write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
2706                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2707                  (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
2708 #else
2709         /* 64 bit code segment */
2710         write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
2711                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2712                  DESC_L_MASK |
2713                  (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
2714 #endif
2715         write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
2716                  DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2717                  (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
2718     }
2719     cpu_x86_load_seg(env, R_CS, __USER_CS);
2720     cpu_x86_load_seg(env, R_SS, __USER_DS);
2721 #ifdef TARGET_ABI32
2722     cpu_x86_load_seg(env, R_DS, __USER_DS);
2723     cpu_x86_load_seg(env, R_ES, __USER_DS);
2724     cpu_x86_load_seg(env, R_FS, __USER_DS);
2725     cpu_x86_load_seg(env, R_GS, __USER_DS);
2726     /* This hack makes Wine work... */
2727     env->segs[R_FS].selector = 0;
2728 #else
2729     cpu_x86_load_seg(env, R_DS, 0);
2730     cpu_x86_load_seg(env, R_ES, 0);
2731     cpu_x86_load_seg(env, R_FS, 0);
2732     cpu_x86_load_seg(env, R_GS, 0);
2733 #endif
2734 #elif defined(TARGET_ARM)
2735     {
2736         int i;
2737         cpsr_write(env, regs->uregs[16], 0xffffffff);
2738         for(i = 0; i < 16; i++) {
2739             env->regs[i] = regs->uregs[i];
2740         }
2741     }
2742 #elif defined(TARGET_SPARC)
2743     {
2744         int i;
2745         env->pc = regs->pc;
2746         env->npc = regs->npc;
2747         env->y = regs->y;
2748         for(i = 0; i < 8; i++)
2749             env->gregs[i] = regs->u_regs[i];
2750         for(i = 0; i < 8; i++)
2751             env->regwptr[i] = regs->u_regs[i + 8];
2752     }
2753 #elif defined(TARGET_PPC)
2754     {
2755         int i;
2756
2757 #if defined(TARGET_PPC64)
2758 #if defined(TARGET_ABI32)
2759         env->msr &= ~((target_ulong)1 << MSR_SF);
2760 #else
2761         env->msr |= (target_ulong)1 << MSR_SF;
2762 #endif
2763 #endif
2764         env->nip = regs->nip;
2765         for(i = 0; i < 32; i++) {
2766             env->gpr[i] = regs->gpr[i];
2767         }
2768     }
2769 #elif defined(TARGET_M68K)
2770     {
2771         env->pc = regs->pc;
2772         env->dregs[0] = regs->d0;
2773         env->dregs[1] = regs->d1;
2774         env->dregs[2] = regs->d2;
2775         env->dregs[3] = regs->d3;
2776         env->dregs[4] = regs->d4;
2777         env->dregs[5] = regs->d5;
2778         env->dregs[6] = regs->d6;
2779         env->dregs[7] = regs->d7;
2780         env->aregs[0] = regs->a0;
2781         env->aregs[1] = regs->a1;
2782         env->aregs[2] = regs->a2;
2783         env->aregs[3] = regs->a3;
2784         env->aregs[4] = regs->a4;
2785         env->aregs[5] = regs->a5;
2786         env->aregs[6] = regs->a6;
2787         env->aregs[7] = regs->usp;
2788         env->sr = regs->sr;
2789         ts->sim_syscalls = 1;
2790     }
2791 #elif defined(TARGET_MIPS)
2792     {
2793         int i;
2794
2795         for(i = 0; i < 32; i++) {
2796             env->active_tc.gpr[i] = regs->regs[i];
2797         }
2798         env->active_tc.PC = regs->cp0_epc;
2799     }
2800 #elif defined(TARGET_SH4)
2801     {
2802         int i;
2803
2804         for(i = 0; i < 16; i++) {
2805             env->gregs[i] = regs->regs[i];
2806         }
2807         env->pc = regs->pc;
2808     }
2809 #elif defined(TARGET_ALPHA)
2810     {
2811         int i;
2812
2813         for(i = 0; i < 28; i++) {
2814             env->ir[i] = ((abi_ulong *)regs)[i];
2815         }
2816         env->ipr[IPR_USP] = regs->usp;
2817         env->ir[30] = regs->usp;
2818         env->pc = regs->pc;
2819         env->unique = regs->unique;
2820     }
2821 #elif defined(TARGET_CRIS)
2822     {
2823             env->regs[0] = regs->r0;
2824             env->regs[1] = regs->r1;
2825             env->regs[2] = regs->r2;
2826             env->regs[3] = regs->r3;
2827             env->regs[4] = regs->r4;
2828             env->regs[5] = regs->r5;
2829             env->regs[6] = regs->r6;
2830             env->regs[7] = regs->r7;
2831             env->regs[8] = regs->r8;
2832             env->regs[9] = regs->r9;
2833             env->regs[10] = regs->r10;
2834             env->regs[11] = regs->r11;
2835             env->regs[12] = regs->r12;
2836             env->regs[13] = regs->r13;
2837             env->regs[14] = info->start_stack;
2838             env->regs[15] = regs->acr;      
2839             env->pc = regs->erp;
2840     }
2841 #else
2842 #error unsupported target CPU
2843 #endif
2844
2845 #if defined(TARGET_ARM) || defined(TARGET_M68K)
2846     ts->stack_base = info->start_stack;
2847     ts->heap_base = info->brk;
2848     /* This will be filled in on the first SYS_HEAPINFO call.  */
2849     ts->heap_limit = 0;
2850 #endif
2851
2852     if (gdbstub_port) {
2853         gdbserver_start (gdbstub_port);
2854         gdb_handlesig(env, 0);
2855     }
2856     cpu_loop(env);
2857     /* never exits */
2858     return 0;
2859 }