book3s_64_mmu_hv.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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 <asm/tlbflush.h>
  27. #include <asm/kvm_ppc.h>
  28. #include <asm/kvm_book3s.h>
  29. #include <asm/mmu-hash64.h>
  30. #include <asm/hvcall.h>
  31. #include <asm/synch.h>
  32. #include <asm/ppc-opcode.h>
  33. #include <asm/cputable.h>
  34. /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
  35. #define MAX_LPID_970 63
  36. #define NR_LPIDS (LPID_RSVD + 1)
  37. unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)];
  38. long kvmppc_alloc_hpt(struct kvm *kvm)
  39. {
  40. unsigned long hpt;
  41. unsigned long lpid;
  42. struct revmap_entry *rev;
  43. struct kvmppc_linear_info *li;
  44. /* Allocate guest's hashed page table */
  45. li = kvm_alloc_hpt();
  46. if (li) {
  47. /* using preallocated memory */
  48. hpt = (ulong)li->base_virt;
  49. kvm->arch.hpt_li = li;
  50. } else {
  51. /* using dynamic memory */
  52. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
  53. __GFP_NOWARN, HPT_ORDER - PAGE_SHIFT);
  54. }
  55. if (!hpt) {
  56. pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
  57. return -ENOMEM;
  58. }
  59. kvm->arch.hpt_virt = hpt;
  60. /* Allocate reverse map array */
  61. rev = vmalloc(sizeof(struct revmap_entry) * HPT_NPTE);
  62. if (!rev) {
  63. pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
  64. goto out_freehpt;
  65. }
  66. kvm->arch.revmap = rev;
  67. /* Allocate the guest's logical partition ID */
  68. do {
  69. lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS);
  70. if (lpid >= NR_LPIDS) {
  71. pr_err("kvm_alloc_hpt: No LPIDs free\n");
  72. goto out_freeboth;
  73. }
  74. } while (test_and_set_bit(lpid, lpid_inuse));
  75. kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18);
  76. kvm->arch.lpid = lpid;
  77. pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid);
  78. return 0;
  79. out_freeboth:
  80. vfree(rev);
  81. out_freehpt:
  82. free_pages(hpt, HPT_ORDER - PAGE_SHIFT);
  83. return -ENOMEM;
  84. }
  85. void kvmppc_free_hpt(struct kvm *kvm)
  86. {
  87. clear_bit(kvm->arch.lpid, lpid_inuse);
  88. vfree(kvm->arch.revmap);
  89. if (kvm->arch.hpt_li)
  90. kvm_release_hpt(kvm->arch.hpt_li);
  91. else
  92. free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
  93. }
  94. /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
  95. static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
  96. {
  97. return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
  98. }
  99. /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
  100. static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
  101. {
  102. return (pgsize == 0x10000) ? 0x1000 : 0;
  103. }
  104. void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
  105. unsigned long porder)
  106. {
  107. unsigned long i;
  108. unsigned long npages;
  109. unsigned long hp_v, hp_r;
  110. unsigned long addr, hash;
  111. unsigned long psize;
  112. unsigned long hp0, hp1;
  113. long ret;
  114. psize = 1ul << porder;
  115. npages = memslot->npages >> (porder - PAGE_SHIFT);
  116. /* VRMA can't be > 1TB */
  117. if (npages > 1ul << (40 - porder))
  118. npages = 1ul << (40 - porder);
  119. /* Can't use more than 1 HPTE per HPTEG */
  120. if (npages > HPT_NPTEG)
  121. npages = HPT_NPTEG;
  122. hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
  123. HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
  124. hp1 = hpte1_pgsize_encoding(psize) |
  125. HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
  126. for (i = 0; i < npages; ++i) {
  127. addr = i << porder;
  128. /* can't use hpt_hash since va > 64 bits */
  129. hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
  130. /*
  131. * We assume that the hash table is empty and no
  132. * vcpus are using it at this stage. Since we create
  133. * at most one HPTE per HPTEG, we just assume entry 7
  134. * is available and use it.
  135. */
  136. hash = (hash << 3) + 7;
  137. hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
  138. hp_r = hp1 | addr;
  139. ret = kvmppc_virtmode_h_enter(vcpu, H_EXACT, hash, hp_v, hp_r);
  140. if (ret != H_SUCCESS) {
  141. pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
  142. addr, ret);
  143. break;
  144. }
  145. }
  146. }
  147. int kvmppc_mmu_hv_init(void)
  148. {
  149. unsigned long host_lpid, rsvd_lpid;
  150. if (!cpu_has_feature(CPU_FTR_HVMODE))
  151. return -EINVAL;
  152. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  153. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  154. host_lpid = mfspr(SPRN_LPID); /* POWER7 */
  155. rsvd_lpid = LPID_RSVD;
  156. } else {
  157. host_lpid = 0; /* PPC970 */
  158. rsvd_lpid = MAX_LPID_970;
  159. }
  160. set_bit(host_lpid, lpid_inuse);
  161. /* rsvd_lpid is reserved for use in partition switching */
  162. set_bit(rsvd_lpid, lpid_inuse);
  163. return 0;
  164. }
  165. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  166. {
  167. }
  168. static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
  169. {
  170. kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
  171. }
  172. /*
  173. * This is called to get a reference to a guest page if there isn't
  174. * one already in the kvm->arch.slot_phys[][] arrays.
  175. */
  176. static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
  177. struct kvm_memory_slot *memslot,
  178. unsigned long psize)
  179. {
  180. unsigned long start;
  181. long np, err;
  182. struct page *page, *hpage, *pages[1];
  183. unsigned long s, pgsize;
  184. unsigned long *physp;
  185. unsigned int is_io, got, pgorder;
  186. struct vm_area_struct *vma;
  187. unsigned long pfn, i, npages;
  188. physp = kvm->arch.slot_phys[memslot->id];
  189. if (!physp)
  190. return -EINVAL;
  191. if (physp[gfn - memslot->base_gfn])
  192. return 0;
  193. is_io = 0;
  194. got = 0;
  195. page = NULL;
  196. pgsize = psize;
  197. err = -EINVAL;
  198. start = gfn_to_hva_memslot(memslot, gfn);
  199. /* Instantiate and get the page we want access to */
  200. np = get_user_pages_fast(start, 1, 1, pages);
  201. if (np != 1) {
  202. /* Look up the vma for the page */
  203. down_read(&current->mm->mmap_sem);
  204. vma = find_vma(current->mm, start);
  205. if (!vma || vma->vm_start > start ||
  206. start + psize > vma->vm_end ||
  207. !(vma->vm_flags & VM_PFNMAP))
  208. goto up_err;
  209. is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
  210. pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  211. /* check alignment of pfn vs. requested page size */
  212. if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1)))
  213. goto up_err;
  214. up_read(&current->mm->mmap_sem);
  215. } else {
  216. page = pages[0];
  217. got = KVMPPC_GOT_PAGE;
  218. /* See if this is a large page */
  219. s = PAGE_SIZE;
  220. if (PageHuge(page)) {
  221. hpage = compound_head(page);
  222. s <<= compound_order(hpage);
  223. /* Get the whole large page if slot alignment is ok */
  224. if (s > psize && slot_is_aligned(memslot, s) &&
  225. !(memslot->userspace_addr & (s - 1))) {
  226. start &= ~(s - 1);
  227. pgsize = s;
  228. page = hpage;
  229. }
  230. }
  231. if (s < psize)
  232. goto out;
  233. pfn = page_to_pfn(page);
  234. }
  235. npages = pgsize >> PAGE_SHIFT;
  236. pgorder = __ilog2(npages);
  237. physp += (gfn - memslot->base_gfn) & ~(npages - 1);
  238. spin_lock(&kvm->arch.slot_phys_lock);
  239. for (i = 0; i < npages; ++i) {
  240. if (!physp[i]) {
  241. physp[i] = ((pfn + i) << PAGE_SHIFT) +
  242. got + is_io + pgorder;
  243. got = 0;
  244. }
  245. }
  246. spin_unlock(&kvm->arch.slot_phys_lock);
  247. err = 0;
  248. out:
  249. if (got) {
  250. if (PageHuge(page))
  251. page = compound_head(page);
  252. put_page(page);
  253. }
  254. return err;
  255. up_err:
  256. up_read(&current->mm->mmap_sem);
  257. return err;
  258. }
  259. /*
  260. * We come here on a H_ENTER call from the guest when we are not
  261. * using mmu notifiers and we don't have the requested page pinned
  262. * already.
  263. */
  264. long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
  265. long pte_index, unsigned long pteh, unsigned long ptel)
  266. {
  267. struct kvm *kvm = vcpu->kvm;
  268. unsigned long psize, gpa, gfn;
  269. struct kvm_memory_slot *memslot;
  270. long ret;
  271. if (kvm->arch.using_mmu_notifiers)
  272. goto do_insert;
  273. psize = hpte_page_size(pteh, ptel);
  274. if (!psize)
  275. return H_PARAMETER;
  276. pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
  277. /* Find the memslot (if any) for this address */
  278. gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
  279. gfn = gpa >> PAGE_SHIFT;
  280. memslot = gfn_to_memslot(kvm, gfn);
  281. if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
  282. if (!slot_is_aligned(memslot, psize))
  283. return H_PARAMETER;
  284. if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
  285. return H_PARAMETER;
  286. }
  287. do_insert:
  288. /* Protect linux PTE lookup from page table destruction */
  289. rcu_read_lock_sched(); /* this disables preemption too */
  290. vcpu->arch.pgdir = current->mm->pgd;
  291. ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel);
  292. rcu_read_unlock_sched();
  293. if (ret == H_TOO_HARD) {
  294. /* this can't happen */
  295. pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
  296. ret = H_RESOURCE; /* or something */
  297. }
  298. return ret;
  299. }
  300. static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
  301. gva_t eaddr)
  302. {
  303. u64 mask;
  304. int i;
  305. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  306. if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
  307. continue;
  308. if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
  309. mask = ESID_MASK_1T;
  310. else
  311. mask = ESID_MASK;
  312. if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
  313. return &vcpu->arch.slb[i];
  314. }
  315. return NULL;
  316. }
  317. static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
  318. unsigned long ea)
  319. {
  320. unsigned long ra_mask;
  321. ra_mask = hpte_page_size(v, r) - 1;
  322. return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
  323. }
  324. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  325. struct kvmppc_pte *gpte, bool data)
  326. {
  327. struct kvm *kvm = vcpu->kvm;
  328. struct kvmppc_slb *slbe;
  329. unsigned long slb_v;
  330. unsigned long pp, key;
  331. unsigned long v, gr;
  332. unsigned long *hptep;
  333. int index;
  334. int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
  335. /* Get SLB entry */
  336. if (virtmode) {
  337. slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
  338. if (!slbe)
  339. return -EINVAL;
  340. slb_v = slbe->origv;
  341. } else {
  342. /* real mode access */
  343. slb_v = vcpu->kvm->arch.vrma_slb_v;
  344. }
  345. /* Find the HPTE in the hash table */
  346. index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
  347. HPTE_V_VALID | HPTE_V_ABSENT);
  348. if (index < 0)
  349. return -ENOENT;
  350. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  351. v = hptep[0] & ~HPTE_V_HVLOCK;
  352. gr = kvm->arch.revmap[index].guest_rpte;
  353. /* Unlock the HPTE */
  354. asm volatile("lwsync" : : : "memory");
  355. hptep[0] = v;
  356. gpte->eaddr = eaddr;
  357. gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
  358. /* Get PP bits and key for permission check */
  359. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  360. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  361. key &= slb_v;
  362. /* Calculate permissions */
  363. gpte->may_read = hpte_read_permission(pp, key);
  364. gpte->may_write = hpte_write_permission(pp, key);
  365. gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
  366. /* Storage key permission check for POWER7 */
  367. if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
  368. int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
  369. if (amrfield & 1)
  370. gpte->may_read = 0;
  371. if (amrfield & 2)
  372. gpte->may_write = 0;
  373. }
  374. /* Get the guest physical address */
  375. gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
  376. return 0;
  377. }
  378. /*
  379. * Quick test for whether an instruction is a load or a store.
  380. * If the instruction is a load or a store, then this will indicate
  381. * which it is, at least on server processors. (Embedded processors
  382. * have some external PID instructions that don't follow the rule
  383. * embodied here.) If the instruction isn't a load or store, then
  384. * this doesn't return anything useful.
  385. */
  386. static int instruction_is_store(unsigned int instr)
  387. {
  388. unsigned int mask;
  389. mask = 0x10000000;
  390. if ((instr & 0xfc000000) == 0x7c000000)
  391. mask = 0x100; /* major opcode 31 */
  392. return (instr & mask) != 0;
  393. }
  394. static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
  395. unsigned long gpa, int is_store)
  396. {
  397. int ret;
  398. u32 last_inst;
  399. unsigned long srr0 = kvmppc_get_pc(vcpu);
  400. /* We try to load the last instruction. We don't let
  401. * emulate_instruction do it as it doesn't check what
  402. * kvmppc_ld returns.
  403. * If we fail, we just return to the guest and try executing it again.
  404. */
  405. if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) {
  406. ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
  407. if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED)
  408. return RESUME_GUEST;
  409. vcpu->arch.last_inst = last_inst;
  410. }
  411. /*
  412. * WARNING: We do not know for sure whether the instruction we just
  413. * read from memory is the same that caused the fault in the first
  414. * place. If the instruction we read is neither an load or a store,
  415. * then it can't access memory, so we don't need to worry about
  416. * enforcing access permissions. So, assuming it is a load or
  417. * store, we just check that its direction (load or store) is
  418. * consistent with the original fault, since that's what we
  419. * checked the access permissions against. If there is a mismatch
  420. * we just return and retry the instruction.
  421. */
  422. if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
  423. return RESUME_GUEST;
  424. /*
  425. * Emulated accesses are emulated by looking at the hash for
  426. * translation once, then performing the access later. The
  427. * translation could be invalidated in the meantime in which
  428. * point performing the subsequent memory access on the old
  429. * physical address could possibly be a security hole for the
  430. * guest (but not the host).
  431. *
  432. * This is less of an issue for MMIO stores since they aren't
  433. * globally visible. It could be an issue for MMIO loads to
  434. * a certain extent but we'll ignore it for now.
  435. */
  436. vcpu->arch.paddr_accessed = gpa;
  437. return kvmppc_emulate_mmio(run, vcpu);
  438. }
  439. int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
  440. unsigned long ea, unsigned long dsisr)
  441. {
  442. struct kvm *kvm = vcpu->kvm;
  443. unsigned long *hptep, hpte[3], r;
  444. unsigned long mmu_seq, psize, pte_size;
  445. unsigned long gfn, hva, pfn;
  446. struct kvm_memory_slot *memslot;
  447. unsigned long *rmap;
  448. struct revmap_entry *rev;
  449. struct page *page, *pages[1];
  450. long index, ret, npages;
  451. unsigned long is_io;
  452. unsigned int writing, write_ok;
  453. struct vm_area_struct *vma;
  454. unsigned long rcbits;
  455. /*
  456. * Real-mode code has already searched the HPT and found the
  457. * entry we're interested in. Lock the entry and check that
  458. * it hasn't changed. If it has, just return and re-execute the
  459. * instruction.
  460. */
  461. if (ea != vcpu->arch.pgfault_addr)
  462. return RESUME_GUEST;
  463. index = vcpu->arch.pgfault_index;
  464. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  465. rev = &kvm->arch.revmap[index];
  466. preempt_disable();
  467. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  468. cpu_relax();
  469. hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
  470. hpte[1] = hptep[1];
  471. hpte[2] = r = rev->guest_rpte;
  472. asm volatile("lwsync" : : : "memory");
  473. hptep[0] = hpte[0];
  474. preempt_enable();
  475. if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
  476. hpte[1] != vcpu->arch.pgfault_hpte[1])
  477. return RESUME_GUEST;
  478. /* Translate the logical address and get the page */
  479. psize = hpte_page_size(hpte[0], r);
  480. gfn = hpte_rpn(r, psize);
  481. memslot = gfn_to_memslot(kvm, gfn);
  482. /* No memslot means it's an emulated MMIO region */
  483. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
  484. unsigned long gpa = (gfn << PAGE_SHIFT) | (ea & (psize - 1));
  485. return kvmppc_hv_emulate_mmio(run, vcpu, gpa,
  486. dsisr & DSISR_ISSTORE);
  487. }
  488. if (!kvm->arch.using_mmu_notifiers)
  489. return -EFAULT; /* should never get here */
  490. /* used to check for invalidations in progress */
  491. mmu_seq = kvm->mmu_notifier_seq;
  492. smp_rmb();
  493. is_io = 0;
  494. pfn = 0;
  495. page = NULL;
  496. pte_size = PAGE_SIZE;
  497. writing = (dsisr & DSISR_ISSTORE) != 0;
  498. /* If writing != 0, then the HPTE must allow writing, if we get here */
  499. write_ok = writing;
  500. hva = gfn_to_hva_memslot(memslot, gfn);
  501. npages = get_user_pages_fast(hva, 1, writing, pages);
  502. if (npages < 1) {
  503. /* Check if it's an I/O mapping */
  504. down_read(&current->mm->mmap_sem);
  505. vma = find_vma(current->mm, hva);
  506. if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
  507. (vma->vm_flags & VM_PFNMAP)) {
  508. pfn = vma->vm_pgoff +
  509. ((hva - vma->vm_start) >> PAGE_SHIFT);
  510. pte_size = psize;
  511. is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
  512. write_ok = vma->vm_flags & VM_WRITE;
  513. }
  514. up_read(&current->mm->mmap_sem);
  515. if (!pfn)
  516. return -EFAULT;
  517. } else {
  518. page = pages[0];
  519. if (PageHuge(page)) {
  520. page = compound_head(page);
  521. pte_size <<= compound_order(page);
  522. }
  523. /* if the guest wants write access, see if that is OK */
  524. if (!writing && hpte_is_writable(r)) {
  525. pte_t *ptep, pte;
  526. /*
  527. * We need to protect against page table destruction
  528. * while looking up and updating the pte.
  529. */
  530. rcu_read_lock_sched();
  531. ptep = find_linux_pte_or_hugepte(current->mm->pgd,
  532. hva, NULL);
  533. if (ptep && pte_present(*ptep)) {
  534. pte = kvmppc_read_update_linux_pte(ptep, 1);
  535. if (pte_write(pte))
  536. write_ok = 1;
  537. }
  538. rcu_read_unlock_sched();
  539. }
  540. pfn = page_to_pfn(page);
  541. }
  542. ret = -EFAULT;
  543. if (psize > pte_size)
  544. goto out_put;
  545. /* Check WIMG vs. the actual page we're accessing */
  546. if (!hpte_cache_flags_ok(r, is_io)) {
  547. if (is_io)
  548. return -EFAULT;
  549. /*
  550. * Allow guest to map emulated device memory as
  551. * uncacheable, but actually make it cacheable.
  552. */
  553. r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
  554. }
  555. /* Set the HPTE to point to pfn */
  556. r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT);
  557. if (hpte_is_writable(r) && !write_ok)
  558. r = hpte_make_readonly(r);
  559. ret = RESUME_GUEST;
  560. preempt_disable();
  561. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  562. cpu_relax();
  563. if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
  564. rev->guest_rpte != hpte[2])
  565. /* HPTE has been changed under us; let the guest retry */
  566. goto out_unlock;
  567. hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  568. rmap = &memslot->rmap[gfn - memslot->base_gfn];
  569. lock_rmap(rmap);
  570. /* Check if we might have been invalidated; let the guest retry if so */
  571. ret = RESUME_GUEST;
  572. if (mmu_notifier_retry(vcpu, mmu_seq)) {
  573. unlock_rmap(rmap);
  574. goto out_unlock;
  575. }
  576. /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
  577. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  578. r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  579. if (hptep[0] & HPTE_V_VALID) {
  580. /* HPTE was previously valid, so we need to invalidate it */
  581. unlock_rmap(rmap);
  582. hptep[0] |= HPTE_V_ABSENT;
  583. kvmppc_invalidate_hpte(kvm, hptep, index);
  584. /* don't lose previous R and C bits */
  585. r |= hptep[1] & (HPTE_R_R | HPTE_R_C);
  586. } else {
  587. kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
  588. }
  589. hptep[1] = r;
  590. eieio();
  591. hptep[0] = hpte[0];
  592. asm volatile("ptesync" : : : "memory");
  593. preempt_enable();
  594. if (page && hpte_is_writable(r))
  595. SetPageDirty(page);
  596. out_put:
  597. if (page)
  598. put_page(page);
  599. return ret;
  600. out_unlock:
  601. hptep[0] &= ~HPTE_V_HVLOCK;
  602. preempt_enable();
  603. goto out_put;
  604. }
  605. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  606. int (*handler)(struct kvm *kvm, unsigned long *rmapp,
  607. unsigned long gfn))
  608. {
  609. int ret;
  610. int retval = 0;
  611. struct kvm_memslots *slots;
  612. struct kvm_memory_slot *memslot;
  613. slots = kvm_memslots(kvm);
  614. kvm_for_each_memslot(memslot, slots) {
  615. unsigned long start = memslot->userspace_addr;
  616. unsigned long end;
  617. end = start + (memslot->npages << PAGE_SHIFT);
  618. if (hva >= start && hva < end) {
  619. gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
  620. ret = handler(kvm, &memslot->rmap[gfn_offset],
  621. memslot->base_gfn + gfn_offset);
  622. retval |= ret;
  623. }
  624. }
  625. return retval;
  626. }
  627. static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
  628. unsigned long gfn)
  629. {
  630. struct revmap_entry *rev = kvm->arch.revmap;
  631. unsigned long h, i, j;
  632. unsigned long *hptep;
  633. unsigned long ptel, psize, rcbits;
  634. for (;;) {
  635. lock_rmap(rmapp);
  636. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  637. unlock_rmap(rmapp);
  638. break;
  639. }
  640. /*
  641. * To avoid an ABBA deadlock with the HPTE lock bit,
  642. * we can't spin on the HPTE lock while holding the
  643. * rmap chain lock.
  644. */
  645. i = *rmapp & KVMPPC_RMAP_INDEX;
  646. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  647. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  648. /* unlock rmap before spinning on the HPTE lock */
  649. unlock_rmap(rmapp);
  650. while (hptep[0] & HPTE_V_HVLOCK)
  651. cpu_relax();
  652. continue;
  653. }
  654. j = rev[i].forw;
  655. if (j == i) {
  656. /* chain is now empty */
  657. *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
  658. } else {
  659. /* remove i from chain */
  660. h = rev[i].back;
  661. rev[h].forw = j;
  662. rev[j].back = h;
  663. rev[i].forw = rev[i].back = i;
  664. *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
  665. }
  666. /* Now check and modify the HPTE */
  667. ptel = rev[i].guest_rpte;
  668. psize = hpte_page_size(hptep[0], ptel);
  669. if ((hptep[0] & HPTE_V_VALID) &&
  670. hpte_rpn(ptel, psize) == gfn) {
  671. hptep[0] |= HPTE_V_ABSENT;
  672. kvmppc_invalidate_hpte(kvm, hptep, i);
  673. /* Harvest R and C */
  674. rcbits = hptep[1] & (HPTE_R_R | HPTE_R_C);
  675. *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
  676. rev[i].guest_rpte = ptel | rcbits;
  677. }
  678. unlock_rmap(rmapp);
  679. hptep[0] &= ~HPTE_V_HVLOCK;
  680. }
  681. return 0;
  682. }
  683. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  684. {
  685. if (kvm->arch.using_mmu_notifiers)
  686. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  687. return 0;
  688. }
  689. static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  690. unsigned long gfn)
  691. {
  692. struct revmap_entry *rev = kvm->arch.revmap;
  693. unsigned long head, i, j;
  694. unsigned long *hptep;
  695. int ret = 0;
  696. retry:
  697. lock_rmap(rmapp);
  698. if (*rmapp & KVMPPC_RMAP_REFERENCED) {
  699. *rmapp &= ~KVMPPC_RMAP_REFERENCED;
  700. ret = 1;
  701. }
  702. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  703. unlock_rmap(rmapp);
  704. return ret;
  705. }
  706. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  707. do {
  708. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  709. j = rev[i].forw;
  710. /* If this HPTE isn't referenced, ignore it */
  711. if (!(hptep[1] & HPTE_R_R))
  712. continue;
  713. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  714. /* unlock rmap before spinning on the HPTE lock */
  715. unlock_rmap(rmapp);
  716. while (hptep[0] & HPTE_V_HVLOCK)
  717. cpu_relax();
  718. goto retry;
  719. }
  720. /* Now check and modify the HPTE */
  721. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
  722. kvmppc_clear_ref_hpte(kvm, hptep, i);
  723. rev[i].guest_rpte |= HPTE_R_R;
  724. ret = 1;
  725. }
  726. hptep[0] &= ~HPTE_V_HVLOCK;
  727. } while ((i = j) != head);
  728. unlock_rmap(rmapp);
  729. return ret;
  730. }
  731. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  732. {
  733. if (!kvm->arch.using_mmu_notifiers)
  734. return 0;
  735. return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
  736. }
  737. static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  738. unsigned long gfn)
  739. {
  740. struct revmap_entry *rev = kvm->arch.revmap;
  741. unsigned long head, i, j;
  742. unsigned long *hp;
  743. int ret = 1;
  744. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  745. return 1;
  746. lock_rmap(rmapp);
  747. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  748. goto out;
  749. if (*rmapp & KVMPPC_RMAP_PRESENT) {
  750. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  751. do {
  752. hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
  753. j = rev[i].forw;
  754. if (hp[1] & HPTE_R_R)
  755. goto out;
  756. } while ((i = j) != head);
  757. }
  758. ret = 0;
  759. out:
  760. unlock_rmap(rmapp);
  761. return ret;
  762. }
  763. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  764. {
  765. if (!kvm->arch.using_mmu_notifiers)
  766. return 0;
  767. return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
  768. }
  769. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  770. {
  771. if (!kvm->arch.using_mmu_notifiers)
  772. return;
  773. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  774. }
  775. static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp)
  776. {
  777. struct revmap_entry *rev = kvm->arch.revmap;
  778. unsigned long head, i, j;
  779. unsigned long *hptep;
  780. int ret = 0;
  781. retry:
  782. lock_rmap(rmapp);
  783. if (*rmapp & KVMPPC_RMAP_CHANGED) {
  784. *rmapp &= ~KVMPPC_RMAP_CHANGED;
  785. ret = 1;
  786. }
  787. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  788. unlock_rmap(rmapp);
  789. return ret;
  790. }
  791. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  792. do {
  793. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  794. j = rev[i].forw;
  795. if (!(hptep[1] & HPTE_R_C))
  796. continue;
  797. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  798. /* unlock rmap before spinning on the HPTE lock */
  799. unlock_rmap(rmapp);
  800. while (hptep[0] & HPTE_V_HVLOCK)
  801. cpu_relax();
  802. goto retry;
  803. }
  804. /* Now check and modify the HPTE */
  805. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
  806. /* need to make it temporarily absent to clear C */
  807. hptep[0] |= HPTE_V_ABSENT;
  808. kvmppc_invalidate_hpte(kvm, hptep, i);
  809. hptep[1] &= ~HPTE_R_C;
  810. eieio();
  811. hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  812. rev[i].guest_rpte |= HPTE_R_C;
  813. ret = 1;
  814. }
  815. hptep[0] &= ~HPTE_V_HVLOCK;
  816. } while ((i = j) != head);
  817. unlock_rmap(rmapp);
  818. return ret;
  819. }
  820. long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  821. {
  822. unsigned long i;
  823. unsigned long *rmapp, *map;
  824. preempt_disable();
  825. rmapp = memslot->rmap;
  826. map = memslot->dirty_bitmap;
  827. for (i = 0; i < memslot->npages; ++i) {
  828. if (kvm_test_clear_dirty(kvm, rmapp))
  829. __set_bit_le(i, map);
  830. ++rmapp;
  831. }
  832. preempt_enable();
  833. return 0;
  834. }
  835. void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
  836. unsigned long *nb_ret)
  837. {
  838. struct kvm_memory_slot *memslot;
  839. unsigned long gfn = gpa >> PAGE_SHIFT;
  840. struct page *page, *pages[1];
  841. int npages;
  842. unsigned long hva, psize, offset;
  843. unsigned long pa;
  844. unsigned long *physp;
  845. memslot = gfn_to_memslot(kvm, gfn);
  846. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  847. return NULL;
  848. if (!kvm->arch.using_mmu_notifiers) {
  849. physp = kvm->arch.slot_phys[memslot->id];
  850. if (!physp)
  851. return NULL;
  852. physp += gfn - memslot->base_gfn;
  853. pa = *physp;
  854. if (!pa) {
  855. if (kvmppc_get_guest_page(kvm, gfn, memslot,
  856. PAGE_SIZE) < 0)
  857. return NULL;
  858. pa = *physp;
  859. }
  860. page = pfn_to_page(pa >> PAGE_SHIFT);
  861. } else {
  862. hva = gfn_to_hva_memslot(memslot, gfn);
  863. npages = get_user_pages_fast(hva, 1, 1, pages);
  864. if (npages < 1)
  865. return NULL;
  866. page = pages[0];
  867. }
  868. psize = PAGE_SIZE;
  869. if (PageHuge(page)) {
  870. page = compound_head(page);
  871. psize <<= compound_order(page);
  872. }
  873. if (!kvm->arch.using_mmu_notifiers)
  874. get_page(page);
  875. offset = gpa & (psize - 1);
  876. if (nb_ret)
  877. *nb_ret = psize - offset;
  878. return page_address(page) + offset;
  879. }
  880. void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
  881. {
  882. struct page *page = virt_to_page(va);
  883. page = compound_head(page);
  884. put_page(page);
  885. }
  886. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  887. {
  888. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  889. if (cpu_has_feature(CPU_FTR_ARCH_206))
  890. vcpu->arch.slb_nr = 32; /* POWER7 */
  891. else
  892. vcpu->arch.slb_nr = 64;
  893. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  894. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  895. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  896. }