19d7c3f4aSDavid Gibson /* 29d7c3f4aSDavid Gibson * PowerPC MMU, TLB and BAT emulation helpers for QEMU. 39d7c3f4aSDavid Gibson * 49d7c3f4aSDavid Gibson * Copyright (c) 2003-2007 Jocelyn Mayer 59d7c3f4aSDavid Gibson * Copyright (c) 2013 David Gibson, IBM Corporation 69d7c3f4aSDavid Gibson * 79d7c3f4aSDavid Gibson * This library is free software; you can redistribute it and/or 89d7c3f4aSDavid Gibson * modify it under the terms of the GNU Lesser General Public 99d7c3f4aSDavid Gibson * License as published by the Free Software Foundation; either 109d7c3f4aSDavid Gibson * version 2 of the License, or (at your option) any later version. 119d7c3f4aSDavid Gibson * 129d7c3f4aSDavid Gibson * This library is distributed in the hope that it will be useful, 139d7c3f4aSDavid Gibson * but WITHOUT ANY WARRANTY; without even the implied warranty of 149d7c3f4aSDavid Gibson * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 159d7c3f4aSDavid Gibson * Lesser General Public License for more details. 169d7c3f4aSDavid Gibson * 179d7c3f4aSDavid Gibson * You should have received a copy of the GNU Lesser General Public 189d7c3f4aSDavid Gibson * License along with this library; if not, see <http://www.gnu.org/licenses/>. 199d7c3f4aSDavid Gibson */ 209d7c3f4aSDavid Gibson 219d7c3f4aSDavid Gibson #include "cpu.h" 229d7c3f4aSDavid Gibson #include "helper.h" 239d7c3f4aSDavid Gibson #include "sysemu/kvm.h" 249d7c3f4aSDavid Gibson #include "kvm_ppc.h" 259d7c3f4aSDavid Gibson #include "mmu-hash32.h" 269d7c3f4aSDavid Gibson 279d7c3f4aSDavid Gibson //#define DEBUG_MMU 2898132796SDavid Gibson //#define DEBUG_BAT 299d7c3f4aSDavid Gibson 309d7c3f4aSDavid Gibson #ifdef DEBUG_MMU 319d7c3f4aSDavid Gibson # define LOG_MMU(...) qemu_log(__VA_ARGS__) 329d7c3f4aSDavid Gibson # define LOG_MMU_STATE(env) log_cpu_state((env), 0) 339d7c3f4aSDavid Gibson #else 349d7c3f4aSDavid Gibson # define LOG_MMU(...) do { } while (0) 359d7c3f4aSDavid Gibson # define LOG_MMU_STATE(...) do { } while (0) 369d7c3f4aSDavid Gibson #endif 379d7c3f4aSDavid Gibson 3898132796SDavid Gibson #ifdef DEBUG_BATS 3998132796SDavid Gibson # define LOG_BATS(...) qemu_log(__VA_ARGS__) 4098132796SDavid Gibson #else 4198132796SDavid Gibson # define LOG_BATS(...) do { } while (0) 4298132796SDavid Gibson #endif 4398132796SDavid Gibson 445dc68eb0SDavid Gibson struct mmu_ctx_hash32 { 455dc68eb0SDavid Gibson hwaddr raddr; /* Real address */ 465dc68eb0SDavid Gibson hwaddr eaddr; /* Effective address */ 475dc68eb0SDavid Gibson int prot; /* Protection bits */ 485dc68eb0SDavid Gibson hwaddr hash[2]; /* Pagetable hash values */ 495dc68eb0SDavid Gibson target_ulong ptem; /* Virtual segment ID | API */ 505dc68eb0SDavid Gibson int key; /* Access key */ 515dc68eb0SDavid Gibson int nx; /* Non-execute area */ 525dc68eb0SDavid Gibson }; 535dc68eb0SDavid Gibson 549d7c3f4aSDavid Gibson #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B) 559d7c3f4aSDavid Gibson 56496272a7SDavid Gibson static int ppc_hash32_pp_check(int key, int pp, int nx) 57496272a7SDavid Gibson { 58496272a7SDavid Gibson int access; 59496272a7SDavid Gibson 60496272a7SDavid Gibson /* Compute access rights */ 61496272a7SDavid Gibson access = 0; 62496272a7SDavid Gibson if (key == 0) { 63496272a7SDavid Gibson switch (pp) { 64496272a7SDavid Gibson case 0x0: 65496272a7SDavid Gibson case 0x1: 66496272a7SDavid Gibson case 0x2: 67496272a7SDavid Gibson access |= PAGE_WRITE; 68496272a7SDavid Gibson /* No break here */ 69496272a7SDavid Gibson case 0x3: 70496272a7SDavid Gibson access |= PAGE_READ; 71496272a7SDavid Gibson break; 72496272a7SDavid Gibson } 73496272a7SDavid Gibson } else { 74496272a7SDavid Gibson switch (pp) { 75496272a7SDavid Gibson case 0x0: 76496272a7SDavid Gibson access = 0; 77496272a7SDavid Gibson break; 78496272a7SDavid Gibson case 0x1: 79496272a7SDavid Gibson case 0x3: 80496272a7SDavid Gibson access = PAGE_READ; 81496272a7SDavid Gibson break; 82496272a7SDavid Gibson case 0x2: 83496272a7SDavid Gibson access = PAGE_READ | PAGE_WRITE; 84496272a7SDavid Gibson break; 85496272a7SDavid Gibson } 86496272a7SDavid Gibson } 87496272a7SDavid Gibson if (nx == 0) { 88496272a7SDavid Gibson access |= PAGE_EXEC; 89496272a7SDavid Gibson } 90496272a7SDavid Gibson 91496272a7SDavid Gibson return access; 92496272a7SDavid Gibson } 93496272a7SDavid Gibson 94496272a7SDavid Gibson static int ppc_hash32_check_prot(int prot, int rw, int access_type) 95496272a7SDavid Gibson { 96496272a7SDavid Gibson int ret; 97496272a7SDavid Gibson 98496272a7SDavid Gibson if (access_type == ACCESS_CODE) { 99496272a7SDavid Gibson if (prot & PAGE_EXEC) { 100496272a7SDavid Gibson ret = 0; 101496272a7SDavid Gibson } else { 102496272a7SDavid Gibson ret = -2; 103496272a7SDavid Gibson } 104496272a7SDavid Gibson } else if (rw) { 105496272a7SDavid Gibson if (prot & PAGE_WRITE) { 106496272a7SDavid Gibson ret = 0; 107496272a7SDavid Gibson } else { 108496272a7SDavid Gibson ret = -2; 109496272a7SDavid Gibson } 110496272a7SDavid Gibson } else { 111496272a7SDavid Gibson if (prot & PAGE_READ) { 112496272a7SDavid Gibson ret = 0; 113496272a7SDavid Gibson } else { 114496272a7SDavid Gibson ret = -2; 115496272a7SDavid Gibson } 116496272a7SDavid Gibson } 117496272a7SDavid Gibson 118496272a7SDavid Gibson return ret; 119496272a7SDavid Gibson } 120496272a7SDavid Gibson 12198132796SDavid Gibson /* Perform BAT hit & translation */ 12298132796SDavid Gibson static void hash32_bat_size_prot(CPUPPCState *env, target_ulong *blp, 12398132796SDavid Gibson int *validp, int *protp, target_ulong *BATu, 12498132796SDavid Gibson target_ulong *BATl) 12598132796SDavid Gibson { 12698132796SDavid Gibson target_ulong bl; 12798132796SDavid Gibson int pp, valid, prot; 12898132796SDavid Gibson 129d5aea6f3SDavid Gibson bl = (*BATu & BATU32_BL) << 15; 13098132796SDavid Gibson valid = 0; 13198132796SDavid Gibson prot = 0; 132d5aea6f3SDavid Gibson if (((msr_pr == 0) && (*BATu & BATU32_VS)) || 133d5aea6f3SDavid Gibson ((msr_pr != 0) && (*BATu & BATU32_VP))) { 13498132796SDavid Gibson valid = 1; 135d5aea6f3SDavid Gibson pp = *BATl & BATL32_PP; 13698132796SDavid Gibson if (pp != 0) { 13798132796SDavid Gibson prot = PAGE_READ | PAGE_EXEC; 13898132796SDavid Gibson if (pp == 0x2) { 13998132796SDavid Gibson prot |= PAGE_WRITE; 14098132796SDavid Gibson } 14198132796SDavid Gibson } 14298132796SDavid Gibson } 14398132796SDavid Gibson *blp = bl; 14498132796SDavid Gibson *validp = valid; 14598132796SDavid Gibson *protp = prot; 14698132796SDavid Gibson } 14798132796SDavid Gibson 14898132796SDavid Gibson static void hash32_bat_601_size_prot(CPUPPCState *env, target_ulong *blp, 14998132796SDavid Gibson int *validp, int *protp, 15098132796SDavid Gibson target_ulong *BATu, target_ulong *BATl) 15198132796SDavid Gibson { 15298132796SDavid Gibson target_ulong bl; 15398132796SDavid Gibson int key, pp, valid, prot; 15498132796SDavid Gibson 155d5aea6f3SDavid Gibson bl = (*BATl & BATL32_601_BL) << 17; 15698132796SDavid Gibson LOG_BATS("b %02x ==> bl " TARGET_FMT_lx " msk " TARGET_FMT_lx "\n", 157d5aea6f3SDavid Gibson (uint8_t)(*BATl & BATL32_601_BL), bl, ~bl); 15898132796SDavid Gibson prot = 0; 159d5aea6f3SDavid Gibson valid = !!(*BATl & BATL32_601_V); 16098132796SDavid Gibson if (valid) { 161d5aea6f3SDavid Gibson pp = *BATu & BATU32_601_PP; 16298132796SDavid Gibson if (msr_pr == 0) { 163d5aea6f3SDavid Gibson key = !!(*BATu & BATU32_601_KS); 16498132796SDavid Gibson } else { 165d5aea6f3SDavid Gibson key = !!(*BATu & BATU32_601_KP); 16698132796SDavid Gibson } 16798132796SDavid Gibson prot = ppc_hash32_pp_check(key, pp, 0); 16898132796SDavid Gibson } 16998132796SDavid Gibson *blp = bl; 17098132796SDavid Gibson *validp = valid; 17198132796SDavid Gibson *protp = prot; 17298132796SDavid Gibson } 17398132796SDavid Gibson 1745dc68eb0SDavid Gibson static int ppc_hash32_get_bat(CPUPPCState *env, struct mmu_ctx_hash32 *ctx, 17598132796SDavid Gibson target_ulong virtual, int rw, int type) 17698132796SDavid Gibson { 17798132796SDavid Gibson target_ulong *BATlt, *BATut, *BATu, *BATl; 17898132796SDavid Gibson target_ulong BEPIl, BEPIu, bl; 17998132796SDavid Gibson int i, valid, prot; 18098132796SDavid Gibson int ret = -1; 18198132796SDavid Gibson 18298132796SDavid Gibson LOG_BATS("%s: %cBAT v " TARGET_FMT_lx "\n", __func__, 18398132796SDavid Gibson type == ACCESS_CODE ? 'I' : 'D', virtual); 18498132796SDavid Gibson switch (type) { 18598132796SDavid Gibson case ACCESS_CODE: 18698132796SDavid Gibson BATlt = env->IBAT[1]; 18798132796SDavid Gibson BATut = env->IBAT[0]; 18898132796SDavid Gibson break; 18998132796SDavid Gibson default: 19098132796SDavid Gibson BATlt = env->DBAT[1]; 19198132796SDavid Gibson BATut = env->DBAT[0]; 19298132796SDavid Gibson break; 19398132796SDavid Gibson } 19498132796SDavid Gibson for (i = 0; i < env->nb_BATs; i++) { 19598132796SDavid Gibson BATu = &BATut[i]; 19698132796SDavid Gibson BATl = &BATlt[i]; 197d5aea6f3SDavid Gibson BEPIu = *BATu & BATU32_BEPIU; 198d5aea6f3SDavid Gibson BEPIl = *BATu & BATU32_BEPIL; 19998132796SDavid Gibson if (unlikely(env->mmu_model == POWERPC_MMU_601)) { 20098132796SDavid Gibson hash32_bat_601_size_prot(env, &bl, &valid, &prot, BATu, BATl); 20198132796SDavid Gibson } else { 20298132796SDavid Gibson hash32_bat_size_prot(env, &bl, &valid, &prot, BATu, BATl); 20398132796SDavid Gibson } 20498132796SDavid Gibson LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx 20598132796SDavid Gibson " BATl " TARGET_FMT_lx "\n", __func__, 20698132796SDavid Gibson type == ACCESS_CODE ? 'I' : 'D', i, virtual, *BATu, *BATl); 207d5aea6f3SDavid Gibson if ((virtual & BATU32_BEPIU) == BEPIu && 208d5aea6f3SDavid Gibson ((virtual & BATU32_BEPIL) & ~bl) == BEPIl) { 20998132796SDavid Gibson /* BAT matches */ 21098132796SDavid Gibson if (valid != 0) { 21198132796SDavid Gibson /* Get physical address */ 212d5aea6f3SDavid Gibson ctx->raddr = (*BATl & BATL32_BRPNU) | 213d5aea6f3SDavid Gibson ((virtual & BATU32_BEPIL & bl) | (*BATl & BATL32_BRPNL)) | 21498132796SDavid Gibson (virtual & 0x0001F000); 21598132796SDavid Gibson /* Compute access rights */ 21698132796SDavid Gibson ctx->prot = prot; 21798132796SDavid Gibson ret = ppc_hash32_check_prot(ctx->prot, rw, type); 21898132796SDavid Gibson if (ret == 0) { 21998132796SDavid Gibson LOG_BATS("BAT %d match: r " TARGET_FMT_plx " prot=%c%c\n", 22098132796SDavid Gibson i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-', 22198132796SDavid Gibson ctx->prot & PAGE_WRITE ? 'W' : '-'); 22298132796SDavid Gibson } 22398132796SDavid Gibson break; 22498132796SDavid Gibson } 22598132796SDavid Gibson } 22698132796SDavid Gibson } 22798132796SDavid Gibson if (ret < 0) { 22898132796SDavid Gibson #if defined(DEBUG_BATS) 22998132796SDavid Gibson if (qemu_log_enabled()) { 23098132796SDavid Gibson LOG_BATS("no BAT match for " TARGET_FMT_lx ":\n", virtual); 23198132796SDavid Gibson for (i = 0; i < 4; i++) { 23298132796SDavid Gibson BATu = &BATut[i]; 23398132796SDavid Gibson BATl = &BATlt[i]; 234d5aea6f3SDavid Gibson BEPIu = *BATu & BATU32_BEPIU; 235d5aea6f3SDavid Gibson BEPIl = *BATu & BATU32_BEPIL; 23698132796SDavid Gibson bl = (*BATu & 0x00001FFC) << 15; 23798132796SDavid Gibson LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx 23898132796SDavid Gibson " BATl " TARGET_FMT_lx "\n\t" TARGET_FMT_lx " " 23998132796SDavid Gibson TARGET_FMT_lx " " TARGET_FMT_lx "\n", 24098132796SDavid Gibson __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual, 24198132796SDavid Gibson *BATu, *BATl, BEPIu, BEPIl, bl); 24298132796SDavid Gibson } 24398132796SDavid Gibson } 24498132796SDavid Gibson #endif 24598132796SDavid Gibson } 24698132796SDavid Gibson /* No hit */ 24798132796SDavid Gibson return ret; 24898132796SDavid Gibson } 24998132796SDavid Gibson 2505dc68eb0SDavid Gibson static int pte_check_hash32(struct mmu_ctx_hash32 *ctx, target_ulong pte0, 2519d7c3f4aSDavid Gibson target_ulong pte1, int h, int rw, int type) 2529d7c3f4aSDavid Gibson { 253d5aea6f3SDavid Gibson target_ulong mmask; 254d5aea6f3SDavid Gibson int access, ret, pp; 2559d7c3f4aSDavid Gibson 2569d7c3f4aSDavid Gibson ret = -1; 2579d7c3f4aSDavid Gibson /* Check validity and table match */ 258d5aea6f3SDavid Gibson if ((pte0 & HPTE32_V_VALID) && (h == !!(pte0 & HPTE32_V_SECONDARY))) { 2599d7c3f4aSDavid Gibson /* Check vsid & api */ 2609d7c3f4aSDavid Gibson mmask = PTE_CHECK_MASK; 261d5aea6f3SDavid Gibson pp = pte1 & HPTE32_R_PP; 262d5aea6f3SDavid Gibson if (HPTE32_V_COMPARE(pte0, ctx->ptem)) { 2639d7c3f4aSDavid Gibson if (ctx->raddr != (hwaddr)-1ULL) { 2649d7c3f4aSDavid Gibson /* all matches should have equal RPN, WIMG & PP */ 2659d7c3f4aSDavid Gibson if ((ctx->raddr & mmask) != (pte1 & mmask)) { 2669d7c3f4aSDavid Gibson qemu_log("Bad RPN/WIMG/PP\n"); 2679d7c3f4aSDavid Gibson return -3; 2689d7c3f4aSDavid Gibson } 2699d7c3f4aSDavid Gibson } 2709d7c3f4aSDavid Gibson /* Compute access rights */ 271496272a7SDavid Gibson access = ppc_hash32_pp_check(ctx->key, pp, ctx->nx); 2729d7c3f4aSDavid Gibson /* Keep the matching PTE informations */ 2739d7c3f4aSDavid Gibson ctx->raddr = pte1; 2749d7c3f4aSDavid Gibson ctx->prot = access; 275496272a7SDavid Gibson ret = ppc_hash32_check_prot(ctx->prot, rw, type); 2769d7c3f4aSDavid Gibson if (ret == 0) { 2779d7c3f4aSDavid Gibson /* Access granted */ 2789d7c3f4aSDavid Gibson LOG_MMU("PTE access granted !\n"); 2799d7c3f4aSDavid Gibson } else { 2809d7c3f4aSDavid Gibson /* Access right violation */ 2819d7c3f4aSDavid Gibson LOG_MMU("PTE access rejected\n"); 2829d7c3f4aSDavid Gibson } 2839d7c3f4aSDavid Gibson } 2849d7c3f4aSDavid Gibson } 2859d7c3f4aSDavid Gibson 2869d7c3f4aSDavid Gibson return ret; 2879d7c3f4aSDavid Gibson } 288c69b6151SDavid Gibson 2895dc68eb0SDavid Gibson static int ppc_hash32_pte_update_flags(struct mmu_ctx_hash32 *ctx, target_ulong *pte1p, 290496272a7SDavid Gibson int ret, int rw) 291496272a7SDavid Gibson { 292496272a7SDavid Gibson int store = 0; 293496272a7SDavid Gibson 294496272a7SDavid Gibson /* Update page flags */ 295d5aea6f3SDavid Gibson if (!(*pte1p & HPTE32_R_R)) { 296496272a7SDavid Gibson /* Update accessed flag */ 297d5aea6f3SDavid Gibson *pte1p |= HPTE32_R_R; 298496272a7SDavid Gibson store = 1; 299496272a7SDavid Gibson } 300d5aea6f3SDavid Gibson if (!(*pte1p & HPTE32_R_C)) { 301496272a7SDavid Gibson if (rw == 1 && ret == 0) { 302496272a7SDavid Gibson /* Update changed flag */ 303d5aea6f3SDavid Gibson *pte1p |= HPTE32_R_C; 304496272a7SDavid Gibson store = 1; 305496272a7SDavid Gibson } else { 306496272a7SDavid Gibson /* Force page fault for first write access */ 307496272a7SDavid Gibson ctx->prot &= ~PAGE_WRITE; 308496272a7SDavid Gibson } 309496272a7SDavid Gibson } 310496272a7SDavid Gibson 311496272a7SDavid Gibson return store; 312496272a7SDavid Gibson } 313496272a7SDavid Gibson 31459191721SDavid Gibson hwaddr get_pteg_offset32(CPUPPCState *env, hwaddr hash) 31559191721SDavid Gibson { 316d5aea6f3SDavid Gibson return (hash * HASH_PTEG_SIZE_32) & env->htab_mask; 31759191721SDavid Gibson } 31859191721SDavid Gibson 319c69b6151SDavid Gibson /* PTE table lookup */ 3205dc68eb0SDavid Gibson static int find_pte32(CPUPPCState *env, struct mmu_ctx_hash32 *ctx, int h, 321c69b6151SDavid Gibson int rw, int type, int target_page_bits) 322c69b6151SDavid Gibson { 323c69b6151SDavid Gibson hwaddr pteg_off; 324c69b6151SDavid Gibson target_ulong pte0, pte1; 325c69b6151SDavid Gibson int i, good = -1; 326c69b6151SDavid Gibson int ret, r; 327c69b6151SDavid Gibson 328c69b6151SDavid Gibson ret = -1; /* No entry found */ 32959191721SDavid Gibson pteg_off = get_pteg_offset32(env, ctx->hash[h]); 330d5aea6f3SDavid Gibson for (i = 0; i < HPTES_PER_GROUP; i++) { 331dffdaf61SDavid Gibson pte0 = ppc_hash32_load_hpte0(env, pteg_off + i*HASH_PTE_SIZE_32); 332dffdaf61SDavid Gibson pte1 = ppc_hash32_load_hpte1(env, pteg_off + i*HASH_PTE_SIZE_32); 333c69b6151SDavid Gibson r = pte_check_hash32(ctx, pte0, pte1, h, rw, type); 334c69b6151SDavid Gibson LOG_MMU("Load pte from %08" HWADDR_PRIx " => " TARGET_FMT_lx " " 335c69b6151SDavid Gibson TARGET_FMT_lx " %d %d %d " TARGET_FMT_lx "\n", 336c69b6151SDavid Gibson pteg_off + (i * 8), pte0, pte1, (int)(pte0 >> 31), h, 337c69b6151SDavid Gibson (int)((pte0 >> 6) & 1), ctx->ptem); 338c69b6151SDavid Gibson switch (r) { 339c69b6151SDavid Gibson case -3: 340c69b6151SDavid Gibson /* PTE inconsistency */ 341c69b6151SDavid Gibson return -1; 342c69b6151SDavid Gibson case -2: 343c69b6151SDavid Gibson /* Access violation */ 344c69b6151SDavid Gibson ret = -2; 345c69b6151SDavid Gibson good = i; 346c69b6151SDavid Gibson break; 347c69b6151SDavid Gibson case -1: 348c69b6151SDavid Gibson default: 349c69b6151SDavid Gibson /* No PTE match */ 350c69b6151SDavid Gibson break; 351c69b6151SDavid Gibson case 0: 352c69b6151SDavid Gibson /* access granted */ 353c69b6151SDavid Gibson /* XXX: we should go on looping to check all PTEs consistency 354c69b6151SDavid Gibson * but if we can speed-up the whole thing as the 355c69b6151SDavid Gibson * result would be undefined if PTEs are not consistent. 356c69b6151SDavid Gibson */ 357c69b6151SDavid Gibson ret = 0; 358c69b6151SDavid Gibson good = i; 359c69b6151SDavid Gibson goto done; 360c69b6151SDavid Gibson } 361c69b6151SDavid Gibson } 362c69b6151SDavid Gibson if (good != -1) { 363c69b6151SDavid Gibson done: 364c69b6151SDavid Gibson LOG_MMU("found PTE at addr %08" HWADDR_PRIx " prot=%01x ret=%d\n", 365c69b6151SDavid Gibson ctx->raddr, ctx->prot, ret); 366c69b6151SDavid Gibson /* Update page flags */ 367c69b6151SDavid Gibson pte1 = ctx->raddr; 368496272a7SDavid Gibson if (ppc_hash32_pte_update_flags(ctx, &pte1, ret, rw) == 1) { 369dffdaf61SDavid Gibson ppc_hash32_store_hpte1(env, pteg_off + good * HASH_PTE_SIZE_32, 370c69b6151SDavid Gibson pte1); 371c69b6151SDavid Gibson } 372c69b6151SDavid Gibson } 373c69b6151SDavid Gibson 374c69b6151SDavid Gibson /* We have a TLB that saves 4K pages, so let's 375c69b6151SDavid Gibson * split a huge page to 4k chunks */ 376c69b6151SDavid Gibson if (target_page_bits != TARGET_PAGE_BITS) { 377c69b6151SDavid Gibson ctx->raddr |= (ctx->eaddr & ((1 << target_page_bits) - 1)) 378c69b6151SDavid Gibson & TARGET_PAGE_MASK; 379c69b6151SDavid Gibson } 380c69b6151SDavid Gibson return ret; 381c69b6151SDavid Gibson } 3820480884fSDavid Gibson 3835dc68eb0SDavid Gibson static int get_segment32(CPUPPCState *env, struct mmu_ctx_hash32 *ctx, 3840480884fSDavid Gibson target_ulong eaddr, int rw, int type) 3850480884fSDavid Gibson { 3860480884fSDavid Gibson hwaddr hash; 3870480884fSDavid Gibson target_ulong vsid; 3880480884fSDavid Gibson int ds, pr, target_page_bits; 3890480884fSDavid Gibson int ret, ret2; 3900480884fSDavid Gibson target_ulong sr, pgidx; 3910480884fSDavid Gibson 3920480884fSDavid Gibson pr = msr_pr; 3930480884fSDavid Gibson ctx->eaddr = eaddr; 3940480884fSDavid Gibson 3950480884fSDavid Gibson sr = env->sr[eaddr >> 28]; 396d5aea6f3SDavid Gibson ctx->key = (((sr & SR32_KP) && (pr != 0)) || 397d5aea6f3SDavid Gibson ((sr & SR32_KS) && (pr == 0))) ? 1 : 0; 398d5aea6f3SDavid Gibson ds = !!(sr & SR32_T); 399d5aea6f3SDavid Gibson ctx->nx = !!(sr & SR32_NX); 400d5aea6f3SDavid Gibson vsid = sr & SR32_VSID; 4010480884fSDavid Gibson target_page_bits = TARGET_PAGE_BITS; 4020480884fSDavid Gibson LOG_MMU("Check segment v=" TARGET_FMT_lx " %d " TARGET_FMT_lx " nip=" 4030480884fSDavid Gibson TARGET_FMT_lx " lr=" TARGET_FMT_lx 4040480884fSDavid Gibson " ir=%d dr=%d pr=%d %d t=%d\n", 4050480884fSDavid Gibson eaddr, (int)(eaddr >> 28), sr, env->nip, env->lr, (int)msr_ir, 4060480884fSDavid Gibson (int)msr_dr, pr != 0 ? 1 : 0, rw, type); 4070480884fSDavid Gibson pgidx = (eaddr & ~SEGMENT_MASK_256M) >> target_page_bits; 4080480884fSDavid Gibson hash = vsid ^ pgidx; 4090480884fSDavid Gibson ctx->ptem = (vsid << 7) | (pgidx >> 10); 4100480884fSDavid Gibson 4110480884fSDavid Gibson LOG_MMU("pte segment: key=%d ds %d nx %d vsid " TARGET_FMT_lx "\n", 4120480884fSDavid Gibson ctx->key, ds, ctx->nx, vsid); 4130480884fSDavid Gibson ret = -1; 4140480884fSDavid Gibson if (!ds) { 4150480884fSDavid Gibson /* Check if instruction fetch is allowed, if needed */ 4160480884fSDavid Gibson if (type != ACCESS_CODE || ctx->nx == 0) { 4170480884fSDavid Gibson /* Page address translation */ 4180480884fSDavid Gibson LOG_MMU("htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx 4190480884fSDavid Gibson " hash " TARGET_FMT_plx "\n", 4200480884fSDavid Gibson env->htab_base, env->htab_mask, hash); 4210480884fSDavid Gibson ctx->hash[0] = hash; 4220480884fSDavid Gibson ctx->hash[1] = ~hash; 4230480884fSDavid Gibson 4240480884fSDavid Gibson /* Initialize real address with an invalid value */ 4250480884fSDavid Gibson ctx->raddr = (hwaddr)-1ULL; 4260480884fSDavid Gibson LOG_MMU("0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 4270480884fSDavid Gibson " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx 4280480884fSDavid Gibson " hash=" TARGET_FMT_plx "\n", 4290480884fSDavid Gibson env->htab_base, env->htab_mask, vsid, ctx->ptem, 4300480884fSDavid Gibson ctx->hash[0]); 4310480884fSDavid Gibson /* Primary table lookup */ 4320480884fSDavid Gibson ret = find_pte32(env, ctx, 0, rw, type, target_page_bits); 4330480884fSDavid Gibson if (ret < 0) { 4340480884fSDavid Gibson /* Secondary table lookup */ 4350480884fSDavid Gibson LOG_MMU("1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 4360480884fSDavid Gibson " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx 4370480884fSDavid Gibson " hash=" TARGET_FMT_plx "\n", env->htab_base, 4380480884fSDavid Gibson env->htab_mask, vsid, ctx->ptem, ctx->hash[1]); 4390480884fSDavid Gibson ret2 = find_pte32(env, ctx, 1, rw, type, 4400480884fSDavid Gibson target_page_bits); 4410480884fSDavid Gibson if (ret2 != -1) { 4420480884fSDavid Gibson ret = ret2; 4430480884fSDavid Gibson } 4440480884fSDavid Gibson } 4450480884fSDavid Gibson #if defined(DUMP_PAGE_TABLES) 4460480884fSDavid Gibson if (qemu_log_enabled()) { 4470480884fSDavid Gibson hwaddr curaddr; 4480480884fSDavid Gibson uint32_t a0, a1, a2, a3; 4490480884fSDavid Gibson 4500480884fSDavid Gibson qemu_log("Page table: " TARGET_FMT_plx " len " TARGET_FMT_plx 4510480884fSDavid Gibson "\n", sdr, mask + 0x80); 4520480884fSDavid Gibson for (curaddr = sdr; curaddr < (sdr + mask + 0x80); 4530480884fSDavid Gibson curaddr += 16) { 4540480884fSDavid Gibson a0 = ldl_phys(curaddr); 4550480884fSDavid Gibson a1 = ldl_phys(curaddr + 4); 4560480884fSDavid Gibson a2 = ldl_phys(curaddr + 8); 4570480884fSDavid Gibson a3 = ldl_phys(curaddr + 12); 4580480884fSDavid Gibson if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) { 4590480884fSDavid Gibson qemu_log(TARGET_FMT_plx ": %08x %08x %08x %08x\n", 4600480884fSDavid Gibson curaddr, a0, a1, a2, a3); 4610480884fSDavid Gibson } 4620480884fSDavid Gibson } 4630480884fSDavid Gibson } 4640480884fSDavid Gibson #endif 4650480884fSDavid Gibson } else { 4660480884fSDavid Gibson LOG_MMU("No access allowed\n"); 4670480884fSDavid Gibson ret = -3; 4680480884fSDavid Gibson } 4690480884fSDavid Gibson } else { 4700480884fSDavid Gibson target_ulong sr; 4710480884fSDavid Gibson 4720480884fSDavid Gibson LOG_MMU("direct store...\n"); 4730480884fSDavid Gibson /* Direct-store segment : absolutely *BUGGY* for now */ 4740480884fSDavid Gibson 4750480884fSDavid Gibson /* Direct-store implies a 32-bit MMU. 4760480884fSDavid Gibson * Check the Segment Register's bus unit ID (BUID). 4770480884fSDavid Gibson */ 4780480884fSDavid Gibson sr = env->sr[eaddr >> 28]; 4790480884fSDavid Gibson if ((sr & 0x1FF00000) >> 20 == 0x07f) { 4800480884fSDavid Gibson /* Memory-forced I/O controller interface access */ 4810480884fSDavid Gibson /* If T=1 and BUID=x'07F', the 601 performs a memory access 4820480884fSDavid Gibson * to SR[28-31] LA[4-31], bypassing all protection mechanisms. 4830480884fSDavid Gibson */ 4840480884fSDavid Gibson ctx->raddr = ((sr & 0xF) << 28) | (eaddr & 0x0FFFFFFF); 4850480884fSDavid Gibson ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 4860480884fSDavid Gibson return 0; 4870480884fSDavid Gibson } 4880480884fSDavid Gibson 4890480884fSDavid Gibson switch (type) { 4900480884fSDavid Gibson case ACCESS_INT: 4910480884fSDavid Gibson /* Integer load/store : only access allowed */ 4920480884fSDavid Gibson break; 4930480884fSDavid Gibson case ACCESS_CODE: 4940480884fSDavid Gibson /* No code fetch is allowed in direct-store areas */ 4950480884fSDavid Gibson return -4; 4960480884fSDavid Gibson case ACCESS_FLOAT: 4970480884fSDavid Gibson /* Floating point load/store */ 4980480884fSDavid Gibson return -4; 4990480884fSDavid Gibson case ACCESS_RES: 5000480884fSDavid Gibson /* lwarx, ldarx or srwcx. */ 5010480884fSDavid Gibson return -4; 5020480884fSDavid Gibson case ACCESS_CACHE: 5030480884fSDavid Gibson /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */ 5040480884fSDavid Gibson /* Should make the instruction do no-op. 5050480884fSDavid Gibson * As it already do no-op, it's quite easy :-) 5060480884fSDavid Gibson */ 5070480884fSDavid Gibson ctx->raddr = eaddr; 5080480884fSDavid Gibson return 0; 5090480884fSDavid Gibson case ACCESS_EXT: 5100480884fSDavid Gibson /* eciwx or ecowx */ 5110480884fSDavid Gibson return -4; 5120480884fSDavid Gibson default: 5130480884fSDavid Gibson qemu_log("ERROR: instruction should not need " 5140480884fSDavid Gibson "address translation\n"); 5150480884fSDavid Gibson return -4; 5160480884fSDavid Gibson } 5170480884fSDavid Gibson if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) { 5180480884fSDavid Gibson ctx->raddr = eaddr; 5190480884fSDavid Gibson ret = 2; 5200480884fSDavid Gibson } else { 5210480884fSDavid Gibson ret = -2; 5220480884fSDavid Gibson } 5230480884fSDavid Gibson } 5240480884fSDavid Gibson 5250480884fSDavid Gibson return ret; 5260480884fSDavid Gibson } 527629bd516SDavid Gibson 5285dc68eb0SDavid Gibson static int ppc_hash32_get_physical_address(CPUPPCState *env, struct mmu_ctx_hash32 *ctx, 529f2ad6be8SDavid Gibson target_ulong eaddr, int rw, 530f2ad6be8SDavid Gibson int access_type) 531629bd516SDavid Gibson { 532629bd516SDavid Gibson bool real_mode = (access_type == ACCESS_CODE && msr_ir == 0) 533629bd516SDavid Gibson || (access_type != ACCESS_CODE && msr_dr == 0); 534629bd516SDavid Gibson 535629bd516SDavid Gibson if (real_mode) { 536629bd516SDavid Gibson ctx->raddr = eaddr; 537629bd516SDavid Gibson ctx->prot = PAGE_READ | PAGE_EXEC | PAGE_WRITE; 538629bd516SDavid Gibson return 0; 539629bd516SDavid Gibson } else { 540629bd516SDavid Gibson int ret = -1; 541629bd516SDavid Gibson 542629bd516SDavid Gibson /* Try to find a BAT */ 543629bd516SDavid Gibson if (env->nb_BATs != 0) { 54498132796SDavid Gibson ret = ppc_hash32_get_bat(env, ctx, eaddr, rw, access_type); 545629bd516SDavid Gibson } 546629bd516SDavid Gibson if (ret < 0) { 547629bd516SDavid Gibson /* We didn't match any BAT entry or don't have BATs */ 548629bd516SDavid Gibson ret = get_segment32(env, ctx, eaddr, rw, access_type); 549629bd516SDavid Gibson } 550629bd516SDavid Gibson return ret; 551629bd516SDavid Gibson } 552629bd516SDavid Gibson } 55325de24abSDavid Gibson 554f2ad6be8SDavid Gibson hwaddr ppc_hash32_get_phys_page_debug(CPUPPCState *env, target_ulong addr) 555f2ad6be8SDavid Gibson { 5565dc68eb0SDavid Gibson struct mmu_ctx_hash32 ctx; 557f2ad6be8SDavid Gibson 558f2ad6be8SDavid Gibson if (unlikely(ppc_hash32_get_physical_address(env, &ctx, addr, 0, ACCESS_INT) 559f2ad6be8SDavid Gibson != 0)) { 560f2ad6be8SDavid Gibson return -1; 561f2ad6be8SDavid Gibson } 562f2ad6be8SDavid Gibson 563f2ad6be8SDavid Gibson return ctx.raddr & TARGET_PAGE_MASK; 564f2ad6be8SDavid Gibson } 565f2ad6be8SDavid Gibson 56625de24abSDavid Gibson int ppc_hash32_handle_mmu_fault(CPUPPCState *env, target_ulong address, int rw, 56725de24abSDavid Gibson int mmu_idx) 56825de24abSDavid Gibson { 5695dc68eb0SDavid Gibson struct mmu_ctx_hash32 ctx; 57025de24abSDavid Gibson int access_type; 57125de24abSDavid Gibson int ret = 0; 57225de24abSDavid Gibson 57325de24abSDavid Gibson if (rw == 2) { 57425de24abSDavid Gibson /* code access */ 57525de24abSDavid Gibson rw = 0; 57625de24abSDavid Gibson access_type = ACCESS_CODE; 57725de24abSDavid Gibson } else { 57825de24abSDavid Gibson /* data access */ 57925de24abSDavid Gibson access_type = env->access_type; 58025de24abSDavid Gibson } 58125de24abSDavid Gibson ret = ppc_hash32_get_physical_address(env, &ctx, address, rw, access_type); 58225de24abSDavid Gibson if (ret == 0) { 58325de24abSDavid Gibson tlb_set_page(env, address & TARGET_PAGE_MASK, 58425de24abSDavid Gibson ctx.raddr & TARGET_PAGE_MASK, ctx.prot, 58525de24abSDavid Gibson mmu_idx, TARGET_PAGE_SIZE); 58625de24abSDavid Gibson ret = 0; 58725de24abSDavid Gibson } else if (ret < 0) { 58825de24abSDavid Gibson LOG_MMU_STATE(env); 58925de24abSDavid Gibson if (access_type == ACCESS_CODE) { 59025de24abSDavid Gibson switch (ret) { 59125de24abSDavid Gibson case -1: 59225de24abSDavid Gibson /* No matches in page tables or TLB */ 59325de24abSDavid Gibson env->exception_index = POWERPC_EXCP_ISI; 59425de24abSDavid Gibson env->error_code = 0x40000000; 59525de24abSDavid Gibson break; 59625de24abSDavid Gibson case -2: 59725de24abSDavid Gibson /* Access rights violation */ 59825de24abSDavid Gibson env->exception_index = POWERPC_EXCP_ISI; 59925de24abSDavid Gibson env->error_code = 0x08000000; 60025de24abSDavid Gibson break; 60125de24abSDavid Gibson case -3: 60225de24abSDavid Gibson /* No execute protection violation */ 60325de24abSDavid Gibson env->exception_index = POWERPC_EXCP_ISI; 60425de24abSDavid Gibson env->error_code = 0x10000000; 60525de24abSDavid Gibson break; 60625de24abSDavid Gibson case -4: 60725de24abSDavid Gibson /* Direct store exception */ 60825de24abSDavid Gibson /* No code fetch is allowed in direct-store areas */ 60925de24abSDavid Gibson env->exception_index = POWERPC_EXCP_ISI; 61025de24abSDavid Gibson env->error_code = 0x10000000; 61125de24abSDavid Gibson break; 61225de24abSDavid Gibson } 61325de24abSDavid Gibson } else { 61425de24abSDavid Gibson switch (ret) { 61525de24abSDavid Gibson case -1: 61625de24abSDavid Gibson /* No matches in page tables or TLB */ 61725de24abSDavid Gibson env->exception_index = POWERPC_EXCP_DSI; 61825de24abSDavid Gibson env->error_code = 0; 61925de24abSDavid Gibson env->spr[SPR_DAR] = address; 62025de24abSDavid Gibson if (rw == 1) { 62125de24abSDavid Gibson env->spr[SPR_DSISR] = 0x42000000; 62225de24abSDavid Gibson } else { 62325de24abSDavid Gibson env->spr[SPR_DSISR] = 0x40000000; 62425de24abSDavid Gibson } 62525de24abSDavid Gibson break; 62625de24abSDavid Gibson case -2: 62725de24abSDavid Gibson /* Access rights violation */ 62825de24abSDavid Gibson env->exception_index = POWERPC_EXCP_DSI; 62925de24abSDavid Gibson env->error_code = 0; 63025de24abSDavid Gibson env->spr[SPR_DAR] = address; 63125de24abSDavid Gibson if (rw == 1) { 63225de24abSDavid Gibson env->spr[SPR_DSISR] = 0x0A000000; 63325de24abSDavid Gibson } else { 63425de24abSDavid Gibson env->spr[SPR_DSISR] = 0x08000000; 63525de24abSDavid Gibson } 63625de24abSDavid Gibson break; 63725de24abSDavid Gibson case -4: 63825de24abSDavid Gibson /* Direct store exception */ 63925de24abSDavid Gibson switch (access_type) { 64025de24abSDavid Gibson case ACCESS_FLOAT: 64125de24abSDavid Gibson /* Floating point load/store */ 64225de24abSDavid Gibson env->exception_index = POWERPC_EXCP_ALIGN; 64325de24abSDavid Gibson env->error_code = POWERPC_EXCP_ALIGN_FP; 64425de24abSDavid Gibson env->spr[SPR_DAR] = address; 64525de24abSDavid Gibson break; 64625de24abSDavid Gibson case ACCESS_RES: 64725de24abSDavid Gibson /* lwarx, ldarx or stwcx. */ 64825de24abSDavid Gibson env->exception_index = POWERPC_EXCP_DSI; 64925de24abSDavid Gibson env->error_code = 0; 65025de24abSDavid Gibson env->spr[SPR_DAR] = address; 65125de24abSDavid Gibson if (rw == 1) { 65225de24abSDavid Gibson env->spr[SPR_DSISR] = 0x06000000; 65325de24abSDavid Gibson } else { 65425de24abSDavid Gibson env->spr[SPR_DSISR] = 0x04000000; 65525de24abSDavid Gibson } 65625de24abSDavid Gibson break; 65725de24abSDavid Gibson case ACCESS_EXT: 65825de24abSDavid Gibson /* eciwx or ecowx */ 65925de24abSDavid Gibson env->exception_index = POWERPC_EXCP_DSI; 66025de24abSDavid Gibson env->error_code = 0; 66125de24abSDavid Gibson env->spr[SPR_DAR] = address; 66225de24abSDavid Gibson if (rw == 1) { 66325de24abSDavid Gibson env->spr[SPR_DSISR] = 0x06100000; 66425de24abSDavid Gibson } else { 66525de24abSDavid Gibson env->spr[SPR_DSISR] = 0x04100000; 66625de24abSDavid Gibson } 66725de24abSDavid Gibson break; 66825de24abSDavid Gibson default: 66925de24abSDavid Gibson printf("DSI: invalid exception (%d)\n", ret); 67025de24abSDavid Gibson env->exception_index = POWERPC_EXCP_PROGRAM; 67125de24abSDavid Gibson env->error_code = 67225de24abSDavid Gibson POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL; 67325de24abSDavid Gibson env->spr[SPR_DAR] = address; 67425de24abSDavid Gibson break; 67525de24abSDavid Gibson } 67625de24abSDavid Gibson break; 67725de24abSDavid Gibson } 67825de24abSDavid Gibson } 67925de24abSDavid Gibson #if 0 68025de24abSDavid Gibson printf("%s: set exception to %d %02x\n", __func__, 68125de24abSDavid Gibson env->exception, env->error_code); 68225de24abSDavid Gibson #endif 68325de24abSDavid Gibson ret = 1; 68425de24abSDavid Gibson } 68525de24abSDavid Gibson 68625de24abSDavid Gibson return ret; 68725de24abSDavid Gibson } 688