blob: d8e08e305cf2af93a7bf0adf32125554460537e9 [file] [log] [blame]
bellardfdf9b3e2006-04-27 21:07:38 +00001/*
2 * SH4 emulation
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfdf9b3e2006-04-27 21:07:38 +00004 * 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, write to the Free Software
aurel32fad6cb12009-01-04 22:05:52 +000018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
bellardfdf9b3e2006-04-27 21:07:38 +000019 */
20#include <stdarg.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <inttypes.h>
25#include <signal.h>
26#include <assert.h>
27
28#include "cpu.h"
29#include "exec-all.h"
thse96e2042007-12-02 06:18:24 +000030#include "hw/sh_intc.h"
bellardfdf9b3e2006-04-27 21:07:38 +000031
pbrook355fb232006-06-17 19:58:25 +000032#if defined(CONFIG_USER_ONLY)
33
34void do_interrupt (CPUState *env)
35{
36 env->exception_index = -1;
37}
38
39int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
j_mayer6ebbf392007-10-14 07:07:08 +000040 int mmu_idx, int is_softmmu)
pbrook355fb232006-06-17 19:58:25 +000041{
42 env->tea = address;
thsc3b5bc82007-12-02 06:31:25 +000043 env->exception_index = 0;
pbrook355fb232006-06-17 19:58:25 +000044 switch (rw) {
45 case 0:
46 env->exception_index = 0x0a0;
47 break;
48 case 1:
49 env->exception_index = 0x0c0;
50 break;
aurel32cf7055b2008-11-21 22:33:15 +000051 case 2:
52 env->exception_index = 0x0a0;
53 break;
pbrook355fb232006-06-17 19:58:25 +000054 }
55 return 1;
56}
57
j_mayer9b3c35e2007-04-07 11:21:28 +000058target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
pbrook355fb232006-06-17 19:58:25 +000059{
60 return addr;
61}
62
edgar_igl3c1adf12009-04-03 07:29:38 +000063int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
64{
65 /* For user mode, only U0 area is cachable. */
edgar_igl679dee32009-04-03 07:33:24 +000066 return !(addr & 0x80000000);
edgar_igl3c1adf12009-04-03 07:29:38 +000067}
68
pbrook355fb232006-06-17 19:58:25 +000069#else /* !CONFIG_USER_ONLY */
70
bellardfdf9b3e2006-04-27 21:07:38 +000071#define MMU_OK 0
72#define MMU_ITLB_MISS (-1)
73#define MMU_ITLB_MULTIPLE (-2)
74#define MMU_ITLB_VIOLATION (-3)
75#define MMU_DTLB_MISS_READ (-4)
76#define MMU_DTLB_MISS_WRITE (-5)
77#define MMU_DTLB_INITIAL_WRITE (-6)
78#define MMU_DTLB_VIOLATION_READ (-7)
79#define MMU_DTLB_VIOLATION_WRITE (-8)
80#define MMU_DTLB_MULTIPLE (-9)
81#define MMU_DTLB_MISS (-10)
aurel32cf7055b2008-11-21 22:33:15 +000082#define MMU_IADDR_ERROR (-11)
83#define MMU_DADDR_ERROR_READ (-12)
84#define MMU_DADDR_ERROR_WRITE (-13)
bellardfdf9b3e2006-04-27 21:07:38 +000085
86void do_interrupt(CPUState * env)
87{
thse96e2042007-12-02 06:18:24 +000088 int do_irq = env->interrupt_request & CPU_INTERRUPT_HARD;
89 int do_exp, irq_vector = env->exception_index;
90
91 /* prioritize exceptions over interrupts */
92
93 do_exp = env->exception_index != -1;
94 do_irq = do_irq && (env->exception_index == -1);
95
96 if (env->sr & SR_BL) {
97 if (do_exp && env->exception_index != 0x1e0) {
98 env->exception_index = 0x000; /* masked exception -> reset */
99 }
aurel32833ed382008-08-22 08:57:27 +0000100 if (do_irq && !env->intr_at_halt) {
thse96e2042007-12-02 06:18:24 +0000101 return; /* masked */
102 }
aurel32833ed382008-08-22 08:57:27 +0000103 env->intr_at_halt = 0;
thse96e2042007-12-02 06:18:24 +0000104 }
105
106 if (do_irq) {
107 irq_vector = sh_intc_get_pending_vector(env->intc_handle,
108 (env->sr >> 4) & 0xf);
109 if (irq_vector == -1) {
110 return; /* masked */
111 }
112 }
113
aliguori8fec2b82009-01-15 22:36:53 +0000114 if (qemu_loglevel_mask(CPU_LOG_INT)) {
bellardfdf9b3e2006-04-27 21:07:38 +0000115 const char *expname;
116 switch (env->exception_index) {
117 case 0x0e0:
118 expname = "addr_error";
119 break;
120 case 0x040:
121 expname = "tlb_miss";
122 break;
123 case 0x0a0:
124 expname = "tlb_violation";
125 break;
126 case 0x180:
127 expname = "illegal_instruction";
128 break;
129 case 0x1a0:
130 expname = "slot_illegal_instruction";
131 break;
132 case 0x800:
133 expname = "fpu_disable";
134 break;
135 case 0x820:
136 expname = "slot_fpu";
137 break;
138 case 0x100:
139 expname = "data_write";
140 break;
141 case 0x060:
142 expname = "dtlb_miss_write";
143 break;
144 case 0x0c0:
145 expname = "dtlb_violation_write";
146 break;
147 case 0x120:
148 expname = "fpu_exception";
149 break;
150 case 0x080:
151 expname = "initial_page_write";
152 break;
153 case 0x160:
154 expname = "trapa";
155 break;
156 default:
thse96e2042007-12-02 06:18:24 +0000157 expname = do_irq ? "interrupt" : "???";
158 break;
bellardfdf9b3e2006-04-27 21:07:38 +0000159 }
aliguori93fcfe32009-01-15 22:34:14 +0000160 qemu_log("exception 0x%03x [%s] raised\n",
161 irq_vector, expname);
162 log_cpu_state(env, 0);
bellardfdf9b3e2006-04-27 21:07:38 +0000163 }
164
165 env->ssr = env->sr;
thse96e2042007-12-02 06:18:24 +0000166 env->spc = env->pc;
bellardfdf9b3e2006-04-27 21:07:38 +0000167 env->sgr = env->gregs[15];
168 env->sr |= SR_BL | SR_MD | SR_RB;
169
aurel32274a9e72008-08-22 08:57:35 +0000170 if (env->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) {
171 /* Branch instruction should be executed again before delay slot. */
172 env->spc -= 2;
173 /* Clear flags for exception/interrupt routine. */
174 env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL | DELAY_SLOT_TRUE);
175 }
176 if (env->flags & DELAY_SLOT_CLEARME)
177 env->flags = 0;
178
thse96e2042007-12-02 06:18:24 +0000179 if (do_exp) {
180 env->expevt = env->exception_index;
181 switch (env->exception_index) {
182 case 0x000:
183 case 0x020:
184 case 0x140:
185 env->sr &= ~SR_FD;
186 env->sr |= 0xf << 4; /* IMASK */
187 env->pc = 0xa0000000;
188 break;
189 case 0x040:
190 case 0x060:
191 env->pc = env->vbr + 0x400;
192 break;
193 case 0x160:
194 env->spc += 2; /* special case for TRAPA */
195 /* fall through */
196 default:
197 env->pc = env->vbr + 0x100;
198 break;
199 }
200 return;
201 }
202
203 if (do_irq) {
204 env->intevt = irq_vector;
205 env->pc = env->vbr + 0x600;
206 return;
bellardfdf9b3e2006-04-27 21:07:38 +0000207 }
208}
209
210static void update_itlb_use(CPUState * env, int itlbnb)
211{
212 uint8_t or_mask = 0, and_mask = (uint8_t) - 1;
213
214 switch (itlbnb) {
215 case 0:
aurel32ea2b5422008-05-09 18:45:55 +0000216 and_mask = 0x1f;
bellardfdf9b3e2006-04-27 21:07:38 +0000217 break;
218 case 1:
219 and_mask = 0xe7;
220 or_mask = 0x80;
221 break;
222 case 2:
223 and_mask = 0xfb;
224 or_mask = 0x50;
225 break;
226 case 3:
227 or_mask = 0x2c;
228 break;
229 }
230
aurel32ea2b5422008-05-09 18:45:55 +0000231 env->mmucr &= (and_mask << 24) | 0x00ffffff;
bellardfdf9b3e2006-04-27 21:07:38 +0000232 env->mmucr |= (or_mask << 24);
233}
234
235static int itlb_replacement(CPUState * env)
236{
237 if ((env->mmucr & 0xe0000000) == 0xe0000000)
238 return 0;
aurel32ea2b5422008-05-09 18:45:55 +0000239 if ((env->mmucr & 0x98000000) == 0x18000000)
bellardfdf9b3e2006-04-27 21:07:38 +0000240 return 1;
241 if ((env->mmucr & 0x54000000) == 0x04000000)
242 return 2;
243 if ((env->mmucr & 0x2c000000) == 0x00000000)
244 return 3;
245 assert(0);
246}
247
248/* Find the corresponding entry in the right TLB
249 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
250*/
251static int find_tlb_entry(CPUState * env, target_ulong address,
252 tlb_t * entries, uint8_t nbtlb, int use_asid)
253{
254 int match = MMU_DTLB_MISS;
255 uint32_t start, end;
256 uint8_t asid;
257 int i;
258
259 asid = env->pteh & 0xff;
260
261 for (i = 0; i < nbtlb; i++) {
262 if (!entries[i].v)
263 continue; /* Invalid entry */
aurel32eeda6772008-12-10 17:31:51 +0000264 if (!entries[i].sh && use_asid && entries[i].asid != asid)
bellardfdf9b3e2006-04-27 21:07:38 +0000265 continue; /* Bad ASID */
266#if 0
267 switch (entries[i].sz) {
268 case 0:
269 size = 1024; /* 1kB */
270 break;
271 case 1:
272 size = 4 * 1024; /* 4kB */
273 break;
274 case 2:
275 size = 64 * 1024; /* 64kB */
276 break;
277 case 3:
278 size = 1024 * 1024; /* 1MB */
279 break;
280 default:
281 assert(0);
282 }
283#endif
284 start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
285 end = start + entries[i].size - 1;
286 if (address >= start && address <= end) { /* Match */
aurel32ea2b5422008-05-09 18:45:55 +0000287 if (match != MMU_DTLB_MISS)
bellardfdf9b3e2006-04-27 21:07:38 +0000288 return MMU_DTLB_MULTIPLE; /* Multiple match */
289 match = i;
290 }
291 }
292 return match;
293}
294
aurel3229e179b2008-08-22 08:57:43 +0000295static int same_tlb_entry_exists(const tlb_t * haystack, uint8_t nbtlb,
296 const tlb_t * needle)
297{
298 int i;
299 for (i = 0; i < nbtlb; i++)
300 if (!memcmp(&haystack[i], needle, sizeof(tlb_t)))
301 return 1;
302 return 0;
303}
304
305static void increment_urc(CPUState * env)
306{
307 uint8_t urb, urc;
308
309 /* Increment URC */
310 urb = ((env->mmucr) >> 18) & 0x3f;
311 urc = ((env->mmucr) >> 10) & 0x3f;
312 urc++;
aurel32927e3a42009-03-03 09:14:01 +0000313 if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
aurel3229e179b2008-08-22 08:57:43 +0000314 urc = 0;
315 env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
316}
317
bellardfdf9b3e2006-04-27 21:07:38 +0000318/* Find itlb entry - update itlb from utlb if necessary and asked for
319 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
320 Update the itlb from utlb if update is not 0
321*/
aurel32ef7ec1c2009-03-03 06:12:03 +0000322static int find_itlb_entry(CPUState * env, target_ulong address,
323 int use_asid, int update)
bellardfdf9b3e2006-04-27 21:07:38 +0000324{
325 int e, n;
326
327 e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
328 if (e == MMU_DTLB_MULTIPLE)
329 e = MMU_ITLB_MULTIPLE;
330 else if (e == MMU_DTLB_MISS && update) {
331 e = find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
332 if (e >= 0) {
aurel3206afe2c2008-08-22 08:57:52 +0000333 tlb_t * ientry;
bellardfdf9b3e2006-04-27 21:07:38 +0000334 n = itlb_replacement(env);
aurel3206afe2c2008-08-22 08:57:52 +0000335 ientry = &env->itlb[n];
336 if (ientry->v) {
337 if (!same_tlb_entry_exists(env->utlb, UTLB_SIZE, ientry))
338 tlb_flush_page(env, ientry->vpn << 10);
339 }
340 *ientry = env->utlb[e];
bellardfdf9b3e2006-04-27 21:07:38 +0000341 e = n;
aurel32ea2b5422008-05-09 18:45:55 +0000342 } else if (e == MMU_DTLB_MISS)
343 e = MMU_ITLB_MISS;
344 } else if (e == MMU_DTLB_MISS)
345 e = MMU_ITLB_MISS;
bellardfdf9b3e2006-04-27 21:07:38 +0000346 if (e >= 0)
347 update_itlb_use(env, e);
348 return e;
349}
350
351/* Find utlb entry
352 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
aurel32ef7ec1c2009-03-03 06:12:03 +0000353static int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
bellardfdf9b3e2006-04-27 21:07:38 +0000354{
aurel3229e179b2008-08-22 08:57:43 +0000355 /* per utlb access */
356 increment_urc(env);
bellardfdf9b3e2006-04-27 21:07:38 +0000357
358 /* Return entry */
359 return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
360}
361
362/* Match address against MMU
363 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
364 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
365 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
aurel32cf7055b2008-11-21 22:33:15 +0000366 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
367 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
bellardfdf9b3e2006-04-27 21:07:38 +0000368*/
369static int get_mmu_address(CPUState * env, target_ulong * physical,
370 int *prot, target_ulong address,
371 int rw, int access_type)
372{
aurel32cf7055b2008-11-21 22:33:15 +0000373 int use_asid, n;
bellardfdf9b3e2006-04-27 21:07:38 +0000374 tlb_t *matching = NULL;
375
aurel3206afe2c2008-08-22 08:57:52 +0000376 use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
bellardfdf9b3e2006-04-27 21:07:38 +0000377
aurel32cf7055b2008-11-21 22:33:15 +0000378 if (rw == 2) {
bellardfdf9b3e2006-04-27 21:07:38 +0000379 n = find_itlb_entry(env, address, use_asid, 1);
380 if (n >= 0) {
381 matching = &env->itlb[n];
382 if ((env->sr & SR_MD) & !(matching->pr & 2))
383 n = MMU_ITLB_VIOLATION;
384 else
385 *prot = PAGE_READ;
386 }
387 } else {
388 n = find_utlb_entry(env, address, use_asid);
389 if (n >= 0) {
390 matching = &env->utlb[n];
391 switch ((matching->pr << 1) | ((env->sr & SR_MD) ? 1 : 0)) {
392 case 0: /* 000 */
393 case 2: /* 010 */
aurel32cf7055b2008-11-21 22:33:15 +0000394 n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE :
bellardfdf9b3e2006-04-27 21:07:38 +0000395 MMU_DTLB_VIOLATION_READ;
396 break;
397 case 1: /* 001 */
398 case 4: /* 100 */
399 case 5: /* 101 */
aurel32cf7055b2008-11-21 22:33:15 +0000400 if (rw == 1)
bellardfdf9b3e2006-04-27 21:07:38 +0000401 n = MMU_DTLB_VIOLATION_WRITE;
402 else
403 *prot = PAGE_READ;
404 break;
405 case 3: /* 011 */
406 case 6: /* 110 */
407 case 7: /* 111 */
aurel32cf7055b2008-11-21 22:33:15 +0000408 *prot = (rw == 1)? PAGE_WRITE : PAGE_READ;
bellardfdf9b3e2006-04-27 21:07:38 +0000409 break;
410 }
411 } else if (n == MMU_DTLB_MISS) {
aurel32cf7055b2008-11-21 22:33:15 +0000412 n = (rw == 1) ? MMU_DTLB_MISS_WRITE :
bellardfdf9b3e2006-04-27 21:07:38 +0000413 MMU_DTLB_MISS_READ;
414 }
415 }
416 if (n >= 0) {
417 *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
418 (address & (matching->size - 1));
aurel32cf7055b2008-11-21 22:33:15 +0000419 if ((rw == 1) & !matching->d)
bellardfdf9b3e2006-04-27 21:07:38 +0000420 n = MMU_DTLB_INITIAL_WRITE;
421 else
422 n = MMU_OK;
423 }
424 return n;
425}
426
aurel32ef7ec1c2009-03-03 06:12:03 +0000427static int get_physical_address(CPUState * env, target_ulong * physical,
428 int *prot, target_ulong address,
429 int rw, int access_type)
bellardfdf9b3e2006-04-27 21:07:38 +0000430{
431 /* P1, P2 and P4 areas do not use translation */
432 if ((address >= 0x80000000 && address < 0xc0000000) ||
433 address >= 0xe0000000) {
434 if (!(env->sr & SR_MD)
435 && (address < 0xe0000000 || address > 0xe4000000)) {
436 /* Unauthorized access in user mode (only store queues are available) */
437 fprintf(stderr, "Unauthorized access\n");
aurel32cf7055b2008-11-21 22:33:15 +0000438 if (rw == 0)
439 return MMU_DADDR_ERROR_READ;
440 else if (rw == 1)
441 return MMU_DADDR_ERROR_WRITE;
442 else
443 return MMU_IADDR_ERROR;
bellardfdf9b3e2006-04-27 21:07:38 +0000444 }
aurel3229e179b2008-08-22 08:57:43 +0000445 if (address >= 0x80000000 && address < 0xc0000000) {
446 /* Mask upper 3 bits for P1 and P2 areas */
447 *physical = address & 0x1fffffff;
aurel3229e179b2008-08-22 08:57:43 +0000448 } else {
aurel3229e179b2008-08-22 08:57:43 +0000449 *physical = address;
450 }
bellardfdf9b3e2006-04-27 21:07:38 +0000451 *prot = PAGE_READ | PAGE_WRITE;
452 return MMU_OK;
453 }
454
455 /* If MMU is disabled, return the corresponding physical page */
456 if (!env->mmucr & MMUCR_AT) {
457 *physical = address & 0x1FFFFFFF;
458 *prot = PAGE_READ | PAGE_WRITE;
459 return MMU_OK;
460 }
461
462 /* We need to resort to the MMU */
463 return get_mmu_address(env, physical, prot, address, rw, access_type);
464}
465
466int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
j_mayer6ebbf392007-10-14 07:07:08 +0000467 int mmu_idx, int is_softmmu)
bellardfdf9b3e2006-04-27 21:07:38 +0000468{
469 target_ulong physical, page_offset, page_size;
470 int prot, ret, access_type;
471
bellardfdf9b3e2006-04-27 21:07:38 +0000472 access_type = ACCESS_INT;
473 ret =
474 get_physical_address(env, &physical, &prot, address, rw,
475 access_type);
476
477 if (ret != MMU_OK) {
478 env->tea = address;
479 switch (ret) {
480 case MMU_ITLB_MISS:
481 case MMU_DTLB_MISS_READ:
482 env->exception_index = 0x040;
483 break;
484 case MMU_DTLB_MULTIPLE:
485 case MMU_ITLB_MULTIPLE:
486 env->exception_index = 0x140;
487 break;
488 case MMU_ITLB_VIOLATION:
489 env->exception_index = 0x0a0;
490 break;
491 case MMU_DTLB_MISS_WRITE:
492 env->exception_index = 0x060;
493 break;
494 case MMU_DTLB_INITIAL_WRITE:
495 env->exception_index = 0x080;
496 break;
497 case MMU_DTLB_VIOLATION_READ:
498 env->exception_index = 0x0a0;
499 break;
500 case MMU_DTLB_VIOLATION_WRITE:
501 env->exception_index = 0x0c0;
502 break;
aurel32cf7055b2008-11-21 22:33:15 +0000503 case MMU_IADDR_ERROR:
504 case MMU_DADDR_ERROR_READ:
505 env->exception_index = 0x0c0;
506 break;
507 case MMU_DADDR_ERROR_WRITE:
508 env->exception_index = 0x100;
509 break;
bellardfdf9b3e2006-04-27 21:07:38 +0000510 default:
511 assert(0);
512 }
513 return 1;
514 }
515
516 page_size = TARGET_PAGE_SIZE;
517 page_offset =
518 (address - (address & TARGET_PAGE_MASK)) & ~(page_size - 1);
519 address = (address & TARGET_PAGE_MASK) + page_offset;
520 physical = (physical & TARGET_PAGE_MASK) + page_offset;
521
j_mayer6ebbf392007-10-14 07:07:08 +0000522 return tlb_set_page(env, address, physical, prot, mmu_idx, is_softmmu);
bellardfdf9b3e2006-04-27 21:07:38 +0000523}
pbrook355fb232006-06-17 19:58:25 +0000524
j_mayer9b3c35e2007-04-07 11:21:28 +0000525target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
pbrook355fb232006-06-17 19:58:25 +0000526{
527 target_ulong physical;
528 int prot;
529
aurel32cf7055b2008-11-21 22:33:15 +0000530 get_physical_address(env, &physical, &prot, addr, 0, 0);
pbrook355fb232006-06-17 19:58:25 +0000531 return physical;
532}
533
aurel32ef7ec1c2009-03-03 06:12:03 +0000534void cpu_load_tlb(CPUSH4State * env)
aurel32ea2b5422008-05-09 18:45:55 +0000535{
536 int n = cpu_mmucr_urc(env->mmucr);
537 tlb_t * entry = &env->utlb[n];
538
aurel3206afe2c2008-08-22 08:57:52 +0000539 if (entry->v) {
540 /* Overwriting valid entry in utlb. */
541 target_ulong address = entry->vpn << 10;
542 if (!same_tlb_entry_exists(env->itlb, ITLB_SIZE, entry)) {
543 tlb_flush_page(env, address);
544 }
545 }
546
aurel32ea2b5422008-05-09 18:45:55 +0000547 /* Take values into cpu status from registers. */
548 entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
549 entry->vpn = cpu_pteh_vpn(env->pteh);
550 entry->v = (uint8_t)cpu_ptel_v(env->ptel);
551 entry->ppn = cpu_ptel_ppn(env->ptel);
552 entry->sz = (uint8_t)cpu_ptel_sz(env->ptel);
553 switch (entry->sz) {
554 case 0: /* 00 */
555 entry->size = 1024; /* 1K */
556 break;
557 case 1: /* 01 */
558 entry->size = 1024 * 4; /* 4K */
559 break;
560 case 2: /* 10 */
561 entry->size = 1024 * 64; /* 64K */
562 break;
563 case 3: /* 11 */
564 entry->size = 1024 * 1024; /* 1M */
565 break;
566 default:
567 assert(0);
568 break;
569 }
570 entry->sh = (uint8_t)cpu_ptel_sh(env->ptel);
571 entry->c = (uint8_t)cpu_ptel_c(env->ptel);
572 entry->pr = (uint8_t)cpu_ptel_pr(env->ptel);
573 entry->d = (uint8_t)cpu_ptel_d(env->ptel);
574 entry->wt = (uint8_t)cpu_ptel_wt(env->ptel);
575 entry->sa = (uint8_t)cpu_ptea_sa(env->ptea);
576 entry->tc = (uint8_t)cpu_ptea_tc(env->ptea);
577}
578
aurel3229e179b2008-08-22 08:57:43 +0000579void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, target_phys_addr_t addr,
580 uint32_t mem_value)
581{
582 int associate = addr & 0x0000080;
583 uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
584 uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9);
585 uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
586 uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
aurel32eeda6772008-12-10 17:31:51 +0000587 int use_asid = (s->mmucr & MMUCR_SV) == 0 || (s->sr & SR_MD) == 0;
aurel3229e179b2008-08-22 08:57:43 +0000588
589 if (associate) {
590 int i;
591 tlb_t * utlb_match_entry = NULL;
592 int needs_tlb_flush = 0;
593
594 /* search UTLB */
595 for (i = 0; i < UTLB_SIZE; i++) {
596 tlb_t * entry = &s->utlb[i];
597 if (!entry->v)
598 continue;
599
aurel32eeda6772008-12-10 17:31:51 +0000600 if (entry->vpn == vpn
601 && (!use_asid || entry->asid == asid || entry->sh)) {
aurel3229e179b2008-08-22 08:57:43 +0000602 if (utlb_match_entry) {
603 /* Multiple TLB Exception */
604 s->exception_index = 0x140;
605 s->tea = addr;
606 break;
607 }
608 if (entry->v && !v)
609 needs_tlb_flush = 1;
610 entry->v = v;
611 entry->d = d;
612 utlb_match_entry = entry;
613 }
614 increment_urc(s); /* per utlb access */
615 }
616
617 /* search ITLB */
618 for (i = 0; i < ITLB_SIZE; i++) {
619 tlb_t * entry = &s->itlb[i];
aurel32eeda6772008-12-10 17:31:51 +0000620 if (entry->vpn == vpn
621 && (!use_asid || entry->asid == asid || entry->sh)) {
aurel3229e179b2008-08-22 08:57:43 +0000622 if (entry->v && !v)
623 needs_tlb_flush = 1;
624 if (utlb_match_entry)
625 *entry = *utlb_match_entry;
626 else
627 entry->v = v;
628 break;
629 }
630 }
631
632 if (needs_tlb_flush)
633 tlb_flush_page(s, vpn << 10);
634
635 } else {
636 int index = (addr & 0x00003f00) >> 8;
637 tlb_t * entry = &s->utlb[index];
638 if (entry->v) {
639 /* Overwriting valid entry in utlb. */
640 target_ulong address = entry->vpn << 10;
641 if (!same_tlb_entry_exists(s->itlb, ITLB_SIZE, entry)) {
642 tlb_flush_page(s, address);
643 }
644 }
645 entry->asid = asid;
646 entry->vpn = vpn;
647 entry->d = d;
648 entry->v = v;
649 increment_urc(s);
650 }
651}
652
edgar_igl852d4812009-04-01 23:10:46 +0000653int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
654{
655 int n;
656 int use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
657
658 /* check area */
659 if (env->sr & SR_MD) {
660 /* For previledged mode, P2 and P4 area is not cachable. */
661 if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr)
662 return 0;
663 } else {
664 /* For user mode, only U0 area is cachable. */
665 if (0x80000000 <= addr)
666 return 0;
667 }
668
669 /*
670 * TODO : Evaluate CCR and check if the cache is on or off.
671 * Now CCR is not in CPUSH4State, but in SH7750State.
672 * When you move the ccr inot CPUSH4State, the code will be
673 * as follows.
674 */
675#if 0
676 /* check if operand cache is enabled or not. */
677 if (!(env->ccr & 1))
678 return 0;
679#endif
680
681 /* if MMU is off, no check for TLB. */
682 if (env->mmucr & MMUCR_AT)
683 return 1;
684
685 /* check TLB */
686 n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid);
687 if (n >= 0)
688 return env->itlb[n].c;
689
690 n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
691 if (n >= 0)
692 return env->utlb[n].c;
693
694 return 0;
695}
696
pbrook355fb232006-06-17 19:58:25 +0000697#endif