Statistics
| Branch: | Revision:

root / target-ppc / helper.c @ 12de9a39

History | View | Annotate | Download (92.3 kB)

1
/*
2
 *  PowerPC emulation helpers for qemu.
3
 *
4
 *  Copyright (c) 2003-2007 Jocelyn Mayer
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
#include <stdarg.h>
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <inttypes.h>
25
#include <signal.h>
26
#include <assert.h>
27

    
28
#include "cpu.h"
29
#include "exec-all.h"
30

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

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

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

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

    
60
    return 1;
61
}
62

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

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

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

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

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

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

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

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

    
181
    return ret;
182
}
183

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

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

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

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

    
220
    return store;
221
}
222

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

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

    
237
    return nr;
238
}
239

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

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

    
261
static inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
262
                                                 target_ulong eaddr,
263
                                                 int is_code, int match_epn)
264
{
265
#if !defined(FLUSH_ALL_TLBS)
266
    ppc6xx_tlb_t *tlb;
267
    int way, nr;
268

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

    
290
static void ppc6xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
291
                                        int is_code)
292
{
293
    __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
294
}
295

    
296
void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
297
                       target_ulong pte0, target_ulong pte1)
298
{
299
    ppc6xx_tlb_t *tlb;
300
    int nr;
301

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

    
319
static int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
320
                             target_ulong eaddr, int rw, int access_type)
321
{
322
    ppc6xx_tlb_t *tlb;
323
    int nr, best, way;
324
    int ret;
325

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

    
391
    return ret;
392
}
393

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

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

    
489
/* PTE table lookup */
490
static inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h, int rw)
491
{
492
    target_ulong base, pte0, pte1;
493
    int i, good = -1;
494
    int ret, r;
495

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

    
576
    return ret;
577
}
578

    
579
static int find_pte32 (mmu_ctx_t *ctx, int h, int rw)
580
{
581
    return _find_pte(ctx, 0, h, rw);
582
}
583

    
584
#if defined(TARGET_PPC64)
585
static int find_pte64 (mmu_ctx_t *ctx, int h, int rw)
586
{
587
    return _find_pte(ctx, 1, h, rw);
588
}
589
#endif
590

    
591
static inline int find_pte (CPUState *env, mmu_ctx_t *ctx, int h, int rw)
592
{
593
#if defined(TARGET_PPC64)
594
    if (env->mmu_model == POWERPC_MMU_64B)
595
        return find_pte64(ctx, h, rw);
596
#endif
597

    
598
    return find_pte32(ctx, h, rw);
599
}
600

    
601
#if defined(TARGET_PPC64)
602
static int slb_lookup (CPUPPCState *env, target_ulong eaddr,
603
                       target_ulong *vsid, target_ulong *page_mask, int *attr)
604
{
605
    target_phys_addr_t sr_base;
606
    target_ulong mask;
607
    uint64_t tmp64;
608
    uint32_t tmp;
609
    int n, ret;
610
    int slb_nr;
611

    
612
    ret = -5;
613
    sr_base = env->spr[SPR_ASR];
614
#if defined(DEBUG_SLB)
615
    if (loglevel != 0) {
616
        fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
617
                __func__, eaddr, sr_base);
618
    }
619
#endif
620
    mask = 0x0000000000000000ULL; /* Avoid gcc warning */
621
    slb_nr = env->slb_nr;
622
    for (n = 0; n < slb_nr; n++) {
623
        tmp64 = ldq_phys(sr_base);
624
        tmp = ldl_phys(sr_base + 8);
625
#if defined(DEBUG_SLB)
626
        if (loglevel != 0) {
627
        fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08" PRIx32 "\n",
628
                __func__, n, sr_base, tmp64, tmp);
629
        }
630
#endif
631
        if (tmp64 & 0x0000000008000000ULL) {
632
            /* SLB entry is valid */
633
            switch (tmp64 & 0x0000000006000000ULL) {
634
            case 0x0000000000000000ULL:
635
                /* 256 MB segment */
636
                mask = 0xFFFFFFFFF0000000ULL;
637
                break;
638
            case 0x0000000002000000ULL:
639
                /* 1 TB segment */
640
                mask = 0xFFFF000000000000ULL;
641
                break;
642
            case 0x0000000004000000ULL:
643
            case 0x0000000006000000ULL:
644
                /* Reserved => segment is invalid */
645
                continue;
646
            }
647
            if ((eaddr & mask) == (tmp64 & mask)) {
648
                /* SLB match */
649
                *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
650
                *page_mask = ~mask;
651
                *attr = tmp & 0xFF;
652
                ret = 0;
653
                break;
654
            }
655
        }
656
        sr_base += 12;
657
    }
658

    
659
    return ret;
660
}
661

    
662
target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
663
{
664
    target_phys_addr_t sr_base;
665
    target_ulong rt;
666
    uint64_t tmp64;
667
    uint32_t tmp;
668

    
669
    sr_base = env->spr[SPR_ASR];
670
    sr_base += 12 * slb_nr;
671
    tmp64 = ldq_phys(sr_base);
672
    tmp = ldl_phys(sr_base + 8);
673
    if (tmp64 & 0x0000000008000000ULL) {
674
        /* SLB entry is valid */
675
        /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
676
        rt = tmp >> 8;             /* 65:88 => 40:63 */
677
        rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
678
        /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
679
        rt |= ((tmp >> 4) & 0xF) << 27;
680
    } else {
681
        rt = 0;
682
    }
683
#if defined(DEBUG_SLB)
684
    if (loglevel != 0) {
685
        fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
686
                ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
687
    }
688
#endif
689

    
690
    return rt;
691
}
692

    
693
void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
694
{
695
    target_phys_addr_t sr_base;
696
    uint64_t tmp64;
697
    uint32_t tmp;
698

    
699
    sr_base = env->spr[SPR_ASR];
700
    sr_base += 12 * slb_nr;
701
    /* Copy Rs bits 37:63 to SLB 62:88 */
702
    tmp = rs << 8;
703
    tmp64 = (rs >> 24) & 0x7;
704
    /* Copy Rs bits 33:36 to SLB 89:92 */
705
    tmp |= ((rs >> 27) & 0xF) << 4;
706
    /* Set the valid bit */
707
    tmp64 |= 1 << 27;
708
    /* Set ESID */
709
    tmp64 |= (uint32_t)slb_nr << 28;
710
#if defined(DEBUG_SLB)
711
    if (loglevel != 0) {
712
        fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64 " %08"
713
                PRIx32 "\n", __func__, slb_nr, rs, sr_base, tmp64, tmp);
714
    }
715
#endif
716
    /* Write SLB entry to memory */
717
    stq_phys(sr_base, tmp64);
718
    stl_phys(sr_base + 8, tmp);
719
}
720
#endif /* defined(TARGET_PPC64) */
721

    
722
/* Perform segment based translation */
723
static inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
724
                                             int sdr_sh,
725
                                             target_phys_addr_t hash,
726
                                             target_phys_addr_t mask)
727
{
728
    return (sdr1 & ((target_ulong)(-1ULL) << sdr_sh)) | (hash & mask);
729
}
730

    
731
static int get_segment (CPUState *env, mmu_ctx_t *ctx,
732
                        target_ulong eaddr, int rw, int type)
