master
c 177 lines 5.95 KB
Raw
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 "user-internals.h"
23 #include "user/cpu_loop.h"
24 #include "signal-common.h"
25
26 void cpu_loop(CPUAlphaState *env)
27 {
28 CPUState *cs = env_cpu(env);
29 int trapnr, si_code;
30 abi_long sysret;
31
32 while (1) {
33 bool arch_interrupt = true;
34
35 cpu_exec_start(cs);
36 trapnr = cpu_exec(cs);
37 cpu_exec_end(cs);
38 qemu_process_cpu_events(cs);
39
40 switch (trapnr) {
41 case EXCP_RESET:
42 fprintf(stderr, "Reset requested. Exit\n");
43 exit(EXIT_FAILURE);
44 break;
45 case EXCP_MCHK:
46 fprintf(stderr, "Machine check exception. Exit\n");
47 exit(EXIT_FAILURE);
48 break;
49 case EXCP_SMP_INTERRUPT:
50 case EXCP_CLK_INTERRUPT:
51 case EXCP_DEV_INTERRUPT:
52 fprintf(stderr, "External interrupt. Exit\n");
53 exit(EXIT_FAILURE);
54 break;
55 case EXCP_OPCDEC:
56 do_sigill:
57 force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC, env->pc);
58 break;
59 case EXCP_ARITH:
60 force_sig_fault(TARGET_SIGFPE, TARGET_FPE_FLTINV, env->pc);
61 break;
62 case EXCP_FEN:
63 /* No-op. Linux simply re-enables the FPU. */
64 break;
65 case EXCP_CALL_PAL:
66 switch (env->error_code) {
67 case 0x80:
68 /* BPT */
69 goto do_sigtrap_brkpt;
70 case 0x81:
71 /* BUGCHK */
72 goto do_sigtrap_unk;
73 case 0x83:
74 /* CALLSYS */
75 trapnr = env->ir[IR_V0];
76 sysret = do_syscall(env, trapnr,
77 env->ir[IR_A0], env->ir[IR_A1],
78 env->ir[IR_A2], env->ir[IR_A3],
79 env->ir[IR_A4], env->ir[IR_A5],
80 0, 0);
81 if (sysret == -QEMU_ERESTARTSYS) {
82 env->pc -= 4;
83 break;
84 }
85 if (sysret == -QEMU_ESIGRETURN || sysret == -QEMU_ESETPC) {
86 break;
87 }
88 /* Syscall writes 0 to V0 to bypass error check, similar
89 to how this is handled internal to Linux kernel.
90 (Ab)use trapnr temporarily as boolean indicating error. */
91 trapnr = (env->ir[IR_V0] != 0 && sysret < 0);
92 env->ir[IR_V0] = (trapnr ? -sysret : sysret);
93 env->ir[IR_A3] = trapnr;
94 break;
95 case 0x86:
96 /* IMB */
97 break;
98 case 0x9E:
99 /* RDUNIQUE */
100 /* Handled in the translator for usermode. */
101 abort();
102 case 0x9F:
103 /* WRUNIQUE */
104 /* Handled in the translator for usermode. */
105 abort();
106 case 0xAA:
107 /* GENTRAP */
108 switch (env->ir[IR_A0]) {
109 case TARGET_GEN_INTOVF:
110 si_code = TARGET_FPE_INTOVF;
111 break;
112 case TARGET_GEN_INTDIV:
113 si_code = TARGET_FPE_INTDIV;
114 break;
115 case TARGET_GEN_FLTOVF:
116 si_code = TARGET_FPE_FLTOVF;
117 break;
118 case TARGET_GEN_FLTUND:
119 si_code = TARGET_FPE_FLTUND;
120 break;
121 case TARGET_GEN_FLTINV:
122 si_code = TARGET_FPE_FLTINV;
123 break;
124 case TARGET_GEN_FLTINE:
125 si_code = TARGET_FPE_FLTRES;
126 break;
127 case TARGET_GEN_ROPRAND:
128 si_code = TARGET_FPE_FLTUNK;
129 break;
130 default:
131 goto do_sigtrap_unk;
132 }
133 force_sig_fault(TARGET_SIGFPE, si_code, env->pc);
134 break;
135 default:
136 goto do_sigill;
137 }
138 break;
139 case EXCP_DEBUG:
140 do_sigtrap_brkpt:
141 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
142 break;
143 do_sigtrap_unk:
144 force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_UNK, env->pc);
145 break;
146 case EXCP_INTERRUPT:
147 /* Just indicate that signals should be handled asap. */
148 break;
149 case EXCP_ATOMIC:
150 cpu_exec_step_atomic(cs);
151 arch_interrupt = false;
152 break;
153 default:
154 fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
155 cpu_dump_state(cs, stderr, 0);
156 exit(EXIT_FAILURE);
157 }
158 process_pending_signals (env);
159
160 /* Most of the traps imply a transition through PALcode, which
161 implies an REI instruction has been executed. Which means
162 that RX and LOCK_ADDR should be cleared. But there are a
163 few exceptions for traps internal to QEMU. */
164 if (arch_interrupt) {
165 env->flags &= ~ENV_FLAG_RX_FLAG;
166 env->lock_addr = -1;
167 }
168 }
169 }
170
171 void init_main_thread(CPUState *cs, struct image_info *info)
172 {
173 CPUArchState *env = cpu_env(cs);
174
175 env->pc = info->entry;
176 env->ir[IR_SP] = info->start_stack;
177 }