Statistics
| Branch: | Revision:

root / target-ppc / helper.c @ a541f297

History | View | Annotate | Download (27.7 kB)

1
/*
2
 *  PPC emulation helpers for qemu.
3
 * 
4
 *  Copyright (c) 2003 Jocelyn Mayer
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 <sys/mman.h>
21

    
22
#include "exec.h"
23
#if defined (USE_OPEN_FIRMWARE)
24
#include <time.h>
25
#include "of.h"
26
#endif
27

    
28
//#define DEBUG_MMU
29
//#define DEBUG_BATS
30
//#define DEBUG_EXCEPTIONS
31

    
32
extern FILE *logfile, *stdout, *stderr;
33
void exit (int);
34
void abort (void);
35

    
36
void cpu_loop_exit(void)
37
{
38
    longjmp(env->jmp_env, 1);
39
}
40

    
41
void do_process_exceptions (void)
42
{
43
    cpu_loop_exit();
44
}
45

    
46
int check_exception_state (CPUState *env)
47
{
48
    int i;
49

    
50
    /* Process PPC exceptions */
51
    for (i = 1; i  < EXCP_PPC_MAX; i++) {
52
        if (env->exceptions & (1 << i)) {
53
            switch (i) {
54
            case EXCP_EXTERNAL:
55
            case EXCP_DECR:
56
                if (msr_ee == 0)
57
                    return 0;
58
                break;
59
            case EXCP_PROGRAM:
60
                if (env->errors[EXCP_PROGRAM] == EXCP_FP &&
61
                    msr_fe0 == 0 && msr_fe1 == 0)
62
                    return 0;
63
                break;
64
            default:
65
                break;
66
            }
67
            env->exception_index = i;
68
            env->error_code = env->errors[i];
69
            return 1;
70
        }
71
    }
72

    
73
    return 0;
74
}
75

    
76
/*****************************************************************************/
77
/* PPC MMU emulation */
78
int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
79
                              int is_user, int is_softmmu);
80

    
81
/* Perform BAT hit & translation */
82
static int get_bat (CPUState *env, uint32_t *real, int *prot,
83
                    uint32_t virtual, int rw, int type)
84
{
85
    uint32_t *BATlt, *BATut, *BATu, *BATl;
86
    uint32_t base, BEPIl, BEPIu, bl;
87
    int i;
88
    int ret = -1;
89

    
90
#if defined (DEBUG_BATS)
91
    if (loglevel > 0) {
92
        fprintf(logfile, "%s: %cBAT v 0x%08x\n", __func__,
93
               type == ACCESS_CODE ? 'I' : 'D', virtual);
94
    }
95
#endif
96
    switch (type) {
97
    case ACCESS_CODE:
98
        BATlt = env->IBAT[1];
99
        BATut = env->IBAT[0];
100
        break;
101
    default:
102
        BATlt = env->DBAT[1];
103
        BATut = env->DBAT[0];
104
        break;
105
    }
106
#if defined (DEBUG_BATS)
107
    if (loglevel > 0) {
108
        fprintf(logfile, "%s...: %cBAT v 0x%08x\n", __func__,
109
               type == ACCESS_CODE ? 'I' : 'D', virtual);
110
    }
111
#endif
112
    base = virtual & 0xFFFC0000;
113
    for (i = 0; i < 4; i++) {
114
        BATu = &BATut[i];
115
        BATl = &BATlt[i];
116
        BEPIu = *BATu & 0xF0000000;
117
        BEPIl = *BATu & 0x0FFE0000;
118
        bl = (*BATu & 0x00001FFC) << 15;
119
#if defined (DEBUG_BATS)
120
        if (loglevel > 0) {
121
            fprintf(logfile, "%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x\n",
122
                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
123
                    *BATu, *BATl);
124
        }
125
#endif
126
        if ((virtual & 0xF0000000) == BEPIu &&
127
            ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
128
            /* BAT matches */
129
            if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
130
                (msr_pr == 1 && (*BATu & 0x00000001))) {
131
                /* Get physical address */
132
                *real = (*BATl & 0xF0000000) |
133
                    ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
134
                    (virtual & 0x0001F000);
135
                if (*BATl & 0x00000001)
136
                    *prot = PROT_READ;
137
                if (*BATl & 0x00000002)
138
                    *prot = PROT_WRITE | PROT_READ;
139
#if defined (DEBUG_BATS)
140
                if (loglevel > 0) {
141
                    fprintf(logfile, "BAT %d match: r 0x%08x prot=%c%c\n",
142
                            i, *real, *prot & PROT_READ ? 'R' : '-',
143
                            *prot & PROT_WRITE ? 'W' : '-');
144
                }
145
#endif
146
                ret = 0;
147
                break;
148
            }
149
        }
150
    }
