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 */
33 #define dprintk(X...) printk(KERN_INFO X)
35 #define dprintk(X...) do { } while(0)
38 static void kvmppc_mmu_book3s_64_reset_msr(struct kvm_vcpu
*vcpu
)
40 kvmppc_set_msr(vcpu
, MSR_SF
);
43 static struct kvmppc_slb
*kvmppc_mmu_book3s_64_find_slbe(
44 struct kvm_vcpu
*vcpu
,
48 u64 esid
= GET_ESID(eaddr
);
49 u64 esid_1t
= GET_ESID_1T(eaddr
);
51 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
54 if (!vcpu
->arch
.slb
[i
].valid
)
57 if (vcpu
->arch
.slb
[i
].tb
)
60 if (vcpu
->arch
.slb
[i
].esid
== cmp_esid
)
61 return &vcpu
->arch
.slb
[i
];
64 dprintk("KVM: No SLB entry found for 0x%lx [%llx | %llx]\n",
65 eaddr
, esid
, esid_1t
);
66 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
67 if (vcpu
->arch
.slb
[i
].vsid
)
68 dprintk(" %d: %c%c%c %llx %llx\n", i
,
69 vcpu
->arch
.slb
[i
].valid
? 'v' : ' ',
70 vcpu
->arch
.slb
[i
].large
? 'l' : ' ',
71 vcpu
->arch
.slb
[i
].tb
? 't' : ' ',
72 vcpu
->arch
.slb
[i
].esid
,
73 vcpu
->arch
.slb
[i
].vsid
);
79 static u64
kvmppc_mmu_book3s_64_ea_to_vp(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
82 struct kvmppc_slb
*slb
;
84 slb
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, eaddr
);
89 return (((u64
)eaddr
>> 12) & 0xfffffff) |
90 (((u64
)slb
->vsid
) << 28);
92 return (((u64
)eaddr
>> 12) & 0xffff) | (((u64
)slb
->vsid
) << 16);
95 static int kvmppc_mmu_book3s_64_get_pagesize(struct kvmppc_slb
*slbe
)
97 return slbe
->large
? 24 : 12;
100 static u32
kvmppc_mmu_book3s_64_get_page(struct kvmppc_slb
*slbe
, gva_t eaddr
)
102 int p
= kvmppc_mmu_book3s_64_get_pagesize(slbe
);
103 return ((eaddr
& 0xfffffff) >> p
);
106 static hva_t
kvmppc_mmu_book3s_64_get_pteg(
107 struct kvmppc_vcpu_book3s
*vcpu_book3s
,
108 struct kvmppc_slb
*slbe
, gva_t eaddr
,
111 u64 hash
, pteg
, htabsize
;
115 page
= kvmppc_mmu_book3s_64_get_page(slbe
, eaddr
);
116 htabsize
= ((1 << ((vcpu_book3s
->sdr1
& 0x1f) + 11)) - 1);
118 hash
= slbe
->vsid
^ page
;
121 hash
&= ((1ULL << 39ULL) - 1ULL);
125 pteg
= vcpu_book3s
->sdr1
& 0xfffffffffffc0000ULL
;
128 dprintk("MMU: page=0x%x sdr1=0x%llx pteg=0x%llx vsid=0x%llx\n",
129 page
, vcpu_book3s
->sdr1
, pteg
, slbe
->vsid
);
131 r
= gfn_to_hva(vcpu_book3s
->vcpu
.kvm
, pteg
>> PAGE_SHIFT
);
132 if (kvm_is_error_hva(r
))
134 return r
| (pteg
& ~PAGE_MASK
);
137 static u64
kvmppc_mmu_book3s_64_get_avpn(struct kvmppc_slb
*slbe
, gva_t eaddr
)
139 int p
= kvmppc_mmu_book3s_64_get_pagesize(slbe
);
142 avpn
= kvmppc_mmu_book3s_64_get_page(slbe
, eaddr
);
143 avpn
|= slbe
->vsid
<< (28 - p
);
146 avpn
>>= ((80 - p
) - 56) - 8;
153 static int kvmppc_mmu_book3s_64_xlate(struct kvm_vcpu
*vcpu
, gva_t eaddr
,
154 struct kvmppc_pte
*gpte
, bool data
)
156 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
157 struct kvmppc_slb
*slbe
;
164 bool perm_err
= false;
166 ulong mp_ea
= vcpu
->arch
.magic_page_ea
;
168 /* Magic page override */
169 if (unlikely(mp_ea
) &&
170 unlikely((eaddr
& ~0xfffULL
) == (mp_ea
& ~0xfffULL
)) &&
171 !(vcpu
->arch
.shared
->msr
& MSR_PR
)) {
173 gpte
->vpage
= kvmppc_mmu_book3s_64_ea_to_vp(vcpu
, eaddr
, data
);
174 gpte
->raddr
= vcpu
->arch
.magic_page_pa
| (gpte
->raddr
& 0xfff);
175 gpte
->raddr
&= KVM_PAM
;
176 gpte
->may_execute
= true;
177 gpte
->may_read
= true;
178 gpte
->may_write
= true;
183 slbe
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, eaddr
);
188 ptegp
= kvmppc_mmu_book3s_64_get_pteg(vcpu_book3s
, slbe
, eaddr
, second
);
189 if (kvm_is_error_hva(ptegp
))
192 avpn
= kvmppc_mmu_book3s_64_get_avpn(slbe
, eaddr
);
194 if(copy_from_user(pteg
, (void __user
*)ptegp
, sizeof(pteg
))) {
195 printk(KERN_ERR
"KVM can't copy data from 0x%lx!\n", ptegp
);
199 if ((vcpu
->arch
.shared
->msr
& MSR_PR
) && slbe
->Kp
)
201 else if (!(vcpu
->arch
.shared
->msr
& MSR_PR
) && slbe
->Ks
)
204 for (i
=0; i
<16; i
+=2) {
209 if (!(v
& HPTE_V_VALID
))
212 if ((v
& HPTE_V_SECONDARY
) != second
)
216 if (HPTE_V_AVPN_VAL(avpn
) == HPTE_V_AVPN_VAL(v
)) {
217 u8 pp
= (r
& HPTE_R_PP
) | key
;
218 int eaddr_mask
= 0xFFF;
221 gpte
->vpage
= kvmppc_mmu_book3s_64_ea_to_vp(vcpu
,
225 eaddr_mask
= 0xFFFFFF;
226 gpte
->raddr
= (r
& HPTE_R_RPN
) | (eaddr
& eaddr_mask
);
227 gpte
->may_execute
= ((r
& HPTE_R_N
) ? false : true);
228 gpte
->may_read
= false;
229 gpte
->may_write
= false;
236 gpte
->may_write
= true;
241 gpte
->may_read
= true;
245 if (!gpte
->may_read
) {
250 dprintk("KVM MMU: Translated 0x%lx [0x%llx] -> 0x%llx "
252 eaddr
, avpn
, gpte
->vpage
, gpte
->raddr
);
258 /* Update PTE R and C bits, so the guest's swapper knows we used the
261 u32 oldr
= pteg
[i
+1];
263 if (gpte
->may_read
) {
264 /* Set the accessed flag */
265 pteg
[i
+1] |= HPTE_R_R
;
267 if (gpte
->may_write
) {
268 /* Set the dirty flag */
269 pteg
[i
+1] |= HPTE_R_C
;
271 dprintk("KVM: Mapping read-only page!\n");
274 /* Write back into the PTEG */
275 if (pteg
[i
+1] != oldr
)
276 copy_to_user((void __user
*)ptegp
, pteg
, sizeof(pteg
));
280 dprintk("KVM MMU: No PTE found (ea=0x%lx sdr1=0x%llx "
282 eaddr
, to_book3s(vcpu
)->sdr1
, ptegp
);
283 for (i
= 0; i
< 16; i
+= 2)
284 dprintk(" %02d: 0x%llx - 0x%llx (0x%llx)\n",
285 i
, pteg
[i
], pteg
[i
+1], avpn
);
288 second
= HPTE_V_SECONDARY
;
304 dprintk("KVM MMU: Trigger segment fault\n");
308 static void kvmppc_mmu_book3s_64_slbmte(struct kvm_vcpu
*vcpu
, u64 rs
, u64 rb
)
310 struct kvmppc_vcpu_book3s
*vcpu_book3s
;
313 struct kvmppc_slb
*slbe
;
315 dprintk("KVM MMU: slbmte(0x%llx, 0x%llx)\n", rs
, rb
);
317 vcpu_book3s
= to_book3s(vcpu
);
320 esid_1t
= GET_ESID_1T(rb
);
323 if (slb_nr
> vcpu
->arch
.slb_nr
)
326 slbe
= &vcpu
->arch
.slb
[slb_nr
];
328 slbe
->large
= (rs
& SLB_VSID_L
) ? 1 : 0;
329 slbe
->tb
= (rs
& SLB_VSID_B_1T
) ? 1 : 0;
330 slbe
->esid
= slbe
->tb
? esid_1t
: esid
;
331 slbe
->vsid
= rs
>> 12;
332 slbe
->valid
= (rb
& SLB_ESID_V
) ? 1 : 0;
333 slbe
->Ks
= (rs
& SLB_VSID_KS
) ? 1 : 0;
334 slbe
->Kp
= (rs
& SLB_VSID_KP
) ? 1 : 0;
335 slbe
->nx
= (rs
& SLB_VSID_N
) ? 1 : 0;
336 slbe
->class = (rs
& SLB_VSID_C
) ? 1 : 0;
338 slbe
->orige
= rb
& (ESID_MASK
| SLB_ESID_V
);
341 /* Map the new segment */
342 kvmppc_mmu_map_segment(vcpu
, esid
<< SID_SHIFT
);
345 static u64
kvmppc_mmu_book3s_64_slbmfee(struct kvm_vcpu
*vcpu
, u64 slb_nr
)
347 struct kvmppc_slb
*slbe
;
349 if (slb_nr
> vcpu
->arch
.slb_nr
)
352 slbe
= &vcpu
->arch
.slb
[slb_nr
];
357 static u64
kvmppc_mmu_book3s_64_slbmfev(struct kvm_vcpu
*vcpu
, u64 slb_nr
)
359 struct kvmppc_slb
*slbe
;
361 if (slb_nr
> vcpu
->arch
.slb_nr
)
364 slbe
= &vcpu
->arch
.slb
[slb_nr
];
369 static void kvmppc_mmu_book3s_64_slbie(struct kvm_vcpu
*vcpu
, u64 ea
)
371 struct kvmppc_slb
*slbe
;
373 dprintk("KVM MMU: slbie(0x%llx)\n", ea
);
375 slbe
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, ea
);
380 dprintk("KVM MMU: slbie(0x%llx, 0x%llx)\n", ea
, slbe
->esid
);
384 kvmppc_mmu_map_segment(vcpu
, ea
);
387 static void kvmppc_mmu_book3s_64_slbia(struct kvm_vcpu
*vcpu
)
391 dprintk("KVM MMU: slbia()\n");
393 for (i
= 1; i
< vcpu
->arch
.slb_nr
; i
++)
394 vcpu
->arch
.slb
[i
].valid
= false;
396 if (vcpu
->arch
.shared
->msr
& MSR_IR
) {
397 kvmppc_mmu_flush_segments(vcpu
);
398 kvmppc_mmu_map_segment(vcpu
, kvmppc_get_pc(vcpu
));
402 static void kvmppc_mmu_book3s_64_mtsrin(struct kvm_vcpu
*vcpu
, u32 srnum
,
408 * According to Book3 2.01 mtsrin is implemented as:
410 * The SLB entry specified by (RB)32:35 is loaded from register
413 * SLBE Bit Source SLB Field
415 * 0:31 0x0000_0000 ESID-0:31
416 * 32:35 (RB)32:35 ESID-32:35
418 * 37:61 0x00_0000|| 0b0 VSID-0:24
419 * 62:88 (RS)37:63 VSID-25:51
420 * 89:91 (RS)33:35 Ks Kp N
421 * 92 (RS)36 L ((RS)36 must be 0b0)
425 dprintk("KVM MMU: mtsrin(0x%x, 0x%lx)\n", srnum
, value
);
428 rb
|= (srnum
& 0xf) << 28;
429 /* Set the valid bit */
435 rs
|= (value
& 0xfffffff) << 12;
437 rs
|= ((value
>> 28) & 0x7) << 9;
439 kvmppc_mmu_book3s_64_slbmte(vcpu
, rs
, rb
);
442 static void kvmppc_mmu_book3s_64_tlbie(struct kvm_vcpu
*vcpu
, ulong va
,
445 u64 mask
= 0xFFFFFFFFFULL
;
447 dprintk("KVM MMU: tlbie(0x%lx)\n", va
);
450 mask
= 0xFFFFFF000ULL
;
451 kvmppc_mmu_pte_vflush(vcpu
, va
>> 12, mask
);
454 static int kvmppc_mmu_book3s_64_esid_to_vsid(struct kvm_vcpu
*vcpu
, ulong esid
,
457 ulong ea
= esid
<< SID_SHIFT
;
458 struct kvmppc_slb
*slb
;
460 ulong mp_ea
= vcpu
->arch
.magic_page_ea
;
462 if (vcpu
->arch
.shared
->msr
& (MSR_DR
|MSR_IR
)) {
463 slb
= kvmppc_mmu_book3s_64_find_slbe(vcpu
, ea
);
468 switch (vcpu
->arch
.shared
->msr
& (MSR_DR
|MSR_IR
)) {
470 *vsid
= VSID_REAL
| esid
;
473 *vsid
= VSID_REAL_IR
| gvsid
;
476 *vsid
= VSID_REAL_DR
| gvsid
;
489 if (vcpu
->arch
.shared
->msr
& MSR_PR
)
495 /* Catch magic page case */
496 if (unlikely(mp_ea
) &&
497 unlikely(esid
== (mp_ea
>> SID_SHIFT
)) &&
498 !(vcpu
->arch
.shared
->msr
& MSR_PR
)) {
499 *vsid
= VSID_REAL
| esid
;
506 static bool kvmppc_mmu_book3s_64_is_dcbz32(struct kvm_vcpu
*vcpu
)
508 return (to_book3s(vcpu
)->hid
[5] & 0x80);
511 void kvmppc_mmu_book3s_64_init(struct kvm_vcpu
*vcpu
)
513 struct kvmppc_mmu
*mmu
= &vcpu
->arch
.mmu
;
516 mmu
->mtsrin
= kvmppc_mmu_book3s_64_mtsrin
;
517 mmu
->slbmte
= kvmppc_mmu_book3s_64_slbmte
;
518 mmu
->slbmfee
= kvmppc_mmu_book3s_64_slbmfee
;
519 mmu
->slbmfev
= kvmppc_mmu_book3s_64_slbmfev
;
520 mmu
->slbie
= kvmppc_mmu_book3s_64_slbie
;
521 mmu
->slbia
= kvmppc_mmu_book3s_64_slbia
;
522 mmu
->xlate
= kvmppc_mmu_book3s_64_xlate
;
523 mmu
->reset_msr
= kvmppc_mmu_book3s_64_reset_msr
;
524 mmu
->tlbie
= kvmppc_mmu_book3s_64_tlbie
;
525 mmu
->esid_to_vsid
= kvmppc_mmu_book3s_64_esid_to_vsid
;
526 mmu
->ea_to_vp
= kvmppc_mmu_book3s_64_ea_to_vp
;
527 mmu
->is_dcbz32
= kvmppc_mmu_book3s_64_is_dcbz32
;
529 vcpu
->arch
.hflags
|= BOOK3S_HFLAG_SLB
;