Fix typo
[qemu] / target-sh4 / helper.c
index 6be544c..94be136 100644 (file)
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
  */
 #include <stdarg.h>
 #include <stdlib.h>
@@ -23,7 +23,6 @@
 #include <string.h>
 #include <inttypes.h>
 #include <signal.h>
-#include <assert.h>
 
 #include "cpu.h"
 #include "exec-all.h"
@@ -43,13 +42,14 @@ int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
     env->exception_index = 0;
     switch (rw) {
     case 0:
-       env->tea = address;
         env->exception_index = 0x0a0;
         break;
     case 1:
-       env->tea = address;
         env->exception_index = 0x0c0;
         break;
+    case 2:
+        env->exception_index = 0x0a0;
+        break;
     }
     return 1;
 }
@@ -59,6 +59,12 @@ target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
     return addr;
 }
 
+int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
+{
+    /* For user mode, only U0 area is cachable. */
+    return !(addr & 0x80000000);
+}
+
 #else /* !CONFIG_USER_ONLY */
 
 #define MMU_OK                   0
@@ -72,6 +78,9 @@ target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
 #define MMU_DTLB_VIOLATION_WRITE (-8)
 #define MMU_DTLB_MULTIPLE        (-9)
 #define MMU_DTLB_MISS            (-10)
+#define MMU_IADDR_ERROR          (-11)
+#define MMU_DADDR_ERROR_READ     (-12)
+#define MMU_DADDR_ERROR_WRITE    (-13)
 
 void do_interrupt(CPUState * env)
 {
@@ -101,7 +110,7 @@ void do_interrupt(CPUState * env)
        }
     }
 
