Statistics
| Branch: | Revision:

root / target-ppc / helper.c @ 5eb7995e

History | View | Annotate | Download (77.8 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, target_ulong 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_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
64
{
65
    return addr;
66
}
67

    
68
#else
69
/* Common routines used by software and hardware TLBs emulation */
70
static inline int pte_is_valid (target_ulong pte0)
71
{
72
    return pte0 & 0x80000000 ? 1 : 0;
73
}
74

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

    
80
#if defined(TARGET_PPC64)
81
static inline int pte64_is_valid (target_ulong pte0)
82
{
83
    return pte0 & 0x0000000000000001ULL ? 1 : 0;
84
}
85

    
86
static inline void pte64_invalidate (target_ulong *pte0)
87
{
88
    *pte0 &= ~0x0000000000000001ULL;
89
}
90
#endif
91

    
92
#define PTE_PTEM_MASK 0x7FFFFFBF
93
#define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
94
#if defined(TARGET_PPC64)
95
#define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
96
#define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
97
#endif
98

    
99
static inline int _pte_check (mmu_ctx_t *ctx, int is_64b,
100
                              target_ulong pte0, target_ulong pte1,
101
                              int h, int rw)
102
{
103
    target_ulong ptem, mmask;
104
    int access, ret, pteh, ptev;
105

    
106
    access = 0;
107
    ret = -1;
108
    /* Check validity and table match */
109
#if defined(TARGET_PPC64)
110
    if (is_64b) {
111
        ptev = pte64_is_valid(pte0);
112
        pteh = (pte0 >> 1) & 1;
113
    } else
114
#endif
115
    {
116
        ptev = pte_is_valid(pte0);
117
        pteh = (pte0 >> 6) & 1;
118
    }
119
    if (ptev && h == pteh) {
120
        /* Check vsid & api */
121
#if defined(TARGET_PPC64)
122
        if (is_64b) {
123
            ptem = pte0 & PTE64_PTEM_MASK;
124
            mmask = PTE64_CHECK_MASK;
125
        } else
126
#endif
127
        {
128
            ptem = pte0 & PTE_PTEM_MASK;
129
            mmask = PTE_CHECK_MASK;
130
        }
131
        if (ptem == ctx->ptem) {
132
            if (ctx->raddr != (target_ulong)-1) {
133
                /* all matches should have equal RPN, WIMG & PP */
134
                if ((ctx->raddr & mmask) != (pte1 & mmask)) {
135
                    if (loglevel != 0)
136
                        fprintf(logfile, "Bad RPN/WIMG/PP\n");
137
                    return -3;
138
                }
139
            }
140
            /* Compute access rights */
141
            if (ctx->key == 0) {
142
                access = PAGE_READ;
143
                if ((pte1 & 0x00000003) != 0x3)
144
                    access |= PAGE_WRITE;
145
            } else {
146
                switch (pte1 & 0x00000003) {
147
                case 0x0:
148
                    access = 0;
149
                    break;
150
                case 0x1:
151
                case 0x3:
152
                    access = PAGE_READ;
153
                    break;
154
                case 0x2:
155
                    access = PAGE_READ | PAGE_WRITE;
156
                    break;
157
                }
158
            }
159
            /* Keep the matching PTE informations */
160
            ctx->raddr = pte1;
161
            ctx->prot = access;
162
            if ((rw == 0 && (access & PAGE_READ)) ||
163
                (rw == 1 && (access & PAGE_WRITE))) {
164
                /* Access granted */
165
#if defined (DEBUG_MMU)
166
                if (loglevel != 0)
167
                    fprintf(logfile, "PTE access granted !\n");
168
#endif
169
                ret = 0;
170
            } else {
171
                /* Access right violation */
172
#if defined (DEBUG_MMU)
173
                if (loglevel != 0)
174
                    fprintf(logfile, "PTE access rejected\n");
175
#endif
176
                ret = -2;
177
            }
178
        }
179
    }
180

    
181
    return ret;
182
}
183

    
184
static int pte32_check (mmu_ctx_t *ctx,
185
                        target_ulong pte0, target_ulong pte1, int h, int rw)
186
{
187
    return _pte_check(ctx, 0, pte0, pte1, h, rw);
188
}
189

    
190
#if defined(TARGET_PPC64)
191
static int pte64_check (mmu_ctx_t *ctx,
192
                        target_ulong pte0, target_ulong pte1, int h, int rw)
193
{
194
    return _pte_check(ctx, 1, pte0, pte1, h, rw);
195
}
196
#endif
197

    
198
static int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
199
                             int ret, int rw)
200
{
201
    int store = 0;
202

    
203
    /* Update page flags */
204
    if (!(*pte1p & 0x00000100)) {
205
        /* Update accessed flag */
206
        *pte1p |= 0x00000100;
207
        store = 1;
208
    }
209
    if (!(*pte1p & 0x00000080)) {
210
        if (rw == 1 && ret == 0) {
211
            /* Update changed flag */
212
            *pte1p |= 0x00000080;
213
            store = 1;
214
        } else {
215
            /* Force page fault for first write access */
216
            ctx->prot &= ~PAGE_WRITE;
217
        }
218
    }
219

    
220
    return store;
221
}
222

    
223
/* Software driven TLB helpers */
224
static int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
225
                              int way, int is_code)
226
{
227
    int nr;
228

    
229
    /* Select TLB num in a way from address */
230
    nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
231
    /* Select TLB way */
232
    nr += env->tlb_per_way * way;
233
    /* 6xx have separate TLBs for instructions and data */
234
    if (is_code && env->id_tlbs == 1)
235
        nr += env->nb_tlb;
236

    
237
    return nr;
238
}
239

    
240
void ppc6xx_tlb_invalidate_all (CPUState *env)
241
{
242
    ppc6xx_tlb_t *tlb;
243
    int nr, max;
244

    
245
#if defined (DEBUG_SOFTWARE_TLB) && 0
246
    if (loglevel != 0) {
247
        fprintf(logfile, "Invalidate all TLBs\n");
248
    }
249
#endif
250
    /* Invalidate all defined software TLB */
251
    max = env->nb_tlb;
252
    if (env->id_tlbs == 1)
253
        max *= 2;
254
    for (nr = 0; nr < max; nr++) {
255
        tlb = &env->tlb[nr].tlb6;
256
#if !defined(FLUSH_ALL_TLBS)
257
        tlb_flush_page(env, tlb->EPN);
258
#endif
259
        pte_invalidate(&tlb->pte0);
260
    }
261
#if defined(FLUSH_ALL_TLBS)
262
    tlb_flush(env, 1);
263
#endif
264
}
265

    
266
static inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
267
                                                 target_ulong eaddr,
268
                                                 int is_code, int match_epn)
269
{
270
#if !defined(FLUSH_ALL_TLBS)
271
    ppc6xx_tlb_t *tlb;
272
    int way, nr;
273

    
274
    /* Invalidate ITLB + DTLB, all ways */
275
    for (way = 0; way < env->nb_ways; way++) {
276
        nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
277
        tlb = &env->tlb[nr].tlb6;
278
        if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
279
#if defined (DEBUG_SOFTWARE_TLB)
280
            if (loglevel != 0) {
281
                fprintf(logfile, "TLB invalidate %d/%d " ADDRX "\n",
282
                        nr, env->nb_tlb, eaddr);
283
            }
284
#endif
285
            pte_invalidate(&tlb->pte0);
286
            tlb_flush_page(env, tlb->EPN);
287
        }
288
    }
289
#else
290
    /* XXX: PowerPC specification say this is valid as well */
291
    ppc6xx_tlb_invalidate_all(env);
292
#endif
293
}
294

    
295
void ppc6xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
296
                                 int is_code)
297
{
298
    __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
299
}
300

    
301
void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
302
                       target_ulong pte0, target_ulong pte1)
303
{
304
    ppc6xx_tlb_t *tlb;
305
    int nr;
306

    
307
    nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
308
    tlb = &env->tlb[nr].tlb6;
309
#if defined (DEBUG_SOFTWARE_TLB)
310
    if (loglevel != 0) {
311
        fprintf(logfile, "Set TLB %d/%d EPN " ADDRX " PTE0 " ADDRX
312
                " PTE1 " ADDRX "\n", nr, env->nb_tlb, EPN, pte0, pte1);
313
    }
314
#endif
315
    /* Invalidate any pending reference in Qemu for this virtual address */
316
    __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
317
    tlb->pte0 = pte0;
318
    tlb->pte1 = pte1;
319
    tlb->EPN = EPN;
320
    /* Store last way for LRU mechanism */
321
    env->last_way = way;
322
}
323

    
324
static int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
325
                             target_ulong eaddr, int rw, int access_type)
