1 /* 2 * PowerPC MMU, TLB and BAT emulation helpers for QEMU. 3 * 4 * Copyright (c) 2003-2007 Jocelyn Mayer 5 * Copyright (c) 2013 David Gibson, IBM Corporation 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include "qemu/osdep.h" 22 #include "cpu.h" 23 #include "exec/exec-all.h" 24 #include "exec/helper-proto.h" 25 #include "sysemu/kvm.h" 26 #include "kvm_ppc.h" 27 #include "mmu-hash32.h" 28 #include "exec/log.h" 29 30 /* #define DEBUG_BAT */ 31 32 #ifdef DEBUG_BATS 33 # define LOG_BATS(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__) 34 #else 35 # define LOG_BATS(...) do { } while (0) 36 #endif 37 38 struct mmu_ctx_hash32 { 39 hwaddr raddr; /* Real address */ 40 int prot; /* Protection bits */ 41 int key; /* Access key */ 42 }; 43 44 static int ppc_hash32_pp_prot(int key, int pp, int nx) 45 { 46 int prot; 47 48 if (key == 0) { 49 switch (pp) { 50 case 0x0: 51 case 0x1: 52 case 0x2: 53 prot = PAGE_READ | PAGE_WRITE; 54 break; 55 56 case 0x3: 57 prot = PAGE_READ; 58 break; 59 60 default: 61 abort(); 62 } 63 } else { 64 switch (pp) { 65 case 0x0: 66 prot = 0; 67 break; 68 69 case 0x1: 70 case 0x3: 71 prot = PAGE_READ; 72 break; 73 74 case 0x2: 75 prot = PAGE_READ | PAGE_WRITE; 76 break; 77 78 default: 79 abort(); 80 } 81 } 82 if (nx == 0) { 83 prot |= PAGE_EXEC; 84 } 85 86 return prot; 87 } 88 89 static int ppc_hash32_pte_prot(PowerPCCPU *cpu, 90 target_ulong sr, ppc_hash_pte32_t pte) 91 { 92 CPUPPCState *env = &cpu->env; 93 unsigned pp, key; 94 95 key = !!(msr_pr ? (sr & SR32_KP) : (sr & SR32_KS)); 96 pp = pte.pte1 & HPTE32_R_PP; 97 98 return ppc_hash32_pp_prot(key, pp, !!(sr & SR32_NX)); 99 } 100 101 static target_ulong hash32_bat_size(PowerPCCPU *cpu, 102 target_ulong batu, target_ulong batl) 103 { 104 CPUPPCState *env = &cpu->env; 105 106 if ((msr_pr && !(batu & BATU32_VP)) 107 || (!msr_pr && !(batu & BATU32_VS))) { 108 return 0; 109 } 110 111 return BATU32_BEPI & ~((batu & BATU32_BL) << 15); 112 } 113 114 static int hash32_bat_prot(PowerPCCPU *cpu, 115 target_ulong batu, target_ulong batl) 116 { 117 int pp, prot; 118 119 prot = 0; 120 pp = batl & BATL32_PP; 121 if (pp != 0) { 122 prot = PAGE_READ | PAGE_EXEC; 123 if (pp == 0x2) { 124 prot |= PAGE_WRITE; 125 } 126 } 127 return prot; 128 } 129 130 static target_ulong hash32_bat_601_size(PowerPCCPU *cpu, 131 target_ulong batu, target_ulong batl) 132 { 133 if (!(batl & BATL32_601_V)) { 134 return 0; 135 } 136 137 return BATU32_BEPI & ~((batl & BATL32_601_BL) << 17); 138 } 139 140 static int hash32_bat_601_prot(PowerPCCPU *cpu, 141 target_ulong batu, target_ulong batl) 142 { 143 CPUPPCState *env = &cpu->env; 144 int key, pp; 145 146 pp = batu & BATU32_601_PP; 147 if (msr_pr == 0) { 148 key = !!(batu & BATU32_601_KS); 149 } else { 150 key = !!(batu & BATU32_601_KP); 151 } 152 return ppc_hash32_pp_prot(key, pp, 0); 153 } 154 155 static hwaddr ppc_hash32_bat_lookup(PowerPCCPU *cpu, target_ulong ea, int rwx, 156 int *prot) 157 { 158 CPUPPCState *env = &cpu->env; 159 target_ulong *BATlt, *BATut; 160 int i; 161 162 LOG_BATS("%s: %cBAT v " TARGET_FMT_lx "\n", __func__, 163 rwx == 2 ? 'I' : 'D', ea); 164 if (rwx == 2) { 165 BATlt = env->IBAT[1]; 166 BATut = env->IBAT[0]; 167 } else { 168 BATlt = env->DBAT[1]; 169 BATut = env->DBAT[0]; 170 } 171 for (i = 0; i < env->nb_BATs; i++) { 172 target_ulong batu = BATut[i]; 173 target_ulong batl = BATlt[i]; 174 target_ulong mask; 175 176 if (unlikely(env->mmu_model == POWERPC_MMU_601)) { 177 mask = hash32_bat_601_size(cpu, batu, batl); 178 } else { 179 mask = hash32_bat_size(cpu, batu, batl); 180 } 181 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx 182 " BATl " TARGET_FMT_lx "\n", __func__, 183 type == ACCESS_CODE ? 'I' : 'D', i, ea, batu, batl); 184 185 if (mask && ((ea & mask) == (batu & BATU32_BEPI))) { 186 hwaddr raddr = (batl & mask) | (ea & ~mask); 187 188 if (unlikely(env->mmu_model == POWERPC_MMU_601)) { 189 *prot = hash32_bat_601_prot(cpu, batu, batl); 190 } else { 191 *prot = hash32_bat_prot(cpu, batu, batl); 192 } 193 194 return raddr & TARGET_PAGE_MASK; 195 } 196 } 197 198 /* No hit */ 199 #if defined(DEBUG_BATS) 200 if (qemu_log_enabled()) { 201 LOG_BATS("no BAT match for " TARGET_FMT_lx ":\n", ea); 202 for (i = 0; i < 4; i++) { 203 BATu = &BATut[i]; 204 BATl = &BATlt[i]; 205 BEPIu = *BATu & BATU32_BEPIU; 206 BEPIl = *BATu & BATU32_BEPIL; 207 bl = (*BATu & 0x00001FFC) << 15; 208 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx 209 " BATl " TARGET_FMT_lx "\n\t" TARGET_FMT_lx " " 210 TARGET_FMT_lx " " TARGET_FMT_lx "\n", 211 __func__, type == ACCESS_CODE ? 'I' : 'D', i, ea, 212 *BATu, *BATl, BEPIu, BEPIl, bl); 213 } 214 } 215 #endif 216 217 return -1; 218 } 219 220 static int ppc_hash32_direct_store(PowerPCCPU *cpu, target_ulong sr, 221 target_ulong eaddr, int rwx, 222 hwaddr *raddr, int *prot) 223 { 224 CPUState *cs = CPU(cpu); 225 CPUPPCState *env = &cpu->env; 226 int key = !!(msr_pr ? (sr & SR32_KP) : (sr & SR32_KS)); 227 228 qemu_log_mask(CPU_LOG_MMU, "direct store...\n"); 229 230 if ((sr & 0x1FF00000) >> 20 == 0x07f) { 231 /* 232 * Memory-forced I/O controller interface access 233 * 234 * If T=1 and BUID=x'07F', the 601 performs a memory access 235 * to SR[28-31] LA[4-31], bypassing all protection mechanisms. 236 */ 237 *raddr = ((sr & 0xF) << 28) | (eaddr & 0x0FFFFFFF); 238 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 239 return 0; 240 } 241 242 if (rwx == 2) { 243 /* No code fetch is allowed in direct-store areas */ 244 cs->exception_index = POWERPC_EXCP_ISI; 245 env->error_code = 0x10000000; 246 return 1; 247 } 248 249 switch (env->access_type) { 250 case ACCESS_INT: 251 /* Integer load/store : only access allowed */ 252 break; 253 case ACCESS_FLOAT: 254 /* Floating point load/store */ 255 cs->exception_index = POWERPC_EXCP_ALIGN; 256 env->error_code = POWERPC_EXCP_ALIGN_FP; 257 env->spr[SPR_DAR] = eaddr; 258 return 1; 259 case ACCESS_RES: 260 /* lwarx, ldarx or srwcx. */ 261 env->error_code = 0; 262 env->spr[SPR_DAR] = eaddr; 263 if (rwx == 1) { 264 env->spr[SPR_DSISR] = 0x06000000; 265 } else { 266 env->spr[SPR_DSISR] = 0x04000000; 267 } 268 return 1; 269 case ACCESS_CACHE: 270 /* 271 * dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi 272 * 273 * Should make the instruction do no-op. As it already do 274 * no-op, it's quite easy :-) 275 */ 276 *raddr = eaddr; 277 return 0; 278 case ACCESS_EXT: 279 /* eciwx or ecowx */ 280 cs->exception_index = POWERPC_EXCP_DSI; 281 env->error_code = 0; 282 env->spr[SPR_DAR] = eaddr; 283 if (rwx == 1) { 284 env->spr[SPR_DSISR] = 0x06100000; 285 } else { 286 env->spr[SPR_DSISR] = 0x04100000; 287 } 288 return 1; 289 default: 290 cpu_abort(cs, "ERROR: instruction should not need " 291 "address translation\n"); 292 } 293 if ((rwx == 1 || key != 1) && (rwx == 0 || key != 0)) { 294 *raddr = eaddr; 295 return 0; 296 } else { 297 cs->exception_index = POWERPC_EXCP_DSI; 298 env->error_code = 0; 299 env->spr[SPR_DAR] = eaddr; 300 if (rwx == 1) { 301 env->spr[SPR_DSISR] = 0x0a000000; 302 } else { 303 env->spr[SPR_DSISR] = 0x08000000; 304 } 305 return 1; 306 } 307 } 308 309 hwaddr get_pteg_offset32(PowerPCCPU *cpu, hwaddr hash) 310 { 311 target_ulong mask = ppc_hash32_hpt_mask(cpu); 312 313 return (hash * HASH_PTEG_SIZE_32) & mask; 314 } 315 316 static hwaddr ppc_hash32_pteg_search(PowerPCCPU *cpu, hwaddr pteg_off, 317 bool secondary, target_ulong ptem, 318 ppc_hash_pte32_t *pte) 319 { 320 hwaddr pte_offset = pteg_off; 321 target_ulong pte0, pte1; 322 int i; 323 324 for (i = 0; i < HPTES_PER_GROUP; i++) { 325 pte0 = ppc_hash32_load_hpte0(cpu, pte_offset); 326 /* 327 * pte0 contains the valid bit and must be read before pte1, 328 * otherwise we might see an old pte1 with a new valid bit and 329 * thus an inconsistent hpte value 330 */ 331 smp_rmb(); 332 pte1 = ppc_hash32_load_hpte1(cpu, pte_offset); 333 334 if ((pte0 & HPTE32_V_VALID) 335 && (secondary == !!(pte0 & HPTE32_V_SECONDARY)) 336 && HPTE32_V_COMPARE(pte0, ptem)) { 337 pte->pte0 = pte0; 338 pte->pte1 = pte1; 339 return pte_offset; 340 } 341 342 pte_offset += HASH_PTE_SIZE_32; 343 } 344 345 return -1; 346 } 347 348 static hwaddr ppc_hash32_htab_lookup(PowerPCCPU *cpu, 349 target_ulong sr, target_ulong eaddr, 350 ppc_hash_pte32_t *pte) 351 { 352 hwaddr pteg_off, pte_offset; 353 hwaddr hash; 354 uint32_t vsid, pgidx, ptem; 355 356 vsid = sr & SR32_VSID; 357 pgidx = (eaddr & ~SEGMENT_MASK_256M) >> TARGET_PAGE_BITS; 358 hash = vsid ^ pgidx; 359 ptem = (vsid << 7) | (pgidx >> 10); 360 361 /* Page address translation */ 362 qemu_log_mask(CPU_LOG_MMU, "htab_base " TARGET_FMT_plx 363 " htab_mask " TARGET_FMT_plx 364 " hash " TARGET_FMT_plx "\n", 365 ppc_hash32_hpt_base(cpu), ppc_hash32_hpt_mask(cpu), hash); 366 367 /* Primary PTEG lookup */ 368 qemu_log_mask(CPU_LOG_MMU, "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 369 " vsid=%" PRIx32 " ptem=%" PRIx32 370 " hash=" TARGET_FMT_plx "\n", 371 ppc_hash32_hpt_base(cpu), ppc_hash32_hpt_mask(cpu), 372 vsid, ptem, hash); 373 pteg_off = get_pteg_offset32(cpu, hash); 374 pte_offset = ppc_hash32_pteg_search(cpu, pteg_off, 0, ptem, pte); 375 if (pte_offset == -1) { 376 /* Secondary PTEG lookup */ 377 qemu_log_mask(CPU_LOG_MMU, "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 378 " vsid=%" PRIx32 " api=%" PRIx32 379 " hash=" TARGET_FMT_plx "\n", ppc_hash32_hpt_base(cpu), 380 ppc_hash32_hpt_mask(cpu), vsid, ptem, ~hash); 381 pteg_off = get_pteg_offset32(cpu, ~hash); 382 pte_offset = ppc_hash32_pteg_search(cpu, pteg_off, 1, ptem, pte); 383 } 384 385 return pte_offset; 386 } 387 388 static hwaddr ppc_hash32_pte_raddr(target_ulong sr, ppc_hash_pte32_t pte, 389 target_ulong eaddr) 390 { 391 hwaddr rpn = pte.pte1 & HPTE32_R_RPN; 392 hwaddr mask = ~TARGET_PAGE_MASK; 393 394 return (rpn & ~mask) | (eaddr & mask); 395 } 396 397 int ppc_hash32_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr, int rwx, 398 int mmu_idx) 399 { 400 CPUState *cs = CPU(cpu); 401 CPUPPCState *env = &cpu->env; 402 target_ulong sr; 403 hwaddr pte_offset; 404 ppc_hash_pte32_t pte; 405 int prot; 406 uint32_t new_pte1; 407 const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC}; 408 hwaddr raddr; 409 410 assert((rwx == 0) || (rwx == 1) || (rwx == 2)); 411 412 /* 1. Handle real mode accesses */ 413 if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) { 414 /* Translation is off */ 415 raddr = eaddr; 416 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 417 PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx, 418 TARGET_PAGE_SIZE); 419 return 0; 420 } 421 422 /* 2. Check Block Address Translation entries (BATs) */ 423 if (env->nb_BATs != 0) { 424 raddr = ppc_hash32_bat_lookup(cpu, eaddr, rwx, &prot); 425 if (raddr != -1) { 426 if (need_prot[rwx] & ~prot) { 427 if (rwx == 2) { 428 cs->exception_index = POWERPC_EXCP_ISI; 429 env->error_code = 0x08000000; 430 } else { 431 cs->exception_index = POWERPC_EXCP_DSI; 432 env->error_code = 0; 433 env->spr[SPR_DAR] = eaddr; 434 if (rwx == 1) { 435 env->spr[SPR_DSISR] = 0x0a000000; 436 } else { 437 env->spr[SPR_DSISR] = 0x08000000; 438 } 439 } 440 return 1; 441 } 442 443 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, 444 raddr & TARGET_PAGE_MASK, prot, mmu_idx, 445 TARGET_PAGE_SIZE); 446 return 0; 447 } 448 } 449 450 /* 3. Look up the Segment Register */ 451 sr = env->sr[eaddr >> 28]; 452 453 /* 4. Handle direct store segments */ 454 if (sr & SR32_T) { 455 if (ppc_hash32_direct_store(cpu, sr, eaddr, rwx, 456 &raddr, &prot) == 0) { 457 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, 458 raddr & TARGET_PAGE_MASK, prot, mmu_idx, 459 TARGET_PAGE_SIZE); 460 return 0; 461 } else { 462 return 1; 463 } 464 } 465 466 /* 5. Check for segment level no-execute violation */ 467 if ((rwx == 2) && (sr & SR32_NX)) { 468 cs->exception_index = POWERPC_EXCP_ISI; 469 env->error_code = 0x10000000; 470 return 1; 471 } 472 473 /* 6. Locate the PTE in the hash table */ 474 pte_offset = ppc_hash32_htab_lookup(cpu, sr, eaddr, &pte); 475 if (pte_offset == -1) { 476 if (rwx == 2) { 477 cs->exception_index = POWERPC_EXCP_ISI; 478 env->error_code = 0x40000000; 479 } else { 480 cs->exception_index = POWERPC_EXCP_DSI; 481 env->error_code = 0; 482 env->spr[SPR_DAR] = eaddr; 483 if (rwx == 1) { 484 env->spr[SPR_DSISR] = 0x42000000; 485 } else { 486 env->spr[SPR_DSISR] = 0x40000000; 487 } 488 } 489 490 return 1; 491 } 492 qemu_log_mask(CPU_LOG_MMU, 493 "found PTE at offset %08" HWADDR_PRIx "\n", pte_offset); 494 495 /* 7. Check access permissions */ 496 497 prot = ppc_hash32_pte_prot(cpu, sr, pte); 498 499 if (need_prot[rwx] & ~prot) { 500 /* Access right violation */ 501 qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n"); 502 if (rwx == 2) { 503 cs->exception_index = POWERPC_EXCP_ISI; 504 env->error_code = 0x08000000; 505 } else { 506 cs->exception_index = POWERPC_EXCP_DSI; 507 env->error_code = 0; 508 env->spr[SPR_DAR] = eaddr; 509 if (rwx == 1) { 510 env->spr[SPR_DSISR] = 0x0a000000; 511 } else { 512 env->spr[SPR_DSISR] = 0x08000000; 513 } 514 } 515 return 1; 516 } 517 518 qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n"); 519 520 /* 8. Update PTE referenced and changed bits if necessary */ 521 522 new_pte1 = pte.pte1 | HPTE32_R_R; /* set referenced bit */ 523 if (rwx == 1) { 524 new_pte1 |= HPTE32_R_C; /* set changed (dirty) bit */ 525 } else { 526 /* 527 * Treat the page as read-only for now, so that a later write 528 * will pass through this function again to set the C bit 529 */ 530 prot &= ~PAGE_WRITE; 531 } 532 533 if (new_pte1 != pte.pte1) { 534 ppc_hash32_store_hpte1(cpu, pte_offset, new_pte1); 535 } 536 537 /* 9. Determine the real address from the PTE */ 538 539 raddr = ppc_hash32_pte_raddr(sr, pte, eaddr); 540 541 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 542 prot, mmu_idx, TARGET_PAGE_SIZE); 543 544 return 0; 545 } 546 547 hwaddr ppc_hash32_get_phys_page_debug(PowerPCCPU *cpu, target_ulong eaddr) 548 { 549 CPUPPCState *env = &cpu->env; 550 target_ulong sr; 551 hwaddr pte_offset; 552 ppc_hash_pte32_t pte; 553 int prot; 554 555 if (msr_dr == 0) { 556 /* Translation is off */ 557 return eaddr; 558 } 559 560 if (env->nb_BATs != 0) { 561 hwaddr raddr = ppc_hash32_bat_lookup(cpu, eaddr, 0, &prot); 562 if (raddr != -1) { 563 return raddr; 564 } 565 } 566 567 sr = env->sr[eaddr >> 28]; 568 569 if (sr & SR32_T) { 570 /* FIXME: Add suitable debug support for Direct Store segments */ 571 return -1; 572 } 573 574 pte_offset = ppc_hash32_htab_lookup(cpu, sr, eaddr, &pte); 575 if (pte_offset == -1) { 576 return -1; 577 } 578 579 return ppc_hash32_pte_raddr(sr, pte, eaddr) & TARGET_PAGE_MASK; 580 } 581