book3s_64_mmu_hv.c 43 KB

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