Make PowerPC hypervisor resources able to compile, even if not enabled for now.
[qemu] / target-ppc / helper.c
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 #if defined(TARGET_PPC64H)
2006     int lpes0, lpes1, lev;
2007
2008     lpes0 = (env->spr[SPR_LPCR] >> 1) & 1;
2009     lpes1 = (env->spr[SPR_LPCR] >> 2) & 1;
2010 #endif
2011
2012     if (loglevel & CPU_LOG_INT) {
2013         fprintf(logfile, "Raise exception at 0x" ADDRX " => 0x%08x (%02x)\n",
2014                 env->nip, excp, env->error_code);
2015     }
2016     msr = env->msr;
2017     new_msr = msr;
2018     srr0 = SPR_SRR0;
2019     srr1 = SPR_SRR1;
2020     asrr0 = -1;
2021     asrr1 = -1;
2022     msr &= ~((target_ulong)0x783F0000);
2023     switch (excp) {
2024     case POWERPC_EXCP_NONE:
2025         /* Should never happen */
2026         return;
2027     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
2028         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2029         switch (excp_model) {
2030         case POWERPC_EXCP_40x:
2031             srr0 = SPR_40x_SRR2;
2032             srr1 = SPR_40x_SRR3;
2033             break;
2034         case POWERPC_EXCP_BOOKE:
2035             srr0 = SPR_BOOKE_CSRR0;
2036             srr1 = SPR_BOOKE_CSRR1;
2037             break;
2038         case POWERPC_EXCP_G2:
2039             break;
2040         default:
2041             goto excp_invalid;
2042         }
2043         goto store_next;
2044     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
2045         if (msr_me == 0) {
2046             /* Machine check exception is not enabled.
2047              * Enter checkstop state.
2048              */
2049             if (loglevel != 0) {
2050                 fprintf(logfile, "Machine check while not allowed. "
2051                         "Entering checkstop state\n");
2052             } else {
2053                 fprintf(stderr, "Machine check while not allowed. "
2054                         "Entering checkstop state\n");
2055             }
2056             env->halted = 1;
2057             env->interrupt_request |= CPU_INTERRUPT_EXITTB;
2058         }
2059         new_msr &= ~((target_ulong)1 << MSR_RI);
2060         new_msr &= ~((target_ulong)1 << MSR_ME);
2061 #if defined(TARGET_PPC64H)
2062         new_msr |= (target_ulong)1 << MSR_HV;
2063 #endif
2064         /* XXX: should also have something loaded in DAR / DSISR */
2065         switch (excp_model) {
2066         case POWERPC_EXCP_40x:
2067             srr0 = SPR_40x_SRR2;
2068             srr1 = SPR_40x_SRR3;
2069             break;
2070         case POWERPC_EXCP_BOOKE:
2071             srr0 = SPR_BOOKE_MCSRR0;
2072             srr1 = SPR_BOOKE_MCSRR1;
2073             asrr0 = SPR_BOOKE_CSRR0;
2074             asrr1 = SPR_BOOKE_CSRR1;
2075             break;
2076         default:
2077             break;
2078         }
2079         goto store_next;
2080     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
2081 #if defined (DEBUG_EXCEPTIONS)
2082         if (loglevel != 0) {
2083             fprintf(logfile, "DSI exception: DSISR=0x" ADDRX" DAR=0x" ADDRX
2084                     "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
2085         }
2086 #endif
2087         new_msr &= ~((target_ulong)1 << MSR_RI);
2088 #if defined(TARGET_PPC64H)
2089         if (lpes1 == 0)
2090             new_msr |= (target_ulong)1 << MSR_HV;
2091 #endif
2092         goto store_next;
2093     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
2094 #if defined (DEBUG_EXCEPTIONS)
2095         if (loglevel != 0) {
2096             fprintf(logfile, "ISI exception: msr=0x" ADDRX ", nip=0x" ADDRX
2097                     "\n", msr, env->nip);
2098         }
2099 #endif
2100         new_msr &= ~((target_ulong)1 << MSR_RI);
2101 #if defined(TARGET_PPC64H)
2102         if (lpes1 == 0)
2103             new_msr |= (target_ulong)1 << MSR_HV;
2104 #endif
2105         msr |= env->error_code;
2106         goto store_next;
2107     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
2108         new_msr &= ~((target_ulong)1 << MSR_RI);
2109 #if defined(TARGET_PPC64H)
2110         if (lpes0 == 1)
2111             new_msr |= (target_ulong)1 << MSR_HV;
2112 #endif
2113         goto store_next;
2114     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
2115         new_msr &= ~((target_ulong)1 << MSR_RI);
2116 #if defined(TARGET_PPC64H)
2117         if (lpes1 == 0)
2118             new_msr |= (target_ulong)1 << MSR_HV;
2119 #endif
2120         /* XXX: this is false */
2121         /* Get rS/rD and rA from faulting opcode */
2122         env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
2123         goto store_current;
2124     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
2125         switch (env->error_code & ~0xF) {
2126         case POWERPC_EXCP_FP:
2127             if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
2128 #if defined (DEBUG_EXCEPTIONS)
2129                 if (loglevel != 0) {
2130                     fprintf(logfile, "Ignore floating point exception\n");
2131                 }
2132 #endif
2133                 return;
2134             }
2135             new_msr &= ~((target_ulong)1 << MSR_RI);
2136 #if defined(TARGET_PPC64H)
2137             if (lpes1 == 0)
2138                 new_msr |= (target_ulong)1 << MSR_HV;
2139 #endif
2140             msr |= 0x00100000;
2141             /* Set FX */
2142             env->fpscr[7] |= 0x8;
2143             /* Finally, update FEX */
2144             if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
2145                 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
2146                 env->fpscr[7] |= 0x4;
2147             if (msr_fe0 != msr_fe1) {
2148                 msr |= 0x00010000;
2149                 goto store_current;
2150             }
2151             break;
2152         case POWERPC_EXCP_INVAL:
2153 #if defined (DEBUG_EXCEPTIONS)
2154             if (loglevel != 0) {
2155                 fprintf(logfile, "Invalid instruction at 0x" ADDRX "\n",
2156                         env->nip);
2157             }
2158 #endif
2159             new_msr &= ~((target_ulong)1 << MSR_RI);
2160 #if defined(TARGET_PPC64H)
2161             if (lpes1 == 0)
2162                 new_msr |= (target_ulong)1 << MSR_HV;
2163 #endif
2164             msr |= 0x00080000;
2165             break;
2166         case POWERPC_EXCP_PRIV:
2167             new_msr &= ~((target_ulong)1 << MSR_RI);
2168 #if defined(TARGET_PPC64H)
2169             if (lpes1 == 0)
2170                 new_msr |= (target_ulong)1 << MSR_HV;
2171 #endif
2172             msr |= 0x00040000;
2173             break;
2174         case POWERPC_EXCP_TRAP:
2175             new_msr &= ~((target_ulong)1 << MSR_RI);
2176 #if defined(TARGET_PPC64H)
2177             if (lpes1 == 0)
2178                 new_msr |= (target_ulong)1 << MSR_HV;
2179 #endif
2180             msr |= 0x00020000;
2181             break;
2182         default:
2183             /* Should never occur */
2184             cpu_abort(env, "Invalid program exception %d. Aborting\n",
2185                       env->error_code);
2186             break;
2187         }
2188         goto store_next;
2189     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
2190         new_msr &= ~((target_ulong)1 << MSR_RI);
2191 #if defined(TARGET_PPC64H)
2192         if (lpes1 == 0)
2193             new_msr |= (target_ulong)1 << MSR_HV;
2194 #endif
2195         goto store_current;
2196     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
2197         /* NOTE: this is a temporary hack to support graphics OSI
2198            calls from the MOL driver */
2199         /* XXX: To be removed */
2200         if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2201             env->osi_call) {
2202             if (env->osi_call(env) != 0)
2203                 return;
2204         }
2205         if (loglevel & CPU_LOG_INT) {
2206             dump_syscall(env);
2207         }
2208         new_msr &= ~((target_ulong)1 << MSR_RI);
2209 #if defined(TARGET_PPC64H)
2210         lev = env->error_code;
2211         if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
2212             new_msr |= (target_ulong)1 << MSR_HV;
2213 #endif
2214         goto store_next;
2215     case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
2216         new_msr &= ~((target_ulong)1 << MSR_RI);
2217         goto store_current;
2218     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
2219         new_msr &= ~((target_ulong)1 << MSR_RI);
2220 #if defined(TARGET_PPC64H)
2221         if (lpes1 == 0)
2222             new_msr |= (target_ulong)1 << MSR_HV;
2223 #endif
2224         goto store_next;
2225     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
2226         /* FIT on 4xx */
2227 #if defined (DEBUG_EXCEPTIONS)
2228         if (loglevel != 0)
2229             fprintf(logfile, "FIT exception\n");
2230 #endif
2231         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2232         goto store_next;
2233     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
2234 #if defined (DEBUG_EXCEPTIONS)
2235         if (loglevel != 0)
2236             fprintf(logfile, "WDT exception\n");
2237 #endif
2238         switch (excp_model) {
2239         case POWERPC_EXCP_BOOKE:
2240             srr0 = SPR_BOOKE_CSRR0;
2241             srr1 = SPR_BOOKE_CSRR1;
2242             break;
2243         default:
2244             break;
2245         }
2246         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2247         goto store_next;
2248     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
2249         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2250         goto store_next;
2251     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
2252         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2253         goto store_next;
2254     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
2255         switch (excp_model) {
2256         case POWERPC_EXCP_BOOKE:
2257             srr0 = SPR_BOOKE_DSRR0;
2258             srr1 = SPR_BOOKE_DSRR1;
2259             asrr0 = SPR_BOOKE_CSRR0;
2260             asrr1 = SPR_BOOKE_CSRR1;
2261             break;
2262         default:
2263             break;
2264         }
2265         /* XXX: TODO */
2266         cpu_abort(env, "Debug exception is not implemented yet !\n");
2267         goto store_next;
2268 #if defined(TARGET_PPCEMB)
2269     case POWERPC_EXCP_SPEU:      /* SPE/embedded floating-point unavailable  */
2270         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2271         goto store_current;
2272     case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
2273         /* XXX: TODO */
2274         cpu_abort(env, "Embedded floating point data exception "
2275                   "is not implemented yet !\n");
2276         goto store_next;
2277     case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
2278         /* XXX: TODO */
2279         cpu_abort(env, "Embedded floating point round exception "
2280                   "is not implemented yet !\n");
2281         goto store_next;
2282     case POWERPC_EXCP_EPERFM:    /* Embedded performance monitor interrupt   */
2283         new_msr &= ~((target_ulong)1 << MSR_RI);
2284         /* XXX: TODO */
2285         cpu_abort(env,
2286                   "Performance counter exception is not implemented yet !\n");
2287         goto store_next;
2288     case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
2289         /* XXX: TODO */
2290         cpu_abort(env,
2291                   "Embedded doorbell interrupt is not implemented yet !\n");
2292         goto store_next;
2293     case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
2294         switch (excp_model) {
2295         case POWERPC_EXCP_BOOKE:
2296             srr0 = SPR_BOOKE_CSRR0;
2297             srr1 = SPR_BOOKE_CSRR1;
2298             break;
2299         default:
2300             break;
2301         }
2302         /* XXX: TODO */
2303         cpu_abort(env, "Embedded doorbell critical interrupt "
2304                   "is not implemented yet !\n");
2305         goto store_next;
2306 #endif /* defined(TARGET_PPCEMB) */
2307     case POWERPC_EXCP_RESET:     /* System reset exception                   */
2308         new_msr &= ~((target_ulong)1 << MSR_RI);
2309 #if defined(TARGET_PPC64H)
2310         new_msr |= (target_ulong)1 << MSR_HV;
2311 #endif
2312         goto store_next;
2313 #if defined(TARGET_PPC64)
2314     case POWERPC_EXCP_DSEG:      /* Data 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     case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
2322         new_msr &= ~((target_ulong)1 << MSR_RI);
2323 #if defined(TARGET_PPC64H)
2324         if (lpes1 == 0)
2325             new_msr |= (target_ulong)1 << MSR_HV;
2326 #endif
2327         goto store_next;
2328 #endif /* defined(TARGET_PPC64) */
2329 #if defined(TARGET_PPC64H)
2330     case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
2331         srr0 = SPR_HSRR0;
2332         srr1 = SPR_HSRR1;
2333         new_msr |= (target_ulong)1 << MSR_HV;
2334         goto store_next;
2335 #endif
2336     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
2337         new_msr &= ~((target_ulong)1 << MSR_RI);
2338 #if defined(TARGET_PPC64H)
2339         if (lpes1 == 0)
2340             new_msr |= (target_ulong)1 << MSR_HV;
2341 #endif
2342         goto store_next;
2343 #if defined(TARGET_PPC64H)
2344     case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
2345         srr0 = SPR_HSRR0;
2346         srr1 = SPR_HSRR1;
2347         new_msr |= (target_ulong)1 << MSR_HV;
2348         goto store_next;
2349     case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
2350         srr0 = SPR_HSRR0;
2351         srr1 = SPR_HSRR1;
2352         new_msr |= (target_ulong)1 << MSR_HV;
2353         goto store_next;
2354     case POWERPC_EXCP_HDSEG:     /* Hypervisor data segment exception        */
2355         srr0 = SPR_HSRR0;
2356         srr1 = SPR_HSRR1;
2357         new_msr |= (target_ulong)1 << MSR_HV;
2358         goto store_next;
2359     case POWERPC_EXCP_HISEG:     /* Hypervisor instruction segment exception */
2360         srr0 = SPR_HSRR0;
2361         srr1 = SPR_HSRR1;
2362         new_msr |= (target_ulong)1 << MSR_HV;
2363         goto store_next;
2364 #endif /* defined(TARGET_PPC64H) */
2365     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
2366         new_msr &= ~((target_ulong)1 << MSR_RI);
2367 #if defined(TARGET_PPC64H)
2368         if (lpes1 == 0)
2369             new_msr |= (target_ulong)1 << MSR_HV;
2370 #endif
2371         goto store_current;
2372     case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
2373 #if defined (DEBUG_EXCEPTIONS)
2374         if (loglevel != 0)
2375             fprintf(logfile, "PIT exception\n");
2376 #endif
2377         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2378         goto store_next;
2379     case POWERPC_EXCP_IO:        /* IO error exception                       */
2380         /* XXX: TODO */
2381         cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2382         goto store_next;
2383     case POWERPC_EXCP_RUNM:      /* Run mode exception                       */
2384         /* XXX: TODO */
2385         cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2386         goto store_next;
2387     case POWERPC_EXCP_EMUL:      /* Emulation trap exception                 */
2388         /* XXX: TODO */
2389         cpu_abort(env, "602 emulation trap exception "
2390                   "is not implemented yet !\n");
2391         goto store_next;
2392     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
2393         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2394 #if defined(TARGET_PPC64H) /* XXX: check this */
2395         if (lpes1 == 0)
2396             new_msr |= (target_ulong)1 << MSR_HV;
2397 #endif
2398         switch (excp_model) {
2399         case POWERPC_EXCP_602:
2400         case POWERPC_EXCP_603:
2401         case POWERPC_EXCP_603E:
2402         case POWERPC_EXCP_G2:
2403             goto tlb_miss_tgpr;
2404         case POWERPC_EXCP_7x5:
2405             goto tlb_miss;
2406         case POWERPC_EXCP_74xx:
2407             goto tlb_miss_74xx;
2408         default:
2409             cpu_abort(env, "Invalid instruction TLB miss exception\n");
2410             break;
2411         }
2412         break;
2413     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
2414         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2415 #if defined(TARGET_PPC64H) /* XXX: check this */
2416         if (lpes1 == 0)
2417             new_msr |= (target_ulong)1 << MSR_HV;
2418 #endif
2419         switch (excp_model) {
2420         case POWERPC_EXCP_602:
2421         case POWERPC_EXCP_603:
2422         case POWERPC_EXCP_603E:
2423         case POWERPC_EXCP_G2:
2424             goto tlb_miss_tgpr;
2425         case POWERPC_EXCP_7x5:
2426             goto tlb_miss;
2427         case POWERPC_EXCP_74xx:
2428             goto tlb_miss_74xx;
2429         default:
2430             cpu_abort(env, "Invalid data load TLB miss exception\n");
2431             break;
2432         }
2433         break;
2434     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
2435         new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2436 #if defined(TARGET_PPC64H) /* XXX: check this */
2437         if (lpes1 == 0)
2438             new_msr |= (target_ulong)1 << MSR_HV;
2439 #endif
2440         switch (excp_model) {
2441         case POWERPC_EXCP_602:
2442         case POWERPC_EXCP_603:
2443         case POWERPC_EXCP_603E:
2444         case POWERPC_EXCP_G2:
2445         tlb_miss_tgpr:
2446             /* Swap temporary saved registers with GPRs */
2447             if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
2448                 new_msr |= (target_ulong)1 << MSR_TGPR;
2449                 hreg_swap_gpr_tgpr(env);
2450             }
2451             goto tlb_miss;
2452         case POWERPC_EXCP_7x5:
2453         tlb_miss:
2454 #if defined (DEBUG_SOFTWARE_TLB)
2455             if (loglevel != 0) {
2456                 const unsigned char *es;
2457                 target_ulong *miss, *cmp;
2458                 int en;
2459                 if (excp == POWERPC_EXCP_IFTLB) {
2460                     es = "I";
2461                     en = 'I';
2462                     miss = &env->spr[SPR_IMISS];
2463                     cmp = &env->spr[SPR_ICMP];
2464                 } else {
2465                     if (excp == POWERPC_EXCP_DLTLB)
2466                         es = "DL";
2467                     else
2468                         es = "DS";
2469                     en = 'D';
2470                     miss = &env->spr[SPR_DMISS];
2471                     cmp = &env->spr[SPR_DCMP];
2472                 }
2473                 fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2474                         " H1 " ADDRX " H2 " ADDRX " %08x\n",
2475                         es, en, *miss, en, *cmp,
2476                         env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2477                         env->error_code);
2478             }
2479 #endif
2480             msr |= env->crf[0] << 28;
2481             msr |= env->error_code; /* key, D/I, S/L bits */
2482             /* Set way using a LRU mechanism */
2483             msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2484             break;
2485         case POWERPC_EXCP_74xx:
2486         tlb_miss_74xx:
2487 #if defined (DEBUG_SOFTWARE_TLB)
2488             if (loglevel != 0) {
2489                 const unsigned char *es;
2490                 target_ulong *miss, *cmp;
2491                 int en;
2492                 if (excp == POWERPC_EXCP_IFTLB) {
2493                     es = "I";
2494                     en = 'I';
2495                     miss = &env->spr[SPR_TLBMISS];
2496                     cmp = &env->spr[SPR_PTEHI];
2497                 } else {
2498                     if (excp == POWERPC_EXCP_DLTLB)
2499                         es = "DL";
2500                     else
2501                         es = "DS";
2502                     en = 'D';
2503                     miss = &env->spr[SPR_TLBMISS];
2504                     cmp = &env->spr[SPR_PTEHI];
2505                 }
2506                 fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2507                         " %08x\n",
2508                         es, en, *miss, en, *cmp, env->error_code);
2509             }
2510 #endif
2511             msr |= env->error_code; /* key bit */
2512             break;
2513         default:
2514             cpu_abort(env, "Invalid data store TLB miss exception\n");
2515             break;
2516         }
2517         goto store_next;
2518     case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
2519         /* XXX: TODO */
2520         cpu_abort(env, "Floating point assist exception "
2521                   "is not implemented yet !\n");
2522         goto store_next;
2523     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
2524         /* XXX: TODO */
2525         cpu_abort(env, "IABR exception is not implemented yet !\n");
2526         goto store_next;
2527     case POWERPC_EXCP_SMI:       /* System management interrupt              */
2528         /* XXX: TODO */
2529         cpu_abort(env, "SMI exception is not implemented yet !\n");
2530         goto store_next;
2531     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
2532         /* XXX: TODO */
2533         cpu_abort(env, "Thermal management exception "
2534                   "is not implemented yet !\n");
2535         goto store_next;
2536     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
2537         new_msr &= ~((target_ulong)1 << MSR_RI);
2538 #if defined(TARGET_PPC64H)
2539         if (lpes1 == 0)
2540             new_msr |= (target_ulong)1 << MSR_HV;
2541 #endif
2542         /* XXX: TODO */
2543         cpu_abort(env,
2544                   "Performance counter exception is not implemented yet !\n");
2545         goto store_next;
2546     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
2547         /* XXX: TODO */
2548         cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2549         goto store_next;
2550     case POWERPC_EXCP_SOFTP:     /* Soft patch exception                     */
2551         /* XXX: TODO */
2552         cpu_abort(env,
2553                   "970 soft-patch exception is not implemented yet !\n");
2554         goto store_next;
2555     case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
2556         /* XXX: TODO */
2557         cpu_abort(env,
2558                   "970 maintenance exception is not implemented yet !\n");
2559         goto store_next;
2560     default:
2561     excp_invalid:
2562         cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2563         break;
2564     store_current:
2565         /* save current instruction location */
2566         env->spr[srr0] = env->nip - 4;
2567         break;
2568     store_next:
2569         /* save next instruction location */
2570         env->spr[srr0] = env->nip;
2571         break;
2572     }
2573     /* Save MSR */
2574     env->spr[srr1] = msr;
2575     /* If any alternate SRR register are defined, duplicate saved values */
2576     if (asrr0 != -1)
2577         env->spr[asrr0] = env->spr[srr0];
2578     if (asrr1 != -1)
2579         env->spr[asrr1] = env->spr[srr1];
2580     /* If we disactivated any translation, flush TLBs */
2581     if (new_msr & ((1 << MSR_IR) | (1 << MSR_DR)))
2582         tlb_flush(env, 1);
2583     /* reload MSR with correct bits */
2584     new_msr &= ~((target_ulong)1 << MSR_EE);
2585     new_msr &= ~((target_ulong)1 << MSR_PR);
2586     new_msr &= ~((target_ulong)1 << MSR_FP);
2587     new_msr &= ~((target_ulong)1 << MSR_FE0);
2588     new_msr &= ~((target_ulong)1 << MSR_SE);
2589     new_msr &= ~((target_ulong)1 << MSR_BE);
2590     new_msr &= ~((target_ulong)1 << MSR_FE1);
2591     new_msr &= ~((target_ulong)1 << MSR_IR);
2592     new_msr &= ~((target_ulong)1 << MSR_DR);
2593 #if 0 /* Fix this: not on all targets */
2594     new_msr &= ~((target_ulong)1 << MSR_PMM);
2595 #endif
2596     new_msr &= ~((target_ulong)1 << MSR_LE);
2597     if (msr_ile)
2598         new_msr |= (target_ulong)1 << MSR_LE;
2599     else
2600         new_msr &= ~((target_ulong)1 << MSR_LE);
2601     /* Jump to handler */
2602     vector = env->excp_vectors[excp];
2603     if (vector == (target_ulong)-1) {
2604         cpu_abort(env, "Raised an exception without defined vector %d\n",
2605                   excp);
2606     }
2607     vector |= env->excp_prefix;
2608 #if defined(TARGET_PPC64)
2609     if (excp_model == POWERPC_EXCP_BOOKE) {
2610         if (!msr_icm) {
2611             new_msr &= ~((target_ulong)1 << MSR_CM);
2612             vector = (uint32_t)vector;
2613         } else {
2614             new_msr |= (target_ulong)1 << MSR_CM;
2615         }
2616     } else {
2617         if (!msr_isf) {
2618             new_msr &= ~((target_ulong)1 << MSR_SF);
2619             vector = (uint32_t)vector;
2620         } else {
2621             new_msr |= (target_ulong)1 << MSR_SF;
2622         }
2623     }
2624 #endif
2625     /* XXX: we don't use hreg_store_msr here as already have treated
2626      *      any special case that could occur. Just store MSR and update hflags
2627      */
2628     env->msr = new_msr;
2629     hreg_compute_hflags(env);
2630     env->nip = vector;
2631     /* Reset exception state */
2632     env->exception_index = POWERPC_EXCP_NONE;
2633     env->error_code = 0;
2634 }
2635
2636 void do_interrupt (CPUState *env)
2637 {
2638     powerpc_excp(env, env->excp_model, env->exception_index);
2639 }
2640
2641 void ppc_hw_interrupt (CPUPPCState *env)
2642 {
2643 #if defined(TARGET_PPC64H)
2644     int hdice;
2645 #endif
2646
2647 #if 0
2648     if (loglevel & CPU_LOG_INT) {
2649         fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2650                 __func__, env, env->pending_interrupts,
2651                 env->interrupt_request, (int)msr_me, (int)msr_ee);
2652     }
2653 #endif
2654     /* External reset */
2655     if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
2656         env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
2657         powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2658         return;
2659     }
2660     /* Machine check exception */
2661     if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2662         env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2663         powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2664         return;
2665     }
2666 #if 0 /* TODO */
2667     /* External debug exception */
2668     if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2669         env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2670         powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2671         return;
2672     }
2673 #endif
2674 #if defined(TARGET_PPC64H)
2675     hdice = env->spr[SPR_LPCR] & 1;
2676     if ((msr_ee != 0 || msr_hv == 0 || msr_pr != 0) && hdice != 0) {
2677         /* Hypervisor decrementer exception */
2678         if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
2679             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
2680             powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2681             return;
2682         }
2683     }
2684 #endif
2685     if (msr_ce != 0) {
2686         /* External critical interrupt */
2687         if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2688             /* Taking a critical external interrupt does not clear the external
2689              * critical interrupt status
2690              */
2691 #if 0
2692             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
2693 #endif
2694             powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2695             return;
2696         }
2697     }
2698     if (msr_ee != 0) {
2699         /* Watchdog timer on embedded PowerPC */
2700         if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2701             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2702             powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2703             return;
2704         }
2705 #if defined(TARGET_PPCEMB)
2706         if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2707             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2708             powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2709             return;
2710         }
2711 #endif
2712 #if defined(TARGET_PPCEMB)
2713         /* External interrupt */
2714         if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2715             /* Taking an external interrupt does not clear the external
2716              * interrupt status
2717              */
2718 #if 0
2719             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2720 #endif
2721             powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2722             return;
2723         }
2724 #endif
2725         /* Fixed interval timer on embedded PowerPC */
2726         if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2727             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2728             powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2729             return;
2730         }
2731         /* Programmable interval timer on embedded PowerPC */
2732         if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2733             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2734             powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2735             return;
2736         }
2737         /* Decrementer exception */
2738         if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
2739             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
2740             powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2741             return;
2742         }
2743 #if !defined(TARGET_PPCEMB)
2744         /* External interrupt */
2745         if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
2746             /* Taking an external interrupt does not clear the external
2747              * interrupt status
2748              */
2749 #if 0
2750             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
2751 #endif
2752             powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2753             return;
2754         }
2755 #endif
2756 #if defined(TARGET_PPCEMB)
2757         if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2758             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2759             powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2760             return;
2761         }
2762 #endif
2763         if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2764             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2765             powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2766             return;
2767         }
2768         /* Thermal interrupt */
2769         if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2770             env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2771             powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2772             return;
2773         }
2774     }
2775 }
2776 #endif /* !CONFIG_USER_ONLY */
2777
2778 void cpu_dump_EA (target_ulong EA)
2779 {
2780     FILE *f;
2781
2782     if (logfile) {
2783         f = logfile;
2784     } else {
2785         f = stdout;
2786         return;
2787     }
2788     fprintf(f, "Memory access at address " ADDRX "\n", EA);
2789 }
2790
2791 void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2792 {
2793     FILE *f;
2794
2795     if (logfile) {
2796         f = logfile;
2797     } else {
2798         f = stdout;
2799         return;
2800     }
2801     fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2802             RA, msr);
2803 }
2804
2805 void cpu_ppc_reset (void *opaque)
2806 {
2807     CPUPPCState *env;
2808     target_ulong msr;
2809
2810     env = opaque;
2811     msr = (target_ulong)0;
2812 #if defined(TARGET_PPC64)
2813     msr |= (target_ulong)0 << MSR_HV; /* Should be 1... */
2814 #endif
2815     msr |= (target_ulong)0 << MSR_AP; /* TO BE CHECKED */
2816     msr |= (target_ulong)0 << MSR_SA; /* TO BE CHECKED */
2817     msr |= (target_ulong)1 << MSR_EP;
2818 #if defined (DO_SINGLE_STEP) && 0
2819     /* Single step trace mode */
2820     msr |= (target_ulong)1 << MSR_SE;
2821     msr |= (target_ulong)1 << MSR_BE;
2822 #endif
2823 #if defined(CONFIG_USER_ONLY)
2824     msr |= (target_ulong)1 << MSR_FP; /* Allow floating point usage */
2825     msr |= (target_ulong)1 << MSR_PR;
2826 #else
2827     env->nip = env->hreset_vector | env->excp_prefix;
2828     if (env->mmu_model != POWERPC_MMU_REAL_4xx)
2829         ppc_tlb_invalidate_all(env);
2830 #endif
2831     env->msr = msr;
2832     hreg_compute_hflags(env);
2833     env->reserve = -1;
2834     /* Be sure no exception or interrupt is pending */
2835     env->pending_interrupts = 0;
2836     env->exception_index = POWERPC_EXCP_NONE;
2837     env->error_code = 0;
2838     /* Flush all TLBs */
2839     tlb_flush(env, 1);
2840 }
2841
2842 CPUPPCState *cpu_ppc_init (void)
2843 {
2844     CPUPPCState *env;
2845
2846     env = qemu_mallocz(sizeof(CPUPPCState));
2847     if (!env)
2848         return NULL;
2849     cpu_exec_init(env);
2850
2851     return env;
2852 }
2853
2854 void cpu_ppc_close (CPUPPCState *env)
2855 {
2856     /* Should also remove all opcode tables... */
2857     free(env);
2858 }