xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision 26d02c9d426761503afb60df3be9f44fb19cf3d0)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  PowerPC MMU, TLB, SLB and BAT emulation helpers for QEMU.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2007 Jocelyn Mayer
5fcf5ef2aSThomas Huth  *  Copyright (c) 2013 David Gibson, IBM Corporation
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
106bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21a864a6b3SDavid Gibson #include "qemu/units.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "exec/exec-all.h"
24fcf5ef2aSThomas Huth #include "qemu/error-report.h"
25fad866daSMarkus Armbruster #include "qemu/qemu-print.h"
26b3946626SVincent Palatin #include "sysemu/hw_accel.h"
27fcf5ef2aSThomas Huth #include "kvm_ppc.h"
28fcf5ef2aSThomas Huth #include "mmu-hash64.h"
29fcf5ef2aSThomas Huth #include "exec/log.h"
307222b94aSDavid Gibson #include "hw/hw.h"
31182357dbSRichard Henderson #include "internal.h"
32b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
33f03de3b4SRichard Henderson #include "helper_regs.h"
34fcf5ef2aSThomas Huth 
352b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
362b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h"
372b44e219SBruno Larsen (billionai) #endif
382b44e219SBruno Larsen (billionai) 
39d75cbae8SDavid Gibson /* #define DEBUG_SLB */
40fcf5ef2aSThomas Huth 
41fcf5ef2aSThomas Huth #ifdef DEBUG_SLB
42fcf5ef2aSThomas Huth #  define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
43fcf5ef2aSThomas Huth #else
44fcf5ef2aSThomas Huth #  define LOG_SLB(...) do { } while (0)
45fcf5ef2aSThomas Huth #endif
46fcf5ef2aSThomas Huth 
47fcf5ef2aSThomas Huth /*
48fcf5ef2aSThomas Huth  * SLB handling
49fcf5ef2aSThomas Huth  */
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr)
52fcf5ef2aSThomas Huth {
53fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
54fcf5ef2aSThomas Huth     uint64_t esid_256M, esid_1T;
55fcf5ef2aSThomas Huth     int n;
56fcf5ef2aSThomas Huth 
57fcf5ef2aSThomas Huth     LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr);
58fcf5ef2aSThomas Huth 
59fcf5ef2aSThomas Huth     esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V;
60fcf5ef2aSThomas Huth     esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V;
61fcf5ef2aSThomas Huth 
6267d7d66fSDavid Gibson     for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
63fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
64fcf5ef2aSThomas Huth 
65fcf5ef2aSThomas Huth         LOG_SLB("%s: slot %d %016" PRIx64 " %016"
66fcf5ef2aSThomas Huth                     PRIx64 "\n", __func__, n, slb->esid, slb->vsid);
67d75cbae8SDavid Gibson         /*
68d75cbae8SDavid Gibson          * We check for 1T matches on all MMUs here - if the MMU
69fcf5ef2aSThomas Huth          * doesn't have 1T segment support, we will have prevented 1T
70d75cbae8SDavid Gibson          * entries from being inserted in the slbmte code.
71d75cbae8SDavid Gibson          */
72fcf5ef2aSThomas Huth         if (((slb->esid == esid_256M) &&
73fcf5ef2aSThomas Huth              ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M))
74fcf5ef2aSThomas Huth             || ((slb->esid == esid_1T) &&
75fcf5ef2aSThomas Huth                 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) {
76fcf5ef2aSThomas Huth             return slb;
77fcf5ef2aSThomas Huth         }
78fcf5ef2aSThomas Huth     }
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     return NULL;
81fcf5ef2aSThomas Huth }
82fcf5ef2aSThomas Huth 
83fad866daSMarkus Armbruster void dump_slb(PowerPCCPU *cpu)
84fcf5ef2aSThomas Huth {
85fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
86fcf5ef2aSThomas Huth     int i;
87fcf5ef2aSThomas Huth     uint64_t slbe, slbv;
88fcf5ef2aSThomas Huth 
89fcf5ef2aSThomas Huth     cpu_synchronize_state(CPU(cpu));
90fcf5ef2aSThomas Huth 
91fad866daSMarkus Armbruster     qemu_printf("SLB\tESID\t\t\tVSID\n");
9267d7d66fSDavid Gibson     for (i = 0; i < cpu->hash64_opts->slb_size; i++) {
93fcf5ef2aSThomas Huth         slbe = env->slb[i].esid;
94fcf5ef2aSThomas Huth         slbv = env->slb[i].vsid;
95fcf5ef2aSThomas Huth         if (slbe == 0 && slbv == 0) {
96fcf5ef2aSThomas Huth             continue;
97fcf5ef2aSThomas Huth         }
98fad866daSMarkus Armbruster         qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n",
99fcf5ef2aSThomas Huth                     i, slbe, slbv);
100fcf5ef2aSThomas Huth     }
101fcf5ef2aSThomas Huth }
102fcf5ef2aSThomas Huth 
1032b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
1042bfcb7a3SLucas Coutinho void helper_SLBIA(CPUPPCState *env, uint32_t ih)
105fcf5ef2aSThomas Huth {
106db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
1070418bf78SNicholas Piggin     int starting_entry;
108fcf5ef2aSThomas Huth     int n;
109fcf5ef2aSThomas Huth 
110f9e3e1a3SNicholas Piggin     /*
111f9e3e1a3SNicholas Piggin      * slbia must always flush all TLB (which is equivalent to ERAT in ppc
112f9e3e1a3SNicholas Piggin      * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
113f9e3e1a3SNicholas Piggin      * can overwrite a valid SLB without flushing its lookaside information.
114f9e3e1a3SNicholas Piggin      *
115f9e3e1a3SNicholas Piggin      * It would be possible to keep the TLB in synch with the SLB by flushing
116f9e3e1a3SNicholas Piggin      * when a valid entry is overwritten by slbmte, and therefore slbia would
117f9e3e1a3SNicholas Piggin      * not have to flush unless it evicts a valid SLB entry. However it is
118f9e3e1a3SNicholas Piggin      * expected that slbmte is more common than slbia, and slbia is usually
119f9e3e1a3SNicholas Piggin      * going to evict valid SLB entries, so that tradeoff is unlikely to be a
120f9e3e1a3SNicholas Piggin      * good one.
1210418bf78SNicholas Piggin      *
1220418bf78SNicholas Piggin      * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate
1230418bf78SNicholas Piggin      * the same SLB entries (everything but entry 0), but differ in what
1240418bf78SNicholas Piggin      * "lookaside information" is invalidated. TCG can ignore this and flush
1250418bf78SNicholas Piggin      * everything.
1260418bf78SNicholas Piggin      *
1270418bf78SNicholas Piggin      * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are
1280418bf78SNicholas Piggin      * invalidated.
129f9e3e1a3SNicholas Piggin      */
130f9e3e1a3SNicholas Piggin 
1310418bf78SNicholas Piggin     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
1320418bf78SNicholas Piggin 
1330418bf78SNicholas Piggin     starting_entry = 1; /* default for IH=0,1,2,6 */
1340418bf78SNicholas Piggin 
1350418bf78SNicholas Piggin     if (env->mmu_model == POWERPC_MMU_3_00) {
1360418bf78SNicholas Piggin         switch (ih) {
1370418bf78SNicholas Piggin         case 0x7:
1380418bf78SNicholas Piggin             /* invalidate no SLBs, but all lookaside information */
1390418bf78SNicholas Piggin             return;
1400418bf78SNicholas Piggin 
1410418bf78SNicholas Piggin         case 0x3:
1420418bf78SNicholas Piggin         case 0x4:
1430418bf78SNicholas Piggin             /* also considers SLB entry 0 */
1440418bf78SNicholas Piggin             starting_entry = 0;
1450418bf78SNicholas Piggin             break;
1460418bf78SNicholas Piggin 
1470418bf78SNicholas Piggin         case 0x5:
1480418bf78SNicholas Piggin             /* treat undefined values as ih==0, and warn */
1490418bf78SNicholas Piggin             qemu_log_mask(LOG_GUEST_ERROR,
1500418bf78SNicholas Piggin                           "slbia undefined IH field %u.\n", ih);
1510418bf78SNicholas Piggin             break;
1520418bf78SNicholas Piggin 
1530418bf78SNicholas Piggin         default:
1540418bf78SNicholas Piggin             /* 0,1,2,6 */
1550418bf78SNicholas Piggin             break;
1560418bf78SNicholas Piggin         }
1570418bf78SNicholas Piggin     }
1580418bf78SNicholas Piggin 
1590418bf78SNicholas Piggin     for (n = starting_entry; n < cpu->hash64_opts->slb_size; n++) {
160fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
161fcf5ef2aSThomas Huth 
1620418bf78SNicholas Piggin         if (!(slb->esid & SLB_ESID_V)) {
1630418bf78SNicholas Piggin             continue;
1640418bf78SNicholas Piggin         }
1650418bf78SNicholas Piggin         if (env->mmu_model == POWERPC_MMU_3_00) {
1660418bf78SNicholas Piggin             if (ih == 0x3 && (slb->vsid & SLB_VSID_C) == 0) {
1670418bf78SNicholas Piggin                 /* preserves entries with a class value of 0 */
1680418bf78SNicholas Piggin                 continue;
169f9e3e1a3SNicholas Piggin             }
170f9e3e1a3SNicholas Piggin         }
171f9e3e1a3SNicholas Piggin 
1720418bf78SNicholas Piggin         slb->esid &= ~SLB_ESID_V;
1730418bf78SNicholas Piggin     }
174fcf5ef2aSThomas Huth }
175fcf5ef2aSThomas Huth 
176a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr,
177a63f1dfcSNikunj A Dadhania                            target_ulong global)
178fcf5ef2aSThomas Huth {
179db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
180fcf5ef2aSThomas Huth     ppc_slb_t *slb;
181fcf5ef2aSThomas Huth 
182fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, addr);
183fcf5ef2aSThomas Huth     if (!slb) {
184fcf5ef2aSThomas Huth         return;
185fcf5ef2aSThomas Huth     }
186fcf5ef2aSThomas Huth 
187fcf5ef2aSThomas Huth     if (slb->esid & SLB_ESID_V) {
188fcf5ef2aSThomas Huth         slb->esid &= ~SLB_ESID_V;
189fcf5ef2aSThomas Huth 
190d75cbae8SDavid Gibson         /*
191d75cbae8SDavid Gibson          * XXX: given the fact that segment size is 256 MB or 1TB,
192fcf5ef2aSThomas Huth          *      and we still don't have a tlb_flush_mask(env, n, mask)
193fcf5ef2aSThomas Huth          *      in QEMU, we just invalidate all TLBs
194fcf5ef2aSThomas Huth          */
195a63f1dfcSNikunj A Dadhania         env->tlb_need_flush |=
196a63f1dfcSNikunj A Dadhania             (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH);
197fcf5ef2aSThomas Huth     }
198fcf5ef2aSThomas Huth }
199fcf5ef2aSThomas Huth 
20043507e47SLucas Coutinho void helper_SLBIE(CPUPPCState *env, target_ulong addr)
201a63f1dfcSNikunj A Dadhania {
202a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, false);
203a63f1dfcSNikunj A Dadhania }
204a63f1dfcSNikunj A Dadhania 
205a1b05c06SLucas Coutinho void helper_SLBIEG(CPUPPCState *env, target_ulong addr)
206a63f1dfcSNikunj A Dadhania {
207a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, true);
208a63f1dfcSNikunj A Dadhania }
2092b44e219SBruno Larsen (billionai) #endif
210a63f1dfcSNikunj A Dadhania 
211fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot,
212fcf5ef2aSThomas Huth                   target_ulong esid, target_ulong vsid)
213fcf5ef2aSThomas Huth {
214fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
215fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
216b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
217fcf5ef2aSThomas Huth     int i;
218fcf5ef2aSThomas Huth 
21967d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
220fcf5ef2aSThomas Huth         return -1; /* Bad slot number */
221fcf5ef2aSThomas Huth     }
222fcf5ef2aSThomas Huth     if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) {
223fcf5ef2aSThomas Huth         return -1; /* Reserved bits set */
224fcf5ef2aSThomas Huth     }
225fcf5ef2aSThomas Huth     if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
226fcf5ef2aSThomas Huth         return -1; /* Bad segment size */
227fcf5ef2aSThomas Huth     }
22858969eeeSDavid Gibson     if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) {
229fcf5ef2aSThomas Huth         return -1; /* 1T segment on MMU that doesn't support it */
230fcf5ef2aSThomas Huth     }
231fcf5ef2aSThomas Huth 
232fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
233b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
236fcf5ef2aSThomas Huth             break;
237fcf5ef2aSThomas Huth         }
238fcf5ef2aSThomas Huth 
239fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
240fcf5ef2aSThomas Huth             sps = sps1;
241fcf5ef2aSThomas Huth             break;
242fcf5ef2aSThomas Huth         }
243fcf5ef2aSThomas Huth     }
244fcf5ef2aSThomas Huth 
245fcf5ef2aSThomas Huth     if (!sps) {
246fcf5ef2aSThomas Huth         error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
247fcf5ef2aSThomas Huth                      " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx,
248fcf5ef2aSThomas Huth                      slot, esid, vsid);
249fcf5ef2aSThomas Huth         return -1;
250fcf5ef2aSThomas Huth     }
251fcf5ef2aSThomas Huth 
252fcf5ef2aSThomas Huth     slb->esid = esid;
253fcf5ef2aSThomas Huth     slb->vsid = vsid;
254fcf5ef2aSThomas Huth     slb->sps = sps;
255fcf5ef2aSThomas Huth 
25676134d48SSuraj Jitindar Singh     LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx
25776134d48SSuraj Jitindar Singh             " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid,
258fcf5ef2aSThomas Huth             slb->esid, slb->vsid);
259fcf5ef2aSThomas Huth 
260fcf5ef2aSThomas Huth     return 0;
261fcf5ef2aSThomas Huth }
262fcf5ef2aSThomas Huth 
2632b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
264fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb,
265fcf5ef2aSThomas Huth                              target_ulong *rt)
266fcf5ef2aSThomas Huth {
267fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
268fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
269fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
270fcf5ef2aSThomas Huth 
27167d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
272fcf5ef2aSThomas Huth         return -1;
273fcf5ef2aSThomas Huth     }
274fcf5ef2aSThomas Huth 
275fcf5ef2aSThomas Huth     *rt = slb->esid;
276fcf5ef2aSThomas Huth     return 0;
277fcf5ef2aSThomas Huth }
278fcf5ef2aSThomas Huth 
279fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
280fcf5ef2aSThomas Huth                              target_ulong *rt)
281fcf5ef2aSThomas Huth {
282fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
283fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
284fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
285fcf5ef2aSThomas Huth 
28667d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
287fcf5ef2aSThomas Huth         return -1;
288fcf5ef2aSThomas Huth     }
289fcf5ef2aSThomas Huth 
290fcf5ef2aSThomas Huth     *rt = slb->vsid;
291fcf5ef2aSThomas Huth     return 0;
292fcf5ef2aSThomas Huth }
293fcf5ef2aSThomas Huth 
294fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
295fcf5ef2aSThomas Huth                              target_ulong *rt)
296fcf5ef2aSThomas Huth {
297fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
298fcf5ef2aSThomas Huth     ppc_slb_t *slb;
299fcf5ef2aSThomas Huth 
300fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, env->msr)) {
301fcf5ef2aSThomas Huth         rb &= 0xffffffff;
302fcf5ef2aSThomas Huth     }
303fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, rb);
304fcf5ef2aSThomas Huth     if (slb == NULL) {
305fcf5ef2aSThomas Huth         *rt = (target_ulong)-1ul;
306fcf5ef2aSThomas Huth     } else {
307fcf5ef2aSThomas Huth         *rt = slb->vsid;
308fcf5ef2aSThomas Huth     }
309fcf5ef2aSThomas Huth     return 0;
310fcf5ef2aSThomas Huth }
311fcf5ef2aSThomas Huth 
3120b0ba40fSLucas Coutinho void helper_SLBMTE(CPUPPCState *env, target_ulong rb, target_ulong rs)
313fcf5ef2aSThomas Huth {
314db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
315fcf5ef2aSThomas Huth 
316fcf5ef2aSThomas Huth     if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) {
317fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
318fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
319fcf5ef2aSThomas Huth     }
320fcf5ef2aSThomas Huth }
321fcf5ef2aSThomas Huth 
32241b60e46SLucas Coutinho target_ulong helper_SLBMFEE(CPUPPCState *env, target_ulong rb)
323fcf5ef2aSThomas Huth {
324db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
325fcf5ef2aSThomas Huth     target_ulong rt = 0;
326fcf5ef2aSThomas Huth 
327fcf5ef2aSThomas Huth     if (ppc_load_slb_esid(cpu, rb, &rt) < 0) {
328fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
329fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
330fcf5ef2aSThomas Huth     }
331fcf5ef2aSThomas Huth     return rt;
332fcf5ef2aSThomas Huth }
333fcf5ef2aSThomas Huth 
334*26d02c9dSLucas Coutinho target_ulong helper_SLBFEE(CPUPPCState *env, target_ulong rb)
335fcf5ef2aSThomas Huth {
336db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
337fcf5ef2aSThomas Huth     target_ulong rt = 0;
338fcf5ef2aSThomas Huth 
339fcf5ef2aSThomas Huth     if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) {
340fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
341fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
342fcf5ef2aSThomas Huth     }
343fcf5ef2aSThomas Huth     return rt;
344fcf5ef2aSThomas Huth }
345fcf5ef2aSThomas Huth 
34674a15384SLucas Coutinho target_ulong helper_SLBMFEV(CPUPPCState *env, target_ulong rb)
347fcf5ef2aSThomas Huth {
348db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
349fcf5ef2aSThomas Huth     target_ulong rt = 0;
350fcf5ef2aSThomas Huth 
351fcf5ef2aSThomas Huth     if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) {
352fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
353fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
354fcf5ef2aSThomas Huth     }
355fcf5ef2aSThomas Huth     return rt;
356fcf5ef2aSThomas Huth }
3572b44e219SBruno Larsen (billionai) #endif
358fcf5ef2aSThomas Huth 
35907a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */
36007a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu,
36107a68f99SSuraj Jitindar Singh                                               ppc_hash_pte64_t pte)
36207a68f99SSuraj Jitindar Singh {
36307a68f99SSuraj Jitindar Singh     /* Exec permissions CANNOT take away read or write permissions */
36407a68f99SSuraj Jitindar Singh     return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ?
36507a68f99SSuraj Jitindar Singh             PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC;
36607a68f99SSuraj Jitindar Singh }
36707a68f99SSuraj Jitindar Singh 
36807a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */
36903695a98SBruno Larsen (billionai) static int ppc_hash64_pte_prot(int mmu_idx,
370fcf5ef2aSThomas Huth                                ppc_slb_t *slb, ppc_hash_pte64_t pte)
371fcf5ef2aSThomas Huth {
372fcf5ef2aSThomas Huth     unsigned pp, key;
373d75cbae8SDavid Gibson     /*
374d75cbae8SDavid Gibson      * Some pp bit combinations have undefined behaviour, so default
375d75cbae8SDavid Gibson      * to no access in those cases
376d75cbae8SDavid Gibson      */
377fcf5ef2aSThomas Huth     int prot = 0;
378fcf5ef2aSThomas Huth 
37903695a98SBruno Larsen (billionai)     key = !!(mmuidx_pr(mmu_idx) ? (slb->vsid & SLB_VSID_KP)
380fcf5ef2aSThomas Huth              : (slb->vsid & SLB_VSID_KS));
381fcf5ef2aSThomas Huth     pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
382fcf5ef2aSThomas Huth 
383fcf5ef2aSThomas Huth     if (key == 0) {
384fcf5ef2aSThomas Huth         switch (pp) {
385fcf5ef2aSThomas Huth         case 0x0:
386fcf5ef2aSThomas Huth         case 0x1:
387fcf5ef2aSThomas Huth         case 0x2:
388347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
389fcf5ef2aSThomas Huth             break;
390fcf5ef2aSThomas Huth 
391fcf5ef2aSThomas Huth         case 0x3:
392fcf5ef2aSThomas Huth         case 0x6:
393347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
394fcf5ef2aSThomas Huth             break;
395fcf5ef2aSThomas Huth         }
396fcf5ef2aSThomas Huth     } else {
397fcf5ef2aSThomas Huth         switch (pp) {
398fcf5ef2aSThomas Huth         case 0x0:
399fcf5ef2aSThomas Huth         case 0x6:
400fcf5ef2aSThomas Huth             break;
401fcf5ef2aSThomas Huth 
402fcf5ef2aSThomas Huth         case 0x1:
403fcf5ef2aSThomas Huth         case 0x3:
404347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
405fcf5ef2aSThomas Huth             break;
406fcf5ef2aSThomas Huth 
407fcf5ef2aSThomas Huth         case 0x2:
408347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
409fcf5ef2aSThomas Huth             break;
410fcf5ef2aSThomas Huth         }
411fcf5ef2aSThomas Huth     }
412fcf5ef2aSThomas Huth 
413fcf5ef2aSThomas Huth     return prot;
414fcf5ef2aSThomas Huth }
415fcf5ef2aSThomas Huth 
416a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */
417a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key)
418a6152b52SSuraj Jitindar Singh {
419a6152b52SSuraj Jitindar Singh     CPUPPCState *env = &cpu->env;
420a6152b52SSuraj Jitindar Singh     int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3;
421a6152b52SSuraj Jitindar Singh 
422a6152b52SSuraj Jitindar Singh     /*
423a6152b52SSuraj Jitindar Singh      * An instruction fetch is permitted if the IAMR bit is 0.
424a6152b52SSuraj Jitindar Singh      * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
425a6152b52SSuraj Jitindar Singh      * can only take away EXEC permissions not READ or WRITE permissions.
426a6152b52SSuraj Jitindar Singh      * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
427a6152b52SSuraj Jitindar Singh      * EXEC permissions are allowed.
428a6152b52SSuraj Jitindar Singh      */
429a6152b52SSuraj Jitindar Singh     return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE :
430a6152b52SSuraj Jitindar Singh                                PAGE_READ | PAGE_WRITE | PAGE_EXEC;
431a6152b52SSuraj Jitindar Singh }
432a6152b52SSuraj Jitindar Singh 
433fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte)
434fcf5ef2aSThomas Huth {
435fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
436fcf5ef2aSThomas Huth     int key, amrbits;
437fcf5ef2aSThomas Huth     int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
438fcf5ef2aSThomas Huth 
439fcf5ef2aSThomas Huth     /* Only recent MMUs implement Virtual Page Class Key Protection */
44058969eeeSDavid Gibson     if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) {
441fcf5ef2aSThomas Huth         return prot;
442fcf5ef2aSThomas Huth     }
443fcf5ef2aSThomas Huth 
444fcf5ef2aSThomas Huth     key = HPTE64_R_KEY(pte.pte1);
445fcf5ef2aSThomas Huth     amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3;
446fcf5ef2aSThomas Huth 
447fcf5ef2aSThomas Huth     /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
448fcf5ef2aSThomas Huth     /*         env->spr[SPR_AMR]); */
449fcf5ef2aSThomas Huth 
450fcf5ef2aSThomas Huth     /*
451fcf5ef2aSThomas Huth      * A store is permitted if the AMR bit is 0. Remove write
452fcf5ef2aSThomas Huth      * protection if it is set.
453fcf5ef2aSThomas Huth      */
454fcf5ef2aSThomas Huth     if (amrbits & 0x2) {
455fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
456fcf5ef2aSThomas Huth     }
457fcf5ef2aSThomas Huth     /*
458fcf5ef2aSThomas Huth      * A load is permitted if the AMR bit is 0. Remove read
459fcf5ef2aSThomas Huth      * protection if it is set.
460fcf5ef2aSThomas Huth      */
461fcf5ef2aSThomas Huth     if (amrbits & 0x1) {
462fcf5ef2aSThomas Huth         prot &= ~PAGE_READ;
463fcf5ef2aSThomas Huth     }
464fcf5ef2aSThomas Huth 
465a6152b52SSuraj Jitindar Singh     switch (env->mmu_model) {
466a6152b52SSuraj Jitindar Singh     /*
467a6152b52SSuraj Jitindar Singh      * MMU version 2.07 and later support IAMR
468a6152b52SSuraj Jitindar Singh      * Check if the IAMR allows the instruction access - it will return
469a6152b52SSuraj Jitindar Singh      * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
470a6152b52SSuraj Jitindar Singh      * if it does (and prot will be unchanged indicating execution support).
471a6152b52SSuraj Jitindar Singh      */
472a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_2_07:
473a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_3_00:
474a6152b52SSuraj Jitindar Singh         prot &= ppc_hash64_iamr_prot(cpu, key);
475a6152b52SSuraj Jitindar Singh         break;
476a6152b52SSuraj Jitindar Singh     default:
477a6152b52SSuraj Jitindar Singh         break;
478a6152b52SSuraj Jitindar Singh     }
479a6152b52SSuraj Jitindar Singh 
480fcf5ef2aSThomas Huth     return prot;
481fcf5ef2aSThomas Huth }
482fcf5ef2aSThomas Huth 
4837222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu,
4847222b94aSDavid Gibson                                              hwaddr ptex, int n)
485fcf5ef2aSThomas Huth {
4867222b94aSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
4873367c62fSBenjamin Herrenschmidt     hwaddr base;
4887222b94aSDavid Gibson     hwaddr plen = n * HASH_PTE_SIZE_64;
489e57ca75cSDavid Gibson     const ppc_hash_pte64_t *hptes;
490e57ca75cSDavid Gibson 
491e57ca75cSDavid Gibson     if (cpu->vhyp) {
492e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
493e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
494e57ca75cSDavid Gibson         return vhc->map_hptes(cpu->vhyp, ptex, n);
495e57ca75cSDavid Gibson     }
4963367c62fSBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
497e57ca75cSDavid Gibson 
498e57ca75cSDavid Gibson     if (!base) {
499e57ca75cSDavid Gibson         return NULL;
500e57ca75cSDavid Gibson     }
501e57ca75cSDavid Gibson 
502f26404fbSPeter Maydell     hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false,
503f26404fbSPeter Maydell                               MEMTXATTRS_UNSPECIFIED);
5047222b94aSDavid Gibson     if (plen < (n * HASH_PTE_SIZE_64)) {
5057222b94aSDavid Gibson         hw_error("%s: Unable to map all requested HPTEs\n", __func__);
506fcf5ef2aSThomas Huth     }
5077222b94aSDavid Gibson     return hptes;
508fcf5ef2aSThomas Huth }
509fcf5ef2aSThomas Huth 
5107222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes,
5117222b94aSDavid Gibson                             hwaddr ptex, int n)
512fcf5ef2aSThomas Huth {
513e57ca75cSDavid Gibson     if (cpu->vhyp) {
514e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
515e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
516e57ca75cSDavid Gibson         vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n);
517e57ca75cSDavid Gibson         return;
518e57ca75cSDavid Gibson     }
519e57ca75cSDavid Gibson 
5207222b94aSDavid Gibson     address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64,
5217222b94aSDavid Gibson                         false, n * HASH_PTE_SIZE_64);
522fcf5ef2aSThomas Huth }
523fcf5ef2aSThomas Huth 
524b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps,
525fcf5ef2aSThomas Huth                                 uint64_t pte0, uint64_t pte1)
526fcf5ef2aSThomas Huth {
527fcf5ef2aSThomas Huth     int i;
528fcf5ef2aSThomas Huth 
529fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
530fcf5ef2aSThomas Huth         if (sps->page_shift != 12) {
531fcf5ef2aSThomas Huth             /* 4kiB page in a non 4kiB segment */
532fcf5ef2aSThomas Huth             return 0;
533fcf5ef2aSThomas Huth         }
534fcf5ef2aSThomas Huth         /* Normal 4kiB page */
535fcf5ef2aSThomas Huth         return 12;
536fcf5ef2aSThomas Huth     }
537fcf5ef2aSThomas Huth 
538fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
539b07c59f7SDavid Gibson         const PPCHash64PageSize *ps = &sps->enc[i];
540fcf5ef2aSThomas Huth         uint64_t mask;
541fcf5ef2aSThomas Huth 
542fcf5ef2aSThomas Huth         if (!ps->page_shift) {
543fcf5ef2aSThomas Huth             break;
544fcf5ef2aSThomas Huth         }
545fcf5ef2aSThomas Huth 
546fcf5ef2aSThomas Huth         if (ps->page_shift == 12) {
547fcf5ef2aSThomas Huth             /* L bit is set so this can't be a 4kiB page */
548fcf5ef2aSThomas Huth             continue;
549fcf5ef2aSThomas Huth         }
550fcf5ef2aSThomas Huth 
551fcf5ef2aSThomas Huth         mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
552fcf5ef2aSThomas Huth 
553fcf5ef2aSThomas Huth         if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
554fcf5ef2aSThomas Huth             return ps->page_shift;
555fcf5ef2aSThomas Huth         }
556fcf5ef2aSThomas Huth     }
557fcf5ef2aSThomas Huth 
558fcf5ef2aSThomas Huth     return 0; /* Bad page size encoding */
559fcf5ef2aSThomas Huth }
560fcf5ef2aSThomas Huth 
56134525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1)
56234525595SBenjamin Herrenschmidt {
56334525595SBenjamin Herrenschmidt     /* Insert B into pte0 */
56434525595SBenjamin Herrenschmidt     *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) |
56534525595SBenjamin Herrenschmidt             ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) <<
56634525595SBenjamin Herrenschmidt              (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT));
56734525595SBenjamin Herrenschmidt 
56834525595SBenjamin Herrenschmidt     /* Remove B from pte1 */
56934525595SBenjamin Herrenschmidt     *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK;
57034525595SBenjamin Herrenschmidt }
57134525595SBenjamin Herrenschmidt 
57234525595SBenjamin Herrenschmidt 
573fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
574b07c59f7SDavid Gibson                                      const PPCHash64SegmentPageSizes *sps,
575fcf5ef2aSThomas Huth                                      target_ulong ptem,
576fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
577fcf5ef2aSThomas Huth {
578fcf5ef2aSThomas Huth     int i;
5797222b94aSDavid Gibson     const ppc_hash_pte64_t *pteg;
580fcf5ef2aSThomas Huth     target_ulong pte0, pte1;
5817222b94aSDavid Gibson     target_ulong ptex;
582fcf5ef2aSThomas Huth 
58336778660SDavid Gibson     ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP;
5847222b94aSDavid Gibson     pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
5857222b94aSDavid Gibson     if (!pteg) {
586fcf5ef2aSThomas Huth         return -1;
587fcf5ef2aSThomas Huth     }
588fcf5ef2aSThomas Huth     for (i = 0; i < HPTES_PER_GROUP; i++) {
5897222b94aSDavid Gibson         pte0 = ppc_hash64_hpte0(cpu, pteg, i);
5903054b0caSBenjamin Herrenschmidt         /*
5913054b0caSBenjamin Herrenschmidt          * pte0 contains the valid bit and must be read before pte1,
5923054b0caSBenjamin Herrenschmidt          * otherwise we might see an old pte1 with a new valid bit and
5933054b0caSBenjamin Herrenschmidt          * thus an inconsistent hpte value
5943054b0caSBenjamin Herrenschmidt          */
5953054b0caSBenjamin Herrenschmidt         smp_rmb();
5967222b94aSDavid Gibson         pte1 = ppc_hash64_hpte1(cpu, pteg, i);
597fcf5ef2aSThomas Huth 
59834525595SBenjamin Herrenschmidt         /* Convert format if necessary */
59934525595SBenjamin Herrenschmidt         if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) {
60034525595SBenjamin Herrenschmidt             ppc64_v3_new_to_old_hpte(&pte0, &pte1);
60134525595SBenjamin Herrenschmidt         }
60234525595SBenjamin Herrenschmidt 
603fcf5ef2aSThomas Huth         /* This compares V, B, H (secondary) and the AVPN */
604fcf5ef2aSThomas Huth         if (HPTE64_V_COMPARE(pte0, ptem)) {
605fcf5ef2aSThomas Huth             *pshift = hpte_page_shift(sps, pte0, pte1);
606fcf5ef2aSThomas Huth             /*
607fcf5ef2aSThomas Huth              * If there is no match, ignore the PTE, it could simply
608fcf5ef2aSThomas Huth              * be for a different segment size encoding and the
609fcf5ef2aSThomas Huth              * architecture specifies we should not match. Linux will
610fcf5ef2aSThomas Huth              * potentially leave behind PTEs for the wrong base page
611fcf5ef2aSThomas Huth              * size when demoting segments.
612fcf5ef2aSThomas Huth              */
613fcf5ef2aSThomas Huth             if (*pshift == 0) {
614fcf5ef2aSThomas Huth                 continue;
615fcf5ef2aSThomas Huth             }
616d75cbae8SDavid Gibson             /*
617d75cbae8SDavid Gibson              * We don't do anything with pshift yet as qemu TLB only
618d75cbae8SDavid Gibson              * deals with 4K pages anyway
619fcf5ef2aSThomas Huth              */
620fcf5ef2aSThomas Huth             pte->pte0 = pte0;
621fcf5ef2aSThomas Huth             pte->pte1 = pte1;
6227222b94aSDavid Gibson             ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
6237222b94aSDavid Gibson             return ptex + i;
624fcf5ef2aSThomas Huth         }
625fcf5ef2aSThomas Huth     }
6267222b94aSDavid Gibson     ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
627fcf5ef2aSThomas Huth     /*
628fcf5ef2aSThomas Huth      * We didn't find a valid entry.
629fcf5ef2aSThomas Huth      */
630fcf5ef2aSThomas Huth     return -1;
631fcf5ef2aSThomas Huth }
632fcf5ef2aSThomas Huth 
633fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
634fcf5ef2aSThomas Huth                                      ppc_slb_t *slb, target_ulong eaddr,
635fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
636fcf5ef2aSThomas Huth {
637fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
6387222b94aSDavid Gibson     hwaddr hash, ptex;
639fcf5ef2aSThomas Huth     uint64_t vsid, epnmask, epn, ptem;
640b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = slb->sps;
641fcf5ef2aSThomas Huth 
642d75cbae8SDavid Gibson     /*
643d75cbae8SDavid Gibson      * The SLB store path should prevent any bad page size encodings
644d75cbae8SDavid Gibson      * getting in there, so:
645d75cbae8SDavid Gibson      */
646fcf5ef2aSThomas Huth     assert(sps);
647fcf5ef2aSThomas Huth 
648fcf5ef2aSThomas Huth     /* If ISL is set in LPCR we need to clamp the page size to 4K */
649fcf5ef2aSThomas Huth     if (env->spr[SPR_LPCR] & LPCR_ISL) {
650fcf5ef2aSThomas Huth         /* We assume that when using TCG, 4k is first entry of SPS */
651b07c59f7SDavid Gibson         sps = &cpu->hash64_opts->sps[0];
652fcf5ef2aSThomas Huth         assert(sps->page_shift == 12);
653fcf5ef2aSThomas Huth     }
654fcf5ef2aSThomas Huth 
655fcf5ef2aSThomas Huth     epnmask = ~((1ULL << sps->page_shift) - 1);
656fcf5ef2aSThomas Huth 
657fcf5ef2aSThomas Huth     if (slb->vsid & SLB_VSID_B) {
658fcf5ef2aSThomas Huth         /* 1TB segment */
659fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
660fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
661fcf5ef2aSThomas Huth         hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
662fcf5ef2aSThomas Huth     } else {
663fcf5ef2aSThomas Huth         /* 256M segment */
664fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
665fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
666fcf5ef2aSThomas Huth         hash = vsid ^ (epn >> sps->page_shift);
667fcf5ef2aSThomas Huth     }
668fcf5ef2aSThomas Huth     ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
669fcf5ef2aSThomas Huth     ptem |= HPTE64_V_VALID;
670fcf5ef2aSThomas Huth 
671fcf5ef2aSThomas Huth     /* Page address translation */
672fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
673fcf5ef2aSThomas Huth             "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
674fcf5ef2aSThomas Huth             " hash " TARGET_FMT_plx "\n",
67536778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash);
676fcf5ef2aSThomas Huth 
677fcf5ef2aSThomas Huth     /* Primary PTEG lookup */
678fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
679fcf5ef2aSThomas Huth             "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
680fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
681fcf5ef2aSThomas Huth             " hash=" TARGET_FMT_plx "\n",
68236778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu),
68336778660SDavid Gibson             vsid, ptem,  hash);
6847222b94aSDavid Gibson     ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
685fcf5ef2aSThomas Huth 
6867222b94aSDavid Gibson     if (ptex == -1) {
687fcf5ef2aSThomas Huth         /* Secondary PTEG lookup */
688fcf5ef2aSThomas Huth         ptem |= HPTE64_V_SECONDARY;
689fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU,
690fcf5ef2aSThomas Huth                 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
691fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
69236778660SDavid Gibson                 " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu),
69336778660SDavid Gibson                 ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash);
694fcf5ef2aSThomas Huth 
6957222b94aSDavid Gibson         ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
696fcf5ef2aSThomas Huth     }
697fcf5ef2aSThomas Huth 
6987222b94aSDavid Gibson     return ptex;
699fcf5ef2aSThomas Huth }
700fcf5ef2aSThomas Huth 
701fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
702fcf5ef2aSThomas Huth                                           uint64_t pte0, uint64_t pte1)
703fcf5ef2aSThomas Huth {
704fcf5ef2aSThomas Huth     int i;
705fcf5ef2aSThomas Huth 
706fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
707fcf5ef2aSThomas Huth         return 12;
708fcf5ef2aSThomas Huth     }
709fcf5ef2aSThomas Huth 
710fcf5ef2aSThomas Huth     /*
711fcf5ef2aSThomas Huth      * The encodings in env->sps need to be carefully chosen so that
712fcf5ef2aSThomas Huth      * this gives an unambiguous result.
713fcf5ef2aSThomas Huth      */
714fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
715b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
716fcf5ef2aSThomas Huth         unsigned shift;
717fcf5ef2aSThomas Huth 
718fcf5ef2aSThomas Huth         if (!sps->page_shift) {
719fcf5ef2aSThomas Huth             break;
720fcf5ef2aSThomas Huth         }
721fcf5ef2aSThomas Huth 
722fcf5ef2aSThomas Huth         shift = hpte_page_shift(sps, pte0, pte1);
723fcf5ef2aSThomas Huth         if (shift) {
724fcf5ef2aSThomas Huth             return shift;
725fcf5ef2aSThomas Huth         }
726fcf5ef2aSThomas Huth     }
727fcf5ef2aSThomas Huth 
728fcf5ef2aSThomas Huth     return 0;
729fcf5ef2aSThomas Huth }
730fcf5ef2aSThomas Huth 
7311b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env)
7321b99e029SDavid Gibson {
7331b99e029SDavid Gibson     switch (env->mmu_model) {
7341b99e029SDavid Gibson     case POWERPC_MMU_3_00:
7351b99e029SDavid Gibson         /*
7361b99e029SDavid Gibson          * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR
7371b99e029SDavid Gibson          * register no longer exist
7381b99e029SDavid Gibson          */
7391b99e029SDavid Gibson         return true;
7401b99e029SDavid Gibson 
7411b99e029SDavid Gibson     default:
7421b99e029SDavid Gibson         return !!(env->spr[SPR_LPCR] & LPCR_VPM0);
7431b99e029SDavid Gibson     }
7441b99e029SDavid Gibson }
7451b99e029SDavid Gibson 
74603695a98SBruno Larsen (billionai) static void ppc_hash64_set_isi(CPUState *cs, int mmu_idx, uint64_t error_code)
747fcf5ef2aSThomas Huth {
7488fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
749fcf5ef2aSThomas Huth     bool vpm;
750fcf5ef2aSThomas Huth 
75103695a98SBruno Larsen (billionai)     if (!mmuidx_real(mmu_idx)) {
752fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
753fcf5ef2aSThomas Huth     } else {
7541b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
755fcf5ef2aSThomas Huth     }
75603695a98SBruno Larsen (billionai)     if (vpm && !mmuidx_hv(mmu_idx)) {
757fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HISI;
758fcf5ef2aSThomas Huth     } else {
759fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
760fcf5ef2aSThomas Huth     }
761fcf5ef2aSThomas Huth     env->error_code = error_code;
762fcf5ef2aSThomas Huth }
763fcf5ef2aSThomas Huth 
76403695a98SBruno Larsen (billionai) static void ppc_hash64_set_dsi(CPUState *cs, int mmu_idx, uint64_t dar, uint64_t dsisr)
765fcf5ef2aSThomas Huth {
7668fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
767fcf5ef2aSThomas Huth     bool vpm;
768fcf5ef2aSThomas Huth 
76903695a98SBruno Larsen (billionai)     if (!mmuidx_real(mmu_idx)) {
770fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
771fcf5ef2aSThomas Huth     } else {
7721b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
773fcf5ef2aSThomas Huth     }
77403695a98SBruno Larsen (billionai)     if (vpm && !mmuidx_hv(mmu_idx)) {
775fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
776fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
777fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
778fcf5ef2aSThomas Huth     } else {
779fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
780fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
781fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
782fcf5ef2aSThomas Huth    }
783fcf5ef2aSThomas Huth     env->error_code = 0;
784fcf5ef2aSThomas Huth }
785fcf5ef2aSThomas Huth 
786fcf5ef2aSThomas Huth 
787a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
788a2dd4e83SBenjamin Herrenschmidt {
7897bf00dfbSLeandro Lupori     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_R;
790a2dd4e83SBenjamin Herrenschmidt 
791a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
792a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
793a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
794a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_r(cpu->vhyp, ptex, pte1);
795a2dd4e83SBenjamin Herrenschmidt         return;
796a2dd4e83SBenjamin Herrenschmidt     }
797a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
798a2dd4e83SBenjamin Herrenschmidt 
799a2dd4e83SBenjamin Herrenschmidt 
800a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
801a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01);
802a2dd4e83SBenjamin Herrenschmidt }
803a2dd4e83SBenjamin Herrenschmidt 
804a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
805a2dd4e83SBenjamin Herrenschmidt {
8067bf00dfbSLeandro Lupori     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_C;
807a2dd4e83SBenjamin Herrenschmidt 
808a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
809a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
810a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
811a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_c(cpu->vhyp, ptex, pte1);
812a2dd4e83SBenjamin Herrenschmidt         return;
813a2dd4e83SBenjamin Herrenschmidt     }
814a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
815a2dd4e83SBenjamin Herrenschmidt 
816a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
817a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80);
818a2dd4e83SBenjamin Herrenschmidt }
819a2dd4e83SBenjamin Herrenschmidt 
820a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu)
821a864a6b3SDavid Gibson {
822a864a6b3SDavid Gibson     CPUPPCState *env = &cpu->env;
823a864a6b3SDavid Gibson     /*
824d37b40daSDavid Gibson      * In theory the meanings of RMLS values are implementation
825d37b40daSDavid Gibson      * dependent.  In practice, this seems to have been the set from
826d37b40daSDavid Gibson      * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
827a864a6b3SDavid Gibson      *
828a864a6b3SDavid Gibson      * Unsupported values mean the OS has shot itself in the
829a864a6b3SDavid Gibson      * foot. Return a 0-sized RMA in this case, which we expect
830a864a6b3SDavid Gibson      * to trigger an immediate DSI or ISI
831a864a6b3SDavid Gibson      */
832a864a6b3SDavid Gibson     static const target_ulong rma_sizes[16] = {
833d37b40daSDavid Gibson         [0] = 256 * GiB,
834a864a6b3SDavid Gibson         [1] = 16 * GiB,
835a864a6b3SDavid Gibson         [2] = 1 * GiB,
836a864a6b3SDavid Gibson         [3] = 64 * MiB,
837a864a6b3SDavid Gibson         [4] = 256 * MiB,
838a864a6b3SDavid Gibson         [7] = 128 * MiB,
839a864a6b3SDavid Gibson         [8] = 32 * MiB,
840a864a6b3SDavid Gibson     };
841a864a6b3SDavid Gibson     target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT;
842a864a6b3SDavid Gibson 
843a864a6b3SDavid Gibson     return rma_sizes[rmls];
844a864a6b3SDavid Gibson }
845a864a6b3SDavid Gibson 
8464c24a87fSDavid Gibson static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb)
8474c24a87fSDavid Gibson {
8484c24a87fSDavid Gibson     CPUPPCState *env = &cpu->env;
8494c24a87fSDavid Gibson     target_ulong lpcr = env->spr[SPR_LPCR];
8504c24a87fSDavid Gibson     uint32_t vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
8514c24a87fSDavid Gibson     target_ulong vsid = SLB_VSID_VRMA | ((vrmasd << 4) & SLB_VSID_LLP_MASK);
8524c24a87fSDavid Gibson     int i;
8534c24a87fSDavid Gibson 
8544c24a87fSDavid Gibson     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
8554c24a87fSDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
8564c24a87fSDavid Gibson 
8574c24a87fSDavid Gibson         if (!sps->page_shift) {
8584c24a87fSDavid Gibson             break;
8594c24a87fSDavid Gibson         }
8604c24a87fSDavid Gibson 
8614c24a87fSDavid Gibson         if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) {
8624c24a87fSDavid Gibson             slb->esid = SLB_ESID_V;
8634c24a87fSDavid Gibson             slb->vsid = vsid;
8644c24a87fSDavid Gibson             slb->sps = sps;
8654c24a87fSDavid Gibson             return 0;
8664c24a87fSDavid Gibson         }
8674c24a87fSDavid Gibson     }
8684c24a87fSDavid Gibson 
8694c24a87fSDavid Gibson     error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x"
870ff5b5d5bSMarkus Armbruster                  TARGET_FMT_lx, lpcr);
8714c24a87fSDavid Gibson 
8724c24a87fSDavid Gibson     return -1;
8734c24a87fSDavid Gibson }
8744c24a87fSDavid Gibson 
87551806b54SRichard Henderson bool ppc_hash64_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type,
87603695a98SBruno Larsen (billionai)                       hwaddr *raddrp, int *psizep, int *protp, int mmu_idx,
8771a8c647bSRichard Henderson                       bool guest_visible)
878fcf5ef2aSThomas Huth {
879fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
880fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
8814c24a87fSDavid Gibson     ppc_slb_t vrma_slbe;
882fcf5ef2aSThomas Huth     ppc_slb_t *slb;
883fcf5ef2aSThomas Huth     unsigned apshift;
8847222b94aSDavid Gibson     hwaddr ptex;
885fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
88607a68f99SSuraj Jitindar Singh     int exec_prot, pp_prot, amr_prot, prot;
887182357dbSRichard Henderson     int need_prot;
888fcf5ef2aSThomas Huth     hwaddr raddr;
889fcf5ef2aSThomas Huth 
890d75cbae8SDavid Gibson     /*
891d75cbae8SDavid Gibson      * Note on LPCR usage: 970 uses HID4, but our special variant of
892d75cbae8SDavid Gibson      * store_spr copies relevant fields into env->spr[SPR_LPCR].
893136fbf65Szhaolichang      * Similarly we filter unimplemented bits when storing into LPCR
894d75cbae8SDavid Gibson      * depending on the MMU version. This code can thus just use the
895d75cbae8SDavid Gibson      * LPCR "as-is".
896fcf5ef2aSThomas Huth      */
897fcf5ef2aSThomas Huth 
898fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
89903695a98SBruno Larsen (billionai)     if (mmuidx_real(mmu_idx)) {
900d75cbae8SDavid Gibson         /*
901d75cbae8SDavid Gibson          * Translation is supposedly "off", but in real mode the top 4
902d75cbae8SDavid Gibson          * effective address bits are (mostly) ignored
903d75cbae8SDavid Gibson          */
904fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
905fcf5ef2aSThomas Huth 
906682c1dfbSDavid Gibson         if (cpu->vhyp) {
907682c1dfbSDavid Gibson             /*
908682c1dfbSDavid Gibson              * In virtual hypervisor mode, there's nothing to do:
909682c1dfbSDavid Gibson              *   EA == GPA == qemu guest address
910682c1dfbSDavid Gibson              */
91103695a98SBruno Larsen (billionai)         } else if (mmuidx_hv(mmu_idx) || !env->has_hv_mode) {
912fcf5ef2aSThomas Huth             /* In HV mode, add HRMOR if top EA bit is clear */
913fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
914fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
915fcf5ef2aSThomas Huth             }
9161b99e029SDavid Gibson         } else if (ppc_hash64_use_vrma(env)) {
917682c1dfbSDavid Gibson             /* Emulated VRMA mode */
9184c24a87fSDavid Gibson             slb = &vrma_slbe;
9194c24a87fSDavid Gibson             if (build_vrma_slbe(cpu, slb) != 0) {
920682c1dfbSDavid Gibson                 /* Invalid VRMA setup, machine check */
9211a8c647bSRichard Henderson                 if (guest_visible) {
922fcf5ef2aSThomas Huth                     cs->exception_index = POWERPC_EXCP_MCHECK;
923fcf5ef2aSThomas Huth                     env->error_code = 0;
9241a8c647bSRichard Henderson                 }
9251a8c647bSRichard Henderson                 return false;
926682c1dfbSDavid Gibson             }
927682c1dfbSDavid Gibson 
928682c1dfbSDavid Gibson             goto skip_slb_search;
929fcf5ef2aSThomas Huth         } else {
9303a56a55cSDavid Gibson             target_ulong limit = rmls_limit(cpu);
9313a56a55cSDavid Gibson 
932682c1dfbSDavid Gibson             /* Emulated old-style RMO mode, bounds check against RMLS */
9333a56a55cSDavid Gibson             if (raddr >= limit) {
9341a8c647bSRichard Henderson                 if (!guest_visible) {
9351a8c647bSRichard Henderson                     return false;
9361a8c647bSRichard Henderson                 }
93759dec5bfSRichard Henderson                 switch (access_type) {
93859dec5bfSRichard Henderson                 case MMU_INST_FETCH:
93903695a98SBruno Larsen (billionai)                     ppc_hash64_set_isi(cs, mmu_idx, SRR1_PROTFAULT);
94059dec5bfSRichard Henderson                     break;
94159dec5bfSRichard Henderson                 case MMU_DATA_LOAD:
94203695a98SBruno Larsen (billionai)                     ppc_hash64_set_dsi(cs, mmu_idx, eaddr, DSISR_PROTFAULT);
94359dec5bfSRichard Henderson                     break;
94459dec5bfSRichard Henderson                 case MMU_DATA_STORE:
94503695a98SBruno Larsen (billionai)                     ppc_hash64_set_dsi(cs, mmu_idx, eaddr,
94659dec5bfSRichard Henderson                                        DSISR_PROTFAULT | DSISR_ISSTORE);
94759dec5bfSRichard Henderson                     break;
94859dec5bfSRichard Henderson                 default:
94959dec5bfSRichard Henderson                     g_assert_not_reached();
950fcf5ef2aSThomas Huth                 }
9511a8c647bSRichard Henderson                 return false;
952fcf5ef2aSThomas Huth             }
953682c1dfbSDavid Gibson 
954682c1dfbSDavid Gibson             raddr |= env->spr[SPR_RMOR];
955fcf5ef2aSThomas Huth         }
9561a8c647bSRichard Henderson 
9571a8c647bSRichard Henderson         *raddrp = raddr;
9581a8c647bSRichard Henderson         *protp = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9591a8c647bSRichard Henderson         *psizep = TARGET_PAGE_BITS;
9601a8c647bSRichard Henderson         return true;
961fcf5ef2aSThomas Huth     }
962fcf5ef2aSThomas Huth 
963fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
964fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
965fcf5ef2aSThomas Huth     if (!slb) {
966b2899495SSuraj Jitindar Singh         /* No entry found, check if in-memory segment tables are in use */
967ca79b3b7SDavid Gibson         if (ppc64_use_proc_tbl(cpu)) {
968b2899495SSuraj Jitindar Singh             /* TODO - Unsupported */
969b2899495SSuraj Jitindar Singh             error_report("Segment Table Support Unimplemented");
970b2899495SSuraj Jitindar Singh             exit(1);
971b2899495SSuraj Jitindar Singh         }
972b2899495SSuraj Jitindar Singh         /* Segment still not found, generate the appropriate interrupt */
9731a8c647bSRichard Henderson         if (!guest_visible) {
9741a8c647bSRichard Henderson             return false;
9751a8c647bSRichard Henderson         }
97659dec5bfSRichard Henderson         switch (access_type) {
97759dec5bfSRichard Henderson         case MMU_INST_FETCH:
978fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
979fcf5ef2aSThomas Huth             env->error_code = 0;
98059dec5bfSRichard Henderson             break;
98159dec5bfSRichard Henderson         case MMU_DATA_LOAD:
98259dec5bfSRichard Henderson         case MMU_DATA_STORE:
983fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
984fcf5ef2aSThomas Huth             env->error_code = 0;
985fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
98659dec5bfSRichard Henderson             break;
98759dec5bfSRichard Henderson         default:
98859dec5bfSRichard Henderson             g_assert_not_reached();
989fcf5ef2aSThomas Huth         }
9901a8c647bSRichard Henderson         return false;
991fcf5ef2aSThomas Huth     }
992fcf5ef2aSThomas Huth 
993fcf5ef2aSThomas Huth  skip_slb_search:
994fcf5ef2aSThomas Huth 
995fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
99659dec5bfSRichard Henderson     if (access_type == MMU_INST_FETCH && (slb->vsid & SLB_VSID_N)) {
9971a8c647bSRichard Henderson         if (guest_visible) {
99803695a98SBruno Larsen (billionai)             ppc_hash64_set_isi(cs, mmu_idx, SRR1_NOEXEC_GUARD);
9991a8c647bSRichard Henderson         }
10001a8c647bSRichard Henderson         return false;
1001fcf5ef2aSThomas Huth     }
1002fcf5ef2aSThomas Huth 
1003fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
10047222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
10057222b94aSDavid Gibson     if (ptex == -1) {
10061a8c647bSRichard Henderson         if (!guest_visible) {
10071a8c647bSRichard Henderson             return false;
10081a8c647bSRichard Henderson         }
100959dec5bfSRichard Henderson         switch (access_type) {
101059dec5bfSRichard Henderson         case MMU_INST_FETCH:
101103695a98SBruno Larsen (billionai)             ppc_hash64_set_isi(cs, mmu_idx, SRR1_NOPTE);
101259dec5bfSRichard Henderson             break;
101359dec5bfSRichard Henderson         case MMU_DATA_LOAD:
101403695a98SBruno Larsen (billionai)             ppc_hash64_set_dsi(cs, mmu_idx, eaddr, DSISR_NOPTE);
101559dec5bfSRichard Henderson             break;
101659dec5bfSRichard Henderson         case MMU_DATA_STORE:
101703695a98SBruno Larsen (billionai)             ppc_hash64_set_dsi(cs, mmu_idx, eaddr, DSISR_NOPTE | DSISR_ISSTORE);
101859dec5bfSRichard Henderson             break;
101959dec5bfSRichard Henderson         default:
102059dec5bfSRichard Henderson             g_assert_not_reached();
1021fcf5ef2aSThomas Huth         }
10221a8c647bSRichard Henderson         return false;
1023fcf5ef2aSThomas Huth     }
1024fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
10257222b94aSDavid Gibson                   "found PTE at index %08" HWADDR_PRIx "\n", ptex);
1026fcf5ef2aSThomas Huth 
1027fcf5ef2aSThomas Huth     /* 5. Check access permissions */
1028fcf5ef2aSThomas Huth 
102907a68f99SSuraj Jitindar Singh     exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
103003695a98SBruno Larsen (billionai)     pp_prot = ppc_hash64_pte_prot(mmu_idx, slb, pte);
1031fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
103207a68f99SSuraj Jitindar Singh     prot = exec_prot & pp_prot & amr_prot;
1033fcf5ef2aSThomas Huth 
103459dec5bfSRichard Henderson     need_prot = prot_for_access_type(access_type);
1035182357dbSRichard Henderson     if (need_prot & ~prot) {
1036fcf5ef2aSThomas Huth         /* Access right violation */
1037fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
10381a8c647bSRichard Henderson         if (!guest_visible) {
10391a8c647bSRichard Henderson             return false;
10401a8c647bSRichard Henderson         }
104159dec5bfSRichard Henderson         if (access_type == MMU_INST_FETCH) {
1042a6152b52SSuraj Jitindar Singh             int srr1 = 0;
104307a68f99SSuraj Jitindar Singh             if (PAGE_EXEC & ~exec_prot) {
104407a68f99SSuraj Jitindar Singh                 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
104507a68f99SSuraj Jitindar Singh             } else if (PAGE_EXEC & ~pp_prot) {
1046a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
1047a6152b52SSuraj Jitindar Singh             }
1048a6152b52SSuraj Jitindar Singh             if (PAGE_EXEC & ~amr_prot) {
1049a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
1050a6152b52SSuraj Jitindar Singh             }
105103695a98SBruno Larsen (billionai)             ppc_hash64_set_isi(cs, mmu_idx, srr1);
1052fcf5ef2aSThomas Huth         } else {
1053da82c73aSSuraj Jitindar Singh             int dsisr = 0;
1054182357dbSRichard Henderson             if (need_prot & ~pp_prot) {
1055da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_PROTFAULT;
1056fcf5ef2aSThomas Huth             }
105759dec5bfSRichard Henderson             if (access_type == MMU_DATA_STORE) {
1058da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
1059fcf5ef2aSThomas Huth             }
1060182357dbSRichard Henderson             if (need_prot & ~amr_prot) {
1061da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_AMR;
1062fcf5ef2aSThomas Huth             }
106303695a98SBruno Larsen (billionai)             ppc_hash64_set_dsi(cs, mmu_idx, eaddr, dsisr);
1064fcf5ef2aSThomas Huth         }
10651a8c647bSRichard Henderson         return false;
1066fcf5ef2aSThomas Huth     }
1067fcf5ef2aSThomas Huth 
1068fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
1069fcf5ef2aSThomas Huth 
1070fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
1071fcf5ef2aSThomas Huth 
1072a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_R)) {
1073a2dd4e83SBenjamin Herrenschmidt         ppc_hash64_set_r(cpu, ptex, pte.pte1);
1074a2dd4e83SBenjamin Herrenschmidt     }
1075a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_C)) {
107659dec5bfSRichard Henderson         if (access_type == MMU_DATA_STORE) {
1077a2dd4e83SBenjamin Herrenschmidt             ppc_hash64_set_c(cpu, ptex, pte.pte1);
1078fcf5ef2aSThomas Huth         } else {
1079d75cbae8SDavid Gibson             /*
1080d75cbae8SDavid Gibson              * Treat the page as read-only for now, so that a later write
1081d75cbae8SDavid Gibson              * will pass through this function again to set the C bit
1082d75cbae8SDavid Gibson              */
1083fcf5ef2aSThomas Huth             prot &= ~PAGE_WRITE;
1084fcf5ef2aSThomas Huth         }
1085fcf5ef2aSThomas Huth     }
1086fcf5ef2aSThomas Huth 
1087fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
1088fcf5ef2aSThomas Huth 
10891a8c647bSRichard Henderson     *raddrp = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
10901a8c647bSRichard Henderson     *protp = prot;
10911a8c647bSRichard Henderson     *psizep = apshift;
10921a8c647bSRichard Henderson     return true;
10931a8c647bSRichard Henderson }
10941a8c647bSRichard Henderson 
10957222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
1096fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
1097fcf5ef2aSThomas Huth {
1098fcf5ef2aSThomas Huth     /*
1099fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
1100fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
1101fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
1102fcf5ef2aSThomas Huth      */
1103fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
1104fcf5ef2aSThomas Huth }
1105fcf5ef2aSThomas Huth 
11062b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
11075ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val)
11085ad55315SDavid Gibson {
1109db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
11105ad55315SDavid Gibson 
11115ad55315SDavid Gibson     ppc_store_lpcr(cpu, val);
11125ad55315SDavid Gibson }
11132b44e219SBruno Larsen (billionai) #endif
11145ad55315SDavid Gibson 
1115a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu)
1116a059471dSDavid Gibson {
1117a059471dSDavid Gibson     CPUPPCState *env = &cpu->env;
1118a059471dSDavid Gibson     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1119a059471dSDavid Gibson 
112021e405f1SDavid Gibson     if (!pcc->hash64_opts) {
1121d57d72a8SGreg Kurz         assert(!mmu_is_64bit(env->mmu_model));
112221e405f1SDavid Gibson         return;
112321e405f1SDavid Gibson     }
112421e405f1SDavid Gibson 
112521e405f1SDavid Gibson     cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
112621e405f1SDavid Gibson }
112721e405f1SDavid Gibson 
112821e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu)
112921e405f1SDavid Gibson {
113021e405f1SDavid Gibson     g_free(cpu->hash64_opts);
113121e405f1SDavid Gibson }
113221e405f1SDavid Gibson 
113321e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = {
113458969eeeSDavid Gibson     .flags = 0,
113567d7d66fSDavid Gibson     .slb_size = 64,
1136a059471dSDavid Gibson     .sps = {
1137a059471dSDavid Gibson         { .page_shift = 12, /* 4K */
1138a059471dSDavid Gibson           .slb_enc = 0,
1139a059471dSDavid Gibson           .enc = { { .page_shift = 12, .pte_enc = 0 } }
1140a059471dSDavid Gibson         },
1141a059471dSDavid Gibson         { .page_shift = 24, /* 16M */
1142a059471dSDavid Gibson           .slb_enc = 0x100,
1143a059471dSDavid Gibson           .enc = { { .page_shift = 24, .pte_enc = 0 } }
1144a059471dSDavid Gibson         },
1145a059471dSDavid Gibson     },
1146a059471dSDavid Gibson };
1147b07c59f7SDavid Gibson 
1148b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = {
114926cd35b8SDavid Gibson     .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
115067d7d66fSDavid Gibson     .slb_size = 32,
1151b07c59f7SDavid Gibson     .sps = {
1152b07c59f7SDavid Gibson         {
1153b07c59f7SDavid Gibson             .page_shift = 12, /* 4K */
1154b07c59f7SDavid Gibson             .slb_enc = 0,
1155b07c59f7SDavid Gibson             .enc = { { .page_shift = 12, .pte_enc = 0 },
1156b07c59f7SDavid Gibson                      { .page_shift = 16, .pte_enc = 0x7 },
1157b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x38 }, },
1158b07c59f7SDavid Gibson         },
1159b07c59f7SDavid Gibson         {
1160b07c59f7SDavid Gibson             .page_shift = 16, /* 64K */
1161b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_64K,
1162b07c59f7SDavid Gibson             .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1163b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x8 }, },
1164b07c59f7SDavid Gibson         },
1165b07c59f7SDavid Gibson         {
1166b07c59f7SDavid Gibson             .page_shift = 24, /* 16M */
1167b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16M,
1168b07c59f7SDavid Gibson             .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1169b07c59f7SDavid Gibson         },
1170b07c59f7SDavid Gibson         {
1171b07c59f7SDavid Gibson             .page_shift = 34, /* 16G */
1172b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16G,
1173b07c59f7SDavid Gibson             .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },
1174b07c59f7SDavid Gibson         },
1175b07c59f7SDavid Gibson     }
1176b07c59f7SDavid Gibson };
117727f00f0aSDavid Gibson 
117827f00f0aSDavid Gibson 
1179