xref: /openbmc/qemu/linux-user/s390x/cpu_loop.c (revision 2df1eb27)
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 "cpu_loop-common.h"
24 #include "signal-common.h"
25 
26 
27 static int get_pgm_data_si_code(int dxc_code)
28 {
29     switch (dxc_code) {
30     /* Non-simulated IEEE exceptions */
31     case 0x80:
32         return TARGET_FPE_FLTINV;
33     case 0x40:
34         return TARGET_FPE_FLTDIV;
35     case 0x20:
36     case 0x28:
37     case 0x2c:
38         return TARGET_FPE_FLTOVF;
39     case 0x10:
40     case 0x18:
41     case 0x1c:
42         return TARGET_FPE_FLTUND;
43     case 0x08:
44     case 0x0c:
45         return TARGET_FPE_FLTRES;
46     }
47     /*
48      * Non-IEEE and simulated IEEE:
49      * Includes compare-and-trap, quantum exception, etc.
50      * Simulated IEEE are included here to match current
51      * s390x linux kernel.
52      */
53     return 0;
54 }
55 
56 void cpu_loop(CPUS390XState *env)
57 {
58     CPUState *cs = env_cpu(env);
59     int trapnr, n, sig;
60     target_ulong addr;
61     abi_long ret;
62 
63     while (1) {
64         cpu_exec_start(cs);
65         trapnr = cpu_exec(cs);
66         cpu_exec_end(cs);
67         process_queued_cpu_work(cs);
68 
69         switch (trapnr) {
70         case EXCP_INTERRUPT:
71             /* Just indicate that signals should be handled asap.  */
72             break;
73 
74         case EXCP_SVC:
75             n = env->int_svc_code;
76             if (!n) {
77                 /* syscalls > 255 */
78                 n = env->regs[1];
79             }
80             env->psw.addr += env->int_svc_ilen;
81             ret = do_syscall(env, n, env->regs[2], env->regs[3],
82                              env->regs[4], env->regs[5],
83                              env->regs[6], env->regs[7], 0, 0);
84             if (ret == -QEMU_ERESTARTSYS) {
85                 env->psw.addr -= env->int_svc_ilen;
86             } else if (ret != -QEMU_ESIGRETURN) {
87                 env->regs[2] = ret;
88             }
89 
90             if (unlikely(cs->singlestep_enabled)) {
91                 /*
92                  * cpu_tb_exec() did not raise EXCP_DEBUG, because it has seen
93                  * that EXCP_SVC was already pending.
94                  */
95                 cs->exception_index = EXCP_DEBUG;
96             }
97 
98             break;
99 
100         case EXCP_DEBUG:
101             sig = TARGET_SIGTRAP;
102             n = TARGET_TRAP_BRKPT;
103             /*
104              * For SIGTRAP the PSW must point after the instruction, which it
105              * already does thanks to s390x_tr_tb_stop(). si_addr doesn't need
106              * to be filled.
107              */
108             addr = 0;
109             goto do_signal;
110         case EXCP_PGM:
111             n = env->int_pgm_code;
112             switch (n) {
113             case PGM_OPERATION:
114             case PGM_PRIVILEGED:
115                 sig = TARGET_SIGILL;
116                 n = TARGET_ILL_ILLOPC;
117                 goto do_signal_pc;
118             case PGM_PROTECTION:
119                 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_ACCERR,
120                                 env->__excp_addr);
121                 break;
122             case PGM_ADDRESSING:
123                 force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR,
124                                 env->__excp_addr);
125                 break;
126             case PGM_EXECUTE:
127             case PGM_SPECIFICATION:
128             case PGM_SPECIAL_OP:
129             case PGM_OPERAND:
130             do_sigill_opn:
131                 sig = TARGET_SIGILL;
132                 n = TARGET_ILL_ILLOPN;
133                 goto do_signal_pc;
134 
135             case PGM_FIXPT_OVERFLOW:
136                 sig = TARGET_SIGFPE;
137                 n = TARGET_FPE_INTOVF;
138                 goto do_signal_pc;
139             case PGM_FIXPT_DIVIDE:
140                 sig = TARGET_SIGFPE;
141                 n = TARGET_FPE_INTDIV;
142                 goto do_signal_pc;
143 
144             case PGM_DATA:
145                 n = (env->fpc >> 8) & 0xff;
146                 if (n == 0) {
147                     goto do_sigill_opn;
148                 }
149 
150                 sig = TARGET_SIGFPE;
151                 n = get_pgm_data_si_code(n);
152                 goto do_signal_pc;
153 
154             default:
155                 fprintf(stderr, "Unhandled program exception: %#x\n", n);
156                 cpu_dump_state(cs, stderr, 0);
157                 exit(EXIT_FAILURE);
158             }
159             break;
160 
161         do_signal_pc:
162             addr = env->psw.addr;
163             /*
164              * For SIGILL and SIGFPE the PSW must point after the instruction.
165              */
166             env->psw.addr += env->int_pgm_ilen;
167         do_signal:
168             force_sig_fault(sig, n, addr);
169             break;
170 
171         case EXCP_ATOMIC:
172             cpu_exec_step_atomic(cs);
173             break;
174         default:
175             fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
176             cpu_dump_state(cs, stderr, 0);
177             exit(EXIT_FAILURE);
178         }
179         process_pending_signals (env);
180     }
181 }
182 
183 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
184 {
185     int i;
186     for (i = 0; i < 16; i++) {
187         env->regs[i] = regs->gprs[i];
188     }
189     env->psw.mask = regs->psw.mask;
190     env->psw.addr = regs->psw.addr;
191 }
192