xref: /openbmc/qemu/linux-user/s390x/cpu_loop.c (revision 2cc0e2e8)
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 "cpu_loop-common.h"
23 
24 /* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */
25 #define S390X_FAIL_ADDR_MASK -4096LL
26 
27 void cpu_loop(CPUS390XState *env)
28 {
29     CPUState *cs = CPU(s390_env_get_cpu(env));
30     int trapnr, n, sig;
31     target_siginfo_t info;
32     target_ulong addr;
33     abi_long ret;
34 
35     while (1) {
36         cpu_exec_start(cs);
37         trapnr = cpu_exec(cs);
38         cpu_exec_end(cs);
39         process_queued_cpu_work(cs);
40 
41         switch (trapnr) {
42         case EXCP_INTERRUPT:
43             /* Just indicate that signals should be handled asap.  */
44             break;
45 
46         case EXCP_SVC:
47             n = env->int_svc_code;
48             if (!n) {
49                 /* syscalls > 255 */
50                 n = env->regs[1];
51             }
52             env->psw.addr += env->int_svc_ilen;
53             ret = do_syscall(env, n, env->regs[2], env->regs[3],
54                              env->regs[4], env->regs[5],
55                              env->regs[6], env->regs[7], 0, 0);
56             if (ret == -TARGET_ERESTARTSYS) {
57                 env->psw.addr -= env->int_svc_ilen;
58             } else if (ret != -TARGET_QEMU_ESIGRETURN) {
59                 env->regs[2] = ret;
60             }
61             break;
62 
63         case EXCP_DEBUG:
64             sig = gdb_handlesig(cs, TARGET_SIGTRAP);
65             if (sig) {
66                 n = TARGET_TRAP_BRKPT;
67                 goto do_signal_pc;
68             }
69             break;
70         case EXCP_PGM:
71             n = env->int_pgm_code;
72             switch (n) {
73             case PGM_OPERATION:
74             case PGM_PRIVILEGED:
75                 sig = TARGET_SIGILL;
76                 n = TARGET_ILL_ILLOPC;
77                 goto do_signal_pc;
78             case PGM_PROTECTION:
79             case PGM_ADDRESSING:
80                 sig = TARGET_SIGSEGV;
81                 /* XXX: check env->error_code */
82                 n = TARGET_SEGV_MAPERR;
83                 addr = env->__excp_addr & S390X_FAIL_ADDR_MASK;
84                 goto do_signal;
85             case PGM_EXECUTE:
86             case PGM_SPECIFICATION:
87             case PGM_SPECIAL_OP:
88             case PGM_OPERAND:
89             do_sigill_opn:
90                 sig = TARGET_SIGILL;
91                 n = TARGET_ILL_ILLOPN;
92                 goto do_signal_pc;
93 
94             case PGM_FIXPT_OVERFLOW:
95                 sig = TARGET_SIGFPE;
96                 n = TARGET_FPE_INTOVF;
97                 goto do_signal_pc;
98             case PGM_FIXPT_DIVIDE:
99                 sig = TARGET_SIGFPE;
100                 n = TARGET_FPE_INTDIV;
101                 goto do_signal_pc;
102 
103             case PGM_DATA:
104                 n = (env->fpc >> 8) & 0xff;
105                 if (n == 0xff) {
106                     /* compare-and-trap */
107                     goto do_sigill_opn;
108                 } else {
109                     /* An IEEE exception, simulated or otherwise.  */
110                     if (n & 0x80) {
111                         n = TARGET_FPE_FLTINV;
112                     } else if (n & 0x40) {
113                         n = TARGET_FPE_FLTDIV;
114                     } else if (n & 0x20) {
115                         n = TARGET_FPE_FLTOVF;
116                     } else if (n & 0x10) {
117                         n = TARGET_FPE_FLTUND;
118                     } else if (n & 0x08) {
119                         n = TARGET_FPE_FLTRES;
120                     } else {
121                         /* ??? Quantum exception; BFP, DFP error.  */
122                         goto do_sigill_opn;
123                     }
124                     sig = TARGET_SIGFPE;
125                     goto do_signal_pc;
126                 }
127 
128             default:
129                 fprintf(stderr, "Unhandled program exception: %#x\n", n);
130                 cpu_dump_state(cs, stderr, fprintf, 0);
131                 exit(EXIT_FAILURE);
132             }
133             break;
134 
135         do_signal_pc:
136             addr = env->psw.addr;
137         do_signal:
138             info.si_signo = sig;
139             info.si_errno = 0;
140             info.si_code = n;
141             info._sifields._sigfault._addr = addr;
142             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
143             break;
144 
145         case EXCP_ATOMIC:
146             cpu_exec_step_atomic(cs);
147             break;
148         default:
149             fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
150             cpu_dump_state(cs, stderr, fprintf, 0);
151             exit(EXIT_FAILURE);
152         }
153         process_pending_signals (env);
154     }
155 }
156 
157 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
158 {
159     int i;
160     for (i = 0; i < 16; i++) {
161         env->regs[i] = regs->gprs[i];
162     }
163     env->psw.mask = regs->psw.mask;
164     env->psw.addr = regs->psw.addr;
165 }
166