Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58: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 | * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com> |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 17 | * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com> |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/kvm.h> |
| 23 | #include <linux/kvm_host.h> |
| 24 | #include <linux/highmem.h> |
| 25 | #include <linux/gfp.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/hugetlb.h> |
| 28 | #include <linux/list.h> |
| 29 | #include <linux/anon_inodes.h> |
| 30 | |
| 31 | #include <asm/tlbflush.h> |
| 32 | #include <asm/kvm_ppc.h> |
| 33 | #include <asm/kvm_book3s.h> |
Aneesh Kumar K.V | f64e808 | 2016-03-01 12:59:20 +0530 | [diff] [blame] | 34 | #include <asm/book3s/64/mmu-hash.h> |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 35 | #include <asm/hvcall.h> |
| 36 | #include <asm/synch.h> |
| 37 | #include <asm/ppc-opcode.h> |
| 38 | #include <asm/kvm_host.h> |
| 39 | #include <asm/udbg.h> |
Alexey Kardashevskiy | 462ee11 | 2016-02-15 12:55:07 +1100 | [diff] [blame] | 40 | #include <asm/iommu.h> |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 41 | #include <asm/tce.h> |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 42 | |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 43 | static unsigned long kvmppc_tce_pages(unsigned long iommu_pages) |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 44 | { |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 45 | return ALIGN(iommu_pages * sizeof(u64), PAGE_SIZE) / PAGE_SIZE; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Alexey Kardashevskiy | f862698 | 2016-02-15 12:55:06 +1100 | [diff] [blame] | 48 | static unsigned long kvmppc_stt_pages(unsigned long tce_pages) |
| 49 | { |
| 50 | unsigned long stt_bytes = sizeof(struct kvmppc_spapr_tce_table) + |
| 51 | (tce_pages * sizeof(struct page *)); |
| 52 | |
| 53 | return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE; |
| 54 | } |
| 55 | |
| 56 | static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc) |
| 57 | { |
| 58 | long ret = 0; |
| 59 | |
| 60 | if (!current || !current->mm) |
| 61 | return ret; /* process exited */ |
| 62 | |
| 63 | down_write(¤t->mm->mmap_sem); |
| 64 | |
| 65 | if (inc) { |
| 66 | unsigned long locked, lock_limit; |
| 67 | |
| 68 | locked = current->mm->locked_vm + stt_pages; |
| 69 | lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 70 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) |
| 71 | ret = -ENOMEM; |
| 72 | else |
| 73 | current->mm->locked_vm += stt_pages; |
| 74 | } else { |
| 75 | if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm)) |
| 76 | stt_pages = current->mm->locked_vm; |
| 77 | |
| 78 | current->mm->locked_vm -= stt_pages; |
| 79 | } |
| 80 | |
| 81 | pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%ld %ld/%ld%s\n", current->pid, |
| 82 | inc ? '+' : '-', |
| 83 | stt_pages << PAGE_SHIFT, |
| 84 | current->mm->locked_vm << PAGE_SHIFT, |
| 85 | rlimit(RLIMIT_MEMLOCK), |
| 86 | ret ? " - exceeded" : ""); |
| 87 | |
| 88 | up_write(¤t->mm->mmap_sem); |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
Alexey Kardashevskiy | 366baf2 | 2016-02-15 12:55:05 +1100 | [diff] [blame] | 93 | static void release_spapr_tce_table(struct rcu_head *head) |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 94 | { |
Alexey Kardashevskiy | 366baf2 | 2016-02-15 12:55:05 +1100 | [diff] [blame] | 95 | struct kvmppc_spapr_tce_table *stt = container_of(head, |
| 96 | struct kvmppc_spapr_tce_table, rcu); |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 97 | unsigned long i, npages = kvmppc_tce_pages(stt->size); |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 98 | |
Alexey Kardashevskiy | f862698 | 2016-02-15 12:55:06 +1100 | [diff] [blame] | 99 | for (i = 0; i < npages; i++) |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 100 | __free_page(stt->pages[i]); |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 101 | |
Alexey Kardashevskiy | 366baf2 | 2016-02-15 12:55:05 +1100 | [diff] [blame] | 102 | kfree(stt); |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 106 | { |
| 107 | struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data; |
| 108 | struct page *page; |
| 109 | |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 110 | if (vmf->pgoff >= kvmppc_tce_pages(stt->size)) |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 111 | return VM_FAULT_SIGBUS; |
| 112 | |
| 113 | page = stt->pages[vmf->pgoff]; |
| 114 | get_page(page); |
| 115 | vmf->page = page; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static const struct vm_operations_struct kvm_spapr_tce_vm_ops = { |
| 120 | .fault = kvm_spapr_tce_fault, |
| 121 | }; |
| 122 | |
| 123 | static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma) |
| 124 | { |
| 125 | vma->vm_ops = &kvm_spapr_tce_vm_ops; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int kvm_spapr_tce_release(struct inode *inode, struct file *filp) |
| 130 | { |
| 131 | struct kvmppc_spapr_tce_table *stt = filp->private_data; |
Paul Mackerras | 8dcf70a | 2017-09-12 15:42:38 +1000 | [diff] [blame] | 132 | struct kvm *kvm = stt->kvm; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 133 | |
Paul Mackerras | 8dcf70a | 2017-09-12 15:42:38 +1000 | [diff] [blame] | 134 | mutex_lock(&kvm->lock); |
Alexey Kardashevskiy | 366baf2 | 2016-02-15 12:55:05 +1100 | [diff] [blame] | 135 | list_del_rcu(&stt->list); |
Paul Mackerras | 8dcf70a | 2017-09-12 15:42:38 +1000 | [diff] [blame] | 136 | mutex_unlock(&kvm->lock); |
Alexey Kardashevskiy | 366baf2 | 2016-02-15 12:55:05 +1100 | [diff] [blame] | 137 | |
| 138 | kvm_put_kvm(stt->kvm); |
| 139 | |
Alexey Kardashevskiy | f862698 | 2016-02-15 12:55:06 +1100 | [diff] [blame] | 140 | kvmppc_account_memlimit( |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 141 | kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false); |
Alexey Kardashevskiy | 366baf2 | 2016-02-15 12:55:05 +1100 | [diff] [blame] | 142 | call_rcu(&stt->rcu, release_spapr_tce_table); |
| 143 | |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Al Viro | 75ef9de | 2013-04-04 19:09:41 -0400 | [diff] [blame] | 147 | static const struct file_operations kvm_spapr_tce_fops = { |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 148 | .mmap = kvm_spapr_tce_mmap, |
| 149 | .release = kvm_spapr_tce_release, |
| 150 | }; |
| 151 | |
| 152 | long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 153 | struct kvm_create_spapr_tce_64 *args) |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 154 | { |
| 155 | struct kvmppc_spapr_tce_table *stt = NULL; |
Paul Mackerras | 18b7919 | 2017-09-12 15:41:49 +1000 | [diff] [blame] | 156 | struct kvmppc_spapr_tce_table *siter; |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 157 | unsigned long npages, size; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 158 | int ret = -ENOMEM; |
| 159 | int i; |
| 160 | |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 161 | if (!args->size) |
| 162 | return -EINVAL; |
| 163 | |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 164 | size = args->size; |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 165 | npages = kvmppc_tce_pages(size); |
Alexey Kardashevskiy | f862698 | 2016-02-15 12:55:06 +1100 | [diff] [blame] | 166 | ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true); |
Paul Mackerras | 18b7919 | 2017-09-12 15:41:49 +1000 | [diff] [blame] | 167 | if (ret) |
| 168 | return ret; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 169 | |
| 170 | stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *), |
| 171 | GFP_KERNEL); |
| 172 | if (!stt) |
Paul Mackerras | 18b7919 | 2017-09-12 15:41:49 +1000 | [diff] [blame] | 173 | goto fail_acct; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 174 | |
| 175 | stt->liobn = args->liobn; |
Alexey Kardashevskiy | 58ded42 | 2016-03-01 17:54:40 +1100 | [diff] [blame] | 176 | stt->page_shift = args->page_shift; |
| 177 | stt->offset = args->offset; |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 178 | stt->size = size; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 179 | stt->kvm = kvm; |
| 180 | |
| 181 | for (i = 0; i < npages; i++) { |
| 182 | stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 183 | if (!stt->pages[i]) |
| 184 | goto fail; |
| 185 | } |
| 186 | |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 187 | mutex_lock(&kvm->lock); |
Paul Mackerras | 18b7919 | 2017-09-12 15:41:49 +1000 | [diff] [blame] | 188 | |
| 189 | /* Check this LIOBN hasn't been previously allocated */ |
| 190 | ret = 0; |
| 191 | list_for_each_entry(siter, &kvm->arch.spapr_tce_tables, list) { |
| 192 | if (siter->liobn == args->liobn) { |
| 193 | ret = -EBUSY; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (!ret) |
| 199 | ret = anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops, |
| 200 | stt, O_RDWR | O_CLOEXEC); |
| 201 | |
| 202 | if (ret >= 0) { |
| 203 | list_add_rcu(&stt->list, &kvm->arch.spapr_tce_tables); |
| 204 | kvm_get_kvm(kvm); |
| 205 | } |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 206 | |
| 207 | mutex_unlock(&kvm->lock); |
| 208 | |
Paul Mackerras | 18b7919 | 2017-09-12 15:41:49 +1000 | [diff] [blame] | 209 | if (ret >= 0) |
| 210 | return ret; |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 211 | |
Paul Mackerras | 18b7919 | 2017-09-12 15:41:49 +1000 | [diff] [blame] | 212 | fail: |
| 213 | for (i = 0; i < npages; i++) |
| 214 | if (stt->pages[i]) |
| 215 | __free_page(stt->pages[i]); |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 216 | |
Paul Mackerras | 18b7919 | 2017-09-12 15:41:49 +1000 | [diff] [blame] | 217 | kfree(stt); |
| 218 | fail_acct: |
| 219 | kvmppc_account_memlimit(kvmppc_stt_pages(npages), false); |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 220 | return ret; |
| 221 | } |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 222 | |
Alexey Kardashevskiy | 31217db | 2016-03-18 13:50:42 +1100 | [diff] [blame] | 223 | long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
| 224 | unsigned long ioba, unsigned long tce) |
| 225 | { |
| 226 | struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn); |
| 227 | long ret; |
| 228 | |
| 229 | /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */ |
| 230 | /* liobn, ioba, tce); */ |
| 231 | |
| 232 | if (!stt) |
| 233 | return H_TOO_HARD; |
| 234 | |
| 235 | ret = kvmppc_ioba_validate(stt, ioba, 1); |
| 236 | if (ret != H_SUCCESS) |
| 237 | return ret; |
| 238 | |
| 239 | ret = kvmppc_tce_validate(stt, tce); |
| 240 | if (ret != H_SUCCESS) |
| 241 | return ret; |
| 242 | |
| 243 | kvmppc_tce_put(stt, ioba >> stt->page_shift, tce); |
| 244 | |
| 245 | return H_SUCCESS; |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(kvmppc_h_put_tce); |
| 248 | |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 249 | long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu, |
| 250 | unsigned long liobn, unsigned long ioba, |
| 251 | unsigned long tce_list, unsigned long npages) |
| 252 | { |
| 253 | struct kvmppc_spapr_tce_table *stt; |
| 254 | long i, ret = H_SUCCESS, idx; |
| 255 | unsigned long entry, ua = 0; |
Daniel Axtens | f875051 | 2016-07-12 10:54:48 +1000 | [diff] [blame] | 256 | u64 __user *tces; |
| 257 | u64 tce; |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 258 | |
| 259 | stt = kvmppc_find_table(vcpu, liobn); |
| 260 | if (!stt) |
| 261 | return H_TOO_HARD; |
| 262 | |
Alexey Kardashevskiy | fe26e52 | 2016-03-01 17:54:38 +1100 | [diff] [blame] | 263 | entry = ioba >> stt->page_shift; |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 264 | /* |
| 265 | * SPAPR spec says that the maximum size of the list is 512 TCEs |
| 266 | * so the whole table fits in 4K page |
| 267 | */ |
| 268 | if (npages > 512) |
| 269 | return H_PARAMETER; |
| 270 | |
| 271 | if (tce_list & (SZ_4K - 1)) |
| 272 | return H_PARAMETER; |
| 273 | |
| 274 | ret = kvmppc_ioba_validate(stt, ioba, npages); |
| 275 | if (ret != H_SUCCESS) |
| 276 | return ret; |
| 277 | |
| 278 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 279 | if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL)) { |
| 280 | ret = H_TOO_HARD; |
| 281 | goto unlock_exit; |
| 282 | } |
| 283 | tces = (u64 __user *) ua; |
| 284 | |
| 285 | for (i = 0; i < npages; ++i) { |
| 286 | if (get_user(tce, tces + i)) { |
| 287 | ret = H_TOO_HARD; |
| 288 | goto unlock_exit; |
| 289 | } |
| 290 | tce = be64_to_cpu(tce); |
| 291 | |
| 292 | ret = kvmppc_tce_validate(stt, tce); |
| 293 | if (ret != H_SUCCESS) |
| 294 | goto unlock_exit; |
| 295 | |
| 296 | kvmppc_tce_put(stt, entry + i, tce); |
| 297 | } |
| 298 | |
| 299 | unlock_exit: |
| 300 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | EXPORT_SYMBOL_GPL(kvmppc_h_put_tce_indirect); |
Alexey Kardashevskiy | 31217db | 2016-03-18 13:50:42 +1100 | [diff] [blame] | 305 | |
| 306 | long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu, |
| 307 | unsigned long liobn, unsigned long ioba, |
| 308 | unsigned long tce_value, unsigned long npages) |
| 309 | { |
| 310 | struct kvmppc_spapr_tce_table *stt; |
| 311 | long i, ret; |
| 312 | |
| 313 | stt = kvmppc_find_table(vcpu, liobn); |
| 314 | if (!stt) |
| 315 | return H_TOO_HARD; |
| 316 | |
| 317 | ret = kvmppc_ioba_validate(stt, ioba, npages); |
| 318 | if (ret != H_SUCCESS) |
| 319 | return ret; |
| 320 | |
| 321 | /* Check permission bits only to allow userspace poison TCE for debug */ |
| 322 | if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ)) |
| 323 | return H_PARAMETER; |
| 324 | |
| 325 | for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift)) |
| 326 | kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value); |
| 327 | |
| 328 | return H_SUCCESS; |
| 329 | } |
| 330 | EXPORT_SYMBOL_GPL(kvmppc_h_stuff_tce); |