326
{
327
    ppc6xx_tlb_t *tlb;
328
    int nr, best, way;
329
    int ret;
330

    
331
    best = -1;
332
    ret = -1; /* No TLB found */
333
    for (way = 0; way < env->nb_ways; way++) {
334
        nr = ppc6xx_tlb_getnum(env, eaddr, way,
335
                               access_type == ACCESS_CODE ? 1 : 0);
336
        tlb = &env->tlb[nr].tlb6;
337
        /* This test "emulates" the PTE index match for hardware TLBs */
338
        if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
339
#if defined (DEBUG_SOFTWARE_TLB)
340
            if (loglevel != 0) {
341
                fprintf(logfile, "TLB %d/%d %s [" ADDRX " " ADDRX
342
                        "] <> " ADDRX "\n",
343
                        nr, env->nb_tlb,
344
                        pte_is_valid(tlb->pte0) ? "valid" : "inval",
345
                        tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
346
            }
347
#endif
348
            continue;
349
        }
350
#if defined (DEBUG_SOFTWARE_TLB)
351
        if (loglevel != 0) {
352
            fprintf(logfile, "TLB %d/%d %s " ADDRX " <> " ADDRX " " ADDRX
353
                    " %c %c\n",
354
                    nr, env->nb_tlb,
355
                    pte_is_valid(tlb->pte0) ? "valid" : "inval",
356
                    tlb->EPN, eaddr, tlb->pte1,
357
                    rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
358
        }
359
#endif
360
        switch (pte32_check(ctx, tlb->pte0, tlb->pte1, 0, rw)) {
361
        case -3:
362
            /* TLB inconsistency */
363
            return -1;
364
        case -2:
365
            /* Access violation */
366
            ret = -2;
367
            best = nr;
368
            break;
369
        case -1:
370
        default:
371
            /* No match */
372
            break;
373
        case 0:
374
            /* access granted */
375
            /* XXX: we should go on looping to check all TLBs consistency
376
             *      but we can speed-up the whole thing as the
377
             *      result would be undefined if TLBs are not consistent.
378
             */
379
            ret = 0;
380
            best = nr;
381
            goto done;
382
        }
383
    }
384
    if (best != -1) {
385
    done:
386
#if defined (DEBUG_SOFTWARE_TLB)
387
        if (loglevel != 0) {
388
            fprintf(logfile, "found TLB at addr 0x%08lx prot=0x%01x ret=%d\n",
389
                    ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
390
        }
391
#endif
392
        /* Update page flags */
393
        pte_update_flags(ctx, &env->tlb[best].tlb6.pte1, ret, rw);
394
    }
395

    
396
    return ret;
397
}
398

    
399
/* Perform BAT hit & translation */
400
static int get_bat (CPUState *env, mmu_ctx_t *ctx,
401
                    target_ulong virtual, int rw, int type)
402
{
403
    target_ulong *BATlt, *BATut, *BATu, *BATl;
404
    target_ulong base, BEPIl, BEPIu, bl;
405
    int i;
406
    int ret = -1;
407

    
408
#if defined (DEBUG_BATS)
409
    if (loglevel != 0) {
410
        fprintf(logfile, "%s: %cBAT v 0x" ADDRX "\n", __func__,
411
                type == ACCESS_CODE ? 'I' : 'D', virtual);
412
    }
413
#endif
414
    switch (type) {
415
    case ACCESS_CODE:
416
        BATlt = env->IBAT[1];
417
        BATut = env->IBAT[0];
418
        break;
419
    default:
420
        BATlt = env->DBAT[1];
421
        BATut = env->DBAT[0];
422
        break;
423
    }
424
#if defined (DEBUG_BATS)
425
    if (loglevel != 0) {
426
        fprintf(logfile, "%s...: %cBAT v 0x" ADDRX "\n", __func__,
427
                type == ACCESS_CODE ? 'I' : 'D', virtual);
428
    }
429
#endif
430
    base = virtual & 0xFFFC0000;
431
    for (i = 0; i < 4; i++) {
432
        BATu = &BATut[i];
433
        BATl = &BATlt[i];
434
        BEPIu = *BATu & 0xF0000000;
435
        BEPIl = *BATu & 0x0FFE0000;
436
        bl = (*BATu & 0x00001FFC) << 15;
437
#if defined (DEBUG_BATS)
438
        if (loglevel != 0) {
439
            fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
440
                    " BATl 0x" ADDRX "\n",
441
                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
442
                    *BATu, *BATl);
443
        }
444
#endif
445
        if ((virtual & 0xF0000000) == BEPIu &&
446
            ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
447
            /* BAT matches */
448
            if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
449
                (msr_pr == 1 && (*BATu & 0x00000001))) {
450
                /* Get physical address */
451
                ctx->raddr = (*BATl & 0xF0000000) |
452
                    ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
453
                    (virtual & 0x0001F000);
454
                if (*BATl & 0x00000001)
455
                    ctx->prot = PAGE_READ;
456
                if (*BATl & 0x00000002)
457
                    ctx->prot = PAGE_WRITE | PAGE_READ;
458
#if defined (DEBUG_BATS)
459
                if (loglevel != 0) {
460
                    fprintf(logfile, "BAT %d match: r 0x" PADDRX
461
                            " prot=%c%c\n",
462
                            i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
463
                            ctx->prot & PAGE_WRITE ? 'W' : '-');
464
                }
465
#endif
466
                ret = 0;
467
                break;
468
            }
469
        }
470
    }
471
    if (ret < 0) {
472
#if defined (DEBUG_BATS)
473
        if (loglevel != 0) {
474
            fprintf(logfile, "no BAT match for 0x" ADDRX ":\n", virtual);
475
            for (i = 0; i < 4; i++) {
476
                BATu = &BATut[i];
477
                BATl = &BATlt[i];
478
                BEPIu = *BATu & 0xF0000000;
479
                BEPIl = *BATu & 0x0FFE0000;
480
                bl = (*BATu & 0x00001FFC) << 15;
481
                fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
482
                        " BATl 0x" ADDRX " \n\t"
483
                        "0x" ADDRX " 0x" ADDRX " 0x" ADDRX "\n",
484
                        __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
485
                        *BATu, *BATl, BEPIu, BEPIl, bl);
486
            }
487
        }
488
#endif
489
    }
490
    /* No hit */
491
    return ret;
492
}
493

    
494
/* PTE table lookup */
495
static inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h, int rw)
496
{
497
    target_ulong base, pte0, pte1;
498
    int i, good = -1;
499
    int ret, r;
500

    
501
    ret = -1; /* No entry found */
502
    base = ctx->pg_addr[h];
503
    for (i = 0; i < 8; i++) {
504
#if defined(TARGET_PPC64)
505
        if (is_64b) {
506
            pte0 = ldq_phys(base + (i * 16));
507
            pte1 =  ldq_phys(base + (i * 16) + 8);
508
            r = pte64_check(ctx, pte0, pte1, h, rw);
509
        } else
510
#endif
511
        {
512
            pte0 = ldl_phys(base + (i * 8));
513
            pte1 =  ldl_phys(base + (i * 8) + 4);
514
            r = pte32_check(ctx, pte0, pte1, h, rw);
515
        }
516
#if defined (DEBUG_MMU)
517
        if (loglevel != 0) {
518
            fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
519
                    " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
520
                    base + (i * 8), pte0, pte1,
521
                    (int)(pte0 >> 31), h, (int)((pte0 >> 6) & 1), ctx->ptem);
522
        }
523
#endif
524
        switch (r) {
525
        case -3:
526
            /* PTE inconsistency */
527
            return -1;
528
        case -2:
529
            /* Access violation */
530
            ret = -2;
531
            good = i;
532
            break;
533
        case -1:
534
        default:
535
            /* No PTE match */
536
            break;
537
        case 0:
538
            /* access granted */
539
            /* XXX: we should go on looping to check all PTEs consistency
540
             *      but if we can speed-up the whole thing as the
541
             *      result would be undefined if PTEs are not consistent.
542
             */
543
            ret = 0;
544
            good = i;
545
            goto done;
546
        }
547
    }
548
    if (good != -1) {
549
    done:
550
#if defined (DEBUG_MMU)
551
        if (loglevel != 0) {
552
            fprintf(logfile, "found PTE at addr 0x" PADDRX " prot=0x%01x "
553
                    "ret=%d\n",
554
                    ctx->raddr, ctx->prot, ret);
555
        }
556
#endif
557
        /* Update page flags */
558
        pte1 = ctx->raddr;
559
        if (pte_update_flags(ctx, &pte1, ret, rw) == 1) {
560
#if defined(TARGET_PPC64)
561
            if (is_64b) {
562
                stq_phys_notdirty(base + (good * 16) + 8, pte1);
563
            } else
564
#endif
565
            {
566
                stl_phys_notdirty(base + (good * 8) + 4, pte1);
567
            }
568
        }
569
    }
570

    
571
    return ret;
572
}
573

    
574
static int find_pte32 (mmu_ctx_t *ctx, int h, int rw)
575
{
576
    return _find_pte(ctx, 0, h, rw);
577
}
578

    
579
#if defined(TARGET_PPC64)
580
static int find_pte64 (mmu_ctx_t *ctx, int h, int rw)
581
{
582
    return _find_pte(ctx, 1, h, rw);
583
}
584
#endif
585

    
586
static inline int find_pte (CPUState *env, mmu_ctx_t *ctx, int h, int rw)
587
{
588
#if defined(TARGET_PPC64)
589
    if (PPC_MMU(env) == PPC_FLAGS_MMU_64B ||
590
        PPC_MMU(env) == PPC_FLAGS_MMU_64BRIDGE)
591
        return find_pte64(ctx, h, rw);
592
#endif
593

    
594
    return find_pte32(ctx, h, rw);
595
}
596

    
597
static inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
598
                                             int sdr_sh,
599
                                             target_phys_addr_t hash,
600
                                             target_phys_addr_t mask)
601
{
602
    return (sdr1 & ((target_ulong)(-1ULL) << sdr_sh)) | (hash & mask);
603
}
604

    
605
#if defined(TARGET_PPC64)
606
static int slb_lookup (CPUState *env, target_ulong eaddr,
607
                       target_ulong *vsid, target_ulong *page_mask, int *attr)
608
{
609
    target_phys_addr_t sr_base;
610
    target_ulong mask;
611
    uint64_t tmp64;
612
    uint32_t tmp;
613
    int n, ret;
614
    int slb_nr;
615

    
616
    ret = -5;
617
    sr_base = env->spr[SPR_ASR];
618
    mask = 0x0000000000000000ULL; /* Avoid gcc warning */
619
#if 0 /* XXX: Fix this */
620
    slb_nr = env->slb_nr;
621
#else
622
    slb_nr = 32;
623
#endif
624
    for (n = 0; n < slb_nr; n++) {
625
        tmp64 = ldq_phys(sr_base);
626
        if (tmp64 & 0x0000000008000000ULL) {
627
            /* SLB entry is valid */
628
            switch (tmp64 & 0x0000000006000000ULL) {
629
            case 0x0000000000000000ULL:
630
                /* 256 MB segment */
631
                mask = 0xFFFFFFFFF0000000ULL;
632
                break;
633
            case 0x0000000002000000ULL:
634
                /* 1 TB segment */
635
                mask = 0xFFFF000000000000ULL;
636
                break;
637
            case 0x0000000004000000ULL:
638
            case 0x0000000006000000ULL:
639
                /* Reserved => segment is invalid */
640
                continue;
641
            }
642
            if ((eaddr & mask) == (tmp64 & mask)) {
643
                /* SLB match */
644
                tmp = ldl_phys(sr_base + 8);
645
                *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
646
                *page_mask = ~mask;
647
                *attr = tmp & 0xFF;
648
                ret = 0;
649
                break;
650
            }
651
        }
652
        sr_base += 12;
653
    }
654

    
655
    return ret;
656
}
657
#endif /* defined(TARGET_PPC64) */
658

    
659
/* Perform segment based translation */
660
static int get_segment (CPUState *env, mmu_ctx_t *ctx,
661
                        target_ulong eaddr, int rw, int type)
