book3s_64_mmu_hv.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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. */
  17. #include <linux/types.h>
  18. #include <linux/string.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/highmem.h>
  22. #include <linux/gfp.h>
  23. #include <linux/slab.h>
  24. #include <linux/hugetlb.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/srcu.h>
  27. #include <linux/anon_inodes.h>
  28. #include <linux/file.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/kvm_ppc.h>
  31. #include <asm/kvm_book3s.h>
  32. #include <asm/mmu-hash64.h>
  33. #include <asm/hvcall.h>
  34. #include <asm/synch.h>
  35. #include <asm/ppc-opcode.h>
  36. #include <asm/cputable.h>
  37. /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
  38. #define MAX_LPID_970 63
  39. /* Power architecture requires HPT is at least 256kB */
  40. #define PPC_MIN_HPT_ORDER 18
  41. static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
  42. long pte_index, unsigned long pteh,
  43. unsigned long ptel, unsigned long *pte_idx_ret);
  44. static void kvmppc_rmap_reset(struct kvm *kvm);
  45. long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp)
  46. {
  47. unsigned long hpt;
  48. struct revmap_entry *rev;
  49. struct kvmppc_linear_info *li;
  50. long order = kvm_hpt_order;
  51. if (htab_orderp) {
  52. order = *htab_orderp;
  53. if (order < PPC_MIN_HPT_ORDER)
  54. order = PPC_MIN_HPT_ORDER;
  55. }
  56. /*
  57. * If the user wants a different size from default,
  58. * try first to allocate it from the kernel page allocator.
  59. */
  60. hpt = 0;
  61. if (order != kvm_hpt_order) {
  62. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
  63. __GFP_NOWARN, order - PAGE_SHIFT);
  64. if (!hpt)
  65. --order;
  66. }
  67. /* Next try to allocate from the preallocated pool */
  68. if (!hpt) {
  69. li = kvm_alloc_hpt();
  70. if (li) {
  71. hpt = (ulong)li->base_virt;
  72. kvm->arch.hpt_li = li;
  73. order = kvm_hpt_order;
  74. }
  75. }
  76. /* Lastly try successively smaller sizes from the page allocator */
  77. while (!hpt && order > PPC_MIN_HPT_ORDER) {
  78. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
  79. __GFP_NOWARN, order - PAGE_SHIFT);
  80. if (!hpt)
  81. --order;
  82. }
  83. if (!hpt)
  84. return -ENOMEM;
  85. kvm->arch.hpt_virt = hpt;
  86. kvm->arch.hpt_order = order;
  87. /* HPTEs are 2**4 bytes long */
  88. kvm->arch.hpt_npte = 1ul << (order - 4);
  89. /* 128 (2**7) bytes in each HPTEG */
  90. kvm->arch.hpt_mask = (1ul << (order - 7)) - 1;
  91. /* Allocate reverse map array */
  92. rev = vmalloc(sizeof(struct revmap_entry) * kvm->arch.hpt_npte);
  93. if (!rev) {
  94. pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
  95. goto out_freehpt;
  96. }
  97. kvm->arch.revmap = rev;
  98. kvm->arch.sdr1 = __pa(hpt) | (order - 18);
  99. pr_info("KVM guest htab at %lx (order %ld), LPID %x\n",
  100. hpt, order, kvm->arch.lpid);
  101. if (htab_orderp)
  102. *htab_orderp = order;
  103. return 0;
  104. out_freehpt:
  105. if (kvm->arch.hpt_li)
  106. kvm_release_hpt(kvm->arch.hpt_li);
  107. else
  108. free_pages(hpt, order - PAGE_SHIFT);
  109. return -ENOMEM;
  110. }
  111. long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp)
  112. {
  113. long err = -EBUSY;
  114. long order;
  115. mutex_lock(&kvm->lock);
  116. if (kvm->arch.rma_setup_done) {
  117. kvm->arch.rma_setup_done = 0;
  118. /* order rma_setup_done vs. vcpus_running */
  119. smp_mb();
  120. if (atomic_read(&kvm->arch.vcpus_running)) {
  121. kvm->arch.rma_setup_done = 1;
  122. goto out;
  123. }
  124. }
  125. if (kvm->arch.hpt_virt) {
  126. order = kvm->arch.hpt_order;
  127. /* Set the entire HPT to 0, i.e. invalid HPTEs */
  128. memset((void *)kvm->arch.hpt_virt, 0, 1ul << order);
  129. /*
  130. * Reset all the reverse-mapping chains for all memslots
  131. */
  132. kvmppc_rmap_reset(kvm);
  133. /*
  134. * Set the whole last_vcpu array to an invalid vcpu number.
  135. * This ensures that each vcpu will flush its TLB on next entry.
  136. */
  137. memset(kvm->arch.last_vcpu, 0xff, sizeof(kvm->arch.last_vcpu));
  138. *htab_orderp = order;
  139. err = 0;
  140. } else {
  141. err = kvmppc_alloc_hpt(kvm, htab_orderp);
  142. order = *htab_orderp;
  143. }
  144. out:
  145. mutex_unlock(&kvm->lock);
  146. return err;
  147. }
  148. void kvmppc_free_hpt(struct kvm *kvm)
  149. {
  150. kvmppc_free_lpid(kvm->arch.lpid);
  151. vfree(kvm->arch.revmap);
  152. if (kvm->arch.hpt_li)
  153. kvm_release_hpt(kvm->arch.hpt_li);
  154. else
  155. free_pages(kvm->arch.hpt_virt,
  156. kvm->arch.hpt_order - PAGE_SHIFT);
  157. }
  158. /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
  159. static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
  160. {
  161. return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
  162. }
  163. /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
  164. static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
  165. {
  166. return (pgsize == 0x10000) ? 0x1000 : 0;
  167. }
  168. void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
  169. unsigned long porder)
  170. {
  171. unsigned long i;
  172. unsigned long npages;
  173. unsigned long hp_v, hp_r;
  174. unsigned long addr, hash;
  175. unsigned long psize;
  176. unsigned long hp0, hp1;
  177. unsigned long idx_ret;
  178. long ret;
  179. struct kvm *kvm = vcpu->kvm;
  180. psize = 1ul << porder;
  181. npages = memslot->npages >> (porder - PAGE_SHIFT);
  182. /* VRMA can't be > 1TB */
  183. if (npages > 1ul << (40 - porder))
  184. npages = 1ul << (40 - porder);
  185. /* Can't use more than 1 HPTE per HPTEG */
  186. if (npages > kvm->arch.hpt_mask + 1)
  187. npages = kvm->arch.hpt_mask + 1;
  188. hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
  189. HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
  190. hp1 = hpte1_pgsize_encoding(psize) |
  191. HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
  192. for (i = 0; i < npages; ++i) {
  193. addr = i << porder;
  194. /* can't use hpt_hash since va > 64 bits */
  195. hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & kvm->arch.hpt_mask;
  196. /*
  197. * We assume that the hash table is empty and no
  198. * vcpus are using it at this stage. Since we create
  199. * at most one HPTE per HPTEG, we just assume entry 7
  200. * is available and use it.
  201. */
  202. hash = (hash << 3) + 7;
  203. hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
  204. hp_r = hp1 | addr;
  205. ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
  206. &idx_ret);
  207. if (ret != H_SUCCESS) {
  208. pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
  209. addr, ret);
  210. break;
  211. }
  212. }
  213. }
  214. int kvmppc_mmu_hv_init(void)
  215. {
  216. unsigned long host_lpid, rsvd_lpid;
  217. if (!cpu_has_feature(CPU_FTR_HVMODE))
  218. return -EINVAL;
  219. /* POWER7 has 10-bit LPIDs, PPC970 and e500mc have 6-bit LPIDs */
  220. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  221. host_lpid = mfspr(SPRN_LPID); /* POWER7 */
  222. rsvd_lpid = LPID_RSVD;
  223. } else {
  224. host_lpid = 0; /* PPC970 */
  225. rsvd_lpid = MAX_LPID_970;
  226. }
  227. kvmppc_init_lpid(rsvd_lpid + 1);
  228. kvmppc_claim_lpid(host_lpid);
  229. /* rsvd_lpid is reserved for use in partition switching */
  230. kvmppc_claim_lpid(rsvd_lpid);
  231. return 0;
  232. }
  233. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  234. {
  235. }
  236. static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
  237. {
  238. kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
  239. }
  240. /*
  241. * This is called to get a reference to a guest page if there isn't
  242. * one already in the memslot->arch.slot_phys[] array.
  243. */
  244. static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
  245. struct kvm_memory_slot *memslot,
  246. unsigned long psize)
  247. {
  248. unsigned long start;
  249. long np, err;
  250. struct page *page, *hpage, *pages[1];
  251. unsigned long s, pgsize;
  252. unsigned long *physp;
  253. unsigned int is_io, got, pgorder;
  254. struct vm_area_struct *vma;
  255. unsigned long pfn, i, npages;
  256. physp = memslot->arch.slot_phys;
  257. if (!physp)
  258. return -EINVAL;
  259. if (physp[gfn - memslot->base_gfn])
  260. return 0;
  261. is_io = 0;
  262. got = 0;
  263. page = NULL;
  264. pgsize = psize;
  265. err = -EINVAL;
  266. start = gfn_to_hva_memslot(memslot, gfn);
  267. /* Instantiate and get the page we want access to */
  268. np = get_user_pages_fast(start, 1, 1, pages);
  269. if (np != 1) {
  270. /* Look up the vma for the page */
  271. down_read(&current->mm->mmap_sem);
  272. vma = find_vma(current->mm, start);
  273. if (!vma || vma->vm_start > start ||
  274. start + psize > vma->vm_end ||
  275. !(vma->vm_flags & VM_PFNMAP))
  276. goto up_err;
  277. is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
  278. pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  279. /* check alignment of pfn vs. requested page size */
  280. if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1)))
  281. goto up_err;
  282. up_read(&current->mm->mmap_sem);
  283. } else {
  284. page = pages[0];
  285. got = KVMPPC_GOT_PAGE;
  286. /* See if this is a large page */
  287. s = PAGE_SIZE;
  288. if (PageHuge(page)) {
  289. hpage = compound_head(page);
  290. s <<= compound_order(hpage);
  291. /* Get the whole large page if slot alignment is ok */
  292. if (s > psize && slot_is_aligned(memslot, s) &&
  293. !(memslot->userspace_addr & (s - 1))) {
  294. start &= ~(s - 1);
  295. pgsize = s;
  296. get_page(hpage);
  297. put_page(page);
  298. page = hpage;
  299. }
  300. }
  301. if (s < psize)
  302. goto out;
  303. pfn = page_to_pfn(page);
  304. }
  305. npages = pgsize >> PAGE_SHIFT;
  306. pgorder = __ilog2(npages);
  307. physp += (gfn - memslot->base_gfn) & ~(npages - 1);
  308. spin_lock(&kvm->arch.slot_phys_lock);
  309. for (i = 0; i < npages; ++i) {
  310. if (!physp[i]) {
  311. physp[i] = ((pfn + i) << PAGE_SHIFT) +
  312. got + is_io + pgorder;
  313. got = 0;
  314. }
  315. }
  316. spin_unlock(&kvm->arch.slot_phys_lock);
  317. err = 0;
  318. out:
  319. if (got)
  320. put_page(page);
  321. return err;
  322. up_err:
  323. up_read(&current->mm->mmap_sem);
  324. return err;
  325. }
  326. long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
  327. long pte_index, unsigned long pteh,
  328. unsigned long ptel, unsigned long *pte_idx_ret)
  329. {
  330. unsigned long psize, gpa, gfn;
  331. struct kvm_memory_slot *memslot;
  332. long ret;
  333. if (kvm->arch.using_mmu_notifiers)
  334. goto do_insert;
  335. psize = hpte_page_size(pteh, ptel);
  336. if (!psize)
  337. return H_PARAMETER;
  338. pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
  339. /* Find the memslot (if any) for this address */
  340. gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
  341. gfn = gpa >> PAGE_SHIFT;
  342. memslot = gfn_to_memslot(kvm, gfn);
  343. if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
  344. if (!slot_is_aligned(memslot, psize))
  345. return H_PARAMETER;
  346. if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
  347. return H_PARAMETER;
  348. }
  349. do_insert:
  350. /* Protect linux PTE lookup from page table destruction */
  351. rcu_read_lock_sched(); /* this disables preemption too */
  352. ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
  353. current->mm->pgd, false, pte_idx_ret);
  354. rcu_read_unlock_sched();
  355. if (ret == H_TOO_HARD) {
  356. /* this can't happen */
  357. pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
  358. ret = H_RESOURCE; /* or something */
  359. }
  360. return ret;
  361. }
  362. /*
  363. * We come here on a H_ENTER call from the guest when we are not
  364. * using mmu notifiers and we don't have the requested page pinned
  365. * already.
  366. */
  367. long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
  368. long pte_index, unsigned long pteh,
  369. unsigned long ptel)
  370. {
  371. return kvmppc_virtmode_do_h_enter(vcpu->kvm, flags, pte_index,
  372. pteh, ptel, &vcpu->arch.gpr[4]);
  373. }
  374. static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
  375. gva_t eaddr)
  376. {
  377. u64 mask;
  378. int i;
  379. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  380. if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
  381. continue;
  382. if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
  383. mask = ESID_MASK_1T;
  384. else
  385. mask = ESID_MASK;
  386. if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
  387. return &vcpu->arch.slb[i];
  388. }
  389. return NULL;
  390. }
  391. static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
  392. unsigned long ea)
  393. {
  394. unsigned long ra_mask;
  395. ra_mask = hpte_page_size(v, r) - 1;
  396. return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
  397. }
  398. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  399. struct kvmppc_pte *gpte, bool data)
  400. {
  401. struct kvm *kvm = vcpu->kvm;
  402. struct kvmppc_slb *slbe;
  403. unsigned long slb_v;
  404. unsigned long pp, key;
  405. unsigned long v, gr;
  406. unsigned long *hptep;
  407. int index;
  408. int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
  409. /* Get SLB entry */
  410. if (virtmode) {
  411. slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
  412. if (!slbe)
  413. return -EINVAL;
  414. slb_v = slbe->origv;
  415. } else {
  416. /* real mode access */
  417. slb_v = vcpu->kvm->arch.vrma_slb_v;
  418. }
  419. /* Find the HPTE in the hash table */
  420. index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
  421. HPTE_V_VALID | HPTE_V_ABSENT);
  422. if (index < 0)
  423. return -ENOENT;
  424. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  425. v = hptep[0] & ~HPTE_V_HVLOCK;
  426. gr = kvm->arch.revmap[index].guest_rpte;
  427. /* Unlock the HPTE */
  428. asm volatile("lwsync" : : : "memory");
  429. hptep[0] = v;
  430. gpte->eaddr = eaddr;
  431. gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
  432. /* Get PP bits and key for permission check */
  433. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  434. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  435. key &= slb_v;
  436. /* Calculate permissions */
  437. gpte->may_read = hpte_read_permission(pp, key);
  438. gpte->may_write = hpte_write_permission(pp, key);
  439. gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
  440. /* Storage key permission check for POWER7 */
  441. if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
  442. int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
  443. if (amrfield & 1)
  444. gpte->may_read = 0;
  445. if (amrfield & 2)
  446. gpte->may_write = 0;
  447. }
  448. /* Get the guest physical address */
  449. gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
  450. return 0;
  451. }
  452. /*
  453. * Quick test for whether an instruction is a load or a store.
  454. * If the instruction is a load or a store, then this will indicate
  455. * which it is, at least on server processors. (Embedded processors
  456. * have some external PID instructions that don't follow the rule
  457. * embodied here.) If the instruction isn't a load or store, then
  458. * this doesn't return anything useful.
  459. */
  460. static int instruction_is_store(unsigned int instr)
  461. {
  462. unsigned int mask;
  463. mask = 0x10000000;
  464. if ((instr & 0xfc000000) == 0x7c000000)
  465. mask = 0x100; /* major opcode 31 */
  466. return (instr & mask) != 0;
  467. }
  468. static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
  469. unsigned long gpa, gva_t ea, int is_store)
  470. {
  471. int ret;
  472. u32 last_inst;
  473. unsigned long srr0 = kvmppc_get_pc(vcpu);
  474. /* We try to load the last instruction. We don't let
  475. * emulate_instruction do it as it doesn't check what
  476. * kvmppc_ld returns.
  477. * If we fail, we just return to the guest and try executing it again.
  478. */
  479. if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) {
  480. ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
  481. if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED)
  482. return RESUME_GUEST;
  483. vcpu->arch.last_inst = last_inst;
  484. }
  485. /*
  486. * WARNING: We do not know for sure whether the instruction we just
  487. * read from memory is the same that caused the fault in the first
  488. * place. If the instruction we read is neither an load or a store,
  489. * then it can't access memory, so we don't need to worry about
  490. * enforcing access permissions. So, assuming it is a load or
  491. * store, we just check that its direction (load or store) is
  492. * consistent with the original fault, since that's what we
  493. * checked the access permissions against. If there is a mismatch
  494. * we just return and retry the instruction.
  495. */
  496. if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
  497. return RESUME_GUEST;
  498. /*
  499. * Emulated accesses are emulated by looking at the hash for
  500. * translation once, then performing the access later. The
  501. * translation could be invalidated in the meantime in which
  502. * point performing the subsequent memory access on the old
  503. * physical address could possibly be a security hole for the
  504. * guest (but not the host).
  505. *
  506. * This is less of an issue for MMIO stores since they aren't
  507. * globally visible. It could be an issue for MMIO loads to
  508. * a certain extent but we'll ignore it for now.
  509. */
  510. vcpu->arch.paddr_accessed = gpa;
  511. vcpu->arch.vaddr_accessed = ea;
  512. return kvmppc_emulate_mmio(run, vcpu);
  513. }
  514. int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
  515. unsigned long ea, unsigned long dsisr)
  516. {
  517. struct kvm *kvm = vcpu->kvm;
  518. unsigned long *hptep, hpte[3], r;
  519. unsigned long mmu_seq, psize, pte_size;
  520. unsigned long gpa, gfn, hva, pfn;
  521. struct kvm_memory_slot *memslot;
  522. unsigned long *rmap;
  523. struct revmap_entry *rev;
  524. struct page *page, *pages[1];
  525. long index, ret, npages;
  526. unsigned long is_io;
  527. unsigned int writing, write_ok;
  528. struct vm_area_struct *vma;
  529. unsigned long rcbits;
  530. /*
  531. * Real-mode code has already searched the HPT and found the
  532. * entry we're interested in. Lock the entry and check that
  533. * it hasn't changed. If it has, just return and re-execute the
  534. * instruction.
  535. */
  536. if (ea != vcpu->arch.pgfault_addr)
  537. return RESUME_GUEST;
  538. index = vcpu->arch.pgfault_index;
  539. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  540. rev = &kvm->arch.revmap[index];
  541. preempt_disable();
  542. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  543. cpu_relax();
  544. hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
  545. hpte[1] = hptep[1];
  546. hpte[2] = r = rev->guest_rpte;
  547. asm volatile("lwsync" : : : "memory");
  548. hptep[0] = hpte[0];
  549. preempt_enable();
  550. if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
  551. hpte[1] != vcpu->arch.pgfault_hpte[1])
  552. return RESUME_GUEST;
  553. /* Translate the logical address and get the page */
  554. psize = hpte_page_size(hpte[0], r);
  555. gpa = (r & HPTE_R_RPN & ~(psize - 1)) | (ea & (psize - 1));
  556. gfn = gpa >> PAGE_SHIFT;
  557. memslot = gfn_to_memslot(kvm, gfn);
  558. /* No memslot means it's an emulated MMIO region */
  559. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  560. return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
  561. dsisr & DSISR_ISSTORE);
  562. if (!kvm->arch.using_mmu_notifiers)
  563. return -EFAULT; /* should never get here */
  564. /* used to check for invalidations in progress */
  565. mmu_seq = kvm->mmu_notifier_seq;
  566. smp_rmb();
  567. is_io = 0;
  568. pfn = 0;
  569. page = NULL;
  570. pte_size = PAGE_SIZE;
  571. writing = (dsisr & DSISR_ISSTORE) != 0;
  572. /* If writing != 0, then the HPTE must allow writing, if we get here */
  573. write_ok = writing;
  574. hva = gfn_to_hva_memslot(memslot, gfn);
  575. npages = get_user_pages_fast(hva, 1, writing, pages);
  576. if (npages < 1) {
  577. /* Check if it's an I/O mapping */
  578. down_read(&current->mm->mmap_sem);
  579. vma = find_vma(current->mm, hva);
  580. if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
  581. (vma->vm_flags & VM_PFNMAP)) {
  582. pfn = vma->vm_pgoff +
  583. ((hva - vma->vm_start) >> PAGE_SHIFT);
  584. pte_size = psize;
  585. is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
  586. write_ok = vma->vm_flags & VM_WRITE;
  587. }
  588. up_read(&current->mm->mmap_sem);
  589. if (!pfn)
  590. return -EFAULT;
  591. } else {
  592. page = pages[0];
  593. if (PageHuge(page)) {
  594. page = compound_head(page);
  595. pte_size <<= compound_order(page);
  596. }
  597. /* if the guest wants write access, see if that is OK */
  598. if (!writing && hpte_is_writable(r)) {
  599. pte_t *ptep, pte;
  600. /*
  601. * We need to protect against page table destruction
  602. * while looking up and updating the pte.
  603. */
  604. rcu_read_lock_sched();
  605. ptep = find_linux_pte_or_hugepte(current->mm->pgd,
  606. hva, NULL);
  607. if (ptep && pte_present(*ptep)) {
  608. pte = kvmppc_read_update_linux_pte(ptep, 1);
  609. if (pte_write(pte))
  610. write_ok = 1;
  611. }
  612. rcu_read_unlock_sched();
  613. }
  614. pfn = page_to_pfn(page);
  615. }
  616. ret = -EFAULT;
  617. if (psize > pte_size)
  618. goto out_put;
  619. /* Check WIMG vs. the actual page we're accessing */
  620. if (!hpte_cache_flags_ok(r, is_io)) {
  621. if (is_io)
  622. return -EFAULT;
  623. /*
  624. * Allow guest to map emulated device memory as
  625. * uncacheable, but actually make it cacheable.
  626. */
  627. r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
  628. }
  629. /* Set the HPTE to point to pfn */
  630. r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT);
  631. if (hpte_is_writable(r) && !write_ok)
  632. r = hpte_make_readonly(r);
  633. ret = RESUME_GUEST;
  634. preempt_disable();
  635. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  636. cpu_relax();
  637. if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
  638. rev->guest_rpte != hpte[2])
  639. /* HPTE has been changed under us; let the guest retry */
  640. goto out_unlock;
  641. hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  642. rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
  643. lock_rmap(rmap);
  644. /* Check if we might have been invalidated; let the guest retry if so */
  645. ret = RESUME_GUEST;
  646. if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
  647. unlock_rmap(rmap);
  648. goto out_unlock;
  649. }
  650. /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
  651. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  652. r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  653. if (hptep[0] & HPTE_V_VALID) {
  654. /* HPTE was previously valid, so we need to invalidate it */
  655. unlock_rmap(rmap);
  656. hptep[0] |= HPTE_V_ABSENT;
  657. kvmppc_invalidate_hpte(kvm, hptep, index);
  658. /* don't lose previous R and C bits */
  659. r |= hptep[1] & (HPTE_R_R | HPTE_R_C);
  660. } else {
  661. kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
  662. }
  663. hptep[1] = r;
  664. eieio();
  665. hptep[0] = hpte[0];
  666. asm volatile("ptesync" : : : "memory");
  667. preempt_enable();
  668. if (page && hpte_is_writable(r))
  669. SetPageDirty(page);
  670. out_put:
  671. if (page) {
  672. /*
  673. * We drop pages[0] here, not page because page might
  674. * have been set to the head page of a compound, but
  675. * we have to drop the reference on the correct tail
  676. * page to match the get inside gup()
  677. */
  678. put_page(pages[0]);
  679. }
  680. return ret;
  681. out_unlock:
  682. hptep[0] &= ~HPTE_V_HVLOCK;
  683. preempt_enable();
  684. goto out_put;
  685. }
  686. static void kvmppc_rmap_reset(struct kvm *kvm)
  687. {
  688. struct kvm_memslots *slots;
  689. struct kvm_memory_slot *memslot;
  690. int srcu_idx;
  691. srcu_idx = srcu_read_lock(&kvm->srcu);
  692. slots = kvm->memslots;
  693. kvm_for_each_memslot(memslot, slots) {
  694. /*
  695. * This assumes it is acceptable to lose reference and
  696. * change bits across a reset.
  697. */
  698. memset(memslot->arch.rmap, 0,
  699. memslot->npages * sizeof(*memslot->arch.rmap));
  700. }
  701. srcu_read_unlock(&kvm->srcu, srcu_idx);
  702. }
  703. static int kvm_handle_hva_range(struct kvm *kvm,
  704. unsigned long start,
  705. unsigned long end,
  706. int (*handler)(struct kvm *kvm,
  707. unsigned long *rmapp,
  708. unsigned long gfn))
  709. {
  710. int ret;
  711. int retval = 0;
  712. struct kvm_memslots *slots;
  713. struct kvm_memory_slot *memslot;
  714. slots = kvm_memslots(kvm);
  715. kvm_for_each_memslot(memslot, slots) {
  716. unsigned long hva_start, hva_end;
  717. gfn_t gfn, gfn_end;
  718. hva_start = max(start, memslot->userspace_addr);
  719. hva_end = min(end, memslot->userspace_addr +
  720. (memslot->npages << PAGE_SHIFT));
  721. if (hva_start >= hva_end)
  722. continue;
  723. /*
  724. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  725. * {gfn, gfn+1, ..., gfn_end-1}.
  726. */
  727. gfn = hva_to_gfn_memslot(hva_start, memslot);
  728. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  729. for (; gfn < gfn_end; ++gfn) {
  730. gfn_t gfn_offset = gfn - memslot->base_gfn;
  731. ret = handler(kvm, &memslot->arch.rmap[gfn_offset], gfn);
  732. retval |= ret;
  733. }
  734. }
  735. return retval;
  736. }
  737. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  738. int (*handler)(struct kvm *kvm, unsigned long *rmapp,
  739. unsigned long gfn))
  740. {
  741. return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
  742. }
  743. static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
  744. unsigned long gfn)
  745. {
  746. struct revmap_entry *rev = kvm->arch.revmap;
  747. unsigned long h, i, j;
  748. unsigned long *hptep;
  749. unsigned long ptel, psize, rcbits;
  750. for (;;) {
  751. lock_rmap(rmapp);
  752. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  753. unlock_rmap(rmapp);
  754. break;
  755. }
  756. /*
  757. * To avoid an ABBA deadlock with the HPTE lock bit,
  758. * we can't spin on the HPTE lock while holding the
  759. * rmap chain lock.
  760. */
  761. i = *rmapp & KVMPPC_RMAP_INDEX;
  762. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  763. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  764. /* unlock rmap before spinning on the HPTE lock */
  765. unlock_rmap(rmapp);
  766. while (hptep[0] & HPTE_V_HVLOCK)
  767. cpu_relax();
  768. continue;
  769. }
  770. j = rev[i].forw;
  771. if (j == i) {
  772. /* chain is now empty */
  773. *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
  774. } else {
  775. /* remove i from chain */
  776. h = rev[i].back;
  777. rev[h].forw = j;
  778. rev[j].back = h;
  779. rev[i].forw = rev[i].back = i;
  780. *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
  781. }
  782. /* Now check and modify the HPTE */
  783. ptel = rev[i].guest_rpte;
  784. psize = hpte_page_size(hptep[0], ptel);
  785. if ((hptep[0] & HPTE_V_VALID) &&
  786. hpte_rpn(ptel, psize) == gfn) {
  787. if (kvm->arch.using_mmu_notifiers)
  788. hptep[0] |= HPTE_V_ABSENT;
  789. kvmppc_invalidate_hpte(kvm, hptep, i);
  790. /* Harvest R and C */
  791. rcbits = hptep[1] & (HPTE_R_R | HPTE_R_C);
  792. *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
  793. rev[i].guest_rpte = ptel | rcbits;
  794. }
  795. unlock_rmap(rmapp);
  796. hptep[0] &= ~HPTE_V_HVLOCK;
  797. }
  798. return 0;
  799. }
  800. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  801. {
  802. if (kvm->arch.using_mmu_notifiers)
  803. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  804. return 0;
  805. }
  806. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  807. {
  808. if (kvm->arch.using_mmu_notifiers)
  809. kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
  810. return 0;
  811. }
  812. void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
  813. {
  814. unsigned long *rmapp;
  815. unsigned long gfn;
  816. unsigned long n;
  817. rmapp = memslot->arch.rmap;
  818. gfn = memslot->base_gfn;
  819. for (n = memslot->npages; n; --n) {
  820. /*
  821. * Testing the present bit without locking is OK because
  822. * the memslot has been marked invalid already, and hence
  823. * no new HPTEs referencing this page can be created,
  824. * thus the present bit can't go from 0 to 1.
  825. */
  826. if (*rmapp & KVMPPC_RMAP_PRESENT)
  827. kvm_unmap_rmapp(kvm, rmapp, gfn);
  828. ++rmapp;
  829. ++gfn;
  830. }
  831. }
  832. static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  833. unsigned long gfn)
  834. {
  835. struct revmap_entry *rev = kvm->arch.revmap;
  836. unsigned long head, i, j;
  837. unsigned long *hptep;
  838. int ret = 0;
  839. retry:
  840. lock_rmap(rmapp);
  841. if (*rmapp & KVMPPC_RMAP_REFERENCED) {
  842. *rmapp &= ~KVMPPC_RMAP_REFERENCED;
  843. ret = 1;
  844. }
  845. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  846. unlock_rmap(rmapp);
  847. return ret;
  848. }
  849. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  850. do {
  851. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  852. j = rev[i].forw;
  853. /* If this HPTE isn't referenced, ignore it */
  854. if (!(hptep[1] & HPTE_R_R))
  855. continue;
  856. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  857. /* unlock rmap before spinning on the HPTE lock */
  858. unlock_rmap(rmapp);
  859. while (hptep[0] & HPTE_V_HVLOCK)
  860. cpu_relax();
  861. goto retry;
  862. }
  863. /* Now check and modify the HPTE */
  864. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
  865. kvmppc_clear_ref_hpte(kvm, hptep, i);
  866. rev[i].guest_rpte |= HPTE_R_R;
  867. ret = 1;
  868. }
  869. hptep[0] &= ~HPTE_V_HVLOCK;
  870. } while ((i = j) != head);
  871. unlock_rmap(rmapp);
  872. return ret;
  873. }
  874. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  875. {
  876. if (!kvm->arch.using_mmu_notifiers)
  877. return 0;
  878. return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
  879. }
  880. static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  881. unsigned long gfn)
  882. {
  883. struct revmap_entry *rev = kvm->arch.revmap;
  884. unsigned long head, i, j;
  885. unsigned long *hp;
  886. int ret = 1;
  887. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  888. return 1;
  889. lock_rmap(rmapp);
  890. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  891. goto out;
  892. if (*rmapp & KVMPPC_RMAP_PRESENT) {
  893. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  894. do {
  895. hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
  896. j = rev[i].forw;
  897. if (hp[1] & HPTE_R_R)
  898. goto out;
  899. } while ((i = j) != head);
  900. }
  901. ret = 0;
  902. out:
  903. unlock_rmap(rmapp);
  904. return ret;
  905. }
  906. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  907. {
  908. if (!kvm->arch.using_mmu_notifiers)
  909. return 0;
  910. return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
  911. }
  912. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  913. {
  914. if (!kvm->arch.using_mmu_notifiers)
  915. return;
  916. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  917. }
  918. static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp)
  919. {
  920. struct revmap_entry *rev = kvm->arch.revmap;
  921. unsigned long head, i, j;
  922. unsigned long *hptep;
  923. int ret = 0;
  924. retry:
  925. lock_rmap(rmapp);
  926. if (*rmapp & KVMPPC_RMAP_CHANGED) {
  927. *rmapp &= ~KVMPPC_RMAP_CHANGED;
  928. ret = 1;
  929. }
  930. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  931. unlock_rmap(rmapp);
  932. return ret;
  933. }
  934. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  935. do {
  936. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  937. j = rev[i].forw;
  938. if (!(hptep[1] & HPTE_R_C))
  939. continue;
  940. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  941. /* unlock rmap before spinning on the HPTE lock */
  942. unlock_rmap(rmapp);
  943. while (hptep[0] & HPTE_V_HVLOCK)
  944. cpu_relax();
  945. goto retry;
  946. }
  947. /* Now check and modify the HPTE */
  948. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
  949. /* need to make it temporarily absent to clear C */
  950. hptep[0] |= HPTE_V_ABSENT;
  951. kvmppc_invalidate_hpte(kvm, hptep, i);
  952. hptep[1] &= ~HPTE_R_C;
  953. eieio();
  954. hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  955. rev[i].guest_rpte |= HPTE_R_C;
  956. ret = 1;
  957. }
  958. hptep[0] &= ~HPTE_V_HVLOCK;
  959. } while ((i = j) != head);
  960. unlock_rmap(rmapp);
  961. return ret;
  962. }
  963. long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot,
  964. unsigned long *map)
  965. {
  966. unsigned long i;
  967. unsigned long *rmapp;
  968. preempt_disable();
  969. rmapp = memslot->arch.rmap;
  970. for (i = 0; i < memslot->npages; ++i) {
  971. if (kvm_test_clear_dirty(kvm, rmapp) && map)
  972. __set_bit_le(i, map);
  973. ++rmapp;
  974. }
  975. preempt_enable();
  976. return 0;
  977. }
  978. void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
  979. unsigned long *nb_ret)
  980. {
  981. struct kvm_memory_slot *memslot;
  982. unsigned long gfn = gpa >> PAGE_SHIFT;
  983. struct page *page, *pages[1];
  984. int npages;
  985. unsigned long hva, psize, offset;
  986. unsigned long pa;
  987. unsigned long *physp;
  988. int srcu_idx;
  989. srcu_idx = srcu_read_lock(&kvm->srcu);
  990. memslot = gfn_to_memslot(kvm, gfn);
  991. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  992. goto err;
  993. if (!kvm->arch.using_mmu_notifiers) {
  994. physp = memslot->arch.slot_phys;
  995. if (!physp)
  996. goto err;
  997. physp += gfn - memslot->base_gfn;
  998. pa = *physp;
  999. if (!pa) {
  1000. if (kvmppc_get_guest_page(kvm, gfn, memslot,
  1001. PAGE_SIZE) < 0)
  1002. goto err;
  1003. pa = *physp;
  1004. }
  1005. page = pfn_to_page(pa >> PAGE_SHIFT);
  1006. get_page(page);
  1007. } else {
  1008. hva = gfn_to_hva_memslot(memslot, gfn);
  1009. npages = get_user_pages_fast(hva, 1, 1, pages);
  1010. if (npages < 1)
  1011. goto err;
  1012. page = pages[0];
  1013. }
  1014. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1015. psize = PAGE_SIZE;
  1016. if (PageHuge(page)) {
  1017. page = compound_head(page);
  1018. psize <<= compound_order(page);
  1019. }
  1020. offset = gpa & (psize - 1);
  1021. if (nb_ret)
  1022. *nb_ret = psize - offset;
  1023. return page_address(page) + offset;
  1024. err:
  1025. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1026. return NULL;
  1027. }
  1028. void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
  1029. {
  1030. struct page *page = virt_to_page(va);
  1031. put_page(page);
  1032. }
  1033. /*
  1034. * Functions for reading and writing the hash table via reads and
  1035. * writes on a file descriptor.
  1036. *
  1037. * Reads return the guest view of the hash table, which has to be
  1038. * pieced together from the real hash table and the guest_rpte
  1039. * values in the revmap array.
  1040. *
  1041. * On writes, each HPTE written is considered in turn, and if it
  1042. * is valid, it is written to the HPT as if an H_ENTER with the
  1043. * exact flag set was done. When the invalid count is non-zero
  1044. * in the header written to the stream, the kernel will make
  1045. * sure that that many HPTEs are invalid, and invalidate them
  1046. * if not.
  1047. */
  1048. struct kvm_htab_ctx {
  1049. unsigned long index;
  1050. unsigned long flags;
  1051. struct kvm *kvm;
  1052. int first_pass;
  1053. };
  1054. #define HPTE_SIZE (2 * sizeof(unsigned long))
  1055. static long record_hpte(unsigned long flags, unsigned long *hptp,
  1056. unsigned long *hpte, struct revmap_entry *revp,
  1057. int want_valid, int first_pass)
  1058. {
  1059. unsigned long v, r;
  1060. int ok = 1;
  1061. int valid, dirty;
  1062. /* Unmodified entries are uninteresting except on the first pass */
  1063. dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
  1064. if (!first_pass && !dirty)
  1065. return 0;
  1066. valid = 0;
  1067. if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT)) {
  1068. valid = 1;
  1069. if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
  1070. !(hptp[0] & HPTE_V_BOLTED))
  1071. valid = 0;
  1072. }
  1073. if (valid != want_valid)
  1074. return 0;
  1075. v = r = 0;
  1076. if (valid || dirty) {
  1077. /* lock the HPTE so it's stable and read it */
  1078. preempt_disable();
  1079. while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
  1080. cpu_relax();
  1081. v = hptp[0];
  1082. if (v & HPTE_V_ABSENT) {
  1083. v &= ~HPTE_V_ABSENT;
  1084. v |= HPTE_V_VALID;
  1085. }
  1086. /* re-evaluate valid and dirty from synchronized HPTE value */
  1087. valid = !!(v & HPTE_V_VALID);
  1088. if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
  1089. valid = 0;
  1090. r = revp->guest_rpte | (hptp[1] & (HPTE_R_R | HPTE_R_C));
  1091. dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
  1092. /* only clear modified if this is the right sort of entry */
  1093. if (valid == want_valid && dirty) {
  1094. r &= ~HPTE_GR_MODIFIED;
  1095. revp->guest_rpte = r;
  1096. }
  1097. asm volatile(PPC_RELEASE_BARRIER "" : : : "memory");
  1098. hptp[0] &= ~HPTE_V_HVLOCK;
  1099. preempt_enable();
  1100. if (!(valid == want_valid && (first_pass || dirty)))
  1101. ok = 0;
  1102. }
  1103. hpte[0] = v;
  1104. hpte[1] = r;
  1105. return ok;
  1106. }
  1107. static ssize_t kvm_htab_read(struct file *file, char __user *buf,
  1108. size_t count, loff_t *ppos)
  1109. {
  1110. struct kvm_htab_ctx *ctx = file->private_data;
  1111. struct kvm *kvm = ctx->kvm;
  1112. struct kvm_get_htab_header hdr;
  1113. unsigned long *hptp;
  1114. struct revmap_entry *revp;
  1115. unsigned long i, nb, nw;
  1116. unsigned long __user *lbuf;
  1117. struct kvm_get_htab_header __user *hptr;
  1118. unsigned long flags;
  1119. int first_pass;
  1120. unsigned long hpte[2];
  1121. if (!access_ok(VERIFY_WRITE, buf, count))
  1122. return -EFAULT;
  1123. first_pass = ctx->first_pass;
  1124. flags = ctx->flags;
  1125. i = ctx->index;
  1126. hptp = (unsigned long *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
  1127. revp = kvm->arch.revmap + i;
  1128. lbuf = (unsigned long __user *)buf;
  1129. nb = 0;
  1130. while (nb + sizeof(hdr) + HPTE_SIZE < count) {
  1131. /* Initialize header */
  1132. hptr = (struct kvm_get_htab_header __user *)buf;
  1133. hdr.n_valid = 0;
  1134. hdr.n_invalid = 0;
  1135. nw = nb;
  1136. nb += sizeof(hdr);
  1137. lbuf = (unsigned long __user *)(buf + sizeof(hdr));
  1138. /* Skip uninteresting entries, i.e. clean on not-first pass */
  1139. if (!first_pass) {
  1140. while (i < kvm->arch.hpt_npte &&
  1141. !(revp->guest_rpte & HPTE_GR_MODIFIED)) {
  1142. ++i;
  1143. hptp += 2;
  1144. ++revp;
  1145. }
  1146. }
  1147. hdr.index = i;
  1148. /* Grab a series of valid entries */
  1149. while (i < kvm->arch.hpt_npte &&
  1150. hdr.n_valid < 0xffff &&
  1151. nb + HPTE_SIZE < count &&
  1152. record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
  1153. /* valid entry, write it out */
  1154. ++hdr.n_valid;
  1155. if (__put_user(hpte[0], lbuf) ||
  1156. __put_user(hpte[1], lbuf + 1))
  1157. return -EFAULT;
  1158. nb += HPTE_SIZE;
  1159. lbuf += 2;
  1160. ++i;
  1161. hptp += 2;
  1162. ++revp;
  1163. }
  1164. /* Now skip invalid entries while we can */
  1165. while (i < kvm->arch.hpt_npte &&
  1166. hdr.n_invalid < 0xffff &&
  1167. record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
  1168. /* found an invalid entry */
  1169. ++hdr.n_invalid;
  1170. ++i;
  1171. hptp += 2;
  1172. ++revp;
  1173. }
  1174. if (hdr.n_valid || hdr.n_invalid) {
  1175. /* write back the header */
  1176. if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
  1177. return -EFAULT;
  1178. nw = nb;
  1179. buf = (char __user *)lbuf;
  1180. } else {
  1181. nb = nw;
  1182. }
  1183. /* Check if we've wrapped around the hash table */
  1184. if (i >= kvm->arch.hpt_npte) {
  1185. i = 0;
  1186. ctx->first_pass = 0;
  1187. break;
  1188. }
  1189. }
  1190. ctx->index = i;
  1191. return nb;
  1192. }
  1193. static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
  1194. size_t count, loff_t *ppos)
  1195. {
  1196. struct kvm_htab_ctx *ctx = file->private_data;
  1197. struct kvm *kvm = ctx->kvm;
  1198. struct kvm_get_htab_header hdr;
  1199. unsigned long i, j;
  1200. unsigned long v, r;
  1201. unsigned long __user *lbuf;
  1202. unsigned long *hptp;
  1203. unsigned long tmp[2];
  1204. ssize_t nb;
  1205. long int err, ret;
  1206. int rma_setup;
  1207. if (!access_ok(VERIFY_READ, buf, count))
  1208. return -EFAULT;
  1209. /* lock out vcpus from running while we're doing this */
  1210. mutex_lock(&kvm->lock);
  1211. rma_setup = kvm->arch.rma_setup_done;
  1212. if (rma_setup) {
  1213. kvm->arch.rma_setup_done = 0; /* temporarily */
  1214. /* order rma_setup_done vs. vcpus_running */
  1215. smp_mb();
  1216. if (atomic_read(&kvm->arch.vcpus_running)) {
  1217. kvm->arch.rma_setup_done = 1;
  1218. mutex_unlock(&kvm->lock);
  1219. return -EBUSY;
  1220. }
  1221. }
  1222. err = 0;
  1223. for (nb = 0; nb + sizeof(hdr) <= count; ) {
  1224. err = -EFAULT;
  1225. if (__copy_from_user(&hdr, buf, sizeof(hdr)))
  1226. break;
  1227. err = 0;
  1228. if (nb + hdr.n_valid * HPTE_SIZE > count)
  1229. break;
  1230. nb += sizeof(hdr);
  1231. buf += sizeof(hdr);
  1232. err = -EINVAL;
  1233. i = hdr.index;
  1234. if (i >= kvm->arch.hpt_npte ||
  1235. i + hdr.n_valid + hdr.n_invalid > kvm->arch.hpt_npte)
  1236. break;
  1237. hptp = (unsigned long *)(kvm->arch.hpt_virt + (i * HPTE_SIZE));
  1238. lbuf = (unsigned long __user *)buf;
  1239. for (j = 0; j < hdr.n_valid; ++j) {
  1240. err = -EFAULT;
  1241. if (__get_user(v, lbuf) || __get_user(r, lbuf + 1))
  1242. goto out;
  1243. err = -EINVAL;
  1244. if (!(v & HPTE_V_VALID))
  1245. goto out;
  1246. lbuf += 2;
  1247. nb += HPTE_SIZE;
  1248. if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT))
  1249. kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
  1250. err = -EIO;
  1251. ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
  1252. tmp);
  1253. if (ret != H_SUCCESS) {
  1254. pr_err("kvm_htab_write ret %ld i=%ld v=%lx "
  1255. "r=%lx\n", ret, i, v, r);
  1256. goto out;
  1257. }
  1258. if (!rma_setup && is_vrma_hpte(v)) {
  1259. unsigned long psize = hpte_page_size(v, r);
  1260. unsigned long senc = slb_pgsize_encoding(psize);
  1261. unsigned long lpcr;
  1262. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1263. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1264. lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
  1265. lpcr |= senc << (LPCR_VRMASD_SH - 4);
  1266. kvm->arch.lpcr = lpcr;
  1267. rma_setup = 1;
  1268. }
  1269. ++i;
  1270. hptp += 2;
  1271. }
  1272. for (j = 0; j < hdr.n_invalid; ++j) {
  1273. if (hptp[0] & (HPTE_V_VALID | HPTE_V_ABSENT))
  1274. kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
  1275. ++i;
  1276. hptp += 2;
  1277. }
  1278. err = 0;
  1279. }
  1280. out:
  1281. /* Order HPTE updates vs. rma_setup_done */
  1282. smp_wmb();
  1283. kvm->arch.rma_setup_done = rma_setup;
  1284. mutex_unlock(&kvm->lock);
  1285. if (err)
  1286. return err;
  1287. return nb;
  1288. }
  1289. static int kvm_htab_release(struct inode *inode, struct file *filp)
  1290. {
  1291. struct kvm_htab_ctx *ctx = filp->private_data;
  1292. filp->private_data = NULL;
  1293. if (!(ctx->flags & KVM_GET_HTAB_WRITE))
  1294. atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
  1295. kvm_put_kvm(ctx->kvm);
  1296. kfree(ctx);
  1297. return 0;
  1298. }
  1299. static struct file_operations kvm_htab_fops = {
  1300. .read = kvm_htab_read,
  1301. .write = kvm_htab_write,
  1302. .llseek = default_llseek,
  1303. .release = kvm_htab_release,
  1304. };
  1305. int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
  1306. {
  1307. int ret;
  1308. struct kvm_htab_ctx *ctx;
  1309. int rwflag;
  1310. /* reject flags we don't recognize */
  1311. if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
  1312. return -EINVAL;
  1313. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1314. if (!ctx)
  1315. return -ENOMEM;
  1316. kvm_get_kvm(kvm);
  1317. ctx->kvm = kvm;
  1318. ctx->index = ghf->start_index;
  1319. ctx->flags = ghf->flags;
  1320. ctx->first_pass = 1;
  1321. rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
  1322. ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag);
  1323. if (ret < 0) {
  1324. kvm_put_kvm(kvm);
  1325. return ret;
  1326. }
  1327. if (rwflag == O_RDONLY) {
  1328. mutex_lock(&kvm->slots_lock);
  1329. atomic_inc(&kvm->arch.hpte_mod_interest);
  1330. /* make sure kvmppc_do_h_enter etc. see the increment */
  1331. synchronize_srcu_expedited(&kvm->srcu);
  1332. mutex_unlock(&kvm->slots_lock);
  1333. }
  1334. return ret;
  1335. }
  1336. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  1337. {
  1338. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  1339. if (cpu_has_feature(CPU_FTR_ARCH_206))
  1340. vcpu->arch.slb_nr = 32; /* POWER7 */
  1341. else
  1342. vcpu->arch.slb_nr = 64;
  1343. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  1344. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  1345. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  1346. }