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