Statistics
| Branch: | Revision:

root / target-ppc / helper.c @ b172c56a

History | View | Annotate | Download (94.9 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_64B)
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_64B) {
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_64B) {
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_64B) {
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_4xx:
1361
    case POWERPC_MMU_BOOKE:
1362
        ctx->prot |= PAGE_WRITE;
1363
        break;
1364
#if defined(TARGET_PPC64)
1365
    case POWERPC_MMU_64B:
1366
        /* Real address are 60 bits long */
1367
        ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1368
        ctx->prot |= PAGE_WRITE;
1369
        break;
1370
#endif
1371
    case POWERPC_MMU_SOFT_4xx_Z:
1372
        if (unlikely(msr_pe != 0)) {
1373
            /* 403 family add some particular protections,
1374
             * using PBL/PBU registers for accesses with no translation.
1375
             */
1376
            in_plb =
1377
                /* Check PLB validity */
1378
                (env->pb[0] < env->pb[1] &&
1379
                 /* and address in plb area */
1380
                 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1381
                (env->pb[2] < env->pb[3] &&
1382
                 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1383
            if (in_plb ^ msr_px) {
1384
                /* Access in protected area */
1385
                if (rw == 1) {
1386
                    /* Access is not allowed */
1387
                    ret = -2;
1388
                }
1389
            } else {
1390
                /* Read-write access is allowed */
1391
                ctx->prot |= PAGE_WRITE;
1392
            }
1393
        }
1394
        break;
1395
    case POWERPC_MMU_BOOKE_FSL:
1396
        /* XXX: TODO */
1397
        cpu_abort(env, "BookE FSL MMU model not implemented\n");
1398
        break;
1399
    default:
1400
        cpu_abort(env, "Unknown or invalid MMU model\n");
1401
        return -1;
1402
    }
1403

    
1404
    return ret;
1405
}
1406

    
1407
int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1408
                          int rw, int access_type)
1409
{
1410
    int ret;
1411

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

    
1467
    return ret;
1468
}
1469

    
1470
target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1471
{
1472
    mmu_ctx_t ctx;
1473

    
1474
    if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT) != 0))
1475
        return -1;
1476

    
1477
    return ctx.raddr & TARGET_PAGE_MASK;
1478
}
1479

    
1480
/* Perform address translation */
1481
int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1482
                              int mmu_idx, int is_softmmu)
1483
{
1484
    mmu_ctx_t ctx;
1485
    int access_type;
1486
    int ret = 0;
1487

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

    
1717
    return ret;
1718
}
1719

    
1720
/*****************************************************************************/
1721
/* BATs management */
1722
#if !defined(FLUSH_ALL_TLBS)
1723
static always_inline void do_invalidate_BAT (CPUPPCState *env,
1724
                                             target_ulong BATu,
1725
                                             target_ulong mask)
1726
{
1727
    target_ulong base, end, page;
1728

    
1729
    base = BATu & ~0x0001FFFF;
1730
    end = base + mask + 0x00020000;
1731
#if defined (DEBUG_BATS)
1732
    if (loglevel != 0) {
1733
        fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1734
                base, end, mask);
1735
    }
1736
#endif
1737
    for (page = base; page != end; page += TARGET_PAGE_SIZE)
1738
        tlb_flush_page(env, page);
1739
#if defined (DEBUG_BATS)
1740
    if (loglevel != 0)
1741
        fprintf(logfile, "Flush done\n");
1742
#endif
1743
}
1744
#endif
1745

    
1746
static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1747
                                          int ul, int nr, target_ulong value)
