blob: fb37290a57b410eaede72333dceff3f677ace967 [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 Mackerrasa2932922012-11-19 22:57:20 +000028#include <linux/anon_inodes.h>
29#include <linux/file.h>
Paul Mackerrase23a8082015-03-28 14:21:01 +110030#include <linux/debugfs.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000031
32#include <asm/tlbflush.h>
33#include <asm/kvm_ppc.h>
34#include <asm/kvm_book3s.h>
35#include <asm/mmu-hash64.h>
36#include <asm/hvcall.h>
37#include <asm/synch.h>
38#include <asm/ppc-opcode.h>
39#include <asm/cputable.h>
40
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060041#include "trace_hv.h"
42
Paul Mackerras32fad282012-05-04 02:32:53 +000043/* Power architecture requires HPT is at least 256kB */
44#define PPC_MIN_HPT_ORDER 18
45
Paul Mackerras7ed661b2012-11-13 18:31:32 +000046static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
47 long pte_index, unsigned long pteh,
48 unsigned long ptel, unsigned long *pte_idx_ret);
Paul Mackerrasa64fd702012-11-21 23:27:19 +000049static void kvmppc_rmap_reset(struct kvm *kvm);
Paul Mackerras7ed661b2012-11-13 18:31:32 +000050
Paul Mackerras32fad282012-05-04 02:32:53 +000051long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp)
Paul Mackerrasde56a942011-06-29 00:21:34 +000052{
Aneesh Kumar K.V792fc492014-05-06 21:24:18 +053053 unsigned long hpt = 0;
Paul Mackerras8936dda2011-12-12 12:27:39 +000054 struct revmap_entry *rev;
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +053055 struct page *page = NULL;
56 long order = KVM_DEFAULT_HPT_ORDER;
Paul Mackerrasde56a942011-06-29 00:21:34 +000057
Paul Mackerras32fad282012-05-04 02:32:53 +000058 if (htab_orderp) {
59 order = *htab_orderp;
60 if (order < PPC_MIN_HPT_ORDER)
61 order = PPC_MIN_HPT_ORDER;
62 }
63
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +053064 kvm->arch.hpt_cma_alloc = 0;
Laurent Dufour02a68d02014-09-02 18:13:01 +020065 page = kvm_alloc_hpt(1ul << (order - PAGE_SHIFT));
Aneesh Kumar K.V792fc492014-05-06 21:24:18 +053066 if (page) {
67 hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
Laurent Dufour02a68d02014-09-02 18:13:01 +020068 memset((void *)hpt, 0, (1ul << order));
Aneesh Kumar K.V792fc492014-05-06 21:24:18 +053069 kvm->arch.hpt_cma_alloc = 1;
Paul Mackerrasde56a942011-06-29 00:21:34 +000070 }
Paul Mackerras32fad282012-05-04 02:32:53 +000071
72 /* Lastly try successively smaller sizes from the page allocator */
Paul Mackerras572abd52015-09-29 17:15:45 +100073 /* Only do this if userspace didn't specify a size via ioctl */
74 while (!hpt && order > PPC_MIN_HPT_ORDER && !htab_orderp) {
Paul Mackerras32fad282012-05-04 02:32:53 +000075 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
76 __GFP_NOWARN, order - PAGE_SHIFT);
77 if (!hpt)
78 --order;
79 }
80
81 if (!hpt)
82 return -ENOMEM;
83
Paul Mackerrasde56a942011-06-29 00:21:34 +000084 kvm->arch.hpt_virt = hpt;
Paul Mackerras32fad282012-05-04 02:32:53 +000085 kvm->arch.hpt_order = order;
86 /* HPTEs are 2**4 bytes long */
87 kvm->arch.hpt_npte = 1ul << (order - 4);
88 /* 128 (2**7) bytes in each HPTEG */
89 kvm->arch.hpt_mask = (1ul << (order - 7)) - 1;
Paul Mackerrasde56a942011-06-29 00:21:34 +000090
Paul Mackerras8936dda2011-12-12 12:27:39 +000091 /* Allocate reverse map array */
Paul Mackerras32fad282012-05-04 02:32:53 +000092 rev = vmalloc(sizeof(struct revmap_entry) * kvm->arch.hpt_npte);
Paul Mackerras8936dda2011-12-12 12:27:39 +000093 if (!rev) {
94 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
95 goto out_freehpt;
96 }
97 kvm->arch.revmap = rev;
Paul Mackerras32fad282012-05-04 02:32:53 +000098 kvm->arch.sdr1 = __pa(hpt) | (order - 18);
Paul Mackerras8936dda2011-12-12 12:27:39 +000099
Paul Mackerras32fad282012-05-04 02:32:53 +0000100 pr_info("KVM guest htab at %lx (order %ld), LPID %x\n",
101 hpt, order, kvm->arch.lpid);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000102
Paul Mackerras32fad282012-05-04 02:32:53 +0000103 if (htab_orderp)
104 *htab_orderp = order;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000105 return 0;
Paul Mackerras8936dda2011-12-12 12:27:39 +0000106
Paul Mackerras8936dda2011-12-12 12:27:39 +0000107 out_freehpt:
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530108 if (kvm->arch.hpt_cma_alloc)
109 kvm_release_hpt(page, 1 << (order - PAGE_SHIFT));
Paul Mackerras32fad282012-05-04 02:32:53 +0000110 else
111 free_pages(hpt, order - PAGE_SHIFT);
Paul Mackerras8936dda2011-12-12 12:27:39 +0000112 return -ENOMEM;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000113}
114
Paul Mackerras32fad282012-05-04 02:32:53 +0000115long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp)
116{
117 long err = -EBUSY;
118 long order;
119
120 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +1100121 if (kvm->arch.hpte_setup_done) {
122 kvm->arch.hpte_setup_done = 0;
123 /* order hpte_setup_done vs. vcpus_running */
Paul Mackerras32fad282012-05-04 02:32:53 +0000124 smp_mb();
125 if (atomic_read(&kvm->arch.vcpus_running)) {
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +1100126 kvm->arch.hpte_setup_done = 1;
Paul Mackerras32fad282012-05-04 02:32:53 +0000127 goto out;
128 }
129 }
130 if (kvm->arch.hpt_virt) {
131 order = kvm->arch.hpt_order;
132 /* Set the entire HPT to 0, i.e. invalid HPTEs */
133 memset((void *)kvm->arch.hpt_virt, 0, 1ul << order);
134 /*
Paul Mackerrasa64fd702012-11-21 23:27:19 +0000135 * Reset all the reverse-mapping chains for all memslots
136 */
137 kvmppc_rmap_reset(kvm);
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000138 /* Ensure that each vcpu will flush its TLB on next entry. */
139 cpumask_setall(&kvm->arch.need_tlb_flush);
Paul Mackerras32fad282012-05-04 02:32:53 +0000140 *htab_orderp = order;
141 err = 0;
142 } else {
143 err = kvmppc_alloc_hpt(kvm, htab_orderp);
144 order = *htab_orderp;
145 }
146 out:
147 mutex_unlock(&kvm->lock);
148 return err;
149}
150
Paul Mackerrasde56a942011-06-29 00:21:34 +0000151void kvmppc_free_hpt(struct kvm *kvm)
152{
Scott Wood043cc4d2011-12-20 15:34:20 +0000153 kvmppc_free_lpid(kvm->arch.lpid);
Paul Mackerras8936dda2011-12-12 12:27:39 +0000154 vfree(kvm->arch.revmap);
Aneesh Kumar K.Vfa61a4e32013-07-02 11:15:16 +0530155 if (kvm->arch.hpt_cma_alloc)
156 kvm_release_hpt(virt_to_page(kvm->arch.hpt_virt),
157 1 << (kvm->arch.hpt_order - PAGE_SHIFT));
Alexander Grafd2a1b482012-01-16 19:12:11 +0100158 else
Paul Mackerras32fad282012-05-04 02:32:53 +0000159 free_pages(kvm->arch.hpt_virt,
160 kvm->arch.hpt_order - PAGE_SHIFT);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000161}
162
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000163/* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
164static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000165{
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000166 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
167}
168
169/* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
170static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
171{
172 return (pgsize == 0x10000) ? 0x1000 : 0;
173}
174
175void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
176 unsigned long porder)
177{
Paul Mackerrasde56a942011-06-29 00:21:34 +0000178 unsigned long i;
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000179 unsigned long npages;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000180 unsigned long hp_v, hp_r;
181 unsigned long addr, hash;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000182 unsigned long psize;
183 unsigned long hp0, hp1;
Paul Mackerras7ed661b2012-11-13 18:31:32 +0000184 unsigned long idx_ret;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000185 long ret;
Paul Mackerras32fad282012-05-04 02:32:53 +0000186 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000187
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000188 psize = 1ul << porder;
189 npages = memslot->npages >> (porder - PAGE_SHIFT);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000190
191 /* VRMA can't be > 1TB */
Paul Mackerras8936dda2011-12-12 12:27:39 +0000192 if (npages > 1ul << (40 - porder))
193 npages = 1ul << (40 - porder);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000194 /* Can't use more than 1 HPTE per HPTEG */
Paul Mackerras32fad282012-05-04 02:32:53 +0000195 if (npages > kvm->arch.hpt_mask + 1)
196 npages = kvm->arch.hpt_mask + 1;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000197
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000198 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
199 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
200 hp1 = hpte1_pgsize_encoding(psize) |
201 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
202
Paul Mackerrasde56a942011-06-29 00:21:34 +0000203 for (i = 0; i < npages; ++i) {
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000204 addr = i << porder;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000205 /* can't use hpt_hash since va > 64 bits */
Paul Mackerras32fad282012-05-04 02:32:53 +0000206 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & kvm->arch.hpt_mask;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000207 /*
208 * We assume that the hash table is empty and no
209 * vcpus are using it at this stage. Since we create
210 * at most one HPTE per HPTEG, we just assume entry 7
211 * is available and use it.
212 */
Paul Mackerras8936dda2011-12-12 12:27:39 +0000213 hash = (hash << 3) + 7;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000214 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
215 hp_r = hp1 | addr;
Paul Mackerras7ed661b2012-11-13 18:31:32 +0000216 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
217 &idx_ret);
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000218 if (ret != H_SUCCESS) {
219 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
220 addr, ret);
221 break;
222 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000223 }
224}
225
226int kvmppc_mmu_hv_init(void)
227{
Paul Mackerras9e368f22011-06-29 00:40:08 +0000228 unsigned long host_lpid, rsvd_lpid;
229
230 if (!cpu_has_feature(CPU_FTR_HVMODE))
Paul Mackerrasde56a942011-06-29 00:21:34 +0000231 return -EINVAL;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000232
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100233 /* POWER7 has 10-bit LPIDs (12-bit in POWER8) */
234 host_lpid = mfspr(SPRN_LPID);
235 rsvd_lpid = LPID_RSVD;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000236
Scott Wood043cc4d2011-12-20 15:34:20 +0000237 kvmppc_init_lpid(rsvd_lpid + 1);
238
239 kvmppc_claim_lpid(host_lpid);
Paul Mackerras9e368f22011-06-29 00:40:08 +0000240 /* rsvd_lpid is reserved for use in partition switching */
Scott Wood043cc4d2011-12-20 15:34:20 +0000241 kvmppc_claim_lpid(rsvd_lpid);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000242
243 return 0;
244}
245
Paul Mackerrasde56a942011-06-29 00:21:34 +0000246static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
247{
Michael Neulinge4e38122014-03-25 10:47:02 +1100248 unsigned long msr = vcpu->arch.intr_msr;
249
250 /* If transactional, change to suspend mode on IRQ delivery */
251 if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
252 msr |= MSR_TS_S;
253 else
254 msr |= vcpu->arch.shregs.msr & MSR_TS_MASK;
255 kvmppc_set_msr(vcpu, msr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000256}
257
Paul Mackerras7ed661b2012-11-13 18:31:32 +0000258long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
259 long pte_index, unsigned long pteh,
260 unsigned long ptel, unsigned long *pte_idx_ret)
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000261{
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000262 long ret;
263
Paul Mackerras342d3db2011-12-12 12:38:05 +0000264 /* Protect linux PTE lookup from page table destruction */
265 rcu_read_lock_sched(); /* this disables preemption too */
Paul Mackerras7ed661b2012-11-13 18:31:32 +0000266 ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
267 current->mm->pgd, false, pte_idx_ret);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000268 rcu_read_unlock_sched();
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000269 if (ret == H_TOO_HARD) {
270 /* this can't happen */
271 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
272 ret = H_RESOURCE; /* or something */
273 }
274 return ret;
275
276}
277
Paul Mackerras697d3892011-12-12 12:36:37 +0000278static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
279 gva_t eaddr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000280{
Paul Mackerras697d3892011-12-12 12:36:37 +0000281 u64 mask;
282 int i;
283
284 for (i = 0; i < vcpu->arch.slb_nr; i++) {
285 if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
286 continue;
287
288 if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
289 mask = ESID_MASK_1T;
290 else
291 mask = ESID_MASK;
292
293 if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
294 return &vcpu->arch.slb[i];
295 }
296 return NULL;
297}
298
299static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
300 unsigned long ea)
301{
302 unsigned long ra_mask;
303
304 ra_mask = hpte_page_size(v, r) - 1;
305 return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
306}
307
308static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
Paul Mackerras93b159b2013-09-20 14:52:51 +1000309 struct kvmppc_pte *gpte, bool data, bool iswrite)
Paul Mackerras697d3892011-12-12 12:36:37 +0000310{
311 struct kvm *kvm = vcpu->kvm;
312 struct kvmppc_slb *slbe;
313 unsigned long slb_v;
314 unsigned long pp, key;
315 unsigned long v, gr;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200316 __be64 *hptep;
Paul Mackerras697d3892011-12-12 12:36:37 +0000317 int index;
318 int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
319
320 /* Get SLB entry */
321 if (virtmode) {
322 slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
323 if (!slbe)
324 return -EINVAL;
325 slb_v = slbe->origv;
326 } else {
327 /* real mode access */
328 slb_v = vcpu->kvm->arch.vrma_slb_v;
329 }
330
pingfan liu91648ec2013-11-15 16:35:00 +0800331 preempt_disable();
Paul Mackerras697d3892011-12-12 12:36:37 +0000332 /* Find the HPTE in the hash table */
333 index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
334 HPTE_V_VALID | HPTE_V_ABSENT);
pingfan liu91648ec2013-11-15 16:35:00 +0800335 if (index < 0) {
336 preempt_enable();
Paul Mackerras697d3892011-12-12 12:36:37 +0000337 return -ENOENT;
pingfan liu91648ec2013-11-15 16:35:00 +0800338 }
Alexander Graf6f22bd32014-06-11 10:16:06 +0200339 hptep = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
340 v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
Paul Mackerras697d3892011-12-12 12:36:37 +0000341 gr = kvm->arch.revmap[index].guest_rpte;
342
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +1100343 unlock_hpte(hptep, v);
pingfan liu91648ec2013-11-15 16:35:00 +0800344 preempt_enable();
Paul Mackerras697d3892011-12-12 12:36:37 +0000345
346 gpte->eaddr = eaddr;
347 gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
348
349 /* Get PP bits and key for permission check */
350 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
351 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
352 key &= slb_v;
353
354 /* Calculate permissions */
355 gpte->may_read = hpte_read_permission(pp, key);
356 gpte->may_write = hpte_write_permission(pp, key);
357 gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
358
359 /* Storage key permission check for POWER7 */
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100360 if (data && virtmode) {
Paul Mackerras697d3892011-12-12 12:36:37 +0000361 int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
362 if (amrfield & 1)
363 gpte->may_read = 0;
364 if (amrfield & 2)
365 gpte->may_write = 0;
366 }
367
368 /* Get the guest physical address */
369 gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
370 return 0;
371}
372
373/*
374 * Quick test for whether an instruction is a load or a store.
375 * If the instruction is a load or a store, then this will indicate
376 * which it is, at least on server processors. (Embedded processors
377 * have some external PID instructions that don't follow the rule
378 * embodied here.) If the instruction isn't a load or store, then
379 * this doesn't return anything useful.
380 */
381static int instruction_is_store(unsigned int instr)
382{
383 unsigned int mask;
384
385 mask = 0x10000000;
386 if ((instr & 0xfc000000) == 0x7c000000)
387 mask = 0x100; /* major opcode 31 */
388 return (instr & mask) != 0;
389}
390
391static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Graf6020c0f2012-03-12 02:26:30 +0100392 unsigned long gpa, gva_t ea, int is_store)
Paul Mackerras697d3892011-12-12 12:36:37 +0000393{
Paul Mackerras697d3892011-12-12 12:36:37 +0000394 u32 last_inst;
Paul Mackerras697d3892011-12-12 12:36:37 +0000395
Mihai Caraman51f04722014-07-23 19:06:21 +0300396 /*
Paul Mackerras697d3892011-12-12 12:36:37 +0000397 * If we fail, we just return to the guest and try executing it again.
398 */
Mihai Caraman51f04722014-07-23 19:06:21 +0300399 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
400 EMULATE_DONE)
401 return RESUME_GUEST;
Paul Mackerras697d3892011-12-12 12:36:37 +0000402
403 /*
404 * WARNING: We do not know for sure whether the instruction we just
405 * read from memory is the same that caused the fault in the first
406 * place. If the instruction we read is neither an load or a store,
407 * then it can't access memory, so we don't need to worry about
408 * enforcing access permissions. So, assuming it is a load or
409 * store, we just check that its direction (load or store) is
410 * consistent with the original fault, since that's what we
411 * checked the access permissions against. If there is a mismatch
412 * we just return and retry the instruction.
413 */
414
Mihai Caraman51f04722014-07-23 19:06:21 +0300415 if (instruction_is_store(last_inst) != !!is_store)
Paul Mackerras697d3892011-12-12 12:36:37 +0000416 return RESUME_GUEST;
417
418 /*
419 * Emulated accesses are emulated by looking at the hash for
420 * translation once, then performing the access later. The
421 * translation could be invalidated in the meantime in which
422 * point performing the subsequent memory access on the old
423 * physical address could possibly be a security hole for the
424 * guest (but not the host).
425 *
426 * This is less of an issue for MMIO stores since they aren't
427 * globally visible. It could be an issue for MMIO loads to
428 * a certain extent but we'll ignore it for now.
429 */
430
431 vcpu->arch.paddr_accessed = gpa;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100432 vcpu->arch.vaddr_accessed = ea;
Paul Mackerras697d3892011-12-12 12:36:37 +0000433 return kvmppc_emulate_mmio(run, vcpu);
434}
435
436int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
437 unsigned long ea, unsigned long dsisr)
438{
439 struct kvm *kvm = vcpu->kvm;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200440 unsigned long hpte[3], r;
441 __be64 *hptep;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000442 unsigned long mmu_seq, psize, pte_size;
Paul Mackerras1066f772014-05-26 19:48:37 +1000443 unsigned long gpa_base, gfn_base;
Paul Mackerras70bddfe2012-09-20 19:39:21 +0000444 unsigned long gpa, gfn, hva, pfn;
Paul Mackerras697d3892011-12-12 12:36:37 +0000445 struct kvm_memory_slot *memslot;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000446 unsigned long *rmap;
Paul Mackerras697d3892011-12-12 12:36:37 +0000447 struct revmap_entry *rev;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000448 struct page *page, *pages[1];
449 long index, ret, npages;
450 unsigned long is_io;
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000451 unsigned int writing, write_ok;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000452 struct vm_area_struct *vma;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000453 unsigned long rcbits;
Paul Mackerras697d3892011-12-12 12:36:37 +0000454
455 /*
456 * Real-mode code has already searched the HPT and found the
457 * entry we're interested in. Lock the entry and check that
458 * it hasn't changed. If it has, just return and re-execute the
459 * instruction.
460 */
461 if (ea != vcpu->arch.pgfault_addr)
462 return RESUME_GUEST;
463 index = vcpu->arch.pgfault_index;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200464 hptep = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
Paul Mackerras697d3892011-12-12 12:36:37 +0000465 rev = &kvm->arch.revmap[index];
466 preempt_disable();
467 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
468 cpu_relax();
Alexander Graf6f22bd32014-06-11 10:16:06 +0200469 hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
470 hpte[1] = be64_to_cpu(hptep[1]);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000471 hpte[2] = r = rev->guest_rpte;
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +1100472 unlock_hpte(hptep, hpte[0]);
Paul Mackerras697d3892011-12-12 12:36:37 +0000473 preempt_enable();
474
475 if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
476 hpte[1] != vcpu->arch.pgfault_hpte[1])
477 return RESUME_GUEST;
478
479 /* Translate the logical address and get the page */
Paul Mackerras342d3db2011-12-12 12:38:05 +0000480 psize = hpte_page_size(hpte[0], r);
Paul Mackerras1066f772014-05-26 19:48:37 +1000481 gpa_base = r & HPTE_R_RPN & ~(psize - 1);
482 gfn_base = gpa_base >> PAGE_SHIFT;
483 gpa = gpa_base | (ea & (psize - 1));
Paul Mackerras70bddfe2012-09-20 19:39:21 +0000484 gfn = gpa >> PAGE_SHIFT;
Paul Mackerras697d3892011-12-12 12:36:37 +0000485 memslot = gfn_to_memslot(kvm, gfn);
486
Suresh E. Warrier3c78f782014-12-03 18:48:10 -0600487 trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
488
Paul Mackerras697d3892011-12-12 12:36:37 +0000489 /* No memslot means it's an emulated MMIO region */
Paul Mackerras70bddfe2012-09-20 19:39:21 +0000490 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Alexander Graf6020c0f2012-03-12 02:26:30 +0100491 return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
Paul Mackerras697d3892011-12-12 12:36:37 +0000492 dsisr & DSISR_ISSTORE);
Paul Mackerras697d3892011-12-12 12:36:37 +0000493
Paul Mackerras1066f772014-05-26 19:48:37 +1000494 /*
495 * This should never happen, because of the slot_is_aligned()
496 * check in kvmppc_do_h_enter().
497 */
498 if (gfn_base < memslot->base_gfn)
499 return -EFAULT;
500
Paul Mackerras342d3db2011-12-12 12:38:05 +0000501 /* used to check for invalidations in progress */
502 mmu_seq = kvm->mmu_notifier_seq;
503 smp_rmb();
504
Suresh E. Warrier3c78f782014-12-03 18:48:10 -0600505 ret = -EFAULT;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000506 is_io = 0;
507 pfn = 0;
508 page = NULL;
509 pte_size = PAGE_SIZE;
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000510 writing = (dsisr & DSISR_ISSTORE) != 0;
511 /* If writing != 0, then the HPTE must allow writing, if we get here */
512 write_ok = writing;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000513 hva = gfn_to_hva_memslot(memslot, gfn);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000514 npages = get_user_pages_fast(hva, 1, writing, pages);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000515 if (npages < 1) {
516 /* Check if it's an I/O mapping */
517 down_read(&current->mm->mmap_sem);
518 vma = find_vma(current->mm, hva);
519 if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
520 (vma->vm_flags & VM_PFNMAP)) {
521 pfn = vma->vm_pgoff +
522 ((hva - vma->vm_start) >> PAGE_SHIFT);
523 pte_size = psize;
524 is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000525 write_ok = vma->vm_flags & VM_WRITE;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000526 }
527 up_read(&current->mm->mmap_sem);
528 if (!pfn)
Suresh E. Warrier3c78f782014-12-03 18:48:10 -0600529 goto out_put;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000530 } else {
531 page = pages[0];
Paul Mackerrascaaa4c82013-11-16 17:46:02 +1100532 pfn = page_to_pfn(page);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000533 if (PageHuge(page)) {
534 page = compound_head(page);
535 pte_size <<= compound_order(page);
536 }
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000537 /* if the guest wants write access, see if that is OK */
538 if (!writing && hpte_is_writable(r)) {
539 pte_t *ptep, pte;
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530540 unsigned long flags;
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000541 /*
542 * We need to protect against page table destruction
Aneesh Kumar K.V7d6e7f72015-03-30 10:41:04 +0530543 * hugepage split and collapse.
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000544 */
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530545 local_irq_save(flags);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000546 ptep = find_linux_pte_or_hugepte(current->mm->pgd,
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +0530547 hva, NULL, NULL);
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530548 if (ptep) {
Aneesh Kumar K.V7d6e7f72015-03-30 10:41:04 +0530549 pte = kvmppc_read_update_linux_pte(ptep, 1);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000550 if (pte_write(pte))
551 write_ok = 1;
552 }
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530553 local_irq_restore(flags);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000554 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000555 }
556
Paul Mackerras342d3db2011-12-12 12:38:05 +0000557 if (psize > pte_size)
558 goto out_put;
559
560 /* Check WIMG vs. the actual page we're accessing */
561 if (!hpte_cache_flags_ok(r, is_io)) {
562 if (is_io)
Suresh E. Warrier3c78f782014-12-03 18:48:10 -0600563 goto out_put;
564
Paul Mackerras342d3db2011-12-12 12:38:05 +0000565 /*
566 * Allow guest to map emulated device memory as
567 * uncacheable, but actually make it cacheable.
568 */
569 r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
570 }
571
Paul Mackerrascaaa4c82013-11-16 17:46:02 +1100572 /*
573 * Set the HPTE to point to pfn.
574 * Since the pfn is at PAGE_SIZE granularity, make sure we
575 * don't mask out lower-order bits if psize < PAGE_SIZE.
576 */
577 if (psize < PAGE_SIZE)
578 psize = PAGE_SIZE;
579 r = (r & ~(HPTE_R_PP0 - psize)) | ((pfn << PAGE_SHIFT) & ~(psize - 1));
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000580 if (hpte_is_writable(r) && !write_ok)
581 r = hpte_make_readonly(r);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000582 ret = RESUME_GUEST;
583 preempt_disable();
584 while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
585 cpu_relax();
Alexander Graf6f22bd32014-06-11 10:16:06 +0200586 if ((be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK) != hpte[0] ||
587 be64_to_cpu(hptep[1]) != hpte[1] ||
588 rev->guest_rpte != hpte[2])
Paul Mackerras342d3db2011-12-12 12:38:05 +0000589 /* HPTE has been changed under us; let the guest retry */
590 goto out_unlock;
591 hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
592
Paul Mackerras1066f772014-05-26 19:48:37 +1000593 /* Always put the HPTE in the rmap chain for the page base address */
594 rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
Paul Mackerras342d3db2011-12-12 12:38:05 +0000595 lock_rmap(rmap);
596
597 /* Check if we might have been invalidated; let the guest retry if so */
598 ret = RESUME_GUEST;
Christoffer Dall8ca40a72012-10-14 23:10:18 -0400599 if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
Paul Mackerras342d3db2011-12-12 12:38:05 +0000600 unlock_rmap(rmap);
601 goto out_unlock;
602 }
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000603
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000604 /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
605 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
606 r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
607
Alexander Graf6f22bd32014-06-11 10:16:06 +0200608 if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000609 /* HPTE was previously valid, so we need to invalidate it */
610 unlock_rmap(rmap);
Alexander Graf6f22bd32014-06-11 10:16:06 +0200611 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000612 kvmppc_invalidate_hpte(kvm, hptep, index);
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000613 /* don't lose previous R and C bits */
Alexander Graf6f22bd32014-06-11 10:16:06 +0200614 r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000615 } else {
616 kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
617 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000618
Alexander Graf6f22bd32014-06-11 10:16:06 +0200619 hptep[1] = cpu_to_be64(r);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000620 eieio();
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +1100621 __unlock_hpte(hptep, hpte[0]);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000622 asm volatile("ptesync" : : : "memory");
623 preempt_enable();
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000624 if (page && hpte_is_writable(r))
Paul Mackerras342d3db2011-12-12 12:38:05 +0000625 SetPageDirty(page);
626
627 out_put:
Suresh E. Warrier3c78f782014-12-03 18:48:10 -0600628 trace_kvm_page_fault_exit(vcpu, hpte, ret);
629
David Gibsonde6c0b02012-05-08 20:24:08 +1000630 if (page) {
631 /*
632 * We drop pages[0] here, not page because page might
633 * have been set to the head page of a compound, but
634 * we have to drop the reference on the correct tail
635 * page to match the get inside gup()
636 */
637 put_page(pages[0]);
638 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000639 return ret;
640
641 out_unlock:
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +1100642 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
Paul Mackerras342d3db2011-12-12 12:38:05 +0000643 preempt_enable();
644 goto out_put;
645}
646
Paul Mackerrasa64fd702012-11-21 23:27:19 +0000647static void kvmppc_rmap_reset(struct kvm *kvm)
648{
649 struct kvm_memslots *slots;
650 struct kvm_memory_slot *memslot;
651 int srcu_idx;
652
653 srcu_idx = srcu_read_lock(&kvm->srcu);
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200654 slots = kvm_memslots(kvm);
Paul Mackerrasa64fd702012-11-21 23:27:19 +0000655 kvm_for_each_memslot(memslot, slots) {
656 /*
657 * This assumes it is acceptable to lose reference and
658 * change bits across a reset.
659 */
660 memset(memslot->arch.rmap, 0,
661 memslot->npages * sizeof(*memslot->arch.rmap));
662 }
663 srcu_read_unlock(&kvm->srcu, srcu_idx);
664}
665
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +0900666static int kvm_handle_hva_range(struct kvm *kvm,
667 unsigned long start,
668 unsigned long end,
669 int (*handler)(struct kvm *kvm,
670 unsigned long *rmapp,
671 unsigned long gfn))
Paul Mackerras342d3db2011-12-12 12:38:05 +0000672{
673 int ret;
674 int retval = 0;
675 struct kvm_memslots *slots;
676 struct kvm_memory_slot *memslot;
677
678 slots = kvm_memslots(kvm);
679 kvm_for_each_memslot(memslot, slots) {
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +0900680 unsigned long hva_start, hva_end;
681 gfn_t gfn, gfn_end;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000682
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +0900683 hva_start = max(start, memslot->userspace_addr);
684 hva_end = min(end, memslot->userspace_addr +
685 (memslot->npages << PAGE_SHIFT));
686 if (hva_start >= hva_end)
687 continue;
688 /*
689 * {gfn(page) | page intersects with [hva_start, hva_end)} =
690 * {gfn, gfn+1, ..., gfn_end-1}.
691 */
692 gfn = hva_to_gfn_memslot(hva_start, memslot);
693 gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
694
695 for (; gfn < gfn_end; ++gfn) {
Takuya Yoshikawad19a7482012-07-02 17:54:30 +0900696 gfn_t gfn_offset = gfn - memslot->base_gfn;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000697
Takuya Yoshikawad89cc612012-08-01 18:03:28 +0900698 ret = handler(kvm, &memslot->arch.rmap[gfn_offset], gfn);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000699 retval |= ret;
700 }
701 }
702
703 return retval;
704}
705
Takuya Yoshikawa84504ef2012-07-02 17:55:48 +0900706static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
707 int (*handler)(struct kvm *kvm, unsigned long *rmapp,
708 unsigned long gfn))
709{
710 return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
711}
712
Paul Mackerras342d3db2011-12-12 12:38:05 +0000713static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
714 unsigned long gfn)
715{
716 struct revmap_entry *rev = kvm->arch.revmap;
717 unsigned long h, i, j;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200718 __be64 *hptep;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000719 unsigned long ptel, psize, rcbits;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000720
721 for (;;) {
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000722 lock_rmap(rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000723 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000724 unlock_rmap(rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000725 break;
726 }
727
728 /*
729 * To avoid an ABBA deadlock with the HPTE lock bit,
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000730 * we can't spin on the HPTE lock while holding the
731 * rmap chain lock.
Paul Mackerras342d3db2011-12-12 12:38:05 +0000732 */
733 i = *rmapp & KVMPPC_RMAP_INDEX;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200734 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000735 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
736 /* unlock rmap before spinning on the HPTE lock */
737 unlock_rmap(rmapp);
Alexander Graf6f22bd32014-06-11 10:16:06 +0200738 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000739 cpu_relax();
740 continue;
741 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000742 j = rev[i].forw;
743 if (j == i) {
744 /* chain is now empty */
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000745 *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000746 } else {
747 /* remove i from chain */
748 h = rev[i].back;
749 rev[h].forw = j;
750 rev[j].back = h;
751 rev[i].forw = rev[i].back = i;
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000752 *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000753 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000754
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000755 /* Now check and modify the HPTE */
Paul Mackerras342d3db2011-12-12 12:38:05 +0000756 ptel = rev[i].guest_rpte;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200757 psize = hpte_page_size(be64_to_cpu(hptep[0]), ptel);
758 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
Paul Mackerras342d3db2011-12-12 12:38:05 +0000759 hpte_rpn(ptel, psize) == gfn) {
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100760 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000761 kvmppc_invalidate_hpte(kvm, hptep, i);
762 /* Harvest R and C */
Alexander Graf6f22bd32014-06-11 10:16:06 +0200763 rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000764 *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
Paul Mackerras08fe1e72015-06-24 21:18:06 +1000765 if (rcbits & HPTE_R_C)
766 kvmppc_update_rmap_change(rmapp, psize);
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000767 if (rcbits & ~rev[i].guest_rpte) {
768 rev[i].guest_rpte = ptel | rcbits;
769 note_hpte_modification(kvm, &rev[i]);
770 }
Paul Mackerras342d3db2011-12-12 12:38:05 +0000771 }
Paul Mackerrasbad3b502011-12-15 02:02:02 +0000772 unlock_rmap(rmapp);
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +1100773 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
Paul Mackerras342d3db2011-12-12 12:38:05 +0000774 }
775 return 0;
776}
777
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530778int kvm_unmap_hva_hv(struct kvm *kvm, unsigned long hva)
Paul Mackerras342d3db2011-12-12 12:38:05 +0000779{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100780 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000781 return 0;
782}
783
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530784int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end)
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +0900785{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100786 kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
Takuya Yoshikawab3ae2092012-07-02 17:56:33 +0900787 return 0;
788}
789
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530790void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
791 struct kvm_memory_slot *memslot)
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000792{
793 unsigned long *rmapp;
794 unsigned long gfn;
795 unsigned long n;
796
797 rmapp = memslot->arch.rmap;
798 gfn = memslot->base_gfn;
799 for (n = memslot->npages; n; --n) {
800 /*
801 * Testing the present bit without locking is OK because
802 * the memslot has been marked invalid already, and hence
803 * no new HPTEs referencing this page can be created,
804 * thus the present bit can't go from 0 to 1.
805 */
806 if (*rmapp & KVMPPC_RMAP_PRESENT)
807 kvm_unmap_rmapp(kvm, rmapp, gfn);
808 ++rmapp;
809 ++gfn;
810 }
811}
812
Paul Mackerras342d3db2011-12-12 12:38:05 +0000813static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
814 unsigned long gfn)
815{
Paul Mackerras55514892011-12-15 02:02:47 +0000816 struct revmap_entry *rev = kvm->arch.revmap;
817 unsigned long head, i, j;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200818 __be64 *hptep;
Paul Mackerras55514892011-12-15 02:02:47 +0000819 int ret = 0;
820
821 retry:
822 lock_rmap(rmapp);
823 if (*rmapp & KVMPPC_RMAP_REFERENCED) {
824 *rmapp &= ~KVMPPC_RMAP_REFERENCED;
825 ret = 1;
826 }
827 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
828 unlock_rmap(rmapp);
829 return ret;
830 }
831
832 i = head = *rmapp & KVMPPC_RMAP_INDEX;
833 do {
Alexander Graf6f22bd32014-06-11 10:16:06 +0200834 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
Paul Mackerras55514892011-12-15 02:02:47 +0000835 j = rev[i].forw;
836
837 /* If this HPTE isn't referenced, ignore it */
Alexander Graf6f22bd32014-06-11 10:16:06 +0200838 if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
Paul Mackerras55514892011-12-15 02:02:47 +0000839 continue;
840
841 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
842 /* unlock rmap before spinning on the HPTE lock */
843 unlock_rmap(rmapp);
Alexander Graf6f22bd32014-06-11 10:16:06 +0200844 while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
Paul Mackerras55514892011-12-15 02:02:47 +0000845 cpu_relax();
846 goto retry;
847 }
848
849 /* Now check and modify the HPTE */
Alexander Graf6f22bd32014-06-11 10:16:06 +0200850 if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
851 (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
Paul Mackerras55514892011-12-15 02:02:47 +0000852 kvmppc_clear_ref_hpte(kvm, hptep, i);
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000853 if (!(rev[i].guest_rpte & HPTE_R_R)) {
854 rev[i].guest_rpte |= HPTE_R_R;
855 note_hpte_modification(kvm, &rev[i]);
856 }
Paul Mackerras55514892011-12-15 02:02:47 +0000857 ret = 1;
858 }
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +1100859 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
Paul Mackerras55514892011-12-15 02:02:47 +0000860 } while ((i = j) != head);
861
862 unlock_rmap(rmapp);
863 return ret;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000864}
865
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700866int kvm_age_hva_hv(struct kvm *kvm, unsigned long start, unsigned long end)
Paul Mackerras342d3db2011-12-12 12:38:05 +0000867{
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700868 return kvm_handle_hva_range(kvm, start, end, kvm_age_rmapp);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000869}
870
871static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
872 unsigned long gfn)
873{
Paul Mackerras55514892011-12-15 02:02:47 +0000874 struct revmap_entry *rev = kvm->arch.revmap;
875 unsigned long head, i, j;
876 unsigned long *hp;
877 int ret = 1;
878
879 if (*rmapp & KVMPPC_RMAP_REFERENCED)
880 return 1;
881
882 lock_rmap(rmapp);
883 if (*rmapp & KVMPPC_RMAP_REFERENCED)
884 goto out;
885
886 if (*rmapp & KVMPPC_RMAP_PRESENT) {
887 i = head = *rmapp & KVMPPC_RMAP_INDEX;
888 do {
889 hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
890 j = rev[i].forw;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200891 if (be64_to_cpu(hp[1]) & HPTE_R_R)
Paul Mackerras55514892011-12-15 02:02:47 +0000892 goto out;
893 } while ((i = j) != head);
894 }
895 ret = 0;
896
897 out:
898 unlock_rmap(rmapp);
899 return ret;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000900}
901
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530902int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva)
Paul Mackerras342d3db2011-12-12 12:38:05 +0000903{
Paul Mackerras342d3db2011-12-12 12:38:05 +0000904 return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
905}
906
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530907void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte)
Paul Mackerras342d3db2011-12-12 12:38:05 +0000908{
Paul Mackerras342d3db2011-12-12 12:38:05 +0000909 kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000910}
911
Paul Mackerras6c576e72014-05-26 19:48:39 +1000912static int vcpus_running(struct kvm *kvm)
913{
914 return atomic_read(&kvm->arch.vcpus_running) != 0;
915}
916
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +1000917/*
918 * Returns the number of system pages that are dirty.
919 * This can be more than 1 if we find a huge-page HPTE.
920 */
921static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
Paul Mackerras82ed3612011-12-15 02:03:22 +0000922{
923 struct revmap_entry *rev = kvm->arch.revmap;
924 unsigned long head, i, j;
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +1000925 unsigned long n;
Paul Mackerras6c576e72014-05-26 19:48:39 +1000926 unsigned long v, r;
Alexander Graf6f22bd32014-06-11 10:16:06 +0200927 __be64 *hptep;
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +1000928 int npages_dirty = 0;
Paul Mackerras82ed3612011-12-15 02:03:22 +0000929
930 retry:
931 lock_rmap(rmapp);
932 if (*rmapp & KVMPPC_RMAP_CHANGED) {
Paul Mackerras08fe1e72015-06-24 21:18:06 +1000933 long change_order = (*rmapp & KVMPPC_RMAP_CHG_ORDER)
934 >> KVMPPC_RMAP_CHG_SHIFT;
935 *rmapp &= ~(KVMPPC_RMAP_CHANGED | KVMPPC_RMAP_CHG_ORDER);
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +1000936 npages_dirty = 1;
Paul Mackerras08fe1e72015-06-24 21:18:06 +1000937 if (change_order > PAGE_SHIFT)
938 npages_dirty = 1ul << (change_order - PAGE_SHIFT);
Paul Mackerras82ed3612011-12-15 02:03:22 +0000939 }
940 if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
941 unlock_rmap(rmapp);
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +1000942 return npages_dirty;
Paul Mackerras82ed3612011-12-15 02:03:22 +0000943 }
944
945 i = head = *rmapp & KVMPPC_RMAP_INDEX;
946 do {
Alexander Graf6f22bd32014-06-11 10:16:06 +0200947 unsigned long hptep1;
948 hptep = (__be64 *) (kvm->arch.hpt_virt + (i << 4));
Paul Mackerras82ed3612011-12-15 02:03:22 +0000949 j = rev[i].forw;
950
Paul Mackerras6c576e72014-05-26 19:48:39 +1000951 /*
952 * Checking the C (changed) bit here is racy since there
953 * is no guarantee about when the hardware writes it back.
954 * If the HPTE is not writable then it is stable since the
955 * page can't be written to, and we would have done a tlbie
956 * (which forces the hardware to complete any writeback)
957 * when making the HPTE read-only.
958 * If vcpus are running then this call is racy anyway
959 * since the page could get dirtied subsequently, so we
960 * expect there to be a further call which would pick up
961 * any delayed C bit writeback.
962 * Otherwise we need to do the tlbie even if C==0 in
963 * order to pick up any delayed writeback of C.
964 */
Alexander Graf6f22bd32014-06-11 10:16:06 +0200965 hptep1 = be64_to_cpu(hptep[1]);
966 if (!(hptep1 & HPTE_R_C) &&
967 (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
Paul Mackerras82ed3612011-12-15 02:03:22 +0000968 continue;
969
970 if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
971 /* unlock rmap before spinning on the HPTE lock */
972 unlock_rmap(rmapp);
Alexander Graf6f22bd32014-06-11 10:16:06 +0200973 while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
Paul Mackerras82ed3612011-12-15 02:03:22 +0000974 cpu_relax();
975 goto retry;
976 }
977
978 /* Now check and modify the HPTE */
Aneesh Kumar K.Vf6fb9e82014-11-05 12:21:13 +1100979 if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +1100980 __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
Paul Mackerras6c576e72014-05-26 19:48:39 +1000981 continue;
Aneesh Kumar K.Vf6fb9e82014-11-05 12:21:13 +1100982 }
Paul Mackerras6c576e72014-05-26 19:48:39 +1000983
984 /* need to make it temporarily absent so C is stable */
Alexander Graf6f22bd32014-06-11 10:16:06 +0200985 hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
Paul Mackerras6c576e72014-05-26 19:48:39 +1000986 kvmppc_invalidate_hpte(kvm, hptep, i);
Alexander Graf6f22bd32014-06-11 10:16:06 +0200987 v = be64_to_cpu(hptep[0]);
988 r = be64_to_cpu(hptep[1]);
Paul Mackerras6c576e72014-05-26 19:48:39 +1000989 if (r & HPTE_R_C) {
Alexander Graf6f22bd32014-06-11 10:16:06 +0200990 hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000991 if (!(rev[i].guest_rpte & HPTE_R_C)) {
992 rev[i].guest_rpte |= HPTE_R_C;
993 note_hpte_modification(kvm, &rev[i]);
994 }
Paul Mackerras6c576e72014-05-26 19:48:39 +1000995 n = hpte_page_size(v, r);
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +1000996 n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
997 if (n > npages_dirty)
998 npages_dirty = n;
Paul Mackerras6c576e72014-05-26 19:48:39 +1000999 eieio();
Paul Mackerras82ed3612011-12-15 02:03:22 +00001000 }
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +11001001 v &= ~HPTE_V_ABSENT;
Paul Mackerras6c576e72014-05-26 19:48:39 +10001002 v |= HPTE_V_VALID;
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +11001003 __unlock_hpte(hptep, v);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001004 } while ((i = j) != head);
1005
1006 unlock_rmap(rmapp);
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +10001007 return npages_dirty;
Paul Mackerras82ed3612011-12-15 02:03:22 +00001008}
1009
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001010static void harvest_vpa_dirty(struct kvmppc_vpa *vpa,
1011 struct kvm_memory_slot *memslot,
1012 unsigned long *map)
1013{
1014 unsigned long gfn;
1015
1016 if (!vpa->dirty || !vpa->pinned_addr)
1017 return;
1018 gfn = vpa->gpa >> PAGE_SHIFT;
1019 if (gfn < memslot->base_gfn ||
1020 gfn >= memslot->base_gfn + memslot->npages)
1021 return;
1022
1023 vpa->dirty = false;
1024 if (map)
1025 __set_bit_le(gfn - memslot->base_gfn, map);
1026}
1027
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001028long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot,
1029 unsigned long *map)
Paul Mackerras82ed3612011-12-15 02:03:22 +00001030{
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +10001031 unsigned long i, j;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001032 unsigned long *rmapp;
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001033 struct kvm_vcpu *vcpu;
Paul Mackerras82ed3612011-12-15 02:03:22 +00001034
1035 preempt_disable();
Takuya Yoshikawad89cc612012-08-01 18:03:28 +09001036 rmapp = memslot->arch.rmap;
Paul Mackerras82ed3612011-12-15 02:03:22 +00001037 for (i = 0; i < memslot->npages; ++i) {
Alexey Kardashevskiy687414b2014-05-26 19:48:38 +10001038 int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
1039 /*
1040 * Note that if npages > 0 then i must be a multiple of npages,
1041 * since we always put huge-page HPTEs in the rmap chain
1042 * corresponding to their page base address.
1043 */
1044 if (npages && map)
1045 for (j = i; npages; ++j, --npages)
1046 __set_bit_le(j, map);
Paul Mackerras82ed3612011-12-15 02:03:22 +00001047 ++rmapp;
1048 }
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001049
1050 /* Harvest dirty bits from VPA and DTL updates */
1051 /* Note: we never modify the SLB shadow buffer areas */
1052 kvm_for_each_vcpu(i, vcpu, kvm) {
1053 spin_lock(&vcpu->arch.vpa_update_lock);
1054 harvest_vpa_dirty(&vcpu->arch.vpa, memslot, map);
1055 harvest_vpa_dirty(&vcpu->arch.dtl, memslot, map);
1056 spin_unlock(&vcpu->arch.vpa_update_lock);
1057 }
Paul Mackerras82ed3612011-12-15 02:03:22 +00001058 preempt_enable();
1059 return 0;
1060}
1061
Paul Mackerras93e60242011-12-12 12:28:55 +00001062void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
1063 unsigned long *nb_ret)
1064{
1065 struct kvm_memory_slot *memslot;
1066 unsigned long gfn = gpa >> PAGE_SHIFT;
Paul Mackerras342d3db2011-12-12 12:38:05 +00001067 struct page *page, *pages[1];
1068 int npages;
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001069 unsigned long hva, offset;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001070 int srcu_idx;
Paul Mackerras93e60242011-12-12 12:28:55 +00001071
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001072 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerras93e60242011-12-12 12:28:55 +00001073 memslot = gfn_to_memslot(kvm, gfn);
1074 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001075 goto err;
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001076 hva = gfn_to_hva_memslot(memslot, gfn);
1077 npages = get_user_pages_fast(hva, 1, 1, pages);
1078 if (npages < 1)
1079 goto err;
1080 page = pages[0];
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001081 srcu_read_unlock(&kvm->srcu, srcu_idx);
1082
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001083 offset = gpa & (PAGE_SIZE - 1);
Paul Mackerras93e60242011-12-12 12:28:55 +00001084 if (nb_ret)
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001085 *nb_ret = PAGE_SIZE - offset;
Paul Mackerras93e60242011-12-12 12:28:55 +00001086 return page_address(page) + offset;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00001087
1088 err:
1089 srcu_read_unlock(&kvm->srcu, srcu_idx);
1090 return NULL;
Paul Mackerras93e60242011-12-12 12:28:55 +00001091}
1092
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001093void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
1094 bool dirty)
Paul Mackerras93e60242011-12-12 12:28:55 +00001095{
1096 struct page *page = virt_to_page(va);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001097 struct kvm_memory_slot *memslot;
1098 unsigned long gfn;
1099 unsigned long *rmap;
1100 int srcu_idx;
Paul Mackerras93e60242011-12-12 12:28:55 +00001101
Paul Mackerras93e60242011-12-12 12:28:55 +00001102 put_page(page);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001103
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001104 if (!dirty)
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001105 return;
1106
1107 /* We need to mark this page dirty in the rmap chain */
1108 gfn = gpa >> PAGE_SHIFT;
1109 srcu_idx = srcu_read_lock(&kvm->srcu);
1110 memslot = gfn_to_memslot(kvm, gfn);
1111 if (memslot) {
1112 rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
1113 lock_rmap(rmap);
1114 *rmap |= KVMPPC_RMAP_CHANGED;
1115 unlock_rmap(rmap);
1116 }
1117 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerras93e60242011-12-12 12:28:55 +00001118}
1119
Paul Mackerrasa2932922012-11-19 22:57:20 +00001120/*
1121 * Functions for reading and writing the hash table via reads and
1122 * writes on a file descriptor.
1123 *
1124 * Reads return the guest view of the hash table, which has to be
1125 * pieced together from the real hash table and the guest_rpte
1126 * values in the revmap array.
1127 *
1128 * On writes, each HPTE written is considered in turn, and if it
1129 * is valid, it is written to the HPT as if an H_ENTER with the
1130 * exact flag set was done. When the invalid count is non-zero
1131 * in the header written to the stream, the kernel will make
1132 * sure that that many HPTEs are invalid, and invalidate them
1133 * if not.
1134 */
1135
1136struct kvm_htab_ctx {
1137 unsigned long index;
1138 unsigned long flags;
1139 struct kvm *kvm;
1140 int first_pass;
1141};
1142
1143#define HPTE_SIZE (2 * sizeof(unsigned long))
1144
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001145/*
1146 * Returns 1 if this HPT entry has been modified or has pending
1147 * R/C bit changes.
1148 */
Alexander Graf6f22bd32014-06-11 10:16:06 +02001149static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001150{
1151 unsigned long rcbits_unset;
1152
1153 if (revp->guest_rpte & HPTE_GR_MODIFIED)
1154 return 1;
1155
1156 /* Also need to consider changes in reference and changed bits */
1157 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
Alexander Graf6f22bd32014-06-11 10:16:06 +02001158 if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
1159 (be64_to_cpu(hptp[1]) & rcbits_unset))
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001160 return 1;
1161
1162 return 0;
1163}
1164
Alexander Graf6f22bd32014-06-11 10:16:06 +02001165static long record_hpte(unsigned long flags, __be64 *hptp,
Paul Mackerrasa2932922012-11-19 22:57:20 +00001166 unsigned long *hpte, struct revmap_entry *revp,
1167 int want_valid, int first_pass)
1168{
1169 unsigned long v, r;
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001170 unsigned long rcbits_unset;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001171 int ok = 1;
1172 int valid, dirty;
1173
1174 /* Unmodified entries are uninteresting except on the first pass */
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001175 dirty = hpte_dirty(revp, hptp);
Paul Mackerrasa2932922012-11-19 22:57:20 +00001176 if (!first_pass && !dirty)
1177 return 0;
1178
1179 valid = 0;
Alexander Graf6f22bd32014-06-11 10:16:06 +02001180 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
Paul Mackerrasa2932922012-11-19 22:57:20 +00001181 valid = 1;
1182 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
Alexander Graf6f22bd32014-06-11 10:16:06 +02001183 !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
Paul Mackerrasa2932922012-11-19 22:57:20 +00001184 valid = 0;
1185 }
1186 if (valid != want_valid)
1187 return 0;
1188
1189 v = r = 0;
1190 if (valid || dirty) {
1191 /* lock the HPTE so it's stable and read it */
1192 preempt_disable();
1193 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1194 cpu_relax();
Alexander Graf6f22bd32014-06-11 10:16:06 +02001195 v = be64_to_cpu(hptp[0]);
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001196
1197 /* re-evaluate valid and dirty from synchronized HPTE value */
1198 valid = !!(v & HPTE_V_VALID);
1199 dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
1200
1201 /* Harvest R and C into guest view if necessary */
1202 rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
Alexander Graf6f22bd32014-06-11 10:16:06 +02001203 if (valid && (rcbits_unset & be64_to_cpu(hptp[1]))) {
1204 revp->guest_rpte |= (be64_to_cpu(hptp[1]) &
1205 (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001206 dirty = 1;
1207 }
1208
Paul Mackerrasa2932922012-11-19 22:57:20 +00001209 if (v & HPTE_V_ABSENT) {
1210 v &= ~HPTE_V_ABSENT;
1211 v |= HPTE_V_VALID;
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001212 valid = 1;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001213 }
Paul Mackerrasa2932922012-11-19 22:57:20 +00001214 if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
1215 valid = 0;
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001216
1217 r = revp->guest_rpte;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001218 /* only clear modified if this is the right sort of entry */
1219 if (valid == want_valid && dirty) {
1220 r &= ~HPTE_GR_MODIFIED;
1221 revp->guest_rpte = r;
1222 }
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +11001223 unlock_hpte(hptp, be64_to_cpu(hptp[0]));
Paul Mackerrasa2932922012-11-19 22:57:20 +00001224 preempt_enable();
1225 if (!(valid == want_valid && (first_pass || dirty)))
1226 ok = 0;
1227 }
Alexander Graf6f22bd32014-06-11 10:16:06 +02001228 hpte[0] = cpu_to_be64(v);
1229 hpte[1] = cpu_to_be64(r);
Paul Mackerrasa2932922012-11-19 22:57:20 +00001230 return ok;
1231}
1232
1233static ssize_t kvm_htab_read(struct file *file, char __user *buf,
1234 size_t count, loff_t *ppos)
1235{
1236 struct kvm_htab_ctx *ctx = file->private_data;
1237 struct kvm *kvm = ctx->kvm;
1238 struct kvm_get_htab_header hdr;
Alexander Graf6f22bd32014-06-11 10:16:06 +02001239 __be64 *hptp;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001240 struct revmap_entry *revp;
1241 unsigned long i, nb, nw;
1242 unsigned long __user *lbuf;
1243 struct kvm_get_htab_header __user *hptr;
1244 unsigned long flags;
1245 int first_pass;
1246 unsigned long hpte[2];
1247
1248 if (!access_ok(VERIFY_WRITE, buf, count))
1249 return -EFAULT;
1250
1251 first_pass = ctx->first_pass;
1252 flags = ctx->flags;
1253
1254 i = ctx->index;
Alexander Graf6f22bd32014-06-11 10:16:06 +02001255 hptp = (__be64 *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
Paul Mackerrasa2932922012-11-19 22:57:20 +00001256 revp = kvm->arch.revmap + i;
1257 lbuf = (unsigned long __user *)buf;
1258
1259 nb = 0;
1260 while (nb + sizeof(hdr) + HPTE_SIZE < count) {
1261 /* Initialize header */
1262 hptr = (struct kvm_get_htab_header __user *)buf;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001263 hdr.n_valid = 0;
1264 hdr.n_invalid = 0;
1265 nw = nb;
1266 nb += sizeof(hdr);
1267 lbuf = (unsigned long __user *)(buf + sizeof(hdr));
1268
1269 /* Skip uninteresting entries, i.e. clean on not-first pass */
1270 if (!first_pass) {
1271 while (i < kvm->arch.hpt_npte &&
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +00001272 !hpte_dirty(revp, hptp)) {
Paul Mackerrasa2932922012-11-19 22:57:20 +00001273 ++i;
1274 hptp += 2;
1275 ++revp;
1276 }
1277 }
Paul Mackerras05dd85f2012-11-21 23:29:12 +00001278 hdr.index = i;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001279
1280 /* Grab a series of valid entries */
1281 while (i < kvm->arch.hpt_npte &&
1282 hdr.n_valid < 0xffff &&
1283 nb + HPTE_SIZE < count &&
1284 record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
1285 /* valid entry, write it out */
1286 ++hdr.n_valid;
1287 if (__put_user(hpte[0], lbuf) ||
1288 __put_user(hpte[1], lbuf + 1))
1289 return -EFAULT;
1290 nb += HPTE_SIZE;
1291 lbuf += 2;
1292 ++i;
1293 hptp += 2;
1294 ++revp;
1295 }
1296 /* Now skip invalid entries while we can */
1297 while (i < kvm->arch.hpt_npte &&
1298 hdr.n_invalid < 0xffff &&
1299 record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
1300 /* found an invalid entry */
1301 ++hdr.n_invalid;
1302 ++i;
1303 hptp += 2;
1304 ++revp;
1305 }
1306
1307 if (hdr.n_valid || hdr.n_invalid) {
1308 /* write back the header */
1309 if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
1310 return -EFAULT;
1311 nw = nb;
1312 buf = (char __user *)lbuf;
1313 } else {
1314 nb = nw;
1315 }
1316
1317 /* Check if we've wrapped around the hash table */
1318 if (i >= kvm->arch.hpt_npte) {
1319 i = 0;
1320 ctx->first_pass = 0;
1321 break;
1322 }
1323 }
1324
1325 ctx->index = i;
1326
1327 return nb;
1328}
1329
1330static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
1331 size_t count, loff_t *ppos)
1332{
1333 struct kvm_htab_ctx *ctx = file->private_data;
1334 struct kvm *kvm = ctx->kvm;
1335 struct kvm_get_htab_header hdr;
1336 unsigned long i, j;
1337 unsigned long v, r;
1338 unsigned long __user *lbuf;
Alexander Graf6f22bd32014-06-11 10:16:06 +02001339 __be64 *hptp;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001340 unsigned long tmp[2];
1341 ssize_t nb;
1342 long int err, ret;
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11001343 int hpte_setup;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001344
1345 if (!access_ok(VERIFY_READ, buf, count))
1346 return -EFAULT;
1347
1348 /* lock out vcpus from running while we're doing this */
1349 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11001350 hpte_setup = kvm->arch.hpte_setup_done;
1351 if (hpte_setup) {
1352 kvm->arch.hpte_setup_done = 0; /* temporarily */
1353 /* order hpte_setup_done vs. vcpus_running */
Paul Mackerrasa2932922012-11-19 22:57:20 +00001354 smp_mb();
1355 if (atomic_read(&kvm->arch.vcpus_running)) {
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11001356 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001357 mutex_unlock(&kvm->lock);
1358 return -EBUSY;
1359 }
1360 }
1361
1362 err = 0;
1363 for (nb = 0; nb + sizeof(hdr) <= count; ) {
1364 err = -EFAULT;
1365 if (__copy_from_user(&hdr, buf, sizeof(hdr)))
1366 break;
1367
1368 err = 0;
1369 if (nb + hdr.n_valid * HPTE_SIZE > count)
1370 break;
1371
1372 nb += sizeof(hdr);
1373 buf += sizeof(hdr);
1374
1375 err = -EINVAL;
1376 i = hdr.index;
1377 if (i >= kvm->arch.hpt_npte ||
1378 i + hdr.n_valid + hdr.n_invalid > kvm->arch.hpt_npte)
1379 break;
1380
Alexander Graf6f22bd32014-06-11 10:16:06 +02001381 hptp = (__be64 *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
Paul Mackerrasa2932922012-11-19 22:57:20 +00001382 lbuf = (unsigned long __user *)buf;
1383 for (j = 0; j < hdr.n_valid; ++j) {
Cédric Le Goaterffada012014-11-21 00:45:59 +01001384 __be64 hpte_v;
1385 __be64 hpte_r;
1386
Paul Mackerrasa2932922012-11-19 22:57:20 +00001387 err = -EFAULT;
Cédric Le Goaterffada012014-11-21 00:45:59 +01001388 if (__get_user(hpte_v, lbuf) ||
1389 __get_user(hpte_r, lbuf + 1))
Paul Mackerrasa2932922012-11-19 22:57:20 +00001390 goto out;
Cédric Le Goaterffada012014-11-21 00:45:59 +01001391 v = be64_to_cpu(hpte_v);
1392 r = be64_to_cpu(hpte_r);
Paul Mackerrasa2932922012-11-19 22:57:20 +00001393 err = -EINVAL;
1394 if (!(v & HPTE_V_VALID))
1395 goto out;
1396 lbuf += 2;
1397 nb += HPTE_SIZE;
1398
Alexander Graf6f22bd32014-06-11 10:16:06 +02001399 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
Paul Mackerrasa2932922012-11-19 22:57:20 +00001400 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1401 err = -EIO;
1402 ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
1403 tmp);
1404 if (ret != H_SUCCESS) {
1405 pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
1406 "r=%lx\n", ret, i, v, r);
1407 goto out;
1408 }
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11001409 if (!hpte_setup && is_vrma_hpte(v)) {
Aneesh Kumar K.V341acbb32014-06-16 00:17:07 +05301410 unsigned long psize = hpte_base_page_size(v, r);
Paul Mackerrasa2932922012-11-19 22:57:20 +00001411 unsigned long senc = slb_pgsize_encoding(psize);
1412 unsigned long lpcr;
1413
1414 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1415 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001416 lpcr = senc << (LPCR_VRMASD_SH - 4);
1417 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11001418 hpte_setup = 1;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001419 }
1420 ++i;
1421 hptp += 2;
1422 }
1423
1424 for (j = 0; j < hdr.n_invalid; ++j) {
Alexander Graf6f22bd32014-06-11 10:16:06 +02001425 if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
Paul Mackerrasa2932922012-11-19 22:57:20 +00001426 kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
1427 ++i;
1428 hptp += 2;
1429 }
1430 err = 0;
1431 }
1432
1433 out:
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11001434 /* Order HPTE updates vs. hpte_setup_done */
Paul Mackerrasa2932922012-11-19 22:57:20 +00001435 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11001436 kvm->arch.hpte_setup_done = hpte_setup;
Paul Mackerrasa2932922012-11-19 22:57:20 +00001437 mutex_unlock(&kvm->lock);
1438
1439 if (err)
1440 return err;
1441 return nb;
1442}
1443
1444static int kvm_htab_release(struct inode *inode, struct file *filp)
1445{
1446 struct kvm_htab_ctx *ctx = filp->private_data;
1447
1448 filp->private_data = NULL;
1449 if (!(ctx->flags & KVM_GET_HTAB_WRITE))
1450 atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
1451 kvm_put_kvm(ctx->kvm);
1452 kfree(ctx);
1453 return 0;
1454}
1455
Al Viro75ef9de2013-04-04 19:09:41 -04001456static const struct file_operations kvm_htab_fops = {
Paul Mackerrasa2932922012-11-19 22:57:20 +00001457 .read = kvm_htab_read,
1458 .write = kvm_htab_write,
1459 .llseek = default_llseek,
1460 .release = kvm_htab_release,
1461};
1462
1463int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
1464{
1465 int ret;
1466 struct kvm_htab_ctx *ctx;
1467 int rwflag;
1468
1469 /* reject flags we don't recognize */
1470 if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
1471 return -EINVAL;
1472 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1473 if (!ctx)
1474 return -ENOMEM;
1475 kvm_get_kvm(kvm);
1476 ctx->kvm = kvm;
1477 ctx->index = ghf->start_index;
1478 ctx->flags = ghf->flags;
1479 ctx->first_pass = 1;
1480
1481 rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
Yann Droneaud2f84d5e2013-08-24 22:14:08 +02001482 ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
Paul Mackerrasa2932922012-11-19 22:57:20 +00001483 if (ret < 0) {
1484 kvm_put_kvm(kvm);
1485 return ret;
1486 }
1487
1488 if (rwflag == O_RDONLY) {
1489 mutex_lock(&kvm->slots_lock);
1490 atomic_inc(&kvm->arch.hpte_mod_interest);
1491 /* make sure kvmppc_do_h_enter etc. see the increment */
1492 synchronize_srcu_expedited(&kvm->srcu);
1493 mutex_unlock(&kvm->slots_lock);
1494 }
1495
1496 return ret;
1497}
1498
Paul Mackerrase23a8082015-03-28 14:21:01 +11001499struct debugfs_htab_state {
1500 struct kvm *kvm;
1501 struct mutex mutex;
1502 unsigned long hpt_index;
1503 int chars_left;
1504 int buf_index;
1505 char buf[64];
1506};
1507
1508static int debugfs_htab_open(struct inode *inode, struct file *file)
1509{
1510 struct kvm *kvm = inode->i_private;
1511 struct debugfs_htab_state *p;
1512
1513 p = kzalloc(sizeof(*p), GFP_KERNEL);
1514 if (!p)
1515 return -ENOMEM;
1516
1517 kvm_get_kvm(kvm);
1518 p->kvm = kvm;
1519 mutex_init(&p->mutex);
1520 file->private_data = p;
1521
1522 return nonseekable_open(inode, file);
1523}
1524
1525static int debugfs_htab_release(struct inode *inode, struct file *file)
1526{
1527 struct debugfs_htab_state *p = file->private_data;
1528
1529 kvm_put_kvm(p->kvm);
1530 kfree(p);
1531 return 0;
1532}
1533
1534static ssize_t debugfs_htab_read(struct file *file, char __user *buf,
1535 size_t len, loff_t *ppos)
1536{
1537 struct debugfs_htab_state *p = file->private_data;
1538 ssize_t ret, r;
1539 unsigned long i, n;
1540 unsigned long v, hr, gr;
1541 struct kvm *kvm;
1542 __be64 *hptp;
1543
1544 ret = mutex_lock_interruptible(&p->mutex);
1545 if (ret)
1546 return ret;
1547
1548 if (p->chars_left) {
1549 n = p->chars_left;
1550 if (n > len)
1551 n = len;
1552 r = copy_to_user(buf, p->buf + p->buf_index, n);
1553 n -= r;
1554 p->chars_left -= n;
1555 p->buf_index += n;
1556 buf += n;
1557 len -= n;
1558 ret = n;
1559 if (r) {
1560 if (!n)
1561 ret = -EFAULT;
1562 goto out;
1563 }
1564 }
1565
1566 kvm = p->kvm;
1567 i = p->hpt_index;
1568 hptp = (__be64 *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
1569 for (; len != 0 && i < kvm->arch.hpt_npte; ++i, hptp += 2) {
1570 if (!(be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)))
1571 continue;
1572
1573 /* lock the HPTE so it's stable and read it */
1574 preempt_disable();
1575 while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
1576 cpu_relax();
1577 v = be64_to_cpu(hptp[0]) & ~HPTE_V_HVLOCK;
1578 hr = be64_to_cpu(hptp[1]);
1579 gr = kvm->arch.revmap[i].guest_rpte;
1580 unlock_hpte(hptp, v);
1581 preempt_enable();
1582
1583 if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
1584 continue;
1585
1586 n = scnprintf(p->buf, sizeof(p->buf),
1587 "%6lx %.16lx %.16lx %.16lx\n",
1588 i, v, hr, gr);
1589 p->chars_left = n;
1590 if (n > len)
1591 n = len;
1592 r = copy_to_user(buf, p->buf, n);
1593 n -= r;
1594 p->chars_left -= n;
1595 p->buf_index = n;
1596 buf += n;
1597 len -= n;
1598 ret += n;
1599 if (r) {
1600 if (!ret)
1601 ret = -EFAULT;
1602 goto out;
1603 }
1604 }
1605 p->hpt_index = i;
1606
1607 out:
1608 mutex_unlock(&p->mutex);
1609 return ret;
1610}
1611
1612ssize_t debugfs_htab_write(struct file *file, const char __user *buf,
1613 size_t len, loff_t *ppos)
1614{
1615 return -EACCES;
1616}
1617
1618static const struct file_operations debugfs_htab_fops = {
1619 .owner = THIS_MODULE,
1620 .open = debugfs_htab_open,
1621 .release = debugfs_htab_release,
1622 .read = debugfs_htab_read,
1623 .write = debugfs_htab_write,
1624 .llseek = generic_file_llseek,
1625};
1626
1627void kvmppc_mmu_debugfs_init(struct kvm *kvm)
1628{
1629 kvm->arch.htab_dentry = debugfs_create_file("htab", 0400,
1630 kvm->arch.debugfs_dir, kvm,
1631 &debugfs_htab_fops);
1632}
1633
Paul Mackerrasde56a942011-06-29 00:21:34 +00001634void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
1635{
1636 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
1637
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001638 vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001639
1640 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
1641 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
1642
1643 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
1644}