xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision 5ad553154d1137fbee9ec92b991e52123fa9bcd8)
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 
434e57ca75cSDavid Gibson     hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false);
4357222b94aSDavid Gibson     if (plen < (n * HASH_PTE_SIZE_64)) {
4367222b94aSDavid Gibson         hw_error("%s: Unable to map all requested HPTEs\n", __func__);
437fcf5ef2aSThomas Huth     }
4387222b94aSDavid Gibson     return hptes;
439fcf5ef2aSThomas Huth }
440fcf5ef2aSThomas Huth 
4417222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes,
4427222b94aSDavid Gibson                             hwaddr ptex, int n)
443fcf5ef2aSThomas Huth {
444e57ca75cSDavid Gibson     if (cpu->vhyp) {
445e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
446e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
447e57ca75cSDavid Gibson         vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n);
448e57ca75cSDavid Gibson         return;
449e57ca75cSDavid Gibson     }
450e57ca75cSDavid Gibson 
4517222b94aSDavid Gibson     address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64,
4527222b94aSDavid Gibson                         false, n * HASH_PTE_SIZE_64);
453fcf5ef2aSThomas Huth }
454fcf5ef2aSThomas Huth 
455b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps,
456fcf5ef2aSThomas Huth                                 uint64_t pte0, uint64_t pte1)
457fcf5ef2aSThomas Huth {
458fcf5ef2aSThomas Huth     int i;
459fcf5ef2aSThomas Huth 
460fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
461fcf5ef2aSThomas Huth         if (sps->page_shift != 12) {
462fcf5ef2aSThomas Huth             /* 4kiB page in a non 4kiB segment */
463fcf5ef2aSThomas Huth             return 0;
464fcf5ef2aSThomas Huth         }
465fcf5ef2aSThomas Huth         /* Normal 4kiB page */
466fcf5ef2aSThomas Huth         return 12;
467fcf5ef2aSThomas Huth     }
468fcf5ef2aSThomas Huth 
469fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
470b07c59f7SDavid Gibson         const PPCHash64PageSize *ps = &sps->enc[i];
471fcf5ef2aSThomas Huth         uint64_t mask;
472fcf5ef2aSThomas Huth 
473fcf5ef2aSThomas Huth         if (!ps->page_shift) {
474fcf5ef2aSThomas Huth             break;
475fcf5ef2aSThomas Huth         }
476fcf5ef2aSThomas Huth 
477fcf5ef2aSThomas Huth         if (ps->page_shift == 12) {
478fcf5ef2aSThomas Huth             /* L bit is set so this can't be a 4kiB page */
479fcf5ef2aSThomas Huth             continue;
480fcf5ef2aSThomas Huth         }
481fcf5ef2aSThomas Huth 
482fcf5ef2aSThomas Huth         mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
483fcf5ef2aSThomas Huth 
484fcf5ef2aSThomas Huth         if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
485fcf5ef2aSThomas Huth             return ps->page_shift;
486fcf5ef2aSThomas Huth         }
487fcf5ef2aSThomas Huth     }
488fcf5ef2aSThomas Huth 
489fcf5ef2aSThomas Huth     return 0; /* Bad page size encoding */
490fcf5ef2aSThomas Huth }
491fcf5ef2aSThomas Huth 
492fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
493b07c59f7SDavid Gibson                                      const PPCHash64SegmentPageSizes *sps,
494fcf5ef2aSThomas Huth                                      target_ulong ptem,
495fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
496fcf5ef2aSThomas Huth {
497fcf5ef2aSThomas Huth     int i;
4987222b94aSDavid Gibson     const ppc_hash_pte64_t *pteg;
499fcf5ef2aSThomas Huth     target_ulong pte0, pte1;
5007222b94aSDavid Gibson     target_ulong ptex;
501fcf5ef2aSThomas Huth 
50236778660SDavid Gibson     ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP;
5037222b94aSDavid Gibson     pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
5047222b94aSDavid Gibson     if (!pteg) {
505fcf5ef2aSThomas Huth         return -1;
506fcf5ef2aSThomas Huth     }
507fcf5ef2aSThomas Huth     for (i = 0; i < HPTES_PER_GROUP; i++) {
5087222b94aSDavid Gibson         pte0 = ppc_hash64_hpte0(cpu, pteg, i);
5097222b94aSDavid Gibson         pte1 = ppc_hash64_hpte1(cpu, pteg, i);
510fcf5ef2aSThomas Huth 
511fcf5ef2aSThomas Huth         /* This compares V, B, H (secondary) and the AVPN */
512fcf5ef2aSThomas Huth         if (HPTE64_V_COMPARE(pte0, ptem)) {
513fcf5ef2aSThomas Huth             *pshift = hpte_page_shift(sps, pte0, pte1);
514fcf5ef2aSThomas Huth             /*
515fcf5ef2aSThomas Huth              * If there is no match, ignore the PTE, it could simply
516fcf5ef2aSThomas Huth              * be for a different segment size encoding and the
517fcf5ef2aSThomas Huth              * architecture specifies we should not match. Linux will
518fcf5ef2aSThomas Huth              * potentially leave behind PTEs for the wrong base page
519fcf5ef2aSThomas Huth              * size when demoting segments.
520fcf5ef2aSThomas Huth              */
521fcf5ef2aSThomas Huth             if (*pshift == 0) {
522fcf5ef2aSThomas Huth                 continue;
523fcf5ef2aSThomas Huth             }
524fcf5ef2aSThomas Huth             /* We don't do anything with pshift yet as qemu TLB only deals
525fcf5ef2aSThomas Huth              * with 4K pages anyway
526fcf5ef2aSThomas Huth              */
527fcf5ef2aSThomas Huth             pte->pte0 = pte0;
528fcf5ef2aSThomas Huth             pte->pte1 = pte1;
5297222b94aSDavid Gibson             ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
5307222b94aSDavid Gibson             return ptex + i;
531fcf5ef2aSThomas Huth         }
532fcf5ef2aSThomas Huth     }
5337222b94aSDavid Gibson     ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
534fcf5ef2aSThomas Huth     /*
535fcf5ef2aSThomas Huth      * We didn't find a valid entry.
536fcf5ef2aSThomas Huth      */
537fcf5ef2aSThomas Huth     return -1;
538fcf5ef2aSThomas Huth }
539fcf5ef2aSThomas Huth 
540fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
541fcf5ef2aSThomas Huth                                      ppc_slb_t *slb, target_ulong eaddr,
542fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
543fcf5ef2aSThomas Huth {
544fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
5457222b94aSDavid Gibson     hwaddr hash, ptex;
546fcf5ef2aSThomas Huth     uint64_t vsid, epnmask, epn, ptem;
547b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = slb->sps;
548fcf5ef2aSThomas Huth 
549fcf5ef2aSThomas Huth     /* The SLB store path should prevent any bad page size encodings
550fcf5ef2aSThomas Huth      * getting in there, so: */
551fcf5ef2aSThomas Huth     assert(sps);
552fcf5ef2aSThomas Huth 
553fcf5ef2aSThomas Huth     /* If ISL is set in LPCR we need to clamp the page size to 4K */
554fcf5ef2aSThomas Huth     if (env->spr[SPR_LPCR] & LPCR_ISL) {
555fcf5ef2aSThomas Huth         /* We assume that when using TCG, 4k is first entry of SPS */
556b07c59f7SDavid Gibson         sps = &cpu->hash64_opts->sps[0];
557fcf5ef2aSThomas Huth         assert(sps->page_shift == 12);
558fcf5ef2aSThomas Huth     }
559fcf5ef2aSThomas Huth 
560fcf5ef2aSThomas Huth     epnmask = ~((1ULL << sps->page_shift) - 1);
561fcf5ef2aSThomas Huth 
562fcf5ef2aSThomas Huth     if (slb->vsid & SLB_VSID_B) {
563fcf5ef2aSThomas Huth         /* 1TB segment */
564fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
565fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
566fcf5ef2aSThomas Huth         hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
567fcf5ef2aSThomas Huth     } else {
568fcf5ef2aSThomas Huth         /* 256M segment */
569fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
570fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
571fcf5ef2aSThomas Huth         hash = vsid ^ (epn >> sps->page_shift);
572fcf5ef2aSThomas Huth     }
573fcf5ef2aSThomas Huth     ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
574fcf5ef2aSThomas Huth     ptem |= HPTE64_V_VALID;
575fcf5ef2aSThomas Huth 
576fcf5ef2aSThomas Huth     /* Page address translation */
577fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
578fcf5ef2aSThomas Huth             "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
579fcf5ef2aSThomas Huth             " hash " TARGET_FMT_plx "\n",
58036778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash);
581fcf5ef2aSThomas Huth 
582fcf5ef2aSThomas Huth     /* Primary PTEG lookup */
583fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
584fcf5ef2aSThomas Huth             "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
585fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
586fcf5ef2aSThomas Huth             " hash=" TARGET_FMT_plx "\n",
58736778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu),
58836778660SDavid Gibson             vsid, ptem,  hash);
5897222b94aSDavid Gibson     ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
590fcf5ef2aSThomas Huth 
5917222b94aSDavid Gibson     if (ptex == -1) {
592fcf5ef2aSThomas Huth         /* Secondary PTEG lookup */
593fcf5ef2aSThomas Huth         ptem |= HPTE64_V_SECONDARY;
594fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU,
595fcf5ef2aSThomas Huth                 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
596fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
59736778660SDavid Gibson                 " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu),
59836778660SDavid Gibson                 ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash);
599fcf5ef2aSThomas Huth 
6007222b94aSDavid Gibson         ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
601fcf5ef2aSThomas Huth     }
602fcf5ef2aSThomas Huth 
6037222b94aSDavid Gibson     return ptex;
604fcf5ef2aSThomas Huth }
605fcf5ef2aSThomas Huth 
606fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
607fcf5ef2aSThomas Huth                                           uint64_t pte0, uint64_t pte1)
608fcf5ef2aSThomas Huth {
609fcf5ef2aSThomas Huth     int i;
610fcf5ef2aSThomas Huth 
611fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
612fcf5ef2aSThomas Huth         return 12;
613fcf5ef2aSThomas Huth     }
614fcf5ef2aSThomas Huth 
615fcf5ef2aSThomas Huth     /*
616fcf5ef2aSThomas Huth      * The encodings in env->sps need to be carefully chosen so that
617fcf5ef2aSThomas Huth      * this gives an unambiguous result.
618fcf5ef2aSThomas Huth      */
619fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
620b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
621fcf5ef2aSThomas Huth         unsigned shift;
622fcf5ef2aSThomas Huth 
623fcf5ef2aSThomas Huth         if (!sps->page_shift) {
624fcf5ef2aSThomas Huth             break;
625fcf5ef2aSThomas Huth         }
626fcf5ef2aSThomas Huth 
627fcf5ef2aSThomas Huth         shift = hpte_page_shift(sps, pte0, pte1);
628fcf5ef2aSThomas Huth         if (shift) {
629fcf5ef2aSThomas Huth             return shift;
630fcf5ef2aSThomas Huth         }
631fcf5ef2aSThomas Huth     }
632fcf5ef2aSThomas Huth 
633fcf5ef2aSThomas Huth     return 0;
634fcf5ef2aSThomas Huth }
635fcf5ef2aSThomas Huth 
6368fe08facSDavid Gibson static void ppc_hash64_set_isi(CPUState *cs, uint64_t error_code)
637fcf5ef2aSThomas Huth {
6388fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
639fcf5ef2aSThomas Huth     bool vpm;
640fcf5ef2aSThomas Huth 
641fcf5ef2aSThomas Huth     if (msr_ir) {
642fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
643fcf5ef2aSThomas Huth     } else {
64450659083SSuraj Jitindar Singh         switch (env->mmu_model) {
64550659083SSuraj Jitindar Singh         case POWERPC_MMU_3_00:
64650659083SSuraj Jitindar Singh             /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
64750659083SSuraj Jitindar Singh             vpm = true;
64850659083SSuraj Jitindar Singh             break;
64950659083SSuraj Jitindar Singh         default:
650fcf5ef2aSThomas Huth             vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM0);
65150659083SSuraj Jitindar Singh             break;
65250659083SSuraj Jitindar Singh         }
653fcf5ef2aSThomas Huth     }
654fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
655fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HISI;
656fcf5ef2aSThomas Huth     } else {
657fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
658fcf5ef2aSThomas Huth     }
659fcf5ef2aSThomas Huth     env->error_code = error_code;
660fcf5ef2aSThomas Huth }
661fcf5ef2aSThomas Huth 
6628fe08facSDavid Gibson static void ppc_hash64_set_dsi(CPUState *cs, uint64_t dar, uint64_t dsisr)
663fcf5ef2aSThomas Huth {
6648fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
665fcf5ef2aSThomas Huth     bool vpm;
666fcf5ef2aSThomas Huth 
667fcf5ef2aSThomas Huth     if (msr_dr) {
668fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
669fcf5ef2aSThomas Huth     } else {
67050659083SSuraj Jitindar Singh         switch (env->mmu_model) {
67150659083SSuraj Jitindar Singh         case POWERPC_MMU_3_00:
67250659083SSuraj Jitindar Singh             /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
67350659083SSuraj Jitindar Singh             vpm = true;
67450659083SSuraj Jitindar Singh             break;
67550659083SSuraj Jitindar Singh         default:
676fcf5ef2aSThomas Huth             vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM0);
67750659083SSuraj Jitindar Singh             break;
67850659083SSuraj Jitindar Singh         }
679fcf5ef2aSThomas Huth     }
680fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
681fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
682fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
683fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
684fcf5ef2aSThomas Huth     } else {
685fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
686fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
687fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
688fcf5ef2aSThomas Huth    }
689fcf5ef2aSThomas Huth     env->error_code = 0;
690fcf5ef2aSThomas Huth }
691fcf5ef2aSThomas Huth 
692fcf5ef2aSThomas Huth 
693fcf5ef2aSThomas Huth int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr,
694fcf5ef2aSThomas Huth                                 int rwx, int mmu_idx)
695fcf5ef2aSThomas Huth {
696fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
697fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
698fcf5ef2aSThomas Huth     ppc_slb_t *slb;
699fcf5ef2aSThomas Huth     unsigned apshift;
7007222b94aSDavid Gibson     hwaddr ptex;
701fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
70207a68f99SSuraj Jitindar Singh     int exec_prot, pp_prot, amr_prot, prot;
703da82c73aSSuraj Jitindar Singh     uint64_t new_pte1;
704fcf5ef2aSThomas Huth     const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC};
705fcf5ef2aSThomas Huth     hwaddr raddr;
706fcf5ef2aSThomas Huth 
707fcf5ef2aSThomas Huth     assert((rwx == 0) || (rwx == 1) || (rwx == 2));
708fcf5ef2aSThomas Huth 
709fcf5ef2aSThomas Huth     /* Note on LPCR usage: 970 uses HID4, but our special variant
710fcf5ef2aSThomas Huth      * of store_spr copies relevant fields into env->spr[SPR_LPCR].
711fcf5ef2aSThomas Huth      * Similarily we filter unimplemented bits when storing into
712fcf5ef2aSThomas Huth      * LPCR depending on the MMU version. This code can thus just
713fcf5ef2aSThomas Huth      * use the LPCR "as-is".
714fcf5ef2aSThomas Huth      */
715fcf5ef2aSThomas Huth 
716fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
717fcf5ef2aSThomas Huth     if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) {
718fcf5ef2aSThomas Huth         /* Translation is supposedly "off"  */
719fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are (mostly) ignored */
720fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
721fcf5ef2aSThomas Huth 
722fcf5ef2aSThomas Huth         /* In HV mode, add HRMOR if top EA bit is clear */
723fcf5ef2aSThomas Huth         if (msr_hv || !env->has_hv_mode) {
724fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
725fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
726fcf5ef2aSThomas Huth             }
727fcf5ef2aSThomas Huth         } else {
728fcf5ef2aSThomas Huth             /* Otherwise, check VPM for RMA vs VRMA */
729fcf5ef2aSThomas Huth             if (env->spr[SPR_LPCR] & LPCR_VPM0) {
730fcf5ef2aSThomas Huth                 slb = &env->vrma_slb;
731fcf5ef2aSThomas Huth                 if (slb->sps) {
732fcf5ef2aSThomas Huth                     goto skip_slb_search;
733fcf5ef2aSThomas Huth                 }
734fcf5ef2aSThomas Huth                 /* Not much else to do here */
735fcf5ef2aSThomas Huth                 cs->exception_index = POWERPC_EXCP_MCHECK;
736fcf5ef2aSThomas Huth                 env->error_code = 0;
737fcf5ef2aSThomas Huth                 return 1;
738fcf5ef2aSThomas Huth             } else if (raddr < env->rmls) {
739fcf5ef2aSThomas Huth                 /* RMA. Check bounds in RMLS */
740fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_RMOR];
741fcf5ef2aSThomas Huth             } else {
742fcf5ef2aSThomas Huth                 /* The access failed, generate the approriate interrupt */
743fcf5ef2aSThomas Huth                 if (rwx == 2) {
7448fe08facSDavid Gibson                     ppc_hash64_set_isi(cs, SRR1_PROTFAULT);
745fcf5ef2aSThomas Huth                 } else {
746da82c73aSSuraj Jitindar Singh                     int dsisr = DSISR_PROTFAULT;
747fcf5ef2aSThomas Huth                     if (rwx == 1) {
748da82c73aSSuraj Jitindar Singh                         dsisr |= DSISR_ISSTORE;
749fcf5ef2aSThomas Huth                     }
7508fe08facSDavid Gibson                     ppc_hash64_set_dsi(cs, eaddr, dsisr);
751fcf5ef2aSThomas Huth                 }
752fcf5ef2aSThomas Huth                 return 1;
753fcf5ef2aSThomas Huth             }
754fcf5ef2aSThomas Huth         }
755fcf5ef2aSThomas Huth         tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
756fcf5ef2aSThomas Huth                      PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx,
757fcf5ef2aSThomas Huth                      TARGET_PAGE_SIZE);
758fcf5ef2aSThomas Huth         return 0;
759fcf5ef2aSThomas Huth     }
760fcf5ef2aSThomas Huth 
761fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
762fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
763fcf5ef2aSThomas Huth     if (!slb) {
764b2899495SSuraj Jitindar Singh         /* No entry found, check if in-memory segment tables are in use */
765ca79b3b7SDavid Gibson         if (ppc64_use_proc_tbl(cpu)) {
766b2899495SSuraj Jitindar Singh             /* TODO - Unsupported */
767b2899495SSuraj Jitindar Singh             error_report("Segment Table Support Unimplemented");
768b2899495SSuraj Jitindar Singh             exit(1);
769b2899495SSuraj Jitindar Singh         }
770b2899495SSuraj Jitindar Singh         /* Segment still not found, generate the appropriate interrupt */
771fcf5ef2aSThomas Huth         if (rwx == 2) {
772fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
773fcf5ef2aSThomas Huth             env->error_code = 0;
774fcf5ef2aSThomas Huth         } else {
775fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
776fcf5ef2aSThomas Huth             env->error_code = 0;
777fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
778fcf5ef2aSThomas Huth         }
779fcf5ef2aSThomas Huth         return 1;
780fcf5ef2aSThomas Huth     }
781fcf5ef2aSThomas Huth 
782fcf5ef2aSThomas Huth skip_slb_search:
783fcf5ef2aSThomas Huth 
784fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
785fcf5ef2aSThomas Huth     if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) {
7868fe08facSDavid Gibson         ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD);
787fcf5ef2aSThomas Huth         return 1;
788fcf5ef2aSThomas Huth     }
789fcf5ef2aSThomas Huth 
790fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
7917222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
7927222b94aSDavid Gibson     if (ptex == -1) {
793fcf5ef2aSThomas Huth         if (rwx == 2) {
7948fe08facSDavid Gibson             ppc_hash64_set_isi(cs, SRR1_NOPTE);
795fcf5ef2aSThomas Huth         } else {
796da82c73aSSuraj Jitindar Singh             int dsisr = DSISR_NOPTE;
797fcf5ef2aSThomas Huth             if (rwx == 1) {
798da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
799fcf5ef2aSThomas Huth             }
8008fe08facSDavid Gibson             ppc_hash64_set_dsi(cs, eaddr, dsisr);
801fcf5ef2aSThomas Huth         }
802fcf5ef2aSThomas Huth         return 1;
803fcf5ef2aSThomas Huth     }
804fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
8057222b94aSDavid Gibson                   "found PTE at index %08" HWADDR_PRIx "\n", ptex);
806fcf5ef2aSThomas Huth 
807fcf5ef2aSThomas Huth     /* 5. Check access permissions */
808fcf5ef2aSThomas Huth 
80907a68f99SSuraj Jitindar Singh     exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
810fcf5ef2aSThomas Huth     pp_prot = ppc_hash64_pte_prot(cpu, slb, pte);
811fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
81207a68f99SSuraj Jitindar Singh     prot = exec_prot & pp_prot & amr_prot;
813fcf5ef2aSThomas Huth 
814fcf5ef2aSThomas Huth     if ((need_prot[rwx] & ~prot) != 0) {
815fcf5ef2aSThomas Huth         /* Access right violation */
816fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
817fcf5ef2aSThomas Huth         if (rwx == 2) {
818a6152b52SSuraj Jitindar Singh             int srr1 = 0;
81907a68f99SSuraj Jitindar Singh             if (PAGE_EXEC & ~exec_prot) {
82007a68f99SSuraj Jitindar Singh                 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
82107a68f99SSuraj Jitindar Singh             } else if (PAGE_EXEC & ~pp_prot) {
822a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
823a6152b52SSuraj Jitindar Singh             }
824a6152b52SSuraj Jitindar Singh             if (PAGE_EXEC & ~amr_prot) {
825a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
826a6152b52SSuraj Jitindar Singh             }
8278fe08facSDavid Gibson             ppc_hash64_set_isi(cs, srr1);
828fcf5ef2aSThomas Huth         } else {
829da82c73aSSuraj Jitindar Singh             int dsisr = 0;
830fcf5ef2aSThomas Huth             if (need_prot[rwx] & ~pp_prot) {
831da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_PROTFAULT;
832fcf5ef2aSThomas Huth             }
833fcf5ef2aSThomas Huth             if (rwx == 1) {
834da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
835fcf5ef2aSThomas Huth             }
836fcf5ef2aSThomas Huth             if (need_prot[rwx] & ~amr_prot) {
837da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_AMR;
838fcf5ef2aSThomas Huth             }
8398fe08facSDavid Gibson             ppc_hash64_set_dsi(cs, eaddr, dsisr);
840fcf5ef2aSThomas Huth         }
841fcf5ef2aSThomas Huth         return 1;
842fcf5ef2aSThomas Huth     }
843fcf5ef2aSThomas Huth 
844fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
845fcf5ef2aSThomas Huth 
846fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
847fcf5ef2aSThomas Huth 
848fcf5ef2aSThomas Huth     new_pte1 = pte.pte1 | HPTE64_R_R; /* set referenced bit */
849fcf5ef2aSThomas Huth     if (rwx == 1) {
850fcf5ef2aSThomas Huth         new_pte1 |= HPTE64_R_C; /* set changed (dirty) bit */
851fcf5ef2aSThomas Huth     } else {
852fcf5ef2aSThomas Huth         /* Treat the page as read-only for now, so that a later write
853fcf5ef2aSThomas Huth          * will pass through this function again to set the C bit */
854fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
855fcf5ef2aSThomas Huth     }
856fcf5ef2aSThomas Huth 
857fcf5ef2aSThomas Huth     if (new_pte1 != pte.pte1) {
8587222b94aSDavid Gibson         ppc_hash64_store_hpte(cpu, ptex, pte.pte0, new_pte1);
859fcf5ef2aSThomas Huth     }
860fcf5ef2aSThomas Huth 
861fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
862fcf5ef2aSThomas Huth 
863fcf5ef2aSThomas Huth     raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
864fcf5ef2aSThomas Huth 
865fcf5ef2aSThomas Huth     tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
866fcf5ef2aSThomas Huth                  prot, mmu_idx, 1ULL << apshift);
867fcf5ef2aSThomas Huth 
868fcf5ef2aSThomas Huth     return 0;
869fcf5ef2aSThomas Huth }
870fcf5ef2aSThomas Huth 
871fcf5ef2aSThomas Huth hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr)
872fcf5ef2aSThomas Huth {
873fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
874fcf5ef2aSThomas Huth     ppc_slb_t *slb;
8757222b94aSDavid Gibson     hwaddr ptex, raddr;
876fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
877fcf5ef2aSThomas Huth     unsigned apshift;
878fcf5ef2aSThomas Huth 
879fcf5ef2aSThomas Huth     /* Handle real mode */
880fcf5ef2aSThomas Huth     if (msr_dr == 0) {
881fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are ignored */
882fcf5ef2aSThomas Huth         raddr = addr & 0x0FFFFFFFFFFFFFFFULL;
883fcf5ef2aSThomas Huth 
884fcf5ef2aSThomas Huth         /* In HV mode, add HRMOR if top EA bit is clear */
885fcf5ef2aSThomas Huth         if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) {
886fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_HRMOR];
887fcf5ef2aSThomas Huth         }
888fcf5ef2aSThomas Huth 
889fcf5ef2aSThomas Huth         /* Otherwise, check VPM for RMA vs VRMA */
890fcf5ef2aSThomas Huth         if (env->spr[SPR_LPCR] & LPCR_VPM0) {
891fcf5ef2aSThomas Huth             slb = &env->vrma_slb;
892fcf5ef2aSThomas Huth             if (!slb->sps) {
893fcf5ef2aSThomas Huth                 return -1;
894fcf5ef2aSThomas Huth             }
895fcf5ef2aSThomas Huth         } else if (raddr < env->rmls) {
896fcf5ef2aSThomas Huth             /* RMA. Check bounds in RMLS */
897fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_RMOR];
898fcf5ef2aSThomas Huth         } else {
899fcf5ef2aSThomas Huth             return -1;
900fcf5ef2aSThomas Huth         }
901fcf5ef2aSThomas Huth     } else {
902fcf5ef2aSThomas Huth         slb = slb_lookup(cpu, addr);
903fcf5ef2aSThomas Huth         if (!slb) {
904fcf5ef2aSThomas Huth             return -1;
905fcf5ef2aSThomas Huth         }
906fcf5ef2aSThomas Huth     }
907fcf5ef2aSThomas Huth 
9087222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift);
9097222b94aSDavid Gibson     if (ptex == -1) {
910fcf5ef2aSThomas Huth         return -1;
911fcf5ef2aSThomas Huth     }
912fcf5ef2aSThomas Huth 
913fcf5ef2aSThomas Huth     return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr)
914fcf5ef2aSThomas Huth         & TARGET_PAGE_MASK;
915fcf5ef2aSThomas Huth }
916fcf5ef2aSThomas Huth 
9177222b94aSDavid Gibson void ppc_hash64_store_hpte(PowerPCCPU *cpu, hwaddr ptex,
9187222b94aSDavid Gibson                            uint64_t pte0, uint64_t pte1)
919fcf5ef2aSThomas Huth {
920e57ca75cSDavid Gibson     hwaddr base = ppc_hash64_hpt_base(cpu);
9217222b94aSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
922fcf5ef2aSThomas Huth 
923e57ca75cSDavid Gibson     if (cpu->vhyp) {
924e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
925e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
926e57ca75cSDavid Gibson         vhc->store_hpte(cpu->vhyp, ptex, pte0, pte1);
927fcf5ef2aSThomas Huth         return;
928fcf5ef2aSThomas Huth     }
929fcf5ef2aSThomas Huth 
93036778660SDavid Gibson     stq_phys(CPU(cpu)->as, base + offset, pte0);
93136778660SDavid Gibson     stq_phys(CPU(cpu)->as, base + offset + HASH_PTE_SIZE_64 / 2, pte1);
932fcf5ef2aSThomas Huth }
933fcf5ef2aSThomas Huth 
9347222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
935fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
936fcf5ef2aSThomas Huth {
937fcf5ef2aSThomas Huth     /*
938fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
939fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
940fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
941fcf5ef2aSThomas Huth      */
942fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
943fcf5ef2aSThomas Huth }
944fcf5ef2aSThomas Huth 
945*5ad55315SDavid Gibson static void ppc_hash64_update_rmls(PowerPCCPU *cpu)
946fcf5ef2aSThomas Huth {
9478fe08facSDavid Gibson     CPUPPCState *env = &cpu->env;
948fcf5ef2aSThomas Huth     uint64_t lpcr = env->spr[SPR_LPCR];
949fcf5ef2aSThomas Huth 
950fcf5ef2aSThomas Huth     /*
951fcf5ef2aSThomas Huth      * This is the full 4 bits encoding of POWER8. Previous
952fcf5ef2aSThomas Huth      * CPUs only support a subset of these but the filtering
953fcf5ef2aSThomas Huth      * is done when writing LPCR
954fcf5ef2aSThomas Huth      */
955fcf5ef2aSThomas Huth     switch ((lpcr & LPCR_RMLS) >> LPCR_RMLS_SHIFT) {
956fcf5ef2aSThomas Huth     case 0x8: /* 32MB */
957fcf5ef2aSThomas Huth         env->rmls = 0x2000000ull;
958fcf5ef2aSThomas Huth         break;
959fcf5ef2aSThomas Huth     case 0x3: /* 64MB */
960fcf5ef2aSThomas Huth         env->rmls = 0x4000000ull;
961fcf5ef2aSThomas Huth         break;
962fcf5ef2aSThomas Huth     case 0x7: /* 128MB */
963fcf5ef2aSThomas Huth         env->rmls = 0x8000000ull;
964fcf5ef2aSThomas Huth         break;
965fcf5ef2aSThomas Huth     case 0x4: /* 256MB */
966fcf5ef2aSThomas Huth         env->rmls = 0x10000000ull;
967fcf5ef2aSThomas Huth         break;
968fcf5ef2aSThomas Huth     case 0x2: /* 1GB */
969fcf5ef2aSThomas Huth         env->rmls = 0x40000000ull;
970fcf5ef2aSThomas Huth         break;
971fcf5ef2aSThomas Huth     case 0x1: /* 16GB */
972fcf5ef2aSThomas Huth         env->rmls = 0x400000000ull;
973fcf5ef2aSThomas Huth         break;
974fcf5ef2aSThomas Huth     default:
975fcf5ef2aSThomas Huth         /* What to do here ??? */
976fcf5ef2aSThomas Huth         env->rmls = 0;
977fcf5ef2aSThomas Huth     }
978fcf5ef2aSThomas Huth }
979fcf5ef2aSThomas Huth 
980*5ad55315SDavid Gibson static void ppc_hash64_update_vrma(PowerPCCPU *cpu)
981fcf5ef2aSThomas Huth {
9828fe08facSDavid Gibson     CPUPPCState *env = &cpu->env;
983b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
984fcf5ef2aSThomas Huth     target_ulong esid, vsid, lpcr;
985fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->vrma_slb;
986fcf5ef2aSThomas Huth     uint32_t vrmasd;
987fcf5ef2aSThomas Huth     int i;
988fcf5ef2aSThomas Huth 
989fcf5ef2aSThomas Huth     /* First clear it */
990fcf5ef2aSThomas Huth     slb->esid = slb->vsid = 0;
991fcf5ef2aSThomas Huth     slb->sps = NULL;
992fcf5ef2aSThomas Huth 
993fcf5ef2aSThomas Huth     /* Is VRMA enabled ? */
994fcf5ef2aSThomas Huth     lpcr = env->spr[SPR_LPCR];
995fcf5ef2aSThomas Huth     if (!(lpcr & LPCR_VPM0)) {
996fcf5ef2aSThomas Huth         return;
997fcf5ef2aSThomas Huth     }
998fcf5ef2aSThomas Huth 
999fcf5ef2aSThomas Huth     /* Make one up. Mostly ignore the ESID which will not be
1000fcf5ef2aSThomas Huth      * needed for translation
1001fcf5ef2aSThomas Huth      */
1002fcf5ef2aSThomas Huth     vsid = SLB_VSID_VRMA;
1003fcf5ef2aSThomas Huth     vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
1004fcf5ef2aSThomas Huth     vsid |= (vrmasd << 4) & (SLB_VSID_L | SLB_VSID_LP);
1005fcf5ef2aSThomas Huth     esid = SLB_ESID_V;
1006fcf5ef2aSThomas Huth 
1007fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
1008b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
1009fcf5ef2aSThomas Huth 
1010fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
1011fcf5ef2aSThomas Huth             break;
1012fcf5ef2aSThomas Huth         }
1013fcf5ef2aSThomas Huth 
1014fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
1015fcf5ef2aSThomas Huth             sps = sps1;
1016fcf5ef2aSThomas Huth             break;
1017fcf5ef2aSThomas Huth         }
1018fcf5ef2aSThomas Huth     }
1019fcf5ef2aSThomas Huth 
1020fcf5ef2aSThomas Huth     if (!sps) {
1021fcf5ef2aSThomas Huth         error_report("Bad page size encoding esid 0x"TARGET_FMT_lx
1022fcf5ef2aSThomas Huth                      " vsid 0x"TARGET_FMT_lx, esid, vsid);
1023fcf5ef2aSThomas Huth         return;
1024fcf5ef2aSThomas Huth     }
1025fcf5ef2aSThomas Huth 
1026fcf5ef2aSThomas Huth     slb->vsid = vsid;
1027fcf5ef2aSThomas Huth     slb->esid = esid;
1028fcf5ef2aSThomas Huth     slb->sps = sps;
1029fcf5ef2aSThomas Huth }
1030fcf5ef2aSThomas Huth 
1031*5ad55315SDavid Gibson void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val)
1032fcf5ef2aSThomas Huth {
1033*5ad55315SDavid Gibson     CPUPPCState *env = &cpu->env;
1034fcf5ef2aSThomas Huth     uint64_t lpcr = 0;
1035fcf5ef2aSThomas Huth 
1036fcf5ef2aSThomas Huth     /* Filter out bits */
10370941d728SDavid Gibson     switch (env->mmu_model) {
10380941d728SDavid Gibson     case POWERPC_MMU_64B: /* 970 */
1039fcf5ef2aSThomas Huth         if (val & 0x40) {
1040fcf5ef2aSThomas Huth             lpcr |= LPCR_LPES0;
1041fcf5ef2aSThomas Huth         }
1042fcf5ef2aSThomas Huth         if (val & 0x8000000000000000ull) {
1043fcf5ef2aSThomas Huth             lpcr |= LPCR_LPES1;
1044fcf5ef2aSThomas Huth         }
1045fcf5ef2aSThomas Huth         if (val & 0x20) {
1046fcf5ef2aSThomas Huth             lpcr |= (0x4ull << LPCR_RMLS_SHIFT);
1047fcf5ef2aSThomas Huth         }
1048fcf5ef2aSThomas Huth         if (val & 0x4000000000000000ull) {
1049fcf5ef2aSThomas Huth             lpcr |= (0x2ull << LPCR_RMLS_SHIFT);
1050fcf5ef2aSThomas Huth         }
1051fcf5ef2aSThomas Huth         if (val & 0x2000000000000000ull) {
1052fcf5ef2aSThomas Huth             lpcr |= (0x1ull << LPCR_RMLS_SHIFT);
1053fcf5ef2aSThomas Huth         }
1054fcf5ef2aSThomas Huth         env->spr[SPR_RMOR] = ((lpcr >> 41) & 0xffffull) << 26;
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth         /* XXX We could also write LPID from HID4 here
1057fcf5ef2aSThomas Huth          * but since we don't tag any translation on it
1058fcf5ef2aSThomas Huth          * it doesn't actually matter
1059fcf5ef2aSThomas Huth          */
1060fcf5ef2aSThomas Huth         /* XXX For proper emulation of 970 we also need
1061fcf5ef2aSThomas Huth          * to dig HRMOR out of HID5
1062fcf5ef2aSThomas Huth          */
1063fcf5ef2aSThomas Huth         break;
10640941d728SDavid Gibson     case POWERPC_MMU_2_03: /* P5p */
1065fcf5ef2aSThomas Huth         lpcr = val & (LPCR_RMLS | LPCR_ILE |
1066fcf5ef2aSThomas Huth                       LPCR_LPES0 | LPCR_LPES1 |
1067fcf5ef2aSThomas Huth                       LPCR_RMI | LPCR_HDICE);
1068fcf5ef2aSThomas Huth         break;
10690941d728SDavid Gibson     case POWERPC_MMU_2_06: /* P7 */
1070fcf5ef2aSThomas Huth         lpcr = val & (LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_DPFD |
1071fcf5ef2aSThomas Huth                       LPCR_VRMASD | LPCR_RMLS | LPCR_ILE |
1072fcf5ef2aSThomas Huth                       LPCR_P7_PECE0 | LPCR_P7_PECE1 | LPCR_P7_PECE2 |
1073fcf5ef2aSThomas Huth                       LPCR_MER | LPCR_TC |
1074fcf5ef2aSThomas Huth                       LPCR_LPES0 | LPCR_LPES1 | LPCR_HDICE);
1075fcf5ef2aSThomas Huth         break;
10760941d728SDavid Gibson     case POWERPC_MMU_2_07: /* P8 */
1077fcf5ef2aSThomas Huth         lpcr = val & (LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_KBV |
1078fcf5ef2aSThomas Huth                       LPCR_DPFD | LPCR_VRMASD | LPCR_RMLS | LPCR_ILE |
1079fcf5ef2aSThomas Huth                       LPCR_AIL | LPCR_ONL | LPCR_P8_PECE0 | LPCR_P8_PECE1 |
1080fcf5ef2aSThomas Huth                       LPCR_P8_PECE2 | LPCR_P8_PECE3 | LPCR_P8_PECE4 |
1081fcf5ef2aSThomas Huth                       LPCR_MER | LPCR_TC | LPCR_LPES0 | LPCR_HDICE);
1082fcf5ef2aSThomas Huth         break;
10830941d728SDavid Gibson     case POWERPC_MMU_3_00: /* P9 */
108418aa49ecSSuraj Jitindar Singh         lpcr = val & (LPCR_VPM1 | LPCR_ISL | LPCR_KBV | LPCR_DPFD |
108518aa49ecSSuraj Jitindar Singh                       (LPCR_PECE_U_MASK & LPCR_HVEE) | LPCR_ILE | LPCR_AIL |
108618aa49ecSSuraj Jitindar Singh                       LPCR_UPRT | LPCR_EVIRT | LPCR_ONL |
108718aa49ecSSuraj Jitindar Singh                       (LPCR_PECE_L_MASK & (LPCR_PDEE | LPCR_HDEE | LPCR_EEE |
108818aa49ecSSuraj Jitindar Singh                       LPCR_DEE | LPCR_OEE)) | LPCR_MER | LPCR_GTSE | LPCR_TC |
108918aa49ecSSuraj Jitindar Singh                       LPCR_HEIC | LPCR_LPES0 | LPCR_HVICE | LPCR_HDICE);
109018aa49ecSSuraj Jitindar Singh         break;
1091fcf5ef2aSThomas Huth     default:
1092fcf5ef2aSThomas Huth         ;
1093fcf5ef2aSThomas Huth     }
1094fcf5ef2aSThomas Huth     env->spr[SPR_LPCR] = lpcr;
10958fe08facSDavid Gibson     ppc_hash64_update_rmls(cpu);
10968fe08facSDavid Gibson     ppc_hash64_update_vrma(cpu);
1097fcf5ef2aSThomas Huth }
1098a059471dSDavid Gibson 
1099*5ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val)
1100*5ad55315SDavid Gibson {
1101*5ad55315SDavid Gibson     PowerPCCPU *cpu = ppc_env_get_cpu(env);
1102*5ad55315SDavid Gibson 
1103*5ad55315SDavid Gibson     ppc_store_lpcr(cpu, val);
1104*5ad55315SDavid Gibson }
1105*5ad55315SDavid Gibson 
1106a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu)
1107a059471dSDavid Gibson {
1108a059471dSDavid Gibson     CPUPPCState *env = &cpu->env;
1109a059471dSDavid Gibson     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1110a059471dSDavid Gibson 
111121e405f1SDavid Gibson     if (!pcc->hash64_opts) {
111221e405f1SDavid Gibson         assert(!(env->mmu_model & POWERPC_MMU_64));
111321e405f1SDavid Gibson         return;
111421e405f1SDavid Gibson     }
111521e405f1SDavid Gibson 
111621e405f1SDavid Gibson     cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
111721e405f1SDavid Gibson }
111821e405f1SDavid Gibson 
111921e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu)
112021e405f1SDavid Gibson {
112121e405f1SDavid Gibson     g_free(cpu->hash64_opts);
112221e405f1SDavid Gibson }
112321e405f1SDavid Gibson 
112421e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = {
112558969eeeSDavid Gibson     .flags = 0,
112667d7d66fSDavid Gibson     .slb_size = 64,
1127a059471dSDavid Gibson     .sps = {
1128a059471dSDavid Gibson         { .page_shift = 12, /* 4K */
1129a059471dSDavid Gibson           .slb_enc = 0,
1130a059471dSDavid Gibson           .enc = { { .page_shift = 12, .pte_enc = 0 } }
1131a059471dSDavid Gibson         },
1132a059471dSDavid Gibson         { .page_shift = 24, /* 16M */
1133a059471dSDavid Gibson           .slb_enc = 0x100,
1134a059471dSDavid Gibson           .enc = { { .page_shift = 24, .pte_enc = 0 } }
1135a059471dSDavid Gibson         },
1136a059471dSDavid Gibson     },
1137a059471dSDavid Gibson };
1138b07c59f7SDavid Gibson 
1139b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = {
114026cd35b8SDavid Gibson     .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
114167d7d66fSDavid Gibson     .slb_size = 32,
1142b07c59f7SDavid Gibson     .sps = {
1143b07c59f7SDavid Gibson         {
1144b07c59f7SDavid Gibson             .page_shift = 12, /* 4K */
1145b07c59f7SDavid Gibson             .slb_enc = 0,
1146b07c59f7SDavid Gibson             .enc = { { .page_shift = 12, .pte_enc = 0 },
1147b07c59f7SDavid Gibson                      { .page_shift = 16, .pte_enc = 0x7 },
1148b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x38 }, },
1149b07c59f7SDavid Gibson         },
1150b07c59f7SDavid Gibson         {
1151b07c59f7SDavid Gibson             .page_shift = 16, /* 64K */
1152b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_64K,
1153b07c59f7SDavid Gibson             .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1154b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x8 }, },
1155b07c59f7SDavid Gibson         },
1156b07c59f7SDavid Gibson         {
1157b07c59f7SDavid Gibson             .page_shift = 24, /* 16M */
1158b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16M,
1159b07c59f7SDavid Gibson             .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1160b07c59f7SDavid Gibson         },
1161b07c59f7SDavid Gibson         {
1162b07c59f7SDavid Gibson             .page_shift = 34, /* 16G */
1163b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16G,
1164b07c59f7SDavid Gibson             .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },
1165b07c59f7SDavid Gibson         },
1166b07c59f7SDavid Gibson     }
1167b07c59f7SDavid Gibson };
1168