pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * M68K helper routines |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 4 | * 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 Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 18 | */ |
Blue Swirl | 3e45717 | 2011-07-13 12:44:15 +0000 | [diff] [blame] | 19 | #include "cpu.h" |
Richard Henderson | e5e84d2 | 2013-09-14 14:41:34 -0700 | [diff] [blame] | 20 | #include "helper.h" |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 21 | |
| 22 | #if defined(CONFIG_USER_ONLY) |
| 23 | |
Andreas Färber | 97a8ea5 | 2013-02-02 10:57:51 +0100 | [diff] [blame] | 24 | void m68k_cpu_do_interrupt(CPUState *cs) |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 25 | { |
Andreas Färber | 97a8ea5 | 2013-02-02 10:57:51 +0100 | [diff] [blame] | 26 | M68kCPU *cpu = M68K_CPU(cs); |
| 27 | CPUM68KState *env = &cpu->env; |
| 28 | |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 29 | env->exception_index = -1; |
Blue Swirl | 3c68882 | 2011-05-21 07:55:24 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 32 | void do_interrupt_m68k_hardirq(CPUM68KState *env) |
Blue Swirl | 3c68882 | 2011-05-21 07:55:24 +0000 | [diff] [blame] | 33 | { |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | #else |
| 37 | |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 38 | extern int semihosting_enabled; |
| 39 | |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 40 | #include "exec/softmmu_exec.h" |
Blue Swirl | 3e45717 | 2011-07-13 12:44:15 +0000 | [diff] [blame] | 41 | |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 42 | #define MMUSUFFIX _mmu |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 43 | |
| 44 | #define SHIFT 0 |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 45 | #include "exec/softmmu_template.h" |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 46 | |
| 47 | #define SHIFT 1 |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 48 | #include "exec/softmmu_template.h" |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 49 | |
| 50 | #define SHIFT 2 |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 51 | #include "exec/softmmu_template.h" |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 52 | |
| 53 | #define SHIFT 3 |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 54 | #include "exec/softmmu_template.h" |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 55 | |
| 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 Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 59 | void tlb_fill(CPUM68KState *env, target_ulong addr, int is_write, int mmu_idx, |
Blue Swirl | 2050396 | 2012-04-09 14:20:20 +0000 | [diff] [blame] | 60 | uintptr_t retaddr) |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 61 | { |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 62 | int ret; |
| 63 | |
Blue Swirl | 97b348e | 2011-08-01 16:12:17 +0000 | [diff] [blame] | 64 | ret = cpu_m68k_handle_mmu_fault(env, addr, is_write, mmu_idx); |
ths | 551bd27 | 2008-07-03 17:57:36 +0000 | [diff] [blame] | 65 | if (unlikely(ret)) { |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 66 | if (retaddr) { |
| 67 | /* now we have a real cpu fault */ |
Blue Swirl | a8a826a | 2012-12-04 20:16:07 +0000 | [diff] [blame] | 68 | cpu_restore_state(env, retaddr); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 69 | } |
Blue Swirl | 1162c04 | 2011-05-14 12:52:35 +0000 | [diff] [blame] | 70 | cpu_loop_exit(env); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 71 | } |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 74 | static void do_rte(CPUM68KState *env) |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 75 | { |
| 76 | uint32_t sp; |
| 77 | uint32_t fmt; |
| 78 | |
| 79 | sp = env->aregs[7]; |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 80 | fmt = cpu_ldl_kernel(env, sp); |
| 81 | env->pc = cpu_ldl_kernel(env, sp + 4); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 82 | sp |= (fmt >> 28) & 3; |
| 83 | env->sr = fmt & 0xffff; |
pbrook | 20dcee9 | 2007-06-03 11:13:39 +0000 | [diff] [blame] | 84 | m68k_switch_sp(env); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 85 | env->aregs[7] = sp + 8; |
| 86 | } |
| 87 | |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 88 | static void do_interrupt_all(CPUM68KState *env, int is_hw) |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 89 | { |
Andreas Färber | 259186a | 2013-01-17 18:51:17 +0100 | [diff] [blame] | 90 | CPUState *cs; |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 91 | 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 Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 103 | do_rte(env); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 104 | return; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 105 | case EXCP_HALT_INSN: |
| 106 | if (semihosting_enabled |
| 107 | && (env->sr & SR_S) != 0 |
| 108 | && (env->pc & 3) == 0 |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 109 | && cpu_lduw_code(env, env->pc - 4) == 0x4e71 |
| 110 | && cpu_ldl_code(env, env->pc) == 0x4e7bf000) { |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 111 | env->pc += 4; |
| 112 | do_m68k_semihosting(env, env->dregs[0]); |
| 113 | return; |
| 114 | } |
Andreas Färber | 259186a | 2013-01-17 18:51:17 +0100 | [diff] [blame] | 115 | cs = CPU(m68k_env_get_cpu(env)); |
| 116 | cs->halted = 1; |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 117 | env->exception_index = EXCP_HLT; |
Blue Swirl | 1162c04 | 2011-05-14 12:52:35 +0000 | [diff] [blame] | 118 | cpu_loop_exit(env); |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 119 | return; |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 120 | } |
| 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 | |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 128 | vector = env->exception_index << 2; |
| 129 | |
pbrook | 0cf5c67 | 2007-06-09 20:48:46 +0000 | [diff] [blame] | 130 | sp = env->aregs[7]; |
| 131 | |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 132 | fmt |= 0x40000000; |
| 133 | fmt |= (sp & 3) << 28; |
| 134 | fmt |= vector << 16; |
| 135 | fmt |= env->sr; |
| 136 | |
pbrook | 20dcee9 | 2007-06-03 11:13:39 +0000 | [diff] [blame] | 137 | 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 | |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 144 | /* ??? This could cause MMU faults. */ |
| 145 | sp &= ~3; |
| 146 | sp -= 4; |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 147 | cpu_stl_kernel(env, sp, retaddr); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 148 | sp -= 4; |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 149 | cpu_stl_kernel(env, sp, fmt); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 150 | env->aregs[7] = sp; |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 151 | /* Jump to vector. */ |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 152 | env->pc = cpu_ldl_kernel(env, env->vbr + vector); |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Andreas Färber | 97a8ea5 | 2013-02-02 10:57:51 +0100 | [diff] [blame] | 155 | void m68k_cpu_do_interrupt(CPUState *cs) |
Blue Swirl | 3c68882 | 2011-05-21 07:55:24 +0000 | [diff] [blame] | 156 | { |
Andreas Färber | 97a8ea5 | 2013-02-02 10:57:51 +0100 | [diff] [blame] | 157 | M68kCPU *cpu = M68K_CPU(cs); |
| 158 | CPUM68KState *env = &cpu->env; |
| 159 | |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 160 | do_interrupt_all(env, 0); |
Blue Swirl | 3c68882 | 2011-05-21 07:55:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 163 | void do_interrupt_m68k_hardirq(CPUM68KState *env) |
Blue Swirl | 3c68882 | 2011-05-21 07:55:24 +0000 | [diff] [blame] | 164 | { |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 165 | do_interrupt_all(env, 1); |
Blue Swirl | 3c68882 | 2011-05-21 07:55:24 +0000 | [diff] [blame] | 166 | } |
pbrook | 0633879 | 2007-05-23 19:58:11 +0000 | [diff] [blame] | 167 | #endif |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 168 | |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 169 | static void raise_exception(CPUM68KState *env, int tt) |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 170 | { |
| 171 | env->exception_index = tt; |
Blue Swirl | 1162c04 | 2011-05-14 12:52:35 +0000 | [diff] [blame] | 172 | cpu_loop_exit(env); |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 175 | void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt) |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 176 | { |
Blue Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 177 | raise_exception(env, tt); |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Andreas Färber | 2b3e3cf | 2012-03-14 01:38:22 +0100 | [diff] [blame] | 180 | void HELPER(divu)(CPUM68KState *env, uint32_t word) |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 181 | { |
| 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 Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 191 | if (den == 0) { |
| 192 | raise_exception(env, EXCP_DIV0); |
| 193 | } |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 194 | quot = num / den; |
| 195 | rem = num % den; |
| 196 | flags = 0; |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 197 | 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ärber | 2b3e3cf | 2012-03-14 01:38:22 +0100 | [diff] [blame] | 208 | void HELPER(divs)(CPUM68KState *env, uint32_t word) |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 209 | { |
| 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 Swirl | 3187114 | 2012-09-02 07:27:38 +0000 | [diff] [blame] | 218 | if (den == 0) { |
| 219 | raise_exception(env, EXCP_DIV0); |
| 220 | } |
pbrook | e1f3808 | 2008-05-24 22:29:16 +0000 | [diff] [blame] | 221 | 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 | } |