2 * PowerPC emulation helpers for qemu.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 //#define DEBUG_SOFTWARE_TLB
34 //#define DEBUG_EXCEPTIONS
35 //#define FLUSH_ALL_TLBS
37 /*****************************************************************************/
38 /* PowerPC MMU emulation */
40 #if defined(CONFIG_USER_ONLY)
41 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
42 int is_user, int is_softmmu)
44 int exception, error_code;
47 exception = POWERPC_EXCP_ISI;
48 error_code = 0x40000000;
50 exception = POWERPC_EXCP_DSI;
51 error_code = 0x40000000;
53 error_code |= 0x02000000;
54 env->spr[SPR_DAR] = address;
55 env->spr[SPR_DSISR] = error_code;
57 env->exception_index = exception;
58 env->error_code = error_code;
63 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
69 /* Common routines used by software and hardware TLBs emulation */
70 static 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 int slb_lookup (CPUPPCState *env, target_ulong eaddr,
605 target_ulong *vsid, target_ulong *page_mask, int *attr)
607 target_phys_addr_t sr_base;
615 sr_base = env->spr[SPR_ASR];
616 #if defined(DEBUG_SLB)
618 fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
619 __func__, eaddr, sr_base);
622 mask = 0x0000000000000000ULL; /* Avoid gcc warning */
623 slb_nr = env->slb_nr;
624 for (n = 0; n < slb_nr; n++) {
625 tmp64 = ldq_phys(sr_base);
626 tmp = ldl_phys(sr_base + 8);
627 #if defined(DEBUG_SLB)
629 fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08"
630 PRIx32 "\n", __func__, n, sr_base, tmp64, tmp);
633 if (tmp64 & 0x0000000008000000ULL) {
634 /* SLB entry is valid */
635 switch (tmp64 & 0x0000000006000000ULL) {
636 case 0x0000000000000000ULL:
638 mask = 0xFFFFFFFFF0000000ULL;
640 case 0x0000000002000000ULL:
642 mask = 0xFFFF000000000000ULL;
644 case 0x0000000004000000ULL:
645 case 0x0000000006000000ULL:
646 /* Reserved => segment is invalid */
649 if ((eaddr & mask) == (tmp64 & mask)) {
651 *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
664 target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
666 target_phys_addr_t sr_base;
671 sr_base = env->spr[SPR_ASR];
672 sr_base += 12 * slb_nr;
673 tmp64 = ldq_phys(sr_base);
674 tmp = ldl_phys(sr_base + 8);
675 if (tmp64 & 0x0000000008000000ULL) {
676 /* SLB entry is valid */
677 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
678 rt = tmp >> 8; /* 65:88 => 40:63 */
679 rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
680 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
681 rt |= ((tmp >> 4) & 0xF) << 27;
685 #if defined(DEBUG_SLB)
687 fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
688 ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
695 void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
697 target_phys_addr_t sr_base;
701 sr_base = env->spr[SPR_ASR];
702 sr_base += 12 * slb_nr;
703 /* Copy Rs bits 37:63 to SLB 62:88 */
705 tmp64 = (rs >> 24) & 0x7;
706 /* Copy Rs bits 33:36 to SLB 89:92 */
707 tmp |= ((rs >> 27) & 0xF) << 4;
708 /* Set the valid bit */
711 tmp64 |= (uint32_t)slb_nr << 28;
712 #if defined(DEBUG_SLB)
714 fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64 " %08"
715 PRIx32 "\n", __func__, slb_nr, rs, sr_base, tmp64, tmp);
718 /* Write SLB entry to memory */
719 stq_phys(sr_base, tmp64);
720 stl_phys(sr_base + 8, tmp);
722 #endif /* defined(TARGET_PPC64) */
724 /* Perform segment based translation */
725 static always_inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
727 target_phys_addr_t hash,
728 target_phys_addr_t mask)
730 return (sdr1 & ((target_ulong)(-1ULL) << sdr_sh)) | (hash & mask);
733 static int get_segment (CPUState *env, mmu_ctx_t *ctx,
734 target_ulong eaddr, int rw, int type)
736 target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
737 target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
738 #if defined(TARGET_PPC64)
741 int ds, nx, vsid_sh, sdr_sh;
744 #if defined(TARGET_PPC64)
745 if (env->mmu_model == POWERPC_MMU_64B) {
746 #if defined (DEBUG_MMU)
748 fprintf(logfile, "Check SLBs\n");
751 ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
754 ctx->key = ((attr & 0x40) && msr_pr == 1) ||
755 ((attr & 0x80) && msr_pr == 0) ? 1 : 0;
757 nx = attr & 0x20 ? 1 : 0;
758 vsid_mask = 0x00003FFFFFFFFF80ULL;
763 #endif /* defined(TARGET_PPC64) */
765 sr = env->sr[eaddr >> 28];
766 page_mask = 0x0FFFFFFF;
767 ctx->key = (((sr & 0x20000000) && msr_pr == 1) ||
768 ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
769 ds = sr & 0x80000000 ? 1 : 0;
770 nx = sr & 0x10000000 ? 1 : 0;
771 vsid = sr & 0x00FFFFFF;
772 vsid_mask = 0x01FFFFC0;
776 #if defined (DEBUG_MMU)
778 fprintf(logfile, "Check segment v=0x" ADDRX " %d 0x" ADDRX
779 " nip=0x" ADDRX " lr=0x" ADDRX
780 " ir=%d dr=%d pr=%d %d t=%d\n",
781 eaddr, (int)(eaddr >> 28), sr, env->nip,
782 env->lr, msr_ir, msr_dr, msr_pr, rw, type);
786 #if defined (DEBUG_MMU)
788 fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
789 ctx->key, ds, nx, vsid);
794 /* Check if instruction fetch is allowed, if needed */
795 if (type != ACCESS_CODE || nx == 0) {
796 /* Page address translation */
797 /* Primary table address */
799 pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
800 #if defined(TARGET_PPC64)
801 if (env->mmu_model == POWERPC_MMU_64B) {
802 htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
803 /* XXX: this is false for 1 TB segments */
804 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
808 htab_mask = sdr & 0x000001FF;
809 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
811 mask = (htab_mask << sdr_sh) | sdr_mask;
812 #if defined (DEBUG_MMU)
814 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
815 PADDRX " " ADDRX "\n", sdr, sdr_sh, hash, mask,
819 ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
820 /* Secondary table address */
821 hash = (~hash) & vsid_mask;
822 #if defined (DEBUG_MMU)
824 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
825 PADDRX "\n", sdr, sdr_sh, hash, mask);
828 ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
829 #if defined(TARGET_PPC64)
830 if (env->mmu_model == POWERPC_MMU_64B) {
831 /* Only 5 bits of the page index are used in the AVPN */
832 ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
836 ctx->ptem = (vsid << 7) | (pgidx >> 10);
838 /* Initialize real address with an invalid value */
839 ctx->raddr = (target_ulong)-1;
840 if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
841 env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
842 /* Software TLB search */
843 ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
845 #if defined (DEBUG_MMU)
847 fprintf(logfile, "0 sdr1=0x" PADDRX " vsid=0x%06x "
848 "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX "\n",
849 sdr, (uint32_t)vsid, (uint32_t)pgidx,
850 (uint32_t)hash, ctx->pg_addr[0]);
853 /* Primary table lookup */
854 ret = find_pte(env, ctx, 0, rw);
856 /* Secondary table lookup */
857 #if defined (DEBUG_MMU)
858 if (eaddr != 0xEFFFFFFF && loglevel != 0) {
860 "1 sdr1=0x" PADDRX " vsid=0x%06x api=0x%04x "
861 "hash=0x%05x pg_addr=0x" PADDRX "\n",
862 sdr, (uint32_t)vsid, (uint32_t)pgidx,
863 (uint32_t)hash, ctx->pg_addr[1]);
866 ret2 = find_pte(env, ctx, 1, rw);
871 #if defined (DEBUG_MMU)
873 target_phys_addr_t curaddr;
874 uint32_t a0, a1, a2, a3;
876 "Page table: " PADDRX " len " PADDRX "\n",
878 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
880 a0 = ldl_phys(curaddr);
881 a1 = ldl_phys(curaddr + 4);
882 a2 = ldl_phys(curaddr + 8);
883 a3 = ldl_phys(curaddr + 12);
884 if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
886 PADDRX ": %08x %08x %08x %08x\n",
887 curaddr, a0, a1, a2, a3);
893 #if defined (DEBUG_MMU)
895 fprintf(logfile, "No access allowed\n");
900 #if defined (DEBUG_MMU)
902 fprintf(logfile, "direct store...\n");
904 /* Direct-store segment : absolutely *BUGGY* for now */
907 /* Integer load/store : only access allowed */
910 /* No code fetch is allowed in direct-store areas */
913 /* Floating point load/store */
916 /* lwarx, ldarx or srwcx. */
919 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
920 /* Should make the instruction do no-op.
921 * As it already do no-op, it's quite easy :-)
930 fprintf(logfile, "ERROR: instruction should not need "
931 "address translation\n");
935 if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
946 /* Generic TLB check function for embedded PowerPC implementations */
947 static int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
948 target_phys_addr_t *raddrp,
949 target_ulong address,
950 uint32_t pid, int ext, int i)
954 /* Check valid flag */
955 if (!(tlb->prot & PAGE_VALID)) {
957 fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
960 mask = ~(tlb->size - 1);
961 #if defined (DEBUG_SOFTWARE_TLB)
963 fprintf(logfile, "%s: TLB %d address " ADDRX " PID %d <=> "
964 ADDRX " " ADDRX " %d\n",
965 __func__, i, address, pid, tlb->EPN, mask, (int)tlb->PID);
969 if (tlb->PID != 0 && tlb->PID != pid)
971 /* Check effective address */
972 if ((address & mask) != tlb->EPN)
974 *raddrp = (tlb->RPN & mask) | (address & ~mask);
975 #if (TARGET_PHYS_ADDR_BITS >= 36)
977 /* Extend the physical address to 36 bits */
978 *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
985 /* Generic TLB search function for PowerPC embedded implementations */
986 int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
989 target_phys_addr_t raddr;
992 /* Default return value is no match */
994 for (i = 0; i < env->nb_tlb; i++) {
995 tlb = &env->tlb[i].tlbe;
996 if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
1005 /* Helpers specific to PowerPC 40x implementations */
1006 static void ppc4xx_tlb_invalidate_all (CPUState *env)
1011 for (i = 0; i < env->nb_tlb; i++) {
1012 tlb = &env->tlb[i].tlbe;
1013 tlb->prot &= ~PAGE_VALID;
1018 static void ppc4xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
1021 #if !defined(FLUSH_ALL_TLBS)
1023 target_phys_addr_t raddr;
1024 target_ulong page, end;
1027 for (i = 0; i < env->nb_tlb; i++) {
1028 tlb = &env->tlb[i].tlbe;
1029 if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
1030 end = tlb->EPN + tlb->size;
1031 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1032 tlb_flush_page(env, page);
1033 tlb->prot &= ~PAGE_VALID;
1038 ppc4xx_tlb_invalidate_all(env);
1042 int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1043 target_ulong address, int rw, int access_type)
1046 target_phys_addr_t raddr;
1047 int i, ret, zsel, zpr;
1051 for (i = 0; i < env->nb_tlb; i++) {
1052 tlb = &env->tlb[i].tlbe;
1053 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1054 env->spr[SPR_40x_PID], 0, i) < 0)
1056 zsel = (tlb->attr >> 4) & 0xF;
1057 zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
1058 #if defined (DEBUG_SOFTWARE_TLB)
1059 if (loglevel != 0) {
1060 fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1061 __func__, i, zsel, zpr, rw, tlb->attr);
1064 if (access_type == ACCESS_CODE) {
1065 /* Check execute enable bit */
1069 goto check_exec_perm;
1080 /* Check from TLB entry */
1081 if (!(tlb->prot & PAGE_EXEC)) {
1084 if (tlb->prot & PAGE_WRITE) {
1085 ctx->prot = PAGE_READ | PAGE_WRITE;
1087 ctx->prot = PAGE_READ;
1094 /* All accesses granted */
1095 ctx->prot = PAGE_READ | PAGE_WRITE;
1114 /* Check from TLB entry */
1115 /* Check write protection bit */
1116 if (tlb->prot & PAGE_WRITE) {
1117 ctx->prot = PAGE_READ | PAGE_WRITE;
1120 ctx->prot = PAGE_READ;
1129 /* All accesses granted */
1130 ctx->prot = PAGE_READ | PAGE_WRITE;
1137 #if defined (DEBUG_SOFTWARE_TLB)
1138 if (loglevel != 0) {
1139 fprintf(logfile, "%s: access granted " ADDRX " => " REGX
1140 " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1147 #if defined (DEBUG_SOFTWARE_TLB)
1148 if (loglevel != 0) {
1149 fprintf(logfile, "%s: access refused " ADDRX " => " REGX
1150 " %d %d\n", __func__, address, raddr, ctx->prot,
1158 void store_40x_sler (CPUPPCState *env, uint32_t val)
1160 /* XXX: TO BE FIXED */
1161 if (val != 0x00000000) {
1162 cpu_abort(env, "Little-endian regions are not supported by now\n");
1164 env->spr[SPR_405_SLER] = val;
1167 int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1168 target_ulong address, int rw,
1172 target_phys_addr_t raddr;
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_BOOKE_PID], 1, i) < 0)
1183 prot = tlb->prot & 0xF;
1185 prot = (tlb->prot >> 4) & 0xF;
1186 /* Check the address space */
1187 if (access_type == ACCESS_CODE) {
1188 if (msr_ir != (tlb->attr & 1))
1191 if (prot & PAGE_EXEC) {
1197 if (msr_dr != (tlb->attr & 1))
1200 if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1213 static int check_physical (CPUState *env, mmu_ctx_t *ctx,
1214 target_ulong eaddr, int rw)
1219 ctx->prot = PAGE_READ;
1221 switch (env->mmu_model) {
1222 case POWERPC_MMU_32B:
1223 case POWERPC_MMU_SOFT_6xx:
1224 case POWERPC_MMU_SOFT_74xx:
1225 case POWERPC_MMU_601:
1226 case POWERPC_MMU_SOFT_4xx:
1227 case POWERPC_MMU_REAL_4xx:
1228 case POWERPC_MMU_BOOKE:
1229 ctx->prot |= PAGE_WRITE;
1231 #if defined(TARGET_PPC64)
1232 case POWERPC_MMU_64B:
1233 /* Real address are 60 bits long */
1234 ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1235 ctx->prot |= PAGE_WRITE;
1238 case POWERPC_MMU_SOFT_4xx_Z:
1239 if (unlikely(msr_pe != 0)) {
1240 /* 403 family add some particular protections,
1241 * using PBL/PBU registers for accesses with no translation.
1244 /* Check PLB validity */
1245 (env->pb[0] < env->pb[1] &&
1246 /* and address in plb area */
1247 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1248 (env->pb[2] < env->pb[3] &&
1249 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1250 if (in_plb ^ msr_px) {
1251 /* Access in protected area */
1253 /* Access is not allowed */
1257 /* Read-write access is allowed */
1258 ctx->prot |= PAGE_WRITE;
1262 case POWERPC_MMU_BOOKE_FSL:
1264 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1267 cpu_abort(env, "Unknown or invalid MMU model\n");
1274 int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1275 int rw, int access_type, int check_BATs)
1279 if (loglevel != 0) {
1280 fprintf(logfile, "%s\n", __func__);
1283 if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1284 (access_type != ACCESS_CODE && msr_dr == 0)) {
1285 /* No address translation */
1286 ret = check_physical(env, ctx, eaddr, rw);
1289 switch (env->mmu_model) {
1290 case POWERPC_MMU_32B:
1291 case POWERPC_MMU_SOFT_6xx:
1292 case POWERPC_MMU_SOFT_74xx:
1293 /* Try to find a BAT */
1295 ret = get_bat(env, ctx, eaddr, rw, access_type);
1297 #if defined(TARGET_PPC64)
1298 case POWERPC_MMU_64B:
1301 /* We didn't match any BAT entry or don't have BATs */
1302 ret = get_segment(env, ctx, eaddr, rw, access_type);
1305 case POWERPC_MMU_SOFT_4xx:
1306 case POWERPC_MMU_SOFT_4xx_Z:
1307 ret = mmu40x_get_physical_address(env, ctx, eaddr,
1310 case POWERPC_MMU_601:
1312 cpu_abort(env, "601 MMU model not implemented\n");
1314 case POWERPC_MMU_BOOKE:
1315 ret = mmubooke_get_physical_address(env, ctx, eaddr,
1318 case POWERPC_MMU_BOOKE_FSL:
1320 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1322 case POWERPC_MMU_REAL_4xx:
1323 cpu_abort(env, "PowerPC 401 does not do any translation\n");
1326 cpu_abort(env, "Unknown or invalid MMU model\n");
1331 if (loglevel != 0) {
1332 fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1333 __func__, eaddr, ret, ctx->raddr);
1340 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1344 if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT, 1) != 0))
1347 return ctx.raddr & TARGET_PAGE_MASK;
1350 /* Perform address translation */
1351 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1352 int is_user, int is_softmmu)
1361 access_type = ACCESS_CODE;
1364 /* XXX: put correct access by using cpu_restore_state()
1366 access_type = ACCESS_INT;
1367 // access_type = env->access_type;
1369 ret = get_physical_address(env, &ctx, address, rw, access_type, 1);
1371 ret = tlb_set_page(env, address & TARGET_PAGE_MASK,
1372 ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1373 is_user, is_softmmu);
1374 } else if (ret < 0) {
1375 #if defined (DEBUG_MMU)
1377 cpu_dump_state(env, logfile, fprintf, 0);
1379 if (access_type == ACCESS_CODE) {
1382 /* No matches in page tables or TLB */
1383 switch (env->mmu_model) {
1384 case POWERPC_MMU_SOFT_6xx:
1385 env->exception_index = POWERPC_EXCP_IFTLB;
1386 env->error_code = 1 << 18;
1387 env->spr[SPR_IMISS] = address;
1388 env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1390 case POWERPC_MMU_SOFT_74xx:
1391 env->exception_index = POWERPC_EXCP_IFTLB;
1393 case POWERPC_MMU_SOFT_4xx:
1394 case POWERPC_MMU_SOFT_4xx_Z:
1395 env->exception_index = POWERPC_EXCP_ITLB;
1396 env->error_code = 0;
1397 env->spr[SPR_40x_DEAR] = address;
1398 env->spr[SPR_40x_ESR] = 0x00000000;
1400 case POWERPC_MMU_32B:
1401 #if defined(TARGET_PPC64)
1402 case POWERPC_MMU_64B:
1404 env->exception_index = POWERPC_EXCP_ISI;
1405 env->error_code = 0x40000000;
1407 case POWERPC_MMU_601:
1409 cpu_abort(env, "MMU model not implemented\n");
1411 case POWERPC_MMU_BOOKE:
1413 cpu_abort(env, "MMU model not implemented\n");
1415 case POWERPC_MMU_BOOKE_FSL:
1417 cpu_abort(env, "MMU model not implemented\n");
1419 case POWERPC_MMU_REAL_4xx:
1420 cpu_abort(env, "PowerPC 401 should never raise any MMU "
1424 cpu_abort(env, "Unknown or invalid MMU model\n");
1429 /* Access rights violation */
1430 env->exception_index = POWERPC_EXCP_ISI;
1431 env->error_code = 0x08000000;
1434 /* No execute protection violation */
1435 env->exception_index = POWERPC_EXCP_ISI;
1436 env->error_code = 0x10000000;
1439 /* Direct store exception */
1440 /* No code fetch is allowed in direct-store areas */
1441 env->exception_index = POWERPC_EXCP_ISI;
1442 env->error_code = 0x10000000;
1444 #if defined(TARGET_PPC64)
1446 /* No match in segment table */
1447 env->exception_index = POWERPC_EXCP_ISEG;
1448 env->error_code = 0;
1455 /* No matches in page tables or TLB */
1456 switch (env->mmu_model) {
1457 case POWERPC_MMU_SOFT_6xx:
1459 env->exception_index = POWERPC_EXCP_DSTLB;
1460 env->error_code = 1 << 16;
1462 env->exception_index = POWERPC_EXCP_DLTLB;
1463 env->error_code = 0;
1465 env->spr[SPR_DMISS] = address;
1466 env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1468 env->error_code |= ctx.key << 19;
1469 env->spr[SPR_HASH1] = ctx.pg_addr[0];
1470 env->spr[SPR_HASH2] = ctx.pg_addr[1];
1472 case POWERPC_MMU_SOFT_74xx:
1474 env->exception_index = POWERPC_EXCP_DSTLB;
1476 env->exception_index = POWERPC_EXCP_DLTLB;
1479 /* Implement LRU algorithm */
1480 env->error_code = ctx.key << 19;
1481 env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1482 ((env->last_way + 1) & (env->nb_ways - 1));
1483 env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
1485 case POWERPC_MMU_SOFT_4xx:
1486 case POWERPC_MMU_SOFT_4xx_Z:
1487 env->exception_index = POWERPC_EXCP_DTLB;
1488 env->error_code = 0;
1489 env->spr[SPR_40x_DEAR] = address;
1491 env->spr[SPR_40x_ESR] = 0x00800000;
1493 env->spr[SPR_40x_ESR] = 0x00000000;
1495 case POWERPC_MMU_32B:
1496 #if defined(TARGET_PPC64)
1497 case POWERPC_MMU_64B:
1499 env->exception_index = POWERPC_EXCP_DSI;
1500 env->error_code = 0;
1501 env->spr[SPR_DAR] = address;
1503 env->spr[SPR_DSISR] = 0x42000000;
1505 env->spr[SPR_DSISR] = 0x40000000;
1507 case POWERPC_MMU_601:
1509 cpu_abort(env, "MMU model not implemented\n");
1511 case POWERPC_MMU_BOOKE:
1513 cpu_abort(env, "MMU model not implemented\n");
1515 case POWERPC_MMU_BOOKE_FSL:
1517 cpu_abort(env, "MMU model not implemented\n");
1519 case POWERPC_MMU_REAL_4xx:
1520 cpu_abort(env, "PowerPC 401 should never raise any MMU "
1524 cpu_abort(env, "Unknown or invalid MMU model\n");
1529 /* Access rights violation */
1530 env->exception_index = POWERPC_EXCP_DSI;
1531 env->error_code = 0;
1532 env->spr[SPR_DAR] = address;
1534 env->spr[SPR_DSISR] = 0x0A000000;
1536 env->spr[SPR_DSISR] = 0x08000000;
1539 /* Direct store exception */
1540 switch (access_type) {
1542 /* Floating point load/store */
1543 env->exception_index = POWERPC_EXCP_ALIGN;
1544 env->error_code = POWERPC_EXCP_ALIGN_FP;
1545 env->spr[SPR_DAR] = address;
1548 /* lwarx, ldarx or stwcx. */
1549 env->exception_index = POWERPC_EXCP_DSI;
1550 env->error_code = 0;
1551 env->spr[SPR_DAR] = address;
1553 env->spr[SPR_DSISR] = 0x06000000;
1555 env->spr[SPR_DSISR] = 0x04000000;
1558 /* eciwx or ecowx */
1559 env->exception_index = POWERPC_EXCP_DSI;
1560 env->error_code = 0;
1561 env->spr[SPR_DAR] = address;
1563 env->spr[SPR_DSISR] = 0x06100000;
1565 env->spr[SPR_DSISR] = 0x04100000;
1568 printf("DSI: invalid exception (%d)\n", ret);
1569 env->exception_index = POWERPC_EXCP_PROGRAM;
1571 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1572 env->spr[SPR_DAR] = address;
1576 #if defined(TARGET_PPC64)
1578 /* No match in segment table */
1579 env->exception_index = POWERPC_EXCP_DSEG;
1580 env->error_code = 0;
1581 env->spr[SPR_DAR] = address;
1587 printf("%s: set exception to %d %02x\n", __func__,
1588 env->exception, env->error_code);
1596 /*****************************************************************************/
1597 /* BATs management */
1598 #if !defined(FLUSH_ALL_TLBS)
1599 static always_inline void do_invalidate_BAT (CPUPPCState *env,
1603 target_ulong base, end, page;
1605 base = BATu & ~0x0001FFFF;
1606 end = base + mask + 0x00020000;
1607 #if defined (DEBUG_BATS)
1608 if (loglevel != 0) {
1609 fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1613 for (page = base; page != end; page += TARGET_PAGE_SIZE)
1614 tlb_flush_page(env, page);
1615 #if defined (DEBUG_BATS)
1617 fprintf(logfile, "Flush done\n");
1622 static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1623 int ul, int nr, target_ulong value)
1625 #if defined (DEBUG_BATS)
1626 if (loglevel != 0) {
1627 fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1628 ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1633 target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1635 return env->IBAT[0][nr];
1638 target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1640 return env->IBAT[1][nr];
1643 void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1647 dump_store_bat(env, 'I', 0, nr, value);
1648 if (env->IBAT[0][nr] != value) {
1649 mask = (value << 15) & 0x0FFE0000UL;
1650 #if !defined(FLUSH_ALL_TLBS)
1651 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1653 /* When storing valid upper BAT, mask BEPI and BRPN
1654 * and invalidate all TLBs covered by this BAT
1656 mask = (value << 15) & 0x0FFE0000UL;
1657 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1658 (value & ~0x0001FFFFUL & ~mask);
1659 env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1660 (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1661 #if !defined(FLUSH_ALL_TLBS)
1662 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1669 void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1671 dump_store_bat(env, 'I', 1, nr, value);
1672 env->IBAT[1][nr] = value;
1675 target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1677 return env->DBAT[0][nr];
1680 target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1682 return env->DBAT[1][nr];
1685 void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1689 dump_store_bat(env, 'D', 0, nr, value);
1690 if (env->DBAT[0][nr] != value) {
1691 /* When storing valid upper BAT, mask BEPI and BRPN
1692 * and invalidate all TLBs covered by this BAT
1694 mask = (value << 15) & 0x0FFE0000UL;
1695 #if !defined(FLUSH_ALL_TLBS)
1696 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1698 mask = (value << 15) & 0x0FFE0000UL;
1699 env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1700 (value & ~0x0001FFFFUL & ~mask);
1701 env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1702 (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1703 #if !defined(FLUSH_ALL_TLBS)
1704 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1711 void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1713 dump_store_bat(env, 'D', 1, nr, value);
1714 env->DBAT[1][nr] = value;
1717 /*****************************************************************************/
1718 /* TLB management */
1719 void ppc_tlb_invalidate_all (CPUPPCState *env)
1721 switch (env->mmu_model) {
1722 case POWERPC_MMU_SOFT_6xx:
1723 case POWERPC_MMU_SOFT_74xx:
1724 ppc6xx_tlb_invalidate_all(env);
1726 case POWERPC_MMU_SOFT_4xx:
1727 case POWERPC_MMU_SOFT_4xx_Z:
1728 ppc4xx_tlb_invalidate_all(env);
1730 case POWERPC_MMU_REAL_4xx:
1731 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1733 case POWERPC_MMU_BOOKE:
1735 cpu_abort(env, "MMU model not implemented\n");
1737 case POWERPC_MMU_BOOKE_FSL:
1739 cpu_abort(env, "MMU model not implemented\n");
1741 case POWERPC_MMU_601:
1743 cpu_abort(env, "MMU model not implemented\n");
1745 case POWERPC_MMU_32B:
1746 #if defined(TARGET_PPC64)
1747 case POWERPC_MMU_64B:
1748 #endif /* defined(TARGET_PPC64) */
1753 cpu_abort(env, "Unknown MMU model\n");
1758 void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1760 #if !defined(FLUSH_ALL_TLBS)
1761 addr &= TARGET_PAGE_MASK;
1762 switch (env->mmu_model) {
1763 case POWERPC_MMU_SOFT_6xx:
1764 case POWERPC_MMU_SOFT_74xx:
1765 ppc6xx_tlb_invalidate_virt(env, addr, 0);
1766 if (env->id_tlbs == 1)
1767 ppc6xx_tlb_invalidate_virt(env, addr, 1);
1769 case POWERPC_MMU_SOFT_4xx:
1770 case POWERPC_MMU_SOFT_4xx_Z:
1771 ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
1773 case POWERPC_MMU_REAL_4xx:
1774 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1776 case POWERPC_MMU_BOOKE:
1778 cpu_abort(env, "MMU model not implemented\n");
1780 case POWERPC_MMU_BOOKE_FSL:
1782 cpu_abort(env, "MMU model not implemented\n");
1784 case POWERPC_MMU_601:
1786 cpu_abort(env, "MMU model not implemented\n");
1788 case POWERPC_MMU_32B:
1789 /* tlbie invalidate TLBs for all segments */
1790 addr &= ~((target_ulong)-1 << 28);
1791 /* XXX: this case should be optimized,
1792 * giving a mask to tlb_flush_page
1794 tlb_flush_page(env, addr | (0x0 << 28));
1795 tlb_flush_page(env, addr | (0x1 << 28));
1796 tlb_flush_page(env, addr | (0x2 << 28));
1797 tlb_flush_page(env, addr | (0x3 << 28));
1798 tlb_flush_page(env, addr | (0x4 << 28));
1799 tlb_flush_page(env, addr | (0x5 << 28));
1800 tlb_flush_page(env, addr | (0x6 << 28));
1801 tlb_flush_page(env, addr | (0x7 << 28));
1802 tlb_flush_page(env, addr | (0x8 << 28));
1803 tlb_flush_page(env, addr | (0x9 << 28));
1804 tlb_flush_page(env, addr | (0xA << 28));
1805 tlb_flush_page(env, addr | (0xB << 28));
1806 tlb_flush_page(env, addr | (0xC << 28));
1807 tlb_flush_page(env, addr | (0xD << 28));
1808 tlb_flush_page(env, addr | (0xE << 28));
1809 tlb_flush_page(env, addr | (0xF << 28));
1811 #if defined(TARGET_PPC64)
1812 case POWERPC_MMU_64B:
1813 /* tlbie invalidate TLBs for all segments */
1814 /* XXX: given the fact that there are too many segments to invalidate,
1815 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
1816 * we just invalidate all TLBs
1820 #endif /* defined(TARGET_PPC64) */
1823 cpu_abort(env, "Unknown MMU model\n");
1827 ppc_tlb_invalidate_all(env);
1831 #if defined(TARGET_PPC64)
1832 void ppc_slb_invalidate_all (CPUPPCState *env)
1838 void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
1845 /*****************************************************************************/
1846 /* Special registers manipulation */
1847 #if defined(TARGET_PPC64)
1848 target_ulong ppc_load_asr (CPUPPCState *env)
1853 void ppc_store_asr (CPUPPCState *env, target_ulong value)
1855 if (env->asr != value) {
1862 target_ulong do_load_sdr1 (CPUPPCState *env)
1867 void do_store_sdr1 (CPUPPCState *env, target_ulong value)
1869 #if defined (DEBUG_MMU)
1870 if (loglevel != 0) {
1871 fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
1874 if (env->sdr1 != value) {
1875 /* XXX: for PowerPC 64, should check that the HTABSIZE value
1884 target_ulong do_load_sr (CPUPPCState *env, int srnum)
1886 return env->sr[srnum];
1890 void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
1892 #if defined (DEBUG_MMU)
1893 if (loglevel != 0) {
1894 fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
1895 __func__, srnum, value, env->sr[srnum]);
1898 if (env->sr[srnum] != value) {
1899 env->sr[srnum] = value;
1900 #if !defined(FLUSH_ALL_TLBS) && 0
1902 target_ulong page, end;
1903 /* Invalidate 256 MB of virtual memory */
1904 page = (16 << 20) * srnum;
1905 end = page + (16 << 20);
1906 for (; page != end; page += TARGET_PAGE_SIZE)
1907 tlb_flush_page(env, page);
1914 #endif /* !defined (CONFIG_USER_ONLY) */
1916 target_ulong ppc_load_xer (CPUPPCState *env)
1918 return (xer_so << XER_SO) |
1919 (xer_ov << XER_OV) |
1920 (xer_ca << XER_CA) |
1921 (xer_bc << XER_BC) |
1922 (xer_cmp << XER_CMP);
1925 void ppc_store_xer (CPUPPCState *env, target_ulong value)
1927 xer_so = (value >> XER_SO) & 0x01;
1928 xer_ov = (value >> XER_OV) & 0x01;
1929 xer_ca = (value >> XER_CA) & 0x01;
1930 xer_cmp = (value >> XER_CMP) & 0xFF;
1931 xer_bc = (value >> XER_BC) & 0x7F;
1934 /* Swap temporary saved registers with GPRs */
1935 static always_inline void swap_gpr_tgpr (CPUPPCState *env)
1940 env->gpr[0] = env->tgpr[0];
1943 env->gpr[1] = env->tgpr[1];
1946 env->gpr[2] = env->tgpr[2];
1949 env->gpr[3] = env->tgpr[3];
1953 /* GDBstub can read and write MSR... */
1954 target_ulong do_load_msr (CPUPPCState *env)
1957 #if defined (TARGET_PPC64)
1958 ((target_ulong)msr_sf << MSR_SF) |
1959 ((target_ulong)msr_isf << MSR_ISF) |
1960 ((target_ulong)msr_hv << MSR_HV) |
1962 ((target_ulong)msr_ucle << MSR_UCLE) |
1963 ((target_ulong)msr_vr << MSR_VR) | /* VR / SPE */
1964 ((target_ulong)msr_ap << MSR_AP) |
1965 ((target_ulong)msr_sa << MSR_SA) |
1966 ((target_ulong)msr_key << MSR_KEY) |
1967 ((target_ulong)msr_pow << MSR_POW) | /* POW / WE */
1968 ((target_ulong)msr_tgpr << MSR_TGPR) | /* TGPR / CE */
1969 ((target_ulong)msr_ile << MSR_ILE) |
1970 ((target_ulong)msr_ee << MSR_EE) |
1971 ((target_ulong)msr_pr << MSR_PR) |
1972 ((target_ulong)msr_fp << MSR_FP) |
1973 ((target_ulong)msr_me << MSR_ME) |
1974 ((target_ulong)msr_fe0 << MSR_FE0) |
1975 ((target_ulong)msr_se << MSR_SE) | /* SE / DWE / UBLE */
1976 ((target_ulong)msr_be << MSR_BE) | /* BE / DE */
1977 ((target_ulong)msr_fe1 << MSR_FE1) |
1978 ((target_ulong)msr_al << MSR_AL) |
1979 ((target_ulong)msr_ip << MSR_IP) |
1980 ((target_ulong)msr_ir << MSR_IR) | /* IR / IS */
1981 ((target_ulong)msr_dr << MSR_DR) | /* DR / DS */
1982 ((target_ulong)msr_pe << MSR_PE) | /* PE / EP */
1983 ((target_ulong)msr_px << MSR_PX) | /* PX / PMM */
1984 ((target_ulong)msr_ri << MSR_RI) |
1985 ((target_ulong)msr_le << MSR_LE);
1988 int do_store_msr (CPUPPCState *env, target_ulong value)
1992 value &= env->msr_mask;
1993 if (((value >> MSR_IR) & 1) != msr_ir ||
1994 ((value >> MSR_DR) & 1) != msr_dr) {
1995 /* Flush all tlb when changing translation mode */
1997 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2000 if (loglevel != 0) {
2001 fprintf(logfile, "%s: T0 %08lx\n", __func__, value);
2004 if (unlikely((env->flags & POWERPC_FLAG_TGPR) &&
2005 ((value >> MSR_TGPR) & 1) != msr_tgpr)) {
2006 /* Swap temporary saved registers with GPRs */
2009 #if defined (TARGET_PPC64)
2010 msr_sf = (value >> MSR_SF) & 1;
2011 msr_isf = (value >> MSR_ISF) & 1;
2012 msr_hv = (value >> MSR_HV) & 1;
2014 msr_ucle = (value >> MSR_UCLE) & 1;
2015 msr_vr = (value >> MSR_VR) & 1; /* VR / SPE */
2016 msr_ap = (value >> MSR_AP) & 1;
2017 msr_sa = (value >> MSR_SA) & 1;
2018 msr_key = (value >> MSR_KEY) & 1;
2019 msr_pow = (value >> MSR_POW) & 1; /* POW / WE */
2020 msr_tgpr = (value >> MSR_TGPR) & 1; /* TGPR / CE */
2021 msr_ile = (value >> MSR_ILE) & 1;
2022 msr_ee = (value >> MSR_EE) & 1;
2023 msr_pr = (value >> MSR_PR) & 1;
2024 msr_fp = (value >> MSR_FP) & 1;
2025 msr_me = (value >> MSR_ME) & 1;
2026 msr_fe0 = (value >> MSR_FE0) & 1;
2027 msr_se = (value >> MSR_SE) & 1; /* SE / DWE / UBLE */
2028 msr_be = (value >> MSR_BE) & 1; /* BE / DE */
2029 msr_fe1 = (value >> MSR_FE1) & 1;
2030 msr_al = (value >> MSR_AL) & 1;
2031 msr_ip = (value >> MSR_IP) & 1;
2032 msr_ir = (value >> MSR_IR) & 1; /* IR / IS */
2033 msr_dr = (value >> MSR_DR) & 1; /* DR / DS */
2034 msr_pe = (value >> MSR_PE) & 1; /* PE / EP */
2035 msr_px = (value >> MSR_PX) & 1; /* PX / PMM */
2036 msr_ri = (value >> MSR_RI) & 1;
2037 msr_le = (value >> MSR_LE) & 1;
2038 do_compute_hflags(env);
2041 switch (env->excp_model) {
2042 case POWERPC_EXCP_603:
2043 case POWERPC_EXCP_603E:
2044 case POWERPC_EXCP_G2:
2045 /* Don't handle SLEEP mode: we should disable all clocks...
2046 * No dynamic power-management.
2048 if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00C00000) != 0)
2051 case POWERPC_EXCP_604:
2055 case POWERPC_EXCP_7x0:
2056 if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00E00000) != 0)
2066 #if defined(TARGET_PPC64)
2067 int ppc_store_msr_32 (CPUPPCState *env, uint32_t value)
2069 return do_store_msr(env, (do_load_msr(env) & ~0xFFFFFFFFULL) |
2070 (value & 0xFFFFFFFF));
2074 void do_compute_hflags (CPUPPCState *env)
2076 /* Compute current hflags */
2077 env->hflags = (msr_vr << MSR_VR) |
2078 (msr_ap << MSR_AP) | (msr_sa << MSR_SA) | (msr_pr << MSR_PR) |
2079 (msr_fp << MSR_FP) | (msr_fe0 << MSR_FE0) | (msr_se << MSR_SE) |
2080 (msr_be << MSR_BE) | (msr_fe1 << MSR_FE1) | (msr_le << MSR_LE);
2081 #if defined (TARGET_PPC64)
2082 env->hflags |= msr_cm << MSR_CM;
2083 env->hflags |= (uint64_t)msr_sf << MSR_SF;
2084 env->hflags |= (uint64_t)msr_hv << MSR_HV;
2088 /*****************************************************************************/
2089 /* Exception processing */
2090 #if defined (CONFIG_USER_ONLY)
2091 void do_interrupt (CPUState *env)
2093 env->exception_index = POWERPC_EXCP_NONE;
2094 env->error_code = 0;
2097 void ppc_hw_interrupt (CPUState *env)
2099 env->exception_index = POWERPC_EXCP_NONE;
2100 env->error_code = 0;
2102 #else /* defined (CONFIG_USER_ONLY) */
2103 static void dump_syscall (CPUState *env)
2105 fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
2106 " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
2107 env->gpr[0], env->gpr[3], env->gpr[4],
2108 env->gpr[5], env->gpr[6], env->nip);
2111 /* Note that this function should be greatly optimized
2112 * when called with a constant excp, from ppc_hw_interrupt
2114 static always_inline void powerpc_excp (CPUState *env,
2115 int excp_model, int excp)
2117 target_ulong msr, vector;
2118 int srr0, srr1, asrr0, asrr1;
2120 if (loglevel & CPU_LOG_INT) {
2121 fprintf(logfile, "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
2122 env->nip, excp, env->error_code);
2124 msr = do_load_msr(env);
2129 msr &= ~((target_ulong)0x783F0000);
2131 case POWERPC_EXCP_NONE:
2132 /* Should never happen */
2134 case POWERPC_EXCP_CRITICAL: /* Critical input */
2135 msr_ri = 0; /* XXX: check this */
2136 switch (excp_model) {
2137 case POWERPC_EXCP_40x:
2138 srr0 = SPR_40x_SRR2;
2139 srr1 = SPR_40x_SRR3;
2141 case POWERPC_EXCP_BOOKE:
2142 srr0 = SPR_BOOKE_CSRR0;
2143 srr1 = SPR_BOOKE_CSRR1;
2145 case POWERPC_EXCP_G2:
2151 case POWERPC_EXCP_MCHECK: /* Machine check exception */
2153 /* Machine check exception is not enabled */
2154 /* XXX: we may just stop the processor here, to allow debugging */
2155 excp = POWERPC_EXCP_RESET;
2160 #if defined(TARGET_PPC64H)
2163 /* XXX: should also have something loaded in DAR / DSISR */
2164 switch (excp_model) {
2165 case POWERPC_EXCP_40x:
2166 srr0 = SPR_40x_SRR2;
2167 srr1 = SPR_40x_SRR3;
2169 case POWERPC_EXCP_BOOKE:
2170 srr0 = SPR_BOOKE_MCSRR0;
2171 srr1 = SPR_BOOKE_MCSRR1;
2172 asrr0 = SPR_BOOKE_CSRR0;
2173 asrr1 = SPR_BOOKE_CSRR1;
2179 case POWERPC_EXCP_DSI: /* Data storage exception */
2180 #if defined (DEBUG_EXCEPTIONS)
2181 if (loglevel != 0) {
2182 fprintf(logfile, "DSI exception: DSISR=0x" ADDRX" DAR=0x" ADDRX
2183 "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
2187 #if defined(TARGET_PPC64H)
2192 case POWERPC_EXCP_ISI: /* Instruction storage exception */
2193 #if defined (DEBUG_EXCEPTIONS)
2194 if (loglevel != 0) {
2195 fprintf(logfile, "ISI exception: msr=0x" ADDRX ", nip=0x" ADDRX
2196 "\n", msr, env->nip);
2200 #if defined(TARGET_PPC64H)
2204 msr |= env->error_code;
2206 case POWERPC_EXCP_EXTERNAL: /* External input */
2208 #if defined(TARGET_PPC64H)
2213 case POWERPC_EXCP_ALIGN: /* Alignment exception */
2215 #if defined(TARGET_PPC64H)
2219 /* XXX: this is false */
2220 /* Get rS/rD and rA from faulting opcode */
2221 env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
2223 case POWERPC_EXCP_PROGRAM: /* Program exception */
2224 switch (env->error_code & ~0xF) {
2225 case POWERPC_EXCP_FP:
2226 if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
2227 #if defined (DEBUG_EXCEPTIONS)
2228 if (loglevel != 0) {
2229 fprintf(logfile, "Ignore floating point exception\n");
2235 #if defined(TARGET_PPC64H)
2241 env->fpscr[7] |= 0x8;
2242 /* Finally, update FEX */
2243 if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
2244 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
2245 env->fpscr[7] |= 0x4;
2246 if (msr_fe0 != msr_fe1) {
2251 case POWERPC_EXCP_INVAL:
2252 #if defined (DEBUG_EXCEPTIONS)
2253 if (loglevel != 0) {
2254 fprintf(logfile, "Invalid instruction at 0x" ADDRX "\n",
2259 #if defined(TARGET_PPC64H)
2265 case POWERPC_EXCP_PRIV:
2267 #if defined(TARGET_PPC64H)
2273 case POWERPC_EXCP_TRAP:
2275 #if defined(TARGET_PPC64H)
2282 /* Should never occur */
2283 cpu_abort(env, "Invalid program exception %d. Aborting\n",
2288 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
2290 #if defined(TARGET_PPC64H)
2295 case POWERPC_EXCP_SYSCALL: /* System call exception */
2296 /* NOTE: this is a temporary hack to support graphics OSI
2297 calls from the MOL driver */
2298 /* XXX: To be removed */
2299 if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2301 if (env->osi_call(env) != 0)
2304 if (loglevel & CPU_LOG_INT) {
2308 #if defined(TARGET_PPC64H)
2309 if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
2313 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
2316 case POWERPC_EXCP_DECR: /* Decrementer exception */
2318 #if defined(TARGET_PPC64H)
2323 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
2325 #if defined (DEBUG_EXCEPTIONS)
2327 fprintf(logfile, "FIT exception\n");
2329 msr_ri = 0; /* XXX: check this */
2331 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
2332 #if defined (DEBUG_EXCEPTIONS)
2334 fprintf(logfile, "WDT exception\n");
2336 switch (excp_model) {
2337 case POWERPC_EXCP_BOOKE:
2338 srr0 = SPR_BOOKE_CSRR0;
2339 srr1 = SPR_BOOKE_CSRR1;
2344 msr_ri = 0; /* XXX: check this */
2346 case POWERPC_EXCP_DTLB: /* Data TLB error */
2347 msr_ri = 0; /* XXX: check this */
2349 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
2350 msr_ri = 0; /* XXX: check this */
2352 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
2353 switch (excp_model) {
2354 case POWERPC_EXCP_BOOKE:
2355 srr0 = SPR_BOOKE_DSRR0;
2356 srr1 = SPR_BOOKE_DSRR1;
2357 asrr0 = SPR_BOOKE_CSRR0;
2358 asrr1 = SPR_BOOKE_CSRR1;
2364 cpu_abort(env, "Debug exception is not implemented yet !\n");
2366 #if defined(TARGET_PPCEMB)
2367 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable */
2368 msr_ri = 0; /* XXX: check this */
2370 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */
2372 cpu_abort(env, "Embedded floating point data exception "
2373 "is not implemented yet !\n");
2375 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */
2377 cpu_abort(env, "Embedded floating point round exception "
2378 "is not implemented yet !\n");
2380 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor interrupt */
2384 "Performance counter exception is not implemented yet !\n");
2386 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
2389 "Embedded doorbell interrupt is not implemented yet !\n");
2391 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
2392 switch (excp_model) {
2393 case POWERPC_EXCP_BOOKE:
2394 srr0 = SPR_BOOKE_CSRR0;
2395 srr1 = SPR_BOOKE_CSRR1;
2401 cpu_abort(env, "Embedded doorbell critical interrupt "
2402 "is not implemented yet !\n");
2404 #endif /* defined(TARGET_PPCEMB) */
2405 case POWERPC_EXCP_RESET: /* System reset exception */
2407 #if defined(TARGET_PPC64H)
2412 #if defined(TARGET_PPC64)
2413 case POWERPC_EXCP_DSEG: /* Data segment exception */
2415 #if defined(TARGET_PPC64H)
2420 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
2422 #if defined(TARGET_PPC64H)
2427 #endif /* defined(TARGET_PPC64) */
2428 #if defined(TARGET_PPC64H)
2429 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
2435 case POWERPC_EXCP_TRACE: /* Trace exception */
2437 #if defined(TARGET_PPC64H)
2442 #if defined(TARGET_PPC64H)
2443 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
2448 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */
2453 cpu_abort(env, "Hypervisor instruction storage exception "
2454 "is not implemented yet !\n");
2456 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
2461 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment exception */
2466 #endif /* defined(TARGET_PPC64H) */
2467 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
2469 #if defined(TARGET_PPC64H)
2474 case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */
2475 #if defined (DEBUG_EXCEPTIONS)
2477 fprintf(logfile, "PIT exception\n");
2479 msr_ri = 0; /* XXX: check this */
2481 case POWERPC_EXCP_IO: /* IO error exception */
2483 cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2485 case POWERPC_EXCP_RUNM: /* Run mode exception */
2487 cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2489 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
2491 cpu_abort(env, "602 emulation trap exception "
2492 "is not implemented yet !\n");
2494 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
2495 msr_ri = 0; /* XXX: check this */
2496 #if defined(TARGET_PPC64H) /* XXX: check this */
2500 switch (excp_model) {
2501 case POWERPC_EXCP_602:
2502 case POWERPC_EXCP_603:
2503 case POWERPC_EXCP_603E:
2504 case POWERPC_EXCP_G2:
2506 case POWERPC_EXCP_7x5:
2508 case POWERPC_EXCP_74xx:
2511 cpu_abort(env, "Invalid instruction TLB miss exception\n");
2515 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
2516 msr_ri = 0; /* XXX: check this */
2517 #if defined(TARGET_PPC64H) /* XXX: check this */
2521 switch (excp_model) {
2522 case POWERPC_EXCP_602:
2523 case POWERPC_EXCP_603:
2524 case POWERPC_EXCP_603E:
2525 case POWERPC_EXCP_G2:
2527 case POWERPC_EXCP_7x5:
2529 case POWERPC_EXCP_74xx:
2532 cpu_abort(env, "Invalid data load TLB miss exception\n");
2536 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
2537 msr_ri = 0; /* XXX: check this */
2538 #if defined(TARGET_PPC64H) /* XXX: check this */
2542 switch (excp_model) {
2543 case POWERPC_EXCP_602:
2544 case POWERPC_EXCP_603:
2545 case POWERPC_EXCP_603E:
2546 case POWERPC_EXCP_G2:
2548 /* Swap temporary saved registers with GPRs */
2552 case POWERPC_EXCP_7x5:
2554 #if defined (DEBUG_SOFTWARE_TLB)
2555 if (loglevel != 0) {
2556 const unsigned char *es;
2557 target_ulong *miss, *cmp;
2559 if (excp == POWERPC_EXCP_IFTLB) {
2562 miss = &env->spr[SPR_IMISS];
2563 cmp = &env->spr[SPR_ICMP];
2565 if (excp == POWERPC_EXCP_DLTLB)
2570 miss = &env->spr[SPR_DMISS];
2571 cmp = &env->spr[SPR_DCMP];
2573 fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2574 " H1 " ADDRX " H2 " ADDRX " %08x\n",
2575 es, en, *miss, en, *cmp,
2576 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2580 msr |= env->crf[0] << 28;
2581 msr |= env->error_code; /* key, D/I, S/L bits */
2582 /* Set way using a LRU mechanism */
2583 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2585 case POWERPC_EXCP_74xx:
2587 #if defined (DEBUG_SOFTWARE_TLB)
2588 if (loglevel != 0) {
2589 const unsigned char *es;
2590 target_ulong *miss, *cmp;
2592 if (excp == POWERPC_EXCP_IFTLB) {
2595 miss = &env->spr[SPR_IMISS];
2596 cmp = &env->spr[SPR_ICMP];
2598 if (excp == POWERPC_EXCP_DLTLB)
2603 miss = &env->spr[SPR_TLBMISS];
2604 cmp = &env->spr[SPR_PTEHI];
2606 fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2608 es, en, *miss, en, *cmp, env->error_code);
2611 msr |= env->error_code; /* key bit */
2614 cpu_abort(env, "Invalid data store TLB miss exception\n");
2618 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
2620 cpu_abort(env, "Floating point assist exception "
2621 "is not implemented yet !\n");
2623 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
2625 cpu_abort(env, "IABR exception is not implemented yet !\n");
2627 case POWERPC_EXCP_SMI: /* System management interrupt */
2629 cpu_abort(env, "SMI exception is not implemented yet !\n");
2631 case POWERPC_EXCP_THERM: /* Thermal interrupt */
2633 cpu_abort(env, "Thermal management exception "
2634 "is not implemented yet !\n");
2636 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
2638 #if defined(TARGET_PPC64H)
2644 "Performance counter exception is not implemented yet !\n");
2646 case POWERPC_EXCP_VPUA: /* Vector assist exception */
2648 cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2650 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
2653 "970 soft-patch exception is not implemented yet !\n");
2655 case POWERPC_EXCP_MAINT: /* Maintenance exception */
2658 "970 maintenance exception is not implemented yet !\n");
2662 cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2665 /* save current instruction location */
2666 env->spr[srr0] = env->nip - 4;
2669 /* save next instruction location */
2670 env->spr[srr0] = env->nip;
2674 env->spr[srr1] = msr;
2675 /* If any alternate SRR register are defined, duplicate saved values */
2677 env->spr[asrr0] = env->spr[srr0];
2679 env->spr[asrr1] = env->spr[srr1];
2680 /* If we disactivated any translation, flush TLBs */
2681 if (msr_ir || msr_dr)
2683 /* reload MSR with correct bits */
2693 #if 0 /* Fix this: not on all targets */
2697 do_compute_hflags(env);
2698 /* Jump to handler */
2699 vector = env->excp_vectors[excp];
2700 if (vector == (target_ulong)-1) {
2701 cpu_abort(env, "Raised an exception without defined vector %d\n",
2704 vector |= env->excp_prefix;
2705 #if defined(TARGET_PPC64)
2706 if (excp_model == POWERPC_EXCP_BOOKE) {
2709 vector = (uint32_t)vector;
2713 vector = (uint32_t)vector;
2717 /* Reset exception state */
2718 env->exception_index = POWERPC_EXCP_NONE;
2719 env->error_code = 0;
2722 void do_interrupt (CPUState *env)
2724 powerpc_excp(env, env->excp_model, env->exception_index);
2727 void ppc_hw_interrupt (CPUPPCState *env)
2730 if (loglevel & CPU_LOG_INT) {
2731 fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2732 __func__, env, env->pending_interrupts,
2733 env->interrupt_request, msr_me, msr_ee);
2736 /* External reset */
2737 if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2738 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2739 powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2742 /* Machine check exception */
2743 if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2744 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2745 powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2749 /* External debug exception */
2750 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2751 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2752 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2756 #if defined(TARGET_PPC64H)
2757 if ((msr_ee != 0 || msr_hv == 0 || msr_pr == 1) & hdice != 0) {
2758 /* Hypervisor decrementer exception */
2759 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2760 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2761 powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2767 /* External critical interrupt */
2768 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2769 /* Taking a critical external interrupt does not clear the external
2770 * critical interrupt status
2773 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2775 powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2780 /* Watchdog timer on embedded PowerPC */
2781 if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2782 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2783 powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2786 #if defined(TARGET_PPCEMB)
2787 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2788 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2789 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2793 #if defined(TARGET_PPCEMB)
2794 /* External interrupt */
2795 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2796 /* Taking an external interrupt does not clear the external
2800 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2802 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2806 /* Fixed interval timer on embedded PowerPC */
2807 if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2808 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2809 powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2812 /* Programmable interval timer on embedded PowerPC */
2813 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2814 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2815 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2818 /* Decrementer exception */
2819 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2820 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2821 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2824 #if !defined(TARGET_PPCEMB)
2825 /* External interrupt */
2826 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2827 /* Taking an external interrupt does not clear the external
2831 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2833 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2837 #if defined(TARGET_PPCEMB)
2838 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2839 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2840 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2844 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2845 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2846 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2849 /* Thermal interrupt */
2850 if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2851 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2852 powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2857 #endif /* !CONFIG_USER_ONLY */
2859 void cpu_dump_EA (target_ulong EA)
2869 fprintf(f, "Memory access at address " ADDRX "\n", EA);
2872 void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2882 fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2886 void cpu_ppc_reset (void *opaque)
2892 /* XXX: some of those flags initialisation values could depend
2893 * on the actual PowerPC implementation
2895 for (i = 0; i < 63; i++)
2897 #if defined(TARGET_PPC64)
2898 msr_hv = 0; /* Should be 1... */
2900 msr_ap = 0; /* TO BE CHECKED */
2901 msr_sa = 0; /* TO BE CHECKED */
2902 msr_ip = 0; /* TO BE CHECKED */
2903 #if defined (DO_SINGLE_STEP) && 0
2904 /* Single step trace mode */
2908 #if defined(CONFIG_USER_ONLY)
2909 msr_fp = 1; /* Allow floating point exceptions */
2912 env->nip = env->hreset_vector | env->excp_prefix;
2913 ppc_tlb_invalidate_all(env);
2915 do_compute_hflags(env);
2917 /* Be sure no exception or interrupt is pending */
2918 env->pending_interrupts = 0;
2919 env->exception_index = POWERPC_EXCP_NONE;
2920 env->error_code = 0;
2921 /* Flush all TLBs */
2925 CPUPPCState *cpu_ppc_init (void)
2929 env = qemu_mallocz(sizeof(CPUPPCState));
2937 void cpu_ppc_close (CPUPPCState *env)
2939 /* Should also remove all opcode tables... */