733
{
734
    target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
735
    target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
736
#if defined(TARGET_PPC64)
737
    int attr;
738
#endif
739
    int ds, nx, vsid_sh, sdr_sh;
740
    int ret, ret2;
741

    
742
#if defined(TARGET_PPC64)
743
    if (env->mmu_model == POWERPC_MMU_64B) {
744
#if defined (DEBUG_MMU)
745
        if (loglevel != 0) {
746
            fprintf(logfile, "Check SLBs\n");
747
        }
748
#endif
749
        ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
750
        if (ret < 0)
751
            return ret;
752
        ctx->key = ((attr & 0x40) && msr_pr == 1) ||
753
            ((attr & 0x80) && msr_pr == 0) ? 1 : 0;
754
        ds = 0;
755
        nx = attr & 0x20 ? 1 : 0;
756
        vsid_mask = 0x00003FFFFFFFFF80ULL;
757
        vsid_sh = 7;
758
        sdr_sh = 18;
759
        sdr_mask = 0x3FF80;
760
    } else
761
#endif /* defined(TARGET_PPC64) */
762
    {
763
        sr = env->sr[eaddr >> 28];
764
        page_mask = 0x0FFFFFFF;
765
        ctx->key = (((sr & 0x20000000) && msr_pr == 1) ||
766
                    ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
767
        ds = sr & 0x80000000 ? 1 : 0;
768
        nx = sr & 0x10000000 ? 1 : 0;
769
        vsid = sr & 0x00FFFFFF;
770
        vsid_mask = 0x01FFFFC0;
771
        vsid_sh = 6;
772
        sdr_sh = 16;
773
        sdr_mask = 0xFFC0;
774
#if defined (DEBUG_MMU)
775
        if (loglevel != 0) {
776
            fprintf(logfile, "Check segment v=0x" ADDRX " %d 0x" ADDRX
777
                    " nip=0x" ADDRX " lr=0x" ADDRX
778
                    " ir=%d dr=%d pr=%d %d t=%d\n",
779
                    eaddr, (int)(eaddr >> 28), sr, env->nip,
780
                    env->lr, msr_ir, msr_dr, msr_pr, rw, type);
781
        }
782
#endif
783
    }
784
#if defined (DEBUG_MMU)
785
    if (loglevel != 0) {
786
        fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
787
                ctx->key, ds, nx, vsid);
788
    }
789
#endif
790
    ret = -1;
791
    if (!ds) {
792
        /* Check if instruction fetch is allowed, if needed */
793
        if (type != ACCESS_CODE || nx == 0) {
794
            /* Page address translation */
795
            /* Primary table address */
796
            sdr = env->sdr1;
797
            pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
798
#if defined(TARGET_PPC64)
799
            if (env->mmu_model == POWERPC_MMU_64B) {
800
                htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
801
                /* XXX: this is false for 1 TB segments */
802
                hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
803
            } else
804
#endif
805
            {
806
                htab_mask = sdr & 0x000001FF;
807
                hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
808
            }
809
            mask = (htab_mask << sdr_sh) | sdr_mask;
810
#if defined (DEBUG_MMU)
811
            if (loglevel != 0) {
812
                fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
813
                        PADDRX " " ADDRX "\n", sdr, sdr_sh, hash, mask,
814
                        page_mask);
815
            }
816
#endif
817
            ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
818
            /* Secondary table address */
819
            hash = (~hash) & vsid_mask;
820
#if defined (DEBUG_MMU)
821
            if (loglevel != 0) {
822
                fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
823
                        PADDRX "\n", sdr, sdr_sh, hash, mask);
824
            }
825
#endif
826
            ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
827
#if defined(TARGET_PPC64)
828
            if (env->mmu_model == POWERPC_MMU_64B) {
829
                /* Only 5 bits of the page index are used in the AVPN */
830
                ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
831
            } else
832
#endif
833
            {
834
                ctx->ptem = (vsid << 7) | (pgidx >> 10);
835
            }
836
            /* Initialize real address with an invalid value */
837
            ctx->raddr = (target_ulong)-1;
838
            if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
839
                         env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
840
                /* Software TLB search */
841
                ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
842
            } else {
843
#if defined (DEBUG_MMU)
844
                if (loglevel != 0) {
845
                    fprintf(logfile, "0 sdr1=0x" PADDRX " vsid=0x%06x "
846
                            "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX "\n",
847
                            sdr, (uint32_t)vsid, (uint32_t)pgidx,
848
                            (uint32_t)hash, ctx->pg_addr[0]);
849
                }
850
#endif
851
                /* Primary table lookup */
852
                ret = find_pte(env, ctx, 0, rw);
853
                if (ret < 0) {
854
                    /* Secondary table lookup */
855
#if defined (DEBUG_MMU)
856
                    if (eaddr != 0xEFFFFFFF && loglevel != 0) {
857
                        fprintf(logfile,
858
                                "1 sdr1=0x" PADDRX " vsid=0x%06x api=0x%04x "
859
                                "hash=0x%05x pg_addr=0x" PADDRX "\n",
860
                                sdr, (uint32_t)vsid, (uint32_t)pgidx,
861
                                (uint32_t)hash, ctx->pg_addr[1]);
862
                    }
863
#endif
864
                    ret2 = find_pte(env, ctx, 1, rw);
865
                    if (ret2 != -1)
866
                        ret = ret2;
867
                }
868
            }
869
#if defined (DEBUG_MMU)
870
                    if (loglevel != 0) {
871
                        target_phys_addr_t curaddr;
872
                        uint32_t a0, a1, a2, a3;
873
                        fprintf(logfile,
874
                                "Page table: " PADDRX " len " PADDRX "\n",
875
                                sdr, mask + 0x80);
876
                        for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
877
                             curaddr += 16) {
878
                            a0 = ldl_phys(curaddr);
879
                            a1 = ldl_phys(curaddr + 4);
880
                            a2 = ldl_phys(curaddr + 8);
881
                            a3 = ldl_phys(curaddr + 12);
882
                            if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
883
                                fprintf(logfile,
884
                                        PADDRX ": %08x %08x %08x %08x\n",
885
                                        curaddr, a0, a1, a2, a3);
886
                            }
887
                        }
888
                    }
889
#endif
890
        } else {
891
#if defined (DEBUG_MMU)
892
            if (loglevel != 0)
893
                fprintf(logfile, "No access allowed\n");
894
#endif
895
            ret = -3;
896
        }
897
    } else {
898
#if defined (DEBUG_MMU)
899
        if (loglevel != 0)
900
            fprintf(logfile, "direct store...\n");
901
#endif
902
        /* Direct-store segment : absolutely *BUGGY* for now */
903
        switch (type) {
904
        case ACCESS_INT:
905
            /* Integer load/store : only access allowed */
906
            break;
907
        case ACCESS_CODE:
908
            /* No code fetch is allowed in direct-store areas */
909
            return -4;
910
        case ACCESS_FLOAT:
911
            /* Floating point load/store */
912
            return -4;
913
        case ACCESS_RES:
914
            /* lwarx, ldarx or srwcx. */
915
            return -4;
916
        case ACCESS_CACHE:
917
            /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
918
            /* Should make the instruction do no-op.
919
             * As it already do no-op, it's quite easy :-)
920
             */
921
            ctx->raddr = eaddr;
922
            return 0;
923
        case ACCESS_EXT:
924
            /* eciwx or ecowx */
925
            return -4;
926
        default:
927
            if (logfile) {
928
                fprintf(logfile, "ERROR: instruction should not need "
929
                        "address translation\n");
930
            }
931
            return -4;
932
        }
933
        if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
934
            ctx->raddr = eaddr;
935
            ret = 2;
936
        } else {
937
            ret = -2;
938
        }
939
    }
940

    
941
    return ret;
942
}
943

    
944
/* Generic TLB check function for embedded PowerPC implementations */
945
static int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
946
                             target_phys_addr_t *raddrp,
947
                             target_ulong address,
948
                             uint32_t pid, int ext, int i)
949
{
950
    target_ulong mask;
951

    
952
    /* Check valid flag */
953
    if (!(tlb->prot & PAGE_VALID)) {
954
        if (loglevel != 0)
955
            fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
956
        return -1;
957
    }
958
    mask = ~(tlb->size - 1);
959
#if defined (DEBUG_SOFTWARE_TLB)
960
    if (loglevel != 0) {
961
        fprintf(logfile, "%s: TLB %d address " ADDRX " PID %d <=> "
962
                ADDRX " " ADDRX " %d\n",
963
                __func__, i, address, pid, tlb->EPN, mask, (int)tlb->PID);
964
    }
965
#endif
966
    /* Check PID */
967
    if (tlb->PID != 0 && tlb->PID != pid)
968
        return -1;
969
    /* Check effective address */
970
    if ((address & mask) != tlb->EPN)
971
        return -1;
972
    *raddrp = (tlb->RPN & mask) | (address & ~mask);
973
#if (TARGET_PHYS_ADDR_BITS >= 36)
974
    if (ext) {
975
        /* Extend the physical address to 36 bits */
976
        *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
977
    }
978
#endif
979

    
980
    return 0;
981
}
982

    
983
/* Generic TLB search function for PowerPC embedded implementations */
984
int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
985
{
986
    ppcemb_tlb_t *tlb;
987
    target_phys_addr_t raddr;
988
    int i, ret;
989

    
990
    /* Default return value is no match */
991
    ret = -1;
992
    for (i = 0; i < env->nb_tlb; i++) {
993
        tlb = &env->tlb[i].tlbe;
994
        if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
995
            ret = i;
996
            break;
997
        }
998
    }
