20 #include <linux/types.h>
21 #include <linux/string.h>
26 #include <asm/tlbflush.h>
35 #define dprintk(X...) printk(KERN_INFO X)
37 #define dprintk(X...) do { } while(0)
41 #define dprintk_pte(X...) printk(KERN_INFO X)
43 #define dprintk_pte(X...) do { } while(0)
46 #define PTEG_FLAG_ACCESSED 0x00000100
47 #define PTEG_FLAG_DIRTY 0x00000080
52 static inline bool check_debug_ip(
struct kvm_vcpu *vcpu)
54 #ifdef DEBUG_MMU_PTE_IP
55 return vcpu->
arch.pc == DEBUG_MMU_PTE_IP;
61 static inline u32 sr_vsid(
u32 sr_raw)
63 return sr_raw & 0x0fffffff;
66 static inline bool sr_valid(
u32 sr_raw)
68 return (sr_raw & 0x80000000) ?
false :
true;
71 static inline bool sr_ks(
u32 sr_raw)
73 return (sr_raw & 0x40000000) ?
true:
false;
76 static inline bool sr_kp(
u32 sr_raw)
78 return (sr_raw & 0x20000000) ?
true:
false;
81 static inline bool sr_nx(
u32 sr_raw)
83 return (sr_raw & 0x10000000) ?
true:
false;
86 static int kvmppc_mmu_book3s_32_xlate_bat(
struct kvm_vcpu *vcpu,
gva_t eaddr,
93 return vcpu->
arch.shared->sr[(eaddr >> 28) & 0xf];
102 if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, &
pte, data))
105 kvmppc_mmu_book3s_32_esid_to_vsid(vcpu, eaddr >>
SID_SHIFT, &vsid);
106 return (((
u64)eaddr >> 12) & 0xffff) | (vsid << 16);
109 static void kvmppc_mmu_book3s_32_reset_msr(
struct kvm_vcpu *vcpu)
121 page = (eaddr & 0x0FFFFFFF) >> 12;
122 htabmask = ((vcpu_book3s->
sdr1 & 0x1FF) << 16) | 0xFFC0;
124 hash = ((sr_vsid(sre) ^
page) << 6);
129 pteg = (vcpu_book3s->
sdr1 & 0xffff0000) | hash;
131 dprintk(
"MMU: pc=0x%lx eaddr=0x%lx sdr1=0x%llx pteg=0x%x vsid=0x%x\n",
132 kvmppc_get_pc(&vcpu_book3s->
vcpu), eaddr, vcpu_book3s->
sdr1, pteg,
136 if (kvm_is_error_hva(r))
141 static u32 kvmppc_mmu_book3s_32_get_ptem(
u32 sre,
gva_t eaddr,
bool primary)
143 return ((eaddr & 0x0fffffff) >> 22) | (sr_vsid(sre) << 7) |
144 (primary ? 0 : 0x40) | 0x80000000;
147 static int kvmppc_mmu_book3s_32_xlate_bat(
struct kvm_vcpu *vcpu,
gva_t eaddr,
154 for (i = 0; i < 8; i++) {
156 bat = &vcpu_book3s->
dbat[
i];
158 bat = &vcpu_book3s->
ibat[
i];
160 if (vcpu->
arch.shared->msr & MSR_PR) {
168 if (check_debug_ip(vcpu))
171 data ?
'd' :
'i', i, eaddr, bat->
bepi,
176 kvmppc_mmu_book3s_32_esid_to_vsid(vcpu,
179 pte->
vpage = (((
u64)eaddr >> 12) & 0xffff) | vsid;
202 static int kvmppc_mmu_book3s_32_xlate_pte(
struct kvm_vcpu *vcpu,
gva_t eaddr,
214 sre = find_sr(vcpu, eaddr);
216 dprintk_pte(
"SR 0x%lx: vsid=0x%x, raw=0x%x\n", eaddr >> 28,
219 pte->
vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
221 ptegp = kvmppc_mmu_book3s_32_get_pteg(vcpu_book3s, sre, eaddr, primary);
222 if (kvm_is_error_hva(ptegp)) {
227 ptem = kvmppc_mmu_book3s_32_get_ptem(sre, eaddr, primary);
234 for (i=0; i<16; i+=2) {
235 if (ptem == pteg[i]) {
238 pte->
raddr = (pteg[i+1] & ~(0xFFFULL)) | (eaddr & 0xFFF);
241 if ((sr_kp(sre) && (vcpu->
arch.shared->msr & MSR_PR)) ||
242 (sr_ks(sre) && !(vcpu->
arch.shared->msr & MSR_PR)))
265 pteg[i], pteg[i+1], pp);
274 u32 oldpte = pteg[i+1];
284 if (pteg[i+1] != oldpte)
292 if (check_debug_ip(vcpu)) {
293 dprintk_pte(
"KVM MMU: No PTE found (sdr1=0x%llx ptegp=0x%lx)\n",
294 to_book3s(vcpu)->
sdr1, ptegp);
295 for (i=0; i<16; i+=2) {
297 i, pteg[i], pteg[i+1], ptem);
304 static int kvmppc_mmu_book3s_32_xlate(
struct kvm_vcpu *vcpu,
gva_t eaddr,
314 unlikely((eaddr & ~0xfffULL) == (mp_ea & ~0xfffULL)) &&
315 !(vcpu->
arch.shared->msr & MSR_PR)) {
316 pte->
vpage = kvmppc_mmu_book3s_32_ea_to_vp(vcpu, eaddr, data);
326 r = kvmppc_mmu_book3s_32_xlate_bat(vcpu, eaddr, pte, data);
328 r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data,
true);
330 r = kvmppc_mmu_book3s_32_xlate_pte(vcpu, eaddr, pte, data,
false);
336 static u32 kvmppc_mmu_book3s_32_mfsrin(
struct kvm_vcpu *vcpu,
u32 srnum)
338 return vcpu->
arch.shared->sr[srnum];
341 static void kvmppc_mmu_book3s_32_mtsrin(
struct kvm_vcpu *vcpu,
u32 srnum,
348 static void kvmppc_mmu_book3s_32_tlbie(
struct kvm_vcpu *vcpu,
ulong ea,
bool large)
360 if (vcpu->
arch.shared->msr & (MSR_DR|MSR_IR)) {
361 sr = find_sr(vcpu, ea);
369 switch (vcpu->
arch.shared->msr & (MSR_DR|MSR_IR)) {
389 if (vcpu->
arch.shared->msr & MSR_PR)
395 static bool kvmppc_mmu_book3s_32_is_dcbz32(
struct kvm_vcpu *vcpu)
405 mmu->
mtsrin = kvmppc_mmu_book3s_32_mtsrin;
406 mmu->
mfsrin = kvmppc_mmu_book3s_32_mfsrin;
407 mmu->
xlate = kvmppc_mmu_book3s_32_xlate;
408 mmu->
reset_msr = kvmppc_mmu_book3s_32_reset_msr;
409 mmu->
tlbie = kvmppc_mmu_book3s_32_tlbie;
411 mmu->
ea_to_vp = kvmppc_mmu_book3s_32_ea_to_vp;
412 mmu->
is_dcbz32 = kvmppc_mmu_book3s_32_is_dcbz32;