Statistics
| Branch: | Revision:

root / target-ppc / helper.c @ add78955

History | View | Annotate | Download (96.5 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
#include "helper_regs.h"
31

    
32
//#define DEBUG_MMU
33
//#define DEBUG_BATS
34
//#define DEBUG_SOFTWARE_TLB
35
//#define DUMP_PAGE_TABLES
36
//#define DEBUG_EXCEPTIONS
37
//#define FLUSH_ALL_TLBS
38

    
39
/*****************************************************************************/
40
/* PowerPC MMU emulation */
41

    
42
#if defined(CONFIG_USER_ONLY)
43
int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
44
                              int mmu_idx, int is_softmmu)
45
{
46
    int exception, error_code;
47

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

    
62
    return 1;
63
}
64

    
65
target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
66
{
67
    return addr;
68
}
69

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

    
77
static always_inline void pte_invalidate (target_ulong *pte0)
78
{
79
    *pte0 &= ~0x80000000;
80
}
81

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

    
88
static always_inline void pte64_invalidate (target_ulong *pte0)
89
{
90
    *pte0 &= ~0x0000000000000001ULL;
91
}
92
#endif
93

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

    
101
static always_inline int pp_check (int key, int pp, int nx)
102
{
103
    int access;
104

    
105
    /* Compute access rights */
106
    /* When pp is 3/7, the result is undefined. Set it to noaccess */
107
    access = 0;
108
    if (key == 0) {
109
        switch (pp) {
110
        case 0x0:
111
        case 0x1:
112
        case 0x2:
113
            access |= PAGE_WRITE;
114
            /* No break here */
115
        case 0x3:
116
        case 0x6:
117
            access |= PAGE_READ;
118
            break;
119
        }
120
    } else {
121
        switch (pp) {
122
        case 0x0:
123
        case 0x6:
124
            access = 0;
125
            break;
126
        case 0x1:
127
        case 0x3:
128
            access = PAGE_READ;
129
            break;
130
        case 0x2:
131
            access = PAGE_READ | PAGE_WRITE;
132
            break;
133
        }
134
    }
135
    if (nx == 0)
136
        access |= PAGE_EXEC;
137

    
138
    return access;
139
}
140

    
141
static always_inline int check_prot (int prot, int rw, int access_type)
142
{
143
    int ret;
144

    
145
    if (access_type == ACCESS_CODE) {
146
        if (prot & PAGE_EXEC)
147
            ret = 0;
148
        else
149
            ret = -2;
150
    } else if (rw) {
151
        if (prot & PAGE_WRITE)
152
            ret = 0;
153
        else
154
            ret = -2;
155
    } else {
156
        if (prot & PAGE_READ)
157
            ret = 0;
158
        else
159
            ret = -2;
160
    }
161

    
162
    return ret;
163
}
164

    
165
static always_inline int _pte_check (mmu_ctx_t *ctx, int is_64b,
166
                                     target_ulong pte0, target_ulong pte1,
167
                                     int h, int rw, int type)
168
{
169
    target_ulong ptem, mmask;
170
    int access, ret, pteh, ptev, pp;
171

    
172
    access = 0;
173
    ret = -1;
174
    /* Check validity and table match */
175
#if defined(TARGET_PPC64)
176
    if (is_64b) {
177
        ptev = pte64_is_valid(pte0);
178
        pteh = (pte0 >> 1) & 1;
179
    } else
180
#endif
181
    {
182
        ptev = pte_is_valid(pte0);
183
        pteh = (pte0 >> 6) & 1;
184
    }
185
    if (ptev && h == pteh) {
186
        /* Check vsid & api */
187
#if defined(TARGET_PPC64)
188
        if (is_64b) {
189
            ptem = pte0 & PTE64_PTEM_MASK;
190
            mmask = PTE64_CHECK_MASK;
191
            pp = (pte1 & 0x00000003) | ((pte1 >> 61) & 0x00000004);
192
            ctx->nx |= (pte1 >> 2) & 1; /* No execute bit */
193
            ctx->nx |= (pte1 >> 3) & 1; /* Guarded bit    */
194
        } else
195
#endif
196
        {
197
            ptem = pte0 & PTE_PTEM_MASK;
198
            mmask = PTE_CHECK_MASK;
199
            pp = pte1 & 0x00000003;
200
        }
201
        if (ptem == ctx->ptem) {
202
            if (ctx->raddr != (target_phys_addr_t)-1ULL) {
203
                /* all matches should have equal RPN, WIMG & PP */
204
                if ((ctx->raddr & mmask) != (pte1 & mmask)) {
205
                    if (loglevel != 0)
206
                        fprintf(logfile, "Bad RPN/WIMG/PP\n");
207
                    return -3;
208
                }
209
            }
210
            /* Compute access rights */
211
            access = pp_check(ctx->key, pp, ctx->nx);
212
            /* Keep the matching PTE informations */
213
            ctx->raddr = pte1;
214
            ctx->prot = access;
215
            ret = check_prot(ctx->prot, rw, type);
216
            if (ret == 0) {
217
                /* Access granted */
218
#if defined (DEBUG_MMU)
219
                if (loglevel != 0)
220
                    fprintf(logfile, "PTE access granted !\n");
221
#endif
222
            } else {
223
                /* Access right violation */
224
#if defined (DEBUG_MMU)
225
                if (loglevel != 0)
226
                    fprintf(logfile, "PTE access rejected\n");
227
#endif
228
            }
229
        }
230
    }
231

    
232
    return ret;
233
}
234

    
235
static always_inline int pte32_check (mmu_ctx_t *ctx,
236
                                      target_ulong pte0, target_ulong pte1,
237
                                      int h, int rw, int type)
238
{
239
    return _pte_check(ctx, 0, pte0, pte1, h, rw, type);
240
}
241

    
242
#if defined(TARGET_PPC64)
243
static always_inline int pte64_check (mmu_ctx_t *ctx,
244
                                      target_ulong pte0, target_ulong pte1,
245
                                      int h, int rw, int type)
246
{
247
    return _pte_check(ctx, 1, pte0, pte1, h, rw, type);
248
}
249
#endif
250

    
251
static always_inline int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
252
                                           int ret, int rw)
253
{
254
    int store = 0;
255

    
256
    /* Update page flags */
257
    if (!(*pte1p & 0x00000100)) {
258
        /* Update accessed flag */
259
        *pte1p |= 0x00000100;
260
        store = 1;
261
    }
262
    if (!(*pte1p & 0x00000080)) {
263
        if (rw == 1 && ret == 0) {
264
            /* Update changed flag */
265
            *pte1p |= 0x00000080;
266
            store = 1;
267
        } else {
268
            /* Force page fault for first write access */
269
            ctx->prot &= ~PAGE_WRITE;
270
        }
271
    }
272

    
273
    return store;
274
}
275

    
276
/* Software driven TLB helpers */
277
static always_inline int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
278
                                            int way, int is_code)
279
{
280
    int nr;
281

    
282
    /* Select TLB num in a way from address */
283
    nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
284
    /* Select TLB way */
285
    nr += env->tlb_per_way * way;
286
    /* 6xx have separate TLBs for instructions and data */
287
    if (is_code && env->id_tlbs == 1)
288
        nr += env->nb_tlb;
289

    
290
    return nr;
291
}
292

    
293
static always_inline void ppc6xx_tlb_invalidate_all (CPUState *env)
294
{
295
    ppc6xx_tlb_t *tlb;
296
    int nr, max;
297

    
298
#if defined (DEBUG_SOFTWARE_TLB) && 0
299
    if (loglevel != 0) {
300
        fprintf(logfile, "Invalidate all TLBs\n");
301
    }
302
#endif
303
    /* Invalidate all defined software TLB */
304
    max = env->nb_tlb;
305
    if (env->id_tlbs == 1)
306
        max *= 2;
307
    for (nr = 0; nr < max; nr++) {
308
        tlb = &env->tlb[nr].tlb6;
309
        pte_invalidate(&tlb->pte0);
310
    }
311
    tlb_flush(env, 1);
312
}
313

    
314
static always_inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
315
                                                        target_ulong eaddr,
316
                                                        int is_code,
317
                                                        int match_epn)
318
{
319
#if !defined(FLUSH_ALL_TLBS)
320
    ppc6xx_tlb_t *tlb;
321
    int way, nr;
322

    
323
    /* Invalidate ITLB + DTLB, all ways */
324
    for (way = 0; way < env->nb_ways; way++) {
325
        nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
326
        tlb = &env->tlb[nr].tlb6;
327
        if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
328
#if defined (DEBUG_SOFTWARE_TLB)
329
            if (loglevel != 0) {
330
                fprintf(logfile, "TLB invalidate %d/%d " ADDRX "\n",
331
                        nr, env->nb_tlb, eaddr);
332
            }
333
#endif
334
            pte_invalidate(&tlb->pte0);
335
            tlb_flush_page(env, tlb->EPN);
336
        }
337
    }
338
#else
339
    /* XXX: PowerPC specification say this is valid as well */
340
    ppc6xx_tlb_invalidate_all(env);
341
#endif
342
}
343

    
344
static always_inline void ppc6xx_tlb_invalidate_virt (CPUState *env,
345
                                                      target_ulong eaddr,
346
                                                      int is_code)
347
{
348
    __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
349
}
350

    
351
void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
352
                       target_ulong pte0, target_ulong pte1)
353
{
354
    ppc6xx_tlb_t *tlb;
355
    int nr;
356

    
357
    nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
358
    tlb = &env->tlb[nr].tlb6;
359
#if defined (DEBUG_SOFTWARE_TLB)
360
    if (loglevel != 0) {
361
        fprintf(logfile, "Set TLB %d/%d EPN " ADDRX " PTE0 " ADDRX
362
                " PTE1 " ADDRX "\n", nr, env->nb_tlb, EPN, pte0, pte1);
363
    }
364
#endif
365
    /* Invalidate any pending reference in Qemu for this virtual address */
366
    __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
367
    tlb->pte0 = pte0;
368
    tlb->pte1 = pte1;
369
    tlb->EPN = EPN;
370
    /* Store last way for LRU mechanism */
371
    env->last_way = way;
372
}
373

    
374
static always_inline int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
375
                                           target_ulong eaddr, int rw,
376
                                           int access_type)