999

    
1000
    return ret;
1001
}
1002

    
1003
/* Helpers specific to PowerPC 40x implementations */
1004
static void ppc4xx_tlb_invalidate_all (CPUState *env)
1005
{
1006
    ppcemb_tlb_t *tlb;
1007
    int i;
1008

    
1009
    for (i = 0; i < env->nb_tlb; i++) {
1010
        tlb = &env->tlb[i].tlbe;
1011
        tlb->prot &= ~PAGE_VALID;
1012
    }
1013
    tlb_flush(env, 1);
1014
}
1015

    
1016
static void ppc4xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
1017
                                        uint32_t pid)
1018
{
1019
#if !defined(FLUSH_ALL_TLBS)
1020
    ppcemb_tlb_t *tlb;
1021
    target_phys_addr_t raddr;
1022
    target_ulong page, end;
1023
    int i;
1024

    
1025
    for (i = 0; i < env->nb_tlb; i++) {
1026
        tlb = &env->tlb[i].tlbe;
1027
        if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
1028
            end = tlb->EPN + tlb->size;
1029
            for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1030
                tlb_flush_page(env, page);
1031
            tlb->prot &= ~PAGE_VALID;
1032
            break;
1033
        }
1034
    }
1035
#else
1036
    ppc4xx_tlb_invalidate_all(env);
1037
#endif
1038
}
1039

    
1040
int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1041
                                 target_ulong address, int rw, int access_type)
1042
{
1043
    ppcemb_tlb_t *tlb;
1044
    target_phys_addr_t raddr;
1045
    int i, ret, zsel, zpr;
1046

    
1047
    ret = -1;
1048
    raddr = -1;
1049
    for (i = 0; i < env->nb_tlb; i++) {
1050
        tlb = &env->tlb[i].tlbe;
1051
        if (ppcemb_tlb_check(env, tlb, &raddr, address,
1052
                             env->spr[SPR_40x_PID], 0, i) < 0)
1053
            continue;
1054
        zsel = (tlb->attr >> 4) & 0xF;
1055
        zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
1056
#if defined (DEBUG_SOFTWARE_TLB)
1057
        if (loglevel != 0) {
1058
            fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1059
                    __func__, i, zsel, zpr, rw, tlb->attr);
1060
        }
1061
#endif
1062
        if (access_type == ACCESS_CODE) {
1063
            /* Check execute enable bit */
1064
            switch (zpr) {
1065
            case 0x2:
1066
                if (msr_pr)
1067
                    goto check_exec_perm;
1068
                goto exec_granted;
1069
            case 0x0:
1070
                if (msr_pr) {
1071
                    ctx->prot = 0;
1072
                    ret = -3;
1073
                    break;
1074
                }
1075
                /* No break here */
1076
            case 0x1:
1077
            check_exec_perm:
1078
                /* Check from TLB entry */
1079
                if (!(tlb->prot & PAGE_EXEC)) {
1080
                    ret = -3;
1081
                } else {
1082
                    if (tlb->prot & PAGE_WRITE) {
1083
                        ctx->prot = PAGE_READ | PAGE_WRITE;
1084
                    } else {
1085
                        ctx->prot = PAGE_READ;
1086
                    }
1087
                    ret = 0;
1088
                }
1089
                break;
1090
            case 0x3:
1091
            exec_granted:
1092
                /* All accesses granted */
1093
                ctx->prot = PAGE_READ | PAGE_WRITE;
1094
                ret = 0;
1095
                break;
1096
            }
1097
        } else {
1098
            switch (zpr) {
1099
            case 0x2:
1100
                if (msr_pr)
1101
                    goto check_rw_perm;
1102
                goto rw_granted;
1103
            case 0x0:
1104
                if (msr_pr) {
1105
                    ctx->prot = 0;
1106
                    ret = -2;
1107
                    break;
1108
                }
1109
                /* No break here */
1110
            case 0x1:
1111
            check_rw_perm:
1112
                /* Check from TLB entry */
1113
                /* Check write protection bit */
1114
                if (tlb->prot & PAGE_WRITE) {
1115
                    ctx->prot = PAGE_READ | PAGE_WRITE;
1116
                    ret = 0;
1117
                } else {
1118
                    ctx->prot = PAGE_READ;
1119
                    if (rw)
1120
                        ret = -2;
1121
                    else
1122
                        ret = 0;
1123
                }
1124
                break;
1125
            case 0x3:
1126
            rw_granted:
1127
                /* All accesses granted */
1128
                ctx->prot = PAGE_READ | PAGE_WRITE;
1129
                ret = 0;
1130
                break;
1131
            }
1132
        }
1133
        if (ret >= 0) {
1134
            ctx->raddr = raddr;
1135
#if defined (DEBUG_SOFTWARE_TLB)
1136
            if (loglevel != 0) {
1137
                fprintf(logfile, "%s: access granted " ADDRX " => " REGX
1138
                        " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1139
                        ret);
1140
            }
1141
#endif
1142
            return 0;
1143
        }
1144
    }
1145
#if defined (DEBUG_SOFTWARE_TLB)
1146
    if (loglevel != 0) {
1147
        fprintf(logfile, "%s: access refused " ADDRX " => " REGX
1148
                " %d %d\n", __func__, address, raddr, ctx->prot,
1149
                ret);
1150
    }
1151
#endif
1152

    
1153
    return ret;
1154
}
1155

    
1156
void store_40x_sler (CPUPPCState *env, uint32_t val)
1157
{
1158
    /* XXX: TO BE FIXED */
1159
    if (val != 0x00000000) {
1160
        cpu_abort(env, "Little-endian regions are not supported by now\n");
1161
    }
1162
    env->spr[SPR_405_SLER] = val;
1163
}
1164

    
1165
int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1166
                                   target_ulong address, int rw,
1167
                                   int access_type)
1168
{
1169
    ppcemb_tlb_t *tlb;
1170
    target_phys_addr_t raddr;
1171
    int i, prot, ret;
1172

    
1173
    ret = -1;
1174
    raddr = -1;
1175
    for (i = 0; i < env->nb_tlb; i++) {
1176
        tlb = &env->tlb[i].tlbe;
1177
        if (ppcemb_tlb_check(env, tlb, &raddr, address,
1178
                             env->spr[SPR_BOOKE_PID], 1, i) < 0)
1179
            continue;
1180
        if (msr_pr)
1181
            prot = tlb->prot & 0xF;
1182
        else
1183
            prot = (tlb->prot >> 4) & 0xF;
1184
        /* Check the address space */
1185
        if (access_type == ACCESS_CODE) {
1186
            if (msr_is != (tlb->attr & 1))
1187
                continue;
1188
            ctx->prot = prot;
1189
            if (prot & PAGE_EXEC) {
1190
                ret = 0;
1191
                break;
1192
            }
1193
            ret = -3;
1194
        } else {
1195
            if (msr_ds != (tlb->attr & 1))
1196
                continue;
1197
            ctx->prot = prot;
1198
            if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1199
                ret = 0;
1200
                break;
1201
            }
1202
            ret = -2;
1203
        }
1204
    }
1205
    if (ret >= 0)
1206
        ctx->raddr = raddr;
1207

    
1208
    return ret;
1209
}
1210

    
1211
static int check_physical (CPUState *env, mmu_ctx_t *ctx,
1212
                           target_ulong eaddr, int rw)
