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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "helper_regs.h"
31 #include "qemu-common.h"
37 //#define DEBUG_SOFTWARE_TLB
38 //#define DUMP_PAGE_TABLES
39 //#define DEBUG_EXCEPTIONS
40 //#define FLUSH_ALL_TLBS
42 /*****************************************************************************/
43 /* PowerPC MMU emulation */
45 #if defined(CONFIG_USER_ONLY)
46 int cpu_ppc_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
47 int mmu_idx
, int is_softmmu
)
49 int exception
, error_code
;
52 exception
= POWERPC_EXCP_ISI
;
53 error_code
= 0x40000000;
55 exception
= POWERPC_EXCP_DSI
;
56 error_code
= 0x40000000;
58 error_code
|= 0x02000000;
59 env
->spr
[SPR_DAR
] = address
;
60 env
->spr
[SPR_DSISR
] = error_code
;
62 env
->exception_index
= exception
;
63 env
->error_code
= error_code
;
68 target_phys_addr_t
cpu_get_phys_page_debug (CPUState
*env
, target_ulong addr
)
74 /* Common routines used by software and hardware TLBs emulation */
75 static always_inline
int pte_is_valid (target_ulong pte0
)
77 return pte0
& 0x80000000 ? 1 : 0;
80 static always_inline
void pte_invalidate (target_ulong
*pte0
)
85 #if defined(TARGET_PPC64)
86 static always_inline
int pte64_is_valid (target_ulong pte0
)
88 return pte0
& 0x0000000000000001ULL
? 1 : 0;
91 static always_inline
void pte64_invalidate (target_ulong
*pte0
)
93 *pte0
&= ~0x0000000000000001ULL
;
97 #define PTE_PTEM_MASK 0x7FFFFFBF
98 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
99 #if defined(TARGET_PPC64)
100 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
101 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
104 static always_inline
int pp_check (int key
, int pp
, int nx
)
108 /* Compute access rights */
109 /* When pp is 3/7, the result is undefined. Set it to noaccess */
116 access
|= PAGE_WRITE
;
134 access
= PAGE_READ
| PAGE_WRITE
;
144 static always_inline
int check_prot (int prot
, int rw
, int access_type
)
148 if (access_type
== ACCESS_CODE
) {
149 if (prot
& PAGE_EXEC
)
154 if (prot
& PAGE_WRITE
)
159 if (prot
& PAGE_READ
)
168 static always_inline
int _pte_check (mmu_ctx_t
*ctx
, int is_64b
,
169 target_ulong pte0
, target_ulong pte1
,
170 int h
, int rw
, int type
)
172 target_ulong ptem
, mmask
;
173 int access
, ret
, pteh
, ptev
, pp
;
177 /* Check validity and table match */
178 #if defined(TARGET_PPC64)
180 ptev
= pte64_is_valid(pte0
);
181 pteh
= (pte0
>> 1) & 1;
185 ptev
= pte_is_valid(pte0
);
186 pteh
= (pte0
>> 6) & 1;
188 if (ptev
&& h
== pteh
) {
189 /* Check vsid & api */
190 #if defined(TARGET_PPC64)
192 ptem
= pte0
& PTE64_PTEM_MASK
;
193 mmask
= PTE64_CHECK_MASK
;
194 pp
= (pte1
& 0x00000003) | ((pte1
>> 61) & 0x00000004);
195 ctx
->nx
|= (pte1
>> 2) & 1; /* No execute bit */
196 ctx
->nx
|= (pte1
>> 3) & 1; /* Guarded bit */
200 ptem
= pte0
& PTE_PTEM_MASK
;
201 mmask
= PTE_CHECK_MASK
;
202 pp
= pte1
& 0x00000003;
204 if (ptem
== ctx
->ptem
) {
205 if (ctx
->raddr
!= (target_phys_addr_t
)-1ULL) {
206 /* all matches should have equal RPN, WIMG & PP */
207 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
209 fprintf(logfile
, "Bad RPN/WIMG/PP\n");
213 /* Compute access rights */
214 access
= pp_check(ctx
->key
, pp
, ctx
->nx
);
215 /* Keep the matching PTE informations */
218 ret
= check_prot(ctx
->prot
, rw
, type
);
221 #if defined (DEBUG_MMU)
223 fprintf(logfile
, "PTE access granted !\n");
226 /* Access right violation */
227 #if defined (DEBUG_MMU)
229 fprintf(logfile
, "PTE access rejected\n");
238 static always_inline
int pte32_check (mmu_ctx_t
*ctx
,
239 target_ulong pte0
, target_ulong pte1
,
240 int h
, int rw
, int type
)
242 return _pte_check(ctx
, 0, pte0
, pte1
, h
, rw
, type
);
245 #if defined(TARGET_PPC64)
246 static always_inline
int pte64_check (mmu_ctx_t
*ctx
,
247 target_ulong pte0
, target_ulong pte1
,
248 int h
, int rw
, int type
)
250 return _pte_check(ctx
, 1, pte0
, pte1
, h
, rw
, type
);
254 static always_inline
int pte_update_flags (mmu_ctx_t
*ctx
, target_ulong
*pte1p
,
259 /* Update page flags */
260 if (!(*pte1p
& 0x00000100)) {
261 /* Update accessed flag */
262 *pte1p
|= 0x00000100;
265 if (!(*pte1p
& 0x00000080)) {
266 if (rw
== 1 && ret
== 0) {
267 /* Update changed flag */
268 *pte1p
|= 0x00000080;
271 /* Force page fault for first write access */
272 ctx
->prot
&= ~PAGE_WRITE
;
279 /* Software driven TLB helpers */
280 static always_inline
int ppc6xx_tlb_getnum (CPUState
*env
, target_ulong eaddr
,
281 int way
, int is_code
)
285 /* Select TLB num in a way from address */
286 nr
= (eaddr
>> TARGET_PAGE_BITS
) & (env
->tlb_per_way
- 1);
288 nr
+= env
->tlb_per_way
* way
;
289 /* 6xx have separate TLBs for instructions and data */
290 if (is_code
&& env
->id_tlbs
== 1)
296 static always_inline
void ppc6xx_tlb_invalidate_all (CPUState
*env
)
301 #if defined (DEBUG_SOFTWARE_TLB) && 0
303 fprintf(logfile
, "Invalidate all TLBs\n");
306 /* Invalidate all defined software TLB */
308 if (env
->id_tlbs
== 1)
310 for (nr
= 0; nr
< max
; nr
++) {
311 tlb
= &env
->tlb
[nr
].tlb6
;
312 pte_invalidate(&tlb
->pte0
);
317 static always_inline
void __ppc6xx_tlb_invalidate_virt (CPUState
*env
,
322 #if !defined(FLUSH_ALL_TLBS)
326 /* Invalidate ITLB + DTLB, all ways */
327 for (way
= 0; way
< env
->nb_ways
; way
++) {
328 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
, is_code
);
329 tlb
= &env
->tlb
[nr
].tlb6
;
330 if (pte_is_valid(tlb
->pte0
) && (match_epn
== 0 || eaddr
== tlb
->EPN
)) {
331 #if defined (DEBUG_SOFTWARE_TLB)
333 fprintf(logfile
, "TLB invalidate %d/%d " ADDRX
"\n",
334 nr
, env
->nb_tlb
, eaddr
);
337 pte_invalidate(&tlb
->pte0
);
338 tlb_flush_page(env
, tlb
->EPN
);
342 /* XXX: PowerPC specification say this is valid as well */
343 ppc6xx_tlb_invalidate_all(env
);
347 static always_inline
void ppc6xx_tlb_invalidate_virt (CPUState
*env
,
351 __ppc6xx_tlb_invalidate_virt(env
, eaddr
, is_code
, 0);
354 void ppc6xx_tlb_store (CPUState
*env
, target_ulong EPN
, int way
, int is_code
,
355 target_ulong pte0
, target_ulong pte1
)
360 nr
= ppc6xx_tlb_getnum(env
, EPN
, way
, is_code
);
361 tlb
= &env
->tlb
[nr
].tlb6
;
362 #if defined (DEBUG_SOFTWARE_TLB)
364 fprintf(logfile
, "Set TLB %d/%d EPN " ADDRX
" PTE0 " ADDRX
365 " PTE1 " ADDRX
"\n", nr
, env
->nb_tlb
, EPN
, pte0
, pte1
);
368 /* Invalidate any pending reference in Qemu for this virtual address */
369 __ppc6xx_tlb_invalidate_virt(env
, EPN
, is_code
, 1);
373 /* Store last way for LRU mechanism */
377 static always_inline
int ppc6xx_tlb_check (CPUState
*env
, mmu_ctx_t
*ctx
,
378 target_ulong eaddr
, int rw
,
386 ret
= -1; /* No TLB found */
387 for (way
= 0; way
< env
->nb_ways
; way
++) {
388 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
,
389 access_type
== ACCESS_CODE
? 1 : 0);
390 tlb
= &env
->tlb
[nr
].tlb6
;
391 /* This test "emulates" the PTE index match for hardware TLBs */
392 if ((eaddr
& TARGET_PAGE_MASK
) != tlb
->EPN
) {
393 #if defined (DEBUG_SOFTWARE_TLB)
395 fprintf(logfile
, "TLB %d/%d %s [" ADDRX
" " ADDRX
398 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
399 tlb
->EPN
, tlb
->EPN
+ TARGET_PAGE_SIZE
, eaddr
);
404 #if defined (DEBUG_SOFTWARE_TLB)
406 fprintf(logfile
, "TLB %d/%d %s " ADDRX
" <> " ADDRX
" " ADDRX
409 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
410 tlb
->EPN
, eaddr
, tlb
->pte1
,
411 rw
? 'S' : 'L', access_type
== ACCESS_CODE
? 'I' : 'D');
414 switch (pte32_check(ctx
, tlb
->pte0
, tlb
->pte1
, 0, rw
, access_type
)) {
416 /* TLB inconsistency */
419 /* Access violation */
429 /* XXX: we should go on looping to check all TLBs consistency
430 * but we can speed-up the whole thing as the
431 * result would be undefined if TLBs are not consistent.
440 #if defined (DEBUG_SOFTWARE_TLB)
442 fprintf(logfile
, "found TLB at addr " PADDRX
" prot=%01x ret=%d\n",
443 ctx
->raddr
& TARGET_PAGE_MASK
, ctx
->prot
, ret
);
446 /* Update page flags */
447 pte_update_flags(ctx
, &env
->tlb
[best
].tlb6
.pte1
, ret
, rw
);
453 /* Perform BAT hit & translation */
454 static always_inline
void bat_size_prot (CPUState
*env
, target_ulong
*blp
,
455 int *validp
, int *protp
,
456 target_ulong
*BATu
, target_ulong
*BATl
)
461 bl
= (*BATu
& 0x00001FFC) << 15;
464 if (((msr_pr
== 0) && (*BATu
& 0x00000002)) ||
465 ((msr_pr
!= 0) && (*BATu
& 0x00000001))) {
467 pp
= *BATl
& 0x00000003;
469 prot
= PAGE_READ
| PAGE_EXEC
;
479 static always_inline
void bat_601_size_prot (CPUState
*env
,target_ulong
*blp
,
480 int *validp
, int *protp
,
485 int key
, pp
, valid
, prot
;
487 bl
= (*BATl
& 0x0000003F) << 17;
488 #if defined (DEBUG_BATS)
490 fprintf(logfile
, "b %02x ==> bl " ADDRX
" msk " ADDRX
"\n",
491 (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 #if defined (DEBUG_BATS)
519 fprintf(logfile
, "%s: %cBAT v " ADDRX
"\n", __func__
,
520 type
== ACCESS_CODE
? 'I' : 'D', virtual);
525 BATlt
= env
->IBAT
[1];
526 BATut
= env
->IBAT
[0];
529 BATlt
= env
->DBAT
[1];
530 BATut
= env
->DBAT
[0];
533 base
= virtual & 0xFFFC0000;
534 for (i
= 0; i
< env
->nb_BATs
; i
++) {
537 BEPIu
= *BATu
& 0xF0000000;
538 BEPIl
= *BATu
& 0x0FFE0000;
539 if (unlikely(env
->mmu_model
== POWERPC_MMU_601
)) {
540 bat_601_size_prot(env
, &bl
, &valid
, &prot
, BATu
, BATl
);
542 bat_size_prot(env
, &bl
, &valid
, &prot
, BATu
, BATl
);
544 #if defined (DEBUG_BATS)
546 fprintf(logfile
, "%s: %cBAT%d v " ADDRX
" BATu " ADDRX
547 " BATl " ADDRX
"\n", __func__
,
548 type
== ACCESS_CODE
? 'I' : 'D', i
, virtual, *BATu
, *BATl
);
551 if ((virtual & 0xF0000000) == BEPIu
&&
552 ((virtual & 0x0FFE0000) & ~bl
) == BEPIl
) {
555 /* Get physical address */
556 ctx
->raddr
= (*BATl
& 0xF0000000) |
557 ((virtual & 0x0FFE0000 & bl
) | (*BATl
& 0x0FFE0000)) |
558 (virtual & 0x0001F000);
559 /* Compute access rights */
561 ret
= check_prot(ctx
->prot
, rw
, type
);
562 #if defined (DEBUG_BATS)
563 if (ret
== 0 && loglevel
!= 0) {
564 fprintf(logfile
, "BAT %d match: r " PADDRX
" prot=%c%c\n",
565 i
, ctx
->raddr
, ctx
->prot
& PAGE_READ
? 'R' : '-',
566 ctx
->prot
& PAGE_WRITE
? 'W' : '-');
574 #if defined (DEBUG_BATS)
576 fprintf(logfile
, "no BAT match for " ADDRX
":\n", virtual);
577 for (i
= 0; i
< 4; i
++) {
580 BEPIu
= *BATu
& 0xF0000000;
581 BEPIl
= *BATu
& 0x0FFE0000;
582 bl
= (*BATu
& 0x00001FFC) << 15;
583 fprintf(logfile
, "%s: %cBAT%d v " ADDRX
" BATu " ADDRX
584 " BATl " ADDRX
" \n\t" ADDRX
" " ADDRX
" " ADDRX
"\n",
585 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, virtual,
586 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
596 /* PTE table lookup */
597 static always_inline
int _find_pte (mmu_ctx_t
*ctx
, int is_64b
, int h
,
600 target_ulong base
, pte0
, pte1
;
604 ret
= -1; /* No entry found */
605 base
= ctx
->pg_addr
[h
];
606 for (i
= 0; i
< 8; i
++) {
607 #if defined(TARGET_PPC64)
609 pte0
= ldq_phys(base
+ (i
* 16));
610 pte1
= ldq_phys(base
+ (i
* 16) + 8);
611 r
= pte64_check(ctx
, pte0
, pte1
, h
, rw
, type
);
612 #if defined (DEBUG_MMU)
614 fprintf(logfile
, "Load pte from " ADDRX
" => " ADDRX
" " ADDRX
615 " %d %d %d " ADDRX
"\n",
616 base
+ (i
* 16), pte0
, pte1
,
617 (int)(pte0
& 1), h
, (int)((pte0
>> 1) & 1),
624 pte0
= ldl_phys(base
+ (i
* 8));
625 pte1
= ldl_phys(base
+ (i
* 8) + 4);
626 r
= pte32_check(ctx
, pte0
, pte1
, h
, rw
, type
);
627 #if defined (DEBUG_MMU)
629 fprintf(logfile
, "Load pte from " ADDRX
" => " ADDRX
" " ADDRX
630 " %d %d %d " ADDRX
"\n",
631 base
+ (i
* 8), pte0
, pte1
,
632 (int)(pte0
>> 31), h
, (int)((pte0
>> 6) & 1),
639 /* PTE inconsistency */
642 /* Access violation */
652 /* XXX: we should go on looping to check all PTEs consistency
653 * but if we can speed-up the whole thing as the
654 * result would be undefined if PTEs are not consistent.
663 #if defined (DEBUG_MMU)
665 fprintf(logfile
, "found PTE at addr " PADDRX
" prot=%01x ret=%d\n",
666 ctx
->raddr
, ctx
->prot
, ret
);
669 /* Update page flags */
671 if (pte_update_flags(ctx
, &pte1
, ret
, rw
) == 1) {
672 #if defined(TARGET_PPC64)
674 stq_phys_notdirty(base
+ (good
* 16) + 8, pte1
);
678 stl_phys_notdirty(base
+ (good
* 8) + 4, pte1
);
686 static always_inline
int find_pte32 (mmu_ctx_t
*ctx
, int h
, int rw
, int type
)
688 return _find_pte(ctx
, 0, h
, rw
, type
);
691 #if defined(TARGET_PPC64)
692 static always_inline
int find_pte64 (mmu_ctx_t
*ctx
, int h
, int rw
, int type
)
694 return _find_pte(ctx
, 1, h
, rw
, type
);
698 static always_inline
int find_pte (CPUState
*env
, mmu_ctx_t
*ctx
,
699 int h
, int rw
, int type
)
701 #if defined(TARGET_PPC64)
702 if (env
->mmu_model
& POWERPC_MMU_64
)
703 return find_pte64(ctx
, h
, rw
, type
);
706 return find_pte32(ctx
, h
, rw
, type
);
709 #if defined(TARGET_PPC64)
710 static always_inline
int slb_is_valid (uint64_t slb64
)
712 return slb64
& 0x0000000008000000ULL
? 1 : 0;
715 static always_inline
void slb_invalidate (uint64_t *slb64
)
717 *slb64
&= ~0x0000000008000000ULL
;
720 static always_inline
int slb_lookup (CPUPPCState
*env
, target_ulong eaddr
,
722 target_ulong
*page_mask
, int *attr
)
724 target_phys_addr_t sr_base
;
731 sr_base
= env
->spr
[SPR_ASR
];
732 #if defined(DEBUG_SLB)
734 fprintf(logfile
, "%s: eaddr " ADDRX
" base " PADDRX
"\n",
735 __func__
, eaddr
, sr_base
);
738 mask
= 0x0000000000000000ULL
; /* Avoid gcc warning */
739 for (n
= 0; n
< env
->slb_nr
; n
++) {
740 tmp64
= ldq_phys(sr_base
);
741 tmp
= ldl_phys(sr_base
+ 8);
742 #if defined(DEBUG_SLB)
744 fprintf(logfile
, "%s: seg %d " PADDRX
" %016" PRIx64
" %08"
745 PRIx32
"\n", __func__
, n
, sr_base
, tmp64
, tmp
);
748 if (slb_is_valid(tmp64
)) {
749 /* SLB entry is valid */
750 switch (tmp64
& 0x0000000006000000ULL
) {
751 case 0x0000000000000000ULL
:
753 mask
= 0xFFFFFFFFF0000000ULL
;
755 case 0x0000000002000000ULL
:
757 mask
= 0xFFFF000000000000ULL
;
759 case 0x0000000004000000ULL
:
760 case 0x0000000006000000ULL
:
761 /* Reserved => segment is invalid */
764 if ((eaddr
& mask
) == (tmp64
& mask
)) {
766 *vsid
= ((tmp64
<< 24) | (tmp
>> 8)) & 0x0003FFFFFFFFFFFFULL
;
779 void ppc_slb_invalidate_all (CPUPPCState
*env
)
781 target_phys_addr_t sr_base
;
783 int n
, do_invalidate
;
786 sr_base
= env
->spr
[SPR_ASR
];
787 /* XXX: Warning: slbia never invalidates the first segment */
788 for (n
= 1; n
< env
->slb_nr
; n
++) {
789 tmp64
= ldq_phys(sr_base
);
790 if (slb_is_valid(tmp64
)) {
791 slb_invalidate(&tmp64
);
792 stq_phys(sr_base
, tmp64
);
793 /* XXX: given the fact that segment size is 256 MB or 1TB,
794 * and we still don't have a tlb_flush_mask(env, n, mask)
795 * in Qemu, we just invalidate all TLBs
805 void ppc_slb_invalidate_one (CPUPPCState
*env
, uint64_t T0
)
807 target_phys_addr_t sr_base
;
808 target_ulong vsid
, page_mask
;
813 n
= slb_lookup(env
, T0
, &vsid
, &page_mask
, &attr
);
815 sr_base
= env
->spr
[SPR_ASR
];
817 tmp64
= ldq_phys(sr_base
);
818 if (slb_is_valid(tmp64
)) {
819 slb_invalidate(&tmp64
);
820 stq_phys(sr_base
, tmp64
);
821 /* XXX: given the fact that segment size is 256 MB or 1TB,
822 * and we still don't have a tlb_flush_mask(env, n, mask)
823 * in Qemu, we just invalidate all TLBs
830 target_ulong
ppc_load_slb (CPUPPCState
*env
, int slb_nr
)
832 target_phys_addr_t sr_base
;
837 sr_base
= env
->spr
[SPR_ASR
];
838 sr_base
+= 12 * slb_nr
;
839 tmp64
= ldq_phys(sr_base
);
840 tmp
= ldl_phys(sr_base
+ 8);
841 if (tmp64
& 0x0000000008000000ULL
) {
842 /* SLB entry is valid */
843 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
844 rt
= tmp
>> 8; /* 65:88 => 40:63 */
845 rt
|= (tmp64
& 0x7) << 24; /* 62:64 => 37:39 */
846 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
847 rt
|= ((tmp
>> 4) & 0xF) << 27;
851 #if defined(DEBUG_SLB)
853 fprintf(logfile
, "%s: " PADDRX
" %016" PRIx64
" %08" PRIx32
" => %d "
854 ADDRX
"\n", __func__
, sr_base
, tmp64
, tmp
, slb_nr
, rt
);
861 void ppc_store_slb (CPUPPCState
*env
, int slb_nr
, target_ulong rs
)
863 target_phys_addr_t sr_base
;
867 sr_base
= env
->spr
[SPR_ASR
];
868 sr_base
+= 12 * slb_nr
;
869 /* Copy Rs bits 37:63 to SLB 62:88 */
871 tmp64
= (rs
>> 24) & 0x7;
872 /* Copy Rs bits 33:36 to SLB 89:92 */
873 tmp
|= ((rs
>> 27) & 0xF) << 4;
874 /* Set the valid bit */
877 tmp64
|= (uint32_t)slb_nr
<< 28;
878 #if defined(DEBUG_SLB)
880 fprintf(logfile
, "%s: %d " ADDRX
" => " PADDRX
" %016" PRIx64
881 " %08" PRIx32
"\n", __func__
,
882 slb_nr
, rs
, sr_base
, tmp64
, tmp
);
885 /* Write SLB entry to memory */
886 stq_phys(sr_base
, tmp64
);
887 stl_phys(sr_base
+ 8, tmp
);
889 #endif /* defined(TARGET_PPC64) */
891 /* Perform segment based translation */
892 static always_inline target_phys_addr_t
get_pgaddr (target_phys_addr_t sdr1
,
894 target_phys_addr_t hash
,
895 target_phys_addr_t mask
)
897 return (sdr1
& ((target_phys_addr_t
)(-1ULL) << sdr_sh
)) | (hash
& mask
);
900 static always_inline
int get_segment (CPUState
*env
, mmu_ctx_t
*ctx
,
901 target_ulong eaddr
, int rw
, int type
)
903 target_phys_addr_t sdr
, hash
, mask
, sdr_mask
, htab_mask
;
904 target_ulong sr
, vsid
, vsid_mask
, pgidx
, page_mask
;
905 #if defined(TARGET_PPC64)
908 int ds
, vsid_sh
, sdr_sh
, pr
;
912 #if defined(TARGET_PPC64)
913 if (env
->mmu_model
& POWERPC_MMU_64
) {
914 #if defined (DEBUG_MMU)
916 fprintf(logfile
, "Check SLBs\n");
919 ret
= slb_lookup(env
, eaddr
, &vsid
, &page_mask
, &attr
);
922 ctx
->key
= ((attr
& 0x40) && (pr
!= 0)) ||
923 ((attr
& 0x80) && (pr
== 0)) ? 1 : 0;
925 ctx
->nx
= attr
& 0x20 ? 1 : 0;
926 vsid_mask
= 0x00003FFFFFFFFF80ULL
;
931 #endif /* defined(TARGET_PPC64) */
933 sr
= env
->sr
[eaddr
>> 28];
934 page_mask
= 0x0FFFFFFF;
935 ctx
->key
= (((sr
& 0x20000000) && (pr
!= 0)) ||
936 ((sr
& 0x40000000) && (pr
== 0))) ? 1 : 0;
937 ds
= sr
& 0x80000000 ? 1 : 0;
938 ctx
->nx
= sr
& 0x10000000 ? 1 : 0;
939 vsid
= sr
& 0x00FFFFFF;
940 vsid_mask
= 0x01FFFFC0;
944 #if defined (DEBUG_MMU)
946 fprintf(logfile
, "Check segment v=" ADDRX
" %d " ADDRX
947 " nip=" ADDRX
" lr=" ADDRX
" ir=%d dr=%d pr=%d %d t=%d\n",
948 eaddr
, (int)(eaddr
>> 28), sr
, env
->nip
,
949 env
->lr
, (int)msr_ir
, (int)msr_dr
, pr
!= 0 ? 1 : 0,
954 #if defined (DEBUG_MMU)
956 fprintf(logfile
, "pte segment: key=%d ds %d nx %d vsid " ADDRX
"\n",
957 ctx
->key
, ds
, ctx
->nx
, vsid
);
962 /* Check if instruction fetch is allowed, if needed */
963 if (type
!= ACCESS_CODE
|| ctx
->nx
== 0) {
964 /* Page address translation */
965 /* Primary table address */
967 pgidx
= (eaddr
& page_mask
) >> TARGET_PAGE_BITS
;
968 #if defined(TARGET_PPC64)
969 if (env
->mmu_model
& POWERPC_MMU_64
) {
970 htab_mask
= 0x0FFFFFFF >> (28 - (sdr
& 0x1F));
971 /* XXX: this is false for 1 TB segments */
972 hash
= ((vsid
^ pgidx
) << vsid_sh
) & vsid_mask
;
976 htab_mask
= sdr
& 0x000001FF;
977 hash
= ((vsid
^ pgidx
) << vsid_sh
) & vsid_mask
;
979 mask
= (htab_mask
<< sdr_sh
) | sdr_mask
;
980 #if defined (DEBUG_MMU)
982 fprintf(logfile
, "sdr " PADDRX
" sh %d hash " PADDRX
983 " mask " PADDRX
" " ADDRX
"\n",
984 sdr
, sdr_sh
, hash
, mask
, page_mask
);
987 ctx
->pg_addr
[0] = get_pgaddr(sdr
, sdr_sh
, hash
, mask
);
988 /* Secondary table address */
989 hash
= (~hash
) & vsid_mask
;
990 #if defined (DEBUG_MMU)
992 fprintf(logfile
, "sdr " PADDRX
" sh %d hash " PADDRX
993 " mask " PADDRX
"\n",
994 sdr
, sdr_sh
, hash
, mask
);
997 ctx
->pg_addr
[1] = get_pgaddr(sdr
, sdr_sh
, hash
, mask
);
998 #if defined(TARGET_PPC64)
999 if (env
->mmu_model
& POWERPC_MMU_64
) {
1000 /* Only 5 bits of the page index are used in the AVPN */
1001 ctx
->ptem
= (vsid
<< 12) | ((pgidx
>> 4) & 0x0F80);
1005 ctx
->ptem
= (vsid
<< 7) | (pgidx
>> 10);
1007 /* Initialize real address with an invalid value */
1008 ctx
->raddr
= (target_phys_addr_t
)-1ULL;
1009 if (unlikely(env
->mmu_model
== POWERPC_MMU_SOFT_6xx
||
1010 env
->mmu_model
== POWERPC_MMU_SOFT_74xx
)) {
1011 /* Software TLB search */
1012 ret
= ppc6xx_tlb_check(env
, ctx
, eaddr
, rw
, type
);
1014 #if defined (DEBUG_MMU)
1015 if (loglevel
!= 0) {
1016 fprintf(logfile
, "0 sdr1=" PADDRX
" vsid=" ADDRX
" "
1017 "api=" ADDRX
" hash=" PADDRX
1018 " pg_addr=" PADDRX
"\n",
1019 sdr
, vsid
, pgidx
, hash
, ctx
->pg_addr
[0]);
1022 /* Primary table lookup */
1023 ret
= find_pte(env
, ctx
, 0, rw
, type
);
1025 /* Secondary table lookup */
1026 #if defined (DEBUG_MMU)
1027 if (eaddr
!= 0xEFFFFFFF && loglevel
!= 0) {
1028 fprintf(logfile
, "1 sdr1=" PADDRX
" vsid=" ADDRX
" "
1029 "api=" ADDRX
" hash=" PADDRX
1030 " pg_addr=" PADDRX
"\n",
1031 sdr
, vsid
, pgidx
, hash
, ctx
->pg_addr
[1]);
1034 ret2
= find_pte(env
, ctx
, 1, rw
, type
);
1039 #if defined (DUMP_PAGE_TABLES)
1040 if (loglevel
!= 0) {
1041 target_phys_addr_t curaddr
;
1042 uint32_t a0
, a1
, a2
, a3
;
1043 fprintf(logfile
, "Page table: " PADDRX
" len " PADDRX
"\n",
1045 for (curaddr
= sdr
; curaddr
< (sdr
+ mask
+ 0x80);
1047 a0
= ldl_phys(curaddr
);
1048 a1
= ldl_phys(curaddr
+ 4);
1049 a2
= ldl_phys(curaddr
+ 8);
1050 a3
= ldl_phys(curaddr
+ 12);
1051 if (a0
!= 0 || a1
!= 0 || a2
!= 0 || a3
!= 0) {
1052 fprintf(logfile
, PADDRX
": %08x %08x %08x %08x\n",
1053 curaddr
, a0
, a1
, a2
, a3
);
1059 #if defined (DEBUG_MMU)
1061 fprintf(logfile
, "No access allowed\n");
1066 #if defined (DEBUG_MMU)
1068 fprintf(logfile
, "direct store...\n");
1070 /* Direct-store segment : absolutely *BUGGY* for now */
1073 /* Integer load/store : only access allowed */
1076 /* No code fetch is allowed in direct-store areas */
1079 /* Floating point load/store */
1082 /* lwarx, ldarx or srwcx. */
1085 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1086 /* Should make the instruction do no-op.
1087 * As it already do no-op, it's quite easy :-)
1092 /* eciwx or ecowx */
1096 fprintf(logfile
, "ERROR: instruction should not need "
1097 "address translation\n");
1101 if ((rw
== 1 || ctx
->key
!= 1) && (rw
== 0 || ctx
->key
!= 0)) {
1112 /* Generic TLB check function for embedded PowerPC implementations */
1113 static always_inline
int ppcemb_tlb_check (CPUState
*env
, ppcemb_tlb_t
*tlb
,
1114 target_phys_addr_t
*raddrp
,
1115 target_ulong address
,
1116 uint32_t pid
, int ext
, int i
)
1120 /* Check valid flag */
1121 if (!(tlb
->prot
& PAGE_VALID
)) {
1123 fprintf(logfile
, "%s: TLB %d not valid\n", __func__
, i
);
1126 mask
= ~(tlb
->size
- 1);
1127 #if defined (DEBUG_SOFTWARE_TLB)
1128 if (loglevel
!= 0) {
1129 fprintf(logfile
, "%s: TLB %d address " ADDRX
" PID %u <=> " ADDRX
1131 __func__
, i
, address
, pid
, tlb
->EPN
, mask
, (uint32_t)tlb
->PID
);
1135 if (tlb
->PID
!= 0 && tlb
->PID
!= pid
)
1137 /* Check effective address */
1138 if ((address
& mask
) != tlb
->EPN
)
1140 *raddrp
= (tlb
->RPN
& mask
) | (address
& ~mask
);
1141 #if (TARGET_PHYS_ADDR_BITS >= 36)
1143 /* Extend the physical address to 36 bits */
1144 *raddrp
|= (target_phys_addr_t
)(tlb
->RPN
& 0xF) << 32;
1151 /* Generic TLB search function for PowerPC embedded implementations */
1152 int ppcemb_tlb_search (CPUPPCState
*env
, target_ulong address
, uint32_t pid
)
1155 target_phys_addr_t raddr
;
1158 /* Default return value is no match */
1160 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1161 tlb
= &env
->tlb
[i
].tlbe
;
1162 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
, pid
, 0, i
) == 0) {
1171 /* Helpers specific to PowerPC 40x implementations */
1172 static always_inline
void ppc4xx_tlb_invalidate_all (CPUState
*env
)
1177 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1178 tlb
= &env
->tlb
[i
].tlbe
;
1179 tlb
->prot
&= ~PAGE_VALID
;
1184 static always_inline
void ppc4xx_tlb_invalidate_virt (CPUState
*env
,
1188 #if !defined(FLUSH_ALL_TLBS)
1190 target_phys_addr_t raddr
;
1191 target_ulong page
, end
;
1194 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1195 tlb
= &env
->tlb
[i
].tlbe
;
1196 if (ppcemb_tlb_check(env
, tlb
, &raddr
, eaddr
, pid
, 0, i
) == 0) {
1197 end
= tlb
->EPN
+ tlb
->size
;
1198 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
1199 tlb_flush_page(env
, page
);
1200 tlb
->prot
&= ~PAGE_VALID
;
1205 ppc4xx_tlb_invalidate_all(env
);
1209 int mmu40x_get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
,
1210 target_ulong address
, int rw
, int access_type
)
1213 target_phys_addr_t raddr
;
1214 int i
, ret
, zsel
, zpr
, pr
;
1217 raddr
= (target_phys_addr_t
)-1ULL;
1219 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1220 tlb
= &env
->tlb
[i
].tlbe
;
1221 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
1222 env
->spr
[SPR_40x_PID
], 0, i
) < 0)
1224 zsel
= (tlb
->attr
>> 4) & 0xF;
1225 zpr
= (env
->spr
[SPR_40x_ZPR
] >> (28 - (2 * zsel
))) & 0x3;
1226 #if defined (DEBUG_SOFTWARE_TLB)
1227 if (loglevel
!= 0) {
1228 fprintf(logfile
, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1229 __func__
, i
, zsel
, zpr
, rw
, tlb
->attr
);
1232 /* Check execute enable bit */
1239 /* All accesses granted */
1240 ctx
->prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1252 /* Check from TLB entry */
1253 /* XXX: there is a problem here or in the TLB fill code... */
1254 ctx
->prot
= tlb
->prot
;
1255 ctx
->prot
|= PAGE_EXEC
;
1256 ret
= check_prot(ctx
->prot
, rw
, access_type
);
1261 #if defined (DEBUG_SOFTWARE_TLB)
1262 if (loglevel
!= 0) {
1263 fprintf(logfile
, "%s: access granted " ADDRX
" => " PADDRX
1264 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
1271 #if defined (DEBUG_SOFTWARE_TLB)
1272 if (loglevel
!= 0) {
1273 fprintf(logfile
, "%s: access refused " ADDRX
" => " PADDRX
1274 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
,
1282 void store_40x_sler (CPUPPCState
*env
, uint32_t val
)
1284 /* XXX: TO BE FIXED */
1285 if (val
!= 0x00000000) {
1286 cpu_abort(env
, "Little-endian regions are not supported by now\n");
1288 env
->spr
[SPR_405_SLER
] = val
;
1291 int mmubooke_get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
,
1292 target_ulong address
, int rw
,
1296 target_phys_addr_t raddr
;
1300 raddr
= (target_phys_addr_t
)-1ULL;
1301 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1302 tlb
= &env
->tlb
[i
].tlbe
;
1303 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
1304 env
->spr
[SPR_BOOKE_PID
], 1, i
) < 0)
1307 prot
= tlb
->prot
& 0xF;
1309 prot
= (tlb
->prot
>> 4) & 0xF;
1310 /* Check the address space */
1311 if (access_type
== ACCESS_CODE
) {
1312 if (msr_ir
!= (tlb
->attr
& 1))
1315 if (prot
& PAGE_EXEC
) {
1321 if (msr_dr
!= (tlb
->attr
& 1))
1324 if ((!rw
&& prot
& PAGE_READ
) || (rw
&& (prot
& PAGE_WRITE
))) {
1337 static always_inline
int check_physical (CPUState
*env
, mmu_ctx_t
*ctx
,
1338 target_ulong eaddr
, int rw
)
1343 ctx
->prot
= PAGE_READ
| PAGE_EXEC
;
1345 switch (env
->mmu_model
) {
1346 case POWERPC_MMU_32B
:
1347 case POWERPC_MMU_601
:
1348 case POWERPC_MMU_SOFT_6xx
:
1349 case POWERPC_MMU_SOFT_74xx
:
1350 case POWERPC_MMU_SOFT_4xx
:
1351 case POWERPC_MMU_REAL
:
1352 case POWERPC_MMU_BOOKE
:
1353 ctx
->prot
|= PAGE_WRITE
;
1355 #if defined(TARGET_PPC64)
1356 case POWERPC_MMU_620
:
1357 case POWERPC_MMU_64B
:
1358 /* Real address are 60 bits long */
1359 ctx
->raddr
&= 0x0FFFFFFFFFFFFFFFULL
;
1360 ctx
->prot
|= PAGE_WRITE
;
1363 case POWERPC_MMU_SOFT_4xx_Z
:
1364 if (unlikely(msr_pe
!= 0)) {
1365 /* 403 family add some particular protections,
1366 * using PBL/PBU registers for accesses with no translation.
1369 /* Check PLB validity */
1370 (env
->pb
[0] < env
->pb
[1] &&
1371 /* and address in plb area */
1372 eaddr
>= env
->pb
[0] && eaddr
< env
->pb
[1]) ||
1373 (env
->pb
[2] < env
->pb
[3] &&
1374 eaddr
>= env
->pb
[2] && eaddr
< env
->pb
[3]) ? 1 : 0;
1375 if (in_plb
^ msr_px
) {
1376 /* Access in protected area */
1378 /* Access is not allowed */
1382 /* Read-write access is allowed */
1383 ctx
->prot
|= PAGE_WRITE
;
1387 case POWERPC_MMU_MPC8xx
:
1389 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1391 case POWERPC_MMU_BOOKE_FSL
:
1393 cpu_abort(env
, "BookE FSL MMU model not implemented\n");
1396 cpu_abort(env
, "Unknown or invalid MMU model\n");
1403 int get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
, target_ulong eaddr
,
1404 int rw
, int access_type
)
1409 if (loglevel
!= 0) {
1410 fprintf(logfile
, "%s\n", __func__
);
1413 if ((access_type
== ACCESS_CODE
&& msr_ir
== 0) ||
1414 (access_type
!= ACCESS_CODE
&& msr_dr
== 0)) {
1415 /* No address translation */
1416 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1419 switch (env
->mmu_model
) {
1420 case POWERPC_MMU_32B
:
1421 case POWERPC_MMU_601
:
1422 case POWERPC_MMU_SOFT_6xx
:
1423 case POWERPC_MMU_SOFT_74xx
:
1424 #if defined(TARGET_PPC64)
1425 case POWERPC_MMU_620
:
1426 case POWERPC_MMU_64B
:
1428 /* Try to find a BAT */
1429 if (env
->nb_BATs
!= 0)
1430 ret
= get_bat(env
, ctx
, eaddr
, rw
, access_type
);
1432 /* We didn't match any BAT entry or don't have BATs */
1433 ret
= get_segment(env
, ctx
, eaddr
, rw
, access_type
);
1436 case POWERPC_MMU_SOFT_4xx
:
1437 case POWERPC_MMU_SOFT_4xx_Z
:
1438 ret
= mmu40x_get_physical_address(env
, ctx
, eaddr
,
1441 case POWERPC_MMU_BOOKE
:
1442 ret
= mmubooke_get_physical_address(env
, ctx
, eaddr
,
1445 case POWERPC_MMU_MPC8xx
:
1447 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1449 case POWERPC_MMU_BOOKE_FSL
:
1451 cpu_abort(env
, "BookE FSL MMU model not implemented\n");
1453 case POWERPC_MMU_REAL
:
1454 cpu_abort(env
, "PowerPC in real mode do not do any translation\n");
1457 cpu_abort(env
, "Unknown or invalid MMU model\n");
1462 if (loglevel
!= 0) {
1463 fprintf(logfile
, "%s address " ADDRX
" => %d " PADDRX
"\n",
1464 __func__
, eaddr
, ret
, ctx
->raddr
);
1471 target_phys_addr_t
cpu_get_phys_page_debug (CPUState
*env
, target_ulong addr
)
1475 if (unlikely(get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) != 0))
1478 return ctx
.raddr
& TARGET_PAGE_MASK
;
1481 /* Perform address translation */
1482 int cpu_ppc_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
1483 int mmu_idx
, int is_softmmu
)
1492 access_type
= ACCESS_CODE
;
1495 /* XXX: put correct access by using cpu_restore_state()
1497 access_type
= ACCESS_INT
;
1498 // access_type = env->access_type;
1500 ret
= get_physical_address(env
, &ctx
, address
, rw
, access_type
);
1502 ret
= tlb_set_page_exec(env
, address
& TARGET_PAGE_MASK
,
1503 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
1504 mmu_idx
, is_softmmu
);
1505 } else if (ret
< 0) {
1506 #if defined (DEBUG_MMU)
1508 cpu_dump_state(env
, logfile
, fprintf
, 0);
1510 if (access_type
== ACCESS_CODE
) {
1513 /* No matches in page tables or TLB */
1514 switch (env
->mmu_model
) {
1515 case POWERPC_MMU_SOFT_6xx
:
1516 env
->exception_index
= POWERPC_EXCP_IFTLB
;
1517 env
->error_code
= 1 << 18;
1518 env
->spr
[SPR_IMISS
] = address
;
1519 env
->spr
[SPR_ICMP
] = 0x80000000 | ctx
.ptem
;
1521 case POWERPC_MMU_SOFT_74xx
:
1522 env
->exception_index
= POWERPC_EXCP_IFTLB
;
1524 case POWERPC_MMU_SOFT_4xx
:
1525 case POWERPC_MMU_SOFT_4xx_Z
:
1526 env
->exception_index
= POWERPC_EXCP_ITLB
;
1527 env
->error_code
= 0;
1528 env
->spr
[SPR_40x_DEAR
] = address
;
1529 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1531 case POWERPC_MMU_32B
:
1532 case POWERPC_MMU_601
:
1533 #if defined(TARGET_PPC64)
1534 case POWERPC_MMU_620
:
1535 case POWERPC_MMU_64B
:
1537 env
->exception_index
= POWERPC_EXCP_ISI
;
1538 env
->error_code
= 0x40000000;
1540 case POWERPC_MMU_BOOKE
:
1542 cpu_abort(env
, "BookE MMU model is not implemented\n");
1544 case POWERPC_MMU_BOOKE_FSL
:
1546 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
1548 case POWERPC_MMU_MPC8xx
:
1550 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1552 case POWERPC_MMU_REAL
:
1553 cpu_abort(env
, "PowerPC in real mode should never raise "
1554 "any MMU exceptions\n");
1557 cpu_abort(env
, "Unknown or invalid MMU model\n");
1562 /* Access rights violation */
1563 env
->exception_index
= POWERPC_EXCP_ISI
;
1564 env
->error_code
= 0x08000000;
1567 /* No execute protection violation */
1568 env
->exception_index
= POWERPC_EXCP_ISI
;
1569 env
->error_code
= 0x10000000;
1572 /* Direct store exception */
1573 /* No code fetch is allowed in direct-store areas */
1574 env
->exception_index
= POWERPC_EXCP_ISI
;
1575 env
->error_code
= 0x10000000;
1577 #if defined(TARGET_PPC64)
1579 /* No match in segment table */
1580 if (env
->mmu_model
== POWERPC_MMU_620
) {
1581 env
->exception_index
= POWERPC_EXCP_ISI
;
1582 /* XXX: this might be incorrect */
1583 env
->error_code
= 0x40000000;
1585 env
->exception_index
= POWERPC_EXCP_ISEG
;
1586 env
->error_code
= 0;
1594 /* No matches in page tables or TLB */
1595 switch (env
->mmu_model
) {
1596 case POWERPC_MMU_SOFT_6xx
:
1598 env
->exception_index
= POWERPC_EXCP_DSTLB
;
1599 env
->error_code
= 1 << 16;
1601 env
->exception_index
= POWERPC_EXCP_DLTLB
;
1602 env
->error_code
= 0;
1604 env
->spr
[SPR_DMISS
] = address
;
1605 env
->spr
[SPR_DCMP
] = 0x80000000 | ctx
.ptem
;
1607 env
->error_code
|= ctx
.key
<< 19;
1608 env
->spr
[SPR_HASH1
] = ctx
.pg_addr
[0];
1609 env
->spr
[SPR_HASH2
] = ctx
.pg_addr
[1];
1611 case POWERPC_MMU_SOFT_74xx
:
1613 env
->exception_index
= POWERPC_EXCP_DSTLB
;
1615 env
->exception_index
= POWERPC_EXCP_DLTLB
;
1618 /* Implement LRU algorithm */
1619 env
->error_code
= ctx
.key
<< 19;
1620 env
->spr
[SPR_TLBMISS
] = (address
& ~((target_ulong
)0x3)) |
1621 ((env
->last_way
+ 1) & (env
->nb_ways
- 1));
1622 env
->spr
[SPR_PTEHI
] = 0x80000000 | ctx
.ptem
;
1624 case POWERPC_MMU_SOFT_4xx
:
1625 case POWERPC_MMU_SOFT_4xx_Z
:
1626 env
->exception_index
= POWERPC_EXCP_DTLB
;
1627 env
->error_code
= 0;
1628 env
->spr
[SPR_40x_DEAR
] = address
;
1630 env
->spr
[SPR_40x_ESR
] = 0x00800000;
1632 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1634 case POWERPC_MMU_32B
:
1635 case POWERPC_MMU_601
:
1636 #if defined(TARGET_PPC64)
1637 case POWERPC_MMU_620
:
1638 case POWERPC_MMU_64B
:
1640 env
->exception_index
= POWERPC_EXCP_DSI
;
1641 env
->error_code
= 0;
1642 env
->spr
[SPR_DAR
] = address
;
1644 env
->spr
[SPR_DSISR
] = 0x42000000;
1646 env
->spr
[SPR_DSISR
] = 0x40000000;
1648 case POWERPC_MMU_MPC8xx
:
1650 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1652 case POWERPC_MMU_BOOKE
:
1654 cpu_abort(env
, "BookE MMU model is not implemented\n");
1656 case POWERPC_MMU_BOOKE_FSL
:
1658 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
1660 case POWERPC_MMU_REAL
:
1661 cpu_abort(env
, "PowerPC in real mode should never raise "
1662 "any MMU exceptions\n");
1665 cpu_abort(env
, "Unknown or invalid MMU model\n");
1670 /* Access rights violation */
1671 env
->exception_index
= POWERPC_EXCP_DSI
;
1672 env
->error_code
= 0;
1673 env
->spr
[SPR_DAR
] = address
;
1675 env
->spr
[SPR_DSISR
] = 0x0A000000;
1677 env
->spr
[SPR_DSISR
] = 0x08000000;
1680 /* Direct store exception */
1681 switch (access_type
) {
1683 /* Floating point load/store */
1684 env
->exception_index
= POWERPC_EXCP_ALIGN
;
1685 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
1686 env
->spr
[SPR_DAR
] = address
;
1689 /* lwarx, ldarx or stwcx. */
1690 env
->exception_index
= POWERPC_EXCP_DSI
;
1691 env
->error_code
= 0;
1692 env
->spr
[SPR_DAR
] = address
;
1694 env
->spr
[SPR_DSISR
] = 0x06000000;
1696 env
->spr
[SPR_DSISR
] = 0x04000000;
1699 /* eciwx or ecowx */
1700 env
->exception_index
= POWERPC_EXCP_DSI
;
1701 env
->error_code
= 0;
1702 env
->spr
[SPR_DAR
] = address
;
1704 env
->spr
[SPR_DSISR
] = 0x06100000;
1706 env
->spr
[SPR_DSISR
] = 0x04100000;
1709 printf("DSI: invalid exception (%d)\n", ret
);
1710 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
1712 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
;
1713 env
->spr
[SPR_DAR
] = address
;
1717 #if defined(TARGET_PPC64)
1719 /* No match in segment table */
1720 if (env
->mmu_model
== POWERPC_MMU_620
) {
1721 env
->exception_index
= POWERPC_EXCP_DSI
;
1722 env
->error_code
= 0;
1723 env
->spr
[SPR_DAR
] = address
;
1724 /* XXX: this might be incorrect */
1726 env
->spr
[SPR_DSISR
] = 0x42000000;
1728 env
->spr
[SPR_DSISR
] = 0x40000000;
1730 env
->exception_index
= POWERPC_EXCP_DSEG
;
1731 env
->error_code
= 0;
1732 env
->spr
[SPR_DAR
] = address
;
1739 printf("%s: set exception to %d %02x\n", __func__
,
1740 env
->exception
, env
->error_code
);
1748 /*****************************************************************************/
1749 /* BATs management */
1750 #if !defined(FLUSH_ALL_TLBS)
1751 static always_inline
void do_invalidate_BAT (CPUPPCState
*env
,
1755 target_ulong base
, end
, page
;
1757 base
= BATu
& ~0x0001FFFF;
1758 end
= base
+ mask
+ 0x00020000;
1759 #if defined (DEBUG_BATS)
1760 if (loglevel
!= 0) {
1761 fprintf(logfile
, "Flush BAT from " ADDRX
" to " ADDRX
" (" ADDRX
")\n",
1765 for (page
= base
; page
!= end
; page
+= TARGET_PAGE_SIZE
)
1766 tlb_flush_page(env
, page
);
1767 #if defined (DEBUG_BATS)
1769 fprintf(logfile
, "Flush done\n");
1774 static always_inline
void dump_store_bat (CPUPPCState
*env
, char ID
,
1775 int ul
, int nr
, target_ulong value
)
1777 #if defined (DEBUG_BATS)
1778 if (loglevel
!= 0) {
1779 fprintf(logfile
, "Set %cBAT%d%c to " ADDRX
" (" ADDRX
")\n",
1780 ID
, nr
, ul
== 0 ? 'u' : 'l', value
, env
->nip
);
1785 target_ulong
do_load_ibatu (CPUPPCState
*env
, int nr
)
1787 return env
->IBAT
[0][nr
];
1790 target_ulong
do_load_ibatl (CPUPPCState
*env
, int nr
)
1792 return env
->IBAT
[1][nr
];
1795 void do_store_ibatu (CPUPPCState
*env
, int nr
, target_ulong value
)
1799 dump_store_bat(env
, 'I', 0, nr
, value
);
1800 if (env
->IBAT
[0][nr
] != value
) {
1801 mask
= (value
<< 15) & 0x0FFE0000UL
;
1802 #if !defined(FLUSH_ALL_TLBS)
1803 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1805 /* When storing valid upper BAT, mask BEPI and BRPN
1806 * and invalidate all TLBs covered by this BAT
1808 mask
= (value
<< 15) & 0x0FFE0000UL
;
1809 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1810 (value
& ~0x0001FFFFUL
& ~mask
);
1811 env
->IBAT
[1][nr
] = (env
->IBAT
[1][nr
] & 0x0000007B) |
1812 (env
->IBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1813 #if !defined(FLUSH_ALL_TLBS)
1814 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1821 void do_store_ibatl (CPUPPCState
*env
, int nr
, target_ulong value
)
1823 dump_store_bat(env
, 'I', 1, nr
, value
);
1824 env
->IBAT
[1][nr
] = value
;
1827 target_ulong
do_load_dbatu (CPUPPCState
*env
, int nr
)
1829 return env
->DBAT
[0][nr
];
1832 target_ulong
do_load_dbatl (CPUPPCState
*env
, int nr
)
1834 return env
->DBAT
[1][nr
];
1837 void do_store_dbatu (CPUPPCState
*env
, int nr
, target_ulong value
)
1841 dump_store_bat(env
, 'D', 0, nr
, value
);
1842 if (env
->DBAT
[0][nr
] != value
) {
1843 /* When storing valid upper BAT, mask BEPI and BRPN
1844 * and invalidate all TLBs covered by this BAT
1846 mask
= (value
<< 15) & 0x0FFE0000UL
;
1847 #if !defined(FLUSH_ALL_TLBS)
1848 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1850 mask
= (value
<< 15) & 0x0FFE0000UL
;
1851 env
->DBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1852 (value
& ~0x0001FFFFUL
& ~mask
);
1853 env
->DBAT
[1][nr
] = (env
->DBAT
[1][nr
] & 0x0000007B) |
1854 (env
->DBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1855 #if !defined(FLUSH_ALL_TLBS)
1856 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1863 void do_store_dbatl (CPUPPCState
*env
, int nr
, target_ulong value
)
1865 dump_store_bat(env
, 'D', 1, nr
, value
);
1866 env
->DBAT
[1][nr
] = value
;
1869 void do_store_ibatu_601 (CPUPPCState
*env
, int nr
, target_ulong value
)
1874 dump_store_bat(env
, 'I', 0, nr
, value
);
1875 if (env
->IBAT
[0][nr
] != value
) {
1877 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1878 if (env
->IBAT
[1][nr
] & 0x40) {
1879 /* Invalidate BAT only if it is valid */
1880 #if !defined(FLUSH_ALL_TLBS)
1881 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1886 /* When storing valid upper BAT, mask BEPI and BRPN
1887 * and invalidate all TLBs covered by this BAT
1889 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1890 (value
& ~0x0001FFFFUL
& ~mask
);
1891 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1892 if (env
->IBAT
[1][nr
] & 0x40) {
1893 #if !defined(FLUSH_ALL_TLBS)
1894 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1899 #if defined(FLUSH_ALL_TLBS)
1906 void do_store_ibatl_601 (CPUPPCState
*env
, int nr
, target_ulong value
)
1911 dump_store_bat(env
, 'I', 1, nr
, value
);
1912 if (env
->IBAT
[1][nr
] != value
) {
1914 if (env
->IBAT
[1][nr
] & 0x40) {
1915 #if !defined(FLUSH_ALL_TLBS)
1916 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1917 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1923 #if !defined(FLUSH_ALL_TLBS)
1924 mask
= (value
<< 17) & 0x0FFE0000UL
;
1925 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1930 env
->IBAT
[1][nr
] = value
;
1931 env
->DBAT
[1][nr
] = value
;
1932 #if defined(FLUSH_ALL_TLBS)
1939 /*****************************************************************************/
1940 /* TLB management */
1941 void ppc_tlb_invalidate_all (CPUPPCState
*env
)
1943 switch (env
->mmu_model
) {
1944 case POWERPC_MMU_SOFT_6xx
:
1945 case POWERPC_MMU_SOFT_74xx
:
1946 ppc6xx_tlb_invalidate_all(env
);
1948 case POWERPC_MMU_SOFT_4xx
:
1949 case POWERPC_MMU_SOFT_4xx_Z
:
1950 ppc4xx_tlb_invalidate_all(env
);
1952 case POWERPC_MMU_REAL
:
1953 cpu_abort(env
, "No TLB for PowerPC 4xx in real mode\n");
1955 case POWERPC_MMU_MPC8xx
:
1957 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
1959 case POWERPC_MMU_BOOKE
:
1961 cpu_abort(env
, "BookE MMU model is not implemented\n");
1963 case POWERPC_MMU_BOOKE_FSL
:
1965 cpu_abort(env
, "BookE MMU model is not implemented\n");
1967 case POWERPC_MMU_32B
:
1968 case POWERPC_MMU_601
:
1969 #if defined(TARGET_PPC64)
1970 case POWERPC_MMU_620
:
1971 case POWERPC_MMU_64B
:
1972 #endif /* defined(TARGET_PPC64) */
1977 cpu_abort(env
, "Unknown MMU model\n");
1982 void ppc_tlb_invalidate_one (CPUPPCState
*env
, target_ulong addr
)
1984 #if !defined(FLUSH_ALL_TLBS)
1985 addr
&= TARGET_PAGE_MASK
;
1986 switch (env
->mmu_model
) {
1987 case POWERPC_MMU_SOFT_6xx
:
1988 case POWERPC_MMU_SOFT_74xx
:
1989 ppc6xx_tlb_invalidate_virt(env
, addr
, 0);
1990 if (env
->id_tlbs
== 1)
1991 ppc6xx_tlb_invalidate_virt(env
, addr
, 1);
1993 case POWERPC_MMU_SOFT_4xx
:
1994 case POWERPC_MMU_SOFT_4xx_Z
:
1995 ppc4xx_tlb_invalidate_virt(env
, addr
, env
->spr
[SPR_40x_PID
]);
1997 case POWERPC_MMU_REAL
:
1998 cpu_abort(env
, "No TLB for PowerPC 4xx in real mode\n");
2000 case POWERPC_MMU_MPC8xx
:
2002 cpu_abort(env
, "MPC8xx MMU model is not implemented\n");
2004 case POWERPC_MMU_BOOKE
:
2006 cpu_abort(env
, "BookE MMU model is not implemented\n");
2008 case POWERPC_MMU_BOOKE_FSL
:
2010 cpu_abort(env
, "BookE FSL MMU model is not implemented\n");
2012 case POWERPC_MMU_32B
:
2013 case POWERPC_MMU_601
:
2014 /* tlbie invalidate TLBs for all segments */
2015 addr
&= ~((target_ulong
)-1ULL << 28);
2016 /* XXX: this case should be optimized,
2017 * giving a mask to tlb_flush_page
2019 tlb_flush_page(env
, addr
| (0x0 << 28));
2020 tlb_flush_page(env
, addr
| (0x1 << 28));
2021 tlb_flush_page(env
, addr
| (0x2 << 28));
2022 tlb_flush_page(env
, addr
| (0x3 << 28));
2023 tlb_flush_page(env
, addr
| (0x4 << 28));
2024 tlb_flush_page(env
, addr
| (0x5 << 28));
2025 tlb_flush_page(env
, addr
| (0x6 << 28));
2026 tlb_flush_page(env
, addr
| (0x7 << 28));
2027 tlb_flush_page(env
, addr
| (0x8 << 28));
2028 tlb_flush_page(env
, addr
| (0x9 << 28));
2029 tlb_flush_page(env
, addr
| (0xA << 28));
2030 tlb_flush_page(env
, addr
| (0xB << 28));
2031 tlb_flush_page(env
, addr
| (0xC << 28));
2032 tlb_flush_page(env
, addr
| (0xD << 28));
2033 tlb_flush_page(env
, addr
| (0xE << 28));
2034 tlb_flush_page(env
, addr
| (0xF << 28));
2036 #if defined(TARGET_PPC64)
2037 case POWERPC_MMU_620
:
2038 case POWERPC_MMU_64B
:
2039 /* tlbie invalidate TLBs for all segments */
2040 /* XXX: given the fact that there are too many segments to invalidate,
2041 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
2042 * we just invalidate all TLBs
2046 #endif /* defined(TARGET_PPC64) */
2049 cpu_abort(env
, "Unknown MMU model\n");
2053 ppc_tlb_invalidate_all(env
);
2057 /*****************************************************************************/
2058 /* Special registers manipulation */
2059 #if defined(TARGET_PPC64)
2060 target_ulong
ppc_load_asr (CPUPPCState
*env
)
2065 void ppc_store_asr (CPUPPCState
*env
, target_ulong value
)
2067 if (env
->asr
!= value
) {
2074 target_ulong
do_load_sdr1 (CPUPPCState
*env
)
2079 void do_store_sdr1 (CPUPPCState
*env
, target_ulong value
)
2081 #if defined (DEBUG_MMU)
2082 if (loglevel
!= 0) {
2083 fprintf(logfile
, "%s: " ADDRX
"\n", __func__
, value
);
2086 if (env
->sdr1
!= value
) {
2087 /* XXX: for PowerPC 64, should check that the HTABSIZE value
2096 target_ulong
do_load_sr (CPUPPCState
*env
, int srnum
)
2098 return env
->sr
[srnum
];
2102 void do_store_sr (CPUPPCState
*env
, int srnum
, target_ulong value
)
2104 #if defined (DEBUG_MMU)
2105 if (loglevel
!= 0) {
2106 fprintf(logfile
, "%s: reg=%d " ADDRX
" " ADDRX
"\n",
2107 __func__
, srnum
, value
, env
->sr
[srnum
]);
2110 if (env
->sr
[srnum
] != value
) {
2111 env
->sr
[srnum
] = value
;
2112 #if !defined(FLUSH_ALL_TLBS) && 0
2114 target_ulong page
, end
;
2115 /* Invalidate 256 MB of virtual memory */
2116 page
= (16 << 20) * srnum
;
2117 end
= page
+ (16 << 20);
2118 for (; page
!= end
; page
+= TARGET_PAGE_SIZE
)
2119 tlb_flush_page(env
, page
);
2126 #endif /* !defined (CONFIG_USER_ONLY) */
2128 /* GDBstub can read and write MSR... */
2129 void ppc_store_msr (CPUPPCState
*env
, target_ulong value
)
2131 hreg_store_msr(env
, value
, 0);
2134 /*****************************************************************************/
2135 /* Exception processing */
2136 #if defined (CONFIG_USER_ONLY)
2137 void do_interrupt (CPUState
*env
)
2139 env
->exception_index
= POWERPC_EXCP_NONE
;
2140 env
->error_code
= 0;
2143 void ppc_hw_interrupt (CPUState
*env
)
2145 env
->exception_index
= POWERPC_EXCP_NONE
;
2146 env
->error_code
= 0;
2148 #else /* defined (CONFIG_USER_ONLY) */
2149 static always_inline
void dump_syscall (CPUState
*env
)
2151 fprintf(logfile
, "syscall r0=" REGX
" r3=" REGX
" r4=" REGX
2152 " r5=" REGX
" r6=" REGX
" nip=" ADDRX
"\n",
2153 ppc_dump_gpr(env
, 0), ppc_dump_gpr(env
, 3), ppc_dump_gpr(env
, 4),
2154 ppc_dump_gpr(env
, 5), ppc_dump_gpr(env
, 6), env
->nip
);
2157 /* Note that this function should be greatly optimized
2158 * when called with a constant excp, from ppc_hw_interrupt
2160 static always_inline
void powerpc_excp (CPUState
*env
,
2161 int excp_model
, int excp
)
2163 target_ulong msr
, new_msr
, vector
;
2164 int srr0
, srr1
, asrr0
, asrr1
;
2165 int lpes0
, lpes1
, lev
;
2168 /* XXX: find a suitable condition to enable the hypervisor mode */
2169 lpes0
= (env
->spr
[SPR_LPCR
] >> 1) & 1;
2170 lpes1
= (env
->spr
[SPR_LPCR
] >> 2) & 1;
2172 /* Those values ensure we won't enter the hypervisor mode */
2177 if (loglevel
& CPU_LOG_INT
) {
2178 fprintf(logfile
, "Raise exception at " ADDRX
" => %08x (%02x)\n",
2179 env
->nip
, excp
, env
->error_code
);
2187 msr
&= ~((target_ulong
)0x783F0000);
2189 case POWERPC_EXCP_NONE
:
2190 /* Should never happen */
2192 case POWERPC_EXCP_CRITICAL
: /* Critical input */
2193 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2194 switch (excp_model
) {
2195 case POWERPC_EXCP_40x
:
2196 srr0
= SPR_40x_SRR2
;
2197 srr1
= SPR_40x_SRR3
;
2199 case POWERPC_EXCP_BOOKE
:
2200 srr0
= SPR_BOOKE_CSRR0
;
2201 srr1
= SPR_BOOKE_CSRR1
;
2203 case POWERPC_EXCP_G2
:
2209 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
2211 /* Machine check exception is not enabled.
2212 * Enter checkstop state.
2214 if (loglevel
!= 0) {
2215 fprintf(logfile
, "Machine check while not allowed. "
2216 "Entering checkstop state\n");
2218 fprintf(stderr
, "Machine check while not allowed. "
2219 "Entering checkstop state\n");
2222 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
2224 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2225 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
2227 /* XXX: find a suitable condition to enable the hypervisor mode */
2228 new_msr
|= (target_ulong
)MSR_HVB
;
2230 /* XXX: should also have something loaded in DAR / DSISR */
2231 switch (excp_model
) {
2232 case POWERPC_EXCP_40x
:
2233 srr0
= SPR_40x_SRR2
;
2234 srr1
= SPR_40x_SRR3
;
2236 case POWERPC_EXCP_BOOKE
:
2237 srr0
= SPR_BOOKE_MCSRR0
;
2238 srr1
= SPR_BOOKE_MCSRR1
;
2239 asrr0
= SPR_BOOKE_CSRR0
;
2240 asrr1
= SPR_BOOKE_CSRR1
;
2246 case POWERPC_EXCP_DSI
: /* Data storage exception */
2247 #if defined (DEBUG_EXCEPTIONS)
2248 if (loglevel
!= 0) {
2249 fprintf(logfile
, "DSI exception: DSISR=" ADDRX
" DAR=" ADDRX
"\n",
2250 env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
2253 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2255 new_msr
|= (target_ulong
)MSR_HVB
;
2257 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
2258 #if defined (DEBUG_EXCEPTIONS)
2259 if (loglevel
!= 0) {
2260 fprintf(logfile
, "ISI exception: msr=" ADDRX
", nip=" ADDRX
"\n",
2264 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2266 new_msr
|= (target_ulong
)MSR_HVB
;
2267 msr
|= env
->error_code
;
2269 case POWERPC_EXCP_EXTERNAL
: /* External input */
2270 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2272 new_msr
|= (target_ulong
)MSR_HVB
;
2274 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
2275 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2277 new_msr
|= (target_ulong
)MSR_HVB
;
2278 /* XXX: this is false */
2279 /* Get rS/rD and rA from faulting opcode */
2280 env
->spr
[SPR_DSISR
] |= (ldl_code((env
->nip
- 4)) & 0x03FF0000) >> 16;
2282 case POWERPC_EXCP_PROGRAM
: /* Program exception */
2283 switch (env
->error_code
& ~0xF) {
2284 case POWERPC_EXCP_FP
:
2285 if ((msr_fe0
== 0 && msr_fe1
== 0) || msr_fp
== 0) {
2286 #if defined (DEBUG_EXCEPTIONS)
2287 if (loglevel
!= 0) {
2288 fprintf(logfile
, "Ignore floating point exception\n");
2291 env
->exception_index
= POWERPC_EXCP_NONE
;
2292 env
->error_code
= 0;
2295 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2297 new_msr
|= (target_ulong
)MSR_HVB
;
2299 if (msr_fe0
== msr_fe1
)
2303 case POWERPC_EXCP_INVAL
:
2304 #if defined (DEBUG_EXCEPTIONS)
2305 if (loglevel
!= 0) {
2306 fprintf(logfile
, "Invalid instruction at " ADDRX
"\n",
2310 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2312 new_msr
|= (target_ulong
)MSR_HVB
;
2315 case POWERPC_EXCP_PRIV
:
2316 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2318 new_msr
|= (target_ulong
)MSR_HVB
;
2321 case POWERPC_EXCP_TRAP
:
2322 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2324 new_msr
|= (target_ulong
)MSR_HVB
;
2328 /* Should never occur */
2329 cpu_abort(env
, "Invalid program exception %d. Aborting\n",
2334 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
2335 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2337 new_msr
|= (target_ulong
)MSR_HVB
;
2339 case POWERPC_EXCP_SYSCALL
: /* System call exception */
2340 /* NOTE: this is a temporary hack to support graphics OSI
2341 calls from the MOL driver */
2342 /* XXX: To be removed */
2343 if (env
->gpr
[3] == 0x113724fa && env
->gpr
[4] == 0x77810f9b &&
2345 if (env
->osi_call(env
) != 0) {
2346 env
->exception_index
= POWERPC_EXCP_NONE
;
2347 env
->error_code
= 0;
2351 if (loglevel
& CPU_LOG_INT
) {
2354 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2355 lev
= env
->error_code
;
2356 if (lev
== 1 || (lpes0
== 0 && lpes1
== 0))
2357 new_msr
|= (target_ulong
)MSR_HVB
;
2359 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
2360 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2362 case POWERPC_EXCP_DECR
: /* Decrementer exception */
2363 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2365 new_msr
|= (target_ulong
)MSR_HVB
;
2367 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
2369 #if defined (DEBUG_EXCEPTIONS)
2371 fprintf(logfile
, "FIT exception\n");
2373 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2375 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
2376 #if defined (DEBUG_EXCEPTIONS)
2378 fprintf(logfile
, "WDT exception\n");
2380 switch (excp_model
) {
2381 case POWERPC_EXCP_BOOKE
:
2382 srr0
= SPR_BOOKE_CSRR0
;
2383 srr1
= SPR_BOOKE_CSRR1
;
2388 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2390 case POWERPC_EXCP_DTLB
: /* Data TLB error */
2391 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2393 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
2394 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2396 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
2397 switch (excp_model
) {
2398 case POWERPC_EXCP_BOOKE
:
2399 srr0
= SPR_BOOKE_DSRR0
;
2400 srr1
= SPR_BOOKE_DSRR1
;
2401 asrr0
= SPR_BOOKE_CSRR0
;
2402 asrr1
= SPR_BOOKE_CSRR1
;
2408 cpu_abort(env
, "Debug exception is not implemented yet !\n");
2410 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavailable */
2411 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2413 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data interrupt */
2415 cpu_abort(env
, "Embedded floating point data exception "
2416 "is not implemented yet !\n");
2418 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round interrupt */
2420 cpu_abort(env
, "Embedded floating point round exception "
2421 "is not implemented yet !\n");
2423 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor interrupt */
2424 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2427 "Performance counter exception is not implemented yet !\n");
2429 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
2432 "Embedded doorbell interrupt is not implemented yet !\n");
2434 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
2435 switch (excp_model
) {
2436 case POWERPC_EXCP_BOOKE
:
2437 srr0
= SPR_BOOKE_CSRR0
;
2438 srr1
= SPR_BOOKE_CSRR1
;
2444 cpu_abort(env
, "Embedded doorbell critical interrupt "
2445 "is not implemented yet !\n");
2447 case POWERPC_EXCP_RESET
: /* System reset exception */
2448 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2450 /* XXX: find a suitable condition to enable the hypervisor mode */
2451 new_msr
|= (target_ulong
)MSR_HVB
;
2454 case POWERPC_EXCP_DSEG
: /* Data segment exception */
2455 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2457 new_msr
|= (target_ulong
)MSR_HVB
;
2459 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
2460 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2462 new_msr
|= (target_ulong
)MSR_HVB
;
2464 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
2467 new_msr
|= (target_ulong
)MSR_HVB
;
2469 case POWERPC_EXCP_TRACE
: /* Trace exception */
2470 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2472 new_msr
|= (target_ulong
)MSR_HVB
;
2474 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
2477 new_msr
|= (target_ulong
)MSR_HVB
;
2479 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage exception */
2482 new_msr
|= (target_ulong
)MSR_HVB
;
2484 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
2487 new_msr
|= (target_ulong
)MSR_HVB
;
2489 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment exception */
2492 new_msr
|= (target_ulong
)MSR_HVB
;
2494 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
2495 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2497 new_msr
|= (target_ulong
)MSR_HVB
;
2499 case POWERPC_EXCP_PIT
: /* Programmable interval timer interrupt */
2500 #if defined (DEBUG_EXCEPTIONS)
2502 fprintf(logfile
, "PIT exception\n");
2504 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2506 case POWERPC_EXCP_IO
: /* IO error exception */
2508 cpu_abort(env
, "601 IO error exception is not implemented yet !\n");
2510 case POWERPC_EXCP_RUNM
: /* Run mode exception */
2512 cpu_abort(env
, "601 run mode exception is not implemented yet !\n");
2514 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
2516 cpu_abort(env
, "602 emulation trap exception "
2517 "is not implemented yet !\n");
2519 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
2520 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2521 if (lpes1
== 0) /* XXX: check this */
2522 new_msr
|= (target_ulong
)MSR_HVB
;
2523 switch (excp_model
) {
2524 case POWERPC_EXCP_602
:
2525 case POWERPC_EXCP_603
:
2526 case POWERPC_EXCP_603E
:
2527 case POWERPC_EXCP_G2
:
2529 case POWERPC_EXCP_7x5
:
2531 case POWERPC_EXCP_74xx
:
2534 cpu_abort(env
, "Invalid instruction TLB miss exception\n");
2538 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
2539 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2540 if (lpes1
== 0) /* XXX: check this */
2541 new_msr
|= (target_ulong
)MSR_HVB
;
2542 switch (excp_model
) {
2543 case POWERPC_EXCP_602
:
2544 case POWERPC_EXCP_603
:
2545 case POWERPC_EXCP_603E
:
2546 case POWERPC_EXCP_G2
:
2548 case POWERPC_EXCP_7x5
:
2550 case POWERPC_EXCP_74xx
:
2553 cpu_abort(env
, "Invalid data load TLB miss exception\n");
2557 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
2558 new_msr
&= ~((target_ulong
)1 << MSR_RI
); /* XXX: check this */
2559 if (lpes1
== 0) /* XXX: check this */
2560 new_msr
|= (target_ulong
)MSR_HVB
;
2561 switch (excp_model
) {
2562 case POWERPC_EXCP_602
:
2563 case POWERPC_EXCP_603
:
2564 case POWERPC_EXCP_603E
:
2565 case POWERPC_EXCP_G2
:
2567 /* Swap temporary saved registers with GPRs */
2568 if (!(new_msr
& ((target_ulong
)1 << MSR_TGPR
))) {
2569 new_msr
|= (target_ulong
)1 << MSR_TGPR
;
2570 hreg_swap_gpr_tgpr(env
);
2573 case POWERPC_EXCP_7x5
:
2575 #if defined (DEBUG_SOFTWARE_TLB)
2576 if (loglevel
!= 0) {
2577 const unsigned char *es
;
2578 target_ulong
*miss
, *cmp
;
2580 if (excp
== POWERPC_EXCP_IFTLB
) {
2583 miss
= &env
->spr
[SPR_IMISS
];
2584 cmp
= &env
->spr
[SPR_ICMP
];
2586 if (excp
== POWERPC_EXCP_DLTLB
)
2591 miss
= &env
->spr
[SPR_DMISS
];
2592 cmp
= &env
->spr
[SPR_DCMP
];
2594 fprintf(logfile
, "6xx %sTLB miss: %cM " ADDRX
" %cC " ADDRX
2595 " H1 " ADDRX
" H2 " ADDRX
" %08x\n",
2596 es
, en
, *miss
, en
, *cmp
,
2597 env
->spr
[SPR_HASH1
], env
->spr
[SPR_HASH2
],
2601 msr
|= env
->crf
[0] << 28;
2602 msr
|= env
->error_code
; /* key, D/I, S/L bits */
2603 /* Set way using a LRU mechanism */
2604 msr
|= ((env
->last_way
+ 1) & (env
->nb_ways
- 1)) << 17;
2606 case POWERPC_EXCP_74xx
:
2608 #if defined (DEBUG_SOFTWARE_TLB)
2609 if (loglevel
!= 0) {
2610 const unsigned char *es
;
2611 target_ulong
*miss
, *cmp
;
2613 if (excp
== POWERPC_EXCP_IFTLB
) {
2616 miss
= &env
->spr
[SPR_TLBMISS
];
2617 cmp
= &env
->spr
[SPR_PTEHI
];
2619 if (excp
== POWERPC_EXCP_DLTLB
)
2624 miss
= &env
->spr
[SPR_TLBMISS
];
2625 cmp
= &env
->spr
[SPR_PTEHI
];
2627 fprintf(logfile
, "74xx %sTLB miss: %cM " ADDRX
" %cC " ADDRX
2629 es
, en
, *miss
, en
, *cmp
, env
->error_code
);
2632 msr
|= env
->error_code
; /* key bit */
2635 cpu_abort(env
, "Invalid data store TLB miss exception\n");
2639 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
2641 cpu_abort(env
, "Floating point assist exception "
2642 "is not implemented yet !\n");
2644 case POWERPC_EXCP_DABR
: /* Data address breakpoint */
2646 cpu_abort(env
, "DABR exception is not implemented yet !\n");
2648 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
2650 cpu_abort(env
, "IABR exception is not implemented yet !\n");
2652 case POWERPC_EXCP_SMI
: /* System management interrupt */
2654 cpu_abort(env
, "SMI exception is not implemented yet !\n");
2656 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
2658 cpu_abort(env
, "Thermal management exception "
2659 "is not implemented yet !\n");
2661 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor interrupt */
2662 new_msr
&= ~((target_ulong
)1 << MSR_RI
);
2664 new_msr
|= (target_ulong
)MSR_HVB
;
2667 "Performance counter exception is not implemented yet !\n");
2669 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
2671 cpu_abort(env
, "VPU assist exception is not implemented yet !\n");
2673 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
2676 "970 soft-patch exception is not implemented yet !\n");
2678 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
2681 "970 maintenance exception is not implemented yet !\n");
2683 case POWERPC_EXCP_MEXTBR
: /* Maskable external breakpoint */
2685 cpu_abort(env
, "Maskable external exception "
2686 "is not implemented yet !\n");
2688 case POWERPC_EXCP_NMEXTBR
: /* Non maskable external breakpoint */
2690 cpu_abort(env
, "Non maskable external exception "
2691 "is not implemented yet !\n");
2695 cpu_abort(env
, "Invalid PowerPC exception %d. Aborting\n", excp
);
2698 /* save current instruction location */
2699 env
->spr
[srr0
] = env
->nip
- 4;
2702 /* save next instruction location */
2703 env
->spr
[srr0
] = env
->nip
;
2707 env
->spr
[srr1
] = msr
;
2708 /* If any alternate SRR register are defined, duplicate saved values */
2710 env
->spr
[asrr0
] = env
->spr
[srr0
];
2712 env
->spr
[asrr1
] = env
->spr
[srr1
];
2713 /* If we disactivated any translation, flush TLBs */
2714 if (new_msr
& ((1 << MSR_IR
) | (1 << MSR_DR
)))
2716 /* reload MSR with correct bits */
2717 new_msr
&= ~((target_ulong
)1 << MSR_EE
);
2718 new_msr
&= ~((target_ulong
)1 << MSR_PR
);
2719 new_msr
&= ~((target_ulong
)1 << MSR_FP
);
2720 new_msr
&= ~((target_ulong
)1 << MSR_FE0
);
2721 new_msr
&= ~((target_ulong
)1 << MSR_SE
);
2722 new_msr
&= ~((target_ulong
)1 << MSR_BE
);
2723 new_msr
&= ~((target_ulong
)1 << MSR_FE1
);
2724 new_msr
&= ~((target_ulong
)1 << MSR_IR
);
2725 new_msr
&= ~((target_ulong
)1 << MSR_DR
);
2726 #if 0 /* Fix this: not on all targets */
2727 new_msr
&= ~((target_ulong
)1 << MSR_PMM
);
2729 new_msr
&= ~((target_ulong
)1 << MSR_LE
);
2731 new_msr
|= (target_ulong
)1 << MSR_LE
;
2733 new_msr
&= ~((target_ulong
)1 << MSR_LE
);
2734 /* Jump to handler */
2735 vector
= env
->excp_vectors
[excp
];
2736 if (vector
== (target_ulong
)-1ULL) {
2737 cpu_abort(env
, "Raised an exception without defined vector %d\n",
2740 vector
|= env
->excp_prefix
;
2741 #if defined(TARGET_PPC64)
2742 if (excp_model
== POWERPC_EXCP_BOOKE
) {
2744 new_msr
&= ~((target_ulong
)1 << MSR_CM
);
2745 vector
= (uint32_t)vector
;
2747 new_msr
|= (target_ulong
)1 << MSR_CM
;
2751 new_msr
&= ~((target_ulong
)1 << MSR_SF
);
2752 vector
= (uint32_t)vector
;
2754 new_msr
|= (target_ulong
)1 << MSR_SF
;
2758 /* XXX: we don't use hreg_store_msr here as already have treated
2759 * any special case that could occur. Just store MSR and update hflags
2761 env
->msr
= new_msr
& env
->msr_mask
;
2762 hreg_compute_hflags(env
);
2764 /* Reset exception state */
2765 env
->exception_index
= POWERPC_EXCP_NONE
;
2766 env
->error_code
= 0;
2769 void do_interrupt (CPUState
*env
)
2771 powerpc_excp(env
, env
->excp_model
, env
->exception_index
);
2774 void ppc_hw_interrupt (CPUPPCState
*env
)
2779 if (loglevel
& CPU_LOG_INT
) {
2780 fprintf(logfile
, "%s: %p pending %08x req %08x me %d ee %d\n",
2781 __func__
, env
, env
->pending_interrupts
,
2782 env
->interrupt_request
, (int)msr_me
, (int)msr_ee
);
2785 /* External reset */
2786 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_RESET
)) {
2787 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_RESET
);
2788 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_RESET
);
2791 /* Machine check exception */
2792 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_MCK
)) {
2793 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_MCK
);
2794 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_MCHECK
);
2798 /* External debug exception */
2799 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DEBUG
)) {
2800 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DEBUG
);
2801 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DEBUG
);
2806 /* XXX: find a suitable condition to enable the hypervisor mode */
2807 hdice
= env
->spr
[SPR_LPCR
] & 1;
2811 if ((msr_ee
!= 0 || msr_hv
== 0 || msr_pr
!= 0) && hdice
!= 0) {
2812 /* Hypervisor decrementer exception */
2813 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_HDECR
)) {
2814 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_HDECR
);
2815 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_HDECR
);
2820 /* External critical interrupt */
2821 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CEXT
)) {
2822 /* Taking a critical external interrupt does not clear the external
2823 * critical interrupt status
2826 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CEXT
);
2828 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_CRITICAL
);
2833 /* Watchdog timer on embedded PowerPC */
2834 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_WDT
)) {
2835 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_WDT
);
2836 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_WDT
);
2839 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CDOORBELL
)) {
2840 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CDOORBELL
);
2841 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DOORCI
);
2844 /* Fixed interval timer on embedded PowerPC */
2845 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_FIT
)) {
2846 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_FIT
);
2847 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_FIT
);
2850 /* Programmable interval timer on embedded PowerPC */
2851 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PIT
)) {
2852 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PIT
);
2853 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_PIT
);
2856 /* Decrementer exception */
2857 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DECR
)) {
2858 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DECR
);
2859 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DECR
);
2862 /* External interrupt */
2863 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_EXT
)) {
2864 /* Taking an external interrupt does not clear the external
2868 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_EXT
);
2870 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_EXTERNAL
);
2873 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DOORBELL
)) {
2874 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DOORBELL
);
2875 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DOORI
);
2878 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PERFM
)) {
2879 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PERFM
);
2880 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_PERFM
);
2883 /* Thermal interrupt */
2884 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_THERM
)) {
2885 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_THERM
);
2886 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_THERM
);
2891 #endif /* !CONFIG_USER_ONLY */
2893 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
)
2903 fprintf(f
, "Return from exception at " ADDRX
" with flags " ADDRX
"\n",
2907 void cpu_ppc_reset (void *opaque
)
2913 msr
= (target_ulong
)0;
2915 /* XXX: find a suitable condition to enable the hypervisor mode */
2916 msr
|= (target_ulong
)MSR_HVB
;
2918 msr
|= (target_ulong
)0 << MSR_AP
; /* TO BE CHECKED */
2919 msr
|= (target_ulong
)0 << MSR_SA
; /* TO BE CHECKED */
2920 msr
|= (target_ulong
)1 << MSR_EP
;
2921 #if defined (DO_SINGLE_STEP) && 0
2922 /* Single step trace mode */
2923 msr
|= (target_ulong
)1 << MSR_SE
;
2924 msr
|= (target_ulong
)1 << MSR_BE
;
2926 #if defined(CONFIG_USER_ONLY)
2927 msr
|= (target_ulong
)1 << MSR_FP
; /* Allow floating point usage */
2928 msr
|= (target_ulong
)1 << MSR_PR
;
2930 env
->nip
= env
->hreset_vector
| env
->excp_prefix
;
2931 if (env
->mmu_model
!= POWERPC_MMU_REAL
)
2932 ppc_tlb_invalidate_all(env
);
2935 hreg_compute_hflags(env
);
2936 env
->reserve
= (target_ulong
)-1ULL;
2937 /* Be sure no exception or interrupt is pending */
2938 env
->pending_interrupts
= 0;
2939 env
->exception_index
= POWERPC_EXCP_NONE
;
2940 env
->error_code
= 0;
2941 /* Flush all TLBs */
2945 CPUPPCState
*cpu_ppc_init (const char *cpu_model
)
2948 const ppc_def_t
*def
;
2950 def
= cpu_ppc_find_by_name(cpu_model
);
2954 env
= qemu_mallocz(sizeof(CPUPPCState
));
2958 ppc_translate_init();
2959 env
->cpu_model_str
= cpu_model
;
2960 cpu_ppc_register_internal(env
, def
);
2965 void cpu_ppc_close (CPUPPCState
*env
)
2967 /* Should also remove all opcode tables... */