blob: c379ff5a4438d43af1837dd15629536be89c7105 [file] [log] [blame]
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
16 * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +110017 * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +000018 */
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.Vf64e8082016-03-01 12:59:20 +053034#include <asm/book3s/64/mmu-hash.h>
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +000035#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 Kardashevskiy462ee112016-02-15 12:55:07 +110040#include <asm/iommu.h>
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +110041#include <asm/tce.h>
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +000042
Alexey Kardashevskiyfe26e522016-03-01 17:54:38 +110043static unsigned long kvmppc_tce_pages(unsigned long iommu_pages)
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +000044{
Alexey Kardashevskiyfe26e522016-03-01 17:54:38 +110045 return ALIGN(iommu_pages * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +000046}
47
Alexey Kardashevskiyf8626982016-02-15 12:55:06 +110048static 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
56static 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(&current->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(&current->mm->mmap_sem);
89
90 return ret;
91}
92
Alexey Kardashevskiy366baf22016-02-15 12:55:05 +110093static void release_spapr_tce_table(struct rcu_head *head)
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +000094{
Alexey Kardashevskiy366baf22016-02-15 12:55:05 +110095 struct kvmppc_spapr_tce_table *stt = container_of(head,
96 struct kvmppc_spapr_tce_table, rcu);
Alexey Kardashevskiyfe26e522016-03-01 17:54:38 +110097 unsigned long i, npages = kvmppc_tce_pages(stt->size);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +000098
Alexey Kardashevskiyf8626982016-02-15 12:55:06 +110099 for (i = 0; i < npages; i++)
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000100 __free_page(stt->pages[i]);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000101
Alexey Kardashevskiy366baf22016-02-15 12:55:05 +1100102 kfree(stt);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000103}
104
105static 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 Kardashevskiyfe26e522016-03-01 17:54:38 +1100110 if (vmf->pgoff >= kvmppc_tce_pages(stt->size))
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000111 return VM_FAULT_SIGBUS;
112
113 page = stt->pages[vmf->pgoff];
114 get_page(page);
115 vmf->page = page;
116 return 0;
117}
118
119static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
120 .fault = kvm_spapr_tce_fault,
121};
122
123static 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
129static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
130{
131 struct kvmppc_spapr_tce_table *stt = filp->private_data;
132
Alexey Kardashevskiy366baf22016-02-15 12:55:05 +1100133 list_del_rcu(&stt->list);
134
135 kvm_put_kvm(stt->kvm);
136
Alexey Kardashevskiyf8626982016-02-15 12:55:06 +1100137 kvmppc_account_memlimit(
Alexey Kardashevskiyfe26e522016-03-01 17:54:38 +1100138 kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);
Alexey Kardashevskiy366baf22016-02-15 12:55:05 +1100139 call_rcu(&stt->rcu, release_spapr_tce_table);
140
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000141 return 0;
142}
143
Al Viro75ef9de2013-04-04 19:09:41 -0400144static const struct file_operations kvm_spapr_tce_fops = {
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000145 .mmap = kvm_spapr_tce_mmap,
146 .release = kvm_spapr_tce_release,
147};
148
149long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +1100150 struct kvm_create_spapr_tce_64 *args)
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000151{
152 struct kvmppc_spapr_tce_table *stt = NULL;
Alexey Kardashevskiyfe26e522016-03-01 17:54:38 +1100153 unsigned long npages, size;
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000154 int ret = -ENOMEM;
155 int i;
156
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +1100157 if (!args->size)
158 return -EINVAL;
159
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000160 /* Check this LIOBN hasn't been previously allocated */
161 list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
162 if (stt->liobn == args->liobn)
163 return -EBUSY;
164 }
165
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +1100166 size = args->size;
Alexey Kardashevskiyfe26e522016-03-01 17:54:38 +1100167 npages = kvmppc_tce_pages(size);
Alexey Kardashevskiyf8626982016-02-15 12:55:06 +1100168 ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true);
169 if (ret) {
170 stt = NULL;
171 goto fail;
172 }
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000173
174 stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
175 GFP_KERNEL);
176 if (!stt)
177 goto fail;
178
179 stt->liobn = args->liobn;
Alexey Kardashevskiy58ded422016-03-01 17:54:40 +1100180 stt->page_shift = args->page_shift;
181 stt->offset = args->offset;
Alexey Kardashevskiyfe26e522016-03-01 17:54:38 +1100182 stt->size = size;
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000183 stt->kvm = kvm;
184
185 for (i = 0; i < npages; i++) {
186 stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
187 if (!stt->pages[i])
188 goto fail;
189 }
190
191 kvm_get_kvm(kvm);
192
193 mutex_lock(&kvm->lock);
Alexey Kardashevskiy366baf22016-02-15 12:55:05 +1100194 list_add_rcu(&stt->list, &kvm->arch.spapr_tce_tables);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000195
196 mutex_unlock(&kvm->lock);
197
198 return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
Yann Droneaud2f84d5e2013-08-24 22:14:08 +0200199 stt, O_RDWR | O_CLOEXEC);
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000200
201fail:
202 if (stt) {
203 for (i = 0; i < npages; i++)
204 if (stt->pages[i])
205 __free_page(stt->pages[i]);
206
207 kfree(stt);
208 }
209 return ret;
210}
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100211
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +1100212long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
213 unsigned long ioba, unsigned long tce)
214{
215 struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
216 long ret;
217
218 /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
219 /* liobn, ioba, tce); */
220
221 if (!stt)
222 return H_TOO_HARD;
223
224 ret = kvmppc_ioba_validate(stt, ioba, 1);
225 if (ret != H_SUCCESS)
226 return ret;
227
228 ret = kvmppc_tce_validate(stt, tce);
229 if (ret != H_SUCCESS)
230 return ret;
231
232 kvmppc_tce_put(stt, ioba >> stt->page_shift, tce);
233
234 return H_SUCCESS;
235}
236EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
237
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100238long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
239 unsigned long liobn, unsigned long ioba,
240 unsigned long tce_list, unsigned long npages)
241{
242 struct kvmppc_spapr_tce_table *stt;
243 long i, ret = H_SUCCESS, idx;
244 unsigned long entry, ua = 0;
Daniel Axtensf8750512016-07-12 10:54:48 +1000245 u64 __user *tces;
246 u64 tce;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100247
248 stt = kvmppc_find_table(vcpu, liobn);
249 if (!stt)
250 return H_TOO_HARD;
251
Alexey Kardashevskiyfe26e522016-03-01 17:54:38 +1100252 entry = ioba >> stt->page_shift;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100253 /*
254 * SPAPR spec says that the maximum size of the list is 512 TCEs
255 * so the whole table fits in 4K page
256 */
257 if (npages > 512)
258 return H_PARAMETER;
259
260 if (tce_list & (SZ_4K - 1))
261 return H_PARAMETER;
262
263 ret = kvmppc_ioba_validate(stt, ioba, npages);
264 if (ret != H_SUCCESS)
265 return ret;
266
267 idx = srcu_read_lock(&vcpu->kvm->srcu);
268 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL)) {
269 ret = H_TOO_HARD;
270 goto unlock_exit;
271 }
272 tces = (u64 __user *) ua;
273
274 for (i = 0; i < npages; ++i) {
275 if (get_user(tce, tces + i)) {
276 ret = H_TOO_HARD;
277 goto unlock_exit;
278 }
279 tce = be64_to_cpu(tce);
280
281 ret = kvmppc_tce_validate(stt, tce);
282 if (ret != H_SUCCESS)
283 goto unlock_exit;
284
285 kvmppc_tce_put(stt, entry + i, tce);
286 }
287
288unlock_exit:
289 srcu_read_unlock(&vcpu->kvm->srcu, idx);
290
291 return ret;
292}
293EXPORT_SYMBOL_GPL(kvmppc_h_put_tce_indirect);
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +1100294
295long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
296 unsigned long liobn, unsigned long ioba,
297 unsigned long tce_value, unsigned long npages)
298{
299 struct kvmppc_spapr_tce_table *stt;
300 long i, ret;
301
302 stt = kvmppc_find_table(vcpu, liobn);
303 if (!stt)
304 return H_TOO_HARD;
305
306 ret = kvmppc_ioba_validate(stt, ioba, npages);
307 if (ret != H_SUCCESS)
308 return ret;
309
310 /* Check permission bits only to allow userspace poison TCE for debug */
311 if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
312 return H_PARAMETER;
313
314 for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
315 kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
316
317 return H_SUCCESS;
318}
319EXPORT_SYMBOL_GPL(kvmppc_h_stuff_tce);