377
{
378
    ppc6xx_tlb_t *tlb;
379
    int nr, best, way;
380
    int ret;
381

    
382
    best = -1;
383
    ret = -1; /* No TLB found */
384
    for (way = 0; way < env->nb_ways; way++) {
385
        nr = ppc6xx_tlb_getnum(env, eaddr, way,
386
                               access_type == ACCESS_CODE ? 1 : 0);
387
        tlb = &env->tlb[nr].tlb6;
388
        /* This test "emulates" the PTE index match for hardware TLBs */
389
        if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
390
#if defined (DEBUG_SOFTWARE_TLB)
391
            if (loglevel != 0) {
392
                fprintf(logfile, "TLB %d/%d %s [" ADDRX " " ADDRX
393
                        "] <> " ADDRX "\n",
394
                        nr, env->nb_tlb,
395
                        pte_is_valid(tlb->pte0) ? "valid" : "inval",
396
                        tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
397
            }
398
#endif
399
            continue;
400
        }
401
#if defined (DEBUG_SOFTWARE_TLB)
402
        if (loglevel != 0) {
403
            fprintf(logfile, "TLB %d/%d %s " ADDRX " <> " ADDRX " " ADDRX
404
                    " %c %c\n",
405
                    nr, env->nb_tlb,
406
                    pte_is_valid(tlb->pte0) ? "valid" : "inval",
407
                    tlb->EPN, eaddr, tlb->pte1,
408
                    rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
409
        }
410
#endif
411
        switch (pte32_check(ctx, tlb->pte0, tlb->pte1, 0, rw, access_type)) {
412
        case -3:
413
            /* TLB inconsistency */
414
            return -1;
415
        case -2:
416
            /* Access violation */
417
            ret = -2;
418
            best = nr;
419
            break;
420
        case -1:
421
        default:
422
            /* No match */
423
            break;
424
        case 0:
425
            /* access granted */
426
            /* XXX: we should go on looping to check all TLBs consistency
427
             *      but we can speed-up the whole thing as the
428
             *      result would be undefined if TLBs are not consistent.
429
             */
430
            ret = 0;
431
            best = nr;
432
            goto done;
433
        }
434
    }
435
    if (best != -1) {
436
    done:
437
#if defined (DEBUG_SOFTWARE_TLB)
438
        if (loglevel != 0) {
439
            fprintf(logfile, "found TLB at addr 0x%08lx prot=0x%01x ret=%d\n",
440
                    ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
441
        }
442
#endif
443
        /* Update page flags */
444
        pte_update_flags(ctx, &env->tlb[best].tlb6.pte1, ret, rw);
445
    }
446

    
447
    return ret;
448
}
449

    
450
/* Perform BAT hit & translation */
451
static always_inline void bat_size_prot (CPUState *env, target_ulong *blp,
452
                                         int *validp, int *protp,
453
                                         target_ulong *BATu, target_ulong *BATl)
454
{
455
    target_ulong bl;
456
    int pp, valid, prot;
457

    
458
    bl = (*BATu & 0x00001FFC) << 15;
459
    valid = 0;
460
    prot = 0;
461
    if (((msr_pr == 0) && (*BATu & 0x00000002)) ||
462
        ((msr_pr != 0) && (*BATu & 0x00000001))) {
463
        valid = 1;
464
        pp = *BATl & 0x00000003;
465
        if (pp != 0) {
466
            prot = PAGE_READ | PAGE_EXEC;
467
            if (pp == 0x2)
468
                prot |= PAGE_WRITE;
469
        }
470
    }
471
    *blp = bl;
472
    *validp = valid;
473
    *protp = prot;
474
}
475

    
476
static always_inline void bat_601_size_prot (CPUState *env,target_ulong *blp,
477
                                             int *validp, int *protp,
478
                                             target_ulong *BATu,
479
                                             target_ulong *BATl)
480
{
481
    target_ulong bl;
482
    int key, pp, valid, prot;
483

    
484
    bl = (*BATl & 0x0000003F) << 17;
485
#if defined (DEBUG_BATS)
486
    if (loglevel != 0) {
487
        fprintf(logfile, "b %02x ==> bl %08x msk %08x\n",
488
                *BATl & 0x0000003F, bl, ~bl);
489
    }
490
#endif
491
    prot = 0;
492
    valid = (*BATl >> 6) & 1;
493
    if (valid) {
494
        pp = *BATu & 0x00000003;
495
        if (msr_pr == 0)
496
            key = (*BATu >> 3) & 1;
497
        else
498
            key = (*BATu >> 2) & 1;
499
        prot = pp_check(key, pp, 0);
500
    }
501
    *blp = bl;
502
    *validp = valid;
503
    *protp = prot;
504
}
505

    
506
static always_inline int get_bat (CPUState *env, mmu_ctx_t *ctx,
507
                                  target_ulong virtual, int rw, int type)
508
{
509
    target_ulong *BATlt, *BATut, *BATu, *BATl;
510
    target_ulong base, BEPIl, BEPIu, bl;
511
    int i, valid, prot;
512
    int ret = -1;
513

    
514
#if defined (DEBUG_BATS)
515
    if (loglevel != 0) {
516
        fprintf(logfile, "%s: %cBAT v 0x" ADDRX "\n", __func__,
517
                type == ACCESS_CODE ? 'I' : 'D', virtual);
518
    }
519
#endif
520
    switch (type) {
521
    case ACCESS_CODE:
522
        BATlt = env->IBAT[1];
523
        BATut = env->IBAT[0];
524
        break;
525
    default:
526
        BATlt = env->DBAT[1];
527
        BATut = env->DBAT[0];
528
        break;
529
    }
530
#if defined (DEBUG_BATS)
531
    if (loglevel != 0) {
532
        fprintf(logfile, "%s...: %cBAT v 0x" ADDRX "\n", __func__,
533
                type == ACCESS_CODE ? 'I' : 'D', virtual);
534
    }
535
#endif
536
    base = virtual & 0xFFFC0000;
537
    for (i = 0; i < env->nb_BATs; i++) {
538
        BATu = &BATut[i];
539
        BATl = &BATlt[i];
540
        BEPIu = *BATu & 0xF0000000;
541
        BEPIl = *BATu & 0x0FFE0000;
542
        if (unlikely(env->mmu_model == POWERPC_MMU_601)) {
543
            bat_601_size_prot(env, &bl, &valid, &prot, BATu, BATl);
544
        } else {
545
            bat_size_prot(env, &bl, &valid, &prot, BATu, BATl);
546
        }
547
#if defined (DEBUG_BATS)
548
        if (loglevel != 0) {
549
            fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
550
                    " BATl 0x" ADDRX "\n",
551
                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
552
                    *BATu, *BATl);
553
        }
554
#endif
555
        if ((virtual & 0xF0000000) == BEPIu &&
556
            ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
557
            /* BAT matches */
558
            if (valid != 0) {
559
                /* Get physical address */
560
                ctx->raddr = (*BATl & 0xF0000000) |
561
                    ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
562
                    (virtual & 0x0001F000);
563
                /* Compute access rights */
564
                ctx->prot = prot;
565
                ret = check_prot(ctx->prot, rw, type);
566
#if defined (DEBUG_BATS)
567
                if (ret == 0 && loglevel != 0) {
568
                    fprintf(logfile, "BAT %d match: r 0x" PADDRX
569
                            " prot=%c%c\n",
570
                            i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
571
                            ctx->prot & PAGE_WRITE ? 'W' : '-');
572
                }
573
#endif
574
                break;
575
            }
576
        }
577
    }
578
    if (ret < 0) {
579
#if defined (DEBUG_BATS)
580
        if (loglevel != 0) {
581
            fprintf(logfile, "no BAT match for 0x" ADDRX ":\n", virtual);
582
            for (i = 0; i < 4; i++) {
583
                BATu = &BATut[i];
584
                BATl = &BATlt[i];
585
                BEPIu = *BATu & 0xF0000000;
586
                BEPIl = *BATu & 0x0FFE0000;
587
                bl = (*BATu & 0x00001FFC) << 15;
588
                fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
589
                        " BATl 0x" ADDRX " \n\t"
590
                        "0x" ADDRX " 0x" ADDRX " 0x" ADDRX "\n",
591
                        __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
592
                        *BATu, *BATl, BEPIu, BEPIl, bl);
593
            }
594
        }
595
#endif
596
    }
597

    
598
    /* No hit */
599
    return ret;
600
}
601

    
602
/* PTE table lookup */
603
static always_inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h,
604
                                    int rw, int type)
605
{
606
    target_ulong base, pte0, pte1;
607
    int i, good = -1;
608
    int ret, r;
609

    
610
    ret = -1; /* No entry found */
611
    base = ctx->pg_addr[h];
612
    for (i = 0; i < 8; i++) {
613
#if defined(TARGET_PPC64)
614
        if (is_64b) {
615
            pte0 = ldq_phys(base + (i * 16));
616
            pte1 =  ldq_phys(base + (i * 16) + 8);
617
            r = pte64_check(ctx, pte0, pte1, h, rw, type);
618
#if defined (DEBUG_MMU)
619
            if (loglevel != 0) {
620
                fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
621
                        " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
622
                        base + (i * 16), pte0, pte1,
623
                        (int)(pte0 & 1), h, (int)((pte0 >> 1) & 1),
624
                        ctx->ptem);
625
            }
626
#endif
627
        } else
628
#endif
629
        {
630
            pte0 = ldl_phys(base + (i * 8));
631
            pte1 =  ldl_phys(base + (i * 8) + 4);
632
            r = pte32_check(ctx, pte0, pte1, h, rw, type);
633
#if defined (DEBUG_MMU)
634
            if (loglevel != 0) {
635
                fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
636
                        " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
637
                        base + (i * 8), pte0, pte1,
638
                        (int)(pte0 >> 31), h, (int)((pte0 >> 6) & 1),
639
                        ctx->ptem);
640
            }
641
#endif
642
        }
643
        switch (r) {
644
        case -3:
645
            /* PTE inconsistency */
646
            return -1;
647
        case -2:
648
            /* Access violation */
649
            ret = -2;
650
            good = i;
651
            break;
652
        case -1:
653
        default:
654
            /* No PTE match */
655
            break;
656
        case 0:
657
            /* access granted */
658
            /* XXX: we should go on looping to check all PTEs consistency
659
             *      but if we can speed-up the whole thing as the
660
             *      result would be undefined if PTEs are not consistent.
661
             */
662
            ret = 0;
663
            good = i;
664
            goto done;
665
        }
666
    }
667
    if (good != -1) {
668
    done:
669
#if defined (DEBUG_MMU)
670
        if (loglevel != 0) {
671
            fprintf(logfile, "found PTE at addr 0x" PADDRX " prot=0x%01x "
672
                    "ret=%d\n",
673
                    ctx->raddr, ctx->prot, ret);
674
        }
675
#endif
676
        /* Update page flags */
677
        pte1 = ctx->raddr;
678
        if (pte_update_flags(ctx, &pte1, ret, rw) == 1) {
679
#if defined(TARGET_PPC64)
680
            if (is_64b) {
681
                stq_phys_notdirty(base + (good * 16) + 8, pte1);
682
            } else
683
#endif
684
            {
685
                stl_phys_notdirty(base + (good * 8) + 4, pte1);
686
            }
687
        }
688
    }
689

    
690
    return ret;
691
}
692

    
693
static always_inline int find_pte32 (mmu_ctx_t *ctx, int h, int rw, int type)
694
{
695
    return _find_pte(ctx, 0, h, rw, type);
696
}
697

    
698
#if defined(TARGET_PPC64)
699
static always_inline int find_pte64 (mmu_ctx_t *ctx, int h, int rw, int type)
700
{
701
    return _find_pte(ctx, 1, h, rw, type);
702
}
703
#endif
704

    
705
static always_inline int find_pte (CPUState *env, mmu_ctx_t *ctx,
706
                                   int h, int rw, int type)
707
{
708
#if defined(TARGET_PPC64)
709
    if (env->mmu_model & POWERPC_MMU_64)
710
        return find_pte64(ctx, h, rw, type);
711
#endif
712

    
713
    return find_pte32(ctx, h, rw, type);
714
}
715

    
716
#if defined(TARGET_PPC64)
717
static always_inline int slb_is_valid (uint64_t slb64)
718
{
719
    return slb64 & 0x0000000008000000ULL ? 1 : 0;
720
}
721

    
722
static always_inline void slb_invalidate (uint64_t *slb64)
723
{
724
    *slb64 &= ~0x0000000008000000ULL;
725
}
726

    
727
static always_inline int slb_lookup (CPUPPCState *env, target_ulong eaddr,
728
                                     target_ulong *vsid,
729
                                     target_ulong *page_mask, int *attr)
730
{
731
    target_phys_addr_t sr_base;
732
    target_ulong mask;
733
    uint64_t tmp64;
734
    uint32_t tmp;
735
    int n, ret;
736

    
737
    ret = -5;
738
    sr_base = env->spr[SPR_ASR];
739
#if defined(DEBUG_SLB)
740
    if (loglevel != 0) {
741
        fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
742
                __func__, eaddr, sr_base);
743
    }
744
#endif
745
    mask = 0x0000000000000000ULL; /* Avoid gcc warning */
746
    for (n = 0; n < env->slb_nr; n++) {
747
        tmp64 = ldq_phys(sr_base);
748
        tmp = ldl_phys(sr_base + 8);
749
#if defined(DEBUG_SLB)
750
        if (loglevel != 0) {
751
            fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08"
752
                    PRIx32 "\n", __func__, n, sr_base, tmp64, tmp);
753
        }
754
#endif
755
        if (slb_is_valid(tmp64)) {
756
            /* SLB entry is valid */
757
            switch (tmp64 & 0x0000000006000000ULL) {
758
            case 0x0000000000000000ULL:
759
                /* 256 MB segment */
760
                mask = 0xFFFFFFFFF0000000ULL;
761
                break;
762
            case 0x0000000002000000ULL:
763
                /* 1 TB segment */
764
                mask = 0xFFFF000000000000ULL;
765
                break;
766
            case 0x0000000004000000ULL:
767
            case 0x0000000006000000ULL:
768
                /* Reserved => segment is invalid */
769
                continue;
770
            }
771
            if ((eaddr & mask) == (tmp64 & mask)) {
772
                /* SLB match */
773
                *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
774
                *page_mask = ~mask;
775
                *attr = tmp & 0xFF;
776
                ret = n;
777
                break;
778
            }
779
        }
780
        sr_base += 12;
781
    }
782

    
783
    return ret;
784
}
785

    
786
void ppc_slb_invalidate_all (CPUPPCState *env)
787
{
788
    target_phys_addr_t sr_base;
789
    uint64_t tmp64;
790
    int n, do_invalidate;
791

    
792
    do_invalidate = 0;
793
    sr_base = env->spr[SPR_ASR];
794
    /* XXX: Warning: slbia never invalidates the first segment */
795
    for (n = 1; n < env->slb_nr; n++) {
796
        tmp64 = ldq_phys(sr_base);
797
        if (slb_is_valid(tmp64)) {
798
            slb_invalidate(&tmp64);
799
            stq_phys(sr_base, tmp64);
800
            /* XXX: given the fact that segment size is 256 MB or 1TB,
801
             *      and we still don't have a tlb_flush_mask(env, n, mask)
802
             *      in Qemu, we just invalidate all TLBs
803
             */
804
            do_invalidate = 1;
805
        }
806
        sr_base += 12;
807
    }
808
    if (do_invalidate)
809
        tlb_flush(env, 1);
810
}
811

    
812
void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
813
{
814
    target_phys_addr_t sr_base;
815
    target_ulong vsid, page_mask;
816
    uint64_t tmp64;
817
    int attr;
818
    int n;
819

    
820
    n = slb_lookup(env, T0, &vsid, &page_mask, &attr);
821
    if (n >= 0) {
822
        sr_base = env->spr[SPR_ASR];
823
        sr_base += 12 * n;
824
        tmp64 = ldq_phys(sr_base);
825
        if (slb_is_valid(tmp64)) {
826
            slb_invalidate(&tmp64);
827
            stq_phys(sr_base, tmp64);
828
            /* XXX: given the fact that segment size is 256 MB or 1TB,
829
             *      and we still don't have a tlb_flush_mask(env, n, mask)
830
             *      in Qemu, we just invalidate all TLBs
831
             */
832
            tlb_flush(env, 1);
833
        }
834
    }
835
}
836

    
837
target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
838
{
839
    target_phys_addr_t sr_base;
840
    target_ulong rt;
841
    uint64_t tmp64;
842
    uint32_t tmp;
843

    
844
    sr_base = env->spr[SPR_ASR];
845
    sr_base += 12 * slb_nr;
846
    tmp64 = ldq_phys(sr_base);
847
    tmp = ldl_phys(sr_base + 8);
848
    if (tmp64 & 0x0000000008000000ULL) {
849
        /* SLB entry is valid */
850
        /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
851
        rt = tmp >> 8;             /* 65:88 => 40:63 */
852
        rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
853
        /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
854
        rt |= ((tmp >> 4) & 0xF) << 27;
855
    } else {
856
        rt = 0;
857
    }
858
#if defined(DEBUG_SLB)
859
    if (loglevel != 0) {
860
        fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
861
                ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
862
    }
863
#endif
864

    
865
    return rt;
866
}
867

    
868
void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
869
{
870
    target_phys_addr_t sr_base;
871
    uint64_t tmp64;
872
    uint32_t tmp;
873

    
874
    sr_base = env->spr[SPR_ASR];
875
    sr_base += 12 * slb_nr;
876
    /* Copy Rs bits 37:63 to SLB 62:88 */
877
    tmp = rs << 8;
878
    tmp64 = (rs >> 24) & 0x7;
879
    /* Copy Rs bits 33:36 to SLB 89:92 */
880
    tmp |= ((rs >> 27) & 0xF) << 4;
881
    /* Set the valid bit */
882
    tmp64 |= 1 << 27;
883
    /* Set ESID */
884
    tmp64 |= (uint32_t)slb_nr << 28;
885
#if defined(DEBUG_SLB)
886
    if (loglevel != 0) {
887
        fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64 " %08"
888
                PRIx32 "\n", __func__, slb_nr, rs, sr_base, tmp64, tmp);
889
    }
