xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision 491a25535c99b838772ff961a39762333f0e852f)
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 
176*491a2553SLucas Coutinho #if defined(TARGET_PPC64)
177*491a2553SLucas Coutinho void helper_SLBIAG(CPUPPCState *env, target_ulong rs, uint32_t l)
178*491a2553SLucas Coutinho {
179*491a2553SLucas Coutinho     PowerPCCPU *cpu = env_archcpu(env);
180*491a2553SLucas Coutinho     int n;
181*491a2553SLucas Coutinho 
182*491a2553SLucas Coutinho     /*
183*491a2553SLucas Coutinho      * slbiag must always flush all TLB (which is equivalent to ERAT in ppc
184*491a2553SLucas Coutinho      * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
185*491a2553SLucas Coutinho      * can overwrite a valid SLB without flushing its lookaside information.
186*491a2553SLucas Coutinho      *
187*491a2553SLucas Coutinho      * It would be possible to keep the TLB in synch with the SLB by flushing
188*491a2553SLucas Coutinho      * when a valid entry is overwritten by slbmte, and therefore slbiag would
189*491a2553SLucas Coutinho      * not have to flush unless it evicts a valid SLB entry. However it is
190*491a2553SLucas Coutinho      * expected that slbmte is more common than slbiag, and slbiag is usually
191*491a2553SLucas Coutinho      * going to evict valid SLB entries, so that tradeoff is unlikely to be a
192*491a2553SLucas Coutinho      * good one.
193*491a2553SLucas Coutinho      */
194*491a2553SLucas Coutinho     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
195*491a2553SLucas Coutinho 
196*491a2553SLucas Coutinho     for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
197*491a2553SLucas Coutinho         ppc_slb_t *slb = &env->slb[n];
198*491a2553SLucas Coutinho         slb->esid &= ~SLB_ESID_V;
199*491a2553SLucas Coutinho     }
200*491a2553SLucas Coutinho }
201*491a2553SLucas Coutinho #endif
202*491a2553SLucas Coutinho 
203a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr,
204a63f1dfcSNikunj A Dadhania                            target_ulong global)
205fcf5ef2aSThomas Huth {
206db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
207fcf5ef2aSThomas Huth     ppc_slb_t *slb;
208fcf5ef2aSThomas Huth 
209fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, addr);
210fcf5ef2aSThomas Huth     if (!slb) {
211fcf5ef2aSThomas Huth         return;
212fcf5ef2aSThomas Huth     }
213fcf5ef2aSThomas Huth 
214fcf5ef2aSThomas Huth     if (slb->esid & SLB_ESID_V) {
215fcf5ef2aSThomas Huth         slb->esid &= ~SLB_ESID_V;
216fcf5ef2aSThomas Huth 
217d75cbae8SDavid Gibson         /*
218d75cbae8SDavid Gibson          * XXX: given the fact that segment size is 256 MB or 1TB,
219fcf5ef2aSThomas Huth          *      and we still don't have a tlb_flush_mask(env, n, mask)
220fcf5ef2aSThomas Huth          *      in QEMU, we just invalidate all TLBs
221fcf5ef2aSThomas Huth          */
222a63f1dfcSNikunj A Dadhania         env->tlb_need_flush |=
223a63f1dfcSNikunj A Dadhania             (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH);
224fcf5ef2aSThomas Huth     }
225fcf5ef2aSThomas Huth }
226fcf5ef2aSThomas Huth 
22743507e47SLucas Coutinho void helper_SLBIE(CPUPPCState *env, target_ulong addr)
228a63f1dfcSNikunj A Dadhania {
229a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, false);
230a63f1dfcSNikunj A Dadhania }
231a63f1dfcSNikunj A Dadhania 
232a1b05c06SLucas Coutinho void helper_SLBIEG(CPUPPCState *env, target_ulong addr)
233a63f1dfcSNikunj A Dadhania {
234a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, true);
235a63f1dfcSNikunj A Dadhania }
2362b44e219SBruno Larsen (billionai) #endif
237a63f1dfcSNikunj A Dadhania 
238fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot,
239fcf5ef2aSThomas Huth                   target_ulong esid, target_ulong vsid)
240fcf5ef2aSThomas Huth {
241fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
242fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
243b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
244fcf5ef2aSThomas Huth     int i;
245fcf5ef2aSThomas Huth 
24667d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
247fcf5ef2aSThomas Huth         return -1; /* Bad slot number */
248fcf5ef2aSThomas Huth     }
249fcf5ef2aSThomas Huth     if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) {
250fcf5ef2aSThomas Huth         return -1; /* Reserved bits set */
251fcf5ef2aSThomas Huth     }
252fcf5ef2aSThomas Huth     if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
253fcf5ef2aSThomas Huth         return -1; /* Bad segment size */
254fcf5ef2aSThomas Huth     }
25558969eeeSDavid Gibson     if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) {
256fcf5ef2aSThomas Huth         return -1; /* 1T segment on MMU that doesn't support it */
257fcf5ef2aSThomas Huth     }
258fcf5ef2aSThomas Huth 
259fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
260b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
261fcf5ef2aSThomas Huth 
262fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
263fcf5ef2aSThomas Huth             break;
264fcf5ef2aSThomas Huth         }
265fcf5ef2aSThomas Huth 
266fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
267fcf5ef2aSThomas Huth             sps = sps1;
268fcf5ef2aSThomas Huth             break;
269fcf5ef2aSThomas Huth         }
270fcf5ef2aSThomas Huth     }
271fcf5ef2aSThomas Huth 
272fcf5ef2aSThomas Huth     if (!sps) {
273fcf5ef2aSThomas Huth         error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
274fcf5ef2aSThomas Huth                      " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx,
275fcf5ef2aSThomas Huth                      slot, esid, vsid);
276fcf5ef2aSThomas Huth         return -1;
277fcf5ef2aSThomas Huth     }
278fcf5ef2aSThomas Huth 
279fcf5ef2aSThomas Huth     slb->esid = esid;
280fcf5ef2aSThomas Huth     slb->vsid = vsid;
281fcf5ef2aSThomas Huth     slb->sps = sps;
282fcf5ef2aSThomas Huth 
28376134d48SSuraj Jitindar Singh     LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx
28476134d48SSuraj Jitindar Singh             " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid,
285fcf5ef2aSThomas Huth             slb->esid, slb->vsid);
286fcf5ef2aSThomas Huth 
287fcf5ef2aSThomas Huth     return 0;
288fcf5ef2aSThomas Huth }
289fcf5ef2aSThomas Huth 
2902b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
291fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb,
292fcf5ef2aSThomas Huth                              target_ulong *rt)
293fcf5ef2aSThomas Huth {
294fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
295fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
296fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
297fcf5ef2aSThomas Huth 
29867d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
299fcf5ef2aSThomas Huth         return -1;
300fcf5ef2aSThomas Huth     }
301fcf5ef2aSThomas Huth 
302fcf5ef2aSThomas Huth     *rt = slb->esid;
303fcf5ef2aSThomas Huth     return 0;
304fcf5ef2aSThomas Huth }
305fcf5ef2aSThomas Huth 
306fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
307fcf5ef2aSThomas Huth                              target_ulong *rt)
308fcf5ef2aSThomas Huth {
309fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
310fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
311fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
312fcf5ef2aSThomas Huth 
31367d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
314fcf5ef2aSThomas Huth         return -1;
315fcf5ef2aSThomas Huth     }
316fcf5ef2aSThomas Huth 
317fcf5ef2aSThomas Huth     *rt = slb->vsid;
318fcf5ef2aSThomas Huth     return 0;
319fcf5ef2aSThomas Huth }
320fcf5ef2aSThomas Huth 
321fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
322fcf5ef2aSThomas Huth                              target_ulong *rt)
323fcf5ef2aSThomas Huth {
324fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
325fcf5ef2aSThomas Huth     ppc_slb_t *slb;
326fcf5ef2aSThomas Huth 
327fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, env->msr)) {
328fcf5ef2aSThomas Huth         rb &= 0xffffffff;
329fcf5ef2aSThomas Huth     }
330fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, rb);
331fcf5ef2aSThomas Huth     if (slb == NULL) {
332fcf5ef2aSThomas Huth         *rt = (target_ulong)-1ul;
333fcf5ef2aSThomas Huth     } else {
334fcf5ef2aSThomas Huth         *rt = slb->vsid;
335fcf5ef2aSThomas Huth     }
336fcf5ef2aSThomas Huth     return 0;
337fcf5ef2aSThomas Huth }
338fcf5ef2aSThomas Huth 
3390b0ba40fSLucas Coutinho void helper_SLBMTE(CPUPPCState *env, target_ulong rb, target_ulong rs)
340fcf5ef2aSThomas Huth {
341db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
342fcf5ef2aSThomas Huth 
343fcf5ef2aSThomas Huth     if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) {
344fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
345fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
346fcf5ef2aSThomas Huth     }
347fcf5ef2aSThomas Huth }
348fcf5ef2aSThomas Huth 
34941b60e46SLucas Coutinho target_ulong helper_SLBMFEE(CPUPPCState *env, target_ulong rb)
350fcf5ef2aSThomas Huth {
351db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
352fcf5ef2aSThomas Huth     target_ulong rt = 0;
353fcf5ef2aSThomas Huth 
354fcf5ef2aSThomas Huth     if (ppc_load_slb_esid(cpu, rb, &rt) < 0) {
355fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
356fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
357fcf5ef2aSThomas Huth     }
358fcf5ef2aSThomas Huth     return rt;
359fcf5ef2aSThomas Huth }
360fcf5ef2aSThomas Huth 
36126d02c9dSLucas Coutinho target_ulong helper_SLBFEE(CPUPPCState *env, target_ulong rb)
362fcf5ef2aSThomas Huth {
363db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
364fcf5ef2aSThomas Huth     target_ulong rt = 0;
365fcf5ef2aSThomas Huth 
366fcf5ef2aSThomas Huth     if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) {
367fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
368fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
369fcf5ef2aSThomas Huth     }
370fcf5ef2aSThomas Huth     return rt;
371fcf5ef2aSThomas Huth }
372fcf5ef2aSThomas Huth 
37374a15384SLucas Coutinho target_ulong helper_SLBMFEV(CPUPPCState *env, target_ulong rb)
374fcf5ef2aSThomas Huth {
375db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
376fcf5ef2aSThomas Huth     target_ulong rt = 0;
377fcf5ef2aSThomas Huth 
378fcf5ef2aSThomas Huth     if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) {
379fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
380fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
381fcf5ef2aSThomas Huth     }
382fcf5ef2aSThomas Huth     return rt;
383fcf5ef2aSThomas Huth }
3842b44e219SBruno Larsen (billionai) #endif
385fcf5ef2aSThomas Huth 
38607a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */
38707a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu,
38807a68f99SSuraj Jitindar Singh                                               ppc_hash_pte64_t pte)
38907a68f99SSuraj Jitindar Singh {
39007a68f99SSuraj Jitindar Singh     /* Exec permissions CANNOT take away read or write permissions */
39107a68f99SSuraj Jitindar Singh     return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ?
39207a68f99SSuraj Jitindar Singh             PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC;
39307a68f99SSuraj Jitindar Singh }
39407a68f99SSuraj Jitindar Singh 
39507a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */
39603695a98SBruno Larsen (billionai) static int ppc_hash64_pte_prot(int mmu_idx,
397fcf5ef2aSThomas Huth                                ppc_slb_t *slb, ppc_hash_pte64_t pte)
398fcf5ef2aSThomas Huth {
399fcf5ef2aSThomas Huth     unsigned pp, key;
400d75cbae8SDavid Gibson     /*
401d75cbae8SDavid Gibson      * Some pp bit combinations have undefined behaviour, so default
402d75cbae8SDavid Gibson      * to no access in those cases
403d75cbae8SDavid Gibson      */
404fcf5ef2aSThomas Huth     int prot = 0;
405fcf5ef2aSThomas Huth 
40603695a98SBruno Larsen (billionai)     key = !!(mmuidx_pr(mmu_idx) ? (slb->vsid & SLB_VSID_KP)
407fcf5ef2aSThomas Huth              : (slb->vsid & SLB_VSID_KS));
408fcf5ef2aSThomas Huth     pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
409fcf5ef2aSThomas Huth 
410fcf5ef2aSThomas Huth     if (key == 0) {
411fcf5ef2aSThomas Huth         switch (pp) {
412fcf5ef2aSThomas Huth         case 0x0:
413fcf5ef2aSThomas Huth         case 0x1:
414fcf5ef2aSThomas Huth         case 0x2:
415347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
416fcf5ef2aSThomas Huth             break;
417fcf5ef2aSThomas Huth 
418fcf5ef2aSThomas Huth         case 0x3:
419fcf5ef2aSThomas Huth         case 0x6:
420347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
421fcf5ef2aSThomas Huth             break;
422fcf5ef2aSThomas Huth         }
423fcf5ef2aSThomas Huth     } else {
424fcf5ef2aSThomas Huth         switch (pp) {
425fcf5ef2aSThomas Huth         case 0x0:
426fcf5ef2aSThomas Huth         case 0x6:
427fcf5ef2aSThomas Huth             break;
428fcf5ef2aSThomas Huth 
429fcf5ef2aSThomas Huth         case 0x1:
430fcf5ef2aSThomas Huth         case 0x3:
431347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
432fcf5ef2aSThomas Huth             break;
433fcf5ef2aSThomas Huth 
434fcf5ef2aSThomas Huth         case 0x2:
435347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
436fcf5ef2aSThomas Huth             break;
437fcf5ef2aSThomas Huth         }
438fcf5ef2aSThomas Huth     }
439fcf5ef2aSThomas Huth 
440fcf5ef2aSThomas Huth     return prot;
441fcf5ef2aSThomas Huth }
442fcf5ef2aSThomas Huth 
443a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */
444a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key)
445a6152b52SSuraj Jitindar Singh {
446a6152b52SSuraj Jitindar Singh     CPUPPCState *env = &cpu->env;
447a6152b52SSuraj Jitindar Singh     int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3;
448a6152b52SSuraj Jitindar Singh 
449a6152b52SSuraj Jitindar Singh     /*
450a6152b52SSuraj Jitindar Singh      * An instruction fetch is permitted if the IAMR bit is 0.
451a6152b52SSuraj Jitindar Singh      * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
452a6152b52SSuraj Jitindar Singh      * can only take away EXEC permissions not READ or WRITE permissions.
453a6152b52SSuraj Jitindar Singh      * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
454a6152b52SSuraj Jitindar Singh      * EXEC permissions are allowed.
455a6152b52SSuraj Jitindar Singh      */
456a6152b52SSuraj Jitindar Singh     return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE :
457a6152b52SSuraj Jitindar Singh                                PAGE_READ | PAGE_WRITE | PAGE_EXEC;
458a6152b52SSuraj Jitindar Singh }
459a6152b52SSuraj Jitindar Singh 
460fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte)
461fcf5ef2aSThomas Huth {
462fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
463fcf5ef2aSThomas Huth     int key, amrbits;
464fcf5ef2aSThomas Huth     int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
465fcf5ef2aSThomas Huth 
466fcf5ef2aSThomas Huth     /* Only recent MMUs implement Virtual Page Class Key Protection */
46758969eeeSDavid Gibson     if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) {
468fcf5ef2aSThomas Huth         return prot;
469fcf5ef2aSThomas Huth     }
470fcf5ef2aSThomas Huth 
471fcf5ef2aSThomas Huth     key = HPTE64_R_KEY(pte.pte1);
472fcf5ef2aSThomas Huth     amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3;
473fcf5ef2aSThomas Huth 
474fcf5ef2aSThomas Huth     /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
475fcf5ef2aSThomas Huth     /*         env->spr[SPR_AMR]); */
476fcf5ef2aSThomas Huth 
477fcf5ef2aSThomas Huth     /*
478fcf5ef2aSThomas Huth      * A store is permitted if the AMR bit is 0. Remove write
479fcf5ef2aSThomas Huth      * protection if it is set.
480fcf5ef2aSThomas Huth      */
481fcf5ef2aSThomas Huth     if (amrbits & 0x2) {
482fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
483fcf5ef2aSThomas Huth     }
484fcf5ef2aSThomas Huth     /*
485fcf5ef2aSThomas Huth      * A load is permitted if the AMR bit is 0. Remove read
486fcf5ef2aSThomas Huth      * protection if it is set.
487fcf5ef2aSThomas Huth      */
488fcf5ef2aSThomas Huth     if (amrbits & 0x1) {
489fcf5ef2aSThomas Huth         prot &= ~PAGE_READ;
490fcf5ef2aSThomas Huth     }
491fcf5ef2aSThomas Huth 
492a6152b52SSuraj Jitindar Singh     switch (env->mmu_model) {
493a6152b52SSuraj Jitindar Singh     /*
494a6152b52SSuraj Jitindar Singh      * MMU version 2.07 and later support IAMR
495a6152b52SSuraj Jitindar Singh      * Check if the IAMR allows the instruction access - it will return
496a6152b52SSuraj Jitindar Singh      * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
497a6152b52SSuraj Jitindar Singh      * if it does (and prot will be unchanged indicating execution support).
498a6152b52SSuraj Jitindar Singh      */
499a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_2_07:
500a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_3_00:
501a6152b52SSuraj Jitindar Singh         prot &= ppc_hash64_iamr_prot(cpu, key);
502a6152b52SSuraj Jitindar Singh         break;
503a6152b52SSuraj Jitindar Singh     default:
504a6152b52SSuraj Jitindar Singh         break;
505a6152b52SSuraj Jitindar Singh     }
506a6152b52SSuraj Jitindar Singh 
507fcf5ef2aSThomas Huth     return prot;
508fcf5ef2aSThomas Huth }
509fcf5ef2aSThomas Huth 
5107222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu,
5117222b94aSDavid Gibson                                              hwaddr ptex, int n)
512fcf5ef2aSThomas Huth {
5137222b94aSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
5143367c62fSBenjamin Herrenschmidt     hwaddr base;
5157222b94aSDavid Gibson     hwaddr plen = n * HASH_PTE_SIZE_64;
516e57ca75cSDavid Gibson     const ppc_hash_pte64_t *hptes;
517e57ca75cSDavid Gibson 
518e57ca75cSDavid Gibson     if (cpu->vhyp) {
519e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
520e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
521e57ca75cSDavid Gibson         return vhc->map_hptes(cpu->vhyp, ptex, n);
522e57ca75cSDavid Gibson     }
5233367c62fSBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
524e57ca75cSDavid Gibson 
525e57ca75cSDavid Gibson     if (!base) {
526e57ca75cSDavid Gibson         return NULL;
527e57ca75cSDavid Gibson     }
528e57ca75cSDavid Gibson 
529f26404fbSPeter Maydell     hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false,
530f26404fbSPeter Maydell                               MEMTXATTRS_UNSPECIFIED);
5317222b94aSDavid Gibson     if (plen < (n * HASH_PTE_SIZE_64)) {
5327222b94aSDavid Gibson         hw_error("%s: Unable to map all requested HPTEs\n", __func__);
533fcf5ef2aSThomas Huth     }
5347222b94aSDavid Gibson     return hptes;
535fcf5ef2aSThomas Huth }
536fcf5ef2aSThomas Huth 
5377222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes,
5387222b94aSDavid Gibson                             hwaddr ptex, int n)
539fcf5ef2aSThomas Huth {
540e57ca75cSDavid Gibson     if (cpu->vhyp) {
541e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
542e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
543e57ca75cSDavid Gibson         vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n);
544e57ca75cSDavid Gibson         return;
545e57ca75cSDavid Gibson     }
546e57ca75cSDavid Gibson 
5477222b94aSDavid Gibson     address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64,
5487222b94aSDavid Gibson                         false, n * HASH_PTE_SIZE_64);
549fcf5ef2aSThomas Huth }
550fcf5ef2aSThomas Huth 
551b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps,
552fcf5ef2aSThomas Huth                                 uint64_t pte0, uint64_t pte1)
553fcf5ef2aSThomas Huth {
554fcf5ef2aSThomas Huth     int i;
555fcf5ef2aSThomas Huth 
556fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
557fcf5ef2aSThomas Huth         if (sps->page_shift != 12) {
558fcf5ef2aSThomas Huth             /* 4kiB page in a non 4kiB segment */
559fcf5ef2aSThomas Huth             return 0;
560fcf5ef2aSThomas Huth         }
561fcf5ef2aSThomas Huth         /* Normal 4kiB page */
562fcf5ef2aSThomas Huth         return 12;
563fcf5ef2aSThomas Huth     }
564fcf5ef2aSThomas Huth 
565fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
566b07c59f7SDavid Gibson         const PPCHash64PageSize *ps = &sps->enc[i];
567fcf5ef2aSThomas Huth         uint64_t mask;
568fcf5ef2aSThomas Huth 
569fcf5ef2aSThomas Huth         if (!ps->page_shift) {
570fcf5ef2aSThomas Huth             break;
571fcf5ef2aSThomas Huth         }
572fcf5ef2aSThomas Huth 
573fcf5ef2aSThomas Huth         if (ps->page_shift == 12) {
574fcf5ef2aSThomas Huth             /* L bit is set so this can't be a 4kiB page */
575fcf5ef2aSThomas Huth             continue;
576fcf5ef2aSThomas Huth         }
577fcf5ef2aSThomas Huth 
578fcf5ef2aSThomas Huth         mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
579fcf5ef2aSThomas Huth 
580fcf5ef2aSThomas Huth         if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
581fcf5ef2aSThomas Huth             return ps->page_shift;
582fcf5ef2aSThomas Huth         }
583fcf5ef2aSThomas Huth     }
584fcf5ef2aSThomas Huth 
585fcf5ef2aSThomas Huth     return 0; /* Bad page size encoding */
586fcf5ef2aSThomas Huth }
587fcf5ef2aSThomas Huth 
58834525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1)
58934525595SBenjamin Herrenschmidt {
59034525595SBenjamin Herrenschmidt     /* Insert B into pte0 */
59134525595SBenjamin Herrenschmidt     *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) |
59234525595SBenjamin Herrenschmidt             ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) <<
59334525595SBenjamin Herrenschmidt              (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT));
59434525595SBenjamin Herrenschmidt 
59534525595SBenjamin Herrenschmidt     /* Remove B from pte1 */
59634525595SBenjamin Herrenschmidt     *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK;
59734525595SBenjamin Herrenschmidt }
59834525595SBenjamin Herrenschmidt 
59934525595SBenjamin Herrenschmidt 
600fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
601b07c59f7SDavid Gibson                                      const PPCHash64SegmentPageSizes *sps,
602fcf5ef2aSThomas Huth                                      target_ulong ptem,
603fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
604fcf5ef2aSThomas Huth {
605fcf5ef2aSThomas Huth     int i;
6067222b94aSDavid Gibson     const ppc_hash_pte64_t *pteg;
607fcf5ef2aSThomas Huth     target_ulong pte0, pte1;
6087222b94aSDavid Gibson     target_ulong ptex;
609fcf5ef2aSThomas Huth 
61036778660SDavid Gibson     ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP;
6117222b94aSDavid Gibson     pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
6127222b94aSDavid Gibson     if (!pteg) {
613fcf5ef2aSThomas Huth         return -1;
614fcf5ef2aSThomas Huth     }
615fcf5ef2aSThomas Huth     for (i = 0; i < HPTES_PER_GROUP; i++) {
6167222b94aSDavid Gibson         pte0 = ppc_hash64_hpte0(cpu, pteg, i);
6173054b0caSBenjamin Herrenschmidt         /*
6183054b0caSBenjamin Herrenschmidt          * pte0 contains the valid bit and must be read before pte1,
6193054b0caSBenjamin Herrenschmidt          * otherwise we might see an old pte1 with a new valid bit and
6203054b0caSBenjamin Herrenschmidt          * thus an inconsistent hpte value
6213054b0caSBenjamin Herrenschmidt          */
6223054b0caSBenjamin Herrenschmidt         smp_rmb();
6237222b94aSDavid Gibson         pte1 = ppc_hash64_hpte1(cpu, pteg, i);
624fcf5ef2aSThomas Huth 
62534525595SBenjamin Herrenschmidt         /* Convert format if necessary */
62634525595SBenjamin Herrenschmidt         if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) {
62734525595SBenjamin Herrenschmidt             ppc64_v3_new_to_old_hpte(&pte0, &pte1);
62834525595SBenjamin Herrenschmidt         }
62934525595SBenjamin Herrenschmidt 
630fcf5ef2aSThomas Huth         /* This compares V, B, H (secondary) and the AVPN */
631fcf5ef2aSThomas Huth         if (HPTE64_V_COMPARE(pte0, ptem)) {
632fcf5ef2aSThomas Huth             *pshift = hpte_page_shift(sps, pte0, pte1);
633fcf5ef2aSThomas Huth             /*
634fcf5ef2aSThomas Huth              * If there is no match, ignore the PTE, it could simply
635fcf5ef2aSThomas Huth              * be for a different segment size encoding and the
636fcf5ef2aSThomas Huth              * architecture specifies we should not match. Linux will
637fcf5ef2aSThomas Huth              * potentially leave behind PTEs for the wrong base page
638fcf5ef2aSThomas Huth              * size when demoting segments.
639fcf5ef2aSThomas Huth              */
640fcf5ef2aSThomas Huth             if (*pshift == 0) {
641fcf5ef2aSThomas Huth                 continue;
642fcf5ef2aSThomas Huth             }
643d75cbae8SDavid Gibson             /*
644d75cbae8SDavid Gibson              * We don't do anything with pshift yet as qemu TLB only
645d75cbae8SDavid Gibson              * deals with 4K pages anyway
646fcf5ef2aSThomas Huth              */
647fcf5ef2aSThomas Huth             pte->pte0 = pte0;
648fcf5ef2aSThomas Huth             pte->pte1 = pte1;
6497222b94aSDavid Gibson             ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
6507222b94aSDavid Gibson             return ptex + i;
651fcf5ef2aSThomas Huth         }
652fcf5ef2aSThomas Huth     }
6537222b94aSDavid Gibson     ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
654fcf5ef2aSThomas Huth     /*
655fcf5ef2aSThomas Huth      * We didn't find a valid entry.
656fcf5ef2aSThomas Huth      */
657fcf5ef2aSThomas Huth     return -1;
658fcf5ef2aSThomas Huth }
659fcf5ef2aSThomas Huth 
660fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
661fcf5ef2aSThomas Huth                                      ppc_slb_t *slb, target_ulong eaddr,
662fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
663fcf5ef2aSThomas Huth {
664fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
6657222b94aSDavid Gibson     hwaddr hash, ptex;
666fcf5ef2aSThomas Huth     uint64_t vsid, epnmask, epn, ptem;
667b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = slb->sps;
668fcf5ef2aSThomas Huth 
669d75cbae8SDavid Gibson     /*
670d75cbae8SDavid Gibson      * The SLB store path should prevent any bad page size encodings
671d75cbae8SDavid Gibson      * getting in there, so:
672d75cbae8SDavid Gibson      */
673fcf5ef2aSThomas Huth     assert(sps);
674fcf5ef2aSThomas Huth 
675fcf5ef2aSThomas Huth     /* If ISL is set in LPCR we need to clamp the page size to 4K */
676fcf5ef2aSThomas Huth     if (env->spr[SPR_LPCR] & LPCR_ISL) {
677fcf5ef2aSThomas Huth         /* We assume that when using TCG, 4k is first entry of SPS */
678b07c59f7SDavid Gibson         sps = &cpu->hash64_opts->sps[0];
679fcf5ef2aSThomas Huth         assert(sps->page_shift == 12);
680fcf5ef2aSThomas Huth     }
681fcf5ef2aSThomas Huth 
682fcf5ef2aSThomas Huth     epnmask = ~((1ULL << sps->page_shift) - 1);
683fcf5ef2aSThomas Huth 
684fcf5ef2aSThomas Huth     if (slb->vsid & SLB_VSID_B) {
685fcf5ef2aSThomas Huth         /* 1TB segment */
686fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
687fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
688fcf5ef2aSThomas Huth         hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
689fcf5ef2aSThomas Huth     } else {
690fcf5ef2aSThomas Huth         /* 256M segment */
691fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
692fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
693fcf5ef2aSThomas Huth         hash = vsid ^ (epn >> sps->page_shift);
694fcf5ef2aSThomas Huth     }
695fcf5ef2aSThomas Huth     ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
696fcf5ef2aSThomas Huth     ptem |= HPTE64_V_VALID;
697fcf5ef2aSThomas Huth 
698fcf5ef2aSThomas Huth     /* Page address translation */
699fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
700fcf5ef2aSThomas Huth             "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
701fcf5ef2aSThomas Huth             " hash " TARGET_FMT_plx "\n",
70236778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash);
703fcf5ef2aSThomas Huth 
704fcf5ef2aSThomas Huth     /* Primary PTEG lookup */
705fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
706fcf5ef2aSThomas Huth             "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
707fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
708fcf5ef2aSThomas Huth             " hash=" TARGET_FMT_plx "\n",
70936778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu),
71036778660SDavid Gibson             vsid, ptem,  hash);
7117222b94aSDavid Gibson     ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
712fcf5ef2aSThomas Huth 
7137222b94aSDavid Gibson     if (ptex == -1) {
714fcf5ef2aSThomas Huth         /* Secondary PTEG lookup */
715fcf5ef2aSThomas Huth         ptem |= HPTE64_V_SECONDARY;
716fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU,
717fcf5ef2aSThomas Huth                 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
718fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
71936778660SDavid Gibson                 " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu),
72036778660SDavid Gibson                 ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash);
721fcf5ef2aSThomas Huth 
7227222b94aSDavid Gibson         ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
723fcf5ef2aSThomas Huth     }
724fcf5ef2aSThomas Huth 
7257222b94aSDavid Gibson     return ptex;
726fcf5ef2aSThomas Huth }
727fcf5ef2aSThomas Huth 
728fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
729fcf5ef2aSThomas Huth                                           uint64_t pte0, uint64_t pte1)
730fcf5ef2aSThomas Huth {
731fcf5ef2aSThomas Huth     int i;
732fcf5ef2aSThomas Huth 
733fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
734fcf5ef2aSThomas Huth         return 12;
735fcf5ef2aSThomas Huth     }
736fcf5ef2aSThomas Huth 
737fcf5ef2aSThomas Huth     /*
738fcf5ef2aSThomas Huth      * The encodings in env->sps need to be carefully chosen so that
739fcf5ef2aSThomas Huth      * this gives an unambiguous result.
740fcf5ef2aSThomas Huth      */
741fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
742b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
743fcf5ef2aSThomas Huth         unsigned shift;
744fcf5ef2aSThomas Huth 
745fcf5ef2aSThomas Huth         if (!sps->page_shift) {
746fcf5ef2aSThomas Huth             break;
747fcf5ef2aSThomas Huth         }
748fcf5ef2aSThomas Huth 
749fcf5ef2aSThomas Huth         shift = hpte_page_shift(sps, pte0, pte1);
750fcf5ef2aSThomas Huth         if (shift) {
751fcf5ef2aSThomas Huth             return shift;
752fcf5ef2aSThomas Huth         }
753fcf5ef2aSThomas Huth     }
754fcf5ef2aSThomas Huth 
755fcf5ef2aSThomas Huth     return 0;
756fcf5ef2aSThomas Huth }
757fcf5ef2aSThomas Huth 
7581b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env)
7591b99e029SDavid Gibson {
7601b99e029SDavid Gibson     switch (env->mmu_model) {
7611b99e029SDavid Gibson     case POWERPC_MMU_3_00:
7621b99e029SDavid Gibson         /*
7631b99e029SDavid Gibson          * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR
7641b99e029SDavid Gibson          * register no longer exist
7651b99e029SDavid Gibson          */
7661b99e029SDavid Gibson         return true;
7671b99e029SDavid Gibson 
7681b99e029SDavid Gibson     default:
7691b99e029SDavid Gibson         return !!(env->spr[SPR_LPCR] & LPCR_VPM0);
7701b99e029SDavid Gibson     }
7711b99e029SDavid Gibson }
7721b99e029SDavid Gibson 
77303695a98SBruno Larsen (billionai) static void ppc_hash64_set_isi(CPUState *cs, int mmu_idx, uint64_t error_code)
774fcf5ef2aSThomas Huth {
7758fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
776fcf5ef2aSThomas Huth     bool vpm;
777fcf5ef2aSThomas Huth 
77803695a98SBruno Larsen (billionai)     if (!mmuidx_real(mmu_idx)) {
779fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
780fcf5ef2aSThomas Huth     } else {
7811b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
782fcf5ef2aSThomas Huth     }
78303695a98SBruno Larsen (billionai)     if (vpm && !mmuidx_hv(mmu_idx)) {
784fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HISI;
785fcf5ef2aSThomas Huth     } else {
786fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
787fcf5ef2aSThomas Huth     }
788fcf5ef2aSThomas Huth     env->error_code = error_code;
789fcf5ef2aSThomas Huth }
790fcf5ef2aSThomas Huth 
79103695a98SBruno Larsen (billionai) static void ppc_hash64_set_dsi(CPUState *cs, int mmu_idx, uint64_t dar, uint64_t dsisr)
792fcf5ef2aSThomas Huth {
7938fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
794fcf5ef2aSThomas Huth     bool vpm;
795fcf5ef2aSThomas Huth 
79603695a98SBruno Larsen (billionai)     if (!mmuidx_real(mmu_idx)) {
797fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
798fcf5ef2aSThomas Huth     } else {
7991b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
800fcf5ef2aSThomas Huth     }
80103695a98SBruno Larsen (billionai)     if (vpm && !mmuidx_hv(mmu_idx)) {
802fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
803fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
804fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
805fcf5ef2aSThomas Huth     } else {
806fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
807fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
808fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
809fcf5ef2aSThomas Huth    }
810fcf5ef2aSThomas Huth     env->error_code = 0;
811fcf5ef2aSThomas Huth }
812fcf5ef2aSThomas Huth 
813fcf5ef2aSThomas Huth 
814a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
815a2dd4e83SBenjamin Herrenschmidt {
8167bf00dfbSLeandro Lupori     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_R;
817a2dd4e83SBenjamin Herrenschmidt 
818a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
819a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
820a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
821a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_r(cpu->vhyp, ptex, pte1);
822a2dd4e83SBenjamin Herrenschmidt         return;
823a2dd4e83SBenjamin Herrenschmidt     }
824a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
825a2dd4e83SBenjamin Herrenschmidt 
826a2dd4e83SBenjamin Herrenschmidt 
827a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
828a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01);
829a2dd4e83SBenjamin Herrenschmidt }
830a2dd4e83SBenjamin Herrenschmidt 
831a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
832a2dd4e83SBenjamin Herrenschmidt {
8337bf00dfbSLeandro Lupori     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_C;
834a2dd4e83SBenjamin Herrenschmidt 
835a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
836a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
837a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
838a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_c(cpu->vhyp, ptex, pte1);
839a2dd4e83SBenjamin Herrenschmidt         return;
840a2dd4e83SBenjamin Herrenschmidt     }
841a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
842a2dd4e83SBenjamin Herrenschmidt 
843a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
844a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80);
845a2dd4e83SBenjamin Herrenschmidt }
846a2dd4e83SBenjamin Herrenschmidt 
847a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu)
848a864a6b3SDavid Gibson {
849a864a6b3SDavid Gibson     CPUPPCState *env = &cpu->env;
850a864a6b3SDavid Gibson     /*
851d37b40daSDavid Gibson      * In theory the meanings of RMLS values are implementation
852d37b40daSDavid Gibson      * dependent.  In practice, this seems to have been the set from
853d37b40daSDavid Gibson      * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
854a864a6b3SDavid Gibson      *
855a864a6b3SDavid Gibson      * Unsupported values mean the OS has shot itself in the
856a864a6b3SDavid Gibson      * foot. Return a 0-sized RMA in this case, which we expect
857a864a6b3SDavid Gibson      * to trigger an immediate DSI or ISI
858a864a6b3SDavid Gibson      */
859a864a6b3SDavid Gibson     static const target_ulong rma_sizes[16] = {
860d37b40daSDavid Gibson         [0] = 256 * GiB,
861a864a6b3SDavid Gibson         [1] = 16 * GiB,
862a864a6b3SDavid Gibson         [2] = 1 * GiB,
863a864a6b3SDavid Gibson         [3] = 64 * MiB,
864a864a6b3SDavid Gibson         [4] = 256 * MiB,
865a864a6b3SDavid Gibson         [7] = 128 * MiB,
866a864a6b3SDavid Gibson         [8] = 32 * MiB,
867a864a6b3SDavid Gibson     };
868a864a6b3SDavid Gibson     target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT;
869a864a6b3SDavid Gibson 
870a864a6b3SDavid Gibson     return rma_sizes[rmls];
871a864a6b3SDavid Gibson }
872a864a6b3SDavid Gibson 
8734c24a87fSDavid Gibson static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb)
8744c24a87fSDavid Gibson {
8754c24a87fSDavid Gibson     CPUPPCState *env = &cpu->env;
8764c24a87fSDavid Gibson     target_ulong lpcr = env->spr[SPR_LPCR];
8774c24a87fSDavid Gibson     uint32_t vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
8784c24a87fSDavid Gibson     target_ulong vsid = SLB_VSID_VRMA | ((vrmasd << 4) & SLB_VSID_LLP_MASK);
8794c24a87fSDavid Gibson     int i;
8804c24a87fSDavid Gibson 
8814c24a87fSDavid Gibson     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
8824c24a87fSDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
8834c24a87fSDavid Gibson 
8844c24a87fSDavid Gibson         if (!sps->page_shift) {
8854c24a87fSDavid Gibson             break;
8864c24a87fSDavid Gibson         }
8874c24a87fSDavid Gibson 
8884c24a87fSDavid Gibson         if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) {
8894c24a87fSDavid Gibson             slb->esid = SLB_ESID_V;
8904c24a87fSDavid Gibson             slb->vsid = vsid;
8914c24a87fSDavid Gibson             slb->sps = sps;
8924c24a87fSDavid Gibson             return 0;
8934c24a87fSDavid Gibson         }
8944c24a87fSDavid Gibson     }
8954c24a87fSDavid Gibson 
8964c24a87fSDavid Gibson     error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x"
897ff5b5d5bSMarkus Armbruster                  TARGET_FMT_lx, lpcr);
8984c24a87fSDavid Gibson 
8994c24a87fSDavid Gibson     return -1;
9004c24a87fSDavid Gibson }
9014c24a87fSDavid Gibson 
90251806b54SRichard Henderson bool ppc_hash64_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type,
90303695a98SBruno Larsen (billionai)                       hwaddr *raddrp, int *psizep, int *protp, int mmu_idx,
9041a8c647bSRichard Henderson                       bool guest_visible)
905fcf5ef2aSThomas Huth {
906fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
907fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
9084c24a87fSDavid Gibson     ppc_slb_t vrma_slbe;
909fcf5ef2aSThomas Huth     ppc_slb_t *slb;
910fcf5ef2aSThomas Huth     unsigned apshift;
9117222b94aSDavid Gibson     hwaddr ptex;
912fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
91307a68f99SSuraj Jitindar Singh     int exec_prot, pp_prot, amr_prot, prot;
914182357dbSRichard Henderson     int need_prot;
915fcf5ef2aSThomas Huth     hwaddr raddr;
916fcf5ef2aSThomas Huth 
917d75cbae8SDavid Gibson     /*
918d75cbae8SDavid Gibson      * Note on LPCR usage: 970 uses HID4, but our special variant of
919d75cbae8SDavid Gibson      * store_spr copies relevant fields into env->spr[SPR_LPCR].
920136fbf65Szhaolichang      * Similarly we filter unimplemented bits when storing into LPCR
921d75cbae8SDavid Gibson      * depending on the MMU version. This code can thus just use the
922d75cbae8SDavid Gibson      * LPCR "as-is".
923fcf5ef2aSThomas Huth      */
924fcf5ef2aSThomas Huth 
925fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
92603695a98SBruno Larsen (billionai)     if (mmuidx_real(mmu_idx)) {
927d75cbae8SDavid Gibson         /*
928d75cbae8SDavid Gibson          * Translation is supposedly "off", but in real mode the top 4
929d75cbae8SDavid Gibson          * effective address bits are (mostly) ignored
930d75cbae8SDavid Gibson          */
931fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
932fcf5ef2aSThomas Huth 
933682c1dfbSDavid Gibson         if (cpu->vhyp) {
934682c1dfbSDavid Gibson             /*
935682c1dfbSDavid Gibson              * In virtual hypervisor mode, there's nothing to do:
936682c1dfbSDavid Gibson              *   EA == GPA == qemu guest address
937682c1dfbSDavid Gibson              */
93803695a98SBruno Larsen (billionai)         } else if (mmuidx_hv(mmu_idx) || !env->has_hv_mode) {
939fcf5ef2aSThomas Huth             /* In HV mode, add HRMOR if top EA bit is clear */
940fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
941fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
942fcf5ef2aSThomas Huth             }
9431b99e029SDavid Gibson         } else if (ppc_hash64_use_vrma(env)) {
944682c1dfbSDavid Gibson             /* Emulated VRMA mode */
9454c24a87fSDavid Gibson             slb = &vrma_slbe;
9464c24a87fSDavid Gibson             if (build_vrma_slbe(cpu, slb) != 0) {
947682c1dfbSDavid Gibson                 /* Invalid VRMA setup, machine check */
9481a8c647bSRichard Henderson                 if (guest_visible) {
949fcf5ef2aSThomas Huth                     cs->exception_index = POWERPC_EXCP_MCHECK;
950fcf5ef2aSThomas Huth                     env->error_code = 0;
9511a8c647bSRichard Henderson                 }
9521a8c647bSRichard Henderson                 return false;
953682c1dfbSDavid Gibson             }
954682c1dfbSDavid Gibson 
955682c1dfbSDavid Gibson             goto skip_slb_search;
956fcf5ef2aSThomas Huth         } else {
9573a56a55cSDavid Gibson             target_ulong limit = rmls_limit(cpu);
9583a56a55cSDavid Gibson 
959682c1dfbSDavid Gibson             /* Emulated old-style RMO mode, bounds check against RMLS */
9603a56a55cSDavid Gibson             if (raddr >= limit) {
9611a8c647bSRichard Henderson                 if (!guest_visible) {
9621a8c647bSRichard Henderson                     return false;
9631a8c647bSRichard Henderson                 }
96459dec5bfSRichard Henderson                 switch (access_type) {
96559dec5bfSRichard Henderson                 case MMU_INST_FETCH:
96603695a98SBruno Larsen (billionai)                     ppc_hash64_set_isi(cs, mmu_idx, SRR1_PROTFAULT);
96759dec5bfSRichard Henderson                     break;
96859dec5bfSRichard Henderson                 case MMU_DATA_LOAD:
96903695a98SBruno Larsen (billionai)                     ppc_hash64_set_dsi(cs, mmu_idx, eaddr, DSISR_PROTFAULT);
97059dec5bfSRichard Henderson                     break;
97159dec5bfSRichard Henderson                 case MMU_DATA_STORE:
97203695a98SBruno Larsen (billionai)                     ppc_hash64_set_dsi(cs, mmu_idx, eaddr,
97359dec5bfSRichard Henderson                                        DSISR_PROTFAULT | DSISR_ISSTORE);
97459dec5bfSRichard Henderson                     break;
97559dec5bfSRichard Henderson                 default:
97659dec5bfSRichard Henderson                     g_assert_not_reached();
977fcf5ef2aSThomas Huth                 }
9781a8c647bSRichard Henderson                 return false;
979fcf5ef2aSThomas Huth             }
980682c1dfbSDavid Gibson 
981682c1dfbSDavid Gibson             raddr |= env->spr[SPR_RMOR];
982fcf5ef2aSThomas Huth         }
9831a8c647bSRichard Henderson 
9841a8c647bSRichard Henderson         *raddrp = raddr;
9851a8c647bSRichard Henderson         *protp = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9861a8c647bSRichard Henderson         *psizep = TARGET_PAGE_BITS;
9871a8c647bSRichard Henderson         return true;
988fcf5ef2aSThomas Huth     }
989fcf5ef2aSThomas Huth 
990fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
991fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
992fcf5ef2aSThomas Huth     if (!slb) {
993b2899495SSuraj Jitindar Singh         /* No entry found, check if in-memory segment tables are in use */
994ca79b3b7SDavid Gibson         if (ppc64_use_proc_tbl(cpu)) {
995b2899495SSuraj Jitindar Singh             /* TODO - Unsupported */
996b2899495SSuraj Jitindar Singh             error_report("Segment Table Support Unimplemented");
997b2899495SSuraj Jitindar Singh             exit(1);
998b2899495SSuraj Jitindar Singh         }
999b2899495SSuraj Jitindar Singh         /* Segment still not found, generate the appropriate interrupt */
10001a8c647bSRichard Henderson         if (!guest_visible) {
10011a8c647bSRichard Henderson             return false;
10021a8c647bSRichard Henderson         }
100359dec5bfSRichard Henderson         switch (access_type) {
100459dec5bfSRichard Henderson         case MMU_INST_FETCH:
1005fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
1006fcf5ef2aSThomas Huth             env->error_code = 0;
100759dec5bfSRichard Henderson             break;
100859dec5bfSRichard Henderson         case MMU_DATA_LOAD:
100959dec5bfSRichard Henderson         case MMU_DATA_STORE:
1010fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
1011fcf5ef2aSThomas Huth             env->error_code = 0;
1012fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
101359dec5bfSRichard Henderson             break;
101459dec5bfSRichard Henderson         default:
101559dec5bfSRichard Henderson             g_assert_not_reached();
1016fcf5ef2aSThomas Huth         }
10171a8c647bSRichard Henderson         return false;
1018fcf5ef2aSThomas Huth     }
1019fcf5ef2aSThomas Huth 
1020fcf5ef2aSThomas Huth  skip_slb_search:
1021fcf5ef2aSThomas Huth 
1022fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
102359dec5bfSRichard Henderson     if (access_type == MMU_INST_FETCH && (slb->vsid & SLB_VSID_N)) {
10241a8c647bSRichard Henderson         if (guest_visible) {
102503695a98SBruno Larsen (billionai)             ppc_hash64_set_isi(cs, mmu_idx, SRR1_NOEXEC_GUARD);
10261a8c647bSRichard Henderson         }
10271a8c647bSRichard Henderson         return false;
1028fcf5ef2aSThomas Huth     }
1029fcf5ef2aSThomas Huth 
1030fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
10317222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
10327222b94aSDavid Gibson     if (ptex == -1) {
10331a8c647bSRichard Henderson         if (!guest_visible) {
10341a8c647bSRichard Henderson             return false;
10351a8c647bSRichard Henderson         }
103659dec5bfSRichard Henderson         switch (access_type) {
103759dec5bfSRichard Henderson         case MMU_INST_FETCH:
103803695a98SBruno Larsen (billionai)             ppc_hash64_set_isi(cs, mmu_idx, SRR1_NOPTE);
103959dec5bfSRichard Henderson             break;
104059dec5bfSRichard Henderson         case MMU_DATA_LOAD:
104103695a98SBruno Larsen (billionai)             ppc_hash64_set_dsi(cs, mmu_idx, eaddr, DSISR_NOPTE);
104259dec5bfSRichard Henderson             break;
104359dec5bfSRichard Henderson         case MMU_DATA_STORE:
104403695a98SBruno Larsen (billionai)             ppc_hash64_set_dsi(cs, mmu_idx, eaddr, DSISR_NOPTE | DSISR_ISSTORE);
104559dec5bfSRichard Henderson             break;
104659dec5bfSRichard Henderson         default:
104759dec5bfSRichard Henderson             g_assert_not_reached();
1048fcf5ef2aSThomas Huth         }
10491a8c647bSRichard Henderson         return false;
1050fcf5ef2aSThomas Huth     }
1051fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
10527222b94aSDavid Gibson                   "found PTE at index %08" HWADDR_PRIx "\n", ptex);
1053fcf5ef2aSThomas Huth 
1054fcf5ef2aSThomas Huth     /* 5. Check access permissions */
1055fcf5ef2aSThomas Huth 
105607a68f99SSuraj Jitindar Singh     exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
105703695a98SBruno Larsen (billionai)     pp_prot = ppc_hash64_pte_prot(mmu_idx, slb, pte);
1058fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
105907a68f99SSuraj Jitindar Singh     prot = exec_prot & pp_prot & amr_prot;
1060fcf5ef2aSThomas Huth 
106159dec5bfSRichard Henderson     need_prot = prot_for_access_type(access_type);
1062182357dbSRichard Henderson     if (need_prot & ~prot) {
1063fcf5ef2aSThomas Huth         /* Access right violation */
1064fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
10651a8c647bSRichard Henderson         if (!guest_visible) {
10661a8c647bSRichard Henderson             return false;
10671a8c647bSRichard Henderson         }
106859dec5bfSRichard Henderson         if (access_type == MMU_INST_FETCH) {
1069a6152b52SSuraj Jitindar Singh             int srr1 = 0;
107007a68f99SSuraj Jitindar Singh             if (PAGE_EXEC & ~exec_prot) {
107107a68f99SSuraj Jitindar Singh                 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
107207a68f99SSuraj Jitindar Singh             } else if (PAGE_EXEC & ~pp_prot) {
1073a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
1074a6152b52SSuraj Jitindar Singh             }
1075a6152b52SSuraj Jitindar Singh             if (PAGE_EXEC & ~amr_prot) {
1076a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
1077a6152b52SSuraj Jitindar Singh             }
107803695a98SBruno Larsen (billionai)             ppc_hash64_set_isi(cs, mmu_idx, srr1);
1079fcf5ef2aSThomas Huth         } else {
1080da82c73aSSuraj Jitindar Singh             int dsisr = 0;
1081182357dbSRichard Henderson             if (need_prot & ~pp_prot) {
1082da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_PROTFAULT;
1083fcf5ef2aSThomas Huth             }
108459dec5bfSRichard Henderson             if (access_type == MMU_DATA_STORE) {
1085da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
1086fcf5ef2aSThomas Huth             }
1087182357dbSRichard Henderson             if (need_prot & ~amr_prot) {
1088da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_AMR;
1089fcf5ef2aSThomas Huth             }
109003695a98SBruno Larsen (billionai)             ppc_hash64_set_dsi(cs, mmu_idx, eaddr, dsisr);
1091fcf5ef2aSThomas Huth         }
10921a8c647bSRichard Henderson         return false;
1093fcf5ef2aSThomas Huth     }
1094fcf5ef2aSThomas Huth 
1095fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
1096fcf5ef2aSThomas Huth 
1097fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
1098fcf5ef2aSThomas Huth 
1099a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_R)) {
1100a2dd4e83SBenjamin Herrenschmidt         ppc_hash64_set_r(cpu, ptex, pte.pte1);
1101a2dd4e83SBenjamin Herrenschmidt     }
1102a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_C)) {
110359dec5bfSRichard Henderson         if (access_type == MMU_DATA_STORE) {
1104a2dd4e83SBenjamin Herrenschmidt             ppc_hash64_set_c(cpu, ptex, pte.pte1);
1105fcf5ef2aSThomas Huth         } else {
1106d75cbae8SDavid Gibson             /*
1107d75cbae8SDavid Gibson              * Treat the page as read-only for now, so that a later write
1108d75cbae8SDavid Gibson              * will pass through this function again to set the C bit
1109d75cbae8SDavid Gibson              */
1110fcf5ef2aSThomas Huth             prot &= ~PAGE_WRITE;
1111fcf5ef2aSThomas Huth         }
1112fcf5ef2aSThomas Huth     }
1113fcf5ef2aSThomas Huth 
1114fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
1115fcf5ef2aSThomas Huth 
11161a8c647bSRichard Henderson     *raddrp = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
11171a8c647bSRichard Henderson     *protp = prot;
11181a8c647bSRichard Henderson     *psizep = apshift;
11191a8c647bSRichard Henderson     return true;
11201a8c647bSRichard Henderson }
11211a8c647bSRichard Henderson 
11227222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
1123fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
1124fcf5ef2aSThomas Huth {
1125fcf5ef2aSThomas Huth     /*
1126fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
1127fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
1128fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
1129fcf5ef2aSThomas Huth      */
1130fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
1131fcf5ef2aSThomas Huth }
1132fcf5ef2aSThomas Huth 
11332b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
11345ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val)
11355ad55315SDavid Gibson {
1136db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
11375ad55315SDavid Gibson 
11385ad55315SDavid Gibson     ppc_store_lpcr(cpu, val);
11395ad55315SDavid Gibson }
11402b44e219SBruno Larsen (billionai) #endif
11415ad55315SDavid Gibson 
1142a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu)
1143a059471dSDavid Gibson {
1144a059471dSDavid Gibson     CPUPPCState *env = &cpu->env;
1145a059471dSDavid Gibson     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1146a059471dSDavid Gibson 
114721e405f1SDavid Gibson     if (!pcc->hash64_opts) {
1148d57d72a8SGreg Kurz         assert(!mmu_is_64bit(env->mmu_model));
114921e405f1SDavid Gibson         return;
115021e405f1SDavid Gibson     }
115121e405f1SDavid Gibson 
115221e405f1SDavid Gibson     cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
115321e405f1SDavid Gibson }
115421e405f1SDavid Gibson 
115521e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu)
115621e405f1SDavid Gibson {
115721e405f1SDavid Gibson     g_free(cpu->hash64_opts);
115821e405f1SDavid Gibson }
115921e405f1SDavid Gibson 
116021e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = {
116158969eeeSDavid Gibson     .flags = 0,
116267d7d66fSDavid Gibson     .slb_size = 64,
1163a059471dSDavid Gibson     .sps = {
1164a059471dSDavid Gibson         { .page_shift = 12, /* 4K */
1165a059471dSDavid Gibson           .slb_enc = 0,
1166a059471dSDavid Gibson           .enc = { { .page_shift = 12, .pte_enc = 0 } }
1167a059471dSDavid Gibson         },
1168a059471dSDavid Gibson         { .page_shift = 24, /* 16M */
1169a059471dSDavid Gibson           .slb_enc = 0x100,
1170a059471dSDavid Gibson           .enc = { { .page_shift = 24, .pte_enc = 0 } }
1171a059471dSDavid Gibson         },
1172a059471dSDavid Gibson     },
1173a059471dSDavid Gibson };
1174b07c59f7SDavid Gibson 
1175b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = {
117626cd35b8SDavid Gibson     .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
117767d7d66fSDavid Gibson     .slb_size = 32,
1178b07c59f7SDavid Gibson     .sps = {
1179b07c59f7SDavid Gibson         {
1180b07c59f7SDavid Gibson             .page_shift = 12, /* 4K */
1181b07c59f7SDavid Gibson             .slb_enc = 0,
1182b07c59f7SDavid Gibson             .enc = { { .page_shift = 12, .pte_enc = 0 },
1183b07c59f7SDavid Gibson                      { .page_shift = 16, .pte_enc = 0x7 },
1184b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x38 }, },
1185b07c59f7SDavid Gibson         },
1186b07c59f7SDavid Gibson         {
1187b07c59f7SDavid Gibson             .page_shift = 16, /* 64K */
1188b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_64K,
1189b07c59f7SDavid Gibson             .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1190b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x8 }, },
1191b07c59f7SDavid Gibson         },
1192b07c59f7SDavid Gibson         {
1193b07c59f7SDavid Gibson             .page_shift = 24, /* 16M */
1194b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16M,
1195b07c59f7SDavid Gibson             .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1196b07c59f7SDavid Gibson         },
1197b07c59f7SDavid Gibson         {
1198b07c59f7SDavid Gibson             .page_shift = 34, /* 16G */
1199b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16G,
1200b07c59f7SDavid Gibson             .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },
1201b07c59f7SDavid Gibson         },
1202b07c59f7SDavid Gibson     }
1203b07c59f7SDavid Gibson };
120427f00f0aSDavid Gibson 
120527f00f0aSDavid Gibson 
1206