book3s_64_vio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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>
  17. * Copyright 2016 Alexey Kardashevskiy, IBM Corporation <aik@au1.ibm.com>
  18. */
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kvm.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/highmem.h>
  24. #include <linux/gfp.h>
  25. #include <linux/slab.h>
  26. #include <linux/hugetlb.h>
  27. #include <linux/list.h>
  28. #include <linux/anon_inodes.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/kvm_ppc.h>
  31. #include <asm/kvm_book3s.h>
  32. #include <asm/book3s/64/mmu-hash.h>
  33. #include <asm/hvcall.h>
  34. #include <asm/synch.h>
  35. #include <asm/ppc-opcode.h>
  36. #include <asm/kvm_host.h>
  37. #include <asm/udbg.h>
  38. #include <asm/iommu.h>
  39. #include <asm/tce.h>
  40. static unsigned long kvmppc_tce_pages(unsigned long iommu_pages)
  41. {
  42. return ALIGN(iommu_pages * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
  43. }
  44. static unsigned long kvmppc_stt_pages(unsigned long tce_pages)
  45. {
  46. unsigned long stt_bytes = sizeof(struct kvmppc_spapr_tce_table) +
  47. (tce_pages * sizeof(struct page *));
  48. return tce_pages + ALIGN(stt_bytes, PAGE_SIZE) / PAGE_SIZE;
  49. }
  50. static long kvmppc_account_memlimit(unsigned long stt_pages, bool inc)
  51. {
  52. long ret = 0;
  53. if (!current || !current->mm)
  54. return ret; /* process exited */
  55. down_write(&current->mm->mmap_sem);
  56. if (inc) {
  57. unsigned long locked, lock_limit;
  58. locked = current->mm->locked_vm + stt_pages;
  59. lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  60. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  61. ret = -ENOMEM;
  62. else
  63. current->mm->locked_vm += stt_pages;
  64. } else {
  65. if (WARN_ON_ONCE(stt_pages > current->mm->locked_vm))
  66. stt_pages = current->mm->locked_vm;
  67. current->mm->locked_vm -= stt_pages;
  68. }
  69. pr_debug("[%d] RLIMIT_MEMLOCK KVM %c%ld %ld/%ld%s\n", current->pid,
  70. inc ? '+' : '-',
  71. stt_pages << PAGE_SHIFT,
  72. current->mm->locked_vm << PAGE_SHIFT,
  73. rlimit(RLIMIT_MEMLOCK),
  74. ret ? " - exceeded" : "");
  75. up_write(&current->mm->mmap_sem);
  76. return ret;
  77. }
  78. static void release_spapr_tce_table(struct rcu_head *head)
  79. {
  80. struct kvmppc_spapr_tce_table *stt = container_of(head,
  81. struct kvmppc_spapr_tce_table, rcu);
  82. unsigned long i, npages = kvmppc_tce_pages(stt->size);
  83. for (i = 0; i < npages; i++)
  84. __free_page(stt->pages[i]);
  85. kfree(stt);
  86. }
  87. static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  88. {
  89. struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data;
  90. struct page *page;
  91. if (vmf->pgoff >= kvmppc_tce_pages(stt->size))
  92. return VM_FAULT_SIGBUS;
  93. page = stt->pages[vmf->pgoff];
  94. get_page(page);
  95. vmf->page = page;
  96. return 0;
  97. }
  98. static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
  99. .fault = kvm_spapr_tce_fault,
  100. };
  101. static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
  102. {
  103. vma->vm_ops = &kvm_spapr_tce_vm_ops;
  104. return 0;
  105. }
  106. static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
  107. {
  108. struct kvmppc_spapr_tce_table *stt = filp->private_data;
  109. list_del_rcu(&stt->list);
  110. kvm_put_kvm(stt->kvm);
  111. kvmppc_account_memlimit(
  112. kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);
  113. call_rcu(&stt->rcu, release_spapr_tce_table);
  114. return 0;
  115. }
  116. static const struct file_operations kvm_spapr_tce_fops = {
  117. .mmap = kvm_spapr_tce_mmap,
  118. .release = kvm_spapr_tce_release,
  119. };
  120. long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  121. struct kvm_create_spapr_tce_64 *args)
  122. {
  123. struct kvmppc_spapr_tce_table *stt = NULL;
  124. unsigned long npages, size;
  125. int ret = -ENOMEM;
  126. int i;
  127. if (!args->size)
  128. return -EINVAL;
  129. /* Check this LIOBN hasn't been previously allocated */
  130. list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
  131. if (stt->liobn == args->liobn)
  132. return -EBUSY;
  133. }
  134. size = args->size;
  135. npages = kvmppc_tce_pages(size);
  136. ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true);
  137. if (ret) {
  138. stt = NULL;
  139. goto fail;
  140. }
  141. stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
  142. GFP_KERNEL);
  143. if (!stt)
  144. goto fail;
  145. stt->liobn = args->liobn;
  146. stt->page_shift = args->page_shift;
  147. stt->offset = args->offset;
  148. stt->size = size;
  149. stt->kvm = kvm;
  150. for (i = 0; i < npages; i++) {
  151. stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  152. if (!stt->pages[i])
  153. goto fail;
  154. }
  155. kvm_get_kvm(kvm);
  156. mutex_lock(&kvm->lock);
  157. list_add_rcu(&stt->list, &kvm->arch.spapr_tce_tables);
  158. mutex_unlock(&kvm->lock);
  159. return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
  160. stt, O_RDWR | O_CLOEXEC);
  161. fail:
  162. if (stt) {
  163. for (i = 0; i < npages; i++)
  164. if (stt->pages[i])
  165. __free_page(stt->pages[i]);
  166. kfree(stt);
  167. }
  168. return ret;
  169. }
  170. long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  171. unsigned long ioba, unsigned long tce)
  172. {
  173. struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
  174. long ret;
  175. /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
  176. /* liobn, ioba, tce); */
  177. if (!stt)
  178. return H_TOO_HARD;
  179. ret = kvmppc_ioba_validate(stt, ioba, 1);
  180. if (ret != H_SUCCESS)
  181. return ret;
  182. ret = kvmppc_tce_validate(stt, tce);
  183. if (ret != H_SUCCESS)
  184. return ret;
  185. kvmppc_tce_put(stt, ioba >> stt->page_shift, tce);
  186. return H_SUCCESS;
  187. }
  188. EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
  189. long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
  190. unsigned long liobn, unsigned long ioba,
  191. unsigned long tce_list, unsigned long npages)
  192. {
  193. struct kvmppc_spapr_tce_table *stt;
  194. long i, ret = H_SUCCESS, idx;
  195. unsigned long entry, ua = 0;
  196. u64 __user *tces;
  197. u64 tce;
  198. stt = kvmppc_find_table(vcpu, liobn);
  199. if (!stt)
  200. return H_TOO_HARD;
  201. entry = ioba >> stt->page_shift;
  202. /*
  203. * SPAPR spec says that the maximum size of the list is 512 TCEs
  204. * so the whole table fits in 4K page
  205. */
  206. if (npages > 512)
  207. return H_PARAMETER;
  208. if (tce_list & (SZ_4K - 1))
  209. return H_PARAMETER;
  210. ret = kvmppc_ioba_validate(stt, ioba, npages);
  211. if (ret != H_SUCCESS)
  212. return ret;
  213. idx = srcu_read_lock(&vcpu->kvm->srcu);
  214. if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL)) {
  215. ret = H_TOO_HARD;
  216. goto unlock_exit;
  217. }
  218. tces = (u64 __user *) ua;
  219. for (i = 0; i < npages; ++i) {
  220. if (get_user(tce, tces + i)) {
  221. ret = H_TOO_HARD;
  222. goto unlock_exit;
  223. }
  224. tce = be64_to_cpu(tce);
  225. ret = kvmppc_tce_validate(stt, tce);
  226. if (ret != H_SUCCESS)
  227. goto unlock_exit;
  228. kvmppc_tce_put(stt, entry + i, tce);
  229. }
  230. unlock_exit:
  231. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL_GPL(kvmppc_h_put_tce_indirect);
  235. long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
  236. unsigned long liobn, unsigned long ioba,
  237. unsigned long tce_value, unsigned long npages)
  238. {
  239. struct kvmppc_spapr_tce_table *stt;
  240. long i, ret;
  241. stt = kvmppc_find_table(vcpu, liobn);
  242. if (!stt)
  243. return H_TOO_HARD;
  244. ret = kvmppc_ioba_validate(stt, ioba, npages);
  245. if (ret != H_SUCCESS)
  246. return ret;
  247. /* Check permission bits only to allow userspace poison TCE for debug */
  248. if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
  249. return H_PARAMETER;
  250. for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
  251. kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
  252. return H_SUCCESS;
  253. }
  254. EXPORT_SYMBOL_GPL(kvmppc_h_stuff_tce);