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.
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.
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.
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
16 * Copyright 2011 David Gibson, IBM Corporation <dwg@au1.ibm.com>
17 * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
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>
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/kvm_host.h>
40 #include <asm/iommu.h>
43 static unsigned long kvmppc_tce_pages(unsigned long window_size)
45 return ALIGN((window_size >> IOMMU_PAGE_SHIFT_4K)
46 * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
49 static unsigned long kvmppc_stt_pages(unsigned long tce_pages)
51 unsigned long stt_bytes = sizeof(struct kvmppc_spapr_tce_table) +
52 (tce_pages * sizeof(struct page *));
54 return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE;
57 static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
61 if (!current || !current->mm)
62 return ret; /* process exited */
64 down_write(¤t->mm->mmap_sem);
67 unsigned long locked, lock_limit;
69 locked = current->mm->locked_vm + stt_pages;
70 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
71 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
74 current->mm->locked_vm += stt_pages;
76 if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm))
77 stt_pages = current->mm->locked_vm;
79 current->mm->locked_vm -= stt_pages;
82 pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%ld %ld/%ld%s\n", current->pid,
84 stt_pages << PAGE_SHIFT,
85 current->mm->locked_vm << PAGE_SHIFT,
86 rlimit(RLIMIT_MEMLOCK),
87 ret ? " - exceeded" : "");
89 up_write(¤t->mm->mmap_sem);
94 static void release_spapr_tce_table(struct rcu_head *head)
96 struct kvmppc_spapr_tce_table *stt = container_of(head,
97 struct kvmppc_spapr_tce_table, rcu);
99 unsigned long npages = kvmppc_tce_pages(stt->window_size);
101 for (i = 0; i < npages; i++)
102 __free_page(stt->pages[i]);
107 static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
109 struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data;
112 if (vmf->pgoff >= kvmppc_tce_pages(stt->window_size))
113 return VM_FAULT_SIGBUS;
115 page = stt->pages[vmf->pgoff];
121 static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
122 .fault = kvm_spapr_tce_fault,
125 static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
127 vma->vm_ops = &kvm_spapr_tce_vm_ops;
131 static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
133 struct kvmppc_spapr_tce_table *stt = filp->private_data;
135 list_del_rcu(&stt->list);
137 kvm_put_kvm(stt->kvm);
139 kvmppc_account_memlimit(
140 kvmppc_stt_pages(kvmppc_tce_pages(stt->window_size)), false);
141 call_rcu(&stt->rcu, release_spapr_tce_table);
146 static const struct file_operations kvm_spapr_tce_fops = {
147 .mmap = kvm_spapr_tce_mmap,
148 .release = kvm_spapr_tce_release,
151 long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
152 struct kvm_create_spapr_tce *args)
154 struct kvmppc_spapr_tce_table *stt = NULL;
155 unsigned long npages;
159 /* Check this LIOBN hasn't been previously allocated */
160 list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
161 if (stt->liobn == args->liobn)
165 npages = kvmppc_tce_pages(args->window_size);
166 ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true);
172 stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
177 stt->liobn = args->liobn;
178 stt->window_size = args->window_size;
181 for (i = 0; i < npages; i++) {
182 stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
189 mutex_lock(&kvm->lock);
190 list_add_rcu(&stt->list, &kvm->arch.spapr_tce_tables);
192 mutex_unlock(&kvm->lock);
194 return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
195 stt, O_RDWR | O_CLOEXEC);
199 for (i = 0; i < npages; i++)
201 __free_page(stt->pages[i]);
208 long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
209 unsigned long liobn, unsigned long ioba,
210 unsigned long tce_list, unsigned long npages)
212 struct kvmppc_spapr_tce_table *stt;
213 long i, ret = H_SUCCESS, idx;
214 unsigned long entry, ua = 0;
215 u64 __user *tces, tce;
217 stt = kvmppc_find_table(vcpu, liobn);
221 entry = ioba >> IOMMU_PAGE_SHIFT_4K;
223 * SPAPR spec says that the maximum size of the list is 512 TCEs
224 * so the whole table fits in 4K page
229 if (tce_list & (SZ_4K - 1))
232 ret = kvmppc_ioba_validate(stt, ioba, npages);
233 if (ret != H_SUCCESS)
236 idx = srcu_read_lock(&vcpu->kvm->srcu);
237 if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL)) {
241 tces = (u64 __user *) ua;
243 for (i = 0; i < npages; ++i) {
244 if (get_user(tce, tces + i)) {
248 tce = be64_to_cpu(tce);
250 ret = kvmppc_tce_validate(stt, tce);
251 if (ret != H_SUCCESS)
254 kvmppc_tce_put(stt, entry + i, tce);
258 srcu_read_unlock(&vcpu->kvm->srcu, idx);
262 EXPORT_SYMBOL_GPL(kvmppc_h_put_tce_indirect);