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
33 //#define DEBUG_SOFTWARE_TLB
34 //#define DEBUG_EXCEPTIONS
35 //#define FLUSH_ALL_TLBS
37 /*****************************************************************************/
38 /* PowerPC MMU emulation */
40 #if defined(CONFIG_USER_ONLY)
41 int cpu_ppc_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
42 int is_user
, int is_softmmu
)
44 int exception
, error_code
;
47 exception
= POWERPC_EXCP_ISI
;
48 error_code
= 0x40000000;
50 exception
= POWERPC_EXCP_DSI
;
51 error_code
= 0x40000000;
53 error_code
|= 0x02000000;
54 env
->spr
[SPR_DAR
] = address
;
55 env
->spr
[SPR_DSISR
] = error_code
;
57 env
->exception_index
= exception
;
58 env
->error_code
= error_code
;
63 target_phys_addr_t
cpu_get_phys_page_debug (CPUState
*env
, target_ulong addr
)
69 /* Common routines used by software and hardware TLBs emulation */
70 static inline int pte_is_valid (target_ulong pte0
)
72 return pte0
& 0x80000000 ? 1 : 0;
75 static inline void pte_invalidate (target_ulong
*pte0
)
80 #if defined(TARGET_PPC64)
81 static inline int pte64_is_valid (target_ulong pte0
)
83 return pte0
& 0x0000000000000001ULL
? 1 : 0;
86 static inline void pte64_invalidate (target_ulong
*pte0
)
88 *pte0
&= ~0x0000000000000001ULL
;
92 #define PTE_PTEM_MASK 0x7FFFFFBF
93 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
94 #if defined(TARGET_PPC64)
95 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
96 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
99 static inline int _pte_check (mmu_ctx_t
*ctx
, int is_64b
,
100 target_ulong pte0
, target_ulong pte1
,
103 target_ulong ptem
, mmask
;
104 int access
, ret
, pteh
, ptev
;
108 /* Check validity and table match */
109 #if defined(TARGET_PPC64)
111 ptev
= pte64_is_valid(pte0
);
112 pteh
= (pte0
>> 1) & 1;
116 ptev
= pte_is_valid(pte0
);
117 pteh
= (pte0
>> 6) & 1;
119 if (ptev
&& h
== pteh
) {
120 /* Check vsid & api */
121 #if defined(TARGET_PPC64)
123 ptem
= pte0
& PTE64_PTEM_MASK
;
124 mmask
= PTE64_CHECK_MASK
;
128 ptem
= pte0
& PTE_PTEM_MASK
;
129 mmask
= PTE_CHECK_MASK
;
131 if (ptem
== ctx
->ptem
) {
132 if (ctx
->raddr
!= (target_ulong
)-1) {
133 /* all matches should have equal RPN, WIMG & PP */
134 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
136 fprintf(logfile
, "Bad RPN/WIMG/PP\n");
140 /* Compute access rights */
143 if ((pte1
& 0x00000003) != 0x3)
144 access
|= PAGE_WRITE
;
146 switch (pte1
& 0x00000003) {
155 access
= PAGE_READ
| PAGE_WRITE
;
159 /* Keep the matching PTE informations */
162 if ((rw
== 0 && (access
& PAGE_READ
)) ||
163 (rw
== 1 && (access
& PAGE_WRITE
))) {
165 #if defined (DEBUG_MMU)
167 fprintf(logfile
, "PTE access granted !\n");
171 /* Access right violation */
172 #if defined (DEBUG_MMU)
174 fprintf(logfile
, "PTE access rejected\n");
184 static int pte32_check (mmu_ctx_t
*ctx
,
185 target_ulong pte0
, target_ulong pte1
, int h
, int rw
)
187 return _pte_check(ctx
, 0, pte0
, pte1
, h
, rw
);
190 #if defined(TARGET_PPC64)
191 static int pte64_check (mmu_ctx_t
*ctx
,
192 target_ulong pte0
, target_ulong pte1
, int h
, int rw
)
194 return _pte_check(ctx
, 1, pte0
, pte1
, h
, rw
);
198 static int pte_update_flags (mmu_ctx_t
*ctx
, target_ulong
*pte1p
,
203 /* Update page flags */
204 if (!(*pte1p
& 0x00000100)) {
205 /* Update accessed flag */
206 *pte1p
|= 0x00000100;
209 if (!(*pte1p
& 0x00000080)) {
210 if (rw
== 1 && ret
== 0) {
211 /* Update changed flag */
212 *pte1p
|= 0x00000080;
215 /* Force page fault for first write access */
216 ctx
->prot
&= ~PAGE_WRITE
;
223 /* Software driven TLB helpers */
224 static int ppc6xx_tlb_getnum (CPUState
*env
, target_ulong eaddr
,
225 int way
, int is_code
)
229 /* Select TLB num in a way from address */
230 nr
= (eaddr
>> TARGET_PAGE_BITS
) & (env
->tlb_per_way
- 1);
232 nr
+= env
->tlb_per_way
* way
;
233 /* 6xx have separate TLBs for instructions and data */
234 if (is_code
&& env
->id_tlbs
== 1)
240 static void ppc6xx_tlb_invalidate_all (CPUState
*env
)
245 #if defined (DEBUG_SOFTWARE_TLB) && 0
247 fprintf(logfile
, "Invalidate all TLBs\n");
250 /* Invalidate all defined software TLB */
252 if (env
->id_tlbs
== 1)
254 for (nr
= 0; nr
< max
; nr
++) {
255 tlb
= &env
->tlb
[nr
].tlb6
;
256 pte_invalidate(&tlb
->pte0
);
261 static inline void __ppc6xx_tlb_invalidate_virt (CPUState
*env
,
263 int is_code
, int match_epn
)
265 #if !defined(FLUSH_ALL_TLBS)
269 /* Invalidate ITLB + DTLB, all ways */
270 for (way
= 0; way
< env
->nb_ways
; way
++) {
271 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
, is_code
);
272 tlb
= &env
->tlb
[nr
].tlb6
;
273 if (pte_is_valid(tlb
->pte0
) && (match_epn
== 0 || eaddr
== tlb
->EPN
)) {
274 #if defined (DEBUG_SOFTWARE_TLB)
276 fprintf(logfile
, "TLB invalidate %d/%d " ADDRX
"\n",
277 nr
, env
->nb_tlb
, eaddr
);
280 pte_invalidate(&tlb
->pte0
);
281 tlb_flush_page(env
, tlb
->EPN
);
285 /* XXX: PowerPC specification say this is valid as well */
286 ppc6xx_tlb_invalidate_all(env
);
290 static void ppc6xx_tlb_invalidate_virt (CPUState
*env
, target_ulong eaddr
,
293 __ppc6xx_tlb_invalidate_virt(env
, eaddr
, is_code
, 0);
296 void ppc6xx_tlb_store (CPUState
*env
, target_ulong EPN
, int way
, int is_code
,
297 target_ulong pte0
, target_ulong pte1
)
302 nr
= ppc6xx_tlb_getnum(env
, EPN
, way
, is_code
);
303 tlb
= &env
->tlb
[nr
].tlb6
;
304 #if defined (DEBUG_SOFTWARE_TLB)
306 fprintf(logfile
, "Set TLB %d/%d EPN " ADDRX
" PTE0 " ADDRX
307 " PTE1 " ADDRX
"\n", nr
, env
->nb_tlb
, EPN
, pte0
, pte1
);
310 /* Invalidate any pending reference in Qemu for this virtual address */
311 __ppc6xx_tlb_invalidate_virt(env
, EPN
, is_code
, 1);
315 /* Store last way for LRU mechanism */
319 static int ppc6xx_tlb_check (CPUState
*env
, mmu_ctx_t
*ctx
,
320 target_ulong eaddr
, int rw
, int access_type
)
327 ret
= -1; /* No TLB found */
328 for (way
= 0; way
< env
->nb_ways
; way
++) {
329 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
,
330 access_type
== ACCESS_CODE
? 1 : 0);
331 tlb
= &env
->tlb
[nr
].tlb6
;
332 /* This test "emulates" the PTE index match for hardware TLBs */
333 if ((eaddr
& TARGET_PAGE_MASK
) != tlb
->EPN
) {
334 #if defined (DEBUG_SOFTWARE_TLB)
336 fprintf(logfile
, "TLB %d/%d %s [" ADDRX
" " ADDRX
339 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
340 tlb
->EPN
, tlb
->EPN
+ TARGET_PAGE_SIZE
, eaddr
);
345 #if defined (DEBUG_SOFTWARE_TLB)
347 fprintf(logfile
, "TLB %d/%d %s " ADDRX
" <> " ADDRX
" " ADDRX
350 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
351 tlb
->EPN
, eaddr
, tlb
->pte1
,
352 rw
? 'S' : 'L', access_type
== ACCESS_CODE
? 'I' : 'D');
355 switch (pte32_check(ctx
, tlb
->pte0
, tlb
->pte1
, 0, rw
)) {
357 /* TLB inconsistency */
360 /* Access violation */
370 /* XXX: we should go on looping to check all TLBs consistency
371 * but we can speed-up the whole thing as the
372 * result would be undefined if TLBs are not consistent.
381 #if defined (DEBUG_SOFTWARE_TLB)
383 fprintf(logfile
, "found TLB at addr 0x%08lx prot=0x%01x ret=%d\n",
384 ctx
->raddr
& TARGET_PAGE_MASK
, ctx
->prot
, ret
);
387 /* Update page flags */
388 pte_update_flags(ctx
, &env
->tlb
[best
].tlb6
.pte1
, ret
, rw
);
394 /* Perform BAT hit & translation */
395 static int get_bat (CPUState
*env
, mmu_ctx_t
*ctx
,
396 target_ulong
virtual, int rw
, int type
)
398 target_ulong
*BATlt
, *BATut
, *BATu
, *BATl
;
399 target_ulong base
, BEPIl
, BEPIu
, bl
;
403 #if defined (DEBUG_BATS)
405 fprintf(logfile
, "%s: %cBAT v 0x" ADDRX
"\n", __func__
,
406 type
== ACCESS_CODE
? 'I' : 'D', virtual);
411 BATlt
= env
->IBAT
[1];
412 BATut
= env
->IBAT
[0];
415 BATlt
= env
->DBAT
[1];
416 BATut
= env
->DBAT
[0];
419 #if defined (DEBUG_BATS)
421 fprintf(logfile
, "%s...: %cBAT v 0x" ADDRX
"\n", __func__
,
422 type
== ACCESS_CODE
? 'I' : 'D', virtual);
425 base
= virtual & 0xFFFC0000;
426 for (i
= 0; i
< 4; i
++) {
429 BEPIu
= *BATu
& 0xF0000000;
430 BEPIl
= *BATu
& 0x0FFE0000;
431 bl
= (*BATu
& 0x00001FFC) << 15;
432 #if defined (DEBUG_BATS)
434 fprintf(logfile
, "%s: %cBAT%d v 0x" ADDRX
" BATu 0x" ADDRX
435 " BATl 0x" ADDRX
"\n",
436 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, virtual,
440 if ((virtual & 0xF0000000) == BEPIu
&&
441 ((virtual & 0x0FFE0000) & ~bl
) == BEPIl
) {
443 if ((msr_pr
== 0 && (*BATu
& 0x00000002)) ||
444 (msr_pr
== 1 && (*BATu
& 0x00000001))) {
445 /* Get physical address */
446 ctx
->raddr
= (*BATl
& 0xF0000000) |
447 ((virtual & 0x0FFE0000 & bl
) | (*BATl
& 0x0FFE0000)) |
448 (virtual & 0x0001F000);
449 if (*BATl
& 0x00000001)
450 ctx
->prot
= PAGE_READ
;
451 if (*BATl
& 0x00000002)
452 ctx
->prot
= PAGE_WRITE
| PAGE_READ
;
453 #if defined (DEBUG_BATS)
455 fprintf(logfile
, "BAT %d match: r 0x" PADDRX
457 i
, ctx
->raddr
, ctx
->prot
& PAGE_READ
? 'R' : '-',
458 ctx
->prot
& PAGE_WRITE
? 'W' : '-');
467 #if defined (DEBUG_BATS)
469 fprintf(logfile
, "no BAT match for 0x" ADDRX
":\n", virtual);
470 for (i
= 0; i
< 4; i
++) {
473 BEPIu
= *BATu
& 0xF0000000;
474 BEPIl
= *BATu
& 0x0FFE0000;
475 bl
= (*BATu
& 0x00001FFC) << 15;
476 fprintf(logfile
, "%s: %cBAT%d v 0x" ADDRX
" BATu 0x" ADDRX
477 " BATl 0x" ADDRX
" \n\t"
478 "0x" ADDRX
" 0x" ADDRX
" 0x" ADDRX
"\n",
479 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, virtual,
480 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
489 /* PTE table lookup */
490 static inline int _find_pte (mmu_ctx_t
*ctx
, int is_64b
, int h
, int rw
)
492 target_ulong base
, pte0
, pte1
;
496 ret
= -1; /* No entry found */
497 base
= ctx
->pg_addr
[h
];
498 for (i
= 0; i
< 8; i
++) {
499 #if defined(TARGET_PPC64)
501 pte0
= ldq_phys(base
+ (i
* 16));
502 pte1
= ldq_phys(base
+ (i
* 16) + 8);
503 r
= pte64_check(ctx
, pte0
, pte1
, h
, rw
);
507 pte0
= ldl_phys(base
+ (i
* 8));
508 pte1
= ldl_phys(base
+ (i
* 8) + 4);
509 r
= pte32_check(ctx
, pte0
, pte1
, h
, rw
);
511 #if defined (DEBUG_MMU)
513 fprintf(logfile
, "Load pte from 0x" ADDRX
" => 0x" ADDRX
514 " 0x" ADDRX
" %d %d %d 0x" ADDRX
"\n",
515 base
+ (i
* 8), pte0
, pte1
,
516 (int)(pte0
>> 31), h
, (int)((pte0
>> 6) & 1), ctx
->ptem
);
521 /* PTE inconsistency */
524 /* Access violation */
534 /* XXX: we should go on looping to check all PTEs consistency
535 * but if we can speed-up the whole thing as the
536 * result would be undefined if PTEs are not consistent.
545 #if defined (DEBUG_MMU)
547 fprintf(logfile
, "found PTE at addr 0x" PADDRX
" prot=0x%01x "
549 ctx
->raddr
, ctx
->prot
, ret
);
552 /* Update page flags */
554 if (pte_update_flags(ctx
, &pte1
, ret
, rw
) == 1) {
555 #if defined(TARGET_PPC64)
557 stq_phys_notdirty(base
+ (good
* 16) + 8, pte1
);
561 stl_phys_notdirty(base
+ (good
* 8) + 4, pte1
);
569 static int find_pte32 (mmu_ctx_t
*ctx
, int h
, int rw
)
571 return _find_pte(ctx
, 0, h
, rw
);
574 #if defined(TARGET_PPC64)
575 static int find_pte64 (mmu_ctx_t
*ctx
, int h
, int rw
)
577 return _find_pte(ctx
, 1, h
, rw
);
581 static inline int find_pte (CPUState
*env
, mmu_ctx_t
*ctx
, int h
, int rw
)
583 #if defined(TARGET_PPC64)
584 if (env
->mmu_model
== POWERPC_MMU_64B
||
585 env
->mmu_model
== POWERPC_MMU_64BRIDGE
)
586 return find_pte64(ctx
, h
, rw
);
589 return find_pte32(ctx
, h
, rw
);
592 static inline target_phys_addr_t
get_pgaddr (target_phys_addr_t sdr1
,
594 target_phys_addr_t hash
,
595 target_phys_addr_t mask
)
597 return (sdr1
& ((target_ulong
)(-1ULL) << sdr_sh
)) | (hash
& mask
);
600 #if defined(TARGET_PPC64)
601 static int slb_lookup (CPUState
*env
, target_ulong eaddr
,
602 target_ulong
*vsid
, target_ulong
*page_mask
, int *attr
)
604 target_phys_addr_t sr_base
;
612 sr_base
= env
->spr
[SPR_ASR
];
613 mask
= 0x0000000000000000ULL
; /* Avoid gcc warning */
614 #if 0 /* XXX: Fix this */
615 slb_nr
= env
->slb_nr
;
619 for (n
= 0; n
< slb_nr
; n
++) {
620 tmp64
= ldq_phys(sr_base
);
621 if (tmp64
& 0x0000000008000000ULL
) {
622 /* SLB entry is valid */
623 switch (tmp64
& 0x0000000006000000ULL
) {
624 case 0x0000000000000000ULL
:
626 mask
= 0xFFFFFFFFF0000000ULL
;
628 case 0x0000000002000000ULL
:
630 mask
= 0xFFFF000000000000ULL
;
632 case 0x0000000004000000ULL
:
633 case 0x0000000006000000ULL
:
634 /* Reserved => segment is invalid */
637 if ((eaddr
& mask
) == (tmp64
& mask
)) {
639 tmp
= ldl_phys(sr_base
+ 8);
640 *vsid
= ((tmp64
<< 24) | (tmp
>> 8)) & 0x0003FFFFFFFFFFFFULL
;
652 #endif /* defined(TARGET_PPC64) */
654 /* Perform segment based translation */
655 static int get_segment (CPUState
*env
, mmu_ctx_t
*ctx
,
656 target_ulong eaddr
, int rw
, int type
)
658 target_phys_addr_t sdr
, hash
, mask
, sdr_mask
;
659 target_ulong sr
, vsid
, vsid_mask
, pgidx
, page_mask
;
660 #if defined(TARGET_PPC64)
663 int ds
, nx
, vsid_sh
, sdr_sh
;
666 #if defined(TARGET_PPC64)
667 if (env
->mmu_model
== POWERPC_MMU_64B
||
668 env
->mmu_model
== POWERPC_MMU_64BRIDGE
) {
669 ret
= slb_lookup(env
, eaddr
, &vsid
, &page_mask
, &attr
);
672 ctx
->key
= ((attr
& 0x40) && msr_pr
== 1) ||
673 ((attr
& 0x80) && msr_pr
== 0) ? 1 : 0;
675 nx
= attr
& 0x20 ? 1 : 0;
676 vsid_mask
= 0x00003FFFFFFFFF80ULL
;
681 #endif /* defined(TARGET_PPC64) */
683 sr
= env
->sr
[eaddr
>> 28];
684 page_mask
= 0x0FFFFFFF;
685 ctx
->key
= (((sr
& 0x20000000) && msr_pr
== 1) ||
686 ((sr
& 0x40000000) && msr_pr
== 0)) ? 1 : 0;
687 ds
= sr
& 0x80000000 ? 1 : 0;
688 nx
= sr
& 0x10000000 ? 1 : 0;
689 vsid
= sr
& 0x00FFFFFF;
690 vsid_mask
= 0x01FFFFC0;
694 #if defined (DEBUG_MMU)
696 fprintf(logfile
, "Check segment v=0x" ADDRX
" %d 0x" ADDRX
697 " nip=0x" ADDRX
" lr=0x" ADDRX
698 " ir=%d dr=%d pr=%d %d t=%d\n",
699 eaddr
, (int)(eaddr
>> 28), sr
, env
->nip
,
700 env
->lr
, msr_ir
, msr_dr
, msr_pr
, rw
, type
);
702 if (!ds
&& loglevel
!= 0) {
703 fprintf(logfile
, "pte segment: key=%d n=0x" ADDRX
"\n",
704 ctx
->key
, sr
& 0x10000000);
710 /* Check if instruction fetch is allowed, if needed */
711 if (type
!= ACCESS_CODE
|| nx
== 0) {
712 /* Page address translation */
713 pgidx
= (eaddr
& page_mask
) >> TARGET_PAGE_BITS
;
714 hash
= ((vsid
^ pgidx
) << vsid_sh
) & vsid_mask
;
715 /* Primary table address */
717 mask
= ((sdr
& 0x000001FF) << sdr_sh
) | sdr_mask
;
718 ctx
->pg_addr
[0] = get_pgaddr(sdr
, sdr_sh
, hash
, mask
);
719 /* Secondary table address */
720 hash
= (~hash
) & vsid_mask
;
721 ctx
->pg_addr
[1] = get_pgaddr(sdr
, sdr_sh
, hash
, mask
);
722 #if defined(TARGET_PPC64)
723 if (env
->mmu_model
== POWERPC_MMU_64B
||
724 env
->mmu_model
== POWERPC_MMU_64BRIDGE
) {
725 /* Only 5 bits of the page index are used in the AVPN */
726 ctx
->ptem
= (vsid
<< 12) | ((pgidx
>> 4) & 0x0F80);
730 ctx
->ptem
= (vsid
<< 7) | (pgidx
>> 10);
732 /* Initialize real address with an invalid value */
733 ctx
->raddr
= (target_ulong
)-1;
734 if (unlikely(env
->mmu_model
== POWERPC_MMU_SOFT_6xx
||
735 env
->mmu_model
== POWERPC_MMU_SOFT_74xx
)) {
736 /* Software TLB search */
737 ret
= ppc6xx_tlb_check(env
, ctx
, eaddr
, rw
, type
);
739 #if defined (DEBUG_MMU)
741 fprintf(logfile
, "0 sdr1=0x" PADDRX
" vsid=0x%06x "
742 "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX
"\n",
743 sdr
, (uint32_t)vsid
, (uint32_t)pgidx
,
744 (uint32_t)hash
, ctx
->pg_addr
[0]);
747 /* Primary table lookup */
748 ret
= find_pte(env
, ctx
, 0, rw
);
750 /* Secondary table lookup */
751 #if defined (DEBUG_MMU)
752 if (eaddr
!= 0xEFFFFFFF && loglevel
!= 0) {
754 "1 sdr1=0x" PADDRX
" vsid=0x%06x api=0x%04x "
755 "hash=0x%05x pg_addr=0x" PADDRX
"\n",
756 sdr
, (uint32_t)vsid
, (uint32_t)pgidx
,
757 (uint32_t)hash
, ctx
->pg_addr
[1]);
760 ret2
= find_pte(env
, ctx
, 1, rw
);
766 #if defined (DEBUG_MMU)
768 fprintf(logfile
, "No access allowed\n");
773 #if defined (DEBUG_MMU)
775 fprintf(logfile
, "direct store...\n");
777 /* Direct-store segment : absolutely *BUGGY* for now */
780 /* Integer load/store : only access allowed */
783 /* No code fetch is allowed in direct-store areas */
786 /* Floating point load/store */
789 /* lwarx, ldarx or srwcx. */
792 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
793 /* Should make the instruction do no-op.
794 * As it already do no-op, it's quite easy :-)
803 fprintf(logfile
, "ERROR: instruction should not need "
804 "address translation\n");
808 if ((rw
== 1 || ctx
->key
!= 1) && (rw
== 0 || ctx
->key
!= 0)) {
819 /* Generic TLB check function for embedded PowerPC implementations */
820 static int ppcemb_tlb_check (CPUState
*env
, ppcemb_tlb_t
*tlb
,
821 target_phys_addr_t
*raddrp
,
822 target_ulong address
,
823 uint32_t pid
, int ext
, int i
)
827 /* Check valid flag */
828 if (!(tlb
->prot
& PAGE_VALID
)) {
830 fprintf(logfile
, "%s: TLB %d not valid\n", __func__
, i
);
833 mask
= ~(tlb
->size
- 1);
834 #if defined (DEBUG_SOFTWARE_TLB)
836 fprintf(logfile
, "%s: TLB %d address " ADDRX
" PID %d <=> "
837 ADDRX
" " ADDRX
" %d\n",
838 __func__
, i
, address
, pid
, tlb
->EPN
, mask
, (int)tlb
->PID
);
842 if (tlb
->PID
!= 0 && tlb
->PID
!= pid
)
844 /* Check effective address */
845 if ((address
& mask
) != tlb
->EPN
)
847 *raddrp
= (tlb
->RPN
& mask
) | (address
& ~mask
);
848 #if (TARGET_PHYS_ADDR_BITS >= 36)
850 /* Extend the physical address to 36 bits */
851 *raddrp
|= (target_phys_addr_t
)(tlb
->RPN
& 0xF) << 32;
858 /* Generic TLB search function for PowerPC embedded implementations */
859 int ppcemb_tlb_search (CPUPPCState
*env
, target_ulong address
, uint32_t pid
)
862 target_phys_addr_t raddr
;
865 /* Default return value is no match */
867 for (i
= 0; i
< env
->nb_tlb
; i
++) {
868 tlb
= &env
->tlb
[i
].tlbe
;
869 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
, pid
, 0, i
) == 0) {
878 /* Helpers specific to PowerPC 40x implementations */
879 static void ppc4xx_tlb_invalidate_all (CPUState
*env
)
884 for (i
= 0; i
< env
->nb_tlb
; i
++) {
885 tlb
= &env
->tlb
[i
].tlbe
;
886 tlb
->prot
&= ~PAGE_VALID
;
891 static void ppc4xx_tlb_invalidate_virt (CPUState
*env
, target_ulong eaddr
,
894 #if !defined(FLUSH_ALL_TLBS)
896 target_phys_addr_t raddr
;
897 target_ulong page
, end
;
900 for (i
= 0; i
< env
->nb_tlb
; i
++) {
901 tlb
= &env
->tlb
[i
].tlbe
;
902 if (ppcemb_tlb_check(env
, tlb
, &raddr
, eaddr
, pid
, 0, i
) == 0) {
903 end
= tlb
->EPN
+ tlb
->size
;
904 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
)
905 tlb_flush_page(env
, page
);
906 tlb
->prot
&= ~PAGE_VALID
;
911 ppc4xx_tlb_invalidate_all(env
);
915 int mmu40x_get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
,
916 target_ulong address
, int rw
, int access_type
)
919 target_phys_addr_t raddr
;
920 int i
, ret
, zsel
, zpr
;
924 for (i
= 0; i
< env
->nb_tlb
; i
++) {
925 tlb
= &env
->tlb
[i
].tlbe
;
926 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
927 env
->spr
[SPR_40x_PID
], 0, i
) < 0)
929 zsel
= (tlb
->attr
>> 4) & 0xF;
930 zpr
= (env
->spr
[SPR_40x_ZPR
] >> (28 - (2 * zsel
))) & 0x3;
931 #if defined (DEBUG_SOFTWARE_TLB)
933 fprintf(logfile
, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
934 __func__
, i
, zsel
, zpr
, rw
, tlb
->attr
);
937 if (access_type
== ACCESS_CODE
) {
938 /* Check execute enable bit */
942 goto check_exec_perm
;
953 /* Check from TLB entry */
954 if (!(tlb
->prot
& PAGE_EXEC
)) {
957 if (tlb
->prot
& PAGE_WRITE
) {
958 ctx
->prot
= PAGE_READ
| PAGE_WRITE
;
960 ctx
->prot
= PAGE_READ
;
967 /* All accesses granted */
968 ctx
->prot
= PAGE_READ
| PAGE_WRITE
;
987 /* Check from TLB entry */
988 /* Check write protection bit */
989 if (tlb
->prot
& PAGE_WRITE
) {
990 ctx
->prot
= PAGE_READ
| PAGE_WRITE
;
993 ctx
->prot
= PAGE_READ
;
1002 /* All accesses granted */
1003 ctx
->prot
= PAGE_READ
| PAGE_WRITE
;
1010 #if defined (DEBUG_SOFTWARE_TLB)
1011 if (loglevel
!= 0) {
1012 fprintf(logfile
, "%s: access granted " ADDRX
" => " REGX
1013 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
1020 #if defined (DEBUG_SOFTWARE_TLB)
1021 if (loglevel
!= 0) {
1022 fprintf(logfile
, "%s: access refused " ADDRX
" => " REGX
1023 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
,
1031 void store_40x_sler (CPUPPCState
*env
, uint32_t val
)
1033 /* XXX: TO BE FIXED */
1034 if (val
!= 0x00000000) {
1035 cpu_abort(env
, "Little-endian regions are not supported by now\n");
1037 env
->spr
[SPR_405_SLER
] = val
;
1040 int mmubooke_get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
,
1041 target_ulong address
, int rw
,
1045 target_phys_addr_t raddr
;
1050 for (i
= 0; i
< env
->nb_tlb
; i
++) {
1051 tlb
= &env
->tlb
[i
].tlbe
;
1052 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
1053 env
->spr
[SPR_BOOKE_PID
], 1, i
) < 0)
1056 prot
= tlb
->prot
& 0xF;
1058 prot
= (tlb
->prot
>> 4) & 0xF;
1059 /* Check the address space */
1060 if (access_type
== ACCESS_CODE
) {
1061 if (msr_is
!= (tlb
->attr
& 1))
1064 if (prot
& PAGE_EXEC
) {
1070 if (msr_ds
!= (tlb
->attr
& 1))
1073 if ((!rw
&& prot
& PAGE_READ
) || (rw
&& (prot
& PAGE_WRITE
))) {
1086 static int check_physical (CPUState
*env
, mmu_ctx_t
*ctx
,
1087 target_ulong eaddr
, int rw
)
1092 ctx
->prot
= PAGE_READ
;
1094 switch (env
->mmu_model
) {
1095 case POWERPC_MMU_32B
:
1096 case POWERPC_MMU_SOFT_6xx
:
1097 case POWERPC_MMU_SOFT_74xx
:
1098 case POWERPC_MMU_601
:
1099 case POWERPC_MMU_SOFT_4xx
:
1100 case POWERPC_MMU_REAL_4xx
:
1101 case POWERPC_MMU_BOOKE
:
1102 ctx
->prot
|= PAGE_WRITE
;
1104 #if defined(TARGET_PPC64)
1105 case POWERPC_MMU_64B
:
1106 case POWERPC_MMU_64BRIDGE
:
1107 /* Real address are 60 bits long */
1108 ctx
->raddr
&= 0x0FFFFFFFFFFFFFFFULL
;
1109 ctx
->prot
|= PAGE_WRITE
;
1112 case POWERPC_MMU_SOFT_4xx_Z
:
1113 if (unlikely(msr_pe
!= 0)) {
1114 /* 403 family add some particular protections,
1115 * using PBL/PBU registers for accesses with no translation.
1118 /* Check PLB validity */
1119 (env
->pb
[0] < env
->pb
[1] &&
1120 /* and address in plb area */
1121 eaddr
>= env
->pb
[0] && eaddr
< env
->pb
[1]) ||
1122 (env
->pb
[2] < env
->pb
[3] &&
1123 eaddr
>= env
->pb
[2] && eaddr
< env
->pb
[3]) ? 1 : 0;
1124 if (in_plb
^ msr_px
) {
1125 /* Access in protected area */
1127 /* Access is not allowed */
1131 /* Read-write access is allowed */
1132 ctx
->prot
|= PAGE_WRITE
;
1136 case POWERPC_MMU_BOOKE_FSL
:
1138 cpu_abort(env
, "BookE FSL MMU model not implemented\n");
1141 cpu_abort(env
, "Unknown or invalid MMU model\n");
1148 int get_physical_address (CPUState
*env
, mmu_ctx_t
*ctx
, target_ulong eaddr
,
1149 int rw
, int access_type
, int check_BATs
)
1153 if (loglevel
!= 0) {
1154 fprintf(logfile
, "%s\n", __func__
);
1157 if ((access_type
== ACCESS_CODE
&& msr_ir
== 0) ||
1158 (access_type
!= ACCESS_CODE
&& msr_dr
== 0)) {
1159 /* No address translation */
1160 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1163 switch (env
->mmu_model
) {
1164 case POWERPC_MMU_32B
:
1165 case POWERPC_MMU_SOFT_6xx
:
1166 case POWERPC_MMU_SOFT_74xx
:
1167 /* Try to find a BAT */
1169 ret
= get_bat(env
, ctx
, eaddr
, rw
, access_type
);
1171 #if defined(TARGET_PPC64)
1172 case POWERPC_MMU_64B
:
1173 case POWERPC_MMU_64BRIDGE
:
1176 /* We didn't match any BAT entry or don't have BATs */
1177 ret
= get_segment(env
, ctx
, eaddr
, rw
, access_type
);
1180 case POWERPC_MMU_SOFT_4xx
:
1181 case POWERPC_MMU_SOFT_4xx_Z
:
1182 ret
= mmu40x_get_physical_address(env
, ctx
, eaddr
,
1185 case POWERPC_MMU_601
:
1187 cpu_abort(env
, "601 MMU model not implemented\n");
1189 case POWERPC_MMU_BOOKE
:
1190 ret
= mmubooke_get_physical_address(env
, ctx
, eaddr
,
1193 case POWERPC_MMU_BOOKE_FSL
:
1195 cpu_abort(env
, "BookE FSL MMU model not implemented\n");
1197 case POWERPC_MMU_REAL_4xx
:
1198 cpu_abort(env
, "PowerPC 401 does not do any translation\n");
1201 cpu_abort(env
, "Unknown or invalid MMU model\n");
1206 if (loglevel
!= 0) {
1207 fprintf(logfile
, "%s address " ADDRX
" => %d " PADDRX
"\n",
1208 __func__
, eaddr
, ret
, ctx
->raddr
);
1215 target_phys_addr_t
cpu_get_phys_page_debug (CPUState
*env
, target_ulong addr
)
1219 if (unlikely(get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
, 1) != 0))
1222 return ctx
.raddr
& TARGET_PAGE_MASK
;
1225 /* Perform address translation */
1226 int cpu_ppc_handle_mmu_fault (CPUState
*env
, target_ulong address
, int rw
,
1227 int is_user
, int is_softmmu
)
1236 access_type
= ACCESS_CODE
;
1239 /* XXX: put correct access by using cpu_restore_state()
1241 access_type
= ACCESS_INT
;
1242 // access_type = env->access_type;
1244 ret
= get_physical_address(env
, &ctx
, address
, rw
, access_type
, 1);
1246 ret
= tlb_set_page(env
, address
& TARGET_PAGE_MASK
,
1247 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
1248 is_user
, is_softmmu
);
1249 } else if (ret
< 0) {
1250 #if defined (DEBUG_MMU)
1252 cpu_dump_state(env
, logfile
, fprintf
, 0);
1254 if (access_type
== ACCESS_CODE
) {
1257 /* No matches in page tables or TLB */
1258 switch (env
->mmu_model
) {
1259 case POWERPC_MMU_SOFT_6xx
:
1260 env
->exception_index
= POWERPC_EXCP_IFTLB
;
1261 env
->error_code
= 1 << 18;
1262 env
->spr
[SPR_IMISS
] = address
;
1263 env
->spr
[SPR_ICMP
] = 0x80000000 | ctx
.ptem
;
1265 case POWERPC_MMU_SOFT_74xx
:
1266 env
->exception_index
= POWERPC_EXCP_IFTLB
;
1268 case POWERPC_MMU_SOFT_4xx
:
1269 case POWERPC_MMU_SOFT_4xx_Z
:
1270 env
->exception_index
= POWERPC_EXCP_ITLB
;
1271 env
->error_code
= 0;
1272 env
->spr
[SPR_40x_DEAR
] = address
;
1273 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1275 case POWERPC_MMU_32B
:
1276 #if defined(TARGET_PPC64)
1277 case POWERPC_MMU_64B
:
1278 case POWERPC_MMU_64BRIDGE
:
1280 env
->exception_index
= POWERPC_EXCP_ISI
;
1281 env
->error_code
= 0x40000000;
1283 case POWERPC_MMU_601
:
1285 cpu_abort(env
, "MMU model not implemented\n");
1287 case POWERPC_MMU_BOOKE
:
1289 cpu_abort(env
, "MMU model not implemented\n");
1291 case POWERPC_MMU_BOOKE_FSL
:
1293 cpu_abort(env
, "MMU model not implemented\n");
1295 case POWERPC_MMU_REAL_4xx
:
1296 cpu_abort(env
, "PowerPC 401 should never raise any MMU "
1300 cpu_abort(env
, "Unknown or invalid MMU model\n");
1305 /* Access rights violation */
1306 env
->exception_index
= POWERPC_EXCP_ISI
;
1307 env
->error_code
= 0x08000000;
1310 /* No execute protection violation */
1311 env
->exception_index
= POWERPC_EXCP_ISI
;
1312 env
->error_code
= 0x10000000;
1315 /* Direct store exception */
1316 /* No code fetch is allowed in direct-store areas */
1317 env
->exception_index
= POWERPC_EXCP_ISI
;
1318 env
->error_code
= 0x10000000;
1320 #if defined(TARGET_PPC64)
1322 /* No match in segment table */
1323 env
->exception_index
= POWERPC_EXCP_ISEG
;
1324 env
->error_code
= 0;
1331 /* No matches in page tables or TLB */
1332 switch (env
->mmu_model
) {
1333 case POWERPC_MMU_SOFT_6xx
:
1335 env
->exception_index
= POWERPC_EXCP_DSTLB
;
1336 env
->error_code
= 1 << 16;
1338 env
->exception_index
= POWERPC_EXCP_DLTLB
;
1339 env
->error_code
= 0;
1341 env
->spr
[SPR_DMISS
] = address
;
1342 env
->spr
[SPR_DCMP
] = 0x80000000 | ctx
.ptem
;
1344 env
->error_code
|= ctx
.key
<< 19;
1345 env
->spr
[SPR_HASH1
] = ctx
.pg_addr
[0];
1346 env
->spr
[SPR_HASH2
] = ctx
.pg_addr
[1];
1348 case POWERPC_MMU_SOFT_74xx
:
1350 env
->exception_index
= POWERPC_EXCP_DSTLB
;
1352 env
->exception_index
= POWERPC_EXCP_DLTLB
;
1355 /* Implement LRU algorithm */
1356 env
->error_code
= ctx
.key
<< 19;
1357 env
->spr
[SPR_TLBMISS
] = (address
& ~((target_ulong
)0x3)) |
1358 ((env
->last_way
+ 1) & (env
->nb_ways
- 1));
1359 env
->spr
[SPR_PTEHI
] = 0x80000000 | ctx
.ptem
;
1361 case POWERPC_MMU_SOFT_4xx
:
1362 case POWERPC_MMU_SOFT_4xx_Z
:
1363 env
->exception_index
= POWERPC_EXCP_DTLB
;
1364 env
->error_code
= 0;
1365 env
->spr
[SPR_40x_DEAR
] = address
;
1367 env
->spr
[SPR_40x_ESR
] = 0x00800000;
1369 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1371 case POWERPC_MMU_32B
:
1372 #if defined(TARGET_PPC64)
1373 case POWERPC_MMU_64B
:
1374 case POWERPC_MMU_64BRIDGE
:
1376 env
->exception_index
= POWERPC_EXCP_DSI
;
1377 env
->error_code
= 0;
1378 env
->spr
[SPR_DAR
] = address
;
1380 env
->spr
[SPR_DSISR
] = 0x42000000;
1382 env
->spr
[SPR_DSISR
] = 0x40000000;
1384 case POWERPC_MMU_601
:
1386 cpu_abort(env
, "MMU model not implemented\n");
1388 case POWERPC_MMU_BOOKE
:
1390 cpu_abort(env
, "MMU model not implemented\n");
1392 case POWERPC_MMU_BOOKE_FSL
:
1394 cpu_abort(env
, "MMU model not implemented\n");
1396 case POWERPC_MMU_REAL_4xx
:
1397 cpu_abort(env
, "PowerPC 401 should never raise any MMU "
1401 cpu_abort(env
, "Unknown or invalid MMU model\n");
1406 /* Access rights violation */
1407 env
->exception_index
= POWERPC_EXCP_DSI
;
1408 env
->error_code
= 0;
1409 env
->spr
[SPR_DAR
] = address
;
1411 env
->spr
[SPR_DSISR
] = 0x0A000000;
1413 env
->spr
[SPR_DSISR
] = 0x08000000;
1416 /* Direct store exception */
1417 switch (access_type
) {
1419 /* Floating point load/store */
1420 env
->exception_index
= POWERPC_EXCP_ALIGN
;
1421 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
1422 env
->spr
[SPR_DAR
] = address
;
1425 /* lwarx, ldarx or stwcx. */
1426 env
->exception_index
= POWERPC_EXCP_DSI
;
1427 env
->error_code
= 0;
1428 env
->spr
[SPR_DAR
] = address
;
1430 env
->spr
[SPR_DSISR
] = 0x06000000;
1432 env
->spr
[SPR_DSISR
] = 0x04000000;
1435 /* eciwx or ecowx */
1436 env
->exception_index
= POWERPC_EXCP_DSI
;
1437 env
->error_code
= 0;
1438 env
->spr
[SPR_DAR
] = address
;
1440 env
->spr
[SPR_DSISR
] = 0x06100000;
1442 env
->spr
[SPR_DSISR
] = 0x04100000;
1445 printf("DSI: invalid exception (%d)\n", ret
);
1446 env
->exception_index
= POWERPC_EXCP_PROGRAM
;
1448 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
;
1449 env
->spr
[SPR_DAR
] = address
;
1453 #if defined(TARGET_PPC64)
1455 /* No match in segment table */
1456 env
->exception_index
= POWERPC_EXCP_DSEG
;
1457 env
->error_code
= 0;
1458 env
->spr
[SPR_DAR
] = address
;
1464 printf("%s: set exception to %d %02x\n", __func__
,
1465 env
->exception
, env
->error_code
);
1473 /*****************************************************************************/
1474 /* BATs management */
1475 #if !defined(FLUSH_ALL_TLBS)
1476 static inline void do_invalidate_BAT (CPUPPCState
*env
,
1477 target_ulong BATu
, target_ulong mask
)
1479 target_ulong base
, end
, page
;
1481 base
= BATu
& ~0x0001FFFF;
1482 end
= base
+ mask
+ 0x00020000;
1483 #if defined (DEBUG_BATS)
1484 if (loglevel
!= 0) {
1485 fprintf(logfile
, "Flush BAT from " ADDRX
" to " ADDRX
" (" ADDRX
")\n",
1489 for (page
= base
; page
!= end
; page
+= TARGET_PAGE_SIZE
)
1490 tlb_flush_page(env
, page
);
1491 #if defined (DEBUG_BATS)
1493 fprintf(logfile
, "Flush done\n");
1498 static inline void dump_store_bat (CPUPPCState
*env
, char ID
, int ul
, int nr
,
1501 #if defined (DEBUG_BATS)
1502 if (loglevel
!= 0) {
1503 fprintf(logfile
, "Set %cBAT%d%c to 0x" ADDRX
" (0x" ADDRX
")\n",
1504 ID
, nr
, ul
== 0 ? 'u' : 'l', value
, env
->nip
);
1509 target_ulong
do_load_ibatu (CPUPPCState
*env
, int nr
)
1511 return env
->IBAT
[0][nr
];
1514 target_ulong
do_load_ibatl (CPUPPCState
*env
, int nr
)
1516 return env
->IBAT
[1][nr
];
1519 void do_store_ibatu (CPUPPCState
*env
, int nr
, target_ulong value
)
1523 dump_store_bat(env
, 'I', 0, nr
, value
);
1524 if (env
->IBAT
[0][nr
] != value
) {
1525 mask
= (value
<< 15) & 0x0FFE0000UL
;
1526 #if !defined(FLUSH_ALL_TLBS)
1527 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1529 /* When storing valid upper BAT, mask BEPI and BRPN
1530 * and invalidate all TLBs covered by this BAT
1532 mask
= (value
<< 15) & 0x0FFE0000UL
;
1533 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1534 (value
& ~0x0001FFFFUL
& ~mask
);
1535 env
->IBAT
[1][nr
] = (env
->IBAT
[1][nr
] & 0x0000007B) |
1536 (env
->IBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1537 #if !defined(FLUSH_ALL_TLBS)
1538 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1545 void do_store_ibatl (CPUPPCState
*env
, int nr
, target_ulong value
)
1547 dump_store_bat(env
, 'I', 1, nr
, value
);
1548 env
->IBAT
[1][nr
] = value
;
1551 target_ulong
do_load_dbatu (CPUPPCState
*env
, int nr
)
1553 return env
->DBAT
[0][nr
];
1556 target_ulong
do_load_dbatl (CPUPPCState
*env
, int nr
)
1558 return env
->DBAT
[1][nr
];
1561 void do_store_dbatu (CPUPPCState
*env
, int nr
, target_ulong value
)
1565 dump_store_bat(env
, 'D', 0, nr
, value
);
1566 if (env
->DBAT
[0][nr
] != value
) {
1567 /* When storing valid upper BAT, mask BEPI and BRPN
1568 * and invalidate all TLBs covered by this BAT
1570 mask
= (value
<< 15) & 0x0FFE0000UL
;
1571 #if !defined(FLUSH_ALL_TLBS)
1572 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1574 mask
= (value
<< 15) & 0x0FFE0000UL
;
1575 env
->DBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1576 (value
& ~0x0001FFFFUL
& ~mask
);
1577 env
->DBAT
[1][nr
] = (env
->DBAT
[1][nr
] & 0x0000007B) |
1578 (env
->DBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1579 #if !defined(FLUSH_ALL_TLBS)
1580 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1587 void do_store_dbatl (CPUPPCState
*env
, int nr
, target_ulong value
)
1589 dump_store_bat(env
, 'D', 1, nr
, value
);
1590 env
->DBAT
[1][nr
] = value
;
1594 /*****************************************************************************/
1595 /* TLB management */
1596 void ppc_tlb_invalidate_all (CPUPPCState
*env
)
1598 switch (env
->mmu_model
) {
1599 case POWERPC_MMU_SOFT_6xx
:
1600 case POWERPC_MMU_SOFT_74xx
:
1601 ppc6xx_tlb_invalidate_all(env
);
1603 case POWERPC_MMU_SOFT_4xx
:
1604 case POWERPC_MMU_SOFT_4xx_Z
:
1605 ppc4xx_tlb_invalidate_all(env
);
1607 case POWERPC_MMU_REAL_4xx
:
1608 cpu_abort(env
, "No TLB for PowerPC 4xx in real mode\n");
1610 case POWERPC_MMU_BOOKE
:
1612 cpu_abort(env
, "MMU model not implemented\n");
1614 case POWERPC_MMU_BOOKE_FSL
:
1616 cpu_abort(env
, "MMU model not implemented\n");
1618 case POWERPC_MMU_601
:
1620 cpu_abort(env
, "MMU model not implemented\n");
1622 case POWERPC_MMU_32B
:
1623 #if defined(TARGET_PPC64)
1624 case POWERPC_MMU_64B
:
1625 case POWERPC_MMU_64BRIDGE
:
1626 #endif /* defined(TARGET_PPC64) */
1631 cpu_abort(env
, "Unknown MMU model %d\n", env
->mmu_model
);
1636 void ppc_tlb_invalidate_one (CPUPPCState
*env
, target_ulong addr
)
1638 #if !defined(FLUSH_ALL_TLBS)
1639 addr
&= TARGET_PAGE_MASK
;
1640 switch (env
->mmu_model
) {
1641 case POWERPC_MMU_SOFT_6xx
:
1642 case POWERPC_MMU_SOFT_74xx
:
1643 ppc6xx_tlb_invalidate_virt(env
, addr
, 0);
1644 if (env
->id_tlbs
== 1)
1645 ppc6xx_tlb_invalidate_virt(env
, addr
, 1);
1647 case POWERPC_MMU_SOFT_4xx
:
1648 case POWERPC_MMU_SOFT_4xx_Z
:
1649 ppc4xx_tlb_invalidate_virt(env
, addr
, env
->spr
[SPR_40x_PID
]);
1651 case POWERPC_MMU_REAL_4xx
:
1652 cpu_abort(env
, "No TLB for PowerPC 4xx in real mode\n");
1654 case POWERPC_MMU_BOOKE
:
1656 cpu_abort(env
, "MMU model not implemented\n");
1658 case POWERPC_MMU_BOOKE_FSL
:
1660 cpu_abort(env
, "MMU model not implemented\n");
1662 case POWERPC_MMU_601
:
1664 cpu_abort(env
, "MMU model not implemented\n");
1666 case POWERPC_MMU_32B
:
1667 /* tlbie invalidate TLBs for all segments */
1668 addr
&= ~((target_ulong
)-1 << 28);
1669 /* XXX: this case should be optimized,
1670 * giving a mask to tlb_flush_page
1672 tlb_flush_page(env
, addr
| (0x0 << 28));
1673 tlb_flush_page(env
, addr
| (0x1 << 28));
1674 tlb_flush_page(env
, addr
| (0x2 << 28));
1675 tlb_flush_page(env
, addr
| (0x3 << 28));
1676 tlb_flush_page(env
, addr
| (0x4 << 28));
1677 tlb_flush_page(env
, addr
| (0x5 << 28));
1678 tlb_flush_page(env
, addr
| (0x6 << 28));
1679 tlb_flush_page(env
, addr
| (0x7 << 28));
1680 tlb_flush_page(env
, addr
| (0x8 << 28));
1681 tlb_flush_page(env
, addr
| (0x9 << 28));
1682 tlb_flush_page(env
, addr
| (0xA << 28));
1683 tlb_flush_page(env
, addr
| (0xB << 28));
1684 tlb_flush_page(env
, addr
| (0xC << 28));
1685 tlb_flush_page(env
, addr
| (0xD << 28));
1686 tlb_flush_page(env
, addr
| (0xE << 28));
1687 tlb_flush_page(env
, addr
| (0xF << 28));
1689 #if defined(TARGET_PPC64)
1690 case POWERPC_MMU_64B
:
1691 case POWERPC_MMU_64BRIDGE
:
1692 /* tlbie invalidate TLBs for all segments */
1693 /* XXX: given the fact that there are too many segments to invalidate,
1694 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
1695 * we just invalidate all TLBs
1699 #endif /* defined(TARGET_PPC64) */
1702 cpu_abort(env
, "Unknown MMU model 2\n");
1706 ppc_tlb_invalidate_all(env
);
1710 #if defined(TARGET_PPC64)
1711 void ppc_slb_invalidate_all (CPUPPCState
*env
)
1717 void ppc_slb_invalidate_one (CPUPPCState
*env
, uint64_t T0
)
1725 /*****************************************************************************/
1726 /* Special registers manipulation */
1727 #if defined(TARGET_PPC64)
1728 target_ulong
ppc_load_asr (CPUPPCState
*env
)
1733 void ppc_store_asr (CPUPPCState
*env
, target_ulong value
)
1735 if (env
->asr
!= value
) {
1742 target_ulong
do_load_sdr1 (CPUPPCState
*env
)
1747 void do_store_sdr1 (CPUPPCState
*env
, target_ulong value
)
1749 #if defined (DEBUG_MMU)
1750 if (loglevel
!= 0) {
1751 fprintf(logfile
, "%s: 0x" ADDRX
"\n", __func__
, value
);
1754 if (env
->sdr1
!= value
) {
1760 target_ulong
do_load_sr (CPUPPCState
*env
, int srnum
)
1762 return env
->sr
[srnum
];
1765 void do_store_sr (CPUPPCState
*env
, int srnum
, target_ulong value
)
1767 #if defined (DEBUG_MMU)
1768 if (loglevel
!= 0) {
1769 fprintf(logfile
, "%s: reg=%d 0x" ADDRX
" " ADDRX
"\n",
1770 __func__
, srnum
, value
, env
->sr
[srnum
]);
1773 if (env
->sr
[srnum
] != value
) {
1774 env
->sr
[srnum
] = value
;
1775 #if !defined(FLUSH_ALL_TLBS) && 0
1777 target_ulong page
, end
;
1778 /* Invalidate 256 MB of virtual memory */
1779 page
= (16 << 20) * srnum
;
1780 end
= page
+ (16 << 20);
1781 for (; page
!= end
; page
+= TARGET_PAGE_SIZE
)
1782 tlb_flush_page(env
, page
);
1789 #endif /* !defined (CONFIG_USER_ONLY) */
1791 target_ulong
ppc_load_xer (CPUPPCState
*env
)
1793 return (xer_so
<< XER_SO
) |
1794 (xer_ov
<< XER_OV
) |
1795 (xer_ca
<< XER_CA
) |
1796 (xer_bc
<< XER_BC
) |
1797 (xer_cmp
<< XER_CMP
);
1800 void ppc_store_xer (CPUPPCState
*env
, target_ulong value
)
1802 xer_so
= (value
>> XER_SO
) & 0x01;
1803 xer_ov
= (value
>> XER_OV
) & 0x01;
1804 xer_ca
= (value
>> XER_CA
) & 0x01;
1805 xer_cmp
= (value
>> XER_CMP
) & 0xFF;
1806 xer_bc
= (value
>> XER_BC
) & 0x7F;
1809 /* Swap temporary saved registers with GPRs */
1810 static inline void swap_gpr_tgpr (CPUPPCState
*env
)
1815 env
->gpr
[0] = env
->tgpr
[0];
1818 env
->gpr
[1] = env
->tgpr
[1];
1821 env
->gpr
[2] = env
->tgpr
[2];
1824 env
->gpr
[3] = env
->tgpr
[3];
1828 /* GDBstub can read and write MSR... */
1829 target_ulong
do_load_msr (CPUPPCState
*env
)
1832 #if defined (TARGET_PPC64)
1833 ((target_ulong
)msr_sf
<< MSR_SF
) |
1834 ((target_ulong
)msr_isf
<< MSR_ISF
) |
1835 ((target_ulong
)msr_hv
<< MSR_HV
) |
1837 ((target_ulong
)msr_ucle
<< MSR_UCLE
) |
1838 ((target_ulong
)msr_vr
<< MSR_VR
) | /* VR / SPE */
1839 ((target_ulong
)msr_ap
<< MSR_AP
) |
1840 ((target_ulong
)msr_sa
<< MSR_SA
) |
1841 ((target_ulong
)msr_key
<< MSR_KEY
) |
1842 ((target_ulong
)msr_pow
<< MSR_POW
) | /* POW / WE */
1843 ((target_ulong
)msr_tlb
<< MSR_TLB
) | /* TLB / TGPE / CE */
1844 ((target_ulong
)msr_ile
<< MSR_ILE
) |
1845 ((target_ulong
)msr_ee
<< MSR_EE
) |
1846 ((target_ulong
)msr_pr
<< MSR_PR
) |
1847 ((target_ulong
)msr_fp
<< MSR_FP
) |
1848 ((target_ulong
)msr_me
<< MSR_ME
) |
1849 ((target_ulong
)msr_fe0
<< MSR_FE0
) |
1850 ((target_ulong
)msr_se
<< MSR_SE
) | /* SE / DWE / UBLE */
1851 ((target_ulong
)msr_be
<< MSR_BE
) | /* BE / DE */
1852 ((target_ulong
)msr_fe1
<< MSR_FE1
) |
1853 ((target_ulong
)msr_al
<< MSR_AL
) |
1854 ((target_ulong
)msr_ip
<< MSR_IP
) |
1855 ((target_ulong
)msr_ir
<< MSR_IR
) | /* IR / IS */
1856 ((target_ulong
)msr_dr
<< MSR_DR
) | /* DR / DS */
1857 ((target_ulong
)msr_pe
<< MSR_PE
) | /* PE / EP */
1858 ((target_ulong
)msr_px
<< MSR_PX
) | /* PX / PMM */
1859 ((target_ulong
)msr_ri
<< MSR_RI
) |
1860 ((target_ulong
)msr_le
<< MSR_LE
);
1863 int do_store_msr (CPUPPCState
*env
, target_ulong value
)
1867 value
&= env
->msr_mask
;
1868 if (((value
>> MSR_IR
) & 1) != msr_ir
||
1869 ((value
>> MSR_DR
) & 1) != msr_dr
) {
1870 /* Flush all tlb when changing translation mode */
1872 env
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1875 if (loglevel
!= 0) {
1876 fprintf(logfile
, "%s: T0 %08lx\n", __func__
, value
);
1879 switch (env
->excp_model
) {
1880 case POWERPC_EXCP_602
:
1881 case POWERPC_EXCP_603
:
1882 case POWERPC_EXCP_603E
:
1883 case POWERPC_EXCP_G2
:
1884 if (((value
>> MSR_TGPR
) & 1) != msr_tgpr
) {
1885 /* Swap temporary saved registers with GPRs */
1892 #if defined (TARGET_PPC64)
1893 msr_sf
= (value
>> MSR_SF
) & 1;
1894 msr_isf
= (value
>> MSR_ISF
) & 1;
1895 msr_hv
= (value
>> MSR_HV
) & 1;
1897 msr_ucle
= (value
>> MSR_UCLE
) & 1;
1898 msr_vr
= (value
>> MSR_VR
) & 1; /* VR / SPE */
1899 msr_ap
= (value
>> MSR_AP
) & 1;
1900 msr_sa
= (value
>> MSR_SA
) & 1;
1901 msr_key
= (value
>> MSR_KEY
) & 1;
1902 msr_pow
= (value
>> MSR_POW
) & 1; /* POW / WE */
1903 msr_tlb
= (value
>> MSR_TLB
) & 1; /* TLB / TGPR / CE */
1904 msr_ile
= (value
>> MSR_ILE
) & 1;
1905 msr_ee
= (value
>> MSR_EE
) & 1;
1906 msr_pr
= (value
>> MSR_PR
) & 1;
1907 msr_fp
= (value
>> MSR_FP
) & 1;
1908 msr_me
= (value
>> MSR_ME
) & 1;
1909 msr_fe0
= (value
>> MSR_FE0
) & 1;
1910 msr_se
= (value
>> MSR_SE
) & 1; /* SE / DWE / UBLE */
1911 msr_be
= (value
>> MSR_BE
) & 1; /* BE / DE */
1912 msr_fe1
= (value
>> MSR_FE1
) & 1;
1913 msr_al
= (value
>> MSR_AL
) & 1;
1914 msr_ip
= (value
>> MSR_IP
) & 1;
1915 msr_ir
= (value
>> MSR_IR
) & 1; /* IR / IS */
1916 msr_dr
= (value
>> MSR_DR
) & 1; /* DR / DS */
1917 msr_pe
= (value
>> MSR_PE
) & 1; /* PE / EP */
1918 msr_px
= (value
>> MSR_PX
) & 1; /* PX / PMM */
1919 msr_ri
= (value
>> MSR_RI
) & 1;
1920 msr_le
= (value
>> MSR_LE
) & 1;
1921 do_compute_hflags(env
);
1924 switch (env
->excp_model
) {
1925 case POWERPC_EXCP_603
:
1926 case POWERPC_EXCP_603E
:
1927 case POWERPC_EXCP_G2
:
1928 /* Don't handle SLEEP mode: we should disable all clocks...
1929 * No dynamic power-management.
1931 if (msr_pow
== 1 && (env
->spr
[SPR_HID0
] & 0x00C00000) != 0)
1934 case POWERPC_EXCP_604
:
1938 case POWERPC_EXCP_7x0
:
1939 if (msr_pow
== 1 && (env
->spr
[SPR_HID0
] & 0x00E00000) != 0)
1949 #if defined(TARGET_PPC64)
1950 int ppc_store_msr_32 (CPUPPCState
*env
, uint32_t value
)
1952 return do_store_msr(env
, (do_load_msr(env
) & ~0xFFFFFFFFULL
) |
1953 (value
& 0xFFFFFFFF));
1957 void do_compute_hflags (CPUPPCState
*env
)
1959 /* Compute current hflags */
1960 env
->hflags
= (msr_vr
<< MSR_VR
) |
1961 (msr_ap
<< MSR_AP
) | (msr_sa
<< MSR_SA
) | (msr_pr
<< MSR_PR
) |
1962 (msr_fp
<< MSR_FP
) | (msr_fe0
<< MSR_FE0
) | (msr_se
<< MSR_SE
) |
1963 (msr_be
<< MSR_BE
) | (msr_fe1
<< MSR_FE1
) | (msr_le
<< MSR_LE
);
1964 #if defined (TARGET_PPC64)
1965 env
->hflags
|= msr_cm
<< MSR_CM
;
1966 env
->hflags
|= (uint64_t)msr_sf
<< MSR_SF
;
1967 env
->hflags
|= (uint64_t)msr_hv
<< MSR_HV
;
1971 /*****************************************************************************/
1972 /* Exception processing */
1973 #if defined (CONFIG_USER_ONLY)
1974 void do_interrupt (CPUState
*env
)
1976 env
->exception_index
= POWERPC_EXCP_NONE
;
1977 env
->error_code
= 0;
1980 void ppc_hw_interrupt (CPUState
*env
)
1982 env
->exception_index
= POWERPC_EXCP_NONE
;
1983 env
->error_code
= 0;
1985 #else /* defined (CONFIG_USER_ONLY) */
1986 static void dump_syscall (CPUState
*env
)
1988 fprintf(logfile
, "syscall r0=0x" REGX
" r3=0x" REGX
" r4=0x" REGX
1989 " r5=0x" REGX
" r6=0x" REGX
" nip=0x" ADDRX
"\n",
1990 env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
1991 env
->gpr
[5], env
->gpr
[6], env
->nip
);
1994 /* Note that this function should be greatly optimized
1995 * when called with a constant excp, from ppc_hw_interrupt
1997 static always_inline
void powerpc_excp (CPUState
*env
,
1998 int excp_model
, int excp
)
2000 target_ulong msr
, vector
;
2001 int srr0
, srr1
, asrr0
, asrr1
;
2003 if (loglevel
& CPU_LOG_INT
) {
2004 fprintf(logfile
, "Raise exception at 0x" ADDRX
" => 0x%08x (%02x)\n",
2005 env
->nip
, excp
, env
->error_code
);
2007 msr
= do_load_msr(env
);
2012 msr
&= ~((target_ulong
)0x783F0000);
2014 case POWERPC_EXCP_NONE
:
2015 /* Should never happen */
2017 case POWERPC_EXCP_CRITICAL
: /* Critical input */
2018 msr_ri
= 0; /* XXX: check this */
2019 switch (excp_model
) {
2020 case POWERPC_EXCP_40x
:
2021 srr0
= SPR_40x_SRR2
;
2022 srr1
= SPR_40x_SRR3
;
2024 case POWERPC_EXCP_BOOKE
:
2025 srr0
= SPR_BOOKE_CSRR0
;
2026 srr1
= SPR_BOOKE_CSRR1
;
2028 case POWERPC_EXCP_G2
:
2034 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
2036 /* Machine check exception is not enabled */
2037 /* XXX: we may just stop the processor here, to allow debugging */
2038 excp
= POWERPC_EXCP_RESET
;
2043 #if defined(TARGET_PPC64H)
2046 /* XXX: should also have something loaded in DAR / DSISR */
2047 switch (excp_model
) {
2048 case POWERPC_EXCP_40x
:
2049 srr0
= SPR_40x_SRR2
;
2050 srr1
= SPR_40x_SRR3
;
2052 case POWERPC_EXCP_BOOKE
:
2053 srr0
= SPR_BOOKE_MCSRR0
;
2054 srr1
= SPR_BOOKE_MCSRR1
;
2055 asrr0
= SPR_BOOKE_CSRR0
;
2056 asrr1
= SPR_BOOKE_CSRR1
;
2062 case POWERPC_EXCP_DSI
: /* Data storage exception */
2063 #if defined (DEBUG_EXCEPTIONS)
2064 if (loglevel
!= 0) {
2065 fprintf(logfile
, "DSI exception: DSISR=0x" ADDRX
" DAR=0x" ADDRX
2066 "\n", env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
2070 #if defined(TARGET_PPC64H)
2075 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
2076 #if defined (DEBUG_EXCEPTIONS)
2077 if (loglevel
!= 0) {
2078 fprintf(logfile
, "ISI exception: msr=0x" ADDRX
", nip=0x" ADDRX
2079 "\n", msr
, env
->nip
);
2083 #if defined(TARGET_PPC64H)
2087 msr
|= env
->error_code
;
2089 case POWERPC_EXCP_EXTERNAL
: /* External input */
2091 #if defined(TARGET_PPC64H)
2096 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
2098 #if defined(TARGET_PPC64H)
2102 /* XXX: this is false */
2103 /* Get rS/rD and rA from faulting opcode */
2104 env
->spr
[SPR_DSISR
] |= (ldl_code((env
->nip
- 4)) & 0x03FF0000) >> 16;
2106 case POWERPC_EXCP_PROGRAM
: /* Program exception */
2107 switch (env
->error_code
& ~0xF) {
2108 case POWERPC_EXCP_FP
:
2109 if ((msr_fe0
== 0 && msr_fe1
== 0) || msr_fp
== 0) {
2110 #if defined (DEBUG_EXCEPTIONS)
2111 if (loglevel
!= 0) {
2112 fprintf(logfile
, "Ignore floating point exception\n");
2118 #if defined(TARGET_PPC64H)
2124 env
->fpscr
[7] |= 0x8;
2125 /* Finally, update FEX */
2126 if ((((env
->fpscr
[7] & 0x3) << 3) | (env
->fpscr
[6] >> 1)) &
2127 ((env
->fpscr
[1] << 1) | (env
->fpscr
[0] >> 3)))
2128 env
->fpscr
[7] |= 0x4;
2129 if (msr_fe0
!= msr_fe1
) {
2134 case POWERPC_EXCP_INVAL
:
2135 #if defined (DEBUG_EXCEPTIONS)
2136 if (loglevel
!= 0) {
2137 fprintf(logfile
, "Invalid instruction at 0x" ADDRX
"\n",
2142 #if defined(TARGET_PPC64H)
2148 case POWERPC_EXCP_PRIV
:
2150 #if defined(TARGET_PPC64H)
2156 case POWERPC_EXCP_TRAP
:
2158 #if defined(TARGET_PPC64H)
2165 /* Should never occur */
2166 cpu_abort(env
, "Invalid program exception %d. Aborting\n",
2171 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
2173 #if defined(TARGET_PPC64H)
2178 case POWERPC_EXCP_SYSCALL
: /* System call exception */
2179 /* NOTE: this is a temporary hack to support graphics OSI
2180 calls from the MOL driver */
2181 /* XXX: To be removed */
2182 if (env
->gpr
[3] == 0x113724fa && env
->gpr
[4] == 0x77810f9b &&
2184 if (env
->osi_call(env
) != 0)
2187 if (loglevel
& CPU_LOG_INT
) {
2191 #if defined(TARGET_PPC64H)
2192 if (lev
== 1 || (lpes0
== 0 && lpes1
== 0))
2196 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
2199 case POWERPC_EXCP_DECR
: /* Decrementer exception */
2201 #if defined(TARGET_PPC64H)
2206 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
2208 #if defined (DEBUG_EXCEPTIONS)
2210 fprintf(logfile
, "FIT exception\n");
2212 msr_ri
= 0; /* XXX: check this */
2214 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
2215 #if defined (DEBUG_EXCEPTIONS)
2217 fprintf(logfile
, "WDT exception\n");
2219 switch (excp_model
) {
2220 case POWERPC_EXCP_BOOKE
:
2221 srr0
= SPR_BOOKE_CSRR0
;
2222 srr1
= SPR_BOOKE_CSRR1
;
2227 msr_ri
= 0; /* XXX: check this */
2229 case POWERPC_EXCP_DTLB
: /* Data TLB error */
2230 msr_ri
= 0; /* XXX: check this */
2232 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
2233 msr_ri
= 0; /* XXX: check this */
2235 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
2236 switch (excp_model
) {
2237 case POWERPC_EXCP_BOOKE
:
2238 srr0
= SPR_BOOKE_DSRR0
;
2239 srr1
= SPR_BOOKE_DSRR1
;
2240 asrr0
= SPR_BOOKE_CSRR0
;
2241 asrr1
= SPR_BOOKE_CSRR1
;
2247 cpu_abort(env
, "Debug exception is not implemented yet !\n");
2249 #if defined(TARGET_PPCEMB)
2250 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavailable */
2251 msr_ri
= 0; /* XXX: check this */
2253 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data interrupt */
2255 cpu_abort(env
, "Embedded floating point data exception "
2256 "is not implemented yet !\n");
2258 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round interrupt */
2260 cpu_abort(env
, "Embedded floating point round exception "
2261 "is not implemented yet !\n");
2263 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor interrupt */
2267 "Performance counter exception is not implemented yet !\n");
2269 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
2272 "Embedded doorbell interrupt is not implemented yet !\n");
2274 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
2275 switch (excp_model
) {
2276 case POWERPC_EXCP_BOOKE
:
2277 srr0
= SPR_BOOKE_CSRR0
;
2278 srr1
= SPR_BOOKE_CSRR1
;
2284 cpu_abort(env
, "Embedded doorbell critical interrupt "
2285 "is not implemented yet !\n");
2287 #endif /* defined(TARGET_PPCEMB) */
2288 case POWERPC_EXCP_RESET
: /* System reset exception */
2290 #if defined(TARGET_PPC64H)
2295 #if defined(TARGET_PPC64)
2296 case POWERPC_EXCP_DSEG
: /* Data segment exception */
2298 #if defined(TARGET_PPC64H)
2303 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
2305 #if defined(TARGET_PPC64H)
2310 #endif /* defined(TARGET_PPC64) */
2311 #if defined(TARGET_PPC64H)
2312 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
2318 case POWERPC_EXCP_TRACE
: /* Trace exception */
2320 #if defined(TARGET_PPC64H)
2325 #if defined(TARGET_PPC64H)
2326 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
2331 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage exception */
2336 cpu_abort(env
, "Hypervisor instruction storage exception "
2337 "is not implemented yet !\n");
2339 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
2344 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment exception */
2349 #endif /* defined(TARGET_PPC64H) */
2350 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
2352 #if defined(TARGET_PPC64H)
2357 case POWERPC_EXCP_PIT
: /* Programmable interval timer interrupt */
2358 #if defined (DEBUG_EXCEPTIONS)
2360 fprintf(logfile
, "PIT exception\n");
2362 msr_ri
= 0; /* XXX: check this */
2364 case POWERPC_EXCP_IO
: /* IO error exception */
2366 cpu_abort(env
, "601 IO error exception is not implemented yet !\n");
2368 case POWERPC_EXCP_RUNM
: /* Run mode exception */
2370 cpu_abort(env
, "601 run mode exception is not implemented yet !\n");
2372 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
2374 cpu_abort(env
, "602 emulation trap exception "
2375 "is not implemented yet !\n");
2377 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
2378 msr_ri
= 0; /* XXX: check this */
2379 #if defined(TARGET_PPC64H) /* XXX: check this */
2383 switch (excp_model
) {
2384 case POWERPC_EXCP_602
:
2385 case POWERPC_EXCP_603
:
2386 case POWERPC_EXCP_603E
:
2387 case POWERPC_EXCP_G2
:
2389 case POWERPC_EXCP_7x5
:
2391 case POWERPC_EXCP_74xx
:
2394 cpu_abort(env
, "Invalid instruction TLB miss exception\n");
2398 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
2399 msr_ri
= 0; /* XXX: check this */
2400 #if defined(TARGET_PPC64H) /* XXX: check this */
2404 switch (excp_model
) {
2405 case POWERPC_EXCP_602
:
2406 case POWERPC_EXCP_603
:
2407 case POWERPC_EXCP_603E
:
2408 case POWERPC_EXCP_G2
:
2410 case POWERPC_EXCP_7x5
:
2412 case POWERPC_EXCP_74xx
:
2415 cpu_abort(env
, "Invalid data load TLB miss exception\n");
2419 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
2420 msr_ri
= 0; /* XXX: check this */
2421 #if defined(TARGET_PPC64H) /* XXX: check this */
2425 switch (excp_model
) {
2426 case POWERPC_EXCP_602
:
2427 case POWERPC_EXCP_603
:
2428 case POWERPC_EXCP_603E
:
2429 case POWERPC_EXCP_G2
:
2431 /* Swap temporary saved registers with GPRs */
2435 case POWERPC_EXCP_7x5
:
2437 #if defined (DEBUG_SOFTWARE_TLB)
2438 if (loglevel
!= 0) {
2439 const unsigned char *es
;
2440 target_ulong
*miss
, *cmp
;
2442 if (excp
== POWERPC_EXCP_IFTLB
) {
2445 miss
= &env
->spr
[SPR_IMISS
];
2446 cmp
= &env
->spr
[SPR_ICMP
];
2448 if (excp
== POWERPC_EXCP_DLTLB
)
2453 miss
= &env
->spr
[SPR_DMISS
];
2454 cmp
= &env
->spr
[SPR_DCMP
];
2456 fprintf(logfile
, "6xx %sTLB miss: %cM " ADDRX
" %cC " ADDRX
2457 " H1 " ADDRX
" H2 " ADDRX
" %08x\n",
2458 es
, en
, *miss
, en
, *cmp
,
2459 env
->spr
[SPR_HASH1
], env
->spr
[SPR_HASH2
],
2463 msr
|= env
->crf
[0] << 28;
2464 msr
|= env
->error_code
; /* key, D/I, S/L bits */
2465 /* Set way using a LRU mechanism */
2466 msr
|= ((env
->last_way
+ 1) & (env
->nb_ways
- 1)) << 17;
2468 case POWERPC_EXCP_74xx
:
2470 #if defined (DEBUG_SOFTWARE_TLB)
2471 if (loglevel
!= 0) {
2472 const unsigned char *es
;
2473 target_ulong
*miss
, *cmp
;
2475 if (excp
== POWERPC_EXCP_IFTLB
) {
2478 miss
= &env
->spr
[SPR_IMISS
];
2479 cmp
= &env
->spr
[SPR_ICMP
];
2481 if (excp
== POWERPC_EXCP_DLTLB
)
2486 miss
= &env
->spr
[SPR_TLBMISS
];
2487 cmp
= &env
->spr
[SPR_PTEHI
];
2489 fprintf(logfile
, "74xx %sTLB miss: %cM " ADDRX
" %cC " ADDRX
2491 es
, en
, *miss
, en
, *cmp
, env
->error_code
);
2494 msr
|= env
->error_code
; /* key bit */
2497 cpu_abort(env
, "Invalid data store TLB miss exception\n");
2501 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
2503 cpu_abort(env
, "Floating point assist exception "
2504 "is not implemented yet !\n");
2506 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
2508 cpu_abort(env
, "IABR exception is not implemented yet !\n");
2510 case POWERPC_EXCP_SMI
: /* System management interrupt */
2512 cpu_abort(env
, "SMI exception is not implemented yet !\n");
2514 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
2516 cpu_abort(env
, "Thermal management exception "
2517 "is not implemented yet !\n");
2519 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor interrupt */
2521 #if defined(TARGET_PPC64H)
2527 "Performance counter exception is not implemented yet !\n");
2529 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
2531 cpu_abort(env
, "VPU assist exception is not implemented yet !\n");
2533 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
2536 "970 soft-patch exception is not implemented yet !\n");
2538 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
2541 "970 maintenance exception is not implemented yet !\n");
2545 cpu_abort(env
, "Invalid PowerPC exception %d. Aborting\n", excp
);
2548 /* save current instruction location */
2549 env
->spr
[srr0
] = env
->nip
- 4;
2552 /* save next instruction location */
2553 env
->spr
[srr0
] = env
->nip
;
2557 env
->spr
[srr1
] = msr
;
2558 /* If any alternate SRR register are defined, duplicate saved values */
2560 env
->spr
[asrr0
] = env
->spr
[srr0
];
2562 env
->spr
[asrr1
] = env
->spr
[srr1
];
2563 /* If we disactivated any translation, flush TLBs */
2564 if (msr_ir
|| msr_dr
)
2566 /* reload MSR with correct bits */
2576 #if 0 /* Fix this: not on all targets */
2580 do_compute_hflags(env
);
2581 /* Jump to handler */
2582 vector
= env
->excp_vectors
[excp
];
2583 if (vector
== (target_ulong
)-1) {
2584 cpu_abort(env
, "Raised an exception without defined vector %d\n",
2587 vector
|= env
->excp_prefix
;
2588 #if defined(TARGET_PPC64)
2589 if (excp_model
== POWERPC_EXCP_BOOKE
) {
2592 vector
= (uint32_t)vector
;
2596 vector
= (uint32_t)vector
;
2600 /* Reset exception state */
2601 env
->exception_index
= POWERPC_EXCP_NONE
;
2602 env
->error_code
= 0;
2605 void do_interrupt (CPUState
*env
)
2607 powerpc_excp(env
, env
->excp_model
, env
->exception_index
);
2610 void ppc_hw_interrupt (CPUPPCState
*env
)
2613 if (loglevel
& CPU_LOG_INT
) {
2614 fprintf(logfile
, "%s: %p pending %08x req %08x me %d ee %d\n",
2615 __func__
, env
, env
->pending_interrupts
,
2616 env
->interrupt_request
, msr_me
, msr_ee
);
2619 /* External reset */
2620 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_RESET
)) {
2621 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_RESET
);
2622 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_RESET
);
2625 /* Machine check exception */
2626 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_MCK
)) {
2627 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_MCK
);
2628 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_MCHECK
);
2632 /* External debug exception */
2633 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DEBUG
)) {
2634 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DEBUG
);
2635 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DEBUG
);
2639 #if defined(TARGET_PPC64H)
2640 if ((msr_ee
!= 0 || msr_hv
== 0 || msr_pr
== 1) & hdice
!= 0) {
2641 /* Hypervisor decrementer exception */
2642 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_HDECR
)) {
2643 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_HDECR
);
2644 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_HDECR
);
2650 /* External critical interrupt */
2651 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CEXT
)) {
2652 /* Taking a critical external interrupt does not clear the external
2653 * critical interrupt status
2656 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CEXT
);
2658 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_CRITICAL
);
2663 /* Watchdog timer on embedded PowerPC */
2664 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_WDT
)) {
2665 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_WDT
);
2666 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_WDT
);
2669 #if defined(TARGET_PPCEMB)
2670 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CDOORBELL
)) {
2671 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CDOORBELL
);
2672 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DOORCI
);
2676 #if defined(TARGET_PPCEMB)
2677 /* External interrupt */
2678 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_EXT
)) {
2679 /* Taking an external interrupt does not clear the external
2683 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_EXT
);
2685 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_EXTERNAL
);
2689 /* Fixed interval timer on embedded PowerPC */
2690 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_FIT
)) {
2691 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_FIT
);
2692 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_FIT
);
2695 /* Programmable interval timer on embedded PowerPC */
2696 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PIT
)) {
2697 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PIT
);
2698 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_PIT
);
2701 /* Decrementer exception */
2702 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DECR
)) {
2703 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DECR
);
2704 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DECR
);
2707 #if !defined(TARGET_PPCEMB)
2708 /* External interrupt */
2709 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_EXT
)) {
2710 /* Taking an external interrupt does not clear the external
2714 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_EXT
);
2716 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_EXTERNAL
);
2720 #if defined(TARGET_PPCEMB)
2721 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DOORBELL
)) {
2722 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DOORBELL
);
2723 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_DOORI
);
2727 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PERFM
)) {
2728 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PERFM
);
2729 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_PERFM
);
2732 /* Thermal interrupt */
2733 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_THERM
)) {
2734 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_THERM
);
2735 powerpc_excp(env
, env
->excp_model
, POWERPC_EXCP_THERM
);
2740 #endif /* !CONFIG_USER_ONLY */
2742 void cpu_dump_EA (target_ulong EA
)
2752 fprintf(f
, "Memory access at address " ADDRX
"\n", EA
);
2755 void cpu_dump_rfi (target_ulong RA
, target_ulong msr
)
2765 fprintf(f
, "Return from exception at " ADDRX
" with flags " ADDRX
"\n",
2769 void cpu_ppc_reset (void *opaque
)
2775 /* XXX: some of those flags initialisation values could depend
2776 * on the actual PowerPC implementation
2778 for (i
= 0; i
< 63; i
++)
2780 #if defined(TARGET_PPC64)
2781 msr_hv
= 0; /* Should be 1... */
2783 msr_ap
= 0; /* TO BE CHECKED */
2784 msr_sa
= 0; /* TO BE CHECKED */
2785 msr_ip
= 0; /* TO BE CHECKED */
2786 #if defined (DO_SINGLE_STEP) && 0
2787 /* Single step trace mode */
2791 #if defined(CONFIG_USER_ONLY)
2792 msr_fp
= 1; /* Allow floating point exceptions */
2795 #if defined(TARGET_PPC64)
2796 env
->nip
= 0x00000100;
2798 env
->nip
= 0xFFFFFFFC;
2800 ppc_tlb_invalidate_all(env
);
2802 do_compute_hflags(env
);
2804 /* Be sure no exception or interrupt is pending */
2805 env
->pending_interrupts
= 0;
2806 env
->exception_index
= POWERPC_EXCP_NONE
;
2807 env
->error_code
= 0;
2808 /* Flush all TLBs */
2812 CPUPPCState
*cpu_ppc_init (void)
2816 env
= qemu_mallocz(sizeof(CPUPPCState
));
2824 void cpu_ppc_close (CPUPPCState
*env
)
2826 /* Should also remove all opcode tables... */