2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 * Copyright SUSE Linux Products GmbH 2009
17 * Authors: Alexander Graf <agraf@suse.de>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kvm.h>
23 #include <linux/kvm_host.h>
24 #include <linux/highmem.h>
26 #include <asm/tlbflush.h>
27 #include <asm/kvm_ppc.h>
28 #include <asm/kvm_book3s.h>
30 /* #define DEBUG_MMU */
31 /* #define DEBUG_MMU_PTE */
32 /* #define DEBUG_MMU_PTE_IP 0xfff14c40 */
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
,
87 struct kvmppc_pte
*pte
, bool data
,
89 static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu
*vcpu
, ulong esid
,
92 static u32
find_sr(struct kvm_vcpu
*vcpu
, gva_t eaddr
)
94 return vcpu
->arch
.shared
->sr
[(eaddr
>> 28) & 0xf];
97 static u64
kvmppc_mmu_book3s_32_ea_to_vp(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
101 struct kvmppc_pte pte
;
103 if (!kvmppc_mmu_book3s_32_xlate_bat(vcpu
, eaddr
, &pte
, data
, false))
106 kvmppc_mmu_book3s_32_esid_to_vsid(vcpu
, eaddr
>> SID_SHIFT
, &vsid
);
107 return (((u64
)eaddr
>> 12) & 0xffff) | (vsid
<< 16);
110 static void kvmppc_mmu_book3s_32_reset_msr(struct kvm_vcpu
*vcpu
)
112 kvmppc_set_msr(vcpu
, 0);
115 static hva_t
kvmppc_mmu_book3s_32_get_pteg(struct kvm_vcpu
*vcpu
,
116 u32 sre
, gva_t eaddr
,
119 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
120 u32 page
, hash
, pteg
, htabmask
;
123 page
= (eaddr
& 0x0FFFFFFF) >> 12;
124 htabmask
= ((vcpu_book3s
->sdr1
& 0x1FF) << 16) | 0xFFC0;
126 hash
= ((sr_vsid(sre
) ^ page
) << 6);
131 pteg
= (vcpu_book3s
->sdr1
& 0xffff0000) | hash
;
133 dprintk("MMU: pc=0x%lx eaddr=0x%lx sdr1=0x%llx pteg=0x%x vsid=0x%x\n",
134 kvmppc_get_pc(&vcpu_book3s
->vcpu
), eaddr
, vcpu_book3s
->sdr1
, pteg
,
137 r
= gfn_to_hva(vcpu
->kvm
, pteg
>> PAGE_SHIFT
);
138 if (kvm_is_error_hva(r
))
140 return r
| (pteg
& ~PAGE_MASK
);
143 static u32
kvmppc_mmu_book3s_32_get_ptem(u32 sre
, gva_t eaddr
, bool primary
)
145 return ((eaddr
& 0x0fffffff) >> 22) | (sr_vsid(sre
) << 7) |
146 (primary
? 0 : 0x40) | 0x80000000;
149 static int kvmppc_mmu_book3s_32_xlate_bat(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
150 struct kvmppc_pte
*pte
, bool data
,
153 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
154 struct kvmppc_bat
*bat
;
157 for (i
= 0; i
< 8; i
++) {
159 bat
= &vcpu_book3s
->dbat
[i
];
161 bat
= &vcpu_book3s
->ibat
[i
];
163 if (vcpu
->arch
.shared
->msr
& MSR_PR
) {
171 if (check_debug_ip(vcpu
))
173 dprintk_pte("%cBAT %02d: 0x%lx - 0x%x (0x%x)\n",
174 data
? 'd' : 'i', i
, eaddr
, bat
->bepi
,
177 if ((eaddr
& bat
->bepi_mask
) == bat
->bepi
) {
179 kvmppc_mmu_book3s_32_esid_to_vsid(vcpu
,
180 eaddr
>> SID_SHIFT
, &vsid
);
182 pte
->vpage
= (((u64
)eaddr
>> 12) & 0xffff) | vsid
;
184 pte
->raddr
= bat
->brpn
| (eaddr
& ~bat
->bepi_mask
);
185 pte
->may_read
= bat
->pp
;
186 pte
->may_write
= bat
->pp
> 1;
187 pte
->may_execute
= true;
188 if (!pte
->may_read
) {
189 printk(KERN_INFO
"BAT is not readable!\n");
192 if (iswrite
&& !pte
->may_write
) {
193 dprintk_pte("BAT is read-only!\n");
204 static int kvmppc_mmu_book3s_32_xlate_pte(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
205 struct kvmppc_pte
*pte
, bool data
,
206 bool iswrite
, bool primary
)
215 sre
= find_sr(vcpu
, eaddr
);
217 dprintk_pte("SR 0x%lx: vsid=0x%x, raw=0x%x\n", eaddr
>> 28,
220 pte
->vpage
= kvmppc_mmu_book3s_32_ea_to_vp(vcpu
, eaddr
, data
);
222 ptegp
= kvmppc_mmu_book3s_32_get_pteg(vcpu
, sre
, eaddr
, primary
);
223 if (kvm_is_error_hva(ptegp
)) {
224 printk(KERN_INFO
"KVM: Invalid PTEG!\n");
228 ptem
= kvmppc_mmu_book3s_32_get_ptem(sre
, eaddr
, primary
);
230 if(copy_from_user(pteg
, (void __user
*)ptegp
, sizeof(pteg
))) {
231 printk(KERN_ERR
"KVM: Can't copy data from 0x%lx!\n", ptegp
);
235 for (i
=0; i
<16; i
+=2) {
236 if (ptem
== pteg
[i
]) {
239 pte
->raddr
= (pteg
[i
+1] & ~(0xFFFULL
)) | (eaddr
& 0xFFF);
242 if ((sr_kp(sre
) && (vcpu
->arch
.shared
->msr
& MSR_PR
)) ||
243 (sr_ks(sre
) && !(vcpu
->arch
.shared
->msr
& MSR_PR
)))
246 pte
->may_write
= false;
247 pte
->may_read
= false;
248 pte
->may_execute
= true;
254 pte
->may_write
= true;
258 pte
->may_read
= true;
262 dprintk_pte("MMU: Found PTE -> %x %x - %x\n",
263 pteg
[i
], pteg
[i
+1], pp
);
269 /* Update PTE C and A bits, so the guest's swapper knows we used the
272 u32 pte_r
= pteg
[i
+1];
273 char __user
*addr
= (char __user
*) &pteg
[i
+1];
276 * Use single-byte writes to update the HPTE, to
277 * conform to what real hardware does.
279 if (pte
->may_read
&& !(pte_r
& PTEG_FLAG_ACCESSED
)) {
280 pte_r
|= PTEG_FLAG_ACCESSED
;
281 put_user(pte_r
>> 8, addr
+ 2);
283 if (iswrite
&& pte
->may_write
&& !(pte_r
& PTEG_FLAG_DIRTY
)) {
284 pte_r
|= PTEG_FLAG_DIRTY
;
285 put_user(pte_r
, addr
+ 3);
287 if (!pte
->may_read
|| (iswrite
&& !pte
->may_write
))
294 if (check_debug_ip(vcpu
)) {
295 dprintk_pte("KVM MMU: No PTE found (sdr1=0x%llx ptegp=0x%lx)\n",
296 to_book3s(vcpu
)->sdr1
, ptegp
);
297 for (i
=0; i
<16; i
+=2) {
298 dprintk_pte(" %02d: 0x%x - 0x%x (0x%x)\n",
299 i
, pteg
[i
], pteg
[i
+1], ptem
);
306 static int kvmppc_mmu_book3s_32_xlate(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
307 struct kvmppc_pte
*pte
, bool data
,
311 ulong mp_ea
= vcpu
->arch
.magic_page_ea
;
314 pte
->page_size
= MMU_PAGE_4K
;
316 /* Magic page override */
317 if (unlikely(mp_ea
) &&
318 unlikely((eaddr
& ~0xfffULL
) == (mp_ea
& ~0xfffULL
)) &&
319 !(vcpu
->arch
.shared
->msr
& MSR_PR
)) {
320 pte
->vpage
= kvmppc_mmu_book3s_32_ea_to_vp(vcpu
, eaddr
, data
);
321 pte
->raddr
= vcpu
->arch
.magic_page_pa
| (pte
->raddr
& 0xfff);
322 pte
->raddr
&= KVM_PAM
;
323 pte
->may_execute
= true;
324 pte
->may_read
= true;
325 pte
->may_write
= true;
330 r
= kvmppc_mmu_book3s_32_xlate_bat(vcpu
, eaddr
, pte
, data
, iswrite
);
332 r
= kvmppc_mmu_book3s_32_xlate_pte(vcpu
, eaddr
, pte
,
333 data
, iswrite
, true);
335 r
= kvmppc_mmu_book3s_32_xlate_pte(vcpu
, eaddr
, pte
,
336 data
, iswrite
, false);
342 static u32
kvmppc_mmu_book3s_32_mfsrin(struct kvm_vcpu
*vcpu
, u32 srnum
)
344 return vcpu
->arch
.shared
->sr
[srnum
];
347 static void kvmppc_mmu_book3s_32_mtsrin(struct kvm_vcpu
*vcpu
, u32 srnum
,
350 vcpu
->arch
.shared
->sr
[srnum
] = value
;
351 kvmppc_mmu_map_segment(vcpu
, srnum
<< SID_SHIFT
);
354 static void kvmppc_mmu_book3s_32_tlbie(struct kvm_vcpu
*vcpu
, ulong ea
, bool large
)
359 /* flush this VA on all cpus */
360 kvm_for_each_vcpu(i
, v
, vcpu
->kvm
)
361 kvmppc_mmu_pte_flush(v
, ea
, 0x0FFFF000);
364 static int kvmppc_mmu_book3s_32_esid_to_vsid(struct kvm_vcpu
*vcpu
, ulong esid
,
367 ulong ea
= esid
<< SID_SHIFT
;
371 if (vcpu
->arch
.shared
->msr
& (MSR_DR
|MSR_IR
)) {
372 sr
= find_sr(vcpu
, ea
);
377 /* In case we only have one of MSR_IR or MSR_DR set, let's put
378 that in the real-mode context (and hope RM doesn't access
380 switch (vcpu
->arch
.shared
->msr
& (MSR_DR
|MSR_IR
)) {
382 *vsid
= VSID_REAL
| esid
;
385 *vsid
= VSID_REAL_IR
| gvsid
;
388 *vsid
= VSID_REAL_DR
| gvsid
;
394 *vsid
= VSID_BAT
| gvsid
;
400 if (vcpu
->arch
.shared
->msr
& MSR_PR
)
406 static bool kvmppc_mmu_book3s_32_is_dcbz32(struct kvm_vcpu
*vcpu
)
412 void kvmppc_mmu_book3s_32_init(struct kvm_vcpu
*vcpu
)
414 struct kvmppc_mmu
*mmu
= &vcpu
->arch
.mmu
;
416 mmu
->mtsrin
= kvmppc_mmu_book3s_32_mtsrin
;
417 mmu
->mfsrin
= kvmppc_mmu_book3s_32_mfsrin
;
418 mmu
->xlate
= kvmppc_mmu_book3s_32_xlate
;
419 mmu
->reset_msr
= kvmppc_mmu_book3s_32_reset_msr
;
420 mmu
->tlbie
= kvmppc_mmu_book3s_32_tlbie
;
421 mmu
->esid_to_vsid
= kvmppc_mmu_book3s_32_esid_to_vsid
;
422 mmu
->ea_to_vp
= kvmppc_mmu_book3s_32_ea_to_vp
;
423 mmu
->is_dcbz32
= kvmppc_mmu_book3s_32_is_dcbz32
;