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