662
{
663
    target_phys_addr_t sdr, hash, mask, sdr_mask;
664
    target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
665
#if defined(TARGET_PPC64)
666
    int attr;
667
#endif
668
    int ds, nx, vsid_sh, sdr_sh;
669
    int ret, ret2;
670

    
671
#if defined(TARGET_PPC64)
672
    if (PPC_MMU(env) == PPC_FLAGS_MMU_64B) {
673
        ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
674
        if (ret < 0)
675
            return ret;
676
        ctx->key = ((attr & 0x40) && msr_pr == 1) ||
677
            ((attr & 0x80) && msr_pr == 0) ? 1 : 0;
678
        ds = 0;
679
        nx = attr & 0x20 ? 1 : 0;
680
        vsid_mask = 0x00003FFFFFFFFF80ULL;
681
        vsid_sh = 7;
682
        sdr_sh = 18;
683
        sdr_mask = 0x3FF80;
684
    } else
685
#endif /* defined(TARGET_PPC64) */
686
    {
687
        sr = env->sr[eaddr >> 28];
688
        page_mask = 0x0FFFFFFF;
689
        ctx->key = (((sr & 0x20000000) && msr_pr == 1) ||
690
                    ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
691
        ds = sr & 0x80000000 ? 1 : 0;
692
        nx = sr & 0x10000000 ? 1 : 0;
693
        vsid = sr & 0x00FFFFFF;
694
        vsid_mask = 0x01FFFFC0;
695
        vsid_sh = 6;
696
        sdr_sh = 16;
697
        sdr_mask = 0xFFC0;
698
#if defined (DEBUG_MMU)
699
        if (loglevel != 0) {
700
            fprintf(logfile, "Check segment v=0x" ADDRX " %d 0x" ADDRX
701
                    " nip=0x" ADDRX " lr=0x" ADDRX
702
                    " ir=%d dr=%d pr=%d %d t=%d\n",
703
                    eaddr, (int)(eaddr >> 28), sr, env->nip,
704
                    env->lr, msr_ir, msr_dr, msr_pr, rw, type);
705
        }
706
        if (!ds && loglevel != 0) {
707
            fprintf(logfile, "pte segment: key=%d n=0x" ADDRX "\n",
708
                    ctx->key, sr & 0x10000000);
709
        }
710
#endif
711
    }
712
    ret = -1;
713
    if (!ds) {
714
        /* Check if instruction fetch is allowed, if needed */
715
        if (type != ACCESS_CODE || nx == 0) {
716
            /* Page address translation */
717
            pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
718
            hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
719
            /* Primary table address */
720
            sdr = env->sdr1;
721
            mask = ((sdr & 0x000001FF) << sdr_sh) | sdr_mask;
722
            ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
723
            /* Secondary table address */
724
            hash = (~hash) & vsid_mask;
725
            ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
726
#if defined(TARGET_PPC64)
727
            if (PPC_MMU(env) == PPC_FLAGS_MMU_64B ||
728
                PPC_MMU(env) == PPC_FLAGS_MMU_64BRIDGE) {
729
                /* Only 5 bits of the page index are used in the AVPN */
730
                ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
731
            } else
732
#endif
733
            {
734
                ctx->ptem = (vsid << 7) | (pgidx >> 10);
735
            }
736
            /* Initialize real address with an invalid value */
737
            ctx->raddr = (target_ulong)-1;
738
            if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_6xx)) {
739
                /* Software TLB search */
740
                ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
741
            } else {
742
#if defined (DEBUG_MMU)
743
                if (loglevel != 0) {
744
                    fprintf(logfile, "0 sdr1=0x" PADDRX " vsid=0x%06x "
745
                            "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX "\n",
746
                            sdr, (uint32_t)vsid, (uint32_t)pgidx,
747
                            (uint32_t)hash, ctx->pg_addr[0]);
748
                }
749
#endif
750
                /* Primary table lookup */
751
                ret = find_pte(env, ctx, 0, rw);
752
                if (ret < 0) {
753
                    /* Secondary table lookup */
754
#if defined (DEBUG_MMU)
755
                    if (eaddr != 0xEFFFFFFF && loglevel != 0) {
756
                        fprintf(logfile,
757
                                "1 sdr1=0x" PADDRX " vsid=0x%06x api=0x%04x "
758
                                "hash=0x%05x pg_addr=0x" PADDRX "\n",
759
                                sdr, (uint32_t)vsid, (uint32_t)pgidx,
760
                                (uint32_t)hash, ctx->pg_addr[1]);
761
                    }
762
#endif
763
                    ret2 = find_pte(env, ctx, 1, rw);
764
                    if (ret2 != -1)
765
                        ret = ret2;
766
                }
767
            }
768
        } else {
769
#if defined (DEBUG_MMU)
770
            if (loglevel != 0)
771
                fprintf(logfile, "No access allowed\n");
772
#endif
773
            ret = -3;
774
        }
775
    } else {
776
#if defined (DEBUG_MMU)
777
        if (loglevel != 0)
778
            fprintf(logfile, "direct store...\n");
779
#endif
780
        /* Direct-store segment : absolutely *BUGGY* for now */
781
        switch (type) {
782
        case ACCESS_INT:
783
            /* Integer load/store : only access allowed */
784
            break;
785
        case ACCESS_CODE:
786
            /* No code fetch is allowed in direct-store areas */
787
            return -4;
788
        case ACCESS_FLOAT:
789
            /* Floating point load/store */
790
            return -4;
791
        case ACCESS_RES:
792
            /* lwarx, ldarx or srwcx. */
793
            return -4;
794
        case ACCESS_CACHE:
795
            /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
796
            /* Should make the instruction do no-op.
797
             * As it already do no-op, it's quite easy :-)
798
             */
799
            ctx->raddr = eaddr;
800
            return 0;
801
        case ACCESS_EXT:
802
            /* eciwx or ecowx */
803
            return -4;
804
        default:
805
            if (logfile) {
806
                fprintf(logfile, "ERROR: instruction should not need "
807
                        "address translation\n");
808
            }
809
            return -4;
810
        }
811
        if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
812
            ctx->raddr = eaddr;
813
            ret = 2;
814
        } else {
815
            ret = -2;
816
        }
817
    }
818

    
819
    return ret;
820
}
821

    
822
/* Generic TLB check function for embedded PowerPC implementations */
823
static int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
824
                             target_phys_addr_t *raddrp,
825
                             target_ulong address,
826
                             uint32_t pid, int ext, int i)
827
{
828
    target_ulong mask;
829

    
830
    /* Check valid flag */
831
    if (!(tlb->prot & PAGE_VALID)) {
832
        if (loglevel != 0)
833
            fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
834
        return -1;
835
    }
836
    mask = ~(tlb->size - 1);
837
    if (loglevel != 0) {
838
        fprintf(logfile, "%s: TLB %d address " ADDRX " PID %d <=> "
839
                ADDRX " " ADDRX " %d\n",
840
                __func__, i, address, pid, tlb->EPN, mask, (int)tlb->PID);
841
    }
842
    /* Check PID */
843
    if (tlb->PID != 0 && tlb->PID != pid)
844
        return -1;
845
    /* Check effective address */
846
    if ((address & mask) != tlb->EPN)
847
        return -1;
848
    *raddrp = (tlb->RPN & mask) | (address & ~mask);
849
    if (ext) {
850
        /* Extend the physical address to 36 bits */
851
        *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
852
    }
853

    
854
    return 0;
855
}
856

    
857
/* Generic TLB search function for PowerPC embedded implementations */
858
int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
859
{
860
    ppcemb_tlb_t *tlb;
861
    target_phys_addr_t raddr;
862
    int i, ret;
863

    
864
    /* Default return value is no match */
865
    ret = -1;
866
    for (i = 0; i < 64; i++) {
867
        tlb = &env->tlb[i].tlbe;
868
        if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
869
            ret = i;
870
            break;
871
        }
872
    }
873

    
874
    return ret;
875
}
876

    
877
/* Helpers specific to PowerPC 40x implementations */
878
void ppc4xx_tlb_invalidate_all (CPUState *env)
879
{
880
    ppcemb_tlb_t *tlb;
881
    int i;
882

    
883
    for (i = 0; i < env->nb_tlb; i++) {
884
        tlb = &env->tlb[i].tlbe;
885
        if (tlb->prot & PAGE_VALID) {
886
#if 0 // XXX: TLB have variable sizes then we flush all Qemu TLB.
887
            end = tlb->EPN + tlb->size;
888
            for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
889
                tlb_flush_page(env, page);
890
#endif
891
            tlb->prot &= ~PAGE_VALID;
892
        }
893
    }
894
    tlb_flush(env, 1);
895
}
896

    
897
int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
898
                                 target_ulong address, int rw, int access_type)
899
{
900
    ppcemb_tlb_t *tlb;
901
    target_phys_addr_t raddr;
902
    int i, ret, zsel, zpr;
903

    
904
    ret = -1;
905
    raddr = -1;
906
    for (i = 0; i < env->nb_tlb; i++) {
907
        tlb = &env->tlb[i].tlbe;
908
        if (ppcemb_tlb_check(env, tlb, &raddr, address,
909
                             env->spr[SPR_40x_PID], 0, i) < 0)
910
            continue;
911
        zsel = (tlb->attr >> 4) & 0xF;
912
        zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
913
        if (loglevel != 0) {
914
            fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
915
                    __func__, i, zsel, zpr, rw, tlb->attr);
916
        }
917
        if (access_type == ACCESS_CODE) {
918
            /* Check execute enable bit */
919
            switch (zpr) {
920
            case 0x2:
921
                if (msr_pr)
922
                    goto check_exec_perm;
923
                goto exec_granted;
924
            case 0x0:
925
                if (msr_pr) {
926
                    ctx->prot = 0;
927
                    ret = -3;
928
                    break;
929
                }
930
                /* No break here */
931
            case 0x1:
932
            check_exec_perm:
933
                /* Check from TLB entry */
934
                if (!(tlb->prot & PAGE_EXEC)) {
935
                    ret = -3;
936
                } else {
937
                    if (tlb->prot & PAGE_WRITE) {
938
                        ctx->prot = PAGE_READ | PAGE_WRITE;
939
                    } else {
940
                        ctx->prot = PAGE_READ;
941
                    }
942
                    ret = 0;
943
                }
944
                break;
945
            case 0x3:
946
            exec_granted:
947
                /* All accesses granted */
948
                ctx->prot = PAGE_READ | PAGE_WRITE;
949
                ret = 0;
950
                break;
951
            }
952
        } else {
953
            switch (zpr) {
954
            case 0x2:
955
                if (msr_pr)
956
                    goto check_rw_perm;
957
                goto rw_granted;
958
            case 0x0:
959
                if (msr_pr) {
960
                    ctx->prot = 0;
961
                    ret = -2;
962
                    break;
963
                }
964
                /* No break here */
965
            case 0x1:
966
            check_rw_perm:
967
                /* Check from TLB entry */
968
                /* Check write protection bit */
969
                if (tlb->prot & PAGE_WRITE) {
970
                    ctx->prot = PAGE_READ | PAGE_WRITE;
971
                    ret = 0;
972
                } else {
973
                    ctx->prot = PAGE_READ;
974
                    if (rw)
975
                        ret = -2;
976
                    else
977
                        ret = 0;
978
                }
979
                break;
980
            case 0x3:
981
            rw_granted:
982
                /* All accesses granted */
983
                ctx->prot = PAGE_READ | PAGE_WRITE;
984
                ret = 0;
985
                break;
986
            }
987
        }
988
        if (ret >= 0) {
989
            ctx->raddr = raddr;
990
            if (loglevel != 0) {
991
                fprintf(logfile, "%s: access granted " ADDRX " => " REGX
992
                        " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
993
                        ret);
994
            }
995
            return 0;
996
        }
997
    }
998
    if (loglevel != 0) {
999
        fprintf(logfile, "%s: access refused " ADDRX " => " REGX
1000
                " %d %d\n", __func__, address, raddr, ctx->prot,
1001
                ret);
1002
    }
1003

    
1004
    return ret;
1005
}
1006

    
1007
void store_40x_sler (CPUPPCState *env, uint32_t val)
1008
{
1009
    /* XXX: TO BE FIXED */
1010
    if (val != 0x00000000) {
1011
        cpu_abort(env, "Little-endian regions are not supported by now\n");
1012
    }
1013
    env->spr[SPR_405_SLER] = val;
1014
}
1015

    
1016
int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1017
                                   target_ulong address, int rw,
1018
                                   int access_type)
1019
{
1020
    ppcemb_tlb_t *tlb;
1021
    target_phys_addr_t raddr;
1022
    int i, prot, ret;
1023

    
1024
    ret = -1;
1025
    raddr = -1;
1026
    for (i = 0; i < env->nb_tlb; i++) {
1027
        tlb = &env->tlb[i].tlbe;
1028
        if (ppcemb_tlb_check(env, tlb, &raddr, address,
1029
                             env->spr[SPR_BOOKE_PID], 1, i) < 0)
1030
            continue;
1031
        if (msr_pr)
1032
            prot = tlb->prot & 0xF;
1033
        else
1034
            prot = (tlb->prot >> 4) & 0xF;
1035
        /* Check the address space */
1036
        if (access_type == ACCESS_CODE) {
1037
            if (msr_is != (tlb->attr & 1))
1038
                continue;
1039
            ctx->prot = prot;
1040
            if (prot & PAGE_EXEC) {
1041
                ret = 0;
1042
                break;
1043
            }
1044
            ret = -3;
1045
        } else {
1046
            if (msr_ds != (tlb->attr & 1))
1047
                continue;
1048
            ctx->prot = prot;
1049
            if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1050
                ret = 0;
1051
                break;
1052
            }
1053
            ret = -2;
1054
        }
1055
    }
1056
    if (ret >= 0)
1057
        ctx->raddr = raddr;
1058

    
1059
    return ret;
1060
}
1061

    
1062
static int check_physical (CPUState *env, mmu_ctx_t *ctx,
1063
                           target_ulong eaddr, int rw)
