Statistics
| Branch: | Revision:

root / target-ppc / helper.c @ d9bce9d9

History | View | Annotate | Download (54.1 kB)

1
/*
2
 *  PowerPC emulation helpers for qemu.
3
 * 
4
 *  Copyright (c) 2003-2007 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 <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

    
31
//#define DEBUG_MMU
32
//#define DEBUG_BATS
33
//#define DEBUG_SOFTWARE_TLB
34
//#define DEBUG_EXCEPTIONS
35
//#define FLUSH_ALL_TLBS
36

    
37
/*****************************************************************************/
38
/* PowerPC MMU emulation */
39

    
40
#if defined(CONFIG_USER_ONLY)
41
int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
42
                              int is_user, int is_softmmu)
43
{
44
    int exception, error_code;
45

    
46
    if (rw == 2) {
47
        exception = EXCP_ISI;
48
        error_code = 0;
49
    } else {
50
        exception = EXCP_DSI;
51
        error_code = 0;
52
        if (rw)
53
            error_code |= 0x02000000;
54
        env->spr[SPR_DAR] = address;
55
        env->spr[SPR_DSISR] = error_code;
56
    }
57
    env->exception_index = exception;
58
    env->error_code = error_code;
59

    
60
    return 1;
61
}
62

    
63
target_ulong cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
64
{
65
    return addr;
66
}
67
#else
68
/* Common routines used by software and hardware TLBs emulation */
69
static inline int pte_is_valid (target_ulong pte0)
70
{
71
    return pte0 & 0x80000000 ? 1 : 0;
72
}
73

    
74
static inline void pte_invalidate (target_ulong *pte0)
75
{
76
    *pte0 &= ~0x80000000;
77
}
78

    
79
#define PTE_PTEM_MASK 0x7FFFFFBF
80
#define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
81

    
82
static int pte_check (mmu_ctx_t *ctx,
83
                      target_ulong pte0, target_ulong pte1, int h, int rw)
84
{
85
    int access, ret;
86

    
87
    access = 0;
88
    ret = -1;
89
    /* Check validity and table match */
90
    if (pte_is_valid(pte0) && (h == ((pte0 >> 6) & 1))) {
91
        /* Check vsid & api */
92
        if ((pte0 & PTE_PTEM_MASK) == ctx->ptem) {
93
            if (ctx->raddr != (target_ulong)-1) {
94
                /* all matches should have equal RPN, WIMG & PP */
95
                if ((ctx->raddr & PTE_CHECK_MASK) != (pte1 & PTE_CHECK_MASK)) {
96
                    if (loglevel > 0)
97
                        fprintf(logfile, "Bad RPN/WIMG/PP\n");
98
                    return -3;
99
                }
100
            }
101
            /* Compute access rights */
102
            if (ctx->key == 0) {
103
                access = PAGE_READ;
104
                if ((pte1 & 0x00000003) != 0x3)
105
                    access |= PAGE_WRITE;
106
            } else {
107
                switch (pte1 & 0x00000003) {
108
                case 0x0:
109
                    access = 0;
110
                    break;
111
                case 0x1:
112
                case 0x3:
113
                    access = PAGE_READ;
114
                    break;
115
                case 0x2:
116
                    access = PAGE_READ | PAGE_WRITE;
117
                    break;
118
                }
119
            }
120
            /* Keep the matching PTE informations */
121
            ctx->raddr = pte1;
122
            ctx->prot = access;
123
            if ((rw == 0 && (access & PAGE_READ)) ||
124
                (rw == 1 && (access & PAGE_WRITE))) {
125
                /* Access granted */
126
#if defined (DEBUG_MMU)
127
                if (loglevel > 0)
128
                    fprintf(logfile, "PTE access granted !\n");
129
#endif
130
                ret = 0;
131
            } else {
132
                /* Access right violation */
133
#if defined (DEBUG_MMU)
134
                if (loglevel > 0)
135
                    fprintf(logfile, "PTE access rejected\n");
136
#endif
137
                ret = -2;
138
            }
139
        }
140
    }
141

    
142
    return ret;
143
}
144

    
145
static int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
146
                             int ret, int rw)
147
{
148
    int store = 0;
149

    
150
    /* Update page flags */
151
    if (!(*pte1p & 0x00000100)) {
152
        /* Update accessed flag */
153
        *pte1p |= 0x00000100;
154
        store = 1;
155
    }
156
    if (!(*pte1p & 0x00000080)) {
157
        if (rw == 1 && ret == 0) {
158
            /* Update changed flag */
159
            *pte1p |= 0x00000080;
160
            store = 1;
161
        } else {
162
            /* Force page fault for first write access */
163
            ctx->prot &= ~PAGE_WRITE;
164
        }
165
    }
166

    
167
    return store;
168
}
169

    
170
/* Software driven TLB helpers */
171
static int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
172
                              int way, int is_code)
173
{
174
    int nr;
175

    
176
    /* Select TLB num in a way from address */
177
    nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
178
    /* Select TLB way */
179
    nr += env->tlb_per_way * way;
180
    /* 6xx have separate TLBs for instructions and data */
181
    if (is_code && env->id_tlbs == 1)
182
        nr += env->nb_tlb;
183

    
184
    return nr;
185
}
186

    
187
void ppc6xx_tlb_invalidate_all (CPUState *env)
188
{
189
    ppc_tlb_t *tlb;
190
    int nr, max;
191

    
192
#if defined (DEBUG_SOFTWARE_TLB) && 0
193
    if (loglevel != 0) {
194
        fprintf(logfile, "Invalidate all TLBs\n");
195
    }
196
#endif
197
    /* Invalidate all defined software TLB */
198
    max = env->nb_tlb;
199
    if (env->id_tlbs == 1)
200
        max *= 2;
201
    for (nr = 0; nr < max; nr++) {
202
        tlb = &env->tlb[nr];
203
#if !defined(FLUSH_ALL_TLBS)
204
        tlb_flush_page(env, tlb->EPN);
205
#endif
206
        pte_invalidate(&tlb->pte0);
207
    }
208
#if defined(FLUSH_ALL_TLBS)
209
    tlb_flush(env, 1);
210
#endif
211
}
212

    
213
static inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
214
                                                 target_ulong eaddr,
215
                                                 int is_code, int match_epn)
216
{
217
    ppc_tlb_t *tlb;
218
    int way, nr;
219

    
220
#if !defined(FLUSH_ALL_TLBS)
221
    /* Invalidate ITLB + DTLB, all ways */
222
    for (way = 0; way < env->nb_ways; way++) {
223
        nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
224
        tlb = &env->tlb[nr];
225
        if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
226
#if defined (DEBUG_SOFTWARE_TLB)
227
            if (loglevel != 0) {
228
                fprintf(logfile, "TLB invalidate %d/%d %08x\n",
229
                        nr, env->nb_tlb, eaddr);
230
            }
231
#endif
232
            pte_invalidate(&tlb->pte0);
233
            tlb_flush_page(env, tlb->EPN);
234
        }
235
    }
236
#else
237
    /* XXX: PowerPC specification say this is valid as well */
238
    ppc6xx_tlb_invalidate_all(env);
239
#endif
240
}
241

    
242
void ppc6xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
243
                                 int is_code)
244
{
245
    __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
246
}
247

    
248
void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
249
                       target_ulong pte0, target_ulong pte1)
250
{
251
    ppc_tlb_t *tlb;
252
    int nr;
253

    
254
    nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
255
    tlb = &env->tlb[nr];
256
#if defined (DEBUG_SOFTWARE_TLB)
257
    if (loglevel != 0) {
258
        fprintf(logfile, "Set TLB %d/%d EPN %08lx PTE0 %08lx PTE1 %08lx\n",
259
                nr, env->nb_tlb, (unsigned long)EPN,
260
                (unsigned long)pte0, (unsigned long)pte1);
261
    }
262
#endif
263
    /* Invalidate any pending reference in Qemu for this virtual address */
264
    __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
265
    tlb->pte0 = pte0;
266
    tlb->pte1 = pte1;
267
    tlb->EPN = EPN;
268
    tlb->PID = 0;
269
    tlb->size = 1;
270
    /* Store last way for LRU mechanism */
271
    env->last_way = way;
272
}
273

    
274
static int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
275
                             target_ulong eaddr, int rw, int access_type)
