1 /* 2 * SH4 emulation 3 * 4 * Copyright (c) 2005 Samuel Tardieu 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.1 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 20 #include "qemu/osdep.h" 21 22 #include "cpu.h" 23 #include "exec/cputlb.h" 24 #include "exec/exec-all.h" 25 #include "exec/page-protection.h" 26 #include "exec/target_page.h" 27 #include "exec/log.h" 28 29 #if !defined(CONFIG_USER_ONLY) 30 #include "hw/sh4/sh_intc.h" 31 #include "system/runstate.h" 32 #endif 33 34 #define MMU_OK 0 35 #define MMU_ITLB_MISS (-1) 36 #define MMU_ITLB_MULTIPLE (-2) 37 #define MMU_ITLB_VIOLATION (-3) 38 #define MMU_DTLB_MISS_READ (-4) 39 #define MMU_DTLB_MISS_WRITE (-5) 40 #define MMU_DTLB_INITIAL_WRITE (-6) 41 #define MMU_DTLB_VIOLATION_READ (-7) 42 #define MMU_DTLB_VIOLATION_WRITE (-8) 43 #define MMU_DTLB_MULTIPLE (-9) 44 #define MMU_DTLB_MISS (-10) 45 #define MMU_IADDR_ERROR (-11) 46 #define MMU_DADDR_ERROR_READ (-12) 47 #define MMU_DADDR_ERROR_WRITE (-13) 48 49 #if defined(CONFIG_USER_ONLY) 50 51 int cpu_sh4_is_cached(CPUSH4State *env, target_ulong addr) 52 { 53 /* For user mode, only U0 area is cacheable. */ 54 return !(addr & 0x80000000); 55 } 56 57 #else /* !CONFIG_USER_ONLY */ 58 59 void superh_cpu_do_interrupt(CPUState *cs) 60 { 61 CPUSH4State *env = cpu_env(cs); 62 int do_irq = cs->interrupt_request & CPU_INTERRUPT_HARD; 63 int do_exp, irq_vector = cs->exception_index; 64 65 /* prioritize exceptions over interrupts */ 66 67 do_exp = cs->exception_index != -1; 68 do_irq = do_irq && (cs->exception_index == -1); 69 70 if (env->sr & (1u << SR_BL)) { 71 if (do_exp && cs->exception_index != 0x1e0) { 72 /* In theory a masked exception generates a reset exception, 73 which in turn jumps to the reset vector. However this only 74 works when using a bootloader. When using a kernel and an 75 initrd, they need to be reloaded and the program counter 76 should be loaded with the kernel entry point. 77 qemu_system_reset_request takes care of that. */ 78 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 79 return; 80 } 81 if (do_irq && !env->in_sleep) { 82 return; /* masked */ 83 } 84 } 85 env->in_sleep = 0; 86 87 if (do_irq) { 88 irq_vector = sh_intc_get_pending_vector(env->intc_handle, 89 (env->sr >> 4) & 0xf); 90 if (irq_vector == -1) { 91 return; /* masked */ 92 } 93 } 94 95 if (qemu_loglevel_mask(CPU_LOG_INT)) { 96 const char *expname; 97 switch (cs->exception_index) { 98 case 0x0e0: 99 expname = "addr_error"; 100 break; 101 case 0x040: 102 expname = "tlb_miss"; 103 break; 104 case 0x0a0: 105 expname = "tlb_violation"; 106 break; 107 case 0x180: 108 expname = "illegal_instruction"; 109 break; 110 case 0x1a0: 111 expname = "slot_illegal_instruction"; 112 break; 113 case 0x800: 114 expname = "fpu_disable"; 115 break; 116 case 0x820: 117 expname = "slot_fpu"; 118 break; 119 case 0x100: 120 expname = "data_write"; 121 break; 122 case 0x060: 123 expname = "dtlb_miss_write"; 124 break; 125 case 0x0c0: 126 expname = "dtlb_violation_write"; 127 break; 128 case 0x120: 129 expname = "fpu_exception"; 130 break; 131 case 0x080: 132 expname = "initial_page_write"; 133 break; 134 case 0x160: 135 expname = "trapa"; 136 break; 137 default: 138 expname = do_irq ? "interrupt" : "???"; 139 break; 140 } 141 qemu_log("exception 0x%03x [%s] raised\n", 142 irq_vector, expname); 143 log_cpu_state(cs, 0); 144 } 145 146 env->ssr = cpu_read_sr(env); 147 env->spc = env->pc; 148 env->sgr = env->gregs[15]; 149 env->sr |= (1u << SR_BL) | (1u << SR_MD) | (1u << SR_RB); 150 env->lock_addr = -1; 151 152 if (env->flags & TB_FLAG_DELAY_SLOT_MASK) { 153 /* Branch instruction should be executed again before delay slot. */ 154 env->spc -= 2; 155 /* Clear flags for exception/interrupt routine. */ 156 env->flags &= ~TB_FLAG_DELAY_SLOT_MASK; 157 } 158 159 if (do_exp) { 160 env->expevt = cs->exception_index; 161 switch (cs->exception_index) { 162 case 0x000: 163 case 0x020: 164 case 0x140: 165 env->sr &= ~(1u << SR_FD); 166 env->sr |= 0xf << 4; /* IMASK */ 167 env->pc = 0xa0000000; 168 break; 169 case 0x040: 170 case 0x060: 171 env->pc = env->vbr + 0x400; 172 break; 173 case 0x160: 174 env->spc += 2; /* special case for TRAPA */ 175 /* fall through */ 176 default: 177 env->pc = env->vbr + 0x100; 178 break; 179 } 180 return; 181 } 182 183 if (do_irq) { 184 env->intevt = irq_vector; 185 env->pc = env->vbr + 0x600; 186 return; 187 } 188 } 189 190 static void update_itlb_use(CPUSH4State * env, int itlbnb) 191 { 192 uint32_t or_mask = 0, and_mask = 0xff; 193 194 switch (itlbnb) { 195 case 0: 196 and_mask = 0x1f; 197 break; 198 case 1: 199 and_mask = 0xe7; 200 or_mask = 0x80; 201 break; 202 case 2: 203 and_mask = 0xfb; 204 or_mask = 0x50; 205 break; 206 case 3: 207 or_mask = 0x2c; 208 break; 209 } 210 211 env->mmucr &= (and_mask << 24) | 0x00ffffff; 212 env->mmucr |= (or_mask << 24); 213 } 214 215 static int itlb_replacement(CPUSH4State * env) 216 { 217 if ((env->mmucr & 0xe0000000) == 0xe0000000) { 218 return 0; 219 } 220 if ((env->mmucr & 0x98000000) == 0x18000000) { 221 return 1; 222 } 223 if ((env->mmucr & 0x54000000) == 0x04000000) { 224 return 2; 225 } 226 if ((env->mmucr & 0x2c000000) == 0x00000000) { 227 return 3; 228 } 229 cpu_abort(env_cpu(env), "Unhandled itlb_replacement"); 230 } 231 232 /* Find the corresponding entry in the right TLB 233 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE 234 */ 235 static int find_tlb_entry(CPUSH4State * env, target_ulong address, 236 tlb_t * entries, uint8_t nbtlb, int use_asid) 237 { 238 int match = MMU_DTLB_MISS; 239 uint32_t start, end; 240 uint8_t asid; 241 int i; 242 243 asid = env->pteh & 0xff; 244 245 for (i = 0; i < nbtlb; i++) { 246 if (!entries[i].v) 247 continue; /* Invalid entry */ 248 if (!entries[i].sh && use_asid && entries[i].asid != asid) 249 continue; /* Bad ASID */ 250 start = (entries[i].vpn << 10) & ~(entries[i].size - 1); 251 end = start + entries[i].size - 1; 252 if (address >= start && address <= end) { /* Match */ 253 if (match != MMU_DTLB_MISS) 254 return MMU_DTLB_MULTIPLE; /* Multiple match */ 255 match = i; 256 } 257 } 258 return match; 259 } 260 261 static void increment_urc(CPUSH4State * env) 262 { 263 uint8_t urb, urc; 264 265 /* Increment URC */ 266 urb = ((env->mmucr) >> 18) & 0x3f; 267 urc = ((env->mmucr) >> 10) & 0x3f; 268 urc++; 269 if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1)) 270 urc = 0; 271 env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10); 272 } 273 274 /* Copy and utlb entry into itlb 275 Return entry 276 */ 277 static int copy_utlb_entry_itlb(CPUSH4State *env, int utlb) 278 { 279 int itlb; 280 281 tlb_t * ientry; 282 itlb = itlb_replacement(env); 283 ientry = &env->itlb[itlb]; 284 if (ientry->v) { 285 tlb_flush_page(env_cpu(env), ientry->vpn << 10); 286 } 287 *ientry = env->utlb[utlb]; 288 update_itlb_use(env, itlb); 289 return itlb; 290 } 291 292 /* Find itlb entry 293 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE 294 */ 295 static int find_itlb_entry(CPUSH4State * env, target_ulong address, 296 int use_asid) 297 { 298 int e; 299 300 e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid); 301 if (e == MMU_DTLB_MULTIPLE) { 302 e = MMU_ITLB_MULTIPLE; 303 } else if (e == MMU_DTLB_MISS) { 304 e = MMU_ITLB_MISS; 305 } else if (e >= 0) { 306 update_itlb_use(env, e); 307 } 308 return e; 309 } 310 311 /* Find utlb entry 312 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */ 313 static int find_utlb_entry(CPUSH4State * env, target_ulong address, int use_asid) 314 { 315 /* per utlb access */ 316 increment_urc(env); 317 318 /* Return entry */ 319 return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid); 320 } 321 322 /* Match address against MMU 323 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE, 324 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ, 325 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS, 326 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION, 327 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE. 328 */ 329 static int get_mmu_address(CPUSH4State * env, target_ulong * physical, 330 int *prot, target_ulong address, 331 MMUAccessType access_type) 332 { 333 int use_asid, n; 334 tlb_t *matching = NULL; 335 336 use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD)); 337 338 if (access_type == MMU_INST_FETCH) { 339 n = find_itlb_entry(env, address, use_asid); 340 if (n >= 0) { 341 matching = &env->itlb[n]; 342 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) { 343 n = MMU_ITLB_VIOLATION; 344 } else { 345 *prot = PAGE_EXEC; 346 } 347 } else { 348 n = find_utlb_entry(env, address, use_asid); 349 if (n >= 0) { 350 n = copy_utlb_entry_itlb(env, n); 351 matching = &env->itlb[n]; 352 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) { 353 n = MMU_ITLB_VIOLATION; 354 } else { 355 *prot = PAGE_READ | PAGE_EXEC; 356 if ((matching->pr & 1) && matching->d) { 357 *prot |= PAGE_WRITE; 358 } 359 } 360 } else if (n == MMU_DTLB_MULTIPLE) { 361 n = MMU_ITLB_MULTIPLE; 362 } else if (n == MMU_DTLB_MISS) { 363 n = MMU_ITLB_MISS; 364 } 365 } 366 } else { 367 n = find_utlb_entry(env, address, use_asid); 368 if (n >= 0) { 369 matching = &env->utlb[n]; 370 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) { 371 n = (access_type == MMU_DATA_STORE) 372 ? MMU_DTLB_VIOLATION_WRITE : MMU_DTLB_VIOLATION_READ; 373 } else if ((access_type == MMU_DATA_STORE) && !(matching->pr & 1)) { 374 n = MMU_DTLB_VIOLATION_WRITE; 375 } else if ((access_type == MMU_DATA_STORE) && !matching->d) { 376 n = MMU_DTLB_INITIAL_WRITE; 377 } else { 378 *prot = PAGE_READ; 379 if ((matching->pr & 1) && matching->d) { 380 *prot |= PAGE_WRITE; 381 } 382 } 383 } else if (n == MMU_DTLB_MISS) { 384 n = (access_type == MMU_DATA_STORE) 385 ? MMU_DTLB_MISS_WRITE : MMU_DTLB_MISS_READ; 386 } 387 } 388 if (n >= 0) { 389 n = MMU_OK; 390 *physical = ((matching->ppn << 10) & ~(matching->size - 1)) 391 | (address & (matching->size - 1)); 392 } 393 return n; 394 } 395 396 static int get_physical_address(CPUSH4State * env, target_ulong * physical, 397 int *prot, target_ulong address, 398 MMUAccessType access_type) 399 { 400 /* P1, P2 and P4 areas do not use translation */ 401 if ((address >= 0x80000000 && address < 0xc0000000) || address >= 0xe0000000) { 402 if (!(env->sr & (1u << SR_MD)) 403 && (address < 0xe0000000 || address >= 0xe4000000)) { 404 /* Unauthorized access in user mode (only store queues are available) */ 405 qemu_log_mask(LOG_GUEST_ERROR, "Unauthorized access\n"); 406 if (access_type == MMU_DATA_LOAD) { 407 return MMU_DADDR_ERROR_READ; 408 } else if (access_type == MMU_DATA_STORE) { 409 return MMU_DADDR_ERROR_WRITE; 410 } else { 411 return MMU_IADDR_ERROR; 412 } 413 } 414 if (address >= 0x80000000 && address < 0xc0000000) { 415 /* Mask upper 3 bits for P1 and P2 areas */ 416 *physical = address & 0x1fffffff; 417 } else { 418 *physical = address; 419 } 420 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 421 return MMU_OK; 422 } 423 424 /* If MMU is disabled, return the corresponding physical page */ 425 if (!(env->mmucr & MMUCR_AT)) { 426 *physical = address & 0x1FFFFFFF; 427 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 428 return MMU_OK; 429 } 430 431 /* We need to resort to the MMU */ 432 return get_mmu_address(env, physical, prot, address, access_type); 433 } 434 435 hwaddr superh_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) 436 { 437 target_ulong physical; 438 int prot; 439 440 if (get_physical_address(cpu_env(cs), &physical, &prot, addr, MMU_DATA_LOAD) 441 == MMU_OK) { 442 return physical; 443 } 444 445 return -1; 446 } 447 448 void cpu_load_tlb(CPUSH4State * env) 449 { 450 CPUState *cs = env_cpu(env); 451 int n = cpu_mmucr_urc(env->mmucr); 452 tlb_t * entry = &env->utlb[n]; 453 454 if (entry->v) { 455 /* Overwriting valid entry in utlb. */ 456 target_ulong address = entry->vpn << 10; 457 tlb_flush_page(cs, address); 458 } 459 460 /* Take values into cpu status from registers. */ 461 entry->asid = (uint8_t)cpu_pteh_asid(env->pteh); 462 entry->vpn = cpu_pteh_vpn(env->pteh); 463 entry->v = (uint8_t)cpu_ptel_v(env->ptel); 464 entry->ppn = cpu_ptel_ppn(env->ptel); 465 entry->sz = (uint8_t)cpu_ptel_sz(env->ptel); 466 switch (entry->sz) { 467 case 0: /* 00 */ 468 entry->size = 1024; /* 1K */ 469 break; 470 case 1: /* 01 */ 471 entry->size = 1024 * 4; /* 4K */ 472 break; 473 case 2: /* 10 */ 474 entry->size = 1024 * 64; /* 64K */ 475 break; 476 case 3: /* 11 */ 477 entry->size = 1024 * 1024; /* 1M */ 478 break; 479 default: 480 cpu_abort(cs, "Unhandled load_tlb"); 481 break; 482 } 483 entry->sh = (uint8_t)cpu_ptel_sh(env->ptel); 484 entry->c = (uint8_t)cpu_ptel_c(env->ptel); 485 entry->pr = (uint8_t)cpu_ptel_pr(env->ptel); 486 entry->d = (uint8_t)cpu_ptel_d(env->ptel); 487 entry->wt = (uint8_t)cpu_ptel_wt(env->ptel); 488 entry->sa = (uint8_t)cpu_ptea_sa(env->ptea); 489 entry->tc = (uint8_t)cpu_ptea_tc(env->ptea); 490 } 491 492 void cpu_sh4_invalidate_tlb(CPUSH4State *s) 493 { 494 int i; 495 496 /* UTLB */ 497 for (i = 0; i < UTLB_SIZE; i++) { 498 tlb_t * entry = &s->utlb[i]; 499 entry->v = 0; 500 } 501 /* ITLB */ 502 for (i = 0; i < ITLB_SIZE; i++) { 503 tlb_t * entry = &s->itlb[i]; 504 entry->v = 0; 505 } 506 507 tlb_flush(env_cpu(s)); 508 } 509 510 uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State *s, 511 hwaddr addr) 512 { 513 int index = (addr & 0x00000300) >> 8; 514 tlb_t * entry = &s->itlb[index]; 515 516 return (entry->vpn << 10) | 517 (entry->v << 8) | 518 (entry->asid); 519 } 520 521 void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State *s, hwaddr addr, 522 uint32_t mem_value) 523 { 524 uint32_t vpn = (mem_value & 0xfffffc00) >> 10; 525 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8); 526 uint8_t asid = (uint8_t)(mem_value & 0x000000ff); 527 528 int index = (addr & 0x00000300) >> 8; 529 tlb_t * entry = &s->itlb[index]; 530 if (entry->v) { 531 /* Overwriting valid entry in itlb. */ 532 target_ulong address = entry->vpn << 10; 533 tlb_flush_page(env_cpu(s), address); 534 } 535 entry->asid = asid; 536 entry->vpn = vpn; 537 entry->v = v; 538 } 539 540 uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State *s, 541 hwaddr addr) 542 { 543 int array = (addr & 0x00800000) >> 23; 544 int index = (addr & 0x00000300) >> 8; 545 tlb_t * entry = &s->itlb[index]; 546 547 if (array == 0) { 548 /* ITLB Data Array 1 */ 549 return (entry->ppn << 10) | 550 (entry->v << 8) | 551 (entry->pr << 5) | 552 ((entry->sz & 1) << 6) | 553 ((entry->sz & 2) << 4) | 554 (entry->c << 3) | 555 (entry->sh << 1); 556 } else { 557 /* ITLB Data Array 2 */ 558 return (entry->tc << 1) | 559 (entry->sa); 560 } 561 } 562 563 void cpu_sh4_write_mmaped_itlb_data(CPUSH4State *s, hwaddr addr, 564 uint32_t mem_value) 565 { 566 int array = (addr & 0x00800000) >> 23; 567 int index = (addr & 0x00000300) >> 8; 568 tlb_t * entry = &s->itlb[index]; 569 570 if (array == 0) { 571 /* ITLB Data Array 1 */ 572 if (entry->v) { 573 /* Overwriting valid entry in utlb. */ 574 target_ulong address = entry->vpn << 10; 575 tlb_flush_page(env_cpu(s), address); 576 } 577 entry->ppn = (mem_value & 0x1ffffc00) >> 10; 578 entry->v = (mem_value & 0x00000100) >> 8; 579 entry->sz = (mem_value & 0x00000080) >> 6 | 580 (mem_value & 0x00000010) >> 4; 581 entry->pr = (mem_value & 0x00000040) >> 5; 582 entry->c = (mem_value & 0x00000008) >> 3; 583 entry->sh = (mem_value & 0x00000002) >> 1; 584 } else { 585 /* ITLB Data Array 2 */ 586 entry->tc = (mem_value & 0x00000008) >> 3; 587 entry->sa = (mem_value & 0x00000007); 588 } 589 } 590 591 uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State *s, 592 hwaddr addr) 593 { 594 int index = (addr & 0x00003f00) >> 8; 595 tlb_t * entry = &s->utlb[index]; 596 597 increment_urc(s); /* per utlb access */ 598 599 return (entry->vpn << 10) | 600 (entry->v << 8) | 601 (entry->asid); 602 } 603 604 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, hwaddr addr, 605 uint32_t mem_value) 606 { 607 int associate = addr & 0x0000080; 608 uint32_t vpn = (mem_value & 0xfffffc00) >> 10; 609 uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9); 610 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8); 611 uint8_t asid = (uint8_t)(mem_value & 0x000000ff); 612 int use_asid = !(s->mmucr & MMUCR_SV) || !(s->sr & (1u << SR_MD)); 613 614 if (associate) { 615 int i; 616 tlb_t * utlb_match_entry = NULL; 617 int needs_tlb_flush = 0; 618 619 /* search UTLB */ 620 for (i = 0; i < UTLB_SIZE; i++) { 621 tlb_t * entry = &s->utlb[i]; 622 if (!entry->v) 623 continue; 624 625 if (entry->vpn == vpn 626 && (!use_asid || entry->asid == asid || entry->sh)) { 627 if (utlb_match_entry) { 628 CPUState *cs = env_cpu(s); 629 630 /* Multiple TLB Exception */ 631 cs->exception_index = 0x140; 632 s->tea = addr; 633 break; 634 } 635 if (entry->v && !v) 636 needs_tlb_flush = 1; 637 entry->v = v; 638 entry->d = d; 639 utlb_match_entry = entry; 640 } 641 increment_urc(s); /* per utlb access */ 642 } 643 644 /* search ITLB */ 645 for (i = 0; i < ITLB_SIZE; i++) { 646 tlb_t * entry = &s->itlb[i]; 647 if (entry->vpn == vpn 648 && (!use_asid || entry->asid == asid || entry->sh)) { 649 if (entry->v && !v) 650 needs_tlb_flush = 1; 651 if (utlb_match_entry) 652 *entry = *utlb_match_entry; 653 else 654 entry->v = v; 655 break; 656 } 657 } 658 659 if (needs_tlb_flush) { 660 tlb_flush_page(env_cpu(s), vpn << 10); 661 } 662 } else { 663 int index = (addr & 0x00003f00) >> 8; 664 tlb_t * entry = &s->utlb[index]; 665 if (entry->v) { 666 CPUState *cs = env_cpu(s); 667 668 /* Overwriting valid entry in utlb. */ 669 target_ulong address = entry->vpn << 10; 670 tlb_flush_page(cs, address); 671 } 672 entry->asid = asid; 673 entry->vpn = vpn; 674 entry->d = d; 675 entry->v = v; 676 increment_urc(s); 677 } 678 } 679 680 uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State *s, 681 hwaddr addr) 682 { 683 int array = (addr & 0x00800000) >> 23; 684 int index = (addr & 0x00003f00) >> 8; 685 tlb_t * entry = &s->utlb[index]; 686 687 increment_urc(s); /* per utlb access */ 688 689 if (array == 0) { 690 /* ITLB Data Array 1 */ 691 return (entry->ppn << 10) | 692 (entry->v << 8) | 693 (entry->pr << 5) | 694 ((entry->sz & 1) << 6) | 695 ((entry->sz & 2) << 4) | 696 (entry->c << 3) | 697 (entry->d << 2) | 698 (entry->sh << 1) | 699 (entry->wt); 700 } else { 701 /* ITLB Data Array 2 */ 702 return (entry->tc << 1) | 703 (entry->sa); 704 } 705 } 706 707 void cpu_sh4_write_mmaped_utlb_data(CPUSH4State *s, hwaddr addr, 708 uint32_t mem_value) 709 { 710 int array = (addr & 0x00800000) >> 23; 711 int index = (addr & 0x00003f00) >> 8; 712 tlb_t * entry = &s->utlb[index]; 713 714 increment_urc(s); /* per utlb access */ 715 716 if (array == 0) { 717 /* UTLB Data Array 1 */ 718 if (entry->v) { 719 /* Overwriting valid entry in utlb. */ 720 target_ulong address = entry->vpn << 10; 721 tlb_flush_page(env_cpu(s), address); 722 } 723 entry->ppn = (mem_value & 0x1ffffc00) >> 10; 724 entry->v = (mem_value & 0x00000100) >> 8; 725 entry->sz = (mem_value & 0x00000080) >> 6 | 726 (mem_value & 0x00000010) >> 4; 727 entry->pr = (mem_value & 0x00000060) >> 5; 728 entry->c = (mem_value & 0x00000008) >> 3; 729 entry->d = (mem_value & 0x00000004) >> 2; 730 entry->sh = (mem_value & 0x00000002) >> 1; 731 entry->wt = (mem_value & 0x00000001); 732 } else { 733 /* UTLB Data Array 2 */ 734 entry->tc = (mem_value & 0x00000008) >> 3; 735 entry->sa = (mem_value & 0x00000007); 736 } 737 } 738 739 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr) 740 { 741 int n; 742 int use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD)); 743 744 /* check area */ 745 if (env->sr & (1u << SR_MD)) { 746 /* For privileged mode, P2 and P4 area is not cacheable. */ 747 if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr) 748 return 0; 749 } else { 750 /* For user mode, only U0 area is cacheable. */ 751 if (0x80000000 <= addr) 752 return 0; 753 } 754 755 /* 756 * TODO : Evaluate CCR and check if the cache is on or off. 757 * Now CCR is not in CPUSH4State, but in SH7750State. 758 * When you move the ccr into CPUSH4State, the code will be 759 * as follows. 760 */ 761 #if 0 762 /* check if operand cache is enabled or not. */ 763 if (!(env->ccr & 1)) 764 return 0; 765 #endif 766 767 /* if MMU is off, no check for TLB. */ 768 if (env->mmucr & MMUCR_AT) 769 return 1; 770 771 /* check TLB */ 772 n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid); 773 if (n >= 0) 774 return env->itlb[n].c; 775 776 n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid); 777 if (n >= 0) 778 return env->utlb[n].c; 779 780 return 0; 781 } 782 783 bool superh_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 784 { 785 if (interrupt_request & CPU_INTERRUPT_HARD) { 786 /* Delay slots are indivisible, ignore interrupts */ 787 if (cpu_env(cs)->flags & TB_FLAG_DELAY_SLOT_MASK) { 788 return false; 789 } else { 790 superh_cpu_do_interrupt(cs); 791 return true; 792 } 793 } 794 return false; 795 } 796 797 bool superh_cpu_tlb_fill(CPUState *cs, vaddr address, int size, 798 MMUAccessType access_type, int mmu_idx, 799 bool probe, uintptr_t retaddr) 800 { 801 CPUSH4State *env = cpu_env(cs); 802 int ret; 803 804 target_ulong physical; 805 int prot; 806 807 ret = get_physical_address(env, &physical, &prot, address, access_type); 808 809 if (ret == MMU_OK) { 810 address &= TARGET_PAGE_MASK; 811 physical &= TARGET_PAGE_MASK; 812 tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE); 813 return true; 814 } 815 if (probe) { 816 return false; 817 } 818 819 if (ret != MMU_DTLB_MULTIPLE && ret != MMU_ITLB_MULTIPLE) { 820 env->pteh = (env->pteh & PTEH_ASID_MASK) | (address & PTEH_VPN_MASK); 821 } 822 823 env->tea = address; 824 switch (ret) { 825 case MMU_ITLB_MISS: 826 case MMU_DTLB_MISS_READ: 827 cs->exception_index = 0x040; 828 break; 829 case MMU_DTLB_MULTIPLE: 830 case MMU_ITLB_MULTIPLE: 831 cs->exception_index = 0x140; 832 break; 833 case MMU_ITLB_VIOLATION: 834 cs->exception_index = 0x0a0; 835 break; 836 case MMU_DTLB_MISS_WRITE: 837 cs->exception_index = 0x060; 838 break; 839 case MMU_DTLB_INITIAL_WRITE: 840 cs->exception_index = 0x080; 841 break; 842 case MMU_DTLB_VIOLATION_READ: 843 cs->exception_index = 0x0a0; 844 break; 845 case MMU_DTLB_VIOLATION_WRITE: 846 cs->exception_index = 0x0c0; 847 break; 848 case MMU_IADDR_ERROR: 849 case MMU_DADDR_ERROR_READ: 850 cs->exception_index = 0x0e0; 851 break; 852 case MMU_DADDR_ERROR_WRITE: 853 cs->exception_index = 0x100; 854 break; 855 default: 856 cpu_abort(cs, "Unhandled MMU fault"); 857 } 858 cpu_loop_exit_restore(cs, retaddr); 859 } 860 #endif /* !CONFIG_USER_ONLY */ 861