151
    if (ret < 0) {
152
#if defined (DEBUG_BATS)
153
        printf("no BAT match for 0x%08x:\n", virtual);
154
        for (i = 0; i < 4; i++) {
155
            BATu = &BATut[i];
156
            BATl = &BATlt[i];
157
            BEPIu = *BATu & 0xF0000000;
158
            BEPIl = *BATu & 0x0FFE0000;
159
            bl = (*BATu & 0x00001FFC) << 15;
160
            printf("%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x \n\t"
161
                   "0x%08x 0x%08x 0x%08x\n",
162
                   __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
163
                   *BATu, *BATl, BEPIu, BEPIl, bl);
164
        }
165
#endif
166
        env->spr[DAR] = virtual;
167
    }
168
    /* No hit */
169
    return ret;
170
}
171

    
172
/* PTE table lookup */
173
static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
174
                     int h, int key, int rw)
175
{
176
    uint32_t pte0, pte1, keep = 0, access = 0;
177
    int i, good = -1, store = 0;
178
    int ret = -1; /* No entry found */
179

    
180
    for (i = 0; i < 8; i++) {
181
        pte0 = ldl_raw((void *)((uint32_t)phys_ram_base + base + (i * 8)));
182
        pte1 =  ldl_raw((void *)((uint32_t)phys_ram_base + base + (i * 8) + 4));
183
#if defined (DEBUG_MMU)
184
        if (loglevel > 0) {
185
            fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
186
                    "%d %d %d 0x%08x\n", base + (i * 8), pte0, pte1,
187
                    pte0 >> 31, h, (pte0 >> 6) & 1, va);
188
        }
189
#endif
190
        /* Check validity and table match */
191
        if (pte0 & 0x80000000 && (h == ((pte0 >> 6) & 1))) {
192
            /* Check vsid & api */
193
            if ((pte0 & 0x7FFFFFBF) == va) {
194
                if (good == -1) {
195
                    good = i;
196
                    keep = pte1;
197
                } else {
198
                    /* All matches should have equal RPN, WIMG & PP */
199
                    if ((keep & 0xFFFFF07B) != (pte1 & 0xFFFFF07B)) {
200
                        if (loglevel > 0)
201
                            fprintf(logfile, "Bad RPN/WIMG/PP\n");
202
                        return -1;
203
                    }
204
                }
205
                /* Check access rights */
206
                if (key == 0) {
207
                    access = PROT_READ;
208
                    if ((pte1 & 0x00000003) != 0x3)
209
                        access |= PROT_WRITE;
210
                } else {
211
                    switch (pte1 & 0x00000003) {
212
                    case 0x0:
213
                        access = 0;
214
                        break;
215
                    case 0x1:
216
                    case 0x3:
217
                        access = PROT_READ;
218
                        break;
219
                    case 0x2:
220
                        access = PROT_READ | PROT_WRITE;
221
                        break;
222
                    }
223
                }
224
                if (ret < 0) {
225
                    if ((rw == 0 && (access & PROT_READ)) ||
226
                        (rw == 1 && (access & PROT_WRITE))) {
227
#if defined (DEBUG_MMU)
228
                        if (loglevel > 0)
229
                            fprintf(logfile, "PTE access granted !\n");
230
#endif
231
                    good = i;
232
                    keep = pte1;
233
                    ret = 0;
234
                    } else {
235
                        /* Access right violation */
236
                        ret = -2;
237
#if defined (DEBUG_MMU)
238
                        if (loglevel > 0)
239
                            fprintf(logfile, "PTE access rejected\n");
240
#endif
241
                }
242
                    *prot = access;
243
                }
244
            }
245
        }
246
    }
