]> git.proxmox.com Git - qemu.git/blob - target-sh4/helper.c
target-sh4: MMU: remove dead code
[qemu.git] / target-sh4 / helper.c
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 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 <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <signal.h>
25
26 #include "cpu.h"
27 #include "exec-all.h"
28 #include "hw/sh_intc.h"
29
30 #if defined(CONFIG_USER_ONLY)
31
32 void do_interrupt (CPUState *env)
33 {
34 env->exception_index = -1;
35 }
36
37 int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
38 int mmu_idx, int is_softmmu)
39 {
40 env->tea = address;
41 env->exception_index = 0;
42 switch (rw) {
43 case 0:
44 env->exception_index = 0x0a0;
45 break;
46 case 1:
47 env->exception_index = 0x0c0;
48 break;
49 case 2:
50 env->exception_index = 0x0a0;
51 break;
52 }
53 return 1;
54 }
55
56 target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
57 {
58 return addr;
59 }
60
61 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
62 {
63 /* For user mode, only U0 area is cachable. */
64 return !(addr & 0x80000000);
65 }
66
67 #else /* !CONFIG_USER_ONLY */
68
69 #define MMU_OK 0
70 #define MMU_ITLB_MISS (-1)
71 #define MMU_ITLB_MULTIPLE (-2)
72 #define MMU_ITLB_VIOLATION (-3)
73 #define MMU_DTLB_MISS_READ (-4)
74 #define MMU_DTLB_MISS_WRITE (-5)
75 #define MMU_DTLB_INITIAL_WRITE (-6)
76 #define MMU_DTLB_VIOLATION_READ (-7)
77 #define MMU_DTLB_VIOLATION_WRITE (-8)
78 #define MMU_DTLB_MULTIPLE (-9)
79 #define MMU_DTLB_MISS (-10)
80 #define MMU_IADDR_ERROR (-11)
81 #define MMU_DADDR_ERROR_READ (-12)
82 #define MMU_DADDR_ERROR_WRITE (-13)
83
84 void do_interrupt(CPUState * env)
85 {
86 int do_irq = env->interrupt_request & CPU_INTERRUPT_HARD;
87 int do_exp, irq_vector = env->exception_index;
88
89 /* prioritize exceptions over interrupts */
90
91 do_exp = env->exception_index != -1;
92 do_irq = do_irq && (env->exception_index == -1);
93
94 if (env->sr & SR_BL) {
95 if (do_exp && env->exception_index != 0x1e0) {
96 env->exception_index = 0x000; /* masked exception -> reset */
97 }
98 if (do_irq && !env->intr_at_halt) {
99 return; /* masked */
100 }
101 env->intr_at_halt = 0;
102 }
103
104 if (do_irq) {
105 irq_vector = sh_intc_get_pending_vector(env->intc_handle,
106 (env->sr >> 4) & 0xf);
107 if (irq_vector == -1) {
108 return; /* masked */
109 }
110 }
111
112 if (qemu_loglevel_mask(CPU_LOG_INT)) {
113 const char *expname;
114 switch (env->exception_index) {
115 case 0x0e0:
116 expname = "addr_error";
117 break;
118 case 0x040:
119 expname = "tlb_miss";
120 break;
121 case 0x0a0:
122 expname = "tlb_violation";
123 break;
124 case 0x180:
125 expname = "illegal_instruction";
126 break;
127 case 0x1a0:
128 expname = "slot_illegal_instruction";
129 break;
130 case 0x800:
131 expname = "fpu_disable";
132 break;
133 case 0x820:
134 expname = "slot_fpu";
135 break;
136 case 0x100:
137 expname = "data_write";
138 break;
139 case 0x060:
140 expname = "dtlb_miss_write";
141 break;
142 case 0x0c0:
143 expname = "dtlb_violation_write";
144 break;
145 case 0x120:
146 expname = "fpu_exception";
147 break;
148 case 0x080:
149 expname = "initial_page_write";
150 break;
151 case 0x160:
152 expname = "trapa";
153 break;
154 default:
155 expname = do_irq ? "interrupt" : "???";
156 break;
157 }
158 qemu_log("exception 0x%03x [%s] raised\n",
159 irq_vector, expname);
160 log_cpu_state(env, 0);
161 }
162
163 env->ssr = env->sr;
164 env->spc = env->pc;
165 env->sgr = env->gregs[15];
166 env->sr |= SR_BL | SR_MD | SR_RB;
167
168 if (env->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) {
169 /* Branch instruction should be executed again before delay slot. */
170 env->spc -= 2;
171 /* Clear flags for exception/interrupt routine. */
172 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL | DELAY_SLOT_TRUE);
173 }
174 if (env->flags & DELAY_SLOT_CLEARME)
175 env->flags = 0;
176
177 if (do_exp) {
178 env->expevt = env->exception_index;
179 switch (env->exception_index) {
180 case 0x000:
181 case 0x020:
182 case 0x140:
183 env->sr &= ~SR_FD;
184 env->sr |= 0xf << 4; /* IMASK */
185 env->pc = 0xa0000000;
186 break;
187 case 0x040:
188 case 0x060:
189 env->pc = env->vbr + 0x400;
190 break;
191 case 0x160:
192 env->spc += 2; /* special case for TRAPA */
193 /* fall through */
194 default:
195 env->pc = env->vbr + 0x100;
196 break;
197 }
198 return;
199 }
200
201 if (do_irq) {
202 env->intevt = irq_vector;
203 env->pc = env->vbr + 0x600;
204 return;
205 }
206 }
207
208 static void update_itlb_use(CPUState * env, int itlbnb)
209 {
210 uint8_t or_mask = 0, and_mask = (uint8_t) - 1;
211
212 switch (itlbnb) {
213 case 0:
214 and_mask = 0x1f;
215 break;
216 case 1:
217 and_mask = 0xe7;
218 or_mask = 0x80;
219 break;
220 case 2:
221 and_mask = 0xfb;
222 or_mask = 0x50;
223 break;
224 case 3:
225 or_mask = 0x2c;
226 break;
227 }
228
229 env->mmucr &= (and_mask << 24) | 0x00ffffff;
230 env->mmucr |= (or_mask << 24);
231 }
232
233 static int itlb_replacement(CPUState * env)
234 {
235 if ((env->mmucr & 0xe0000000) == 0xe0000000)
236 return 0;
237 if ((env->mmucr & 0x98000000) == 0x18000000)
238 return 1;
239 if ((env->mmucr & 0x54000000) == 0x04000000)
240 return 2;
241 if ((env->mmucr & 0x2c000000) == 0x00000000)
242 return 3;
243 assert(0);
244 }
245
246 /* Find the corresponding entry in the right TLB
247 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
248 */
249 static int find_tlb_entry(CPUState * env, target_ulong address,
250 tlb_t * entries, uint8_t nbtlb, int use_asid)
251 {
252 int match = MMU_DTLB_MISS;
253 uint32_t start, end;
254 uint8_t asid;
255 int i;
256
257 asid = env->pteh & 0xff;
258
259 for (i = 0; i < nbtlb; i++) {
260 if (!entries[i].v)
261 continue; /* Invalid entry */
262 if (!entries[i].sh && use_asid && entries[i].asid != asid)
263 continue; /* Bad ASID */
264 start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
265 end = start + entries[i].size - 1;
266 if (address >= start && address <= end) { /* Match */
267 if (match != MMU_DTLB_MISS)
268 return MMU_DTLB_MULTIPLE; /* Multiple match */
269 match = i;
270 }
271 }
272 return match;
273 }
274
275 static int same_tlb_entry_exists(const tlb_t * haystack, uint8_t nbtlb,
276 const tlb_t * needle)
277 {
278 int i;
279 for (i = 0; i < nbtlb; i++)
280 if (!memcmp(&haystack[i], needle, sizeof(tlb_t)))
281 return 1;
282 return 0;
283 }
284
285 static void increment_urc(CPUState * env)
286 {
287 uint8_t urb, urc;
288
289 /* Increment URC */
290 urb = ((env->mmucr) >> 18) & 0x3f;
291 urc = ((env->mmucr) >> 10) & 0x3f;
292 urc++;
293 if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
294 urc = 0;
295 env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
296 }
297
298 /* Find itlb entry - update itlb from utlb if necessary and asked for
299 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
300 Update the itlb from utlb if update is not 0
301 */
302 static int find_itlb_entry(CPUState * env, target_ulong address,
303 int use_asid, int update)
304 {
305 int e, n;
306
307 e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
308 if (e == MMU_DTLB_MULTIPLE)
309 e = MMU_ITLB_MULTIPLE;
310 else if (e == MMU_DTLB_MISS && update) {
311 e = find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
312 if (e >= 0) {
313 tlb_t * ientry;
314 n = itlb_replacement(env);
315 ientry = &env->itlb[n];
316 if (ientry->v) {
317 if (!same_tlb_entry_exists(env->utlb, UTLB_SIZE, ientry))
318 tlb_flush_page(env, ientry->vpn << 10);
319 }
320 *ientry = env->utlb[e];
321 e = n;
322 } else if (e == MMU_DTLB_MISS)
323 e = MMU_ITLB_MISS;
324 } else if (e == MMU_DTLB_MISS)
325 e = MMU_ITLB_MISS;
326 if (e >= 0)
327 update_itlb_use(env, e);
328 return e;
329 }
330
331 /* Find utlb entry
332 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
333 static int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
334 {
335 /* per utlb access */
336 increment_urc(env);
337
338 /* Return entry */
339 return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
340 }
341
342 /* Match address against MMU
343 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
344 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
345 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
346 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
347 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
348 */
349 static int get_mmu_address(CPUState * env, target_ulong * physical,
350 int *prot, target_ulong address,
351 int rw, int access_type)
352 {
353 int use_asid, n;
354 tlb_t *matching = NULL;
355
356 use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
357
358 if (rw == 2) {
359 n = find_itlb_entry(env, address, use_asid, 1);
360 if (n >= 0) {
361 matching = &env->itlb[n];
362 if (!(env->sr & SR_MD) && !(matching->pr & 2))
363 n = MMU_ITLB_VIOLATION;
364 else
365 *prot = PAGE_READ;
366 }
367 } else {
368 n = find_utlb_entry(env, address, use_asid);
369 if (n >= 0) {
370 matching = &env->utlb[n];
371 if (!(env->sr & SR_MD) && !(matching->pr & 2)) {
372 n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE :
373 MMU_DTLB_VIOLATION_READ;
374 } else if ((rw == 1) && !(matching->pr & 1)) {
375 n = MMU_DTLB_VIOLATION_WRITE;
376 } else if ((rw == 1) & !matching->d) {
377 n = MMU_DTLB_INITIAL_WRITE;
378 } else {
379 *prot = PAGE_READ;
380 if ((matching->pr & 1) && matching->d) {
381 *prot |= PAGE_WRITE;
382 }
383 }
384 } else if (n == MMU_DTLB_MISS) {
385 n = (rw == 1) ? MMU_DTLB_MISS_WRITE :
386 MMU_DTLB_MISS_READ;
387 }
388 }
389 if (n >= 0) {
390 n = MMU_OK;
391 *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
392 (address & (matching->size - 1));
393 }
394 return n;
395 }
396
397 static int get_physical_address(CPUState * env, target_ulong * physical,
398 int *prot, target_ulong address,
399 int rw, int access_type)
400 {
401 /* P1, P2 and P4 areas do not use translation */
402 if ((address >= 0x80000000 && address < 0xc0000000) ||
403 address >= 0xe0000000) {
404 if (!(env->sr & SR_MD)
405 && (address < 0xe0000000 || address > 0xe4000000)) {
406 /* Unauthorized access in user mode (only store queues are available) */
407 fprintf(stderr, "Unauthorized access\n");
408 if (rw == 0)
409 return MMU_DADDR_ERROR_READ;
410 else if (rw == 1)
411 return MMU_DADDR_ERROR_WRITE;
412 else
413 return MMU_IADDR_ERROR;
414 }
415 if (address >= 0x80000000 && address < 0xc0000000) {
416 /* Mask upper 3 bits for P1 and P2 areas */
417 *physical = address & 0x1fffffff;
418 } else {
419 *physical = address;
420 }
421 *prot = PAGE_READ | PAGE_WRITE;
422 return MMU_OK;
423 }
424
425 /* If MMU is disabled, return the corresponding physical page */
426 if (!env->mmucr & MMUCR_AT) {
427 *physical = address & 0x1FFFFFFF;
428 *prot = PAGE_READ | PAGE_WRITE;
429 return MMU_OK;
430 }
431
432 /* We need to resort to the MMU */
433 return get_mmu_address(env, physical, prot, address, rw, access_type);
434 }
435
436 int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
437 int mmu_idx, int is_softmmu)
438 {
439 target_ulong physical;
440 int prot, ret, access_type;
441
442 access_type = ACCESS_INT;
443 ret =
444 get_physical_address(env, &physical, &prot, address, rw,
445 access_type);
446
447 if (ret != MMU_OK) {
448 env->tea = address;
449 switch (ret) {
450 case MMU_ITLB_MISS:
451 case MMU_DTLB_MISS_READ:
452 env->exception_index = 0x040;
453 break;
454 case MMU_DTLB_MULTIPLE:
455 case MMU_ITLB_MULTIPLE:
456 env->exception_index = 0x140;
457 break;
458 case MMU_ITLB_VIOLATION:
459 env->exception_index = 0x0a0;
460 break;
461 case MMU_DTLB_MISS_WRITE:
462 env->exception_index = 0x060;
463 break;
464 case MMU_DTLB_INITIAL_WRITE:
465 env->exception_index = 0x080;
466 break;
467 case MMU_DTLB_VIOLATION_READ:
468 env->exception_index = 0x0a0;
469 break;
470 case MMU_DTLB_VIOLATION_WRITE:
471 env->exception_index = 0x0c0;
472 break;
473 case MMU_IADDR_ERROR:
474 case MMU_DADDR_ERROR_READ:
475 env->exception_index = 0x0c0;
476 break;
477 case MMU_DADDR_ERROR_WRITE:
478 env->exception_index = 0x100;
479 break;
480 default:
481 assert(0);
482 }
483 return 1;
484 }
485
486 address &= TARGET_PAGE_MASK;
487 physical &= TARGET_PAGE_MASK;
488
489 return tlb_set_page(env, address, physical, prot, mmu_idx, is_softmmu);
490 }
491
492 target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
493 {
494 target_ulong physical;
495 int prot;
496
497 get_physical_address(env, &physical, &prot, addr, 0, 0);
498 return physical;
499 }
500
501 void cpu_load_tlb(CPUSH4State * env)
502 {
503 int n = cpu_mmucr_urc(env->mmucr);
504 tlb_t * entry = &env->utlb[n];
505
506 if (entry->v) {
507 /* Overwriting valid entry in utlb. */
508 target_ulong address = entry->vpn << 10;
509 if (!same_tlb_entry_exists(env->itlb, ITLB_SIZE, entry)) {
510 tlb_flush_page(env, address);
511 }
512 }
513
514 /* Take values into cpu status from registers. */
515 entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
516 entry->vpn = cpu_pteh_vpn(env->pteh);
517 entry->v = (uint8_t)cpu_ptel_v(env->ptel);
518 entry->ppn = cpu_ptel_ppn(env->ptel);
519 entry->sz = (uint8_t)cpu_ptel_sz(env->ptel);
520 switch (entry->sz) {
521 case 0: /* 00 */
522 entry->size = 1024; /* 1K */
523 break;
524 case 1: /* 01 */
525 entry->size = 1024 * 4; /* 4K */
526 break;
527 case 2: /* 10 */
528 entry->size = 1024 * 64; /* 64K */
529 break;
530 case 3: /* 11 */
531 entry->size = 1024 * 1024; /* 1M */
532 break;
533 default:
534 assert(0);
535 break;
536 }
537 entry->sh = (uint8_t)cpu_ptel_sh(env->ptel);
538 entry->c = (uint8_t)cpu_ptel_c(env->ptel);
539 entry->pr = (uint8_t)cpu_ptel_pr(env->ptel);
540 entry->d = (uint8_t)cpu_ptel_d(env->ptel);
541 entry->wt = (uint8_t)cpu_ptel_wt(env->ptel);
542 entry->sa = (uint8_t)cpu_ptea_sa(env->ptea);
543 entry->tc = (uint8_t)cpu_ptea_tc(env->ptea);
544 }
545
546 void cpu_sh4_invalidate_tlb(CPUSH4State *s)
547 {
548 int i;
549
550 /* UTLB */
551 for (i = 0; i < UTLB_SIZE; i++) {
552 tlb_t * entry = &s->utlb[i];
553 entry->v = 0;
554 }
555 /* ITLB */
556 for (i = 0; i < UTLB_SIZE; i++) {
557 tlb_t * entry = &s->utlb[i];
558 entry->v = 0;
559 }
560
561 tlb_flush(s, 1);
562 }
563
564 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, target_phys_addr_t addr,
565 uint32_t mem_value)
566 {
567 int associate = addr & 0x0000080;
568 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
569 uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9);
570 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
571 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
572 int use_asid = (s->mmucr & MMUCR_SV) == 0 || (s->sr & SR_MD) == 0;
573
574 if (associate) {
575 int i;
576 tlb_t * utlb_match_entry = NULL;
577 int needs_tlb_flush = 0;
578
579 /* search UTLB */
580 for (i = 0; i < UTLB_SIZE; i++) {
581 tlb_t * entry = &s->utlb[i];
582 if (!entry->v)
583 continue;
584
585 if (entry->vpn == vpn
586 && (!use_asid || entry->asid == asid || entry->sh)) {
587 if (utlb_match_entry) {
588 /* Multiple TLB Exception */
589 s->exception_index = 0x140;
590 s->tea = addr;
591 break;
592 }
593 if (entry->v && !v)
594 needs_tlb_flush = 1;
595 entry->v = v;
596 entry->d = d;
597 utlb_match_entry = entry;
598 }
599 increment_urc(s); /* per utlb access */
600 }
601
602 /* search ITLB */
603 for (i = 0; i < ITLB_SIZE; i++) {
604 tlb_t * entry = &s->itlb[i];
605 if (entry->vpn == vpn
606 && (!use_asid || entry->asid == asid || entry->sh)) {
607 if (entry->v && !v)
608 needs_tlb_flush = 1;
609 if (utlb_match_entry)
610 *entry = *utlb_match_entry;
611 else
612 entry->v = v;
613 break;
614 }
615 }
616
617 if (needs_tlb_flush)
618 tlb_flush_page(s, vpn << 10);
619
620 } else {
621 int index = (addr & 0x00003f00) >> 8;
622 tlb_t * entry = &s->utlb[index];
623 if (entry->v) {
624 /* Overwriting valid entry in utlb. */
625 target_ulong address = entry->vpn << 10;
626 if (!same_tlb_entry_exists(s->itlb, ITLB_SIZE, entry)) {
627 tlb_flush_page(s, address);
628 }
629 }
630 entry->asid = asid;
631 entry->vpn = vpn;
632 entry->d = d;
633 entry->v = v;
634 increment_urc(s);
635 }
636 }
637
638 int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
639 {
640 int n;
641 int use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
642
643 /* check area */
644 if (env->sr & SR_MD) {
645 /* For previledged mode, P2 and P4 area is not cachable. */
646 if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr)
647 return 0;
648 } else {
649 /* For user mode, only U0 area is cachable. */
650 if (0x80000000 <= addr)
651 return 0;
652 }
653
654 /*
655 * TODO : Evaluate CCR and check if the cache is on or off.
656 * Now CCR is not in CPUSH4State, but in SH7750State.
657 * When you move the ccr inot CPUSH4State, the code will be
658 * as follows.
659 */
660 #if 0
661 /* check if operand cache is enabled or not. */
662 if (!(env->ccr & 1))
663 return 0;
664 #endif
665
666 /* if MMU is off, no check for TLB. */
667 if (env->mmucr & MMUCR_AT)
668 return 1;
669
670 /* check TLB */
671 n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid);
672 if (n >= 0)
673 return env->itlb[n].c;
674
675 n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
676 if (n >= 0)
677 return env->utlb[n].c;
678
679 return 0;
680 }
681
682 #endif