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