1213
{
1214
    int in_plb, ret;
1215

    
1216
    ctx->raddr = eaddr;
1217
    ctx->prot = PAGE_READ;
1218
    ret = 0;
1219
    switch (env->mmu_model) {
1220
    case POWERPC_MMU_32B:
1221
    case POWERPC_MMU_SOFT_6xx:
1222
    case POWERPC_MMU_SOFT_74xx:
1223
    case POWERPC_MMU_601:
1224
    case POWERPC_MMU_SOFT_4xx:
1225
    case POWERPC_MMU_REAL_4xx:
1226
    case POWERPC_MMU_BOOKE:
1227
        ctx->prot |= PAGE_WRITE;
1228
        break;
1229
#if defined(TARGET_PPC64)
1230
    case POWERPC_MMU_64B:
1231
        /* Real address are 60 bits long */
1232
        ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1233
        ctx->prot |= PAGE_WRITE;
1234
        break;
1235
#endif
1236
    case POWERPC_MMU_SOFT_4xx_Z:
1237
        if (unlikely(msr_pe != 0)) {
1238
            /* 403 family add some particular protections,
1239
             * using PBL/PBU registers for accesses with no translation.
1240
             */
1241
            in_plb =
1242
                /* Check PLB validity */
1243
                (env->pb[0] < env->pb[1] &&
1244
                 /* and address in plb area */
1245
                 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1246
                (env->pb[2] < env->pb[3] &&
1247
                 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1248
            if (in_plb ^ msr_px) {
1249
                /* Access in protected area */
1250
                if (rw == 1) {
1251
                    /* Access is not allowed */
1252
                    ret = -2;
1253
                }
1254
            } else {
1255
                /* Read-write access is allowed */
1256
                ctx->prot |= PAGE_WRITE;
1257
            }
1258
        }
1259
        break;
1260
    case POWERPC_MMU_BOOKE_FSL:
1261
        /* XXX: TODO */
1262
        cpu_abort(env, "BookE FSL MMU model not implemented\n");
1263
        break;
1264
    default:
1265
        cpu_abort(env, "Unknown or invalid MMU model\n");
1266
        return -1;
1267
    }
1268

    
1269
    return ret;
1270
}
1271

    
1272
int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1273
                          int rw, int access_type, int check_BATs)
1274
{
1275
    int ret;
1276
#if 0
1277
    if (loglevel != 0) {
1278
        fprintf(logfile, "%s\n", __func__);
1279
    }
1280
#endif
1281
    if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1282
        (access_type != ACCESS_CODE && msr_dr == 0)) {
1283
        /* No address translation */
1284
        ret = check_physical(env, ctx, eaddr, rw);
1285
    } else {
1286
        ret = -1;
1287
        switch (env->mmu_model) {
1288
        case POWERPC_MMU_32B:
1289
        case POWERPC_MMU_SOFT_6xx:
1290
        case POWERPC_MMU_SOFT_74xx:
1291
            /* Try to find a BAT */
1292
            if (check_BATs)
1293
                ret = get_bat(env, ctx, eaddr, rw, access_type);
1294
            /* No break here */
1295
#if defined(TARGET_PPC64)
1296
        case POWERPC_MMU_64B:
1297
#endif
1298
            if (ret < 0) {
1299
                /* We didn't match any BAT entry or don't have BATs */
1300
                ret = get_segment(env, ctx, eaddr, rw, access_type);
1301
            }
1302
            break;
1303
        case POWERPC_MMU_SOFT_4xx:
1304
        case POWERPC_MMU_SOFT_4xx_Z:
1305
            ret = mmu40x_get_physical_address(env, ctx, eaddr,
1306
                                              rw, access_type);
1307
            break;
1308
        case POWERPC_MMU_601:
1309
            /* XXX: TODO */
1310
            cpu_abort(env, "601 MMU model not implemented\n");
1311
            return -1;
1312
        case POWERPC_MMU_BOOKE:
1313
            ret = mmubooke_get_physical_address(env, ctx, eaddr,
1314
                                                rw, access_type);
1315
            break;
1316
        case POWERPC_MMU_BOOKE_FSL:
1317
            /* XXX: TODO */
1318
            cpu_abort(env, "BookE FSL MMU model not implemented\n");
1319
            return -1;
1320
        case POWERPC_MMU_REAL_4xx:
1321
            cpu_abort(env, "PowerPC 401 does not do any translation\n");
1322
            return -1;
1323
        default:
1324
            cpu_abort(env, "Unknown or invalid MMU model\n");
1325
            return -1;
1326
        }
1327
    }
1328
#if 0
1329
    if (loglevel != 0) {
1330
        fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1331
                __func__, eaddr, ret, ctx->raddr);
1332
    }
1333
#endif
1334

    
1335
    return ret;
1336
}
1337

    
1338
target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1339
{
1340
    mmu_ctx_t ctx;
1341

    
1342
    if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT, 1) != 0))
1343
        return -1;
1344

    
1345
    return ctx.raddr & TARGET_PAGE_MASK;
1346
}
1347

    
1348
/* Perform address translation */
1349
int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1350
                              int is_user, int is_softmmu)
