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 void cpu_loop(CPUMBState *env)
28 {
29     CPUState *cs = env_cpu(env);
30     int trapnr, ret;
31     target_siginfo_t info;
32 
33     while (1) {
34         cpu_exec_start(cs);
35         trapnr = cpu_exec(cs);
36         cpu_exec_end(cs);
37         process_queued_cpu_work(cs);
38 
39         switch (trapnr) {
40         case EXCP_INTERRUPT:
41           /* just indicate that signals should be handled asap */
42           break;
43         case EXCP_SYSCALL:
44             /* Return address is 4 bytes after the call.  */
45             env->regs[14] += 4;
46             env->pc = env->regs[14];
47             ret = do_syscall(env,
48                              env->regs[12],
49                              env->regs[5],
50                              env->regs[6],
51                              env->regs[7],
52                              env->regs[8],
53                              env->regs[9],
54                              env->regs[10],
55                              0, 0);
56             if (ret == -QEMU_ERESTARTSYS) {
57                 /* Wind back to before the syscall. */
58                 env->pc -= 4;
59             } else if (ret != -QEMU_ESIGRETURN) {
60                 env->regs[3] = ret;
61             }
62             /* All syscall exits result in guest r14 being equal to the
63              * PC we return to, because the kernel syscall exit "rtbd" does
64              * this. (This is true even for sigreturn(); note that r14 is
65              * not a userspace-usable register, as the kernel may clobber it
66              * at any point.)
67              */
68             env->regs[14] = env->pc;
69             break;
70         case EXCP_HW_EXCP:
71             env->regs[17] = env->pc + 4;
72             if (env->iflags & D_FLAG) {
73                 env->esr |= 1 << 12;
74                 env->pc -= 4;
75                 /* FIXME: if branch was immed, replay the imm as well.  */
76             }
77 
78             env->iflags &= ~(IMM_FLAG | D_FLAG);
79 
80             switch (env->esr & 31) {
81                 case ESR_EC_DIVZERO:
82                     info.si_signo = TARGET_SIGFPE;
83                     info.si_errno = 0;
84                     info.si_code = TARGET_FPE_FLTDIV;
85                     info._sifields._sigfault._addr = 0;
86                     queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
87                     break;
88                 case ESR_EC_FPU:
89                     info.si_signo = TARGET_SIGFPE;
90                     info.si_errno = 0;
91                     if (env->fsr & FSR_IO) {
92                         info.si_code = TARGET_FPE_FLTINV;
93                     }
94                     if (env->fsr & FSR_DZ) {
95                         info.si_code = TARGET_FPE_FLTDIV;
96                     }
97                     info._sifields._sigfault._addr = 0;
98                     queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
99                     break;
100                 default:
101                     fprintf(stderr, "Unhandled hw-exception: 0x%x\n",
102                             env->esr & ESR_EC_MASK);
103                     cpu_dump_state(cs, stderr, 0);
104                     exit(EXIT_FAILURE);
105                     break;
106             }
107             break;
108         case EXCP_DEBUG:
109             info.si_signo = TARGET_SIGTRAP;
110             info.si_errno = 0;
111             info.si_code = TARGET_TRAP_BRKPT;
112             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
113             break;
114         case EXCP_ATOMIC:
115             cpu_exec_step_atomic(cs);
116             break;
117         default:
118             fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
119             cpu_dump_state(cs, stderr, 0);
120             exit(EXIT_FAILURE);
121         }
122         process_pending_signals (env);
123     }
124 }
125 
126 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
127 {
128     env->regs[0] = regs->r0;
129     env->regs[1] = regs->r1;
130     env->regs[2] = regs->r2;
131     env->regs[3] = regs->r3;
132     env->regs[4] = regs->r4;
133     env->regs[5] = regs->r5;
134     env->regs[6] = regs->r6;
135     env->regs[7] = regs->r7;
136     env->regs[8] = regs->r8;
137     env->regs[9] = regs->r9;
138     env->regs[10] = regs->r10;
139     env->regs[11] = regs->r11;
140     env->regs[12] = regs->r12;
141     env->regs[13] = regs->r13;
142     env->regs[14] = regs->r14;
143     env->regs[15] = regs->r15;
144     env->regs[16] = regs->r16;
145     env->regs[17] = regs->r17;
146     env->regs[18] = regs->r18;
147     env->regs[19] = regs->r19;
148     env->regs[20] = regs->r20;
149     env->regs[21] = regs->r21;
150     env->regs[22] = regs->r22;
151     env->regs[23] = regs->r23;
152     env->regs[24] = regs->r24;
153     env->regs[25] = regs->r25;
154     env->regs[26] = regs->r26;
155     env->regs[27] = regs->r27;
156     env->regs[28] = regs->r28;
157     env->regs[29] = regs->r29;
158     env->regs[30] = regs->r30;
159     env->regs[31] = regs->r31;
160     env->pc = regs->pc;
161 }
162