]> git.proxmox.com Git - qemu.git/blob - target-m68k/op_helper.c
exec: refactor cpu_restore_state
[qemu.git] / target-m68k / op_helper.c
1 /*
2 * M68K helper routines
3 *
4 * Copyright (c) 2007 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "cpu.h"
20 #include "helpers.h"
21
22 #if defined(CONFIG_USER_ONLY)
23
24 void do_interrupt(CPUM68KState *env)
25 {
26 env->exception_index = -1;
27 }
28
29 void do_interrupt_m68k_hardirq(CPUM68KState *env)
30 {
31 }
32
33 #else
34
35 extern int semihosting_enabled;
36
37 #include "softmmu_exec.h"
38
39 #define MMUSUFFIX _mmu
40
41 #define SHIFT 0
42 #include "softmmu_template.h"
43
44 #define SHIFT 1
45 #include "softmmu_template.h"
46
47 #define SHIFT 2
48 #include "softmmu_template.h"
49
50 #define SHIFT 3
51 #include "softmmu_template.h"
52
53 /* Try to fill the TLB and return an exception if error. If retaddr is
54 NULL, it means that the function was called in C code (i.e. not
55 from generated code or from helper.c) */
56 void tlb_fill(CPUM68KState *env, target_ulong addr, int is_write, int mmu_idx,
57 uintptr_t retaddr)
58 {
59 int ret;
60
61 ret = cpu_m68k_handle_mmu_fault(env, addr, is_write, mmu_idx);
62 if (unlikely(ret)) {
63 if (retaddr) {
64 /* now we have a real cpu fault */
65 cpu_restore_state(env, retaddr);
66 }
67 cpu_loop_exit(env);
68 }
69 }
70
71 static void do_rte(CPUM68KState *env)
72 {
73 uint32_t sp;
74 uint32_t fmt;
75
76 sp = env->aregs[7];
77 fmt = cpu_ldl_kernel(env, sp);
78 env->pc = cpu_ldl_kernel(env, sp + 4);
79 sp |= (fmt >> 28) & 3;
80 env->sr = fmt & 0xffff;
81 m68k_switch_sp(env);
82 env->aregs[7] = sp + 8;
83 }
84
85 static void do_interrupt_all(CPUM68KState *env, int is_hw)
86 {
87 uint32_t sp;
88 uint32_t fmt;
89 uint32_t retaddr;
90 uint32_t vector;
91
92 fmt = 0;
93 retaddr = env->pc;
94
95 if (!is_hw) {
96 switch (env->exception_index) {
97 case EXCP_RTE:
98 /* Return from an exception. */
99 do_rte(env);
100 return;
101 case EXCP_HALT_INSN:
102 if (semihosting_enabled
103 && (env->sr & SR_S) != 0
104 && (env->pc & 3) == 0
105 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
106 && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
107 env->pc += 4;
108 do_m68k_semihosting(env, env->dregs[0]);
109 return;
110 }
111 env->halted = 1;
112 env->exception_index = EXCP_HLT;
113 cpu_loop_exit(env);
114 return;
115 }
116 if (env->exception_index >= EXCP_TRAP0
117 && env->exception_index <= EXCP_TRAP15) {
118 /* Move the PC after the trap instruction. */
119 retaddr += 2;
120 }
121 }
122
123 vector = env->exception_index << 2;
124
125 sp = env->aregs[7];
126
127 fmt |= 0x40000000;
128 fmt |= (sp & 3) << 28;
129 fmt |= vector << 16;
130 fmt |= env->sr;
131
132 env->sr |= SR_S;
133 if (is_hw) {
134 env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
135 env->sr &= ~SR_M;
136 }
137 m68k_switch_sp(env);
138
139 /* ??? This could cause MMU faults. */
140 sp &= ~3;
141 sp -= 4;
142 cpu_stl_kernel(env, sp, retaddr);
143 sp -= 4;
144 cpu_stl_kernel(env, sp, fmt);
145 env->aregs[7] = sp;
146 /* Jump to vector. */
147 env->pc = cpu_ldl_kernel(env, env->vbr + vector);
148 }
149
150 void do_interrupt(CPUM68KState *env)
151 {
152 do_interrupt_all(env, 0);
153 }
154
155 void do_interrupt_m68k_hardirq(CPUM68KState *env)
156 {
157 do_interrupt_all(env, 1);
158 }
159 #endif
160
161 static void raise_exception(CPUM68KState *env, int tt)
162 {
163 env->exception_index = tt;
164 cpu_loop_exit(env);
165 }
166
167 void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
168 {
169 raise_exception(env, tt);
170 }
171
172 void HELPER(divu)(CPUM68KState *env, uint32_t word)
173 {
174 uint32_t num;
175 uint32_t den;
176 uint32_t quot;
177 uint32_t rem;
178 uint32_t flags;
179
180 num = env->div1;
181 den = env->div2;
182 /* ??? This needs to make sure the throwing location is accurate. */
183 if (den == 0) {
184 raise_exception(env, EXCP_DIV0);
185 }
186 quot = num / den;
187 rem = num % den;
188 flags = 0;
189 if (word && quot > 0xffff)
190 flags |= CCF_V;
191 if (quot == 0)
192 flags |= CCF_Z;
193 else if ((int32_t)quot < 0)
194 flags |= CCF_N;
195 env->div1 = quot;
196 env->div2 = rem;
197 env->cc_dest = flags;
198 }
199
200 void HELPER(divs)(CPUM68KState *env, uint32_t word)
201 {
202 int32_t num;
203 int32_t den;
204 int32_t quot;
205 int32_t rem;
206 int32_t flags;
207
208 num = env->div1;
209 den = env->div2;
210 if (den == 0) {
211 raise_exception(env, EXCP_DIV0);
212 }
213 quot = num / den;
214 rem = num % den;
215 flags = 0;
216 if (word && quot != (int16_t)quot)
217 flags |= CCF_V;
218 if (quot == 0)
219 flags |= CCF_Z;
220 else if (quot < 0)
221 flags |= CCF_N;
222 env->div1 = quot;
223 env->div2 = rem;
224 env->cc_dest = flags;
225 }