ppc fixes - gcc 3.4 compile fix (initial patch by Jocelyn Mayer)
[qemu] / target-ppc / helper.c
index ccfd2ea..21e8900 100644 (file)
@@ -17,8 +17,6 @@
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-#include <sys/mman.h>
-
 #include "exec.h"
 #if defined (USE_OPEN_FIRMWARE)
 #include <time.h>
 //#define DEBUG_BATS
 //#define DEBUG_EXCEPTIONS
 
-extern FILE *logfile, *stdout, *stderr;
-void exit (int);
-void abort (void);
-
-void cpu_loop_exit(void)
-{
-    longjmp(env->jmp_env, 1);
-}
-
-void do_process_exceptions (void)
-{
-    cpu_loop_exit();
-}
-
-int check_exception_state (CPUState *env)
-{
-    int i;
-
-    /* Process PPC exceptions */
-    for (i = 1; i  < EXCP_PPC_MAX; i++) {
-        if (env->exceptions & (1 << i)) {
-            switch (i) {
-            case EXCP_EXTERNAL:
-            case EXCP_DECR:
-                if (msr_ee == 0)
-                    return 0;
-                break;
-            case EXCP_PROGRAM:
-                if (env->errors[EXCP_PROGRAM] == EXCP_FP &&
-                    msr_fe0 == 0 && msr_fe1 == 0)
-                    return 0;
-                break;
-            default:
-                break;
-            }
-            env->exception_index = i;
-            env->error_code = env->errors[i];
-            return 1;
-        }
-    }
-
-    return 0;
-}
-
 /*****************************************************************************/
 /* PPC MMU emulation */
-int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
-                              int is_user, int is_softmmu);
 
 /* Perform BAT hit & translation */
 static int get_bat (CPUState *env, uint32_t *real, int *prot,
@@ -133,14 +85,14 @@ static int get_bat (CPUState *env, uint32_t *real, int *prot,
                     ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
                     (virtual & 0x0001F000);
                 if (*BATl & 0x00000001)
-                    *prot = PROT_READ;
+                    *prot = PAGE_READ;
                 if (*BATl & 0x00000002)
-                    *prot = PROT_WRITE | PROT_READ;
+                    *prot = PAGE_WRITE | PAGE_READ;
 #if defined (DEBUG_BATS)
                 if (loglevel > 0) {
                     fprintf(logfile, "BAT %d match: r 0x%08x prot=%c%c\n",
-                            i, *real, *prot & PROT_READ ? 'R' : '-',
-                            *prot & PROT_WRITE ? 'W' : '-');
+                            i, *real, *prot & PAGE_READ ? 'R' : '-',
+                            *prot & PAGE_WRITE ? 'W' : '-');
                 }
 #endif
                 ret = 0;
@@ -163,7 +115,6 @@ static int get_bat (CPUState *env, uint32_t *real, int *prot,
                    *BATu, *BATl, BEPIu, BEPIl, bl);
         }
 #endif
-        env->spr[DAR] = virtual;
     }
     /* No hit */
     return ret;
