2 * PowerPC MMU, TLB, SLB and BAT emulation helpers for QEMU.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
5 * Copyright (c) 2013 David Gibson, IBM Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "sysemu/kvm.h"
24 #include "mmu-hash64.h"
30 # define LOG_MMU(...) qemu_log(__VA_ARGS__)
31 # define LOG_MMU_STATE(env) log_cpu_state((env), 0)
33 # define LOG_MMU(...) do { } while (0)
34 # define LOG_MMU_STATE(...) do { } while (0)
38 # define LOG_SLB(...) qemu_log(__VA_ARGS__)
40 # define LOG_SLB(...) do { } while (0)
47 ppc_slb_t
*slb_lookup(CPUPPCState
*env
, target_ulong eaddr
)
49 uint64_t esid_256M
, esid_1T
;
52 LOG_SLB("%s: eaddr " TARGET_FMT_lx
"\n", __func__
, eaddr
);
54 esid_256M
= (eaddr
& SEGMENT_MASK_256M
) | SLB_ESID_V
;
55 esid_1T
= (eaddr
& SEGMENT_MASK_1T
) | SLB_ESID_V
;
57 for (n
= 0; n
< env
->slb_nr
; n
++) {
58 ppc_slb_t
*slb
= &env
->slb
[n
];
60 LOG_SLB("%s: slot %d %016" PRIx64
" %016"
61 PRIx64
"\n", __func__
, n
, slb
->esid
, slb
->vsid
);
62 /* We check for 1T matches on all MMUs here - if the MMU
63 * doesn't have 1T segment support, we will have prevented 1T
64 * entries from being inserted in the slbmte code. */
65 if (((slb
->esid
== esid_256M
) &&
66 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
67 || ((slb
->esid
== esid_1T
) &&
68 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
76 void dump_slb(FILE *f
, fprintf_function cpu_fprintf
, CPUPPCState
*env
)
81 cpu_synchronize_state(env
);
83 cpu_fprintf(f
, "SLB\tESID\t\t\tVSID\n");
84 for (i
= 0; i
< env
->slb_nr
; i
++) {
85 slbe
= env
->slb
[i
].esid
;
86 slbv
= env
->slb
[i
].vsid
;
87 if (slbe
== 0 && slbv
== 0) {
90 cpu_fprintf(f
, "%d\t0x%016" PRIx64
"\t0x%016" PRIx64
"\n",
95 void helper_slbia(CPUPPCState
*env
)
100 /* XXX: Warning: slbia never invalidates the first segment */
101 for (n
= 1; n
< env
->slb_nr
; n
++) {
102 ppc_slb_t
*slb
= &env
->slb
[n
];
104 if (slb
->esid
& SLB_ESID_V
) {
105 slb
->esid
&= ~SLB_ESID_V
;
106 /* XXX: given the fact that segment size is 256 MB or 1TB,
107 * and we still don't have a tlb_flush_mask(env, n, mask)
108 * in QEMU, we just invalidate all TLBs
118 void helper_slbie(CPUPPCState
*env
, target_ulong addr
)
122 slb
= slb_lookup(env
, addr
);
127 if (slb
->esid
& SLB_ESID_V
) {
128 slb
->esid
&= ~SLB_ESID_V
;
130 /* XXX: given the fact that segment size is 256 MB or 1TB,
131 * and we still don't have a tlb_flush_mask(env, n, mask)
132 * in QEMU, we just invalidate all TLBs
138 int ppc_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
140 int slot
= rb
& 0xfff;
141 ppc_slb_t
*slb
= &env
->slb
[slot
];
143 if (rb
& (0x1000 - env
->slb_nr
)) {
144 return -1; /* Reserved bits set or slot too high */
146 if (rs
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
147 return -1; /* Bad segment size */
149 if ((rs
& SLB_VSID_B
) && !(env
->mmu_model
& POWERPC_MMU_1TSEG
)) {
150 return -1; /* 1T segment on MMU that doesn't support it */
153 /* Mask out the slot number as we store the entry */
154 slb
->esid
= rb
& (SLB_ESID_ESID
| SLB_ESID_V
);
157 LOG_SLB("%s: %d " TARGET_FMT_lx
" - " TARGET_FMT_lx
" => %016" PRIx64
158 " %016" PRIx64
"\n", __func__
, slot
, rb
, rs
,
159 slb
->esid
, slb
->vsid
);
164 static int ppc_load_slb_esid(CPUPPCState
*env
, target_ulong rb
,
167 int slot
= rb
& 0xfff;
168 ppc_slb_t
*slb
= &env
->slb
[slot
];
170 if (slot
>= env
->slb_nr
) {
178 static int ppc_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
,
181 int slot
= rb
& 0xfff;
182 ppc_slb_t
*slb
= &env
->slb
[slot
];
184 if (slot
>= env
->slb_nr
) {
192 void helper_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
194 if (ppc_store_slb(env
, rb
, rs
) < 0) {
195 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
200 target_ulong
helper_load_slb_esid(CPUPPCState
*env
, target_ulong rb
)
204 if (ppc_load_slb_esid(env
, rb
, &rt
) < 0) {
205 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
211 target_ulong
helper_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
215 if (ppc_load_slb_vsid(env
, rb
, &rt
) < 0) {
216 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
223 * 64-bit hash table MMU handling
226 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
227 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
229 static inline int pte64_is_valid(target_ulong pte0
)
231 return pte0
& 0x0000000000000001ULL
? 1 : 0;
234 static int pte64_check(mmu_ctx_t
*ctx
, target_ulong pte0
,
235 target_ulong pte1
, int h
, int rw
, int type
)
237 target_ulong ptem
, mmask
;
238 int access
, ret
, pteh
, ptev
, pp
;
241 /* Check validity and table match */
242 ptev
= pte64_is_valid(pte0
);
243 pteh
= (pte0
>> 1) & 1;
244 if (ptev
&& h
== pteh
) {
245 /* Check vsid & api */
246 ptem
= pte0
& PTE64_PTEM_MASK
;
247 mmask
= PTE64_CHECK_MASK
;
248 pp
= (pte1
& 0x00000003) | ((pte1
>> 61) & 0x00000004);
249 ctx
->nx
= (pte1
>> 2) & 1; /* No execute bit */
250 ctx
->nx
|= (pte1
>> 3) & 1; /* Guarded bit */
251 if (ptem
== ctx
->ptem
) {
252 if (ctx
->raddr
!= (hwaddr
)-1ULL) {
253 /* all matches should have equal RPN, WIMG & PP */
254 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
255 qemu_log("Bad RPN/WIMG/PP\n");
259 /* Compute access rights */
260 access
= pp_check(ctx
->key
, pp
, ctx
->nx
);
261 /* Keep the matching PTE informations */
264 ret
= check_prot(ctx
->prot
, rw
, type
);
267 LOG_MMU("PTE access granted !\n");
269 /* Access right violation */
270 LOG_MMU("PTE access rejected\n");
278 /* PTE table lookup */
279 int find_pte64(CPUPPCState
*env
, mmu_ctx_t
*ctx
, int h
,
280 int rw
, int type
, int target_page_bits
)
283 target_ulong pte0
, pte1
;
287 ret
= -1; /* No entry found */
288 pteg_off
= get_pteg_offset(env
, ctx
->hash
[h
], HASH_PTE_SIZE_64
);
289 for (i
= 0; i
< 8; i
++) {
290 if (env
->external_htab
) {
291 pte0
= ldq_p(env
->external_htab
+ pteg_off
+ (i
* 16));
292 pte1
= ldq_p(env
->external_htab
+ pteg_off
+ (i
* 16) + 8);
294 pte0
= ldq_phys(env
->htab_base
+ pteg_off
+ (i
* 16));
295 pte1
= ldq_phys(env
->htab_base
+ pteg_off
+ (i
* 16) + 8);
298 r
= pte64_check(ctx
, pte0
, pte1
, h
, rw
, type
);
299 LOG_MMU("Load pte from %016" HWADDR_PRIx
" => " TARGET_FMT_lx
" "
300 TARGET_FMT_lx
" %d %d %d " TARGET_FMT_lx
"\n",
301 pteg_off
+ (i
* 16), pte0
, pte1
, (int)(pte0
& 1), h
,
302 (int)((pte0
>> 1) & 1), ctx
->ptem
);
305 /* PTE inconsistency */
308 /* Access violation */
318 /* XXX: we should go on looping to check all PTEs consistency
319 * but if we can speed-up the whole thing as the
320 * result would be undefined if PTEs are not consistent.
329 LOG_MMU("found PTE at addr %08" HWADDR_PRIx
" prot=%01x ret=%d\n",
330 ctx
->raddr
, ctx
->prot
, ret
);
331 /* Update page flags */
333 if (pte_update_flags(ctx
, &pte1
, ret
, rw
) == 1) {
334 if (env
->external_htab
) {
335 stq_p(env
->external_htab
+ pteg_off
+ (good
* 16) + 8,
338 stq_phys_notdirty(env
->htab_base
+ pteg_off
+
339 (good
* 16) + 8, pte1
);
344 /* We have a TLB that saves 4K pages, so let's
345 * split a huge page to 4k chunks */
346 if (target_page_bits
!= TARGET_PAGE_BITS
) {
347 ctx
->raddr
|= (ctx
->eaddr
& ((1 << target_page_bits
) - 1))