Statistics
| Branch: | Revision:

root / target-ppc / helper.c @ 2c1ee068

History | View | Annotate | Download (90.5 kB)

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

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

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

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

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

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

    
62
    return 1;
63
}
64

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

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

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

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

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

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

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

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

    
138
    return access;
139
}
140

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

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

    
162
    return ret;
163
}
164

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

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

    
232
    return ret;
233
}
234

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

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

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

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

    
271
    return store;
272
}
273

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

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

    
288
    return nr;
289
}
290

    
291
static void ppc6xx_tlb_invalidate_all (CPUState *env)
292
{
293
    ppc6xx_tlb_t *tlb;
294
    int nr, max;
295

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

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

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

    
342
static void ppc6xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
343
                                        int is_code)
344
{
345
    __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
346
}
347

    
348
void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
349
                       target_ulong pte0, target_ulong pte1)
350
{
351
    ppc6xx_tlb_t *tlb;
352
    int nr;
353

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

    
371
static int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
372
                             target_ulong eaddr, int rw, int access_type)
373
{
374
    ppc6xx_tlb_t *tlb;
375
    int nr, best, way;
376
    int ret;
377

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

    
443
    return ret;
444
}
445

    
446
/* Perform BAT hit & translation */
447
static int get_bat (CPUState *env, mmu_ctx_t *ctx,
448
                    target_ulong virtual, int rw, int type)
449
{
450
    target_ulong *BATlt, *BATut, *BATu, *BATl;
451
    target_ulong base, BEPIl, BEPIu, bl;
452
    int i, pp, pr;
453
    int ret = -1;
454

    
455
#if defined (DEBUG_BATS)
456
    if (loglevel != 0) {
457
        fprintf(logfile, "%s: %cBAT v 0x" ADDRX "\n", __func__,
458
                type == ACCESS_CODE ? 'I' : 'D', virtual);
459
    }
460
#endif
461
    pr = msr_pr;
462
    switch (type) {
463
    case ACCESS_CODE:
464
        BATlt = env->IBAT[1];
465
        BATut = env->IBAT[0];
466
        break;
467
    default:
468
        BATlt = env->DBAT[1];
469
        BATut = env->DBAT[0];
470
        break;
471
    }
472
#if defined (DEBUG_BATS)
473
    if (loglevel != 0) {
474
        fprintf(logfile, "%s...: %cBAT v 0x" ADDRX "\n", __func__,
475
                type == ACCESS_CODE ? 'I' : 'D', virtual);
476
    }
477
#endif
478
    base = virtual & 0xFFFC0000;
479
    for (i = 0; i < 4; i++) {
480
        BATu = &BATut[i];
481
        BATl = &BATlt[i];
482
        BEPIu = *BATu & 0xF0000000;
483
        BEPIl = *BATu & 0x0FFE0000;
484
        bl = (*BATu & 0x00001FFC) << 15;
485
#if defined (DEBUG_BATS)
486
        if (loglevel != 0) {
487
            fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
488
                    " BATl 0x" ADDRX "\n",
489
                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
490
                    *BATu, *BATl);
491
        }
492
#endif
493
        if ((virtual & 0xF0000000) == BEPIu &&
494
            ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
495
            /* BAT matches */
496
            if (((pr == 0) && (*BATu & 0x00000002)) ||
497
                ((pr != 0) && (*BATu & 0x00000001))) {
498
                /* Get physical address */
499
                ctx->raddr = (*BATl & 0xF0000000) |
500
                    ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
501
                    (virtual & 0x0001F000);
502
                /* Compute access rights */
503
                pp = *BATl & 0x00000003;
504
                ctx->prot = 0;
505
                if (pp != 0) {
506
                    ctx->prot = PAGE_READ | PAGE_EXEC;
507
                    if (pp == 0x2)
508
                        ctx->prot |= PAGE_WRITE;
509
                }
510
                ret = check_prot(ctx->prot, rw, type);
511
#if defined (DEBUG_BATS)
512
                if (ret == 0 && loglevel != 0) {
513
                    fprintf(logfile, "BAT %d match: r 0x" PADDRX
514
                            " prot=%c%c\n",
515
                            i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
516
                            ctx->prot & PAGE_WRITE ? 'W' : '-');
517
                }
518
#endif
519
                break;
520
            }
521
        }
522
    }
523
    if (ret < 0) {
524
#if defined (DEBUG_BATS)
525
        if (loglevel != 0) {
526
            fprintf(logfile, "no BAT match for 0x" ADDRX ":\n", virtual);
527
            for (i = 0; i < 4; i++) {
528
                BATu = &BATut[i];
529
                BATl = &BATlt[i];
530
                BEPIu = *BATu & 0xF0000000;
531
                BEPIl = *BATu & 0x0FFE0000;
532
                bl = (*BATu & 0x00001FFC) << 15;
533
                fprintf(logfile, "%s: %cBAT%d v 0x" ADDRX " BATu 0x" ADDRX
534
                        " BATl 0x" ADDRX " \n\t"
535
                        "0x" ADDRX " 0x" ADDRX " 0x" ADDRX "\n",
536
                        __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
537
                        *BATu, *BATl, BEPIu, BEPIl, bl);
538
            }
539
        }
540
#endif
541
    }
542

    
543
    /* No hit */
544
    return ret;
545
}
546

    
547
/* PTE table lookup */
548
static always_inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h,
549
                                    int rw, int type)
550
{
551
    target_ulong base, pte0, pte1;
552
    int i, good = -1;
553
    int ret, r;
554

    
555
    ret = -1; /* No entry found */
556
    base = ctx->pg_addr[h];
557
    for (i = 0; i < 8; i++) {
558
#if defined(TARGET_PPC64)
559
        if (is_64b) {
560
            pte0 = ldq_phys(base + (i * 16));
561
            pte1 =  ldq_phys(base + (i * 16) + 8);
562
            r = pte64_check(ctx, pte0, pte1, h, rw, type);
563
#if defined (DEBUG_MMU)
564
            if (loglevel != 0) {
565
                fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
566
                        " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
567
                        base + (i * 16), pte0, pte1,
568
                        (int)(pte0 & 1), h, (int)((pte0 >> 1) & 1),
569
                        ctx->ptem);
570
            }
571
#endif
572
        } else
573
#endif
574
        {
575
            pte0 = ldl_phys(base + (i * 8));
576
            pte1 =  ldl_phys(base + (i * 8) + 4);
577
            r = pte32_check(ctx, pte0, pte1, h, rw, type);
578
#if defined (DEBUG_MMU)
579
            if (loglevel != 0) {
580
                fprintf(logfile, "Load pte from 0x" ADDRX " => 0x" ADDRX
581
                        " 0x" ADDRX " %d %d %d 0x" ADDRX "\n",
582
                        base + (i * 8), pte0, pte1,
583
                        (int)(pte0 >> 31), h, (int)((pte0 >> 6) & 1),
584
                        ctx->ptem);
585
            }
586
#endif
587
        }
588
        switch (r) {
589
        case -3:
590
            /* PTE inconsistency */
591
            return -1;
592
        case -2:
593
            /* Access violation */
594
            ret = -2;
595
            good = i;
596
            break;
597
        case -1:
598
        default:
599
            /* No PTE match */
600
            break;
601
        case 0:
602
            /* access granted */
603
            /* XXX: we should go on looping to check all PTEs consistency
604
             *      but if we can speed-up the whole thing as the
605
             *      result would be undefined if PTEs are not consistent.
606
             */
607
            ret = 0;
608
            good = i;
609
            goto done;
610
        }
611
    }
612
    if (good != -1) {
613
    done:
614
#if defined (DEBUG_MMU)
615
        if (loglevel != 0) {
616
            fprintf(logfile, "found PTE at addr 0x" PADDRX " prot=0x%01x "
617
                    "ret=%d\n",
618
                    ctx->raddr, ctx->prot, ret);
619
        }
620
#endif
621
        /* Update page flags */
622
        pte1 = ctx->raddr;
623
        if (pte_update_flags(ctx, &pte1, ret, rw) == 1) {
624
#if defined(TARGET_PPC64)
625
            if (is_64b) {
626
                stq_phys_notdirty(base + (good * 16) + 8, pte1);
627
            } else
628
#endif
629
            {
630
                stl_phys_notdirty(base + (good * 8) + 4, pte1);
631
            }
632
        }
633
    }
634

    
635
    return ret;
636
}
637

    
638
static int find_pte32 (mmu_ctx_t *ctx, int h, int rw, int type)
639
{
640
    return _find_pte(ctx, 0, h, rw, type);
641
}
642

    
643
#if defined(TARGET_PPC64)
644
static int find_pte64 (mmu_ctx_t *ctx, int h, int rw, int type)
645
{
646
    return _find_pte(ctx, 1, h, rw, type);
647
}
648
#endif
649

    
650
static always_inline int find_pte (CPUState *env, mmu_ctx_t *ctx,
651
                                   int h, int rw, int type)
652
{
653
#if defined(TARGET_PPC64)
654
    if (env->mmu_model == POWERPC_MMU_64B)
655
        return find_pte64(ctx, h, rw, type);
656
#endif
657

    
658
    return find_pte32(ctx, h, rw, type);
659
}
660

    
661
#if defined(TARGET_PPC64)
662
static inline int slb_is_valid (uint64_t slb64)
663
{
664
    return slb64 & 0x0000000008000000ULL ? 1 : 0;
665
}
666

    
667
static inline void slb_invalidate (uint64_t *slb64)
668
{
669
    *slb64 &= ~0x0000000008000000ULL;
670
}
671

    
672
static int slb_lookup (CPUPPCState *env, target_ulong eaddr,
673
                       target_ulong *vsid, target_ulong *page_mask, int *attr)
674
{
675
    target_phys_addr_t sr_base;
676
    target_ulong mask;
677
    uint64_t tmp64;
678
    uint32_t tmp;
679
    int n, ret;
680

    
681
    ret = -5;
682
    sr_base = env->spr[SPR_ASR];
683
#if defined(DEBUG_SLB)
684
    if (loglevel != 0) {
685
        fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
686
                __func__, eaddr, sr_base);
687
    }
688
#endif
689
    mask = 0x0000000000000000ULL; /* Avoid gcc warning */
690
    for (n = 0; n < env->slb_nr; n++) {
691
        tmp64 = ldq_phys(sr_base);
692
        tmp = ldl_phys(sr_base + 8);
693
#if defined(DEBUG_SLB)
694
        if (loglevel != 0) {
695
            fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08"
696
                    PRIx32 "\n", __func__, n, sr_base, tmp64, tmp);
697
        }
698
#endif
699
        if (slb_is_valid(tmp64)) {
700
            /* SLB entry is valid */
701
            switch (tmp64 & 0x0000000006000000ULL) {
702
            case 0x0000000000000000ULL:
703
                /* 256 MB segment */
704
                mask = 0xFFFFFFFFF0000000ULL;
705
                break;
706
            case 0x0000000002000000ULL:
707
                /* 1 TB segment */
708
                mask = 0xFFFF000000000000ULL;
709
                break;
710
            case 0x0000000004000000ULL:
711
            case 0x0000000006000000ULL:
712
                /* Reserved => segment is invalid */
713
                continue;
714
            }
715
            if ((eaddr & mask) == (tmp64 & mask)) {
716
                /* SLB match */
717
                *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
718
                *page_mask = ~mask;
719
                *attr = tmp & 0xFF;
720
                ret = n;
721
                break;
722
            }
723
        }
