blob: bbbfd7f130c14d1e093053cf92192fd29c6a285d [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 */
Blue Swirl3e457172011-07-13 12:44:15 +000019#include "cpu.h"
Richard Hendersone5e84d22013-09-14 14:41:34 -070020#include "helper.h"
pbrook06338792007-05-23 19:58:11 +000021
22#if defined(CONFIG_USER_ONLY)
23
Andreas Färber97a8ea52013-02-02 10:57:51 +010024void m68k_cpu_do_interrupt(CPUState *cs)
pbrook06338792007-05-23 19:58:11 +000025{
Andreas Färber97a8ea52013-02-02 10:57:51 +010026 M68kCPU *cpu = M68K_CPU(cs);
27 CPUM68KState *env = &cpu->env;
28
Blue Swirl31871142012-09-02 07:27:38 +000029 env->exception_index = -1;
Blue Swirl3c688822011-05-21 07:55:24 +000030}
31
Blue Swirl31871142012-09-02 07:27:38 +000032void 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
pbrooka87295e2007-05-26 15:09:38 +000038extern int semihosting_enabled;
39
Paolo Bonzini022c62c2012-12-17 18:19:49 +010040#include "exec/softmmu_exec.h"
Blue Swirl3e457172011-07-13 12:44:15 +000041
pbrook06338792007-05-23 19:58:11 +000042#define MMUSUFFIX _mmu
pbrook06338792007-05-23 19:58:11 +000043
44#define SHIFT 0
Paolo Bonzini022c62c2012-12-17 18:19:49 +010045#include "exec/softmmu_template.h"
pbrook06338792007-05-23 19:58:11 +000046
47#define SHIFT 1
Paolo Bonzini022c62c2012-12-17 18:19:49 +010048#include "exec/softmmu_template.h"
pbrook06338792007-05-23 19:58:11 +000049
50#define SHIFT 2
Paolo Bonzini022c62c2012-12-17 18:19:49 +010051#include "exec/softmmu_template.h"
pbrook06338792007-05-23 19:58:11 +000052
53#define SHIFT 3
Paolo Bonzini022c62c2012-12-17 18:19:49 +010054#include "exec/softmmu_template.h"
pbrook06338792007-05-23 19:58:11 +000055
56/* Try to fill the TLB and return an exception if error. If retaddr is
57 NULL, it means that the function was called in C code (i.e. not
58 from generated code or from helper.c) */
Blue Swirl31871142012-09-02 07:27:38 +000059void tlb_fill(CPUM68KState *env, target_ulong addr, int is_write, int mmu_idx,
Blue Swirl20503962012-04-09 14:20:20 +000060 uintptr_t retaddr)
pbrook06338792007-05-23 19:58:11 +000061{
pbrook06338792007-05-23 19:58:11 +000062 int ret;
63
Blue Swirl97b348e2011-08-01 16:12:17 +000064 ret = cpu_m68k_handle_mmu_fault(env, addr, is_write, mmu_idx);
ths551bd272008-07-03 17:57:36 +000065 if (unlikely(ret)) {
pbrook06338792007-05-23 19:58:11 +000066 if (retaddr) {
67 /* now we have a real cpu fault */
Blue Swirla8a826a2012-12-04 20:16:07 +000068 cpu_restore_state(env, retaddr);
pbrook06338792007-05-23 19:58:11 +000069 }
Blue Swirl1162c042011-05-14 12:52:35 +000070 cpu_loop_exit(env);
pbrook06338792007-05-23 19:58:11 +000071 }
pbrook06338792007-05-23 19:58:11 +000072}
73
Blue Swirl31871142012-09-02 07:27:38 +000074static void do_rte(CPUM68KState *env)
pbrook06338792007-05-23 19:58:11 +000075{
76 uint32_t sp;
77 uint32_t fmt;
78
79 sp = env->aregs[7];
Blue Swirl31871142012-09-02 07:27:38 +000080 fmt = cpu_ldl_kernel(env, sp);
81 env->pc = cpu_ldl_kernel(env, sp + 4);
pbrook06338792007-05-23 19:58:11 +000082 sp |= (fmt >> 28) & 3;
83 env->sr = fmt & 0xffff;
pbrook20dcee92007-06-03 11:13:39 +000084 m68k_switch_sp(env);
pbrook06338792007-05-23 19:58:11 +000085 env->aregs[7] = sp + 8;
86}
87
Blue Swirl31871142012-09-02 07:27:38 +000088static void do_interrupt_all(CPUM68KState *env, int is_hw)
pbrook06338792007-05-23 19:58:11 +000089{
Andreas Färber259186a2013-01-17 18:51:17 +010090 CPUState *cs;
pbrook06338792007-05-23 19:58:11 +000091 uint32_t sp;
92 uint32_t fmt;
93 uint32_t retaddr;
94 uint32_t vector;
95
96 fmt = 0;
97 retaddr = env->pc;
98
99 if (!is_hw) {
100 switch (env->exception_index) {
101 case EXCP_RTE:
102 /* Return from an exception. */
Blue Swirl31871142012-09-02 07:27:38 +0000103 do_rte(env);
pbrook06338792007-05-23 19:58:11 +0000104 return;
pbrooka87295e2007-05-26 15:09:38 +0000105 case EXCP_HALT_INSN:
106 if (semihosting_enabled
107 && (env->sr & SR_S) != 0
108 && (env->pc & 3) == 0
Blue Swirl31871142012-09-02 07:27:38 +0000109 && cpu_lduw_code(env, env->pc - 4) == 0x4e71
110 && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
pbrooka87295e2007-05-26 15:09:38 +0000111 env->pc += 4;
112 do_m68k_semihosting(env, env->dregs[0]);
113 return;
114 }
Andreas Färber259186a2013-01-17 18:51:17 +0100115 cs = CPU(m68k_env_get_cpu(env));
116 cs->halted = 1;
pbrooka87295e2007-05-26 15:09:38 +0000117 env->exception_index = EXCP_HLT;
Blue Swirl1162c042011-05-14 12:52:35 +0000118 cpu_loop_exit(env);
pbrooka87295e2007-05-26 15:09:38 +0000119 return;
pbrook06338792007-05-23 19:58:11 +0000120 }
121 if (env->exception_index >= EXCP_TRAP0
122 && env->exception_index <= EXCP_TRAP15) {
123 /* Move the PC after the trap instruction. */
124 retaddr += 2;
125 }
126 }
127
pbrook06338792007-05-23 19:58:11 +0000128 vector = env->exception_index << 2;
129
pbrook0cf5c672007-06-09 20:48:46 +0000130 sp = env->aregs[7];
131
pbrook06338792007-05-23 19:58:11 +0000132 fmt |= 0x40000000;
133 fmt |= (sp & 3) << 28;
134 fmt |= vector << 16;
135 fmt |= env->sr;
136
pbrook20dcee92007-06-03 11:13:39 +0000137 env->sr |= SR_S;
138 if (is_hw) {
139 env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
140 env->sr &= ~SR_M;
141 }
142 m68k_switch_sp(env);
143
pbrook06338792007-05-23 19:58:11 +0000144 /* ??? This could cause MMU faults. */
145 sp &= ~3;
146 sp -= 4;
Blue Swirl31871142012-09-02 07:27:38 +0000147 cpu_stl_kernel(env, sp, retaddr);
pbrook06338792007-05-23 19:58:11 +0000148 sp -= 4;
Blue Swirl31871142012-09-02 07:27:38 +0000149 cpu_stl_kernel(env, sp, fmt);
pbrook06338792007-05-23 19:58:11 +0000150 env->aregs[7] = sp;
pbrook06338792007-05-23 19:58:11 +0000151 /* Jump to vector. */
Blue Swirl31871142012-09-02 07:27:38 +0000152 env->pc = cpu_ldl_kernel(env, env->vbr + vector);
pbrook06338792007-05-23 19:58:11 +0000153}
154
Andreas Färber97a8ea52013-02-02 10:57:51 +0100155void m68k_cpu_do_interrupt(CPUState *cs)
Blue Swirl3c688822011-05-21 07:55:24 +0000156{
Andreas Färber97a8ea52013-02-02 10:57:51 +0100157 M68kCPU *cpu = M68K_CPU(cs);
158 CPUM68KState *env = &cpu->env;
159
Blue Swirl31871142012-09-02 07:27:38 +0000160 do_interrupt_all(env, 0);
Blue Swirl3c688822011-05-21 07:55:24 +0000161}
162
Blue Swirl31871142012-09-02 07:27:38 +0000163void do_interrupt_m68k_hardirq(CPUM68KState *env)
Blue Swirl3c688822011-05-21 07:55:24 +0000164{
Blue Swirl31871142012-09-02 07:27:38 +0000165 do_interrupt_all(env, 1);
Blue Swirl3c688822011-05-21 07:55:24 +0000166}
pbrook06338792007-05-23 19:58:11 +0000167#endif
pbrooke1f38082008-05-24 22:29:16 +0000168
Blue Swirl31871142012-09-02 07:27:38 +0000169static void raise_exception(CPUM68KState *env, int tt)
pbrooke1f38082008-05-24 22:29:16 +0000170{
171 env->exception_index = tt;
Blue Swirl1162c042011-05-14 12:52:35 +0000172 cpu_loop_exit(env);
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}