helper.c 18.2 KB
Newer Older
B
bellard 已提交
1 2
/*
 *  SH4 emulation
3
 *
B
bellard 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *  Copyright (c) 2005 Samuel Tardieu
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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/>.
B
bellard 已提交
18 19 20 21 22 23 24 25 26 27
 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <signal.h>

#include "cpu.h"
#include "exec-all.h"
28
#include "hw/sh_intc.h"
B
bellard 已提交
29

P
pbrook 已提交
30 31 32 33 34 35 36 37
#if defined(CONFIG_USER_ONLY)

void do_interrupt (CPUState *env)
{
  env->exception_index = -1;
}

int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
38
			     int mmu_idx, int is_softmmu)
P
pbrook 已提交
39 40
{
    env->tea = address;
B
Blue Swirl 已提交
41
    env->exception_index = -1;
P
pbrook 已提交
42 43 44 45 46 47 48
    switch (rw) {
    case 0:
        env->exception_index = 0x0a0;
        break;
    case 1:
        env->exception_index = 0x0c0;
        break;
A
aurel32 已提交
49 50 51
    case 2:
        env->exception_index = 0x0a0;
        break;
P
pbrook 已提交
52 53 54 55
    }
    return 1;
}

56 57 58
int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
{
    /* For user mode, only U0 area is cachable. */
E
edgar_igl 已提交
59
    return !(addr & 0x80000000);
60 61
}

P
pbrook 已提交
62 63
#else /* !CONFIG_USER_ONLY */

B
bellard 已提交
64 65 66 67 68 69 70 71 72 73 74
#define MMU_OK                   0
#define MMU_ITLB_MISS            (-1)
#define MMU_ITLB_MULTIPLE        (-2)
#define MMU_ITLB_VIOLATION       (-3)
#define MMU_DTLB_MISS_READ       (-4)
#define MMU_DTLB_MISS_WRITE      (-5)
#define MMU_DTLB_INITIAL_WRITE   (-6)
#define MMU_DTLB_VIOLATION_READ  (-7)
#define MMU_DTLB_VIOLATION_WRITE (-8)
#define MMU_DTLB_MULTIPLE        (-9)
#define MMU_DTLB_MISS            (-10)
A
aurel32 已提交
75 76 77
#define MMU_IADDR_ERROR          (-11)
#define MMU_DADDR_ERROR_READ     (-12)
#define MMU_DADDR_ERROR_WRITE    (-13)
B
bellard 已提交
78 79 80

void do_interrupt(CPUState * env)
{
81 82 83 84 85 86 87 88 89 90 91 92
    int do_irq = env->interrupt_request & CPU_INTERRUPT_HARD;
    int do_exp, irq_vector = env->exception_index;

    /* prioritize exceptions over interrupts */

    do_exp = env->exception_index != -1;
    do_irq = do_irq && (env->exception_index == -1);

    if (env->sr & SR_BL) {
        if (do_exp && env->exception_index != 0x1e0) {
            env->exception_index = 0x000; /* masked exception -> reset */
        }
A
aurel32 已提交
93
        if (do_irq && !env->intr_at_halt) {
94 95
            return; /* masked */
        }
A
aurel32 已提交
96
        env->intr_at_halt = 0;
97 98 99 100 101 102 103 104 105 106
    }

    if (do_irq) {
        irq_vector = sh_intc_get_pending_vector(env->intc_handle,
						(env->sr >> 4) & 0xf);
        if (irq_vector == -1) {
            return; /* masked */
	}
    }

107
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
B
bellard 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	const char *expname;
	switch (env->exception_index) {
	case 0x0e0:
	    expname = "addr_error";
	    break;
	case 0x040:
	    expname = "tlb_miss";
	    break;
	case 0x0a0:
	    expname = "tlb_violation";
	    break;
	case 0x180:
	    expname = "illegal_instruction";
	    break;
	case 0x1a0:
	    expname = "slot_illegal_instruction";
	    break;
	case 0x800:
	    expname = "fpu_disable";
	    break;
	case 0x820:
	    expname = "slot_fpu";
	    break;
	case 0x100:
	    expname = "data_write";
	    break;
	case 0x060:
	    expname = "dtlb_miss_write";
	    break;
	case 0x0c0:
	    expname = "dtlb_violation_write";
	    break;
	case 0x120:
	    expname = "fpu_exception";
	    break;
	case 0x080:
	    expname = "initial_page_write";
	    break;
	case 0x160:
	    expname = "trapa";
	    break;
	default:
150 151
            expname = do_irq ? "interrupt" : "???";
            break;
B
bellard 已提交
152
	}
153 154 155
	qemu_log("exception 0x%03x [%s] raised\n",
		  irq_vector, expname);
	log_cpu_state(env, 0);
B
bellard 已提交
156 157 158
    }

    env->ssr = env->sr;