724
        sr_base += 12;
725
    }
726

    
727
    return ret;
728
}
729

    
730
void ppc_slb_invalidate_all (CPUPPCState *env)
731
{
732
    target_phys_addr_t sr_base;
733
    uint64_t tmp64;
734
    int n, do_invalidate;
735

    
736
    do_invalidate = 0;
737
    sr_base = env->spr[SPR_ASR];
738
    /* XXX: Warning: slbia never invalidates the first segment */
739
    for (n = 1; n < env->slb_nr; n++) {
740
        tmp64 = ldq_phys(sr_base);
741
        if (slb_is_valid(tmp64)) {
742
            slb_invalidate(&tmp64);
743
            stq_phys(sr_base, tmp64);
744
            /* XXX: given the fact that segment size is 256 MB or 1TB,
745
             *      and we still don't have a tlb_flush_mask(env, n, mask)
746
             *      in Qemu, we just invalidate all TLBs
747
             */
748
            do_invalidate = 1;
749
        }
750
        sr_base += 12;
751
    }
752
    if (do_invalidate)
753
        tlb_flush(env, 1);
754
}
755

    
756
void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
757
{
758
    target_phys_addr_t sr_base;
759
    target_ulong vsid, page_mask;
760
    uint64_t tmp64;
761
    int attr;
762
    int n;
763

    
764
    n = slb_lookup(env, T0, &vsid, &page_mask, &attr);
765
    if (n >= 0) {
766
        sr_base = env->spr[SPR_ASR];
767
        sr_base += 12 * n;
768
        tmp64 = ldq_phys(sr_base);
769
        if (slb_is_valid(tmp64)) {
770
            slb_invalidate(&tmp64);
771
            stq_phys(sr_base, tmp64);
772
            /* XXX: given the fact that segment size is 256 MB or 1TB,
773
             *      and we still don't have a tlb_flush_mask(env, n, mask)
774
             *      in Qemu, we just invalidate all TLBs
775
             */
776
            tlb_flush(env, 1);
777
        }
778
    }
779
}
780

    
781
target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
782
{
783
    target_phys_addr_t sr_base;
784
    target_ulong rt;
785
    uint64_t tmp64;
786
    uint32_t tmp;
787

    
788
    sr_base = env->spr[SPR_ASR];
789
    sr_base += 12 * slb_nr;
790
    tmp64 = ldq_phys(sr_base);
791
    tmp = ldl_phys(sr_base + 8);
792
    if (tmp64 & 0x0000000008000000ULL) {
793
        /* SLB entry is valid */
794
        /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
795
        rt = tmp >> 8;             /* 65:88 => 40:63 */
796
        rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
797
        /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
798
        rt |= ((tmp >> 4) & 0xF) << 27;
799
    } else {
800
        rt = 0;
801
    }
802
#if defined(DEBUG_SLB)
803
    if (loglevel != 0) {
804
        fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
805
                ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
806
    }
807
#endif
808

    
809
    return rt;
810
}
811

    
812
void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
813
{
814
    target_phys_addr_t sr_base;
815
    uint64_t tmp64;
816
    uint32_t tmp;
817

    
818
    sr_base = env->spr[SPR_ASR];
819
    sr_base += 12 * slb_nr;
820
    /* Copy Rs bits 37:63 to SLB 62:88 */
821
    tmp = rs << 8;
822
    tmp64 = (rs >> 24) & 0x7;
823
    /* Copy Rs bits 33:36 to SLB 89:92 */
824
    tmp |= ((rs >> 27) & 0xF) << 4;
825
    /* Set the valid bit */
826
    tmp64 |= 1 << 27;
827
    /* Set ESID */
828
    tmp64 |= (uint32_t)slb_nr << 28;
829
#if defined(DEBUG_SLB)
830
    if (loglevel != 0) {
831
        fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64 " %08"
832
                PRIx32 "\n", __func__, slb_nr, rs, sr_base, tmp64, tmp);
833
    }
834
#endif
835
    /* Write SLB entry to memory */
836
    stq_phys(sr_base, tmp64);
837
    stl_phys(sr_base + 8, tmp);
838
}
839
#endif /* defined(TARGET_PPC64) */
840

    
841
/* Perform segment based translation */
842
static always_inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
843
                                                    int sdr_sh,
844
                                                    target_phys_addr_t hash,
845
                                                    target_phys_addr_t mask)
846
{
847
    return (sdr1 & ((target_ulong)(-1ULL) << sdr_sh)) | (hash & mask);
848
}
849

    
850
static int get_segment (CPUState *env, mmu_ctx_t *ctx,
851
                        target_ulong eaddr, int rw, int type)
852
{
853
    target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
854
    target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
855
#if defined(TARGET_PPC64)
856
    int attr;
857
#endif
858
    int ds, vsid_sh, sdr_sh, pr;
859
    int ret, ret2;
860

    
861
    pr = msr_pr;
862
#if defined(TARGET_PPC64)
863
    if (env->mmu_model == POWERPC_MMU_64B) {
864
#if defined (DEBUG_MMU)
865
        if (loglevel != 0) {
866
            fprintf(logfile, "Check SLBs\n");
867
        }
868
#endif
869
        ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
870
        if (ret < 0)
871
            return ret;
872
        ctx->key = ((attr & 0x40) && (pr != 0)) ||
873
            ((attr & 0x80) && (pr == 0)) ? 1 : 0;
874
        ds = 0;
875
        ctx->nx = attr & 0x20 ? 1 : 0;
876
        vsid_mask = 0x00003FFFFFFFFF80ULL;
877
        vsid_sh = 7;
878
        sdr_sh = 18;
879
        sdr_mask = 0x3FF80;
880
    } else
881
#endif /* defined(TARGET_PPC64) */
882
    {
883
        sr = env->sr[eaddr >> 28];
884
        page_mask = 0x0FFFFFFF;
885
        ctx->key = (((sr & 0x20000000) && (pr != 0)) ||
886
                    ((sr & 0x40000000) && (pr == 0))) ? 1 : 0;
887
        ds = sr & 0x80000000 ? 1 : 0;
888
        ctx->nx = sr & 0x10000000 ? 1 : 0;
889
        vsid = sr & 0x00FFFFFF;
890
        vsid_mask = 0x01FFFFC0;
891
        vsid_sh = 6;
892
        sdr_sh = 16;
893
        sdr_mask = 0xFFC0;
894
#if defined (DEBUG_MMU)
895
        if (loglevel != 0) {
896
            fprintf(logfile, "Check segment v=0x" ADDRX " %d 0x" ADDRX
897
                    " nip=0x" ADDRX " lr=0x" ADDRX
898
                    " ir=%d dr=%d pr=%d %d t=%d\n",
899
                    eaddr, (int)(eaddr >> 28), sr, env->nip,
900
                    env->lr, (int)msr_ir, (int)msr_dr, pr != 0 ? 1 : 0,
901
                    rw, type);
902
        }
903
#endif
904
    }
905
#if defined (DEBUG_MMU)
906
    if (loglevel != 0) {
907
        fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
908
                ctx->key, ds, ctx->nx, vsid);
909
    }
910
#endif
911
    ret = -1;
912
    if (!ds) {
913
        /* Check if instruction fetch is allowed, if needed */
914
        if (type != ACCESS_CODE || ctx->nx == 0) {
915
            /* Page address translation */
916
            /* Primary table address */
917
            sdr = env->sdr1;
918
            pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
919
#if defined(TARGET_PPC64)
920
            if (env->mmu_model == POWERPC_MMU_64B) {
921
                htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
922
                /* XXX: this is false for 1 TB segments */
923
                hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
924
            } else
925
#endif
926
            {
927
                htab_mask = sdr & 0x000001FF;
928
                hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
929
            }
930
            mask = (htab_mask << sdr_sh) | sdr_mask;
931
#if defined (DEBUG_MMU)
932
            if (loglevel != 0) {
933
                fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
934
                        PADDRX " " ADDRX "\n", sdr, sdr_sh, hash, mask,
935
                        page_mask);
936
            }
937
#endif
938
            ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
939
            /* Secondary table address */
940
            hash = (~hash) & vsid_mask;
941
#if defined (DEBUG_MMU)
942
            if (loglevel != 0) {
943
                fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX " mask "
944
                        PADDRX "\n", sdr, sdr_sh, hash, mask);
945
            }
946
#endif
947
            ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
948
#if defined(TARGET_PPC64)
949
            if (env->mmu_model == POWERPC_MMU_64B) {
950
                /* Only 5 bits of the page index are used in the AVPN */
951
                ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
952
            } else
953
#endif
954
            {
955
                ctx->ptem = (vsid << 7) | (pgidx >> 10);
956
            }
957
            /* Initialize real address with an invalid value */
958
            ctx->raddr = (target_ulong)-1;
959
            if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
960
                         env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
961
                /* Software TLB search */
962
                ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
963
            } else {
964
#if defined (DEBUG_MMU)
965
                if (loglevel != 0) {
966
                    fprintf(logfile, "0 sdr1=0x" PADDRX " vsid=0x%06x "
967
                            "api=0x%04x hash=0x%07x pg_addr=0x" PADDRX "\n",
968
                            sdr, (uint32_t)vsid, (uint32_t)pgidx,
969
                            (uint32_t)hash, ctx->pg_addr[0]);
970
                }
971
#endif
972
                /* Primary table lookup */
973
                ret = find_pte(env, ctx, 0, rw, type);
974
                if (ret < 0) {
975
                    /* Secondary table lookup */
976
#if defined (DEBUG_MMU)
977
                    if (eaddr != 0xEFFFFFFF && loglevel != 0) {
978
                        fprintf(logfile,
979
                                "1 sdr1=0x" PADDRX " vsid=0x%06x api=0x%04x "
980
                                "hash=0x%05x pg_addr=0x" PADDRX "\n",
981
                                sdr, (uint32_t)vsid, (uint32_t)pgidx,
982
                                (uint32_t)hash, ctx->pg_addr[1]);
983
                    }
984
#endif
985
                    ret2 = find_pte(env, ctx, 1, rw, type);
986
                    if (ret2 != -1)
987
                        ret = ret2;
988
                }
989
            }
990
#if defined (DUMP_PAGE_TABLES)
991
            if (loglevel != 0) {
992
                target_phys_addr_t curaddr;
993
                uint32_t a0, a1, a2, a3;
994
                fprintf(logfile,
995
                        "Page table: " PADDRX " len " PADDRX "\n",
996
                        sdr, mask + 0x80);
997
                for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
998
                     curaddr += 16) {
999
                    a0 = ldl_phys(curaddr);
1000
                    a1 = ldl_phys(curaddr + 4);
1001
                    a2 = ldl_phys(curaddr + 8);
1002
                    a3 = ldl_phys(curaddr + 12);
1003
                    if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
1004
                        fprintf(logfile,
1005
                                PADDRX ": %08x %08x %08x %08x\n",
1006
                                curaddr, a0, a1, a2, a3);
1007
                    }
1008
                }
1009
            }
1010
#endif
1011
        } else {
1012
#if defined (DEBUG_MMU)
1013
            if (loglevel != 0)
1014
                fprintf(logfile, "No access allowed\n");
1015
#endif
1016
            ret = -3;
1017
        }
