Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-2.6-20160426' into staging
[qemu.git] / target-m68k / op_helper.c
blob17d0a11914aab1c61a24e0d7ab3d1800689760b3
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 "qemu/osdep.h"
20 #include "cpu.h"
21 #include "exec/helper-proto.h"
22 #include "exec/cpu_ldst.h"
23 #include "exec/semihost.h"
25 #if defined(CONFIG_USER_ONLY)
27 void m68k_cpu_do_interrupt(CPUState *cs)
29 cs->exception_index = -1;
32 static inline void do_interrupt_m68k_hardirq(CPUM68KState *env)
36 #else
38 /* Try to fill the TLB and return an exception if error. If retaddr is
39 NULL, it means that the function was called in C code (i.e. not
40 from generated code or from helper.c) */
41 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
42 uintptr_t retaddr)
44 int ret;
46 ret = m68k_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
47 if (unlikely(ret)) {
48 if (retaddr) {
49 /* now we have a real cpu fault */
50 cpu_restore_state(cs, retaddr);
52 cpu_loop_exit(cs);
56 static void do_rte(CPUM68KState *env)
58 uint32_t sp;
59 uint32_t fmt;
61 sp = env->aregs[7];
62 fmt = cpu_ldl_kernel(env, sp);
63 env->pc = cpu_ldl_kernel(env, sp + 4);
64 sp |= (fmt >> 28) & 3;
65 env->sr = fmt & 0xffff;
66 env->aregs[7] = sp + 8;
67 m68k_switch_sp(env);
70 static void do_interrupt_all(CPUM68KState *env, int is_hw)
72 CPUState *cs = CPU(m68k_env_get_cpu(env));
73 uint32_t sp;
74 uint32_t fmt;
75 uint32_t retaddr;
76 uint32_t vector;
78 fmt = 0;
79 retaddr = env->pc;
81 if (!is_hw) {
82 switch (cs->exception_index) {
83 case EXCP_RTE:
84 /* Return from an exception. */
85 do_rte(env);
86 return;
87 case EXCP_HALT_INSN:
88 if (semihosting_enabled()
89 && (env->sr & SR_S) != 0
90 && (env->pc & 3) == 0
91 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
92 && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
93 env->pc += 4;
94 do_m68k_semihosting(env, env->dregs[0]);
95 return;
97 cs->halted = 1;
98 cs->exception_index = EXCP_HLT;
99 cpu_loop_exit(cs);
100 return;
102 if (cs->exception_index >= EXCP_TRAP0
103 && cs->exception_index <= EXCP_TRAP15) {
104 /* Move the PC after the trap instruction. */
105 retaddr += 2;
109 vector = cs->exception_index << 2;
111 fmt |= 0x40000000;
112 fmt |= vector << 16;
113 fmt |= env->sr;
115 env->sr |= SR_S;
116 if (is_hw) {
117 env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
118 env->sr &= ~SR_M;
120 m68k_switch_sp(env);
121 sp = env->aregs[7];
122 fmt |= (sp & 3) << 28;
124 /* ??? This could cause MMU faults. */
125 sp &= ~3;
126 sp -= 4;
127 cpu_stl_kernel(env, sp, retaddr);
128 sp -= 4;
129 cpu_stl_kernel(env, sp, fmt);
130 env->aregs[7] = sp;
131 /* Jump to vector. */
132 env->pc = cpu_ldl_kernel(env, env->vbr + vector);
135 void m68k_cpu_do_interrupt(CPUState *cs)
137 M68kCPU *cpu = M68K_CPU(cs);
138 CPUM68KState *env = &cpu->env;
140 do_interrupt_all(env, 0);
143 static inline void do_interrupt_m68k_hardirq(CPUM68KState *env)
145 do_interrupt_all(env, 1);
147 #endif
149 bool m68k_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
151 M68kCPU *cpu = M68K_CPU(cs);
152 CPUM68KState *env = &cpu->env;
154 if (interrupt_request & CPU_INTERRUPT_HARD
155 && ((env->sr & SR_I) >> SR_I_SHIFT) < env->pending_level) {
156 /* Real hardware gets the interrupt vector via an IACK cycle
157 at this point. Current emulated hardware doesn't rely on
158 this, so we provide/save the vector when the interrupt is
159 first signalled. */
160 cs->exception_index = env->pending_vector;
161 do_interrupt_m68k_hardirq(env);
162 return true;
164 return false;
167 static void raise_exception(CPUM68KState *env, int tt)
169 CPUState *cs = CPU(m68k_env_get_cpu(env));
171 cs->exception_index = tt;
172 cpu_loop_exit(cs);
175 void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
177 raise_exception(env, tt);
180 void HELPER(divu)(CPUM68KState *env, uint32_t word)
182 uint32_t num;
183 uint32_t den;
184 uint32_t quot;
185 uint32_t rem;
186 uint32_t flags;
188 num = env->div1;
189 den = env->div2;
190 /* ??? This needs to make sure the throwing location is accurate. */
191 if (den == 0) {
192 raise_exception(env, EXCP_DIV0);
194 quot = num / den;
195 rem = num % den;
196 flags = 0;
197 if (word && quot > 0xffff)
198 flags |= CCF_V;
199 if (quot == 0)
200 flags |= CCF_Z;
201 else if ((int32_t)quot < 0)
202 flags |= CCF_N;
203 env->div1 = quot;
204 env->div2 = rem;
205 env->cc_dest = flags;
208 void HELPER(divs)(CPUM68KState *env, uint32_t word)
210 int32_t num;
211 int32_t den;
212 int32_t quot;
213 int32_t rem;
214 int32_t flags;
216 num = env->div1;
217 den = env->div2;
218 if (den == 0) {
219 raise_exception(env, EXCP_DIV0);
221 quot = num / den;
222 rem = num % den;
223 flags = 0;
224 if (word && quot != (int16_t)quot)
225 flags |= CCF_V;
226 if (quot == 0)
227 flags |= CCF_Z;
228 else if (quot < 0)
229 flags |= CCF_N;
230 env->div1 = quot;
231 env->div2 = rem;
232 env->cc_dest = flags;