book3s_64_vio_hv.c 9.1 KB

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