2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 //#define DEBUG_SOFTWARE_TLB
34 //#define DEBUG_EXCEPTIONS
35 //#define FLUSH_ALL_TLBS
37 /*****************************************************************************/
38 /* PowerPC MMU emulation */
40 #if defined(CONFIG_USER_ONLY)
41 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
42 int mmu_idx, int is_softmmu)
44 int exception, error_code;
47 exception = POWERPC_EXCP_ISI;
48 error_code = 0x40000000;
50 exception = POWERPC_EXCP_DSI;
51 error_code = 0x40000000;
53 error_code |= 0x02000000;
54 env->spr[SPR_DAR] = address;
55 env->spr[SPR_DSISR] = error_code;
57 env->exception_index = exception;
58 env->error_code = error_code;
63 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
69 /* Common routines used by software and hardware TLBs emulation */
70 static always_inline int pte_is_valid (target_ulong pte0)
72 return pte0 & 0x80000000 ? 1 : 0;
75 static always_inline void pte_invalidate (target_ulong *pte0)
80 #if defined(TARGET_PPC64)
81 static always_inline int pte64_is_valid (target_ulong pte0)
83 return pte0 & 0x0000000000000001ULL ? 1 : 0;
86 static always_inline void pte64_invalidate (target_ulong *pte0)
88 *pte0 &= ~0x0000000000000001ULL;
92 #define PTE_PTEM_MASK 0x7FFFFFBF
93 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
94 #if defined(TARGET_PPC64)
95 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
96 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
99 static always_inline int _pte_check (mmu_ctx_t *ctx, int is_64b,
100 target_ulong pte0, target_ulong pte1,
103 target_ulong ptem, mmask;
104 int access, ret, pteh, ptev;
108 /* Check validity and table match */
109 #if defined(TARGET_PPC64)
111 ptev = pte64_is_valid(pte0);
112 pteh = (pte0 >> 1) & 1;
116 ptev = pte_is_valid(pte0);
117 pteh = (pte0 >> 6) & 1;
119 if (ptev && h == pteh) {
120 /* Check vsid & api */
121 #if defined(TARGET_PPC64)
123 ptem = pte0 & PTE64_PTEM_MASK;
124 mmask = PTE64_CHECK_MASK;
128 ptem = pte0 & PTE_PTEM_MASK;
129 mmask = PTE_CHECK_MASK;
131 if (ptem == ctx->ptem) {
132 if (ctx->raddr != (target_ulong)-1) {
133 /* all matches should have equal RPN, WIMG & PP */
134 if ((ctx->raddr & mmask) != (pte1 & mmask)) {
136 fprintf(logfile, "Bad RPN/WIMG/PP\n");
140 /* Compute access rights */
143 if ((pte1 & 0x00000003) != 0x3)
144 access |= PAGE_WRITE;
146 switch (pte1 & 0x00000003) {
155 access = PAGE_READ | PAGE_WRITE;
159 /* Keep the matching PTE informations */
162 if ((rw == 0 && (access & PAGE_READ)) ||
163 (rw == 1 && (access & PAGE_WRITE))) {
165 #if defined (DEBUG_MMU)
167 fprintf(logfile, "PTE access granted !\n");
171 /* Access right violation */
172 #if defined (DEBUG_MMU)
174 fprintf(logfile, "PTE access rejected\n");
184 static int pte32_check (mmu_ctx_t *ctx,
185 target_ulong pte0, target_ulong pte1, int h, int rw)
187 return _pte_check(ctx, 0, pte0, pte1, h, rw);
190 #if defined(TARGET_PPC64)
191 static int pte64_check (mmu_ctx_t *ctx,
192 target_ulong pte0, target_ulong pte1, int h, int rw)
194 return _pte_check(ctx, 1, pte0, pte1, h, rw);
198 static int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
203 /* Update page flags */
204 if (!(*pte1p & 0x00000100)) {
205 /* Update accessed flag */
206 *pte1p |= 0x00000100;
209 if (!(*pte1p & 0x00000080)) {
210 if (rw == 1 && ret == 0) {
211 /* Update changed flag */
212 *pte1p |= 0x00000080;
215 /* Force page fault for first write access */
216 ctx->prot &= ~PAGE_WRITE;
223 /* Software driven TLB helpers */
224 static int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
225 int way, int is_code)
229 /* Select TLB num in a way from address */
230 nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
232 nr += env->tlb_per_way * way;
233 /* 6xx have separate TLBs for instructions and data */
234 if (is_code && env->id_tlbs == 1)
240 static void ppc6xx_tlb_invalidate_all (CPUState *env)
245 #if defined (DEBUG_SOFTWARE_TLB) && 0
247 fprintf(logfile, "Invalidate all TLBs\n");
250 /* Invalidate all defined software TLB */
252 if (env->id_tlbs == 1)
254 for (nr = 0; nr < max; nr++) {
255 tlb = &env->tlb[nr].tlb6;
256 pte_invalidate(&tlb->pte0);
261 static always_inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
266 #if !defined(FLUSH_ALL_TLBS)
270 /* Invalidate ITLB + DTLB, all ways */
271 for (way = 0; way < env->nb_ways; way++) {
272 nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
273 tlb = &env->tlb[nr].tlb6;
274 if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
275 #if defined (DEBUG_SOFTWARE_TLB)
277 fprintf(logfile, "TLB invalidate %d/%d " ADDRX "\n",
278 nr, env->nb_tlb, eaddr);
281 pte_invalidate(&tlb->pte0);
282 tlb_flush_page(env, tlb->EPN);
286 /* XXX: PowerPC specification say this is valid as well */
287 ppc6xx_tlb_invalidate_all(env);
291 static void ppc6xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
294 __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
297 void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
298 target_ulong pte0, target_ulong pte1)
303 nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
304 tlb = &env->tlb[nr].tlb6;
305 #if defined (DEBUG_SOFTWARE_TLB)
307 fprintf(logfile, "Set TLB %d/%d EPN " ADDRX " PTE0 " ADDRX
308 " PTE1 " ADDRX "\n", nr, env->nb_tlb, EPN, pte0, pte1);
311 /* Invalidate any pending reference in Qemu for this virtual address */
312 __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
316 /* Store last way for LRU mechanism */
320 static int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
321 target_ulong eaddr, int rw, int access_type)
328 ret = -1; /* No TLB found */
329 for (way = 0; way < env->nb_ways; way++) {
330 nr = ppc6xx_tlb_getnum(env, eaddr, way,
331 access_type == ACCESS_CODE ? 1 : 0);
332 tlb = &env->tlb[nr].tlb6;
333 /* This test "emulates" the PTE index match for hardware TLBs */
334 if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
335 #if defined (DEBUG_SOFTWARE_TLB)
337 fprintf(logfile, "TLB %d/%d %s [" ADDRX " " ADDRX
340 pte_is_valid(tlb->pte0) ? "valid" : "inval",
341 tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
346 #if defined (DEBUG_SOFTWARE_TLB)
348 fprintf(logfile, "TLB %d/%d %s " ADDRX " <> " ADDRX " " ADDRX
351 pte_is_valid(tlb->pte0) ? "valid" : "inval",
352 tlb->EPN, eaddr, tlb->pte1,
353 rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
356 switch (pte32_check(ctx, tlb->pte0, tlb->pte1, 0, rw)) {
358 /* TLB inconsistency */
361 /* Access violation */
371 /* XXX: we should go on looping to check all TLBs consistency
372 * but we can speed-up the whole thing as the
373 * result would be undefined if TLBs are not consistent.
382 #if defined (DEBUG_SOFTWARE_TLB)
384 fprintf(logfile, "found TLB at addr 0x%08lx prot=0x%01x ret=%d\n",
385 ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
388 /* Update page flags */
389 pte_update_flags(ctx, &env->tlb[best].tlb6.pte1, ret, rw);
395 /* Perform BAT hit & translation */
396 static int get_bat (CPUState *env, mmu_ctx_t *ctx,
397 target_ulong virtual, int rw, int type)
399 target_ulong *BATlt, *BATut, *BATu, *BATl;
400 target_ulong base, BEPIl, BEPIu, bl;
404 #if defined (DEBUG_BATS)
406 fprintf(logfile, "%s: %cBAT v 0x" ADDRX "\n", __func__,
407 type == ACCESS_CODE ? 'I' : 'D', virtual);
412 BATlt = env->IBAT[1];
413 BATut = env->IBAT[0];
416 BATlt = env->DBAT[1];
417 BATut = env->DBAT[0];
420 #if defined (DEBUG_BATS)
422 fprintf(logfile, "%s...: %cBAT v 0x" ADDRX "\n", __func__,
423 type == ACCESS_CODE ? 'I' : 'D', virtual);
426 base = virtual & 0xFFFC0000;
427 for (i = 0; i < 4; i++) {
430 BEPIu = *BATu & 0xF0000000;
431 BEPIl = *BATu & 0x0FFE0000;
432 bl = (*BATu & 0x00001FFC) << 15;
433 #if defined (DEBUG_BATS)
435 fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
436 " BATl 0x" ADDRX "\n",
437 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
441 if ((virtual & 0xF0000000) == BEPIu &&
442 ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
444 if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
445 (msr_pr == 1 && (*BATu & 0x00000001))) {
446 /* Get physical address */
447 ctx->raddr = (*BATl & 0xF0000000) |
448 ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
449 (virtual & 0x0001F000);
450 if (*BATl & 0x00000001)
451 ctx->prot = PAGE_READ;
452 if (*BATl & 0x00000002)
453 ctx->prot = PAGE_WRITE | PAGE_READ;
454 #if defined (DEBUG_BATS)
456 fprintf(logfile, "BAT %d match: r 0x" PADDRX
458 i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
459 ctx->prot & PAGE_WRITE ? 'W' : '-');
468 #if defined (DEBUG_BATS)
470 fprintf(logfile, "no BAT match for 0x" ADDRX ":\n", virtual);
471 for (i = 0; i < 4; i++) {
474 BEPIu = *BATu & 0xF0000000;
475 BEPIl = *BATu & 0x0FFE0000;
476 bl = (*BATu & 0x00001FFC) << 15;
477 fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
478 " BATl 0x" ADDRX " \n\t"
479 "0x" ADDRX " 0x" ADDRX " 0x" ADDRX "\n",
480 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
481 *BATu, *BATl, BEPIu, BEPIl, bl);
490 /* PTE table lookup */
491 static always_inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h, int rw)
493 target_ulong base, pte0, pte1;
497 ret = -1; /* No entry found */
498 base = ctx->pg_addr[h];
499 for (i = 0; i < 8; i++) {
500 #if defined(TARGET_PPC64)
502 pte0 = ldq_phys(base + (i * 16));
503 pte1 = ldq_phys(base + (i * 16) + 8);
504 r = pte64_check(ctx, pte0, pte1, h, rw);
505 #if defined (DEBUG_MMU)
507 fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
508 " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
509 base + (i * 16), pte0, pte1,
510 (int)(pte0 & 1), h, (int)((pte0 >> 1) & 1),
517 pte0 = ldl_phys(base + (i * 8));
518 pte1 = ldl_phys(base + (i * 8) + 4);
519 r = pte32_check(ctx, pte0, pte1, h, rw);
520 #if defined (DEBUG_MMU)
522 fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
523 " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
524 base + (i * 8), pte0, pte1,
525 (int)(pte0 >> 31), h, (int)((pte0 >> 6) & 1),
532 /* PTE inconsistency */
535 /* Access violation */
545 /* XXX: we should go on looping to check all PTEs consistency
546 * but if we can speed-up the whole thing as the
547 * result would be undefined if PTEs are not consistent.
556 #if defined (DEBUG_MMU)
558 fprintf(logfile, "found PTE at addr 0x" PADDRX " prot=0x%01x "
560 ctx->raddr, ctx->prot, ret);
563 /* Update page flags */
565 if (pte_update_flags(ctx, &pte1, ret, rw) == 1) {
566 #if defined(TARGET_PPC64)
568 stq_phys_notdirty(base + (good * 16) + 8, pte1);
572 stl_phys_notdirty(base + (good * 8) + 4, pte1);
580 static int find_pte32 (mmu_ctx_t *ctx, int h, int rw)
582 return _find_pte(ctx, 0, h, rw);
585 #if defined(TARGET_PPC64)
586 static int find_pte64 (mmu_ctx_t *ctx, int h, int rw)
588 return _find_pte(ctx, 1, h, rw);
592 static always_inline int find_pte (CPUState *env, mmu_ctx_t *ctx,
595 #if defined(TARGET_PPC64)
596 if (env->mmu_model == POWERPC_MMU_64B)
597 return find_pte64(ctx, h, rw);
600 return find_pte32(ctx, h, rw);
603 #if defined(TARGET_PPC64)
604 static inline int slb_is_valid (uint64_t slb64)
606 return slb64 & 0x0000000008000000ULL ? 1 : 0;
609 static inline void slb_invalidate (uint64_t *slb64)
611 *slb64 &= ~0x0000000008000000ULL;
614 static int slb_lookup (CPUPPCState *env, target_ulong eaddr,
615 target_ulong *vsid, target_ulong *page_mask, int *attr)
617 target_phys_addr_t sr_base;
624 sr_base = env->spr[SPR_ASR];
625 #if defined(DEBUG_SLB)
627 fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
628 __func__, eaddr, sr_base);
631 mask = 0x0000000000000000ULL; /* Avoid gcc warning */
632 for (n = 0; n < env->slb_nr; n++) {
633 tmp64 = ldq_phys(sr_base);
634 tmp = ldl_phys(sr_base + 8);
635 #if defined(DEBUG_SLB)
637 fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08"
638 PRIx32 "\n", __func__, n, sr_base, tmp64, tmp);
641 if (slb_is_valid(tmp64)) {
642 /* SLB entry is valid */
643 switch (tmp64 & 0x0000000006000000ULL) {
644 case 0x0000000000000000ULL:
646 mask = 0xFFFFFFFFF0000000ULL;
648 case 0x0000000002000000ULL:
650 mask = 0xFFFF000000000000ULL;
652 case 0x0000000004000000ULL:
653 case 0x0000000006000000ULL:
654 /* Reserved => segment is invalid */
657 if ((eaddr & mask) == (tmp64 & mask)) {
659 *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
672 void ppc_slb_invalidate_all (CPUPPCState *env)
674 target_phys_addr_t sr_base;
676 int n, do_invalidate;
679 sr_base = env->spr[SPR_ASR];
680 for (n = 0; n < env->slb_nr; n++) {
681 tmp64 = ldq_phys(sr_base);
682 if (slb_is_valid(tmp64)) {
683 slb_invalidate(&tmp64);
684 stq_phys(sr_base, tmp64);
685 /* XXX: given the fact that segment size is 256 MB or 1TB,
686 * and we still don't have a tlb_flush_mask(env, n, mask)
687 * in Qemu, we just invalidate all TLBs
697 void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
699 target_phys_addr_t sr_base;
700 target_ulong vsid, page_mask;
705 n = slb_lookup(env, T0, &vsid, &page_mask, &attr);
707 sr_base = env->spr[SPR_ASR];
709 tmp64 = ldq_phys(sr_base);
710 if (slb_is_valid(tmp64)) {
711 slb_invalidate(&tmp64);
712 stq_phys(sr_base, tmp64);
713 /* XXX: given the fact that segment size is 256 MB or 1TB,
714 * and we still don't have a tlb_flush_mask(env, n, mask)
715 * in Qemu, we just invalidate all TLBs
722 target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
724 target_phys_addr_t sr_base;
729 sr_base = env->spr[SPR_ASR];
730 sr_base += 12 * slb_nr;
731 tmp64 = ldq_phys(sr_base);
732 tmp = ldl_phys(sr_base + 8);
733 if (tmp64 & 0x0000000008000000ULL) {
734 /* SLB entry is valid */
735 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
736 rt = tmp >> 8; /* 65:88 => 40:63 */
737 rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
738 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
739 rt |= ((tmp >> 4) & 0xF) << 27;
743 #if defined(DEBUG_SLB)
745 fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
746 ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
753 void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
755 target_phys_addr_t sr_base;
759 sr_base = env->spr[SPR_ASR];
760 sr_base += 12 * slb_nr;
761 /* Copy Rs bits 37:63 to SLB 62:88 */
763 tmp64 = (rs >> 24) & 0x7;
764 /* Copy Rs bits 33:36 to SLB 89:92 */
765 tmp |= ((rs >> 27) & 0xF) << 4;
766 /* Set the valid bit */
769 tmp64 |= (uint32_t)slb_nr << 28;
770 #if defined(DEBUG_SLB)
772 fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64 " %08"
773 PRIx32 "\n", __func__, slb_nr, rs, sr_base, tmp64, tmp);
776 /* Write SLB entry to memory */
777 stq_phys(sr_base, tmp64);
778 stl_phys(sr_base + 8, tmp);
780 #endif /* defined(TARGET_PPC64) */
782 /* Perform segment based translation */
783 static always_inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
785 target_phys_addr_t hash,
786 target_phys_addr_t mask)
788 return (sdr1 & ((target_ulong)(-1ULL) << sdr_sh)) | (hash & mask);
791 static int get_segment (CPUState *env, mmu_ctx_t *ctx,
792 target_ulong eaddr, int rw, int type)
794 target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
795 target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
796 #if defined(TARGET_PPC64)
799 int ds, nx, vsid_sh, sdr_sh;
802 #if defined(TARGET_PPC64)
803 if (env->mmu_model == POWERPC_MMU_64B) {
804 #if defined (DEBUG_MMU)
806 fprintf(logfile, "Check SLBs\n");
809 ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
812 ctx->key = ((attr & 0x40) && msr_pr == 1) ||
813 ((attr & 0x80) && msr_pr == 0) ? 1 : 0;
815 nx = attr & 0x20 ? 1 : 0;
816 vsid_mask = 0x00003FFFFFFFFF80ULL;
821 #endif /* defined(TARGET_PPC64) */
823 sr = env->sr[eaddr >> 28];
824 page_mask = 0x0FFFFFFF;
825 ctx->key = (((sr & 0x20000000) && msr_pr == 1) ||
826 ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
827 ds = sr & 0x80000000 ? 1 : 0;
828 nx = sr & 0x10000000 ? 1 : 0;
829 vsid = sr & 0x00FFFFFF;
830 vsid_mask = 0x01FFFFC0;
834 #if defined (DEBUG_MMU)
836 fprintf(logfile, "Check segment v=0x" ADDRX " %d 0x" ADDRX
837 " nip=0x" ADDRX " lr=0x" ADDRX
838 " ir=%d dr=%d pr=%d %d t=%d\n",
839 eaddr, (int)(eaddr >> 28), sr, env->nip,
840 env->lr, msr_ir, msr_dr, msr_pr, rw, type);
844 #if defined (DEBUG_MMU)
846 fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
847 ctx->key, ds, nx, vsid);
852 /* Check if instruction fetch is allowed, if needed */
853 if (type != ACCESS_CODE || nx == 0) {
854 /* Page address translation */
855 /* Primary table address */
857 pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
858 #if defined(TARGET_PPC64)
859 if (env->mmu_model == POWERPC_MMU_64B) {
860 htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
861 /* XXX: this is false for 1 TB segments */
862 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
866 htab_mask = sdr & 0x000001FF;
867 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
869 mask = (htab_mask << sdr_sh) | sdr_mask;
870 #if defined (DEBUG_MMU)
872 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
873 PADDRX " " ADDRX "\n", sdr, sdr_sh, hash, mask,
877 ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
878 /* Secondary table address */
879 hash = (~hash) & vsid_mask;
880 #if defined (DEBUG_MMU)
882 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
883 PADDRX "\n", sdr, sdr_sh, hash, mask);
886 ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
887 #if defined(TARGET_PPC64)
888 if (env->mmu_model == POWERPC_MMU_64B) {
889 /* Only 5 bits of the page index are used in the AVPN */
890 ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
894 ctx->ptem = (vsid << 7) | (pgidx >> 10);
896 /* Initialize real address with an invalid value */
897 ctx->raddr = (target_ulong)-1;
898 if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
899 env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
900 /* Software TLB search */
901 ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
903 #if defined (DEBUG_MMU)
905 fprintf(logfile, "0 sdr1=0x" PADDRX " vsid=0x%06x "
906 "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX "\n",
907 sdr, (uint32_t)vsid, (uint32_t)pgidx,
908 (uint32_t)hash, ctx->pg_addr[0]);
911 /* Primary table lookup */
912 ret = find_pte(env, ctx, 0, rw);
914 /* Secondary table lookup */
915 #if defined (DEBUG_MMU)
916 if (eaddr != 0xEFFFFFFF && loglevel != 0) {
918 "1 sdr1=0x" PADDRX " vsid=0x%06x api=0x%04x "
919 "hash=0x%05x pg_addr=0x" PADDRX "\n",
920 sdr, (uint32_t)vsid, (uint32_t)pgidx,
921 (uint32_t)hash, ctx->pg_addr[1]);
924 ret2 = find_pte(env, ctx, 1, rw);
929 #if defined (DEBUG_MMU)
931 target_phys_addr_t curaddr;
932 uint32_t a0, a1, a2, a3;
934 "Page table: " PADDRX " len " PADDRX "\n",
936 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
938 a0 = ldl_phys(curaddr);
939 a1 = ldl_phys(curaddr + 4);
940 a2 = ldl_phys(curaddr + 8);
941 a3 = ldl_phys(curaddr + 12);
942 if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
944 PADDRX ": %08x %08x %08x %08x\n",
945 curaddr, a0, a1, a2, a3);
951 #if defined (DEBUG_MMU)
953 fprintf(logfile, "No access allowed\n");
958 #if defined (DEBUG_MMU)
960 fprintf(logfile, "direct store...\n");
962 /* Direct-store segment : absolutely *BUGGY* for now */
965 /* Integer load/store : only access allowed */
968 /* No code fetch is allowed in direct-store areas */
971 /* Floating point load/store */
974 /* lwarx, ldarx or srwcx. */
977 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
978 /* Should make the instruction do no-op.
979 * As it already do no-op, it's quite easy :-)
988 fprintf(logfile, "ERROR: instruction should not need "
989 "address translation\n");
993 if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
1004 /* Generic TLB check function for embedded PowerPC implementations */
1005 static int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
1006 target_phys_addr_t *raddrp,
1007 target_ulong address,
1008 uint32_t pid, int ext, int i)
1012 /* Check valid flag */
1013 if (!(tlb->prot & PAGE_VALID)) {
1015 fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
1018 mask = ~(tlb->size - 1);
1019 #if defined (DEBUG_SOFTWARE_TLB)
1020 if (loglevel != 0) {
1021 fprintf(logfile, "%s: TLB %d address " ADDRX " PID %d <=> "
1022 ADDRX " " ADDRX " %d\n",
1023 __func__, i, address, pid, tlb->EPN, mask, (int)tlb->PID);
1027 if (tlb->PID != 0 && tlb->PID != pid)
1029 /* Check effective address */
1030 if ((address & mask) != tlb->EPN)
1032 *raddrp = (tlb->RPN & mask) | (address & ~mask);
1033 #if (TARGET_PHYS_ADDR_BITS >= 36)
1035 /* Extend the physical address to 36 bits */
1036 *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
1043 /* Generic TLB search function for PowerPC embedded implementations */
1044 int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
1047 target_phys_addr_t raddr;
1050 /* Default return value is no match */
1052 for (i = 0; i < env->nb_tlb; i++) {
1053 tlb = &env->tlb[i].tlbe;
1054 if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
1063 /* Helpers specific to PowerPC 40x implementations */
1064 static void ppc4xx_tlb_invalidate_all (CPUState *env)
1069 for (i = 0; i < env->nb_tlb; i++) {
1070 tlb = &env->tlb[i].tlbe;
1071 tlb->prot &= ~PAGE_VALID;
1076 static void ppc4xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
1079 #if !defined(FLUSH_ALL_TLBS)
1081 target_phys_addr_t raddr;
1082 target_ulong page, end;
1085 for (i = 0; i < env->nb_tlb; i++) {
1086 tlb = &env->tlb[i].tlbe;
1087 if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
1088 end = tlb->EPN + tlb->size;
1089 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1090 tlb_flush_page(env, page);
1091 tlb->prot &= ~PAGE_VALID;
1096 ppc4xx_tlb_invalidate_all(env);
1100 int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1101 target_ulong address, int rw, int access_type)
1104 target_phys_addr_t raddr;
1105 int i, ret, zsel, zpr;
1109 for (i = 0; i < env->nb_tlb; i++) {
1110 tlb = &env->tlb[i].tlbe;
1111 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1112 env->spr[SPR_40x_PID], 0, i) < 0)
1114 zsel = (tlb->attr >> 4) & 0xF;
1115 zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
1116 #if defined (DEBUG_SOFTWARE_TLB)
1117 if (loglevel != 0) {
1118 fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1119 __func__, i, zsel, zpr, rw, tlb->attr);
1122 if (access_type == ACCESS_CODE) {
1123 /* Check execute enable bit */
1127 goto check_exec_perm;
1138 /* Check from TLB entry */
1139 if (!(tlb->prot & PAGE_EXEC)) {
1142 if (tlb->prot & PAGE_WRITE) {
1143 ctx->prot = PAGE_READ | PAGE_WRITE;
1145 ctx->prot = PAGE_READ;
1152 /* All accesses granted */
1153 ctx->prot = PAGE_READ | PAGE_WRITE;
1172 /* Check from TLB entry */
1173 /* Check write protection bit */
1174 if (tlb->prot & PAGE_WRITE) {
1175 ctx->prot = PAGE_READ | PAGE_WRITE;
1178 ctx->prot = PAGE_READ;
1187 /* All accesses granted */
1188 ctx->prot = PAGE_READ | PAGE_WRITE;
1195 #if defined (DEBUG_SOFTWARE_TLB)
1196 if (loglevel != 0) {
1197 fprintf(logfile, "%s: access granted " ADDRX " => " REGX
1198 " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1205 #if defined (DEBUG_SOFTWARE_TLB)
1206 if (loglevel != 0) {
1207 fprintf(logfile, "%s: access refused " ADDRX " => " REGX
1208 " %d %d\n", __func__, address, raddr, ctx->prot,
1216 void store_40x_sler (CPUPPCState *env, uint32_t val)
1218 /* XXX: TO BE FIXED */
1219 if (val != 0x00000000) {
1220 cpu_abort(env, "Little-endian regions are not supported by now\n");
1222 env->spr[SPR_405_SLER] = val;
1225 int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1226 target_ulong address, int rw,
1230 target_phys_addr_t raddr;
1235 for (i = 0; i < env->nb_tlb; i++) {
1236 tlb = &env->tlb[i].tlbe;
1237 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1238 env->spr[SPR_BOOKE_PID], 1, i) < 0)
1241 prot = tlb->prot & 0xF;
1243 prot = (tlb->prot >> 4) & 0xF;
1244 /* Check the address space */
1245 if (access_type == ACCESS_CODE) {
1246 if (msr_ir != (tlb->attr & 1))
1249 if (prot & PAGE_EXEC) {
1255 if (msr_dr != (tlb->attr & 1))
1258 if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1271 static int check_physical (CPUState *env, mmu_ctx_t *ctx,
1272 target_ulong eaddr, int rw)
1277 ctx->prot = PAGE_READ;
1279 switch (env->mmu_model) {
1280 case POWERPC_MMU_32B:
1281 case POWERPC_MMU_SOFT_6xx:
1282 case POWERPC_MMU_SOFT_74xx:
1283 case POWERPC_MMU_601:
1284 case POWERPC_MMU_SOFT_4xx:
1285 case POWERPC_MMU_REAL_4xx:
1286 case POWERPC_MMU_BOOKE:
1287 ctx->prot |= PAGE_WRITE;
1289 #if defined(TARGET_PPC64)
1290 case POWERPC_MMU_64B:
1291 /* Real address are 60 bits long */
1292 ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1293 ctx->prot |= PAGE_WRITE;
1296 case POWERPC_MMU_SOFT_4xx_Z:
1297 if (unlikely(msr_pe != 0)) {
1298 /* 403 family add some particular protections,
1299 * using PBL/PBU registers for accesses with no translation.
1302 /* Check PLB validity */
1303 (env->pb[0] < env->pb[1] &&
1304 /* and address in plb area */
1305 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1306 (env->pb[2] < env->pb[3] &&
1307 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1308 if (in_plb ^ msr_px) {
1309 /* Access in protected area */
1311 /* Access is not allowed */
1315 /* Read-write access is allowed */
1316 ctx->prot |= PAGE_WRITE;
1320 case POWERPC_MMU_BOOKE_FSL:
1322 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1325 cpu_abort(env, "Unknown or invalid MMU model\n");
1332 int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1333 int rw, int access_type, int check_BATs)
1337 if (loglevel != 0) {
1338 fprintf(logfile, "%s\n", __func__);
1341 if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1342 (access_type != ACCESS_CODE && msr_dr == 0)) {
1343 /* No address translation */
1344 ret = check_physical(env, ctx, eaddr, rw);
1347 switch (env->mmu_model) {
1348 case POWERPC_MMU_32B:
1349 case POWERPC_MMU_SOFT_6xx:
1350 case POWERPC_MMU_SOFT_74xx:
1351 /* Try to find a BAT */
1353 ret = get_bat(env, ctx, eaddr, rw, access_type);
1355 #if defined(TARGET_PPC64)
1356 case POWERPC_MMU_64B:
1359 /* We didn't match any BAT entry or don't have BATs */
1360 ret = get_segment(env, ctx, eaddr, rw, access_type);
1363 case POWERPC_MMU_SOFT_4xx:
1364 case POWERPC_MMU_SOFT_4xx_Z:
1365 ret = mmu40x_get_physical_address(env, ctx, eaddr,
1368 case POWERPC_MMU_601:
1370 cpu_abort(env, "601 MMU model not implemented\n");
1372 case POWERPC_MMU_BOOKE:
1373 ret = mmubooke_get_physical_address(env, ctx, eaddr,
1376 case POWERPC_MMU_BOOKE_FSL:
1378 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1380 case POWERPC_MMU_REAL_4xx:
1381 cpu_abort(env, "PowerPC 401 does not do any translation\n");
1384 cpu_abort(env, "Unknown or invalid MMU model\n");
1389 if (loglevel != 0) {
1390 fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1391 __func__, eaddr, ret, ctx->raddr);
1398 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1402 if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT, 1) != 0))
1405 return ctx.raddr & TARGET_PAGE_MASK;
1408 /* Perform address translation */
1409 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1410 int mmu_idx, int is_softmmu)
1419 access_type = ACCESS_CODE;
1422 /* XXX: put correct access by using cpu_restore_state()
1424 access_type = ACCESS_INT;
1425 // access_type = env->access_type;
1427 ret = get_physical_address(env, &ctx, address, rw, access_type, 1);
1429 ret = tlb_set_page(env, address & TARGET_PAGE_MASK,
1430 ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1431 mmu_idx, is_softmmu);
1432 } else if (ret < 0) {
1433 #if defined (DEBUG_MMU)
1435 cpu_dump_state(env, logfile, fprintf, 0);
1437 if (access_type == ACCESS_CODE) {
1440 /* No matches in page tables or TLB */
1441 switch (env->mmu_model) {
1442 case POWERPC_MMU_SOFT_6xx:
1443 env->exception_index = POWERPC_EXCP_IFTLB;
1444 env->error_code = 1 << 18;
1445 env->spr[SPR_IMISS] = address;
1446 env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1448 case POWERPC_MMU_SOFT_74xx:
1449 env->exception_index = POWERPC_EXCP_IFTLB;
1451 case POWERPC_MMU_SOFT_4xx:
1452 case POWERPC_MMU_SOFT_4xx_Z:
1453 env->exception_index = POWERPC_EXCP_ITLB;
1454 env->error_code = 0;
1455 env->spr[SPR_40x_DEAR] = address;
1456 env->spr[SPR_40x_ESR] = 0x00000000;
1458 case POWERPC_MMU_32B:
1459 #if defined(TARGET_PPC64)
1460 case POWERPC_MMU_64B:
1462 env->exception_index = POWERPC_EXCP_ISI;
1463 env->error_code = 0x40000000;
1465 case POWERPC_MMU_601:
1467 cpu_abort(env, "MMU model not implemented\n");
1469 case POWERPC_MMU_BOOKE:
1471 cpu_abort(env, "MMU model not implemented\n");
1473 case POWERPC_MMU_BOOKE_FSL:
1475 cpu_abort(env, "MMU model not implemented\n");
1477 case POWERPC_MMU_REAL_4xx:
1478 cpu_abort(env, "PowerPC 401 should never raise any MMU "
1482 cpu_abort(env, "Unknown or invalid MMU model\n");
1487 /* Access rights violation */
1488 env->exception_index = POWERPC_EXCP_ISI;
1489 env->error_code = 0x08000000;
1492 /* No execute protection violation */
1493 env->exception_index = POWERPC_EXCP_ISI;
1494 env->error_code = 0x10000000;
1497 /* Direct store exception */
1498 /* No code fetch is allowed in direct-store areas */
1499 env->exception_index = POWERPC_EXCP_ISI;
1500 env->error_code = 0x10000000;
1502 #if defined(TARGET_PPC64)
1504 /* No match in segment table */
1505 env->exception_index = POWERPC_EXCP_ISEG;
1506 env->error_code = 0;
1513 /* No matches in page tables or TLB */
1514 switch (env->mmu_model) {
1515 case POWERPC_MMU_SOFT_6xx:
1517 env->exception_index = POWERPC_EXCP_DSTLB;
1518 env->error_code = 1 << 16;
1520 env->exception_index = POWERPC_EXCP_DLTLB;
1521 env->error_code = 0;
1523 env->spr[SPR_DMISS] = address;
1524 env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1526 env->error_code |= ctx.key << 19;
1527 env->spr[SPR_HASH1] = ctx.pg_addr[0];
1528 env->spr[SPR_HASH2] = ctx.pg_addr[1];
1530 case POWERPC_MMU_SOFT_74xx:
1532 env->exception_index = POWERPC_EXCP_DSTLB;
1534 env->exception_index = POWERPC_EXCP_DLTLB;
1537 /* Implement LRU algorithm */
1538 env->error_code = ctx.key << 19;
1539 env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1540 ((env->last_way + 1) & (env->nb_ways - 1));
1541 env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
1543 case POWERPC_MMU_SOFT_4xx:
1544 case POWERPC_MMU_SOFT_4xx_Z:
1545 env->exception_index = POWERPC_EXCP_DTLB;
1546 env->error_code = 0;
1547 env->spr[SPR_40x_DEAR] = address;
1549 env->spr[SPR_40x_ESR] = 0x00800000;
1551 env->spr[SPR_40x_ESR] = 0x00000000;
1553 case POWERPC_MMU_32B:
1554 #if defined(TARGET_PPC64)
1555 case POWERPC_MMU_64B:
1557 env->exception_index = POWERPC_EXCP_DSI;
1558 env->error_code = 0;
1559 env->spr[SPR_DAR] = address;
1561 env->spr[SPR_DSISR] = 0x42000000;
1563 env->spr[SPR_DSISR] = 0x40000000;
1565 case POWERPC_MMU_601:
1567 cpu_abort(env, "MMU model not implemented\n");
1569 case POWERPC_MMU_BOOKE:
1571 cpu_abort(env, "MMU model not implemented\n");
1573 case POWERPC_MMU_BOOKE_FSL:
1575 cpu_abort(env, "MMU model not implemented\n");
1577 case POWERPC_MMU_REAL_4xx:
1578 cpu_abort(env, "PowerPC 401 should never raise any MMU "
1582 cpu_abort(env, "Unknown or invalid MMU model\n");
1587 /* Access rights violation */
1588 env->exception_index = POWERPC_EXCP_DSI;
1589 env->error_code = 0;
1590 env->spr[SPR_DAR] = address;
1592 env->spr[SPR_DSISR] = 0x0A000000;
1594 env->spr[SPR_DSISR] = 0x08000000;
1597 /* Direct store exception */
1598 switch (access_type) {
1600 /* Floating point load/store */
1601 env->exception_index = POWERPC_EXCP_ALIGN;
1602 env->error_code = POWERPC_EXCP_ALIGN_FP;
1603 env->spr[SPR_DAR] = address;
1606 /* lwarx, ldarx or stwcx. */
1607 env->exception_index = POWERPC_EXCP_DSI;
1608 env->error_code = 0;
1609 env->spr[SPR_DAR] = address;
1611 env->spr[SPR_DSISR] = 0x06000000;
1613 env->spr[SPR_DSISR] = 0x04000000;
1616 /* eciwx or ecowx */
1617 env->exception_index = POWERPC_EXCP_DSI;
1618 env->error_code = 0;
1619 env->spr[SPR_DAR] = address;
1621 env->spr[SPR_DSISR] = 0x06100000;
1623 env->spr[SPR_DSISR] = 0x04100000;
1626 printf("DSI: invalid exception (%d)\n", ret);
1627 env->exception_index = POWERPC_EXCP_PROGRAM;
1629 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1630 env->spr[SPR_DAR] = address;
1634 #if defined(TARGET_PPC64)
1636 /* No match in segment table */
1637 env->exception_index = POWERPC_EXCP_DSEG;
1638 env->error_code = 0;
1639 env->spr[SPR_DAR] = address;
1645 printf("%s: set exception to %d %02x\n", __func__,
1646 env->exception, env->error_code);
1654 /*****************************************************************************/
1655 /* BATs management */
1656 #if !defined(FLUSH_ALL_TLBS)
1657 static always_inline void do_invalidate_BAT (CPUPPCState *env,
1661 target_ulong base, end, page;
1663 base = BATu & ~0x0001FFFF;
1664 end = base + mask + 0x00020000;
1665 #if defined (DEBUG_BATS)
1666 if (loglevel != 0) {
1667 fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1671 for (page = base; page != end; page += TARGET_PAGE_SIZE)
1672 tlb_flush_page(env, page);
1673 #if defined (DEBUG_BATS)
1675 fprintf(logfile, "Flush done\n");
1680 static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1681 int ul, int nr, target_ulong value)
1683 #if defined (DEBUG_BATS)
1684 if (loglevel != 0) {
1685 fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1686 ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1691 target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1693 return env->IBAT[0][nr];
1696 target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1698 return env->IBAT[1][nr];
1701 void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1705 dump_store_bat(env, 'I', 0, nr, value);
1706 if (env->IBAT[0][nr] != value) {
1707 mask = (value << 15) & 0x0FFE0000UL;
1708 #if !defined(FLUSH_ALL_TLBS)
1709 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1711 /* When storing valid upper BAT, mask BEPI and BRPN
1712 * and invalidate all TLBs covered by this BAT
1714 mask = (value << 15) & 0x0FFE0000UL;
1715 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1716 (value & ~0x0001FFFFUL & ~mask);
1717 env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1718 (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1719 #if !defined(FLUSH_ALL_TLBS)
1720 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1727 void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1729 dump_store_bat(env, 'I', 1, nr, value);
1730 env->IBAT[1][nr] = value;
1733 target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1735 return env->DBAT[0][nr];
1738 target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1740 return env->DBAT[1][nr];
1743 void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1747 dump_store_bat(env, 'D', 0, nr, value);
1748 if (env->DBAT[0][nr] != value) {
1749 /* When storing valid upper BAT, mask BEPI and BRPN
1750 * and invalidate all TLBs covered by this BAT
1752 mask = (value << 15) & 0x0FFE0000UL;
1753 #if !defined(FLUSH_ALL_TLBS)
1754 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1756 mask = (value << 15) & 0x0FFE0000UL;
1757 env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1758 (value & ~0x0001FFFFUL & ~mask);
1759 env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1760 (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1761 #if !defined(FLUSH_ALL_TLBS)
1762 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1769 void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1771 dump_store_bat(env, 'D', 1, nr, value);
1772 env->DBAT[1][nr] = value;
1775 /*****************************************************************************/
1776 /* TLB management */
1777 void ppc_tlb_invalidate_all (CPUPPCState *env)
1779 switch (env->mmu_model) {
1780 case POWERPC_MMU_SOFT_6xx:
1781 case POWERPC_MMU_SOFT_74xx:
1782 ppc6xx_tlb_invalidate_all(env);
1784 case POWERPC_MMU_SOFT_4xx:
1785 case POWERPC_MMU_SOFT_4xx_Z:
1786 ppc4xx_tlb_invalidate_all(env);
1788 case POWERPC_MMU_REAL_4xx:
1789 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1791 case POWERPC_MMU_BOOKE:
1793 cpu_abort(env, "MMU model not implemented\n");
1795 case POWERPC_MMU_BOOKE_FSL:
1797 cpu_abort(env, "MMU model not implemented\n");
1799 case POWERPC_MMU_601:
1801 cpu_abort(env, "MMU model not implemented\n");
1803 case POWERPC_MMU_32B:
1804 #if defined(TARGET_PPC64)
1805 case POWERPC_MMU_64B:
1806 #endif /* defined(TARGET_PPC64) */
1811 cpu_abort(env, "Unknown MMU model\n");
1816 void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1818 #if !defined(FLUSH_ALL_TLBS)
1819 addr &= TARGET_PAGE_MASK;
1820 switch (env->mmu_model) {
1821 case POWERPC_MMU_SOFT_6xx:
1822 case POWERPC_MMU_SOFT_74xx:
1823 ppc6xx_tlb_invalidate_virt(env, addr, 0);
1824 if (env->id_tlbs == 1)
1825 ppc6xx_tlb_invalidate_virt(env, addr, 1);
1827 case POWERPC_MMU_SOFT_4xx:
1828 case POWERPC_MMU_SOFT_4xx_Z:
1829 ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
1831 case POWERPC_MMU_REAL_4xx:
1832 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1834 case POWERPC_MMU_BOOKE:
1836 cpu_abort(env, "MMU model not implemented\n");
1838 case POWERPC_MMU_BOOKE_FSL:
1840 cpu_abort(env, "MMU model not implemented\n");
1842 case POWERPC_MMU_601:
1844 cpu_abort(env, "MMU model not implemented\n");
1846 case POWERPC_MMU_32B:
1847 /* tlbie invalidate TLBs for all segments */
1848 addr &= ~((target_ulong)-1 << 28);
1849 /* XXX: this case should be optimized,
1850 * giving a mask to tlb_flush_page
1852 tlb_flush_page(env, addr | (0x0 << 28));
1853 tlb_flush_page(env, addr | (0x1 << 28));
1854 tlb_flush_page(env, addr | (0x2 << 28));
1855 tlb_flush_page(env, addr | (0x3 << 28));
1856 tlb_flush_page(env, addr | (0x4 << 28));
1857 tlb_flush_page(env, addr | (0x5 << 28));
1858 tlb_flush_page(env, addr | (0x6 << 28));
1859 tlb_flush_page(env, addr | (0x7 << 28));
1860 tlb_flush_page(env, addr | (0x8 << 28));
1861 tlb_flush_page(env, addr | (0x9 << 28));
1862 tlb_flush_page(env, addr | (0xA << 28));
1863 tlb_flush_page(env, addr | (0xB << 28));
1864 tlb_flush_page(env, addr | (0xC << 28));
1865 tlb_flush_page(env, addr | (0xD << 28));
1866 tlb_flush_page(env, addr | (0xE << 28));
1867 tlb_flush_page(env, addr | (0xF << 28));
1869 #if defined(TARGET_PPC64)
1870 case POWERPC_MMU_64B:
1871 /* tlbie invalidate TLBs for all segments */
1872 /* XXX: given the fact that there are too many segments to invalidate,
1873 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
1874 * we just invalidate all TLBs
1878 #endif /* defined(TARGET_PPC64) */
1881 cpu_abort(env, "Unknown MMU model\n");
1885 ppc_tlb_invalidate_all(env);
1889 /*****************************************************************************/
1890 /* Special registers manipulation */
1891 #if defined(TARGET_PPC64)
1892 target_ulong ppc_load_asr (CPUPPCState *env)
1897 void ppc_store_asr (CPUPPCState *env, target_ulong value)
1899 if (env->asr != value) {
1906 target_ulong do_load_sdr1 (CPUPPCState *env)
1911 void do_store_sdr1 (CPUPPCState *env, target_ulong value)
1913 #if defined (DEBUG_MMU)
1914 if (loglevel != 0) {
1915 fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
1918 if (env->sdr1 != value) {
1919 /* XXX: for PowerPC 64, should check that the HTABSIZE value
1928 target_ulong do_load_sr (CPUPPCState *env, int srnum)
1930 return env->sr[srnum];
1934 void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
1936 #if defined (DEBUG_MMU)
1937 if (loglevel != 0) {
1938 fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
1939 __func__, srnum, value, env->sr[srnum]);
1942 if (env->sr[srnum] != value) {
1943 env->sr[srnum] = value;
1944 #if !defined(FLUSH_ALL_TLBS) && 0
1946 target_ulong page, end;
1947 /* Invalidate 256 MB of virtual memory */
1948 page = (16 << 20) * srnum;
1949 end = page + (16 << 20);
1950 for (; page != end; page += TARGET_PAGE_SIZE)
1951 tlb_flush_page(env, page);
1958 #endif /* !defined (CONFIG_USER_ONLY) */
1960 target_ulong ppc_load_xer (CPUPPCState *env)
1962 return (xer_so << XER_SO) |
1963 (xer_ov << XER_OV) |
1964 (xer_ca << XER_CA) |
1965 (xer_bc << XER_BC) |
1966 (xer_cmp << XER_CMP);
1969 void ppc_store_xer (CPUPPCState *env, target_ulong value)
1971 xer_so = (value >> XER_SO) & 0x01;
1972 xer_ov = (value >> XER_OV) & 0x01;
1973 xer_ca = (value >> XER_CA) & 0x01;
1974 xer_cmp = (value >> XER_CMP) & 0xFF;
1975 xer_bc = (value >> XER_BC) & 0x7F;
1978 /* Swap temporary saved registers with GPRs */
1979 static always_inline void swap_gpr_tgpr (CPUPPCState *env)
1984 env->gpr[0] = env->tgpr[0];
1987 env->gpr[1] = env->tgpr[1];
1990 env->gpr[2] = env->tgpr[2];
1993 env->gpr[3] = env->tgpr[3];
1997 /* GDBstub can read and write MSR... */
1998 target_ulong do_load_msr (CPUPPCState *env)
2001 #if defined (TARGET_PPC64)
2002 ((target_ulong)msr_sf << MSR_SF) |
2003 ((target_ulong)msr_isf << MSR_ISF) |
2004 ((target_ulong)msr_hv << MSR_HV) |
2006 ((target_ulong)msr_ucle << MSR_UCLE) |
2007 ((target_ulong)msr_vr << MSR_VR) | /* VR / SPE */
2008 ((target_ulong)msr_ap << MSR_AP) |
2009 ((target_ulong)msr_sa << MSR_SA) |
2010 ((target_ulong)msr_key << MSR_KEY) |
2011 ((target_ulong)msr_pow << MSR_POW) |
2012 ((target_ulong)msr_tgpr << MSR_TGPR) | /* TGPR / CE */
2013 ((target_ulong)msr_ile << MSR_ILE) |
2014 ((target_ulong)msr_ee << MSR_EE) |
2015 ((target_ulong)msr_pr << MSR_PR) |
2016 ((target_ulong)msr_fp << MSR_FP) |
2017 ((target_ulong)msr_me << MSR_ME) |
2018 ((target_ulong)msr_fe0 << MSR_FE0) |
2019 ((target_ulong)msr_se << MSR_SE) | /* SE / DWE / UBLE */
2020 ((target_ulong)msr_be << MSR_BE) | /* BE / DE */
2021 ((target_ulong)msr_fe1 << MSR_FE1) |
2022 ((target_ulong)msr_al << MSR_AL) |
2023 ((target_ulong)msr_ep << MSR_EP) |
2024 ((target_ulong)msr_ir << MSR_IR) |
2025 ((target_ulong)msr_dr << MSR_DR) |
2026 ((target_ulong)msr_pe << MSR_PE) |
2027 ((target_ulong)msr_px << MSR_PX) | /* PX / PMM */
2028 ((target_ulong)msr_ri << MSR_RI) |
2029 ((target_ulong)msr_le << MSR_LE);
2032 int do_store_msr (CPUPPCState *env, target_ulong value)
2036 value &= env->msr_mask;
2037 if (((value >> MSR_IR) & 1) != msr_ir ||
2038 ((value >> MSR_DR) & 1) != msr_dr) {
2039 /* Flush all tlb when changing translation mode */
2041 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2043 #if !defined (CONFIG_USER_ONLY)
2044 if (unlikely((env->flags & POWERPC_FLAG_TGPR) &&
2045 ((value >> MSR_TGPR) & 1) != msr_tgpr)) {
2046 /* Swap temporary saved registers with GPRs */
2049 if (unlikely((value >> MSR_EP) & 1) != msr_ep) {
2050 /* Change the exception prefix on PowerPC 601 */
2051 env->excp_prefix = ((value >> MSR_EP) & 1) * 0xFFF00000;
2054 #if defined (TARGET_PPC64)
2055 msr_sf = (value >> MSR_SF) & 1;
2056 msr_isf = (value >> MSR_ISF) & 1;
2057 msr_hv = (value >> MSR_HV) & 1;
2059 msr_ucle = (value >> MSR_UCLE) & 1;
2060 msr_vr = (value >> MSR_VR) & 1; /* VR / SPE */
2061 msr_ap = (value >> MSR_AP) & 1;
2062 msr_sa = (value >> MSR_SA) & 1;
2063 msr_key = (value >> MSR_KEY) & 1;
2064 msr_pow = (value >> MSR_POW) & 1;
2065 msr_tgpr = (value >> MSR_TGPR) & 1; /* TGPR / CE */
2066 msr_ile = (value >> MSR_ILE) & 1;
2067 msr_ee = (value >> MSR_EE) & 1;
2068 msr_pr = (value >> MSR_PR) & 1;
2069 msr_fp = (value >> MSR_FP) & 1;
2070 msr_me = (value >> MSR_ME) & 1;
2071 msr_fe0 = (value >> MSR_FE0) & 1;
2072 msr_se = (value >> MSR_SE) & 1; /* SE / DWE / UBLE */
2073 msr_be = (value >> MSR_BE) & 1; /* BE / DE */
2074 msr_fe1 = (value >> MSR_FE1) & 1;
2075 msr_al = (value >> MSR_AL) & 1;
2076 msr_ep = (value >> MSR_EP) & 1;
2077 msr_ir = (value >> MSR_IR) & 1;
2078 msr_dr = (value >> MSR_DR) & 1;
2079 msr_pe = (value >> MSR_PE) & 1;
2080 msr_px = (value >> MSR_PX) & 1; /* PX / PMM */
2081 msr_ri = (value >> MSR_RI) & 1;
2082 msr_le = (value >> MSR_LE) & 1;
2083 do_compute_hflags(env);
2086 switch (env->excp_model) {
2087 case POWERPC_EXCP_603:
2088 case POWERPC_EXCP_603E:
2089 case POWERPC_EXCP_G2:
2090 /* Don't handle SLEEP mode: we should disable all clocks...
2091 * No dynamic power-management.
2093 if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00C00000) != 0)
2096 case POWERPC_EXCP_604:
2100 case POWERPC_EXCP_7x0:
2101 if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00E00000) != 0)
2111 #if defined(TARGET_PPC64)
2112 int ppc_store_msr_32 (CPUPPCState *env, uint32_t value)
2114 return do_store_msr(env, (do_load_msr(env) & ~0xFFFFFFFFULL) |
2115 (value & 0xFFFFFFFF));
2119 void do_compute_hflags (CPUPPCState *env)
2121 /* Compute current hflags */
2122 env->hflags = (msr_vr << MSR_VR) |
2123 (msr_ap << MSR_AP) | (msr_sa << MSR_SA) | (msr_pr << MSR_PR) |
2124 (msr_fp << MSR_FP) | (msr_fe0 << MSR_FE0) | (msr_se << MSR_SE) |
2125 (msr_be << MSR_BE) | (msr_fe1 << MSR_FE1) | (msr_le << MSR_LE);
2126 #if defined (TARGET_PPC64)
2127 env->hflags |= msr_cm << MSR_CM;
2128 env->hflags |= (uint64_t)msr_sf << MSR_SF;
2129 env->hflags |= (uint64_t)msr_hv << MSR_HV;
2130 /* Precompute MMU index */
2131 if (msr_pr == 0 && msr_hv == 1)
2135 env->mmu_idx = 1 - msr_pr;
2138 /*****************************************************************************/
2139 /* Exception processing */
2140 #if defined (CONFIG_USER_ONLY)
2141 void do_interrupt (CPUState *env)
2143 env->exception_index = POWERPC_EXCP_NONE;
2144 env->error_code = 0;
2147 void ppc_hw_interrupt (CPUState *env)
2149 env->exception_index = POWERPC_EXCP_NONE;
2150 env->error_code = 0;
2152 #else /* defined (CONFIG_USER_ONLY) */
2153 static void dump_syscall (CPUState *env)
2155 fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
2156 " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
2157 env->gpr[0], env->gpr[3], env->gpr[4],
2158 env->gpr[5], env->gpr[6], env->nip);
2161 /* Note that this function should be greatly optimized
2162 * when called with a constant excp, from ppc_hw_interrupt
2164 static always_inline void powerpc_excp (CPUState *env,
2165 int excp_model, int excp)
2167 target_ulong msr, vector;
2168 int srr0, srr1, asrr0, asrr1;
2170 if (loglevel & CPU_LOG_INT) {
2171 fprintf(logfile, "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
2172 env->nip, excp, env->error_code);
2174 msr = do_load_msr(env);
2179 msr &= ~((target_ulong)0x783F0000);
2181 case POWERPC_EXCP_NONE:
2182 /* Should never happen */
2184 case POWERPC_EXCP_CRITICAL: /* Critical input */
2185 msr_ri = 0; /* XXX: check this */
2186 switch (excp_model) {
2187 case POWERPC_EXCP_40x:
2188 srr0 = SPR_40x_SRR2;
2189 srr1 = SPR_40x_SRR3;
2191 case POWERPC_EXCP_BOOKE:
2192 srr0 = SPR_BOOKE_CSRR0;
2193 srr1 = SPR_BOOKE_CSRR1;
2195 case POWERPC_EXCP_G2:
2201 case POWERPC_EXCP_MCHECK: /* Machine check exception */
2203 /* Machine check exception is not enabled.
2204 * Enter checkstop state.
2206 if (loglevel != 0) {
2207 fprintf(logfile, "Machine check while not allowed. "
2208 "Entering checkstop state\n");
2210 fprintf(stderr, "Machine check while not allowed. "
2211 "Entering checkstop state\n");
2214 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2218 #if defined(TARGET_PPC64H)
2221 /* XXX: should also have something loaded in DAR / DSISR */
2222 switch (excp_model) {
2223 case POWERPC_EXCP_40x:
2224 srr0 = SPR_40x_SRR2;
2225 srr1 = SPR_40x_SRR3;
2227 case POWERPC_EXCP_BOOKE:
2228 srr0 = SPR_BOOKE_MCSRR0;
2229 srr1 = SPR_BOOKE_MCSRR1;
2230 asrr0 = SPR_BOOKE_CSRR0;
2231 asrr1 = SPR_BOOKE_CSRR1;
2237 case POWERPC_EXCP_DSI: /* Data storage exception */
2238 #if defined (DEBUG_EXCEPTIONS)
2239 if (loglevel != 0) {
2240 fprintf(logfile, "DSI exception: DSISR=0x" ADDRX" DAR=0x" ADDRX
2241 "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
2245 #if defined(TARGET_PPC64H)
2250 case POWERPC_EXCP_ISI: /* Instruction storage exception */
2251 #if defined (DEBUG_EXCEPTIONS)
2252 if (loglevel != 0) {
2253 fprintf(logfile, "ISI exception: msr=0x" ADDRX ", nip=0x" ADDRX
2254 "\n", msr, env->nip);
2258 #if defined(TARGET_PPC64H)
2262 msr |= env->error_code;
2264 case POWERPC_EXCP_EXTERNAL: /* External input */
2266 #if defined(TARGET_PPC64H)
2271 case POWERPC_EXCP_ALIGN: /* Alignment exception */
2273 #if defined(TARGET_PPC64H)
2277 /* XXX: this is false */
2278 /* Get rS/rD and rA from faulting opcode */
2279 env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
2281 case POWERPC_EXCP_PROGRAM: /* Program exception */
2282 switch (env->error_code & ~0xF) {
2283 case POWERPC_EXCP_FP:
2284 if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
2285 #if defined (DEBUG_EXCEPTIONS)
2286 if (loglevel != 0) {
2287 fprintf(logfile, "Ignore floating point exception\n");
2293 #if defined(TARGET_PPC64H)
2299 env->fpscr[7] |= 0x8;
2300 /* Finally, update FEX */
2301 if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
2302 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
2303 env->fpscr[7] |= 0x4;
2304 if (msr_fe0 != msr_fe1) {
2309 case POWERPC_EXCP_INVAL:
2310 #if defined (DEBUG_EXCEPTIONS)
2311 if (loglevel != 0) {
2312 fprintf(logfile, "Invalid instruction at 0x" ADDRX "\n",
2317 #if defined(TARGET_PPC64H)
2323 case POWERPC_EXCP_PRIV:
2325 #if defined(TARGET_PPC64H)
2331 case POWERPC_EXCP_TRAP:
2333 #if defined(TARGET_PPC64H)
2340 /* Should never occur */
2341 cpu_abort(env, "Invalid program exception %d. Aborting\n",
2346 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
2348 #if defined(TARGET_PPC64H)
2353 case POWERPC_EXCP_SYSCALL: /* System call exception */
2354 /* NOTE: this is a temporary hack to support graphics OSI
2355 calls from the MOL driver */
2356 /* XXX: To be removed */
2357 if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2359 if (env->osi_call(env) != 0)
2362 if (loglevel & CPU_LOG_INT) {
2366 #if defined(TARGET_PPC64H)
2367 if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
2371 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
2374 case POWERPC_EXCP_DECR: /* Decrementer exception */
2376 #if defined(TARGET_PPC64H)
2381 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
2383 #if defined (DEBUG_EXCEPTIONS)
2385 fprintf(logfile, "FIT exception\n");
2387 msr_ri = 0; /* XXX: check this */
2389 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
2390 #if defined (DEBUG_EXCEPTIONS)
2392 fprintf(logfile, "WDT exception\n");
2394 switch (excp_model) {
2395 case POWERPC_EXCP_BOOKE:
2396 srr0 = SPR_BOOKE_CSRR0;
2397 srr1 = SPR_BOOKE_CSRR1;
2402 msr_ri = 0; /* XXX: check this */
2404 case POWERPC_EXCP_DTLB: /* Data TLB error */
2405 msr_ri = 0; /* XXX: check this */
2407 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
2408 msr_ri = 0; /* XXX: check this */
2410 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
2411 switch (excp_model) {
2412 case POWERPC_EXCP_BOOKE:
2413 srr0 = SPR_BOOKE_DSRR0;
2414 srr1 = SPR_BOOKE_DSRR1;
2415 asrr0 = SPR_BOOKE_CSRR0;
2416 asrr1 = SPR_BOOKE_CSRR1;
2422 cpu_abort(env, "Debug exception is not implemented yet !\n");
2424 #if defined(TARGET_PPCEMB)
2425 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable */
2426 msr_ri = 0; /* XXX: check this */
2428 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */
2430 cpu_abort(env, "Embedded floating point data exception "
2431 "is not implemented yet !\n");
2433 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */
2435 cpu_abort(env, "Embedded floating point round exception "
2436 "is not implemented yet !\n");
2438 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor interrupt */
2442 "Performance counter exception is not implemented yet !\n");
2444 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
2447 "Embedded doorbell interrupt is not implemented yet !\n");
2449 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
2450 switch (excp_model) {
2451 case POWERPC_EXCP_BOOKE:
2452 srr0 = SPR_BOOKE_CSRR0;
2453 srr1 = SPR_BOOKE_CSRR1;
2459 cpu_abort(env, "Embedded doorbell critical interrupt "
2460 "is not implemented yet !\n");
2462 #endif /* defined(TARGET_PPCEMB) */
2463 case POWERPC_EXCP_RESET: /* System reset exception */
2465 #if defined(TARGET_PPC64H)
2469 #if defined(TARGET_PPC64)
2470 case POWERPC_EXCP_DSEG: /* Data segment exception */
2472 #if defined(TARGET_PPC64H)
2477 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
2479 #if defined(TARGET_PPC64H)
2484 #endif /* defined(TARGET_PPC64) */
2485 #if defined(TARGET_PPC64H)
2486 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
2492 case POWERPC_EXCP_TRACE: /* Trace exception */
2494 #if defined(TARGET_PPC64H)
2499 #if defined(TARGET_PPC64H)
2500 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
2505 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */
2510 cpu_abort(env, "Hypervisor instruction storage exception "
2511 "is not implemented yet !\n");
2513 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
2518 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment exception */
2523 #endif /* defined(TARGET_PPC64H) */
2524 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
2526 #if defined(TARGET_PPC64H)
2531 case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */
2532 #if defined (DEBUG_EXCEPTIONS)
2534 fprintf(logfile, "PIT exception\n");
2536 msr_ri = 0; /* XXX: check this */
2538 case POWERPC_EXCP_IO: /* IO error exception */
2540 cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2542 case POWERPC_EXCP_RUNM: /* Run mode exception */
2544 cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2546 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
2548 cpu_abort(env, "602 emulation trap exception "
2549 "is not implemented yet !\n");
2551 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
2552 msr_ri = 0; /* XXX: check this */
2553 #if defined(TARGET_PPC64H) /* XXX: check this */
2557 switch (excp_model) {
2558 case POWERPC_EXCP_602:
2559 case POWERPC_EXCP_603:
2560 case POWERPC_EXCP_603E:
2561 case POWERPC_EXCP_G2:
2563 case POWERPC_EXCP_7x5:
2565 case POWERPC_EXCP_74xx:
2568 cpu_abort(env, "Invalid instruction TLB miss exception\n");
2572 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
2573 msr_ri = 0; /* XXX: check this */
2574 #if defined(TARGET_PPC64H) /* XXX: check this */
2578 switch (excp_model) {
2579 case POWERPC_EXCP_602:
2580 case POWERPC_EXCP_603:
2581 case POWERPC_EXCP_603E:
2582 case POWERPC_EXCP_G2:
2584 case POWERPC_EXCP_7x5:
2586 case POWERPC_EXCP_74xx:
2589 cpu_abort(env, "Invalid data load TLB miss exception\n");
2593 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
2594 msr_ri = 0; /* XXX: check this */
2595 #if defined(TARGET_PPC64H) /* XXX: check this */
2599 switch (excp_model) {
2600 case POWERPC_EXCP_602:
2601 case POWERPC_EXCP_603:
2602 case POWERPC_EXCP_603E:
2603 case POWERPC_EXCP_G2:
2605 /* Swap temporary saved registers with GPRs */
2609 case POWERPC_EXCP_7x5:
2611 #if defined (DEBUG_SOFTWARE_TLB)
2612 if (loglevel != 0) {
2613 const unsigned char *es;
2614 target_ulong *miss, *cmp;
2616 if (excp == POWERPC_EXCP_IFTLB) {
2619 miss = &env->spr[SPR_IMISS];
2620 cmp = &env->spr[SPR_ICMP];
2622 if (excp == POWERPC_EXCP_DLTLB)
2627 miss = &env->spr[SPR_DMISS];
2628 cmp = &env->spr[SPR_DCMP];
2630 fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2631 " H1 " ADDRX " H2 " ADDRX " %08x\n",
2632 es, en, *miss, en, *cmp,
2633 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2637 msr |= env->crf[0] << 28;
2638 msr |= env->error_code; /* key, D/I, S/L bits */
2639 /* Set way using a LRU mechanism */
2640 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2642 case POWERPC_EXCP_74xx:
2644 #if defined (DEBUG_SOFTWARE_TLB)
2645 if (loglevel != 0) {
2646 const unsigned char *es;
2647 target_ulong *miss, *cmp;
2649 if (excp == POWERPC_EXCP_IFTLB) {
2652 miss = &env->spr[SPR_IMISS];
2653 cmp = &env->spr[SPR_ICMP];
2655 if (excp == POWERPC_EXCP_DLTLB)
2660 miss = &env->spr[SPR_TLBMISS];
2661 cmp = &env->spr[SPR_PTEHI];
2663 fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2665 es, en, *miss, en, *cmp, env->error_code);
2668 msr |= env->error_code; /* key bit */
2671 cpu_abort(env, "Invalid data store TLB miss exception\n");
2675 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
2677 cpu_abort(env, "Floating point assist exception "
2678 "is not implemented yet !\n");
2680 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
2682 cpu_abort(env, "IABR exception is not implemented yet !\n");
2684 case POWERPC_EXCP_SMI: /* System management interrupt */
2686 cpu_abort(env, "SMI exception is not implemented yet !\n");
2688 case POWERPC_EXCP_THERM: /* Thermal interrupt */
2690 cpu_abort(env, "Thermal management exception "
2691 "is not implemented yet !\n");
2693 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
2695 #if defined(TARGET_PPC64H)
2701 "Performance counter exception is not implemented yet !\n");
2703 case POWERPC_EXCP_VPUA: /* Vector assist exception */
2705 cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2707 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
2710 "970 soft-patch exception is not implemented yet !\n");
2712 case POWERPC_EXCP_MAINT: /* Maintenance exception */
2715 "970 maintenance exception is not implemented yet !\n");
2719 cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2722 /* save current instruction location */
2723 env->spr[srr0] = env->nip - 4;
2726 /* save next instruction location */
2727 env->spr[srr0] = env->nip;
2731 env->spr[srr1] = msr;
2732 /* If any alternate SRR register are defined, duplicate saved values */
2734 env->spr[asrr0] = env->spr[srr0];
2736 env->spr[asrr1] = env->spr[srr1];
2737 /* If we disactivated any translation, flush TLBs */
2738 if (msr_ir || msr_dr)
2740 /* reload MSR with correct bits */
2750 #if 0 /* Fix this: not on all targets */
2754 do_compute_hflags(env);
2755 /* Jump to handler */
2756 vector = env->excp_vectors[excp];
2757 if (vector == (target_ulong)-1) {
2758 cpu_abort(env, "Raised an exception without defined vector %d\n",
2761 vector |= env->excp_prefix;
2762 #if defined(TARGET_PPC64)
2763 if (excp_model == POWERPC_EXCP_BOOKE) {
2766 vector = (uint32_t)vector;
2770 vector = (uint32_t)vector;
2774 /* Reset exception state */
2775 env->exception_index = POWERPC_EXCP_NONE;
2776 env->error_code = 0;
2779 void do_interrupt (CPUState *env)
2781 powerpc_excp(env, env->excp_model, env->exception_index);
2784 void ppc_hw_interrupt (CPUPPCState *env)
2787 if (loglevel & CPU_LOG_INT) {
2788 fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2789 __func__, env, env->pending_interrupts,
2790 env->interrupt_request, msr_me, msr_ee);
2793 /* External reset */
2794 if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2795 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2796 powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2799 /* Machine check exception */
2800 if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2801 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2802 powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2806 /* External debug exception */
2807 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2808 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2809 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2813 #if defined(TARGET_PPC64H)
2814 if ((msr_ee != 0 || msr_hv == 0 || msr_pr == 1) & hdice != 0) {
2815 /* Hypervisor decrementer exception */
2816 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2817 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2818 powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2824 /* External critical interrupt */
2825 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2826 /* Taking a critical external interrupt does not clear the external
2827 * critical interrupt status
2830 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2832 powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2837 /* Watchdog timer on embedded PowerPC */
2838 if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2839 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2840 powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2843 #if defined(TARGET_PPCEMB)
2844 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2845 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2846 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2850 #if defined(TARGET_PPCEMB)
2851 /* External interrupt */
2852 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2853 /* Taking an external interrupt does not clear the external
2857 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2859 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2863 /* Fixed interval timer on embedded PowerPC */
2864 if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2865 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2866 powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2869 /* Programmable interval timer on embedded PowerPC */
2870 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2871 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2872 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2875 /* Decrementer exception */
2876 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2877 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2878 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2881 #if !defined(TARGET_PPCEMB)
2882 /* External interrupt */
2883 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2884 /* Taking an external interrupt does not clear the external
2888 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2890 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2894 #if defined(TARGET_PPCEMB)
2895 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2896 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2897 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2901 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2902 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2903 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2906 /* Thermal interrupt */
2907 if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2908 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2909 powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2914 #endif /* !CONFIG_USER_ONLY */
2916 void cpu_dump_EA (target_ulong EA)
2926 fprintf(f, "Memory access at address " ADDRX "\n", EA);
2929 void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2939 fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2943 void cpu_ppc_reset (void *opaque)
2949 /* XXX: some of those flags initialisation values could depend
2950 * on the actual PowerPC implementation
2952 for (i = 0; i < 63; i++)
2954 #if defined(TARGET_PPC64)
2955 msr_hv = 0; /* Should be 1... */
2957 msr_ap = 0; /* TO BE CHECKED */
2958 msr_sa = 0; /* TO BE CHECKED */
2960 #if defined (DO_SINGLE_STEP) && 0
2961 /* Single step trace mode */
2965 #if defined(CONFIG_USER_ONLY)
2966 msr_fp = 1; /* Allow floating point exceptions */
2969 env->nip = env->hreset_vector | env->excp_prefix;
2970 if (env->mmu_model != POWERPC_MMU_REAL_4xx)
2971 ppc_tlb_invalidate_all(env);
2973 do_compute_hflags(env);
2975 /* Be sure no exception or interrupt is pending */
2976 env->pending_interrupts = 0;
2977 env->exception_index = POWERPC_EXCP_NONE;
2978 env->error_code = 0;
2979 /* Flush all TLBs */
2983 CPUPPCState *cpu_ppc_init (void)
2987 env = qemu_mallocz(sizeof(CPUPPCState));
2995 void cpu_ppc_close (CPUPPCState *env)
2997 /* Should also remove all opcode tables... */