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