159
    env->spc = env->pc;
B
bellard 已提交
160 161 162
    env->sgr = env->gregs[15];
    env->sr |= SR_BL | SR_MD | SR_RB;

A
aurel32 已提交
163 164 165 166 167 168 169 170 171
    if (env->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) {
        /* Branch instruction should be executed again before delay slot. */
	env->spc -= 2;
	/* Clear flags for exception/interrupt routine. */
	env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL | DELAY_SLOT_TRUE);
    }
    if (env->flags & DELAY_SLOT_CLEARME)
        env->flags = 0;

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    if (do_exp) {
        env->expevt = env->exception_index;
        switch (env->exception_index) {
        case 0x000:
        case 0x020:
        case 0x140:
            env->sr &= ~SR_FD;
            env->sr |= 0xf << 4; /* IMASK */
            env->pc = 0xa0000000;
            break;
        case 0x040:
        case 0x060:
            env->pc = env->vbr + 0x400;
            break;
        case 0x160:
            env->spc += 2; /* special case for TRAPA */
            /* fall through */
        default:
            env->pc = env->vbr + 0x100;
            break;
        }
        return;
    }

    if (do_irq) {
        env->intevt = irq_vector;
        env->pc = env->vbr + 0x600;
        return;
B
bellard 已提交
200 201 202 203 204 205 206 207 208
    }
}

static void update_itlb_use(CPUState * env, int itlbnb)
{
    uint8_t or_mask = 0, and_mask = (uint8_t) - 1;

    switch (itlbnb) {
    case 0:
A
aurel32 已提交
209
	and_mask = 0x1f;
B
bellard 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223
	break;
    case 1:
	and_mask = 0xe7;
	or_mask = 0x80;
	break;
    case 2:
	and_mask = 0xfb;
	or_mask = 0x50;
	break;
    case 3:
	or_mask = 0x2c;
	break;
    }

A
aurel32 已提交
224
    env->mmucr &= (and_mask << 24) | 0x00ffffff;
B
bellard 已提交
225 226 227 228 229 230 231
    env->mmucr |= (or_mask << 24);
}

static int itlb_replacement(CPUState * env)
{
    if ((env->mmucr & 0xe0000000) == 0xe0000000)
	return 0;
A
aurel32 已提交
232
    if ((env->mmucr & 0x98000000) == 0x18000000)
B
bellard 已提交
233 234 235 236 237
	return 1;
    if ((env->mmucr & 0x54000000) == 0x04000000)
	return 2;
    if ((env->mmucr & 0x2c000000) == 0x00000000)
	return 3;
238
    cpu_abort(env, "Unhandled itlb_replacement");
B
bellard 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
}

/* Find the corresponding entry in the right TLB
   Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
*/
static int find_tlb_entry(CPUState * env, target_ulong address,
			  tlb_t * entries, uint8_t nbtlb, int use_asid)
{
    int match = MMU_DTLB_MISS;
    uint32_t start, end;
    uint8_t asid;
    int i;

    asid = env->pteh & 0xff;

    for (i = 0; i < nbtlb; i++) {
	if (!entries[i].v)
	    continue;		/* Invalid entry */
A
aurel32 已提交
257
	if (!entries[i].sh && use_asid && entries[i].asid != asid)
B
bellard 已提交
258 259 260 261
	    continue;		/* Bad ASID */
	start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
	end = start + entries[i].size - 1;
	if (address >= start && address <= end) {	/* Match */
A
aurel32 已提交
262
	    if (match != MMU_DTLB_MISS)
B
bellard 已提交
263 264 265 266 267 268 269
		return MMU_DTLB_MULTIPLE;	/* Multiple match */
	    match = i;
	}
    }
    return match;
}

