Merge remote-tracking branch 'remotes/jasowang/tags/net-pull-request' into staging
[qemu.git] / linux-user / nios2 / cpu_loop.c
1 /*
2 * qemu user cpu loop
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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "qemu.h"
22 #include "cpu_loop-common.h"
23
24 void cpu_loop(CPUNios2State *env)
25 {
26 CPUState *cs = env_cpu(env);
27 Nios2CPU *cpu = NIOS2_CPU(cs);
28 target_siginfo_t info;
29 int trapnr, ret;
30
31 for (;;) {
32 cpu_exec_start(cs);
33 trapnr = cpu_exec(cs);
34 cpu_exec_end(cs);
35
36 switch (trapnr) {
37 case EXCP_INTERRUPT:
38 /* just indicate that signals should be handled asap */
39 break;
40 case EXCP_TRAP:
41 if (env->regs[R_AT] == 0) {
42 abi_long ret;
43 qemu_log_mask(CPU_LOG_INT, "\nSyscall\n");
44
45 ret = do_syscall(env, env->regs[2],
46 env->regs[4], env->regs[5], env->regs[6],
47 env->regs[7], env->regs[8], env->regs[9],
48 0, 0);
49
50 if (env->regs[2] == 0) { /* FIXME: syscall 0 workaround */
51 ret = 0;
52 }
53
54 env->regs[2] = abs(ret);
55 /* Return value is 0..4096 */
56 env->regs[7] = (ret > 0xfffffffffffff000ULL);
57 env->regs[CR_ESTATUS] = env->regs[CR_STATUS];
58 env->regs[CR_STATUS] &= ~0x3;
59 env->regs[R_EA] = env->regs[R_PC] + 4;
60 env->regs[R_PC] += 4;
61 break;
62 } else {
63 qemu_log_mask(CPU_LOG_INT, "\nTrap\n");
64
65 env->regs[CR_ESTATUS] = env->regs[CR_STATUS];
66 env->regs[CR_STATUS] &= ~0x3;
67 env->regs[R_EA] = env->regs[R_PC] + 4;
68 env->regs[R_PC] = cpu->exception_addr;
69
70 info.si_signo = TARGET_SIGTRAP;
71 info.si_errno = 0;
72 info.si_code = TARGET_TRAP_BRKPT;
73 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
74 break;
75 }
76 case EXCP_DEBUG:
77 info.si_signo = TARGET_SIGTRAP;
78 info.si_errno = 0;
79 info.si_code = TARGET_TRAP_BRKPT;
80 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
81 break;
82 case 0xaa:
83 switch (env->regs[R_PC]) {
84 /*case 0x1000:*/ /* TODO:__kuser_helper_version */
85 case 0x1004: /* __kuser_cmpxchg */
86 start_exclusive();
87 if (env->regs[4] & 0x3) {
88 goto kuser_fail;
89 }
90 ret = get_user_u32(env->regs[2], env->regs[4]);
91 if (ret) {
92 end_exclusive();
93 goto kuser_fail;
94 }
95 env->regs[2] -= env->regs[5];
96 if (env->regs[2] == 0) {
97 put_user_u32(env->regs[6], env->regs[4]);
98 }
99 end_exclusive();
100 env->regs[R_PC] = env->regs[R_RA];
101 break;
102 /*case 0x1040:*/ /* TODO:__kuser_sigtramp */
103 default:
104 ;
105 kuser_fail:
106 info.si_signo = TARGET_SIGSEGV;
107 info.si_errno = 0;
108 /* TODO: check env->error_code */
109 info.si_code = TARGET_SEGV_MAPERR;
110 info._sifields._sigfault._addr = env->regs[R_PC];
111 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
112 }
113 break;
114 default:
115 EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
116 trapnr);
117 abort();
118 }
119
120 process_pending_signals(env);
121 }
122 }
123
124 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
125 {
126 env->regs[0] = 0;
127 env->regs[1] = regs->r1;
128 env->regs[2] = regs->r2;
129 env->regs[3] = regs->r3;
130 env->regs[4] = regs->r4;
131 env->regs[5] = regs->r5;
132 env->regs[6] = regs->r6;
133 env->regs[7] = regs->r7;
134 env->regs[8] = regs->r8;
135 env->regs[9] = regs->r9;
136 env->regs[10] = regs->r10;
137 env->regs[11] = regs->r11;
138 env->regs[12] = regs->r12;
139 env->regs[13] = regs->r13;
140 env->regs[14] = regs->r14;
141 env->regs[15] = regs->r15;
142 /* TODO: unsigned long orig_r2; */
143 env->regs[R_RA] = regs->ra;
144 env->regs[R_FP] = regs->fp;
145 env->regs[R_SP] = regs->sp;
146 env->regs[R_GP] = regs->gp;
147 env->regs[CR_ESTATUS] = regs->estatus;
148 env->regs[R_EA] = regs->ea;
149 /* TODO: unsigned long orig_r7; */
150
151 /* Emulate eret when starting thread. */
152 env->regs[R_PC] = regs->ea;
153 }