1018
    } else {
1019
#if defined (DEBUG_MMU)
1020
        if (loglevel != 0)
1021
            fprintf(logfile, "direct store...\n");
1022
#endif
1023
        /* Direct-store segment : absolutely *BUGGY* for now */
1024
        switch (type) {
1025
        case ACCESS_INT:
1026
            /* Integer load/store : only access allowed */
1027
            break;
1028
        case ACCESS_CODE:
1029
            /* No code fetch is allowed in direct-store areas */
1030
            return -4;
1031
        case ACCESS_FLOAT:
1032
            /* Floating point load/store */
1033
            return -4;
1034
        case ACCESS_RES:
1035
            /* lwarx, ldarx or srwcx. */
1036
            return -4;
1037
        case ACCESS_CACHE:
1038
            /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1039
            /* Should make the instruction do no-op.
1040
             * As it already do no-op, it's quite easy :-)
1041
             */
1042
            ctx->raddr = eaddr;
1043
            return 0;
1044
        case ACCESS_EXT:
1045
            /* eciwx or ecowx */
1046
            return -4;
1047
        default:
1048
            if (logfile) {
1049
                fprintf(logfile, "ERROR: instruction should not need "
1050
                        "address translation\n");
1051
            }
1052
            return -4;
1053
        }
1054
        if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
1055
            ctx->raddr = eaddr;
1056
            ret = 2;
1057
        } else {
1058
            ret = -2;
1059
        }
1060
    }
1061

    
1062
    return ret;
1063
}
1064

    
1065
/* Generic TLB check function for embedded PowerPC implementations */
1066
static int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
1067
                             target_phys_addr_t *raddrp,
1068
                             target_ulong address,
1069
                             uint32_t pid, int ext, int i)
1070
{
1071
    target_ulong mask;
1072

    
1073
    /* Check valid flag */
1074
    if (!(tlb->prot & PAGE_VALID)) {
1075
        if (loglevel != 0)
1076
            fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
1077
        return -1;
1078
    }
1079
    mask = ~(tlb->size - 1);
1080
#if defined (DEBUG_SOFTWARE_TLB)
1081
    if (loglevel != 0) {
1082
        fprintf(logfile, "%s: TLB %d address " ADDRX " PID %d <=> "
1083
                ADDRX " " ADDRX " %d\n",
1084
                __func__, i, address, pid, tlb->EPN, mask, (int)tlb->PID);
1085
    }
1086
#endif
1087
    /* Check PID */
1088
    if (tlb->PID != 0 && tlb->PID != pid)
1089
        return -1;
1090
    /* Check effective address */
1091
    if ((address & mask) != tlb->EPN)
1092
        return -1;
1093
    *raddrp = (tlb->RPN & mask) | (address & ~mask);
1094
#if (TARGET_PHYS_ADDR_BITS >= 36)
1095
    if (ext) {
1096
        /* Extend the physical address to 36 bits */
1097
        *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
1098
    }
1099
#endif
1100

    
1101
    return 0;
1102
}
1103

    
1104
/* Generic TLB search function for PowerPC embedded implementations */
1105
int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
1106
{
1107
    ppcemb_tlb_t *tlb;
1108
    target_phys_addr_t raddr;
1109
    int i, ret;
1110

    
1111
    /* Default return value is no match */
1112
    ret = -1;
1113
    for (i = 0; i < env->nb_tlb; i++) {
1114
        tlb = &env->tlb[i].tlbe;
1115
        if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
1116
            ret = i;
1117
            break;
1118
        }
1119
    }
1120

    
1121
    return ret;
1122
}
1123

    
1124
/* Helpers specific to PowerPC 40x implementations */
1125
static void ppc4xx_tlb_invalidate_all (CPUState *env)
1126
{
1127
    ppcemb_tlb_t *tlb;
1128
    int i;
1129

    
1130
    for (i = 0; i < env->nb_tlb; i++) {
1131
        tlb = &env->tlb[i].tlbe;
1132
        tlb->prot &= ~PAGE_VALID;
1133
    }
1134
    tlb_flush(env, 1);
1135
}
1136

    
1137
static void ppc4xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
1138
                                        uint32_t pid)
1139
{
1140
#if !defined(FLUSH_ALL_TLBS)
1141
    ppcemb_tlb_t *tlb;
1142
    target_phys_addr_t raddr;
1143
    target_ulong page, end;
1144
    int i;
1145

    
1146
    for (i = 0; i < env->nb_tlb; i++) {
1147
        tlb = &env->tlb[i].tlbe;
1148
        if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
1149
            end = tlb->EPN + tlb->size;
1150
            for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1151
                tlb_flush_page(env, page);
1152
            tlb->prot &= ~PAGE_VALID;
1153
            break;
1154
        }
1155
    }
1156
#else
1157
    ppc4xx_tlb_invalidate_all(env);
1158
#endif
1159
}
1160

    
1161
int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1162
                                 target_ulong address, int rw, int access_type)
1163
{
1164
    ppcemb_tlb_t *tlb;
1165
    target_phys_addr_t raddr;
1166
    int i, ret, zsel, zpr, pr;
1167

    
1168
    ret = -1;
1169
    raddr = -1;
1170
    pr = msr_pr;
1171
    for (i = 0; i < env->nb_tlb; i++) {
1172
        tlb = &env->tlb[i].tlbe;
1173
        if (ppcemb_tlb_check(env, tlb, &raddr, address,
1174
                             env->spr[SPR_40x_PID], 0, i) < 0)
1175
            continue;
1176
        zsel = (tlb->attr >> 4) & 0xF;
1177
        zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
1178
#if defined (DEBUG_SOFTWARE_TLB)
1179
        if (loglevel != 0) {
1180
            fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1181
                    __func__, i, zsel, zpr, rw, tlb->attr);
1182
        }
1183
#endif
1184
        /* Check execute enable bit */
1185
        switch (zpr) {
1186
        case 0x2:
1187
            if (pr != 0)
1188
                goto check_perms;
1189
            /* No break here */
1190
        case 0x3:
1191
            /* All accesses granted */
1192
            ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1193
            ret = 0;
1194
            break;
1195
        case 0x0:
1196
            if (pr != 0) {
1197
                ctx->prot = 0;
1198
                ret = -2;
1199
                break;
1200
            }
1201
            /* No break here */
1202
        case 0x1:
1203
        check_perms:
1204
            /* Check from TLB entry */
1205
            /* XXX: there is a problem here or in the TLB fill code... */
1206
            ctx->prot = tlb->prot;
1207
            ctx->prot |= PAGE_EXEC;
1208
            ret = check_prot(ctx->prot, rw, access_type);
1209
            break;
1210
        }
1211
        if (ret >= 0) {
1212
            ctx->raddr = raddr;
1213
#if defined (DEBUG_SOFTWARE_TLB)
1214
            if (loglevel != 0) {
1215
                fprintf(logfile, "%s: access granted " ADDRX " => " REGX
1216
                        " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1217
                        ret);
1218
            }
1219
#endif
1220
            return 0;
1221
        }
1222
    }
1223
#if defined (DEBUG_SOFTWARE_TLB)
1224
    if (loglevel != 0) {
1225
        fprintf(logfile, "%s: access refused " ADDRX " => " REGX
1226
                " %d %d\n", __func__, address, raddr, ctx->prot,
1227
                ret);
1228
    }
1229
#endif
1230

    
1231
    return ret;
1232
}
1233

    
1234
void store_40x_sler (CPUPPCState *env, uint32_t val)
1235
{
1236
    /* XXX: TO BE FIXED */
1237
    if (val != 0x00000000) {
1238
        cpu_abort(env, "Little-endian regions are not supported by now\n");
1239
    }
1240
    env->spr[SPR_405_SLER] = val;
1241
}
1242

    
1243
int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1244
                                   target_ulong address, int rw,
1245
                                   int access_type)
1246
{
1247
    ppcemb_tlb_t *tlb;
1248
    target_phys_addr_t raddr;
1249
    int i, prot, ret;
1250

    
1251
    ret = -1;
1252
    raddr = -1;
1253
    for (i = 0; i < env->nb_tlb; i++) {
1254
        tlb = &env->tlb[i].tlbe;
1255
        if (ppcemb_tlb_check(env, tlb, &raddr, address,
1256
                             env->spr[SPR_BOOKE_PID], 1, i) < 0)
1257
            continue;
1258
        if (msr_pr != 0)
1259
            prot = tlb->prot & 0xF;
1260
        else
1261
            prot = (tlb->prot >> 4) & 0xF;
1262
        /* Check the address space */
1263
        if (access_type == ACCESS_CODE) {
1264
            if (msr_ir != (tlb->attr & 1))
1265
                continue;
1266
            ctx->prot = prot;
1267
            if (prot & PAGE_EXEC) {
1268
                ret = 0;
1269
                break;
1270
            }
1271
            ret = -3;
1272
        } else {
1273
            if (msr_dr != (tlb->attr & 1))
1274
                continue;
1275
            ctx->prot = prot;
1276
            if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1277
                ret = 0;
1278
                break;
1279
            }
1280
            ret = -2;
1281
        }
1282
    }
1283
    if (ret >= 0)
1284
        ctx->raddr = raddr;
1285

    
1286
    return ret;
1287
}
1288

    
1289
static int check_physical (CPUState *env, mmu_ctx_t *ctx,
1290
                           target_ulong eaddr, int rw)