1064
{
1065
    int in_plb, ret;
1066

    
1067
    ctx->raddr = eaddr;
1068
    ctx->prot = PAGE_READ;
1069
    ret = 0;
1070
    switch (PPC_MMU(env)) {
1071
    case PPC_FLAGS_MMU_32B:
1072
    case PPC_FLAGS_MMU_SOFT_6xx:
1073
    case PPC_FLAGS_MMU_601:
1074
    case PPC_FLAGS_MMU_SOFT_4xx:
1075
        ctx->prot |= PAGE_WRITE;
1076
        break;
1077
#if defined(TARGET_PPC64)
1078
    case PPC_FLAGS_MMU_64B:
1079
    case PPC_FLAGS_MMU_64BRIDGE:
1080
#endif
1081
        /* Real address are 60 bits long */
1082
        ctx->raddr &= 0x0FFFFFFFFFFFFFFFUL;
1083
        ctx->prot |= PAGE_WRITE;
1084
        break;
1085
    case PPC_FLAGS_MMU_403:
1086
        if (unlikely(msr_pe != 0)) {
1087
            /* 403 family add some particular protections,
1088
             * using PBL/PBU registers for accesses with no translation.
1089
             */
1090
            in_plb =
1091
                /* Check PLB validity */
1092
                (env->pb[0] < env->pb[1] &&
1093
                 /* and address in plb area */
1094
                 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1095
                (env->pb[2] < env->pb[3] &&
1096
                 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1097
            if (in_plb ^ msr_px) {
1098
                /* Access in protected area */
1099
                if (rw == 1) {
1100
                    /* Access is not allowed */
1101
                    ret = -2;
1102
                }
1103
            } else {
1104
                /* Read-write access is allowed */
1105
                ctx->prot |= PAGE_WRITE;
1106
            }
1107
        }
1108
    case PPC_FLAGS_MMU_BOOKE:
1109
        ctx->prot |= PAGE_WRITE;
1110
        break;
1111
    case PPC_FLAGS_MMU_BOOKE_FSL:
1112
        /* XXX: TODO */
1113
        cpu_abort(env, "BookE FSL MMU model not implemented\n");
1114
        break;
1115
    default:
1116
        cpu_abort(env, "Unknown or invalid MMU model\n");
1117
        return -1;
1118
    }
1119

    
1120
    return ret;
1121
}
1122

    
1123
int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1124
                          int rw, int access_type, int check_BATs)
1125
{
1126
    int ret;
1127
#if 0
1128
    if (loglevel != 0) {
1129
        fprintf(logfile, "%s\n", __func__);
1130
    }
1131
#endif
1132
    if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1133
        (access_type != ACCESS_CODE && msr_dr == 0)) {
1134
        /* No address translation */
1135
        ret = check_physical(env, ctx, eaddr, rw);
1136
    } else {
1137
        ret = -1;
1138
        switch (PPC_MMU(env)) {
1139
        case PPC_FLAGS_MMU_32B:
1140
        case PPC_FLAGS_MMU_SOFT_6xx:
1141
            /* Try to find a BAT */
1142
            if (check_BATs)
1143
                ret = get_bat(env, ctx, eaddr, rw, access_type);
1144
            /* No break here */
1145
#if defined(TARGET_PPC64)
1146
        case PPC_FLAGS_MMU_64B:
1147
        case PPC_FLAGS_MMU_64BRIDGE:
1148
#endif
1149
            if (ret < 0) {
1150
                /* We didn't match any BAT entry or don't have BATs */
1151
                ret = get_segment(env, ctx, eaddr, rw, access_type);
1152
            }
1153
            break;
1154
        case PPC_FLAGS_MMU_SOFT_4xx:
1155
        case PPC_FLAGS_MMU_403:
1156
            ret = mmu40x_get_physical_address(env, ctx, eaddr,
1157
                                              rw, access_type);
1158
            break;
1159
        case PPC_FLAGS_MMU_601:
1160
            /* XXX: TODO */
1161
            cpu_abort(env, "601 MMU model not implemented\n");
1162
            return -1;
1163
        case PPC_FLAGS_MMU_BOOKE:
1164
            ret = mmubooke_get_physical_address(env, ctx, eaddr,
1165
                                                rw, access_type);
1166
            break;
1167
        case PPC_FLAGS_MMU_BOOKE_FSL:
1168
            /* XXX: TODO */
1169
            cpu_abort(env, "BookE FSL MMU model not implemented\n");
1170
            return -1;
1171
        default:
1172
            cpu_abort(env, "Unknown or invalid MMU model\n");
1173
            return -1;
1174
        }
1175
    }
1176
#if 0
1177
    if (loglevel != 0) {
1178
        fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1179
                __func__, eaddr, ret, ctx->raddr);
1180
    }
1181
#endif
1182

    
1183
    return ret;
1184
}
1185

    
1186
target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1187
{
1188
    mmu_ctx_t ctx;
1189

    
1190
    if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT, 1) != 0))
1191
        return -1;
1192

    
1193
    return ctx.raddr & TARGET_PAGE_MASK;
1194
}
1195

    
1196
/* Perform address translation */
1197
int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1198
                              int is_user, int is_softmmu)
1199
{
1200
    mmu_ctx_t ctx;
1201
    int exception = 0, error_code = 0;
1202
    int access_type;
1203
    int ret = 0;
1204

    
1205
    if (rw == 2) {
1206
        /* code access */
1207
        rw = 0;
1208
        access_type = ACCESS_CODE;
1209
    } else {
1210
        /* data access */
1211
        /* XXX: put correct access by using cpu_restore_state()
1212
           correctly */
1213
        access_type = ACCESS_INT;
1214
        //        access_type = env->access_type;
1215
    }
1216
    ret = get_physical_address(env, &ctx, address, rw, access_type, 1);
1217
    if (ret == 0) {
1218
        ret = tlb_set_page(env, address & TARGET_PAGE_MASK,
1219
                           ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1220
                           is_user, is_softmmu);
1221
    } else if (ret < 0) {
1222
#if defined (DEBUG_MMU)
1223
        if (loglevel != 0)
1224
            cpu_dump_state(env, logfile, fprintf, 0);
1225
#endif
1226
        if (access_type == ACCESS_CODE) {
1227
            exception = EXCP_ISI;
1228
            switch (ret) {
1229
            case -1:
1230
                /* No matches in page tables or TLB */
1231
                switch (PPC_MMU(env)) {
1232
                case PPC_FLAGS_MMU_SOFT_6xx:
1233
                    exception = EXCP_I_TLBMISS;
1234
                    env->spr[SPR_IMISS] = address;
1235
                    env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1236
                    error_code = 1 << 18;
1237
                    goto tlb_miss;
1238
                case PPC_FLAGS_MMU_SOFT_4xx:
1239
                case PPC_FLAGS_MMU_403:
1240
                    exception = EXCP_40x_ITLBMISS;
1241
                    error_code = 0;
1242
                    env->spr[SPR_40x_DEAR] = address;
1243
                    env->spr[SPR_40x_ESR] = 0x00000000;
1244
                    break;
1245
                case PPC_FLAGS_MMU_32B:
1246
                    error_code = 0x40000000;
1247
                    break;
1248
#if defined(TARGET_PPC64)
1249
                case PPC_FLAGS_MMU_64B:
1250
                    /* XXX: TODO */
1251
                    cpu_abort(env, "MMU model not implemented\n");
1252
                    return -1;
1253
                case PPC_FLAGS_MMU_64BRIDGE:
1254
                    /* XXX: TODO */
1255
                    cpu_abort(env, "MMU model not implemented\n");
1256
                    return -1;
1257
#endif
1258
                case PPC_FLAGS_MMU_601:
1259
                    /* XXX: TODO */
1260
                    cpu_abort(env, "MMU model not implemented\n");
1261
                    return -1;
1262
                case PPC_FLAGS_MMU_BOOKE:
1263
                    /* XXX: TODO */
1264
                    cpu_abort(env, "MMU model not implemented\n");
1265
                    return -1;
1266
                case PPC_FLAGS_MMU_BOOKE_FSL:
1267
                    /* XXX: TODO */
1268
                    cpu_abort(env, "MMU model not implemented\n");
1269
                    return -1;
1270
                default:
1271
                    cpu_abort(env, "Unknown or invalid MMU model\n");
1272
                    return -1;
1273
                }
1274
                break;
1275
            case -2:
1276
                /* Access rights violation */
1277
                error_code = 0x08000000;
1278
                break;
1279
            case -3:
1280
                /* No execute protection violation */
1281
                error_code = 0x10000000;
1282
                break;
1283
            case -4:
1284
                /* Direct store exception */
1285
                /* No code fetch is allowed in direct-store areas */
1286
                error_code = 0x10000000;
1287
                break;
1288
            case -5:
1289
                /* No match in segment table */
1290
                exception = EXCP_ISEG;
1291
                error_code = 0;
1292
                break;
1293
            }
1294
        } else {
1295
            exception = EXCP_DSI;
1296
            switch (ret) {
1297
            case -1:
1298
                /* No matches in page tables or TLB */
1299
                switch (PPC_MMU(env)) {
1300
                case PPC_FLAGS_MMU_SOFT_6xx:
1301
                    if (rw == 1) {
1302
                        exception = EXCP_DS_TLBMISS;
1303
                        error_code = 1 << 16;
1304
                    } else {
1305
                        exception = EXCP_DL_TLBMISS;
1306
                        error_code = 0;
1307
                    }
1308
                    env->spr[SPR_DMISS] = address;
1309
                    env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1310
                tlb_miss:
1311
                    error_code |= ctx.key << 19;
1312
                    env->spr[SPR_HASH1] = ctx.pg_addr[0];
1313
                    env->spr[SPR_HASH2] = ctx.pg_addr[1];
1314
                    /* Do not alter DAR nor DSISR */
1315
                    goto out;
1316
                case PPC_FLAGS_MMU_SOFT_4xx:
1317
                case PPC_FLAGS_MMU_403:
1318
                    exception = EXCP_40x_DTLBMISS;
1319
                    error_code = 0;
1320
                    env->spr[SPR_40x_DEAR] = address;
1321
                    if (rw)
1322
                        env->spr[SPR_40x_ESR] = 0x00800000;
1323
                    else
1324
                        env->spr[SPR_40x_ESR] = 0x00000000;
1325
                    break;
1326
                case PPC_FLAGS_MMU_32B:
1327
                    error_code = 0x40000000;
1328
                    break;
1329
#if defined(TARGET_PPC64)
1330
                case PPC_FLAGS_MMU_64B:
1331
                    /* XXX: TODO */
1332
                    cpu_abort(env, "MMU model not implemented\n");
1333
                    return -1;
1334
                case PPC_FLAGS_MMU_64BRIDGE:
1335
                    /* XXX: TODO */
1336
                    cpu_abort(env, "MMU model not implemented\n");
1337
                    return -1;
1338
#endif
1339
                case PPC_FLAGS_MMU_601:
1340
                    /* XXX: TODO */
1341
                    cpu_abort(env, "MMU model not implemented\n");
1342
                    return -1;
1343
                case PPC_FLAGS_MMU_BOOKE:
1344
                    /* XXX: TODO */
1345
                    cpu_abort(env, "MMU model not implemented\n");
1346
                    return -1;
1347
                case PPC_FLAGS_MMU_BOOKE_FSL:
1348
                    /* XXX: TODO */
1349
                    cpu_abort(env, "MMU model not implemented\n");
1350
                    return -1;
1351
                default:
1352
                    cpu_abort(env, "Unknown or invalid MMU model\n");
1353
                    return -1;
1354
                }
1355
                break;
1356
            case -2:
1357
                /* Access rights violation */
1358
                error_code = 0x08000000;
1359
                break;
1360
            case -4:
1361
                /* Direct store exception */
1362
                switch (access_type) {
1363
                case ACCESS_FLOAT:
1364
                    /* Floating point load/store */
1365
                    exception = EXCP_ALIGN;
1366
                    error_code = EXCP_ALIGN_FP;
1367
                    break;
1368
                case ACCESS_RES:
1369
                    /* lwarx, ldarx or srwcx. */
1370
                    error_code = 0x04000000;
1371
                    break;
1372
                case ACCESS_EXT:
1373
                    /* eciwx or ecowx */
1374
                    error_code = 0x04100000;
1375
                    break;
1376
                default:
1377
                    printf("DSI: invalid exception (%d)\n", ret);
1378
                    exception = EXCP_PROGRAM;
1379
                    error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
1380
                    break;
1381
                }
1382
                break;
1383
            case -5:
1384
                /* No match in segment table */
1385
                exception = EXCP_DSEG;
1386
                error_code = 0;
1387
                break;
1388
            }
1389
            if (exception == EXCP_DSI && rw == 1)
1390
                error_code |= 0x02000000;
1391
            /* Store fault address */
1392
            env->spr[SPR_DAR] = address;
1393
            env->spr[SPR_DSISR] = error_code;
1394
        }
1395
    out:
1396
#if 0
1397
        printf("%s: set exception to %d %02x\n",
1398
               __func__, exception, error_code);
1399
#endif
1400
        env->exception_index = exception;
1401
        env->error_code = error_code;
1402
        ret = 1;
1403
    }
1404

    
1405
    return ret;
1406
}
1407

    
1408
/*****************************************************************************/
1409
/* BATs management */
1410
#if !defined(FLUSH_ALL_TLBS)
1411
static inline void do_invalidate_BAT (CPUPPCState *env,
1412
                                      target_ulong BATu, target_ulong mask)
