xref: /qemu/target/m68k/op_helper.c (revision ab409bb3fea7e46bbce557ebda14e8eb420f5377)
10633879fSpbrook /*
20633879fSpbrook  *  M68K helper routines
30633879fSpbrook  *
40633879fSpbrook  *  Copyright (c) 2007 CodeSourcery
50633879fSpbrook  *
60633879fSpbrook  * This library is free software; you can redistribute it and/or
70633879fSpbrook  * modify it under the terms of the GNU Lesser General Public
80633879fSpbrook  * License as published by the Free Software Foundation; either
90633879fSpbrook  * version 2 of the License, or (at your option) any later version.
100633879fSpbrook  *
110633879fSpbrook  * This library is distributed in the hope that it will be useful,
120633879fSpbrook  * but WITHOUT ANY WARRANTY; without even the implied warranty of
130633879fSpbrook  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
140633879fSpbrook  * Lesser General Public License for more details.
150633879fSpbrook  *
160633879fSpbrook  * You should have received a copy of the GNU Lesser General Public
178167ee88SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
180633879fSpbrook  */
193e457172SBlue Swirl #include "cpu.h"
202ef6175aSRichard Henderson #include "exec/helper-proto.h"
21f08b6170SPaolo Bonzini #include "exec/cpu_ldst.h"
220633879fSpbrook 
230633879fSpbrook #if defined(CONFIG_USER_ONLY)
240633879fSpbrook 
2597a8ea5aSAndreas Färber void m68k_cpu_do_interrupt(CPUState *cs)
260633879fSpbrook {
2727103424SAndreas Färber     cs->exception_index = -1;
283c688828SBlue Swirl }
293c688828SBlue Swirl 
30ab409bb3SRichard Henderson static inline void do_interrupt_m68k_hardirq(CPUM68KState *env)
313c688828SBlue Swirl {
320633879fSpbrook }
330633879fSpbrook 
340633879fSpbrook #else
350633879fSpbrook 
36a87295e8Spbrook extern int semihosting_enabled;
37a87295e8Spbrook 
380633879fSpbrook /* Try to fill the TLB and return an exception if error. If retaddr is
390633879fSpbrook    NULL, it means that the function was called in C code (i.e. not
400633879fSpbrook    from generated code or from helper.c) */
41d5a11fefSAndreas Färber void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
4220503968SBlue Swirl               uintptr_t retaddr)
430633879fSpbrook {
440633879fSpbrook     int ret;
450633879fSpbrook 
46d5a11fefSAndreas Färber     ret = m68k_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
47551bd27fSths     if (unlikely(ret)) {
480633879fSpbrook         if (retaddr) {
490633879fSpbrook             /* now we have a real cpu fault */
503f38f309SAndreas Färber             cpu_restore_state(cs, retaddr);
510633879fSpbrook         }
525638d180SAndreas Färber         cpu_loop_exit(cs);
530633879fSpbrook     }
540633879fSpbrook }
550633879fSpbrook 
5631871141SBlue Swirl static void do_rte(CPUM68KState *env)
570633879fSpbrook {
580633879fSpbrook     uint32_t sp;
590633879fSpbrook     uint32_t fmt;
600633879fSpbrook 
610633879fSpbrook     sp = env->aregs[7];
6231871141SBlue Swirl     fmt = cpu_ldl_kernel(env, sp);
6331871141SBlue Swirl     env->pc = cpu_ldl_kernel(env, sp + 4);
640633879fSpbrook     sp |= (fmt >> 28) & 3;
650633879fSpbrook     env->sr = fmt & 0xffff;
6620dcee94Spbrook     m68k_switch_sp(env);
670633879fSpbrook     env->aregs[7] = sp + 8;
680633879fSpbrook }
690633879fSpbrook 
7031871141SBlue Swirl static void do_interrupt_all(CPUM68KState *env, int is_hw)
710633879fSpbrook {
7227103424SAndreas Färber     CPUState *cs = CPU(m68k_env_get_cpu(env));
730633879fSpbrook     uint32_t sp;
740633879fSpbrook     uint32_t fmt;
750633879fSpbrook     uint32_t retaddr;
760633879fSpbrook     uint32_t vector;
770633879fSpbrook 
780633879fSpbrook     fmt = 0;
790633879fSpbrook     retaddr = env->pc;
800633879fSpbrook 
810633879fSpbrook     if (!is_hw) {
8227103424SAndreas Färber         switch (cs->exception_index) {
830633879fSpbrook         case EXCP_RTE:
840633879fSpbrook             /* Return from an exception.  */
8531871141SBlue Swirl             do_rte(env);
860633879fSpbrook             return;
87a87295e8Spbrook         case EXCP_HALT_INSN:
88a87295e8Spbrook             if (semihosting_enabled
89a87295e8Spbrook                     && (env->sr & SR_S) != 0
90a87295e8Spbrook                     && (env->pc & 3) == 0
9131871141SBlue Swirl                     && cpu_lduw_code(env, env->pc - 4) == 0x4e71
9231871141SBlue Swirl                     && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
93a87295e8Spbrook                 env->pc += 4;
94a87295e8Spbrook                 do_m68k_semihosting(env, env->dregs[0]);
95a87295e8Spbrook                 return;
96a87295e8Spbrook             }
97259186a7SAndreas Färber             cs->halted = 1;
9827103424SAndreas Färber             cs->exception_index = EXCP_HLT;
995638d180SAndreas Färber             cpu_loop_exit(cs);
100a87295e8Spbrook             return;
1010633879fSpbrook         }
10227103424SAndreas Färber         if (cs->exception_index >= EXCP_TRAP0
10327103424SAndreas Färber             && cs->exception_index <= EXCP_TRAP15) {
1040633879fSpbrook             /* Move the PC after the trap instruction.  */
1050633879fSpbrook             retaddr += 2;
1060633879fSpbrook         }
1070633879fSpbrook     }
1080633879fSpbrook 
10927103424SAndreas Färber     vector = cs->exception_index << 2;
1100633879fSpbrook 
1110cf5c677Spbrook     sp = env->aregs[7];
1120cf5c677Spbrook 
1130633879fSpbrook     fmt |= 0x40000000;
1140633879fSpbrook     fmt |= (sp & 3) << 28;
1150633879fSpbrook     fmt |= vector << 16;
1160633879fSpbrook     fmt |= env->sr;
1170633879fSpbrook 
11820dcee94Spbrook     env->sr |= SR_S;
11920dcee94Spbrook     if (is_hw) {
12020dcee94Spbrook         env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
12120dcee94Spbrook         env->sr &= ~SR_M;
12220dcee94Spbrook     }
12320dcee94Spbrook     m68k_switch_sp(env);
12420dcee94Spbrook 
1250633879fSpbrook     /* ??? This could cause MMU faults.  */
1260633879fSpbrook     sp &= ~3;
1270633879fSpbrook     sp -= 4;
12831871141SBlue Swirl     cpu_stl_kernel(env, sp, retaddr);
1290633879fSpbrook     sp -= 4;
13031871141SBlue Swirl     cpu_stl_kernel(env, sp, fmt);
1310633879fSpbrook     env->aregs[7] = sp;
1320633879fSpbrook     /* Jump to vector.  */
13331871141SBlue Swirl     env->pc = cpu_ldl_kernel(env, env->vbr + vector);
1340633879fSpbrook }
1350633879fSpbrook 
13697a8ea5aSAndreas Färber void m68k_cpu_do_interrupt(CPUState *cs)
1373c688828SBlue Swirl {
13897a8ea5aSAndreas Färber     M68kCPU *cpu = M68K_CPU(cs);
13997a8ea5aSAndreas Färber     CPUM68KState *env = &cpu->env;
14097a8ea5aSAndreas Färber 
14131871141SBlue Swirl     do_interrupt_all(env, 0);
1423c688828SBlue Swirl }
1433c688828SBlue Swirl 
144ab409bb3SRichard Henderson static inline void do_interrupt_m68k_hardirq(CPUM68KState *env)
1453c688828SBlue Swirl {
14631871141SBlue Swirl     do_interrupt_all(env, 1);
1473c688828SBlue Swirl }
1480633879fSpbrook #endif
149e1f3808eSpbrook 
150ab409bb3SRichard Henderson bool m68k_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
151ab409bb3SRichard Henderson {
152ab409bb3SRichard Henderson     M68kCPU *cpu = M68K_CPU(cs);
153ab409bb3SRichard Henderson     CPUM68KState *env = &cpu->env;
154ab409bb3SRichard Henderson 
155ab409bb3SRichard Henderson     if (interrupt_request & CPU_INTERRUPT_HARD
156ab409bb3SRichard Henderson         && ((env->sr & SR_I) >> SR_I_SHIFT) < env->pending_level) {
157ab409bb3SRichard Henderson         /* Real hardware gets the interrupt vector via an IACK cycle
158ab409bb3SRichard Henderson            at this point.  Current emulated hardware doesn't rely on
159ab409bb3SRichard Henderson            this, so we provide/save the vector when the interrupt is
160ab409bb3SRichard Henderson            first signalled.  */
161ab409bb3SRichard Henderson         cs->exception_index = env->pending_vector;
162ab409bb3SRichard Henderson         do_interrupt_m68k_hardirq(env);
163ab409bb3SRichard Henderson         return true;
164ab409bb3SRichard Henderson     }
165ab409bb3SRichard Henderson     return false;
166ab409bb3SRichard Henderson }
167ab409bb3SRichard Henderson 
16831871141SBlue Swirl static void raise_exception(CPUM68KState *env, int tt)
169e1f3808eSpbrook {
17027103424SAndreas Färber     CPUState *cs = CPU(m68k_env_get_cpu(env));
17127103424SAndreas Färber 
17227103424SAndreas Färber     cs->exception_index = tt;
1735638d180SAndreas Färber     cpu_loop_exit(cs);
174e1f3808eSpbrook }
175e1f3808eSpbrook 
17631871141SBlue Swirl void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
177e1f3808eSpbrook {
17831871141SBlue Swirl     raise_exception(env, tt);
179e1f3808eSpbrook }
180e1f3808eSpbrook 
1812b3e3cfeSAndreas Färber void HELPER(divu)(CPUM68KState *env, uint32_t word)
182e1f3808eSpbrook {
183e1f3808eSpbrook     uint32_t num;
184e1f3808eSpbrook     uint32_t den;
185e1f3808eSpbrook     uint32_t quot;
186e1f3808eSpbrook     uint32_t rem;
187e1f3808eSpbrook     uint32_t flags;
188e1f3808eSpbrook 
189e1f3808eSpbrook     num = env->div1;
190e1f3808eSpbrook     den = env->div2;
191e1f3808eSpbrook     /* ??? This needs to make sure the throwing location is accurate.  */
19231871141SBlue Swirl     if (den == 0) {
19331871141SBlue Swirl         raise_exception(env, EXCP_DIV0);
19431871141SBlue Swirl     }
195e1f3808eSpbrook     quot = num / den;
196e1f3808eSpbrook     rem = num % den;
197e1f3808eSpbrook     flags = 0;
198e1f3808eSpbrook     if (word && quot > 0xffff)
199e1f3808eSpbrook         flags |= CCF_V;
200e1f3808eSpbrook     if (quot == 0)
201e1f3808eSpbrook         flags |= CCF_Z;
202e1f3808eSpbrook     else if ((int32_t)quot < 0)
203e1f3808eSpbrook         flags |= CCF_N;
204e1f3808eSpbrook     env->div1 = quot;
205e1f3808eSpbrook     env->div2 = rem;
206e1f3808eSpbrook     env->cc_dest = flags;
207e1f3808eSpbrook }
208e1f3808eSpbrook 
2092b3e3cfeSAndreas Färber void HELPER(divs)(CPUM68KState *env, uint32_t word)
210e1f3808eSpbrook {
211e1f3808eSpbrook     int32_t num;
212e1f3808eSpbrook     int32_t den;
213e1f3808eSpbrook     int32_t quot;
214e1f3808eSpbrook     int32_t rem;
215e1f3808eSpbrook     int32_t flags;
216e1f3808eSpbrook 
217e1f3808eSpbrook     num = env->div1;
218e1f3808eSpbrook     den = env->div2;
21931871141SBlue Swirl     if (den == 0) {
22031871141SBlue Swirl         raise_exception(env, EXCP_DIV0);
22131871141SBlue Swirl     }
222e1f3808eSpbrook     quot = num / den;
223e1f3808eSpbrook     rem = num % den;
224e1f3808eSpbrook     flags = 0;
225e1f3808eSpbrook     if (word && quot != (int16_t)quot)
226e1f3808eSpbrook         flags |= CCF_V;
227e1f3808eSpbrook     if (quot == 0)
228e1f3808eSpbrook         flags |= CCF_Z;
229e1f3808eSpbrook     else if (quot < 0)
230e1f3808eSpbrook         flags |= CCF_N;
231e1f3808eSpbrook     env->div1 = quot;
232e1f3808eSpbrook     env->div2 = rem;
233e1f3808eSpbrook     env->cc_dest = flags;
234e1f3808eSpbrook }
235