1291
{
1292
    int in_plb, ret;
1293

    
1294
    ctx->raddr = eaddr;
1295
    ctx->prot = PAGE_READ | PAGE_EXEC;
1296
    ret = 0;
1297
    switch (env->mmu_model) {
1298
    case POWERPC_MMU_32B:
1299
    case POWERPC_MMU_SOFT_6xx:
1300
    case POWERPC_MMU_SOFT_74xx:
1301
    case POWERPC_MMU_SOFT_4xx:
1302
    case POWERPC_MMU_REAL_4xx:
1303
    case POWERPC_MMU_BOOKE:
1304
        ctx->prot |= PAGE_WRITE;
1305
        break;
1306
#if defined(TARGET_PPC64)
1307
    case POWERPC_MMU_64B:
1308
        /* Real address are 60 bits long */
1309
        ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
1310
        ctx->prot |= PAGE_WRITE;
1311
        break;
1312
#endif
1313
    case POWERPC_MMU_SOFT_4xx_Z:
1314
        if (unlikely(msr_pe != 0)) {
1315
            /* 403 family add some particular protections,
1316
             * using PBL/PBU registers for accesses with no translation.
1317
             */
1318
            in_plb =
1319
                /* Check PLB validity */
1320
                (env->pb[0] < env->pb[1] &&
1321
                 /* and address in plb area */
1322
                 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1323
                (env->pb[2] < env->pb[3] &&
1324
                 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1325
            if (in_plb ^ msr_px) {
1326
                /* Access in protected area */
1327
                if (rw == 1) {
1328
                    /* Access is not allowed */
1329
                    ret = -2;
1330
                }
1331
            } else {
1332
                /* Read-write access is allowed */
1333
                ctx->prot |= PAGE_WRITE;
1334
            }
1335
        }
1336
        break;
1337
    case POWERPC_MMU_BOOKE_FSL:
1338
        /* XXX: TODO */
1339
        cpu_abort(env, "BookE FSL MMU model not implemented\n");
1340
        break;
1341
    default:
1342
        cpu_abort(env, "Unknown or invalid MMU model\n");
1343
        return -1;
1344
    }
1345

    
1346
    return ret;
1347
}
1348

    
1349
int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
1350
                          int rw, int access_type, int check_BATs)
1351
{
1352
    int ret;
1353

    
1354
#if 0
1355
    if (loglevel != 0) {
1356
        fprintf(logfile, "%s\n", __func__);
1357
    }
1358
#endif
1359
    if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1360
        (access_type != ACCESS_CODE && msr_dr == 0)) {
1361
        /* No address translation */
1362
        ret = check_physical(env, ctx, eaddr, rw);
1363
    } else {
1364
        ret = -1;
1365
        switch (env->mmu_model) {
1366
        case POWERPC_MMU_32B:
1367
        case POWERPC_MMU_SOFT_6xx:
1368
        case POWERPC_MMU_SOFT_74xx:
1369
            /* Try to find a BAT */
1370
            if (check_BATs)
1371
                ret = get_bat(env, ctx, eaddr, rw, access_type);
1372
            /* No break here */
1373
#if defined(TARGET_PPC64)
1374
        case POWERPC_MMU_64B:
1375
#endif
1376
            if (ret < 0) {
1377
                /* We didn't match any BAT entry or don't have BATs */
1378
                ret = get_segment(env, ctx, eaddr, rw, access_type);
1379
            }
1380
            break;
1381
        case POWERPC_MMU_SOFT_4xx:
1382
        case POWERPC_MMU_SOFT_4xx_Z:
1383
            ret = mmu40x_get_physical_address(env, ctx, eaddr,
1384
                                              rw, access_type);
1385
            break;
1386
        case POWERPC_MMU_BOOKE:
1387
            ret = mmubooke_get_physical_address(env, ctx, eaddr,
1388
                                                rw, access_type);
1389
            break;
1390
        case POWERPC_MMU_BOOKE_FSL:
1391
            /* XXX: TODO */
1392
            cpu_abort(env, "BookE FSL MMU model not implemented\n");
1393
            return -1;
1394
        case POWERPC_MMU_REAL_4xx:
1395
            cpu_abort(env, "PowerPC 401 does not do any translation\n");
1396
            return -1;
1397
        default:
1398
            cpu_abort(env, "Unknown or invalid MMU model\n");
1399
            return -1;
1400
        }
1401
    }
1402
#if 0
1403
    if (loglevel != 0) {
1404
        fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
1405
                __func__, eaddr, ret, ctx->raddr);
1406
    }
1407
#endif
1408

    
1409
    return ret;
1410
}
1411

    
1412
target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
1413
{
1414
    mmu_ctx_t ctx;
1415

    
1416
    if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT, 1) != 0))
1417
        return -1;
1418

    
1419
    return ctx.raddr & TARGET_PAGE_MASK;
1420
}
1421

    
1422
/* Perform address translation */
1423
int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
1424
                              int mmu_idx, int is_softmmu)
1425
{
1426
    mmu_ctx_t ctx;
1427
    int access_type;
1428
    int ret = 0;
1429

    
1430
    if (rw == 2) {
1431
        /* code access */
1432
        rw = 0;
1433
        access_type = ACCESS_CODE;
1434
    } else {
1435
        /* data access */
1436
        /* XXX: put correct access by using cpu_restore_state()
1437
           correctly */
1438
        access_type = ACCESS_INT;
1439
        //        access_type = env->access_type;
1440
    }
1441
    ret = get_physical_address(env, &ctx, address, rw, access_type, 1);
1442
    if (ret == 0) {
1443
        ret = tlb_set_page_exec(env, address & TARGET_PAGE_MASK,
1444
                                ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1445
                                mmu_idx, is_softmmu);
1446
    } else if (ret < 0) {
1447
#if defined (DEBUG_MMU)
1448
        if (loglevel != 0)
1449
            cpu_dump_state(env, logfile, fprintf, 0);
1450
#endif
1451
        if (access_type == ACCESS_CODE) {
1452
            switch (ret) {
1453
            case -1:
1454
                /* No matches in page tables or TLB */
1455
                switch (env->mmu_model) {
1456
                case POWERPC_MMU_SOFT_6xx:
1457
                    env->exception_index = POWERPC_EXCP_IFTLB;
1458
                    env->error_code = 1 << 18;
1459
                    env->spr[SPR_IMISS] = address;
1460
                    env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
1461
                    goto tlb_miss;
1462
                case POWERPC_MMU_SOFT_74xx:
1463
                    env->exception_index = POWERPC_EXCP_IFTLB;
1464
                    goto tlb_miss_74xx;
1465
                case POWERPC_MMU_SOFT_4xx:
1466
                case POWERPC_MMU_SOFT_4xx_Z:
1467
                    env->exception_index = POWERPC_EXCP_ITLB;
1468
                    env->error_code = 0;
1469
                    env->spr[SPR_40x_DEAR] = address;
1470
                    env->spr[SPR_40x_ESR] = 0x00000000;
1471
                    break;
1472
                case POWERPC_MMU_32B:
1473
#if defined(TARGET_PPC64)
1474
                case POWERPC_MMU_64B:
1475
#endif
1476
                    env->exception_index = POWERPC_EXCP_ISI;
1477
                    env->error_code = 0x40000000;
1478
                    break;
1479
                case POWERPC_MMU_BOOKE:
1480
                    /* XXX: TODO */
1481
                    cpu_abort(env, "MMU model not implemented\n");
1482
                    return -1;
1483
                case POWERPC_MMU_BOOKE_FSL:
1484
                    /* XXX: TODO */
1485
                    cpu_abort(env, "MMU model not implemented\n");
1486
                    return -1;
1487
                case POWERPC_MMU_REAL_4xx:
1488
                    cpu_abort(env, "PowerPC 401 should never raise any MMU "
1489
                              "exceptions\n");
1490
                    return -1;
1491
                default:
1492
                    cpu_abort(env, "Unknown or invalid MMU model\n");
1493
                    return -1;
1494
                }
1495
                break;
1496
            case -2:
1497
                /* Access rights violation */
1498
                env->exception_index = POWERPC_EXCP_ISI;
1499
                env->error_code = 0x08000000;
1500
                break;
1501
            case -3:
1502
                /* No execute protection violation */
1503
                env->exception_index = POWERPC_EXCP_ISI;
1504
                env->error_code = 0x10000000;
1505
                break;
1506
            case -4:
1507
                /* Direct store exception */
1508
                /* No code fetch is allowed in direct-store areas */
1509
                env->exception_index = POWERPC_EXCP_ISI;
1510
                env->error_code = 0x10000000;
1511
                break;
1512
#if defined(TARGET_PPC64)
1513
            case -5:
1514
                /* No match in segment table */
1515
                env->exception_index = POWERPC_EXCP_ISEG;
1516
                env->error_code = 0;
1517
                break;
1518
#endif
1519
            }
1520
        } else {
1521
            switch (ret) {
1522
            case -1:
1523
                /* No matches in page tables or TLB */
1524
                switch (env->mmu_model) {
1525
                case POWERPC_MMU_SOFT_6xx:
1526
                    if (rw == 1) {
1527
                        env->exception_index = POWERPC_EXCP_DSTLB;
1528
                        env->error_code = 1 << 16;
1529
                    } else {
1530
                        env->exception_index = POWERPC_EXCP_DLTLB;
1531
                        env->error_code = 0;
1532
                    }
1533
                    env->spr[SPR_DMISS] = address;
1534
                    env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1535
                tlb_miss:
1536
                    env->error_code |= ctx.key << 19;
1537
                    env->spr[SPR_HASH1] = ctx.pg_addr[0];
1538
                    env->spr[SPR_HASH2] = ctx.pg_addr[1];
1539
                    break;
1540
                case POWERPC_MMU_SOFT_74xx:
1541
                    if (rw == 1) {
1542
                        env->exception_index = POWERPC_EXCP_DSTLB;
1543
                    } else {
1544
                        env->exception_index = POWERPC_EXCP_DLTLB;
1545
                    }
1546
                tlb_miss_74xx:
1547
                    /* Implement LRU algorithm */
1548
                    env->error_code = ctx.key << 19;
1549
                    env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1550
                        ((env->last_way + 1) & (env->nb_ways - 1));
1551
                    env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
1552
                    break;
1553
                case POWERPC_MMU_SOFT_4xx:
1554
                case POWERPC_MMU_SOFT_4xx_Z:
1555
                    env->exception_index = POWERPC_EXCP_DTLB;
1556
                    env->error_code = 0;
1557
                    env->spr[SPR_40x_DEAR] = address;
1558
                    if (rw)
1559
                        env->spr[SPR_40x_ESR] = 0x00800000;
1560
                    else
1561
                        env->spr[SPR_40x_ESR] = 0x00000000;
1562
                    break;
1563
                case POWERPC_MMU_32B:
1564
#if defined(TARGET_PPC64)
1565
                case POWERPC_MMU_64B:
1566
#endif
1567
                    env->exception_index = POWERPC_EXCP_DSI;
1568
                    env->error_code = 0;
1569
                    env->spr[SPR_DAR] = address;
1570
                    if (rw == 1)
1571
                        env->spr[SPR_DSISR] = 0x42000000;
1572
                    else
1573
                        env->spr[SPR_DSISR] = 0x40000000;
1574
                    break;
1575
                case POWERPC_MMU_BOOKE:
1576
                    /* XXX: TODO */
1577
                    cpu_abort(env, "MMU model not implemented\n");
1578
                    return -1;
1579
                case POWERPC_MMU_BOOKE_FSL:
1580
                    /* XXX: TODO */
1581
                    cpu_abort(env, "MMU model not implemented\n");
1582
                    return -1;
1583
                case POWERPC_MMU_REAL_4xx:
1584
                    cpu_abort(env, "PowerPC 401 should never raise any MMU "
1585
                              "exceptions\n");
1586
                    return -1;
1587
                default:
1588
                    cpu_abort(env, "Unknown or invalid MMU model\n");
1589
                    return -1;
1590
                }
1591
                break;
1592
            case -2:
1593
                /* Access rights violation */
1594
                env->exception_index = POWERPC_EXCP_DSI;
1595
                env->error_code = 0;
1596
                env->spr[SPR_DAR] = address;
1597
                if (rw == 1)
1598
                    env->spr[SPR_DSISR] = 0x0A000000;
1599
                else
1600
                    env->spr[SPR_DSISR] = 0x08000000;
1601
                break;
1602
            case -4:
1603
                /* Direct store exception */
1604
                switch (access_type) {
1605
                case ACCESS_FLOAT:
1606
                    /* Floating point load/store */
1607
                    env->exception_index = POWERPC_EXCP_ALIGN;
1608
                    env->error_code = POWERPC_EXCP_ALIGN_FP;
1609
                    env->spr[SPR_DAR] = address;
1610
                    break;
1611
                case ACCESS_RES:
1612
                    /* lwarx, ldarx or stwcx. */
1613
                    env->exception_index = POWERPC_EXCP_DSI;
1614
                    env->error_code = 0;
1615
                    env->spr[SPR_DAR] = address;
1616
                    if (rw == 1)
1617
                        env->spr[SPR_DSISR] = 0x06000000;
1618
                    else
1619
                        env->spr[SPR_DSISR] = 0x04000000;
1620
                    break;
1621
                case ACCESS_EXT:
1622
                    /* eciwx or ecowx */
1623
                    env->exception_index = POWERPC_EXCP_DSI;
1624
                    env->error_code = 0;
1625
                    env->spr[SPR_DAR] = address;
1626
                    if (rw == 1)
1627
                        env->spr[SPR_DSISR] = 0x06100000;
1628
                    else
1629
                        env->spr[SPR_DSISR] = 0x04100000;
1630
                    break;
1631
                default:
1632
                    printf("DSI: invalid exception (%d)\n", ret);
1633
                    env->exception_index = POWERPC_EXCP_PROGRAM;
1634
                    env->error_code =
1635
                        POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1636
                    env->spr[SPR_DAR] = address;
1637
                    break;
1638
                }
1639
                break;
1640
#if defined(TARGET_PPC64)
1641
            case -5:
1642
                /* No match in segment table */
1643
                env->exception_index = POWERPC_EXCP_DSEG;
1644
                env->error_code = 0;
1645
                env->spr[SPR_DAR] = address;
1646
                break;
1647
#endif
1648
            }
1649
        }
1650
#if 0
1651
        printf("%s: set exception to %d %02x\n", __func__,
1652
               env->exception, env->error_code);
1653
#endif
1654
        ret = 1;
1655
    }
1656

    
1657
    return ret;
1658
}
1659

    
1660
/*****************************************************************************/
1661
/* BATs management */
1662
#if !defined(FLUSH_ALL_TLBS)
1663
static always_inline void do_invalidate_BAT (CPUPPCState *env,
1664
                                             target_ulong BATu,
1665
                                             target_ulong mask)
