2 * PowerPC Radix MMU mulation helpers for QEMU.
4 * Copyright (c) 2016 Suraj Jitindar Singh, IBM Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "exec/exec-all.h"
23 #include "qemu/error-report.h"
24 #include "sysemu/kvm.h"
28 #include "mmu-radix64.h"
29 #include "mmu-book3s-v3.h"
31 static bool ppc_radix64_get_fully_qualified_addr(const CPUPPCState
*env
,
33 uint64_t *lpid
, uint64_t *pid
)
35 /* When EA(2:11) are nonzero, raise a segment interrupt */
36 if (eaddr
& ~R_EADDR_VALID_MASK
) {
40 if (msr_hv
) { /* MSR[HV] -> Hypervisor/bare metal */
41 switch (eaddr
& R_EADDR_QUADRANT
) {
42 case R_EADDR_QUADRANT0
:
44 *pid
= env
->spr
[SPR_BOOKS_PID
];
46 case R_EADDR_QUADRANT1
:
47 *lpid
= env
->spr
[SPR_LPIDR
];
48 *pid
= env
->spr
[SPR_BOOKS_PID
];
50 case R_EADDR_QUADRANT2
:
51 *lpid
= env
->spr
[SPR_LPIDR
];
54 case R_EADDR_QUADRANT3
:
59 g_assert_not_reached();
61 } else { /* !MSR[HV] -> Guest */
62 switch (eaddr
& R_EADDR_QUADRANT
) {
63 case R_EADDR_QUADRANT0
: /* Guest application */
64 *lpid
= env
->spr
[SPR_LPIDR
];
65 *pid
= env
->spr
[SPR_BOOKS_PID
];
67 case R_EADDR_QUADRANT1
: /* Illegal */
68 case R_EADDR_QUADRANT2
:
70 case R_EADDR_QUADRANT3
: /* Guest OS */
71 *lpid
= env
->spr
[SPR_LPIDR
];
72 *pid
= 0; /* pid set to 0 -> addresses guest operating system */
75 g_assert_not_reached();
82 static void ppc_radix64_raise_segi(PowerPCCPU
*cpu
, MMUAccessType access_type
,
85 CPUState
*cs
= CPU(cpu
);
86 CPUPPCState
*env
= &cpu
->env
;
88 switch (access_type
) {
90 /* Instruction Segment Interrupt */
91 cs
->exception_index
= POWERPC_EXCP_ISEG
;
95 /* Data Segment Interrupt */
96 cs
->exception_index
= POWERPC_EXCP_DSEG
;
97 env
->spr
[SPR_DAR
] = eaddr
;
100 g_assert_not_reached();
105 static inline const char *access_str(MMUAccessType access_type
)
107 return access_type
== MMU_DATA_LOAD
? "reading" :
108 (access_type
== MMU_DATA_STORE
? "writing" : "execute");
111 static void ppc_radix64_raise_si(PowerPCCPU
*cpu
, MMUAccessType access_type
,
112 vaddr eaddr
, uint32_t cause
)
114 CPUState
*cs
= CPU(cpu
);
115 CPUPPCState
*env
= &cpu
->env
;
117 qemu_log_mask(CPU_LOG_MMU
, "%s for %s @0x%"VADDR_PRIx
" cause %08x\n",
118 __func__
, access_str(access_type
),
121 switch (access_type
) {
123 /* Instruction Storage Interrupt */
124 cs
->exception_index
= POWERPC_EXCP_ISI
;
125 env
->error_code
= cause
;
128 cause
|= DSISR_ISSTORE
;
131 /* Data Storage Interrupt */
132 cs
->exception_index
= POWERPC_EXCP_DSI
;
133 env
->spr
[SPR_DSISR
] = cause
;
134 env
->spr
[SPR_DAR
] = eaddr
;
138 g_assert_not_reached();
142 static void ppc_radix64_raise_hsi(PowerPCCPU
*cpu
, MMUAccessType access_type
,
143 vaddr eaddr
, hwaddr g_raddr
, uint32_t cause
)
145 CPUState
*cs
= CPU(cpu
);
146 CPUPPCState
*env
= &cpu
->env
;
148 qemu_log_mask(CPU_LOG_MMU
, "%s for %s @0x%"VADDR_PRIx
" 0x%"
149 HWADDR_PRIx
" cause %08x\n",
150 __func__
, access_str(access_type
),
151 eaddr
, g_raddr
, cause
);
153 switch (access_type
) {
155 /* H Instruction Storage Interrupt */
156 cs
->exception_index
= POWERPC_EXCP_HISI
;
157 env
->spr
[SPR_ASDR
] = g_raddr
;
158 env
->error_code
= cause
;
161 cause
|= DSISR_ISSTORE
;
164 /* H Data Storage Interrupt */
165 cs
->exception_index
= POWERPC_EXCP_HDSI
;
166 env
->spr
[SPR_HDSISR
] = cause
;
167 env
->spr
[SPR_HDAR
] = eaddr
;
168 env
->spr
[SPR_ASDR
] = g_raddr
;
172 g_assert_not_reached();
176 static bool ppc_radix64_check_prot(PowerPCCPU
*cpu
, MMUAccessType access_type
,
177 uint64_t pte
, int *fault_cause
, int *prot
,
178 int mmu_idx
, bool partition_scoped
)
180 CPUPPCState
*env
= &cpu
->env
;
183 /* Check Page Attributes (pte58:59) */
184 if ((pte
& R_PTE_ATT
) == R_PTE_ATT_NI_IO
&& access_type
== MMU_INST_FETCH
) {
186 * Radix PTE entries with the non-idempotent I/O attribute are treated
189 *fault_cause
|= SRR1_NOEXEC_GUARD
;
193 /* Determine permissions allowed by Encoded Access Authority */
194 if (!partition_scoped
&& (pte
& R_PTE_EAA_PRIV
) && msr_pr
) {
196 } else if (mmuidx_pr(mmu_idx
) || (pte
& R_PTE_EAA_PRIV
) ||
198 *prot
= ppc_radix64_get_prot_eaa(pte
);
199 } else { /* !msr_pr && !(pte & R_PTE_EAA_PRIV) && !partition_scoped */
200 *prot
= ppc_radix64_get_prot_eaa(pte
);
201 *prot
&= ppc_radix64_get_prot_amr(cpu
); /* Least combined permissions */
204 /* Check if requested access type is allowed */
205 need_prot
= prot_for_access_type(access_type
);
206 if (need_prot
& ~*prot
) { /* Page Protected for that Access */
207 *fault_cause
|= DSISR_PROTFAULT
;
214 static void ppc_radix64_set_rc(PowerPCCPU
*cpu
, MMUAccessType access_type
,
215 uint64_t pte
, hwaddr pte_addr
, int *prot
)
217 CPUState
*cs
= CPU(cpu
);
220 npte
= pte
| R_PTE_R
; /* Always set reference bit */
222 if (access_type
== MMU_DATA_STORE
) { /* Store/Write */
223 npte
|= R_PTE_C
; /* Set change bit */
226 * Treat the page as read-only for now, so that a later write
227 * will pass through this function again to set the C bit.
229 *prot
&= ~PAGE_WRITE
;
232 if (pte
^ npte
) { /* If pte has changed then write it back */
233 stq_phys(cs
->as
, pte_addr
, npte
);
237 static int ppc_radix64_next_level(AddressSpace
*as
, vaddr eaddr
,
238 uint64_t *pte_addr
, uint64_t *nls
,
239 int *psize
, uint64_t *pte
, int *fault_cause
)
243 if (*nls
< 5) { /* Directory maps less than 2**5 entries */
244 *fault_cause
|= DSISR_R_BADCONFIG
;
248 /* Read page <directory/table> entry from guest address space */
249 pde
= ldq_phys(as
, *pte_addr
);
250 if (!(pde
& R_PTE_VALID
)) { /* Invalid Entry */
251 *fault_cause
|= DSISR_NOPTE
;
257 if (!(pde
& R_PTE_LEAF
)) { /* Prepare for next iteration */
258 *nls
= pde
& R_PDE_NLS
;
259 index
= eaddr
>> (*psize
- *nls
); /* Shift */
260 index
&= ((1UL << *nls
) - 1); /* Mask */
261 *pte_addr
= (pde
& R_PDE_NLB
) + (index
* sizeof(pde
));
266 static int ppc_radix64_walk_tree(AddressSpace
*as
, vaddr eaddr
,
267 uint64_t base_addr
, uint64_t nls
,
268 hwaddr
*raddr
, int *psize
, uint64_t *pte
,
269 int *fault_cause
, hwaddr
*pte_addr
)
271 uint64_t index
, pde
, rpn
, mask
;
273 if (nls
< 5) { /* Directory maps less than 2**5 entries */
274 *fault_cause
|= DSISR_R_BADCONFIG
;
278 index
= eaddr
>> (*psize
- nls
); /* Shift */
279 index
&= ((1UL << nls
) - 1); /* Mask */
280 *pte_addr
= base_addr
+ (index
* sizeof(pde
));
284 ret
= ppc_radix64_next_level(as
, eaddr
, pte_addr
, &nls
, psize
, &pde
,
289 } while (!(pde
& R_PTE_LEAF
));
292 rpn
= pde
& R_PTE_RPN
;
293 mask
= (1UL << *psize
) - 1;
295 /* Or high bits of rpn and low bits to ea to form whole real addr */
296 *raddr
= (rpn
& ~mask
) | (eaddr
& mask
);
300 static bool validate_pate(PowerPCCPU
*cpu
, uint64_t lpid
, ppc_v3_pate_t
*pate
)
302 CPUPPCState
*env
= &cpu
->env
;
304 if (!(pate
->dw0
& PATE0_HR
)) {
307 if (lpid
== 0 && !msr_hv
) {
310 if ((pate
->dw0
& PATE1_R_PRTS
) < 5) {
313 /* More checks ... */
317 static int ppc_radix64_partition_scoped_xlate(PowerPCCPU
*cpu
,
318 MMUAccessType access_type
,
319 vaddr eaddr
, hwaddr g_raddr
,
321 hwaddr
*h_raddr
, int *h_prot
,
322 int *h_page_size
, bool pde_addr
,
323 int mmu_idx
, bool guest_visible
)
329 qemu_log_mask(CPU_LOG_MMU
, "%s for %s @0x%"VADDR_PRIx
330 " mmu_idx %u (prot %c%c%c) 0x%"HWADDR_PRIx
"\n",
331 __func__
, access_str(access_type
),
333 *h_prot
& PAGE_READ
? 'r' : '-',
334 *h_prot
& PAGE_WRITE
? 'w' : '-',
335 *h_prot
& PAGE_EXEC
? 'x' : '-',
338 *h_page_size
= PRTBE_R_GET_RTS(pate
.dw0
);
339 /* No valid pte or access denied due to protection */
340 if (ppc_radix64_walk_tree(CPU(cpu
)->as
, g_raddr
, pate
.dw0
& PRTBE_R_RPDB
,
341 pate
.dw0
& PRTBE_R_RPDS
, h_raddr
, h_page_size
,
342 &pte
, &fault_cause
, &pte_addr
) ||
343 ppc_radix64_check_prot(cpu
, access_type
, pte
,
344 &fault_cause
, h_prot
, mmu_idx
, true)) {
345 if (pde_addr
) { /* address being translated was that of a guest pde */
346 fault_cause
|= DSISR_PRTABLE_FAULT
;
349 ppc_radix64_raise_hsi(cpu
, access_type
, eaddr
, g_raddr
, fault_cause
);
355 ppc_radix64_set_rc(cpu
, access_type
, pte
, pte_addr
, h_prot
);
361 static int ppc_radix64_process_scoped_xlate(PowerPCCPU
*cpu
,
362 MMUAccessType access_type
,
363 vaddr eaddr
, uint64_t pid
,
364 ppc_v3_pate_t pate
, hwaddr
*g_raddr
,
365 int *g_prot
, int *g_page_size
,
366 int mmu_idx
, bool guest_visible
)
368 CPUState
*cs
= CPU(cpu
);
369 CPUPPCState
*env
= &cpu
->env
;
370 uint64_t offset
, size
, prtbe_addr
, prtbe0
, base_addr
, nls
, index
, pte
;
371 int fault_cause
= 0, h_page_size
, h_prot
;
372 hwaddr h_raddr
, pte_addr
;
375 qemu_log_mask(CPU_LOG_MMU
, "%s for %s @0x%"VADDR_PRIx
376 " mmu_idx %u pid %"PRIu64
"\n",
377 __func__
, access_str(access_type
),
378 eaddr
, mmu_idx
, pid
);
380 /* Index Process Table by PID to Find Corresponding Process Table Entry */
381 offset
= pid
* sizeof(struct prtb_entry
);
382 size
= 1ULL << ((pate
.dw1
& PATE1_R_PRTS
) + 12);
383 if (offset
>= size
) {
384 /* offset exceeds size of the process table */
386 ppc_radix64_raise_si(cpu
, access_type
, eaddr
, DSISR_NOPTE
);
390 prtbe_addr
= (pate
.dw1
& PATE1_R_PRTB
) + offset
;
393 prtbe0
= ldq_phys(cs
->as
, prtbe_addr
);
396 * Process table addresses are subject to partition-scoped
399 * On a Radix host, the partition-scoped page table for LPID=0
400 * is only used to translate the effective addresses of the
401 * process table entries.
403 ret
= ppc_radix64_partition_scoped_xlate(cpu
, 0, eaddr
, prtbe_addr
,
404 pate
, &h_raddr
, &h_prot
,
406 /* mmu_idx is 5 because we're translating from hypervisor scope */
411 prtbe0
= ldq_phys(cs
->as
, h_raddr
);
414 /* Walk Radix Tree from Process Table Entry to Convert EA to RA */
415 *g_page_size
= PRTBE_R_GET_RTS(prtbe0
);
416 base_addr
= prtbe0
& PRTBE_R_RPDB
;
417 nls
= prtbe0
& PRTBE_R_RPDS
;
418 if (msr_hv
|| cpu
->vhyp
) {
420 * Can treat process table addresses as real addresses
422 ret
= ppc_radix64_walk_tree(cs
->as
, eaddr
& R_EADDR_MASK
, base_addr
,
423 nls
, g_raddr
, g_page_size
, &pte
,
424 &fault_cause
, &pte_addr
);
428 ppc_radix64_raise_si(cpu
, access_type
, eaddr
, fault_cause
);
435 index
= (eaddr
& R_EADDR_MASK
) >> (*g_page_size
- nls
); /* Shift */
436 index
&= ((1UL << nls
) - 1); /* Mask */
437 pte_addr
= base_addr
+ (index
* sizeof(pte
));
440 * Each process table address is subject to a partition-scoped
444 ret
= ppc_radix64_partition_scoped_xlate(cpu
, 0, eaddr
, pte_addr
,
445 pate
, &h_raddr
, &h_prot
,
447 /* mmu_idx is 5 because we're translating from hypervisor scope */
453 ret
= ppc_radix64_next_level(cs
->as
, eaddr
& R_EADDR_MASK
, &h_raddr
,
454 &nls
, g_page_size
, &pte
, &fault_cause
);
458 ppc_radix64_raise_si(cpu
, access_type
, eaddr
, fault_cause
);
463 } while (!(pte
& R_PTE_LEAF
));
465 rpn
= pte
& R_PTE_RPN
;
466 mask
= (1UL << *g_page_size
) - 1;
468 /* Or high bits of rpn and low bits to ea to form whole real addr */
469 *g_raddr
= (rpn
& ~mask
) | (eaddr
& mask
);
472 if (ppc_radix64_check_prot(cpu
, access_type
, pte
, &fault_cause
,
473 g_prot
, mmu_idx
, false)) {
474 /* Access denied due to protection */
476 ppc_radix64_raise_si(cpu
, access_type
, eaddr
, fault_cause
);
482 ppc_radix64_set_rc(cpu
, access_type
, pte
, pte_addr
, g_prot
);
489 * Radix tree translation is a 2 steps translation process:
491 * 1. Process-scoped translation: Guest Eff Addr -> Guest Real Addr
492 * 2. Partition-scoped translation: Guest Real Addr -> Host Real Addr
495 * +-------------+----------------+---------------+
496 * | | HV = 0 | HV = 1 |
497 * +-------------+----------------+---------------+
498 * | Relocation | Partition | No |
499 * | = Off | Scoped | Translation |
500 * Relocation +-------------+----------------+---------------+
501 * | Relocation | Partition & | Process |
502 * | = On | Process Scoped | Scoped |
503 * +-------------+----------------+---------------+
505 static bool ppc_radix64_xlate_impl(PowerPCCPU
*cpu
, vaddr eaddr
,
506 MMUAccessType access_type
, hwaddr
*raddr
,
507 int *psizep
, int *protp
, int mmu_idx
,
510 CPUPPCState
*env
= &cpu
->env
;
517 assert(!(mmuidx_hv(mmu_idx
) && cpu
->vhyp
));
519 relocation
= !mmuidx_real(mmu_idx
);
521 /* HV or virtual hypervisor Real Mode Access */
522 if (!relocation
&& (mmuidx_hv(mmu_idx
) || cpu
->vhyp
)) {
523 /* In real mode top 4 effective addr bits (mostly) ignored */
524 *raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
526 /* In HV mode, add HRMOR if top EA bit is clear */
527 if (mmuidx_hv(mmu_idx
) || !env
->has_hv_mode
) {
528 if (!(eaddr
>> 63)) {
529 *raddr
|= env
->spr
[SPR_HRMOR
];
532 *protp
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
533 *psizep
= TARGET_PAGE_BITS
;
538 * Check UPRT (we avoid the check in real mode to deal with
539 * transitional states during kexec.
541 if (guest_visible
&& !ppc64_use_proc_tbl(cpu
)) {
542 qemu_log_mask(LOG_GUEST_ERROR
,
543 "LPCR:UPRT not set in radix mode ! LPCR="
544 TARGET_FMT_lx
"\n", env
->spr
[SPR_LPCR
]);
547 /* Virtual Mode Access - get the fully qualified address */
548 if (!ppc_radix64_get_fully_qualified_addr(&cpu
->env
, eaddr
, &lpid
, &pid
)) {
550 ppc_radix64_raise_segi(cpu
, access_type
, eaddr
);
555 /* Get Process Table */
557 PPCVirtualHypervisorClass
*vhc
;
558 vhc
= PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
559 vhc
->get_pate(cpu
->vhyp
, &pate
);
561 if (!ppc64_v3_get_pate(cpu
, lpid
, &pate
)) {
563 ppc_radix64_raise_si(cpu
, access_type
, eaddr
, DSISR_NOPTE
);
567 if (!validate_pate(cpu
, lpid
, &pate
)) {
569 ppc_radix64_raise_si(cpu
, access_type
, eaddr
, DSISR_R_BADCONFIG
);
576 *protp
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
579 * Perform process-scoped translation if relocation enabled.
581 * - Translates an effective address to a host real address in
582 * quadrants 0 and 3 when HV=1.
584 * - Translates an effective address to a guest real address.
587 int ret
= ppc_radix64_process_scoped_xlate(cpu
, access_type
, eaddr
, pid
,
588 pate
, &g_raddr
, &prot
,
589 &psize
, mmu_idx
, guest_visible
);
593 *psizep
= MIN(*psizep
, psize
);
596 g_raddr
= eaddr
& R_EADDR_MASK
;
603 * Perform partition-scoped translation if !HV or HV access to
604 * quadrants 1 or 2. Translates a guest real address to a host
607 if (lpid
|| !mmuidx_hv(mmu_idx
)) {
610 ret
= ppc_radix64_partition_scoped_xlate(cpu
, access_type
, eaddr
,
611 g_raddr
, pate
, raddr
,
612 &prot
, &psize
, false,
613 mmu_idx
, guest_visible
);
617 *psizep
= MIN(*psizep
, psize
);
627 bool ppc_radix64_xlate(PowerPCCPU
*cpu
, vaddr eaddr
, MMUAccessType access_type
,
628 hwaddr
*raddrp
, int *psizep
, int *protp
, int mmu_idx
,
631 bool ret
= ppc_radix64_xlate_impl(cpu
, eaddr
, access_type
, raddrp
,
632 psizep
, protp
, mmu_idx
, guest_visible
);
634 qemu_log_mask(CPU_LOG_MMU
, "%s for %s @0x%"VADDR_PRIx
635 " mmu_idx %u (prot %c%c%c) -> 0x%"HWADDR_PRIx
"\n",
636 __func__
, access_str(access_type
),
638 *protp
& PAGE_READ
? 'r' : '-',
639 *protp
& PAGE_WRITE
? 'w' : '-',
640 *protp
& PAGE_EXEC
? 'x' : '-',