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