1748
{
1749
#if defined (DEBUG_BATS)
1750
    if (loglevel != 0) {
1751
        fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1752
                ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1753
    }
1754
#endif
1755
}
1756

    
1757
target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1758
{
1759
    return env->IBAT[0][nr];
1760
}
1761

    
1762
target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1763
{
1764
    return env->IBAT[1][nr];
1765
}
1766

    
1767
void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1768
{
1769
    target_ulong mask;
1770

    
1771
    dump_store_bat(env, 'I', 0, nr, value);
1772
    if (env->IBAT[0][nr] != value) {
1773
        mask = (value << 15) & 0x0FFE0000UL;
1774
#if !defined(FLUSH_ALL_TLBS)
1775
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1776
#endif
1777
        /* When storing valid upper BAT, mask BEPI and BRPN
1778
         * and invalidate all TLBs covered by this BAT
1779
         */
1780
        mask = (value << 15) & 0x0FFE0000UL;
1781
        env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1782
            (value & ~0x0001FFFFUL & ~mask);
1783
        env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1784
            (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1785
#if !defined(FLUSH_ALL_TLBS)
1786
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1787
#else
1788
        tlb_flush(env, 1);
1789
#endif
1790
    }
1791
}
1792

    
1793
void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1794
{
1795
    dump_store_bat(env, 'I', 1, nr, value);
1796
    env->IBAT[1][nr] = value;
1797
}
1798

    
1799
target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1800
{
1801
    return env->DBAT[0][nr];
1802
}
1803

    
1804
target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1805
{
1806
    return env->DBAT[1][nr];
1807
}
1808

    
1809
void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1810
{
1811
    target_ulong mask;
1812

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

    
1835
void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1836
{
1837
    dump_store_bat(env, 'D', 1, nr, value);
1838
    env->DBAT[1][nr] = value;
1839
}
1840

    
1841
void do_store_ibatu_601 (CPUPPCState *env, int nr, target_ulong value)
1842
{
1843
    target_ulong mask;
1844
    int do_inval;
1845

    
1846
    dump_store_bat(env, 'I', 0, nr, value);
1847
    if (env->IBAT[0][nr] != value) {
1848
        do_inval = 0;
1849
        mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1850
        if (env->IBAT[1][nr] & 0x40) {
1851
            /* Invalidate BAT only if it is valid */
1852
#if !defined(FLUSH_ALL_TLBS)
1853
            do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1854
#else
1855
            do_inval = 1;
1856
#endif
1857
        }
1858
        /* When storing valid upper BAT, mask BEPI and BRPN
1859
         * and invalidate all TLBs covered by this BAT
1860
         */
1861
        env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1862
            (value & ~0x0001FFFFUL & ~mask);
1863
        env->DBAT[0][nr] = env->IBAT[0][nr];
1864
        if (env->IBAT[1][nr] & 0x40) {
1865
#if !defined(FLUSH_ALL_TLBS)
1866
            do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1867
#else
1868
            do_inval = 1;
1869
#endif
1870
        }
1871
#if defined(FLUSH_ALL_TLBS)
1872
        if (do_inval)
1873
            tlb_flush(env, 1);
1874
#endif
1875
    }
1876
}
1877

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

    
1883
    dump_store_bat(env, 'I', 1, nr, value);
1884
    if (env->IBAT[1][nr] != value) {
1885
        do_inval = 0;
1886
        if (env->IBAT[1][nr] & 0x40) {
1887
#if !defined(FLUSH_ALL_TLBS)
1888
            mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1889
            do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1890
#else
1891
            do_inval = 1;
1892
#endif
1893
        }
1894
        if (value & 0x40) {
1895
#if !defined(FLUSH_ALL_TLBS)
1896
            mask = (value << 17) & 0x0FFE0000UL;
1897
            do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1898
#else
1899
            do_inval = 1;
1900
#endif
1901
        }
1902
        env->IBAT[1][nr] = value;
1903
        env->DBAT[1][nr] = value;
1904
#if defined(FLUSH_ALL_TLBS)
1905
        if (do_inval)
1906
            tlb_flush(env, 1);
1907
#endif
1908
    }
1909
}
1910

    
1911
/*****************************************************************************/
1912
/* TLB management */
1913
void ppc_tlb_invalidate_all (CPUPPCState *env)
1914
{
1915
    switch (env->mmu_model) {
1916
    case POWERPC_MMU_SOFT_6xx:
1917
    case POWERPC_MMU_SOFT_74xx:
1918
        ppc6xx_tlb_invalidate_all(env);
1919
        break;
1920
    case POWERPC_MMU_SOFT_4xx:
1921
    case POWERPC_MMU_SOFT_4xx_Z:
1922
        ppc4xx_tlb_invalidate_all(env);
1923
        break;
1924
    case POWERPC_MMU_REAL_4xx:
1925
        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1926
        break;
1927
    case POWERPC_MMU_BOOKE:
1928
        /* XXX: TODO */
1929
        cpu_abort(env, "MMU model not implemented\n");
1930
        break;
1931
    case POWERPC_MMU_BOOKE_FSL:
1932
        /* XXX: TODO */
1933
        cpu_abort(env, "MMU model not implemented\n");
1934
        break;
1935
    case POWERPC_MMU_32B:
1936
    case POWERPC_MMU_601:
1937
#if defined(TARGET_PPC64)
1938
    case POWERPC_MMU_64B:
1939
#endif /* defined(TARGET_PPC64) */
1940
        tlb_flush(env, 1);
1941
        break;
1942
    default:
1943
        /* XXX: TODO */
1944
        cpu_abort(env, "Unknown MMU model\n");
1945
        break;
1946
    }
1947
}
1948

    
1949
void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1950
{
1951
#if !defined(FLUSH_ALL_TLBS)
1952
    addr &= TARGET_PAGE_MASK;
1953
    switch (env->mmu_model) {
1954
    case POWERPC_MMU_SOFT_6xx:
1955
    case POWERPC_MMU_SOFT_74xx:
1956
        ppc6xx_tlb_invalidate_virt(env, addr, 0);
1957
        if (env->id_tlbs == 1)
1958
            ppc6xx_tlb_invalidate_virt(env, addr, 1);
1959
        break;
1960
    case POWERPC_MMU_SOFT_4xx:
1961
    case POWERPC_MMU_SOFT_4xx_Z:
1962
        ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
1963
        break;
1964
    case POWERPC_MMU_REAL_4xx:
1965
        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1966
        break;
1967
    case POWERPC_MMU_BOOKE:
1968
        /* XXX: TODO */
1969
        cpu_abort(env, "MMU model not implemented\n");
1970
        break;
1971
    case POWERPC_MMU_BOOKE_FSL:
1972
        /* XXX: TODO */
1973
        cpu_abort(env, "MMU model not implemented\n");
1974
        break;
1975
    case POWERPC_MMU_32B:
1976
    case POWERPC_MMU_601:
1977
        /* tlbie invalidate TLBs for all segments */
1978
        addr &= ~((target_ulong)-1ULL << 28);
1979
        /* XXX: this case should be optimized,
1980
         * giving a mask to tlb_flush_page
1981
         */
1982
        tlb_flush_page(env, addr | (0x0 << 28));
1983
        tlb_flush_page(env, addr | (0x1 << 28));
1984
        tlb_flush_page(env, addr | (0x2 << 28));
1985
        tlb_flush_page(env, addr | (0x3 << 28));
1986
        tlb_flush_page(env, addr | (0x4 << 28));
1987
        tlb_flush_page(env, addr | (0x5 << 28));
1988
        tlb_flush_page(env, addr | (0x6 << 28));
1989
        tlb_flush_page(env, addr | (0x7 << 28));
1990
        tlb_flush_page(env, addr | (0x8 << 28));
1991
        tlb_flush_page(env, addr | (0x9 << 28));
1992
        tlb_flush_page(env, addr | (0xA << 28));
1993
        tlb_flush_page(env, addr | (0xB << 28));
1994
        tlb_flush_page(env, addr | (0xC << 28));
1995
        tlb_flush_page(env, addr | (0xD << 28));
1996
        tlb_flush_page(env, addr | (0xE << 28));
1997
        tlb_flush_page(env, addr | (0xF << 28));
1998
        break;
1999
#if defined(TARGET_PPC64)
2000
    case POWERPC_MMU_64B:
2001
        /* tlbie invalidate TLBs for all segments */
2002
        /* XXX: given the fact that there are too many segments to invalidate,
2003
         *      and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
2004
         *      we just invalidate all TLBs
2005
         */
2006
        tlb_flush(env, 1);
2007
        break;
2008
#endif /* defined(TARGET_PPC64) */
2009
    default:
2010
        /* XXX: TODO */
2011
        cpu_abort(env, "Unknown MMU model\n");
2012
        break;
2013
    }
2014
#else
2015
    ppc_tlb_invalidate_all(env);
2016
#endif
2017
}
2018

    
2019
/*****************************************************************************/
2020
/* Special registers manipulation */
2021
#if defined(TARGET_PPC64)
2022
target_ulong ppc_load_asr (CPUPPCState *env)
2023
{
2024
    return env->asr;
2025
}
2026

    
2027
void ppc_store_asr (CPUPPCState *env, target_ulong value)
2028
{
2029
    if (env->asr != value) {
2030
        env->asr = value;
2031
        tlb_flush(env, 1);
2032
    }
2033
}
2034
#endif
2035

    
2036
target_ulong do_load_sdr1 (CPUPPCState *env)
2037
{
2038
    return env->sdr1;
2039
}
2040

    
2041
void do_store_sdr1 (CPUPPCState *env, target_ulong value)
2042
{
2043
#if defined (DEBUG_MMU)
2044
    if (loglevel != 0) {
2045
        fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
2046
    }
2047
#endif
2048
    if (env->sdr1 != value) {
2049
        /* XXX: for PowerPC 64, should check that the HTABSIZE value
2050
         *      is <= 28
2051
         */
2052
        env->sdr1 = value;
2053
        tlb_flush(env, 1);
2054
    }
2055
}
2056

    
2057
#if 0 // Unused
2058
target_ulong do_load_sr (CPUPPCState *env, int srnum)
2059
{
2060
    return env->sr[srnum];
2061
}
2062
#endif
2063

    
2064
void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
2065
{
2066
#if defined (DEBUG_MMU)
2067
    if (loglevel != 0) {
2068
        fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
2069
                __func__, srnum, value, env->sr[srnum]);
2070
    }
2071
#endif
2072
    if (env->sr[srnum] != value) {
2073
        env->sr[srnum] = value;
2074
#if !defined(FLUSH_ALL_TLBS) && 0
2075
        {
2076
            target_ulong page, end;
2077
            /* Invalidate 256 MB of virtual memory */
2078
            page = (16 << 20) * srnum;
2079
            end = page + (16 << 20);
2080
            for (; page != end; page += TARGET_PAGE_SIZE)
2081
                tlb_flush_page(env, page);
2082
        }
2083
#else
2084
        tlb_flush(env, 1);
2085
#endif
2086
    }
2087
}
2088
#endif /* !defined (CONFIG_USER_ONLY) */
2089

    
2090
target_ulong ppc_load_xer (CPUPPCState *env)
2091
{
2092
    return hreg_load_xer(env);
2093
}
2094

    
2095
void ppc_store_xer (CPUPPCState *env, target_ulong value)
2096
{
2097
    hreg_store_xer(env, value);
2098
}
2099

    
2100
/* GDBstub can read and write MSR... */
2101
void ppc_store_msr (CPUPPCState *env, target_ulong value)
2102
{
2103
    hreg_store_msr(env, value);
2104
}
2105

    
2106
/*****************************************************************************/
2107
/* Exception processing */
2108
#if defined (CONFIG_USER_ONLY)
2109
void do_interrupt (CPUState *env)
2110
{
2111
    env->exception_index = POWERPC_EXCP_NONE;
2112
    env->error_code = 0;
2113
}
2114

    
2115
void ppc_hw_interrupt (CPUState *env)
2116
{
2117
    env->exception_index = POWERPC_EXCP_NONE;
2118
    env->error_code = 0;
2119
}
2120
#else /* defined (CONFIG_USER_ONLY) */
2121
static always_inline void dump_syscall (CPUState *env)
2122
{
2123
    fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
2124
            " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
2125
            env->gpr[0], env->gpr[3], env->gpr[4],
2126
            env->gpr[5], env->gpr[6], env->nip);
