xref: /qemu/target/m68k/op_helper.c (revision 27103424c40ce71053c07d8a54ef431365fa9b7f)
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"
20e5e84d22SRichard Henderson #include "helper.h"
210633879fSpbrook 
220633879fSpbrook #if defined(CONFIG_USER_ONLY)
230633879fSpbrook 
2497a8ea5aSAndreas Färber void m68k_cpu_do_interrupt(CPUState *cs)
250633879fSpbrook {
2627103424SAndreas Färber     cs->exception_index = -1;
273c688828SBlue Swirl }
283c688828SBlue Swirl 
2931871141SBlue Swirl void do_interrupt_m68k_hardirq(CPUM68KState *env)
303c688828SBlue Swirl {
310633879fSpbrook }
320633879fSpbrook 
330633879fSpbrook #else
340633879fSpbrook 
35a87295e8Spbrook extern int semihosting_enabled;
36a87295e8Spbrook 
37022c62cbSPaolo Bonzini #include "exec/softmmu_exec.h"
383e457172SBlue Swirl 
390633879fSpbrook #define MMUSUFFIX _mmu
400633879fSpbrook 
410633879fSpbrook #define SHIFT 0
42022c62cbSPaolo Bonzini #include "exec/softmmu_template.h"
430633879fSpbrook 
440633879fSpbrook #define SHIFT 1
45022c62cbSPaolo Bonzini #include "exec/softmmu_template.h"
460633879fSpbrook 
470633879fSpbrook #define SHIFT 2
48022c62cbSPaolo Bonzini #include "exec/softmmu_template.h"
490633879fSpbrook 
500633879fSpbrook #define SHIFT 3
51022c62cbSPaolo Bonzini #include "exec/softmmu_template.h"
520633879fSpbrook 
530633879fSpbrook /* Try to fill the TLB and return an exception if error. If retaddr is
540633879fSpbrook    NULL, it means that the function was called in C code (i.e. not
550633879fSpbrook    from generated code or from helper.c) */
5631871141SBlue Swirl void tlb_fill(CPUM68KState *env, target_ulong addr, int is_write, int mmu_idx,
5720503968SBlue Swirl               uintptr_t retaddr)
580633879fSpbrook {
597510454eSAndreas Färber     M68kCPU *cpu = m68k_env_get_cpu(env);
600633879fSpbrook     int ret;
610633879fSpbrook 
627510454eSAndreas Färber     ret = m68k_cpu_handle_mmu_fault(CPU(cpu), addr, is_write, mmu_idx);
63551bd27fSths     if (unlikely(ret)) {
640633879fSpbrook         if (retaddr) {
650633879fSpbrook             /* now we have a real cpu fault */
66a8a826a3SBlue Swirl             cpu_restore_state(env, retaddr);
670633879fSpbrook         }
681162c041SBlue Swirl         cpu_loop_exit(env);
690633879fSpbrook     }
700633879fSpbrook }
710633879fSpbrook 
7231871141SBlue Swirl static void do_rte(CPUM68KState *env)
730633879fSpbrook {
740633879fSpbrook     uint32_t sp;
750633879fSpbrook     uint32_t fmt;
760633879fSpbrook 
770633879fSpbrook     sp = env->aregs[7];
7831871141SBlue Swirl     fmt = cpu_ldl_kernel(env, sp);
7931871141SBlue Swirl     env->pc = cpu_ldl_kernel(env, sp + 4);
800633879fSpbrook     sp |= (fmt >> 28) & 3;
810633879fSpbrook     env->sr = fmt & 0xffff;
8220dcee94Spbrook     m68k_switch_sp(env);
830633879fSpbrook     env->aregs[7] = sp + 8;
840633879fSpbrook }
850633879fSpbrook 
8631871141SBlue Swirl static void do_interrupt_all(CPUM68KState *env, int is_hw)
870633879fSpbrook {
8827103424SAndreas Färber     CPUState *cs = CPU(m68k_env_get_cpu(env));
890633879fSpbrook     uint32_t sp;
900633879fSpbrook     uint32_t fmt;
910633879fSpbrook     uint32_t retaddr;
920633879fSpbrook     uint32_t vector;
930633879fSpbrook 
940633879fSpbrook     fmt = 0;
950633879fSpbrook     retaddr = env->pc;
960633879fSpbrook 
970633879fSpbrook     if (!is_hw) {
9827103424SAndreas Färber         switch (cs->exception_index) {
990633879fSpbrook         case EXCP_RTE:
1000633879fSpbrook             /* Return from an exception.  */
10131871141SBlue Swirl             do_rte(env);
1020633879fSpbrook             return;
103a87295e8Spbrook         case EXCP_HALT_INSN:
104a87295e8Spbrook             if (semihosting_enabled
105a87295e8Spbrook                     && (env->sr & SR_S) != 0
106a87295e8Spbrook                     && (env->pc & 3) == 0
10731871141SBlue Swirl                     && cpu_lduw_code(env, env->pc - 4) == 0x4e71
10831871141SBlue Swirl                     && cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
109a87295e8Spbrook                 env->pc += 4;
110a87295e8Spbrook                 do_m68k_semihosting(env, env->dregs[0]);
111a87295e8Spbrook                 return;
112a87295e8Spbrook             }
113259186a7SAndreas Färber             cs->halted = 1;
11427103424SAndreas Färber             cs->exception_index = EXCP_HLT;
1151162c041SBlue Swirl             cpu_loop_exit(env);
116a87295e8Spbrook             return;
1170633879fSpbrook         }
11827103424SAndreas Färber         if (cs->exception_index >= EXCP_TRAP0
11927103424SAndreas Färber             && cs->exception_index <= EXCP_TRAP15) {
1200633879fSpbrook             /* Move the PC after the trap instruction.  */
1210633879fSpbrook             retaddr += 2;
1220633879fSpbrook         }
1230633879fSpbrook     }
1240633879fSpbrook 
12527103424SAndreas Färber     vector = cs->exception_index << 2;
1260633879fSpbrook 
1270cf5c677Spbrook     sp = env->aregs[7];
1280cf5c677Spbrook 
1290633879fSpbrook     fmt |= 0x40000000;
1300633879fSpbrook     fmt |= (sp & 3) << 28;
1310633879fSpbrook     fmt |= vector << 16;
1320633879fSpbrook     fmt |= env->sr;
1330633879fSpbrook 
13420dcee94Spbrook     env->sr |= SR_S;
13520dcee94Spbrook     if (is_hw) {
13620dcee94Spbrook         env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT);
13720dcee94Spbrook         env->sr &= ~SR_M;
13820dcee94Spbrook     }
13920dcee94Spbrook     m68k_switch_sp(env);
14020dcee94Spbrook 
1410633879fSpbrook     /* ??? This could cause MMU faults.  */
1420633879fSpbrook     sp &= ~3;
1430633879fSpbrook     sp -= 4;
14431871141SBlue Swirl     cpu_stl_kernel(env, sp, retaddr);
1450633879fSpbrook     sp -= 4;
14631871141SBlue Swirl     cpu_stl_kernel(env, sp, fmt);
1470633879fSpbrook     env->aregs[7] = sp;
1480633879fSpbrook     /* Jump to vector.  */
14931871141SBlue Swirl     env->pc = cpu_ldl_kernel(env, env->vbr + vector);
1500633879fSpbrook }
1510633879fSpbrook 
15297a8ea5aSAndreas Färber void m68k_cpu_do_interrupt(CPUState *cs)
1533c688828SBlue Swirl {
15497a8ea5aSAndreas Färber     M68kCPU *cpu = M68K_CPU(cs);
15597a8ea5aSAndreas Färber     CPUM68KState *env = &cpu->env;
15697a8ea5aSAndreas Färber 
15731871141SBlue Swirl     do_interrupt_all(env, 0);
1583c688828SBlue Swirl }
1593c688828SBlue Swirl 
16031871141SBlue Swirl void do_interrupt_m68k_hardirq(CPUM68KState *env)
1613c688828SBlue Swirl {
16231871141SBlue Swirl     do_interrupt_all(env, 1);
1633c688828SBlue Swirl }
1640633879fSpbrook #endif
165e1f3808eSpbrook 
16631871141SBlue Swirl static void raise_exception(CPUM68KState *env, int tt)
167e1f3808eSpbrook {
16827103424SAndreas Färber     CPUState *cs = CPU(m68k_env_get_cpu(env));
16927103424SAndreas Färber 
17027103424SAndreas Färber     cs->exception_index = tt;
1711162c041SBlue Swirl     cpu_loop_exit(env);
172e1f3808eSpbrook }
173e1f3808eSpbrook 
17431871141SBlue Swirl void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)
175e1f3808eSpbrook {
17631871141SBlue Swirl     raise_exception(env, tt);
177e1f3808eSpbrook }
178e1f3808eSpbrook 
1792b3e3cfeSAndreas Färber void HELPER(divu)(CPUM68KState *env, uint32_t word)
180e1f3808eSpbrook {
181e1f3808eSpbrook     uint32_t num;
182e1f3808eSpbrook     uint32_t den;
183e1f3808eSpbrook     uint32_t quot;
184e1f3808eSpbrook     uint32_t rem;
185e1f3808eSpbrook     uint32_t flags;
186e1f3808eSpbrook 
187e1f3808eSpbrook     num = env->div1;
188e1f3808eSpbrook     den = env->div2;
189e1f3808eSpbrook     /* ??? This needs to make sure the throwing location is accurate.  */
19031871141SBlue Swirl     if (den == 0) {
19131871141SBlue Swirl         raise_exception(env, EXCP_DIV0);
19231871141SBlue Swirl     }
193e1f3808eSpbrook     quot = num / den;
194e1f3808eSpbrook     rem = num % den;
195e1f3808eSpbrook     flags = 0;
196e1f3808eSpbrook     if (word && quot > 0xffff)
197e1f3808eSpbrook         flags |= CCF_V;
198e1f3808eSpbrook     if (quot == 0)
199e1f3808eSpbrook         flags |= CCF_Z;
200e1f3808eSpbrook     else if ((int32_t)quot < 0)
201e1f3808eSpbrook         flags |= CCF_N;
202e1f3808eSpbrook     env->div1 = quot;
203e1f3808eSpbrook     env->div2 = rem;
204e1f3808eSpbrook     env->cc_dest = flags;
205e1f3808eSpbrook }
206e1f3808eSpbrook 
2072b3e3cfeSAndreas Färber void HELPER(divs)(CPUM68KState *env, uint32_t word)
208e1f3808eSpbrook {
209e1f3808eSpbrook     int32_t num;
210e1f3808eSpbrook     int32_t den;
211e1f3808eSpbrook     int32_t quot;
212e1f3808eSpbrook     int32_t rem;
213e1f3808eSpbrook     int32_t flags;
214e1f3808eSpbrook 
215e1f3808eSpbrook     num = env->div1;
216e1f3808eSpbrook     den = env->div2;
21731871141SBlue Swirl     if (den == 0) {
21831871141SBlue Swirl         raise_exception(env, EXCP_DIV0);
21931871141SBlue Swirl     }
220e1f3808eSpbrook     quot = num / den;
221e1f3808eSpbrook     rem = num % den;
222e1f3808eSpbrook     flags = 0;
223e1f3808eSpbrook     if (word && quot != (int16_t)quot)
224e1f3808eSpbrook         flags |= CCF_V;
225e1f3808eSpbrook     if (quot == 0)
226e1f3808eSpbrook         flags |= CCF_Z;
227e1f3808eSpbrook     else if (quot < 0)
228e1f3808eSpbrook         flags |= CCF_N;
229e1f3808eSpbrook     env->div1 = quot;
230e1f3808eSpbrook     env->div2 = rem;
231e1f3808eSpbrook     env->cc_dest = flags;
232e1f3808eSpbrook }
233