1351
{
1352
    mmu_ctx_t ctx;
1353
    int access_type;
1354
    int ret = 0;
1355

    
1356
    if (rw == 2) {
1357
        /* code access */
1358
        rw = 0;
1359
        access_type = ACCESS_CODE;
1360
    } else {
1361
        /* data access */
1362
        /* XXX: put correct access by using cpu_restore_state()
1363
           correctly */
1364
        access_type = ACCESS_INT;
1365
        //        access_type = env->access_type;
1366
    }
1367
    ret = get_physical_address(env, &ctx, address, rw, access_type, 1);
1368
    if (ret == 0) {
1369
        ret = tlb_set_page(env, address & TARGET_PAGE_MASK,
1370
                           ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1371
                           is_user, is_softmmu);
1372
    } else if (ret < 0) {
1373
#if defined (DEBUG_MMU)
1374
        if (loglevel != 0)
1375
            cpu_dump_state(env, logfile, fprintf, 0);
1376
#endif
1377
        if (access_type == ACCESS_CODE) {
1378
            switch (ret) {
1379
            case -1:
1380
                /* No matches in page tables or TLB */
1381
                switch (env->mmu_model) {
1382
                case POWERPC_MMU_SOFT_6xx:
1383
                    env->exception_index = POWERPC_EXCP_IFTLB;
1384
                    env->error_code = 1 << 18;
1385
                    env->spr[SPR_IMISS] = address;
1386
                    env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1387
                    goto tlb_miss;
1388
                case POWERPC_MMU_SOFT_74xx:
1389
                    env->exception_index = POWERPC_EXCP_IFTLB;
1390
                    goto tlb_miss_74xx;
1391
                case POWERPC_MMU_SOFT_4xx:
1392
                case POWERPC_MMU_SOFT_4xx_Z:
1393
                    env->exception_index = POWERPC_EXCP_ITLB;
1394
                    env->error_code = 0;
1395
                    env->spr[SPR_40x_DEAR] = address;
1396
                    env->spr[SPR_40x_ESR] = 0x00000000;
1397
                    break;
1398
                case POWERPC_MMU_32B:
1399
#if defined(TARGET_PPC64)
1400
                case POWERPC_MMU_64B:
1401
#endif
1402
                    env->exception_index = POWERPC_EXCP_ISI;
1403
                    env->error_code = 0x40000000;
1404
                    break;
1405
                case POWERPC_MMU_601:
1406
                    /* XXX: TODO */
1407
                    cpu_abort(env, "MMU model not implemented\n");
1408
                    return -1;
1409
                case POWERPC_MMU_BOOKE:
1410
                    /* XXX: TODO */
1411
                    cpu_abort(env, "MMU model not implemented\n");
1412
                    return -1;
1413
                case POWERPC_MMU_BOOKE_FSL:
1414
                    /* XXX: TODO */
1415
                    cpu_abort(env, "MMU model not implemented\n");
1416
                    return -1;
1417
                case POWERPC_MMU_REAL_4xx:
1418
                    cpu_abort(env, "PowerPC 401 should never raise any MMU "
1419
                              "exceptions\n");
1420
                    return -1;
1421
                default:
1422
                    cpu_abort(env, "Unknown or invalid MMU model\n");
1423
                    return -1;
1424
                }
1425
                break;
1426
            case -2:
1427
                /* Access rights violation */
1428
                env->exception_index = POWERPC_EXCP_ISI;
1429
                env->error_code = 0x08000000;
1430
                break;
1431
            case -3:
1432
                /* No execute protection violation */
1433
                env->exception_index = POWERPC_EXCP_ISI;
1434
                env->error_code = 0x10000000;
1435
                break;
1436
            case -4:
1437
                /* Direct store exception */
1438
                /* No code fetch is allowed in direct-store areas */
1439
                env->exception_index = POWERPC_EXCP_ISI;
1440
                env->error_code = 0x10000000;
1441
                break;
1442
#if defined(TARGET_PPC64)
1443
            case -5:
1444
                /* No match in segment table */
1445
                env->exception_index = POWERPC_EXCP_ISEG;
1446
                env->error_code = 0;
1447
                break;
1448
#endif
1449
            }
1450
        } else {
1451
            switch (ret) {
1452
            case -1:
1453
                /* No matches in page tables or TLB */
1454
                switch (env->mmu_model) {
1455
                case POWERPC_MMU_SOFT_6xx:
1456
                    if (rw == 1) {
1457
                        env->exception_index = POWERPC_EXCP_DSTLB;
1458
                        env->error_code = 1 << 16;
1459
                    } else {
1460
                        env->exception_index = POWERPC_EXCP_DLTLB;
1461
                        env->error_code = 0;
1462
                    }
1463
                    env->spr[SPR_DMISS] = address;
1464
                    env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1465
                tlb_miss:
1466
                    env->error_code |= ctx.key << 19;
1467
                    env->spr[SPR_HASH1] = ctx.pg_addr[0];
1468
                    env->spr[SPR_HASH2] = ctx.pg_addr[1];
1469
                    break;
1470
                case POWERPC_MMU_SOFT_74xx:
1471
                    if (rw == 1) {
1472
                        env->exception_index = POWERPC_EXCP_DSTLB;
1473
                    } else {
1474
                        env->exception_index = POWERPC_EXCP_DLTLB;
1475
                    }
1476
                tlb_miss_74xx:
1477
                    /* Implement LRU algorithm */
1478
                    env->error_code = ctx.key << 19;
1479
                    env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1480
                        ((env->last_way + 1) & (env->nb_ways - 1));
1481
                    env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
1482
                    break;
1483
                case POWERPC_MMU_SOFT_4xx:
1484
                case POWERPC_MMU_SOFT_4xx_Z:
1485
                    env->exception_index = POWERPC_EXCP_DTLB;
1486
                    env->error_code = 0;
1487
                    env->spr[SPR_40x_DEAR] = address;
1488
                    if (rw)
1489
                        env->spr[SPR_40x_ESR] = 0x00800000;
1490
                    else
1491
                        env->spr[SPR_40x_ESR] = 0x00000000;
1492
                    break;
1493
                case POWERPC_MMU_32B:
1494
#if defined(TARGET_PPC64)
1495
                case POWERPC_MMU_64B:
1496
#endif
1497
                    env->exception_index = POWERPC_EXCP_DSI;
1498
                    env->error_code = 0;
1499
                    env->spr[SPR_DAR] = address;
1500
                    if (rw == 1)
1501
                        env->spr[SPR_DSISR] = 0x42000000;
1502
                    else
1503
                        env->spr[SPR_DSISR] = 0x40000000;
1504
                    break;
1505
                case POWERPC_MMU_601:
1506
                    /* XXX: TODO */
1507
                    cpu_abort(env, "MMU model not implemented\n");
1508
                    return -1;
1509
                case POWERPC_MMU_BOOKE:
1510
                    /* XXX: TODO */
1511
                    cpu_abort(env, "MMU model not implemented\n");
1512
                    return -1;
1513
                case POWERPC_MMU_BOOKE_FSL:
1514
                    /* XXX: TODO */
1515
                    cpu_abort(env, "MMU model not implemented\n");
1516
                    return -1;
1517
                case POWERPC_MMU_REAL_4xx:
1518
                    cpu_abort(env, "PowerPC 401 should never raise any MMU "
1519
                              "exceptions\n");
1520
                    return -1;
1521
                default:
1522
                    cpu_abort(env, "Unknown or invalid MMU model\n");
1523
                    return -1;
1524
                }
1525
                break;
1526
            case -2:
1527
                /* Access rights violation */
1528
                env->exception_index = POWERPC_EXCP_DSI;
1529
                env->error_code = 0;
1530
                env->spr[SPR_DAR] = address;
1531
                if (rw == 1)
1532
                    env->spr[SPR_DSISR] = 0x0A000000;
1533
                else
1534
                    env->spr[SPR_DSISR] = 0x08000000;
1535
                break;
1536
            case -4:
1537
                /* Direct store exception */
1538
                switch (access_type) {
1539
                case ACCESS_FLOAT:
1540
                    /* Floating point load/store */
1541
                    env->exception_index = POWERPC_EXCP_ALIGN;
1542
                    env->error_code = POWERPC_EXCP_ALIGN_FP;
1543
                    env->spr[SPR_DAR] = address;
1544
                    break;
1545
                case ACCESS_RES:
1546
                    /* lwarx, ldarx or stwcx. */
1547
                    env->exception_index = POWERPC_EXCP_DSI;
1548
                    env->error_code = 0;
1549
                    env->spr[SPR_DAR] = address;
1550
                    if (rw == 1)
1551
                        env->spr[SPR_DSISR] = 0x06000000;
1552
                    else
1553
                        env->spr[SPR_DSISR] = 0x04000000;
1554
                    break;
1555
                case ACCESS_EXT:
1556
                    /* eciwx or ecowx */
1557
                    env->exception_index = POWERPC_EXCP_DSI;
1558
                    env->error_code = 0;
1559
                    env->spr[SPR_DAR] = address;
1560
                    if (rw == 1)
1561
                        env->spr[SPR_DSISR] = 0x06100000;
1562
                    else
1563
                        env->spr[SPR_DSISR] = 0x04100000;
1564
                    break;
1565
                default:
1566
                    printf("DSI: invalid exception (%d)\n", ret);
1567
                    env->exception_index = POWERPC_EXCP_PROGRAM;
1568
                    env->error_code =
1569
                        POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1570
                    env->spr[SPR_DAR] = address;
1571
                    break;
1572
                }
1573
                break;
1574
#if defined(TARGET_PPC64)
1575
            case -5:
1576
                /* No match in segment table */
1577
                env->exception_index = POWERPC_EXCP_DSEG;
1578
                env->error_code = 0;
1579
                env->spr[SPR_DAR] = address;
1580
                break;
1581
#endif
1582
            }
1583
        }
1584
#if 0
1585
        printf("%s: set exception to %d %02x\n", __func__,
1586
               env->exception, env->error_code);
1587
#endif
1588
        ret = 1;
1589
    }
1590

    
1591
    return ret;
1592
}
1593

    
1594
/*****************************************************************************/
1595
/* BATs management */
1596
#if !defined(FLUSH_ALL_TLBS)
1597
static inline void do_invalidate_BAT (CPUPPCState *env,
1598
                                      target_ulong BATu, target_ulong mask)
1599
{
1600
    target_ulong base, end, page;
1601

    
1602
    base = BATu & ~0x0001FFFF;
1603
    end = base + mask + 0x00020000;
1604
#if defined (DEBUG_BATS)
1605
    if (loglevel != 0) {
1606
        fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1607
                base, end, mask);
1608
    }
1609
#endif
1610
    for (page = base; page != end; page += TARGET_PAGE_SIZE)
1611
        tlb_flush_page(env, page);
1612
#if defined (DEBUG_BATS)
1613
    if (loglevel != 0)
1614
        fprintf(logfile, "Flush done\n");
1615
#endif
1616
}
1617
#endif
1618

    
1619
static inline void dump_store_bat (CPUPPCState *env, char ID, int ul, int nr,
1620
                                   target_ulong value)