247
    if (good != -1) {
248
        *RPN = keep & 0xFFFFF000;
249
#if defined (DEBUG_MMU)
250
        if (loglevel > 0) {
251
            fprintf(logfile, "found PTE at addr 0x%08x prot=0x%01x ret=%d\n",
252
               *RPN, *prot, ret);
253
        }
254
#endif
255
        /* Update page flags */
256
        if (!(keep & 0x00000100)) {
257
            /* Access flag */
258
            keep |= 0x00000100;
259
            store = 1;
260
        }
261
            if (!(keep & 0x00000080)) {
262
            if (rw && ret == 0) {
263
                /* Change flag */
264
                keep |= 0x00000080;
265
                store = 1;
266
            } else {
267
                /* Force page fault for first write access */
268
                *prot &= ~PROT_WRITE;
269
            }
270
        }
271
        if (store) {
272
            stl_raw((void *)((uint32_t)phys_ram_base + base + (good * 8) + 4),
273
                    keep);
274
        }
275
    }
276

    
277
    return ret;
278
}
279

    
280
static inline uint32_t get_pgaddr (uint32_t sdr1, uint32_t hash, uint32_t mask)
281
{
282
    return (sdr1 & 0xFFFF0000) | (hash & mask);
283
}
284

    
285
/* Perform segment based translation */
286
static int get_segment (CPUState *env, uint32_t *real, int *prot,
287
                        uint32_t virtual, int rw, int type)
