book3s_64_mmu_hv.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. get_page(hpage);
  229. put_page(page);
  230. page = hpage;
  231. }
  232. }
  233. if (s < psize)
  234. goto out;
  235. pfn = page_to_pfn(page);
  236. }
  237. npages = pgsize >> PAGE_SHIFT;
  238. pgorder = __ilog2(npages);
  239. physp += (gfn - memslot->base_gfn) & ~(npages - 1);
  240. spin_lock(&kvm->arch.slot_phys_lock);
  241. for (i = 0; i < npages; ++i) {
  242. if (!physp[i]) {
  243. physp[i] = ((pfn + i) << PAGE_SHIFT) +
  244. got + is_io + pgorder;
  245. got = 0;
  246. }
  247. }
  248. spin_unlock(&kvm->arch.slot_phys_lock);
  249. err = 0;
  250. out:
  251. if (got)
  252. put_page(page);
  253. return err;
  254. up_err:
  255. up_read(&current->mm->mmap_sem);
  256. return err;
  257. }
  258. /*
  259. * We come here on a H_ENTER call from the guest when we are not
  260. * using mmu notifiers and we don't have the requested page pinned
  261. * already.
  262. */
  263. long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
  264. long pte_index, unsigned long pteh, unsigned long ptel)
  265. {
  266. struct kvm *kvm = vcpu->kvm;
  267. unsigned long psize, gpa, gfn;
  268. struct kvm_memory_slot *memslot;
  269. long ret;
  270. if (kvm->arch.using_mmu_notifiers)
  271. goto do_insert;
  272. psize = hpte_page_size(pteh, ptel);
  273. if (!psize)
  274. return H_PARAMETER;
  275. pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
  276. /* Find the memslot (if any) for this address */
  277. gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
  278. gfn = gpa >> PAGE_SHIFT;
  279. memslot = gfn_to_memslot(kvm, gfn);
  280. if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
  281. if (!slot_is_aligned(memslot, psize))
  282. return H_PARAMETER;
  283. if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
  284. return H_PARAMETER;
  285. }
  286. do_insert:
  287. /* Protect linux PTE lookup from page table destruction */
  288. rcu_read_lock_sched(); /* this disables preemption too */
  289. vcpu->arch.pgdir = current->mm->pgd;
  290. ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel);
  291. rcu_read_unlock_sched();
  292. if (ret == H_TOO_HARD) {
  293. /* this can't happen */
  294. pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
  295. ret = H_RESOURCE; /* or something */
  296. }
  297. return ret;
  298. }
  299. static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
  300. gva_t eaddr)
  301. {
  302. u64 mask;
  303. int i;
  304. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  305. if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
  306. continue;
  307. if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
  308. mask = ESID_MASK_1T;
  309. else
  310. mask = ESID_MASK;
  311. if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
  312. return &vcpu->arch.slb[i];
  313. }
  314. return NULL;
  315. }
  316. static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
  317. unsigned long ea)
  318. {
  319. unsigned long ra_mask;
  320. ra_mask = hpte_page_size(v, r) - 1;
  321. return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
  322. }
  323. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  324. struct kvmppc_pte *gpte, bool data)
  325. {
  326. struct kvm *kvm = vcpu->kvm;
  327. struct kvmppc_slb *slbe;
  328. unsigned long slb_v;
  329. unsigned long pp, key;
  330. unsigned long v, gr;
  331. unsigned long *hptep;
  332. int index;
  333. int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
  334. /* Get SLB entry */
  335. if (virtmode) {
  336. slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
  337. if (!slbe)
  338. return -EINVAL;
  339. slb_v = slbe->origv;
  340. } else {
  341. /* real mode access */
  342. slb_v = vcpu->kvm->arch.vrma_slb_v;
  343. }
  344. preempt_disable();
  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. preempt_enable();
  350. return -ENOENT;
  351. }
  352. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  353. v = hptep[0] & ~HPTE_V_HVLOCK;
  354. gr = kvm->arch.revmap[index].guest_rpte;
  355. /* Unlock the HPTE */
  356. asm volatile("lwsync" : : : "memory");
  357. hptep[0] = v;
  358. preempt_enable();
  359. gpte->eaddr = eaddr;
  360. gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
  361. /* Get PP bits and key for permission check */
  362. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  363. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  364. key &= slb_v;
  365. /* Calculate permissions */
  366. gpte->may_read = hpte_read_permission(pp, key);
  367. gpte->may_write = hpte_write_permission(pp, key);
  368. gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
  369. /* Storage key permission check for POWER7 */
  370. if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
  371. int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
  372. if (amrfield & 1)
  373. gpte->may_read = 0;
  374. if (amrfield & 2)
  375. gpte->may_write = 0;
  376. }
  377. /* Get the guest physical address */
  378. gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
  379. return 0;
  380. }
  381. /*
  382. * Quick test for whether an instruction is a load or a store.
  383. * If the instruction is a load or a store, then this will indicate
  384. * which it is, at least on server processors. (Embedded processors
  385. * have some external PID instructions that don't follow the rule
  386. * embodied here.) If the instruction isn't a load or store, then
  387. * this doesn't return anything useful.
  388. */
  389. static int instruction_is_store(unsigned int instr)
  390. {
  391. unsigned int mask;
  392. mask = 0x10000000;
  393. if ((instr & 0xfc000000) == 0x7c000000)
  394. mask = 0x100; /* major opcode 31 */
  395. return (instr & mask) != 0;
  396. }
  397. static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
  398. unsigned long gpa, int is_store)
  399. {
  400. int ret;
  401. u32 last_inst;
  402. unsigned long srr0 = kvmppc_get_pc(vcpu);
  403. /* We try to load the last instruction. We don't let
  404. * emulate_instruction do it as it doesn't check what
  405. * kvmppc_ld returns.
  406. * If we fail, we just return to the guest and try executing it again.
  407. */
  408. if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) {
  409. ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
  410. if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED)
  411. return RESUME_GUEST;
  412. vcpu->arch.last_inst = last_inst;
  413. }
  414. /*
  415. * WARNING: We do not know for sure whether the instruction we just
  416. * read from memory is the same that caused the fault in the first
  417. * place. If the instruction we read is neither an load or a store,
  418. * then it can't access memory, so we don't need to worry about
  419. * enforcing access permissions. So, assuming it is a load or
  420. * store, we just check that its direction (load or store) is
  421. * consistent with the original fault, since that's what we
  422. * checked the access permissions against. If there is a mismatch
  423. * we just return and retry the instruction.
  424. */
  425. if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
  426. return RESUME_GUEST;
  427. /*
  428. * Emulated accesses are emulated by looking at the hash for
  429. * translation once, then performing the access later. The
  430. * translation could be invalidated in the meantime in which
  431. * point performing the subsequent memory access on the old
  432. * physical address could possibly be a security hole for the
  433. * guest (but not the host).
  434. *
  435. * This is less of an issue for MMIO stores since they aren't
  436. * globally visible. It could be an issue for MMIO loads to
  437. * a certain extent but we'll ignore it for now.
  438. */
  439. vcpu->arch.paddr_accessed = gpa;
  440. return kvmppc_emulate_mmio(run, vcpu);
  441. }
  442. int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
  443. unsigned long ea, unsigned long dsisr)
  444. {
  445. struct kvm *kvm = vcpu->kvm;
  446. unsigned long *hptep, hpte[3], r;
  447. unsigned long mmu_seq, psize, pte_size;
  448. unsigned long gfn, hva, pfn;
  449. struct kvm_memory_slot *memslot;
  450. unsigned long *rmap;
  451. struct revmap_entry *rev;
  452. struct page *page, *pages[1];
  453. long index, ret, npages;
  454. unsigned long is_io;
  455. unsigned int writing, write_ok;
  456. struct vm_area_struct *vma;
  457. unsigned long rcbits;
  458. /*
  459. * Real-mode code has already searched the HPT and found the
  460. * entry we're interested in. Lock the entry and check that
  461. * it hasn't changed. If it has, just return and re-execute the
  462. * instruction.
  463. */
  464. if (ea != vcpu->arch.pgfault_addr)
  465. return RESUME_GUEST;
  466. index = vcpu->arch.pgfault_index;
  467. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  468. rev = &kvm->arch.revmap[index];
  469. preempt_disable();
  470. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  471. cpu_relax();
  472. hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
  473. hpte[1] = hptep[1];
  474. hpte[2] = r = rev->guest_rpte;
  475. asm volatile("lwsync" : : : "memory");
  476. hptep[0] = hpte[0];
  477. preempt_enable();
  478. if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
  479. hpte[1] != vcpu->arch.pgfault_hpte[1])
  480. return RESUME_GUEST;
  481. /* Translate the logical address and get the page */
  482. psize = hpte_page_size(hpte[0], r);
  483. gfn = hpte_rpn(r, psize);
  484. memslot = gfn_to_memslot(kvm, gfn);
  485. /* No memslot means it's an emulated MMIO region */
  486. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
  487. unsigned long gpa = (gfn << PAGE_SHIFT) | (ea & (psize - 1));
  488. return kvmppc_hv_emulate_mmio(run, vcpu, gpa,
  489. dsisr & DSISR_ISSTORE);
  490. }
  491. if (!kvm->arch.using_mmu_notifiers)
  492. return -EFAULT; /* should never get here */
  493. /* used to check for invalidations in progress */
  494. mmu_seq = kvm->mmu_notifier_seq;
  495. smp_rmb();
  496. is_io = 0;
  497. pfn = 0;
  498. page = NULL;
  499. pte_size = PAGE_SIZE;
  500. writing = (dsisr & DSISR_ISSTORE) != 0;
  501. /* If writing != 0, then the HPTE must allow writing, if we get here */
  502. write_ok = writing;
  503. hva = gfn_to_hva_memslot(memslot, gfn);
  504. npages = get_user_pages_fast(hva, 1, writing, pages);
  505. if (npages < 1) {
  506. /* Check if it's an I/O mapping */
  507. down_read(&current->mm->mmap_sem);
  508. vma = find_vma(current->mm, hva);
  509. if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
  510. (vma->vm_flags & VM_PFNMAP)) {
  511. pfn = vma->vm_pgoff +
  512. ((hva - vma->vm_start) >> PAGE_SHIFT);
  513. pte_size = psize;
  514. is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
  515. write_ok = vma->vm_flags & VM_WRITE;
  516. }
  517. up_read(&current->mm->mmap_sem);
  518. if (!pfn)
  519. return -EFAULT;
  520. } else {
  521. page = pages[0];
  522. if (PageHuge(page)) {
  523. page = compound_head(page);
  524. pte_size <<= compound_order(page);
  525. }
  526. /* if the guest wants write access, see if that is OK */
  527. if (!writing && hpte_is_writable(r)) {
  528. pte_t *ptep, pte;
  529. /*
  530. * We need to protect against page table destruction
  531. * while looking up and updating the pte.
  532. */
  533. rcu_read_lock_sched();
  534. ptep = find_linux_pte_or_hugepte(current->mm->pgd,
  535. hva, NULL);
  536. if (ptep && pte_present(*ptep)) {
  537. pte = kvmppc_read_update_linux_pte(ptep, 1);
  538. if (pte_write(pte))
  539. write_ok = 1;
  540. }
  541. rcu_read_unlock_sched();
  542. }
  543. pfn = page_to_pfn(page);
  544. }
  545. ret = -EFAULT;
  546. if (psize > pte_size)
  547. goto out_put;
  548. /* Check WIMG vs. the actual page we're accessing */
  549. if (!hpte_cache_flags_ok(r, is_io)) {
  550. if (is_io)
  551. return -EFAULT;
  552. /*
  553. * Allow guest to map emulated device memory as
  554. * uncacheable, but actually make it cacheable.
  555. */
  556. r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
  557. }
  558. /* Set the HPTE to point to pfn */
  559. r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT);
  560. if (hpte_is_writable(r) && !write_ok)
  561. r = hpte_make_readonly(r);
  562. ret = RESUME_GUEST;
  563. preempt_disable();
  564. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  565. cpu_relax();
  566. if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
  567. rev->guest_rpte != hpte[2])
  568. /* HPTE has been changed under us; let the guest retry */
  569. goto out_unlock;
  570. hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  571. rmap = &memslot->rmap[gfn - memslot->base_gfn];
  572. lock_rmap(rmap);
  573. /* Check if we might have been invalidated; let the guest retry if so */
  574. ret = RESUME_GUEST;
  575. if (mmu_notifier_retry(vcpu, mmu_seq)) {
  576. unlock_rmap(rmap);
  577. goto out_unlock;
  578. }
  579. /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
  580. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  581. r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  582. if (hptep[0] & HPTE_V_VALID) {
  583. /* HPTE was previously valid, so we need to invalidate it */
  584. unlock_rmap(rmap);
  585. hptep[0] |= HPTE_V_ABSENT;
  586. kvmppc_invalidate_hpte(kvm, hptep, index);
  587. /* don't lose previous R and C bits */
  588. r |= hptep[1] & (HPTE_R_R | HPTE_R_C);
  589. } else {
  590. kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
  591. }
  592. hptep[1] = r;
  593. eieio();
  594. hptep[0] = hpte[0];
  595. asm volatile("ptesync" : : : "memory");
  596. preempt_enable();
  597. if (page && hpte_is_writable(r))
  598. SetPageDirty(page);
  599. out_put:
  600. if (page) {
  601. /*
  602. * We drop pages[0] here, not page because page might
  603. * have been set to the head page of a compound, but
  604. * we have to drop the reference on the correct tail
  605. * page to match the get inside gup()
  606. */
  607. put_page(pages[0]);
  608. }
  609. return ret;
  610. out_unlock:
  611. hptep[0] &= ~HPTE_V_HVLOCK;
  612. preempt_enable();
  613. goto out_put;
  614. }
  615. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  616. int (*handler)(struct kvm *kvm, unsigned long *rmapp,
  617. unsigned long gfn))
  618. {
  619. int ret;
  620. int retval = 0;
  621. struct kvm_memslots *slots;
  622. struct kvm_memory_slot *memslot;
  623. slots = kvm_memslots(kvm);
  624. kvm_for_each_memslot(memslot, slots) {
  625. unsigned long start = memslot->userspace_addr;
  626. unsigned long end;
  627. end = start + (memslot->npages << PAGE_SHIFT);
  628. if (hva >= start && hva < end) {
  629. gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
  630. ret = handler(kvm, &memslot->rmap[gfn_offset],
  631. memslot->base_gfn + gfn_offset);
  632. retval |= ret;
  633. }
  634. }
  635. return retval;
  636. }
  637. static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
  638. unsigned long gfn)
  639. {
  640. struct revmap_entry *rev = kvm->arch.revmap;
  641. unsigned long h, i, j;
  642. unsigned long *hptep;
  643. unsigned long ptel, psize, rcbits;
  644. for (;;) {
  645. lock_rmap(rmapp);
  646. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  647. unlock_rmap(rmapp);
  648. break;
  649. }
  650. /*
  651. * To avoid an ABBA deadlock with the HPTE lock bit,
  652. * we can't spin on the HPTE lock while holding the
  653. * rmap chain lock.
  654. */
  655. i = *rmapp & KVMPPC_RMAP_INDEX;
  656. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  657. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  658. /* unlock rmap before spinning on the HPTE lock */
  659. unlock_rmap(rmapp);
  660. while (hptep[0] & HPTE_V_HVLOCK)
  661. cpu_relax();
  662. continue;
  663. }
  664. j = rev[i].forw;
  665. if (j == i) {
  666. /* chain is now empty */
  667. *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
  668. } else {
  669. /* remove i from chain */
  670. h = rev[i].back;
  671. rev[h].forw = j;
  672. rev[j].back = h;
  673. rev[i].forw = rev[i].back = i;
  674. *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
  675. }
  676. /* Now check and modify the HPTE */
  677. ptel = rev[i].guest_rpte;
  678. psize = hpte_page_size(hptep[0], ptel);
  679. if ((hptep[0] & HPTE_V_VALID) &&
  680. hpte_rpn(ptel, psize) == gfn) {
  681. hptep[0] |= HPTE_V_ABSENT;
  682. kvmppc_invalidate_hpte(kvm, hptep, i);
  683. /* Harvest R and C */
  684. rcbits = hptep[1] & (HPTE_R_R | HPTE_R_C);
  685. *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
  686. rev[i].guest_rpte = ptel | rcbits;
  687. }
  688. unlock_rmap(rmapp);
  689. hptep[0] &= ~HPTE_V_HVLOCK;
  690. }
  691. return 0;
  692. }
  693. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  694. {
  695. if (kvm->arch.using_mmu_notifiers)
  696. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  697. return 0;
  698. }
  699. static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  700. unsigned long gfn)
  701. {
  702. struct revmap_entry *rev = kvm->arch.revmap;
  703. unsigned long head, i, j;
  704. unsigned long *hptep;
  705. int ret = 0;
  706. retry:
  707. lock_rmap(rmapp);
  708. if (*rmapp & KVMPPC_RMAP_REFERENCED) {
  709. *rmapp &= ~KVMPPC_RMAP_REFERENCED;
  710. ret = 1;
  711. }
  712. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  713. unlock_rmap(rmapp);
  714. return ret;
  715. }
  716. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  717. do {
  718. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  719. j = rev[i].forw;
  720. /* If this HPTE isn't referenced, ignore it */
  721. if (!(hptep[1] & HPTE_R_R))
  722. continue;
  723. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  724. /* unlock rmap before spinning on the HPTE lock */
  725. unlock_rmap(rmapp);
  726. while (hptep[0] & HPTE_V_HVLOCK)
  727. cpu_relax();
  728. goto retry;
  729. }
  730. /* Now check and modify the HPTE */
  731. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
  732. kvmppc_clear_ref_hpte(kvm, hptep, i);
  733. rev[i].guest_rpte |= HPTE_R_R;
  734. ret = 1;
  735. }
  736. hptep[0] &= ~HPTE_V_HVLOCK;
  737. } while ((i = j) != head);
  738. unlock_rmap(rmapp);
  739. return ret;
  740. }
  741. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  742. {
  743. if (!kvm->arch.using_mmu_notifiers)
  744. return 0;
  745. return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
  746. }
  747. static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  748. unsigned long gfn)
  749. {
  750. struct revmap_entry *rev = kvm->arch.revmap;
  751. unsigned long head, i, j;
  752. unsigned long *hp;
  753. int ret = 1;
  754. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  755. return 1;
  756. lock_rmap(rmapp);
  757. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  758. goto out;
  759. if (*rmapp & KVMPPC_RMAP_PRESENT) {
  760. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  761. do {
  762. hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
  763. j = rev[i].forw;
  764. if (hp[1] & HPTE_R_R)
  765. goto out;
  766. } while ((i = j) != head);
  767. }
  768. ret = 0;
  769. out:
  770. unlock_rmap(rmapp);
  771. return ret;
  772. }
  773. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  774. {
  775. if (!kvm->arch.using_mmu_notifiers)
  776. return 0;
  777. return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
  778. }
  779. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  780. {
  781. if (!kvm->arch.using_mmu_notifiers)
  782. return;
  783. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  784. }
  785. static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp)
  786. {
  787. struct revmap_entry *rev = kvm->arch.revmap;
  788. unsigned long head, i, j;
  789. unsigned long *hptep;
  790. int ret = 0;
  791. retry:
  792. lock_rmap(rmapp);
  793. if (*rmapp & KVMPPC_RMAP_CHANGED) {
  794. *rmapp &= ~KVMPPC_RMAP_CHANGED;
  795. ret = 1;
  796. }
  797. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  798. unlock_rmap(rmapp);
  799. return ret;
  800. }
  801. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  802. do {
  803. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  804. j = rev[i].forw;
  805. if (!(hptep[1] & HPTE_R_C))
  806. continue;
  807. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  808. /* unlock rmap before spinning on the HPTE lock */
  809. unlock_rmap(rmapp);
  810. while (hptep[0] & HPTE_V_HVLOCK)
  811. cpu_relax();
  812. goto retry;
  813. }
  814. /* Now check and modify the HPTE */
  815. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
  816. /* need to make it temporarily absent to clear C */
  817. hptep[0] |= HPTE_V_ABSENT;
  818. kvmppc_invalidate_hpte(kvm, hptep, i);
  819. hptep[1] &= ~HPTE_R_C;
  820. eieio();
  821. hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  822. rev[i].guest_rpte |= HPTE_R_C;
  823. ret = 1;
  824. }
  825. hptep[0] &= ~HPTE_V_HVLOCK;
  826. } while ((i = j) != head);
  827. unlock_rmap(rmapp);
  828. return ret;
  829. }
  830. long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  831. {
  832. unsigned long i;
  833. unsigned long *rmapp, *map;
  834. preempt_disable();
  835. rmapp = memslot->rmap;
  836. map = memslot->dirty_bitmap;
  837. for (i = 0; i < memslot->npages; ++i) {
  838. if (kvm_test_clear_dirty(kvm, rmapp))
  839. __set_bit_le(i, map);
  840. ++rmapp;
  841. }
  842. preempt_enable();
  843. return 0;
  844. }
  845. void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
  846. unsigned long *nb_ret)
  847. {
  848. struct kvm_memory_slot *memslot;
  849. unsigned long gfn = gpa >> PAGE_SHIFT;
  850. struct page *page, *pages[1];
  851. int npages;
  852. unsigned long hva, psize, offset;
  853. unsigned long pa;
  854. unsigned long *physp;
  855. memslot = gfn_to_memslot(kvm, gfn);
  856. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  857. return NULL;
  858. if (!kvm->arch.using_mmu_notifiers) {
  859. physp = kvm->arch.slot_phys[memslot->id];
  860. if (!physp)
  861. return NULL;
  862. physp += gfn - memslot->base_gfn;
  863. pa = *physp;
  864. if (!pa) {
  865. if (kvmppc_get_guest_page(kvm, gfn, memslot,
  866. PAGE_SIZE) < 0)
  867. return NULL;
  868. pa = *physp;
  869. }
  870. page = pfn_to_page(pa >> PAGE_SHIFT);
  871. get_page(page);
  872. } else {
  873. hva = gfn_to_hva_memslot(memslot, gfn);
  874. npages = get_user_pages_fast(hva, 1, 1, pages);
  875. if (npages < 1)
  876. return NULL;
  877. page = pages[0];
  878. }
  879. psize = PAGE_SIZE;
  880. if (PageHuge(page)) {
  881. page = compound_head(page);
  882. psize <<= compound_order(page);
  883. }
  884. offset = gpa & (psize - 1);
  885. if (nb_ret)
  886. *nb_ret = psize - offset;
  887. return page_address(page) + offset;
  888. }
  889. void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
  890. {
  891. struct page *page = virt_to_page(va);
  892. put_page(page);
  893. }
  894. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  895. {
  896. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  897. if (cpu_has_feature(CPU_FTR_ARCH_206))
  898. vcpu->arch.slb_nr = 32; /* POWER7 */
  899. else
  900. vcpu->arch.slb_nr = 64;
  901. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  902. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  903. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  904. }