Statistics
| Branch: | Revision:

root / target-sparc / helper.c @ 55754d9e

History | View | Annotate | Download (12 kB)

1
/*
2
 *  sparc helpers
3
 * 
4
 *  Copyright (c) 2003 Fabrice Bellard
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 "exec.h"
21

    
22
//#define DEBUG_PCALL
23
//#define DEBUG_MMU
24

    
25
/* Sparc MMU emulation */
26

    
27
/* thread support */
28

    
29
spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
30

    
31
void cpu_lock(void)
32
{
33
    spin_lock(&global_cpu_lock);
34
}
35

    
36
void cpu_unlock(void)
37
{
38
    spin_unlock(&global_cpu_lock);
39
}
40

    
41
#if defined(CONFIG_USER_ONLY) 
42

    
43
int cpu_sparc_handle_mmu_fault(CPUState *env, target_ulong address, int rw,
44
                               int is_user, int is_softmmu)
45
{
46
    env->mmuregs[4] = address;
47
    if (rw & 2)
48
        env->exception_index = TT_TFAULT;
49
    else
50
        env->exception_index = TT_DFAULT;
51
    return 1;
52
}
53

    
54
#else
55

    
56
#define MMUSUFFIX _mmu
57
#define GETPC() (__builtin_return_address(0))
58

    
59
#define SHIFT 0
60
#include "softmmu_template.h"
61

    
62
#define SHIFT 1
63
#include "softmmu_template.h"
64

    
65
#define SHIFT 2
66
#include "softmmu_template.h"
67

    
68
#define SHIFT 3
69
#include "softmmu_template.h"
70

    
71

    
72
/* try to fill the TLB and return an exception if error. If retaddr is
73
   NULL, it means that the function was called in C code (i.e. not
74
   from generated code or from helper.c) */
75
/* XXX: fix it to restore all registers */
76
void tlb_fill(target_ulong addr, int is_write, int is_user, void *retaddr)
77
{
78
    TranslationBlock *tb;
79
    int ret;
80
    unsigned long pc;
81
    CPUState *saved_env;
82

    
83
    /* XXX: hack to restore env in all cases, even if not called from
84
       generated code */
85
    saved_env = env;
86
    env = cpu_single_env;
87

    
88
    ret = cpu_sparc_handle_mmu_fault(env, addr, is_write, is_user, 1);
89
    if (ret) {
90
        if (retaddr) {
91
            /* now we have a real cpu fault */
92
            pc = (unsigned long)retaddr;
93
            tb = tb_find_pc(pc);
94
            if (tb) {
95
                /* the PC is inside the translated code. It means that we have
96
                   a virtual CPU fault */
97
                cpu_restore_state(tb, env, pc, NULL);
98
            }
99
        }
100
        cpu_loop_exit();
101
    }
102
    env = saved_env;
103
}
104

    
105
static const int access_table[8][8] = {
106
    { 0, 0, 0, 0, 2, 0, 3, 3 },
107
    { 0, 0, 0, 0, 2, 0, 0, 0 },
108
    { 2, 2, 0, 0, 0, 2, 3, 3 },
109
    { 2, 2, 0, 0, 0, 2, 0, 0 },
110
    { 2, 0, 2, 0, 2, 2, 3, 3 },
111
    { 2, 0, 2, 0, 2, 0, 2, 0 },
112
    { 2, 2, 2, 0, 2, 2, 3, 3 },
113
    { 2, 2, 2, 0, 2, 2, 2, 0 }
114
};
115

    
116
/* 1 = write OK */
117
static const int rw_table[2][8] = {
118
    { 0, 1, 0, 1, 0, 1, 0, 1 },
119
    { 0, 1, 0, 1, 0, 0, 0, 0 }
120
};
121

    
122
int get_physical_address (CPUState *env, target_phys_addr_t *physical, int *prot,
123
                          int *access_index, target_ulong address, int rw,
124
                          int is_user)
