Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * MIPS emulation helpers for qemu. |
| 3 | * |
| 4 | * Copyright (c) 2004-2005 Jocelyn Mayer |
| 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 |
| 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | #include <stdarg.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <signal.h> |
| 25 | |
| 26 | #include "cpu.h" |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 27 | |
| 28 | enum { |
| 29 | TLBRET_DIRTY = -4, |
| 30 | TLBRET_INVALID = -3, |
| 31 | TLBRET_NOMATCH = -2, |
| 32 | TLBRET_BADADDR = -1, |
| 33 | TLBRET_MATCH = 0 |
| 34 | }; |
| 35 | |
| 36 | /* no MMU emulation */ |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 37 | int no_mmu_map_address (CPUMIPSState *env, hwaddr *physical, int *prot, |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 38 | target_ulong address, int rw, int access_type) |
| 39 | { |
| 40 | *physical = address; |
| 41 | *prot = PAGE_READ | PAGE_WRITE; |
| 42 | return TLBRET_MATCH; |
| 43 | } |
| 44 | |
| 45 | /* fixed mapping MMU emulation */ |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 46 | int fixed_mmu_map_address (CPUMIPSState *env, hwaddr *physical, int *prot, |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 47 | target_ulong address, int rw, int access_type) |
| 48 | { |
| 49 | if (address <= (int32_t)0x7FFFFFFFUL) { |
| 50 | if (!(env->CP0_Status & (1 << CP0St_ERL))) |
| 51 | *physical = address + 0x40000000UL; |
| 52 | else |
| 53 | *physical = address; |
| 54 | } else if (address <= (int32_t)0xBFFFFFFFUL) |
| 55 | *physical = address & 0x1FFFFFFF; |
| 56 | else |
| 57 | *physical = address; |
| 58 | |
| 59 | *prot = PAGE_READ | PAGE_WRITE; |
| 60 | return TLBRET_MATCH; |
| 61 | } |
| 62 | |
| 63 | /* MIPS32/MIPS64 R4000-style MMU emulation */ |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 64 | int r4k_map_address (CPUMIPSState *env, hwaddr *physical, int *prot, |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 65 | target_ulong address, int rw, int access_type) |
| 66 | { |
| 67 | uint8_t ASID = env->CP0_EntryHi & 0xFF; |
| 68 | int i; |
| 69 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 70 | for (i = 0; i < env->tlb->tlb_in_use; i++) { |
| 71 | r4k_tlb_t *tlb = &env->tlb->mmu.r4k.tlb[i]; |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 72 | /* 1k pages are not supported. */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 73 | target_ulong mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1); |
| 74 | target_ulong tag = address & ~mask; |
| 75 | target_ulong VPN = tlb->VPN & ~mask; |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 76 | #if defined(TARGET_MIPS64) |
| 77 | tag &= env->SEGMask; |
| 78 | #endif |
| 79 | |
| 80 | /* Check ASID, virtual page number & size */ |
| 81 | if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) { |
| 82 | /* TLB match */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 83 | int n = !!(address & mask & ~(mask >> 1)); |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 84 | /* Check access rights */ |
| 85 | if (!(n ? tlb->V1 : tlb->V0)) |
| 86 | return TLBRET_INVALID; |
| 87 | if (rw == 0 || (n ? tlb->D1 : tlb->D0)) { |
| 88 | *physical = tlb->PFN[n] | (address & (mask >> 1)); |
| 89 | *prot = PAGE_READ; |
| 90 | if (n ? tlb->D1 : tlb->D0) |
| 91 | *prot |= PAGE_WRITE; |
| 92 | return TLBRET_MATCH; |
| 93 | } |
| 94 | return TLBRET_DIRTY; |
| 95 | } |
| 96 | } |
| 97 | return TLBRET_NOMATCH; |
| 98 | } |
| 99 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 100 | |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 101 | #if !defined(CONFIG_USER_ONLY) |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 102 | static int get_physical_address (CPUMIPSState *env, hwaddr *physical, |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 103 | int *prot, target_ulong address, |
| 104 | int rw, int access_type) |
| 105 | { |
| 106 | /* User mode can only access useg/xuseg */ |
| 107 | int user_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM; |
| 108 | int supervisor_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_SM; |
| 109 | int kernel_mode = !user_mode && !supervisor_mode; |
| 110 | #if defined(TARGET_MIPS64) |
| 111 | int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0; |
| 112 | int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0; |
| 113 | int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0; |
| 114 | #endif |
| 115 | int ret = TLBRET_MATCH; |
| 116 | |
| 117 | #if 0 |
| 118 | qemu_log("user mode %d h %08x\n", user_mode, env->hflags); |
| 119 | #endif |
| 120 | |
| 121 | if (address <= (int32_t)0x7FFFFFFFUL) { |
| 122 | /* useg */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 123 | if (env->CP0_Status & (1 << CP0St_ERL)) { |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 124 | *physical = address & 0xFFFFFFFF; |
| 125 | *prot = PAGE_READ | PAGE_WRITE; |
| 126 | } else { |
| 127 | ret = env->tlb->map_address(env, physical, prot, address, rw, access_type); |
| 128 | } |
| 129 | #if defined(TARGET_MIPS64) |
| 130 | } else if (address < 0x4000000000000000ULL) { |
| 131 | /* xuseg */ |
| 132 | if (UX && address <= (0x3FFFFFFFFFFFFFFFULL & env->SEGMask)) { |
| 133 | ret = env->tlb->map_address(env, physical, prot, address, rw, access_type); |
| 134 | } else { |
| 135 | ret = TLBRET_BADADDR; |
| 136 | } |
| 137 | } else if (address < 0x8000000000000000ULL) { |
| 138 | /* xsseg */ |
| 139 | if ((supervisor_mode || kernel_mode) && |
| 140 | SX && address <= (0x7FFFFFFFFFFFFFFFULL & env->SEGMask)) { |
| 141 | ret = env->tlb->map_address(env, physical, prot, address, rw, access_type); |
| 142 | } else { |
| 143 | ret = TLBRET_BADADDR; |
| 144 | } |
| 145 | } else if (address < 0xC000000000000000ULL) { |
| 146 | /* xkphys */ |
| 147 | if (kernel_mode && KX && |
| 148 | (address & 0x07FFFFFFFFFFFFFFULL) <= env->PAMask) { |
| 149 | *physical = address & env->PAMask; |
| 150 | *prot = PAGE_READ | PAGE_WRITE; |
| 151 | } else { |
| 152 | ret = TLBRET_BADADDR; |
| 153 | } |
| 154 | } else if (address < 0xFFFFFFFF80000000ULL) { |
| 155 | /* xkseg */ |
| 156 | if (kernel_mode && KX && |
| 157 | address <= (0xFFFFFFFF7FFFFFFFULL & env->SEGMask)) { |
| 158 | ret = env->tlb->map_address(env, physical, prot, address, rw, access_type); |
| 159 | } else { |
| 160 | ret = TLBRET_BADADDR; |
| 161 | } |
| 162 | #endif |
| 163 | } else if (address < (int32_t)0xA0000000UL) { |
| 164 | /* kseg0 */ |
| 165 | if (kernel_mode) { |
| 166 | *physical = address - (int32_t)0x80000000UL; |
| 167 | *prot = PAGE_READ | PAGE_WRITE; |
| 168 | } else { |
| 169 | ret = TLBRET_BADADDR; |
| 170 | } |
| 171 | } else if (address < (int32_t)0xC0000000UL) { |
| 172 | /* kseg1 */ |
| 173 | if (kernel_mode) { |
| 174 | *physical = address - (int32_t)0xA0000000UL; |
| 175 | *prot = PAGE_READ | PAGE_WRITE; |
| 176 | } else { |
| 177 | ret = TLBRET_BADADDR; |
| 178 | } |
| 179 | } else if (address < (int32_t)0xE0000000UL) { |
| 180 | /* sseg (kseg2) */ |
| 181 | if (supervisor_mode || kernel_mode) { |
| 182 | ret = env->tlb->map_address(env, physical, prot, address, rw, access_type); |
| 183 | } else { |
| 184 | ret = TLBRET_BADADDR; |
| 185 | } |
| 186 | } else { |
| 187 | /* kseg3 */ |
| 188 | /* XXX: debug segment is not emulated */ |
| 189 | if (kernel_mode) { |
| 190 | ret = env->tlb->map_address(env, physical, prot, address, rw, access_type); |
| 191 | } else { |
| 192 | ret = TLBRET_BADADDR; |
| 193 | } |
| 194 | } |
| 195 | #if 0 |
| 196 | qemu_log(TARGET_FMT_lx " %d %d => " TARGET_FMT_lx " %d (%d)\n", |
| 197 | address, rw, access_type, *physical, *prot, ret); |
| 198 | #endif |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | #endif |
| 203 | |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 204 | static void raise_mmu_exception(CPUMIPSState *env, target_ulong address, |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 205 | int rw, int tlb_error) |
| 206 | { |
| 207 | int exception = 0, error_code = 0; |
| 208 | |
| 209 | switch (tlb_error) { |
| 210 | default: |
| 211 | case TLBRET_BADADDR: |
| 212 | /* Reference to kernel address from user mode or supervisor mode */ |
| 213 | /* Reference to supervisor address from user mode */ |
| 214 | if (rw) |
| 215 | exception = EXCP_AdES; |
| 216 | else |
| 217 | exception = EXCP_AdEL; |
| 218 | break; |
| 219 | case TLBRET_NOMATCH: |
| 220 | /* No TLB match for a mapped address */ |
| 221 | if (rw) |
| 222 | exception = EXCP_TLBS; |
| 223 | else |
| 224 | exception = EXCP_TLBL; |
| 225 | error_code = 1; |
| 226 | break; |
| 227 | case TLBRET_INVALID: |
| 228 | /* TLB match with no valid bit */ |
| 229 | if (rw) |
| 230 | exception = EXCP_TLBS; |
| 231 | else |
| 232 | exception = EXCP_TLBL; |
| 233 | break; |
| 234 | case TLBRET_DIRTY: |
| 235 | /* TLB match but 'D' bit is cleared */ |
| 236 | exception = EXCP_LTLBL; |
| 237 | break; |
| 238 | |
| 239 | } |
| 240 | /* Raise exception */ |
| 241 | env->CP0_BadVAddr = address; |
| 242 | env->CP0_Context = (env->CP0_Context & ~0x007fffff) | |
| 243 | ((address >> 9) & 0x007ffff0); |
| 244 | env->CP0_EntryHi = |
| 245 | (env->CP0_EntryHi & 0xFF) | (address & (TARGET_PAGE_MASK << 1)); |
| 246 | #if defined(TARGET_MIPS64) |
| 247 | env->CP0_EntryHi &= env->SEGMask; |
| 248 | env->CP0_XContext = (env->CP0_XContext & ((~0ULL) << (env->SEGBITS - 7))) | |
| 249 | ((address & 0xC00000000000ULL) >> (55 - env->SEGBITS)) | |
| 250 | ((address & ((1ULL << env->SEGBITS) - 1) & 0xFFFFFFFFFFFFE000ULL) >> 9); |
| 251 | #endif |
| 252 | env->exception_index = exception; |
| 253 | env->error_code = error_code; |
| 254 | } |
| 255 | |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 256 | /* |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 257 | * Get the pgd_current from TLB refill handler |
| 258 | * The kernel refill handler is generated by |
| 259 | * function build_r4000_tlb_refill_handler. |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 260 | */ |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 261 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 262 | typedef void (*pagetable_walk_t)(CPUMIPSState *env, |
| 263 | target_ulong pgd_addr, target_ulong vaddr, |
| 264 | target_ulong *entrylo0, target_ulong *entrylo1); |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 265 | static struct { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 266 | enum {PROBE, USEFASTTLB, USESLOWTLB} state; |
| 267 | uint32_t is_64bit; |
| 268 | pagetable_walk_t pagetable_walk; |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 269 | target_ulong pgd_current_p; |
| 270 | int softshift; |
| 271 | } linux_pte_info = {0}; |
| 272 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 273 | static inline void pagetable_walk32(CPUMIPSState *env, |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 274 | target_ulong pgd_addr, target_ulong vaddr, |
| 275 | target_ulong *entrylo0, target_ulong *entrylo1) |
| 276 | { |
| 277 | target_ulong ptw_phys, pt_addr, index; |
| 278 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 279 | #if defined(TARGET_MIPS64) |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 280 | /* workaround when running a 32bit |
| 281 | * emulation with the 64bit target emulator |
| 282 | */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 283 | vaddr = (uint32_t)vaddr; |
| 284 | #endif |
| 285 | |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 286 | ptw_phys = pgd_addr & 0x1fffffffUL; /* Assume pgd is in KSEG0/KSEG1 */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 287 | /* 32bit PTE lookup */ |
| 288 | index = (vaddr >> 22) << 2; /* Use bits 31..22 to index pgd */ |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 289 | ptw_phys += index; |
| 290 | |
| 291 | pt_addr = ldl_phys(ptw_phys); |
| 292 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 293 | ptw_phys = pt_addr & 0x1fffffffUL; /* Assume pgt is in KSEG0/KSEG1 */ |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 294 | index = ((vaddr >> 13) & 0x1ff) << 3; /* Use bits 21..13 to index pgt */ |
| 295 | ptw_phys += index; |
| 296 | |
| 297 | /* Get the entrylo values from pgt */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 298 | *entrylo0 = ldl_phys(ptw_phys) >> linux_pte_info.softshift; |
| 299 | *entrylo1 = ldl_phys(ptw_phys + 4) >> linux_pte_info.softshift; |
| 300 | } |
| 301 | |
| 302 | static inline void pagetable_walk64(CPUMIPSState *env, |
| 303 | target_ulong pgd_addr, target_ulong vaddr, |
| 304 | target_ulong *entrylo0, target_ulong *entrylo1) |
| 305 | { |
| 306 | target_ulong ptw_phys, pt_addr, index; |
| 307 | |
| 308 | pgd_addr = pgd_addr & 0x1fffffffUL; |
| 309 | index = ((uint64_t)vaddr >> 0x1b) & 0x1ff8; |
| 310 | pgd_addr += index; |
| 311 | |
| 312 | pgd_addr = ldl_phys(pgd_addr); |
| 313 | |
| 314 | ptw_phys = pgd_addr & 0x1fffffffUL; |
| 315 | index = ((uint64_t)vaddr >> 0x12) & 0xff8; |
| 316 | ptw_phys += index; |
| 317 | |
| 318 | pt_addr = ldl_phys(ptw_phys); |
| 319 | |
| 320 | ptw_phys = pt_addr & 0x1fffffffUL; |
| 321 | index = (((vaddr & 0xC00000000000ULL) >> (55 - env->SEGBITS)) | |
| 322 | ((vaddr & ((1ULL << env->SEGBITS) - 1) & 0xFFFFFFFFFFFFE000ULL) >> 9)) & 0xff0; |
| 323 | ptw_phys += index; |
| 324 | |
| 325 | /* Get the entrylo values from pgt */ |
| 326 | *entrylo0 = ldl_phys(ptw_phys) >> linux_pte_info.softshift; |
| 327 | *entrylo1 = ldl_phys(ptw_phys + 8) >> linux_pte_info.softshift; |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 330 | static inline target_ulong cpu_mips_get_pgd(CPUMIPSState *env, target_long bad_vaddr) |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 331 | { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 332 | if (unlikely(linux_pte_info.state == PROBE)) { |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 333 | int i; |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 334 | uint32_t lui_ins, lw_ins, srl_ins, is_64bit; |
| 335 | target_ulong address; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 336 | uint32_t ebase; |
| 337 | |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 338 | /* |
| 339 | * The exact TLB refill code varies depeing on the kernel version |
| 340 | * and configuration. Examins the TLB handler to extract |
| 341 | * pgd_current_p and the shift required to convert in memory PTE |
| 342 | * to TLB format |
| 343 | */ |
| 344 | static struct { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 345 | uint32_t is_64bit; |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 346 | struct { |
| 347 | uint32_t off; |
| 348 | uint32_t op; |
| 349 | uint32_t mask; |
| 350 | } lui, lw, srl; |
| 351 | } handlers[] = { |
| 352 | /* 2.6.29+ */ |
| 353 | { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 354 | 0, /* 32-bit refill handler */ |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 355 | {0x00, 0x3c1b0000, 0xffff0000}, /* 0x3c1b803f : lui k1,%hi(pgd_current_p) */ |
| 356 | {0x08, 0x8f7b0000, 0xffff0000}, /* 0x8f7b3000 : lw k1,%lo(k1) */ |
| 357 | {0x34, 0x001ad182, 0xffffffff} /* 0x001ad182 : srl k0,k0,0x6 */ |
| 358 | }, |
| 359 | /* 3.4+ */ |
| 360 | { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 361 | 0, /* 32-bit refill handler */ |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 362 | {0x00, 0x3c1b0000, 0xffff0000}, /* 0x3c1b803f : lui k1,%hi(pgd_current_p) */ |
| 363 | {0x08, 0x8f7b0000, 0xffff0000}, /* 0x8f7b3000 : lw k1,%lo(k1) */ |
| 364 | {0x34, 0x001ad142, 0xffffffff} /* 0x001ad182 : srl k0,k0,0x5 */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 365 | }, |
| 366 | /* 3.10+ No HugeTLB support*/ |
| 367 | { |
| 368 | 1, /* 64-bit refill handler */ |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 369 | {0x04, 0x3c1b0000, 0xffff0000}, /* 0x3c1b0000 : lui k1,%hi(swapper_pg_dir) */ |
| 370 | {0xac, 0xdf7b0000, 0xffff0000}, /* 0xdf7b0000 : ld k1,0(k1) */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 371 | {0xd4, 0x001ad17a, 0xffffffff} /* 0x001ad17a : dsrl k0,k0,0x5 */ |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 372 | } |
| 373 | }; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 374 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 375 | ebase = env->CP0_EBase - 0x80000000; |
| 376 | linux_pte_info.is_64bit = 0; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 377 | |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 378 | /* Match the kernel TLB refill exception handler against known code */ |
| 379 | for (i = 0; i < sizeof(handlers)/sizeof(handlers[0]); i++) { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 380 | is_64bit = handlers[i].is_64bit; |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 381 | lui_ins = ldl_phys(ebase + handlers[i].lui.off); |
| 382 | lw_ins = ldl_phys(ebase + handlers[i].lw.off); |
| 383 | srl_ins = ldl_phys(ebase + handlers[i].srl.off); |
| 384 | if (((lui_ins & handlers[i].lui.mask) == handlers[i].lui.op) && |
| 385 | ((lw_ins & handlers[i].lw.mask) == handlers[i].lw.op) && |
| 386 | ((srl_ins & handlers[i].srl.mask) == handlers[i].srl.op)) |
| 387 | break; |
| 388 | } |
| 389 | if (i >= sizeof(handlers)/sizeof(handlers[0])) { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 390 | #if defined(MUST_HAVE_FASTTLB) |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 391 | printf("TLBMiss handler dump:\n"); |
| 392 | for (i = 0; i < 0x80; i+= 4) |
| 393 | printf("0x%08x: 0x%08x\n", ebase + i, ldl_phys(ebase + i)); |
| 394 | cpu_abort(env, "TLBMiss handler signature not recognised\n"); |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 395 | #else |
| 396 | fprintf(stderr, "TLBMiss handler signature not recognised, using slowpath\n"); |
| 397 | linux_pte_info.state = USESLOWTLB; |
| 398 | linux_pte_info.pagetable_walk = NULL; |
| 399 | goto done; |
| 400 | #endif |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 401 | } |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 402 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 403 | if (is_64bit) { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 404 | linux_pte_info.pagetable_walk = &pagetable_walk64; |
| 405 | linux_pte_info.is_64bit = 1; |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 406 | /* swapper_pg_dir address */ |
| 407 | address = (lui_ins & 0xffff) << 16; |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 408 | } else { |
| 409 | address = (lui_ins & 0xffff) << 16; |
| 410 | address += (((int32_t)(lw_ins & 0xffff)) << 16) >> 16; |
| 411 | if (address >= 0x80000000 && address < 0xa0000000) |
| 412 | address -= 0x80000000; |
| 413 | else if (address >= 0xa0000000 && address <= 0xc0000000) |
| 414 | address -= 0xa0000000; |
| 415 | else |
| 416 | cpu_abort(env, "pgd_current_p not in KSEG0/KSEG1\n"); |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 417 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 418 | linux_pte_info.pagetable_walk = &pagetable_walk32; |
| 419 | } |
| 420 | |
| 421 | linux_pte_info.state = USEFASTTLB; |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 422 | linux_pte_info.pgd_current_p = address; |
| 423 | linux_pte_info.softshift = (srl_ins >> 6) & 0x1f; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 424 | } |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 425 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 426 | done: |
Chris Dearman | 6e22b20 | 2013-03-25 13:56:40 -0700 | [diff] [blame] | 427 | /* Get pgd_current */ |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 428 | if (linux_pte_info.state == USEFASTTLB) { |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 429 | if (linux_pte_info.is_64bit) { |
| 430 | target_ulong address = 0; |
| 431 | /* |
| 432 | * The kernel currently implicitely assumes that the |
| 433 | * MIPS SEGBITS parameter for the processor is |
| 434 | * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never |
| 435 | * allocate virtual addresses outside the maximum |
| 436 | * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But |
| 437 | * that doesn't prevent user code from accessing the |
| 438 | * higher xuseg addresses. Here, we make sure that |
| 439 | * everything but the lower xuseg addresses goes down |
| 440 | * the module_alloc/vmalloc path. |
| 441 | */ |
| 442 | address = ((uint64_t)bad_vaddr) >> 40; |
| 443 | if (likely(!address)) { |
| 444 | /* |
| 445 | * &pgd << 11 stored in CONTEXT [23..63]. |
| 446 | */ |
| 447 | address = env->CP0_Context; |
| 448 | address = ((uint64_t)address >> 23) << 23; |
| 449 | /* 1 0 1 0 1 << 6 xkphys cached */ |
| 450 | address |= 0x540; |
| 451 | /* dror k1,k1,0xb */ |
| 452 | address = ((uint64_t)address >> 11) | |
| 453 | (((uint64_t)address & 0x7ff) << 53); |
| 454 | return address; |
| 455 | } else if (bad_vaddr < 0) { |
| 456 | /* swapper_pg_dir address */ |
| 457 | return linux_pte_info.pgd_current_p; |
| 458 | } else { |
| 459 | /* |
| 460 | * We get here if we are an xsseg address, or if we are |
| 461 | * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary. |
| 462 | * |
| 463 | * Ignoring xsseg (assume disabled so would generate |
| 464 | * (address errors?), the only remaining possibility |
| 465 | * is the upper xuseg addresses. On processors with |
| 466 | * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these |
| 467 | * addresses would have taken an address error. We try |
| 468 | * to mimic that here by taking a load/istream page |
| 469 | * fault. |
| 470 | */ |
| 471 | return 0; /* fallback to software handler and do page fault */ |
| 472 | } |
| 473 | } else { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 474 | return ldl_phys(linux_pte_info.pgd_current_p); |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 475 | } |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 476 | } |
| 477 | return 0; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 480 | static inline int cpu_mips_tlb_refill(CPUMIPSState *env, target_ulong address, int rw, |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 481 | int mmu_idx, int is_softmmu) |
| 482 | { |
| 483 | int32_t saved_hflags; |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 484 | target_ulong saved_badvaddr,saved_entryhi,saved_context,saved_xcontext; |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 485 | target_ulong pgd_addr; |
| 486 | target_ulong fault_addr; |
| 487 | target_ulong entrylo0, entrylo1; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 488 | int ret; |
| 489 | |
| 490 | saved_badvaddr = env->CP0_BadVAddr; |
| 491 | saved_context = env->CP0_Context; |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 492 | saved_xcontext = env->CP0_XContext; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 493 | saved_entryhi = env->CP0_EntryHi; |
| 494 | saved_hflags = env->hflags; |
| 495 | |
| 496 | env->CP0_BadVAddr = address; |
| 497 | env->CP0_Context = (env->CP0_Context & ~0x007fffff) | |
| 498 | ((address >> 9) & 0x007ffff0); |
| 499 | env->CP0_EntryHi = |
| 500 | (env->CP0_EntryHi & 0xFF) | (address & (TARGET_PAGE_MASK << 1)); |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 501 | #if defined(TARGET_MIPS64) |
| 502 | env->CP0_EntryHi &= env->SEGMask; |
| 503 | env->CP0_XContext = (env->CP0_XContext & ((~0ULL) << (env->SEGBITS - 7))) | |
| 504 | ((address & 0xC00000000000ULL) >> (55 - env->SEGBITS)) | |
| 505 | ((address & ((1ULL << env->SEGBITS) - 1) & 0xFFFFFFFFFFFFE000ULL) >> 9); |
| 506 | #endif |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 507 | |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 508 | pgd_addr = 0; |
| 509 | pgd_addr = cpu_mips_get_pgd(env, address); |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 510 | |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 511 | /* if pgd_addr is unknown return TLBRET_NOMATCH |
| 512 | * to allow software handler to run |
| 513 | */ |
| 514 | if (unlikely(pgd_addr == 0)) { |
| 515 | ret = TLBRET_NOMATCH; |
| 516 | goto out; |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 519 | env->hflags = MIPS_HFLAG_KM; |
| 520 | fault_addr = env->CP0_BadVAddr; |
| 521 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 522 | linux_pte_info.pagetable_walk(env, pgd_addr, fault_addr, &entrylo0, &entrylo1); |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 523 | |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 524 | /* Refill the TLB */ |
| 525 | env->CP0_EntryLo0 = entrylo0; |
| 526 | env->CP0_EntryLo1 = entrylo1; |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 527 | r4k_helper_tlbwr(env); |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 528 | |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 529 | /* Since we know the TLB contents, we can |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 530 | * return the TLB lookup value here. |
| 531 | */ |
| 532 | |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 533 | target_ulong mask = env->CP0_PageMask | ~(TARGET_PAGE_MASK << 1); |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 534 | target_ulong lo = (address & mask & ~(mask >> 1)) ? entrylo1 : entrylo0; |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 535 | |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 536 | /* Is the TLB entry valid? */ |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 537 | if ((lo & (1 << CP0EnLo_V)) == 0) { |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 538 | ret = TLBRET_INVALID; |
| 539 | goto out; |
| 540 | } |
| 541 | |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 542 | /* Is this a read access or a write to a modifiable page? */ |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 543 | if (rw == 0 || (lo & (1 << CP0EnLo_D))) { |
| 544 | hwaddr physical = (lo >> CP0EnLo_PFN) << 12; |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 545 | physical |= address & (mask >> 1); |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 546 | int prot = PAGE_READ; |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 547 | if (lo & (1 << CP0EnLo_D)) |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 548 | prot |= PAGE_WRITE; |
| 549 | |
| 550 | tlb_set_page(env, address & TARGET_PAGE_MASK, |
David 'Digit' Turner | 0d8b235 | 2014-03-20 17:13:13 +0100 | [diff] [blame] | 551 | physical & TARGET_PAGE_MASK, prot | PAGE_EXEC, |
| 552 | mmu_idx, TARGET_PAGE_SIZE); |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 553 | ret = TLBRET_MATCH; |
| 554 | goto out; |
| 555 | } |
| 556 | ret = TLBRET_DIRTY; |
| 557 | |
| 558 | out: |
| 559 | env->CP0_BadVAddr = saved_badvaddr; |
| 560 | env->CP0_Context = saved_context; |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 561 | env->CP0_XContext = saved_xcontext; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 562 | env->CP0_EntryHi = saved_entryhi; |
| 563 | env->hflags = saved_hflags; |
| 564 | return ret; |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 565 | } |
| 566 | |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 567 | int cpu_mips_handle_mmu_fault (CPUMIPSState *env, target_ulong address, int rw, |
David 'Digit' Turner | 0d8b235 | 2014-03-20 17:13:13 +0100 | [diff] [blame] | 568 | int mmu_idx) |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 569 | { |
| 570 | #if !defined(CONFIG_USER_ONLY) |
David 'Digit' Turner | bcde109 | 2014-01-09 23:19:19 +0100 | [diff] [blame] | 571 | hwaddr physical; |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 572 | int prot; |
| 573 | #endif |
David 'Digit' Turner | a2c14f9 | 2014-02-04 01:02:30 +0100 | [diff] [blame] | 574 | //int exception = 0, error_code = 0; |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 575 | int access_type; |
| 576 | int ret = 0; |
| 577 | |
| 578 | #if 0 |
| 579 | log_cpu_state(env, 0); |
| 580 | #endif |
David 'Digit' Turner | 0d8b235 | 2014-03-20 17:13:13 +0100 | [diff] [blame] | 581 | qemu_log("%s pc " TARGET_FMT_lx " ad " TARGET_FMT_lx " rw %d mmu_idx %d\n", |
| 582 | __func__, env->active_tc.PC, address, rw, mmu_idx); |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 583 | |
| 584 | rw &= 1; |
| 585 | |
| 586 | /* data access */ |
| 587 | /* XXX: put correct access by using cpu_restore_state() |
| 588 | correctly */ |
| 589 | access_type = ACCESS_INT; |
| 590 | #if defined(CONFIG_USER_ONLY) |
| 591 | ret = TLBRET_NOMATCH; |
| 592 | #else |
| 593 | ret = get_physical_address(env, &physical, &prot, |
| 594 | address, rw, access_type); |
| 595 | qemu_log("%s address=" TARGET_FMT_lx " ret %d physical " TARGET_FMT_plx " prot %d\n", |
| 596 | __func__, address, ret, physical, prot); |
| 597 | if (ret == TLBRET_MATCH) { |
David 'Digit' Turner | 0d8b235 | 2014-03-20 17:13:13 +0100 | [diff] [blame] | 598 | tlb_set_page(env, address & TARGET_PAGE_MASK, |
| 599 | physical & TARGET_PAGE_MASK, prot | PAGE_EXEC, |
| 600 | mmu_idx, TARGET_PAGE_SIZE); |
| 601 | ret = 0; |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 602 | } else if (ret == TLBRET_NOMATCH) |
David 'Digit' Turner | 0d8b235 | 2014-03-20 17:13:13 +0100 | [diff] [blame] | 603 | ret = cpu_mips_tlb_refill(env,address,rw,mmu_idx,1); |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 604 | |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 605 | if (ret < 0) |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 606 | #endif |
| 607 | { |
| 608 | raise_mmu_exception(env, address, rw, ret); |
| 609 | ret = 1; |
| 610 | } |
| 611 | |
| 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | #if !defined(CONFIG_USER_ONLY) |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 616 | hwaddr cpu_mips_translate_address(CPUMIPSState *env, target_ulong address, int rw) |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 617 | { |
David 'Digit' Turner | bcde109 | 2014-01-09 23:19:19 +0100 | [diff] [blame] | 618 | hwaddr physical; |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 619 | int prot; |
| 620 | int access_type; |
| 621 | int ret = 0; |
| 622 | |
| 623 | rw &= 1; |
| 624 | |
| 625 | /* data access */ |
| 626 | access_type = ACCESS_INT; |
| 627 | ret = get_physical_address(env, &physical, &prot, |
| 628 | address, rw, access_type); |
Miodrag Dinic | 06635c3 | 2014-12-25 14:58:51 +0100 | [diff] [blame] | 629 | if (ret != TLBRET_MATCH) { |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 630 | raise_mmu_exception(env, address, rw, ret); |
| 631 | return -1LL; |
| 632 | } else { |
| 633 | return physical; |
| 634 | } |
| 635 | } |
| 636 | #endif |
| 637 | |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 638 | hwaddr cpu_get_phys_page_debug(CPUMIPSState *env, target_ulong addr) |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 639 | { |
| 640 | #if defined(CONFIG_USER_ONLY) |
| 641 | return addr; |
| 642 | #else |
David 'Digit' Turner | bcde109 | 2014-01-09 23:19:19 +0100 | [diff] [blame] | 643 | hwaddr phys_addr; |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 644 | int prot, ret; |
| 645 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 646 | #if defined(TARGET_MIPS64) |
| 647 | if (!(linux_pte_info.is_64bit) && |
| 648 | (linux_pte_info.state == USEFASTTLB)) |
| 649 | addr = ((int64_t)addr << 32) >> 32; |
| 650 | #endif |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 651 | ret = get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT); |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 652 | if (ret != TLBRET_MATCH) { |
| 653 | target_ulong pgd_addr = cpu_mips_get_pgd(env, addr); |
| 654 | |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 655 | phys_addr = -1; |
| 656 | if (likely(pgd_addr)) { |
| 657 | target_ulong entrylo0, entrylo1; |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 658 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 659 | linux_pte_info.pagetable_walk(env, pgd_addr, addr, &entrylo0, &entrylo1); |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 660 | |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 661 | target_ulong mask = env->CP0_PageMask | ~(TARGET_PAGE_MASK << 1); |
| 662 | target_ulong lo = (addr & mask & ~(mask >> 1)) ? entrylo1 : entrylo0; |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 663 | |
| 664 | if (lo & (1 << CP0EnLo_V)) { |
| 665 | phys_addr = ((lo >> CP0EnLo_PFN) << 12) | (addr & (mask >> 1)); |
| 666 | } else { |
| 667 | qemu_log("cpu_get_phys_page_debug() invalid mapping for vaddr: 0x" TARGET_FMT_plx "\n", addr); |
| 668 | } |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 669 | } else { |
Miodrag Dinic | 6c882a6 | 2014-10-22 15:06:55 +0200 | [diff] [blame] | 670 | qemu_log("cpu_get_phys_page_debug() fails for vaddr: 0x" TARGET_FMT_plx "\n", addr); |
Chris Dearman | 092b978 | 2014-07-17 11:42:30 +0200 | [diff] [blame] | 671 | } |
Chris Dearman | 55ff318 | 2012-08-03 14:35:52 -0700 | [diff] [blame] | 672 | } |
| 673 | return phys_addr; |
| 674 | #endif |
| 675 | } |
| 676 | |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 677 | static const char * const excp_names[EXCP_LAST + 1] = { |
| 678 | [EXCP_RESET] = "reset", |
| 679 | [EXCP_SRESET] = "soft reset", |
| 680 | [EXCP_DSS] = "debug single step", |
| 681 | [EXCP_DINT] = "debug interrupt", |
| 682 | [EXCP_NMI] = "non-maskable interrupt", |
| 683 | [EXCP_MCHECK] = "machine check", |
| 684 | [EXCP_EXT_INTERRUPT] = "interrupt", |
| 685 | [EXCP_DFWATCH] = "deferred watchpoint", |
| 686 | [EXCP_DIB] = "debug instruction breakpoint", |
| 687 | [EXCP_IWATCH] = "instruction fetch watchpoint", |
| 688 | [EXCP_AdEL] = "address error load", |
| 689 | [EXCP_AdES] = "address error store", |
| 690 | [EXCP_TLBF] = "TLB refill", |
| 691 | [EXCP_IBE] = "instruction bus error", |
| 692 | [EXCP_DBp] = "debug breakpoint", |
| 693 | [EXCP_SYSCALL] = "syscall", |
| 694 | [EXCP_BREAK] = "break", |
| 695 | [EXCP_CpU] = "coprocessor unusable", |
| 696 | [EXCP_RI] = "reserved instruction", |
| 697 | [EXCP_OVERFLOW] = "arithmetic overflow", |
| 698 | [EXCP_TRAP] = "trap", |
| 699 | [EXCP_FPE] = "floating point", |
| 700 | [EXCP_DDBS] = "debug data break store", |
| 701 | [EXCP_DWATCH] = "data watchpoint", |
| 702 | [EXCP_LTLBL] = "TLB modify", |
| 703 | [EXCP_TLBL] = "TLB load", |
| 704 | [EXCP_TLBS] = "TLB store", |
| 705 | [EXCP_DBE] = "data bus error", |
| 706 | [EXCP_DDBL] = "debug data break load", |
| 707 | [EXCP_THREAD] = "thread", |
| 708 | [EXCP_MDMX] = "MDMX", |
| 709 | [EXCP_C2E] = "precise coprocessor 2", |
| 710 | [EXCP_CACHE] = "cache error", |
| 711 | }; |
| 712 | |
David 'Digit' Turner | e2678e1 | 2014-01-16 15:56:43 +0100 | [diff] [blame] | 713 | void do_interrupt (CPUMIPSState *env) |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 714 | { |
| 715 | #if !defined(CONFIG_USER_ONLY) |
| 716 | target_ulong offset; |
| 717 | int cause = -1; |
| 718 | const char *name; |
| 719 | |
| 720 | if (qemu_log_enabled() && env->exception_index != EXCP_EXT_INTERRUPT) { |
| 721 | if (env->exception_index < 0 || env->exception_index > EXCP_LAST) |
| 722 | name = "unknown"; |
| 723 | else |
| 724 | name = excp_names[env->exception_index]; |
| 725 | |
| 726 | qemu_log("%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " %s exception\n", |
| 727 | __func__, env->active_tc.PC, env->CP0_EPC, name); |
| 728 | } |
| 729 | if (env->exception_index == EXCP_EXT_INTERRUPT && |
| 730 | (env->hflags & MIPS_HFLAG_DM)) |
| 731 | env->exception_index = EXCP_DINT; |
| 732 | offset = 0x180; |
| 733 | switch (env->exception_index) { |
| 734 | case EXCP_DSS: |
| 735 | env->CP0_Debug |= 1 << CP0DB_DSS; |
| 736 | /* Debug single step cannot be raised inside a delay slot and |
| 737 | resume will always occur on the next instruction |
| 738 | (but we assume the pc has always been updated during |
| 739 | code translation). */ |
| 740 | env->CP0_DEPC = env->active_tc.PC; |
| 741 | goto enter_debug_mode; |
| 742 | case EXCP_DINT: |
| 743 | env->CP0_Debug |= 1 << CP0DB_DINT; |
| 744 | goto set_DEPC; |
| 745 | case EXCP_DIB: |
| 746 | env->CP0_Debug |= 1 << CP0DB_DIB; |
| 747 | goto set_DEPC; |
| 748 | case EXCP_DBp: |
| 749 | env->CP0_Debug |= 1 << CP0DB_DBp; |
| 750 | goto set_DEPC; |
| 751 | case EXCP_DDBS: |
| 752 | env->CP0_Debug |= 1 << CP0DB_DDBS; |
| 753 | goto set_DEPC; |
| 754 | case EXCP_DDBL: |
| 755 | env->CP0_Debug |= 1 << CP0DB_DDBL; |
| 756 | set_DEPC: |
| 757 | if (env->hflags & MIPS_HFLAG_BMASK) { |
| 758 | /* If the exception was raised from a delay slot, |
| 759 | come back to the jump. */ |
| 760 | env->CP0_DEPC = env->active_tc.PC - 4; |
| 761 | env->hflags &= ~MIPS_HFLAG_BMASK; |
| 762 | } else { |
| 763 | env->CP0_DEPC = env->active_tc.PC; |
| 764 | } |
| 765 | enter_debug_mode: |
| 766 | env->hflags |= MIPS_HFLAG_DM | MIPS_HFLAG_64 | MIPS_HFLAG_CP0; |
| 767 | env->hflags &= ~(MIPS_HFLAG_KSU); |
| 768 | /* EJTAG probe trap enable is not implemented... */ |
| 769 | if (!(env->CP0_Status & (1 << CP0St_EXL))) |
| 770 | env->CP0_Cause &= ~(1 << CP0Ca_BD); |
| 771 | env->active_tc.PC = (int32_t)0xBFC00480; |
| 772 | break; |
| 773 | case EXCP_RESET: |
David 'Digit' Turner | bf7a22f | 2014-03-25 16:36:03 +0100 | [diff] [blame] | 774 | cpu_reset(ENV_GET_CPU(env)); |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 775 | break; |
| 776 | case EXCP_SRESET: |
| 777 | env->CP0_Status |= (1 << CP0St_SR); |
| 778 | memset(env->CP0_WatchLo, 0, sizeof(*env->CP0_WatchLo)); |
| 779 | goto set_error_EPC; |
| 780 | case EXCP_NMI: |
| 781 | env->CP0_Status |= (1 << CP0St_NMI); |
| 782 | set_error_EPC: |
| 783 | if (env->hflags & MIPS_HFLAG_BMASK) { |
| 784 | /* If the exception was raised from a delay slot, |
| 785 | come back to the jump. */ |
| 786 | env->CP0_ErrorEPC = env->active_tc.PC - 4; |
| 787 | env->hflags &= ~MIPS_HFLAG_BMASK; |
| 788 | } else { |
| 789 | env->CP0_ErrorEPC = env->active_tc.PC; |
| 790 | } |
| 791 | env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV); |
| 792 | env->hflags |= MIPS_HFLAG_64 | MIPS_HFLAG_CP0; |
| 793 | env->hflags &= ~(MIPS_HFLAG_KSU); |
| 794 | if (!(env->CP0_Status & (1 << CP0St_EXL))) |
| 795 | env->CP0_Cause &= ~(1 << CP0Ca_BD); |
| 796 | env->active_tc.PC = (int32_t)0xBFC00000; |
| 797 | break; |
| 798 | case EXCP_EXT_INTERRUPT: |
| 799 | cause = 0; |
| 800 | if (env->CP0_Cause & (1 << CP0Ca_IV)) |
| 801 | offset = 0x200; |
| 802 | goto set_EPC; |
| 803 | case EXCP_LTLBL: |
| 804 | cause = 1; |
| 805 | goto set_EPC; |
| 806 | case EXCP_TLBL: |
| 807 | cause = 2; |
| 808 | if (env->error_code == 1 && !(env->CP0_Status & (1 << CP0St_EXL))) { |
| 809 | #if defined(TARGET_MIPS64) |
| 810 | int R = env->CP0_BadVAddr >> 62; |
| 811 | int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0; |
| 812 | int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0; |
| 813 | int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0; |
| 814 | |
| 815 | if ((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) |
| 816 | offset = 0x080; |
| 817 | else |
| 818 | #endif |
| 819 | offset = 0x000; |
| 820 | } |
| 821 | goto set_EPC; |
| 822 | case EXCP_TLBS: |
| 823 | cause = 3; |
| 824 | if (env->error_code == 1 && !(env->CP0_Status & (1 << CP0St_EXL))) { |
| 825 | #if defined(TARGET_MIPS64) |
| 826 | int R = env->CP0_BadVAddr >> 62; |
| 827 | int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0; |
| 828 | int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0; |
| 829 | int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0; |
| 830 | |
| 831 | if ((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) |
| 832 | offset = 0x080; |
| 833 | else |
| 834 | #endif |
| 835 | offset = 0x000; |
| 836 | } |
| 837 | goto set_EPC; |
| 838 | case EXCP_AdEL: |
| 839 | cause = 4; |
| 840 | goto set_EPC; |
| 841 | case EXCP_AdES: |
| 842 | cause = 5; |
| 843 | goto set_EPC; |
| 844 | case EXCP_IBE: |
| 845 | cause = 6; |
| 846 | goto set_EPC; |
| 847 | case EXCP_DBE: |
| 848 | cause = 7; |
| 849 | goto set_EPC; |
| 850 | case EXCP_SYSCALL: |
| 851 | cause = 8; |
| 852 | goto set_EPC; |
| 853 | case EXCP_BREAK: |
| 854 | cause = 9; |
| 855 | goto set_EPC; |
| 856 | case EXCP_RI: |
| 857 | cause = 10; |
| 858 | goto set_EPC; |
| 859 | case EXCP_CpU: |
| 860 | cause = 11; |
| 861 | env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) | |
| 862 | (env->error_code << CP0Ca_CE); |
| 863 | goto set_EPC; |
| 864 | case EXCP_OVERFLOW: |
| 865 | cause = 12; |
| 866 | goto set_EPC; |
| 867 | case EXCP_TRAP: |
| 868 | cause = 13; |
| 869 | goto set_EPC; |
| 870 | case EXCP_FPE: |
| 871 | cause = 15; |
| 872 | goto set_EPC; |
| 873 | case EXCP_C2E: |
| 874 | cause = 18; |
| 875 | goto set_EPC; |
| 876 | case EXCP_MDMX: |
| 877 | cause = 22; |
| 878 | goto set_EPC; |
| 879 | case EXCP_DWATCH: |
| 880 | cause = 23; |
| 881 | /* XXX: TODO: manage defered watch exceptions */ |
| 882 | goto set_EPC; |
| 883 | case EXCP_MCHECK: |
| 884 | cause = 24; |
| 885 | goto set_EPC; |
| 886 | case EXCP_THREAD: |
| 887 | cause = 25; |
| 888 | goto set_EPC; |
| 889 | case EXCP_CACHE: |
| 890 | cause = 30; |
| 891 | if (env->CP0_Status & (1 << CP0St_BEV)) { |
| 892 | offset = 0x100; |
| 893 | } else { |
| 894 | offset = 0x20000100; |
| 895 | } |
| 896 | set_EPC: |
| 897 | if (!(env->CP0_Status & (1 << CP0St_EXL))) { |
| 898 | if (env->hflags & MIPS_HFLAG_BMASK) { |
| 899 | /* If the exception was raised from a delay slot, |
| 900 | come back to the jump. */ |
| 901 | env->CP0_EPC = env->active_tc.PC - 4; |
| 902 | env->CP0_Cause |= (1 << CP0Ca_BD); |
| 903 | } else { |
| 904 | env->CP0_EPC = env->active_tc.PC; |
| 905 | env->CP0_Cause &= ~(1 << CP0Ca_BD); |
| 906 | } |
| 907 | env->CP0_Status |= (1 << CP0St_EXL); |
| 908 | env->hflags |= MIPS_HFLAG_64 | MIPS_HFLAG_CP0; |
| 909 | env->hflags &= ~(MIPS_HFLAG_KSU); |
| 910 | } |
| 911 | env->hflags &= ~MIPS_HFLAG_BMASK; |
| 912 | if (env->CP0_Status & (1 << CP0St_BEV)) { |
| 913 | env->active_tc.PC = (int32_t)0xBFC00200; |
| 914 | } else { |
| 915 | env->active_tc.PC = (int32_t)(env->CP0_EBase & ~0x3ff); |
| 916 | } |
| 917 | env->active_tc.PC += offset; |
| 918 | env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC); |
| 919 | break; |
| 920 | default: |
| 921 | qemu_log("Invalid MIPS exception %d. Exiting\n", env->exception_index); |
| 922 | printf("Invalid MIPS exception %d. Exiting\n", env->exception_index); |
| 923 | exit(1); |
| 924 | } |
| 925 | if (qemu_log_enabled() && env->exception_index != EXCP_EXT_INTERRUPT) { |
| 926 | qemu_log("%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d\n" |
| 927 | " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n", |
| 928 | __func__, env->active_tc.PC, env->CP0_EPC, cause, |
| 929 | env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr, |
| 930 | env->CP0_DEPC); |
| 931 | } |
| 932 | #endif |
| 933 | env->exception_index = EXCP_NONE; |
| 934 | } |
| 935 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 936 | void r4k_invalidate_tlb (CPUMIPSState *env, int idx, int use_extra) |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 937 | { |
| 938 | r4k_tlb_t *tlb; |
| 939 | target_ulong addr; |
| 940 | target_ulong end; |
| 941 | uint8_t ASID = env->CP0_EntryHi & 0xFF; |
| 942 | target_ulong mask; |
| 943 | |
| 944 | tlb = &env->tlb->mmu.r4k.tlb[idx]; |
| 945 | /* The qemu TLB is flushed when the ASID changes, so no need to |
| 946 | flush these entries again. */ |
| 947 | if (tlb->G == 0 && tlb->ASID != ASID) { |
| 948 | return; |
| 949 | } |
| 950 | |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 951 | if (use_extra && env->tlb->tlb_in_use < MIPS_TLB_MAX) { |
| 952 | /* For tlbwr, we can shadow the discarded entry into |
| 953 | a new (fake) TLB entry, as long as the guest can not |
| 954 | tell that it's there. */ |
| 955 | env->tlb->mmu.r4k.tlb[env->tlb->tlb_in_use] = *tlb; |
| 956 | env->tlb->tlb_in_use++; |
| 957 | return; |
| 958 | } |
| 959 | |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 960 | /* 1k pages are not supported. */ |
| 961 | mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1); |
| 962 | if (tlb->V0) { |
| 963 | addr = tlb->VPN & ~mask; |
| 964 | #if defined(TARGET_MIPS64) |
| 965 | if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) { |
| 966 | addr |= 0x3FFFFF0000000000ULL; |
| 967 | } |
| 968 | #endif |
| 969 | end = addr | (mask >> 1); |
| 970 | while (addr < end) { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 971 | tlb_flush_page(env, addr); |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 972 | addr += TARGET_PAGE_SIZE; |
| 973 | } |
| 974 | } |
| 975 | if (tlb->V1) { |
| 976 | addr = (tlb->VPN & ~mask) | ((mask >> 1) + 1); |
| 977 | #if defined(TARGET_MIPS64) |
| 978 | if (addr >= (0xFFFFFFFF80000000ULL & env->SEGMask)) { |
| 979 | addr |= 0x3FFFFF0000000000ULL; |
| 980 | } |
| 981 | #endif |
| 982 | end = addr | mask; |
| 983 | while (addr - 1 < end) { |
Miodrag Dinic | fe4583d | 2014-07-25 15:05:19 +0200 | [diff] [blame] | 984 | tlb_flush_page(env, addr); |
Bhanu Chetlapalli | 409c7b6 | 2012-01-31 16:25:04 -0800 | [diff] [blame] | 985 | addr += TARGET_PAGE_SIZE; |
| 986 | } |
| 987 | } |
| 988 | } |