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"
34 //#define DEBUG_SOFTWARE_TLB
35 //#define DUMP_PAGE_TABLES
36 //#define DEBUG_EXCEPTIONS
37 //#define FLUSH_ALL_TLBS
39 /*****************************************************************************/
40 /* PowerPC MMU emulation */
42 #if defined(CONFIG_USER_ONLY)
43 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
44 int mmu_idx, int is_softmmu)
46 int exception, error_code;
49 exception = POWERPC_EXCP_ISI;
50 error_code = 0x40000000;
52 exception = POWERPC_EXCP_DSI;
53 error_code = 0x40000000;
55 error_code |= 0x02000000;
56 env->spr[SPR_DAR] = address;
57 env->spr[SPR_DSISR] = error_code;
59 env->exception_index = exception;
60 env->error_code = error_code;
65 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
71 /* Common routines used by software and hardware TLBs emulation */
72 static always_inline int pte_is_valid (target_ulong pte0)
74 return pte0 & 0x80000000 ? 1 : 0;
77 static always_inline void pte_invalidate (target_ulong *pte0)
82 #if defined(TARGET_PPC64)
83 static always_inline int pte64_is_valid (target_ulong pte0)
85 return pte0 & 0x0000000000000001ULL ? 1 : 0;
88 static always_inline void pte64_invalidate (target_ulong *pte0)
90 *pte0 &= ~0x0000000000000001ULL;
94 #define PTE_PTEM_MASK 0x7FFFFFBF
95 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
96 #if defined(TARGET_PPC64)
97 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
98 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
101 static always_inline int pp_check (int key, int pp, int nx)
105 /* Compute access rights */
106 /* When pp is 3/7, the result is undefined. Set it to noaccess */
113 access |= PAGE_WRITE;
131 access = PAGE_READ | PAGE_WRITE;
141 static always_inline int check_prot (int prot, int rw, int access_type)
145 if (access_type == ACCESS_CODE) {
146 if (prot & PAGE_EXEC)
151 if (prot & PAGE_WRITE)
156 if (prot & PAGE_READ)
165 static always_inline int _pte_check (mmu_ctx_t *ctx, int is_64b,
166 target_ulong pte0, target_ulong pte1,
167 int h, int rw, int type)
169 target_ulong ptem, mmask;
170 int access, ret, pteh, ptev, pp;
174 /* Check validity and table match */
175 #if defined(TARGET_PPC64)
177 ptev = pte64_is_valid(pte0);
178 pteh = (pte0 >> 1) & 1;
182 ptev = pte_is_valid(pte0);
183 pteh = (pte0 >> 6) & 1;
185 if (ptev && h == pteh) {
186 /* Check vsid & api */
187 #if defined(TARGET_PPC64)
189 ptem = pte0 & PTE64_PTEM_MASK;
190 mmask = PTE64_CHECK_MASK;
191 pp = (pte1 & 0x00000003) | ((pte1 >> 61) & 0x00000004);
192 ctx->nx |= (pte1 >> 2) & 1; /* No execute bit */
193 ctx->nx |= (pte1 >> 3) & 1; /* Guarded bit */
197 ptem = pte0 & PTE_PTEM_MASK;
198 mmask = PTE_CHECK_MASK;
199 pp = pte1 & 0x00000003;
201 if (ptem == ctx->ptem) {
202 if (ctx->raddr != (target_ulong)-1) {
203 /* all matches should have equal RPN, WIMG & PP */
204 if ((ctx->raddr & mmask) != (pte1 & mmask)) {
206 fprintf(logfile, "Bad RPN/WIMG/PP\n");
210 /* Compute access rights */
211 access = pp_check(ctx->key, pp, ctx->nx);
212 /* Keep the matching PTE informations */
215 ret = check_prot(ctx->prot, rw, type);
218 #if defined (DEBUG_MMU)
220 fprintf(logfile, "PTE access granted !\n");
223 /* Access right violation */
224 #if defined (DEBUG_MMU)
226 fprintf(logfile, "PTE access rejected\n");
235 static always_inline int pte32_check (mmu_ctx_t *ctx,
236 target_ulong pte0, target_ulong pte1,
237 int h, int rw, int type)
239 return _pte_check(ctx, 0, pte0, pte1, h, rw, type);
242 #if defined(TARGET_PPC64)
243 static always_inline int pte64_check (mmu_ctx_t *ctx,
244 target_ulong pte0, target_ulong pte1,
245 int h, int rw, int type)
247 return _pte_check(ctx, 1, pte0, pte1, h, rw, type);
251 static always_inline int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
256 /* Update page flags */
257 if (!(*pte1p & 0x00000100)) {
258 /* Update accessed flag */
259 *pte1p |= 0x00000100;
262 if (!(*pte1p & 0x00000080)) {
263 if (rw == 1 && ret == 0) {
264 /* Update changed flag */
265 *pte1p |= 0x00000080;
268 /* Force page fault for first write access */
269 ctx->prot &= ~PAGE_WRITE;
276 /* Software driven TLB helpers */
277 static always_inline int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
278 int way, int is_code)
282 /* Select TLB num in a way from address */
283 nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
285 nr += env->tlb_per_way * way;
286 /* 6xx have separate TLBs for instructions and data */
287 if (is_code && env->id_tlbs == 1)
293 static always_inline void ppc6xx_tlb_invalidate_all (CPUState *env)
298 #if defined (DEBUG_SOFTWARE_TLB) && 0
300 fprintf(logfile, "Invalidate all TLBs\n");
303 /* Invalidate all defined software TLB */
305 if (env->id_tlbs == 1)
307 for (nr = 0; nr < max; nr++) {
308 tlb = &env->tlb[nr].tlb6;
309 pte_invalidate(&tlb->pte0);
314 static always_inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
319 #if !defined(FLUSH_ALL_TLBS)
323 /* Invalidate ITLB + DTLB, all ways */
324 for (way = 0; way < env->nb_ways; way++) {
325 nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
326 tlb = &env->tlb[nr].tlb6;
327 if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
328 #if defined (DEBUG_SOFTWARE_TLB)
330 fprintf(logfile, "TLB invalidate %d/%d " ADDRX "\n",
331 nr, env->nb_tlb, eaddr);
334 pte_invalidate(&tlb->pte0);
335 tlb_flush_page(env, tlb->EPN);
339 /* XXX: PowerPC specification say this is valid as well */
340 ppc6xx_tlb_invalidate_all(env);
344 static always_inline void ppc6xx_tlb_invalidate_virt (CPUState *env,
348 __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
351 void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
352 target_ulong pte0, target_ulong pte1)
357 nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
358 tlb = &env->tlb[nr].tlb6;
359 #if defined (DEBUG_SOFTWARE_TLB)
361 fprintf(logfile, "Set TLB %d/%d EPN " ADDRX " PTE0 " ADDRX
362 " PTE1 " ADDRX "\n", nr, env->nb_tlb, EPN, pte0, pte1);
365 /* Invalidate any pending reference in Qemu for this virtual address */
366 __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
370 /* Store last way for LRU mechanism */
374 static always_inline int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
375 target_ulong eaddr, int rw,
383 ret = -1; /* No TLB found */
384 for (way = 0; way < env->nb_ways; way++) {
385 nr = ppc6xx_tlb_getnum(env, eaddr, way,
386 access_type == ACCESS_CODE ? 1 : 0);
387 tlb = &env->tlb[nr].tlb6;
388 /* This test "emulates" the PTE index match for hardware TLBs */
389 if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
390 #if defined (DEBUG_SOFTWARE_TLB)
392 fprintf(logfile, "TLB %d/%d %s [" ADDRX " " ADDRX
395 pte_is_valid(tlb->pte0) ? "valid" : "inval",
396 tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
401 #if defined (DEBUG_SOFTWARE_TLB)
403 fprintf(logfile, "TLB %d/%d %s " ADDRX " <> " ADDRX " " ADDRX
406 pte_is_valid(tlb->pte0) ? "valid" : "inval",
407 tlb->EPN, eaddr, tlb->pte1,
408 rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
411 switch (pte32_check(ctx, tlb->pte0, tlb->pte1, 0, rw, access_type)) {
413 /* TLB inconsistency */
416 /* Access violation */
426 /* XXX: we should go on looping to check all TLBs consistency
427 * but we can speed-up the whole thing as the
428 * result would be undefined if TLBs are not consistent.
437 #if defined (DEBUG_SOFTWARE_TLB)
439 fprintf(logfile, "found TLB at addr 0x%08lx prot=0x%01x ret=%d\n",
440 ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
443 /* Update page flags */
444 pte_update_flags(ctx, &env->tlb[best].tlb6.pte1, ret, rw);
450 /* Perform BAT hit & translation */
451 static always_inline int get_bat (CPUState *env, mmu_ctx_t *ctx,
452 target_ulong virtual, int rw, int type)
454 target_ulong *BATlt, *BATut, *BATu, *BATl;
455 target_ulong base, BEPIl, BEPIu, bl;
459 #if defined (DEBUG_BATS)
461 fprintf(logfile, "%s: %cBAT v 0x" ADDRX "\n", __func__,
462 type == ACCESS_CODE ? 'I' : 'D', virtual);
468 BATlt = env->IBAT[1];
469 BATut = env->IBAT[0];
472 BATlt = env->DBAT[1];
473 BATut = env->DBAT[0];
476 #if defined (DEBUG_BATS)
478 fprintf(logfile, "%s...: %cBAT v 0x" ADDRX "\n", __func__,
479 type == ACCESS_CODE ? 'I' : 'D', virtual);
482 base = virtual & 0xFFFC0000;
483 for (i = 0; i < 4; i++) {
486 BEPIu = *BATu & 0xF0000000;
487 BEPIl = *BATu & 0x0FFE0000;
488 bl = (*BATu & 0x00001FFC) << 15;
489 #if defined (DEBUG_BATS)
491 fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
492 " BATl 0x" ADDRX "\n",
493 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
497 if ((virtual & 0xF0000000) == BEPIu &&
498 ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
500 if (((pr == 0) && (*BATu & 0x00000002)) ||
501 ((pr != 0) && (*BATu & 0x00000001))) {
502 /* Get physical address */
503 ctx->raddr = (*BATl & 0xF0000000) |
504 ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
505 (virtual & 0x0001F000);
506 /* Compute access rights */
507 pp = *BATl & 0x00000003;
510 ctx->prot = PAGE_READ | PAGE_EXEC;
512 ctx->prot |= PAGE_WRITE;
514 ret = check_prot(ctx->prot, rw, type);
515 #if defined (DEBUG_BATS)
516 if (ret == 0 && loglevel != 0) {
517 fprintf(logfile, "BAT %d match: r 0x" PADDRX
519 i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
520 ctx->prot & PAGE_WRITE ? 'W' : '-');
528 #if defined (DEBUG_BATS)
530 fprintf(logfile, "no BAT match for 0x" ADDRX ":\n", virtual);
531 for (i = 0; i < 4; i++) {
534 BEPIu = *BATu & 0xF0000000;
535 BEPIl = *BATu & 0x0FFE0000;
536 bl = (*BATu & 0x00001FFC) << 15;
537 fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
538 " BATl 0x" ADDRX " \n\t"
539 "0x" ADDRX " 0x" ADDRX " 0x" ADDRX "\n",
540 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
541 *BATu, *BATl, BEPIu, BEPIl, bl);
551 /* PTE table lookup */
552 static always_inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h,
555 target_ulong base, pte0, pte1;
559 ret = -1; /* No entry found */
560 base = ctx->pg_addr[h];
561 for (i = 0; i < 8; i++) {
562 #if defined(TARGET_PPC64)
564 pte0 = ldq_phys(base + (i * 16));
565 pte1 = ldq_phys(base + (i * 16) + 8);
566 r = pte64_check(ctx, pte0, pte1, h, rw, type);
567 #if defined (DEBUG_MMU)
569 fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
570 " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
571 base + (i * 16), pte0, pte1,
572 (int)(pte0 & 1), h, (int)((pte0 >> 1) & 1),
579 pte0 = ldl_phys(base + (i * 8));
580 pte1 = ldl_phys(base + (i * 8) + 4);
581 r = pte32_check(ctx, pte0, pte1, h, rw, type);
582 #if defined (DEBUG_MMU)
584 fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
585 " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
586 base + (i * 8), pte0, pte1,
587 (int)(pte0 >> 31), h, (int)((pte0 >> 6) & 1),
594 /* PTE inconsistency */
597 /* Access violation */
607 /* XXX: we should go on looping to check all PTEs consistency
608 * but if we can speed-up the whole thing as the
609 * result would be undefined if PTEs are not consistent.
618 #if defined (DEBUG_MMU)
620 fprintf(logfile, "found PTE at addr 0x" PADDRX " prot=0x%01x "
622 ctx->raddr, ctx->prot, ret);
625 /* Update page flags */
627 if (pte_update_flags(ctx, &pte1, ret, rw) == 1) {
628 #if defined(TARGET_PPC64)
630 stq_phys_notdirty(base + (good * 16) + 8, pte1);
634 stl_phys_notdirty(base + (good * 8) + 4, pte1);
642 static always_inline int find_pte32 (mmu_ctx_t *ctx, int h, int rw, int type)
644 return _find_pte(ctx, 0, h, rw, type);
647 #if defined(TARGET_PPC64)
648 static always_inline int find_pte64 (mmu_ctx_t *ctx, int h, int rw, int type)
650 return _find_pte(ctx, 1, h, rw, type);
654 static always_inline int find_pte (CPUState *env, mmu_ctx_t *ctx,
655 int h, int rw, int type)
657 #if defined(TARGET_PPC64)
658 if (env->mmu_model == POWERPC_MMU_64B)
659 return find_pte64(ctx, h, rw, type);
662 return find_pte32(ctx, h, rw, type);
665 #if defined(TARGET_PPC64)
666 static always_inline int slb_is_valid (uint64_t slb64)
668 return slb64 & 0x0000000008000000ULL ? 1 : 0;
671 static always_inline void slb_invalidate (uint64_t *slb64)
673 *slb64 &= ~0x0000000008000000ULL;
676 static always_inline int slb_lookup (CPUPPCState *env, target_ulong eaddr,
678 target_ulong *page_mask, int *attr)
680 target_phys_addr_t sr_base;
687 sr_base = env->spr[SPR_ASR];
688 #if defined(DEBUG_SLB)
690 fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
691 __func__, eaddr, sr_base);
694 mask = 0x0000000000000000ULL; /* Avoid gcc warning */
695 for (n = 0; n < env->slb_nr; n++) {
696 tmp64 = ldq_phys(sr_base);
697 tmp = ldl_phys(sr_base + 8);
698 #if defined(DEBUG_SLB)
700 fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08"
701 PRIx32 "\n", __func__, n, sr_base, tmp64, tmp);
704 if (slb_is_valid(tmp64)) {
705 /* SLB entry is valid */
706 switch (tmp64 & 0x0000000006000000ULL) {
707 case 0x0000000000000000ULL:
709 mask = 0xFFFFFFFFF0000000ULL;
711 case 0x0000000002000000ULL:
713 mask = 0xFFFF000000000000ULL;
715 case 0x0000000004000000ULL:
716 case 0x0000000006000000ULL:
717 /* Reserved => segment is invalid */
720 if ((eaddr & mask) == (tmp64 & mask)) {
722 *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
735 void ppc_slb_invalidate_all (CPUPPCState *env)
737 target_phys_addr_t sr_base;
739 int n, do_invalidate;
742 sr_base = env->spr[SPR_ASR];
743 /* XXX: Warning: slbia never invalidates the first segment */
744 for (n = 1; n < env->slb_nr; n++) {
745 tmp64 = ldq_phys(sr_base);
746 if (slb_is_valid(tmp64)) {
747 slb_invalidate(&tmp64);
748 stq_phys(sr_base, tmp64);
749 /* XXX: given the fact that segment size is 256 MB or 1TB,
750 * and we still don't have a tlb_flush_mask(env, n, mask)
751 * in Qemu, we just invalidate all TLBs
761 void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
763 target_phys_addr_t sr_base;
764 target_ulong vsid, page_mask;
769 n = slb_lookup(env, T0, &vsid, &page_mask, &attr);
771 sr_base = env->spr[SPR_ASR];
773 tmp64 = ldq_phys(sr_base);
774 if (slb_is_valid(tmp64)) {
775 slb_invalidate(&tmp64);
776 stq_phys(sr_base, tmp64);
777 /* XXX: given the fact that segment size is 256 MB or 1TB,
778 * and we still don't have a tlb_flush_mask(env, n, mask)
779 * in Qemu, we just invalidate all TLBs
786 target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
788 target_phys_addr_t sr_base;
793 sr_base = env->spr[SPR_ASR];
794 sr_base += 12 * slb_nr;
795 tmp64 = ldq_phys(sr_base);
796 tmp = ldl_phys(sr_base + 8);
797 if (tmp64 & 0x0000000008000000ULL) {
798 /* SLB entry is valid */
799 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
800 rt = tmp >> 8; /* 65:88 => 40:63 */
801 rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
802 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
803 rt |= ((tmp >> 4) & 0xF) << 27;
807 #if defined(DEBUG_SLB)
809 fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
810 ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
817 void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
819 target_phys_addr_t sr_base;
823 sr_base = env->spr[SPR_ASR];
824 sr_base += 12 * slb_nr;
825 /* Copy Rs bits 37:63 to SLB 62:88 */
827 tmp64 = (rs >> 24) & 0x7;
828 /* Copy Rs bits 33:36 to SLB 89:92 */
829 tmp |= ((rs >> 27) & 0xF) << 4;
830 /* Set the valid bit */
833 tmp64 |= (uint32_t)slb_nr << 28;
834 #if defined(DEBUG_SLB)
836 fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64 " %08"
837 PRIx32 "\n", __func__, slb_nr, rs, sr_base, tmp64, tmp);
840 /* Write SLB entry to memory */
841 stq_phys(sr_base, tmp64);
842 stl_phys(sr_base + 8, tmp);
844 #endif /* defined(TARGET_PPC64) */
846 /* Perform segment based translation */
847 static always_inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
849 target_phys_addr_t hash,
850 target_phys_addr_t mask)
852 return (sdr1 & ((target_ulong)(-1ULL) << sdr_sh)) | (hash & mask);
855 static always_inline int get_segment (CPUState *env, mmu_ctx_t *ctx,
856 target_ulong eaddr, int rw, int type)
858 target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
859 target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
860 #if defined(TARGET_PPC64)
863 int ds, vsid_sh, sdr_sh, pr;
867 #if defined(TARGET_PPC64)
868 if (env->mmu_model == POWERPC_MMU_64B) {
869 #if defined (DEBUG_MMU)
871 fprintf(logfile, "Check SLBs\n");
874 ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
877 ctx->key = ((attr & 0x40) && (pr != 0)) ||
878 ((attr & 0x80) && (pr == 0)) ? 1 : 0;
880 ctx->nx = attr & 0x20 ? 1 : 0;
881 vsid_mask = 0x00003FFFFFFFFF80ULL;
886 #endif /* defined(TARGET_PPC64) */
888 sr = env->sr[eaddr >> 28];
889 page_mask = 0x0FFFFFFF;
890 ctx->key = (((sr & 0x20000000) && (pr != 0)) ||
891 ((sr & 0x40000000) && (pr == 0))) ? 1 : 0;
892 ds = sr & 0x80000000 ? 1 : 0;
893 ctx->nx = sr & 0x10000000 ? 1 : 0;
894 vsid = sr & 0x00FFFFFF;
895 vsid_mask = 0x01FFFFC0;
899 #if defined (DEBUG_MMU)
901 fprintf(logfile, "Check segment v=0x" ADDRX " %d 0x" ADDRX
902 " nip=0x" ADDRX " lr=0x" ADDRX
903 " ir=%d dr=%d pr=%d %d t=%d\n",
904 eaddr, (int)(eaddr >> 28), sr, env->nip,
905 env->lr, (int)msr_ir, (int)msr_dr, pr != 0 ? 1 : 0,
910 #if defined (DEBUG_MMU)
912 fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
913 ctx->key, ds, ctx->nx, vsid);
918 /* Check if instruction fetch is allowed, if needed */
919 if (type != ACCESS_CODE || ctx->nx == 0) {
920 /* Page address translation */
921 /* Primary table address */
923 pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
924 #if defined(TARGET_PPC64)
925 if (env->mmu_model == POWERPC_MMU_64B) {
926 htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
927 /* XXX: this is false for 1 TB segments */
928 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
932 htab_mask = sdr & 0x000001FF;
933 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
935 mask = (htab_mask << sdr_sh) | sdr_mask;
936 #if defined (DEBUG_MMU)
938 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
939 PADDRX " " ADDRX "\n", sdr, sdr_sh, hash, mask,
943 ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
944 /* Secondary table address */
945 hash = (~hash) & vsid_mask;
946 #if defined (DEBUG_MMU)
948 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
949 PADDRX "\n", sdr, sdr_sh, hash, mask);
952 ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
953 #if defined(TARGET_PPC64)
954 if (env->mmu_model == POWERPC_MMU_64B) {
955 /* Only 5 bits of the page index are used in the AVPN */
956 ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
960 ctx->ptem = (vsid << 7) | (pgidx >> 10);
962 /* Initialize real address with an invalid value */
963 ctx->raddr = (target_ulong)-1;
964 if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
965 env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
966 /* Software TLB search */
967 ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
969 #if defined (DEBUG_MMU)
971 fprintf(logfile, "0 sdr1=0x" PADDRX " vsid=0x%06x "
972 "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX "\n",
973 sdr, (uint32_t)vsid, (uint32_t)pgidx,
974 (uint32_t)hash, ctx->pg_addr[0]);
977 /* Primary table lookup */
978 ret = find_pte(env, ctx, 0, rw, type);
980 /* Secondary table lookup */
981 #if defined (DEBUG_MMU)
982 if (eaddr != 0xEFFFFFFF && loglevel != 0) {
984 "1 sdr1=0x" PADDRX " vsid=0x%06x api=0x%04x "
985 "hash=0x%05x pg_addr=0x" PADDRX "\n",
986 sdr, (uint32_t)vsid, (uint32_t)pgidx,
987 (uint32_t)hash, ctx->pg_addr[1]);
990 ret2 = find_pte(env, ctx, 1, rw, type);
995 #if defined (DUMP_PAGE_TABLES)
997 target_phys_addr_t curaddr;
998 uint32_t a0, a1, a2, a3;
1000 "Page table: " PADDRX " len " PADDRX "\n",
1002 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
1004 a0 = ldl_phys(curaddr);
1005 a1 = ldl_phys(curaddr + 4);
1006 a2 = ldl_phys(curaddr + 8);
1007 a3 = ldl_phys(curaddr + 12);
1008 if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
1010 PADDRX ": %08x %08x %08x %08x\n",
1011 curaddr, a0, a1, a2, a3);
1017 #if defined (DEBUG_MMU)
1019 fprintf(logfile, "No access allowed\n");
1024 #if defined (DEBUG_MMU)
1026 fprintf(logfile, "direct store...\n");
1028 /* Direct-store segment : absolutely *BUGGY* for now */
1031 /* Integer load/store : only access allowed */
1034 /* No code fetch is allowed in direct-store areas */
1037 /* Floating point load/store */
1040 /* lwarx, ldarx or srwcx. */
1043 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1044 /* Should make the instruction do no-op.
1045 * As it already do no-op, it's quite easy :-)
1050 /* eciwx or ecowx */
1054 fprintf(logfile, "ERROR: instruction should not need "
1055 "address translation\n");
1059 if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
1070 /* Generic TLB check function for embedded PowerPC implementations */
1071 static always_inline int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
1072 target_phys_addr_t *raddrp,
1073 target_ulong address,
1074 uint32_t pid, int ext, int i)
1078 /* Check valid flag */
1079 if (!(tlb->prot & PAGE_VALID)) {
1081 fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
1084 mask = ~(tlb->size - 1);
1085 #if defined (DEBUG_SOFTWARE_TLB)
1086 if (loglevel != 0) {
1087 fprintf(logfile, "%s: TLB %d address " ADDRX " PID %d <=> "
1088 ADDRX " " ADDRX " %d\n",
1089 __func__, i, address, pid, tlb->EPN, mask, (int)tlb->PID);
1093 if (tlb->PID != 0 && tlb->PID != pid)
1095 /* Check effective address */
1096 if ((address & mask) != tlb->EPN)
1098 *raddrp = (tlb->RPN & mask) | (address & ~mask);
1099 #if (TARGET_PHYS_ADDR_BITS >= 36)
1101 /* Extend the physical address to 36 bits */
1102 *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
1109 /* Generic TLB search function for PowerPC embedded implementations */
1110 int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
1113 target_phys_addr_t raddr;
1116 /* Default return value is no match */
1118 for (i = 0; i < env->nb_tlb; i++) {
1119 tlb = &env->tlb[i].tlbe;
1120 if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
1129 /* Helpers specific to PowerPC 40x implementations */
1130 static always_inline void ppc4xx_tlb_invalidate_all (CPUState *env)
1135 for (i = 0; i < env->nb_tlb; i++) {
1136 tlb = &env->tlb[i].tlbe;
1137 tlb->prot &= ~PAGE_VALID;
1142 static always_inline void ppc4xx_tlb_invalidate_virt (CPUState *env,
1146 #if !defined(FLUSH_ALL_TLBS)
1148 target_phys_addr_t raddr;
1149 target_ulong page, end;
1152 for (i = 0; i < env->nb_tlb; i++) {
1153 tlb = &env->tlb[i].tlbe;
1154 if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
1155 end = tlb->EPN + tlb->size;
1156 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1157 tlb_flush_page(env, page);
1158 tlb->prot &= ~PAGE_VALID;
1163 ppc4xx_tlb_invalidate_all(env);
1167 int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1168 target_ulong address, int rw, int access_type)
1171 target_phys_addr_t raddr;
1172 int i, ret, zsel, zpr, pr;
1177 for (i = 0; i < env->nb_tlb; i++) {
1178 tlb = &env->tlb[i].tlbe;
1179 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1180 env->spr[SPR_40x_PID], 0, i) < 0)
1182 zsel = (tlb->attr >> 4) & 0xF;
1183 zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
1184 #if defined (DEBUG_SOFTWARE_TLB)
1185 if (loglevel != 0) {
1186 fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1187 __func__, i, zsel, zpr, rw, tlb->attr);
1190 /* Check execute enable bit */
1197 /* All accesses granted */
1198 ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1210 /* Check from TLB entry */
1211 /* XXX: there is a problem here or in the TLB fill code... */
1212 ctx->prot = tlb->prot;
1213 ctx->prot |= PAGE_EXEC;
1214 ret = check_prot(ctx->prot, rw, access_type);
1219 #if defined (DEBUG_SOFTWARE_TLB)
1220 if (loglevel != 0) {
1221 fprintf(logfile, "%s: access granted " ADDRX " => " REGX
1222 " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1229 #if defined (DEBUG_SOFTWARE_TLB)
1230 if (loglevel != 0) {
1231 fprintf(logfile, "%s: access refused " ADDRX " => " REGX
1232 " %d %d\n", __func__, address, raddr, ctx->prot,
1240 void store_40x_sler (CPUPPCState *env, uint32_t val)
1242 /* XXX: TO BE FIXED */
1243 if (val != 0x00000000) {
1244 cpu_abort(env, "Little-endian regions are not supported by now\n");
1246 env->spr[SPR_405_SLER] = val;
1249 int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1250 target_ulong address, int rw,
1254 target_phys_addr_t raddr;
1259 for (i = 0; i < env->nb_tlb; i++) {
1260 tlb = &env->tlb[i].tlbe;
1261 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1262 env->spr[SPR_BOOKE_PID], 1, i) < 0)
1265 prot = tlb->prot & 0xF;
1267 prot = (tlb->prot >> 4) & 0xF;
1268 /* Check the address space */
1269 if (access_type == ACCESS_CODE) {
1270 if (msr_ir != (tlb->attr & 1))
1273 if (prot & PAGE_EXEC) {
1279 if (msr_dr != (tlb->attr & 1))
1282 if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1295 static always_inline int check_physical (CPUState *env, mmu_ctx_t *ctx,
1296 target_ulong eaddr, int rw)
1301 ctx->prot = PAGE_READ | PAGE_EXEC;
1303 switch (env->mmu_model) {
1304 case POWERPC_MMU_32B:
1305 case POWERPC_MMU_SOFT_6xx:
1306 case POWERPC_MMU_SOFT_74xx:
1307 case POWERPC_MMU_SOFT_4xx:
1308 case POWERPC_MMU_REAL_4xx:
1309 case POWERPC_MMU_BOOKE:
1310 ctx->prot |= PAGE_WRITE;
1312 #if defined(TARGET_PPC64)
1313 case POWERPC_MMU_64B:
1314 /* Real address are 60 bits long */
1315 ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1316 ctx->prot |= PAGE_WRITE;
1319 case POWERPC_MMU_SOFT_4xx_Z:
1320 if (unlikely(msr_pe != 0)) {
1321 /* 403 family add some particular protections,
1322 * using PBL/PBU registers for accesses with no translation.
1325 /* Check PLB validity */
1326 (env->pb[0] < env->pb[1] &&
1327 /* and address in plb area */
1328 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1329 (env->pb[2] < env->pb[3] &&
1330 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1331 if (in_plb ^ msr_px) {
1332 /* Access in protected area */
1334 /* Access is not allowed */
1338 /* Read-write access is allowed */
1339 ctx->prot |= PAGE_WRITE;
1343 case POWERPC_MMU_BOOKE_FSL:
1345 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1348 cpu_abort(env, "Unknown or invalid MMU model\n");
1355 int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1356 int rw, int access_type, int check_BATs)
1361 if (loglevel != 0) {
1362 fprintf(logfile, "%s\n", __func__);
1365 if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1366 (access_type != ACCESS_CODE && msr_dr == 0)) {
1367 /* No address translation */
1368 ret = check_physical(env, ctx, eaddr, rw);
1371 switch (env->mmu_model) {
1372 case POWERPC_MMU_32B:
1373 case POWERPC_MMU_SOFT_6xx:
1374 case POWERPC_MMU_SOFT_74xx:
1375 /* Try to find a BAT */
1377 ret = get_bat(env, ctx, eaddr, rw, access_type);
1379 #if defined(TARGET_PPC64)
1380 case POWERPC_MMU_64B:
1383 /* We didn't match any BAT entry or don't have BATs */
1384 ret = get_segment(env, ctx, eaddr, rw, access_type);
1387 case POWERPC_MMU_SOFT_4xx:
1388 case POWERPC_MMU_SOFT_4xx_Z:
1389 ret = mmu40x_get_physical_address(env, ctx, eaddr,
1392 case POWERPC_MMU_BOOKE:
1393 ret = mmubooke_get_physical_address(env, ctx, eaddr,
1396 case POWERPC_MMU_BOOKE_FSL:
1398 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1400 case POWERPC_MMU_REAL_4xx:
1401 cpu_abort(env, "PowerPC 401 does not do any translation\n");
1404 cpu_abort(env, "Unknown or invalid MMU model\n");
1409 if (loglevel != 0) {
1410 fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1411 __func__, eaddr, ret, ctx->raddr);
1418 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1422 if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT, 1) != 0))
1425 return ctx.raddr & TARGET_PAGE_MASK;
1428 /* Perform address translation */
1429 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1430 int mmu_idx, int is_softmmu)
1439 access_type = ACCESS_CODE;
1442 /* XXX: put correct access by using cpu_restore_state()
1444 access_type = ACCESS_INT;
1445 // access_type = env->access_type;
1447 ret = get_physical_address(env, &ctx, address, rw, access_type, 1);
1449 ret = tlb_set_page_exec(env, address & TARGET_PAGE_MASK,
1450 ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1451 mmu_idx, is_softmmu);
1452 } else if (ret < 0) {
1453 #if defined (DEBUG_MMU)
1455 cpu_dump_state(env, logfile, fprintf, 0);
1457 if (access_type == ACCESS_CODE) {
1460 /* No matches in page tables or TLB */
1461 switch (env->mmu_model) {
1462 case POWERPC_MMU_SOFT_6xx:
1463 env->exception_index = POWERPC_EXCP_IFTLB;
1464 env->error_code = 1 << 18;
1465 env->spr[SPR_IMISS] = address;
1466 env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1468 case POWERPC_MMU_SOFT_74xx:
1469 env->exception_index = POWERPC_EXCP_IFTLB;
1471 case POWERPC_MMU_SOFT_4xx:
1472 case POWERPC_MMU_SOFT_4xx_Z:
1473 env->exception_index = POWERPC_EXCP_ITLB;
1474 env->error_code = 0;
1475 env->spr[SPR_40x_DEAR] = address;
1476 env->spr[SPR_40x_ESR] = 0x00000000;
1478 case POWERPC_MMU_32B:
1479 #if defined(TARGET_PPC64)
1480 case POWERPC_MMU_64B:
1482 env->exception_index = POWERPC_EXCP_ISI;
1483 env->error_code = 0x40000000;
1485 case POWERPC_MMU_BOOKE:
1487 cpu_abort(env, "MMU model not implemented\n");
1489 case POWERPC_MMU_BOOKE_FSL:
1491 cpu_abort(env, "MMU model not implemented\n");
1493 case POWERPC_MMU_REAL_4xx:
1494 cpu_abort(env, "PowerPC 401 should never raise any MMU "
1498 cpu_abort(env, "Unknown or invalid MMU model\n");
1503 /* Access rights violation */
1504 env->exception_index = POWERPC_EXCP_ISI;
1505 env->error_code = 0x08000000;
1508 /* No execute protection violation */
1509 env->exception_index = POWERPC_EXCP_ISI;
1510 env->error_code = 0x10000000;
1513 /* Direct store exception */
1514 /* No code fetch is allowed in direct-store areas */
1515 env->exception_index = POWERPC_EXCP_ISI;
1516 env->error_code = 0x10000000;
1518 #if defined(TARGET_PPC64)
1520 /* No match in segment table */
1521 env->exception_index = POWERPC_EXCP_ISEG;
1522 env->error_code = 0;
1529 /* No matches in page tables or TLB */
1530 switch (env->mmu_model) {
1531 case POWERPC_MMU_SOFT_6xx:
1533 env->exception_index = POWERPC_EXCP_DSTLB;
1534 env->error_code = 1 << 16;
1536 env->exception_index = POWERPC_EXCP_DLTLB;
1537 env->error_code = 0;
1539 env->spr[SPR_DMISS] = address;
1540 env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1542 env->error_code |= ctx.key << 19;
1543 env->spr[SPR_HASH1] = ctx.pg_addr[0];
1544 env->spr[SPR_HASH2] = ctx.pg_addr[1];
1546 case POWERPC_MMU_SOFT_74xx:
1548 env->exception_index = POWERPC_EXCP_DSTLB;
1550 env->exception_index = POWERPC_EXCP_DLTLB;
1553 /* Implement LRU algorithm */
1554 env->error_code = ctx.key << 19;
1555 env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1556 ((env->last_way + 1) & (env->nb_ways - 1));
1557 env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
1559 case POWERPC_MMU_SOFT_4xx:
1560 case POWERPC_MMU_SOFT_4xx_Z:
1561 env->exception_index = POWERPC_EXCP_DTLB;
1562 env->error_code = 0;
1563 env->spr[SPR_40x_DEAR] = address;
1565 env->spr[SPR_40x_ESR] = 0x00800000;
1567 env->spr[SPR_40x_ESR] = 0x00000000;
1569 case POWERPC_MMU_32B:
1570 #if defined(TARGET_PPC64)
1571 case POWERPC_MMU_64B:
1573 env->exception_index = POWERPC_EXCP_DSI;
1574 env->error_code = 0;
1575 env->spr[SPR_DAR] = address;
1577 env->spr[SPR_DSISR] = 0x42000000;
1579 env->spr[SPR_DSISR] = 0x40000000;
1581 case POWERPC_MMU_BOOKE:
1583 cpu_abort(env, "MMU model not implemented\n");
1585 case POWERPC_MMU_BOOKE_FSL:
1587 cpu_abort(env, "MMU model not implemented\n");
1589 case POWERPC_MMU_REAL_4xx:
1590 cpu_abort(env, "PowerPC 401 should never raise any MMU "
1594 cpu_abort(env, "Unknown or invalid MMU model\n");
1599 /* Access rights violation */
1600 env->exception_index = POWERPC_EXCP_DSI;
1601 env->error_code = 0;
1602 env->spr[SPR_DAR] = address;
1604 env->spr[SPR_DSISR] = 0x0A000000;
1606 env->spr[SPR_DSISR] = 0x08000000;
1609 /* Direct store exception */
1610 switch (access_type) {
1612 /* Floating point load/store */
1613 env->exception_index = POWERPC_EXCP_ALIGN;
1614 env->error_code = POWERPC_EXCP_ALIGN_FP;
1615 env->spr[SPR_DAR] = address;
1618 /* lwarx, ldarx or stwcx. */
1619 env->exception_index = POWERPC_EXCP_DSI;
1620 env->error_code = 0;
1621 env->spr[SPR_DAR] = address;
1623 env->spr[SPR_DSISR] = 0x06000000;
1625 env->spr[SPR_DSISR] = 0x04000000;
1628 /* eciwx or ecowx */
1629 env->exception_index = POWERPC_EXCP_DSI;
1630 env->error_code = 0;
1631 env->spr[SPR_DAR] = address;
1633 env->spr[SPR_DSISR] = 0x06100000;
1635 env->spr[SPR_DSISR] = 0x04100000;
1638 printf("DSI: invalid exception (%d)\n", ret);
1639 env->exception_index = POWERPC_EXCP_PROGRAM;
1641 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1642 env->spr[SPR_DAR] = address;
1646 #if defined(TARGET_PPC64)
1648 /* No match in segment table */
1649 env->exception_index = POWERPC_EXCP_DSEG;
1650 env->error_code = 0;
1651 env->spr[SPR_DAR] = address;
1657 printf("%s: set exception to %d %02x\n", __func__,
1658 env->exception, env->error_code);
1666 /*****************************************************************************/
1667 /* BATs management */
1668 #if !defined(FLUSH_ALL_TLBS)
1669 static always_inline void do_invalidate_BAT (CPUPPCState *env,
1673 target_ulong base, end, page;
1675 base = BATu & ~0x0001FFFF;
1676 end = base + mask + 0x00020000;
1677 #if defined (DEBUG_BATS)
1678 if (loglevel != 0) {
1679 fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1683 for (page = base; page != end; page += TARGET_PAGE_SIZE)
1684 tlb_flush_page(env, page);
1685 #if defined (DEBUG_BATS)
1687 fprintf(logfile, "Flush done\n");
1692 static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1693 int ul, int nr, target_ulong value)
1695 #if defined (DEBUG_BATS)
1696 if (loglevel != 0) {
1697 fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1698 ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1703 target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1705 return env->IBAT[0][nr];
1708 target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1710 return env->IBAT[1][nr];
1713 void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1717 dump_store_bat(env, 'I', 0, nr, value);
1718 if (env->IBAT[0][nr] != value) {
1719 mask = (value << 15) & 0x0FFE0000UL;
1720 #if !defined(FLUSH_ALL_TLBS)
1721 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1723 /* When storing valid upper BAT, mask BEPI and BRPN
1724 * and invalidate all TLBs covered by this BAT
1726 mask = (value << 15) & 0x0FFE0000UL;
1727 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1728 (value & ~0x0001FFFFUL & ~mask);
1729 env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1730 (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1731 #if !defined(FLUSH_ALL_TLBS)
1732 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1739 void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1741 dump_store_bat(env, 'I', 1, nr, value);
1742 env->IBAT[1][nr] = value;
1745 target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1747 return env->DBAT[0][nr];
1750 target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1752 return env->DBAT[1][nr];
1755 void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1759 dump_store_bat(env, 'D', 0, nr, value);
1760 if (env->DBAT[0][nr] != value) {
1761 /* When storing valid upper BAT, mask BEPI and BRPN
1762 * and invalidate all TLBs covered by this BAT
1764 mask = (value << 15) & 0x0FFE0000UL;
1765 #if !defined(FLUSH_ALL_TLBS)
1766 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1768 mask = (value << 15) & 0x0FFE0000UL;
1769 env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1770 (value & ~0x0001FFFFUL & ~mask);
1771 env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1772 (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1773 #if !defined(FLUSH_ALL_TLBS)
1774 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1781 void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1783 dump_store_bat(env, 'D', 1, nr, value);
1784 env->DBAT[1][nr] = value;
1787 /*****************************************************************************/
1788 /* TLB management */
1789 void ppc_tlb_invalidate_all (CPUPPCState *env)
1791 switch (env->mmu_model) {
1792 case POWERPC_MMU_SOFT_6xx:
1793 case POWERPC_MMU_SOFT_74xx:
1794 ppc6xx_tlb_invalidate_all(env);
1796 case POWERPC_MMU_SOFT_4xx:
1797 case POWERPC_MMU_SOFT_4xx_Z:
1798 ppc4xx_tlb_invalidate_all(env);
1800 case POWERPC_MMU_REAL_4xx:
1801 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1803 case POWERPC_MMU_BOOKE:
1805 cpu_abort(env, "MMU model not implemented\n");
1807 case POWERPC_MMU_BOOKE_FSL:
1809 cpu_abort(env, "MMU model not implemented\n");
1811 case POWERPC_MMU_32B:
1812 #if defined(TARGET_PPC64)
1813 case POWERPC_MMU_64B:
1814 #endif /* defined(TARGET_PPC64) */
1819 cpu_abort(env, "Unknown MMU model\n");
1824 void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1826 #if !defined(FLUSH_ALL_TLBS)
1827 addr &= TARGET_PAGE_MASK;
1828 switch (env->mmu_model) {
1829 case POWERPC_MMU_SOFT_6xx:
1830 case POWERPC_MMU_SOFT_74xx:
1831 ppc6xx_tlb_invalidate_virt(env, addr, 0);
1832 if (env->id_tlbs == 1)
1833 ppc6xx_tlb_invalidate_virt(env, addr, 1);
1835 case POWERPC_MMU_SOFT_4xx:
1836 case POWERPC_MMU_SOFT_4xx_Z:
1837 ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
1839 case POWERPC_MMU_REAL_4xx:
1840 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1842 case POWERPC_MMU_BOOKE:
1844 cpu_abort(env, "MMU model not implemented\n");
1846 case POWERPC_MMU_BOOKE_FSL:
1848 cpu_abort(env, "MMU model not implemented\n");
1850 case POWERPC_MMU_32B:
1851 /* tlbie invalidate TLBs for all segments */
1852 addr &= ~((target_ulong)-1 << 28);
1853 /* XXX: this case should be optimized,
1854 * giving a mask to tlb_flush_page
1856 tlb_flush_page(env, addr | (0x0 << 28));
1857 tlb_flush_page(env, addr | (0x1 << 28));
1858 tlb_flush_page(env, addr | (0x2 << 28));
1859 tlb_flush_page(env, addr | (0x3 << 28));
1860 tlb_flush_page(env, addr | (0x4 << 28));
1861 tlb_flush_page(env, addr | (0x5 << 28));
1862 tlb_flush_page(env, addr | (0x6 << 28));
1863 tlb_flush_page(env, addr | (0x7 << 28));
1864 tlb_flush_page(env, addr | (0x8 << 28));
1865 tlb_flush_page(env, addr | (0x9 << 28));
1866 tlb_flush_page(env, addr | (0xA << 28));
1867 tlb_flush_page(env, addr | (0xB << 28));
1868 tlb_flush_page(env, addr | (0xC << 28));
1869 tlb_flush_page(env, addr | (0xD << 28));
1870 tlb_flush_page(env, addr | (0xE << 28));
1871 tlb_flush_page(env, addr | (0xF << 28));
1873 #if defined(TARGET_PPC64)
1874 case POWERPC_MMU_64B:
1875 /* tlbie invalidate TLBs for all segments */
1876 /* XXX: given the fact that there are too many segments to invalidate,
1877 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
1878 * we just invalidate all TLBs
1882 #endif /* defined(TARGET_PPC64) */
1885 cpu_abort(env, "Unknown MMU model\n");
1889 ppc_tlb_invalidate_all(env);
1893 /*****************************************************************************/
1894 /* Special registers manipulation */
1895 #if defined(TARGET_PPC64)
1896 target_ulong ppc_load_asr (CPUPPCState *env)
1901 void ppc_store_asr (CPUPPCState *env, target_ulong value)
1903 if (env->asr != value) {
1910 target_ulong do_load_sdr1 (CPUPPCState *env)
1915 void do_store_sdr1 (CPUPPCState *env, target_ulong value)
1917 #if defined (DEBUG_MMU)
1918 if (loglevel != 0) {
1919 fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
1922 if (env->sdr1 != value) {
1923 /* XXX: for PowerPC 64, should check that the HTABSIZE value
1932 target_ulong do_load_sr (CPUPPCState *env, int srnum)
1934 return env->sr[srnum];
1938 void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
1940 #if defined (DEBUG_MMU)
1941 if (loglevel != 0) {
1942 fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
1943 __func__, srnum, value, env->sr[srnum]);
1946 if (env->sr[srnum] != value) {
1947 env->sr[srnum] = value;
1948 #if !defined(FLUSH_ALL_TLBS) && 0
1950 target_ulong page, end;
1951 /* Invalidate 256 MB of virtual memory */
1952 page = (16 << 20) * srnum;
1953 end = page + (16 << 20);
1954 for (; page != end; page += TARGET_PAGE_SIZE)
1955 tlb_flush_page(env, page);
1962 #endif /* !defined (CONFIG_USER_ONLY) */
1964 target_ulong ppc_load_xer (CPUPPCState *env)
1966 return hreg_load_xer(env);
1969 void ppc_store_xer (CPUPPCState *env, target_ulong value)
1971 hreg_store_xer(env, value);
1974 /* GDBstub can read and write MSR... */
1975 void ppc_store_msr (CPUPPCState *env, target_ulong value)
1977 hreg_store_msr(env, value);
1980 /*****************************************************************************/
1981 /* Exception processing */
1982 #if defined (CONFIG_USER_ONLY)
1983 void do_interrupt (CPUState *env)
1985 env->exception_index = POWERPC_EXCP_NONE;
1986 env->error_code = 0;
1989 void ppc_hw_interrupt (CPUState *env)
1991 env->exception_index = POWERPC_EXCP_NONE;
1992 env->error_code = 0;
1994 #else /* defined (CONFIG_USER_ONLY) */
1995 static always_inline void dump_syscall (CPUState *env)
1997 fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
1998 " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
1999 env->gpr[0], env->gpr[3], env->gpr[4],
2000 env->gpr[5], env->gpr[6], env->nip);
2003 /* Note that this function should be greatly optimized
2004 * when called with a constant excp, from ppc_hw_interrupt
2006 static always_inline void powerpc_excp (CPUState *env,
2007 int excp_model, int excp)
2009 target_ulong msr, new_msr, vector;
2010 int srr0, srr1, asrr0, asrr1;
2011 #if defined(TARGET_PPC64H)
2012 int lpes0, lpes1, lev;
2014 lpes0 = (env->spr[SPR_LPCR] >> 1) & 1;
2015 lpes1 = (env->spr[SPR_LPCR] >> 2) & 1;
2018 if (loglevel & CPU_LOG_INT) {
2019 fprintf(logfile, "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
2020 env->nip, excp, env->error_code);
2028 msr &= ~((target_ulong)0x783F0000);
2030 case POWERPC_EXCP_NONE:
2031 /* Should never happen */
2033 case POWERPC_EXCP_CRITICAL: /* Critical input */
2034 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2035 switch (excp_model) {
2036 case POWERPC_EXCP_40x:
2037 srr0 = SPR_40x_SRR2;
2038 srr1 = SPR_40x_SRR3;
2040 case POWERPC_EXCP_BOOKE:
2041 srr0 = SPR_BOOKE_CSRR0;
2042 srr1 = SPR_BOOKE_CSRR1;
2044 case POWERPC_EXCP_G2:
2050 case POWERPC_EXCP_MCHECK: /* Machine check exception */
2052 /* Machine check exception is not enabled.
2053 * Enter checkstop state.
2055 if (loglevel != 0) {
2056 fprintf(logfile, "Machine check while not allowed. "
2057 "Entering checkstop state\n");
2059 fprintf(stderr, "Machine check while not allowed. "
2060 "Entering checkstop state\n");
2063 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2065 new_msr &= ~((target_ulong)1 << MSR_RI);
2066 new_msr &= ~((target_ulong)1 << MSR_ME);
2067 #if defined(TARGET_PPC64H)
2068 new_msr |= (target_ulong)1 << MSR_HV;
2070 /* XXX: should also have something loaded in DAR / DSISR */
2071 switch (excp_model) {
2072 case POWERPC_EXCP_40x:
2073 srr0 = SPR_40x_SRR2;
2074 srr1 = SPR_40x_SRR3;
2076 case POWERPC_EXCP_BOOKE:
2077 srr0 = SPR_BOOKE_MCSRR0;
2078 srr1 = SPR_BOOKE_MCSRR1;
2079 asrr0 = SPR_BOOKE_CSRR0;
2080 asrr1 = SPR_BOOKE_CSRR1;
2086 case POWERPC_EXCP_DSI: /* Data storage exception */
2087 #if defined (DEBUG_EXCEPTIONS)
2088 if (loglevel != 0) {
2089 fprintf(logfile, "DSI exception: DSISR=0x" ADDRX" DAR=0x" ADDRX
2090 "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
2093 new_msr &= ~((target_ulong)1 << MSR_RI);
2094 #if defined(TARGET_PPC64H)
2096 new_msr |= (target_ulong)1 << MSR_HV;
2099 case POWERPC_EXCP_ISI: /* Instruction storage exception */
2100 #if defined (DEBUG_EXCEPTIONS)
2101 if (loglevel != 0) {
2102 fprintf(logfile, "ISI exception: msr=0x" ADDRX ", nip=0x" ADDRX
2103 "\n", msr, env->nip);
2106 new_msr &= ~((target_ulong)1 << MSR_RI);
2107 #if defined(TARGET_PPC64H)
2109 new_msr |= (target_ulong)1 << MSR_HV;
2111 msr |= env->error_code;
2113 case POWERPC_EXCP_EXTERNAL: /* External input */
2114 new_msr &= ~((target_ulong)1 << MSR_RI);
2115 #if defined(TARGET_PPC64H)
2117 new_msr |= (target_ulong)1 << MSR_HV;
2120 case POWERPC_EXCP_ALIGN: /* Alignment exception */
2121 new_msr &= ~((target_ulong)1 << MSR_RI);
2122 #if defined(TARGET_PPC64H)
2124 new_msr |= (target_ulong)1 << MSR_HV;
2126 /* XXX: this is false */
2127 /* Get rS/rD and rA from faulting opcode */
2128 env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
2130 case POWERPC_EXCP_PROGRAM: /* Program exception */
2131 switch (env->error_code & ~0xF) {
2132 case POWERPC_EXCP_FP:
2133 if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
2134 #if defined (DEBUG_EXCEPTIONS)
2135 if (loglevel != 0) {
2136 fprintf(logfile, "Ignore floating point exception\n");
2139 env->exception_index = POWERPC_EXCP_NONE;
2140 env->error_code = 0;
2143 new_msr &= ~((target_ulong)1 << MSR_RI);
2144 #if defined(TARGET_PPC64H)
2146 new_msr |= (target_ulong)1 << MSR_HV;
2149 if (msr_fe0 != msr_fe1) {
2154 case POWERPC_EXCP_INVAL:
2155 #if defined (DEBUG_EXCEPTIONS)
2156 if (loglevel != 0) {
2157 fprintf(logfile, "Invalid instruction at 0x" ADDRX "\n",
2161 new_msr &= ~((target_ulong)1 << MSR_RI);
2162 #if defined(TARGET_PPC64H)
2164 new_msr |= (target_ulong)1 << MSR_HV;
2168 case POWERPC_EXCP_PRIV:
2169 new_msr &= ~((target_ulong)1 << MSR_RI);
2170 #if defined(TARGET_PPC64H)
2172 new_msr |= (target_ulong)1 << MSR_HV;
2176 case POWERPC_EXCP_TRAP:
2177 new_msr &= ~((target_ulong)1 << MSR_RI);
2178 #if defined(TARGET_PPC64H)
2180 new_msr |= (target_ulong)1 << MSR_HV;
2185 /* Should never occur */
2186 cpu_abort(env, "Invalid program exception %d. Aborting\n",
2191 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
2192 new_msr &= ~((target_ulong)1 << MSR_RI);
2193 #if defined(TARGET_PPC64H)
2195 new_msr |= (target_ulong)1 << MSR_HV;
2198 case POWERPC_EXCP_SYSCALL: /* System call exception */
2199 /* NOTE: this is a temporary hack to support graphics OSI
2200 calls from the MOL driver */
2201 /* XXX: To be removed */
2202 if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2204 if (env->osi_call(env) != 0) {
2205 env->exception_index = POWERPC_EXCP_NONE;
2206 env->error_code = 0;
2210 if (loglevel & CPU_LOG_INT) {
2213 new_msr &= ~((target_ulong)1 << MSR_RI);
2214 #if defined(TARGET_PPC64H)
2215 lev = env->error_code;
2216 if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
2217 new_msr |= (target_ulong)1 << MSR_HV;
2220 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
2221 new_msr &= ~((target_ulong)1 << MSR_RI);
2223 case POWERPC_EXCP_DECR: /* Decrementer exception */
2224 new_msr &= ~((target_ulong)1 << MSR_RI);
2225 #if defined(TARGET_PPC64H)
2227 new_msr |= (target_ulong)1 << MSR_HV;
2230 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
2232 #if defined (DEBUG_EXCEPTIONS)
2234 fprintf(logfile, "FIT exception\n");
2236 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2238 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
2239 #if defined (DEBUG_EXCEPTIONS)
2241 fprintf(logfile, "WDT exception\n");
2243 switch (excp_model) {
2244 case POWERPC_EXCP_BOOKE:
2245 srr0 = SPR_BOOKE_CSRR0;
2246 srr1 = SPR_BOOKE_CSRR1;
2251 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2253 case POWERPC_EXCP_DTLB: /* Data TLB error */
2254 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2256 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
2257 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2259 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
2260 switch (excp_model) {
2261 case POWERPC_EXCP_BOOKE:
2262 srr0 = SPR_BOOKE_DSRR0;
2263 srr1 = SPR_BOOKE_DSRR1;
2264 asrr0 = SPR_BOOKE_CSRR0;
2265 asrr1 = SPR_BOOKE_CSRR1;
2271 cpu_abort(env, "Debug exception is not implemented yet !\n");
2273 #if defined(TARGET_PPCEMB)
2274 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable */
2275 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2277 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */
2279 cpu_abort(env, "Embedded floating point data exception "
2280 "is not implemented yet !\n");
2282 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */
2284 cpu_abort(env, "Embedded floating point round exception "
2285 "is not implemented yet !\n");
2287 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor interrupt */
2288 new_msr &= ~((target_ulong)1 << MSR_RI);
2291 "Performance counter exception is not implemented yet !\n");
2293 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
2296 "Embedded doorbell interrupt is not implemented yet !\n");
2298 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
2299 switch (excp_model) {
2300 case POWERPC_EXCP_BOOKE:
2301 srr0 = SPR_BOOKE_CSRR0;
2302 srr1 = SPR_BOOKE_CSRR1;
2308 cpu_abort(env, "Embedded doorbell critical interrupt "
2309 "is not implemented yet !\n");
2311 #endif /* defined(TARGET_PPCEMB) */
2312 case POWERPC_EXCP_RESET: /* System reset exception */
2313 new_msr &= ~((target_ulong)1 << MSR_RI);
2314 #if defined(TARGET_PPC64H)
2315 new_msr |= (target_ulong)1 << MSR_HV;
2318 #if defined(TARGET_PPC64)
2319 case POWERPC_EXCP_DSEG: /* Data segment exception */
2320 new_msr &= ~((target_ulong)1 << MSR_RI);
2321 #if defined(TARGET_PPC64H)
2323 new_msr |= (target_ulong)1 << MSR_HV;
2326 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
2327 new_msr &= ~((target_ulong)1 << MSR_RI);
2328 #if defined(TARGET_PPC64H)
2330 new_msr |= (target_ulong)1 << MSR_HV;
2333 #endif /* defined(TARGET_PPC64) */
2334 #if defined(TARGET_PPC64H)
2335 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
2338 new_msr |= (target_ulong)1 << MSR_HV;
2341 case POWERPC_EXCP_TRACE: /* Trace exception */
2342 new_msr &= ~((target_ulong)1 << MSR_RI);
2343 #if defined(TARGET_PPC64H)
2345 new_msr |= (target_ulong)1 << MSR_HV;
2348 #if defined(TARGET_PPC64H)
2349 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
2352 new_msr |= (target_ulong)1 << MSR_HV;
2354 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */
2357 new_msr |= (target_ulong)1 << MSR_HV;
2359 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
2362 new_msr |= (target_ulong)1 << MSR_HV;
2364 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment exception */
2367 new_msr |= (target_ulong)1 << MSR_HV;
2369 #endif /* defined(TARGET_PPC64H) */
2370 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
2371 new_msr &= ~((target_ulong)1 << MSR_RI);
2372 #if defined(TARGET_PPC64H)
2374 new_msr |= (target_ulong)1 << MSR_HV;
2377 case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */
2378 #if defined (DEBUG_EXCEPTIONS)
2380 fprintf(logfile, "PIT exception\n");
2382 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2384 case POWERPC_EXCP_IO: /* IO error exception */
2386 cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2388 case POWERPC_EXCP_RUNM: /* Run mode exception */
2390 cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2392 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
2394 cpu_abort(env, "602 emulation trap exception "
2395 "is not implemented yet !\n");
2397 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
2398 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2399 #if defined(TARGET_PPC64H) /* XXX: check this */
2401 new_msr |= (target_ulong)1 << MSR_HV;
2403 switch (excp_model) {
2404 case POWERPC_EXCP_602:
2405 case POWERPC_EXCP_603:
2406 case POWERPC_EXCP_603E:
2407 case POWERPC_EXCP_G2:
2409 case POWERPC_EXCP_7x5:
2411 case POWERPC_EXCP_74xx:
2414 cpu_abort(env, "Invalid instruction TLB miss exception\n");
2418 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
2419 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2420 #if defined(TARGET_PPC64H) /* XXX: check this */
2422 new_msr |= (target_ulong)1 << MSR_HV;
2424 switch (excp_model) {
2425 case POWERPC_EXCP_602:
2426 case POWERPC_EXCP_603:
2427 case POWERPC_EXCP_603E:
2428 case POWERPC_EXCP_G2:
2430 case POWERPC_EXCP_7x5:
2432 case POWERPC_EXCP_74xx:
2435 cpu_abort(env, "Invalid data load TLB miss exception\n");
2439 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
2440 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2441 #if defined(TARGET_PPC64H) /* XXX: check this */
2443 new_msr |= (target_ulong)1 << MSR_HV;
2445 switch (excp_model) {
2446 case POWERPC_EXCP_602:
2447 case POWERPC_EXCP_603:
2448 case POWERPC_EXCP_603E:
2449 case POWERPC_EXCP_G2:
2451 /* Swap temporary saved registers with GPRs */
2452 if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
2453 new_msr |= (target_ulong)1 << MSR_TGPR;
2454 hreg_swap_gpr_tgpr(env);
2457 case POWERPC_EXCP_7x5:
2459 #if defined (DEBUG_SOFTWARE_TLB)
2460 if (loglevel != 0) {
2461 const unsigned char *es;
2462 target_ulong *miss, *cmp;
2464 if (excp == POWERPC_EXCP_IFTLB) {
2467 miss = &env->spr[SPR_IMISS];
2468 cmp = &env->spr[SPR_ICMP];
2470 if (excp == POWERPC_EXCP_DLTLB)
2475 miss = &env->spr[SPR_DMISS];
2476 cmp = &env->spr[SPR_DCMP];
2478 fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2479 " H1 " ADDRX " H2 " ADDRX " %08x\n",
2480 es, en, *miss, en, *cmp,
2481 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2485 msr |= env->crf[0] << 28;
2486 msr |= env->error_code; /* key, D/I, S/L bits */
2487 /* Set way using a LRU mechanism */
2488 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2490 case POWERPC_EXCP_74xx:
2492 #if defined (DEBUG_SOFTWARE_TLB)
2493 if (loglevel != 0) {
2494 const unsigned char *es;
2495 target_ulong *miss, *cmp;
2497 if (excp == POWERPC_EXCP_IFTLB) {
2500 miss = &env->spr[SPR_TLBMISS];
2501 cmp = &env->spr[SPR_PTEHI];
2503 if (excp == POWERPC_EXCP_DLTLB)
2508 miss = &env->spr[SPR_TLBMISS];
2509 cmp = &env->spr[SPR_PTEHI];
2511 fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2513 es, en, *miss, en, *cmp, env->error_code);
2516 msr |= env->error_code; /* key bit */
2519 cpu_abort(env, "Invalid data store TLB miss exception\n");
2523 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
2525 cpu_abort(env, "Floating point assist exception "
2526 "is not implemented yet !\n");
2528 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
2530 cpu_abort(env, "IABR exception is not implemented yet !\n");
2532 case POWERPC_EXCP_SMI: /* System management interrupt */
2534 cpu_abort(env, "SMI exception is not implemented yet !\n");
2536 case POWERPC_EXCP_THERM: /* Thermal interrupt */
2538 cpu_abort(env, "Thermal management exception "
2539 "is not implemented yet !\n");
2541 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
2542 new_msr &= ~((target_ulong)1 << MSR_RI);
2543 #if defined(TARGET_PPC64H)
2545 new_msr |= (target_ulong)1 << MSR_HV;
2549 "Performance counter exception is not implemented yet !\n");
2551 case POWERPC_EXCP_VPUA: /* Vector assist exception */
2553 cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2555 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
2558 "970 soft-patch exception is not implemented yet !\n");
2560 case POWERPC_EXCP_MAINT: /* Maintenance exception */
2563 "970 maintenance exception is not implemented yet !\n");
2567 cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2570 /* save current instruction location */
2571 env->spr[srr0] = env->nip - 4;
2574 /* save next instruction location */
2575 env->spr[srr0] = env->nip;
2579 env->spr[srr1] = msr;
2580 /* If any alternate SRR register are defined, duplicate saved values */
2582 env->spr[asrr0] = env->spr[srr0];
2584 env->spr[asrr1] = env->spr[srr1];
2585 /* If we disactivated any translation, flush TLBs */
2586 if (new_msr & ((1 << MSR_IR) | (1 << MSR_DR)))
2588 /* reload MSR with correct bits */
2589 new_msr &= ~((target_ulong)1 << MSR_EE);
2590 new_msr &= ~((target_ulong)1 << MSR_PR);
2591 new_msr &= ~((target_ulong)1 << MSR_FP);
2592 new_msr &= ~((target_ulong)1 << MSR_FE0);
2593 new_msr &= ~((target_ulong)1 << MSR_SE);
2594 new_msr &= ~((target_ulong)1 << MSR_BE);
2595 new_msr &= ~((target_ulong)1 << MSR_FE1);
2596 new_msr &= ~((target_ulong)1 << MSR_IR);
2597 new_msr &= ~((target_ulong)1 << MSR_DR);
2598 #if 0 /* Fix this: not on all targets */
2599 new_msr &= ~((target_ulong)1 << MSR_PMM);
2601 new_msr &= ~((target_ulong)1 << MSR_LE);
2603 new_msr |= (target_ulong)1 << MSR_LE;
2605 new_msr &= ~((target_ulong)1 << MSR_LE);
2606 /* Jump to handler */
2607 vector = env->excp_vectors[excp];
2608 if (vector == (target_ulong)-1) {
2609 cpu_abort(env, "Raised an exception without defined vector %d\n",
2612 vector |= env->excp_prefix;
2613 #if defined(TARGET_PPC64)
2614 if (excp_model == POWERPC_EXCP_BOOKE) {
2616 new_msr &= ~((target_ulong)1 << MSR_CM);
2617 vector = (uint32_t)vector;
2619 new_msr |= (target_ulong)1 << MSR_CM;
2623 new_msr &= ~((target_ulong)1 << MSR_SF);
2624 vector = (uint32_t)vector;
2626 new_msr |= (target_ulong)1 << MSR_SF;
2630 /* XXX: we don't use hreg_store_msr here as already have treated
2631 * any special case that could occur. Just store MSR and update hflags
2634 hreg_compute_hflags(env);
2636 /* Reset exception state */
2637 env->exception_index = POWERPC_EXCP_NONE;
2638 env->error_code = 0;
2641 void do_interrupt (CPUState *env)
2643 powerpc_excp(env, env->excp_model, env->exception_index);
2646 void ppc_hw_interrupt (CPUPPCState *env)
2648 #if defined(TARGET_PPC64H)
2653 if (loglevel & CPU_LOG_INT) {
2654 fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2655 __func__, env, env->pending_interrupts,
2656 env->interrupt_request, (int)msr_me, (int)msr_ee);
2659 /* External reset */
2660 if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2661 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2662 powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2665 /* Machine check exception */
2666 if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2667 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2668 powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2672 /* External debug exception */
2673 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2674 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2675 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2679 #if defined(TARGET_PPC64H)
2680 hdice = env->spr[SPR_LPCR] & 1;
2681 if ((msr_ee != 0 || msr_hv == 0 || msr_pr != 0) && hdice != 0) {
2682 /* Hypervisor decrementer exception */
2683 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2684 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2685 powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2691 /* External critical interrupt */
2692 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2693 /* Taking a critical external interrupt does not clear the external
2694 * critical interrupt status
2697 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2699 powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2704 /* Watchdog timer on embedded PowerPC */
2705 if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2706 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2707 powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2710 #if defined(TARGET_PPCEMB)
2711 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2712 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2713 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2717 #if defined(TARGET_PPCEMB)
2718 /* External interrupt */
2719 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2720 /* Taking an external interrupt does not clear the external
2724 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2726 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2730 /* Fixed interval timer on embedded PowerPC */
2731 if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2732 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2733 powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2736 /* Programmable interval timer on embedded PowerPC */
2737 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2738 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2739 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2742 /* Decrementer exception */
2743 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2744 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2745 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2748 #if !defined(TARGET_PPCEMB)
2749 /* External interrupt */
2750 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2751 /* Taking an external interrupt does not clear the external
2755 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2757 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2761 #if defined(TARGET_PPCEMB)
2762 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2763 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2764 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2768 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2769 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2770 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2773 /* Thermal interrupt */
2774 if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2775 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2776 powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2781 #endif /* !CONFIG_USER_ONLY */
2783 void cpu_dump_EA (target_ulong EA)
2793 fprintf(f, "Memory access at address " ADDRX "\n", EA);
2796 void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2806 fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2810 void cpu_ppc_reset (void *opaque)
2816 msr = (target_ulong)0;
2817 #if defined(TARGET_PPC64)
2818 msr |= (target_ulong)0 << MSR_HV; /* Should be 1... */
2820 msr |= (target_ulong)0 << MSR_AP; /* TO BE CHECKED */
2821 msr |= (target_ulong)0 << MSR_SA; /* TO BE CHECKED */
2822 msr |= (target_ulong)1 << MSR_EP;
2823 #if defined (DO_SINGLE_STEP) && 0
2824 /* Single step trace mode */
2825 msr |= (target_ulong)1 << MSR_SE;
2826 msr |= (target_ulong)1 << MSR_BE;
2828 #if defined(CONFIG_USER_ONLY)
2829 msr |= (target_ulong)1 << MSR_FP; /* Allow floating point usage */
2830 msr |= (target_ulong)1 << MSR_PR;
2832 env->nip = env->hreset_vector | env->excp_prefix;
2833 if (env->mmu_model != POWERPC_MMU_REAL_4xx)
2834 ppc_tlb_invalidate_all(env);
2837 hreg_compute_hflags(env);
2839 /* Be sure no exception or interrupt is pending */
2840 env->pending_interrupts = 0;
2841 env->exception_index = POWERPC_EXCP_NONE;
2842 env->error_code = 0;
2843 /* Flush all TLBs */
2847 CPUPPCState *cpu_ppc_init (void)
2851 env = qemu_mallocz(sizeof(CPUPPCState));
2859 void cpu_ppc_close (CPUPPCState *env)
2861 /* Should also remove all opcode tables... */