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);
166 env->spr[DAR] = virtual;
172 /* PTE table lookup */
173 static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
174 int h, int key, int rw)
176 uint32_t pte0, pte1, keep = 0, access = 0;
177 int i, good = -1, store = 0;
178 int ret = -1; /* No entry found */
180 for (i = 0; i < 8; i++) {
181 pte0 = ldl_raw((void *)((uint32_t)phys_ram_base + base + (i * 8)));
182 pte1 = ldl_raw((void *)((uint32_t)phys_ram_base + base + (i * 8) + 4));
183 #if defined (DEBUG_MMU)
185 fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
186 "%d %d %d 0x%08x\n", base + (i * 8), pte0, pte1,
187 pte0 >> 31, h, (pte0 >> 6) & 1, va);
190 /* Check validity and table match */
191 if (pte0 & 0x80000000 && (h == ((pte0 >> 6) & 1))) {
192 /* Check vsid & api */
193 if ((pte0 & 0x7FFFFFBF) == va) {
198 /* All matches should have equal RPN, WIMG & PP */
199 if ((keep & 0xFFFFF07B) != (pte1 & 0xFFFFF07B)) {
201 fprintf(logfile, "Bad RPN/WIMG/PP\n");
205 /* Check access rights */
208 if ((pte1 & 0x00000003) != 0x3)
209 access |= PROT_WRITE;
211 switch (pte1 & 0x00000003) {
220 access = PROT_READ | PROT_WRITE;
225 if ((rw == 0 && (access & PROT_READ)) ||
226 (rw == 1 && (access & PROT_WRITE))) {
227 #if defined (DEBUG_MMU)
229 fprintf(logfile, "PTE access granted !\n");
235 /* Access right violation */
237 #if defined (DEBUG_MMU)
239 fprintf(logfile, "PTE access rejected\n");
248 *RPN = keep & 0xFFFFF000;
249 #if defined (DEBUG_MMU)
251 fprintf(logfile, "found PTE at addr 0x%08x prot=0x%01x ret=%d\n",
255 /* Update page flags */
256 if (!(keep & 0x00000100)) {
261 if (!(keep & 0x00000080)) {
262 if (rw && ret == 0) {
267 /* Force page fault for first write access */
268 *prot &= ~PROT_WRITE;
272 stl_raw((void *)((uint32_t)phys_ram_base + base + (good * 8) + 4),
280 static inline uint32_t get_pgaddr (uint32_t sdr1, uint32_t hash, uint32_t mask)
282 return (sdr1 & 0xFFFF0000) | (hash & mask);
285 /* Perform segment based translation */
286 static int get_segment (CPUState *env, uint32_t *real, int *prot,
287 uint32_t virtual, int rw, int type)
289 uint32_t pg_addr, sdr, ptem, vsid, pgidx;
295 sr = env->sr[virtual >> 28];
296 #if defined (DEBUG_MMU)
298 fprintf(logfile, "Check segment v=0x%08x %d 0x%08x nip=0x%08x "
299 "lr=0x%08x ir=%d dr=%d pr=%d %d t=%d\n",
300 virtual, virtual >> 28, sr, env->nip,
301 env->lr, msr_ir, msr_dr, msr_pr, rw, type);
304 key = (((sr & 0x20000000) && msr_pr == 1) ||
305 ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
306 if ((sr & 0x80000000) == 0) {
307 #if defined (DEBUG_MMU)
309 fprintf(logfile, "pte segment: key=%d n=0x%08x\n",
310 key, sr & 0x10000000);
312 /* Check if instruction fetch is allowed, if needed */
313 if (type != ACCESS_CODE || (sr & 0x10000000) == 0) {
314 /* Page address translation */
315 vsid = sr & 0x00FFFFFF;
316 pgidx = (virtual >> 12) & 0xFFFF;
318 hash = ((vsid ^ pgidx) & 0x0007FFFF) << 6;
319 mask = ((sdr & 0x000001FF) << 16) | 0xFFC0;
320 pg_addr = get_pgaddr(sdr, hash, mask);
321 ptem = (vsid << 7) | (pgidx >> 10);
322 #if defined (DEBUG_MMU)
324 fprintf(logfile, "0 sdr1=0x%08x vsid=0x%06x api=0x%04x "
325 "hash=0x%07x pg_addr=0x%08x\n", sdr, vsid, pgidx, hash,
329 /* Primary table lookup */
330 ret = find_pte(real, prot, pg_addr, ptem, 0, key, rw);
332 /* Secondary table lookup */
333 hash = (~hash) & 0x01FFFFC0;
334 pg_addr = get_pgaddr(sdr, hash, mask);
335 #if defined (DEBUG_MMU)
336 if (virtual != 0xEFFFFFFF && loglevel > 0) {
337 fprintf(logfile, "1 sdr1=0x%08x vsid=0x%06x api=0x%04x "
338 "hash=0x%05x pg_addr=0x%08x\n", sdr, vsid, pgidx,
342 ret2 = find_pte(real, prot, pg_addr, ptem, 1, key, rw);
347 #if defined (DEBUG_MMU)
349 fprintf(logfile, "No access allowed\n");
354 #if defined (DEBUG_MMU)
356 fprintf(logfile, "direct store...\n");
358 /* Direct-store segment : absolutely *BUGGY* for now */
361 /* Integer load/store : only access allowed */
364 /* No code fetch is allowed in direct-store areas */
367 /* Floating point load/store */
370 /* lwarx, ldarx or srwcx. */
373 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
374 /* Should make the instruction do no-op.
375 * As it already do no-op, it's quite easy :-)
384 fprintf(logfile, "ERROR: instruction should not need "
385 "address translation\n");
387 printf("ERROR: instruction should not need "
388 "address translation\n");
391 if ((rw == 1 || key != 1) && (rw == 0 || key != 0)) {
402 int get_physical_address (CPUState *env, uint32_t *physical, int *prot,
403 uint32_t address, int rw, int access_type)
408 fprintf(logfile, "%s\n", __func__);
411 if ((access_type == ACCESS_CODE && msr_ir == 0) || msr_dr == 0) {
412 /* No address translation */
413 *physical = address & ~0xFFF;
414 *prot = PROT_READ | PROT_WRITE;
417 /* Try to find a BAT */
418 ret = get_bat(env, physical, prot, address, rw, access_type);
420 /* We didn't match any BAT entry */
421 ret = get_segment(env, physical, prot, address, rw, access_type);
425 fprintf(logfile, "%s address %08x => %08x\n",
426 __func__, address, *physical);
432 #if defined(CONFIG_USER_ONLY)
433 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
438 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
443 if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
449 #if !defined(CONFIG_USER_ONLY)
451 #define MMUSUFFIX _mmu
452 #define GETPC() (__builtin_return_address(0))
455 #include "softmmu_template.h"
458 #include "softmmu_template.h"
461 #include "softmmu_template.h"
464 #include "softmmu_template.h"
466 /* try to fill the TLB and return an exception if error. If retaddr is
467 NULL, it means that the function was called in C code (i.e. not
468 from generated code or from helper.c) */
469 /* XXX: fix it to restore all registers */
470 void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
472 TranslationBlock *tb;
477 /* XXX: hack to restore env in all cases, even if not called from
480 env = cpu_single_env;
482 unsigned long tlb_addrr, tlb_addrw;
484 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
485 tlb_addrr = env->tlb_read[is_user][index].address;
486 tlb_addrw = env->tlb_write[is_user][index].address;
488 printf("%s 1 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
489 "(0x%08lx 0x%08lx)\n", __func__, env,
490 &env->tlb_read[is_user][index], index, addr,
491 tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
492 tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
495 ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, is_user, 1);
498 /* now we have a real cpu fault */
499 pc = (unsigned long)retaddr;
502 /* the PC is inside the translated code. It means that we have
503 a virtual CPU fault */
504 cpu_restore_state(tb, env, pc, NULL);
507 do_queue_exception_err(env->exception_index, env->error_code);
508 do_process_exceptions();
511 unsigned long tlb_addrr, tlb_addrw;
513 index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
514 tlb_addrr = env->tlb_read[is_user][index].address;
515 tlb_addrw = env->tlb_write[is_user][index].address;
517 printf("%s 2 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
518 "(0x%08lx 0x%08lx)\n", __func__, env,
519 &env->tlb_read[is_user][index], index, addr,
520 tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
521 tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
527 void cpu_ppc_init_mmu(CPUState *env)
529 /* Nothing to do: all translation are disabled */
533 /* Perform address translation */
534 int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
535 int is_user, int is_softmmu)
539 int exception = 0, error_code = 0;
543 // printf("%s 0\n", __func__);
544 access_type = env->access_type;
545 if (env->user_mode_only) {
546 /* user mode only emulation */
550 /* NASTY BUG workaround */
551 if (access_type == ACCESS_CODE && rw) {
552 // printf("%s: ERROR WRITE CODE ACCESS\n", __func__);
553 access_type = ACCESS_INT;
555 ret = get_physical_address(env, &physical, &prot,
556 address, rw, access_type);
558 ret = tlb_set_page(env, address & ~0xFFF, physical, prot,
559 is_user, is_softmmu);
560 } else if (ret < 0) {
562 #if defined (DEBUG_MMU)
564 cpu_ppc_dump_state(env, logfile, 0);
566 if (access_type == ACCESS_CODE) {
567 exception = EXCP_ISI;
570 /* No matches in page tables */
571 error_code = EXCP_ISI_TRANSLATE;
574 /* Access rights violation */
575 error_code = EXCP_ISI_PROT;
578 /* No execute protection violation */
579 error_code = EXCP_ISI_NOEXEC;
582 /* Direct store exception */
583 /* No code fetch is allowed in direct-store areas */
584 error_code = EXCP_ISI_DIRECT;
588 exception = EXCP_DSI;
591 /* No matches in page tables */
592 error_code = EXCP_DSI_TRANSLATE;
595 /* Access rights violation */
596 error_code = EXCP_DSI_PROT;
599 /* Direct store exception */
600 switch (access_type) {
602 /* Floating point load/store */
603 exception = EXCP_ALIGN;
604 error_code = EXCP_ALIGN_FP;
607 /* lwarx, ldarx or srwcx. */
608 exception = EXCP_DSI;
609 error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT;
613 exception = EXCP_DSI;
614 error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT |
618 printf("DSI: invalid exception (%d)\n", ret);
619 exception = EXCP_PROGRAM;
620 error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
625 error_code |= EXCP_DSI_STORE;
626 /* Store fault address */
627 env->spr[DAR] = address;
630 printf("%s: set exception to %d %02x\n",
631 __func__, exception, error_code);
633 env->exception_index = exception;
634 env->error_code = error_code;
641 uint32_t _load_xer (CPUState *env)
643 return (xer_so << XER_SO) |
649 void _store_xer (CPUState *env, uint32_t value)
651 xer_so = (value >> XER_SO) & 0x01;
652 xer_ov = (value >> XER_OV) & 0x01;
653 xer_ca = (value >> XER_CA) & 0x01;
654 xer_bc = (value >> XER_BC) & 0x1f;
657 uint32_t _load_msr (CPUState *env)
659 return (msr_pow << MSR_POW) |
660 (msr_ile << MSR_ILE) |
665 (msr_fe0 << MSR_FE0) |
668 (msr_fe1 << MSR_FE1) |
676 void _store_msr (CPUState *env, uint32_t value)
678 if (((T0 >> MSR_IR) & 0x01) != msr_ir ||
679 ((T0 >> MSR_DR) & 0x01) != msr_dr) {
680 /* Flush all tlb when changing translation mode or privilege level */
683 msr_pow = (value >> MSR_POW) & 0x03;
684 msr_ile = (value >> MSR_ILE) & 0x01;
685 msr_ee = (value >> MSR_EE) & 0x01;
686 msr_pr = (value >> MSR_PR) & 0x01;
687 msr_fp = (value >> MSR_FP) & 0x01;
688 msr_me = (value >> MSR_ME) & 0x01;
689 msr_fe0 = (value >> MSR_FE0) & 0x01;
690 msr_se = (value >> MSR_SE) & 0x01;
691 msr_be = (value >> MSR_BE) & 0x01;
692 msr_fe1 = (value >> MSR_FE1) & 0x01;
693 msr_ip = (value >> MSR_IP) & 0x01;
694 msr_ir = (value >> MSR_IR) & 0x01;
695 msr_dr = (value >> MSR_DR) & 0x01;
696 msr_ri = (value >> MSR_RI) & 0x01;
697 msr_le = (value >> MSR_LE) & 0x01;
700 void do_interrupt (CPUState *env)
702 #if defined (CONFIG_USER_ONLY)
703 env->exception_index |= 0x100;
706 int excp = env->exception_index;
708 /* Dequeue PPC exceptions */
709 if (excp < EXCP_PPC_MAX)
710 env->exceptions &= ~(1 << excp);
711 msr = _load_msr(env);
712 #if defined (DEBUG_EXCEPTIONS)
713 if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1)
716 fprintf(logfile, "Raise exception at 0x%08x => 0x%08x (%02x)\n",
717 env->nip, excp << 8, env->error_code);
720 cpu_ppc_dump_state(env, logfile, 0);
723 /* Generate informations in save/restore registers */
726 #if defined (USE_OPEN_FIRMWARE)
727 env->gpr[3] = OF_client_entry((void *)env->gpr[3]);
731 #if defined (USE_OPEN_FIRMWARE)
732 printf("RTAS call !\n");
733 env->gpr[3] = RTAS_entry((void *)env->gpr[3]);
734 printf("RTAS call done\n");
739 #if defined (DEBUG_EXCEPTIONS)
740 printf("%s: escape EXCP_NONE\n", __func__);
747 case EXCP_MACHINE_CHECK:
749 printf("Machine check exception while not allowed !\n");
752 "Machine check exception while not allowed !\n");
759 /* Store exception cause */
760 /* data location address has been stored
761 * when the fault has been detected
765 if (env->error_code & EXCP_DSI_TRANSLATE)
766 env->spr[DSISR] |= 0x40000000;
767 else if (env->error_code & EXCP_DSI_PROT)
768 env->spr[DSISR] |= 0x08000000;
769 else if (env->error_code & EXCP_DSI_NOTSUP) {
770 env->spr[DSISR] |= 0x80000000;
771 if (env->error_code & EXCP_DSI_DIRECT)
772 env->spr[DSISR] |= 0x04000000;
774 if (env->error_code & EXCP_DSI_STORE)
775 env->spr[DSISR] |= 0x02000000;
776 if ((env->error_code & 0xF) == EXCP_DSI_DABR)
777 env->spr[DSISR] |= 0x00400000;
778 if (env->error_code & EXCP_DSI_ECXW)
779 env->spr[DSISR] |= 0x00100000;
780 #if defined (DEBUG_EXCEPTIONS)
782 fprintf(logfile, "DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
783 env->spr[DSISR], env->spr[DAR]);
785 printf("DSI exception: DSISR=0x%08x, DAR=0x%08x nip=0x%08x\n",
786 env->spr[DSISR], env->spr[DAR], env->nip);
791 /* Store exception cause */
793 if (env->error_code == EXCP_ISI_TRANSLATE)
795 else if (env->error_code == EXCP_ISI_NOEXEC ||
796 env->error_code == EXCP_ISI_GUARD ||
797 env->error_code == EXCP_ISI_DIRECT)
801 #if defined (DEBUG_EXCEPTIONS)
803 fprintf(logfile, "ISI exception: msr=0x%08x, nip=0x%08x\n",
806 printf("ISI exception: msr=0x%08x, nip=0x%08x tbl:0x%08x\n",
807 msr, env->nip, env->spr[V_TBL]);
813 #if defined (DEBUG_EXCEPTIONS)
815 fprintf(logfile, "Skipping hardware interrupt\n");
819 do_queue_exception(EXCP_EXTERNAL);
824 /* Store exception cause */
825 /* Get rS/rD and rA from faulting opcode */
827 (ldl_code((void *)(env->nip - 4)) & 0x03FF0000) >> 16;
828 /* data location address has been stored
829 * when the fault has been detected
834 switch (env->error_code & ~0xF) {
836 if (msr_fe0 == 0 && msr_fe1 == 0) {
837 #if defined (DEBUG_EXCEPTIONS)
838 printf("Ignore floating point exception\n");
844 env->fpscr[7] |= 0x8;
845 /* Finally, update FEX */
846 if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
847 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
848 env->fpscr[7] |= 0x4;
851 printf("Invalid instruction at 0x%08x\n", env->nip);
861 /* Should never occur */
871 do_queue_exception(EXCP_DECR);
876 #if defined (DEBUG_EXCEPTIONS)
879 fprintf(logfile, "syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n",
880 env->gpr[0], env->gpr[3], env->gpr[4],
881 env->gpr[5], env->gpr[6]);
883 printf("syscall %d from 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
884 env->gpr[0], env->nip, env->gpr[3], env->gpr[4],
885 env->gpr[5], env->gpr[6]);
901 /* Restore user-mode state */
903 #if defined (DEBUG_EXCEPTIONS)
905 printf("Return from exception => 0x%08x\n", (uint32_t)env->nip);
909 /* SRR0 is set to current instruction */
910 env->spr[SRR0] = (uint32_t)env->nip - 4;
913 /* SRR0 is set to next instruction */
914 env->spr[SRR0] = (uint32_t)env->nip;
917 env->spr[SRR1] = msr;
918 /* reload MSR with correct bits */
931 /* Jump to handler */
932 env->nip = excp << 8;
933 env->exception_index = EXCP_NONE;
934 /* Invalidate all TLB as we may have changed translation mode */
936 /* ensure that no TB jump will be modified as
937 the program flow was changed */