4 * Copyright (c) 2003-2008 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include <sys/syscall.h>
27 #include <sys/resource.h>
30 #include "qemu-common.h"
31 #include "cache-utils.h"
34 #include "qemu-timer.h"
37 #define DEBUG_LOGFILE "/tmp/qemu.log"
42 unsigned long mmap_min_addr;
43 #if defined(CONFIG_USE_GUEST_BASE)
44 unsigned long guest_base;
46 unsigned long reserved_va;
49 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
50 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
52 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
53 we allocate a bigger stack. Need a better solution, for example
54 by remapping the process stack directly at the right place */
55 unsigned long guest_stack_size = 8 * 1024 * 1024UL;
57 void gemu_log(const char *fmt, ...)
62 vfprintf(stderr, fmt, ap);
66 #if defined(TARGET_I386)
67 int cpu_get_pic_interrupt(CPUState *env)
73 /* timers for rdtsc */
77 static uint64_t emu_time;
79 int64_t cpu_get_real_ticks(void)
86 #if defined(CONFIG_USE_NPTL)
87 /***********************************************************/
88 /* Helper routines for implementing atomic operations. */
90 /* To implement exclusive operations we force all cpus to syncronise.
91 We don't require a full sync, only that no cpus are executing guest code.
92 The alternative is to map target atomic ops onto host equivalents,
93 which requires quite a lot of per host/target work. */
94 static pthread_mutex_t cpu_list_mutex = PTHREAD_MUTEX_INITIALIZER;
95 static pthread_mutex_t exclusive_lock = PTHREAD_MUTEX_INITIALIZER;
96 static pthread_cond_t exclusive_cond = PTHREAD_COND_INITIALIZER;
97 static pthread_cond_t exclusive_resume = PTHREAD_COND_INITIALIZER;
98 static int pending_cpus;
100 /* Make sure everything is in a consistent state for calling fork(). */
101 void fork_start(void)
103 pthread_mutex_lock(&tb_lock);
104 pthread_mutex_lock(&exclusive_lock);
108 void fork_end(int child)
110 mmap_fork_end(child);
112 /* Child processes created by fork() only have a single thread.
113 Discard information about the parent threads. */
114 first_cpu = thread_env;
115 thread_env->next_cpu = NULL;
117 pthread_mutex_init(&exclusive_lock, NULL);
118 pthread_mutex_init(&cpu_list_mutex, NULL);
119 pthread_cond_init(&exclusive_cond, NULL);
120 pthread_cond_init(&exclusive_resume, NULL);
121 pthread_mutex_init(&tb_lock, NULL);
122 gdbserver_fork(thread_env);
124 pthread_mutex_unlock(&exclusive_lock);
125 pthread_mutex_unlock(&tb_lock);
129 /* Wait for pending exclusive operations to complete. The exclusive lock
131 static inline void exclusive_idle(void)
133 while (pending_cpus) {
134 pthread_cond_wait(&exclusive_resume, &exclusive_lock);
138 /* Start an exclusive operation.
139 Must only be called from outside cpu_arm_exec. */
140 static inline void start_exclusive(void)
143 pthread_mutex_lock(&exclusive_lock);
147 /* Make all other cpus stop executing. */
148 for (other = first_cpu; other; other = other->next_cpu) {
149 if (other->running) {
154 if (pending_cpus > 1) {
155 pthread_cond_wait(&exclusive_cond, &exclusive_lock);
159 /* Finish an exclusive operation. */
160 static inline void end_exclusive(void)
163 pthread_cond_broadcast(&exclusive_resume);
164 pthread_mutex_unlock(&exclusive_lock);
167 /* Wait for exclusive ops to finish, and begin cpu execution. */
168 static inline void cpu_exec_start(CPUState *env)
170 pthread_mutex_lock(&exclusive_lock);
173 pthread_mutex_unlock(&exclusive_lock);
176 /* Mark cpu as not executing, and release pending exclusive ops. */
177 static inline void cpu_exec_end(CPUState *env)
179 pthread_mutex_lock(&exclusive_lock);
181 if (pending_cpus > 1) {
183 if (pending_cpus == 1) {
184 pthread_cond_signal(&exclusive_cond);
188 pthread_mutex_unlock(&exclusive_lock);
191 void cpu_list_lock(void)
193 pthread_mutex_lock(&cpu_list_mutex);
196 void cpu_list_unlock(void)
198 pthread_mutex_unlock(&cpu_list_mutex);
200 #else /* if !CONFIG_USE_NPTL */
201 /* These are no-ops because we are not threadsafe. */
202 static inline void cpu_exec_start(CPUState *env)
206 static inline void cpu_exec_end(CPUState *env)
210 static inline void start_exclusive(void)
214 static inline void end_exclusive(void)
218 void fork_start(void)
222 void fork_end(int child)
225 gdbserver_fork(thread_env);
229 void cpu_list_lock(void)
233 void cpu_list_unlock(void)
240 /***********************************************************/
241 /* CPUX86 core interface */
243 void cpu_smm_update(CPUState *env)
247 uint64_t cpu_get_tsc(CPUX86State *env)
249 return cpu_get_real_ticks();
252 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
257 e1 = (addr << 16) | (limit & 0xffff);
258 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
265 static uint64_t *idt_table;
267 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
268 uint64_t addr, unsigned int sel)
271 e1 = (addr & 0xffff) | (sel << 16);
272 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
276 p[2] = tswap32(addr >> 32);
279 /* only dpl matters as we do only user space emulation */
280 static void set_idt(int n, unsigned int dpl)
282 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
285 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
286 uint32_t addr, unsigned int sel)
289 e1 = (addr & 0xffff) | (sel << 16);
290 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
296 /* only dpl matters as we do only user space emulation */
297 static void set_idt(int n, unsigned int dpl)
299 set_gate(idt_table + n, 0, dpl, 0, 0);
303 void cpu_loop(CPUX86State *env)
307 target_siginfo_t info;
310 trapnr = cpu_x86_exec(env);
313 /* linux syscall from int $0x80 */
314 env->regs[R_EAX] = do_syscall(env,
326 /* linux syscall from syscall instruction */
327 env->regs[R_EAX] = do_syscall(env,
336 env->eip = env->exception_next_eip;
341 info.si_signo = SIGBUS;
343 info.si_code = TARGET_SI_KERNEL;
344 info._sifields._sigfault._addr = 0;
345 queue_signal(env, info.si_signo, &info);
348 /* XXX: potential problem if ABI32 */
349 #ifndef TARGET_X86_64
350 if (env->eflags & VM_MASK) {
351 handle_vm86_fault(env);
355 info.si_signo = SIGSEGV;
357 info.si_code = TARGET_SI_KERNEL;
358 info._sifields._sigfault._addr = 0;
359 queue_signal(env, info.si_signo, &info);
363 info.si_signo = SIGSEGV;
365 if (!(env->error_code & 1))
366 info.si_code = TARGET_SEGV_MAPERR;
368 info.si_code = TARGET_SEGV_ACCERR;
369 info._sifields._sigfault._addr = env->cr[2];
370 queue_signal(env, info.si_signo, &info);
373 #ifndef TARGET_X86_64
374 if (env->eflags & VM_MASK) {
375 handle_vm86_trap(env, trapnr);
379 /* division by zero */
380 info.si_signo = SIGFPE;
382 info.si_code = TARGET_FPE_INTDIV;
383 info._sifields._sigfault._addr = env->eip;
384 queue_signal(env, info.si_signo, &info);
389 #ifndef TARGET_X86_64
390 if (env->eflags & VM_MASK) {
391 handle_vm86_trap(env, trapnr);
395 info.si_signo = SIGTRAP;
397 if (trapnr == EXCP01_DB) {
398 info.si_code = TARGET_TRAP_BRKPT;
399 info._sifields._sigfault._addr = env->eip;
401 info.si_code = TARGET_SI_KERNEL;
402 info._sifields._sigfault._addr = 0;
404 queue_signal(env, info.si_signo, &info);
409 #ifndef TARGET_X86_64
410 if (env->eflags & VM_MASK) {
411 handle_vm86_trap(env, trapnr);
415 info.si_signo = SIGSEGV;
417 info.si_code = TARGET_SI_KERNEL;
418 info._sifields._sigfault._addr = 0;
419 queue_signal(env, info.si_signo, &info);
423 info.si_signo = SIGILL;
425 info.si_code = TARGET_ILL_ILLOPN;
426 info._sifields._sigfault._addr = env->eip;
427 queue_signal(env, info.si_signo, &info);
430 /* just indicate that signals should be handled asap */
436 sig = gdb_handlesig (env, TARGET_SIGTRAP);
441 info.si_code = TARGET_TRAP_BRKPT;
442 queue_signal(env, info.si_signo, &info);
447 pc = env->segs[R_CS].base + env->eip;
448 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
452 process_pending_signals(env);
460 * See the Linux kernel's Documentation/arm/kernel_user_helpers.txt
462 * r0 = pointer to oldval
463 * r1 = pointer to newval
464 * r2 = pointer to target value
467 * r0 = 0 if *ptr was changed, non-0 if no exchange happened
468 * C set if *ptr was changed, clear if no exchange happened
470 * Note segv's in kernel helpers are a bit tricky, we can set the
471 * data address sensibly but the PC address is just the entry point.
473 static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
475 uint64_t oldval, newval, val;
477 target_siginfo_t info;
479 /* Based on the 32 bit code in do_kernel_trap */
481 /* XXX: This only works between threads, not between processes.
482 It's probably possible to implement this with native host
483 operations. However things like ldrex/strex are much harder so
484 there's not much point trying. */
486 cpsr = cpsr_read(env);
489 if (get_user_u64(oldval, env->regs[0])) {
490 env->cp15.c6_data = env->regs[0];
494 if (get_user_u64(newval, env->regs[1])) {
495 env->cp15.c6_data = env->regs[1];
499 if (get_user_u64(val, addr)) {
500 env->cp15.c6_data = addr;
507 if (put_user_u64(val, addr)) {
508 env->cp15.c6_data = addr;
518 cpsr_write(env, cpsr, CPSR_C);
524 /* We get the PC of the entry address - which is as good as anything,
525 on a real kernel what you get depends on which mode it uses. */
526 info.si_signo = SIGSEGV;
528 /* XXX: check env->error_code */
529 info.si_code = TARGET_SEGV_MAPERR;
530 info._sifields._sigfault._addr = env->cp15.c6_data;
531 queue_signal(env, info.si_signo, &info);
536 /* Handle a jump to the kernel code page. */
538 do_kernel_trap(CPUARMState *env)
544 switch (env->regs[15]) {
545 case 0xffff0fa0: /* __kernel_memory_barrier */
546 /* ??? No-op. Will need to do better for SMP. */
548 case 0xffff0fc0: /* __kernel_cmpxchg */
549 /* XXX: This only works between threads, not between processes.
550 It's probably possible to implement this with native host
551 operations. However things like ldrex/strex are much harder so
552 there's not much point trying. */
554 cpsr = cpsr_read(env);
556 /* FIXME: This should SEGV if the access fails. */
557 if (get_user_u32(val, addr))
559 if (val == env->regs[0]) {
561 /* FIXME: Check for segfaults. */
562 put_user_u32(val, addr);
569 cpsr_write(env, cpsr, CPSR_C);
572 case 0xffff0fe0: /* __kernel_get_tls */
573 env->regs[0] = env->cp15.c13_tls2;
575 case 0xffff0f60: /* __kernel_cmpxchg64 */
576 arm_kernel_cmpxchg64_helper(env);
582 /* Jump back to the caller. */
583 addr = env->regs[14];
588 env->regs[15] = addr;
593 static int do_strex(CPUARMState *env)
601 addr = env->exclusive_addr;
602 if (addr != env->exclusive_test) {
605 size = env->exclusive_info & 0xf;
608 segv = get_user_u8(val, addr);
611 segv = get_user_u16(val, addr);
615 segv = get_user_u32(val, addr);
621 env->cp15.c6_data = addr;
624 if (val != env->exclusive_val) {
628 segv = get_user_u32(val, addr + 4);
630 env->cp15.c6_data = addr + 4;
633 if (val != env->exclusive_high) {
637 val = env->regs[(env->exclusive_info >> 8) & 0xf];
640 segv = put_user_u8(val, addr);
643 segv = put_user_u16(val, addr);
647 segv = put_user_u32(val, addr);
651 env->cp15.c6_data = addr;
655 val = env->regs[(env->exclusive_info >> 12) & 0xf];
656 segv = put_user_u32(val, addr + 4);
658 env->cp15.c6_data = addr + 4;
665 env->regs[(env->exclusive_info >> 4) & 0xf] = rc;
671 void cpu_loop(CPUARMState *env)
674 unsigned int n, insn;
675 target_siginfo_t info;
680 trapnr = cpu_arm_exec(env);
685 TaskState *ts = env->opaque;
689 /* we handle the FPU emulation here, as Linux */
690 /* we get the opcode */
691 /* FIXME - what to do if get_user() fails? */
692 get_user_u32(opcode, env->regs[15]);
694 rc = EmulateAll(opcode, &ts->fpa, env);
695 if (rc == 0) { /* illegal instruction */
696 info.si_signo = SIGILL;
698 info.si_code = TARGET_ILL_ILLOPN;
699 info._sifields._sigfault._addr = env->regs[15];
700 queue_signal(env, info.si_signo, &info);
701 } else if (rc < 0) { /* FP exception */
704 /* translate softfloat flags to FPSR flags */
705 if (-rc & float_flag_invalid)
707 if (-rc & float_flag_divbyzero)
709 if (-rc & float_flag_overflow)
711 if (-rc & float_flag_underflow)
713 if (-rc & float_flag_inexact)
716 FPSR fpsr = ts->fpa.fpsr;
717 //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
719 if (fpsr & (arm_fpe << 16)) { /* exception enabled? */
720 info.si_signo = SIGFPE;
723 /* ordered by priority, least first */
724 if (arm_fpe & BIT_IXC) info.si_code = TARGET_FPE_FLTRES;
725 if (arm_fpe & BIT_UFC) info.si_code = TARGET_FPE_FLTUND;
726 if (arm_fpe & BIT_OFC) info.si_code = TARGET_FPE_FLTOVF;
727 if (arm_fpe & BIT_DZC) info.si_code = TARGET_FPE_FLTDIV;
728 if (arm_fpe & BIT_IOC) info.si_code = TARGET_FPE_FLTINV;
730 info._sifields._sigfault._addr = env->regs[15];
731 queue_signal(env, info.si_signo, &info);
736 /* accumulate unenabled exceptions */
737 if ((!(fpsr & BIT_IXE)) && (arm_fpe & BIT_IXC))
739 if ((!(fpsr & BIT_UFE)) && (arm_fpe & BIT_UFC))
741 if ((!(fpsr & BIT_OFE)) && (arm_fpe & BIT_OFC))
743 if ((!(fpsr & BIT_DZE)) && (arm_fpe & BIT_DZC))
745 if ((!(fpsr & BIT_IOE)) && (arm_fpe & BIT_IOC))
748 } else { /* everything OK */
759 if (trapnr == EXCP_BKPT) {
761 /* FIXME - what to do if get_user() fails? */
762 get_user_u16(insn, env->regs[15]);
766 /* FIXME - what to do if get_user() fails? */
767 get_user_u32(insn, env->regs[15]);
768 n = (insn & 0xf) | ((insn >> 4) & 0xff0);
773 /* FIXME - what to do if get_user() fails? */
774 get_user_u16(insn, env->regs[15] - 2);
777 /* FIXME - what to do if get_user() fails? */
778 get_user_u32(insn, env->regs[15] - 4);
783 if (n == ARM_NR_cacheflush) {
785 } else if (n == ARM_NR_semihosting
786 || n == ARM_NR_thumb_semihosting) {
787 env->regs[0] = do_arm_semihosting (env);
788 } else if (n == 0 || n >= ARM_SYSCALL_BASE
789 || (env->thumb && n == ARM_THUMB_SYSCALL)) {
791 if (env->thumb || n == 0) {
794 n -= ARM_SYSCALL_BASE;
797 if ( n > ARM_NR_BASE) {
799 case ARM_NR_cacheflush:
803 cpu_set_tls(env, env->regs[0]);
807 gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
809 env->regs[0] = -TARGET_ENOSYS;
813 env->regs[0] = do_syscall(env,
829 /* just indicate that signals should be handled asap */
831 case EXCP_PREFETCH_ABORT:
832 addr = env->cp15.c6_insn;
834 case EXCP_DATA_ABORT:
835 addr = env->cp15.c6_data;
838 info.si_signo = SIGSEGV;
840 /* XXX: check env->error_code */
841 info.si_code = TARGET_SEGV_MAPERR;
842 info._sifields._sigfault._addr = addr;
843 queue_signal(env, info.si_signo, &info);
850 sig = gdb_handlesig (env, TARGET_SIGTRAP);
855 info.si_code = TARGET_TRAP_BRKPT;
856 queue_signal(env, info.si_signo, &info);
860 case EXCP_KERNEL_TRAP:
861 if (do_kernel_trap(env))
866 addr = env->cp15.c6_data;
872 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
874 cpu_dump_state(env, stderr, fprintf, 0);
877 process_pending_signals(env);
883 #ifdef TARGET_UNICORE32
885 void cpu_loop(CPUState *env)
888 unsigned int n, insn;
889 target_siginfo_t info;
893 trapnr = uc32_cpu_exec(env);
899 get_user_u32(insn, env->regs[31] - 4);
902 if (n >= UC32_SYSCALL_BASE) {
904 n -= UC32_SYSCALL_BASE;
905 if (n == UC32_SYSCALL_NR_set_tls) {
906 cpu_set_tls(env, env->regs[0]);
909 env->regs[0] = do_syscall(env,
925 info.si_signo = SIGSEGV;
927 /* XXX: check env->error_code */
928 info.si_code = TARGET_SEGV_MAPERR;
929 info._sifields._sigfault._addr = env->cp0.c4_faultaddr;
930 queue_signal(env, info.si_signo, &info);
933 /* just indicate that signals should be handled asap */
939 sig = gdb_handlesig(env, TARGET_SIGTRAP);
943 info.si_code = TARGET_TRAP_BRKPT;
944 queue_signal(env, info.si_signo, &info);
951 process_pending_signals(env);
955 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr);
956 cpu_dump_state(env, stderr, fprintf, 0);
962 #define SPARC64_STACK_BIAS 2047
966 /* WARNING: dealing with register windows _is_ complicated. More info
967 can be found at http://www.sics.se/~psm/sparcstack.html */
968 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
970 index = (index + cwp * 16) % (16 * env->nwindows);
971 /* wrap handling : if cwp is on the last window, then we use the
972 registers 'after' the end */
973 if (index < 8 && env->cwp == env->nwindows - 1)
974 index += 16 * env->nwindows;
978 /* save the register window 'cwp1' */
979 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
984 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
985 #ifdef TARGET_SPARC64
987 sp_ptr += SPARC64_STACK_BIAS;
989 #if defined(DEBUG_WIN)
990 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
993 for(i = 0; i < 16; i++) {
994 /* FIXME - what to do if put_user() fails? */
995 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
996 sp_ptr += sizeof(abi_ulong);
1000 static void save_window(CPUSPARCState *env)
1002 #ifndef TARGET_SPARC64
1003 unsigned int new_wim;
1004 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
1005 ((1LL << env->nwindows) - 1);
1006 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
1009 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
1015 static void restore_window(CPUSPARCState *env)
1017 #ifndef TARGET_SPARC64
1018 unsigned int new_wim;
1020 unsigned int i, cwp1;
1023 #ifndef TARGET_SPARC64
1024 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
1025 ((1LL << env->nwindows) - 1);
1028 /* restore the invalid window */
1029 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
1030 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
1031 #ifdef TARGET_SPARC64
1033 sp_ptr += SPARC64_STACK_BIAS;
1035 #if defined(DEBUG_WIN)
1036 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
1039 for(i = 0; i < 16; i++) {
1040 /* FIXME - what to do if get_user() fails? */
1041 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
1042 sp_ptr += sizeof(abi_ulong);
1044 #ifdef TARGET_SPARC64
1046 if (env->cleanwin < env->nwindows - 1)
1054 static void flush_windows(CPUSPARCState *env)
1060 /* if restore would invoke restore_window(), then we can stop */
1061 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
1062 #ifndef TARGET_SPARC64
1063 if (env->wim & (1 << cwp1))
1066 if (env->canrestore == 0)
1071 save_window_offset(env, cwp1);
1074 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
1075 #ifndef TARGET_SPARC64
1076 /* set wim so that restore will reload the registers */
1077 env->wim = 1 << cwp1;
1079 #if defined(DEBUG_WIN)
1080 printf("flush_windows: nb=%d\n", offset - 1);
1084 void cpu_loop (CPUSPARCState *env)
1088 target_siginfo_t info;
1091 trapnr = cpu_sparc_exec (env);
1094 #ifndef TARGET_SPARC64
1101 ret = do_syscall (env, env->gregs[1],
1102 env->regwptr[0], env->regwptr[1],
1103 env->regwptr[2], env->regwptr[3],
1104 env->regwptr[4], env->regwptr[5],
1106 if ((abi_ulong)ret >= (abi_ulong)(-515)) {
1107 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1108 env->xcc |= PSR_CARRY;
1110 env->psr |= PSR_CARRY;
1114 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1115 env->xcc &= ~PSR_CARRY;
1117 env->psr &= ~PSR_CARRY;
1120 env->regwptr[0] = ret;
1121 /* next instruction */
1123 env->npc = env->npc + 4;
1125 case 0x83: /* flush windows */
1130 /* next instruction */
1132 env->npc = env->npc + 4;
1134 #ifndef TARGET_SPARC64
1135 case TT_WIN_OVF: /* window overflow */
1138 case TT_WIN_UNF: /* window underflow */
1139 restore_window(env);
1144 info.si_signo = SIGSEGV;
1146 /* XXX: check env->error_code */
1147 info.si_code = TARGET_SEGV_MAPERR;
1148 info._sifields._sigfault._addr = env->mmuregs[4];
1149 queue_signal(env, info.si_signo, &info);
1153 case TT_SPILL: /* window overflow */
1156 case TT_FILL: /* window underflow */
1157 restore_window(env);
1162 info.si_signo = SIGSEGV;
1164 /* XXX: check env->error_code */
1165 info.si_code = TARGET_SEGV_MAPERR;
1166 if (trapnr == TT_DFAULT)
1167 info._sifields._sigfault._addr = env->dmmuregs[4];
1169 info._sifields._sigfault._addr = cpu_tsptr(env)->tpc;
1170 queue_signal(env, info.si_signo, &info);
1173 #ifndef TARGET_ABI32
1176 sparc64_get_context(env);
1180 sparc64_set_context(env);
1184 case EXCP_INTERRUPT:
1185 /* just indicate that signals should be handled asap */
1191 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1194 info.si_signo = sig;
1196 info.si_code = TARGET_TRAP_BRKPT;
1197 queue_signal(env, info.si_signo, &info);
1202 printf ("Unhandled trap: 0x%x\n", trapnr);
1203 cpu_dump_state(env, stderr, fprintf, 0);
1206 process_pending_signals (env);
1213 static inline uint64_t cpu_ppc_get_tb (CPUState *env)
1219 uint64_t cpu_ppc_load_tbl (CPUState *env)
1221 return cpu_ppc_get_tb(env);
1224 uint32_t cpu_ppc_load_tbu (CPUState *env)
1226 return cpu_ppc_get_tb(env) >> 32;
1229 uint64_t cpu_ppc_load_atbl (CPUState *env)
1231 return cpu_ppc_get_tb(env);
1234 uint32_t cpu_ppc_load_atbu (CPUState *env)
1236 return cpu_ppc_get_tb(env) >> 32;
1239 uint32_t cpu_ppc601_load_rtcu (CPUState *env)
1240 __attribute__ (( alias ("cpu_ppc_load_tbu") ));
1242 uint32_t cpu_ppc601_load_rtcl (CPUState *env)
1244 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
1247 /* XXX: to be fixed */
1248 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
1253 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
1258 #define EXCP_DUMP(env, fmt, ...) \
1260 fprintf(stderr, fmt , ## __VA_ARGS__); \
1261 cpu_dump_state(env, stderr, fprintf, 0); \
1262 qemu_log(fmt, ## __VA_ARGS__); \
1264 log_cpu_state(env, 0); \
1267 static int do_store_exclusive(CPUPPCState *env)
1270 target_ulong page_addr;
1275 addr = env->reserve_ea;
1276 page_addr = addr & TARGET_PAGE_MASK;
1279 flags = page_get_flags(page_addr);
1280 if ((flags & PAGE_READ) == 0) {
1283 int reg = env->reserve_info & 0x1f;
1284 int size = (env->reserve_info >> 5) & 0xf;
1287 if (addr == env->reserve_addr) {
1289 case 1: segv = get_user_u8(val, addr); break;
1290 case 2: segv = get_user_u16(val, addr); break;
1291 case 4: segv = get_user_u32(val, addr); break;
1292 #if defined(TARGET_PPC64)
1293 case 8: segv = get_user_u64(val, addr); break;
1297 if (!segv && val == env->reserve_val) {
1298 val = env->gpr[reg];
1300 case 1: segv = put_user_u8(val, addr); break;
1301 case 2: segv = put_user_u16(val, addr); break;
1302 case 4: segv = put_user_u32(val, addr); break;
1303 #if defined(TARGET_PPC64)
1304 case 8: segv = put_user_u64(val, addr); break;
1313 env->crf[0] = (stored << 1) | xer_so;
1314 env->reserve_addr = (target_ulong)-1;
1324 void cpu_loop(CPUPPCState *env)
1326 target_siginfo_t info;
1331 cpu_exec_start(env);
1332 trapnr = cpu_ppc_exec(env);
1335 case POWERPC_EXCP_NONE:
1338 case POWERPC_EXCP_CRITICAL: /* Critical input */
1339 cpu_abort(env, "Critical interrupt while in user mode. "
1342 case POWERPC_EXCP_MCHECK: /* Machine check exception */
1343 cpu_abort(env, "Machine check exception while in user mode. "
1346 case POWERPC_EXCP_DSI: /* Data storage exception */
1347 EXCP_DUMP(env, "Invalid data memory access: 0x" TARGET_FMT_lx "\n",
1349 /* XXX: check this. Seems bugged */
1350 switch (env->error_code & 0xFF000000) {
1352 info.si_signo = TARGET_SIGSEGV;
1354 info.si_code = TARGET_SEGV_MAPERR;
1357 info.si_signo = TARGET_SIGILL;
1359 info.si_code = TARGET_ILL_ILLADR;
1362 info.si_signo = TARGET_SIGSEGV;
1364 info.si_code = TARGET_SEGV_ACCERR;
1367 /* Let's send a regular segfault... */
1368 EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
1370 info.si_signo = TARGET_SIGSEGV;
1372 info.si_code = TARGET_SEGV_MAPERR;
1375 info._sifields._sigfault._addr = env->nip;
1376 queue_signal(env, info.si_signo, &info);
1378 case POWERPC_EXCP_ISI: /* Instruction storage exception */
1379 EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" TARGET_FMT_lx
1380 "\n", env->spr[SPR_SRR0]);
1381 /* XXX: check this */
1382 switch (env->error_code & 0xFF000000) {
1384 info.si_signo = TARGET_SIGSEGV;
1386 info.si_code = TARGET_SEGV_MAPERR;
1390 info.si_signo = TARGET_SIGSEGV;
1392 info.si_code = TARGET_SEGV_ACCERR;
1395 /* Let's send a regular segfault... */
1396 EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
1398 info.si_signo = TARGET_SIGSEGV;
1400 info.si_code = TARGET_SEGV_MAPERR;
1403 info._sifields._sigfault._addr = env->nip - 4;
1404 queue_signal(env, info.si_signo, &info);
1406 case POWERPC_EXCP_EXTERNAL: /* External input */
1407 cpu_abort(env, "External interrupt while in user mode. "
1410 case POWERPC_EXCP_ALIGN: /* Alignment exception */
1411 EXCP_DUMP(env, "Unaligned memory access\n");
1412 /* XXX: check this */
1413 info.si_signo = TARGET_SIGBUS;
1415 info.si_code = TARGET_BUS_ADRALN;
1416 info._sifields._sigfault._addr = env->nip - 4;
1417 queue_signal(env, info.si_signo, &info);
1419 case POWERPC_EXCP_PROGRAM: /* Program exception */
1420 /* XXX: check this */
1421 switch (env->error_code & ~0xF) {
1422 case POWERPC_EXCP_FP:
1423 EXCP_DUMP(env, "Floating point program exception\n");
1424 info.si_signo = TARGET_SIGFPE;
1426 switch (env->error_code & 0xF) {
1427 case POWERPC_EXCP_FP_OX:
1428 info.si_code = TARGET_FPE_FLTOVF;
1430 case POWERPC_EXCP_FP_UX:
1431 info.si_code = TARGET_FPE_FLTUND;
1433 case POWERPC_EXCP_FP_ZX:
1434 case POWERPC_EXCP_FP_VXZDZ:
1435 info.si_code = TARGET_FPE_FLTDIV;
1437 case POWERPC_EXCP_FP_XX:
1438 info.si_code = TARGET_FPE_FLTRES;
1440 case POWERPC_EXCP_FP_VXSOFT:
1441 info.si_code = TARGET_FPE_FLTINV;
1443 case POWERPC_EXCP_FP_VXSNAN:
1444 case POWERPC_EXCP_FP_VXISI:
1445 case POWERPC_EXCP_FP_VXIDI:
1446 case POWERPC_EXCP_FP_VXIMZ:
1447 case POWERPC_EXCP_FP_VXVC:
1448 case POWERPC_EXCP_FP_VXSQRT:
1449 case POWERPC_EXCP_FP_VXCVI:
1450 info.si_code = TARGET_FPE_FLTSUB;
1453 EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
1458 case POWERPC_EXCP_INVAL:
1459 EXCP_DUMP(env, "Invalid instruction\n");
1460 info.si_signo = TARGET_SIGILL;
1462 switch (env->error_code & 0xF) {
1463 case POWERPC_EXCP_INVAL_INVAL:
1464 info.si_code = TARGET_ILL_ILLOPC;
1466 case POWERPC_EXCP_INVAL_LSWX:
1467 info.si_code = TARGET_ILL_ILLOPN;
1469 case POWERPC_EXCP_INVAL_SPR:
1470 info.si_code = TARGET_ILL_PRVREG;
1472 case POWERPC_EXCP_INVAL_FP:
1473 info.si_code = TARGET_ILL_COPROC;
1476 EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
1477 env->error_code & 0xF);
1478 info.si_code = TARGET_ILL_ILLADR;
1482 case POWERPC_EXCP_PRIV:
1483 EXCP_DUMP(env, "Privilege violation\n");
1484 info.si_signo = TARGET_SIGILL;
1486 switch (env->error_code & 0xF) {
1487 case POWERPC_EXCP_PRIV_OPC:
1488 info.si_code = TARGET_ILL_PRVOPC;
1490 case POWERPC_EXCP_PRIV_REG:
1491 info.si_code = TARGET_ILL_PRVREG;
1494 EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
1495 env->error_code & 0xF);
1496 info.si_code = TARGET_ILL_PRVOPC;
1500 case POWERPC_EXCP_TRAP:
1501 cpu_abort(env, "Tried to call a TRAP\n");
1504 /* Should not happen ! */
1505 cpu_abort(env, "Unknown program exception (%02x)\n",
1509 info._sifields._sigfault._addr = env->nip - 4;
1510 queue_signal(env, info.si_signo, &info);
1512 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
1513 EXCP_DUMP(env, "No floating point allowed\n");
1514 info.si_signo = TARGET_SIGILL;
1516 info.si_code = TARGET_ILL_COPROC;
1517 info._sifields._sigfault._addr = env->nip - 4;
1518 queue_signal(env, info.si_signo, &info);
1520 case POWERPC_EXCP_SYSCALL: /* System call exception */
1521 cpu_abort(env, "Syscall exception while in user mode. "
1524 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
1525 EXCP_DUMP(env, "No APU instruction allowed\n");
1526 info.si_signo = TARGET_SIGILL;
1528 info.si_code = TARGET_ILL_COPROC;
1529 info._sifields._sigfault._addr = env->nip - 4;
1530 queue_signal(env, info.si_signo, &info);
1532 case POWERPC_EXCP_DECR: /* Decrementer exception */
1533 cpu_abort(env, "Decrementer interrupt while in user mode. "
1536 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
1537 cpu_abort(env, "Fix interval timer interrupt while in user mode. "
1540 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
1541 cpu_abort(env, "Watchdog timer interrupt while in user mode. "
1544 case POWERPC_EXCP_DTLB: /* Data TLB error */
1545 cpu_abort(env, "Data TLB exception while in user mode. "
1548 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
1549 cpu_abort(env, "Instruction TLB exception while in user mode. "
1552 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavail. */
1553 EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
1554 info.si_signo = TARGET_SIGILL;
1556 info.si_code = TARGET_ILL_COPROC;
1557 info._sifields._sigfault._addr = env->nip - 4;
1558 queue_signal(env, info.si_signo, &info);
1560 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data IRQ */
1561 cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
1563 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round IRQ */
1564 cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
1566 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor IRQ */
1567 cpu_abort(env, "Performance monitor exception not handled\n");
1569 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
1570 cpu_abort(env, "Doorbell interrupt while in user mode. "
1573 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
1574 cpu_abort(env, "Doorbell critical interrupt while in user mode. "
1577 case POWERPC_EXCP_RESET: /* System reset exception */
1578 cpu_abort(env, "Reset interrupt while in user mode. "
1581 case POWERPC_EXCP_DSEG: /* Data segment exception */
1582 cpu_abort(env, "Data segment exception while in user mode. "
1585 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
1586 cpu_abort(env, "Instruction segment exception "
1587 "while in user mode. Aborting\n");
1589 /* PowerPC 64 with hypervisor mode support */
1590 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
1591 cpu_abort(env, "Hypervisor decrementer interrupt "
1592 "while in user mode. Aborting\n");
1594 case POWERPC_EXCP_TRACE: /* Trace exception */
1596 * we use this exception to emulate step-by-step execution mode.
1599 /* PowerPC 64 with hypervisor mode support */
1600 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
1601 cpu_abort(env, "Hypervisor data storage exception "
1602 "while in user mode. Aborting\n");
1604 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage excp */
1605 cpu_abort(env, "Hypervisor instruction storage exception "
1606 "while in user mode. Aborting\n");
1608 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
1609 cpu_abort(env, "Hypervisor data segment exception "
1610 "while in user mode. Aborting\n");
1612 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment excp */
1613 cpu_abort(env, "Hypervisor instruction segment exception "
1614 "while in user mode. Aborting\n");
1616 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
1617 EXCP_DUMP(env, "No Altivec instructions allowed\n");
1618 info.si_signo = TARGET_SIGILL;
1620 info.si_code = TARGET_ILL_COPROC;
1621 info._sifields._sigfault._addr = env->nip - 4;
1622 queue_signal(env, info.si_signo, &info);
1624 case POWERPC_EXCP_PIT: /* Programmable interval timer IRQ */
1625 cpu_abort(env, "Programable interval timer interrupt "
1626 "while in user mode. Aborting\n");
1628 case POWERPC_EXCP_IO: /* IO error exception */
1629 cpu_abort(env, "IO error exception while in user mode. "
1632 case POWERPC_EXCP_RUNM: /* Run mode exception */
1633 cpu_abort(env, "Run mode exception while in user mode. "
1636 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
1637 cpu_abort(env, "Emulation trap exception not handled\n");
1639 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
1640 cpu_abort(env, "Instruction fetch TLB exception "
1641 "while in user-mode. Aborting");
1643 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
1644 cpu_abort(env, "Data load TLB exception while in user-mode. "
1647 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
1648 cpu_abort(env, "Data store TLB exception while in user-mode. "
1651 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
1652 cpu_abort(env, "Floating-point assist exception not handled\n");
1654 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
1655 cpu_abort(env, "Instruction address breakpoint exception "
1658 case POWERPC_EXCP_SMI: /* System management interrupt */
1659 cpu_abort(env, "System management interrupt while in user mode. "
1662 case POWERPC_EXCP_THERM: /* Thermal interrupt */
1663 cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
1666 case POWERPC_EXCP_PERFM: /* Embedded performance monitor IRQ */
1667 cpu_abort(env, "Performance monitor exception not handled\n");
1669 case POWERPC_EXCP_VPUA: /* Vector assist exception */
1670 cpu_abort(env, "Vector assist exception not handled\n");
1672 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
1673 cpu_abort(env, "Soft patch exception not handled\n");
1675 case POWERPC_EXCP_MAINT: /* Maintenance exception */
1676 cpu_abort(env, "Maintenance exception while in user mode. "
1679 case POWERPC_EXCP_STOP: /* stop translation */
1680 /* We did invalidate the instruction cache. Go on */
1682 case POWERPC_EXCP_BRANCH: /* branch instruction: */
1683 /* We just stopped because of a branch. Go on */
1685 case POWERPC_EXCP_SYSCALL_USER:
1686 /* system call in user-mode emulation */
1688 * PPC ABI uses overflow flag in cr0 to signal an error
1692 printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0],
1693 env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]);
1695 env->crf[0] &= ~0x1;
1696 ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
1697 env->gpr[5], env->gpr[6], env->gpr[7],
1699 if (ret == (uint32_t)(-TARGET_QEMU_ESIGRETURN)) {
1700 /* Returning from a successful sigreturn syscall.
1701 Avoid corrupting register state. */
1704 if (ret > (uint32_t)(-515)) {
1710 printf("syscall returned 0x%08x (%d)\n", ret, ret);
1713 case POWERPC_EXCP_STCX:
1714 if (do_store_exclusive(env)) {
1715 info.si_signo = TARGET_SIGSEGV;
1717 info.si_code = TARGET_SEGV_MAPERR;
1718 info._sifields._sigfault._addr = env->nip;
1719 queue_signal(env, info.si_signo, &info);
1726 sig = gdb_handlesig(env, TARGET_SIGTRAP);
1728 info.si_signo = sig;
1730 info.si_code = TARGET_TRAP_BRKPT;
1731 queue_signal(env, info.si_signo, &info);
1735 case EXCP_INTERRUPT:
1736 /* just indicate that signals should be handled asap */
1739 cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
1742 process_pending_signals(env);
1749 #define MIPS_SYS(name, args) args,
1751 static const uint8_t mips_syscall_args[] = {
1752 MIPS_SYS(sys_syscall , 8) /* 4000 */
1753 MIPS_SYS(sys_exit , 1)
1754 MIPS_SYS(sys_fork , 0)
1755 MIPS_SYS(sys_read , 3)
1756 MIPS_SYS(sys_write , 3)
1757 MIPS_SYS(sys_open , 3) /* 4005 */
1758 MIPS_SYS(sys_close , 1)
1759 MIPS_SYS(sys_waitpid , 3)
1760 MIPS_SYS(sys_creat , 2)
1761 MIPS_SYS(sys_link , 2)
1762 MIPS_SYS(sys_unlink , 1) /* 4010 */
1763 MIPS_SYS(sys_execve , 0)
1764 MIPS_SYS(sys_chdir , 1)
1765 MIPS_SYS(sys_time , 1)
1766 MIPS_SYS(sys_mknod , 3)
1767 MIPS_SYS(sys_chmod , 2) /* 4015 */
1768 MIPS_SYS(sys_lchown , 3)
1769 MIPS_SYS(sys_ni_syscall , 0)
1770 MIPS_SYS(sys_ni_syscall , 0) /* was sys_stat */
1771 MIPS_SYS(sys_lseek , 3)
1772 MIPS_SYS(sys_getpid , 0) /* 4020 */
1773 MIPS_SYS(sys_mount , 5)
1774 MIPS_SYS(sys_oldumount , 1)
1775 MIPS_SYS(sys_setuid , 1)
1776 MIPS_SYS(sys_getuid , 0)
1777 MIPS_SYS(sys_stime , 1) /* 4025 */
1778 MIPS_SYS(sys_ptrace , 4)
1779 MIPS_SYS(sys_alarm , 1)
1780 MIPS_SYS(sys_ni_syscall , 0) /* was sys_fstat */
1781 MIPS_SYS(sys_pause , 0)
1782 MIPS_SYS(sys_utime , 2) /* 4030 */
1783 MIPS_SYS(sys_ni_syscall , 0)
1784 MIPS_SYS(sys_ni_syscall , 0)
1785 MIPS_SYS(sys_access , 2)
1786 MIPS_SYS(sys_nice , 1)
1787 MIPS_SYS(sys_ni_syscall , 0) /* 4035 */
1788 MIPS_SYS(sys_sync , 0)
1789 MIPS_SYS(sys_kill , 2)
1790 MIPS_SYS(sys_rename , 2)
1791 MIPS_SYS(sys_mkdir , 2)
1792 MIPS_SYS(sys_rmdir , 1) /* 4040 */
1793 MIPS_SYS(sys_dup , 1)
1794 MIPS_SYS(sys_pipe , 0)
1795 MIPS_SYS(sys_times , 1)
1796 MIPS_SYS(sys_ni_syscall , 0)
1797 MIPS_SYS(sys_brk , 1) /* 4045 */
1798 MIPS_SYS(sys_setgid , 1)
1799 MIPS_SYS(sys_getgid , 0)
1800 MIPS_SYS(sys_ni_syscall , 0) /* was signal(2) */
1801 MIPS_SYS(sys_geteuid , 0)
1802 MIPS_SYS(sys_getegid , 0) /* 4050 */
1803 MIPS_SYS(sys_acct , 0)
1804 MIPS_SYS(sys_umount , 2)
1805 MIPS_SYS(sys_ni_syscall , 0)
1806 MIPS_SYS(sys_ioctl , 3)
1807 MIPS_SYS(sys_fcntl , 3) /* 4055 */
1808 MIPS_SYS(sys_ni_syscall , 2)
1809 MIPS_SYS(sys_setpgid , 2)
1810 MIPS_SYS(sys_ni_syscall , 0)
1811 MIPS_SYS(sys_olduname , 1)
1812 MIPS_SYS(sys_umask , 1) /* 4060 */
1813 MIPS_SYS(sys_chroot , 1)
1814 MIPS_SYS(sys_ustat , 2)
1815 MIPS_SYS(sys_dup2 , 2)
1816 MIPS_SYS(sys_getppid , 0)
1817 MIPS_SYS(sys_getpgrp , 0) /* 4065 */
1818 MIPS_SYS(sys_setsid , 0)
1819 MIPS_SYS(sys_sigaction , 3)
1820 MIPS_SYS(sys_sgetmask , 0)
1821 MIPS_SYS(sys_ssetmask , 1)
1822 MIPS_SYS(sys_setreuid , 2) /* 4070 */
1823 MIPS_SYS(sys_setregid , 2)
1824 MIPS_SYS(sys_sigsuspend , 0)
1825 MIPS_SYS(sys_sigpending , 1)
1826 MIPS_SYS(sys_sethostname , 2)
1827 MIPS_SYS(sys_setrlimit , 2) /* 4075 */
1828 MIPS_SYS(sys_getrlimit , 2)
1829 MIPS_SYS(sys_getrusage , 2)
1830 MIPS_SYS(sys_gettimeofday, 2)
1831 MIPS_SYS(sys_settimeofday, 2)
1832 MIPS_SYS(sys_getgroups , 2) /* 4080 */
1833 MIPS_SYS(sys_setgroups , 2)
1834 MIPS_SYS(sys_ni_syscall , 0) /* old_select */
1835 MIPS_SYS(sys_symlink , 2)
1836 MIPS_SYS(sys_ni_syscall , 0) /* was sys_lstat */
1837 MIPS_SYS(sys_readlink , 3) /* 4085 */
1838 MIPS_SYS(sys_uselib , 1)
1839 MIPS_SYS(sys_swapon , 2)
1840 MIPS_SYS(sys_reboot , 3)
1841 MIPS_SYS(old_readdir , 3)
1842 MIPS_SYS(old_mmap , 6) /* 4090 */
1843 MIPS_SYS(sys_munmap , 2)
1844 MIPS_SYS(sys_truncate , 2)
1845 MIPS_SYS(sys_ftruncate , 2)
1846 MIPS_SYS(sys_fchmod , 2)
1847 MIPS_SYS(sys_fchown , 3) /* 4095 */
1848 MIPS_SYS(sys_getpriority , 2)
1849 MIPS_SYS(sys_setpriority , 3)
1850 MIPS_SYS(sys_ni_syscall , 0)
1851 MIPS_SYS(sys_statfs , 2)
1852 MIPS_SYS(sys_fstatfs , 2) /* 4100 */
1853 MIPS_SYS(sys_ni_syscall , 0) /* was ioperm(2) */
1854 MIPS_SYS(sys_socketcall , 2)
1855 MIPS_SYS(sys_syslog , 3)
1856 MIPS_SYS(sys_setitimer , 3)
1857 MIPS_SYS(sys_getitimer , 2) /* 4105 */
1858 MIPS_SYS(sys_newstat , 2)
1859 MIPS_SYS(sys_newlstat , 2)
1860 MIPS_SYS(sys_newfstat , 2)
1861 MIPS_SYS(sys_uname , 1)
1862 MIPS_SYS(sys_ni_syscall , 0) /* 4110 was iopl(2) */
1863 MIPS_SYS(sys_vhangup , 0)
1864 MIPS_SYS(sys_ni_syscall , 0) /* was sys_idle() */
1865 MIPS_SYS(sys_ni_syscall , 0) /* was sys_vm86 */
1866 MIPS_SYS(sys_wait4 , 4)
1867 MIPS_SYS(sys_swapoff , 1) /* 4115 */
1868 MIPS_SYS(sys_sysinfo , 1)
1869 MIPS_SYS(sys_ipc , 6)
1870 MIPS_SYS(sys_fsync , 1)
1871 MIPS_SYS(sys_sigreturn , 0)
1872 MIPS_SYS(sys_clone , 6) /* 4120 */
1873 MIPS_SYS(sys_setdomainname, 2)
1874 MIPS_SYS(sys_newuname , 1)
1875 MIPS_SYS(sys_ni_syscall , 0) /* sys_modify_ldt */
1876 MIPS_SYS(sys_adjtimex , 1)
1877 MIPS_SYS(sys_mprotect , 3) /* 4125 */
1878 MIPS_SYS(sys_sigprocmask , 3)
1879 MIPS_SYS(sys_ni_syscall , 0) /* was create_module */
1880 MIPS_SYS(sys_init_module , 5)
1881 MIPS_SYS(sys_delete_module, 1)
1882 MIPS_SYS(sys_ni_syscall , 0) /* 4130 was get_kernel_syms */
1883 MIPS_SYS(sys_quotactl , 0)
1884 MIPS_SYS(sys_getpgid , 1)
1885 MIPS_SYS(sys_fchdir , 1)
1886 MIPS_SYS(sys_bdflush , 2)
1887 MIPS_SYS(sys_sysfs , 3) /* 4135 */
1888 MIPS_SYS(sys_personality , 1)
1889 MIPS_SYS(sys_ni_syscall , 0) /* for afs_syscall */
1890 MIPS_SYS(sys_setfsuid , 1)
1891 MIPS_SYS(sys_setfsgid , 1)
1892 MIPS_SYS(sys_llseek , 5) /* 4140 */
1893 MIPS_SYS(sys_getdents , 3)
1894 MIPS_SYS(sys_select , 5)
1895 MIPS_SYS(sys_flock , 2)
1896 MIPS_SYS(sys_msync , 3)
1897 MIPS_SYS(sys_readv , 3) /* 4145 */
1898 MIPS_SYS(sys_writev , 3)
1899 MIPS_SYS(sys_cacheflush , 3)
1900 MIPS_SYS(sys_cachectl , 3)
1901 MIPS_SYS(sys_sysmips , 4)
1902 MIPS_SYS(sys_ni_syscall , 0) /* 4150 */
1903 MIPS_SYS(sys_getsid , 1)
1904 MIPS_SYS(sys_fdatasync , 0)
1905 MIPS_SYS(sys_sysctl , 1)
1906 MIPS_SYS(sys_mlock , 2)
1907 MIPS_SYS(sys_munlock , 2) /* 4155 */
1908 MIPS_SYS(sys_mlockall , 1)
1909 MIPS_SYS(sys_munlockall , 0)
1910 MIPS_SYS(sys_sched_setparam, 2)
1911 MIPS_SYS(sys_sched_getparam, 2)
1912 MIPS_SYS(sys_sched_setscheduler, 3) /* 4160 */
1913 MIPS_SYS(sys_sched_getscheduler, 1)
1914 MIPS_SYS(sys_sched_yield , 0)
1915 MIPS_SYS(sys_sched_get_priority_max, 1)
1916 MIPS_SYS(sys_sched_get_priority_min, 1)
1917 MIPS_SYS(sys_sched_rr_get_interval, 2) /* 4165 */
1918 MIPS_SYS(sys_nanosleep, 2)
1919 MIPS_SYS(sys_mremap , 4)
1920 MIPS_SYS(sys_accept , 3)
1921 MIPS_SYS(sys_bind , 3)
1922 MIPS_SYS(sys_connect , 3) /* 4170 */
1923 MIPS_SYS(sys_getpeername , 3)
1924 MIPS_SYS(sys_getsockname , 3)
1925 MIPS_SYS(sys_getsockopt , 5)
1926 MIPS_SYS(sys_listen , 2)
1927 MIPS_SYS(sys_recv , 4) /* 4175 */
1928 MIPS_SYS(sys_recvfrom , 6)
1929 MIPS_SYS(sys_recvmsg , 3)
1930 MIPS_SYS(sys_send , 4)
1931 MIPS_SYS(sys_sendmsg , 3)
1932 MIPS_SYS(sys_sendto , 6) /* 4180 */
1933 MIPS_SYS(sys_setsockopt , 5)
1934 MIPS_SYS(sys_shutdown , 2)
1935 MIPS_SYS(sys_socket , 3)
1936 MIPS_SYS(sys_socketpair , 4)
1937 MIPS_SYS(sys_setresuid , 3) /* 4185 */
1938 MIPS_SYS(sys_getresuid , 3)
1939 MIPS_SYS(sys_ni_syscall , 0) /* was sys_query_module */
1940 MIPS_SYS(sys_poll , 3)
1941 MIPS_SYS(sys_nfsservctl , 3)
1942 MIPS_SYS(sys_setresgid , 3) /* 4190 */
1943 MIPS_SYS(sys_getresgid , 3)
1944 MIPS_SYS(sys_prctl , 5)
1945 MIPS_SYS(sys_rt_sigreturn, 0)
1946 MIPS_SYS(sys_rt_sigaction, 4)
1947 MIPS_SYS(sys_rt_sigprocmask, 4) /* 4195 */
1948 MIPS_SYS(sys_rt_sigpending, 2)
1949 MIPS_SYS(sys_rt_sigtimedwait, 4)
1950 MIPS_SYS(sys_rt_sigqueueinfo, 3)
1951 MIPS_SYS(sys_rt_sigsuspend, 0)
1952 MIPS_SYS(sys_pread64 , 6) /* 4200 */
1953 MIPS_SYS(sys_pwrite64 , 6)
1954 MIPS_SYS(sys_chown , 3)
1955 MIPS_SYS(sys_getcwd , 2)
1956 MIPS_SYS(sys_capget , 2)
1957 MIPS_SYS(sys_capset , 2) /* 4205 */
1958 MIPS_SYS(sys_sigaltstack , 2)
1959 MIPS_SYS(sys_sendfile , 4)
1960 MIPS_SYS(sys_ni_syscall , 0)
1961 MIPS_SYS(sys_ni_syscall , 0)
1962 MIPS_SYS(sys_mmap2 , 6) /* 4210 */
1963 MIPS_SYS(sys_truncate64 , 4)
1964 MIPS_SYS(sys_ftruncate64 , 4)
1965 MIPS_SYS(sys_stat64 , 2)
1966 MIPS_SYS(sys_lstat64 , 2)
1967 MIPS_SYS(sys_fstat64 , 2) /* 4215 */
1968 MIPS_SYS(sys_pivot_root , 2)
1969 MIPS_SYS(sys_mincore , 3)
1970 MIPS_SYS(sys_madvise , 3)
1971 MIPS_SYS(sys_getdents64 , 3)
1972 MIPS_SYS(sys_fcntl64 , 3) /* 4220 */
1973 MIPS_SYS(sys_ni_syscall , 0)
1974 MIPS_SYS(sys_gettid , 0)
1975 MIPS_SYS(sys_readahead , 5)
1976 MIPS_SYS(sys_setxattr , 5)
1977 MIPS_SYS(sys_lsetxattr , 5) /* 4225 */
1978 MIPS_SYS(sys_fsetxattr , 5)
1979 MIPS_SYS(sys_getxattr , 4)
1980 MIPS_SYS(sys_lgetxattr , 4)
1981 MIPS_SYS(sys_fgetxattr , 4)
1982 MIPS_SYS(sys_listxattr , 3) /* 4230 */
1983 MIPS_SYS(sys_llistxattr , 3)
1984 MIPS_SYS(sys_flistxattr , 3)
1985 MIPS_SYS(sys_removexattr , 2)
1986 MIPS_SYS(sys_lremovexattr, 2)
1987 MIPS_SYS(sys_fremovexattr, 2) /* 4235 */
1988 MIPS_SYS(sys_tkill , 2)
1989 MIPS_SYS(sys_sendfile64 , 5)
1990 MIPS_SYS(sys_futex , 2)
1991 MIPS_SYS(sys_sched_setaffinity, 3)
1992 MIPS_SYS(sys_sched_getaffinity, 3) /* 4240 */
1993 MIPS_SYS(sys_io_setup , 2)
1994 MIPS_SYS(sys_io_destroy , 1)
1995 MIPS_SYS(sys_io_getevents, 5)
1996 MIPS_SYS(sys_io_submit , 3)
1997 MIPS_SYS(sys_io_cancel , 3) /* 4245 */
1998 MIPS_SYS(sys_exit_group , 1)
1999 MIPS_SYS(sys_lookup_dcookie, 3)
2000 MIPS_SYS(sys_epoll_create, 1)
2001 MIPS_SYS(sys_epoll_ctl , 4)
2002 MIPS_SYS(sys_epoll_wait , 3) /* 4250 */
2003 MIPS_SYS(sys_remap_file_pages, 5)
2004 MIPS_SYS(sys_set_tid_address, 1)
2005 MIPS_SYS(sys_restart_syscall, 0)
2006 MIPS_SYS(sys_fadvise64_64, 7)
2007 MIPS_SYS(sys_statfs64 , 3) /* 4255 */
2008 MIPS_SYS(sys_fstatfs64 , 2)
2009 MIPS_SYS(sys_timer_create, 3)
2010 MIPS_SYS(sys_timer_settime, 4)
2011 MIPS_SYS(sys_timer_gettime, 2)
2012 MIPS_SYS(sys_timer_getoverrun, 1) /* 4260 */
2013 MIPS_SYS(sys_timer_delete, 1)
2014 MIPS_SYS(sys_clock_settime, 2)
2015 MIPS_SYS(sys_clock_gettime, 2)
2016 MIPS_SYS(sys_clock_getres, 2)
2017 MIPS_SYS(sys_clock_nanosleep, 4) /* 4265 */
2018 MIPS_SYS(sys_tgkill , 3)
2019 MIPS_SYS(sys_utimes , 2)
2020 MIPS_SYS(sys_mbind , 4)
2021 MIPS_SYS(sys_ni_syscall , 0) /* sys_get_mempolicy */
2022 MIPS_SYS(sys_ni_syscall , 0) /* 4270 sys_set_mempolicy */
2023 MIPS_SYS(sys_mq_open , 4)
2024 MIPS_SYS(sys_mq_unlink , 1)
2025 MIPS_SYS(sys_mq_timedsend, 5)
2026 MIPS_SYS(sys_mq_timedreceive, 5)
2027 MIPS_SYS(sys_mq_notify , 2) /* 4275 */
2028 MIPS_SYS(sys_mq_getsetattr, 3)
2029 MIPS_SYS(sys_ni_syscall , 0) /* sys_vserver */
2030 MIPS_SYS(sys_waitid , 4)
2031 MIPS_SYS(sys_ni_syscall , 0) /* available, was setaltroot */
2032 MIPS_SYS(sys_add_key , 5)
2033 MIPS_SYS(sys_request_key, 4)
2034 MIPS_SYS(sys_keyctl , 5)
2035 MIPS_SYS(sys_set_thread_area, 1)
2036 MIPS_SYS(sys_inotify_init, 0)
2037 MIPS_SYS(sys_inotify_add_watch, 3) /* 4285 */
2038 MIPS_SYS(sys_inotify_rm_watch, 2)
2039 MIPS_SYS(sys_migrate_pages, 4)
2040 MIPS_SYS(sys_openat, 4)
2041 MIPS_SYS(sys_mkdirat, 3)
2042 MIPS_SYS(sys_mknodat, 4) /* 4290 */
2043 MIPS_SYS(sys_fchownat, 5)
2044 MIPS_SYS(sys_futimesat, 3)
2045 MIPS_SYS(sys_fstatat64, 4)
2046 MIPS_SYS(sys_unlinkat, 3)
2047 MIPS_SYS(sys_renameat, 4) /* 4295 */
2048 MIPS_SYS(sys_linkat, 5)
2049 MIPS_SYS(sys_symlinkat, 3)
2050 MIPS_SYS(sys_readlinkat, 4)
2051 MIPS_SYS(sys_fchmodat, 3)
2052 MIPS_SYS(sys_faccessat, 3) /* 4300 */
2053 MIPS_SYS(sys_pselect6, 6)
2054 MIPS_SYS(sys_ppoll, 5)
2055 MIPS_SYS(sys_unshare, 1)
2056 MIPS_SYS(sys_splice, 4)
2057 MIPS_SYS(sys_sync_file_range, 7) /* 4305 */
2058 MIPS_SYS(sys_tee, 4)
2059 MIPS_SYS(sys_vmsplice, 4)
2060 MIPS_SYS(sys_move_pages, 6)
2061 MIPS_SYS(sys_set_robust_list, 2)
2062 MIPS_SYS(sys_get_robust_list, 3) /* 4310 */
2063 MIPS_SYS(sys_kexec_load, 4)
2064 MIPS_SYS(sys_getcpu, 3)
2065 MIPS_SYS(sys_epoll_pwait, 6)
2066 MIPS_SYS(sys_ioprio_set, 3)
2067 MIPS_SYS(sys_ioprio_get, 2)
2068 MIPS_SYS(sys_utimensat, 4)
2069 MIPS_SYS(sys_signalfd, 3)
2070 MIPS_SYS(sys_ni_syscall, 0) /* was timerfd */
2071 MIPS_SYS(sys_eventfd, 1)
2072 MIPS_SYS(sys_fallocate, 6) /* 4320 */
2073 MIPS_SYS(sys_timerfd_create, 2)
2074 MIPS_SYS(sys_timerfd_gettime, 2)
2075 MIPS_SYS(sys_timerfd_settime, 4)
2076 MIPS_SYS(sys_signalfd4, 4)
2077 MIPS_SYS(sys_eventfd2, 2) /* 4325 */
2078 MIPS_SYS(sys_epoll_create1, 1)
2079 MIPS_SYS(sys_dup3, 3)
2080 MIPS_SYS(sys_pipe2, 2)
2081 MIPS_SYS(sys_inotify_init1, 1)
2082 MIPS_SYS(sys_preadv, 6) /* 4330 */
2083 MIPS_SYS(sys_pwritev, 6)
2084 MIPS_SYS(sys_rt_tgsigqueueinfo, 4)
2085 MIPS_SYS(sys_perf_event_open, 5)
2086 MIPS_SYS(sys_accept4, 4)
2087 MIPS_SYS(sys_recvmmsg, 5) /* 4335 */
2088 MIPS_SYS(sys_fanotify_init, 2)
2089 MIPS_SYS(sys_fanotify_mark, 6)
2090 MIPS_SYS(sys_prlimit64, 4)
2091 MIPS_SYS(sys_name_to_handle_at, 5)
2092 MIPS_SYS(sys_open_by_handle_at, 3) /* 4340 */
2093 MIPS_SYS(sys_clock_adjtime, 2)
2094 MIPS_SYS(sys_syncfs, 1)
2099 static int do_store_exclusive(CPUMIPSState *env)
2102 target_ulong page_addr;
2110 page_addr = addr & TARGET_PAGE_MASK;
2113 flags = page_get_flags(page_addr);
2114 if ((flags & PAGE_READ) == 0) {
2117 reg = env->llreg & 0x1f;
2118 d = (env->llreg & 0x20) != 0;
2120 segv = get_user_s64(val, addr);
2122 segv = get_user_s32(val, addr);
2125 if (val != env->llval) {
2126 env->active_tc.gpr[reg] = 0;
2129 segv = put_user_u64(env->llnewval, addr);
2131 segv = put_user_u32(env->llnewval, addr);
2134 env->active_tc.gpr[reg] = 1;
2141 env->active_tc.PC += 4;
2148 void cpu_loop(CPUMIPSState *env)
2150 target_siginfo_t info;
2152 unsigned int syscall_num;
2155 cpu_exec_start(env);
2156 trapnr = cpu_mips_exec(env);
2160 syscall_num = env->active_tc.gpr[2] - 4000;
2161 env->active_tc.PC += 4;
2162 if (syscall_num >= sizeof(mips_syscall_args)) {
2163 ret = -TARGET_ENOSYS;
2167 abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
2169 nb_args = mips_syscall_args[syscall_num];
2170 sp_reg = env->active_tc.gpr[29];
2172 /* these arguments are taken from the stack */
2173 /* FIXME - what to do if get_user() fails? */
2174 case 8: get_user_ual(arg8, sp_reg + 28);
2175 case 7: get_user_ual(arg7, sp_reg + 24);
2176 case 6: get_user_ual(arg6, sp_reg + 20);
2177 case 5: get_user_ual(arg5, sp_reg + 16);
2181 ret = do_syscall(env, env->active_tc.gpr[2],
2182 env->active_tc.gpr[4],
2183 env->active_tc.gpr[5],
2184 env->active_tc.gpr[6],
2185 env->active_tc.gpr[7],
2186 arg5, arg6, arg7, arg8);
2188 if (ret == -TARGET_QEMU_ESIGRETURN) {
2189 /* Returning from a successful sigreturn syscall.
2190 Avoid clobbering register state. */
2193 if ((unsigned int)ret >= (unsigned int)(-1133)) {
2194 env->active_tc.gpr[7] = 1; /* error flag */
2197 env->active_tc.gpr[7] = 0; /* error flag */
2199 env->active_tc.gpr[2] = ret;
2205 info.si_signo = TARGET_SIGSEGV;
2207 /* XXX: check env->error_code */
2208 info.si_code = TARGET_SEGV_MAPERR;
2209 info._sifields._sigfault._addr = env->CP0_BadVAddr;
2210 queue_signal(env, info.si_signo, &info);
2214 info.si_signo = TARGET_SIGILL;
2217 queue_signal(env, info.si_signo, &info);
2219 case EXCP_INTERRUPT:
2220 /* just indicate that signals should be handled asap */
2226 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2229 info.si_signo = sig;
2231 info.si_code = TARGET_TRAP_BRKPT;
2232 queue_signal(env, info.si_signo, &info);
2237 if (do_store_exclusive(env)) {
2238 info.si_signo = TARGET_SIGSEGV;
2240 info.si_code = TARGET_SEGV_MAPERR;
2241 info._sifields._sigfault._addr = env->active_tc.PC;
2242 queue_signal(env, info.si_signo, &info);
2247 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
2249 cpu_dump_state(env, stderr, fprintf, 0);
2252 process_pending_signals(env);
2258 void cpu_loop (CPUState *env)
2261 target_siginfo_t info;
2264 trapnr = cpu_sh4_exec (env);
2269 ret = do_syscall(env,
2278 env->gregs[0] = ret;
2280 case EXCP_INTERRUPT:
2281 /* just indicate that signals should be handled asap */
2287 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2290 info.si_signo = sig;
2292 info.si_code = TARGET_TRAP_BRKPT;
2293 queue_signal(env, info.si_signo, &info);
2299 info.si_signo = SIGSEGV;
2301 info.si_code = TARGET_SEGV_MAPERR;
2302 info._sifields._sigfault._addr = env->tea;
2303 queue_signal(env, info.si_signo, &info);
2307 printf ("Unhandled trap: 0x%x\n", trapnr);
2308 cpu_dump_state(env, stderr, fprintf, 0);
2311 process_pending_signals (env);
2317 void cpu_loop (CPUState *env)
2320 target_siginfo_t info;
2323 trapnr = cpu_cris_exec (env);
2327 info.si_signo = SIGSEGV;
2329 /* XXX: check env->error_code */
2330 info.si_code = TARGET_SEGV_MAPERR;
2331 info._sifields._sigfault._addr = env->pregs[PR_EDA];
2332 queue_signal(env, info.si_signo, &info);
2335 case EXCP_INTERRUPT:
2336 /* just indicate that signals should be handled asap */
2339 ret = do_syscall(env,
2348 env->regs[10] = ret;
2354 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2357 info.si_signo = sig;
2359 info.si_code = TARGET_TRAP_BRKPT;
2360 queue_signal(env, info.si_signo, &info);
2365 printf ("Unhandled trap: 0x%x\n", trapnr);
2366 cpu_dump_state(env, stderr, fprintf, 0);
2369 process_pending_signals (env);
2374 #ifdef TARGET_MICROBLAZE
2375 void cpu_loop (CPUState *env)
2378 target_siginfo_t info;
2381 trapnr = cpu_mb_exec (env);
2385 info.si_signo = SIGSEGV;
2387 /* XXX: check env->error_code */
2388 info.si_code = TARGET_SEGV_MAPERR;
2389 info._sifields._sigfault._addr = 0;
2390 queue_signal(env, info.si_signo, &info);
2393 case EXCP_INTERRUPT:
2394 /* just indicate that signals should be handled asap */
2397 /* Return address is 4 bytes after the call. */
2399 ret = do_syscall(env,
2409 env->sregs[SR_PC] = env->regs[14];
2412 env->regs[17] = env->sregs[SR_PC] + 4;
2413 if (env->iflags & D_FLAG) {
2414 env->sregs[SR_ESR] |= 1 << 12;
2415 env->sregs[SR_PC] -= 4;
2416 /* FIXME: if branch was immed, replay the imm aswell. */
2419 env->iflags &= ~(IMM_FLAG | D_FLAG);
2421 switch (env->sregs[SR_ESR] & 31) {
2422 case ESR_EC_DIVZERO:
2423 info.si_signo = SIGFPE;
2425 info.si_code = TARGET_FPE_FLTDIV;
2426 info._sifields._sigfault._addr = 0;
2427 queue_signal(env, info.si_signo, &info);
2430 info.si_signo = SIGFPE;
2432 if (env->sregs[SR_FSR] & FSR_IO) {
2433 info.si_code = TARGET_FPE_FLTINV;
2435 if (env->sregs[SR_FSR] & FSR_DZ) {
2436 info.si_code = TARGET_FPE_FLTDIV;
2438 info._sifields._sigfault._addr = 0;
2439 queue_signal(env, info.si_signo, &info);
2442 printf ("Unhandled hw-exception: 0x%x\n",
2443 env->sregs[SR_ESR] & ESR_EC_MASK);
2444 cpu_dump_state(env, stderr, fprintf, 0);
2453 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2456 info.si_signo = sig;
2458 info.si_code = TARGET_TRAP_BRKPT;
2459 queue_signal(env, info.si_signo, &info);
2464 printf ("Unhandled trap: 0x%x\n", trapnr);
2465 cpu_dump_state(env, stderr, fprintf, 0);
2468 process_pending_signals (env);
2475 void cpu_loop(CPUM68KState *env)
2479 target_siginfo_t info;
2480 TaskState *ts = env->opaque;
2483 trapnr = cpu_m68k_exec(env);
2487 if (ts->sim_syscalls) {
2489 nr = lduw(env->pc + 2);
2491 do_m68k_simcall(env, nr);
2497 case EXCP_HALT_INSN:
2498 /* Semihosing syscall. */
2500 do_m68k_semihosting(env, env->dregs[0]);
2504 case EXCP_UNSUPPORTED:
2506 info.si_signo = SIGILL;
2508 info.si_code = TARGET_ILL_ILLOPN;
2509 info._sifields._sigfault._addr = env->pc;
2510 queue_signal(env, info.si_signo, &info);
2514 ts->sim_syscalls = 0;
2517 env->dregs[0] = do_syscall(env,
2528 case EXCP_INTERRUPT:
2529 /* just indicate that signals should be handled asap */
2533 info.si_signo = SIGSEGV;
2535 /* XXX: check env->error_code */
2536 info.si_code = TARGET_SEGV_MAPERR;
2537 info._sifields._sigfault._addr = env->mmu.ar;
2538 queue_signal(env, info.si_signo, &info);
2545 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2548 info.si_signo = sig;
2550 info.si_code = TARGET_TRAP_BRKPT;
2551 queue_signal(env, info.si_signo, &info);
2556 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
2558 cpu_dump_state(env, stderr, fprintf, 0);
2561 process_pending_signals(env);
2564 #endif /* TARGET_M68K */
2567 static void do_store_exclusive(CPUAlphaState *env, int reg, int quad)
2569 target_ulong addr, val, tmp;
2570 target_siginfo_t info;
2573 addr = env->lock_addr;
2574 tmp = env->lock_st_addr;
2575 env->lock_addr = -1;
2576 env->lock_st_addr = 0;
2582 if (quad ? get_user_s64(val, addr) : get_user_s32(val, addr)) {
2586 if (val == env->lock_value) {
2588 if (quad ? put_user_u64(tmp, addr) : put_user_u32(tmp, addr)) {
2605 info.si_signo = TARGET_SIGSEGV;
2607 info.si_code = TARGET_SEGV_MAPERR;
2608 info._sifields._sigfault._addr = addr;
2609 queue_signal(env, TARGET_SIGSEGV, &info);
2612 void cpu_loop (CPUState *env)
2615 target_siginfo_t info;
2619 trapnr = cpu_alpha_exec (env);
2621 /* All of the traps imply a transition through PALcode, which
2622 implies an REI instruction has been executed. Which means
2623 that the intr_flag should be cleared. */
2628 fprintf(stderr, "Reset requested. Exit\n");
2632 fprintf(stderr, "Machine check exception. Exit\n");
2635 case EXCP_SMP_INTERRUPT:
2636 case EXCP_CLK_INTERRUPT:
2637 case EXCP_DEV_INTERRUPT:
2638 fprintf(stderr, "External interrupt. Exit\n");
2642 env->lock_addr = -1;
2643 info.si_signo = TARGET_SIGSEGV;
2645 info.si_code = (page_get_flags(env->trap_arg0) & PAGE_VALID
2646 ? TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR);
2647 info._sifields._sigfault._addr = env->trap_arg0;
2648 queue_signal(env, info.si_signo, &info);
2651 env->lock_addr = -1;
2652 info.si_signo = TARGET_SIGBUS;
2654 info.si_code = TARGET_BUS_ADRALN;
2655 info._sifields._sigfault._addr = env->trap_arg0;
2656 queue_signal(env, info.si_signo, &info);
2660 env->lock_addr = -1;
2661 info.si_signo = TARGET_SIGILL;
2663 info.si_code = TARGET_ILL_ILLOPC;
2664 info._sifields._sigfault._addr = env->pc;
2665 queue_signal(env, info.si_signo, &info);
2668 env->lock_addr = -1;
2669 info.si_signo = TARGET_SIGFPE;
2671 info.si_code = TARGET_FPE_FLTINV;
2672 info._sifields._sigfault._addr = env->pc;
2673 queue_signal(env, info.si_signo, &info);
2676 /* No-op. Linux simply re-enables the FPU. */
2679 env->lock_addr = -1;
2680 switch (env->error_code) {
2683 info.si_signo = TARGET_SIGTRAP;
2685 info.si_code = TARGET_TRAP_BRKPT;
2686 info._sifields._sigfault._addr = env->pc;
2687 queue_signal(env, info.si_signo, &info);
2691 info.si_signo = TARGET_SIGTRAP;
2694 info._sifields._sigfault._addr = env->pc;
2695 queue_signal(env, info.si_signo, &info);
2699 trapnr = env->ir[IR_V0];
2700 sysret = do_syscall(env, trapnr,
2701 env->ir[IR_A0], env->ir[IR_A1],
2702 env->ir[IR_A2], env->ir[IR_A3],
2703 env->ir[IR_A4], env->ir[IR_A5],
2705 if (trapnr == TARGET_NR_sigreturn
2706 || trapnr == TARGET_NR_rt_sigreturn) {
2709 /* Syscall writes 0 to V0 to bypass error check, similar
2710 to how this is handled internal to Linux kernel. */
2711 if (env->ir[IR_V0] == 0) {
2712 env->ir[IR_V0] = sysret;
2714 env->ir[IR_V0] = (sysret < 0 ? -sysret : sysret);
2715 env->ir[IR_A3] = (sysret < 0);
2720 /* ??? We can probably elide the code using page_unprotect
2721 that is checking for self-modifying code. Instead we
2722 could simply call tb_flush here. Until we work out the
2723 changes required to turn off the extra write protection,
2724 this can be a no-op. */
2728 /* Handled in the translator for usermode. */
2732 /* Handled in the translator for usermode. */
2736 info.si_signo = TARGET_SIGFPE;
2737 switch (env->ir[IR_A0]) {
2738 case TARGET_GEN_INTOVF:
2739 info.si_code = TARGET_FPE_INTOVF;
2741 case TARGET_GEN_INTDIV:
2742 info.si_code = TARGET_FPE_INTDIV;
2744 case TARGET_GEN_FLTOVF:
2745 info.si_code = TARGET_FPE_FLTOVF;
2747 case TARGET_GEN_FLTUND:
2748 info.si_code = TARGET_FPE_FLTUND;
2750 case TARGET_GEN_FLTINV:
2751 info.si_code = TARGET_FPE_FLTINV;
2753 case TARGET_GEN_FLTINE:
2754 info.si_code = TARGET_FPE_FLTRES;
2756 case TARGET_GEN_ROPRAND:
2760 info.si_signo = TARGET_SIGTRAP;
2765 info._sifields._sigfault._addr = env->pc;
2766 queue_signal(env, info.si_signo, &info);
2773 info.si_signo = gdb_handlesig (env, TARGET_SIGTRAP);
2774 if (info.si_signo) {
2775 env->lock_addr = -1;
2777 info.si_code = TARGET_TRAP_BRKPT;
2778 queue_signal(env, info.si_signo, &info);
2783 do_store_exclusive(env, env->error_code, trapnr - EXCP_STL_C);
2786 printf ("Unhandled trap: 0x%x\n", trapnr);
2787 cpu_dump_state(env, stderr, fprintf, 0);
2790 process_pending_signals (env);
2793 #endif /* TARGET_ALPHA */
2796 void cpu_loop(CPUS390XState *env)
2799 target_siginfo_t info;
2802 trapnr = cpu_s390x_exec (env);
2805 case EXCP_INTERRUPT:
2806 /* just indicate that signals should be handled asap */
2812 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2814 info.si_signo = sig;
2816 info.si_code = TARGET_TRAP_BRKPT;
2817 queue_signal(env, info.si_signo, &info);
2823 int n = env->int_svc_code;
2825 /* syscalls > 255 */
2828 env->psw.addr += env->int_svc_ilc;
2829 env->regs[2] = do_syscall(env, n,
2841 info.si_signo = SIGSEGV;
2843 /* XXX: check env->error_code */
2844 info.si_code = TARGET_SEGV_MAPERR;
2845 info._sifields._sigfault._addr = env->__excp_addr;
2846 queue_signal(env, info.si_signo, &info);
2851 fprintf(stderr,"specification exception insn 0x%08x%04x\n", ldl(env->psw.addr), lduw(env->psw.addr + 4));
2852 info.si_signo = SIGILL;
2854 info.si_code = TARGET_ILL_ILLOPC;
2855 info._sifields._sigfault._addr = env->__excp_addr;
2856 queue_signal(env, info.si_signo, &info);
2860 printf ("Unhandled trap: 0x%x\n", trapnr);
2861 cpu_dump_state(env, stderr, fprintf, 0);
2864 process_pending_signals (env);
2868 #endif /* TARGET_S390X */
2870 static void version(void)
2872 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION QEMU_PKGVERSION
2873 ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2876 static void usage(void)
2879 printf("usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
2880 "Linux CPU emulator (compiled for %s emulation)\n"
2882 "Standard options:\n"
2883 "-h print this help\n"
2884 "-version display version information and exit\n"
2885 "-g port wait gdb connection to port\n"
2886 "-L path set the elf interpreter prefix (default=%s)\n"
2887 "-s size set the stack size in bytes (default=%ld)\n"
2888 "-cpu model select CPU (-cpu ? for list)\n"
2889 "-drop-ld-preload drop LD_PRELOAD for target process\n"
2890 "-E var=value sets/modifies targets environment variable(s)\n"
2891 "-U var unsets targets environment variable(s)\n"
2892 "-0 argv0 forces target process argv[0] to be argv0\n"
2893 #if defined(CONFIG_USE_GUEST_BASE)
2894 "-B address set guest_base address to address\n"
2895 "-R size reserve size bytes for guest virtual address space\n"
2899 "-d options activate log (logfile=%s)\n"
2900 "-p pagesize set the host page size to 'pagesize'\n"
2901 "-singlestep always run in singlestep mode\n"
2902 "-strace log system calls\n"
2904 "Environment variables:\n"
2905 "QEMU_STRACE Print system calls and arguments similar to the\n"
2906 " 'strace' program. Enable by setting to any value.\n"
2907 "You can use -E and -U options to set/unset environment variables\n"
2908 "for target process. It is possible to provide several variables\n"
2909 "by repeating the option. For example:\n"
2910 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
2911 "Note that if you provide several changes to single variable\n"
2912 "last change will stay in effect.\n"
2921 THREAD CPUState *thread_env;
2923 void task_settid(TaskState *ts)
2925 if (ts->ts_tid == 0) {
2926 #ifdef CONFIG_USE_NPTL
2927 ts->ts_tid = (pid_t)syscall(SYS_gettid);
2929 /* when no threads are used, tid becomes pid */
2930 ts->ts_tid = getpid();
2935 void stop_all_tasks(void)
2938 * We trust that when using NPTL, start_exclusive()
2939 * handles thread stopping correctly.
2944 /* Assumes contents are already zeroed. */
2945 void init_task_state(TaskState *ts)
2950 ts->first_free = ts->sigqueue_table;
2951 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
2952 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
2954 ts->sigqueue_table[i].next = NULL;
2957 int main(int argc, char **argv, char **envp)
2959 const char *filename;
2960 const char *cpu_model;
2961 const char *log_file = DEBUG_LOGFILE;
2962 const char *log_mask = NULL;
2963 struct target_pt_regs regs1, *regs = ®s1;
2964 struct image_info info1, *info = &info1;
2965 struct linux_binprm bprm;
2970 int gdbstub_port = 0;
2971 char **target_environ, **wrk;
2974 envlist_t *envlist = NULL;
2975 const char *argv0 = NULL;
2982 qemu_cache_utils_init(envp);
2984 if ((envlist = envlist_create()) == NULL) {
2985 (void) fprintf(stderr, "Unable to allocate envlist\n");
2989 /* add current environment into the list */
2990 for (wrk = environ; *wrk != NULL; wrk++) {
2991 (void) envlist_setenv(envlist, *wrk);
2994 /* Read the stack limit from the kernel. If it's "unlimited",
2995 then we can do little else besides use the default. */
2998 if (getrlimit(RLIMIT_STACK, &lim) == 0
2999 && lim.rlim_cur != RLIM_INFINITY
3000 && lim.rlim_cur == (target_long)lim.rlim_cur) {
3001 guest_stack_size = lim.rlim_cur;
3006 #if defined(cpudef_setup)
3007 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
3019 if (!strcmp(r, "-")) {
3021 } else if (!strcmp(r, "d")) {
3022 if (optind >= argc) {
3025 log_mask = argv[optind++];
3026 } else if (!strcmp(r, "D")) {
3027 if (optind >= argc) {
3030 log_file = argv[optind++];
3031 } else if (!strcmp(r, "E")) {
3033 if (envlist_setenv(envlist, r) != 0)
3035 } else if (!strcmp(r, "ignore-environment")) {
3036 envlist_free(envlist);
3037 if ((envlist = envlist_create()) == NULL) {
3038 (void) fprintf(stderr, "Unable to allocate envlist\n");
3041 } else if (!strcmp(r, "U")) {
3043 if (envlist_unsetenv(envlist, r) != 0)
3045 } else if (!strcmp(r, "0")) {
3048 } else if (!strcmp(r, "s")) {
3052 guest_stack_size = strtoul(r, (char **)&r, 0);
3053 if (guest_stack_size == 0)
3056 guest_stack_size *= 1024 * 1024;
3057 else if (*r == 'k' || *r == 'K')
3058 guest_stack_size *= 1024;
3059 } else if (!strcmp(r, "L")) {
3060 interp_prefix = argv[optind++];
3061 } else if (!strcmp(r, "p")) {
3064 qemu_host_page_size = atoi(argv[optind++]);
3065 if (qemu_host_page_size == 0 ||
3066 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
3067 fprintf(stderr, "page size must be a power of two\n");
3070 } else if (!strcmp(r, "g")) {
3073 gdbstub_port = atoi(argv[optind++]);
3074 } else if (!strcmp(r, "r")) {
3075 qemu_uname_release = argv[optind++];
3076 } else if (!strcmp(r, "cpu")) {
3077 cpu_model = argv[optind++];
3078 if (cpu_model == NULL || strcmp(cpu_model, "?") == 0) {
3079 /* XXX: implement xxx_cpu_list for targets that still miss it */
3080 #if defined(cpu_list_id)
3081 cpu_list_id(stdout, &fprintf, "");
3082 #elif defined(cpu_list)
3083 cpu_list(stdout, &fprintf); /* deprecated */
3087 #if defined(CONFIG_USE_GUEST_BASE)
3088 } else if (!strcmp(r, "B")) {
3089 guest_base = strtol(argv[optind++], NULL, 0);
3090 have_guest_base = 1;
3091 } else if (!strcmp(r, "R")) {
3094 reserved_va = strtoul(argv[optind++], &p, 0);
3108 unsigned long unshifted = reserved_va;
3110 reserved_va <<= shift;
3111 if (((reserved_va >> shift) != unshifted)
3112 #if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
3113 || (reserved_va > (1ul << TARGET_VIRT_ADDR_SPACE_BITS))
3116 fprintf(stderr, "Reserved virtual address too big\n");
3121 fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
3125 } else if (!strcmp(r, "drop-ld-preload")) {
3126 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
3127 } else if (!strcmp(r, "singlestep")) {
3129 } else if (!strcmp(r, "strace")) {
3131 } else if (!strcmp(r, "version")) {
3139 cpu_set_log_filename(log_file);
3142 const CPULogItem *item;
3144 mask = cpu_str_to_log_mask(log_mask);
3146 printf("Log items (comma separated):\n");
3147 for (item = cpu_log_items; item->mask != 0; item++) {
3148 printf("%-10s %s\n", item->name, item->help);
3155 if (optind >= argc) {
3158 filename = argv[optind];
3159 exec_path = argv[optind];
3162 memset(regs, 0, sizeof(struct target_pt_regs));
3164 /* Zero out image_info */
3165 memset(info, 0, sizeof(struct image_info));
3167 memset(&bprm, 0, sizeof (bprm));
3169 /* Scan interp_prefix dir for replacement files. */
3170 init_paths(interp_prefix);
3172 if (cpu_model == NULL) {
3173 #if defined(TARGET_I386)
3174 #ifdef TARGET_X86_64
3175 cpu_model = "qemu64";
3177 cpu_model = "qemu32";
3179 #elif defined(TARGET_ARM)
3181 #elif defined(TARGET_UNICORE32)
3183 #elif defined(TARGET_M68K)
3185 #elif defined(TARGET_SPARC)
3186 #ifdef TARGET_SPARC64
3187 cpu_model = "TI UltraSparc II";
3189 cpu_model = "Fujitsu MB86904";
3191 #elif defined(TARGET_MIPS)
3192 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
3197 #elif defined(TARGET_PPC)
3199 cpu_model = "970fx";
3208 cpu_exec_init_all();
3209 /* NOTE: we need to init the CPU at this stage to get
3210 qemu_host_page_size */
3211 env = cpu_init(cpu_model);
3213 fprintf(stderr, "Unable to find CPU definition\n");
3216 #if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3222 if (getenv("QEMU_STRACE")) {
3226 target_environ = envlist_to_environ(envlist, NULL);
3227 envlist_free(envlist);
3229 #if defined(CONFIG_USE_GUEST_BASE)
3231 * Now that page sizes are configured in cpu_init() we can do
3232 * proper page alignment for guest_base.
3234 guest_base = HOST_PAGE_ALIGN(guest_base);
3240 flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
3241 if (have_guest_base) {
3244 p = mmap((void *)guest_base, reserved_va, PROT_NONE, flags, -1, 0);
3245 if (p == MAP_FAILED) {
3246 fprintf(stderr, "Unable to reserve guest address space\n");
3249 guest_base = (unsigned long)p;
3250 /* Make sure the address is properly aligned. */
3251 if (guest_base & ~qemu_host_page_mask) {
3252 munmap(p, reserved_va);
3253 p = mmap((void *)guest_base, reserved_va + qemu_host_page_size,
3254 PROT_NONE, flags, -1, 0);
3255 if (p == MAP_FAILED) {
3256 fprintf(stderr, "Unable to reserve guest address space\n");
3259 guest_base = HOST_PAGE_ALIGN((unsigned long)p);
3261 qemu_log("Reserved 0x%lx bytes of guest address space\n", reserved_va);
3264 if (reserved_va || have_guest_base) {
3265 if (!guest_validate_base(guest_base)) {
3266 fprintf(stderr, "Guest base/Reserved VA rejected by guest code\n");
3270 #endif /* CONFIG_USE_GUEST_BASE */
3273 * Read in mmap_min_addr kernel parameter. This value is used
3274 * When loading the ELF image to determine whether guest_base
3275 * is needed. It is also used in mmap_find_vma.
3280 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
3282 if (fscanf(fp, "%lu", &tmp) == 1) {
3283 mmap_min_addr = tmp;
3284 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr);
3291 * Prepare copy of argv vector for target.
3293 target_argc = argc - optind;
3294 target_argv = calloc(target_argc + 1, sizeof (char *));
3295 if (target_argv == NULL) {
3296 (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
3301 * If argv0 is specified (using '-0' switch) we replace
3302 * argv[0] pointer with the given one.
3305 if (argv0 != NULL) {
3306 target_argv[i++] = strdup(argv0);
3308 for (; i < target_argc; i++) {
3309 target_argv[i] = strdup(argv[optind + i]);
3311 target_argv[target_argc] = NULL;
3313 ts = g_malloc0 (sizeof(TaskState));
3314 init_task_state(ts);
3315 /* build Task State */
3321 ret = loader_exec(filename, target_argv, target_environ, regs,
3324 printf("Error %d while loading %s\n", ret, filename);
3328 for (i = 0; i < target_argc; i++) {
3329 free(target_argv[i]);
3333 for (wrk = target_environ; *wrk; wrk++) {
3337 free(target_environ);
3339 if (qemu_log_enabled()) {
3340 #if defined(CONFIG_USE_GUEST_BASE)
3341 qemu_log("guest_base 0x%lx\n", guest_base);
3345 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
3346 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
3347 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
3349 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
3351 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
3352 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
3354 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
3355 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
3358 target_set_brk(info->brk);
3362 #if defined(CONFIG_USE_GUEST_BASE)
3363 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
3364 generating the prologue until now so that the prologue can take
3365 the real value of GUEST_BASE into account. */
3366 tcg_prologue_init(&tcg_ctx);
3369 #if defined(TARGET_I386)
3370 cpu_x86_set_cpl(env, 3);
3372 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
3373 env->hflags |= HF_PE_MASK;
3374 if (env->cpuid_features & CPUID_SSE) {
3375 env->cr[4] |= CR4_OSFXSR_MASK;
3376 env->hflags |= HF_OSFXSR_MASK;
3378 #ifndef TARGET_ABI32
3379 /* enable 64 bit mode if possible */
3380 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
3381 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
3384 env->cr[4] |= CR4_PAE_MASK;
3385 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
3386 env->hflags |= HF_LMA_MASK;
3389 /* flags setup : we activate the IRQs by default as in user mode */
3390 env->eflags |= IF_MASK;
3392 /* linux register setup */
3393 #ifndef TARGET_ABI32
3394 env->regs[R_EAX] = regs->rax;
3395 env->regs[R_EBX] = regs->rbx;
3396 env->regs[R_ECX] = regs->rcx;
3397 env->regs[R_EDX] = regs->rdx;
3398 env->regs[R_ESI] = regs->rsi;
3399 env->regs[R_EDI] = regs->rdi;
3400 env->regs[R_EBP] = regs->rbp;
3401 env->regs[R_ESP] = regs->rsp;
3402 env->eip = regs->rip;
3404 env->regs[R_EAX] = regs->eax;
3405 env->regs[R_EBX] = regs->ebx;
3406 env->regs[R_ECX] = regs->ecx;
3407 env->regs[R_EDX] = regs->edx;
3408 env->regs[R_ESI] = regs->esi;
3409 env->regs[R_EDI] = regs->edi;
3410 env->regs[R_EBP] = regs->ebp;
3411 env->regs[R_ESP] = regs->esp;
3412 env->eip = regs->eip;
3415 /* linux interrupt setup */
3416 #ifndef TARGET_ABI32
3417 env->idt.limit = 511;
3419 env->idt.limit = 255;
3421 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
3422 PROT_READ|PROT_WRITE,
3423 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3424 idt_table = g2h(env->idt.base);
3447 /* linux segment setup */
3449 uint64_t *gdt_table;
3450 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
3451 PROT_READ|PROT_WRITE,
3452 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3453 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
3454 gdt_table = g2h(env->gdt.base);
3456 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
3457 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
3458 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
3460 /* 64 bit code segment */
3461 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
3462 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
3464 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
3466 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
3467 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
3468 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
3470 cpu_x86_load_seg(env, R_CS, __USER_CS);
3471 cpu_x86_load_seg(env, R_SS, __USER_DS);
3473 cpu_x86_load_seg(env, R_DS, __USER_DS);
3474 cpu_x86_load_seg(env, R_ES, __USER_DS);
3475 cpu_x86_load_seg(env, R_FS, __USER_DS);
3476 cpu_x86_load_seg(env, R_GS, __USER_DS);
3477 /* This hack makes Wine work... */
3478 env->segs[R_FS].selector = 0;
3480 cpu_x86_load_seg(env, R_DS, 0);
3481 cpu_x86_load_seg(env, R_ES, 0);
3482 cpu_x86_load_seg(env, R_FS, 0);
3483 cpu_x86_load_seg(env, R_GS, 0);
3485 #elif defined(TARGET_ARM)
3488 cpsr_write(env, regs->uregs[16], 0xffffffff);
3489 for(i = 0; i < 16; i++) {
3490 env->regs[i] = regs->uregs[i];
3493 #elif defined(TARGET_UNICORE32)
3496 cpu_asr_write(env, regs->uregs[32], 0xffffffff);
3497 for (i = 0; i < 32; i++) {
3498 env->regs[i] = regs->uregs[i];
3501 #elif defined(TARGET_SPARC)
3505 env->npc = regs->npc;
3507 for(i = 0; i < 8; i++)
3508 env->gregs[i] = regs->u_regs[i];
3509 for(i = 0; i < 8; i++)
3510 env->regwptr[i] = regs->u_regs[i + 8];
3512 #elif defined(TARGET_PPC)
3516 #if defined(TARGET_PPC64)
3517 #if defined(TARGET_ABI32)
3518 env->msr &= ~((target_ulong)1 << MSR_SF);
3520 env->msr |= (target_ulong)1 << MSR_SF;
3523 env->nip = regs->nip;
3524 for(i = 0; i < 32; i++) {
3525 env->gpr[i] = regs->gpr[i];
3528 #elif defined(TARGET_M68K)
3531 env->dregs[0] = regs->d0;
3532 env->dregs[1] = regs->d1;
3533 env->dregs[2] = regs->d2;
3534 env->dregs[3] = regs->d3;
3535 env->dregs[4] = regs->d4;
3536 env->dregs[5] = regs->d5;
3537 env->dregs[6] = regs->d6;
3538 env->dregs[7] = regs->d7;
3539 env->aregs[0] = regs->a0;
3540 env->aregs[1] = regs->a1;
3541 env->aregs[2] = regs->a2;
3542 env->aregs[3] = regs->a3;
3543 env->aregs[4] = regs->a4;
3544 env->aregs[5] = regs->a5;
3545 env->aregs[6] = regs->a6;
3546 env->aregs[7] = regs->usp;
3548 ts->sim_syscalls = 1;
3550 #elif defined(TARGET_MICROBLAZE)
3552 env->regs[0] = regs->r0;
3553 env->regs[1] = regs->r1;
3554 env->regs[2] = regs->r2;
3555 env->regs[3] = regs->r3;
3556 env->regs[4] = regs->r4;
3557 env->regs[5] = regs->r5;
3558 env->regs[6] = regs->r6;
3559 env->regs[7] = regs->r7;
3560 env->regs[8] = regs->r8;
3561 env->regs[9] = regs->r9;
3562 env->regs[10] = regs->r10;
3563 env->regs[11] = regs->r11;
3564 env->regs[12] = regs->r12;
3565 env->regs[13] = regs->r13;
3566 env->regs[14] = regs->r14;
3567 env->regs[15] = regs->r15;
3568 env->regs[16] = regs->r16;
3569 env->regs[17] = regs->r17;
3570 env->regs[18] = regs->r18;
3571 env->regs[19] = regs->r19;
3572 env->regs[20] = regs->r20;
3573 env->regs[21] = regs->r21;
3574 env->regs[22] = regs->r22;
3575 env->regs[23] = regs->r23;
3576 env->regs[24] = regs->r24;
3577 env->regs[25] = regs->r25;
3578 env->regs[26] = regs->r26;
3579 env->regs[27] = regs->r27;
3580 env->regs[28] = regs->r28;
3581 env->regs[29] = regs->r29;
3582 env->regs[30] = regs->r30;
3583 env->regs[31] = regs->r31;
3584 env->sregs[SR_PC] = regs->pc;
3586 #elif defined(TARGET_MIPS)
3590 for(i = 0; i < 32; i++) {
3591 env->active_tc.gpr[i] = regs->regs[i];
3593 env->active_tc.PC = regs->cp0_epc & ~(target_ulong)1;
3594 if (regs->cp0_epc & 1) {
3595 env->hflags |= MIPS_HFLAG_M16;
3598 #elif defined(TARGET_SH4)
3602 for(i = 0; i < 16; i++) {
3603 env->gregs[i] = regs->regs[i];
3607 #elif defined(TARGET_ALPHA)
3611 for(i = 0; i < 28; i++) {
3612 env->ir[i] = ((abi_ulong *)regs)[i];
3614 env->ir[IR_SP] = regs->usp;
3617 #elif defined(TARGET_CRIS)
3619 env->regs[0] = regs->r0;
3620 env->regs[1] = regs->r1;
3621 env->regs[2] = regs->r2;
3622 env->regs[3] = regs->r3;
3623 env->regs[4] = regs->r4;
3624 env->regs[5] = regs->r5;
3625 env->regs[6] = regs->r6;
3626 env->regs[7] = regs->r7;
3627 env->regs[8] = regs->r8;
3628 env->regs[9] = regs->r9;
3629 env->regs[10] = regs->r10;
3630 env->regs[11] = regs->r11;
3631 env->regs[12] = regs->r12;
3632 env->regs[13] = regs->r13;
3633 env->regs[14] = info->start_stack;
3634 env->regs[15] = regs->acr;
3635 env->pc = regs->erp;
3637 #elif defined(TARGET_S390X)
3640 for (i = 0; i < 16; i++) {
3641 env->regs[i] = regs->gprs[i];
3643 env->psw.mask = regs->psw.mask;
3644 env->psw.addr = regs->psw.addr;
3647 #error unsupported target CPU
3650 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
3651 ts->stack_base = info->start_stack;
3652 ts->heap_base = info->brk;
3653 /* This will be filled in on the first SYS_HEAPINFO call. */
3658 if (gdbserver_start(gdbstub_port) < 0) {
3659 fprintf(stderr, "qemu: could not open gdbserver on port %d\n",
3663 gdb_handlesig(env, 0);