Fix ppc32 register dumps on 64-bit hosts.
[qemu/mini2440.git] / target-ppc / helper.c
blob149f6cbfa545c723355bfcb4c1145ceaa371e7c5
1 /*
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
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <signal.h>
26 #include <assert.h>
28 #include "cpu.h"
29 #include "exec-all.h"
30 #include "helper_regs.h"
32 //#define DEBUG_MMU
33 //#define DEBUG_BATS
34 //#define DEBUG_SOFTWARE_TLB
35 //#define DUMP_PAGE_TABLES
36 //#define DEBUG_EXCEPTIONS
37 //#define FLUSH_ALL_TLBS
39 /*****************************************************************************/
40 /* PowerPC MMU emulation */
42 #if defined(CONFIG_USER_ONLY)
43 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
44 int mmu_idx, int is_softmmu)
46 int exception, error_code;
48 if (rw == 2) {
49 exception = POWERPC_EXCP_ISI;
50 error_code = 0x40000000;
51 } else {
52 exception = POWERPC_EXCP_DSI;
53 error_code = 0x40000000;
54 if (rw)
55 error_code |= 0x02000000;
56 env->spr[SPR_DAR] = address;
57 env->spr[SPR_DSISR] = error_code;
59 env->exception_index = exception;
60 env->error_code = error_code;
62 return 1;
65 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
67 return addr;
70 #else
71 /* Common routines used by software and hardware TLBs emulation */
72 static always_inline int pte_is_valid (target_ulong pte0)
74 return pte0 & 0x80000000 ? 1 : 0;
77 static always_inline void pte_invalidate (target_ulong *pte0)
79 *pte0 &= ~0x80000000;
82 #if defined(TARGET_PPC64)
83 static always_inline int pte64_is_valid (target_ulong pte0)
85 return pte0 & 0x0000000000000001ULL ? 1 : 0;
88 static always_inline void pte64_invalidate (target_ulong *pte0)
90 *pte0 &= ~0x0000000000000001ULL;
92 #endif
94 #define PTE_PTEM_MASK 0x7FFFFFBF
95 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
96 #if defined(TARGET_PPC64)
97 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
98 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
99 #endif
101 static always_inline int pp_check (int key, int pp, int nx)
103 int access;
105 /* Compute access rights */
106 /* When pp is 3/7, the result is undefined. Set it to noaccess */
107 access = 0;
108 if (key == 0) {
109 switch (pp) {
110 case 0x0:
111 case 0x1:
112 case 0x2:
113 access |= PAGE_WRITE;
114 /* No break here */
115 case 0x3:
116 case 0x6:
117 access |= PAGE_READ;
118 break;
120 } else {
121 switch (pp) {
122 case 0x0:
123 case 0x6:
124 access = 0;
125 break;
126 case 0x1:
127 case 0x3:
128 access = PAGE_READ;
129 break;
130 case 0x2:
131 access = PAGE_READ | PAGE_WRITE;
132 break;
135 if (nx == 0)
136 access |= PAGE_EXEC;
138 return access;
141 static always_inline int check_prot (int prot, int rw, int access_type)
143 int ret;
145 if (access_type == ACCESS_CODE) {
146 if (prot & PAGE_EXEC)
147 ret = 0;
148 else
149 ret = -2;
150 } else if (rw) {
151 if (prot & PAGE_WRITE)
152 ret = 0;
153 else
154 ret = -2;
155 } else {
156 if (prot & PAGE_READ)
157 ret = 0;
158 else
159 ret = -2;
162 return ret;
165 static always_inline int _pte_check (mmu_ctx_t *ctx, int is_64b,
166 target_ulong pte0, target_ulong pte1,
167 int h, int rw, int type)
169 target_ulong ptem, mmask;
170 int access, ret, pteh, ptev, pp;
172 access = 0;
173 ret = -1;
174 /* Check validity and table match */
175 #if defined(TARGET_PPC64)
176 if (is_64b) {
177 ptev = pte64_is_valid(pte0);
178 pteh = (pte0 >> 1) & 1;
179 } else
180 #endif
182 ptev = pte_is_valid(pte0);
183 pteh = (pte0 >> 6) & 1;
185 if (ptev && h == pteh) {
186 /* Check vsid & api */
187 #if defined(TARGET_PPC64)
188 if (is_64b) {
189 ptem = pte0 & PTE64_PTEM_MASK;
190 mmask = PTE64_CHECK_MASK;
191 pp = (pte1 & 0x00000003) | ((pte1 >> 61) & 0x00000004);
192 ctx->nx |= (pte1 >> 2) & 1; /* No execute bit */
193 ctx->nx |= (pte1 >> 3) & 1; /* Guarded bit */
194 } else
195 #endif
197 ptem = pte0 & PTE_PTEM_MASK;
198 mmask = PTE_CHECK_MASK;
199 pp = pte1 & 0x00000003;
201 if (ptem == ctx->ptem) {
202 if (ctx->raddr != (target_phys_addr_t)-1ULL) {
203 /* all matches should have equal RPN, WIMG & PP */
204 if ((ctx->raddr & mmask) != (pte1 & mmask)) {
205 if (loglevel != 0)
206 fprintf(logfile, "Bad RPN/WIMG/PP\n");
207 return -3;
210 /* Compute access rights */
211 access = pp_check(ctx->key, pp, ctx->nx);
212 /* Keep the matching PTE informations */
213 ctx->raddr = pte1;
214 ctx->prot = access;
215 ret = check_prot(ctx->prot, rw, type);
216 if (ret == 0) {
217 /* Access granted */
218 #if defined (DEBUG_MMU)
219 if (loglevel != 0)
220 fprintf(logfile, "PTE access granted !\n");
221 #endif
222 } else {
223 /* Access right violation */
224 #if defined (DEBUG_MMU)
225 if (loglevel != 0)
226 fprintf(logfile, "PTE access rejected\n");
227 #endif
232 return ret;
235 static always_inline int pte32_check (mmu_ctx_t *ctx,
236 target_ulong pte0, target_ulong pte1,
237 int h, int rw, int type)
239 return _pte_check(ctx, 0, pte0, pte1, h, rw, type);
242 #if defined(TARGET_PPC64)
243 static always_inline int pte64_check (mmu_ctx_t *ctx,
244 target_ulong pte0, target_ulong pte1,
245 int h, int rw, int type)
247 return _pte_check(ctx, 1, pte0, pte1, h, rw, type);
249 #endif
251 static always_inline int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
252 int ret, int rw)
254 int store = 0;
256 /* Update page flags */
257 if (!(*pte1p & 0x00000100)) {
258 /* Update accessed flag */
259 *pte1p |= 0x00000100;
260 store = 1;
262 if (!(*pte1p & 0x00000080)) {
263 if (rw == 1 && ret == 0) {
264 /* Update changed flag */
265 *pte1p |= 0x00000080;
266 store = 1;
267 } else {
268 /* Force page fault for first write access */
269 ctx->prot &= ~PAGE_WRITE;
273 return store;
276 /* Software driven TLB helpers */
277 static always_inline int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
278 int way, int is_code)
280 int nr;
282 /* Select TLB num in a way from address */
283 nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
284 /* Select TLB way */
285 nr += env->tlb_per_way * way;
286 /* 6xx have separate TLBs for instructions and data */
287 if (is_code && env->id_tlbs == 1)
288 nr += env->nb_tlb;
290 return nr;
293 static always_inline void ppc6xx_tlb_invalidate_all (CPUState *env)
295 ppc6xx_tlb_t *tlb;
296 int nr, max;
298 #if defined (DEBUG_SOFTWARE_TLB) && 0
299 if (loglevel != 0) {
300 fprintf(logfile, "Invalidate all TLBs\n");
302 #endif
303 /* Invalidate all defined software TLB */
304 max = env->nb_tlb;
305 if (env->id_tlbs == 1)
306 max *= 2;
307 for (nr = 0; nr < max; nr++) {
308 tlb = &env->tlb[nr].tlb6;
309 pte_invalidate(&tlb->pte0);
311 tlb_flush(env, 1);
314 static always_inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
315 target_ulong eaddr,
316 int is_code,
317 int match_epn)
319 #if !defined(FLUSH_ALL_TLBS)
320 ppc6xx_tlb_t *tlb;
321 int way, nr;
323 /* Invalidate ITLB + DTLB, all ways */
324 for (way = 0; way < env->nb_ways; way++) {
325 nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
326 tlb = &env->tlb[nr].tlb6;
327 if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
328 #if defined (DEBUG_SOFTWARE_TLB)
329 if (loglevel != 0) {
330 fprintf(logfile, "TLB invalidate %d/%d " ADDRX "\n",
331 nr, env->nb_tlb, eaddr);
333 #endif
334 pte_invalidate(&tlb->pte0);
335 tlb_flush_page(env, tlb->EPN);
338 #else
339 /* XXX: PowerPC specification say this is valid as well */
340 ppc6xx_tlb_invalidate_all(env);
341 #endif
344 static always_inline void ppc6xx_tlb_invalidate_virt (CPUState *env,
345 target_ulong eaddr,
346 int is_code)
348 __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
351 void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
352 target_ulong pte0, target_ulong pte1)
354 ppc6xx_tlb_t *tlb;
355 int nr;
357 nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
358 tlb = &env->tlb[nr].tlb6;
359 #if defined (DEBUG_SOFTWARE_TLB)
360 if (loglevel != 0) {
361 fprintf(logfile, "Set TLB %d/%d EPN " ADDRX " PTE0 " ADDRX
362 " PTE1 " ADDRX "\n", nr, env->nb_tlb, EPN, pte0, pte1);
364 #endif
365 /* Invalidate any pending reference in Qemu for this virtual address */
366 __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
367 tlb->pte0 = pte0;
368 tlb->pte1 = pte1;
369 tlb->EPN = EPN;
370 /* Store last way for LRU mechanism */
371 env->last_way = way;
374 static always_inline int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
375 target_ulong eaddr, int rw,
376 int access_type)
378 ppc6xx_tlb_t *tlb;
379 int nr, best, way;
380 int ret;
382 best = -1;
383 ret = -1; /* No TLB found */
384 for (way = 0; way < env->nb_ways; way++) {
385 nr = ppc6xx_tlb_getnum(env, eaddr, way,
386 access_type == ACCESS_CODE ? 1 : 0);
387 tlb = &env->tlb[nr].tlb6;
388 /* This test "emulates" the PTE index match for hardware TLBs */
389 if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
390 #if defined (DEBUG_SOFTWARE_TLB)
391 if (loglevel != 0) {
392 fprintf(logfile, "TLB %d/%d %s [" ADDRX " " ADDRX
393 "] <> " ADDRX "\n",
394 nr, env->nb_tlb,
395 pte_is_valid(tlb->pte0) ? "valid" : "inval",
396 tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
398 #endif
399 continue;
401 #if defined (DEBUG_SOFTWARE_TLB)
402 if (loglevel != 0) {
403 fprintf(logfile, "TLB %d/%d %s " ADDRX " <> " ADDRX " " ADDRX
404 " %c %c\n",
405 nr, env->nb_tlb,
406 pte_is_valid(tlb->pte0) ? "valid" : "inval",
407 tlb->EPN, eaddr, tlb->pte1,
408 rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
410 #endif
411 switch (pte32_check(ctx, tlb->pte0, tlb->pte1, 0, rw, access_type)) {
412 case -3:
413 /* TLB inconsistency */
414 return -1;
415 case -2:
416 /* Access violation */
417 ret = -2;
418 best = nr;
419 break;
420 case -1:
421 default:
422 /* No match */
423 break;
424 case 0:
425 /* access granted */
426 /* XXX: we should go on looping to check all TLBs consistency
427 * but we can speed-up the whole thing as the
428 * result would be undefined if TLBs are not consistent.
430 ret = 0;
431 best = nr;
432 goto done;
435 if (best != -1) {
436 done:
437 #if defined (DEBUG_SOFTWARE_TLB)
438 if (loglevel != 0) {
439 fprintf(logfile, "found TLB at addr 0x%08lx prot=0x%01x ret=%d\n",
440 ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
442 #endif
443 /* Update page flags */
444 pte_update_flags(ctx, &env->tlb[best].tlb6.pte1, ret, rw);
447 return ret;
450 /* Perform BAT hit & translation */
451 static always_inline void bat_size_prot (CPUState *env, target_ulong *blp,
452 int *validp, int *protp,
453 target_ulong *BATu, target_ulong *BATl)
455 target_ulong bl;
456 int pp, valid, prot;
458 bl = (*BATu & 0x00001FFC) << 15;
459 valid = 0;
460 prot = 0;
461 if (((msr_pr == 0) && (*BATu & 0x00000002)) ||
462 ((msr_pr != 0) && (*BATu & 0x00000001))) {
463 valid = 1;
464 pp = *BATl & 0x00000003;
465 if (pp != 0) {
466 prot = PAGE_READ | PAGE_EXEC;
467 if (pp == 0x2)
468 prot |= PAGE_WRITE;
471 *blp = bl;
472 *validp = valid;
473 *protp = prot;
476 static always_inline void bat_601_size_prot (CPUState *env,target_ulong *blp,
477 int *validp, int *protp,
478 target_ulong *BATu,
479 target_ulong *BATl)
481 target_ulong bl;
482 int key, pp, valid, prot;
484 bl = (*BATl & 0x0000003F) << 17;
485 #if defined (DEBUG_BATS)
486 if (loglevel != 0) {
487 fprintf(logfile, "b %02x ==> bl %08x msk %08x\n",
488 *BATl & 0x0000003F, bl, ~bl);
490 #endif
491 prot = 0;
492 valid = (*BATl >> 6) & 1;
493 if (valid) {
494 pp = *BATu & 0x00000003;
495 if (msr_pr == 0)
496 key = (*BATu >> 3) & 1;
497 else
498 key = (*BATu >> 2) & 1;
499 prot = pp_check(key, pp, 0);
501 *blp = bl;
502 *validp = valid;
503 *protp = prot;
506 static always_inline int get_bat (CPUState *env, mmu_ctx_t *ctx,
507 target_ulong virtual, int rw, int type)
509 target_ulong *BATlt, *BATut, *BATu, *BATl;
510 target_ulong base, BEPIl, BEPIu, bl;
511 int i, valid, prot;
512 int ret = -1;
514 #if defined (DEBUG_BATS)
515 if (loglevel != 0) {
516 fprintf(logfile, "%s: %cBAT v 0x" ADDRX "\n", __func__,
517 type == ACCESS_CODE ? 'I' : 'D', virtual);
519 #endif
520 switch (type) {
521 case ACCESS_CODE:
522 BATlt = env->IBAT[1];
523 BATut = env->IBAT[0];
524 break;
525 default:
526 BATlt = env->DBAT[1];
527 BATut = env->DBAT[0];
528 break;
530 #if defined (DEBUG_BATS)
531 if (loglevel != 0) {
532 fprintf(logfile, "%s...: %cBAT v 0x" ADDRX "\n", __func__,
533 type == ACCESS_CODE ? 'I' : 'D', virtual);
535 #endif
536 base = virtual & 0xFFFC0000;
537 for (i = 0; i < env->nb_BATs; i++) {
538 BATu = &BATut[i];
539 BATl = &BATlt[i];
540 BEPIu = *BATu & 0xF0000000;
541 BEPIl = *BATu & 0x0FFE0000;
542 if (unlikely(env->mmu_model == POWERPC_MMU_601)) {
543 bat_601_size_prot(env, &bl, &valid, &prot, BATu, BATl);
544 } else {
545 bat_size_prot(env, &bl, &valid, &prot, BATu, BATl);
547 #if defined (DEBUG_BATS)
548 if (loglevel != 0) {
549 fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
550 " BATl 0x" ADDRX "\n",
551 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
552 *BATu, *BATl);
554 #endif
555 if ((virtual & 0xF0000000) == BEPIu &&
556 ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
557 /* BAT matches */
558 if (valid != 0) {
559 /* Get physical address */
560 ctx->raddr = (*BATl & 0xF0000000) |
561 ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
562 (virtual & 0x0001F000);
563 /* Compute access rights */
564 ctx->prot = prot;
565 ret = check_prot(ctx->prot, rw, type);
566 #if defined (DEBUG_BATS)
567 if (ret == 0 && loglevel != 0) {
568 fprintf(logfile, "BAT %d match: r 0x" PADDRX
569 " prot=%c%c\n",
570 i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
571 ctx->prot & PAGE_WRITE ? 'W' : '-');
573 #endif
574 break;
578 if (ret < 0) {
579 #if defined (DEBUG_BATS)
580 if (loglevel != 0) {
581 fprintf(logfile, "no BAT match for 0x" ADDRX ":\n", virtual);
582 for (i = 0; i < 4; i++) {
583 BATu = &BATut[i];
584 BATl = &BATlt[i];
585 BEPIu = *BATu & 0xF0000000;
586 BEPIl = *BATu & 0x0FFE0000;
587 bl = (*BATu & 0x00001FFC) << 15;
588 fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
589 " BATl 0x" ADDRX " \n\t"
590 "0x" ADDRX " 0x" ADDRX " 0x" ADDRX "\n",
591 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
592 *BATu, *BATl, BEPIu, BEPIl, bl);
595 #endif
598 /* No hit */
599 return ret;
602 /* PTE table lookup */
603 static always_inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h,
604 int rw, int type)
606 target_ulong base, pte0, pte1;
607 int i, good = -1;
608 int ret, r;
610 ret = -1; /* No entry found */
611 base = ctx->pg_addr[h];
612 for (i = 0; i < 8; i++) {
613 #if defined(TARGET_PPC64)
614 if (is_64b) {
615 pte0 = ldq_phys(base + (i * 16));
616 pte1 = ldq_phys(base + (i * 16) + 8);
617 r = pte64_check(ctx, pte0, pte1, h, rw, type);
618 #if defined (DEBUG_MMU)
619 if (loglevel != 0) {
620 fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
621 " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
622 base + (i * 16), pte0, pte1,
623 (int)(pte0 & 1), h, (int)((pte0 >> 1) & 1),
624 ctx->ptem);
626 #endif
627 } else
628 #endif
630 pte0 = ldl_phys(base + (i * 8));
631 pte1 = ldl_phys(base + (i * 8) + 4);
632 r = pte32_check(ctx, pte0, pte1, h, rw, type);
633 #if defined (DEBUG_MMU)
634 if (loglevel != 0) {
635 fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
636 " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
637 base + (i * 8), pte0, pte1,
638 (int)(pte0 >> 31), h, (int)((pte0 >> 6) & 1),
639 ctx->ptem);
641 #endif
643 switch (r) {
644 case -3:
645 /* PTE inconsistency */
646 return -1;
647 case -2:
648 /* Access violation */
649 ret = -2;
650 good = i;
651 break;
652 case -1:
653 default:
654 /* No PTE match */
655 break;
656 case 0:
657 /* access granted */
658 /* XXX: we should go on looping to check all PTEs consistency
659 * but if we can speed-up the whole thing as the
660 * result would be undefined if PTEs are not consistent.
662 ret = 0;
663 good = i;
664 goto done;
667 if (good != -1) {
668 done:
669 #if defined (DEBUG_MMU)
670 if (loglevel != 0) {
671 fprintf(logfile, "found PTE at addr 0x" PADDRX " prot=0x%01x "
672 "ret=%d\n",
673 ctx->raddr, ctx->prot, ret);
675 #endif
676 /* Update page flags */
677 pte1 = ctx->raddr;
678 if (pte_update_flags(ctx, &pte1, ret, rw) == 1) {
679 #if defined(TARGET_PPC64)
680 if (is_64b) {
681 stq_phys_notdirty(base + (good * 16) + 8, pte1);
682 } else
683 #endif
685 stl_phys_notdirty(base + (good * 8) + 4, pte1);
690 return ret;
693 static always_inline int find_pte32 (mmu_ctx_t *ctx, int h, int rw, int type)
695 return _find_pte(ctx, 0, h, rw, type);
698 #if defined(TARGET_PPC64)
699 static always_inline int find_pte64 (mmu_ctx_t *ctx, int h, int rw, int type)
701 return _find_pte(ctx, 1, h, rw, type);
703 #endif
705 static always_inline int find_pte (CPUState *env, mmu_ctx_t *ctx,
706 int h, int rw, int type)
708 #if defined(TARGET_PPC64)
709 if (env->mmu_model & POWERPC_MMU_64)
710 return find_pte64(ctx, h, rw, type);
711 #endif
713 return find_pte32(ctx, h, rw, type);
716 #if defined(TARGET_PPC64)
717 static always_inline int slb_is_valid (uint64_t slb64)
719 return slb64 & 0x0000000008000000ULL ? 1 : 0;
722 static always_inline void slb_invalidate (uint64_t *slb64)
724 *slb64 &= ~0x0000000008000000ULL;
727 static always_inline int slb_lookup (CPUPPCState *env, target_ulong eaddr,
728 target_ulong *vsid,
729 target_ulong *page_mask, int *attr)
731 target_phys_addr_t sr_base;
732 target_ulong mask;
733 uint64_t tmp64;
734 uint32_t tmp;
735 int n, ret;
737 ret = -5;
738 sr_base = env->spr[SPR_ASR];
739 #if defined(DEBUG_SLB)
740 if (loglevel != 0) {
741 fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
742 __func__, eaddr, sr_base);
744 #endif
745 mask = 0x0000000000000000ULL; /* Avoid gcc warning */
746 for (n = 0; n < env->slb_nr; n++) {
747 tmp64 = ldq_phys(sr_base);
748 tmp = ldl_phys(sr_base + 8);
749 #if defined(DEBUG_SLB)
750 if (loglevel != 0) {
751 fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08"
752 PRIx32 "\n", __func__, n, sr_base, tmp64, tmp);
754 #endif
755 if (slb_is_valid(tmp64)) {
756 /* SLB entry is valid */
757 switch (tmp64 & 0x0000000006000000ULL) {
758 case 0x0000000000000000ULL:
759 /* 256 MB segment */
760 mask = 0xFFFFFFFFF0000000ULL;
761 break;
762 case 0x0000000002000000ULL:
763 /* 1 TB segment */
764 mask = 0xFFFF000000000000ULL;
765 break;
766 case 0x0000000004000000ULL:
767 case 0x0000000006000000ULL:
768 /* Reserved => segment is invalid */
769 continue;
771 if ((eaddr & mask) == (tmp64 & mask)) {
772 /* SLB match */
773 *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
774 *page_mask = ~mask;
775 *attr = tmp & 0xFF;
776 ret = n;
777 break;
780 sr_base += 12;
783 return ret;
786 void ppc_slb_invalidate_all (CPUPPCState *env)
788 target_phys_addr_t sr_base;
789 uint64_t tmp64;
790 int n, do_invalidate;
792 do_invalidate = 0;
793 sr_base = env->spr[SPR_ASR];
794 /* XXX: Warning: slbia never invalidates the first segment */
795 for (n = 1; n < env->slb_nr; n++) {
796 tmp64 = ldq_phys(sr_base);
797 if (slb_is_valid(tmp64)) {
798 slb_invalidate(&tmp64);
799 stq_phys(sr_base, tmp64);
800 /* XXX: given the fact that segment size is 256 MB or 1TB,
801 * and we still don't have a tlb_flush_mask(env, n, mask)
802 * in Qemu, we just invalidate all TLBs
804 do_invalidate = 1;
806 sr_base += 12;
808 if (do_invalidate)
809 tlb_flush(env, 1);
812 void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
814 target_phys_addr_t sr_base;
815 target_ulong vsid, page_mask;
816 uint64_t tmp64;
817 int attr;
818 int n;
820 n = slb_lookup(env, T0, &vsid, &page_mask, &attr);
821 if (n >= 0) {
822 sr_base = env->spr[SPR_ASR];
823 sr_base += 12 * n;
824 tmp64 = ldq_phys(sr_base);
825 if (slb_is_valid(tmp64)) {
826 slb_invalidate(&tmp64);
827 stq_phys(sr_base, tmp64);
828 /* XXX: given the fact that segment size is 256 MB or 1TB,
829 * and we still don't have a tlb_flush_mask(env, n, mask)
830 * in Qemu, we just invalidate all TLBs
832 tlb_flush(env, 1);
837 target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
839 target_phys_addr_t sr_base;
840 target_ulong rt;
841 uint64_t tmp64;
842 uint32_t tmp;
844 sr_base = env->spr[SPR_ASR];
845 sr_base += 12 * slb_nr;
846 tmp64 = ldq_phys(sr_base);
847 tmp = ldl_phys(sr_base + 8);
848 if (tmp64 & 0x0000000008000000ULL) {
849 /* SLB entry is valid */
850 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
851 rt = tmp >> 8; /* 65:88 => 40:63 */
852 rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
853 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
854 rt |= ((tmp >> 4) & 0xF) << 27;
855 } else {
856 rt = 0;
858 #if defined(DEBUG_SLB)
859 if (loglevel != 0) {
860 fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
861 ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
863 #endif
865 return rt;
868 void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
870 target_phys_addr_t sr_base;
871 uint64_t tmp64;
872 uint32_t tmp;
874 sr_base = env->spr[SPR_ASR];
875 sr_base += 12 * slb_nr;
876 /* Copy Rs bits 37:63 to SLB 62:88 */
877 tmp = rs << 8;
878 tmp64 = (rs >> 24) & 0x7;
879 /* Copy Rs bits 33:36 to SLB 89:92 */
880 tmp |= ((rs >> 27) & 0xF) << 4;
881 /* Set the valid bit */
882 tmp64 |= 1 << 27;
883 /* Set ESID */
884 tmp64 |= (uint32_t)slb_nr << 28;
885 #if defined(DEBUG_SLB)
886 if (loglevel != 0) {
887 fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64 " %08"
888 PRIx32 "\n", __func__, slb_nr, rs, sr_base, tmp64, tmp);
890 #endif
891 /* Write SLB entry to memory */
892 stq_phys(sr_base, tmp64);
893 stl_phys(sr_base + 8, tmp);
895 #endif /* defined(TARGET_PPC64) */
897 /* Perform segment based translation */
898 static always_inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
899 int sdr_sh,
900 target_phys_addr_t hash,
901 target_phys_addr_t mask)
903 return (sdr1 & ((target_phys_addr_t)(-1ULL) << sdr_sh)) | (hash & mask);
906 static always_inline int get_segment (CPUState *env, mmu_ctx_t *ctx,
907 target_ulong eaddr, int rw, int type)
909 target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
910 target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
911 #if defined(TARGET_PPC64)
912 int attr;
913 #endif
914 int ds, vsid_sh, sdr_sh, pr;
915 int ret, ret2;
917 pr = msr_pr;
918 #if defined(TARGET_PPC64)
919 if (env->mmu_model & POWERPC_MMU_64) {
920 #if defined (DEBUG_MMU)
921 if (loglevel != 0) {
922 fprintf(logfile, "Check SLBs\n");
924 #endif
925 ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
926 if (ret < 0)
927 return ret;
928 ctx->key = ((attr & 0x40) && (pr != 0)) ||
929 ((attr & 0x80) && (pr == 0)) ? 1 : 0;
930 ds = 0;
931 ctx->nx = attr & 0x20 ? 1 : 0;
932 vsid_mask = 0x00003FFFFFFFFF80ULL;
933 vsid_sh = 7;
934 sdr_sh = 18;
935 sdr_mask = 0x3FF80;
936 } else
937 #endif /* defined(TARGET_PPC64) */
939 sr = env->sr[eaddr >> 28];
940 page_mask = 0x0FFFFFFF;
941 ctx->key = (((sr & 0x20000000) && (pr != 0)) ||
942 ((sr & 0x40000000) && (pr == 0))) ? 1 : 0;
943 ds = sr & 0x80000000 ? 1 : 0;
944 ctx->nx = sr & 0x10000000 ? 1 : 0;
945 vsid = sr & 0x00FFFFFF;
946 vsid_mask = 0x01FFFFC0;
947 vsid_sh = 6;
948 sdr_sh = 16;
949 sdr_mask = 0xFFC0;
950 #if defined (DEBUG_MMU)
951 if (loglevel != 0) {
952 fprintf(logfile, "Check segment v=0x" ADDRX " %d 0x" ADDRX
953 " nip=0x" ADDRX " lr=0x" ADDRX
954 " ir=%d dr=%d pr=%d %d t=%d\n",
955 eaddr, (int)(eaddr >> 28), sr, env->nip,
956 env->lr, (int)msr_ir, (int)msr_dr, pr != 0 ? 1 : 0,
957 rw, type);
959 #endif
961 #if defined (DEBUG_MMU)
962 if (loglevel != 0) {
963 fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
964 ctx->key, ds, ctx->nx, vsid);
966 #endif
967 ret = -1;
968 if (!ds) {
969 /* Check if instruction fetch is allowed, if needed */
970 if (type != ACCESS_CODE || ctx->nx == 0) {
971 /* Page address translation */
972 /* Primary table address */
973 sdr = env->sdr1;
974 pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
975 #if defined(TARGET_PPC64)
976 if (env->mmu_model & POWERPC_MMU_64) {
977 htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
978 /* XXX: this is false for 1 TB segments */
979 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
980 } else
981 #endif
983 htab_mask = sdr & 0x000001FF;
984 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
986 mask = (htab_mask << sdr_sh) | sdr_mask;
987 #if defined (DEBUG_MMU)
988 if (loglevel != 0) {
989 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
990 PADDRX " " ADDRX "\n", sdr, sdr_sh, hash, mask,
991 page_mask);
993 #endif
994 ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
995 /* Secondary table address */
996 hash = (~hash) & vsid_mask;
997 #if defined (DEBUG_MMU)
998 if (loglevel != 0) {
999 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
1000 PADDRX "\n", sdr, sdr_sh, hash, mask);
1002 #endif
1003 ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
1004 #if defined(TARGET_PPC64)
1005 if (env->mmu_model & POWERPC_MMU_64) {
1006 /* Only 5 bits of the page index are used in the AVPN */
1007 ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
1008 } else
1009 #endif
1011 ctx->ptem = (vsid << 7) | (pgidx >> 10);
1013 /* Initialize real address with an invalid value */
1014 ctx->raddr = (target_phys_addr_t)-1ULL;
1015 if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
1016 env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
1017 /* Software TLB search */
1018 ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
1019 } else {
1020 #if defined (DEBUG_MMU)
1021 if (loglevel != 0) {
1022 fprintf(logfile, "0 sdr1=0x" PADDRX " vsid=0x%06x "
1023 "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX "\n",
1024 sdr, (uint32_t)vsid, (uint32_t)pgidx,
1025 (uint32_t)hash, ctx->pg_addr[0]);
1027 #endif
1028 /* Primary table lookup */
1029 ret = find_pte(env, ctx, 0, rw, type);
1030 if (ret < 0) {
1031 /* Secondary table lookup */
1032 #if defined (DEBUG_MMU)
1033 if (eaddr != 0xEFFFFFFF && loglevel != 0) {
1034 fprintf(logfile,
1035 "1 sdr1=0x" PADDRX " vsid=0x%06x api=0x%04x "
1036 "hash=0x%05x pg_addr=0x" PADDRX "\n",
1037 sdr, (uint32_t)vsid, (uint32_t)pgidx,
1038 (uint32_t)hash, ctx->pg_addr[1]);
1040 #endif
1041 ret2 = find_pte(env, ctx, 1, rw, type);
1042 if (ret2 != -1)
1043 ret = ret2;
1046 #if defined (DUMP_PAGE_TABLES)
1047 if (loglevel != 0) {
1048 target_phys_addr_t curaddr;
1049 uint32_t a0, a1, a2, a3;
1050 fprintf(logfile,
1051 "Page table: " PADDRX " len " PADDRX "\n",
1052 sdr, mask + 0x80);
1053 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
1054 curaddr += 16) {
1055 a0 = ldl_phys(curaddr);
1056 a1 = ldl_phys(curaddr + 4);
1057 a2 = ldl_phys(curaddr + 8);
1058 a3 = ldl_phys(curaddr + 12);
1059 if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
1060 fprintf(logfile,
1061 PADDRX ": %08x %08x %08x %08x\n",
1062 curaddr, a0, a1, a2, a3);
1066 #endif
1067 } else {
1068 #if defined (DEBUG_MMU)
1069 if (loglevel != 0)
1070 fprintf(logfile, "No access allowed\n");
1071 #endif
1072 ret = -3;
1074 } else {
1075 #if defined (DEBUG_MMU)
1076 if (loglevel != 0)
1077 fprintf(logfile, "direct store...\n");
1078 #endif
1079 /* Direct-store segment : absolutely *BUGGY* for now */
1080 switch (type) {
1081 case ACCESS_INT:
1082 /* Integer load/store : only access allowed */
1083 break;
1084 case ACCESS_CODE:
1085 /* No code fetch is allowed in direct-store areas */
1086 return -4;
1087 case ACCESS_FLOAT:
1088 /* Floating point load/store */
1089 return -4;
1090 case ACCESS_RES:
1091 /* lwarx, ldarx or srwcx. */
1092 return -4;
1093 case ACCESS_CACHE:
1094 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1095 /* Should make the instruction do no-op.
1096 * As it already do no-op, it's quite easy :-)
1098 ctx->raddr = eaddr;
1099 return 0;
1100 case ACCESS_EXT:
1101 /* eciwx or ecowx */
1102 return -4;
1103 default:
1104 if (logfile) {
1105 fprintf(logfile, "ERROR: instruction should not need "
1106 "address translation\n");
1108 return -4;
1110 if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
1111 ctx->raddr = eaddr;
1112 ret = 2;
1113 } else {
1114 ret = -2;
1118 return ret;
1121 /* Generic TLB check function for embedded PowerPC implementations */
1122 static always_inline int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
1123 target_phys_addr_t *raddrp,
1124 target_ulong address,
1125 uint32_t pid, int ext, int i)
1127 target_ulong mask;
1129 /* Check valid flag */
1130 if (!(tlb->prot & PAGE_VALID)) {
1131 if (loglevel != 0)
1132 fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
1133 return -1;
1135 mask = ~(tlb->size - 1);
1136 #if defined (DEBUG_SOFTWARE_TLB)
1137 if (loglevel != 0) {
1138 fprintf(logfile, "%s: TLB %d address " ADDRX " PID %d <=> "
1139 ADDRX " " ADDRX " %d\n",
1140 __func__, i, address, pid, tlb->EPN, mask, (int)tlb->PID);
1142 #endif
1143 /* Check PID */
1144 if (tlb->PID != 0 && tlb->PID != pid)
1145 return -1;
1146 /* Check effective address */
1147 if ((address & mask) != tlb->EPN)
1148 return -1;
1149 *raddrp = (tlb->RPN & mask) | (address & ~mask);
1150 #if (TARGET_PHYS_ADDR_BITS >= 36)
1151 if (ext) {
1152 /* Extend the physical address to 36 bits */
1153 *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
1155 #endif
1157 return 0;
1160 /* Generic TLB search function for PowerPC embedded implementations */
1161 int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
1163 ppcemb_tlb_t *tlb;
1164 target_phys_addr_t raddr;
1165 int i, ret;
1167 /* Default return value is no match */
1168 ret = -1;
1169 for (i = 0; i < env->nb_tlb; i++) {
1170 tlb = &env->tlb[i].tlbe;
1171 if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
1172 ret = i;
1173 break;
1177 return ret;
1180 /* Helpers specific to PowerPC 40x implementations */
1181 static always_inline void ppc4xx_tlb_invalidate_all (CPUState *env)
1183 ppcemb_tlb_t *tlb;
1184 int i;
1186 for (i = 0; i < env->nb_tlb; i++) {
1187 tlb = &env->tlb[i].tlbe;
1188 tlb->prot &= ~PAGE_VALID;
1190 tlb_flush(env, 1);
1193 static always_inline void ppc4xx_tlb_invalidate_virt (CPUState *env,
1194 target_ulong eaddr,
1195 uint32_t pid)
1197 #if !defined(FLUSH_ALL_TLBS)
1198 ppcemb_tlb_t *tlb;
1199 target_phys_addr_t raddr;
1200 target_ulong page, end;
1201 int i;
1203 for (i = 0; i < env->nb_tlb; i++) {
1204 tlb = &env->tlb[i].tlbe;
1205 if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
1206 end = tlb->EPN + tlb->size;
1207 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1208 tlb_flush_page(env, page);
1209 tlb->prot &= ~PAGE_VALID;
1210 break;
1213 #else
1214 ppc4xx_tlb_invalidate_all(env);
1215 #endif
1218 int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1219 target_ulong address, int rw, int access_type)
1221 ppcemb_tlb_t *tlb;
1222 target_phys_addr_t raddr;
1223 int i, ret, zsel, zpr, pr;
1225 ret = -1;
1226 raddr = (target_phys_addr_t)-1ULL;
1227 pr = msr_pr;
1228 for (i = 0; i < env->nb_tlb; i++) {
1229 tlb = &env->tlb[i].tlbe;
1230 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1231 env->spr[SPR_40x_PID], 0, i) < 0)
1232 continue;
1233 zsel = (tlb->attr >> 4) & 0xF;
1234 zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
1235 #if defined (DEBUG_SOFTWARE_TLB)
1236 if (loglevel != 0) {
1237 fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1238 __func__, i, zsel, zpr, rw, tlb->attr);
1240 #endif
1241 /* Check execute enable bit */
1242 switch (zpr) {
1243 case 0x2:
1244 if (pr != 0)
1245 goto check_perms;
1246 /* No break here */
1247 case 0x3:
1248 /* All accesses granted */
1249 ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1250 ret = 0;
1251 break;
1252 case 0x0:
1253 if (pr != 0) {
1254 ctx->prot = 0;
1255 ret = -2;
1256 break;
1258 /* No break here */
1259 case 0x1:
1260 check_perms:
1261 /* Check from TLB entry */
1262 /* XXX: there is a problem here or in the TLB fill code... */
1263 ctx->prot = tlb->prot;
1264 ctx->prot |= PAGE_EXEC;
1265 ret = check_prot(ctx->prot, rw, access_type);
1266 break;
1268 if (ret >= 0) {
1269 ctx->raddr = raddr;
1270 #if defined (DEBUG_SOFTWARE_TLB)
1271 if (loglevel != 0) {
1272 fprintf(logfile, "%s: access granted " ADDRX " => " REGX
1273 " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1274 ret);
1276 #endif
1277 return 0;
1280 #if defined (DEBUG_SOFTWARE_TLB)
1281 if (loglevel != 0) {
1282 fprintf(logfile, "%s: access refused " ADDRX " => " REGX
1283 " %d %d\n", __func__, address, raddr, ctx->prot,
1284 ret);
1286 #endif
1288 return ret;
1291 void store_40x_sler (CPUPPCState *env, uint32_t val)
1293 /* XXX: TO BE FIXED */
1294 if (val != 0x00000000) {
1295 cpu_abort(env, "Little-endian regions are not supported by now\n");
1297 env->spr[SPR_405_SLER] = val;
1300 int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1301 target_ulong address, int rw,
1302 int access_type)
1304 ppcemb_tlb_t *tlb;
1305 target_phys_addr_t raddr;
1306 int i, prot, ret;
1308 ret = -1;
1309 raddr = (target_phys_addr_t)-1ULL;
1310 for (i = 0; i < env->nb_tlb; i++) {
1311 tlb = &env->tlb[i].tlbe;
1312 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1313 env->spr[SPR_BOOKE_PID], 1, i) < 0)
1314 continue;
1315 if (msr_pr != 0)
1316 prot = tlb->prot & 0xF;
1317 else
1318 prot = (tlb->prot >> 4) & 0xF;
1319 /* Check the address space */
1320 if (access_type == ACCESS_CODE) {
1321 if (msr_ir != (tlb->attr & 1))
1322 continue;
1323 ctx->prot = prot;
1324 if (prot & PAGE_EXEC) {
1325 ret = 0;
1326 break;
1328 ret = -3;
1329 } else {
1330 if (msr_dr != (tlb->attr & 1))
1331 continue;
1332 ctx->prot = prot;
1333 if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1334 ret = 0;
1335 break;
1337 ret = -2;
1340 if (ret >= 0)
1341 ctx->raddr = raddr;
1343 return ret;
1346 static always_inline int check_physical (CPUState *env, mmu_ctx_t *ctx,
1347 target_ulong eaddr, int rw)
1349 int in_plb, ret;
1351 ctx->raddr = eaddr;
1352 ctx->prot = PAGE_READ | PAGE_EXEC;
1353 ret = 0;
1354 switch (env->mmu_model) {
1355 case POWERPC_MMU_32B:
1356 case POWERPC_MMU_601:
1357 case POWERPC_MMU_SOFT_6xx:
1358 case POWERPC_MMU_SOFT_74xx:
1359 case POWERPC_MMU_SOFT_4xx:
1360 case POWERPC_MMU_REAL:
1361 case POWERPC_MMU_BOOKE:
1362 ctx->prot |= PAGE_WRITE;
1363 break;
1364 #if defined(TARGET_PPC64)
1365 case POWERPC_MMU_620:
1366 case POWERPC_MMU_64B:
1367 /* Real address are 60 bits long */
1368 ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1369 ctx->prot |= PAGE_WRITE;
1370 break;
1371 #endif
1372 case POWERPC_MMU_SOFT_4xx_Z:
1373 if (unlikely(msr_pe != 0)) {
1374 /* 403 family add some particular protections,
1375 * using PBL/PBU registers for accesses with no translation.
1377 in_plb =
1378 /* Check PLB validity */
1379 (env->pb[0] < env->pb[1] &&
1380 /* and address in plb area */
1381 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1382 (env->pb[2] < env->pb[3] &&
1383 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1384 if (in_plb ^ msr_px) {
1385 /* Access in protected area */
1386 if (rw == 1) {
1387 /* Access is not allowed */
1388 ret = -2;
1390 } else {
1391 /* Read-write access is allowed */
1392 ctx->prot |= PAGE_WRITE;
1395 break;
1396 case POWERPC_MMU_MPC8xx:
1397 /* XXX: TODO */
1398 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1399 break;
1400 case POWERPC_MMU_BOOKE_FSL:
1401 /* XXX: TODO */
1402 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1403 break;
1404 default:
1405 cpu_abort(env, "Unknown or invalid MMU model\n");
1406 return -1;
1409 return ret;
1412 int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1413 int rw, int access_type)
1415 int ret;
1417 #if 0
1418 if (loglevel != 0) {
1419 fprintf(logfile, "%s\n", __func__);
1421 #endif
1422 if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1423 (access_type != ACCESS_CODE && msr_dr == 0)) {
1424 /* No address translation */
1425 ret = check_physical(env, ctx, eaddr, rw);
1426 } else {
1427 ret = -1;
1428 switch (env->mmu_model) {
1429 case POWERPC_MMU_32B:
1430 case POWERPC_MMU_601:
1431 case POWERPC_MMU_SOFT_6xx:
1432 case POWERPC_MMU_SOFT_74xx:
1433 #if defined(TARGET_PPC64)
1434 case POWERPC_MMU_620:
1435 case POWERPC_MMU_64B:
1436 #endif
1437 /* Try to find a BAT */
1438 if (env->nb_BATs != 0)
1439 ret = get_bat(env, ctx, eaddr, rw, access_type);
1440 if (ret < 0) {
1441 /* We didn't match any BAT entry or don't have BATs */
1442 ret = get_segment(env, ctx, eaddr, rw, access_type);
1444 break;
1445 case POWERPC_MMU_SOFT_4xx:
1446 case POWERPC_MMU_SOFT_4xx_Z:
1447 ret = mmu40x_get_physical_address(env, ctx, eaddr,
1448 rw, access_type);
1449 break;
1450 case POWERPC_MMU_BOOKE:
1451 ret = mmubooke_get_physical_address(env, ctx, eaddr,
1452 rw, access_type);
1453 break;
1454 case POWERPC_MMU_MPC8xx:
1455 /* XXX: TODO */
1456 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1457 break;
1458 case POWERPC_MMU_BOOKE_FSL:
1459 /* XXX: TODO */
1460 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1461 return -1;
1462 case POWERPC_MMU_REAL:
1463 cpu_abort(env, "PowerPC in real mode do not do any translation\n");
1464 return -1;
1465 default:
1466 cpu_abort(env, "Unknown or invalid MMU model\n");
1467 return -1;
1470 #if 0
1471 if (loglevel != 0) {
1472 fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1473 __func__, eaddr, ret, ctx->raddr);
1475 #endif
1477 return ret;
1480 target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1482 mmu_ctx_t ctx;
1484 if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT) != 0))
1485 return -1;
1487 return ctx.raddr & TARGET_PAGE_MASK;
1490 /* Perform address translation */
1491 int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1492 int mmu_idx, int is_softmmu)
1494 mmu_ctx_t ctx;
1495 int access_type;
1496 int ret = 0;
1498 if (rw == 2) {
1499 /* code access */
1500 rw = 0;
1501 access_type = ACCESS_CODE;
1502 } else {
1503 /* data access */
1504 /* XXX: put correct access by using cpu_restore_state()
1505 correctly */
1506 access_type = ACCESS_INT;
1507 // access_type = env->access_type;
1509 ret = get_physical_address(env, &ctx, address, rw, access_type);
1510 if (ret == 0) {
1511 ret = tlb_set_page_exec(env, address & TARGET_PAGE_MASK,
1512 ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1513 mmu_idx, is_softmmu);
1514 } else if (ret < 0) {
1515 #if defined (DEBUG_MMU)
1516 if (loglevel != 0)
1517 cpu_dump_state(env, logfile, fprintf, 0);
1518 #endif
1519 if (access_type == ACCESS_CODE) {
1520 switch (ret) {
1521 case -1:
1522 /* No matches in page tables or TLB */
1523 switch (env->mmu_model) {
1524 case POWERPC_MMU_SOFT_6xx:
1525 env->exception_index = POWERPC_EXCP_IFTLB;
1526 env->error_code = 1 << 18;
1527 env->spr[SPR_IMISS] = address;
1528 env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1529 goto tlb_miss;
1530 case POWERPC_MMU_SOFT_74xx:
1531 env->exception_index = POWERPC_EXCP_IFTLB;
1532 goto tlb_miss_74xx;
1533 case POWERPC_MMU_SOFT_4xx:
1534 case POWERPC_MMU_SOFT_4xx_Z:
1535 env->exception_index = POWERPC_EXCP_ITLB;
1536 env->error_code = 0;
1537 env->spr[SPR_40x_DEAR] = address;
1538 env->spr[SPR_40x_ESR] = 0x00000000;
1539 break;
1540 case POWERPC_MMU_32B:
1541 case POWERPC_MMU_601:
1542 #if defined(TARGET_PPC64)
1543 case POWERPC_MMU_620:
1544 case POWERPC_MMU_64B:
1545 #endif
1546 env->exception_index = POWERPC_EXCP_ISI;
1547 env->error_code = 0x40000000;
1548 break;
1549 case POWERPC_MMU_BOOKE:
1550 /* XXX: TODO */
1551 cpu_abort(env, "BookE MMU model is not implemented\n");
1552 return -1;
1553 case POWERPC_MMU_BOOKE_FSL:
1554 /* XXX: TODO */
1555 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
1556 return -1;
1557 case POWERPC_MMU_MPC8xx:
1558 /* XXX: TODO */
1559 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1560 break;
1561 case POWERPC_MMU_REAL:
1562 cpu_abort(env, "PowerPC in real mode should never raise "
1563 "any MMU exceptions\n");
1564 return -1;
1565 default:
1566 cpu_abort(env, "Unknown or invalid MMU model\n");
1567 return -1;
1569 break;
1570 case -2:
1571 /* Access rights violation */
1572 env->exception_index = POWERPC_EXCP_ISI;
1573 env->error_code = 0x08000000;
1574 break;
1575 case -3:
1576 /* No execute protection violation */
1577 env->exception_index = POWERPC_EXCP_ISI;
1578 env->error_code = 0x10000000;
1579 break;
1580 case -4:
1581 /* Direct store exception */
1582 /* No code fetch is allowed in direct-store areas */
1583 env->exception_index = POWERPC_EXCP_ISI;
1584 env->error_code = 0x10000000;
1585 break;
1586 #if defined(TARGET_PPC64)
1587 case -5:
1588 /* No match in segment table */
1589 if (env->mmu_model == POWERPC_MMU_620) {
1590 env->exception_index = POWERPC_EXCP_ISI;
1591 /* XXX: this might be incorrect */
1592 env->error_code = 0x40000000;
1593 } else {
1594 env->exception_index = POWERPC_EXCP_ISEG;
1595 env->error_code = 0;
1597 break;
1598 #endif
1600 } else {
1601 switch (ret) {
1602 case -1:
1603 /* No matches in page tables or TLB */
1604 switch (env->mmu_model) {
1605 case POWERPC_MMU_SOFT_6xx:
1606 if (rw == 1) {
1607 env->exception_index = POWERPC_EXCP_DSTLB;
1608 env->error_code = 1 << 16;
1609 } else {
1610 env->exception_index = POWERPC_EXCP_DLTLB;
1611 env->error_code = 0;
1613 env->spr[SPR_DMISS] = address;
1614 env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1615 tlb_miss:
1616 env->error_code |= ctx.key << 19;
1617 env->spr[SPR_HASH1] = ctx.pg_addr[0];
1618 env->spr[SPR_HASH2] = ctx.pg_addr[1];
1619 break;
1620 case POWERPC_MMU_SOFT_74xx:
1621 if (rw == 1) {
1622 env->exception_index = POWERPC_EXCP_DSTLB;
1623 } else {
1624 env->exception_index = POWERPC_EXCP_DLTLB;
1626 tlb_miss_74xx:
1627 /* Implement LRU algorithm */
1628 env->error_code = ctx.key << 19;
1629 env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1630 ((env->last_way + 1) & (env->nb_ways - 1));
1631 env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
1632 break;
1633 case POWERPC_MMU_SOFT_4xx:
1634 case POWERPC_MMU_SOFT_4xx_Z:
1635 env->exception_index = POWERPC_EXCP_DTLB;
1636 env->error_code = 0;
1637 env->spr[SPR_40x_DEAR] = address;
1638 if (rw)
1639 env->spr[SPR_40x_ESR] = 0x00800000;
1640 else
1641 env->spr[SPR_40x_ESR] = 0x00000000;
1642 break;
1643 case POWERPC_MMU_32B:
1644 case POWERPC_MMU_601:
1645 #if defined(TARGET_PPC64)
1646 case POWERPC_MMU_620:
1647 case POWERPC_MMU_64B:
1648 #endif
1649 env->exception_index = POWERPC_EXCP_DSI;
1650 env->error_code = 0;
1651 env->spr[SPR_DAR] = address;
1652 if (rw == 1)
1653 env->spr[SPR_DSISR] = 0x42000000;
1654 else
1655 env->spr[SPR_DSISR] = 0x40000000;
1656 break;
1657 case POWERPC_MMU_MPC8xx:
1658 /* XXX: TODO */
1659 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1660 break;
1661 case POWERPC_MMU_BOOKE:
1662 /* XXX: TODO */
1663 cpu_abort(env, "BookE MMU model is not implemented\n");
1664 return -1;
1665 case POWERPC_MMU_BOOKE_FSL:
1666 /* XXX: TODO */
1667 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
1668 return -1;
1669 case POWERPC_MMU_REAL:
1670 cpu_abort(env, "PowerPC in real mode should never raise "
1671 "any MMU exceptions\n");
1672 return -1;
1673 default:
1674 cpu_abort(env, "Unknown or invalid MMU model\n");
1675 return -1;
1677 break;
1678 case -2:
1679 /* Access rights violation */
1680 env->exception_index = POWERPC_EXCP_DSI;
1681 env->error_code = 0;
1682 env->spr[SPR_DAR] = address;
1683 if (rw == 1)
1684 env->spr[SPR_DSISR] = 0x0A000000;
1685 else
1686 env->spr[SPR_DSISR] = 0x08000000;
1687 break;
1688 case -4:
1689 /* Direct store exception */
1690 switch (access_type) {
1691 case ACCESS_FLOAT:
1692 /* Floating point load/store */
1693 env->exception_index = POWERPC_EXCP_ALIGN;
1694 env->error_code = POWERPC_EXCP_ALIGN_FP;
1695 env->spr[SPR_DAR] = address;
1696 break;
1697 case ACCESS_RES:
1698 /* lwarx, ldarx or stwcx. */
1699 env->exception_index = POWERPC_EXCP_DSI;
1700 env->error_code = 0;
1701 env->spr[SPR_DAR] = address;
1702 if (rw == 1)
1703 env->spr[SPR_DSISR] = 0x06000000;
1704 else
1705 env->spr[SPR_DSISR] = 0x04000000;
1706 break;
1707 case ACCESS_EXT:
1708 /* eciwx or ecowx */
1709 env->exception_index = POWERPC_EXCP_DSI;
1710 env->error_code = 0;
1711 env->spr[SPR_DAR] = address;
1712 if (rw == 1)
1713 env->spr[SPR_DSISR] = 0x06100000;
1714 else
1715 env->spr[SPR_DSISR] = 0x04100000;
1716 break;
1717 default:
1718 printf("DSI: invalid exception (%d)\n", ret);
1719 env->exception_index = POWERPC_EXCP_PROGRAM;
1720 env->error_code =
1721 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1722 env->spr[SPR_DAR] = address;
1723 break;
1725 break;
1726 #if defined(TARGET_PPC64)
1727 case -5:
1728 /* No match in segment table */
1729 if (env->mmu_model == POWERPC_MMU_620) {
1730 env->exception_index = POWERPC_EXCP_DSI;
1731 env->error_code = 0;
1732 env->spr[SPR_DAR] = address;
1733 /* XXX: this might be incorrect */
1734 if (rw == 1)
1735 env->spr[SPR_DSISR] = 0x42000000;
1736 else
1737 env->spr[SPR_DSISR] = 0x40000000;
1738 } else {
1739 env->exception_index = POWERPC_EXCP_DSEG;
1740 env->error_code = 0;
1741 env->spr[SPR_DAR] = address;
1743 break;
1744 #endif
1747 #if 0
1748 printf("%s: set exception to %d %02x\n", __func__,
1749 env->exception, env->error_code);
1750 #endif
1751 ret = 1;
1754 return ret;
1757 /*****************************************************************************/
1758 /* BATs management */
1759 #if !defined(FLUSH_ALL_TLBS)
1760 static always_inline void do_invalidate_BAT (CPUPPCState *env,
1761 target_ulong BATu,
1762 target_ulong mask)
1764 target_ulong base, end, page;
1766 base = BATu & ~0x0001FFFF;
1767 end = base + mask + 0x00020000;
1768 #if defined (DEBUG_BATS)
1769 if (loglevel != 0) {
1770 fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1771 base, end, mask);
1773 #endif
1774 for (page = base; page != end; page += TARGET_PAGE_SIZE)
1775 tlb_flush_page(env, page);
1776 #if defined (DEBUG_BATS)
1777 if (loglevel != 0)
1778 fprintf(logfile, "Flush done\n");
1779 #endif
1781 #endif
1783 static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1784 int ul, int nr, target_ulong value)
1786 #if defined (DEBUG_BATS)
1787 if (loglevel != 0) {
1788 fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1789 ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1791 #endif
1794 target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1796 return env->IBAT[0][nr];
1799 target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1801 return env->IBAT[1][nr];
1804 void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1806 target_ulong mask;
1808 dump_store_bat(env, 'I', 0, nr, value);
1809 if (env->IBAT[0][nr] != value) {
1810 mask = (value << 15) & 0x0FFE0000UL;
1811 #if !defined(FLUSH_ALL_TLBS)
1812 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1813 #endif
1814 /* When storing valid upper BAT, mask BEPI and BRPN
1815 * and invalidate all TLBs covered by this BAT
1817 mask = (value << 15) & 0x0FFE0000UL;
1818 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1819 (value & ~0x0001FFFFUL & ~mask);
1820 env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1821 (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1822 #if !defined(FLUSH_ALL_TLBS)
1823 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1824 #else
1825 tlb_flush(env, 1);
1826 #endif
1830 void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1832 dump_store_bat(env, 'I', 1, nr, value);
1833 env->IBAT[1][nr] = value;
1836 target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1838 return env->DBAT[0][nr];
1841 target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1843 return env->DBAT[1][nr];
1846 void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1848 target_ulong mask;
1850 dump_store_bat(env, 'D', 0, nr, value);
1851 if (env->DBAT[0][nr] != value) {
1852 /* When storing valid upper BAT, mask BEPI and BRPN
1853 * and invalidate all TLBs covered by this BAT
1855 mask = (value << 15) & 0x0FFE0000UL;
1856 #if !defined(FLUSH_ALL_TLBS)
1857 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1858 #endif
1859 mask = (value << 15) & 0x0FFE0000UL;
1860 env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1861 (value & ~0x0001FFFFUL & ~mask);
1862 env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1863 (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1864 #if !defined(FLUSH_ALL_TLBS)
1865 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1866 #else
1867 tlb_flush(env, 1);
1868 #endif
1872 void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1874 dump_store_bat(env, 'D', 1, nr, value);
1875 env->DBAT[1][nr] = value;
1878 void do_store_ibatu_601 (CPUPPCState *env, int nr, target_ulong value)
1880 target_ulong mask;
1881 int do_inval;
1883 dump_store_bat(env, 'I', 0, nr, value);
1884 if (env->IBAT[0][nr] != value) {
1885 do_inval = 0;
1886 mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1887 if (env->IBAT[1][nr] & 0x40) {
1888 /* Invalidate BAT only if it is valid */
1889 #if !defined(FLUSH_ALL_TLBS)
1890 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1891 #else
1892 do_inval = 1;
1893 #endif
1895 /* When storing valid upper BAT, mask BEPI and BRPN
1896 * and invalidate all TLBs covered by this BAT
1898 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1899 (value & ~0x0001FFFFUL & ~mask);
1900 env->DBAT[0][nr] = env->IBAT[0][nr];
1901 if (env->IBAT[1][nr] & 0x40) {
1902 #if !defined(FLUSH_ALL_TLBS)
1903 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1904 #else
1905 do_inval = 1;
1906 #endif
1908 #if defined(FLUSH_ALL_TLBS)
1909 if (do_inval)
1910 tlb_flush(env, 1);
1911 #endif
1915 void do_store_ibatl_601 (CPUPPCState *env, int nr, target_ulong value)
1917 target_ulong mask;
1918 int do_inval;
1920 dump_store_bat(env, 'I', 1, nr, value);
1921 if (env->IBAT[1][nr] != value) {
1922 do_inval = 0;
1923 if (env->IBAT[1][nr] & 0x40) {
1924 #if !defined(FLUSH_ALL_TLBS)
1925 mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1926 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1927 #else
1928 do_inval = 1;
1929 #endif
1931 if (value & 0x40) {
1932 #if !defined(FLUSH_ALL_TLBS)
1933 mask = (value << 17) & 0x0FFE0000UL;
1934 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1935 #else
1936 do_inval = 1;
1937 #endif
1939 env->IBAT[1][nr] = value;
1940 env->DBAT[1][nr] = value;
1941 #if defined(FLUSH_ALL_TLBS)
1942 if (do_inval)
1943 tlb_flush(env, 1);
1944 #endif
1948 /*****************************************************************************/
1949 /* TLB management */
1950 void ppc_tlb_invalidate_all (CPUPPCState *env)
1952 switch (env->mmu_model) {
1953 case POWERPC_MMU_SOFT_6xx:
1954 case POWERPC_MMU_SOFT_74xx:
1955 ppc6xx_tlb_invalidate_all(env);
1956 break;
1957 case POWERPC_MMU_SOFT_4xx:
1958 case POWERPC_MMU_SOFT_4xx_Z:
1959 ppc4xx_tlb_invalidate_all(env);
1960 break;
1961 case POWERPC_MMU_REAL:
1962 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1963 break;
1964 case POWERPC_MMU_MPC8xx:
1965 /* XXX: TODO */
1966 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1967 break;
1968 case POWERPC_MMU_BOOKE:
1969 /* XXX: TODO */
1970 cpu_abort(env, "BookE MMU model is not implemented\n");
1971 break;
1972 case POWERPC_MMU_BOOKE_FSL:
1973 /* XXX: TODO */
1974 cpu_abort(env, "BookE MMU model is not implemented\n");
1975 break;
1976 case POWERPC_MMU_32B:
1977 case POWERPC_MMU_601:
1978 #if defined(TARGET_PPC64)
1979 case POWERPC_MMU_620:
1980 case POWERPC_MMU_64B:
1981 #endif /* defined(TARGET_PPC64) */
1982 tlb_flush(env, 1);
1983 break;
1984 default:
1985 /* XXX: TODO */
1986 cpu_abort(env, "Unknown MMU model\n");
1987 break;
1991 void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1993 #if !defined(FLUSH_ALL_TLBS)
1994 addr &= TARGET_PAGE_MASK;
1995 switch (env->mmu_model) {
1996 case POWERPC_MMU_SOFT_6xx:
1997 case POWERPC_MMU_SOFT_74xx:
1998 ppc6xx_tlb_invalidate_virt(env, addr, 0);
1999 if (env->id_tlbs == 1)
2000 ppc6xx_tlb_invalidate_virt(env, addr, 1);
2001 break;
2002 case POWERPC_MMU_SOFT_4xx:
2003 case POWERPC_MMU_SOFT_4xx_Z:
2004 ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
2005 break;
2006 case POWERPC_MMU_REAL:
2007 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
2008 break;
2009 case POWERPC_MMU_MPC8xx:
2010 /* XXX: TODO */
2011 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
2012 break;
2013 case POWERPC_MMU_BOOKE:
2014 /* XXX: TODO */
2015 cpu_abort(env, "BookE MMU model is not implemented\n");
2016 break;
2017 case POWERPC_MMU_BOOKE_FSL:
2018 /* XXX: TODO */
2019 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
2020 break;
2021 case POWERPC_MMU_32B:
2022 case POWERPC_MMU_601:
2023 /* tlbie invalidate TLBs for all segments */
2024 addr &= ~((target_ulong)-1ULL << 28);
2025 /* XXX: this case should be optimized,
2026 * giving a mask to tlb_flush_page
2028 tlb_flush_page(env, addr | (0x0 << 28));
2029 tlb_flush_page(env, addr | (0x1 << 28));
2030 tlb_flush_page(env, addr | (0x2 << 28));
2031 tlb_flush_page(env, addr | (0x3 << 28));
2032 tlb_flush_page(env, addr | (0x4 << 28));
2033 tlb_flush_page(env, addr | (0x5 << 28));
2034 tlb_flush_page(env, addr | (0x6 << 28));
2035 tlb_flush_page(env, addr | (0x7 << 28));
2036 tlb_flush_page(env, addr | (0x8 << 28));
2037 tlb_flush_page(env, addr | (0x9 << 28));
2038 tlb_flush_page(env, addr | (0xA << 28));
2039 tlb_flush_page(env, addr | (0xB << 28));
2040 tlb_flush_page(env, addr | (0xC << 28));
2041 tlb_flush_page(env, addr | (0xD << 28));
2042 tlb_flush_page(env, addr | (0xE << 28));
2043 tlb_flush_page(env, addr | (0xF << 28));
2044 break;
2045 #if defined(TARGET_PPC64)
2046 case POWERPC_MMU_620:
2047 case POWERPC_MMU_64B:
2048 /* tlbie invalidate TLBs for all segments */
2049 /* XXX: given the fact that there are too many segments to invalidate,
2050 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
2051 * we just invalidate all TLBs
2053 tlb_flush(env, 1);
2054 break;
2055 #endif /* defined(TARGET_PPC64) */
2056 default:
2057 /* XXX: TODO */
2058 cpu_abort(env, "Unknown MMU model\n");
2059 break;
2061 #else
2062 ppc_tlb_invalidate_all(env);
2063 #endif
2066 /*****************************************************************************/
2067 /* Special registers manipulation */
2068 #if defined(TARGET_PPC64)
2069 target_ulong ppc_load_asr (CPUPPCState *env)
2071 return env->asr;
2074 void ppc_store_asr (CPUPPCState *env, target_ulong value)
2076 if (env->asr != value) {
2077 env->asr = value;
2078 tlb_flush(env, 1);
2081 #endif
2083 target_ulong do_load_sdr1 (CPUPPCState *env)
2085 return env->sdr1;
2088 void do_store_sdr1 (CPUPPCState *env, target_ulong value)
2090 #if defined (DEBUG_MMU)
2091 if (loglevel != 0) {
2092 fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
2094 #endif
2095 if (env->sdr1 != value) {
2096 /* XXX: for PowerPC 64, should check that the HTABSIZE value
2097 * is <= 28
2099 env->sdr1 = value;
2100 tlb_flush(env, 1);
2104 #if 0 // Unused
2105 target_ulong do_load_sr (CPUPPCState *env, int srnum)
2107 return env->sr[srnum];
2109 #endif
2111 void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
2113 #if defined (DEBUG_MMU)
2114 if (loglevel != 0) {
2115 fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
2116 __func__, srnum, value, env->sr[srnum]);
2118 #endif
2119 if (env->sr[srnum] != value) {
2120 env->sr[srnum] = value;
2121 #if !defined(FLUSH_ALL_TLBS) && 0
2123 target_ulong page, end;
2124 /* Invalidate 256 MB of virtual memory */
2125 page = (16 << 20) * srnum;
2126 end = page + (16 << 20);
2127 for (; page != end; page += TARGET_PAGE_SIZE)
2128 tlb_flush_page(env, page);
2130 #else
2131 tlb_flush(env, 1);
2132 #endif
2135 #endif /* !defined (CONFIG_USER_ONLY) */
2137 target_ulong ppc_load_xer (CPUPPCState *env)
2139 return hreg_load_xer(env);
2142 void ppc_store_xer (CPUPPCState *env, target_ulong value)
2144 hreg_store_xer(env, value);
2147 /* GDBstub can read and write MSR... */
2148 void ppc_store_msr (CPUPPCState *env, target_ulong value)
2150 hreg_store_msr(env, value, 0);
2153 /*****************************************************************************/
2154 /* Exception processing */
2155 #if defined (CONFIG_USER_ONLY)
2156 void do_interrupt (CPUState *env)
2158 env->exception_index = POWERPC_EXCP_NONE;
2159 env->error_code = 0;
2162 void ppc_hw_interrupt (CPUState *env)
2164 env->exception_index = POWERPC_EXCP_NONE;
2165 env->error_code = 0;
2167 #else /* defined (CONFIG_USER_ONLY) */
2168 static always_inline void dump_syscall (CPUState *env)
2170 fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
2171 " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
2172 (target_ulong)env->gpr[0], (target_ulong)env->gpr[3],
2173 (target_ulong)env->gpr[4], (target_ulong)env->gpr[5],
2174 (target_ulong)env->gpr[6], env->nip);
2177 /* Note that this function should be greatly optimized
2178 * when called with a constant excp, from ppc_hw_interrupt
2180 static always_inline void powerpc_excp (CPUState *env,
2181 int excp_model, int excp)
2183 target_ulong msr, new_msr, vector;
2184 int srr0, srr1, asrr0, asrr1;
2185 int lpes0, lpes1, lev;
2187 if (0) {
2188 /* XXX: find a suitable condition to enable the hypervisor mode */
2189 lpes0 = (env->spr[SPR_LPCR] >> 1) & 1;
2190 lpes1 = (env->spr[SPR_LPCR] >> 2) & 1;
2191 } else {
2192 /* Those values ensure we won't enter the hypervisor mode */
2193 lpes0 = 0;
2194 lpes1 = 1;
2197 if (loglevel & CPU_LOG_INT) {
2198 fprintf(logfile, "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
2199 env->nip, excp, env->error_code);
2201 msr = env->msr;
2202 new_msr = msr;
2203 srr0 = SPR_SRR0;
2204 srr1 = SPR_SRR1;
2205 asrr0 = -1;
2206 asrr1 = -1;
2207 msr &= ~((target_ulong)0x783F0000);
2208 switch (excp) {
2209 case POWERPC_EXCP_NONE:
2210 /* Should never happen */
2211 return;
2212 case POWERPC_EXCP_CRITICAL: /* Critical input */
2213 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2214 switch (excp_model) {
2215 case POWERPC_EXCP_40x:
2216 srr0 = SPR_40x_SRR2;
2217 srr1 = SPR_40x_SRR3;
2218 break;
2219 case POWERPC_EXCP_BOOKE:
2220 srr0 = SPR_BOOKE_CSRR0;
2221 srr1 = SPR_BOOKE_CSRR1;
2222 break;
2223 case POWERPC_EXCP_G2:
2224 break;
2225 default:
2226 goto excp_invalid;
2228 goto store_next;
2229 case POWERPC_EXCP_MCHECK: /* Machine check exception */
2230 if (msr_me == 0) {
2231 /* Machine check exception is not enabled.
2232 * Enter checkstop state.
2234 if (loglevel != 0) {
2235 fprintf(logfile, "Machine check while not allowed. "
2236 "Entering checkstop state\n");
2237 } else {
2238 fprintf(stderr, "Machine check while not allowed. "
2239 "Entering checkstop state\n");
2241 env->halted = 1;
2242 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2244 new_msr &= ~((target_ulong)1 << MSR_RI);
2245 new_msr &= ~((target_ulong)1 << MSR_ME);
2246 if (0) {
2247 /* XXX: find a suitable condition to enable the hypervisor mode */
2248 new_msr |= (target_ulong)MSR_HVB;
2250 /* XXX: should also have something loaded in DAR / DSISR */
2251 switch (excp_model) {
2252 case POWERPC_EXCP_40x:
2253 srr0 = SPR_40x_SRR2;
2254 srr1 = SPR_40x_SRR3;
2255 break;
2256 case POWERPC_EXCP_BOOKE:
2257 srr0 = SPR_BOOKE_MCSRR0;
2258 srr1 = SPR_BOOKE_MCSRR1;
2259 asrr0 = SPR_BOOKE_CSRR0;
2260 asrr1 = SPR_BOOKE_CSRR1;
2261 break;
2262 default:
2263 break;
2265 goto store_next;
2266 case POWERPC_EXCP_DSI: /* Data storage exception */
2267 #if defined (DEBUG_EXCEPTIONS)
2268 if (loglevel != 0) {
2269 fprintf(logfile, "DSI exception: DSISR=0x" ADDRX" DAR=0x" ADDRX
2270 "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
2272 #endif
2273 new_msr &= ~((target_ulong)1 << MSR_RI);
2274 if (lpes1 == 0)
2275 new_msr |= (target_ulong)MSR_HVB;
2276 goto store_next;
2277 case POWERPC_EXCP_ISI: /* Instruction storage exception */
2278 #if defined (DEBUG_EXCEPTIONS)
2279 if (loglevel != 0) {
2280 fprintf(logfile, "ISI exception: msr=0x" ADDRX ", nip=0x" ADDRX
2281 "\n", msr, env->nip);
2283 #endif
2284 new_msr &= ~((target_ulong)1 << MSR_RI);
2285 if (lpes1 == 0)
2286 new_msr |= (target_ulong)MSR_HVB;
2287 msr |= env->error_code;
2288 goto store_next;
2289 case POWERPC_EXCP_EXTERNAL: /* External input */
2290 new_msr &= ~((target_ulong)1 << MSR_RI);
2291 if (lpes0 == 1)
2292 new_msr |= (target_ulong)MSR_HVB;
2293 goto store_next;
2294 case POWERPC_EXCP_ALIGN: /* Alignment exception */
2295 new_msr &= ~((target_ulong)1 << MSR_RI);
2296 if (lpes1 == 0)
2297 new_msr |= (target_ulong)MSR_HVB;
2298 /* XXX: this is false */
2299 /* Get rS/rD and rA from faulting opcode */
2300 env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
2301 goto store_current;
2302 case POWERPC_EXCP_PROGRAM: /* Program exception */
2303 switch (env->error_code & ~0xF) {
2304 case POWERPC_EXCP_FP:
2305 if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
2306 #if defined (DEBUG_EXCEPTIONS)
2307 if (loglevel != 0) {
2308 fprintf(logfile, "Ignore floating point exception\n");
2310 #endif
2311 env->exception_index = POWERPC_EXCP_NONE;
2312 env->error_code = 0;
2313 return;
2315 new_msr &= ~((target_ulong)1 << MSR_RI);
2316 if (lpes1 == 0)
2317 new_msr |= (target_ulong)MSR_HVB;
2318 msr |= 0x00100000;
2319 if (msr_fe0 == msr_fe1)
2320 goto store_next;
2321 msr |= 0x00010000;
2322 break;
2323 case POWERPC_EXCP_INVAL:
2324 #if defined (DEBUG_EXCEPTIONS)
2325 if (loglevel != 0) {
2326 fprintf(logfile, "Invalid instruction at 0x" ADDRX "\n",
2327 env->nip);
2329 #endif
2330 new_msr &= ~((target_ulong)1 << MSR_RI);
2331 if (lpes1 == 0)
2332 new_msr |= (target_ulong)MSR_HVB;
2333 msr |= 0x00080000;
2334 break;
2335 case POWERPC_EXCP_PRIV:
2336 new_msr &= ~((target_ulong)1 << MSR_RI);
2337 if (lpes1 == 0)
2338 new_msr |= (target_ulong)MSR_HVB;
2339 msr |= 0x00040000;
2340 break;
2341 case POWERPC_EXCP_TRAP:
2342 new_msr &= ~((target_ulong)1 << MSR_RI);
2343 if (lpes1 == 0)
2344 new_msr |= (target_ulong)MSR_HVB;
2345 msr |= 0x00020000;
2346 break;
2347 default:
2348 /* Should never occur */
2349 cpu_abort(env, "Invalid program exception %d. Aborting\n",
2350 env->error_code);
2351 break;
2353 goto store_current;
2354 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
2355 new_msr &= ~((target_ulong)1 << MSR_RI);
2356 if (lpes1 == 0)
2357 new_msr |= (target_ulong)MSR_HVB;
2358 goto store_current;
2359 case POWERPC_EXCP_SYSCALL: /* System call exception */
2360 /* NOTE: this is a temporary hack to support graphics OSI
2361 calls from the MOL driver */
2362 /* XXX: To be removed */
2363 if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2364 env->osi_call) {
2365 if (env->osi_call(env) != 0) {
2366 env->exception_index = POWERPC_EXCP_NONE;
2367 env->error_code = 0;
2368 return;
2371 if (loglevel & CPU_LOG_INT) {
2372 dump_syscall(env);
2374 new_msr &= ~((target_ulong)1 << MSR_RI);
2375 lev = env->error_code;
2376 if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
2377 new_msr |= (target_ulong)MSR_HVB;
2378 goto store_next;
2379 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
2380 new_msr &= ~((target_ulong)1 << MSR_RI);
2381 goto store_current;
2382 case POWERPC_EXCP_DECR: /* Decrementer exception */
2383 new_msr &= ~((target_ulong)1 << MSR_RI);
2384 if (lpes1 == 0)
2385 new_msr |= (target_ulong)MSR_HVB;
2386 goto store_next;
2387 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
2388 /* FIT on 4xx */
2389 #if defined (DEBUG_EXCEPTIONS)
2390 if (loglevel != 0)
2391 fprintf(logfile, "FIT exception\n");
2392 #endif
2393 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2394 goto store_next;
2395 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
2396 #if defined (DEBUG_EXCEPTIONS)
2397 if (loglevel != 0)
2398 fprintf(logfile, "WDT exception\n");
2399 #endif
2400 switch (excp_model) {
2401 case POWERPC_EXCP_BOOKE:
2402 srr0 = SPR_BOOKE_CSRR0;
2403 srr1 = SPR_BOOKE_CSRR1;
2404 break;
2405 default:
2406 break;
2408 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2409 goto store_next;
2410 case POWERPC_EXCP_DTLB: /* Data TLB error */
2411 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2412 goto store_next;
2413 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
2414 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2415 goto store_next;
2416 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
2417 switch (excp_model) {
2418 case POWERPC_EXCP_BOOKE:
2419 srr0 = SPR_BOOKE_DSRR0;
2420 srr1 = SPR_BOOKE_DSRR1;
2421 asrr0 = SPR_BOOKE_CSRR0;
2422 asrr1 = SPR_BOOKE_CSRR1;
2423 break;
2424 default:
2425 break;
2427 /* XXX: TODO */
2428 cpu_abort(env, "Debug exception is not implemented yet !\n");
2429 goto store_next;
2430 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable */
2431 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2432 goto store_current;
2433 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */
2434 /* XXX: TODO */
2435 cpu_abort(env, "Embedded floating point data exception "
2436 "is not implemented yet !\n");
2437 goto store_next;
2438 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */
2439 /* XXX: TODO */
2440 cpu_abort(env, "Embedded floating point round exception "
2441 "is not implemented yet !\n");
2442 goto store_next;
2443 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor interrupt */
2444 new_msr &= ~((target_ulong)1 << MSR_RI);
2445 /* XXX: TODO */
2446 cpu_abort(env,
2447 "Performance counter exception is not implemented yet !\n");
2448 goto store_next;
2449 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
2450 /* XXX: TODO */
2451 cpu_abort(env,
2452 "Embedded doorbell interrupt is not implemented yet !\n");
2453 goto store_next;
2454 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
2455 switch (excp_model) {
2456 case POWERPC_EXCP_BOOKE:
2457 srr0 = SPR_BOOKE_CSRR0;
2458 srr1 = SPR_BOOKE_CSRR1;
2459 break;
2460 default:
2461 break;
2463 /* XXX: TODO */
2464 cpu_abort(env, "Embedded doorbell critical interrupt "
2465 "is not implemented yet !\n");
2466 goto store_next;
2467 case POWERPC_EXCP_RESET: /* System reset exception */
2468 new_msr &= ~((target_ulong)1 << MSR_RI);
2469 if (0) {
2470 /* XXX: find a suitable condition to enable the hypervisor mode */
2471 new_msr |= (target_ulong)MSR_HVB;
2473 goto store_next;
2474 case POWERPC_EXCP_DSEG: /* Data segment exception */
2475 new_msr &= ~((target_ulong)1 << MSR_RI);
2476 if (lpes1 == 0)
2477 new_msr |= (target_ulong)MSR_HVB;
2478 goto store_next;
2479 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
2480 new_msr &= ~((target_ulong)1 << MSR_RI);
2481 if (lpes1 == 0)
2482 new_msr |= (target_ulong)MSR_HVB;
2483 goto store_next;
2484 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
2485 srr0 = SPR_HSRR0;
2486 srr1 = SPR_HSRR1;
2487 new_msr |= (target_ulong)MSR_HVB;
2488 goto store_next;
2489 case POWERPC_EXCP_TRACE: /* Trace exception */
2490 new_msr &= ~((target_ulong)1 << MSR_RI);
2491 if (lpes1 == 0)
2492 new_msr |= (target_ulong)MSR_HVB;
2493 goto store_next;
2494 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
2495 srr0 = SPR_HSRR0;
2496 srr1 = SPR_HSRR1;
2497 new_msr |= (target_ulong)MSR_HVB;
2498 goto store_next;
2499 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */
2500 srr0 = SPR_HSRR0;
2501 srr1 = SPR_HSRR1;
2502 new_msr |= (target_ulong)MSR_HVB;
2503 goto store_next;
2504 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
2505 srr0 = SPR_HSRR0;
2506 srr1 = SPR_HSRR1;
2507 new_msr |= (target_ulong)MSR_HVB;
2508 goto store_next;
2509 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment exception */
2510 srr0 = SPR_HSRR0;
2511 srr1 = SPR_HSRR1;
2512 new_msr |= (target_ulong)MSR_HVB;
2513 goto store_next;
2514 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
2515 new_msr &= ~((target_ulong)1 << MSR_RI);
2516 if (lpes1 == 0)
2517 new_msr |= (target_ulong)MSR_HVB;
2518 goto store_current;
2519 case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */
2520 #if defined (DEBUG_EXCEPTIONS)
2521 if (loglevel != 0)
2522 fprintf(logfile, "PIT exception\n");
2523 #endif
2524 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2525 goto store_next;
2526 case POWERPC_EXCP_IO: /* IO error exception */
2527 /* XXX: TODO */
2528 cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2529 goto store_next;
2530 case POWERPC_EXCP_RUNM: /* Run mode exception */
2531 /* XXX: TODO */
2532 cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2533 goto store_next;
2534 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
2535 /* XXX: TODO */
2536 cpu_abort(env, "602 emulation trap exception "
2537 "is not implemented yet !\n");
2538 goto store_next;
2539 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
2540 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2541 if (lpes1 == 0) /* XXX: check this */
2542 new_msr |= (target_ulong)MSR_HVB;
2543 switch (excp_model) {
2544 case POWERPC_EXCP_602:
2545 case POWERPC_EXCP_603:
2546 case POWERPC_EXCP_603E:
2547 case POWERPC_EXCP_G2:
2548 goto tlb_miss_tgpr;
2549 case POWERPC_EXCP_7x5:
2550 goto tlb_miss;
2551 case POWERPC_EXCP_74xx:
2552 goto tlb_miss_74xx;
2553 default:
2554 cpu_abort(env, "Invalid instruction TLB miss exception\n");
2555 break;
2557 break;
2558 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
2559 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2560 if (lpes1 == 0) /* XXX: check this */
2561 new_msr |= (target_ulong)MSR_HVB;
2562 switch (excp_model) {
2563 case POWERPC_EXCP_602:
2564 case POWERPC_EXCP_603:
2565 case POWERPC_EXCP_603E:
2566 case POWERPC_EXCP_G2:
2567 goto tlb_miss_tgpr;
2568 case POWERPC_EXCP_7x5:
2569 goto tlb_miss;
2570 case POWERPC_EXCP_74xx:
2571 goto tlb_miss_74xx;
2572 default:
2573 cpu_abort(env, "Invalid data load TLB miss exception\n");
2574 break;
2576 break;
2577 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
2578 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2579 if (lpes1 == 0) /* XXX: check this */
2580 new_msr |= (target_ulong)MSR_HVB;
2581 switch (excp_model) {
2582 case POWERPC_EXCP_602:
2583 case POWERPC_EXCP_603:
2584 case POWERPC_EXCP_603E:
2585 case POWERPC_EXCP_G2:
2586 tlb_miss_tgpr:
2587 /* Swap temporary saved registers with GPRs */
2588 if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
2589 new_msr |= (target_ulong)1 << MSR_TGPR;
2590 hreg_swap_gpr_tgpr(env);
2592 goto tlb_miss;
2593 case POWERPC_EXCP_7x5:
2594 tlb_miss:
2595 #if defined (DEBUG_SOFTWARE_TLB)
2596 if (loglevel != 0) {
2597 const unsigned char *es;
2598 target_ulong *miss, *cmp;
2599 int en;
2600 if (excp == POWERPC_EXCP_IFTLB) {
2601 es = "I";
2602 en = 'I';
2603 miss = &env->spr[SPR_IMISS];
2604 cmp = &env->spr[SPR_ICMP];
2605 } else {
2606 if (excp == POWERPC_EXCP_DLTLB)
2607 es = "DL";
2608 else
2609 es = "DS";
2610 en = 'D';
2611 miss = &env->spr[SPR_DMISS];
2612 cmp = &env->spr[SPR_DCMP];
2614 fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2615 " H1 " ADDRX " H2 " ADDRX " %08x\n",
2616 es, en, *miss, en, *cmp,
2617 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2618 env->error_code);
2620 #endif
2621 msr |= env->crf[0] << 28;
2622 msr |= env->error_code; /* key, D/I, S/L bits */
2623 /* Set way using a LRU mechanism */
2624 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2625 break;
2626 case POWERPC_EXCP_74xx:
2627 tlb_miss_74xx:
2628 #if defined (DEBUG_SOFTWARE_TLB)
2629 if (loglevel != 0) {
2630 const unsigned char *es;
2631 target_ulong *miss, *cmp;
2632 int en;
2633 if (excp == POWERPC_EXCP_IFTLB) {
2634 es = "I";
2635 en = 'I';
2636 miss = &env->spr[SPR_TLBMISS];
2637 cmp = &env->spr[SPR_PTEHI];
2638 } else {
2639 if (excp == POWERPC_EXCP_DLTLB)
2640 es = "DL";
2641 else
2642 es = "DS";
2643 en = 'D';
2644 miss = &env->spr[SPR_TLBMISS];
2645 cmp = &env->spr[SPR_PTEHI];
2647 fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2648 " %08x\n",
2649 es, en, *miss, en, *cmp, env->error_code);
2651 #endif
2652 msr |= env->error_code; /* key bit */
2653 break;
2654 default:
2655 cpu_abort(env, "Invalid data store TLB miss exception\n");
2656 break;
2658 goto store_next;
2659 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
2660 /* XXX: TODO */
2661 cpu_abort(env, "Floating point assist exception "
2662 "is not implemented yet !\n");
2663 goto store_next;
2664 case POWERPC_EXCP_DABR: /* Data address breakpoint */
2665 /* XXX: TODO */
2666 cpu_abort(env, "DABR exception is not implemented yet !\n");
2667 goto store_next;
2668 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
2669 /* XXX: TODO */
2670 cpu_abort(env, "IABR exception is not implemented yet !\n");
2671 goto store_next;
2672 case POWERPC_EXCP_SMI: /* System management interrupt */
2673 /* XXX: TODO */
2674 cpu_abort(env, "SMI exception is not implemented yet !\n");
2675 goto store_next;
2676 case POWERPC_EXCP_THERM: /* Thermal interrupt */
2677 /* XXX: TODO */
2678 cpu_abort(env, "Thermal management exception "
2679 "is not implemented yet !\n");
2680 goto store_next;
2681 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
2682 new_msr &= ~((target_ulong)1 << MSR_RI);
2683 if (lpes1 == 0)
2684 new_msr |= (target_ulong)MSR_HVB;
2685 /* XXX: TODO */
2686 cpu_abort(env,
2687 "Performance counter exception is not implemented yet !\n");
2688 goto store_next;
2689 case POWERPC_EXCP_VPUA: /* Vector assist exception */
2690 /* XXX: TODO */
2691 cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2692 goto store_next;
2693 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
2694 /* XXX: TODO */
2695 cpu_abort(env,
2696 "970 soft-patch exception is not implemented yet !\n");
2697 goto store_next;
2698 case POWERPC_EXCP_MAINT: /* Maintenance exception */
2699 /* XXX: TODO */
2700 cpu_abort(env,
2701 "970 maintenance exception is not implemented yet !\n");
2702 goto store_next;
2703 case POWERPC_EXCP_MEXTBR: /* Maskable external breakpoint */
2704 /* XXX: TODO */
2705 cpu_abort(env, "Maskable external exception "
2706 "is not implemented yet !\n");
2707 goto store_next;
2708 case POWERPC_EXCP_NMEXTBR: /* Non maskable external breakpoint */
2709 /* XXX: TODO */
2710 cpu_abort(env, "Non maskable external exception "
2711 "is not implemented yet !\n");
2712 goto store_next;
2713 default:
2714 excp_invalid:
2715 cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2716 break;
2717 store_current:
2718 /* save current instruction location */
2719 env->spr[srr0] = env->nip - 4;
2720 break;
2721 store_next:
2722 /* save next instruction location */
2723 env->spr[srr0] = env->nip;
2724 break;
2726 /* Save MSR */
2727 env->spr[srr1] = msr;
2728 /* If any alternate SRR register are defined, duplicate saved values */
2729 if (asrr0 != -1)
2730 env->spr[asrr0] = env->spr[srr0];
2731 if (asrr1 != -1)
2732 env->spr[asrr1] = env->spr[srr1];
2733 /* If we disactivated any translation, flush TLBs */
2734 if (new_msr & ((1 << MSR_IR) | (1 << MSR_DR)))
2735 tlb_flush(env, 1);
2736 /* reload MSR with correct bits */
2737 new_msr &= ~((target_ulong)1 << MSR_EE);
2738 new_msr &= ~((target_ulong)1 << MSR_PR);
2739 new_msr &= ~((target_ulong)1 << MSR_FP);
2740 new_msr &= ~((target_ulong)1 << MSR_FE0);
2741 new_msr &= ~((target_ulong)1 << MSR_SE);
2742 new_msr &= ~((target_ulong)1 << MSR_BE);
2743 new_msr &= ~((target_ulong)1 << MSR_FE1);
2744 new_msr &= ~((target_ulong)1 << MSR_IR);
2745 new_msr &= ~((target_ulong)1 << MSR_DR);
2746 #if 0 /* Fix this: not on all targets */
2747 new_msr &= ~((target_ulong)1 << MSR_PMM);
2748 #endif
2749 new_msr &= ~((target_ulong)1 << MSR_LE);
2750 if (msr_ile)
2751 new_msr |= (target_ulong)1 << MSR_LE;
2752 else
2753 new_msr &= ~((target_ulong)1 << MSR_LE);
2754 /* Jump to handler */
2755 vector = env->excp_vectors[excp];
2756 if (vector == (target_ulong)-1ULL) {
2757 cpu_abort(env, "Raised an exception without defined vector %d\n",
2758 excp);
2760 vector |= env->excp_prefix;
2761 #if defined(TARGET_PPC64)
2762 if (excp_model == POWERPC_EXCP_BOOKE) {
2763 if (!msr_icm) {
2764 new_msr &= ~((target_ulong)1 << MSR_CM);
2765 vector = (uint32_t)vector;
2766 } else {
2767 new_msr |= (target_ulong)1 << MSR_CM;
2769 } else {
2770 if (!msr_isf) {
2771 new_msr &= ~((target_ulong)1 << MSR_SF);
2772 vector = (uint32_t)vector;
2773 } else {
2774 new_msr |= (target_ulong)1 << MSR_SF;
2777 #endif
2778 /* XXX: we don't use hreg_store_msr here as already have treated
2779 * any special case that could occur. Just store MSR and update hflags
2781 env->msr = new_msr & env->msr_mask;
2782 hreg_compute_hflags(env);
2783 env->nip = vector;
2784 /* Reset exception state */
2785 env->exception_index = POWERPC_EXCP_NONE;
2786 env->error_code = 0;
2789 void do_interrupt (CPUState *env)
2791 powerpc_excp(env, env->excp_model, env->exception_index);
2794 void ppc_hw_interrupt (CPUPPCState *env)
2796 int hdice;
2798 #if 0
2799 if (loglevel & CPU_LOG_INT) {
2800 fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2801 __func__, env, env->pending_interrupts,
2802 env->interrupt_request, (int)msr_me, (int)msr_ee);
2804 #endif
2805 /* External reset */
2806 if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2807 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2808 powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2809 return;
2811 /* Machine check exception */
2812 if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2813 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2814 powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2815 return;
2817 #if 0 /* TODO */
2818 /* External debug exception */
2819 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2820 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2821 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2822 return;
2824 #endif
2825 if (0) {
2826 /* XXX: find a suitable condition to enable the hypervisor mode */
2827 hdice = env->spr[SPR_LPCR] & 1;
2828 } else {
2829 hdice = 0;
2831 if ((msr_ee != 0 || msr_hv == 0 || msr_pr != 0) && hdice != 0) {
2832 /* Hypervisor decrementer exception */
2833 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2834 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2835 powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2836 return;
2839 if (msr_ce != 0) {
2840 /* External critical interrupt */
2841 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2842 /* Taking a critical external interrupt does not clear the external
2843 * critical interrupt status
2845 #if 0
2846 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2847 #endif
2848 powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2849 return;
2852 if (msr_ee != 0) {
2853 /* Watchdog timer on embedded PowerPC */
2854 if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2855 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2856 powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2857 return;
2859 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2860 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2861 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2862 return;
2864 /* Fixed interval timer on embedded PowerPC */
2865 if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2866 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2867 powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2868 return;
2870 /* Programmable interval timer on embedded PowerPC */
2871 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2872 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2873 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2874 return;
2876 /* Decrementer exception */
2877 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2878 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2879 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2880 return;
2882 /* External interrupt */
2883 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2884 /* Taking an external interrupt does not clear the external
2885 * interrupt status
2887 #if 0
2888 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2889 #endif
2890 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2891 return;
2893 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2894 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2895 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2896 return;
2898 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2899 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2900 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2901 return;
2903 /* Thermal interrupt */
2904 if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2905 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2906 powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2907 return;
2911 #endif /* !CONFIG_USER_ONLY */
2913 void cpu_dump_EA (target_ulong EA)
2915 FILE *f;
2917 if (logfile) {
2918 f = logfile;
2919 } else {
2920 f = stdout;
2921 return;
2923 fprintf(f, "Memory access at address " ADDRX "\n", EA);
2926 void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2928 FILE *f;
2930 if (logfile) {
2931 f = logfile;
2932 } else {
2933 f = stdout;
2934 return;
2936 fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2937 RA, msr);
2940 void cpu_ppc_reset (void *opaque)
2942 CPUPPCState *env;
2943 target_ulong msr;
2945 env = opaque;
2946 msr = (target_ulong)0;
2947 if (0) {
2948 /* XXX: find a suitable condition to enable the hypervisor mode */
2949 msr |= (target_ulong)MSR_HVB;
2951 msr |= (target_ulong)0 << MSR_AP; /* TO BE CHECKED */
2952 msr |= (target_ulong)0 << MSR_SA; /* TO BE CHECKED */
2953 msr |= (target_ulong)1 << MSR_EP;
2954 #if defined (DO_SINGLE_STEP) && 0
2955 /* Single step trace mode */
2956 msr |= (target_ulong)1 << MSR_SE;
2957 msr |= (target_ulong)1 << MSR_BE;
2958 #endif
2959 #if defined(CONFIG_USER_ONLY)
2960 msr |= (target_ulong)1 << MSR_FP; /* Allow floating point usage */
2961 msr |= (target_ulong)1 << MSR_PR;
2962 #else
2963 env->nip = env->hreset_vector | env->excp_prefix;
2964 if (env->mmu_model != POWERPC_MMU_REAL)
2965 ppc_tlb_invalidate_all(env);
2966 #endif
2967 env->msr = msr;
2968 hreg_compute_hflags(env);
2969 env->reserve = (target_ulong)-1ULL;
2970 /* Be sure no exception or interrupt is pending */
2971 env->pending_interrupts = 0;
2972 env->exception_index = POWERPC_EXCP_NONE;
2973 env->error_code = 0;
2974 /* Flush all TLBs */
2975 tlb_flush(env, 1);
2978 CPUPPCState *cpu_ppc_init (const char *cpu_model)
2980 CPUPPCState *env;
2981 const ppc_def_t *def;
2983 def = cpu_ppc_find_by_name(cpu_model);
2984 if (!def)
2985 return NULL;
2987 env = qemu_mallocz(sizeof(CPUPPCState));
2988 if (!env)
2989 return NULL;
2990 cpu_exec_init(env);
2991 cpu_ppc_register_internal(env, def);
2992 cpu_ppc_reset(env);
2993 return env;
2996 void cpu_ppc_close (CPUPPCState *env)
2998 /* Should also remove all opcode tables... */
2999 qemu_free(env);