288
{
289
    uint32_t pg_addr, sdr, ptem, vsid, pgidx;
290
    uint32_t hash, mask;
291
    uint32_t sr;
292
    int key;
293
    int ret = -1, ret2;
294

    
295
    sr = env->sr[virtual >> 28];
296
#if defined (DEBUG_MMU)
297
    if (loglevel > 0) {
298
        fprintf(logfile, "Check segment v=0x%08x %d 0x%08x nip=0x%08x "
299
                "lr=0x%08x ir=%d dr=%d pr=%d %d t=%d\n",
300
                virtual, virtual >> 28, sr, env->nip,
301
                env->lr, msr_ir, msr_dr, msr_pr, rw, type);
302
    }
303
#endif
304
    key = (((sr & 0x20000000) && msr_pr == 1) ||
305
        ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
306
    if ((sr & 0x80000000) == 0) {
307
#if defined (DEBUG_MMU)
308
        if (loglevel > 0)
309
            fprintf(logfile, "pte segment: key=%d n=0x%08x\n",
310
                    key, sr & 0x10000000);
311
#endif
312
        /* Check if instruction fetch is allowed, if needed */
313
        if (type != ACCESS_CODE || (sr & 0x10000000) == 0) {
314
            /* Page address translation */
315
            vsid = sr & 0x00FFFFFF;
316
            pgidx = (virtual >> 12) & 0xFFFF;
317
            sdr = env->sdr1;
318
            hash = ((vsid ^ pgidx) & 0x0007FFFF) << 6;
319
            mask = ((sdr & 0x000001FF) << 16) | 0xFFC0;
320
            pg_addr = get_pgaddr(sdr, hash, mask);
321
            ptem = (vsid << 7) | (pgidx >> 10);
322
#if defined (DEBUG_MMU)
323
            if (loglevel > 0) {
324
                fprintf(logfile, "0 sdr1=0x%08x vsid=0x%06x api=0x%04x "
325
                        "hash=0x%07x pg_addr=0x%08x\n", sdr, vsid, pgidx, hash,
326
                        pg_addr);
327
            }
328
#endif
329
            /* Primary table lookup */
330
            ret = find_pte(real, prot, pg_addr, ptem, 0, key, rw);
331
            if (ret < 0) {
332
                /* Secondary table lookup */
333
                hash = (~hash) & 0x01FFFFC0;
334
                pg_addr = get_pgaddr(sdr, hash, mask);
335
#if defined (DEBUG_MMU)
336
                if (virtual != 0xEFFFFFFF && loglevel > 0) {
337
                    fprintf(logfile, "1 sdr1=0x%08x vsid=0x%06x api=0x%04x "
338
                            "hash=0x%05x pg_addr=0x%08x\n", sdr, vsid, pgidx,
339
                            hash, pg_addr);
340
                }
341
#endif
342
                ret2 = find_pte(real, prot, pg_addr, ptem, 1, key, rw);
343
                if (ret2 != -1)
344
                    ret = ret2;
345
            }
346
        } else {
347
#if defined (DEBUG_MMU)
348
            if (loglevel > 0)
349
                fprintf(logfile, "No access allowed\n");
350
#endif
351
            ret = -3;
352
        }
353
    } else {
354
#if defined (DEBUG_MMU)
355
        if (loglevel > 0)
356
            fprintf(logfile, "direct store...\n");
357
#endif
358
        /* Direct-store segment : absolutely *BUGGY* for now */
359
        switch (type) {
360
        case ACCESS_INT:
361
            /* Integer load/store : only access allowed */
362
            break;
363
        case ACCESS_CODE:
364
            /* No code fetch is allowed in direct-store areas */
365
            return -4;
366
        case ACCESS_FLOAT:
367
            /* Floating point load/store */
368
            return -4;
369
        case ACCESS_RES:
370
            /* lwarx, ldarx or srwcx. */
371
            return -4;
372
        case ACCESS_CACHE:
373
            /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
374
            /* Should make the instruction do no-op.
375
             * As it already do no-op, it's quite easy :-)
376
             */
377
            *real = virtual;
378
            return 0;
379
        case ACCESS_EXT:
380
            /* eciwx or ecowx */
381
            return -4;
382
        default:
383
            if (logfile) {
384
                fprintf(logfile, "ERROR: instruction should not need "
385
                        "address translation\n");
386
            }
387
            printf("ERROR: instruction should not need "
388
                   "address translation\n");
389
            return -4;
390
        }
391
        if ((rw == 1 || key != 1) && (rw == 0 || key != 0)) {
392
            *real = virtual;
393
            ret = 2;
394
        } else {
395
            ret = -2;
396
        }
397
    }
398

    
399
    return ret;
400
}
401

    
402
int get_physical_address (CPUState *env, uint32_t *physical, int *prot,
403
                          uint32_t address, int rw, int access_type)
404
{
405
    int ret;
406

    
407
    if (loglevel > 0) {
408
        fprintf(logfile, "%s\n", __func__);
409
    }
410
    
411
    if ((access_type == ACCESS_CODE && msr_ir == 0) || msr_dr == 0) {
412
        /* No address translation */
413
        *physical = address & ~0xFFF;
414
        *prot = PROT_READ | PROT_WRITE;
415
        ret = 0;
416
    } else {
417
        /* Try to find a BAT */
418
        ret = get_bat(env, physical, prot, address, rw, access_type);
419
        if (ret < 0) {
420
            /* We didn't match any BAT entry */
421
            ret = get_segment(env, physical, prot, address, rw, access_type);
422
        }
423
    }
424
    if (loglevel > 0) {
425
        fprintf(logfile, "%s address %08x => %08x\n",
426
                __func__, address, *physical);
427
    }
428
    
429
    return ret;
430
}
431

    
432
#if defined(CONFIG_USER_ONLY) 
433
target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
434
{
435
    return addr;
436
}
437
#else
438
target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
439
{
440
    uint32_t phys_addr;
441
    int prot;
442

    
443
    if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
444
        return -1;
445
    return phys_addr;
446
}
447
#endif
448

    
449
#if !defined(CONFIG_USER_ONLY) 
450

    
451
#define MMUSUFFIX _mmu
452
#define GETPC() (__builtin_return_address(0))
453

    
454
#define SHIFT 0
455
#include "softmmu_template.h"
456

    
457
#define SHIFT 1
458
#include "softmmu_template.h"
459

    
460
#define SHIFT 2
461
#include "softmmu_template.h"
462

    
463
#define SHIFT 3
464
#include "softmmu_template.h"
465

    
466
/* try to fill the TLB and return an exception if error. If retaddr is
467
   NULL, it means that the function was called in C code (i.e. not
468
   from generated code or from helper.c) */
469
/* XXX: fix it to restore all registers */
470
void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
471
{
472
    TranslationBlock *tb;
473
    CPUState *saved_env;
474
    unsigned long pc;
475
    int ret;
476

    
477
    /* XXX: hack to restore env in all cases, even if not called from
478
       generated code */
479
    saved_env = env;
480
    env = cpu_single_env;
481
    {
482
        unsigned long tlb_addrr, tlb_addrw;
483
        int index;
484
        index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
485
        tlb_addrr = env->tlb_read[is_user][index].address;
486
        tlb_addrw = env->tlb_write[is_user][index].address;
487
#if 0
488
        printf("%s 1 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
489
               "(0x%08lx 0x%08lx)\n", __func__, env,
490
               &env->tlb_read[is_user][index], index, addr,
491
               tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
492
               tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
493
#endif
494
    }
495
    ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, is_user, 1);
496
    if (ret) {
497
        if (retaddr) {
498
            /* now we have a real cpu fault */
499
            pc = (unsigned long)retaddr;
500
            tb = tb_find_pc(pc);
501
            if (tb) {
502
                /* the PC is inside the translated code. It means that we have
503
                   a virtual CPU fault */
504
                cpu_restore_state(tb, env, pc, NULL);
505
            }
506
        }
507
        do_queue_exception_err(env->exception_index, env->error_code);
508
        do_process_exceptions();
509
    }
510
    {
511
        unsigned long tlb_addrr, tlb_addrw;
512
        int index;
513
        index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
514
        tlb_addrr = env->tlb_read[is_user][index].address;
515
        tlb_addrw = env->tlb_write[is_user][index].address;
516
#if 0
517
        printf("%s 2 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
518
               "(0x%08lx 0x%08lx)\n", __func__, env,
519
               &env->tlb_read[is_user][index], index, addr,
520
               tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
521
               tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
522
#endif
523
    }
524
    env = saved_env;
525
}
526

    
527
void cpu_ppc_init_mmu(CPUState *env)
528
{
529
    /* Nothing to do: all translation are disabled */
530
}
531
#endif
532

    
533
/* Perform address translation */
534
int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
535
                              int is_user, int is_softmmu)
536
{
537
    uint32_t physical;
538
    int prot;
539
    int exception = 0, error_code = 0;
540
    int access_type;
541
    int ret = 0;
542

    
543
//    printf("%s 0\n", __func__);
544
    access_type = env->access_type;
545
    if (env->user_mode_only) {
546
        /* user mode only emulation */
547
        ret = -1;
548
        goto do_fault;
549
    }
550
    /* NASTY BUG workaround */
551
    if (access_type == ACCESS_CODE && rw) {
552
        //        printf("%s: ERROR WRITE CODE ACCESS\n", __func__);
553
        access_type = ACCESS_INT;
554
    }
555
    ret = get_physical_address(env, &physical, &prot,
556
                               address, rw, access_type);
557
    if (ret == 0) {
558
        ret = tlb_set_page(env, address & ~0xFFF, physical, prot,
559
                           is_user, is_softmmu);
560
    } else if (ret < 0) {
561
    do_fault:
562
#if defined (DEBUG_MMU)
563
        if (loglevel > 0)
564
            cpu_ppc_dump_state(env, logfile, 0);
565
#endif
566
        if (access_type == ACCESS_CODE) {
567
            exception = EXCP_ISI;
568
            switch (ret) {
569
            case -1:
570
                /* No matches in page tables */
571
                error_code = EXCP_ISI_TRANSLATE;
572
                break;
573
            case -2:
574
                /* Access rights violation */
575
                error_code = EXCP_ISI_PROT;
576
                break;
577
            case -3:
578
                /* No execute protection violation */
579
                error_code = EXCP_ISI_NOEXEC;
580
                break;
581
            case -4:
582
                /* Direct store exception */
583
                /* No code fetch is allowed in direct-store areas */
584
                error_code = EXCP_ISI_DIRECT;
585
                break;
586
            }
587
        } else {
588
            exception = EXCP_DSI;
589
            switch (ret) {
590
            case -1:
591
                /* No matches in page tables */
592
                error_code = EXCP_DSI_TRANSLATE;
593
                break;
594
            case -2:
595
                /* Access rights violation */
596
                error_code = EXCP_DSI_PROT;
597
                break;
598
            case -4:
599
                /* Direct store exception */
600
                switch (access_type) {
601
                case ACCESS_FLOAT:
602
                    /* Floating point load/store */
603
                    exception = EXCP_ALIGN;
604
                    error_code = EXCP_ALIGN_FP;
605
                    break;
606
                case ACCESS_RES:
607
                    /* lwarx, ldarx or srwcx. */
608
                    exception = EXCP_DSI;
609
                    error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT;
610
                    break;
611
                case ACCESS_EXT:
612
                    /* eciwx or ecowx */
613
                    exception = EXCP_DSI;
614
                    error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT |
615
                        EXCP_DSI_ECXW;
616
                    break;
617
                default:
618
                    printf("DSI: invalid exception (%d)\n", ret);
619
                    exception = EXCP_PROGRAM;
620
                    error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
621
                    break;
622
                }
623
            }
624
            if (rw)
625
                error_code |= EXCP_DSI_STORE;
626
            /* Store fault address */
627
            env->spr[DAR] = address;
628
        }
629
#if 0
630
        printf("%s: set exception to %d %02x\n",
631
               __func__, exception, error_code);
632
#endif
633
        env->exception_index = exception;
634
        env->error_code = error_code;
635
        ret = 1;
636
    }
637

    
638
    return ret;
639
}
640

    
641
uint32_t _load_xer (CPUState *env)
642
{
643
    return (xer_so << XER_SO) |
644
        (xer_ov << XER_OV) |
645
        (xer_ca << XER_CA) |
646
        (xer_bc << XER_BC);
647
}
648

    
649
void _store_xer (CPUState *env, uint32_t value)
650
{
651
    xer_so = (value >> XER_SO) & 0x01;
652
    xer_ov = (value >> XER_OV) & 0x01;
653
    xer_ca = (value >> XER_CA) & 0x01;
654
    xer_bc = (value >> XER_BC) & 0x1f;
655
}
656

    
657
uint32_t _load_msr (CPUState *env)
658
{
659
    return (msr_pow << MSR_POW) |
660
        (msr_ile << MSR_ILE) |
661
        (msr_ee << MSR_EE) |
662
        (msr_pr << MSR_PR) |
663
        (msr_fp << MSR_FP) |
664
        (msr_me << MSR_ME) |
665
        (msr_fe0 << MSR_FE0) |
666
        (msr_se << MSR_SE) |
667
        (msr_be << MSR_BE) |
668
        (msr_fe1 << MSR_FE1) |
669
        (msr_ip << MSR_IP) |
670
        (msr_ir << MSR_IR) |
671
        (msr_dr << MSR_DR) |
672
        (msr_ri << MSR_RI) |
673
        (msr_le << MSR_LE);
674
}
675

    
676
void _store_msr (CPUState *env, uint32_t value)
677
{
678
    if (((T0 >> MSR_IR) & 0x01) != msr_ir ||
679
        ((T0 >> MSR_DR) & 0x01) != msr_dr) {
680
        /* Flush all tlb when changing translation mode or privilege level */
681
        do_tlbia();
682
    }
683
    msr_pow = (value >> MSR_POW) & 0x03;
684
    msr_ile = (value >> MSR_ILE) & 0x01;
685
    msr_ee = (value >> MSR_EE) & 0x01;
686
    msr_pr = (value >> MSR_PR) & 0x01;
687
    msr_fp = (value >> MSR_FP) & 0x01;
688
    msr_me = (value >> MSR_ME) & 0x01;
689
    msr_fe0 = (value >> MSR_FE0) & 0x01;
690
    msr_se = (value >> MSR_SE) & 0x01;
691
    msr_be = (value >> MSR_BE) & 0x01;
692
    msr_fe1 = (value >> MSR_FE1) & 0x01;
693
    msr_ip = (value >> MSR_IP) & 0x01;
694
    msr_ir = (value >> MSR_IR) & 0x01;
695
    msr_dr = (value >> MSR_DR) & 0x01;
696
    msr_ri = (value >> MSR_RI) & 0x01;
697
    msr_le = (value >> MSR_LE) & 0x01;
698
}
699

    
700
void do_interrupt (CPUState *env)
701
{
702
#if defined (CONFIG_USER_ONLY)
703
    env->exception_index |= 0x100;
704
#else
705
    uint32_t msr;
706
    int excp = env->exception_index;
707

    
708
    /* Dequeue PPC exceptions */
709
    if (excp < EXCP_PPC_MAX)
710
        env->exceptions &= ~(1 << excp);
711
    msr = _load_msr(env);
712
#if defined (DEBUG_EXCEPTIONS)
713
    if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1) 
714
    {
715
        if (loglevel > 0) {
716
            fprintf(logfile, "Raise exception at 0x%08x => 0x%08x (%02x)\n",
717
                    env->nip, excp << 8, env->error_code);
718
    }
719
        if (loglevel > 0)
720
            cpu_ppc_dump_state(env, logfile, 0);
721
    }
722
#endif
723
    /* Generate informations in save/restore registers */
724
    switch (excp) {
725
    case EXCP_OFCALL:
726
#if defined (USE_OPEN_FIRMWARE)
727
        env->gpr[3] = OF_client_entry((void *)env->gpr[3]);
728
#endif
729
        return;
730
    case EXCP_RTASCALL:
731
#if defined (USE_OPEN_FIRMWARE)
732
        printf("RTAS call !\n");
733
        env->gpr[3] = RTAS_entry((void *)env->gpr[3]);
734
        printf("RTAS call done\n");
735
#endif
736
        return;
737
    case EXCP_NONE:
738
        /* Do nothing */
739
#if defined (DEBUG_EXCEPTIONS)
740
        printf("%s: escape EXCP_NONE\n", __func__);
741
#endif
742
        return;
743
    case EXCP_RESET:
744
        if (msr_ip)
745
            excp += 0xFFC00;
746
        goto store_next;
747
    case EXCP_MACHINE_CHECK:
748
        if (msr_me == 0) {
749
            printf("Machine check exception while not allowed !\n");
750
            if (loglevel) {
751
                fprintf(logfile,
752
                        "Machine check exception while not allowed !\n");
753
        }
754
            abort();
755
    }
756
        msr_me = 0;
757
        break;
758
    case EXCP_DSI:
759
        /* Store exception cause */
760
        /* data location address has been stored
761
         * when the fault has been detected
762
     */
763
        msr &= ~0xFFFF0000;
764
        env->spr[DSISR] = 0;
765
        if (env->error_code &  EXCP_DSI_TRANSLATE)
766
            env->spr[DSISR] |= 0x40000000;
767
        else if (env->error_code & EXCP_DSI_PROT)
768
            env->spr[DSISR] |= 0x08000000;
769
        else if (env->error_code & EXCP_DSI_NOTSUP) {
770
            env->spr[DSISR] |= 0x80000000;
771
            if (env->error_code & EXCP_DSI_DIRECT)
772
                env->spr[DSISR] |= 0x04000000;
773
        }
774
        if (env->error_code & EXCP_DSI_STORE)
775
            env->spr[DSISR] |= 0x02000000;
776
        if ((env->error_code & 0xF) == EXCP_DSI_DABR)
777
            env->spr[DSISR] |= 0x00400000;
778
        if (env->error_code & EXCP_DSI_ECXW)
779
            env->spr[DSISR] |= 0x00100000;
780
#if defined (DEBUG_EXCEPTIONS)
781
        if (loglevel) {
782
            fprintf(logfile, "DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
783
                    env->spr[DSISR], env->spr[DAR]);
784
        } else {
785
            printf("DSI exception: DSISR=0x%08x, DAR=0x%08x nip=0x%08x\n",
786
                   env->spr[DSISR], env->spr[DAR], env->nip);
787
        }
788
#endif
789
        goto store_next;
790
    case EXCP_ISI:
791
        /* Store exception cause */
792
        msr &= ~0xFFFF0000;
793
        if (env->error_code == EXCP_ISI_TRANSLATE)
794
            msr |= 0x40000000;
795
        else if (env->error_code == EXCP_ISI_NOEXEC ||
796
                 env->error_code == EXCP_ISI_GUARD ||
797
                 env->error_code == EXCP_ISI_DIRECT)
798
            msr |= 0x10000000;
799
        else
800
            msr |= 0x08000000;
801
#if defined (DEBUG_EXCEPTIONS)
802
        if (loglevel) {
803
            fprintf(logfile, "ISI exception: msr=0x%08x, nip=0x%08x\n",
804
                    msr, env->nip);
805
        } else {
806
            printf("ISI exception: msr=0x%08x, nip=0x%08x tbl:0x%08x\n",
807
                   msr, env->nip, env->spr[V_TBL]);
808
        }
809
#endif
810
        goto store_next;
811
    case EXCP_EXTERNAL:
812
        if (msr_ee == 0) {
813
#if defined (DEBUG_EXCEPTIONS)
814
            if (loglevel > 0) {
815
                fprintf(logfile, "Skipping hardware interrupt\n");
816
    }
817
#endif
818
            /* Requeue it */
819
            do_queue_exception(EXCP_EXTERNAL);
820
            return;
821
            }
822
        goto store_next;
823
    case EXCP_ALIGN:
824
        /* Store exception cause */
825
        /* Get rS/rD and rA from faulting opcode */
826
        env->spr[DSISR] |=
827
            (ldl_code((void *)(env->nip - 4)) & 0x03FF0000) >> 16;
828
        /* data location address has been stored
829
         * when the fault has been detected
830
         */
831
        goto store_current;
832
    case EXCP_PROGRAM:
833
        msr &= ~0xFFFF0000;
834
        switch (env->error_code & ~0xF) {
835
        case EXCP_FP:
836
            if (msr_fe0 == 0 && msr_fe1 == 0) {
837
#if defined (DEBUG_EXCEPTIONS)
838
                printf("Ignore floating point exception\n");
839
#endif
840
                return;
841
        }
842
            msr |= 0x00100000;
843
            /* Set FX */
844
            env->fpscr[7] |= 0x8;
845
            /* Finally, update FEX */
846
            if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
847
                ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
848
                env->fpscr[7] |= 0x4;
849
        break;
850
        case EXCP_INVAL:
851
            printf("Invalid instruction at 0x%08x\n", env->nip);
852
            msr |= 0x00080000;
853
        break;
854
        case EXCP_PRIV:
855
            msr |= 0x00040000;
856
        break;
857
        case EXCP_TRAP:
858
            msr |= 0x00020000;
859
            break;
860
        default:
861
            /* Should never occur */
862
        break;
863
    }
864
        msr |= 0x00010000;
865
        goto store_current;
866
    case EXCP_NO_FP:
867
        goto store_current;
868
    case EXCP_DECR:
869
        if (msr_ee == 0) {
870
            /* Requeue it */
871
            do_queue_exception(EXCP_DECR);
872
            return;
873
        }
874
        goto store_next;
875
    case EXCP_SYSCALL:
876
#if defined (DEBUG_EXCEPTIONS)
877
        if (msr_pr) {
878
            if (loglevel) {
879
                fprintf(logfile, "syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n",
880
                        env->gpr[0], env->gpr[3], env->gpr[4],
881
                        env->gpr[5], env->gpr[6]);
882
            } else {
883
                printf("syscall %d from 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
884
                       env->gpr[0], env->nip, env->gpr[3], env->gpr[4],
885
                       env->gpr[5], env->gpr[6]);
886
            }
887
        }
888
#endif
889
        goto store_next;
890
    case EXCP_TRACE:
891
        goto store_next;
892
    case EXCP_FP_ASSIST:
893
        goto store_next;
894
    case EXCP_MTMSR:
895
        /* Nothing to do */
896
        return;
897
    case EXCP_BRANCH:
898
        /* Nothing to do */
899
        return;
900
    case EXCP_RFI:
901
        /* Restore user-mode state */
902
        tb_flush(env);
903
#if defined (DEBUG_EXCEPTIONS)
904
        if (msr_pr == 1)
905
            printf("Return from exception => 0x%08x\n", (uint32_t)env->nip);
906
#endif
907
        return;
908
    store_current:
909
        /* SRR0 is set to current instruction */
910
        env->spr[SRR0] = (uint32_t)env->nip - 4;
911
        break;
912
    store_next:
913
        /* SRR0 is set to next instruction */
914
        env->spr[SRR0] = (uint32_t)env->nip;
915
        break;
916
    }
917
    env->spr[SRR1] = msr;
918
    /* reload MSR with correct bits */
919
    msr_pow = 0;
920
    msr_ee = 0;
921
    msr_pr = 0;
922
    msr_fp = 0;
923
    msr_fe0 = 0;
924
    msr_se = 0;
925
    msr_be = 0;
926
    msr_fe1 = 0;
927
    msr_ir = 0;
928
    msr_dr = 0;
929
    msr_ri = 0;
930
    msr_le = msr_ile;
931
    /* Jump to handler */
932
    env->nip = excp << 8;
933
    env->exception_index = EXCP_NONE;
934
    /* Invalidate all TLB as we may have changed translation mode */
935
    do_tlbia();
936
    /* ensure that no TB jump will be modified as
937
       the program flow was changed */
938
#ifdef __sparc__
939
    tmp_T0 = 0;
940
#else
941
    T0 = 0;
942
#endif
943
#endif
944
}