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