book3s_64_mmu_hv.c 42 KB

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