Statistics
| Branch: | Revision:

root / target-sh4 / helper.c @ 833ed386

History | View | Annotate | Download (13.4 kB)

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, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
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"
30
#include "hw/sh_intc.h"
31

    
32
#if defined(CONFIG_USER_ONLY)
33

    
34
void do_interrupt (CPUState *env)
35
{
36
  env->exception_index = -1;
37
}
38

    
39
int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
40
                             int mmu_idx, int is_softmmu)
41
{
42
    env->tea = address;
43
    env->exception_index = 0;
44
    switch (rw) {
45
    case 0:
46
        env->tea = address;
47
        env->exception_index = 0x0a0;
48
        break;
49
    case 1:
50
        env->tea = address;
51
        env->exception_index = 0x0c0;
52
        break;
53
    }
54
    return 1;
55
}
56

    
57
target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
58
{
59
    return addr;
60
}
61

    
62
#else /* !CONFIG_USER_ONLY */
63

    
64
#define MMU_OK                   0
65
#define MMU_ITLB_MISS            (-1)
66
#define MMU_ITLB_MULTIPLE        (-2)
67
#define MMU_ITLB_VIOLATION       (-3)
68
#define MMU_DTLB_MISS_READ       (-4)
69
#define MMU_DTLB_MISS_WRITE      (-5)
70
#define MMU_DTLB_INITIAL_WRITE   (-6)
71
#define MMU_DTLB_VIOLATION_READ  (-7)
72
#define MMU_DTLB_VIOLATION_WRITE (-8)
73
#define MMU_DTLB_MULTIPLE        (-9)
74
#define MMU_DTLB_MISS            (-10)
75

    
76
void do_interrupt(CPUState * env)
77
{
78
    int do_irq = env->interrupt_request & CPU_INTERRUPT_HARD;
79
    int do_exp, irq_vector = env->exception_index;
80

    
81
    /* prioritize exceptions over interrupts */
82

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

    
86
    if (env->sr & SR_BL) {
87
        if (do_exp && env->exception_index != 0x1e0) {
88
            env->exception_index = 0x000; /* masked exception -> reset */
89
        }
90
        if (do_irq && !env->intr_at_halt) {
91
            return; /* masked */
92
        }
93
        env->intr_at_halt = 0;
94
    }
95

    
96
    if (do_irq) {
97
        irq_vector = sh_intc_get_pending_vector(env->intc_handle,
98
                                                (env->sr >> 4) & 0xf);
99
        if (irq_vector == -1) {
100
            return; /* masked */
101
        }
102
    }
103

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

    
155
    env->ssr = env->sr;
156
    env->spc = env->pc;
157
    env->sgr = env->gregs[15];
158
    env->sr |= SR_BL | SR_MD | SR_RB;
159

    
160
    if (do_exp) {
161
        env->expevt = env->exception_index;
162
        switch (env->exception_index) {
163
        case 0x000:
164
        case 0x020:
165
        case 0x140:
166
            env->sr &= ~SR_FD;
167
            env->sr |= 0xf << 4; /* IMASK */
168
            env->pc = 0xa0000000;
169
            break;
170
        case 0x040:
171
        case 0x060:
172
            env->pc = env->vbr + 0x400;
173
            break;
174
        case 0x160:
175
            env->spc += 2; /* special case for TRAPA */
176
            /* fall through */
177
        default:
178
            env->pc = env->vbr + 0x100;
179
            break;
180
        }
181
        return;
182
    }
183

    
184
    if (do_irq) {
185
        env->intevt = irq_vector;
186
        env->pc = env->vbr + 0x600;
187
        return;
188
    }
189
}
190

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

    
195
    switch (itlbnb) {
196
    case 0:
197
        and_mask = 0x1f;
198
        break;
199
    case 1:
200
        and_mask = 0xe7;
201
        or_mask = 0x80;
202
        break;
203
    case 2:
204
        and_mask = 0xfb;
205
        or_mask = 0x50;
206
        break;
207
    case 3:
208
        or_mask = 0x2c;
209
        break;
210
    }
