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