xref: /qemu/linux-user/openrisc/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 void cpu_loop(CPUOpenRISCState *env)
28 {
29     CPUState *cs = env_cpu(env);
30     int trapnr;
31     abi_long ret;
32     target_siginfo_t info;
33 
34     for (;;) {
35         cpu_exec_start(cs);
36         trapnr = cpu_exec(cs);
37         cpu_exec_end(cs);
38         process_queued_cpu_work(cs);
39 
40         switch (trapnr) {
41         case EXCP_SYSCALL:
42             env->pc += 4;   /* 0xc00; */
43             ret = do_syscall(env,
44                              cpu_get_gpr(env, 11), /* return value       */
45                              cpu_get_gpr(env, 3),  /* r3 - r7 are params */
46                              cpu_get_gpr(env, 4),
47                              cpu_get_gpr(env, 5),
48                              cpu_get_gpr(env, 6),
49                              cpu_get_gpr(env, 7),
50                              cpu_get_gpr(env, 8), 0, 0);
51             if (ret == -TARGET_ERESTARTSYS) {
52                 env->pc -= 4;
53             } else if (ret != -TARGET_QEMU_ESIGRETURN) {
54                 cpu_set_gpr(env, 11, ret);
55             }
56             break;
57         case EXCP_DPF:
58         case EXCP_IPF:
59         case EXCP_RANGE:
60             info.si_signo = TARGET_SIGSEGV;
61             info.si_errno = 0;
62             info.si_code = TARGET_SEGV_MAPERR;
63             info._sifields._sigfault._addr = env->pc;
64             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
65             break;
66         case EXCP_ALIGN:
67             info.si_signo = TARGET_SIGBUS;
68             info.si_errno = 0;
69             info.si_code = TARGET_BUS_ADRALN;
70             info._sifields._sigfault._addr = env->pc;
71             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
72             break;
73         case EXCP_ILLEGAL:
74             info.si_signo = TARGET_SIGILL;
75             info.si_errno = 0;
76             info.si_code = TARGET_ILL_ILLOPC;
77             info._sifields._sigfault._addr = env->pc;
78             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
79             break;
80         case EXCP_FPE:
81             info.si_signo = TARGET_SIGFPE;
82             info.si_errno = 0;
83             info.si_code = 0;
84             info._sifields._sigfault._addr = env->pc;
85             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
86             break;
87         case EXCP_INTERRUPT:
88             /* We processed the pending cpu work above.  */
89             break;
90         case EXCP_DEBUG:
91             info.si_signo = TARGET_SIGTRAP;
92             info.si_errno = 0;
93             info.si_code = TARGET_TRAP_BRKPT;
94             queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
95             break;
96         case EXCP_ATOMIC:
97             cpu_exec_step_atomic(cs);
98             break;
99         default:
100             g_assert_not_reached();
101         }
102         process_pending_signals(env);
103     }
104 }
105 
106 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
107 {
108     int i;
109 
110     for (i = 0; i < 32; i++) {
111         cpu_set_gpr(env, i, regs->gpr[i]);
112     }
113     env->pc = regs->pc;
114     cpu_set_sr(env, regs->sr);
115 }
116