xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision 182357dbb6989a175d2a653c9edcd5422c651922)
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 "exec/helper-proto.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"
32*182357dbSRichard Henderson #include "internal.h"
33b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
34f03de3b4SRichard Henderson #include "helper_regs.h"
35fcf5ef2aSThomas Huth 
36d75cbae8SDavid Gibson /* #define DEBUG_SLB */
37fcf5ef2aSThomas Huth 
38fcf5ef2aSThomas Huth #ifdef DEBUG_SLB
39fcf5ef2aSThomas Huth #  define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
40fcf5ef2aSThomas Huth #else
41fcf5ef2aSThomas Huth #  define LOG_SLB(...) do { } while (0)
42fcf5ef2aSThomas Huth #endif
43fcf5ef2aSThomas Huth 
44fcf5ef2aSThomas Huth /*
45fcf5ef2aSThomas Huth  * SLB handling
46fcf5ef2aSThomas Huth  */
47fcf5ef2aSThomas Huth 
48fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr)
49fcf5ef2aSThomas Huth {
50fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
51fcf5ef2aSThomas Huth     uint64_t esid_256M, esid_1T;
52fcf5ef2aSThomas Huth     int n;
53fcf5ef2aSThomas Huth 
54fcf5ef2aSThomas Huth     LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr);
55fcf5ef2aSThomas Huth 
56fcf5ef2aSThomas Huth     esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V;
57fcf5ef2aSThomas Huth     esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V;
58fcf5ef2aSThomas Huth 
5967d7d66fSDavid Gibson     for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
60fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
61fcf5ef2aSThomas Huth 
62fcf5ef2aSThomas Huth         LOG_SLB("%s: slot %d %016" PRIx64 " %016"
63fcf5ef2aSThomas Huth                     PRIx64 "\n", __func__, n, slb->esid, slb->vsid);
64d75cbae8SDavid Gibson         /*
65d75cbae8SDavid Gibson          * We check for 1T matches on all MMUs here - if the MMU
66fcf5ef2aSThomas Huth          * doesn't have 1T segment support, we will have prevented 1T
67d75cbae8SDavid Gibson          * entries from being inserted in the slbmte code.
68d75cbae8SDavid Gibson          */
69fcf5ef2aSThomas Huth         if (((slb->esid == esid_256M) &&
70fcf5ef2aSThomas Huth              ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M))
71fcf5ef2aSThomas Huth             || ((slb->esid == esid_1T) &&
72fcf5ef2aSThomas Huth                 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) {
73fcf5ef2aSThomas Huth             return slb;
74fcf5ef2aSThomas Huth         }
75fcf5ef2aSThomas Huth     }
76fcf5ef2aSThomas Huth 
77fcf5ef2aSThomas Huth     return NULL;
78fcf5ef2aSThomas Huth }
79fcf5ef2aSThomas Huth 
80fad866daSMarkus Armbruster void dump_slb(PowerPCCPU *cpu)
81fcf5ef2aSThomas Huth {
82fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
83fcf5ef2aSThomas Huth     int i;
84fcf5ef2aSThomas Huth     uint64_t slbe, slbv;
85fcf5ef2aSThomas Huth 
86fcf5ef2aSThomas Huth     cpu_synchronize_state(CPU(cpu));
87fcf5ef2aSThomas Huth 
88fad866daSMarkus Armbruster     qemu_printf("SLB\tESID\t\t\tVSID\n");
8967d7d66fSDavid Gibson     for (i = 0; i < cpu->hash64_opts->slb_size; i++) {
90fcf5ef2aSThomas Huth         slbe = env->slb[i].esid;
91fcf5ef2aSThomas Huth         slbv = env->slb[i].vsid;
92fcf5ef2aSThomas Huth         if (slbe == 0 && slbv == 0) {
93fcf5ef2aSThomas Huth             continue;
94fcf5ef2aSThomas Huth         }
95fad866daSMarkus Armbruster         qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n",
96fcf5ef2aSThomas Huth                     i, slbe, slbv);
97fcf5ef2aSThomas Huth     }
98fcf5ef2aSThomas Huth }
99fcf5ef2aSThomas Huth 
1000418bf78SNicholas Piggin void helper_slbia(CPUPPCState *env, uint32_t ih)
101fcf5ef2aSThomas Huth {
102db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
1030418bf78SNicholas Piggin     int starting_entry;
104fcf5ef2aSThomas Huth     int n;
105fcf5ef2aSThomas Huth 
106f9e3e1a3SNicholas Piggin     /*
107f9e3e1a3SNicholas Piggin      * slbia must always flush all TLB (which is equivalent to ERAT in ppc
108f9e3e1a3SNicholas Piggin      * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
109f9e3e1a3SNicholas Piggin      * can overwrite a valid SLB without flushing its lookaside information.
110f9e3e1a3SNicholas Piggin      *
111f9e3e1a3SNicholas Piggin      * It would be possible to keep the TLB in synch with the SLB by flushing
112f9e3e1a3SNicholas Piggin      * when a valid entry is overwritten by slbmte, and therefore slbia would
113f9e3e1a3SNicholas Piggin      * not have to flush unless it evicts a valid SLB entry. However it is
114f9e3e1a3SNicholas Piggin      * expected that slbmte is more common than slbia, and slbia is usually
115f9e3e1a3SNicholas Piggin      * going to evict valid SLB entries, so that tradeoff is unlikely to be a
116f9e3e1a3SNicholas Piggin      * good one.
1170418bf78SNicholas Piggin      *
1180418bf78SNicholas Piggin      * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate
1190418bf78SNicholas Piggin      * the same SLB entries (everything but entry 0), but differ in what
1200418bf78SNicholas Piggin      * "lookaside information" is invalidated. TCG can ignore this and flush
1210418bf78SNicholas Piggin      * everything.
1220418bf78SNicholas Piggin      *
1230418bf78SNicholas Piggin      * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are
1240418bf78SNicholas Piggin      * invalidated.
125f9e3e1a3SNicholas Piggin      */
126f9e3e1a3SNicholas Piggin 
1270418bf78SNicholas Piggin     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
1280418bf78SNicholas Piggin 
1290418bf78SNicholas Piggin     starting_entry = 1; /* default for IH=0,1,2,6 */
1300418bf78SNicholas Piggin 
1310418bf78SNicholas Piggin     if (env->mmu_model == POWERPC_MMU_3_00) {
1320418bf78SNicholas Piggin         switch (ih) {
1330418bf78SNicholas Piggin         case 0x7:
1340418bf78SNicholas Piggin             /* invalidate no SLBs, but all lookaside information */
1350418bf78SNicholas Piggin             return;
1360418bf78SNicholas Piggin 
1370418bf78SNicholas Piggin         case 0x3:
1380418bf78SNicholas Piggin         case 0x4:
1390418bf78SNicholas Piggin             /* also considers SLB entry 0 */
1400418bf78SNicholas Piggin             starting_entry = 0;
1410418bf78SNicholas Piggin             break;
1420418bf78SNicholas Piggin 
1430418bf78SNicholas Piggin         case 0x5:
1440418bf78SNicholas Piggin             /* treat undefined values as ih==0, and warn */
1450418bf78SNicholas Piggin             qemu_log_mask(LOG_GUEST_ERROR,
1460418bf78SNicholas Piggin                           "slbia undefined IH field %u.\n", ih);
1470418bf78SNicholas Piggin             break;
1480418bf78SNicholas Piggin 
1490418bf78SNicholas Piggin         default:
1500418bf78SNicholas Piggin             /* 0,1,2,6 */
1510418bf78SNicholas Piggin             break;
1520418bf78SNicholas Piggin         }
1530418bf78SNicholas Piggin     }
1540418bf78SNicholas Piggin 
1550418bf78SNicholas Piggin     for (n = starting_entry; n < cpu->hash64_opts->slb_size; n++) {
156fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
157fcf5ef2aSThomas Huth 
1580418bf78SNicholas Piggin         if (!(slb->esid & SLB_ESID_V)) {
1590418bf78SNicholas Piggin             continue;
1600418bf78SNicholas Piggin         }
1610418bf78SNicholas Piggin         if (env->mmu_model == POWERPC_MMU_3_00) {
1620418bf78SNicholas Piggin             if (ih == 0x3 && (slb->vsid & SLB_VSID_C) == 0) {
1630418bf78SNicholas Piggin                 /* preserves entries with a class value of 0 */
1640418bf78SNicholas Piggin                 continue;
165f9e3e1a3SNicholas Piggin             }
166f9e3e1a3SNicholas Piggin         }
167f9e3e1a3SNicholas Piggin 
1680418bf78SNicholas Piggin         slb->esid &= ~SLB_ESID_V;
1690418bf78SNicholas Piggin     }
170fcf5ef2aSThomas Huth }
171fcf5ef2aSThomas Huth 
172a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr,
173a63f1dfcSNikunj A Dadhania                            target_ulong global)
174fcf5ef2aSThomas Huth {
175db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
176fcf5ef2aSThomas Huth     ppc_slb_t *slb;
177fcf5ef2aSThomas Huth 
178fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, addr);
179fcf5ef2aSThomas Huth     if (!slb) {
180fcf5ef2aSThomas Huth         return;
181fcf5ef2aSThomas Huth     }
182fcf5ef2aSThomas Huth 
183fcf5ef2aSThomas Huth     if (slb->esid & SLB_ESID_V) {
184fcf5ef2aSThomas Huth         slb->esid &= ~SLB_ESID_V;
185fcf5ef2aSThomas Huth 
186d75cbae8SDavid Gibson         /*
187d75cbae8SDavid Gibson          * XXX: given the fact that segment size is 256 MB or 1TB,
188fcf5ef2aSThomas Huth          *      and we still don't have a tlb_flush_mask(env, n, mask)
189fcf5ef2aSThomas Huth          *      in QEMU, we just invalidate all TLBs
190fcf5ef2aSThomas Huth          */
191a63f1dfcSNikunj A Dadhania         env->tlb_need_flush |=
192a63f1dfcSNikunj A Dadhania             (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH);
193fcf5ef2aSThomas Huth     }
194fcf5ef2aSThomas Huth }
195fcf5ef2aSThomas Huth 
196a63f1dfcSNikunj A Dadhania void helper_slbie(CPUPPCState *env, target_ulong addr)
197a63f1dfcSNikunj A Dadhania {
198a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, false);
199a63f1dfcSNikunj A Dadhania }
200a63f1dfcSNikunj A Dadhania 
201a63f1dfcSNikunj A Dadhania void helper_slbieg(CPUPPCState *env, target_ulong addr)
202a63f1dfcSNikunj A Dadhania {
203a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, true);
204a63f1dfcSNikunj A Dadhania }
205a63f1dfcSNikunj A Dadhania 
206fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot,
207fcf5ef2aSThomas Huth                   target_ulong esid, target_ulong vsid)
208fcf5ef2aSThomas Huth {
209fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
210fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
211b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
212fcf5ef2aSThomas Huth     int i;
213fcf5ef2aSThomas Huth 
21467d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
215fcf5ef2aSThomas Huth         return -1; /* Bad slot number */
216fcf5ef2aSThomas Huth     }
217fcf5ef2aSThomas Huth     if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) {
218fcf5ef2aSThomas Huth         return -1; /* Reserved bits set */
219fcf5ef2aSThomas Huth     }
220fcf5ef2aSThomas Huth     if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
221fcf5ef2aSThomas Huth         return -1; /* Bad segment size */
222fcf5ef2aSThomas Huth     }
22358969eeeSDavid Gibson     if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) {
224fcf5ef2aSThomas Huth         return -1; /* 1T segment on MMU that doesn't support it */
225fcf5ef2aSThomas Huth     }
226fcf5ef2aSThomas Huth 
227fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
228b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
229fcf5ef2aSThomas Huth 
230fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
231fcf5ef2aSThomas Huth             break;
232fcf5ef2aSThomas Huth         }
233fcf5ef2aSThomas Huth 
234fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
235fcf5ef2aSThomas Huth             sps = sps1;
236fcf5ef2aSThomas Huth             break;
237fcf5ef2aSThomas Huth         }
238fcf5ef2aSThomas Huth     }
239fcf5ef2aSThomas Huth 
240fcf5ef2aSThomas Huth     if (!sps) {
241fcf5ef2aSThomas Huth         error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
242fcf5ef2aSThomas Huth                      " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx,
243fcf5ef2aSThomas Huth                      slot, esid, vsid);
244fcf5ef2aSThomas Huth         return -1;
245fcf5ef2aSThomas Huth     }
246fcf5ef2aSThomas Huth 
247fcf5ef2aSThomas Huth     slb->esid = esid;
248fcf5ef2aSThomas Huth     slb->vsid = vsid;
249fcf5ef2aSThomas Huth     slb->sps = sps;
250fcf5ef2aSThomas Huth 
25176134d48SSuraj Jitindar Singh     LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx
25276134d48SSuraj Jitindar Singh             " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid,
253fcf5ef2aSThomas Huth             slb->esid, slb->vsid);
254fcf5ef2aSThomas Huth 
255fcf5ef2aSThomas Huth     return 0;
256fcf5ef2aSThomas Huth }
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb,
259fcf5ef2aSThomas Huth                              target_ulong *rt)
260fcf5ef2aSThomas Huth {
261fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
262fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
263fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
264fcf5ef2aSThomas Huth 
26567d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
266fcf5ef2aSThomas Huth         return -1;
267fcf5ef2aSThomas Huth     }
268fcf5ef2aSThomas Huth 
269fcf5ef2aSThomas Huth     *rt = slb->esid;
270fcf5ef2aSThomas Huth     return 0;
271fcf5ef2aSThomas Huth }
272fcf5ef2aSThomas Huth 
273fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
274fcf5ef2aSThomas Huth                              target_ulong *rt)
275fcf5ef2aSThomas Huth {
276fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
277fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
278fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
279fcf5ef2aSThomas Huth 
28067d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
281fcf5ef2aSThomas Huth         return -1;
282fcf5ef2aSThomas Huth     }
283fcf5ef2aSThomas Huth 
284fcf5ef2aSThomas Huth     *rt = slb->vsid;
285fcf5ef2aSThomas Huth     return 0;
286fcf5ef2aSThomas Huth }
287fcf5ef2aSThomas Huth 
288fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
289fcf5ef2aSThomas Huth                              target_ulong *rt)
290fcf5ef2aSThomas Huth {
291fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
292fcf5ef2aSThomas Huth     ppc_slb_t *slb;
293fcf5ef2aSThomas Huth 
294fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, env->msr)) {
295fcf5ef2aSThomas Huth         rb &= 0xffffffff;
296fcf5ef2aSThomas Huth     }
297fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, rb);
298fcf5ef2aSThomas Huth     if (slb == NULL) {
299fcf5ef2aSThomas Huth         *rt = (target_ulong)-1ul;
300fcf5ef2aSThomas Huth     } else {
301fcf5ef2aSThomas Huth         *rt = slb->vsid;
302fcf5ef2aSThomas Huth     }
303fcf5ef2aSThomas Huth     return 0;
304fcf5ef2aSThomas Huth }
305fcf5ef2aSThomas Huth 
306fcf5ef2aSThomas Huth void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs)
307fcf5ef2aSThomas Huth {
308db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
309fcf5ef2aSThomas Huth 
310fcf5ef2aSThomas Huth     if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) {
311fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
312fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
313fcf5ef2aSThomas Huth     }
314fcf5ef2aSThomas Huth }
315fcf5ef2aSThomas Huth 
316fcf5ef2aSThomas Huth target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb)
317fcf5ef2aSThomas Huth {
318db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
319fcf5ef2aSThomas Huth     target_ulong rt = 0;
320fcf5ef2aSThomas Huth 
321fcf5ef2aSThomas Huth     if (ppc_load_slb_esid(cpu, rb, &rt) < 0) {
322fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
323fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
324fcf5ef2aSThomas Huth     }
325fcf5ef2aSThomas Huth     return rt;
326fcf5ef2aSThomas Huth }
327fcf5ef2aSThomas Huth 
328fcf5ef2aSThomas Huth target_ulong helper_find_slb_vsid(CPUPPCState *env, target_ulong rb)
329fcf5ef2aSThomas Huth {
330db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
331fcf5ef2aSThomas Huth     target_ulong rt = 0;
332fcf5ef2aSThomas Huth 
333fcf5ef2aSThomas Huth     if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) {
334fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
335fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
336fcf5ef2aSThomas Huth     }
337fcf5ef2aSThomas Huth     return rt;
338fcf5ef2aSThomas Huth }
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb)
341fcf5ef2aSThomas Huth {
342db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
343fcf5ef2aSThomas Huth     target_ulong rt = 0;
344fcf5ef2aSThomas Huth 
345fcf5ef2aSThomas Huth     if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) {
346fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
347fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
348fcf5ef2aSThomas Huth     }
349fcf5ef2aSThomas Huth     return rt;
350fcf5ef2aSThomas Huth }
351fcf5ef2aSThomas Huth 
35207a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */
35307a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu,
35407a68f99SSuraj Jitindar Singh                                               ppc_hash_pte64_t pte)
35507a68f99SSuraj Jitindar Singh {
35607a68f99SSuraj Jitindar Singh     /* Exec permissions CANNOT take away read or write permissions */
35707a68f99SSuraj Jitindar Singh     return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ?
35807a68f99SSuraj Jitindar Singh             PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC;
35907a68f99SSuraj Jitindar Singh }
36007a68f99SSuraj Jitindar Singh 
36107a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */
362fcf5ef2aSThomas Huth static int ppc_hash64_pte_prot(PowerPCCPU *cpu,
363fcf5ef2aSThomas Huth                                ppc_slb_t *slb, ppc_hash_pte64_t pte)
364fcf5ef2aSThomas Huth {
365fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
366fcf5ef2aSThomas Huth     unsigned pp, key;
367d75cbae8SDavid Gibson     /*
368d75cbae8SDavid Gibson      * Some pp bit combinations have undefined behaviour, so default
369d75cbae8SDavid Gibson      * to no access in those cases
370d75cbae8SDavid Gibson      */
371fcf5ef2aSThomas Huth     int prot = 0;
372fcf5ef2aSThomas Huth 
373fcf5ef2aSThomas Huth     key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP)
374fcf5ef2aSThomas Huth              : (slb->vsid & SLB_VSID_KS));
375fcf5ef2aSThomas Huth     pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
376fcf5ef2aSThomas Huth 
377fcf5ef2aSThomas Huth     if (key == 0) {
378fcf5ef2aSThomas Huth         switch (pp) {
379fcf5ef2aSThomas Huth         case 0x0:
380fcf5ef2aSThomas Huth         case 0x1:
381fcf5ef2aSThomas Huth         case 0x2:
382347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
383fcf5ef2aSThomas Huth             break;
384fcf5ef2aSThomas Huth 
385fcf5ef2aSThomas Huth         case 0x3:
386fcf5ef2aSThomas Huth         case 0x6:
387347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
388fcf5ef2aSThomas Huth             break;
389fcf5ef2aSThomas Huth         }
390fcf5ef2aSThomas Huth     } else {
391fcf5ef2aSThomas Huth         switch (pp) {
392fcf5ef2aSThomas Huth         case 0x0:
393fcf5ef2aSThomas Huth         case 0x6:
394fcf5ef2aSThomas Huth             break;
395fcf5ef2aSThomas Huth 
396fcf5ef2aSThomas Huth         case 0x1:
397fcf5ef2aSThomas Huth         case 0x3:
398347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
399fcf5ef2aSThomas Huth             break;
400fcf5ef2aSThomas Huth 
401fcf5ef2aSThomas Huth         case 0x2:
402347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
403fcf5ef2aSThomas Huth             break;
404fcf5ef2aSThomas Huth         }
405fcf5ef2aSThomas Huth     }
406fcf5ef2aSThomas Huth 
407fcf5ef2aSThomas Huth     return prot;
408fcf5ef2aSThomas Huth }
409fcf5ef2aSThomas Huth 
410a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */
411a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key)
412a6152b52SSuraj Jitindar Singh {
413a6152b52SSuraj Jitindar Singh     CPUPPCState *env = &cpu->env;
414a6152b52SSuraj Jitindar Singh     int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3;
415a6152b52SSuraj Jitindar Singh 
416a6152b52SSuraj Jitindar Singh     /*
417a6152b52SSuraj Jitindar Singh      * An instruction fetch is permitted if the IAMR bit is 0.
418a6152b52SSuraj Jitindar Singh      * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
419a6152b52SSuraj Jitindar Singh      * can only take away EXEC permissions not READ or WRITE permissions.
420a6152b52SSuraj Jitindar Singh      * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
421a6152b52SSuraj Jitindar Singh      * EXEC permissions are allowed.
422a6152b52SSuraj Jitindar Singh      */
423a6152b52SSuraj Jitindar Singh     return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE :
424a6152b52SSuraj Jitindar Singh                                PAGE_READ | PAGE_WRITE | PAGE_EXEC;
425a6152b52SSuraj Jitindar Singh }
426a6152b52SSuraj Jitindar Singh 
427fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte)
428fcf5ef2aSThomas Huth {
429fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
430fcf5ef2aSThomas Huth     int key, amrbits;
431fcf5ef2aSThomas Huth     int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
432fcf5ef2aSThomas Huth 
433fcf5ef2aSThomas Huth     /* Only recent MMUs implement Virtual Page Class Key Protection */
43458969eeeSDavid Gibson     if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) {
435fcf5ef2aSThomas Huth         return prot;
436fcf5ef2aSThomas Huth     }
437fcf5ef2aSThomas Huth 
438fcf5ef2aSThomas Huth     key = HPTE64_R_KEY(pte.pte1);
439fcf5ef2aSThomas Huth     amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3;
440fcf5ef2aSThomas Huth 
441fcf5ef2aSThomas Huth     /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
442fcf5ef2aSThomas Huth     /*         env->spr[SPR_AMR]); */
443fcf5ef2aSThomas Huth 
444fcf5ef2aSThomas Huth     /*
445fcf5ef2aSThomas Huth      * A store is permitted if the AMR bit is 0. Remove write
446fcf5ef2aSThomas Huth      * protection if it is set.
447fcf5ef2aSThomas Huth      */
448fcf5ef2aSThomas Huth     if (amrbits & 0x2) {
449fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
450fcf5ef2aSThomas Huth     }
451fcf5ef2aSThomas Huth     /*
452fcf5ef2aSThomas Huth      * A load is permitted if the AMR bit is 0. Remove read
453fcf5ef2aSThomas Huth      * protection if it is set.
454fcf5ef2aSThomas Huth      */
455fcf5ef2aSThomas Huth     if (amrbits & 0x1) {
456fcf5ef2aSThomas Huth         prot &= ~PAGE_READ;
457fcf5ef2aSThomas Huth     }
458fcf5ef2aSThomas Huth 
459a6152b52SSuraj Jitindar Singh     switch (env->mmu_model) {
460a6152b52SSuraj Jitindar Singh     /*
461a6152b52SSuraj Jitindar Singh      * MMU version 2.07 and later support IAMR
462a6152b52SSuraj Jitindar Singh      * Check if the IAMR allows the instruction access - it will return
463a6152b52SSuraj Jitindar Singh      * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
464a6152b52SSuraj Jitindar Singh      * if it does (and prot will be unchanged indicating execution support).
465a6152b52SSuraj Jitindar Singh      */
466a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_2_07:
467a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_3_00:
468a6152b52SSuraj Jitindar Singh         prot &= ppc_hash64_iamr_prot(cpu, key);
469a6152b52SSuraj Jitindar Singh         break;
470a6152b52SSuraj Jitindar Singh     default:
471a6152b52SSuraj Jitindar Singh         break;
472a6152b52SSuraj Jitindar Singh     }
473a6152b52SSuraj Jitindar Singh 
474fcf5ef2aSThomas Huth     return prot;
475fcf5ef2aSThomas Huth }
476fcf5ef2aSThomas Huth 
4777222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu,
4787222b94aSDavid Gibson                                              hwaddr ptex, int n)
479fcf5ef2aSThomas Huth {
4807222b94aSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
4813367c62fSBenjamin Herrenschmidt     hwaddr base;
4827222b94aSDavid Gibson     hwaddr plen = n * HASH_PTE_SIZE_64;
483e57ca75cSDavid Gibson     const ppc_hash_pte64_t *hptes;
484e57ca75cSDavid Gibson 
485e57ca75cSDavid Gibson     if (cpu->vhyp) {
486e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
487e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
488e57ca75cSDavid Gibson         return vhc->map_hptes(cpu->vhyp, ptex, n);
489e57ca75cSDavid Gibson     }
4903367c62fSBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
491e57ca75cSDavid Gibson 
492e57ca75cSDavid Gibson     if (!base) {
493e57ca75cSDavid Gibson         return NULL;
494e57ca75cSDavid Gibson     }
495e57ca75cSDavid Gibson 
496f26404fbSPeter Maydell     hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false,
497f26404fbSPeter Maydell                               MEMTXATTRS_UNSPECIFIED);
4987222b94aSDavid Gibson     if (plen < (n * HASH_PTE_SIZE_64)) {
4997222b94aSDavid Gibson         hw_error("%s: Unable to map all requested HPTEs\n", __func__);
500fcf5ef2aSThomas Huth     }
5017222b94aSDavid Gibson     return hptes;
502fcf5ef2aSThomas Huth }
503fcf5ef2aSThomas Huth 
5047222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes,
5057222b94aSDavid Gibson                             hwaddr ptex, int n)
506fcf5ef2aSThomas Huth {
507e57ca75cSDavid Gibson     if (cpu->vhyp) {
508e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
509e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
510e57ca75cSDavid Gibson         vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n);
511e57ca75cSDavid Gibson         return;
512e57ca75cSDavid Gibson     }
513e57ca75cSDavid Gibson 
5147222b94aSDavid Gibson     address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64,
5157222b94aSDavid Gibson                         false, n * HASH_PTE_SIZE_64);
516fcf5ef2aSThomas Huth }
517fcf5ef2aSThomas Huth 
518b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps,
519fcf5ef2aSThomas Huth                                 uint64_t pte0, uint64_t pte1)
520fcf5ef2aSThomas Huth {
521fcf5ef2aSThomas Huth     int i;
522fcf5ef2aSThomas Huth 
523fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
524fcf5ef2aSThomas Huth         if (sps->page_shift != 12) {
525fcf5ef2aSThomas Huth             /* 4kiB page in a non 4kiB segment */
526fcf5ef2aSThomas Huth             return 0;
527fcf5ef2aSThomas Huth         }
528fcf5ef2aSThomas Huth         /* Normal 4kiB page */
529fcf5ef2aSThomas Huth         return 12;
530fcf5ef2aSThomas Huth     }
531fcf5ef2aSThomas Huth 
532fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
533b07c59f7SDavid Gibson         const PPCHash64PageSize *ps = &sps->enc[i];
534fcf5ef2aSThomas Huth         uint64_t mask;
535fcf5ef2aSThomas Huth 
536fcf5ef2aSThomas Huth         if (!ps->page_shift) {
537fcf5ef2aSThomas Huth             break;
538fcf5ef2aSThomas Huth         }
539fcf5ef2aSThomas Huth 
540fcf5ef2aSThomas Huth         if (ps->page_shift == 12) {
541fcf5ef2aSThomas Huth             /* L bit is set so this can't be a 4kiB page */
542fcf5ef2aSThomas Huth             continue;
543fcf5ef2aSThomas Huth         }
544fcf5ef2aSThomas Huth 
545fcf5ef2aSThomas Huth         mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
546fcf5ef2aSThomas Huth 
547fcf5ef2aSThomas Huth         if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
548fcf5ef2aSThomas Huth             return ps->page_shift;
549fcf5ef2aSThomas Huth         }
550fcf5ef2aSThomas Huth     }
551fcf5ef2aSThomas Huth 
552fcf5ef2aSThomas Huth     return 0; /* Bad page size encoding */
553fcf5ef2aSThomas Huth }
554fcf5ef2aSThomas Huth 
55534525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1)
55634525595SBenjamin Herrenschmidt {
55734525595SBenjamin Herrenschmidt     /* Insert B into pte0 */
55834525595SBenjamin Herrenschmidt     *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) |
55934525595SBenjamin Herrenschmidt             ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) <<
56034525595SBenjamin Herrenschmidt              (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT));
56134525595SBenjamin Herrenschmidt 
56234525595SBenjamin Herrenschmidt     /* Remove B from pte1 */
56334525595SBenjamin Herrenschmidt     *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK;
56434525595SBenjamin Herrenschmidt }
56534525595SBenjamin Herrenschmidt 
56634525595SBenjamin Herrenschmidt 
567fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
568b07c59f7SDavid Gibson                                      const PPCHash64SegmentPageSizes *sps,
569fcf5ef2aSThomas Huth                                      target_ulong ptem,
570fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
571fcf5ef2aSThomas Huth {
572fcf5ef2aSThomas Huth     int i;
5737222b94aSDavid Gibson     const ppc_hash_pte64_t *pteg;
574fcf5ef2aSThomas Huth     target_ulong pte0, pte1;
5757222b94aSDavid Gibson     target_ulong ptex;
576fcf5ef2aSThomas Huth 
57736778660SDavid Gibson     ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP;
5787222b94aSDavid Gibson     pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
5797222b94aSDavid Gibson     if (!pteg) {
580fcf5ef2aSThomas Huth         return -1;
581fcf5ef2aSThomas Huth     }
582fcf5ef2aSThomas Huth     for (i = 0; i < HPTES_PER_GROUP; i++) {
5837222b94aSDavid Gibson         pte0 = ppc_hash64_hpte0(cpu, pteg, i);
5843054b0caSBenjamin Herrenschmidt         /*
5853054b0caSBenjamin Herrenschmidt          * pte0 contains the valid bit and must be read before pte1,
5863054b0caSBenjamin Herrenschmidt          * otherwise we might see an old pte1 with a new valid bit and
5873054b0caSBenjamin Herrenschmidt          * thus an inconsistent hpte value
5883054b0caSBenjamin Herrenschmidt          */
5893054b0caSBenjamin Herrenschmidt         smp_rmb();
5907222b94aSDavid Gibson         pte1 = ppc_hash64_hpte1(cpu, pteg, i);
591fcf5ef2aSThomas Huth 
59234525595SBenjamin Herrenschmidt         /* Convert format if necessary */
59334525595SBenjamin Herrenschmidt         if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) {
59434525595SBenjamin Herrenschmidt             ppc64_v3_new_to_old_hpte(&pte0, &pte1);
59534525595SBenjamin Herrenschmidt         }
59634525595SBenjamin Herrenschmidt 
597fcf5ef2aSThomas Huth         /* This compares V, B, H (secondary) and the AVPN */
598fcf5ef2aSThomas Huth         if (HPTE64_V_COMPARE(pte0, ptem)) {
599fcf5ef2aSThomas Huth             *pshift = hpte_page_shift(sps, pte0, pte1);
600fcf5ef2aSThomas Huth             /*
601fcf5ef2aSThomas Huth              * If there is no match, ignore the PTE, it could simply
602fcf5ef2aSThomas Huth              * be for a different segment size encoding and the
603fcf5ef2aSThomas Huth              * architecture specifies we should not match. Linux will
604fcf5ef2aSThomas Huth              * potentially leave behind PTEs for the wrong base page
605fcf5ef2aSThomas Huth              * size when demoting segments.
606fcf5ef2aSThomas Huth              */
607fcf5ef2aSThomas Huth             if (*pshift == 0) {
608fcf5ef2aSThomas Huth                 continue;
609fcf5ef2aSThomas Huth             }
610d75cbae8SDavid Gibson             /*
611d75cbae8SDavid Gibson              * We don't do anything with pshift yet as qemu TLB only
612d75cbae8SDavid Gibson              * deals with 4K pages anyway
613fcf5ef2aSThomas Huth              */
614fcf5ef2aSThomas Huth             pte->pte0 = pte0;
615fcf5ef2aSThomas Huth             pte->pte1 = pte1;
6167222b94aSDavid Gibson             ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
6177222b94aSDavid Gibson             return ptex + i;
618fcf5ef2aSThomas Huth         }
619fcf5ef2aSThomas Huth     }
6207222b94aSDavid Gibson     ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
621fcf5ef2aSThomas Huth     /*
622fcf5ef2aSThomas Huth      * We didn't find a valid entry.
623fcf5ef2aSThomas Huth      */
624fcf5ef2aSThomas Huth     return -1;
625fcf5ef2aSThomas Huth }
626fcf5ef2aSThomas Huth 
627fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
628fcf5ef2aSThomas Huth                                      ppc_slb_t *slb, target_ulong eaddr,
629fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
630fcf5ef2aSThomas Huth {
631fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
6327222b94aSDavid Gibson     hwaddr hash, ptex;
633fcf5ef2aSThomas Huth     uint64_t vsid, epnmask, epn, ptem;
634b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = slb->sps;
635fcf5ef2aSThomas Huth 
636d75cbae8SDavid Gibson     /*
637d75cbae8SDavid Gibson      * The SLB store path should prevent any bad page size encodings
638d75cbae8SDavid Gibson      * getting in there, so:
639d75cbae8SDavid Gibson      */
640fcf5ef2aSThomas Huth     assert(sps);
641fcf5ef2aSThomas Huth 
642fcf5ef2aSThomas Huth     /* If ISL is set in LPCR we need to clamp the page size to 4K */
643fcf5ef2aSThomas Huth     if (env->spr[SPR_LPCR] & LPCR_ISL) {
644fcf5ef2aSThomas Huth         /* We assume that when using TCG, 4k is first entry of SPS */
645b07c59f7SDavid Gibson         sps = &cpu->hash64_opts->sps[0];
646fcf5ef2aSThomas Huth         assert(sps->page_shift == 12);
647fcf5ef2aSThomas Huth     }
648fcf5ef2aSThomas Huth 
649fcf5ef2aSThomas Huth     epnmask = ~((1ULL << sps->page_shift) - 1);
650fcf5ef2aSThomas Huth 
651fcf5ef2aSThomas Huth     if (slb->vsid & SLB_VSID_B) {
652fcf5ef2aSThomas Huth         /* 1TB segment */
653fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
654fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
655fcf5ef2aSThomas Huth         hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
656fcf5ef2aSThomas Huth     } else {
657fcf5ef2aSThomas Huth         /* 256M segment */
658fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
659fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
660fcf5ef2aSThomas Huth         hash = vsid ^ (epn >> sps->page_shift);
661fcf5ef2aSThomas Huth     }
662fcf5ef2aSThomas Huth     ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
663fcf5ef2aSThomas Huth     ptem |= HPTE64_V_VALID;
664fcf5ef2aSThomas Huth 
665fcf5ef2aSThomas Huth     /* Page address translation */
666fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
667fcf5ef2aSThomas Huth             "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
668fcf5ef2aSThomas Huth             " hash " TARGET_FMT_plx "\n",
66936778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash);
670fcf5ef2aSThomas Huth 
671fcf5ef2aSThomas Huth     /* Primary PTEG lookup */
672fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
673fcf5ef2aSThomas Huth             "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
674fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
675fcf5ef2aSThomas Huth             " hash=" TARGET_FMT_plx "\n",
67636778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu),
67736778660SDavid Gibson             vsid, ptem,  hash);
6787222b94aSDavid Gibson     ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
679fcf5ef2aSThomas Huth 
6807222b94aSDavid Gibson     if (ptex == -1) {
681fcf5ef2aSThomas Huth         /* Secondary PTEG lookup */
682fcf5ef2aSThomas Huth         ptem |= HPTE64_V_SECONDARY;
683fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU,
684fcf5ef2aSThomas Huth                 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
685fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
68636778660SDavid Gibson                 " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu),
68736778660SDavid Gibson                 ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash);
688fcf5ef2aSThomas Huth 
6897222b94aSDavid Gibson         ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
690fcf5ef2aSThomas Huth     }
691fcf5ef2aSThomas Huth 
6927222b94aSDavid Gibson     return ptex;
693fcf5ef2aSThomas Huth }
694fcf5ef2aSThomas Huth 
695fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
696fcf5ef2aSThomas Huth                                           uint64_t pte0, uint64_t pte1)
697fcf5ef2aSThomas Huth {
698fcf5ef2aSThomas Huth     int i;
699fcf5ef2aSThomas Huth 
700fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
701fcf5ef2aSThomas Huth         return 12;
702fcf5ef2aSThomas Huth     }
703fcf5ef2aSThomas Huth 
704fcf5ef2aSThomas Huth     /*
705fcf5ef2aSThomas Huth      * The encodings in env->sps need to be carefully chosen so that
706fcf5ef2aSThomas Huth      * this gives an unambiguous result.
707fcf5ef2aSThomas Huth      */
708fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
709b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
710fcf5ef2aSThomas Huth         unsigned shift;
711fcf5ef2aSThomas Huth 
712fcf5ef2aSThomas Huth         if (!sps->page_shift) {
713fcf5ef2aSThomas Huth             break;
714fcf5ef2aSThomas Huth         }
715fcf5ef2aSThomas Huth 
716fcf5ef2aSThomas Huth         shift = hpte_page_shift(sps, pte0, pte1);
717fcf5ef2aSThomas Huth         if (shift) {
718fcf5ef2aSThomas Huth             return shift;
719fcf5ef2aSThomas Huth         }
720fcf5ef2aSThomas Huth     }
721fcf5ef2aSThomas Huth 
722fcf5ef2aSThomas Huth     return 0;
723fcf5ef2aSThomas Huth }
724fcf5ef2aSThomas Huth 
7251b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env)
7261b99e029SDavid Gibson {
7271b99e029SDavid Gibson     switch (env->mmu_model) {
7281b99e029SDavid Gibson     case POWERPC_MMU_3_00:
7291b99e029SDavid Gibson         /*
7301b99e029SDavid Gibson          * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR
7311b99e029SDavid Gibson          * register no longer exist
7321b99e029SDavid Gibson          */
7331b99e029SDavid Gibson         return true;
7341b99e029SDavid Gibson 
7351b99e029SDavid Gibson     default:
7361b99e029SDavid Gibson         return !!(env->spr[SPR_LPCR] & LPCR_VPM0);
7371b99e029SDavid Gibson     }
7381b99e029SDavid Gibson }
7391b99e029SDavid Gibson 
7408fe08facSDavid Gibson static void ppc_hash64_set_isi(CPUState *cs, uint64_t error_code)
741fcf5ef2aSThomas Huth {
7428fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
743fcf5ef2aSThomas Huth     bool vpm;
744fcf5ef2aSThomas Huth 
745fcf5ef2aSThomas Huth     if (msr_ir) {
746fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
747fcf5ef2aSThomas Huth     } else {
7481b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
749fcf5ef2aSThomas Huth     }
750fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
751fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HISI;
752fcf5ef2aSThomas Huth     } else {
753fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
754fcf5ef2aSThomas Huth     }
755fcf5ef2aSThomas Huth     env->error_code = error_code;
756fcf5ef2aSThomas Huth }
757fcf5ef2aSThomas Huth 
7588fe08facSDavid Gibson static void ppc_hash64_set_dsi(CPUState *cs, uint64_t dar, uint64_t dsisr)
759fcf5ef2aSThomas Huth {
7608fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
761fcf5ef2aSThomas Huth     bool vpm;
762fcf5ef2aSThomas Huth 
763fcf5ef2aSThomas Huth     if (msr_dr) {
764fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
765fcf5ef2aSThomas Huth     } else {
7661b99e029SDavid Gibson         vpm = ppc_hash64_use_vrma(env);
767fcf5ef2aSThomas Huth     }
768fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
769fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
770fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
771fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
772fcf5ef2aSThomas Huth     } else {
773fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
774fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
775fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
776fcf5ef2aSThomas Huth    }
777fcf5ef2aSThomas Huth     env->error_code = 0;
778fcf5ef2aSThomas Huth }
779fcf5ef2aSThomas Huth 
780fcf5ef2aSThomas Huth 
781a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
782a2dd4e83SBenjamin Herrenschmidt {
783a2dd4e83SBenjamin Herrenschmidt     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 16;
784a2dd4e83SBenjamin Herrenschmidt 
785a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
786a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
787a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
788a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_r(cpu->vhyp, ptex, pte1);
789a2dd4e83SBenjamin Herrenschmidt         return;
790a2dd4e83SBenjamin Herrenschmidt     }
791a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
792a2dd4e83SBenjamin Herrenschmidt 
793a2dd4e83SBenjamin Herrenschmidt 
794a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
795a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01);
796a2dd4e83SBenjamin Herrenschmidt }
797a2dd4e83SBenjamin Herrenschmidt 
798a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
799a2dd4e83SBenjamin Herrenschmidt {
800a2dd4e83SBenjamin Herrenschmidt     hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 15;
801a2dd4e83SBenjamin Herrenschmidt 
802a2dd4e83SBenjamin Herrenschmidt     if (cpu->vhyp) {
803a2dd4e83SBenjamin Herrenschmidt         PPCVirtualHypervisorClass *vhc =
804a2dd4e83SBenjamin Herrenschmidt             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
805a2dd4e83SBenjamin Herrenschmidt         vhc->hpte_set_c(cpu->vhyp, ptex, pte1);
806a2dd4e83SBenjamin Herrenschmidt         return;
807a2dd4e83SBenjamin Herrenschmidt     }
808a2dd4e83SBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
809a2dd4e83SBenjamin Herrenschmidt 
810a2dd4e83SBenjamin Herrenschmidt     /* The HW performs a non-atomic byte update */
811a2dd4e83SBenjamin Herrenschmidt     stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80);
812a2dd4e83SBenjamin Herrenschmidt }
813a2dd4e83SBenjamin Herrenschmidt 
814a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu)
815a864a6b3SDavid Gibson {
816a864a6b3SDavid Gibson     CPUPPCState *env = &cpu->env;
817a864a6b3SDavid Gibson     /*
818d37b40daSDavid Gibson      * In theory the meanings of RMLS values are implementation
819d37b40daSDavid Gibson      * dependent.  In practice, this seems to have been the set from
820d37b40daSDavid Gibson      * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
821a864a6b3SDavid Gibson      *
822a864a6b3SDavid Gibson      * Unsupported values mean the OS has shot itself in the
823a864a6b3SDavid Gibson      * foot. Return a 0-sized RMA in this case, which we expect
824a864a6b3SDavid Gibson      * to trigger an immediate DSI or ISI
825a864a6b3SDavid Gibson      */
826a864a6b3SDavid Gibson     static const target_ulong rma_sizes[16] = {
827d37b40daSDavid Gibson         [0] = 256 * GiB,
828a864a6b3SDavid Gibson         [1] = 16 * GiB,
829a864a6b3SDavid Gibson         [2] = 1 * GiB,
830a864a6b3SDavid Gibson         [3] = 64 * MiB,
831a864a6b3SDavid Gibson         [4] = 256 * MiB,
832a864a6b3SDavid Gibson         [7] = 128 * MiB,
833a864a6b3SDavid Gibson         [8] = 32 * MiB,
834a864a6b3SDavid Gibson     };
835a864a6b3SDavid Gibson     target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT;
836a864a6b3SDavid Gibson 
837a864a6b3SDavid Gibson     return rma_sizes[rmls];
838a864a6b3SDavid Gibson }
839a864a6b3SDavid Gibson 
8404c24a87fSDavid Gibson static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb)
8414c24a87fSDavid Gibson {
8424c24a87fSDavid Gibson     CPUPPCState *env = &cpu->env;
8434c24a87fSDavid Gibson     target_ulong lpcr = env->spr[SPR_LPCR];
8444c24a87fSDavid Gibson     uint32_t vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
8454c24a87fSDavid Gibson     target_ulong vsid = SLB_VSID_VRMA | ((vrmasd << 4) & SLB_VSID_LLP_MASK);
8464c24a87fSDavid Gibson     int i;
8474c24a87fSDavid Gibson 
8484c24a87fSDavid Gibson     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
8494c24a87fSDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
8504c24a87fSDavid Gibson 
8514c24a87fSDavid Gibson         if (!sps->page_shift) {
8524c24a87fSDavid Gibson             break;
8534c24a87fSDavid Gibson         }
8544c24a87fSDavid Gibson 
8554c24a87fSDavid Gibson         if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) {
8564c24a87fSDavid Gibson             slb->esid = SLB_ESID_V;
8574c24a87fSDavid Gibson             slb->vsid = vsid;
8584c24a87fSDavid Gibson             slb->sps = sps;
8594c24a87fSDavid Gibson             return 0;
8604c24a87fSDavid Gibson         }
8614c24a87fSDavid Gibson     }
8624c24a87fSDavid Gibson 
8634c24a87fSDavid Gibson     error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x"
864ff5b5d5bSMarkus Armbruster                  TARGET_FMT_lx, lpcr);
8654c24a87fSDavid Gibson 
8664c24a87fSDavid Gibson     return -1;
8674c24a87fSDavid Gibson }
8684c24a87fSDavid Gibson 
869fcf5ef2aSThomas Huth int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr,
870fcf5ef2aSThomas Huth                                 int rwx, int mmu_idx)
871fcf5ef2aSThomas Huth {
872fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
873fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
8744c24a87fSDavid Gibson     ppc_slb_t vrma_slbe;
875fcf5ef2aSThomas Huth     ppc_slb_t *slb;
876fcf5ef2aSThomas Huth     unsigned apshift;
8777222b94aSDavid Gibson     hwaddr ptex;
878fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
87907a68f99SSuraj Jitindar Singh     int exec_prot, pp_prot, amr_prot, prot;
880*182357dbSRichard Henderson     int need_prot;
881fcf5ef2aSThomas Huth     hwaddr raddr;
882fcf5ef2aSThomas Huth 
883fcf5ef2aSThomas Huth     assert((rwx == 0) || (rwx == 1) || (rwx == 2));
884fcf5ef2aSThomas Huth 
885d75cbae8SDavid Gibson     /*
886d75cbae8SDavid Gibson      * Note on LPCR usage: 970 uses HID4, but our special variant of
887d75cbae8SDavid Gibson      * store_spr copies relevant fields into env->spr[SPR_LPCR].
888136fbf65Szhaolichang      * Similarly we filter unimplemented bits when storing into LPCR
889d75cbae8SDavid Gibson      * depending on the MMU version. This code can thus just use the
890d75cbae8SDavid Gibson      * LPCR "as-is".
891fcf5ef2aSThomas Huth      */
892fcf5ef2aSThomas Huth 
893fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
894fcf5ef2aSThomas Huth     if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) {
895d75cbae8SDavid Gibson         /*
896d75cbae8SDavid Gibson          * Translation is supposedly "off", but in real mode the top 4
897d75cbae8SDavid Gibson          * effective address bits are (mostly) ignored
898d75cbae8SDavid Gibson          */
899fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
900fcf5ef2aSThomas Huth 
901682c1dfbSDavid Gibson         if (cpu->vhyp) {
902682c1dfbSDavid Gibson             /*
903682c1dfbSDavid Gibson              * In virtual hypervisor mode, there's nothing to do:
904682c1dfbSDavid Gibson              *   EA == GPA == qemu guest address
905682c1dfbSDavid Gibson              */
906682c1dfbSDavid Gibson         } else if (msr_hv || !env->has_hv_mode) {
907fcf5ef2aSThomas Huth             /* In HV mode, add HRMOR if top EA bit is clear */
908fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
909fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
910fcf5ef2aSThomas Huth             }
9111b99e029SDavid Gibson         } else if (ppc_hash64_use_vrma(env)) {
912682c1dfbSDavid Gibson             /* Emulated VRMA mode */
9134c24a87fSDavid Gibson             slb = &vrma_slbe;
9144c24a87fSDavid Gibson             if (build_vrma_slbe(cpu, slb) != 0) {
915682c1dfbSDavid Gibson                 /* Invalid VRMA setup, machine check */
916fcf5ef2aSThomas Huth                 cs->exception_index = POWERPC_EXCP_MCHECK;
917fcf5ef2aSThomas Huth                 env->error_code = 0;
918fcf5ef2aSThomas Huth                 return 1;
919682c1dfbSDavid Gibson             }
920682c1dfbSDavid Gibson 
921682c1dfbSDavid Gibson             goto skip_slb_search;
922fcf5ef2aSThomas Huth         } else {
9233a56a55cSDavid Gibson             target_ulong limit = rmls_limit(cpu);
9243a56a55cSDavid Gibson 
925682c1dfbSDavid Gibson             /* Emulated old-style RMO mode, bounds check against RMLS */
9263a56a55cSDavid Gibson             if (raddr >= limit) {
927fcf5ef2aSThomas Huth                 if (rwx == 2) {
9288fe08facSDavid Gibson                     ppc_hash64_set_isi(cs, SRR1_PROTFAULT);
929fcf5ef2aSThomas Huth                 } else {
930da82c73aSSuraj Jitindar Singh                     int dsisr = DSISR_PROTFAULT;
931fcf5ef2aSThomas Huth                     if (rwx == 1) {
932da82c73aSSuraj Jitindar Singh                         dsisr |= DSISR_ISSTORE;
933fcf5ef2aSThomas Huth                     }
9348fe08facSDavid Gibson                     ppc_hash64_set_dsi(cs, eaddr, dsisr);
935fcf5ef2aSThomas Huth                 }
936fcf5ef2aSThomas Huth                 return 1;
937fcf5ef2aSThomas Huth             }
938682c1dfbSDavid Gibson 
939682c1dfbSDavid Gibson             raddr |= env->spr[SPR_RMOR];
940fcf5ef2aSThomas Huth         }
941fcf5ef2aSThomas Huth         tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
942fcf5ef2aSThomas Huth                      PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx,
943fcf5ef2aSThomas Huth                      TARGET_PAGE_SIZE);
944fcf5ef2aSThomas Huth         return 0;
945fcf5ef2aSThomas Huth     }
946fcf5ef2aSThomas Huth 
947fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
948fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
949fcf5ef2aSThomas Huth     if (!slb) {
950b2899495SSuraj Jitindar Singh         /* No entry found, check if in-memory segment tables are in use */
951ca79b3b7SDavid Gibson         if (ppc64_use_proc_tbl(cpu)) {
952b2899495SSuraj Jitindar Singh             /* TODO - Unsupported */
953b2899495SSuraj Jitindar Singh             error_report("Segment Table Support Unimplemented");
954b2899495SSuraj Jitindar Singh             exit(1);
955b2899495SSuraj Jitindar Singh         }
956b2899495SSuraj Jitindar Singh         /* Segment still not found, generate the appropriate interrupt */
957fcf5ef2aSThomas Huth         if (rwx == 2) {
958fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
959fcf5ef2aSThomas Huth             env->error_code = 0;
960fcf5ef2aSThomas Huth         } else {
961fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
962fcf5ef2aSThomas Huth             env->error_code = 0;
963fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
964fcf5ef2aSThomas Huth         }
965fcf5ef2aSThomas Huth         return 1;
966fcf5ef2aSThomas Huth     }
967fcf5ef2aSThomas Huth 
968fcf5ef2aSThomas Huth skip_slb_search:
969fcf5ef2aSThomas Huth 
970fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
971fcf5ef2aSThomas Huth     if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) {
9728fe08facSDavid Gibson         ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD);
973fcf5ef2aSThomas Huth         return 1;
974fcf5ef2aSThomas Huth     }
975fcf5ef2aSThomas Huth 
976fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
9777222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
9787222b94aSDavid Gibson     if (ptex == -1) {
979fcf5ef2aSThomas Huth         if (rwx == 2) {
9808fe08facSDavid Gibson             ppc_hash64_set_isi(cs, SRR1_NOPTE);
981fcf5ef2aSThomas Huth         } else {
982da82c73aSSuraj Jitindar Singh             int dsisr = DSISR_NOPTE;
983fcf5ef2aSThomas Huth             if (rwx == 1) {
984da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
985fcf5ef2aSThomas Huth             }
9868fe08facSDavid Gibson             ppc_hash64_set_dsi(cs, eaddr, dsisr);
987fcf5ef2aSThomas Huth         }
988fcf5ef2aSThomas Huth         return 1;
989fcf5ef2aSThomas Huth     }
990fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
9917222b94aSDavid Gibson                   "found PTE at index %08" HWADDR_PRIx "\n", ptex);
992fcf5ef2aSThomas Huth 
993fcf5ef2aSThomas Huth     /* 5. Check access permissions */
994fcf5ef2aSThomas Huth 
99507a68f99SSuraj Jitindar Singh     exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
996fcf5ef2aSThomas Huth     pp_prot = ppc_hash64_pte_prot(cpu, slb, pte);
997fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
99807a68f99SSuraj Jitindar Singh     prot = exec_prot & pp_prot & amr_prot;
999fcf5ef2aSThomas Huth 
1000*182357dbSRichard Henderson     need_prot = prot_for_access_type(rwx);
1001*182357dbSRichard Henderson     if (need_prot & ~prot) {
1002fcf5ef2aSThomas Huth         /* Access right violation */
1003fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
1004fcf5ef2aSThomas Huth         if (rwx == 2) {
1005a6152b52SSuraj Jitindar Singh             int srr1 = 0;
100607a68f99SSuraj Jitindar Singh             if (PAGE_EXEC & ~exec_prot) {
100707a68f99SSuraj Jitindar Singh                 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
100807a68f99SSuraj Jitindar Singh             } else if (PAGE_EXEC & ~pp_prot) {
1009a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
1010a6152b52SSuraj Jitindar Singh             }
1011a6152b52SSuraj Jitindar Singh             if (PAGE_EXEC & ~amr_prot) {
1012a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
1013a6152b52SSuraj Jitindar Singh             }
10148fe08facSDavid Gibson             ppc_hash64_set_isi(cs, srr1);
1015fcf5ef2aSThomas Huth         } else {
1016da82c73aSSuraj Jitindar Singh             int dsisr = 0;
1017*182357dbSRichard Henderson             if (need_prot & ~pp_prot) {
1018da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_PROTFAULT;
1019fcf5ef2aSThomas Huth             }
1020fcf5ef2aSThomas Huth             if (rwx == 1) {
1021da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
1022fcf5ef2aSThomas Huth             }
1023*182357dbSRichard Henderson             if (need_prot & ~amr_prot) {
1024da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_AMR;
1025fcf5ef2aSThomas Huth             }
10268fe08facSDavid Gibson             ppc_hash64_set_dsi(cs, eaddr, dsisr);
1027fcf5ef2aSThomas Huth         }
1028fcf5ef2aSThomas Huth         return 1;
1029fcf5ef2aSThomas Huth     }
1030fcf5ef2aSThomas Huth 
1031fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
1032fcf5ef2aSThomas Huth 
1033fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
1034fcf5ef2aSThomas Huth 
1035a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_R)) {
1036a2dd4e83SBenjamin Herrenschmidt         ppc_hash64_set_r(cpu, ptex, pte.pte1);
1037a2dd4e83SBenjamin Herrenschmidt     }
1038a2dd4e83SBenjamin Herrenschmidt     if (!(pte.pte1 & HPTE64_R_C)) {
1039fcf5ef2aSThomas Huth         if (rwx == 1) {
1040a2dd4e83SBenjamin Herrenschmidt             ppc_hash64_set_c(cpu, ptex, pte.pte1);
1041fcf5ef2aSThomas Huth         } else {
1042d75cbae8SDavid Gibson             /*
1043d75cbae8SDavid Gibson              * Treat the page as read-only for now, so that a later write
1044d75cbae8SDavid Gibson              * will pass through this function again to set the C bit
1045d75cbae8SDavid Gibson              */
1046fcf5ef2aSThomas Huth             prot &= ~PAGE_WRITE;
1047fcf5ef2aSThomas Huth         }
1048fcf5ef2aSThomas Huth     }
1049fcf5ef2aSThomas Huth 
1050fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
1051fcf5ef2aSThomas Huth 
1052fcf5ef2aSThomas Huth     raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
1053fcf5ef2aSThomas Huth 
1054fcf5ef2aSThomas Huth     tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
1055fcf5ef2aSThomas Huth                  prot, mmu_idx, 1ULL << apshift);
1056fcf5ef2aSThomas Huth 
1057fcf5ef2aSThomas Huth     return 0;
1058fcf5ef2aSThomas Huth }
1059fcf5ef2aSThomas Huth 
1060fcf5ef2aSThomas Huth hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr)
1061fcf5ef2aSThomas Huth {
1062fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
10634c24a87fSDavid Gibson     ppc_slb_t vrma_slbe;
1064fcf5ef2aSThomas Huth     ppc_slb_t *slb;
10657222b94aSDavid Gibson     hwaddr ptex, raddr;
1066fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
1067fcf5ef2aSThomas Huth     unsigned apshift;
1068fcf5ef2aSThomas Huth 
1069fcf5ef2aSThomas Huth     /* Handle real mode */
1070fcf5ef2aSThomas Huth     if (msr_dr == 0) {
1071fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are ignored */
1072fcf5ef2aSThomas Huth         raddr = addr & 0x0FFFFFFFFFFFFFFFULL;
1073fcf5ef2aSThomas Huth 
1074682c1dfbSDavid Gibson         if (cpu->vhyp) {
1075682c1dfbSDavid Gibson             /*
1076682c1dfbSDavid Gibson              * In virtual hypervisor mode, there's nothing to do:
1077682c1dfbSDavid Gibson              *   EA == GPA == qemu guest address
1078682c1dfbSDavid Gibson              */
1079682c1dfbSDavid Gibson             return raddr;
1080682c1dfbSDavid Gibson         } else if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) {
1081fcf5ef2aSThomas Huth             /* In HV mode, add HRMOR if top EA bit is clear */
1082fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_HRMOR];
10831b99e029SDavid Gibson         } else if (ppc_hash64_use_vrma(env)) {
1084682c1dfbSDavid Gibson             /* Emulated VRMA mode */
10854c24a87fSDavid Gibson             slb = &vrma_slbe;
10864c24a87fSDavid Gibson             if (build_vrma_slbe(cpu, slb) != 0) {
1087fcf5ef2aSThomas Huth                 return -1;
1088fcf5ef2aSThomas Huth             }
1089fcf5ef2aSThomas Huth         } else {
10903a56a55cSDavid Gibson             target_ulong limit = rmls_limit(cpu);
10913a56a55cSDavid Gibson 
1092682c1dfbSDavid Gibson             /* Emulated old-style RMO mode, bounds check against RMLS */
10933a56a55cSDavid Gibson             if (raddr >= limit) {
1094fcf5ef2aSThomas Huth                 return -1;
1095fcf5ef2aSThomas Huth             }
1096682c1dfbSDavid Gibson             return raddr | env->spr[SPR_RMOR];
1097682c1dfbSDavid Gibson         }
1098fcf5ef2aSThomas Huth     } else {
1099fcf5ef2aSThomas Huth         slb = slb_lookup(cpu, addr);
1100fcf5ef2aSThomas Huth         if (!slb) {
1101fcf5ef2aSThomas Huth             return -1;
1102fcf5ef2aSThomas Huth         }
1103fcf5ef2aSThomas Huth     }
1104fcf5ef2aSThomas Huth 
11057222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift);
11067222b94aSDavid Gibson     if (ptex == -1) {
1107fcf5ef2aSThomas Huth         return -1;
1108fcf5ef2aSThomas Huth     }
1109fcf5ef2aSThomas Huth 
1110fcf5ef2aSThomas Huth     return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr)
1111fcf5ef2aSThomas Huth         & TARGET_PAGE_MASK;
1112fcf5ef2aSThomas Huth }
1113fcf5ef2aSThomas Huth 
11147222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
1115fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
1116fcf5ef2aSThomas Huth {
1117fcf5ef2aSThomas Huth     /*
1118fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
1119fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
1120fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
1121fcf5ef2aSThomas Huth      */
1122fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
1123fcf5ef2aSThomas Huth }
1124fcf5ef2aSThomas Huth 
11255ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val)
11265ad55315SDavid Gibson {
1127db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
11285ad55315SDavid Gibson 
11295ad55315SDavid Gibson     ppc_store_lpcr(cpu, val);
11305ad55315SDavid Gibson }
11315ad55315SDavid Gibson 
1132a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu)
1133a059471dSDavid Gibson {
1134a059471dSDavid Gibson     CPUPPCState *env = &cpu->env;
1135a059471dSDavid Gibson     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1136a059471dSDavid Gibson 
113721e405f1SDavid Gibson     if (!pcc->hash64_opts) {
1138d57d72a8SGreg Kurz         assert(!mmu_is_64bit(env->mmu_model));
113921e405f1SDavid Gibson         return;
114021e405f1SDavid Gibson     }
114121e405f1SDavid Gibson 
114221e405f1SDavid Gibson     cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
114321e405f1SDavid Gibson }
114421e405f1SDavid Gibson 
114521e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu)
114621e405f1SDavid Gibson {
114721e405f1SDavid Gibson     g_free(cpu->hash64_opts);
114821e405f1SDavid Gibson }
114921e405f1SDavid Gibson 
115021e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = {
115158969eeeSDavid Gibson     .flags = 0,
115267d7d66fSDavid Gibson     .slb_size = 64,
1153a059471dSDavid Gibson     .sps = {
1154a059471dSDavid Gibson         { .page_shift = 12, /* 4K */
1155a059471dSDavid Gibson           .slb_enc = 0,
1156a059471dSDavid Gibson           .enc = { { .page_shift = 12, .pte_enc = 0 } }
1157a059471dSDavid Gibson         },
1158a059471dSDavid Gibson         { .page_shift = 24, /* 16M */
1159a059471dSDavid Gibson           .slb_enc = 0x100,
1160a059471dSDavid Gibson           .enc = { { .page_shift = 24, .pte_enc = 0 } }
1161a059471dSDavid Gibson         },
1162a059471dSDavid Gibson     },
1163a059471dSDavid Gibson };
1164b07c59f7SDavid Gibson 
1165b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = {
116626cd35b8SDavid Gibson     .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
116767d7d66fSDavid Gibson     .slb_size = 32,
1168b07c59f7SDavid Gibson     .sps = {
1169b07c59f7SDavid Gibson         {
1170b07c59f7SDavid Gibson             .page_shift = 12, /* 4K */
1171b07c59f7SDavid Gibson             .slb_enc = 0,
1172b07c59f7SDavid Gibson             .enc = { { .page_shift = 12, .pte_enc = 0 },
1173b07c59f7SDavid Gibson                      { .page_shift = 16, .pte_enc = 0x7 },
1174b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x38 }, },
1175b07c59f7SDavid Gibson         },
1176b07c59f7SDavid Gibson         {
1177b07c59f7SDavid Gibson             .page_shift = 16, /* 64K */
1178b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_64K,
1179b07c59f7SDavid Gibson             .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1180b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x8 }, },
1181b07c59f7SDavid Gibson         },
1182b07c59f7SDavid Gibson         {
1183b07c59f7SDavid Gibson             .page_shift = 24, /* 16M */
1184b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16M,
1185b07c59f7SDavid Gibson             .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1186b07c59f7SDavid Gibson         },
1187b07c59f7SDavid Gibson         {
1188b07c59f7SDavid Gibson             .page_shift = 34, /* 16G */
1189b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16G,
1190b07c59f7SDavid Gibson             .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },
1191b07c59f7SDavid Gibson         },
1192b07c59f7SDavid Gibson     }
1193b07c59f7SDavid Gibson };
119427f00f0aSDavid Gibson 
119527f00f0aSDavid Gibson 
1196