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"
31 # define LOG_MMU(...) qemu_log(__VA_ARGS__)
32 # define LOG_MMU_STATE(cpu) log_cpu_state((cpu), 0)
34 # define LOG_MMU(...) do { } while (0)
35 # define LOG_MMU_STATE(cpu) do { } while (0)
39 # define LOG_BATS(...) qemu_log(__VA_ARGS__)
41 # define LOG_BATS(...) do { } while (0)
44 struct mmu_ctx_hash32
{
45 hwaddr raddr
; /* Real address */
46 int prot
; /* Protection bits */
47 int key
; /* Access key */
50 static int ppc_hash32_pp_prot(int key
, int pp
, int nx
)
59 prot
= PAGE_READ
| PAGE_WRITE
;
81 prot
= PAGE_READ
| PAGE_WRITE
;
95 static int ppc_hash32_pte_prot(CPUPPCState
*env
,
96 target_ulong sr
, ppc_hash_pte32_t pte
)
100 key
= !!(msr_pr
? (sr
& SR32_KP
) : (sr
& SR32_KS
));
101 pp
= pte
.pte1
& HPTE32_R_PP
;
103 return ppc_hash32_pp_prot(key
, pp
, !!(sr
& SR32_NX
));
106 static target_ulong
hash32_bat_size(CPUPPCState
*env
,
107 target_ulong batu
, target_ulong batl
)
109 if ((msr_pr
&& !(batu
& BATU32_VP
))
110 || (!msr_pr
&& !(batu
& BATU32_VS
))) {
114 return BATU32_BEPI
& ~((batu
& BATU32_BL
) << 15);
117 static int hash32_bat_prot(CPUPPCState
*env
,
118 target_ulong batu
, target_ulong batl
)
123 pp
= batl
& BATL32_PP
;
125 prot
= PAGE_READ
| PAGE_EXEC
;
133 static target_ulong
hash32_bat_601_size(CPUPPCState
*env
,
134 target_ulong batu
, target_ulong batl
)
136 if (!(batl
& BATL32_601_V
)) {
140 return BATU32_BEPI
& ~((batl
& BATL32_601_BL
) << 17);
143 static int hash32_bat_601_prot(CPUPPCState
*env
,
144 target_ulong batu
, target_ulong batl
)
148 pp
= batu
& BATU32_601_PP
;
150 key
= !!(batu
& BATU32_601_KS
);
152 key
= !!(batu
& BATU32_601_KP
);
154 return ppc_hash32_pp_prot(key
, pp
, 0);
157 static hwaddr
ppc_hash32_bat_lookup(CPUPPCState
*env
, target_ulong ea
, int rwx
,
160 target_ulong
*BATlt
, *BATut
;
163 LOG_BATS("%s: %cBAT v " TARGET_FMT_lx
"\n", __func__
,
164 rwx
== 2 ? 'I' : 'D', ea
);
166 BATlt
= env
->IBAT
[1];
167 BATut
= env
->IBAT
[0];
169 BATlt
= env
->DBAT
[1];
170 BATut
= env
->DBAT
[0];
172 for (i
= 0; i
< env
->nb_BATs
; i
++) {
173 target_ulong batu
= BATut
[i
];
174 target_ulong batl
= BATlt
[i
];
177 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
178 mask
= hash32_bat_601_size(env
, batu
, batl
);
180 mask
= hash32_bat_size(env
, batu
, batl
);
182 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
183 " BATl " TARGET_FMT_lx
"\n", __func__
,
184 type
== ACCESS_CODE
? 'I' : 'D', i
, ea
, batu
, batl
);
186 if (mask
&& ((ea
& mask
) == (batu
& BATU32_BEPI
))) {
187 hwaddr raddr
= (batl
& mask
) | (ea
& ~mask
);
189 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
190 *prot
= hash32_bat_601_prot(env
, batu
, batl
);
192 *prot
= hash32_bat_prot(env
, batu
, batl
);
195 return raddr
& TARGET_PAGE_MASK
;
200 #if defined(DEBUG_BATS)
201 if (qemu_log_enabled()) {
202 LOG_BATS("no BAT match for " TARGET_FMT_lx
":\n", ea
);
203 for (i
= 0; i
< 4; i
++) {
206 BEPIu
= *BATu
& BATU32_BEPIU
;
207 BEPIl
= *BATu
& BATU32_BEPIL
;
208 bl
= (*BATu
& 0x00001FFC) << 15;
209 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
210 " BATl " TARGET_FMT_lx
"\n\t" TARGET_FMT_lx
" "
211 TARGET_FMT_lx
" " TARGET_FMT_lx
"\n",
212 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, ea
,
213 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
221 static int ppc_hash32_direct_store(CPUPPCState
*env
, target_ulong sr
,
222 target_ulong eaddr
, int rwx
,
223 hwaddr
*raddr
, int *prot
)
225 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
226 int key
= !!(msr_pr
? (sr
& SR32_KP
) : (sr
& SR32_KS
));
228 LOG_MMU("direct store...\n");
230 if ((sr
& 0x1FF00000) >> 20 == 0x07f) {
231 /* Memory-forced I/O controller interface access */
232 /* If T=1 and BUID=x'07F', the 601 performs a memory access
233 * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
235 *raddr
= ((sr
& 0xF) << 28) | (eaddr
& 0x0FFFFFFF);
236 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
241 /* No code fetch is allowed in direct-store areas */
242 cs
->exception_index
= POWERPC_EXCP_ISI
;
243 env
->error_code
= 0x10000000;
247 switch (env
->access_type
) {
249 /* Integer load/store : only access allowed */
252 /* Floating point load/store */
253 cs
->exception_index
= POWERPC_EXCP_ALIGN
;
254 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
255 env
->spr
[SPR_DAR
] = eaddr
;
258 /* lwarx, ldarx or srwcx. */
260 env
->spr
[SPR_DAR
] = eaddr
;
262 env
->spr
[SPR_DSISR
] = 0x06000000;
264 env
->spr
[SPR_DSISR
] = 0x04000000;
268 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
269 /* Should make the instruction do no-op.
270 * As it already do no-op, it's quite easy :-)
276 cs
->exception_index
= POWERPC_EXCP_DSI
;
278 env
->spr
[SPR_DAR
] = eaddr
;
280 env
->spr
[SPR_DSISR
] = 0x06100000;
282 env
->spr
[SPR_DSISR
] = 0x04100000;
286 qemu_log("ERROR: instruction should not need "
287 "address translation\n");
290 if ((rwx
== 1 || key
!= 1) && (rwx
== 0 || key
!= 0)) {
294 cs
->exception_index
= POWERPC_EXCP_DSI
;
296 env
->spr
[SPR_DAR
] = eaddr
;
298 env
->spr
[SPR_DSISR
] = 0x0a000000;
300 env
->spr
[SPR_DSISR
] = 0x08000000;
306 hwaddr
get_pteg_offset32(CPUPPCState
*env
, hwaddr hash
)
308 return (hash
* HASH_PTEG_SIZE_32
) & env
->htab_mask
;
311 static hwaddr
ppc_hash32_pteg_search(CPUPPCState
*env
, hwaddr pteg_off
,
312 bool secondary
, target_ulong ptem
,
313 ppc_hash_pte32_t
*pte
)
315 hwaddr pte_offset
= pteg_off
;
316 target_ulong pte0
, pte1
;
319 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
320 pte0
= ppc_hash32_load_hpte0(env
, pte_offset
);
321 pte1
= ppc_hash32_load_hpte1(env
, pte_offset
);
323 if ((pte0
& HPTE32_V_VALID
)
324 && (secondary
== !!(pte0
& HPTE32_V_SECONDARY
))
325 && HPTE32_V_COMPARE(pte0
, ptem
)) {
331 pte_offset
+= HASH_PTE_SIZE_32
;
337 static hwaddr
ppc_hash32_htab_lookup(CPUPPCState
*env
,
338 target_ulong sr
, target_ulong eaddr
,
339 ppc_hash_pte32_t
*pte
)
341 hwaddr pteg_off
, pte_offset
;
343 uint32_t vsid
, pgidx
, ptem
;
345 vsid
= sr
& SR32_VSID
;
346 pgidx
= (eaddr
& ~SEGMENT_MASK_256M
) >> TARGET_PAGE_BITS
;
348 ptem
= (vsid
<< 7) | (pgidx
>> 10);
350 /* Page address translation */
351 LOG_MMU("htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
352 " hash " TARGET_FMT_plx
"\n",
353 env
->htab_base
, env
->htab_mask
, hash
);
355 /* Primary PTEG lookup */
356 LOG_MMU("0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
357 " vsid=%" PRIx32
" ptem=%" PRIx32
358 " hash=" TARGET_FMT_plx
"\n",
359 env
->htab_base
, env
->htab_mask
, vsid
, ptem
, hash
);
360 pteg_off
= get_pteg_offset32(env
, hash
);
361 pte_offset
= ppc_hash32_pteg_search(env
, pteg_off
, 0, ptem
, pte
);
362 if (pte_offset
== -1) {
363 /* Secondary PTEG lookup */
364 LOG_MMU("1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
365 " vsid=%" PRIx32
" api=%" PRIx32
366 " hash=" TARGET_FMT_plx
"\n", env
->htab_base
,
367 env
->htab_mask
, vsid
, ptem
, ~hash
);
368 pteg_off
= get_pteg_offset32(env
, ~hash
);
369 pte_offset
= ppc_hash32_pteg_search(env
, pteg_off
, 1, ptem
, pte
);
375 static hwaddr
ppc_hash32_pte_raddr(target_ulong sr
, ppc_hash_pte32_t pte
,
378 hwaddr rpn
= pte
.pte1
& HPTE32_R_RPN
;
379 hwaddr mask
= ~TARGET_PAGE_MASK
;
381 return (rpn
& ~mask
) | (eaddr
& mask
);
384 int ppc_hash32_handle_mmu_fault(PowerPCCPU
*cpu
, target_ulong eaddr
, int rwx
,
387 CPUState
*cs
= CPU(cpu
);
388 CPUPPCState
*env
= &cpu
->env
;
391 ppc_hash_pte32_t pte
;
394 const int need_prot
[] = {PAGE_READ
, PAGE_WRITE
, PAGE_EXEC
};
397 assert((rwx
== 0) || (rwx
== 1) || (rwx
== 2));
399 /* 1. Handle real mode accesses */
400 if (((rwx
== 2) && (msr_ir
== 0)) || ((rwx
!= 2) && (msr_dr
== 0))) {
401 /* Translation is off */
403 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
404 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
, mmu_idx
,
409 /* 2. Check Block Address Translation entries (BATs) */
410 if (env
->nb_BATs
!= 0) {
411 raddr
= ppc_hash32_bat_lookup(env
, eaddr
, rwx
, &prot
);
413 if (need_prot
[rwx
] & ~prot
) {
415 cs
->exception_index
= POWERPC_EXCP_ISI
;
416 env
->error_code
= 0x08000000;
418 cs
->exception_index
= POWERPC_EXCP_DSI
;
420 env
->spr
[SPR_DAR
] = eaddr
;
422 env
->spr
[SPR_DSISR
] = 0x0a000000;
424 env
->spr
[SPR_DSISR
] = 0x08000000;
430 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
,
431 raddr
& TARGET_PAGE_MASK
, prot
, mmu_idx
,
437 /* 3. Look up the Segment Register */
438 sr
= env
->sr
[eaddr
>> 28];
440 /* 4. Handle direct store segments */
442 if (ppc_hash32_direct_store(env
, sr
, eaddr
, rwx
,
443 &raddr
, &prot
) == 0) {
444 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
,
445 raddr
& TARGET_PAGE_MASK
, prot
, mmu_idx
,
453 /* 5. Check for segment level no-execute violation */
454 if ((rwx
== 2) && (sr
& SR32_NX
)) {
455 cs
->exception_index
= POWERPC_EXCP_ISI
;
456 env
->error_code
= 0x10000000;
460 /* 6. Locate the PTE in the hash table */
461 pte_offset
= ppc_hash32_htab_lookup(env
, sr
, eaddr
, &pte
);
462 if (pte_offset
== -1) {
464 cs
->exception_index
= POWERPC_EXCP_ISI
;
465 env
->error_code
= 0x40000000;
467 cs
->exception_index
= POWERPC_EXCP_DSI
;
469 env
->spr
[SPR_DAR
] = eaddr
;
471 env
->spr
[SPR_DSISR
] = 0x42000000;
473 env
->spr
[SPR_DSISR
] = 0x40000000;
479 LOG_MMU("found PTE at offset %08" HWADDR_PRIx
"\n", pte_offset
);
481 /* 7. Check access permissions */
483 prot
= ppc_hash32_pte_prot(env
, sr
, pte
);
485 if (need_prot
[rwx
] & ~prot
) {
486 /* Access right violation */
487 LOG_MMU("PTE access rejected\n");
489 cs
->exception_index
= POWERPC_EXCP_ISI
;
490 env
->error_code
= 0x08000000;
492 cs
->exception_index
= POWERPC_EXCP_DSI
;
494 env
->spr
[SPR_DAR
] = eaddr
;
496 env
->spr
[SPR_DSISR
] = 0x0a000000;
498 env
->spr
[SPR_DSISR
] = 0x08000000;
504 LOG_MMU("PTE access granted !\n");
506 /* 8. Update PTE referenced and changed bits if necessary */
508 new_pte1
= pte
.pte1
| HPTE32_R_R
; /* set referenced bit */
510 new_pte1
|= HPTE32_R_C
; /* set changed (dirty) bit */
512 /* Treat the page as read-only for now, so that a later write
513 * will pass through this function again to set the C bit */
517 if (new_pte1
!= pte
.pte1
) {
518 ppc_hash32_store_hpte1(env
, pte_offset
, new_pte1
);
521 /* 9. Determine the real address from the PTE */
523 raddr
= ppc_hash32_pte_raddr(sr
, pte
, eaddr
);
525 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
526 prot
, mmu_idx
, TARGET_PAGE_SIZE
);
531 hwaddr
ppc_hash32_get_phys_page_debug(CPUPPCState
*env
, target_ulong eaddr
)
535 ppc_hash_pte32_t pte
;
539 /* Translation is off */
543 if (env
->nb_BATs
!= 0) {
544 hwaddr raddr
= ppc_hash32_bat_lookup(env
, eaddr
, 0, &prot
);
550 sr
= env
->sr
[eaddr
>> 28];
553 /* FIXME: Add suitable debug support for Direct Store segments */
557 pte_offset
= ppc_hash32_htab_lookup(env
, sr
, eaddr
, &pte
);
558 if (pte_offset
== -1) {
562 return ppc_hash32_pte_raddr(sr
, pte
, eaddr
) & TARGET_PAGE_MASK
;