xref: /openbmc/qemu/linux-user/m68k/cpu_loop.c (revision 438c78da)
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 void cpu_loop(CPUM68KState *env)
25 {
26     CPUState *cs = CPU(m68k_env_get_cpu(env));
27     int trapnr;
28     unsigned int n;
29     target_siginfo_t info;
30     TaskState *ts = cs->opaque;
31 
32     for(;;) {
33         cpu_exec_start(cs);
34         trapnr = cpu_exec(cs);
35         cpu_exec_end(cs);
36         process_queued_cpu_work(cs);
37 
38         switch(trapnr) {
39         case EXCP_ILLEGAL:
40             {
41                 if (ts->sim_syscalls) {
42                     uint16_t nr;
43                     get_user_u16(nr, env->pc + 2);
44                     env->pc += 4;
45                     do_m68k_simcall(env, nr);
46                 } else {
47                     goto do_sigill;
48                 }
49             }
50             break;
51         case EXCP_HALT_INSN:
52             /* Semihosing syscall.  */
53             env->pc += 4;
54             do_m68k_semihosting(env, env->dregs[0]);
55             break;
56         case EXCP_LINEA:
57         case EXCP_LINEF:
58         do_sigill:
59             info.si_signo = TARGET_SIGILL;
60             info.si_errno = 0;
61             info.si_code = TARGET_ILL_ILLOPN;
62             info._sifields._sigfault._addr = env->pc;
63             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
64             break;
65         case EXCP_CHK:
66             info.si_signo = TARGET_SIGFPE;
67             info.si_errno = 0;
68             info.si_code = TARGET_FPE_INTOVF;
69             info._sifields._sigfault._addr = env->pc;
70             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
71             break;
72         case EXCP_DIV0:
73             info.si_signo = TARGET_SIGFPE;
74             info.si_errno = 0;
75             info.si_code = TARGET_FPE_INTDIV;
76             info._sifields._sigfault._addr = env->pc;
77             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
78             break;
79         case EXCP_TRAP0:
80             {
81                 abi_long ret;
82                 ts->sim_syscalls = 0;
83                 n = env->dregs[0];
84                 env->pc += 2;
85                 ret = do_syscall(env,
86                                  n,
87                                  env->dregs[1],
88                                  env->dregs[2],
89                                  env->dregs[3],
90                                  env->dregs[4],
91                                  env->dregs[5],
92                                  env->aregs[0],
93                                  0, 0);
94                 if (ret == -TARGET_ERESTARTSYS) {
95                     env->pc -= 2;
96                 } else if (ret != -TARGET_QEMU_ESIGRETURN) {
97                     env->dregs[0] = ret;
98                 }
99             }
100             break;
101         case EXCP_INTERRUPT:
102             /* just indicate that signals should be handled asap */
103             break;
104         case EXCP_ACCESS:
105             {
106                 info.si_signo = TARGET_SIGSEGV;
107                 info.si_errno = 0;
108                 /* XXX: check env->error_code */
109                 info.si_code = TARGET_SEGV_MAPERR;
110                 info._sifields._sigfault._addr = env->mmu.ar;
111                 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
112             }
113             break;
114         case EXCP_DEBUG:
115             {
116                 int sig;
117 
118                 sig = gdb_handlesig(cs, TARGET_SIGTRAP);
119                 if (sig)
120                   {
121                     info.si_signo = sig;
122                     info.si_errno = 0;
123                     info.si_code = TARGET_TRAP_BRKPT;
124                     queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
125                   }
126             }
127             break;
128         case EXCP_ATOMIC:
129             cpu_exec_step_atomic(cs);
130             break;
131         default:
132             EXCP_DUMP(env, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr);
133             abort();
134         }
135         process_pending_signals(env);
136     }
137 }
138 
139 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
140 {
141     CPUState *cpu = ENV_GET_CPU(env);
142     TaskState *ts = cpu->opaque;
143     struct image_info *info = ts->info;
144 
145     env->pc = regs->pc;
146     env->dregs[0] = regs->d0;
147     env->dregs[1] = regs->d1;
148     env->dregs[2] = regs->d2;
149     env->dregs[3] = regs->d3;
150     env->dregs[4] = regs->d4;
151     env->dregs[5] = regs->d5;
152     env->dregs[6] = regs->d6;
153     env->dregs[7] = regs->d7;
154     env->aregs[0] = regs->a0;
155     env->aregs[1] = regs->a1;
156     env->aregs[2] = regs->a2;
157     env->aregs[3] = regs->a3;
158     env->aregs[4] = regs->a4;
159     env->aregs[5] = regs->a5;
160     env->aregs[6] = regs->a6;
161     env->aregs[7] = regs->usp;
162     env->sr = regs->sr;
163 
164     ts->sim_syscalls = 1;
165     ts->stack_base = info->start_stack;
166     ts->heap_base = info->brk;
167     /* This will be filled in on the first SYS_HEAPINFO call.  */
168     ts->heap_limit = 0;
169 }
170