890
#endif
891
    /* Write SLB entry to memory */
892
    stq_phys(sr_base, tmp64);
893
    stl_phys(sr_base + 8, tmp);
894
}
895
#endif /* defined(TARGET_PPC64) */
896

    
897
/* Perform segment based translation */
898
static always_inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
899
                                                    int sdr_sh,
900
                                                    target_phys_addr_t hash,
901
                                                    target_phys_addr_t mask)
902
{
903
    return (sdr1 & ((target_phys_addr_t)(-1ULL) << sdr_sh)) | (hash & mask);
904
}
905

    
906
static always_inline int get_segment (CPUState *env, mmu_ctx_t *ctx,
907
                                      target_ulong eaddr, int rw, int type)
908
{
909
    target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
910
    target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
911
#if defined(TARGET_PPC64)
912
    int attr;
913
#endif
914
    int ds, vsid_sh, sdr_sh, pr;
915
    int ret, ret2;
916

    
917
    pr = msr_pr;
918
#if defined(TARGET_PPC64)
919
    if (env->mmu_model & POWERPC_MMU_64) {
920
#if defined (DEBUG_MMU)
921
        if (loglevel != 0) {
922
            fprintf(logfile, "Check SLBs\n");
923
        }
924
#endif
925
        ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
926
        if (ret < 0)
927
            return ret;
928
        ctx->key = ((attr & 0x40) && (pr != 0)) ||
929
            ((attr & 0x80) && (pr == 0)) ? 1 : 0;
930
        ds = 0;
931
        ctx->nx = attr & 0x20 ? 1 : 0;
932
        vsid_mask = 0x00003FFFFFFFFF80ULL;
933
        vsid_sh = 7;
934
        sdr_sh = 18;
935
        sdr_mask = 0x3FF80;
936
    } else
937
#endif /* defined(TARGET_PPC64) */
938
    {
939
        sr = env->sr[eaddr >> 28];
940
        page_mask = 0x0FFFFFFF;
941
        ctx->key = (((sr & 0x20000000) && (pr != 0)) ||
942
                    ((sr & 0x40000000) && (pr == 0))) ? 1 : 0;
943
        ds = sr & 0x80000000 ? 1 : 0;
944
        ctx->nx = sr & 0x10000000 ? 1 : 0;
945
        vsid = sr & 0x00FFFFFF;
946
        vsid_mask = 0x01FFFFC0;
947
        vsid_sh = 6;
948
        sdr_sh = 16;
949
        sdr_mask = 0xFFC0;
950
#if defined (DEBUG_MMU)
951
        if (loglevel != 0) {
952
            fprintf(logfile, "Check segment v=0x" ADDRX " %d 0x" ADDRX
953
                    " nip=0x" ADDRX " lr=0x" ADDRX
954
                    " ir=%d dr=%d pr=%d %d t=%d\n",
955
                    eaddr, (int)(eaddr >> 28), sr, env->nip,
956
                    env->lr, (int)msr_ir, (int)msr_dr, pr != 0 ? 1 : 0,
957
                    rw, type);
958
        }
959
#endif
960
    }
961
#if defined (DEBUG_MMU)
962
    if (loglevel != 0) {
963
        fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
964
                ctx->key, ds, ctx->nx, vsid);
965
    }
966
#endif
967
    ret = -1;
968
    if (!ds) {
969
        /* Check if instruction fetch is allowed, if needed */
970
        if (type != ACCESS_CODE || ctx->nx == 0) {
971
            /* Page address translation */
972
            /* Primary table address */
973
            sdr = env->sdr1;
974
            pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
975
#if defined(TARGET_PPC64)
976
            if (env->mmu_model & POWERPC_MMU_64) {
977
                htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
978
                /* XXX: this is false for 1 TB segments */
979
                hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
980
            } else
981
#endif
982
            {
983
                htab_mask = sdr & 0x000001FF;
984
                hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
985
            }
986
            mask = (htab_mask << sdr_sh) | sdr_mask;
987
#if defined (DEBUG_MMU)
988
            if (loglevel != 0) {
989
                fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
990
                        PADDRX " " ADDRX "\n", sdr, sdr_sh, hash, mask,
991
                        page_mask);
992
            }
993
#endif
994
            ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
995
            /* Secondary table address */
996
            hash = (~hash) & vsid_mask;
997
#if defined (DEBUG_MMU)
998
            if (loglevel != 0) {
999
                fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
1000
                        PADDRX "\n", sdr, sdr_sh, hash, mask);
1001
            }
1002
#endif
1003
            ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
1004
#if defined(TARGET_PPC64)
1005
            if (env->mmu_model & POWERPC_MMU_64) {
1006
                /* Only 5 bits of the page index are used in the AVPN */
1007
                ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
1008
            } else
1009
#endif
1010
            {
1011
                ctx->ptem = (vsid << 7) | (pgidx >> 10);
1012
            }
1013
            /* Initialize real address with an invalid value */
1014
            ctx->raddr = (target_phys_addr_t)-1ULL;
1015
            if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
1016
                         env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
1017
                /* Software TLB search */
1018
                ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
1019
            } else {
1020
#if defined (DEBUG_MMU)
1021
                if (loglevel != 0) {
1022
                    fprintf(logfile, "0 sdr1=0x" PADDRX " vsid=0x%06x "
1023
                            "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX "\n",
1024
                            sdr, (uint32_t)vsid, (uint32_t)pgidx,
1025
                            (uint32_t)hash, ctx->pg_addr[0]);
1026
                }
1027
#endif
1028
                /* Primary table lookup */
1029
                ret = find_pte(env, ctx, 0, rw, type);
1030
                if (ret < 0) {
1031
                    /* Secondary table lookup */
1032
#if defined (DEBUG_MMU)
1033
                    if (eaddr != 0xEFFFFFFF && loglevel != 0) {
1034
                        fprintf(logfile,
1035
                                "1 sdr1=0x" PADDRX " vsid=0x%06x api=0x%04x "
1036
                                "hash=0x%05x pg_addr=0x" PADDRX "\n",
1037
                                sdr, (uint32_t)vsid, (uint32_t)pgidx,
1038
                                (uint32_t)hash, ctx->pg_addr[1]);
1039
                    }
1040
#endif
1041
                    ret2 = find_pte(env, ctx, 1, rw, type);
1042
                    if (ret2 != -1)
1043
                        ret = ret2;
1044
                }
1045
            }
1046
#if defined (DUMP_PAGE_TABLES)
1047
            if (loglevel != 0) {
1048
                target_phys_addr_t curaddr;
1049
                uint32_t a0, a1, a2, a3;
1050
                fprintf(logfile,
1051
                        "Page table: " PADDRX " len " PADDRX "\n",
1052
                        sdr, mask + 0x80);
1053
                for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
1054
                     curaddr += 16) {
1055
                    a0 = ldl_phys(curaddr);
1056
                    a1 = ldl_phys(curaddr + 4);
1057
                    a2 = ldl_phys(curaddr + 8);
1058
                    a3 = ldl_phys(curaddr + 12);
1059
                    if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
1060
                        fprintf(logfile,
1061
                                PADDRX ": %08x %08x %08x %08x\n",
1062
                                curaddr, a0, a1, a2, a3);
1063
                    }
1064
                }
1065
            }
1066
#endif
1067
        } else {
1068
#if defined (DEBUG_MMU)
1069
            if (loglevel != 0)
1070
                fprintf(logfile, "No access allowed\n");
1071
#endif
1072
            ret = -3;
1073
        }
1074
    } else {
1075
#if defined (DEBUG_MMU)
1076
        if (loglevel != 0)
1077
            fprintf(logfile, "direct store...\n");
1078
#endif
1079
        /* Direct-store segment : absolutely *BUGGY* for now */
1080
        switch (type) {
1081
        case ACCESS_INT:
1082
            /* Integer load/store : only access allowed */
1083
            break;
1084
        case ACCESS_CODE:
1085
            /* No code fetch is allowed in direct-store areas */
1086
            return -4;
1087
        case ACCESS_FLOAT:
1088
            /* Floating point load/store */
1089
            return -4;
1090
        case ACCESS_RES:
1091
            /* lwarx, ldarx or srwcx. */
1092
            return -4;
1093
        case ACCESS_CACHE:
1094
            /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1095
            /* Should make the instruction do no-op.
1096
             * As it already do no-op, it's quite easy :-)
1097
             */
1098
            ctx->raddr = eaddr;
1099
            return 0;
1100
        case ACCESS_EXT:
1101
            /* eciwx or ecowx */
1102
            return -4;
1103
        default:
1104
            if (logfile) {
1105
                fprintf(logfile, "ERROR: instruction should not need "
1106
                        "address translation\n");
1107
            }
1108
            return -4;
1109
        }
1110
        if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
1111
            ctx->raddr = eaddr;
1112
            ret = 2;
1113
        } else {
1114
            ret = -2;
1115
        }
1116
    }
1117

    
1118
    return ret;
1119
}
1120

    
1121
/* Generic TLB check function for embedded PowerPC implementations */
1122
static always_inline int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
1123
                                           target_phys_addr_t *raddrp,
1124
                                           target_ulong address,
1125
                                           uint32_t pid, int ext, int i)
1126
{
1127
    target_ulong mask;
1128

    
1129
    /* Check valid flag */
1130
    if (!(tlb->prot & PAGE_VALID)) {
1131
        if (loglevel != 0)
1132
            fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
1133
        return -1;
1134
    }
1135
    mask = ~(tlb->size - 1);
1136
#if defined (DEBUG_SOFTWARE_TLB)
1137
    if (loglevel != 0) {
1138
        fprintf(logfile, "%s: TLB %d address " ADDRX " PID %d <=> "
1139
                ADDRX " " ADDRX " %d\n",
1140
                __func__, i, address, pid, tlb->EPN, mask, (int)tlb->PID);
1141
    }
1142
#endif
1143
    /* Check PID */
1144
    if (tlb->PID != 0 && tlb->PID != pid)
1145
        return -1;
1146
    /* Check effective address */
1147
    if ((address & mask) != tlb->EPN)
1148
        return -1;
1149
    *raddrp = (tlb->RPN & mask) | (address & ~mask);
1150
#if (TARGET_PHYS_ADDR_BITS >= 36)
1151
    if (ext) {
1152
        /* Extend the physical address to 36 bits */
1153
        *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
1154
    }
1155
#endif
1156

    
1157
    return 0;
1158
}
1159

    
1160
/* Generic TLB search function for PowerPC embedded implementations */
1161
int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
1162
{
1163
    ppcemb_tlb_t *tlb;
1164
    target_phys_addr_t raddr;
1165
    int i, ret;
1166

    
1167
    /* Default return value is no match */
1168
    ret = -1;
1169
    for (i = 0; i < env->nb_tlb; i++) {
1170
        tlb = &env->tlb[i].tlbe;
1171
        if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
1172
            ret = i;
1173
            break;
1174
        }
1175
    }
1176

    
1177
    return ret;
1178
}
1179

    
1180
/* Helpers specific to PowerPC 40x implementations */
1181
static always_inline void ppc4xx_tlb_invalidate_all (CPUState *env)
1182
{
1183
    ppcemb_tlb_t *tlb;
1184
    int i;
1185

    
1186
    for (i = 0; i < env->nb_tlb; i++) {
1187
        tlb = &env->tlb[i].tlbe;
1188
        tlb->prot &= ~PAGE_VALID;
1189
    }
1190
    tlb_flush(env, 1);
1191
}
1192

    
1193
static always_inline void ppc4xx_tlb_invalidate_virt (CPUState *env,
1194
                                                      target_ulong eaddr,
1195
                                                      uint32_t pid)
1196
{
1197
#if !defined(FLUSH_ALL_TLBS)
1198
    ppcemb_tlb_t *tlb;
1199
    target_phys_addr_t raddr;
1200
    target_ulong page, end;
1201
    int i;
1202

    
1203
    for (i = 0; i < env->nb_tlb; i++) {
1204
        tlb = &env->tlb[i].tlbe;
1205
        if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
1206
            end = tlb->EPN + tlb->size;
1207
            for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1208
                tlb_flush_page(env, page);
1209
            tlb->prot &= ~PAGE_VALID;
1210
            break;
1211
        }
1212
    }
