book3s_64_mmu_hv.c 43 KB

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