2 * PowerPC MMU, TLB and BAT emulation helpers for QEMU.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
5 * Copyright (c) 2013 David Gibson, IBM Corporation
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.
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.
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/>.
23 #include "sysemu/kvm.h"
25 #include "mmu-hash32.h"
31 # define LOG_MMU(...) qemu_log(__VA_ARGS__)
32 # define LOG_MMU_STATE(env) log_cpu_state((env), 0)
34 # define LOG_MMU(...) do { } while (0)
35 # define LOG_MMU_STATE(...) do { } while (0)
39 # define LOG_BATS(...) qemu_log(__VA_ARGS__)
41 # define LOG_BATS(...) do { } while (0)
44 struct mmu_ctx_hash32 {
45 hwaddr raddr; /* Real address */
46 hwaddr eaddr; /* Effective address */
47 int prot; /* Protection bits */
48 hwaddr hash[2]; /* Pagetable hash values */
49 target_ulong ptem; /* Virtual segment ID | API */
50 int key; /* Access key */
51 int nx; /* Non-execute area */
54 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
56 static int ppc_hash32_pp_check(int key, int pp, int nx)
60 /* Compute access rights */
83 access = PAGE_READ | PAGE_WRITE;
94 static int ppc_hash32_check_prot(int prot, int rwx)
99 if (prot & PAGE_EXEC) {
105 if (prot & PAGE_WRITE) {
111 if (prot & PAGE_READ) {
121 /* Perform BAT hit & translation */
122 static void hash32_bat_size_prot(CPUPPCState *env, target_ulong *blp,
123 int *validp, int *protp, target_ulong *BATu,
129 bl = (*BATu & BATU32_BL) << 15;
132 if (((msr_pr == 0) && (*BATu & BATU32_VS)) ||
133 ((msr_pr != 0) && (*BATu & BATU32_VP))) {
135 pp = *BATl & BATL32_PP;
137 prot = PAGE_READ | PAGE_EXEC;
148 static void hash32_bat_601_size_prot(CPUPPCState *env, target_ulong *blp,
149 int *validp, int *protp,
150 target_ulong *BATu, target_ulong *BATl)
153 int key, pp, valid, prot;
155 bl = (*BATl & BATL32_601_BL) << 17;
156 LOG_BATS("b %02x ==> bl " TARGET_FMT_lx " msk " TARGET_FMT_lx "\n",
157 (uint8_t)(*BATl & BATL32_601_BL), bl, ~bl);
159 valid = !!(*BATl & BATL32_601_V);
161 pp = *BATu & BATU32_601_PP;
163 key = !!(*BATu & BATU32_601_KS);
165 key = !!(*BATu & BATU32_601_KP);
167 prot = ppc_hash32_pp_check(key, pp, 0);
174 static int ppc_hash32_get_bat(CPUPPCState *env, struct mmu_ctx_hash32 *ctx,
175 target_ulong virtual, int rwx)
177 target_ulong *BATlt, *BATut, *BATu, *BATl;
178 target_ulong BEPIl, BEPIu, bl;
182 LOG_BATS("%s: %cBAT v " TARGET_FMT_lx "\n", __func__,
183 rwx == 2 ? 'I' : 'D', virtual);
185 BATlt = env->IBAT[1];
186 BATut = env->IBAT[0];
188 BATlt = env->DBAT[1];
189 BATut = env->DBAT[0];
191 for (i = 0; i < env->nb_BATs; i++) {
194 BEPIu = *BATu & BATU32_BEPIU;
195 BEPIl = *BATu & BATU32_BEPIL;
196 if (unlikely(env->mmu_model == POWERPC_MMU_601)) {
197 hash32_bat_601_size_prot(env, &bl, &valid, &prot, BATu, BATl);
199 hash32_bat_size_prot(env, &bl, &valid, &prot, BATu, BATl);
201 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx
202 " BATl " TARGET_FMT_lx "\n", __func__,
203 type == ACCESS_CODE ? 'I' : 'D', i, virtual, *BATu, *BATl);
204 if ((virtual & BATU32_BEPIU) == BEPIu &&
205 ((virtual & BATU32_BEPIL) & ~bl) == BEPIl) {
208 /* Get physical address */
209 ctx->raddr = (*BATl & BATL32_BRPNU) |
210 ((virtual & BATU32_BEPIL & bl) | (*BATl & BATL32_BRPNL)) |
211 (virtual & 0x0001F000);
212 /* Compute access rights */
214 ret = ppc_hash32_check_prot(ctx->prot, rwx);
216 LOG_BATS("BAT %d match: r " TARGET_FMT_plx " prot=%c%c\n",
217 i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
218 ctx->prot & PAGE_WRITE ? 'W' : '-');
225 #if defined(DEBUG_BATS)
226 if (qemu_log_enabled()) {
227 LOG_BATS("no BAT match for " TARGET_FMT_lx ":\n", virtual);
228 for (i = 0; i < 4; i++) {
231 BEPIu = *BATu & BATU32_BEPIU;
232 BEPIl = *BATu & BATU32_BEPIL;
233 bl = (*BATu & 0x00001FFC) << 15;
234 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx
235 " BATl " TARGET_FMT_lx "\n\t" TARGET_FMT_lx " "
236 TARGET_FMT_lx " " TARGET_FMT_lx "\n",
237 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
238 *BATu, *BATl, BEPIu, BEPIl, bl);
247 static int pte_check_hash32(struct mmu_ctx_hash32 *ctx, target_ulong pte0,
248 target_ulong pte1, int h, int rwx)
254 /* Check validity and table match */
255 if ((pte0 & HPTE32_V_VALID) && (h == !!(pte0 & HPTE32_V_SECONDARY))) {
256 /* Check vsid & api */
257 mmask = PTE_CHECK_MASK;
258 pp = pte1 & HPTE32_R_PP;
259 if (HPTE32_V_COMPARE(pte0, ctx->ptem)) {
260 if (ctx->raddr != (hwaddr)-1ULL) {
261 /* all matches should have equal RPN, WIMG & PP */
262 if ((ctx->raddr & mmask) != (pte1 & mmask)) {
263 qemu_log("Bad RPN/WIMG/PP\n");
267 /* Compute access rights */
268 access = ppc_hash32_pp_check(ctx->key, pp, ctx->nx);
269 /* Keep the matching PTE informations */
272 ret = ppc_hash32_check_prot(ctx->prot, rwx);
275 LOG_MMU("PTE access granted !\n");
277 /* Access right violation */
278 LOG_MMU("PTE access rejected\n");
286 static int ppc_hash32_pte_update_flags(struct mmu_ctx_hash32 *ctx, target_ulong *pte1p,
291 /* Update page flags */
292 if (!(*pte1p & HPTE32_R_R)) {
293 /* Update accessed flag */
294 *pte1p |= HPTE32_R_R;
297 if (!(*pte1p & HPTE32_R_C)) {
298 if (rwx == 1 && ret == 0) {
299 /* Update changed flag */
300 *pte1p |= HPTE32_R_C;
303 /* Force page fault for first write access */
304 ctx->prot &= ~PAGE_WRITE;
311 hwaddr get_pteg_offset32(CPUPPCState *env, hwaddr hash)
313 return (hash * HASH_PTEG_SIZE_32) & env->htab_mask;
316 /* PTE table lookup */
317 static int find_pte32(CPUPPCState *env, struct mmu_ctx_hash32 *ctx, int h,
318 int rwx, int target_page_bits)
321 target_ulong pte0, pte1;
325 ret = -1; /* No entry found */
326 pteg_off = get_pteg_offset32(env, ctx->hash[h]);
327 for (i = 0; i < HPTES_PER_GROUP; i++) {
328 pte0 = ppc_hash32_load_hpte0(env, pteg_off + i*HASH_PTE_SIZE_32);
329 pte1 = ppc_hash32_load_hpte1(env, pteg_off + i*HASH_PTE_SIZE_32);
330 r = pte_check_hash32(ctx, pte0, pte1, h, rwx);
331 LOG_MMU("Load pte from %08" HWADDR_PRIx " => " TARGET_FMT_lx " "
332 TARGET_FMT_lx " %d %d %d " TARGET_FMT_lx "\n",
333 pteg_off + (i * 8), pte0, pte1, (int)(pte0 >> 31), h,
334 (int)((pte0 >> 6) & 1), ctx->ptem);
337 /* PTE inconsistency */
340 /* Access violation */
350 /* XXX: we should go on looping to check all PTEs consistency
351 * but if we can speed-up the whole thing as the
352 * result would be undefined if PTEs are not consistent.
361 LOG_MMU("found PTE at addr %08" HWADDR_PRIx " prot=%01x ret=%d\n",
362 ctx->raddr, ctx->prot, ret);
363 /* Update page flags */
365 if (ppc_hash32_pte_update_flags(ctx, &pte1, ret, rwx) == 1) {
366 ppc_hash32_store_hpte1(env, pteg_off + good * HASH_PTE_SIZE_32,
371 /* We have a TLB that saves 4K pages, so let's
372 * split a huge page to 4k chunks */
373 if (target_page_bits != TARGET_PAGE_BITS) {
374 ctx->raddr |= (ctx->eaddr & ((1 << target_page_bits) - 1))
380 static int get_segment32(CPUPPCState *env, struct mmu_ctx_hash32 *ctx,
381 target_ulong eaddr, int rwx)
385 int ds, pr, target_page_bits;
387 target_ulong sr, pgidx;
392 sr = env->sr[eaddr >> 28];
393 ctx->key = (((sr & SR32_KP) && (pr != 0)) ||
394 ((sr & SR32_KS) && (pr == 0))) ? 1 : 0;
395 ds = !!(sr & SR32_T);
396 ctx->nx = !!(sr & SR32_NX);
397 vsid = sr & SR32_VSID;
398 target_page_bits = TARGET_PAGE_BITS;
399 LOG_MMU("Check segment v=" TARGET_FMT_lx " %d " TARGET_FMT_lx " nip="
400 TARGET_FMT_lx " lr=" TARGET_FMT_lx
401 " ir=%d dr=%d pr=%d %d\n",
402 eaddr, (int)(eaddr >> 28), sr, env->nip, env->lr, (int)msr_ir,
403 (int)msr_dr, pr != 0 ? 1 : 0, rwx);
404 pgidx = (eaddr & ~SEGMENT_MASK_256M) >> target_page_bits;
406 ctx->ptem = (vsid << 7) | (pgidx >> 10);
408 LOG_MMU("pte segment: key=%d ds %d nx %d vsid " TARGET_FMT_lx "\n",
409 ctx->key, ds, ctx->nx, vsid);
412 /* Check if instruction fetch is allowed, if needed */
413 if (rwx != 2 || ctx->nx == 0) {
414 /* Page address translation */
415 LOG_MMU("htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
416 " hash " TARGET_FMT_plx "\n",
417 env->htab_base, env->htab_mask, hash);
419 ctx->hash[1] = ~hash;
421 /* Initialize real address with an invalid value */
422 ctx->raddr = (hwaddr)-1ULL;
423 LOG_MMU("0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
424 " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
425 " hash=" TARGET_FMT_plx "\n",
426 env->htab_base, env->htab_mask, vsid, ctx->ptem,
428 /* Primary table lookup */
429 ret = find_pte32(env, ctx, 0, rwx, target_page_bits);
431 /* Secondary table lookup */
432 LOG_MMU("1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
433 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
434 " hash=" TARGET_FMT_plx "\n", env->htab_base,
435 env->htab_mask, vsid, ctx->ptem, ctx->hash[1]);
436 ret2 = find_pte32(env, ctx, 1, rwx, target_page_bits);
441 #if defined(DUMP_PAGE_TABLES)
442 if (qemu_log_enabled()) {
444 uint32_t a0, a1, a2, a3;
446 qemu_log("Page table: " TARGET_FMT_plx " len " TARGET_FMT_plx
447 "\n", sdr, mask + 0x80);
448 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
450 a0 = ldl_phys(curaddr);
451 a1 = ldl_phys(curaddr + 4);
452 a2 = ldl_phys(curaddr + 8);
453 a3 = ldl_phys(curaddr + 12);
454 if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
455 qemu_log(TARGET_FMT_plx ": %08x %08x %08x %08x\n",
456 curaddr, a0, a1, a2, a3);
462 LOG_MMU("No access allowed\n");
468 LOG_MMU("direct store...\n");
469 /* Direct-store segment : absolutely *BUGGY* for now */
471 /* Direct-store implies a 32-bit MMU.
472 * Check the Segment Register's bus unit ID (BUID).
474 sr = env->sr[eaddr >> 28];
475 if ((sr & 0x1FF00000) >> 20 == 0x07f) {
476 /* Memory-forced I/O controller interface access */
477 /* If T=1 and BUID=x'07F', the 601 performs a memory access
478 * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
480 ctx->raddr = ((sr & 0xF) << 28) | (eaddr & 0x0FFFFFFF);
481 ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
486 /* No code fetch is allowed in direct-store areas */
490 switch (env->access_type) {
492 /* Integer load/store : only access allowed */
495 /* Floating point load/store */
498 /* lwarx, ldarx or srwcx. */
501 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
502 /* Should make the instruction do no-op.
503 * As it already do no-op, it's quite easy :-)
511 qemu_log("ERROR: instruction should not need "
512 "address translation\n");
515 if ((rwx == 1 || ctx->key != 1) && (rwx == 0 || ctx->key != 0)) {
526 static int ppc_hash32_get_physical_address(CPUPPCState *env, struct mmu_ctx_hash32 *ctx,
527 target_ulong eaddr, int rwx)
529 bool real_mode = (rwx == 2 && msr_ir == 0)
530 || (rwx != 2 && msr_dr == 0);
534 ctx->prot = PAGE_READ | PAGE_EXEC | PAGE_WRITE;
539 /* Try to find a BAT */
540 if (env->nb_BATs != 0) {
541 ret = ppc_hash32_get_bat(env, ctx, eaddr, rwx);
544 /* We didn't match any BAT entry or don't have BATs */
545 ret = get_segment32(env, ctx, eaddr, rwx);
551 hwaddr ppc_hash32_get_phys_page_debug(CPUPPCState *env, target_ulong addr)
553 struct mmu_ctx_hash32 ctx;
555 /* FIXME: Will not behave sanely for direct store segments, but
556 * they're almost never used */
557 if (unlikely(ppc_hash32_get_physical_address(env, &ctx, addr, 0)
562 return ctx.raddr & TARGET_PAGE_MASK;
565 int ppc_hash32_handle_mmu_fault(CPUPPCState *env, target_ulong address, int rwx,
568 struct mmu_ctx_hash32 ctx;
571 ret = ppc_hash32_get_physical_address(env, &ctx, address, rwx);
573 tlb_set_page(env, address & TARGET_PAGE_MASK,
574 ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
575 mmu_idx, TARGET_PAGE_SIZE);
577 } else if (ret < 0) {
582 /* No matches in page tables or TLB */
583 env->exception_index = POWERPC_EXCP_ISI;
584 env->error_code = 0x40000000;
587 /* Access rights violation */
588 env->exception_index = POWERPC_EXCP_ISI;
589 env->error_code = 0x08000000;
592 /* No execute protection violation */
593 env->exception_index = POWERPC_EXCP_ISI;
594 env->error_code = 0x10000000;
597 /* Direct store exception */
598 /* No code fetch is allowed in direct-store areas */
599 env->exception_index = POWERPC_EXCP_ISI;
600 env->error_code = 0x10000000;
606 /* No matches in page tables or TLB */
607 env->exception_index = POWERPC_EXCP_DSI;
609 env->spr[SPR_DAR] = address;
611 env->spr[SPR_DSISR] = 0x42000000;
613 env->spr[SPR_DSISR] = 0x40000000;
617 /* Access rights violation */
618 env->exception_index = POWERPC_EXCP_DSI;
620 env->spr[SPR_DAR] = address;
622 env->spr[SPR_DSISR] = 0x0A000000;
624 env->spr[SPR_DSISR] = 0x08000000;
628 /* Direct store exception */
629 switch (env->access_type) {
631 /* Floating point load/store */
632 env->exception_index = POWERPC_EXCP_ALIGN;
633 env->error_code = POWERPC_EXCP_ALIGN_FP;
634 env->spr[SPR_DAR] = address;
637 /* lwarx, ldarx or stwcx. */
638 env->exception_index = POWERPC_EXCP_DSI;
640 env->spr[SPR_DAR] = address;
642 env->spr[SPR_DSISR] = 0x06000000;
644 env->spr[SPR_DSISR] = 0x04000000;
649 env->exception_index = POWERPC_EXCP_DSI;
651 env->spr[SPR_DAR] = address;
653 env->spr[SPR_DSISR] = 0x06100000;
655 env->spr[SPR_DSISR] = 0x04100000;
659 printf("DSI: invalid exception (%d)\n", ret);
660 env->exception_index = POWERPC_EXCP_PROGRAM;
662 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
663 env->spr[SPR_DAR] = address;
670 printf("%s: set exception to %d %02x\n", __func__,
671 env->exception, env->error_code);