1621
{
1622
#if defined (DEBUG_BATS)
1623
    if (loglevel != 0) {
1624
        fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1625
                ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1626
    }
1627
#endif
1628
}
1629

    
1630
target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1631
{
1632
    return env->IBAT[0][nr];
1633
}
1634

    
1635
target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1636
{
1637
    return env->IBAT[1][nr];
1638
}
1639

    
1640
void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1641
{
1642
    target_ulong mask;
1643

    
1644
    dump_store_bat(env, 'I', 0, nr, value);
1645
    if (env->IBAT[0][nr] != value) {
1646
        mask = (value << 15) & 0x0FFE0000UL;
1647
#if !defined(FLUSH_ALL_TLBS)
1648
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1649
#endif
1650
        /* When storing valid upper BAT, mask BEPI and BRPN
1651
         * and invalidate all TLBs covered by this BAT
1652
         */
1653
        mask = (value << 15) & 0x0FFE0000UL;
1654
        env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1655
            (value & ~0x0001FFFFUL & ~mask);
1656
        env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1657
            (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1658
#if !defined(FLUSH_ALL_TLBS)
1659
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1660
#else
1661
        tlb_flush(env, 1);
1662
#endif
1663
    }
1664
}
1665

    
1666
void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1667
{
1668
    dump_store_bat(env, 'I', 1, nr, value);
1669
    env->IBAT[1][nr] = value;
1670
}
1671

    
1672
target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1673
{
1674
    return env->DBAT[0][nr];
1675
}
1676

    
1677
target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1678
{
1679
    return env->DBAT[1][nr];
1680
}
1681

    
1682
void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1683
{
1684
    target_ulong mask;
1685

    
1686
    dump_store_bat(env, 'D', 0, nr, value);
1687
    if (env->DBAT[0][nr] != value) {
1688
        /* When storing valid upper BAT, mask BEPI and BRPN
1689
         * and invalidate all TLBs covered by this BAT
1690
         */
1691
        mask = (value << 15) & 0x0FFE0000UL;
1692
#if !defined(FLUSH_ALL_TLBS)
1693
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1694
#endif
1695
        mask = (value << 15) & 0x0FFE0000UL;
1696
        env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1697
            (value & ~0x0001FFFFUL & ~mask);
1698
        env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1699
            (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1700
#if !defined(FLUSH_ALL_TLBS)
1701
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1702
#else
1703
        tlb_flush(env, 1);
1704
#endif
1705
    }
1706
}
1707

    
1708
void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1709
{
1710
    dump_store_bat(env, 'D', 1, nr, value);
1711
    env->DBAT[1][nr] = value;
1712
}
1713

    
1714

    
1715
/*****************************************************************************/
1716
/* TLB management */
1717
void ppc_tlb_invalidate_all (CPUPPCState *env)
1718
{
1719
    switch (env->mmu_model) {
1720
    case POWERPC_MMU_SOFT_6xx:
1721
    case POWERPC_MMU_SOFT_74xx:
1722
        ppc6xx_tlb_invalidate_all(env);
1723
        break;
1724
    case POWERPC_MMU_SOFT_4xx:
1725
    case POWERPC_MMU_SOFT_4xx_Z:
1726
        ppc4xx_tlb_invalidate_all(env);
1727
        break;
1728
    case POWERPC_MMU_REAL_4xx:
1729
        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1730
        break;
1731
    case POWERPC_MMU_BOOKE:
1732
        /* XXX: TODO */
1733
        cpu_abort(env, "MMU model not implemented\n");
1734
        break;
1735
    case POWERPC_MMU_BOOKE_FSL:
1736
        /* XXX: TODO */
1737
        cpu_abort(env, "MMU model not implemented\n");
1738
        break;
1739
    case POWERPC_MMU_601:
1740
        /* XXX: TODO */
1741
        cpu_abort(env, "MMU model not implemented\n");
1742
        break;
1743
    case POWERPC_MMU_32B:
1744
#if defined(TARGET_PPC64)
1745
    case POWERPC_MMU_64B:
1746
#endif /* defined(TARGET_PPC64) */
1747
        tlb_flush(env, 1);
1748
        break;
1749
    default:
1750
        /* XXX: TODO */
1751
        cpu_abort(env, "Unknown MMU model\n");
1752
        break;
1753
    }
1754
}
1755

    
1756
void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1757
{
1758
#if !defined(FLUSH_ALL_TLBS)
1759
    addr &= TARGET_PAGE_MASK;
1760
    switch (env->mmu_model) {
1761
    case POWERPC_MMU_SOFT_6xx:
1762
    case POWERPC_MMU_SOFT_74xx:
1763
        ppc6xx_tlb_invalidate_virt(env, addr, 0);
1764
        if (env->id_tlbs == 1)
1765
            ppc6xx_tlb_invalidate_virt(env, addr, 1);
1766
        break;
1767
    case POWERPC_MMU_SOFT_4xx:
1768
    case POWERPC_MMU_SOFT_4xx_Z:
1769
        ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
1770
        break;
1771
    case POWERPC_MMU_REAL_4xx:
1772
        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1773
        break;
1774
    case POWERPC_MMU_BOOKE:
1775
        /* XXX: TODO */
1776
        cpu_abort(env, "MMU model not implemented\n");
1777
        break;
1778
    case POWERPC_MMU_BOOKE_FSL:
1779
        /* XXX: TODO */
1780
        cpu_abort(env, "MMU model not implemented\n");
1781
        break;
1782
    case POWERPC_MMU_601:
1783
        /* XXX: TODO */
1784
        cpu_abort(env, "MMU model not implemented\n");
1785
        break;
1786
    case POWERPC_MMU_32B:
1787
        /* tlbie invalidate TLBs for all segments */
1788
        addr &= ~((target_ulong)-1 << 28);
1789
        /* XXX: this case should be optimized,
1790
         * giving a mask to tlb_flush_page
1791
         */
1792
        tlb_flush_page(env, addr | (0x0 << 28));
1793
        tlb_flush_page(env, addr | (0x1 << 28));
1794
        tlb_flush_page(env, addr | (0x2 << 28));
1795
        tlb_flush_page(env, addr | (0x3 << 28));
1796
        tlb_flush_page(env, addr | (0x4 << 28));
1797
        tlb_flush_page(env, addr | (0x5 << 28));
1798
        tlb_flush_page(env, addr | (0x6 << 28));
1799
        tlb_flush_page(env, addr | (0x7 << 28));
1800
        tlb_flush_page(env, addr | (0x8 << 28));
1801
        tlb_flush_page(env, addr | (0x9 << 28));
1802
        tlb_flush_page(env, addr | (0xA << 28));
1803
        tlb_flush_page(env, addr | (0xB << 28));
1804
        tlb_flush_page(env, addr | (0xC << 28));
1805
        tlb_flush_page(env, addr | (0xD << 28));
1806
        tlb_flush_page(env, addr | (0xE << 28));
1807
        tlb_flush_page(env, addr | (0xF << 28));
1808
        break;
1809
#if defined(TARGET_PPC64)
1810
    case POWERPC_MMU_64B:
1811
        /* tlbie invalidate TLBs for all segments */
1812
        /* XXX: given the fact that there are too many segments to invalidate,
1813
         *      and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
1814
         *      we just invalidate all TLBs
1815
         */
1816
        tlb_flush(env, 1);
1817
        break;
1818
#endif /* defined(TARGET_PPC64) */
1819
    default:
1820
        /* XXX: TODO */
1821
        cpu_abort(env, "Unknown MMU model\n");
1822
        break;
1823
    }
1824
#else
1825
    ppc_tlb_invalidate_all(env);
1826
#endif
1827
}
1828

    
1829
#if defined(TARGET_PPC64)
1830
void ppc_slb_invalidate_all (CPUPPCState *env)
1831
{
1832
    /* XXX: TODO */
1833
    tlb_flush(env, 1);
1834
}
1835

    
1836
void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
1837
{
1838
    /* XXX: TODO */
1839
    tlb_flush(env, 1);
1840
}
1841
#endif
1842

    
1843

    
1844
/*****************************************************************************/
1845
/* Special registers manipulation */
1846
#if defined(TARGET_PPC64)
1847
target_ulong ppc_load_asr (CPUPPCState *env)
1848
{
1849
    return env->asr;
1850
}
1851

    
1852
void ppc_store_asr (CPUPPCState *env, target_ulong value)
1853
{
1854
    if (env->asr != value) {
1855
        env->asr = value;
1856
        tlb_flush(env, 1);
1857
    }
1858
}
1859
#endif
1860

    
1861
target_ulong do_load_sdr1 (CPUPPCState *env)
1862
{
1863
    return env->sdr1;
1864
}
1865

    
1866
void do_store_sdr1 (CPUPPCState *env, target_ulong value)
1867
{
1868
#if defined (DEBUG_MMU)
1869
    if (loglevel != 0) {
1870
        fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
1871
    }
1872
#endif
1873
    if (env->sdr1 != value) {
1874
        /* XXX: for PowerPC 64, should check that the HTABSIZE value
1875
         *      is <= 28
1876
         */
1877
        env->sdr1 = value;
1878
        tlb_flush(env, 1);
1879
    }
1880
}
1881

    
1882
#if 0 // Unused
1883
target_ulong do_load_sr (CPUPPCState *env, int srnum)
1884
{
1885
    return env->sr[srnum];
1886
}
1887
#endif
1888

    
1889
void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
1890
{
1891
#if defined (DEBUG_MMU)
1892
    if (loglevel != 0) {
1893
        fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
1894
                __func__, srnum, value, env->sr[srnum]);
1895
    }
1896
#endif
1897
    if (env->sr[srnum] != value) {
1898
        env->sr[srnum] = value;
1899
#if !defined(FLUSH_ALL_TLBS) && 0
1900
        {
1901
            target_ulong page, end;
1902
            /* Invalidate 256 MB of virtual memory */
1903
            page = (16 << 20) * srnum;
1904
            end = page + (16 << 20);
1905
            for (; page != end; page += TARGET_PAGE_SIZE)
1906
                tlb_flush_page(env, page);
1907
        }
1908
#else
1909
        tlb_flush(env, 1);
1910
#endif
1911
    }
1912
}
1913
#endif /* !defined (CONFIG_USER_ONLY) */
1914

    
1915
target_ulong ppc_load_xer (CPUPPCState *env)
1916
{
1917
    return (xer_so << XER_SO) |
1918
        (xer_ov << XER_OV) |
1919
        (xer_ca << XER_CA) |
1920
        (xer_bc << XER_BC) |
1921
        (xer_cmp << XER_CMP);
1922
}
1923

    
1924
void ppc_store_xer (CPUPPCState *env, target_ulong value)
1925
{
1926
    xer_so = (value >> XER_SO) & 0x01;
1927
    xer_ov = (value >> XER_OV) & 0x01;
1928
    xer_ca = (value >> XER_CA) & 0x01;
1929
    xer_cmp = (value >> XER_CMP) & 0xFF;
1930
    xer_bc = (value >> XER_BC) & 0x7F;
1931
}
1932

    
1933
/* Swap temporary saved registers with GPRs */
1934
static inline void swap_gpr_tgpr (CPUPPCState *env)
1935
{
1936
    ppc_gpr_t tmp;
1937

    
1938
    tmp = env->gpr[0];
1939
    env->gpr[0] = env->tgpr[0];
1940
    env->tgpr[0] = tmp;
1941
    tmp = env->gpr[1];
1942
    env->gpr[1] = env->tgpr[1];
1943
    env->tgpr[1] = tmp;
1944
    tmp = env->gpr[2];
1945
    env->gpr[2] = env->tgpr[2];
1946
    env->tgpr[2] = tmp;
1947
    tmp = env->gpr[3];
1948
    env->gpr[3] = env->tgpr[3];
1949
    env->tgpr[3] = tmp;
1950
}
1951

    
1952
/* GDBstub can read and write MSR... */
1953
target_ulong do_load_msr (CPUPPCState *env)
1954
{
1955
    return
1956
#if defined (TARGET_PPC64)
1957
        ((target_ulong)msr_sf   << MSR_SF)   |
1958
        ((target_ulong)msr_isf  << MSR_ISF)  |
1959
        ((target_ulong)msr_hv   << MSR_HV)   |
1960
#endif
1961
        ((target_ulong)msr_ucle << MSR_UCLE) |
1962
        ((target_ulong)msr_vr   << MSR_VR)   | /* VR / SPE */
1963
        ((target_ulong)msr_ap   << MSR_AP)   |
1964
        ((target_ulong)msr_sa   << MSR_SA)   |
1965
        ((target_ulong)msr_key  << MSR_KEY)  |
1966
        ((target_ulong)msr_pow  << MSR_POW)  | /* POW / WE */
1967
        ((target_ulong)msr_tlb  << MSR_TLB)  | /* TLB / TGPE / CE */
1968
        ((target_ulong)msr_ile  << MSR_ILE)  |
1969
        ((target_ulong)msr_ee   << MSR_EE)   |
1970
        ((target_ulong)msr_pr   << MSR_PR)   |
1971
        ((target_ulong)msr_fp   << MSR_FP)   |
1972
        ((target_ulong)msr_me   << MSR_ME)   |
1973
        ((target_ulong)msr_fe0  << MSR_FE0)  |
1974
        ((target_ulong)msr_se   << MSR_SE)   | /* SE / DWE / UBLE */
1975
        ((target_ulong)msr_be   << MSR_BE)   | /* BE / DE */
1976
        ((target_ulong)msr_fe1  << MSR_FE1)  |
1977
        ((target_ulong)msr_al   << MSR_AL)   |
1978
        ((target_ulong)msr_ip   << MSR_IP)   |
1979
        ((target_ulong)msr_ir   << MSR_IR)   | /* IR / IS */
1980
        ((target_ulong)msr_dr   << MSR_DR)   | /* DR / DS */
1981
        ((target_ulong)msr_pe   << MSR_PE)   | /* PE / EP */
1982
        ((target_ulong)msr_px   << MSR_PX)   | /* PX / PMM */
1983
        ((target_ulong)msr_ri   << MSR_RI)   |
1984
        ((target_ulong)msr_le   << MSR_LE);
1985
}
1986

    
1987
int do_store_msr (CPUPPCState *env, target_ulong value)
1988
{
1989
    int enter_pm;
1990

    
1991
    value &= env->msr_mask;
1992
    if (((value >> MSR_IR) & 1) != msr_ir ||
1993
        ((value >> MSR_DR) & 1) != msr_dr) {
1994
        /* Flush all tlb when changing translation mode */
1995
        tlb_flush(env, 1);
1996
        env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1997
    }
1998
#if 0
1999
    if (loglevel != 0) {
2000
        fprintf(logfile, "%s: T0 %08lx\n", __func__, value);
2001
    }
2002
#endif
2003
    switch (env->excp_model) {
2004
    case POWERPC_EXCP_602:
2005
    case POWERPC_EXCP_603:
2006
    case POWERPC_EXCP_603E:
2007
    case POWERPC_EXCP_G2:
2008
        if (((value >> MSR_TGPR) & 1) != msr_tgpr) {
2009
            /* Swap temporary saved registers with GPRs */
2010
            swap_gpr_tgpr(env);
2011
        }
2012
        break;
2013
    default:
2014
        break;
2015
    }
2016
#if defined (TARGET_PPC64)
2017
    msr_sf   = (value >> MSR_SF)   & 1;
2018
    msr_isf  = (value >> MSR_ISF)  & 1;
2019
    msr_hv   = (value >> MSR_HV)   & 1;
2020
#endif
2021
    msr_ucle = (value >> MSR_UCLE) & 1;
2022
    msr_vr   = (value >> MSR_VR)   & 1; /* VR / SPE */
2023
    msr_ap   = (value >> MSR_AP)   & 1;
2024
    msr_sa   = (value >> MSR_SA)   & 1;
2025
    msr_key  = (value >> MSR_KEY)  & 1;
2026
    msr_pow  = (value >> MSR_POW)  & 1; /* POW / WE */
2027
    msr_tlb  = (value >> MSR_TLB)  & 1; /* TLB / TGPR / CE */
2028
    msr_ile  = (value >> MSR_ILE)  & 1;
2029
    msr_ee   = (value >> MSR_EE)   & 1;
2030
    msr_pr   = (value >> MSR_PR)   & 1;
2031
    msr_fp   = (value >> MSR_FP)   & 1;
2032
    msr_me   = (value >> MSR_ME)   & 1;
2033
    msr_fe0  = (value >> MSR_FE0)  & 1;
2034
    msr_se   = (value >> MSR_SE)   & 1; /* SE / DWE / UBLE */
2035
    msr_be   = (value >> MSR_BE)   & 1; /* BE / DE */
2036
    msr_fe1  = (value >> MSR_FE1)  & 1;
2037
    msr_al   = (value >> MSR_AL)   & 1;
2038
    msr_ip   = (value >> MSR_IP)   & 1;
2039
    msr_ir   = (value >> MSR_IR)   & 1; /* IR / IS */
2040
    msr_dr   = (value >> MSR_DR)   & 1; /* DR / DS */
2041
    msr_pe   = (value >> MSR_PE)   & 1; /* PE / EP */
2042
    msr_px   = (value >> MSR_PX)   & 1; /* PX / PMM */
2043
    msr_ri   = (value >> MSR_RI)   & 1;
2044
    msr_le   = (value >> MSR_LE)   & 1;
2045
    do_compute_hflags(env);
2046

    
2047
    enter_pm = 0;
2048
    switch (env->excp_model) {
2049
    case POWERPC_EXCP_603:
2050
    case POWERPC_EXCP_603E:
2051
    case POWERPC_EXCP_G2:
2052
        /* Don't handle SLEEP mode: we should disable all clocks...
2053
         * No dynamic power-management.
2054
         */
2055
        if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00C00000) != 0)
2056
            enter_pm = 1;
2057
        break;
2058
    case POWERPC_EXCP_604:
2059
        if (msr_pow == 1)
2060
            enter_pm = 1;
2061
        break;
2062
    case POWERPC_EXCP_7x0:
2063
        if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00E00000) != 0)
