]>
Commit | Line | Data |
---|---|---|
0633879f PB |
1 | /* |
2 | * M68K helper routines | |
5fafdf24 | 3 | * |
0633879f PB |
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
0633879f | 18 | */ |
3e457172 BS |
19 | #include "cpu.h" |
20 | #include "dyngen-exec.h" | |
e1f3808e | 21 | #include "helpers.h" |
0633879f PB |
22 | |
23 | #if defined(CONFIG_USER_ONLY) | |
24 | ||
3c688828 BS |
25 | void do_interrupt(CPUState *env1) |
26 | { | |
27 | env1->exception_index = -1; | |
28 | } | |
29 | ||
30 | void do_interrupt_m68k_hardirq(CPUState *env1) | |
0633879f | 31 | { |
0633879f PB |
32 | } |
33 | ||
34 | #else | |
35 | ||
a87295e8 PB |
36 | extern int semihosting_enabled; |
37 | ||
3e457172 BS |
38 | #include "softmmu_exec.h" |
39 | ||
0633879f | 40 | #define MMUSUFFIX _mmu |
0633879f PB |
41 | |
42 | #define SHIFT 0 | |
43 | #include "softmmu_template.h" | |
44 | ||
45 | #define SHIFT 1 | |
46 | #include "softmmu_template.h" | |
47 | ||
48 | #define SHIFT 2 | |
49 | #include "softmmu_template.h" | |
50 | ||
51 | #define SHIFT 3 | |
52 | #include "softmmu_template.h" | |
53 | ||
54 | /* Try to fill the TLB and return an exception if error. If retaddr is | |
55 | NULL, it means that the function was called in C code (i.e. not | |
56 | from generated code or from helper.c) */ | |
57 | /* XXX: fix it to restore all registers */ | |
6ebbf390 | 58 | void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr) |
0633879f PB |
59 | { |
60 | TranslationBlock *tb; | |
61 | CPUState *saved_env; | |
44f8625d | 62 | unsigned long pc; |
0633879f PB |
63 | int ret; |
64 | ||
65 | /* XXX: hack to restore env in all cases, even if not called from | |
66 | generated code */ | |
67 | saved_env = env; | |
68 | env = cpu_single_env; | |
6ebbf390 | 69 | ret = cpu_m68k_handle_mmu_fault(env, addr, is_write, mmu_idx, 1); |
551bd27f | 70 | if (unlikely(ret)) { |
0633879f PB |
71 | if (retaddr) { |
72 | /* now we have a real cpu fault */ | |
44f8625d | 73 | pc = (unsigned long)retaddr; |
0633879f PB |
74 | tb = tb_find_pc(pc); |
75 | if (tb) { | |
76 | /* the PC is inside the translated code. It means that we have | |
77 | a virtual CPU fault */ | |
618ba8e6 | 78 | cpu_restore_state(tb, env, pc); |
0633879f PB |
79 | } |
80 | } | |
1162c041 | 81 | cpu_loop_exit(env); |
0633879f PB |
82 | } |
83 | env = saved_env; | |
84 | } | |
85 | ||
86 | static void do_rte(void) | |
87 | { | |
88 | uint32_t sp; | |
89 | uint32_t fmt; | |
90 | ||
91 | sp = env->aregs[7]; | |
92 | fmt = ldl_kernel(sp); | |
93 | env->pc = ldl_kernel(sp + 4); | |
94 | sp |= (fmt >> 28) & 3; | |
95 | env->sr = fmt & 0xffff; | |
20dcee94 | 96 | m68k_switch_sp(env); |
0633879f PB |
97 | env->aregs[7] = sp + 8; |
98 | } | |
99 | ||
3c688828 | 100 | static void do_interrupt_all(int is_hw) |
0633879f PB |
101 | { |
102 | uint32_t sp; | |
103 | uint32_t fmt; | |
104 | uint32_t retaddr; | |
105 | uint32_t vector; | |
106 | ||
107 | fmt = 0; | |
108 | retaddr = env->pc; | |
109 | ||
110 | if (!is_hw) { | |
111 | switch (env->exception_index) { | |
112 | case EXCP_RTE: | |
113 | /* Return from an exception. */ | |
114 | do_rte(); | |
115 | return; | |
a87295e8 PB |
116 | case EXCP_HALT_INSN: |
117 | if (semihosting_enabled | |
118 | && (env->sr & SR_S) != 0 | |
119 | && (env->pc & 3) == 0 | |
120 | && lduw_code(env->pc - 4) == 0x4e71 | |
121 | && ldl_code(env->pc) == 0x4e7bf000) { | |
122 | env->pc += 4; | |
123 | do_m68k_semihosting(env, env->dregs[0]); | |
124 | return; | |
125 | } | |
126 | env->halted = 1; | |
127 | env->exception_index = EXCP_HLT; | |
1162c041 | 128 | cpu_loop_exit(env); |
a87295e8 | 129 | return; |
0633879f PB |
130 | } |
131 | if (env->exception_index >= EXCP_TRAP0 | |
132 | && env->exception_index <= EXCP_TRAP15) { | |
133 | /* Move the PC after the trap instruction. */ | |
134 | retaddr += 2; | |
135 | } | |
136 | } | |
137 | ||
0633879f PB |
138 | vector = env->exception_index << 2; |
139 | ||
0cf5c677 PB |
140 | sp = env->aregs[7]; |
141 | ||
0633879f PB |
142 | fmt |= 0x40000000; |
143 | fmt |= (sp & 3) << 28; | |
144 | fmt |= vector << 16; | |
145 | fmt |= env->sr; | |
146 | ||
20dcee94 PB |
147 | env->sr |= SR_S; |
148 | if (is_hw) { | |
149 | env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT); | |
150 | env->sr &= ~SR_M; | |
151 | } | |
152 | m68k_switch_sp(env); | |
153 | ||
0633879f PB |
154 | /* ??? This could cause MMU faults. */ |
155 | sp &= ~3; | |
156 | sp -= 4; | |
157 | stl_kernel(sp, retaddr); | |
158 | sp -= 4; | |
159 | stl_kernel(sp, fmt); | |
160 | env->aregs[7] = sp; | |
0633879f PB |
161 | /* Jump to vector. */ |
162 | env->pc = ldl_kernel(env->vbr + vector); | |
163 | } | |
164 | ||
3c688828 BS |
165 | void do_interrupt(CPUState *env1) |
166 | { | |
167 | CPUState *saved_env; | |
168 | ||
169 | saved_env = env; | |
170 | env = env1; | |
171 | do_interrupt_all(0); | |
172 | env = saved_env; | |
173 | } | |
174 | ||
175 | void do_interrupt_m68k_hardirq(CPUState *env1) | |
176 | { | |
177 | CPUState *saved_env; | |
178 | ||
179 | saved_env = env; | |
180 | env = env1; | |
181 | do_interrupt_all(1); | |
182 | env = saved_env; | |
183 | } | |
0633879f | 184 | #endif |
e1f3808e PB |
185 | |
186 | static void raise_exception(int tt) | |
187 | { | |
188 | env->exception_index = tt; | |
1162c041 | 189 | cpu_loop_exit(env); |
e1f3808e PB |
190 | } |
191 | ||
192 | void HELPER(raise_exception)(uint32_t tt) | |
193 | { | |
194 | raise_exception(tt); | |
195 | } | |
196 | ||
197 | void HELPER(divu)(CPUState *env, uint32_t word) | |
198 | { | |
199 | uint32_t num; | |
200 | uint32_t den; | |
201 | uint32_t quot; | |
202 | uint32_t rem; | |
203 | uint32_t flags; | |
204 | ||
205 | num = env->div1; | |
206 | den = env->div2; | |
207 | /* ??? This needs to make sure the throwing location is accurate. */ | |
208 | if (den == 0) | |
209 | raise_exception(EXCP_DIV0); | |
210 | quot = num / den; | |
211 | rem = num % den; | |
212 | flags = 0; | |
213 | /* Avoid using a PARAM1 of zero. This breaks dyngen because it uses | |
214 | the address of a symbol, and gcc knows symbols can't have address | |
215 | zero. */ | |
216 | if (word && quot > 0xffff) | |
217 | flags |= CCF_V; | |
218 | if (quot == 0) | |
219 | flags |= CCF_Z; | |
220 | else if ((int32_t)quot < 0) | |
221 | flags |= CCF_N; | |
222 | env->div1 = quot; | |
223 | env->div2 = rem; | |
224 | env->cc_dest = flags; | |
225 | } | |
226 | ||
227 | void HELPER(divs)(CPUState *env, uint32_t word) | |
228 | { | |
229 | int32_t num; | |
230 | int32_t den; | |
231 | int32_t quot; | |
232 | int32_t rem; | |
233 | int32_t flags; | |
234 | ||
235 | num = env->div1; | |
236 | den = env->div2; | |
237 | if (den == 0) | |
238 | raise_exception(EXCP_DIV0); | |
239 | quot = num / den; | |
240 | rem = num % den; | |
241 | flags = 0; | |
242 | if (word && quot != (int16_t)quot) | |
243 | flags |= CCF_V; | |
244 | if (quot == 0) | |
245 | flags |= CCF_Z; | |
246 | else if (quot < 0) | |
247 | flags |= CCF_N; | |
248 | env->div1 = quot; | |
249 | env->div2 = rem; | |
250 | env->cc_dest = flags; | |
251 | } |