A
aurel32 已提交
270 271 272 273 274 275 276 277
static void increment_urc(CPUState * env)
{
    uint8_t urb, urc;

    /* Increment URC */
    urb = ((env->mmucr) >> 18) & 0x3f;
    urc = ((env->mmucr) >> 10) & 0x3f;
    urc++;
278
    if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1))
A
aurel32 已提交
279 280 281 282
	urc = 0;
    env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
}

A
Aurelien Jarno 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/* Copy and utlb entry into itlb
   Return entry
*/
static int copy_utlb_entry_itlb(CPUState *env, int utlb)
{
    int itlb;

    tlb_t * ientry;
    itlb = itlb_replacement(env);
    ientry = &env->itlb[itlb];
    if (ientry->v) {
        tlb_flush_page(env, ientry->vpn << 10);
    }
    *ientry = env->utlb[utlb];
    update_itlb_use(env, itlb);
    return itlb;
}

/* Find itlb entry
B
bellard 已提交
302 303
   Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
*/
304
static int find_itlb_entry(CPUState * env, target_ulong address,
A
Aurelien Jarno 已提交
305
                           int use_asid)
B
bellard 已提交
306
{
A
Aurelien Jarno 已提交
307
    int e;
B
bellard 已提交
308 309

    e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
A
Aurelien Jarno 已提交
310
    if (e == MMU_DTLB_MULTIPLE) {
B
bellard 已提交
311
	e = MMU_ITLB_MULTIPLE;
A
Aurelien Jarno 已提交
312
    } else if (e == MMU_DTLB_MISS) {
A
aurel32 已提交
313
	e = MMU_ITLB_MISS;
A
Aurelien Jarno 已提交
314
    } else if (e >= 0) {
B
bellard 已提交
315
	update_itlb_use(env, e);
A
Aurelien Jarno 已提交
316
    }
B
bellard 已提交
317 318 319 320 321
    return e;
}

/* Find utlb entry
   Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
322
static int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
B
bellard 已提交
323
{
A
aurel32 已提交
324 325
    /* per utlb access */
    increment_urc(env);
B
bellard 已提交
326 327 328 329 330 331 332 333 334

    /* Return entry */
    return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
}

/* Match address against MMU
   Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
   MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
   MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
A
aurel32 已提交
335 336
   MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
   MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
B
bellard 已提交
337 338 339 340 341
*/
static int get_mmu_address(CPUState * env, target_ulong * physical,
			   int *prot, target_ulong address,
			   int rw, int access_type)
{
A
aurel32 已提交
342
    int use_asid, n;
B
bellard 已提交
343 344
    tlb_t *matching = NULL;

A
aurel32 已提交
345
    use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;
B
bellard 已提交
346

A
aurel32 已提交
347
    if (rw == 2) {
A
Aurelien Jarno 已提交
348
        n = find_itlb_entry(env, address, use_asid);
B
bellard 已提交
349 350
	if (n >= 0) {
	    matching = &env->itlb[n];
351
	    if (!(env->sr & SR_MD) && !(matching->pr & 2))
B
bellard 已提交
352 353
		n = MMU_ITLB_VIOLATION;
	    else
354
		*prot = PAGE_EXEC;
A
Aurelien Jarno 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        } else {
            n = find_utlb_entry(env, address, use_asid);
            if (n >= 0) {
                n = copy_utlb_entry_itlb(env, n);
                matching = &env->itlb[n];
                if (!(env->sr & SR_MD) && !(matching->pr & 2)) {
                      n = MMU_ITLB_VIOLATION;
                } else {
                    *prot = PAGE_READ | PAGE_EXEC;
                    if ((matching->pr & 1) && matching->d) {
                        *prot |= PAGE_WRITE;
                    }
                }
            } else if (n == MMU_DTLB_MULTIPLE) {
                n = MMU_ITLB_MULTIPLE;
            } else if (n == MMU_DTLB_MISS) {
                n = MMU_ITLB_MISS;
            }
B
bellard 已提交
373 374 375 376 377
	}
    } else {
	n = find_utlb_entry(env, address, use_asid);
	if (n >= 0) {
	    matching = &env->utlb[n];
378 379 380 381 382
            if (!(env->sr & SR_MD) && !(matching->pr & 2)) {
                n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE :
                    MMU_DTLB_VIOLATION_READ;
            } else if ((rw == 1) && !(matching->pr & 1)) {
                n = MMU_DTLB_VIOLATION_WRITE;
383
            } else if ((rw == 1) && !matching->d) {
384 385 386 387 388 389 390
                n = MMU_DTLB_INITIAL_WRITE;
            } else {
                *prot = PAGE_READ;
                if ((matching->pr & 1) && matching->d) {
                    *prot |= PAGE_WRITE;
                }
            }
B
bellard 已提交
391
	} else if (n == MMU_DTLB_MISS) {
A
aurel32 已提交
392
	    n = (rw == 1) ? MMU_DTLB_MISS_WRITE :
B
bellard 已提交
393 394 395 396
		MMU_DTLB_MISS_READ;
	}
    }
    if (n >= 0) {
397
	n = MMU_OK;
B
bellard 已提交
398 399 400 401 402 403
	*physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
	    (address & (matching->size - 1));
    }
    return n;
}