2064
            enter_pm = 1;
2065
        break;
2066
    default:
2067
        break;
2068
    }
2069

    
2070
    return enter_pm;
2071
}
2072

    
2073
#if defined(TARGET_PPC64)
2074
int ppc_store_msr_32 (CPUPPCState *env, uint32_t value)
2075
{
2076
    return do_store_msr(env, (do_load_msr(env) & ~0xFFFFFFFFULL) |
2077
                        (value & 0xFFFFFFFF));
2078
}
2079
#endif
2080

    
2081
void do_compute_hflags (CPUPPCState *env)
2082
{
2083
    /* Compute current hflags */
2084
    env->hflags = (msr_vr << MSR_VR) |
2085
        (msr_ap << MSR_AP) | (msr_sa << MSR_SA) | (msr_pr << MSR_PR) |
2086
        (msr_fp << MSR_FP) | (msr_fe0 << MSR_FE0) | (msr_se << MSR_SE) |
2087
        (msr_be << MSR_BE) | (msr_fe1 << MSR_FE1) | (msr_le << MSR_LE);
2088
#if defined (TARGET_PPC64)
2089
    env->hflags |= msr_cm << MSR_CM;
2090
    env->hflags |= (uint64_t)msr_sf << MSR_SF;
2091
    env->hflags |= (uint64_t)msr_hv << MSR_HV;
2092
#endif
2093
}
2094

    
2095
/*****************************************************************************/
2096
/* Exception processing */
2097
#if defined (CONFIG_USER_ONLY)
2098
void do_interrupt (CPUState *env)
2099
{
2100
    env->exception_index = POWERPC_EXCP_NONE;
2101
    env->error_code = 0;
2102
}
2103

    
2104
void ppc_hw_interrupt (CPUState *env)
2105
{
2106
    env->exception_index = POWERPC_EXCP_NONE;
2107
    env->error_code = 0;
2108
}
2109
#else /* defined (CONFIG_USER_ONLY) */
2110
static void dump_syscall (CPUState *env)
2111
{
2112
    fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
2113
            " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
2114
            env->gpr[0], env->gpr[3], env->gpr[4],
2115
            env->gpr[5], env->gpr[6], env->nip);
