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