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"
36 //#define DEBUG_SOFTWARE_TLB
37 //#define DUMP_PAGE_TABLES
38 //#define DEBUG_EXCEPTIONS
39 //#define FLUSH_ALL_TLBS
41 /*****************************************************************************/
42 /* PowerPC MMU emulation */
44 #if defined(CONFIG_USER_ONLY)
45 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
46 int mmu_idx, int is_softmmu)
48 int exception, error_code;
51 exception = POWERPC_EXCP_ISI;
52 error_code = 0x40000000;
54 exception = POWERPC_EXCP_DSI;
55 error_code = 0x40000000;
57 error_code |= 0x02000000;
58 env->spr[SPR_DAR] = address;
59 env->spr[SPR_DSISR] = error_code;
61 env->exception_index = exception;
62 env->error_code = error_code;
67 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
73 /* Common routines used by software and hardware TLBs emulation */
74 static always_inline int pte_is_valid (target_ulong pte0)
76 return pte0 & 0x80000000 ? 1 : 0;
79 static always_inline void pte_invalidate (target_ulong *pte0)
84 #if defined(TARGET_PPC64)
85 static always_inline int pte64_is_valid (target_ulong pte0)
87 return pte0 & 0x0000000000000001ULL ? 1 : 0;
90 static always_inline void pte64_invalidate (target_ulong *pte0)
92 *pte0 &= ~0x0000000000000001ULL;
96 #define PTE_PTEM_MASK 0x7FFFFFBF
97 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
98 #if defined(TARGET_PPC64)
99 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
100 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
103 static always_inline int pp_check (int key, int pp, int nx)
107 /* Compute access rights */
108 /* When pp is 3/7, the result is undefined. Set it to noaccess */
115 access |= PAGE_WRITE;
133 access = PAGE_READ | PAGE_WRITE;
143 static always_inline int check_prot (int prot, int rw, int access_type)
147 if (access_type == ACCESS_CODE) {
148 if (prot & PAGE_EXEC)
153 if (prot & PAGE_WRITE)
158 if (prot & PAGE_READ)
167 static always_inline int _pte_check (mmu_ctx_t *ctx, int is_64b,
168 target_ulong pte0, target_ulong pte1,
169 int h, int rw, int type)
171 target_ulong ptem, mmask;
172 int access, ret, pteh, ptev, pp;
176 /* Check validity and table match */
177 #if defined(TARGET_PPC64)
179 ptev = pte64_is_valid(pte0);
180 pteh = (pte0 >> 1) & 1;
184 ptev = pte_is_valid(pte0);
185 pteh = (pte0 >> 6) & 1;
187 if (ptev && h == pteh) {
188 /* Check vsid & api */
189 #if defined(TARGET_PPC64)
191 ptem = pte0 & PTE64_PTEM_MASK;
192 mmask = PTE64_CHECK_MASK;
193 pp = (pte1 & 0x00000003) | ((pte1 >> 61) & 0x00000004);
194 ctx->nx |= (pte1 >> 2) & 1; /* No execute bit */
195 ctx->nx |= (pte1 >> 3) & 1; /* Guarded bit */
199 ptem = pte0 & PTE_PTEM_MASK;
200 mmask = PTE_CHECK_MASK;
201 pp = pte1 & 0x00000003;
203 if (ptem == ctx->ptem) {
204 if (ctx->raddr != (target_phys_addr_t)-1ULL) {
205 /* all matches should have equal RPN, WIMG & PP */
206 if ((ctx->raddr & mmask) != (pte1 & mmask)) {
208 fprintf(logfile, "Bad RPN/WIMG/PP\n");
212 /* Compute access rights */
213 access = pp_check(ctx->key, pp, ctx->nx);
214 /* Keep the matching PTE informations */
217 ret = check_prot(ctx->prot, rw, type);
220 #if defined (DEBUG_MMU)
222 fprintf(logfile, "PTE access granted !\n");
225 /* Access right violation */
226 #if defined (DEBUG_MMU)
228 fprintf(logfile, "PTE access rejected\n");
237 static always_inline int pte32_check (mmu_ctx_t *ctx,
238 target_ulong pte0, target_ulong pte1,
239 int h, int rw, int type)
241 return _pte_check(ctx, 0, pte0, pte1, h, rw, type);
244 #if defined(TARGET_PPC64)
245 static always_inline int pte64_check (mmu_ctx_t *ctx,
246 target_ulong pte0, target_ulong pte1,
247 int h, int rw, int type)
249 return _pte_check(ctx, 1, pte0, pte1, h, rw, type);
253 static always_inline int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
258 /* Update page flags */
259 if (!(*pte1p & 0x00000100)) {
260 /* Update accessed flag */
261 *pte1p |= 0x00000100;
264 if (!(*pte1p & 0x00000080)) {
265 if (rw == 1 && ret == 0) {
266 /* Update changed flag */
267 *pte1p |= 0x00000080;
270 /* Force page fault for first write access */
271 ctx->prot &= ~PAGE_WRITE;
278 /* Software driven TLB helpers */
279 static always_inline int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
280 int way, int is_code)
284 /* Select TLB num in a way from address */
285 nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
287 nr += env->tlb_per_way * way;
288 /* 6xx have separate TLBs for instructions and data */
289 if (is_code && env->id_tlbs == 1)
295 static always_inline void ppc6xx_tlb_invalidate_all (CPUState *env)
300 #if defined (DEBUG_SOFTWARE_TLB) && 0
302 fprintf(logfile, "Invalidate all TLBs\n");
305 /* Invalidate all defined software TLB */
307 if (env->id_tlbs == 1)
309 for (nr = 0; nr < max; nr++) {
310 tlb = &env->tlb[nr].tlb6;
311 pte_invalidate(&tlb->pte0);
316 static always_inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
321 #if !defined(FLUSH_ALL_TLBS)
325 /* Invalidate ITLB + DTLB, all ways */
326 for (way = 0; way < env->nb_ways; way++) {
327 nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
328 tlb = &env->tlb[nr].tlb6;
329 if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
330 #if defined (DEBUG_SOFTWARE_TLB)
332 fprintf(logfile, "TLB invalidate %d/%d " ADDRX "\n",
333 nr, env->nb_tlb, eaddr);
336 pte_invalidate(&tlb->pte0);
337 tlb_flush_page(env, tlb->EPN);
341 /* XXX: PowerPC specification say this is valid as well */
342 ppc6xx_tlb_invalidate_all(env);
346 static always_inline void ppc6xx_tlb_invalidate_virt (CPUState *env,
350 __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
353 void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
354 target_ulong pte0, target_ulong pte1)
359 nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
360 tlb = &env->tlb[nr].tlb6;
361 #if defined (DEBUG_SOFTWARE_TLB)
363 fprintf(logfile, "Set TLB %d/%d EPN " ADDRX " PTE0 " ADDRX
364 " PTE1 " ADDRX "\n", nr, env->nb_tlb, EPN, pte0, pte1);
367 /* Invalidate any pending reference in Qemu for this virtual address */
368 __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
372 /* Store last way for LRU mechanism */
376 static always_inline int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
377 target_ulong eaddr, int rw,
385 ret = -1; /* No TLB found */
386 for (way = 0; way < env->nb_ways; way++) {
387 nr = ppc6xx_tlb_getnum(env, eaddr, way,
388 access_type == ACCESS_CODE ? 1 : 0);
389 tlb = &env->tlb[nr].tlb6;
390 /* This test "emulates" the PTE index match for hardware TLBs */
391 if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
392 #if defined (DEBUG_SOFTWARE_TLB)
394 fprintf(logfile, "TLB %d/%d %s [" ADDRX " " ADDRX
397 pte_is_valid(tlb->pte0) ? "valid" : "inval",
398 tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
403 #if defined (DEBUG_SOFTWARE_TLB)
405 fprintf(logfile, "TLB %d/%d %s " ADDRX " <> " ADDRX " " ADDRX
408 pte_is_valid(tlb->pte0) ? "valid" : "inval",
409 tlb->EPN, eaddr, tlb->pte1,
410 rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
413 switch (pte32_check(ctx, tlb->pte0, tlb->pte1, 0, rw, access_type)) {
415 /* TLB inconsistency */
418 /* Access violation */
428 /* XXX: we should go on looping to check all TLBs consistency
429 * but we can speed-up the whole thing as the
430 * result would be undefined if TLBs are not consistent.
439 #if defined (DEBUG_SOFTWARE_TLB)
441 fprintf(logfile, "found TLB at addr " PADDRX " prot=%01x ret=%d\n",
442 ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
445 /* Update page flags */
446 pte_update_flags(ctx, &env->tlb[best].tlb6.pte1, ret, rw);
452 /* Perform BAT hit & translation */
453 static always_inline void bat_size_prot (CPUState *env, target_ulong *blp,
454 int *validp, int *protp,
455 target_ulong *BATu, target_ulong *BATl)
460 bl = (*BATu & 0x00001FFC) << 15;
463 if (((msr_pr == 0) && (*BATu & 0x00000002)) ||
464 ((msr_pr != 0) && (*BATu & 0x00000001))) {
466 pp = *BATl & 0x00000003;
468 prot = PAGE_READ | PAGE_EXEC;
478 static always_inline void bat_601_size_prot (CPUState *env,target_ulong *blp,
479 int *validp, int *protp,
484 int key, pp, valid, prot;
486 bl = (*BATl & 0x0000003F) << 17;
487 #if defined (DEBUG_BATS)
489 fprintf(logfile, "b %02x ==> bl " ADDRX " msk " ADDRX "\n",
490 (uint8_t)(*BATl & 0x0000003F), bl, ~bl);
494 valid = (*BATl >> 6) & 1;
496 pp = *BATu & 0x00000003;
498 key = (*BATu >> 3) & 1;
500 key = (*BATu >> 2) & 1;
501 prot = pp_check(key, pp, 0);
508 static always_inline int get_bat (CPUState *env, mmu_ctx_t *ctx,
509 target_ulong virtual, int rw, int type)
511 target_ulong *BATlt, *BATut, *BATu, *BATl;
512 target_ulong base, BEPIl, BEPIu, bl;
516 #if defined (DEBUG_BATS)
518 fprintf(logfile, "%s: %cBAT v " ADDRX "\n", __func__,
519 type == ACCESS_CODE ? 'I' : 'D', virtual);
524 BATlt = env->IBAT[1];
525 BATut = env->IBAT[0];
528 BATlt = env->DBAT[1];
529 BATut = env->DBAT[0];
532 base = virtual & 0xFFFC0000;
533 for (i = 0; i < env->nb_BATs; i++) {
536 BEPIu = *BATu & 0xF0000000;
537 BEPIl = *BATu & 0x0FFE0000;
538 if (unlikely(env->mmu_model == POWERPC_MMU_601)) {
539 bat_601_size_prot(env, &bl, &valid, &prot, BATu, BATl);
541 bat_size_prot(env, &bl, &valid, &prot, BATu, BATl);
543 #if defined (DEBUG_BATS)
545 fprintf(logfile, "%s: %cBAT%d v " ADDRX " BATu " ADDRX
546 " BATl " ADDRX "\n", __func__,
547 type == ACCESS_CODE ? 'I' : 'D', i, virtual, *BATu, *BATl);
550 if ((virtual & 0xF0000000) == BEPIu &&
551 ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
554 /* Get physical address */
555 ctx->raddr = (*BATl & 0xF0000000) |
556 ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
557 (virtual & 0x0001F000);
558 /* Compute access rights */
560 ret = check_prot(ctx->prot, rw, type);
561 #if defined (DEBUG_BATS)
562 if (ret == 0 && loglevel != 0) {
563 fprintf(logfile, "BAT %d match: r " PADDRX " prot=%c%c\n",
564 i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
565 ctx->prot & PAGE_WRITE ? 'W' : '-');
573 #if defined (DEBUG_BATS)
575 fprintf(logfile, "no BAT match for " ADDRX ":\n", virtual);
576 for (i = 0; i < 4; i++) {
579 BEPIu = *BATu & 0xF0000000;
580 BEPIl = *BATu & 0x0FFE0000;
581 bl = (*BATu & 0x00001FFC) << 15;
582 fprintf(logfile, "%s: %cBAT%d v " ADDRX " BATu " ADDRX
583 " BATl " ADDRX " \n\t" ADDRX " " ADDRX " " ADDRX "\n",
584 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
585 *BATu, *BATl, BEPIu, BEPIl, bl);
595 /* PTE table lookup */
596 static always_inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h,
599 target_ulong base, pte0, pte1;
603 ret = -1; /* No entry found */
604 base = ctx->pg_addr[h];
605 for (i = 0; i < 8; i++) {
606 #if defined(TARGET_PPC64)
608 pte0 = ldq_phys(base + (i * 16));
609 pte1 = ldq_phys(base + (i * 16) + 8);
610 r = pte64_check(ctx, pte0, pte1, h, rw, type);
611 #if defined (DEBUG_MMU)
613 fprintf(logfile, "Load pte from " ADDRX " => " ADDRX " " ADDRX
614 " %d %d %d " ADDRX "\n",
615 base + (i * 16), pte0, pte1,
616 (int)(pte0 & 1), h, (int)((pte0 >> 1) & 1),
623 pte0 = ldl_phys(base + (i * 8));
624 pte1 = ldl_phys(base + (i * 8) + 4);
625 r = pte32_check(ctx, pte0, pte1, h, rw, type);
626 #if defined (DEBUG_MMU)
628 fprintf(logfile, "Load pte from " ADDRX " => " ADDRX " " ADDRX
629 " %d %d %d " ADDRX "\n",
630 base + (i * 8), pte0, pte1,
631 (int)(pte0 >> 31), h, (int)((pte0 >> 6) & 1),
638 /* PTE inconsistency */
641 /* Access violation */
651 /* XXX: we should go on looping to check all PTEs consistency
652 * but if we can speed-up the whole thing as the
653 * result would be undefined if PTEs are not consistent.
662 #if defined (DEBUG_MMU)
664 fprintf(logfile, "found PTE at addr " PADDRX " prot=%01x ret=%d\n",
665 ctx->raddr, ctx->prot, ret);
668 /* Update page flags */
670 if (pte_update_flags(ctx, &pte1, ret, rw) == 1) {
671 #if defined(TARGET_PPC64)
673 stq_phys_notdirty(base + (good * 16) + 8, pte1);
677 stl_phys_notdirty(base + (good * 8) + 4, pte1);
685 static always_inline int find_pte32 (mmu_ctx_t *ctx, int h, int rw, int type)
687 return _find_pte(ctx, 0, h, rw, type);
690 #if defined(TARGET_PPC64)
691 static always_inline int find_pte64 (mmu_ctx_t *ctx, int h, int rw, int type)
693 return _find_pte(ctx, 1, h, rw, type);
697 static always_inline int find_pte (CPUState *env, mmu_ctx_t *ctx,
698 int h, int rw, int type)
700 #if defined(TARGET_PPC64)
701 if (env->mmu_model & POWERPC_MMU_64)
702 return find_pte64(ctx, h, rw, type);
705 return find_pte32(ctx, h, rw, type);
708 #if defined(TARGET_PPC64)
709 static always_inline int slb_is_valid (uint64_t slb64)
711 return slb64 & 0x0000000008000000ULL ? 1 : 0;
714 static always_inline void slb_invalidate (uint64_t *slb64)
716 *slb64 &= ~0x0000000008000000ULL;
719 static always_inline int slb_lookup (CPUPPCState *env, target_ulong eaddr,
721 target_ulong *page_mask, int *attr)
723 target_phys_addr_t sr_base;
730 sr_base = env->spr[SPR_ASR];
731 #if defined(DEBUG_SLB)
733 fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
734 __func__, eaddr, sr_base);
737 mask = 0x0000000000000000ULL; /* Avoid gcc warning */
738 for (n = 0; n < env->slb_nr; n++) {
739 tmp64 = ldq_phys(sr_base);
740 tmp = ldl_phys(sr_base + 8);
741 #if defined(DEBUG_SLB)
743 fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08"
744 PRIx32 "\n", __func__, n, sr_base, tmp64, tmp);
747 if (slb_is_valid(tmp64)) {
748 /* SLB entry is valid */
749 switch (tmp64 & 0x0000000006000000ULL) {
750 case 0x0000000000000000ULL:
752 mask = 0xFFFFFFFFF0000000ULL;
754 case 0x0000000002000000ULL:
756 mask = 0xFFFF000000000000ULL;
758 case 0x0000000004000000ULL:
759 case 0x0000000006000000ULL:
760 /* Reserved => segment is invalid */
763 if ((eaddr & mask) == (tmp64 & mask)) {
765 *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
778 void ppc_slb_invalidate_all (CPUPPCState *env)
780 target_phys_addr_t sr_base;
782 int n, do_invalidate;
785 sr_base = env->spr[SPR_ASR];
786 /* XXX: Warning: slbia never invalidates the first segment */
787 for (n = 1; n < env->slb_nr; n++) {
788 tmp64 = ldq_phys(sr_base);
789 if (slb_is_valid(tmp64)) {
790 slb_invalidate(&tmp64);
791 stq_phys(sr_base, tmp64);
792 /* XXX: given the fact that segment size is 256 MB or 1TB,
793 * and we still don't have a tlb_flush_mask(env, n, mask)
794 * in Qemu, we just invalidate all TLBs
804 void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
806 target_phys_addr_t sr_base;
807 target_ulong vsid, page_mask;
812 n = slb_lookup(env, T0, &vsid, &page_mask, &attr);
814 sr_base = env->spr[SPR_ASR];
816 tmp64 = ldq_phys(sr_base);
817 if (slb_is_valid(tmp64)) {
818 slb_invalidate(&tmp64);
819 stq_phys(sr_base, tmp64);
820 /* XXX: given the fact that segment size is 256 MB or 1TB,
821 * and we still don't have a tlb_flush_mask(env, n, mask)
822 * in Qemu, we just invalidate all TLBs
829 target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
831 target_phys_addr_t sr_base;
836 sr_base = env->spr[SPR_ASR];
837 sr_base += 12 * slb_nr;
838 tmp64 = ldq_phys(sr_base);
839 tmp = ldl_phys(sr_base + 8);
840 if (tmp64 & 0x0000000008000000ULL) {
841 /* SLB entry is valid */
842 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
843 rt = tmp >> 8; /* 65:88 => 40:63 */
844 rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
845 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
846 rt |= ((tmp >> 4) & 0xF) << 27;
850 #if defined(DEBUG_SLB)
852 fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
853 ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
860 void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
862 target_phys_addr_t sr_base;
866 sr_base = env->spr[SPR_ASR];
867 sr_base += 12 * slb_nr;
868 /* Copy Rs bits 37:63 to SLB 62:88 */
870 tmp64 = (rs >> 24) & 0x7;
871 /* Copy Rs bits 33:36 to SLB 89:92 */
872 tmp |= ((rs >> 27) & 0xF) << 4;
873 /* Set the valid bit */
876 tmp64 |= (uint32_t)slb_nr << 28;
877 #if defined(DEBUG_SLB)
879 fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64
880 " %08" PRIx32 "\n", __func__,
881 slb_nr, rs, sr_base, tmp64, tmp);
884 /* Write SLB entry to memory */
885 stq_phys(sr_base, tmp64);
886 stl_phys(sr_base + 8, tmp);
888 #endif /* defined(TARGET_PPC64) */
890 /* Perform segment based translation */
891 static always_inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
893 target_phys_addr_t hash,
894 target_phys_addr_t mask)
896 return (sdr1 & ((target_phys_addr_t)(-1ULL) << sdr_sh)) | (hash & mask);
899 static always_inline int get_segment (CPUState *env, mmu_ctx_t *ctx,
900 target_ulong eaddr, int rw, int type)
902 target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
903 target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
904 #if defined(TARGET_PPC64)
907 int ds, vsid_sh, sdr_sh, pr;
911 #if defined(TARGET_PPC64)
912 if (env->mmu_model & POWERPC_MMU_64) {
913 #if defined (DEBUG_MMU)
915 fprintf(logfile, "Check SLBs\n");
918 ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
921 ctx->key = ((attr & 0x40) && (pr != 0)) ||
922 ((attr & 0x80) && (pr == 0)) ? 1 : 0;
924 ctx->nx = attr & 0x20 ? 1 : 0;
925 vsid_mask = 0x00003FFFFFFFFF80ULL;
930 #endif /* defined(TARGET_PPC64) */
932 sr = env->sr[eaddr >> 28];
933 page_mask = 0x0FFFFFFF;
934 ctx->key = (((sr & 0x20000000) && (pr != 0)) ||
935 ((sr & 0x40000000) && (pr == 0))) ? 1 : 0;
936 ds = sr & 0x80000000 ? 1 : 0;
937 ctx->nx = sr & 0x10000000 ? 1 : 0;
938 vsid = sr & 0x00FFFFFF;
939 vsid_mask = 0x01FFFFC0;
943 #if defined (DEBUG_MMU)
945 fprintf(logfile, "Check segment v=" ADDRX " %d " ADDRX
946 " nip=" ADDRX " lr=" ADDRX " ir=%d dr=%d pr=%d %d t=%d\n",
947 eaddr, (int)(eaddr >> 28), sr, env->nip,
948 env->lr, (int)msr_ir, (int)msr_dr, pr != 0 ? 1 : 0,
953 #if defined (DEBUG_MMU)
955 fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
956 ctx->key, ds, ctx->nx, vsid);
961 /* Check if instruction fetch is allowed, if needed */
962 if (type != ACCESS_CODE || ctx->nx == 0) {
963 /* Page address translation */
964 /* Primary table address */
966 pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
967 #if defined(TARGET_PPC64)
968 if (env->mmu_model & POWERPC_MMU_64) {
969 htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
970 /* XXX: this is false for 1 TB segments */
971 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
975 htab_mask = sdr & 0x000001FF;
976 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
978 mask = (htab_mask << sdr_sh) | sdr_mask;
979 #if defined (DEBUG_MMU)
981 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX
982 " mask " PADDRX " " ADDRX "\n",
983 sdr, sdr_sh, hash, mask, page_mask);
986 ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
987 /* Secondary table address */
988 hash = (~hash) & vsid_mask;
989 #if defined (DEBUG_MMU)
991 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX
992 " mask " PADDRX "\n",
993 sdr, sdr_sh, hash, mask);
996 ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
997 #if defined(TARGET_PPC64)
998 if (env->mmu_model & POWERPC_MMU_64) {
999 /* Only 5 bits of the page index are used in the AVPN */
1000 ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
1004 ctx->ptem = (vsid << 7) | (pgidx >> 10);
1006 /* Initialize real address with an invalid value */
1007 ctx->raddr = (target_phys_addr_t)-1ULL;
1008 if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
1009 env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
1010 /* Software TLB search */
1011 ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
1013 #if defined (DEBUG_MMU)
1014 if (loglevel != 0) {
1015 fprintf(logfile, "0 sdr1=" PADDRX " vsid=" ADDRX " "
1016 "api=" ADDRX " hash=" PADDRX
1017 " pg_addr=" PADDRX "\n",
1018 sdr, vsid, pgidx, hash, ctx->pg_addr[0]);
1021 /* Primary table lookup */
1022 ret = find_pte(env, ctx, 0, rw, type);
1024 /* Secondary table lookup */
1025 #if defined (DEBUG_MMU)
1026 if (eaddr != 0xEFFFFFFF && loglevel != 0) {
1027 fprintf(logfile, "1 sdr1=" PADDRX " vsid=" ADDRX " "
1028 "api=" ADDRX " hash=" PADDRX
1029 " pg_addr=" PADDRX "\n",
1030 sdr, vsid, pgidx, hash, ctx->pg_addr[1]);
1033 ret2 = find_pte(env, ctx, 1, rw, type);
1038 #if defined (DUMP_PAGE_TABLES)
1039 if (loglevel != 0) {
1040 target_phys_addr_t curaddr;
1041 uint32_t a0, a1, a2, a3;
1042 fprintf(logfile, "Page table: " PADDRX " len " PADDRX "\n",
1044 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
1046 a0 = ldl_phys(curaddr);
1047 a1 = ldl_phys(curaddr + 4);
1048 a2 = ldl_phys(curaddr + 8);
1049 a3 = ldl_phys(curaddr + 12);
1050 if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
1051 fprintf(logfile, PADDRX ": %08x %08x %08x %08x\n",
1052 curaddr, a0, a1, a2, a3);
1058 #if defined (DEBUG_MMU)
1060 fprintf(logfile, "No access allowed\n");
1065 #if defined (DEBUG_MMU)
1067 fprintf(logfile, "direct store...\n");
1069 /* Direct-store segment : absolutely *BUGGY* for now */
1072 /* Integer load/store : only access allowed */
1075 /* No code fetch is allowed in direct-store areas */
1078 /* Floating point load/store */
1081 /* lwarx, ldarx or srwcx. */
1084 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1085 /* Should make the instruction do no-op.
1086 * As it already do no-op, it's quite easy :-)
1091 /* eciwx or ecowx */
1095 fprintf(logfile, "ERROR: instruction should not need "
1096 "address translation\n");
1100 if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
1111 /* Generic TLB check function for embedded PowerPC implementations */
1112 static always_inline int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
1113 target_phys_addr_t *raddrp,
1114 target_ulong address,
1115 uint32_t pid, int ext, int i)
1119 /* Check valid flag */
1120 if (!(tlb->prot & PAGE_VALID)) {
1122 fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
1125 mask = ~(tlb->size - 1);
1126 #if defined (DEBUG_SOFTWARE_TLB)
1127 if (loglevel != 0) {
1128 fprintf(logfile, "%s: TLB %d address " ADDRX " PID %u <=> " ADDRX
1130 __func__, i, address, pid, tlb->EPN, mask, (uint32_t)tlb->PID);
1134 if (tlb->PID != 0 && tlb->PID != pid)
1136 /* Check effective address */
1137 if ((address & mask) != tlb->EPN)
1139 *raddrp = (tlb->RPN & mask) | (address & ~mask);
1140 #if (TARGET_PHYS_ADDR_BITS >= 36)
1142 /* Extend the physical address to 36 bits */
1143 *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
1150 /* Generic TLB search function for PowerPC embedded implementations */
1151 int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
1154 target_phys_addr_t raddr;
1157 /* Default return value is no match */
1159 for (i = 0; i < env->nb_tlb; i++) {
1160 tlb = &env->tlb[i].tlbe;
1161 if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
1170 /* Helpers specific to PowerPC 40x implementations */
1171 static always_inline void ppc4xx_tlb_invalidate_all (CPUState *env)
1176 for (i = 0; i < env->nb_tlb; i++) {
1177 tlb = &env->tlb[i].tlbe;
1178 tlb->prot &= ~PAGE_VALID;
1183 static always_inline void ppc4xx_tlb_invalidate_virt (CPUState *env,
1187 #if !defined(FLUSH_ALL_TLBS)
1189 target_phys_addr_t raddr;
1190 target_ulong page, end;
1193 for (i = 0; i < env->nb_tlb; i++) {
1194 tlb = &env->tlb[i].tlbe;
1195 if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
1196 end = tlb->EPN + tlb->size;
1197 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1198 tlb_flush_page(env, page);
1199 tlb->prot &= ~PAGE_VALID;
1204 ppc4xx_tlb_invalidate_all(env);
1208 int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1209 target_ulong address, int rw, int access_type)
1212 target_phys_addr_t raddr;
1213 int i, ret, zsel, zpr, pr;
1216 raddr = (target_phys_addr_t)-1ULL;
1218 for (i = 0; i < env->nb_tlb; i++) {
1219 tlb = &env->tlb[i].tlbe;
1220 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1221 env->spr[SPR_40x_PID], 0, i) < 0)
1223 zsel = (tlb->attr >> 4) & 0xF;
1224 zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
1225 #if defined (DEBUG_SOFTWARE_TLB)
1226 if (loglevel != 0) {
1227 fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1228 __func__, i, zsel, zpr, rw, tlb->attr);
1231 /* Check execute enable bit */
1238 /* All accesses granted */
1239 ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1251 /* Check from TLB entry */
1252 /* XXX: there is a problem here or in the TLB fill code... */
1253 ctx->prot = tlb->prot;
1254 ctx->prot |= PAGE_EXEC;
1255 ret = check_prot(ctx->prot, rw, access_type);
1260 #if defined (DEBUG_SOFTWARE_TLB)
1261 if (loglevel != 0) {
1262 fprintf(logfile, "%s: access granted " ADDRX " => " PADDRX
1263 " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1270 #if defined (DEBUG_SOFTWARE_TLB)
1271 if (loglevel != 0) {
1272 fprintf(logfile, "%s: access refused " ADDRX " => " PADDRX
1273 " %d %d\n", __func__, address, raddr, ctx->prot,
1281 void store_40x_sler (CPUPPCState *env, uint32_t val)
1283 /* XXX: TO BE FIXED */
1284 if (val != 0x00000000) {
1285 cpu_abort(env, "Little-endian regions are not supported by now\n");
1287 env->spr[SPR_405_SLER] = val;
1290 int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1291 target_ulong address, int rw,
1295 target_phys_addr_t raddr;
1299 raddr = (target_phys_addr_t)-1ULL;
1300 for (i = 0; i < env->nb_tlb; i++) {
1301 tlb = &env->tlb[i].tlbe;
1302 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1303 env->spr[SPR_BOOKE_PID], 1, i) < 0)
1306 prot = tlb->prot & 0xF;
1308 prot = (tlb->prot >> 4) & 0xF;
1309 /* Check the address space */
1310 if (access_type == ACCESS_CODE) {
1311 if (msr_ir != (tlb->attr & 1))
1314 if (prot & PAGE_EXEC) {
1320 if (msr_dr != (tlb->attr & 1))
1323 if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1336 static always_inline int check_physical (CPUState *env, mmu_ctx_t *ctx,
1337 target_ulong eaddr, int rw)
1342 ctx->prot = PAGE_READ | PAGE_EXEC;
1344 switch (env->mmu_model) {
1345 case POWERPC_MMU_32B:
1346 case POWERPC_MMU_601:
1347 case POWERPC_MMU_SOFT_6xx:
1348 case POWERPC_MMU_SOFT_74xx:
1349 case POWERPC_MMU_SOFT_4xx:
1350 case POWERPC_MMU_REAL:
1351 case POWERPC_MMU_BOOKE:
1352 ctx->prot |= PAGE_WRITE;
1354 #if defined(TARGET_PPC64)
1355 case POWERPC_MMU_620:
1356 case POWERPC_MMU_64B:
1357 /* Real address are 60 bits long */
1358 ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1359 ctx->prot |= PAGE_WRITE;
1362 case POWERPC_MMU_SOFT_4xx_Z:
1363 if (unlikely(msr_pe != 0)) {
1364 /* 403 family add some particular protections,
1365 * using PBL/PBU registers for accesses with no translation.
1368 /* Check PLB validity */
1369 (env->pb[0] < env->pb[1] &&
1370 /* and address in plb area */
1371 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1372 (env->pb[2] < env->pb[3] &&
1373 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1374 if (in_plb ^ msr_px) {
1375 /* Access in protected area */
1377 /* Access is not allowed */
1381 /* Read-write access is allowed */
1382 ctx->prot |= PAGE_WRITE;
1386 case POWERPC_MMU_MPC8xx:
1388 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1390 case POWERPC_MMU_BOOKE_FSL:
1392 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1395 cpu_abort(env, "Unknown or invalid MMU model\n");
1402 int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1403 int rw, int access_type)
1408 if (loglevel != 0) {
1409 fprintf(logfile, "%s\n", __func__);
1412 if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1413 (access_type != ACCESS_CODE && msr_dr == 0)) {
1414 /* No address translation */
1415 ret = check_physical(env, ctx, eaddr, rw);
1418 switch (env->mmu_model) {
1419 case POWERPC_MMU_32B:
1420 case POWERPC_MMU_601:
1421 case POWERPC_MMU_SOFT_6xx:
1422 case POWERPC_MMU_SOFT_74xx:
1423 #if defined(TARGET_PPC64)
1424 case POWERPC_MMU_620:
1425 case POWERPC_MMU_64B:
1427 /* Try to find a BAT */
1428 if (env->nb_BATs != 0)
1429 ret = get_bat(env, ctx, eaddr, rw, access_type);
1431 /* We didn't match any BAT entry or don't have BATs */
1432 ret = get_segment(env, ctx, eaddr, rw, access_type);
1435 case POWERPC_MMU_SOFT_4xx:
1436 case POWERPC_MMU_SOFT_4xx_Z:
1437 ret = mmu40x_get_physical_address(env, ctx, eaddr,
1440 case POWERPC_MMU_BOOKE:
1441 ret = mmubooke_get_physical_address(env, ctx, eaddr,
1444 case POWERPC_MMU_MPC8xx:
1446 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1448 case POWERPC_MMU_BOOKE_FSL:
1450 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1452 case POWERPC_MMU_REAL:
1453 cpu_abort(env, "PowerPC in real mode do not do any translation\n");
1456 cpu_abort(env, "Unknown or invalid MMU model\n");
1461 if (loglevel != 0) {
1462 fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1463 __func__, eaddr, ret, ctx->raddr);
1470 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1474 if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT) != 0))
1477 return ctx.raddr & TARGET_PAGE_MASK;
1480 /* Perform address translation */
1481 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1482 int mmu_idx, int is_softmmu)
1491 access_type = ACCESS_CODE;
1494 access_type = env->access_type;
1496 ret = get_physical_address(env, &ctx, address, rw, access_type);
1498 ret = tlb_set_page_exec(env, address & TARGET_PAGE_MASK,
1499 ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1500 mmu_idx, is_softmmu);
1501 } else if (ret < 0) {
1502 #if defined (DEBUG_MMU)
1504 cpu_dump_state(env, logfile, fprintf, 0);
1506 if (access_type == ACCESS_CODE) {
1509 /* No matches in page tables or TLB */
1510 switch (env->mmu_model) {
1511 case POWERPC_MMU_SOFT_6xx:
1512 env->exception_index = POWERPC_EXCP_IFTLB;
1513 env->error_code = 1 << 18;
1514 env->spr[SPR_IMISS] = address;
1515 env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1517 case POWERPC_MMU_SOFT_74xx:
1518 env->exception_index = POWERPC_EXCP_IFTLB;
1520 case POWERPC_MMU_SOFT_4xx:
1521 case POWERPC_MMU_SOFT_4xx_Z:
1522 env->exception_index = POWERPC_EXCP_ITLB;
1523 env->error_code = 0;
1524 env->spr[SPR_40x_DEAR] = address;
1525 env->spr[SPR_40x_ESR] = 0x00000000;
1527 case POWERPC_MMU_32B:
1528 case POWERPC_MMU_601:
1529 #if defined(TARGET_PPC64)
1530 case POWERPC_MMU_620:
1531 case POWERPC_MMU_64B:
1533 env->exception_index = POWERPC_EXCP_ISI;
1534 env->error_code = 0x40000000;
1536 case POWERPC_MMU_BOOKE:
1538 cpu_abort(env, "BookE MMU model is not implemented\n");
1540 case POWERPC_MMU_BOOKE_FSL:
1542 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
1544 case POWERPC_MMU_MPC8xx:
1546 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1548 case POWERPC_MMU_REAL:
1549 cpu_abort(env, "PowerPC in real mode should never raise "
1550 "any MMU exceptions\n");
1553 cpu_abort(env, "Unknown or invalid MMU model\n");
1558 /* Access rights violation */
1559 env->exception_index = POWERPC_EXCP_ISI;
1560 env->error_code = 0x08000000;
1563 /* No execute protection violation */
1564 env->exception_index = POWERPC_EXCP_ISI;
1565 env->error_code = 0x10000000;
1568 /* Direct store exception */
1569 /* No code fetch is allowed in direct-store areas */
1570 env->exception_index = POWERPC_EXCP_ISI;
1571 env->error_code = 0x10000000;
1573 #if defined(TARGET_PPC64)
1575 /* No match in segment table */
1576 if (env->mmu_model == POWERPC_MMU_620) {
1577 env->exception_index = POWERPC_EXCP_ISI;
1578 /* XXX: this might be incorrect */
1579 env->error_code = 0x40000000;
1581 env->exception_index = POWERPC_EXCP_ISEG;
1582 env->error_code = 0;
1590 /* No matches in page tables or TLB */
1591 switch (env->mmu_model) {
1592 case POWERPC_MMU_SOFT_6xx:
1594 env->exception_index = POWERPC_EXCP_DSTLB;
1595 env->error_code = 1 << 16;
1597 env->exception_index = POWERPC_EXCP_DLTLB;
1598 env->error_code = 0;
1600 env->spr[SPR_DMISS] = address;
1601 env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1603 env->error_code |= ctx.key << 19;
1604 env->spr[SPR_HASH1] = ctx.pg_addr[0];
1605 env->spr[SPR_HASH2] = ctx.pg_addr[1];
1607 case POWERPC_MMU_SOFT_74xx:
1609 env->exception_index = POWERPC_EXCP_DSTLB;
1611 env->exception_index = POWERPC_EXCP_DLTLB;
1614 /* Implement LRU algorithm */
1615 env->error_code = ctx.key << 19;
1616 env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1617 ((env->last_way + 1) & (env->nb_ways - 1));
1618 env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
1620 case POWERPC_MMU_SOFT_4xx:
1621 case POWERPC_MMU_SOFT_4xx_Z:
1622 env->exception_index = POWERPC_EXCP_DTLB;
1623 env->error_code = 0;
1624 env->spr[SPR_40x_DEAR] = address;
1626 env->spr[SPR_40x_ESR] = 0x00800000;
1628 env->spr[SPR_40x_ESR] = 0x00000000;
1630 case POWERPC_MMU_32B:
1631 case POWERPC_MMU_601:
1632 #if defined(TARGET_PPC64)
1633 case POWERPC_MMU_620:
1634 case POWERPC_MMU_64B:
1636 env->exception_index = POWERPC_EXCP_DSI;
1637 env->error_code = 0;
1638 env->spr[SPR_DAR] = address;
1640 env->spr[SPR_DSISR] = 0x42000000;
1642 env->spr[SPR_DSISR] = 0x40000000;
1644 case POWERPC_MMU_MPC8xx:
1646 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1648 case POWERPC_MMU_BOOKE:
1650 cpu_abort(env, "BookE MMU model is not implemented\n");
1652 case POWERPC_MMU_BOOKE_FSL:
1654 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
1656 case POWERPC_MMU_REAL:
1657 cpu_abort(env, "PowerPC in real mode should never raise "
1658 "any MMU exceptions\n");
1661 cpu_abort(env, "Unknown or invalid MMU model\n");
1666 /* Access rights violation */
1667 env->exception_index = POWERPC_EXCP_DSI;
1668 env->error_code = 0;
1669 env->spr[SPR_DAR] = address;
1671 env->spr[SPR_DSISR] = 0x0A000000;
1673 env->spr[SPR_DSISR] = 0x08000000;
1676 /* Direct store exception */
1677 switch (access_type) {
1679 /* Floating point load/store */
1680 env->exception_index = POWERPC_EXCP_ALIGN;
1681 env->error_code = POWERPC_EXCP_ALIGN_FP;
1682 env->spr[SPR_DAR] = address;
1685 /* lwarx, ldarx or stwcx. */
1686 env->exception_index = POWERPC_EXCP_DSI;
1687 env->error_code = 0;
1688 env->spr[SPR_DAR] = address;
1690 env->spr[SPR_DSISR] = 0x06000000;
1692 env->spr[SPR_DSISR] = 0x04000000;
1695 /* eciwx or ecowx */
1696 env->exception_index = POWERPC_EXCP_DSI;
1697 env->error_code = 0;
1698 env->spr[SPR_DAR] = address;
1700 env->spr[SPR_DSISR] = 0x06100000;
1702 env->spr[SPR_DSISR] = 0x04100000;
1705 printf("DSI: invalid exception (%d)\n", ret);
1706 env->exception_index = POWERPC_EXCP_PROGRAM;
1708 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1709 env->spr[SPR_DAR] = address;
1713 #if defined(TARGET_PPC64)
1715 /* No match in segment table */
1716 if (env->mmu_model == POWERPC_MMU_620) {
1717 env->exception_index = POWERPC_EXCP_DSI;
1718 env->error_code = 0;
1719 env->spr[SPR_DAR] = address;
1720 /* XXX: this might be incorrect */
1722 env->spr[SPR_DSISR] = 0x42000000;
1724 env->spr[SPR_DSISR] = 0x40000000;
1726 env->exception_index = POWERPC_EXCP_DSEG;
1727 env->error_code = 0;
1728 env->spr[SPR_DAR] = address;
1735 printf("%s: set exception to %d %02x\n", __func__,
1736 env->exception, env->error_code);
1744 /*****************************************************************************/
1745 /* BATs management */
1746 #if !defined(FLUSH_ALL_TLBS)
1747 static always_inline void do_invalidate_BAT (CPUPPCState *env,
1751 target_ulong base, end, page;
1753 base = BATu & ~0x0001FFFF;
1754 end = base + mask + 0x00020000;
1755 #if defined (DEBUG_BATS)
1756 if (loglevel != 0) {
1757 fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1761 for (page = base; page != end; page += TARGET_PAGE_SIZE)
1762 tlb_flush_page(env, page);
1763 #if defined (DEBUG_BATS)
1765 fprintf(logfile, "Flush done\n");
1770 static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1771 int ul, int nr, target_ulong value)
1773 #if defined (DEBUG_BATS)
1774 if (loglevel != 0) {
1775 fprintf(logfile, "Set %cBAT%d%c to " ADDRX " (" ADDRX ")\n",
1776 ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1781 void ppc_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1785 dump_store_bat(env, 'I', 0, nr, value);
1786 if (env->IBAT[0][nr] != value) {
1787 mask = (value << 15) & 0x0FFE0000UL;
1788 #if !defined(FLUSH_ALL_TLBS)
1789 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1791 /* When storing valid upper BAT, mask BEPI and BRPN
1792 * and invalidate all TLBs covered by this BAT
1794 mask = (value << 15) & 0x0FFE0000UL;
1795 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1796 (value & ~0x0001FFFFUL & ~mask);
1797 env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1798 (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1799 #if !defined(FLUSH_ALL_TLBS)
1800 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1807 void ppc_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1809 dump_store_bat(env, 'I', 1, nr, value);
1810 env->IBAT[1][nr] = value;
1813 void ppc_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1817 dump_store_bat(env, 'D', 0, nr, value);
1818 if (env->DBAT[0][nr] != value) {
1819 /* When storing valid upper BAT, mask BEPI and BRPN
1820 * and invalidate all TLBs covered by this BAT
1822 mask = (value << 15) & 0x0FFE0000UL;
1823 #if !defined(FLUSH_ALL_TLBS)
1824 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1826 mask = (value << 15) & 0x0FFE0000UL;
1827 env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1828 (value & ~0x0001FFFFUL & ~mask);
1829 env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1830 (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1831 #if !defined(FLUSH_ALL_TLBS)
1832 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1839 void ppc_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1841 dump_store_bat(env, 'D', 1, nr, value);
1842 env->DBAT[1][nr] = value;
1845 void ppc_store_ibatu_601 (CPUPPCState *env, int nr, target_ulong value)
1850 dump_store_bat(env, 'I', 0, nr, value);
1851 if (env->IBAT[0][nr] != value) {
1853 mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1854 if (env->IBAT[1][nr] & 0x40) {
1855 /* Invalidate BAT only if it is valid */
1856 #if !defined(FLUSH_ALL_TLBS)
1857 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1862 /* When storing valid upper BAT, mask BEPI and BRPN
1863 * and invalidate all TLBs covered by this BAT
1865 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1866 (value & ~0x0001FFFFUL & ~mask);
1867 env->DBAT[0][nr] = env->IBAT[0][nr];
1868 if (env->IBAT[1][nr] & 0x40) {
1869 #if !defined(FLUSH_ALL_TLBS)
1870 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1875 #if defined(FLUSH_ALL_TLBS)
1882 void ppc_store_ibatl_601 (CPUPPCState *env, int nr, target_ulong value)
1887 dump_store_bat(env, 'I', 1, nr, value);
1888 if (env->IBAT[1][nr] != value) {
1890 if (env->IBAT[1][nr] & 0x40) {
1891 #if !defined(FLUSH_ALL_TLBS)
1892 mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1893 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1899 #if !defined(FLUSH_ALL_TLBS)
1900 mask = (value << 17) & 0x0FFE0000UL;
1901 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1906 env->IBAT[1][nr] = value;
1907 env->DBAT[1][nr] = value;
1908 #if defined(FLUSH_ALL_TLBS)
1915 /*****************************************************************************/
1916 /* TLB management */
1917 void ppc_tlb_invalidate_all (CPUPPCState *env)
1919 switch (env->mmu_model) {
1920 case POWERPC_MMU_SOFT_6xx:
1921 case POWERPC_MMU_SOFT_74xx:
1922 ppc6xx_tlb_invalidate_all(env);
1924 case POWERPC_MMU_SOFT_4xx:
1925 case POWERPC_MMU_SOFT_4xx_Z:
1926 ppc4xx_tlb_invalidate_all(env);
1928 case POWERPC_MMU_REAL:
1929 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1931 case POWERPC_MMU_MPC8xx:
1933 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1935 case POWERPC_MMU_BOOKE:
1937 cpu_abort(env, "BookE MMU model is not implemented\n");
1939 case POWERPC_MMU_BOOKE_FSL:
1941 cpu_abort(env, "BookE MMU model is not implemented\n");
1943 case POWERPC_MMU_32B:
1944 case POWERPC_MMU_601:
1945 #if defined(TARGET_PPC64)
1946 case POWERPC_MMU_620:
1947 case POWERPC_MMU_64B:
1948 #endif /* defined(TARGET_PPC64) */
1953 cpu_abort(env, "Unknown MMU model\n");
1958 void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1960 #if !defined(FLUSH_ALL_TLBS)
1961 addr &= TARGET_PAGE_MASK;
1962 switch (env->mmu_model) {
1963 case POWERPC_MMU_SOFT_6xx:
1964 case POWERPC_MMU_SOFT_74xx:
1965 ppc6xx_tlb_invalidate_virt(env, addr, 0);
1966 if (env->id_tlbs == 1)
1967 ppc6xx_tlb_invalidate_virt(env, addr, 1);
1969 case POWERPC_MMU_SOFT_4xx:
1970 case POWERPC_MMU_SOFT_4xx_Z:
1971 ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
1973 case POWERPC_MMU_REAL:
1974 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1976 case POWERPC_MMU_MPC8xx:
1978 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1980 case POWERPC_MMU_BOOKE:
1982 cpu_abort(env, "BookE MMU model is not implemented\n");
1984 case POWERPC_MMU_BOOKE_FSL:
1986 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
1988 case POWERPC_MMU_32B:
1989 case POWERPC_MMU_601:
1990 /* tlbie invalidate TLBs for all segments */
1991 addr &= ~((target_ulong)-1ULL << 28);
1992 /* XXX: this case should be optimized,
1993 * giving a mask to tlb_flush_page
1995 tlb_flush_page(env, addr | (0x0 << 28));
1996 tlb_flush_page(env, addr | (0x1 << 28));
1997 tlb_flush_page(env, addr | (0x2 << 28));
1998 tlb_flush_page(env, addr | (0x3 << 28));
1999 tlb_flush_page(env, addr | (0x4 << 28));
2000 tlb_flush_page(env, addr | (0x5 << 28));
2001 tlb_flush_page(env, addr | (0x6 << 28));
2002 tlb_flush_page(env, addr | (0x7 << 28));
2003 tlb_flush_page(env, addr | (0x8 << 28));
2004 tlb_flush_page(env, addr | (0x9 << 28));
2005 tlb_flush_page(env, addr | (0xA << 28));
2006 tlb_flush_page(env, addr | (0xB << 28));
2007 tlb_flush_page(env, addr | (0xC << 28));
2008 tlb_flush_page(env, addr | (0xD << 28));
2009 tlb_flush_page(env, addr | (0xE << 28));
2010 tlb_flush_page(env, addr | (0xF << 28));
2012 #if defined(TARGET_PPC64)
2013 case POWERPC_MMU_620:
2014 case POWERPC_MMU_64B:
2015 /* tlbie invalidate TLBs for all segments */
2016 /* XXX: given the fact that there are too many segments to invalidate,
2017 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
2018 * we just invalidate all TLBs
2022 #endif /* defined(TARGET_PPC64) */
2025 cpu_abort(env, "Unknown MMU model\n");
2029 ppc_tlb_invalidate_all(env);
2033 /*****************************************************************************/
2034 /* Special registers manipulation */
2035 #if defined(TARGET_PPC64)
2036 void ppc_store_asr (CPUPPCState *env, target_ulong value)
2038 if (env->asr != value) {
2045 void ppc_store_sdr1 (CPUPPCState *env, target_ulong value)
2047 #if defined (DEBUG_MMU)
2048 if (loglevel != 0) {
2049 fprintf(logfile, "%s: " ADDRX "\n", __func__, value);
2052 if (env->sdr1 != value) {
2053 /* XXX: for PowerPC 64, should check that the HTABSIZE value
2061 void ppc_store_sr (CPUPPCState *env, int srnum, target_ulong value)
2063 #if defined (DEBUG_MMU)
2064 if (loglevel != 0) {
2065 fprintf(logfile, "%s: reg=%d " ADDRX " " ADDRX "\n",
2066 __func__, srnum, value, env->sr[srnum]);
2069 if (env->sr[srnum] != value) {
2070 env->sr[srnum] = value;
2071 #if !defined(FLUSH_ALL_TLBS) && 0
2073 target_ulong page, end;
2074 /* Invalidate 256 MB of virtual memory */
2075 page = (16 << 20) * srnum;
2076 end = page + (16 << 20);
2077 for (; page != end; page += TARGET_PAGE_SIZE)
2078 tlb_flush_page(env, page);
2085 #endif /* !defined (CONFIG_USER_ONLY) */
2087 /* GDBstub can read and write MSR... */
2088 void ppc_store_msr (CPUPPCState *env, target_ulong value)
2090 hreg_store_msr(env, value, 0);
2093 /*****************************************************************************/
2094 /* Exception processing */
2095 #if defined (CONFIG_USER_ONLY)
2096 void do_interrupt (CPUState *env)
2098 env->exception_index = POWERPC_EXCP_NONE;
2099 env->error_code = 0;
2102 void ppc_hw_interrupt (CPUState *env)
2104 env->exception_index = POWERPC_EXCP_NONE;
2105 env->error_code = 0;
2107 #else /* defined (CONFIG_USER_ONLY) */
2108 static always_inline void dump_syscall (CPUState *env)
2110 fprintf(logfile, "syscall r0=" REGX " r3=" REGX " r4=" REGX
2111 " r5=" REGX " r6=" REGX " nip=" ADDRX "\n",
2112 ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4),
2113 ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6), env->nip);
2116 /* Note that this function should be greatly optimized
2117 * when called with a constant excp, from ppc_hw_interrupt
2119 static always_inline void powerpc_excp (CPUState *env,
2120 int excp_model, int excp)
2122 target_ulong msr, new_msr, vector;
2123 int srr0, srr1, asrr0, asrr1;
2124 int lpes0, lpes1, lev;
2127 /* XXX: find a suitable condition to enable the hypervisor mode */
2128 lpes0 = (env->spr[SPR_LPCR] >> 1) & 1;
2129 lpes1 = (env->spr[SPR_LPCR] >> 2) & 1;
2131 /* Those values ensure we won't enter the hypervisor mode */
2136 if (loglevel & CPU_LOG_INT) {
2137 fprintf(logfile, "Raise exception at " ADDRX " => %08x (%02x)\n",
2138 env->nip, excp, env->error_code);
2146 msr &= ~((target_ulong)0x783F0000);
2148 case POWERPC_EXCP_NONE:
2149 /* Should never happen */
2151 case POWERPC_EXCP_CRITICAL: /* Critical input */
2152 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2153 switch (excp_model) {
2154 case POWERPC_EXCP_40x:
2155 srr0 = SPR_40x_SRR2;
2156 srr1 = SPR_40x_SRR3;
2158 case POWERPC_EXCP_BOOKE:
2159 srr0 = SPR_BOOKE_CSRR0;
2160 srr1 = SPR_BOOKE_CSRR1;
2162 case POWERPC_EXCP_G2:
2168 case POWERPC_EXCP_MCHECK: /* Machine check exception */
2170 /* Machine check exception is not enabled.
2171 * Enter checkstop state.
2173 if (loglevel != 0) {
2174 fprintf(logfile, "Machine check while not allowed. "
2175 "Entering checkstop state\n");
2177 fprintf(stderr, "Machine check while not allowed. "
2178 "Entering checkstop state\n");
2181 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2183 new_msr &= ~((target_ulong)1 << MSR_RI);
2184 new_msr &= ~((target_ulong)1 << MSR_ME);
2186 /* XXX: find a suitable condition to enable the hypervisor mode */
2187 new_msr |= (target_ulong)MSR_HVB;
2189 /* XXX: should also have something loaded in DAR / DSISR */
2190 switch (excp_model) {
2191 case POWERPC_EXCP_40x:
2192 srr0 = SPR_40x_SRR2;
2193 srr1 = SPR_40x_SRR3;
2195 case POWERPC_EXCP_BOOKE:
2196 srr0 = SPR_BOOKE_MCSRR0;
2197 srr1 = SPR_BOOKE_MCSRR1;
2198 asrr0 = SPR_BOOKE_CSRR0;
2199 asrr1 = SPR_BOOKE_CSRR1;
2205 case POWERPC_EXCP_DSI: /* Data storage exception */
2206 #if defined (DEBUG_EXCEPTIONS)
2207 if (loglevel != 0) {
2208 fprintf(logfile, "DSI exception: DSISR=" ADDRX" DAR=" ADDRX "\n",
2209 env->spr[SPR_DSISR], env->spr[SPR_DAR]);
2212 new_msr &= ~((target_ulong)1 << MSR_RI);
2214 new_msr |= (target_ulong)MSR_HVB;
2216 case POWERPC_EXCP_ISI: /* Instruction storage exception */
2217 #if defined (DEBUG_EXCEPTIONS)
2218 if (loglevel != 0) {
2219 fprintf(logfile, "ISI exception: msr=" ADDRX ", nip=" ADDRX "\n",
2223 new_msr &= ~((target_ulong)1 << MSR_RI);
2225 new_msr |= (target_ulong)MSR_HVB;
2226 msr |= env->error_code;
2228 case POWERPC_EXCP_EXTERNAL: /* External input */
2229 new_msr &= ~((target_ulong)1 << MSR_RI);
2231 new_msr |= (target_ulong)MSR_HVB;
2233 case POWERPC_EXCP_ALIGN: /* Alignment exception */
2234 new_msr &= ~((target_ulong)1 << MSR_RI);
2236 new_msr |= (target_ulong)MSR_HVB;
2237 /* XXX: this is false */
2238 /* Get rS/rD and rA from faulting opcode */
2239 env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
2241 case POWERPC_EXCP_PROGRAM: /* Program exception */
2242 switch (env->error_code & ~0xF) {
2243 case POWERPC_EXCP_FP:
2244 if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
2245 #if defined (DEBUG_EXCEPTIONS)
2246 if (loglevel != 0) {
2247 fprintf(logfile, "Ignore floating point exception\n");
2250 env->exception_index = POWERPC_EXCP_NONE;
2251 env->error_code = 0;
2254 new_msr &= ~((target_ulong)1 << MSR_RI);
2256 new_msr |= (target_ulong)MSR_HVB;
2258 if (msr_fe0 == msr_fe1)
2262 case POWERPC_EXCP_INVAL:
2263 #if defined (DEBUG_EXCEPTIONS)
2264 if (loglevel != 0) {
2265 fprintf(logfile, "Invalid instruction at " ADDRX "\n",
2269 new_msr &= ~((target_ulong)1 << MSR_RI);
2271 new_msr |= (target_ulong)MSR_HVB;
2274 case POWERPC_EXCP_PRIV:
2275 new_msr &= ~((target_ulong)1 << MSR_RI);
2277 new_msr |= (target_ulong)MSR_HVB;
2280 case POWERPC_EXCP_TRAP:
2281 new_msr &= ~((target_ulong)1 << MSR_RI);
2283 new_msr |= (target_ulong)MSR_HVB;
2287 /* Should never occur */
2288 cpu_abort(env, "Invalid program exception %d. Aborting\n",
2293 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
2294 new_msr &= ~((target_ulong)1 << MSR_RI);
2296 new_msr |= (target_ulong)MSR_HVB;
2298 case POWERPC_EXCP_SYSCALL: /* System call exception */
2299 /* NOTE: this is a temporary hack to support graphics OSI
2300 calls from the MOL driver */
2301 /* XXX: To be removed */
2302 if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2304 if (env->osi_call(env) != 0) {
2305 env->exception_index = POWERPC_EXCP_NONE;
2306 env->error_code = 0;
2310 if (loglevel & CPU_LOG_INT) {
2313 new_msr &= ~((target_ulong)1 << MSR_RI);
2314 lev = env->error_code;
2315 if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
2316 new_msr |= (target_ulong)MSR_HVB;
2318 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
2319 new_msr &= ~((target_ulong)1 << MSR_RI);
2321 case POWERPC_EXCP_DECR: /* Decrementer exception */
2322 new_msr &= ~((target_ulong)1 << MSR_RI);
2324 new_msr |= (target_ulong)MSR_HVB;
2326 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
2328 #if defined (DEBUG_EXCEPTIONS)
2330 fprintf(logfile, "FIT exception\n");
2332 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2334 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
2335 #if defined (DEBUG_EXCEPTIONS)
2337 fprintf(logfile, "WDT exception\n");
2339 switch (excp_model) {
2340 case POWERPC_EXCP_BOOKE:
2341 srr0 = SPR_BOOKE_CSRR0;
2342 srr1 = SPR_BOOKE_CSRR1;
2347 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2349 case POWERPC_EXCP_DTLB: /* Data TLB error */
2350 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2352 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
2353 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2355 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
2356 switch (excp_model) {
2357 case POWERPC_EXCP_BOOKE:
2358 srr0 = SPR_BOOKE_DSRR0;
2359 srr1 = SPR_BOOKE_DSRR1;
2360 asrr0 = SPR_BOOKE_CSRR0;
2361 asrr1 = SPR_BOOKE_CSRR1;
2367 cpu_abort(env, "Debug exception is not implemented yet !\n");
2369 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable */
2370 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2372 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */
2374 cpu_abort(env, "Embedded floating point data exception "
2375 "is not implemented yet !\n");
2377 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */
2379 cpu_abort(env, "Embedded floating point round exception "
2380 "is not implemented yet !\n");
2382 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor interrupt */
2383 new_msr &= ~((target_ulong)1 << MSR_RI);
2386 "Performance counter exception is not implemented yet !\n");
2388 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
2391 "Embedded doorbell interrupt is not implemented yet !\n");
2393 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
2394 switch (excp_model) {
2395 case POWERPC_EXCP_BOOKE:
2396 srr0 = SPR_BOOKE_CSRR0;
2397 srr1 = SPR_BOOKE_CSRR1;
2403 cpu_abort(env, "Embedded doorbell critical interrupt "
2404 "is not implemented yet !\n");
2406 case POWERPC_EXCP_RESET: /* System reset exception */
2407 new_msr &= ~((target_ulong)1 << MSR_RI);
2409 /* XXX: find a suitable condition to enable the hypervisor mode */
2410 new_msr |= (target_ulong)MSR_HVB;
2413 case POWERPC_EXCP_DSEG: /* Data segment exception */
2414 new_msr &= ~((target_ulong)1 << MSR_RI);
2416 new_msr |= (target_ulong)MSR_HVB;
2418 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
2419 new_msr &= ~((target_ulong)1 << MSR_RI);
2421 new_msr |= (target_ulong)MSR_HVB;
2423 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
2426 new_msr |= (target_ulong)MSR_HVB;
2428 case POWERPC_EXCP_TRACE: /* Trace exception */
2429 new_msr &= ~((target_ulong)1 << MSR_RI);
2431 new_msr |= (target_ulong)MSR_HVB;
2433 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
2436 new_msr |= (target_ulong)MSR_HVB;
2438 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */
2441 new_msr |= (target_ulong)MSR_HVB;
2443 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
2446 new_msr |= (target_ulong)MSR_HVB;
2448 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment exception */
2451 new_msr |= (target_ulong)MSR_HVB;
2453 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
2454 new_msr &= ~((target_ulong)1 << MSR_RI);
2456 new_msr |= (target_ulong)MSR_HVB;
2458 case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */
2459 #if defined (DEBUG_EXCEPTIONS)
2461 fprintf(logfile, "PIT exception\n");
2463 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2465 case POWERPC_EXCP_IO: /* IO error exception */
2467 cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2469 case POWERPC_EXCP_RUNM: /* Run mode exception */
2471 cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2473 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
2475 cpu_abort(env, "602 emulation trap exception "
2476 "is not implemented yet !\n");
2478 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
2479 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2480 if (lpes1 == 0) /* XXX: check this */
2481 new_msr |= (target_ulong)MSR_HVB;
2482 switch (excp_model) {
2483 case POWERPC_EXCP_602:
2484 case POWERPC_EXCP_603:
2485 case POWERPC_EXCP_603E:
2486 case POWERPC_EXCP_G2:
2488 case POWERPC_EXCP_7x5:
2490 case POWERPC_EXCP_74xx:
2493 cpu_abort(env, "Invalid instruction TLB miss exception\n");
2497 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
2498 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2499 if (lpes1 == 0) /* XXX: check this */
2500 new_msr |= (target_ulong)MSR_HVB;
2501 switch (excp_model) {
2502 case POWERPC_EXCP_602:
2503 case POWERPC_EXCP_603:
2504 case POWERPC_EXCP_603E:
2505 case POWERPC_EXCP_G2:
2507 case POWERPC_EXCP_7x5:
2509 case POWERPC_EXCP_74xx:
2512 cpu_abort(env, "Invalid data load TLB miss exception\n");
2516 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
2517 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2518 if (lpes1 == 0) /* XXX: check this */
2519 new_msr |= (target_ulong)MSR_HVB;
2520 switch (excp_model) {
2521 case POWERPC_EXCP_602:
2522 case POWERPC_EXCP_603:
2523 case POWERPC_EXCP_603E:
2524 case POWERPC_EXCP_G2:
2526 /* Swap temporary saved registers with GPRs */
2527 if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
2528 new_msr |= (target_ulong)1 << MSR_TGPR;
2529 hreg_swap_gpr_tgpr(env);
2532 case POWERPC_EXCP_7x5:
2534 #if defined (DEBUG_SOFTWARE_TLB)
2535 if (loglevel != 0) {
2536 const unsigned char *es;
2537 target_ulong *miss, *cmp;
2539 if (excp == POWERPC_EXCP_IFTLB) {
2542 miss = &env->spr[SPR_IMISS];
2543 cmp = &env->spr[SPR_ICMP];
2545 if (excp == POWERPC_EXCP_DLTLB)
2550 miss = &env->spr[SPR_DMISS];
2551 cmp = &env->spr[SPR_DCMP];
2553 fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2554 " H1 " ADDRX " H2 " ADDRX " %08x\n",
2555 es, en, *miss, en, *cmp,
2556 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2560 msr |= env->crf[0] << 28;
2561 msr |= env->error_code; /* key, D/I, S/L bits */
2562 /* Set way using a LRU mechanism */
2563 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2565 case POWERPC_EXCP_74xx:
2567 #if defined (DEBUG_SOFTWARE_TLB)
2568 if (loglevel != 0) {
2569 const unsigned char *es;
2570 target_ulong *miss, *cmp;
2572 if (excp == POWERPC_EXCP_IFTLB) {
2575 miss = &env->spr[SPR_TLBMISS];
2576 cmp = &env->spr[SPR_PTEHI];
2578 if (excp == POWERPC_EXCP_DLTLB)
2583 miss = &env->spr[SPR_TLBMISS];
2584 cmp = &env->spr[SPR_PTEHI];
2586 fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2588 es, en, *miss, en, *cmp, env->error_code);
2591 msr |= env->error_code; /* key bit */
2594 cpu_abort(env, "Invalid data store TLB miss exception\n");
2598 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
2600 cpu_abort(env, "Floating point assist exception "
2601 "is not implemented yet !\n");
2603 case POWERPC_EXCP_DABR: /* Data address breakpoint */
2605 cpu_abort(env, "DABR exception is not implemented yet !\n");
2607 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
2609 cpu_abort(env, "IABR exception is not implemented yet !\n");
2611 case POWERPC_EXCP_SMI: /* System management interrupt */
2613 cpu_abort(env, "SMI exception is not implemented yet !\n");
2615 case POWERPC_EXCP_THERM: /* Thermal interrupt */
2617 cpu_abort(env, "Thermal management exception "
2618 "is not implemented yet !\n");
2620 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
2621 new_msr &= ~((target_ulong)1 << MSR_RI);
2623 new_msr |= (target_ulong)MSR_HVB;
2626 "Performance counter exception is not implemented yet !\n");
2628 case POWERPC_EXCP_VPUA: /* Vector assist exception */
2630 cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2632 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
2635 "970 soft-patch exception is not implemented yet !\n");
2637 case POWERPC_EXCP_MAINT: /* Maintenance exception */
2640 "970 maintenance exception is not implemented yet !\n");
2642 case POWERPC_EXCP_MEXTBR: /* Maskable external breakpoint */
2644 cpu_abort(env, "Maskable external exception "
2645 "is not implemented yet !\n");
2647 case POWERPC_EXCP_NMEXTBR: /* Non maskable external breakpoint */
2649 cpu_abort(env, "Non maskable external exception "
2650 "is not implemented yet !\n");
2654 cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2657 /* save current instruction location */
2658 env->spr[srr0] = env->nip - 4;
2661 /* save next instruction location */
2662 env->spr[srr0] = env->nip;
2666 env->spr[srr1] = msr;
2667 /* If any alternate SRR register are defined, duplicate saved values */
2669 env->spr[asrr0] = env->spr[srr0];
2671 env->spr[asrr1] = env->spr[srr1];
2672 /* If we disactivated any translation, flush TLBs */
2673 if (new_msr & ((1 << MSR_IR) | (1 << MSR_DR)))
2675 /* reload MSR with correct bits */
2676 new_msr &= ~((target_ulong)1 << MSR_EE);
2677 new_msr &= ~((target_ulong)1 << MSR_PR);
2678 new_msr &= ~((target_ulong)1 << MSR_FP);
2679 new_msr &= ~((target_ulong)1 << MSR_FE0);
2680 new_msr &= ~((target_ulong)1 << MSR_SE);
2681 new_msr &= ~((target_ulong)1 << MSR_BE);
2682 new_msr &= ~((target_ulong)1 << MSR_FE1);
2683 new_msr &= ~((target_ulong)1 << MSR_IR);
2684 new_msr &= ~((target_ulong)1 << MSR_DR);
2685 #if 0 /* Fix this: not on all targets */
2686 new_msr &= ~((target_ulong)1 << MSR_PMM);
2688 new_msr &= ~((target_ulong)1 << MSR_LE);
2690 new_msr |= (target_ulong)1 << MSR_LE;
2692 new_msr &= ~((target_ulong)1 << MSR_LE);
2693 /* Jump to handler */
2694 vector = env->excp_vectors[excp];
2695 if (vector == (target_ulong)-1ULL) {
2696 cpu_abort(env, "Raised an exception without defined vector %d\n",
2699 vector |= env->excp_prefix;
2700 #if defined(TARGET_PPC64)
2701 if (excp_model == POWERPC_EXCP_BOOKE) {
2703 new_msr &= ~((target_ulong)1 << MSR_CM);
2704 vector = (uint32_t)vector;
2706 new_msr |= (target_ulong)1 << MSR_CM;
2710 new_msr &= ~((target_ulong)1 << MSR_SF);
2711 vector = (uint32_t)vector;
2713 new_msr |= (target_ulong)1 << MSR_SF;
2717 /* XXX: we don't use hreg_store_msr here as already have treated
2718 * any special case that could occur. Just store MSR and update hflags
2720 env->msr = new_msr & env->msr_mask;
2721 hreg_compute_hflags(env);
2723 /* Reset exception state */
2724 env->exception_index = POWERPC_EXCP_NONE;
2725 env->error_code = 0;
2728 void do_interrupt (CPUState *env)
2730 powerpc_excp(env, env->excp_model, env->exception_index);
2733 void ppc_hw_interrupt (CPUPPCState *env)
2738 if (loglevel & CPU_LOG_INT) {
2739 fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2740 __func__, env, env->pending_interrupts,
2741 env->interrupt_request, (int)msr_me, (int)msr_ee);
2744 /* External reset */
2745 if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2746 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2747 powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2750 /* Machine check exception */
2751 if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2752 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2753 powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2757 /* External debug exception */
2758 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2759 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2760 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2765 /* XXX: find a suitable condition to enable the hypervisor mode */
2766 hdice = env->spr[SPR_LPCR] & 1;
2770 if ((msr_ee != 0 || msr_hv == 0 || msr_pr != 0) && hdice != 0) {
2771 /* Hypervisor decrementer exception */
2772 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2773 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2774 powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2779 /* External critical interrupt */
2780 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2781 /* Taking a critical external interrupt does not clear the external
2782 * critical interrupt status
2785 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2787 powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2792 /* Watchdog timer on embedded PowerPC */
2793 if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2794 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2795 powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2798 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2799 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2800 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2803 /* Fixed interval timer on embedded PowerPC */
2804 if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2805 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2806 powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2809 /* Programmable interval timer on embedded PowerPC */
2810 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2811 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2812 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2815 /* Decrementer exception */
2816 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2817 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2818 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2821 /* External interrupt */
2822 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2823 /* Taking an external interrupt does not clear the external
2827 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2829 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2832 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2833 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2834 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2837 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2838 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2839 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2842 /* Thermal interrupt */
2843 if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2844 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2845 powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2850 #endif /* !CONFIG_USER_ONLY */
2852 void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2862 fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2866 void cpu_ppc_reset (void *opaque)
2872 msr = (target_ulong)0;
2874 /* XXX: find a suitable condition to enable the hypervisor mode */
2875 msr |= (target_ulong)MSR_HVB;
2877 msr |= (target_ulong)0 << MSR_AP; /* TO BE CHECKED */
2878 msr |= (target_ulong)0 << MSR_SA; /* TO BE CHECKED */
2879 msr |= (target_ulong)1 << MSR_EP;
2880 #if defined (DO_SINGLE_STEP) && 0
2881 /* Single step trace mode */
2882 msr |= (target_ulong)1 << MSR_SE;
2883 msr |= (target_ulong)1 << MSR_BE;
2885 #if defined(CONFIG_USER_ONLY)
2886 msr |= (target_ulong)1 << MSR_FP; /* Allow floating point usage */
2887 msr |= (target_ulong)1 << MSR_VR; /* Allow altivec usage */
2888 msr |= (target_ulong)1 << MSR_SPE; /* Allow SPE usage */
2889 msr |= (target_ulong)1 << MSR_PR;
2890 env->msr = msr & env->msr_mask;
2892 env->nip = env->hreset_vector | env->excp_prefix;
2893 if (env->mmu_model != POWERPC_MMU_REAL)
2894 ppc_tlb_invalidate_all(env);
2896 hreg_compute_hflags(env);
2897 env->reserve = (target_ulong)-1ULL;
2898 /* Be sure no exception or interrupt is pending */
2899 env->pending_interrupts = 0;
2900 env->exception_index = POWERPC_EXCP_NONE;
2901 env->error_code = 0;
2902 /* Flush all TLBs */
2906 CPUPPCState *cpu_ppc_init (const char *cpu_model)
2909 const ppc_def_t *def;
2911 def = cpu_ppc_find_by_name(cpu_model);
2915 env = qemu_mallocz(sizeof(CPUPPCState));
2919 ppc_translate_init();
2920 env->cpu_model_str = cpu_model;
2921 cpu_ppc_register_internal(env, def);
2926 void cpu_ppc_close (CPUPPCState *env)
2928 /* Should also remove all opcode tables... */