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" 22cfe67cefSLeon Alrae #include "exec/semihost.h" 230633879fSpbrook 240633879fSpbrook #if defined(CONFIG_USER_ONLY) 250633879fSpbrook 2697a8ea5aSAndreas Färber void m68k_cpu_do_interrupt(CPUState *cs) 270633879fSpbrook { 2827103424SAndreas Färber cs->exception_index = -1; 293c688828SBlue Swirl } 303c688828SBlue Swirl 31ab409bb3SRichard Henderson static inline void do_interrupt_m68k_hardirq(CPUM68KState *env) 323c688828SBlue Swirl { 330633879fSpbrook } 340633879fSpbrook 350633879fSpbrook #else 360633879fSpbrook 370633879fSpbrook /* Try to fill the TLB and return an exception if error. If retaddr is 380633879fSpbrook NULL, it means that the function was called in C code (i.e. not 390633879fSpbrook from generated code or from helper.c) */ 40d5a11fefSAndreas Färber void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx, 4120503968SBlue Swirl uintptr_t retaddr) 420633879fSpbrook { 430633879fSpbrook int ret; 440633879fSpbrook 45d5a11fefSAndreas Färber ret = m68k_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx); 46551bd27fSths if (unlikely(ret)) { 470633879fSpbrook if (retaddr) { 480633879fSpbrook /* now we have a real cpu fault */ 493f38f309SAndreas Färber cpu_restore_state(cs, retaddr); 500633879fSpbrook } 515638d180SAndreas Färber cpu_loop_exit(cs); 520633879fSpbrook } 530633879fSpbrook } 540633879fSpbrook 5531871141SBlue Swirl static void do_rte(CPUM68KState *env) 560633879fSpbrook { 570633879fSpbrook uint32_t sp; 580633879fSpbrook uint32_t fmt; 590633879fSpbrook 600633879fSpbrook sp = env->aregs[7]; 6131871141SBlue Swirl fmt = cpu_ldl_kernel(env, sp); 6231871141SBlue Swirl env->pc = cpu_ldl_kernel(env, sp + 4); 630633879fSpbrook sp |= (fmt >> 28) & 3; 640633879fSpbrook env->sr = fmt & 0xffff; 6520dcee94Spbrook m68k_switch_sp(env); 660633879fSpbrook env->aregs[7] = sp + 8; 670633879fSpbrook } 680633879fSpbrook 6931871141SBlue Swirl static void do_interrupt_all(CPUM68KState *env, int is_hw) 700633879fSpbrook { 7127103424SAndreas Färber CPUState *cs = CPU(m68k_env_get_cpu(env)); 720633879fSpbrook uint32_t sp; 730633879fSpbrook uint32_t fmt; 740633879fSpbrook uint32_t retaddr; 750633879fSpbrook uint32_t vector; 760633879fSpbrook 770633879fSpbrook fmt = 0; 780633879fSpbrook retaddr = env->pc; 790633879fSpbrook 800633879fSpbrook if (!is_hw) { 8127103424SAndreas Färber switch (cs->exception_index) { 820633879fSpbrook case EXCP_RTE: 830633879fSpbrook /* Return from an exception. */ 8431871141SBlue Swirl do_rte(env); 850633879fSpbrook return; 86a87295e8Spbrook case EXCP_HALT_INSN: 87cfe67cefSLeon Alrae if (semihosting_enabled() 88a87295e8Spbrook && (env->sr & SR_S) != 0 89a87295e8Spbrook && (env->pc & 3) == 0 9031871141SBlue Swirl && cpu_lduw_code(env, env->pc - 4) == 0x4e71 9131871141SBlue Swirl && cpu_ldl_code(env, env->pc) == 0x4e7bf000) { 92a87295e8Spbrook env->pc += 4; 93a87295e8Spbrook do_m68k_semihosting(env, env->dregs[0]); 94a87295e8Spbrook return; 95a87295e8Spbrook } 96259186a7SAndreas Färber cs->halted = 1; 9727103424SAndreas Färber cs->exception_index = EXCP_HLT; 985638d180SAndreas Färber cpu_loop_exit(cs); 99a87295e8Spbrook return; 1000633879fSpbrook } 10127103424SAndreas Färber if (cs->exception_index >= EXCP_TRAP0 10227103424SAndreas Färber && cs->exception_index <= EXCP_TRAP15) { 1030633879fSpbrook /* Move the PC after the trap instruction. */ 1040633879fSpbrook retaddr += 2; 1050633879fSpbrook } 1060633879fSpbrook } 1070633879fSpbrook 10827103424SAndreas Färber vector = cs->exception_index << 2; 1090633879fSpbrook 1100cf5c677Spbrook sp = env->aregs[7]; 1110cf5c677Spbrook 1120633879fSpbrook fmt |= 0x40000000; 1130633879fSpbrook fmt |= (sp & 3) << 28; 1140633879fSpbrook fmt |= vector << 16; 1150633879fSpbrook fmt |= env->sr; 1160633879fSpbrook 11720dcee94Spbrook env->sr |= SR_S; 11820dcee94Spbrook if (is_hw) { 11920dcee94Spbrook env->sr = (env->sr & ~SR_I) | (env->pending_level << SR_I_SHIFT); 12020dcee94Spbrook env->sr &= ~SR_M; 12120dcee94Spbrook } 12220dcee94Spbrook m68k_switch_sp(env); 12320dcee94Spbrook 1240633879fSpbrook /* ??? This could cause MMU faults. */ 1250633879fSpbrook sp &= ~3; 1260633879fSpbrook sp -= 4; 12731871141SBlue Swirl cpu_stl_kernel(env, sp, retaddr); 1280633879fSpbrook sp -= 4; 12931871141SBlue Swirl cpu_stl_kernel(env, sp, fmt); 1300633879fSpbrook env->aregs[7] = sp; 1310633879fSpbrook /* Jump to vector. */ 13231871141SBlue Swirl env->pc = cpu_ldl_kernel(env, env->vbr + vector); 1330633879fSpbrook } 1340633879fSpbrook 13597a8ea5aSAndreas Färber void m68k_cpu_do_interrupt(CPUState *cs) 1363c688828SBlue Swirl { 13797a8ea5aSAndreas Färber M68kCPU *cpu = M68K_CPU(cs); 13897a8ea5aSAndreas Färber CPUM68KState *env = &cpu->env; 13997a8ea5aSAndreas Färber 14031871141SBlue Swirl do_interrupt_all(env, 0); 1413c688828SBlue Swirl } 1423c688828SBlue Swirl 143ab409bb3SRichard Henderson static inline void do_interrupt_m68k_hardirq(CPUM68KState *env) 1443c688828SBlue Swirl { 14531871141SBlue Swirl do_interrupt_all(env, 1); 1463c688828SBlue Swirl } 1470633879fSpbrook #endif 148e1f3808eSpbrook 149ab409bb3SRichard Henderson bool m68k_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 150ab409bb3SRichard Henderson { 151ab409bb3SRichard Henderson M68kCPU *cpu = M68K_CPU(cs); 152ab409bb3SRichard Henderson CPUM68KState *env = &cpu->env; 153ab409bb3SRichard Henderson 154ab409bb3SRichard Henderson if (interrupt_request & CPU_INTERRUPT_HARD 155ab409bb3SRichard Henderson && ((env->sr & SR_I) >> SR_I_SHIFT) < env->pending_level) { 156ab409bb3SRichard Henderson /* Real hardware gets the interrupt vector via an IACK cycle 157ab409bb3SRichard Henderson at this point. Current emulated hardware doesn't rely on 158ab409bb3SRichard Henderson this, so we provide/save the vector when the interrupt is 159ab409bb3SRichard Henderson first signalled. */ 160ab409bb3SRichard Henderson cs->exception_index = env->pending_vector; 161ab409bb3SRichard Henderson do_interrupt_m68k_hardirq(env); 162ab409bb3SRichard Henderson return true; 163ab409bb3SRichard Henderson } 164ab409bb3SRichard Henderson return false; 165ab409bb3SRichard Henderson } 166ab409bb3SRichard Henderson 16731871141SBlue Swirl static void raise_exception(CPUM68KState *env, int tt) 168e1f3808eSpbrook { 16927103424SAndreas Färber CPUState *cs = CPU(m68k_env_get_cpu(env)); 17027103424SAndreas Färber 17127103424SAndreas Färber cs->exception_index = tt; 1725638d180SAndreas Färber cpu_loop_exit(cs); 173e1f3808eSpbrook } 174e1f3808eSpbrook 17531871141SBlue Swirl void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) 176e1f3808eSpbrook { 17731871141SBlue Swirl raise_exception(env, tt); 178e1f3808eSpbrook } 179e1f3808eSpbrook 1802b3e3cfeSAndreas Färber void HELPER(divu)(CPUM68KState *env, uint32_t word) 181e1f3808eSpbrook { 182e1f3808eSpbrook uint32_t num; 183e1f3808eSpbrook uint32_t den; 184e1f3808eSpbrook uint32_t quot; 185e1f3808eSpbrook uint32_t rem; 186e1f3808eSpbrook uint32_t flags; 187e1f3808eSpbrook 188e1f3808eSpbrook num = env->div1; 189e1f3808eSpbrook den = env->div2; 190e1f3808eSpbrook /* ??? This needs to make sure the throwing location is accurate. */ 19131871141SBlue Swirl if (den == 0) { 19231871141SBlue Swirl raise_exception(env, EXCP_DIV0); 19331871141SBlue Swirl } 194e1f3808eSpbrook quot = num / den; 195e1f3808eSpbrook rem = num % den; 196e1f3808eSpbrook flags = 0; 197e1f3808eSpbrook if (word && quot > 0xffff) 198e1f3808eSpbrook flags |= CCF_V; 199e1f3808eSpbrook if (quot == 0) 200e1f3808eSpbrook flags |= CCF_Z; 201e1f3808eSpbrook else if ((int32_t)quot < 0) 202e1f3808eSpbrook flags |= CCF_N; 203e1f3808eSpbrook env->div1 = quot; 204e1f3808eSpbrook env->div2 = rem; 205e1f3808eSpbrook env->cc_dest = flags; 206e1f3808eSpbrook } 207e1f3808eSpbrook 2082b3e3cfeSAndreas Färber void HELPER(divs)(CPUM68KState *env, uint32_t word) 209e1f3808eSpbrook { 210e1f3808eSpbrook int32_t num; 211e1f3808eSpbrook int32_t den; 212e1f3808eSpbrook int32_t quot; 213e1f3808eSpbrook int32_t rem; 214e1f3808eSpbrook int32_t flags; 215e1f3808eSpbrook 216e1f3808eSpbrook num = env->div1; 217e1f3808eSpbrook den = env->div2; 21831871141SBlue Swirl if (den == 0) { 21931871141SBlue Swirl raise_exception(env, EXCP_DIV0); 22031871141SBlue Swirl } 221e1f3808eSpbrook quot = num / den; 222e1f3808eSpbrook rem = num % den; 223e1f3808eSpbrook flags = 0; 224e1f3808eSpbrook if (word && quot != (int16_t)quot) 225e1f3808eSpbrook flags |= CCF_V; 226e1f3808eSpbrook if (quot == 0) 227e1f3808eSpbrook flags |= CCF_Z; 228e1f3808eSpbrook else if (quot < 0) 229e1f3808eSpbrook flags |= CCF_N; 230e1f3808eSpbrook env->div1 = quot; 231e1f3808eSpbrook env->div2 = rem; 232e1f3808eSpbrook env->cc_dest = flags; 233e1f3808eSpbrook } 234