1cd71c089SLaurent Vivier /*
2cd71c089SLaurent Vivier * qemu user cpu loop
3cd71c089SLaurent Vivier *
4cd71c089SLaurent Vivier * Copyright (c) 2003-2008 Fabrice Bellard
5cd71c089SLaurent Vivier *
6cd71c089SLaurent Vivier * This program is free software; you can redistribute it and/or modify
7cd71c089SLaurent Vivier * it under the terms of the GNU General Public License as published by
8cd71c089SLaurent Vivier * the Free Software Foundation; either version 2 of the License, or
9cd71c089SLaurent Vivier * (at your option) any later version.
10cd71c089SLaurent Vivier *
11cd71c089SLaurent Vivier * This program is distributed in the hope that it will be useful,
12cd71c089SLaurent Vivier * but WITHOUT ANY WARRANTY; without even the implied warranty of
13cd71c089SLaurent Vivier * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14cd71c089SLaurent Vivier * GNU General Public License for more details.
15cd71c089SLaurent Vivier *
16cd71c089SLaurent Vivier * You should have received a copy of the GNU General Public License
17cd71c089SLaurent Vivier * along with this program; if not, see <http://www.gnu.org/licenses/>.
18cd71c089SLaurent Vivier */
19cd71c089SLaurent Vivier
20cd71c089SLaurent Vivier #include "qemu/osdep.h"
21cd71c089SLaurent Vivier #include "qemu.h"
223b249d26SPeter Maydell #include "user-internals.h"
23cd71c089SLaurent Vivier #include "cpu_loop-common.h"
242113aed6SPeter Maydell #include "signal-common.h"
25cd71c089SLaurent Vivier
cpu_loop(CPUAlphaState * env)26e256aefeSLaurent Vivier void cpu_loop(CPUAlphaState *env)
27e256aefeSLaurent Vivier {
281c7ad260SRichard Henderson CPUState *cs = env_cpu(env);
29*1c165977SRichard Henderson int trapnr, si_code;
30e256aefeSLaurent Vivier abi_long sysret;
31e256aefeSLaurent Vivier
32e256aefeSLaurent Vivier while (1) {
33e256aefeSLaurent Vivier bool arch_interrupt = true;
34e256aefeSLaurent Vivier
35e256aefeSLaurent Vivier cpu_exec_start(cs);
36e256aefeSLaurent Vivier trapnr = cpu_exec(cs);
37e256aefeSLaurent Vivier cpu_exec_end(cs);
38e256aefeSLaurent Vivier process_queued_cpu_work(cs);
39e256aefeSLaurent Vivier
40e256aefeSLaurent Vivier switch (trapnr) {
41e256aefeSLaurent Vivier case EXCP_RESET:
42e256aefeSLaurent Vivier fprintf(stderr, "Reset requested. Exit\n");
43e256aefeSLaurent Vivier exit(EXIT_FAILURE);
44e256aefeSLaurent Vivier break;
45e256aefeSLaurent Vivier case EXCP_MCHK:
46e256aefeSLaurent Vivier fprintf(stderr, "Machine check exception. Exit\n");
47e256aefeSLaurent Vivier exit(EXIT_FAILURE);
48e256aefeSLaurent Vivier break;
49e256aefeSLaurent Vivier case EXCP_SMP_INTERRUPT:
50e256aefeSLaurent Vivier case EXCP_CLK_INTERRUPT:
51e256aefeSLaurent Vivier case EXCP_DEV_INTERRUPT:
52e256aefeSLaurent Vivier fprintf(stderr, "External interrupt. Exit\n");
53e256aefeSLaurent Vivier exit(EXIT_FAILURE);
54e256aefeSLaurent Vivier break;
55e256aefeSLaurent Vivier case EXCP_OPCDEC:
56e256aefeSLaurent Vivier do_sigill:
57*1c165977SRichard Henderson force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC, env->pc);
58e256aefeSLaurent Vivier break;
59e256aefeSLaurent Vivier case EXCP_ARITH:
60*1c165977SRichard Henderson force_sig_fault(TARGET_SIGFPE, TARGET_FPE_FLTINV, env->pc);
61e256aefeSLaurent Vivier break;
62e256aefeSLaurent Vivier case EXCP_FEN:
63e256aefeSLaurent Vivier /* No-op. Linux simply re-enables the FPU. */
64e256aefeSLaurent Vivier break;
65e256aefeSLaurent Vivier case EXCP_CALL_PAL:
66e256aefeSLaurent Vivier switch (env->error_code) {
67e256aefeSLaurent Vivier case 0x80:
68e256aefeSLaurent Vivier /* BPT */
69*1c165977SRichard Henderson goto do_sigtrap_brkpt;
70e256aefeSLaurent Vivier case 0x81:
71e256aefeSLaurent Vivier /* BUGCHK */
72*1c165977SRichard Henderson goto do_sigtrap_unk;
73e256aefeSLaurent Vivier case 0x83:
74e256aefeSLaurent Vivier /* CALLSYS */
75e256aefeSLaurent Vivier trapnr = env->ir[IR_V0];
76e256aefeSLaurent Vivier sysret = do_syscall(env, trapnr,
77e256aefeSLaurent Vivier env->ir[IR_A0], env->ir[IR_A1],
78e256aefeSLaurent Vivier env->ir[IR_A2], env->ir[IR_A3],
79e256aefeSLaurent Vivier env->ir[IR_A4], env->ir[IR_A5],
80e256aefeSLaurent Vivier 0, 0);
81af254a27SRichard Henderson if (sysret == -QEMU_ERESTARTSYS) {
82e256aefeSLaurent Vivier env->pc -= 4;
83e256aefeSLaurent Vivier break;
84e256aefeSLaurent Vivier }
8557a0c938SRichard Henderson if (sysret == -QEMU_ESIGRETURN) {
86e256aefeSLaurent Vivier break;
87e256aefeSLaurent Vivier }
88e256aefeSLaurent Vivier /* Syscall writes 0 to V0 to bypass error check, similar
89e256aefeSLaurent Vivier to how this is handled internal to Linux kernel.
90e256aefeSLaurent Vivier (Ab)use trapnr temporarily as boolean indicating error. */
91e256aefeSLaurent Vivier trapnr = (env->ir[IR_V0] != 0 && sysret < 0);
92e256aefeSLaurent Vivier env->ir[IR_V0] = (trapnr ? -sysret : sysret);
93e256aefeSLaurent Vivier env->ir[IR_A3] = trapnr;
94e256aefeSLaurent Vivier break;
95e256aefeSLaurent Vivier case 0x86:
96e256aefeSLaurent Vivier /* IMB */
97e256aefeSLaurent Vivier /* ??? We can probably elide the code using page_unprotect
98e256aefeSLaurent Vivier that is checking for self-modifying code. Instead we
99e256aefeSLaurent Vivier could simply call tb_flush here. Until we work out the
100e256aefeSLaurent Vivier changes required to turn off the extra write protection,
101e256aefeSLaurent Vivier this can be a no-op. */
102e256aefeSLaurent Vivier break;
103e256aefeSLaurent Vivier case 0x9E:
104e256aefeSLaurent Vivier /* RDUNIQUE */
105e256aefeSLaurent Vivier /* Handled in the translator for usermode. */
106e256aefeSLaurent Vivier abort();
107e256aefeSLaurent Vivier case 0x9F:
108e256aefeSLaurent Vivier /* WRUNIQUE */
109e256aefeSLaurent Vivier /* Handled in the translator for usermode. */
110e256aefeSLaurent Vivier abort();
111e256aefeSLaurent Vivier case 0xAA:
112e256aefeSLaurent Vivier /* GENTRAP */
113e256aefeSLaurent Vivier switch (env->ir[IR_A0]) {
114e256aefeSLaurent Vivier case TARGET_GEN_INTOVF:
115*1c165977SRichard Henderson si_code = TARGET_FPE_INTOVF;
116e256aefeSLaurent Vivier break;
117e256aefeSLaurent Vivier case TARGET_GEN_INTDIV:
118*1c165977SRichard Henderson si_code = TARGET_FPE_INTDIV;
119e256aefeSLaurent Vivier break;
120e256aefeSLaurent Vivier case TARGET_GEN_FLTOVF:
121*1c165977SRichard Henderson si_code = TARGET_FPE_FLTOVF;
122e256aefeSLaurent Vivier break;
123e256aefeSLaurent Vivier case TARGET_GEN_FLTUND:
124*1c165977SRichard Henderson si_code = TARGET_FPE_FLTUND;
125e256aefeSLaurent Vivier break;
126e256aefeSLaurent Vivier case TARGET_GEN_FLTINV:
127*1c165977SRichard Henderson si_code = TARGET_FPE_FLTINV;
128e256aefeSLaurent Vivier break;
129e256aefeSLaurent Vivier case TARGET_GEN_FLTINE:
130*1c165977SRichard Henderson si_code = TARGET_FPE_FLTRES;
131e256aefeSLaurent Vivier break;
132e256aefeSLaurent Vivier case TARGET_GEN_ROPRAND:
133*1c165977SRichard Henderson si_code = TARGET_FPE_FLTUNK;
134e256aefeSLaurent Vivier break;
135e256aefeSLaurent Vivier default:
136*1c165977SRichard Henderson goto do_sigtrap_unk;
137e256aefeSLaurent Vivier }
138*1c165977SRichard Henderson force_sig_fault(TARGET_SIGFPE, si_code, env->pc);
139e256aefeSLaurent Vivier break;
140e256aefeSLaurent Vivier default:
141e256aefeSLaurent Vivier goto do_sigill;
142e256aefeSLaurent Vivier }
143e256aefeSLaurent Vivier break;
144e256aefeSLaurent Vivier case EXCP_DEBUG:
145*1c165977SRichard Henderson do_sigtrap_brkpt:
146*1c165977SRichard Henderson force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
147*1c165977SRichard Henderson break;
148*1c165977SRichard Henderson do_sigtrap_unk:
149*1c165977SRichard Henderson force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_UNK, env->pc);
150e256aefeSLaurent Vivier break;
151e256aefeSLaurent Vivier case EXCP_INTERRUPT:
152e256aefeSLaurent Vivier /* Just indicate that signals should be handled asap. */
153e256aefeSLaurent Vivier break;
154e256aefeSLaurent Vivier case EXCP_ATOMIC:
155e256aefeSLaurent Vivier cpu_exec_step_atomic(cs);
156e256aefeSLaurent Vivier arch_interrupt = false;
157e256aefeSLaurent Vivier break;
158e256aefeSLaurent Vivier default:
15984ca4fa9SPhilippe Mathieu-Daudé fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
16090c84c56SMarkus Armbruster cpu_dump_state(cs, stderr, 0);
161e256aefeSLaurent Vivier exit(EXIT_FAILURE);
162e256aefeSLaurent Vivier }
163e256aefeSLaurent Vivier process_pending_signals (env);
164e256aefeSLaurent Vivier
165e256aefeSLaurent Vivier /* Most of the traps imply a transition through PALcode, which
166e256aefeSLaurent Vivier implies an REI instruction has been executed. Which means
167e256aefeSLaurent Vivier that RX and LOCK_ADDR should be cleared. But there are a
168e256aefeSLaurent Vivier few exceptions for traps internal to QEMU. */
169e256aefeSLaurent Vivier if (arch_interrupt) {
170e256aefeSLaurent Vivier env->flags &= ~ENV_FLAG_RX_FLAG;
171e256aefeSLaurent Vivier env->lock_addr = -1;
172e256aefeSLaurent Vivier }
173e256aefeSLaurent Vivier }
174e256aefeSLaurent Vivier }
175e256aefeSLaurent Vivier
target_cpu_copy_regs(CPUArchState * env,struct target_pt_regs * regs)176cd71c089SLaurent Vivier void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
177cd71c089SLaurent Vivier {
178e256aefeSLaurent Vivier int i;
179e256aefeSLaurent Vivier
180e256aefeSLaurent Vivier for(i = 0; i < 28; i++) {
181e256aefeSLaurent Vivier env->ir[i] = ((abi_ulong *)regs)[i];
182e256aefeSLaurent Vivier }
183e256aefeSLaurent Vivier env->ir[IR_SP] = regs->usp;
184e256aefeSLaurent Vivier env->pc = regs->pc;
185cd71c089SLaurent Vivier }
186