276
{
277
    ppc_tlb_t *tlb;
278
    int nr, best, way;
279
    int ret;
280

    
281
    best = -1;
282
    ret = -1; /* No TLB found */
283
    for (way = 0; way < env->nb_ways; way++) {
284
        nr = ppc6xx_tlb_getnum(env, eaddr, way,
285
                               access_type == ACCESS_CODE ? 1 : 0);
286
        tlb = &env->tlb[nr];
287
        /* This test "emulates" the PTE index match for hardware TLBs */
288
        if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
289
#if defined (DEBUG_SOFTWARE_TLB)
290
            if (loglevel != 0) {
291
                fprintf(logfile, "TLB %d/%d %s [%08x %08x] <> %08x\n",
292
                        nr, env->nb_tlb,
293
                        pte_is_valid(tlb->pte0) ? "valid" : "inval",
294
                        tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
295
            }
296
#endif
297
            continue;
298
        }
299
#if defined (DEBUG_SOFTWARE_TLB)
300
        if (loglevel != 0) {
301
            fprintf(logfile, "TLB %d/%d %s %08x <> %08x %08x %c %c\n",
302
                    nr, env->nb_tlb,
303
                    pte_is_valid(tlb->pte0) ? "valid" : "inval",
304
                    tlb->EPN, eaddr, tlb->pte1,
305
                    rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
306
        }
307
#endif
308
        switch (pte_check(ctx, tlb->pte0, tlb->pte1, 0, rw)) {
309
        case -3:
310
            /* TLB inconsistency */
311
            return -1;
312
        case -2:
313
            /* Access violation */
314
            ret = -2;
315
            best = nr;
316
            break;
317
        case -1:
318
        default:
319
            /* No match */
320
            break;
321
        case 0:
322
            /* access granted */
323
            /* XXX: we should go on looping to check all TLBs consistency
324
             *      but we can speed-up the whole thing as the
325
             *      result would be undefined if TLBs are not consistent.
326
             */
327
            ret = 0;
328
            best = nr;
329
            goto done;
330
        }
331
    }
332
    if (best != -1) {
333
    done:
334
#if defined (DEBUG_SOFTWARE_TLB)
335
        if (loglevel > 0) {
336
            fprintf(logfile, "found TLB at addr 0x%08lx prot=0x%01x ret=%d\n",
337
                    ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
338
        }
339
#endif
340
        /* Update page flags */
341
        pte_update_flags(ctx, &env->tlb[best].pte1, ret, rw);
342
    }
343

    
344
    return ret;
345
}
346

    
347
/* Perform BAT hit & translation */
348
static int get_bat (CPUState *env, mmu_ctx_t *ctx,
349
                    target_ulong virtual, int rw, int type)
350
{
351
    target_ulong *BATlt, *BATut, *BATu, *BATl;
352
    target_ulong base, BEPIl, BEPIu, bl;
353
    int i;
354
    int ret = -1;
355

    
356
#if defined (DEBUG_BATS)
357
    if (loglevel > 0) {
358
        fprintf(logfile, "%s: %cBAT v 0x%08x\n", __func__,
359
                type == ACCESS_CODE ? 'I' : 'D', virtual);
360
    }
361
#endif
362
    switch (type) {
363
    case ACCESS_CODE:
364
        BATlt = env->IBAT[1];
365
        BATut = env->IBAT[0];
366
        break;
367
    default:
368
        BATlt = env->DBAT[1];
369
        BATut = env->DBAT[0];
370
        break;
371
    }
372
#if defined (DEBUG_BATS)
373
    if (loglevel > 0) {
374
        fprintf(logfile, "%s...: %cBAT v 0x%08x\n", __func__,
375
                type == ACCESS_CODE ? 'I' : 'D', virtual);
376
    }
377
#endif
378
    base = virtual & 0xFFFC0000;
379
    for (i = 0; i < 4; i++) {
380
        BATu = &BATut[i];
381
        BATl = &BATlt[i];
382
        BEPIu = *BATu & 0xF0000000;
383
        BEPIl = *BATu & 0x0FFE0000;
384
        bl = (*BATu & 0x00001FFC) << 15;
385
#if defined (DEBUG_BATS)
386
        if (loglevel > 0) {
387
            fprintf(logfile, "%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x\n",
388
                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
389
                    *BATu, *BATl);
390
        }
391
#endif
392
        if ((virtual & 0xF0000000) == BEPIu &&
393
            ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
394
            /* BAT matches */
395
            if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
396
                (msr_pr == 1 && (*BATu & 0x00000001))) {
397
                /* Get physical address */
398
                ctx->raddr = (*BATl & 0xF0000000) |
399
                    ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
400
                    (virtual & 0x0001F000);
401
                if (*BATl & 0x00000001)
402
                    ctx->prot = PAGE_READ;
403
                if (*BATl & 0x00000002)
404
                    ctx->prot = PAGE_WRITE | PAGE_READ;
405
#if defined (DEBUG_BATS)
406
                if (loglevel > 0) {
407
                    fprintf(logfile, "BAT %d match: r 0x%08x prot=%c%c\n",
408
                            i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
409
                            ctx->prot & PAGE_WRITE ? 'W' : '-');
410
                }
411
#endif
412
                ret = 0;
413
                break;
414
            }
415
        }
416
    }
417
    if (ret < 0) {
418
#if defined (DEBUG_BATS)
419
        printf("no BAT match for 0x%08x:\n", virtual);
420
        for (i = 0; i < 4; i++) {
421
            BATu = &BATut[i];
422
            BATl = &BATlt[i];
423
            BEPIu = *BATu & 0xF0000000;
424
            BEPIl = *BATu & 0x0FFE0000;
425
            bl = (*BATu & 0x00001FFC) << 15;
426
            printf("%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x \n\t"
427
                   "0x%08x 0x%08x 0x%08x\n",
428
                   __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
429
                   *BATu, *BATl, BEPIu, BEPIl, bl);
430
        }
431
#endif
432
    }
433
    /* No hit */
434
    return ret;
435
}
436

    
437
/* PTE table lookup */
438
static int find_pte (mmu_ctx_t *ctx, int h, int rw)
439
{
440
    target_ulong base, pte0, pte1;
441
    int i, good = -1;
442
    int ret;
443

    
444
    ret = -1; /* No entry found */
445
    base = ctx->pg_addr[h];
446
    for (i = 0; i < 8; i++) {
447
        pte0 = ldl_phys(base + (i * 8));
448
        pte1 =  ldl_phys(base + (i * 8) + 4);
449
#if defined (DEBUG_MMU)
450
        if (loglevel > 0) {
451
            fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
452
                    "%d %d %d 0x%08x\n", base + (i * 8), pte0, pte1,
453
                    pte0 >> 31, h, (pte0 >> 6) & 1, ctx->ptem);
454
        }
455
#endif
456
        switch (pte_check(ctx, pte0, pte1, h, rw)) {
457
        case -3:
458
            /* PTE inconsistency */
459
            return -1;
460
        case -2:
461
            /* Access violation */
462
            ret = -2;
463
            good = i;
464
            break;
465
        case -1:
466
        default:
467
            /* No PTE match */
468
            break;
469
        case 0:
470
            /* access granted */
471
            /* XXX: we should go on looping to check all PTEs consistency
472
             *      but if we can speed-up the whole thing as the
473
             *      result would be undefined if PTEs are not consistent.
474
             */
475
            ret = 0;
476
            good = i;
477
            goto done;
478
        }
479
    }
480
    if (good != -1) {
481
    done:
482
#if defined (DEBUG_MMU)
483
        if (loglevel > 0) {
484
            fprintf(logfile, "found PTE at addr 0x%08x prot=0x%01x ret=%d\n",
485
                    ctx->raddr, ctx->prot, ret);
486
        }
487
#endif
488
        /* Update page flags */
489
        pte1 = ctx->raddr;
490
        if (pte_update_flags(ctx, &pte1, ret, rw) == 1)
491
            stl_phys_notdirty(base + (good * 8) + 4, pte1);
492
    }
493

    
494
    return ret;
495
}
496

    
497
static inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
498
                                             target_phys_addr_t hash,
499
                                             target_phys_addr_t mask)
500
{
501
    return (sdr1 & 0xFFFF0000) | (hash & mask);
502
}
503

    
504
/* Perform segment based translation */
505
static int get_segment (CPUState *env, mmu_ctx_t *ctx,
506
                        target_ulong eaddr, int rw, int type)
