xref: /openbmc/qemu/linux-user/alpha/cpu_loop.c (revision 2113aed687cb0b84ad512c440c1edf6eea8fcde2)
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-common.h"
22 #include "qemu.h"
23 #include "cpu_loop-common.h"
24 #include "signal-common.h"
25 
26 void cpu_loop(CPUAlphaState *env)
27 {
28     CPUState *cs = env_cpu(env);
29     int trapnr;
30     target_siginfo_t info;
31     abi_long sysret;
32 
33     while (1) {
34         bool arch_interrupt = true;
35 
36         cpu_exec_start(cs);
37         trapnr = cpu_exec(cs);
38         cpu_exec_end(cs);
39         process_queued_cpu_work(cs);
40 
41         switch (trapnr) {
42         case EXCP_RESET:
43             fprintf(stderr, "Reset requested. Exit\n");
44             exit(EXIT_FAILURE);
45             break;
46         case EXCP_MCHK:
47             fprintf(stderr, "Machine check exception. Exit\n");
48             exit(EXIT_FAILURE);
49             break;
50         case EXCP_SMP_INTERRUPT:
51         case EXCP_CLK_INTERRUPT:
52         case EXCP_DEV_INTERRUPT:
53             fprintf(stderr, "External interrupt. Exit\n");
54             exit(EXIT_FAILURE);
55             break;
56         case EXCP_MMFAULT:
57             info.si_signo = TARGET_SIGSEGV;
58             info.si_errno = 0;
59             info.si_code = (page_get_flags(env->trap_arg0) & PAGE_VALID
60                             ? TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR);
61             info._sifields._sigfault._addr = env->trap_arg0;
62             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
63             break;
64         case EXCP_UNALIGN:
65             info.si_signo = TARGET_SIGBUS;
66             info.si_errno = 0;
67             info.si_code = TARGET_BUS_ADRALN;
68             info._sifields._sigfault._addr = env->trap_arg0;
69             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
70             break;
71         case EXCP_OPCDEC:
72         do_sigill:
73             info.si_signo = TARGET_SIGILL;
74             info.si_errno = 0;
75             info.si_code = TARGET_ILL_ILLOPC;
76             info._sifields._sigfault._addr = env->pc;
77             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
78             break;
79         case EXCP_ARITH:
80             info.si_signo = TARGET_SIGFPE;
81             info.si_errno = 0;
82             info.si_code = TARGET_FPE_FLTINV;
83             info._sifields._sigfault._addr = env->pc;
84             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
85             break;
86         case EXCP_FEN:
87             /* No-op.  Linux simply re-enables the FPU.  */
88             break;
89         case EXCP_CALL_PAL:
90             switch (env->error_code) {
91             case 0x80:
92                 /* BPT */
93                 info.si_signo = TARGET_SIGTRAP;
94                 info.si_errno = 0;
95                 info.si_code = TARGET_TRAP_BRKPT;
96                 info._sifields._sigfault._addr = env->pc;
97                 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
98                 break;
99             case 0x81:
100                 /* BUGCHK */
101                 info.si_signo = TARGET_SIGTRAP;
102                 info.si_errno = 0;
103                 info.si_code = 0;
104                 info._sifields._sigfault._addr = env->pc;
105                 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
106                 break;
107             case 0x83:
108                 /* CALLSYS */
109                 trapnr = env->ir[IR_V0];
110                 sysret = do_syscall(env, trapnr,
111                                     env->ir[IR_A0], env->ir[IR_A1],
112                                     env->ir[IR_A2], env->ir[IR_A3],
113                                     env->ir[IR_A4], env->ir[IR_A5],
114                                     0, 0);
115                 if (sysret == -TARGET_ERESTARTSYS) {
116                     env->pc -= 4;
117                     break;
118                 }
119                 if (sysret == -TARGET_QEMU_ESIGRETURN) {
120                     break;
121                 }
122                 /* Syscall writes 0 to V0 to bypass error check, similar
123                    to how this is handled internal to Linux kernel.
124                    (Ab)use trapnr temporarily as boolean indicating error.  */
125                 trapnr = (env->ir[IR_V0] != 0 && sysret < 0);
126                 env->ir[IR_V0] = (trapnr ? -sysret : sysret);
127                 env->ir[IR_A3] = trapnr;
128                 break;
129             case 0x86:
130                 /* IMB */
131                 /* ??? We can probably elide the code using page_unprotect
132                    that is checking for self-modifying code.  Instead we
133                    could simply call tb_flush here.  Until we work out the
134                    changes required to turn off the extra write protection,
135                    this can be a no-op.  */
136                 break;
137             case 0x9E:
138                 /* RDUNIQUE */
139                 /* Handled in the translator for usermode.  */
140                 abort();
141             case 0x9F:
142                 /* WRUNIQUE */
143                 /* Handled in the translator for usermode.  */
144                 abort();
145             case 0xAA:
146                 /* GENTRAP */
147                 info.si_signo = TARGET_SIGFPE;
148                 switch (env->ir[IR_A0]) {
149                 case TARGET_GEN_INTOVF:
150                     info.si_code = TARGET_FPE_INTOVF;
151                     break;
152                 case TARGET_GEN_INTDIV:
153                     info.si_code = TARGET_FPE_INTDIV;
154                     break;
155                 case TARGET_GEN_FLTOVF:
156                     info.si_code = TARGET_FPE_FLTOVF;
157                     break;
158                 case TARGET_GEN_FLTUND:
159                     info.si_code = TARGET_FPE_FLTUND;
160                     break;
161                 case TARGET_GEN_FLTINV:
162                     info.si_code = TARGET_FPE_FLTINV;
163                     break;
164                 case TARGET_GEN_FLTINE:
165                     info.si_code = TARGET_FPE_FLTRES;
166                     break;
167                 case TARGET_GEN_ROPRAND:
168                     info.si_code = 0;
169                     break;
170                 default:
171                     info.si_signo = TARGET_SIGTRAP;
172                     info.si_code = 0;
173                     break;
174                 }
175                 info.si_errno = 0;
176                 info._sifields._sigfault._addr = env->pc;
177                 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
178                 break;
179             default:
180                 goto do_sigill;
181             }
182             break;
183         case EXCP_DEBUG:
184             info.si_signo = TARGET_SIGTRAP;
185             info.si_errno = 0;
186             info.si_code = TARGET_TRAP_BRKPT;
187             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
188             break;
189         case EXCP_INTERRUPT:
190             /* Just indicate that signals should be handled asap.  */
191             break;
192         case EXCP_ATOMIC:
193             cpu_exec_step_atomic(cs);
194             arch_interrupt = false;
195             break;
196         default:
197             fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
198             cpu_dump_state(cs, stderr, 0);
199             exit(EXIT_FAILURE);
200         }
201         process_pending_signals (env);
202 
203         /* Most of the traps imply a transition through PALcode, which
204            implies an REI instruction has been executed.  Which means
205            that RX and LOCK_ADDR should be cleared.  But there are a
206            few exceptions for traps internal to QEMU.  */
207         if (arch_interrupt) {
208             env->flags &= ~ENV_FLAG_RX_FLAG;
209             env->lock_addr = -1;
210         }
211     }
212 }
213 
214 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
215 {
216     int i;
217 
218     for(i = 0; i < 28; i++) {
219         env->ir[i] = ((abi_ulong *)regs)[i];
220     }
221     env->ir[IR_SP] = regs->usp;
222     env->pc = regs->pc;
223 }
224