2 * PPC emulation helpers for qemu.
4 * Copyright (c) 2003 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
23 #if defined (USE_OPEN_FIRMWARE)
30 //#define DEBUG_EXCEPTIONS
32 extern FILE *logfile, *stdout, *stderr;
36 void cpu_loop_exit(void)
38 longjmp(env->jmp_env, 1);
41 void do_process_exceptions (void)
46 int check_exception_state (CPUState *env)
50 /* Process PPC exceptions */
51 for (i = 1; i < EXCP_PPC_MAX; i++) {
52 if (env->exceptions & (1 << i)) {
60 if (env->errors[EXCP_PROGRAM] == EXCP_FP &&
61 msr_fe0 == 0 && msr_fe1 == 0)
67 env->exception_index = i;
68 env->error_code = env->errors[i];
76 /*****************************************************************************/
77 /* PPC MMU emulation */
78 int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
79 int is_user, int is_softmmu);
81 /* Perform BAT hit & translation */
82 static int get_bat (CPUState *env, uint32_t *real, int *prot,
83 uint32_t virtual, int rw, int type)
85 uint32_t *BATlt, *BATut, *BATu, *BATl;
86 uint32_t base, BEPIl, BEPIu, bl;
90 #if defined (DEBUG_BATS)
92 fprintf(logfile, "%s: %cBAT v 0x%08x\n", __func__,
93 type == ACCESS_CODE ? 'I' : 'D', virtual);
102 BATlt = env->DBAT[1];
103 BATut = env->DBAT[0];
106 #if defined (DEBUG_BATS)
108 fprintf(logfile, "%s...: %cBAT v 0x%08x\n", __func__,
109 type == ACCESS_CODE ? 'I' : 'D', virtual);
112 base = virtual & 0xFFFC0000;
113 for (i = 0; i < 4; i++) {
116 BEPIu = *BATu & 0xF0000000;
117 BEPIl = *BATu & 0x0FFE0000;
118 bl = (*BATu & 0x00001FFC) << 15;
119 #if defined (DEBUG_BATS)
121 fprintf(logfile, "%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x\n",
122 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
126 if ((virtual & 0xF0000000) == BEPIu &&
127 ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
129 if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
130 (msr_pr == 1 && (*BATu & 0x00000001))) {
131 /* Get physical address */
132 *real = (*BATl & 0xF0000000) |
133 ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
134 (virtual & 0x0001F000);
135 if (*BATl & 0x00000001)
137 if (*BATl & 0x00000002)
138 *prot = PROT_WRITE | PROT_READ;
139 #if defined (DEBUG_BATS)
141 fprintf(logfile, "BAT %d match: r 0x%08x prot=%c%c\n",
142 i, *real, *prot & PROT_READ ? 'R' : '-',
143 *prot & PROT_WRITE ? 'W' : '-');
152 #if defined (DEBUG_BATS)
153 printf("no BAT match for 0x%08x:\n", virtual);
154 for (i = 0; i < 4; i++) {
157 BEPIu = *BATu & 0xF0000000;
158 BEPIl = *BATu & 0x0FFE0000;
159 bl = (*BATu & 0x00001FFC) << 15;
160 printf("%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x \n\t"
161 "0x%08x 0x%08x 0x%08x\n",
162 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
163 *BATu, *BATl, BEPIu, BEPIl, bl);
171 /* PTE table lookup */
172 static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
173 int h, int key, int rw)
175 uint32_t pte0, pte1, keep = 0, access = 0;
176 int i, good = -1, store = 0;
177 int ret = -1; /* No entry found */
179 for (i = 0; i < 8; i++) {
180 pte0 = ldl_raw(phys_ram_base + base + (i * 8));
181 pte1 = ldl_raw(phys_ram_base + base + (i * 8) + 4);
182 #if defined (DEBUG_MMU)
184 fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
185 "%d %d %d 0x%08x\n", base + (i * 8), pte0, pte1,
186 pte0 >> 31, h, (pte0 >> 6) & 1, va);
189 /* Check validity and table match */
190 if (pte0 & 0x80000000 && (h == ((pte0 >> 6) & 1))) {
191 /* Check vsid & api */
192 if ((pte0 & 0x7FFFFFBF) == va) {
197 /* All matches should have equal RPN, WIMG & PP */
198 if ((keep & 0xFFFFF07B) != (pte1 & 0xFFFFF07B)) {
200 fprintf(logfile, "Bad RPN/WIMG/PP\n");
204 /* Check access rights */
207 if ((pte1 & 0x00000003) != 0x3)
208 access |= PROT_WRITE;
210 switch (pte1 & 0x00000003) {
219 access = PROT_READ | PROT_WRITE;
224 if ((rw == 0 && (access & PROT_READ)) ||
225 (rw == 1 && (access & PROT_WRITE))) {
226 #if defined (DEBUG_MMU)
228 fprintf(logfile, "PTE access granted !\n");
234 /* Access right violation */
236 #if defined (DEBUG_MMU)
238 fprintf(logfile, "PTE access rejected\n");
247 *RPN = keep & 0xFFFFF000;
248 #if defined (DEBUG_MMU)
250 fprintf(logfile, "found PTE at addr 0x%08x prot=0x%01x ret=%d\n",
254 /* Update page flags */
255 if (!(keep & 0x00000100)) {
260 if (!(keep & 0x00000080)) {
261 if (rw && ret == 0) {
266 /* Force page fault for first write access */
267 *prot &= ~PROT_WRITE;
271 stl_raw(phys_ram_base + base + (good * 8) + 4, keep);
278 static inline uint32_t get_pgaddr (uint32_t sdr1, uint32_t hash, uint32_t mask)
280 return (sdr1 & 0xFFFF0000) | (hash & mask);
283 /* Perform segment based translation */
284 static int get_segment (CPUState *env, uint32_t *real, int *prot,
285 uint32_t virtual, int rw, int type)
287 uint32_t pg_addr, sdr, ptem, vsid, pgidx;
293 sr = env->sr[virtual >> 28];
294 #if defined (DEBUG_MMU)
296 fprintf(logfile, "Check segment v=0x%08x %d 0x%08x nip=0x%08x "
297 "lr=0x%08x ir=%d dr=%d pr=%d %d t=%d\n",
298 virtual, virtual >> 28, sr, env->nip,
299 env->lr, msr_ir, msr_dr, msr_pr, rw, type);
302 key = (((sr & 0x20000000) && msr_pr == 1) ||
303 ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
304 if ((sr & 0x80000000) == 0) {
305 #if defined (DEBUG_MMU)
307 fprintf(logfile, "pte segment: key=%d n=0x%08x\n",
308 key, sr & 0x10000000);
310 /* Check if instruction fetch is allowed, if needed */
311 if (type != ACCESS_CODE || (sr & 0x10000000) == 0) {
312 /* Page address translation */
313 vsid = sr & 0x00FFFFFF;
314 pgidx = (virtual >> 12) & 0xFFFF;
316 hash = ((vsid ^ pgidx) & 0x0007FFFF) << 6;
317 mask = ((sdr & 0x000001FF) << 16) | 0xFFC0;
318 pg_addr = get_pgaddr(sdr, hash, mask);
319 ptem = (vsid << 7) | (pgidx >> 10);
320 #if defined (DEBUG_MMU)
322 fprintf(logfile, "0 sdr1=0x%08x vsid=0x%06x api=0x%04x "
323 "hash=0x%07x pg_addr=0x%08x\n", sdr, vsid, pgidx, hash,
327 /* Primary table lookup */
328 ret = find_pte(real, prot, pg_addr, ptem, 0, key, rw);
330 /* Secondary table lookup */
331 hash = (~hash) & 0x01FFFFC0;
332 pg_addr = get_pgaddr(sdr, hash, mask);
333 #if defined (DEBUG_MMU)
334 if (virtual != 0xEFFFFFFF && loglevel > 0) {
335 fprintf(logfile, "1 sdr1=0x%08x vsid=0x%06x api=0x%04x "
336 "hash=0x%05x pg_addr=0x%08x\n", sdr, vsid, pgidx,
340 ret2 = find_pte(real, prot, pg_addr, ptem, 1, key, rw);
345 #if defined (DEBUG_MMU)
347 fprintf(logfile, "No access allowed\n");
352 #if defined (DEBUG_MMU)
354 fprintf(logfile, "direct store...\n");
356 /* Direct-store segment : absolutely *BUGGY* for now */
359 /* Integer load/store : only access allowed */
362 /* No code fetch is allowed in direct-store areas */
365 /* Floating point load/store */
368 /* lwarx, ldarx or srwcx. */
371 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
372 /* Should make the instruction do no-op.
373 * As it already do no-op, it's quite easy :-)
382 fprintf(logfile, "ERROR: instruction should not need "
383 "address translation\n");
385 printf("ERROR: instruction should not need "
386 "address translation\n");
389 if ((rw == 1 || key != 1) && (rw == 0 || key != 0)) {
400 int get_physical_address (CPUState *env, uint32_t *physical, int *prot,
401 uint32_t address, int rw, int access_type)
406 fprintf(logfile, "%s\n", __func__);
409 if ((access_type == ACCESS_CODE && msr_ir == 0) || msr_dr == 0) {
410 /* No address translation */
411 *physical = address & ~0xFFF;
412 *prot = PROT_READ | PROT_WRITE;
415 /* Try to find a BAT */
416 ret = get_bat(env, physical, prot, address, rw, access_type);
418 /* We didn't match any BAT entry */
419 ret = get_segment(env, physical, prot, address, rw, access_type);
423 fprintf(logfile, "%s address %08x => %08x\n",
424 __func__, address, *physical);
430 #if defined(CONFIG_USER_ONLY)
431 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
436 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
441 if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
447 #if !defined(CONFIG_USER_ONLY)
449 #define MMUSUFFIX _mmu
450 #define GETPC() (__builtin_return_address(0))
453 #include "softmmu_template.h"
456 #include "softmmu_template.h"
459 #include "softmmu_template.h"
462 #include "softmmu_template.h"
464 /* try to fill the TLB and return an exception if error. If retaddr is
465 NULL, it means that the function was called in C code (i.e. not
466 from generated code or from helper.c) */
467 /* XXX: fix it to restore all registers */
468 void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
470 TranslationBlock *tb;
475 /* XXX: hack to restore env in all cases, even if not called from
478 env = cpu_single_env;
480 unsigned long tlb_addrr, tlb_addrw;
482 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
483 tlb_addrr = env->tlb_read[is_user][index].address;
484 tlb_addrw = env->tlb_write[is_user][index].address;
486 printf("%s 1 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
487 "(0x%08lx 0x%08lx)\n", __func__, env,
488 &env->tlb_read[is_user][index], index, addr,
489 tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
490 tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
493 ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, is_user, 1);
496 /* now we have a real cpu fault */
497 pc = (unsigned long)retaddr;
500 /* the PC is inside the translated code. It means that we have
501 a virtual CPU fault */
502 cpu_restore_state(tb, env, pc, NULL);
505 do_queue_exception_err(env->exception_index, env->error_code);
506 do_process_exceptions();
509 unsigned long tlb_addrr, tlb_addrw;
511 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
512 tlb_addrr = env->tlb_read[is_user][index].address;
513 tlb_addrw = env->tlb_write[is_user][index].address;
515 printf("%s 2 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
516 "(0x%08lx 0x%08lx)\n", __func__, env,
517 &env->tlb_read[is_user][index], index, addr,
518 tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
519 tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
525 void cpu_ppc_init_mmu(CPUState *env)
527 /* Nothing to do: all translation are disabled */
531 /* Perform address translation */
532 int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
533 int is_user, int is_softmmu)
537 int exception = 0, error_code = 0;
541 // printf("%s 0\n", __func__);
542 access_type = env->access_type;
543 if (env->user_mode_only) {
544 /* user mode only emulation */
548 /* NASTY BUG workaround */
549 if (access_type == ACCESS_CODE && rw) {
550 printf("%s: ERROR WRITE CODE ACCESS\n", __func__);
551 access_type = ACCESS_INT;
553 ret = get_physical_address(env, &physical, &prot,
554 address, rw, access_type);
556 ret = tlb_set_page(env, address & ~0xFFF, physical, prot,
557 is_user, is_softmmu);
558 } else if (ret < 0) {
560 #if defined (DEBUG_MMU)
562 cpu_ppc_dump_state(env, logfile, 0);
564 if (access_type == ACCESS_CODE) {
565 exception = EXCP_ISI;
568 /* No matches in page tables */
569 error_code = EXCP_ISI_TRANSLATE;
572 /* Access rights violation */
573 error_code = EXCP_ISI_PROT;
576 /* No execute protection violation */
577 error_code = EXCP_ISI_NOEXEC;
580 /* Direct store exception */
581 /* No code fetch is allowed in direct-store areas */
582 error_code = EXCP_ISI_DIRECT;
586 exception = EXCP_DSI;
589 /* No matches in page tables */
590 error_code = EXCP_DSI_TRANSLATE;
593 /* Access rights violation */
594 error_code = EXCP_DSI_PROT;
597 /* Direct store exception */
598 switch (access_type) {
600 /* Floating point load/store */
601 exception = EXCP_ALIGN;
602 error_code = EXCP_ALIGN_FP;
605 /* lwarx, ldarx or srwcx. */
606 exception = EXCP_DSI;
607 error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT;
611 exception = EXCP_DSI;
612 error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT |
616 printf("DSI: invalid exception (%d)\n", ret);
617 exception = EXCP_PROGRAM;
618 error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
623 error_code |= EXCP_DSI_STORE;
624 /* Store fault address */
625 env->spr[DAR] = address;
628 printf("%s: set exception to %d %02x\n",
629 __func__, exception, error_code);
631 env->exception_index = exception;
632 env->error_code = error_code;
639 uint32_t _load_xer (CPUState *env)
641 return (xer_so << XER_SO) |
647 void _store_xer (CPUState *env, uint32_t value)
649 xer_so = (value >> XER_SO) & 0x01;
650 xer_ov = (value >> XER_OV) & 0x01;
651 xer_ca = (value >> XER_CA) & 0x01;
652 xer_bc = (value >> XER_BC) & 0x1f;
655 uint32_t _load_msr (CPUState *env)
657 return (msr_pow << MSR_POW) |
658 (msr_ile << MSR_ILE) |
663 (msr_fe0 << MSR_FE0) |
666 (msr_fe1 << MSR_FE1) |
674 void _store_msr (CPUState *env, uint32_t value)
676 if (((value >> MSR_IR) & 0x01) != msr_ir ||
677 ((value >> MSR_DR) & 0x01) != msr_dr) {
678 /* Flush all tlb when changing translation mode or privilege level */
681 msr_pow = (value >> MSR_POW) & 0x03;
682 msr_ile = (value >> MSR_ILE) & 0x01;
683 msr_ee = (value >> MSR_EE) & 0x01;
684 msr_pr = (value >> MSR_PR) & 0x01;
685 msr_fp = (value >> MSR_FP) & 0x01;
686 msr_me = (value >> MSR_ME) & 0x01;
687 msr_fe0 = (value >> MSR_FE0) & 0x01;
688 msr_se = (value >> MSR_SE) & 0x01;
689 msr_be = (value >> MSR_BE) & 0x01;
690 msr_fe1 = (value >> MSR_FE1) & 0x01;
691 msr_ip = (value >> MSR_IP) & 0x01;
692 msr_ir = (value >> MSR_IR) & 0x01;
693 msr_dr = (value >> MSR_DR) & 0x01;
694 msr_ri = (value >> MSR_RI) & 0x01;
695 msr_le = (value >> MSR_LE) & 0x01;
698 void do_interrupt (CPUState *env)
700 #if defined (CONFIG_USER_ONLY)
701 env->exception_index |= 0x100;
704 int excp = env->exception_index;
706 /* Dequeue PPC exceptions */
707 if (excp < EXCP_PPC_MAX)
708 env->exceptions &= ~(1 << excp);
709 msr = _load_msr(env);
710 #if defined (DEBUG_EXCEPTIONS)
711 if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1)
714 fprintf(logfile, "Raise exception at 0x%08x => 0x%08x (%02x)\n",
715 env->nip, excp << 8, env->error_code);
718 cpu_ppc_dump_state(env, logfile, 0);
721 /* Generate informations in save/restore registers */
724 #if defined (USE_OPEN_FIRMWARE)
725 env->gpr[3] = OF_client_entry((void *)env->gpr[3]);
729 #if defined (USE_OPEN_FIRMWARE)
730 printf("RTAS call !\n");
731 env->gpr[3] = RTAS_entry((void *)env->gpr[3]);
732 printf("RTAS call done\n");
737 #if defined (DEBUG_EXCEPTIONS)
738 printf("%s: escape EXCP_NONE\n", __func__);
745 case EXCP_MACHINE_CHECK:
747 printf("Machine check exception while not allowed !\n");
750 "Machine check exception while not allowed !\n");
757 /* Store exception cause */
758 /* data location address has been stored
759 * when the fault has been detected
763 if (env->error_code & EXCP_DSI_TRANSLATE)
764 env->spr[DSISR] |= 0x40000000;
765 else if (env->error_code & EXCP_DSI_PROT)
766 env->spr[DSISR] |= 0x08000000;
767 else if (env->error_code & EXCP_DSI_NOTSUP) {
768 env->spr[DSISR] |= 0x80000000;
769 if (env->error_code & EXCP_DSI_DIRECT)
770 env->spr[DSISR] |= 0x04000000;
772 if (env->error_code & EXCP_DSI_STORE)
773 env->spr[DSISR] |= 0x02000000;
774 if ((env->error_code & 0xF) == EXCP_DSI_DABR)
775 env->spr[DSISR] |= 0x00400000;
776 if (env->error_code & EXCP_DSI_ECXW)
777 env->spr[DSISR] |= 0x00100000;
778 #if defined (DEBUG_EXCEPTIONS)
780 fprintf(logfile, "DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
781 env->spr[DSISR], env->spr[DAR]);
783 printf("DSI exception: DSISR=0x%08x, DAR=0x%08x nip=0x%08x\n",
784 env->spr[DSISR], env->spr[DAR], env->nip);
789 /* Store exception cause */
791 if (env->error_code == EXCP_ISI_TRANSLATE)
793 else if (env->error_code == EXCP_ISI_NOEXEC ||
794 env->error_code == EXCP_ISI_GUARD ||
795 env->error_code == EXCP_ISI_DIRECT)
799 #if defined (DEBUG_EXCEPTIONS)
801 fprintf(logfile, "ISI exception: msr=0x%08x, nip=0x%08x\n",
804 printf("ISI exception: msr=0x%08x, nip=0x%08x tbl:0x%08x\n",
805 msr, env->nip, env->spr[V_TBL]);
811 #if defined (DEBUG_EXCEPTIONS)
813 fprintf(logfile, "Skipping hardware interrupt\n");
817 do_queue_exception(EXCP_EXTERNAL);
822 /* Store exception cause */
823 /* Get rS/rD and rA from faulting opcode */
825 (ldl_code((void *)(env->nip - 4)) & 0x03FF0000) >> 16;
826 /* data location address has been stored
827 * when the fault has been detected
832 switch (env->error_code & ~0xF) {
834 if (msr_fe0 == 0 && msr_fe1 == 0) {
835 #if defined (DEBUG_EXCEPTIONS)
836 printf("Ignore floating point exception\n");
842 env->fpscr[7] |= 0x8;
843 /* Finally, update FEX */
844 if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
845 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
846 env->fpscr[7] |= 0x4;
849 printf("Invalid instruction at 0x%08x\n", env->nip);
859 /* Should never occur */
869 do_queue_exception(EXCP_DECR);
874 #if defined (DEBUG_EXCEPTIONS)
877 fprintf(logfile, "syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n",
878 env->gpr[0], env->gpr[3], env->gpr[4],
879 env->gpr[5], env->gpr[6]);
881 printf("syscall %d from 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
882 env->gpr[0], env->nip, env->gpr[3], env->gpr[4],
883 env->gpr[5], env->gpr[6]);
899 /* Restore user-mode state */
901 #if defined (DEBUG_EXCEPTIONS)
903 printf("Return from exception => 0x%08x\n", (uint32_t)env->nip);
907 /* SRR0 is set to current instruction */
908 env->spr[SRR0] = (uint32_t)env->nip - 4;
911 /* SRR0 is set to next instruction */
912 env->spr[SRR0] = (uint32_t)env->nip;
915 env->spr[SRR1] = msr;
916 /* reload MSR with correct bits */
929 /* Jump to handler */
930 env->nip = excp << 8;
931 env->exception_index = EXCP_NONE;
932 /* Invalidate all TLB as we may have changed translation mode */
934 /* ensure that no TB jump will be modified as
935 the program flow was changed */