1666
{
1667
    target_ulong base, end, page;
1668

    
1669
    base = BATu & ~0x0001FFFF;
1670
    end = base + mask + 0x00020000;
1671
#if defined (DEBUG_BATS)
1672
    if (loglevel != 0) {
1673
        fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
1674
                base, end, mask);
1675
    }
1676
#endif
1677
    for (page = base; page != end; page += TARGET_PAGE_SIZE)
1678
        tlb_flush_page(env, page);
1679
#if defined (DEBUG_BATS)
1680
    if (loglevel != 0)
1681
        fprintf(logfile, "Flush done\n");
1682
#endif
1683
}
1684
#endif
1685

    
1686
static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1687
                                          int ul, int nr, target_ulong value)
1688
{
1689
#if defined (DEBUG_BATS)
1690
    if (loglevel != 0) {
1691
        fprintf(logfile, "Set %cBAT%d%c to 0x" ADDRX " (0x" ADDRX ")\n",
1692
                ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
1693
    }
1694
#endif
1695
}
1696

    
1697
target_ulong do_load_ibatu (CPUPPCState *env, int nr)
1698
{
1699
    return env->IBAT[0][nr];
1700
}
1701

    
1702
target_ulong do_load_ibatl (CPUPPCState *env, int nr)
1703
{
1704
    return env->IBAT[1][nr];
1705
}
1706

    
1707
void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
1708
{
1709
    target_ulong mask;
1710

    
1711
    dump_store_bat(env, 'I', 0, nr, value);
1712
    if (env->IBAT[0][nr] != value) {
1713
        mask = (value << 15) & 0x0FFE0000UL;
1714
#if !defined(FLUSH_ALL_TLBS)
1715
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1716
#endif
1717
        /* When storing valid upper BAT, mask BEPI and BRPN
1718
         * and invalidate all TLBs covered by this BAT
1719
         */
1720
        mask = (value << 15) & 0x0FFE0000UL;
1721
        env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1722
            (value & ~0x0001FFFFUL & ~mask);
1723
        env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1724
            (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1725
#if !defined(FLUSH_ALL_TLBS)
1726
        do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1727
#else
1728
        tlb_flush(env, 1);
1729
#endif
1730
    }
1731
}
1732

    
1733
void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
1734
{
1735
    dump_store_bat(env, 'I', 1, nr, value);
1736
    env->IBAT[1][nr] = value;
1737
}
1738

    
1739
target_ulong do_load_dbatu (CPUPPCState *env, int nr)
1740
{
1741
    return env->DBAT[0][nr];
1742
}
1743

    
1744
target_ulong do_load_dbatl (CPUPPCState *env, int nr)
1745
{
1746
    return env->DBAT[1][nr];
1747
}
1748

    
1749
void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
1750
{
1751
    target_ulong mask;
1752

    
1753
    dump_store_bat(env, 'D', 0, nr, value);
1754
    if (env->DBAT[0][nr] != value) {
1755
        /* When storing valid upper BAT, mask BEPI and BRPN
1756
         * and invalidate all TLBs covered by this BAT
1757
         */
1758
        mask = (value << 15) & 0x0FFE0000UL;
1759
#if !defined(FLUSH_ALL_TLBS)
1760
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1761
#endif
1762
        mask = (value << 15) & 0x0FFE0000UL;
1763
        env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1764
            (value & ~0x0001FFFFUL & ~mask);
1765
        env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1766
            (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1767
#if !defined(FLUSH_ALL_TLBS)
1768
        do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1769
#else
1770
        tlb_flush(env, 1);
1771
#endif
1772
    }
1773
}
1774

    
1775
void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
1776
{
1777
    dump_store_bat(env, 'D', 1, nr, value);
1778
    env->DBAT[1][nr] = value;
1779
}
1780

    
1781
/*****************************************************************************/
1782
/* TLB management */
1783
void ppc_tlb_invalidate_all (CPUPPCState *env)
1784
{
1785
    switch (env->mmu_model) {
1786
    case POWERPC_MMU_SOFT_6xx:
1787
    case POWERPC_MMU_SOFT_74xx:
1788
        ppc6xx_tlb_invalidate_all(env);
1789
        break;
1790
    case POWERPC_MMU_SOFT_4xx:
1791
    case POWERPC_MMU_SOFT_4xx_Z:
1792
        ppc4xx_tlb_invalidate_all(env);
1793
        break;
1794
    case POWERPC_MMU_REAL_4xx:
1795
        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1796
        break;
1797
    case POWERPC_MMU_BOOKE:
1798
        /* XXX: TODO */
1799
        cpu_abort(env, "MMU model not implemented\n");
1800
        break;
1801
    case POWERPC_MMU_BOOKE_FSL:
1802
        /* XXX: TODO */
1803
        cpu_abort(env, "MMU model not implemented\n");
1804
        break;
1805
    case POWERPC_MMU_32B:
1806
#if defined(TARGET_PPC64)
1807
    case POWERPC_MMU_64B:
1808
#endif /* defined(TARGET_PPC64) */
1809
        tlb_flush(env, 1);
1810
        break;
1811
    default:
1812
        /* XXX: TODO */
1813
        cpu_abort(env, "Unknown MMU model\n");
1814
        break;
1815
    }
1816
}
1817

    
1818
void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1819
{
1820
#if !defined(FLUSH_ALL_TLBS)
1821
    addr &= TARGET_PAGE_MASK;
1822
    switch (env->mmu_model) {
1823
    case POWERPC_MMU_SOFT_6xx:
1824
    case POWERPC_MMU_SOFT_74xx:
1825
        ppc6xx_tlb_invalidate_virt(env, addr, 0);
1826
        if (env->id_tlbs == 1)
1827
            ppc6xx_tlb_invalidate_virt(env, addr, 1);
1828
        break;
1829
    case POWERPC_MMU_SOFT_4xx:
1830
    case POWERPC_MMU_SOFT_4xx_Z:
1831
        ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
1832
        break;
1833
    case POWERPC_MMU_REAL_4xx:
1834
        cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1835
        break;
1836
    case POWERPC_MMU_BOOKE:
1837
        /* XXX: TODO */
1838
        cpu_abort(env, "MMU model not implemented\n");
1839
        break;
1840
    case POWERPC_MMU_BOOKE_FSL:
1841
        /* XXX: TODO */
1842
        cpu_abort(env, "MMU model not implemented\n");
1843
        break;
1844
    case POWERPC_MMU_32B:
1845
        /* tlbie invalidate TLBs for all segments */
1846
        addr &= ~((target_ulong)-1 << 28);
1847
        /* XXX: this case should be optimized,
1848
         * giving a mask to tlb_flush_page
1849
         */
1850
        tlb_flush_page(env, addr | (0x0 << 28));
1851
        tlb_flush_page(env, addr | (0x1 << 28));
1852
        tlb_flush_page(env, addr | (0x2 << 28));
1853
        tlb_flush_page(env, addr | (0x3 << 28));
1854
        tlb_flush_page(env, addr | (0x4 << 28));
1855
        tlb_flush_page(env, addr | (0x5 << 28));
1856
        tlb_flush_page(env, addr | (0x6 << 28));
1857
        tlb_flush_page(env, addr | (0x7 << 28));
1858
        tlb_flush_page(env, addr | (0x8 << 28));
1859
        tlb_flush_page(env, addr | (0x9 << 28));
1860
        tlb_flush_page(env, addr | (0xA << 28));
1861
        tlb_flush_page(env, addr | (0xB << 28));
1862
        tlb_flush_page(env, addr | (0xC << 28));
1863
        tlb_flush_page(env, addr | (0xD << 28));
1864
        tlb_flush_page(env, addr | (0xE << 28));
1865
        tlb_flush_page(env, addr | (0xF << 28));
1866
        break;
1867
#if defined(TARGET_PPC64)
1868
    case POWERPC_MMU_64B:
1869
        /* tlbie invalidate TLBs for all segments */
1870
        /* XXX: given the fact that there are too many segments to invalidate,
1871
         *      and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
1872
         *      we just invalidate all TLBs
1873
         */
1874
        tlb_flush(env, 1);
1875
        break;
1876
#endif /* defined(TARGET_PPC64) */
1877
    default:
1878
        /* XXX: TODO */
1879
        cpu_abort(env, "Unknown MMU model\n");
1880
        break;
1881
    }
1882
#else
1883
    ppc_tlb_invalidate_all(env);
1884
#endif
1885
}
1886

    
1887
/*****************************************************************************/
1888
/* Special registers manipulation */
1889
#if defined(TARGET_PPC64)
1890
target_ulong ppc_load_asr (CPUPPCState *env)
1891
{
1892
    return env->asr;
1893
}
1894

    
1895
void ppc_store_asr (CPUPPCState *env, target_ulong value)
1896
{
1897
    if (env->asr != value) {
1898
        env->asr = value;
1899
        tlb_flush(env, 1);
1900
    }
1901
}
1902
#endif
1903

    
1904
target_ulong do_load_sdr1 (CPUPPCState *env)
1905
{
1906
    return env->sdr1;
1907
}
1908

    
1909
void do_store_sdr1 (CPUPPCState *env, target_ulong value)
1910
{
1911
#if defined (DEBUG_MMU)
1912
    if (loglevel != 0) {
1913
        fprintf(logfile, "%s: 0x" ADDRX "\n", __func__, value);
1914
    }
1915
#endif
1916
    if (env->sdr1 != value) {
1917
        /* XXX: for PowerPC 64, should check that the HTABSIZE value
1918
         *      is <= 28
1919
         */
1920
        env->sdr1 = value;
1921
        tlb_flush(env, 1);
1922
    }
1923
}
1924

    
1925
#if 0 // Unused
1926
target_ulong do_load_sr (CPUPPCState *env, int srnum)
1927
{
1928
    return env->sr[srnum];
1929
}
1930
#endif
1931

    
1932
void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
1933
{
1934
#if defined (DEBUG_MMU)
1935
    if (loglevel != 0) {
1936
        fprintf(logfile, "%s: reg=%d 0x" ADDRX " " ADDRX "\n",
1937
                __func__, srnum, value, env->sr[srnum]);
1938
    }
1939
#endif
1940
    if (env->sr[srnum] != value) {
1941
        env->sr[srnum] = value;
1942
#if !defined(FLUSH_ALL_TLBS) && 0
1943
        {
1944
            target_ulong page, end;
1945
            /* Invalidate 256 MB of virtual memory */
1946
            page = (16 << 20) * srnum;
1947
            end = page + (16 << 20);
1948
            for (; page != end; page += TARGET_PAGE_SIZE)
1949
                tlb_flush_page(env, page);
1950
        }
1951
#else
1952
        tlb_flush(env, 1);
1953
#endif
1954
    }
1955
}
1956
#endif /* !defined (CONFIG_USER_ONLY) */
1957

    
1958
target_ulong ppc_load_xer (CPUPPCState *env)
1959
{
1960
    return hreg_load_xer(env);
1961
}
1962

    
1963
void ppc_store_xer (CPUPPCState *env, target_ulong value)
1964
{
1965
    hreg_store_xer(env, value);
1966
}
1967

    
1968
/* GDBstub can read and write MSR... */
1969
void ppc_store_msr (CPUPPCState *env, target_ulong value)
1970
{
1971
    hreg_store_msr(env, value);
1972
}
1973

    
1974
/*****************************************************************************/
1975
/* Exception processing */
1976
#if defined (CONFIG_USER_ONLY)
1977
void do_interrupt (CPUState *env)
1978
{
1979
    env->exception_index = POWERPC_EXCP_NONE;
1980
    env->error_code = 0;
1981
}
1982

    
1983
void ppc_hw_interrupt (CPUState *env)
1984
{
1985
    env->exception_index = POWERPC_EXCP_NONE;
1986
    env->error_code = 0;
1987
}
1988
#else /* defined (CONFIG_USER_ONLY) */
1989
static void dump_syscall (CPUState *env)
1990
{
1991
    fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
1992
            " r5=0x" REGX " r6=0x" REGX " nip=0x" ADDRX "\n",
1993
            env->gpr[0], env->gpr[3], env->gpr[4],
1994
            env->gpr[5], env->gpr[6], env->nip);
1995
}
1996

    
1997
/* Note that this function should be greatly optimized
1998
 * when called with a constant excp, from ppc_hw_interrupt
1999
 */
2000
static always_inline void powerpc_excp (CPUState *env,
2001
                                        int excp_model, int excp)
2002
{
2003
    target_ulong msr, new_msr, vector;
2004
    int srr0, srr1, asrr0, asrr1;
2005

    
2006
    if (loglevel & CPU_LOG_INT) {
2007
        fprintf(logfile, "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
2008
                env->nip, excp, env->error_code);
2009
    }
2010
    msr = env->msr;
2011
    new_msr = msr;
2012
    srr0 = SPR_SRR0;
2013
    srr1 = SPR_SRR1;
2014
    asrr0 = -1;
2015
    asrr1 = -1;
2016
    msr &= ~((target_ulong)0x783F0000);
2017
    switch (excp) {
2018
    case POWERPC_EXCP_NONE:
2019
        /* Should never happen */
2020
        return;
2021
    case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
2022
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2023
        switch (excp_model) {
2024
        case POWERPC_EXCP_40x:
2025
            srr0 = SPR_40x_SRR2;
2026
            srr1 = SPR_40x_SRR3;
2027
            break;
2028
        case POWERPC_EXCP_BOOKE:
2029
            srr0 = SPR_BOOKE_CSRR0;
2030
            srr1 = SPR_BOOKE_CSRR1;
2031
            break;
2032
        case POWERPC_EXCP_G2:
2033
            break;
2034
        default:
2035
            goto excp_invalid;
2036
        }
2037
        goto store_next;
2038
    case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
2039
        if (msr_me == 0) {
2040
            /* Machine check exception is not enabled.
2041
             * Enter checkstop state.
2042
             */
2043
            if (loglevel != 0) {
2044
                fprintf(logfile, "Machine check while not allowed. "
2045
                        "Entering checkstop state\n");
2046
            } else {
2047
                fprintf(stderr, "Machine check while not allowed. "
2048
                        "Entering checkstop state\n");
2049
            }
2050
            env->halted = 1;
2051
            env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2052
        }
2053
        new_msr &= ~((target_ulong)1 << MSR_RI);
2054
        new_msr &= ~((target_ulong)1 << MSR_ME);
2055
#if defined(TARGET_PPC64H)
2056
        new_msr |= (target_ulong)1 << MSR_HV;
2057
#endif
2058
        /* XXX: should also have something loaded in DAR / DSISR */
2059
        switch (excp_model) {
2060
        case POWERPC_EXCP_40x:
2061
            srr0 = SPR_40x_SRR2;
2062
            srr1 = SPR_40x_SRR3;
2063
            break;
2064
        case POWERPC_EXCP_BOOKE:
2065
            srr0 = SPR_BOOKE_MCSRR0;
2066
            srr1 = SPR_BOOKE_MCSRR1;
2067
            asrr0 = SPR_BOOKE_CSRR0;
2068
            asrr1 = SPR_BOOKE_CSRR1;
2069
            break;
2070
        default:
2071
            break;
2072
        }
2073
        goto store_next;
2074
    case POWERPC_EXCP_DSI:       /* Data storage exception                   */
2075
#if defined (DEBUG_EXCEPTIONS)
2076
        if (loglevel != 0) {
2077
            fprintf(logfile, "DSI exception: DSISR=0x" ADDRX" DAR=0x" ADDRX
2078
                    "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
2079
        }
2080
#endif
2081
        new_msr &= ~((target_ulong)1 << MSR_RI);
2082
#if defined(TARGET_PPC64H)
2083
        if (lpes1 == 0)
2084
            new_msr |= (target_ulong)1 << MSR_HV;
2085
#endif
2086
        goto store_next;
2087
    case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
2088
#if defined (DEBUG_EXCEPTIONS)
2089
        if (loglevel != 0) {
2090
            fprintf(logfile, "ISI exception: msr=0x" ADDRX ", nip=0x" ADDRX
2091
                    "\n", msr, env->nip);
2092
        }
2093
#endif
2094
        new_msr &= ~((target_ulong)1 << MSR_RI);
2095
#if defined(TARGET_PPC64H)
2096
        if (lpes1 == 0)
2097
            new_msr |= (target_ulong)1 << MSR_HV;
2098
#endif
2099
        msr |= env->error_code;
2100
        goto store_next;
2101
    case POWERPC_EXCP_EXTERNAL:  /* External input                           */
2102
        new_msr &= ~((target_ulong)1 << MSR_RI);
2103
#if defined(TARGET_PPC64H)
2104
        if (lpes0 == 1)
2105
            new_msr |= (target_ulong)1 << MSR_HV;
2106
#endif
2107
        goto store_next;
2108
    case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
2109
        new_msr &= ~((target_ulong)1 << MSR_RI);
2110
#if defined(TARGET_PPC64H)
2111
        if (lpes1 == 0)
2112
            new_msr |= (target_ulong)1 << MSR_HV;
2113
#endif
2114
        /* XXX: this is false */
2115
        /* Get rS/rD and rA from faulting opcode */
2116
        env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
2117
        goto store_current;
2118
    case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
2119
        switch (env->error_code & ~0xF) {
2120
        case POWERPC_EXCP_FP:
2121
            if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
2122
#if defined (DEBUG_EXCEPTIONS)
2123
                if (loglevel != 0) {
2124
                    fprintf(logfile, "Ignore floating point exception\n");
2125
                }
2126
#endif
2127
                return;
2128
            }
2129
            new_msr &= ~((target_ulong)1 << MSR_RI);
2130
#if defined(TARGET_PPC64H)
2131
            if (lpes1 == 0)
2132
                new_msr |= (target_ulong)1 << MSR_HV;
2133
#endif
2134
            msr |= 0x00100000;
2135
            /* Set FX */
2136
            env->fpscr[7] |= 0x8;
2137
            /* Finally, update FEX */
2138
            if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
2139
                ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
2140
                env->fpscr[7] |= 0x4;
2141
            if (msr_fe0 != msr_fe1) {
2142
                msr |= 0x00010000;
2143
                goto store_current;
2144
            }
2145
            break;
2146
        case POWERPC_EXCP_INVAL:
2147
#if defined (DEBUG_EXCEPTIONS)
2148
            if (loglevel != 0) {
2149
                fprintf(logfile, "Invalid instruction at 0x" ADDRX "\n",
2150
                        env->nip);
2151
            }
2152
#endif
2153
            new_msr &= ~((target_ulong)1 << MSR_RI);
2154
#if defined(TARGET_PPC64H)
2155
            if (lpes1 == 0)
2156
                new_msr |= (target_ulong)1 << MSR_HV;
2157
#endif
2158
            msr |= 0x00080000;
2159
            break;
2160
        case POWERPC_EXCP_PRIV:
2161
            new_msr &= ~((target_ulong)1 << MSR_RI);
2162
#if defined(TARGET_PPC64H)
2163
            if (lpes1 == 0)
2164
                new_msr |= (target_ulong)1 << MSR_HV;
2165
#endif
2166
            msr |= 0x00040000;
2167
            break;
2168
        case POWERPC_EXCP_TRAP:
2169
            new_msr &= ~((target_ulong)1 << MSR_RI);
2170
#if defined(TARGET_PPC64H)
2171
            if (lpes1 == 0)
2172
                new_msr |= (target_ulong)1 << MSR_HV;
2173
#endif
2174
            msr |= 0x00020000;
2175
            break;
2176
        default:
2177
            /* Should never occur */
2178
            cpu_abort(env, "Invalid program exception %d. Aborting\n",
2179
                      env->error_code);
2180
            break;
2181
        }
2182
        goto store_next;
2183
    case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
2184
        new_msr &= ~((target_ulong)1 << MSR_RI);
2185
#if defined(TARGET_PPC64H)
2186
        if (lpes1 == 0)
2187
            new_msr |= (target_ulong)1 << MSR_HV;
2188
#endif
2189
        goto store_current;
2190
    case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
2191
        /* NOTE: this is a temporary hack to support graphics OSI
2192
           calls from the MOL driver */
2193
        /* XXX: To be removed */
2194
        if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2195
            env->osi_call) {
2196
            if (env->osi_call(env) != 0)
2197
                return;
2198
        }
2199
        if (loglevel & CPU_LOG_INT) {
2200
            dump_syscall(env);
2201
        }
2202
        new_msr &= ~((target_ulong)1 << MSR_RI);
2203
#if defined(TARGET_PPC64H)
2204
        if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
2205
            new_msr |= (target_ulong)1 << MSR_HV;
2206
#endif
2207
        goto store_next;
2208
    case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
2209
        new_msr &= ~((target_ulong)1 << MSR_RI);
2210
        goto store_current;
2211
    case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
2212
        new_msr &= ~((target_ulong)1 << MSR_RI);
2213
#if defined(TARGET_PPC64H)
2214
        if (lpes1 == 0)
2215
            new_msr |= (target_ulong)1 << MSR_HV;
2216
#endif
2217
        goto store_next;
2218
    case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
2219
        /* FIT on 4xx */
2220
#if defined (DEBUG_EXCEPTIONS)
2221
        if (loglevel != 0)
2222
            fprintf(logfile, "FIT exception\n");
2223
#endif
2224
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2225
        goto store_next;
2226
    case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
2227
#if defined (DEBUG_EXCEPTIONS)
2228
        if (loglevel != 0)
2229
            fprintf(logfile, "WDT exception\n");
2230
#endif
2231
        switch (excp_model) {
2232
        case POWERPC_EXCP_BOOKE:
2233
            srr0 = SPR_BOOKE_CSRR0;
2234
            srr1 = SPR_BOOKE_CSRR1;
2235
            break;
2236
        default:
2237
            break;
2238
        }
2239
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2240
        goto store_next;
2241
    case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
2242
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2243
        goto store_next;
2244
    case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
2245
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2246
        goto store_next;
2247
    case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
2248
        switch (excp_model) {
2249
        case POWERPC_EXCP_BOOKE:
2250
            srr0 = SPR_BOOKE_DSRR0;
2251
            srr1 = SPR_BOOKE_DSRR1;
2252
            asrr0 = SPR_BOOKE_CSRR0;
2253
            asrr1 = SPR_BOOKE_CSRR1;
2254
            break;
2255
        default:
2256
            break;
2257
        }
2258
        /* XXX: TODO */
2259
        cpu_abort(env, "Debug exception is not implemented yet !\n");
2260
        goto store_next;
2261
#if defined(TARGET_PPCEMB)
2262
    case POWERPC_EXCP_SPEU:      /* SPE/embedded floating-point unavailable  */
2263
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2264
        goto store_current;
2265
    case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
2266
        /* XXX: TODO */
2267
        cpu_abort(env, "Embedded floating point data exception "
2268
                  "is not implemented yet !\n");
2269
        goto store_next;
2270
    case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
2271
        /* XXX: TODO */
2272
        cpu_abort(env, "Embedded floating point round exception "
2273
                  "is not implemented yet !\n");
2274
        goto store_next;
2275
    case POWERPC_EXCP_EPERFM:    /* Embedded performance monitor interrupt   */
2276
        new_msr &= ~((target_ulong)1 << MSR_RI);
2277
        /* XXX: TODO */
2278
        cpu_abort(env,
2279
                  "Performance counter exception is not implemented yet !\n");
2280
        goto store_next;
2281
    case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
2282
        /* XXX: TODO */
2283
        cpu_abort(env,
2284
                  "Embedded doorbell interrupt is not implemented yet !\n");
2285
        goto store_next;
2286
    case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
2287
        switch (excp_model) {
2288
        case POWERPC_EXCP_BOOKE:
2289
            srr0 = SPR_BOOKE_CSRR0;
2290
            srr1 = SPR_BOOKE_CSRR1;
2291
            break;
2292
        default:
2293
            break;
2294
        }
2295
        /* XXX: TODO */
2296
        cpu_abort(env, "Embedded doorbell critical interrupt "
2297
                  "is not implemented yet !\n");
2298
        goto store_next;
2299
#endif /* defined(TARGET_PPCEMB) */
2300
    case POWERPC_EXCP_RESET:     /* System reset exception                   */
2301
        new_msr &= ~((target_ulong)1 << MSR_RI);
2302
#if defined(TARGET_PPC64H)
2303
        new_msr |= (target_ulong)1 << MSR_HV;
2304
#endif
2305
        goto store_next;
2306
#if defined(TARGET_PPC64)
2307
    case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
2308
        new_msr &= ~((target_ulong)1 << MSR_RI);
2309
#if defined(TARGET_PPC64H)
2310
        if (lpes1 == 0)
2311
            new_msr |= (target_ulong)1 << MSR_HV;
2312
#endif
2313
        goto store_next;
2314
    case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
2315
        new_msr &= ~((target_ulong)1 << MSR_RI);
2316
#if defined(TARGET_PPC64H)
2317
        if (lpes1 == 0)
2318
            new_msr |= (target_ulong)1 << MSR_HV;
2319
#endif
2320
        goto store_next;
2321
#endif /* defined(TARGET_PPC64) */
2322
#if defined(TARGET_PPC64H)
2323
    case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
2324
        srr0 = SPR_HSRR0;
2325
        srr1 = SPR_HSSR1;
2326
        new_msr |= (target_ulong)1 << MSR_HV;
2327
        goto store_next;
2328
#endif
2329
    case POWERPC_EXCP_TRACE:     /* Trace exception                          */
2330
        new_msr &= ~((target_ulong)1 << MSR_RI);
2331
#if defined(TARGET_PPC64H)
2332
        if (lpes1 == 0)
2333
            new_msr |= (target_ulong)1 << MSR_HV;
2334
#endif
2335
        goto store_next;
2336
#if defined(TARGET_PPC64H)
2337
    case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
2338
        srr0 = SPR_HSRR0;
2339
        srr1 = SPR_HSSR1;
2340
        new_msr |= (target_ulong)1 << MSR_HV;
2341
        goto store_next;
2342
    case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
2343
        srr0 = SPR_HSRR0;
2344
        srr1 = SPR_HSSR1;
2345
        new_msr |= (target_ulong)1 << MSR_HV;
2346
        goto store_next;
2347
    case POWERPC_EXCP_HDSEG:     /* Hypervisor data segment exception        */
2348
        srr0 = SPR_HSRR0;
2349
        srr1 = SPR_HSSR1;
2350
        new_msr |= (target_ulong)1 << MSR_HV;
2351
        goto store_next;
2352
    case POWERPC_EXCP_HISEG:     /* Hypervisor instruction segment exception */
2353
        srr0 = SPR_HSRR0;
2354
        srr1 = SPR_HSSR1;
2355
        new_msr |= (target_ulong)1 << MSR_HV;
2356
        goto store_next;
2357
#endif /* defined(TARGET_PPC64H) */
2358
    case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
2359
        new_msr &= ~((target_ulong)1 << MSR_RI);
2360
#if defined(TARGET_PPC64H)
2361
        if (lpes1 == 0)
2362
            new_msr |= (target_ulong)1 << MSR_HV;
2363
#endif
2364
        goto store_current;
2365
    case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
2366
#if defined (DEBUG_EXCEPTIONS)
2367
        if (loglevel != 0)
2368
            fprintf(logfile, "PIT exception\n");
2369
#endif
2370
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2371
        goto store_next;
2372
    case POWERPC_EXCP_IO:        /* IO error exception                       */
2373
        /* XXX: TODO */
2374
        cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2375
        goto store_next;
2376
    case POWERPC_EXCP_RUNM:      /* Run mode exception                       */
2377
        /* XXX: TODO */
2378
        cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2379
        goto store_next;
2380
    case POWERPC_EXCP_EMUL:      /* Emulation trap exception                 */
2381
        /* XXX: TODO */
2382
        cpu_abort(env, "602 emulation trap exception "
2383
                  "is not implemented yet !\n");
2384
        goto store_next;
2385
    case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
2386
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2387
#if defined(TARGET_PPC64H) /* XXX: check this */
2388
        if (lpes1 == 0)
2389
            new_msr |= (target_ulong)1 << MSR_HV;
2390
#endif
2391
        switch (excp_model) {
2392
        case POWERPC_EXCP_602:
2393
        case POWERPC_EXCP_603:
2394
        case POWERPC_EXCP_603E:
2395
        case POWERPC_EXCP_G2:
2396
            goto tlb_miss_tgpr;
2397
        case POWERPC_EXCP_7x5:
2398
            goto tlb_miss;
2399
        case POWERPC_EXCP_74xx:
2400
            goto tlb_miss_74xx;
2401
        default:
2402
            cpu_abort(env, "Invalid instruction TLB miss exception\n");
2403
            break;
2404
        }
2405
        break;
2406
    case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
2407
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2408
#if defined(TARGET_PPC64H) /* XXX: check this */
2409
        if (lpes1 == 0)
2410
            new_msr |= (target_ulong)1 << MSR_HV;
2411
#endif
2412
        switch (excp_model) {
2413
        case POWERPC_EXCP_602:
2414
        case POWERPC_EXCP_603:
2415
        case POWERPC_EXCP_603E:
2416
        case POWERPC_EXCP_G2:
2417
            goto tlb_miss_tgpr;
2418
        case POWERPC_EXCP_7x5:
2419
            goto tlb_miss;
2420
        case POWERPC_EXCP_74xx:
2421
            goto tlb_miss_74xx;
2422
        default:
2423
            cpu_abort(env, "Invalid data load TLB miss exception\n");
2424
            break;
2425
        }
2426
        break;
2427
    case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
2428
        new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2429
#if defined(TARGET_PPC64H) /* XXX: check this */
2430
        if (lpes1 == 0)
2431
            new_msr |= (target_ulong)1 << MSR_HV;
2432
#endif
2433
        switch (excp_model) {
2434
        case POWERPC_EXCP_602:
2435
        case POWERPC_EXCP_603:
2436
        case POWERPC_EXCP_603E:
2437
        case POWERPC_EXCP_G2:
2438
        tlb_miss_tgpr:
2439
            /* Swap temporary saved registers with GPRs */
2440
            if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
2441
                new_msr |= (target_ulong)1 << MSR_TGPR;
2442
                hreg_swap_gpr_tgpr(env);
2443
            }
2444
            goto tlb_miss;
2445
        case POWERPC_EXCP_7x5:
2446
        tlb_miss:
2447
#if defined (DEBUG_SOFTWARE_TLB)
2448
            if (loglevel != 0) {
2449
                const unsigned char *es;
2450
                target_ulong *miss, *cmp;
2451
                int en;
2452
                if (excp == POWERPC_EXCP_IFTLB) {
2453
                    es = "I";
2454
                    en = 'I';
2455
                    miss = &env->spr[SPR_IMISS];
2456
                    cmp = &env->spr[SPR_ICMP];
2457
                } else {
2458
                    if (excp == POWERPC_EXCP_DLTLB)
2459
                        es = "DL";
2460
                    else
2461
                        es = "DS";
2462
                    en = 'D';
2463
                    miss = &env->spr[SPR_DMISS];
2464
                    cmp = &env->spr[SPR_DCMP];
2465
                }
2466
                fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2467
                        " H1 " ADDRX " H2 " ADDRX " %08x\n",
2468
                        es, en, *miss, en, *cmp,
2469
                        env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2470
                        env->error_code);
2471
            }
2472
#endif
2473
            msr |= env->crf[0] << 28;
2474
            msr |= env->error_code; /* key, D/I, S/L bits */
2475
            /* Set way using a LRU mechanism */
2476
            msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2477
            break;
2478
        case POWERPC_EXCP_74xx:
2479
        tlb_miss_74xx:
2480
#if defined (DEBUG_SOFTWARE_TLB)
2481
            if (loglevel != 0) {
2482
                const unsigned char *es;
2483
                target_ulong *miss, *cmp;
2484
                int en;
2485
                if (excp == POWERPC_EXCP_IFTLB) {
2486
                    es = "I";
2487
                    en = 'I';
2488
                    miss = &env->spr[SPR_TLBMISS];
2489
                    cmp = &env->spr[SPR_PTEHI];
2490
                } else {
2491
                    if (excp == POWERPC_EXCP_DLTLB)
2492
                        es = "DL";
2493
                    else
2494
                        es = "DS";
2495
                    en = 'D';
2496
                    miss = &env->spr[SPR_TLBMISS];
2497
                    cmp = &env->spr[SPR_PTEHI];
2498
                }
2499
                fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2500
                        " %08x\n",
2501
                        es, en, *miss, en, *cmp, env->error_code);
2502
            }
2503
#endif
2504
            msr |= env->error_code; /* key bit */
2505
            break;
2506
        default:
2507
            cpu_abort(env, "Invalid data store TLB miss exception\n");
2508
            break;
2509
        }