125
{
126
    int access_perms = 0;
127
    target_phys_addr_t pde_ptr;
128
    uint32_t pde;
129
    target_ulong virt_addr;
130
    int error_code = 0, is_dirty;
131
    unsigned long page_offset;
132

    
133
    virt_addr = address & TARGET_PAGE_MASK;
134
    if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
135
        *physical = address;
136
        *prot = PAGE_READ | PAGE_WRITE;
137
        return 0;
138
    }
139

    
140
    *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
141

    
142
    /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
143
    /* Context base + context number */
144
    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 4);
145
    pde = ldl_phys(pde_ptr);
146

    
147
    /* Ctx pde */
148
    switch (pde & PTE_ENTRYTYPE_MASK) {
149
    default:
150
    case 0: /* Invalid */
151
        return 1 << 2;
152
    case 2: /* L0 PTE, maybe should not happen? */
153
    case 3: /* Reserved */
154
        return 4 << 2;
155
    case 1: /* L0 PDE */
156
        pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
157
        pde = ldl_phys(pde_ptr);
158

    
159
        switch (pde & PTE_ENTRYTYPE_MASK) {
160
        default:
161
        case 0: /* Invalid */
162
            return (1 << 8) | (1 << 2);
163
        case 3: /* Reserved */
164
            return (1 << 8) | (4 << 2);
165
        case 1: /* L1 PDE */
166
            pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
167
            pde = ldl_phys(pde_ptr);
168

    
169
            switch (pde & PTE_ENTRYTYPE_MASK) {
170
            default:
171
            case 0: /* Invalid */
172
                return (2 << 8) | (1 << 2);
173
            case 3: /* Reserved */
174
                return (2 << 8) | (4 << 2);
175
            case 1: /* L2 PDE */
176
                pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
177
                pde = ldl_phys(pde_ptr);
178

    
179
                switch (pde & PTE_ENTRYTYPE_MASK) {
180
                default:
181
                case 0: /* Invalid */
182
                    return (3 << 8) | (1 << 2);
183
                case 1: /* PDE, should not happen */
184
                case 3: /* Reserved */
185
                    return (3 << 8) | (4 << 2);
186
                case 2: /* L3 PTE */
187
                    virt_addr = address & TARGET_PAGE_MASK;
188
                    page_offset = (address & TARGET_PAGE_MASK) & (TARGET_PAGE_SIZE - 1);
189
                }
190
                break;
191
            case 2: /* L2 PTE */
192
                virt_addr = address & ~0x3ffff;
193
                page_offset = address & 0x3ffff;
194
            }
195
            break;
196
        case 2: /* L1 PTE */
197
            virt_addr = address & ~0xffffff;
198
            page_offset = address & 0xffffff;
199
        }
200
    }
201

    
202
    /* update page modified and dirty bits */
203
    is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
204
    if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
205
        pde |= PG_ACCESSED_MASK;
206
        if (is_dirty)
207
            pde |= PG_MODIFIED_MASK;
208
        stl_phys_notdirty(pde_ptr, pde);
209
    }
210
    /* check access */
211
    access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
212
    error_code = access_table[*access_index][access_perms];
213
    if (error_code)
214
        return error_code;
215

    
216
    /* the page can be put in the TLB */
217
    *prot = PAGE_READ;
218
    if (pde & PG_MODIFIED_MASK) {
219
        /* only set write access if already dirty... otherwise wait
220
           for dirty access */
221
        if (rw_table[is_user][access_perms])
222
                *prot |= PAGE_WRITE;
223
    }
224

    
225
    /* Even if large ptes, we map only one 4KB page in the cache to
226
       avoid filling it too fast */
227
    *physical = ((pde & PTE_ADDR_MASK) << 4) + page_offset;
228
    return 0;
229
}
230

    
231
/* Perform address translation */
232
int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
233
                              int is_user, int is_softmmu)