@@ -178,8 +129,8 @@ static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
     int ret = -1; /* No entry found */
 
     for (i = 0; i < 8; i++) {
-        pte0 = ldl_raw(phys_ram_base + base + (i * 8));
-        pte1 =  ldl_raw(phys_ram_base + base + (i * 8) + 4);
+        pte0 = ldl_phys(base + (i * 8));
+        pte1 =  ldl_phys(base + (i * 8) + 4);
 #if defined (DEBUG_MMU)
        if (loglevel > 0) {
            fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
@@ -204,9 +155,9 @@ static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
                 }
                 /* Check access rights */
                 if (key == 0) {
-                    access = PROT_READ;
+                    access = PAGE_READ;
                     if ((pte1 & 0x00000003) != 0x3)
-                        access |= PROT_WRITE;
+                        access |= PAGE_WRITE;
                 } else {
                     switch (pte1 & 0x00000003) {
                     case 0x0:
@@ -214,16 +165,16 @@ static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
                         break;
                     case 0x1:
                     case 0x3:
-                        access = PROT_READ;
+                        access = PAGE_READ;
                         break;
                     case 0x2:
-                        access = PROT_READ | PROT_WRITE;
+                        access = PAGE_READ | PAGE_WRITE;
                         break;
                     }
                 }
                 if (ret < 0) {
-                   if ((rw == 0 && (access & PROT_READ)) ||
-                       (rw == 1 && (access & PROT_WRITE))) {
+                   if ((rw == 0 && (access & PAGE_READ)) ||
+                       (rw == 1 && (access & PAGE_WRITE))) {
 #if defined (DEBUG_MMU)
                        if (loglevel > 0)
                            fprintf(logfile, "PTE access granted !\n");
@@ -265,11 +216,11 @@ static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
                 store = 1;
            } else {
                /* Force page fault for first write access */
-               *prot &= ~PROT_WRITE;
+               *prot &= ~PAGE_WRITE;
             }
         }
         if (store) {
-           stl_raw(phys_ram_base + base + (good * 8) + 4, keep);
+           stl_phys_notdirty(base + (good * 8) + 4, keep);
        }
     }
 
@@ -402,15 +353,16 @@ int get_physical_address (CPUState *env, uint32_t *physical, int *prot,
                           uint32_t address, int rw, int access_type)
 {
     int ret;
-
+#if 0
     if (loglevel > 0) {
         fprintf(logfile, "%s\n", __func__);
     }
-    
-    if ((access_type == ACCESS_CODE && msr_ir == 0) || msr_dr == 0) {
+#endif    
+    if ((access_type == ACCESS_CODE && msr_ir == 0) ||
+        (access_type != ACCESS_CODE && msr_dr == 0)) {
         /* No address translation */
         *physical = address & ~0xFFF;
-        *prot = PROT_READ | PROT_WRITE;
+        *prot = PAGE_READ | PAGE_WRITE;
         ret = 0;
     } else {
         /* Try to find a BAT */
@@ -420,11 +372,12 @@ int get_physical_address (CPUState *env, uint32_t *physical, int *prot,
             ret = get_segment(env, physical, prot, address, rw, access_type);
         }
     }
+#if 0
     if (loglevel > 0) {
         fprintf(logfile, "%s address %08x => %08x\n",
                __func__, address, *physical);
     }
-    
+#endif    
     return ret;
 }
 
@@ -466,7 +419,7 @@ target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
    NULL, it means that the function was called in C code (i.e. not
    from generated code or from helper.c) */
 /* XXX: fix it to restore all registers */
-void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
+void tlb_fill(target_ulong addr, int is_write, int is_user, void *retaddr)
 {
     TranslationBlock *tb;
     CPUState *saved_env;
@@ -477,20 +430,23 @@ void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
        generated code */
     saved_env = env;
     env = cpu_single_env;
+#if 0
     {
         unsigned long tlb_addrr, tlb_addrw;
         int index;
         index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
         tlb_addrr = env->tlb_read[is_user][index].address;
         tlb_addrw = env->tlb_write[is_user][index].address;
-#if 0
-        printf("%s 1 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
+        if (loglevel) {
+            fprintf(logfile,
+                    "%s 1 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
                "(0x%08lx 0x%08lx)\n", __func__, env,
                &env->tlb_read[is_user][index], index, addr,
                tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
                tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
-#endif
+        }
     }
+#endif
     ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, is_user, 1);
     if (ret) {
         if (retaddr) {
@@ -503,23 +459,22 @@ void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
                 cpu_restore_state(tb, env, pc, NULL);
             }
         }
-        do_queue_exception_err(env->exception_index, env->error_code);
-        do_process_exceptions();
+        do_raise_exception_err(env->exception_index, env->error_code);
     }
+#if 0
     {
         unsigned long tlb_addrr, tlb_addrw;
         int index;
         index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
         tlb_addrr = env->tlb_read[is_user][index].address;
         tlb_addrw = env->tlb_write[is_user][index].address;
-#if 0
         printf("%s 2 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
                "(0x%08lx 0x%08lx)\n", __func__, env,
                &env->tlb_read[is_user][index], index, addr,
                tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
                tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
-#endif
     }
+#endif
     env = saved_env;
 }
 
@@ -539,18 +494,22 @@ int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
     int access_type;
     int ret = 0;
 
-//    printf("%s 0\n", __func__);
-    access_type = env->access_type;
+    if (rw == 2) {
+        /* code access */
+        rw = 0;
+        access_type = ACCESS_CODE;
+    } else {
+        /* data access */
+        /* XXX: put correct access by using cpu_restore_state()
+           correctly */
+        access_type = ACCESS_INT;
+        //        access_type = env->access_type;
+    }
     if (env->user_mode_only) {
         /* user mode only emulation */
-        ret = -1;
+        ret = -2;
         goto do_fault;
     }
-    /* NASTY BUG workaround */
-    if (access_type == ACCESS_CODE && rw) {
-       //      printf("%s: ERROR WRITE CODE ACCESS\n", __func__);
-       access_type = ACCESS_INT;
-    }
     ret = get_physical_address(env, &physical, &prot,
                                address, rw, access_type);
     if (ret == 0) {
@@ -560,7 +519,7 @@ int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
     do_fault:
 #if defined (DEBUG_MMU)
        if (loglevel > 0)
-           cpu_ppc_dump_state(env, logfile, 0);
+           cpu_dump_state(env, logfile, fprintf, 0);
 #endif
         if (access_type == ACCESS_CODE) {
             exception = EXCP_ISI;
@@ -633,7 +592,6 @@ int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
         env->error_code = error_code;
         ret = 1;
     }
-
     return ret;
 }
 
@@ -674,11 +632,14 @@ uint32_t _load_msr (CPUState *env)
 
 void _store_msr (CPUState *env, uint32_t value)
 {
-    if (((T0 >> MSR_IR) & 0x01) != msr_ir ||
-        ((T0 >> MSR_DR) & 0x01) != msr_dr) {
+#if 0 // TRY
+    if (((value >> MSR_IR) & 0x01) != msr_ir ||
+        ((value >> MSR_DR) & 0x01) != msr_dr)
+    {
         /* Flush all tlb when changing translation mode or privilege level */
-        do_tlbia();
+       tlb_flush(env, 1);
     }
+#endif
     msr_pow = (value >> MSR_POW) & 0x03;
     msr_ile = (value >> MSR_ILE) & 0x01;
     msr_ee = (value >> MSR_EE) & 0x01;
@@ -694,19 +655,21 @@ void _store_msr (CPUState *env, uint32_t value)
     msr_dr = (value >> MSR_DR) & 0x01;
     msr_ri = (value >> MSR_RI) & 0x01;
     msr_le = (value >> MSR_LE) & 0x01;
+    /* XXX: should enter PM state if msr_pow has been set */
 }
 
+#if defined (CONFIG_USER_ONLY)
 void do_interrupt (CPUState *env)
 {
-#if defined (CONFIG_USER_ONLY)
-    env->exception_index |= 0x100;
+    env->exception_index = -1;
+}
 #else
+void do_interrupt (CPUState *env)
+{
     uint32_t msr;
-    int excp = env->exception_index;
+    int excp;
 
-    /* Dequeue PPC exceptions */
-    if (excp < EXCP_PPC_MAX)
-        env->exceptions &= ~(1 << excp);
+    excp = env->exception_index;
     msr = _load_msr(env);
 #if defined (DEBUG_EXCEPTIONS)
     if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1) 
@@ -714,11 +677,15 @@ void do_interrupt (CPUState *env)
         if (loglevel > 0) {
             fprintf(logfile, "Raise exception at 0x%08x => 0x%08x (%02x)\n",
                     env->nip, excp << 8, env->error_code);
-    }
+        }
        if (loglevel > 0)
-           cpu_ppc_dump_state(env, logfile, 0);
+           cpu_dump_state(env, logfile, fprintf, 0);
     }
 #endif
+    if (loglevel & CPU_LOG_INT) {
+        fprintf(logfile, "Raise exception at 0x%08x => 0x%08x (%02x)\n",
+                env->nip, excp << 8, env->error_code);
+    }
     /* Generate informations in save/restore registers */
     switch (excp) {
     case EXCP_OFCALL:
@@ -745,13 +712,8 @@ void do_interrupt (CPUState *env)
         goto store_next;
     case EXCP_MACHINE_CHECK:
         if (msr_me == 0) {
-            printf("Machine check exception while not allowed !\n");
-            if (loglevel) {
-                fprintf(logfile,
-                        "Machine check exception while not allowed !\n");
+            cpu_abort(env, "Machine check exception while not allowed\n");
         }
-            abort();
-    }
         msr_me = 0;
         break;
     case EXCP_DSI:
@@ -815,7 +777,7 @@ void do_interrupt (CPUState *env)
     }
 #endif
             /* Requeue it */
-            do_queue_exception(EXCP_EXTERNAL);
+            do_raise_exception(EXCP_EXTERNAL);
             return;
             }
         goto store_next;
@@ -823,7 +785,7 @@ void do_interrupt (CPUState *env)
         /* Store exception cause */
         /* Get rS/rD and rA from faulting opcode */
         env->spr[DSISR] |=
-            (ldl_code((void *)(env->nip - 4)) & 0x03FF0000) >> 16;
+            (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
         /* data location address has been stored
          * when the fault has been detected
          */
@@ -847,7 +809,7 @@ void do_interrupt (CPUState *env)
                 env->fpscr[7] |= 0x4;
         break;
         case EXCP_INVAL:
-           printf("Invalid instruction at 0x%08x\n", env->nip);
+            //     printf("Invalid instruction at 0x%08x\n", env->nip);
             msr |= 0x00080000;
         break;
         case EXCP_PRIV:
@@ -867,24 +829,34 @@ void do_interrupt (CPUState *env)
     case EXCP_DECR:
         if (msr_ee == 0) {
             /* Requeue it */
-            do_queue_exception(EXCP_DECR);
+            do_raise_exception(EXCP_DECR);
             return;
         }
         goto store_next;
     case EXCP_SYSCALL:
-#if defined (DEBUG_EXCEPTIONS)
-       if (msr_pr) {
-           if (loglevel) {
-               fprintf(logfile, "syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n",
-                       env->gpr[0], env->gpr[3], env->gpr[4],
-                       env->gpr[5], env->gpr[6]);
-           } else {
-               printf("syscall %d from 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
-                      env->gpr[0], env->nip, env->gpr[3], env->gpr[4],
-                      env->gpr[5], env->gpr[6]);
-           }
-       }
-#endif
+        if (loglevel & CPU_LOG_INT) {
+            fprintf(logfile, "syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n",
+                    env->gpr[0], env->gpr[3], env->gpr[4],
+                    env->gpr[5], env->gpr[6]);
+            if (env->gpr[0] == 4 && env->gpr[3] == 1) {
+                int len, addr, i;
+                uint8_t c;
+
+                fprintf(logfile, "write: ");
+                addr = env->gpr[4];
+                len = env->gpr[5];
+                if (len > 64)
+                    len = 64;
+                for(i = 0; i < len; i++) {
+                    c = 0;
+                    cpu_memory_rw_debug(env, addr + i, &c, 1, 0);
+                    if (c < 32 || c > 126)
+                        c = '.';
+                    fprintf(logfile, "%c", c);
+                }
+                fprintf(logfile, "\n");
+            }
+        }
         goto store_next;
     case EXCP_TRACE:
         goto store_next;
@@ -931,7 +903,7 @@ void do_interrupt (CPUState *env)
     env->nip = excp << 8;
     env->exception_index = EXCP_NONE;
     /* Invalidate all TLB as we may have changed translation mode */
-    do_tlbia();
+    tlb_flush(env, 1);
     /* ensure that no TB jump will be modified as
        the program flow was changed */
 #ifdef __sparc__
@@ -939,5 +911,6 @@ void do_interrupt (CPUState *env)
 #else
     T0 = 0;
 #endif
-#endif
+    env->exception_index = -1;
 }
+#endif /* !CONFIG_USER_ONLY */