xref: /openbmc/qemu/target/ppc/mmu-hash64.c (revision fad866daa85c65267fa44de40f10cc1ee904ae1a)
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"
25*fad866daSMarkus Armbruster #include "qemu/qemu-print.h"
26b3946626SVincent Palatin #include "sysemu/hw_accel.h"
27fcf5ef2aSThomas Huth #include "kvm_ppc.h"
28fcf5ef2aSThomas Huth #include "mmu-hash64.h"
29fcf5ef2aSThomas Huth #include "exec/log.h"
307222b94aSDavid Gibson #include "hw/hw.h"
31b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.h"
32fcf5ef2aSThomas Huth 
33fcf5ef2aSThomas Huth //#define DEBUG_SLB
34fcf5ef2aSThomas Huth 
35fcf5ef2aSThomas Huth #ifdef DEBUG_SLB
36fcf5ef2aSThomas Huth #  define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
37fcf5ef2aSThomas Huth #else
38fcf5ef2aSThomas Huth #  define LOG_SLB(...) do { } while (0)
39fcf5ef2aSThomas Huth #endif
40fcf5ef2aSThomas Huth 
41fcf5ef2aSThomas Huth /*
42fcf5ef2aSThomas Huth  * SLB handling
43fcf5ef2aSThomas Huth  */
44fcf5ef2aSThomas Huth 
45fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr)
46fcf5ef2aSThomas Huth {
47fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
48fcf5ef2aSThomas Huth     uint64_t esid_256M, esid_1T;
49fcf5ef2aSThomas Huth     int n;
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth     LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr);
52fcf5ef2aSThomas Huth 
53fcf5ef2aSThomas Huth     esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V;
54fcf5ef2aSThomas Huth     esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V;
55fcf5ef2aSThomas Huth 
5667d7d66fSDavid Gibson     for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
57fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
58fcf5ef2aSThomas Huth 
59fcf5ef2aSThomas Huth         LOG_SLB("%s: slot %d %016" PRIx64 " %016"
60fcf5ef2aSThomas Huth                     PRIx64 "\n", __func__, n, slb->esid, slb->vsid);
61fcf5ef2aSThomas Huth         /* We check for 1T matches on all MMUs here - if the MMU
62fcf5ef2aSThomas Huth          * doesn't have 1T segment support, we will have prevented 1T
63fcf5ef2aSThomas Huth          * entries from being inserted in the slbmte code. */
64fcf5ef2aSThomas Huth         if (((slb->esid == esid_256M) &&
65fcf5ef2aSThomas Huth              ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M))
66fcf5ef2aSThomas Huth             || ((slb->esid == esid_1T) &&
67fcf5ef2aSThomas Huth                 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) {
68fcf5ef2aSThomas Huth             return slb;
69fcf5ef2aSThomas Huth         }
70fcf5ef2aSThomas Huth     }
71fcf5ef2aSThomas Huth 
72fcf5ef2aSThomas Huth     return NULL;
73fcf5ef2aSThomas Huth }
74fcf5ef2aSThomas Huth 
75*fad866daSMarkus Armbruster void dump_slb(PowerPCCPU *cpu)
76fcf5ef2aSThomas Huth {
77fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
78fcf5ef2aSThomas Huth     int i;
79fcf5ef2aSThomas Huth     uint64_t slbe, slbv;
80fcf5ef2aSThomas Huth 
81fcf5ef2aSThomas Huth     cpu_synchronize_state(CPU(cpu));
82fcf5ef2aSThomas Huth 
83*fad866daSMarkus Armbruster     qemu_printf("SLB\tESID\t\t\tVSID\n");
8467d7d66fSDavid Gibson     for (i = 0; i < cpu->hash64_opts->slb_size; i++) {
85fcf5ef2aSThomas Huth         slbe = env->slb[i].esid;
86fcf5ef2aSThomas Huth         slbv = env->slb[i].vsid;
87fcf5ef2aSThomas Huth         if (slbe == 0 && slbv == 0) {
88fcf5ef2aSThomas Huth             continue;
89fcf5ef2aSThomas Huth         }
90*fad866daSMarkus Armbruster         qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n",
91fcf5ef2aSThomas Huth                     i, slbe, slbv);
92fcf5ef2aSThomas Huth     }
93fcf5ef2aSThomas Huth }
94fcf5ef2aSThomas Huth 
95fcf5ef2aSThomas Huth void helper_slbia(CPUPPCState *env)
96fcf5ef2aSThomas Huth {
9767d7d66fSDavid Gibson     PowerPCCPU *cpu = ppc_env_get_cpu(env);
98fcf5ef2aSThomas Huth     int n;
99fcf5ef2aSThomas Huth 
100fcf5ef2aSThomas Huth     /* XXX: Warning: slbia never invalidates the first segment */
10167d7d66fSDavid Gibson     for (n = 1; n < cpu->hash64_opts->slb_size; n++) {
102fcf5ef2aSThomas Huth         ppc_slb_t *slb = &env->slb[n];
103fcf5ef2aSThomas Huth 
104fcf5ef2aSThomas Huth         if (slb->esid & SLB_ESID_V) {
105fcf5ef2aSThomas Huth             slb->esid &= ~SLB_ESID_V;
106fcf5ef2aSThomas Huth             /* XXX: given the fact that segment size is 256 MB or 1TB,
107fcf5ef2aSThomas Huth              *      and we still don't have a tlb_flush_mask(env, n, mask)
108fcf5ef2aSThomas Huth              *      in QEMU, we just invalidate all TLBs
109fcf5ef2aSThomas Huth              */
110fcf5ef2aSThomas Huth             env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
111fcf5ef2aSThomas Huth         }
112fcf5ef2aSThomas Huth     }
113fcf5ef2aSThomas Huth }
114fcf5ef2aSThomas Huth 
115a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr,
116a63f1dfcSNikunj A Dadhania                            target_ulong global)
117fcf5ef2aSThomas Huth {
118fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
119fcf5ef2aSThomas Huth     ppc_slb_t *slb;
120fcf5ef2aSThomas Huth 
121fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, addr);
122fcf5ef2aSThomas Huth     if (!slb) {
123fcf5ef2aSThomas Huth         return;
124fcf5ef2aSThomas Huth     }
125fcf5ef2aSThomas Huth 
126fcf5ef2aSThomas Huth     if (slb->esid & SLB_ESID_V) {
127fcf5ef2aSThomas Huth         slb->esid &= ~SLB_ESID_V;
128fcf5ef2aSThomas Huth 
129fcf5ef2aSThomas Huth         /* XXX: given the fact that segment size is 256 MB or 1TB,
130fcf5ef2aSThomas Huth          *      and we still don't have a tlb_flush_mask(env, n, mask)
131fcf5ef2aSThomas Huth          *      in QEMU, we just invalidate all TLBs
132fcf5ef2aSThomas Huth          */
133a63f1dfcSNikunj A Dadhania         env->tlb_need_flush |=
134a63f1dfcSNikunj A Dadhania             (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH);
135fcf5ef2aSThomas Huth     }
136fcf5ef2aSThomas Huth }
137fcf5ef2aSThomas Huth 
138a63f1dfcSNikunj A Dadhania void helper_slbie(CPUPPCState *env, target_ulong addr)
139a63f1dfcSNikunj A Dadhania {
140a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, false);
141a63f1dfcSNikunj A Dadhania }
142a63f1dfcSNikunj A Dadhania 
143a63f1dfcSNikunj A Dadhania void helper_slbieg(CPUPPCState *env, target_ulong addr)
144a63f1dfcSNikunj A Dadhania {
145a63f1dfcSNikunj A Dadhania     __helper_slbie(env, addr, true);
146a63f1dfcSNikunj A Dadhania }
147a63f1dfcSNikunj A Dadhania 
148fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot,
149fcf5ef2aSThomas Huth                   target_ulong esid, target_ulong vsid)
150fcf5ef2aSThomas Huth {
151fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
152fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
153b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
154fcf5ef2aSThomas Huth     int i;
155fcf5ef2aSThomas Huth 
15667d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
157fcf5ef2aSThomas Huth         return -1; /* Bad slot number */
158fcf5ef2aSThomas Huth     }
159fcf5ef2aSThomas Huth     if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) {
160fcf5ef2aSThomas Huth         return -1; /* Reserved bits set */
161fcf5ef2aSThomas Huth     }
162fcf5ef2aSThomas Huth     if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
163fcf5ef2aSThomas Huth         return -1; /* Bad segment size */
164fcf5ef2aSThomas Huth     }
16558969eeeSDavid Gibson     if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) {
166fcf5ef2aSThomas Huth         return -1; /* 1T segment on MMU that doesn't support it */
167fcf5ef2aSThomas Huth     }
168fcf5ef2aSThomas Huth 
169fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
170b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
171fcf5ef2aSThomas Huth 
172fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
173fcf5ef2aSThomas Huth             break;
174fcf5ef2aSThomas Huth         }
175fcf5ef2aSThomas Huth 
176fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
177fcf5ef2aSThomas Huth             sps = sps1;
178fcf5ef2aSThomas Huth             break;
179fcf5ef2aSThomas Huth         }
180fcf5ef2aSThomas Huth     }
181fcf5ef2aSThomas Huth 
182fcf5ef2aSThomas Huth     if (!sps) {
183fcf5ef2aSThomas Huth         error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
184fcf5ef2aSThomas Huth                      " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx,
185fcf5ef2aSThomas Huth                      slot, esid, vsid);
186fcf5ef2aSThomas Huth         return -1;
187fcf5ef2aSThomas Huth     }
188fcf5ef2aSThomas Huth 
189fcf5ef2aSThomas Huth     slb->esid = esid;
190fcf5ef2aSThomas Huth     slb->vsid = vsid;
191fcf5ef2aSThomas Huth     slb->sps = sps;
192fcf5ef2aSThomas Huth 
19376134d48SSuraj Jitindar Singh     LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx
19476134d48SSuraj Jitindar Singh             " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid,
195fcf5ef2aSThomas Huth             slb->esid, slb->vsid);
196fcf5ef2aSThomas Huth 
197fcf5ef2aSThomas Huth     return 0;
198fcf5ef2aSThomas Huth }
199fcf5ef2aSThomas Huth 
200fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb,
201fcf5ef2aSThomas Huth                              target_ulong *rt)
202fcf5ef2aSThomas Huth {
203fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
204fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
205fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
206fcf5ef2aSThomas Huth 
20767d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
208fcf5ef2aSThomas Huth         return -1;
209fcf5ef2aSThomas Huth     }
210fcf5ef2aSThomas Huth 
211fcf5ef2aSThomas Huth     *rt = slb->esid;
212fcf5ef2aSThomas Huth     return 0;
213fcf5ef2aSThomas Huth }
214fcf5ef2aSThomas Huth 
215fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
216fcf5ef2aSThomas Huth                              target_ulong *rt)
217fcf5ef2aSThomas Huth {
218fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
219fcf5ef2aSThomas Huth     int slot = rb & 0xfff;
220fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->slb[slot];
221fcf5ef2aSThomas Huth 
22267d7d66fSDavid Gibson     if (slot >= cpu->hash64_opts->slb_size) {
223fcf5ef2aSThomas Huth         return -1;
224fcf5ef2aSThomas Huth     }
225fcf5ef2aSThomas Huth 
226fcf5ef2aSThomas Huth     *rt = slb->vsid;
227fcf5ef2aSThomas Huth     return 0;
228fcf5ef2aSThomas Huth }
229fcf5ef2aSThomas Huth 
230fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
231fcf5ef2aSThomas Huth                              target_ulong *rt)
232fcf5ef2aSThomas Huth {
233fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
234fcf5ef2aSThomas Huth     ppc_slb_t *slb;
235fcf5ef2aSThomas Huth 
236fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, env->msr)) {
237fcf5ef2aSThomas Huth         rb &= 0xffffffff;
238fcf5ef2aSThomas Huth     }
239fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, rb);
240fcf5ef2aSThomas Huth     if (slb == NULL) {
241fcf5ef2aSThomas Huth         *rt = (target_ulong)-1ul;
242fcf5ef2aSThomas Huth     } else {
243fcf5ef2aSThomas Huth         *rt = slb->vsid;
244fcf5ef2aSThomas Huth     }
245fcf5ef2aSThomas Huth     return 0;
246fcf5ef2aSThomas Huth }
247fcf5ef2aSThomas Huth 
248fcf5ef2aSThomas Huth void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs)
249fcf5ef2aSThomas Huth {
250fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
251fcf5ef2aSThomas Huth 
252fcf5ef2aSThomas Huth     if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) {
253fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
254fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
255fcf5ef2aSThomas Huth     }
256fcf5ef2aSThomas Huth }
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb)
259fcf5ef2aSThomas Huth {
260fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
261fcf5ef2aSThomas Huth     target_ulong rt = 0;
262fcf5ef2aSThomas Huth 
263fcf5ef2aSThomas Huth     if (ppc_load_slb_esid(cpu, rb, &rt) < 0) {
264fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
265fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
266fcf5ef2aSThomas Huth     }
267fcf5ef2aSThomas Huth     return rt;
268fcf5ef2aSThomas Huth }
269fcf5ef2aSThomas Huth 
270fcf5ef2aSThomas Huth target_ulong helper_find_slb_vsid(CPUPPCState *env, target_ulong rb)
271fcf5ef2aSThomas Huth {
272fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
273fcf5ef2aSThomas Huth     target_ulong rt = 0;
274fcf5ef2aSThomas Huth 
275fcf5ef2aSThomas Huth     if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) {
276fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
277fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
278fcf5ef2aSThomas Huth     }
279fcf5ef2aSThomas Huth     return rt;
280fcf5ef2aSThomas Huth }
281fcf5ef2aSThomas Huth 
282fcf5ef2aSThomas Huth target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb)
283fcf5ef2aSThomas Huth {
284fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
285fcf5ef2aSThomas Huth     target_ulong rt = 0;
286fcf5ef2aSThomas Huth 
287fcf5ef2aSThomas Huth     if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) {
288fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
289fcf5ef2aSThomas Huth                                POWERPC_EXCP_INVAL, GETPC());
290fcf5ef2aSThomas Huth     }
291fcf5ef2aSThomas Huth     return rt;
292fcf5ef2aSThomas Huth }
293fcf5ef2aSThomas Huth 
29407a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */
29507a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu,
29607a68f99SSuraj Jitindar Singh                                               ppc_hash_pte64_t pte)
29707a68f99SSuraj Jitindar Singh {
29807a68f99SSuraj Jitindar Singh     /* Exec permissions CANNOT take away read or write permissions */
29907a68f99SSuraj Jitindar Singh     return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ?
30007a68f99SSuraj Jitindar Singh             PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC;
30107a68f99SSuraj Jitindar Singh }
30207a68f99SSuraj Jitindar Singh 
30307a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */
304fcf5ef2aSThomas Huth static int ppc_hash64_pte_prot(PowerPCCPU *cpu,
305fcf5ef2aSThomas Huth                                ppc_slb_t *slb, ppc_hash_pte64_t pte)
306fcf5ef2aSThomas Huth {
307fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
308fcf5ef2aSThomas Huth     unsigned pp, key;
309fcf5ef2aSThomas Huth     /* Some pp bit combinations have undefined behaviour, so default
310fcf5ef2aSThomas Huth      * to no access in those cases */
311fcf5ef2aSThomas Huth     int prot = 0;
312fcf5ef2aSThomas Huth 
313fcf5ef2aSThomas Huth     key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP)
314fcf5ef2aSThomas Huth              : (slb->vsid & SLB_VSID_KS));
315fcf5ef2aSThomas Huth     pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
316fcf5ef2aSThomas Huth 
317fcf5ef2aSThomas Huth     if (key == 0) {
318fcf5ef2aSThomas Huth         switch (pp) {
319fcf5ef2aSThomas Huth         case 0x0:
320fcf5ef2aSThomas Huth         case 0x1:
321fcf5ef2aSThomas Huth         case 0x2:
322347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
323fcf5ef2aSThomas Huth             break;
324fcf5ef2aSThomas Huth 
325fcf5ef2aSThomas Huth         case 0x3:
326fcf5ef2aSThomas Huth         case 0x6:
327347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
328fcf5ef2aSThomas Huth             break;
329fcf5ef2aSThomas Huth         }
330fcf5ef2aSThomas Huth     } else {
331fcf5ef2aSThomas Huth         switch (pp) {
332fcf5ef2aSThomas Huth         case 0x0:
333fcf5ef2aSThomas Huth         case 0x6:
334fcf5ef2aSThomas Huth             break;
335fcf5ef2aSThomas Huth 
336fcf5ef2aSThomas Huth         case 0x1:
337fcf5ef2aSThomas Huth         case 0x3:
338347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_EXEC;
339fcf5ef2aSThomas Huth             break;
340fcf5ef2aSThomas Huth 
341fcf5ef2aSThomas Huth         case 0x2:
342347a5c73SSuraj Jitindar Singh             prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
343fcf5ef2aSThomas Huth             break;
344fcf5ef2aSThomas Huth         }
345fcf5ef2aSThomas Huth     }
346fcf5ef2aSThomas Huth 
347fcf5ef2aSThomas Huth     return prot;
348fcf5ef2aSThomas Huth }
349fcf5ef2aSThomas Huth 
350a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */
351a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key)
352a6152b52SSuraj Jitindar Singh {
353a6152b52SSuraj Jitindar Singh     CPUPPCState *env = &cpu->env;
354a6152b52SSuraj Jitindar Singh     int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3;
355a6152b52SSuraj Jitindar Singh 
356a6152b52SSuraj Jitindar Singh     /*
357a6152b52SSuraj Jitindar Singh      * An instruction fetch is permitted if the IAMR bit is 0.
358a6152b52SSuraj Jitindar Singh      * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
359a6152b52SSuraj Jitindar Singh      * can only take away EXEC permissions not READ or WRITE permissions.
360a6152b52SSuraj Jitindar Singh      * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
361a6152b52SSuraj Jitindar Singh      * EXEC permissions are allowed.
362a6152b52SSuraj Jitindar Singh      */
363a6152b52SSuraj Jitindar Singh     return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE :
364a6152b52SSuraj Jitindar Singh                                PAGE_READ | PAGE_WRITE | PAGE_EXEC;
365a6152b52SSuraj Jitindar Singh }
366a6152b52SSuraj Jitindar Singh 
367fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte)
368fcf5ef2aSThomas Huth {
369fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
370fcf5ef2aSThomas Huth     int key, amrbits;
371fcf5ef2aSThomas Huth     int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
372fcf5ef2aSThomas Huth 
373fcf5ef2aSThomas Huth     /* Only recent MMUs implement Virtual Page Class Key Protection */
37458969eeeSDavid Gibson     if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) {
375fcf5ef2aSThomas Huth         return prot;
376fcf5ef2aSThomas Huth     }
377fcf5ef2aSThomas Huth 
378fcf5ef2aSThomas Huth     key = HPTE64_R_KEY(pte.pte1);
379fcf5ef2aSThomas Huth     amrbits = (env->spr[SPR_AMR] >> 2*(31 - key)) & 0x3;
380fcf5ef2aSThomas Huth 
381fcf5ef2aSThomas Huth     /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
382fcf5ef2aSThomas Huth     /*         env->spr[SPR_AMR]); */
383fcf5ef2aSThomas Huth 
384fcf5ef2aSThomas Huth     /*
385fcf5ef2aSThomas Huth      * A store is permitted if the AMR bit is 0. Remove write
386fcf5ef2aSThomas Huth      * protection if it is set.
387fcf5ef2aSThomas Huth      */
388fcf5ef2aSThomas Huth     if (amrbits & 0x2) {
389fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
390fcf5ef2aSThomas Huth     }
391fcf5ef2aSThomas Huth     /*
392fcf5ef2aSThomas Huth      * A load is permitted if the AMR bit is 0. Remove read
393fcf5ef2aSThomas Huth      * protection if it is set.
394fcf5ef2aSThomas Huth      */
395fcf5ef2aSThomas Huth     if (amrbits & 0x1) {
396fcf5ef2aSThomas Huth         prot &= ~PAGE_READ;
397fcf5ef2aSThomas Huth     }
398fcf5ef2aSThomas Huth 
399a6152b52SSuraj Jitindar Singh     switch (env->mmu_model) {
400a6152b52SSuraj Jitindar Singh     /*
401a6152b52SSuraj Jitindar Singh      * MMU version 2.07 and later support IAMR
402a6152b52SSuraj Jitindar Singh      * Check if the IAMR allows the instruction access - it will return
403a6152b52SSuraj Jitindar Singh      * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
404a6152b52SSuraj Jitindar Singh      * if it does (and prot will be unchanged indicating execution support).
405a6152b52SSuraj Jitindar Singh      */
406a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_2_07:
407a6152b52SSuraj Jitindar Singh     case POWERPC_MMU_3_00:
408a6152b52SSuraj Jitindar Singh         prot &= ppc_hash64_iamr_prot(cpu, key);
409a6152b52SSuraj Jitindar Singh         break;
410a6152b52SSuraj Jitindar Singh     default:
411a6152b52SSuraj Jitindar Singh         break;
412a6152b52SSuraj Jitindar Singh     }
413a6152b52SSuraj Jitindar Singh 
414fcf5ef2aSThomas Huth     return prot;
415fcf5ef2aSThomas Huth }
416fcf5ef2aSThomas Huth 
4177222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu,
4187222b94aSDavid Gibson                                              hwaddr ptex, int n)
419fcf5ef2aSThomas Huth {
4207222b94aSDavid Gibson     hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
4213367c62fSBenjamin Herrenschmidt     hwaddr base;
4227222b94aSDavid Gibson     hwaddr plen = n * HASH_PTE_SIZE_64;
423e57ca75cSDavid Gibson     const ppc_hash_pte64_t *hptes;
424e57ca75cSDavid Gibson 
425e57ca75cSDavid Gibson     if (cpu->vhyp) {
426e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
427e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
428e57ca75cSDavid Gibson         return vhc->map_hptes(cpu->vhyp, ptex, n);
429e57ca75cSDavid Gibson     }
4303367c62fSBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
431e57ca75cSDavid Gibson 
432e57ca75cSDavid Gibson     if (!base) {
433e57ca75cSDavid Gibson         return NULL;
434e57ca75cSDavid Gibson     }
435e57ca75cSDavid Gibson 
436f26404fbSPeter Maydell     hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false,
437f26404fbSPeter Maydell                               MEMTXATTRS_UNSPECIFIED);
4387222b94aSDavid Gibson     if (plen < (n * HASH_PTE_SIZE_64)) {
4397222b94aSDavid Gibson         hw_error("%s: Unable to map all requested HPTEs\n", __func__);
440fcf5ef2aSThomas Huth     }
4417222b94aSDavid Gibson     return hptes;
442fcf5ef2aSThomas Huth }
443fcf5ef2aSThomas Huth 
4447222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes,
4457222b94aSDavid Gibson                             hwaddr ptex, int n)
446fcf5ef2aSThomas Huth {
447e57ca75cSDavid Gibson     if (cpu->vhyp) {
448e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
449e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
450e57ca75cSDavid Gibson         vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n);
451e57ca75cSDavid Gibson         return;
452e57ca75cSDavid Gibson     }
453e57ca75cSDavid Gibson 
4547222b94aSDavid Gibson     address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64,
4557222b94aSDavid Gibson                         false, n * HASH_PTE_SIZE_64);
456fcf5ef2aSThomas Huth }
457fcf5ef2aSThomas Huth 
458b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps,
459fcf5ef2aSThomas Huth                                 uint64_t pte0, uint64_t pte1)
460fcf5ef2aSThomas Huth {
461fcf5ef2aSThomas Huth     int i;
462fcf5ef2aSThomas Huth 
463fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
464fcf5ef2aSThomas Huth         if (sps->page_shift != 12) {
465fcf5ef2aSThomas Huth             /* 4kiB page in a non 4kiB segment */
466fcf5ef2aSThomas Huth             return 0;
467fcf5ef2aSThomas Huth         }
468fcf5ef2aSThomas Huth         /* Normal 4kiB page */
469fcf5ef2aSThomas Huth         return 12;
470fcf5ef2aSThomas Huth     }
471fcf5ef2aSThomas Huth 
472fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
473b07c59f7SDavid Gibson         const PPCHash64PageSize *ps = &sps->enc[i];
474fcf5ef2aSThomas Huth         uint64_t mask;
475fcf5ef2aSThomas Huth 
476fcf5ef2aSThomas Huth         if (!ps->page_shift) {
477fcf5ef2aSThomas Huth             break;
478fcf5ef2aSThomas Huth         }
479fcf5ef2aSThomas Huth 
480fcf5ef2aSThomas Huth         if (ps->page_shift == 12) {
481fcf5ef2aSThomas Huth             /* L bit is set so this can't be a 4kiB page */
482fcf5ef2aSThomas Huth             continue;
483fcf5ef2aSThomas Huth         }
484fcf5ef2aSThomas Huth 
485fcf5ef2aSThomas Huth         mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
486fcf5ef2aSThomas Huth 
487fcf5ef2aSThomas Huth         if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
488fcf5ef2aSThomas Huth             return ps->page_shift;
489fcf5ef2aSThomas Huth         }
490fcf5ef2aSThomas Huth     }
491fcf5ef2aSThomas Huth 
492fcf5ef2aSThomas Huth     return 0; /* Bad page size encoding */
493fcf5ef2aSThomas Huth }
494fcf5ef2aSThomas Huth 
49534525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1)
49634525595SBenjamin Herrenschmidt {
49734525595SBenjamin Herrenschmidt     /* Insert B into pte0 */
49834525595SBenjamin Herrenschmidt     *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) |
49934525595SBenjamin Herrenschmidt             ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) <<
50034525595SBenjamin Herrenschmidt              (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT));
50134525595SBenjamin Herrenschmidt 
50234525595SBenjamin Herrenschmidt     /* Remove B from pte1 */
50334525595SBenjamin Herrenschmidt     *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK;
50434525595SBenjamin Herrenschmidt }
50534525595SBenjamin Herrenschmidt 
50634525595SBenjamin Herrenschmidt 
507fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
508b07c59f7SDavid Gibson                                      const PPCHash64SegmentPageSizes *sps,
509fcf5ef2aSThomas Huth                                      target_ulong ptem,
510fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
511fcf5ef2aSThomas Huth {
512fcf5ef2aSThomas Huth     int i;
5137222b94aSDavid Gibson     const ppc_hash_pte64_t *pteg;
514fcf5ef2aSThomas Huth     target_ulong pte0, pte1;
5157222b94aSDavid Gibson     target_ulong ptex;
516fcf5ef2aSThomas Huth 
51736778660SDavid Gibson     ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP;
5187222b94aSDavid Gibson     pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
5197222b94aSDavid Gibson     if (!pteg) {
520fcf5ef2aSThomas Huth         return -1;
521fcf5ef2aSThomas Huth     }
522fcf5ef2aSThomas Huth     for (i = 0; i < HPTES_PER_GROUP; i++) {
5237222b94aSDavid Gibson         pte0 = ppc_hash64_hpte0(cpu, pteg, i);
5243054b0caSBenjamin Herrenschmidt         /*
5253054b0caSBenjamin Herrenschmidt          * pte0 contains the valid bit and must be read before pte1,
5263054b0caSBenjamin Herrenschmidt          * otherwise we might see an old pte1 with a new valid bit and
5273054b0caSBenjamin Herrenschmidt          * thus an inconsistent hpte value
5283054b0caSBenjamin Herrenschmidt          */
5293054b0caSBenjamin Herrenschmidt         smp_rmb();
5307222b94aSDavid Gibson         pte1 = ppc_hash64_hpte1(cpu, pteg, i);
531fcf5ef2aSThomas Huth 
53234525595SBenjamin Herrenschmidt         /* Convert format if necessary */
53334525595SBenjamin Herrenschmidt         if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) {
53434525595SBenjamin Herrenschmidt             ppc64_v3_new_to_old_hpte(&pte0, &pte1);
53534525595SBenjamin Herrenschmidt         }
53634525595SBenjamin Herrenschmidt 
537fcf5ef2aSThomas Huth         /* This compares V, B, H (secondary) and the AVPN */
538fcf5ef2aSThomas Huth         if (HPTE64_V_COMPARE(pte0, ptem)) {
539fcf5ef2aSThomas Huth             *pshift = hpte_page_shift(sps, pte0, pte1);
540fcf5ef2aSThomas Huth             /*
541fcf5ef2aSThomas Huth              * If there is no match, ignore the PTE, it could simply
542fcf5ef2aSThomas Huth              * be for a different segment size encoding and the
543fcf5ef2aSThomas Huth              * architecture specifies we should not match. Linux will
544fcf5ef2aSThomas Huth              * potentially leave behind PTEs for the wrong base page
545fcf5ef2aSThomas Huth              * size when demoting segments.
546fcf5ef2aSThomas Huth              */
547fcf5ef2aSThomas Huth             if (*pshift == 0) {
548fcf5ef2aSThomas Huth                 continue;
549fcf5ef2aSThomas Huth             }
550fcf5ef2aSThomas Huth             /* We don't do anything with pshift yet as qemu TLB only deals
551fcf5ef2aSThomas Huth              * with 4K pages anyway
552fcf5ef2aSThomas Huth              */
553fcf5ef2aSThomas Huth             pte->pte0 = pte0;
554fcf5ef2aSThomas Huth             pte->pte1 = pte1;
5557222b94aSDavid Gibson             ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
5567222b94aSDavid Gibson             return ptex + i;
557fcf5ef2aSThomas Huth         }
558fcf5ef2aSThomas Huth     }
5597222b94aSDavid Gibson     ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
560fcf5ef2aSThomas Huth     /*
561fcf5ef2aSThomas Huth      * We didn't find a valid entry.
562fcf5ef2aSThomas Huth      */
563fcf5ef2aSThomas Huth     return -1;
564fcf5ef2aSThomas Huth }
565fcf5ef2aSThomas Huth 
566fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
567fcf5ef2aSThomas Huth                                      ppc_slb_t *slb, target_ulong eaddr,
568fcf5ef2aSThomas Huth                                      ppc_hash_pte64_t *pte, unsigned *pshift)
569fcf5ef2aSThomas Huth {
570fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
5717222b94aSDavid Gibson     hwaddr hash, ptex;
572fcf5ef2aSThomas Huth     uint64_t vsid, epnmask, epn, ptem;
573b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = slb->sps;
574fcf5ef2aSThomas Huth 
575fcf5ef2aSThomas Huth     /* The SLB store path should prevent any bad page size encodings
576fcf5ef2aSThomas Huth      * getting in there, so: */
577fcf5ef2aSThomas Huth     assert(sps);
578fcf5ef2aSThomas Huth 
579fcf5ef2aSThomas Huth     /* If ISL is set in LPCR we need to clamp the page size to 4K */
580fcf5ef2aSThomas Huth     if (env->spr[SPR_LPCR] & LPCR_ISL) {
581fcf5ef2aSThomas Huth         /* We assume that when using TCG, 4k is first entry of SPS */
582b07c59f7SDavid Gibson         sps = &cpu->hash64_opts->sps[0];
583fcf5ef2aSThomas Huth         assert(sps->page_shift == 12);
584fcf5ef2aSThomas Huth     }
585fcf5ef2aSThomas Huth 
586fcf5ef2aSThomas Huth     epnmask = ~((1ULL << sps->page_shift) - 1);
587fcf5ef2aSThomas Huth 
588fcf5ef2aSThomas Huth     if (slb->vsid & SLB_VSID_B) {
589fcf5ef2aSThomas Huth         /* 1TB segment */
590fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
591fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
592fcf5ef2aSThomas Huth         hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
593fcf5ef2aSThomas Huth     } else {
594fcf5ef2aSThomas Huth         /* 256M segment */
595fcf5ef2aSThomas Huth         vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
596fcf5ef2aSThomas Huth         epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
597fcf5ef2aSThomas Huth         hash = vsid ^ (epn >> sps->page_shift);
598fcf5ef2aSThomas Huth     }
599fcf5ef2aSThomas Huth     ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
600fcf5ef2aSThomas Huth     ptem |= HPTE64_V_VALID;
601fcf5ef2aSThomas Huth 
602fcf5ef2aSThomas Huth     /* Page address translation */
603fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
604fcf5ef2aSThomas Huth             "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
605fcf5ef2aSThomas Huth             " hash " TARGET_FMT_plx "\n",
60636778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash);
607fcf5ef2aSThomas Huth 
608fcf5ef2aSThomas Huth     /* Primary PTEG lookup */
609fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
610fcf5ef2aSThomas Huth             "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
611fcf5ef2aSThomas Huth             " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
612fcf5ef2aSThomas Huth             " hash=" TARGET_FMT_plx "\n",
61336778660SDavid Gibson             ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu),
61436778660SDavid Gibson             vsid, ptem,  hash);
6157222b94aSDavid Gibson     ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
616fcf5ef2aSThomas Huth 
6177222b94aSDavid Gibson     if (ptex == -1) {
618fcf5ef2aSThomas Huth         /* Secondary PTEG lookup */
619fcf5ef2aSThomas Huth         ptem |= HPTE64_V_SECONDARY;
620fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU,
621fcf5ef2aSThomas Huth                 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
622fcf5ef2aSThomas Huth                 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
62336778660SDavid Gibson                 " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu),
62436778660SDavid Gibson                 ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash);
625fcf5ef2aSThomas Huth 
6267222b94aSDavid Gibson         ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
627fcf5ef2aSThomas Huth     }
628fcf5ef2aSThomas Huth 
6297222b94aSDavid Gibson     return ptex;
630fcf5ef2aSThomas Huth }
631fcf5ef2aSThomas Huth 
632fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
633fcf5ef2aSThomas Huth                                           uint64_t pte0, uint64_t pte1)
634fcf5ef2aSThomas Huth {
635fcf5ef2aSThomas Huth     int i;
636fcf5ef2aSThomas Huth 
637fcf5ef2aSThomas Huth     if (!(pte0 & HPTE64_V_LARGE)) {
638fcf5ef2aSThomas Huth         return 12;
639fcf5ef2aSThomas Huth     }
640fcf5ef2aSThomas Huth 
641fcf5ef2aSThomas Huth     /*
642fcf5ef2aSThomas Huth      * The encodings in env->sps need to be carefully chosen so that
643fcf5ef2aSThomas Huth      * this gives an unambiguous result.
644fcf5ef2aSThomas Huth      */
645fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
646b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
647fcf5ef2aSThomas Huth         unsigned shift;
648fcf5ef2aSThomas Huth 
649fcf5ef2aSThomas Huth         if (!sps->page_shift) {
650fcf5ef2aSThomas Huth             break;
651fcf5ef2aSThomas Huth         }
652fcf5ef2aSThomas Huth 
653fcf5ef2aSThomas Huth         shift = hpte_page_shift(sps, pte0, pte1);
654fcf5ef2aSThomas Huth         if (shift) {
655fcf5ef2aSThomas Huth             return shift;
656fcf5ef2aSThomas Huth         }
657fcf5ef2aSThomas Huth     }
658fcf5ef2aSThomas Huth 
659fcf5ef2aSThomas Huth     return 0;
660fcf5ef2aSThomas Huth }
661fcf5ef2aSThomas Huth 
6628fe08facSDavid Gibson static void ppc_hash64_set_isi(CPUState *cs, uint64_t error_code)
663fcf5ef2aSThomas Huth {
6648fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
665fcf5ef2aSThomas Huth     bool vpm;
666fcf5ef2aSThomas Huth 
667fcf5ef2aSThomas Huth     if (msr_ir) {
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_HISI;
682fcf5ef2aSThomas Huth     } else {
683fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_ISI;
684fcf5ef2aSThomas Huth     }
685fcf5ef2aSThomas Huth     env->error_code = error_code;
686fcf5ef2aSThomas Huth }
687fcf5ef2aSThomas Huth 
6888fe08facSDavid Gibson static void ppc_hash64_set_dsi(CPUState *cs, uint64_t dar, uint64_t dsisr)
689fcf5ef2aSThomas Huth {
6908fe08facSDavid Gibson     CPUPPCState *env = &POWERPC_CPU(cs)->env;
691fcf5ef2aSThomas Huth     bool vpm;
692fcf5ef2aSThomas Huth 
693fcf5ef2aSThomas Huth     if (msr_dr) {
694fcf5ef2aSThomas Huth         vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
695fcf5ef2aSThomas Huth     } else {
69650659083SSuraj Jitindar Singh         switch (env->mmu_model) {
69750659083SSuraj Jitindar Singh         case POWERPC_MMU_3_00:
69850659083SSuraj Jitindar Singh             /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
69950659083SSuraj Jitindar Singh             vpm = true;
70050659083SSuraj Jitindar Singh             break;
70150659083SSuraj Jitindar Singh         default:
702fcf5ef2aSThomas Huth             vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM0);
70350659083SSuraj Jitindar Singh             break;
70450659083SSuraj Jitindar Singh         }
705fcf5ef2aSThomas Huth     }
706fcf5ef2aSThomas Huth     if (vpm && !msr_hv) {
707fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_HDSI;
708fcf5ef2aSThomas Huth         env->spr[SPR_HDAR] = dar;
709fcf5ef2aSThomas Huth         env->spr[SPR_HDSISR] = dsisr;
710fcf5ef2aSThomas Huth     } else {
711fcf5ef2aSThomas Huth         cs->exception_index = POWERPC_EXCP_DSI;
712fcf5ef2aSThomas Huth         env->spr[SPR_DAR] = dar;
713fcf5ef2aSThomas Huth         env->spr[SPR_DSISR] = dsisr;
714fcf5ef2aSThomas Huth    }
715fcf5ef2aSThomas Huth     env->error_code = 0;
716fcf5ef2aSThomas Huth }
717fcf5ef2aSThomas Huth 
718fcf5ef2aSThomas Huth 
719fcf5ef2aSThomas Huth int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr,
720fcf5ef2aSThomas Huth                                 int rwx, int mmu_idx)
721fcf5ef2aSThomas Huth {
722fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
723fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
724fcf5ef2aSThomas Huth     ppc_slb_t *slb;
725fcf5ef2aSThomas Huth     unsigned apshift;
7267222b94aSDavid Gibson     hwaddr ptex;
727fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
72807a68f99SSuraj Jitindar Singh     int exec_prot, pp_prot, amr_prot, prot;
729da82c73aSSuraj Jitindar Singh     uint64_t new_pte1;
730fcf5ef2aSThomas Huth     const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC};
731fcf5ef2aSThomas Huth     hwaddr raddr;
732fcf5ef2aSThomas Huth 
733fcf5ef2aSThomas Huth     assert((rwx == 0) || (rwx == 1) || (rwx == 2));
734fcf5ef2aSThomas Huth 
735fcf5ef2aSThomas Huth     /* Note on LPCR usage: 970 uses HID4, but our special variant
736fcf5ef2aSThomas Huth      * of store_spr copies relevant fields into env->spr[SPR_LPCR].
737fcf5ef2aSThomas Huth      * Similarily we filter unimplemented bits when storing into
738fcf5ef2aSThomas Huth      * LPCR depending on the MMU version. This code can thus just
739fcf5ef2aSThomas Huth      * use the LPCR "as-is".
740fcf5ef2aSThomas Huth      */
741fcf5ef2aSThomas Huth 
742fcf5ef2aSThomas Huth     /* 1. Handle real mode accesses */
743fcf5ef2aSThomas Huth     if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) {
744fcf5ef2aSThomas Huth         /* Translation is supposedly "off"  */
745fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are (mostly) ignored */
746fcf5ef2aSThomas Huth         raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
747fcf5ef2aSThomas Huth 
748fcf5ef2aSThomas Huth         /* In HV mode, add HRMOR if top EA bit is clear */
749fcf5ef2aSThomas Huth         if (msr_hv || !env->has_hv_mode) {
750fcf5ef2aSThomas Huth             if (!(eaddr >> 63)) {
751fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_HRMOR];
752fcf5ef2aSThomas Huth             }
753fcf5ef2aSThomas Huth         } else {
754fcf5ef2aSThomas Huth             /* Otherwise, check VPM for RMA vs VRMA */
755fcf5ef2aSThomas Huth             if (env->spr[SPR_LPCR] & LPCR_VPM0) {
756fcf5ef2aSThomas Huth                 slb = &env->vrma_slb;
757fcf5ef2aSThomas Huth                 if (slb->sps) {
758fcf5ef2aSThomas Huth                     goto skip_slb_search;
759fcf5ef2aSThomas Huth                 }
760fcf5ef2aSThomas Huth                 /* Not much else to do here */
761fcf5ef2aSThomas Huth                 cs->exception_index = POWERPC_EXCP_MCHECK;
762fcf5ef2aSThomas Huth                 env->error_code = 0;
763fcf5ef2aSThomas Huth                 return 1;
764fcf5ef2aSThomas Huth             } else if (raddr < env->rmls) {
765fcf5ef2aSThomas Huth                 /* RMA. Check bounds in RMLS */
766fcf5ef2aSThomas Huth                 raddr |= env->spr[SPR_RMOR];
767fcf5ef2aSThomas Huth             } else {
768fcf5ef2aSThomas Huth                 /* The access failed, generate the approriate interrupt */
769fcf5ef2aSThomas Huth                 if (rwx == 2) {
7708fe08facSDavid Gibson                     ppc_hash64_set_isi(cs, SRR1_PROTFAULT);
771fcf5ef2aSThomas Huth                 } else {
772da82c73aSSuraj Jitindar Singh                     int dsisr = DSISR_PROTFAULT;
773fcf5ef2aSThomas Huth                     if (rwx == 1) {
774da82c73aSSuraj Jitindar Singh                         dsisr |= DSISR_ISSTORE;
775fcf5ef2aSThomas Huth                     }
7768fe08facSDavid Gibson                     ppc_hash64_set_dsi(cs, eaddr, dsisr);
777fcf5ef2aSThomas Huth                 }
778fcf5ef2aSThomas Huth                 return 1;
779fcf5ef2aSThomas Huth             }
780fcf5ef2aSThomas Huth         }
781fcf5ef2aSThomas Huth         tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
782fcf5ef2aSThomas Huth                      PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx,
783fcf5ef2aSThomas Huth                      TARGET_PAGE_SIZE);
784fcf5ef2aSThomas Huth         return 0;
785fcf5ef2aSThomas Huth     }
786fcf5ef2aSThomas Huth 
787fcf5ef2aSThomas Huth     /* 2. Translation is on, so look up the SLB */
788fcf5ef2aSThomas Huth     slb = slb_lookup(cpu, eaddr);
789fcf5ef2aSThomas Huth     if (!slb) {
790b2899495SSuraj Jitindar Singh         /* No entry found, check if in-memory segment tables are in use */
791ca79b3b7SDavid Gibson         if (ppc64_use_proc_tbl(cpu)) {
792b2899495SSuraj Jitindar Singh             /* TODO - Unsupported */
793b2899495SSuraj Jitindar Singh             error_report("Segment Table Support Unimplemented");
794b2899495SSuraj Jitindar Singh             exit(1);
795b2899495SSuraj Jitindar Singh         }
796b2899495SSuraj Jitindar Singh         /* Segment still not found, generate the appropriate interrupt */
797fcf5ef2aSThomas Huth         if (rwx == 2) {
798fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_ISEG;
799fcf5ef2aSThomas Huth             env->error_code = 0;
800fcf5ef2aSThomas Huth         } else {
801fcf5ef2aSThomas Huth             cs->exception_index = POWERPC_EXCP_DSEG;
802fcf5ef2aSThomas Huth             env->error_code = 0;
803fcf5ef2aSThomas Huth             env->spr[SPR_DAR] = eaddr;
804fcf5ef2aSThomas Huth         }
805fcf5ef2aSThomas Huth         return 1;
806fcf5ef2aSThomas Huth     }
807fcf5ef2aSThomas Huth 
808fcf5ef2aSThomas Huth skip_slb_search:
809fcf5ef2aSThomas Huth 
810fcf5ef2aSThomas Huth     /* 3. Check for segment level no-execute violation */
811fcf5ef2aSThomas Huth     if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) {
8128fe08facSDavid Gibson         ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD);
813fcf5ef2aSThomas Huth         return 1;
814fcf5ef2aSThomas Huth     }
815fcf5ef2aSThomas Huth 
816fcf5ef2aSThomas Huth     /* 4. Locate the PTE in the hash table */
8177222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
8187222b94aSDavid Gibson     if (ptex == -1) {
819fcf5ef2aSThomas Huth         if (rwx == 2) {
8208fe08facSDavid Gibson             ppc_hash64_set_isi(cs, SRR1_NOPTE);
821fcf5ef2aSThomas Huth         } else {
822da82c73aSSuraj Jitindar Singh             int dsisr = DSISR_NOPTE;
823fcf5ef2aSThomas Huth             if (rwx == 1) {
824da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
825fcf5ef2aSThomas Huth             }
8268fe08facSDavid Gibson             ppc_hash64_set_dsi(cs, eaddr, dsisr);
827fcf5ef2aSThomas Huth         }
828fcf5ef2aSThomas Huth         return 1;
829fcf5ef2aSThomas Huth     }
830fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU,
8317222b94aSDavid Gibson                   "found PTE at index %08" HWADDR_PRIx "\n", ptex);
832fcf5ef2aSThomas Huth 
833fcf5ef2aSThomas Huth     /* 5. Check access permissions */
834fcf5ef2aSThomas Huth 
83507a68f99SSuraj Jitindar Singh     exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
836fcf5ef2aSThomas Huth     pp_prot = ppc_hash64_pte_prot(cpu, slb, pte);
837fcf5ef2aSThomas Huth     amr_prot = ppc_hash64_amr_prot(cpu, pte);
83807a68f99SSuraj Jitindar Singh     prot = exec_prot & pp_prot & amr_prot;
839fcf5ef2aSThomas Huth 
840fcf5ef2aSThomas Huth     if ((need_prot[rwx] & ~prot) != 0) {
841fcf5ef2aSThomas Huth         /* Access right violation */
842fcf5ef2aSThomas Huth         qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
843fcf5ef2aSThomas Huth         if (rwx == 2) {
844a6152b52SSuraj Jitindar Singh             int srr1 = 0;
84507a68f99SSuraj Jitindar Singh             if (PAGE_EXEC & ~exec_prot) {
84607a68f99SSuraj Jitindar Singh                 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
84707a68f99SSuraj Jitindar Singh             } else if (PAGE_EXEC & ~pp_prot) {
848a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
849a6152b52SSuraj Jitindar Singh             }
850a6152b52SSuraj Jitindar Singh             if (PAGE_EXEC & ~amr_prot) {
851a6152b52SSuraj Jitindar Singh                 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
852a6152b52SSuraj Jitindar Singh             }
8538fe08facSDavid Gibson             ppc_hash64_set_isi(cs, srr1);
854fcf5ef2aSThomas Huth         } else {
855da82c73aSSuraj Jitindar Singh             int dsisr = 0;
856fcf5ef2aSThomas Huth             if (need_prot[rwx] & ~pp_prot) {
857da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_PROTFAULT;
858fcf5ef2aSThomas Huth             }
859fcf5ef2aSThomas Huth             if (rwx == 1) {
860da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_ISSTORE;
861fcf5ef2aSThomas Huth             }
862fcf5ef2aSThomas Huth             if (need_prot[rwx] & ~amr_prot) {
863da82c73aSSuraj Jitindar Singh                 dsisr |= DSISR_AMR;
864fcf5ef2aSThomas Huth             }
8658fe08facSDavid Gibson             ppc_hash64_set_dsi(cs, eaddr, dsisr);
866fcf5ef2aSThomas Huth         }
867fcf5ef2aSThomas Huth         return 1;
868fcf5ef2aSThomas Huth     }
869fcf5ef2aSThomas Huth 
870fcf5ef2aSThomas Huth     qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
871fcf5ef2aSThomas Huth 
872fcf5ef2aSThomas Huth     /* 6. Update PTE referenced and changed bits if necessary */
873fcf5ef2aSThomas Huth 
874fcf5ef2aSThomas Huth     new_pte1 = pte.pte1 | HPTE64_R_R; /* set referenced bit */
875fcf5ef2aSThomas Huth     if (rwx == 1) {
876fcf5ef2aSThomas Huth         new_pte1 |= HPTE64_R_C; /* set changed (dirty) bit */
877fcf5ef2aSThomas Huth     } else {
878fcf5ef2aSThomas Huth         /* Treat the page as read-only for now, so that a later write
879fcf5ef2aSThomas Huth          * will pass through this function again to set the C bit */
880fcf5ef2aSThomas Huth         prot &= ~PAGE_WRITE;
881fcf5ef2aSThomas Huth     }
882fcf5ef2aSThomas Huth 
883fcf5ef2aSThomas Huth     if (new_pte1 != pte.pte1) {
8847222b94aSDavid Gibson         ppc_hash64_store_hpte(cpu, ptex, pte.pte0, new_pte1);
885fcf5ef2aSThomas Huth     }
886fcf5ef2aSThomas Huth 
887fcf5ef2aSThomas Huth     /* 7. Determine the real address from the PTE */
888fcf5ef2aSThomas Huth 
889fcf5ef2aSThomas Huth     raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
890fcf5ef2aSThomas Huth 
891fcf5ef2aSThomas Huth     tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
892fcf5ef2aSThomas Huth                  prot, mmu_idx, 1ULL << apshift);
893fcf5ef2aSThomas Huth 
894fcf5ef2aSThomas Huth     return 0;
895fcf5ef2aSThomas Huth }
896fcf5ef2aSThomas Huth 
897fcf5ef2aSThomas Huth hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr)
898fcf5ef2aSThomas Huth {
899fcf5ef2aSThomas Huth     CPUPPCState *env = &cpu->env;
900fcf5ef2aSThomas Huth     ppc_slb_t *slb;
9017222b94aSDavid Gibson     hwaddr ptex, raddr;
902fcf5ef2aSThomas Huth     ppc_hash_pte64_t pte;
903fcf5ef2aSThomas Huth     unsigned apshift;
904fcf5ef2aSThomas Huth 
905fcf5ef2aSThomas Huth     /* Handle real mode */
906fcf5ef2aSThomas Huth     if (msr_dr == 0) {
907fcf5ef2aSThomas Huth         /* In real mode the top 4 effective address bits are ignored */
908fcf5ef2aSThomas Huth         raddr = addr & 0x0FFFFFFFFFFFFFFFULL;
909fcf5ef2aSThomas Huth 
910fcf5ef2aSThomas Huth         /* In HV mode, add HRMOR if top EA bit is clear */
911fcf5ef2aSThomas Huth         if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) {
912fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_HRMOR];
913fcf5ef2aSThomas Huth         }
914fcf5ef2aSThomas Huth 
915fcf5ef2aSThomas Huth         /* Otherwise, check VPM for RMA vs VRMA */
916fcf5ef2aSThomas Huth         if (env->spr[SPR_LPCR] & LPCR_VPM0) {
917fcf5ef2aSThomas Huth             slb = &env->vrma_slb;
918fcf5ef2aSThomas Huth             if (!slb->sps) {
919fcf5ef2aSThomas Huth                 return -1;
920fcf5ef2aSThomas Huth             }
921fcf5ef2aSThomas Huth         } else if (raddr < env->rmls) {
922fcf5ef2aSThomas Huth             /* RMA. Check bounds in RMLS */
923fcf5ef2aSThomas Huth             return raddr | env->spr[SPR_RMOR];
924fcf5ef2aSThomas Huth         } else {
925fcf5ef2aSThomas Huth             return -1;
926fcf5ef2aSThomas Huth         }
927fcf5ef2aSThomas Huth     } else {
928fcf5ef2aSThomas Huth         slb = slb_lookup(cpu, addr);
929fcf5ef2aSThomas Huth         if (!slb) {
930fcf5ef2aSThomas Huth             return -1;
931fcf5ef2aSThomas Huth         }
932fcf5ef2aSThomas Huth     }
933fcf5ef2aSThomas Huth 
9347222b94aSDavid Gibson     ptex = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift);
9357222b94aSDavid Gibson     if (ptex == -1) {
936fcf5ef2aSThomas Huth         return -1;
937fcf5ef2aSThomas Huth     }
938fcf5ef2aSThomas Huth 
939fcf5ef2aSThomas Huth     return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr)
940fcf5ef2aSThomas Huth         & TARGET_PAGE_MASK;
941fcf5ef2aSThomas Huth }
942fcf5ef2aSThomas Huth 
9437222b94aSDavid Gibson void ppc_hash64_store_hpte(PowerPCCPU *cpu, hwaddr ptex,
9447222b94aSDavid Gibson                            uint64_t pte0, uint64_t pte1)
945fcf5ef2aSThomas Huth {
9463367c62fSBenjamin Herrenschmidt     hwaddr base;
9477222b94aSDavid Gibson     hwaddr offset = ptex * HASH_PTE_SIZE_64;
948fcf5ef2aSThomas Huth 
949e57ca75cSDavid Gibson     if (cpu->vhyp) {
950e57ca75cSDavid Gibson         PPCVirtualHypervisorClass *vhc =
951e57ca75cSDavid Gibson             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
952e57ca75cSDavid Gibson         vhc->store_hpte(cpu->vhyp, ptex, pte0, pte1);
953fcf5ef2aSThomas Huth         return;
954fcf5ef2aSThomas Huth     }
9553367c62fSBenjamin Herrenschmidt     base = ppc_hash64_hpt_base(cpu);
956fcf5ef2aSThomas Huth 
95736778660SDavid Gibson     stq_phys(CPU(cpu)->as, base + offset, pte0);
95836778660SDavid Gibson     stq_phys(CPU(cpu)->as, base + offset + HASH_PTE_SIZE_64 / 2, pte1);
959fcf5ef2aSThomas Huth }
960fcf5ef2aSThomas Huth 
9617222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
962fcf5ef2aSThomas Huth                                target_ulong pte0, target_ulong pte1)
963fcf5ef2aSThomas Huth {
964fcf5ef2aSThomas Huth     /*
965fcf5ef2aSThomas Huth      * XXX: given the fact that there are too many segments to
966fcf5ef2aSThomas Huth      * invalidate, and we still don't have a tlb_flush_mask(env, n,
967fcf5ef2aSThomas Huth      * mask) in QEMU, we just invalidate all TLBs
968fcf5ef2aSThomas Huth      */
969fcf5ef2aSThomas Huth     cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
970fcf5ef2aSThomas Huth }
971fcf5ef2aSThomas Huth 
9725ad55315SDavid Gibson static void ppc_hash64_update_rmls(PowerPCCPU *cpu)
973fcf5ef2aSThomas Huth {
9748fe08facSDavid Gibson     CPUPPCState *env = &cpu->env;
975fcf5ef2aSThomas Huth     uint64_t lpcr = env->spr[SPR_LPCR];
976fcf5ef2aSThomas Huth 
977fcf5ef2aSThomas Huth     /*
978fcf5ef2aSThomas Huth      * This is the full 4 bits encoding of POWER8. Previous
979fcf5ef2aSThomas Huth      * CPUs only support a subset of these but the filtering
980fcf5ef2aSThomas Huth      * is done when writing LPCR
981fcf5ef2aSThomas Huth      */
982fcf5ef2aSThomas Huth     switch ((lpcr & LPCR_RMLS) >> LPCR_RMLS_SHIFT) {
983fcf5ef2aSThomas Huth     case 0x8: /* 32MB */
984fcf5ef2aSThomas Huth         env->rmls = 0x2000000ull;
985fcf5ef2aSThomas Huth         break;
986fcf5ef2aSThomas Huth     case 0x3: /* 64MB */
987fcf5ef2aSThomas Huth         env->rmls = 0x4000000ull;
988fcf5ef2aSThomas Huth         break;
989fcf5ef2aSThomas Huth     case 0x7: /* 128MB */
990fcf5ef2aSThomas Huth         env->rmls = 0x8000000ull;
991fcf5ef2aSThomas Huth         break;
992fcf5ef2aSThomas Huth     case 0x4: /* 256MB */
993fcf5ef2aSThomas Huth         env->rmls = 0x10000000ull;
994fcf5ef2aSThomas Huth         break;
995fcf5ef2aSThomas Huth     case 0x2: /* 1GB */
996fcf5ef2aSThomas Huth         env->rmls = 0x40000000ull;
997fcf5ef2aSThomas Huth         break;
998fcf5ef2aSThomas Huth     case 0x1: /* 16GB */
999fcf5ef2aSThomas Huth         env->rmls = 0x400000000ull;
1000fcf5ef2aSThomas Huth         break;
1001fcf5ef2aSThomas Huth     default:
1002fcf5ef2aSThomas Huth         /* What to do here ??? */
1003fcf5ef2aSThomas Huth         env->rmls = 0;
1004fcf5ef2aSThomas Huth     }
1005fcf5ef2aSThomas Huth }
1006fcf5ef2aSThomas Huth 
10075ad55315SDavid Gibson static void ppc_hash64_update_vrma(PowerPCCPU *cpu)
1008fcf5ef2aSThomas Huth {
10098fe08facSDavid Gibson     CPUPPCState *env = &cpu->env;
1010b07c59f7SDavid Gibson     const PPCHash64SegmentPageSizes *sps = NULL;
1011fcf5ef2aSThomas Huth     target_ulong esid, vsid, lpcr;
1012fcf5ef2aSThomas Huth     ppc_slb_t *slb = &env->vrma_slb;
1013fcf5ef2aSThomas Huth     uint32_t vrmasd;
1014fcf5ef2aSThomas Huth     int i;
1015fcf5ef2aSThomas Huth 
1016fcf5ef2aSThomas Huth     /* First clear it */
1017fcf5ef2aSThomas Huth     slb->esid = slb->vsid = 0;
1018fcf5ef2aSThomas Huth     slb->sps = NULL;
1019fcf5ef2aSThomas Huth 
1020fcf5ef2aSThomas Huth     /* Is VRMA enabled ? */
1021fcf5ef2aSThomas Huth     lpcr = env->spr[SPR_LPCR];
1022fcf5ef2aSThomas Huth     if (!(lpcr & LPCR_VPM0)) {
1023fcf5ef2aSThomas Huth         return;
1024fcf5ef2aSThomas Huth     }
1025fcf5ef2aSThomas Huth 
1026fcf5ef2aSThomas Huth     /* Make one up. Mostly ignore the ESID which will not be
1027fcf5ef2aSThomas Huth      * needed for translation
1028fcf5ef2aSThomas Huth      */
1029fcf5ef2aSThomas Huth     vsid = SLB_VSID_VRMA;
1030fcf5ef2aSThomas Huth     vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
1031fcf5ef2aSThomas Huth     vsid |= (vrmasd << 4) & (SLB_VSID_L | SLB_VSID_LP);
1032fcf5ef2aSThomas Huth     esid = SLB_ESID_V;
1033fcf5ef2aSThomas Huth 
1034fcf5ef2aSThomas Huth     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
1035b07c59f7SDavid Gibson         const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
1036fcf5ef2aSThomas Huth 
1037fcf5ef2aSThomas Huth         if (!sps1->page_shift) {
1038fcf5ef2aSThomas Huth             break;
1039fcf5ef2aSThomas Huth         }
1040fcf5ef2aSThomas Huth 
1041fcf5ef2aSThomas Huth         if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
1042fcf5ef2aSThomas Huth             sps = sps1;
1043fcf5ef2aSThomas Huth             break;
1044fcf5ef2aSThomas Huth         }
1045fcf5ef2aSThomas Huth     }
1046fcf5ef2aSThomas Huth 
1047fcf5ef2aSThomas Huth     if (!sps) {
1048fcf5ef2aSThomas Huth         error_report("Bad page size encoding esid 0x"TARGET_FMT_lx
1049fcf5ef2aSThomas Huth                      " vsid 0x"TARGET_FMT_lx, esid, vsid);
1050fcf5ef2aSThomas Huth         return;
1051fcf5ef2aSThomas Huth     }
1052fcf5ef2aSThomas Huth 
1053fcf5ef2aSThomas Huth     slb->vsid = vsid;
1054fcf5ef2aSThomas Huth     slb->esid = esid;
1055fcf5ef2aSThomas Huth     slb->sps = sps;
1056fcf5ef2aSThomas Huth }
1057fcf5ef2aSThomas Huth 
10585ad55315SDavid Gibson void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val)
1059fcf5ef2aSThomas Huth {
10605ad55315SDavid Gibson     CPUPPCState *env = &cpu->env;
1061fcf5ef2aSThomas Huth     uint64_t lpcr = 0;
1062fcf5ef2aSThomas Huth 
1063fcf5ef2aSThomas Huth     /* Filter out bits */
10640941d728SDavid Gibson     switch (env->mmu_model) {
10650941d728SDavid Gibson     case POWERPC_MMU_64B: /* 970 */
1066fcf5ef2aSThomas Huth         if (val & 0x40) {
1067fcf5ef2aSThomas Huth             lpcr |= LPCR_LPES0;
1068fcf5ef2aSThomas Huth         }
1069fcf5ef2aSThomas Huth         if (val & 0x8000000000000000ull) {
1070fcf5ef2aSThomas Huth             lpcr |= LPCR_LPES1;
1071fcf5ef2aSThomas Huth         }
1072fcf5ef2aSThomas Huth         if (val & 0x20) {
1073fcf5ef2aSThomas Huth             lpcr |= (0x4ull << LPCR_RMLS_SHIFT);
1074fcf5ef2aSThomas Huth         }
1075fcf5ef2aSThomas Huth         if (val & 0x4000000000000000ull) {
1076fcf5ef2aSThomas Huth             lpcr |= (0x2ull << LPCR_RMLS_SHIFT);
1077fcf5ef2aSThomas Huth         }
1078fcf5ef2aSThomas Huth         if (val & 0x2000000000000000ull) {
1079fcf5ef2aSThomas Huth             lpcr |= (0x1ull << LPCR_RMLS_SHIFT);
1080fcf5ef2aSThomas Huth         }
1081fcf5ef2aSThomas Huth         env->spr[SPR_RMOR] = ((lpcr >> 41) & 0xffffull) << 26;
1082fcf5ef2aSThomas Huth 
1083fcf5ef2aSThomas Huth         /* XXX We could also write LPID from HID4 here
1084fcf5ef2aSThomas Huth          * but since we don't tag any translation on it
1085fcf5ef2aSThomas Huth          * it doesn't actually matter
1086fcf5ef2aSThomas Huth          */
1087fcf5ef2aSThomas Huth         /* XXX For proper emulation of 970 we also need
1088fcf5ef2aSThomas Huth          * to dig HRMOR out of HID5
1089fcf5ef2aSThomas Huth          */
1090fcf5ef2aSThomas Huth         break;
10910941d728SDavid Gibson     case POWERPC_MMU_2_03: /* P5p */
1092fcf5ef2aSThomas Huth         lpcr = val & (LPCR_RMLS | LPCR_ILE |
1093fcf5ef2aSThomas Huth                       LPCR_LPES0 | LPCR_LPES1 |
1094fcf5ef2aSThomas Huth                       LPCR_RMI | LPCR_HDICE);
1095fcf5ef2aSThomas Huth         break;
10960941d728SDavid Gibson     case POWERPC_MMU_2_06: /* P7 */
1097fcf5ef2aSThomas Huth         lpcr = val & (LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_DPFD |
1098fcf5ef2aSThomas Huth                       LPCR_VRMASD | LPCR_RMLS | LPCR_ILE |
1099fcf5ef2aSThomas Huth                       LPCR_P7_PECE0 | LPCR_P7_PECE1 | LPCR_P7_PECE2 |
1100fcf5ef2aSThomas Huth                       LPCR_MER | LPCR_TC |
1101fcf5ef2aSThomas Huth                       LPCR_LPES0 | LPCR_LPES1 | LPCR_HDICE);
1102fcf5ef2aSThomas Huth         break;
11030941d728SDavid Gibson     case POWERPC_MMU_2_07: /* P8 */
1104fcf5ef2aSThomas Huth         lpcr = val & (LPCR_VPM0 | LPCR_VPM1 | LPCR_ISL | LPCR_KBV |
1105fcf5ef2aSThomas Huth                       LPCR_DPFD | LPCR_VRMASD | LPCR_RMLS | LPCR_ILE |
1106fcf5ef2aSThomas Huth                       LPCR_AIL | LPCR_ONL | LPCR_P8_PECE0 | LPCR_P8_PECE1 |
1107fcf5ef2aSThomas Huth                       LPCR_P8_PECE2 | LPCR_P8_PECE3 | LPCR_P8_PECE4 |
1108fcf5ef2aSThomas Huth                       LPCR_MER | LPCR_TC | LPCR_LPES0 | LPCR_HDICE);
1109fcf5ef2aSThomas Huth         break;
11100941d728SDavid Gibson     case POWERPC_MMU_3_00: /* P9 */
111118aa49ecSSuraj Jitindar Singh         lpcr = val & (LPCR_VPM1 | LPCR_ISL | LPCR_KBV | LPCR_DPFD |
111218aa49ecSSuraj Jitindar Singh                       (LPCR_PECE_U_MASK & LPCR_HVEE) | LPCR_ILE | LPCR_AIL |
1113a8dafa52SSuraj Jitindar Singh                       LPCR_UPRT | LPCR_EVIRT | LPCR_ONL | LPCR_HR | LPCR_LD |
111418aa49ecSSuraj Jitindar Singh                       (LPCR_PECE_L_MASK & (LPCR_PDEE | LPCR_HDEE | LPCR_EEE |
111518aa49ecSSuraj Jitindar Singh                       LPCR_DEE | LPCR_OEE)) | LPCR_MER | LPCR_GTSE | LPCR_TC |
111618aa49ecSSuraj Jitindar Singh                       LPCR_HEIC | LPCR_LPES0 | LPCR_HVICE | LPCR_HDICE);
11172b9e0a6bSBenjamin Herrenschmidt         /*
11182b9e0a6bSBenjamin Herrenschmidt          * If we have a virtual hypervisor, we need to bring back RMLS. It
11192b9e0a6bSBenjamin Herrenschmidt          * doesn't exist on an actual P9 but that's all we know how to
11202b9e0a6bSBenjamin Herrenschmidt          * configure with softmmu at the moment
11212b9e0a6bSBenjamin Herrenschmidt          */
11222b9e0a6bSBenjamin Herrenschmidt         if (cpu->vhyp) {
11232b9e0a6bSBenjamin Herrenschmidt             lpcr |= (val & LPCR_RMLS);
11242b9e0a6bSBenjamin Herrenschmidt         }
112518aa49ecSSuraj Jitindar Singh         break;
1126fcf5ef2aSThomas Huth     default:
1127fcf5ef2aSThomas Huth         ;
1128fcf5ef2aSThomas Huth     }
1129fcf5ef2aSThomas Huth     env->spr[SPR_LPCR] = lpcr;
11308fe08facSDavid Gibson     ppc_hash64_update_rmls(cpu);
11318fe08facSDavid Gibson     ppc_hash64_update_vrma(cpu);
1132fcf5ef2aSThomas Huth }
1133a059471dSDavid Gibson 
11345ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val)
11355ad55315SDavid Gibson {
11365ad55315SDavid Gibson     PowerPCCPU *cpu = ppc_env_get_cpu(env);
11375ad55315SDavid Gibson 
11385ad55315SDavid Gibson     ppc_store_lpcr(cpu, val);
11395ad55315SDavid Gibson }
11405ad55315SDavid Gibson 
1141a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu)
1142a059471dSDavid Gibson {
1143a059471dSDavid Gibson     CPUPPCState *env = &cpu->env;
1144a059471dSDavid Gibson     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1145a059471dSDavid Gibson 
114621e405f1SDavid Gibson     if (!pcc->hash64_opts) {
114721e405f1SDavid Gibson         assert(!(env->mmu_model & POWERPC_MMU_64));
114821e405f1SDavid Gibson         return;
114921e405f1SDavid Gibson     }
115021e405f1SDavid Gibson 
115121e405f1SDavid Gibson     cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
115221e405f1SDavid Gibson }
115321e405f1SDavid Gibson 
115421e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu)
115521e405f1SDavid Gibson {
115621e405f1SDavid Gibson     g_free(cpu->hash64_opts);
115721e405f1SDavid Gibson }
115821e405f1SDavid Gibson 
115921e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = {
116058969eeeSDavid Gibson     .flags = 0,
116167d7d66fSDavid Gibson     .slb_size = 64,
1162a059471dSDavid Gibson     .sps = {
1163a059471dSDavid Gibson         { .page_shift = 12, /* 4K */
1164a059471dSDavid Gibson           .slb_enc = 0,
1165a059471dSDavid Gibson           .enc = { { .page_shift = 12, .pte_enc = 0 } }
1166a059471dSDavid Gibson         },
1167a059471dSDavid Gibson         { .page_shift = 24, /* 16M */
1168a059471dSDavid Gibson           .slb_enc = 0x100,
1169a059471dSDavid Gibson           .enc = { { .page_shift = 24, .pte_enc = 0 } }
1170a059471dSDavid Gibson         },
1171a059471dSDavid Gibson     },
1172a059471dSDavid Gibson };
1173b07c59f7SDavid Gibson 
1174b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = {
117526cd35b8SDavid Gibson     .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
117667d7d66fSDavid Gibson     .slb_size = 32,
1177b07c59f7SDavid Gibson     .sps = {
1178b07c59f7SDavid Gibson         {
1179b07c59f7SDavid Gibson             .page_shift = 12, /* 4K */
1180b07c59f7SDavid Gibson             .slb_enc = 0,
1181b07c59f7SDavid Gibson             .enc = { { .page_shift = 12, .pte_enc = 0 },
1182b07c59f7SDavid Gibson                      { .page_shift = 16, .pte_enc = 0x7 },
1183b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x38 }, },
1184b07c59f7SDavid Gibson         },
1185b07c59f7SDavid Gibson         {
1186b07c59f7SDavid Gibson             .page_shift = 16, /* 64K */
1187b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_64K,
1188b07c59f7SDavid Gibson             .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1189b07c59f7SDavid Gibson                      { .page_shift = 24, .pte_enc = 0x8 }, },
1190b07c59f7SDavid Gibson         },
1191b07c59f7SDavid Gibson         {
1192b07c59f7SDavid Gibson             .page_shift = 24, /* 16M */
1193b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16M,
1194b07c59f7SDavid Gibson             .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1195b07c59f7SDavid Gibson         },
1196b07c59f7SDavid Gibson         {
1197b07c59f7SDavid Gibson             .page_shift = 34, /* 16G */
1198b07c59f7SDavid Gibson             .slb_enc = SLB_VSID_16G,
1199b07c59f7SDavid Gibson             .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },
1200b07c59f7SDavid Gibson         },
1201b07c59f7SDavid Gibson     }
1202b07c59f7SDavid Gibson };
120327f00f0aSDavid Gibson 
120427f00f0aSDavid Gibson void ppc_hash64_filter_pagesizes(PowerPCCPU *cpu,
120527f00f0aSDavid Gibson                                  bool (*cb)(void *, uint32_t, uint32_t),
120627f00f0aSDavid Gibson                                  void *opaque)
120727f00f0aSDavid Gibson {
120827f00f0aSDavid Gibson     PPCHash64Options *opts = cpu->hash64_opts;
120927f00f0aSDavid Gibson     int i;
121027f00f0aSDavid Gibson     int n = 0;
121127f00f0aSDavid Gibson     bool ci_largepage = false;
121227f00f0aSDavid Gibson 
121327f00f0aSDavid Gibson     assert(opts);
121427f00f0aSDavid Gibson 
121527f00f0aSDavid Gibson     n = 0;
121627f00f0aSDavid Gibson     for (i = 0; i < ARRAY_SIZE(opts->sps); i++) {
121727f00f0aSDavid Gibson         PPCHash64SegmentPageSizes *sps = &opts->sps[i];
121827f00f0aSDavid Gibson         int j;
121927f00f0aSDavid Gibson         int m = 0;
122027f00f0aSDavid Gibson 
122127f00f0aSDavid Gibson         assert(n <= i);
122227f00f0aSDavid Gibson 
122327f00f0aSDavid Gibson         if (!sps->page_shift) {
122427f00f0aSDavid Gibson             break;
122527f00f0aSDavid Gibson         }
122627f00f0aSDavid Gibson 
122727f00f0aSDavid Gibson         for (j = 0; j < ARRAY_SIZE(sps->enc); j++) {
122827f00f0aSDavid Gibson             PPCHash64PageSize *ps = &sps->enc[j];
122927f00f0aSDavid Gibson 
123027f00f0aSDavid Gibson             assert(m <= j);
123127f00f0aSDavid Gibson             if (!ps->page_shift) {
123227f00f0aSDavid Gibson                 break;
123327f00f0aSDavid Gibson             }
123427f00f0aSDavid Gibson 
123527f00f0aSDavid Gibson             if (cb(opaque, sps->page_shift, ps->page_shift)) {
123627f00f0aSDavid Gibson                 if (ps->page_shift >= 16) {
123727f00f0aSDavid Gibson                     ci_largepage = true;
123827f00f0aSDavid Gibson                 }
123927f00f0aSDavid Gibson                 sps->enc[m++] = *ps;
124027f00f0aSDavid Gibson             }
124127f00f0aSDavid Gibson         }
124227f00f0aSDavid Gibson 
124327f00f0aSDavid Gibson         /* Clear rest of the row */
124427f00f0aSDavid Gibson         for (j = m; j < ARRAY_SIZE(sps->enc); j++) {
124527f00f0aSDavid Gibson             memset(&sps->enc[j], 0, sizeof(sps->enc[j]));
124627f00f0aSDavid Gibson         }
124727f00f0aSDavid Gibson 
124827f00f0aSDavid Gibson         if (m) {
124927f00f0aSDavid Gibson             n++;
125027f00f0aSDavid Gibson         }
125127f00f0aSDavid Gibson     }
125227f00f0aSDavid Gibson 
125327f00f0aSDavid Gibson     /* Clear the rest of the table */
125427f00f0aSDavid Gibson     for (i = n; i < ARRAY_SIZE(opts->sps); i++) {
125527f00f0aSDavid Gibson         memset(&opts->sps[i], 0, sizeof(opts->sps[i]));
125627f00f0aSDavid Gibson     }
125727f00f0aSDavid Gibson 
125827f00f0aSDavid Gibson     if (!ci_largepage) {
125927f00f0aSDavid Gibson         opts->flags &= ~PPC_HASH64_CI_LARGEPAGE;
126027f00f0aSDavid Gibson     }
126127f00f0aSDavid Gibson }
1262