507
{
508
    target_phys_addr_t sdr, hash, mask;
509
    target_ulong sr, vsid, pgidx;
510
    int ret = -1, ret2;
511

    
512
    sr = env->sr[eaddr >> 28];
513
#if defined (DEBUG_MMU)
514
    if (loglevel > 0) {
515
        fprintf(logfile, "Check segment v=0x%08x %d 0x%08x nip=0x%08x "
516
                "lr=0x%08x ir=%d dr=%d pr=%d %d t=%d\n",
517
                eaddr, eaddr >> 28, sr, env->nip,
518
                env->lr, msr_ir, msr_dr, msr_pr, rw, type);
519
    }
520
#endif
521
    ctx->key = (((sr & 0x20000000) && msr_pr == 1) ||
522
                ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
523
    if ((sr & 0x80000000) == 0) {
524
#if defined (DEBUG_MMU)
525
        if (loglevel > 0) 
526
            fprintf(logfile, "pte segment: key=%d n=0x%08x\n",
527
                    ctx->key, sr & 0x10000000);
528
#endif
529
        /* Check if instruction fetch is allowed, if needed */
530
        if (type != ACCESS_CODE || (sr & 0x10000000) == 0) {
531
            /* Page address translation */
532
            pgidx = (eaddr >> TARGET_PAGE_BITS) & 0xFFFF;
533
            vsid = sr & 0x00FFFFFF;
534
            hash = ((vsid ^ pgidx) & 0x0007FFFF) << 6;
535
            /* Primary table address */
536
            sdr = env->sdr1;
537
            mask = ((sdr & 0x000001FF) << 16) | 0xFFC0;
538
            ctx->pg_addr[0] = get_pgaddr(sdr, hash, mask);
539
            /* Secondary table address */
540
            hash = (~hash) & 0x01FFFFC0;
541
            ctx->pg_addr[1] = get_pgaddr(sdr, hash, mask);
542
            ctx->ptem = (vsid << 7) | (pgidx >> 10);
543
            /* Initialize real address with an invalid value */
544
            ctx->raddr = (target_ulong)-1;
545
            if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_6xx)) {
546
                /* Software TLB search */
547
                ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
548
            } else if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_4xx)) {
549
                /* XXX: TODO */
550
            } else {
551
#if defined (DEBUG_MMU)
552
                if (loglevel > 0) {
553
                    fprintf(logfile, "0 sdr1=0x%08x vsid=0x%06x api=0x%04x "
554
                            "hash=0x%07x pg_addr=0x%08x\n", sdr, vsid, pgidx,
555
                            hash, ctx->pg_addr[0]);
556
                }
557
#endif
558
                /* Primary table lookup */
559
                ret = find_pte(ctx, 0, rw);
560
                if (ret < 0) {
561
                    /* Secondary table lookup */
562
#if defined (DEBUG_MMU)
563
                    if (eaddr != 0xEFFFFFFF && loglevel > 0) {
564
                        fprintf(logfile,
565
                                "1 sdr1=0x%08x vsid=0x%06x api=0x%04x "
566
                                "hash=0x%05x pg_addr=0x%08x\n", sdr, vsid,
567
                                pgidx, hash, ctx->pg_addr[1]);
568
                    }
569
#endif
570
                    ret2 = find_pte(ctx, 1, rw);
571
                    if (ret2 != -1)
572
                        ret = ret2;
573
                }
574
            }
575
        } else {
576
#if defined (DEBUG_MMU)
577
            if (loglevel > 0)
578
                fprintf(logfile, "No access allowed\n");
579
#endif
580
            ret = -3;
581
        }
582
    } else {
583
#if defined (DEBUG_MMU)
584
        if (loglevel > 0)
585
            fprintf(logfile, "direct store...\n");
586
#endif
587
        /* Direct-store segment : absolutely *BUGGY* for now */
588
        switch (type) {
589
        case ACCESS_INT:
590
            /* Integer load/store : only access allowed */
591
            break;
592
        case ACCESS_CODE:
593
            /* No code fetch is allowed in direct-store areas */
594
            return -4;
595
        case ACCESS_FLOAT:
596
            /* Floating point load/store */
597
            return -4;
598
        case ACCESS_RES:
599
            /* lwarx, ldarx or srwcx. */
600
            return -4;
601
        case ACCESS_CACHE:
602
            /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
603
            /* Should make the instruction do no-op.
604
             * As it already do no-op, it's quite easy :-)
605
             */
606
            ctx->raddr = eaddr;
607
            return 0;
608
        case ACCESS_EXT:
609
            /* eciwx or ecowx */
610
            return -4;
611
        default:
612
            if (logfile) {
613
                fprintf(logfile, "ERROR: instruction should not need "
614
                        "address translation\n");
615
            }
616
            printf("ERROR: instruction should not need "
617
                   "address translation\n");
618
            return -4;
619
        }
620
        if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
621
            ctx->raddr = eaddr;
622
            ret = 2;
623
        } else {
624
            ret = -2;
625
        }
626
    }
627

    
628
    return ret;
629
}
630

    
631
static int check_physical (CPUState *env, mmu_ctx_t *ctx,
632
                           target_ulong eaddr, int rw)
633
{
634
    int in_plb, ret;
635
        
636
    ctx->raddr = eaddr;
637
    ctx->prot = PAGE_READ;
638
    ret = 0;
639
    if (unlikely(msr_pe != 0 && PPC_MMU(env) == PPC_FLAGS_MMU_403)) {
640
        /* 403 family add some particular protections,
641
         * using PBL/PBU registers for accesses with no translation.
642
         */
643
        in_plb =
644
            /* Check PLB validity */
645
            (env->pb[0] < env->pb[1] &&
646
             /* and address in plb area */
647
             eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
648
            (env->pb[2] < env->pb[3] &&
649
             eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
650
        if (in_plb ^ msr_px) {
651
            /* Access in protected area */
652
            if (rw == 1) {
653
                /* Access is not allowed */
654
                ret = -2;
655
            }
656
        } else {
657
            /* Read-write access is allowed */
658
            ctx->prot |= PAGE_WRITE;
659
        }
660
    } else {
661
        ctx->prot |= PAGE_WRITE;
662
    }
663

    
664
    return ret;
665
}
666

    
667
int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
668
                          int rw, int access_type, int check_BATs)
669
{
670
    int ret;
671
#if 0
672
    if (loglevel > 0) {
673
        fprintf(logfile, "%s\n", __func__);
674
    }
675
#endif
676
    if ((access_type == ACCESS_CODE && msr_ir == 0) ||
677
        (access_type != ACCESS_CODE && msr_dr == 0)) {
678
        /* No address translation */
679
        ret = check_physical(env, ctx, eaddr, rw);
680
    } else {
681
        /* Try to find a BAT */
682
        ret = -1;
683
        if (check_BATs)
684
            ret = get_bat(env, ctx, eaddr, rw, access_type);
685
        if (ret < 0) {
686
            /* We didn't match any BAT entry */
687
            ret = get_segment(env, ctx, eaddr, rw, access_type);
688
        }
689
    }
690
#if 0
691
    if (loglevel > 0) {
692
        fprintf(logfile, "%s address %08x => %08lx\n",
693
                __func__, eaddr, ctx->raddr);
694
    }
695
#endif
696

    
697
    return ret;
698
}
699

    
700
target_ulong cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
701
{
702
    mmu_ctx_t ctx;
703

    
704
    if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT, 1) != 0))
705
        return -1;
706

    
707
    return ctx.raddr & TARGET_PAGE_MASK;
708
}
709

    
710
/* Perform address translation */
711
int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
712
                              int is_user, int is_softmmu)