1213
#else
1214
    ppc4xx_tlb_invalidate_all(env);
1215
#endif
1216
}
1217

    
1218
int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1219
                                 target_ulong address, int rw, int access_type)
1220
{
1221
    ppcemb_tlb_t *tlb;
1222
    target_phys_addr_t raddr;
1223
    int i, ret, zsel, zpr, pr;
1224

    
1225
    ret = -1;
1226
    raddr = (target_phys_addr_t)-1ULL;
1227
    pr = msr_pr;
1228
    for (i = 0; i < env->nb_tlb; i++) {
1229
        tlb = &env->tlb[i].tlbe;
1230
        if (ppcemb_tlb_check(env, tlb, &raddr, address,
1231
                             env->spr[SPR_40x_PID], 0, i) < 0)
1232
            continue;
1233
        zsel = (tlb->attr >> 4) & 0xF;
1234
        zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
1235
#if defined (DEBUG_SOFTWARE_TLB)
1236
        if (loglevel != 0) {
1237
            fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1238
                    __func__, i, zsel, zpr, rw, tlb->attr);
1239
        }
1240
#endif
1241
        /* Check execute enable bit */
1242
        switch (zpr) {
1243
        case 0x2:
1244
            if (pr != 0)
1245
                goto check_perms;
1246
            /* No break here */
1247
        case 0x3:
1248
            /* All accesses granted */
1249
            ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1250
            ret = 0;
1251
            break;
1252
        case 0x0:
1253
            if (pr != 0) {
1254
                ctx->prot = 0;
1255
                ret = -2;
1256
                break;
1257
            }
1258
            /* No break here */
1259
        case 0x1:
1260
        check_perms:
1261
            /* Check from TLB entry */
1262
            /* XXX: there is a problem here or in the TLB fill code... */
1263
            ctx->prot = tlb->prot;
1264
            ctx->prot |= PAGE_EXEC;
1265
            ret = check_prot(ctx->prot, rw, access_type);
1266
            break;
1267
        }
1268
        if (ret >= 0) {
1269
            ctx->raddr = raddr;
1270
#if defined (DEBUG_SOFTWARE_TLB)
1271
            if (loglevel != 0) {
1272
                fprintf(logfile, "%s: access granted " ADDRX " => " REGX
1273
                        " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1274
                        ret);
1275
            }
1276
#endif
1277
            return 0;
1278
        }
1279
    }
1280
#if defined (DEBUG_SOFTWARE_TLB)
1281
    if (loglevel != 0) {
1282
        fprintf(logfile, "%s: access refused " ADDRX " => " REGX
1283
                " %d %d\n", __func__, address, raddr, ctx->prot,
1284
                ret);
1285
    }
1286
#endif
1287

    
1288
    return ret;
1289
}
1290

    
1291
void store_40x_sler (CPUPPCState *env, uint32_t val)
1292
{
1293
    /* XXX: TO BE FIXED */
1294
    if (val != 0x00000000) {
1295
        cpu_abort(env, "Little-endian regions are not supported by now\n");
1296
    }
1297
    env->spr[SPR_405_SLER] = val;
1298
}
1299

    
1300
int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1301
                                   target_ulong address, int rw,
1302
                                   int access_type)
1303
{
1304
    ppcemb_tlb_t *tlb;
1305
    target_phys_addr_t raddr;
1306
    int i, prot, ret;
1307

    
1308
    ret = -1;
1309
    raddr = (target_phys_addr_t)-1ULL;
1310
    for (i = 0; i < env->nb_tlb; i++) {
1311
        tlb = &env->tlb[i].tlbe;
1312
        if (ppcemb_tlb_check(env, tlb, &raddr, address,
1313
                             env->spr[SPR_BOOKE_PID], 1, i) < 0)
1314
            continue;
1315
        if (msr_pr != 0)
1316
            prot = tlb->prot & 0xF;
1317
        else
1318
            prot = (tlb->prot >> 4) & 0xF;
1319
        /* Check the address space */
1320
        if (access_type == ACCESS_CODE) {
1321
            if (msr_ir != (tlb->attr & 1))
1322
                continue;
1323
            ctx->prot = prot;
1324
            if (prot & PAGE_EXEC) {
1325
                ret = 0;
1326
                break;
1327
            }
1328
            ret = -3;
1329
        } else {
1330
            if (msr_dr != (tlb->attr & 1))
1331
                continue;
1332
            ctx->prot = prot;
1333
            if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1334
                ret = 0;
1335
                break;
1336
            }
1337
            ret = -2;
1338
        }
1339
    }
1340
    if (ret >= 0)
1341
        ctx->raddr = raddr;
1342

    
1343
    return ret;
1344
}
1345

    
1346
static always_inline int check_physical (CPUState *env, mmu_ctx_t *ctx,
1347
                                         target_ulong eaddr, int rw)
1348
{
1349
    int in_plb, ret;
1350

    
1351
    ctx->raddr = eaddr;
1352
    ctx->prot = PAGE_READ | PAGE_EXEC;
1353
    ret = 0;
1354
    switch (env->mmu_model) {
1355
    case POWERPC_MMU_32B:
1356
    case POWERPC_MMU_601:
1357
    case POWERPC_MMU_SOFT_6xx:
1358
    case POWERPC_MMU_SOFT_74xx:
1359
    case POWERPC_MMU_SOFT_4xx:
1360
    case POWERPC_MMU_REAL:
1361
    case POWERPC_MMU_BOOKE:
1362
        ctx->prot |= PAGE_WRITE;
1363
        break;
1364
#if defined(TARGET_PPC64)
1365
    case POWERPC_MMU_620:
1366
    case POWERPC_MMU_64B:
1367
        /* Real address are 60 bits long */
1368
        ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1369
        ctx->prot |= PAGE_WRITE;
1370
        break;
1371
#endif
1372
    case POWERPC_MMU_SOFT_4xx_Z:
1373
        if (unlikely(msr_pe != 0)) {
1374
            /* 403 family add some particular protections,
1375
             * using PBL/PBU registers for accesses with no translation.
1376
             */
1377
            in_plb =
1378
                /* Check PLB validity */
1379
                (env->pb[0] < env->pb[1] &&
1380
                 /* and address in plb area */
1381
                 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1382
                (env->pb[2] < env->pb[3] &&
1383
                 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1384
            if (in_plb ^ msr_px) {
1385
                /* Access in protected area */
1386
                if (rw == 1) {
1387
                    /* Access is not allowed */
1388
                    ret = -2;
1389
                }
1390
            } else {
1391
                /* Read-write access is allowed */
1392
                ctx->prot |= PAGE_WRITE;
1393
            }
1394
        }
1395
        break;
1396
    case POWERPC_MMU_MPC8xx:
1397
        /* XXX: TODO */
1398
        cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1399
        break;
1400
    case POWERPC_MMU_BOOKE_FSL:
1401
        /* XXX: TODO */
1402
        cpu_abort(env, "BookE FSL MMU model not implemented\n");
1403
        break;
1404
    default:
1405
        cpu_abort(env, "Unknown or invalid MMU model\n");
1406
        return -1;
1407
    }
1408

    
1409
    return ret;
1410
}
1411

    
1412
int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1413
                          int rw, int access_type)
1414
{
1415
    int ret;
1416

    
1417
#if 0
1418
    if (loglevel != 0) {
1419
        fprintf(logfile, "%s\n", __func__);
1420
    }
1421
#endif
1422
    if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1423
        (access_type != ACCESS_CODE && msr_dr == 0)) {
1424
        /* No address translation */
1425
        ret = check_physical(env, ctx, eaddr, rw);
1426
    } else {
1427
        ret = -1;
1428
        switch (env->mmu_model) {
1429
        case POWERPC_MMU_32B:
1430
        case POWERPC_MMU_601:
1431
        case POWERPC_MMU_SOFT_6xx:
1432
        case POWERPC_MMU_SOFT_74xx:
1433
#if defined(TARGET_PPC64)
1434
        case POWERPC_MMU_620:
1435
        case POWERPC_MMU_64B:
1436
#endif
1437
            /* Try to find a BAT */
1438
            if (env->nb_BATs != 0)
1439
                ret = get_bat(env, ctx, eaddr, rw, access_type);
1440
            if (ret < 0) {
1441
                /* We didn't match any BAT entry or don't have BATs */
1442
                ret = get_segment(env, ctx, eaddr, rw, access_type);
1443
            }
1444
            break;
1445
        case POWERPC_MMU_SOFT_4xx:
1446
        case POWERPC_MMU_SOFT_4xx_Z:
1447
            ret = mmu40x_get_physical_address(env, ctx, eaddr,
1448
                                              rw, access_type);
1449
            break;
1450
        case POWERPC_MMU_BOOKE:
1451
            ret = mmubooke_get_physical_address(env, ctx, eaddr,
1452
                                                rw, access_type);
1453
            break;
1454
        case POWERPC_MMU_MPC8xx:
1455
            /* XXX: TODO */
1456
            cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1457
            break;
1458
        case POWERPC_MMU_BOOKE_FSL:
1459
            /* XXX: TODO */
1460
            cpu_abort(env, "BookE FSL MMU model not implemented\n");
1461
            return -1;
1462
        case POWERPC_MMU_REAL:
1463
            cpu_abort(env, "PowerPC in real mode do not do any translation\n");
1464
            return -1;
1465
        default:
1466
            cpu_abort(env, "Unknown or invalid MMU model\n");
1467
            return -1;
1468
        }
1469
    }
1470
#if 0
1471
    if (loglevel != 0) {
1472
        fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1473
                __func__, eaddr, ret, ctx->raddr);
1474
    }
1475
#endif
1476

    
1477
    return ret;
1478
}
1479

    
1480
target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1481
{
1482
    mmu_ctx_t ctx;
1483

    
1484
    if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT) != 0))
1485
        return -1;
1486

    
1487
    return ctx.raddr & TARGET_PAGE_MASK;
1488
}
1489

    
1490
/* Perform address translation */
1491
int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1492
                              int mmu_idx, int is_softmmu)