2116
}
2117

    
2118
/* Note that this function should be greatly optimized
2119
 * when called with a constant excp, from ppc_hw_interrupt
2120
 */
2121
static always_inline void powerpc_excp (CPUState *env,
2122
                                        int excp_model, int excp)
2123
{
2124
    target_ulong msr, vector;
2125
    int srr0, srr1, asrr0, asrr1;
2126

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

    
2729
void do_interrupt (CPUState *env)
2730
{
2731
    powerpc_excp(env, env->excp_model, env->exception_index);
2732
}
2733

    
2734
void ppc_hw_interrupt (CPUPPCState *env)
2735
{
2736
#if 1
2737
    if (loglevel & CPU_LOG_INT) {
2738
        fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2739
                __func__, env, env->pending_interrupts,
2740
                env->interrupt_request, msr_me, msr_ee);
2741
    }
2742
#endif
2743
    /* External reset */
2744
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2745
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2746
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2747
        return;
2748
    }
2749
    /* Machine check exception */
2750
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2751
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2752
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2753
        return;
2754
    }
2755
#if 0 /* TODO */
2756
    /* External debug exception */
2757
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2758
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2759
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2760
        return;
2761
    }
2762
#endif
2763
#if defined(TARGET_PPC64H)
2764
    if ((msr_ee != 0 || msr_hv == 0 || msr_pr == 1) & hdice != 0) {
2765
        /* Hypervisor decrementer exception */
2766
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2767
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2768
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2769
            return;
2770
        }
2771
    }
2772
#endif
2773
    if (msr_ce != 0) {
2774
        /* External critical interrupt */
2775
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2776
            /* Taking a critical external interrupt does not clear the external
2777
             * critical interrupt status
2778
             */
2779
#if 0
2780
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2781
#endif
2782
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2783
            return;
2784
        }
2785
    }
2786
    if (msr_ee != 0) {
2787
        /* Watchdog timer on embedded PowerPC */
2788
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2789
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2790
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2791
            return;
2792
        }
2793
#if defined(TARGET_PPCEMB)
2794
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2795
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2796
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2797
            return;
2798
        }
2799
#endif
2800
#if defined(TARGET_PPCEMB)
2801
        /* External interrupt */
2802
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2803
            /* Taking an external interrupt does not clear the external
2804
             * interrupt status
2805
             */
2806
#if 0
2807
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2808
#endif
2809
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2810
            return;
2811
        }
2812
#endif
2813
        /* Fixed interval timer on embedded PowerPC */
2814
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2815
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2816
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2817
            return;
2818
        }
2819
        /* Programmable interval timer on embedded PowerPC */
2820
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2821
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2822
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2823
            return;
2824
        }
2825
        /* Decrementer exception */
2826
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2827
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2828
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2829
            return;
2830
        }
2831
#if !defined(TARGET_PPCEMB)
2832
        /* External interrupt */
2833
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2834
            /* Taking an external interrupt does not clear the external
2835
             * interrupt status
2836
             */
2837
#if 0
2838
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2839
#endif
2840
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2841
            return;
2842
        }
2843
#endif
2844
#if defined(TARGET_PPCEMB)
2845
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2846
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2847
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2848
            return;
2849
        }
2850
#endif
2851
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2852
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2853
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2854
            return;
2855
        }
2856
        /* Thermal interrupt */
2857
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2858
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2859
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2860
            return;
2861
        }
2862
    }
2863
}
2864
#endif /* !CONFIG_USER_ONLY */
2865

    
2866
void cpu_dump_EA (target_ulong EA)
2867
{
2868
    FILE *f;
2869

    
2870
    if (logfile) {
2871
        f = logfile;
2872
    } else {
2873
        f = stdout;
2874
        return;
2875
    }
2876
    fprintf(f, "Memory access at address " ADDRX "\n", EA);
2877
}
2878

    
2879
void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2880
{
2881
    FILE *f;
2882

    
2883
    if (logfile) {
2884
        f = logfile;
2885
    } else {
2886
        f = stdout;
2887
        return;
2888
    }
2889
    fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2890
            RA, msr);
2891
}
2892

    
2893
void cpu_ppc_reset (void *opaque)
2894
{
2895
    CPUPPCState *env;
2896
    int i;
2897

    
2898
    env = opaque;
2899
    /* XXX: some of those flags initialisation values could depend
2900
     *      on the actual PowerPC implementation
2901
     */
2902
    for (i = 0; i < 63; i++)
2903
        env->msr[i] = 0;
2904
#if defined(TARGET_PPC64)
2905
    msr_hv = 0; /* Should be 1... */
2906
#endif
2907
    msr_ap = 0; /* TO BE CHECKED */
2908
    msr_sa = 0; /* TO BE CHECKED */
2909
    msr_ip = 0; /* TO BE CHECKED */
2910
#if defined (DO_SINGLE_STEP) && 0
2911
    /* Single step trace mode */
2912
    msr_se = 1;
2913
    msr_be = 1;
2914
#endif
2915
#if defined(CONFIG_USER_ONLY)
2916
    msr_fp = 1; /* Allow floating point exceptions */
2917
    msr_pr = 1;
2918
#else
2919
    env->nip = env->hreset_vector | env->excp_prefix;
2920
    ppc_tlb_invalidate_all(env);
2921
#endif
2922
    do_compute_hflags(env);
2923
    env->reserve = -1;
2924
    /* Be sure no exception or interrupt is pending */
2925
    env->pending_interrupts = 0;
2926
    env->exception_index = POWERPC_EXCP_NONE;
2927
    env->error_code = 0;
2928
    /* Flush all TLBs */
2929
    tlb_flush(env, 1);
2930
}
2931

    
2932
CPUPPCState *cpu_ppc_init (void)
2933
{
2934
    CPUPPCState *env;
2935

    
2936
    env = qemu_mallocz(sizeof(CPUPPCState));
2937
    if (!env)
2938
        return NULL;
2939
    cpu_exec_init(env);
2940

    
2941
    return env;
2942
}
2943

    
2944
void cpu_ppc_close (CPUPPCState *env)
2945
{
2946
    /* Should also remove all opcode tables... */
2947
    free(env);
2948
}