234
{
235
    target_ulong virt_addr;
236
    target_phys_addr_t paddr;
237
    unsigned long vaddr;
238
    int error_code = 0, prot, ret = 0, access_index;
239

    
240
    error_code = get_physical_address(env, &paddr, &prot, &access_index, address, rw, is_user);
241
    if (error_code == 0) {
242
        virt_addr = address & TARGET_PAGE_MASK;
243
        vaddr = virt_addr + ((address & TARGET_PAGE_MASK) & (TARGET_PAGE_SIZE - 1));
244
        ret = tlb_set_page(env, vaddr, paddr, prot, is_user, is_softmmu);
245
        return ret;
246
    }
247

    
248
    if (env->mmuregs[3]) /* Fault status register */
249
        env->mmuregs[3] = 1; /* overflow (not read before another fault) */
250
    env->mmuregs[3] |= (access_index << 5) | error_code | 2;
251
    env->mmuregs[4] = address; /* Fault address register */
252

    
253
    if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
254
#if 0
255
        // No fault
256
        vaddr = address & TARGET_PAGE_MASK;
257
        paddr = 0xfffff000;
258
        prot = PAGE_READ | PAGE_WRITE;
259
        ret = tlb_set_page(env, vaddr, paddr, prot, is_user, is_softmmu);
260
        return ret;
261
#else
262
        cpu_abort(env, "MMU no fault case no handled");
263
        return 0;
264
#endif
265
    } else {
266
        if (rw & 2)
267
            env->exception_index = TT_TFAULT;
268
        else
269
            env->exception_index = TT_DFAULT;
270
        return 1;
271
    }
272
}
273
#endif
274

    
275
void memcpy32(target_ulong *dst, const target_ulong *src)
276
{
277
    dst[0] = src[0];
278
    dst[1] = src[1];
279
    dst[2] = src[2];
280
    dst[3] = src[3];
281
    dst[4] = src[4];
282
    dst[5] = src[5];
283
    dst[6] = src[6];
284
    dst[7] = src[7];
285
}
286

    
287
void set_cwp(int new_cwp)
288
{
289
    /* put the modified wrap registers at their proper location */
290
    if (env->cwp == (NWINDOWS - 1))
291
        memcpy32(env->regbase, env->regbase + NWINDOWS * 16);
292
    env->cwp = new_cwp;
293
    /* put the wrap registers at their temporary location */
294
    if (new_cwp == (NWINDOWS - 1))
295
        memcpy32(env->regbase + NWINDOWS * 16, env->regbase);
296
    env->regwptr = env->regbase + (new_cwp * 16);
297
}
298

    
299
void cpu_set_cwp(CPUState *env1, int new_cwp)
300
{
301
    CPUState *saved_env;
302
    saved_env = env;
303
    env = env1;
304
    set_cwp(new_cwp);
305
    env = saved_env;
306
}
307

    
308
void do_interrupt(int intno)
309
{
310
    int cwp;
311

    
312
#ifdef DEBUG_PCALL
313
    if (loglevel & CPU_LOG_INT) {
314
        static int count;
315
        fprintf(logfile, "%6d: v=%02x pc=%08x npc=%08x SP=%08x\n",
316
                count, intno,
317
                env->pc,
318
                env->npc, env->regwptr[6]);
319
#if 1
320
        cpu_dump_state(env, logfile, fprintf, 0);
321
        {
322
            int i;
323
            uint8_t *ptr;
324

    
325
            fprintf(logfile, "       code=");
326
            ptr = (uint8_t *)env->pc;
327
            for(i = 0; i < 16; i++) {
328
                fprintf(logfile, " %02x", ldub(ptr + i));
329
            }
330
            fprintf(logfile, "\n");
331
        }
332
#endif
333
        count++;
334
    }
335
#endif
336
#if !defined(CONFIG_USER_ONLY) 
337
    if (env->psret == 0) {
338
        cpu_abort(cpu_single_env, "Trap 0x%02x while interrupts disabled, Error state", env->exception_index);
339
        return;
340
    }
341
#endif
342
    env->psret = 0;
343
    cwp = (env->cwp - 1) & (NWINDOWS - 1); 
344
    set_cwp(cwp);
345
    env->regwptr[9] = env->pc;
346
    env->regwptr[10] = env->npc;
347
    env->psrps = env->psrs;
348
    env->psrs = 1;
349
    env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
350
    env->pc = env->tbr;
351
    env->npc = env->pc + 4;
352
    env->exception_index = 0;
353
}
354

    
355
target_ulong mmu_probe(target_ulong address, int mmulev)
356
{
357
    target_phys_addr_t pde_ptr;
358
    uint32_t pde;
359

    
360
    /* Context base + context number */
361
    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 4);