1493
{
1494
    mmu_ctx_t ctx;
1495
    int access_type;
1496
    int ret = 0;
1497

    
1498
    if (rw == 2) {
1499
        /* code access */
1500
        rw = 0;
1501
        access_type = ACCESS_CODE;
1502
    } else {
1503
        /* data access */
1504
        /* XXX: put correct access by using cpu_restore_state()
1505
           correctly */
1506
        access_type = ACCESS_INT;
1507
        //        access_type = env->access_type;
1508
    }
1509
    ret = get_physical_address(env, &ctx, address, rw, access_type);
1510
    if (ret == 0) {
1511
        ret = tlb_set_page_exec(env, address & TARGET_PAGE_MASK,
1512
                                ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1513
                                mmu_idx, is_softmmu);
1514
    } else if (ret < 0) {
1515
#if defined (DEBUG_MMU)
1516
        if (loglevel != 0)
1517
            cpu_dump_state(env, logfile, fprintf, 0);
1518
#endif
1519
        if (access_type == ACCESS_CODE) {
1520
            switch (ret) {
1521
            case -1:
1522
                /* No matches in page tables or TLB */
1523
                switch (env->mmu_model) {
1524
                case POWERPC_MMU_SOFT_6xx:
1525
                    env->exception_index = POWERPC_EXCP_IFTLB;
1526
                    env->error_code = 1 << 18;
1527
                    env->spr[SPR_IMISS] = address;
1528
                    env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1529
                    goto tlb_miss;
1530
                case POWERPC_MMU_SOFT_74xx:
1531
                    env->exception_index = POWERPC_EXCP_IFTLB;
1532
                    goto tlb_miss_74xx;
1533
                case POWERPC_MMU_SOFT_4xx:
1534
                case POWERPC_MMU_SOFT_4xx_Z:
1535
                    env->exception_index = POWERPC_EXCP_ITLB;
1536
                    env->error_code = 0;
1537
                    env->spr[SPR_40x_DEAR] = address;
1538
                    env->spr[SPR_40x_ESR] = 0x00000000;
1539
                    break;
1540
                case POWERPC_MMU_32B:
1541
                case POWERPC_MMU_601:
1542
#if defined(TARGET_PPC64)
1543
                case POWERPC_MMU_620:
1544
                case POWERPC_MMU_64B:
1545
#endif
1546
                    env->exception_index = POWERPC_EXCP_ISI;
1547
                    env->error_code = 0x40000000;
1548
                    break;
1549
                case POWERPC_MMU_BOOKE:
1550
                    /* XXX: TODO */
1551
                    cpu_abort(env, "BookE MMU model is not implemented\n");
1552
                    return -1;
1553
                case POWERPC_MMU_BOOKE_FSL:
1554
                    /* XXX: TODO */
1555
                    cpu_abort(env, "BookE FSL MMU model is not implemented\n");
1556
                    return -1;
1557
                case POWERPC_MMU_MPC8xx:
1558
                    /* XXX: TODO */
1559
                    cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1560
                    break;
1561
                case POWERPC_MMU_REAL:
1562
                    cpu_abort(env, "PowerPC in real mode should never raise "
1563
                              "any MMU exceptions\n");
1564
                    return -1;
1565
                default:
1566
                    cpu_abort(env, "Unknown or invalid MMU model\n");
1567
                    return -1;
1568
                }
1569
                break;
1570
            case -2:
1571
                /* Access rights violation */
1572
                env->exception_index = POWERPC_EXCP_ISI;
1573
                env->error_code = 0x08000000;
1574
                break;
1575
            case -3:
1576
                /* No execute protection violation */
1577
                env->exception_index = POWERPC_EXCP_ISI;
1578
                env->error_code = 0x10000000;
1579
                break;
1580
            case -4:
1581
                /* Direct store exception */
1582
                /* No code fetch is allowed in direct-store areas */
1583
                env->exception_index = POWERPC_EXCP_ISI;
1584
                env->error_code = 0x10000000;
1585
                break;
1586
#if defined(TARGET_PPC64)
1587
            case -5:
1588
                /* No match in segment table */
1589
                if (env->mmu_model == POWERPC_MMU_620) {
1590
                    env->exception_index = POWERPC_EXCP_ISI;
1591
                    /* XXX: this might be incorrect */
1592
                    env->error_code = 0x40000000;
1593
                } else {
1594
                    env->exception_index = POWERPC_EXCP_ISEG;
1595
                    env->error_code = 0;
1596
                }
1597
                break;
1598
#endif
1599
            }
1600
        } else {
1601
            switch (ret) {
1602
            case -1:
1603
                /* No matches in page tables or TLB */
1604
                switch (env->mmu_model) {
1605
                case POWERPC_MMU_SOFT_6xx:
1606
                    if (rw == 1) {
1607
                        env->exception_index = POWERPC_EXCP_DSTLB;
1608
                        env->error_code = 1 << 16;
1609
                    } else {
1610
                        env->exception_index = POWERPC_EXCP_DLTLB;
1611
                        env->error_code = 0;
1612
                    }
1613
                    env->spr[SPR_DMISS] = address;
1614
                    env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1615
                tlb_miss:
1616
                    env->error_code |= ctx.key << 19;
1617
                    env->spr[SPR_HASH1] = ctx.pg_addr[0];
1618
                    env->spr[SPR_HASH2] = ctx.pg_addr[1];
1619
                    break;
1620
                case POWERPC_MMU_SOFT_74xx:
1621
                    if (rw == 1) {
1622
                        env->exception_index = POWERPC_EXCP_DSTLB;
1623
                    } else {
1624
                        env->exception_index = POWERPC_EXCP_DLTLB;
1625
                    }
1626
                tlb_miss_74xx:
1627
                    /* Implement LRU algorithm */
1628
                    env->error_code = ctx.key << 19;
1629
                    env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1630
                        ((env->last_way + 1) & (env->nb_ways - 1));
1631
                    env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
1632
                    break;
1633
                case POWERPC_MMU_SOFT_4xx:
1634
                case POWERPC_MMU_SOFT_4xx_Z:
1635
                    env->exception_index = POWERPC_EXCP_DTLB;
1636
                    env->error_code = 0;
1637
                    env->spr[SPR_40x_DEAR] = address;
1638
                    if (rw)
1639
                        env->spr[SPR_40x_ESR] = 0x00800000;
1640
                    else
1641
                        env->spr[SPR_40x_ESR] = 0x00000000;
1642
                    break;
1643
                case POWERPC_MMU_32B:
1644
                case POWERPC_MMU_601:
1645
#if defined(TARGET_PPC64)
1646
                case POWERPC_MMU_620:
1647
                case POWERPC_MMU_64B:
1648
#endif
1649
                    env->exception_index = POWERPC_EXCP_DSI;
1650
                    env->error_code = 0;
1651
                    env->spr[SPR_DAR] = address;
1652
                    if (rw == 1)
1653
                        env->spr[SPR_DSISR] = 0x42000000;
1654
                    else
1655
                        env->spr[SPR_DSISR] = 0x40000000;
1656
                    break;
1657
                case POWERPC_MMU_MPC8xx:
1658
                    /* XXX: TODO */
1659
                    cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1660
                    break;
1661
                case POWERPC_MMU_BOOKE:
1662
                    /* XXX: TODO */
1663
                    cpu_abort(env, "BookE MMU model is not implemented\n");
1664
                    return -1;
1665
                case POWERPC_MMU_BOOKE_FSL:
1666
                    /* XXX: TODO */
1667
                    cpu_abort(env, "BookE FSL MMU model is not implemented\n");
1668
                    return -1;
1669
                case POWERPC_MMU_REAL:
1670
                    cpu_abort(env, "PowerPC in real mode should never raise "
1671
                              "any MMU exceptions\n");
1672
                    return -1;
1673
                default:
1674
                    cpu_abort(env, "Unknown or invalid MMU model\n");
1675
                    return -1;
1676
                }
1677
                break;
1678
            case -2:
1679
                /* Access rights violation */
1680
                env->exception_index = POWERPC_EXCP_DSI;
1681
                env->error_code = 0;
1682
                env->spr[SPR_DAR] = address;
1683
                if (rw == 1)
1684
                    env->spr[SPR_DSISR] = 0x0A000000;
1685
                else
1686
                    env->spr[SPR_DSISR] = 0x08000000;
1687
                break;
1688
            case -4:
1689
                /* Direct store exception */
1690
                switch (access_type) {
1691
                case ACCESS_FLOAT:
1692
                    /* Floating point load/store */
1693
                    env->exception_index = POWERPC_EXCP_ALIGN;
1694
                    env->error_code = POWERPC_EXCP_ALIGN_FP;
1695
                    env->spr[SPR_DAR] = address;
1696
                    break;
1697
                case ACCESS_RES:
1698
                    /* lwarx, ldarx or stwcx. */
1699
                    env->exception_index = POWERPC_EXCP_DSI;
1700
                    env->error_code = 0;
1701
                    env->spr[SPR_DAR] = address;
1702
                    if (rw == 1)
1703
                        env->spr[SPR_DSISR] = 0x06000000;
1704
                    else
1705
                        env->spr[SPR_DSISR] = 0x04000000;
1706
                    break;
1707
                case ACCESS_EXT:
1708
                    /* eciwx or ecowx */
1709
                    env->exception_index = POWERPC_EXCP_DSI;
1710
                    env->error_code = 0;
1711
                    env->spr[SPR_DAR] = address;
1712
                    if (rw == 1)
1713
                        env->spr[SPR_DSISR] = 0x06100000;
1714
                    else
1715
                        env->spr[SPR_DSISR] = 0x04100000;
1716
                    break;
1717
                default:
1718
                    printf("DSI: invalid exception (%d)\n", ret);
1719
                    env->exception_index = POWERPC_EXCP_PROGRAM;
1720
                    env->error_code =
1721
                        POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1722
                    env->spr[SPR_DAR] = address;
1723
                    break;
1724
                }
1725
                break;
1726
#if defined(TARGET_PPC64)
1727
            case -5:
1728
                /* No match in segment table */
1729
                if (env->mmu_model == POWERPC_MMU_620) {
1730
                    env->exception_index = POWERPC_EXCP_DSI;
1731
                    env->error_code = 0;
1732
                    env->spr[SPR_DAR] = address;
1733
                    /* XXX: this might be incorrect */
1734
                    if (rw == 1)
1735
                        env->spr[SPR_DSISR] = 0x42000000;
1736
                    else
1737
                        env->spr[SPR_DSISR] = 0x40000000;
1738
                } else {
1739
                    env->exception_index = POWERPC_EXCP_DSEG;
1740
                    env->error_code = 0;
1741
                    env->spr[SPR_DAR] = address;
1742
                }
1743
                break;
1744
#endif
1745
            }
1746
        }
1747
#if 0
1748
        printf("%s: set exception to %d %02x\n", __func__,
1749
               env->exception, env->error_code);
1750
#endif
1751
        ret = 1;
1752
    }
1753

    
1754
    return ret;
1755
}
1756

    
1757
/*****************************************************************************/
1758
/* BATs management */
1759
#if !defined(FLUSH_ALL_TLBS)
1760
static always_inline void do_invalidate_BAT (CPUPPCState *env,
1761
                                             target_ulong BATu,
1762
                                             target_ulong mask)
1763
{
1764
    target_ulong base, end, page;
1765

    
1766
    base = BATu & ~0x0001FFFF;
1767
    end = base + mask + 0x00020000;
1768
#if defined (DEBUG_BATS)
1769
    if (loglevel != 0) {
1770
        fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1771
                base, end, mask);
1772
    }
1773
#endif
1774
    for (page = base; page != end; page += TARGET_PAGE_SIZE)
1775
        tlb_flush_page(env, page);
1776
#if defined (DEBUG_BATS)
1777
    if (loglevel != 0)
1778
        fprintf(logfile, "Flush done\n");
1779
#endif
1780
}
1781
#endif
1782

    
1783
static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1784
                                          int ul, int nr, target_ulong value)
