xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision 0e2a3ec36885f6d79a96230f582d4455878c6373)
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 
176491a2553SLucas Coutinho #if defined(TARGET_PPC64)
177491a2553SLucas Coutinho void helper_SLBIAG(CPUPPCState *env, target_ulong rs, uint32_t l)
178491a2553SLucas Coutinho {
179491a2553SLucas Coutinho     PowerPCCPU *cpu = env_archcpu(env);
180491a2553SLucas Coutinho     int n;
181491a2553SLucas Coutinho 
182491a2553SLucas Coutinho     /*
183491a2553SLucas Coutinho      * slbiag must always flush all TLB (which is equivalent to ERAT in ppc
184491a2553SLucas Coutinho      * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
185491a2553SLucas Coutinho      * can overwrite a valid SLB without flushing its lookaside information.
186491a2553SLucas Coutinho      *
187491a2553SLucas Coutinho      * It would be possible to keep the TLB in synch with the SLB by flushing
188491a2553SLucas Coutinho      * when a valid entry is overwritten by slbmte, and therefore slbiag would
189491a2553SLucas Coutinho      * not have to flush unless it evicts a valid SLB entry. However it is
190491a2553SLucas Coutinho      * expected that slbmte is more common than slbiag, and slbiag is usually
191491a2553SLucas Coutinho      * going to evict valid SLB entries, so that tradeoff is unlikely to be a
192491a2553SLucas Coutinho      * good one.
193491a2553SLucas Coutinho      */
194491a2553SLucas Coutinho     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
195491a2553SLucas Coutinho 
196491a2553SLucas Coutinho     for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
197491a2553SLucas Coutinho         ppc_slb_t *slb = &env->slb[n];
198491a2553SLucas Coutinho         slb->esid &= ~SLB_ESID_V;
199491a2553SLucas Coutinho     }
200491a2553SLucas Coutinho }
201491a2553SLucas Coutinho #endif
202491a2553SLucas 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,
700883f2c59SPhilippe Mathieu-Daudé             "htab_base " HWADDR_FMT_plx " htab_mask " HWADDR_FMT_plx
701883f2c59SPhilippe Mathieu-Daudé             " hash " HWADDR_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,
706883f2c59SPhilippe Mathieu-Daudé             "0 htab=" HWADDR_FMT_plx "/" HWADDR_FMT_plx
707fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
708883f2c59SPhilippe Mathieu-Daudé             " hash=" HWADDR_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,
717883f2c59SPhilippe Mathieu-Daudé                 "1 htab=" HWADDR_FMT_plx "/" HWADDR_FMT_plx
718fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
719883f2c59SPhilippe Mathieu-Daudé                 " hash=" HWADDR_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 
7739201af09SNicholas Piggin static void ppc_hash64_set_isi(CPUState *cs, int mmu_idx, uint64_t slb_vsid,
7749201af09SNicholas Piggin                                uint64_t error_code)
775fcf5ef2aSThomas Huth {
7768fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
777fcf5ef2aSThomas Huth     bool vpm;
778fcf5ef2aSThomas Huth 
77903695a98SBruno Larsen (billionai)     if (!mmuidx_real(mmu_idx)) {
780fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
781fcf5ef2aSThomas Huth     } else {
7821b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
783fcf5ef2aSThomas Huth     }
78403695a98SBruno Larsen (billionai)     if (vpm && !mmuidx_hv(mmu_idx)) {
785fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HISI;
7869201af09SNicholas Piggin         env->spr[SPR_ASDR] = slb_vsid;
787fcf5ef2aSThomas Huth     } else {
788fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
789fcf5ef2aSThomas Huth     }
790fcf5ef2aSThomas Huth     env->error_code = error_code;
791fcf5ef2aSThomas Huth }
792fcf5ef2aSThomas Huth 
7939201af09SNicholas Piggin static void ppc_hash64_set_dsi(CPUState *cs, int mmu_idx, uint64_t slb_vsid,
7949201af09SNicholas Piggin                                uint64_t dar, uint64_t dsisr)
795fcf5ef2aSThomas Huth {
7968fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
797fcf5ef2aSThomas Huth     bool vpm;
798fcf5ef2aSThomas Huth 
79903695a98SBruno Larsen (billionai)     if (!mmuidx_real(mmu_idx)) {
800fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
801fcf5ef2aSThomas Huth     } else {
8021b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
803fcf5ef2aSThomas Huth     }
80403695a98SBruno Larsen (billionai)     if (vpm && !mmuidx_hv(mmu_idx)) {
805fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
806fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
807fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
8089201af09SNicholas Piggin         env->spr[SPR_ASDR] = slb_vsid;
809fcf5ef2aSThomas Huth     } else {
810fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
811fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
812fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
813fcf5ef2aSThomas Huth    }
814fcf5ef2aSThomas Huth     env->error_code = 0;
815fcf5ef2aSThomas Huth }
816fcf5ef2aSThomas Huth 
817fcf5ef2aSThomas Huth 
818a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
819a2dd4e83SBenjamin Herrenschmidt {
8207bf00dfbSLeandro Lupori     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_R;
821a2dd4e83SBenjamin Herrenschmidt 
822a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
823a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
824a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
825a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_r(cpu->vhyp, ptex, pte1);
826a2dd4e83SBenjamin Herrenschmidt         return;
827a2dd4e83SBenjamin Herrenschmidt     }
828a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
829a2dd4e83SBenjamin Herrenschmidt 
830a2dd4e83SBenjamin Herrenschmidt 
831a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
832a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01);
833a2dd4e83SBenjamin Herrenschmidt }
834a2dd4e83SBenjamin Herrenschmidt 
835a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
836a2dd4e83SBenjamin Herrenschmidt {
8377bf00dfbSLeandro Lupori     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_C;
838a2dd4e83SBenjamin Herrenschmidt 
839a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
840a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
841a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
842a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_c(cpu->vhyp, ptex, pte1);
843a2dd4e83SBenjamin Herrenschmidt         return;
844a2dd4e83SBenjamin Herrenschmidt     }
845a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
846a2dd4e83SBenjamin Herrenschmidt 
847a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
848a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80);
849a2dd4e83SBenjamin Herrenschmidt }
850a2dd4e83SBenjamin Herrenschmidt 
851a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu)
852a864a6b3SDavid Gibson {
853a864a6b3SDavid Gibson     CPUPPCState *env = &cpu->env;
854a864a6b3SDavid Gibson     /*
855d37b40daSDavid Gibson      * In theory the meanings of RMLS values are implementation
856d37b40daSDavid Gibson      * dependent.  In practice, this seems to have been the set from
857d37b40daSDavid Gibson      * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
858a864a6b3SDavid Gibson      *
859a864a6b3SDavid Gibson      * Unsupported values mean the OS has shot itself in the
860a864a6b3SDavid Gibson      * foot. Return a 0-sized RMA in this case, which we expect
861a864a6b3SDavid Gibson      * to trigger an immediate DSI or ISI
862a864a6b3SDavid Gibson      */
863a864a6b3SDavid Gibson     static const target_ulong rma_sizes[16] = {
864d37b40daSDavid Gibson         [0] = 256 * GiB,
865a864a6b3SDavid Gibson         [1] = 16 * GiB,
866a864a6b3SDavid Gibson         [2] = 1 * GiB,
867a864a6b3SDavid Gibson         [3] = 64 * MiB,
868a864a6b3SDavid Gibson         [4] = 256 * MiB,
869a864a6b3SDavid Gibson         [7] = 128 * MiB,
870a864a6b3SDavid Gibson         [8] = 32 * MiB,
871a864a6b3SDavid Gibson     };
872a864a6b3SDavid Gibson     target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT;
873a864a6b3SDavid Gibson 
874a864a6b3SDavid Gibson     return rma_sizes[rmls];
875a864a6b3SDavid Gibson }
876a864a6b3SDavid Gibson 
877*0e2a3ec3SNicholas Piggin /* Return the LLP in SLB_VSID format */
878*0e2a3ec3SNicholas Piggin static uint64_t get_vrma_llp(PowerPCCPU *cpu)
8794c24a87fSDavid Gibson {
8804c24a87fSDavid Gibson     CPUPPCState *env = &cpu->env;
881*0e2a3ec3SNicholas Piggin     uint64_t llp;
882*0e2a3ec3SNicholas Piggin 
883*0e2a3ec3SNicholas Piggin     if (env->mmu_model == POWERPC_MMU_3_00) {
884*0e2a3ec3SNicholas Piggin         ppc_v3_pate_t pate;
885*0e2a3ec3SNicholas Piggin         uint64_t ps, l, lp;
886*0e2a3ec3SNicholas Piggin 
887*0e2a3ec3SNicholas Piggin         /*
888*0e2a3ec3SNicholas Piggin          * ISA v3.0 removes the LPCR[VRMASD] field and puts the VRMA base
889*0e2a3ec3SNicholas Piggin          * page size (L||LP equivalent) in the PS field in the HPT partition
890*0e2a3ec3SNicholas Piggin          * table entry.
891*0e2a3ec3SNicholas Piggin          */
892*0e2a3ec3SNicholas Piggin         if (!ppc64_v3_get_pate(cpu, cpu->env.spr[SPR_LPIDR], &pate)) {
893*0e2a3ec3SNicholas Piggin             error_report("Bad VRMA with no partition table entry");
894*0e2a3ec3SNicholas Piggin             return 0;
895*0e2a3ec3SNicholas Piggin         }
896*0e2a3ec3SNicholas Piggin         ps = PATE0_GET_PS(pate.dw0);
897*0e2a3ec3SNicholas Piggin         /* PS has L||LP in 3 consecutive bits, put them into SLB LLP format */
898*0e2a3ec3SNicholas Piggin         l = (ps >> 2) & 0x1;
899*0e2a3ec3SNicholas Piggin         lp = ps & 0x3;
900*0e2a3ec3SNicholas Piggin         llp = (l << SLB_VSID_L_SHIFT) | (lp << SLB_VSID_LP_SHIFT);
901*0e2a3ec3SNicholas Piggin 
902*0e2a3ec3SNicholas Piggin     } else {
903*0e2a3ec3SNicholas Piggin         uint64_t lpcr = env->spr[SPR_LPCR];
904*0e2a3ec3SNicholas Piggin         target_ulong vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
905*0e2a3ec3SNicholas Piggin 
906*0e2a3ec3SNicholas Piggin         /* VRMASD LLP matches SLB format, just shift and mask it */
907*0e2a3ec3SNicholas Piggin         llp = (vrmasd << SLB_VSID_LP_SHIFT) & SLB_VSID_LLP_MASK;
908*0e2a3ec3SNicholas Piggin     }
909*0e2a3ec3SNicholas Piggin 
910*0e2a3ec3SNicholas Piggin     return llp;
911*0e2a3ec3SNicholas Piggin }
912*0e2a3ec3SNicholas Piggin 
913*0e2a3ec3SNicholas Piggin static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb)
914*0e2a3ec3SNicholas Piggin {
915*0e2a3ec3SNicholas Piggin     uint64_t llp = get_vrma_llp(cpu);
916*0e2a3ec3SNicholas Piggin     target_ulong vsid = SLB_VSID_VRMA | llp;
9174c24a87fSDavid Gibson     int i;
9184c24a87fSDavid Gibson 
9194c24a87fSDavid Gibson     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
9204c24a87fSDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
9214c24a87fSDavid Gibson 
9224c24a87fSDavid Gibson         if (!sps->page_shift) {
9234c24a87fSDavid Gibson             break;
9244c24a87fSDavid Gibson         }
9254c24a87fSDavid Gibson 
9264c24a87fSDavid Gibson         if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) {
9274c24a87fSDavid Gibson             slb->esid = SLB_ESID_V;
9284c24a87fSDavid Gibson             slb->vsid = vsid;
9294c24a87fSDavid Gibson             slb->sps = sps;
9304c24a87fSDavid Gibson             return 0;
9314c24a87fSDavid Gibson         }
9324c24a87fSDavid Gibson     }
9334c24a87fSDavid Gibson 
934*0e2a3ec3SNicholas Piggin     error_report("Bad VRMA page size encoding 0x" TARGET_FMT_lx, llp);
9354c24a87fSDavid Gibson 
9364c24a87fSDavid Gibson     return -1;
9374c24a87fSDavid Gibson }
9384c24a87fSDavid Gibson 
93951806b54SRichard Henderson bool ppc_hash64_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type,
94003695a98SBruno Larsen (billionai)                       hwaddr *raddrp, int *psizep, int *protp, int mmu_idx,
9411a8c647bSRichard Henderson                       bool guest_visible)
942fcf5ef2aSThomas Huth {
943fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
944fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
9454c24a87fSDavid Gibson     ppc_slb_t vrma_slbe;
946fcf5ef2aSThomas Huth     ppc_slb_t *slb;
947fcf5ef2aSThomas Huth     unsigned apshift;
9487222b94aSDavid Gibson     hwaddr ptex;
949fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
95007a68f99SSuraj Jitindar Singh     int exec_prot, pp_prot, amr_prot, prot;
951182357dbSRichard Henderson     int need_prot;
952fcf5ef2aSThomas Huth     hwaddr raddr;
953fcf5ef2aSThomas Huth 
954d75cbae8SDavid Gibson     /*
955d75cbae8SDavid Gibson      * Note on LPCR usage: 970 uses HID4, but our special variant of
956d75cbae8SDavid Gibson      * store_spr copies relevant fields into env->spr[SPR_LPCR].
957136fbf65Szhaolichang      * Similarly we filter unimplemented bits when storing into LPCR
958d75cbae8SDavid Gibson      * depending on the MMU version. This code can thus just use the
959d75cbae8SDavid Gibson      * LPCR "as-is".
960fcf5ef2aSThomas Huth      */
961fcf5ef2aSThomas Huth 
962fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
96303695a98SBruno Larsen (billionai)     if (mmuidx_real(mmu_idx)) {
964d75cbae8SDavid Gibson         /*
965d75cbae8SDavid Gibson          * Translation is supposedly "off", but in real mode the top 4
966d75cbae8SDavid Gibson          * effective address bits are (mostly) ignored
967d75cbae8SDavid Gibson          */
968fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
969fcf5ef2aSThomas Huth 
970682c1dfbSDavid Gibson         if (cpu->vhyp) {
971682c1dfbSDavid Gibson             /*
972682c1dfbSDavid Gibson              * In virtual hypervisor mode, there's nothing to do:
973682c1dfbSDavid Gibson              *   EA == GPA == qemu guest address
974682c1dfbSDavid Gibson              */
97503695a98SBruno Larsen (billionai)         } else if (mmuidx_hv(mmu_idx) || !env->has_hv_mode) {
976fcf5ef2aSThomas Huth             /* In HV mode, add HRMOR if top EA bit is clear */
977fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
978fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
979fcf5ef2aSThomas Huth             }
9801b99e029SDavid Gibson         } else if (ppc_hash64_use_vrma(env)) {
981682c1dfbSDavid Gibson             /* Emulated VRMA mode */
9824c24a87fSDavid Gibson             slb = &vrma_slbe;
9834c24a87fSDavid Gibson             if (build_vrma_slbe(cpu, slb) != 0) {
984682c1dfbSDavid Gibson                 /* Invalid VRMA setup, machine check */
9851a8c647bSRichard Henderson                 if (guest_visible) {
986fcf5ef2aSThomas Huth                     cs->exception_index = POWERPC_EXCP_MCHECK;
987fcf5ef2aSThomas Huth                     env->error_code = 0;
9881a8c647bSRichard Henderson                 }
9891a8c647bSRichard Henderson                 return false;
990682c1dfbSDavid Gibson             }
991682c1dfbSDavid Gibson 
992682c1dfbSDavid Gibson             goto skip_slb_search;
993fcf5ef2aSThomas Huth         } else {
9943a56a55cSDavid Gibson             target_ulong limit = rmls_limit(cpu);
9953a56a55cSDavid Gibson 
996682c1dfbSDavid Gibson             /* Emulated old-style RMO mode, bounds check against RMLS */
9973a56a55cSDavid Gibson             if (raddr >= limit) {
9981a8c647bSRichard Henderson                 if (!guest_visible) {
9991a8c647bSRichard Henderson                     return false;
10001a8c647bSRichard Henderson                 }
100159dec5bfSRichard Henderson                 switch (access_type) {
100259dec5bfSRichard Henderson                 case MMU_INST_FETCH:
10039201af09SNicholas Piggin                     ppc_hash64_set_isi(cs, mmu_idx, 0, SRR1_PROTFAULT);
100459dec5bfSRichard Henderson                     break;
100559dec5bfSRichard Henderson                 case MMU_DATA_LOAD:
10069201af09SNicholas Piggin                     ppc_hash64_set_dsi(cs, mmu_idx, 0, eaddr, DSISR_PROTFAULT);
100759dec5bfSRichard Henderson                     break;
100859dec5bfSRichard Henderson                 case MMU_DATA_STORE:
10099201af09SNicholas Piggin                     ppc_hash64_set_dsi(cs, mmu_idx, 0, eaddr,
101059dec5bfSRichard Henderson                                        DSISR_PROTFAULT | DSISR_ISSTORE);
101159dec5bfSRichard Henderson                     break;
101259dec5bfSRichard Henderson                 default:
101359dec5bfSRichard Henderson                     g_assert_not_reached();
1014fcf5ef2aSThomas Huth                 }
10151a8c647bSRichard Henderson                 return false;
1016fcf5ef2aSThomas Huth             }
1017682c1dfbSDavid Gibson 
1018682c1dfbSDavid Gibson             raddr |= env->spr[SPR_RMOR];
1019fcf5ef2aSThomas Huth         }
10201a8c647bSRichard Henderson 
10211a8c647bSRichard Henderson         *raddrp = raddr;
10221a8c647bSRichard Henderson         *protp = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10231a8c647bSRichard Henderson         *psizep = TARGET_PAGE_BITS;
10241a8c647bSRichard Henderson         return true;
1025fcf5ef2aSThomas Huth     }
1026fcf5ef2aSThomas Huth 
1027fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
1028fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
1029fcf5ef2aSThomas Huth     if (!slb) {
1030b2899495SSuraj Jitindar Singh         /* No entry found, check if in-memory segment tables are in use */
1031ca79b3b7SDavid Gibson         if (ppc64_use_proc_tbl(cpu)) {
1032b2899495SSuraj Jitindar Singh             /* TODO - Unsupported */
1033b2899495SSuraj Jitindar Singh             error_report("Segment Table Support Unimplemented");
1034b2899495SSuraj Jitindar Singh             exit(1);
1035b2899495SSuraj Jitindar Singh         }
1036b2899495SSuraj Jitindar Singh         /* Segment still not found, generate the appropriate interrupt */
10371a8c647bSRichard Henderson         if (!guest_visible) {
10381a8c647bSRichard Henderson             return false;
10391a8c647bSRichard Henderson         }
104059dec5bfSRichard Henderson         switch (access_type) {
104159dec5bfSRichard Henderson         case MMU_INST_FETCH:
1042fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
1043fcf5ef2aSThomas Huth             env->error_code = 0;
104459dec5bfSRichard Henderson             break;
104559dec5bfSRichard Henderson         case MMU_DATA_LOAD:
104659dec5bfSRichard Henderson         case MMU_DATA_STORE:
1047fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
1048fcf5ef2aSThomas Huth             env->error_code = 0;
1049fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
105059dec5bfSRichard Henderson             break;
105159dec5bfSRichard Henderson         default:
105259dec5bfSRichard Henderson             g_assert_not_reached();
1053fcf5ef2aSThomas Huth         }
10541a8c647bSRichard Henderson         return false;
1055fcf5ef2aSThomas Huth     }
1056fcf5ef2aSThomas Huth 
1057fcf5ef2aSThomas Huth  skip_slb_search:
1058fcf5ef2aSThomas Huth 
1059fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
106059dec5bfSRichard Henderson     if (access_type == MMU_INST_FETCH && (slb->vsid & SLB_VSID_N)) {
10611a8c647bSRichard Henderson         if (guest_visible) {
10629201af09SNicholas Piggin             ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, SRR1_NOEXEC_GUARD);
10631a8c647bSRichard Henderson         }
10641a8c647bSRichard Henderson         return false;
1065fcf5ef2aSThomas Huth     }
1066fcf5ef2aSThomas Huth 
1067fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
10687222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
10697222b94aSDavid Gibson     if (ptex == -1) {
10701a8c647bSRichard Henderson         if (!guest_visible) {
10711a8c647bSRichard Henderson             return false;
10721a8c647bSRichard Henderson         }
107359dec5bfSRichard Henderson         switch (access_type) {
107459dec5bfSRichard Henderson         case MMU_INST_FETCH:
10759201af09SNicholas Piggin             ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, SRR1_NOPTE);
107659dec5bfSRichard Henderson             break;
107759dec5bfSRichard Henderson         case MMU_DATA_LOAD:
10789201af09SNicholas Piggin             ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, DSISR_NOPTE);
107959dec5bfSRichard Henderson             break;
108059dec5bfSRichard Henderson         case MMU_DATA_STORE:
10819201af09SNicholas Piggin             ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr,
10829201af09SNicholas Piggin                                DSISR_NOPTE | DSISR_ISSTORE);
108359dec5bfSRichard Henderson             break;
108459dec5bfSRichard Henderson         default:
108559dec5bfSRichard Henderson             g_assert_not_reached();
1086fcf5ef2aSThomas Huth         }
10871a8c647bSRichard Henderson         return false;
1088fcf5ef2aSThomas Huth     }
1089fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
10907222b94aSDavid Gibson                   "found PTE at index %08" HWADDR_PRIx "\n", ptex);
1091fcf5ef2aSThomas Huth 
1092fcf5ef2aSThomas Huth     /* 5. Check access permissions */
1093fcf5ef2aSThomas Huth 
109407a68f99SSuraj Jitindar Singh     exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
109503695a98SBruno Larsen (billionai)     pp_prot = ppc_hash64_pte_prot(mmu_idx, slb, pte);
1096fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
109707a68f99SSuraj Jitindar Singh     prot = exec_prot & pp_prot & amr_prot;
1098fcf5ef2aSThomas Huth 
109959dec5bfSRichard Henderson     need_prot = prot_for_access_type(access_type);
1100182357dbSRichard Henderson     if (need_prot & ~prot) {
1101fcf5ef2aSThomas Huth         /* Access right violation */
1102fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
11031a8c647bSRichard Henderson         if (!guest_visible) {
11041a8c647bSRichard Henderson             return false;
11051a8c647bSRichard Henderson         }
110659dec5bfSRichard Henderson         if (access_type == MMU_INST_FETCH) {
1107a6152b52SSuraj Jitindar Singh             int srr1 = 0;
110807a68f99SSuraj Jitindar Singh             if (PAGE_EXEC & ~exec_prot) {
110907a68f99SSuraj Jitindar Singh                 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
111007a68f99SSuraj Jitindar Singh             } else if (PAGE_EXEC & ~pp_prot) {
1111a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
1112a6152b52SSuraj Jitindar Singh             }
1113a6152b52SSuraj Jitindar Singh             if (PAGE_EXEC & ~amr_prot) {
1114a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
1115a6152b52SSuraj Jitindar Singh             }
11169201af09SNicholas Piggin             ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, srr1);
1117fcf5ef2aSThomas Huth         } else {
1118da82c73aSSuraj Jitindar Singh             int dsisr = 0;
1119182357dbSRichard Henderson             if (need_prot & ~pp_prot) {
1120da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_PROTFAULT;
1121fcf5ef2aSThomas Huth             }
112259dec5bfSRichard Henderson             if (access_type == MMU_DATA_STORE) {
1123da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
1124fcf5ef2aSThomas Huth             }
1125182357dbSRichard Henderson             if (need_prot & ~amr_prot) {
1126da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_AMR;
1127fcf5ef2aSThomas Huth             }
11289201af09SNicholas Piggin             ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, dsisr);
1129fcf5ef2aSThomas Huth         }
11301a8c647bSRichard Henderson         return false;
1131fcf5ef2aSThomas Huth     }
1132fcf5ef2aSThomas Huth 
1133fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
1134fcf5ef2aSThomas Huth 
1135fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
1136fcf5ef2aSThomas Huth 
1137a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_R)) {
1138a2dd4e83SBenjamin Herrenschmidt         ppc_hash64_set_r(cpu, ptex, pte.pte1);
1139a2dd4e83SBenjamin Herrenschmidt     }
1140a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_C)) {
114159dec5bfSRichard Henderson         if (access_type == MMU_DATA_STORE) {
1142a2dd4e83SBenjamin Herrenschmidt             ppc_hash64_set_c(cpu, ptex, pte.pte1);
1143fcf5ef2aSThomas Huth         } else {
1144d75cbae8SDavid Gibson             /*
1145d75cbae8SDavid Gibson              * Treat the page as read-only for now, so that a later write
1146d75cbae8SDavid Gibson              * will pass through this function again to set the C bit
1147d75cbae8SDavid Gibson              */
1148fcf5ef2aSThomas Huth             prot &= ~PAGE_WRITE;
1149fcf5ef2aSThomas Huth         }
1150fcf5ef2aSThomas Huth     }
1151fcf5ef2aSThomas Huth 
1152fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
1153fcf5ef2aSThomas Huth 
11541a8c647bSRichard Henderson     *raddrp = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
11551a8c647bSRichard Henderson     *protp = prot;
11561a8c647bSRichard Henderson     *psizep = apshift;
11571a8c647bSRichard Henderson     return true;
11581a8c647bSRichard Henderson }
11591a8c647bSRichard Henderson 
11607222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
1161fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
1162fcf5ef2aSThomas Huth {
1163fcf5ef2aSThomas Huth     /*
1164fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
1165fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
1166fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
1167fcf5ef2aSThomas Huth      */
1168fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
1169fcf5ef2aSThomas Huth }
1170fcf5ef2aSThomas Huth 
11712b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
11725ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val)
11735ad55315SDavid Gibson {
1174db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
11755ad55315SDavid Gibson 
11765ad55315SDavid Gibson     ppc_store_lpcr(cpu, val);
11775ad55315SDavid Gibson }
11782b44e219SBruno Larsen (billionai) #endif
11795ad55315SDavid Gibson 
1180a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu)
1181a059471dSDavid Gibson {
1182a059471dSDavid Gibson     CPUPPCState *env = &cpu->env;
1183a059471dSDavid Gibson     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1184a059471dSDavid Gibson 
118521e405f1SDavid Gibson     if (!pcc->hash64_opts) {
1186d57d72a8SGreg Kurz         assert(!mmu_is_64bit(env->mmu_model));
118721e405f1SDavid Gibson         return;
118821e405f1SDavid Gibson     }
118921e405f1SDavid Gibson 
119021e405f1SDavid Gibson     cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
119121e405f1SDavid Gibson }
119221e405f1SDavid Gibson 
119321e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu)
119421e405f1SDavid Gibson {
119521e405f1SDavid Gibson     g_free(cpu->hash64_opts);
119621e405f1SDavid Gibson }
119721e405f1SDavid Gibson 
119821e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = {
119958969eeeSDavid Gibson     .flags = 0,
120067d7d66fSDavid Gibson     .slb_size = 64,
1201a059471dSDavid Gibson     .sps = {
1202a059471dSDavid Gibson         { .page_shift = 12, /* 4K */
1203a059471dSDavid Gibson           .slb_enc = 0,
1204a059471dSDavid Gibson           .enc = { { .page_shift = 12, .pte_enc = 0 } }
1205a059471dSDavid Gibson         },
1206a059471dSDavid Gibson         { .page_shift = 24, /* 16M */
1207a059471dSDavid Gibson           .slb_enc = 0x100,
1208a059471dSDavid Gibson           .enc = { { .page_shift = 24, .pte_enc = 0 } }
1209a059471dSDavid Gibson         },
1210a059471dSDavid Gibson     },
1211a059471dSDavid Gibson };
1212b07c59f7SDavid Gibson 
1213b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = {
121426cd35b8SDavid Gibson     .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
121567d7d66fSDavid Gibson     .slb_size = 32,
1216b07c59f7SDavid Gibson     .sps = {
1217b07c59f7SDavid Gibson         {
1218b07c59f7SDavid Gibson             .page_shift = 12, /* 4K */
1219b07c59f7SDavid Gibson             .slb_enc = 0,
1220b07c59f7SDavid Gibson             .enc = { { .page_shift = 12, .pte_enc = 0 },
1221b07c59f7SDavid Gibson                      { .page_shift = 16, .pte_enc = 0x7 },
1222b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x38 }, },
1223b07c59f7SDavid Gibson         },
1224b07c59f7SDavid Gibson         {
1225b07c59f7SDavid Gibson             .page_shift = 16, /* 64K */
1226b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_64K,
1227b07c59f7SDavid Gibson             .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1228b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x8 }, },
1229b07c59f7SDavid Gibson         },
1230b07c59f7SDavid Gibson         {
1231b07c59f7SDavid Gibson             .page_shift = 24, /* 16M */
1232b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16M,
1233b07c59f7SDavid Gibson             .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1234b07c59f7SDavid Gibson         },
1235b07c59f7SDavid Gibson         {
1236b07c59f7SDavid Gibson             .page_shift = 34, /* 16G */
1237b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16G,
1238b07c59f7SDavid Gibson             .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },
1239b07c59f7SDavid Gibson         },
1240b07c59f7SDavid Gibson     }
1241b07c59f7SDavid Gibson };
124227f00f0aSDavid Gibson 
124327f00f0aSDavid Gibson 
1244