1413
{
1414
    target_ulong base, end, page;
1415

    
1416
    base = BATu & ~0x0001FFFF;
1417
    end = base + mask + 0x00020000;
1418
#if defined (DEBUG_BATS)
1419
    if (loglevel != 0) {
1420
        fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1421
                base, end, mask);
1422
    }
1423
#endif
1424
    for (page = base; page != end; page += TARGET_PAGE_SIZE)
1425
        tlb_flush_page(env, page);
1426
#if defined (DEBUG_BATS)
1427
    if (loglevel != 0)
1428
        fprintf(logfile, "Flush done\n");
1429
#endif
1430
}
1431
#endif
1432

    
1433
static inline void dump_store_bat (CPUPPCState *env, char ID, int ul, int nr,
1434
                                   target_ulong value)
1435
{
1436
#if defined (DEBUG_BATS)
1437
    if (loglevel != 0) {
1438
        fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1439
                ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1440
    }
1441
#endif
1442
}
1443

    
1444
target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1445
{
1446
    return env->IBAT[0][nr];
1447
}
1448

    
1449
target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1450
{
1451
    return env->IBAT[1][nr];
1452
}
1453

    
1454
void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1455
{
1456
    target_ulong mask;
1457

    
1458
    dump_store_bat(env, 'I', 0, nr, value);
1459
    if (env->IBAT[0][nr] != value) {
1460
        mask = (value << 15) & 0x0FFE0000UL;
1461
#if !defined(FLUSH_ALL_TLBS)
1462
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1463
#endif
1464
        /* When storing valid upper BAT, mask BEPI and BRPN
1465
         * and invalidate all TLBs covered by this BAT
1466
         */
1467
        mask = (value << 15) & 0x0FFE0000UL;
1468
        env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1469
            (value & ~0x0001FFFFUL & ~mask);
1470
        env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1471
            (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1472
#if !defined(FLUSH_ALL_TLBS)
1473
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1474
#else
1475
        tlb_flush(env, 1);
1476
#endif
1477
    }
1478
}
1479

    
1480
void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1481
{
1482
    dump_store_bat(env, 'I', 1, nr, value);
1483
    env->IBAT[1][nr] = value;
1484
}
1485

    
1486
target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1487
{
1488
    return env->DBAT[0][nr];
1489
}
1490

    
1491
target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1492
{
1493
    return env->DBAT[1][nr];
1494
}
1495

    
1496
void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1497
{
1498
    target_ulong mask;
1499

    
1500
    dump_store_bat(env, 'D', 0, nr, value);
1501
    if (env->DBAT[0][nr] != value) {
1502
        /* When storing valid upper BAT, mask BEPI and BRPN
1503
         * and invalidate all TLBs covered by this BAT
1504
         */
1505
        mask = (value << 15) & 0x0FFE0000UL;
1506
#if !defined(FLUSH_ALL_TLBS)
1507
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1508
#endif
1509
        mask = (value << 15) & 0x0FFE0000UL;
1510
        env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1511
            (value & ~0x0001FFFFUL & ~mask);
1512
        env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1513
            (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1514
#if !defined(FLUSH_ALL_TLBS)
1515
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1516
#else
1517
        tlb_flush(env, 1);
1518
#endif
1519
    }
1520
}
1521

    
1522
void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1523
{
1524
    dump_store_bat(env, 'D', 1, nr, value);
1525
    env->DBAT[1][nr] = value;
1526
}
1527

    
1528

    
1529
/*****************************************************************************/
1530
/* TLB management */
1531
void ppc_tlb_invalidate_all (CPUPPCState *env)
1532
{
1533
    if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_6xx)) {
1534
        ppc6xx_tlb_invalidate_all(env);
1535
    } else if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_4xx)) {
1536
        ppc4xx_tlb_invalidate_all(env);
1537
    } else {
1538
        tlb_flush(env, 1);
1539
    }
1540
}
1541

    
1542
/*****************************************************************************/
1543
/* Special registers manipulation */
1544
#if defined(TARGET_PPC64)
1545
target_ulong ppc_load_asr (CPUPPCState *env)
1546
{
1547
    return env->asr;
1548
}
1549

    
1550
void ppc_store_asr (CPUPPCState *env, target_ulong value)
1551
{
1552
    if (env->asr != value) {
1553
        env->asr = value;
1554
        tlb_flush(env, 1);
1555
    }
1556
}
1557
#endif
1558

    
1559
target_ulong do_load_sdr1 (CPUPPCState *env)
1560
{
1561
    return env->sdr1;
1562
}
1563

    
1564
void do_store_sdr1 (CPUPPCState *env, target_ulong value)
1565
{
1566
#if defined (DEBUG_MMU)
1567
    if (loglevel != 0) {
1568
        fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
1569
    }
1570
#endif
1571
    if (env->sdr1 != value) {
1572
        env->sdr1 = value;
1573
        tlb_flush(env, 1);
1574
    }
1575
}
1576

    
1577
target_ulong do_load_sr (CPUPPCState *env, int srnum)
1578
{
1579
    return env->sr[srnum];
1580
}
1581

    
1582
void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
1583
{
1584
#if defined (DEBUG_MMU)
1585
    if (loglevel != 0) {
1586
        fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
1587
                __func__, srnum, value, env->sr[srnum]);
1588
    }
1589
#endif
1590
    if (env->sr[srnum] != value) {
1591
        env->sr[srnum] = value;
1592
#if !defined(FLUSH_ALL_TLBS) && 0
1593
        {
1594
            target_ulong page, end;
1595
            /* Invalidate 256 MB of virtual memory */
1596
            page = (16 << 20) * srnum;
1597
            end = page + (16 << 20);
1598
            for (; page != end; page += TARGET_PAGE_SIZE)
1599
                tlb_flush_page(env, page);
1600
        }
1601
#else
1602
        tlb_flush(env, 1);
1603
#endif
1604
    }
1605
}
1606
#endif /* !defined (CONFIG_USER_ONLY) */
1607

    
1608
uint32_t ppc_load_xer (CPUPPCState *env)
1609
{
1610
    return (xer_so << XER_SO) |
1611
        (xer_ov << XER_OV) |
1612
        (xer_ca << XER_CA) |
1613
        (xer_bc << XER_BC) |
1614
        (xer_cmp << XER_CMP);
1615
}
1616

    
1617
void ppc_store_xer (CPUPPCState *env, uint32_t value)
1618
{
1619
    xer_so = (value >> XER_SO) & 0x01;
1620
    xer_ov = (value >> XER_OV) & 0x01;
1621
    xer_ca = (value >> XER_CA) & 0x01;
1622
    xer_cmp = (value >> XER_CMP) & 0xFF;
1623
    xer_bc = (value >> XER_BC) & 0x7F;
1624
}
1625

    
1626
/* Swap temporary saved registers with GPRs */
1627
static inline void swap_gpr_tgpr (CPUPPCState *env)
1628
{
1629
    ppc_gpr_t tmp;
1630

    
1631
    tmp = env->gpr[0];
1632
    env->gpr[0] = env->tgpr[0];
1633
    env->tgpr[0] = tmp;
1634
    tmp = env->gpr[1];
1635
    env->gpr[1] = env->tgpr[1];
1636
    env->tgpr[1] = tmp;
1637
    tmp = env->gpr[2];
1638
    env->gpr[2] = env->tgpr[2];
1639
    env->tgpr[2] = tmp;
1640
    tmp = env->gpr[3];
1641
    env->gpr[3] = env->tgpr[3];
1642
    env->tgpr[3] = tmp;
1643
}
1644

    
1645
/* GDBstub can read and write MSR... */
1646
target_ulong do_load_msr (CPUPPCState *env)
1647
{
1648
    return
1649
#if defined (TARGET_PPC64)
1650
        ((target_ulong)msr_sf   << MSR_SF)   |
1651
        ((target_ulong)msr_isf  << MSR_ISF)  |
1652
        ((target_ulong)msr_hv   << MSR_HV)   |
1653
#endif
1654
        ((target_ulong)msr_ucle << MSR_UCLE) |
1655
        ((target_ulong)msr_vr   << MSR_VR)   | /* VR / SPE */
1656
        ((target_ulong)msr_ap   << MSR_AP)   |
1657
        ((target_ulong)msr_sa   << MSR_SA)   |
1658
        ((target_ulong)msr_key  << MSR_KEY)  |
1659
        ((target_ulong)msr_pow  << MSR_POW)  | /* POW / WE */
1660
        ((target_ulong)msr_tlb  << MSR_TLB)  | /* TLB / TGPE / CE */
1661
        ((target_ulong)msr_ile  << MSR_ILE)  |
1662
        ((target_ulong)msr_ee   << MSR_EE)   |
1663
        ((target_ulong)msr_pr   << MSR_PR)   |
1664
        ((target_ulong)msr_fp   << MSR_FP)   |
1665
        ((target_ulong)msr_me   << MSR_ME)   |
1666
        ((target_ulong)msr_fe0  << MSR_FE0)  |
1667
        ((target_ulong)msr_se   << MSR_SE)   | /* SE / DWE / UBLE */
1668
        ((target_ulong)msr_be   << MSR_BE)   | /* BE / DE */
1669
        ((target_ulong)msr_fe1  << MSR_FE1)  |
1670
        ((target_ulong)msr_al   << MSR_AL)   |
1671
        ((target_ulong)msr_ip   << MSR_IP)   |
1672
        ((target_ulong)msr_ir   << MSR_IR)   | /* IR / IS */
1673
        ((target_ulong)msr_dr   << MSR_DR)   | /* DR / DS */
1674
        ((target_ulong)msr_pe   << MSR_PE)   | /* PE / EP */
1675
        ((target_ulong)msr_px   << MSR_PX)   | /* PX / PMM */
1676
        ((target_ulong)msr_ri   << MSR_RI)   |
1677
        ((target_ulong)msr_le   << MSR_LE);
1678
}
1679

    
1680
void do_store_msr (CPUPPCState *env, target_ulong value)
1681
{
1682
    int enter_pm;
1683

    
1684
    value &= env->msr_mask;
1685
    if (((value >> MSR_IR) & 1) != msr_ir ||
1686
        ((value >> MSR_DR) & 1) != msr_dr) {
1687
        /* Flush all tlb when changing translation mode */
1688
        tlb_flush(env, 1);
1689
        env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1690
    }
1691
#if 0
1692
    if (loglevel != 0) {
1693
        fprintf(logfile, "%s: T0 %08lx\n", __func__, value);
1694
    }
1695
#endif
1696
    switch (PPC_EXCP(env)) {
1697
    case PPC_FLAGS_EXCP_602:
1698
    case PPC_FLAGS_EXCP_603:
1699
        if (((value >> MSR_TGPR) & 1) != msr_tgpr) {
1700
            /* Swap temporary saved registers with GPRs */
1701
            swap_gpr_tgpr(env);
1702
        }
1703
        break;
1704
    default:
1705
        break;
1706
    }
1707
#if defined (TARGET_PPC64)
1708
    msr_sf   = (value >> MSR_SF)   & 1;
1709
    msr_isf  = (value >> MSR_ISF)  & 1;
1710
    msr_hv   = (value >> MSR_HV)   & 1;
1711
#endif
1712
    msr_ucle = (value >> MSR_UCLE) & 1;
1713
    msr_vr   = (value >> MSR_VR)   & 1; /* VR / SPE */
1714
    msr_ap   = (value >> MSR_AP)   & 1;
1715
    msr_sa   = (value >> MSR_SA)   & 1;
1716
    msr_key  = (value >> MSR_KEY)  & 1;
1717
    msr_pow  = (value >> MSR_POW)  & 1; /* POW / WE */
1718
    msr_tlb  = (value >> MSR_TLB)  & 1; /* TLB / TGPR / CE */
1719
    msr_ile  = (value >> MSR_ILE)  & 1;
1720
    msr_ee   = (value >> MSR_EE)   & 1;
1721
    msr_pr   = (value >> MSR_PR)   & 1;
1722
    msr_fp   = (value >> MSR_FP)   & 1;
1723
    msr_me   = (value >> MSR_ME)   & 1;
1724
    msr_fe0  = (value >> MSR_FE0)  & 1;
1725
    msr_se   = (value >> MSR_SE)   & 1; /* SE / DWE / UBLE */
1726
    msr_be   = (value >> MSR_BE)   & 1; /* BE / DE */
1727
    msr_fe1  = (value >> MSR_FE1)  & 1;
1728
    msr_al   = (value >> MSR_AL)   & 1;
1729
    msr_ip   = (value >> MSR_IP)   & 1;
1730
    msr_ir   = (value >> MSR_IR)   & 1; /* IR / IS */
1731
    msr_dr   = (value >> MSR_DR)   & 1; /* DR / DS */
1732
    msr_pe   = (value >> MSR_PE)   & 1; /* PE / EP */
1733
    msr_px   = (value >> MSR_PX)   & 1; /* PX / PMM */
1734
    msr_ri   = (value >> MSR_RI)   & 1;
1735
    msr_le   = (value >> MSR_LE)   & 1;
1736
    do_compute_hflags(env);
1737

    
1738
    enter_pm = 0;
1739
    switch (PPC_EXCP(env)) {
1740
    case PPC_FLAGS_EXCP_603:
1741
        /* Don't handle SLEEP mode: we should disable all clocks...
1742
         * No dynamic power-management.
1743
         */
1744
        if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00C00000) != 0)
