blob: 34e20fa3a96027041172695afa931c27293b188c [file] [log] [blame]
David Gibson10b46522013-03-12 00:31:06 +00001/*
2 * PowerPC MMU, TLB, SLB and BAT emulation helpers for QEMU.
3 *
4 * Copyright (c) 2003-2007 Jocelyn Mayer
5 * Copyright (c) 2013 David Gibson, IBM Corporation
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 */
20#include "cpu.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070021#include "exec/helper-proto.h"
David Gibson10b46522013-03-12 00:31:06 +000022#include "sysemu/kvm.h"
23#include "kvm_ppc.h"
24#include "mmu-hash64.h"
25
26//#define DEBUG_SLB
27
28#ifdef DEBUG_SLB
Paolo Bonzini48880da2015-11-13 13:34:23 +010029# define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
David Gibson10b46522013-03-12 00:31:06 +000030#else
31# define LOG_SLB(...) do { } while (0)
32#endif
33
34/*
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +010035 * Used to indicate whether we have allocated htab in the
36 * host kernel
37 */
38bool kvmppc_kern_htab;
39/*
David Gibson10b46522013-03-12 00:31:06 +000040 * SLB handling
41 */
42
David Gibson04808842013-03-12 00:31:09 +000043static ppc_slb_t *slb_lookup(CPUPPCState *env, target_ulong eaddr)
David Gibson10b46522013-03-12 00:31:06 +000044{
45 uint64_t esid_256M, esid_1T;
46 int n;
47
48 LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr);
49
50 esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V;
51 esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V;
52
53 for (n = 0; n < env->slb_nr; n++) {
54 ppc_slb_t *slb = &env->slb[n];
55
56 LOG_SLB("%s: slot %d %016" PRIx64 " %016"
57 PRIx64 "\n", __func__, n, slb->esid, slb->vsid);
58 /* We check for 1T matches on all MMUs here - if the MMU
59 * doesn't have 1T segment support, we will have prevented 1T
60 * entries from being inserted in the slbmte code. */
61 if (((slb->esid == esid_256M) &&
62 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M))
63 || ((slb->esid == esid_1T) &&
64 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) {
65 return slb;
66 }
67 }
68
69 return NULL;
70}
71
72void dump_slb(FILE *f, fprintf_function cpu_fprintf, CPUPPCState *env)
73{
74 int i;
75 uint64_t slbe, slbv;
76
Andreas Färbercb446ec2013-05-01 14:24:52 +020077 cpu_synchronize_state(CPU(ppc_env_get_cpu(env)));
David Gibson10b46522013-03-12 00:31:06 +000078
79 cpu_fprintf(f, "SLB\tESID\t\t\tVSID\n");
80 for (i = 0; i < env->slb_nr; i++) {
81 slbe = env->slb[i].esid;
82 slbv = env->slb[i].vsid;
83 if (slbe == 0 && slbv == 0) {
84 continue;
85 }
86 cpu_fprintf(f, "%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n",
87 i, slbe, slbv);
88 }
89}
90
91void helper_slbia(CPUPPCState *env)
92{
Andreas Färber00c8cb02013-09-04 02:19:44 +020093 PowerPCCPU *cpu = ppc_env_get_cpu(env);
David Gibson10b46522013-03-12 00:31:06 +000094 int n, do_invalidate;
95
96 do_invalidate = 0;
97 /* XXX: Warning: slbia never invalidates the first segment */
98 for (n = 1; n < env->slb_nr; n++) {
99 ppc_slb_t *slb = &env->slb[n];
100
101 if (slb->esid & SLB_ESID_V) {
102 slb->esid &= ~SLB_ESID_V;
103 /* XXX: given the fact that segment size is 256 MB or 1TB,
104 * and we still don't have a tlb_flush_mask(env, n, mask)
105 * in QEMU, we just invalidate all TLBs
106 */
107 do_invalidate = 1;
108 }
109 }
110 if (do_invalidate) {
Andreas Färber00c8cb02013-09-04 02:19:44 +0200111 tlb_flush(CPU(cpu), 1);
David Gibson10b46522013-03-12 00:31:06 +0000112 }
113}
114
115void helper_slbie(CPUPPCState *env, target_ulong addr)
116{
Andreas Färber00c8cb02013-09-04 02:19:44 +0200117 PowerPCCPU *cpu = ppc_env_get_cpu(env);
David Gibson10b46522013-03-12 00:31:06 +0000118 ppc_slb_t *slb;
119
120 slb = slb_lookup(env, addr);
121 if (!slb) {
122 return;
123 }
124
125 if (slb->esid & SLB_ESID_V) {
126 slb->esid &= ~SLB_ESID_V;
127
128 /* XXX: given the fact that segment size is 256 MB or 1TB,
129 * and we still don't have a tlb_flush_mask(env, n, mask)
130 * in QEMU, we just invalidate all TLBs
131 */
Andreas Färber00c8cb02013-09-04 02:19:44 +0200132 tlb_flush(CPU(cpu), 1);
David Gibson10b46522013-03-12 00:31:06 +0000133 }
134}
135
136int ppc_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs)
137{
138 int slot = rb & 0xfff;
139 ppc_slb_t *slb = &env->slb[slot];
140
141 if (rb & (0x1000 - env->slb_nr)) {
142 return -1; /* Reserved bits set or slot too high */
143 }
144 if (rs & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
145 return -1; /* Bad segment size */
146 }
147 if ((rs & SLB_VSID_B) && !(env->mmu_model & POWERPC_MMU_1TSEG)) {
148 return -1; /* 1T segment on MMU that doesn't support it */
149 }
150
151 /* Mask out the slot number as we store the entry */
152 slb->esid = rb & (SLB_ESID_ESID | SLB_ESID_V);
153 slb->vsid = rs;
154
155 LOG_SLB("%s: %d " TARGET_FMT_lx " - " TARGET_FMT_lx " => %016" PRIx64
156 " %016" PRIx64 "\n", __func__, slot, rb, rs,
157 slb->esid, slb->vsid);
158
159 return 0;
160}
161
162static int ppc_load_slb_esid(CPUPPCState *env, target_ulong rb,
163 target_ulong *rt)
164{
165 int slot = rb & 0xfff;
166 ppc_slb_t *slb = &env->slb[slot];
167
168 if (slot >= env->slb_nr) {
169 return -1;
170 }
171
172 *rt = slb->esid;
173 return 0;
174}
175
176static int ppc_load_slb_vsid(CPUPPCState *env, target_ulong rb,
177 target_ulong *rt)
178{
179 int slot = rb & 0xfff;
180 ppc_slb_t *slb = &env->slb[slot];
181
182 if (slot >= env->slb_nr) {
183 return -1;
184 }
185
186 *rt = slb->vsid;
187 return 0;
188}
189
190void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs)
191{
192 if (ppc_store_slb(env, rb, rs) < 0) {
193 helper_raise_exception_err(env, POWERPC_EXCP_PROGRAM,
194 POWERPC_EXCP_INVAL);
195 }
196}
197
198target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb)
199{
200 target_ulong rt = 0;
201
202 if (ppc_load_slb_esid(env, rb, &rt) < 0) {
203 helper_raise_exception_err(env, POWERPC_EXCP_PROGRAM,
204 POWERPC_EXCP_INVAL);
205 }
206 return rt;
207}
208
209target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb)
210{
211 target_ulong rt = 0;
212
213 if (ppc_load_slb_vsid(env, rb, &rt) < 0) {
214 helper_raise_exception_err(env, POWERPC_EXCP_PROGRAM,
215 POWERPC_EXCP_INVAL);
216 }
217 return rt;
218}
David Gibson9d7c3f42013-03-12 00:31:07 +0000219
220/*
221 * 64-bit hash table MMU handling
222 */
223
David Gibsone01b4442013-03-12 00:31:40 +0000224static int ppc_hash64_pte_prot(CPUPPCState *env,
225 ppc_slb_t *slb, ppc_hash_pte64_t pte)
David Gibson496272a2013-03-12 00:31:14 +0000226{
David Gibsone01b4442013-03-12 00:31:40 +0000227 unsigned pp, key;
228 /* Some pp bit combinations have undefined behaviour, so default
229 * to no access in those cases */
230 int prot = 0;
David Gibson496272a2013-03-12 00:31:14 +0000231
David Gibsone01b4442013-03-12 00:31:40 +0000232 key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP)
233 : (slb->vsid & SLB_VSID_KS));
234 pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
235
David Gibson496272a2013-03-12 00:31:14 +0000236 if (key == 0) {
237 switch (pp) {
238 case 0x0:
239 case 0x1:
240 case 0x2:
David Gibsone01b4442013-03-12 00:31:40 +0000241 prot = PAGE_READ | PAGE_WRITE;
242 break;
243
David Gibson496272a2013-03-12 00:31:14 +0000244 case 0x3:
245 case 0x6:
David Gibsone01b4442013-03-12 00:31:40 +0000246 prot = PAGE_READ;
David Gibson496272a2013-03-12 00:31:14 +0000247 break;
248 }
249 } else {
250 switch (pp) {
251 case 0x0:
252 case 0x6:
David Gibsone01b4442013-03-12 00:31:40 +0000253 prot = 0;
David Gibson496272a2013-03-12 00:31:14 +0000254 break;
David Gibsone01b4442013-03-12 00:31:40 +0000255
David Gibson496272a2013-03-12 00:31:14 +0000256 case 0x1:
257 case 0x3:
David Gibsone01b4442013-03-12 00:31:40 +0000258 prot = PAGE_READ;
David Gibson496272a2013-03-12 00:31:14 +0000259 break;
David Gibsone01b4442013-03-12 00:31:40 +0000260
David Gibson496272a2013-03-12 00:31:14 +0000261 case 0x2:
David Gibsone01b4442013-03-12 00:31:40 +0000262 prot = PAGE_READ | PAGE_WRITE;
David Gibson496272a2013-03-12 00:31:14 +0000263 break;
264 }
265 }
David Gibsone01b4442013-03-12 00:31:40 +0000266
267 /* No execute if either noexec or guarded bits set */
David Gibson57d0a392013-03-12 00:31:41 +0000268 if (!(pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G)
269 || (slb->vsid & SLB_VSID_N)) {
David Gibsone01b4442013-03-12 00:31:40 +0000270 prot |= PAGE_EXEC;
David Gibson496272a2013-03-12 00:31:14 +0000271 }
272
David Gibsone01b4442013-03-12 00:31:40 +0000273 return prot;
David Gibson496272a2013-03-12 00:31:14 +0000274}
275
David Gibsonf80872e2013-03-12 00:31:47 +0000276static int ppc_hash64_amr_prot(CPUPPCState *env, ppc_hash_pte64_t pte)
277{
278 int key, amrbits;
Cédric Le Goater363248e2014-02-04 18:21:39 +0100279 int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
David Gibsonf80872e2013-03-12 00:31:47 +0000280
281
282 /* Only recent MMUs implement Virtual Page Class Key Protection */
283 if (!(env->mmu_model & POWERPC_MMU_AMR)) {
Cédric Le Goater363248e2014-02-04 18:21:39 +0100284 return prot;
David Gibsonf80872e2013-03-12 00:31:47 +0000285 }
286
287 key = HPTE64_R_KEY(pte.pte1);
288 amrbits = (env->spr[SPR_AMR] >> 2*(31 - key)) & 0x3;
289
290 /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
291 /* env->spr[SPR_AMR]); */
292
Cédric Le Goater363248e2014-02-04 18:21:39 +0100293 /*
294 * A store is permitted if the AMR bit is 0. Remove write
295 * protection if it is set.
296 */
David Gibsonf80872e2013-03-12 00:31:47 +0000297 if (amrbits & 0x2) {
Cédric Le Goater363248e2014-02-04 18:21:39 +0100298 prot &= ~PAGE_WRITE;
David Gibsonf80872e2013-03-12 00:31:47 +0000299 }
Cédric Le Goater363248e2014-02-04 18:21:39 +0100300 /*
301 * A load is permitted if the AMR bit is 0. Remove read
302 * protection if it is set.
303 */
David Gibsonf80872e2013-03-12 00:31:47 +0000304 if (amrbits & 0x1) {
Cédric Le Goater363248e2014-02-04 18:21:39 +0100305 prot &= ~PAGE_READ;
David Gibsonf80872e2013-03-12 00:31:47 +0000306 }
307
308 return prot;
309}
310
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100311uint64_t ppc_hash64_start_access(PowerPCCPU *cpu, target_ulong pte_index)
312{
313 uint64_t token = 0;
314 hwaddr pte_offset;
315
316 pte_offset = pte_index * HASH_PTE_SIZE_64;
317 if (kvmppc_kern_htab) {
318 /*
319 * HTAB is controlled by KVM. Fetch the PTEG into a new buffer.
320 */
321 token = kvmppc_hash64_read_pteg(cpu, pte_index);
322 if (token) {
323 return token;
324 }
325 /*
326 * pteg read failed, even though we have allocated htab via
327 * kvmppc_reset_htab.
328 */
329 return 0;
330 }
331 /*
332 * HTAB is controlled by QEMU. Just point to the internally
333 * accessible PTEG.
334 */
335 if (cpu->env.external_htab) {
336 token = (uint64_t)(uintptr_t) cpu->env.external_htab + pte_offset;
337 } else if (cpu->env.htab_base) {
338 token = cpu->env.htab_base + pte_offset;
339 }
340 return token;
341}
342
343void ppc_hash64_stop_access(uint64_t token)
344{
345 if (kvmppc_kern_htab) {
Stefan Weila9ab06d2015-03-07 23:16:38 +0100346 kvmppc_hash64_free_pteg(token);
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100347 }
348}
349
350static hwaddr ppc_hash64_pteg_search(CPUPPCState *env, hwaddr hash,
David Gibsonaea390e2013-03-12 00:31:28 +0000351 bool secondary, target_ulong ptem,
352 ppc_hash_pte64_t *pte)
353{
David Gibsonaea390e2013-03-12 00:31:28 +0000354 int i;
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100355 uint64_t token;
356 target_ulong pte0, pte1;
357 target_ulong pte_index;
David Gibsonaea390e2013-03-12 00:31:28 +0000358
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100359 pte_index = (hash & env->htab_mask) * HPTES_PER_GROUP;
360 token = ppc_hash64_start_access(ppc_env_get_cpu(env), pte_index);
361 if (!token) {
362 return -1;
363 }
David Gibsonaea390e2013-03-12 00:31:28 +0000364 for (i = 0; i < HPTES_PER_GROUP; i++) {
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100365 pte0 = ppc_hash64_load_hpte0(env, token, i);
366 pte1 = ppc_hash64_load_hpte1(env, token, i);
David Gibsonaea390e2013-03-12 00:31:28 +0000367
368 if ((pte0 & HPTE64_V_VALID)
369 && (secondary == !!(pte0 & HPTE64_V_SECONDARY))
370 && HPTE64_V_COMPARE(pte0, ptem)) {
371 pte->pte0 = pte0;
372 pte->pte1 = pte1;
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100373 ppc_hash64_stop_access(token);
374 return (pte_index + i) * HASH_PTE_SIZE_64;
David Gibsonaea390e2013-03-12 00:31:28 +0000375 }
David Gibsonaea390e2013-03-12 00:31:28 +0000376 }
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100377 ppc_hash64_stop_access(token);
378 /*
379 * We didn't find a valid entry.
380 */
David Gibsonaea390e2013-03-12 00:31:28 +0000381 return -1;
382}
383
Aneesh Kumar K.Vad3e67d2015-01-26 19:51:58 +0530384static uint64_t ppc_hash64_page_shift(ppc_slb_t *slb)
385{
386 uint64_t epnshift;
387
388 /* Page size according to the SLB, which we use to generate the
389 * EPN for hash table lookup.. When we implement more recent MMU
390 * extensions this might be different from the actual page size
391 * encoded in the PTE */
392 if ((slb->vsid & SLB_VSID_LLP_MASK) == SLB_VSID_4K) {
393 epnshift = TARGET_PAGE_BITS;
394 } else if ((slb->vsid & SLB_VSID_LLP_MASK) == SLB_VSID_64K) {
395 epnshift = TARGET_PAGE_BITS_64K;
396 } else {
397 epnshift = TARGET_PAGE_BITS_16M;
398 }
399 return epnshift;
400}
401
David Gibson7f3bdc22013-03-12 00:31:30 +0000402static hwaddr ppc_hash64_htab_lookup(CPUPPCState *env,
403 ppc_slb_t *slb, target_ulong eaddr,
404 ppc_hash_pte64_t *pte)
David Gibsonc69b6152013-03-12 00:31:08 +0000405{
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100406 hwaddr pte_offset;
David Gibsona1ff7512013-03-12 00:31:29 +0000407 hwaddr hash;
David Gibson18148892013-03-12 00:31:31 +0000408 uint64_t vsid, epnshift, epnmask, epn, ptem;
David Gibsona1ff7512013-03-12 00:31:29 +0000409
Aneesh Kumar K.Vad3e67d2015-01-26 19:51:58 +0530410 epnshift = ppc_hash64_page_shift(slb);
David Gibson18148892013-03-12 00:31:31 +0000411 epnmask = ~((1ULL << epnshift) - 1);
David Gibsona1ff7512013-03-12 00:31:29 +0000412
David Gibsona1ff7512013-03-12 00:31:29 +0000413 if (slb->vsid & SLB_VSID_B) {
David Gibson18148892013-03-12 00:31:31 +0000414 /* 1TB segment */
415 vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
416 epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
417 hash = vsid ^ (vsid << 25) ^ (epn >> epnshift);
David Gibsona1ff7512013-03-12 00:31:29 +0000418 } else {
David Gibson18148892013-03-12 00:31:31 +0000419 /* 256M segment */
420 vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
421 epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
422 hash = vsid ^ (epn >> epnshift);
David Gibsona1ff7512013-03-12 00:31:29 +0000423 }
David Gibson18148892013-03-12 00:31:31 +0000424 ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
David Gibsona1ff7512013-03-12 00:31:29 +0000425
David Gibsona1ff7512013-03-12 00:31:29 +0000426 /* Page address translation */
Antony Pavlov339aaf52014-12-13 19:48:18 +0300427 qemu_log_mask(CPU_LOG_MMU,
428 "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
David Gibsona1ff7512013-03-12 00:31:29 +0000429 " hash " TARGET_FMT_plx "\n",
430 env->htab_base, env->htab_mask, hash);
431
David Gibsona1ff7512013-03-12 00:31:29 +0000432 /* Primary PTEG lookup */
Antony Pavlov339aaf52014-12-13 19:48:18 +0300433 qemu_log_mask(CPU_LOG_MMU,
434 "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
David Gibsona1ff7512013-03-12 00:31:29 +0000435 " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
436 " hash=" TARGET_FMT_plx "\n",
437 env->htab_base, env->htab_mask, vsid, ptem, hash);
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100438 pte_offset = ppc_hash64_pteg_search(env, hash, 0, ptem, pte);
David Gibson7f3bdc22013-03-12 00:31:30 +0000439
David Gibsona1ff7512013-03-12 00:31:29 +0000440 if (pte_offset == -1) {
441 /* Secondary PTEG lookup */
Antony Pavlov339aaf52014-12-13 19:48:18 +0300442 qemu_log_mask(CPU_LOG_MMU,
443 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
David Gibsona1ff7512013-03-12 00:31:29 +0000444 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
445 " hash=" TARGET_FMT_plx "\n", env->htab_base,
446 env->htab_mask, vsid, ptem, ~hash);
447
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +0100448 pte_offset = ppc_hash64_pteg_search(env, ~hash, 1, ptem, pte);
David Gibsona1ff7512013-03-12 00:31:29 +0000449 }
450
David Gibson7f3bdc22013-03-12 00:31:30 +0000451 return pte_offset;
David Gibsonc69b6152013-03-12 00:31:08 +0000452}
David Gibson04808842013-03-12 00:31:09 +0000453
David Gibson6d11d992013-03-12 00:31:43 +0000454static hwaddr ppc_hash64_pte_raddr(ppc_slb_t *slb, ppc_hash_pte64_t pte,
455 target_ulong eaddr)
456{
Aneesh Kumar K.Vad3e67d2015-01-26 19:51:58 +0530457 hwaddr mask;
458 int target_page_bits;
David Gibson75d5ec82013-03-12 00:31:44 +0000459 hwaddr rpn = pte.pte1 & HPTE64_R_RPN;
Aneesh Kumar K.Vad3e67d2015-01-26 19:51:58 +0530460 /*
461 * We support 4K, 64K and 16M now
462 */
463 target_page_bits = ppc_hash64_page_shift(slb);
464 mask = (1ULL << target_page_bits) - 1;
David Gibson6d11d992013-03-12 00:31:43 +0000465 return (rpn & ~mask) | (eaddr & mask);
466}
467
Andreas Färberd0e39c52013-09-02 14:14:24 +0200468int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, target_ulong eaddr,
David Gibsoncaa597b2013-03-12 00:31:46 +0000469 int rwx, int mmu_idx)
David Gibson04808842013-03-12 00:31:09 +0000470{
Andreas Färberd0e39c52013-09-02 14:14:24 +0200471 CPUState *cs = CPU(cpu);
472 CPUPPCState *env = &cpu->env;
David Gibson04808842013-03-12 00:31:09 +0000473 ppc_slb_t *slb;
David Gibson7f3bdc22013-03-12 00:31:30 +0000474 hwaddr pte_offset;
475 ppc_hash_pte64_t pte;
David Gibsonf80872e2013-03-12 00:31:47 +0000476 int pp_prot, amr_prot, prot;
David Gibsonb3440742013-03-12 00:31:42 +0000477 uint64_t new_pte1;
David Gibsone01b4442013-03-12 00:31:40 +0000478 const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC};
David Gibsoncaa597b2013-03-12 00:31:46 +0000479 hwaddr raddr;
David Gibson04808842013-03-12 00:31:09 +0000480
David Gibson6a980112013-03-12 00:31:32 +0000481 assert((rwx == 0) || (rwx == 1) || (rwx == 2));
482
David Gibson65d61642013-03-12 00:31:23 +0000483 /* 1. Handle real mode accesses */
484 if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) {
485 /* Translation is off */
486 /* In real mode the top 4 effective address bits are ignored */
David Gibsoncaa597b2013-03-12 00:31:46 +0000487 raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
Andreas Färber0c591eb2013-09-03 13:59:37 +0200488 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
David Gibsoncaa597b2013-03-12 00:31:46 +0000489 PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx,
490 TARGET_PAGE_SIZE);
David Gibson65d61642013-03-12 00:31:23 +0000491 return 0;
492 }
493
David Gibsonbb218042013-03-12 00:31:26 +0000494 /* 2. Translation is on, so look up the SLB */
David Gibson04808842013-03-12 00:31:09 +0000495 slb = slb_lookup(env, eaddr);
David Gibsonbb218042013-03-12 00:31:26 +0000496
David Gibson04808842013-03-12 00:31:09 +0000497 if (!slb) {
David Gibsoncaa597b2013-03-12 00:31:46 +0000498 if (rwx == 2) {
Andreas Färber27103422013-08-26 08:31:06 +0200499 cs->exception_index = POWERPC_EXCP_ISEG;
David Gibsoncaa597b2013-03-12 00:31:46 +0000500 env->error_code = 0;
501 } else {
Andreas Färber27103422013-08-26 08:31:06 +0200502 cs->exception_index = POWERPC_EXCP_DSEG;
David Gibsoncaa597b2013-03-12 00:31:46 +0000503 env->error_code = 0;
504 env->spr[SPR_DAR] = eaddr;
505 }
506 return 1;
David Gibson04808842013-03-12 00:31:09 +0000507 }
508
David Gibsonbb218042013-03-12 00:31:26 +0000509 /* 3. Check for segment level no-execute violation */
510 if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) {
Andreas Färber27103422013-08-26 08:31:06 +0200511 cs->exception_index = POWERPC_EXCP_ISI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000512 env->error_code = 0x10000000;
513 return 1;
David Gibsonbb218042013-03-12 00:31:26 +0000514 }
515
David Gibson7f3bdc22013-03-12 00:31:30 +0000516 /* 4. Locate the PTE in the hash table */
517 pte_offset = ppc_hash64_htab_lookup(env, slb, eaddr, &pte);
518 if (pte_offset == -1) {
David Gibsoncaa597b2013-03-12 00:31:46 +0000519 if (rwx == 2) {
Andreas Färber27103422013-08-26 08:31:06 +0200520 cs->exception_index = POWERPC_EXCP_ISI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000521 env->error_code = 0x40000000;
522 } else {
Andreas Färber27103422013-08-26 08:31:06 +0200523 cs->exception_index = POWERPC_EXCP_DSI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000524 env->error_code = 0;
525 env->spr[SPR_DAR] = eaddr;
526 if (rwx == 1) {
527 env->spr[SPR_DSISR] = 0x42000000;
528 } else {
529 env->spr[SPR_DSISR] = 0x40000000;
530 }
531 }
532 return 1;
David Gibson7f3bdc22013-03-12 00:31:30 +0000533 }
Antony Pavlov339aaf52014-12-13 19:48:18 +0300534 qemu_log_mask(CPU_LOG_MMU,
535 "found PTE at offset %08" HWADDR_PRIx "\n", pte_offset);
David Gibson04808842013-03-12 00:31:09 +0000536
David Gibson7f3bdc22013-03-12 00:31:30 +0000537 /* 5. Check access permissions */
David Gibson7f3bdc22013-03-12 00:31:30 +0000538
David Gibsonf80872e2013-03-12 00:31:47 +0000539 pp_prot = ppc_hash64_pte_prot(env, slb, pte);
540 amr_prot = ppc_hash64_amr_prot(env, pte);
541 prot = pp_prot & amr_prot;
David Gibson6a980112013-03-12 00:31:32 +0000542
David Gibsoncaa597b2013-03-12 00:31:46 +0000543 if ((need_prot[rwx] & ~prot) != 0) {
David Gibson6a980112013-03-12 00:31:32 +0000544 /* Access right violation */
Antony Pavlov339aaf52014-12-13 19:48:18 +0300545 qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
David Gibsoncaa597b2013-03-12 00:31:46 +0000546 if (rwx == 2) {
Andreas Färber27103422013-08-26 08:31:06 +0200547 cs->exception_index = POWERPC_EXCP_ISI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000548 env->error_code = 0x08000000;
549 } else {
David Gibsonf80872e2013-03-12 00:31:47 +0000550 target_ulong dsisr = 0;
551
Andreas Färber27103422013-08-26 08:31:06 +0200552 cs->exception_index = POWERPC_EXCP_DSI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000553 env->error_code = 0;
554 env->spr[SPR_DAR] = eaddr;
David Gibsonf80872e2013-03-12 00:31:47 +0000555 if (need_prot[rwx] & ~pp_prot) {
556 dsisr |= 0x08000000;
David Gibsoncaa597b2013-03-12 00:31:46 +0000557 }
David Gibsonf80872e2013-03-12 00:31:47 +0000558 if (rwx == 1) {
559 dsisr |= 0x02000000;
560 }
561 if (need_prot[rwx] & ~amr_prot) {
562 dsisr |= 0x00200000;
563 }
564 env->spr[SPR_DSISR] = dsisr;
David Gibsoncaa597b2013-03-12 00:31:46 +0000565 }
566 return 1;
David Gibson6a980112013-03-12 00:31:32 +0000567 }
568
Antony Pavlov339aaf52014-12-13 19:48:18 +0300569 qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
David Gibson87dc3fd2013-03-12 00:31:38 +0000570
571 /* 6. Update PTE referenced and changed bits if necessary */
572
David Gibsonb3440742013-03-12 00:31:42 +0000573 new_pte1 = pte.pte1 | HPTE64_R_R; /* set referenced bit */
574 if (rwx == 1) {
575 new_pte1 |= HPTE64_R_C; /* set changed (dirty) bit */
576 } else {
577 /* Treat the page as read-only for now, so that a later write
578 * will pass through this function again to set the C bit */
David Gibsoncaa597b2013-03-12 00:31:46 +0000579 prot &= ~PAGE_WRITE;
David Gibsonb3440742013-03-12 00:31:42 +0000580 }
581
582 if (new_pte1 != pte.pte1) {
Aneesh Kumar K.V3f941702014-02-20 18:52:31 +0100583 ppc_hash64_store_hpte(env, pte_offset / HASH_PTE_SIZE_64,
584 pte.pte0, new_pte1);
David Gibson7f3bdc22013-03-12 00:31:30 +0000585 }
586
David Gibson6d11d992013-03-12 00:31:43 +0000587 /* 7. Determine the real address from the PTE */
David Gibsone01b4442013-03-12 00:31:40 +0000588
David Gibsoncaa597b2013-03-12 00:31:46 +0000589 raddr = ppc_hash64_pte_raddr(slb, pte, eaddr);
590
Andreas Färber0c591eb2013-09-03 13:59:37 +0200591 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
David Gibsoncaa597b2013-03-12 00:31:46 +0000592 prot, mmu_idx, TARGET_PAGE_SIZE);
David Gibson6d11d992013-03-12 00:31:43 +0000593
David Gibsone01b4442013-03-12 00:31:40 +0000594 return 0;
David Gibson04808842013-03-12 00:31:09 +0000595}
David Gibson629bd512013-03-12 00:31:11 +0000596
David Gibsonf2ad6be2013-03-12 00:31:13 +0000597hwaddr ppc_hash64_get_phys_page_debug(CPUPPCState *env, target_ulong addr)
598{
David Gibson5883d8b2013-03-12 00:31:45 +0000599 ppc_slb_t *slb;
600 hwaddr pte_offset;
601 ppc_hash_pte64_t pte;
David Gibsonf2ad6be2013-03-12 00:31:13 +0000602
David Gibson5883d8b2013-03-12 00:31:45 +0000603 if (msr_dr == 0) {
604 /* In real mode the top 4 effective address bits are ignored */
605 return addr & 0x0FFFFFFFFFFFFFFFULL;
606 }
607
608 slb = slb_lookup(env, addr);
609 if (!slb) {
David Gibsonf2ad6be2013-03-12 00:31:13 +0000610 return -1;
611 }
612
David Gibson5883d8b2013-03-12 00:31:45 +0000613 pte_offset = ppc_hash64_htab_lookup(env, slb, addr, &pte);
614 if (pte_offset == -1) {
615 return -1;
616 }
617
618 return ppc_hash64_pte_raddr(slb, pte, addr) & TARGET_PAGE_MASK;
David Gibsonf2ad6be2013-03-12 00:31:13 +0000619}
Aneesh Kumar K.Vc1385932014-02-20 18:52:38 +0100620
621void ppc_hash64_store_hpte(CPUPPCState *env,
622 target_ulong pte_index,
623 target_ulong pte0, target_ulong pte1)
624{
Andreas Färber33276f12014-03-09 19:29:41 +0100625 CPUState *cs = CPU(ppc_env_get_cpu(env));
Aneesh Kumar K.Vc1385932014-02-20 18:52:38 +0100626
627 if (kvmppc_kern_htab) {
Stefan Weila9ab06d2015-03-07 23:16:38 +0100628 kvmppc_hash64_write_pte(env, pte_index, pte0, pte1);
629 return;
Aneesh Kumar K.Vc1385932014-02-20 18:52:38 +0100630 }
631
632 pte_index *= HASH_PTE_SIZE_64;
633 if (env->external_htab) {
634 stq_p(env->external_htab + pte_index, pte0);
635 stq_p(env->external_htab + pte_index + HASH_PTE_SIZE_64/2, pte1);
636 } else {
637 stq_phys(cs->as, env->htab_base + pte_index, pte0);
638 stq_phys(cs->as, env->htab_base + pte_index + HASH_PTE_SIZE_64/2, pte1);
639 }
640}