blob: a389cc62b16c7103541d3936e1893af7a1472ab4 [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 Mackerras2c9097e2012-09-11 13:27:01 +000027#include <linux/srcu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000028
29#include <asm/tlbflush.h>
30#include <asm/kvm_ppc.h>
31#include <asm/kvm_book3s.h>
32#include <asm/mmu-hash64.h>
33#include <asm/hvcall.h>
34#include <asm/synch.h>
35#include <asm/ppc-opcode.h>
36#include <asm/cputable.h>
37
Paul Mackerras9e368f22011-06-29 00:40:08 +000038/* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
39#define MAX_LPID_970 63
Paul Mackerrasde56a942011-06-29 00:21:34 +000040
Paul Mackerras32fad282012-05-04 02:32:53 +000041/* Power architecture requires HPT is at least 256kB */
42#define PPC_MIN_HPT_ORDER 18
43
44long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp)
Paul Mackerrasde56a942011-06-29 00:21:34 +000045{
46 unsigned long hpt;
Paul Mackerras8936dda2011-12-12 12:27:39 +000047 struct revmap_entry *rev;
Alexander Grafd2a1b482012-01-16 19:12:11 +010048 struct kvmppc_linear_info *li;
Paul Mackerras32fad282012-05-04 02:32:53 +000049 long order = kvm_hpt_order;
Paul Mackerrasde56a942011-06-29 00:21:34 +000050
Paul Mackerras32fad282012-05-04 02:32:53 +000051 if (htab_orderp) {
52 order = *htab_orderp;
53 if (order < PPC_MIN_HPT_ORDER)
54 order = PPC_MIN_HPT_ORDER;
55 }
56
57 /*
58 * If the user wants a different size from default,
59 * try first to allocate it from the kernel page allocator.
60 */
61 hpt = 0;
62 if (order != kvm_hpt_order) {
Alexander Grafd2a1b482012-01-16 19:12:11 +010063 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
Paul Mackerras32fad282012-05-04 02:32:53 +000064 __GFP_NOWARN, order - PAGE_SHIFT);
65 if (!hpt)
66 --order;
Alexander Grafd2a1b482012-01-16 19:12:11 +010067 }
68
Paul Mackerras32fad282012-05-04 02:32:53 +000069 /* Next try to allocate from the preallocated pool */
Paul Mackerrasde56a942011-06-29 00:21:34 +000070 if (!hpt) {
Paul Mackerras32fad282012-05-04 02:32:53 +000071 li = kvm_alloc_hpt();
72 if (li) {
73 hpt = (ulong)li->base_virt;
74 kvm->arch.hpt_li = li;
75 order = kvm_hpt_order;
76 }
Paul Mackerrasde56a942011-06-29 00:21:34 +000077 }
Paul Mackerras32fad282012-05-04 02:32:53 +000078
79 /* Lastly try successively smaller sizes from the page allocator */
80 while (!hpt && order > PPC_MIN_HPT_ORDER) {
81 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
82 __GFP_NOWARN, order - PAGE_SHIFT);
83 if (!hpt)
84 --order;
85 }
86
87 if (!hpt)
88 return -ENOMEM;
89
Paul Mackerrasde56a942011-06-29 00:21:34 +000090 kvm->arch.hpt_virt = hpt;
Paul Mackerras32fad282012-05-04 02:32:53 +000091 kvm->arch.hpt_order = order;
92 /* HPTEs are 2**4 bytes long */
93 kvm->arch.hpt_npte = 1ul << (order - 4);
94 /* 128 (2**7) bytes in each HPTEG */
95 kvm->arch.hpt_mask = (1ul << (order - 7)) - 1;
Paul Mackerrasde56a942011-06-29 00:21:34 +000096
Paul Mackerras8936dda2011-12-12 12:27:39 +000097 /* Allocate reverse map array */
Paul Mackerras32fad282012-05-04 02:32:53 +000098 rev = vmalloc(sizeof(struct revmap_entry) * kvm->arch.hpt_npte);
Paul Mackerras8936dda2011-12-12 12:27:39 +000099 if (!rev) {
100 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
101 goto out_freehpt;
102 }
103 kvm->arch.revmap = rev;
Paul Mackerras32fad282012-05-04 02:32:53 +0000104 kvm->arch.sdr1 = __pa(hpt) | (order - 18);
Paul Mackerras8936dda2011-12-12 12:27:39 +0000105
Paul Mackerras32fad282012-05-04 02:32:53 +0000106 pr_info("KVM guest htab at %lx (order %ld), LPID %x\n",
107 hpt, order, kvm->arch.lpid);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000108
Paul Mackerras32fad282012-05-04 02:32:53 +0000109 if (htab_orderp)
110 *htab_orderp = order;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000111 return 0;
Paul Mackerras8936dda2011-12-12 12:27:39 +0000112
Paul Mackerras8936dda2011-12-12 12:27:39 +0000113 out_freehpt:
Paul Mackerras32fad282012-05-04 02:32:53 +0000114 if (kvm->arch.hpt_li)
115 kvm_release_hpt(kvm->arch.hpt_li);
116 else
117 free_pages(hpt, order - PAGE_SHIFT);
Paul Mackerras8936dda2011-12-12 12:27:39 +0000118 return -ENOMEM;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000119}
120
Paul Mackerras32fad282012-05-04 02:32:53 +0000121long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp)
122{
123 long err = -EBUSY;
124 long order;
125
126 mutex_lock(&kvm->lock);
127 if (kvm->arch.rma_setup_done) {
128 kvm->arch.rma_setup_done = 0;
129 /* order rma_setup_done vs. vcpus_running */
130 smp_mb();
131 if (atomic_read(&kvm->arch.vcpus_running)) {
132 kvm->arch.rma_setup_done = 1;
133 goto out;
134 }
135 }
136 if (kvm->arch.hpt_virt) {
137 order = kvm->arch.hpt_order;
138 /* Set the entire HPT to 0, i.e. invalid HPTEs */
139 memset((void *)kvm->arch.hpt_virt, 0, 1ul << order);
140 /*
141 * Set the whole last_vcpu array to an invalid vcpu number.
142 * This ensures that each vcpu will flush its TLB on next entry.
143 */
144 memset(kvm->arch.last_vcpu, 0xff, sizeof(kvm->arch.last_vcpu));
145 *htab_orderp = order;
146 err = 0;
147 } else {
148 err = kvmppc_alloc_hpt(kvm, htab_orderp);
149 order = *htab_orderp;
150 }
151 out:
152 mutex_unlock(&kvm->lock);
153 return err;
154}
155
Paul Mackerrasde56a942011-06-29 00:21:34 +0000156void kvmppc_free_hpt(struct kvm *kvm)
157{
Scott Wood043cc4d2011-12-20 15:34:20 +0000158 kvmppc_free_lpid(kvm->arch.lpid);
Paul Mackerras8936dda2011-12-12 12:27:39 +0000159 vfree(kvm->arch.revmap);
Alexander Grafd2a1b482012-01-16 19:12:11 +0100160 if (kvm->arch.hpt_li)
161 kvm_release_hpt(kvm->arch.hpt_li);
162 else
Paul Mackerras32fad282012-05-04 02:32:53 +0000163 free_pages(kvm->arch.hpt_virt,
164 kvm->arch.hpt_order - PAGE_SHIFT);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000165}
166
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000167/* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
168static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000169{
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000170 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
171}
172
173/* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
174static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
175{
176 return (pgsize == 0x10000) ? 0x1000 : 0;
177}
178
179void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
180 unsigned long porder)
181{
Paul Mackerrasde56a942011-06-29 00:21:34 +0000182 unsigned long i;
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000183 unsigned long npages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000184 unsigned long hp_v, hp_r;
185 unsigned long addr, hash;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000186 unsigned long psize;
187 unsigned long hp0, hp1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000188 long ret;
Paul Mackerras32fad282012-05-04 02:32:53 +0000189 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000190
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000191 psize = 1ul << porder;
192 npages = memslot->npages >> (porder - PAGE_SHIFT);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000193
194 /* VRMA can't be > 1TB */
Paul Mackerras8936dda2011-12-12 12:27:39 +0000195 if (npages > 1ul << (40 - porder))
196 npages = 1ul << (40 - porder);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000197 /* Can't use more than 1 HPTE per HPTEG */
Paul Mackerras32fad282012-05-04 02:32:53 +0000198 if (npages > kvm->arch.hpt_mask + 1)
199 npages = kvm->arch.hpt_mask + 1;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000200
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000201 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
202 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
203 hp1 = hpte1_pgsize_encoding(psize) |
204 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
205
Paul Mackerrasde56a942011-06-29 00:21:34 +0000206 for (i = 0; i < npages; ++i) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000207 addr = i << porder;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000208 /* can't use hpt_hash since va > 64 bits */
Paul Mackerras32fad282012-05-04 02:32:53 +0000209 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & kvm->arch.hpt_mask;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000210 /*
211 * We assume that the hash table is empty and no
212 * vcpus are using it at this stage. Since we create
213 * at most one HPTE per HPTEG, we just assume entry 7
214 * is available and use it.
215 */
Paul Mackerras8936dda2011-12-12 12:27:39 +0000216 hash = (hash << 3) + 7;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000217 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
218 hp_r = hp1 | addr;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000219 ret = kvmppc_virtmode_h_enter(vcpu, H_EXACT, hash, hp_v, hp_r);
220 if (ret != H_SUCCESS) {
221 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
222 addr, ret);
223 break;
224 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000225 }
226}
227
228int kvmppc_mmu_hv_init(void)
229{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000230 unsigned long host_lpid, rsvd_lpid;
231
232 if (!cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000233 return -EINVAL;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000234
Scott Wood043cc4d2011-12-20 15:34:20 +0000235 /* POWER7 has 10-bit LPIDs, PPC970 and e500mc have 6-bit LPIDs */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000236 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
237 host_lpid = mfspr(SPRN_LPID); /* POWER7 */
238 rsvd_lpid = LPID_RSVD;
239 } else {
240 host_lpid = 0; /* PPC970 */
241 rsvd_lpid = MAX_LPID_970;
242 }
243
Scott Wood043cc4d2011-12-20 15:34:20 +0000244 kvmppc_init_lpid(rsvd_lpid + 1);
245
246 kvmppc_claim_lpid(host_lpid);
Paul Mackerras9e368f22011-06-29 00:40:08 +0000247 /* rsvd_lpid is reserved for use in partition switching */
Scott Wood043cc4d2011-12-20 15:34:20 +0000248 kvmppc_claim_lpid(rsvd_lpid);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000249
250 return 0;
251}
252
253void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
254{
255}
256
257static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
258{
259 kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
260}
261
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000262/*
263 * This is called to get a reference to a guest page if there isn't
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000264 * one already in the memslot->arch.slot_phys[] array.
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000265 */
266static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000267 struct kvm_memory_slot *memslot,
268 unsigned long psize)
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000269{
270 unsigned long start;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000271 long np, err;
272 struct page *page, *hpage, *pages[1];
273 unsigned long s, pgsize;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000274 unsigned long *physp;
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000275 unsigned int is_io, got, pgorder;
276 struct vm_area_struct *vma;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000277 unsigned long pfn, i, npages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000278
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000279 physp = memslot->arch.slot_phys;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000280 if (!physp)
281 return -EINVAL;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000282 if (physp[gfn - memslot->base_gfn])
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000283 return 0;
284
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000285 is_io = 0;
286 got = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000287 page = NULL;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000288 pgsize = psize;
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000289 err = -EINVAL;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000290 start = gfn_to_hva_memslot(memslot, gfn);
291
292 /* Instantiate and get the page we want access to */
293 np = get_user_pages_fast(start, 1, 1, pages);
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000294 if (np != 1) {
295 /* Look up the vma for the page */
296 down_read(&current->mm->mmap_sem);
297 vma = find_vma(current->mm, start);
298 if (!vma || vma->vm_start > start ||
299 start + psize > vma->vm_end ||
300 !(vma->vm_flags & VM_PFNMAP))
301 goto up_err;
302 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
303 pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
304 /* check alignment of pfn vs. requested page size */
305 if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1)))
306 goto up_err;
307 up_read(&current->mm->mmap_sem);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000308
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000309 } else {
310 page = pages[0];
311 got = KVMPPC_GOT_PAGE;
312
313 /* See if this is a large page */
314 s = PAGE_SIZE;
315 if (PageHuge(page)) {
316 hpage = compound_head(page);
317 s <<= compound_order(hpage);
318 /* Get the whole large page if slot alignment is ok */
319 if (s > psize && slot_is_aligned(memslot, s) &&
320 !(memslot->userspace_addr & (s - 1))) {
321 start &= ~(s - 1);
322 pgsize = s;
David Gibsonde6c0b02012-05-08 20:24:08 +1000323 get_page(hpage);
324 put_page(page);
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000325 page = hpage;
326 }
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000327 }
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000328 if (s < psize)
329 goto out;
330 pfn = page_to_pfn(page);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000331 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000332
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000333 npages = pgsize >> PAGE_SHIFT;
334 pgorder = __ilog2(npages);
335 physp += (gfn - memslot->base_gfn) & ~(npages - 1);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000336 spin_lock(&kvm->arch.slot_phys_lock);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000337 for (i = 0; i < npages; ++i) {
338 if (!physp[i]) {
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000339 physp[i] = ((pfn + i) << PAGE_SHIFT) +
340 got + is_io + pgorder;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000341 got = 0;
342 }
343 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000344 spin_unlock(&kvm->arch.slot_phys_lock);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000345 err = 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000346
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000347 out:
David Gibsonde6c0b02012-05-08 20:24:08 +1000348 if (got)
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000349 put_page(page);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000350 return err;
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000351
352 up_err:
353 up_read(&current->mm->mmap_sem);
354 return err;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000355}
356
357/*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000358 * We come here on a H_ENTER call from the guest when we are not
359 * using mmu notifiers and we don't have the requested page pinned
360 * already.
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000361 */
362long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
363 long pte_index, unsigned long pteh, unsigned long ptel)
364{
365 struct kvm *kvm = vcpu->kvm;
366 unsigned long psize, gpa, gfn;
367 struct kvm_memory_slot *memslot;
368 long ret;
369
Paul Mackerras342d3db2011-12-12 12:38:05 +0000370 if (kvm->arch.using_mmu_notifiers)
371 goto do_insert;
372
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000373 psize = hpte_page_size(pteh, ptel);
374 if (!psize)
375 return H_PARAMETER;
376
Paul Mackerras697d3892011-12-12 12:36:37 +0000377 pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
378
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000379 /* Find the memslot (if any) for this address */
380 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
381 gfn = gpa >> PAGE_SHIFT;
382 memslot = gfn_to_memslot(kvm, gfn);
Paul Mackerras697d3892011-12-12 12:36:37 +0000383 if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
384 if (!slot_is_aligned(memslot, psize))
385 return H_PARAMETER;
386 if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
387 return H_PARAMETER;
388 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000389
Paul Mackerras342d3db2011-12-12 12:38:05 +0000390 do_insert:
391 /* Protect linux PTE lookup from page table destruction */
392 rcu_read_lock_sched(); /* this disables preemption too */
393 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000394 ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000395 rcu_read_unlock_sched();
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000396 if (ret == H_TOO_HARD) {
397 /* this can't happen */
398 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
399 ret = H_RESOURCE; /* or something */
400 }
401 return ret;
402
403}
404
Paul Mackerras697d3892011-12-12 12:36:37 +0000405static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
406 gva_t eaddr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000407{
Paul Mackerras697d3892011-12-12 12:36:37 +0000408 u64 mask;
409 int i;
410
411 for (i = 0; i < vcpu->arch.slb_nr; i++) {
412 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
413 continue;
414
415 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
416 mask = ESID_MASK_1T;
417 else
418 mask = ESID_MASK;
419
420 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
421 return &vcpu->arch.slb[i];
422 }
423 return NULL;
424}
425
426static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
427 unsigned long ea)
428{
429 unsigned long ra_mask;
430
431 ra_mask = hpte_page_size(v, r) - 1;
432 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
433}
434
435static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
436 struct kvmppc_pte *gpte, bool data)
437{
438 struct kvm *kvm = vcpu->kvm;
439 struct kvmppc_slb *slbe;
440 unsigned long slb_v;
441 unsigned long pp, key;
442 unsigned long v, gr;
443 unsigned long *hptep;
444 int index;
445 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
446
447 /* Get SLB entry */
448 if (virtmode) {
449 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
450 if (!slbe)
451 return -EINVAL;
452 slb_v = slbe->origv;
453 } else {
454 /* real mode access */
455 slb_v = vcpu->kvm->arch.vrma_slb_v;
456 }
457
458 /* Find the HPTE in the hash table */
459 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
460 HPTE_V_VALID | HPTE_V_ABSENT);
461 if (index < 0)
462 return -ENOENT;
463 hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
464 v = hptep[0] & ~HPTE_V_HVLOCK;
465 gr = kvm->arch.revmap[index].guest_rpte;
466
467 /* Unlock the HPTE */
468 asm volatile("lwsync" : : : "memory");
469 hptep[0] = v;
470
471 gpte->eaddr = eaddr;
472 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
473
474 /* Get PP bits and key for permission check */
475 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
476 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
477 key &= slb_v;
478
479 /* Calculate permissions */
480 gpte->may_read = hpte_read_permission(pp, key);
481 gpte->may_write = hpte_write_permission(pp, key);
482 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
483
484 /* Storage key permission check for POWER7 */
485 if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
486 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
487 if (amrfield & 1)
488 gpte->may_read = 0;
489 if (amrfield & 2)
490 gpte->may_write = 0;
491 }
492
493 /* Get the guest physical address */
494 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
495 return 0;
496}
497
498/*
499 * Quick test for whether an instruction is a load or a store.
500 * If the instruction is a load or a store, then this will indicate
501 * which it is, at least on server processors. (Embedded processors
502 * have some external PID instructions that don't follow the rule
503 * embodied here.) If the instruction isn't a load or store, then
504 * this doesn't return anything useful.
505 */
506static int instruction_is_store(unsigned int instr)
507{
508 unsigned int mask;
509
510 mask = 0x10000000;
511 if ((instr & 0xfc000000) == 0x7c000000)
512 mask = 0x100; /* major opcode 31 */
513 return (instr & mask) != 0;
514}
515
516static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Graf6020c0f2012-03-12 02:26:30 +0100517 unsigned long gpa, gva_t ea, int is_store)
Paul Mackerras697d3892011-12-12 12:36:37 +0000518{
519 int ret;
520 u32 last_inst;
521 unsigned long srr0 = kvmppc_get_pc(vcpu);
522
523 /* We try to load the last instruction. We don't let
524 * emulate_instruction do it as it doesn't check what
525 * kvmppc_ld returns.
526 * If we fail, we just return to the guest and try executing it again.
527 */
528 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) {
529 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
530 if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED)
531 return RESUME_GUEST;
532 vcpu->arch.last_inst = last_inst;
533 }
534
535 /*
536 * WARNING: We do not know for sure whether the instruction we just
537 * read from memory is the same that caused the fault in the first
538 * place. If the instruction we read is neither an load or a store,
539 * then it can't access memory, so we don't need to worry about
540 * enforcing access permissions. So, assuming it is a load or
541 * store, we just check that its direction (load or store) is
542 * consistent with the original fault, since that's what we
543 * checked the access permissions against. If there is a mismatch
544 * we just return and retry the instruction.
545 */
546
547 if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
548 return RESUME_GUEST;
549
550 /*
551 * Emulated accesses are emulated by looking at the hash for
552 * translation once, then performing the access later. The
553 * translation could be invalidated in the meantime in which
554 * point performing the subsequent memory access on the old
555 * physical address could possibly be a security hole for the
556 * guest (but not the host).
557 *
558 * This is less of an issue for MMIO stores since they aren't
559 * globally visible. It could be an issue for MMIO loads to
560 * a certain extent but we'll ignore it for now.
561 */
562
563 vcpu->arch.paddr_accessed = gpa;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100564 vcpu->arch.vaddr_accessed = ea;
Paul Mackerras697d3892011-12-12 12:36:37 +0000565 return kvmppc_emulate_mmio(run, vcpu);
566}
567
568int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
569 unsigned long ea, unsigned long dsisr)
570{
571 struct kvm *kvm = vcpu->kvm;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000572 unsigned long *hptep, hpte[3], r;
573 unsigned long mmu_seq, psize, pte_size;
574 unsigned long gfn, hva, pfn;
Paul Mackerras697d3892011-12-12 12:36:37 +0000575 struct kvm_memory_slot *memslot;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000576 unsigned long *rmap;
Paul Mackerras697d3892011-12-12 12:36:37 +0000577 struct revmap_entry *rev;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000578 struct page *page, *pages[1];
579 long index, ret, npages;
580 unsigned long is_io;
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000581 unsigned int writing, write_ok;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000582 struct vm_area_struct *vma;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000583 unsigned long rcbits;
Paul Mackerras697d3892011-12-12 12:36:37 +0000584
585 /*
586 * Real-mode code has already searched the HPT and found the
587 * entry we're interested in. Lock the entry and check that
588 * it hasn't changed. If it has, just return and re-execute the
589 * instruction.
590 */
591 if (ea != vcpu->arch.pgfault_addr)
592 return RESUME_GUEST;
593 index = vcpu->arch.pgfault_index;
594 hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
595 rev = &kvm->arch.revmap[index];
596 preempt_disable();
597 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
598 cpu_relax();
599 hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
600 hpte[1] = hptep[1];
Paul Mackerras342d3db2011-12-12 12:38:05 +0000601 hpte[2] = r = rev->guest_rpte;
Paul Mackerras697d3892011-12-12 12:36:37 +0000602 asm volatile("lwsync" : : : "memory");
603 hptep[0] = hpte[0];
604 preempt_enable();
605
606 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
607 hpte[1] != vcpu->arch.pgfault_hpte[1])
608 return RESUME_GUEST;
609
610 /* Translate the logical address and get the page */
Paul Mackerras342d3db2011-12-12 12:38:05 +0000611 psize = hpte_page_size(hpte[0], r);
612 gfn = hpte_rpn(r, psize);
Paul Mackerras697d3892011-12-12 12:36:37 +0000613 memslot = gfn_to_memslot(kvm, gfn);
614
615 /* No memslot means it's an emulated MMIO region */
616 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
617 unsigned long gpa = (gfn << PAGE_SHIFT) | (ea & (psize - 1));
Alexander Graf6020c0f2012-03-12 02:26:30 +0100618 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
Paul Mackerras697d3892011-12-12 12:36:37 +0000619 dsisr & DSISR_ISSTORE);
620 }
621
Paul Mackerras342d3db2011-12-12 12:38:05 +0000622 if (!kvm->arch.using_mmu_notifiers)
623 return -EFAULT; /* should never get here */
624
625 /* used to check for invalidations in progress */
626 mmu_seq = kvm->mmu_notifier_seq;
627 smp_rmb();
628
629 is_io = 0;
630 pfn = 0;
631 page = NULL;
632 pte_size = PAGE_SIZE;
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000633 writing = (dsisr & DSISR_ISSTORE) != 0;
634 /* If writing != 0, then the HPTE must allow writing, if we get here */
635 write_ok = writing;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000636 hva = gfn_to_hva_memslot(memslot, gfn);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000637 npages = get_user_pages_fast(hva, 1, writing, pages);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000638 if (npages < 1) {
639 /* Check if it's an I/O mapping */
640 down_read(&current->mm->mmap_sem);
641 vma = find_vma(current->mm, hva);
642 if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
643 (vma->vm_flags & VM_PFNMAP)) {
644 pfn = vma->vm_pgoff +
645 ((hva - vma->vm_start) >> PAGE_SHIFT);
646 pte_size = psize;
647 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000648 write_ok = vma->vm_flags & VM_WRITE;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000649 }
650 up_read(&current->mm->mmap_sem);
651 if (!pfn)
652 return -EFAULT;
653 } else {
654 page = pages[0];
655 if (PageHuge(page)) {
656 page = compound_head(page);
657 pte_size <<= compound_order(page);
658 }
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000659 /* if the guest wants write access, see if that is OK */
660 if (!writing && hpte_is_writable(r)) {
661 pte_t *ptep, pte;
662
663 /*
664 * We need to protect against page table destruction
665 * while looking up and updating the pte.
666 */
667 rcu_read_lock_sched();
668 ptep = find_linux_pte_or_hugepte(current->mm->pgd,
669 hva, NULL);
670 if (ptep && pte_present(*ptep)) {
671 pte = kvmppc_read_update_linux_pte(ptep, 1);
672 if (pte_write(pte))
673 write_ok = 1;
674 }
675 rcu_read_unlock_sched();
676 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000677 pfn = page_to_pfn(page);
678 }
679
680 ret = -EFAULT;
681 if (psize > pte_size)
682 goto out_put;
683
684 /* Check WIMG vs. the actual page we're accessing */
685 if (!hpte_cache_flags_ok(r, is_io)) {
686 if (is_io)
687 return -EFAULT;
688 /*
689 * Allow guest to map emulated device memory as
690 * uncacheable, but actually make it cacheable.
691 */
692 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
693 }
694
695 /* Set the HPTE to point to pfn */
696 r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000697 if (hpte_is_writable(r) && !write_ok)
698 r = hpte_make_readonly(r);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000699 ret = RESUME_GUEST;
700 preempt_disable();
701 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
702 cpu_relax();
703 if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
704 rev->guest_rpte != hpte[2])
705 /* HPTE has been changed under us; let the guest retry */
706 goto out_unlock;
707 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
708
Takuya Yoshikawad89cc612012-08-01 18:03:28 +0900709 rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
Paul Mackerras342d3db2011-12-12 12:38:05 +0000710 lock_rmap(rmap);
711
712 /* Check if we might have been invalidated; let the guest retry if so */
713 ret = RESUME_GUEST;
714 if (mmu_notifier_retry(vcpu, mmu_seq)) {
715 unlock_rmap(rmap);
716 goto out_unlock;
717 }
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000718
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000719 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
720 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
721 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
722
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000723 if (hptep[0] & HPTE_V_VALID) {
724 /* HPTE was previously valid, so we need to invalidate it */
725 unlock_rmap(rmap);
726 hptep[0] |= HPTE_V_ABSENT;
727 kvmppc_invalidate_hpte(kvm, hptep, index);
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000728 /* don't lose previous R and C bits */
729 r |= hptep[1] & (HPTE_R_R | HPTE_R_C);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000730 } else {
731 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
732 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000733
734 hptep[1] = r;
735 eieio();
736 hptep[0] = hpte[0];
737 asm volatile("ptesync" : : : "memory");
738 preempt_enable();
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000739 if (page && hpte_is_writable(r))
Paul Mackerras342d3db2011-12-12 12:38:05 +0000740 SetPageDirty(page);
741
742 out_put:
David Gibsonde6c0b02012-05-08 20:24:08 +1000743 if (page) {
744 /*
745 * We drop pages[0] here, not page because page might
746 * have been set to the head page of a compound, but
747 * we have to drop the reference on the correct tail
748 * page to match the get inside gup()
749 */
750 put_page(pages[0]);
751 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000752 return ret;
753
754 out_unlock:
755 hptep[0] &= ~HPTE_V_HVLOCK;
756 preempt_enable();
757 goto out_put;
758}
759
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +0900760static int kvm_handle_hva_range(struct kvm *kvm,
761 unsigned long start,
762 unsigned long end,
763 int (*handler)(struct kvm *kvm,
764 unsigned long *rmapp,
765 unsigned long gfn))
Paul Mackerras342d3db2011-12-12 12:38:05 +0000766{
767 int ret;
768 int retval = 0;
769 struct kvm_memslots *slots;
770 struct kvm_memory_slot *memslot;
771
772 slots = kvm_memslots(kvm);
773 kvm_for_each_memslot(memslot, slots) {
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +0900774 unsigned long hva_start, hva_end;
775 gfn_t gfn, gfn_end;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000776
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +0900777 hva_start = max(start, memslot->userspace_addr);
778 hva_end = min(end, memslot->userspace_addr +
779 (memslot->npages << PAGE_SHIFT));
780 if (hva_start >= hva_end)
781 continue;
782 /*
783 * {gfn(page) | page intersects with [hva_start, hva_end)} =
784 * {gfn, gfn+1, ..., gfn_end-1}.
785 */
786 gfn = hva_to_gfn_memslot(hva_start, memslot);
787 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
788
789 for (; gfn < gfn_end; ++gfn) {
Takuya Yoshikawad19a7482012-07-02 17:54:30 +0900790 gfn_t gfn_offset = gfn - memslot->base_gfn;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000791
Takuya Yoshikawad89cc612012-08-01 18:03:28 +0900792 ret = handler(kvm, &memslot->arch.rmap[gfn_offset], gfn);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000793 retval |= ret;
794 }
795 }
796
797 return retval;
798}
799
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +0900800static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
801 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
802 unsigned long gfn))
803{
804 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
805}
806
Paul Mackerras342d3db2011-12-12 12:38:05 +0000807static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
808 unsigned long gfn)
809{
810 struct revmap_entry *rev = kvm->arch.revmap;
811 unsigned long h, i, j;
812 unsigned long *hptep;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000813 unsigned long ptel, psize, rcbits;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000814
815 for (;;) {
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000816 lock_rmap(rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000817 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000818 unlock_rmap(rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000819 break;
820 }
821
822 /*
823 * To avoid an ABBA deadlock with the HPTE lock bit,
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000824 * we can't spin on the HPTE lock while holding the
825 * rmap chain lock.
Paul Mackerras342d3db2011-12-12 12:38:05 +0000826 */
827 i = *rmapp & KVMPPC_RMAP_INDEX;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000828 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
829 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
830 /* unlock rmap before spinning on the HPTE lock */
831 unlock_rmap(rmapp);
832 while (hptep[0] & HPTE_V_HVLOCK)
833 cpu_relax();
834 continue;
835 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000836 j = rev[i].forw;
837 if (j == i) {
838 /* chain is now empty */
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000839 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000840 } else {
841 /* remove i from chain */
842 h = rev[i].back;
843 rev[h].forw = j;
844 rev[j].back = h;
845 rev[i].forw = rev[i].back = i;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000846 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000847 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000848
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000849 /* Now check and modify the HPTE */
Paul Mackerras342d3db2011-12-12 12:38:05 +0000850 ptel = rev[i].guest_rpte;
851 psize = hpte_page_size(hptep[0], ptel);
852 if ((hptep[0] & HPTE_V_VALID) &&
853 hpte_rpn(ptel, psize) == gfn) {
Paul Mackerras342d3db2011-12-12 12:38:05 +0000854 hptep[0] |= HPTE_V_ABSENT;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000855 kvmppc_invalidate_hpte(kvm, hptep, i);
856 /* Harvest R and C */
857 rcbits = hptep[1] & (HPTE_R_R | HPTE_R_C);
858 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
859 rev[i].guest_rpte = ptel | rcbits;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000860 }
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000861 unlock_rmap(rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000862 hptep[0] &= ~HPTE_V_HVLOCK;
863 }
864 return 0;
865}
866
867int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
868{
869 if (kvm->arch.using_mmu_notifiers)
870 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
871 return 0;
872}
873
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +0900874int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
875{
876 if (kvm->arch.using_mmu_notifiers)
877 kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
878 return 0;
879}
880
Paul Mackerras342d3db2011-12-12 12:38:05 +0000881static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
882 unsigned long gfn)
883{
Paul Mackerras55514892011-12-15 02:02:47 +0000884 struct revmap_entry *rev = kvm->arch.revmap;
885 unsigned long head, i, j;
886 unsigned long *hptep;
887 int ret = 0;
888
889 retry:
890 lock_rmap(rmapp);
891 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
892 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
893 ret = 1;
894 }
895 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
896 unlock_rmap(rmapp);
897 return ret;
898 }
899
900 i = head = *rmapp & KVMPPC_RMAP_INDEX;
901 do {
902 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
903 j = rev[i].forw;
904
905 /* If this HPTE isn't referenced, ignore it */
906 if (!(hptep[1] & HPTE_R_R))
907 continue;
908
909 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
910 /* unlock rmap before spinning on the HPTE lock */
911 unlock_rmap(rmapp);
912 while (hptep[0] & HPTE_V_HVLOCK)
913 cpu_relax();
914 goto retry;
915 }
916
917 /* Now check and modify the HPTE */
918 if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
919 kvmppc_clear_ref_hpte(kvm, hptep, i);
920 rev[i].guest_rpte |= HPTE_R_R;
921 ret = 1;
922 }
923 hptep[0] &= ~HPTE_V_HVLOCK;
924 } while ((i = j) != head);
925
926 unlock_rmap(rmapp);
927 return ret;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000928}
929
930int kvm_age_hva(struct kvm *kvm, unsigned long hva)
931{
932 if (!kvm->arch.using_mmu_notifiers)
933 return 0;
934 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
935}
936
937static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
938 unsigned long gfn)
939{
Paul Mackerras55514892011-12-15 02:02:47 +0000940 struct revmap_entry *rev = kvm->arch.revmap;
941 unsigned long head, i, j;
942 unsigned long *hp;
943 int ret = 1;
944
945 if (*rmapp & KVMPPC_RMAP_REFERENCED)
946 return 1;
947
948 lock_rmap(rmapp);
949 if (*rmapp & KVMPPC_RMAP_REFERENCED)
950 goto out;
951
952 if (*rmapp & KVMPPC_RMAP_PRESENT) {
953 i = head = *rmapp & KVMPPC_RMAP_INDEX;
954 do {
955 hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
956 j = rev[i].forw;
957 if (hp[1] & HPTE_R_R)
958 goto out;
959 } while ((i = j) != head);
960 }
961 ret = 0;
962
963 out:
964 unlock_rmap(rmapp);
965 return ret;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000966}
967
968int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
969{
970 if (!kvm->arch.using_mmu_notifiers)
971 return 0;
972 return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
973}
974
975void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
976{
977 if (!kvm->arch.using_mmu_notifiers)
978 return;
979 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000980}
981
Paul Mackerras82ed3612011-12-15 02:03:22 +0000982static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp)
983{
984 struct revmap_entry *rev = kvm->arch.revmap;
985 unsigned long head, i, j;
986 unsigned long *hptep;
987 int ret = 0;
988
989 retry:
990 lock_rmap(rmapp);
991 if (*rmapp & KVMPPC_RMAP_CHANGED) {
992 *rmapp &= ~KVMPPC_RMAP_CHANGED;
993 ret = 1;
994 }
995 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
996 unlock_rmap(rmapp);
997 return ret;
998 }
999
1000 i = head = *rmapp & KVMPPC_RMAP_INDEX;
1001 do {
1002 hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
1003 j = rev[i].forw;
1004
1005 if (!(hptep[1] & HPTE_R_C))
1006 continue;
1007
1008 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
1009 /* unlock rmap before spinning on the HPTE lock */
1010 unlock_rmap(rmapp);
1011 while (hptep[0] & HPTE_V_HVLOCK)
1012 cpu_relax();
1013 goto retry;
1014 }
1015
1016 /* Now check and modify the HPTE */
1017 if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
1018 /* need to make it temporarily absent to clear C */
1019 hptep[0] |= HPTE_V_ABSENT;
1020 kvmppc_invalidate_hpte(kvm, hptep, i);
1021 hptep[1] &= ~HPTE_R_C;
1022 eieio();
1023 hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
1024 rev[i].guest_rpte |= HPTE_R_C;
1025 ret = 1;
1026 }
1027 hptep[0] &= ~HPTE_V_HVLOCK;
1028 } while ((i = j) != head);
1029
1030 unlock_rmap(rmapp);
1031 return ret;
1032}
1033
1034long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
1035{
1036 unsigned long i;
1037 unsigned long *rmapp, *map;
1038
1039 preempt_disable();
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09001040 rmapp = memslot->arch.rmap;
Paul Mackerras82ed3612011-12-15 02:03:22 +00001041 map = memslot->dirty_bitmap;
1042 for (i = 0; i < memslot->npages; ++i) {
1043 if (kvm_test_clear_dirty(kvm, rmapp))
1044 __set_bit_le(i, map);
1045 ++rmapp;
1046 }
1047 preempt_enable();
1048 return 0;
1049}
1050
Paul Mackerras93e60242011-12-12 12:28:55 +00001051void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1052 unsigned long *nb_ret)
1053{
1054 struct kvm_memory_slot *memslot;
1055 unsigned long gfn = gpa >> PAGE_SHIFT;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001056 struct page *page, *pages[1];
1057 int npages;
1058 unsigned long hva, psize, offset;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001059 unsigned long pa;
Paul Mackerras93e60242011-12-12 12:28:55 +00001060 unsigned long *physp;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001061 int srcu_idx;
Paul Mackerras93e60242011-12-12 12:28:55 +00001062
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001063 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerras93e60242011-12-12 12:28:55 +00001064 memslot = gfn_to_memslot(kvm, gfn);
1065 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001066 goto err;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001067 if (!kvm->arch.using_mmu_notifiers) {
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001068 physp = memslot->arch.slot_phys;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001069 if (!physp)
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001070 goto err;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001071 physp += gfn - memslot->base_gfn;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001072 pa = *physp;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001073 if (!pa) {
1074 if (kvmppc_get_guest_page(kvm, gfn, memslot,
1075 PAGE_SIZE) < 0)
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001076 goto err;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001077 pa = *physp;
1078 }
1079 page = pfn_to_page(pa >> PAGE_SHIFT);
David Gibsonde6c0b02012-05-08 20:24:08 +10001080 get_page(page);
Paul Mackerras342d3db2011-12-12 12:38:05 +00001081 } else {
1082 hva = gfn_to_hva_memslot(memslot, gfn);
1083 npages = get_user_pages_fast(hva, 1, 1, pages);
1084 if (npages < 1)
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001085 goto err;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001086 page = pages[0];
Paul Mackerrasc77162d2011-12-12 12:31:00 +00001087 }
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001088 srcu_read_unlock(&kvm->srcu, srcu_idx);
1089
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001090 psize = PAGE_SIZE;
1091 if (PageHuge(page)) {
1092 page = compound_head(page);
1093 psize <<= compound_order(page);
1094 }
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001095 offset = gpa & (psize - 1);
Paul Mackerras93e60242011-12-12 12:28:55 +00001096 if (nb_ret)
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00001097 *nb_ret = psize - offset;
Paul Mackerras93e60242011-12-12 12:28:55 +00001098 return page_address(page) + offset;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001099
1100 err:
1101 srcu_read_unlock(&kvm->srcu, srcu_idx);
1102 return NULL;
Paul Mackerras93e60242011-12-12 12:28:55 +00001103}
1104
1105void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
1106{
1107 struct page *page = virt_to_page(va);
1108
Paul Mackerras93e60242011-12-12 12:28:55 +00001109 put_page(page);
1110}
1111
Paul Mackerrasde56a942011-06-29 00:21:34 +00001112void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
1113{
1114 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
1115
Paul Mackerras9e368f22011-06-29 00:40:08 +00001116 if (cpu_has_feature(CPU_FTR_ARCH_206))
1117 vcpu->arch.slb_nr = 32; /* POWER7 */
1118 else
1119 vcpu->arch.slb_nr = 64;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001120
1121 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
1122 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
1123
1124 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
1125}