404 405 406
static int get_physical_address(CPUState * env, target_ulong * physical,
                                int *prot, target_ulong address,
                                int rw, int access_type)
B
bellard 已提交
407 408 409 410 411
{
    /* P1, P2 and P4 areas do not use translation */
    if ((address >= 0x80000000 && address < 0xc0000000) ||
	address >= 0xe0000000) {
	if (!(env->sr & SR_MD)
412
	    && (address < 0xe0000000 || address >= 0xe4000000)) {
B
bellard 已提交
413 414
	    /* Unauthorized access in user mode (only store queues are available) */
	    fprintf(stderr, "Unauthorized access\n");
A
aurel32 已提交
415 416 417 418 419 420
	    if (rw == 0)
		return MMU_DADDR_ERROR_READ;
	    else if (rw == 1)
		return MMU_DADDR_ERROR_WRITE;
	    else
		return MMU_IADDR_ERROR;
B
bellard 已提交
421
	}
A
aurel32 已提交
422 423 424 425 426 427
	if (address >= 0x80000000 && address < 0xc0000000) {
	    /* Mask upper 3 bits for P1 and P2 areas */
	    *physical = address & 0x1fffffff;
	} else {
	    *physical = address;
	}
428
	*prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
B
bellard 已提交
429 430 431 432
	return MMU_OK;
    }

    /* If MMU is disabled, return the corresponding physical page */
433
    if (!(env->mmucr & MMUCR_AT)) {
B
bellard 已提交
434
	*physical = address & 0x1FFFFFFF;
435
	*prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
B
bellard 已提交
436 437 438 439 440 441 442 443
	return MMU_OK;
    }

    /* We need to resort to the MMU */
    return get_mmu_address(env, physical, prot, address, rw, access_type);
}

int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
444
			     int mmu_idx, int is_softmmu)