-    if (loglevel & CPU_LOG_INT) {
+    if (qemu_loglevel_mask(CPU_LOG_INT)) {
        const char *expname;
        switch (env->exception_index) {
        case 0x0e0:
@@ -147,9 +156,9 @@ void do_interrupt(CPUState * env)
             expname = do_irq ? "interrupt" : "???";
             break;
        }
-       fprintf(logfile, "exception 0x%03x [%s] raised\n",
-               irq_vector, expname);
-       cpu_dump_state(env, logfile, fprintf, 0);
+       qemu_log("exception 0x%03x [%s] raised\n",
+                 irq_vector, expname);
+       log_cpu_state(env, 0);
     }
 
     env->ssr = env->sr;
@@ -251,7 +260,7 @@ static int find_tlb_entry(CPUState * env, target_ulong address,
     for (i = 0; i < nbtlb; i++) {
        if (!entries[i].v)
            continue;           /* Invalid entry */
-       if (use_asid && entries[i].asid != asid && !entries[i].sh)
+       if (!entries[i].sh && use_asid && entries[i].asid != asid)
            continue;           /* Bad ASID */
 #if 0
        switch (entries[i].sz) {
@@ -282,12 +291,35 @@ static int find_tlb_entry(CPUState * env, target_ulong address,
     return match;
 }
 
+static int same_tlb_entry_exists(const tlb_t * haystack, uint8_t nbtlb,
+                                const tlb_t * needle)
+{
+    int i;
+    for (i = 0; i < nbtlb; i++)
+        if (!memcmp(&haystack[i], needle, sizeof(tlb_t)))
+           return 1;
+    return 0;
+}
+
+static void increment_urc(CPUState * env)
+{
+    uint8_t urb, urc;
+
+    /* Increment URC */
+    urb = ((env->mmucr) >> 18) & 0x3f;
+    urc = ((env->mmucr) >> 10) & 0x3f;
+    urc++;
+    if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
+       urc = 0;
+    env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
+}
+
 /* Find itlb entry - update itlb from utlb if necessary and asked for
    Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
    Update the itlb from utlb if update is not 0
 */
-int find_itlb_entry(CPUState * env, target_ulong address,
-                   int use_asid, int update)
+static int find_itlb_entry(CPUState * env, target_ulong address,
+                           int use_asid, int update)
 {
     int e, n;
 
@@ -297,8 +329,14 @@ int find_itlb_entry(CPUState * env, target_ulong address,
     else if (e == MMU_DTLB_MISS && update) {
        e = find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
        if (e >= 0) {
+           tlb_t * ientry;
            n = itlb_replacement(env);
-           env->itlb[n] = env->utlb[e];
+           ientry = &env->itlb[n];
+           if (ientry->v) {
+               if (!same_tlb_entry_exists(env->utlb, UTLB_SIZE, ientry))
+                   tlb_flush_page(env, ientry->vpn << 10);
+           }
+           *ientry = env->utlb[e];
            e = n;
        } else if (e == MMU_DTLB_MISS)
            e = MMU_ITLB_MISS;
@@ -311,17 +349,10 @@ int find_itlb_entry(CPUState * env, target_ulong address,
 
 /* Find utlb entry
    Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
-int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
+static int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
 {
-    uint8_t urb, urc;
-
-    /* Increment URC */
-    urb = ((env->mmucr) >> 18) & 0x3f;
-    urc = ((env->mmucr) >> 10) & 0x3f;
-    urc++;
-    if (urc == urb || urc == UTLB_SIZE - 1)
-       urc = 0;
-    env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
+    /* per utlb access */
+    increment_urc(env);
 
     /* Return entry */
     return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
@@ -331,20 +362,19 @@ int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
    Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
    MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
    MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
-   MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION
+   MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
+   MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
 */
 static int get_mmu_address(CPUState * env, target_ulong * physical,
                           int *prot, target_ulong address,
                           int rw, int access_type)
 {
-    int use_asid, is_code, n;
+    int use_asid, n;
     tlb_t *matching = NULL;
 
-    use_asid = (env->mmucr & MMUCR_SV) == 0 && (env->sr & SR_MD) == 0;
-    is_code = env->pc == address;      /* Hack */
+    use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
 
-    /* Use a hack to find if this is an instruction or data access */
-    if (env->pc == address && !(rw & PAGE_WRITE)) {
+    if (rw == 2) {
        n = find_itlb_entry(env, address, use_asid, 1);
        if (n >= 0) {
            matching = &env->itlb[n];
@@ -360,13 +390,13 @@ static int get_mmu_address(CPUState * env, target_ulong * physical,
            switch ((matching->pr << 1) | ((env->sr & SR_MD) ? 1 : 0)) {
            case 0:             /* 000 */
            case 2:             /* 010 */
-               n = (rw & PAGE_WRITE) ? MMU_DTLB_VIOLATION_WRITE :
+               n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE :
                    MMU_DTLB_VIOLATION_READ;
                break;
            case 1:             /* 001 */
            case 4:             /* 100 */
            case 5:             /* 101 */
-               if (rw & PAGE_WRITE)
+               if (rw == 1)
                    n = MMU_DTLB_VIOLATION_WRITE;
                else
                    *prot = PAGE_READ;
@@ -374,18 +404,18 @@ static int get_mmu_address(CPUState * env, target_ulong * physical,
            case 3:             /* 011 */
            case 6:             /* 110 */
            case 7:             /* 111 */
-               *prot = rw & (PAGE_READ | PAGE_WRITE);
+               *prot = (rw == 1)? PAGE_WRITE : PAGE_READ;
                break;
            }
        } else if (n == MMU_DTLB_MISS) {
-           n = (rw & PAGE_WRITE) ? MMU_DTLB_MISS_WRITE :
+           n = (rw == 1) ? MMU_DTLB_MISS_WRITE :
                MMU_DTLB_MISS_READ;
        }
     }
     if (n >= 0) {
        *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
            (address & (matching->size - 1));
-       if ((rw & PAGE_WRITE) & !matching->d)
+       if ((rw == 1) & !matching->d)
            n = MMU_DTLB_INITIAL_WRITE;
        else
            n = MMU_OK;
@@ -393,9 +423,9 @@ static int get_mmu_address(CPUState * env, target_ulong * physical,
     return n;
 }
 
-int get_physical_address(CPUState * env, target_ulong * physical,
-                        int *prot, target_ulong address,
-                        int rw, int access_type)
+static int get_physical_address(CPUState * env, target_ulong * physical,
+                                int *prot, target_ulong address,
+                                int rw, int access_type)
 {
     /* P1, P2 and P4 areas do not use translation */
     if ((address >= 0x80000000 && address < 0xc0000000) ||
@@ -404,11 +434,19 @@ int get_physical_address(CPUState * env, target_ulong * physical,
            && (address < 0xe0000000 || address > 0xe4000000)) {
            /* Unauthorized access in user mode (only store queues are available) */
            fprintf(stderr, "Unauthorized access\n");
-           return (rw & PAGE_WRITE) ? MMU_DTLB_MISS_WRITE :
-               MMU_DTLB_MISS_READ;
+           if (rw == 0)
+               return MMU_DADDR_ERROR_READ;
+           else if (rw == 1)
+               return MMU_DADDR_ERROR_WRITE;
+           else
+               return MMU_IADDR_ERROR;
+       }
+       if (address >= 0x80000000 && address < 0xc0000000) {
+           /* Mask upper 3 bits for P1 and P2 areas */
+           *physical = address & 0x1fffffff;
+       } else {
+           *physical = address;
        }
-       /* Mask upper 3 bits */
-       *physical = address & 0x1FFFFFFF;
        *prot = PAGE_READ | PAGE_WRITE;
        return MMU_OK;
     }
@@ -430,27 +468,6 @@ int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
     target_ulong physical, page_offset, page_size;
     int prot, ret, access_type;
 
-    switch (rw) {
-    case 0:
-        rw = PAGE_READ;
-        break;
-    case 1:
-        rw = PAGE_WRITE;
-        break;
-    case 2: /* READ_ACCESS_TYPE == 2 defined in softmmu_template.h */
-        rw = PAGE_READ;
-        break;
-    default:
-        /* fatal error */
-        assert(0);
-    }
-
-    /* XXXXX */
-#if 0
-    fprintf(stderr, "%s pc %08x ad %08x rw %d mmu_idx %d smmu %d\n",
-           __func__, env->pc, address, rw, mmu_idx, is_softmmu);
-#endif
-
     access_type = ACCESS_INT;
     ret =
        get_physical_address(env, &physical, &prot, address, rw,
@@ -482,6 +499,13 @@ int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
        case MMU_DTLB_VIOLATION_WRITE:
            env->exception_index = 0x0c0;
            break;
+       case MMU_IADDR_ERROR:
+       case MMU_DADDR_ERROR_READ:
+           env->exception_index = 0x0c0;
+           break;
+       case MMU_DADDR_ERROR_WRITE:
+           env->exception_index = 0x100;
+           break;
        default:
            assert(0);
        }
@@ -502,15 +526,23 @@ target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
     target_ulong physical;
     int prot;
 
-    get_physical_address(env, &physical, &prot, addr, PAGE_READ, 0);
+    get_physical_address(env, &physical, &prot, addr, 0, 0);
     return physical;
 }
 
-void cpu_load_tlb(CPUState * env)
+void cpu_load_tlb(CPUSH4State * env)
 {
     int n = cpu_mmucr_urc(env->mmucr);
     tlb_t * entry = &env->utlb[n];
 
+    if (entry->v) {
+        /* Overwriting valid entry in utlb. */
+        target_ulong address = entry->vpn << 10;
+       if (!same_tlb_entry_exists(env->itlb, ITLB_SIZE, entry)) {
+           tlb_flush_page(env, address);
+       }
+    }
+
     /* Take values into cpu status from registers. */
     entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
     entry->vpn  = cpu_pteh_vpn(env->pteh);
@@ -543,4 +575,122 @@ void cpu_load_tlb(CPUState * env)
     entry->tc   = (uint8_t)cpu_ptea_tc(env->ptea);
 }
 
+void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, target_phys_addr_t addr,
+                                   uint32_t mem_value)
+{
+    int associate = addr & 0x0000080;
+    uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
+    uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9);
+    uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
+    uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
+    int use_asid = (s->mmucr & MMUCR_SV) == 0 || (s->sr & SR_MD) == 0;
+
+    if (associate) {
+        int i;
+       tlb_t * utlb_match_entry = NULL;
+       int needs_tlb_flush = 0;
+
+       /* search UTLB */
+       for (i = 0; i < UTLB_SIZE; i++) {
+            tlb_t * entry = &s->utlb[i];
+            if (!entry->v)
+               continue;
+
+            if (entry->vpn == vpn
+                && (!use_asid || entry->asid == asid || entry->sh)) {
+               if (utlb_match_entry) {
+                   /* Multiple TLB Exception */
+                   s->exception_index = 0x140;
+                   s->tea = addr;
+                   break;
+               }
+               if (entry->v && !v)
+                   needs_tlb_flush = 1;
+               entry->v = v;
+               entry->d = d;
+               utlb_match_entry = entry;
+           }
+           increment_urc(s); /* per utlb access */
+       }
+
+       /* search ITLB */
+       for (i = 0; i < ITLB_SIZE; i++) {
+            tlb_t * entry = &s->itlb[i];
+            if (entry->vpn == vpn
+                && (!use_asid || entry->asid == asid || entry->sh)) {
+               if (entry->v && !v)
+                   needs_tlb_flush = 1;
+               if (utlb_match_entry)
+                   *entry = *utlb_match_entry;
+               else
+                   entry->v = v;
+               break;
+           }
+       }
+
+       if (needs_tlb_flush)
+           tlb_flush_page(s, vpn << 10);
+        
+    } else {
+        int index = (addr & 0x00003f00) >> 8;
+        tlb_t * entry = &s->utlb[index];
+       if (entry->v) {
+           /* Overwriting valid entry in utlb. */
+            target_ulong address = entry->vpn << 10;
+           if (!same_tlb_entry_exists(s->itlb, ITLB_SIZE, entry)) {
+               tlb_flush_page(s, address);
+           }
+       }
+       entry->asid = asid;
+       entry->vpn = vpn;
+       entry->d = d;
+       entry->v = v;
+       increment_urc(s);
+    }
+}
+
+int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
+{
+    int n;
+    int use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
+
+    /* check area */
+    if (env->sr & SR_MD) {
+        /* For previledged mode, P2 and P4 area is not cachable. */
+        if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr)
+            return 0;
+    } else {
+        /* For user mode, only U0 area is cachable. */
+        if (0x80000000 <= addr)
+            return 0;
+    }
+
+    /*
+     * TODO : Evaluate CCR and check if the cache is on or off.
+     *        Now CCR is not in CPUSH4State, but in SH7750State.
+     *        When you move the ccr inot CPUSH4State, the code will be
+     *        as follows.
+     */
+#if 0
+    /* check if operand cache is enabled or not. */
+    if (!(env->ccr & 1))
+        return 0;
+#endif
+
+    /* if MMU is off, no check for TLB. */
+    if (env->mmucr & MMUCR_AT)
+        return 1;
+
+    /* check TLB */
+    n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid);
+    if (n >= 0)
+        return env->itlb[n].c;
+
+    n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
+    if (n >= 0)
+        return env->utlb[n].c;
+
+    return 0;
+}
+
 #endif