blob: c3beaeef3f60b41013c723197b2b898cf47f3d1b [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
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>
Paul Mackerras8936dda2011-12-12 12:27:39 +000026#include <linux/vmalloc.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027
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
Paul Mackerras9e368f22011-06-29 00:40:08 +000037/* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
38#define MAX_LPID_970 63
Paul Mackerrasde56a942011-06-29 00:21:34 +000039#define NR_LPIDS (LPID_RSVD + 1)
40unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)];
41
42long kvmppc_alloc_hpt(struct kvm *kvm)
43{
44 unsigned long hpt;
45 unsigned long lpid;
Paul Mackerras8936dda2011-12-12 12:27:39 +000046 struct revmap_entry *rev;
Alexander Grafd2a1b482012-01-16 19:12:11 +010047 struct kvmppc_linear_info *li;
Paul Mackerrasde56a942011-06-29 00:21:34 +000048
Paul Mackerras8936dda2011-12-12 12:27:39 +000049 /* Allocate guest's hashed page table */
Alexander Grafd2a1b482012-01-16 19:12:11 +010050 li = kvm_alloc_hpt();
51 if (li) {
52 /* using preallocated memory */
53 hpt = (ulong)li->base_virt;
54 kvm->arch.hpt_li = li;
55 } else {
56 /* using dynamic memory */
57 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
58 __GFP_NOWARN, HPT_ORDER - PAGE_SHIFT);
59 }
60
Paul Mackerrasde56a942011-06-29 00:21:34 +000061 if (!hpt) {
62 pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
63 return -ENOMEM;
64 }
65 kvm->arch.hpt_virt = hpt;
66
Paul Mackerras8936dda2011-12-12 12:27:39 +000067 /* Allocate reverse map array */
68 rev = vmalloc(sizeof(struct revmap_entry) * HPT_NPTE);
69 if (!rev) {
70 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
71 goto out_freehpt;
72 }
73 kvm->arch.revmap = rev;
74
75 /* Allocate the guest's logical partition ID */
Paul Mackerrasde56a942011-06-29 00:21:34 +000076 do {
77 lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS);
78 if (lpid >= NR_LPIDS) {
79 pr_err("kvm_alloc_hpt: No LPIDs free\n");
Paul Mackerras8936dda2011-12-12 12:27:39 +000080 goto out_freeboth;
Paul Mackerrasde56a942011-06-29 00:21:34 +000081 }
82 } while (test_and_set_bit(lpid, lpid_inuse));
83
84 kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18);
85 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +000086
87 pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid);
88 return 0;
Paul Mackerras8936dda2011-12-12 12:27:39 +000089
90 out_freeboth:
91 vfree(rev);
92 out_freehpt:
93 free_pages(hpt, HPT_ORDER - PAGE_SHIFT);
94 return -ENOMEM;
Paul Mackerrasde56a942011-06-29 00:21:34 +000095}
96
97void kvmppc_free_hpt(struct kvm *kvm)
98{
Paul Mackerrasde56a942011-06-29 00:21:34 +000099 clear_bit(kvm->arch.lpid, lpid_inuse);
Paul Mackerras8936dda2011-12-12 12:27:39 +0000100 vfree(kvm->arch.revmap);
Alexander Grafd2a1b482012-01-16 19:12:11 +0100101 if (kvm->arch.hpt_li)
102 kvm_release_hpt(kvm->arch.hpt_li);
103 else
104 free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000105}
106
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000107/* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
108static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000109{
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000110 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
111}
112
113/* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
114static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
115{
116 return (pgsize == 0x10000) ? 0x1000 : 0;
117}
118
119void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
120 unsigned long porder)
121{
Paul Mackerrasde56a942011-06-29 00:21:34 +0000122 unsigned long i;
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000123 unsigned long npages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000124 unsigned long hp_v, hp_r;
125 unsigned long addr, hash;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000126 unsigned long psize;
127 unsigned long hp0, hp1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000128 long ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000129
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000130 psize = 1ul << porder;
131 npages = memslot->npages >> (porder - PAGE_SHIFT);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000132
133 /* VRMA can't be > 1TB */
Paul Mackerras8936dda2011-12-12 12:27:39 +0000134 if (npages > 1ul << (40 - porder))
135 npages = 1ul << (40 - porder);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000136 /* Can't use more than 1 HPTE per HPTEG */
137 if (npages > HPT_NPTEG)
138 npages = HPT_NPTEG;
139
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000140 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
141 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
142 hp1 = hpte1_pgsize_encoding(psize) |
143 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
144
Paul Mackerrasde56a942011-06-29 00:21:34 +0000145 for (i = 0; i < npages; ++i) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000146 addr = i << porder;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000147 /* can't use hpt_hash since va > 64 bits */
148 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
149 /*
150 * We assume that the hash table is empty and no
151 * vcpus are using it at this stage. Since we create
152 * at most one HPTE per HPTEG, we just assume entry 7
153 * is available and use it.
154 */
Paul Mackerras8936dda2011-12-12 12:27:39 +0000155 hash = (hash << 3) + 7;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000156 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
157 hp_r = hp1 | addr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000158 ret = kvmppc_virtmode_h_enter(vcpu, H_EXACT, hash, hp_v, hp_r);
159 if (ret != H_SUCCESS) {
160 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
161 addr, ret);
162 break;
163 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000164 }
165}
166
167int kvmppc_mmu_hv_init(void)
168{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000169 unsigned long host_lpid, rsvd_lpid;
170
171 if (!cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000172 return -EINVAL;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000173
Paul Mackerrasde56a942011-06-29 00:21:34 +0000174 memset(lpid_inuse, 0, sizeof(lpid_inuse));
Paul Mackerras9e368f22011-06-29 00:40:08 +0000175
176 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
177 host_lpid = mfspr(SPRN_LPID); /* POWER7 */
178 rsvd_lpid = LPID_RSVD;
179 } else {
180 host_lpid = 0; /* PPC970 */
181 rsvd_lpid = MAX_LPID_970;
182 }
183
184 set_bit(host_lpid, lpid_inuse);
185 /* rsvd_lpid is reserved for use in partition switching */
186 set_bit(rsvd_lpid, lpid_inuse);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000187
188 return 0;
189}
190
191void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
192{
193}
194
195static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
196{
197 kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
198}
199
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000200/*
201 * This is called to get a reference to a guest page if there isn't
202 * one already in the kvm->arch.slot_phys[][] arrays.
203 */
204static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000205 struct kvm_memory_slot *memslot,
206 unsigned long psize)
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000207{
208 unsigned long start;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000209 long np, err;
210 struct page *page, *hpage, *pages[1];
211 unsigned long s, pgsize;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000212 unsigned long *physp;
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000213 unsigned int is_io, got, pgorder;
214 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000215 unsigned long pfn, i, npages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000216
217 physp = kvm->arch.slot_phys[memslot->id];
218 if (!physp)
219 return -EINVAL;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000220 if (physp[gfn - memslot->base_gfn])
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000221 return 0;
222
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000223 is_io = 0;
224 got = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000225 page = NULL;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000226 pgsize = psize;
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000227 err = -EINVAL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000228 start = gfn_to_hva_memslot(memslot, gfn);
229
230 /* Instantiate and get the page we want access to */
231 np = get_user_pages_fast(start, 1, 1, pages);
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000232 if (np != 1) {
233 /* Look up the vma for the page */
234 down_read(&current->mm->mmap_sem);
235 vma = find_vma(current->mm, start);
236 if (!vma || vma->vm_start > start ||
237 start + psize > vma->vm_end ||
238 !(vma->vm_flags & VM_PFNMAP))
239 goto up_err;
240 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
241 pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
242 /* check alignment of pfn vs. requested page size */
243 if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1)))
244 goto up_err;
245 up_read(&current->mm->mmap_sem);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000246
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000247 } else {
248 page = pages[0];
249 got = KVMPPC_GOT_PAGE;
250
251 /* See if this is a large page */
252 s = PAGE_SIZE;
253 if (PageHuge(page)) {
254 hpage = compound_head(page);
255 s <<= compound_order(hpage);
256 /* Get the whole large page if slot alignment is ok */
257 if (s > psize && slot_is_aligned(memslot, s) &&
258 !(memslot->userspace_addr & (s - 1))) {
259 start &= ~(s - 1);
260 pgsize = s;
David Gibsonde6c0b02012-05-08 20:24:08 +1000261 get_page(hpage);
262 put_page(page);
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000263 page = hpage;
264 }
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000265 }
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000266 if (s < psize)
267 goto out;
268 pfn = page_to_pfn(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000269 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000270
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000271 npages = pgsize >> PAGE_SHIFT;
272 pgorder = __ilog2(npages);
273 physp += (gfn - memslot->base_gfn) & ~(npages - 1);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000274 spin_lock(&kvm->arch.slot_phys_lock);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000275 for (i = 0; i < npages; ++i) {
276 if (!physp[i]) {
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000277 physp[i] = ((pfn + i) << PAGE_SHIFT) +
278 got + is_io + pgorder;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000279 got = 0;
280 }
281 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000282 spin_unlock(&kvm->arch.slot_phys_lock);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000283 err = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000284
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000285 out:
David Gibsonde6c0b02012-05-08 20:24:08 +1000286 if (got)
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000287 put_page(page);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000288 return err;
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000289
290 up_err:
291 up_read(&current->mm->mmap_sem);
292 return err;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000293}
294
295/*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000296 * We come here on a H_ENTER call from the guest when we are not
297 * using mmu notifiers and we don't have the requested page pinned
298 * already.
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000299 */
300long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
301 long pte_index, unsigned long pteh, unsigned long ptel)
302{
303 struct kvm *kvm = vcpu->kvm;
304 unsigned long psize, gpa, gfn;
305 struct kvm_memory_slot *memslot;
306 long ret;
307
Paul Mackerras342d3db2011-12-12 12:38:05 +0000308 if (kvm->arch.using_mmu_notifiers)
309 goto do_insert;
310
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000311 psize = hpte_page_size(pteh, ptel);
312 if (!psize)
313 return H_PARAMETER;
314
Paul Mackerras697d3892011-12-12 12:36:37 +0000315 pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
316
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000317 /* Find the memslot (if any) for this address */
318 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
319 gfn = gpa >> PAGE_SHIFT;
320 memslot = gfn_to_memslot(kvm, gfn);
Paul Mackerras697d3892011-12-12 12:36:37 +0000321 if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
322 if (!slot_is_aligned(memslot, psize))
323 return H_PARAMETER;
324 if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
325 return H_PARAMETER;
326 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000327
Paul Mackerras342d3db2011-12-12 12:38:05 +0000328 do_insert:
329 /* Protect linux PTE lookup from page table destruction */
330 rcu_read_lock_sched(); /* this disables preemption too */
331 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000332 ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000333 rcu_read_unlock_sched();
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000334 if (ret == H_TOO_HARD) {
335 /* this can't happen */
336 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
337 ret = H_RESOURCE; /* or something */
338 }
339 return ret;
340
341}
342
Paul Mackerras697d3892011-12-12 12:36:37 +0000343static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
344 gva_t eaddr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000345{
Paul Mackerras697d3892011-12-12 12:36:37 +0000346 u64 mask;
347 int i;
348
349 for (i = 0; i < vcpu->arch.slb_nr; i++) {
350 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
351 continue;
352
353 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
354 mask = ESID_MASK_1T;
355 else
356 mask = ESID_MASK;
357
358 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
359 return &vcpu->arch.slb[i];
360 }
361 return NULL;
362}
363
364static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
365 unsigned long ea)
366{
367 unsigned long ra_mask;
368
369 ra_mask = hpte_page_size(v, r) - 1;
370 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
371}
372
373static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
374 struct kvmppc_pte *gpte, bool data)
375{
376 struct kvm *kvm = vcpu->kvm;
377 struct kvmppc_slb *slbe;
378 unsigned long slb_v;
379 unsigned long pp, key;
380 unsigned long v, gr;
381 unsigned long *hptep;
382 int index;
383 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
384
385 /* Get SLB entry */
386 if (virtmode) {
387 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
388 if (!slbe)
389 return -EINVAL;
390 slb_v = slbe->origv;
391 } else {
392 /* real mode access */
393 slb_v = vcpu->kvm->arch.vrma_slb_v;
394 }
395
396 /* Find the HPTE in the hash table */
397 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
398 HPTE_V_VALID | HPTE_V_ABSENT);
399 if (index < 0)
400 return -ENOENT;
401 hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
402 v = hptep[0] & ~HPTE_V_HVLOCK;
403 gr = kvm->arch.revmap[index].guest_rpte;
404
405 /* Unlock the HPTE */
406 asm volatile("lwsync" : : : "memory");
407 hptep[0] = v;
408
409 gpte->eaddr = eaddr;
410 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
411
412 /* Get PP bits and key for permission check */
413 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
414 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
415 key &= slb_v;
416
417 /* Calculate permissions */
418 gpte->may_read = hpte_read_permission(pp, key);
419 gpte->may_write = hpte_write_permission(pp, key);
420 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
421
422 /* Storage key permission check for POWER7 */
423 if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
424 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
425 if (amrfield & 1)
426 gpte->may_read = 0;
427 if (amrfield & 2)
428 gpte->may_write = 0;
429 }
430
431 /* Get the guest physical address */
432 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
433 return 0;
434}
435
436/*
437 * Quick test for whether an instruction is a load or a store.
438 * If the instruction is a load or a store, then this will indicate
439 * which it is, at least on server processors. (Embedded processors
440 * have some external PID instructions that don't follow the rule
441 * embodied here.) If the instruction isn't a load or store, then
442 * this doesn't return anything useful.
443 */
444static int instruction_is_store(unsigned int instr)
445{
446 unsigned int mask;
447
448 mask = 0x10000000;
449 if ((instr & 0xfc000000) == 0x7c000000)
450 mask = 0x100; /* major opcode 31 */
451 return (instr & mask) != 0;
452}
453
454static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
455 unsigned long gpa, int is_store)
456{
457 int ret;
458 u32 last_inst;
459 unsigned long srr0 = kvmppc_get_pc(vcpu);
460
461 /* We try to load the last instruction. We don't let
462 * emulate_instruction do it as it doesn't check what
463 * kvmppc_ld returns.
464 * If we fail, we just return to the guest and try executing it again.
465 */
466 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) {
467 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
468 if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED)
469 return RESUME_GUEST;
470 vcpu->arch.last_inst = last_inst;
471 }
472
473 /*
474 * WARNING: We do not know for sure whether the instruction we just
475 * read from memory is the same that caused the fault in the first
476 * place. If the instruction we read is neither an load or a store,
477 * then it can't access memory, so we don't need to worry about
478 * enforcing access permissions. So, assuming it is a load or
479 * store, we just check that its direction (load or store) is
480 * consistent with the original fault, since that's what we
481 * checked the access permissions against. If there is a mismatch
482 * we just return and retry the instruction.
483 */
484
485 if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
486 return RESUME_GUEST;
487
488 /*
489 * Emulated accesses are emulated by looking at the hash for
490 * translation once, then performing the access later. The
491 * translation could be invalidated in the meantime in which
492 * point performing the subsequent memory access on the old
493 * physical address could possibly be a security hole for the
494 * guest (but not the host).
495 *
496 * This is less of an issue for MMIO stores since they aren't
497 * globally visible. It could be an issue for MMIO loads to
498 * a certain extent but we'll ignore it for now.
499 */
500
501 vcpu->arch.paddr_accessed = gpa;
502 return kvmppc_emulate_mmio(run, vcpu);
503}
504
505int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
506 unsigned long ea, unsigned long dsisr)
507{
508 struct kvm *kvm = vcpu->kvm;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000509 unsigned long *hptep, hpte[3], r;
510 unsigned long mmu_seq, psize, pte_size;
511 unsigned long gfn, hva, pfn;
Paul Mackerras697d3892011-12-12 12:36:37 +0000512 struct kvm_memory_slot *memslot;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000513 unsigned long *rmap;
Paul Mackerras697d3892011-12-12 12:36:37 +0000514 struct revmap_entry *rev;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000515 struct page *page, *pages[1];
516 long index, ret, npages;
517 unsigned long is_io;
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000518 unsigned int writing, write_ok;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000519 struct vm_area_struct *vma;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000520 unsigned long rcbits;
Paul Mackerras697d3892011-12-12 12:36:37 +0000521
522 /*
523 * Real-mode code has already searched the HPT and found the
524 * entry we're interested in. Lock the entry and check that
525 * it hasn't changed. If it has, just return and re-execute the
526 * instruction.
527 */
528 if (ea != vcpu->arch.pgfault_addr)
529 return RESUME_GUEST;
530 index = vcpu->arch.pgfault_index;
531 hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
532 rev = &kvm->arch.revmap[index];
533 preempt_disable();
534 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
535 cpu_relax();
536 hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
537 hpte[1] = hptep[1];
Paul Mackerras342d3db2011-12-12 12:38:05 +0000538 hpte[2] = r = rev->guest_rpte;
Paul Mackerras697d3892011-12-12 12:36:37 +0000539 asm volatile("lwsync" : : : "memory");
540 hptep[0] = hpte[0];
541 preempt_enable();
542
543 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
544 hpte[1] != vcpu->arch.pgfault_hpte[1])
545 return RESUME_GUEST;
546
547 /* Translate the logical address and get the page */
Paul Mackerras342d3db2011-12-12 12:38:05 +0000548 psize = hpte_page_size(hpte[0], r);
549 gfn = hpte_rpn(r, psize);
Paul Mackerras697d3892011-12-12 12:36:37 +0000550 memslot = gfn_to_memslot(kvm, gfn);
551
552 /* No memslot means it's an emulated MMIO region */
553 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
554 unsigned long gpa = (gfn << PAGE_SHIFT) | (ea & (psize - 1));
555 return kvmppc_hv_emulate_mmio(run, vcpu, gpa,
556 dsisr & DSISR_ISSTORE);
557 }
558
Paul Mackerras342d3db2011-12-12 12:38:05 +0000559 if (!kvm->arch.using_mmu_notifiers)
560 return -EFAULT; /* should never get here */
561
562 /* used to check for invalidations in progress */
563 mmu_seq = kvm->mmu_notifier_seq;
564 smp_rmb();
565
566 is_io = 0;
567 pfn = 0;
568 page = NULL;
569 pte_size = PAGE_SIZE;
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000570 writing = (dsisr & DSISR_ISSTORE) != 0;
571 /* If writing != 0, then the HPTE must allow writing, if we get here */
572 write_ok = writing;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000573 hva = gfn_to_hva_memslot(memslot, gfn);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000574 npages = get_user_pages_fast(hva, 1, writing, pages);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000575 if (npages < 1) {
576 /* Check if it's an I/O mapping */
577 down_read(&current->mm->mmap_sem);
578 vma = find_vma(current->mm, hva);
579 if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
580 (vma->vm_flags & VM_PFNMAP)) {
581 pfn = vma->vm_pgoff +
582 ((hva - vma->vm_start) >> PAGE_SHIFT);
583 pte_size = psize;
584 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000585 write_ok = vma->vm_flags & VM_WRITE;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000586 }
587 up_read(&current->mm->mmap_sem);
588 if (!pfn)
589 return -EFAULT;
590 } else {
591 page = pages[0];
592 if (PageHuge(page)) {
593 page = compound_head(page);
594 pte_size <<= compound_order(page);
595 }
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000596 /* if the guest wants write access, see if that is OK */
597 if (!writing && hpte_is_writable(r)) {
598 pte_t *ptep, pte;
599
600 /*
601 * We need to protect against page table destruction
602 * while looking up and updating the pte.
603 */
604 rcu_read_lock_sched();
605 ptep = find_linux_pte_or_hugepte(current->mm->pgd,
606 hva, NULL);
607 if (ptep && pte_present(*ptep)) {
608 pte = kvmppc_read_update_linux_pte(ptep, 1);
609 if (pte_write(pte))
610 write_ok = 1;
611 }
612 rcu_read_unlock_sched();
613 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000614 pfn = page_to_pfn(page);
615 }
616
617 ret = -EFAULT;
618 if (psize > pte_size)
619 goto out_put;
620
621 /* Check WIMG vs. the actual page we're accessing */
622 if (!hpte_cache_flags_ok(r, is_io)) {
623 if (is_io)
624 return -EFAULT;
625 /*
626 * Allow guest to map emulated device memory as
627 * uncacheable, but actually make it cacheable.
628 */
629 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
630 }
631
632 /* Set the HPTE to point to pfn */
633 r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000634 if (hpte_is_writable(r) && !write_ok)
635 r = hpte_make_readonly(r);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000636 ret = RESUME_GUEST;
637 preempt_disable();
638 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
639 cpu_relax();
640 if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
641 rev->guest_rpte != hpte[2])
642 /* HPTE has been changed under us; let the guest retry */
643 goto out_unlock;
644 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
645
646 rmap = &memslot->rmap[gfn - memslot->base_gfn];
647 lock_rmap(rmap);
648
649 /* Check if we might have been invalidated; let the guest retry if so */
650 ret = RESUME_GUEST;
651 if (mmu_notifier_retry(vcpu, mmu_seq)) {
652 unlock_rmap(rmap);
653 goto out_unlock;
654 }
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000655
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000656 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
657 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
658 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
659
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000660 if (hptep[0] & HPTE_V_VALID) {
661 /* HPTE was previously valid, so we need to invalidate it */
662 unlock_rmap(rmap);
663 hptep[0] |= HPTE_V_ABSENT;
664 kvmppc_invalidate_hpte(kvm, hptep, index);
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000665 /* don't lose previous R and C bits */
666 r |= hptep[1] & (HPTE_R_R | HPTE_R_C);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000667 } else {
668 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
669 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000670
671 hptep[1] = r;
672 eieio();
673 hptep[0] = hpte[0];
674 asm volatile("ptesync" : : : "memory");
675 preempt_enable();
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000676 if (page && hpte_is_writable(r))
Paul Mackerras342d3db2011-12-12 12:38:05 +0000677 SetPageDirty(page);
678
679 out_put:
David Gibsonde6c0b02012-05-08 20:24:08 +1000680 if (page) {
681 /*
682 * We drop pages[0] here, not page because page might
683 * have been set to the head page of a compound, but
684 * we have to drop the reference on the correct tail
685 * page to match the get inside gup()
686 */
687 put_page(pages[0]);
688 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000689 return ret;
690
691 out_unlock:
692 hptep[0] &= ~HPTE_V_HVLOCK;
693 preempt_enable();
694 goto out_put;
695}
696
697static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
698 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
699 unsigned long gfn))
700{
701 int ret;
702 int retval = 0;
703 struct kvm_memslots *slots;
704 struct kvm_memory_slot *memslot;
705
706 slots = kvm_memslots(kvm);
707 kvm_for_each_memslot(memslot, slots) {
708 unsigned long start = memslot->userspace_addr;
709 unsigned long end;
710
711 end = start + (memslot->npages << PAGE_SHIFT);
712 if (hva >= start && hva < end) {
713 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
714
715 ret = handler(kvm, &memslot->rmap[gfn_offset],
716 memslot->base_gfn + gfn_offset);
717 retval |= ret;
718 }
719 }
720
721 return retval;
722}
723
724static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
725 unsigned long gfn)
726{
727 struct revmap_entry *rev = kvm->arch.revmap;
728 unsigned long h, i, j;
729 unsigned long *hptep;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000730 unsigned long ptel, psize, rcbits;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000731
732 for (;;) {
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000733 lock_rmap(rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000734 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000735 unlock_rmap(rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000736 break;
737 }
738
739 /*
740 * To avoid an ABBA deadlock with the HPTE lock bit,
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000741 * we can't spin on the HPTE lock while holding the
742 * rmap chain lock.
Paul Mackerras342d3db2011-12-12 12:38:05 +0000743 */
744 i = *rmapp & KVMPPC_RMAP_INDEX;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000745 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
746 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
747 /* unlock rmap before spinning on the HPTE lock */
748 unlock_rmap(rmapp);
749 while (hptep[0] & HPTE_V_HVLOCK)
750 cpu_relax();
751 continue;
752 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000753 j = rev[i].forw;
754 if (j == i) {
755 /* chain is now empty */
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000756 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000757 } else {
758 /* remove i from chain */
759 h = rev[i].back;
760 rev[h].forw = j;
761 rev[j].back = h;
762 rev[i].forw = rev[i].back = i;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000763 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000764 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000765
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000766 /* Now check and modify the HPTE */
Paul Mackerras342d3db2011-12-12 12:38:05 +0000767 ptel = rev[i].guest_rpte;
768 psize = hpte_page_size(hptep[0], ptel);
769 if ((hptep[0] & HPTE_V_VALID) &&
770 hpte_rpn(ptel, psize) == gfn) {
Paul Mackerras342d3db2011-12-12 12:38:05 +0000771 hptep[0] |= HPTE_V_ABSENT;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000772 kvmppc_invalidate_hpte(kvm, hptep, i);
773 /* Harvest R and C */
774 rcbits = hptep[1] & (HPTE_R_R | HPTE_R_C);
775 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
776 rev[i].guest_rpte = ptel | rcbits;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000777 }
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000778 unlock_rmap(rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000779 hptep[0] &= ~HPTE_V_HVLOCK;
780 }
781 return 0;
782}
783
784int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
785{
786 if (kvm->arch.using_mmu_notifiers)
787 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
788 return 0;
789}
790
791static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
792 unsigned long gfn)
793{
Paul Mackerras55514892011-12-15 02:02:47 +0000794 struct revmap_entry *rev = kvm->arch.revmap;
795 unsigned long head, i, j;
796 unsigned long *hptep;
797 int ret = 0;
798
799 retry:
800 lock_rmap(rmapp);
801 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
802 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
803 ret = 1;
804 }
805 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
806 unlock_rmap(rmapp);
807 return ret;
808 }
809
810 i = head = *rmapp & KVMPPC_RMAP_INDEX;
811 do {
812 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
813 j = rev[i].forw;
814
815 /* If this HPTE isn't referenced, ignore it */
816 if (!(hptep[1] & HPTE_R_R))
817 continue;
818
819 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
820 /* unlock rmap before spinning on the HPTE lock */
821 unlock_rmap(rmapp);
822 while (hptep[0] & HPTE_V_HVLOCK)
823 cpu_relax();
824 goto retry;
825 }
826
827 /* Now check and modify the HPTE */
828 if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
829 kvmppc_clear_ref_hpte(kvm, hptep, i);
830 rev[i].guest_rpte |= HPTE_R_R;
831 ret = 1;
832 }
833 hptep[0] &= ~HPTE_V_HVLOCK;
834 } while ((i = j) != head);
835
836 unlock_rmap(rmapp);
837 return ret;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000838}
839
840int kvm_age_hva(struct kvm *kvm, unsigned long hva)
841{
842 if (!kvm->arch.using_mmu_notifiers)
843 return 0;
844 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
845}
846
847static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
848 unsigned long gfn)
849{
Paul Mackerras55514892011-12-15 02:02:47 +0000850 struct revmap_entry *rev = kvm->arch.revmap;
851 unsigned long head, i, j;
852 unsigned long *hp;
853 int ret = 1;
854
855 if (*rmapp & KVMPPC_RMAP_REFERENCED)
856 return 1;
857
858 lock_rmap(rmapp);
859 if (*rmapp & KVMPPC_RMAP_REFERENCED)
860 goto out;
861
862 if (*rmapp & KVMPPC_RMAP_PRESENT) {
863 i = head = *rmapp & KVMPPC_RMAP_INDEX;
864 do {
865 hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
866 j = rev[i].forw;
867 if (hp[1] & HPTE_R_R)
868 goto out;
869 } while ((i = j) != head);
870 }
871 ret = 0;
872
873 out:
874 unlock_rmap(rmapp);
875 return ret;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000876}
877
878int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
879{
880 if (!kvm->arch.using_mmu_notifiers)
881 return 0;
882 return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
883}
884
885void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
886{
887 if (!kvm->arch.using_mmu_notifiers)
888 return;
889 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000890}
891
Paul Mackerras82ed3612011-12-15 02:03:22 +0000892static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp)
893{
894 struct revmap_entry *rev = kvm->arch.revmap;
895 unsigned long head, i, j;
896 unsigned long *hptep;
897 int ret = 0;
898
899 retry:
900 lock_rmap(rmapp);
901 if (*rmapp & KVMPPC_RMAP_CHANGED) {
902 *rmapp &= ~KVMPPC_RMAP_CHANGED;
903 ret = 1;
904 }
905 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
906 unlock_rmap(rmapp);
907 return ret;
908 }
909
910 i = head = *rmapp & KVMPPC_RMAP_INDEX;
911 do {
912 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
913 j = rev[i].forw;
914
915 if (!(hptep[1] & HPTE_R_C))
916 continue;
917
918 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
919 /* unlock rmap before spinning on the HPTE lock */
920 unlock_rmap(rmapp);
921 while (hptep[0] & HPTE_V_HVLOCK)
922 cpu_relax();
923 goto retry;
924 }
925
926 /* Now check and modify the HPTE */
927 if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
928 /* need to make it temporarily absent to clear C */
929 hptep[0] |= HPTE_V_ABSENT;
930 kvmppc_invalidate_hpte(kvm, hptep, i);
931 hptep[1] &= ~HPTE_R_C;
932 eieio();
933 hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
934 rev[i].guest_rpte |= HPTE_R_C;
935 ret = 1;
936 }
937 hptep[0] &= ~HPTE_V_HVLOCK;
938 } while ((i = j) != head);
939
940 unlock_rmap(rmapp);
941 return ret;
942}
943
944long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
945{
946 unsigned long i;
947 unsigned long *rmapp, *map;
948
949 preempt_disable();
950 rmapp = memslot->rmap;
951 map = memslot->dirty_bitmap;
952 for (i = 0; i < memslot->npages; ++i) {
953 if (kvm_test_clear_dirty(kvm, rmapp))
954 __set_bit_le(i, map);
955 ++rmapp;
956 }
957 preempt_enable();
958 return 0;
959}
960
Paul Mackerras93e60242011-12-12 12:28:55 +0000961void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
962 unsigned long *nb_ret)
963{
964 struct kvm_memory_slot *memslot;
965 unsigned long gfn = gpa >> PAGE_SHIFT;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000966 struct page *page, *pages[1];
967 int npages;
968 unsigned long hva, psize, offset;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000969 unsigned long pa;
Paul Mackerras93e60242011-12-12 12:28:55 +0000970 unsigned long *physp;
971
972 memslot = gfn_to_memslot(kvm, gfn);
973 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
974 return NULL;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000975 if (!kvm->arch.using_mmu_notifiers) {
976 physp = kvm->arch.slot_phys[memslot->id];
977 if (!physp)
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000978 return NULL;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000979 physp += gfn - memslot->base_gfn;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000980 pa = *physp;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000981 if (!pa) {
982 if (kvmppc_get_guest_page(kvm, gfn, memslot,
983 PAGE_SIZE) < 0)
984 return NULL;
985 pa = *physp;
986 }
987 page = pfn_to_page(pa >> PAGE_SHIFT);
David Gibsonde6c0b02012-05-08 20:24:08 +1000988 get_page(page);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000989 } else {
990 hva = gfn_to_hva_memslot(memslot, gfn);
991 npages = get_user_pages_fast(hva, 1, 1, pages);
992 if (npages < 1)
993 return NULL;
994 page = pages[0];
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000995 }
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000996 psize = PAGE_SIZE;
997 if (PageHuge(page)) {
998 page = compound_head(page);
999 psize <<= compound_order(page);
1000 }
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001001 offset = gpa & (psize - 1);
Paul Mackerras93e60242011-12-12 12:28:55 +00001002 if (nb_ret)
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001003 *nb_ret = psize - offset;
Paul Mackerras93e60242011-12-12 12:28:55 +00001004 return page_address(page) + offset;
1005}
1006
1007void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
1008{
1009 struct page *page = virt_to_page(va);
1010
Paul Mackerras93e60242011-12-12 12:28:55 +00001011 put_page(page);
1012}
1013
Paul Mackerrasde56a942011-06-29 00:21:34 +00001014void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
1015{
1016 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
1017
Paul Mackerras9e368f22011-06-29 00:40:08 +00001018 if (cpu_has_feature(CPU_FTR_ARCH_206))
1019 vcpu->arch.slb_nr = 32; /* POWER7 */
1020 else
1021 vcpu->arch.slb_nr = 64;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001022
1023 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
1024 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
1025
1026 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
1027}