B
bellard 已提交
445
{
446
    target_ulong physical;
B
bellard 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    int prot, ret, access_type;

    access_type = ACCESS_INT;
    ret =
	get_physical_address(env, &physical, &prot, address, rw,
			     access_type);

    if (ret != MMU_OK) {
	env->tea = address;
	switch (ret) {
	case MMU_ITLB_MISS:
	case MMU_DTLB_MISS_READ:
	    env->exception_index = 0x040;
	    break;
	case MMU_DTLB_MULTIPLE:
	case MMU_ITLB_MULTIPLE:
	    env->exception_index = 0x140;
	    break;
	case MMU_ITLB_VIOLATION:
	    env->exception_index = 0x0a0;
	    break;
	case MMU_DTLB_MISS_WRITE:
	    env->exception_index = 0x060;
	    break;
	case MMU_DTLB_INITIAL_WRITE:
	    env->exception_index = 0x080;
	    break;
	case MMU_DTLB_VIOLATION_READ:
	    env->exception_index = 0x0a0;
	    break;
	case MMU_DTLB_VIOLATION_WRITE:
	    env->exception_index = 0x0c0;
	    break;
A
aurel32 已提交
480 481 482 483 484 485 486
	case MMU_IADDR_ERROR:
	case MMU_DADDR_ERROR_READ:
	    env->exception_index = 0x0c0;
	    break;
	case MMU_DADDR_ERROR_WRITE:
	    env->exception_index = 0x100;
	    break;
B
bellard 已提交
487
	default:
488
            cpu_abort(env, "Unhandled MMU fault");
B
bellard 已提交
489 490 491 492
	}
	return 1;
    }

493 494
    address &= TARGET_PAGE_MASK;
    physical &= TARGET_PAGE_MASK;
B
bellard 已提交
495

P
Paul Brook 已提交
496 497
    tlb_set_page(env, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
    return 0;
B
bellard 已提交
498
}
P
pbrook 已提交
499

A
Anthony Liguori 已提交
500
target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
P
pbrook 已提交
501 502 503 504
{
    target_ulong physical;
    int prot;

A
aurel32 已提交
505
    get_physical_address(env, &physical, &prot, addr, 0, 0);
P
pbrook 已提交
506 507 508
    return physical;
}

509
void cpu_load_tlb(CPUSH4State * env)
A
aurel32 已提交
510 511 512 513
{
    int n = cpu_mmucr_urc(env->mmucr);
    tlb_t * entry = &env->utlb[n];

A
aurel32 已提交
514 515 516
    if (entry->v) {
        /* Overwriting valid entry in utlb. */
        target_ulong address = entry->vpn << 10;
517
	tlb_flush_page(env, address);
A
aurel32 已提交
518 519
    }

A
aurel32 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    /* Take values into cpu status from registers. */
    entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
    entry->vpn  = cpu_pteh_vpn(env->pteh);
    entry->v    = (uint8_t)cpu_ptel_v(env->ptel);
    entry->ppn  = cpu_ptel_ppn(env->ptel);
    entry->sz   = (uint8_t)cpu_ptel_sz(env->ptel);
    switch (entry->sz) {
    case 0: /* 00 */
        entry->size = 1024; /* 1K */
        break;
    case 1: /* 01 */
        entry->size = 1024 * 4; /* 4K */
        break;
    case 2: /* 10 */
        entry->size = 1024 * 64; /* 64K */
        break;
    case 3: /* 11 */
        entry->size = 1024 * 1024; /* 1M */
        break;
    default:
540
        cpu_abort(env, "Unhandled load_tlb");
A
aurel32 已提交
541 542 543 544 545 546 547 548 549 550 551
        break;
    }
    entry->sh   = (uint8_t)cpu_ptel_sh(env->ptel);
    entry->c    = (uint8_t)cpu_ptel_c(env->ptel);
    entry->pr   = (uint8_t)cpu_ptel_pr(env->ptel);
    entry->d    = (uint8_t)cpu_ptel_d(env->ptel);
    entry->wt   = (uint8_t)cpu_ptel_wt(env->ptel);
    entry->sa   = (uint8_t)cpu_ptea_sa(env->ptea);
    entry->tc   = (uint8_t)cpu_ptea_tc(env->ptea);
}

A
Aurelien Jarno 已提交
552 553 554 555 556 557 558 559 560 561
 void cpu_sh4_invalidate_tlb(CPUSH4State *s)
{
    int i;

    /* UTLB */
    for (i = 0; i < UTLB_SIZE; i++) {
        tlb_t * entry = &s->utlb[i];
        entry->v = 0;
    }
    /* ITLB */
562 563
    for (i = 0; i < ITLB_SIZE; i++) {
        tlb_t * entry = &s->itlb[i];
A
Aurelien Jarno 已提交
564 565 566 567 568 569
        entry->v = 0;
    }

    tlb_flush(s, 1);
}

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State *s, target_phys_addr_t addr,
				    uint32_t mem_value)
{
    uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
    uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
    uint8_t asid = (uint8_t)(mem_value & 0x000000ff);

    int index = (addr & 0x00003f00) >> 8;
    tlb_t * entry = &s->itlb[index];
    if (entry->v) {
        /* Overwriting valid entry in itlb. */
        target_ulong address = entry->vpn << 10;
        tlb_flush_page(s, address);
    }
    entry->asid = asid;
    entry->vpn = vpn;
    entry->v = v;
}