362
    pde = ldl_phys(pde_ptr);
363

    
364
    switch (pde & PTE_ENTRYTYPE_MASK) {
365
    default:
366
    case 0: /* Invalid */
367
    case 2: /* PTE, maybe should not happen? */
368
    case 3: /* Reserved */
369
        return 0;
370
    case 1: /* L1 PDE */
371
        if (mmulev == 3)
372
            return pde;
373
        pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
374
        pde = ldl_phys(pde_ptr);
375

    
376
        switch (pde & PTE_ENTRYTYPE_MASK) {
377
        default:
378
        case 0: /* Invalid */
379
        case 3: /* Reserved */
380
            return 0;
381
        case 2: /* L1 PTE */
382
            return pde;
383
        case 1: /* L2 PDE */
384
            if (mmulev == 2)
385
                return pde;
386
            pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
387
            pde = ldl_phys(pde_ptr);
388

    
389
            switch (pde & PTE_ENTRYTYPE_MASK) {
390
            default:
391
            case 0: /* Invalid */
392
            case 3: /* Reserved */
393
                return 0;
394
            case 2: /* L2 PTE */
395
                return pde;
396
            case 1: /* L3 PDE */
397
                if (mmulev == 1)
398
                    return pde;
399
                pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
400
                pde = ldl_phys(pde_ptr);
401

    
402
                switch (pde & PTE_ENTRYTYPE_MASK) {
403
                default:
404
                case 0: /* Invalid */
405
                case 1: /* PDE, should not happen */
406
                case 3: /* Reserved */
407
                    return 0;
408
                case 2: /* L3 PTE */
409
                    return pde;
410
                }
411
            }
412
        }
413
    }
414
    return 0;
415
}
416

    
417
#ifdef DEBUG_MMU
418
void dump_mmu(void)
419
{
420
     target_ulong va, va1, va2;
421
     unsigned int n, m, o;
422
     target_phys_addr_t pde_ptr, pa;
423
    uint32_t pde;
424

    
425
    printf("MMU dump:\n");
426
    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 4);
427
    pde = ldl_phys(pde_ptr);
428
    printf("Root ptr: " TARGET_FMT_lx ", ctx: %d\n", env->mmuregs[1] << 4, env->mmuregs[2]);
429
    for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
430
        pde_ptr = mmu_probe(va, 2);
431
        if (pde_ptr) {
432
            pa = cpu_get_phys_page_debug(env, va);
433
             printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx " PDE: " TARGET_FMT_lx "\n", va, pa, pde_ptr);
434
            for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
435
                pde_ptr = mmu_probe(va1, 1);
436
                if (pde_ptr) {
437
                    pa = cpu_get_phys_page_debug(env, va1);
438
                     printf(" VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx " PDE: " TARGET_FMT_lx "\n", va1, pa, pde_ptr);
439
                    for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
440
                        pde_ptr = mmu_probe(va2, 0);
441
                        if (pde_ptr) {
442
                            pa = cpu_get_phys_page_debug(env, va2);
443
                             printf("  VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx " PTE: " TARGET_FMT_lx "\n", va2, pa, pde_ptr);
444
                        }
445
                    }
446
                }
447
            }
448
        }
449
    }
450
    printf("MMU dump ends\n");
451
}
452
#endif