1745
            enter_pm = 1;
1746
        break;
1747
    case PPC_FLAGS_EXCP_604:
1748
        if (msr_pow == 1)
1749
            enter_pm = 1;
1750
        break;
1751
    case PPC_FLAGS_EXCP_7x0:
1752
        if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00E00000) != 0)
1753
            enter_pm = 1;
1754
        break;
1755
    default:
1756
        break;
1757
    }
1758
    if (enter_pm) {
1759
        if (likely(!env->halted)) {
1760
            /* power save: exit cpu loop */
1761
            env->halted = 1;
1762
            env->exception_index = EXCP_HLT;
1763
            cpu_loop_exit();
1764
        }
1765
    }
1766
}
1767

    
1768
#if defined(TARGET_PPC64)
1769
void ppc_store_msr_32 (CPUPPCState *env, uint32_t value)
1770
{
1771
    do_store_msr(env,
1772
                 (do_load_msr(env) & ~0xFFFFFFFFULL) | (value & 0xFFFFFFFF));
1773
}
1774
#endif
1775

    
1776
void do_compute_hflags (CPUPPCState *env)
1777
{
1778
    /* Compute current hflags */
1779
    env->hflags = (msr_cm << MSR_CM) | (msr_vr << MSR_VR) |
1780
        (msr_ap << MSR_AP) | (msr_sa << MSR_SA) | (msr_pr << MSR_PR) |
1781
        (msr_fp << MSR_FP) | (msr_fe0 << MSR_FE0) | (msr_se << MSR_SE) |
1782
        (msr_be << MSR_BE) | (msr_fe1 << MSR_FE1) | (msr_le << MSR_LE);
1783
#if defined (TARGET_PPC64)
1784
    /* No care here: PowerPC 64 MSR_SF means the same as MSR_CM for BookE */
1785
    env->hflags |= (msr_sf << (MSR_SF - 32)) | (msr_hv << (MSR_HV - 32));
1786
#endif
1787
}
1788

    
1789
/*****************************************************************************/
1790
/* Exception processing */
1791
#if defined (CONFIG_USER_ONLY)
1792
void do_interrupt (CPUState *env)
1793
{
1794
    env->exception_index = -1;
1795
}
1796

    
1797
void ppc_hw_interrupt (CPUState *env)
1798
{
1799
    env->exception_index = -1;
1800
}
1801
#else /* defined (CONFIG_USER_ONLY) */
1802
static void dump_syscall (CPUState *env)
1803
{
1804
    fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
1805
            " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
1806
            env->gpr[0], env->gpr[3], env->gpr[4],
1807
            env->gpr[5], env->gpr[6], env->nip);
1808
}
1809

    
1810
void do_interrupt (CPUState *env)
1811
{
1812
    target_ulong msr, *srr_0, *srr_1, *asrr_0, *asrr_1;
1813
    int excp, idx;
1814

    
1815
    excp = env->exception_index;
1816
    msr = do_load_msr(env);
1817
    /* The default is to use SRR0 & SRR1 to save the exception context */
1818
    srr_0 = &env->spr[SPR_SRR0];
1819
    srr_1 = &env->spr[SPR_SRR1];
1820
    asrr_0 = NULL;
1821
    asrr_1 = NULL;
1822
#if defined (DEBUG_EXCEPTIONS)
1823
    if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1) {
1824
        if (loglevel != 0) {
1825
            fprintf(logfile,
1826
                    "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
1827
                    env->nip, excp, env->error_code);
1828
            cpu_dump_state(env, logfile, fprintf, 0);
1829
        }
1830
    }
1831
#endif
1832
    if (loglevel & CPU_LOG_INT) {
1833
        fprintf(logfile, "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
1834
                env->nip, excp, env->error_code);
1835
    }
1836
    msr_pow = 0;
1837
    idx = -1;
1838
    /* Generate informations in save/restore registers */
