target/sh4: Let get_physical_address() use MMUAccessType access_type
[qemu/ar7.git] / target / sh4 / helper.c
blobbd8e034f174d530354913acb7fa16673702072a0
1 /*
2 * SH4 emulation
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.1 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, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "exec/exec-all.h"
24 #include "exec/log.h"
26 #if !defined(CONFIG_USER_ONLY)
27 #include "hw/sh4/sh_intc.h"
28 #include "sysemu/runstate.h"
29 #endif
31 #define MMU_OK 0
32 #define MMU_ITLB_MISS (-1)
33 #define MMU_ITLB_MULTIPLE (-2)
34 #define MMU_ITLB_VIOLATION (-3)
35 #define MMU_DTLB_MISS_READ (-4)
36 #define MMU_DTLB_MISS_WRITE (-5)
37 #define MMU_DTLB_INITIAL_WRITE (-6)
38 #define MMU_DTLB_VIOLATION_READ (-7)
39 #define MMU_DTLB_VIOLATION_WRITE (-8)
40 #define MMU_DTLB_MULTIPLE (-9)
41 #define MMU_DTLB_MISS (-10)
42 #define MMU_IADDR_ERROR (-11)
43 #define MMU_DADDR_ERROR_READ (-12)
44 #define MMU_DADDR_ERROR_WRITE (-13)
46 #if defined(CONFIG_USER_ONLY)
48 void superh_cpu_do_interrupt(CPUState *cs)
50 cs->exception_index = -1;
53 int cpu_sh4_is_cached(CPUSH4State *env, target_ulong addr)
55 /* For user mode, only U0 area is cacheable. */
56 return !(addr & 0x80000000);
59 #else /* !CONFIG_USER_ONLY */
61 void superh_cpu_do_interrupt(CPUState *cs)
63 SuperHCPU *cpu = SUPERH_CPU(cs);
64 CPUSH4State *env = &cpu->env;
65 int do_irq = cs->interrupt_request & CPU_INTERRUPT_HARD;
66 int do_exp, irq_vector = cs->exception_index;
68 /* prioritize exceptions over interrupts */
70 do_exp = cs->exception_index != -1;
71 do_irq = do_irq && (cs->exception_index == -1);
73 if (env->sr & (1u << SR_BL)) {
74 if (do_exp && cs->exception_index != 0x1e0) {
75 /* In theory a masked exception generates a reset exception,
76 which in turn jumps to the reset vector. However this only
77 works when using a bootloader. When using a kernel and an
78 initrd, they need to be reloaded and the program counter
79 should be loaded with the kernel entry point.
80 qemu_system_reset_request takes care of that. */
81 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
82 return;
84 if (do_irq && !env->in_sleep) {
85 return; /* masked */
88 env->in_sleep = 0;
90 if (do_irq) {
91 irq_vector = sh_intc_get_pending_vector(env->intc_handle,
92 (env->sr >> 4) & 0xf);
93 if (irq_vector == -1) {
94 return; /* masked */
98 if (qemu_loglevel_mask(CPU_LOG_INT)) {
99 const char *expname;
100 switch (cs->exception_index) {
101 case 0x0e0:
102 expname = "addr_error";
103 break;
104 case 0x040:
105 expname = "tlb_miss";
106 break;
107 case 0x0a0:
108 expname = "tlb_violation";
109 break;
110 case 0x180:
111 expname = "illegal_instruction";
112 break;
113 case 0x1a0:
114 expname = "slot_illegal_instruction";
115 break;
116 case 0x800:
117 expname = "fpu_disable";
118 break;
119 case 0x820:
120 expname = "slot_fpu";
121 break;
122 case 0x100:
123 expname = "data_write";
124 break;
125 case 0x060:
126 expname = "dtlb_miss_write";
127 break;
128 case 0x0c0:
129 expname = "dtlb_violation_write";
130 break;
131 case 0x120:
132 expname = "fpu_exception";
133 break;
134 case 0x080:
135 expname = "initial_page_write";
136 break;
137 case 0x160:
138 expname = "trapa";
139 break;
140 default:
141 expname = do_irq ? "interrupt" : "???";
142 break;
144 qemu_log("exception 0x%03x [%s] raised\n",
145 irq_vector, expname);
146 log_cpu_state(cs, 0);
149 env->ssr = cpu_read_sr(env);
150 env->spc = env->pc;
151 env->sgr = env->gregs[15];
152 env->sr |= (1u << SR_BL) | (1u << SR_MD) | (1u << SR_RB);
153 env->lock_addr = -1;
155 if (env->flags & DELAY_SLOT_MASK) {
156 /* Branch instruction should be executed again before delay slot. */
157 env->spc -= 2;
158 /* Clear flags for exception/interrupt routine. */
159 env->flags &= ~DELAY_SLOT_MASK;
162 if (do_exp) {
163 env->expevt = cs->exception_index;
164 switch (cs->exception_index) {
165 case 0x000:
166 case 0x020:
167 case 0x140:
168 env->sr &= ~(1u << SR_FD);
169 env->sr |= 0xf << 4; /* IMASK */
170 env->pc = 0xa0000000;
171 break;
172 case 0x040:
173 case 0x060:
174 env->pc = env->vbr + 0x400;
175 break;
176 case 0x160:
177 env->spc += 2; /* special case for TRAPA */
178 /* fall through */
179 default:
180 env->pc = env->vbr + 0x100;
181 break;
183 return;
186 if (do_irq) {
187 env->intevt = irq_vector;
188 env->pc = env->vbr + 0x600;
189 return;
193 static void update_itlb_use(CPUSH4State * env, int itlbnb)
195 uint8_t or_mask = 0, and_mask = (uint8_t) - 1;
197 switch (itlbnb) {
198 case 0:
199 and_mask = 0x1f;
200 break;
201 case 1:
202 and_mask = 0xe7;
203 or_mask = 0x80;
204 break;
205 case 2:
206 and_mask = 0xfb;
207 or_mask = 0x50;
208 break;
209 case 3:
210 or_mask = 0x2c;
211 break;
214 env->mmucr &= (and_mask << 24) | 0x00ffffff;
215 env->mmucr |= (or_mask << 24);
218 static int itlb_replacement(CPUSH4State * env)
220 if ((env->mmucr & 0xe0000000) == 0xe0000000) {
221 return 0;
223 if ((env->mmucr & 0x98000000) == 0x18000000) {
224 return 1;
226 if ((env->mmucr & 0x54000000) == 0x04000000) {
227 return 2;
229 if ((env->mmucr & 0x2c000000) == 0x00000000) {
230 return 3;
232 cpu_abort(env_cpu(env), "Unhandled itlb_replacement");
235 /* Find the corresponding entry in the right TLB
236 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
238 static int find_tlb_entry(CPUSH4State * env, target_ulong address,
239 tlb_t * entries, uint8_t nbtlb, int use_asid)
241 int match = MMU_DTLB_MISS;
242 uint32_t start, end;
243 uint8_t asid;
244 int i;
246 asid = env->pteh & 0xff;
248 for (i = 0; i < nbtlb; i++) {
249 if (!entries[i].v)
250 continue; /* Invalid entry */
251 if (!entries[i].sh && use_asid && entries[i].asid != asid)
252 continue; /* Bad ASID */
253 start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
254 end = start + entries[i].size - 1;
255 if (address >= start && address <= end) { /* Match */
256 if (match != MMU_DTLB_MISS)
257 return MMU_DTLB_MULTIPLE; /* Multiple match */
258 match = i;
261 return match;
264 static void increment_urc(CPUSH4State * env)
266 uint8_t urb, urc;
268 /* Increment URC */
269 urb = ((env->mmucr) >> 18) & 0x3f;
270 urc = ((env->mmucr) >> 10) & 0x3f;
271 urc++;
272 if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
273 urc = 0;
274 env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
277 /* Copy and utlb entry into itlb
278 Return entry
280 static int copy_utlb_entry_itlb(CPUSH4State *env, int utlb)
282 int itlb;
284 tlb_t * ientry;
285 itlb = itlb_replacement(env);
286 ientry = &env->itlb[itlb];
287 if (ientry->v) {
288 tlb_flush_page(env_cpu(env), ientry->vpn << 10);
290 *ientry = env->utlb[utlb];
291 update_itlb_use(env, itlb);
292 return itlb;
295 /* Find itlb entry
296 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
298 static int find_itlb_entry(CPUSH4State * env, target_ulong address,
299 int use_asid)
301 int e;
303 e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
304 if (e == MMU_DTLB_MULTIPLE) {
305 e = MMU_ITLB_MULTIPLE;
306 } else if (e == MMU_DTLB_MISS) {
307 e = MMU_ITLB_MISS;
308 } else if (e >= 0) {
309 update_itlb_use(env, e);
311 return e;
314 /* Find utlb entry
315 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
316 static int find_utlb_entry(CPUSH4State * env, target_ulong address, int use_asid)
318 /* per utlb access */
319 increment_urc(env);
321 /* Return entry */
322 return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
325 /* Match address against MMU
326 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
327 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
328 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
329 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
330 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
332 static int get_mmu_address(CPUSH4State * env, target_ulong * physical,
333 int *prot, target_ulong address,
334 MMUAccessType access_type)
336 int use_asid, n;
337 tlb_t *matching = NULL;
339 use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD));
341 if (access_type == MMU_INST_FETCH) {
342 n = find_itlb_entry(env, address, use_asid);
343 if (n >= 0) {
344 matching = &env->itlb[n];
345 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
346 n = MMU_ITLB_VIOLATION;
347 } else {
348 *prot = PAGE_EXEC;
350 } else {
351 n = find_utlb_entry(env, address, use_asid);
352 if (n >= 0) {
353 n = copy_utlb_entry_itlb(env, n);
354 matching = &env->itlb[n];
355 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
356 n = MMU_ITLB_VIOLATION;
357 } else {
358 *prot = PAGE_READ | PAGE_EXEC;
359 if ((matching->pr & 1) && matching->d) {
360 *prot |= PAGE_WRITE;
363 } else if (n == MMU_DTLB_MULTIPLE) {
364 n = MMU_ITLB_MULTIPLE;
365 } else if (n == MMU_DTLB_MISS) {
366 n = MMU_ITLB_MISS;
369 } else {
370 n = find_utlb_entry(env, address, use_asid);
371 if (n >= 0) {
372 matching = &env->utlb[n];
373 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
374 n = (access_type == MMU_DATA_STORE)
375 ? MMU_DTLB_VIOLATION_WRITE : MMU_DTLB_VIOLATION_READ;
376 } else if ((access_type == MMU_DATA_STORE) && !(matching->pr & 1)) {
377 n = MMU_DTLB_VIOLATION_WRITE;
378 } else if ((access_type == MMU_DATA_STORE) && !matching->d) {
379 n = MMU_DTLB_INITIAL_WRITE;
380 } else {
381 *prot = PAGE_READ;
382 if ((matching->pr & 1) && matching->d) {
383 *prot |= PAGE_WRITE;
386 } else if (n == MMU_DTLB_MISS) {
387 n = (access_type == MMU_DATA_STORE)
388 ? MMU_DTLB_MISS_WRITE : MMU_DTLB_MISS_READ;
391 if (n >= 0) {
392 n = MMU_OK;
393 *physical = ((matching->ppn << 10) & ~(matching->size - 1))
394 | (address & (matching->size - 1));
396 return n;
399 static int get_physical_address(CPUSH4State * env, target_ulong * physical,
400 int *prot, target_ulong address,
401 MMUAccessType access_type)
403 /* P1, P2 and P4 areas do not use translation */
404 if ((address >= 0x80000000 && address < 0xc0000000) || address >= 0xe0000000) {
405 if (!(env->sr & (1u << SR_MD))
406 && (address < 0xe0000000 || address >= 0xe4000000)) {
407 /* Unauthorized access in user mode (only store queues are available) */
408 qemu_log_mask(LOG_GUEST_ERROR, "Unauthorized access\n");
409 if (access_type == MMU_DATA_LOAD) {
410 return MMU_DADDR_ERROR_READ;
411 } else if (access_type == MMU_DATA_STORE) {
412 return MMU_DADDR_ERROR_WRITE;
413 } else {
414 return MMU_IADDR_ERROR;
417 if (address >= 0x80000000 && address < 0xc0000000) {
418 /* Mask upper 3 bits for P1 and P2 areas */
419 *physical = address & 0x1fffffff;
420 } else {
421 *physical = address;
423 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
424 return MMU_OK;
427 /* If MMU is disabled, return the corresponding physical page */
428 if (!(env->mmucr & MMUCR_AT)) {
429 *physical = address & 0x1FFFFFFF;
430 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
431 return MMU_OK;
434 /* We need to resort to the MMU */
435 return get_mmu_address(env, physical, prot, address, access_type);
438 hwaddr superh_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
440 SuperHCPU *cpu = SUPERH_CPU(cs);
441 target_ulong physical;
442 int prot;
444 get_physical_address(&cpu->env, &physical, &prot, addr, MMU_DATA_LOAD);
446 return physical;
449 void cpu_load_tlb(CPUSH4State * env)
451 CPUState *cs = env_cpu(env);
452 int n = cpu_mmucr_urc(env->mmucr);
453 tlb_t * entry = &env->utlb[n];
455 if (entry->v) {
456 /* Overwriting valid entry in utlb. */
457 target_ulong address = entry->vpn << 10;
458 tlb_flush_page(cs, address);
461 /* Take values into cpu status from registers. */
462 entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
463 entry->vpn = cpu_pteh_vpn(env->pteh);
464 entry->v = (uint8_t)cpu_ptel_v(env->ptel);
465 entry->ppn = cpu_ptel_ppn(env->ptel);
466 entry->sz = (uint8_t)cpu_ptel_sz(env->ptel);
467 switch (entry->sz) {
468 case 0: /* 00 */
469 entry->size = 1024; /* 1K */
470 break;
471 case 1: /* 01 */
472 entry->size = 1024 * 4; /* 4K */
473 break;
474 case 2: /* 10 */
475 entry->size = 1024 * 64; /* 64K */
476 break;
477 case 3: /* 11 */
478 entry->size = 1024 * 1024; /* 1M */
479 break;
480 default:
481 cpu_abort(cs, "Unhandled load_tlb");
482 break;
484 entry->sh = (uint8_t)cpu_ptel_sh(env->ptel);
485 entry->c = (uint8_t)cpu_ptel_c(env->ptel);
486 entry->pr = (uint8_t)cpu_ptel_pr(env->ptel);
487 entry->d = (uint8_t)cpu_ptel_d(env->ptel);
488 entry->wt = (uint8_t)cpu_ptel_wt(env->ptel);
489 entry->sa = (uint8_t)cpu_ptea_sa(env->ptea);
490 entry->tc = (uint8_t)cpu_ptea_tc(env->ptea);
493 void cpu_sh4_invalidate_tlb(CPUSH4State *s)
495 int i;
497 /* UTLB */
498 for (i = 0; i < UTLB_SIZE; i++) {
499 tlb_t * entry = &s->utlb[i];
500 entry->v = 0;
502 /* ITLB */
503 for (i = 0; i < ITLB_SIZE; i++) {
504 tlb_t * entry = &s->itlb[i];
505 entry->v = 0;
508 tlb_flush(env_cpu(s));
511 uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State *s,
512 hwaddr addr)
514 int index = (addr & 0x00000300) >> 8;
515 tlb_t * entry = &s->itlb[index];
517 return (entry->vpn << 10) |
518 (entry->v << 8) |
519 (entry->asid);
522 void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State *s, hwaddr addr,
523 uint32_t mem_value)
525 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
526 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
527 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
529 int index = (addr & 0x00000300) >> 8;
530 tlb_t * entry = &s->itlb[index];
531 if (entry->v) {
532 /* Overwriting valid entry in itlb. */
533 target_ulong address = entry->vpn << 10;
534 tlb_flush_page(env_cpu(s), address);
536 entry->asid = asid;
537 entry->vpn = vpn;
538 entry->v = v;
541 uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State *s,
542 hwaddr addr)
544 int array = (addr & 0x00800000) >> 23;
545 int index = (addr & 0x00000300) >> 8;
546 tlb_t * entry = &s->itlb[index];
548 if (array == 0) {
549 /* ITLB Data Array 1 */
550 return (entry->ppn << 10) |
551 (entry->v << 8) |
552 (entry->pr << 5) |
553 ((entry->sz & 1) << 6) |
554 ((entry->sz & 2) << 4) |
555 (entry->c << 3) |
556 (entry->sh << 1);
557 } else {
558 /* ITLB Data Array 2 */
559 return (entry->tc << 1) |
560 (entry->sa);
564 void cpu_sh4_write_mmaped_itlb_data(CPUSH4State *s, hwaddr addr,
565 uint32_t mem_value)
567 int array = (addr & 0x00800000) >> 23;
568 int index = (addr & 0x00000300) >> 8;
569 tlb_t * entry = &s->itlb[index];
571 if (array == 0) {
572 /* ITLB Data Array 1 */
573 if (entry->v) {
574 /* Overwriting valid entry in utlb. */
575 target_ulong address = entry->vpn << 10;
576 tlb_flush_page(env_cpu(s), address);
578 entry->ppn = (mem_value & 0x1ffffc00) >> 10;
579 entry->v = (mem_value & 0x00000100) >> 8;
580 entry->sz = (mem_value & 0x00000080) >> 6 |
581 (mem_value & 0x00000010) >> 4;
582 entry->pr = (mem_value & 0x00000040) >> 5;
583 entry->c = (mem_value & 0x00000008) >> 3;
584 entry->sh = (mem_value & 0x00000002) >> 1;
585 } else {
586 /* ITLB Data Array 2 */
587 entry->tc = (mem_value & 0x00000008) >> 3;
588 entry->sa = (mem_value & 0x00000007);
592 uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State *s,
593 hwaddr addr)
595 int index = (addr & 0x00003f00) >> 8;
596 tlb_t * entry = &s->utlb[index];
598 increment_urc(s); /* per utlb access */
600 return (entry->vpn << 10) |
601 (entry->v << 8) |
602 (entry->asid);
605 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, hwaddr addr,
606 uint32_t mem_value)
608 int associate = addr & 0x0000080;
609 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
610 uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9);
611 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
612 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
613 int use_asid = !(s->mmucr & MMUCR_SV) || !(s->sr & (1u << SR_MD));
615 if (associate) {
616 int i;
617 tlb_t * utlb_match_entry = NULL;
618 int needs_tlb_flush = 0;
620 /* search UTLB */
621 for (i = 0; i < UTLB_SIZE; i++) {
622 tlb_t * entry = &s->utlb[i];
623 if (!entry->v)
624 continue;
626 if (entry->vpn == vpn
627 && (!use_asid || entry->asid == asid || entry->sh)) {
628 if (utlb_match_entry) {
629 CPUState *cs = env_cpu(s);
631 /* Multiple TLB Exception */
632 cs->exception_index = 0x140;
633 s->tea = addr;
634 break;
636 if (entry->v && !v)
637 needs_tlb_flush = 1;
638 entry->v = v;
639 entry->d = d;
640 utlb_match_entry = entry;
642 increment_urc(s); /* per utlb access */
645 /* search ITLB */
646 for (i = 0; i < ITLB_SIZE; i++) {
647 tlb_t * entry = &s->itlb[i];
648 if (entry->vpn == vpn
649 && (!use_asid || entry->asid == asid || entry->sh)) {
650 if (entry->v && !v)
651 needs_tlb_flush = 1;
652 if (utlb_match_entry)
653 *entry = *utlb_match_entry;
654 else
655 entry->v = v;
656 break;
660 if (needs_tlb_flush) {
661 tlb_flush_page(env_cpu(s), vpn << 10);
663 } else {
664 int index = (addr & 0x00003f00) >> 8;
665 tlb_t * entry = &s->utlb[index];
666 if (entry->v) {
667 CPUState *cs = env_cpu(s);
669 /* Overwriting valid entry in utlb. */
670 target_ulong address = entry->vpn << 10;
671 tlb_flush_page(cs, address);
673 entry->asid = asid;
674 entry->vpn = vpn;
675 entry->d = d;
676 entry->v = v;
677 increment_urc(s);
681 uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State *s,
682 hwaddr addr)
684 int array = (addr & 0x00800000) >> 23;
685 int index = (addr & 0x00003f00) >> 8;
686 tlb_t * entry = &s->utlb[index];
688 increment_urc(s); /* per utlb access */
690 if (array == 0) {
691 /* ITLB Data Array 1 */
692 return (entry->ppn << 10) |
693 (entry->v << 8) |
694 (entry->pr << 5) |
695 ((entry->sz & 1) << 6) |
696 ((entry->sz & 2) << 4) |
697 (entry->c << 3) |
698 (entry->d << 2) |
699 (entry->sh << 1) |
700 (entry->wt);
701 } else {
702 /* ITLB Data Array 2 */
703 return (entry->tc << 1) |
704 (entry->sa);
708 void cpu_sh4_write_mmaped_utlb_data(CPUSH4State *s, hwaddr addr,
709 uint32_t mem_value)
711 int array = (addr & 0x00800000) >> 23;
712 int index = (addr & 0x00003f00) >> 8;
713 tlb_t * entry = &s->utlb[index];
715 increment_urc(s); /* per utlb access */
717 if (array == 0) {
718 /* UTLB Data Array 1 */
719 if (entry->v) {
720 /* Overwriting valid entry in utlb. */
721 target_ulong address = entry->vpn << 10;
722 tlb_flush_page(env_cpu(s), address);
724 entry->ppn = (mem_value & 0x1ffffc00) >> 10;
725 entry->v = (mem_value & 0x00000100) >> 8;
726 entry->sz = (mem_value & 0x00000080) >> 6 |
727 (mem_value & 0x00000010) >> 4;
728 entry->pr = (mem_value & 0x00000060) >> 5;
729 entry->c = (mem_value & 0x00000008) >> 3;
730 entry->d = (mem_value & 0x00000004) >> 2;
731 entry->sh = (mem_value & 0x00000002) >> 1;
732 entry->wt = (mem_value & 0x00000001);
733 } else {
734 /* UTLB Data Array 2 */
735 entry->tc = (mem_value & 0x00000008) >> 3;
736 entry->sa = (mem_value & 0x00000007);
740 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
742 int n;
743 int use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD));
745 /* check area */
746 if (env->sr & (1u << SR_MD)) {
747 /* For privileged mode, P2 and P4 area is not cacheable. */
748 if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr)
749 return 0;
750 } else {
751 /* For user mode, only U0 area is cacheable. */
752 if (0x80000000 <= addr)
753 return 0;
757 * TODO : Evaluate CCR and check if the cache is on or off.
758 * Now CCR is not in CPUSH4State, but in SH7750State.
759 * When you move the ccr into CPUSH4State, the code will be
760 * as follows.
762 #if 0
763 /* check if operand cache is enabled or not. */
764 if (!(env->ccr & 1))
765 return 0;
766 #endif
768 /* if MMU is off, no check for TLB. */
769 if (env->mmucr & MMUCR_AT)
770 return 1;
772 /* check TLB */
773 n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid);
774 if (n >= 0)
775 return env->itlb[n].c;
777 n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
778 if (n >= 0)
779 return env->utlb[n].c;
781 return 0;
784 #endif
786 bool superh_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
788 if (interrupt_request & CPU_INTERRUPT_HARD) {
789 SuperHCPU *cpu = SUPERH_CPU(cs);
790 CPUSH4State *env = &cpu->env;
792 /* Delay slots are indivisible, ignore interrupts */
793 if (env->flags & DELAY_SLOT_MASK) {
794 return false;
795 } else {
796 superh_cpu_do_interrupt(cs);
797 return true;
800 return false;
803 bool superh_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
804 MMUAccessType access_type, int mmu_idx,
805 bool probe, uintptr_t retaddr)
807 SuperHCPU *cpu = SUPERH_CPU(cs);
808 CPUSH4State *env = &cpu->env;
809 int ret;
811 #ifdef CONFIG_USER_ONLY
812 ret = (access_type == MMU_DATA_STORE ? MMU_DTLB_VIOLATION_WRITE :
813 access_type == MMU_INST_FETCH ? MMU_ITLB_VIOLATION :
814 MMU_DTLB_VIOLATION_READ);
815 #else
816 target_ulong physical;
817 int prot;
819 ret = get_physical_address(env, &physical, &prot, address, access_type);
821 if (ret == MMU_OK) {
822 address &= TARGET_PAGE_MASK;
823 physical &= TARGET_PAGE_MASK;
824 tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
825 return true;
827 if (probe) {
828 return false;
831 if (ret != MMU_DTLB_MULTIPLE && ret != MMU_ITLB_MULTIPLE) {
832 env->pteh = (env->pteh & PTEH_ASID_MASK) | (address & PTEH_VPN_MASK);
834 #endif
836 env->tea = address;
837 switch (ret) {
838 case MMU_ITLB_MISS:
839 case MMU_DTLB_MISS_READ:
840 cs->exception_index = 0x040;
841 break;
842 case MMU_DTLB_MULTIPLE:
843 case MMU_ITLB_MULTIPLE:
844 cs->exception_index = 0x140;
845 break;
846 case MMU_ITLB_VIOLATION:
847 cs->exception_index = 0x0a0;
848 break;
849 case MMU_DTLB_MISS_WRITE:
850 cs->exception_index = 0x060;
851 break;
852 case MMU_DTLB_INITIAL_WRITE:
853 cs->exception_index = 0x080;
854 break;
855 case MMU_DTLB_VIOLATION_READ:
856 cs->exception_index = 0x0a0;
857 break;
858 case MMU_DTLB_VIOLATION_WRITE:
859 cs->exception_index = 0x0c0;
860 break;
861 case MMU_IADDR_ERROR:
862 case MMU_DADDR_ERROR_READ:
863 cs->exception_index = 0x0e0;
864 break;
865 case MMU_DADDR_ERROR_WRITE:
866 cs->exception_index = 0x100;
867 break;
868 default:
869 cpu_abort(cs, "Unhandled MMU fault");
871 cpu_loop_exit_restore(cs, retaddr);