book3s_64_vio_hv.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 <asm/tlbflush.h>
  29. #include <asm/kvm_ppc.h>
  30. #include <asm/kvm_book3s.h>
  31. #include <asm/book3s/64/mmu-hash.h>
  32. #include <asm/mmu_context.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. #include <asm/iommu.h>
  41. #define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
  42. /*
  43. * Finds a TCE table descriptor by LIOBN.
  44. *
  45. * WARNING: This will be called in real or virtual mode on HV KVM and virtual
  46. * mode on PR KVM
  47. */
  48. struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm_vcpu *vcpu,
  49. unsigned long liobn)
  50. {
  51. struct kvm *kvm = vcpu->kvm;
  52. struct kvmppc_spapr_tce_table *stt;
  53. list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
  54. if (stt->liobn == liobn)
  55. return stt;
  56. return NULL;
  57. }
  58. EXPORT_SYMBOL_GPL(kvmppc_find_table);
  59. /*
  60. * Validates IO address.
  61. *
  62. * WARNING: This will be called in real-mode on HV KVM and virtual
  63. * mode on PR KVM
  64. */
  65. long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt,
  66. unsigned long ioba, unsigned long npages)
  67. {
  68. unsigned long mask = (1ULL << stt->page_shift) - 1;
  69. unsigned long idx = ioba >> stt->page_shift;
  70. if ((ioba & mask) || (idx < stt->offset) ||
  71. (idx - stt->offset + npages > stt->size) ||
  72. (idx + npages < idx))
  73. return H_PARAMETER;
  74. return H_SUCCESS;
  75. }
  76. EXPORT_SYMBOL_GPL(kvmppc_ioba_validate);
  77. /*
  78. * Validates TCE address.
  79. * At the moment flags and page mask are validated.
  80. * As the host kernel does not access those addresses (just puts them
  81. * to the table and user space is supposed to process them), we can skip
  82. * checking other things (such as TCE is a guest RAM address or the page
  83. * was actually allocated).
  84. *
  85. * WARNING: This will be called in real-mode on HV KVM and virtual
  86. * mode on PR KVM
  87. */
  88. long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *stt, unsigned long tce)
  89. {
  90. unsigned long page_mask = ~((1ULL << stt->page_shift) - 1);
  91. unsigned long mask = ~(page_mask | TCE_PCI_WRITE | TCE_PCI_READ);
  92. if (tce & mask)
  93. return H_PARAMETER;
  94. return H_SUCCESS;
  95. }
  96. EXPORT_SYMBOL_GPL(kvmppc_tce_validate);
  97. /* Note on the use of page_address() in real mode,
  98. *
  99. * It is safe to use page_address() in real mode on ppc64 because
  100. * page_address() is always defined as lowmem_page_address()
  101. * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
  102. * operation and does not access page struct.
  103. *
  104. * Theoretically page_address() could be defined different
  105. * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
  106. * would have to be enabled.
  107. * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
  108. * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
  109. * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
  110. * is not expected to be enabled on ppc32, page_address()
  111. * is safe for ppc32 as well.
  112. *
  113. * WARNING: This will be called in real-mode on HV KVM and virtual
  114. * mode on PR KVM
  115. */
  116. static u64 *kvmppc_page_address(struct page *page)
  117. {
  118. #if defined(HASHED_PAGE_VIRTUAL) || defined(WANT_PAGE_VIRTUAL)
  119. #error TODO: fix to avoid page_address() here
  120. #endif
  121. return (u64 *) page_address(page);
  122. }
  123. /*
  124. * Handles TCE requests for emulated devices.
  125. * Puts guest TCE values to the table and expects user space to convert them.
  126. * Called in both real and virtual modes.
  127. * Cannot fail so kvmppc_tce_validate must be called before it.
  128. *
  129. * WARNING: This will be called in real-mode on HV KVM and virtual
  130. * mode on PR KVM
  131. */
  132. void kvmppc_tce_put(struct kvmppc_spapr_tce_table *stt,
  133. unsigned long idx, unsigned long tce)
  134. {
  135. struct page *page;
  136. u64 *tbl;
  137. idx -= stt->offset;
  138. page = stt->pages[idx / TCES_PER_PAGE];
  139. tbl = kvmppc_page_address(page);
  140. tbl[idx % TCES_PER_PAGE] = tce;
  141. }
  142. EXPORT_SYMBOL_GPL(kvmppc_tce_put);
  143. long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa,
  144. unsigned long *ua, unsigned long **prmap)
  145. {
  146. unsigned long gfn = gpa >> PAGE_SHIFT;
  147. struct kvm_memory_slot *memslot;
  148. memslot = search_memslots(kvm_memslots(kvm), gfn);
  149. if (!memslot)
  150. return -EINVAL;
  151. *ua = __gfn_to_hva_memslot(memslot, gfn) |
  152. (gpa & ~(PAGE_MASK | TCE_PCI_READ | TCE_PCI_WRITE));
  153. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  154. if (prmap)
  155. *prmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
  156. #endif
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_GPL(kvmppc_gpa_to_ua);
  160. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  161. long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  162. unsigned long ioba, unsigned long tce)
  163. {
  164. struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
  165. long ret;
  166. /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
  167. /* liobn, ioba, tce); */
  168. if (!stt)
  169. return H_TOO_HARD;
  170. ret = kvmppc_ioba_validate(stt, ioba, 1);
  171. if (ret != H_SUCCESS)
  172. return ret;
  173. ret = kvmppc_tce_validate(stt, tce);
  174. if (ret != H_SUCCESS)
  175. return ret;
  176. kvmppc_tce_put(stt, ioba >> stt->page_shift, tce);
  177. return H_SUCCESS;
  178. }
  179. static long kvmppc_rm_ua_to_hpa(struct kvm_vcpu *vcpu,
  180. unsigned long ua, unsigned long *phpa)
  181. {
  182. pte_t *ptep, pte;
  183. unsigned shift = 0;
  184. ptep = __find_linux_pte_or_hugepte(vcpu->arch.pgdir, ua, NULL, &shift);
  185. if (!ptep || !pte_present(*ptep))
  186. return -ENXIO;
  187. pte = *ptep;
  188. if (!shift)
  189. shift = PAGE_SHIFT;
  190. /* Avoid handling anything potentially complicated in realmode */
  191. if (shift > PAGE_SHIFT)
  192. return -EAGAIN;
  193. if (!pte_young(pte))
  194. return -EAGAIN;
  195. *phpa = (pte_pfn(pte) << PAGE_SHIFT) | (ua & ((1ULL << shift) - 1)) |
  196. (ua & ~PAGE_MASK);
  197. return 0;
  198. }
  199. long kvmppc_rm_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;
  205. unsigned long tces, entry, ua = 0;
  206. unsigned long *rmap = NULL;
  207. stt = kvmppc_find_table(vcpu, liobn);
  208. if (!stt)
  209. return H_TOO_HARD;
  210. entry = ioba >> stt->page_shift;
  211. /*
  212. * The spec says that the maximum size of the list is 512 TCEs
  213. * so the whole table addressed resides in 4K page
  214. */
  215. if (npages > 512)
  216. return H_PARAMETER;
  217. if (tce_list & (SZ_4K - 1))
  218. return H_PARAMETER;
  219. ret = kvmppc_ioba_validate(stt, ioba, npages);
  220. if (ret != H_SUCCESS)
  221. return ret;
  222. if (kvmppc_gpa_to_ua(vcpu->kvm, tce_list, &ua, &rmap))
  223. return H_TOO_HARD;
  224. rmap = (void *) vmalloc_to_phys(rmap);
  225. /*
  226. * Synchronize with the MMU notifier callbacks in
  227. * book3s_64_mmu_hv.c (kvm_unmap_hva_hv etc.).
  228. * While we have the rmap lock, code running on other CPUs
  229. * cannot finish unmapping the host real page that backs
  230. * this guest real page, so we are OK to access the host
  231. * real page.
  232. */
  233. lock_rmap(rmap);
  234. if (kvmppc_rm_ua_to_hpa(vcpu, ua, &tces)) {
  235. ret = H_TOO_HARD;
  236. goto unlock_exit;
  237. }
  238. for (i = 0; i < npages; ++i) {
  239. unsigned long tce = be64_to_cpu(((u64 *)tces)[i]);
  240. ret = kvmppc_tce_validate(stt, tce);
  241. if (ret != H_SUCCESS)
  242. goto unlock_exit;
  243. kvmppc_tce_put(stt, entry + i, tce);
  244. }
  245. unlock_exit:
  246. unlock_rmap(rmap);
  247. return ret;
  248. }
  249. long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
  250. unsigned long liobn, unsigned long ioba,
  251. unsigned long tce_value, unsigned long npages)
  252. {
  253. struct kvmppc_spapr_tce_table *stt;
  254. long i, ret;
  255. stt = kvmppc_find_table(vcpu, liobn);
  256. if (!stt)
  257. return H_TOO_HARD;
  258. ret = kvmppc_ioba_validate(stt, ioba, npages);
  259. if (ret != H_SUCCESS)
  260. return ret;
  261. /* Check permission bits only to allow userspace poison TCE for debug */
  262. if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
  263. return H_PARAMETER;
  264. for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
  265. kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
  266. return H_SUCCESS;
  267. }
  268. long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
  269. unsigned long ioba)
  270. {
  271. struct kvmppc_spapr_tce_table *stt = kvmppc_find_table(vcpu, liobn);
  272. long ret;
  273. unsigned long idx;
  274. struct page *page;
  275. u64 *tbl;
  276. if (!stt)
  277. return H_TOO_HARD;
  278. ret = kvmppc_ioba_validate(stt, ioba, 1);
  279. if (ret != H_SUCCESS)
  280. return ret;
  281. idx = (ioba >> stt->page_shift) - stt->offset;
  282. page = stt->pages[idx / TCES_PER_PAGE];
  283. tbl = (u64 *)page_address(page);
  284. vcpu->arch.gpr[4] = tbl[idx % TCES_PER_PAGE];
  285. return H_SUCCESS;
  286. }
  287. EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
  288. #endif /* KVM_BOOK3S_HV_POSSIBLE */