2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
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 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/>.
28 #include "helper_regs.h"
29 #include "qemu-common.h"
35 //#define DEBUG_SOFTWARE_TLB
36 //#define DUMP_PAGE_TABLES
37 //#define DEBUG_EXCEPTIONS
38 //#define FLUSH_ALL_TLBS
41 # define LOG_MMU(...) qemu_log(__VA_ARGS__)
42 # define LOG_MMU_STATE(env) log_cpu_state((env), 0)
44 # define LOG_MMU(...) do { } while (0)
45 # define LOG_MMU_STATE(...) do { } while (0)
49 #ifdef DEBUG_SOFTWARE_TLB
50 # define LOG_SWTLB(...) qemu_log(__VA_ARGS__)
52 # define LOG_SWTLB(...) do { } while (0)
56 # define LOG_BATS(...) qemu_log(__VA_ARGS__)
58 # define LOG_BATS(...) do { } while (0)
62 # define LOG_SLB(...) qemu_log(__VA_ARGS__)
64 # define LOG_SLB(...) do { } while (0)
67 #ifdef DEBUG_EXCEPTIONS
68 # define LOG_EXCP(...) qemu_log(__VA_ARGS__)
70 # define LOG_EXCP(...) do { } while (0)
74 /*****************************************************************************/
75 /* PowerPC MMU emulation */
77 #if defined(CONFIG_USER_ONLY)
78 int cpu_ppc_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
79 int mmu_idx
, int is_softmmu
)
81 int exception
, error_code
;
84 exception
= POWERPC_EXCP_ISI
;
85 error_code
= 0x40000000;
87 exception
= POWERPC_EXCP_DSI
;
88 error_code
= 0x40000000;
90 error_code
|= 0x02000000;
91 env
->spr
[SPR_DAR
] = address
;
92 env
->spr
[SPR_DSISR
] = error_code
;
94 env
->exception_index
= exception
;
95 env
->error_code
= error_code
;
100 target_phys_addr_t
cpu_get_phys_page_debug (CPUState
*env
, target_ulong addr
)
106 /* Common routines used by software and hardware TLBs emulation */
107 static always_inline
int pte_is_valid (target_ulong pte0
)
109 return pte0
& 0x80000000 ? 1 : 0;
112 static always_inline
void pte_invalidate (target_ulong
*pte0
)
114 *pte0
&= ~0x80000000;
117 #if defined(TARGET_PPC64)
118 static always_inline
int pte64_is_valid (target_ulong pte0
)
120 return pte0
& 0x0000000000000001ULL
? 1 : 0;
123 static always_inline
void pte64_invalidate (target_ulong
*pte0
)
125 *pte0
&= ~0x0000000000000001ULL
;
129 #define PTE_PTEM_MASK 0x7FFFFFBF
130 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
131 #if defined(TARGET_PPC64)
132 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
133 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
136 static always_inline
int pp_check (int key
, int pp
, int nx
)
140 /* Compute access rights */
141 /* When pp is 3/7, the result is undefined. Set it to noaccess */
148 access
|= PAGE_WRITE
;
166 access
= PAGE_READ
| PAGE_WRITE
;
176 static always_inline
int check_prot (int prot
, int rw
, int access_type
)
180 if (access_type
== ACCESS_CODE
) {
181 if (prot
& PAGE_EXEC
)
186 if (prot
& PAGE_WRITE
)
191 if (prot
& PAGE_READ
)
200 static always_inline
int _pte_check (mmu_ctx_t
*ctx
, int is_64b
,
201 target_ulong pte0
, target_ulong pte1
,
202 int h
, int rw
, int type
)
204 target_ulong ptem
, mmask
;
205 int access
, ret
, pteh
, ptev
, pp
;
209 /* Check validity and table match */
210 #if defined(TARGET_PPC64)
212 ptev
= pte64_is_valid(pte0
);
213 pteh
= (pte0
>> 1) & 1;
217 ptev
= pte_is_valid(pte0
);
218 pteh
= (pte0
>> 6) & 1;
220 if (ptev
&& h
== pteh
) {
221 /* Check vsid & api */
222 #if defined(TARGET_PPC64)
224 ptem
= pte0
& PTE64_PTEM_MASK
;
225 mmask
= PTE64_CHECK_MASK
;
226 pp
= (pte1
& 0x00000003) | ((pte1
>> 61) & 0x00000004);
227 ctx
->nx
= (pte1
>> 2) & 1; /* No execute bit */
228 ctx
->nx
|= (pte1
>> 3) & 1; /* Guarded bit */
232 ptem
= pte0
& PTE_PTEM_MASK
;
233 mmask
= PTE_CHECK_MASK
;
234 pp
= pte1
& 0x00000003;
236 if (ptem
== ctx
->ptem
) {
237 if (ctx
->raddr
!= (target_phys_addr_t
)-1ULL) {
238 /* all matches should have equal RPN, WIMG & PP */
239 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
240 qemu_log("Bad RPN/WIMG/PP\n");
244 /* Compute access rights */
245 access
= pp_check(ctx
->key
, pp
, ctx
->nx
);
246 /* Keep the matching PTE informations */
249 ret
= check_prot(ctx
->prot
, rw
, type
);
252 LOG_MMU("PTE access granted !\n");
254 /* Access right violation */
255 LOG_MMU("PTE access rejected\n");
263 static always_inline
int pte32_check (mmu_ctx_t
*ctx
,
264 target_ulong pte0
, target_ulong pte1
,
265 int h
, int rw
, int type
)
267 return _pte_check(ctx
, 0, pte0
, pte1
, h
, rw
, type
);
270 #if defined(TARGET_PPC64)
271 static always_inline
int pte64_check (mmu_ctx_t
*ctx
,
272 target_ulong pte0
, target_ulong pte1
,
273 int h
, int rw
, int type
)
275 return _pte_check(ctx
, 1, pte0
, pte1
, h
, rw
, type
);
279 static always_inline
int pte_update_flags (mmu_ctx_t
*ctx
, target_ulong
*pte1p
,
284 /* Update page flags */
285 if (!(*pte1p
& 0x00000100)) {
286 /* Update accessed flag */
287 *pte1p
|= 0x00000100;
290 if (!(*pte1p
& 0x00000080)) {
291 if (rw
== 1 && ret
== 0) {
292 /* Update changed flag */
293 *pte1p
|= 0x00000080;
296 /* Force page fault for first write access */
297 ctx
->prot
&= ~PAGE_WRITE
;
304 /* Software driven TLB helpers */
305 static always_inline
int ppc6xx_tlb_getnum (CPUState
*env
, target_ulong eaddr
,
306 int way
, int is_code
)
310 /* Select TLB num in a way from address */
311 nr
= (eaddr
>> TARGET_PAGE_BITS
) & (env
->tlb_per_way
- 1);
313 nr
+= env
->tlb_per_way
* way
;
314 /* 6xx have separate TLBs for instructions and data */
315 if (is_code
&& env
->id_tlbs
== 1)
321 static always_inline
void ppc6xx_tlb_invalidate_all (CPUState
*env
)
326 //LOG_SWTLB("Invalidate all TLBs\n");
327 /* Invalidate all defined software TLB */
329 if (env
->id_tlbs
== 1)
331 for (nr
= 0; nr
< max
; nr
++) {
332 tlb
= &env
->tlb
[nr
].tlb6
;
333 pte_invalidate(&tlb
->pte0
);
338 static always_inline
void __ppc6xx_tlb_invalidate_virt (CPUState
*env
,
343 #if !defined(FLUSH_ALL_TLBS)
347 /* Invalidate ITLB + DTLB, all ways */
348 for (way
= 0; way
< env
->nb_ways
; way
++) {
349 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
, is_code
);
350 tlb
= &env
->tlb
[nr
].tlb6
;
351 if (pte_is_valid(tlb
->pte0
) && (match_epn
== 0 || eaddr
== tlb
->EPN
)) {
352 LOG_SWTLB("TLB invalidate %d/%d " ADDRX
"\n",
353 nr
, env
->nb_tlb
, eaddr
);
354 pte_invalidate(&tlb
->pte0
);
355 tlb_flush_page(env
, tlb
->EPN
);
359 /* XXX: PowerPC specification say this is valid as well */
360 ppc6xx_tlb_invalidate_all(env
);
364 static always_inline
void ppc6xx_tlb_invalidate_virt (CPUState
*env
,
368 __ppc6xx_tlb_invalidate_virt(env
, eaddr
, is_code
, 0);
371 void ppc6xx_tlb_store (CPUState
*env
, target_ulong EPN
, int way
, int is_code
,
372 target_ulong pte0
, target_ulong pte1
)
377 nr
= ppc6xx_tlb_getnum(env
, EPN
, way
, is_code
);
378 tlb
= &env
->tlb
[nr
].tlb6
;
379 LOG_SWTLB("Set TLB %d/%d EPN " ADDRX
" PTE0 " ADDRX
380 " PTE1 " ADDRX
"\n", nr
, env
->nb_tlb
, EPN
, pte0
, pte1
);
381 /* Invalidate any pending reference in Qemu for this virtual address */
382 __ppc6xx_tlb_invalidate_virt(env
, EPN
, is_code
, 1);
386 /* Store last way for LRU mechanism */
390 static always_inline
int ppc6xx_tlb_check (CPUState
*env
, mmu_ctx_t
*ctx
,
391 target_ulong eaddr
, int rw
,
399 ret
= -1; /* No TLB found */
400 for (way
= 0; way
< env
->nb_ways
; way
++) {
401 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
,
402 access_type
== ACCESS_CODE
? 1 : 0);
403 tlb
= &env
->tlb
[nr
].tlb6
;
404 /* This test "emulates" the PTE index match for hardware TLBs */
405 if ((eaddr
& TARGET_PAGE_MASK
) != tlb
->EPN
) {
406 LOG_SWTLB("TLB %d/%d %s [" ADDRX
" " ADDRX
409 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
410 tlb
->EPN
, tlb
->EPN
+ TARGET_PAGE_SIZE
, eaddr
);
413 LOG_SWTLB("TLB %d/%d %s " ADDRX
" <> " ADDRX
" " ADDRX
416 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
417 tlb
->EPN
, eaddr
, tlb
->pte1
,
418 rw
? 'S' : 'L', access_type
== ACCESS_CODE
? 'I' : 'D');
419 switch (pte32_check(ctx
, tlb
->pte0
, tlb
->pte1
, 0, rw
, access_type
)) {
421 /* TLB inconsistency */
424 /* Access violation */
434 /* XXX: we should go on looping to check all TLBs consistency
435 * but we can speed-up the whole thing as the
436 * result would be undefined if TLBs are not consistent.
445 LOG_SWTLB("found TLB at addr " PADDRX
" prot=%01x ret=%d\n",
446 ctx
->raddr
& TARGET_PAGE_MASK
, ctx
->prot
, ret
);
447 /* Update page flags */
448 pte_update_flags(ctx
, &env
->tlb
[best
].tlb6
.pte1
, ret
, rw
);
454 /* Perform BAT hit & translation */
455 static always_inline
void bat_size_prot (CPUState
*env
, target_ulong
*blp
,
456 int *validp
, int *protp
,
457 target_ulong
*BATu
, target_ulong
*BATl
)
462 bl
= (*BATu
& 0x00001FFC) << 15;
465 if (((msr_pr
== 0) && (*BATu
& 0x00000002)) ||
466 ((msr_pr
!= 0) && (*BATu
& 0x00000001))) {
468 pp
= *BATl
& 0x00000003;
470 prot
= PAGE_READ
| PAGE_EXEC
;
480 static always_inline
void bat_601_size_prot (CPUState
*env
,target_ulong
*blp
,
481 int *validp
, int *protp
,
486 int key
, pp
, valid
, prot
;
488 bl
= (*BATl
& 0x0000003F) << 17;
489 LOG_BATS("b %02x ==> bl " ADDRX
" msk " ADDRX
"\n",
490 (uint8_t)(*BATl
& 0x0000003F), bl
, ~bl
);
492 valid
= (*BATl
>> 6) & 1;
494 pp
= *BATu
& 0x00000003;
496 key
= (*BATu
>> 3) & 1;
498 key
= (*BATu
>> 2) & 1;
499 prot
= pp_check(key
, pp
, 0);
506 static always_inline
int get_bat (CPUState
*env
, mmu_ctx_t
*ctx
,
507 target_ulong
virtual, int rw
, int type
)
509 target_ulong
*BATlt
, *BATut
, *BATu
, *BATl
;
510 target_ulong base
, BEPIl
, BEPIu
, bl
;
514 LOG_BATS("%s: %cBAT v " ADDRX
"\n", __func__
,
515 type
== ACCESS_CODE
? 'I' : 'D', virtual);
518 BATlt
= env
->IBAT
[1];
519 BATut
= env
->IBAT
[0];
522 BATlt
= env
->DBAT
[1];
523 BATut
= env
->DBAT
[0];
526 base
= virtual & 0xFFFC0000;
527 for (i
= 0; i
< env
->nb_BATs
; i
++) {
530 BEPIu
= *BATu
& 0xF0000000;
531 BEPIl
= *BATu
& 0x0FFE0000;
532 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
533 bat_601_size_prot(env
, &bl
, &valid
, &prot
, BATu
, BATl
);
535 bat_size_prot(env
, &bl
, &valid
, &prot
, BATu
, BATl
);
537 LOG_BATS("%s: %cBAT%d v " ADDRX
" BATu " ADDRX
538 " BATl " ADDRX
"\n", __func__
,
539 type
== ACCESS_CODE
? 'I' : 'D', i
, virtual, *BATu
, *BATl
);
540 if ((virtual & 0xF0000000) == BEPIu
&&
541 ((virtual & 0x0FFE0000) & ~bl
) == BEPIl
) {
544 /* Get physical address */
545 ctx
->raddr
= (*BATl
& 0xF0000000) |
546 ((virtual & 0x0FFE0000 & bl
) | (*BATl
& 0x0FFE0000)) |
547 (virtual & 0x0001F000);
548 /* Compute access rights */
550 ret
= check_prot(ctx
->prot
, rw
, type
);
552 LOG_BATS("BAT %d match: r " PADDRX
" prot=%c%c\n",
553 i
, ctx
->raddr
, ctx
->prot
& PAGE_READ
? 'R' : '-',
554 ctx
->prot
& PAGE_WRITE
? 'W' : '-');
560 #if defined(DEBUG_BATS)
561 if (qemu_log_enabled()) {
562 LOG_BATS("no BAT match for " ADDRX
":\n", virtual);
563 for (i
= 0; i
< 4; i
++) {
566 BEPIu
= *BATu
& 0xF0000000;
567 BEPIl
= *BATu
& 0x0FFE0000;
568 bl
= (*BATu
& 0x00001FFC) << 15;
569 LOG_BATS("%s: %cBAT%d v " ADDRX
" BATu " ADDRX
570 " BATl " ADDRX
" \n\t" ADDRX
" " ADDRX
" " ADDRX
"\n",
571 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, virtual,
572 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
581 /* PTE table lookup */
582 static always_inline
int _find_pte (mmu_ctx_t
*ctx
, int is_64b
, int h
,
584 int target_page_bits
)
586 target_ulong base
, pte0
, pte1
;
590 ret
= -1; /* No entry found */
591 base
= ctx
->pg_addr
[h
];
592 for (i
= 0; i
< 8; i
++) {
593 #if defined(TARGET_PPC64)
595 pte0
= ldq_phys(base
+ (i
* 16));
596 pte1
= ldq_phys(base
+ (i
* 16) + 8);
598 /* We have a TLB that saves 4K pages, so let's
599 * split a huge page to 4k chunks */
600 if (target_page_bits
!= TARGET_PAGE_BITS
)
601 pte1
|= (ctx
->eaddr
& (( 1 << target_page_bits
) - 1))
604 r
= pte64_check(ctx
, pte0
, pte1
, h
, rw
, type
);
605 LOG_MMU("Load pte from " ADDRX
" => " ADDRX
" " ADDRX
606 " %d %d %d " ADDRX
"\n",
607 base
+ (i
* 16), pte0
, pte1
,
608 (int)(pte0
& 1), h
, (int)((pte0
>> 1) & 1),
613 pte0
= ldl_phys(base
+ (i
* 8));
614 pte1
= ldl_phys(base
+ (i
* 8) + 4);
615 r
= pte32_check(ctx
, pte0
, pte1
, h
, rw
, type
);
616 LOG_MMU("Load pte from " ADDRX
" => " ADDRX
" " ADDRX
617 " %d %d %d " ADDRX
"\n",
618 base
+ (i
* 8), pte0
, pte1
,
619 (int)(pte0
>> 31), h
, (int)((pte0
>> 6) & 1),
624 /* PTE inconsistency */
627 /* Access violation */
637 /* XXX: we should go on looping to check all PTEs consistency
638 * but if we can speed-up the whole thing as the
639 * result would be undefined if PTEs are not consistent.
648 LOG_MMU("found PTE at addr " PADDRX
" prot=%01x ret=%d\n",
649 ctx
->raddr
, ctx
->prot
, ret
);
650 /* Update page flags */
652 if (pte_update_flags(ctx
, &pte1
, ret
, rw
) == 1) {
653 #if defined(TARGET_PPC64)
655 stq_phys_notdirty(base
+ (good
* 16) + 8, pte1
);
659 stl_phys_notdirty(base
+ (good
* 8) + 4, pte1
);
667 static always_inline
int find_pte32 (mmu_ctx_t
*ctx
, int h
, int rw
,
668 int type
, int target_page_bits
)
670 return _find_pte(ctx
, 0, h
, rw
, type
, target_page_bits
);
673 #if defined(TARGET_PPC64)
674 static always_inline
int find_pte64 (mmu_ctx_t
*ctx
, int h
, int rw
,
675 int type
, int target_page_bits
)
677 return _find_pte(ctx
, 1, h
, rw
, type
, target_page_bits
);
681 static always_inline
int find_pte (CPUState
*env
, mmu_ctx_t
*ctx
,
682 int h
, int rw
, int type
,
683 int target_page_bits
)
685 #if defined(TARGET_PPC64)
686 if (env
->mmu_model
& POWERPC_MMU_64
)
687 return find_pte64(ctx
, h
, rw
, type
, target_page_bits
);
690 return find_pte32(ctx
, h
, rw
, type
, target_page_bits
);
693 #if defined(TARGET_PPC64)
694 static ppc_slb_t
*slb_get_entry(CPUPPCState
*env
, int nr
)
696 ppc_slb_t
*retval
= &env
->slb
[nr
];
698 #if 0 // XXX implement bridge mode?
699 if (env
->spr
[SPR_ASR
] & 1) {
700 target_phys_addr_t sr_base
;
702 sr_base
= env
->spr
[SPR_ASR
] & 0xfffffffffffff000;
703 sr_base
+= (12 * nr
);
705 retval
->tmp64
= ldq_phys(sr_base
);
706 retval
->tmp
= ldl_phys(sr_base
+ 8);
713 static void slb_set_entry(CPUPPCState
*env
, int nr
, ppc_slb_t
*slb
)
715 ppc_slb_t
*entry
= &env
->slb
[nr
];
720 entry
->tmp64
= slb
->tmp64
;
721 entry
->tmp
= slb
->tmp
;
724 static always_inline
int slb_is_valid (ppc_slb_t
*slb
)
726 return (int)(slb
->tmp64
& 0x0000000008000000ULL
);
729 static always_inline
void slb_invalidate (ppc_slb_t
*slb
)
731 slb
->tmp64
&= ~0x0000000008000000ULL
;
734 static always_inline
int slb_lookup (CPUPPCState
*env
, target_ulong eaddr
,
736 target_ulong
*page_mask
, int *attr
,
737 int *target_page_bits
)
743 LOG_SLB("%s: eaddr " ADDRX
"\n", __func__
, eaddr
);
744 mask
= 0x0000000000000000ULL
; /* Avoid gcc warning */
745 for (n
= 0; n
< env
->slb_nr
; n
++) {
746 ppc_slb_t
*slb
= slb_get_entry(env
, n
);
748 LOG_SLB("%s: seg %d %016" PRIx64
" %08"
749 PRIx32
"\n", __func__
, n
, slb
->tmp64
, slb
->tmp
);
750 if (slb_is_valid(slb
)) {
751 /* SLB entry is valid */
752 if (slb
->tmp
& 0x8) {
754 mask
= 0xFFFF000000000000ULL
;
755 if (target_page_bits
)
756 *target_page_bits
= 24; // XXX 16M pages?
759 mask
= 0xFFFFFFFFF0000000ULL
;
760 if (target_page_bits
)
761 *target_page_bits
= TARGET_PAGE_BITS
;
763 if ((eaddr
& mask
) == (slb
->tmp64
& mask
)) {
765 *vsid
= ((slb
->tmp64
<< 24) | (slb
->tmp
>> 8)) & 0x0003FFFFFFFFFFFFULL
;
767 *attr
= slb
->tmp
& 0xFF;
777 void ppc_slb_invalidate_all (CPUPPCState
*env
)
779 int n
, do_invalidate
;
782 /* XXX: Warning: slbia never invalidates the first segment */
783 for (n
= 1; n
< env
->slb_nr
; n
++) {
784 ppc_slb_t
*slb
= slb_get_entry(env
, n
);
786 if (slb_is_valid(slb
)) {
788 slb_set_entry(env
, n
, slb
);
789 /* XXX: given the fact that segment size is 256 MB or 1TB,
790 * and we still don't have a tlb_flush_mask(env, n, mask)
791 * in Qemu, we just invalidate all TLBs
800 void ppc_slb_invalidate_one (CPUPPCState
*env
, uint64_t T0
)
802 target_ulong vsid
, page_mask
;
806 n
= slb_lookup(env
, T0
, &vsid
, &page_mask
, &attr
, NULL
);
808 ppc_slb_t
*slb
= slb_get_entry(env
, n
);
810 if (slb_is_valid(slb
)) {
812 slb_set_entry(env
, n
, slb
);
813 /* XXX: given the fact that segment size is 256 MB or 1TB,
814 * and we still don't have a tlb_flush_mask(env, n, mask)
815 * in Qemu, we just invalidate all TLBs
822 target_ulong
ppc_load_slb (CPUPPCState
*env
, int slb_nr
)
825 ppc_slb_t
*slb
= slb_get_entry(env
, slb_nr
);
827 if (slb_is_valid(slb
)) {
828 /* SLB entry is valid */
829 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
830 rt
= slb
->tmp
>> 8; /* 65:88 => 40:63 */
831 rt
|= (slb
->tmp64
& 0x7) << 24; /* 62:64 => 37:39 */
832 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
833 rt
|= ((slb
->tmp
>> 4) & 0xF) << 27;
837 LOG_SLB("%s: %016" PRIx64
" %08" PRIx32
" => %d "
838 ADDRX
"\n", __func__
, slb
->tmp64
, slb
->tmp
, slb_nr
, rt
);
843 void ppc_store_slb (CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
849 int flags
, valid
, slb_nr
;
852 flags
= ((rs
>> 8) & 0xf);
855 valid
= (rb
& (1 << 27));
858 slb
= slb_get_entry(env
, slb_nr
);
859 slb
->tmp64
= (esid
<< 28) | valid
| (vsid
>> 24);
860 slb
->tmp
= (vsid
<< 8) | (flags
<< 3);
862 LOG_SLB("%s: %d " ADDRX
" - " ADDRX
" => %016" PRIx64
863 " %08" PRIx32
"\n", __func__
,
864 slb_nr
, rb
, rs
, slb
->tmp64
, slb
->tmp
);
866 slb_set_entry(env
, slb_nr
, slb
);
868 #endif /* defined(TARGET_PPC64) */
870 /* Perform segment based translation */
871 static always_inline target_phys_addr_t
get_pgaddr (target_phys_addr_t sdr1
,
873 target_phys_addr_t hash
,
874 target_phys_addr_t mask
)
876 return (sdr1
& ((target_phys_addr_t
)(-1ULL) << sdr_sh
)) | (hash
& mask
);
879 static always_inline
int get_segment (CPUState
*env
, mmu_ctx_t
*ctx
,
880 target_ulong eaddr
, int rw
, int type
)
882 target_phys_addr_t sdr
, hash
, mask
, sdr_mask
, htab_mask
;
883 target_ulong sr
, vsid
, vsid_mask
, pgidx
, page_mask
;
884 #if defined(TARGET_PPC64)
887 int ds
, vsid_sh
, sdr_sh
, pr
, target_page_bits
;
891 #if defined(TARGET_PPC64)
892 if (env
->mmu_model
& POWERPC_MMU_64
) {
893 LOG_MMU("Check SLBs\n");
894 ret
= slb_lookup(env
, eaddr
, &vsid
, &page_mask
, &attr
,
898 ctx
->key
= ((attr
& 0x40) && (pr
!= 0)) ||
899 ((attr
& 0x80) && (pr
== 0)) ? 1 : 0;
901 ctx
->nx
= attr
& 0x10 ? 1 : 0;
903 vsid_mask
= 0x00003FFFFFFFFF80ULL
;
908 #endif /* defined(TARGET_PPC64) */
910 sr
= env
->sr
[eaddr
>> 28];
911 page_mask
= 0x0FFFFFFF;
912 ctx
->key
= (((sr
& 0x20000000) && (pr
!= 0)) ||
913 ((sr
& 0x40000000) && (pr
== 0))) ? 1 : 0;
914 ds
= sr
& 0x80000000 ? 1 : 0;
915 ctx
->nx
= sr
& 0x10000000 ? 1 : 0;
916 vsid
= sr
& 0x00FFFFFF;
917 vsid_mask
= 0x01FFFFC0;
921 target_page_bits
= TARGET_PAGE_BITS
;
922 LOG_MMU("Check segment v=" ADDRX
" %d " ADDRX
923 " nip=" ADDRX
" lr=" ADDRX
" ir=%d dr=%d pr=%d %d t=%d\n",
924 eaddr
, (int)(eaddr
>> 28), sr
, env
->nip
,
925 env
->lr
, (int)msr_ir
, (int)msr_dr
, pr
!= 0 ? 1 : 0,
928 LOG_MMU("pte segment: key=%d ds %d nx %d vsid " ADDRX
"\n",
929 ctx
->key
, ds
, ctx
->nx
, vsid
);
932 /* Check if instruction fetch is allowed, if needed */
933 if (type
!= ACCESS_CODE
|| ctx
->nx
== 0) {
934 /* Page address translation */
935 /* Primary table address */
937 pgidx
= (eaddr
& page_mask
) >> target_page_bits
;
938 #if defined(TARGET_PPC64)
939 if (env
->mmu_model
& POWERPC_MMU_64
) {
940 htab_mask
= 0x0FFFFFFF >> (28 - (sdr
& 0x1F));
941 /* XXX: this is false for 1 TB segments */
942 hash
= ((vsid
^ pgidx
) << vsid_sh
) & vsid_mask
;
946 htab_mask
= sdr
& 0x000001FF;
947 hash
= ((vsid
^ pgidx
) << vsid_sh
) & vsid_mask
;
949 mask
= (htab_mask
<< sdr_sh
) | sdr_mask
;
950 LOG_MMU("sdr " PADDRX
" sh %d hash " PADDRX
951 " mask " PADDRX
" " ADDRX
"\n",
952 sdr
, sdr_sh
, hash
, mask
, page_mask
);
953 ctx
->pg_addr
[0] = get_pgaddr(sdr
, sdr_sh
, hash
, mask
);
954 /* Secondary table address */
955 hash
= (~hash
) & vsid_mask
;
956 LOG_MMU("sdr " PADDRX
" sh %d hash " PADDRX
957 " mask " PADDRX
"\n",
958 sdr
, sdr_sh
, hash
, mask
);
959 ctx
->pg_addr
[1] = get_pgaddr(sdr
, sdr_sh
, hash
, mask
);
960 #if defined(TARGET_PPC64)
961 if (env
->mmu_model
& POWERPC_MMU_64
) {
962 /* Only 5 bits of the page index are used in the AVPN */
963 if (target_page_bits
> 23) {
964 ctx
->ptem
= (vsid
<< 12) |
965 ((pgidx
<< (target_page_bits
- 16)) & 0xF80);
967 ctx
->ptem
= (vsid
<< 12) | ((pgidx
>> 4) & 0x0F80);
972 ctx
->ptem
= (vsid
<< 7) | (pgidx
>> 10);
974 /* Initialize real address with an invalid value */
975 ctx
->raddr
= (target_phys_addr_t
)-1ULL;
976 if (unlikely(env
->mmu_model
== POWERPC_MMU_SOFT_6xx
||
977 env
->mmu_model
== POWERPC_MMU_SOFT_74xx
)) {
978 /* Software TLB search */
979 ret
= ppc6xx_tlb_check(env
, ctx
, eaddr
, rw
, type
);
981 LOG_MMU("0 sdr1=" PADDRX
" vsid=" ADDRX
" "
982 "api=" ADDRX
" hash=" PADDRX
983 " pg_addr=" PADDRX
"\n",
984 sdr
, vsid
, pgidx
, hash
, ctx
->pg_addr
[0]);
985 /* Primary table lookup */
986 ret
= find_pte(env
, ctx
, 0, rw
, type
, target_page_bits
);
988 /* Secondary table lookup */
989 if (eaddr
!= 0xEFFFFFFF)
990 LOG_MMU("1 sdr1=" PADDRX
" vsid=" ADDRX
" "
991 "api=" ADDRX
" hash=" PADDRX
992 " pg_addr=" PADDRX
"\n",
993 sdr
, vsid
, pgidx
, hash
, ctx
->pg_addr
[1]);
994 ret2
= find_pte(env
, ctx
, 1, rw
, type
,
1000 #if defined (DUMP_PAGE_TABLES)
1001 if (qemu_log_enabled()) {
1002 target_phys_addr_t curaddr
;
1003 uint32_t a0
, a1
, a2
, a3
;
1004 qemu_log("Page table: " PADDRX
" len " PADDRX
"\n",
1006 for (curaddr
= sdr
; curaddr
< (sdr
+ mask
+ 0x80);
1008 a0
= ldl_phys(curaddr
);
1009 a1
= ldl_phys(curaddr
+ 4);
1010 a2
= ldl_phys(curaddr
+ 8);
1011 a3
= ldl_phys(curaddr
+ 12);
1012 if (a0
!= 0 || a1
!= 0 || a2
!= 0 || a3
!= 0) {
1013 qemu_log(PADDRX
": %08x %08x %08x %08x\n",
1014 curaddr
, a0
, a1
, a2
, a3
);
1020 LOG_MMU("No access allowed\n");
1024 LOG_MMU("direct store...\n");
1025 /* Direct-store segment : absolutely *BUGGY* for now */
1028 /* Integer load/store : only access allowed */
1031 /* No code fetch is allowed in direct-store areas */
1034 /* Floating point load/store */
1037 /* lwarx, ldarx or srwcx. */
1040 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1041 /* Should make the instruction do no-op.
1042 * As it already do no-op, it's quite easy :-)
1047 /* eciwx or ecowx */
1050 qemu_log("ERROR: instruction should not need "
1051 "address translation\n");
1054 if ((rw
== 1 || ctx
->key
!= 1) && (rw
== 0 || ctx
->key
!= 0)) {
1065 /* Generic TLB check function for embedded PowerPC implementations */
1066 static always_inline
int ppcemb_tlb_check (CPUState
*env
, ppcemb_tlb_t
*tlb
,
1067 target_phys_addr_t
*raddrp
,
1068 target_ulong address
,
1069 uint32_t pid
, int ext
, int i
)
1073 /* Check valid flag */
1074 if (!(tlb
->prot
& PAGE_VALID
)) {
1075 qemu_log("%s: TLB %d not valid\n", __func__
, i
);
1078 mask
= ~(tlb
->size
- 1);
1079 LOG_SWTLB("%s: TLB %d address " ADDRX
" PID %u <=> " ADDRX
1081 __func__
, i
, address
, pid
, tlb
->EPN
, mask
, (uint32_t)tlb
->PID
);
1083 if (tlb
->PID
!= 0 && tlb
->PID
!= pid
)
1085 /* Check effective address */
1086 if ((address
& mask
) != tlb
->EPN
)
1088 *raddrp
= (tlb
->RPN
& mask
) | (address
& ~mask
);
1089 #if (TARGET_PHYS_ADDR_BITS >= 36)
1091 /* Extend the physical address to 36 bits */
1092 *raddrp
|= (target_phys_addr_t
)(tlb
->RPN
& 0xF) << 32;
1099 /* Generic TLB search function for PowerPC embedded implementations */
1100 int ppcemb_tlb_search (CPUPPCState
*env
, target_ulong address
, uint32_t pid
)
1103 target_phys_addr_t raddr
;
1106 /* Default return value is no match */
1108 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1109 tlb
= &env
->tlb
[i
].tlbe
;
1110 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
, pid
, 0, i
) == 0) {
1119 /* Helpers specific to PowerPC 40x implementations */
1120 static always_inline
void ppc4xx_tlb_invalidate_all (CPUState
*env
)
1125 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1126 tlb
= &env
->tlb
[i
].tlbe
;
1127 tlb
->prot
&= ~PAGE_VALID
;
1132 static always_inline
void ppc4xx_tlb_invalidate_virt (CPUState
*env
,
1136 #if !defined(FLUSH_ALL_TLBS)
1138 target_phys_addr_t raddr
;
1139 target_ulong page
, end
;
1142 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1143 tlb
= &env
->tlb
[i
].tlbe
;
1144 if (ppcemb_tlb_check(env
, tlb
, &raddr
, eaddr
, pid
, 0, i
) == 0) {
1145 end
= tlb
->EPN
+ tlb
->size
;
1146 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
1147 tlb_flush_page(env
, page
);
1148 tlb
->prot
&= ~PAGE_VALID
;
1153 ppc4xx_tlb_invalidate_all(env
);
1157 static int mmu40x_get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
,
1158 target_ulong address
, int rw
, int access_type
)
1161 target_phys_addr_t raddr
;
1162 int i
, ret
, zsel
, zpr
, pr
;
1165 raddr
= (target_phys_addr_t
)-1ULL;
1167 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1168 tlb
= &env
->tlb
[i
].tlbe
;
1169 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
1170 env
->spr
[SPR_40x_PID
], 0, i
) < 0)
1172 zsel
= (tlb
->attr
>> 4) & 0xF;
1173 zpr
= (env
->spr
[SPR_40x_ZPR
] >> (28 - (2 * zsel
))) & 0x3;
1174 LOG_SWTLB("%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1175 __func__
, i
, zsel
, zpr
, rw
, tlb
->attr
);
1176 /* Check execute enable bit */
1183 /* All accesses granted */
1184 ctx
->prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1196 /* Check from TLB entry */
1197 /* XXX: there is a problem here or in the TLB fill code... */
1198 ctx
->prot
= tlb
->prot
;
1199 ctx
->prot
|= PAGE_EXEC
;
1200 ret
= check_prot(ctx
->prot
, rw
, access_type
);
1205 LOG_SWTLB("%s: access granted " ADDRX
" => " PADDRX
1206 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
1211 LOG_SWTLB("%s: access refused " ADDRX
" => " PADDRX
1212 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
,
1218 void store_40x_sler (CPUPPCState
*env
, uint32_t val
)
1220 /* XXX: TO BE FIXED */
1221 if (val
!= 0x00000000) {
1222 cpu_abort(env
, "Little-endian regions are not supported by now\n");
1224 env
->spr
[SPR_405_SLER
] = val
;
1227 static int mmubooke_get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
,
1228 target_ulong address
, int rw
,
1232 target_phys_addr_t raddr
;
1236 raddr
= (target_phys_addr_t
)-1ULL;
1237 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1238 tlb
= &env
->tlb
[i
].tlbe
;
1239 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
1240 env
->spr
[SPR_BOOKE_PID
], 1, i
) < 0)
1243 prot
= tlb
->prot
& 0xF;
1245 prot
= (tlb
->prot
>> 4) & 0xF;
1246 /* Check the address space */
1247 if (access_type
== ACCESS_CODE
) {
1248 if (msr_ir
!= (tlb
->attr
& 1))
1251 if (prot
& PAGE_EXEC
) {
1257 if (msr_dr
!= (tlb
->attr
& 1))
1260 if ((!rw
&& prot
& PAGE_READ
) || (rw
&& (prot
& PAGE_WRITE
))) {
1273 static always_inline
int check_physical (CPUState
*env
, mmu_ctx_t
*ctx
,
1274 target_ulong eaddr
, int rw
)
1279 ctx
->prot
= PAGE_READ
| PAGE_EXEC
;
1281 switch (env
->mmu_model
) {
1282 case POWERPC_MMU_32B
:
1283 case POWERPC_MMU_601
:
1284 case POWERPC_MMU_SOFT_6xx
:
1285 case POWERPC_MMU_SOFT_74xx
:
1286 case POWERPC_MMU_SOFT_4xx
:
1287 case POWERPC_MMU_REAL
:
1288 case POWERPC_MMU_BOOKE
:
1289 ctx
->prot
|= PAGE_WRITE
;
1291 #if defined(TARGET_PPC64)
1292 case POWERPC_MMU_620
:
1293 case POWERPC_MMU_64B
:
1294 /* Real address are 60 bits long */
1295 ctx
->raddr
&= 0x0FFFFFFFFFFFFFFFULL
;
1296 ctx
->prot
|= PAGE_WRITE
;
1299 case POWERPC_MMU_SOFT_4xx_Z
:
1300 if (unlikely(msr_pe
!= 0)) {
1301 /* 403 family add some particular protections,
1302 * using PBL/PBU registers for accesses with no translation.
1305 /* Check PLB validity */
1306 (env
->pb
[0] < env
->pb
[1] &&
1307 /* and address in plb area */
1308 eaddr
>= env
->pb
[0] && eaddr
< env
->pb
[1]) ||
1309 (env
->pb
[2] < env
->pb
[3] &&
1310 eaddr
>= env
->pb
[2] && eaddr
< env
->pb
[3]) ? 1 : 0;
1311 if (in_plb
^ msr_px
) {
1312 /* Access in protected area */
1314 /* Access is not allowed */
1318 /* Read-write access is allowed */
1319 ctx
->prot
|= PAGE_WRITE
;
1323 case POWERPC_MMU_MPC8xx
:
1325 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1327 case POWERPC_MMU_BOOKE_FSL
:
1329 cpu_abort(env
, "BookE FSL MMU model not implemented\n");
1332 cpu_abort(env
, "Unknown or invalid MMU model\n");
1339 int get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
, target_ulong eaddr
,
1340 int rw
, int access_type
)
1345 qemu_log("%s\n", __func__
);
1347 if ((access_type
== ACCESS_CODE
&& msr_ir
== 0) ||
1348 (access_type
!= ACCESS_CODE
&& msr_dr
== 0)) {
1349 /* No address translation */
1350 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1353 switch (env
->mmu_model
) {
1354 case POWERPC_MMU_32B
:
1355 case POWERPC_MMU_601
:
1356 case POWERPC_MMU_SOFT_6xx
:
1357 case POWERPC_MMU_SOFT_74xx
:
1358 /* Try to find a BAT */
1359 if (env
->nb_BATs
!= 0)
1360 ret
= get_bat(env
, ctx
, eaddr
, rw
, access_type
);
1361 #if defined(TARGET_PPC64)
1362 case POWERPC_MMU_620
:
1363 case POWERPC_MMU_64B
:
1366 /* We didn't match any BAT entry or don't have BATs */
1367 ret
= get_segment(env
, ctx
, eaddr
, rw
, access_type
);
1370 case POWERPC_MMU_SOFT_4xx
:
1371 case POWERPC_MMU_SOFT_4xx_Z
:
1372 ret
= mmu40x_get_physical_address(env
, ctx
, eaddr
,
1375 case POWERPC_MMU_BOOKE
:
1376 ret
= mmubooke_get_physical_address(env
, ctx
, eaddr
,
1379 case POWERPC_MMU_MPC8xx
:
1381 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1383 case POWERPC_MMU_BOOKE_FSL
:
1385 cpu_abort(env
, "BookE FSL MMU model not implemented\n");
1387 case POWERPC_MMU_REAL
:
1388 cpu_abort(env
, "PowerPC in real mode do not do any translation\n");
1391 cpu_abort(env
, "Unknown or invalid MMU model\n");
1396 qemu_log("%s address " ADDRX
" => %d " PADDRX
"\n",
1397 __func__
, eaddr
, ret
, ctx
->raddr
);
1403 target_phys_addr_t
cpu_get_phys_page_debug (CPUState
*env
, target_ulong addr
)
1407 if (unlikely(get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) != 0))
1410 return ctx
.raddr
& TARGET_PAGE_MASK
;
1413 /* Perform address translation */
1414 int cpu_ppc_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
1415 int mmu_idx
, int is_softmmu
)
1424 access_type
= ACCESS_CODE
;
1427 access_type
= env
->access_type
;
1429 ret
= get_physical_address(env
, &ctx
, address
, rw
, access_type
);
1431 ret
= tlb_set_page_exec(env
, address
& TARGET_PAGE_MASK
,
1432 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
1433 mmu_idx
, is_softmmu
);
1434 } else if (ret
< 0) {
1436 if (access_type
== ACCESS_CODE
) {
1439 /* No matches in page tables or TLB */
1440 switch (env
->mmu_model
) {
1441 case POWERPC_MMU_SOFT_6xx
:
1442 env
->exception_index
= POWERPC_EXCP_IFTLB
;
1443 env
->error_code
= 1 << 18;
1444 env
->spr
[SPR_IMISS
] = address
;
1445 env
->spr
[SPR_ICMP
] = 0x80000000 | ctx
.ptem
;
1447 case POWERPC_MMU_SOFT_74xx
:
1448 env
->exception_index
= POWERPC_EXCP_IFTLB
;
1450 case POWERPC_MMU_SOFT_4xx
:
1451 case POWERPC_MMU_SOFT_4xx_Z
:
1452 env
->exception_index
= POWERPC_EXCP_ITLB
;
1453 env
->error_code
= 0;
1454 env
->spr
[SPR_40x_DEAR
] = address
;
1455 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1457 case POWERPC_MMU_32B
:
1458 case POWERPC_MMU_601
:
1459 #if defined(TARGET_PPC64)
1460 case POWERPC_MMU_620
:
1461 case POWERPC_MMU_64B
:
1463 env
->exception_index
= POWERPC_EXCP_ISI
;
1464 env
->error_code
= 0x40000000;
1466 case POWERPC_MMU_BOOKE
:
1468 cpu_abort(env
, "BookE MMU model is not implemented\n");
1470 case POWERPC_MMU_BOOKE_FSL
:
1472 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
1474 case POWERPC_MMU_MPC8xx
:
1476 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1478 case POWERPC_MMU_REAL
:
1479 cpu_abort(env
, "PowerPC in real mode should never raise "
1480 "any MMU exceptions\n");
1483 cpu_abort(env
, "Unknown or invalid MMU model\n");
1488 /* Access rights violation */
1489 env
->exception_index
= POWERPC_EXCP_ISI
;
1490 env
->error_code
= 0x08000000;
1493 /* No execute protection violation */
1494 env
->exception_index
= POWERPC_EXCP_ISI
;
1495 env
->error_code
= 0x10000000;
1498 /* Direct store exception */
1499 /* No code fetch is allowed in direct-store areas */
1500 env
->exception_index
= POWERPC_EXCP_ISI
;
1501 env
->error_code
= 0x10000000;
1503 #if defined(TARGET_PPC64)
1505 /* No match in segment table */
1506 if (env
->mmu_model
== POWERPC_MMU_620
) {
1507 env
->exception_index
= POWERPC_EXCP_ISI
;
1508 /* XXX: this might be incorrect */
1509 env
->error_code
= 0x40000000;
1511 env
->exception_index
= POWERPC_EXCP_ISEG
;
1512 env
->error_code
= 0;
1520 /* No matches in page tables or TLB */
1521 switch (env
->mmu_model
) {
1522 case POWERPC_MMU_SOFT_6xx
:
1524 env
->exception_index
= POWERPC_EXCP_DSTLB
;
1525 env
->error_code
= 1 << 16;
1527 env
->exception_index
= POWERPC_EXCP_DLTLB
;
1528 env
->error_code
= 0;
1530 env
->spr
[SPR_DMISS
] = address
;
1531 env
->spr
[SPR_DCMP
] = 0x80000000 | ctx
.ptem
;
1533 env
->error_code
|= ctx
.key
<< 19;
1534 env
->spr
[SPR_HASH1
] = ctx
.pg_addr
[0];
1535 env
->spr
[SPR_HASH2
] = ctx
.pg_addr
[1];
1537 case POWERPC_MMU_SOFT_74xx
:
1539 env
->exception_index
= POWERPC_EXCP_DSTLB
;
1541 env
->exception_index
= POWERPC_EXCP_DLTLB
;
1544 /* Implement LRU algorithm */
1545 env
->error_code
= ctx
.key
<< 19;
1546 env
->spr
[SPR_TLBMISS
] = (address
& ~((target_ulong
)0x3)) |
1547 ((env
->last_way
+ 1) & (env
->nb_ways
- 1));
1548 env
->spr
[SPR_PTEHI
] = 0x80000000 | ctx
.ptem
;
1550 case POWERPC_MMU_SOFT_4xx
:
1551 case POWERPC_MMU_SOFT_4xx_Z
:
1552 env
->exception_index
= POWERPC_EXCP_DTLB
;
1553 env
->error_code
= 0;
1554 env
->spr
[SPR_40x_DEAR
] = address
;
1556 env
->spr
[SPR_40x_ESR
] = 0x00800000;
1558 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1560 case POWERPC_MMU_32B
:
1561 case POWERPC_MMU_601
:
1562 #if defined(TARGET_PPC64)
1563 case POWERPC_MMU_620
:
1564 case POWERPC_MMU_64B
:
1566 env
->exception_index
= POWERPC_EXCP_DSI
;
1567 env
->error_code
= 0;
1568 env
->spr
[SPR_DAR
] = address
;
1570 env
->spr
[SPR_DSISR
] = 0x42000000;
1572 env
->spr
[SPR_DSISR
] = 0x40000000;
1574 case POWERPC_MMU_MPC8xx
:
1576 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1578 case POWERPC_MMU_BOOKE
:
1580 cpu_abort(env
, "BookE MMU model is not implemented\n");
1582 case POWERPC_MMU_BOOKE_FSL
:
1584 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
1586 case POWERPC_MMU_REAL
:
1587 cpu_abort(env
, "PowerPC in real mode should never raise "
1588 "any MMU exceptions\n");
1591 cpu_abort(env
, "Unknown or invalid MMU model\n");
1596 /* Access rights violation */
1597 env
->exception_index
= POWERPC_EXCP_DSI
;
1598 env
->error_code
= 0;
1599 env
->spr
[SPR_DAR
] = address
;
1601 env
->spr
[SPR_DSISR
] = 0x0A000000;
1603 env
->spr
[SPR_DSISR
] = 0x08000000;
1606 /* Direct store exception */
1607 switch (access_type
) {
1609 /* Floating point load/store */
1610 env
->exception_index
= POWERPC_EXCP_ALIGN
;
1611 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
1612 env
->spr
[SPR_DAR
] = address
;
1615 /* lwarx, ldarx or stwcx. */
1616 env
->exception_index
= POWERPC_EXCP_DSI
;
1617 env
->error_code
= 0;
1618 env
->spr
[SPR_DAR
] = address
;
1620 env
->spr
[SPR_DSISR
] = 0x06000000;
1622 env
->spr
[SPR_DSISR
] = 0x04000000;
1625 /* eciwx or ecowx */
1626 env
->exception_index
= POWERPC_EXCP_DSI
;
1627 env
->error_code
= 0;
1628 env
->spr
[SPR_DAR
] = address
;
1630 env
->spr
[SPR_DSISR
] = 0x06100000;
1632 env
->spr
[SPR_DSISR
] = 0x04100000;
1635 printf("DSI: invalid exception (%d)\n", ret
);
1636 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
1638 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
;
1639 env
->spr
[SPR_DAR
] = address
;
1643 #if defined(TARGET_PPC64)
1645 /* No match in segment table */
1646 if (env
->mmu_model
== POWERPC_MMU_620
) {
1647 env
->exception_index
= POWERPC_EXCP_DSI
;
1648 env
->error_code
= 0;
1649 env
->spr
[SPR_DAR
] = address
;
1650 /* XXX: this might be incorrect */
1652 env
->spr
[SPR_DSISR
] = 0x42000000;
1654 env
->spr
[SPR_DSISR
] = 0x40000000;
1656 env
->exception_index
= POWERPC_EXCP_DSEG
;
1657 env
->error_code
= 0;
1658 env
->spr
[SPR_DAR
] = address
;
1665 printf("%s: set exception to %d %02x\n", __func__
,
1666 env
->exception
, env
->error_code
);
1674 /*****************************************************************************/
1675 /* BATs management */
1676 #if !defined(FLUSH_ALL_TLBS)
1677 static always_inline
void do_invalidate_BAT (CPUPPCState
*env
,
1681 target_ulong base
, end
, page
;
1683 base
= BATu
& ~0x0001FFFF;
1684 end
= base
+ mask
+ 0x00020000;
1685 LOG_BATS("Flush BAT from " ADDRX
" to " ADDRX
" (" ADDRX
")\n",
1687 for (page
= base
; page
!= end
; page
+= TARGET_PAGE_SIZE
)
1688 tlb_flush_page(env
, page
);
1689 LOG_BATS("Flush done\n");
1693 static always_inline
void dump_store_bat (CPUPPCState
*env
, char ID
,
1694 int ul
, int nr
, target_ulong value
)
1696 LOG_BATS("Set %cBAT%d%c to " ADDRX
" (" ADDRX
")\n",
1697 ID
, nr
, ul
== 0 ? 'u' : 'l', value
, env
->nip
);
1700 void ppc_store_ibatu (CPUPPCState
*env
, int nr
, target_ulong value
)
1704 dump_store_bat(env
, 'I', 0, nr
, value
);
1705 if (env
->IBAT
[0][nr
] != value
) {
1706 mask
= (value
<< 15) & 0x0FFE0000UL
;
1707 #if !defined(FLUSH_ALL_TLBS)
1708 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1710 /* When storing valid upper BAT, mask BEPI and BRPN
1711 * and invalidate all TLBs covered by this BAT
1713 mask
= (value
<< 15) & 0x0FFE0000UL
;
1714 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1715 (value
& ~0x0001FFFFUL
& ~mask
);
1716 env
->IBAT
[1][nr
] = (env
->IBAT
[1][nr
] & 0x0000007B) |
1717 (env
->IBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1718 #if !defined(FLUSH_ALL_TLBS)
1719 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1726 void ppc_store_ibatl (CPUPPCState
*env
, int nr
, target_ulong value
)
1728 dump_store_bat(env
, 'I', 1, nr
, value
);
1729 env
->IBAT
[1][nr
] = value
;
1732 void ppc_store_dbatu (CPUPPCState
*env
, int nr
, target_ulong value
)
1736 dump_store_bat(env
, 'D', 0, nr
, value
);
1737 if (env
->DBAT
[0][nr
] != value
) {
1738 /* When storing valid upper BAT, mask BEPI and BRPN
1739 * and invalidate all TLBs covered by this BAT
1741 mask
= (value
<< 15) & 0x0FFE0000UL
;
1742 #if !defined(FLUSH_ALL_TLBS)
1743 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1745 mask
= (value
<< 15) & 0x0FFE0000UL
;
1746 env
->DBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1747 (value
& ~0x0001FFFFUL
& ~mask
);
1748 env
->DBAT
[1][nr
] = (env
->DBAT
[1][nr
] & 0x0000007B) |
1749 (env
->DBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1750 #if !defined(FLUSH_ALL_TLBS)
1751 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1758 void ppc_store_dbatl (CPUPPCState
*env
, int nr
, target_ulong value
)
1760 dump_store_bat(env
, 'D', 1, nr
, value
);
1761 env
->DBAT
[1][nr
] = value
;
1764 void ppc_store_ibatu_601 (CPUPPCState
*env
, int nr
, target_ulong value
)
1769 dump_store_bat(env
, 'I', 0, nr
, value
);
1770 if (env
->IBAT
[0][nr
] != value
) {
1772 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1773 if (env
->IBAT
[1][nr
] & 0x40) {
1774 /* Invalidate BAT only if it is valid */
1775 #if !defined(FLUSH_ALL_TLBS)
1776 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1781 /* When storing valid upper BAT, mask BEPI and BRPN
1782 * and invalidate all TLBs covered by this BAT
1784 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1785 (value
& ~0x0001FFFFUL
& ~mask
);
1786 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1787 if (env
->IBAT
[1][nr
] & 0x40) {
1788 #if !defined(FLUSH_ALL_TLBS)
1789 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1794 #if defined(FLUSH_ALL_TLBS)
1801 void ppc_store_ibatl_601 (CPUPPCState
*env
, int nr
, target_ulong value
)
1806 dump_store_bat(env
, 'I', 1, nr
, value
);
1807 if (env
->IBAT
[1][nr
] != value
) {
1809 if (env
->IBAT
[1][nr
] & 0x40) {
1810 #if !defined(FLUSH_ALL_TLBS)
1811 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1812 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1818 #if !defined(FLUSH_ALL_TLBS)
1819 mask
= (value
<< 17) & 0x0FFE0000UL
;
1820 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1825 env
->IBAT
[1][nr
] = value
;
1826 env
->DBAT
[1][nr
] = value
;
1827 #if defined(FLUSH_ALL_TLBS)
1834 /*****************************************************************************/
1835 /* TLB management */
1836 void ppc_tlb_invalidate_all (CPUPPCState
*env
)
1838 switch (env
->mmu_model
) {
1839 case POWERPC_MMU_SOFT_6xx
:
1840 case POWERPC_MMU_SOFT_74xx
:
1841 ppc6xx_tlb_invalidate_all(env
);
1843 case POWERPC_MMU_SOFT_4xx
:
1844 case POWERPC_MMU_SOFT_4xx_Z
:
1845 ppc4xx_tlb_invalidate_all(env
);
1847 case POWERPC_MMU_REAL
:
1848 cpu_abort(env
, "No TLB for PowerPC 4xx in real mode\n");
1850 case POWERPC_MMU_MPC8xx
:
1852 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1854 case POWERPC_MMU_BOOKE
:
1856 cpu_abort(env
, "BookE MMU model is not implemented\n");
1858 case POWERPC_MMU_BOOKE_FSL
:
1861 cpu_abort(env
, "BookE MMU model is not implemented\n");
1863 case POWERPC_MMU_32B
:
1864 case POWERPC_MMU_601
:
1865 #if defined(TARGET_PPC64)
1866 case POWERPC_MMU_620
:
1867 case POWERPC_MMU_64B
:
1868 #endif /* defined(TARGET_PPC64) */
1873 cpu_abort(env
, "Unknown MMU model\n");
1878 void ppc_tlb_invalidate_one (CPUPPCState
*env
, target_ulong addr
)
1880 #if !defined(FLUSH_ALL_TLBS)
1881 addr
&= TARGET_PAGE_MASK
;
1882 switch (env
->mmu_model
) {
1883 case POWERPC_MMU_SOFT_6xx
:
1884 case POWERPC_MMU_SOFT_74xx
:
1885 ppc6xx_tlb_invalidate_virt(env
, addr
, 0);
1886 if (env
->id_tlbs
== 1)
1887 ppc6xx_tlb_invalidate_virt(env
, addr
, 1);
1889 case POWERPC_MMU_SOFT_4xx
:
1890 case POWERPC_MMU_SOFT_4xx_Z
:
1891 ppc4xx_tlb_invalidate_virt(env
, addr
, env
->spr
[SPR_40x_PID
]);
1893 case POWERPC_MMU_REAL
:
1894 cpu_abort(env
, "No TLB for PowerPC 4xx in real mode\n");
1896 case POWERPC_MMU_MPC8xx
:
1898 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1900 case POWERPC_MMU_BOOKE
:
1902 cpu_abort(env
, "BookE MMU model is not implemented\n");
1904 case POWERPC_MMU_BOOKE_FSL
:
1906 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
1908 case POWERPC_MMU_32B
:
1909 case POWERPC_MMU_601
:
1910 /* tlbie invalidate TLBs for all segments */
1911 addr
&= ~((target_ulong
)-1ULL << 28);
1912 /* XXX: this case should be optimized,
1913 * giving a mask to tlb_flush_page
1915 tlb_flush_page(env
, addr
| (0x0 << 28));
1916 tlb_flush_page(env
, addr
| (0x1 << 28));
1917 tlb_flush_page(env
, addr
| (0x2 << 28));
1918 tlb_flush_page(env
, addr
| (0x3 << 28));
1919 tlb_flush_page(env
, addr
| (0x4 << 28));
1920 tlb_flush_page(env
, addr
| (0x5 << 28));
1921 tlb_flush_page(env
, addr
| (0x6 << 28));
1922 tlb_flush_page(env
, addr
| (0x7 << 28));
1923 tlb_flush_page(env
, addr
| (0x8 << 28));
1924 tlb_flush_page(env
, addr
| (0x9 << 28));
1925 tlb_flush_page(env
, addr
| (0xA << 28));
1926 tlb_flush_page(env
, addr
| (0xB << 28));
1927 tlb_flush_page(env
, addr
| (0xC << 28));
1928 tlb_flush_page(env
, addr
| (0xD << 28));
1929 tlb_flush_page(env
, addr
| (0xE << 28));
1930 tlb_flush_page(env
, addr
| (0xF << 28));
1932 #if defined(TARGET_PPC64)
1933 case POWERPC_MMU_620
:
1934 case POWERPC_MMU_64B
:
1935 /* tlbie invalidate TLBs for all segments */
1936 /* XXX: given the fact that there are too many segments to invalidate,
1937 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
1938 * we just invalidate all TLBs
1942 #endif /* defined(TARGET_PPC64) */
1945 cpu_abort(env
, "Unknown MMU model\n");
1949 ppc_tlb_invalidate_all(env
);
1953 /*****************************************************************************/
1954 /* Special registers manipulation */
1955 #if defined(TARGET_PPC64)
1956 void ppc_store_asr (CPUPPCState
*env
, target_ulong value
)
1958 if (env
->asr
!= value
) {
1965 void ppc_store_sdr1 (CPUPPCState
*env
, target_ulong value
)
1967 LOG_MMU("%s: " ADDRX
"\n", __func__
, value
);
1968 if (env
->sdr1
!= value
) {
1969 /* XXX: for PowerPC 64, should check that the HTABSIZE value
1977 #if defined(TARGET_PPC64)
1978 target_ulong
ppc_load_sr (CPUPPCState
*env
, int slb_nr
)
1985 void ppc_store_sr (CPUPPCState
*env
, int srnum
, target_ulong value
)
1987 LOG_MMU("%s: reg=%d " ADDRX
" " ADDRX
"\n",
1988 __func__
, srnum
, value
, env
->sr
[srnum
]);
1989 #if defined(TARGET_PPC64)
1990 if (env
->mmu_model
& POWERPC_MMU_64
) {
1991 uint64_t rb
= 0, rs
= 0;
1994 rb
|= ((uint32_t)srnum
& 0xf) << 28;
1995 /* Set the valid bit */
1998 rb
|= (uint32_t)srnum
;
2001 rs
|= (value
& 0xfffffff) << 12;
2003 rs
|= ((value
>> 27) & 0xf) << 9;
2005 ppc_store_slb(env
, rb
, rs
);
2008 if (env
->sr
[srnum
] != value
) {
2009 env
->sr
[srnum
] = value
;
2010 /* Invalidating 256MB of virtual memory in 4kB pages is way longer than
2011 flusing the whole TLB. */
2012 #if !defined(FLUSH_ALL_TLBS) && 0
2014 target_ulong page
, end
;
2015 /* Invalidate 256 MB of virtual memory */
2016 page
= (16 << 20) * srnum
;
2017 end
= page
+ (16 << 20);
2018 for (; page
!= end
; page
+= TARGET_PAGE_SIZE
)
2019 tlb_flush_page(env
, page
);
2026 #endif /* !defined (CONFIG_USER_ONLY) */
2028 /* GDBstub can read and write MSR... */
2029 void ppc_store_msr (CPUPPCState
*env
, target_ulong value
)
2031 hreg_store_msr(env
, value
, 0);
2034 /*****************************************************************************/
2035 /* Exception processing */
2036 #if defined (CONFIG_USER_ONLY)
2037 void do_interrupt (CPUState
*env
)
2039 env
->exception_index
= POWERPC_EXCP_NONE
;
2040 env
->error_code
= 0;
2043 void ppc_hw_interrupt (CPUState
*env
)
2045 env
->exception_index
= POWERPC_EXCP_NONE
;
2046 env
->error_code
= 0;
2048 #else /* defined (CONFIG_USER_ONLY) */
2049 static always_inline
void dump_syscall (CPUState
*env
)
2051 qemu_log_mask(CPU_LOG_INT
, "syscall r0=" REGX
" r3=" REGX
" r4=" REGX
2052 " r5=" REGX
" r6=" REGX
" nip=" ADDRX
"\n",
2053 ppc_dump_gpr(env
, 0), ppc_dump_gpr(env
, 3), ppc_dump_gpr(env
, 4),
2054 ppc_dump_gpr(env
, 5), ppc_dump_gpr(env
, 6), env
->nip
);
2057 /* Note that this function should be greatly optimized
2058 * when called with a constant excp, from ppc_hw_interrupt
2060 static always_inline
void powerpc_excp (CPUState
*env
,
2061 int excp_model
, int excp
)
2063 target_ulong msr
, new_msr
, vector
;
2064 int srr0
, srr1
, asrr0
, asrr1
;
2065 int lpes0
, lpes1
, lev
;
2068 /* XXX: find a suitable condition to enable the hypervisor mode */
2069 lpes0
= (env
->spr
[SPR_LPCR
] >> 1) & 1;
2070 lpes1
= (env
->spr
[SPR_LPCR
] >> 2) & 1;
2072 /* Those values ensure we won't enter the hypervisor mode */
2077 qemu_log_mask(CPU_LOG_INT
, "Raise exception at " ADDRX
" => %08x (%02x)\n",
2078 env
->nip
, excp
, env
->error_code
);
2085 msr
&= ~((target_ulong
)0x783F0000);
2087 case POWERPC_EXCP_NONE
:
2088 /* Should never happen */
2090 case POWERPC_EXCP_CRITICAL
: /* Critical input */
2091 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2092 switch (excp_model
) {
2093 case POWERPC_EXCP_40x
:
2094 srr0
= SPR_40x_SRR2
;
2095 srr1
= SPR_40x_SRR3
;
2097 case POWERPC_EXCP_BOOKE
:
2098 srr0
= SPR_BOOKE_CSRR0
;
2099 srr1
= SPR_BOOKE_CSRR1
;
2101 case POWERPC_EXCP_G2
:
2107 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
2109 /* Machine check exception is not enabled.
2110 * Enter checkstop state.
2112 if (qemu_log_enabled()) {
2113 qemu_log("Machine check while not allowed. "
2114 "Entering checkstop state\n");
2116 fprintf(stderr
, "Machine check while not allowed. "
2117 "Entering checkstop state\n");
2120 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
2122 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2123 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
2125 /* XXX: find a suitable condition to enable the hypervisor mode */
2126 new_msr
|= (target_ulong
)MSR_HVB
;
2128 /* XXX: should also have something loaded in DAR / DSISR */
2129 switch (excp_model
) {
2130 case POWERPC_EXCP_40x
:
2131 srr0
= SPR_40x_SRR2
;
2132 srr1
= SPR_40x_SRR3
;
2134 case POWERPC_EXCP_BOOKE
:
2135 srr0
= SPR_BOOKE_MCSRR0
;
2136 srr1
= SPR_BOOKE_MCSRR1
;
2137 asrr0
= SPR_BOOKE_CSRR0
;
2138 asrr1
= SPR_BOOKE_CSRR1
;
2144 case POWERPC_EXCP_DSI
: /* Data storage exception */
2145 LOG_EXCP("DSI exception: DSISR=" ADDRX
" DAR=" ADDRX
"\n",
2146 env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
2147 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2149 new_msr
|= (target_ulong
)MSR_HVB
;
2151 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
2152 LOG_EXCP("ISI exception: msr=" ADDRX
", nip=" ADDRX
"\n",
2154 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2156 new_msr
|= (target_ulong
)MSR_HVB
;
2157 msr
|= env
->error_code
;
2159 case POWERPC_EXCP_EXTERNAL
: /* External input */
2160 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2162 new_msr
|= (target_ulong
)MSR_HVB
;
2164 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
2165 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2167 new_msr
|= (target_ulong
)MSR_HVB
;
2168 /* XXX: this is false */
2169 /* Get rS/rD and rA from faulting opcode */
2170 env
->spr
[SPR_DSISR
] |= (ldl_code((env
->nip
- 4)) & 0x03FF0000) >> 16;
2172 case POWERPC_EXCP_PROGRAM
: /* Program exception */
2173 switch (env
->error_code
& ~0xF) {
2174 case POWERPC_EXCP_FP
:
2175 if ((msr_fe0
== 0 && msr_fe1
== 0) || msr_fp
== 0) {
2176 LOG_EXCP("Ignore floating point exception\n");
2177 env
->exception_index
= POWERPC_EXCP_NONE
;
2178 env
->error_code
= 0;
2181 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2183 new_msr
|= (target_ulong
)MSR_HVB
;
2185 if (msr_fe0
== msr_fe1
)
2189 case POWERPC_EXCP_INVAL
:
2190 LOG_EXCP("Invalid instruction at " ADDRX
"\n",
2192 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2194 new_msr
|= (target_ulong
)MSR_HVB
;
2197 case POWERPC_EXCP_PRIV
:
2198 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2200 new_msr
|= (target_ulong
)MSR_HVB
;
2203 case POWERPC_EXCP_TRAP
:
2204 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2206 new_msr
|= (target_ulong
)MSR_HVB
;
2210 /* Should never occur */
2211 cpu_abort(env
, "Invalid program exception %d. Aborting\n",
2216 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
2217 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2219 new_msr
|= (target_ulong
)MSR_HVB
;
2221 case POWERPC_EXCP_SYSCALL
: /* System call exception */
2222 /* NOTE: this is a temporary hack to support graphics OSI
2223 calls from the MOL driver */
2224 /* XXX: To be removed */
2225 if (env
->gpr
[3] == 0x113724fa && env
->gpr
[4] == 0x77810f9b &&
2227 if (env
->osi_call(env
) != 0) {
2228 env
->exception_index
= POWERPC_EXCP_NONE
;
2229 env
->error_code
= 0;
2234 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2235 lev
= env
->error_code
;
2236 if (lev
== 1 || (lpes0
== 0 && lpes1
== 0))
2237 new_msr
|= (target_ulong
)MSR_HVB
;
2239 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
2240 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2242 case POWERPC_EXCP_DECR
: /* Decrementer exception */
2243 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2245 new_msr
|= (target_ulong
)MSR_HVB
;
2247 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
2249 LOG_EXCP("FIT exception\n");
2250 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2252 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
2253 LOG_EXCP("WDT exception\n");
2254 switch (excp_model
) {
2255 case POWERPC_EXCP_BOOKE
:
2256 srr0
= SPR_BOOKE_CSRR0
;
2257 srr1
= SPR_BOOKE_CSRR1
;
2262 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2264 case POWERPC_EXCP_DTLB
: /* Data TLB error */
2265 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2267 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
2268 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2270 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
2271 switch (excp_model
) {
2272 case POWERPC_EXCP_BOOKE
:
2273 srr0
= SPR_BOOKE_DSRR0
;
2274 srr1
= SPR_BOOKE_DSRR1
;
2275 asrr0
= SPR_BOOKE_CSRR0
;
2276 asrr1
= SPR_BOOKE_CSRR1
;
2282 cpu_abort(env
, "Debug exception is not implemented yet !\n");
2284 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavailable */
2285 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2287 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data interrupt */
2289 cpu_abort(env
, "Embedded floating point data exception "
2290 "is not implemented yet !\n");
2292 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round interrupt */
2294 cpu_abort(env
, "Embedded floating point round exception "
2295 "is not implemented yet !\n");
2297 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor interrupt */
2298 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2301 "Performance counter exception is not implemented yet !\n");
2303 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
2306 "Embedded doorbell interrupt is not implemented yet !\n");
2308 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
2309 switch (excp_model
) {
2310 case POWERPC_EXCP_BOOKE
:
2311 srr0
= SPR_BOOKE_CSRR0
;
2312 srr1
= SPR_BOOKE_CSRR1
;
2318 cpu_abort(env
, "Embedded doorbell critical interrupt "
2319 "is not implemented yet !\n");
2321 case POWERPC_EXCP_RESET
: /* System reset exception */
2322 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2324 /* XXX: find a suitable condition to enable the hypervisor mode */
2325 new_msr
|= (target_ulong
)MSR_HVB
;
2328 case POWERPC_EXCP_DSEG
: /* Data segment exception */
2329 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2331 new_msr
|= (target_ulong
)MSR_HVB
;
2333 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
2334 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2336 new_msr
|= (target_ulong
)MSR_HVB
;
2338 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
2341 new_msr
|= (target_ulong
)MSR_HVB
;
2343 case POWERPC_EXCP_TRACE
: /* Trace exception */
2344 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2346 new_msr
|= (target_ulong
)MSR_HVB
;
2348 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
2351 new_msr
|= (target_ulong
)MSR_HVB
;
2353 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage exception */
2356 new_msr
|= (target_ulong
)MSR_HVB
;
2358 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
2361 new_msr
|= (target_ulong
)MSR_HVB
;
2363 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment exception */
2366 new_msr
|= (target_ulong
)MSR_HVB
;
2368 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
2369 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2371 new_msr
|= (target_ulong
)MSR_HVB
;
2373 case POWERPC_EXCP_PIT
: /* Programmable interval timer interrupt */
2374 LOG_EXCP("PIT exception\n");
2375 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2377 case POWERPC_EXCP_IO
: /* IO error exception */
2379 cpu_abort(env
, "601 IO error exception is not implemented yet !\n");
2381 case POWERPC_EXCP_RUNM
: /* Run mode exception */
2383 cpu_abort(env
, "601 run mode exception is not implemented yet !\n");
2385 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
2387 cpu_abort(env
, "602 emulation trap exception "
2388 "is not implemented yet !\n");
2390 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
2391 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2392 if (lpes1
== 0) /* XXX: check this */
2393 new_msr
|= (target_ulong
)MSR_HVB
;
2394 switch (excp_model
) {
2395 case POWERPC_EXCP_602
:
2396 case POWERPC_EXCP_603
:
2397 case POWERPC_EXCP_603E
:
2398 case POWERPC_EXCP_G2
:
2400 case POWERPC_EXCP_7x5
:
2402 case POWERPC_EXCP_74xx
:
2405 cpu_abort(env
, "Invalid instruction TLB miss exception\n");
2409 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
2410 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2411 if (lpes1
== 0) /* XXX: check this */
2412 new_msr
|= (target_ulong
)MSR_HVB
;
2413 switch (excp_model
) {
2414 case POWERPC_EXCP_602
:
2415 case POWERPC_EXCP_603
:
2416 case POWERPC_EXCP_603E
:
2417 case POWERPC_EXCP_G2
:
2419 case POWERPC_EXCP_7x5
:
2421 case POWERPC_EXCP_74xx
:
2424 cpu_abort(env
, "Invalid data load TLB miss exception\n");
2428 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
2429 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2430 if (lpes1
== 0) /* XXX: check this */
2431 new_msr
|= (target_ulong
)MSR_HVB
;
2432 switch (excp_model
) {
2433 case POWERPC_EXCP_602
:
2434 case POWERPC_EXCP_603
:
2435 case POWERPC_EXCP_603E
:
2436 case POWERPC_EXCP_G2
:
2438 /* Swap temporary saved registers with GPRs */
2439 if (!(new_msr
& ((target_ulong
)1 << MSR_TGPR
))) {
2440 new_msr
|= (target_ulong
)1 << MSR_TGPR
;
2441 hreg_swap_gpr_tgpr(env
);
2444 case POWERPC_EXCP_7x5
:
2446 #if defined (DEBUG_SOFTWARE_TLB)
2447 if (qemu_log_enabled()) {
2449 target_ulong
*miss
, *cmp
;
2451 if (excp
== POWERPC_EXCP_IFTLB
) {
2454 miss
= &env
->spr
[SPR_IMISS
];
2455 cmp
= &env
->spr
[SPR_ICMP
];
2457 if (excp
== POWERPC_EXCP_DLTLB
)
2462 miss
= &env
->spr
[SPR_DMISS
];
2463 cmp
= &env
->spr
[SPR_DCMP
];
2465 qemu_log("6xx %sTLB miss: %cM " ADDRX
" %cC " ADDRX
2466 " H1 " ADDRX
" H2 " ADDRX
" %08x\n",
2467 es
, en
, *miss
, en
, *cmp
,
2468 env
->spr
[SPR_HASH1
], env
->spr
[SPR_HASH2
],
2472 msr
|= env
->crf
[0] << 28;
2473 msr
|= env
->error_code
; /* key, D/I, S/L bits */
2474 /* Set way using a LRU mechanism */
2475 msr
|= ((env
->last_way
+ 1) & (env
->nb_ways
- 1)) << 17;
2477 case POWERPC_EXCP_74xx
:
2479 #if defined (DEBUG_SOFTWARE_TLB)
2480 if (qemu_log_enabled()) {
2482 target_ulong
*miss
, *cmp
;
2484 if (excp
== POWERPC_EXCP_IFTLB
) {
2487 miss
= &env
->spr
[SPR_TLBMISS
];
2488 cmp
= &env
->spr
[SPR_PTEHI
];
2490 if (excp
== POWERPC_EXCP_DLTLB
)
2495 miss
= &env
->spr
[SPR_TLBMISS
];
2496 cmp
= &env
->spr
[SPR_PTEHI
];
2498 qemu_log("74xx %sTLB miss: %cM " ADDRX
" %cC " ADDRX
2500 es
, en
, *miss
, en
, *cmp
, env
->error_code
);
2503 msr
|= env
->error_code
; /* key bit */
2506 cpu_abort(env
, "Invalid data store TLB miss exception\n");
2510 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
2512 cpu_abort(env
, "Floating point assist exception "
2513 "is not implemented yet !\n");
2515 case POWERPC_EXCP_DABR
: /* Data address breakpoint */
2517 cpu_abort(env
, "DABR exception is not implemented yet !\n");
2519 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
2521 cpu_abort(env
, "IABR exception is not implemented yet !\n");
2523 case POWERPC_EXCP_SMI
: /* System management interrupt */
2525 cpu_abort(env
, "SMI exception is not implemented yet !\n");
2527 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
2529 cpu_abort(env
, "Thermal management exception "
2530 "is not implemented yet !\n");
2532 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor interrupt */
2533 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2535 new_msr
|= (target_ulong
)MSR_HVB
;
2538 "Performance counter exception is not implemented yet !\n");
2540 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
2542 cpu_abort(env
, "VPU assist exception is not implemented yet !\n");
2544 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
2547 "970 soft-patch exception is not implemented yet !\n");
2549 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
2552 "970 maintenance exception is not implemented yet !\n");
2554 case POWERPC_EXCP_MEXTBR
: /* Maskable external breakpoint */
2556 cpu_abort(env
, "Maskable external exception "
2557 "is not implemented yet !\n");
2559 case POWERPC_EXCP_NMEXTBR
: /* Non maskable external breakpoint */
2561 cpu_abort(env
, "Non maskable external exception "
2562 "is not implemented yet !\n");
2566 cpu_abort(env
, "Invalid PowerPC exception %d. Aborting\n", excp
);
2569 /* save current instruction location */
2570 env
->spr
[srr0
] = env
->nip
- 4;
2573 /* save next instruction location */
2574 env
->spr
[srr0
] = env
->nip
;
2578 env
->spr
[srr1
] = msr
;
2579 /* If any alternate SRR register are defined, duplicate saved values */
2581 env
->spr
[asrr0
] = env
->spr
[srr0
];
2583 env
->spr
[asrr1
] = env
->spr
[srr1
];
2584 /* If we disactivated any translation, flush TLBs */
2585 if (new_msr
& ((1 << MSR_IR
) | (1 << MSR_DR
)))
2587 /* reload MSR with correct bits */
2588 new_msr
&= ~((target_ulong
)1 << MSR_EE
);
2589 new_msr
&= ~((target_ulong
)1 << MSR_PR
);
2590 new_msr
&= ~((target_ulong
)1 << MSR_FP
);
2591 new_msr
&= ~((target_ulong
)1 << MSR_FE0
);
2592 new_msr
&= ~((target_ulong
)1 << MSR_SE
);
2593 new_msr
&= ~((target_ulong
)1 << MSR_BE
);
2594 new_msr
&= ~((target_ulong
)1 << MSR_FE1
);
2595 new_msr
&= ~((target_ulong
)1 << MSR_IR
);
2596 new_msr
&= ~((target_ulong
)1 << MSR_DR
);
2597 #if 0 /* Fix this: not on all targets */
2598 new_msr
&= ~((target_ulong
)1 << MSR_PMM
);
2600 new_msr
&= ~((target_ulong
)1 << MSR_LE
);
2602 new_msr
|= (target_ulong
)1 << MSR_LE
;
2604 new_msr
&= ~((target_ulong
)1 << MSR_LE
);
2605 /* Jump to handler */
2606 vector
= env
->excp_vectors
[excp
];
2607 if (vector
== (target_ulong
)-1ULL) {
2608 cpu_abort(env
, "Raised an exception without defined vector %d\n",
2611 vector
|= env
->excp_prefix
;
2612 #if defined(TARGET_PPC64)
2613 if (excp_model
== POWERPC_EXCP_BOOKE
) {
2615 new_msr
&= ~((target_ulong
)1 << MSR_CM
);
2616 vector
= (uint32_t)vector
;
2618 new_msr
|= (target_ulong
)1 << MSR_CM
;
2621 if (!msr_isf
&& !(env
->mmu_model
& POWERPC_MMU_64
)) {
2622 new_msr
&= ~((target_ulong
)1 << MSR_SF
);
2623 vector
= (uint32_t)vector
;
2625 new_msr
|= (target_ulong
)1 << MSR_SF
;
2629 /* XXX: we don't use hreg_store_msr here as already have treated
2630 * any special case that could occur. Just store MSR and update hflags
2632 env
->msr
= new_msr
& env
->msr_mask
;
2633 hreg_compute_hflags(env
);
2635 /* Reset exception state */
2636 env
->exception_index
= POWERPC_EXCP_NONE
;
2637 env
->error_code
= 0;
2640 void do_interrupt (CPUState
*env
)
2642 powerpc_excp(env
, env
->excp_model
, env
->exception_index
);
2645 void ppc_hw_interrupt (CPUPPCState
*env
)
2650 qemu_log_mask(CPU_LOG_INT
, "%s: %p pending %08x req %08x me %d ee %d\n",
2651 __func__
, env
, env
->pending_interrupts
,
2652 env
->interrupt_request
, (int)msr_me
, (int)msr_ee
);
2654 /* External reset */
2655 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_RESET
)) {
2656 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_RESET
);
2657 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_RESET
);
2660 /* Machine check exception */
2661 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_MCK
)) {
2662 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_MCK
);
2663 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_MCHECK
);
2667 /* External debug exception */
2668 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DEBUG
)) {
2669 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DEBUG
);
2670 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DEBUG
);
2675 /* XXX: find a suitable condition to enable the hypervisor mode */
2676 hdice
= env
->spr
[SPR_LPCR
] & 1;
2680 if ((msr_ee
!= 0 || msr_hv
== 0 || msr_pr
!= 0) && hdice
!= 0) {
2681 /* Hypervisor decrementer exception */
2682 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_HDECR
)) {
2683 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_HDECR
);
2684 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_HDECR
);
2689 /* External critical interrupt */
2690 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CEXT
)) {
2691 /* Taking a critical external interrupt does not clear the external
2692 * critical interrupt status
2695 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CEXT
);
2697 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_CRITICAL
);
2702 /* Watchdog timer on embedded PowerPC */
2703 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_WDT
)) {
2704 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_WDT
);
2705 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_WDT
);
2708 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CDOORBELL
)) {
2709 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CDOORBELL
);
2710 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DOORCI
);
2713 /* Fixed interval timer on embedded PowerPC */
2714 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_FIT
)) {
2715 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_FIT
);
2716 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_FIT
);
2719 /* Programmable interval timer on embedded PowerPC */
2720 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PIT
)) {
2721 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PIT
);
2722 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_PIT
);
2725 /* Decrementer exception */
2726 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DECR
)) {
2727 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DECR
);
2728 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DECR
);
2731 /* External interrupt */
2732 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_EXT
)) {
2733 /* Taking an external interrupt does not clear the external
2737 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_EXT
);
2739 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_EXTERNAL
);
2742 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DOORBELL
)) {
2743 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DOORBELL
);
2744 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DOORI
);
2747 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PERFM
)) {
2748 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PERFM
);
2749 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_PERFM
);
2752 /* Thermal interrupt */
2753 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_THERM
)) {
2754 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_THERM
);
2755 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_THERM
);
2760 #endif /* !CONFIG_USER_ONLY */
2762 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
)
2764 qemu_log("Return from exception at " ADDRX
" with flags " ADDRX
"\n",
2768 void cpu_ppc_reset (void *opaque
)
2770 CPUPPCState
*env
= opaque
;
2773 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
2774 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
2775 log_cpu_state(env
, 0);
2778 msr
= (target_ulong
)0;
2780 /* XXX: find a suitable condition to enable the hypervisor mode */
2781 msr
|= (target_ulong
)MSR_HVB
;
2783 msr
|= (target_ulong
)0 << MSR_AP
; /* TO BE CHECKED */
2784 msr
|= (target_ulong
)0 << MSR_SA
; /* TO BE CHECKED */
2785 msr
|= (target_ulong
)1 << MSR_EP
;
2786 #if defined (DO_SINGLE_STEP) && 0
2787 /* Single step trace mode */
2788 msr
|= (target_ulong
)1 << MSR_SE
;
2789 msr
|= (target_ulong
)1 << MSR_BE
;
2791 #if defined(CONFIG_USER_ONLY)
2792 msr
|= (target_ulong
)1 << MSR_FP
; /* Allow floating point usage */
2793 msr
|= (target_ulong
)1 << MSR_VR
; /* Allow altivec usage */
2794 msr
|= (target_ulong
)1 << MSR_SPE
; /* Allow SPE usage */
2795 msr
|= (target_ulong
)1 << MSR_PR
;
2797 env
->excp_prefix
= env
->hreset_excp_prefix
;
2798 env
->nip
= env
->hreset_vector
| env
->excp_prefix
;
2799 if (env
->mmu_model
!= POWERPC_MMU_REAL
)
2800 ppc_tlb_invalidate_all(env
);
2802 env
->msr
= msr
& env
->msr_mask
;
2803 #if defined(TARGET_PPC64)
2804 if (env
->mmu_model
& POWERPC_MMU_64
)
2805 env
->msr
|= (1ULL << MSR_SF
);
2807 hreg_compute_hflags(env
);
2808 env
->reserve
= (target_ulong
)-1ULL;
2809 /* Be sure no exception or interrupt is pending */
2810 env
->pending_interrupts
= 0;
2811 env
->exception_index
= POWERPC_EXCP_NONE
;
2812 env
->error_code
= 0;
2813 /* Flush all TLBs */
2817 CPUPPCState
*cpu_ppc_init (const char *cpu_model
)
2820 const ppc_def_t
*def
;
2822 def
= cpu_ppc_find_by_name(cpu_model
);
2826 env
= qemu_mallocz(sizeof(CPUPPCState
));
2828 ppc_translate_init();
2829 env
->cpu_model_str
= cpu_model
;
2830 cpu_ppc_register_internal(env
, def
);
2833 qemu_init_vcpu(env
);
2838 void cpu_ppc_close (CPUPPCState
*env
)
2840 /* Should also remove all opcode tables... */