xref: /openbmc/qemu/target/sh4/helper.c (revision 52f2b896)
1 /*
2  *  SH4 emulation
3  *
4  *  Copyright (c) 2005 Samuel Tardieu
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "cpu.h"
22 #include "exec/exec-all.h"
23 #include "exec/log.h"
24 #include "sysemu/sysemu.h"
25 
26 #if !defined(CONFIG_USER_ONLY)
27 #include "hw/sh4/sh_intc.h"
28 #endif
29 
30 #define MMU_OK                   0
31 #define MMU_ITLB_MISS            (-1)
32 #define MMU_ITLB_MULTIPLE        (-2)
33 #define MMU_ITLB_VIOLATION       (-3)
34 #define MMU_DTLB_MISS_READ       (-4)
35 #define MMU_DTLB_MISS_WRITE      (-5)
36 #define MMU_DTLB_INITIAL_WRITE   (-6)
37 #define MMU_DTLB_VIOLATION_READ  (-7)
38 #define MMU_DTLB_VIOLATION_WRITE (-8)
39 #define MMU_DTLB_MULTIPLE        (-9)
40 #define MMU_DTLB_MISS            (-10)
41 #define MMU_IADDR_ERROR          (-11)
42 #define MMU_DADDR_ERROR_READ     (-12)
43 #define MMU_DADDR_ERROR_WRITE    (-13)
44 
45 #if defined(CONFIG_USER_ONLY)
46 
47 void superh_cpu_do_interrupt(CPUState *cs)
48 {
49     cs->exception_index = -1;
50 }
51 
52 int cpu_sh4_is_cached(CPUSH4State *env, target_ulong addr)
53 {
54     /* For user mode, only U0 area is cacheable. */
55     return !(addr & 0x80000000);
56 }
57 
58 #else /* !CONFIG_USER_ONLY */
59 
60 void superh_cpu_do_interrupt(CPUState *cs)
61 {
62     SuperHCPU *cpu = SUPERH_CPU(cs);
63     CPUSH4State *env = &cpu->env;
64     int do_irq = cs->interrupt_request & CPU_INTERRUPT_HARD;
65     int do_exp, irq_vector = cs->exception_index;
66 
67     /* prioritize exceptions over interrupts */
68 
69     do_exp = cs->exception_index != -1;
70     do_irq = do_irq && (cs->exception_index == -1);
71 
72     if (env->sr & (1u << SR_BL)) {
73         if (do_exp && cs->exception_index != 0x1e0) {
74             /* In theory a masked exception generates a reset exception,
75                which in turn jumps to the reset vector. However this only
76                works when using a bootloader. When using a kernel and an
77                initrd, they need to be reloaded and the program counter
78                should be loaded with the kernel entry point.
79                qemu_system_reset_request takes care of that.  */
80             qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
81             return;
82         }
83         if (do_irq && !env->in_sleep) {
84             return; /* masked */
85         }
86     }
87     env->in_sleep = 0;
88 
89     if (do_irq) {
90         irq_vector = sh_intc_get_pending_vector(env->intc_handle,
91 						(env->sr >> 4) & 0xf);
92         if (irq_vector == -1) {
93             return; /* masked */
94 	}
95     }
96 
97     if (qemu_loglevel_mask(CPU_LOG_INT)) {
98 	const char *expname;
99         switch (cs->exception_index) {
100 	case 0x0e0:
101 	    expname = "addr_error";
102 	    break;
103 	case 0x040:
104 	    expname = "tlb_miss";
105 	    break;
106 	case 0x0a0:
107 	    expname = "tlb_violation";
108 	    break;
109 	case 0x180:
110 	    expname = "illegal_instruction";
111 	    break;
112 	case 0x1a0:
113 	    expname = "slot_illegal_instruction";
114 	    break;
115 	case 0x800:
116 	    expname = "fpu_disable";
117 	    break;
118 	case 0x820:
119 	    expname = "slot_fpu";
120 	    break;
121 	case 0x100:
122 	    expname = "data_write";
123 	    break;
124 	case 0x060:
125 	    expname = "dtlb_miss_write";
126 	    break;
127 	case 0x0c0:
128 	    expname = "dtlb_violation_write";
129 	    break;
130 	case 0x120:
131 	    expname = "fpu_exception";
132 	    break;
133 	case 0x080:
134 	    expname = "initial_page_write";
135 	    break;
136 	case 0x160:
137 	    expname = "trapa";
138 	    break;
139 	default:
140             expname = do_irq ? "interrupt" : "???";
141             break;
142 	}
143 	qemu_log("exception 0x%03x [%s] raised\n",
144 		  irq_vector, expname);
145         log_cpu_state(cs, 0);
146     }
147 
148     env->ssr = cpu_read_sr(env);
149     env->spc = env->pc;
150     env->sgr = env->gregs[15];
151     env->sr |= (1u << SR_BL) | (1u << SR_MD) | (1u << SR_RB);
152     env->lock_addr = -1;
153 
154     if (env->flags & DELAY_SLOT_MASK) {
155         /* Branch instruction should be executed again before delay slot. */
156 	env->spc -= 2;
157 	/* Clear flags for exception/interrupt routine. */
158         env->flags &= ~DELAY_SLOT_MASK;
159     }
160 
161     if (do_exp) {
162         env->expevt = cs->exception_index;
163         switch (cs->exception_index) {
164         case 0x000:
165         case 0x020:
166         case 0x140:
167             env->sr &= ~(1u << SR_FD);
168             env->sr |= 0xf << 4; /* IMASK */
169             env->pc = 0xa0000000;
170             break;
171         case 0x040:
172         case 0x060:
173             env->pc = env->vbr + 0x400;
174             break;
175         case 0x160:
176             env->spc += 2; /* special case for TRAPA */
177             /* fall through */
178         default:
179             env->pc = env->vbr + 0x100;
180             break;
181         }
182         return;
183     }
184 
185     if (do_irq) {
186         env->intevt = irq_vector;
187         env->pc = env->vbr + 0x600;
188         return;
189     }
190 }
191 
192 static void update_itlb_use(CPUSH4State * env, int itlbnb)
193 {
194     uint8_t or_mask = 0, and_mask = (uint8_t) - 1;
195 
196     switch (itlbnb) {
197     case 0:
198 	and_mask = 0x1f;
199 	break;
200     case 1:
201 	and_mask = 0xe7;
202 	or_mask = 0x80;
203 	break;
204     case 2:
205 	and_mask = 0xfb;
206 	or_mask = 0x50;
207 	break;
208     case 3:
209 	or_mask = 0x2c;
210 	break;
211     }
212 
213     env->mmucr &= (and_mask << 24) | 0x00ffffff;
214     env->mmucr |= (or_mask << 24);
215 }
216 
217 static int itlb_replacement(CPUSH4State * env)
218 {
219     SuperHCPU *cpu = sh_env_get_cpu(env);
220 
221     if ((env->mmucr & 0xe0000000) == 0xe0000000) {
222 	return 0;
223     }
224     if ((env->mmucr & 0x98000000) == 0x18000000) {
225 	return 1;
226     }
227     if ((env->mmucr & 0x54000000) == 0x04000000) {
228 	return 2;
229     }
230     if ((env->mmucr & 0x2c000000) == 0x00000000) {
231 	return 3;
232     }
233     cpu_abort(CPU(cpu), "Unhandled itlb_replacement");
234 }
235 
236 /* Find the corresponding entry in the right TLB
237    Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
238 */
239 static int find_tlb_entry(CPUSH4State * env, target_ulong address,
240 			  tlb_t * entries, uint8_t nbtlb, int use_asid)
241 {
242     int match = MMU_DTLB_MISS;
243     uint32_t start, end;
244     uint8_t asid;
245     int i;
246 
247     asid = env->pteh & 0xff;
248 
249     for (i = 0; i < nbtlb; i++) {
250 	if (!entries[i].v)
251 	    continue;		/* Invalid entry */
252 	if (!entries[i].sh && use_asid && entries[i].asid != asid)
253 	    continue;		/* Bad ASID */
254 	start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
255 	end = start + entries[i].size - 1;
256 	if (address >= start && address <= end) {	/* Match */
257 	    if (match != MMU_DTLB_MISS)
258 		return MMU_DTLB_MULTIPLE;	/* Multiple match */
259 	    match = i;
260 	}
261     }
262     return match;
263 }
264 
265 static void increment_urc(CPUSH4State * env)
266 {
267     uint8_t urb, urc;
268 
269     /* Increment URC */
270     urb = ((env->mmucr) >> 18) & 0x3f;
271     urc = ((env->mmucr) >> 10) & 0x3f;
272     urc++;
273     if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
274 	urc = 0;
275     env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
276 }
277 
278 /* Copy and utlb entry into itlb
279    Return entry
280 */
281 static int copy_utlb_entry_itlb(CPUSH4State *env, int utlb)
282 {
283     int itlb;
284 
285     tlb_t * ientry;
286     itlb = itlb_replacement(env);
287     ientry = &env->itlb[itlb];
288     if (ientry->v) {
289         tlb_flush_page(CPU(sh_env_get_cpu(env)), ientry->vpn << 10);
290     }
291     *ientry = env->utlb[utlb];
292     update_itlb_use(env, itlb);
293     return itlb;
294 }
295 
296 /* Find itlb entry
297    Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
298 */
299 static int find_itlb_entry(CPUSH4State * env, target_ulong address,
300                            int use_asid)
301 {
302     int e;
303 
304     e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
305     if (e == MMU_DTLB_MULTIPLE) {
306 	e = MMU_ITLB_MULTIPLE;
307     } else if (e == MMU_DTLB_MISS) {
308 	e = MMU_ITLB_MISS;
309     } else if (e >= 0) {
310 	update_itlb_use(env, e);
311     }
312     return e;
313 }
314 
315 /* Find utlb entry
316    Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
317 static int find_utlb_entry(CPUSH4State * env, target_ulong address, int use_asid)
318 {
319     /* per utlb access */
320     increment_urc(env);
321 
322     /* Return entry */
323     return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
324 }
325 
326 /* Match address against MMU
327    Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
328    MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
329    MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
330    MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
331    MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
332 */
333 static int get_mmu_address(CPUSH4State * env, target_ulong * physical,
334 			   int *prot, target_ulong address,
335 			   int rw, int access_type)
336 {
337     int use_asid, n;
338     tlb_t *matching = NULL;
339 
340     use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD));
341 
342     if (rw == 2) {
343         n = find_itlb_entry(env, address, use_asid);
344 	if (n >= 0) {
345 	    matching = &env->itlb[n];
346             if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
347 		n = MMU_ITLB_VIOLATION;
348             } else {
349 		*prot = PAGE_EXEC;
350             }
351         } else {
352             n = find_utlb_entry(env, address, use_asid);
353             if (n >= 0) {
354                 n = copy_utlb_entry_itlb(env, n);
355                 matching = &env->itlb[n];
356                 if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
357                     n = MMU_ITLB_VIOLATION;
358                 } else {
359                     *prot = PAGE_READ | PAGE_EXEC;
360                     if ((matching->pr & 1) && matching->d) {
361                         *prot |= PAGE_WRITE;
362                     }
363                 }
364             } else if (n == MMU_DTLB_MULTIPLE) {
365                 n = MMU_ITLB_MULTIPLE;
366             } else if (n == MMU_DTLB_MISS) {
367                 n = MMU_ITLB_MISS;
368             }
369 	}
370     } else {
371 	n = find_utlb_entry(env, address, use_asid);
372 	if (n >= 0) {
373 	    matching = &env->utlb[n];
374             if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) {
375                 n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE :
376                     MMU_DTLB_VIOLATION_READ;
377             } else if ((rw == 1) && !(matching->pr & 1)) {
378                 n = MMU_DTLB_VIOLATION_WRITE;
379             } else if ((rw == 1) && !matching->d) {
380                 n = MMU_DTLB_INITIAL_WRITE;
381             } else {
382                 *prot = PAGE_READ;
383                 if ((matching->pr & 1) && matching->d) {
384                     *prot |= PAGE_WRITE;
385                 }
386             }
387 	} else if (n == MMU_DTLB_MISS) {
388 	    n = (rw == 1) ? MMU_DTLB_MISS_WRITE :
389 		MMU_DTLB_MISS_READ;
390 	}
391     }
392     if (n >= 0) {
393 	n = MMU_OK;
394 	*physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
395 	    (address & (matching->size - 1));
396     }
397     return n;
398 }
399 
400 static int get_physical_address(CPUSH4State * env, target_ulong * physical,
401                                 int *prot, target_ulong address,
402                                 int rw, int access_type)
403 {
404     /* P1, P2 and P4 areas do not use translation */
405     if ((address >= 0x80000000 && address < 0xc0000000) ||
406 	address >= 0xe0000000) {
407         if (!(env->sr & (1u << SR_MD))
408 	    && (address < 0xe0000000 || address >= 0xe4000000)) {
409 	    /* Unauthorized access in user mode (only store queues are available) */
410             qemu_log_mask(LOG_GUEST_ERROR, "Unauthorized access\n");
411 	    if (rw == 0)
412 		return MMU_DADDR_ERROR_READ;
413 	    else if (rw == 1)
414 		return MMU_DADDR_ERROR_WRITE;
415 	    else
416 		return MMU_IADDR_ERROR;
417 	}
418 	if (address >= 0x80000000 && address < 0xc0000000) {
419 	    /* Mask upper 3 bits for P1 and P2 areas */
420 	    *physical = address & 0x1fffffff;
421 	} else {
422 	    *physical = address;
423 	}
424 	*prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
425 	return MMU_OK;
426     }
427 
428     /* If MMU is disabled, return the corresponding physical page */
429     if (!(env->mmucr & MMUCR_AT)) {
430 	*physical = address & 0x1FFFFFFF;
431 	*prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
432 	return MMU_OK;
433     }
434 
435     /* We need to resort to the MMU */
436     return get_mmu_address(env, physical, prot, address, rw, access_type);
437 }
438 
439 hwaddr superh_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
440 {
441     SuperHCPU *cpu = SUPERH_CPU(cs);
442     target_ulong physical;
443     int prot;
444 
445     get_physical_address(&cpu->env, &physical, &prot, addr, 0, 0);
446     return physical;
447 }
448 
449 void cpu_load_tlb(CPUSH4State * env)
450 {
451     SuperHCPU *cpu = sh_env_get_cpu(env);
452     int n = cpu_mmucr_urc(env->mmucr);
453     tlb_t * entry = &env->utlb[n];
454 
455     if (entry->v) {
456         /* Overwriting valid entry in utlb. */
457         target_ulong address = entry->vpn << 10;
458         tlb_flush_page(CPU(cpu), address);
459     }
460 
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(CPU(cpu), "Unhandled load_tlb");
482         break;
483     }
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);
491 }
492 
493  void cpu_sh4_invalidate_tlb(CPUSH4State *s)
494 {
495     int i;
496 
497     /* UTLB */
498     for (i = 0; i < UTLB_SIZE; i++) {
499         tlb_t * entry = &s->utlb[i];
500         entry->v = 0;
501     }
502     /* ITLB */
503     for (i = 0; i < ITLB_SIZE; i++) {
504         tlb_t * entry = &s->itlb[i];
505         entry->v = 0;
506     }
507 
508     tlb_flush(CPU(sh_env_get_cpu(s)));
509 }
510 
511 uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State *s,
512                                        hwaddr addr)
513 {
514     int index = (addr & 0x00000300) >> 8;
515     tlb_t * entry = &s->itlb[index];
516 
517     return (entry->vpn  << 10) |
518            (entry->v    <<  8) |
519            (entry->asid);
520 }
521 
522 void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State *s, hwaddr addr,
523 				    uint32_t mem_value)
524 {
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);
528 
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(CPU(sh_env_get_cpu(s)), address);
535     }
536     entry->asid = asid;
537     entry->vpn = vpn;
538     entry->v = v;
539 }
540 
541 uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State *s,
542                                        hwaddr addr)
543 {
544     int array = (addr & 0x00800000) >> 23;
545     int index = (addr & 0x00000300) >> 8;
546     tlb_t * entry = &s->itlb[index];
547 
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);
561     }
562 }
563 
564 void cpu_sh4_write_mmaped_itlb_data(CPUSH4State *s, hwaddr addr,
565                                     uint32_t mem_value)
566 {
567     int array = (addr & 0x00800000) >> 23;
568     int index = (addr & 0x00000300) >> 8;
569     tlb_t * entry = &s->itlb[index];
570 
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(CPU(sh_env_get_cpu(s)), address);
577         }
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);
589     }
590 }
591 
592 uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State *s,
593                                        hwaddr addr)
594 {
595     int index = (addr & 0x00003f00) >> 8;
596     tlb_t * entry = &s->utlb[index];
597 
598     increment_urc(s); /* per utlb access */
599 
600     return (entry->vpn  << 10) |
601            (entry->v    <<  8) |
602            (entry->asid);
603 }
604 
605 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, hwaddr addr,
606 				    uint32_t mem_value)
607 {
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));
614 
615     if (associate) {
616         int i;
617 	tlb_t * utlb_match_entry = NULL;
618 	int needs_tlb_flush = 0;
619 
620 	/* search UTLB */
621 	for (i = 0; i < UTLB_SIZE; i++) {
622             tlb_t * entry = &s->utlb[i];
623             if (!entry->v)
624 	        continue;
625 
626             if (entry->vpn == vpn
627                 && (!use_asid || entry->asid == asid || entry->sh)) {
628 	        if (utlb_match_entry) {
629                     CPUState *cs = CPU(sh_env_get_cpu(s));
630 
631 		    /* Multiple TLB Exception */
632                     cs->exception_index = 0x140;
633 		    s->tea = addr;
634 		    break;
635 	        }
636 		if (entry->v && !v)
637 		    needs_tlb_flush = 1;
638 		entry->v = v;
639 		entry->d = d;
640 	        utlb_match_entry = entry;
641 	    }
642 	    increment_urc(s); /* per utlb access */
643 	}
644 
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;
657 	    }
658 	}
659 
660         if (needs_tlb_flush) {
661             tlb_flush_page(CPU(sh_env_get_cpu(s)), vpn << 10);
662         }
663     } else {
664         int index = (addr & 0x00003f00) >> 8;
665         tlb_t * entry = &s->utlb[index];
666 	if (entry->v) {
667             CPUState *cs = CPU(sh_env_get_cpu(s));
668 
669 	    /* Overwriting valid entry in utlb. */
670             target_ulong address = entry->vpn << 10;
671             tlb_flush_page(cs, address);
672 	}
673 	entry->asid = asid;
674 	entry->vpn = vpn;
675 	entry->d = d;
676 	entry->v = v;
677 	increment_urc(s);
678     }
679 }
680 
681 uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State *s,
682                                        hwaddr addr)
683 {
684     int array = (addr & 0x00800000) >> 23;
685     int index = (addr & 0x00003f00) >> 8;
686     tlb_t * entry = &s->utlb[index];
687 
688     increment_urc(s); /* per utlb access */
689 
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);
705     }
706 }
707 
708 void cpu_sh4_write_mmaped_utlb_data(CPUSH4State *s, hwaddr addr,
709                                     uint32_t mem_value)
710 {
711     int array = (addr & 0x00800000) >> 23;
712     int index = (addr & 0x00003f00) >> 8;
713     tlb_t * entry = &s->utlb[index];
714 
715     increment_urc(s); /* per utlb access */
716 
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(CPU(sh_env_get_cpu(s)), address);
723         }
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);
737     }
738 }
739 
740 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
741 {
742     int n;
743     int use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD));
744 
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;
754     }
755 
756     /*
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.
761      */
762 #if 0
763     /* check if operand cache is enabled or not. */
764     if (!(env->ccr & 1))
765         return 0;
766 #endif
767 
768     /* if MMU is off, no check for TLB. */
769     if (env->mmucr & MMUCR_AT)
770         return 1;
771 
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;
776 
777     n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
778     if (n >= 0)
779         return env->utlb[n].c;
780 
781     return 0;
782 }
783 
784 #endif
785 
786 bool superh_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
787 {
788     if (interrupt_request & CPU_INTERRUPT_HARD) {
789         SuperHCPU *cpu = SUPERH_CPU(cs);
790         CPUSH4State *env = &cpu->env;
791 
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;
798         }
799     }
800     return false;
801 }
802 
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)
806 {
807     SuperHCPU *cpu = SUPERH_CPU(cs);
808     CPUSH4State *env = &cpu->env;
809     int ret;
810 
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, sh_access_type;
818 
819     sh_access_type = ACCESS_INT;
820     ret = get_physical_address(env, &physical, &prot, address,
821                                access_type, sh_access_type);
822 
823     if (ret == MMU_OK) {
824         address &= TARGET_PAGE_MASK;
825         physical &= TARGET_PAGE_MASK;
826         tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
827         return true;
828     }
829     if (probe) {
830         return false;
831     }
832 
833     if (ret != MMU_DTLB_MULTIPLE && ret != MMU_ITLB_MULTIPLE) {
834         env->pteh = (env->pteh & PTEH_ASID_MASK) | (address & PTEH_VPN_MASK);
835     }
836 #endif
837 
838     env->tea = address;
839     switch (ret) {
840     case MMU_ITLB_MISS:
841     case MMU_DTLB_MISS_READ:
842         cs->exception_index = 0x040;
843         break;
844     case MMU_DTLB_MULTIPLE:
845     case MMU_ITLB_MULTIPLE:
846         cs->exception_index = 0x140;
847         break;
848     case MMU_ITLB_VIOLATION:
849         cs->exception_index = 0x0a0;
850         break;
851     case MMU_DTLB_MISS_WRITE:
852         cs->exception_index = 0x060;
853         break;
854     case MMU_DTLB_INITIAL_WRITE:
855         cs->exception_index = 0x080;
856         break;
857     case MMU_DTLB_VIOLATION_READ:
858         cs->exception_index = 0x0a0;
859         break;
860     case MMU_DTLB_VIOLATION_WRITE:
861         cs->exception_index = 0x0c0;
862         break;
863     case MMU_IADDR_ERROR:
864     case MMU_DADDR_ERROR_READ:
865         cs->exception_index = 0x0e0;
866         break;
867     case MMU_DADDR_ERROR_WRITE:
868         cs->exception_index = 0x100;
869         break;
870     default:
871         cpu_abort(cs, "Unhandled MMU fault");
872     }
873     cpu_loop_exit_restore(cs, retaddr);
874 }
875