book3s_64_vio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. struct kvm *kvm = stt->kvm;
  110. mutex_lock(&kvm->lock);
  111. list_del_rcu(&stt->list);
  112. mutex_unlock(&kvm->lock);
  113. kvm_put_kvm(stt->kvm);
  114. kvmppc_account_memlimit(
  115. kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);
  116. call_rcu(&stt->rcu, release_spapr_tce_table);
  117. return 0;
  118. }
  119. static const struct file_operations kvm_spapr_tce_fops = {
  120. .mmap = kvm_spapr_tce_mmap,
  121. .release = kvm_spapr_tce_release,
  122. };
  123. long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  124. struct kvm_create_spapr_tce_64 *args)
  125. {
  126. struct kvmppc_spapr_tce_table *stt = NULL;
  127. struct kvmppc_spapr_tce_table *siter;
  128. unsigned long npages, size;
  129. int ret = -ENOMEM;
  130. int i;
  131. if (!args->size)
  132. return -EINVAL;
  133. size = args->size;
  134. npages = kvmppc_tce_pages(size);
  135. ret = kvmppc_account_memlimit(kvmppc_stt_pages(npages), true);
  136. if (ret)
  137. return ret;
  138. stt = kzalloc(sizeof(*stt) + npages * sizeof(struct page *),
  139. GFP_KERNEL);
  140. if (!stt)
  141. goto fail_acct;
  142. stt->liobn = args->liobn;
  143. stt->page_shift = args->page_shift;
  144. stt->offset = args->offset;
  145. stt->size = size;
  146. stt->kvm = kvm;
  147. for (i = 0; i < npages; i++) {
  148. stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  149. if (!stt->pages[i])
  150. goto fail;
  151. }
  152. mutex_lock(&kvm->lock);
  153. /* Check this LIOBN hasn't been previously allocated */
  154. ret = 0;
  155. list_for_each_entry(siter, &kvm->arch.spapr_tce_tables, list) {
  156. if (siter->liobn == args->liobn) {
  157. ret = -EBUSY;
  158. break;
  159. }
  160. }
  161. if (!ret)
  162. ret = anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
  163. stt, O_RDWR | O_CLOEXEC);
  164. if (ret >= 0) {
  165. list_add_rcu(&stt->list, &kvm->arch.spapr_tce_tables);
  166. kvm_get_kvm(kvm);
  167. }
  168. mutex_unlock(&kvm->lock);
  169. if (ret >= 0)
  170. return ret;
  171. fail:
  172. for (i = 0; i < npages; i++)
  173. if (stt->pages[i])
  174. __free_page(stt->pages[i]);
  175. kfree(stt);
  176. fail_acct:
  177. kvmppc_account_memlimit(kvmppc_stt_pages(npages), false);
  178. return ret;
  179. }
  180. long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  181. unsigned long ioba, unsigned long tce)
  182. {
  183. struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
  184. long ret;
  185. /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
  186. /* liobn, ioba, tce); */
  187. if (!stt)
  188. return H_TOO_HARD;
  189. ret = kvmppc_ioba_validate(stt, ioba, 1);
  190. if (ret != H_SUCCESS)
  191. return ret;
  192. ret = kvmppc_tce_validate(stt, tce);
  193. if (ret != H_SUCCESS)
  194. return ret;
  195. kvmppc_tce_put(stt, ioba >> stt->page_shift, tce);
  196. return H_SUCCESS;
  197. }
  198. EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
  199. long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
  200. unsigned long liobn, unsigned long ioba,
  201. unsigned long tce_list, unsigned long npages)
  202. {
  203. struct kvmppc_spapr_tce_table *stt;
  204. long i, ret = H_SUCCESS, idx;
  205. unsigned long entry, ua = 0;
  206. u64 __user *tces;
  207. u64 tce;
  208. stt = kvmppc_find_table(vcpu, liobn);
  209. if (!stt)
  210. return H_TOO_HARD;
  211. entry = ioba >> stt->page_shift;
  212. /*
  213. * SPAPR spec says that the maximum size of the list is 512 TCEs
  214. * so the whole table fits in 4K page
  215. */
  216. if (npages > 512)
  217. return H_PARAMETER;
  218. if (tce_list & (SZ_4K - 1))
  219. return H_PARAMETER;
  220. ret = kvmppc_ioba_validate(stt, ioba, npages);
  221. if (ret != H_SUCCESS)
  222. return ret;
  223. idx = srcu_read_lock(&vcpu->kvm->srcu);
  224. if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, NULL)) {
  225. ret = H_TOO_HARD;
  226. goto unlock_exit;
  227. }
  228. tces = (u64 __user *) ua;
  229. for (i = 0; i < npages; ++i) {
  230. if (get_user(tce, tces + i)) {
  231. ret = H_TOO_HARD;
  232. goto unlock_exit;
  233. }
  234. tce = be64_to_cpu(tce);
  235. ret = kvmppc_tce_validate(stt, tce);
  236. if (ret != H_SUCCESS)
  237. goto unlock_exit;
  238. kvmppc_tce_put(stt, entry + i, tce);
  239. }
  240. unlock_exit:
  241. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  242. return ret;
  243. }
  244. EXPORT_SYMBOL_GPL(kvmppc_h_put_tce_indirect);
  245. long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
  246. unsigned long liobn, unsigned long ioba,
  247. unsigned long tce_value, unsigned long npages)
  248. {
  249. struct kvmppc_spapr_tce_table *stt;
  250. long i, ret;
  251. stt = kvmppc_find_table(vcpu, liobn);
  252. if (!stt)
  253. return H_TOO_HARD;
  254. ret = kvmppc_ioba_validate(stt, ioba, npages);
  255. if (ret != H_SUCCESS)
  256. return ret;
  257. /* Check permission bits only to allow userspace poison TCE for debug */
  258. if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
  259. return H_PARAMETER;
  260. for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
  261. kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
  262. return H_SUCCESS;
  263. }
  264. EXPORT_SYMBOL_GPL(kvmppc_h_stuff_tce);