1839
    switch (excp) {
1840
    /* Generic PowerPC exceptions */
1841
    case EXCP_RESET: /* 0x0100 */
1842
        switch (PPC_EXCP(env)) {
1843
        case PPC_FLAGS_EXCP_40x:
1844
            srr_0 = &env->spr[SPR_40x_SRR2];
1845
            srr_1 = &env->spr[SPR_40x_SRR3];
1846
            break;
1847
        case PPC_FLAGS_EXCP_BOOKE:
1848
            idx = 0;
1849
            srr_0 = &env->spr[SPR_BOOKE_CSRR0];
1850
            srr_1 = &env->spr[SPR_BOOKE_CSRR1];
1851
            break;
1852
        default:
1853
            if (msr_ip)
1854
                excp += 0xFFC00;
1855
            excp |= 0xFFC00000;
1856
            break;
1857
        }
1858
        goto store_next;
1859
    case EXCP_MACHINE_CHECK: /* 0x0200 */
1860
        switch (PPC_EXCP(env)) {
1861
        case PPC_FLAGS_EXCP_40x:
1862
            srr_0 = &env->spr[SPR_40x_SRR2];
1863
            srr_1 = &env->spr[SPR_40x_SRR3];
1864
            break;
1865
        case PPC_FLAGS_EXCP_BOOKE:
1866
            idx = 1;
1867
            srr_0 = &env->spr[SPR_BOOKE_MCSRR0];
1868
            srr_1 = &env->spr[SPR_BOOKE_MCSRR1];
1869
            asrr_0 = &env->spr[SPR_BOOKE_CSRR0];
1870
            asrr_1 = &env->spr[SPR_BOOKE_CSRR1];
1871
            msr_ce = 0;
1872
            break;
1873
        default:
1874
            break;
1875
        }
1876
        msr_me = 0;
1877
        break;
1878
    case EXCP_DSI: /* 0x0300 */
1879
        /* Store exception cause */
1880
        /* data location address has been stored
1881
         * when the fault has been detected
1882
         */
1883
        idx = 2;
1884
        msr &= ~0xFFFF0000;
1885
#if defined (DEBUG_EXCEPTIONS)
1886
        if (loglevel != 0) {
1887
            fprintf(logfile, "DSI exception: DSISR=0x" ADDRX" DAR=0x" ADDRX
1888
                    "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
1889
        }
1890
#endif
1891
        goto store_next;
1892
    case EXCP_ISI: /* 0x0400 */
1893
        /* Store exception cause */
1894
        idx = 3;
1895
        msr &= ~0xFFFF0000;
1896
        msr |= env->error_code;
1897
#if defined (DEBUG_EXCEPTIONS)
1898
        if (loglevel != 0) {
1899
            fprintf(logfile, "ISI exception: msr=0x" ADDRX ", nip=0x" ADDRX
1900
                    "\n", msr, env->nip);
1901
        }
1902
#endif
1903
        goto store_next;
1904
    case EXCP_EXTERNAL: /* 0x0500 */
1905
        idx = 4;
1906
        goto store_next;
1907
    case EXCP_ALIGN: /* 0x0600 */
1908
        if (likely(PPC_EXCP(env) != PPC_FLAGS_EXCP_601)) {
1909
            /* Store exception cause */
1910
            idx = 5;
1911
            /* Get rS/rD and rA from faulting opcode */
1912
            env->spr[SPR_DSISR] |=
1913
                (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
1914
            /* data location address has been stored
1915
             * when the fault has been detected
1916
             */
1917
        } else {
1918
            /* IO error exception on PowerPC 601 */
1919
            /* XXX: TODO */
1920
            cpu_abort(env,
1921
                      "601 IO error exception is not implemented yet !\n");
1922
        }
1923
        goto store_current;
1924
    case EXCP_PROGRAM: /* 0x0700 */
1925
        idx = 6;
1926
        msr &= ~0xFFFF0000;
1927
        switch (env->error_code & ~0xF) {
1928
        case EXCP_FP:
1929
            if (msr_fe0 == 0 && msr_fe1 == 0) {
1930
#if defined (DEBUG_EXCEPTIONS)
1931
                if (loglevel != 0) {
1932
                    fprintf(logfile, "Ignore floating point exception\n");
1933
                }
1934
#endif
1935
                return;
1936
            }
1937
            msr |= 0x00100000;
1938
            /* Set FX */
1939
            env->fpscr[7] |= 0x8;
1940
            /* Finally, update FEX */
1941
            if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
1942
                ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
1943
                env->fpscr[7] |= 0x4;
1944
            break;
1945
        case EXCP_INVAL:
1946
#if defined (DEBUG_EXCEPTIONS)
1947
            if (loglevel != 0) {
1948
                fprintf(logfile, "Invalid instruction at 0x" ADDRX "\n",
1949
                        env->nip);
1950
            }
1951
#endif
1952
            msr |= 0x00080000;
1953
            break;
1954
        case EXCP_PRIV:
1955
            msr |= 0x00040000;
1956
            break;
1957
        case EXCP_TRAP:
1958
            idx = 15;
1959
            msr |= 0x00020000;
1960
            break;
1961
        default:
1962
            /* Should never occur */
1963
            break;
1964
        }
1965
        msr |= 0x00010000;
1966
        goto store_current;
1967
    case EXCP_NO_FP: /* 0x0800 */
1968
        idx = 7;
1969
        msr &= ~0xFFFF0000;
1970
        goto store_current;
1971
    case EXCP_DECR:
1972
        goto store_next;
1973
    case EXCP_SYSCALL: /* 0x0C00 */
1974
        idx = 8;
1975
        /* NOTE: this is a temporary hack to support graphics OSI
1976
           calls from the MOL driver */
1977
        if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
1978
            env->osi_call) {
1979
            if (env->osi_call(env) != 0)
1980
                return;
1981
        }
1982
        if (loglevel & CPU_LOG_INT) {
1983
            dump_syscall(env);
1984
        }
1985
        goto store_next;
1986
    case EXCP_TRACE: /* 0x0D00 */
1987
        goto store_next;
1988
    case EXCP_PERF: /* 0x0F00 */
1989
        /* XXX: TODO */
1990
        cpu_abort(env,
1991
                  "Performance counter exception is not implemented yet !\n");
1992
        goto store_next;
1993
    /* 32 bits PowerPC specific exceptions */
1994
    case EXCP_FP_ASSIST: /* 0x0E00 */
1995
        /* XXX: TODO */
1996
        cpu_abort(env, "Floating point assist exception "
1997
                  "is not implemented yet !\n");
1998
        goto store_next;
1999
    /* 64 bits PowerPC exceptions */
2000
    case EXCP_DSEG: /* 0x0380 */
2001
        /* XXX: TODO */
2002
        cpu_abort(env, "Data segment exception is not implemented yet !\n");
2003
        goto store_next;
2004
    case EXCP_ISEG: /* 0x0480 */
2005
        /* XXX: TODO */
2006
        cpu_abort(env,
2007
                  "Instruction segment exception is not implemented yet !\n");
2008
        goto store_next;
2009
    case EXCP_HDECR: /* 0x0980 */
2010
        /* XXX: TODO */
2011
        cpu_abort(env, "Hypervisor decrementer exception is not implemented "
2012
                  "yet !\n");
2013
        goto store_next;
2014
    /* Implementation specific exceptions */
2015
    case 0x0A00:
2016
        if (likely(env->spr[SPR_PVR] == CPU_PPC_G2 ||
2017
                   env->spr[SPR_PVR] == CPU_PPC_G2LE)) {
2018
            /* Critical interrupt on G2 */
2019
            /* XXX: TODO */
2020
            cpu_abort(env, "G2 critical interrupt is not implemented yet !\n");
2021
            goto store_next;
2022
        } else {
2023
            cpu_abort(env, "Invalid exception 0x0A00 !\n");
2024
        }
2025
        return;
2026
    case 0x0F20:
2027
        idx = 9;
2028
        switch (PPC_EXCP(env)) {
2029
        case PPC_FLAGS_EXCP_40x:
2030
            /* APU unavailable on 405 */
2031
            /* XXX: TODO */
2032
            cpu_abort(env,
2033
                      "APU unavailable exception is not implemented yet !\n");
2034
            goto store_next;
2035
        case PPC_FLAGS_EXCP_74xx:
2036
            /* Altivec unavailable */
2037
            /* XXX: TODO */
2038
            cpu_abort(env, "Altivec unavailable exception "
2039
                      "is not implemented yet !\n");
2040
            goto store_next;
2041
        default:
2042
            cpu_abort(env, "Invalid exception 0x0F20 !\n");
2043
            break;
2044
        }
2045
        return;
2046
    case 0x1000:
2047
        idx = 10;
2048
        switch (PPC_EXCP(env)) {
2049
        case PPC_FLAGS_EXCP_40x:
2050
            /* PIT on 4xx */
2051
            msr &= ~0xFFFF0000;
2052
#if defined (DEBUG_EXCEPTIONS)
2053
            if (loglevel != 0)
2054
                fprintf(logfile, "PIT exception\n");
2055
#endif
2056
            goto store_next;
2057
        case PPC_FLAGS_EXCP_602:
2058
        case PPC_FLAGS_EXCP_603:
2059
            /* ITLBMISS on 602/603 */
2060
            goto store_gprs;
2061
        case PPC_FLAGS_EXCP_7x5:
2062
            /* ITLBMISS on 745/755 */
2063
            goto tlb_miss;
2064
        default:
2065
            cpu_abort(env, "Invalid exception 0x1000 !\n");
2066
            break;
2067
        }
2068
        return;
2069
    case 0x1010:
2070
        idx = 11;
2071
        switch (PPC_EXCP(env)) {
2072
        case PPC_FLAGS_EXCP_40x:
2073
            /* FIT on 4xx */
2074
            msr &= ~0xFFFF0000;
2075
#if defined (DEBUG_EXCEPTIONS)
2076
            if (loglevel != 0)
2077
                fprintf(logfile, "FIT exception\n");
2078
#endif
2079
            goto store_next;
2080
        default:
2081
            cpu_abort(env, "Invalid exception 0x1010 !\n");
2082
            break;
2083
        }
2084
        return;
2085
    case 0x1020:
2086
        idx = 12;
2087
        switch (PPC_EXCP(env)) {
2088
        case PPC_FLAGS_EXCP_40x:
2089
            /* Watchdog on 4xx */
2090
            msr &= ~0xFFFF0000;
2091
#if defined (DEBUG_EXCEPTIONS)
2092
            if (loglevel != 0)
2093
                fprintf(logfile, "WDT exception\n");
2094
#endif
2095
            goto store_next;
2096
        case PPC_FLAGS_EXCP_BOOKE:
2097
            srr_0 = &env->spr[SPR_BOOKE_CSRR0];
2098
            srr_1 = &env->spr[SPR_BOOKE_CSRR1];
2099
            break;
2100
        default:
2101
            cpu_abort(env, "Invalid exception 0x1020 !\n");
2102
            break;
2103
        }
2104
        return;
2105
    case 0x1100:
2106
        idx = 13;
2107
        switch (PPC_EXCP(env)) {
2108
        case PPC_FLAGS_EXCP_40x:
2109
            /* DTLBMISS on 4xx */
2110
            msr &= ~0xFFFF0000;
2111
            goto store_next;
2112
        case PPC_FLAGS_EXCP_602:
2113
        case PPC_FLAGS_EXCP_603:
2114
            /* DLTLBMISS on 602/603 */
2115
            goto store_gprs;
2116
        case PPC_FLAGS_EXCP_7x5:
2117
            /* DLTLBMISS on 745/755 */
2118
            goto tlb_miss;
2119
        default:
2120
            cpu_abort(env, "Invalid exception 0x1100 !\n");
2121
            break;
2122
        }
2123
        return;
2124
    case 0x1200:
2125
        idx = 14;
2126
        switch (PPC_EXCP(env)) {
2127
        case PPC_FLAGS_EXCP_40x:
2128
            /* ITLBMISS on 4xx */
2129
            msr &= ~0xFFFF0000;
2130
            goto store_next;
2131
        case PPC_FLAGS_EXCP_602:
2132
        case PPC_FLAGS_EXCP_603:
2133
            /* DSTLBMISS on 602/603 */
2134
        store_gprs:
2135
            /* Swap temporary saved registers with GPRs */
2136
            swap_gpr_tgpr(env);
2137
            msr_tgpr = 1;
2138
#if defined (DEBUG_SOFTWARE_TLB)
2139
            if (loglevel != 0) {
2140
                const unsigned char *es;
2141
                target_ulong *miss, *cmp;
2142
                int en;
2143
                if (excp == 0x1000) {
2144
                    es = "I";
2145
                    en = 'I';
2146
                    miss = &env->spr[SPR_IMISS];
2147
                    cmp = &env->spr[SPR_ICMP];
2148
                } else {
2149
                    if (excp == 0x1100)
2150
                        es = "DL";
2151
                    else
2152
                        es = "DS";
2153
                    en = 'D';
2154
                    miss = &env->spr[SPR_DMISS];
2155
                    cmp = &env->spr[SPR_DCMP];
2156
                }
2157
                fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2158
                        " H1 " ADDRX " H2 " ADDRX " %08x\n",
2159
                        es, en, *miss, en, *cmp,
2160
                        env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2161
                        env->error_code);
2162
            }
2163
#endif
2164
            goto tlb_miss;
2165
        case PPC_FLAGS_EXCP_7x5:
2166
            /* DSTLBMISS on 745/755 */
2167
        tlb_miss:
2168
            msr &= ~0xF83F0000;
2169
            msr |= env->crf[0] << 28;
2170
            msr |= env->error_code; /* key, D/I, S/L bits */
2171
            /* Set way using a LRU mechanism */
2172
            msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2173
            goto store_next;
2174
        default:
2175
            cpu_abort(env, "Invalid exception 0x1200 !\n");
2176
            break;
2177
        }
2178
        return;
2179
    case 0x1300:
2180
        switch (PPC_EXCP(env)) {
2181
        case PPC_FLAGS_EXCP_601:
2182
        case PPC_FLAGS_EXCP_602:
2183
        case PPC_FLAGS_EXCP_603:
2184
        case PPC_FLAGS_EXCP_604:
2185
        case PPC_FLAGS_EXCP_7x0:
2186
        case PPC_FLAGS_EXCP_7x5:
2187
            /* IABR on 6xx/7xx */
2188
            /* XXX: TODO */
2189
            cpu_abort(env, "IABR exception is not implemented yet !\n");
2190
            goto store_next;
2191
        default:
2192
            cpu_abort(env, "Invalid exception 0x1300 !\n");
2193
            break;
2194
        }
2195
        return;
2196
    case 0x1400:
2197
        switch (PPC_EXCP(env)) {
2198
        case PPC_FLAGS_EXCP_601:
2199
        case PPC_FLAGS_EXCP_602:
2200
        case PPC_FLAGS_EXCP_603:
2201
        case PPC_FLAGS_EXCP_604:
2202
        case PPC_FLAGS_EXCP_7x0:
2203
        case PPC_FLAGS_EXCP_7x5:
2204
            /* SMI on 6xx/7xx */
2205
            /* XXX: TODO */
2206
            cpu_abort(env, "SMI exception is not implemented yet !\n");
2207
            goto store_next;
2208
        default:
2209
            cpu_abort(env, "Invalid exception 0x1400 !\n");
2210
            break;
2211
        }
2212
        return;
2213
    case 0x1500:
2214
        switch (PPC_EXCP(env)) {
2215
        case PPC_FLAGS_EXCP_602:
2216
            /* Watchdog on 602 */
2217
            /* XXX: TODO */
2218
            cpu_abort(env,
2219
                      "602 watchdog exception is not implemented yet !\n");
2220
            goto store_next;
2221
        case PPC_FLAGS_EXCP_970:
2222
            /* Soft patch exception on 970 */
2223
            /* XXX: TODO */
2224
            cpu_abort(env,
2225
                      "970 soft-patch exception is not implemented yet !\n");
2226
            goto store_next;
2227
        case PPC_FLAGS_EXCP_74xx:
2228
            /* VPU assist on 74xx */
2229
            /* XXX: TODO */
2230
            cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2231
            goto store_next;
2232
        default:
2233
            cpu_abort(env, "Invalid exception 0x1500 !\n");
2234
            break;
2235
        }
2236
        return;
2237
    case 0x1600:
2238
        switch (PPC_EXCP(env)) {
2239
        case PPC_FLAGS_EXCP_602:
2240
            /* Emulation trap on 602 */
2241
            /* XXX: TODO */
2242
            cpu_abort(env, "602 emulation trap exception "
2243
                      "is not implemented yet !\n");
2244
            goto store_next;
2245
        case PPC_FLAGS_EXCP_970:
2246
            /* Maintenance exception on 970 */
2247
            /* XXX: TODO */
2248
            cpu_abort(env,
2249
                      "970 maintenance exception is not implemented yet !\n");
2250
            goto store_next;
2251
        default:
2252
            cpu_abort(env, "Invalid exception 0x1600 !\n");
2253
            break;
2254
        }
2255
        return;
2256
    case 0x1700:
2257
        switch (PPC_EXCP(env)) {
2258
        case PPC_FLAGS_EXCP_7x0:
2259
        case PPC_FLAGS_EXCP_7x5:
2260
            /* Thermal management interrupt on G3 */
2261
            /* XXX: TODO */
2262
            cpu_abort(env, "G3 thermal management exception "
2263
                      "is not implemented yet !\n");
2264
            goto store_next;
2265
        case PPC_FLAGS_EXCP_970:
2266
            /* VPU assist on 970 */
2267
            /* XXX: TODO */
2268
            cpu_abort(env,
2269
                      "970 VPU assist exception is not implemented yet !\n");
2270
            goto store_next;
2271
        default:
2272
            cpu_abort(env, "Invalid exception 0x1700 !\n");
2273
            break;
2274
        }
2275
        return;
2276
    case 0x1800:
2277
        switch (PPC_EXCP(env)) {
2278
        case PPC_FLAGS_EXCP_970:
2279
            /* Thermal exception on 970 */
2280
            /* XXX: TODO */
2281
            cpu_abort(env, "970 thermal management exception "
2282
                      "is not implemented yet !\n");
2283
            goto store_next;
2284
        default:
2285
            cpu_abort(env, "Invalid exception 0x1800 !\n");
2286
            break;
2287
        }
2288
        return;
2289
    case 0x2000:
2290
        switch (PPC_EXCP(env)) {
2291
        case PPC_FLAGS_EXCP_40x:
2292
            /* DEBUG on 4xx */
2293
            /* XXX: TODO */
2294
            cpu_abort(env, "40x debug exception is not implemented yet !\n");
2295
            goto store_next;
2296
        case PPC_FLAGS_EXCP_601:
2297
            /* Run mode exception on 601 */
2298
            /* XXX: TODO */
2299
            cpu_abort(env,
2300
                      "601 run mode exception is not implemented yet !\n");
2301
            goto store_next;
2302
        case PPC_FLAGS_EXCP_BOOKE:
2303
            srr_0 = &env->spr[SPR_BOOKE_CSRR0];
2304
            srr_1 = &env->spr[SPR_BOOKE_CSRR1];
2305
            break;
2306
        default:
2307
            cpu_abort(env, "Invalid exception 0x1800 !\n");
2308
            break;
2309
        }
2310
        return;
2311
    /* Other exceptions */
2312
    /* Qemu internal exceptions:
2313
     * we should never come here with those values: abort execution
2314
     */
2315
    default:
2316
        cpu_abort(env, "Invalid exception: code %d (%04x)\n", excp, excp);
2317
        return;
2318
    store_current:
2319
        /* save current instruction location */
2320
        *srr_0 = env->nip - 4;
2321
        break;
2322
    store_next:
2323
        /* save next instruction location */
2324
        *srr_0 = env->nip;
2325
        break;
2326
    }