2510
        goto store_next;
2511
    case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
2512
        /* XXX: TODO */
2513
        cpu_abort(env, "Floating point assist exception "
2514
                  "is not implemented yet !\n");
2515
        goto store_next;
2516
    case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
2517
        /* XXX: TODO */
2518
        cpu_abort(env, "IABR exception is not implemented yet !\n");
2519
        goto store_next;
2520
    case POWERPC_EXCP_SMI:       /* System management interrupt              */
2521
        /* XXX: TODO */
2522
        cpu_abort(env, "SMI exception is not implemented yet !\n");
2523
        goto store_next;
2524
    case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
2525
        /* XXX: TODO */
2526
        cpu_abort(env, "Thermal management exception "
2527
                  "is not implemented yet !\n");
2528
        goto store_next;
2529
    case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
2530
        new_msr &= ~((target_ulong)1 << MSR_RI);
2531
#if defined(TARGET_PPC64H)
2532
        if (lpes1 == 0)
2533
            new_msr |= (target_ulong)1 << MSR_HV;
2534
#endif
2535
        /* XXX: TODO */
2536
        cpu_abort(env,
2537
                  "Performance counter exception is not implemented yet !\n");
2538
        goto store_next;
2539
    case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
2540
        /* XXX: TODO */
2541
        cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2542
        goto store_next;