1785
{
1786
#if defined (DEBUG_BATS)
1787
    if (loglevel != 0) {
1788
        fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1789
                ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1790
    }
1791
#endif
1792
}
1793

    
1794
target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1795
{
1796
    return env->IBAT[0][nr];
1797
}
1798

    
1799
target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1800
{
1801
    return env->IBAT[1][nr];
1802
}
1803

    
1804
void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1805
{
1806
    target_ulong mask;
1807

    
1808
    dump_store_bat(env, 'I', 0, nr, value);
1809
    if (env->IBAT[0][nr] != value) {
1810
        mask = (value << 15) & 0x0FFE0000UL;
1811
#if !defined(FLUSH_ALL_TLBS)
1812
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1813
#endif
1814
        /* When storing valid upper BAT, mask BEPI and BRPN
1815
         * and invalidate all TLBs covered by this BAT
1816
         */
1817
        mask = (value << 15) & 0x0FFE0000UL;
1818
        env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1819
            (value & ~0x0001FFFFUL & ~mask);
1820
        env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1821
            (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1822
#if !defined(FLUSH_ALL_TLBS)
1823
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1824
#else
1825
        tlb_flush(env, 1);
1826
#endif
1827
    }
1828
}
1829

    
1830
void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1831
{
1832
    dump_store_bat(env, 'I', 1, nr, value);
1833
    env->IBAT[1][nr] = value;
1834
}
1835

    
1836
target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1837
{
1838
    return env->DBAT[0][nr];
1839
}
1840

    
1841
target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1842
{
1843
    return env->DBAT[1][nr];
1844
}
1845

    
1846
void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1847
{
1848
    target_ulong mask;
1849

    
1850
    dump_store_bat(env, 'D', 0, nr, value);
1851
    if (env->DBAT[0][nr] != value) {
1852
        /* When storing valid upper BAT, mask BEPI and BRPN
1853
         * and invalidate all TLBs covered by this BAT
1854
         */
1855
        mask = (value << 15) & 0x0FFE0000UL;
1856
#if !defined(FLUSH_ALL_TLBS)
1857
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1858
#endif
1859
        mask = (value << 15) & 0x0FFE0000UL;
1860
        env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1861
            (value & ~0x0001FFFFUL & ~mask);
1862
        env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1863
            (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1864
#if !defined(FLUSH_ALL_TLBS)
1865
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1866
#else
1867
        tlb_flush(env, 1);
1868
#endif
1869
    }
1870
}
1871

    
1872
void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1873
{
1874
    dump_store_bat(env, 'D', 1, nr, value);
1875
    env->DBAT[1][nr] = value;
1876
}
1877

    
1878
void do_store_ibatu_601 (CPUPPCState *env, int nr, target_ulong value)
1879
{
1880
    target_ulong mask;
1881
    int do_inval;
1882

    
1883
    dump_store_bat(env, 'I', 0, nr, value);
1884
    if (env->IBAT[0][nr] != value) {
1885
        do_inval = 0;
1886
        mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1887
        if (env->IBAT[1][nr] & 0x40) {
1888
            /* Invalidate BAT only if it is valid */
1889
#if !defined(FLUSH_ALL_TLBS)
1890
            do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1891
#else
1892
            do_inval = 1;
1893
#endif
1894
        }
1895
        /* When storing valid upper BAT, mask BEPI and BRPN
1896
         * and invalidate all TLBs covered by this BAT
1897
         */
1898
        env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1899
            (value & ~0x0001FFFFUL & ~mask);
1900
        env->DBAT[0][nr] = env->IBAT[0][nr];
1901
        if (env->IBAT[1][nr] & 0x40) {
1902
#if !defined(FLUSH_ALL_TLBS)
1903
            do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1904
#else
1905
            do_inval = 1;
1906
#endif
1907
        }
1908
#if defined(FLUSH_ALL_TLBS)
1909
        if (do_inval)
1910
            tlb_flush(env, 1);
1911
#endif
1912
    }
1913
}
1914

    
1915
void do_store_ibatl_601 (CPUPPCState *env, int nr, target_ulong value)
1916
{
1917
    target_ulong mask;
1918
    int do_inval;
1919

    
1920
    dump_store_bat(env, 'I', 1, nr, value);
1921
    if (env->IBAT[1][nr] != value) {
1922
        do_inval = 0;
1923
        if (env->IBAT[1][nr] & 0x40) {
1924
#if !defined(FLUSH_ALL_TLBS)
1925
            mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1926
            do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1927
#else
1928
            do_inval = 1;
1929
#endif
1930
        }
1931
        if (value & 0x40) {
1932
#if !defined(FLUSH_ALL_TLBS)
1933
            mask = (value << 17) & 0x0FFE0000UL;
1934
            do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1935
#else
1936
            do_inval = 1;
1937
#endif
1938
        }
1939
        env->IBAT[1][nr] = value;
1940
        env->DBAT[1][nr] = value;
1941
#if defined(FLUSH_ALL_TLBS)
1942
        if (do_inval)
1943
            tlb_flush(env, 1);
1944
#endif
1945
    }
1946
}
1947

    
1948
/*****************************************************************************/
1949
/* TLB management */
1950
void ppc_tlb_invalidate_all (CPUPPCState *env)
1951
{
1952
    switch (env->mmu_model) {
1953
    case POWERPC_MMU_SOFT_6xx:
1954
    case POWERPC_MMU_SOFT_74xx:
1955
        ppc6xx_tlb_invalidate_all(env);
1956
        break;
1957
    case POWERPC_MMU_SOFT_4xx:
1958
    case POWERPC_MMU_SOFT_4xx_Z:
1959
        ppc4xx_tlb_invalidate_all(env);
1960
        break;
1961
    case POWERPC_MMU_REAL:
1962
        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1963
        break;
1964
    case POWERPC_MMU_MPC8xx:
1965
        /* XXX: TODO */
1966
        cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1967
        break;
1968
    case POWERPC_MMU_BOOKE:
1969
        /* XXX: TODO */
1970
        cpu_abort(env, "BookE MMU model is not implemented\n");
1971
        break;
1972
    case POWERPC_MMU_BOOKE_FSL:
1973
        /* XXX: TODO */
1974
        cpu_abort(env, "BookE MMU model is not implemented\n");
1975
        break;
1976
    case POWERPC_MMU_32B:
1977
    case POWERPC_MMU_601:
1978
#if defined(TARGET_PPC64)
1979
    case POWERPC_MMU_620:
1980
    case POWERPC_MMU_64B:
1981
#endif /* defined(TARGET_PPC64) */
1982
        tlb_flush(env, 1);
1983
        break;
1984
    default:
1985
        /* XXX: TODO */
1986
        cpu_abort(env, "Unknown MMU model\n");
1987
        break;
1988
    }
1989
}
1990

    
1991
void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1992
{
1993
#if !defined(FLUSH_ALL_TLBS)
1994
    addr &= TARGET_PAGE_MASK;
1995
    switch (env->mmu_model) {
1996
    case POWERPC_MMU_SOFT_6xx:
1997
    case POWERPC_MMU_SOFT_74xx:
1998
        ppc6xx_tlb_invalidate_virt(env, addr, 0);
1999
        if (env->id_tlbs == 1)
2000
            ppc6xx_tlb_invalidate_virt(env, addr, 1);
2001
        break;
2002
    case POWERPC_MMU_SOFT_4xx:
2003
    case POWERPC_MMU_SOFT_4xx_Z:
2004
        ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
2005
        break;
2006
    case POWERPC_MMU_REAL:
2007
        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
2008
        break;
2009
    case POWERPC_MMU_MPC8xx:
2010
        /* XXX: TODO */
2011
        cpu_abort(env, "MPC8xx MMU model is not implemented\n");
2012
        break;
2013
    case POWERPC_MMU_BOOKE:
2014
        /* XXX: TODO */
2015
        cpu_abort(env, "BookE MMU model is not implemented\n");
2016
        break;
2017
    case POWERPC_MMU_BOOKE_FSL:
2018
        /* XXX: TODO */
2019
        cpu_abort(env, "BookE FSL MMU model is not implemented\n");
2020
        break;
2021
    case POWERPC_MMU_32B:
2022
    case POWERPC_MMU_601:
2023
        /* tlbie invalidate TLBs for all segments */
2024
        addr &= ~((target_ulong)-1ULL << 28);
2025
        /* XXX: this case should be optimized,
2026
         * giving a mask to tlb_flush_page
2027
         */
2028
        tlb_flush_page(env, addr | (0x0 << 28));
2029
        tlb_flush_page(env, addr | (0x1 << 28));
2030
        tlb_flush_page(env, addr | (0x2 << 28));
2031
        tlb_flush_page(env, addr | (0x3 << 28));
2032
        tlb_flush_page(env, addr | (0x4 << 28));
2033
        tlb_flush_page(env, addr | (0x5 << 28));
2034
        tlb_flush_page(env, addr | (0x6 << 28));
2035
        tlb_flush_page(env, addr | (0x7 << 28));
2036
        tlb_flush_page(env, addr | (0x8 << 28));
2037
        tlb_flush_page(env, addr | (0x9 << 28));
2038
        tlb_flush_page(env, addr | (0xA << 28));
2039
        tlb_flush_page(env, addr | (0xB << 28));
2040
        tlb_flush_page(env, addr | (0xC << 28));
2041
        tlb_flush_page(env, addr | (0xD << 28));
2042
        tlb_flush_page(env, addr | (0xE << 28));
2043
        tlb_flush_page(env, addr | (0xF << 28));
2044
        break;
2045
#if defined(TARGET_PPC64)
2046
    case POWERPC_MMU_620:
2047
    case POWERPC_MMU_64B:
2048
        /* tlbie invalidate TLBs for all segments */
2049
        /* XXX: given the fact that there are too many segments to invalidate,
2050
         *      and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
2051
         *      we just invalidate all TLBs
2052
         */
2053
        tlb_flush(env, 1);
2054
        break;
2055
#endif /* defined(TARGET_PPC64) */
2056
    default:
2057
        /* XXX: TODO */
2058
        cpu_abort(env, "Unknown MMU model\n");
2059
        break;
2060
    }
2061
#else
2062
    ppc_tlb_invalidate_all(env);
2063
#endif
2064
}
2065

    
2066
/*****************************************************************************/
2067
/* Special registers manipulation */
2068
#if defined(TARGET_PPC64)
2069
target_ulong ppc_load_asr (CPUPPCState *env)
2070
{
2071
    return env->asr;
2072
}
2073

    
2074
void ppc_store_asr (CPUPPCState *env, target_ulong value)
2075
{
2076
    if (env->asr != value) {
2077
        env->asr = value;
2078
        tlb_flush(env, 1);
2079
    }
2080
}
2081
#endif
2082

    
2083
target_ulong do_load_sdr1 (CPUPPCState *env)
2084
{
2085
    return env->sdr1;
2086
}
2087

    
2088
void do_store_sdr1 (CPUPPCState *env, target_ulong value)
2089
{
2090
#if defined (DEBUG_MMU)
2091
    if (loglevel != 0) {
2092
        fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
2093
    }
2094
#endif
2095
    if (env->sdr1 != value) {
2096
        /* XXX: for PowerPC 64, should check that the HTABSIZE value
2097
         *      is <= 28
2098
         */
2099
        env->sdr1 = value;
2100
        tlb_flush(env, 1);
2101
    }
2102
}
2103

    
2104
#if 0 // Unused
2105
target_ulong do_load_sr (CPUPPCState *env, int srnum)
2106
{
2107
    return env->sr[srnum];
2108
}
2109
#endif
2110

    
2111
void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
2112
{
2113
#if defined (DEBUG_MMU)
2114
    if (loglevel != 0) {
2115
        fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
2116
                __func__, srnum, value, env->sr[srnum]);
2117
    }
2118
#endif
2119
    if (env->sr[srnum] != value) {
2120
        env->sr[srnum] = value;
2121
#if !defined(FLUSH_ALL_TLBS) && 0
2122
        {
2123
            target_ulong page, end;
2124
            /* Invalidate 256 MB of virtual memory */
2125
            page = (16 << 20) * srnum;
2126
            end = page + (16 << 20);
2127
            for (; page != end; page += TARGET_PAGE_SIZE)
2128
                tlb_flush_page(env, page);
2129
        }
2130
#else
2131
        tlb_flush(env, 1);
2132
#endif
2133
    }
2134
}
2135
#endif /* !defined (CONFIG_USER_ONLY) */
2136

    
2137
target_ulong ppc_load_xer (CPUPPCState *env)
2138
{
2139
    return hreg_load_xer(env);
2140
}
2141

    
2142
void ppc_store_xer (CPUPPCState *env, target_ulong value)
2143
{
2144
    hreg_store_xer(env, value);
2145
}
2146

    
2147
/* GDBstub can read and write MSR... */
2148
void ppc_store_msr (CPUPPCState *env, target_ulong value)
2149
{
2150
    hreg_store_msr(env, value, 0);
2151
}
2152

    
2153
/*****************************************************************************/
2154
/* Exception processing */
2155
#if defined (CONFIG_USER_ONLY)
2156
void do_interrupt (CPUState *env)
2157
{
2158
    env->exception_index = POWERPC_EXCP_NONE;
2159
    env->error_code = 0;
2160
}
2161

    
2162
void ppc_hw_interrupt (CPUState *env)
2163
{
2164
    env->exception_index = POWERPC_EXCP_NONE;
2165
    env->error_code = 0;
2166
}
2167
#else /* defined (CONFIG_USER_ONLY) */
2168
static always_inline void dump_syscall (CPUState *env)
2169
{
2170
    fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
2171
            " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
2172
            env->gpr[0], env->gpr[3], env->gpr[4],
2173
            env->gpr[5], env->gpr[6], env->nip);
2174
}
2175

    
2176
/* Note that this function should be greatly optimized
2177
 * when called with a constant excp, from ppc_hw_interrupt
2178
 */
2179
static always_inline void powerpc_excp (CPUState *env,
2180
                                        int excp_model, int excp)
