2 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
4 * Author: Yu Liu, yu.liu@freescale.com
7 * This file is based on arch/powerpc/kvm/44x_tlb.c,
8 * by Hollis Blanchard <hollisb@us.ibm.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License, version 2, as
12 * published by the Free Software Foundation.
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/kvm.h>
19 #include <linux/kvm_host.h>
20 #include <linux/highmem.h>
21 #include <asm/kvm_ppc.h>
22 #include <asm/kvm_e500.h>
24 #include "../mm/mmu_decl.h"
29 #define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1)
31 static unsigned int tlb1_entry_num
;
33 void kvmppc_dump_tlbs(struct kvm_vcpu
*vcpu
)
35 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
39 printk("| %8s | %8s | %8s | %8s | %8s |\n",
40 "nr", "mas1", "mas2", "mas3", "mas7");
42 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
43 printk("Guest TLB%d:\n", tlbsel
);
44 for (i
= 0; i
< vcpu_e500
->guest_tlb_size
[tlbsel
]; i
++) {
45 tlbe
= &vcpu_e500
->guest_tlb
[tlbsel
][i
];
46 if (tlbe
->mas1
& MAS1_VALID
)
47 printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n",
48 tlbsel
, i
, tlbe
->mas1
, tlbe
->mas2
,
49 tlbe
->mas3
, tlbe
->mas7
);
53 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
54 printk("Shadow TLB%d:\n", tlbsel
);
55 for (i
= 0; i
< vcpu_e500
->shadow_tlb_size
[tlbsel
]; i
++) {
56 tlbe
= &vcpu_e500
->shadow_tlb
[tlbsel
][i
];
57 if (tlbe
->mas1
& MAS1_VALID
)
58 printk(" S[%d][%3d] | %08X | %08X | %08X | %08X |\n",
59 tlbsel
, i
, tlbe
->mas1
, tlbe
->mas2
,
60 tlbe
->mas3
, tlbe
->mas7
);
65 static inline unsigned int tlb0_get_next_victim(
66 struct kvmppc_vcpu_e500
*vcpu_e500
)
70 victim
= vcpu_e500
->guest_tlb_nv
[0]++;
71 if (unlikely(vcpu_e500
->guest_tlb_nv
[0] >= KVM_E500_TLB0_WAY_NUM
))
72 vcpu_e500
->guest_tlb_nv
[0] = 0;
77 static inline unsigned int tlb1_max_shadow_size(void)
79 return tlb1_entry_num
- tlbcam_index
;
82 static inline int tlbe_is_writable(struct tlbe
*tlbe
)
84 return tlbe
->mas3
& (MAS3_SW
|MAS3_UW
);
87 static inline u32
e500_shadow_mas3_attrib(u32 mas3
, int usermode
)
89 /* Mask off reserved bits. */
90 mas3
&= MAS3_ATTRIB_MASK
;
93 /* Guest is in supervisor mode,
94 * so we need to translate guest
95 * supervisor permissions into user permissions. */
96 mas3
&= ~E500_TLB_USER_PERM_MASK
;
97 mas3
|= (mas3
& E500_TLB_SUPER_PERM_MASK
) << 1;
100 return mas3
| E500_TLB_SUPER_PERM_MASK
;
103 static inline u32
e500_shadow_mas2_attrib(u32 mas2
, int usermode
)
106 return (mas2
& MAS2_ATTRIB_MASK
) | MAS2_M
;
108 return mas2
& MAS2_ATTRIB_MASK
;
113 * writing shadow tlb entry to host TLB
115 static inline void __write_host_tlbe(struct tlbe
*stlbe
)
117 mtspr(SPRN_MAS1
, stlbe
->mas1
);
118 mtspr(SPRN_MAS2
, stlbe
->mas2
);
119 mtspr(SPRN_MAS3
, stlbe
->mas3
);
120 mtspr(SPRN_MAS7
, stlbe
->mas7
);
121 __asm__
__volatile__ ("tlbwe\n" : : );
124 static inline void write_host_tlbe(struct kvmppc_vcpu_e500
*vcpu_e500
,
125 int tlbsel
, int esel
)
127 struct tlbe
*stlbe
= &vcpu_e500
->shadow_tlb
[tlbsel
][esel
];
131 __write_host_tlbe(stlbe
);
133 unsigned register mas0
;
135 mas0
= mfspr(SPRN_MAS0
);
137 mtspr(SPRN_MAS0
, MAS0_TLBSEL(1) | MAS0_ESEL(to_htlb1_esel(esel
)));
138 __write_host_tlbe(stlbe
);
140 mtspr(SPRN_MAS0
, mas0
);
145 void kvmppc_e500_tlb_load(struct kvm_vcpu
*vcpu
, int cpu
)
147 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
149 unsigned register mas0
;
151 /* Load all valid TLB1 entries to reduce guest tlb miss fault */
153 mas0
= mfspr(SPRN_MAS0
);
154 for (i
= 0; i
< tlb1_max_shadow_size(); i
++) {
155 struct tlbe
*stlbe
= &vcpu_e500
->shadow_tlb
[1][i
];
157 if (get_tlb_v(stlbe
)) {
158 mtspr(SPRN_MAS0
, MAS0_TLBSEL(1)
159 | MAS0_ESEL(to_htlb1_esel(i
)));
160 __write_host_tlbe(stlbe
);
163 mtspr(SPRN_MAS0
, mas0
);
167 void kvmppc_e500_tlb_put(struct kvm_vcpu
*vcpu
)
172 /* Search the guest TLB for a matching entry. */
173 static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500
*vcpu_e500
,
174 gva_t eaddr
, int tlbsel
, unsigned int pid
, int as
)
178 /* XXX Replace loop with fancy data structures. */
179 for (i
= 0; i
< vcpu_e500
->guest_tlb_size
[tlbsel
]; i
++) {
180 struct tlbe
*tlbe
= &vcpu_e500
->guest_tlb
[tlbsel
][i
];
183 if (eaddr
< get_tlb_eaddr(tlbe
))
186 if (eaddr
> get_tlb_end(tlbe
))
189 tid
= get_tlb_tid(tlbe
);
190 if (tid
&& (tid
!= pid
))
193 if (!get_tlb_v(tlbe
))
196 if (get_tlb_ts(tlbe
) != as
&& as
!= -1)
205 static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500
*vcpu_e500
,
206 int tlbsel
, int esel
)
208 struct tlbe
*stlbe
= &vcpu_e500
->shadow_tlb
[tlbsel
][esel
];
209 struct page
*page
= vcpu_e500
->shadow_pages
[tlbsel
][esel
];
212 vcpu_e500
->shadow_pages
[tlbsel
][esel
] = NULL
;
214 if (get_tlb_v(stlbe
)) {
215 if (tlbe_is_writable(stlbe
))
216 kvm_release_page_dirty(page
);
218 kvm_release_page_clean(page
);
223 static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500
*vcpu_e500
,
224 int tlbsel
, int esel
)
226 struct tlbe
*stlbe
= &vcpu_e500
->shadow_tlb
[tlbsel
][esel
];
228 kvmppc_e500_shadow_release(vcpu_e500
, tlbsel
, esel
);
230 trace_kvm_stlb_inval(index_of(tlbsel
, esel
));
233 static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500
*vcpu_e500
,
234 gva_t eaddr
, gva_t eend
, u32 tid
)
236 unsigned int pid
= tid
& 0xff;
239 /* XXX Replace loop with fancy data structures. */
240 for (i
= 0; i
< vcpu_e500
->guest_tlb_size
[1]; i
++) {
241 struct tlbe
*stlbe
= &vcpu_e500
->shadow_tlb
[1][i
];
244 if (!get_tlb_v(stlbe
))
247 if (eend
< get_tlb_eaddr(stlbe
))
250 if (eaddr
> get_tlb_end(stlbe
))
253 tid
= get_tlb_tid(stlbe
);
254 if (tid
&& (tid
!= pid
))
257 kvmppc_e500_stlbe_invalidate(vcpu_e500
, 1, i
);
258 write_host_tlbe(vcpu_e500
, 1, i
);
262 static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu
*vcpu
,
263 unsigned int eaddr
, int as
)
265 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
266 unsigned int victim
, pidsel
, tsized
;
269 /* since we only have two TLBs, only lower bit is used. */
270 tlbsel
= (vcpu_e500
->mas4
>> 28) & 0x1;
271 victim
= (tlbsel
== 0) ? tlb0_get_next_victim(vcpu_e500
) : 0;
272 pidsel
= (vcpu_e500
->mas4
>> 16) & 0xf;
273 tsized
= (vcpu_e500
->mas4
>> 7) & 0x1f;
275 vcpu_e500
->mas0
= MAS0_TLBSEL(tlbsel
) | MAS0_ESEL(victim
)
276 | MAS0_NV(vcpu_e500
->guest_tlb_nv
[tlbsel
]);
277 vcpu_e500
->mas1
= MAS1_VALID
| (as
? MAS1_TS
: 0)
278 | MAS1_TID(vcpu_e500
->pid
[pidsel
])
279 | MAS1_TSIZE(tsized
);
280 vcpu_e500
->mas2
= (eaddr
& MAS2_EPN
)
281 | (vcpu_e500
->mas4
& MAS2_ATTRIB_MASK
);
282 vcpu_e500
->mas3
&= MAS3_U0
| MAS3_U1
| MAS3_U2
| MAS3_U3
;
283 vcpu_e500
->mas6
= (vcpu_e500
->mas6
& MAS6_SPID1
)
284 | (get_cur_pid(vcpu
) << 16)
285 | (as
? MAS6_SAS
: 0);
289 static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
290 u64 gvaddr
, gfn_t gfn
, struct tlbe
*gtlbe
, int tlbsel
, int esel
)
292 struct page
*new_page
;
296 stlbe
= &vcpu_e500
->shadow_tlb
[tlbsel
][esel
];
298 /* Get reference to new page. */
299 new_page
= gfn_to_page(vcpu_e500
->vcpu
.kvm
, gfn
);
300 if (is_error_page(new_page
)) {
301 printk(KERN_ERR
"Couldn't get guest page for gfn %lx!\n",
303 kvm_release_page_clean(new_page
);
306 hpaddr
= page_to_phys(new_page
);
308 /* Drop reference to old page. */
309 kvmppc_e500_shadow_release(vcpu_e500
, tlbsel
, esel
);
311 vcpu_e500
->shadow_pages
[tlbsel
][esel
] = new_page
;
313 /* Force TS=1 IPROT=0 TSIZE=4KB for all guest mappings. */
314 stlbe
->mas1
= MAS1_TSIZE(BOOK3E_PAGESZ_4K
)
315 | MAS1_TID(get_tlb_tid(gtlbe
)) | MAS1_TS
| MAS1_VALID
;
316 stlbe
->mas2
= (gvaddr
& MAS2_EPN
)
317 | e500_shadow_mas2_attrib(gtlbe
->mas2
,
318 vcpu_e500
->vcpu
.arch
.shared
->msr
& MSR_PR
);
319 stlbe
->mas3
= (hpaddr
& MAS3_RPN
)
320 | e500_shadow_mas3_attrib(gtlbe
->mas3
,
321 vcpu_e500
->vcpu
.arch
.shared
->msr
& MSR_PR
);
322 stlbe
->mas7
= (hpaddr
>> 32) & MAS7_RPN
;
324 trace_kvm_stlb_write(index_of(tlbsel
, esel
), stlbe
->mas1
, stlbe
->mas2
,
325 stlbe
->mas3
, stlbe
->mas7
);
328 /* XXX only map the one-one case, for now use TLB0 */
329 static int kvmppc_e500_stlbe_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
330 int tlbsel
, int esel
)
334 gtlbe
= &vcpu_e500
->guest_tlb
[tlbsel
][esel
];
336 kvmppc_e500_shadow_map(vcpu_e500
, get_tlb_eaddr(gtlbe
),
337 get_tlb_raddr(gtlbe
) >> PAGE_SHIFT
,
338 gtlbe
, tlbsel
, esel
);
343 /* Caller must ensure that the specified guest TLB entry is safe to insert into
345 /* XXX for both one-one and one-to-many , for now use TLB1 */
346 static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500
*vcpu_e500
,
347 u64 gvaddr
, gfn_t gfn
, struct tlbe
*gtlbe
)
351 victim
= vcpu_e500
->guest_tlb_nv
[1]++;
353 if (unlikely(vcpu_e500
->guest_tlb_nv
[1] >= tlb1_max_shadow_size()))
354 vcpu_e500
->guest_tlb_nv
[1] = 0;
356 kvmppc_e500_shadow_map(vcpu_e500
, gvaddr
, gfn
, gtlbe
, 1, victim
);
361 /* Invalidate all guest kernel mappings when enter usermode,
362 * so that when they fault back in they will get the
363 * proper permission bits. */
364 void kvmppc_mmu_priv_switch(struct kvm_vcpu
*vcpu
, int usermode
)
367 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
370 /* XXX Replace loop with fancy data structures. */
371 for (i
= 0; i
< tlb1_max_shadow_size(); i
++)
372 kvmppc_e500_stlbe_invalidate(vcpu_e500
, 1, i
);
378 static int kvmppc_e500_gtlbe_invalidate(struct kvmppc_vcpu_e500
*vcpu_e500
,
379 int tlbsel
, int esel
)
381 struct tlbe
*gtlbe
= &vcpu_e500
->guest_tlb
[tlbsel
][esel
];
383 if (unlikely(get_tlb_iprot(gtlbe
)))
387 kvmppc_e500_tlb1_invalidate(vcpu_e500
, get_tlb_eaddr(gtlbe
),
391 kvmppc_e500_stlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
399 int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500
*vcpu_e500
, ulong value
)
403 if (value
& MMUCSR0_TLB0FI
)
404 for (esel
= 0; esel
< vcpu_e500
->guest_tlb_size
[0]; esel
++)
405 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, 0, esel
);
406 if (value
& MMUCSR0_TLB1FI
)
407 for (esel
= 0; esel
< vcpu_e500
->guest_tlb_size
[1]; esel
++)
408 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, 1, esel
);
415 int kvmppc_e500_emul_tlbivax(struct kvm_vcpu
*vcpu
, int ra
, int rb
)
417 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
422 ea
= ((ra
) ? kvmppc_get_gpr(vcpu
, ra
) : 0) + kvmppc_get_gpr(vcpu
, rb
);
424 ia
= (ea
>> 2) & 0x1;
426 /* since we only have two TLBs, only lower bit is used. */
427 tlbsel
= (ea
>> 3) & 0x1;
430 /* invalidate all entries */
431 for (esel
= 0; esel
< vcpu_e500
->guest_tlb_size
[tlbsel
]; esel
++)
432 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
435 esel
= kvmppc_e500_tlb_index(vcpu_e500
, ea
, tlbsel
,
436 get_cur_pid(vcpu
), -1);
438 kvmppc_e500_gtlbe_invalidate(vcpu_e500
, tlbsel
, esel
);
446 int kvmppc_e500_emul_tlbre(struct kvm_vcpu
*vcpu
)
448 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
452 tlbsel
= get_tlb_tlbsel(vcpu_e500
);
453 esel
= get_tlb_esel(vcpu_e500
, tlbsel
);
455 gtlbe
= &vcpu_e500
->guest_tlb
[tlbsel
][esel
];
456 vcpu_e500
->mas0
&= ~MAS0_NV(~0);
457 vcpu_e500
->mas0
|= MAS0_NV(vcpu_e500
->guest_tlb_nv
[tlbsel
]);
458 vcpu_e500
->mas1
= gtlbe
->mas1
;
459 vcpu_e500
->mas2
= gtlbe
->mas2
;
460 vcpu_e500
->mas3
= gtlbe
->mas3
;
461 vcpu_e500
->mas7
= gtlbe
->mas7
;
466 int kvmppc_e500_emul_tlbsx(struct kvm_vcpu
*vcpu
, int rb
)
468 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
469 int as
= !!get_cur_sas(vcpu_e500
);
470 unsigned int pid
= get_cur_spid(vcpu_e500
);
472 struct tlbe
*gtlbe
= NULL
;
475 ea
= kvmppc_get_gpr(vcpu
, rb
);
477 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
478 esel
= kvmppc_e500_tlb_index(vcpu_e500
, ea
, tlbsel
, pid
, as
);
480 gtlbe
= &vcpu_e500
->guest_tlb
[tlbsel
][esel
];
486 vcpu_e500
->mas0
= MAS0_TLBSEL(tlbsel
) | MAS0_ESEL(esel
)
487 | MAS0_NV(vcpu_e500
->guest_tlb_nv
[tlbsel
]);
488 vcpu_e500
->mas1
= gtlbe
->mas1
;
489 vcpu_e500
->mas2
= gtlbe
->mas2
;
490 vcpu_e500
->mas3
= gtlbe
->mas3
;
491 vcpu_e500
->mas7
= gtlbe
->mas7
;
495 /* since we only have two TLBs, only lower bit is used. */
496 tlbsel
= vcpu_e500
->mas4
>> 28 & 0x1;
497 victim
= (tlbsel
== 0) ? tlb0_get_next_victim(vcpu_e500
) : 0;
499 vcpu_e500
->mas0
= MAS0_TLBSEL(tlbsel
) | MAS0_ESEL(victim
)
500 | MAS0_NV(vcpu_e500
->guest_tlb_nv
[tlbsel
]);
501 vcpu_e500
->mas1
= (vcpu_e500
->mas6
& MAS6_SPID0
)
502 | (vcpu_e500
->mas6
& (MAS6_SAS
? MAS1_TS
: 0))
503 | (vcpu_e500
->mas4
& MAS4_TSIZED(~0));
504 vcpu_e500
->mas2
&= MAS2_EPN
;
505 vcpu_e500
->mas2
|= vcpu_e500
->mas4
& MAS2_ATTRIB_MASK
;
506 vcpu_e500
->mas3
&= MAS3_U0
| MAS3_U1
| MAS3_U2
| MAS3_U3
;
510 kvmppc_set_exit_type(vcpu
, EMULATED_TLBSX_EXITS
);
514 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu
*vcpu
)
516 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
521 int tlbsel
, esel
, stlbsel
, sesel
;
523 tlbsel
= get_tlb_tlbsel(vcpu_e500
);
524 esel
= get_tlb_esel(vcpu_e500
, tlbsel
);
526 gtlbe
= &vcpu_e500
->guest_tlb
[tlbsel
][esel
];
528 if (get_tlb_v(gtlbe
) && tlbsel
== 1) {
529 eaddr
= get_tlb_eaddr(gtlbe
);
530 tid
= get_tlb_tid(gtlbe
);
531 kvmppc_e500_tlb1_invalidate(vcpu_e500
, eaddr
,
532 get_tlb_end(gtlbe
), tid
);
535 gtlbe
->mas1
= vcpu_e500
->mas1
;
536 gtlbe
->mas2
= vcpu_e500
->mas2
;
537 gtlbe
->mas3
= vcpu_e500
->mas3
;
538 gtlbe
->mas7
= vcpu_e500
->mas7
;
540 trace_kvm_gtlb_write(vcpu_e500
->mas0
, gtlbe
->mas1
, gtlbe
->mas2
,
541 gtlbe
->mas3
, gtlbe
->mas7
);
543 /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
544 if (tlbe_is_host_safe(vcpu
, gtlbe
)) {
548 gtlbe
->mas1
&= ~MAS1_TSIZE(~0);
549 gtlbe
->mas1
|= MAS1_TSIZE(BOOK3E_PAGESZ_4K
);
552 sesel
= kvmppc_e500_stlbe_map(vcpu_e500
, 0, esel
);
558 eaddr
= get_tlb_eaddr(gtlbe
);
559 raddr
= get_tlb_raddr(gtlbe
);
561 /* Create a 4KB mapping on the host.
562 * If the guest wanted a large page,
563 * only the first 4KB is mapped here and the rest
564 * are mapped on the fly. */
566 sesel
= kvmppc_e500_tlb1_map(vcpu_e500
, eaddr
,
567 raddr
>> PAGE_SHIFT
, gtlbe
);
573 write_host_tlbe(vcpu_e500
, stlbsel
, sesel
);
576 kvmppc_set_exit_type(vcpu
, EMULATED_TLBWE_EXITS
);
580 int kvmppc_mmu_itlb_index(struct kvm_vcpu
*vcpu
, gva_t eaddr
)
582 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_IS
);
584 return kvmppc_e500_tlb_search(vcpu
, eaddr
, get_cur_pid(vcpu
), as
);
587 int kvmppc_mmu_dtlb_index(struct kvm_vcpu
*vcpu
, gva_t eaddr
)
589 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_DS
);
591 return kvmppc_e500_tlb_search(vcpu
, eaddr
, get_cur_pid(vcpu
), as
);
594 void kvmppc_mmu_itlb_miss(struct kvm_vcpu
*vcpu
)
596 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_IS
);
598 kvmppc_e500_deliver_tlb_miss(vcpu
, vcpu
->arch
.pc
, as
);
601 void kvmppc_mmu_dtlb_miss(struct kvm_vcpu
*vcpu
)
603 unsigned int as
= !!(vcpu
->arch
.shared
->msr
& MSR_DS
);
605 kvmppc_e500_deliver_tlb_miss(vcpu
, vcpu
->arch
.fault_dear
, as
);
608 gpa_t
kvmppc_mmu_xlate(struct kvm_vcpu
*vcpu
, unsigned int index
,
611 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
613 &vcpu_e500
->guest_tlb
[tlbsel_of(index
)][esel_of(index
)];
614 u64 pgmask
= get_tlb_bytes(gtlbe
) - 1;
616 return get_tlb_raddr(gtlbe
) | (eaddr
& pgmask
);
619 void kvmppc_mmu_destroy(struct kvm_vcpu
*vcpu
)
621 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
624 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++)
625 for (i
= 0; i
< vcpu_e500
->guest_tlb_size
[tlbsel
]; i
++)
626 kvmppc_e500_shadow_release(vcpu_e500
, tlbsel
, i
);
628 /* discard all guest mapping */
632 void kvmppc_mmu_map(struct kvm_vcpu
*vcpu
, u64 eaddr
, gpa_t gpaddr
,
635 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
636 int tlbsel
= tlbsel_of(index
);
637 int esel
= esel_of(index
);
647 gfn_t gfn
= gpaddr
>> PAGE_SHIFT
;
649 = &vcpu_e500
->guest_tlb
[tlbsel
][esel
];
652 sesel
= kvmppc_e500_tlb1_map(vcpu_e500
, eaddr
, gfn
, gtlbe
);
660 write_host_tlbe(vcpu_e500
, stlbsel
, sesel
);
663 int kvmppc_e500_tlb_search(struct kvm_vcpu
*vcpu
,
664 gva_t eaddr
, unsigned int pid
, int as
)
666 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
669 for (tlbsel
= 0; tlbsel
< 2; tlbsel
++) {
670 esel
= kvmppc_e500_tlb_index(vcpu_e500
, eaddr
, tlbsel
, pid
, as
);
672 return index_of(tlbsel
, esel
);
678 void kvmppc_set_pid(struct kvm_vcpu
*vcpu
, u32 pid
)
680 struct kvmppc_vcpu_e500
*vcpu_e500
= to_e500(vcpu
);
682 vcpu_e500
->pid
[0] = vcpu
->arch
.shadow_pid
=
683 vcpu
->arch
.pid
= pid
;
686 void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500
*vcpu_e500
)
690 /* Insert large initial mapping for guest. */
691 tlbe
= &vcpu_e500
->guest_tlb
[1][0];
692 tlbe
->mas1
= MAS1_VALID
| MAS1_TSIZE(BOOK3E_PAGESZ_256M
);
694 tlbe
->mas3
= E500_TLB_SUPER_PERM_MASK
;
697 /* 4K map for serial output. Used by kernel wrapper. */
698 tlbe
= &vcpu_e500
->guest_tlb
[1][1];
699 tlbe
->mas1
= MAS1_VALID
| MAS1_TSIZE(BOOK3E_PAGESZ_4K
);
700 tlbe
->mas2
= (0xe0004500 & 0xFFFFF000) | MAS2_I
| MAS2_G
;
701 tlbe
->mas3
= (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK
;
705 int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500
*vcpu_e500
)
707 tlb1_entry_num
= mfspr(SPRN_TLB1CFG
) & 0xFFF;
709 vcpu_e500
->guest_tlb_size
[0] = KVM_E500_TLB0_SIZE
;
710 vcpu_e500
->guest_tlb
[0] =
711 kzalloc(sizeof(struct tlbe
) * KVM_E500_TLB0_SIZE
, GFP_KERNEL
);
712 if (vcpu_e500
->guest_tlb
[0] == NULL
)
715 vcpu_e500
->shadow_tlb_size
[0] = KVM_E500_TLB0_SIZE
;
716 vcpu_e500
->shadow_tlb
[0] =
717 kzalloc(sizeof(struct tlbe
) * KVM_E500_TLB0_SIZE
, GFP_KERNEL
);
718 if (vcpu_e500
->shadow_tlb
[0] == NULL
)
721 vcpu_e500
->guest_tlb_size
[1] = KVM_E500_TLB1_SIZE
;
722 vcpu_e500
->guest_tlb
[1] =
723 kzalloc(sizeof(struct tlbe
) * KVM_E500_TLB1_SIZE
, GFP_KERNEL
);
724 if (vcpu_e500
->guest_tlb
[1] == NULL
)
725 goto err_out_shadow0
;
727 vcpu_e500
->shadow_tlb_size
[1] = tlb1_entry_num
;
728 vcpu_e500
->shadow_tlb
[1] =
729 kzalloc(sizeof(struct tlbe
) * tlb1_entry_num
, GFP_KERNEL
);
730 if (vcpu_e500
->shadow_tlb
[1] == NULL
)
733 vcpu_e500
->shadow_pages
[0] = (struct page
**)
734 kzalloc(sizeof(struct page
*) * KVM_E500_TLB0_SIZE
, GFP_KERNEL
);
735 if (vcpu_e500
->shadow_pages
[0] == NULL
)
736 goto err_out_shadow1
;
738 vcpu_e500
->shadow_pages
[1] = (struct page
**)
739 kzalloc(sizeof(struct page
*) * tlb1_entry_num
, GFP_KERNEL
);
740 if (vcpu_e500
->shadow_pages
[1] == NULL
)
743 /* Init TLB configuration register */
744 vcpu_e500
->tlb0cfg
= mfspr(SPRN_TLB0CFG
) & ~0xfffUL
;
745 vcpu_e500
->tlb0cfg
|= vcpu_e500
->guest_tlb_size
[0];
746 vcpu_e500
->tlb1cfg
= mfspr(SPRN_TLB1CFG
) & ~0xfffUL
;
747 vcpu_e500
->tlb1cfg
|= vcpu_e500
->guest_tlb_size
[1];
752 kfree(vcpu_e500
->shadow_pages
[0]);
754 kfree(vcpu_e500
->shadow_tlb
[1]);
756 kfree(vcpu_e500
->guest_tlb
[1]);
758 kfree(vcpu_e500
->shadow_tlb
[0]);
760 kfree(vcpu_e500
->guest_tlb
[0]);
765 void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500
*vcpu_e500
)
767 kfree(vcpu_e500
->shadow_pages
[1]);
768 kfree(vcpu_e500
->shadow_pages
[0]);
769 kfree(vcpu_e500
->shadow_tlb
[1]);
770 kfree(vcpu_e500
->guest_tlb
[1]);
771 kfree(vcpu_e500
->shadow_tlb
[0]);
772 kfree(vcpu_e500
->guest_tlb
[0]);
This page took 0.061495 seconds and 5 git commands to generate.