2 * Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved.
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License, version 2, as
10 * published by the Free Software Foundation.
12 * This program 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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <linux/kvm_host.h>
24 #include <asm/kvm_ppc.h>
25 #include <asm/kvm_book3s.h>
26 #include <asm/mmu-hash64.h>
27 #include <asm/machdep.h>
28 #include <asm/mmu_context.h>
29 #include <asm/hw_irq.h>
34 void kvmppc_mmu_invalidate_pte(struct kvm_vcpu
*vcpu
, struct hpte_cache
*pte
)
36 ppc_md
.hpte_invalidate(pte
->slot
, pte
->host_vpn
,
37 pte
->pagesize
, pte
->pagesize
, MMU_SEGSIZE_256M
,
41 /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
42 * a hash, so we don't waste cycles on looping */
43 static u16
kvmppc_sid_hash(struct kvm_vcpu
*vcpu
, u64 gvsid
)
45 return (u16
)(((gvsid
>> (SID_MAP_BITS
* 7)) & SID_MAP_MASK
) ^
46 ((gvsid
>> (SID_MAP_BITS
* 6)) & SID_MAP_MASK
) ^
47 ((gvsid
>> (SID_MAP_BITS
* 5)) & SID_MAP_MASK
) ^
48 ((gvsid
>> (SID_MAP_BITS
* 4)) & SID_MAP_MASK
) ^
49 ((gvsid
>> (SID_MAP_BITS
* 3)) & SID_MAP_MASK
) ^
50 ((gvsid
>> (SID_MAP_BITS
* 2)) & SID_MAP_MASK
) ^
51 ((gvsid
>> (SID_MAP_BITS
* 1)) & SID_MAP_MASK
) ^
52 ((gvsid
>> (SID_MAP_BITS
* 0)) & SID_MAP_MASK
));
56 static struct kvmppc_sid_map
*find_sid_vsid(struct kvm_vcpu
*vcpu
, u64 gvsid
)
58 struct kvmppc_sid_map
*map
;
61 if (kvmppc_get_msr(vcpu
) & MSR_PR
)
64 sid_map_mask
= kvmppc_sid_hash(vcpu
, gvsid
);
65 map
= &to_book3s(vcpu
)->sid_map
[sid_map_mask
];
66 if (map
->valid
&& (map
->guest_vsid
== gvsid
)) {
67 trace_kvm_book3s_slb_found(gvsid
, map
->host_vsid
);
71 map
= &to_book3s(vcpu
)->sid_map
[SID_MAP_MASK
- sid_map_mask
];
72 if (map
->valid
&& (map
->guest_vsid
== gvsid
)) {
73 trace_kvm_book3s_slb_found(gvsid
, map
->host_vsid
);
77 trace_kvm_book3s_slb_fail(sid_map_mask
, gvsid
);
81 int kvmppc_mmu_map_page(struct kvm_vcpu
*vcpu
, struct kvmppc_pte
*orig_pte
,
92 struct kvmppc_sid_map
*map
;
94 int hpsize
= MMU_PAGE_4K
;
96 unsigned long mmu_seq
;
97 struct kvm
*kvm
= vcpu
->kvm
;
98 struct hpte_cache
*cpte
;
99 unsigned long gfn
= orig_pte
->raddr
>> PAGE_SHIFT
;
102 /* used to check for invalidations in progress */
103 mmu_seq
= kvm
->mmu_notifier_seq
;
106 /* Get host physical address for gpa */
107 pfn
= kvmppc_gpa_to_pfn(vcpu
, orig_pte
->raddr
, iswrite
, &writable
);
108 if (is_error_noslot_pfn(pfn
)) {
109 printk(KERN_INFO
"Couldn't get guest page for gpa %lx!\n",
114 hpaddr
= pfn
<< PAGE_SHIFT
;
116 /* and write the mapping ea -> hpa into the pt */
117 vcpu
->arch
.mmu
.esid_to_vsid(vcpu
, orig_pte
->eaddr
>> SID_SHIFT
, &vsid
);
118 map
= find_sid_vsid(vcpu
, vsid
);
120 ret
= kvmppc_mmu_map_segment(vcpu
, orig_pte
->eaddr
);
122 map
= find_sid_vsid(vcpu
, vsid
);
125 printk(KERN_ERR
"KVM: Segment map for 0x%llx (0x%lx) failed\n",
126 vsid
, orig_pte
->eaddr
);
132 vpn
= hpt_vpn(orig_pte
->eaddr
, map
->host_vsid
, MMU_SEGSIZE_256M
);
134 kvm_set_pfn_accessed(pfn
);
135 if (!orig_pte
->may_write
|| !writable
)
138 mark_page_dirty(vcpu
->kvm
, gfn
);
139 kvm_set_pfn_dirty(pfn
);
142 if (!orig_pte
->may_execute
)
145 kvmppc_mmu_flush_icache(pfn
);
148 * Use 64K pages if possible; otherwise, on 64K page kernels,
149 * we need to transfer 4 more bits from guest real to host real addr.
152 hpsize
= MMU_PAGE_64K
;
154 hpaddr
|= orig_pte
->raddr
& (~0xfffULL
& ~PAGE_MASK
);
156 hash
= hpt_hash(vpn
, mmu_psize_defs
[hpsize
].shift
, MMU_SEGSIZE_256M
);
158 cpte
= kvmppc_mmu_hpte_cache_next(vcpu
);
160 spin_lock(&kvm
->mmu_lock
);
161 if (!cpte
|| mmu_notifier_retry(kvm
, mmu_seq
)) {
167 hpteg
= ((hash
& htab_hash_mask
) * HPTES_PER_GROUP
);
169 /* In case we tried normal mapping already, let's nuke old entries */
171 if (ppc_md
.hpte_remove(hpteg
) < 0) {
176 ret
= ppc_md
.hpte_insert(hpteg
, vpn
, hpaddr
, rflags
, vflags
,
177 hpsize
, hpsize
, MMU_SEGSIZE_256M
);
180 /* If we couldn't map a primary PTE, try a secondary */
182 vflags
^= HPTE_V_SECONDARY
;
186 trace_kvm_book3s_64_mmu_map(rflags
, hpteg
,
187 vpn
, hpaddr
, orig_pte
);
189 /* The ppc_md code may give us a secondary entry even though we
190 asked for a primary. Fix up. */
191 if ((ret
& _PTEIDX_SECONDARY
) && !(vflags
& HPTE_V_SECONDARY
)) {
193 hpteg
= ((hash
& htab_hash_mask
) * HPTES_PER_GROUP
);
196 cpte
->slot
= hpteg
+ (ret
& 7);
197 cpte
->host_vpn
= vpn
;
198 cpte
->pte
= *orig_pte
;
200 cpte
->pagesize
= hpsize
;
202 kvmppc_mmu_hpte_cache_map(vcpu
, cpte
);
207 spin_unlock(&kvm
->mmu_lock
);
208 kvm_release_pfn_clean(pfn
);
210 kvmppc_mmu_hpte_cache_free(cpte
);
216 void kvmppc_mmu_unmap_page(struct kvm_vcpu
*vcpu
, struct kvmppc_pte
*pte
)
218 u64 mask
= 0xfffffffffULL
;
221 vcpu
->arch
.mmu
.esid_to_vsid(vcpu
, pte
->eaddr
>> SID_SHIFT
, &vsid
);
223 mask
= 0xffffffff0ULL
;
224 kvmppc_mmu_pte_vflush(vcpu
, pte
->vpage
, mask
);
227 static struct kvmppc_sid_map
*create_sid_map(struct kvm_vcpu
*vcpu
, u64 gvsid
)
229 struct kvmppc_sid_map
*map
;
230 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
232 static int backwards_map
= 0;
234 if (kvmppc_get_msr(vcpu
) & MSR_PR
)
237 /* We might get collisions that trap in preceding order, so let's
238 map them differently */
240 sid_map_mask
= kvmppc_sid_hash(vcpu
, gvsid
);
242 sid_map_mask
= SID_MAP_MASK
- sid_map_mask
;
244 map
= &to_book3s(vcpu
)->sid_map
[sid_map_mask
];
246 /* Make sure we're taking the other map next time */
247 backwards_map
= !backwards_map
;
249 /* Uh-oh ... out of mappings. Let's flush! */
250 if (vcpu_book3s
->proto_vsid_next
== vcpu_book3s
->proto_vsid_max
) {
251 vcpu_book3s
->proto_vsid_next
= vcpu_book3s
->proto_vsid_first
;
252 memset(vcpu_book3s
->sid_map
, 0,
253 sizeof(struct kvmppc_sid_map
) * SID_MAP_NUM
);
254 kvmppc_mmu_pte_flush(vcpu
, 0, 0);
255 kvmppc_mmu_flush_segments(vcpu
);
257 map
->host_vsid
= vsid_scramble(vcpu_book3s
->proto_vsid_next
++, 256M
);
259 map
->guest_vsid
= gvsid
;
262 trace_kvm_book3s_slb_map(sid_map_mask
, gvsid
, map
->host_vsid
);
267 static int kvmppc_mmu_next_segment(struct kvm_vcpu
*vcpu
, ulong esid
)
269 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
271 int max_slb_size
= 64;
272 int found_inval
= -1;
275 /* Are we overwriting? */
276 for (i
= 0; i
< svcpu
->slb_max
; i
++) {
277 if (!(svcpu
->slb
[i
].esid
& SLB_ESID_V
))
279 else if ((svcpu
->slb
[i
].esid
& ESID_MASK
) == esid
) {
285 /* Found a spare entry that was invalidated before */
286 if (found_inval
>= 0) {
291 /* No spare invalid entry, so create one */
293 if (mmu_slb_size
< 64)
294 max_slb_size
= mmu_slb_size
;
296 /* Overflowing -> purge */
297 if ((svcpu
->slb_max
) == max_slb_size
)
298 kvmppc_mmu_flush_segments(vcpu
);
308 int kvmppc_mmu_map_segment(struct kvm_vcpu
*vcpu
, ulong eaddr
)
310 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
311 u64 esid
= eaddr
>> SID_SHIFT
;
312 u64 slb_esid
= (eaddr
& ESID_MASK
) | SLB_ESID_V
;
313 u64 slb_vsid
= SLB_VSID_USER
;
316 struct kvmppc_sid_map
*map
;
319 slb_index
= kvmppc_mmu_next_segment(vcpu
, eaddr
& ESID_MASK
);
321 if (vcpu
->arch
.mmu
.esid_to_vsid(vcpu
, esid
, &gvsid
)) {
322 /* Invalidate an entry */
323 svcpu
->slb
[slb_index
].esid
= 0;
328 map
= find_sid_vsid(vcpu
, gvsid
);
330 map
= create_sid_map(vcpu
, gvsid
);
332 map
->guest_esid
= esid
;
334 slb_vsid
|= (map
->host_vsid
<< 12);
335 slb_vsid
&= ~SLB_VSID_KP
;
336 slb_esid
|= slb_index
;
338 #ifdef CONFIG_PPC_64K_PAGES
339 /* Set host segment base page size to 64K if possible */
340 if (gvsid
& VSID_64K
)
341 slb_vsid
|= mmu_psize_defs
[MMU_PAGE_64K
].sllp
;
344 svcpu
->slb
[slb_index
].esid
= slb_esid
;
345 svcpu
->slb
[slb_index
].vsid
= slb_vsid
;
347 trace_kvm_book3s_slbmte(slb_vsid
, slb_esid
);
354 void kvmppc_mmu_flush_segment(struct kvm_vcpu
*vcpu
, ulong ea
, ulong seg_size
)
356 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
357 ulong seg_mask
= -seg_size
;
360 for (i
= 0; i
< svcpu
->slb_max
; i
++) {
361 if ((svcpu
->slb
[i
].esid
& SLB_ESID_V
) &&
362 (svcpu
->slb
[i
].esid
& seg_mask
) == ea
) {
363 /* Invalidate this entry */
364 svcpu
->slb
[i
].esid
= 0;
371 void kvmppc_mmu_flush_segments(struct kvm_vcpu
*vcpu
)
373 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
375 svcpu
->slb
[0].esid
= 0;
379 void kvmppc_mmu_destroy_pr(struct kvm_vcpu
*vcpu
)
381 kvmppc_mmu_hpte_destroy(vcpu
);
382 __destroy_context(to_book3s(vcpu
)->context_id
[0]);
385 int kvmppc_mmu_init(struct kvm_vcpu
*vcpu
)
387 struct kvmppc_vcpu_book3s
*vcpu3s
= to_book3s(vcpu
);
390 err
= __init_new_context();
393 vcpu3s
->context_id
[0] = err
;
395 vcpu3s
->proto_vsid_max
= ((u64
)(vcpu3s
->context_id
[0] + 1)
397 vcpu3s
->proto_vsid_first
= (u64
)vcpu3s
->context_id
[0] << ESID_BITS
;
398 vcpu3s
->proto_vsid_next
= vcpu3s
->proto_vsid_first
;
400 kvmppc_mmu_hpte_init(vcpu
);