2181
{
2182
    target_ulong msr, new_msr, vector;
2183
    int srr0, srr1, asrr0, asrr1;
2184
    int lpes0, lpes1, lev;
2185

    
2186
    if (0) {
2187
        /* XXX: find a suitable condition to enable the hypervisor mode */
2188
        lpes0 = (env->spr[SPR_LPCR] >> 1) & 1;
2189
        lpes1 = (env->spr[SPR_LPCR] >> 2) & 1;
2190
    } else {
2191
        /* Those values ensure we won't enter the hypervisor mode */
2192
        lpes0 = 0;
2193
        lpes1 = 1;
2194
    }
2195

    
2196
    if (loglevel & CPU_LOG_INT) {
2197
        fprintf(logfile, "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
2198
                env->nip, excp, env->error_code);
2199
    }
2200
    msr = env->msr;
2201
    new_msr = msr;
2202
    srr0 = SPR_SRR0;
2203
    srr1 = SPR_SRR1;
2204
    asrr0 = -1;
2205
    asrr1 = -1;
2206
    msr &= ~((target_ulong)0x783F0000);
2207
    switch (excp) {
2208
    case POWERPC_EXCP_NONE:
2209
        /* Should never happen */
2210
        return;
2211
    case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
2212
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2213
        switch (excp_model) {
2214
        case POWERPC_EXCP_40x:
2215
            srr0 = SPR_40x_SRR2;
2216
            srr1 = SPR_40x_SRR3;
2217
            break;
2218
        case POWERPC_EXCP_BOOKE:
2219
            srr0 = SPR_BOOKE_CSRR0;
2220
            srr1 = SPR_BOOKE_CSRR1;
2221
            break;
2222
        case POWERPC_EXCP_G2:
2223
            break;
2224
        default:
2225
            goto excp_invalid;
2226
        }
2227
        goto store_next;
2228
    case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
2229
        if (msr_me == 0) {
2230
            /* Machine check exception is not enabled.
2231
             * Enter checkstop state.
2232
             */
2233
            if (loglevel != 0) {
2234
                fprintf(logfile, "Machine check while not allowed. "
2235
                        "Entering checkstop state\n");
2236
            } else {
2237
                fprintf(stderr, "Machine check while not allowed. "
2238
                        "Entering checkstop state\n");
2239
            }
2240
            env->halted = 1;
2241
            env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2242
        }
2243
        new_msr &= ~((target_ulong)1 << MSR_RI);
2244
        new_msr &= ~((target_ulong)1 << MSR_ME);
2245
        if (0) {
2246
            /* XXX: find a suitable condition to enable the hypervisor mode */
2247
            new_msr |= (target_ulong)MSR_HVB;
2248
        }
2249
        /* XXX: should also have something loaded in DAR / DSISR */
2250
        switch (excp_model) {
2251
        case POWERPC_EXCP_40x:
2252
            srr0 = SPR_40x_SRR2;
2253
            srr1 = SPR_40x_SRR3;
2254
            break;
2255
        case POWERPC_EXCP_BOOKE:
2256
            srr0 = SPR_BOOKE_MCSRR0;
2257
            srr1 = SPR_BOOKE_MCSRR1;
2258
            asrr0 = SPR_BOOKE_CSRR0;
2259
            asrr1 = SPR_BOOKE_CSRR1;
2260
            break;
2261
        default:
2262
            break;
2263
        }
2264
        goto store_next;
2265
    case POWERPC_EXCP_DSI:       /* Data storage exception                   */
2266
#if defined (DEBUG_EXCEPTIONS)
2267
        if (loglevel != 0) {
2268
            fprintf(logfile, "DSI exception: DSISR=0x" ADDRX" DAR=0x" ADDRX
2269
                    "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
2270
        }
2271
#endif
2272
        new_msr &= ~((target_ulong)1 << MSR_RI);
2273
        if (lpes1 == 0)
2274
            new_msr |= (target_ulong)MSR_HVB;
2275
        goto store_next;
2276
    case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
2277
#if defined (DEBUG_EXCEPTIONS)
2278
        if (loglevel != 0) {
2279
            fprintf(logfile, "ISI exception: msr=0x" ADDRX ", nip=0x" ADDRX
2280
                    "\n", msr, env->nip);
2281
        }
2282
#endif
2283
        new_msr &= ~((target_ulong)1 << MSR_RI);
2284
        if (lpes1 == 0)
2285
            new_msr |= (target_ulong)MSR_HVB;
2286
        msr |= env->error_code;
2287
        goto store_next;
2288
    case POWERPC_EXCP_EXTERNAL:  /* External input                           */
2289
        new_msr &= ~((target_ulong)1 << MSR_RI);
2290
        if (lpes0 == 1)
2291
            new_msr |= (target_ulong)MSR_HVB;
2292
        goto store_next;
2293
    case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
2294
        new_msr &= ~((target_ulong)1 << MSR_RI);
2295
        if (lpes1 == 0)
2296
            new_msr |= (target_ulong)MSR_HVB;
2297
        /* XXX: this is false */
2298
        /* Get rS/rD and rA from faulting opcode */
2299
        env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
2300
        goto store_current;
2301
    case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
2302
        switch (env->error_code & ~0xF) {
2303
        case POWERPC_EXCP_FP:
2304
            if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
2305
#if defined (DEBUG_EXCEPTIONS)
2306
                if (loglevel != 0) {
2307
                    fprintf(logfile, "Ignore floating point exception\n");
2308
                }
2309
#endif
2310
                env->exception_index = POWERPC_EXCP_NONE;
2311
                env->error_code = 0;
2312
                return;
2313
            }
2314
            new_msr &= ~((target_ulong)1 << MSR_RI);
2315
            if (lpes1 == 0)
2316
                new_msr |= (target_ulong)MSR_HVB;
2317
            msr |= 0x00100000;
2318
            if (msr_fe0 == msr_fe1)
2319
                goto store_next;
2320
            msr |= 0x00010000;
2321
            break;
2322
        case POWERPC_EXCP_INVAL:
2323
#if defined (DEBUG_EXCEPTIONS)
2324
            if (loglevel != 0) {
2325
                fprintf(logfile, "Invalid instruction at 0x" ADDRX "\n",
2326
                        env->nip);
2327
            }
2328
#endif
2329
            new_msr &= ~((target_ulong)1 << MSR_RI);
2330
            if (lpes1 == 0)
2331
                new_msr |= (target_ulong)MSR_HVB;
2332
            msr |= 0x00080000;
2333
            break;
2334
        case POWERPC_EXCP_PRIV:
2335
            new_msr &= ~((target_ulong)1 << MSR_RI);
2336
            if (lpes1 == 0)
2337
                new_msr |= (target_ulong)MSR_HVB;
2338
            msr |= 0x00040000;
2339
            break;
2340
        case POWERPC_EXCP_TRAP:
2341
            new_msr &= ~((target_ulong)1 << MSR_RI);
2342
            if (lpes1 == 0)
2343
                new_msr |= (target_ulong)MSR_HVB;
2344
            msr |= 0x00020000;
2345
            break;
2346
        default:
2347
            /* Should never occur */
2348
            cpu_abort(env, "Invalid program exception %d. Aborting\n",
2349
                      env->error_code);
2350
            break;
2351
        }
2352
        goto store_current;
2353
    case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
2354
        new_msr &= ~((target_ulong)1 << MSR_RI);
2355
        if (lpes1 == 0)
2356
            new_msr |= (target_ulong)MSR_HVB;
2357
        goto store_current;
2358
    case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
2359
        /* NOTE: this is a temporary hack to support graphics OSI
2360
           calls from the MOL driver */
2361
        /* XXX: To be removed */
2362
        if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2363
            env->osi_call) {
2364
            if (env->osi_call(env) != 0) {
2365
                env->exception_index = POWERPC_EXCP_NONE;
2366
                env->error_code = 0;
2367
                return;
2368
            }
2369
        }
2370
        if (loglevel & CPU_LOG_INT) {
2371
            dump_syscall(env);
2372
        }
2373
        new_msr &= ~((target_ulong)1 << MSR_RI);
2374
        lev = env->error_code;
2375
        if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
2376
            new_msr |= (target_ulong)MSR_HVB;
2377
        goto store_next;
2378
    case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
2379
        new_msr &= ~((target_ulong)1 << MSR_RI);
2380
        goto store_current;
2381
    case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
2382
        new_msr &= ~((target_ulong)1 << MSR_RI);
2383
        if (lpes1 == 0)
2384
            new_msr |= (target_ulong)MSR_HVB;
2385
        goto store_next;
2386
    case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
2387
        /* FIT on 4xx */
2388
#if defined (DEBUG_EXCEPTIONS)
2389
        if (loglevel != 0)
2390
            fprintf(logfile, "FIT exception\n");
2391
#endif
2392
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2393
        goto store_next;
2394
    case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
2395
#if defined (DEBUG_EXCEPTIONS)
2396
        if (loglevel != 0)
2397
            fprintf(logfile, "WDT exception\n");
2398
#endif
2399
        switch (excp_model) {
2400
        case POWERPC_EXCP_BOOKE:
2401
            srr0 = SPR_BOOKE_CSRR0;
2402
            srr1 = SPR_BOOKE_CSRR1;
2403
            break;
2404
        default:
2405
            break;
2406
        }
2407
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2408
        goto store_next;
2409
    case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
2410
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2411
        goto store_next;
2412
    case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
2413
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2414
        goto store_next;
2415
    case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
2416
        switch (excp_model) {
2417
        case POWERPC_EXCP_BOOKE:
2418
            srr0 = SPR_BOOKE_DSRR0;
2419
            srr1 = SPR_BOOKE_DSRR1;
2420
            asrr0 = SPR_BOOKE_CSRR0;
2421
            asrr1 = SPR_BOOKE_CSRR1;
2422
            break;
2423
        default:
2424
            break;
2425
        }
2426
        /* XXX: TODO */
2427
        cpu_abort(env, "Debug exception is not implemented yet !\n");
2428
        goto store_next;
2429
    case POWERPC_EXCP_SPEU:      /* SPE/embedded floating-point unavailable  */
2430
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2431
        goto store_current;
2432
    case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
2433
        /* XXX: TODO */
2434
        cpu_abort(env, "Embedded floating point data exception "
2435
                  "is not implemented yet !\n");
2436
        goto store_next;
2437
    case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
2438
        /* XXX: TODO */
2439
        cpu_abort(env, "Embedded floating point round exception "
2440
                  "is not implemented yet !\n");
2441
        goto store_next;
2442
    case POWERPC_EXCP_EPERFM:    /* Embedded performance monitor interrupt   */
2443
        new_msr &= ~((target_ulong)1 << MSR_RI);
2444
        /* XXX: TODO */
2445
        cpu_abort(env,
2446
                  "Performance counter exception is not implemented yet !\n");
2447
        goto store_next;
2448
    case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
2449
        /* XXX: TODO */
2450
        cpu_abort(env,
2451
                  "Embedded doorbell interrupt is not implemented yet !\n");
2452
        goto store_next;
2453
    case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
2454
        switch (excp_model) {
2455
        case POWERPC_EXCP_BOOKE:
2456
            srr0 = SPR_BOOKE_CSRR0;
2457
            srr1 = SPR_BOOKE_CSRR1;
2458
            break;
2459
        default:
2460
            break;
2461
        }
2462
        /* XXX: TODO */
2463
        cpu_abort(env, "Embedded doorbell critical interrupt "
2464
                  "is not implemented yet !\n");
2465
        goto store_next;
2466
    case POWERPC_EXCP_RESET:     /* System reset exception                   */
2467
        new_msr &= ~((target_ulong)1 << MSR_RI);
2468
        if (0) {
2469
            /* XXX: find a suitable condition to enable the hypervisor mode */
2470
            new_msr |= (target_ulong)MSR_HVB;
2471
        }
2472
        goto store_next;
2473
    case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
2474
        new_msr &= ~((target_ulong)1 << MSR_RI);
2475
        if (lpes1 == 0)
2476
            new_msr |= (target_ulong)MSR_HVB;
2477
        goto store_next;
2478
    case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
2479
        new_msr &= ~((target_ulong)1 << MSR_RI);
2480
        if (lpes1 == 0)
2481
            new_msr |= (target_ulong)MSR_HVB;
2482
        goto store_next;
2483
    case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
2484
        srr0 = SPR_HSRR0;
2485
        srr1 = SPR_HSRR1;
2486
        new_msr |= (target_ulong)MSR_HVB;
2487
        goto store_next;
2488
    case POWERPC_EXCP_TRACE:     /* Trace exception                          */
2489
        new_msr &= ~((target_ulong)1 << MSR_RI);
2490
        if (lpes1 == 0)
2491
            new_msr |= (target_ulong)MSR_HVB;
2492
        goto store_next;
2493
    case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
2494
        srr0 = SPR_HSRR0;
2495
        srr1 = SPR_HSRR1;
2496
        new_msr |= (target_ulong)MSR_HVB;
2497
        goto store_next;
2498
    case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
2499
        srr0 = SPR_HSRR0;
2500
        srr1 = SPR_HSRR1;
2501
        new_msr |= (target_ulong)MSR_HVB;
2502
        goto store_next;
2503
    case POWERPC_EXCP_HDSEG:     /* Hypervisor data segment exception        */
2504
        srr0 = SPR_HSRR0;
2505
        srr1 = SPR_HSRR1;
2506
        new_msr |= (target_ulong)MSR_HVB;
2507
        goto store_next;
2508
    case POWERPC_EXCP_HISEG:     /* Hypervisor instruction segment exception */
2509
        srr0 = SPR_HSRR0;
2510
        srr1 = SPR_HSRR1;
2511
        new_msr |= (target_ulong)MSR_HVB;
2512
        goto store_next;
2513
    case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
2514
        new_msr &= ~((target_ulong)1 << MSR_RI);
2515
        if (lpes1 == 0)
2516
            new_msr |= (target_ulong)MSR_HVB;
2517
        goto store_current;
2518
    case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
2519
#if defined (DEBUG_EXCEPTIONS)
2520
        if (loglevel != 0)
2521
            fprintf(logfile, "PIT exception\n");
2522
#endif
2523
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2524
        goto store_next;
2525
    case POWERPC_EXCP_IO:        /* IO error exception                       */
2526
        /* XXX: TODO */
2527
        cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2528
        goto store_next;
2529
    case POWERPC_EXCP_RUNM:      /* Run mode exception                       */
2530
        /* XXX: TODO */
2531
        cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2532
        goto store_next;
2533
    case POWERPC_EXCP_EMUL:      /* Emulation trap exception                 */
2534
        /* XXX: TODO */
2535
        cpu_abort(env, "602 emulation trap exception "
2536
                  "is not implemented yet !\n");
2537
        goto store_next;
2538
    case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
2539
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2540
        if (lpes1 == 0) /* XXX: check this */
2541
            new_msr |= (target_ulong)MSR_HVB;
2542
        switch (excp_model) {
2543
        case POWERPC_EXCP_602:
2544
        case POWERPC_EXCP_603:
2545
        case POWERPC_EXCP_603E:
2546
        case POWERPC_EXCP_G2:
2547
            goto tlb_miss_tgpr;
2548
        case POWERPC_EXCP_7x5:
2549
            goto tlb_miss;
2550
        case POWERPC_EXCP_74xx:
2551
            goto tlb_miss_74xx;
2552
        default:
2553
            cpu_abort(env, "Invalid instruction TLB miss exception\n");
2554
            break;
2555
        }
2556
        break;
2557
    case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
2558
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2559
        if (lpes1 == 0) /* XXX: check this */
2560
            new_msr |= (target_ulong)MSR_HVB;
2561
        switch (excp_model) {
2562
        case POWERPC_EXCP_602:
2563
        case POWERPC_EXCP_603:
2564
        case POWERPC_EXCP_603E:
2565
        case POWERPC_EXCP_G2:
2566
            goto tlb_miss_tgpr;
2567
        case POWERPC_EXCP_7x5:
2568
            goto tlb_miss;
2569
        case POWERPC_EXCP_74xx:
2570
            goto tlb_miss_74xx;
2571
        default:
2572
            cpu_abort(env, "Invalid data load TLB miss exception\n");
2573
            break;
2574
        }
2575
        break;
2576
    case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
2577
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2578
        if (lpes1 == 0) /* XXX: check this */
2579
            new_msr |= (target_ulong)MSR_HVB;
2580
        switch (excp_model) {
2581
        case POWERPC_EXCP_602:
2582
        case POWERPC_EXCP_603:
2583
        case POWERPC_EXCP_603E:
2584
        case POWERPC_EXCP_G2:
2585
        tlb_miss_tgpr:
2586
            /* Swap temporary saved registers with GPRs */
2587
            if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
2588
                new_msr |= (target_ulong)1 << MSR_TGPR;
2589
                hreg_swap_gpr_tgpr(env);
2590
            }
2591
            goto tlb_miss;
2592
        case POWERPC_EXCP_7x5:
2593
        tlb_miss:
2594
#if defined (DEBUG_SOFTWARE_TLB)
2595
            if (loglevel != 0) {
2596
                const unsigned char *es;
2597
                target_ulong *miss, *cmp;
2598
                int en;
2599
                if (excp == POWERPC_EXCP_IFTLB) {
2600
                    es = "I";
2601
                    en = 'I';
2602
                    miss = &env->spr[SPR_IMISS];
2603
                    cmp = &env->spr[SPR_ICMP];
2604
                } else {
2605
                    if (excp == POWERPC_EXCP_DLTLB)
2606
                        es = "DL";
2607
                    else
2608
                        es = "DS";
2609
                    en = 'D';
2610
                    miss = &env->spr[SPR_DMISS];
2611
                    cmp = &env->spr[SPR_DCMP];
2612
                }
2613
                fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2614
                        " H1 " ADDRX " H2 " ADDRX " %08x\n",
2615
                        es, en, *miss, en, *cmp,
2616
                        env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2617
                        env->error_code);
2618
            }