211

    
212
    env->mmucr &= (and_mask << 24) | 0x00ffffff;
213
    env->mmucr |= (or_mask << 24);
214
}
215

    
216
static int itlb_replacement(CPUState * env)
217
{
218
    if ((env->mmucr & 0xe0000000) == 0xe0000000)
219
        return 0;
220
    if ((env->mmucr & 0x98000000) == 0x18000000)
221
        return 1;
222
    if ((env->mmucr & 0x54000000) == 0x04000000)
223
        return 2;
224
    if ((env->mmucr & 0x2c000000) == 0x00000000)
225
        return 3;
226
    assert(0);
227
}
228

    
229
/* Find the corresponding entry in the right TLB
230
   Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
231
*/
232
static int find_tlb_entry(CPUState * env, target_ulong address,
233
                          tlb_t * entries, uint8_t nbtlb, int use_asid)
234
{
235
    int match = MMU_DTLB_MISS;
236
    uint32_t start, end;
237
    uint8_t asid;
238
    int i;
239

    
240
    asid = env->pteh & 0xff;
241

    
242
    for (i = 0; i < nbtlb; i++) {
243
        if (!entries[i].v)
244
            continue;                /* Invalid entry */
245
        if (use_asid && entries[i].asid != asid && !entries[i].sh)
246
            continue;                /* Bad ASID */
247
#if 0
248
        switch (entries[i].sz) {
249
        case 0:
250
            size = 1024;        /* 1kB */
251
            break;
252
        case 1:
253
            size = 4 * 1024;        /* 4kB */
254
            break;
255
        case 2:
256
            size = 64 * 1024;        /* 64kB */
257
            break;
258
        case 3:
259
            size = 1024 * 1024;        /* 1MB */
260
            break;
261
        default:
262
            assert(0);
263
        }
264
#endif
265
        start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
266
        end = start + entries[i].size - 1;
267
        if (address >= start && address <= end) {        /* Match */
268
            if (match != MMU_DTLB_MISS)
269
                return MMU_DTLB_MULTIPLE;        /* Multiple match */
270
            match = i;
271
        }
272
    }
273
    return match;
274
}
275

    
276
/* Find itlb entry - update itlb from utlb if necessary and asked for
277
   Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
278
   Update the itlb from utlb if update is not 0
279
*/
280
int find_itlb_entry(CPUState * env, target_ulong address,
281
                    int use_asid, int update)
282
{
283
    int e, n;
284

    
285
    e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
286
    if (e == MMU_DTLB_MULTIPLE)
287
        e = MMU_ITLB_MULTIPLE;
288
    else if (e == MMU_DTLB_MISS && update) {
289
        e = find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
290
        if (e >= 0) {
291
            n = itlb_replacement(env);
292
            env->itlb[n] = env->utlb[e];
293
            e = n;
294
        } else if (e == MMU_DTLB_MISS)
295
            e = MMU_ITLB_MISS;
296
    } else if (e == MMU_DTLB_MISS)
297
        e = MMU_ITLB_MISS;
298
    if (e >= 0)
299
        update_itlb_use(env, e);
300
    return e;
301
}
302

    
303
/* Find utlb entry
304
   Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
305
int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
306
{
307
    uint8_t urb, urc;
308

    
309
    /* Increment URC */
310
    urb = ((env->mmucr) >> 18) & 0x3f;
311
    urc = ((env->mmucr) >> 10) & 0x3f;
312
    urc++;
313
    if (urc == urb || urc == UTLB_SIZE - 1)
314
        urc = 0;
315
    env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
316

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

    
321
/* Match address against MMU
322
   Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
323
   MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
324
   MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
325
   MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION
326
*/
327
static int get_mmu_address(CPUState * env, target_ulong * physical,
328
                           int *prot, target_ulong address,
329
                           int rw, int access_type)