A
Anthony Liguori 已提交
589
void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, target_phys_addr_t addr,
A
aurel32 已提交
590 591 592 593 594 595 596
				    uint32_t mem_value)
{
    int associate = addr & 0x0000080;
    uint32_t vpn = (mem_value & 0xfffffc00) >> 10;
    uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9);
    uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8);
    uint8_t asid = (uint8_t)(mem_value & 0x000000ff);
A
aurel32 已提交
597
    int use_asid = (s->mmucr & MMUCR_SV) == 0 || (s->sr & SR_MD) == 0;
A
aurel32 已提交
598 599 600 601 602 603 604 605 606 607 608 609

    if (associate) {
        int i;
	tlb_t * utlb_match_entry = NULL;
	int needs_tlb_flush = 0;

	/* search UTLB */
	for (i = 0; i < UTLB_SIZE; i++) {
            tlb_t * entry = &s->utlb[i];
            if (!entry->v)
	        continue;

A
aurel32 已提交
610 611
            if (entry->vpn == vpn
                && (!use_asid || entry->asid == asid || entry->sh)) {
A
aurel32 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	        if (utlb_match_entry) {
		    /* Multiple TLB Exception */
		    s->exception_index = 0x140;
		    s->tea = addr;
		    break;
	        }
		if (entry->v && !v)
		    needs_tlb_flush = 1;
		entry->v = v;
		entry->d = d;
	        utlb_match_entry = entry;
	    }
	    increment_urc(s); /* per utlb access */
	}

	/* search ITLB */
	for (i = 0; i < ITLB_SIZE; i++) {
            tlb_t * entry = &s->itlb[i];
A
aurel32 已提交
630 631
            if (entry->vpn == vpn
                && (!use_asid || entry->asid == asid || entry->sh)) {
A
aurel32 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	        if (entry->v && !v)
		    needs_tlb_flush = 1;
	        if (utlb_match_entry)
		    *entry = *utlb_match_entry;
	        else
		    entry->v = v;
		break;
	    }
	}

	if (needs_tlb_flush)
	    tlb_flush_page(s, vpn << 10);
        
    } else {
        int index = (addr & 0x00003f00) >> 8;
        tlb_t * entry = &s->utlb[index];
	if (entry->v) {
	    /* Overwriting valid entry in utlb. */
            target_ulong address = entry->vpn << 10;
651
	    tlb_flush_page(s, address);
A
aurel32 已提交
652 653 654 655 656 657 658 659 660
	}
	entry->asid = asid;
	entry->vpn = vpn;
	entry->d = d;
	entry->v = v;
	increment_urc(s);
    }
}

E
edgar_igl 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr)
{
    int n;
    int use_asid = (env->mmucr & MMUCR_SV) == 0 || (env->sr & SR_MD) == 0;

    /* check area */
    if (env->sr & SR_MD) {
        /* For previledged mode, P2 and P4 area is not cachable. */
        if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr)
            return 0;
    } else {
        /* For user mode, only U0 area is cachable. */
        if (0x80000000 <= addr)
            return 0;
    }

    /*
     * TODO : Evaluate CCR and check if the cache is on or off.
     *        Now CCR is not in CPUSH4State, but in SH7750State.
     *        When you move the ccr inot CPUSH4State, the code will be
     *        as follows.
     */
#if 0
    /* check if operand cache is enabled or not. */
    if (!(env->ccr & 1))
        return 0;
#endif

    /* if MMU is off, no check for TLB. */
    if (env->mmucr & MMUCR_AT)
        return 1;

    /* check TLB */
    n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid);
    if (n >= 0)
        return env->itlb[n].c;

    n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid);
    if (n >= 0)
        return env->utlb[n].c;

    return 0;
}

P
pbrook 已提交
705
#endif