2327
    /* Save msr */
2328
    *srr_1 = msr;
2329
    if (asrr_0 != NULL)
2330
        *asrr_0 = *srr_0;
2331
    if (asrr_1 != NULL)
2332
        *asrr_1 = *srr_1;
2333
    /* If we disactivated any translation, flush TLBs */
2334
    if (msr_ir || msr_dr) {
2335
        tlb_flush(env, 1);
2336
    }
2337
    /* reload MSR with correct bits */
2338
    msr_ee = 0;
2339
    msr_pr = 0;
2340
    msr_fp = 0;
2341
    msr_fe0 = 0;
2342
    msr_se = 0;
2343
    msr_be = 0;
2344
    msr_fe1 = 0;
2345
    msr_ir = 0;
2346
    msr_dr = 0;
2347
    msr_ri = 0;
2348
    msr_le = msr_ile;
2349
    if (PPC_EXCP(env) == PPC_FLAGS_EXCP_BOOKE) {
2350
        msr_cm = msr_icm;
2351
        if (idx == -1 || (idx >= 16 && idx < 32)) {
2352
            cpu_abort(env, "Invalid exception index for excp %d %08x idx %d\n",
2353
                      excp, excp, idx);
2354
        }
2355
#if defined(TARGET_PPC64)
2356
        if (msr_cm)
2357
            env->nip = (uint64_t)env->spr[SPR_BOOKE_IVPR];
2358
        else
2359
#endif
2360
            env->nip = (uint32_t)env->spr[SPR_BOOKE_IVPR];
2361
        if (idx < 16)
2362
            env->nip |= env->spr[SPR_BOOKE_IVOR0 + idx];
2363
        else if (idx < 38)
2364
            env->nip |= env->spr[SPR_BOOKE_IVOR32 + idx - 32];
2365
    } else {
2366
        msr_sf = msr_isf;
2367
        env->nip = excp;
2368
    }
2369
    do_compute_hflags(env);
2370
    /* Jump to handler */
2371
    env->exception_index = EXCP_NONE;
2372
}
2373

    
2374
void ppc_hw_interrupt (CPUPPCState *env)
2375
{
2376
    int raised = 0;
2377

    
2378
#if 1
2379
    if (loglevel & CPU_LOG_INT) {
2380
        fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2381
                __func__, env, env->pending_interrupts,
2382
                env->interrupt_request, msr_me, msr_ee);
2383
    }
2384
#endif
2385
    /* Raise it */
2386
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2387
        /* External reset / critical input */
2388
        /* XXX: critical input should be handled another way.
2389
         *      This code is not correct !
2390
         */
2391
        env->exception_index = EXCP_RESET;
2392
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2393
        raised = 1;
2394
    }
2395
    if (raised == 0 && msr_me != 0) {
2396
        /* Machine check exception */
2397
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2398
            env->exception_index = EXCP_MACHINE_CHECK;
2399
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2400
            raised = 1;
2401
        }
2402
    }
2403
    if (raised == 0 && msr_ee != 0) {
2404
#if defined(TARGET_PPC64H) /* PowerPC 64 with hypervisor mode support */
2405
        /* Hypervisor decrementer exception */
2406
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2407
            env->exception_index = EXCP_HDECR;
2408
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2409
            raised = 1;
2410
        } else
2411
#endif
2412
        /* Decrementer exception */
2413
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2414
            env->exception_index = EXCP_DECR;
2415
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2416
            raised = 1;
2417
        /* Programmable interval timer on embedded PowerPC */
2418
        } else if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2419
            env->exception_index = EXCP_40x_PIT;
2420
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2421
            raised = 1;
2422
        /* Fixed interval timer on embedded PowerPC */
2423
        } else if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2424
            env->exception_index = EXCP_40x_FIT;
2425
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2426
            raised = 1;
2427
        /* Watchdog timer on embedded PowerPC */
2428
        } else if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2429
            env->exception_index = EXCP_40x_WATCHDOG;
2430
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2431
            raised = 1;
2432
        /* External interrupt */
2433
        } else if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2434
            env->exception_index = EXCP_EXTERNAL;
2435
            /* Taking an external interrupt does not clear the external
2436
             * interrupt status
2437
             */
2438
#if 0
2439
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2440
#endif
2441
            raised = 1;
2442
#if 0 // TODO
2443
        /* Thermal interrupt */
2444
        } else if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2445
            env->exception_index = EXCP_970_THRM;
2446
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2447
            raised = 1;
2448
#endif
2449
        }
2450
#if 0 // TODO
2451
    /* External debug exception */
2452
    } else if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2453
        env->exception_index = EXCP_xxx;
2454
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2455
        raised = 1;
2456
#endif
2457
    }
2458
    if (raised != 0) {
2459
        env->error_code = 0;
2460
        do_interrupt(env);
2461
    }
2462
}
2463
#endif /* !CONFIG_USER_ONLY */
2464

    
2465
void cpu_dump_EA (target_ulong EA)
2466
{
2467
    FILE *f;
2468

    
2469
    if (logfile) {
2470
        f = logfile;
2471
    } else {
2472
        f = stdout;
2473
        return;
2474
    }
2475
    fprintf(f, "Memory access at address " ADDRX "\n", EA);
2476
}
2477

    
2478
void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2479
{
2480
    FILE *f;
2481

    
2482
    if (logfile) {
2483
        f = logfile;
2484
    } else {
2485
        f = stdout;
2486
        return;
2487
    }
2488
    fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2489
            RA, msr);
2490
}
2491

    
2492
void cpu_ppc_reset (void *opaque)
2493
{
2494
    CPUPPCState *env;
2495
    int i;
2496

    
2497
    env = opaque;
2498
    /* XXX: some of those flags initialisation values could depend
2499
     *      on the actual PowerPC implementation
2500
     */
2501
    for (i = 0; i < 63; i++)
2502
        env->msr[i] = 0;
2503
#if defined(TARGET_PPC64)
2504
    msr_hv = 0; /* Should be 1... */
2505
#endif
2506
    msr_ap = 0; /* TO BE CHECKED */
2507
    msr_sa = 0; /* TO BE CHECKED */
2508
    msr_ip = 0; /* TO BE CHECKED */
2509
#if defined (DO_SINGLE_STEP) && 0
2510
    /* Single step trace mode */
2511
    msr_se = 1;
2512
    msr_be = 1;
2513
#endif
2514
#if defined(CONFIG_USER_ONLY)
2515
    msr_fp = 1; /* Allow floating point exceptions */
2516
    msr_pr = 1;
2517
#else
2518
    env->nip = 0xFFFFFFFC;
2519
    ppc_tlb_invalidate_all(env);
2520
#endif
2521
    do_compute_hflags(env);
2522
    env->reserve = -1;
2523
    /* Be sure no exception or interrupt is pending */
2524
    env->pending_interrupts = 0;
2525
    env->exception_index = EXCP_NONE;
2526
    /* Flush all TLBs */
2527
    tlb_flush(env, 1);
2528
}
2529

    
2530
CPUPPCState *cpu_ppc_init (void)
2531
{
2532
    CPUPPCState *env;
2533

    
2534
    env = qemu_mallocz(sizeof(CPUPPCState));
2535
    if (!env)
2536
        return NULL;
2537
    cpu_exec_init(env);
2538
    cpu_ppc_reset(env);
2539

    
2540
    return env;
2541
}
2542

    
2543
void cpu_ppc_close (CPUPPCState *env)
2544
{
2545
    /* Should also remove all opcode tables... */
2546
    free(env);
2547
}