330
{
331
    int use_asid, is_code, n;
332
    tlb_t *matching = NULL;
333

    
334
    use_asid = (env->mmucr & MMUCR_SV) == 0 && (env->sr & SR_MD) == 0;
335
    is_code = env->pc == address;        /* Hack */
336

    
337
    /* Use a hack to find if this is an instruction or data access */
338
    if (env->pc == address && !(rw & PAGE_WRITE)) {
339
        n = find_itlb_entry(env, address, use_asid, 1);
340
        if (n >= 0) {
341
            matching = &env->itlb[n];
342
            if ((env->sr & SR_MD) & !(matching->pr & 2))
343
                n = MMU_ITLB_VIOLATION;
344
            else
345
                *prot = PAGE_READ;
346
        }
347
    } else {
348
        n = find_utlb_entry(env, address, use_asid);
349
        if (n >= 0) {
350
            matching = &env->utlb[n];
351
            switch ((matching->pr << 1) | ((env->sr & SR_MD) ? 1 : 0)) {
352
            case 0:                /* 000 */
353
            case 2:                /* 010 */
354
                n = (rw & PAGE_WRITE) ? MMU_DTLB_VIOLATION_WRITE :
355
                    MMU_DTLB_VIOLATION_READ;
356
                break;
357
            case 1:                /* 001 */
358
            case 4:                /* 100 */
359
            case 5:                /* 101 */
360
                if (rw & PAGE_WRITE)
361
                    n = MMU_DTLB_VIOLATION_WRITE;
362
                else
363
                    *prot = PAGE_READ;
364
                break;
365
            case 3:                /* 011 */
366
            case 6:                /* 110 */
367
            case 7:                /* 111 */
368
                *prot = rw & (PAGE_READ | PAGE_WRITE);
369
                break;
370
            }
371
        } else if (n == MMU_DTLB_MISS) {
372
            n = (rw & PAGE_WRITE) ? MMU_DTLB_MISS_WRITE :
373
                MMU_DTLB_MISS_READ;
374
        }
375
    }
376
    if (n >= 0) {
377
        *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
378
            (address & (matching->size - 1));
379
        if ((rw & PAGE_WRITE) & !matching->d)
380
            n = MMU_DTLB_INITIAL_WRITE;
381
        else
382
            n = MMU_OK;
383
    }
384
    return n;
385
}
386

    
387
int get_physical_address(CPUState * env, target_ulong * physical,
388
                         int *prot, target_ulong address,
389
                         int rw, int access_type)
390
{
391
    /* P1, P2 and P4 areas do not use translation */
392
    if ((address >= 0x80000000 && address < 0xc0000000) ||
393
        address >= 0xe0000000) {
394
        if (!(env->sr & SR_MD)
395
            && (address < 0xe0000000 || address > 0xe4000000)) {
396
            /* Unauthorized access in user mode (only store queues are available) */
397
            fprintf(stderr, "Unauthorized access\n");
398
            return (rw & PAGE_WRITE) ? MMU_DTLB_MISS_WRITE :
399
                MMU_DTLB_MISS_READ;
400
        }
401
        /* Mask upper 3 bits */
402
        *physical = address & 0x1FFFFFFF;
403
        *prot = PAGE_READ | PAGE_WRITE;
404
        return MMU_OK;
405
    }
406

    
407
    /* If MMU is disabled, return the corresponding physical page */
408
    if (!env->mmucr & MMUCR_AT) {
409
        *physical = address & 0x1FFFFFFF;
410
        *prot = PAGE_READ | PAGE_WRITE;
411
        return MMU_OK;
412
    }
413

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

    
418
int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
419
                             int mmu_idx, int is_softmmu)
