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