book3s_64_mmu_hv.c 53 KB

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