420
{
421
    target_ulong physical, page_offset, page_size;
422
    int prot, ret, access_type;
423

    
424
    switch (rw) {
425
    case 0:
426
        rw = PAGE_READ;
427
        break;
428
    case 1:
429
        rw = PAGE_WRITE;
430
        break;
431
    case 2: /* READ_ACCESS_TYPE == 2 defined in softmmu_template.h */
432
        rw = PAGE_READ;
433
        break;
434
    default:
435
        /* fatal error */
436
        assert(0);
437
    }
438

    
439
    /* XXXXX */
440
#if 0
441
    fprintf(stderr, "%s pc %08x ad %08x rw %d mmu_idx %d smmu %d\n",
442
            __func__, env->pc, address, rw, mmu_idx, is_softmmu);
443
#endif
444

    
445
    access_type = ACCESS_INT;
446
    ret =
447
        get_physical_address(env, &physical, &prot, address, rw,
448
                             access_type);
449

    
450
    if (ret != MMU_OK) {
451
        env->tea = address;
452
        switch (ret) {
453
        case MMU_ITLB_MISS:
454
        case MMU_DTLB_MISS_READ:
455
            env->exception_index = 0x040;
456
            break;
457
        case MMU_DTLB_MULTIPLE:
458
        case MMU_ITLB_MULTIPLE:
459
            env->exception_index = 0x140;
460
            break;
461
        case MMU_ITLB_VIOLATION:
462
            env->exception_index = 0x0a0;
463
            break;
464
        case MMU_DTLB_MISS_WRITE:
465
            env->exception_index = 0x060;
466
            break;
467
        case MMU_DTLB_INITIAL_WRITE:
468
            env->exception_index = 0x080;
469
            break;
470
        case MMU_DTLB_VIOLATION_READ:
471
            env->exception_index = 0x0a0;
472
            break;
473
        case MMU_DTLB_VIOLATION_WRITE:
474
            env->exception_index = 0x0c0;
475
            break;
476
        default:
477
            assert(0);
478
        }
479
        return 1;
480
    }
481

    
482
    page_size = TARGET_PAGE_SIZE;
483
    page_offset =
484
        (address - (address & TARGET_PAGE_MASK)) & ~(page_size - 1);
485
    address = (address & TARGET_PAGE_MASK) + page_offset;
486
    physical = (physical & TARGET_PAGE_MASK) + page_offset;
487

    
488
    return tlb_set_page(env, address, physical, prot, mmu_idx, is_softmmu);
489
}
490

    
491
target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
492
{
493
    target_ulong physical;
494
    int prot;
495

    
496
    get_physical_address(env, &physical, &prot, addr, PAGE_READ, 0);
497
    return physical;
498
}
499

    
500
void cpu_load_tlb(CPUState * env)
501
{
502
    int n = cpu_mmucr_urc(env->mmucr);
503
    tlb_t * entry = &env->utlb[n];
504

    
505
    /* Take values into cpu status from registers. */
506
    entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
507
    entry->vpn  = cpu_pteh_vpn(env->pteh);
508
    entry->v    = (uint8_t)cpu_ptel_v(env->ptel);
509
    entry->ppn  = cpu_ptel_ppn(env->ptel);
510
    entry->sz   = (uint8_t)cpu_ptel_sz(env->ptel);
511
    switch (entry->sz) {
512
    case 0: /* 00 */
513
        entry->size = 1024; /* 1K */
514
        break;
515
    case 1: /* 01 */
516
        entry->size = 1024 * 4; /* 4K */
517
        break;
518
    case 2: /* 10 */
519
        entry->size = 1024 * 64; /* 64K */
520
        break;
521
    case 3: /* 11 */
522
        entry->size = 1024 * 1024; /* 1M */
523
        break;
524
    default:
525
        assert(0);
526
        break;
527
    }
528
    entry->sh   = (uint8_t)cpu_ptel_sh(env->ptel);
529
    entry->c    = (uint8_t)cpu_ptel_c(env->ptel);
530
    entry->pr   = (uint8_t)cpu_ptel_pr(env->ptel);
531
    entry->d    = (uint8_t)cpu_ptel_d(env->ptel);
532
    entry->wt   = (uint8_t)cpu_ptel_wt(env->ptel);
533
    entry->sa   = (uint8_t)cpu_ptea_sa(env->ptea);
534
    entry->tc   = (uint8_t)cpu_ptea_tc(env->ptea);
535
}
536

    
537
#endif