xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision 74781c0888e819552538593c0932d98ea16c766b)
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"
24*74781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
25fcf5ef2aSThomas Huth #include "qemu/error-report.h"
26fad866daSMarkus Armbruster #include "qemu/qemu-print.h"
27b3946626SVincent Palatin #include "sysemu/hw_accel.h"
28fcf5ef2aSThomas Huth #include "kvm_ppc.h"
29fcf5ef2aSThomas Huth #include "mmu-hash64.h"
30fcf5ef2aSThomas Huth #include "exec/log.h"
317222b94aSDavid Gibson #include "hw/hw.h"
32182357dbSRichard Henderson #include "internal.h"
33b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
34f03de3b4SRichard Henderson #include "helper_regs.h"
35fcf5ef2aSThomas Huth 
362b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
372b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h"
382b44e219SBruno Larsen (billionai) #endif
392b44e219SBruno Larsen (billionai) 
40d75cbae8SDavid Gibson /* #define DEBUG_SLB */
41fcf5ef2aSThomas Huth 
42fcf5ef2aSThomas Huth #ifdef DEBUG_SLB
43fcf5ef2aSThomas Huth #  define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
44fcf5ef2aSThomas Huth #else
45fcf5ef2aSThomas Huth #  define LOG_SLB(...) do { } while (0)
46fcf5ef2aSThomas Huth #endif
47fcf5ef2aSThomas Huth 
48fcf5ef2aSThomas Huth /*
49fcf5ef2aSThomas Huth  * SLB handling
50fcf5ef2aSThomas Huth  */
51fcf5ef2aSThomas Huth 
52fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr)
53fcf5ef2aSThomas Huth {
54fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
55fcf5ef2aSThomas Huth     uint64_t esid_256M, esid_1T;
56fcf5ef2aSThomas Huth     int n;
57fcf5ef2aSThomas Huth 
58fcf5ef2aSThomas Huth     LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr);
59fcf5ef2aSThomas Huth 
60fcf5ef2aSThomas Huth     esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V;
61fcf5ef2aSThomas Huth     esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V;
62fcf5ef2aSThomas Huth 
6367d7d66fSDavid Gibson     for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
64fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
65fcf5ef2aSThomas Huth 
66fcf5ef2aSThomas Huth         LOG_SLB("%s: slot %d %016" PRIx64 " %016"
67fcf5ef2aSThomas Huth                     PRIx64 "\n", __func__, n, slb->esid, slb->vsid);
68d75cbae8SDavid Gibson         /*
69d75cbae8SDavid Gibson          * We check for 1T matches on all MMUs here - if the MMU
70fcf5ef2aSThomas Huth          * doesn't have 1T segment support, we will have prevented 1T
71d75cbae8SDavid Gibson          * entries from being inserted in the slbmte code.
72d75cbae8SDavid Gibson          */
73fcf5ef2aSThomas Huth         if (((slb->esid == esid_256M) &&
74fcf5ef2aSThomas Huth              ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M))
75fcf5ef2aSThomas Huth             || ((slb->esid == esid_1T) &&
76fcf5ef2aSThomas Huth                 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) {
77fcf5ef2aSThomas Huth             return slb;
78fcf5ef2aSThomas Huth         }
79fcf5ef2aSThomas Huth     }
80fcf5ef2aSThomas Huth 
81fcf5ef2aSThomas Huth     return NULL;
82fcf5ef2aSThomas Huth }
83fcf5ef2aSThomas Huth 
84fad866daSMarkus Armbruster void dump_slb(PowerPCCPU *cpu)
85fcf5ef2aSThomas Huth {
86fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
87fcf5ef2aSThomas Huth     int i;
88fcf5ef2aSThomas Huth     uint64_t slbe, slbv;
89fcf5ef2aSThomas Huth 
90fcf5ef2aSThomas Huth     cpu_synchronize_state(CPU(cpu));
91fcf5ef2aSThomas Huth 
92fad866daSMarkus Armbruster     qemu_printf("SLB\tESID\t\t\tVSID\n");
9367d7d66fSDavid Gibson     for (i = 0; i < cpu->hash64_opts->slb_size; i++) {
94fcf5ef2aSThomas Huth         slbe = env->slb[i].esid;
95fcf5ef2aSThomas Huth         slbv = env->slb[i].vsid;
96fcf5ef2aSThomas Huth         if (slbe == 0 && slbv == 0) {
97fcf5ef2aSThomas Huth             continue;
98fcf5ef2aSThomas Huth         }
99fad866daSMarkus Armbruster         qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n",
100fcf5ef2aSThomas Huth                     i, slbe, slbv);
101fcf5ef2aSThomas Huth     }
102fcf5ef2aSThomas Huth }
103fcf5ef2aSThomas Huth 
1042b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
1052bfcb7a3SLucas Coutinho void helper_SLBIA(CPUPPCState *env, uint32_t ih)
106fcf5ef2aSThomas Huth {
107db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
1080418bf78SNicholas Piggin     int starting_entry;
109fcf5ef2aSThomas Huth     int n;
110fcf5ef2aSThomas Huth 
111f9e3e1a3SNicholas Piggin     /*
112f9e3e1a3SNicholas Piggin      * slbia must always flush all TLB (which is equivalent to ERAT in ppc
113f9e3e1a3SNicholas Piggin      * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
114f9e3e1a3SNicholas Piggin      * can overwrite a valid SLB without flushing its lookaside information.
115f9e3e1a3SNicholas Piggin      *
116f9e3e1a3SNicholas Piggin      * It would be possible to keep the TLB in synch with the SLB by flushing
117f9e3e1a3SNicholas Piggin      * when a valid entry is overwritten by slbmte, and therefore slbia would
118f9e3e1a3SNicholas Piggin      * not have to flush unless it evicts a valid SLB entry. However it is
119f9e3e1a3SNicholas Piggin      * expected that slbmte is more common than slbia, and slbia is usually
120f9e3e1a3SNicholas Piggin      * going to evict valid SLB entries, so that tradeoff is unlikely to be a
121f9e3e1a3SNicholas Piggin      * good one.
1220418bf78SNicholas Piggin      *
1230418bf78SNicholas Piggin      * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate
1240418bf78SNicholas Piggin      * the same SLB entries (everything but entry 0), but differ in what
1250418bf78SNicholas Piggin      * "lookaside information" is invalidated. TCG can ignore this and flush
1260418bf78SNicholas Piggin      * everything.
1270418bf78SNicholas Piggin      *
1280418bf78SNicholas Piggin      * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are
1290418bf78SNicholas Piggin      * invalidated.
130f9e3e1a3SNicholas Piggin      */
131f9e3e1a3SNicholas Piggin 
1320418bf78SNicholas Piggin     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
1330418bf78SNicholas Piggin 
1340418bf78SNicholas Piggin     starting_entry = 1; /* default for IH=0,1,2,6 */
1350418bf78SNicholas Piggin 
1360418bf78SNicholas Piggin     if (env->mmu_model == POWERPC_MMU_3_00) {
1370418bf78SNicholas Piggin         switch (ih) {
1380418bf78SNicholas Piggin         case 0x7:
1390418bf78SNicholas Piggin             /* invalidate no SLBs, but all lookaside information */
1400418bf78SNicholas Piggin             return;
1410418bf78SNicholas Piggin 
1420418bf78SNicholas Piggin         case 0x3:
1430418bf78SNicholas Piggin         case 0x4:
1440418bf78SNicholas Piggin             /* also considers SLB entry 0 */
1450418bf78SNicholas Piggin             starting_entry = 0;
1460418bf78SNicholas Piggin             break;
1470418bf78SNicholas Piggin 
1480418bf78SNicholas Piggin         case 0x5:
1490418bf78SNicholas Piggin             /* treat undefined values as ih==0, and warn */
1500418bf78SNicholas Piggin             qemu_log_mask(LOG_GUEST_ERROR,
1510418bf78SNicholas Piggin                           "slbia undefined IH field %u.\n", ih);
1520418bf78SNicholas Piggin             break;
1530418bf78SNicholas Piggin 
1540418bf78SNicholas Piggin         default:
1550418bf78SNicholas Piggin             /* 0,1,2,6 */
1560418bf78SNicholas Piggin             break;
1570418bf78SNicholas Piggin         }
1580418bf78SNicholas Piggin     }
1590418bf78SNicholas Piggin 
1600418bf78SNicholas Piggin     for (n = starting_entry; n < cpu->hash64_opts->slb_size; n++) {
161fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
162fcf5ef2aSThomas Huth 
1630418bf78SNicholas Piggin         if (!(slb->esid & SLB_ESID_V)) {
1640418bf78SNicholas Piggin             continue;
1650418bf78SNicholas Piggin         }
1660418bf78SNicholas Piggin         if (env->mmu_model == POWERPC_MMU_3_00) {
1670418bf78SNicholas Piggin             if (ih == 0x3 && (slb->vsid & SLB_VSID_C) == 0) {
1680418bf78SNicholas Piggin                 /* preserves entries with a class value of 0 */
1690418bf78SNicholas Piggin                 continue;
170f9e3e1a3SNicholas Piggin             }
171f9e3e1a3SNicholas Piggin         }
172f9e3e1a3SNicholas Piggin 
1730418bf78SNicholas Piggin         slb->esid &= ~SLB_ESID_V;
1740418bf78SNicholas Piggin     }
175fcf5ef2aSThomas Huth }
176fcf5ef2aSThomas Huth 
177491a2553SLucas Coutinho #if defined(TARGET_PPC64)
178491a2553SLucas Coutinho void helper_SLBIAG(CPUPPCState *env, target_ulong rs, uint32_t l)
179491a2553SLucas Coutinho {
180491a2553SLucas Coutinho     PowerPCCPU *cpu = env_archcpu(env);
181491a2553SLucas Coutinho     int n;
182491a2553SLucas Coutinho 
183491a2553SLucas Coutinho     /*
184491a2553SLucas Coutinho      * slbiag must always flush all TLB (which is equivalent to ERAT in ppc
185491a2553SLucas Coutinho      * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
186491a2553SLucas Coutinho      * can overwrite a valid SLB without flushing its lookaside information.
187491a2553SLucas Coutinho      *
188491a2553SLucas Coutinho      * It would be possible to keep the TLB in synch with the SLB by flushing
189491a2553SLucas Coutinho      * when a valid entry is overwritten by slbmte, and therefore slbiag would
190491a2553SLucas Coutinho      * not have to flush unless it evicts a valid SLB entry. However it is
191491a2553SLucas Coutinho      * expected that slbmte is more common than slbiag, and slbiag is usually
192491a2553SLucas Coutinho      * going to evict valid SLB entries, so that tradeoff is unlikely to be a
193491a2553SLucas Coutinho      * good one.
194491a2553SLucas Coutinho      */
195491a2553SLucas Coutinho     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
196491a2553SLucas Coutinho 
197491a2553SLucas Coutinho     for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
198491a2553SLucas Coutinho         ppc_slb_t *slb = &env->slb[n];
199491a2553SLucas Coutinho         slb->esid &= ~SLB_ESID_V;
200491a2553SLucas Coutinho     }
201491a2553SLucas Coutinho }
202491a2553SLucas Coutinho #endif
203491a2553SLucas Coutinho 
204a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr,
205a63f1dfcSNikunj A Dadhania                            target_ulong global)
206fcf5ef2aSThomas Huth {
207db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
208fcf5ef2aSThomas Huth     ppc_slb_t *slb;
209fcf5ef2aSThomas Huth 
210fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, addr);
211fcf5ef2aSThomas Huth     if (!slb) {
212fcf5ef2aSThomas Huth         return;
213fcf5ef2aSThomas Huth     }
214fcf5ef2aSThomas Huth 
215fcf5ef2aSThomas Huth     if (slb->esid & SLB_ESID_V) {
216fcf5ef2aSThomas Huth         slb->esid &= ~SLB_ESID_V;
217fcf5ef2aSThomas Huth 
218d75cbae8SDavid Gibson         /*
219d75cbae8SDavid Gibson          * XXX: given the fact that segment size is 256 MB or 1TB,
220fcf5ef2aSThomas Huth          *      and we still don't have a tlb_flush_mask(env, n, mask)
221fcf5ef2aSThomas Huth          *      in QEMU, we just invalidate all TLBs
222fcf5ef2aSThomas Huth          */
223a63f1dfcSNikunj A Dadhania         env->tlb_need_flush |=
224a63f1dfcSNikunj A Dadhania             (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH);
225fcf5ef2aSThomas Huth     }
226fcf5ef2aSThomas Huth }
227fcf5ef2aSThomas Huth 
22843507e47SLucas Coutinho void helper_SLBIE(CPUPPCState *env, target_ulong addr)
229a63f1dfcSNikunj A Dadhania {
230a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, false);
231a63f1dfcSNikunj A Dadhania }
232a63f1dfcSNikunj A Dadhania 
233a1b05c06SLucas Coutinho void helper_SLBIEG(CPUPPCState *env, target_ulong addr)
234a63f1dfcSNikunj A Dadhania {
235a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, true);
236a63f1dfcSNikunj A Dadhania }
2372b44e219SBruno Larsen (billionai) #endif
238a63f1dfcSNikunj A Dadhania 
239fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot,
240fcf5ef2aSThomas Huth                   target_ulong esid, target_ulong vsid)
241fcf5ef2aSThomas Huth {
242fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
243fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
244b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
245fcf5ef2aSThomas Huth     int i;
246fcf5ef2aSThomas Huth 
24767d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
248fcf5ef2aSThomas Huth         return -1; /* Bad slot number */
249fcf5ef2aSThomas Huth     }
250fcf5ef2aSThomas Huth     if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) {
251fcf5ef2aSThomas Huth         return -1; /* Reserved bits set */
252fcf5ef2aSThomas Huth     }
253fcf5ef2aSThomas Huth     if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
254fcf5ef2aSThomas Huth         return -1; /* Bad segment size */
255fcf5ef2aSThomas Huth     }
25658969eeeSDavid Gibson     if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) {
257fcf5ef2aSThomas Huth         return -1; /* 1T segment on MMU that doesn't support it */
258fcf5ef2aSThomas Huth     }
259fcf5ef2aSThomas Huth 
260fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
261b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
262fcf5ef2aSThomas Huth 
263fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
264fcf5ef2aSThomas Huth             break;
265fcf5ef2aSThomas Huth         }
266fcf5ef2aSThomas Huth 
267fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
268fcf5ef2aSThomas Huth             sps = sps1;
269fcf5ef2aSThomas Huth             break;
270fcf5ef2aSThomas Huth         }
271fcf5ef2aSThomas Huth     }
272fcf5ef2aSThomas Huth 
273fcf5ef2aSThomas Huth     if (!sps) {
274fcf5ef2aSThomas Huth         error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
275fcf5ef2aSThomas Huth                      " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx,
276fcf5ef2aSThomas Huth                      slot, esid, vsid);
277fcf5ef2aSThomas Huth         return -1;
278fcf5ef2aSThomas Huth     }
279fcf5ef2aSThomas Huth 
280fcf5ef2aSThomas Huth     slb->esid = esid;
281fcf5ef2aSThomas Huth     slb->vsid = vsid;
282fcf5ef2aSThomas Huth     slb->sps = sps;
283fcf5ef2aSThomas Huth 
28476134d48SSuraj Jitindar Singh     LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx
28576134d48SSuraj Jitindar Singh             " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid,
286fcf5ef2aSThomas Huth             slb->esid, slb->vsid);
287fcf5ef2aSThomas Huth 
288fcf5ef2aSThomas Huth     return 0;
289fcf5ef2aSThomas Huth }
290fcf5ef2aSThomas Huth 
2912b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
292fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb,
293fcf5ef2aSThomas Huth                              target_ulong *rt)
294fcf5ef2aSThomas Huth {
295fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
296fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
297fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
298fcf5ef2aSThomas Huth 
29967d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
300fcf5ef2aSThomas Huth         return -1;
301fcf5ef2aSThomas Huth     }
302fcf5ef2aSThomas Huth 
303fcf5ef2aSThomas Huth     *rt = slb->esid;
304fcf5ef2aSThomas Huth     return 0;
305fcf5ef2aSThomas Huth }
306fcf5ef2aSThomas Huth 
307fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
308fcf5ef2aSThomas Huth                              target_ulong *rt)
309fcf5ef2aSThomas Huth {
310fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
311fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
312fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
313fcf5ef2aSThomas Huth 
31467d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
315fcf5ef2aSThomas Huth         return -1;
316fcf5ef2aSThomas Huth     }
317fcf5ef2aSThomas Huth 
318fcf5ef2aSThomas Huth     *rt = slb->vsid;
319fcf5ef2aSThomas Huth     return 0;
320fcf5ef2aSThomas Huth }
321fcf5ef2aSThomas Huth 
322fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
323fcf5ef2aSThomas Huth                              target_ulong *rt)
324fcf5ef2aSThomas Huth {
325fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
326fcf5ef2aSThomas Huth     ppc_slb_t *slb;
327fcf5ef2aSThomas Huth 
328fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, env->msr)) {
329fcf5ef2aSThomas Huth         rb &= 0xffffffff;
330fcf5ef2aSThomas Huth     }
331fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, rb);
332fcf5ef2aSThomas Huth     if (slb == NULL) {
333fcf5ef2aSThomas Huth         *rt = (target_ulong)-1ul;
334fcf5ef2aSThomas Huth     } else {
335fcf5ef2aSThomas Huth         *rt = slb->vsid;
336fcf5ef2aSThomas Huth     }
337fcf5ef2aSThomas Huth     return 0;
338fcf5ef2aSThomas Huth }
339fcf5ef2aSThomas Huth 
3400b0ba40fSLucas Coutinho void helper_SLBMTE(CPUPPCState *env, target_ulong rb, target_ulong rs)
341fcf5ef2aSThomas Huth {
342db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
343fcf5ef2aSThomas Huth 
344fcf5ef2aSThomas Huth     if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) {
345fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
346fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
347fcf5ef2aSThomas Huth     }
348fcf5ef2aSThomas Huth }
349fcf5ef2aSThomas Huth 
35041b60e46SLucas Coutinho target_ulong helper_SLBMFEE(CPUPPCState *env, target_ulong rb)
351fcf5ef2aSThomas Huth {
352db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
353fcf5ef2aSThomas Huth     target_ulong rt = 0;
354fcf5ef2aSThomas Huth 
355fcf5ef2aSThomas Huth     if (ppc_load_slb_esid(cpu, rb, &rt) < 0) {
356fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
357fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
358fcf5ef2aSThomas Huth     }
359fcf5ef2aSThomas Huth     return rt;
360fcf5ef2aSThomas Huth }
361fcf5ef2aSThomas Huth 
36226d02c9dSLucas Coutinho target_ulong helper_SLBFEE(CPUPPCState *env, target_ulong rb)
363fcf5ef2aSThomas Huth {
364db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
365fcf5ef2aSThomas Huth     target_ulong rt = 0;
366fcf5ef2aSThomas Huth 
367fcf5ef2aSThomas Huth     if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) {
368fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
369fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
370fcf5ef2aSThomas Huth     }
371fcf5ef2aSThomas Huth     return rt;
372fcf5ef2aSThomas Huth }
373fcf5ef2aSThomas Huth 
37474a15384SLucas Coutinho target_ulong helper_SLBMFEV(CPUPPCState *env, target_ulong rb)
375fcf5ef2aSThomas Huth {
376db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
377fcf5ef2aSThomas Huth     target_ulong rt = 0;
378fcf5ef2aSThomas Huth 
379fcf5ef2aSThomas Huth     if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) {
380fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
381fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
382fcf5ef2aSThomas Huth     }
383fcf5ef2aSThomas Huth     return rt;
384fcf5ef2aSThomas Huth }
3852b44e219SBruno Larsen (billionai) #endif
386fcf5ef2aSThomas Huth 
38707a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */
38807a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu,
38907a68f99SSuraj Jitindar Singh                                               ppc_hash_pte64_t pte)
39007a68f99SSuraj Jitindar Singh {
39107a68f99SSuraj Jitindar Singh     /* Exec permissions CANNOT take away read or write permissions */
39207a68f99SSuraj Jitindar Singh     return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ?
39307a68f99SSuraj Jitindar Singh             PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC;
39407a68f99SSuraj Jitindar Singh }
39507a68f99SSuraj Jitindar Singh 
39607a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */
39703695a98SBruno Larsen (billionai) static int ppc_hash64_pte_prot(int mmu_idx,
398fcf5ef2aSThomas Huth                                ppc_slb_t *slb, ppc_hash_pte64_t pte)
399fcf5ef2aSThomas Huth {
400fcf5ef2aSThomas Huth     unsigned pp, key;
401d75cbae8SDavid Gibson     /*
402d75cbae8SDavid Gibson      * Some pp bit combinations have undefined behaviour, so default
403d75cbae8SDavid Gibson      * to no access in those cases
404d75cbae8SDavid Gibson      */
405fcf5ef2aSThomas Huth     int prot = 0;
406fcf5ef2aSThomas Huth 
40703695a98SBruno Larsen (billionai)     key = !!(mmuidx_pr(mmu_idx) ? (slb->vsid & SLB_VSID_KP)
408fcf5ef2aSThomas Huth              : (slb->vsid & SLB_VSID_KS));
409fcf5ef2aSThomas Huth     pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
410fcf5ef2aSThomas Huth 
411fcf5ef2aSThomas Huth     if (key == 0) {
412fcf5ef2aSThomas Huth         switch (pp) {
413fcf5ef2aSThomas Huth         case 0x0:
414fcf5ef2aSThomas Huth         case 0x1:
415fcf5ef2aSThomas Huth         case 0x2:
416347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
417fcf5ef2aSThomas Huth             break;
418fcf5ef2aSThomas Huth 
419fcf5ef2aSThomas Huth         case 0x3:
420fcf5ef2aSThomas Huth         case 0x6:
421347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
422fcf5ef2aSThomas Huth             break;
423fcf5ef2aSThomas Huth         }
424fcf5ef2aSThomas Huth     } else {
425fcf5ef2aSThomas Huth         switch (pp) {
426fcf5ef2aSThomas Huth         case 0x0:
427fcf5ef2aSThomas Huth         case 0x6:
428fcf5ef2aSThomas Huth             break;
429fcf5ef2aSThomas Huth 
430fcf5ef2aSThomas Huth         case 0x1:
431fcf5ef2aSThomas Huth         case 0x3:
432347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
433fcf5ef2aSThomas Huth             break;
434fcf5ef2aSThomas Huth 
435fcf5ef2aSThomas Huth         case 0x2:
436347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
437fcf5ef2aSThomas Huth             break;
438fcf5ef2aSThomas Huth         }
439fcf5ef2aSThomas Huth     }
440fcf5ef2aSThomas Huth 
441fcf5ef2aSThomas Huth     return prot;
442fcf5ef2aSThomas Huth }
443fcf5ef2aSThomas Huth 
444a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */
445a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key)
446a6152b52SSuraj Jitindar Singh {
447a6152b52SSuraj Jitindar Singh     CPUPPCState *env = &cpu->env;
448a6152b52SSuraj Jitindar Singh     int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3;
449a6152b52SSuraj Jitindar Singh 
450a6152b52SSuraj Jitindar Singh     /*
451a6152b52SSuraj Jitindar Singh      * An instruction fetch is permitted if the IAMR bit is 0.
452a6152b52SSuraj Jitindar Singh      * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
453a6152b52SSuraj Jitindar Singh      * can only take away EXEC permissions not READ or WRITE permissions.
454a6152b52SSuraj Jitindar Singh      * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
455a6152b52SSuraj Jitindar Singh      * EXEC permissions are allowed.
456a6152b52SSuraj Jitindar Singh      */
457a6152b52SSuraj Jitindar Singh     return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE :
458a6152b52SSuraj Jitindar Singh                                PAGE_READ | PAGE_WRITE | PAGE_EXEC;
459a6152b52SSuraj Jitindar Singh }
460a6152b52SSuraj Jitindar Singh 
461fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte)
462fcf5ef2aSThomas Huth {
463fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
464fcf5ef2aSThomas Huth     int key, amrbits;
465fcf5ef2aSThomas Huth     int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
466fcf5ef2aSThomas Huth 
467fcf5ef2aSThomas Huth     /* Only recent MMUs implement Virtual Page Class Key Protection */
46858969eeeSDavid Gibson     if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) {
469fcf5ef2aSThomas Huth         return prot;
470fcf5ef2aSThomas Huth     }
471fcf5ef2aSThomas Huth 
472fcf5ef2aSThomas Huth     key = HPTE64_R_KEY(pte.pte1);
473fcf5ef2aSThomas Huth     amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3;
474fcf5ef2aSThomas Huth 
475fcf5ef2aSThomas Huth     /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
476fcf5ef2aSThomas Huth     /*         env->spr[SPR_AMR]); */
477fcf5ef2aSThomas Huth 
478fcf5ef2aSThomas Huth     /*
479fcf5ef2aSThomas Huth      * A store is permitted if the AMR bit is 0. Remove write
480fcf5ef2aSThomas Huth      * protection if it is set.
481fcf5ef2aSThomas Huth      */
482fcf5ef2aSThomas Huth     if (amrbits & 0x2) {
483fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
484fcf5ef2aSThomas Huth     }
485fcf5ef2aSThomas Huth     /*
486fcf5ef2aSThomas Huth      * A load is permitted if the AMR bit is 0. Remove read
487fcf5ef2aSThomas Huth      * protection if it is set.
488fcf5ef2aSThomas Huth      */
489fcf5ef2aSThomas Huth     if (amrbits & 0x1) {
490fcf5ef2aSThomas Huth         prot &= ~PAGE_READ;
491fcf5ef2aSThomas Huth     }
492fcf5ef2aSThomas Huth 
493a6152b52SSuraj Jitindar Singh     switch (env->mmu_model) {
494a6152b52SSuraj Jitindar Singh     /*
495a6152b52SSuraj Jitindar Singh      * MMU version 2.07 and later support IAMR
496a6152b52SSuraj Jitindar Singh      * Check if the IAMR allows the instruction access - it will return
497a6152b52SSuraj Jitindar Singh      * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
498a6152b52SSuraj Jitindar Singh      * if it does (and prot will be unchanged indicating execution support).
499a6152b52SSuraj Jitindar Singh      */
500a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_2_07:
501a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_3_00:
502a6152b52SSuraj Jitindar Singh         prot &= ppc_hash64_iamr_prot(cpu, key);
503a6152b52SSuraj Jitindar Singh         break;
504a6152b52SSuraj Jitindar Singh     default:
505a6152b52SSuraj Jitindar Singh         break;
506a6152b52SSuraj Jitindar Singh     }
507a6152b52SSuraj Jitindar Singh 
508fcf5ef2aSThomas Huth     return prot;
509fcf5ef2aSThomas Huth }
510fcf5ef2aSThomas Huth 
5117222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu,
5127222b94aSDavid Gibson                                              hwaddr ptex, int n)
513fcf5ef2aSThomas Huth {
5147222b94aSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
5153367c62fSBenjamin Herrenschmidt     hwaddr base;
5167222b94aSDavid Gibson     hwaddr plen = n * HASH_PTE_SIZE_64;
517e57ca75cSDavid Gibson     const ppc_hash_pte64_t *hptes;
518e57ca75cSDavid Gibson 
519e57ca75cSDavid Gibson     if (cpu->vhyp) {
520e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
521e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
522e57ca75cSDavid Gibson         return vhc->map_hptes(cpu->vhyp, ptex, n);
523e57ca75cSDavid Gibson     }
5243367c62fSBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
525e57ca75cSDavid Gibson 
526e57ca75cSDavid Gibson     if (!base) {
527e57ca75cSDavid Gibson         return NULL;
528e57ca75cSDavid Gibson     }
529e57ca75cSDavid Gibson 
530f26404fbSPeter Maydell     hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false,
531f26404fbSPeter Maydell                               MEMTXATTRS_UNSPECIFIED);
5327222b94aSDavid Gibson     if (plen < (n * HASH_PTE_SIZE_64)) {
5337222b94aSDavid Gibson         hw_error("%s: Unable to map all requested HPTEs\n", __func__);
534fcf5ef2aSThomas Huth     }
5357222b94aSDavid Gibson     return hptes;
536fcf5ef2aSThomas Huth }
537fcf5ef2aSThomas Huth 
5387222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes,
5397222b94aSDavid Gibson                             hwaddr ptex, int n)
540fcf5ef2aSThomas Huth {
541e57ca75cSDavid Gibson     if (cpu->vhyp) {
542e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
543e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
544e57ca75cSDavid Gibson         vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n);
545e57ca75cSDavid Gibson         return;
546e57ca75cSDavid Gibson     }
547e57ca75cSDavid Gibson 
5487222b94aSDavid Gibson     address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64,
5497222b94aSDavid Gibson                         false, n * HASH_PTE_SIZE_64);
550fcf5ef2aSThomas Huth }
551fcf5ef2aSThomas Huth 
552b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps,
553fcf5ef2aSThomas Huth                                 uint64_t pte0, uint64_t pte1)
554fcf5ef2aSThomas Huth {
555fcf5ef2aSThomas Huth     int i;
556fcf5ef2aSThomas Huth 
557fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
558fcf5ef2aSThomas Huth         if (sps->page_shift != 12) {
559fcf5ef2aSThomas Huth             /* 4kiB page in a non 4kiB segment */
560fcf5ef2aSThomas Huth             return 0;
561fcf5ef2aSThomas Huth         }
562fcf5ef2aSThomas Huth         /* Normal 4kiB page */
563fcf5ef2aSThomas Huth         return 12;
564fcf5ef2aSThomas Huth     }
565fcf5ef2aSThomas Huth 
566fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
567b07c59f7SDavid Gibson         const PPCHash64PageSize *ps = &sps->enc[i];
568fcf5ef2aSThomas Huth         uint64_t mask;
569fcf5ef2aSThomas Huth 
570fcf5ef2aSThomas Huth         if (!ps->page_shift) {
571fcf5ef2aSThomas Huth             break;
572fcf5ef2aSThomas Huth         }
573fcf5ef2aSThomas Huth 
574fcf5ef2aSThomas Huth         if (ps->page_shift == 12) {
575fcf5ef2aSThomas Huth             /* L bit is set so this can't be a 4kiB page */
576fcf5ef2aSThomas Huth             continue;
577fcf5ef2aSThomas Huth         }
578fcf5ef2aSThomas Huth 
579fcf5ef2aSThomas Huth         mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
580fcf5ef2aSThomas Huth 
581fcf5ef2aSThomas Huth         if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
582fcf5ef2aSThomas Huth             return ps->page_shift;
583fcf5ef2aSThomas Huth         }
584fcf5ef2aSThomas Huth     }
585fcf5ef2aSThomas Huth 
586fcf5ef2aSThomas Huth     return 0; /* Bad page size encoding */
587fcf5ef2aSThomas Huth }
588fcf5ef2aSThomas Huth 
58934525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1)
59034525595SBenjamin Herrenschmidt {
59134525595SBenjamin Herrenschmidt     /* Insert B into pte0 */
59234525595SBenjamin Herrenschmidt     *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) |
59334525595SBenjamin Herrenschmidt             ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) <<
59434525595SBenjamin Herrenschmidt              (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT));
59534525595SBenjamin Herrenschmidt 
59634525595SBenjamin Herrenschmidt     /* Remove B from pte1 */
59734525595SBenjamin Herrenschmidt     *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK;
59834525595SBenjamin Herrenschmidt }
59934525595SBenjamin Herrenschmidt 
60034525595SBenjamin Herrenschmidt 
601fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
602b07c59f7SDavid Gibson                                      const PPCHash64SegmentPageSizes *sps,
603fcf5ef2aSThomas Huth                                      target_ulong ptem,
604fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
605fcf5ef2aSThomas Huth {
606fcf5ef2aSThomas Huth     int i;
6077222b94aSDavid Gibson     const ppc_hash_pte64_t *pteg;
608fcf5ef2aSThomas Huth     target_ulong pte0, pte1;
6097222b94aSDavid Gibson     target_ulong ptex;
610fcf5ef2aSThomas Huth 
61136778660SDavid Gibson     ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP;
6127222b94aSDavid Gibson     pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
6137222b94aSDavid Gibson     if (!pteg) {
614fcf5ef2aSThomas Huth         return -1;
615fcf5ef2aSThomas Huth     }
616fcf5ef2aSThomas Huth     for (i = 0; i < HPTES_PER_GROUP; i++) {
6177222b94aSDavid Gibson         pte0 = ppc_hash64_hpte0(cpu, pteg, i);
6183054b0caSBenjamin Herrenschmidt         /*
6193054b0caSBenjamin Herrenschmidt          * pte0 contains the valid bit and must be read before pte1,
6203054b0caSBenjamin Herrenschmidt          * otherwise we might see an old pte1 with a new valid bit and
6213054b0caSBenjamin Herrenschmidt          * thus an inconsistent hpte value
6223054b0caSBenjamin Herrenschmidt          */
6233054b0caSBenjamin Herrenschmidt         smp_rmb();
6247222b94aSDavid Gibson         pte1 = ppc_hash64_hpte1(cpu, pteg, i);
625fcf5ef2aSThomas Huth 
62634525595SBenjamin Herrenschmidt         /* Convert format if necessary */
62734525595SBenjamin Herrenschmidt         if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) {
62834525595SBenjamin Herrenschmidt             ppc64_v3_new_to_old_hpte(&pte0, &pte1);
62934525595SBenjamin Herrenschmidt         }
63034525595SBenjamin Herrenschmidt 
631fcf5ef2aSThomas Huth         /* This compares V, B, H (secondary) and the AVPN */
632fcf5ef2aSThomas Huth         if (HPTE64_V_COMPARE(pte0, ptem)) {
633fcf5ef2aSThomas Huth             *pshift = hpte_page_shift(sps, pte0, pte1);
634fcf5ef2aSThomas Huth             /*
635fcf5ef2aSThomas Huth              * If there is no match, ignore the PTE, it could simply
636fcf5ef2aSThomas Huth              * be for a different segment size encoding and the
637fcf5ef2aSThomas Huth              * architecture specifies we should not match. Linux will
638fcf5ef2aSThomas Huth              * potentially leave behind PTEs for the wrong base page
639fcf5ef2aSThomas Huth              * size when demoting segments.
640fcf5ef2aSThomas Huth              */
641fcf5ef2aSThomas Huth             if (*pshift == 0) {
642fcf5ef2aSThomas Huth                 continue;
643fcf5ef2aSThomas Huth             }
644d75cbae8SDavid Gibson             /*
645d75cbae8SDavid Gibson              * We don't do anything with pshift yet as qemu TLB only
646d75cbae8SDavid Gibson              * deals with 4K pages anyway
647fcf5ef2aSThomas Huth              */
648fcf5ef2aSThomas Huth             pte->pte0 = pte0;
649fcf5ef2aSThomas Huth             pte->pte1 = pte1;
6507222b94aSDavid Gibson             ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
6517222b94aSDavid Gibson             return ptex + i;
652fcf5ef2aSThomas Huth         }
653fcf5ef2aSThomas Huth     }
6547222b94aSDavid Gibson     ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
655fcf5ef2aSThomas Huth     /*
656fcf5ef2aSThomas Huth      * We didn't find a valid entry.
657fcf5ef2aSThomas Huth      */
658fcf5ef2aSThomas Huth     return -1;
659fcf5ef2aSThomas Huth }
660fcf5ef2aSThomas Huth 
661fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
662fcf5ef2aSThomas Huth                                      ppc_slb_t *slb, target_ulong eaddr,
663fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
664fcf5ef2aSThomas Huth {
665fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
6667222b94aSDavid Gibson     hwaddr hash, ptex;
667fcf5ef2aSThomas Huth     uint64_t vsid, epnmask, epn, ptem;
668b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = slb->sps;
669fcf5ef2aSThomas Huth 
670d75cbae8SDavid Gibson     /*
671d75cbae8SDavid Gibson      * The SLB store path should prevent any bad page size encodings
672d75cbae8SDavid Gibson      * getting in there, so:
673d75cbae8SDavid Gibson      */
674fcf5ef2aSThomas Huth     assert(sps);
675fcf5ef2aSThomas Huth 
676fcf5ef2aSThomas Huth     /* If ISL is set in LPCR we need to clamp the page size to 4K */
677fcf5ef2aSThomas Huth     if (env->spr[SPR_LPCR] & LPCR_ISL) {
678fcf5ef2aSThomas Huth         /* We assume that when using TCG, 4k is first entry of SPS */
679b07c59f7SDavid Gibson         sps = &cpu->hash64_opts->sps[0];
680fcf5ef2aSThomas Huth         assert(sps->page_shift == 12);
681fcf5ef2aSThomas Huth     }
682fcf5ef2aSThomas Huth 
683fcf5ef2aSThomas Huth     epnmask = ~((1ULL << sps->page_shift) - 1);
684fcf5ef2aSThomas Huth 
685fcf5ef2aSThomas Huth     if (slb->vsid & SLB_VSID_B) {
686fcf5ef2aSThomas Huth         /* 1TB segment */
687fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
688fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
689fcf5ef2aSThomas Huth         hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
690fcf5ef2aSThomas Huth     } else {
691fcf5ef2aSThomas Huth         /* 256M segment */
692fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
693fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
694fcf5ef2aSThomas Huth         hash = vsid ^ (epn >> sps->page_shift);
695fcf5ef2aSThomas Huth     }
696fcf5ef2aSThomas Huth     ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
697fcf5ef2aSThomas Huth     ptem |= HPTE64_V_VALID;
698fcf5ef2aSThomas Huth 
699fcf5ef2aSThomas Huth     /* Page address translation */
700fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
701883f2c59SPhilippe Mathieu-Daudé             "htab_base " HWADDR_FMT_plx " htab_mask " HWADDR_FMT_plx
702883f2c59SPhilippe Mathieu-Daudé             " hash " HWADDR_FMT_plx "\n",
70336778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash);
704fcf5ef2aSThomas Huth 
705fcf5ef2aSThomas Huth     /* Primary PTEG lookup */
706fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
707883f2c59SPhilippe Mathieu-Daudé             "0 htab=" HWADDR_FMT_plx "/" HWADDR_FMT_plx
708fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
709883f2c59SPhilippe Mathieu-Daudé             " hash=" HWADDR_FMT_plx "\n",
71036778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu),
71136778660SDavid Gibson             vsid, ptem,  hash);
7127222b94aSDavid Gibson     ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
713fcf5ef2aSThomas Huth 
7147222b94aSDavid Gibson     if (ptex == -1) {
715fcf5ef2aSThomas Huth         /* Secondary PTEG lookup */
716fcf5ef2aSThomas Huth         ptem |= HPTE64_V_SECONDARY;
717fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU,
718883f2c59SPhilippe Mathieu-Daudé                 "1 htab=" HWADDR_FMT_plx "/" HWADDR_FMT_plx
719fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
720883f2c59SPhilippe Mathieu-Daudé                 " hash=" HWADDR_FMT_plx "\n", ppc_hash64_hpt_base(cpu),
72136778660SDavid Gibson                 ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash);
722fcf5ef2aSThomas Huth 
7237222b94aSDavid Gibson         ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
724fcf5ef2aSThomas Huth     }
725fcf5ef2aSThomas Huth 
7267222b94aSDavid Gibson     return ptex;
727fcf5ef2aSThomas Huth }
728fcf5ef2aSThomas Huth 
729fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
730fcf5ef2aSThomas Huth                                           uint64_t pte0, uint64_t pte1)
731fcf5ef2aSThomas Huth {
732fcf5ef2aSThomas Huth     int i;
733fcf5ef2aSThomas Huth 
734fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
735fcf5ef2aSThomas Huth         return 12;
736fcf5ef2aSThomas Huth     }
737fcf5ef2aSThomas Huth 
738fcf5ef2aSThomas Huth     /*
739fcf5ef2aSThomas Huth      * The encodings in env->sps need to be carefully chosen so that
740fcf5ef2aSThomas Huth      * this gives an unambiguous result.
741fcf5ef2aSThomas Huth      */
742fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
743b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
744fcf5ef2aSThomas Huth         unsigned shift;
745fcf5ef2aSThomas Huth 
746fcf5ef2aSThomas Huth         if (!sps->page_shift) {
747fcf5ef2aSThomas Huth             break;
748fcf5ef2aSThomas Huth         }
749fcf5ef2aSThomas Huth 
750fcf5ef2aSThomas Huth         shift = hpte_page_shift(sps, pte0, pte1);
751fcf5ef2aSThomas Huth         if (shift) {
752fcf5ef2aSThomas Huth             return shift;
753fcf5ef2aSThomas Huth         }
754fcf5ef2aSThomas Huth     }
755fcf5ef2aSThomas Huth 
756fcf5ef2aSThomas Huth     return 0;
757fcf5ef2aSThomas Huth }
758fcf5ef2aSThomas Huth 
7591b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env)
7601b99e029SDavid Gibson {
7611b99e029SDavid Gibson     switch (env->mmu_model) {
7621b99e029SDavid Gibson     case POWERPC_MMU_3_00:
7631b99e029SDavid Gibson         /*
7641b99e029SDavid Gibson          * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR
7651b99e029SDavid Gibson          * register no longer exist
7661b99e029SDavid Gibson          */
7671b99e029SDavid Gibson         return true;
7681b99e029SDavid Gibson 
7691b99e029SDavid Gibson     default:
7701b99e029SDavid Gibson         return !!(env->spr[SPR_LPCR] & LPCR_VPM0);
7711b99e029SDavid Gibson     }
7721b99e029SDavid Gibson }
7731b99e029SDavid Gibson 
7749201af09SNicholas Piggin static void ppc_hash64_set_isi(CPUState *cs, int mmu_idx, uint64_t slb_vsid,
7759201af09SNicholas Piggin                                uint64_t error_code)
776fcf5ef2aSThomas Huth {
7778fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
778fcf5ef2aSThomas Huth     bool vpm;
779fcf5ef2aSThomas Huth 
78003695a98SBruno Larsen (billionai)     if (!mmuidx_real(mmu_idx)) {
781fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
782fcf5ef2aSThomas Huth     } else {
7831b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
784fcf5ef2aSThomas Huth     }
78503695a98SBruno Larsen (billionai)     if (vpm && !mmuidx_hv(mmu_idx)) {
786fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HISI;
7879201af09SNicholas Piggin         env->spr[SPR_ASDR] = slb_vsid;
788fcf5ef2aSThomas Huth     } else {
789fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
790fcf5ef2aSThomas Huth     }
791fcf5ef2aSThomas Huth     env->error_code = error_code;
792fcf5ef2aSThomas Huth }
793fcf5ef2aSThomas Huth 
7949201af09SNicholas Piggin static void ppc_hash64_set_dsi(CPUState *cs, int mmu_idx, uint64_t slb_vsid,
7959201af09SNicholas Piggin                                uint64_t dar, uint64_t dsisr)
796fcf5ef2aSThomas Huth {
7978fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
798fcf5ef2aSThomas Huth     bool vpm;
799fcf5ef2aSThomas Huth 
80003695a98SBruno Larsen (billionai)     if (!mmuidx_real(mmu_idx)) {
801fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
802fcf5ef2aSThomas Huth     } else {
8031b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
804fcf5ef2aSThomas Huth     }
80503695a98SBruno Larsen (billionai)     if (vpm && !mmuidx_hv(mmu_idx)) {
806fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
807fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
808fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
8099201af09SNicholas Piggin         env->spr[SPR_ASDR] = slb_vsid;
810fcf5ef2aSThomas Huth     } else {
811fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
812fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
813fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
814fcf5ef2aSThomas Huth    }
815fcf5ef2aSThomas Huth     env->error_code = 0;
816fcf5ef2aSThomas Huth }
817fcf5ef2aSThomas Huth 
818fcf5ef2aSThomas Huth 
819a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
820a2dd4e83SBenjamin Herrenschmidt {
8217bf00dfbSLeandro Lupori     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_R;
822a2dd4e83SBenjamin Herrenschmidt 
823a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
824a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
825a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
826a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_r(cpu->vhyp, ptex, pte1);
827a2dd4e83SBenjamin Herrenschmidt         return;
828a2dd4e83SBenjamin Herrenschmidt     }
829a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
830a2dd4e83SBenjamin Herrenschmidt 
831a2dd4e83SBenjamin Herrenschmidt 
832a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
833a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01);
834a2dd4e83SBenjamin Herrenschmidt }
835a2dd4e83SBenjamin Herrenschmidt 
836a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
837a2dd4e83SBenjamin Herrenschmidt {
8387bf00dfbSLeandro Lupori     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_C;
839a2dd4e83SBenjamin Herrenschmidt 
840a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
841a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
842a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
843a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_c(cpu->vhyp, ptex, pte1);
844a2dd4e83SBenjamin Herrenschmidt         return;
845a2dd4e83SBenjamin Herrenschmidt     }
846a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
847a2dd4e83SBenjamin Herrenschmidt 
848a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
849a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80);
850a2dd4e83SBenjamin Herrenschmidt }
851a2dd4e83SBenjamin Herrenschmidt 
852a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu)
853a864a6b3SDavid Gibson {
854a864a6b3SDavid Gibson     CPUPPCState *env = &cpu->env;
855a864a6b3SDavid Gibson     /*
856d37b40daSDavid Gibson      * In theory the meanings of RMLS values are implementation
857d37b40daSDavid Gibson      * dependent.  In practice, this seems to have been the set from
858d37b40daSDavid Gibson      * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
859a864a6b3SDavid Gibson      *
860a864a6b3SDavid Gibson      * Unsupported values mean the OS has shot itself in the
861a864a6b3SDavid Gibson      * foot. Return a 0-sized RMA in this case, which we expect
862a864a6b3SDavid Gibson      * to trigger an immediate DSI or ISI
863a864a6b3SDavid Gibson      */
864a864a6b3SDavid Gibson     static const target_ulong rma_sizes[16] = {
865d37b40daSDavid Gibson         [0] = 256 * GiB,
866a864a6b3SDavid Gibson         [1] = 16 * GiB,
867a864a6b3SDavid Gibson         [2] = 1 * GiB,
868a864a6b3SDavid Gibson         [3] = 64 * MiB,
869a864a6b3SDavid Gibson         [4] = 256 * MiB,
870a864a6b3SDavid Gibson         [7] = 128 * MiB,
871a864a6b3SDavid Gibson         [8] = 32 * MiB,
872a864a6b3SDavid Gibson     };
873a864a6b3SDavid Gibson     target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT;
874a864a6b3SDavid Gibson 
875a864a6b3SDavid Gibson     return rma_sizes[rmls];
876a864a6b3SDavid Gibson }
877a864a6b3SDavid Gibson 
8780e2a3ec3SNicholas Piggin /* Return the LLP in SLB_VSID format */
8790e2a3ec3SNicholas Piggin static uint64_t get_vrma_llp(PowerPCCPU *cpu)
8804c24a87fSDavid Gibson {
8814c24a87fSDavid Gibson     CPUPPCState *env = &cpu->env;
8820e2a3ec3SNicholas Piggin     uint64_t llp;
8830e2a3ec3SNicholas Piggin 
8840e2a3ec3SNicholas Piggin     if (env->mmu_model == POWERPC_MMU_3_00) {
8850e2a3ec3SNicholas Piggin         ppc_v3_pate_t pate;
8860e2a3ec3SNicholas Piggin         uint64_t ps, l, lp;
8870e2a3ec3SNicholas Piggin 
8880e2a3ec3SNicholas Piggin         /*
8890e2a3ec3SNicholas Piggin          * ISA v3.0 removes the LPCR[VRMASD] field and puts the VRMA base
8900e2a3ec3SNicholas Piggin          * page size (L||LP equivalent) in the PS field in the HPT partition
8910e2a3ec3SNicholas Piggin          * table entry.
8920e2a3ec3SNicholas Piggin          */
8930e2a3ec3SNicholas Piggin         if (!ppc64_v3_get_pate(cpu, cpu->env.spr[SPR_LPIDR], &pate)) {
8940e2a3ec3SNicholas Piggin             error_report("Bad VRMA with no partition table entry");
8950e2a3ec3SNicholas Piggin             return 0;
8960e2a3ec3SNicholas Piggin         }
8970e2a3ec3SNicholas Piggin         ps = PATE0_GET_PS(pate.dw0);
8980e2a3ec3SNicholas Piggin         /* PS has L||LP in 3 consecutive bits, put them into SLB LLP format */
8990e2a3ec3SNicholas Piggin         l = (ps >> 2) & 0x1;
9000e2a3ec3SNicholas Piggin         lp = ps & 0x3;
9010e2a3ec3SNicholas Piggin         llp = (l << SLB_VSID_L_SHIFT) | (lp << SLB_VSID_LP_SHIFT);
9020e2a3ec3SNicholas Piggin 
9030e2a3ec3SNicholas Piggin     } else {
9040e2a3ec3SNicholas Piggin         uint64_t lpcr = env->spr[SPR_LPCR];
9050e2a3ec3SNicholas Piggin         target_ulong vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
9060e2a3ec3SNicholas Piggin 
9070e2a3ec3SNicholas Piggin         /* VRMASD LLP matches SLB format, just shift and mask it */
9080e2a3ec3SNicholas Piggin         llp = (vrmasd << SLB_VSID_LP_SHIFT) & SLB_VSID_LLP_MASK;
9090e2a3ec3SNicholas Piggin     }
9100e2a3ec3SNicholas Piggin 
9110e2a3ec3SNicholas Piggin     return llp;
9120e2a3ec3SNicholas Piggin }
9130e2a3ec3SNicholas Piggin 
9140e2a3ec3SNicholas Piggin static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb)
9150e2a3ec3SNicholas Piggin {
9160e2a3ec3SNicholas Piggin     uint64_t llp = get_vrma_llp(cpu);
9170e2a3ec3SNicholas Piggin     target_ulong vsid = SLB_VSID_VRMA | llp;
9184c24a87fSDavid Gibson     int i;
9194c24a87fSDavid Gibson 
9204c24a87fSDavid Gibson     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
9214c24a87fSDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
9224c24a87fSDavid Gibson 
9234c24a87fSDavid Gibson         if (!sps->page_shift) {
9244c24a87fSDavid Gibson             break;
9254c24a87fSDavid Gibson         }
9264c24a87fSDavid Gibson 
9274c24a87fSDavid Gibson         if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) {
9284c24a87fSDavid Gibson             slb->esid = SLB_ESID_V;
9294c24a87fSDavid Gibson             slb->vsid = vsid;
9304c24a87fSDavid Gibson             slb->sps = sps;
9314c24a87fSDavid Gibson             return 0;
9324c24a87fSDavid Gibson         }
9334c24a87fSDavid Gibson     }
9344c24a87fSDavid Gibson 
9350e2a3ec3SNicholas Piggin     error_report("Bad VRMA page size encoding 0x" TARGET_FMT_lx, llp);
9364c24a87fSDavid Gibson 
9374c24a87fSDavid Gibson     return -1;
9384c24a87fSDavid Gibson }
9394c24a87fSDavid Gibson 
94051806b54SRichard Henderson bool ppc_hash64_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type,
94103695a98SBruno Larsen (billionai)                       hwaddr *raddrp, int *psizep, int *protp, int mmu_idx,
9421a8c647bSRichard Henderson                       bool guest_visible)
943fcf5ef2aSThomas Huth {
944fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
945fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
9464c24a87fSDavid Gibson     ppc_slb_t vrma_slbe;
947fcf5ef2aSThomas Huth     ppc_slb_t *slb;
948fcf5ef2aSThomas Huth     unsigned apshift;
9497222b94aSDavid Gibson     hwaddr ptex;
950fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
95107a68f99SSuraj Jitindar Singh     int exec_prot, pp_prot, amr_prot, prot;
952182357dbSRichard Henderson     int need_prot;
953fcf5ef2aSThomas Huth     hwaddr raddr;
954fcf5ef2aSThomas Huth 
955d75cbae8SDavid Gibson     /*
956d75cbae8SDavid Gibson      * Note on LPCR usage: 970 uses HID4, but our special variant of
957d75cbae8SDavid Gibson      * store_spr copies relevant fields into env->spr[SPR_LPCR].
958136fbf65Szhaolichang      * Similarly we filter unimplemented bits when storing into LPCR
959d75cbae8SDavid Gibson      * depending on the MMU version. This code can thus just use the
960d75cbae8SDavid Gibson      * LPCR "as-is".
961fcf5ef2aSThomas Huth      */
962fcf5ef2aSThomas Huth 
963fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
96403695a98SBruno Larsen (billionai)     if (mmuidx_real(mmu_idx)) {
965d75cbae8SDavid Gibson         /*
966d75cbae8SDavid Gibson          * Translation is supposedly "off", but in real mode the top 4
967d75cbae8SDavid Gibson          * effective address bits are (mostly) ignored
968d75cbae8SDavid Gibson          */
969fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
970fcf5ef2aSThomas Huth 
971682c1dfbSDavid Gibson         if (cpu->vhyp) {
972682c1dfbSDavid Gibson             /*
973682c1dfbSDavid Gibson              * In virtual hypervisor mode, there's nothing to do:
974682c1dfbSDavid Gibson              *   EA == GPA == qemu guest address
975682c1dfbSDavid Gibson              */
97603695a98SBruno Larsen (billionai)         } else if (mmuidx_hv(mmu_idx) || !env->has_hv_mode) {
977fcf5ef2aSThomas Huth             /* In HV mode, add HRMOR if top EA bit is clear */
978fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
979fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
980fcf5ef2aSThomas Huth             }
9811b99e029SDavid Gibson         } else if (ppc_hash64_use_vrma(env)) {
982682c1dfbSDavid Gibson             /* Emulated VRMA mode */
9834c24a87fSDavid Gibson             slb = &vrma_slbe;
9844c24a87fSDavid Gibson             if (build_vrma_slbe(cpu, slb) != 0) {
985682c1dfbSDavid Gibson                 /* Invalid VRMA setup, machine check */
9861a8c647bSRichard Henderson                 if (guest_visible) {
987fcf5ef2aSThomas Huth                     cs->exception_index = POWERPC_EXCP_MCHECK;
988fcf5ef2aSThomas Huth                     env->error_code = 0;
9891a8c647bSRichard Henderson                 }
9901a8c647bSRichard Henderson                 return false;
991682c1dfbSDavid Gibson             }
992682c1dfbSDavid Gibson 
993682c1dfbSDavid Gibson             goto skip_slb_search;
994fcf5ef2aSThomas Huth         } else {
9953a56a55cSDavid Gibson             target_ulong limit = rmls_limit(cpu);
9963a56a55cSDavid Gibson 
997682c1dfbSDavid Gibson             /* Emulated old-style RMO mode, bounds check against RMLS */
9983a56a55cSDavid Gibson             if (raddr >= limit) {
9991a8c647bSRichard Henderson                 if (!guest_visible) {
10001a8c647bSRichard Henderson                     return false;
10011a8c647bSRichard Henderson                 }
100259dec5bfSRichard Henderson                 switch (access_type) {
100359dec5bfSRichard Henderson                 case MMU_INST_FETCH:
10049201af09SNicholas Piggin                     ppc_hash64_set_isi(cs, mmu_idx, 0, SRR1_PROTFAULT);
100559dec5bfSRichard Henderson                     break;
100659dec5bfSRichard Henderson                 case MMU_DATA_LOAD:
10079201af09SNicholas Piggin                     ppc_hash64_set_dsi(cs, mmu_idx, 0, eaddr, DSISR_PROTFAULT);
100859dec5bfSRichard Henderson                     break;
100959dec5bfSRichard Henderson                 case MMU_DATA_STORE:
10109201af09SNicholas Piggin                     ppc_hash64_set_dsi(cs, mmu_idx, 0, eaddr,
101159dec5bfSRichard Henderson                                        DSISR_PROTFAULT | DSISR_ISSTORE);
101259dec5bfSRichard Henderson                     break;
101359dec5bfSRichard Henderson                 default:
101459dec5bfSRichard Henderson                     g_assert_not_reached();
1015fcf5ef2aSThomas Huth                 }
10161a8c647bSRichard Henderson                 return false;
1017fcf5ef2aSThomas Huth             }
1018682c1dfbSDavid Gibson 
1019682c1dfbSDavid Gibson             raddr |= env->spr[SPR_RMOR];
1020fcf5ef2aSThomas Huth         }
10211a8c647bSRichard Henderson 
10221a8c647bSRichard Henderson         *raddrp = raddr;
10231a8c647bSRichard Henderson         *protp = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10241a8c647bSRichard Henderson         *psizep = TARGET_PAGE_BITS;
10251a8c647bSRichard Henderson         return true;
1026fcf5ef2aSThomas Huth     }
1027fcf5ef2aSThomas Huth 
1028fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
1029fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
1030fcf5ef2aSThomas Huth     if (!slb) {
1031b2899495SSuraj Jitindar Singh         /* No entry found, check if in-memory segment tables are in use */
1032ca79b3b7SDavid Gibson         if (ppc64_use_proc_tbl(cpu)) {
1033b2899495SSuraj Jitindar Singh             /* TODO - Unsupported */
1034b2899495SSuraj Jitindar Singh             error_report("Segment Table Support Unimplemented");
1035b2899495SSuraj Jitindar Singh             exit(1);
1036b2899495SSuraj Jitindar Singh         }
1037b2899495SSuraj Jitindar Singh         /* Segment still not found, generate the appropriate interrupt */
10381a8c647bSRichard Henderson         if (!guest_visible) {
10391a8c647bSRichard Henderson             return false;
10401a8c647bSRichard Henderson         }
104159dec5bfSRichard Henderson         switch (access_type) {
104259dec5bfSRichard Henderson         case MMU_INST_FETCH:
1043fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
1044fcf5ef2aSThomas Huth             env->error_code = 0;
104559dec5bfSRichard Henderson             break;
104659dec5bfSRichard Henderson         case MMU_DATA_LOAD:
104759dec5bfSRichard Henderson         case MMU_DATA_STORE:
1048fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
1049fcf5ef2aSThomas Huth             env->error_code = 0;
1050fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
105159dec5bfSRichard Henderson             break;
105259dec5bfSRichard Henderson         default:
105359dec5bfSRichard Henderson             g_assert_not_reached();
1054fcf5ef2aSThomas Huth         }
10551a8c647bSRichard Henderson         return false;
1056fcf5ef2aSThomas Huth     }
1057fcf5ef2aSThomas Huth 
1058fcf5ef2aSThomas Huth  skip_slb_search:
1059fcf5ef2aSThomas Huth 
1060fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
106159dec5bfSRichard Henderson     if (access_type == MMU_INST_FETCH && (slb->vsid & SLB_VSID_N)) {
10621a8c647bSRichard Henderson         if (guest_visible) {
10639201af09SNicholas Piggin             ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, SRR1_NOEXEC_GUARD);
10641a8c647bSRichard Henderson         }
10651a8c647bSRichard Henderson         return false;
1066fcf5ef2aSThomas Huth     }
1067fcf5ef2aSThomas Huth 
1068fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
10697222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
10707222b94aSDavid Gibson     if (ptex == -1) {
10711a8c647bSRichard Henderson         if (!guest_visible) {
10721a8c647bSRichard Henderson             return false;
10731a8c647bSRichard Henderson         }
107459dec5bfSRichard Henderson         switch (access_type) {
107559dec5bfSRichard Henderson         case MMU_INST_FETCH:
10769201af09SNicholas Piggin             ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, SRR1_NOPTE);
107759dec5bfSRichard Henderson             break;
107859dec5bfSRichard Henderson         case MMU_DATA_LOAD:
10799201af09SNicholas Piggin             ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, DSISR_NOPTE);
108059dec5bfSRichard Henderson             break;
108159dec5bfSRichard Henderson         case MMU_DATA_STORE:
10829201af09SNicholas Piggin             ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr,
10839201af09SNicholas Piggin                                DSISR_NOPTE | DSISR_ISSTORE);
108459dec5bfSRichard Henderson             break;
108559dec5bfSRichard Henderson         default:
108659dec5bfSRichard Henderson             g_assert_not_reached();
1087fcf5ef2aSThomas Huth         }
10881a8c647bSRichard Henderson         return false;
1089fcf5ef2aSThomas Huth     }
1090fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
10917222b94aSDavid Gibson                   "found PTE at index %08" HWADDR_PRIx "\n", ptex);
1092fcf5ef2aSThomas Huth 
1093fcf5ef2aSThomas Huth     /* 5. Check access permissions */
1094fcf5ef2aSThomas Huth 
109507a68f99SSuraj Jitindar Singh     exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
109603695a98SBruno Larsen (billionai)     pp_prot = ppc_hash64_pte_prot(mmu_idx, slb, pte);
1097fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
109807a68f99SSuraj Jitindar Singh     prot = exec_prot & pp_prot & amr_prot;
1099fcf5ef2aSThomas Huth 
110059dec5bfSRichard Henderson     need_prot = prot_for_access_type(access_type);
1101182357dbSRichard Henderson     if (need_prot & ~prot) {
1102fcf5ef2aSThomas Huth         /* Access right violation */
1103fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
11041a8c647bSRichard Henderson         if (!guest_visible) {
11051a8c647bSRichard Henderson             return false;
11061a8c647bSRichard Henderson         }
110759dec5bfSRichard Henderson         if (access_type == MMU_INST_FETCH) {
1108a6152b52SSuraj Jitindar Singh             int srr1 = 0;
110907a68f99SSuraj Jitindar Singh             if (PAGE_EXEC & ~exec_prot) {
111007a68f99SSuraj Jitindar Singh                 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
111107a68f99SSuraj Jitindar Singh             } else if (PAGE_EXEC & ~pp_prot) {
1112a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
1113a6152b52SSuraj Jitindar Singh             }
1114a6152b52SSuraj Jitindar Singh             if (PAGE_EXEC & ~amr_prot) {
1115a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
1116a6152b52SSuraj Jitindar Singh             }
11179201af09SNicholas Piggin             ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, srr1);
1118fcf5ef2aSThomas Huth         } else {
1119da82c73aSSuraj Jitindar Singh             int dsisr = 0;
1120182357dbSRichard Henderson             if (need_prot & ~pp_prot) {
1121da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_PROTFAULT;
1122fcf5ef2aSThomas Huth             }
112359dec5bfSRichard Henderson             if (access_type == MMU_DATA_STORE) {
1124da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
1125fcf5ef2aSThomas Huth             }
1126182357dbSRichard Henderson             if (need_prot & ~amr_prot) {
1127da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_AMR;
1128fcf5ef2aSThomas Huth             }
11299201af09SNicholas Piggin             ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, dsisr);
1130fcf5ef2aSThomas Huth         }
11311a8c647bSRichard Henderson         return false;
1132fcf5ef2aSThomas Huth     }
1133fcf5ef2aSThomas Huth 
1134fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
1135fcf5ef2aSThomas Huth 
1136fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
1137fcf5ef2aSThomas Huth 
1138a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_R)) {
1139a2dd4e83SBenjamin Herrenschmidt         ppc_hash64_set_r(cpu, ptex, pte.pte1);
1140a2dd4e83SBenjamin Herrenschmidt     }
1141a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_C)) {
114259dec5bfSRichard Henderson         if (access_type == MMU_DATA_STORE) {
1143a2dd4e83SBenjamin Herrenschmidt             ppc_hash64_set_c(cpu, ptex, pte.pte1);
1144fcf5ef2aSThomas Huth         } else {
1145d75cbae8SDavid Gibson             /*
1146d75cbae8SDavid Gibson              * Treat the page as read-only for now, so that a later write
1147d75cbae8SDavid Gibson              * will pass through this function again to set the C bit
1148d75cbae8SDavid Gibson              */
1149fcf5ef2aSThomas Huth             prot &= ~PAGE_WRITE;
1150fcf5ef2aSThomas Huth         }
1151fcf5ef2aSThomas Huth     }
1152fcf5ef2aSThomas Huth 
1153fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
1154fcf5ef2aSThomas Huth 
11551a8c647bSRichard Henderson     *raddrp = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
11561a8c647bSRichard Henderson     *protp = prot;
11571a8c647bSRichard Henderson     *psizep = apshift;
11581a8c647bSRichard Henderson     return true;
11591a8c647bSRichard Henderson }
11601a8c647bSRichard Henderson 
11617222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
1162fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
1163fcf5ef2aSThomas Huth {
1164fcf5ef2aSThomas Huth     /*
1165fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
1166fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
1167fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
1168fcf5ef2aSThomas Huth      */
1169fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
1170fcf5ef2aSThomas Huth }
1171fcf5ef2aSThomas Huth 
11722b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
11735ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val)
11745ad55315SDavid Gibson {
1175db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
11765ad55315SDavid Gibson 
11775ad55315SDavid Gibson     ppc_store_lpcr(cpu, val);
11785ad55315SDavid Gibson }
11792b44e219SBruno Larsen (billionai) #endif
11805ad55315SDavid Gibson 
1181a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu)
1182a059471dSDavid Gibson {
1183a059471dSDavid Gibson     CPUPPCState *env = &cpu->env;
1184a059471dSDavid Gibson     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1185a059471dSDavid Gibson 
118621e405f1SDavid Gibson     if (!pcc->hash64_opts) {
1187d57d72a8SGreg Kurz         assert(!mmu_is_64bit(env->mmu_model));
118821e405f1SDavid Gibson         return;
118921e405f1SDavid Gibson     }
119021e405f1SDavid Gibson 
119121e405f1SDavid Gibson     cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
119221e405f1SDavid Gibson }
119321e405f1SDavid Gibson 
119421e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu)
119521e405f1SDavid Gibson {
119621e405f1SDavid Gibson     g_free(cpu->hash64_opts);
119721e405f1SDavid Gibson }
119821e405f1SDavid Gibson 
119921e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = {
120058969eeeSDavid Gibson     .flags = 0,
120167d7d66fSDavid Gibson     .slb_size = 64,
1202a059471dSDavid Gibson     .sps = {
1203a059471dSDavid Gibson         { .page_shift = 12, /* 4K */
1204a059471dSDavid Gibson           .slb_enc = 0,
1205a059471dSDavid Gibson           .enc = { { .page_shift = 12, .pte_enc = 0 } }
1206a059471dSDavid Gibson         },
1207a059471dSDavid Gibson         { .page_shift = 24, /* 16M */
1208a059471dSDavid Gibson           .slb_enc = 0x100,
1209a059471dSDavid Gibson           .enc = { { .page_shift = 24, .pte_enc = 0 } }
1210a059471dSDavid Gibson         },
1211a059471dSDavid Gibson     },
1212a059471dSDavid Gibson };
1213b07c59f7SDavid Gibson 
1214b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = {
121526cd35b8SDavid Gibson     .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
121667d7d66fSDavid Gibson     .slb_size = 32,
1217b07c59f7SDavid Gibson     .sps = {
1218b07c59f7SDavid Gibson         {
1219b07c59f7SDavid Gibson             .page_shift = 12, /* 4K */
1220b07c59f7SDavid Gibson             .slb_enc = 0,
1221b07c59f7SDavid Gibson             .enc = { { .page_shift = 12, .pte_enc = 0 },
1222b07c59f7SDavid Gibson                      { .page_shift = 16, .pte_enc = 0x7 },
1223b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x38 }, },
1224b07c59f7SDavid Gibson         },
1225b07c59f7SDavid Gibson         {
1226b07c59f7SDavid Gibson             .page_shift = 16, /* 64K */
1227b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_64K,
1228b07c59f7SDavid Gibson             .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1229b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x8 }, },
1230b07c59f7SDavid Gibson         },
1231b07c59f7SDavid Gibson         {
1232b07c59f7SDavid Gibson             .page_shift = 24, /* 16M */
1233b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16M,
1234b07c59f7SDavid Gibson             .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1235b07c59f7SDavid Gibson         },
1236b07c59f7SDavid Gibson         {
1237b07c59f7SDavid Gibson             .page_shift = 34, /* 16G */
1238b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16G,
1239b07c59f7SDavid Gibson             .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },
1240b07c59f7SDavid Gibson         },
1241b07c59f7SDavid Gibson     }
1242b07c59f7SDavid Gibson };
124327f00f0aSDavid Gibson 
124427f00f0aSDavid Gibson 
1245