713
{
714
    mmu_ctx_t ctx;
715
    int exception = 0, error_code = 0;
716
    int access_type;
717
    int ret = 0;
718

    
719
    if (rw == 2) {
720
        /* code access */
721
        rw = 0;
722
        access_type = ACCESS_CODE;
723
    } else {
724
        /* data access */
725
        /* XXX: put correct access by using cpu_restore_state()
726
           correctly */
727
        access_type = ACCESS_INT;
728
        //        access_type = env->access_type;
729
    }
730
    ret = get_physical_address(env, &ctx, address, rw, access_type, 1);
731
    if (ret == 0) {
732
        ret = tlb_set_page(env, address & TARGET_PAGE_MASK,
733
                           ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
734
                           is_user, is_softmmu);
735
    } else if (ret < 0) {
736
#if defined (DEBUG_MMU)
737
        if (loglevel > 0)
738
            cpu_dump_state(env, logfile, fprintf, 0);
739
#endif
740
        if (access_type == ACCESS_CODE) {
741
            exception = EXCP_ISI;
742
            switch (ret) {
743
            case -1:
744
                /* No matches in page tables or TLB */
745
                if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_6xx)) {
746
                    exception = EXCP_I_TLBMISS;
747
                    env->spr[SPR_IMISS] = address;
748
                    env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
749
                    error_code = 1 << 18;
750
                    goto tlb_miss;
751
                } else if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_4xx)) {
752
                    /* XXX: TODO */
753
                } else {
754
                    error_code = 0x40000000;
755
                }
756
                break;
757
            case -2:
758
                /* Access rights violation */
759
                error_code = 0x08000000;
760
                break;
761
            case -3:
762
                /* No execute protection violation */
763
                error_code = 0x10000000;
764
                break;
765
            case -4:
766
                /* Direct store exception */
767
                /* No code fetch is allowed in direct-store areas */
768
                error_code = 0x10000000;
769
                break;
770
            case -5:
771
                /* No match in segment table */
772
                exception = EXCP_ISEG;
773
                error_code = 0;
774
                break;
775
            }
776
        } else {
777
            exception = EXCP_DSI;
778
            switch (ret) {
779
            case -1:
780
                /* No matches in page tables or TLB */
781
                if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_6xx)) {
782
                    if (rw == 1) {
783
                        exception = EXCP_DS_TLBMISS;
784
                        error_code = 1 << 16;
785
                    } else {
786
                        exception = EXCP_DL_TLBMISS;
787
                        error_code = 0;
788
                    }
789
                    env->spr[SPR_DMISS] = address;
790
                    env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
791
                tlb_miss:
792
                    error_code |= ctx.key << 19;
793
                    env->spr[SPR_HASH1] = ctx.pg_addr[0];
794
                    env->spr[SPR_HASH2] = ctx.pg_addr[1];
795
                    /* Do not alter DAR nor DSISR */
796
                    goto out;
797
                } else if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_4xx)) {
798
                    /* XXX: TODO */
799
                } else {
800
                    error_code = 0x40000000;
801
                }
802
                break;
803
            case -2:
804
                /* Access rights violation */
805
                error_code = 0x08000000;
806
                break;
807
            case -4:
808
                /* Direct store exception */
809
                switch (access_type) {
810
                case ACCESS_FLOAT:
811
                    /* Floating point load/store */
812
                    exception = EXCP_ALIGN;
813
                    error_code = EXCP_ALIGN_FP;
814
                    break;
815
                case ACCESS_RES:
816
                    /* lwarx, ldarx or srwcx. */
817
                    error_code = 0x04000000;
818
                    break;
819
                case ACCESS_EXT:
820
                    /* eciwx or ecowx */
821
                    error_code = 0x04100000;
822
                    break;
823
                default:
824
                    printf("DSI: invalid exception (%d)\n", ret);
825
                    exception = EXCP_PROGRAM;
826
                    error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
827
                    break;
828
                }
829
                break;
830
            case -5:
831
                /* No match in segment table */
832
                exception = EXCP_DSEG;
833
                error_code = 0;
834
                break;
835
            }
836
            if (exception == EXCP_DSI && rw == 1)
837
                error_code |= 0x02000000;
838
            /* Store fault address */
839
            env->spr[SPR_DAR] = address;
840
            env->spr[SPR_DSISR] = error_code;
841
        }
842
    out:
843
#if 0
844
        printf("%s: set exception to %d %02x\n",
845
               __func__, exception, error_code);
846
#endif
847
        env->exception_index = exception;
848
        env->error_code = error_code;
849
        ret = 1;
850
    }
851

    
852
    return ret;
853
}
854

    
855
/*****************************************************************************/
856
/* BATs management */
857
#if !defined(FLUSH_ALL_TLBS)
858
static inline void do_invalidate_BAT (CPUPPCState *env,
859
                                      target_ulong BATu, target_ulong mask)
860
{
861
    target_ulong base, end, page;
862

    
863
    base = BATu & ~0x0001FFFF;
864
    end = base + mask + 0x00020000;
865
#if defined (DEBUG_BATS)
866
    if (loglevel != 0) {
867
        fprintf(logfile, "Flush BAT from %08x to %08x (%08x)\n",
868
                base, end, mask);
869
    }
870
#endif
871
    for (page = base; page != end; page += TARGET_PAGE_SIZE)
872
        tlb_flush_page(env, page);
873
#if defined (DEBUG_BATS)
874
    if (loglevel != 0)
875
        fprintf(logfile, "Flush done\n");
876
#endif
877
}
878
#endif
879

    
880
static inline void dump_store_bat (CPUPPCState *env, char ID, int ul, int nr,
881
                                   target_ulong value)