2127
}
2128

    
2129
/* Note that this function should be greatly optimized
2130
 * when called with a constant excp, from ppc_hw_interrupt
2131
 */
2132
static always_inline void powerpc_excp (CPUState *env,
2133
                                        int excp_model, int excp)
2134
{
2135
    target_ulong msr, new_msr, vector;
2136
    int srr0, srr1, asrr0, asrr1;
2137
    int lpes0, lpes1;
2138
#if defined(TARGET_PPC64)
2139
    int lev;
2140
#endif
2141

    
2142
    if (0) {
2143
        /* XXX: find a suitable condition to enable the hypervisor mode */
2144
        lpes0 = (env->spr[SPR_LPCR] >> 1) & 1;
2145
        lpes1 = (env->spr[SPR_LPCR] >> 2) & 1;
2146
    } else {
2147
        /* Those values ensure we won't enter the hypervisor mode */
2148
        lpes0 = 0;
2149
        lpes1 = 1;
2150
    }
2151

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

    
2780
void do_interrupt (CPUState *env)
2781
{
2782
    powerpc_excp(env, env->excp_model, env->exception_index);
2783
}
2784

    
2785
void ppc_hw_interrupt (CPUPPCState *env)
2786
{
2787
#if defined(TARGET_PPC64)
2788
    int hdice;
2789
#endif
2790

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

    
2908
void cpu_dump_EA (target_ulong EA)
2909
{
2910
    FILE *f;
2911

    
2912
    if (logfile) {
2913
        f = logfile;
2914
    } else {
2915
        f = stdout;
2916
        return;
2917
    }
2918
    fprintf(f, "Memory access at address " ADDRX "\n", EA);
2919
}
2920

    
2921
void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2922
{
2923
    FILE *f;
2924

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

    
2935
void cpu_ppc_reset (void *opaque)
2936
{
2937
    CPUPPCState *env;
2938
    target_ulong msr;
2939

    
2940
    env = opaque;
2941
    msr = (target_ulong)0;
2942
#if defined(TARGET_PPC64)
2943
    msr |= (target_ulong)0 << MSR_HV; /* Should be 1... */
2944
#endif
2945
    msr |= (target_ulong)0 << MSR_AP; /* TO BE CHECKED */
2946
    msr |= (target_ulong)0 << MSR_SA; /* TO BE CHECKED */
2947
    msr |= (target_ulong)1 << MSR_EP;
2948
#if defined (DO_SINGLE_STEP) && 0
2949
    /* Single step trace mode */
2950
    msr |= (target_ulong)1 << MSR_SE;
2951
    msr |= (target_ulong)1 << MSR_BE;
2952
#endif
2953
#if defined(CONFIG_USER_ONLY)
2954
    msr |= (target_ulong)1 << MSR_FP; /* Allow floating point usage */
2955
    msr |= (target_ulong)1 << MSR_PR;
2956
#else
2957
    env->nip = env->hreset_vector | env->excp_prefix;
2958
    if (env->mmu_model != POWERPC_MMU_REAL_4xx)
2959
        ppc_tlb_invalidate_all(env);
2960
#endif
2961
    env->msr = msr;
2962
    hreg_compute_hflags(env);
2963
    env->reserve = (target_ulong)-1ULL;
2964
    /* Be sure no exception or interrupt is pending */
2965
    env->pending_interrupts = 0;
2966
    env->exception_index = POWERPC_EXCP_NONE;
2967
    env->error_code = 0;
2968
    /* Flush all TLBs */
2969
    tlb_flush(env, 1);
2970
}
2971

    
2972
CPUPPCState *cpu_ppc_init (const char *cpu_model)
2973
{
2974
    CPUPPCState *env;
2975
    const ppc_def_t *def;
2976

    
2977
    def = cpu_ppc_find_by_name(cpu_model);
2978
    if (!def)
2979
        return NULL;
2980

    
2981
    env = qemu_mallocz(sizeof(CPUPPCState));
2982
    if (!env)
2983
        return NULL;
2984
    cpu_exec_init(env);
2985
    cpu_ppc_register_internal(env, def);
2986
    cpu_ppc_reset(env);
2987
    return env;
2988
}
2989

    
2990
void cpu_ppc_close (CPUPPCState *env)
2991
{
2992
    /* Should also remove all opcode tables... */
2993
    qemu_free(env);
2994
}