2543
    case POWERPC_EXCP_SOFTP:     /* Soft patch exception                     */
2544
        /* XXX: TODO */
2545
        cpu_abort(env,
2546
                  "970 soft-patch exception is not implemented yet !\n");
2547
        goto store_next;
2548
    case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
2549
        /* XXX: TODO */
2550
        cpu_abort(env,
2551
                  "970 maintenance exception is not implemented yet !\n");
2552
        goto store_next;
2553
    default:
2554
    excp_invalid:
2555
        cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2556
        break;
2557
    store_current:
2558
        /* save current instruction location */
2559
        env->spr[srr0] = env->nip - 4;
2560
        break;
2561
    store_next:
2562
        /* save next instruction location */
2563
        env->spr[srr0] = env->nip;
2564
        break;
2565
    }
2566
    /* Save MSR */
2567
    env->spr[srr1] = msr;
2568
    /* If any alternate SRR register are defined, duplicate saved values */
2569
    if (asrr0 != -1)
2570
        env->spr[asrr0] = env->spr[srr0];
2571
    if (asrr1 != -1)
2572
        env->spr[asrr1] = env->spr[srr1];
2573
    /* If we disactivated any translation, flush TLBs */
2574
    if (new_msr & ((1 << MSR_IR) | (1 << MSR_DR)))
