xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision 34525595fb379796241b0f3a9946bb21c4da1178)
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 "cpu.h"
22fcf5ef2aSThomas Huth #include "exec/exec-all.h"
23fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
24fcf5ef2aSThomas Huth #include "qemu/error-report.h"
25b3946626SVincent Palatin #include "sysemu/hw_accel.h"
26fcf5ef2aSThomas Huth #include "kvm_ppc.h"
27fcf5ef2aSThomas Huth #include "mmu-hash64.h"
28fcf5ef2aSThomas Huth #include "exec/log.h"
297222b94aSDavid Gibson #include "hw/hw.h"
30b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.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  * SLB handling
42fcf5ef2aSThomas Huth  */
43fcf5ef2aSThomas Huth 
44fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr)
45fcf5ef2aSThomas Huth {
46fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
47fcf5ef2aSThomas Huth     uint64_t esid_256M, esid_1T;
48fcf5ef2aSThomas Huth     int n;
49fcf5ef2aSThomas Huth 
50fcf5ef2aSThomas Huth     LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr);
51fcf5ef2aSThomas Huth 
52fcf5ef2aSThomas Huth     esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V;
53fcf5ef2aSThomas Huth     esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V;
54fcf5ef2aSThomas Huth 
5567d7d66fSDavid Gibson     for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
56fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
57fcf5ef2aSThomas Huth 
58fcf5ef2aSThomas Huth         LOG_SLB("%s: slot %d %016" PRIx64 " %016"
59fcf5ef2aSThomas Huth                     PRIx64 "\n", __func__, n, slb->esid, slb->vsid);
60fcf5ef2aSThomas Huth         /* We check for 1T matches on all MMUs here - if the MMU
61fcf5ef2aSThomas Huth          * doesn't have 1T segment support, we will have prevented 1T
62fcf5ef2aSThomas Huth          * entries from being inserted in the slbmte code. */
63fcf5ef2aSThomas Huth         if (((slb->esid == esid_256M) &&
64fcf5ef2aSThomas Huth              ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M))
65fcf5ef2aSThomas Huth             || ((slb->esid == esid_1T) &&
66fcf5ef2aSThomas Huth                 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) {
67fcf5ef2aSThomas Huth             return slb;
68fcf5ef2aSThomas Huth         }
69fcf5ef2aSThomas Huth     }
70fcf5ef2aSThomas Huth 
71fcf5ef2aSThomas Huth     return NULL;
72fcf5ef2aSThomas Huth }
73fcf5ef2aSThomas Huth 
74fcf5ef2aSThomas Huth void dump_slb(FILE *f, fprintf_function cpu_fprintf, PowerPCCPU *cpu)
75fcf5ef2aSThomas Huth {
76fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
77fcf5ef2aSThomas Huth     int i;
78fcf5ef2aSThomas Huth     uint64_t slbe, slbv;
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     cpu_synchronize_state(CPU(cpu));
81fcf5ef2aSThomas Huth 
82fcf5ef2aSThomas Huth     cpu_fprintf(f, "SLB\tESID\t\t\tVSID\n");
8367d7d66fSDavid Gibson     for (i = 0; i < cpu->hash64_opts->slb_size; i++) {
84fcf5ef2aSThomas Huth         slbe = env->slb[i].esid;
85fcf5ef2aSThomas Huth         slbv = env->slb[i].vsid;
86fcf5ef2aSThomas Huth         if (slbe == 0 && slbv == 0) {
87fcf5ef2aSThomas Huth             continue;
88fcf5ef2aSThomas Huth         }
89fcf5ef2aSThomas Huth         cpu_fprintf(f, "%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n",
90fcf5ef2aSThomas Huth                     i, slbe, slbv);
91fcf5ef2aSThomas Huth     }
92fcf5ef2aSThomas Huth }
93fcf5ef2aSThomas Huth 
94fcf5ef2aSThomas Huth void helper_slbia(CPUPPCState *env)
95fcf5ef2aSThomas Huth {
9667d7d66fSDavid Gibson     PowerPCCPU *cpu = ppc_env_get_cpu(env);
97fcf5ef2aSThomas Huth     int n;
98fcf5ef2aSThomas Huth 
99fcf5ef2aSThomas Huth     /* XXX: Warning: slbia never invalidates the first segment */
10067d7d66fSDavid Gibson     for (n = 1; n < cpu->hash64_opts->slb_size; n++) {
101fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
102fcf5ef2aSThomas Huth 
103fcf5ef2aSThomas Huth         if (slb->esid & SLB_ESID_V) {
104fcf5ef2aSThomas Huth             slb->esid &= ~SLB_ESID_V;
105fcf5ef2aSThomas Huth             /* XXX: given the fact that segment size is 256 MB or 1TB,
106fcf5ef2aSThomas Huth              *      and we still don't have a tlb_flush_mask(env, n, mask)
107fcf5ef2aSThomas Huth              *      in QEMU, we just invalidate all TLBs
108fcf5ef2aSThomas Huth              */
109fcf5ef2aSThomas Huth             env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
110fcf5ef2aSThomas Huth         }
111fcf5ef2aSThomas Huth     }
112fcf5ef2aSThomas Huth }
113fcf5ef2aSThomas Huth 
114a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr,
115a63f1dfcSNikunj A Dadhania                            target_ulong global)
116fcf5ef2aSThomas Huth {
117fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
118fcf5ef2aSThomas Huth     ppc_slb_t *slb;
119fcf5ef2aSThomas Huth 
120fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, addr);
121fcf5ef2aSThomas Huth     if (!slb) {
122fcf5ef2aSThomas Huth         return;
123fcf5ef2aSThomas Huth     }
124fcf5ef2aSThomas Huth 
125fcf5ef2aSThomas Huth     if (slb->esid & SLB_ESID_V) {
126fcf5ef2aSThomas Huth         slb->esid &= ~SLB_ESID_V;
127fcf5ef2aSThomas Huth 
128fcf5ef2aSThomas Huth         /* XXX: given the fact that segment size is 256 MB or 1TB,
129fcf5ef2aSThomas Huth          *      and we still don't have a tlb_flush_mask(env, n, mask)
130fcf5ef2aSThomas Huth          *      in QEMU, we just invalidate all TLBs
131fcf5ef2aSThomas Huth          */
132a63f1dfcSNikunj A Dadhania         env->tlb_need_flush |=
133a63f1dfcSNikunj A Dadhania             (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH);
134fcf5ef2aSThomas Huth     }
135fcf5ef2aSThomas Huth }
136fcf5ef2aSThomas Huth 
137a63f1dfcSNikunj A Dadhania void helper_slbie(CPUPPCState *env, target_ulong addr)
138a63f1dfcSNikunj A Dadhania {
139a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, false);
140a63f1dfcSNikunj A Dadhania }
141a63f1dfcSNikunj A Dadhania 
142a63f1dfcSNikunj A Dadhania void helper_slbieg(CPUPPCState *env, target_ulong addr)
143a63f1dfcSNikunj A Dadhania {
144a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, true);
145a63f1dfcSNikunj A Dadhania }
146a63f1dfcSNikunj A Dadhania 
147fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot,
148fcf5ef2aSThomas Huth                   target_ulong esid, target_ulong vsid)
149fcf5ef2aSThomas Huth {
150fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
151fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
152b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
153fcf5ef2aSThomas Huth     int i;
154fcf5ef2aSThomas Huth 
15567d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
156fcf5ef2aSThomas Huth         return -1; /* Bad slot number */
157fcf5ef2aSThomas Huth     }
158fcf5ef2aSThomas Huth     if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) {
159fcf5ef2aSThomas Huth         return -1; /* Reserved bits set */
160fcf5ef2aSThomas Huth     }
161fcf5ef2aSThomas Huth     if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
162fcf5ef2aSThomas Huth         return -1; /* Bad segment size */
163fcf5ef2aSThomas Huth     }
16458969eeeSDavid Gibson     if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) {
165fcf5ef2aSThomas Huth         return -1; /* 1T segment on MMU that doesn't support it */
166fcf5ef2aSThomas Huth     }
167fcf5ef2aSThomas Huth 
168fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
169b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
170fcf5ef2aSThomas Huth 
171fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
172fcf5ef2aSThomas Huth             break;
173fcf5ef2aSThomas Huth         }
174fcf5ef2aSThomas Huth 
175fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
176fcf5ef2aSThomas Huth             sps = sps1;
177fcf5ef2aSThomas Huth             break;
178fcf5ef2aSThomas Huth         }
179fcf5ef2aSThomas Huth     }
180fcf5ef2aSThomas Huth 
181fcf5ef2aSThomas Huth     if (!sps) {
182fcf5ef2aSThomas Huth         error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
183fcf5ef2aSThomas Huth                      " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx,
184fcf5ef2aSThomas Huth                      slot, esid, vsid);
185fcf5ef2aSThomas Huth         return -1;
186fcf5ef2aSThomas Huth     }
187fcf5ef2aSThomas Huth 
188fcf5ef2aSThomas Huth     slb->esid = esid;
189fcf5ef2aSThomas Huth     slb->vsid = vsid;
190fcf5ef2aSThomas Huth     slb->sps = sps;
191fcf5ef2aSThomas Huth 
19276134d48SSuraj Jitindar Singh     LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx
19376134d48SSuraj Jitindar Singh             " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid,
194fcf5ef2aSThomas Huth             slb->esid, slb->vsid);
195fcf5ef2aSThomas Huth 
196fcf5ef2aSThomas Huth     return 0;
197fcf5ef2aSThomas Huth }
198fcf5ef2aSThomas Huth 
199fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb,
200fcf5ef2aSThomas Huth                              target_ulong *rt)
201fcf5ef2aSThomas Huth {
202fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
203fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
204fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
205fcf5ef2aSThomas Huth 
20667d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
207fcf5ef2aSThomas Huth         return -1;
208fcf5ef2aSThomas Huth     }
209fcf5ef2aSThomas Huth 
210fcf5ef2aSThomas Huth     *rt = slb->esid;
211fcf5ef2aSThomas Huth     return 0;
212fcf5ef2aSThomas Huth }
213fcf5ef2aSThomas Huth 
214fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
215fcf5ef2aSThomas Huth                              target_ulong *rt)
216fcf5ef2aSThomas Huth {
217fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
218fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
219fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
220fcf5ef2aSThomas Huth 
22167d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
222fcf5ef2aSThomas Huth         return -1;
223fcf5ef2aSThomas Huth     }
224fcf5ef2aSThomas Huth 
225fcf5ef2aSThomas Huth     *rt = slb->vsid;
226fcf5ef2aSThomas Huth     return 0;
227fcf5ef2aSThomas Huth }
228fcf5ef2aSThomas Huth 
229fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
230fcf5ef2aSThomas Huth                              target_ulong *rt)
231fcf5ef2aSThomas Huth {
232fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
233fcf5ef2aSThomas Huth     ppc_slb_t *slb;
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, env->msr)) {
236fcf5ef2aSThomas Huth         rb &= 0xffffffff;
237fcf5ef2aSThomas Huth     }
238fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, rb);
239fcf5ef2aSThomas Huth     if (slb == NULL) {
240fcf5ef2aSThomas Huth         *rt = (target_ulong)-1ul;
241fcf5ef2aSThomas Huth     } else {
242fcf5ef2aSThomas Huth         *rt = slb->vsid;
243fcf5ef2aSThomas Huth     }
244fcf5ef2aSThomas Huth     return 0;
245fcf5ef2aSThomas Huth }
246fcf5ef2aSThomas Huth 
247fcf5ef2aSThomas Huth void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs)
248fcf5ef2aSThomas Huth {
249fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
250fcf5ef2aSThomas Huth 
251fcf5ef2aSThomas Huth     if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) {
252fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
253fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
254fcf5ef2aSThomas Huth     }
255fcf5ef2aSThomas Huth }
256fcf5ef2aSThomas Huth 
257fcf5ef2aSThomas Huth target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb)
258fcf5ef2aSThomas Huth {
259fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
260fcf5ef2aSThomas Huth     target_ulong rt = 0;
261fcf5ef2aSThomas Huth 
262fcf5ef2aSThomas Huth     if (ppc_load_slb_esid(cpu, rb, &rt) < 0) {
263fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
264fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
265fcf5ef2aSThomas Huth     }
266fcf5ef2aSThomas Huth     return rt;
267fcf5ef2aSThomas Huth }
268fcf5ef2aSThomas Huth 
269fcf5ef2aSThomas Huth target_ulong helper_find_slb_vsid(CPUPPCState *env, target_ulong rb)
270fcf5ef2aSThomas Huth {
271fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
272fcf5ef2aSThomas Huth     target_ulong rt = 0;
273fcf5ef2aSThomas Huth 
274fcf5ef2aSThomas Huth     if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) {
275fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
276fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
277fcf5ef2aSThomas Huth     }
278fcf5ef2aSThomas Huth     return rt;
279fcf5ef2aSThomas Huth }
280fcf5ef2aSThomas Huth 
281fcf5ef2aSThomas Huth target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb)
282fcf5ef2aSThomas Huth {
283fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
284fcf5ef2aSThomas Huth     target_ulong rt = 0;
285fcf5ef2aSThomas Huth 
286fcf5ef2aSThomas Huth     if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) {
287fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
288fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
289fcf5ef2aSThomas Huth     }
290fcf5ef2aSThomas Huth     return rt;
291fcf5ef2aSThomas Huth }
292fcf5ef2aSThomas Huth 
29307a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */
29407a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu,
29507a68f99SSuraj Jitindar Singh                                               ppc_hash_pte64_t pte)
29607a68f99SSuraj Jitindar Singh {
29707a68f99SSuraj Jitindar Singh     /* Exec permissions CANNOT take away read or write permissions */
29807a68f99SSuraj Jitindar Singh     return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ?
29907a68f99SSuraj Jitindar Singh             PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC;
30007a68f99SSuraj Jitindar Singh }
30107a68f99SSuraj Jitindar Singh 
30207a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */
303fcf5ef2aSThomas Huth static int ppc_hash64_pte_prot(PowerPCCPU *cpu,
304fcf5ef2aSThomas Huth                                ppc_slb_t *slb, ppc_hash_pte64_t pte)
305fcf5ef2aSThomas Huth {
306fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
307fcf5ef2aSThomas Huth     unsigned pp, key;
308fcf5ef2aSThomas Huth     /* Some pp bit combinations have undefined behaviour, so default
309fcf5ef2aSThomas Huth      * to no access in those cases */
310fcf5ef2aSThomas Huth     int prot = 0;
311fcf5ef2aSThomas Huth 
312fcf5ef2aSThomas Huth     key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP)
313fcf5ef2aSThomas Huth              : (slb->vsid & SLB_VSID_KS));
314fcf5ef2aSThomas Huth     pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
315fcf5ef2aSThomas Huth 
316fcf5ef2aSThomas Huth     if (key == 0) {
317fcf5ef2aSThomas Huth         switch (pp) {
318fcf5ef2aSThomas Huth         case 0x0:
319fcf5ef2aSThomas Huth         case 0x1:
320fcf5ef2aSThomas Huth         case 0x2:
321347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
322fcf5ef2aSThomas Huth             break;
323fcf5ef2aSThomas Huth 
324fcf5ef2aSThomas Huth         case 0x3:
325fcf5ef2aSThomas Huth         case 0x6:
326347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
327fcf5ef2aSThomas Huth             break;
328fcf5ef2aSThomas Huth         }
329fcf5ef2aSThomas Huth     } else {
330fcf5ef2aSThomas Huth         switch (pp) {
331fcf5ef2aSThomas Huth         case 0x0:
332fcf5ef2aSThomas Huth         case 0x6:
333fcf5ef2aSThomas Huth             break;
334fcf5ef2aSThomas Huth 
335fcf5ef2aSThomas Huth         case 0x1:
336fcf5ef2aSThomas Huth         case 0x3:
337347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
338fcf5ef2aSThomas Huth             break;
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth         case 0x2:
341347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
342fcf5ef2aSThomas Huth             break;
343fcf5ef2aSThomas Huth         }
344fcf5ef2aSThomas Huth     }
345fcf5ef2aSThomas Huth 
346fcf5ef2aSThomas Huth     return prot;
347fcf5ef2aSThomas Huth }
348fcf5ef2aSThomas Huth 
349a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */
350a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key)
351a6152b52SSuraj Jitindar Singh {
352a6152b52SSuraj Jitindar Singh     CPUPPCState *env = &cpu->env;
353a6152b52SSuraj Jitindar Singh     int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3;
354a6152b52SSuraj Jitindar Singh 
355a6152b52SSuraj Jitindar Singh     /*
356a6152b52SSuraj Jitindar Singh      * An instruction fetch is permitted if the IAMR bit is 0.
357a6152b52SSuraj Jitindar Singh      * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
358a6152b52SSuraj Jitindar Singh      * can only take away EXEC permissions not READ or WRITE permissions.
359a6152b52SSuraj Jitindar Singh      * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
360a6152b52SSuraj Jitindar Singh      * EXEC permissions are allowed.
361a6152b52SSuraj Jitindar Singh      */
362a6152b52SSuraj Jitindar Singh     return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE :
363a6152b52SSuraj Jitindar Singh                                PAGE_READ | PAGE_WRITE | PAGE_EXEC;
364a6152b52SSuraj Jitindar Singh }
365a6152b52SSuraj Jitindar Singh 
366fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte)
367fcf5ef2aSThomas Huth {
368fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
369fcf5ef2aSThomas Huth     int key, amrbits;
370fcf5ef2aSThomas Huth     int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
371fcf5ef2aSThomas Huth 
372fcf5ef2aSThomas Huth     /* Only recent MMUs implement Virtual Page Class Key Protection */
37358969eeeSDavid Gibson     if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) {
374fcf5ef2aSThomas Huth         return prot;
375fcf5ef2aSThomas Huth     }
376fcf5ef2aSThomas Huth 
377fcf5ef2aSThomas Huth     key = HPTE64_R_KEY(pte.pte1);
378fcf5ef2aSThomas Huth     amrbits = (env->spr[SPR_AMR] >> 2*(31 - key)) & 0x3;
379fcf5ef2aSThomas Huth 
380fcf5ef2aSThomas Huth     /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
381fcf5ef2aSThomas Huth     /*         env->spr[SPR_AMR]); */
382fcf5ef2aSThomas Huth 
383fcf5ef2aSThomas Huth     /*
384fcf5ef2aSThomas Huth      * A store is permitted if the AMR bit is 0. Remove write
385fcf5ef2aSThomas Huth      * protection if it is set.
386fcf5ef2aSThomas Huth      */
387fcf5ef2aSThomas Huth     if (amrbits & 0x2) {
388fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
389fcf5ef2aSThomas Huth     }
390fcf5ef2aSThomas Huth     /*
391fcf5ef2aSThomas Huth      * A load is permitted if the AMR bit is 0. Remove read
392fcf5ef2aSThomas Huth      * protection if it is set.
393fcf5ef2aSThomas Huth      */
394fcf5ef2aSThomas Huth     if (amrbits & 0x1) {
395fcf5ef2aSThomas Huth         prot &= ~PAGE_READ;
396fcf5ef2aSThomas Huth     }
397fcf5ef2aSThomas Huth 
398a6152b52SSuraj Jitindar Singh     switch (env->mmu_model) {
399a6152b52SSuraj Jitindar Singh     /*
400a6152b52SSuraj Jitindar Singh      * MMU version 2.07 and later support IAMR
401a6152b52SSuraj Jitindar Singh      * Check if the IAMR allows the instruction access - it will return
402a6152b52SSuraj Jitindar Singh      * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
403a6152b52SSuraj Jitindar Singh      * if it does (and prot will be unchanged indicating execution support).
404a6152b52SSuraj Jitindar Singh      */
405a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_2_07:
406a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_3_00:
407a6152b52SSuraj Jitindar Singh         prot &= ppc_hash64_iamr_prot(cpu, key);
408a6152b52SSuraj Jitindar Singh         break;
409a6152b52SSuraj Jitindar Singh     default:
410a6152b52SSuraj Jitindar Singh         break;
411a6152b52SSuraj Jitindar Singh     }
412a6152b52SSuraj Jitindar Singh 
413fcf5ef2aSThomas Huth     return prot;
414fcf5ef2aSThomas Huth }
415fcf5ef2aSThomas Huth 
4167222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu,
4177222b94aSDavid Gibson                                              hwaddr ptex, int n)
418fcf5ef2aSThomas Huth {
4197222b94aSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
42036778660SDavid Gibson     hwaddr base = ppc_hash64_hpt_base(cpu);
4217222b94aSDavid Gibson     hwaddr plen = n * HASH_PTE_SIZE_64;
422e57ca75cSDavid Gibson     const ppc_hash_pte64_t *hptes;
423e57ca75cSDavid Gibson 
424e57ca75cSDavid Gibson     if (cpu->vhyp) {
425e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
426e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
427e57ca75cSDavid Gibson         return vhc->map_hptes(cpu->vhyp, ptex, n);
428e57ca75cSDavid Gibson     }
429e57ca75cSDavid Gibson 
430e57ca75cSDavid Gibson     if (!base) {
431e57ca75cSDavid Gibson         return NULL;
432e57ca75cSDavid Gibson     }
433e57ca75cSDavid Gibson 
434f26404fbSPeter Maydell     hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false,
435f26404fbSPeter Maydell                               MEMTXATTRS_UNSPECIFIED);
4367222b94aSDavid Gibson     if (plen < (n * HASH_PTE_SIZE_64)) {
4377222b94aSDavid Gibson         hw_error("%s: Unable to map all requested HPTEs\n", __func__);
438fcf5ef2aSThomas Huth     }
4397222b94aSDavid Gibson     return hptes;
440fcf5ef2aSThomas Huth }
441fcf5ef2aSThomas Huth 
4427222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes,
4437222b94aSDavid Gibson                             hwaddr ptex, int n)
444fcf5ef2aSThomas Huth {
445e57ca75cSDavid Gibson     if (cpu->vhyp) {
446e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
447e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
448e57ca75cSDavid Gibson         vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n);
449e57ca75cSDavid Gibson         return;
450e57ca75cSDavid Gibson     }
451e57ca75cSDavid Gibson 
4527222b94aSDavid Gibson     address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64,
4537222b94aSDavid Gibson                         false, n * HASH_PTE_SIZE_64);
454fcf5ef2aSThomas Huth }
455fcf5ef2aSThomas Huth 
456b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps,
457fcf5ef2aSThomas Huth                                 uint64_t pte0, uint64_t pte1)
458fcf5ef2aSThomas Huth {
459fcf5ef2aSThomas Huth     int i;
460fcf5ef2aSThomas Huth 
461fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
462fcf5ef2aSThomas Huth         if (sps->page_shift != 12) {
463fcf5ef2aSThomas Huth             /* 4kiB page in a non 4kiB segment */
464fcf5ef2aSThomas Huth             return 0;
465fcf5ef2aSThomas Huth         }
466fcf5ef2aSThomas Huth         /* Normal 4kiB page */
467fcf5ef2aSThomas Huth         return 12;
468fcf5ef2aSThomas Huth     }
469fcf5ef2aSThomas Huth 
470fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
471b07c59f7SDavid Gibson         const PPCHash64PageSize *ps = &sps->enc[i];
472fcf5ef2aSThomas Huth         uint64_t mask;
473fcf5ef2aSThomas Huth 
474fcf5ef2aSThomas Huth         if (!ps->page_shift) {
475fcf5ef2aSThomas Huth             break;
476fcf5ef2aSThomas Huth         }
477fcf5ef2aSThomas Huth 
478fcf5ef2aSThomas Huth         if (ps->page_shift == 12) {
479fcf5ef2aSThomas Huth             /* L bit is set so this can't be a 4kiB page */
480fcf5ef2aSThomas Huth             continue;
481fcf5ef2aSThomas Huth         }
482fcf5ef2aSThomas Huth 
483fcf5ef2aSThomas Huth         mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
484fcf5ef2aSThomas Huth 
485fcf5ef2aSThomas Huth         if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
486fcf5ef2aSThomas Huth             return ps->page_shift;
487fcf5ef2aSThomas Huth         }
488fcf5ef2aSThomas Huth     }
489fcf5ef2aSThomas Huth 
490fcf5ef2aSThomas Huth     return 0; /* Bad page size encoding */
491fcf5ef2aSThomas Huth }
492fcf5ef2aSThomas Huth 
493*34525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1)
494*34525595SBenjamin Herrenschmidt {
495*34525595SBenjamin Herrenschmidt     /* Insert B into pte0 */
496*34525595SBenjamin Herrenschmidt     *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) |
497*34525595SBenjamin Herrenschmidt             ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) <<
498*34525595SBenjamin Herrenschmidt              (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT));
499*34525595SBenjamin Herrenschmidt 
500*34525595SBenjamin Herrenschmidt     /* Remove B from pte1 */
501*34525595SBenjamin Herrenschmidt     *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK;
502*34525595SBenjamin Herrenschmidt }
503*34525595SBenjamin Herrenschmidt 
504*34525595SBenjamin Herrenschmidt 
505fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
506b07c59f7SDavid Gibson                                      const PPCHash64SegmentPageSizes *sps,
507fcf5ef2aSThomas Huth                                      target_ulong ptem,
508fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
509fcf5ef2aSThomas Huth {
510fcf5ef2aSThomas Huth     int i;
5117222b94aSDavid Gibson     const ppc_hash_pte64_t *pteg;
512fcf5ef2aSThomas Huth     target_ulong pte0, pte1;
5137222b94aSDavid Gibson     target_ulong ptex;
514fcf5ef2aSThomas Huth 
51536778660SDavid Gibson     ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP;
5167222b94aSDavid Gibson     pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
5177222b94aSDavid Gibson     if (!pteg) {
518fcf5ef2aSThomas Huth         return -1;
519fcf5ef2aSThomas Huth     }
520fcf5ef2aSThomas Huth     for (i = 0; i < HPTES_PER_GROUP; i++) {
5217222b94aSDavid Gibson         pte0 = ppc_hash64_hpte0(cpu, pteg, i);
5223054b0caSBenjamin Herrenschmidt         /*
5233054b0caSBenjamin Herrenschmidt          * pte0 contains the valid bit and must be read before pte1,
5243054b0caSBenjamin Herrenschmidt          * otherwise we might see an old pte1 with a new valid bit and
5253054b0caSBenjamin Herrenschmidt          * thus an inconsistent hpte value
5263054b0caSBenjamin Herrenschmidt          */
5273054b0caSBenjamin Herrenschmidt         smp_rmb();
5287222b94aSDavid Gibson         pte1 = ppc_hash64_hpte1(cpu, pteg, i);
529fcf5ef2aSThomas Huth 
530*34525595SBenjamin Herrenschmidt         /* Convert format if necessary */
531*34525595SBenjamin Herrenschmidt         if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) {
532*34525595SBenjamin Herrenschmidt             ppc64_v3_new_to_old_hpte(&pte0, &pte1);
533*34525595SBenjamin Herrenschmidt         }
534*34525595SBenjamin Herrenschmidt 
535fcf5ef2aSThomas Huth         /* This compares V, B, H (secondary) and the AVPN */
536fcf5ef2aSThomas Huth         if (HPTE64_V_COMPARE(pte0, ptem)) {
537fcf5ef2aSThomas Huth             *pshift = hpte_page_shift(sps, pte0, pte1);
538fcf5ef2aSThomas Huth             /*
539fcf5ef2aSThomas Huth              * If there is no match, ignore the PTE, it could simply
540fcf5ef2aSThomas Huth              * be for a different segment size encoding and the
541fcf5ef2aSThomas Huth              * architecture specifies we should not match. Linux will
542fcf5ef2aSThomas Huth              * potentially leave behind PTEs for the wrong base page
543fcf5ef2aSThomas Huth              * size when demoting segments.
544fcf5ef2aSThomas Huth              */
545fcf5ef2aSThomas Huth             if (*pshift == 0) {
546fcf5ef2aSThomas Huth                 continue;
547fcf5ef2aSThomas Huth             }
548fcf5ef2aSThomas Huth             /* We don't do anything with pshift yet as qemu TLB only deals
549fcf5ef2aSThomas Huth              * with 4K pages anyway
550fcf5ef2aSThomas Huth              */
551fcf5ef2aSThomas Huth             pte->pte0 = pte0;
552fcf5ef2aSThomas Huth             pte->pte1 = pte1;
5537222b94aSDavid Gibson             ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
5547222b94aSDavid Gibson             return ptex + i;
555fcf5ef2aSThomas Huth         }
556fcf5ef2aSThomas Huth     }
5577222b94aSDavid Gibson     ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
558fcf5ef2aSThomas Huth     /*
559fcf5ef2aSThomas Huth      * We didn't find a valid entry.
560fcf5ef2aSThomas Huth      */
561fcf5ef2aSThomas Huth     return -1;
562fcf5ef2aSThomas Huth }
563fcf5ef2aSThomas Huth 
564fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
565fcf5ef2aSThomas Huth                                      ppc_slb_t *slb, target_ulong eaddr,
566fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
567fcf5ef2aSThomas Huth {
568fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
5697222b94aSDavid Gibson     hwaddr hash, ptex;
570fcf5ef2aSThomas Huth     uint64_t vsid, epnmask, epn, ptem;
571b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = slb->sps;
572fcf5ef2aSThomas Huth 
573fcf5ef2aSThomas Huth     /* The SLB store path should prevent any bad page size encodings
574fcf5ef2aSThomas Huth      * getting in there, so: */
575fcf5ef2aSThomas Huth     assert(sps);
576fcf5ef2aSThomas Huth 
577fcf5ef2aSThomas Huth     /* If ISL is set in LPCR we need to clamp the page size to 4K */
578fcf5ef2aSThomas Huth     if (env->spr[SPR_LPCR] & LPCR_ISL) {
579fcf5ef2aSThomas Huth         /* We assume that when using TCG, 4k is first entry of SPS */
580b07c59f7SDavid Gibson         sps = &cpu->hash64_opts->sps[0];
581fcf5ef2aSThomas Huth         assert(sps->page_shift == 12);
582fcf5ef2aSThomas Huth     }
583fcf5ef2aSThomas Huth 
584fcf5ef2aSThomas Huth     epnmask = ~((1ULL << sps->page_shift) - 1);
585fcf5ef2aSThomas Huth 
586fcf5ef2aSThomas Huth     if (slb->vsid & SLB_VSID_B) {
587fcf5ef2aSThomas Huth         /* 1TB segment */
588fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
589fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
590fcf5ef2aSThomas Huth         hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
591fcf5ef2aSThomas Huth     } else {
592fcf5ef2aSThomas Huth         /* 256M segment */
593fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
594fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
595fcf5ef2aSThomas Huth         hash = vsid ^ (epn >> sps->page_shift);
596fcf5ef2aSThomas Huth     }
597fcf5ef2aSThomas Huth     ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
598fcf5ef2aSThomas Huth     ptem |= HPTE64_V_VALID;
599fcf5ef2aSThomas Huth 
600fcf5ef2aSThomas Huth     /* Page address translation */
601fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
602fcf5ef2aSThomas Huth             "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
603fcf5ef2aSThomas Huth             " hash " TARGET_FMT_plx "\n",
60436778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash);
605fcf5ef2aSThomas Huth 
606fcf5ef2aSThomas Huth     /* Primary PTEG lookup */
607fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
608fcf5ef2aSThomas Huth             "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
609fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
610fcf5ef2aSThomas Huth             " hash=" TARGET_FMT_plx "\n",
61136778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu),
61236778660SDavid Gibson             vsid, ptem,  hash);
6137222b94aSDavid Gibson     ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
614fcf5ef2aSThomas Huth 
6157222b94aSDavid Gibson     if (ptex == -1) {
616fcf5ef2aSThomas Huth         /* Secondary PTEG lookup */
617fcf5ef2aSThomas Huth         ptem |= HPTE64_V_SECONDARY;
618fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU,
619fcf5ef2aSThomas Huth                 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
620fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
62136778660SDavid Gibson                 " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu),
62236778660SDavid Gibson                 ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash);
623fcf5ef2aSThomas Huth 
6247222b94aSDavid Gibson         ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
625fcf5ef2aSThomas Huth     }
626fcf5ef2aSThomas Huth 
6277222b94aSDavid Gibson     return ptex;
628fcf5ef2aSThomas Huth }
629fcf5ef2aSThomas Huth 
630fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
631fcf5ef2aSThomas Huth                                           uint64_t pte0, uint64_t pte1)
632fcf5ef2aSThomas Huth {
633fcf5ef2aSThomas Huth     int i;
634fcf5ef2aSThomas Huth 
635fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
636fcf5ef2aSThomas Huth         return 12;
637fcf5ef2aSThomas Huth     }
638fcf5ef2aSThomas Huth 
639fcf5ef2aSThomas Huth     /*
640fcf5ef2aSThomas Huth      * The encodings in env->sps need to be carefully chosen so that
641fcf5ef2aSThomas Huth      * this gives an unambiguous result.
642fcf5ef2aSThomas Huth      */
643fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
644b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
645fcf5ef2aSThomas Huth         unsigned shift;
646fcf5ef2aSThomas Huth 
647fcf5ef2aSThomas Huth         if (!sps->page_shift) {
648fcf5ef2aSThomas Huth             break;
649fcf5ef2aSThomas Huth         }
650fcf5ef2aSThomas Huth 
651fcf5ef2aSThomas Huth         shift = hpte_page_shift(sps, pte0, pte1);
652fcf5ef2aSThomas Huth         if (shift) {
653fcf5ef2aSThomas Huth             return shift;
654fcf5ef2aSThomas Huth         }
655fcf5ef2aSThomas Huth     }
656fcf5ef2aSThomas Huth 
657fcf5ef2aSThomas Huth     return 0;
658fcf5ef2aSThomas Huth }
659fcf5ef2aSThomas Huth 
6608fe08facSDavid Gibson static void ppc_hash64_set_isi(CPUState *cs, uint64_t error_code)
661fcf5ef2aSThomas Huth {
6628fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
663fcf5ef2aSThomas Huth     bool vpm;
664fcf5ef2aSThomas Huth 
665fcf5ef2aSThomas Huth     if (msr_ir) {
666fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
667fcf5ef2aSThomas Huth     } else {
66850659083SSuraj Jitindar Singh         switch (env->mmu_model) {
66950659083SSuraj Jitindar Singh         case POWERPC_MMU_3_00:
67050659083SSuraj Jitindar Singh             /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
67150659083SSuraj Jitindar Singh             vpm = true;
67250659083SSuraj Jitindar Singh             break;
67350659083SSuraj Jitindar Singh         default:
674fcf5ef2aSThomas Huth             vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM0);
67550659083SSuraj Jitindar Singh             break;
67650659083SSuraj Jitindar Singh         }
677fcf5ef2aSThomas Huth     }
678fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
679fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HISI;
680fcf5ef2aSThomas Huth     } else {
681fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
682fcf5ef2aSThomas Huth     }
683fcf5ef2aSThomas Huth     env->error_code = error_code;
684fcf5ef2aSThomas Huth }
685fcf5ef2aSThomas Huth 
6868fe08facSDavid Gibson static void ppc_hash64_set_dsi(CPUState *cs, uint64_t dar, uint64_t dsisr)
687fcf5ef2aSThomas Huth {
6888fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
689fcf5ef2aSThomas Huth     bool vpm;
690fcf5ef2aSThomas Huth 
691fcf5ef2aSThomas Huth     if (msr_dr) {
692fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
693fcf5ef2aSThomas Huth     } else {
69450659083SSuraj Jitindar Singh         switch (env->mmu_model) {
69550659083SSuraj Jitindar Singh         case POWERPC_MMU_3_00:
69650659083SSuraj Jitindar Singh             /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
69750659083SSuraj Jitindar Singh             vpm = true;
69850659083SSuraj Jitindar Singh             break;
69950659083SSuraj Jitindar Singh         default:
700fcf5ef2aSThomas Huth             vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM0);
70150659083SSuraj Jitindar Singh             break;
70250659083SSuraj Jitindar Singh         }
703fcf5ef2aSThomas Huth     }
704fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
705fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
706fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
707fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
708fcf5ef2aSThomas Huth     } else {
709fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
710fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
711fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
712fcf5ef2aSThomas Huth    }
713fcf5ef2aSThomas Huth     env->error_code = 0;
714fcf5ef2aSThomas Huth }
715fcf5ef2aSThomas Huth 
716fcf5ef2aSThomas Huth 
717fcf5ef2aSThomas Huth int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr,
718fcf5ef2aSThomas Huth                                 int rwx, int mmu_idx)
719fcf5ef2aSThomas Huth {
720fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
721fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
722fcf5ef2aSThomas Huth     ppc_slb_t *slb;
723fcf5ef2aSThomas Huth     unsigned apshift;
7247222b94aSDavid Gibson     hwaddr ptex;
725fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
72607a68f99SSuraj Jitindar Singh     int exec_prot, pp_prot, amr_prot, prot;
727da82c73aSSuraj Jitindar Singh     uint64_t new_pte1;
728fcf5ef2aSThomas Huth     const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC};
729fcf5ef2aSThomas Huth     hwaddr raddr;
730fcf5ef2aSThomas Huth 
731fcf5ef2aSThomas Huth     assert((rwx == 0) || (rwx == 1) || (rwx == 2));
732fcf5ef2aSThomas Huth 
733fcf5ef2aSThomas Huth     /* Note on LPCR usage: 970 uses HID4, but our special variant
734fcf5ef2aSThomas Huth      * of store_spr copies relevant fields into env->spr[SPR_LPCR].
735fcf5ef2aSThomas Huth      * Similarily we filter unimplemented bits when storing into
736fcf5ef2aSThomas Huth      * LPCR depending on the MMU version. This code can thus just
737fcf5ef2aSThomas Huth      * use the LPCR "as-is".
738fcf5ef2aSThomas Huth      */
739fcf5ef2aSThomas Huth 
740fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
741fcf5ef2aSThomas Huth     if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) {
742fcf5ef2aSThomas Huth         /* Translation is supposedly "off"  */
743fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are (mostly) ignored */
744fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
745fcf5ef2aSThomas Huth 
746fcf5ef2aSThomas Huth         /* In HV mode, add HRMOR if top EA bit is clear */
747fcf5ef2aSThomas Huth         if (msr_hv || !env->has_hv_mode) {
748fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
749fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
750fcf5ef2aSThomas Huth             }
751fcf5ef2aSThomas Huth         } else {
752fcf5ef2aSThomas Huth             /* Otherwise, check VPM for RMA vs VRMA */
753fcf5ef2aSThomas Huth             if (env->spr[SPR_LPCR] & LPCR_VPM0) {
754fcf5ef2aSThomas Huth                 slb = &env->vrma_slb;
755fcf5ef2aSThomas Huth                 if (slb->sps) {
756fcf5ef2aSThomas Huth                     goto skip_slb_search;
757fcf5ef2aSThomas Huth                 }
758fcf5ef2aSThomas Huth                 /* Not much else to do here */
759fcf5ef2aSThomas Huth                 cs->exception_index = POWERPC_EXCP_MCHECK;
760fcf5ef2aSThomas Huth                 env->error_code = 0;
761fcf5ef2aSThomas Huth                 return 1;
762fcf5ef2aSThomas Huth             } else if (raddr < env->rmls) {
763fcf5ef2aSThomas Huth                 /* RMA. Check bounds in RMLS */
764fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_RMOR];
765fcf5ef2aSThomas Huth             } else {
766fcf5ef2aSThomas Huth                 /* The access failed, generate the approriate interrupt */
767fcf5ef2aSThomas Huth                 if (rwx == 2) {
7688fe08facSDavid Gibson                     ppc_hash64_set_isi(cs, SRR1_PROTFAULT);
769fcf5ef2aSThomas Huth                 } else {
770da82c73aSSuraj Jitindar Singh                     int dsisr = DSISR_PROTFAULT;
771fcf5ef2aSThomas Huth                     if (rwx == 1) {
772da82c73aSSuraj Jitindar Singh                         dsisr |= DSISR_ISSTORE;
773fcf5ef2aSThomas Huth                     }
7748fe08facSDavid Gibson                     ppc_hash64_set_dsi(cs, eaddr, dsisr);
775fcf5ef2aSThomas Huth                 }
776fcf5ef2aSThomas Huth                 return 1;
777fcf5ef2aSThomas Huth             }
778fcf5ef2aSThomas Huth         }
779fcf5ef2aSThomas Huth         tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
780fcf5ef2aSThomas Huth                      PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx,
781fcf5ef2aSThomas Huth                      TARGET_PAGE_SIZE);
782fcf5ef2aSThomas Huth         return 0;
783fcf5ef2aSThomas Huth     }
784fcf5ef2aSThomas Huth 
785fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
786fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
787fcf5ef2aSThomas Huth     if (!slb) {
788b2899495SSuraj Jitindar Singh         /* No entry found, check if in-memory segment tables are in use */
789ca79b3b7SDavid Gibson         if (ppc64_use_proc_tbl(cpu)) {
790b2899495SSuraj Jitindar Singh             /* TODO - Unsupported */
791b2899495SSuraj Jitindar Singh             error_report("Segment Table Support Unimplemented");
792b2899495SSuraj Jitindar Singh             exit(1);
793b2899495SSuraj Jitindar Singh         }
794b2899495SSuraj Jitindar Singh         /* Segment still not found, generate the appropriate interrupt */
795fcf5ef2aSThomas Huth         if (rwx == 2) {
796fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
797fcf5ef2aSThomas Huth             env->error_code = 0;
798fcf5ef2aSThomas Huth         } else {
799fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
800fcf5ef2aSThomas Huth             env->error_code = 0;
801fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
802fcf5ef2aSThomas Huth         }
803fcf5ef2aSThomas Huth         return 1;
804fcf5ef2aSThomas Huth     }
805fcf5ef2aSThomas Huth 
806fcf5ef2aSThomas Huth skip_slb_search:
807fcf5ef2aSThomas Huth 
808fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
809fcf5ef2aSThomas Huth     if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) {
8108fe08facSDavid Gibson         ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD);
811fcf5ef2aSThomas Huth         return 1;
812fcf5ef2aSThomas Huth     }
813fcf5ef2aSThomas Huth 
814fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
8157222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
8167222b94aSDavid Gibson     if (ptex == -1) {
817fcf5ef2aSThomas Huth         if (rwx == 2) {
8188fe08facSDavid Gibson             ppc_hash64_set_isi(cs, SRR1_NOPTE);
819fcf5ef2aSThomas Huth         } else {
820da82c73aSSuraj Jitindar Singh             int dsisr = DSISR_NOPTE;
821fcf5ef2aSThomas Huth             if (rwx == 1) {
822da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
823fcf5ef2aSThomas Huth             }
8248fe08facSDavid Gibson             ppc_hash64_set_dsi(cs, eaddr, dsisr);
825fcf5ef2aSThomas Huth         }
826fcf5ef2aSThomas Huth         return 1;
827fcf5ef2aSThomas Huth     }
828fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
8297222b94aSDavid Gibson                   "found PTE at index %08" HWADDR_PRIx "\n", ptex);
830fcf5ef2aSThomas Huth 
831fcf5ef2aSThomas Huth     /* 5. Check access permissions */
832fcf5ef2aSThomas Huth 
83307a68f99SSuraj Jitindar Singh     exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
834fcf5ef2aSThomas Huth     pp_prot = ppc_hash64_pte_prot(cpu, slb, pte);
835fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
83607a68f99SSuraj Jitindar Singh     prot = exec_prot & pp_prot & amr_prot;
837fcf5ef2aSThomas Huth 
838fcf5ef2aSThomas Huth     if ((need_prot[rwx] & ~prot) != 0) {
839fcf5ef2aSThomas Huth         /* Access right violation */
840fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
841fcf5ef2aSThomas Huth         if (rwx == 2) {
842a6152b52SSuraj Jitindar Singh             int srr1 = 0;
84307a68f99SSuraj Jitindar Singh             if (PAGE_EXEC & ~exec_prot) {
84407a68f99SSuraj Jitindar Singh                 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
84507a68f99SSuraj Jitindar Singh             } else if (PAGE_EXEC & ~pp_prot) {
846a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
847a6152b52SSuraj Jitindar Singh             }
848a6152b52SSuraj Jitindar Singh             if (PAGE_EXEC & ~amr_prot) {
849a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
850a6152b52SSuraj Jitindar Singh             }
8518fe08facSDavid Gibson             ppc_hash64_set_isi(cs, srr1);
852fcf5ef2aSThomas Huth         } else {
853da82c73aSSuraj Jitindar Singh             int dsisr = 0;
854fcf5ef2aSThomas Huth             if (need_prot[rwx] & ~pp_prot) {
855da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_PROTFAULT;
856fcf5ef2aSThomas Huth             }
857fcf5ef2aSThomas Huth             if (rwx == 1) {
858da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
859fcf5ef2aSThomas Huth             }
860fcf5ef2aSThomas Huth             if (need_prot[rwx] & ~amr_prot) {
861da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_AMR;
862fcf5ef2aSThomas Huth             }
8638fe08facSDavid Gibson             ppc_hash64_set_dsi(cs, eaddr, dsisr);
864fcf5ef2aSThomas Huth         }
865fcf5ef2aSThomas Huth         return 1;
866fcf5ef2aSThomas Huth     }
867fcf5ef2aSThomas Huth 
868fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
869fcf5ef2aSThomas Huth 
870fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
871fcf5ef2aSThomas Huth 
872fcf5ef2aSThomas Huth     new_pte1 = pte.pte1 | HPTE64_R_R; /* set referenced bit */
873fcf5ef2aSThomas Huth     if (rwx == 1) {
874fcf5ef2aSThomas Huth         new_pte1 |= HPTE64_R_C; /* set changed (dirty) bit */
875fcf5ef2aSThomas Huth     } else {
876fcf5ef2aSThomas Huth         /* Treat the page as read-only for now, so that a later write
877fcf5ef2aSThomas Huth          * will pass through this function again to set the C bit */
878fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
879fcf5ef2aSThomas Huth     }
880fcf5ef2aSThomas Huth 
881fcf5ef2aSThomas Huth     if (new_pte1 != pte.pte1) {
8827222b94aSDavid Gibson         ppc_hash64_store_hpte(cpu, ptex, pte.pte0, new_pte1);
883fcf5ef2aSThomas Huth     }
884fcf5ef2aSThomas Huth 
885fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
886fcf5ef2aSThomas Huth 
887fcf5ef2aSThomas Huth     raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
888fcf5ef2aSThomas Huth 
889fcf5ef2aSThomas Huth     tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
890fcf5ef2aSThomas Huth                  prot, mmu_idx, 1ULL << apshift);
891fcf5ef2aSThomas Huth 
892fcf5ef2aSThomas Huth     return 0;
893fcf5ef2aSThomas Huth }
894fcf5ef2aSThomas Huth 
895fcf5ef2aSThomas Huth hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr)
896fcf5ef2aSThomas Huth {
897fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
898fcf5ef2aSThomas Huth     ppc_slb_t *slb;
8997222b94aSDavid Gibson     hwaddr ptex, raddr;
900fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
901fcf5ef2aSThomas Huth     unsigned apshift;
902fcf5ef2aSThomas Huth 
903fcf5ef2aSThomas Huth     /* Handle real mode */
904fcf5ef2aSThomas Huth     if (msr_dr == 0) {
905fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are ignored */
906fcf5ef2aSThomas Huth         raddr = addr & 0x0FFFFFFFFFFFFFFFULL;
907fcf5ef2aSThomas Huth 
908fcf5ef2aSThomas Huth         /* In HV mode, add HRMOR if top EA bit is clear */
909fcf5ef2aSThomas Huth         if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) {
910fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_HRMOR];
911fcf5ef2aSThomas Huth         }
912fcf5ef2aSThomas Huth 
913fcf5ef2aSThomas Huth         /* Otherwise, check VPM for RMA vs VRMA */
914fcf5ef2aSThomas Huth         if (env->spr[SPR_LPCR] & LPCR_VPM0) {
915fcf5ef2aSThomas Huth             slb = &env->vrma_slb;
916fcf5ef2aSThomas Huth             if (!slb->sps) {
917fcf5ef2aSThomas Huth                 return -1;
918fcf5ef2aSThomas Huth             }
919fcf5ef2aSThomas Huth         } else if (raddr < env->rmls) {
920fcf5ef2aSThomas Huth             /* RMA. Check bounds in RMLS */
921fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_RMOR];
922fcf5ef2aSThomas Huth         } else {
923fcf5ef2aSThomas Huth             return -1;
924fcf5ef2aSThomas Huth         }
925fcf5ef2aSThomas Huth     } else {
926fcf5ef2aSThomas Huth         slb = slb_lookup(cpu, addr);
927fcf5ef2aSThomas Huth         if (!slb) {
928fcf5ef2aSThomas Huth             return -1;
929fcf5ef2aSThomas Huth         }
930fcf5ef2aSThomas Huth     }
931fcf5ef2aSThomas Huth 
9327222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift);
9337222b94aSDavid Gibson     if (ptex == -1) {
934fcf5ef2aSThomas Huth         return -1;
935fcf5ef2aSThomas Huth     }
936fcf5ef2aSThomas Huth 
937fcf5ef2aSThomas Huth     return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr)
938fcf5ef2aSThomas Huth         & TARGET_PAGE_MASK;
939fcf5ef2aSThomas Huth }
940fcf5ef2aSThomas Huth 
9417222b94aSDavid Gibson void ppc_hash64_store_hpte(PowerPCCPU *cpu, hwaddr ptex,
9427222b94aSDavid Gibson                            uint64_t pte0, uint64_t pte1)
943fcf5ef2aSThomas Huth {
944e57ca75cSDavid Gibson     hwaddr base = ppc_hash64_hpt_base(cpu);
9457222b94aSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
946fcf5ef2aSThomas Huth 
947e57ca75cSDavid Gibson     if (cpu->vhyp) {
948e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
949e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
950e57ca75cSDavid Gibson         vhc->store_hpte(cpu->vhyp, ptex, pte0, pte1);
951fcf5ef2aSThomas Huth         return;
952fcf5ef2aSThomas Huth     }
953fcf5ef2aSThomas Huth 
95436778660SDavid Gibson     stq_phys(CPU(cpu)->as, base + offset, pte0);
95536778660SDavid Gibson     stq_phys(CPU(cpu)->as, base + offset + HASH_PTE_SIZE_64 / 2, pte1);
956fcf5ef2aSThomas Huth }
957fcf5ef2aSThomas Huth 
9587222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
959fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
960fcf5ef2aSThomas Huth {
961fcf5ef2aSThomas Huth     /*
962fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
963fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
964fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
965fcf5ef2aSThomas Huth      */
966fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
967fcf5ef2aSThomas Huth }
968fcf5ef2aSThomas Huth 
9695ad55315SDavid Gibson static void ppc_hash64_update_rmls(PowerPCCPU *cpu)
970fcf5ef2aSThomas Huth {
9718fe08facSDavid Gibson     CPUPPCState *env = &cpu->env;
972fcf5ef2aSThomas Huth     uint64_t lpcr = env->spr[SPR_LPCR];
973fcf5ef2aSThomas Huth 
974fcf5ef2aSThomas Huth     /*
975fcf5ef2aSThomas Huth      * This is the full 4 bits encoding of POWER8. Previous
976fcf5ef2aSThomas Huth      * CPUs only support a subset of these but the filtering
977fcf5ef2aSThomas Huth      * is done when writing LPCR
978fcf5ef2aSThomas Huth      */
979fcf5ef2aSThomas Huth     switch ((lpcr & LPCR_RMLS) >> LPCR_RMLS_SHIFT) {
980fcf5ef2aSThomas Huth     case 0x8: /* 32MB */
981fcf5ef2aSThomas Huth         env->rmls = 0x2000000ull;
982fcf5ef2aSThomas Huth         break;
983fcf5ef2aSThomas Huth     case 0x3: /* 64MB */
984fcf5ef2aSThomas Huth         env->rmls = 0x4000000ull;
985fcf5ef2aSThomas Huth         break;
986fcf5ef2aSThomas Huth     case 0x7: /* 128MB */
987fcf5ef2aSThomas Huth         env->rmls = 0x8000000ull;
988fcf5ef2aSThomas Huth         break;
989fcf5ef2aSThomas Huth     case 0x4: /* 256MB */
990fcf5ef2aSThomas Huth         env->rmls = 0x10000000ull;
991fcf5ef2aSThomas Huth         break;
992fcf5ef2aSThomas Huth     case 0x2: /* 1GB */
993fcf5ef2aSThomas Huth         env->rmls = 0x40000000ull;
994fcf5ef2aSThomas Huth         break;
995fcf5ef2aSThomas Huth     case 0x1: /* 16GB */
996fcf5ef2aSThomas Huth         env->rmls = 0x400000000ull;
997fcf5ef2aSThomas Huth         break;
998fcf5ef2aSThomas Huth     default:
999fcf5ef2aSThomas Huth         /* What to do here ??? */
1000fcf5ef2aSThomas Huth         env->rmls = 0;
1001fcf5ef2aSThomas Huth     }
1002fcf5ef2aSThomas Huth }
1003fcf5ef2aSThomas Huth 
10045ad55315SDavid Gibson static void ppc_hash64_update_vrma(PowerPCCPU *cpu)
1005fcf5ef2aSThomas Huth {
10068fe08facSDavid Gibson     CPUPPCState *env = &cpu->env;
1007b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
1008fcf5ef2aSThomas Huth     target_ulong esid, vsid, lpcr;
1009fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->vrma_slb;
1010fcf5ef2aSThomas Huth     uint32_t vrmasd;
1011fcf5ef2aSThomas Huth     int i;
1012fcf5ef2aSThomas Huth 
1013fcf5ef2aSThomas Huth     /* First clear it */
1014fcf5ef2aSThomas Huth     slb->esid = slb->vsid = 0;
1015fcf5ef2aSThomas Huth     slb->sps = NULL;
1016fcf5ef2aSThomas Huth 
1017fcf5ef2aSThomas Huth     /* Is VRMA enabled ? */
1018fcf5ef2aSThomas Huth     lpcr = env->spr[SPR_LPCR];
1019fcf5ef2aSThomas Huth     if (!(lpcr & LPCR_VPM0)) {
1020fcf5ef2aSThomas Huth         return;
1021fcf5ef2aSThomas Huth     }
1022fcf5ef2aSThomas Huth 
1023fcf5ef2aSThomas Huth     /* Make one up. Mostly ignore the ESID which will not be
1024fcf5ef2aSThomas Huth      * needed for translation
1025fcf5ef2aSThomas Huth      */
1026fcf5ef2aSThomas Huth     vsid = SLB_VSID_VRMA;
1027fcf5ef2aSThomas Huth     vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
1028fcf5ef2aSThomas Huth     vsid |= (vrmasd << 4) & (SLB_VSID_L | SLB_VSID_LP);
1029fcf5ef2aSThomas Huth     esid = SLB_ESID_V;
1030fcf5ef2aSThomas Huth 
1031fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
1032b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
1033fcf5ef2aSThomas Huth 
1034fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
1035fcf5ef2aSThomas Huth             break;
1036fcf5ef2aSThomas Huth         }
1037fcf5ef2aSThomas Huth 
1038fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
1039fcf5ef2aSThomas Huth             sps = sps1;
1040fcf5ef2aSThomas Huth             break;
1041fcf5ef2aSThomas Huth         }
1042fcf5ef2aSThomas Huth     }
1043fcf5ef2aSThomas Huth 
1044fcf5ef2aSThomas Huth     if (!sps) {
1045fcf5ef2aSThomas Huth         error_report("Bad page size encoding esid 0x"TARGET_FMT_lx
1046fcf5ef2aSThomas Huth                      " vsid 0x"TARGET_FMT_lx, esid, vsid);
1047fcf5ef2aSThomas Huth         return;
1048fcf5ef2aSThomas Huth     }
1049fcf5ef2aSThomas Huth 
1050fcf5ef2aSThomas Huth     slb->vsid = vsid;
1051fcf5ef2aSThomas Huth     slb->esid = esid;
1052fcf5ef2aSThomas Huth     slb->sps = sps;
1053fcf5ef2aSThomas Huth }
1054fcf5ef2aSThomas Huth 
10555ad55315SDavid Gibson void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val)
1056fcf5ef2aSThomas Huth {
10575ad55315SDavid Gibson     CPUPPCState *env = &cpu->env;
1058fcf5ef2aSThomas Huth     uint64_t lpcr = 0;
1059fcf5ef2aSThomas Huth 
1060fcf5ef2aSThomas Huth     /* Filter out bits */
10610941d728SDavid Gibson     switch (env->mmu_model) {
10620941d728SDavid Gibson     case POWERPC_MMU_64B: /* 970 */
1063fcf5ef2aSThomas Huth         if (val & 0x40) {
1064fcf5ef2aSThomas Huth             lpcr |= LPCR_LPES0;
1065fcf5ef2aSThomas Huth         }
1066fcf5ef2aSThomas Huth         if (val & 0x8000000000000000ull) {
1067fcf5ef2aSThomas Huth             lpcr |= LPCR_LPES1;
1068fcf5ef2aSThomas Huth         }
1069fcf5ef2aSThomas Huth         if (val & 0x20) {
1070fcf5ef2aSThomas Huth             lpcr |= (0x4ull << LPCR_RMLS_SHIFT);
1071fcf5ef2aSThomas Huth         }
1072fcf5ef2aSThomas Huth         if (val & 0x4000000000000000ull) {
1073fcf5ef2aSThomas Huth             lpcr |= (0x2ull << LPCR_RMLS_SHIFT);
1074fcf5ef2aSThomas Huth         }
1075fcf5ef2aSThomas Huth         if (val & 0x2000000000000000ull) {
1076fcf5ef2aSThomas Huth             lpcr |= (0x1ull << LPCR_RMLS_SHIFT);
1077fcf5ef2aSThomas Huth         }
1078fcf5ef2aSThomas Huth         env->spr[SPR_RMOR] = ((lpcr >> 41) & 0xffffull) << 26;
1079fcf5ef2aSThomas Huth 
1080fcf5ef2aSThomas Huth         /* XXX We could also write LPID from HID4 here
1081fcf5ef2aSThomas Huth          * but since we don't tag any translation on it
1082fcf5ef2aSThomas Huth          * it doesn't actually matter
1083fcf5ef2aSThomas Huth          */
1084fcf5ef2aSThomas Huth         /* XXX For proper emulation of 970 we also need
1085fcf5ef2aSThomas Huth          * to dig HRMOR out of HID5
1086fcf5ef2aSThomas Huth          */
1087fcf5ef2aSThomas Huth         break;
10880941d728SDavid Gibson     case POWERPC_MMU_2_03: /* P5p */
1089fcf5ef2aSThomas Huth         lpcr = val & (LPCR_RMLS | LPCR_ILE |
1090fcf5ef2aSThomas Huth                       LPCR_LPES0 | LPCR_LPES1 |
1091fcf5ef2aSThomas Huth                       LPCR_RMI | LPCR_HDICE);
1092fcf5ef2aSThomas Huth         break;
10930941d728SDavid Gibson     case POWERPC_MMU_2_06: /* P7 */
1094fcf5ef2aSThomas Huth         lpcr = val & (LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_DPFD |
1095fcf5ef2aSThomas Huth                       LPCR_VRMASD | LPCR_RMLS | LPCR_ILE |
1096fcf5ef2aSThomas Huth                       LPCR_P7_PECE0 | LPCR_P7_PECE1 | LPCR_P7_PECE2 |
1097fcf5ef2aSThomas Huth                       LPCR_MER | LPCR_TC |
1098fcf5ef2aSThomas Huth                       LPCR_LPES0 | LPCR_LPES1 | LPCR_HDICE);
1099fcf5ef2aSThomas Huth         break;
11000941d728SDavid Gibson     case POWERPC_MMU_2_07: /* P8 */
1101fcf5ef2aSThomas Huth         lpcr = val & (LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_KBV |
1102fcf5ef2aSThomas Huth                       LPCR_DPFD | LPCR_VRMASD | LPCR_RMLS | LPCR_ILE |
1103fcf5ef2aSThomas Huth                       LPCR_AIL | LPCR_ONL | LPCR_P8_PECE0 | LPCR_P8_PECE1 |
1104fcf5ef2aSThomas Huth                       LPCR_P8_PECE2 | LPCR_P8_PECE3 | LPCR_P8_PECE4 |
1105fcf5ef2aSThomas Huth                       LPCR_MER | LPCR_TC | LPCR_LPES0 | LPCR_HDICE);
1106fcf5ef2aSThomas Huth         break;
11070941d728SDavid Gibson     case POWERPC_MMU_3_00: /* P9 */
110818aa49ecSSuraj Jitindar Singh         lpcr = val & (LPCR_VPM1 | LPCR_ISL | LPCR_KBV | LPCR_DPFD |
110918aa49ecSSuraj Jitindar Singh                       (LPCR_PECE_U_MASK & LPCR_HVEE) | LPCR_ILE | LPCR_AIL |
111000fd075eSBenjamin Herrenschmidt                       LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR |
111118aa49ecSSuraj Jitindar Singh                       (LPCR_PECE_L_MASK & (LPCR_PDEE | LPCR_HDEE | LPCR_EEE |
111218aa49ecSSuraj Jitindar Singh                       LPCR_DEE | LPCR_OEE)) | LPCR_MER | LPCR_GTSE | LPCR_TC |
111318aa49ecSSuraj Jitindar Singh                       LPCR_HEIC | LPCR_LPES0 | LPCR_HVICE | LPCR_HDICE);
11142b9e0a6bSBenjamin Herrenschmidt         /*
11152b9e0a6bSBenjamin Herrenschmidt          * If we have a virtual hypervisor, we need to bring back RMLS. It
11162b9e0a6bSBenjamin Herrenschmidt          * doesn't exist on an actual P9 but that's all we know how to
11172b9e0a6bSBenjamin Herrenschmidt          * configure with softmmu at the moment
11182b9e0a6bSBenjamin Herrenschmidt          */
11192b9e0a6bSBenjamin Herrenschmidt         if (cpu->vhyp) {
11202b9e0a6bSBenjamin Herrenschmidt             lpcr |= (val & LPCR_RMLS);
11212b9e0a6bSBenjamin Herrenschmidt         }
112218aa49ecSSuraj Jitindar Singh         break;
1123fcf5ef2aSThomas Huth     default:
1124fcf5ef2aSThomas Huth         ;
1125fcf5ef2aSThomas Huth     }
1126fcf5ef2aSThomas Huth     env->spr[SPR_LPCR] = lpcr;
11278fe08facSDavid Gibson     ppc_hash64_update_rmls(cpu);
11288fe08facSDavid Gibson     ppc_hash64_update_vrma(cpu);
1129fcf5ef2aSThomas Huth }
1130a059471dSDavid Gibson 
11315ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val)
11325ad55315SDavid Gibson {
11335ad55315SDavid Gibson     PowerPCCPU *cpu = ppc_env_get_cpu(env);
11345ad55315SDavid Gibson 
11355ad55315SDavid Gibson     ppc_store_lpcr(cpu, val);
11365ad55315SDavid Gibson }
11375ad55315SDavid Gibson 
1138a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu)
1139a059471dSDavid Gibson {
1140a059471dSDavid Gibson     CPUPPCState *env = &cpu->env;
1141a059471dSDavid Gibson     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1142a059471dSDavid Gibson 
114321e405f1SDavid Gibson     if (!pcc->hash64_opts) {
114421e405f1SDavid Gibson         assert(!(env->mmu_model & POWERPC_MMU_64));
114521e405f1SDavid Gibson         return;
114621e405f1SDavid Gibson     }
114721e405f1SDavid Gibson 
114821e405f1SDavid Gibson     cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
114921e405f1SDavid Gibson }
115021e405f1SDavid Gibson 
115121e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu)
115221e405f1SDavid Gibson {
115321e405f1SDavid Gibson     g_free(cpu->hash64_opts);
115421e405f1SDavid Gibson }
115521e405f1SDavid Gibson 
115621e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = {
115758969eeeSDavid Gibson     .flags = 0,
115867d7d66fSDavid Gibson     .slb_size = 64,
1159a059471dSDavid Gibson     .sps = {
1160a059471dSDavid Gibson         { .page_shift = 12, /* 4K */
1161a059471dSDavid Gibson           .slb_enc = 0,
1162a059471dSDavid Gibson           .enc = { { .page_shift = 12, .pte_enc = 0 } }
1163a059471dSDavid Gibson         },
1164a059471dSDavid Gibson         { .page_shift = 24, /* 16M */
1165a059471dSDavid Gibson           .slb_enc = 0x100,
1166a059471dSDavid Gibson           .enc = { { .page_shift = 24, .pte_enc = 0 } }
1167a059471dSDavid Gibson         },
1168a059471dSDavid Gibson     },
1169a059471dSDavid Gibson };
1170b07c59f7SDavid Gibson 
1171b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = {
117226cd35b8SDavid Gibson     .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
117367d7d66fSDavid Gibson     .slb_size = 32,
1174b07c59f7SDavid Gibson     .sps = {
1175b07c59f7SDavid Gibson         {
1176b07c59f7SDavid Gibson             .page_shift = 12, /* 4K */
1177b07c59f7SDavid Gibson             .slb_enc = 0,
1178b07c59f7SDavid Gibson             .enc = { { .page_shift = 12, .pte_enc = 0 },
1179b07c59f7SDavid Gibson                      { .page_shift = 16, .pte_enc = 0x7 },
1180b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x38 }, },
1181b07c59f7SDavid Gibson         },
1182b07c59f7SDavid Gibson         {
1183b07c59f7SDavid Gibson             .page_shift = 16, /* 64K */
1184b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_64K,
1185b07c59f7SDavid Gibson             .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1186b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x8 }, },
1187b07c59f7SDavid Gibson         },
1188b07c59f7SDavid Gibson         {
1189b07c59f7SDavid Gibson             .page_shift = 24, /* 16M */
1190b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16M,
1191b07c59f7SDavid Gibson             .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1192b07c59f7SDavid Gibson         },
1193b07c59f7SDavid Gibson         {
1194b07c59f7SDavid Gibson             .page_shift = 34, /* 16G */
1195b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16G,
1196b07c59f7SDavid Gibson             .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },
1197b07c59f7SDavid Gibson         },
1198b07c59f7SDavid Gibson     }
1199b07c59f7SDavid Gibson };
120027f00f0aSDavid Gibson 
120127f00f0aSDavid Gibson void ppc_hash64_filter_pagesizes(PowerPCCPU *cpu,
120227f00f0aSDavid Gibson                                  bool (*cb)(void *, uint32_t, uint32_t),
120327f00f0aSDavid Gibson                                  void *opaque)
120427f00f0aSDavid Gibson {
120527f00f0aSDavid Gibson     PPCHash64Options *opts = cpu->hash64_opts;
120627f00f0aSDavid Gibson     int i;
120727f00f0aSDavid Gibson     int n = 0;
120827f00f0aSDavid Gibson     bool ci_largepage = false;
120927f00f0aSDavid Gibson 
121027f00f0aSDavid Gibson     assert(opts);
121127f00f0aSDavid Gibson 
121227f00f0aSDavid Gibson     n = 0;
121327f00f0aSDavid Gibson     for (i = 0; i < ARRAY_SIZE(opts->sps); i++) {
121427f00f0aSDavid Gibson         PPCHash64SegmentPageSizes *sps = &opts->sps[i];
121527f00f0aSDavid Gibson         int j;
121627f00f0aSDavid Gibson         int m = 0;
121727f00f0aSDavid Gibson 
121827f00f0aSDavid Gibson         assert(n <= i);
121927f00f0aSDavid Gibson 
122027f00f0aSDavid Gibson         if (!sps->page_shift) {
122127f00f0aSDavid Gibson             break;
122227f00f0aSDavid Gibson         }
122327f00f0aSDavid Gibson 
122427f00f0aSDavid Gibson         for (j = 0; j < ARRAY_SIZE(sps->enc); j++) {
122527f00f0aSDavid Gibson             PPCHash64PageSize *ps = &sps->enc[j];
122627f00f0aSDavid Gibson 
122727f00f0aSDavid Gibson             assert(m <= j);
122827f00f0aSDavid Gibson             if (!ps->page_shift) {
122927f00f0aSDavid Gibson                 break;
123027f00f0aSDavid Gibson             }
123127f00f0aSDavid Gibson 
123227f00f0aSDavid Gibson             if (cb(opaque, sps->page_shift, ps->page_shift)) {
123327f00f0aSDavid Gibson                 if (ps->page_shift >= 16) {
123427f00f0aSDavid Gibson                     ci_largepage = true;
123527f00f0aSDavid Gibson                 }
123627f00f0aSDavid Gibson                 sps->enc[m++] = *ps;
123727f00f0aSDavid Gibson             }
123827f00f0aSDavid Gibson         }
123927f00f0aSDavid Gibson 
124027f00f0aSDavid Gibson         /* Clear rest of the row */
124127f00f0aSDavid Gibson         for (j = m; j < ARRAY_SIZE(sps->enc); j++) {
124227f00f0aSDavid Gibson             memset(&sps->enc[j], 0, sizeof(sps->enc[j]));
124327f00f0aSDavid Gibson         }
124427f00f0aSDavid Gibson 
124527f00f0aSDavid Gibson         if (m) {
124627f00f0aSDavid Gibson             n++;
124727f00f0aSDavid Gibson         }
124827f00f0aSDavid Gibson     }
124927f00f0aSDavid Gibson 
125027f00f0aSDavid Gibson     /* Clear the rest of the table */
125127f00f0aSDavid Gibson     for (i = n; i < ARRAY_SIZE(opts->sps); i++) {
125227f00f0aSDavid Gibson         memset(&opts->sps[i], 0, sizeof(opts->sps[i]));
125327f00f0aSDavid Gibson     }
125427f00f0aSDavid Gibson 
125527f00f0aSDavid Gibson     if (!ci_largepage) {
125627f00f0aSDavid Gibson         opts->flags &= ~PPC_HASH64_CI_LARGEPAGE;
125727f00f0aSDavid Gibson     }
125827f00f0aSDavid Gibson }
1259