]>
Commit | Line | Data |
---|---|---|
de56a948 PM |
1 | /* |
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. | |
5 | * | |
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. | |
10 | * | |
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. | |
14 | * | |
15 | * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> | |
16 | */ | |
17 | ||
18 | #include <linux/types.h> | |
19 | #include <linux/string.h> | |
20 | #include <linux/kvm.h> | |
21 | #include <linux/kvm_host.h> | |
22 | #include <linux/highmem.h> | |
23 | #include <linux/gfp.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/hugetlb.h> | |
8936dda4 | 26 | #include <linux/vmalloc.h> |
de56a948 PM |
27 | |
28 | #include <asm/tlbflush.h> | |
29 | #include <asm/kvm_ppc.h> | |
30 | #include <asm/kvm_book3s.h> | |
31 | #include <asm/mmu-hash64.h> | |
32 | #include <asm/hvcall.h> | |
33 | #include <asm/synch.h> | |
34 | #include <asm/ppc-opcode.h> | |
35 | #include <asm/cputable.h> | |
36 | ||
9e368f29 PM |
37 | /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */ |
38 | #define MAX_LPID_970 63 | |
de56a948 PM |
39 | #define NR_LPIDS (LPID_RSVD + 1) |
40 | unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)]; | |
41 | ||
42 | long kvmppc_alloc_hpt(struct kvm *kvm) | |
43 | { | |
44 | unsigned long hpt; | |
45 | unsigned long lpid; | |
8936dda4 | 46 | struct revmap_entry *rev; |
de56a948 | 47 | |
8936dda4 | 48 | /* Allocate guest's hashed page table */ |
de56a948 PM |
49 | hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN, |
50 | HPT_ORDER - PAGE_SHIFT); | |
51 | if (!hpt) { | |
52 | pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n"); | |
53 | return -ENOMEM; | |
54 | } | |
55 | kvm->arch.hpt_virt = hpt; | |
56 | ||
8936dda4 PM |
57 | /* Allocate reverse map array */ |
58 | rev = vmalloc(sizeof(struct revmap_entry) * HPT_NPTE); | |
59 | if (!rev) { | |
60 | pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n"); | |
61 | goto out_freehpt; | |
62 | } | |
63 | kvm->arch.revmap = rev; | |
64 | ||
65 | /* Allocate the guest's logical partition ID */ | |
de56a948 PM |
66 | do { |
67 | lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS); | |
68 | if (lpid >= NR_LPIDS) { | |
69 | pr_err("kvm_alloc_hpt: No LPIDs free\n"); | |
8936dda4 | 70 | goto out_freeboth; |
de56a948 PM |
71 | } |
72 | } while (test_and_set_bit(lpid, lpid_inuse)); | |
73 | ||
74 | kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18); | |
75 | kvm->arch.lpid = lpid; | |
de56a948 PM |
76 | |
77 | pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid); | |
78 | return 0; | |
8936dda4 PM |
79 | |
80 | out_freeboth: | |
81 | vfree(rev); | |
82 | out_freehpt: | |
83 | free_pages(hpt, HPT_ORDER - PAGE_SHIFT); | |
84 | return -ENOMEM; | |
de56a948 PM |
85 | } |
86 | ||
87 | void kvmppc_free_hpt(struct kvm *kvm) | |
88 | { | |
de56a948 | 89 | clear_bit(kvm->arch.lpid, lpid_inuse); |
8936dda4 | 90 | vfree(kvm->arch.revmap); |
de56a948 | 91 | free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT); |
de56a948 PM |
92 | } |
93 | ||
da9d1d7f PM |
94 | /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */ |
95 | static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize) | |
96 | { | |
97 | return (pgsize > 0x1000) ? HPTE_V_LARGE : 0; | |
98 | } | |
99 | ||
100 | /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */ | |
101 | static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize) | |
102 | { | |
103 | return (pgsize == 0x10000) ? 0x1000 : 0; | |
104 | } | |
105 | ||
106 | void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot, | |
107 | unsigned long porder) | |
de56a948 PM |
108 | { |
109 | unsigned long i; | |
b2b2f165 | 110 | unsigned long npages; |
c77162de PM |
111 | unsigned long hp_v, hp_r; |
112 | unsigned long addr, hash; | |
da9d1d7f PM |
113 | unsigned long psize; |
114 | unsigned long hp0, hp1; | |
c77162de | 115 | long ret; |
de56a948 | 116 | |
da9d1d7f PM |
117 | psize = 1ul << porder; |
118 | npages = memslot->npages >> (porder - PAGE_SHIFT); | |
de56a948 PM |
119 | |
120 | /* VRMA can't be > 1TB */ | |
8936dda4 PM |
121 | if (npages > 1ul << (40 - porder)) |
122 | npages = 1ul << (40 - porder); | |
de56a948 PM |
123 | /* Can't use more than 1 HPTE per HPTEG */ |
124 | if (npages > HPT_NPTEG) | |
125 | npages = HPT_NPTEG; | |
126 | ||
da9d1d7f PM |
127 | hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) | |
128 | HPTE_V_BOLTED | hpte0_pgsize_encoding(psize); | |
129 | hp1 = hpte1_pgsize_encoding(psize) | | |
130 | HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX; | |
131 | ||
de56a948 | 132 | for (i = 0; i < npages; ++i) { |
c77162de | 133 | addr = i << porder; |
de56a948 PM |
134 | /* can't use hpt_hash since va > 64 bits */ |
135 | hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK; | |
136 | /* | |
137 | * We assume that the hash table is empty and no | |
138 | * vcpus are using it at this stage. Since we create | |
139 | * at most one HPTE per HPTEG, we just assume entry 7 | |
140 | * is available and use it. | |
141 | */ | |
8936dda4 | 142 | hash = (hash << 3) + 7; |
da9d1d7f PM |
143 | hp_v = hp0 | ((addr >> 16) & ~0x7fUL); |
144 | hp_r = hp1 | addr; | |
c77162de PM |
145 | ret = kvmppc_virtmode_h_enter(vcpu, H_EXACT, hash, hp_v, hp_r); |
146 | if (ret != H_SUCCESS) { | |
147 | pr_err("KVM: map_vrma at %lx failed, ret=%ld\n", | |
148 | addr, ret); | |
149 | break; | |
150 | } | |
de56a948 PM |
151 | } |
152 | } | |
153 | ||
154 | int kvmppc_mmu_hv_init(void) | |
155 | { | |
9e368f29 PM |
156 | unsigned long host_lpid, rsvd_lpid; |
157 | ||
158 | if (!cpu_has_feature(CPU_FTR_HVMODE)) | |
de56a948 | 159 | return -EINVAL; |
9e368f29 | 160 | |
de56a948 | 161 | memset(lpid_inuse, 0, sizeof(lpid_inuse)); |
9e368f29 PM |
162 | |
163 | if (cpu_has_feature(CPU_FTR_ARCH_206)) { | |
164 | host_lpid = mfspr(SPRN_LPID); /* POWER7 */ | |
165 | rsvd_lpid = LPID_RSVD; | |
166 | } else { | |
167 | host_lpid = 0; /* PPC970 */ | |
168 | rsvd_lpid = MAX_LPID_970; | |
169 | } | |
170 | ||
171 | set_bit(host_lpid, lpid_inuse); | |
172 | /* rsvd_lpid is reserved for use in partition switching */ | |
173 | set_bit(rsvd_lpid, lpid_inuse); | |
de56a948 PM |
174 | |
175 | return 0; | |
176 | } | |
177 | ||
178 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) | |
179 | { | |
180 | } | |
181 | ||
182 | static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu) | |
183 | { | |
184 | kvmppc_set_msr(vcpu, MSR_SF | MSR_ME); | |
185 | } | |
186 | ||
c77162de PM |
187 | /* |
188 | * This is called to get a reference to a guest page if there isn't | |
189 | * one already in the kvm->arch.slot_phys[][] arrays. | |
190 | */ | |
191 | static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn, | |
da9d1d7f PM |
192 | struct kvm_memory_slot *memslot, |
193 | unsigned long psize) | |
c77162de PM |
194 | { |
195 | unsigned long start; | |
da9d1d7f PM |
196 | long np, err; |
197 | struct page *page, *hpage, *pages[1]; | |
198 | unsigned long s, pgsize; | |
c77162de | 199 | unsigned long *physp; |
9d0ef5ea PM |
200 | unsigned int is_io, got, pgorder; |
201 | struct vm_area_struct *vma; | |
da9d1d7f | 202 | unsigned long pfn, i, npages; |
c77162de PM |
203 | |
204 | physp = kvm->arch.slot_phys[memslot->id]; | |
205 | if (!physp) | |
206 | return -EINVAL; | |
da9d1d7f | 207 | if (physp[gfn - memslot->base_gfn]) |
c77162de PM |
208 | return 0; |
209 | ||
9d0ef5ea PM |
210 | is_io = 0; |
211 | got = 0; | |
c77162de | 212 | page = NULL; |
da9d1d7f | 213 | pgsize = psize; |
9d0ef5ea | 214 | err = -EINVAL; |
c77162de PM |
215 | start = gfn_to_hva_memslot(memslot, gfn); |
216 | ||
217 | /* Instantiate and get the page we want access to */ | |
218 | np = get_user_pages_fast(start, 1, 1, pages); | |
9d0ef5ea PM |
219 | if (np != 1) { |
220 | /* Look up the vma for the page */ | |
221 | down_read(¤t->mm->mmap_sem); | |
222 | vma = find_vma(current->mm, start); | |
223 | if (!vma || vma->vm_start > start || | |
224 | start + psize > vma->vm_end || | |
225 | !(vma->vm_flags & VM_PFNMAP)) | |
226 | goto up_err; | |
227 | is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot)); | |
228 | pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); | |
229 | /* check alignment of pfn vs. requested page size */ | |
230 | if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1))) | |
231 | goto up_err; | |
232 | up_read(¤t->mm->mmap_sem); | |
233 | ||
234 | } else { | |
235 | page = pages[0]; | |
236 | got = KVMPPC_GOT_PAGE; | |
237 | ||
238 | /* See if this is a large page */ | |
239 | s = PAGE_SIZE; | |
240 | if (PageHuge(page)) { | |
241 | hpage = compound_head(page); | |
242 | s <<= compound_order(hpage); | |
243 | /* Get the whole large page if slot alignment is ok */ | |
244 | if (s > psize && slot_is_aligned(memslot, s) && | |
245 | !(memslot->userspace_addr & (s - 1))) { | |
246 | start &= ~(s - 1); | |
247 | pgsize = s; | |
248 | page = hpage; | |
249 | } | |
da9d1d7f | 250 | } |
9d0ef5ea PM |
251 | if (s < psize) |
252 | goto out; | |
253 | pfn = page_to_pfn(page); | |
c77162de | 254 | } |
c77162de | 255 | |
da9d1d7f PM |
256 | npages = pgsize >> PAGE_SHIFT; |
257 | pgorder = __ilog2(npages); | |
258 | physp += (gfn - memslot->base_gfn) & ~(npages - 1); | |
c77162de | 259 | spin_lock(&kvm->arch.slot_phys_lock); |
da9d1d7f PM |
260 | for (i = 0; i < npages; ++i) { |
261 | if (!physp[i]) { | |
9d0ef5ea PM |
262 | physp[i] = ((pfn + i) << PAGE_SHIFT) + |
263 | got + is_io + pgorder; | |
da9d1d7f PM |
264 | got = 0; |
265 | } | |
266 | } | |
c77162de | 267 | spin_unlock(&kvm->arch.slot_phys_lock); |
da9d1d7f | 268 | err = 0; |
c77162de | 269 | |
da9d1d7f PM |
270 | out: |
271 | if (got) { | |
272 | if (PageHuge(page)) | |
273 | page = compound_head(page); | |
274 | put_page(page); | |
275 | } | |
276 | return err; | |
9d0ef5ea PM |
277 | |
278 | up_err: | |
279 | up_read(¤t->mm->mmap_sem); | |
280 | return err; | |
c77162de PM |
281 | } |
282 | ||
283 | /* | |
284 | * We come here on a H_ENTER call from the guest when | |
285 | * we don't have the requested page pinned already. | |
286 | */ | |
287 | long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags, | |
288 | long pte_index, unsigned long pteh, unsigned long ptel) | |
289 | { | |
290 | struct kvm *kvm = vcpu->kvm; | |
291 | unsigned long psize, gpa, gfn; | |
292 | struct kvm_memory_slot *memslot; | |
293 | long ret; | |
294 | ||
295 | psize = hpte_page_size(pteh, ptel); | |
296 | if (!psize) | |
297 | return H_PARAMETER; | |
298 | ||
697d3899 PM |
299 | pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID); |
300 | ||
c77162de PM |
301 | /* Find the memslot (if any) for this address */ |
302 | gpa = (ptel & HPTE_R_RPN) & ~(psize - 1); | |
303 | gfn = gpa >> PAGE_SHIFT; | |
304 | memslot = gfn_to_memslot(kvm, gfn); | |
697d3899 PM |
305 | if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) { |
306 | if (!slot_is_aligned(memslot, psize)) | |
307 | return H_PARAMETER; | |
308 | if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0) | |
309 | return H_PARAMETER; | |
310 | } | |
c77162de PM |
311 | |
312 | preempt_disable(); | |
313 | ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel); | |
314 | preempt_enable(); | |
315 | if (ret == H_TOO_HARD) { | |
316 | /* this can't happen */ | |
317 | pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n"); | |
318 | ret = H_RESOURCE; /* or something */ | |
319 | } | |
320 | return ret; | |
321 | ||
322 | } | |
323 | ||
697d3899 PM |
324 | static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu, |
325 | gva_t eaddr) | |
326 | { | |
327 | u64 mask; | |
328 | int i; | |
329 | ||
330 | for (i = 0; i < vcpu->arch.slb_nr; i++) { | |
331 | if (!(vcpu->arch.slb[i].orige & SLB_ESID_V)) | |
332 | continue; | |
333 | ||
334 | if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T) | |
335 | mask = ESID_MASK_1T; | |
336 | else | |
337 | mask = ESID_MASK; | |
338 | ||
339 | if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0) | |
340 | return &vcpu->arch.slb[i]; | |
341 | } | |
342 | return NULL; | |
343 | } | |
344 | ||
345 | static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r, | |
346 | unsigned long ea) | |
347 | { | |
348 | unsigned long ra_mask; | |
349 | ||
350 | ra_mask = hpte_page_size(v, r) - 1; | |
351 | return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask); | |
352 | } | |
353 | ||
de56a948 | 354 | static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, |
697d3899 | 355 | struct kvmppc_pte *gpte, bool data) |
de56a948 | 356 | { |
697d3899 PM |
357 | struct kvm *kvm = vcpu->kvm; |
358 | struct kvmppc_slb *slbe; | |
359 | unsigned long slb_v; | |
360 | unsigned long pp, key; | |
361 | unsigned long v, gr; | |
362 | unsigned long *hptep; | |
363 | int index; | |
364 | int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR); | |
365 | ||
366 | /* Get SLB entry */ | |
367 | if (virtmode) { | |
368 | slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr); | |
369 | if (!slbe) | |
370 | return -EINVAL; | |
371 | slb_v = slbe->origv; | |
372 | } else { | |
373 | /* real mode access */ | |
374 | slb_v = vcpu->kvm->arch.vrma_slb_v; | |
375 | } | |
376 | ||
377 | /* Find the HPTE in the hash table */ | |
378 | index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v, | |
379 | HPTE_V_VALID | HPTE_V_ABSENT); | |
380 | if (index < 0) | |
381 | return -ENOENT; | |
382 | hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4)); | |
383 | v = hptep[0] & ~HPTE_V_HVLOCK; | |
384 | gr = kvm->arch.revmap[index].guest_rpte; | |
385 | ||
386 | /* Unlock the HPTE */ | |
387 | asm volatile("lwsync" : : : "memory"); | |
388 | hptep[0] = v; | |
389 | ||
390 | gpte->eaddr = eaddr; | |
391 | gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff); | |
392 | ||
393 | /* Get PP bits and key for permission check */ | |
394 | pp = gr & (HPTE_R_PP0 | HPTE_R_PP); | |
395 | key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS; | |
396 | key &= slb_v; | |
397 | ||
398 | /* Calculate permissions */ | |
399 | gpte->may_read = hpte_read_permission(pp, key); | |
400 | gpte->may_write = hpte_write_permission(pp, key); | |
401 | gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G)); | |
402 | ||
403 | /* Storage key permission check for POWER7 */ | |
404 | if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) { | |
405 | int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr); | |
406 | if (amrfield & 1) | |
407 | gpte->may_read = 0; | |
408 | if (amrfield & 2) | |
409 | gpte->may_write = 0; | |
410 | } | |
411 | ||
412 | /* Get the guest physical address */ | |
413 | gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr); | |
414 | return 0; | |
415 | } | |
416 | ||
417 | /* | |
418 | * Quick test for whether an instruction is a load or a store. | |
419 | * If the instruction is a load or a store, then this will indicate | |
420 | * which it is, at least on server processors. (Embedded processors | |
421 | * have some external PID instructions that don't follow the rule | |
422 | * embodied here.) If the instruction isn't a load or store, then | |
423 | * this doesn't return anything useful. | |
424 | */ | |
425 | static int instruction_is_store(unsigned int instr) | |
426 | { | |
427 | unsigned int mask; | |
428 | ||
429 | mask = 0x10000000; | |
430 | if ((instr & 0xfc000000) == 0x7c000000) | |
431 | mask = 0x100; /* major opcode 31 */ | |
432 | return (instr & mask) != 0; | |
433 | } | |
434 | ||
435 | static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
436 | unsigned long gpa, int is_store) | |
437 | { | |
438 | int ret; | |
439 | u32 last_inst; | |
440 | unsigned long srr0 = kvmppc_get_pc(vcpu); | |
441 | ||
442 | /* We try to load the last instruction. We don't let | |
443 | * emulate_instruction do it as it doesn't check what | |
444 | * kvmppc_ld returns. | |
445 | * If we fail, we just return to the guest and try executing it again. | |
446 | */ | |
447 | if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) { | |
448 | ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false); | |
449 | if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED) | |
450 | return RESUME_GUEST; | |
451 | vcpu->arch.last_inst = last_inst; | |
452 | } | |
453 | ||
454 | /* | |
455 | * WARNING: We do not know for sure whether the instruction we just | |
456 | * read from memory is the same that caused the fault in the first | |
457 | * place. If the instruction we read is neither an load or a store, | |
458 | * then it can't access memory, so we don't need to worry about | |
459 | * enforcing access permissions. So, assuming it is a load or | |
460 | * store, we just check that its direction (load or store) is | |
461 | * consistent with the original fault, since that's what we | |
462 | * checked the access permissions against. If there is a mismatch | |
463 | * we just return and retry the instruction. | |
464 | */ | |
465 | ||
466 | if (instruction_is_store(vcpu->arch.last_inst) != !!is_store) | |
467 | return RESUME_GUEST; | |
468 | ||
469 | /* | |
470 | * Emulated accesses are emulated by looking at the hash for | |
471 | * translation once, then performing the access later. The | |
472 | * translation could be invalidated in the meantime in which | |
473 | * point performing the subsequent memory access on the old | |
474 | * physical address could possibly be a security hole for the | |
475 | * guest (but not the host). | |
476 | * | |
477 | * This is less of an issue for MMIO stores since they aren't | |
478 | * globally visible. It could be an issue for MMIO loads to | |
479 | * a certain extent but we'll ignore it for now. | |
480 | */ | |
481 | ||
482 | vcpu->arch.paddr_accessed = gpa; | |
483 | return kvmppc_emulate_mmio(run, vcpu); | |
484 | } | |
485 | ||
486 | int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
487 | unsigned long ea, unsigned long dsisr) | |
488 | { | |
489 | struct kvm *kvm = vcpu->kvm; | |
490 | unsigned long *hptep, hpte[3]; | |
491 | unsigned long psize; | |
492 | unsigned long gfn; | |
493 | struct kvm_memory_slot *memslot; | |
494 | struct revmap_entry *rev; | |
495 | long index; | |
496 | ||
497 | /* | |
498 | * Real-mode code has already searched the HPT and found the | |
499 | * entry we're interested in. Lock the entry and check that | |
500 | * it hasn't changed. If it has, just return and re-execute the | |
501 | * instruction. | |
502 | */ | |
503 | if (ea != vcpu->arch.pgfault_addr) | |
504 | return RESUME_GUEST; | |
505 | index = vcpu->arch.pgfault_index; | |
506 | hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4)); | |
507 | rev = &kvm->arch.revmap[index]; | |
508 | preempt_disable(); | |
509 | while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) | |
510 | cpu_relax(); | |
511 | hpte[0] = hptep[0] & ~HPTE_V_HVLOCK; | |
512 | hpte[1] = hptep[1]; | |
513 | hpte[2] = rev->guest_rpte; | |
514 | asm volatile("lwsync" : : : "memory"); | |
515 | hptep[0] = hpte[0]; | |
516 | preempt_enable(); | |
517 | ||
518 | if (hpte[0] != vcpu->arch.pgfault_hpte[0] || | |
519 | hpte[1] != vcpu->arch.pgfault_hpte[1]) | |
520 | return RESUME_GUEST; | |
521 | ||
522 | /* Translate the logical address and get the page */ | |
523 | psize = hpte_page_size(hpte[0], hpte[1]); | |
524 | gfn = hpte_rpn(hpte[2], psize); | |
525 | memslot = gfn_to_memslot(kvm, gfn); | |
526 | ||
527 | /* No memslot means it's an emulated MMIO region */ | |
528 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) { | |
529 | unsigned long gpa = (gfn << PAGE_SHIFT) | (ea & (psize - 1)); | |
530 | return kvmppc_hv_emulate_mmio(run, vcpu, gpa, | |
531 | dsisr & DSISR_ISSTORE); | |
532 | } | |
533 | ||
534 | /* should never get here otherwise */ | |
535 | return -EFAULT; | |
de56a948 PM |
536 | } |
537 | ||
93e60249 PM |
538 | void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa, |
539 | unsigned long *nb_ret) | |
540 | { | |
541 | struct kvm_memory_slot *memslot; | |
542 | unsigned long gfn = gpa >> PAGE_SHIFT; | |
543 | struct page *page; | |
da9d1d7f PM |
544 | unsigned long psize, offset; |
545 | unsigned long pa; | |
93e60249 PM |
546 | unsigned long *physp; |
547 | ||
548 | memslot = gfn_to_memslot(kvm, gfn); | |
549 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) | |
550 | return NULL; | |
551 | physp = kvm->arch.slot_phys[memslot->id]; | |
552 | if (!physp) | |
553 | return NULL; | |
da9d1d7f | 554 | physp += gfn - memslot->base_gfn; |
93e60249 | 555 | pa = *physp; |
c77162de | 556 | if (!pa) { |
da9d1d7f | 557 | if (kvmppc_get_guest_page(kvm, gfn, memslot, PAGE_SIZE) < 0) |
c77162de PM |
558 | return NULL; |
559 | pa = *physp; | |
560 | } | |
da9d1d7f PM |
561 | page = pfn_to_page(pa >> PAGE_SHIFT); |
562 | psize = PAGE_SIZE; | |
563 | if (PageHuge(page)) { | |
564 | page = compound_head(page); | |
565 | psize <<= compound_order(page); | |
566 | } | |
93e60249 | 567 | get_page(page); |
da9d1d7f | 568 | offset = gpa & (psize - 1); |
93e60249 | 569 | if (nb_ret) |
da9d1d7f | 570 | *nb_ret = psize - offset; |
93e60249 PM |
571 | return page_address(page) + offset; |
572 | } | |
573 | ||
574 | void kvmppc_unpin_guest_page(struct kvm *kvm, void *va) | |
575 | { | |
576 | struct page *page = virt_to_page(va); | |
577 | ||
578 | page = compound_head(page); | |
579 | put_page(page); | |
580 | } | |
581 | ||
de56a948 PM |
582 | void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu) |
583 | { | |
584 | struct kvmppc_mmu *mmu = &vcpu->arch.mmu; | |
585 | ||
9e368f29 PM |
586 | if (cpu_has_feature(CPU_FTR_ARCH_206)) |
587 | vcpu->arch.slb_nr = 32; /* POWER7 */ | |
588 | else | |
589 | vcpu->arch.slb_nr = 64; | |
de56a948 PM |
590 | |
591 | mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate; | |
592 | mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr; | |
593 | ||
594 | vcpu->arch.hflags |= BOOK3S_HFLAG_SLB; | |
595 | } |