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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
30 #include "helper_regs.h"
31 #include "qemu-common.h"
38 //#define DEBUG_SOFTWARE_TLB
39 //#define DUMP_PAGE_TABLES
40 //#define DEBUG_EXCEPTIONS
41 //#define FLUSH_ALL_TLBS
44 # define LOG_MMU(...) qemu_log(__VA_ARGS__)
45 # define LOG_MMU_STATE(env) log_cpu_state((env), 0)
47 # define LOG_MMU(...) do { } while (0)
48 # define LOG_MMU_STATE(...) do { } while (0)
52 #ifdef DEBUG_SOFTWARE_TLB
53 # define LOG_SWTLB(...) qemu_log(__VA_ARGS__)
55 # define LOG_SWTLB(...) do { } while (0)
59 # define LOG_BATS(...) qemu_log(__VA_ARGS__)
61 # define LOG_BATS(...) do { } while (0)
65 # define LOG_SLB(...) qemu_log(__VA_ARGS__)
67 # define LOG_SLB(...) do { } while (0)
70 #ifdef DEBUG_EXCEPTIONS
71 # define LOG_EXCP(...) qemu_log(__VA_ARGS__)
73 # define LOG_EXCP(...) do { } while (0)
77 /*****************************************************************************/
78 /* PowerPC MMU emulation */
80 #if defined(CONFIG_USER_ONLY)
81 int cpu_ppc_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
82 int mmu_idx
, int is_softmmu
)
84 int exception
, error_code
;
87 exception
= POWERPC_EXCP_ISI
;
88 error_code
= 0x40000000;
90 exception
= POWERPC_EXCP_DSI
;
91 error_code
= 0x40000000;
93 error_code
|= 0x02000000;
94 env
->spr
[SPR_DAR
] = address
;
95 env
->spr
[SPR_DSISR
] = error_code
;
97 env
->exception_index
= exception
;
98 env
->error_code
= error_code
;
103 target_phys_addr_t
cpu_get_phys_page_debug (CPUState
*env
, target_ulong addr
)
109 /* Common routines used by software and hardware TLBs emulation */
110 static always_inline
int pte_is_valid (target_ulong pte0
)
112 return pte0
& 0x80000000 ? 1 : 0;
115 static always_inline
void pte_invalidate (target_ulong
*pte0
)
117 *pte0
&= ~0x80000000;
120 #if defined(TARGET_PPC64)
121 static always_inline
int pte64_is_valid (target_ulong pte0
)
123 return pte0
& 0x0000000000000001ULL
? 1 : 0;
126 static always_inline
void pte64_invalidate (target_ulong
*pte0
)
128 *pte0
&= ~0x0000000000000001ULL
;
132 #define PTE_PTEM_MASK 0x7FFFFFBF
133 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
134 #if defined(TARGET_PPC64)
135 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
136 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
139 static always_inline
int pp_check (int key
, int pp
, int nx
)
143 /* Compute access rights */
144 /* When pp is 3/7, the result is undefined. Set it to noaccess */
151 access
|= PAGE_WRITE
;
169 access
= PAGE_READ
| PAGE_WRITE
;
179 static always_inline
int check_prot (int prot
, int rw
, int access_type
)
183 if (access_type
== ACCESS_CODE
) {
184 if (prot
& PAGE_EXEC
)
189 if (prot
& PAGE_WRITE
)
194 if (prot
& PAGE_READ
)
203 static always_inline
int _pte_check (mmu_ctx_t
*ctx
, int is_64b
,
204 target_ulong pte0
, target_ulong pte1
,
205 int h
, int rw
, int type
)
207 target_ulong ptem
, mmask
;
208 int access
, ret
, pteh
, ptev
, pp
;
212 /* Check validity and table match */
213 #if defined(TARGET_PPC64)
215 ptev
= pte64_is_valid(pte0
);
216 pteh
= (pte0
>> 1) & 1;
220 ptev
= pte_is_valid(pte0
);
221 pteh
= (pte0
>> 6) & 1;
223 if (ptev
&& h
== pteh
) {
224 /* Check vsid & api */
225 #if defined(TARGET_PPC64)
227 ptem
= pte0
& PTE64_PTEM_MASK
;
228 mmask
= PTE64_CHECK_MASK
;
229 pp
= (pte1
& 0x00000003) | ((pte1
>> 61) & 0x00000004);
230 ctx
->nx
|= (pte1
>> 2) & 1; /* No execute bit */
231 ctx
->nx
|= (pte1
>> 3) & 1; /* Guarded bit */
235 ptem
= pte0
& PTE_PTEM_MASK
;
236 mmask
= PTE_CHECK_MASK
;
237 pp
= pte1
& 0x00000003;
239 if (ptem
== ctx
->ptem
) {
240 if (ctx
->raddr
!= (target_phys_addr_t
)-1ULL) {
241 /* all matches should have equal RPN, WIMG & PP */
242 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
243 qemu_log("Bad RPN/WIMG/PP\n");
247 /* Compute access rights */
248 access
= pp_check(ctx
->key
, pp
, ctx
->nx
);
249 /* Keep the matching PTE informations */
252 ret
= check_prot(ctx
->prot
, rw
, type
);
255 LOG_MMU("PTE access granted !\n");
257 /* Access right violation */
258 LOG_MMU("PTE access rejected\n");
266 static always_inline
int pte32_check (mmu_ctx_t
*ctx
,
267 target_ulong pte0
, target_ulong pte1
,
268 int h
, int rw
, int type
)
270 return _pte_check(ctx
, 0, pte0
, pte1
, h
, rw
, type
);
273 #if defined(TARGET_PPC64)
274 static always_inline
int pte64_check (mmu_ctx_t
*ctx
,
275 target_ulong pte0
, target_ulong pte1
,
276 int h
, int rw
, int type
)
278 return _pte_check(ctx
, 1, pte0
, pte1
, h
, rw
, type
);
282 static always_inline
int pte_update_flags (mmu_ctx_t
*ctx
, target_ulong
*pte1p
,
287 /* Update page flags */
288 if (!(*pte1p
& 0x00000100)) {
289 /* Update accessed flag */
290 *pte1p
|= 0x00000100;
293 if (!(*pte1p
& 0x00000080)) {
294 if (rw
== 1 && ret
== 0) {
295 /* Update changed flag */
296 *pte1p
|= 0x00000080;
299 /* Force page fault for first write access */
300 ctx
->prot
&= ~PAGE_WRITE
;
307 /* Software driven TLB helpers */
308 static always_inline
int ppc6xx_tlb_getnum (CPUState
*env
, target_ulong eaddr
,
309 int way
, int is_code
)
313 /* Select TLB num in a way from address */
314 nr
= (eaddr
>> TARGET_PAGE_BITS
) & (env
->tlb_per_way
- 1);
316 nr
+= env
->tlb_per_way
* way
;
317 /* 6xx have separate TLBs for instructions and data */
318 if (is_code
&& env
->id_tlbs
== 1)
324 static always_inline
void ppc6xx_tlb_invalidate_all (CPUState
*env
)
329 //LOG_SWTLB("Invalidate all TLBs\n");
330 /* Invalidate all defined software TLB */
332 if (env
->id_tlbs
== 1)
334 for (nr
= 0; nr
< max
; nr
++) {
335 tlb
= &env
->tlb
[nr
].tlb6
;
336 pte_invalidate(&tlb
->pte0
);
341 static always_inline
void __ppc6xx_tlb_invalidate_virt (CPUState
*env
,
346 #if !defined(FLUSH_ALL_TLBS)
350 /* Invalidate ITLB + DTLB, all ways */
351 for (way
= 0; way
< env
->nb_ways
; way
++) {
352 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
, is_code
);
353 tlb
= &env
->tlb
[nr
].tlb6
;
354 if (pte_is_valid(tlb
->pte0
) && (match_epn
== 0 || eaddr
== tlb
->EPN
)) {
355 LOG_SWTLB("TLB invalidate %d/%d " ADDRX
"\n",
356 nr
, env
->nb_tlb
, eaddr
);
357 pte_invalidate(&tlb
->pte0
);
358 tlb_flush_page(env
, tlb
->EPN
);
362 /* XXX: PowerPC specification say this is valid as well */
363 ppc6xx_tlb_invalidate_all(env
);
367 static always_inline
void ppc6xx_tlb_invalidate_virt (CPUState
*env
,
371 __ppc6xx_tlb_invalidate_virt(env
, eaddr
, is_code
, 0);
374 void ppc6xx_tlb_store (CPUState
*env
, target_ulong EPN
, int way
, int is_code
,
375 target_ulong pte0
, target_ulong pte1
)
380 nr
= ppc6xx_tlb_getnum(env
, EPN
, way
, is_code
);
381 tlb
= &env
->tlb
[nr
].tlb6
;
382 LOG_SWTLB("Set TLB %d/%d EPN " ADDRX
" PTE0 " ADDRX
383 " PTE1 " ADDRX
"\n", nr
, env
->nb_tlb
, EPN
, pte0
, pte1
);
384 /* Invalidate any pending reference in Qemu for this virtual address */
385 __ppc6xx_tlb_invalidate_virt(env
, EPN
, is_code
, 1);
389 /* Store last way for LRU mechanism */
393 static always_inline
int ppc6xx_tlb_check (CPUState
*env
, mmu_ctx_t
*ctx
,
394 target_ulong eaddr
, int rw
,
402 ret
= -1; /* No TLB found */
403 for (way
= 0; way
< env
->nb_ways
; way
++) {
404 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
,
405 access_type
== ACCESS_CODE
? 1 : 0);
406 tlb
= &env
->tlb
[nr
].tlb6
;
407 /* This test "emulates" the PTE index match for hardware TLBs */
408 if ((eaddr
& TARGET_PAGE_MASK
) != tlb
->EPN
) {
409 LOG_SWTLB("TLB %d/%d %s [" ADDRX
" " ADDRX
412 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
413 tlb
->EPN
, tlb
->EPN
+ TARGET_PAGE_SIZE
, eaddr
);
416 LOG_SWTLB("TLB %d/%d %s " ADDRX
" <> " ADDRX
" " ADDRX
419 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
420 tlb
->EPN
, eaddr
, tlb
->pte1
,
421 rw
? 'S' : 'L', access_type
== ACCESS_CODE
? 'I' : 'D');
422 switch (pte32_check(ctx
, tlb
->pte0
, tlb
->pte1
, 0, rw
, access_type
)) {
424 /* TLB inconsistency */
427 /* Access violation */
437 /* XXX: we should go on looping to check all TLBs consistency
438 * but we can speed-up the whole thing as the
439 * result would be undefined if TLBs are not consistent.
448 LOG_SWTLB("found TLB at addr " PADDRX
" prot=%01x ret=%d\n",
449 ctx
->raddr
& TARGET_PAGE_MASK
, ctx
->prot
, ret
);
450 /* Update page flags */
451 pte_update_flags(ctx
, &env
->tlb
[best
].tlb6
.pte1
, ret
, rw
);
457 /* Perform BAT hit & translation */
458 static always_inline
void bat_size_prot (CPUState
*env
, target_ulong
*blp
,
459 int *validp
, int *protp
,
460 target_ulong
*BATu
, target_ulong
*BATl
)
465 bl
= (*BATu
& 0x00001FFC) << 15;
468 if (((msr_pr
== 0) && (*BATu
& 0x00000002)) ||
469 ((msr_pr
!= 0) && (*BATu
& 0x00000001))) {
471 pp
= *BATl
& 0x00000003;
473 prot
= PAGE_READ
| PAGE_EXEC
;
483 static always_inline
void bat_601_size_prot (CPUState
*env
,target_ulong
*blp
,
484 int *validp
, int *protp
,
489 int key
, pp
, valid
, prot
;
491 bl
= (*BATl
& 0x0000003F) << 17;
492 LOG_BATS("b %02x ==> bl " ADDRX
" msk " ADDRX
"\n",
493 (uint8_t)(*BATl
& 0x0000003F), bl
, ~bl
);
495 valid
= (*BATl
>> 6) & 1;
497 pp
= *BATu
& 0x00000003;
499 key
= (*BATu
>> 3) & 1;
501 key
= (*BATu
>> 2) & 1;
502 prot
= pp_check(key
, pp
, 0);
509 static always_inline
int get_bat (CPUState
*env
, mmu_ctx_t
*ctx
,
510 target_ulong
virtual, int rw
, int type
)
512 target_ulong
*BATlt
, *BATut
, *BATu
, *BATl
;
513 target_ulong base
, BEPIl
, BEPIu
, bl
;
517 LOG_BATS("%s: %cBAT v " ADDRX
"\n", __func__
,
518 type
== ACCESS_CODE
? 'I' : 'D', virtual);
521 BATlt
= env
->IBAT
[1];
522 BATut
= env
->IBAT
[0];
525 BATlt
= env
->DBAT
[1];
526 BATut
= env
->DBAT
[0];
529 base
= virtual & 0xFFFC0000;
530 for (i
= 0; i
< env
->nb_BATs
; i
++) {
533 BEPIu
= *BATu
& 0xF0000000;
534 BEPIl
= *BATu
& 0x0FFE0000;
535 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
536 bat_601_size_prot(env
, &bl
, &valid
, &prot
, BATu
, BATl
);
538 bat_size_prot(env
, &bl
, &valid
, &prot
, BATu
, BATl
);
540 LOG_BATS("%s: %cBAT%d v " ADDRX
" BATu " ADDRX
541 " BATl " ADDRX
"\n", __func__
,
542 type
== ACCESS_CODE
? 'I' : 'D', i
, virtual, *BATu
, *BATl
);
543 if ((virtual & 0xF0000000) == BEPIu
&&
544 ((virtual & 0x0FFE0000) & ~bl
) == BEPIl
) {
547 /* Get physical address */
548 ctx
->raddr
= (*BATl
& 0xF0000000) |
549 ((virtual & 0x0FFE0000 & bl
) | (*BATl
& 0x0FFE0000)) |
550 (virtual & 0x0001F000);
551 /* Compute access rights */
553 ret
= check_prot(ctx
->prot
, rw
, type
);
555 LOG_BATS("BAT %d match: r " PADDRX
" prot=%c%c\n",
556 i
, ctx
->raddr
, ctx
->prot
& PAGE_READ
? 'R' : '-',
557 ctx
->prot
& PAGE_WRITE
? 'W' : '-');
563 #if defined(DEBUG_BATS)
565 QEMU_LOG0("no BAT match for " ADDRX
":\n", virtual);
566 for (i
= 0; i
< 4; i
++) {
569 BEPIu
= *BATu
& 0xF0000000;
570 BEPIl
= *BATu
& 0x0FFE0000;
571 bl
= (*BATu
& 0x00001FFC) << 15;
572 QEMU_LOG0("%s: %cBAT%d v " ADDRX
" BATu " ADDRX
573 " BATl " ADDRX
" \n\t" ADDRX
" " ADDRX
" " ADDRX
"\n",
574 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, virtual,
575 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
584 /* PTE table lookup */
585 static always_inline
int _find_pte (mmu_ctx_t
*ctx
, int is_64b
, int h
,
588 target_ulong base
, pte0
, pte1
;
592 ret
= -1; /* No entry found */
593 base
= ctx
->pg_addr
[h
];
594 for (i
= 0; i
< 8; i
++) {
595 #if defined(TARGET_PPC64)
597 pte0
= ldq_phys(base
+ (i
* 16));
598 pte1
= ldq_phys(base
+ (i
* 16) + 8);
599 r
= pte64_check(ctx
, pte0
, pte1
, h
, rw
, type
);
600 LOG_MMU("Load pte from " ADDRX
" => " ADDRX
" " ADDRX
601 " %d %d %d " ADDRX
"\n",
602 base
+ (i
* 16), pte0
, pte1
,
603 (int)(pte0
& 1), h
, (int)((pte0
>> 1) & 1),
608 pte0
= ldl_phys(base
+ (i
* 8));
609 pte1
= ldl_phys(base
+ (i
* 8) + 4);
610 r
= pte32_check(ctx
, pte0
, pte1
, h
, rw
, type
);
611 LOG_MMU("Load pte from " ADDRX
" => " ADDRX
" " ADDRX
612 " %d %d %d " ADDRX
"\n",
613 base
+ (i
* 8), pte0
, pte1
,
614 (int)(pte0
>> 31), h
, (int)((pte0
>> 6) & 1),
619 /* PTE inconsistency */
622 /* Access violation */
632 /* XXX: we should go on looping to check all PTEs consistency
633 * but if we can speed-up the whole thing as the
634 * result would be undefined if PTEs are not consistent.
643 LOG_MMU("found PTE at addr " PADDRX
" prot=%01x ret=%d\n",
644 ctx
->raddr
, ctx
->prot
, ret
);
645 /* Update page flags */
647 if (pte_update_flags(ctx
, &pte1
, ret
, rw
) == 1) {
648 #if defined(TARGET_PPC64)
650 stq_phys_notdirty(base
+ (good
* 16) + 8, pte1
);
654 stl_phys_notdirty(base
+ (good
* 8) + 4, pte1
);
662 static always_inline
int find_pte32 (mmu_ctx_t
*ctx
, int h
, int rw
, int type
)
664 return _find_pte(ctx
, 0, h
, rw
, type
);
667 #if defined(TARGET_PPC64)
668 static always_inline
int find_pte64 (mmu_ctx_t
*ctx
, int h
, int rw
, int type
)
670 return _find_pte(ctx
, 1, h
, rw
, type
);
674 static always_inline
int find_pte (CPUState
*env
, mmu_ctx_t
*ctx
,
675 int h
, int rw
, int type
)
677 #if defined(TARGET_PPC64)
678 if (env
->mmu_model
& POWERPC_MMU_64
)
679 return find_pte64(ctx
, h
, rw
, type
);
682 return find_pte32(ctx
, h
, rw
, type
);
685 #if defined(TARGET_PPC64)
686 static always_inline
int slb_is_valid (uint64_t slb64
)
688 return slb64
& 0x0000000008000000ULL
? 1 : 0;
691 static always_inline
void slb_invalidate (uint64_t *slb64
)
693 *slb64
&= ~0x0000000008000000ULL
;
696 static always_inline
int slb_lookup (CPUPPCState
*env
, target_ulong eaddr
,
698 target_ulong
*page_mask
, int *attr
)
700 target_phys_addr_t sr_base
;
707 sr_base
= env
->spr
[SPR_ASR
];
708 LOG_SLB("%s: eaddr " ADDRX
" base " PADDRX
"\n",
709 __func__
, eaddr
, sr_base
);
710 mask
= 0x0000000000000000ULL
; /* Avoid gcc warning */
711 for (n
= 0; n
< env
->slb_nr
; n
++) {
712 tmp64
= ldq_phys(sr_base
);
713 tmp
= ldl_phys(sr_base
+ 8);
714 LOG_SLB("%s: seg %d " PADDRX
" %016" PRIx64
" %08"
715 PRIx32
"\n", __func__
, n
, sr_base
, tmp64
, tmp
);
716 if (slb_is_valid(tmp64
)) {
717 /* SLB entry is valid */
718 switch (tmp64
& 0x0000000006000000ULL
) {
719 case 0x0000000000000000ULL
:
721 mask
= 0xFFFFFFFFF0000000ULL
;
723 case 0x0000000002000000ULL
:
725 mask
= 0xFFFF000000000000ULL
;
727 case 0x0000000004000000ULL
:
728 case 0x0000000006000000ULL
:
729 /* Reserved => segment is invalid */
732 if ((eaddr
& mask
) == (tmp64
& mask
)) {
734 *vsid
= ((tmp64
<< 24) | (tmp
>> 8)) & 0x0003FFFFFFFFFFFFULL
;
747 void ppc_slb_invalidate_all (CPUPPCState
*env
)
749 target_phys_addr_t sr_base
;
751 int n
, do_invalidate
;
754 sr_base
= env
->spr
[SPR_ASR
];
755 /* XXX: Warning: slbia never invalidates the first segment */
756 for (n
= 1; n
< env
->slb_nr
; n
++) {
757 tmp64
= ldq_phys(sr_base
);
758 if (slb_is_valid(tmp64
)) {
759 slb_invalidate(&tmp64
);
760 stq_phys(sr_base
, tmp64
);
761 /* XXX: given the fact that segment size is 256 MB or 1TB,
762 * and we still don't have a tlb_flush_mask(env, n, mask)
763 * in Qemu, we just invalidate all TLBs
773 void ppc_slb_invalidate_one (CPUPPCState
*env
, uint64_t T0
)
775 target_phys_addr_t sr_base
;
776 target_ulong vsid
, page_mask
;
781 n
= slb_lookup(env
, T0
, &vsid
, &page_mask
, &attr
);
783 sr_base
= env
->spr
[SPR_ASR
];
785 tmp64
= ldq_phys(sr_base
);
786 if (slb_is_valid(tmp64
)) {
787 slb_invalidate(&tmp64
);
788 stq_phys(sr_base
, tmp64
);
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
798 target_ulong
ppc_load_slb (CPUPPCState
*env
, int slb_nr
)
800 target_phys_addr_t sr_base
;
805 sr_base
= env
->spr
[SPR_ASR
];
806 sr_base
+= 12 * slb_nr
;
807 tmp64
= ldq_phys(sr_base
);
808 tmp
= ldl_phys(sr_base
+ 8);
809 if (tmp64
& 0x0000000008000000ULL
) {
810 /* SLB entry is valid */
811 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
812 rt
= tmp
>> 8; /* 65:88 => 40:63 */
813 rt
|= (tmp64
& 0x7) << 24; /* 62:64 => 37:39 */
814 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
815 rt
|= ((tmp
>> 4) & 0xF) << 27;
819 LOG_SLB("%s: " PADDRX
" %016" PRIx64
" %08" PRIx32
" => %d "
820 ADDRX
"\n", __func__
, sr_base
, tmp64
, tmp
, slb_nr
, rt
);
825 void ppc_store_slb (CPUPPCState
*env
, int slb_nr
, target_ulong rs
)
827 target_phys_addr_t sr_base
;
831 sr_base
= env
->spr
[SPR_ASR
];
832 sr_base
+= 12 * slb_nr
;
833 /* Copy Rs bits 37:63 to SLB 62:88 */
835 tmp64
= (rs
>> 24) & 0x7;
836 /* Copy Rs bits 33:36 to SLB 89:92 */
837 tmp
|= ((rs
>> 27) & 0xF) << 4;
838 /* Set the valid bit */
841 tmp64
|= (uint32_t)slb_nr
<< 28;
842 LOG_SLB("%s: %d " ADDRX
" => " PADDRX
" %016" PRIx64
843 " %08" PRIx32
"\n", __func__
,
844 slb_nr
, rs
, sr_base
, tmp64
, tmp
);
845 /* Write SLB entry to memory */
846 stq_phys(sr_base
, tmp64
);
847 stl_phys(sr_base
+ 8, tmp
);
849 #endif /* defined(TARGET_PPC64) */
851 /* Perform segment based translation */
852 static always_inline target_phys_addr_t
get_pgaddr (target_phys_addr_t sdr1
,
854 target_phys_addr_t hash
,
855 target_phys_addr_t mask
)
857 return (sdr1
& ((target_phys_addr_t
)(-1ULL) << sdr_sh
)) | (hash
& mask
);
860 static always_inline
int get_segment (CPUState
*env
, mmu_ctx_t
*ctx
,
861 target_ulong eaddr
, int rw
, int type
)
863 target_phys_addr_t sdr
, hash
, mask
, sdr_mask
, htab_mask
;
864 target_ulong sr
, vsid
, vsid_mask
, pgidx
, page_mask
;
865 #if defined(TARGET_PPC64)
868 int ds
, vsid_sh
, sdr_sh
, pr
;
872 #if defined(TARGET_PPC64)
873 if (env
->mmu_model
& POWERPC_MMU_64
) {
874 LOG_MMU("Check SLBs\n");
875 ret
= slb_lookup(env
, eaddr
, &vsid
, &page_mask
, &attr
);
878 ctx
->key
= ((attr
& 0x40) && (pr
!= 0)) ||
879 ((attr
& 0x80) && (pr
== 0)) ? 1 : 0;
881 ctx
->nx
= attr
& 0x20 ? 1 : 0;
882 vsid_mask
= 0x00003FFFFFFFFF80ULL
;
887 #endif /* defined(TARGET_PPC64) */
889 sr
= env
->sr
[eaddr
>> 28];
890 page_mask
= 0x0FFFFFFF;
891 ctx
->key
= (((sr
& 0x20000000) && (pr
!= 0)) ||
892 ((sr
& 0x40000000) && (pr
== 0))) ? 1 : 0;
893 ds
= sr
& 0x80000000 ? 1 : 0;
894 ctx
->nx
= sr
& 0x10000000 ? 1 : 0;
895 vsid
= sr
& 0x00FFFFFF;
896 vsid_mask
= 0x01FFFFC0;
900 LOG_MMU("Check segment v=" ADDRX
" %d " ADDRX
901 " nip=" ADDRX
" lr=" ADDRX
" ir=%d dr=%d pr=%d %d t=%d\n",
902 eaddr
, (int)(eaddr
>> 28), sr
, env
->nip
,
903 env
->lr
, (int)msr_ir
, (int)msr_dr
, pr
!= 0 ? 1 : 0,
906 LOG_MMU("pte segment: key=%d ds %d nx %d vsid " ADDRX
"\n",
907 ctx
->key
, ds
, ctx
->nx
, vsid
);
910 /* Check if instruction fetch is allowed, if needed */
911 if (type
!= ACCESS_CODE
|| ctx
->nx
== 0) {
912 /* Page address translation */
913 /* Primary table address */
915 pgidx
= (eaddr
& page_mask
) >> TARGET_PAGE_BITS
;
916 #if defined(TARGET_PPC64)
917 if (env
->mmu_model
& POWERPC_MMU_64
) {
918 htab_mask
= 0x0FFFFFFF >> (28 - (sdr
& 0x1F));
919 /* XXX: this is false for 1 TB segments */
920 hash
= ((vsid
^ pgidx
) << vsid_sh
) & vsid_mask
;
924 htab_mask
= sdr
& 0x000001FF;
925 hash
= ((vsid
^ pgidx
) << vsid_sh
) & vsid_mask
;
927 mask
= (htab_mask
<< sdr_sh
) | sdr_mask
;
928 LOG_MMU("sdr " PADDRX
" sh %d hash " PADDRX
929 " mask " PADDRX
" " ADDRX
"\n",
930 sdr
, sdr_sh
, hash
, mask
, page_mask
);
931 ctx
->pg_addr
[0] = get_pgaddr(sdr
, sdr_sh
, hash
, mask
);
932 /* Secondary table address */
933 hash
= (~hash
) & vsid_mask
;
934 LOG_MMU("sdr " PADDRX
" sh %d hash " PADDRX
935 " mask " PADDRX
"\n",
936 sdr
, sdr_sh
, hash
, mask
);
937 ctx
->pg_addr
[1] = get_pgaddr(sdr
, sdr_sh
, hash
, mask
);
938 #if defined(TARGET_PPC64)
939 if (env
->mmu_model
& POWERPC_MMU_64
) {
940 /* Only 5 bits of the page index are used in the AVPN */
941 ctx
->ptem
= (vsid
<< 12) | ((pgidx
>> 4) & 0x0F80);
945 ctx
->ptem
= (vsid
<< 7) | (pgidx
>> 10);
947 /* Initialize real address with an invalid value */
948 ctx
->raddr
= (target_phys_addr_t
)-1ULL;
949 if (unlikely(env
->mmu_model
== POWERPC_MMU_SOFT_6xx
||
950 env
->mmu_model
== POWERPC_MMU_SOFT_74xx
)) {
951 /* Software TLB search */
952 ret
= ppc6xx_tlb_check(env
, ctx
, eaddr
, rw
, type
);
954 LOG_MMU("0 sdr1=" PADDRX
" vsid=" ADDRX
" "
955 "api=" ADDRX
" hash=" PADDRX
956 " pg_addr=" PADDRX
"\n",
957 sdr
, vsid
, pgidx
, hash
, ctx
->pg_addr
[0]);
958 /* Primary table lookup */
959 ret
= find_pte(env
, ctx
, 0, rw
, type
);
961 /* Secondary table lookup */
962 if (eaddr
!= 0xEFFFFFFF)
963 LOG_MMU("1 sdr1=" PADDRX
" vsid=" ADDRX
" "
964 "api=" ADDRX
" hash=" PADDRX
965 " pg_addr=" PADDRX
"\n",
966 sdr
, vsid
, pgidx
, hash
, ctx
->pg_addr
[1]);
967 ret2
= find_pte(env
, ctx
, 1, rw
, type
);
972 #if defined (DUMP_PAGE_TABLES)
973 if (qemu_log_enabled()) {
974 target_phys_addr_t curaddr
;
975 uint32_t a0
, a1
, a2
, a3
;
976 qemu_log("Page table: " PADDRX
" len " PADDRX
"\n",
978 for (curaddr
= sdr
; curaddr
< (sdr
+ mask
+ 0x80);
980 a0
= ldl_phys(curaddr
);
981 a1
= ldl_phys(curaddr
+ 4);
982 a2
= ldl_phys(curaddr
+ 8);
983 a3
= ldl_phys(curaddr
+ 12);
984 if (a0
!= 0 || a1
!= 0 || a2
!= 0 || a3
!= 0) {
985 qemu_log(PADDRX
": %08x %08x %08x %08x\n",
986 curaddr
, a0
, a1
, a2
, a3
);
992 LOG_MMU("No access allowed\n");
996 LOG_MMU("direct store...\n");
997 /* Direct-store segment : absolutely *BUGGY* for now */
1000 /* Integer load/store : only access allowed */
1003 /* No code fetch is allowed in direct-store areas */
1006 /* Floating point load/store */
1009 /* lwarx, ldarx or srwcx. */
1012 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1013 /* Should make the instruction do no-op.
1014 * As it already do no-op, it's quite easy :-)
1019 /* eciwx or ecowx */
1022 qemu_log("ERROR: instruction should not need "
1023 "address translation\n");
1026 if ((rw
== 1 || ctx
->key
!= 1) && (rw
== 0 || ctx
->key
!= 0)) {
1037 /* Generic TLB check function for embedded PowerPC implementations */
1038 static always_inline
int ppcemb_tlb_check (CPUState
*env
, ppcemb_tlb_t
*tlb
,
1039 target_phys_addr_t
*raddrp
,
1040 target_ulong address
,
1041 uint32_t pid
, int ext
, int i
)
1045 /* Check valid flag */
1046 if (!(tlb
->prot
& PAGE_VALID
)) {
1047 qemu_log("%s: TLB %d not valid\n", __func__
, i
);
1050 mask
= ~(tlb
->size
- 1);
1051 LOG_SWTLB("%s: TLB %d address " ADDRX
" PID %u <=> " ADDRX
1053 __func__
, i
, address
, pid
, tlb
->EPN
, mask
, (uint32_t)tlb
->PID
);
1055 if (tlb
->PID
!= 0 && tlb
->PID
!= pid
)
1057 /* Check effective address */
1058 if ((address
& mask
) != tlb
->EPN
)
1060 *raddrp
= (tlb
->RPN
& mask
) | (address
& ~mask
);
1061 #if (TARGET_PHYS_ADDR_BITS >= 36)
1063 /* Extend the physical address to 36 bits */
1064 *raddrp
|= (target_phys_addr_t
)(tlb
->RPN
& 0xF) << 32;
1071 /* Generic TLB search function for PowerPC embedded implementations */
1072 int ppcemb_tlb_search (CPUPPCState
*env
, target_ulong address
, uint32_t pid
)
1075 target_phys_addr_t raddr
;
1078 /* Default return value is no match */
1080 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1081 tlb
= &env
->tlb
[i
].tlbe
;
1082 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
, pid
, 0, i
) == 0) {
1091 /* Helpers specific to PowerPC 40x implementations */
1092 static always_inline
void ppc4xx_tlb_invalidate_all (CPUState
*env
)
1097 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1098 tlb
= &env
->tlb
[i
].tlbe
;
1099 tlb
->prot
&= ~PAGE_VALID
;
1104 static always_inline
void ppc4xx_tlb_invalidate_virt (CPUState
*env
,
1108 #if !defined(FLUSH_ALL_TLBS)
1110 target_phys_addr_t raddr
;
1111 target_ulong page
, end
;
1114 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1115 tlb
= &env
->tlb
[i
].tlbe
;
1116 if (ppcemb_tlb_check(env
, tlb
, &raddr
, eaddr
, pid
, 0, i
) == 0) {
1117 end
= tlb
->EPN
+ tlb
->size
;
1118 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
1119 tlb_flush_page(env
, page
);
1120 tlb
->prot
&= ~PAGE_VALID
;
1125 ppc4xx_tlb_invalidate_all(env
);
1129 static int mmu40x_get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
,
1130 target_ulong address
, int rw
, int access_type
)
1133 target_phys_addr_t raddr
;
1134 int i
, ret
, zsel
, zpr
, pr
;
1137 raddr
= (target_phys_addr_t
)-1ULL;
1139 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1140 tlb
= &env
->tlb
[i
].tlbe
;
1141 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
1142 env
->spr
[SPR_40x_PID
], 0, i
) < 0)
1144 zsel
= (tlb
->attr
>> 4) & 0xF;
1145 zpr
= (env
->spr
[SPR_40x_ZPR
] >> (28 - (2 * zsel
))) & 0x3;
1146 LOG_SWTLB("%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1147 __func__
, i
, zsel
, zpr
, rw
, tlb
->attr
);
1148 /* Check execute enable bit */
1155 /* All accesses granted */
1156 ctx
->prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1168 /* Check from TLB entry */
1169 /* XXX: there is a problem here or in the TLB fill code... */
1170 ctx
->prot
= tlb
->prot
;
1171 ctx
->prot
|= PAGE_EXEC
;
1172 ret
= check_prot(ctx
->prot
, rw
, access_type
);
1177 LOG_SWTLB("%s: access granted " ADDRX
" => " PADDRX
1178 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
1183 LOG_SWTLB("%s: access refused " ADDRX
" => " PADDRX
1184 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
,
1190 void store_40x_sler (CPUPPCState
*env
, uint32_t val
)
1192 /* XXX: TO BE FIXED */
1193 if (val
!= 0x00000000) {
1194 cpu_abort(env
, "Little-endian regions are not supported by now\n");
1196 env
->spr
[SPR_405_SLER
] = val
;
1199 static int mmubooke_get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
,
1200 target_ulong address
, int rw
,
1204 target_phys_addr_t raddr
;
1208 raddr
= (target_phys_addr_t
)-1ULL;
1209 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1210 tlb
= &env
->tlb
[i
].tlbe
;
1211 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
1212 env
->spr
[SPR_BOOKE_PID
], 1, i
) < 0)
1215 prot
= tlb
->prot
& 0xF;
1217 prot
= (tlb
->prot
>> 4) & 0xF;
1218 /* Check the address space */
1219 if (access_type
== ACCESS_CODE
) {
1220 if (msr_ir
!= (tlb
->attr
& 1))
1223 if (prot
& PAGE_EXEC
) {
1229 if (msr_dr
!= (tlb
->attr
& 1))
1232 if ((!rw
&& prot
& PAGE_READ
) || (rw
&& (prot
& PAGE_WRITE
))) {
1245 static always_inline
int check_physical (CPUState
*env
, mmu_ctx_t
*ctx
,
1246 target_ulong eaddr
, int rw
)
1251 ctx
->prot
= PAGE_READ
| PAGE_EXEC
;
1253 switch (env
->mmu_model
) {
1254 case POWERPC_MMU_32B
:
1255 case POWERPC_MMU_601
:
1256 case POWERPC_MMU_SOFT_6xx
:
1257 case POWERPC_MMU_SOFT_74xx
:
1258 case POWERPC_MMU_SOFT_4xx
:
1259 case POWERPC_MMU_REAL
:
1260 case POWERPC_MMU_BOOKE
:
1261 ctx
->prot
|= PAGE_WRITE
;
1263 #if defined(TARGET_PPC64)
1264 case POWERPC_MMU_620
:
1265 case POWERPC_MMU_64B
:
1266 /* Real address are 60 bits long */
1267 ctx
->raddr
&= 0x0FFFFFFFFFFFFFFFULL
;
1268 ctx
->prot
|= PAGE_WRITE
;
1271 case POWERPC_MMU_SOFT_4xx_Z
:
1272 if (unlikely(msr_pe
!= 0)) {
1273 /* 403 family add some particular protections,
1274 * using PBL/PBU registers for accesses with no translation.
1277 /* Check PLB validity */
1278 (env
->pb
[0] < env
->pb
[1] &&
1279 /* and address in plb area */
1280 eaddr
>= env
->pb
[0] && eaddr
< env
->pb
[1]) ||
1281 (env
->pb
[2] < env
->pb
[3] &&
1282 eaddr
>= env
->pb
[2] && eaddr
< env
->pb
[3]) ? 1 : 0;
1283 if (in_plb
^ msr_px
) {
1284 /* Access in protected area */
1286 /* Access is not allowed */
1290 /* Read-write access is allowed */
1291 ctx
->prot
|= PAGE_WRITE
;
1295 case POWERPC_MMU_MPC8xx
:
1297 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1299 case POWERPC_MMU_BOOKE_FSL
:
1301 cpu_abort(env
, "BookE FSL MMU model not implemented\n");
1304 cpu_abort(env
, "Unknown or invalid MMU model\n");
1311 int get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
, target_ulong eaddr
,
1312 int rw
, int access_type
)
1317 qemu_log("%s\n", __func__
);
1319 if ((access_type
== ACCESS_CODE
&& msr_ir
== 0) ||
1320 (access_type
!= ACCESS_CODE
&& msr_dr
== 0)) {
1321 /* No address translation */
1322 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1325 switch (env
->mmu_model
) {
1326 case POWERPC_MMU_32B
:
1327 case POWERPC_MMU_601
:
1328 case POWERPC_MMU_SOFT_6xx
:
1329 case POWERPC_MMU_SOFT_74xx
:
1330 #if defined(TARGET_PPC64)
1331 case POWERPC_MMU_620
:
1332 case POWERPC_MMU_64B
:
1334 /* Try to find a BAT */
1335 if (env
->nb_BATs
!= 0)
1336 ret
= get_bat(env
, ctx
, eaddr
, rw
, access_type
);
1338 /* We didn't match any BAT entry or don't have BATs */
1339 ret
= get_segment(env
, ctx
, eaddr
, rw
, access_type
);
1342 case POWERPC_MMU_SOFT_4xx
:
1343 case POWERPC_MMU_SOFT_4xx_Z
:
1344 ret
= mmu40x_get_physical_address(env
, ctx
, eaddr
,
1347 case POWERPC_MMU_BOOKE
:
1348 ret
= mmubooke_get_physical_address(env
, ctx
, eaddr
,
1351 case POWERPC_MMU_MPC8xx
:
1353 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1355 case POWERPC_MMU_BOOKE_FSL
:
1357 cpu_abort(env
, "BookE FSL MMU model not implemented\n");
1359 case POWERPC_MMU_REAL
:
1360 cpu_abort(env
, "PowerPC in real mode do not do any translation\n");
1363 cpu_abort(env
, "Unknown or invalid MMU model\n");
1368 qemu_log("%s address " ADDRX
" => %d " PADDRX
"\n",
1369 __func__
, eaddr
, ret
, ctx
->raddr
);
1375 target_phys_addr_t
cpu_get_phys_page_debug (CPUState
*env
, target_ulong addr
)
1379 if (unlikely(get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) != 0))
1382 return ctx
.raddr
& TARGET_PAGE_MASK
;
1385 /* Perform address translation */
1386 int cpu_ppc_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
1387 int mmu_idx
, int is_softmmu
)
1396 access_type
= ACCESS_CODE
;
1399 access_type
= env
->access_type
;
1401 ret
= get_physical_address(env
, &ctx
, address
, rw
, access_type
);
1403 ret
= tlb_set_page_exec(env
, address
& TARGET_PAGE_MASK
,
1404 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
1405 mmu_idx
, is_softmmu
);
1406 } else if (ret
< 0) {
1408 if (access_type
== ACCESS_CODE
) {
1411 /* No matches in page tables or TLB */
1412 switch (env
->mmu_model
) {
1413 case POWERPC_MMU_SOFT_6xx
:
1414 env
->exception_index
= POWERPC_EXCP_IFTLB
;
1415 env
->error_code
= 1 << 18;
1416 env
->spr
[SPR_IMISS
] = address
;
1417 env
->spr
[SPR_ICMP
] = 0x80000000 | ctx
.ptem
;
1419 case POWERPC_MMU_SOFT_74xx
:
1420 env
->exception_index
= POWERPC_EXCP_IFTLB
;
1422 case POWERPC_MMU_SOFT_4xx
:
1423 case POWERPC_MMU_SOFT_4xx_Z
:
1424 env
->exception_index
= POWERPC_EXCP_ITLB
;
1425 env
->error_code
= 0;
1426 env
->spr
[SPR_40x_DEAR
] = address
;
1427 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1429 case POWERPC_MMU_32B
:
1430 case POWERPC_MMU_601
:
1431 #if defined(TARGET_PPC64)
1432 case POWERPC_MMU_620
:
1433 case POWERPC_MMU_64B
:
1435 env
->exception_index
= POWERPC_EXCP_ISI
;
1436 env
->error_code
= 0x40000000;
1438 case POWERPC_MMU_BOOKE
:
1440 cpu_abort(env
, "BookE MMU model is not implemented\n");
1442 case POWERPC_MMU_BOOKE_FSL
:
1444 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
1446 case POWERPC_MMU_MPC8xx
:
1448 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1450 case POWERPC_MMU_REAL
:
1451 cpu_abort(env
, "PowerPC in real mode should never raise "
1452 "any MMU exceptions\n");
1455 cpu_abort(env
, "Unknown or invalid MMU model\n");
1460 /* Access rights violation */
1461 env
->exception_index
= POWERPC_EXCP_ISI
;
1462 env
->error_code
= 0x08000000;
1465 /* No execute protection violation */
1466 env
->exception_index
= POWERPC_EXCP_ISI
;
1467 env
->error_code
= 0x10000000;
1470 /* Direct store exception */
1471 /* No code fetch is allowed in direct-store areas */
1472 env
->exception_index
= POWERPC_EXCP_ISI
;
1473 env
->error_code
= 0x10000000;
1475 #if defined(TARGET_PPC64)
1477 /* No match in segment table */
1478 if (env
->mmu_model
== POWERPC_MMU_620
) {
1479 env
->exception_index
= POWERPC_EXCP_ISI
;
1480 /* XXX: this might be incorrect */
1481 env
->error_code
= 0x40000000;
1483 env
->exception_index
= POWERPC_EXCP_ISEG
;
1484 env
->error_code
= 0;
1492 /* No matches in page tables or TLB */
1493 switch (env
->mmu_model
) {
1494 case POWERPC_MMU_SOFT_6xx
:
1496 env
->exception_index
= POWERPC_EXCP_DSTLB
;
1497 env
->error_code
= 1 << 16;
1499 env
->exception_index
= POWERPC_EXCP_DLTLB
;
1500 env
->error_code
= 0;
1502 env
->spr
[SPR_DMISS
] = address
;
1503 env
->spr
[SPR_DCMP
] = 0x80000000 | ctx
.ptem
;
1505 env
->error_code
|= ctx
.key
<< 19;
1506 env
->spr
[SPR_HASH1
] = ctx
.pg_addr
[0];
1507 env
->spr
[SPR_HASH2
] = ctx
.pg_addr
[1];
1509 case POWERPC_MMU_SOFT_74xx
:
1511 env
->exception_index
= POWERPC_EXCP_DSTLB
;
1513 env
->exception_index
= POWERPC_EXCP_DLTLB
;
1516 /* Implement LRU algorithm */
1517 env
->error_code
= ctx
.key
<< 19;
1518 env
->spr
[SPR_TLBMISS
] = (address
& ~((target_ulong
)0x3)) |
1519 ((env
->last_way
+ 1) & (env
->nb_ways
- 1));
1520 env
->spr
[SPR_PTEHI
] = 0x80000000 | ctx
.ptem
;
1522 case POWERPC_MMU_SOFT_4xx
:
1523 case POWERPC_MMU_SOFT_4xx_Z
:
1524 env
->exception_index
= POWERPC_EXCP_DTLB
;
1525 env
->error_code
= 0;
1526 env
->spr
[SPR_40x_DEAR
] = address
;
1528 env
->spr
[SPR_40x_ESR
] = 0x00800000;
1530 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1532 case POWERPC_MMU_32B
:
1533 case POWERPC_MMU_601
:
1534 #if defined(TARGET_PPC64)
1535 case POWERPC_MMU_620
:
1536 case POWERPC_MMU_64B
:
1538 env
->exception_index
= POWERPC_EXCP_DSI
;
1539 env
->error_code
= 0;
1540 env
->spr
[SPR_DAR
] = address
;
1542 env
->spr
[SPR_DSISR
] = 0x42000000;
1544 env
->spr
[SPR_DSISR
] = 0x40000000;
1546 case POWERPC_MMU_MPC8xx
:
1548 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1550 case POWERPC_MMU_BOOKE
:
1552 cpu_abort(env
, "BookE MMU model is not implemented\n");
1554 case POWERPC_MMU_BOOKE_FSL
:
1556 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
1558 case POWERPC_MMU_REAL
:
1559 cpu_abort(env
, "PowerPC in real mode should never raise "
1560 "any MMU exceptions\n");
1563 cpu_abort(env
, "Unknown or invalid MMU model\n");
1568 /* Access rights violation */
1569 env
->exception_index
= POWERPC_EXCP_DSI
;
1570 env
->error_code
= 0;
1571 env
->spr
[SPR_DAR
] = address
;
1573 env
->spr
[SPR_DSISR
] = 0x0A000000;
1575 env
->spr
[SPR_DSISR
] = 0x08000000;
1578 /* Direct store exception */
1579 switch (access_type
) {
1581 /* Floating point load/store */
1582 env
->exception_index
= POWERPC_EXCP_ALIGN
;
1583 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
1584 env
->spr
[SPR_DAR
] = address
;
1587 /* lwarx, ldarx or stwcx. */
1588 env
->exception_index
= POWERPC_EXCP_DSI
;
1589 env
->error_code
= 0;
1590 env
->spr
[SPR_DAR
] = address
;
1592 env
->spr
[SPR_DSISR
] = 0x06000000;
1594 env
->spr
[SPR_DSISR
] = 0x04000000;
1597 /* eciwx or ecowx */
1598 env
->exception_index
= POWERPC_EXCP_DSI
;
1599 env
->error_code
= 0;
1600 env
->spr
[SPR_DAR
] = address
;
1602 env
->spr
[SPR_DSISR
] = 0x06100000;
1604 env
->spr
[SPR_DSISR
] = 0x04100000;
1607 printf("DSI: invalid exception (%d)\n", ret
);
1608 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
1610 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
;
1611 env
->spr
[SPR_DAR
] = address
;
1615 #if defined(TARGET_PPC64)
1617 /* No match in segment table */
1618 if (env
->mmu_model
== POWERPC_MMU_620
) {
1619 env
->exception_index
= POWERPC_EXCP_DSI
;
1620 env
->error_code
= 0;
1621 env
->spr
[SPR_DAR
] = address
;
1622 /* XXX: this might be incorrect */
1624 env
->spr
[SPR_DSISR
] = 0x42000000;
1626 env
->spr
[SPR_DSISR
] = 0x40000000;
1628 env
->exception_index
= POWERPC_EXCP_DSEG
;
1629 env
->error_code
= 0;
1630 env
->spr
[SPR_DAR
] = address
;
1637 printf("%s: set exception to %d %02x\n", __func__
,
1638 env
->exception
, env
->error_code
);
1646 /*****************************************************************************/
1647 /* BATs management */
1648 #if !defined(FLUSH_ALL_TLBS)
1649 static always_inline
void do_invalidate_BAT (CPUPPCState
*env
,
1653 target_ulong base
, end
, page
;
1655 base
= BATu
& ~0x0001FFFF;
1656 end
= base
+ mask
+ 0x00020000;
1657 LOG_BATS("Flush BAT from " ADDRX
" to " ADDRX
" (" ADDRX
")\n",
1659 for (page
= base
; page
!= end
; page
+= TARGET_PAGE_SIZE
)
1660 tlb_flush_page(env
, page
);
1661 LOG_BATS("Flush done\n");
1665 static always_inline
void dump_store_bat (CPUPPCState
*env
, char ID
,
1666 int ul
, int nr
, target_ulong value
)
1668 LOG_BATS("Set %cBAT%d%c to " ADDRX
" (" ADDRX
")\n",
1669 ID
, nr
, ul
== 0 ? 'u' : 'l', value
, env
->nip
);
1672 void ppc_store_ibatu (CPUPPCState
*env
, int nr
, target_ulong value
)
1676 dump_store_bat(env
, 'I', 0, nr
, value
);
1677 if (env
->IBAT
[0][nr
] != value
) {
1678 mask
= (value
<< 15) & 0x0FFE0000UL
;
1679 #if !defined(FLUSH_ALL_TLBS)
1680 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1682 /* When storing valid upper BAT, mask BEPI and BRPN
1683 * and invalidate all TLBs covered by this BAT
1685 mask
= (value
<< 15) & 0x0FFE0000UL
;
1686 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1687 (value
& ~0x0001FFFFUL
& ~mask
);
1688 env
->IBAT
[1][nr
] = (env
->IBAT
[1][nr
] & 0x0000007B) |
1689 (env
->IBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1690 #if !defined(FLUSH_ALL_TLBS)
1691 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1698 void ppc_store_ibatl (CPUPPCState
*env
, int nr
, target_ulong value
)
1700 dump_store_bat(env
, 'I', 1, nr
, value
);
1701 env
->IBAT
[1][nr
] = value
;
1704 void ppc_store_dbatu (CPUPPCState
*env
, int nr
, target_ulong value
)
1708 dump_store_bat(env
, 'D', 0, nr
, value
);
1709 if (env
->DBAT
[0][nr
] != value
) {
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 #if !defined(FLUSH_ALL_TLBS)
1715 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1717 mask
= (value
<< 15) & 0x0FFE0000UL
;
1718 env
->DBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1719 (value
& ~0x0001FFFFUL
& ~mask
);
1720 env
->DBAT
[1][nr
] = (env
->DBAT
[1][nr
] & 0x0000007B) |
1721 (env
->DBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1722 #if !defined(FLUSH_ALL_TLBS)
1723 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1730 void ppc_store_dbatl (CPUPPCState
*env
, int nr
, target_ulong value
)
1732 dump_store_bat(env
, 'D', 1, nr
, value
);
1733 env
->DBAT
[1][nr
] = value
;
1736 void ppc_store_ibatu_601 (CPUPPCState
*env
, int nr
, target_ulong value
)
1741 dump_store_bat(env
, 'I', 0, nr
, value
);
1742 if (env
->IBAT
[0][nr
] != value
) {
1744 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1745 if (env
->IBAT
[1][nr
] & 0x40) {
1746 /* Invalidate BAT only if it is valid */
1747 #if !defined(FLUSH_ALL_TLBS)
1748 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1753 /* When storing valid upper BAT, mask BEPI and BRPN
1754 * and invalidate all TLBs covered by this BAT
1756 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1757 (value
& ~0x0001FFFFUL
& ~mask
);
1758 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1759 if (env
->IBAT
[1][nr
] & 0x40) {
1760 #if !defined(FLUSH_ALL_TLBS)
1761 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1766 #if defined(FLUSH_ALL_TLBS)
1773 void ppc_store_ibatl_601 (CPUPPCState
*env
, int nr
, target_ulong value
)
1778 dump_store_bat(env
, 'I', 1, nr
, value
);
1779 if (env
->IBAT
[1][nr
] != value
) {
1781 if (env
->IBAT
[1][nr
] & 0x40) {
1782 #if !defined(FLUSH_ALL_TLBS)
1783 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1784 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1790 #if !defined(FLUSH_ALL_TLBS)
1791 mask
= (value
<< 17) & 0x0FFE0000UL
;
1792 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1797 env
->IBAT
[1][nr
] = value
;
1798 env
->DBAT
[1][nr
] = value
;
1799 #if defined(FLUSH_ALL_TLBS)
1806 /*****************************************************************************/
1807 /* TLB management */
1808 void ppc_tlb_invalidate_all (CPUPPCState
*env
)
1810 switch (env
->mmu_model
) {
1811 case POWERPC_MMU_SOFT_6xx
:
1812 case POWERPC_MMU_SOFT_74xx
:
1813 ppc6xx_tlb_invalidate_all(env
);
1815 case POWERPC_MMU_SOFT_4xx
:
1816 case POWERPC_MMU_SOFT_4xx_Z
:
1817 ppc4xx_tlb_invalidate_all(env
);
1819 case POWERPC_MMU_REAL
:
1820 cpu_abort(env
, "No TLB for PowerPC 4xx in real mode\n");
1822 case POWERPC_MMU_MPC8xx
:
1824 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1826 case POWERPC_MMU_BOOKE
:
1828 cpu_abort(env
, "BookE MMU model is not implemented\n");
1830 case POWERPC_MMU_BOOKE_FSL
:
1833 cpu_abort(env
, "BookE MMU model is not implemented\n");
1835 case POWERPC_MMU_32B
:
1836 case POWERPC_MMU_601
:
1837 #if defined(TARGET_PPC64)
1838 case POWERPC_MMU_620
:
1839 case POWERPC_MMU_64B
:
1840 #endif /* defined(TARGET_PPC64) */
1845 cpu_abort(env
, "Unknown MMU model\n");
1850 void ppc_tlb_invalidate_one (CPUPPCState
*env
, target_ulong addr
)
1852 #if !defined(FLUSH_ALL_TLBS)
1853 addr
&= TARGET_PAGE_MASK
;
1854 switch (env
->mmu_model
) {
1855 case POWERPC_MMU_SOFT_6xx
:
1856 case POWERPC_MMU_SOFT_74xx
:
1857 ppc6xx_tlb_invalidate_virt(env
, addr
, 0);
1858 if (env
->id_tlbs
== 1)
1859 ppc6xx_tlb_invalidate_virt(env
, addr
, 1);
1861 case POWERPC_MMU_SOFT_4xx
:
1862 case POWERPC_MMU_SOFT_4xx_Z
:
1863 ppc4xx_tlb_invalidate_virt(env
, addr
, env
->spr
[SPR_40x_PID
]);
1865 case POWERPC_MMU_REAL
:
1866 cpu_abort(env
, "No TLB for PowerPC 4xx in real mode\n");
1868 case POWERPC_MMU_MPC8xx
:
1870 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1872 case POWERPC_MMU_BOOKE
:
1874 cpu_abort(env
, "BookE MMU model is not implemented\n");
1876 case POWERPC_MMU_BOOKE_FSL
:
1878 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
1880 case POWERPC_MMU_32B
:
1881 case POWERPC_MMU_601
:
1882 /* tlbie invalidate TLBs for all segments */
1883 addr
&= ~((target_ulong
)-1ULL << 28);
1884 /* XXX: this case should be optimized,
1885 * giving a mask to tlb_flush_page
1887 tlb_flush_page(env
, addr
| (0x0 << 28));
1888 tlb_flush_page(env
, addr
| (0x1 << 28));
1889 tlb_flush_page(env
, addr
| (0x2 << 28));
1890 tlb_flush_page(env
, addr
| (0x3 << 28));
1891 tlb_flush_page(env
, addr
| (0x4 << 28));
1892 tlb_flush_page(env
, addr
| (0x5 << 28));
1893 tlb_flush_page(env
, addr
| (0x6 << 28));
1894 tlb_flush_page(env
, addr
| (0x7 << 28));
1895 tlb_flush_page(env
, addr
| (0x8 << 28));
1896 tlb_flush_page(env
, addr
| (0x9 << 28));
1897 tlb_flush_page(env
, addr
| (0xA << 28));
1898 tlb_flush_page(env
, addr
| (0xB << 28));
1899 tlb_flush_page(env
, addr
| (0xC << 28));
1900 tlb_flush_page(env
, addr
| (0xD << 28));
1901 tlb_flush_page(env
, addr
| (0xE << 28));
1902 tlb_flush_page(env
, addr
| (0xF << 28));
1904 #if defined(TARGET_PPC64)
1905 case POWERPC_MMU_620
:
1906 case POWERPC_MMU_64B
:
1907 /* tlbie invalidate TLBs for all segments */
1908 /* XXX: given the fact that there are too many segments to invalidate,
1909 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
1910 * we just invalidate all TLBs
1914 #endif /* defined(TARGET_PPC64) */
1917 cpu_abort(env
, "Unknown MMU model\n");
1921 ppc_tlb_invalidate_all(env
);
1925 /*****************************************************************************/
1926 /* Special registers manipulation */
1927 #if defined(TARGET_PPC64)
1928 void ppc_store_asr (CPUPPCState
*env
, target_ulong value
)
1930 if (env
->asr
!= value
) {
1937 void ppc_store_sdr1 (CPUPPCState
*env
, target_ulong value
)
1939 LOG_MMU("%s: " ADDRX
"\n", __func__
, value
);
1940 if (env
->sdr1
!= value
) {
1941 /* XXX: for PowerPC 64, should check that the HTABSIZE value
1949 void ppc_store_sr (CPUPPCState
*env
, int srnum
, target_ulong value
)
1951 LOG_MMU("%s: reg=%d " ADDRX
" " ADDRX
"\n",
1952 __func__
, srnum
, value
, env
->sr
[srnum
]);
1953 if (env
->sr
[srnum
] != value
) {
1954 env
->sr
[srnum
] = value
;
1955 #if !defined(FLUSH_ALL_TLBS) && 0
1957 target_ulong page
, end
;
1958 /* Invalidate 256 MB of virtual memory */
1959 page
= (16 << 20) * srnum
;
1960 end
= page
+ (16 << 20);
1961 for (; page
!= end
; page
+= TARGET_PAGE_SIZE
)
1962 tlb_flush_page(env
, page
);
1969 #endif /* !defined (CONFIG_USER_ONLY) */
1971 /* GDBstub can read and write MSR... */
1972 void ppc_store_msr (CPUPPCState
*env
, target_ulong value
)
1974 hreg_store_msr(env
, value
, 0);
1977 /*****************************************************************************/
1978 /* Exception processing */
1979 #if defined (CONFIG_USER_ONLY)
1980 void do_interrupt (CPUState
*env
)
1982 env
->exception_index
= POWERPC_EXCP_NONE
;
1983 env
->error_code
= 0;
1986 void ppc_hw_interrupt (CPUState
*env
)
1988 env
->exception_index
= POWERPC_EXCP_NONE
;
1989 env
->error_code
= 0;
1991 #else /* defined (CONFIG_USER_ONLY) */
1992 static always_inline
void dump_syscall (CPUState
*env
)
1994 qemu_log_mask(CPU_LOG_INT
, "syscall r0=" REGX
" r3=" REGX
" r4=" REGX
1995 " r5=" REGX
" r6=" REGX
" nip=" ADDRX
"\n",
1996 ppc_dump_gpr(env
, 0), ppc_dump_gpr(env
, 3), ppc_dump_gpr(env
, 4),
1997 ppc_dump_gpr(env
, 5), ppc_dump_gpr(env
, 6), env
->nip
);
2000 /* Note that this function should be greatly optimized
2001 * when called with a constant excp, from ppc_hw_interrupt
2003 static always_inline
void powerpc_excp (CPUState
*env
,
2004 int excp_model
, int excp
)
2006 target_ulong msr
, new_msr
, vector
;
2007 int srr0
, srr1
, asrr0
, asrr1
;
2008 int lpes0
, lpes1
, lev
;
2011 /* XXX: find a suitable condition to enable the hypervisor mode */
2012 lpes0
= (env
->spr
[SPR_LPCR
] >> 1) & 1;
2013 lpes1
= (env
->spr
[SPR_LPCR
] >> 2) & 1;
2015 /* Those values ensure we won't enter the hypervisor mode */
2020 qemu_log_mask(CPU_LOG_INT
, "Raise exception at " ADDRX
" => %08x (%02x)\n",
2021 env
->nip
, excp
, env
->error_code
);
2028 msr
&= ~((target_ulong
)0x783F0000);
2030 case POWERPC_EXCP_NONE
:
2031 /* Should never happen */
2033 case POWERPC_EXCP_CRITICAL
: /* Critical input */
2034 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2035 switch (excp_model
) {
2036 case POWERPC_EXCP_40x
:
2037 srr0
= SPR_40x_SRR2
;
2038 srr1
= SPR_40x_SRR3
;
2040 case POWERPC_EXCP_BOOKE
:
2041 srr0
= SPR_BOOKE_CSRR0
;
2042 srr1
= SPR_BOOKE_CSRR1
;
2044 case POWERPC_EXCP_G2
:
2050 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
2052 /* Machine check exception is not enabled.
2053 * Enter checkstop state.
2055 if (qemu_log_enabled()) {
2056 qemu_log("Machine check while not allowed. "
2057 "Entering checkstop state\n");
2059 fprintf(stderr
, "Machine check while not allowed. "
2060 "Entering checkstop state\n");
2063 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
2065 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2066 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
2068 /* XXX: find a suitable condition to enable the hypervisor mode */
2069 new_msr
|= (target_ulong
)MSR_HVB
;
2071 /* XXX: should also have something loaded in DAR / DSISR */
2072 switch (excp_model
) {
2073 case POWERPC_EXCP_40x
:
2074 srr0
= SPR_40x_SRR2
;
2075 srr1
= SPR_40x_SRR3
;
2077 case POWERPC_EXCP_BOOKE
:
2078 srr0
= SPR_BOOKE_MCSRR0
;
2079 srr1
= SPR_BOOKE_MCSRR1
;
2080 asrr0
= SPR_BOOKE_CSRR0
;
2081 asrr1
= SPR_BOOKE_CSRR1
;
2087 case POWERPC_EXCP_DSI
: /* Data storage exception */
2088 LOG_EXCP("DSI exception: DSISR=" ADDRX
" DAR=" ADDRX
"\n",
2089 env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
2090 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2092 new_msr
|= (target_ulong
)MSR_HVB
;
2094 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
2095 LOG_EXCP("ISI exception: msr=" ADDRX
", nip=" ADDRX
"\n",
2097 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2099 new_msr
|= (target_ulong
)MSR_HVB
;
2100 msr
|= env
->error_code
;
2102 case POWERPC_EXCP_EXTERNAL
: /* External input */
2103 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2105 new_msr
|= (target_ulong
)MSR_HVB
;
2107 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
2108 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2110 new_msr
|= (target_ulong
)MSR_HVB
;
2111 /* XXX: this is false */
2112 /* Get rS/rD and rA from faulting opcode */
2113 env
->spr
[SPR_DSISR
] |= (ldl_code((env
->nip
- 4)) & 0x03FF0000) >> 16;
2115 case POWERPC_EXCP_PROGRAM
: /* Program exception */
2116 switch (env
->error_code
& ~0xF) {
2117 case POWERPC_EXCP_FP
:
2118 if ((msr_fe0
== 0 && msr_fe1
== 0) || msr_fp
== 0) {
2119 LOG_EXCP("Ignore floating point exception\n");
2120 env
->exception_index
= POWERPC_EXCP_NONE
;
2121 env
->error_code
= 0;
2124 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2126 new_msr
|= (target_ulong
)MSR_HVB
;
2128 if (msr_fe0
== msr_fe1
)
2132 case POWERPC_EXCP_INVAL
:
2133 LOG_EXCP("Invalid instruction at " ADDRX
"\n",
2135 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2137 new_msr
|= (target_ulong
)MSR_HVB
;
2140 case POWERPC_EXCP_PRIV
:
2141 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2143 new_msr
|= (target_ulong
)MSR_HVB
;
2146 case POWERPC_EXCP_TRAP
:
2147 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2149 new_msr
|= (target_ulong
)MSR_HVB
;
2153 /* Should never occur */
2154 cpu_abort(env
, "Invalid program exception %d. Aborting\n",
2159 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
2160 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2162 new_msr
|= (target_ulong
)MSR_HVB
;
2164 case POWERPC_EXCP_SYSCALL
: /* System call exception */
2165 /* NOTE: this is a temporary hack to support graphics OSI
2166 calls from the MOL driver */
2167 /* XXX: To be removed */
2168 if (env
->gpr
[3] == 0x113724fa && env
->gpr
[4] == 0x77810f9b &&
2170 if (env
->osi_call(env
) != 0) {
2171 env
->exception_index
= POWERPC_EXCP_NONE
;
2172 env
->error_code
= 0;
2177 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2178 lev
= env
->error_code
;
2179 if (lev
== 1 || (lpes0
== 0 && lpes1
== 0))
2180 new_msr
|= (target_ulong
)MSR_HVB
;
2182 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
2183 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2185 case POWERPC_EXCP_DECR
: /* Decrementer exception */
2186 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2188 new_msr
|= (target_ulong
)MSR_HVB
;
2190 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
2192 LOG_EXCP("FIT exception\n");
2193 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2195 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
2196 LOG_EXCP("WDT exception\n");
2197 switch (excp_model
) {
2198 case POWERPC_EXCP_BOOKE
:
2199 srr0
= SPR_BOOKE_CSRR0
;
2200 srr1
= SPR_BOOKE_CSRR1
;
2205 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2207 case POWERPC_EXCP_DTLB
: /* Data TLB error */
2208 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2210 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
2211 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2213 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
2214 switch (excp_model
) {
2215 case POWERPC_EXCP_BOOKE
:
2216 srr0
= SPR_BOOKE_DSRR0
;
2217 srr1
= SPR_BOOKE_DSRR1
;
2218 asrr0
= SPR_BOOKE_CSRR0
;
2219 asrr1
= SPR_BOOKE_CSRR1
;
2225 cpu_abort(env
, "Debug exception is not implemented yet !\n");
2227 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavailable */
2228 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2230 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data interrupt */
2232 cpu_abort(env
, "Embedded floating point data exception "
2233 "is not implemented yet !\n");
2235 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round interrupt */
2237 cpu_abort(env
, "Embedded floating point round exception "
2238 "is not implemented yet !\n");
2240 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor interrupt */
2241 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2244 "Performance counter exception is not implemented yet !\n");
2246 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
2249 "Embedded doorbell interrupt is not implemented yet !\n");
2251 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
2252 switch (excp_model
) {
2253 case POWERPC_EXCP_BOOKE
:
2254 srr0
= SPR_BOOKE_CSRR0
;
2255 srr1
= SPR_BOOKE_CSRR1
;
2261 cpu_abort(env
, "Embedded doorbell critical interrupt "
2262 "is not implemented yet !\n");
2264 case POWERPC_EXCP_RESET
: /* System reset exception */
2265 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2267 /* XXX: find a suitable condition to enable the hypervisor mode */
2268 new_msr
|= (target_ulong
)MSR_HVB
;
2271 case POWERPC_EXCP_DSEG
: /* Data segment exception */
2272 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2274 new_msr
|= (target_ulong
)MSR_HVB
;
2276 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
2277 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2279 new_msr
|= (target_ulong
)MSR_HVB
;
2281 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
2284 new_msr
|= (target_ulong
)MSR_HVB
;
2286 case POWERPC_EXCP_TRACE
: /* Trace exception */
2287 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2289 new_msr
|= (target_ulong
)MSR_HVB
;
2291 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
2294 new_msr
|= (target_ulong
)MSR_HVB
;
2296 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage exception */
2299 new_msr
|= (target_ulong
)MSR_HVB
;
2301 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
2304 new_msr
|= (target_ulong
)MSR_HVB
;
2306 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment exception */
2309 new_msr
|= (target_ulong
)MSR_HVB
;
2311 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
2312 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2314 new_msr
|= (target_ulong
)MSR_HVB
;
2316 case POWERPC_EXCP_PIT
: /* Programmable interval timer interrupt */
2317 LOG_EXCP("PIT exception\n");
2318 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2320 case POWERPC_EXCP_IO
: /* IO error exception */
2322 cpu_abort(env
, "601 IO error exception is not implemented yet !\n");
2324 case POWERPC_EXCP_RUNM
: /* Run mode exception */
2326 cpu_abort(env
, "601 run mode exception is not implemented yet !\n");
2328 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
2330 cpu_abort(env
, "602 emulation trap exception "
2331 "is not implemented yet !\n");
2333 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
2334 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2335 if (lpes1
== 0) /* XXX: check this */
2336 new_msr
|= (target_ulong
)MSR_HVB
;
2337 switch (excp_model
) {
2338 case POWERPC_EXCP_602
:
2339 case POWERPC_EXCP_603
:
2340 case POWERPC_EXCP_603E
:
2341 case POWERPC_EXCP_G2
:
2343 case POWERPC_EXCP_7x5
:
2345 case POWERPC_EXCP_74xx
:
2348 cpu_abort(env
, "Invalid instruction TLB miss exception\n");
2352 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
2353 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2354 if (lpes1
== 0) /* XXX: check this */
2355 new_msr
|= (target_ulong
)MSR_HVB
;
2356 switch (excp_model
) {
2357 case POWERPC_EXCP_602
:
2358 case POWERPC_EXCP_603
:
2359 case POWERPC_EXCP_603E
:
2360 case POWERPC_EXCP_G2
:
2362 case POWERPC_EXCP_7x5
:
2364 case POWERPC_EXCP_74xx
:
2367 cpu_abort(env
, "Invalid data load TLB miss exception\n");
2371 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
2372 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2373 if (lpes1
== 0) /* XXX: check this */
2374 new_msr
|= (target_ulong
)MSR_HVB
;
2375 switch (excp_model
) {
2376 case POWERPC_EXCP_602
:
2377 case POWERPC_EXCP_603
:
2378 case POWERPC_EXCP_603E
:
2379 case POWERPC_EXCP_G2
:
2381 /* Swap temporary saved registers with GPRs */
2382 if (!(new_msr
& ((target_ulong
)1 << MSR_TGPR
))) {
2383 new_msr
|= (target_ulong
)1 << MSR_TGPR
;
2384 hreg_swap_gpr_tgpr(env
);
2387 case POWERPC_EXCP_7x5
:
2389 #if defined (DEBUG_SOFTWARE_TLB)
2390 if (qemu_log_enabled()) {
2391 const unsigned char *es
;
2392 target_ulong
*miss
, *cmp
;
2394 if (excp
== POWERPC_EXCP_IFTLB
) {
2397 miss
= &env
->spr
[SPR_IMISS
];
2398 cmp
= &env
->spr
[SPR_ICMP
];
2400 if (excp
== POWERPC_EXCP_DLTLB
)
2405 miss
= &env
->spr
[SPR_DMISS
];
2406 cmp
= &env
->spr
[SPR_DCMP
];
2408 qemu_log("6xx %sTLB miss: %cM " ADDRX
" %cC " ADDRX
2409 " H1 " ADDRX
" H2 " ADDRX
" %08x\n",
2410 es
, en
, *miss
, en
, *cmp
,
2411 env
->spr
[SPR_HASH1
], env
->spr
[SPR_HASH2
],
2415 msr
|= env
->crf
[0] << 28;
2416 msr
|= env
->error_code
; /* key, D/I, S/L bits */
2417 /* Set way using a LRU mechanism */
2418 msr
|= ((env
->last_way
+ 1) & (env
->nb_ways
- 1)) << 17;
2420 case POWERPC_EXCP_74xx
:
2422 #if defined (DEBUG_SOFTWARE_TLB)
2423 if (qemu_log_enabled()) {
2424 const unsigned char *es
;
2425 target_ulong
*miss
, *cmp
;
2427 if (excp
== POWERPC_EXCP_IFTLB
) {
2430 miss
= &env
->spr
[SPR_TLBMISS
];
2431 cmp
= &env
->spr
[SPR_PTEHI
];
2433 if (excp
== POWERPC_EXCP_DLTLB
)
2438 miss
= &env
->spr
[SPR_TLBMISS
];
2439 cmp
= &env
->spr
[SPR_PTEHI
];
2441 qemu_log("74xx %sTLB miss: %cM " ADDRX
" %cC " ADDRX
2443 es
, en
, *miss
, en
, *cmp
, env
->error_code
);
2446 msr
|= env
->error_code
; /* key bit */
2449 cpu_abort(env
, "Invalid data store TLB miss exception\n");
2453 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
2455 cpu_abort(env
, "Floating point assist exception "
2456 "is not implemented yet !\n");
2458 case POWERPC_EXCP_DABR
: /* Data address breakpoint */
2460 cpu_abort(env
, "DABR exception is not implemented yet !\n");
2462 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
2464 cpu_abort(env
, "IABR exception is not implemented yet !\n");
2466 case POWERPC_EXCP_SMI
: /* System management interrupt */
2468 cpu_abort(env
, "SMI exception is not implemented yet !\n");
2470 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
2472 cpu_abort(env
, "Thermal management exception "
2473 "is not implemented yet !\n");
2475 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor interrupt */
2476 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2478 new_msr
|= (target_ulong
)MSR_HVB
;
2481 "Performance counter exception is not implemented yet !\n");
2483 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
2485 cpu_abort(env
, "VPU assist exception is not implemented yet !\n");
2487 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
2490 "970 soft-patch exception is not implemented yet !\n");
2492 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
2495 "970 maintenance exception is not implemented yet !\n");
2497 case POWERPC_EXCP_MEXTBR
: /* Maskable external breakpoint */
2499 cpu_abort(env
, "Maskable external exception "
2500 "is not implemented yet !\n");
2502 case POWERPC_EXCP_NMEXTBR
: /* Non maskable external breakpoint */
2504 cpu_abort(env
, "Non maskable external exception "
2505 "is not implemented yet !\n");
2509 cpu_abort(env
, "Invalid PowerPC exception %d. Aborting\n", excp
);
2512 /* save current instruction location */
2513 env
->spr
[srr0
] = env
->nip
- 4;
2516 /* save next instruction location */
2517 env
->spr
[srr0
] = env
->nip
;
2521 env
->spr
[srr1
] = msr
;
2522 /* If any alternate SRR register are defined, duplicate saved values */
2524 env
->spr
[asrr0
] = env
->spr
[srr0
];
2526 env
->spr
[asrr1
] = env
->spr
[srr1
];
2527 /* If we disactivated any translation, flush TLBs */
2528 if (new_msr
& ((1 << MSR_IR
) | (1 << MSR_DR
)))
2530 /* reload MSR with correct bits */
2531 new_msr
&= ~((target_ulong
)1 << MSR_EE
);
2532 new_msr
&= ~((target_ulong
)1 << MSR_PR
);
2533 new_msr
&= ~((target_ulong
)1 << MSR_FP
);
2534 new_msr
&= ~((target_ulong
)1 << MSR_FE0
);
2535 new_msr
&= ~((target_ulong
)1 << MSR_SE
);
2536 new_msr
&= ~((target_ulong
)1 << MSR_BE
);
2537 new_msr
&= ~((target_ulong
)1 << MSR_FE1
);
2538 new_msr
&= ~((target_ulong
)1 << MSR_IR
);
2539 new_msr
&= ~((target_ulong
)1 << MSR_DR
);
2540 #if 0 /* Fix this: not on all targets */
2541 new_msr
&= ~((target_ulong
)1 << MSR_PMM
);
2543 new_msr
&= ~((target_ulong
)1 << MSR_LE
);
2545 new_msr
|= (target_ulong
)1 << MSR_LE
;
2547 new_msr
&= ~((target_ulong
)1 << MSR_LE
);
2548 /* Jump to handler */
2549 vector
= env
->excp_vectors
[excp
];
2550 if (vector
== (target_ulong
)-1ULL) {
2551 cpu_abort(env
, "Raised an exception without defined vector %d\n",
2554 vector
|= env
->excp_prefix
;
2555 #if defined(TARGET_PPC64)
2556 if (excp_model
== POWERPC_EXCP_BOOKE
) {
2558 new_msr
&= ~((target_ulong
)1 << MSR_CM
);
2559 vector
= (uint32_t)vector
;
2561 new_msr
|= (target_ulong
)1 << MSR_CM
;
2565 new_msr
&= ~((target_ulong
)1 << MSR_SF
);
2566 vector
= (uint32_t)vector
;
2568 new_msr
|= (target_ulong
)1 << MSR_SF
;
2572 /* XXX: we don't use hreg_store_msr here as already have treated
2573 * any special case that could occur. Just store MSR and update hflags
2575 env
->msr
= new_msr
& env
->msr_mask
;
2576 hreg_compute_hflags(env
);
2578 /* Reset exception state */
2579 env
->exception_index
= POWERPC_EXCP_NONE
;
2580 env
->error_code
= 0;
2583 void do_interrupt (CPUState
*env
)
2585 powerpc_excp(env
, env
->excp_model
, env
->exception_index
);
2588 void ppc_hw_interrupt (CPUPPCState
*env
)
2593 qemu_log_mask(CPU_LOG_INT
, "%s: %p pending %08x req %08x me %d ee %d\n",
2594 __func__
, env
, env
->pending_interrupts
,
2595 env
->interrupt_request
, (int)msr_me
, (int)msr_ee
);
2597 /* External reset */
2598 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_RESET
)) {
2599 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_RESET
);
2600 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_RESET
);
2603 /* Machine check exception */
2604 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_MCK
)) {
2605 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_MCK
);
2606 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_MCHECK
);
2610 /* External debug exception */
2611 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DEBUG
)) {
2612 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DEBUG
);
2613 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DEBUG
);
2618 /* XXX: find a suitable condition to enable the hypervisor mode */
2619 hdice
= env
->spr
[SPR_LPCR
] & 1;
2623 if ((msr_ee
!= 0 || msr_hv
== 0 || msr_pr
!= 0) && hdice
!= 0) {
2624 /* Hypervisor decrementer exception */
2625 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_HDECR
)) {
2626 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_HDECR
);
2627 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_HDECR
);
2632 /* External critical interrupt */
2633 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CEXT
)) {
2634 /* Taking a critical external interrupt does not clear the external
2635 * critical interrupt status
2638 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CEXT
);
2640 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_CRITICAL
);
2645 /* Watchdog timer on embedded PowerPC */
2646 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_WDT
)) {
2647 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_WDT
);
2648 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_WDT
);
2651 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CDOORBELL
)) {
2652 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CDOORBELL
);
2653 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DOORCI
);
2656 /* Fixed interval timer on embedded PowerPC */
2657 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_FIT
)) {
2658 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_FIT
);
2659 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_FIT
);
2662 /* Programmable interval timer on embedded PowerPC */
2663 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PIT
)) {
2664 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PIT
);
2665 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_PIT
);
2668 /* Decrementer exception */
2669 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DECR
)) {
2670 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DECR
);
2671 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DECR
);
2674 /* External interrupt */
2675 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_EXT
)) {
2676 /* Taking an external interrupt does not clear the external
2680 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_EXT
);
2682 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_EXTERNAL
);
2685 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DOORBELL
)) {
2686 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DOORBELL
);
2687 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DOORI
);
2690 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PERFM
)) {
2691 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PERFM
);
2692 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_PERFM
);
2695 /* Thermal interrupt */
2696 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_THERM
)) {
2697 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_THERM
);
2698 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_THERM
);
2703 #endif /* !CONFIG_USER_ONLY */
2705 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
)
2707 qemu_log("Return from exception at " ADDRX
" with flags " ADDRX
"\n",
2711 void cpu_ppc_reset (void *opaque
)
2713 CPUPPCState
*env
= opaque
;
2716 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
2717 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
2718 log_cpu_state(env
, 0);
2721 msr
= (target_ulong
)0;
2723 /* XXX: find a suitable condition to enable the hypervisor mode */
2724 msr
|= (target_ulong
)MSR_HVB
;
2726 msr
|= (target_ulong
)0 << MSR_AP
; /* TO BE CHECKED */
2727 msr
|= (target_ulong
)0 << MSR_SA
; /* TO BE CHECKED */
2728 msr
|= (target_ulong
)1 << MSR_EP
;
2729 #if defined (DO_SINGLE_STEP) && 0
2730 /* Single step trace mode */
2731 msr
|= (target_ulong
)1 << MSR_SE
;
2732 msr
|= (target_ulong
)1 << MSR_BE
;
2734 #if defined(CONFIG_USER_ONLY)
2735 msr
|= (target_ulong
)1 << MSR_FP
; /* Allow floating point usage */
2736 msr
|= (target_ulong
)1 << MSR_VR
; /* Allow altivec usage */
2737 msr
|= (target_ulong
)1 << MSR_SPE
; /* Allow SPE usage */
2738 msr
|= (target_ulong
)1 << MSR_PR
;
2740 env
->nip
= env
->hreset_vector
| env
->excp_prefix
;
2741 if (env
->mmu_model
!= POWERPC_MMU_REAL
)
2742 ppc_tlb_invalidate_all(env
);
2744 env
->msr
= msr
& env
->msr_mask
;
2745 hreg_compute_hflags(env
);
2746 env
->reserve
= (target_ulong
)-1ULL;
2747 /* Be sure no exception or interrupt is pending */
2748 env
->pending_interrupts
= 0;
2749 env
->exception_index
= POWERPC_EXCP_NONE
;
2750 env
->error_code
= 0;
2751 /* Flush all TLBs */
2755 CPUPPCState
*cpu_ppc_init (const char *cpu_model
)
2758 const ppc_def_t
*def
;
2760 def
= cpu_ppc_find_by_name(cpu_model
);
2764 env
= qemu_mallocz(sizeof(CPUPPCState
));
2766 ppc_translate_init();
2767 env
->cpu_model_str
= cpu_model
;
2768 cpu_ppc_register_internal(env
, def
);
2777 void cpu_ppc_close (CPUPPCState
*env
)
2779 /* Should also remove all opcode tables... */