882
{
883
#if defined (DEBUG_BATS)
884
    if (loglevel != 0) {
885
        fprintf(logfile, "Set %cBAT%d%c to 0x%08lx (0x%08lx)\n",
886
                ID, nr, ul == 0 ? 'u' : 'l', (unsigned long)value,
887
                (unsigned long)env->nip);
888
    }
889
#endif
890
}
891

    
892
target_ulong do_load_ibatu (CPUPPCState *env, int nr)
893
{
894
    return env->IBAT[0][nr];
895
}
896

    
897
target_ulong do_load_ibatl (CPUPPCState *env, int nr)
898
{
899
    return env->IBAT[1][nr];
900
}
901

    
902
void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
903
{
904
    target_ulong mask;
905

    
906
    dump_store_bat(env, 'I', 0, nr, value);
907
    if (env->IBAT[0][nr] != value) {
908
        mask = (value << 15) & 0x0FFE0000UL;
909
#if !defined(FLUSH_ALL_TLBS)
910
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
911
#endif
912
        /* When storing valid upper BAT, mask BEPI and BRPN
913
         * and invalidate all TLBs covered by this BAT
914
         */
915
        mask = (value << 15) & 0x0FFE0000UL;
916
        env->IBAT[0][nr] = (value & 0x00001FFFUL) |
917
            (value & ~0x0001FFFFUL & ~mask);
918
        env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
919
            (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
920
#if !defined(FLUSH_ALL_TLBS)
921
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
922
#else
923
        tlb_flush(env, 1);
924
#endif
925
    }
926
}
927

    
928
void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
929
{
930
    dump_store_bat(env, 'I', 1, nr, value);
931
    env->IBAT[1][nr] = value;
932
}
933

    
934
target_ulong do_load_dbatu (CPUPPCState *env, int nr)
935
{
936
    return env->DBAT[0][nr];
937
}
938

    
939
target_ulong do_load_dbatl (CPUPPCState *env, int nr)
940
{
941
    return env->DBAT[1][nr];
942
}
943

    
944
void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
945
{
946
    target_ulong mask;
947

    
948
    dump_store_bat(env, 'D', 0, nr, value);
949
    if (env->DBAT[0][nr] != value) {
950
        /* When storing valid upper BAT, mask BEPI and BRPN
951
         * and invalidate all TLBs covered by this BAT
952
         */
953
        mask = (value << 15) & 0x0FFE0000UL;
954
#if !defined(FLUSH_ALL_TLBS)
955
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
956
#endif
957
        mask = (value << 15) & 0x0FFE0000UL;
958
        env->DBAT[0][nr] = (value & 0x00001FFFUL) |
959
            (value & ~0x0001FFFFUL & ~mask);
960
        env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
961
            (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
962
#if !defined(FLUSH_ALL_TLBS)
963
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
964
#else
965
        tlb_flush(env, 1);
966
#endif
967
    }
968
}
969

    
970
void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
971
{
972
    dump_store_bat(env, 'D', 1, nr, value);
973
    env->DBAT[1][nr] = value;
974
}
975

    
976
/*****************************************************************************/
977
/* Special registers manipulation */
978
#if defined(TARGET_PPC64)
979
target_ulong ppc_load_asr (CPUPPCState *env)
980
{
981
    return env->asr;
982
}
983

    
984
void ppc_store_asr (CPUPPCState *env, target_ulong value)
985
{
986
    if (env->asr != value) {
987
        env->asr = value;
988
        tlb_flush(env, 1);
989
    }
990
}
991
#endif
992

    
993
target_ulong do_load_sdr1 (CPUPPCState *env)
994
{
995
    return env->sdr1;
996
}
997

    
998
void do_store_sdr1 (CPUPPCState *env, target_ulong value)
999
{
1000
#if defined (DEBUG_MMU)
1001
    if (loglevel != 0) {
1002
        fprintf(logfile, "%s: 0x%08lx\n", __func__, (unsigned long)value);
1003
    }
1004
#endif
1005
    if (env->sdr1 != value) {
1006
        env->sdr1 = value;
1007
        tlb_flush(env, 1);
1008
    }
1009
}
1010

    
1011
target_ulong do_load_sr (CPUPPCState *env, int srnum)
1012
{
1013
    return env->sr[srnum];
1014
}
1015

    
1016
void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
1017
{
1018
#if defined (DEBUG_MMU)
1019
    if (loglevel != 0) {
1020
        fprintf(logfile, "%s: reg=%d 0x%08lx %08lx\n",
1021
                __func__, srnum, (unsigned long)value, env->sr[srnum]);
1022
    }
1023
#endif
1024
    if (env->sr[srnum] != value) {
1025
        env->sr[srnum] = value;
1026
#if !defined(FLUSH_ALL_TLBS) && 0
1027
        {
1028
            target_ulong page, end;
1029
            /* Invalidate 256 MB of virtual memory */
1030
            page = (16 << 20) * srnum;
1031
            end = page + (16 << 20);
1032
            for (; page != end; page += TARGET_PAGE_SIZE)
1033
                tlb_flush_page(env, page);
1034
        }
1035
#else
1036
        tlb_flush(env, 1);
1037
#endif
1038
    }
1039
}
1040
#endif /* !defined (CONFIG_USER_ONLY) */
1041

    
1042
uint32_t ppc_load_xer (CPUPPCState *env)
1043
{
1044
    return (xer_so << XER_SO) |
1045
        (xer_ov << XER_OV) |
1046
        (xer_ca << XER_CA) |
1047
        (xer_bc << XER_BC) |
1048
        (xer_cmp << XER_CMP);
1049
}
1050

    
1051
void ppc_store_xer (CPUPPCState *env, uint32_t value)
1052
{
1053
    xer_so = (value >> XER_SO) & 0x01;
1054
    xer_ov = (value >> XER_OV) & 0x01;
1055
    xer_ca = (value >> XER_CA) & 0x01;
1056
    xer_cmp = (value >> XER_CMP) & 0xFF;
1057
    xer_bc = (value >> XER_BC) & 0x7F;
1058
}
1059

    
1060
/* Swap temporary saved registers with GPRs */
1061
static inline void swap_gpr_tgpr (CPUPPCState *env)
1062
{
1063
    ppc_gpr_t tmp;
1064

    
1065
    tmp = env->gpr[0];
1066
    env->gpr[0] = env->tgpr[0];
1067
    env->tgpr[0] = tmp;
1068
    tmp = env->gpr[1];
1069
    env->gpr[1] = env->tgpr[1];
1070
    env->tgpr[1] = tmp;
1071
    tmp = env->gpr[2];
1072
    env->gpr[2] = env->tgpr[2];
1073
    env->tgpr[2] = tmp;
1074
    tmp = env->gpr[3];
1075
    env->gpr[3] = env->tgpr[3];
1076
    env->tgpr[3] = tmp;
1077
}
1078

    
1079
/* GDBstub can read and write MSR... */
1080
target_ulong do_load_msr (CPUPPCState *env)
1081
{
1082
    return
1083
#if defined (TARGET_PPC64)
1084
        ((target_ulong)msr_sf   << MSR_SF)   |
1085
        ((target_ulong)msr_isf  << MSR_ISF)  |
1086
        ((target_ulong)msr_hv   << MSR_HV)   |
1087
#endif
1088
        ((target_ulong)msr_ucle << MSR_UCLE) |
1089
        ((target_ulong)msr_vr   << MSR_VR)   | /* VR / SPE */
1090
        ((target_ulong)msr_ap   << MSR_AP)   |
1091
        ((target_ulong)msr_sa   << MSR_SA)   |
1092
        ((target_ulong)msr_key  << MSR_KEY)  |
1093
        ((target_ulong)msr_pow  << MSR_POW)  | /* POW / WE */
1094
        ((target_ulong)msr_tlb  << MSR_TLB)  | /* TLB / TGPE / CE */
1095
        ((target_ulong)msr_ile  << MSR_ILE)  |
1096
        ((target_ulong)msr_ee   << MSR_EE)   |
1097
        ((target_ulong)msr_pr   << MSR_PR)   |
1098
        ((target_ulong)msr_fp   << MSR_FP)   |
1099
        ((target_ulong)msr_me   << MSR_ME)   |
1100
        ((target_ulong)msr_fe0  << MSR_FE0)  |
1101
        ((target_ulong)msr_se   << MSR_SE)   | /* SE / DWE / UBLE */
1102
        ((target_ulong)msr_be   << MSR_BE)   | /* BE / DE */
1103
        ((target_ulong)msr_fe1  << MSR_FE1)  |
1104
        ((target_ulong)msr_al   << MSR_AL)   |
1105
        ((target_ulong)msr_ip   << MSR_IP)   |
1106
        ((target_ulong)msr_ir   << MSR_IR)   | /* IR / IS */
1107
        ((target_ulong)msr_dr   << MSR_DR)   | /* DR / DS */
1108
        ((target_ulong)msr_pe   << MSR_PE)   | /* PE / EP */
1109
        ((target_ulong)msr_px   << MSR_PX)   | /* PX / PMM */
1110
        ((target_ulong)msr_ri   << MSR_RI)   |
1111
        ((target_ulong)msr_le   << MSR_LE);
1112
}
1113

    
1114
void do_store_msr (CPUPPCState *env, target_ulong value)
1115
{
1116
    int enter_pm;
1117

    
1118
    value &= env->msr_mask;
1119
    if (((value >> MSR_IR) & 1) != msr_ir ||
1120
        ((value >> MSR_DR) & 1) != msr_dr) {
1121
        /* Flush all tlb when changing translation mode */
1122
        tlb_flush(env, 1);
1123
        env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1124
    }
1125
#if 0
1126
    if (loglevel != 0) {
1127
        fprintf(logfile, "%s: T0 %08lx\n", __func__, value);
1128
    }
1129
#endif
1130
    switch (PPC_EXCP(env)) {
1131
    case PPC_FLAGS_EXCP_602:
1132
    case PPC_FLAGS_EXCP_603:
1133
        if (((value >> MSR_TGPR) & 1) != msr_tgpr) {
1134
            /* Swap temporary saved registers with GPRs */
1135
            swap_gpr_tgpr(env);
1136
        }
1137
        break;
1138
    default:
1139
        break;
1140
    }
1141
#if defined (TARGET_PPC64)
1142
    msr_sf   = (value >> MSR_SF)   & 1;
1143
    msr_isf  = (value >> MSR_ISF)  & 1;
1144
    msr_hv   = (value >> MSR_HV)   & 1;
1145
#endif
1146
    msr_ucle = (value >> MSR_UCLE) & 1;
1147
    msr_vr   = (value >> MSR_VR)   & 1; /* VR / SPE */
1148
    msr_ap   = (value >> MSR_AP)   & 1;
1149
    msr_sa   = (value >> MSR_SA)   & 1;
1150
    msr_key  = (value >> MSR_KEY)  & 1;
1151
    msr_pow  = (value >> MSR_POW)  & 1; /* POW / WE */
1152
    msr_tlb  = (value >> MSR_TLB)  & 1; /* TLB / TGPR / CE */
1153
    msr_ile  = (value >> MSR_ILE)  & 1;
1154
    msr_ee   = (value >> MSR_EE)   & 1;
1155
    msr_pr   = (value >> MSR_PR)   & 1;
1156
    msr_fp   = (value >> MSR_FP)   & 1;
1157
    msr_me   = (value >> MSR_ME)   & 1;
1158
    msr_fe0  = (value >> MSR_FE0)  & 1;
1159
    msr_se   = (value >> MSR_SE)   & 1; /* SE / DWE / UBLE */
1160
    msr_be   = (value >> MSR_BE)   & 1; /* BE / DE */
1161
    msr_fe1  = (value >> MSR_FE1)  & 1;
1162
    msr_al   = (value >> MSR_AL)   & 1;
1163
    msr_ip   = (value >> MSR_IP)   & 1;
1164
    msr_ir   = (value >> MSR_IR)   & 1; /* IR / IS */
1165
    msr_dr   = (value >> MSR_DR)   & 1; /* DR / DS */
1166
    msr_pe   = (value >> MSR_PE)   & 1; /* PE / EP */
1167
    msr_px   = (value >> MSR_PX)   & 1; /* PX / PMM */
1168
    msr_ri   = (value >> MSR_RI)   & 1;
1169
    msr_le   = (value >> MSR_LE)   & 1;
1170
    do_compute_hflags(env);
1171

    
1172
    enter_pm = 0;
1173
    switch (PPC_EXCP(env)) {
1174
    case PPC_FLAGS_EXCP_603:
1175
        /* Don't handle SLEEP mode: we should disable all clocks...
1176
         * No dynamic power-management.
1177
         */
1178
        if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00C00000) != 0)
1179
            enter_pm = 1;
1180
        break;
1181
    case PPC_FLAGS_EXCP_604:
1182
        if (msr_pow == 1)
1183
            enter_pm = 1;
1184
        break;
1185
    case PPC_FLAGS_EXCP_7x0:
1186
        if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00E00000) != 0)
