book3s_64_mmu_hv.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  5. */
  6. #include <linux/types.h>
  7. #include <linux/string.h>
  8. #include <linux/kvm.h>
  9. #include <linux/kvm_host.h>
  10. #include <linux/highmem.h>
  11. #include <linux/gfp.h>
  12. #include <linux/slab.h>
  13. #include <linux/hugetlb.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/srcu.h>
  16. #include <linux/anon_inodes.h>
  17. #include <linux/file.h>
  18. #include <linux/debugfs.h>
  19. #include <asm/kvm_ppc.h>
  20. #include <asm/kvm_book3s.h>
  21. #include <asm/book3s/64/mmu-hash.h>
  22. #include <asm/hvcall.h>
  23. #include <asm/synch.h>
  24. #include <asm/ppc-opcode.h>
  25. #include <asm/cputable.h>
  26. #include <asm/pte-walk.h>
  27. #include "book3s.h"
  28. #include "book3s_hv.h"
  29. #include "trace_hv.h"
  30. //#define DEBUG_RESIZE_HPT 1
  31. #ifdef DEBUG_RESIZE_HPT
  32. #define resize_hpt_debug(resize, ...) \
  33. do { \
  34. printk(KERN_DEBUG "RESIZE HPT %p: ", resize); \
  35. printk(__VA_ARGS__); \
  36. } while (0)
  37. #else
  38. #define resize_hpt_debug(resize, ...) \
  39. do { } while (0)
  40. #endif
  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. struct kvm_resize_hpt {
  45. /* These fields read-only after init */
  46. struct kvm *kvm;
  47. struct work_struct work;
  48. u32 order;
  49. /* These fields protected by kvm->arch.mmu_setup_lock */
  50. /* Possible values and their usage:
  51. * <0 an error occurred during allocation,
  52. * -EBUSY allocation is in the progress,
  53. * 0 allocation made successfully.
  54. */
  55. int error;
  56. /* Private to the work thread, until error != -EBUSY,
  57. * then protected by kvm->arch.mmu_setup_lock.
  58. */
  59. struct kvm_hpt_info hpt;
  60. };
  61. int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order)
  62. {
  63. unsigned long hpt = 0;
  64. int cma = 0;
  65. struct page *page = NULL;
  66. struct revmap_entry *rev;
  67. unsigned long npte;
  68. if ((order < PPC_MIN_HPT_ORDER) || (order > PPC_MAX_HPT_ORDER))
  69. return -EINVAL;
  70. page = kvm_alloc_hpt_cma(1ul << (order - PAGE_SHIFT));
  71. if (page) {
  72. hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page));
  73. memset((void *)hpt, 0, (1ul << order));
  74. cma = 1;
  75. }
  76. if (!hpt)
  77. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_RETRY_MAYFAIL
  78. |__GFP_NOWARN, order - PAGE_SHIFT);
  79. if (!hpt)
  80. return -ENOMEM;
  81. /* HPTEs are 2**4 bytes long */
  82. npte = 1ul << (order - 4);
  83. /* Allocate reverse map array */
  84. rev = vmalloc(array_size(npte, sizeof(struct revmap_entry)));
  85. if (!rev) {
  86. if (cma)
  87. kvm_free_hpt_cma(page, 1 << (order - PAGE_SHIFT));
  88. else
  89. free_pages(hpt, order - PAGE_SHIFT);
  90. return -ENOMEM;
  91. }
  92. info->order = order;
  93. info->virt = hpt;
  94. info->cma = cma;
  95. info->rev = rev;
  96. return 0;
  97. }
  98. void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info)
  99. {
  100. atomic64_set(&kvm->arch.mmio_update, 0);
  101. kvm->arch.hpt = *info;
  102. kvm->arch.sdr1 = __pa(info->virt) | (info->order - 18);
  103. pr_debug("KVM guest htab at %lx (order %ld), LPID %llx\n",
  104. info->virt, (long)info->order, kvm->arch.lpid);
  105. }
  106. int kvmppc_alloc_reset_hpt(struct kvm *kvm, int order)
  107. {
  108. int err = -EBUSY;
  109. struct kvm_hpt_info info;
  110. mutex_lock(&kvm->arch.mmu_setup_lock);
  111. if (kvm->arch.mmu_ready) {
  112. kvm->arch.mmu_ready = 0;
  113. /* order mmu_ready vs. vcpus_running */
  114. smp_mb();
  115. if (atomic_read(&kvm->arch.vcpus_running)) {
  116. kvm->arch.mmu_ready = 1;
  117. goto out;
  118. }
  119. }
  120. if (kvm_is_radix(kvm)) {
  121. err = kvmppc_switch_mmu_to_hpt(kvm);
  122. if (err)
  123. goto out;
  124. }
  125. if (kvm->arch.hpt.order == order) {
  126. /* We already have a suitable HPT */
  127. /* Set the entire HPT to 0, i.e. invalid HPTEs */
  128. memset((void *)kvm->arch.hpt.virt, 0, 1ul << order);
  129. /*
  130. * Reset all the reverse-mapping chains for all memslots
  131. */
  132. kvmppc_rmap_reset(kvm);
  133. err = 0;
  134. goto out;
  135. }
  136. if (kvm->arch.hpt.virt) {
  137. kvmppc_free_hpt(&kvm->arch.hpt);
  138. kvmppc_rmap_reset(kvm);
  139. }
  140. err = kvmppc_allocate_hpt(&info, order);
  141. if (err < 0)
  142. goto out;
  143. kvmppc_set_hpt(kvm, &info);
  144. out:
  145. if (err == 0)
  146. /* Ensure that each vcpu will flush its TLB on next entry. */
  147. cpumask_setall(&kvm->arch.need_tlb_flush);
  148. mutex_unlock(&kvm->arch.mmu_setup_lock);
  149. return err;
  150. }
  151. void kvmppc_free_hpt(struct kvm_hpt_info *info)
  152. {
  153. vfree(info->rev);
  154. info->rev = NULL;
  155. if (info->cma)
  156. kvm_free_hpt_cma(virt_to_page((void *)info->virt),
  157. 1 << (info->order - PAGE_SHIFT));
  158. else if (info->virt)
  159. free_pages(info->virt, info->order - PAGE_SHIFT);
  160. info->virt = 0;
  161. info->order = 0;
  162. }
  163. /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
  164. static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
  165. {
  166. return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
  167. }
  168. /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
  169. static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
  170. {
  171. return (pgsize == 0x10000) ? 0x1000 : 0;
  172. }
  173. void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
  174. unsigned long porder)
  175. {
  176. unsigned long i;
  177. unsigned long npages;
  178. unsigned long hp_v, hp_r;
  179. unsigned long addr, hash;
  180. unsigned long psize;
  181. unsigned long hp0, hp1;
  182. unsigned long idx_ret;
  183. long ret;
  184. struct kvm *kvm = vcpu->kvm;
  185. psize = 1ul << porder;
  186. npages = memslot->npages >> (porder - PAGE_SHIFT);
  187. /* VRMA can't be > 1TB */
  188. if (npages > 1ul << (40 - porder))
  189. npages = 1ul << (40 - porder);
  190. /* Can't use more than 1 HPTE per HPTEG */
  191. if (npages > kvmppc_hpt_mask(&kvm->arch.hpt) + 1)
  192. npages = kvmppc_hpt_mask(&kvm->arch.hpt) + 1;
  193. hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
  194. HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
  195. hp1 = hpte1_pgsize_encoding(psize) |
  196. HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
  197. for (i = 0; i < npages; ++i) {
  198. addr = i << porder;
  199. /* can't use hpt_hash since va > 64 bits */
  200. hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25)))
  201. & kvmppc_hpt_mask(&kvm->arch.hpt);
  202. /*
  203. * We assume that the hash table is empty and no
  204. * vcpus are using it at this stage. Since we create
  205. * at most one HPTE per HPTEG, we just assume entry 7
  206. * is available and use it.
  207. */
  208. hash = (hash << 3) + 7;
  209. hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
  210. hp_r = hp1 | addr;
  211. ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r,
  212. &idx_ret);
  213. if (ret != H_SUCCESS) {
  214. pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
  215. addr, ret);
  216. break;
  217. }
  218. }
  219. }
  220. int kvmppc_mmu_hv_init(void)
  221. {
  222. unsigned long nr_lpids;
  223. if (!mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE))
  224. return -EINVAL;
  225. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  226. if (WARN_ON(mfspr(SPRN_LPID) != 0))
  227. return -EINVAL;
  228. nr_lpids = 1UL << mmu_lpid_bits;
  229. } else {
  230. nr_lpids = 1UL << KVM_MAX_NESTED_GUESTS_SHIFT;
  231. }
  232. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  233. /* POWER7 has 10-bit LPIDs, POWER8 has 12-bit LPIDs */
  234. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  235. WARN_ON(nr_lpids != 1UL << 12);
  236. else
  237. WARN_ON(nr_lpids != 1UL << 10);
  238. /*
  239. * Reserve the last implemented LPID use in partition
  240. * switching for POWER7 and POWER8.
  241. */
  242. nr_lpids -= 1;
  243. }
  244. kvmppc_init_lpid(nr_lpids);
  245. return 0;
  246. }
  247. static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags,
  248. long pte_index, unsigned long pteh,
  249. unsigned long ptel, unsigned long *pte_idx_ret)
  250. {
  251. long ret;
  252. preempt_disable();
  253. ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel,
  254. kvm->mm->pgd, false, pte_idx_ret);
  255. preempt_enable();
  256. if (ret == H_TOO_HARD) {
  257. /* this can't happen */
  258. pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
  259. ret = H_RESOURCE; /* or something */
  260. }
  261. return ret;
  262. }
  263. static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
  264. gva_t eaddr)
  265. {
  266. u64 mask;
  267. int i;
  268. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  269. if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
  270. continue;
  271. if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
  272. mask = ESID_MASK_1T;
  273. else
  274. mask = ESID_MASK;
  275. if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
  276. return &vcpu->arch.slb[i];
  277. }
  278. return NULL;
  279. }
  280. static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
  281. unsigned long ea)
  282. {
  283. unsigned long ra_mask;
  284. ra_mask = kvmppc_actual_pgsz(v, r) - 1;
  285. return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
  286. }
  287. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  288. struct kvmppc_pte *gpte, bool data, bool iswrite)
  289. {
  290. struct kvm *kvm = vcpu->kvm;
  291. struct kvmppc_slb *slbe;
  292. unsigned long slb_v;
  293. unsigned long pp, key;
  294. unsigned long v, orig_v, gr;
  295. __be64 *hptep;
  296. long int index;
  297. int virtmode = __kvmppc_get_msr_hv(vcpu) & (data ? MSR_DR : MSR_IR);
  298. if (kvm_is_radix(vcpu->kvm))
  299. return kvmppc_mmu_radix_xlate(vcpu, eaddr, gpte, data, iswrite);
  300. /* Get SLB entry */
  301. if (virtmode) {
  302. slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
  303. if (!slbe)
  304. return -EINVAL;
  305. slb_v = slbe->origv;
  306. } else {
  307. /* real mode access */
  308. slb_v = vcpu->kvm->arch.vrma_slb_v;
  309. }
  310. preempt_disable();
  311. /* Find the HPTE in the hash table */
  312. index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
  313. HPTE_V_VALID | HPTE_V_ABSENT);
  314. if (index < 0) {
  315. preempt_enable();
  316. return -ENOENT;
  317. }
  318. hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
  319. v = orig_v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
  320. if (cpu_has_feature(CPU_FTR_ARCH_300))
  321. v = hpte_new_to_old_v(v, be64_to_cpu(hptep[1]));
  322. gr = kvm->arch.hpt.rev[index].guest_rpte;
  323. unlock_hpte(hptep, orig_v);
  324. preempt_enable();
  325. gpte->eaddr = eaddr;
  326. gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
  327. /* Get PP bits and key for permission check */
  328. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  329. key = (__kvmppc_get_msr_hv(vcpu) & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  330. key &= slb_v;
  331. /* Calculate permissions */
  332. gpte->may_read = hpte_read_permission(pp, key);
  333. gpte->may_write = hpte_write_permission(pp, key);
  334. gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
  335. /* Storage key permission check for POWER7 */
  336. if (data && virtmode) {
  337. int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
  338. if (amrfield & 1)
  339. gpte->may_read = 0;
  340. if (amrfield & 2)
  341. gpte->may_write = 0;
  342. }
  343. /* Get the guest physical address */
  344. gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
  345. return 0;
  346. }
  347. /*
  348. * Quick test for whether an instruction is a load or a store.
  349. * If the instruction is a load or a store, then this will indicate
  350. * which it is, at least on server processors. (Embedded processors
  351. * have some external PID instructions that don't follow the rule
  352. * embodied here.) If the instruction isn't a load or store, then
  353. * this doesn't return anything useful.
  354. */
  355. static int instruction_is_store(ppc_inst_t instr)
  356. {
  357. unsigned int mask;
  358. unsigned int suffix;
  359. mask = 0x10000000;
  360. suffix = ppc_inst_val(instr);
  361. if (ppc_inst_prefixed(instr))
  362. suffix = ppc_inst_suffix(instr);
  363. else if ((suffix & 0xfc000000) == 0x7c000000)
  364. mask = 0x100; /* major opcode 31 */
  365. return (suffix & mask) != 0;
  366. }
  367. int kvmppc_hv_emulate_mmio(struct kvm_vcpu *vcpu,
  368. unsigned long gpa, gva_t ea, int is_store)
  369. {
  370. ppc_inst_t last_inst;
  371. bool is_prefixed = !!(kvmppc_get_msr(vcpu) & SRR1_PREFIXED);
  372. /*
  373. * Fast path - check if the guest physical address corresponds to a
  374. * device on the FAST_MMIO_BUS, if so we can avoid loading the
  375. * instruction all together, then we can just handle it and return.
  376. */
  377. if (is_store) {
  378. int idx, ret;
  379. idx = srcu_read_lock(&vcpu->kvm->srcu);
  380. ret = kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, (gpa_t) gpa, 0,
  381. NULL);
  382. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  383. if (!ret) {
  384. kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + (is_prefixed ? 8 : 4));
  385. return RESUME_GUEST;
  386. }
  387. }
  388. /*
  389. * If we fail, we just return to the guest and try executing it again.
  390. */
  391. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
  392. EMULATE_DONE)
  393. return RESUME_GUEST;
  394. /*
  395. * WARNING: We do not know for sure whether the instruction we just
  396. * read from memory is the same that caused the fault in the first
  397. * place.
  398. *
  399. * If the fault is prefixed but the instruction is not or vice
  400. * versa, try again so that we don't advance pc the wrong amount.
  401. */
  402. if (ppc_inst_prefixed(last_inst) != is_prefixed)
  403. return RESUME_GUEST;
  404. /*
  405. * If the instruction we read is neither an load or a store,
  406. * then it can't access memory, so we don't need to worry about
  407. * enforcing access permissions. So, assuming it is a load or
  408. * store, we just check that its direction (load or store) is
  409. * consistent with the original fault, since that's what we
  410. * checked the access permissions against. If there is a mismatch
  411. * we just return and retry the instruction.
  412. */
  413. if (instruction_is_store(last_inst) != !!is_store)
  414. return RESUME_GUEST;
  415. /*
  416. * Emulated accesses are emulated by looking at the hash for
  417. * translation once, then performing the access later. The
  418. * translation could be invalidated in the meantime in which
  419. * point performing the subsequent memory access on the old
  420. * physical address could possibly be a security hole for the
  421. * guest (but not the host).
  422. *
  423. * This is less of an issue for MMIO stores since they aren't
  424. * globally visible. It could be an issue for MMIO loads to
  425. * a certain extent but we'll ignore it for now.
  426. */
  427. vcpu->arch.paddr_accessed = gpa;
  428. vcpu->arch.vaddr_accessed = ea;
  429. return kvmppc_emulate_mmio(vcpu);
  430. }
  431. int kvmppc_book3s_hv_page_fault(struct kvm_vcpu *vcpu,
  432. unsigned long ea, unsigned long dsisr)
  433. {
  434. struct kvm *kvm = vcpu->kvm;
  435. unsigned long hpte[3], r;
  436. unsigned long hnow_v, hnow_r;
  437. __be64 *hptep;
  438. unsigned long mmu_seq, psize, pte_size;
  439. unsigned long gpa_base, gfn_base;
  440. unsigned long gpa, gfn, hva, pfn, hpa;
  441. struct kvm_memory_slot *memslot;
  442. unsigned long *rmap;
  443. struct revmap_entry *rev;
  444. struct page *page;
  445. long index, ret;
  446. bool is_ci;
  447. bool writing, write_ok;
  448. unsigned int shift;
  449. unsigned long rcbits;
  450. long mmio_update;
  451. pte_t pte, *ptep;
  452. if (kvm_is_radix(kvm))
  453. return kvmppc_book3s_radix_page_fault(vcpu, ea, dsisr);
  454. /*
  455. * Real-mode code has already searched the HPT and found the
  456. * entry we're interested in. Lock the entry and check that
  457. * it hasn't changed. If it has, just return and re-execute the
  458. * instruction.
  459. */
  460. if (ea != vcpu->arch.pgfault_addr)
  461. return RESUME_GUEST;
  462. if (vcpu->arch.pgfault_cache) {
  463. mmio_update = atomic64_read(&kvm->arch.mmio_update);
  464. if (mmio_update == vcpu->arch.pgfault_cache->mmio_update) {
  465. r = vcpu->arch.pgfault_cache->rpte;
  466. psize = kvmppc_actual_pgsz(vcpu->arch.pgfault_hpte[0],
  467. r);
  468. gpa_base = r & HPTE_R_RPN & ~(psize - 1);
  469. gfn_base = gpa_base >> PAGE_SHIFT;
  470. gpa = gpa_base | (ea & (psize - 1));
  471. return kvmppc_hv_emulate_mmio(vcpu, gpa, ea,
  472. dsisr & DSISR_ISSTORE);
  473. }
  474. }
  475. index = vcpu->arch.pgfault_index;
  476. hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
  477. rev = &kvm->arch.hpt.rev[index];
  478. preempt_disable();
  479. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  480. cpu_relax();
  481. hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK;
  482. hpte[1] = be64_to_cpu(hptep[1]);
  483. hpte[2] = r = rev->guest_rpte;
  484. unlock_hpte(hptep, hpte[0]);
  485. preempt_enable();
  486. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  487. hpte[0] = hpte_new_to_old_v(hpte[0], hpte[1]);
  488. hpte[1] = hpte_new_to_old_r(hpte[1]);
  489. }
  490. if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
  491. hpte[1] != vcpu->arch.pgfault_hpte[1])
  492. return RESUME_GUEST;
  493. /* Translate the logical address and get the page */
  494. psize = kvmppc_actual_pgsz(hpte[0], r);
  495. gpa_base = r & HPTE_R_RPN & ~(psize - 1);
  496. gfn_base = gpa_base >> PAGE_SHIFT;
  497. gpa = gpa_base | (ea & (psize - 1));
  498. gfn = gpa >> PAGE_SHIFT;
  499. memslot = gfn_to_memslot(kvm, gfn);
  500. trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr);
  501. /* No memslot means it's an emulated MMIO region */
  502. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  503. return kvmppc_hv_emulate_mmio(vcpu, gpa, ea,
  504. dsisr & DSISR_ISSTORE);
  505. /*
  506. * This should never happen, because of the slot_is_aligned()
  507. * check in kvmppc_do_h_enter().
  508. */
  509. if (gfn_base < memslot->base_gfn)
  510. return -EFAULT;
  511. /* used to check for invalidations in progress */
  512. mmu_seq = kvm->mmu_invalidate_seq;
  513. smp_rmb();
  514. ret = -EFAULT;
  515. page = NULL;
  516. writing = (dsisr & DSISR_ISSTORE) != 0;
  517. /* If writing != 0, then the HPTE must allow writing, if we get here */
  518. write_ok = writing;
  519. hva = gfn_to_hva_memslot(memslot, gfn);
  520. /*
  521. * Do a fast check first, since __gfn_to_pfn_memslot doesn't
  522. * do it with !atomic && !async, which is how we call it.
  523. * We always ask for write permission since the common case
  524. * is that the page is writable.
  525. */
  526. if (get_user_page_fast_only(hva, FOLL_WRITE, &page)) {
  527. write_ok = true;
  528. } else {
  529. /* Call KVM generic code to do the slow-path check */
  530. pfn = __gfn_to_pfn_memslot(memslot, gfn, false, false, NULL,
  531. writing, &write_ok, NULL);
  532. if (is_error_noslot_pfn(pfn))
  533. return -EFAULT;
  534. page = NULL;
  535. if (pfn_valid(pfn)) {
  536. page = pfn_to_page(pfn);
  537. if (PageReserved(page))
  538. page = NULL;
  539. }
  540. }
  541. /*
  542. * Read the PTE from the process' radix tree and use that
  543. * so we get the shift and attribute bits.
  544. */
  545. spin_lock(&kvm->mmu_lock);
  546. ptep = find_kvm_host_pte(kvm, mmu_seq, hva, &shift);
  547. pte = __pte(0);
  548. if (ptep)
  549. pte = READ_ONCE(*ptep);
  550. spin_unlock(&kvm->mmu_lock);
  551. /*
  552. * If the PTE disappeared temporarily due to a THP
  553. * collapse, just return and let the guest try again.
  554. */
  555. if (!pte_present(pte)) {
  556. if (page)
  557. put_page(page);
  558. return RESUME_GUEST;
  559. }
  560. hpa = pte_pfn(pte) << PAGE_SHIFT;
  561. pte_size = PAGE_SIZE;
  562. if (shift)
  563. pte_size = 1ul << shift;
  564. is_ci = pte_ci(pte);
  565. if (psize > pte_size)
  566. goto out_put;
  567. if (pte_size > psize)
  568. hpa |= hva & (pte_size - psize);
  569. /* Check WIMG vs. the actual page we're accessing */
  570. if (!hpte_cache_flags_ok(r, is_ci)) {
  571. if (is_ci)
  572. goto out_put;
  573. /*
  574. * Allow guest to map emulated device memory as
  575. * uncacheable, but actually make it cacheable.
  576. */
  577. r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
  578. }
  579. /*
  580. * Set the HPTE to point to hpa.
  581. * Since the hpa is at PAGE_SIZE granularity, make sure we
  582. * don't mask out lower-order bits if psize < PAGE_SIZE.
  583. */
  584. if (psize < PAGE_SIZE)
  585. psize = PAGE_SIZE;
  586. r = (r & HPTE_R_KEY_HI) | (r & ~(HPTE_R_PP0 - psize)) | hpa;
  587. if (hpte_is_writable(r) && !write_ok)
  588. r = hpte_make_readonly(r);
  589. ret = RESUME_GUEST;
  590. preempt_disable();
  591. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  592. cpu_relax();
  593. hnow_v = be64_to_cpu(hptep[0]);
  594. hnow_r = be64_to_cpu(hptep[1]);
  595. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  596. hnow_v = hpte_new_to_old_v(hnow_v, hnow_r);
  597. hnow_r = hpte_new_to_old_r(hnow_r);
  598. }
  599. /*
  600. * If the HPT is being resized, don't update the HPTE,
  601. * instead let the guest retry after the resize operation is complete.
  602. * The synchronization for mmu_ready test vs. set is provided
  603. * by the HPTE lock.
  604. */
  605. if (!kvm->arch.mmu_ready)
  606. goto out_unlock;
  607. if ((hnow_v & ~HPTE_V_HVLOCK) != hpte[0] || hnow_r != hpte[1] ||
  608. rev->guest_rpte != hpte[2])
  609. /* HPTE has been changed under us; let the guest retry */
  610. goto out_unlock;
  611. hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  612. /* Always put the HPTE in the rmap chain for the page base address */
  613. rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn];
  614. lock_rmap(rmap);
  615. /* Check if we might have been invalidated; let the guest retry if so */
  616. ret = RESUME_GUEST;
  617. if (mmu_invalidate_retry(vcpu->kvm, mmu_seq)) {
  618. unlock_rmap(rmap);
  619. goto out_unlock;
  620. }
  621. /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
  622. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  623. r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  624. if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) {
  625. /* HPTE was previously valid, so we need to invalidate it */
  626. unlock_rmap(rmap);
  627. hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
  628. kvmppc_invalidate_hpte(kvm, hptep, index);
  629. /* don't lose previous R and C bits */
  630. r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
  631. } else {
  632. kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
  633. }
  634. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  635. r = hpte_old_to_new_r(hpte[0], r);
  636. hpte[0] = hpte_old_to_new_v(hpte[0]);
  637. }
  638. hptep[1] = cpu_to_be64(r);
  639. eieio();
  640. __unlock_hpte(hptep, hpte[0]);
  641. asm volatile("ptesync" : : : "memory");
  642. preempt_enable();
  643. if (page && hpte_is_writable(r))
  644. set_page_dirty_lock(page);
  645. out_put:
  646. trace_kvm_page_fault_exit(vcpu, hpte, ret);
  647. if (page)
  648. put_page(page);
  649. return ret;
  650. out_unlock:
  651. __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
  652. preempt_enable();
  653. goto out_put;
  654. }
  655. void kvmppc_rmap_reset(struct kvm *kvm)
  656. {
  657. struct kvm_memslots *slots;
  658. struct kvm_memory_slot *memslot;
  659. int srcu_idx, bkt;
  660. srcu_idx = srcu_read_lock(&kvm->srcu);
  661. slots = kvm_memslots(kvm);
  662. kvm_for_each_memslot(memslot, bkt, slots) {
  663. /* Mutual exclusion with kvm_unmap_hva_range etc. */
  664. spin_lock(&kvm->mmu_lock);
  665. /*
  666. * This assumes it is acceptable to lose reference and
  667. * change bits across a reset.
  668. */
  669. memset(memslot->arch.rmap, 0,
  670. memslot->npages * sizeof(*memslot->arch.rmap));
  671. spin_unlock(&kvm->mmu_lock);
  672. }
  673. srcu_read_unlock(&kvm->srcu, srcu_idx);
  674. }
  675. /* Must be called with both HPTE and rmap locked */
  676. static void kvmppc_unmap_hpte(struct kvm *kvm, unsigned long i,
  677. struct kvm_memory_slot *memslot,
  678. unsigned long *rmapp, unsigned long gfn)
  679. {
  680. __be64 *hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
  681. struct revmap_entry *rev = kvm->arch.hpt.rev;
  682. unsigned long j, h;
  683. unsigned long ptel, psize, rcbits;
  684. j = rev[i].forw;
  685. if (j == i) {
  686. /* chain is now empty */
  687. *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
  688. } else {
  689. /* remove i from chain */
  690. h = rev[i].back;
  691. rev[h].forw = j;
  692. rev[j].back = h;
  693. rev[i].forw = rev[i].back = i;
  694. *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
  695. }
  696. /* Now check and modify the HPTE */
  697. ptel = rev[i].guest_rpte;
  698. psize = kvmppc_actual_pgsz(be64_to_cpu(hptep[0]), ptel);
  699. if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
  700. hpte_rpn(ptel, psize) == gfn) {
  701. hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
  702. kvmppc_invalidate_hpte(kvm, hptep, i);
  703. hptep[1] &= ~cpu_to_be64(HPTE_R_KEY_HI | HPTE_R_KEY_LO);
  704. /* Harvest R and C */
  705. rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C);
  706. *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
  707. if ((rcbits & HPTE_R_C) && memslot->dirty_bitmap)
  708. kvmppc_update_dirty_map(memslot, gfn, psize);
  709. if (rcbits & ~rev[i].guest_rpte) {
  710. rev[i].guest_rpte = ptel | rcbits;
  711. note_hpte_modification(kvm, &rev[i]);
  712. }
  713. }
  714. }
  715. static void kvm_unmap_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
  716. unsigned long gfn)
  717. {
  718. unsigned long i;
  719. __be64 *hptep;
  720. unsigned long *rmapp;
  721. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  722. for (;;) {
  723. lock_rmap(rmapp);
  724. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  725. unlock_rmap(rmapp);
  726. break;
  727. }
  728. /*
  729. * To avoid an ABBA deadlock with the HPTE lock bit,
  730. * we can't spin on the HPTE lock while holding the
  731. * rmap chain lock.
  732. */
  733. i = *rmapp & KVMPPC_RMAP_INDEX;
  734. hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
  735. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  736. /* unlock rmap before spinning on the HPTE lock */
  737. unlock_rmap(rmapp);
  738. while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
  739. cpu_relax();
  740. continue;
  741. }
  742. kvmppc_unmap_hpte(kvm, i, memslot, rmapp, gfn);
  743. unlock_rmap(rmapp);
  744. __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
  745. }
  746. }
  747. bool kvm_unmap_gfn_range_hv(struct kvm *kvm, struct kvm_gfn_range *range)
  748. {
  749. gfn_t gfn;
  750. if (kvm_is_radix(kvm)) {
  751. for (gfn = range->start; gfn < range->end; gfn++)
  752. kvm_unmap_radix(kvm, range->slot, gfn);
  753. } else {
  754. for (gfn = range->start; gfn < range->end; gfn++)
  755. kvm_unmap_rmapp(kvm, range->slot, gfn);
  756. }
  757. return false;
  758. }
  759. void kvmppc_core_flush_memslot_hv(struct kvm *kvm,
  760. struct kvm_memory_slot *memslot)
  761. {
  762. unsigned long gfn;
  763. unsigned long n;
  764. unsigned long *rmapp;
  765. gfn = memslot->base_gfn;
  766. rmapp = memslot->arch.rmap;
  767. if (kvm_is_radix(kvm)) {
  768. kvmppc_radix_flush_memslot(kvm, memslot);
  769. return;
  770. }
  771. for (n = memslot->npages; n; --n, ++gfn) {
  772. /*
  773. * Testing the present bit without locking is OK because
  774. * the memslot has been marked invalid already, and hence
  775. * no new HPTEs referencing this page can be created,
  776. * thus the present bit can't go from 0 to 1.
  777. */
  778. if (*rmapp & KVMPPC_RMAP_PRESENT)
  779. kvm_unmap_rmapp(kvm, memslot, gfn);
  780. ++rmapp;
  781. }
  782. }
  783. static bool kvm_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
  784. unsigned long gfn)
  785. {
  786. struct revmap_entry *rev = kvm->arch.hpt.rev;
  787. unsigned long head, i, j;
  788. __be64 *hptep;
  789. bool ret = false;
  790. unsigned long *rmapp;
  791. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  792. retry:
  793. lock_rmap(rmapp);
  794. if (*rmapp & KVMPPC_RMAP_REFERENCED) {
  795. *rmapp &= ~KVMPPC_RMAP_REFERENCED;
  796. ret = true;
  797. }
  798. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  799. unlock_rmap(rmapp);
  800. return ret;
  801. }
  802. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  803. do {
  804. hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
  805. j = rev[i].forw;
  806. /* If this HPTE isn't referenced, ignore it */
  807. if (!(be64_to_cpu(hptep[1]) & HPTE_R_R))
  808. continue;
  809. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  810. /* unlock rmap before spinning on the HPTE lock */
  811. unlock_rmap(rmapp);
  812. while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK)
  813. cpu_relax();
  814. goto retry;
  815. }
  816. /* Now check and modify the HPTE */
  817. if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) &&
  818. (be64_to_cpu(hptep[1]) & HPTE_R_R)) {
  819. kvmppc_clear_ref_hpte(kvm, hptep, i);
  820. if (!(rev[i].guest_rpte & HPTE_R_R)) {
  821. rev[i].guest_rpte |= HPTE_R_R;
  822. note_hpte_modification(kvm, &rev[i]);
  823. }
  824. ret = true;
  825. }
  826. __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
  827. } while ((i = j) != head);
  828. unlock_rmap(rmapp);
  829. return ret;
  830. }
  831. bool kvm_age_gfn_hv(struct kvm *kvm, struct kvm_gfn_range *range)
  832. {
  833. gfn_t gfn;
  834. bool ret = false;
  835. if (kvm_is_radix(kvm)) {
  836. for (gfn = range->start; gfn < range->end; gfn++)
  837. ret |= kvm_age_radix(kvm, range->slot, gfn);
  838. } else {
  839. for (gfn = range->start; gfn < range->end; gfn++)
  840. ret |= kvm_age_rmapp(kvm, range->slot, gfn);
  841. }
  842. return ret;
  843. }
  844. static bool kvm_test_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot,
  845. unsigned long gfn)
  846. {
  847. struct revmap_entry *rev = kvm->arch.hpt.rev;
  848. unsigned long head, i, j;
  849. unsigned long *hp;
  850. bool ret = true;
  851. unsigned long *rmapp;
  852. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  853. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  854. return true;
  855. lock_rmap(rmapp);
  856. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  857. goto out;
  858. if (*rmapp & KVMPPC_RMAP_PRESENT) {
  859. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  860. do {
  861. hp = (unsigned long *)(kvm->arch.hpt.virt + (i << 4));
  862. j = rev[i].forw;
  863. if (be64_to_cpu(hp[1]) & HPTE_R_R)
  864. goto out;
  865. } while ((i = j) != head);
  866. }
  867. ret = false;
  868. out:
  869. unlock_rmap(rmapp);
  870. return ret;
  871. }
  872. bool kvm_test_age_gfn_hv(struct kvm *kvm, struct kvm_gfn_range *range)
  873. {
  874. WARN_ON(range->start + 1 != range->end);
  875. if (kvm_is_radix(kvm))
  876. return kvm_test_age_radix(kvm, range->slot, range->start);
  877. else
  878. return kvm_test_age_rmapp(kvm, range->slot, range->start);
  879. }
  880. static int vcpus_running(struct kvm *kvm)
  881. {
  882. return atomic_read(&kvm->arch.vcpus_running) != 0;
  883. }
  884. /*
  885. * Returns the number of system pages that are dirty.
  886. * This can be more than 1 if we find a huge-page HPTE.
  887. */
  888. static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp)
  889. {
  890. struct revmap_entry *rev = kvm->arch.hpt.rev;
  891. unsigned long head, i, j;
  892. unsigned long n;
  893. unsigned long v, r;
  894. __be64 *hptep;
  895. int npages_dirty = 0;
  896. retry:
  897. lock_rmap(rmapp);
  898. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  899. unlock_rmap(rmapp);
  900. return npages_dirty;
  901. }
  902. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  903. do {
  904. unsigned long hptep1;
  905. hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4));
  906. j = rev[i].forw;
  907. /*
  908. * Checking the C (changed) bit here is racy since there
  909. * is no guarantee about when the hardware writes it back.
  910. * If the HPTE is not writable then it is stable since the
  911. * page can't be written to, and we would have done a tlbie
  912. * (which forces the hardware to complete any writeback)
  913. * when making the HPTE read-only.
  914. * If vcpus are running then this call is racy anyway
  915. * since the page could get dirtied subsequently, so we
  916. * expect there to be a further call which would pick up
  917. * any delayed C bit writeback.
  918. * Otherwise we need to do the tlbie even if C==0 in
  919. * order to pick up any delayed writeback of C.
  920. */
  921. hptep1 = be64_to_cpu(hptep[1]);
  922. if (!(hptep1 & HPTE_R_C) &&
  923. (!hpte_is_writable(hptep1) || vcpus_running(kvm)))
  924. continue;
  925. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  926. /* unlock rmap before spinning on the HPTE lock */
  927. unlock_rmap(rmapp);
  928. while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK))
  929. cpu_relax();
  930. goto retry;
  931. }
  932. /* Now check and modify the HPTE */
  933. if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) {
  934. __unlock_hpte(hptep, be64_to_cpu(hptep[0]));
  935. continue;
  936. }
  937. /* need to make it temporarily absent so C is stable */
  938. hptep[0] |= cpu_to_be64(HPTE_V_ABSENT);
  939. kvmppc_invalidate_hpte(kvm, hptep, i);
  940. v = be64_to_cpu(hptep[0]);
  941. r = be64_to_cpu(hptep[1]);
  942. if (r & HPTE_R_C) {
  943. hptep[1] = cpu_to_be64(r & ~HPTE_R_C);
  944. if (!(rev[i].guest_rpte & HPTE_R_C)) {
  945. rev[i].guest_rpte |= HPTE_R_C;
  946. note_hpte_modification(kvm, &rev[i]);
  947. }
  948. n = kvmppc_actual_pgsz(v, r);
  949. n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT;
  950. if (n > npages_dirty)
  951. npages_dirty = n;
  952. eieio();
  953. }
  954. v &= ~HPTE_V_ABSENT;
  955. v |= HPTE_V_VALID;
  956. __unlock_hpte(hptep, v);
  957. } while ((i = j) != head);
  958. unlock_rmap(rmapp);
  959. return npages_dirty;
  960. }
  961. void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa,
  962. struct kvm_memory_slot *memslot,
  963. unsigned long *map)
  964. {
  965. unsigned long gfn;
  966. if (!vpa->dirty || !vpa->pinned_addr)
  967. return;
  968. gfn = vpa->gpa >> PAGE_SHIFT;
  969. if (gfn < memslot->base_gfn ||
  970. gfn >= memslot->base_gfn + memslot->npages)
  971. return;
  972. vpa->dirty = false;
  973. if (map)
  974. __set_bit_le(gfn - memslot->base_gfn, map);
  975. }
  976. long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm,
  977. struct kvm_memory_slot *memslot, unsigned long *map)
  978. {
  979. unsigned long i;
  980. unsigned long *rmapp;
  981. preempt_disable();
  982. rmapp = memslot->arch.rmap;
  983. for (i = 0; i < memslot->npages; ++i) {
  984. int npages = kvm_test_clear_dirty_npages(kvm, rmapp);
  985. /*
  986. * Note that if npages > 0 then i must be a multiple of npages,
  987. * since we always put huge-page HPTEs in the rmap chain
  988. * corresponding to their page base address.
  989. */
  990. if (npages)
  991. set_dirty_bits(map, i, npages);
  992. ++rmapp;
  993. }
  994. preempt_enable();
  995. return 0;
  996. }
  997. void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
  998. unsigned long *nb_ret)
  999. {
  1000. struct kvm_memory_slot *memslot;
  1001. unsigned long gfn = gpa >> PAGE_SHIFT;
  1002. struct page *page, *pages[1];
  1003. int npages;
  1004. unsigned long hva, offset;
  1005. int srcu_idx;
  1006. srcu_idx = srcu_read_lock(&kvm->srcu);
  1007. memslot = gfn_to_memslot(kvm, gfn);
  1008. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1009. goto err;
  1010. hva = gfn_to_hva_memslot(memslot, gfn);
  1011. npages = get_user_pages_fast(hva, 1, FOLL_WRITE, pages);
  1012. if (npages < 1)
  1013. goto err;
  1014. page = pages[0];
  1015. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1016. offset = gpa & (PAGE_SIZE - 1);
  1017. if (nb_ret)
  1018. *nb_ret = PAGE_SIZE - offset;
  1019. return page_address(page) + offset;
  1020. err:
  1021. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1022. return NULL;
  1023. }
  1024. void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa,
  1025. bool dirty)
  1026. {
  1027. struct page *page = virt_to_page(va);
  1028. struct kvm_memory_slot *memslot;
  1029. unsigned long gfn;
  1030. int srcu_idx;
  1031. put_page(page);
  1032. if (!dirty)
  1033. return;
  1034. /* We need to mark this page dirty in the memslot dirty_bitmap, if any */
  1035. gfn = gpa >> PAGE_SHIFT;
  1036. srcu_idx = srcu_read_lock(&kvm->srcu);
  1037. memslot = gfn_to_memslot(kvm, gfn);
  1038. if (memslot && memslot->dirty_bitmap)
  1039. set_bit_le(gfn - memslot->base_gfn, memslot->dirty_bitmap);
  1040. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1041. }
  1042. /*
  1043. * HPT resizing
  1044. */
  1045. static int resize_hpt_allocate(struct kvm_resize_hpt *resize)
  1046. {
  1047. int rc;
  1048. rc = kvmppc_allocate_hpt(&resize->hpt, resize->order);
  1049. if (rc < 0)
  1050. return rc;
  1051. resize_hpt_debug(resize, "%s(): HPT @ 0x%lx\n", __func__,
  1052. resize->hpt.virt);
  1053. return 0;
  1054. }
  1055. static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt *resize,
  1056. unsigned long idx)
  1057. {
  1058. struct kvm *kvm = resize->kvm;
  1059. struct kvm_hpt_info *old = &kvm->arch.hpt;
  1060. struct kvm_hpt_info *new = &resize->hpt;
  1061. unsigned long old_hash_mask = (1ULL << (old->order - 7)) - 1;
  1062. unsigned long new_hash_mask = (1ULL << (new->order - 7)) - 1;
  1063. __be64 *hptep, *new_hptep;
  1064. unsigned long vpte, rpte, guest_rpte;
  1065. int ret;
  1066. struct revmap_entry *rev;
  1067. unsigned long apsize, avpn, pteg, hash;
  1068. unsigned long new_idx, new_pteg, replace_vpte;
  1069. int pshift;
  1070. hptep = (__be64 *)(old->virt + (idx << 4));
  1071. /* Guest is stopped, so new HPTEs can't be added or faulted
  1072. * in, only unmapped or altered by host actions. So, it's
  1073. * safe to check this before we take the HPTE lock */
  1074. vpte = be64_to_cpu(hptep[0]);
  1075. if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
  1076. return 0; /* nothing to do */
  1077. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  1078. cpu_relax();
  1079. vpte = be64_to_cpu(hptep[0]);
  1080. ret = 0;
  1081. if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT))
  1082. /* Nothing to do */
  1083. goto out;
  1084. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1085. rpte = be64_to_cpu(hptep[1]);
  1086. vpte = hpte_new_to_old_v(vpte, rpte);
  1087. }
  1088. /* Unmap */
  1089. rev = &old->rev[idx];
  1090. guest_rpte = rev->guest_rpte;
  1091. ret = -EIO;
  1092. apsize = kvmppc_actual_pgsz(vpte, guest_rpte);
  1093. if (!apsize)
  1094. goto out;
  1095. if (vpte & HPTE_V_VALID) {
  1096. unsigned long gfn = hpte_rpn(guest_rpte, apsize);
  1097. int srcu_idx = srcu_read_lock(&kvm->srcu);
  1098. struct kvm_memory_slot *memslot =
  1099. __gfn_to_memslot(kvm_memslots(kvm), gfn);
  1100. if (memslot) {
  1101. unsigned long *rmapp;
  1102. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  1103. lock_rmap(rmapp);
  1104. kvmppc_unmap_hpte(kvm, idx, memslot, rmapp, gfn);
  1105. unlock_rmap(rmapp);
  1106. }
  1107. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1108. }
  1109. /* Reload PTE after unmap */
  1110. vpte = be64_to_cpu(hptep[0]);
  1111. BUG_ON(vpte & HPTE_V_VALID);
  1112. BUG_ON(!(vpte & HPTE_V_ABSENT));
  1113. ret = 0;
  1114. if (!(vpte & HPTE_V_BOLTED))
  1115. goto out;
  1116. rpte = be64_to_cpu(hptep[1]);
  1117. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1118. vpte = hpte_new_to_old_v(vpte, rpte);
  1119. rpte = hpte_new_to_old_r(rpte);
  1120. }
  1121. pshift = kvmppc_hpte_base_page_shift(vpte, rpte);
  1122. avpn = HPTE_V_AVPN_VAL(vpte) & ~(((1ul << pshift) - 1) >> 23);
  1123. pteg = idx / HPTES_PER_GROUP;
  1124. if (vpte & HPTE_V_SECONDARY)
  1125. pteg = ~pteg;
  1126. if (!(vpte & HPTE_V_1TB_SEG)) {
  1127. unsigned long offset, vsid;
  1128. /* We only have 28 - 23 bits of offset in avpn */
  1129. offset = (avpn & 0x1f) << 23;
  1130. vsid = avpn >> 5;
  1131. /* We can find more bits from the pteg value */
  1132. if (pshift < 23)
  1133. offset |= ((vsid ^ pteg) & old_hash_mask) << pshift;
  1134. hash = vsid ^ (offset >> pshift);
  1135. } else {
  1136. unsigned long offset, vsid;
  1137. /* We only have 40 - 23 bits of seg_off in avpn */
  1138. offset = (avpn & 0x1ffff) << 23;
  1139. vsid = avpn >> 17;
  1140. if (pshift < 23)
  1141. offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask) << pshift;
  1142. hash = vsid ^ (vsid << 25) ^ (offset >> pshift);
  1143. }
  1144. new_pteg = hash & new_hash_mask;
  1145. if (vpte & HPTE_V_SECONDARY)
  1146. new_pteg = ~hash & new_hash_mask;
  1147. new_idx = new_pteg * HPTES_PER_GROUP + (idx % HPTES_PER_GROUP);
  1148. new_hptep = (__be64 *)(new->virt + (new_idx << 4));
  1149. replace_vpte = be64_to_cpu(new_hptep[0]);
  1150. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1151. unsigned long replace_rpte = be64_to_cpu(new_hptep[1]);
  1152. replace_vpte = hpte_new_to_old_v(replace_vpte, replace_rpte);
  1153. }
  1154. if (replace_vpte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
  1155. BUG_ON(new->order >= old->order);
  1156. if (replace_vpte & HPTE_V_BOLTED) {
  1157. if (vpte & HPTE_V_BOLTED)
  1158. /* Bolted collision, nothing we can do */
  1159. ret = -ENOSPC;
  1160. /* Discard the new HPTE */
  1161. goto out;
  1162. }
  1163. /* Discard the previous HPTE */
  1164. }
  1165. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1166. rpte = hpte_old_to_new_r(vpte, rpte);
  1167. vpte = hpte_old_to_new_v(vpte);
  1168. }
  1169. new_hptep[1] = cpu_to_be64(rpte);
  1170. new->rev[new_idx].guest_rpte = guest_rpte;
  1171. /* No need for a barrier, since new HPT isn't active */
  1172. new_hptep[0] = cpu_to_be64(vpte);
  1173. unlock_hpte(new_hptep, vpte);
  1174. out:
  1175. unlock_hpte(hptep, vpte);
  1176. return ret;
  1177. }
  1178. static int resize_hpt_rehash(struct kvm_resize_hpt *resize)
  1179. {
  1180. struct kvm *kvm = resize->kvm;
  1181. unsigned long i;
  1182. int rc;
  1183. for (i = 0; i < kvmppc_hpt_npte(&kvm->arch.hpt); i++) {
  1184. rc = resize_hpt_rehash_hpte(resize, i);
  1185. if (rc != 0)
  1186. return rc;
  1187. }
  1188. return 0;
  1189. }
  1190. static void resize_hpt_pivot(struct kvm_resize_hpt *resize)
  1191. {
  1192. struct kvm *kvm = resize->kvm;
  1193. struct kvm_hpt_info hpt_tmp;
  1194. /* Exchange the pending tables in the resize structure with
  1195. * the active tables */
  1196. resize_hpt_debug(resize, "resize_hpt_pivot()\n");
  1197. spin_lock(&kvm->mmu_lock);
  1198. asm volatile("ptesync" : : : "memory");
  1199. hpt_tmp = kvm->arch.hpt;
  1200. kvmppc_set_hpt(kvm, &resize->hpt);
  1201. resize->hpt = hpt_tmp;
  1202. spin_unlock(&kvm->mmu_lock);
  1203. synchronize_srcu_expedited(&kvm->srcu);
  1204. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1205. kvmppc_setup_partition_table(kvm);
  1206. resize_hpt_debug(resize, "resize_hpt_pivot() done\n");
  1207. }
  1208. static void resize_hpt_release(struct kvm *kvm, struct kvm_resize_hpt *resize)
  1209. {
  1210. if (WARN_ON(!mutex_is_locked(&kvm->arch.mmu_setup_lock)))
  1211. return;
  1212. if (!resize)
  1213. return;
  1214. if (resize->error != -EBUSY) {
  1215. if (resize->hpt.virt)
  1216. kvmppc_free_hpt(&resize->hpt);
  1217. kfree(resize);
  1218. }
  1219. if (kvm->arch.resize_hpt == resize)
  1220. kvm->arch.resize_hpt = NULL;
  1221. }
  1222. static void resize_hpt_prepare_work(struct work_struct *work)
  1223. {
  1224. struct kvm_resize_hpt *resize = container_of(work,
  1225. struct kvm_resize_hpt,
  1226. work);
  1227. struct kvm *kvm = resize->kvm;
  1228. int err = 0;
  1229. if (WARN_ON(resize->error != -EBUSY))
  1230. return;
  1231. mutex_lock(&kvm->arch.mmu_setup_lock);
  1232. /* Request is still current? */
  1233. if (kvm->arch.resize_hpt == resize) {
  1234. /* We may request large allocations here:
  1235. * do not sleep with kvm->arch.mmu_setup_lock held for a while.
  1236. */
  1237. mutex_unlock(&kvm->arch.mmu_setup_lock);
  1238. resize_hpt_debug(resize, "%s(): order = %d\n", __func__,
  1239. resize->order);
  1240. err = resize_hpt_allocate(resize);
  1241. /* We have strict assumption about -EBUSY
  1242. * when preparing for HPT resize.
  1243. */
  1244. if (WARN_ON(err == -EBUSY))
  1245. err = -EINPROGRESS;
  1246. mutex_lock(&kvm->arch.mmu_setup_lock);
  1247. /* It is possible that kvm->arch.resize_hpt != resize
  1248. * after we grab kvm->arch.mmu_setup_lock again.
  1249. */
  1250. }
  1251. resize->error = err;
  1252. if (kvm->arch.resize_hpt != resize)
  1253. resize_hpt_release(kvm, resize);
  1254. mutex_unlock(&kvm->arch.mmu_setup_lock);
  1255. }
  1256. int kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
  1257. struct kvm_ppc_resize_hpt *rhpt)
  1258. {
  1259. unsigned long flags = rhpt->flags;
  1260. unsigned long shift = rhpt->shift;
  1261. struct kvm_resize_hpt *resize;
  1262. int ret;
  1263. if (flags != 0 || kvm_is_radix(kvm))
  1264. return -EINVAL;
  1265. if (shift && ((shift < 18) || (shift > 46)))
  1266. return -EINVAL;
  1267. mutex_lock(&kvm->arch.mmu_setup_lock);
  1268. resize = kvm->arch.resize_hpt;
  1269. if (resize) {
  1270. if (resize->order == shift) {
  1271. /* Suitable resize in progress? */
  1272. ret = resize->error;
  1273. if (ret == -EBUSY)
  1274. ret = 100; /* estimated time in ms */
  1275. else if (ret)
  1276. resize_hpt_release(kvm, resize);
  1277. goto out;
  1278. }
  1279. /* not suitable, cancel it */
  1280. resize_hpt_release(kvm, resize);
  1281. }
  1282. ret = 0;
  1283. if (!shift)
  1284. goto out; /* nothing to do */
  1285. /* start new resize */
  1286. resize = kzalloc(sizeof(*resize), GFP_KERNEL);
  1287. if (!resize) {
  1288. ret = -ENOMEM;
  1289. goto out;
  1290. }
  1291. resize->error = -EBUSY;
  1292. resize->order = shift;
  1293. resize->kvm = kvm;
  1294. INIT_WORK(&resize->work, resize_hpt_prepare_work);
  1295. kvm->arch.resize_hpt = resize;
  1296. schedule_work(&resize->work);
  1297. ret = 100; /* estimated time in ms */
  1298. out:
  1299. mutex_unlock(&kvm->arch.mmu_setup_lock);
  1300. return ret;
  1301. }
  1302. static void resize_hpt_boot_vcpu(void *opaque)
  1303. {
  1304. /* Nothing to do, just force a KVM exit */
  1305. }
  1306. int kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
  1307. struct kvm_ppc_resize_hpt *rhpt)
  1308. {
  1309. unsigned long flags = rhpt->flags;
  1310. unsigned long shift = rhpt->shift;
  1311. struct kvm_resize_hpt *resize;
  1312. int ret;
  1313. if (flags != 0 || kvm_is_radix(kvm))
  1314. return -EINVAL;
  1315. if (shift && ((shift < 18) || (shift > 46)))
  1316. return -EINVAL;
  1317. mutex_lock(&kvm->arch.mmu_setup_lock);
  1318. resize = kvm->arch.resize_hpt;
  1319. /* This shouldn't be possible */
  1320. ret = -EIO;
  1321. if (WARN_ON(!kvm->arch.mmu_ready))
  1322. goto out_no_hpt;
  1323. /* Stop VCPUs from running while we mess with the HPT */
  1324. kvm->arch.mmu_ready = 0;
  1325. smp_mb();
  1326. /* Boot all CPUs out of the guest so they re-read
  1327. * mmu_ready */
  1328. on_each_cpu(resize_hpt_boot_vcpu, NULL, 1);
  1329. ret = -ENXIO;
  1330. if (!resize || (resize->order != shift))
  1331. goto out;
  1332. ret = resize->error;
  1333. if (ret)
  1334. goto out;
  1335. ret = resize_hpt_rehash(resize);
  1336. if (ret)
  1337. goto out;
  1338. resize_hpt_pivot(resize);
  1339. out:
  1340. /* Let VCPUs run again */
  1341. kvm->arch.mmu_ready = 1;
  1342. smp_mb();
  1343. out_no_hpt:
  1344. resize_hpt_release(kvm, resize);
  1345. mutex_unlock(&kvm->arch.mmu_setup_lock);
  1346. return ret;
  1347. }
  1348. /*
  1349. * Functions for reading and writing the hash table via reads and
  1350. * writes on a file descriptor.
  1351. *
  1352. * Reads return the guest view of the hash table, which has to be
  1353. * pieced together from the real hash table and the guest_rpte
  1354. * values in the revmap array.
  1355. *
  1356. * On writes, each HPTE written is considered in turn, and if it
  1357. * is valid, it is written to the HPT as if an H_ENTER with the
  1358. * exact flag set was done. When the invalid count is non-zero
  1359. * in the header written to the stream, the kernel will make
  1360. * sure that that many HPTEs are invalid, and invalidate them
  1361. * if not.
  1362. */
  1363. struct kvm_htab_ctx {
  1364. unsigned long index;
  1365. unsigned long flags;
  1366. struct kvm *kvm;
  1367. int first_pass;
  1368. };
  1369. #define HPTE_SIZE (2 * sizeof(unsigned long))
  1370. /*
  1371. * Returns 1 if this HPT entry has been modified or has pending
  1372. * R/C bit changes.
  1373. */
  1374. static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp)
  1375. {
  1376. unsigned long rcbits_unset;
  1377. if (revp->guest_rpte & HPTE_GR_MODIFIED)
  1378. return 1;
  1379. /* Also need to consider changes in reference and changed bits */
  1380. rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
  1381. if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) &&
  1382. (be64_to_cpu(hptp[1]) & rcbits_unset))
  1383. return 1;
  1384. return 0;
  1385. }
  1386. static long record_hpte(unsigned long flags, __be64 *hptp,
  1387. unsigned long *hpte, struct revmap_entry *revp,
  1388. int want_valid, int first_pass)
  1389. {
  1390. unsigned long v, r, hr;
  1391. unsigned long rcbits_unset;
  1392. int ok = 1;
  1393. int valid, dirty;
  1394. /* Unmodified entries are uninteresting except on the first pass */
  1395. dirty = hpte_dirty(revp, hptp);
  1396. if (!first_pass && !dirty)
  1397. return 0;
  1398. valid = 0;
  1399. if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) {
  1400. valid = 1;
  1401. if ((flags & KVM_GET_HTAB_BOLTED_ONLY) &&
  1402. !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED))
  1403. valid = 0;
  1404. }
  1405. if (valid != want_valid)
  1406. return 0;
  1407. v = r = 0;
  1408. if (valid || dirty) {
  1409. /* lock the HPTE so it's stable and read it */
  1410. preempt_disable();
  1411. while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
  1412. cpu_relax();
  1413. v = be64_to_cpu(hptp[0]);
  1414. hr = be64_to_cpu(hptp[1]);
  1415. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1416. v = hpte_new_to_old_v(v, hr);
  1417. hr = hpte_new_to_old_r(hr);
  1418. }
  1419. /* re-evaluate valid and dirty from synchronized HPTE value */
  1420. valid = !!(v & HPTE_V_VALID);
  1421. dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED);
  1422. /* Harvest R and C into guest view if necessary */
  1423. rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C);
  1424. if (valid && (rcbits_unset & hr)) {
  1425. revp->guest_rpte |= (hr &
  1426. (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED;
  1427. dirty = 1;
  1428. }
  1429. if (v & HPTE_V_ABSENT) {
  1430. v &= ~HPTE_V_ABSENT;
  1431. v |= HPTE_V_VALID;
  1432. valid = 1;
  1433. }
  1434. if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED))
  1435. valid = 0;
  1436. r = revp->guest_rpte;
  1437. /* only clear modified if this is the right sort of entry */
  1438. if (valid == want_valid && dirty) {
  1439. r &= ~HPTE_GR_MODIFIED;
  1440. revp->guest_rpte = r;
  1441. }
  1442. unlock_hpte(hptp, be64_to_cpu(hptp[0]));
  1443. preempt_enable();
  1444. if (!(valid == want_valid && (first_pass || dirty)))
  1445. ok = 0;
  1446. }
  1447. hpte[0] = cpu_to_be64(v);
  1448. hpte[1] = cpu_to_be64(r);
  1449. return ok;
  1450. }
  1451. static ssize_t kvm_htab_read(struct file *file, char __user *buf,
  1452. size_t count, loff_t *ppos)
  1453. {
  1454. struct kvm_htab_ctx *ctx = file->private_data;
  1455. struct kvm *kvm = ctx->kvm;
  1456. struct kvm_get_htab_header hdr;
  1457. __be64 *hptp;
  1458. struct revmap_entry *revp;
  1459. unsigned long i, nb, nw;
  1460. unsigned long __user *lbuf;
  1461. struct kvm_get_htab_header __user *hptr;
  1462. unsigned long flags;
  1463. int first_pass;
  1464. unsigned long hpte[2];
  1465. if (!access_ok(buf, count))
  1466. return -EFAULT;
  1467. if (kvm_is_radix(kvm))
  1468. return 0;
  1469. first_pass = ctx->first_pass;
  1470. flags = ctx->flags;
  1471. i = ctx->index;
  1472. hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
  1473. revp = kvm->arch.hpt.rev + i;
  1474. lbuf = (unsigned long __user *)buf;
  1475. nb = 0;
  1476. while (nb + sizeof(hdr) + HPTE_SIZE < count) {
  1477. /* Initialize header */
  1478. hptr = (struct kvm_get_htab_header __user *)buf;
  1479. hdr.n_valid = 0;
  1480. hdr.n_invalid = 0;
  1481. nw = nb;
  1482. nb += sizeof(hdr);
  1483. lbuf = (unsigned long __user *)(buf + sizeof(hdr));
  1484. /* Skip uninteresting entries, i.e. clean on not-first pass */
  1485. if (!first_pass) {
  1486. while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
  1487. !hpte_dirty(revp, hptp)) {
  1488. ++i;
  1489. hptp += 2;
  1490. ++revp;
  1491. }
  1492. }
  1493. hdr.index = i;
  1494. /* Grab a series of valid entries */
  1495. while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
  1496. hdr.n_valid < 0xffff &&
  1497. nb + HPTE_SIZE < count &&
  1498. record_hpte(flags, hptp, hpte, revp, 1, first_pass)) {
  1499. /* valid entry, write it out */
  1500. ++hdr.n_valid;
  1501. if (__put_user(hpte[0], lbuf) ||
  1502. __put_user(hpte[1], lbuf + 1))
  1503. return -EFAULT;
  1504. nb += HPTE_SIZE;
  1505. lbuf += 2;
  1506. ++i;
  1507. hptp += 2;
  1508. ++revp;
  1509. }
  1510. /* Now skip invalid entries while we can */
  1511. while (i < kvmppc_hpt_npte(&kvm->arch.hpt) &&
  1512. hdr.n_invalid < 0xffff &&
  1513. record_hpte(flags, hptp, hpte, revp, 0, first_pass)) {
  1514. /* found an invalid entry */
  1515. ++hdr.n_invalid;
  1516. ++i;
  1517. hptp += 2;
  1518. ++revp;
  1519. }
  1520. if (hdr.n_valid || hdr.n_invalid) {
  1521. /* write back the header */
  1522. if (__copy_to_user(hptr, &hdr, sizeof(hdr)))
  1523. return -EFAULT;
  1524. nw = nb;
  1525. buf = (char __user *)lbuf;
  1526. } else {
  1527. nb = nw;
  1528. }
  1529. /* Check if we've wrapped around the hash table */
  1530. if (i >= kvmppc_hpt_npte(&kvm->arch.hpt)) {
  1531. i = 0;
  1532. ctx->first_pass = 0;
  1533. break;
  1534. }
  1535. }
  1536. ctx->index = i;
  1537. return nb;
  1538. }
  1539. static ssize_t kvm_htab_write(struct file *file, const char __user *buf,
  1540. size_t count, loff_t *ppos)
  1541. {
  1542. struct kvm_htab_ctx *ctx = file->private_data;
  1543. struct kvm *kvm = ctx->kvm;
  1544. struct kvm_get_htab_header hdr;
  1545. unsigned long i, j;
  1546. unsigned long v, r;
  1547. unsigned long __user *lbuf;
  1548. __be64 *hptp;
  1549. unsigned long tmp[2];
  1550. ssize_t nb;
  1551. long int err, ret;
  1552. int mmu_ready;
  1553. int pshift;
  1554. if (!access_ok(buf, count))
  1555. return -EFAULT;
  1556. if (kvm_is_radix(kvm))
  1557. return -EINVAL;
  1558. /* lock out vcpus from running while we're doing this */
  1559. mutex_lock(&kvm->arch.mmu_setup_lock);
  1560. mmu_ready = kvm->arch.mmu_ready;
  1561. if (mmu_ready) {
  1562. kvm->arch.mmu_ready = 0; /* temporarily */
  1563. /* order mmu_ready vs. vcpus_running */
  1564. smp_mb();
  1565. if (atomic_read(&kvm->arch.vcpus_running)) {
  1566. kvm->arch.mmu_ready = 1;
  1567. mutex_unlock(&kvm->arch.mmu_setup_lock);
  1568. return -EBUSY;
  1569. }
  1570. }
  1571. err = 0;
  1572. for (nb = 0; nb + sizeof(hdr) <= count; ) {
  1573. err = -EFAULT;
  1574. if (__copy_from_user(&hdr, buf, sizeof(hdr)))
  1575. break;
  1576. err = 0;
  1577. if (nb + hdr.n_valid * HPTE_SIZE > count)
  1578. break;
  1579. nb += sizeof(hdr);
  1580. buf += sizeof(hdr);
  1581. err = -EINVAL;
  1582. i = hdr.index;
  1583. if (i >= kvmppc_hpt_npte(&kvm->arch.hpt) ||
  1584. i + hdr.n_valid + hdr.n_invalid > kvmppc_hpt_npte(&kvm->arch.hpt))
  1585. break;
  1586. hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
  1587. lbuf = (unsigned long __user *)buf;
  1588. for (j = 0; j < hdr.n_valid; ++j) {
  1589. __be64 hpte_v;
  1590. __be64 hpte_r;
  1591. err = -EFAULT;
  1592. if (__get_user(hpte_v, lbuf) ||
  1593. __get_user(hpte_r, lbuf + 1))
  1594. goto out;
  1595. v = be64_to_cpu(hpte_v);
  1596. r = be64_to_cpu(hpte_r);
  1597. err = -EINVAL;
  1598. if (!(v & HPTE_V_VALID))
  1599. goto out;
  1600. pshift = kvmppc_hpte_base_page_shift(v, r);
  1601. if (pshift <= 0)
  1602. goto out;
  1603. lbuf += 2;
  1604. nb += HPTE_SIZE;
  1605. if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
  1606. kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
  1607. err = -EIO;
  1608. ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r,
  1609. tmp);
  1610. if (ret != H_SUCCESS) {
  1611. pr_err("%s ret %ld i=%ld v=%lx r=%lx\n", __func__, ret, i, v, r);
  1612. goto out;
  1613. }
  1614. if (!mmu_ready && is_vrma_hpte(v)) {
  1615. unsigned long senc, lpcr;
  1616. senc = slb_pgsize_encoding(1ul << pshift);
  1617. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1618. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1619. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  1620. lpcr = senc << (LPCR_VRMASD_SH - 4);
  1621. kvmppc_update_lpcr(kvm, lpcr,
  1622. LPCR_VRMASD);
  1623. } else {
  1624. kvmppc_setup_partition_table(kvm);
  1625. }
  1626. mmu_ready = 1;
  1627. }
  1628. ++i;
  1629. hptp += 2;
  1630. }
  1631. for (j = 0; j < hdr.n_invalid; ++j) {
  1632. if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))
  1633. kvmppc_do_h_remove(kvm, 0, i, 0, tmp);
  1634. ++i;
  1635. hptp += 2;
  1636. }
  1637. err = 0;
  1638. }
  1639. out:
  1640. /* Order HPTE updates vs. mmu_ready */
  1641. smp_wmb();
  1642. kvm->arch.mmu_ready = mmu_ready;
  1643. mutex_unlock(&kvm->arch.mmu_setup_lock);
  1644. if (err)
  1645. return err;
  1646. return nb;
  1647. }
  1648. static int kvm_htab_release(struct inode *inode, struct file *filp)
  1649. {
  1650. struct kvm_htab_ctx *ctx = filp->private_data;
  1651. filp->private_data = NULL;
  1652. if (!(ctx->flags & KVM_GET_HTAB_WRITE))
  1653. atomic_dec(&ctx->kvm->arch.hpte_mod_interest);
  1654. kvm_put_kvm(ctx->kvm);
  1655. kfree(ctx);
  1656. return 0;
  1657. }
  1658. static const struct file_operations kvm_htab_fops = {
  1659. .read = kvm_htab_read,
  1660. .write = kvm_htab_write,
  1661. .llseek = default_llseek,
  1662. .release = kvm_htab_release,
  1663. };
  1664. int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf)
  1665. {
  1666. int ret;
  1667. struct kvm_htab_ctx *ctx;
  1668. int rwflag;
  1669. /* reject flags we don't recognize */
  1670. if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE))
  1671. return -EINVAL;
  1672. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1673. if (!ctx)
  1674. return -ENOMEM;
  1675. kvm_get_kvm(kvm);
  1676. ctx->kvm = kvm;
  1677. ctx->index = ghf->start_index;
  1678. ctx->flags = ghf->flags;
  1679. ctx->first_pass = 1;
  1680. rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY;
  1681. ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC);
  1682. if (ret < 0) {
  1683. kfree(ctx);
  1684. kvm_put_kvm_no_destroy(kvm);
  1685. return ret;
  1686. }
  1687. if (rwflag == O_RDONLY) {
  1688. mutex_lock(&kvm->slots_lock);
  1689. atomic_inc(&kvm->arch.hpte_mod_interest);
  1690. /* make sure kvmppc_do_h_enter etc. see the increment */
  1691. synchronize_srcu_expedited(&kvm->srcu);
  1692. mutex_unlock(&kvm->slots_lock);
  1693. }
  1694. return ret;
  1695. }
  1696. struct debugfs_htab_state {
  1697. struct kvm *kvm;
  1698. struct mutex mutex;
  1699. unsigned long hpt_index;
  1700. int chars_left;
  1701. int buf_index;
  1702. char buf[64];
  1703. };
  1704. static int debugfs_htab_open(struct inode *inode, struct file *file)
  1705. {
  1706. struct kvm *kvm = inode->i_private;
  1707. struct debugfs_htab_state *p;
  1708. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1709. if (!p)
  1710. return -ENOMEM;
  1711. kvm_get_kvm(kvm);
  1712. p->kvm = kvm;
  1713. mutex_init(&p->mutex);
  1714. file->private_data = p;
  1715. return nonseekable_open(inode, file);
  1716. }
  1717. static int debugfs_htab_release(struct inode *inode, struct file *file)
  1718. {
  1719. struct debugfs_htab_state *p = file->private_data;
  1720. kvm_put_kvm(p->kvm);
  1721. kfree(p);
  1722. return 0;
  1723. }
  1724. static ssize_t debugfs_htab_read(struct file *file, char __user *buf,
  1725. size_t len, loff_t *ppos)
  1726. {
  1727. struct debugfs_htab_state *p = file->private_data;
  1728. ssize_t ret, r;
  1729. unsigned long i, n;
  1730. unsigned long v, hr, gr;
  1731. struct kvm *kvm;
  1732. __be64 *hptp;
  1733. kvm = p->kvm;
  1734. if (kvm_is_radix(kvm))
  1735. return 0;
  1736. ret = mutex_lock_interruptible(&p->mutex);
  1737. if (ret)
  1738. return ret;
  1739. if (p->chars_left) {
  1740. n = p->chars_left;
  1741. if (n > len)
  1742. n = len;
  1743. r = copy_to_user(buf, p->buf + p->buf_index, n);
  1744. n -= r;
  1745. p->chars_left -= n;
  1746. p->buf_index += n;
  1747. buf += n;
  1748. len -= n;
  1749. ret = n;
  1750. if (r) {
  1751. if (!n)
  1752. ret = -EFAULT;
  1753. goto out;
  1754. }
  1755. }
  1756. i = p->hpt_index;
  1757. hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE));
  1758. for (; len != 0 && i < kvmppc_hpt_npte(&kvm->arch.hpt);
  1759. ++i, hptp += 2) {
  1760. if (!(be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)))
  1761. continue;
  1762. /* lock the HPTE so it's stable and read it */
  1763. preempt_disable();
  1764. while (!try_lock_hpte(hptp, HPTE_V_HVLOCK))
  1765. cpu_relax();
  1766. v = be64_to_cpu(hptp[0]) & ~HPTE_V_HVLOCK;
  1767. hr = be64_to_cpu(hptp[1]);
  1768. gr = kvm->arch.hpt.rev[i].guest_rpte;
  1769. unlock_hpte(hptp, v);
  1770. preempt_enable();
  1771. if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
  1772. continue;
  1773. n = scnprintf(p->buf, sizeof(p->buf),
  1774. "%6lx %.16lx %.16lx %.16lx\n",
  1775. i, v, hr, gr);
  1776. p->chars_left = n;
  1777. if (n > len)
  1778. n = len;
  1779. r = copy_to_user(buf, p->buf, n);
  1780. n -= r;
  1781. p->chars_left -= n;
  1782. p->buf_index = n;
  1783. buf += n;
  1784. len -= n;
  1785. ret += n;
  1786. if (r) {
  1787. if (!ret)
  1788. ret = -EFAULT;
  1789. goto out;
  1790. }
  1791. }
  1792. p->hpt_index = i;
  1793. out:
  1794. mutex_unlock(&p->mutex);
  1795. return ret;
  1796. }
  1797. static ssize_t debugfs_htab_write(struct file *file, const char __user *buf,
  1798. size_t len, loff_t *ppos)
  1799. {
  1800. return -EACCES;
  1801. }
  1802. static const struct file_operations debugfs_htab_fops = {
  1803. .owner = THIS_MODULE,
  1804. .open = debugfs_htab_open,
  1805. .release = debugfs_htab_release,
  1806. .read = debugfs_htab_read,
  1807. .write = debugfs_htab_write,
  1808. .llseek = generic_file_llseek,
  1809. };
  1810. void kvmppc_mmu_debugfs_init(struct kvm *kvm)
  1811. {
  1812. debugfs_create_file("htab", 0400, kvm->debugfs_dentry, kvm,
  1813. &debugfs_htab_fops);
  1814. }
  1815. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  1816. {
  1817. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  1818. vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */
  1819. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  1820. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  1821. }