xref: /openbmc/qemu/target/ppc/mmu_helper.c (revision 5f88dd43)
1 /*
2  *  PowerPC MMU, TLB, SLB and BAT 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.1 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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/units.h"
22 #include "cpu.h"
23 #include "sysemu/kvm.h"
24 #include "kvm_ppc.h"
25 #include "mmu-hash64.h"
26 #include "mmu-hash32.h"
27 #include "exec/exec-all.h"
28 #include "exec/log.h"
29 #include "helper_regs.h"
30 #include "qemu/error-report.h"
31 #include "qemu/qemu-print.h"
32 #include "internal.h"
33 #include "mmu-book3s-v3.h"
34 #include "mmu-radix64.h"
35 #include "exec/helper-proto.h"
36 #include "exec/cpu_ldst.h"
37 
38 /* #define FLUSH_ALL_TLBS */
39 
40 /*****************************************************************************/
41 /* PowerPC MMU emulation */
42 
43 /* Software driven TLB helpers */
44 static inline void ppc6xx_tlb_invalidate_all(CPUPPCState *env)
45 {
46     ppc6xx_tlb_t *tlb;
47     int nr, max;
48 
49     /* LOG_SWTLB("Invalidate all TLBs\n"); */
50     /* Invalidate all defined software TLB */
51     max = env->nb_tlb;
52     if (env->id_tlbs == 1) {
53         max *= 2;
54     }
55     for (nr = 0; nr < max; nr++) {
56         tlb = &env->tlb.tlb6[nr];
57         pte_invalidate(&tlb->pte0);
58     }
59     tlb_flush(env_cpu(env));
60 }
61 
62 static inline void ppc6xx_tlb_invalidate_virt2(CPUPPCState *env,
63                                                target_ulong eaddr,
64                                                int is_code, int match_epn)
65 {
66 #if !defined(FLUSH_ALL_TLBS)
67     CPUState *cs = env_cpu(env);
68     ppc6xx_tlb_t *tlb;
69     int way, nr;
70 
71     /* Invalidate ITLB + DTLB, all ways */
72     for (way = 0; way < env->nb_ways; way++) {
73         nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
74         tlb = &env->tlb.tlb6[nr];
75         if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
76             qemu_log_mask(CPU_LOG_MMU, "TLB invalidate %d/%d "
77                           TARGET_FMT_lx "\n", nr, env->nb_tlb, eaddr);
78             pte_invalidate(&tlb->pte0);
79             tlb_flush_page(cs, tlb->EPN);
80         }
81     }
82 #else
83     /* XXX: PowerPC specification say this is valid as well */
84     ppc6xx_tlb_invalidate_all(env);
85 #endif
86 }
87 
88 static inline void ppc6xx_tlb_invalidate_virt(CPUPPCState *env,
89                                               target_ulong eaddr, int is_code)
90 {
91     ppc6xx_tlb_invalidate_virt2(env, eaddr, is_code, 0);
92 }
93 
94 static void ppc6xx_tlb_store(CPUPPCState *env, target_ulong EPN, int way,
95                              int is_code, target_ulong pte0, target_ulong pte1)
96 {
97     ppc6xx_tlb_t *tlb;
98     int nr;
99 
100     nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
101     tlb = &env->tlb.tlb6[nr];
102     qemu_log_mask(CPU_LOG_MMU, "Set TLB %d/%d EPN " TARGET_FMT_lx " PTE0 "
103                   TARGET_FMT_lx " PTE1 " TARGET_FMT_lx "\n", nr, env->nb_tlb,
104                   EPN, pte0, pte1);
105     /* Invalidate any pending reference in QEMU for this virtual address */
106     ppc6xx_tlb_invalidate_virt2(env, EPN, is_code, 1);
107     tlb->pte0 = pte0;
108     tlb->pte1 = pte1;
109     tlb->EPN = EPN;
110     /* Store last way for LRU mechanism */
111     env->last_way = way;
112 }
113 
114 /* Helpers specific to PowerPC 40x implementations */
115 static inline void ppc4xx_tlb_invalidate_all(CPUPPCState *env)
116 {
117     ppcemb_tlb_t *tlb;
118     int i;
119 
120     for (i = 0; i < env->nb_tlb; i++) {
121         tlb = &env->tlb.tlbe[i];
122         tlb->prot &= ~PAGE_VALID;
123     }
124     tlb_flush(env_cpu(env));
125 }
126 
127 static void booke206_flush_tlb(CPUPPCState *env, int flags,
128                                const int check_iprot)
129 {
130     int tlb_size;
131     int i, j;
132     ppcmas_tlb_t *tlb = env->tlb.tlbm;
133 
134     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
135         if (flags & (1 << i)) {
136             tlb_size = booke206_tlb_size(env, i);
137             for (j = 0; j < tlb_size; j++) {
138                 if (!check_iprot || !(tlb[j].mas1 & MAS1_IPROT)) {
139                     tlb[j].mas1 &= ~MAS1_VALID;
140                 }
141             }
142         }
143         tlb += booke206_tlb_size(env, i);
144     }
145 
146     tlb_flush(env_cpu(env));
147 }
148 
149 /*****************************************************************************/
150 /* BATs management */
151 #if !defined(FLUSH_ALL_TLBS)
152 static inline void do_invalidate_BAT(CPUPPCState *env, target_ulong BATu,
153                                      target_ulong mask)
154 {
155     CPUState *cs = env_cpu(env);
156     target_ulong base, end, page;
157 
158     base = BATu & ~0x0001FFFF;
159     end = base + mask + 0x00020000;
160     if (((end - base) >> TARGET_PAGE_BITS) > 1024) {
161         /* Flushing 1024 4K pages is slower than a complete flush */
162         qemu_log_mask(CPU_LOG_MMU, "Flush all BATs\n");
163         tlb_flush(cs);
164         qemu_log_mask(CPU_LOG_MMU, "Flush done\n");
165         return;
166     }
167     qemu_log_mask(CPU_LOG_MMU, "Flush BAT from " TARGET_FMT_lx
168                   " to " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n",
169                   base, end, mask);
170     for (page = base; page != end; page += TARGET_PAGE_SIZE) {
171         tlb_flush_page(cs, page);
172     }
173     qemu_log_mask(CPU_LOG_MMU, "Flush done\n");
174 }
175 #endif
176 
177 static inline void dump_store_bat(CPUPPCState *env, char ID, int ul, int nr,
178                                   target_ulong value)
179 {
180     qemu_log_mask(CPU_LOG_MMU, "Set %cBAT%d%c to " TARGET_FMT_lx " ("
181                   TARGET_FMT_lx ")\n", ID, nr, ul == 0 ? 'u' : 'l',
182                   value, env->nip);
183 }
184 
185 void helper_store_ibatu(CPUPPCState *env, uint32_t nr, target_ulong value)
186 {
187     target_ulong mask;
188 
189     dump_store_bat(env, 'I', 0, nr, value);
190     if (env->IBAT[0][nr] != value) {
191         mask = (value << 15) & 0x0FFE0000UL;
192 #if !defined(FLUSH_ALL_TLBS)
193         do_invalidate_BAT(env, env->IBAT[0][nr], mask);
194 #endif
195         /*
196          * When storing valid upper BAT, mask BEPI and BRPN and
197          * invalidate all TLBs covered by this BAT
198          */
199         mask = (value << 15) & 0x0FFE0000UL;
200         env->IBAT[0][nr] = (value & 0x00001FFFUL) |
201             (value & ~0x0001FFFFUL & ~mask);
202         env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
203             (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
204 #if !defined(FLUSH_ALL_TLBS)
205         do_invalidate_BAT(env, env->IBAT[0][nr], mask);
206 #else
207         tlb_flush(env_cpu(env));
208 #endif
209     }
210 }
211 
212 void helper_store_ibatl(CPUPPCState *env, uint32_t nr, target_ulong value)
213 {
214     dump_store_bat(env, 'I', 1, nr, value);
215     env->IBAT[1][nr] = value;
216 }
217 
218 void helper_store_dbatu(CPUPPCState *env, uint32_t nr, target_ulong value)
219 {
220     target_ulong mask;
221 
222     dump_store_bat(env, 'D', 0, nr, value);
223     if (env->DBAT[0][nr] != value) {
224         /*
225          * When storing valid upper BAT, mask BEPI and BRPN and
226          * invalidate all TLBs covered by this BAT
227          */
228         mask = (value << 15) & 0x0FFE0000UL;
229 #if !defined(FLUSH_ALL_TLBS)
230         do_invalidate_BAT(env, env->DBAT[0][nr], mask);
231 #endif
232         mask = (value << 15) & 0x0FFE0000UL;
233         env->DBAT[0][nr] = (value & 0x00001FFFUL) |
234             (value & ~0x0001FFFFUL & ~mask);
235         env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
236             (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
237 #if !defined(FLUSH_ALL_TLBS)
238         do_invalidate_BAT(env, env->DBAT[0][nr], mask);
239 #else
240         tlb_flush(env_cpu(env));
241 #endif
242     }
243 }
244 
245 void helper_store_dbatl(CPUPPCState *env, uint32_t nr, target_ulong value)
246 {
247     dump_store_bat(env, 'D', 1, nr, value);
248     env->DBAT[1][nr] = value;
249 }
250 
251 /*****************************************************************************/
252 /* TLB management */
253 void ppc_tlb_invalidate_all(CPUPPCState *env)
254 {
255 #if defined(TARGET_PPC64)
256     if (mmu_is_64bit(env->mmu_model)) {
257         env->tlb_need_flush = 0;
258         tlb_flush(env_cpu(env));
259     } else
260 #endif /* defined(TARGET_PPC64) */
261     switch (env->mmu_model) {
262     case POWERPC_MMU_SOFT_6xx:
263         ppc6xx_tlb_invalidate_all(env);
264         break;
265     case POWERPC_MMU_SOFT_4xx:
266         ppc4xx_tlb_invalidate_all(env);
267         break;
268     case POWERPC_MMU_REAL:
269         cpu_abort(env_cpu(env), "No TLB for PowerPC 4xx in real mode\n");
270         break;
271     case POWERPC_MMU_MPC8xx:
272         /* XXX: TODO */
273         cpu_abort(env_cpu(env), "MPC8xx MMU model is not implemented\n");
274         break;
275     case POWERPC_MMU_BOOKE:
276         tlb_flush(env_cpu(env));
277         break;
278     case POWERPC_MMU_BOOKE206:
279         booke206_flush_tlb(env, -1, 0);
280         break;
281     case POWERPC_MMU_32B:
282         env->tlb_need_flush = 0;
283         tlb_flush(env_cpu(env));
284         break;
285     default:
286         /* XXX: TODO */
287         cpu_abort(env_cpu(env), "Unknown MMU model %x\n", env->mmu_model);
288         break;
289     }
290 }
291 
292 void ppc_tlb_invalidate_one(CPUPPCState *env, target_ulong addr)
293 {
294 #if !defined(FLUSH_ALL_TLBS)
295     addr &= TARGET_PAGE_MASK;
296 #if defined(TARGET_PPC64)
297     if (mmu_is_64bit(env->mmu_model)) {
298         /* tlbie invalidate TLBs for all segments */
299         /*
300          * XXX: given the fact that there are too many segments to invalidate,
301          *      and we still don't have a tlb_flush_mask(env, n, mask) in QEMU,
302          *      we just invalidate all TLBs
303          */
304         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
305     } else
306 #endif /* defined(TARGET_PPC64) */
307     switch (env->mmu_model) {
308     case POWERPC_MMU_SOFT_6xx:
309         ppc6xx_tlb_invalidate_virt(env, addr, 0);
310         if (env->id_tlbs == 1) {
311             ppc6xx_tlb_invalidate_virt(env, addr, 1);
312         }
313         break;
314     case POWERPC_MMU_32B:
315         /*
316          * Actual CPUs invalidate entire congruence classes based on
317          * the geometry of their TLBs and some OSes take that into
318          * account, we just mark the TLB to be flushed later (context
319          * synchronizing event or sync instruction on 32-bit).
320          */
321         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
322         break;
323     default:
324         /* Should never reach here with other MMU models */
325         assert(0);
326     }
327 #else
328     ppc_tlb_invalidate_all(env);
329 #endif
330 }
331 
332 /*****************************************************************************/
333 /* Special registers manipulation */
334 
335 /* Segment registers load and store */
336 target_ulong helper_load_sr(CPUPPCState *env, target_ulong sr_num)
337 {
338 #if defined(TARGET_PPC64)
339     if (mmu_is_64bit(env->mmu_model)) {
340         /* XXX */
341         return 0;
342     }
343 #endif
344     return env->sr[sr_num];
345 }
346 
347 void helper_store_sr(CPUPPCState *env, target_ulong srnum, target_ulong value)
348 {
349     qemu_log_mask(CPU_LOG_MMU,
350             "%s: reg=%d " TARGET_FMT_lx " " TARGET_FMT_lx "\n", __func__,
351             (int)srnum, value, env->sr[srnum]);
352 #if defined(TARGET_PPC64)
353     if (mmu_is_64bit(env->mmu_model)) {
354         PowerPCCPU *cpu = env_archcpu(env);
355         uint64_t esid, vsid;
356 
357         /* ESID = srnum */
358         esid = ((uint64_t)(srnum & 0xf) << 28) | SLB_ESID_V;
359 
360         /* VSID = VSID */
361         vsid = (value & 0xfffffff) << 12;
362         /* flags = flags */
363         vsid |= ((value >> 27) & 0xf) << 8;
364 
365         ppc_store_slb(cpu, srnum, esid, vsid);
366     } else
367 #endif
368     if (env->sr[srnum] != value) {
369         env->sr[srnum] = value;
370         /*
371          * Invalidating 256MB of virtual memory in 4kB pages is way
372          * longer than flushing the whole TLB.
373          */
374 #if !defined(FLUSH_ALL_TLBS) && 0
375         {
376             target_ulong page, end;
377             /* Invalidate 256 MB of virtual memory */
378             page = (16 << 20) * srnum;
379             end = page + (16 << 20);
380             for (; page != end; page += TARGET_PAGE_SIZE) {
381                 tlb_flush_page(env_cpu(env), page);
382             }
383         }
384 #else
385         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
386 #endif
387     }
388 }
389 
390 /* TLB management */
391 void helper_tlbia(CPUPPCState *env)
392 {
393     ppc_tlb_invalidate_all(env);
394 }
395 
396 void helper_tlbie(CPUPPCState *env, target_ulong addr)
397 {
398     ppc_tlb_invalidate_one(env, addr);
399 }
400 
401 #if defined(TARGET_PPC64)
402 
403 /* Invalidation Selector */
404 #define TLBIE_IS_VA         0
405 #define TLBIE_IS_PID        1
406 #define TLBIE_IS_LPID       2
407 #define TLBIE_IS_ALL        3
408 
409 /* Radix Invalidation Control */
410 #define TLBIE_RIC_TLB       0
411 #define TLBIE_RIC_PWC       1
412 #define TLBIE_RIC_ALL       2
413 #define TLBIE_RIC_GRP       3
414 
415 /* Radix Actual Page sizes */
416 #define TLBIE_R_AP_4K       0
417 #define TLBIE_R_AP_64K      5
418 #define TLBIE_R_AP_2M       1
419 #define TLBIE_R_AP_1G       2
420 
421 /* RB field masks */
422 #define TLBIE_RB_EPN_MASK   PPC_BITMASK(0, 51)
423 #define TLBIE_RB_IS_MASK    PPC_BITMASK(52, 53)
424 #define TLBIE_RB_AP_MASK    PPC_BITMASK(56, 58)
425 
426 void helper_tlbie_isa300(CPUPPCState *env, target_ulong rb, target_ulong rs,
427                          uint32_t flags)
428 {
429     unsigned ric = (flags & TLBIE_F_RIC_MASK) >> TLBIE_F_RIC_SHIFT;
430     /*
431      * With the exception of the checks for invalid instruction forms,
432      * PRS is currently ignored, because we don't know if a given TLB entry
433      * is process or partition scoped.
434      */
435     bool prs = flags & TLBIE_F_PRS;
436     bool r = flags & TLBIE_F_R;
437     bool local = flags & TLBIE_F_LOCAL;
438     bool effR;
439     unsigned is = extract64(rb, PPC_BIT_NR(53), 2);
440     unsigned ap;        /* actual page size */
441     target_ulong addr, pgoffs_mask;
442 
443     qemu_log_mask(CPU_LOG_MMU,
444         "%s: local=%d addr=" TARGET_FMT_lx " ric=%u prs=%d r=%d is=%u\n",
445         __func__, local, rb & TARGET_PAGE_MASK, ric, prs, r, is);
446 
447     effR = FIELD_EX64(env->msr, MSR, HV) ? r : env->spr[SPR_LPCR] & LPCR_HR;
448 
449     /* Partial TLB invalidation is supported for Radix only for now. */
450     if (!effR) {
451         goto inval_all;
452     }
453 
454     /* Check for invalid instruction forms (effR=1). */
455     if (unlikely(ric == TLBIE_RIC_GRP ||
456                  ((ric == TLBIE_RIC_PWC || ric == TLBIE_RIC_ALL) &&
457                                            is == TLBIE_IS_VA) ||
458                  (!prs && is == TLBIE_IS_PID))) {
459         qemu_log_mask(LOG_GUEST_ERROR,
460             "%s: invalid instruction form: ric=%u prs=%d r=%d is=%u\n",
461             __func__, ric, prs, r, is);
462         goto invalid;
463     }
464 
465     /* We don't cache Page Walks. */
466     if (ric == TLBIE_RIC_PWC) {
467         if (local) {
468             unsigned set = extract64(rb, PPC_BIT_NR(51), 12);
469             if (set != 0) {
470                 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid set: %d\n",
471                               __func__, set);
472                 goto invalid;
473             }
474         }
475         return;
476     }
477 
478     /*
479      * Invalidation by LPID or PID is not supported, so fallback
480      * to full TLB flush in these cases.
481      */
482     if (is != TLBIE_IS_VA) {
483         goto inval_all;
484     }
485 
486     /*
487      * The results of an attempt to invalidate a translation outside of
488      * quadrant 0 for Radix Tree translation (effR=1, RIC=0, PRS=1, IS=0,
489      * and EA 0:1 != 0b00) are boundedly undefined.
490      */
491     if (unlikely(ric == TLBIE_RIC_TLB && prs && is == TLBIE_IS_VA &&
492                  (rb & R_EADDR_QUADRANT) != R_EADDR_QUADRANT0)) {
493         qemu_log_mask(LOG_GUEST_ERROR,
494             "%s: attempt to invalidate a translation outside of quadrant 0\n",
495             __func__);
496         goto inval_all;
497     }
498 
499     assert(is == TLBIE_IS_VA);
500     assert(ric == TLBIE_RIC_TLB || ric == TLBIE_RIC_ALL);
501 
502     ap = extract64(rb, PPC_BIT_NR(58), 3);
503     switch (ap) {
504     case TLBIE_R_AP_4K:
505         pgoffs_mask = 0xfffull;
506         break;
507 
508     case TLBIE_R_AP_64K:
509         pgoffs_mask = 0xffffull;
510         break;
511 
512     case TLBIE_R_AP_2M:
513         pgoffs_mask = 0x1fffffull;
514         break;
515 
516     case TLBIE_R_AP_1G:
517         pgoffs_mask = 0x3fffffffull;
518         break;
519 
520     default:
521         /*
522          * If the value specified in RS 0:31, RS 32:63, RB 54:55, RB 56:58,
523          * RB 44:51, or RB 56:63, when it is needed to perform the specified
524          * operation, is not supported by the implementation, the instruction
525          * is treated as if the instruction form were invalid.
526          */
527         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid AP: %d\n", __func__, ap);
528         goto invalid;
529     }
530 
531     addr = rb & TLBIE_RB_EPN_MASK & ~pgoffs_mask;
532 
533     if (local) {
534         tlb_flush_page(env_cpu(env), addr);
535     } else {
536         tlb_flush_page_all_cpus(env_cpu(env), addr);
537     }
538     return;
539 
540 inval_all:
541     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
542     if (!local) {
543         env->tlb_need_flush |= TLB_NEED_GLOBAL_FLUSH;
544     }
545     return;
546 
547 invalid:
548     raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
549                            POWERPC_EXCP_INVAL |
550                            POWERPC_EXCP_INVAL_INVAL, GETPC());
551 }
552 
553 #endif
554 
555 void helper_tlbiva(CPUPPCState *env, target_ulong addr)
556 {
557     /* tlbiva instruction only exists on BookE */
558     assert(env->mmu_model == POWERPC_MMU_BOOKE);
559     /* XXX: TODO */
560     cpu_abort(env_cpu(env), "BookE MMU model is not implemented\n");
561 }
562 
563 /* Software driven TLBs management */
564 /* PowerPC 602/603 software TLB load instructions helpers */
565 static void do_6xx_tlb(CPUPPCState *env, target_ulong new_EPN, int is_code)
566 {
567     target_ulong RPN, CMP, EPN;
568     int way;
569 
570     RPN = env->spr[SPR_RPA];
571     if (is_code) {
572         CMP = env->spr[SPR_ICMP];
573         EPN = env->spr[SPR_IMISS];
574     } else {
575         CMP = env->spr[SPR_DCMP];
576         EPN = env->spr[SPR_DMISS];
577     }
578     way = (env->spr[SPR_SRR1] >> 17) & 1;
579     (void)EPN; /* avoid a compiler warning */
580     qemu_log_mask(CPU_LOG_MMU, "%s: EPN " TARGET_FMT_lx " " TARGET_FMT_lx
581                   " PTE0 " TARGET_FMT_lx " PTE1 " TARGET_FMT_lx " way %d\n",
582                   __func__, new_EPN, EPN, CMP, RPN, way);
583     /* Store this TLB */
584     ppc6xx_tlb_store(env, (uint32_t)(new_EPN & TARGET_PAGE_MASK),
585                      way, is_code, CMP, RPN);
586 }
587 
588 void helper_6xx_tlbd(CPUPPCState *env, target_ulong EPN)
589 {
590     do_6xx_tlb(env, EPN, 0);
591 }
592 
593 void helper_6xx_tlbi(CPUPPCState *env, target_ulong EPN)
594 {
595     do_6xx_tlb(env, EPN, 1);
596 }
597 
598 /*****************************************************************************/
599 /* PowerPC 601 specific instructions (POWER bridge) */
600 
601 target_ulong helper_rac(CPUPPCState *env, target_ulong addr)
602 {
603     mmu_ctx_t ctx;
604     int nb_BATs;
605     target_ulong ret = 0;
606 
607     /*
608      * We don't have to generate many instances of this instruction,
609      * as rac is supervisor only.
610      *
611      * XXX: FIX THIS: Pretend we have no BAT
612      */
613     nb_BATs = env->nb_BATs;
614     env->nb_BATs = 0;
615     if (get_physical_address_wtlb(env, &ctx, addr, 0, ACCESS_INT, 0) == 0) {
616         ret = ctx.raddr;
617     }
618     env->nb_BATs = nb_BATs;
619     return ret;
620 }
621 
622 static inline target_ulong booke_tlb_to_page_size(int size)
623 {
624     return 1024 << (2 * size);
625 }
626 
627 static inline int booke_page_size_to_tlb(target_ulong page_size)
628 {
629     int size;
630 
631     switch (page_size) {
632     case 0x00000400UL:
633         size = 0x0;
634         break;
635     case 0x00001000UL:
636         size = 0x1;
637         break;
638     case 0x00004000UL:
639         size = 0x2;
640         break;
641     case 0x00010000UL:
642         size = 0x3;
643         break;
644     case 0x00040000UL:
645         size = 0x4;
646         break;
647     case 0x00100000UL:
648         size = 0x5;
649         break;
650     case 0x00400000UL:
651         size = 0x6;
652         break;
653     case 0x01000000UL:
654         size = 0x7;
655         break;
656     case 0x04000000UL:
657         size = 0x8;
658         break;
659     case 0x10000000UL:
660         size = 0x9;
661         break;
662     case 0x40000000UL:
663         size = 0xA;
664         break;
665 #if defined(TARGET_PPC64)
666     case 0x000100000000ULL:
667         size = 0xB;
668         break;
669     case 0x000400000000ULL:
670         size = 0xC;
671         break;
672     case 0x001000000000ULL:
673         size = 0xD;
674         break;
675     case 0x004000000000ULL:
676         size = 0xE;
677         break;
678     case 0x010000000000ULL:
679         size = 0xF;
680         break;
681 #endif
682     default:
683         size = -1;
684         break;
685     }
686 
687     return size;
688 }
689 
690 /* Helpers for 4xx TLB management */
691 #define PPC4XX_TLB_ENTRY_MASK       0x0000003f  /* Mask for 64 TLB entries */
692 
693 #define PPC4XX_TLBHI_V              0x00000040
694 #define PPC4XX_TLBHI_E              0x00000020
695 #define PPC4XX_TLBHI_SIZE_MIN       0
696 #define PPC4XX_TLBHI_SIZE_MAX       7
697 #define PPC4XX_TLBHI_SIZE_DEFAULT   1
698 #define PPC4XX_TLBHI_SIZE_SHIFT     7
699 #define PPC4XX_TLBHI_SIZE_MASK      0x00000007
700 
701 #define PPC4XX_TLBLO_EX             0x00000200
702 #define PPC4XX_TLBLO_WR             0x00000100
703 #define PPC4XX_TLBLO_ATTR_MASK      0x000000FF
704 #define PPC4XX_TLBLO_RPN_MASK       0xFFFFFC00
705 
706 void helper_store_40x_pid(CPUPPCState *env, target_ulong val)
707 {
708     if (env->spr[SPR_40x_PID] != val) {
709         env->spr[SPR_40x_PID] = val;
710         env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
711     }
712 }
713 
714 target_ulong helper_4xx_tlbre_hi(CPUPPCState *env, target_ulong entry)
715 {
716     ppcemb_tlb_t *tlb;
717     target_ulong ret;
718     int size;
719 
720     entry &= PPC4XX_TLB_ENTRY_MASK;
721     tlb = &env->tlb.tlbe[entry];
722     ret = tlb->EPN;
723     if (tlb->prot & PAGE_VALID) {
724         ret |= PPC4XX_TLBHI_V;
725     }
726     size = booke_page_size_to_tlb(tlb->size);
727     if (size < PPC4XX_TLBHI_SIZE_MIN || size > PPC4XX_TLBHI_SIZE_MAX) {
728         size = PPC4XX_TLBHI_SIZE_DEFAULT;
729     }
730     ret |= size << PPC4XX_TLBHI_SIZE_SHIFT;
731     helper_store_40x_pid(env, tlb->PID);
732     return ret;
733 }
734 
735 target_ulong helper_4xx_tlbre_lo(CPUPPCState *env, target_ulong entry)
736 {
737     ppcemb_tlb_t *tlb;
738     target_ulong ret;
739 
740     entry &= PPC4XX_TLB_ENTRY_MASK;
741     tlb = &env->tlb.tlbe[entry];
742     ret = tlb->RPN;
743     if (tlb->prot & PAGE_EXEC) {
744         ret |= PPC4XX_TLBLO_EX;
745     }
746     if (tlb->prot & PAGE_WRITE) {
747         ret |= PPC4XX_TLBLO_WR;
748     }
749     return ret;
750 }
751 
752 void helper_4xx_tlbwe_hi(CPUPPCState *env, target_ulong entry,
753                          target_ulong val)
754 {
755     CPUState *cs = env_cpu(env);
756     ppcemb_tlb_t *tlb;
757     target_ulong page, end;
758 
759     qemu_log_mask(CPU_LOG_MMU, "%s entry %d val " TARGET_FMT_lx "\n",
760                   __func__, (int)entry,
761               val);
762     entry &= PPC4XX_TLB_ENTRY_MASK;
763     tlb = &env->tlb.tlbe[entry];
764     /* Invalidate previous TLB (if it's valid) */
765     if (tlb->prot & PAGE_VALID) {
766         end = tlb->EPN + tlb->size;
767         qemu_log_mask(CPU_LOG_MMU, "%s: invalidate old TLB %d start "
768                       TARGET_FMT_lx " end " TARGET_FMT_lx "\n", __func__,
769                       (int)entry, tlb->EPN, end);
770         for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE) {
771             tlb_flush_page(cs, page);
772         }
773     }
774     tlb->size = booke_tlb_to_page_size((val >> PPC4XX_TLBHI_SIZE_SHIFT)
775                                        & PPC4XX_TLBHI_SIZE_MASK);
776     /*
777      * We cannot handle TLB size < TARGET_PAGE_SIZE.
778      * If this ever occurs, we should implement TARGET_PAGE_BITS_VARY
779      */
780     if ((val & PPC4XX_TLBHI_V) && tlb->size < TARGET_PAGE_SIZE) {
781         cpu_abort(cs, "TLB size " TARGET_FMT_lu " < %u "
782                   "are not supported (%d)\n"
783                   "Please implement TARGET_PAGE_BITS_VARY\n",
784                   tlb->size, TARGET_PAGE_SIZE, (int)((val >> 7) & 0x7));
785     }
786     tlb->EPN = val & ~(tlb->size - 1);
787     if (val & PPC4XX_TLBHI_V) {
788         tlb->prot |= PAGE_VALID;
789         if (val & PPC4XX_TLBHI_E) {
790             /* XXX: TO BE FIXED */
791             cpu_abort(cs,
792                       "Little-endian TLB entries are not supported by now\n");
793         }
794     } else {
795         tlb->prot &= ~PAGE_VALID;
796     }
797     tlb->PID = env->spr[SPR_40x_PID]; /* PID */
798     qemu_log_mask(CPU_LOG_MMU, "%s: set up TLB %d RPN " HWADDR_FMT_plx
799                   " EPN " TARGET_FMT_lx " size " TARGET_FMT_lx
800                   " prot %c%c%c%c PID %d\n", __func__,
801                   (int)entry, tlb->RPN, tlb->EPN, tlb->size,
802                   tlb->prot & PAGE_READ ? 'r' : '-',
803                   tlb->prot & PAGE_WRITE ? 'w' : '-',
804                   tlb->prot & PAGE_EXEC ? 'x' : '-',
805                   tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
806     /* Invalidate new TLB (if valid) */
807     if (tlb->prot & PAGE_VALID) {
808         end = tlb->EPN + tlb->size;
809         qemu_log_mask(CPU_LOG_MMU, "%s: invalidate TLB %d start "
810                       TARGET_FMT_lx " end " TARGET_FMT_lx "\n", __func__,
811                       (int)entry, tlb->EPN, end);
812         for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE) {
813             tlb_flush_page(cs, page);
814         }
815     }
816 }
817 
818 void helper_4xx_tlbwe_lo(CPUPPCState *env, target_ulong entry,
819                          target_ulong val)
820 {
821     ppcemb_tlb_t *tlb;
822 
823     qemu_log_mask(CPU_LOG_MMU, "%s entry %i val " TARGET_FMT_lx "\n",
824                   __func__, (int)entry, val);
825     entry &= PPC4XX_TLB_ENTRY_MASK;
826     tlb = &env->tlb.tlbe[entry];
827     tlb->attr = val & PPC4XX_TLBLO_ATTR_MASK;
828     tlb->RPN = val & PPC4XX_TLBLO_RPN_MASK;
829     tlb->prot = PAGE_READ;
830     if (val & PPC4XX_TLBLO_EX) {
831         tlb->prot |= PAGE_EXEC;
832     }
833     if (val & PPC4XX_TLBLO_WR) {
834         tlb->prot |= PAGE_WRITE;
835     }
836     qemu_log_mask(CPU_LOG_MMU, "%s: set up TLB %d RPN " HWADDR_FMT_plx
837                   " EPN " TARGET_FMT_lx
838                   " size " TARGET_FMT_lx " prot %c%c%c%c PID %d\n", __func__,
839                   (int)entry, tlb->RPN, tlb->EPN, tlb->size,
840                   tlb->prot & PAGE_READ ? 'r' : '-',
841                   tlb->prot & PAGE_WRITE ? 'w' : '-',
842                   tlb->prot & PAGE_EXEC ? 'x' : '-',
843                   tlb->prot & PAGE_VALID ? 'v' : '-', (int)tlb->PID);
844 
845     env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
846 }
847 
848 target_ulong helper_4xx_tlbsx(CPUPPCState *env, target_ulong address)
849 {
850     return ppcemb_tlb_search(env, address, env->spr[SPR_40x_PID]);
851 }
852 
853 /* PowerPC 440 TLB management */
854 void helper_440_tlbwe(CPUPPCState *env, uint32_t word, target_ulong entry,
855                       target_ulong value)
856 {
857     ppcemb_tlb_t *tlb;
858     target_ulong EPN, RPN, size;
859     int do_flush_tlbs;
860 
861     qemu_log_mask(CPU_LOG_MMU, "%s word %d entry %d value " TARGET_FMT_lx "\n",
862                   __func__, word, (int)entry, value);
863     do_flush_tlbs = 0;
864     entry &= 0x3F;
865     tlb = &env->tlb.tlbe[entry];
866     switch (word) {
867     default:
868         /* Just here to please gcc */
869     case 0:
870         EPN = value & 0xFFFFFC00;
871         if ((tlb->prot & PAGE_VALID) && EPN != tlb->EPN) {
872             do_flush_tlbs = 1;
873         }
874         tlb->EPN = EPN;
875         size = booke_tlb_to_page_size((value >> 4) & 0xF);
876         if ((tlb->prot & PAGE_VALID) && tlb->size < size) {
877             do_flush_tlbs = 1;
878         }
879         tlb->size = size;
880         tlb->attr &= ~0x1;
881         tlb->attr |= (value >> 8) & 1;
882         if (value & 0x200) {
883             tlb->prot |= PAGE_VALID;
884         } else {
885             if (tlb->prot & PAGE_VALID) {
886                 tlb->prot &= ~PAGE_VALID;
887                 do_flush_tlbs = 1;
888             }
889         }
890         tlb->PID = env->spr[SPR_440_MMUCR] & 0x000000FF;
891         if (do_flush_tlbs) {
892             tlb_flush(env_cpu(env));
893         }
894         break;
895     case 1:
896         RPN = value & 0xFFFFFC0F;
897         if ((tlb->prot & PAGE_VALID) && tlb->RPN != RPN) {
898             tlb_flush(env_cpu(env));
899         }
900         tlb->RPN = RPN;
901         break;
902     case 2:
903         tlb->attr = (tlb->attr & 0x1) | (value & 0x0000FF00);
904         tlb->prot = tlb->prot & PAGE_VALID;
905         if (value & 0x1) {
906             tlb->prot |= PAGE_READ << 4;
907         }
908         if (value & 0x2) {
909             tlb->prot |= PAGE_WRITE << 4;
910         }
911         if (value & 0x4) {
912             tlb->prot |= PAGE_EXEC << 4;
913         }
914         if (value & 0x8) {
915             tlb->prot |= PAGE_READ;
916         }
917         if (value & 0x10) {
918             tlb->prot |= PAGE_WRITE;
919         }
920         if (value & 0x20) {
921             tlb->prot |= PAGE_EXEC;
922         }
923         break;
924     }
925 }
926 
927 target_ulong helper_440_tlbre(CPUPPCState *env, uint32_t word,
928                               target_ulong entry)
929 {
930     ppcemb_tlb_t *tlb;
931     target_ulong ret;
932     int size;
933 
934     entry &= 0x3F;
935     tlb = &env->tlb.tlbe[entry];
936     switch (word) {
937     default:
938         /* Just here to please gcc */
939     case 0:
940         ret = tlb->EPN;
941         size = booke_page_size_to_tlb(tlb->size);
942         if (size < 0 || size > 0xF) {
943             size = 1;
944         }
945         ret |= size << 4;
946         if (tlb->attr & 0x1) {
947             ret |= 0x100;
948         }
949         if (tlb->prot & PAGE_VALID) {
950             ret |= 0x200;
951         }
952         env->spr[SPR_440_MMUCR] &= ~0x000000FF;
953         env->spr[SPR_440_MMUCR] |= tlb->PID;
954         break;
955     case 1:
956         ret = tlb->RPN;
957         break;
958     case 2:
959         ret = tlb->attr & ~0x1;
960         if (tlb->prot & (PAGE_READ << 4)) {
961             ret |= 0x1;
962         }
963         if (tlb->prot & (PAGE_WRITE << 4)) {
964             ret |= 0x2;
965         }
966         if (tlb->prot & (PAGE_EXEC << 4)) {
967             ret |= 0x4;
968         }
969         if (tlb->prot & PAGE_READ) {
970             ret |= 0x8;
971         }
972         if (tlb->prot & PAGE_WRITE) {
973             ret |= 0x10;
974         }
975         if (tlb->prot & PAGE_EXEC) {
976             ret |= 0x20;
977         }
978         break;
979     }
980     return ret;
981 }
982 
983 target_ulong helper_440_tlbsx(CPUPPCState *env, target_ulong address)
984 {
985     return ppcemb_tlb_search(env, address, env->spr[SPR_440_MMUCR] & 0xFF);
986 }
987 
988 /* PowerPC BookE 2.06 TLB management */
989 
990 static ppcmas_tlb_t *booke206_cur_tlb(CPUPPCState *env)
991 {
992     uint32_t tlbncfg = 0;
993     int esel = (env->spr[SPR_BOOKE_MAS0] & MAS0_ESEL_MASK) >> MAS0_ESEL_SHIFT;
994     int ea = (env->spr[SPR_BOOKE_MAS2] & MAS2_EPN_MASK);
995     int tlb;
996 
997     tlb = (env->spr[SPR_BOOKE_MAS0] & MAS0_TLBSEL_MASK) >> MAS0_TLBSEL_SHIFT;
998     tlbncfg = env->spr[SPR_BOOKE_TLB0CFG + tlb];
999 
1000     if ((tlbncfg & TLBnCFG_HES) && (env->spr[SPR_BOOKE_MAS0] & MAS0_HES)) {
1001         cpu_abort(env_cpu(env), "we don't support HES yet\n");
1002     }
1003 
1004     return booke206_get_tlbm(env, tlb, ea, esel);
1005 }
1006 
1007 void helper_booke_setpid(CPUPPCState *env, uint32_t pidn, target_ulong pid)
1008 {
1009     env->spr[pidn] = pid;
1010     /* changing PIDs mean we're in a different address space now */
1011     tlb_flush(env_cpu(env));
1012 }
1013 
1014 void helper_booke_set_eplc(CPUPPCState *env, target_ulong val)
1015 {
1016     env->spr[SPR_BOOKE_EPLC] = val & EPID_MASK;
1017     tlb_flush_by_mmuidx(env_cpu(env), 1 << PPC_TLB_EPID_LOAD);
1018 }
1019 void helper_booke_set_epsc(CPUPPCState *env, target_ulong val)
1020 {
1021     env->spr[SPR_BOOKE_EPSC] = val & EPID_MASK;
1022     tlb_flush_by_mmuidx(env_cpu(env), 1 << PPC_TLB_EPID_STORE);
1023 }
1024 
1025 static inline void flush_page(CPUPPCState *env, ppcmas_tlb_t *tlb)
1026 {
1027     if (booke206_tlb_to_page_size(env, tlb) == TARGET_PAGE_SIZE) {
1028         tlb_flush_page(env_cpu(env), tlb->mas2 & MAS2_EPN_MASK);
1029     } else {
1030         tlb_flush(env_cpu(env));
1031     }
1032 }
1033 
1034 void helper_booke206_tlbwe(CPUPPCState *env)
1035 {
1036     uint32_t tlbncfg, tlbn;
1037     ppcmas_tlb_t *tlb;
1038     uint32_t size_tlb, size_ps;
1039     target_ulong mask;
1040 
1041 
1042     switch (env->spr[SPR_BOOKE_MAS0] & MAS0_WQ_MASK) {
1043     case MAS0_WQ_ALWAYS:
1044         /* good to go, write that entry */
1045         break;
1046     case MAS0_WQ_COND:
1047         /* XXX check if reserved */
1048         if (0) {
1049             return;
1050         }
1051         break;
1052     case MAS0_WQ_CLR_RSRV:
1053         /* XXX clear entry */
1054         return;
1055     default:
1056         /* no idea what to do */
1057         return;
1058     }
1059 
1060     if (((env->spr[SPR_BOOKE_MAS0] & MAS0_ATSEL) == MAS0_ATSEL_LRAT) &&
1061         !FIELD_EX64(env->msr, MSR, GS)) {
1062         /* XXX we don't support direct LRAT setting yet */
1063         fprintf(stderr, "cpu: don't support LRAT setting yet\n");
1064         return;
1065     }
1066 
1067     tlbn = (env->spr[SPR_BOOKE_MAS0] & MAS0_TLBSEL_MASK) >> MAS0_TLBSEL_SHIFT;
1068     tlbncfg = env->spr[SPR_BOOKE_TLB0CFG + tlbn];
1069 
1070     tlb = booke206_cur_tlb(env);
1071 
1072     if (!tlb) {
1073         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
1074                                POWERPC_EXCP_INVAL |
1075                                POWERPC_EXCP_INVAL_INVAL, GETPC());
1076     }
1077 
1078     /* check that we support the targeted size */
1079     size_tlb = (env->spr[SPR_BOOKE_MAS1] & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
1080     size_ps = booke206_tlbnps(env, tlbn);
1081     if ((env->spr[SPR_BOOKE_MAS1] & MAS1_VALID) && (tlbncfg & TLBnCFG_AVAIL) &&
1082         !(size_ps & (1 << size_tlb))) {
1083         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
1084                                POWERPC_EXCP_INVAL |
1085                                POWERPC_EXCP_INVAL_INVAL, GETPC());
1086     }
1087 
1088     if (FIELD_EX64(env->msr, MSR, GS)) {
1089         cpu_abort(env_cpu(env), "missing HV implementation\n");
1090     }
1091 
1092     if (tlb->mas1 & MAS1_VALID) {
1093         /*
1094          * Invalidate the page in QEMU TLB if it was a valid entry.
1095          *
1096          * In "PowerPC e500 Core Family Reference Manual, Rev. 1",
1097          * Section "12.4.2 TLB Write Entry (tlbwe) Instruction":
1098          * (https://www.nxp.com/docs/en/reference-manual/E500CORERM.pdf)
1099          *
1100          * "Note that when an L2 TLB entry is written, it may be displacing an
1101          * already valid entry in the same L2 TLB location (a victim). If a
1102          * valid L1 TLB entry corresponds to the L2 MMU victim entry, that L1
1103          * TLB entry is automatically invalidated."
1104          */
1105         flush_page(env, tlb);
1106     }
1107 
1108     tlb->mas7_3 = ((uint64_t)env->spr[SPR_BOOKE_MAS7] << 32) |
1109         env->spr[SPR_BOOKE_MAS3];
1110     tlb->mas1 = env->spr[SPR_BOOKE_MAS1];
1111 
1112     if ((env->spr[SPR_MMUCFG] & MMUCFG_MAVN) == MMUCFG_MAVN_V2) {
1113         /* For TLB which has a fixed size TSIZE is ignored with MAV2 */
1114         booke206_fixed_size_tlbn(env, tlbn, tlb);
1115     } else {
1116         if (!(tlbncfg & TLBnCFG_AVAIL)) {
1117             /* force !AVAIL TLB entries to correct page size */
1118             tlb->mas1 &= ~MAS1_TSIZE_MASK;
1119             /* XXX can be configured in MMUCSR0 */
1120             tlb->mas1 |= (tlbncfg & TLBnCFG_MINSIZE) >> 12;
1121         }
1122     }
1123 
1124     /* Make a mask from TLB size to discard invalid bits in EPN field */
1125     mask = ~(booke206_tlb_to_page_size(env, tlb) - 1);
1126     /* Add a mask for page attributes */
1127     mask |= MAS2_ACM | MAS2_VLE | MAS2_W | MAS2_I | MAS2_M | MAS2_G | MAS2_E;
1128 
1129     if (!FIELD_EX64(env->msr, MSR, CM)) {
1130         /*
1131          * Executing a tlbwe instruction in 32-bit mode will set bits
1132          * 0:31 of the TLB EPN field to zero.
1133          */
1134         mask &= 0xffffffff;
1135     }
1136 
1137     tlb->mas2 = env->spr[SPR_BOOKE_MAS2] & mask;
1138 
1139     if (!(tlbncfg & TLBnCFG_IPROT)) {
1140         /* no IPROT supported by TLB */
1141         tlb->mas1 &= ~MAS1_IPROT;
1142     }
1143 
1144     flush_page(env, tlb);
1145 }
1146 
1147 static inline void booke206_tlb_to_mas(CPUPPCState *env, ppcmas_tlb_t *tlb)
1148 {
1149     int tlbn = booke206_tlbm_to_tlbn(env, tlb);
1150     int way = booke206_tlbm_to_way(env, tlb);
1151 
1152     env->spr[SPR_BOOKE_MAS0] = tlbn << MAS0_TLBSEL_SHIFT;
1153     env->spr[SPR_BOOKE_MAS0] |= way << MAS0_ESEL_SHIFT;
1154     env->spr[SPR_BOOKE_MAS0] |= env->last_way << MAS0_NV_SHIFT;
1155 
1156     env->spr[SPR_BOOKE_MAS1] = tlb->mas1;
1157     env->spr[SPR_BOOKE_MAS2] = tlb->mas2;
1158     env->spr[SPR_BOOKE_MAS3] = tlb->mas7_3;
1159     env->spr[SPR_BOOKE_MAS7] = tlb->mas7_3 >> 32;
1160 }
1161 
1162 void helper_booke206_tlbre(CPUPPCState *env)
1163 {
1164     ppcmas_tlb_t *tlb = NULL;
1165 
1166     tlb = booke206_cur_tlb(env);
1167     if (!tlb) {
1168         env->spr[SPR_BOOKE_MAS1] = 0;
1169     } else {
1170         booke206_tlb_to_mas(env, tlb);
1171     }
1172 }
1173 
1174 void helper_booke206_tlbsx(CPUPPCState *env, target_ulong address)
1175 {
1176     ppcmas_tlb_t *tlb = NULL;
1177     int i, j;
1178     hwaddr raddr;
1179     uint32_t spid, sas;
1180 
1181     spid = (env->spr[SPR_BOOKE_MAS6] & MAS6_SPID_MASK) >> MAS6_SPID_SHIFT;
1182     sas = env->spr[SPR_BOOKE_MAS6] & MAS6_SAS;
1183 
1184     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
1185         int ways = booke206_tlb_ways(env, i);
1186 
1187         for (j = 0; j < ways; j++) {
1188             tlb = booke206_get_tlbm(env, i, address, j);
1189 
1190             if (!tlb) {
1191                 continue;
1192             }
1193 
1194             if (ppcmas_tlb_check(env, tlb, &raddr, address, spid)) {
1195                 continue;
1196             }
1197 
1198             if (sas != ((tlb->mas1 & MAS1_TS) >> MAS1_TS_SHIFT)) {
1199                 continue;
1200             }
1201 
1202             booke206_tlb_to_mas(env, tlb);
1203             return;
1204         }
1205     }
1206 
1207     /* no entry found, fill with defaults */
1208     env->spr[SPR_BOOKE_MAS0] = env->spr[SPR_BOOKE_MAS4] & MAS4_TLBSELD_MASK;
1209     env->spr[SPR_BOOKE_MAS1] = env->spr[SPR_BOOKE_MAS4] & MAS4_TSIZED_MASK;
1210     env->spr[SPR_BOOKE_MAS2] = env->spr[SPR_BOOKE_MAS4] & MAS4_WIMGED_MASK;
1211     env->spr[SPR_BOOKE_MAS3] = 0;
1212     env->spr[SPR_BOOKE_MAS7] = 0;
1213 
1214     if (env->spr[SPR_BOOKE_MAS6] & MAS6_SAS) {
1215         env->spr[SPR_BOOKE_MAS1] |= MAS1_TS;
1216     }
1217 
1218     env->spr[SPR_BOOKE_MAS1] |= (env->spr[SPR_BOOKE_MAS6] >> 16)
1219         << MAS1_TID_SHIFT;
1220 
1221     /* next victim logic */
1222     env->spr[SPR_BOOKE_MAS0] |= env->last_way << MAS0_ESEL_SHIFT;
1223     env->last_way++;
1224     env->last_way &= booke206_tlb_ways(env, 0) - 1;
1225     env->spr[SPR_BOOKE_MAS0] |= env->last_way << MAS0_NV_SHIFT;
1226 }
1227 
1228 static inline void booke206_invalidate_ea_tlb(CPUPPCState *env, int tlbn,
1229                                               vaddr ea)
1230 {
1231     int i;
1232     int ways = booke206_tlb_ways(env, tlbn);
1233     target_ulong mask;
1234 
1235     for (i = 0; i < ways; i++) {
1236         ppcmas_tlb_t *tlb = booke206_get_tlbm(env, tlbn, ea, i);
1237         if (!tlb) {
1238             continue;
1239         }
1240         mask = ~(booke206_tlb_to_page_size(env, tlb) - 1);
1241         if (((tlb->mas2 & MAS2_EPN_MASK) == (ea & mask)) &&
1242             !(tlb->mas1 & MAS1_IPROT)) {
1243             tlb->mas1 &= ~MAS1_VALID;
1244         }
1245     }
1246 }
1247 
1248 void helper_booke206_tlbivax(CPUPPCState *env, target_ulong address)
1249 {
1250     CPUState *cs;
1251 
1252     if (address & 0x4) {
1253         /* flush all entries */
1254         if (address & 0x8) {
1255             /* flush all of TLB1 */
1256             booke206_flush_tlb(env, BOOKE206_FLUSH_TLB1, 1);
1257         } else {
1258             /* flush all of TLB0 */
1259             booke206_flush_tlb(env, BOOKE206_FLUSH_TLB0, 0);
1260         }
1261         return;
1262     }
1263 
1264     if (address & 0x8) {
1265         /* flush TLB1 entries */
1266         booke206_invalidate_ea_tlb(env, 1, address);
1267         CPU_FOREACH(cs) {
1268             tlb_flush(cs);
1269         }
1270     } else {
1271         /* flush TLB0 entries */
1272         booke206_invalidate_ea_tlb(env, 0, address);
1273         CPU_FOREACH(cs) {
1274             tlb_flush_page(cs, address & MAS2_EPN_MASK);
1275         }
1276     }
1277 }
1278 
1279 void helper_booke206_tlbilx0(CPUPPCState *env, target_ulong address)
1280 {
1281     /* XXX missing LPID handling */
1282     booke206_flush_tlb(env, -1, 1);
1283 }
1284 
1285 void helper_booke206_tlbilx1(CPUPPCState *env, target_ulong address)
1286 {
1287     int i, j;
1288     int tid = (env->spr[SPR_BOOKE_MAS6] & MAS6_SPID);
1289     ppcmas_tlb_t *tlb = env->tlb.tlbm;
1290     int tlb_size;
1291 
1292     /* XXX missing LPID handling */
1293     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
1294         tlb_size = booke206_tlb_size(env, i);
1295         for (j = 0; j < tlb_size; j++) {
1296             if (!(tlb[j].mas1 & MAS1_IPROT) &&
1297                 ((tlb[j].mas1 & MAS1_TID_MASK) == tid)) {
1298                 tlb[j].mas1 &= ~MAS1_VALID;
1299             }
1300         }
1301         tlb += booke206_tlb_size(env, i);
1302     }
1303     tlb_flush(env_cpu(env));
1304 }
1305 
1306 void helper_booke206_tlbilx3(CPUPPCState *env, target_ulong address)
1307 {
1308     int i, j;
1309     ppcmas_tlb_t *tlb;
1310     int tid = (env->spr[SPR_BOOKE_MAS6] & MAS6_SPID);
1311     int pid = tid >> MAS6_SPID_SHIFT;
1312     int sgs = env->spr[SPR_BOOKE_MAS5] & MAS5_SGS;
1313     int ind = (env->spr[SPR_BOOKE_MAS6] & MAS6_SIND) ? MAS1_IND : 0;
1314     /* XXX check for unsupported isize and raise an invalid opcode then */
1315     int size = env->spr[SPR_BOOKE_MAS6] & MAS6_ISIZE_MASK;
1316     /* XXX implement MAV2 handling */
1317     bool mav2 = false;
1318 
1319     /* XXX missing LPID handling */
1320     /* flush by pid and ea */
1321     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
1322         int ways = booke206_tlb_ways(env, i);
1323 
1324         for (j = 0; j < ways; j++) {
1325             tlb = booke206_get_tlbm(env, i, address, j);
1326             if (!tlb) {
1327                 continue;
1328             }
1329             if ((ppcmas_tlb_check(env, tlb, NULL, address, pid) != 0) ||
1330                 (tlb->mas1 & MAS1_IPROT) ||
1331                 ((tlb->mas1 & MAS1_IND) != ind) ||
1332                 ((tlb->mas8 & MAS8_TGS) != sgs)) {
1333                 continue;
1334             }
1335             if (mav2 && ((tlb->mas1 & MAS1_TSIZE_MASK) != size)) {
1336                 /* XXX only check when MMUCFG[TWC] || TLBnCFG[HES] */
1337                 continue;
1338             }
1339             /* XXX e500mc doesn't match SAS, but other cores might */
1340             tlb->mas1 &= ~MAS1_VALID;
1341         }
1342     }
1343     tlb_flush(env_cpu(env));
1344 }
1345 
1346 void helper_booke206_tlbflush(CPUPPCState *env, target_ulong type)
1347 {
1348     int flags = 0;
1349 
1350     if (type & 2) {
1351         flags |= BOOKE206_FLUSH_TLB1;
1352     }
1353 
1354     if (type & 4) {
1355         flags |= BOOKE206_FLUSH_TLB0;
1356     }
1357 
1358     booke206_flush_tlb(env, flags, 1);
1359 }
1360 
1361 
1362 void helper_check_tlb_flush_local(CPUPPCState *env)
1363 {
1364     check_tlb_flush(env, false);
1365 }
1366 
1367 void helper_check_tlb_flush_global(CPUPPCState *env)
1368 {
1369     check_tlb_flush(env, true);
1370 }
1371 
1372 
1373 bool ppc_cpu_tlb_fill(CPUState *cs, vaddr eaddr, int size,
1374                       MMUAccessType access_type, int mmu_idx,
1375                       bool probe, uintptr_t retaddr)
1376 {
1377     PowerPCCPU *cpu = POWERPC_CPU(cs);
1378     hwaddr raddr;
1379     int page_size, prot;
1380 
1381     if (ppc_xlate(cpu, eaddr, access_type, &raddr,
1382                   &page_size, &prot, mmu_idx, !probe)) {
1383         tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
1384                      prot, mmu_idx, 1UL << page_size);
1385         return true;
1386     }
1387     if (probe) {
1388         return false;
1389     }
1390     raise_exception_err_ra(&cpu->env, cs->exception_index,
1391                            cpu->env.error_code, retaddr);
1392 }
1393