1187
            enter_pm = 1;
1188
        break;
1189
    default:
1190
        break;
1191
    }
1192
    if (enter_pm) {
1193
        /* power save: exit cpu loop */
1194
        env->halted = 1;
1195
        env->exception_index = EXCP_HLT;
1196
        cpu_loop_exit();
1197
    }
1198
}
1199

    
1200
#if defined(TARGET_PPC64)
1201
void ppc_store_msr_32 (CPUPPCState *env, target_ulong value)
1202
{
1203
    do_store_msr(env, (uint32_t)value);
1204
}
1205
#endif
1206

    
1207
void do_compute_hflags (CPUPPCState *env)
1208
{
1209
    /* Compute current hflags */
1210
    env->hflags = (msr_pr << MSR_PR) | (msr_le << MSR_LE) |
1211
        (msr_fp << MSR_FP) | (msr_fe0 << MSR_FE0) | (msr_fe1 << MSR_FE1) |
1212
        (msr_vr << MSR_VR) | (msr_ap << MSR_AP) | (msr_sa << MSR_SA) |
1213
        (msr_se << MSR_SE) | (msr_be << MSR_BE);
1214
#if defined (TARGET_PPC64)
1215
    env->hflags |= (msr_sf << (MSR_SF - 32)) | (msr_hv << (MSR_HV - 32));
1216
#endif
1217
}
1218

    
1219
/*****************************************************************************/
1220
/* Exception processing */
1221
#if defined (CONFIG_USER_ONLY)
1222
void do_interrupt (CPUState *env)
1223
{
1224
    env->exception_index = -1;
1225
}
1226
#else /* defined (CONFIG_USER_ONLY) */
1227
static void dump_syscall(CPUState *env)
1228
{
1229
    fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
1230
            " r5=0x" REGX " r6=0x" REGX " nip=0x" REGX "\n",
1231
            env->gpr[0], env->gpr[3], env->gpr[4],
1232
            env->gpr[5], env->gpr[6], env->nip);
1233
}
1234

    
1235
void do_interrupt (CPUState *env)
1236
{
1237
    target_ulong msr, *srr_0, *srr_1;
1238
    int excp;
1239

    
1240
    excp = env->exception_index;
1241
    msr = do_load_msr(env);
1242
    /* The default is to use SRR0 & SRR1 to save the exception context */
1243
    srr_0 = &env->spr[SPR_SRR0];
1244
    srr_1 = &env->spr[SPR_SRR1];
1245
#if defined (DEBUG_EXCEPTIONS)
1246
    if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1) {
1247
        if (loglevel != 0) {
1248
            fprintf(logfile, "Raise exception at 0x%08lx => 0x%08x (%02x)\n",
1249
                    (unsigned long)env->nip, excp, env->error_code);
1250
            cpu_dump_state(env, logfile, fprintf, 0);
1251
        }
1252
    }
1253
#endif
1254
    if (loglevel & CPU_LOG_INT) {
1255
        fprintf(logfile, "Raise exception at 0x%08lx => 0x%08x (%02x)\n",
1256
                (unsigned long)env->nip, excp, env->error_code);
1257
    }
1258
    msr_pow = 0;
1259
    /* Generate informations in save/restore registers */
