4 * Copyright (c) 2005 Samuel Tardieu
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
30 #include "hw/sh_intc.h"
32 #if defined(CONFIG_USER_ONLY)
34 void do_interrupt (CPUState *env)
36 env->exception_index = -1;
39 int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
40 int mmu_idx, int is_softmmu)
45 env->exception_index = 0x0a0;
48 env->exception_index = 0x0c0;
51 env->exception_index = 0x0a0;
57 target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
62 #else /* !CONFIG_USER_ONLY */
65 #define MMU_ITLB_MISS (-1)
66 #define MMU_ITLB_MULTIPLE (-2)
67 #define MMU_ITLB_VIOLATION (-3)
68 #define MMU_DTLB_MISS_READ (-4)
69 #define MMU_DTLB_MISS_WRITE (-5)
70 #define MMU_DTLB_INITIAL_WRITE (-6)
71 #define MMU_DTLB_VIOLATION_READ (-7)
72 #define MMU_DTLB_VIOLATION_WRITE (-8)
73 #define MMU_DTLB_MULTIPLE (-9)
74 #define MMU_DTLB_MISS (-10)
76 void do_interrupt(CPUState * env)
78 int do_irq = env->interrupt_request & CPU_INTERRUPT_HARD;
79 int do_exp, irq_vector = env->exception_index;
81 /* prioritize exceptions over interrupts */
83 do_exp = env->exception_index != -1;
84 do_irq = do_irq && (env->exception_index == -1);
86 if (env->sr & SR_BL) {
87 if (do_exp && env->exception_index != 0x1e0) {
88 env->exception_index = 0x000; /* masked exception -> reset */
96 irq_vector = sh_intc_get_pending_vector(env->intc_handle,
97 (env->sr >> 4) & 0xf);
98 if (irq_vector == -1) {
103 if (loglevel & CPU_LOG_INT) {
105 switch (env->exception_index) {
107 expname = "addr_error";
110 expname = "tlb_miss";
113 expname = "tlb_violation";
116 expname = "illegal_instruction";
119 expname = "slot_illegal_instruction";
122 expname = "fpu_disable";
125 expname = "slot_fpu";
128 expname = "data_write";
131 expname = "dtlb_miss_write";
134 expname = "dtlb_violation_write";
137 expname = "fpu_exception";
140 expname = "initial_page_write";
146 expname = do_irq ? "interrupt" : "???";
149 fprintf(logfile, "exception 0x%03x [%s] raised\n",
150 irq_vector, expname);
151 cpu_dump_state(env, logfile, fprintf, 0);
156 env->sgr = env->gregs[15];
157 env->sr |= SR_BL | SR_MD | SR_RB;
160 env->expevt = env->exception_index;
161 switch (env->exception_index) {
166 env->sr |= 0xf << 4; /* IMASK */
167 env->pc = 0xa0000000;
171 env->pc = env->vbr + 0x400;
174 env->spc += 2; /* special case for TRAPA */
177 env->pc = env->vbr + 0x100;
184 env->intevt = irq_vector;
185 env->pc = env->vbr + 0x600;
190 static void update_itlb_use(CPUState * env, int itlbnb)
192 uint8_t or_mask = 0, and_mask = (uint8_t) - 1;
211 env->mmucr &= (and_mask << 24);
212 env->mmucr |= (or_mask << 24);
215 static int itlb_replacement(CPUState * env)
217 if ((env->mmucr & 0xe0000000) == 0xe0000000)
219 if ((env->mmucr & 0x98000000) == 0x08000000)
221 if ((env->mmucr & 0x54000000) == 0x04000000)
223 if ((env->mmucr & 0x2c000000) == 0x00000000)
228 /* Find the corresponding entry in the right TLB
229 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
231 static int find_tlb_entry(CPUState * env, target_ulong address,
232 tlb_t * entries, uint8_t nbtlb, int use_asid)
234 int match = MMU_DTLB_MISS;
239 asid = env->pteh & 0xff;
241 for (i = 0; i < nbtlb; i++) {
243 continue; /* Invalid entry */
244 if (use_asid && entries[i].asid != asid && !entries[i].sh)
245 continue; /* Bad ASID */
247 switch (entries[i].sz) {
249 size = 1024; /* 1kB */
252 size = 4 * 1024; /* 4kB */
255 size = 64 * 1024; /* 64kB */
258 size = 1024 * 1024; /* 1MB */
264 start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
265 end = start + entries[i].size - 1;
266 if (address >= start && address <= end) { /* Match */
268 return MMU_DTLB_MULTIPLE; /* Multiple match */
275 /* Find itlb entry - update itlb from utlb if necessary and asked for
276 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
277 Update the itlb from utlb if update is not 0
279 int find_itlb_entry(CPUState * env, target_ulong address,
280 int use_asid, int update)
284 e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
285 if (e == MMU_DTLB_MULTIPLE)
286 e = MMU_ITLB_MULTIPLE;
287 else if (e == MMU_DTLB_MISS && update) {
288 e = find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
290 n = itlb_replacement(env);
291 env->itlb[n] = env->utlb[e];
296 update_itlb_use(env, e);
301 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
302 int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
307 urb = ((env->mmucr) >> 18) & 0x3f;
308 urc = ((env->mmucr) >> 10) & 0x3f;
310 if (urc == urb || urc == UTLB_SIZE - 1)
312 env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
315 return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
318 /* Match address against MMU
319 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
320 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
321 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
322 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION
324 static int get_mmu_address(CPUState * env, target_ulong * physical,
325 int *prot, target_ulong address,
326 int rw, int access_type)
328 int use_asid, is_code, n;
329 tlb_t *matching = NULL;
331 use_asid = (env->mmucr & MMUCR_SV) == 0 && (env->sr & SR_MD) == 0;
332 is_code = env->pc == address; /* Hack */
334 /* Use a hack to find if this is an instruction or data access */
335 if (env->pc == address && !(rw & PAGE_WRITE)) {
336 n = find_itlb_entry(env, address, use_asid, 1);
338 matching = &env->itlb[n];
339 if ((env->sr & SR_MD) & !(matching->pr & 2))
340 n = MMU_ITLB_VIOLATION;
345 n = find_utlb_entry(env, address, use_asid);
347 matching = &env->utlb[n];
348 switch ((matching->pr << 1) | ((env->sr & SR_MD) ? 1 : 0)) {
351 n = (rw & PAGE_WRITE) ? MMU_DTLB_VIOLATION_WRITE :
352 MMU_DTLB_VIOLATION_READ;
358 n = MMU_DTLB_VIOLATION_WRITE;
365 *prot = rw & (PAGE_READ | PAGE_WRITE);
368 } else if (n == MMU_DTLB_MISS) {
369 n = (rw & PAGE_WRITE) ? MMU_DTLB_MISS_WRITE :
374 *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
375 (address & (matching->size - 1));
376 if ((rw & PAGE_WRITE) & !matching->d)
377 n = MMU_DTLB_INITIAL_WRITE;
384 int get_physical_address(CPUState * env, target_ulong * physical,
385 int *prot, target_ulong address,
386 int rw, int access_type)
388 /* P1, P2 and P4 areas do not use translation */
389 if ((address >= 0x80000000 && address < 0xc0000000) ||
390 address >= 0xe0000000) {
391 if (!(env->sr & SR_MD)
392 && (address < 0xe0000000 || address > 0xe4000000)) {
393 /* Unauthorized access in user mode (only store queues are available) */
394 fprintf(stderr, "Unauthorized access\n");
395 return (rw & PAGE_WRITE) ? MMU_DTLB_MISS_WRITE :
398 /* Mask upper 3 bits */
399 *physical = address & 0x1FFFFFFF;
400 *prot = PAGE_READ | PAGE_WRITE;
404 /* If MMU is disabled, return the corresponding physical page */
405 if (!env->mmucr & MMUCR_AT) {
406 *physical = address & 0x1FFFFFFF;
407 *prot = PAGE_READ | PAGE_WRITE;
411 /* We need to resort to the MMU */
412 return get_mmu_address(env, physical, prot, address, rw, access_type);
415 int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
416 int mmu_idx, int is_softmmu)
418 target_ulong physical, page_offset, page_size;
419 int prot, ret, access_type;
423 fprintf(stderr, "%s pc %08x ad %08x rw %d mmu_idx %d smmu %d\n",
424 __func__, env->pc, address, rw, mmu_idx, is_softmmu);
427 access_type = ACCESS_INT;
429 get_physical_address(env, &physical, &prot, address, rw,
436 case MMU_DTLB_MISS_READ:
437 env->exception_index = 0x040;
439 case MMU_DTLB_MULTIPLE:
440 case MMU_ITLB_MULTIPLE:
441 env->exception_index = 0x140;
443 case MMU_ITLB_VIOLATION:
444 env->exception_index = 0x0a0;
446 case MMU_DTLB_MISS_WRITE:
447 env->exception_index = 0x060;
449 case MMU_DTLB_INITIAL_WRITE:
450 env->exception_index = 0x080;
452 case MMU_DTLB_VIOLATION_READ:
453 env->exception_index = 0x0a0;
455 case MMU_DTLB_VIOLATION_WRITE:
456 env->exception_index = 0x0c0;
464 page_size = TARGET_PAGE_SIZE;
466 (address - (address & TARGET_PAGE_MASK)) & ~(page_size - 1);
467 address = (address & TARGET_PAGE_MASK) + page_offset;
468 physical = (physical & TARGET_PAGE_MASK) + page_offset;
470 return tlb_set_page(env, address, physical, prot, mmu_idx, is_softmmu);
473 target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
475 target_ulong physical;
478 get_physical_address(env, &physical, &prot, addr, PAGE_READ, 0);