xref: /qemu/target/ppc/mmu-hash32.c (revision f078cd46de9efb5f102a4b32aaf1c8b96a90bfbd)
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     int prot;                      /* Protection bits           */
475dc68eb0SDavid Gibson     hwaddr hash[2];    /* Pagetable hash values     */
485dc68eb0SDavid Gibson     target_ulong ptem;             /* Virtual segment ID | API  */
495dc68eb0SDavid Gibson     int key;                       /* Access key                */
505dc68eb0SDavid Gibson     int nx;                        /* Non-execute area          */
515dc68eb0SDavid Gibson };
525dc68eb0SDavid Gibson 
539d7c3f4aSDavid Gibson #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
549d7c3f4aSDavid Gibson 
55496272a7SDavid Gibson static int ppc_hash32_pp_check(int key, int pp, int nx)
56496272a7SDavid Gibson {
57496272a7SDavid Gibson     int access;
58496272a7SDavid Gibson 
59496272a7SDavid Gibson     /* Compute access rights */
60496272a7SDavid Gibson     access = 0;
61496272a7SDavid Gibson     if (key == 0) {
62496272a7SDavid Gibson         switch (pp) {
63496272a7SDavid Gibson         case 0x0:
64496272a7SDavid Gibson         case 0x1:
65496272a7SDavid Gibson         case 0x2:
66496272a7SDavid Gibson             access |= PAGE_WRITE;
67496272a7SDavid Gibson             /* No break here */
68496272a7SDavid Gibson         case 0x3:
69496272a7SDavid Gibson             access |= PAGE_READ;
70496272a7SDavid Gibson             break;
71496272a7SDavid Gibson         }
72496272a7SDavid Gibson     } else {
73496272a7SDavid Gibson         switch (pp) {
74496272a7SDavid Gibson         case 0x0:
75496272a7SDavid Gibson             access = 0;
76496272a7SDavid Gibson             break;
77496272a7SDavid Gibson         case 0x1:
78496272a7SDavid Gibson         case 0x3:
79496272a7SDavid Gibson             access = PAGE_READ;
80496272a7SDavid Gibson             break;
81496272a7SDavid Gibson         case 0x2:
82496272a7SDavid Gibson             access = PAGE_READ | PAGE_WRITE;
83496272a7SDavid Gibson             break;
84496272a7SDavid Gibson         }
85496272a7SDavid Gibson     }
86496272a7SDavid Gibson     if (nx == 0) {
87496272a7SDavid Gibson         access |= PAGE_EXEC;
88496272a7SDavid Gibson     }
89496272a7SDavid Gibson 
90496272a7SDavid Gibson     return access;
91496272a7SDavid Gibson }
92496272a7SDavid Gibson 
9391cda45bSDavid Gibson static int ppc_hash32_check_prot(int prot, int rwx)
94496272a7SDavid Gibson {
95496272a7SDavid Gibson     int ret;
96496272a7SDavid Gibson 
9791cda45bSDavid Gibson     if (rwx == 2) {
98496272a7SDavid Gibson         if (prot & PAGE_EXEC) {
99496272a7SDavid Gibson             ret = 0;
100496272a7SDavid Gibson         } else {
101496272a7SDavid Gibson             ret = -2;
102496272a7SDavid Gibson         }
10391cda45bSDavid Gibson     } else if (rwx) {
104496272a7SDavid Gibson         if (prot & PAGE_WRITE) {
105496272a7SDavid Gibson             ret = 0;
106496272a7SDavid Gibson         } else {
107496272a7SDavid Gibson             ret = -2;
108496272a7SDavid Gibson         }
109496272a7SDavid Gibson     } else {
110496272a7SDavid Gibson         if (prot & PAGE_READ) {
111496272a7SDavid Gibson             ret = 0;
112496272a7SDavid Gibson         } else {
113496272a7SDavid Gibson             ret = -2;
114496272a7SDavid Gibson         }
115496272a7SDavid Gibson     }
116496272a7SDavid Gibson 
117496272a7SDavid Gibson     return ret;
118496272a7SDavid Gibson }
119496272a7SDavid Gibson 
12098132796SDavid Gibson /* Perform BAT hit & translation */
12198132796SDavid Gibson static void hash32_bat_size_prot(CPUPPCState *env, target_ulong *blp,
12298132796SDavid Gibson                                  int *validp, int *protp, target_ulong *BATu,
12398132796SDavid Gibson                                  target_ulong *BATl)
12498132796SDavid Gibson {
12598132796SDavid Gibson     target_ulong bl;
12698132796SDavid Gibson     int pp, valid, prot;
12798132796SDavid Gibson 
128d5aea6f3SDavid Gibson     bl = (*BATu & BATU32_BL) << 15;
12998132796SDavid Gibson     valid = 0;
13098132796SDavid Gibson     prot = 0;
131d5aea6f3SDavid Gibson     if (((msr_pr == 0) && (*BATu & BATU32_VS)) ||
132d5aea6f3SDavid Gibson         ((msr_pr != 0) && (*BATu & BATU32_VP))) {
13398132796SDavid Gibson         valid = 1;
134d5aea6f3SDavid Gibson         pp = *BATl & BATL32_PP;
13598132796SDavid Gibson         if (pp != 0) {
13698132796SDavid Gibson             prot = PAGE_READ | PAGE_EXEC;
13798132796SDavid Gibson             if (pp == 0x2) {
13898132796SDavid Gibson                 prot |= PAGE_WRITE;
13998132796SDavid Gibson             }
14098132796SDavid Gibson         }
14198132796SDavid Gibson     }
14298132796SDavid Gibson     *blp = bl;
14398132796SDavid Gibson     *validp = valid;
14498132796SDavid Gibson     *protp = prot;
14598132796SDavid Gibson }
14698132796SDavid Gibson 
14798132796SDavid Gibson static void hash32_bat_601_size_prot(CPUPPCState *env, target_ulong *blp,
14898132796SDavid Gibson                                      int *validp, int *protp,
14998132796SDavid Gibson                                      target_ulong *BATu, target_ulong *BATl)
15098132796SDavid Gibson {
15198132796SDavid Gibson     target_ulong bl;
15298132796SDavid Gibson     int key, pp, valid, prot;
15398132796SDavid Gibson 
154d5aea6f3SDavid Gibson     bl = (*BATl & BATL32_601_BL) << 17;
15598132796SDavid Gibson     LOG_BATS("b %02x ==> bl " TARGET_FMT_lx " msk " TARGET_FMT_lx "\n",
156d5aea6f3SDavid Gibson              (uint8_t)(*BATl & BATL32_601_BL), bl, ~bl);
15798132796SDavid Gibson     prot = 0;
158d5aea6f3SDavid Gibson     valid = !!(*BATl & BATL32_601_V);
15998132796SDavid Gibson     if (valid) {
160d5aea6f3SDavid Gibson         pp = *BATu & BATU32_601_PP;
16198132796SDavid Gibson         if (msr_pr == 0) {
162d5aea6f3SDavid Gibson             key = !!(*BATu & BATU32_601_KS);
16398132796SDavid Gibson         } else {
164d5aea6f3SDavid Gibson             key = !!(*BATu & BATU32_601_KP);
16598132796SDavid Gibson         }
16698132796SDavid Gibson         prot = ppc_hash32_pp_check(key, pp, 0);
16798132796SDavid Gibson     }
16898132796SDavid Gibson     *blp = bl;
16998132796SDavid Gibson     *validp = valid;
17098132796SDavid Gibson     *protp = prot;
17198132796SDavid Gibson }
17298132796SDavid Gibson 
1735dc68eb0SDavid Gibson static int ppc_hash32_get_bat(CPUPPCState *env, struct mmu_ctx_hash32 *ctx,
17491cda45bSDavid Gibson                               target_ulong virtual, int rwx)
17598132796SDavid Gibson {
17698132796SDavid Gibson     target_ulong *BATlt, *BATut, *BATu, *BATl;
17798132796SDavid Gibson     target_ulong BEPIl, BEPIu, bl;
17898132796SDavid Gibson     int i, valid, prot;
17998132796SDavid Gibson     int ret = -1;
18098132796SDavid Gibson 
18198132796SDavid Gibson     LOG_BATS("%s: %cBAT v " TARGET_FMT_lx "\n", __func__,
18291cda45bSDavid Gibson              rwx == 2 ? 'I' : 'D', virtual);
18391cda45bSDavid Gibson     if (rwx == 2) {
18498132796SDavid Gibson         BATlt = env->IBAT[1];
18598132796SDavid Gibson         BATut = env->IBAT[0];
18691cda45bSDavid Gibson     } else {
18798132796SDavid Gibson         BATlt = env->DBAT[1];
18898132796SDavid Gibson         BATut = env->DBAT[0];
18998132796SDavid Gibson     }
19098132796SDavid Gibson     for (i = 0; i < env->nb_BATs; i++) {
19198132796SDavid Gibson         BATu = &BATut[i];
19298132796SDavid Gibson         BATl = &BATlt[i];
193d5aea6f3SDavid Gibson         BEPIu = *BATu & BATU32_BEPIU;
194d5aea6f3SDavid Gibson         BEPIl = *BATu & BATU32_BEPIL;
19598132796SDavid Gibson         if (unlikely(env->mmu_model == POWERPC_MMU_601)) {
19698132796SDavid Gibson             hash32_bat_601_size_prot(env, &bl, &valid, &prot, BATu, BATl);
19798132796SDavid Gibson         } else {
19898132796SDavid Gibson             hash32_bat_size_prot(env, &bl, &valid, &prot, BATu, BATl);
19998132796SDavid Gibson         }
20098132796SDavid Gibson         LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx
20198132796SDavid Gibson                  " BATl " TARGET_FMT_lx "\n", __func__,
20298132796SDavid Gibson                  type == ACCESS_CODE ? 'I' : 'D', i, virtual, *BATu, *BATl);
203d5aea6f3SDavid Gibson         if ((virtual & BATU32_BEPIU) == BEPIu &&
204d5aea6f3SDavid Gibson             ((virtual & BATU32_BEPIL) & ~bl) == BEPIl) {
20598132796SDavid Gibson             /* BAT matches */
20698132796SDavid Gibson             if (valid != 0) {
20798132796SDavid Gibson                 /* Get physical address */
208d5aea6f3SDavid Gibson                 ctx->raddr = (*BATl & BATL32_BRPNU) |
209d5aea6f3SDavid Gibson                     ((virtual & BATU32_BEPIL & bl) | (*BATl & BATL32_BRPNL)) |
21098132796SDavid Gibson                     (virtual & 0x0001F000);
21198132796SDavid Gibson                 /* Compute access rights */
21298132796SDavid Gibson                 ctx->prot = prot;
21391cda45bSDavid Gibson                 ret = ppc_hash32_check_prot(ctx->prot, rwx);
21498132796SDavid Gibson                 if (ret == 0) {
21598132796SDavid Gibson                     LOG_BATS("BAT %d match: r " TARGET_FMT_plx " prot=%c%c\n",
21698132796SDavid Gibson                              i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
21798132796SDavid Gibson                              ctx->prot & PAGE_WRITE ? 'W' : '-');
21898132796SDavid Gibson                 }
21998132796SDavid Gibson                 break;
22098132796SDavid Gibson             }
22198132796SDavid Gibson         }
22298132796SDavid Gibson     }
22398132796SDavid Gibson     if (ret < 0) {
22498132796SDavid Gibson #if defined(DEBUG_BATS)
22598132796SDavid Gibson         if (qemu_log_enabled()) {
22698132796SDavid Gibson             LOG_BATS("no BAT match for " TARGET_FMT_lx ":\n", virtual);
22798132796SDavid Gibson             for (i = 0; i < 4; i++) {
22898132796SDavid Gibson                 BATu = &BATut[i];
22998132796SDavid Gibson                 BATl = &BATlt[i];
230d5aea6f3SDavid Gibson                 BEPIu = *BATu & BATU32_BEPIU;
231d5aea6f3SDavid Gibson                 BEPIl = *BATu & BATU32_BEPIL;
23298132796SDavid Gibson                 bl = (*BATu & 0x00001FFC) << 15;
23398132796SDavid Gibson                 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx
23498132796SDavid Gibson                          " BATl " TARGET_FMT_lx "\n\t" TARGET_FMT_lx " "
23598132796SDavid Gibson                          TARGET_FMT_lx " " TARGET_FMT_lx "\n",
23698132796SDavid Gibson                          __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
23798132796SDavid Gibson                          *BATu, *BATl, BEPIu, BEPIl, bl);
23898132796SDavid Gibson             }
23998132796SDavid Gibson         }
24098132796SDavid Gibson #endif
24198132796SDavid Gibson     }
24298132796SDavid Gibson     /* No hit */
24398132796SDavid Gibson     return ret;
24498132796SDavid Gibson }
24598132796SDavid Gibson 
2465dc68eb0SDavid Gibson static int pte_check_hash32(struct mmu_ctx_hash32 *ctx, target_ulong pte0,
24791cda45bSDavid Gibson                             target_ulong pte1, int h, int rwx)
2489d7c3f4aSDavid Gibson {
249d5aea6f3SDavid Gibson     target_ulong mmask;
250d5aea6f3SDavid Gibson     int access, ret, pp;
2519d7c3f4aSDavid Gibson 
2529d7c3f4aSDavid Gibson     ret = -1;
2539d7c3f4aSDavid Gibson     /* Check validity and table match */
254d5aea6f3SDavid Gibson     if ((pte0 & HPTE32_V_VALID) && (h == !!(pte0 & HPTE32_V_SECONDARY))) {
2559d7c3f4aSDavid Gibson         /* Check vsid & api */
2569d7c3f4aSDavid Gibson         mmask = PTE_CHECK_MASK;
257d5aea6f3SDavid Gibson         pp = pte1 & HPTE32_R_PP;
258d5aea6f3SDavid Gibson         if (HPTE32_V_COMPARE(pte0, ctx->ptem)) {
2599d7c3f4aSDavid Gibson             if (ctx->raddr != (hwaddr)-1ULL) {
2609d7c3f4aSDavid Gibson                 /* all matches should have equal RPN, WIMG & PP */
2619d7c3f4aSDavid Gibson                 if ((ctx->raddr & mmask) != (pte1 & mmask)) {
2629d7c3f4aSDavid Gibson                     qemu_log("Bad RPN/WIMG/PP\n");
2639d7c3f4aSDavid Gibson                     return -3;
2649d7c3f4aSDavid Gibson                 }
2659d7c3f4aSDavid Gibson             }
2669d7c3f4aSDavid Gibson             /* Compute access rights */
267496272a7SDavid Gibson             access = ppc_hash32_pp_check(ctx->key, pp, ctx->nx);
2689d7c3f4aSDavid Gibson             /* Keep the matching PTE informations */
2699d7c3f4aSDavid Gibson             ctx->raddr = pte1;
2709d7c3f4aSDavid Gibson             ctx->prot = access;
27191cda45bSDavid Gibson             ret = ppc_hash32_check_prot(ctx->prot, rwx);
2729d7c3f4aSDavid Gibson             if (ret == 0) {
2739d7c3f4aSDavid Gibson                 /* Access granted */
2749d7c3f4aSDavid Gibson                 LOG_MMU("PTE access granted !\n");
2759d7c3f4aSDavid Gibson             } else {
2769d7c3f4aSDavid Gibson                 /* Access right violation */
2779d7c3f4aSDavid Gibson                 LOG_MMU("PTE access rejected\n");
2789d7c3f4aSDavid Gibson             }
2799d7c3f4aSDavid Gibson         }
2809d7c3f4aSDavid Gibson     }
2819d7c3f4aSDavid Gibson 
2829d7c3f4aSDavid Gibson     return ret;
2839d7c3f4aSDavid Gibson }
284c69b6151SDavid Gibson 
2855dc68eb0SDavid Gibson static int ppc_hash32_pte_update_flags(struct mmu_ctx_hash32 *ctx, target_ulong *pte1p,
28691cda45bSDavid Gibson                                        int ret, int rwx)
287496272a7SDavid Gibson {
288496272a7SDavid Gibson     int store = 0;
289496272a7SDavid Gibson 
290496272a7SDavid Gibson     /* Update page flags */
291d5aea6f3SDavid Gibson     if (!(*pte1p & HPTE32_R_R)) {
292496272a7SDavid Gibson         /* Update accessed flag */
293d5aea6f3SDavid Gibson         *pte1p |= HPTE32_R_R;
294496272a7SDavid Gibson         store = 1;
295496272a7SDavid Gibson     }
296d5aea6f3SDavid Gibson     if (!(*pte1p & HPTE32_R_C)) {
29791cda45bSDavid Gibson         if (rwx == 1 && ret == 0) {
298496272a7SDavid Gibson             /* Update changed flag */
299d5aea6f3SDavid Gibson             *pte1p |= HPTE32_R_C;
300496272a7SDavid Gibson             store = 1;
301496272a7SDavid Gibson         } else {
302496272a7SDavid Gibson             /* Force page fault for first write access */
303496272a7SDavid Gibson             ctx->prot &= ~PAGE_WRITE;
304496272a7SDavid Gibson         }
305496272a7SDavid Gibson     }
306496272a7SDavid Gibson 
307496272a7SDavid Gibson     return store;
308496272a7SDavid Gibson }
309496272a7SDavid Gibson 
31059191721SDavid Gibson hwaddr get_pteg_offset32(CPUPPCState *env, hwaddr hash)
31159191721SDavid Gibson {
312d5aea6f3SDavid Gibson     return (hash * HASH_PTEG_SIZE_32) & env->htab_mask;
31359191721SDavid Gibson }
31459191721SDavid Gibson 
315c69b6151SDavid Gibson /* PTE table lookup */
316f078cd46SDavid Gibson static int find_pte32(CPUPPCState *env, struct mmu_ctx_hash32 *ctx,
317f078cd46SDavid Gibson                       target_ulong eaddr, int h, int rwx, int target_page_bits)
318c69b6151SDavid Gibson {
319c69b6151SDavid Gibson     hwaddr pteg_off;
320c69b6151SDavid Gibson     target_ulong pte0, pte1;
321c69b6151SDavid Gibson     int i, good = -1;
322c69b6151SDavid Gibson     int ret, r;
323c69b6151SDavid Gibson 
324c69b6151SDavid Gibson     ret = -1; /* No entry found */
32559191721SDavid Gibson     pteg_off = get_pteg_offset32(env, ctx->hash[h]);
326d5aea6f3SDavid Gibson     for (i = 0; i < HPTES_PER_GROUP; i++) {
327dffdaf61SDavid Gibson         pte0 = ppc_hash32_load_hpte0(env, pteg_off + i*HASH_PTE_SIZE_32);
328dffdaf61SDavid Gibson         pte1 = ppc_hash32_load_hpte1(env, pteg_off + i*HASH_PTE_SIZE_32);
32991cda45bSDavid Gibson         r = pte_check_hash32(ctx, pte0, pte1, h, rwx);
330c69b6151SDavid Gibson         LOG_MMU("Load pte from %08" HWADDR_PRIx " => " TARGET_FMT_lx " "
331c69b6151SDavid Gibson                 TARGET_FMT_lx " %d %d %d " TARGET_FMT_lx "\n",
332c69b6151SDavid Gibson                 pteg_off + (i * 8), pte0, pte1, (int)(pte0 >> 31), h,
333c69b6151SDavid Gibson                 (int)((pte0 >> 6) & 1), ctx->ptem);
334c69b6151SDavid Gibson         switch (r) {
335c69b6151SDavid Gibson         case -3:
336c69b6151SDavid Gibson             /* PTE inconsistency */
337c69b6151SDavid Gibson             return -1;
338c69b6151SDavid Gibson         case -2:
339c69b6151SDavid Gibson             /* Access violation */
340c69b6151SDavid Gibson             ret = -2;
341c69b6151SDavid Gibson             good = i;
342c69b6151SDavid Gibson             break;
343c69b6151SDavid Gibson         case -1:
344c69b6151SDavid Gibson         default:
345c69b6151SDavid Gibson             /* No PTE match */
346c69b6151SDavid Gibson             break;
347c69b6151SDavid Gibson         case 0:
348c69b6151SDavid Gibson             /* access granted */
349c69b6151SDavid Gibson             /* XXX: we should go on looping to check all PTEs consistency
350c69b6151SDavid Gibson              *      but if we can speed-up the whole thing as the
351c69b6151SDavid Gibson              *      result would be undefined if PTEs are not consistent.
352c69b6151SDavid Gibson              */
353c69b6151SDavid Gibson             ret = 0;
354c69b6151SDavid Gibson             good = i;
355c69b6151SDavid Gibson             goto done;
356c69b6151SDavid Gibson         }
357c69b6151SDavid Gibson     }
358c69b6151SDavid Gibson     if (good != -1) {
359c69b6151SDavid Gibson     done:
360c69b6151SDavid Gibson         LOG_MMU("found PTE at addr %08" HWADDR_PRIx " prot=%01x ret=%d\n",
361c69b6151SDavid Gibson                 ctx->raddr, ctx->prot, ret);
362c69b6151SDavid Gibson         /* Update page flags */
363c69b6151SDavid Gibson         pte1 = ctx->raddr;
36491cda45bSDavid Gibson         if (ppc_hash32_pte_update_flags(ctx, &pte1, ret, rwx) == 1) {
365dffdaf61SDavid Gibson             ppc_hash32_store_hpte1(env, pteg_off + good * HASH_PTE_SIZE_32,
366c69b6151SDavid Gibson                                    pte1);
367c69b6151SDavid Gibson         }
368c69b6151SDavid Gibson     }
369c69b6151SDavid Gibson 
370c69b6151SDavid Gibson     /* We have a TLB that saves 4K pages, so let's
371c69b6151SDavid Gibson      * split a huge page to 4k chunks */
372c69b6151SDavid Gibson     if (target_page_bits != TARGET_PAGE_BITS) {
373f078cd46SDavid Gibson         ctx->raddr |= (eaddr & ((1 << target_page_bits) - 1))
374c69b6151SDavid Gibson                       & TARGET_PAGE_MASK;
375c69b6151SDavid Gibson     }
376c69b6151SDavid Gibson     return ret;
377c69b6151SDavid Gibson }
3780480884fSDavid Gibson 
3795dc68eb0SDavid Gibson static int get_segment32(CPUPPCState *env, struct mmu_ctx_hash32 *ctx,
38091cda45bSDavid Gibson                          target_ulong eaddr, int rwx)
3810480884fSDavid Gibson {
3820480884fSDavid Gibson     hwaddr hash;
3830480884fSDavid Gibson     target_ulong vsid;
3840480884fSDavid Gibson     int ds, pr, target_page_bits;
3850480884fSDavid Gibson     int ret, ret2;
3860480884fSDavid Gibson     target_ulong sr, pgidx;
3870480884fSDavid Gibson 
3880480884fSDavid Gibson     pr = msr_pr;
3890480884fSDavid Gibson 
3900480884fSDavid Gibson     sr = env->sr[eaddr >> 28];
391d5aea6f3SDavid Gibson     ctx->key = (((sr & SR32_KP) && (pr != 0)) ||
392d5aea6f3SDavid Gibson                 ((sr & SR32_KS) && (pr == 0))) ? 1 : 0;
393d5aea6f3SDavid Gibson     ds = !!(sr & SR32_T);
394d5aea6f3SDavid Gibson     ctx->nx = !!(sr & SR32_NX);
395d5aea6f3SDavid Gibson     vsid = sr & SR32_VSID;
3960480884fSDavid Gibson     target_page_bits = TARGET_PAGE_BITS;
3970480884fSDavid Gibson     LOG_MMU("Check segment v=" TARGET_FMT_lx " %d " TARGET_FMT_lx " nip="
3980480884fSDavid Gibson             TARGET_FMT_lx " lr=" TARGET_FMT_lx
39991cda45bSDavid Gibson             " ir=%d dr=%d pr=%d %d\n",
4000480884fSDavid Gibson             eaddr, (int)(eaddr >> 28), sr, env->nip, env->lr, (int)msr_ir,
40191cda45bSDavid Gibson             (int)msr_dr, pr != 0 ? 1 : 0, rwx);
4020480884fSDavid Gibson     pgidx = (eaddr & ~SEGMENT_MASK_256M) >> target_page_bits;
4030480884fSDavid Gibson     hash = vsid ^ pgidx;
4040480884fSDavid Gibson     ctx->ptem = (vsid << 7) | (pgidx >> 10);
4050480884fSDavid Gibson 
4060480884fSDavid Gibson     LOG_MMU("pte segment: key=%d ds %d nx %d vsid " TARGET_FMT_lx "\n",
4070480884fSDavid Gibson             ctx->key, ds, ctx->nx, vsid);
4080480884fSDavid Gibson     ret = -1;
4090480884fSDavid Gibson     if (!ds) {
4100480884fSDavid Gibson         /* Check if instruction fetch is allowed, if needed */
41191cda45bSDavid Gibson         if (rwx != 2 || ctx->nx == 0) {
4120480884fSDavid Gibson             /* Page address translation */
4130480884fSDavid Gibson             LOG_MMU("htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
4140480884fSDavid Gibson                     " hash " TARGET_FMT_plx "\n",
4150480884fSDavid Gibson                     env->htab_base, env->htab_mask, hash);
4160480884fSDavid Gibson             ctx->hash[0] = hash;
4170480884fSDavid Gibson             ctx->hash[1] = ~hash;
4180480884fSDavid Gibson 
4190480884fSDavid Gibson             /* Initialize real address with an invalid value */
4200480884fSDavid Gibson             ctx->raddr = (hwaddr)-1ULL;
4210480884fSDavid Gibson             LOG_MMU("0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
4220480884fSDavid Gibson                     " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
4230480884fSDavid Gibson                     " hash=" TARGET_FMT_plx "\n",
4240480884fSDavid Gibson                     env->htab_base, env->htab_mask, vsid, ctx->ptem,
4250480884fSDavid Gibson                     ctx->hash[0]);
4260480884fSDavid Gibson             /* Primary table lookup */
427f078cd46SDavid Gibson             ret = find_pte32(env, ctx, eaddr, 0, rwx, target_page_bits);
4280480884fSDavid Gibson             if (ret < 0) {
4290480884fSDavid Gibson                 /* Secondary table lookup */
4300480884fSDavid Gibson                 LOG_MMU("1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
4310480884fSDavid Gibson                         " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
4320480884fSDavid Gibson                         " hash=" TARGET_FMT_plx "\n", env->htab_base,
4330480884fSDavid Gibson                         env->htab_mask, vsid, ctx->ptem, ctx->hash[1]);
434f078cd46SDavid Gibson                 ret2 = find_pte32(env, ctx, eaddr, 1, rwx, target_page_bits);
4350480884fSDavid Gibson                 if (ret2 != -1) {
4360480884fSDavid Gibson                     ret = ret2;
4370480884fSDavid Gibson                 }
4380480884fSDavid Gibson             }
4390480884fSDavid Gibson #if defined(DUMP_PAGE_TABLES)
4400480884fSDavid Gibson             if (qemu_log_enabled()) {
4410480884fSDavid Gibson                 hwaddr curaddr;
4420480884fSDavid Gibson                 uint32_t a0, a1, a2, a3;
4430480884fSDavid Gibson 
4440480884fSDavid Gibson                 qemu_log("Page table: " TARGET_FMT_plx " len " TARGET_FMT_plx
4450480884fSDavid Gibson                          "\n", sdr, mask + 0x80);
4460480884fSDavid Gibson                 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
4470480884fSDavid Gibson                      curaddr += 16) {
4480480884fSDavid Gibson                     a0 = ldl_phys(curaddr);
4490480884fSDavid Gibson                     a1 = ldl_phys(curaddr + 4);
4500480884fSDavid Gibson                     a2 = ldl_phys(curaddr + 8);
4510480884fSDavid Gibson                     a3 = ldl_phys(curaddr + 12);
4520480884fSDavid Gibson                     if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
4530480884fSDavid Gibson                         qemu_log(TARGET_FMT_plx ": %08x %08x %08x %08x\n",
4540480884fSDavid Gibson                                  curaddr, a0, a1, a2, a3);
4550480884fSDavid Gibson                     }
4560480884fSDavid Gibson                 }
4570480884fSDavid Gibson             }
4580480884fSDavid Gibson #endif
4590480884fSDavid Gibson         } else {
4600480884fSDavid Gibson             LOG_MMU("No access allowed\n");
4610480884fSDavid Gibson             ret = -3;
4620480884fSDavid Gibson         }
4630480884fSDavid Gibson     } else {
4640480884fSDavid Gibson         target_ulong sr;
4650480884fSDavid Gibson 
4660480884fSDavid Gibson         LOG_MMU("direct store...\n");
4670480884fSDavid Gibson         /* Direct-store segment : absolutely *BUGGY* for now */
4680480884fSDavid Gibson 
4690480884fSDavid Gibson         /* Direct-store implies a 32-bit MMU.
4700480884fSDavid Gibson          * Check the Segment Register's bus unit ID (BUID).
4710480884fSDavid Gibson          */
4720480884fSDavid Gibson         sr = env->sr[eaddr >> 28];
4730480884fSDavid Gibson         if ((sr & 0x1FF00000) >> 20 == 0x07f) {
4740480884fSDavid Gibson             /* Memory-forced I/O controller interface access */
4750480884fSDavid Gibson             /* If T=1 and BUID=x'07F', the 601 performs a memory access
4760480884fSDavid Gibson              * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
4770480884fSDavid Gibson              */
4780480884fSDavid Gibson             ctx->raddr = ((sr & 0xF) << 28) | (eaddr & 0x0FFFFFFF);
4790480884fSDavid Gibson             ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
4800480884fSDavid Gibson             return 0;
4810480884fSDavid Gibson         }
4820480884fSDavid Gibson 
48391cda45bSDavid Gibson         if (rwx == 2) {
48491cda45bSDavid Gibson             /* No code fetch is allowed in direct-store areas */
48591cda45bSDavid Gibson             return -4;
48691cda45bSDavid Gibson         }
48791cda45bSDavid Gibson 
48891cda45bSDavid Gibson         switch (env->access_type) {
4890480884fSDavid Gibson         case ACCESS_INT:
4900480884fSDavid Gibson             /* Integer load/store : only access allowed */
4910480884fSDavid Gibson             break;
4920480884fSDavid Gibson         case ACCESS_FLOAT:
4930480884fSDavid Gibson             /* Floating point load/store */
4940480884fSDavid Gibson             return -4;
4950480884fSDavid Gibson         case ACCESS_RES:
4960480884fSDavid Gibson             /* lwarx, ldarx or srwcx. */
4970480884fSDavid Gibson             return -4;
4980480884fSDavid Gibson         case ACCESS_CACHE:
4990480884fSDavid Gibson             /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
5000480884fSDavid Gibson             /* Should make the instruction do no-op.
5010480884fSDavid Gibson              * As it already do no-op, it's quite easy :-)
5020480884fSDavid Gibson              */
5030480884fSDavid Gibson             ctx->raddr = eaddr;
5040480884fSDavid Gibson             return 0;
5050480884fSDavid Gibson         case ACCESS_EXT:
5060480884fSDavid Gibson             /* eciwx or ecowx */
5070480884fSDavid Gibson             return -4;
5080480884fSDavid Gibson         default:
5090480884fSDavid Gibson             qemu_log("ERROR: instruction should not need "
5100480884fSDavid Gibson                         "address translation\n");
5110480884fSDavid Gibson             return -4;
5120480884fSDavid Gibson         }
51391cda45bSDavid Gibson         if ((rwx == 1 || ctx->key != 1) && (rwx == 0 || ctx->key != 0)) {
5140480884fSDavid Gibson             ctx->raddr = eaddr;
5150480884fSDavid Gibson             ret = 2;
5160480884fSDavid Gibson         } else {
5170480884fSDavid Gibson             ret = -2;
5180480884fSDavid Gibson         }
5190480884fSDavid Gibson     }
5200480884fSDavid Gibson 
5210480884fSDavid Gibson     return ret;
5220480884fSDavid Gibson }
523629bd516SDavid Gibson 
5245dc68eb0SDavid Gibson static int ppc_hash32_get_physical_address(CPUPPCState *env, struct mmu_ctx_hash32 *ctx,
52591cda45bSDavid Gibson                                            target_ulong eaddr, int rwx)
526629bd516SDavid Gibson {
52791cda45bSDavid Gibson     bool real_mode = (rwx == 2 && msr_ir == 0)
52891cda45bSDavid Gibson         || (rwx != 2 && msr_dr == 0);
529629bd516SDavid Gibson 
530629bd516SDavid Gibson     if (real_mode) {
531629bd516SDavid Gibson         ctx->raddr = eaddr;
532629bd516SDavid Gibson         ctx->prot = PAGE_READ | PAGE_EXEC | PAGE_WRITE;
533629bd516SDavid Gibson         return 0;
534629bd516SDavid Gibson     } else {
535629bd516SDavid Gibson         int ret = -1;
536629bd516SDavid Gibson 
537629bd516SDavid Gibson         /* Try to find a BAT */
538629bd516SDavid Gibson         if (env->nb_BATs != 0) {
53991cda45bSDavid Gibson             ret = ppc_hash32_get_bat(env, ctx, eaddr, rwx);
540629bd516SDavid Gibson         }
541629bd516SDavid Gibson         if (ret < 0) {
542629bd516SDavid Gibson             /* We didn't match any BAT entry or don't have BATs */
54391cda45bSDavid Gibson             ret = get_segment32(env, ctx, eaddr, rwx);
544629bd516SDavid Gibson         }
545629bd516SDavid Gibson         return ret;
546629bd516SDavid Gibson     }
547629bd516SDavid Gibson }
54825de24abSDavid Gibson 
549f2ad6be8SDavid Gibson hwaddr ppc_hash32_get_phys_page_debug(CPUPPCState *env, target_ulong addr)
550f2ad6be8SDavid Gibson {
5515dc68eb0SDavid Gibson     struct mmu_ctx_hash32 ctx;
552f2ad6be8SDavid Gibson 
55391cda45bSDavid Gibson     /* FIXME: Will not behave sanely for direct store segments, but
55491cda45bSDavid Gibson      * they're almost never used */
55591cda45bSDavid Gibson     if (unlikely(ppc_hash32_get_physical_address(env, &ctx, addr, 0)
556f2ad6be8SDavid Gibson                  != 0)) {
557f2ad6be8SDavid Gibson         return -1;
558f2ad6be8SDavid Gibson     }
559f2ad6be8SDavid Gibson 
560f2ad6be8SDavid Gibson     return ctx.raddr & TARGET_PAGE_MASK;
561f2ad6be8SDavid Gibson }
562f2ad6be8SDavid Gibson 
56391cda45bSDavid Gibson int ppc_hash32_handle_mmu_fault(CPUPPCState *env, target_ulong address, int rwx,
56425de24abSDavid Gibson                                 int mmu_idx)
56525de24abSDavid Gibson {
5665dc68eb0SDavid Gibson     struct mmu_ctx_hash32 ctx;
56725de24abSDavid Gibson     int ret = 0;
56825de24abSDavid Gibson 
56991cda45bSDavid Gibson     ret = ppc_hash32_get_physical_address(env, &ctx, address, rwx);
57025de24abSDavid Gibson     if (ret == 0) {
57125de24abSDavid Gibson         tlb_set_page(env, address & TARGET_PAGE_MASK,
57225de24abSDavid Gibson                      ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
57325de24abSDavid Gibson                      mmu_idx, TARGET_PAGE_SIZE);
57425de24abSDavid Gibson         ret = 0;
57525de24abSDavid Gibson     } else if (ret < 0) {
57625de24abSDavid Gibson         LOG_MMU_STATE(env);
57791cda45bSDavid Gibson         if (rwx == 2) {
57825de24abSDavid Gibson             switch (ret) {
57925de24abSDavid Gibson             case -1:
58025de24abSDavid Gibson                 /* No matches in page tables or TLB */
58125de24abSDavid Gibson                 env->exception_index = POWERPC_EXCP_ISI;
58225de24abSDavid Gibson                 env->error_code = 0x40000000;
58325de24abSDavid Gibson                 break;
58425de24abSDavid Gibson             case -2:
58525de24abSDavid Gibson                 /* Access rights violation */
58625de24abSDavid Gibson                 env->exception_index = POWERPC_EXCP_ISI;
58725de24abSDavid Gibson                 env->error_code = 0x08000000;
58825de24abSDavid Gibson                 break;
58925de24abSDavid Gibson             case -3:
59025de24abSDavid Gibson                 /* No execute protection violation */
59125de24abSDavid Gibson                 env->exception_index = POWERPC_EXCP_ISI;
59225de24abSDavid Gibson                 env->error_code = 0x10000000;
59325de24abSDavid Gibson                 break;
59425de24abSDavid Gibson             case -4:
59525de24abSDavid Gibson                 /* Direct store exception */
59625de24abSDavid Gibson                 /* No code fetch is allowed in direct-store areas */
59725de24abSDavid Gibson                 env->exception_index = POWERPC_EXCP_ISI;
59825de24abSDavid Gibson                 env->error_code = 0x10000000;
59925de24abSDavid Gibson                 break;
60025de24abSDavid Gibson             }
60125de24abSDavid Gibson         } else {
60225de24abSDavid Gibson             switch (ret) {
60325de24abSDavid Gibson             case -1:
60425de24abSDavid Gibson                 /* No matches in page tables or TLB */
60525de24abSDavid Gibson                 env->exception_index = POWERPC_EXCP_DSI;
60625de24abSDavid Gibson                 env->error_code = 0;
60725de24abSDavid Gibson                 env->spr[SPR_DAR] = address;
60891cda45bSDavid Gibson                 if (rwx == 1) {
60925de24abSDavid Gibson                     env->spr[SPR_DSISR] = 0x42000000;
61025de24abSDavid Gibson                 } else {
61125de24abSDavid Gibson                     env->spr[SPR_DSISR] = 0x40000000;
61225de24abSDavid Gibson                 }
61325de24abSDavid Gibson                 break;
61425de24abSDavid Gibson             case -2:
61525de24abSDavid Gibson                 /* Access rights violation */
61625de24abSDavid Gibson                 env->exception_index = POWERPC_EXCP_DSI;
61725de24abSDavid Gibson                 env->error_code = 0;
61825de24abSDavid Gibson                 env->spr[SPR_DAR] = address;
61991cda45bSDavid Gibson                 if (rwx == 1) {
62025de24abSDavid Gibson                     env->spr[SPR_DSISR] = 0x0A000000;
62125de24abSDavid Gibson                 } else {
62225de24abSDavid Gibson                     env->spr[SPR_DSISR] = 0x08000000;
62325de24abSDavid Gibson                 }
62425de24abSDavid Gibson                 break;
62525de24abSDavid Gibson             case -4:
62625de24abSDavid Gibson                 /* Direct store exception */
62791cda45bSDavid Gibson                 switch (env->access_type) {
62825de24abSDavid Gibson                 case ACCESS_FLOAT:
62925de24abSDavid Gibson                     /* Floating point load/store */
63025de24abSDavid Gibson                     env->exception_index = POWERPC_EXCP_ALIGN;
63125de24abSDavid Gibson                     env->error_code = POWERPC_EXCP_ALIGN_FP;
63225de24abSDavid Gibson                     env->spr[SPR_DAR] = address;
63325de24abSDavid Gibson                     break;
63425de24abSDavid Gibson                 case ACCESS_RES:
63525de24abSDavid Gibson                     /* lwarx, ldarx or stwcx. */
63625de24abSDavid Gibson                     env->exception_index = POWERPC_EXCP_DSI;
63725de24abSDavid Gibson                     env->error_code = 0;
63825de24abSDavid Gibson                     env->spr[SPR_DAR] = address;
63991cda45bSDavid Gibson                     if (rwx == 1) {
64025de24abSDavid Gibson                         env->spr[SPR_DSISR] = 0x06000000;
64125de24abSDavid Gibson                     } else {
64225de24abSDavid Gibson                         env->spr[SPR_DSISR] = 0x04000000;
64325de24abSDavid Gibson                     }
64425de24abSDavid Gibson                     break;
64525de24abSDavid Gibson                 case ACCESS_EXT:
64625de24abSDavid Gibson                     /* eciwx or ecowx */
64725de24abSDavid Gibson                     env->exception_index = POWERPC_EXCP_DSI;
64825de24abSDavid Gibson                     env->error_code = 0;
64925de24abSDavid Gibson                     env->spr[SPR_DAR] = address;
65091cda45bSDavid Gibson                     if (rwx == 1) {
65125de24abSDavid Gibson                         env->spr[SPR_DSISR] = 0x06100000;
65225de24abSDavid Gibson                     } else {
65325de24abSDavid Gibson                         env->spr[SPR_DSISR] = 0x04100000;
65425de24abSDavid Gibson                     }
65525de24abSDavid Gibson                     break;
65625de24abSDavid Gibson                 default:
65725de24abSDavid Gibson                     printf("DSI: invalid exception (%d)\n", ret);
65825de24abSDavid Gibson                     env->exception_index = POWERPC_EXCP_PROGRAM;
65925de24abSDavid Gibson                     env->error_code =
66025de24abSDavid Gibson                         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
66125de24abSDavid Gibson                     env->spr[SPR_DAR] = address;
66225de24abSDavid Gibson                     break;
66325de24abSDavid Gibson                 }
66425de24abSDavid Gibson                 break;
66525de24abSDavid Gibson             }
66625de24abSDavid Gibson         }
66725de24abSDavid Gibson #if 0
66825de24abSDavid Gibson         printf("%s: set exception to %d %02x\n", __func__,
66925de24abSDavid Gibson                env->exception, env->error_code);
67025de24abSDavid Gibson #endif
67125de24abSDavid Gibson         ret = 1;
67225de24abSDavid Gibson     }
67325de24abSDavid Gibson 
67425de24abSDavid Gibson     return ret;
67525de24abSDavid Gibson }
676