Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License, version 2, as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * You should have received a copy of the GNU General Public License |
| 12 | * along with this program; if not, write to the Free Software |
| 13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 | * |
| 15 | * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
| 16 | */ |
| 17 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/kvm.h> |
| 21 | #include <linux/kvm_host.h> |
| 22 | #include <linux/highmem.h> |
| 23 | #include <linux/gfp.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/hugetlb.h> |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 26 | #include <linux/vmalloc.h> |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 27 | #include <linux/srcu.h> |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 28 | #include <linux/anon_inodes.h> |
| 29 | #include <linux/file.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 30 | |
| 31 | #include <asm/tlbflush.h> |
| 32 | #include <asm/kvm_ppc.h> |
| 33 | #include <asm/kvm_book3s.h> |
| 34 | #include <asm/mmu-hash64.h> |
| 35 | #include <asm/hvcall.h> |
| 36 | #include <asm/synch.h> |
| 37 | #include <asm/ppc-opcode.h> |
| 38 | #include <asm/cputable.h> |
| 39 | |
Aneesh Kumar K.V | 990978e | 2013-07-02 11:15:18 +0530 | [diff] [blame] | 40 | #include "book3s_hv_cma.h" |
| 41 | |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 42 | /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */ |
| 43 | #define MAX_LPID_970 63 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 44 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 45 | /* Power architecture requires HPT is at least 256kB */ |
| 46 | #define PPC_MIN_HPT_ORDER 18 |
| 47 | |
Paul Mackerras | 7ed661b | 2012-11-13 18:31:32 +0000 | [diff] [blame] | 48 | static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags, |
| 49 | long pte_index, unsigned long pteh, |
| 50 | unsigned long ptel, unsigned long *pte_idx_ret); |
Paul Mackerras | a64fd70 | 2012-11-21 23:27:19 +0000 | [diff] [blame] | 51 | static void kvmppc_rmap_reset(struct kvm *kvm); |
Paul Mackerras | 7ed661b | 2012-11-13 18:31:32 +0000 | [diff] [blame] | 52 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 53 | long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 54 | { |
| 55 | unsigned long hpt; |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 56 | struct revmap_entry *rev; |
Aneesh Kumar K.V | fa61a4e3 | 2013-07-02 11:15:16 +0530 | [diff] [blame] | 57 | struct page *page = NULL; |
| 58 | long order = KVM_DEFAULT_HPT_ORDER; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 59 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 60 | if (htab_orderp) { |
| 61 | order = *htab_orderp; |
| 62 | if (order < PPC_MIN_HPT_ORDER) |
| 63 | order = PPC_MIN_HPT_ORDER; |
| 64 | } |
| 65 | |
Aneesh Kumar K.V | fa61a4e3 | 2013-07-02 11:15:16 +0530 | [diff] [blame] | 66 | kvm->arch.hpt_cma_alloc = 0; |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 67 | /* |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 68 | * try first to allocate it from the kernel page allocator. |
Aneesh Kumar K.V | fa61a4e3 | 2013-07-02 11:15:16 +0530 | [diff] [blame] | 69 | * We keep the CMA reserved for failed allocation. |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 70 | */ |
Aneesh Kumar K.V | fa61a4e3 | 2013-07-02 11:15:16 +0530 | [diff] [blame] | 71 | hpt = __get_free_pages(GFP_KERNEL | __GFP_ZERO | __GFP_REPEAT | |
| 72 | __GFP_NOWARN, order - PAGE_SHIFT); |
Alexander Graf | d2a1b48 | 2012-01-16 19:12:11 +0100 | [diff] [blame] | 73 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 74 | /* Next try to allocate from the preallocated pool */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 75 | if (!hpt) { |
Aneesh Kumar K.V | 990978e | 2013-07-02 11:15:18 +0530 | [diff] [blame] | 76 | VM_BUG_ON(order < KVM_CMA_CHUNK_ORDER); |
Aneesh Kumar K.V | fa61a4e3 | 2013-07-02 11:15:16 +0530 | [diff] [blame] | 77 | page = kvm_alloc_hpt(1 << (order - PAGE_SHIFT)); |
| 78 | if (page) { |
| 79 | hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page)); |
| 80 | kvm->arch.hpt_cma_alloc = 1; |
| 81 | } else |
| 82 | --order; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 83 | } |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 84 | |
| 85 | /* Lastly try successively smaller sizes from the page allocator */ |
| 86 | while (!hpt && order > PPC_MIN_HPT_ORDER) { |
| 87 | hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT| |
| 88 | __GFP_NOWARN, order - PAGE_SHIFT); |
| 89 | if (!hpt) |
| 90 | --order; |
| 91 | } |
| 92 | |
| 93 | if (!hpt) |
| 94 | return -ENOMEM; |
| 95 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 96 | kvm->arch.hpt_virt = hpt; |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 97 | kvm->arch.hpt_order = order; |
| 98 | /* HPTEs are 2**4 bytes long */ |
| 99 | kvm->arch.hpt_npte = 1ul << (order - 4); |
| 100 | /* 128 (2**7) bytes in each HPTEG */ |
| 101 | kvm->arch.hpt_mask = (1ul << (order - 7)) - 1; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 102 | |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 103 | /* Allocate reverse map array */ |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 104 | rev = vmalloc(sizeof(struct revmap_entry) * kvm->arch.hpt_npte); |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 105 | if (!rev) { |
| 106 | pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n"); |
| 107 | goto out_freehpt; |
| 108 | } |
| 109 | kvm->arch.revmap = rev; |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 110 | kvm->arch.sdr1 = __pa(hpt) | (order - 18); |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 111 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 112 | pr_info("KVM guest htab at %lx (order %ld), LPID %x\n", |
| 113 | hpt, order, kvm->arch.lpid); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 114 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 115 | if (htab_orderp) |
| 116 | *htab_orderp = order; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 117 | return 0; |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 118 | |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 119 | out_freehpt: |
Aneesh Kumar K.V | fa61a4e3 | 2013-07-02 11:15:16 +0530 | [diff] [blame] | 120 | if (kvm->arch.hpt_cma_alloc) |
| 121 | kvm_release_hpt(page, 1 << (order - PAGE_SHIFT)); |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 122 | else |
| 123 | free_pages(hpt, order - PAGE_SHIFT); |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 124 | return -ENOMEM; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 127 | long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp) |
| 128 | { |
| 129 | long err = -EBUSY; |
| 130 | long order; |
| 131 | |
| 132 | mutex_lock(&kvm->lock); |
| 133 | if (kvm->arch.rma_setup_done) { |
| 134 | kvm->arch.rma_setup_done = 0; |
| 135 | /* order rma_setup_done vs. vcpus_running */ |
| 136 | smp_mb(); |
| 137 | if (atomic_read(&kvm->arch.vcpus_running)) { |
| 138 | kvm->arch.rma_setup_done = 1; |
| 139 | goto out; |
| 140 | } |
| 141 | } |
| 142 | if (kvm->arch.hpt_virt) { |
| 143 | order = kvm->arch.hpt_order; |
| 144 | /* Set the entire HPT to 0, i.e. invalid HPTEs */ |
| 145 | memset((void *)kvm->arch.hpt_virt, 0, 1ul << order); |
| 146 | /* |
Paul Mackerras | a64fd70 | 2012-11-21 23:27:19 +0000 | [diff] [blame] | 147 | * Reset all the reverse-mapping chains for all memslots |
| 148 | */ |
| 149 | kvmppc_rmap_reset(kvm); |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 150 | /* Ensure that each vcpu will flush its TLB on next entry. */ |
| 151 | cpumask_setall(&kvm->arch.need_tlb_flush); |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 152 | *htab_orderp = order; |
| 153 | err = 0; |
| 154 | } else { |
| 155 | err = kvmppc_alloc_hpt(kvm, htab_orderp); |
| 156 | order = *htab_orderp; |
| 157 | } |
| 158 | out: |
| 159 | mutex_unlock(&kvm->lock); |
| 160 | return err; |
| 161 | } |
| 162 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 163 | void kvmppc_free_hpt(struct kvm *kvm) |
| 164 | { |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 165 | kvmppc_free_lpid(kvm->arch.lpid); |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 166 | vfree(kvm->arch.revmap); |
Aneesh Kumar K.V | fa61a4e3 | 2013-07-02 11:15:16 +0530 | [diff] [blame] | 167 | if (kvm->arch.hpt_cma_alloc) |
| 168 | kvm_release_hpt(virt_to_page(kvm->arch.hpt_virt), |
| 169 | 1 << (kvm->arch.hpt_order - PAGE_SHIFT)); |
Alexander Graf | d2a1b48 | 2012-01-16 19:12:11 +0100 | [diff] [blame] | 170 | else |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 171 | free_pages(kvm->arch.hpt_virt, |
| 172 | kvm->arch.hpt_order - PAGE_SHIFT); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 175 | /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */ |
| 176 | static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 177 | { |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 178 | return (pgsize > 0x1000) ? HPTE_V_LARGE : 0; |
| 179 | } |
| 180 | |
| 181 | /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */ |
| 182 | static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize) |
| 183 | { |
| 184 | return (pgsize == 0x10000) ? 0x1000 : 0; |
| 185 | } |
| 186 | |
| 187 | void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot, |
| 188 | unsigned long porder) |
| 189 | { |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 190 | unsigned long i; |
Paul Mackerras | b2b2f16 | 2011-12-12 12:28:21 +0000 | [diff] [blame] | 191 | unsigned long npages; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 192 | unsigned long hp_v, hp_r; |
| 193 | unsigned long addr, hash; |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 194 | unsigned long psize; |
| 195 | unsigned long hp0, hp1; |
Paul Mackerras | 7ed661b | 2012-11-13 18:31:32 +0000 | [diff] [blame] | 196 | unsigned long idx_ret; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 197 | long ret; |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 198 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 199 | |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 200 | psize = 1ul << porder; |
| 201 | npages = memslot->npages >> (porder - PAGE_SHIFT); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 202 | |
| 203 | /* VRMA can't be > 1TB */ |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 204 | if (npages > 1ul << (40 - porder)) |
| 205 | npages = 1ul << (40 - porder); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 206 | /* Can't use more than 1 HPTE per HPTEG */ |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 207 | if (npages > kvm->arch.hpt_mask + 1) |
| 208 | npages = kvm->arch.hpt_mask + 1; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 209 | |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 210 | hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) | |
| 211 | HPTE_V_BOLTED | hpte0_pgsize_encoding(psize); |
| 212 | hp1 = hpte1_pgsize_encoding(psize) | |
| 213 | HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX; |
| 214 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 215 | for (i = 0; i < npages; ++i) { |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 216 | addr = i << porder; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 217 | /* can't use hpt_hash since va > 64 bits */ |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 218 | hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & kvm->arch.hpt_mask; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 219 | /* |
| 220 | * We assume that the hash table is empty and no |
| 221 | * vcpus are using it at this stage. Since we create |
| 222 | * at most one HPTE per HPTEG, we just assume entry 7 |
| 223 | * is available and use it. |
| 224 | */ |
Paul Mackerras | 8936dda | 2011-12-12 12:27:39 +0000 | [diff] [blame] | 225 | hash = (hash << 3) + 7; |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 226 | hp_v = hp0 | ((addr >> 16) & ~0x7fUL); |
| 227 | hp_r = hp1 | addr; |
Paul Mackerras | 7ed661b | 2012-11-13 18:31:32 +0000 | [diff] [blame] | 228 | ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r, |
| 229 | &idx_ret); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 230 | if (ret != H_SUCCESS) { |
| 231 | pr_err("KVM: map_vrma at %lx failed, ret=%ld\n", |
| 232 | addr, ret); |
| 233 | break; |
| 234 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | int kvmppc_mmu_hv_init(void) |
| 239 | { |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 240 | unsigned long host_lpid, rsvd_lpid; |
| 241 | |
| 242 | if (!cpu_has_feature(CPU_FTR_HVMODE)) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 243 | return -EINVAL; |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 244 | |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 245 | /* POWER7 has 10-bit LPIDs, PPC970 and e500mc have 6-bit LPIDs */ |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 246 | if (cpu_has_feature(CPU_FTR_ARCH_206)) { |
| 247 | host_lpid = mfspr(SPRN_LPID); /* POWER7 */ |
| 248 | rsvd_lpid = LPID_RSVD; |
| 249 | } else { |
| 250 | host_lpid = 0; /* PPC970 */ |
| 251 | rsvd_lpid = MAX_LPID_970; |
| 252 | } |
| 253 | |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 254 | kvmppc_init_lpid(rsvd_lpid + 1); |
| 255 | |
| 256 | kvmppc_claim_lpid(host_lpid); |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 257 | /* rsvd_lpid is reserved for use in partition switching */ |
Scott Wood | 043cc4d | 2011-12-20 15:34:20 +0000 | [diff] [blame] | 258 | kvmppc_claim_lpid(rsvd_lpid); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) |
| 264 | { |
| 265 | } |
| 266 | |
| 267 | static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu) |
| 268 | { |
| 269 | kvmppc_set_msr(vcpu, MSR_SF | MSR_ME); |
| 270 | } |
| 271 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 272 | /* |
| 273 | * This is called to get a reference to a guest page if there isn't |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 274 | * one already in the memslot->arch.slot_phys[] array. |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 275 | */ |
| 276 | static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn, |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 277 | struct kvm_memory_slot *memslot, |
| 278 | unsigned long psize) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 279 | { |
| 280 | unsigned long start; |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 281 | long np, err; |
| 282 | struct page *page, *hpage, *pages[1]; |
| 283 | unsigned long s, pgsize; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 284 | unsigned long *physp; |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 285 | unsigned int is_io, got, pgorder; |
| 286 | struct vm_area_struct *vma; |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 287 | unsigned long pfn, i, npages; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 288 | |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 289 | physp = memslot->arch.slot_phys; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 290 | if (!physp) |
| 291 | return -EINVAL; |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 292 | if (physp[gfn - memslot->base_gfn]) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 293 | return 0; |
| 294 | |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 295 | is_io = 0; |
| 296 | got = 0; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 297 | page = NULL; |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 298 | pgsize = psize; |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 299 | err = -EINVAL; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 300 | start = gfn_to_hva_memslot(memslot, gfn); |
| 301 | |
| 302 | /* Instantiate and get the page we want access to */ |
| 303 | np = get_user_pages_fast(start, 1, 1, pages); |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 304 | if (np != 1) { |
| 305 | /* Look up the vma for the page */ |
| 306 | down_read(¤t->mm->mmap_sem); |
| 307 | vma = find_vma(current->mm, start); |
| 308 | if (!vma || vma->vm_start > start || |
| 309 | start + psize > vma->vm_end || |
| 310 | !(vma->vm_flags & VM_PFNMAP)) |
| 311 | goto up_err; |
| 312 | is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot)); |
| 313 | pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); |
| 314 | /* check alignment of pfn vs. requested page size */ |
| 315 | if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1))) |
| 316 | goto up_err; |
| 317 | up_read(¤t->mm->mmap_sem); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 318 | |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 319 | } else { |
| 320 | page = pages[0]; |
| 321 | got = KVMPPC_GOT_PAGE; |
| 322 | |
| 323 | /* See if this is a large page */ |
| 324 | s = PAGE_SIZE; |
| 325 | if (PageHuge(page)) { |
| 326 | hpage = compound_head(page); |
| 327 | s <<= compound_order(hpage); |
| 328 | /* Get the whole large page if slot alignment is ok */ |
| 329 | if (s > psize && slot_is_aligned(memslot, s) && |
| 330 | !(memslot->userspace_addr & (s - 1))) { |
| 331 | start &= ~(s - 1); |
| 332 | pgsize = s; |
David Gibson | de6c0b0 | 2012-05-08 20:24:08 +1000 | [diff] [blame] | 333 | get_page(hpage); |
| 334 | put_page(page); |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 335 | page = hpage; |
| 336 | } |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 337 | } |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 338 | if (s < psize) |
| 339 | goto out; |
| 340 | pfn = page_to_pfn(page); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 341 | } |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 342 | |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 343 | npages = pgsize >> PAGE_SHIFT; |
| 344 | pgorder = __ilog2(npages); |
| 345 | physp += (gfn - memslot->base_gfn) & ~(npages - 1); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 346 | spin_lock(&kvm->arch.slot_phys_lock); |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 347 | for (i = 0; i < npages; ++i) { |
| 348 | if (!physp[i]) { |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 349 | physp[i] = ((pfn + i) << PAGE_SHIFT) + |
| 350 | got + is_io + pgorder; |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 351 | got = 0; |
| 352 | } |
| 353 | } |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 354 | spin_unlock(&kvm->arch.slot_phys_lock); |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 355 | err = 0; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 356 | |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 357 | out: |
David Gibson | de6c0b0 | 2012-05-08 20:24:08 +1000 | [diff] [blame] | 358 | if (got) |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 359 | put_page(page); |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 360 | return err; |
Paul Mackerras | 9d0ef5ea | 2011-12-12 12:32:27 +0000 | [diff] [blame] | 361 | |
| 362 | up_err: |
| 363 | up_read(¤t->mm->mmap_sem); |
| 364 | return err; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Paul Mackerras | 7ed661b | 2012-11-13 18:31:32 +0000 | [diff] [blame] | 367 | long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags, |
| 368 | long pte_index, unsigned long pteh, |
| 369 | unsigned long ptel, unsigned long *pte_idx_ret) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 370 | { |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 371 | unsigned long psize, gpa, gfn; |
| 372 | struct kvm_memory_slot *memslot; |
| 373 | long ret; |
| 374 | |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 375 | if (kvm->arch.using_mmu_notifiers) |
| 376 | goto do_insert; |
| 377 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 378 | psize = hpte_page_size(pteh, ptel); |
| 379 | if (!psize) |
| 380 | return H_PARAMETER; |
| 381 | |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 382 | pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID); |
| 383 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 384 | /* Find the memslot (if any) for this address */ |
| 385 | gpa = (ptel & HPTE_R_RPN) & ~(psize - 1); |
| 386 | gfn = gpa >> PAGE_SHIFT; |
| 387 | memslot = gfn_to_memslot(kvm, gfn); |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 388 | if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) { |
| 389 | if (!slot_is_aligned(memslot, psize)) |
| 390 | return H_PARAMETER; |
| 391 | if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0) |
| 392 | return H_PARAMETER; |
| 393 | } |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 394 | |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 395 | do_insert: |
| 396 | /* Protect linux PTE lookup from page table destruction */ |
| 397 | rcu_read_lock_sched(); /* this disables preemption too */ |
Paul Mackerras | 7ed661b | 2012-11-13 18:31:32 +0000 | [diff] [blame] | 398 | ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel, |
| 399 | current->mm->pgd, false, pte_idx_ret); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 400 | rcu_read_unlock_sched(); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 401 | if (ret == H_TOO_HARD) { |
| 402 | /* this can't happen */ |
| 403 | pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n"); |
| 404 | ret = H_RESOURCE; /* or something */ |
| 405 | } |
| 406 | return ret; |
| 407 | |
| 408 | } |
| 409 | |
Paul Mackerras | 7ed661b | 2012-11-13 18:31:32 +0000 | [diff] [blame] | 410 | /* |
| 411 | * We come here on a H_ENTER call from the guest when we are not |
| 412 | * using mmu notifiers and we don't have the requested page pinned |
| 413 | * already. |
| 414 | */ |
| 415 | long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags, |
| 416 | long pte_index, unsigned long pteh, |
| 417 | unsigned long ptel) |
| 418 | { |
| 419 | return kvmppc_virtmode_do_h_enter(vcpu->kvm, flags, pte_index, |
| 420 | pteh, ptel, &vcpu->arch.gpr[4]); |
| 421 | } |
| 422 | |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 423 | static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu, |
| 424 | gva_t eaddr) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 425 | { |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 426 | u64 mask; |
| 427 | int i; |
| 428 | |
| 429 | for (i = 0; i < vcpu->arch.slb_nr; i++) { |
| 430 | if (!(vcpu->arch.slb[i].orige & SLB_ESID_V)) |
| 431 | continue; |
| 432 | |
| 433 | if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T) |
| 434 | mask = ESID_MASK_1T; |
| 435 | else |
| 436 | mask = ESID_MASK; |
| 437 | |
| 438 | if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0) |
| 439 | return &vcpu->arch.slb[i]; |
| 440 | } |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r, |
| 445 | unsigned long ea) |
| 446 | { |
| 447 | unsigned long ra_mask; |
| 448 | |
| 449 | ra_mask = hpte_page_size(v, r) - 1; |
| 450 | return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask); |
| 451 | } |
| 452 | |
| 453 | static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, |
| 454 | struct kvmppc_pte *gpte, bool data) |
| 455 | { |
| 456 | struct kvm *kvm = vcpu->kvm; |
| 457 | struct kvmppc_slb *slbe; |
| 458 | unsigned long slb_v; |
| 459 | unsigned long pp, key; |
| 460 | unsigned long v, gr; |
| 461 | unsigned long *hptep; |
| 462 | int index; |
| 463 | int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR); |
| 464 | |
| 465 | /* Get SLB entry */ |
| 466 | if (virtmode) { |
| 467 | slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr); |
| 468 | if (!slbe) |
| 469 | return -EINVAL; |
| 470 | slb_v = slbe->origv; |
| 471 | } else { |
| 472 | /* real mode access */ |
| 473 | slb_v = vcpu->kvm->arch.vrma_slb_v; |
| 474 | } |
| 475 | |
| 476 | /* Find the HPTE in the hash table */ |
| 477 | index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v, |
| 478 | HPTE_V_VALID | HPTE_V_ABSENT); |
| 479 | if (index < 0) |
| 480 | return -ENOENT; |
| 481 | hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4)); |
| 482 | v = hptep[0] & ~HPTE_V_HVLOCK; |
| 483 | gr = kvm->arch.revmap[index].guest_rpte; |
| 484 | |
| 485 | /* Unlock the HPTE */ |
| 486 | asm volatile("lwsync" : : : "memory"); |
| 487 | hptep[0] = v; |
| 488 | |
| 489 | gpte->eaddr = eaddr; |
| 490 | gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff); |
| 491 | |
| 492 | /* Get PP bits and key for permission check */ |
| 493 | pp = gr & (HPTE_R_PP0 | HPTE_R_PP); |
| 494 | key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS; |
| 495 | key &= slb_v; |
| 496 | |
| 497 | /* Calculate permissions */ |
| 498 | gpte->may_read = hpte_read_permission(pp, key); |
| 499 | gpte->may_write = hpte_write_permission(pp, key); |
| 500 | gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G)); |
| 501 | |
| 502 | /* Storage key permission check for POWER7 */ |
| 503 | if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) { |
| 504 | int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr); |
| 505 | if (amrfield & 1) |
| 506 | gpte->may_read = 0; |
| 507 | if (amrfield & 2) |
| 508 | gpte->may_write = 0; |
| 509 | } |
| 510 | |
| 511 | /* Get the guest physical address */ |
| 512 | gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Quick test for whether an instruction is a load or a store. |
| 518 | * If the instruction is a load or a store, then this will indicate |
| 519 | * which it is, at least on server processors. (Embedded processors |
| 520 | * have some external PID instructions that don't follow the rule |
| 521 | * embodied here.) If the instruction isn't a load or store, then |
| 522 | * this doesn't return anything useful. |
| 523 | */ |
| 524 | static int instruction_is_store(unsigned int instr) |
| 525 | { |
| 526 | unsigned int mask; |
| 527 | |
| 528 | mask = 0x10000000; |
| 529 | if ((instr & 0xfc000000) == 0x7c000000) |
| 530 | mask = 0x100; /* major opcode 31 */ |
| 531 | return (instr & mask) != 0; |
| 532 | } |
| 533 | |
| 534 | static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu, |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 535 | unsigned long gpa, gva_t ea, int is_store) |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 536 | { |
| 537 | int ret; |
| 538 | u32 last_inst; |
| 539 | unsigned long srr0 = kvmppc_get_pc(vcpu); |
| 540 | |
| 541 | /* We try to load the last instruction. We don't let |
| 542 | * emulate_instruction do it as it doesn't check what |
| 543 | * kvmppc_ld returns. |
| 544 | * If we fail, we just return to the guest and try executing it again. |
| 545 | */ |
| 546 | if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) { |
| 547 | ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false); |
| 548 | if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED) |
| 549 | return RESUME_GUEST; |
| 550 | vcpu->arch.last_inst = last_inst; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * WARNING: We do not know for sure whether the instruction we just |
| 555 | * read from memory is the same that caused the fault in the first |
| 556 | * place. If the instruction we read is neither an load or a store, |
| 557 | * then it can't access memory, so we don't need to worry about |
| 558 | * enforcing access permissions. So, assuming it is a load or |
| 559 | * store, we just check that its direction (load or store) is |
| 560 | * consistent with the original fault, since that's what we |
| 561 | * checked the access permissions against. If there is a mismatch |
| 562 | * we just return and retry the instruction. |
| 563 | */ |
| 564 | |
| 565 | if (instruction_is_store(vcpu->arch.last_inst) != !!is_store) |
| 566 | return RESUME_GUEST; |
| 567 | |
| 568 | /* |
| 569 | * Emulated accesses are emulated by looking at the hash for |
| 570 | * translation once, then performing the access later. The |
| 571 | * translation could be invalidated in the meantime in which |
| 572 | * point performing the subsequent memory access on the old |
| 573 | * physical address could possibly be a security hole for the |
| 574 | * guest (but not the host). |
| 575 | * |
| 576 | * This is less of an issue for MMIO stores since they aren't |
| 577 | * globally visible. It could be an issue for MMIO loads to |
| 578 | * a certain extent but we'll ignore it for now. |
| 579 | */ |
| 580 | |
| 581 | vcpu->arch.paddr_accessed = gpa; |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 582 | vcpu->arch.vaddr_accessed = ea; |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 583 | return kvmppc_emulate_mmio(run, vcpu); |
| 584 | } |
| 585 | |
| 586 | int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 587 | unsigned long ea, unsigned long dsisr) |
| 588 | { |
| 589 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 590 | unsigned long *hptep, hpte[3], r; |
| 591 | unsigned long mmu_seq, psize, pte_size; |
Paul Mackerras | 70bddfe | 2012-09-20 19:39:21 +0000 | [diff] [blame] | 592 | unsigned long gpa, gfn, hva, pfn; |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 593 | struct kvm_memory_slot *memslot; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 594 | unsigned long *rmap; |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 595 | struct revmap_entry *rev; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 596 | struct page *page, *pages[1]; |
| 597 | long index, ret, npages; |
| 598 | unsigned long is_io; |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 599 | unsigned int writing, write_ok; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 600 | struct vm_area_struct *vma; |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 601 | unsigned long rcbits; |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 602 | |
| 603 | /* |
| 604 | * Real-mode code has already searched the HPT and found the |
| 605 | * entry we're interested in. Lock the entry and check that |
| 606 | * it hasn't changed. If it has, just return and re-execute the |
| 607 | * instruction. |
| 608 | */ |
| 609 | if (ea != vcpu->arch.pgfault_addr) |
| 610 | return RESUME_GUEST; |
| 611 | index = vcpu->arch.pgfault_index; |
| 612 | hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4)); |
| 613 | rev = &kvm->arch.revmap[index]; |
| 614 | preempt_disable(); |
| 615 | while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) |
| 616 | cpu_relax(); |
| 617 | hpte[0] = hptep[0] & ~HPTE_V_HVLOCK; |
| 618 | hpte[1] = hptep[1]; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 619 | hpte[2] = r = rev->guest_rpte; |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 620 | asm volatile("lwsync" : : : "memory"); |
| 621 | hptep[0] = hpte[0]; |
| 622 | preempt_enable(); |
| 623 | |
| 624 | if (hpte[0] != vcpu->arch.pgfault_hpte[0] || |
| 625 | hpte[1] != vcpu->arch.pgfault_hpte[1]) |
| 626 | return RESUME_GUEST; |
| 627 | |
| 628 | /* Translate the logical address and get the page */ |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 629 | psize = hpte_page_size(hpte[0], r); |
Paul Mackerras | 70bddfe | 2012-09-20 19:39:21 +0000 | [diff] [blame] | 630 | gpa = (r & HPTE_R_RPN & ~(psize - 1)) | (ea & (psize - 1)); |
| 631 | gfn = gpa >> PAGE_SHIFT; |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 632 | memslot = gfn_to_memslot(kvm, gfn); |
| 633 | |
| 634 | /* No memslot means it's an emulated MMIO region */ |
Paul Mackerras | 70bddfe | 2012-09-20 19:39:21 +0000 | [diff] [blame] | 635 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 636 | return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea, |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 637 | dsisr & DSISR_ISSTORE); |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 638 | |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 639 | if (!kvm->arch.using_mmu_notifiers) |
| 640 | return -EFAULT; /* should never get here */ |
| 641 | |
| 642 | /* used to check for invalidations in progress */ |
| 643 | mmu_seq = kvm->mmu_notifier_seq; |
| 644 | smp_rmb(); |
| 645 | |
| 646 | is_io = 0; |
| 647 | pfn = 0; |
| 648 | page = NULL; |
| 649 | pte_size = PAGE_SIZE; |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 650 | writing = (dsisr & DSISR_ISSTORE) != 0; |
| 651 | /* If writing != 0, then the HPTE must allow writing, if we get here */ |
| 652 | write_ok = writing; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 653 | hva = gfn_to_hva_memslot(memslot, gfn); |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 654 | npages = get_user_pages_fast(hva, 1, writing, pages); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 655 | if (npages < 1) { |
| 656 | /* Check if it's an I/O mapping */ |
| 657 | down_read(¤t->mm->mmap_sem); |
| 658 | vma = find_vma(current->mm, hva); |
| 659 | if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end && |
| 660 | (vma->vm_flags & VM_PFNMAP)) { |
| 661 | pfn = vma->vm_pgoff + |
| 662 | ((hva - vma->vm_start) >> PAGE_SHIFT); |
| 663 | pte_size = psize; |
| 664 | is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot)); |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 665 | write_ok = vma->vm_flags & VM_WRITE; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 666 | } |
| 667 | up_read(¤t->mm->mmap_sem); |
| 668 | if (!pfn) |
| 669 | return -EFAULT; |
| 670 | } else { |
| 671 | page = pages[0]; |
| 672 | if (PageHuge(page)) { |
| 673 | page = compound_head(page); |
| 674 | pte_size <<= compound_order(page); |
| 675 | } |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 676 | /* if the guest wants write access, see if that is OK */ |
| 677 | if (!writing && hpte_is_writable(r)) { |
Aneesh Kumar K.V | db7cb5b | 2013-06-20 14:30:19 +0530 | [diff] [blame] | 678 | unsigned int hugepage_shift; |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 679 | pte_t *ptep, pte; |
| 680 | |
| 681 | /* |
| 682 | * We need to protect against page table destruction |
| 683 | * while looking up and updating the pte. |
| 684 | */ |
| 685 | rcu_read_lock_sched(); |
| 686 | ptep = find_linux_pte_or_hugepte(current->mm->pgd, |
Aneesh Kumar K.V | db7cb5b | 2013-06-20 14:30:19 +0530 | [diff] [blame] | 687 | hva, &hugepage_shift); |
| 688 | if (ptep) { |
| 689 | pte = kvmppc_read_update_linux_pte(ptep, 1, |
| 690 | hugepage_shift); |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 691 | if (pte_write(pte)) |
| 692 | write_ok = 1; |
| 693 | } |
| 694 | rcu_read_unlock_sched(); |
| 695 | } |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 696 | pfn = page_to_pfn(page); |
| 697 | } |
| 698 | |
| 699 | ret = -EFAULT; |
| 700 | if (psize > pte_size) |
| 701 | goto out_put; |
| 702 | |
| 703 | /* Check WIMG vs. the actual page we're accessing */ |
| 704 | if (!hpte_cache_flags_ok(r, is_io)) { |
| 705 | if (is_io) |
| 706 | return -EFAULT; |
| 707 | /* |
| 708 | * Allow guest to map emulated device memory as |
| 709 | * uncacheable, but actually make it cacheable. |
| 710 | */ |
| 711 | r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M; |
| 712 | } |
| 713 | |
| 714 | /* Set the HPTE to point to pfn */ |
| 715 | r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT); |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 716 | if (hpte_is_writable(r) && !write_ok) |
| 717 | r = hpte_make_readonly(r); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 718 | ret = RESUME_GUEST; |
| 719 | preempt_disable(); |
| 720 | while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) |
| 721 | cpu_relax(); |
| 722 | if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] || |
| 723 | rev->guest_rpte != hpte[2]) |
| 724 | /* HPTE has been changed under us; let the guest retry */ |
| 725 | goto out_unlock; |
| 726 | hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID; |
| 727 | |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 728 | rmap = &memslot->arch.rmap[gfn - memslot->base_gfn]; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 729 | lock_rmap(rmap); |
| 730 | |
| 731 | /* Check if we might have been invalidated; let the guest retry if so */ |
| 732 | ret = RESUME_GUEST; |
Christoffer Dall | 8ca40a7 | 2012-10-14 23:10:18 -0400 | [diff] [blame] | 733 | if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) { |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 734 | unlock_rmap(rmap); |
| 735 | goto out_unlock; |
| 736 | } |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 737 | |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 738 | /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */ |
| 739 | rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT; |
| 740 | r &= rcbits | ~(HPTE_R_R | HPTE_R_C); |
| 741 | |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 742 | if (hptep[0] & HPTE_V_VALID) { |
| 743 | /* HPTE was previously valid, so we need to invalidate it */ |
| 744 | unlock_rmap(rmap); |
| 745 | hptep[0] |= HPTE_V_ABSENT; |
| 746 | kvmppc_invalidate_hpte(kvm, hptep, index); |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 747 | /* don't lose previous R and C bits */ |
| 748 | r |= hptep[1] & (HPTE_R_R | HPTE_R_C); |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 749 | } else { |
| 750 | kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0); |
| 751 | } |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 752 | |
| 753 | hptep[1] = r; |
| 754 | eieio(); |
| 755 | hptep[0] = hpte[0]; |
| 756 | asm volatile("ptesync" : : : "memory"); |
| 757 | preempt_enable(); |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 758 | if (page && hpte_is_writable(r)) |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 759 | SetPageDirty(page); |
| 760 | |
| 761 | out_put: |
David Gibson | de6c0b0 | 2012-05-08 20:24:08 +1000 | [diff] [blame] | 762 | if (page) { |
| 763 | /* |
| 764 | * We drop pages[0] here, not page because page might |
| 765 | * have been set to the head page of a compound, but |
| 766 | * we have to drop the reference on the correct tail |
| 767 | * page to match the get inside gup() |
| 768 | */ |
| 769 | put_page(pages[0]); |
| 770 | } |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 771 | return ret; |
| 772 | |
| 773 | out_unlock: |
| 774 | hptep[0] &= ~HPTE_V_HVLOCK; |
| 775 | preempt_enable(); |
| 776 | goto out_put; |
| 777 | } |
| 778 | |
Paul Mackerras | a64fd70 | 2012-11-21 23:27:19 +0000 | [diff] [blame] | 779 | static void kvmppc_rmap_reset(struct kvm *kvm) |
| 780 | { |
| 781 | struct kvm_memslots *slots; |
| 782 | struct kvm_memory_slot *memslot; |
| 783 | int srcu_idx; |
| 784 | |
| 785 | srcu_idx = srcu_read_lock(&kvm->srcu); |
| 786 | slots = kvm->memslots; |
| 787 | kvm_for_each_memslot(memslot, slots) { |
| 788 | /* |
| 789 | * This assumes it is acceptable to lose reference and |
| 790 | * change bits across a reset. |
| 791 | */ |
| 792 | memset(memslot->arch.rmap, 0, |
| 793 | memslot->npages * sizeof(*memslot->arch.rmap)); |
| 794 | } |
| 795 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
| 796 | } |
| 797 | |
Takuya Yoshikawa | 84504ef | 2012-07-02 17:55:48 +0900 | [diff] [blame] | 798 | static int kvm_handle_hva_range(struct kvm *kvm, |
| 799 | unsigned long start, |
| 800 | unsigned long end, |
| 801 | int (*handler)(struct kvm *kvm, |
| 802 | unsigned long *rmapp, |
| 803 | unsigned long gfn)) |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 804 | { |
| 805 | int ret; |
| 806 | int retval = 0; |
| 807 | struct kvm_memslots *slots; |
| 808 | struct kvm_memory_slot *memslot; |
| 809 | |
| 810 | slots = kvm_memslots(kvm); |
| 811 | kvm_for_each_memslot(memslot, slots) { |
Takuya Yoshikawa | 84504ef | 2012-07-02 17:55:48 +0900 | [diff] [blame] | 812 | unsigned long hva_start, hva_end; |
| 813 | gfn_t gfn, gfn_end; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 814 | |
Takuya Yoshikawa | 84504ef | 2012-07-02 17:55:48 +0900 | [diff] [blame] | 815 | hva_start = max(start, memslot->userspace_addr); |
| 816 | hva_end = min(end, memslot->userspace_addr + |
| 817 | (memslot->npages << PAGE_SHIFT)); |
| 818 | if (hva_start >= hva_end) |
| 819 | continue; |
| 820 | /* |
| 821 | * {gfn(page) | page intersects with [hva_start, hva_end)} = |
| 822 | * {gfn, gfn+1, ..., gfn_end-1}. |
| 823 | */ |
| 824 | gfn = hva_to_gfn_memslot(hva_start, memslot); |
| 825 | gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot); |
| 826 | |
| 827 | for (; gfn < gfn_end; ++gfn) { |
Takuya Yoshikawa | d19a748 | 2012-07-02 17:54:30 +0900 | [diff] [blame] | 828 | gfn_t gfn_offset = gfn - memslot->base_gfn; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 829 | |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 830 | ret = handler(kvm, &memslot->arch.rmap[gfn_offset], gfn); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 831 | retval |= ret; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | return retval; |
| 836 | } |
| 837 | |
Takuya Yoshikawa | 84504ef | 2012-07-02 17:55:48 +0900 | [diff] [blame] | 838 | static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, |
| 839 | int (*handler)(struct kvm *kvm, unsigned long *rmapp, |
| 840 | unsigned long gfn)) |
| 841 | { |
| 842 | return kvm_handle_hva_range(kvm, hva, hva + 1, handler); |
| 843 | } |
| 844 | |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 845 | static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp, |
| 846 | unsigned long gfn) |
| 847 | { |
| 848 | struct revmap_entry *rev = kvm->arch.revmap; |
| 849 | unsigned long h, i, j; |
| 850 | unsigned long *hptep; |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 851 | unsigned long ptel, psize, rcbits; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 852 | |
| 853 | for (;;) { |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 854 | lock_rmap(rmapp); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 855 | if (!(*rmapp & KVMPPC_RMAP_PRESENT)) { |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 856 | unlock_rmap(rmapp); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 857 | break; |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * To avoid an ABBA deadlock with the HPTE lock bit, |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 862 | * we can't spin on the HPTE lock while holding the |
| 863 | * rmap chain lock. |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 864 | */ |
| 865 | i = *rmapp & KVMPPC_RMAP_INDEX; |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 866 | hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4)); |
| 867 | if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) { |
| 868 | /* unlock rmap before spinning on the HPTE lock */ |
| 869 | unlock_rmap(rmapp); |
| 870 | while (hptep[0] & HPTE_V_HVLOCK) |
| 871 | cpu_relax(); |
| 872 | continue; |
| 873 | } |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 874 | j = rev[i].forw; |
| 875 | if (j == i) { |
| 876 | /* chain is now empty */ |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 877 | *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 878 | } else { |
| 879 | /* remove i from chain */ |
| 880 | h = rev[i].back; |
| 881 | rev[h].forw = j; |
| 882 | rev[j].back = h; |
| 883 | rev[i].forw = rev[i].back = i; |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 884 | *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 885 | } |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 886 | |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 887 | /* Now check and modify the HPTE */ |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 888 | ptel = rev[i].guest_rpte; |
| 889 | psize = hpte_page_size(hptep[0], ptel); |
| 890 | if ((hptep[0] & HPTE_V_VALID) && |
| 891 | hpte_rpn(ptel, psize) == gfn) { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 892 | if (kvm->arch.using_mmu_notifiers) |
| 893 | hptep[0] |= HPTE_V_ABSENT; |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 894 | kvmppc_invalidate_hpte(kvm, hptep, i); |
| 895 | /* Harvest R and C */ |
| 896 | rcbits = hptep[1] & (HPTE_R_R | HPTE_R_C); |
| 897 | *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT; |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 898 | if (rcbits & ~rev[i].guest_rpte) { |
| 899 | rev[i].guest_rpte = ptel | rcbits; |
| 900 | note_hpte_modification(kvm, &rev[i]); |
| 901 | } |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 902 | } |
Paul Mackerras | bad3b50 | 2011-12-15 02:02:02 +0000 | [diff] [blame] | 903 | unlock_rmap(rmapp); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 904 | hptep[0] &= ~HPTE_V_HVLOCK; |
| 905 | } |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) |
| 910 | { |
| 911 | if (kvm->arch.using_mmu_notifiers) |
| 912 | kvm_handle_hva(kvm, hva, kvm_unmap_rmapp); |
| 913 | return 0; |
| 914 | } |
| 915 | |
Takuya Yoshikawa | b3ae209 | 2012-07-02 17:56:33 +0900 | [diff] [blame] | 916 | int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end) |
| 917 | { |
| 918 | if (kvm->arch.using_mmu_notifiers) |
| 919 | kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp); |
| 920 | return 0; |
| 921 | } |
| 922 | |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 923 | void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot) |
| 924 | { |
| 925 | unsigned long *rmapp; |
| 926 | unsigned long gfn; |
| 927 | unsigned long n; |
| 928 | |
| 929 | rmapp = memslot->arch.rmap; |
| 930 | gfn = memslot->base_gfn; |
| 931 | for (n = memslot->npages; n; --n) { |
| 932 | /* |
| 933 | * Testing the present bit without locking is OK because |
| 934 | * the memslot has been marked invalid already, and hence |
| 935 | * no new HPTEs referencing this page can be created, |
| 936 | * thus the present bit can't go from 0 to 1. |
| 937 | */ |
| 938 | if (*rmapp & KVMPPC_RMAP_PRESENT) |
| 939 | kvm_unmap_rmapp(kvm, rmapp, gfn); |
| 940 | ++rmapp; |
| 941 | ++gfn; |
| 942 | } |
| 943 | } |
| 944 | |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 945 | static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, |
| 946 | unsigned long gfn) |
| 947 | { |
Paul Mackerras | 5551489 | 2011-12-15 02:02:47 +0000 | [diff] [blame] | 948 | struct revmap_entry *rev = kvm->arch.revmap; |
| 949 | unsigned long head, i, j; |
| 950 | unsigned long *hptep; |
| 951 | int ret = 0; |
| 952 | |
| 953 | retry: |
| 954 | lock_rmap(rmapp); |
| 955 | if (*rmapp & KVMPPC_RMAP_REFERENCED) { |
| 956 | *rmapp &= ~KVMPPC_RMAP_REFERENCED; |
| 957 | ret = 1; |
| 958 | } |
| 959 | if (!(*rmapp & KVMPPC_RMAP_PRESENT)) { |
| 960 | unlock_rmap(rmapp); |
| 961 | return ret; |
| 962 | } |
| 963 | |
| 964 | i = head = *rmapp & KVMPPC_RMAP_INDEX; |
| 965 | do { |
| 966 | hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4)); |
| 967 | j = rev[i].forw; |
| 968 | |
| 969 | /* If this HPTE isn't referenced, ignore it */ |
| 970 | if (!(hptep[1] & HPTE_R_R)) |
| 971 | continue; |
| 972 | |
| 973 | if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) { |
| 974 | /* unlock rmap before spinning on the HPTE lock */ |
| 975 | unlock_rmap(rmapp); |
| 976 | while (hptep[0] & HPTE_V_HVLOCK) |
| 977 | cpu_relax(); |
| 978 | goto retry; |
| 979 | } |
| 980 | |
| 981 | /* Now check and modify the HPTE */ |
| 982 | if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) { |
| 983 | kvmppc_clear_ref_hpte(kvm, hptep, i); |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 984 | if (!(rev[i].guest_rpte & HPTE_R_R)) { |
| 985 | rev[i].guest_rpte |= HPTE_R_R; |
| 986 | note_hpte_modification(kvm, &rev[i]); |
| 987 | } |
Paul Mackerras | 5551489 | 2011-12-15 02:02:47 +0000 | [diff] [blame] | 988 | ret = 1; |
| 989 | } |
| 990 | hptep[0] &= ~HPTE_V_HVLOCK; |
| 991 | } while ((i = j) != head); |
| 992 | |
| 993 | unlock_rmap(rmapp); |
| 994 | return ret; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | int kvm_age_hva(struct kvm *kvm, unsigned long hva) |
| 998 | { |
| 999 | if (!kvm->arch.using_mmu_notifiers) |
| 1000 | return 0; |
| 1001 | return kvm_handle_hva(kvm, hva, kvm_age_rmapp); |
| 1002 | } |
| 1003 | |
| 1004 | static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp, |
| 1005 | unsigned long gfn) |
| 1006 | { |
Paul Mackerras | 5551489 | 2011-12-15 02:02:47 +0000 | [diff] [blame] | 1007 | struct revmap_entry *rev = kvm->arch.revmap; |
| 1008 | unsigned long head, i, j; |
| 1009 | unsigned long *hp; |
| 1010 | int ret = 1; |
| 1011 | |
| 1012 | if (*rmapp & KVMPPC_RMAP_REFERENCED) |
| 1013 | return 1; |
| 1014 | |
| 1015 | lock_rmap(rmapp); |
| 1016 | if (*rmapp & KVMPPC_RMAP_REFERENCED) |
| 1017 | goto out; |
| 1018 | |
| 1019 | if (*rmapp & KVMPPC_RMAP_PRESENT) { |
| 1020 | i = head = *rmapp & KVMPPC_RMAP_INDEX; |
| 1021 | do { |
| 1022 | hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4)); |
| 1023 | j = rev[i].forw; |
| 1024 | if (hp[1] & HPTE_R_R) |
| 1025 | goto out; |
| 1026 | } while ((i = j) != head); |
| 1027 | } |
| 1028 | ret = 0; |
| 1029 | |
| 1030 | out: |
| 1031 | unlock_rmap(rmapp); |
| 1032 | return ret; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | int kvm_test_age_hva(struct kvm *kvm, unsigned long hva) |
| 1036 | { |
| 1037 | if (!kvm->arch.using_mmu_notifiers) |
| 1038 | return 0; |
| 1039 | return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp); |
| 1040 | } |
| 1041 | |
| 1042 | void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) |
| 1043 | { |
| 1044 | if (!kvm->arch.using_mmu_notifiers) |
| 1045 | return; |
| 1046 | kvm_handle_hva(kvm, hva, kvm_unmap_rmapp); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1049 | static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp) |
| 1050 | { |
| 1051 | struct revmap_entry *rev = kvm->arch.revmap; |
| 1052 | unsigned long head, i, j; |
| 1053 | unsigned long *hptep; |
| 1054 | int ret = 0; |
| 1055 | |
| 1056 | retry: |
| 1057 | lock_rmap(rmapp); |
| 1058 | if (*rmapp & KVMPPC_RMAP_CHANGED) { |
| 1059 | *rmapp &= ~KVMPPC_RMAP_CHANGED; |
| 1060 | ret = 1; |
| 1061 | } |
| 1062 | if (!(*rmapp & KVMPPC_RMAP_PRESENT)) { |
| 1063 | unlock_rmap(rmapp); |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
| 1067 | i = head = *rmapp & KVMPPC_RMAP_INDEX; |
| 1068 | do { |
| 1069 | hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4)); |
| 1070 | j = rev[i].forw; |
| 1071 | |
| 1072 | if (!(hptep[1] & HPTE_R_C)) |
| 1073 | continue; |
| 1074 | |
| 1075 | if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) { |
| 1076 | /* unlock rmap before spinning on the HPTE lock */ |
| 1077 | unlock_rmap(rmapp); |
| 1078 | while (hptep[0] & HPTE_V_HVLOCK) |
| 1079 | cpu_relax(); |
| 1080 | goto retry; |
| 1081 | } |
| 1082 | |
| 1083 | /* Now check and modify the HPTE */ |
| 1084 | if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) { |
| 1085 | /* need to make it temporarily absent to clear C */ |
| 1086 | hptep[0] |= HPTE_V_ABSENT; |
| 1087 | kvmppc_invalidate_hpte(kvm, hptep, i); |
| 1088 | hptep[1] &= ~HPTE_R_C; |
| 1089 | eieio(); |
| 1090 | hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID; |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 1091 | if (!(rev[i].guest_rpte & HPTE_R_C)) { |
| 1092 | rev[i].guest_rpte |= HPTE_R_C; |
| 1093 | note_hpte_modification(kvm, &rev[i]); |
| 1094 | } |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1095 | ret = 1; |
| 1096 | } |
| 1097 | hptep[0] &= ~HPTE_V_HVLOCK; |
| 1098 | } while ((i = j) != head); |
| 1099 | |
| 1100 | unlock_rmap(rmapp); |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1104 | static void harvest_vpa_dirty(struct kvmppc_vpa *vpa, |
| 1105 | struct kvm_memory_slot *memslot, |
| 1106 | unsigned long *map) |
| 1107 | { |
| 1108 | unsigned long gfn; |
| 1109 | |
| 1110 | if (!vpa->dirty || !vpa->pinned_addr) |
| 1111 | return; |
| 1112 | gfn = vpa->gpa >> PAGE_SHIFT; |
| 1113 | if (gfn < memslot->base_gfn || |
| 1114 | gfn >= memslot->base_gfn + memslot->npages) |
| 1115 | return; |
| 1116 | |
| 1117 | vpa->dirty = false; |
| 1118 | if (map) |
| 1119 | __set_bit_le(gfn - memslot->base_gfn, map); |
| 1120 | } |
| 1121 | |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 1122 | long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot, |
| 1123 | unsigned long *map) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1124 | { |
| 1125 | unsigned long i; |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 1126 | unsigned long *rmapp; |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1127 | struct kvm_vcpu *vcpu; |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1128 | |
| 1129 | preempt_disable(); |
Takuya Yoshikawa | d89cc61 | 2012-08-01 18:03:28 +0900 | [diff] [blame] | 1130 | rmapp = memslot->arch.rmap; |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1131 | for (i = 0; i < memslot->npages; ++i) { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 1132 | if (kvm_test_clear_dirty(kvm, rmapp) && map) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1133 | __set_bit_le(i, map); |
| 1134 | ++rmapp; |
| 1135 | } |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* Harvest dirty bits from VPA and DTL updates */ |
| 1138 | /* Note: we never modify the SLB shadow buffer areas */ |
| 1139 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1140 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1141 | harvest_vpa_dirty(&vcpu->arch.vpa, memslot, map); |
| 1142 | harvest_vpa_dirty(&vcpu->arch.dtl, memslot, map); |
| 1143 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1144 | } |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1145 | preempt_enable(); |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1149 | void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa, |
| 1150 | unsigned long *nb_ret) |
| 1151 | { |
| 1152 | struct kvm_memory_slot *memslot; |
| 1153 | unsigned long gfn = gpa >> PAGE_SHIFT; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1154 | struct page *page, *pages[1]; |
| 1155 | int npages; |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1156 | unsigned long hva, offset; |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 1157 | unsigned long pa; |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1158 | unsigned long *physp; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1159 | int srcu_idx; |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1160 | |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1161 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1162 | memslot = gfn_to_memslot(kvm, gfn); |
| 1163 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1164 | goto err; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1165 | if (!kvm->arch.using_mmu_notifiers) { |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 1166 | physp = memslot->arch.slot_phys; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1167 | if (!physp) |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1168 | goto err; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1169 | physp += gfn - memslot->base_gfn; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 1170 | pa = *physp; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1171 | if (!pa) { |
| 1172 | if (kvmppc_get_guest_page(kvm, gfn, memslot, |
| 1173 | PAGE_SIZE) < 0) |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1174 | goto err; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1175 | pa = *physp; |
| 1176 | } |
| 1177 | page = pfn_to_page(pa >> PAGE_SHIFT); |
David Gibson | de6c0b0 | 2012-05-08 20:24:08 +1000 | [diff] [blame] | 1178 | get_page(page); |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1179 | } else { |
| 1180 | hva = gfn_to_hva_memslot(memslot, gfn); |
| 1181 | npages = get_user_pages_fast(hva, 1, 1, pages); |
| 1182 | if (npages < 1) |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1183 | goto err; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1184 | page = pages[0]; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 1185 | } |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1186 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
| 1187 | |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1188 | offset = gpa & (PAGE_SIZE - 1); |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1189 | if (nb_ret) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1190 | *nb_ret = PAGE_SIZE - offset; |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1191 | return page_address(page) + offset; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1192 | |
| 1193 | err: |
| 1194 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
| 1195 | return NULL; |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1198 | void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa, |
| 1199 | bool dirty) |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1200 | { |
| 1201 | struct page *page = virt_to_page(va); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1202 | struct kvm_memory_slot *memslot; |
| 1203 | unsigned long gfn; |
| 1204 | unsigned long *rmap; |
| 1205 | int srcu_idx; |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1206 | |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1207 | put_page(page); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1208 | |
| 1209 | if (!dirty || !kvm->arch.using_mmu_notifiers) |
| 1210 | return; |
| 1211 | |
| 1212 | /* We need to mark this page dirty in the rmap chain */ |
| 1213 | gfn = gpa >> PAGE_SHIFT; |
| 1214 | srcu_idx = srcu_read_lock(&kvm->srcu); |
| 1215 | memslot = gfn_to_memslot(kvm, gfn); |
| 1216 | if (memslot) { |
| 1217 | rmap = &memslot->arch.rmap[gfn - memslot->base_gfn]; |
| 1218 | lock_rmap(rmap); |
| 1219 | *rmap |= KVMPPC_RMAP_CHANGED; |
| 1220 | unlock_rmap(rmap); |
| 1221 | } |
| 1222 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1225 | /* |
| 1226 | * Functions for reading and writing the hash table via reads and |
| 1227 | * writes on a file descriptor. |
| 1228 | * |
| 1229 | * Reads return the guest view of the hash table, which has to be |
| 1230 | * pieced together from the real hash table and the guest_rpte |
| 1231 | * values in the revmap array. |
| 1232 | * |
| 1233 | * On writes, each HPTE written is considered in turn, and if it |
| 1234 | * is valid, it is written to the HPT as if an H_ENTER with the |
| 1235 | * exact flag set was done. When the invalid count is non-zero |
| 1236 | * in the header written to the stream, the kernel will make |
| 1237 | * sure that that many HPTEs are invalid, and invalidate them |
| 1238 | * if not. |
| 1239 | */ |
| 1240 | |
| 1241 | struct kvm_htab_ctx { |
| 1242 | unsigned long index; |
| 1243 | unsigned long flags; |
| 1244 | struct kvm *kvm; |
| 1245 | int first_pass; |
| 1246 | }; |
| 1247 | |
| 1248 | #define HPTE_SIZE (2 * sizeof(unsigned long)) |
| 1249 | |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 1250 | /* |
| 1251 | * Returns 1 if this HPT entry has been modified or has pending |
| 1252 | * R/C bit changes. |
| 1253 | */ |
| 1254 | static int hpte_dirty(struct revmap_entry *revp, unsigned long *hptp) |
| 1255 | { |
| 1256 | unsigned long rcbits_unset; |
| 1257 | |
| 1258 | if (revp->guest_rpte & HPTE_GR_MODIFIED) |
| 1259 | return 1; |
| 1260 | |
| 1261 | /* Also need to consider changes in reference and changed bits */ |
| 1262 | rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C); |
| 1263 | if ((hptp[0] & HPTE_V_VALID) && (hptp[1] & rcbits_unset)) |
| 1264 | return 1; |
| 1265 | |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1269 | static long record_hpte(unsigned long flags, unsigned long *hptp, |
| 1270 | unsigned long *hpte, struct revmap_entry *revp, |
| 1271 | int want_valid, int first_pass) |
| 1272 | { |
| 1273 | unsigned long v, r; |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 1274 | unsigned long rcbits_unset; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1275 | int ok = 1; |
| 1276 | int valid, dirty; |
| 1277 | |
| 1278 | /* Unmodified entries are uninteresting except on the first pass */ |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 1279 | dirty = hpte_dirty(revp, hptp); |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1280 | if (!first_pass && !dirty) |
| 1281 | return 0; |
| 1282 | |
| 1283 | valid = 0; |
| 1284 | if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT)) { |
| 1285 | valid = 1; |
| 1286 | if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && |
| 1287 | !(hptp[0] & HPTE_V_BOLTED)) |
| 1288 | valid = 0; |
| 1289 | } |
| 1290 | if (valid != want_valid) |
| 1291 | return 0; |
| 1292 | |
| 1293 | v = r = 0; |
| 1294 | if (valid || dirty) { |
| 1295 | /* lock the HPTE so it's stable and read it */ |
| 1296 | preempt_disable(); |
| 1297 | while (!try_lock_hpte(hptp, HPTE_V_HVLOCK)) |
| 1298 | cpu_relax(); |
| 1299 | v = hptp[0]; |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 1300 | |
| 1301 | /* re-evaluate valid and dirty from synchronized HPTE value */ |
| 1302 | valid = !!(v & HPTE_V_VALID); |
| 1303 | dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED); |
| 1304 | |
| 1305 | /* Harvest R and C into guest view if necessary */ |
| 1306 | rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C); |
| 1307 | if (valid && (rcbits_unset & hptp[1])) { |
| 1308 | revp->guest_rpte |= (hptp[1] & (HPTE_R_R | HPTE_R_C)) | |
| 1309 | HPTE_GR_MODIFIED; |
| 1310 | dirty = 1; |
| 1311 | } |
| 1312 | |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1313 | if (v & HPTE_V_ABSENT) { |
| 1314 | v &= ~HPTE_V_ABSENT; |
| 1315 | v |= HPTE_V_VALID; |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 1316 | valid = 1; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1317 | } |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1318 | if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED)) |
| 1319 | valid = 0; |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 1320 | |
| 1321 | r = revp->guest_rpte; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1322 | /* only clear modified if this is the right sort of entry */ |
| 1323 | if (valid == want_valid && dirty) { |
| 1324 | r &= ~HPTE_GR_MODIFIED; |
| 1325 | revp->guest_rpte = r; |
| 1326 | } |
| 1327 | asm volatile(PPC_RELEASE_BARRIER "" : : : "memory"); |
| 1328 | hptp[0] &= ~HPTE_V_HVLOCK; |
| 1329 | preempt_enable(); |
| 1330 | if (!(valid == want_valid && (first_pass || dirty))) |
| 1331 | ok = 0; |
| 1332 | } |
| 1333 | hpte[0] = v; |
| 1334 | hpte[1] = r; |
| 1335 | return ok; |
| 1336 | } |
| 1337 | |
| 1338 | static ssize_t kvm_htab_read(struct file *file, char __user *buf, |
| 1339 | size_t count, loff_t *ppos) |
| 1340 | { |
| 1341 | struct kvm_htab_ctx *ctx = file->private_data; |
| 1342 | struct kvm *kvm = ctx->kvm; |
| 1343 | struct kvm_get_htab_header hdr; |
| 1344 | unsigned long *hptp; |
| 1345 | struct revmap_entry *revp; |
| 1346 | unsigned long i, nb, nw; |
| 1347 | unsigned long __user *lbuf; |
| 1348 | struct kvm_get_htab_header __user *hptr; |
| 1349 | unsigned long flags; |
| 1350 | int first_pass; |
| 1351 | unsigned long hpte[2]; |
| 1352 | |
| 1353 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 1354 | return -EFAULT; |
| 1355 | |
| 1356 | first_pass = ctx->first_pass; |
| 1357 | flags = ctx->flags; |
| 1358 | |
| 1359 | i = ctx->index; |
| 1360 | hptp = (unsigned long *)(kvm->arch.hpt_virt + (i * HPTE_SIZE)); |
| 1361 | revp = kvm->arch.revmap + i; |
| 1362 | lbuf = (unsigned long __user *)buf; |
| 1363 | |
| 1364 | nb = 0; |
| 1365 | while (nb + sizeof(hdr) + HPTE_SIZE < count) { |
| 1366 | /* Initialize header */ |
| 1367 | hptr = (struct kvm_get_htab_header __user *)buf; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1368 | hdr.n_valid = 0; |
| 1369 | hdr.n_invalid = 0; |
| 1370 | nw = nb; |
| 1371 | nb += sizeof(hdr); |
| 1372 | lbuf = (unsigned long __user *)(buf + sizeof(hdr)); |
| 1373 | |
| 1374 | /* Skip uninteresting entries, i.e. clean on not-first pass */ |
| 1375 | if (!first_pass) { |
| 1376 | while (i < kvm->arch.hpt_npte && |
Paul Mackerras | a1b4a0f | 2013-04-18 19:50:24 +0000 | [diff] [blame] | 1377 | !hpte_dirty(revp, hptp)) { |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1378 | ++i; |
| 1379 | hptp += 2; |
| 1380 | ++revp; |
| 1381 | } |
| 1382 | } |
Paul Mackerras | 05dd85f | 2012-11-21 23:29:12 +0000 | [diff] [blame] | 1383 | hdr.index = i; |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1384 | |
| 1385 | /* Grab a series of valid entries */ |
| 1386 | while (i < kvm->arch.hpt_npte && |
| 1387 | hdr.n_valid < 0xffff && |
| 1388 | nb + HPTE_SIZE < count && |
| 1389 | record_hpte(flags, hptp, hpte, revp, 1, first_pass)) { |
| 1390 | /* valid entry, write it out */ |
| 1391 | ++hdr.n_valid; |
| 1392 | if (__put_user(hpte[0], lbuf) || |
| 1393 | __put_user(hpte[1], lbuf + 1)) |
| 1394 | return -EFAULT; |
| 1395 | nb += HPTE_SIZE; |
| 1396 | lbuf += 2; |
| 1397 | ++i; |
| 1398 | hptp += 2; |
| 1399 | ++revp; |
| 1400 | } |
| 1401 | /* Now skip invalid entries while we can */ |
| 1402 | while (i < kvm->arch.hpt_npte && |
| 1403 | hdr.n_invalid < 0xffff && |
| 1404 | record_hpte(flags, hptp, hpte, revp, 0, first_pass)) { |
| 1405 | /* found an invalid entry */ |
| 1406 | ++hdr.n_invalid; |
| 1407 | ++i; |
| 1408 | hptp += 2; |
| 1409 | ++revp; |
| 1410 | } |
| 1411 | |
| 1412 | if (hdr.n_valid || hdr.n_invalid) { |
| 1413 | /* write back the header */ |
| 1414 | if (__copy_to_user(hptr, &hdr, sizeof(hdr))) |
| 1415 | return -EFAULT; |
| 1416 | nw = nb; |
| 1417 | buf = (char __user *)lbuf; |
| 1418 | } else { |
| 1419 | nb = nw; |
| 1420 | } |
| 1421 | |
| 1422 | /* Check if we've wrapped around the hash table */ |
| 1423 | if (i >= kvm->arch.hpt_npte) { |
| 1424 | i = 0; |
| 1425 | ctx->first_pass = 0; |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | ctx->index = i; |
| 1431 | |
| 1432 | return nb; |
| 1433 | } |
| 1434 | |
| 1435 | static ssize_t kvm_htab_write(struct file *file, const char __user *buf, |
| 1436 | size_t count, loff_t *ppos) |
| 1437 | { |
| 1438 | struct kvm_htab_ctx *ctx = file->private_data; |
| 1439 | struct kvm *kvm = ctx->kvm; |
| 1440 | struct kvm_get_htab_header hdr; |
| 1441 | unsigned long i, j; |
| 1442 | unsigned long v, r; |
| 1443 | unsigned long __user *lbuf; |
| 1444 | unsigned long *hptp; |
| 1445 | unsigned long tmp[2]; |
| 1446 | ssize_t nb; |
| 1447 | long int err, ret; |
| 1448 | int rma_setup; |
| 1449 | |
| 1450 | if (!access_ok(VERIFY_READ, buf, count)) |
| 1451 | return -EFAULT; |
| 1452 | |
| 1453 | /* lock out vcpus from running while we're doing this */ |
| 1454 | mutex_lock(&kvm->lock); |
| 1455 | rma_setup = kvm->arch.rma_setup_done; |
| 1456 | if (rma_setup) { |
| 1457 | kvm->arch.rma_setup_done = 0; /* temporarily */ |
| 1458 | /* order rma_setup_done vs. vcpus_running */ |
| 1459 | smp_mb(); |
| 1460 | if (atomic_read(&kvm->arch.vcpus_running)) { |
| 1461 | kvm->arch.rma_setup_done = 1; |
| 1462 | mutex_unlock(&kvm->lock); |
| 1463 | return -EBUSY; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | err = 0; |
| 1468 | for (nb = 0; nb + sizeof(hdr) <= count; ) { |
| 1469 | err = -EFAULT; |
| 1470 | if (__copy_from_user(&hdr, buf, sizeof(hdr))) |
| 1471 | break; |
| 1472 | |
| 1473 | err = 0; |
| 1474 | if (nb + hdr.n_valid * HPTE_SIZE > count) |
| 1475 | break; |
| 1476 | |
| 1477 | nb += sizeof(hdr); |
| 1478 | buf += sizeof(hdr); |
| 1479 | |
| 1480 | err = -EINVAL; |
| 1481 | i = hdr.index; |
| 1482 | if (i >= kvm->arch.hpt_npte || |
| 1483 | i + hdr.n_valid + hdr.n_invalid > kvm->arch.hpt_npte) |
| 1484 | break; |
| 1485 | |
| 1486 | hptp = (unsigned long *)(kvm->arch.hpt_virt + (i * HPTE_SIZE)); |
| 1487 | lbuf = (unsigned long __user *)buf; |
| 1488 | for (j = 0; j < hdr.n_valid; ++j) { |
| 1489 | err = -EFAULT; |
| 1490 | if (__get_user(v, lbuf) || __get_user(r, lbuf + 1)) |
| 1491 | goto out; |
| 1492 | err = -EINVAL; |
| 1493 | if (!(v & HPTE_V_VALID)) |
| 1494 | goto out; |
| 1495 | lbuf += 2; |
| 1496 | nb += HPTE_SIZE; |
| 1497 | |
| 1498 | if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT)) |
| 1499 | kvmppc_do_h_remove(kvm, 0, i, 0, tmp); |
| 1500 | err = -EIO; |
| 1501 | ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r, |
| 1502 | tmp); |
| 1503 | if (ret != H_SUCCESS) { |
| 1504 | pr_err("kvm_htab_write ret %ld i=%ld v=%lx " |
| 1505 | "r=%lx\n", ret, i, v, r); |
| 1506 | goto out; |
| 1507 | } |
| 1508 | if (!rma_setup && is_vrma_hpte(v)) { |
| 1509 | unsigned long psize = hpte_page_size(v, r); |
| 1510 | unsigned long senc = slb_pgsize_encoding(psize); |
| 1511 | unsigned long lpcr; |
| 1512 | |
| 1513 | kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | |
| 1514 | (VRMA_VSID << SLB_VSID_SHIFT_1T); |
| 1515 | lpcr = kvm->arch.lpcr & ~LPCR_VRMASD; |
| 1516 | lpcr |= senc << (LPCR_VRMASD_SH - 4); |
| 1517 | kvm->arch.lpcr = lpcr; |
| 1518 | rma_setup = 1; |
| 1519 | } |
| 1520 | ++i; |
| 1521 | hptp += 2; |
| 1522 | } |
| 1523 | |
| 1524 | for (j = 0; j < hdr.n_invalid; ++j) { |
| 1525 | if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT)) |
| 1526 | kvmppc_do_h_remove(kvm, 0, i, 0, tmp); |
| 1527 | ++i; |
| 1528 | hptp += 2; |
| 1529 | } |
| 1530 | err = 0; |
| 1531 | } |
| 1532 | |
| 1533 | out: |
| 1534 | /* Order HPTE updates vs. rma_setup_done */ |
| 1535 | smp_wmb(); |
| 1536 | kvm->arch.rma_setup_done = rma_setup; |
| 1537 | mutex_unlock(&kvm->lock); |
| 1538 | |
| 1539 | if (err) |
| 1540 | return err; |
| 1541 | return nb; |
| 1542 | } |
| 1543 | |
| 1544 | static int kvm_htab_release(struct inode *inode, struct file *filp) |
| 1545 | { |
| 1546 | struct kvm_htab_ctx *ctx = filp->private_data; |
| 1547 | |
| 1548 | filp->private_data = NULL; |
| 1549 | if (!(ctx->flags & KVM_GET_HTAB_WRITE)) |
| 1550 | atomic_dec(&ctx->kvm->arch.hpte_mod_interest); |
| 1551 | kvm_put_kvm(ctx->kvm); |
| 1552 | kfree(ctx); |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Al Viro | 75ef9de | 2013-04-04 19:09:41 -0400 | [diff] [blame] | 1556 | static const struct file_operations kvm_htab_fops = { |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1557 | .read = kvm_htab_read, |
| 1558 | .write = kvm_htab_write, |
| 1559 | .llseek = default_llseek, |
| 1560 | .release = kvm_htab_release, |
| 1561 | }; |
| 1562 | |
| 1563 | int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf) |
| 1564 | { |
| 1565 | int ret; |
| 1566 | struct kvm_htab_ctx *ctx; |
| 1567 | int rwflag; |
| 1568 | |
| 1569 | /* reject flags we don't recognize */ |
| 1570 | if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE)) |
| 1571 | return -EINVAL; |
| 1572 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1573 | if (!ctx) |
| 1574 | return -ENOMEM; |
| 1575 | kvm_get_kvm(kvm); |
| 1576 | ctx->kvm = kvm; |
| 1577 | ctx->index = ghf->start_index; |
| 1578 | ctx->flags = ghf->flags; |
| 1579 | ctx->first_pass = 1; |
| 1580 | |
| 1581 | rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY; |
Yann Droneaud | 2f84d5e | 2013-08-24 22:14:08 +0200 | [diff] [blame] | 1582 | ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC); |
Paul Mackerras | a293292 | 2012-11-19 22:57:20 +0000 | [diff] [blame] | 1583 | if (ret < 0) { |
| 1584 | kvm_put_kvm(kvm); |
| 1585 | return ret; |
| 1586 | } |
| 1587 | |
| 1588 | if (rwflag == O_RDONLY) { |
| 1589 | mutex_lock(&kvm->slots_lock); |
| 1590 | atomic_inc(&kvm->arch.hpte_mod_interest); |
| 1591 | /* make sure kvmppc_do_h_enter etc. see the increment */ |
| 1592 | synchronize_srcu_expedited(&kvm->srcu); |
| 1593 | mutex_unlock(&kvm->slots_lock); |
| 1594 | } |
| 1595 | |
| 1596 | return ret; |
| 1597 | } |
| 1598 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1599 | void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu) |
| 1600 | { |
| 1601 | struct kvmppc_mmu *mmu = &vcpu->arch.mmu; |
| 1602 | |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 1603 | if (cpu_has_feature(CPU_FTR_ARCH_206)) |
| 1604 | vcpu->arch.slb_nr = 32; /* POWER7 */ |
| 1605 | else |
| 1606 | vcpu->arch.slb_nr = 64; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1607 | |
| 1608 | mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate; |
| 1609 | mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr; |
| 1610 | |
| 1611 | vcpu->arch.hflags |= BOOK3S_HFLAG_SLB; |
| 1612 | } |