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