2575
        tlb_flush(env, 1);
2576
    /* reload MSR with correct bits */
2577
    new_msr &= ~((target_ulong)1 << MSR_EE);
2578
    new_msr &= ~((target_ulong)1 << MSR_PR);
2579
    new_msr &= ~((target_ulong)1 << MSR_FP);
2580
    new_msr &= ~((target_ulong)1 << MSR_FE0);
2581
    new_msr &= ~((target_ulong)1 << MSR_SE);
2582
    new_msr &= ~((target_ulong)1 << MSR_BE);
2583
    new_msr &= ~((target_ulong)1 << MSR_FE1);
2584
    new_msr &= ~((target_ulong)1 << MSR_IR);
2585
    new_msr &= ~((target_ulong)1 << MSR_DR);
2586
#if 0 /* Fix this: not on all targets */
2587
    new_msr &= ~((target_ulong)1 << MSR_PMM);
2588
#endif
2589
    new_msr &= ~((target_ulong)1 << MSR_LE);
2590
    if (msr_ile)
2591
        new_msr |= (target_ulong)1 << MSR_LE;
2592
    else
2593
        new_msr &= ~((target_ulong)1 << MSR_LE);
2594
    /* Jump to handler */
2595
    vector = env->excp_vectors[excp];
2596
    if (vector == (target_ulong)-1) {
2597
        cpu_abort(env, "Raised an exception without defined vector %d\n",
2598
                  excp);
2599
    }
2600
    vector |= env->excp_prefix;
2601
#if defined(TARGET_PPC64)
2602
    if (excp_model == POWERPC_EXCP_BOOKE) {
2603
        if (!msr_icm) {
2604
            new_msr &= ~((target_ulong)1 << MSR_CM);
2605
            vector = (uint32_t)vector;
2606
        } else {
2607
            new_msr |= (target_ulong)1 << MSR_CM;
2608
        }
2609
    } else {
2610
        if (!msr_isf) {
2611
            new_msr &= ~((target_ulong)1 << MSR_SF);
2612
            vector = (uint32_t)vector;
2613
        } else {
2614
            new_msr |= (target_ulong)1 << MSR_SF;
2615
        }
2616
    }
2617
#endif
2618
    /* XXX: we don't use hreg_store_msr here as already have treated
2619
     *      any special case that could occur. Just store MSR and update hflags
2620
     */
2621
    env->msr = new_msr;
2622
    hreg_compute_hflags(env);
2623
    env->nip = vector;
2624
    /* Reset exception state */
2625
    env->exception_index = POWERPC_EXCP_NONE;
2626
    env->error_code = 0;
2627
}
2628

    
2629
void do_interrupt (CPUState *env)
2630
{
2631
    powerpc_excp(env, env->excp_model, env->exception_index);
2632
}
2633

    
2634
void ppc_hw_interrupt (CPUPPCState *env)
2635
{
2636
#if 0
2637
    if (loglevel & CPU_LOG_INT) {
2638
        fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2639
                __func__, env, env->pending_interrupts,
2640
                env->interrupt_request, (int)msr_me, (int)msr_ee);
2641
    }
2642
#endif
2643
    /* External reset */
2644
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2645
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2646
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2647
        return;
2648
    }
2649
    /* Machine check exception */
2650
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2651
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2652
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2653
        return;
2654
    }
2655
#if 0 /* TODO */
2656
    /* External debug exception */
2657
    if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2658
        env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2659
        powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2660
        return;
2661
    }
2662
#endif
2663
#if defined(TARGET_PPC64H)
2664
    if ((msr_ee != 0 || msr_hv == 0 || msr_pr != 0) & hdice != 0) {
2665
        /* Hypervisor decrementer exception */
2666
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2667
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2668
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2669
            return;
2670
        }
2671
    }
2672
#endif
2673
    if (msr_ce != 0) {
2674
        /* External critical interrupt */
2675
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2676
            /* Taking a critical external interrupt does not clear the external
2677
             * critical interrupt status
2678
             */
2679
#if 0
2680
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2681
#endif
2682
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2683
            return;
2684
        }
2685
    }
2686
    if (msr_ee != 0) {
2687
        /* Watchdog timer on embedded PowerPC */
2688
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2689
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2690
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2691
            return;
2692
        }
2693
#if defined(TARGET_PPCEMB)
2694
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2695
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2696
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2697
            return;
2698
        }
2699
#endif
2700
#if defined(TARGET_PPCEMB)
2701
        /* External interrupt */
2702
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2703
            /* Taking an external interrupt does not clear the external
2704
             * interrupt status
2705
             */
2706
#if 0
2707
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2708
#endif
2709
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2710
            return;
2711
        }
2712
#endif
2713
        /* Fixed interval timer on embedded PowerPC */
2714
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2715
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2716
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2717
            return;
2718
        }
2719
        /* Programmable interval timer on embedded PowerPC */
2720
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2721
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2722
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2723
            return;
2724
        }
2725
        /* Decrementer exception */
2726
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2727
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2728
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2729
            return;
2730
        }
2731
#if !defined(TARGET_PPCEMB)
2732
        /* External interrupt */
2733
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2734
            /* Taking an external interrupt does not clear the external
2735
             * interrupt status
2736
             */
2737
#if 0
2738
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2739
#endif
2740
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2741
            return;
2742
        }
2743
#endif
2744
#if defined(TARGET_PPCEMB)
2745
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2746
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2747
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2748
            return;
2749
        }
2750
#endif
2751
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2752
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2753
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2754
            return;
2755
        }
2756
        /* Thermal interrupt */
2757
        if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2758
            env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2759
            powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2760
            return;
2761
        }
2762
    }
2763
}
2764
#endif /* !CONFIG_USER_ONLY */
2765

    
2766
void cpu_dump_EA (target_ulong EA)
2767
{
2768
    FILE *f;
2769

    
2770
    if (logfile) {
2771
        f = logfile;
2772
    } else {
2773
        f = stdout;
2774
        return;
2775
    }
2776
    fprintf(f, "Memory access at address " ADDRX "\n", EA);
2777
}
2778

    
2779
void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2780
{
2781
    FILE *f;
2782

    
2783
    if (logfile) {
2784
        f = logfile;
2785
    } else {
2786
        f = stdout;
2787
        return;
2788
    }
2789
    fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2790
            RA, msr);
2791
}
2792

    
2793
void cpu_ppc_reset (void *opaque)
2794
{
2795
    CPUPPCState *env;
2796
    target_ulong msr;
2797

    
2798
    env = opaque;
2799
    msr = (target_ulong)0;
2800
#if defined(TARGET_PPC64)
2801
    msr |= (target_ulong)0 << MSR_HV; /* Should be 1... */
2802
#endif
2803
    msr |= (target_ulong)0 << MSR_AP; /* TO BE CHECKED */
2804
    msr |= (target_ulong)0 << MSR_SA; /* TO BE CHECKED */
2805
    msr |= (target_ulong)1 << MSR_EP;
2806
#if defined (DO_SINGLE_STEP) && 0
2807
    /* Single step trace mode */
2808
    msr |= (target_ulong)1 << MSR_SE;
2809
    msr |= (target_ulong)1 << MSR_BE;
2810
#endif
2811
#if defined(CONFIG_USER_ONLY)
2812
    msr |= (target_ulong)1 << MSR_FP; /* Allow floating point usage */
2813
    msr |= (target_ulong)1 << MSR_PR;
2814
#else
2815
    env->nip = env->hreset_vector | env->excp_prefix;
2816
    if (env->mmu_model != POWERPC_MMU_REAL_4xx)
2817
        ppc_tlb_invalidate_all(env);
2818
#endif
2819
    env->msr = msr;
2820
    hreg_compute_hflags(env);
2821
    env->reserve = -1;
2822
    /* Be sure no exception or interrupt is pending */
2823
    env->pending_interrupts = 0;
2824
    env->exception_index = POWERPC_EXCP_NONE;
2825
    env->error_code = 0;
2826
    /* Flush all TLBs */
2827
    tlb_flush(env, 1);
2828
}
2829

    
2830
CPUPPCState *cpu_ppc_init (void)
2831
{
2832
    CPUPPCState *env;
2833

    
2834
    env = qemu_mallocz(sizeof(CPUPPCState));
2835
    if (!env)
2836
        return NULL;
2837
    cpu_exec_init(env);
2838

    
2839
    return env;
2840
}
2841

    
2842
void cpu_ppc_close (CPUPPCState *env)
2843
{
2844
    /* Should also remove all opcode tables... */
2845
    free(env);
2846
}