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 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/>.
22 #include "exec/helper-proto.h"
23 #include "sysemu/kvm.h"
25 #include "mmu-hash32.h"
30 # define LOG_BATS(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
32 # define LOG_BATS(...) do { } while (0)
35 struct mmu_ctx_hash32
{
36 hwaddr raddr
; /* Real address */
37 int prot
; /* Protection bits */
38 int key
; /* Access key */
41 static int ppc_hash32_pp_prot(int key
, int pp
, int nx
)
50 prot
= PAGE_READ
| PAGE_WRITE
;
72 prot
= PAGE_READ
| PAGE_WRITE
;
86 static int ppc_hash32_pte_prot(CPUPPCState
*env
,
87 target_ulong sr
, ppc_hash_pte32_t pte
)
91 key
= !!(msr_pr
? (sr
& SR32_KP
) : (sr
& SR32_KS
));
92 pp
= pte
.pte1
& HPTE32_R_PP
;
94 return ppc_hash32_pp_prot(key
, pp
, !!(sr
& SR32_NX
));
97 static target_ulong
hash32_bat_size(CPUPPCState
*env
,
98 target_ulong batu
, target_ulong batl
)
100 if ((msr_pr
&& !(batu
& BATU32_VP
))
101 || (!msr_pr
&& !(batu
& BATU32_VS
))) {
105 return BATU32_BEPI
& ~((batu
& BATU32_BL
) << 15);
108 static int hash32_bat_prot(CPUPPCState
*env
,
109 target_ulong batu
, target_ulong batl
)
114 pp
= batl
& BATL32_PP
;
116 prot
= PAGE_READ
| PAGE_EXEC
;
124 static target_ulong
hash32_bat_601_size(CPUPPCState
*env
,
125 target_ulong batu
, target_ulong batl
)
127 if (!(batl
& BATL32_601_V
)) {
131 return BATU32_BEPI
& ~((batl
& BATL32_601_BL
) << 17);
134 static int hash32_bat_601_prot(CPUPPCState
*env
,
135 target_ulong batu
, target_ulong batl
)
139 pp
= batu
& BATU32_601_PP
;
141 key
= !!(batu
& BATU32_601_KS
);
143 key
= !!(batu
& BATU32_601_KP
);
145 return ppc_hash32_pp_prot(key
, pp
, 0);
148 static hwaddr
ppc_hash32_bat_lookup(CPUPPCState
*env
, target_ulong ea
, int rwx
,
151 target_ulong
*BATlt
, *BATut
;
154 LOG_BATS("%s: %cBAT v " TARGET_FMT_lx
"\n", __func__
,
155 rwx
== 2 ? 'I' : 'D', ea
);
157 BATlt
= env
->IBAT
[1];
158 BATut
= env
->IBAT
[0];
160 BATlt
= env
->DBAT
[1];
161 BATut
= env
->DBAT
[0];
163 for (i
= 0; i
< env
->nb_BATs
; i
++) {
164 target_ulong batu
= BATut
[i
];
165 target_ulong batl
= BATlt
[i
];
168 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
169 mask
= hash32_bat_601_size(env
, batu
, batl
);
171 mask
= hash32_bat_size(env
, batu
, batl
);
173 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
174 " BATl " TARGET_FMT_lx
"\n", __func__
,
175 type
== ACCESS_CODE
? 'I' : 'D', i
, ea
, batu
, batl
);
177 if (mask
&& ((ea
& mask
) == (batu
& BATU32_BEPI
))) {
178 hwaddr raddr
= (batl
& mask
) | (ea
& ~mask
);
180 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
181 *prot
= hash32_bat_601_prot(env
, batu
, batl
);
183 *prot
= hash32_bat_prot(env
, batu
, batl
);
186 return raddr
& TARGET_PAGE_MASK
;
191 #if defined(DEBUG_BATS)
192 if (qemu_log_enabled()) {
193 LOG_BATS("no BAT match for " TARGET_FMT_lx
":\n", ea
);
194 for (i
= 0; i
< 4; i
++) {
197 BEPIu
= *BATu
& BATU32_BEPIU
;
198 BEPIl
= *BATu
& BATU32_BEPIL
;
199 bl
= (*BATu
& 0x00001FFC) << 15;
200 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
201 " BATl " TARGET_FMT_lx
"\n\t" TARGET_FMT_lx
" "
202 TARGET_FMT_lx
" " TARGET_FMT_lx
"\n",
203 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, ea
,
204 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
212 static int ppc_hash32_direct_store(CPUPPCState
*env
, target_ulong sr
,
213 target_ulong eaddr
, int rwx
,
214 hwaddr
*raddr
, int *prot
)
216 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
217 int key
= !!(msr_pr
? (sr
& SR32_KP
) : (sr
& SR32_KS
));
219 qemu_log_mask(CPU_LOG_MMU
, "direct store...\n");
221 if ((sr
& 0x1FF00000) >> 20 == 0x07f) {
222 /* Memory-forced I/O controller interface access */
223 /* If T=1 and BUID=x'07F', the 601 performs a memory access
224 * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
226 *raddr
= ((sr
& 0xF) << 28) | (eaddr
& 0x0FFFFFFF);
227 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
232 /* No code fetch is allowed in direct-store areas */
233 cs
->exception_index
= POWERPC_EXCP_ISI
;
234 env
->error_code
= 0x10000000;
238 switch (env
->access_type
) {
240 /* Integer load/store : only access allowed */
243 /* Floating point load/store */
244 cs
->exception_index
= POWERPC_EXCP_ALIGN
;
245 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
246 env
->spr
[SPR_DAR
] = eaddr
;
249 /* lwarx, ldarx or srwcx. */
251 env
->spr
[SPR_DAR
] = eaddr
;
253 env
->spr
[SPR_DSISR
] = 0x06000000;
255 env
->spr
[SPR_DSISR
] = 0x04000000;
259 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
260 /* Should make the instruction do no-op.
261 * As it already do no-op, it's quite easy :-)
267 cs
->exception_index
= POWERPC_EXCP_DSI
;
269 env
->spr
[SPR_DAR
] = eaddr
;
271 env
->spr
[SPR_DSISR
] = 0x06100000;
273 env
->spr
[SPR_DSISR
] = 0x04100000;
277 cpu_abort(cs
, "ERROR: instruction should not need "
278 "address translation\n");
280 if ((rwx
== 1 || key
!= 1) && (rwx
== 0 || key
!= 0)) {
284 cs
->exception_index
= POWERPC_EXCP_DSI
;
286 env
->spr
[SPR_DAR
] = eaddr
;
288 env
->spr
[SPR_DSISR
] = 0x0a000000;
290 env
->spr
[SPR_DSISR
] = 0x08000000;
296 hwaddr
get_pteg_offset32(CPUPPCState
*env
, hwaddr hash
)
298 return (hash
* HASH_PTEG_SIZE_32
) & env
->htab_mask
;
301 static hwaddr
ppc_hash32_pteg_search(CPUPPCState
*env
, hwaddr pteg_off
,
302 bool secondary
, target_ulong ptem
,
303 ppc_hash_pte32_t
*pte
)
305 hwaddr pte_offset
= pteg_off
;
306 target_ulong pte0
, pte1
;
309 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
310 pte0
= ppc_hash32_load_hpte0(env
, pte_offset
);
311 pte1
= ppc_hash32_load_hpte1(env
, pte_offset
);
313 if ((pte0
& HPTE32_V_VALID
)
314 && (secondary
== !!(pte0
& HPTE32_V_SECONDARY
))
315 && HPTE32_V_COMPARE(pte0
, ptem
)) {
321 pte_offset
+= HASH_PTE_SIZE_32
;
327 static hwaddr
ppc_hash32_htab_lookup(CPUPPCState
*env
,
328 target_ulong sr
, target_ulong eaddr
,
329 ppc_hash_pte32_t
*pte
)
331 hwaddr pteg_off
, pte_offset
;
333 uint32_t vsid
, pgidx
, ptem
;
335 vsid
= sr
& SR32_VSID
;
336 pgidx
= (eaddr
& ~SEGMENT_MASK_256M
) >> TARGET_PAGE_BITS
;
338 ptem
= (vsid
<< 7) | (pgidx
>> 10);
340 /* Page address translation */
341 qemu_log_mask(CPU_LOG_MMU
, "htab_base " TARGET_FMT_plx
342 " htab_mask " TARGET_FMT_plx
343 " hash " TARGET_FMT_plx
"\n",
344 env
->htab_base
, env
->htab_mask
, hash
);
346 /* Primary PTEG lookup */
347 qemu_log_mask(CPU_LOG_MMU
, "0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
348 " vsid=%" PRIx32
" ptem=%" PRIx32
349 " hash=" TARGET_FMT_plx
"\n",
350 env
->htab_base
, env
->htab_mask
, vsid
, ptem
, hash
);
351 pteg_off
= get_pteg_offset32(env
, hash
);
352 pte_offset
= ppc_hash32_pteg_search(env
, pteg_off
, 0, ptem
, pte
);
353 if (pte_offset
== -1) {
354 /* Secondary PTEG lookup */
355 qemu_log_mask(CPU_LOG_MMU
, "1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
356 " vsid=%" PRIx32
" api=%" PRIx32
357 " hash=" TARGET_FMT_plx
"\n", env
->htab_base
,
358 env
->htab_mask
, vsid
, ptem
, ~hash
);
359 pteg_off
= get_pteg_offset32(env
, ~hash
);
360 pte_offset
= ppc_hash32_pteg_search(env
, pteg_off
, 1, ptem
, pte
);
366 static hwaddr
ppc_hash32_pte_raddr(target_ulong sr
, ppc_hash_pte32_t pte
,
369 hwaddr rpn
= pte
.pte1
& HPTE32_R_RPN
;
370 hwaddr mask
= ~TARGET_PAGE_MASK
;
372 return (rpn
& ~mask
) | (eaddr
& mask
);
375 int ppc_hash32_handle_mmu_fault(PowerPCCPU
*cpu
, target_ulong eaddr
, int rwx
,
378 CPUState
*cs
= CPU(cpu
);
379 CPUPPCState
*env
= &cpu
->env
;
382 ppc_hash_pte32_t pte
;
385 const int need_prot
[] = {PAGE_READ
, PAGE_WRITE
, PAGE_EXEC
};
388 assert((rwx
== 0) || (rwx
== 1) || (rwx
== 2));
390 /* 1. Handle real mode accesses */
391 if (((rwx
== 2) && (msr_ir
== 0)) || ((rwx
!= 2) && (msr_dr
== 0))) {
392 /* Translation is off */
394 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
395 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
, mmu_idx
,
400 /* 2. Check Block Address Translation entries (BATs) */
401 if (env
->nb_BATs
!= 0) {
402 raddr
= ppc_hash32_bat_lookup(env
, eaddr
, rwx
, &prot
);
404 if (need_prot
[rwx
] & ~prot
) {
406 cs
->exception_index
= POWERPC_EXCP_ISI
;
407 env
->error_code
= 0x08000000;
409 cs
->exception_index
= POWERPC_EXCP_DSI
;
411 env
->spr
[SPR_DAR
] = eaddr
;
413 env
->spr
[SPR_DSISR
] = 0x0a000000;
415 env
->spr
[SPR_DSISR
] = 0x08000000;
421 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
,
422 raddr
& TARGET_PAGE_MASK
, prot
, mmu_idx
,
428 /* 3. Look up the Segment Register */
429 sr
= env
->sr
[eaddr
>> 28];
431 /* 4. Handle direct store segments */
433 if (ppc_hash32_direct_store(env
, sr
, eaddr
, rwx
,
434 &raddr
, &prot
) == 0) {
435 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
,
436 raddr
& TARGET_PAGE_MASK
, prot
, mmu_idx
,
444 /* 5. Check for segment level no-execute violation */
445 if ((rwx
== 2) && (sr
& SR32_NX
)) {
446 cs
->exception_index
= POWERPC_EXCP_ISI
;
447 env
->error_code
= 0x10000000;
451 /* 6. Locate the PTE in the hash table */
452 pte_offset
= ppc_hash32_htab_lookup(env
, sr
, eaddr
, &pte
);
453 if (pte_offset
== -1) {
455 cs
->exception_index
= POWERPC_EXCP_ISI
;
456 env
->error_code
= 0x40000000;
458 cs
->exception_index
= POWERPC_EXCP_DSI
;
460 env
->spr
[SPR_DAR
] = eaddr
;
462 env
->spr
[SPR_DSISR
] = 0x42000000;
464 env
->spr
[SPR_DSISR
] = 0x40000000;
470 qemu_log_mask(CPU_LOG_MMU
,
471 "found PTE at offset %08" HWADDR_PRIx
"\n", pte_offset
);
473 /* 7. Check access permissions */
475 prot
= ppc_hash32_pte_prot(env
, sr
, pte
);
477 if (need_prot
[rwx
] & ~prot
) {
478 /* Access right violation */
479 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
481 cs
->exception_index
= POWERPC_EXCP_ISI
;
482 env
->error_code
= 0x08000000;
484 cs
->exception_index
= POWERPC_EXCP_DSI
;
486 env
->spr
[SPR_DAR
] = eaddr
;
488 env
->spr
[SPR_DSISR
] = 0x0a000000;
490 env
->spr
[SPR_DSISR
] = 0x08000000;
496 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
498 /* 8. Update PTE referenced and changed bits if necessary */
500 new_pte1
= pte
.pte1
| HPTE32_R_R
; /* set referenced bit */
502 new_pte1
|= HPTE32_R_C
; /* set changed (dirty) bit */
504 /* Treat the page as read-only for now, so that a later write
505 * will pass through this function again to set the C bit */
509 if (new_pte1
!= pte
.pte1
) {
510 ppc_hash32_store_hpte1(env
, pte_offset
, new_pte1
);
513 /* 9. Determine the real address from the PTE */
515 raddr
= ppc_hash32_pte_raddr(sr
, pte
, eaddr
);
517 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
518 prot
, mmu_idx
, TARGET_PAGE_SIZE
);
523 hwaddr
ppc_hash32_get_phys_page_debug(CPUPPCState
*env
, target_ulong eaddr
)
527 ppc_hash_pte32_t pte
;
531 /* Translation is off */
535 if (env
->nb_BATs
!= 0) {
536 hwaddr raddr
= ppc_hash32_bat_lookup(env
, eaddr
, 0, &prot
);
542 sr
= env
->sr
[eaddr
>> 28];
545 /* FIXME: Add suitable debug support for Direct Store segments */
549 pte_offset
= ppc_hash32_htab_lookup(env
, sr
, eaddr
, &pte
);
550 if (pte_offset
== -1) {
554 return ppc_hash32_pte_raddr(sr
, pte
, eaddr
) & TARGET_PAGE_MASK
;