blob: 17d0a11914aab1c61a24e0d7ab3d1800689760b3 [file] [log] [blame]
pbrook06338792007-05-23 19:58:11 +00001/*
2 * M68K helper routines
ths5fafdf22007-09-16 21:08:06 +00003 *
pbrook06338792007-05-23 19:58:11 +00004 * 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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
pbrook06338792007-05-23 19:58:11 +000018 */
Peter Maydelld8416662016-01-26 18:17:23 +000019#include "qemu/osdep.h"
Blue Swirl3e457172011-07-13 12:44:15 +000020#include "cpu.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070021#include "exec/helper-proto.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010022#include "exec/cpu_ldst.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +010023#include "exec/semihost.h"
pbrook06338792007-05-23 19:58:11 +000024
25#if defined(CONFIG_USER_ONLY)
26
Andreas Färber97a8ea52013-02-02 10:57:51 +010027void m68k_cpu_do_interrupt(CPUState *cs)
pbrook06338792007-05-23 19:58:11 +000028{
Andreas Färber27103422013-08-26 08:31:06 +020029 cs->exception_index = -1;
Blue Swirl3c688822011-05-21 07:55:24 +000030}
31
Richard Hendersonab409bb2014-09-13 09:45:20 -070032static inline void do_interrupt_m68k_hardirq(CPUM68KState *env)
Blue Swirl3c688822011-05-21 07:55:24 +000033{
pbrook06338792007-05-23 19:58:11 +000034}
35
36#else
37
pbrook06338792007-05-23 19:58:11 +000038/* 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) */
Andreas Färberd5a11fe2013-08-27 00:28:06 +020041void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
Blue Swirl20503962012-04-09 14:20:20 +000042 uintptr_t retaddr)
pbrook06338792007-05-23 19:58:11 +000043{
pbrook06338792007-05-23 19:58:11 +000044 int ret;
45
Andreas Färberd5a11fe2013-08-27 00:28:06 +020046 ret = m68k_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
ths551bd272008-07-03 17:57:36 +000047 if (unlikely(ret)) {
pbrook06338792007-05-23 19:58:11 +000048 if (retaddr) {
49 /* now we have a real cpu fault */
Andreas Färber3f38f302013-09-01 16:51:34 +020050 cpu_restore_state(cs, retaddr);
pbrook06338792007-05-23 19:58:11 +000051 }
Andreas Färber5638d182013-08-27 17:52:12 +020052 cpu_loop_exit(cs);
pbrook06338792007-05-23 19:58:11 +000053 }
pbrook06338792007-05-23 19:58:11 +000054}
55
Blue Swirl31871142012-09-02 07:27:38 +000056static void do_rte(CPUM68KState *env)
pbrook06338792007-05-23 19:58:11 +000057{
58 uint32_t sp;
59 uint32_t fmt;
60
61 sp = env->aregs[7];
Blue Swirl31871142012-09-02 07:27:38 +000062 fmt = cpu_ldl_kernel(env, sp);
63 env->pc = cpu_ldl_kernel(env, sp + 4);
pbrook06338792007-05-23 19:58:11 +000064 sp |= (fmt >> 28) & 3;
65 env->sr = fmt & 0xffff;
66 env->aregs[7] = sp + 8;
Greg Ungerer0c8ff722015-06-19 23:43:26 +100067 m68k_switch_sp(env);
pbrook06338792007-05-23 19:58:11 +000068}
69
Blue Swirl31871142012-09-02 07:27:38 +000070static void do_interrupt_all(CPUM68KState *env, int is_hw)
pbrook06338792007-05-23 19:58:11 +000071{
Andreas Färber27103422013-08-26 08:31:06 +020072 CPUState *cs = CPU(m68k_env_get_cpu(env));
pbrook06338792007-05-23 19:58:11 +000073 uint32_t sp;
74 uint32_t fmt;
75 uint32_t retaddr;
76 uint32_t vector;
77
78 fmt = 0;
79 retaddr = env->pc;
80
81 if (!is_hw) {
Andreas Färber27103422013-08-26 08:31:06 +020082 switch (cs->exception_index) {
pbrook06338792007-05-23 19:58:11 +000083 case EXCP_RTE:
84 /* Return from an exception. */
Blue Swirl31871142012-09-02 07:27:38 +000085 do_rte(env);
pbrook06338792007-05-23 19:58:11 +000086 return;
pbrooka87295e2007-05-26 15:09:38 +000087 case EXCP_HALT_INSN:
Leon Alraecfe67ce2015-06-19 14:17:45 +010088 if (semihosting_enabled()
pbrooka87295e2007-05-26 15:09:38 +000089 && (env->sr & SR_S) != 0
90 && (env->pc & 3) == 0
Blue Swirl31871142012-09-02 07:27:38 +000091 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
92 && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
pbrooka87295e2007-05-26 15:09:38 +000093 env->pc += 4;
94 do_m68k_semihosting(env, env->dregs[0]);
95 return;
96 }
Andreas Färber259186a2013-01-17 18:51:17 +010097 cs->halted = 1;
Andreas Färber27103422013-08-26 08:31:06 +020098 cs->exception_index = EXCP_HLT;
Andreas Färber5638d182013-08-27 17:52:12 +020099 cpu_loop_exit(cs);
pbrooka87295e2007-05-26 15:09:38 +0000100 return;
pbrook06338792007-05-23 19:58:11 +0000101 }
Andreas Färber27103422013-08-26 08:31:06 +0200102 if (cs->exception_index >= EXCP_TRAP0
103 && cs->exception_index <= EXCP_TRAP15) {
pbrook06338792007-05-23 19:58:11 +0000104 /* Move the PC after the trap instruction. */
105 retaddr += 2;
106 }
107 }
108
Andreas Färber27103422013-08-26 08:31:06 +0200109 vector = cs->exception_index << 2;
pbrook06338792007-05-23 19:58:11 +0000110
111 fmt |= 0x40000000;
pbrook06338792007-05-23 19:58:11 +0000112 fmt |= vector << 16;
113 fmt |= env->sr;
114
pbrook20dcee92007-06-03 11:13:39 +0000115 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;
119 }
120 m68k_switch_sp(env);
Greg Ungerer0c8ff722015-06-19 23:43:26 +1000121 sp = env->aregs[7];
122 fmt |= (sp & 3) << 28;
pbrook20dcee92007-06-03 11:13:39 +0000123
pbrook06338792007-05-23 19:58:11 +0000124 /* ??? This could cause MMU faults. */
125 sp &= ~3;
126 sp -= 4;
Blue Swirl31871142012-09-02 07:27:38 +0000127 cpu_stl_kernel(env, sp, retaddr);
pbrook06338792007-05-23 19:58:11 +0000128 sp -= 4;
Blue Swirl31871142012-09-02 07:27:38 +0000129 cpu_stl_kernel(env, sp, fmt);
pbrook06338792007-05-23 19:58:11 +0000130 env->aregs[7] = sp;
pbrook06338792007-05-23 19:58:11 +0000131 /* Jump to vector. */
Blue Swirl31871142012-09-02 07:27:38 +0000132 env->pc = cpu_ldl_kernel(env, env->vbr + vector);
pbrook06338792007-05-23 19:58:11 +0000133}
134
Andreas Färber97a8ea52013-02-02 10:57:51 +0100135void m68k_cpu_do_interrupt(CPUState *cs)
Blue Swirl3c688822011-05-21 07:55:24 +0000136{
Andreas Färber97a8ea52013-02-02 10:57:51 +0100137 M68kCPU *cpu = M68K_CPU(cs);
138 CPUM68KState *env = &cpu->env;
139
Blue Swirl31871142012-09-02 07:27:38 +0000140 do_interrupt_all(env, 0);
Blue Swirl3c688822011-05-21 07:55:24 +0000141}
142
Richard Hendersonab409bb2014-09-13 09:45:20 -0700143static inline void do_interrupt_m68k_hardirq(CPUM68KState *env)
Blue Swirl3c688822011-05-21 07:55:24 +0000144{
Blue Swirl31871142012-09-02 07:27:38 +0000145 do_interrupt_all(env, 1);
Blue Swirl3c688822011-05-21 07:55:24 +0000146}
pbrook06338792007-05-23 19:58:11 +0000147#endif
pbrooke1f38082008-05-24 22:29:16 +0000148
Richard Hendersonab409bb2014-09-13 09:45:20 -0700149bool m68k_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
150{
151 M68kCPU *cpu = M68K_CPU(cs);
152 CPUM68KState *env = &cpu->env;
153
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;
163 }
164 return false;
165}
166
Blue Swirl31871142012-09-02 07:27:38 +0000167static void raise_exception(CPUM68KState *env, int tt)
pbrooke1f38082008-05-24 22:29:16 +0000168{
Andreas Färber27103422013-08-26 08:31:06 +0200169 CPUState *cs = CPU(m68k_env_get_cpu(env));
170
171 cs->exception_index = tt;
Andreas Färber5638d182013-08-27 17:52:12 +0200172 cpu_loop_exit(cs);
pbrooke1f38082008-05-24 22:29:16 +0000173}
174
Blue Swirl31871142012-09-02 07:27:38 +0000175void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
pbrooke1f38082008-05-24 22:29:16 +0000176{
Blue Swirl31871142012-09-02 07:27:38 +0000177 raise_exception(env, tt);
pbrooke1f38082008-05-24 22:29:16 +0000178}
179
Andreas Färber2b3e3cf2012-03-14 01:38:22 +0100180void HELPER(divu)(CPUM68KState *env, uint32_t word)
pbrooke1f38082008-05-24 22:29:16 +0000181{
182 uint32_t num;
183 uint32_t den;
184 uint32_t quot;
185 uint32_t rem;
186 uint32_t flags;
187
188 num = env->div1;
189 den = env->div2;
190 /* ??? This needs to make sure the throwing location is accurate. */
Blue Swirl31871142012-09-02 07:27:38 +0000191 if (den == 0) {
192 raise_exception(env, EXCP_DIV0);
193 }
pbrooke1f38082008-05-24 22:29:16 +0000194 quot = num / den;
195 rem = num % den;
196 flags = 0;
pbrooke1f38082008-05-24 22:29:16 +0000197 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;
206}
207
Andreas Färber2b3e3cf2012-03-14 01:38:22 +0100208void HELPER(divs)(CPUM68KState *env, uint32_t word)
pbrooke1f38082008-05-24 22:29:16 +0000209{
210 int32_t num;
211 int32_t den;
212 int32_t quot;
213 int32_t rem;
214 int32_t flags;
215
216 num = env->div1;
217 den = env->div2;
Blue Swirl31871142012-09-02 07:27:38 +0000218 if (den == 0) {
219 raise_exception(env, EXCP_DIV0);
220 }
pbrooke1f38082008-05-24 22:29:16 +0000221 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;
233}