s390x/helper: Fixed real-to-absolute address translation
[qemu/kevin.git] / target-m68k / op_helper.c
blob06302b1071d4df25e109b97e33506a38927f0edc
1 /*
2 * M68K helper routines
4 * Copyright (c) 2007 CodeSourcery
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.
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.
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/>.
19 #include "cpu.h"
20 #include "helper.h"
22 #if defined(CONFIG_USER_ONLY)
24 void m68k_cpu_do_interrupt(CPUState *cs)
26 cs->exception_index = -1;
29 void do_interrupt_m68k_hardirq(CPUM68KState *env)
33 #else
35 extern int semihosting_enabled;
37 #include "exec/softmmu_exec.h"
39 #define MMUSUFFIX _mmu
41 #define SHIFT 0
42 #include "exec/softmmu_template.h"
44 #define SHIFT 1
45 #include "exec/softmmu_template.h"
47 #define SHIFT 2
48 #include "exec/softmmu_template.h"
50 #define SHIFT 3
51 #include "exec/softmmu_template.h"
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(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
57 uintptr_t retaddr)
59 int ret;
61 ret = m68k_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
62 if (unlikely(ret)) {
63 if (retaddr) {
64 /* now we have a real cpu fault */
65 cpu_restore_state(cs, retaddr);
67 cpu_loop_exit(cs);
71 static void do_rte(CPUM68KState *env)
73 uint32_t sp;
74 uint32_t fmt;
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;
85 static void do_interrupt_all(CPUM68KState *env, int is_hw)
87 CPUState *cs = CPU(m68k_env_get_cpu(env));
88 uint32_t sp;
89 uint32_t fmt;
90 uint32_t retaddr;
91 uint32_t vector;
93 fmt = 0;
94 retaddr = env->pc;
96 if (!is_hw) {
97 switch (cs->exception_index) {
98 case EXCP_RTE:
99 /* Return from an exception. */
100 do_rte(env);
101 return;
102 case EXCP_HALT_INSN:
103 if (semihosting_enabled
104 && (env->sr & SR_S) != 0
105 && (env->pc & 3) == 0
106 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
107 && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
108 env->pc += 4;
109 do_m68k_semihosting(env, env->dregs[0]);
110 return;
112 cs->halted = 1;
113 cs->exception_index = EXCP_HLT;
114 cpu_loop_exit(cs);
115 return;
117 if (cs->exception_index >= EXCP_TRAP0
118 && cs->exception_index <= EXCP_TRAP15) {
119 /* Move the PC after the trap instruction. */
120 retaddr += 2;
124 vector = cs->exception_index << 2;
126 sp = env->aregs[7];
128 fmt |= 0x40000000;
129 fmt |= (sp & 3) << 28;
130 fmt |= vector << 16;
131 fmt |= env->sr;
133 env->sr |= SR_S;
134 if (is_hw) {
135 env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
136 env->sr &= ~SR_M;
138 m68k_switch_sp(env);
140 /* ??? This could cause MMU faults. */
141 sp &= ~3;
142 sp -= 4;
143 cpu_stl_kernel(env, sp, retaddr);
144 sp -= 4;
145 cpu_stl_kernel(env, sp, fmt);
146 env->aregs[7] = sp;
147 /* Jump to vector. */
148 env->pc = cpu_ldl_kernel(env, env->vbr + vector);
151 void m68k_cpu_do_interrupt(CPUState *cs)
153 M68kCPU *cpu = M68K_CPU(cs);
154 CPUM68KState *env = &cpu->env;
156 do_interrupt_all(env, 0);
159 void do_interrupt_m68k_hardirq(CPUM68KState *env)
161 do_interrupt_all(env, 1);
163 #endif
165 static void raise_exception(CPUM68KState *env, int tt)
167 CPUState *cs = CPU(m68k_env_get_cpu(env));
169 cs->exception_index = tt;
170 cpu_loop_exit(cs);
173 void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
175 raise_exception(env, tt);
178 void HELPER(divu)(CPUM68KState *env, uint32_t word)
180 uint32_t num;
181 uint32_t den;
182 uint32_t quot;
183 uint32_t rem;
184 uint32_t flags;
186 num = env->div1;
187 den = env->div2;
188 /* ??? This needs to make sure the throwing location is accurate. */
189 if (den == 0) {
190 raise_exception(env, EXCP_DIV0);
192 quot = num / den;
193 rem = num % den;
194 flags = 0;
195 if (word && quot > 0xffff)
196 flags |= CCF_V;
197 if (quot == 0)
198 flags |= CCF_Z;
199 else if ((int32_t)quot < 0)
200 flags |= CCF_N;
201 env->div1 = quot;
202 env->div2 = rem;
203 env->cc_dest = flags;
206 void HELPER(divs)(CPUM68KState *env, uint32_t word)
208 int32_t num;
209 int32_t den;
210 int32_t quot;
211 int32_t rem;
212 int32_t flags;
214 num = env->div1;
215 den = env->div2;
216 if (den == 0) {
217 raise_exception(env, EXCP_DIV0);
219 quot = num / den;
220 rem = num % den;
221 flags = 0;
222 if (word && quot != (int16_t)quot)
223 flags |= CCF_V;
224 if (quot == 0)
225 flags |= CCF_Z;
226 else if (quot < 0)
227 flags |= CCF_N;
228 env->div1 = quot;
229 env->div2 = rem;
230 env->cc_dest = flags;