2619
#endif
2620
            msr |= env->crf[0] << 28;
2621
            msr |= env->error_code; /* key, D/I, S/L bits */
2622
            /* Set way using a LRU mechanism */
2623
            msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2624
            break;
2625
        case POWERPC_EXCP_74xx:
2626
        tlb_miss_74xx:
2627
#if defined (DEBUG_SOFTWARE_TLB)
2628
            if (loglevel != 0) {
2629
                const unsigned char *es;
2630
                target_ulong *miss, *cmp;
2631
                int en;
2632
                if (excp == POWERPC_EXCP_IFTLB) {
2633
                    es = "I";
2634
                    en = 'I';
2635
                    miss = &env->spr[SPR_TLBMISS];
2636
                    cmp = &env->spr[SPR_PTEHI];
2637
                } else {
2638
                    if (excp == POWERPC_EXCP_DLTLB)
2639
                        es = "DL";
2640
                    else
2641
                        es = "DS";
2642
                    en = 'D';
2643
                    miss = &env->spr[SPR_TLBMISS];
2644
                    cmp = &env->spr[SPR_PTEHI];
2645
                }
2646
                fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2647
                        " %08x\n",
2648
                        es, en, *miss, en, *cmp, env->error_code);
2649
            }
2650
#endif
2651
            msr |= env->error_code; /* key bit */
2652
            break;
2653
        default:
2654
            cpu_abort(env, "Invalid data store TLB miss exception\n");
2655
            break;
2656
        }
2657
        goto store_next;
2658
    case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
2659
        /* XXX: TODO */
2660
        cpu_abort(env, "Floating point assist exception "
2661
                  "is not implemented yet !\n");
2662
        goto store_next;
2663
    case POWERPC_EXCP_DABR:      /* Data address breakpoint                  */
2664
        /* XXX: TODO */
2665
        cpu_abort(env, "DABR exception is not implemented yet !\n");
2666
        goto store_next;
2667
    case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
2668
        /* XXX: TODO */
2669
        cpu_abort(env, "IABR exception is not implemented yet !\n");
2670
        goto store_next;
2671
    case POWERPC_EXCP_SMI:       /* System management interrupt              */
2672
        /* XXX: TODO */
2673
        cpu_abort(env, "SMI exception is not implemented yet !\n");
2674
        goto store_next;
2675
    case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
2676
        /* XXX: TODO */
2677
        cpu_abort(env, "Thermal management exception "
2678
                  "is not implemented yet !\n");
2679
        goto store_next;
2680
    case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
2681
        new_msr &= ~((target_ulong)1 << MSR_RI);
2682
        if (lpes1 == 0)
2683
            new_msr |= (target_ulong)MSR_HVB;
2684
        /* XXX: TODO */
2685
        cpu_abort(env,
2686
                  "Performance counter exception is not implemented yet !\n");
2687
        goto store_next;
2688
    case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
2689
        /* XXX: TODO */
2690
        cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2691
        goto store_next;
2692
    case POWERPC_EXCP_SOFTP:     /* Soft patch exception                     */
2693
        /* XXX: TODO */
2694
        cpu_abort(env,
2695
                  "970 soft-patch exception is not implemented yet !\n");
2696
        goto store_next;
2697
    case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
2698
        /* XXX: TODO */
2699
        cpu_abort(env,
2700
                  "970 maintenance exception is not implemented yet !\n");
2701
        goto store_next;
2702
    case POWERPC_EXCP_MEXTBR:    /* Maskable external breakpoint             */
2703
        /* XXX: TODO */
2704
        cpu_abort(env, "Maskable external exception "
2705
                  "is not implemented yet !\n");
2706
        goto store_next;
2707
    case POWERPC_EXCP_NMEXTBR:   /* Non maskable external breakpoint         */
2708
        /* XXX: TODO */
2709
        cpu_abort(env, "Non maskable external exception "
2710
                  "is not implemented yet !\n");
2711
        goto store_next;
2712
    default:
2713
    excp_invalid:
2714
        cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2715
        break;
2716
    store_current:
2717
        /* save current instruction location */
2718
        env->spr[srr0] = env->nip - 4;
2719
        break;
2720
    store_next:
2721
        /* save next instruction location */
2722
        env->spr[srr0] = env->nip;
2723
        break;
2724
    }
2725
    /* Save MSR */
2726
    env->spr[srr1] = msr;
2727
    /* If any alternate SRR register are defined, duplicate saved values */
2728
    if (asrr0 != -1)
2729
        env->spr[asrr0] = env->spr[srr0];
2730
    if (asrr1 != -1)
2731
        env->spr[asrr1] = env->spr[srr1];
2732
    /* If we disactivated any translation, flush TLBs */
2733
    if (new_msr & ((1 << MSR_IR) | (1 << MSR_DR)))
2734
        tlb_flush(env, 1);
2735
    /* reload MSR with correct bits */
2736
    new_msr &= ~((target_ulong)1 << MSR_EE);
2737
    new_msr &= ~((target_ulong)1 << MSR_PR);
2738
    new_msr &= ~((target_ulong)1 << MSR_FP);
2739
    new_msr &= ~((target_ulong)1 << MSR_FE0);
2740
    new_msr &= ~((target_ulong)1 << MSR_SE);
2741
    new_msr &= ~((target_ulong)1 << MSR_BE);
2742
    new_msr &= ~((target_ulong)1 << MSR_FE1);
2743
    new_msr &= ~((target_ulong)1 << MSR_IR);
2744
    new_msr &= ~((target_ulong)1 << MSR_DR);
2745
#if 0 /* Fix this: not on all targets */
2746
    new_msr &= ~((target_ulong)1 << MSR_PMM);
2747
#endif
2748
    new_msr &= ~((target_ulong)1 << MSR_LE);
2749
    if (msr_ile)
2750
        new_msr |= (target_ulong)1 << MSR_LE;
2751
    else
2752
        new_msr &= ~((target_ulong)1 << MSR_LE);
2753
    /* Jump to handler */
2754
    vector = env->excp_vectors[excp];
2755
    if (vector == (target_ulong)-1ULL) {
2756
        cpu_abort(env, "Raised an exception without defined vector %d\n",
2757
                  excp);
2758
    }
2759
    vector |= env->excp_prefix;
2760
#if defined(TARGET_PPC64)
2761
    if (excp_model == POWERPC_EXCP_BOOKE) {
2762
        if (!msr_icm) {
2763
            new_msr &= ~((target_ulong)1 << MSR_CM);
2764
            vector = (uint32_t)vector;
2765
        } else {
2766
            new_msr |= (target_ulong)1 << MSR_CM;
2767
        }
2768
    } else {
2769
        if (!msr_isf) {
2770
            new_msr &= ~((target_ulong)1 << MSR_SF);
2771
            vector = (uint32_t)vector;
2772
        } else {
2773
            new_msr |= (target_ulong)1 << MSR_SF;
2774
        }
2775
    }
2776
#endif
2777
    /* XXX: we don't use hreg_store_msr here as already have treated
2778
     *      any special case that could occur. Just store MSR and update hflags
2779
     */
2780
    env->msr = new_msr & env->msr_mask;
2781
    hreg_compute_hflags(env);
2782
    env->nip = vector;
2783
    /* Reset exception state */
2784
    env->exception_index = POWERPC_EXCP_NONE;
2785
    env->error_code = 0;
2786
}
2787

    
2788
void do_interrupt (CPUState *env)
2789
{
2790
    powerpc_excp(env, env->excp_model, env->exception_index);
2791
}
2792

    
2793
void ppc_hw_interrupt (CPUPPCState *env)
2794
{
2795
    int hdice;
2796

    
2797
#if 0
2798
    if (loglevel & CPU_LOG_INT) {
2799
        fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2800
                __func__, env, env->pending_interrupts,
2801
                env->interrupt_request, (int)msr_me, (int)msr_ee);
2802
    }
2803
#endif
2804
    /* External reset */
2805
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2806
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2807
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2808
        return;
2809
    }
2810
    /* Machine check exception */
2811
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2812
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2813
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2814
        return;
2815
    }
2816
#if 0 /* TODO */
2817
    /* External debug exception */
2818
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2819
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2820
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2821
        return;
2822
    }
2823
#endif
2824
    if (0) {
2825
        /* XXX: find a suitable condition to enable the hypervisor mode */
2826
        hdice = env->spr[SPR_LPCR] & 1;
2827
    } else {
2828
        hdice = 0;
2829
    }
2830
    if ((msr_ee != 0 || msr_hv == 0 || msr_pr != 0) && hdice != 0) {
2831
        /* Hypervisor decrementer exception */
2832
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2833
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2834
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2835
            return;
2836
        }
2837
    }
2838
    if (msr_ce != 0) {
2839
        /* External critical interrupt */
2840
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2841
            /* Taking a critical external interrupt does not clear the external
2842
             * critical interrupt status
2843
             */
2844
#if 0
2845
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2846
#endif
2847
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2848
            return;
2849
        }
2850
    }
2851
    if (msr_ee != 0) {
2852
        /* Watchdog timer on embedded PowerPC */
2853
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2854
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2855
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2856
            return;
2857
        }
2858
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2859
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2860
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2861
            return;
2862
        }
2863
        /* Fixed interval timer on embedded PowerPC */
2864
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2865
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2866
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2867
            return;
2868
        }
2869
        /* Programmable interval timer on embedded PowerPC */
2870
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2871
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2872
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2873
            return;
2874
        }
2875
        /* Decrementer exception */
2876
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2877
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2878
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2879
            return;
2880
        }
2881
        /* External interrupt */
2882
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2883
            /* Taking an external interrupt does not clear the external
2884
             * interrupt status
2885
             */
2886
#if 0
2887
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2888
#endif
2889
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2890
            return;
2891
        }
2892
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2893
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2894
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2895
            return;
2896
        }
2897
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2898
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2899
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2900
            return;
2901
        }
2902
        /* Thermal interrupt */
2903
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2904
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2905
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2906
            return;
2907
        }
2908
    }
2909
}
2910
#endif /* !CONFIG_USER_ONLY */
2911

    
2912
void cpu_dump_EA (target_ulong EA)
2913
{
2914
    FILE *f;
2915

    
2916
    if (logfile) {
2917
        f = logfile;
2918
    } else {
2919
        f = stdout;
2920
        return;
2921
    }
2922
    fprintf(f, "Memory access at address " ADDRX "\n", EA);
2923
}
2924

    
2925
void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2926
{
2927
    FILE *f;
2928

    
2929
    if (logfile) {
2930
        f = logfile;
2931
    } else {
2932
        f = stdout;
2933
        return;
2934
    }
2935
    fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2936
            RA, msr);
2937
}
2938

    
2939
void cpu_ppc_reset (void *opaque)
2940
{
2941
    CPUPPCState *env;
2942
    target_ulong msr;
2943

    
2944
    env = opaque;
2945
    msr = (target_ulong)0;
2946
    if (0) {
2947
        /* XXX: find a suitable condition to enable the hypervisor mode */
2948
        msr |= (target_ulong)MSR_HVB;
2949
    }
2950
    msr |= (target_ulong)0 << MSR_AP; /* TO BE CHECKED */
2951
    msr |= (target_ulong)0 << MSR_SA; /* TO BE CHECKED */
2952
    msr |= (target_ulong)1 << MSR_EP;
2953
#if defined (DO_SINGLE_STEP) && 0
2954
    /* Single step trace mode */
2955
    msr |= (target_ulong)1 << MSR_SE;
2956
    msr |= (target_ulong)1 << MSR_BE;
2957
#endif
2958
#if defined(CONFIG_USER_ONLY)
2959
    msr |= (target_ulong)1 << MSR_FP; /* Allow floating point usage */
2960
    msr |= (target_ulong)1 << MSR_PR;
2961
#else
2962
    env->nip = env->hreset_vector | env->excp_prefix;
2963
    if (env->mmu_model != POWERPC_MMU_REAL)
2964
        ppc_tlb_invalidate_all(env);
2965
#endif
2966
    env->msr = msr;
2967
    hreg_compute_hflags(env);
2968
    env->reserve = (target_ulong)-1ULL;
2969
    /* Be sure no exception or interrupt is pending */
2970
    env->pending_interrupts = 0;
2971
    env->exception_index = POWERPC_EXCP_NONE;
2972
    env->error_code = 0;
2973
    /* Flush all TLBs */
2974
    tlb_flush(env, 1);
2975
}
2976

    
2977
CPUPPCState *cpu_ppc_init (const char *cpu_model)
2978
{
2979
    CPUPPCState *env;
2980
    const ppc_def_t *def;
2981

    
2982
    def = cpu_ppc_find_by_name(cpu_model);
2983
    if (!def)
2984
        return NULL;
2985

    
2986
    env = qemu_mallocz(sizeof(CPUPPCState));
2987
    if (!env)
2988
        return NULL;
2989
    cpu_exec_init(env);
2990
    cpu_ppc_register_internal(env, def);
2991
    cpu_ppc_reset(env);
2992
    return env;
2993
}
2994

    
2995
void cpu_ppc_close (CPUPPCState *env)
2996
{
2997
    /* Should also remove all opcode tables... */
2998
    qemu_free(env);
2999
}