1260
    switch (excp) {
1261
    /* Generic PowerPC exceptions */
1262
    case EXCP_RESET: /* 0x0100 */
1263
        if (PPC_EXCP(env) != PPC_FLAGS_EXCP_40x) {
1264
            if (msr_ip)
1265
                excp += 0xFFC00;
1266
            excp |= 0xFFC00000;
1267
        } else {
1268
            srr_0 = &env->spr[SPR_40x_SRR2];
1269
            srr_1 = &env->spr[SPR_40x_SRR3];
1270
        }
1271
        goto store_next;
1272
    case EXCP_MACHINE_CHECK: /* 0x0200 */
1273
        if (msr_me == 0) {
1274
            cpu_abort(env, "Machine check exception while not allowed\n");
1275
        }
1276
        if (unlikely(PPC_EXCP(env) == PPC_FLAGS_EXCP_40x)) {
1277
            srr_0 = &env->spr[SPR_40x_SRR2];
1278
            srr_1 = &env->spr[SPR_40x_SRR3];
1279
        }
1280
        msr_me = 0;
1281
        break;
1282
    case EXCP_DSI: /* 0x0300 */
1283
        /* Store exception cause */
1284
        /* data location address has been stored
1285
         * when the fault has been detected
1286
         */
1287
        msr &= ~0xFFFF0000;
1288
#if defined (DEBUG_EXCEPTIONS)
1289
        if (loglevel) {
1290
            fprintf(logfile, "DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
1291
                    env->spr[SPR_DSISR], env->spr[SPR_DAR]);
1292
        } else {
1293
            printf("DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
1294
                   env->spr[SPR_DSISR], env->spr[SPR_DAR]);
1295
        }
1296
#endif
1297
        goto store_next;
1298
    case EXCP_ISI: /* 0x0400 */
1299
        /* Store exception cause */
1300
        msr &= ~0xFFFF0000;
1301
        msr |= env->error_code;
1302
#if defined (DEBUG_EXCEPTIONS)
1303
        if (loglevel != 0) {
1304
            fprintf(logfile, "ISI exception: msr=0x%08x, nip=0x%08x\n",
1305
                    msr, env->nip);
1306
        }
1307
#endif
1308
        goto store_next;
1309
    case EXCP_EXTERNAL: /* 0x0500 */
1310
        if (msr_ee == 0) {
1311
#if defined (DEBUG_EXCEPTIONS)
1312
            if (loglevel > 0) {
1313
                fprintf(logfile, "Skipping hardware interrupt\n");
1314
            }
1315
#endif
1316
            /* Requeue it */
1317
            env->interrupt_request |= CPU_INTERRUPT_HARD;
1318
            return;
1319
        }
1320
        goto store_next;
1321
    case EXCP_ALIGN: /* 0x0600 */
1322
        if (likely(PPC_EXCP(env) != PPC_FLAGS_EXCP_601)) {
1323
            /* Store exception cause */
1324
            /* Get rS/rD and rA from faulting opcode */
1325
            env->spr[SPR_DSISR] |=
1326
                (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
1327
            /* data location address has been stored
1328
             * when the fault has been detected
1329
             */
1330
        } else {
1331
            /* IO error exception on PowerPC 601 */
1332
            /* XXX: TODO */
1333
            cpu_abort(env,
1334
                      "601 IO error exception is not implemented yet !\n");
1335
        }
1336
        goto store_current;
1337
    case EXCP_PROGRAM: /* 0x0700 */
1338
        msr &= ~0xFFFF0000;
1339
        switch (env->error_code & ~0xF) {
1340
        case EXCP_FP:
1341
            if (msr_fe0 == 0 && msr_fe1 == 0) {
1342
#if defined (DEBUG_EXCEPTIONS)
1343
                printf("Ignore floating point exception\n");
1344
#endif
1345
                return;
1346
            }
1347
            msr |= 0x00100000;
1348
            /* Set FX */
1349
            env->fpscr[7] |= 0x8;
1350
            /* Finally, update FEX */
1351
            if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
1352
                ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
1353
                env->fpscr[7] |= 0x4;
1354
            break;
1355
        case EXCP_INVAL:
1356
            //      printf("Invalid instruction at 0x%08x\n", env->nip);
1357
            msr |= 0x00080000;
1358
            break;
1359
        case EXCP_PRIV:
1360
            msr |= 0x00040000;
1361
            break;
1362
        case EXCP_TRAP:
1363
            msr |= 0x00020000;
1364
            break;
1365
        default:
1366
            /* Should never occur */
1367
            break;
1368
        }
1369
        msr |= 0x00010000;
1370
        goto store_current;
1371
    case EXCP_NO_FP: /* 0x0800 */
1372
        msr &= ~0xFFFF0000;
1373
        goto store_current;
1374
    case EXCP_DECR:
1375
        if (msr_ee == 0) {
1376
#if 1
1377
            /* Requeue it */
1378
            env->interrupt_request |= CPU_INTERRUPT_TIMER;
1379
#endif
1380
            return;
1381
        }
1382
        goto store_next;
1383
    case EXCP_SYSCALL: /* 0x0C00 */
1384
        /* NOTE: this is a temporary hack to support graphics OSI
1385
           calls from the MOL driver */
1386
        if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
1387
            env->osi_call) {
1388
            if (env->osi_call(env) != 0)
1389
                return;
1390
        }
1391
        if (loglevel & CPU_LOG_INT) {
1392
            dump_syscall(env);
1393
        }
1394
        goto store_next;
1395
    case EXCP_TRACE: /* 0x0D00 */
1396
        goto store_next;
1397
    case EXCP_PERF: /* 0x0F00 */
1398
        /* XXX: TODO */
1399
        cpu_abort(env,
1400
                  "Performance counter exception is not implemented yet !\n");
1401
        goto store_next;
1402
    /* 32 bits PowerPC specific exceptions */
1403
    case EXCP_FP_ASSIST: /* 0x0E00 */
1404
        /* XXX: TODO */
1405
        cpu_abort(env, "Floating point assist exception "
1406
                  "is not implemented yet !\n");
1407
        goto store_next;
1408
        /* 64 bits PowerPC exceptions */
1409
    case EXCP_DSEG: /* 0x0380 */
1410
        /* XXX: TODO */
1411
        cpu_abort(env, "Data segment exception is not implemented yet !\n");
1412
        goto store_next;
1413
    case EXCP_ISEG: /* 0x0480 */
1414
        /* XXX: TODO */
1415
        cpu_abort(env,
1416
                  "Instruction segment exception is not implemented yet !\n");
1417
        goto store_next;
1418
    case EXCP_HDECR: /* 0x0980 */
1419
        if (msr_ee == 0) {
1420
#if 1
1421
            /* Requeue it */
1422
            env->interrupt_request |= CPU_INTERRUPT_TIMER;
1423
#endif
1424
            return;
1425
        }
1426
        /* XXX: TODO */
1427
        cpu_abort(env, "Hypervisor decrementer exception is not implemented "
1428
                  "yet !\n");
1429
        goto store_next;
1430
    /* Implementation specific exceptions */
1431
    case 0x0A00:
1432
        if (likely(env->spr[SPR_PVR] == CPU_PPC_G2 ||
1433
                   env->spr[SPR_PVR] == CPU_PPC_G2LE)) {
1434
            /* Critical interrupt on G2 */
1435
            /* XXX: TODO */
1436
            cpu_abort(env, "G2 critical interrupt is not implemented yet !\n");
1437
            goto store_next;
1438
        } else {
1439
            cpu_abort(env, "Invalid exception 0x0A00 !\n");
1440
        }
1441
        return;
1442
    case 0x0F20:
1443
        switch (PPC_EXCP(env)) {
1444
        case PPC_FLAGS_EXCP_40x:
1445
            /* APU unavailable on 405 */
1446
            /* XXX: TODO */
1447
            cpu_abort(env,
1448
                      "APU unavailable exception is not implemented yet !\n");
1449
            goto store_next;
1450
        case PPC_FLAGS_EXCP_74xx:
1451
            /* Altivec unavailable */
1452
            /* XXX: TODO */
1453
            cpu_abort(env, "Altivec unavailable exception "
1454
                      "is not implemented yet !\n");
1455
            goto store_next;
1456
        default:
1457
            cpu_abort(env, "Invalid exception 0x0F20 !\n");
1458
            break;
1459
        }
1460
        return;
1461
    case 0x1000:
1462
        switch (PPC_EXCP(env)) {
1463
        case PPC_FLAGS_EXCP_40x:
1464
            /* PIT on 4xx */
1465
            /* XXX: TODO */
1466
            cpu_abort(env, "40x PIT exception is not implemented yet !\n");
1467
            goto store_next;
1468
        case PPC_FLAGS_EXCP_602:
1469
        case PPC_FLAGS_EXCP_603:
1470
            /* ITLBMISS on 602/603 */
1471
            goto store_gprs;
1472
        case PPC_FLAGS_EXCP_7x5:
1473
            /* ITLBMISS on 745/755 */
1474
            goto tlb_miss;
1475
        default:
1476
            cpu_abort(env, "Invalid exception 0x1000 !\n");
1477
            break;
1478
        }
1479
        return;
1480
    case 0x1010:
1481
        switch (PPC_EXCP(env)) {
1482
        case PPC_FLAGS_EXCP_40x:
1483
            /* FIT on 4xx */
1484
            /* XXX: TODO */
1485
            cpu_abort(env, "40x FIT exception is not implemented yet !\n");
1486
            goto store_next;
1487
        default:
1488
            cpu_abort(env, "Invalid exception 0x1010 !\n");
1489
            break;
1490
        }
1491
        return;
1492
    case 0x1020:
1493
        switch (PPC_EXCP(env)) {
1494
        case PPC_FLAGS_EXCP_40x:
1495
            /* Watchdog on 4xx */
1496
            /* XXX: TODO */
1497
            cpu_abort(env,
1498
                      "40x watchdog exception is not implemented yet !\n");
1499
            goto store_next;
1500
        default:
1501
            cpu_abort(env, "Invalid exception 0x1020 !\n");
1502
            break;
1503
        }
1504
        return;
1505
    case 0x1100:
1506
        switch (PPC_EXCP(env)) {
1507
        case PPC_FLAGS_EXCP_40x:
1508
            /* DTLBMISS on 4xx */
1509
            /* XXX: TODO */
1510
            cpu_abort(env,
1511
                      "40x DTLBMISS exception is not implemented yet !\n");
1512
            goto store_next;
1513
        case PPC_FLAGS_EXCP_602:
1514
        case PPC_FLAGS_EXCP_603:
1515
            /* DLTLBMISS on 602/603 */
1516
            goto store_gprs;
1517
        case PPC_FLAGS_EXCP_7x5:
1518
            /* DLTLBMISS on 745/755 */
1519
            goto tlb_miss;
1520
        default:
1521
            cpu_abort(env, "Invalid exception 0x1100 !\n");
1522
            break;
1523
        }
1524
        return;
1525
    case 0x1200:
1526
        switch (PPC_EXCP(env)) {
1527
        case PPC_FLAGS_EXCP_40x:
1528
            /* ITLBMISS on 4xx */
1529
            /* XXX: TODO */
1530
            cpu_abort(env,
1531
                      "40x ITLBMISS exception is not implemented yet !\n");
1532
            goto store_next;
1533
        case PPC_FLAGS_EXCP_602:
1534
        case PPC_FLAGS_EXCP_603:
1535
            /* DSTLBMISS on 602/603 */
1536
        store_gprs:
1537
            /* Swap temporary saved registers with GPRs */
1538
            swap_gpr_tgpr(env);
1539
            msr_tgpr = 1;
1540
#if defined (DEBUG_SOFTWARE_TLB)
1541
            if (loglevel != 0) {
1542
                const unsigned char *es;
1543
                target_ulong *miss, *cmp;
1544
                int en;
1545
                if (excp == 0x1000) {
1546
                    es = "I";
1547
                    en = 'I';
1548
                    miss = &env->spr[SPR_IMISS];
1549
                    cmp = &env->spr[SPR_ICMP];
1550
                } else {
1551
                    if (excp == 0x1100)
1552
                        es = "DL";
1553
                    else
1554
                        es = "DS";
1555
                    en = 'D';
1556
                    miss = &env->spr[SPR_DMISS];
1557
                    cmp = &env->spr[SPR_DCMP];
1558
                }
1559
                fprintf(logfile, "6xx %sTLB miss: %cM %08x %cC %08x "
1560
                        "H1 %08x H2 %08x %08x\n", es, en, *miss, en, *cmp,
1561
                        env->spr[SPR_HASH1], env->spr[SPR_HASH2],
1562
                        env->error_code);
1563
            }
1564
#endif
1565
            goto tlb_miss;
1566
        case PPC_FLAGS_EXCP_7x5:
1567
            /* DSTLBMISS on 745/755 */
1568
        tlb_miss:
1569
            msr &= ~0xF83F0000;
1570
            msr |= env->crf[0] << 28;
1571
            msr |= env->error_code; /* key, D/I, S/L bits */
1572
            /* Set way using a LRU mechanism */
1573
            msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
1574
            goto store_next;
1575
        default:
1576
            cpu_abort(env, "Invalid exception 0x1200 !\n");
1577
            break;
1578
        }
1579
        return;
1580
    case 0x1300:
1581
        switch (PPC_EXCP(env)) {
1582
        case PPC_FLAGS_EXCP_601:
1583
        case PPC_FLAGS_EXCP_602:
1584
        case PPC_FLAGS_EXCP_603:
1585
        case PPC_FLAGS_EXCP_604:
1586
        case PPC_FLAGS_EXCP_7x0:
1587
        case PPC_FLAGS_EXCP_7x5:
1588
            /* IABR on 6xx/7xx */
1589
            /* XXX: TODO */
1590
            cpu_abort(env, "IABR exception is not implemented yet !\n");
1591
            goto store_next;
1592
        default:
1593
            cpu_abort(env, "Invalid exception 0x1300 !\n");
1594
            break;
1595
        }
1596
        return;
1597
    case 0x1400:
1598
        switch (PPC_EXCP(env)) {
1599
        case PPC_FLAGS_EXCP_601:
1600
        case PPC_FLAGS_EXCP_602:
1601
        case PPC_FLAGS_EXCP_603:
1602
        case PPC_FLAGS_EXCP_604:
1603
        case PPC_FLAGS_EXCP_7x0:
1604
        case PPC_FLAGS_EXCP_7x5:
1605
            /* SMI on 6xx/7xx */
1606
            /* XXX: TODO */
1607
            cpu_abort(env, "SMI exception is not implemented yet !\n");
1608
            goto store_next;
1609
        default:
1610
            cpu_abort(env, "Invalid exception 0x1400 !\n");
1611
            break;
1612
        }
1613
        return;
1614
    case 0x1500:
1615
        switch (PPC_EXCP(env)) {
1616
        case PPC_FLAGS_EXCP_602:
1617
            /* Watchdog on 602 */
1618
            /* XXX: TODO */
1619
            cpu_abort(env,
1620
                      "602 watchdog exception is not implemented yet !\n");
1621
            goto store_next;
1622
        case PPC_FLAGS_EXCP_970:
1623
            /* Soft patch exception on 970 */
1624
            /* XXX: TODO */
1625
            cpu_abort(env,
1626
                      "970 soft-patch exception is not implemented yet !\n");
1627
            goto store_next;
1628
        case PPC_FLAGS_EXCP_74xx:
1629
            /* VPU assist on 74xx */
1630
            /* XXX: TODO */
1631
            cpu_abort(env, "VPU assist exception is not implemented yet !\n");
1632
            goto store_next;
1633
        default:
1634
            cpu_abort(env, "Invalid exception 0x1500 !\n");
1635
            break;
1636
        }
1637
        return;
1638
    case 0x1600:
1639
        switch (PPC_EXCP(env)) {
1640
        case PPC_FLAGS_EXCP_602:
1641
            /* Emulation trap on 602 */
1642
            /* XXX: TODO */
1643
            cpu_abort(env, "602 emulation trap exception "
1644
                      "is not implemented yet !\n");
1645
            goto store_next;
1646
        case PPC_FLAGS_EXCP_970:
1647
            /* Maintenance exception on 970 */
1648
            /* XXX: TODO */
1649
            cpu_abort(env,
1650
                      "970 maintenance exception is not implemented yet !\n");
1651
            goto store_next;
1652
        default:
1653
            cpu_abort(env, "Invalid exception 0x1600 !\n");
1654
            break;
1655
        }
1656
        return;
1657
    case 0x1700:
1658
        switch (PPC_EXCP(env)) {
1659
        case PPC_FLAGS_EXCP_7x0:
1660
        case PPC_FLAGS_EXCP_7x5:
1661
            /* Thermal management interrupt on G3 */
1662
            /* XXX: TODO */
1663
            cpu_abort(env, "G3 thermal management exception "
1664
                      "is not implemented yet !\n");
1665
            goto store_next;
1666
        case PPC_FLAGS_EXCP_970:
1667
            /* VPU assist on 970 */
1668
            /* XXX: TODO */
1669
            cpu_abort(env,
1670
                      "970 VPU assist exception is not implemented yet !\n");
1671
            goto store_next;
1672
        default:
1673
            cpu_abort(env, "Invalid exception 0x1700 !\n");
1674
            break;
1675
        }
1676
        return;
1677
    case 0x1800:
1678
        switch (PPC_EXCP(env)) {
1679
        case PPC_FLAGS_EXCP_970:
1680
            /* Thermal exception on 970 */
1681
            /* XXX: TODO */
1682
            cpu_abort(env, "970 thermal management exception "
1683
                      "is not implemented yet !\n");
1684
            goto store_next;
1685
        default:
1686
            cpu_abort(env, "Invalid exception 0x1800 !\n");
1687
            break;
1688
        }
1689
        return;
1690
    case 0x2000:
1691
        switch (PPC_EXCP(env)) {
1692
        case PPC_FLAGS_EXCP_40x:
1693
            /* DEBUG on 4xx */
1694
            /* XXX: TODO */
1695
            cpu_abort(env, "40x debug exception is not implemented yet !\n");
1696
            goto store_next;
1697
        case PPC_FLAGS_EXCP_601:
1698
            /* Run mode exception on 601 */
1699
            /* XXX: TODO */
1700
            cpu_abort(env,
1701
                      "601 run mode exception is not implemented yet !\n");
1702
            goto store_next;
1703
        default:
1704
            cpu_abort(env, "Invalid exception 0x1800 !\n");
1705
            break;
1706
        }
1707
        return;
1708
    /* Other exceptions */
1709
    /* Qemu internal exceptions:
1710
     * we should never come here with those values: abort execution
1711
     */
1712
    default:
1713
        cpu_abort(env, "Invalid exception: code %d (%04x)\n", excp, excp);
1714
        return;
1715
    store_current:
1716
        /* save current instruction location */
1717
        *srr_0 = (env->nip - 4) & 0xFFFFFFFFULL;
1718
        break;
1719
    store_next:
1720
        /* save next instruction location */
1721
        *srr_0 = env->nip & 0xFFFFFFFFULL;
1722
        break;
1723
    }
1724
    /* Save msr */
1725
    *srr_1 = msr;
1726
    /* If we disactivated any translation, flush TLBs */
1727
    if (msr_ir || msr_dr) {
1728
        tlb_flush(env, 1);
1729
    }
1730
    /* reload MSR with correct bits */
1731
    msr_ee = 0;
1732
    msr_pr = 0;
1733
    msr_fp = 0;
1734
    msr_fe0 = 0;
1735
    msr_se = 0;
1736
    msr_be = 0;
1737
    msr_fe1 = 0;
1738
    msr_ir = 0;
1739
    msr_dr = 0;
1740
    msr_ri = 0;
1741
    msr_le = msr_ile;
1742
    msr_sf = msr_isf;
1743
    do_compute_hflags(env);
1744
    /* Jump to handler */
1745
    env->nip = excp;
1746
    env->exception_index = EXCP_NONE;
1747
}
1748
#endif /* !CONFIG_USER_ONLY */