xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision fcf5ef2ab52c621a4617ebbef36bf43b4003f4c0)
1*fcf5ef2aSThomas Huth /*
2*fcf5ef2aSThomas Huth  *  PowerPC MMU, TLB, SLB and BAT emulation helpers for QEMU.
3*fcf5ef2aSThomas Huth  *
4*fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2007 Jocelyn Mayer
5*fcf5ef2aSThomas Huth  *  Copyright (c) 2013 David Gibson, IBM Corporation
6*fcf5ef2aSThomas Huth  *
7*fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8*fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9*fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10*fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
11*fcf5ef2aSThomas Huth  *
12*fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13*fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14*fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16*fcf5ef2aSThomas Huth  *
17*fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18*fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19*fcf5ef2aSThomas Huth  */
20*fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21*fcf5ef2aSThomas Huth #include "qapi/error.h"
22*fcf5ef2aSThomas Huth #include "cpu.h"
23*fcf5ef2aSThomas Huth #include "exec/exec-all.h"
24*fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
25*fcf5ef2aSThomas Huth #include "qemu/error-report.h"
26*fcf5ef2aSThomas Huth #include "sysemu/kvm.h"
27*fcf5ef2aSThomas Huth #include "kvm_ppc.h"
28*fcf5ef2aSThomas Huth #include "mmu-hash64.h"
29*fcf5ef2aSThomas Huth #include "exec/log.h"
30*fcf5ef2aSThomas Huth 
31*fcf5ef2aSThomas Huth //#define DEBUG_SLB
32*fcf5ef2aSThomas Huth 
33*fcf5ef2aSThomas Huth #ifdef DEBUG_SLB
34*fcf5ef2aSThomas Huth #  define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
35*fcf5ef2aSThomas Huth #else
36*fcf5ef2aSThomas Huth #  define LOG_SLB(...) do { } while (0)
37*fcf5ef2aSThomas Huth #endif
38*fcf5ef2aSThomas Huth 
39*fcf5ef2aSThomas Huth /*
40*fcf5ef2aSThomas Huth  * Used to indicate that a CPU has its hash page table (HPT) managed
41*fcf5ef2aSThomas Huth  * within the host kernel
42*fcf5ef2aSThomas Huth  */
43*fcf5ef2aSThomas Huth #define MMU_HASH64_KVM_MANAGED_HPT      ((void *)-1)
44*fcf5ef2aSThomas Huth 
45*fcf5ef2aSThomas Huth /*
46*fcf5ef2aSThomas Huth  * SLB handling
47*fcf5ef2aSThomas Huth  */
48*fcf5ef2aSThomas Huth 
49*fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr)
50*fcf5ef2aSThomas Huth {
51*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
52*fcf5ef2aSThomas Huth     uint64_t esid_256M, esid_1T;
53*fcf5ef2aSThomas Huth     int n;
54*fcf5ef2aSThomas Huth 
55*fcf5ef2aSThomas Huth     LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr);
56*fcf5ef2aSThomas Huth 
57*fcf5ef2aSThomas Huth     esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V;
58*fcf5ef2aSThomas Huth     esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V;
59*fcf5ef2aSThomas Huth 
60*fcf5ef2aSThomas Huth     for (n = 0; n < env->slb_nr; n++) {
61*fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
62*fcf5ef2aSThomas Huth 
63*fcf5ef2aSThomas Huth         LOG_SLB("%s: slot %d %016" PRIx64 " %016"
64*fcf5ef2aSThomas Huth                     PRIx64 "\n", __func__, n, slb->esid, slb->vsid);
65*fcf5ef2aSThomas Huth         /* We check for 1T matches on all MMUs here - if the MMU
66*fcf5ef2aSThomas Huth          * doesn't have 1T segment support, we will have prevented 1T
67*fcf5ef2aSThomas Huth          * entries from being inserted in the slbmte code. */
68*fcf5ef2aSThomas Huth         if (((slb->esid == esid_256M) &&
69*fcf5ef2aSThomas Huth              ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M))
70*fcf5ef2aSThomas Huth             || ((slb->esid == esid_1T) &&
71*fcf5ef2aSThomas Huth                 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) {
72*fcf5ef2aSThomas Huth             return slb;
73*fcf5ef2aSThomas Huth         }
74*fcf5ef2aSThomas Huth     }
75*fcf5ef2aSThomas Huth 
76*fcf5ef2aSThomas Huth     return NULL;
77*fcf5ef2aSThomas Huth }
78*fcf5ef2aSThomas Huth 
79*fcf5ef2aSThomas Huth void dump_slb(FILE *f, fprintf_function cpu_fprintf, PowerPCCPU *cpu)
80*fcf5ef2aSThomas Huth {
81*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
82*fcf5ef2aSThomas Huth     int i;
83*fcf5ef2aSThomas Huth     uint64_t slbe, slbv;
84*fcf5ef2aSThomas Huth 
85*fcf5ef2aSThomas Huth     cpu_synchronize_state(CPU(cpu));
86*fcf5ef2aSThomas Huth 
87*fcf5ef2aSThomas Huth     cpu_fprintf(f, "SLB\tESID\t\t\tVSID\n");
88*fcf5ef2aSThomas Huth     for (i = 0; i < env->slb_nr; i++) {
89*fcf5ef2aSThomas Huth         slbe = env->slb[i].esid;
90*fcf5ef2aSThomas Huth         slbv = env->slb[i].vsid;
91*fcf5ef2aSThomas Huth         if (slbe == 0 && slbv == 0) {
92*fcf5ef2aSThomas Huth             continue;
93*fcf5ef2aSThomas Huth         }
94*fcf5ef2aSThomas Huth         cpu_fprintf(f, "%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n",
95*fcf5ef2aSThomas Huth                     i, slbe, slbv);
96*fcf5ef2aSThomas Huth     }
97*fcf5ef2aSThomas Huth }
98*fcf5ef2aSThomas Huth 
99*fcf5ef2aSThomas Huth void helper_slbia(CPUPPCState *env)
100*fcf5ef2aSThomas Huth {
101*fcf5ef2aSThomas Huth     int n;
102*fcf5ef2aSThomas Huth 
103*fcf5ef2aSThomas Huth     /* XXX: Warning: slbia never invalidates the first segment */
104*fcf5ef2aSThomas Huth     for (n = 1; n < env->slb_nr; n++) {
105*fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
106*fcf5ef2aSThomas Huth 
107*fcf5ef2aSThomas Huth         if (slb->esid & SLB_ESID_V) {
108*fcf5ef2aSThomas Huth             slb->esid &= ~SLB_ESID_V;
109*fcf5ef2aSThomas Huth             /* XXX: given the fact that segment size is 256 MB or 1TB,
110*fcf5ef2aSThomas Huth              *      and we still don't have a tlb_flush_mask(env, n, mask)
111*fcf5ef2aSThomas Huth              *      in QEMU, we just invalidate all TLBs
112*fcf5ef2aSThomas Huth              */
113*fcf5ef2aSThomas Huth             env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
114*fcf5ef2aSThomas Huth         }
115*fcf5ef2aSThomas Huth     }
116*fcf5ef2aSThomas Huth }
117*fcf5ef2aSThomas Huth 
118*fcf5ef2aSThomas Huth void helper_slbie(CPUPPCState *env, target_ulong addr)
119*fcf5ef2aSThomas Huth {
120*fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
121*fcf5ef2aSThomas Huth     ppc_slb_t *slb;
122*fcf5ef2aSThomas Huth 
123*fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, addr);
124*fcf5ef2aSThomas Huth     if (!slb) {
125*fcf5ef2aSThomas Huth         return;
126*fcf5ef2aSThomas Huth     }
127*fcf5ef2aSThomas Huth 
128*fcf5ef2aSThomas Huth     if (slb->esid & SLB_ESID_V) {
129*fcf5ef2aSThomas Huth         slb->esid &= ~SLB_ESID_V;
130*fcf5ef2aSThomas Huth 
131*fcf5ef2aSThomas Huth         /* XXX: given the fact that segment size is 256 MB or 1TB,
132*fcf5ef2aSThomas Huth          *      and we still don't have a tlb_flush_mask(env, n, mask)
133*fcf5ef2aSThomas Huth          *      in QEMU, we just invalidate all TLBs
134*fcf5ef2aSThomas Huth          */
135*fcf5ef2aSThomas Huth         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
136*fcf5ef2aSThomas Huth     }
137*fcf5ef2aSThomas Huth }
138*fcf5ef2aSThomas Huth 
139*fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot,
140*fcf5ef2aSThomas Huth                   target_ulong esid, target_ulong vsid)
141*fcf5ef2aSThomas Huth {
142*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
143*fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
144*fcf5ef2aSThomas Huth     const struct ppc_one_seg_page_size *sps = NULL;
145*fcf5ef2aSThomas Huth     int i;
146*fcf5ef2aSThomas Huth 
147*fcf5ef2aSThomas Huth     if (slot >= env->slb_nr) {
148*fcf5ef2aSThomas Huth         return -1; /* Bad slot number */
149*fcf5ef2aSThomas Huth     }
150*fcf5ef2aSThomas Huth     if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) {
151*fcf5ef2aSThomas Huth         return -1; /* Reserved bits set */
152*fcf5ef2aSThomas Huth     }
153*fcf5ef2aSThomas Huth     if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
154*fcf5ef2aSThomas Huth         return -1; /* Bad segment size */
155*fcf5ef2aSThomas Huth     }
156*fcf5ef2aSThomas Huth     if ((vsid & SLB_VSID_B) && !(env->mmu_model & POWERPC_MMU_1TSEG)) {
157*fcf5ef2aSThomas Huth         return -1; /* 1T segment on MMU that doesn't support it */
158*fcf5ef2aSThomas Huth     }
159*fcf5ef2aSThomas Huth 
160*fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
161*fcf5ef2aSThomas Huth         const struct ppc_one_seg_page_size *sps1 = &env->sps.sps[i];
162*fcf5ef2aSThomas Huth 
163*fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
164*fcf5ef2aSThomas Huth             break;
165*fcf5ef2aSThomas Huth         }
166*fcf5ef2aSThomas Huth 
167*fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
168*fcf5ef2aSThomas Huth             sps = sps1;
169*fcf5ef2aSThomas Huth             break;
170*fcf5ef2aSThomas Huth         }
171*fcf5ef2aSThomas Huth     }
172*fcf5ef2aSThomas Huth 
173*fcf5ef2aSThomas Huth     if (!sps) {
174*fcf5ef2aSThomas Huth         error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
175*fcf5ef2aSThomas Huth                      " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx,
176*fcf5ef2aSThomas Huth                      slot, esid, vsid);
177*fcf5ef2aSThomas Huth         return -1;
178*fcf5ef2aSThomas Huth     }
179*fcf5ef2aSThomas Huth 
180*fcf5ef2aSThomas Huth     slb->esid = esid;
181*fcf5ef2aSThomas Huth     slb->vsid = vsid;
182*fcf5ef2aSThomas Huth     slb->sps = sps;
183*fcf5ef2aSThomas Huth 
184*fcf5ef2aSThomas Huth     LOG_SLB("%s: %d " TARGET_FMT_lx " - " TARGET_FMT_lx " => %016" PRIx64
185*fcf5ef2aSThomas Huth             " %016" PRIx64 "\n", __func__, slot, esid, vsid,
186*fcf5ef2aSThomas Huth             slb->esid, slb->vsid);
187*fcf5ef2aSThomas Huth 
188*fcf5ef2aSThomas Huth     return 0;
189*fcf5ef2aSThomas Huth }
190*fcf5ef2aSThomas Huth 
191*fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb,
192*fcf5ef2aSThomas Huth                              target_ulong *rt)
193*fcf5ef2aSThomas Huth {
194*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
195*fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
196*fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
197*fcf5ef2aSThomas Huth 
198*fcf5ef2aSThomas Huth     if (slot >= env->slb_nr) {
199*fcf5ef2aSThomas Huth         return -1;
200*fcf5ef2aSThomas Huth     }
201*fcf5ef2aSThomas Huth 
202*fcf5ef2aSThomas Huth     *rt = slb->esid;
203*fcf5ef2aSThomas Huth     return 0;
204*fcf5ef2aSThomas Huth }
205*fcf5ef2aSThomas Huth 
206*fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
207*fcf5ef2aSThomas Huth                              target_ulong *rt)
208*fcf5ef2aSThomas Huth {
209*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
210*fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
211*fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
212*fcf5ef2aSThomas Huth 
213*fcf5ef2aSThomas Huth     if (slot >= env->slb_nr) {
214*fcf5ef2aSThomas Huth         return -1;
215*fcf5ef2aSThomas Huth     }
216*fcf5ef2aSThomas Huth 
217*fcf5ef2aSThomas Huth     *rt = slb->vsid;
218*fcf5ef2aSThomas Huth     return 0;
219*fcf5ef2aSThomas Huth }
220*fcf5ef2aSThomas Huth 
221*fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
222*fcf5ef2aSThomas Huth                              target_ulong *rt)
223*fcf5ef2aSThomas Huth {
224*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
225*fcf5ef2aSThomas Huth     ppc_slb_t *slb;
226*fcf5ef2aSThomas Huth 
227*fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, env->msr)) {
228*fcf5ef2aSThomas Huth         rb &= 0xffffffff;
229*fcf5ef2aSThomas Huth     }
230*fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, rb);
231*fcf5ef2aSThomas Huth     if (slb == NULL) {
232*fcf5ef2aSThomas Huth         *rt = (target_ulong)-1ul;
233*fcf5ef2aSThomas Huth     } else {
234*fcf5ef2aSThomas Huth         *rt = slb->vsid;
235*fcf5ef2aSThomas Huth     }
236*fcf5ef2aSThomas Huth     return 0;
237*fcf5ef2aSThomas Huth }
238*fcf5ef2aSThomas Huth 
239*fcf5ef2aSThomas Huth void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs)
240*fcf5ef2aSThomas Huth {
241*fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
242*fcf5ef2aSThomas Huth 
243*fcf5ef2aSThomas Huth     if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) {
244*fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
245*fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
246*fcf5ef2aSThomas Huth     }
247*fcf5ef2aSThomas Huth }
248*fcf5ef2aSThomas Huth 
249*fcf5ef2aSThomas Huth target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb)
250*fcf5ef2aSThomas Huth {
251*fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
252*fcf5ef2aSThomas Huth     target_ulong rt = 0;
253*fcf5ef2aSThomas Huth 
254*fcf5ef2aSThomas Huth     if (ppc_load_slb_esid(cpu, rb, &rt) < 0) {
255*fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
256*fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
257*fcf5ef2aSThomas Huth     }
258*fcf5ef2aSThomas Huth     return rt;
259*fcf5ef2aSThomas Huth }
260*fcf5ef2aSThomas Huth 
261*fcf5ef2aSThomas Huth target_ulong helper_find_slb_vsid(CPUPPCState *env, target_ulong rb)
262*fcf5ef2aSThomas Huth {
263*fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
264*fcf5ef2aSThomas Huth     target_ulong rt = 0;
265*fcf5ef2aSThomas Huth 
266*fcf5ef2aSThomas Huth     if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) {
267*fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
268*fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
269*fcf5ef2aSThomas Huth     }
270*fcf5ef2aSThomas Huth     return rt;
271*fcf5ef2aSThomas Huth }
272*fcf5ef2aSThomas Huth 
273*fcf5ef2aSThomas Huth target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb)
274*fcf5ef2aSThomas Huth {
275*fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
276*fcf5ef2aSThomas Huth     target_ulong rt = 0;
277*fcf5ef2aSThomas Huth 
278*fcf5ef2aSThomas Huth     if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) {
279*fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
280*fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
281*fcf5ef2aSThomas Huth     }
282*fcf5ef2aSThomas Huth     return rt;
283*fcf5ef2aSThomas Huth }
284*fcf5ef2aSThomas Huth 
285*fcf5ef2aSThomas Huth /*
286*fcf5ef2aSThomas Huth  * 64-bit hash table MMU handling
287*fcf5ef2aSThomas Huth  */
288*fcf5ef2aSThomas Huth void ppc_hash64_set_sdr1(PowerPCCPU *cpu, target_ulong value,
289*fcf5ef2aSThomas Huth                          Error **errp)
290*fcf5ef2aSThomas Huth {
291*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
292*fcf5ef2aSThomas Huth     target_ulong htabsize = value & SDR_64_HTABSIZE;
293*fcf5ef2aSThomas Huth 
294*fcf5ef2aSThomas Huth     env->spr[SPR_SDR1] = value;
295*fcf5ef2aSThomas Huth     if (htabsize > 28) {
296*fcf5ef2aSThomas Huth         error_setg(errp,
297*fcf5ef2aSThomas Huth                    "Invalid HTABSIZE 0x" TARGET_FMT_lx" stored in SDR1",
298*fcf5ef2aSThomas Huth                    htabsize);
299*fcf5ef2aSThomas Huth         htabsize = 28;
300*fcf5ef2aSThomas Huth     }
301*fcf5ef2aSThomas Huth     env->htab_mask = (1ULL << (htabsize + 18 - 7)) - 1;
302*fcf5ef2aSThomas Huth     env->htab_base = value & SDR_64_HTABORG;
303*fcf5ef2aSThomas Huth }
304*fcf5ef2aSThomas Huth 
305*fcf5ef2aSThomas Huth void ppc_hash64_set_external_hpt(PowerPCCPU *cpu, void *hpt, int shift,
306*fcf5ef2aSThomas Huth                                  Error **errp)
307*fcf5ef2aSThomas Huth {
308*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
309*fcf5ef2aSThomas Huth     Error *local_err = NULL;
310*fcf5ef2aSThomas Huth 
311*fcf5ef2aSThomas Huth     if (hpt) {
312*fcf5ef2aSThomas Huth         env->external_htab = hpt;
313*fcf5ef2aSThomas Huth     } else {
314*fcf5ef2aSThomas Huth         env->external_htab = MMU_HASH64_KVM_MANAGED_HPT;
315*fcf5ef2aSThomas Huth     }
316*fcf5ef2aSThomas Huth     ppc_hash64_set_sdr1(cpu, (target_ulong)(uintptr_t)hpt | (shift - 18),
317*fcf5ef2aSThomas Huth                         &local_err);
318*fcf5ef2aSThomas Huth     if (local_err) {
319*fcf5ef2aSThomas Huth         error_propagate(errp, local_err);
320*fcf5ef2aSThomas Huth         return;
321*fcf5ef2aSThomas Huth     }
322*fcf5ef2aSThomas Huth 
323*fcf5ef2aSThomas Huth     /* Not strictly necessary, but makes it clearer that an external
324*fcf5ef2aSThomas Huth      * htab is in use when debugging */
325*fcf5ef2aSThomas Huth     env->htab_base = -1;
326*fcf5ef2aSThomas Huth 
327*fcf5ef2aSThomas Huth     if (kvm_enabled()) {
328*fcf5ef2aSThomas Huth         if (kvmppc_put_books_sregs(cpu) < 0) {
329*fcf5ef2aSThomas Huth             error_setg(errp, "Unable to update SDR1 in KVM");
330*fcf5ef2aSThomas Huth         }
331*fcf5ef2aSThomas Huth     }
332*fcf5ef2aSThomas Huth }
333*fcf5ef2aSThomas Huth 
334*fcf5ef2aSThomas Huth static int ppc_hash64_pte_prot(PowerPCCPU *cpu,
335*fcf5ef2aSThomas Huth                                ppc_slb_t *slb, ppc_hash_pte64_t pte)
336*fcf5ef2aSThomas Huth {
337*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
338*fcf5ef2aSThomas Huth     unsigned pp, key;
339*fcf5ef2aSThomas Huth     /* Some pp bit combinations have undefined behaviour, so default
340*fcf5ef2aSThomas Huth      * to no access in those cases */
341*fcf5ef2aSThomas Huth     int prot = 0;
342*fcf5ef2aSThomas Huth 
343*fcf5ef2aSThomas Huth     key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP)
344*fcf5ef2aSThomas Huth              : (slb->vsid & SLB_VSID_KS));
345*fcf5ef2aSThomas Huth     pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
346*fcf5ef2aSThomas Huth 
347*fcf5ef2aSThomas Huth     if (key == 0) {
348*fcf5ef2aSThomas Huth         switch (pp) {
349*fcf5ef2aSThomas Huth         case 0x0:
350*fcf5ef2aSThomas Huth         case 0x1:
351*fcf5ef2aSThomas Huth         case 0x2:
352*fcf5ef2aSThomas Huth             prot = PAGE_READ | PAGE_WRITE;
353*fcf5ef2aSThomas Huth             break;
354*fcf5ef2aSThomas Huth 
355*fcf5ef2aSThomas Huth         case 0x3:
356*fcf5ef2aSThomas Huth         case 0x6:
357*fcf5ef2aSThomas Huth             prot = PAGE_READ;
358*fcf5ef2aSThomas Huth             break;
359*fcf5ef2aSThomas Huth         }
360*fcf5ef2aSThomas Huth     } else {
361*fcf5ef2aSThomas Huth         switch (pp) {
362*fcf5ef2aSThomas Huth         case 0x0:
363*fcf5ef2aSThomas Huth         case 0x6:
364*fcf5ef2aSThomas Huth             prot = 0;
365*fcf5ef2aSThomas Huth             break;
366*fcf5ef2aSThomas Huth 
367*fcf5ef2aSThomas Huth         case 0x1:
368*fcf5ef2aSThomas Huth         case 0x3:
369*fcf5ef2aSThomas Huth             prot = PAGE_READ;
370*fcf5ef2aSThomas Huth             break;
371*fcf5ef2aSThomas Huth 
372*fcf5ef2aSThomas Huth         case 0x2:
373*fcf5ef2aSThomas Huth             prot = PAGE_READ | PAGE_WRITE;
374*fcf5ef2aSThomas Huth             break;
375*fcf5ef2aSThomas Huth         }
376*fcf5ef2aSThomas Huth     }
377*fcf5ef2aSThomas Huth 
378*fcf5ef2aSThomas Huth     /* No execute if either noexec or guarded bits set */
379*fcf5ef2aSThomas Huth     if (!(pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G)
380*fcf5ef2aSThomas Huth         || (slb->vsid & SLB_VSID_N)) {
381*fcf5ef2aSThomas Huth         prot |= PAGE_EXEC;
382*fcf5ef2aSThomas Huth     }
383*fcf5ef2aSThomas Huth 
384*fcf5ef2aSThomas Huth     return prot;
385*fcf5ef2aSThomas Huth }
386*fcf5ef2aSThomas Huth 
387*fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte)
388*fcf5ef2aSThomas Huth {
389*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
390*fcf5ef2aSThomas Huth     int key, amrbits;
391*fcf5ef2aSThomas Huth     int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
392*fcf5ef2aSThomas Huth 
393*fcf5ef2aSThomas Huth     /* Only recent MMUs implement Virtual Page Class Key Protection */
394*fcf5ef2aSThomas Huth     if (!(env->mmu_model & POWERPC_MMU_AMR)) {
395*fcf5ef2aSThomas Huth         return prot;
396*fcf5ef2aSThomas Huth     }
397*fcf5ef2aSThomas Huth 
398*fcf5ef2aSThomas Huth     key = HPTE64_R_KEY(pte.pte1);
399*fcf5ef2aSThomas Huth     amrbits = (env->spr[SPR_AMR] >> 2*(31 - key)) & 0x3;
400*fcf5ef2aSThomas Huth 
401*fcf5ef2aSThomas Huth     /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
402*fcf5ef2aSThomas Huth     /*         env->spr[SPR_AMR]); */
403*fcf5ef2aSThomas Huth 
404*fcf5ef2aSThomas Huth     /*
405*fcf5ef2aSThomas Huth      * A store is permitted if the AMR bit is 0. Remove write
406*fcf5ef2aSThomas Huth      * protection if it is set.
407*fcf5ef2aSThomas Huth      */
408*fcf5ef2aSThomas Huth     if (amrbits & 0x2) {
409*fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
410*fcf5ef2aSThomas Huth     }
411*fcf5ef2aSThomas Huth     /*
412*fcf5ef2aSThomas Huth      * A load is permitted if the AMR bit is 0. Remove read
413*fcf5ef2aSThomas Huth      * protection if it is set.
414*fcf5ef2aSThomas Huth      */
415*fcf5ef2aSThomas Huth     if (amrbits & 0x1) {
416*fcf5ef2aSThomas Huth         prot &= ~PAGE_READ;
417*fcf5ef2aSThomas Huth     }
418*fcf5ef2aSThomas Huth 
419*fcf5ef2aSThomas Huth     return prot;
420*fcf5ef2aSThomas Huth }
421*fcf5ef2aSThomas Huth 
422*fcf5ef2aSThomas Huth uint64_t ppc_hash64_start_access(PowerPCCPU *cpu, target_ulong pte_index)
423*fcf5ef2aSThomas Huth {
424*fcf5ef2aSThomas Huth     uint64_t token = 0;
425*fcf5ef2aSThomas Huth     hwaddr pte_offset;
426*fcf5ef2aSThomas Huth 
427*fcf5ef2aSThomas Huth     pte_offset = pte_index * HASH_PTE_SIZE_64;
428*fcf5ef2aSThomas Huth     if (cpu->env.external_htab == MMU_HASH64_KVM_MANAGED_HPT) {
429*fcf5ef2aSThomas Huth         /*
430*fcf5ef2aSThomas Huth          * HTAB is controlled by KVM. Fetch the PTEG into a new buffer.
431*fcf5ef2aSThomas Huth          */
432*fcf5ef2aSThomas Huth         token = kvmppc_hash64_read_pteg(cpu, pte_index);
433*fcf5ef2aSThomas Huth     } else if (cpu->env.external_htab) {
434*fcf5ef2aSThomas Huth         /*
435*fcf5ef2aSThomas Huth          * HTAB is controlled by QEMU. Just point to the internally
436*fcf5ef2aSThomas Huth          * accessible PTEG.
437*fcf5ef2aSThomas Huth          */
438*fcf5ef2aSThomas Huth         token = (uint64_t)(uintptr_t) cpu->env.external_htab + pte_offset;
439*fcf5ef2aSThomas Huth     } else if (cpu->env.htab_base) {
440*fcf5ef2aSThomas Huth         token = cpu->env.htab_base + pte_offset;
441*fcf5ef2aSThomas Huth     }
442*fcf5ef2aSThomas Huth     return token;
443*fcf5ef2aSThomas Huth }
444*fcf5ef2aSThomas Huth 
445*fcf5ef2aSThomas Huth void ppc_hash64_stop_access(PowerPCCPU *cpu, uint64_t token)
446*fcf5ef2aSThomas Huth {
447*fcf5ef2aSThomas Huth     if (cpu->env.external_htab == MMU_HASH64_KVM_MANAGED_HPT) {
448*fcf5ef2aSThomas Huth         kvmppc_hash64_free_pteg(token);
449*fcf5ef2aSThomas Huth     }
450*fcf5ef2aSThomas Huth }
451*fcf5ef2aSThomas Huth 
452*fcf5ef2aSThomas Huth static unsigned hpte_page_shift(const struct ppc_one_seg_page_size *sps,
453*fcf5ef2aSThomas Huth     uint64_t pte0, uint64_t pte1)
454*fcf5ef2aSThomas Huth {
455*fcf5ef2aSThomas Huth     int i;
456*fcf5ef2aSThomas Huth 
457*fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
458*fcf5ef2aSThomas Huth         if (sps->page_shift != 12) {
459*fcf5ef2aSThomas Huth             /* 4kiB page in a non 4kiB segment */
460*fcf5ef2aSThomas Huth             return 0;
461*fcf5ef2aSThomas Huth         }
462*fcf5ef2aSThomas Huth         /* Normal 4kiB page */
463*fcf5ef2aSThomas Huth         return 12;
464*fcf5ef2aSThomas Huth     }
465*fcf5ef2aSThomas Huth 
466*fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
467*fcf5ef2aSThomas Huth         const struct ppc_one_page_size *ps = &sps->enc[i];
468*fcf5ef2aSThomas Huth         uint64_t mask;
469*fcf5ef2aSThomas Huth 
470*fcf5ef2aSThomas Huth         if (!ps->page_shift) {
471*fcf5ef2aSThomas Huth             break;
472*fcf5ef2aSThomas Huth         }
473*fcf5ef2aSThomas Huth 
474*fcf5ef2aSThomas Huth         if (ps->page_shift == 12) {
475*fcf5ef2aSThomas Huth             /* L bit is set so this can't be a 4kiB page */
476*fcf5ef2aSThomas Huth             continue;
477*fcf5ef2aSThomas Huth         }
478*fcf5ef2aSThomas Huth 
479*fcf5ef2aSThomas Huth         mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
480*fcf5ef2aSThomas Huth 
481*fcf5ef2aSThomas Huth         if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
482*fcf5ef2aSThomas Huth             return ps->page_shift;
483*fcf5ef2aSThomas Huth         }
484*fcf5ef2aSThomas Huth     }
485*fcf5ef2aSThomas Huth 
486*fcf5ef2aSThomas Huth     return 0; /* Bad page size encoding */
487*fcf5ef2aSThomas Huth }
488*fcf5ef2aSThomas Huth 
489*fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
490*fcf5ef2aSThomas Huth                                      const struct ppc_one_seg_page_size *sps,
491*fcf5ef2aSThomas Huth                                      target_ulong ptem,
492*fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
493*fcf5ef2aSThomas Huth {
494*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
495*fcf5ef2aSThomas Huth     int i;
496*fcf5ef2aSThomas Huth     uint64_t token;
497*fcf5ef2aSThomas Huth     target_ulong pte0, pte1;
498*fcf5ef2aSThomas Huth     target_ulong pte_index;
499*fcf5ef2aSThomas Huth 
500*fcf5ef2aSThomas Huth     pte_index = (hash & env->htab_mask) * HPTES_PER_GROUP;
501*fcf5ef2aSThomas Huth     token = ppc_hash64_start_access(cpu, pte_index);
502*fcf5ef2aSThomas Huth     if (!token) {
503*fcf5ef2aSThomas Huth         return -1;
504*fcf5ef2aSThomas Huth     }
505*fcf5ef2aSThomas Huth     for (i = 0; i < HPTES_PER_GROUP; i++) {
506*fcf5ef2aSThomas Huth         pte0 = ppc_hash64_load_hpte0(cpu, token, i);
507*fcf5ef2aSThomas Huth         pte1 = ppc_hash64_load_hpte1(cpu, token, i);
508*fcf5ef2aSThomas Huth 
509*fcf5ef2aSThomas Huth         /* This compares V, B, H (secondary) and the AVPN */
510*fcf5ef2aSThomas Huth         if (HPTE64_V_COMPARE(pte0, ptem)) {
511*fcf5ef2aSThomas Huth             *pshift = hpte_page_shift(sps, pte0, pte1);
512*fcf5ef2aSThomas Huth             /*
513*fcf5ef2aSThomas Huth              * If there is no match, ignore the PTE, it could simply
514*fcf5ef2aSThomas Huth              * be for a different segment size encoding and the
515*fcf5ef2aSThomas Huth              * architecture specifies we should not match. Linux will
516*fcf5ef2aSThomas Huth              * potentially leave behind PTEs for the wrong base page
517*fcf5ef2aSThomas Huth              * size when demoting segments.
518*fcf5ef2aSThomas Huth              */
519*fcf5ef2aSThomas Huth             if (*pshift == 0) {
520*fcf5ef2aSThomas Huth                 continue;
521*fcf5ef2aSThomas Huth             }
522*fcf5ef2aSThomas Huth             /* We don't do anything with pshift yet as qemu TLB only deals
523*fcf5ef2aSThomas Huth              * with 4K pages anyway
524*fcf5ef2aSThomas Huth              */
525*fcf5ef2aSThomas Huth             pte->pte0 = pte0;
526*fcf5ef2aSThomas Huth             pte->pte1 = pte1;
527*fcf5ef2aSThomas Huth             ppc_hash64_stop_access(cpu, token);
528*fcf5ef2aSThomas Huth             return (pte_index + i) * HASH_PTE_SIZE_64;
529*fcf5ef2aSThomas Huth         }
530*fcf5ef2aSThomas Huth     }
531*fcf5ef2aSThomas Huth     ppc_hash64_stop_access(cpu, token);
532*fcf5ef2aSThomas Huth     /*
533*fcf5ef2aSThomas Huth      * We didn't find a valid entry.
534*fcf5ef2aSThomas Huth      */
535*fcf5ef2aSThomas Huth     return -1;
536*fcf5ef2aSThomas Huth }
537*fcf5ef2aSThomas Huth 
538*fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
539*fcf5ef2aSThomas Huth                                      ppc_slb_t *slb, target_ulong eaddr,
540*fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
541*fcf5ef2aSThomas Huth {
542*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
543*fcf5ef2aSThomas Huth     hwaddr pte_offset;
544*fcf5ef2aSThomas Huth     hwaddr hash;
545*fcf5ef2aSThomas Huth     uint64_t vsid, epnmask, epn, ptem;
546*fcf5ef2aSThomas Huth     const struct ppc_one_seg_page_size *sps = slb->sps;
547*fcf5ef2aSThomas Huth 
548*fcf5ef2aSThomas Huth     /* The SLB store path should prevent any bad page size encodings
549*fcf5ef2aSThomas Huth      * getting in there, so: */
550*fcf5ef2aSThomas Huth     assert(sps);
551*fcf5ef2aSThomas Huth 
552*fcf5ef2aSThomas Huth     /* If ISL is set in LPCR we need to clamp the page size to 4K */
553*fcf5ef2aSThomas Huth     if (env->spr[SPR_LPCR] & LPCR_ISL) {
554*fcf5ef2aSThomas Huth         /* We assume that when using TCG, 4k is first entry of SPS */
555*fcf5ef2aSThomas Huth         sps = &env->sps.sps[0];
556*fcf5ef2aSThomas Huth         assert(sps->page_shift == 12);
557*fcf5ef2aSThomas Huth     }
558*fcf5ef2aSThomas Huth 
559*fcf5ef2aSThomas Huth     epnmask = ~((1ULL << sps->page_shift) - 1);
560*fcf5ef2aSThomas Huth 
561*fcf5ef2aSThomas Huth     if (slb->vsid & SLB_VSID_B) {
562*fcf5ef2aSThomas Huth         /* 1TB segment */
563*fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
564*fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
565*fcf5ef2aSThomas Huth         hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
566*fcf5ef2aSThomas Huth     } else {
567*fcf5ef2aSThomas Huth         /* 256M segment */
568*fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
569*fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
570*fcf5ef2aSThomas Huth         hash = vsid ^ (epn >> sps->page_shift);
571*fcf5ef2aSThomas Huth     }
572*fcf5ef2aSThomas Huth     ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
573*fcf5ef2aSThomas Huth     ptem |= HPTE64_V_VALID;
574*fcf5ef2aSThomas Huth 
575*fcf5ef2aSThomas Huth     /* Page address translation */
576*fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
577*fcf5ef2aSThomas Huth             "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
578*fcf5ef2aSThomas Huth             " hash " TARGET_FMT_plx "\n",
579*fcf5ef2aSThomas Huth             env->htab_base, env->htab_mask, hash);
580*fcf5ef2aSThomas Huth 
581*fcf5ef2aSThomas Huth     /* Primary PTEG lookup */
582*fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
583*fcf5ef2aSThomas Huth             "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
584*fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
585*fcf5ef2aSThomas Huth             " hash=" TARGET_FMT_plx "\n",
586*fcf5ef2aSThomas Huth             env->htab_base, env->htab_mask, vsid, ptem,  hash);
587*fcf5ef2aSThomas Huth     pte_offset = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
588*fcf5ef2aSThomas Huth 
589*fcf5ef2aSThomas Huth     if (pte_offset == -1) {
590*fcf5ef2aSThomas Huth         /* Secondary PTEG lookup */
591*fcf5ef2aSThomas Huth         ptem |= HPTE64_V_SECONDARY;
592*fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU,
593*fcf5ef2aSThomas Huth                 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
594*fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
595*fcf5ef2aSThomas Huth                 " hash=" TARGET_FMT_plx "\n", env->htab_base,
596*fcf5ef2aSThomas Huth                 env->htab_mask, vsid, ptem, ~hash);
597*fcf5ef2aSThomas Huth 
598*fcf5ef2aSThomas Huth         pte_offset = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
599*fcf5ef2aSThomas Huth     }
600*fcf5ef2aSThomas Huth 
601*fcf5ef2aSThomas Huth     return pte_offset;
602*fcf5ef2aSThomas Huth }
603*fcf5ef2aSThomas Huth 
604*fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
605*fcf5ef2aSThomas Huth                                           uint64_t pte0, uint64_t pte1)
606*fcf5ef2aSThomas Huth {
607*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
608*fcf5ef2aSThomas Huth     int i;
609*fcf5ef2aSThomas Huth 
610*fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
611*fcf5ef2aSThomas Huth         return 12;
612*fcf5ef2aSThomas Huth     }
613*fcf5ef2aSThomas Huth 
614*fcf5ef2aSThomas Huth     /*
615*fcf5ef2aSThomas Huth      * The encodings in env->sps need to be carefully chosen so that
616*fcf5ef2aSThomas Huth      * this gives an unambiguous result.
617*fcf5ef2aSThomas Huth      */
618*fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
619*fcf5ef2aSThomas Huth         const struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
620*fcf5ef2aSThomas Huth         unsigned shift;
621*fcf5ef2aSThomas Huth 
622*fcf5ef2aSThomas Huth         if (!sps->page_shift) {
623*fcf5ef2aSThomas Huth             break;
624*fcf5ef2aSThomas Huth         }
625*fcf5ef2aSThomas Huth 
626*fcf5ef2aSThomas Huth         shift = hpte_page_shift(sps, pte0, pte1);
627*fcf5ef2aSThomas Huth         if (shift) {
628*fcf5ef2aSThomas Huth             return shift;
629*fcf5ef2aSThomas Huth         }
630*fcf5ef2aSThomas Huth     }
631*fcf5ef2aSThomas Huth 
632*fcf5ef2aSThomas Huth     return 0;
633*fcf5ef2aSThomas Huth }
634*fcf5ef2aSThomas Huth 
635*fcf5ef2aSThomas Huth static void ppc_hash64_set_isi(CPUState *cs, CPUPPCState *env,
636*fcf5ef2aSThomas Huth                                uint64_t error_code)
637*fcf5ef2aSThomas Huth {
638*fcf5ef2aSThomas Huth     bool vpm;
639*fcf5ef2aSThomas Huth 
640*fcf5ef2aSThomas Huth     if (msr_ir) {
641*fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
642*fcf5ef2aSThomas Huth     } else {
643*fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM0);
644*fcf5ef2aSThomas Huth     }
645*fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
646*fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HISI;
647*fcf5ef2aSThomas Huth     } else {
648*fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
649*fcf5ef2aSThomas Huth     }
650*fcf5ef2aSThomas Huth     env->error_code = error_code;
651*fcf5ef2aSThomas Huth }
652*fcf5ef2aSThomas Huth 
653*fcf5ef2aSThomas Huth static void ppc_hash64_set_dsi(CPUState *cs, CPUPPCState *env, uint64_t dar,
654*fcf5ef2aSThomas Huth                                uint64_t dsisr)
655*fcf5ef2aSThomas Huth {
656*fcf5ef2aSThomas Huth     bool vpm;
657*fcf5ef2aSThomas Huth 
658*fcf5ef2aSThomas Huth     if (msr_dr) {
659*fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
660*fcf5ef2aSThomas Huth     } else {
661*fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM0);
662*fcf5ef2aSThomas Huth     }
663*fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
664*fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
665*fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
666*fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
667*fcf5ef2aSThomas Huth     } else {
668*fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
669*fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
670*fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
671*fcf5ef2aSThomas Huth    }
672*fcf5ef2aSThomas Huth     env->error_code = 0;
673*fcf5ef2aSThomas Huth }
674*fcf5ef2aSThomas Huth 
675*fcf5ef2aSThomas Huth 
676*fcf5ef2aSThomas Huth int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr,
677*fcf5ef2aSThomas Huth                                 int rwx, int mmu_idx)
678*fcf5ef2aSThomas Huth {
679*fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
680*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
681*fcf5ef2aSThomas Huth     ppc_slb_t *slb;
682*fcf5ef2aSThomas Huth     unsigned apshift;
683*fcf5ef2aSThomas Huth     hwaddr pte_offset;
684*fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
685*fcf5ef2aSThomas Huth     int pp_prot, amr_prot, prot;
686*fcf5ef2aSThomas Huth     uint64_t new_pte1, dsisr;
687*fcf5ef2aSThomas Huth     const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC};
688*fcf5ef2aSThomas Huth     hwaddr raddr;
689*fcf5ef2aSThomas Huth 
690*fcf5ef2aSThomas Huth     assert((rwx == 0) || (rwx == 1) || (rwx == 2));
691*fcf5ef2aSThomas Huth 
692*fcf5ef2aSThomas Huth     /* Note on LPCR usage: 970 uses HID4, but our special variant
693*fcf5ef2aSThomas Huth      * of store_spr copies relevant fields into env->spr[SPR_LPCR].
694*fcf5ef2aSThomas Huth      * Similarily we filter unimplemented bits when storing into
695*fcf5ef2aSThomas Huth      * LPCR depending on the MMU version. This code can thus just
696*fcf5ef2aSThomas Huth      * use the LPCR "as-is".
697*fcf5ef2aSThomas Huth      */
698*fcf5ef2aSThomas Huth 
699*fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
700*fcf5ef2aSThomas Huth     if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) {
701*fcf5ef2aSThomas Huth         /* Translation is supposedly "off"  */
702*fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are (mostly) ignored */
703*fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
704*fcf5ef2aSThomas Huth 
705*fcf5ef2aSThomas Huth         /* In HV mode, add HRMOR if top EA bit is clear */
706*fcf5ef2aSThomas Huth         if (msr_hv || !env->has_hv_mode) {
707*fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
708*fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
709*fcf5ef2aSThomas Huth             }
710*fcf5ef2aSThomas Huth         } else {
711*fcf5ef2aSThomas Huth             /* Otherwise, check VPM for RMA vs VRMA */
712*fcf5ef2aSThomas Huth             if (env->spr[SPR_LPCR] & LPCR_VPM0) {
713*fcf5ef2aSThomas Huth                 slb = &env->vrma_slb;
714*fcf5ef2aSThomas Huth                 if (slb->sps) {
715*fcf5ef2aSThomas Huth                     goto skip_slb_search;
716*fcf5ef2aSThomas Huth                 }
717*fcf5ef2aSThomas Huth                 /* Not much else to do here */
718*fcf5ef2aSThomas Huth                 cs->exception_index = POWERPC_EXCP_MCHECK;
719*fcf5ef2aSThomas Huth                 env->error_code = 0;
720*fcf5ef2aSThomas Huth                 return 1;
721*fcf5ef2aSThomas Huth             } else if (raddr < env->rmls) {
722*fcf5ef2aSThomas Huth                 /* RMA. Check bounds in RMLS */
723*fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_RMOR];
724*fcf5ef2aSThomas Huth             } else {
725*fcf5ef2aSThomas Huth                 /* The access failed, generate the approriate interrupt */
726*fcf5ef2aSThomas Huth                 if (rwx == 2) {
727*fcf5ef2aSThomas Huth                     ppc_hash64_set_isi(cs, env, 0x08000000);
728*fcf5ef2aSThomas Huth                 } else {
729*fcf5ef2aSThomas Huth                     dsisr = 0x08000000;
730*fcf5ef2aSThomas Huth                     if (rwx == 1) {
731*fcf5ef2aSThomas Huth                         dsisr |= 0x02000000;
732*fcf5ef2aSThomas Huth                     }
733*fcf5ef2aSThomas Huth                     ppc_hash64_set_dsi(cs, env, eaddr, dsisr);
734*fcf5ef2aSThomas Huth                 }
735*fcf5ef2aSThomas Huth                 return 1;
736*fcf5ef2aSThomas Huth             }
737*fcf5ef2aSThomas Huth         }
738*fcf5ef2aSThomas Huth         tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
739*fcf5ef2aSThomas Huth                      PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx,
740*fcf5ef2aSThomas Huth                      TARGET_PAGE_SIZE);
741*fcf5ef2aSThomas Huth         return 0;
742*fcf5ef2aSThomas Huth     }
743*fcf5ef2aSThomas Huth 
744*fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
745*fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
746*fcf5ef2aSThomas Huth     if (!slb) {
747*fcf5ef2aSThomas Huth         if (rwx == 2) {
748*fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
749*fcf5ef2aSThomas Huth             env->error_code = 0;
750*fcf5ef2aSThomas Huth         } else {
751*fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
752*fcf5ef2aSThomas Huth             env->error_code = 0;
753*fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
754*fcf5ef2aSThomas Huth         }
755*fcf5ef2aSThomas Huth         return 1;
756*fcf5ef2aSThomas Huth     }
757*fcf5ef2aSThomas Huth 
758*fcf5ef2aSThomas Huth skip_slb_search:
759*fcf5ef2aSThomas Huth 
760*fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
761*fcf5ef2aSThomas Huth     if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) {
762*fcf5ef2aSThomas Huth         ppc_hash64_set_isi(cs, env, 0x10000000);
763*fcf5ef2aSThomas Huth         return 1;
764*fcf5ef2aSThomas Huth     }
765*fcf5ef2aSThomas Huth 
766*fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
767*fcf5ef2aSThomas Huth     pte_offset = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
768*fcf5ef2aSThomas Huth     if (pte_offset == -1) {
769*fcf5ef2aSThomas Huth         dsisr = 0x40000000;
770*fcf5ef2aSThomas Huth         if (rwx == 2) {
771*fcf5ef2aSThomas Huth             ppc_hash64_set_isi(cs, env, dsisr);
772*fcf5ef2aSThomas Huth         } else {
773*fcf5ef2aSThomas Huth             if (rwx == 1) {
774*fcf5ef2aSThomas Huth                 dsisr |= 0x02000000;
775*fcf5ef2aSThomas Huth             }
776*fcf5ef2aSThomas Huth             ppc_hash64_set_dsi(cs, env, eaddr, dsisr);
777*fcf5ef2aSThomas Huth         }
778*fcf5ef2aSThomas Huth         return 1;
779*fcf5ef2aSThomas Huth     }
780*fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
781*fcf5ef2aSThomas Huth                 "found PTE at offset %08" HWADDR_PRIx "\n", pte_offset);
782*fcf5ef2aSThomas Huth 
783*fcf5ef2aSThomas Huth     /* 5. Check access permissions */
784*fcf5ef2aSThomas Huth 
785*fcf5ef2aSThomas Huth     pp_prot = ppc_hash64_pte_prot(cpu, slb, pte);
786*fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
787*fcf5ef2aSThomas Huth     prot = pp_prot & amr_prot;
788*fcf5ef2aSThomas Huth 
789*fcf5ef2aSThomas Huth     if ((need_prot[rwx] & ~prot) != 0) {
790*fcf5ef2aSThomas Huth         /* Access right violation */
791*fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
792*fcf5ef2aSThomas Huth         if (rwx == 2) {
793*fcf5ef2aSThomas Huth             ppc_hash64_set_isi(cs, env, 0x08000000);
794*fcf5ef2aSThomas Huth         } else {
795*fcf5ef2aSThomas Huth             dsisr = 0;
796*fcf5ef2aSThomas Huth             if (need_prot[rwx] & ~pp_prot) {
797*fcf5ef2aSThomas Huth                 dsisr |= 0x08000000;
798*fcf5ef2aSThomas Huth             }
799*fcf5ef2aSThomas Huth             if (rwx == 1) {
800*fcf5ef2aSThomas Huth                 dsisr |= 0x02000000;
801*fcf5ef2aSThomas Huth             }
802*fcf5ef2aSThomas Huth             if (need_prot[rwx] & ~amr_prot) {
803*fcf5ef2aSThomas Huth                 dsisr |= 0x00200000;
804*fcf5ef2aSThomas Huth             }
805*fcf5ef2aSThomas Huth             ppc_hash64_set_dsi(cs, env, eaddr, dsisr);
806*fcf5ef2aSThomas Huth         }
807*fcf5ef2aSThomas Huth         return 1;
808*fcf5ef2aSThomas Huth     }
809*fcf5ef2aSThomas Huth 
810*fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
811*fcf5ef2aSThomas Huth 
812*fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
813*fcf5ef2aSThomas Huth 
814*fcf5ef2aSThomas Huth     new_pte1 = pte.pte1 | HPTE64_R_R; /* set referenced bit */
815*fcf5ef2aSThomas Huth     if (rwx == 1) {
816*fcf5ef2aSThomas Huth         new_pte1 |= HPTE64_R_C; /* set changed (dirty) bit */
817*fcf5ef2aSThomas Huth     } else {
818*fcf5ef2aSThomas Huth         /* Treat the page as read-only for now, so that a later write
819*fcf5ef2aSThomas Huth          * will pass through this function again to set the C bit */
820*fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
821*fcf5ef2aSThomas Huth     }
822*fcf5ef2aSThomas Huth 
823*fcf5ef2aSThomas Huth     if (new_pte1 != pte.pte1) {
824*fcf5ef2aSThomas Huth         ppc_hash64_store_hpte(cpu, pte_offset / HASH_PTE_SIZE_64,
825*fcf5ef2aSThomas Huth                               pte.pte0, new_pte1);
826*fcf5ef2aSThomas Huth     }
827*fcf5ef2aSThomas Huth 
828*fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
829*fcf5ef2aSThomas Huth 
830*fcf5ef2aSThomas Huth     raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
831*fcf5ef2aSThomas Huth 
832*fcf5ef2aSThomas Huth     tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
833*fcf5ef2aSThomas Huth                  prot, mmu_idx, 1ULL << apshift);
834*fcf5ef2aSThomas Huth 
835*fcf5ef2aSThomas Huth     return 0;
836*fcf5ef2aSThomas Huth }
837*fcf5ef2aSThomas Huth 
838*fcf5ef2aSThomas Huth hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr)
839*fcf5ef2aSThomas Huth {
840*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
841*fcf5ef2aSThomas Huth     ppc_slb_t *slb;
842*fcf5ef2aSThomas Huth     hwaddr pte_offset, raddr;
843*fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
844*fcf5ef2aSThomas Huth     unsigned apshift;
845*fcf5ef2aSThomas Huth 
846*fcf5ef2aSThomas Huth     /* Handle real mode */
847*fcf5ef2aSThomas Huth     if (msr_dr == 0) {
848*fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are ignored */
849*fcf5ef2aSThomas Huth         raddr = addr & 0x0FFFFFFFFFFFFFFFULL;
850*fcf5ef2aSThomas Huth 
851*fcf5ef2aSThomas Huth         /* In HV mode, add HRMOR if top EA bit is clear */
852*fcf5ef2aSThomas Huth         if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) {
853*fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_HRMOR];
854*fcf5ef2aSThomas Huth         }
855*fcf5ef2aSThomas Huth 
856*fcf5ef2aSThomas Huth         /* Otherwise, check VPM for RMA vs VRMA */
857*fcf5ef2aSThomas Huth         if (env->spr[SPR_LPCR] & LPCR_VPM0) {
858*fcf5ef2aSThomas Huth             slb = &env->vrma_slb;
859*fcf5ef2aSThomas Huth             if (!slb->sps) {
860*fcf5ef2aSThomas Huth                 return -1;
861*fcf5ef2aSThomas Huth             }
862*fcf5ef2aSThomas Huth         } else if (raddr < env->rmls) {
863*fcf5ef2aSThomas Huth             /* RMA. Check bounds in RMLS */
864*fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_RMOR];
865*fcf5ef2aSThomas Huth         } else {
866*fcf5ef2aSThomas Huth             return -1;
867*fcf5ef2aSThomas Huth         }
868*fcf5ef2aSThomas Huth     } else {
869*fcf5ef2aSThomas Huth         slb = slb_lookup(cpu, addr);
870*fcf5ef2aSThomas Huth         if (!slb) {
871*fcf5ef2aSThomas Huth             return -1;
872*fcf5ef2aSThomas Huth         }
873*fcf5ef2aSThomas Huth     }
874*fcf5ef2aSThomas Huth 
875*fcf5ef2aSThomas Huth     pte_offset = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift);
876*fcf5ef2aSThomas Huth     if (pte_offset == -1) {
877*fcf5ef2aSThomas Huth         return -1;
878*fcf5ef2aSThomas Huth     }
879*fcf5ef2aSThomas Huth 
880*fcf5ef2aSThomas Huth     return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr)
881*fcf5ef2aSThomas Huth         & TARGET_PAGE_MASK;
882*fcf5ef2aSThomas Huth }
883*fcf5ef2aSThomas Huth 
884*fcf5ef2aSThomas Huth void ppc_hash64_store_hpte(PowerPCCPU *cpu,
885*fcf5ef2aSThomas Huth                            target_ulong pte_index,
886*fcf5ef2aSThomas Huth                            target_ulong pte0, target_ulong pte1)
887*fcf5ef2aSThomas Huth {
888*fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
889*fcf5ef2aSThomas Huth 
890*fcf5ef2aSThomas Huth     if (env->external_htab == MMU_HASH64_KVM_MANAGED_HPT) {
891*fcf5ef2aSThomas Huth         kvmppc_hash64_write_pte(env, pte_index, pte0, pte1);
892*fcf5ef2aSThomas Huth         return;
893*fcf5ef2aSThomas Huth     }
894*fcf5ef2aSThomas Huth 
895*fcf5ef2aSThomas Huth     pte_index *= HASH_PTE_SIZE_64;
896*fcf5ef2aSThomas Huth     if (env->external_htab) {
897*fcf5ef2aSThomas Huth         stq_p(env->external_htab + pte_index, pte0);
898*fcf5ef2aSThomas Huth         stq_p(env->external_htab + pte_index + HASH_PTE_SIZE_64 / 2, pte1);
899*fcf5ef2aSThomas Huth     } else {
900*fcf5ef2aSThomas Huth         stq_phys(CPU(cpu)->as, env->htab_base + pte_index, pte0);
901*fcf5ef2aSThomas Huth         stq_phys(CPU(cpu)->as,
902*fcf5ef2aSThomas Huth                  env->htab_base + pte_index + HASH_PTE_SIZE_64 / 2, pte1);
903*fcf5ef2aSThomas Huth     }
904*fcf5ef2aSThomas Huth }
905*fcf5ef2aSThomas Huth 
906*fcf5ef2aSThomas Huth void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu,
907*fcf5ef2aSThomas Huth                                target_ulong pte_index,
908*fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
909*fcf5ef2aSThomas Huth {
910*fcf5ef2aSThomas Huth     /*
911*fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
912*fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
913*fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
914*fcf5ef2aSThomas Huth      */
915*fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
916*fcf5ef2aSThomas Huth }
917*fcf5ef2aSThomas Huth 
918*fcf5ef2aSThomas Huth void ppc_hash64_update_rmls(CPUPPCState *env)
919*fcf5ef2aSThomas Huth {
920*fcf5ef2aSThomas Huth     uint64_t lpcr = env->spr[SPR_LPCR];
921*fcf5ef2aSThomas Huth 
922*fcf5ef2aSThomas Huth     /*
923*fcf5ef2aSThomas Huth      * This is the full 4 bits encoding of POWER8. Previous
924*fcf5ef2aSThomas Huth      * CPUs only support a subset of these but the filtering
925*fcf5ef2aSThomas Huth      * is done when writing LPCR
926*fcf5ef2aSThomas Huth      */
927*fcf5ef2aSThomas Huth     switch ((lpcr & LPCR_RMLS) >> LPCR_RMLS_SHIFT) {
928*fcf5ef2aSThomas Huth     case 0x8: /* 32MB */
929*fcf5ef2aSThomas Huth         env->rmls = 0x2000000ull;
930*fcf5ef2aSThomas Huth         break;
931*fcf5ef2aSThomas Huth     case 0x3: /* 64MB */
932*fcf5ef2aSThomas Huth         env->rmls = 0x4000000ull;
933*fcf5ef2aSThomas Huth         break;
934*fcf5ef2aSThomas Huth     case 0x7: /* 128MB */
935*fcf5ef2aSThomas Huth         env->rmls = 0x8000000ull;
936*fcf5ef2aSThomas Huth         break;
937*fcf5ef2aSThomas Huth     case 0x4: /* 256MB */
938*fcf5ef2aSThomas Huth         env->rmls = 0x10000000ull;
939*fcf5ef2aSThomas Huth         break;
940*fcf5ef2aSThomas Huth     case 0x2: /* 1GB */
941*fcf5ef2aSThomas Huth         env->rmls = 0x40000000ull;
942*fcf5ef2aSThomas Huth         break;
943*fcf5ef2aSThomas Huth     case 0x1: /* 16GB */
944*fcf5ef2aSThomas Huth         env->rmls = 0x400000000ull;
945*fcf5ef2aSThomas Huth         break;
946*fcf5ef2aSThomas Huth     default:
947*fcf5ef2aSThomas Huth         /* What to do here ??? */
948*fcf5ef2aSThomas Huth         env->rmls = 0;
949*fcf5ef2aSThomas Huth     }
950*fcf5ef2aSThomas Huth }
951*fcf5ef2aSThomas Huth 
952*fcf5ef2aSThomas Huth void ppc_hash64_update_vrma(CPUPPCState *env)
953*fcf5ef2aSThomas Huth {
954*fcf5ef2aSThomas Huth     const struct ppc_one_seg_page_size *sps = NULL;
955*fcf5ef2aSThomas Huth     target_ulong esid, vsid, lpcr;
956*fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->vrma_slb;
957*fcf5ef2aSThomas Huth     uint32_t vrmasd;
958*fcf5ef2aSThomas Huth     int i;
959*fcf5ef2aSThomas Huth 
960*fcf5ef2aSThomas Huth     /* First clear it */
961*fcf5ef2aSThomas Huth     slb->esid = slb->vsid = 0;
962*fcf5ef2aSThomas Huth     slb->sps = NULL;
963*fcf5ef2aSThomas Huth 
964*fcf5ef2aSThomas Huth     /* Is VRMA enabled ? */
965*fcf5ef2aSThomas Huth     lpcr = env->spr[SPR_LPCR];
966*fcf5ef2aSThomas Huth     if (!(lpcr & LPCR_VPM0)) {
967*fcf5ef2aSThomas Huth         return;
968*fcf5ef2aSThomas Huth     }
969*fcf5ef2aSThomas Huth 
970*fcf5ef2aSThomas Huth     /* Make one up. Mostly ignore the ESID which will not be
971*fcf5ef2aSThomas Huth      * needed for translation
972*fcf5ef2aSThomas Huth      */
973*fcf5ef2aSThomas Huth     vsid = SLB_VSID_VRMA;
974*fcf5ef2aSThomas Huth     vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
975*fcf5ef2aSThomas Huth     vsid |= (vrmasd << 4) & (SLB_VSID_L | SLB_VSID_LP);
976*fcf5ef2aSThomas Huth     esid = SLB_ESID_V;
977*fcf5ef2aSThomas Huth 
978*fcf5ef2aSThomas Huth    for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
979*fcf5ef2aSThomas Huth         const struct ppc_one_seg_page_size *sps1 = &env->sps.sps[i];
980*fcf5ef2aSThomas Huth 
981*fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
982*fcf5ef2aSThomas Huth             break;
983*fcf5ef2aSThomas Huth         }
984*fcf5ef2aSThomas Huth 
985*fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
986*fcf5ef2aSThomas Huth             sps = sps1;
987*fcf5ef2aSThomas Huth             break;
988*fcf5ef2aSThomas Huth         }
989*fcf5ef2aSThomas Huth     }
990*fcf5ef2aSThomas Huth 
991*fcf5ef2aSThomas Huth     if (!sps) {
992*fcf5ef2aSThomas Huth         error_report("Bad page size encoding esid 0x"TARGET_FMT_lx
993*fcf5ef2aSThomas Huth                      " vsid 0x"TARGET_FMT_lx, esid, vsid);
994*fcf5ef2aSThomas Huth         return;
995*fcf5ef2aSThomas Huth     }
996*fcf5ef2aSThomas Huth 
997*fcf5ef2aSThomas Huth     slb->vsid = vsid;
998*fcf5ef2aSThomas Huth     slb->esid = esid;
999*fcf5ef2aSThomas Huth     slb->sps = sps;
1000*fcf5ef2aSThomas Huth }
1001*fcf5ef2aSThomas Huth 
1002*fcf5ef2aSThomas Huth void helper_store_lpcr(CPUPPCState *env, target_ulong val)
1003*fcf5ef2aSThomas Huth {
1004*fcf5ef2aSThomas Huth     uint64_t lpcr = 0;
1005*fcf5ef2aSThomas Huth 
1006*fcf5ef2aSThomas Huth     /* Filter out bits */
1007*fcf5ef2aSThomas Huth     switch (env->mmu_model) {
1008*fcf5ef2aSThomas Huth     case POWERPC_MMU_64B: /* 970 */
1009*fcf5ef2aSThomas Huth         if (val & 0x40) {
1010*fcf5ef2aSThomas Huth             lpcr |= LPCR_LPES0;
1011*fcf5ef2aSThomas Huth         }
1012*fcf5ef2aSThomas Huth         if (val & 0x8000000000000000ull) {
1013*fcf5ef2aSThomas Huth             lpcr |= LPCR_LPES1;
1014*fcf5ef2aSThomas Huth         }
1015*fcf5ef2aSThomas Huth         if (val & 0x20) {
1016*fcf5ef2aSThomas Huth             lpcr |= (0x4ull << LPCR_RMLS_SHIFT);
1017*fcf5ef2aSThomas Huth         }
1018*fcf5ef2aSThomas Huth         if (val & 0x4000000000000000ull) {
1019*fcf5ef2aSThomas Huth             lpcr |= (0x2ull << LPCR_RMLS_SHIFT);
1020*fcf5ef2aSThomas Huth         }
1021*fcf5ef2aSThomas Huth         if (val & 0x2000000000000000ull) {
1022*fcf5ef2aSThomas Huth             lpcr |= (0x1ull << LPCR_RMLS_SHIFT);
1023*fcf5ef2aSThomas Huth         }
1024*fcf5ef2aSThomas Huth         env->spr[SPR_RMOR] = ((lpcr >> 41) & 0xffffull) << 26;
1025*fcf5ef2aSThomas Huth 
1026*fcf5ef2aSThomas Huth         /* XXX We could also write LPID from HID4 here
1027*fcf5ef2aSThomas Huth          * but since we don't tag any translation on it
1028*fcf5ef2aSThomas Huth          * it doesn't actually matter
1029*fcf5ef2aSThomas Huth          */
1030*fcf5ef2aSThomas Huth         /* XXX For proper emulation of 970 we also need
1031*fcf5ef2aSThomas Huth          * to dig HRMOR out of HID5
1032*fcf5ef2aSThomas Huth          */
1033*fcf5ef2aSThomas Huth         break;
1034*fcf5ef2aSThomas Huth     case POWERPC_MMU_2_03: /* P5p */
1035*fcf5ef2aSThomas Huth         lpcr = val & (LPCR_RMLS | LPCR_ILE |
1036*fcf5ef2aSThomas Huth                       LPCR_LPES0 | LPCR_LPES1 |
1037*fcf5ef2aSThomas Huth                       LPCR_RMI | LPCR_HDICE);
1038*fcf5ef2aSThomas Huth         break;
1039*fcf5ef2aSThomas Huth     case POWERPC_MMU_2_06: /* P7 */
1040*fcf5ef2aSThomas Huth         lpcr = val & (LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_DPFD |
1041*fcf5ef2aSThomas Huth                       LPCR_VRMASD | LPCR_RMLS | LPCR_ILE |
1042*fcf5ef2aSThomas Huth                       LPCR_P7_PECE0 | LPCR_P7_PECE1 | LPCR_P7_PECE2 |
1043*fcf5ef2aSThomas Huth                       LPCR_MER | LPCR_TC |
1044*fcf5ef2aSThomas Huth                       LPCR_LPES0 | LPCR_LPES1 | LPCR_HDICE);
1045*fcf5ef2aSThomas Huth         break;
1046*fcf5ef2aSThomas Huth     case POWERPC_MMU_2_07: /* P8 */
1047*fcf5ef2aSThomas Huth         lpcr = val & (LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_KBV |
1048*fcf5ef2aSThomas Huth                       LPCR_DPFD | LPCR_VRMASD | LPCR_RMLS | LPCR_ILE |
1049*fcf5ef2aSThomas Huth                       LPCR_AIL | LPCR_ONL | LPCR_P8_PECE0 | LPCR_P8_PECE1 |
1050*fcf5ef2aSThomas Huth                       LPCR_P8_PECE2 | LPCR_P8_PECE3 | LPCR_P8_PECE4 |
1051*fcf5ef2aSThomas Huth                       LPCR_MER | LPCR_TC | LPCR_LPES0 | LPCR_HDICE);
1052*fcf5ef2aSThomas Huth         break;
1053*fcf5ef2aSThomas Huth     default:
1054*fcf5ef2aSThomas Huth         ;
1055*fcf5ef2aSThomas Huth     }
1056*fcf5ef2aSThomas Huth     env->spr[SPR_LPCR] = lpcr;
1057*fcf5ef2aSThomas Huth     ppc_hash64_update_rmls(env);
1058*fcf5ef2aSThomas Huth     ppc_hash64_update_vrma(env);
1059*fcf5ef2aSThomas Huth }
1060