]>
Commit | Line | Data |
---|---|---|
cd71c089 LV |
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" | |
2113aed6 | 23 | #include "signal-common.h" |
cd71c089 | 24 | |
0ec0f01c LV |
25 | void cpu_loop(CPUNios2State *env) |
26 | { | |
29a0af61 | 27 | CPUState *cs = env_cpu(env); |
0ec0f01c LV |
28 | Nios2CPU *cpu = NIOS2_CPU(cs); |
29 | target_siginfo_t info; | |
9f214bd3 | 30 | int trapnr, ret; |
0ec0f01c LV |
31 | |
32 | for (;;) { | |
33 | cpu_exec_start(cs); | |
34 | trapnr = cpu_exec(cs); | |
35 | cpu_exec_end(cs); | |
0ec0f01c LV |
36 | |
37 | switch (trapnr) { | |
38 | case EXCP_INTERRUPT: | |
39 | /* just indicate that signals should be handled asap */ | |
40 | break; | |
41 | case EXCP_TRAP: | |
42 | if (env->regs[R_AT] == 0) { | |
43 | abi_long ret; | |
44 | qemu_log_mask(CPU_LOG_INT, "\nSyscall\n"); | |
45 | ||
46 | ret = do_syscall(env, env->regs[2], | |
47 | env->regs[4], env->regs[5], env->regs[6], | |
48 | env->regs[7], env->regs[8], env->regs[9], | |
49 | 0, 0); | |
50 | ||
51 | if (env->regs[2] == 0) { /* FIXME: syscall 0 workaround */ | |
52 | ret = 0; | |
53 | } | |
54 | ||
55 | env->regs[2] = abs(ret); | |
56 | /* Return value is 0..4096 */ | |
57 | env->regs[7] = (ret > 0xfffffffffffff000ULL); | |
58 | env->regs[CR_ESTATUS] = env->regs[CR_STATUS]; | |
59 | env->regs[CR_STATUS] &= ~0x3; | |
60 | env->regs[R_EA] = env->regs[R_PC] + 4; | |
61 | env->regs[R_PC] += 4; | |
62 | break; | |
63 | } else { | |
64 | qemu_log_mask(CPU_LOG_INT, "\nTrap\n"); | |
65 | ||
66 | env->regs[CR_ESTATUS] = env->regs[CR_STATUS]; | |
67 | env->regs[CR_STATUS] &= ~0x3; | |
68 | env->regs[R_EA] = env->regs[R_PC] + 4; | |
69 | env->regs[R_PC] = cpu->exception_addr; | |
70 | ||
9f214bd3 PM |
71 | info.si_signo = TARGET_SIGTRAP; |
72 | info.si_errno = 0; | |
73 | info.si_code = TARGET_TRAP_BRKPT; | |
74 | queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info); | |
0ec0f01c LV |
75 | break; |
76 | } | |
14c8a3a1 SL |
77 | case EXCP_DEBUG: |
78 | info.si_signo = TARGET_SIGTRAP; | |
79 | info.si_errno = 0; | |
80 | info.si_code = TARGET_TRAP_BRKPT; | |
81 | queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info); | |
82 | break; | |
0ec0f01c LV |
83 | case 0xaa: |
84 | switch (env->regs[R_PC]) { | |
85 | /*case 0x1000:*/ /* TODO:__kuser_helper_version */ | |
86 | case 0x1004: /* __kuser_cmpxchg */ | |
87 | start_exclusive(); | |
88 | if (env->regs[4] & 0x3) { | |
89 | goto kuser_fail; | |
90 | } | |
91 | ret = get_user_u32(env->regs[2], env->regs[4]); | |
92 | if (ret) { | |
93 | end_exclusive(); | |
94 | goto kuser_fail; | |
95 | } | |
96 | env->regs[2] -= env->regs[5]; | |
97 | if (env->regs[2] == 0) { | |
98 | put_user_u32(env->regs[6], env->regs[4]); | |
99 | } | |
100 | end_exclusive(); | |
101 | env->regs[R_PC] = env->regs[R_RA]; | |
102 | break; | |
103 | /*case 0x1040:*/ /* TODO:__kuser_sigtramp */ | |
104 | default: | |
105 | ; | |
106 | kuser_fail: | |
107 | info.si_signo = TARGET_SIGSEGV; | |
108 | info.si_errno = 0; | |
109 | /* TODO: check env->error_code */ | |
110 | info.si_code = TARGET_SEGV_MAPERR; | |
111 | info._sifields._sigfault._addr = env->regs[R_PC]; | |
112 | queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info); | |
113 | } | |
114 | break; | |
115 | default: | |
116 | EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n", | |
117 | trapnr); | |
9f214bd3 | 118 | abort(); |
0ec0f01c LV |
119 | } |
120 | ||
121 | process_pending_signals(env); | |
122 | } | |
123 | } | |
124 | ||
cd71c089 LV |
125 | void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs) |
126 | { | |
0ec0f01c LV |
127 | env->regs[0] = 0; |
128 | env->regs[1] = regs->r1; | |
129 | env->regs[2] = regs->r2; | |
130 | env->regs[3] = regs->r3; | |
131 | env->regs[4] = regs->r4; | |
132 | env->regs[5] = regs->r5; | |
133 | env->regs[6] = regs->r6; | |
134 | env->regs[7] = regs->r7; | |
135 | env->regs[8] = regs->r8; | |
136 | env->regs[9] = regs->r9; | |
137 | env->regs[10] = regs->r10; | |
138 | env->regs[11] = regs->r11; | |
139 | env->regs[12] = regs->r12; | |
140 | env->regs[13] = regs->r13; | |
141 | env->regs[14] = regs->r14; | |
142 | env->regs[15] = regs->r15; | |
143 | /* TODO: unsigned long orig_r2; */ | |
144 | env->regs[R_RA] = regs->ra; | |
145 | env->regs[R_FP] = regs->fp; | |
146 | env->regs[R_SP] = regs->sp; | |
147 | env->regs[R_GP] = regs->gp; | |
148 | env->regs[CR_ESTATUS] = regs->estatus; | |
149 | env->regs[R_EA] = regs->ea; | |
150 | /* TODO: unsigned long orig_r7; */ | |
151 | ||
152 | /* Emulate eret when starting thread. */ | |
153 | env->regs[R_PC] = regs->ea; | |
cd71c089 | 154 | } |