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