2 * PowerPC MMU, TLB and BAT emulation helpers for QEMU.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
5 * Copyright (c) 2013 David Gibson, IBM Corporation
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.1 of the License, or (at your option) any later version.
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.
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/>.
21 #include "qemu/osdep.h"
23 #include "exec/exec-all.h"
24 #include "sysemu/kvm.h"
27 #include "mmu-hash32.h"
28 #include "mmu-books.h"
31 /* #define DEBUG_BATS */
34 # define LOG_BATS(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
36 # define LOG_BATS(...) do { } while (0)
39 struct mmu_ctx_hash32
{
40 hwaddr raddr
; /* Real address */
41 int prot
; /* Protection bits */
42 int key
; /* Access key */
45 static int ppc_hash32_pp_prot(int key
, int pp
, int nx
)
54 prot
= PAGE_READ
| PAGE_WRITE
;
76 prot
= PAGE_READ
| PAGE_WRITE
;
90 static int ppc_hash32_pte_prot(int mmu_idx
,
91 target_ulong sr
, ppc_hash_pte32_t pte
)
95 key
= !!(mmuidx_pr(mmu_idx
) ? (sr
& SR32_KP
) : (sr
& SR32_KS
));
96 pp
= pte
.pte1
& HPTE32_R_PP
;
98 return ppc_hash32_pp_prot(key
, pp
, !!(sr
& SR32_NX
));
101 static target_ulong
hash32_bat_size(int mmu_idx
,
102 target_ulong batu
, target_ulong batl
)
104 if ((mmuidx_pr(mmu_idx
) && !(batu
& BATU32_VP
))
105 || (!mmuidx_pr(mmu_idx
) && !(batu
& BATU32_VS
))) {
109 return BATU32_BEPI
& ~((batu
& BATU32_BL
) << 15);
112 static int hash32_bat_prot(PowerPCCPU
*cpu
,
113 target_ulong batu
, target_ulong batl
)
118 pp
= batl
& BATL32_PP
;
120 prot
= PAGE_READ
| PAGE_EXEC
;
128 static target_ulong
hash32_bat_601_size(PowerPCCPU
*cpu
,
129 target_ulong batu
, target_ulong batl
)
131 if (!(batl
& BATL32_601_V
)) {
135 return BATU32_BEPI
& ~((batl
& BATL32_601_BL
) << 17);
138 static int hash32_bat_601_prot(int mmu_idx
,
139 target_ulong batu
, target_ulong batl
)
143 pp
= batu
& BATU32_601_PP
;
144 if (mmuidx_pr(mmu_idx
) == 0) {
145 key
= !!(batu
& BATU32_601_KS
);
147 key
= !!(batu
& BATU32_601_KP
);
149 return ppc_hash32_pp_prot(key
, pp
, 0);
152 static hwaddr
ppc_hash32_bat_lookup(PowerPCCPU
*cpu
, target_ulong ea
,
153 MMUAccessType access_type
, int *prot
,
156 CPUPPCState
*env
= &cpu
->env
;
157 target_ulong
*BATlt
, *BATut
;
158 bool ifetch
= access_type
== MMU_INST_FETCH
;
161 LOG_BATS("%s: %cBAT v " TARGET_FMT_lx
"\n", __func__
,
162 ifetch
? 'I' : 'D', ea
);
164 BATlt
= env
->IBAT
[1];
165 BATut
= env
->IBAT
[0];
167 BATlt
= env
->DBAT
[1];
168 BATut
= env
->DBAT
[0];
170 for (i
= 0; i
< env
->nb_BATs
; i
++) {
171 target_ulong batu
= BATut
[i
];
172 target_ulong batl
= BATlt
[i
];
175 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
176 mask
= hash32_bat_601_size(cpu
, batu
, batl
);
178 mask
= hash32_bat_size(mmu_idx
, batu
, batl
);
180 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
181 " BATl " TARGET_FMT_lx
"\n", __func__
,
182 ifetch
? 'I' : 'D', i
, ea
, batu
, batl
);
184 if (mask
&& ((ea
& mask
) == (batu
& BATU32_BEPI
))) {
185 hwaddr raddr
= (batl
& mask
) | (ea
& ~mask
);
187 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
188 *prot
= hash32_bat_601_prot(mmu_idx
, batu
, batl
);
190 *prot
= hash32_bat_prot(cpu
, batu
, batl
);
193 return raddr
& TARGET_PAGE_MASK
;
198 #if defined(DEBUG_BATS)
199 if (qemu_log_enabled()) {
200 target_ulong
*BATu
, *BATl
;
201 target_ulong BEPIl
, BEPIu
, bl
;
203 LOG_BATS("no BAT match for " TARGET_FMT_lx
":\n", ea
);
204 for (i
= 0; i
< 4; i
++) {
207 BEPIu
= *BATu
& BATU32_BEPIU
;
208 BEPIl
= *BATu
& BATU32_BEPIL
;
209 bl
= (*BATu
& 0x00001FFC) << 15;
210 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
211 " BATl " TARGET_FMT_lx
"\n\t" TARGET_FMT_lx
" "
212 TARGET_FMT_lx
" " TARGET_FMT_lx
"\n",
213 __func__
, ifetch
? 'I' : 'D', i
, ea
,
214 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
222 static bool ppc_hash32_direct_store(PowerPCCPU
*cpu
, target_ulong sr
,
224 MMUAccessType access_type
,
225 hwaddr
*raddr
, int *prot
, int mmu_idx
,
228 CPUState
*cs
= CPU(cpu
);
229 CPUPPCState
*env
= &cpu
->env
;
230 int key
= !!(mmuidx_pr(mmu_idx
) ? (sr
& SR32_KP
) : (sr
& SR32_KS
));
232 qemu_log_mask(CPU_LOG_MMU
, "direct store...\n");
234 if ((sr
& 0x1FF00000) >> 20 == 0x07f) {
236 * Memory-forced I/O controller interface access
238 * If T=1 and BUID=x'07F', the 601 performs a memory access
239 * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
241 *raddr
= ((sr
& 0xF) << 28) | (eaddr
& 0x0FFFFFFF);
242 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
246 if (access_type
== MMU_INST_FETCH
) {
247 /* No code fetch is allowed in direct-store areas */
249 cs
->exception_index
= POWERPC_EXCP_ISI
;
250 env
->error_code
= 0x10000000;
256 * From ppc_cpu_get_phys_page_debug, env->access_type is not set.
257 * Assume ACCESS_INT for that case.
259 switch (guest_visible
? env
->access_type
: ACCESS_INT
) {
261 /* Integer load/store : only access allowed */
264 /* Floating point load/store */
265 cs
->exception_index
= POWERPC_EXCP_ALIGN
;
266 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
267 env
->spr
[SPR_DAR
] = eaddr
;
270 /* lwarx, ldarx or srwcx. */
272 env
->spr
[SPR_DAR
] = eaddr
;
273 if (access_type
== MMU_DATA_STORE
) {
274 env
->spr
[SPR_DSISR
] = 0x06000000;
276 env
->spr
[SPR_DSISR
] = 0x04000000;
281 * dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi
283 * Should make the instruction do no-op. As it already do
284 * no-op, it's quite easy :-)
290 cs
->exception_index
= POWERPC_EXCP_DSI
;
292 env
->spr
[SPR_DAR
] = eaddr
;
293 if (access_type
== MMU_DATA_STORE
) {
294 env
->spr
[SPR_DSISR
] = 0x06100000;
296 env
->spr
[SPR_DSISR
] = 0x04100000;
300 cpu_abort(cs
, "ERROR: insn should not need address translation\n");
303 *prot
= key
? PAGE_READ
| PAGE_WRITE
: PAGE_READ
;
304 if (*prot
& prot_for_access_type(access_type
)) {
310 cs
->exception_index
= POWERPC_EXCP_DSI
;
312 env
->spr
[SPR_DAR
] = eaddr
;
313 if (access_type
== MMU_DATA_STORE
) {
314 env
->spr
[SPR_DSISR
] = 0x0a000000;
316 env
->spr
[SPR_DSISR
] = 0x08000000;
322 hwaddr
get_pteg_offset32(PowerPCCPU
*cpu
, hwaddr hash
)
324 target_ulong mask
= ppc_hash32_hpt_mask(cpu
);
326 return (hash
* HASH_PTEG_SIZE_32
) & mask
;
329 static hwaddr
ppc_hash32_pteg_search(PowerPCCPU
*cpu
, hwaddr pteg_off
,
330 bool secondary
, target_ulong ptem
,
331 ppc_hash_pte32_t
*pte
)
333 hwaddr pte_offset
= pteg_off
;
334 target_ulong pte0
, pte1
;
337 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
338 pte0
= ppc_hash32_load_hpte0(cpu
, pte_offset
);
340 * pte0 contains the valid bit and must be read before pte1,
341 * otherwise we might see an old pte1 with a new valid bit and
342 * thus an inconsistent hpte value
345 pte1
= ppc_hash32_load_hpte1(cpu
, pte_offset
);
347 if ((pte0
& HPTE32_V_VALID
)
348 && (secondary
== !!(pte0
& HPTE32_V_SECONDARY
))
349 && HPTE32_V_COMPARE(pte0
, ptem
)) {
355 pte_offset
+= HASH_PTE_SIZE_32
;
361 static void ppc_hash32_set_r(PowerPCCPU
*cpu
, hwaddr pte_offset
, uint32_t pte1
)
363 target_ulong base
= ppc_hash32_hpt_base(cpu
);
364 hwaddr offset
= pte_offset
+ 6;
366 /* The HW performs a non-atomic byte update */
367 stb_phys(CPU(cpu
)->as
, base
+ offset
, ((pte1
>> 8) & 0xff) | 0x01);
370 static void ppc_hash32_set_c(PowerPCCPU
*cpu
, hwaddr pte_offset
, uint64_t pte1
)
372 target_ulong base
= ppc_hash32_hpt_base(cpu
);
373 hwaddr offset
= pte_offset
+ 7;
375 /* The HW performs a non-atomic byte update */
376 stb_phys(CPU(cpu
)->as
, base
+ offset
, (pte1
& 0xff) | 0x80);
379 static hwaddr
ppc_hash32_htab_lookup(PowerPCCPU
*cpu
,
380 target_ulong sr
, target_ulong eaddr
,
381 ppc_hash_pte32_t
*pte
)
383 hwaddr pteg_off
, pte_offset
;
385 uint32_t vsid
, pgidx
, ptem
;
387 vsid
= sr
& SR32_VSID
;
388 pgidx
= (eaddr
& ~SEGMENT_MASK_256M
) >> TARGET_PAGE_BITS
;
390 ptem
= (vsid
<< 7) | (pgidx
>> 10);
392 /* Page address translation */
393 qemu_log_mask(CPU_LOG_MMU
, "htab_base " TARGET_FMT_plx
394 " htab_mask " TARGET_FMT_plx
395 " hash " TARGET_FMT_plx
"\n",
396 ppc_hash32_hpt_base(cpu
), ppc_hash32_hpt_mask(cpu
), hash
);
398 /* Primary PTEG lookup */
399 qemu_log_mask(CPU_LOG_MMU
, "0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
400 " vsid=%" PRIx32
" ptem=%" PRIx32
401 " hash=" TARGET_FMT_plx
"\n",
402 ppc_hash32_hpt_base(cpu
), ppc_hash32_hpt_mask(cpu
),
404 pteg_off
= get_pteg_offset32(cpu
, hash
);
405 pte_offset
= ppc_hash32_pteg_search(cpu
, pteg_off
, 0, ptem
, pte
);
406 if (pte_offset
== -1) {
407 /* Secondary PTEG lookup */
408 qemu_log_mask(CPU_LOG_MMU
, "1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
409 " vsid=%" PRIx32
" api=%" PRIx32
410 " hash=" TARGET_FMT_plx
"\n", ppc_hash32_hpt_base(cpu
),
411 ppc_hash32_hpt_mask(cpu
), vsid
, ptem
, ~hash
);
412 pteg_off
= get_pteg_offset32(cpu
, ~hash
);
413 pte_offset
= ppc_hash32_pteg_search(cpu
, pteg_off
, 1, ptem
, pte
);
419 static hwaddr
ppc_hash32_pte_raddr(target_ulong sr
, ppc_hash_pte32_t pte
,
422 hwaddr rpn
= pte
.pte1
& HPTE32_R_RPN
;
423 hwaddr mask
= ~TARGET_PAGE_MASK
;
425 return (rpn
& ~mask
) | (eaddr
& mask
);
428 bool ppc_hash32_xlate(PowerPCCPU
*cpu
, vaddr eaddr
, MMUAccessType access_type
,
429 hwaddr
*raddrp
, int *psizep
, int *protp
, int mmu_idx
,
432 CPUState
*cs
= CPU(cpu
);
433 CPUPPCState
*env
= &cpu
->env
;
436 ppc_hash_pte32_t pte
;
441 /* There are no hash32 large pages. */
442 *psizep
= TARGET_PAGE_BITS
;
444 /* 1. Handle real mode accesses */
445 if (mmuidx_real(mmu_idx
)) {
446 /* Translation is off */
448 *protp
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
452 need_prot
= prot_for_access_type(access_type
);
454 /* 2. Check Block Address Translation entries (BATs) */
455 if (env
->nb_BATs
!= 0) {
456 raddr
= ppc_hash32_bat_lookup(cpu
, eaddr
, access_type
, protp
, mmu_idx
);
458 if (need_prot
& ~*protp
) {
460 if (access_type
== MMU_INST_FETCH
) {
461 cs
->exception_index
= POWERPC_EXCP_ISI
;
462 env
->error_code
= 0x08000000;
464 cs
->exception_index
= POWERPC_EXCP_DSI
;
466 env
->spr
[SPR_DAR
] = eaddr
;
467 if (access_type
== MMU_DATA_STORE
) {
468 env
->spr
[SPR_DSISR
] = 0x0a000000;
470 env
->spr
[SPR_DSISR
] = 0x08000000;
481 /* 3. Look up the Segment Register */
482 sr
= env
->sr
[eaddr
>> 28];
484 /* 4. Handle direct store segments */
486 return ppc_hash32_direct_store(cpu
, sr
, eaddr
, access_type
,
487 raddrp
, protp
, mmu_idx
, guest_visible
);
490 /* 5. Check for segment level no-execute violation */
491 if (access_type
== MMU_INST_FETCH
&& (sr
& SR32_NX
)) {
493 cs
->exception_index
= POWERPC_EXCP_ISI
;
494 env
->error_code
= 0x10000000;
499 /* 6. Locate the PTE in the hash table */
500 pte_offset
= ppc_hash32_htab_lookup(cpu
, sr
, eaddr
, &pte
);
501 if (pte_offset
== -1) {
503 if (access_type
== MMU_INST_FETCH
) {
504 cs
->exception_index
= POWERPC_EXCP_ISI
;
505 env
->error_code
= 0x40000000;
507 cs
->exception_index
= POWERPC_EXCP_DSI
;
509 env
->spr
[SPR_DAR
] = eaddr
;
510 if (access_type
== MMU_DATA_STORE
) {
511 env
->spr
[SPR_DSISR
] = 0x42000000;
513 env
->spr
[SPR_DSISR
] = 0x40000000;
519 qemu_log_mask(CPU_LOG_MMU
,
520 "found PTE at offset %08" HWADDR_PRIx
"\n", pte_offset
);
522 /* 7. Check access permissions */
524 prot
= ppc_hash32_pte_prot(mmu_idx
, sr
, pte
);
526 if (need_prot
& ~prot
) {
527 /* Access right violation */
528 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
530 if (access_type
== MMU_INST_FETCH
) {
531 cs
->exception_index
= POWERPC_EXCP_ISI
;
532 env
->error_code
= 0x08000000;
534 cs
->exception_index
= POWERPC_EXCP_DSI
;
536 env
->spr
[SPR_DAR
] = eaddr
;
537 if (access_type
== MMU_DATA_STORE
) {
538 env
->spr
[SPR_DSISR
] = 0x0a000000;
540 env
->spr
[SPR_DSISR
] = 0x08000000;
547 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
549 /* 8. Update PTE referenced and changed bits if necessary */
551 if (!(pte
.pte1
& HPTE32_R_R
)) {
552 ppc_hash32_set_r(cpu
, pte_offset
, pte
.pte1
);
554 if (!(pte
.pte1
& HPTE32_R_C
)) {
555 if (access_type
== MMU_DATA_STORE
) {
556 ppc_hash32_set_c(cpu
, pte_offset
, pte
.pte1
);
559 * Treat the page as read-only for now, so that a later write
560 * will pass through this function again to set the C bit
566 /* 9. Determine the real address from the PTE */
568 *raddrp
= ppc_hash32_pte_raddr(sr
, pte
, eaddr
);