1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corporation, 2018 4 * Authors Suraj Jitindar Singh <sjitindarsingh@gmail.com> 5 * Paul Mackerras <paulus@ozlabs.org> 6 * 7 * Description: KVM functions specific to running nested KVM-HV guests 8 * on Book3S processors (specifically POWER9 and later). 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/kvm_host.h> 13 #include <linux/llist.h> 14 #include <linux/pgtable.h> 15 16 #include <asm/kvm_ppc.h> 17 #include <asm/kvm_book3s.h> 18 #include <asm/mmu.h> 19 #include <asm/pgalloc.h> 20 #include <asm/pte-walk.h> 21 #include <asm/reg.h> 22 23 static struct patb_entry *pseries_partition_tb; 24 25 static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp); 26 static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free); 27 28 void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr) 29 { 30 struct kvmppc_vcore *vc = vcpu->arch.vcore; 31 32 hr->pcr = vc->pcr | PCR_MASK; 33 hr->dpdes = vc->dpdes; 34 hr->hfscr = vcpu->arch.hfscr; 35 hr->tb_offset = vc->tb_offset; 36 hr->dawr0 = vcpu->arch.dawr0; 37 hr->dawrx0 = vcpu->arch.dawrx0; 38 hr->ciabr = vcpu->arch.ciabr; 39 hr->purr = vcpu->arch.purr; 40 hr->spurr = vcpu->arch.spurr; 41 hr->ic = vcpu->arch.ic; 42 hr->vtb = vc->vtb; 43 hr->srr0 = vcpu->arch.shregs.srr0; 44 hr->srr1 = vcpu->arch.shregs.srr1; 45 hr->sprg[0] = vcpu->arch.shregs.sprg0; 46 hr->sprg[1] = vcpu->arch.shregs.sprg1; 47 hr->sprg[2] = vcpu->arch.shregs.sprg2; 48 hr->sprg[3] = vcpu->arch.shregs.sprg3; 49 hr->pidr = vcpu->arch.pid; 50 hr->cfar = vcpu->arch.cfar; 51 hr->ppr = vcpu->arch.ppr; 52 hr->dawr1 = vcpu->arch.dawr1; 53 hr->dawrx1 = vcpu->arch.dawrx1; 54 } 55 56 static void byteswap_pt_regs(struct pt_regs *regs) 57 { 58 unsigned long *addr = (unsigned long *) regs; 59 60 for (; addr < ((unsigned long *) (regs + 1)); addr++) 61 *addr = swab64(*addr); 62 } 63 64 static void byteswap_hv_regs(struct hv_guest_state *hr) 65 { 66 hr->version = swab64(hr->version); 67 hr->lpid = swab32(hr->lpid); 68 hr->vcpu_token = swab32(hr->vcpu_token); 69 hr->lpcr = swab64(hr->lpcr); 70 hr->pcr = swab64(hr->pcr) | PCR_MASK; 71 hr->amor = swab64(hr->amor); 72 hr->dpdes = swab64(hr->dpdes); 73 hr->hfscr = swab64(hr->hfscr); 74 hr->tb_offset = swab64(hr->tb_offset); 75 hr->dawr0 = swab64(hr->dawr0); 76 hr->dawrx0 = swab64(hr->dawrx0); 77 hr->ciabr = swab64(hr->ciabr); 78 hr->hdec_expiry = swab64(hr->hdec_expiry); 79 hr->purr = swab64(hr->purr); 80 hr->spurr = swab64(hr->spurr); 81 hr->ic = swab64(hr->ic); 82 hr->vtb = swab64(hr->vtb); 83 hr->hdar = swab64(hr->hdar); 84 hr->hdsisr = swab64(hr->hdsisr); 85 hr->heir = swab64(hr->heir); 86 hr->asdr = swab64(hr->asdr); 87 hr->srr0 = swab64(hr->srr0); 88 hr->srr1 = swab64(hr->srr1); 89 hr->sprg[0] = swab64(hr->sprg[0]); 90 hr->sprg[1] = swab64(hr->sprg[1]); 91 hr->sprg[2] = swab64(hr->sprg[2]); 92 hr->sprg[3] = swab64(hr->sprg[3]); 93 hr->pidr = swab64(hr->pidr); 94 hr->cfar = swab64(hr->cfar); 95 hr->ppr = swab64(hr->ppr); 96 hr->dawr1 = swab64(hr->dawr1); 97 hr->dawrx1 = swab64(hr->dawrx1); 98 } 99 100 static void save_hv_return_state(struct kvm_vcpu *vcpu, int trap, 101 struct hv_guest_state *hr) 102 { 103 struct kvmppc_vcore *vc = vcpu->arch.vcore; 104 105 hr->dpdes = vc->dpdes; 106 hr->hfscr = vcpu->arch.hfscr; 107 hr->purr = vcpu->arch.purr; 108 hr->spurr = vcpu->arch.spurr; 109 hr->ic = vcpu->arch.ic; 110 hr->vtb = vc->vtb; 111 hr->srr0 = vcpu->arch.shregs.srr0; 112 hr->srr1 = vcpu->arch.shregs.srr1; 113 hr->sprg[0] = vcpu->arch.shregs.sprg0; 114 hr->sprg[1] = vcpu->arch.shregs.sprg1; 115 hr->sprg[2] = vcpu->arch.shregs.sprg2; 116 hr->sprg[3] = vcpu->arch.shregs.sprg3; 117 hr->pidr = vcpu->arch.pid; 118 hr->cfar = vcpu->arch.cfar; 119 hr->ppr = vcpu->arch.ppr; 120 switch (trap) { 121 case BOOK3S_INTERRUPT_H_DATA_STORAGE: 122 hr->hdar = vcpu->arch.fault_dar; 123 hr->hdsisr = vcpu->arch.fault_dsisr; 124 hr->asdr = vcpu->arch.fault_gpa; 125 break; 126 case BOOK3S_INTERRUPT_H_INST_STORAGE: 127 hr->asdr = vcpu->arch.fault_gpa; 128 break; 129 case BOOK3S_INTERRUPT_H_EMUL_ASSIST: 130 hr->heir = vcpu->arch.emul_inst; 131 break; 132 } 133 } 134 135 static void sanitise_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr) 136 { 137 /* 138 * Don't let L1 enable features for L2 which we've disabled for L1, 139 * but preserve the interrupt cause field. 140 */ 141 hr->hfscr &= (HFSCR_INTR_CAUSE | vcpu->arch.hfscr); 142 143 /* Don't let data address watchpoint match in hypervisor state */ 144 hr->dawrx0 &= ~DAWRX_HYP; 145 hr->dawrx1 &= ~DAWRX_HYP; 146 147 /* Don't let completed instruction address breakpt match in HV state */ 148 if ((hr->ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER) 149 hr->ciabr &= ~CIABR_PRIV; 150 } 151 152 static void restore_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr) 153 { 154 struct kvmppc_vcore *vc = vcpu->arch.vcore; 155 156 vc->pcr = hr->pcr | PCR_MASK; 157 vc->dpdes = hr->dpdes; 158 vcpu->arch.hfscr = hr->hfscr; 159 vcpu->arch.dawr0 = hr->dawr0; 160 vcpu->arch.dawrx0 = hr->dawrx0; 161 vcpu->arch.ciabr = hr->ciabr; 162 vcpu->arch.purr = hr->purr; 163 vcpu->arch.spurr = hr->spurr; 164 vcpu->arch.ic = hr->ic; 165 vc->vtb = hr->vtb; 166 vcpu->arch.shregs.srr0 = hr->srr0; 167 vcpu->arch.shregs.srr1 = hr->srr1; 168 vcpu->arch.shregs.sprg0 = hr->sprg[0]; 169 vcpu->arch.shregs.sprg1 = hr->sprg[1]; 170 vcpu->arch.shregs.sprg2 = hr->sprg[2]; 171 vcpu->arch.shregs.sprg3 = hr->sprg[3]; 172 vcpu->arch.pid = hr->pidr; 173 vcpu->arch.cfar = hr->cfar; 174 vcpu->arch.ppr = hr->ppr; 175 vcpu->arch.dawr1 = hr->dawr1; 176 vcpu->arch.dawrx1 = hr->dawrx1; 177 } 178 179 void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu, 180 struct hv_guest_state *hr) 181 { 182 struct kvmppc_vcore *vc = vcpu->arch.vcore; 183 184 vc->dpdes = hr->dpdes; 185 vcpu->arch.hfscr = hr->hfscr; 186 vcpu->arch.purr = hr->purr; 187 vcpu->arch.spurr = hr->spurr; 188 vcpu->arch.ic = hr->ic; 189 vc->vtb = hr->vtb; 190 vcpu->arch.fault_dar = hr->hdar; 191 vcpu->arch.fault_dsisr = hr->hdsisr; 192 vcpu->arch.fault_gpa = hr->asdr; 193 vcpu->arch.emul_inst = hr->heir; 194 vcpu->arch.shregs.srr0 = hr->srr0; 195 vcpu->arch.shregs.srr1 = hr->srr1; 196 vcpu->arch.shregs.sprg0 = hr->sprg[0]; 197 vcpu->arch.shregs.sprg1 = hr->sprg[1]; 198 vcpu->arch.shregs.sprg2 = hr->sprg[2]; 199 vcpu->arch.shregs.sprg3 = hr->sprg[3]; 200 vcpu->arch.pid = hr->pidr; 201 vcpu->arch.cfar = hr->cfar; 202 vcpu->arch.ppr = hr->ppr; 203 } 204 205 static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr) 206 { 207 /* No need to reflect the page fault to L1, we've handled it */ 208 vcpu->arch.trap = 0; 209 210 /* 211 * Since the L2 gprs have already been written back into L1 memory when 212 * we complete the mmio, store the L1 memory location of the L2 gpr 213 * being loaded into by the mmio so that the loaded value can be 214 * written there in kvmppc_complete_mmio_load() 215 */ 216 if (((vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) == KVM_MMIO_REG_GPR) 217 && (vcpu->mmio_is_write == 0)) { 218 vcpu->arch.nested_io_gpr = (gpa_t) regs_ptr + 219 offsetof(struct pt_regs, 220 gpr[vcpu->arch.io_gpr]); 221 vcpu->arch.io_gpr = KVM_MMIO_REG_NESTED_GPR; 222 } 223 } 224 225 static int kvmhv_read_guest_state_and_regs(struct kvm_vcpu *vcpu, 226 struct hv_guest_state *l2_hv, 227 struct pt_regs *l2_regs, 228 u64 hv_ptr, u64 regs_ptr) 229 { 230 int size; 231 232 if (kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv->version, 233 sizeof(l2_hv->version))) 234 return -1; 235 236 if (kvmppc_need_byteswap(vcpu)) 237 l2_hv->version = swab64(l2_hv->version); 238 239 size = hv_guest_state_size(l2_hv->version); 240 if (size < 0) 241 return -1; 242 243 return kvm_vcpu_read_guest(vcpu, hv_ptr, l2_hv, size) || 244 kvm_vcpu_read_guest(vcpu, regs_ptr, l2_regs, 245 sizeof(struct pt_regs)); 246 } 247 248 static int kvmhv_write_guest_state_and_regs(struct kvm_vcpu *vcpu, 249 struct hv_guest_state *l2_hv, 250 struct pt_regs *l2_regs, 251 u64 hv_ptr, u64 regs_ptr) 252 { 253 int size; 254 255 size = hv_guest_state_size(l2_hv->version); 256 if (size < 0) 257 return -1; 258 259 return kvm_vcpu_write_guest(vcpu, hv_ptr, l2_hv, size) || 260 kvm_vcpu_write_guest(vcpu, regs_ptr, l2_regs, 261 sizeof(struct pt_regs)); 262 } 263 264 long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) 265 { 266 long int err, r; 267 struct kvm_nested_guest *l2; 268 struct pt_regs l2_regs, saved_l1_regs; 269 struct hv_guest_state l2_hv = {0}, saved_l1_hv; 270 struct kvmppc_vcore *vc = vcpu->arch.vcore; 271 u64 hv_ptr, regs_ptr; 272 u64 hdec_exp; 273 s64 delta_purr, delta_spurr, delta_ic, delta_vtb; 274 u64 mask; 275 unsigned long lpcr; 276 277 if (vcpu->kvm->arch.l1_ptcr == 0) 278 return H_NOT_AVAILABLE; 279 280 /* copy parameters in */ 281 hv_ptr = kvmppc_get_gpr(vcpu, 4); 282 regs_ptr = kvmppc_get_gpr(vcpu, 5); 283 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 284 err = kvmhv_read_guest_state_and_regs(vcpu, &l2_hv, &l2_regs, 285 hv_ptr, regs_ptr); 286 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 287 if (err) 288 return H_PARAMETER; 289 290 if (kvmppc_need_byteswap(vcpu)) 291 byteswap_hv_regs(&l2_hv); 292 if (l2_hv.version > HV_GUEST_STATE_VERSION) 293 return H_P2; 294 295 if (kvmppc_need_byteswap(vcpu)) 296 byteswap_pt_regs(&l2_regs); 297 if (l2_hv.vcpu_token >= NR_CPUS) 298 return H_PARAMETER; 299 300 /* translate lpid */ 301 l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true); 302 if (!l2) 303 return H_PARAMETER; 304 if (!l2->l1_gr_to_hr) { 305 mutex_lock(&l2->tlb_lock); 306 kvmhv_update_ptbl_cache(l2); 307 mutex_unlock(&l2->tlb_lock); 308 } 309 310 /* save l1 values of things */ 311 vcpu->arch.regs.msr = vcpu->arch.shregs.msr; 312 saved_l1_regs = vcpu->arch.regs; 313 kvmhv_save_hv_regs(vcpu, &saved_l1_hv); 314 315 /* convert TB values/offsets to host (L0) values */ 316 hdec_exp = l2_hv.hdec_expiry - vc->tb_offset; 317 vc->tb_offset += l2_hv.tb_offset; 318 319 /* set L1 state to L2 state */ 320 vcpu->arch.nested = l2; 321 vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token; 322 vcpu->arch.regs = l2_regs; 323 vcpu->arch.shregs.msr = vcpu->arch.regs.msr; 324 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD | 325 LPCR_LPES | LPCR_MER; 326 lpcr = (vc->lpcr & ~mask) | (l2_hv.lpcr & mask); 327 sanitise_hv_regs(vcpu, &l2_hv); 328 restore_hv_regs(vcpu, &l2_hv); 329 330 vcpu->arch.ret = RESUME_GUEST; 331 vcpu->arch.trap = 0; 332 do { 333 if (mftb() >= hdec_exp) { 334 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER; 335 r = RESUME_HOST; 336 break; 337 } 338 r = kvmhv_run_single_vcpu(vcpu, hdec_exp, lpcr); 339 } while (is_kvmppc_resume_guest(r)); 340 341 /* save L2 state for return */ 342 l2_regs = vcpu->arch.regs; 343 l2_regs.msr = vcpu->arch.shregs.msr; 344 delta_purr = vcpu->arch.purr - l2_hv.purr; 345 delta_spurr = vcpu->arch.spurr - l2_hv.spurr; 346 delta_ic = vcpu->arch.ic - l2_hv.ic; 347 delta_vtb = vc->vtb - l2_hv.vtb; 348 save_hv_return_state(vcpu, vcpu->arch.trap, &l2_hv); 349 350 /* restore L1 state */ 351 vcpu->arch.nested = NULL; 352 vcpu->arch.regs = saved_l1_regs; 353 vcpu->arch.shregs.msr = saved_l1_regs.msr & ~MSR_TS_MASK; 354 /* set L1 MSR TS field according to L2 transaction state */ 355 if (l2_regs.msr & MSR_TS_MASK) 356 vcpu->arch.shregs.msr |= MSR_TS_S; 357 vc->tb_offset = saved_l1_hv.tb_offset; 358 restore_hv_regs(vcpu, &saved_l1_hv); 359 vcpu->arch.purr += delta_purr; 360 vcpu->arch.spurr += delta_spurr; 361 vcpu->arch.ic += delta_ic; 362 vc->vtb += delta_vtb; 363 364 kvmhv_put_nested(l2); 365 366 /* copy l2_hv_state and regs back to guest */ 367 if (kvmppc_need_byteswap(vcpu)) { 368 byteswap_hv_regs(&l2_hv); 369 byteswap_pt_regs(&l2_regs); 370 } 371 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 372 err = kvmhv_write_guest_state_and_regs(vcpu, &l2_hv, &l2_regs, 373 hv_ptr, regs_ptr); 374 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 375 if (err) 376 return H_AUTHORITY; 377 378 if (r == -EINTR) 379 return H_INTERRUPT; 380 381 if (vcpu->mmio_needed) { 382 kvmhv_nested_mmio_needed(vcpu, regs_ptr); 383 return H_TOO_HARD; 384 } 385 386 return vcpu->arch.trap; 387 } 388 389 long kvmhv_nested_init(void) 390 { 391 long int ptb_order; 392 unsigned long ptcr; 393 long rc; 394 395 if (!kvmhv_on_pseries()) 396 return 0; 397 if (!radix_enabled()) 398 return -ENODEV; 399 400 /* find log base 2 of KVMPPC_NR_LPIDS, rounding up */ 401 ptb_order = __ilog2(KVMPPC_NR_LPIDS - 1) + 1; 402 if (ptb_order < 8) 403 ptb_order = 8; 404 pseries_partition_tb = kmalloc(sizeof(struct patb_entry) << ptb_order, 405 GFP_KERNEL); 406 if (!pseries_partition_tb) { 407 pr_err("kvm-hv: failed to allocated nested partition table\n"); 408 return -ENOMEM; 409 } 410 411 ptcr = __pa(pseries_partition_tb) | (ptb_order - 8); 412 rc = plpar_hcall_norets(H_SET_PARTITION_TABLE, ptcr); 413 if (rc != H_SUCCESS) { 414 pr_err("kvm-hv: Parent hypervisor does not support nesting (rc=%ld)\n", 415 rc); 416 kfree(pseries_partition_tb); 417 pseries_partition_tb = NULL; 418 return -ENODEV; 419 } 420 421 return 0; 422 } 423 424 void kvmhv_nested_exit(void) 425 { 426 /* 427 * N.B. the kvmhv_on_pseries() test is there because it enables 428 * the compiler to remove the call to plpar_hcall_norets() 429 * when CONFIG_PPC_PSERIES=n. 430 */ 431 if (kvmhv_on_pseries() && pseries_partition_tb) { 432 plpar_hcall_norets(H_SET_PARTITION_TABLE, 0); 433 kfree(pseries_partition_tb); 434 pseries_partition_tb = NULL; 435 } 436 } 437 438 static void kvmhv_flush_lpid(unsigned int lpid) 439 { 440 long rc; 441 442 if (!kvmhv_on_pseries()) { 443 radix__flush_all_lpid(lpid); 444 return; 445 } 446 447 rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(2, 0, 1), 448 lpid, TLBIEL_INVAL_SET_LPID); 449 if (rc) 450 pr_err("KVM: TLB LPID invalidation hcall failed, rc=%ld\n", rc); 451 } 452 453 void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1) 454 { 455 if (!kvmhv_on_pseries()) { 456 mmu_partition_table_set_entry(lpid, dw0, dw1, true); 457 return; 458 } 459 460 pseries_partition_tb[lpid].patb0 = cpu_to_be64(dw0); 461 pseries_partition_tb[lpid].patb1 = cpu_to_be64(dw1); 462 /* L0 will do the necessary barriers */ 463 kvmhv_flush_lpid(lpid); 464 } 465 466 static void kvmhv_set_nested_ptbl(struct kvm_nested_guest *gp) 467 { 468 unsigned long dw0; 469 470 dw0 = PATB_HR | radix__get_tree_size() | 471 __pa(gp->shadow_pgtable) | RADIX_PGD_INDEX_SIZE; 472 kvmhv_set_ptbl_entry(gp->shadow_lpid, dw0, gp->process_table); 473 } 474 475 void kvmhv_vm_nested_init(struct kvm *kvm) 476 { 477 kvm->arch.max_nested_lpid = -1; 478 } 479 480 /* 481 * Handle the H_SET_PARTITION_TABLE hcall. 482 * r4 = guest real address of partition table + log_2(size) - 12 483 * (formatted as for the PTCR). 484 */ 485 long kvmhv_set_partition_table(struct kvm_vcpu *vcpu) 486 { 487 struct kvm *kvm = vcpu->kvm; 488 unsigned long ptcr = kvmppc_get_gpr(vcpu, 4); 489 int srcu_idx; 490 long ret = H_SUCCESS; 491 492 srcu_idx = srcu_read_lock(&kvm->srcu); 493 /* 494 * Limit the partition table to 4096 entries (because that's what 495 * hardware supports), and check the base address. 496 */ 497 if ((ptcr & PRTS_MASK) > 12 - 8 || 498 !kvm_is_visible_gfn(vcpu->kvm, (ptcr & PRTB_MASK) >> PAGE_SHIFT)) 499 ret = H_PARAMETER; 500 srcu_read_unlock(&kvm->srcu, srcu_idx); 501 if (ret == H_SUCCESS) 502 kvm->arch.l1_ptcr = ptcr; 503 return ret; 504 } 505 506 /* 507 * Handle the H_COPY_TOFROM_GUEST hcall. 508 * r4 = L1 lpid of nested guest 509 * r5 = pid 510 * r6 = eaddr to access 511 * r7 = to buffer (L1 gpa) 512 * r8 = from buffer (L1 gpa) 513 * r9 = n bytes to copy 514 */ 515 long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu) 516 { 517 struct kvm_nested_guest *gp; 518 int l1_lpid = kvmppc_get_gpr(vcpu, 4); 519 int pid = kvmppc_get_gpr(vcpu, 5); 520 gva_t eaddr = kvmppc_get_gpr(vcpu, 6); 521 gpa_t gp_to = (gpa_t) kvmppc_get_gpr(vcpu, 7); 522 gpa_t gp_from = (gpa_t) kvmppc_get_gpr(vcpu, 8); 523 void *buf; 524 unsigned long n = kvmppc_get_gpr(vcpu, 9); 525 bool is_load = !!gp_to; 526 long rc; 527 528 if (gp_to && gp_from) /* One must be NULL to determine the direction */ 529 return H_PARAMETER; 530 531 if (eaddr & (0xFFFUL << 52)) 532 return H_PARAMETER; 533 534 buf = kzalloc(n, GFP_KERNEL); 535 if (!buf) 536 return H_NO_MEM; 537 538 gp = kvmhv_get_nested(vcpu->kvm, l1_lpid, false); 539 if (!gp) { 540 rc = H_PARAMETER; 541 goto out_free; 542 } 543 544 mutex_lock(&gp->tlb_lock); 545 546 if (is_load) { 547 /* Load from the nested guest into our buffer */ 548 rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid, 549 eaddr, buf, NULL, n); 550 if (rc) 551 goto not_found; 552 553 /* Write what was loaded into our buffer back to the L1 guest */ 554 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 555 rc = kvm_vcpu_write_guest(vcpu, gp_to, buf, n); 556 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 557 if (rc) 558 goto not_found; 559 } else { 560 /* Load the data to be stored from the L1 guest into our buf */ 561 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 562 rc = kvm_vcpu_read_guest(vcpu, gp_from, buf, n); 563 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 564 if (rc) 565 goto not_found; 566 567 /* Store from our buffer into the nested guest */ 568 rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid, 569 eaddr, NULL, buf, n); 570 if (rc) 571 goto not_found; 572 } 573 574 out_unlock: 575 mutex_unlock(&gp->tlb_lock); 576 kvmhv_put_nested(gp); 577 out_free: 578 kfree(buf); 579 return rc; 580 not_found: 581 rc = H_NOT_FOUND; 582 goto out_unlock; 583 } 584 585 /* 586 * Reload the partition table entry for a guest. 587 * Caller must hold gp->tlb_lock. 588 */ 589 static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp) 590 { 591 int ret; 592 struct patb_entry ptbl_entry; 593 unsigned long ptbl_addr; 594 struct kvm *kvm = gp->l1_host; 595 596 ret = -EFAULT; 597 ptbl_addr = (kvm->arch.l1_ptcr & PRTB_MASK) + (gp->l1_lpid << 4); 598 if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 8))) { 599 int srcu_idx = srcu_read_lock(&kvm->srcu); 600 ret = kvm_read_guest(kvm, ptbl_addr, 601 &ptbl_entry, sizeof(ptbl_entry)); 602 srcu_read_unlock(&kvm->srcu, srcu_idx); 603 } 604 if (ret) { 605 gp->l1_gr_to_hr = 0; 606 gp->process_table = 0; 607 } else { 608 gp->l1_gr_to_hr = be64_to_cpu(ptbl_entry.patb0); 609 gp->process_table = be64_to_cpu(ptbl_entry.patb1); 610 } 611 kvmhv_set_nested_ptbl(gp); 612 } 613 614 static struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid) 615 { 616 struct kvm_nested_guest *gp; 617 long shadow_lpid; 618 619 gp = kzalloc(sizeof(*gp), GFP_KERNEL); 620 if (!gp) 621 return NULL; 622 gp->l1_host = kvm; 623 gp->l1_lpid = lpid; 624 mutex_init(&gp->tlb_lock); 625 gp->shadow_pgtable = pgd_alloc(kvm->mm); 626 if (!gp->shadow_pgtable) 627 goto out_free; 628 shadow_lpid = kvmppc_alloc_lpid(); 629 if (shadow_lpid < 0) 630 goto out_free2; 631 gp->shadow_lpid = shadow_lpid; 632 gp->radix = 1; 633 634 memset(gp->prev_cpu, -1, sizeof(gp->prev_cpu)); 635 636 return gp; 637 638 out_free2: 639 pgd_free(kvm->mm, gp->shadow_pgtable); 640 out_free: 641 kfree(gp); 642 return NULL; 643 } 644 645 /* 646 * Free up any resources allocated for a nested guest. 647 */ 648 static void kvmhv_release_nested(struct kvm_nested_guest *gp) 649 { 650 struct kvm *kvm = gp->l1_host; 651 652 if (gp->shadow_pgtable) { 653 /* 654 * No vcpu is using this struct and no call to 655 * kvmhv_get_nested can find this struct, 656 * so we don't need to hold kvm->mmu_lock. 657 */ 658 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, 659 gp->shadow_lpid); 660 pgd_free(kvm->mm, gp->shadow_pgtable); 661 } 662 kvmhv_set_ptbl_entry(gp->shadow_lpid, 0, 0); 663 kvmppc_free_lpid(gp->shadow_lpid); 664 kfree(gp); 665 } 666 667 static void kvmhv_remove_nested(struct kvm_nested_guest *gp) 668 { 669 struct kvm *kvm = gp->l1_host; 670 int lpid = gp->l1_lpid; 671 long ref; 672 673 spin_lock(&kvm->mmu_lock); 674 if (gp == kvm->arch.nested_guests[lpid]) { 675 kvm->arch.nested_guests[lpid] = NULL; 676 if (lpid == kvm->arch.max_nested_lpid) { 677 while (--lpid >= 0 && !kvm->arch.nested_guests[lpid]) 678 ; 679 kvm->arch.max_nested_lpid = lpid; 680 } 681 --gp->refcnt; 682 } 683 ref = gp->refcnt; 684 spin_unlock(&kvm->mmu_lock); 685 if (ref == 0) 686 kvmhv_release_nested(gp); 687 } 688 689 /* 690 * Free up all nested resources allocated for this guest. 691 * This is called with no vcpus of the guest running, when 692 * switching the guest to HPT mode or when destroying the 693 * guest. 694 */ 695 void kvmhv_release_all_nested(struct kvm *kvm) 696 { 697 int i; 698 struct kvm_nested_guest *gp; 699 struct kvm_nested_guest *freelist = NULL; 700 struct kvm_memory_slot *memslot; 701 int srcu_idx; 702 703 spin_lock(&kvm->mmu_lock); 704 for (i = 0; i <= kvm->arch.max_nested_lpid; i++) { 705 gp = kvm->arch.nested_guests[i]; 706 if (!gp) 707 continue; 708 kvm->arch.nested_guests[i] = NULL; 709 if (--gp->refcnt == 0) { 710 gp->next = freelist; 711 freelist = gp; 712 } 713 } 714 kvm->arch.max_nested_lpid = -1; 715 spin_unlock(&kvm->mmu_lock); 716 while ((gp = freelist) != NULL) { 717 freelist = gp->next; 718 kvmhv_release_nested(gp); 719 } 720 721 srcu_idx = srcu_read_lock(&kvm->srcu); 722 kvm_for_each_memslot(memslot, kvm_memslots(kvm)) 723 kvmhv_free_memslot_nest_rmap(memslot); 724 srcu_read_unlock(&kvm->srcu, srcu_idx); 725 } 726 727 /* caller must hold gp->tlb_lock */ 728 static void kvmhv_flush_nested(struct kvm_nested_guest *gp) 729 { 730 struct kvm *kvm = gp->l1_host; 731 732 spin_lock(&kvm->mmu_lock); 733 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, gp->shadow_lpid); 734 spin_unlock(&kvm->mmu_lock); 735 kvmhv_flush_lpid(gp->shadow_lpid); 736 kvmhv_update_ptbl_cache(gp); 737 if (gp->l1_gr_to_hr == 0) 738 kvmhv_remove_nested(gp); 739 } 740 741 struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid, 742 bool create) 743 { 744 struct kvm_nested_guest *gp, *newgp; 745 746 if (l1_lpid >= KVM_MAX_NESTED_GUESTS || 747 l1_lpid >= (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4))) 748 return NULL; 749 750 spin_lock(&kvm->mmu_lock); 751 gp = kvm->arch.nested_guests[l1_lpid]; 752 if (gp) 753 ++gp->refcnt; 754 spin_unlock(&kvm->mmu_lock); 755 756 if (gp || !create) 757 return gp; 758 759 newgp = kvmhv_alloc_nested(kvm, l1_lpid); 760 if (!newgp) 761 return NULL; 762 spin_lock(&kvm->mmu_lock); 763 if (kvm->arch.nested_guests[l1_lpid]) { 764 /* someone else beat us to it */ 765 gp = kvm->arch.nested_guests[l1_lpid]; 766 } else { 767 kvm->arch.nested_guests[l1_lpid] = newgp; 768 ++newgp->refcnt; 769 gp = newgp; 770 newgp = NULL; 771 if (l1_lpid > kvm->arch.max_nested_lpid) 772 kvm->arch.max_nested_lpid = l1_lpid; 773 } 774 ++gp->refcnt; 775 spin_unlock(&kvm->mmu_lock); 776 777 if (newgp) 778 kvmhv_release_nested(newgp); 779 780 return gp; 781 } 782 783 void kvmhv_put_nested(struct kvm_nested_guest *gp) 784 { 785 struct kvm *kvm = gp->l1_host; 786 long ref; 787 788 spin_lock(&kvm->mmu_lock); 789 ref = --gp->refcnt; 790 spin_unlock(&kvm->mmu_lock); 791 if (ref == 0) 792 kvmhv_release_nested(gp); 793 } 794 795 static struct kvm_nested_guest *kvmhv_find_nested(struct kvm *kvm, int lpid) 796 { 797 if (lpid > kvm->arch.max_nested_lpid) 798 return NULL; 799 return kvm->arch.nested_guests[lpid]; 800 } 801 802 pte_t *find_kvm_nested_guest_pte(struct kvm *kvm, unsigned long lpid, 803 unsigned long ea, unsigned *hshift) 804 { 805 struct kvm_nested_guest *gp; 806 pte_t *pte; 807 808 gp = kvmhv_find_nested(kvm, lpid); 809 if (!gp) 810 return NULL; 811 812 VM_WARN(!spin_is_locked(&kvm->mmu_lock), 813 "%s called with kvm mmu_lock not held \n", __func__); 814 pte = __find_linux_pte(gp->shadow_pgtable, ea, NULL, hshift); 815 816 return pte; 817 } 818 819 static inline bool kvmhv_n_rmap_is_equal(u64 rmap_1, u64 rmap_2) 820 { 821 return !((rmap_1 ^ rmap_2) & (RMAP_NESTED_LPID_MASK | 822 RMAP_NESTED_GPA_MASK)); 823 } 824 825 void kvmhv_insert_nest_rmap(struct kvm *kvm, unsigned long *rmapp, 826 struct rmap_nested **n_rmap) 827 { 828 struct llist_node *entry = ((struct llist_head *) rmapp)->first; 829 struct rmap_nested *cursor; 830 u64 rmap, new_rmap = (*n_rmap)->rmap; 831 832 /* Are there any existing entries? */ 833 if (!(*rmapp)) { 834 /* No -> use the rmap as a single entry */ 835 *rmapp = new_rmap | RMAP_NESTED_IS_SINGLE_ENTRY; 836 return; 837 } 838 839 /* Do any entries match what we're trying to insert? */ 840 for_each_nest_rmap_safe(cursor, entry, &rmap) { 841 if (kvmhv_n_rmap_is_equal(rmap, new_rmap)) 842 return; 843 } 844 845 /* Do we need to create a list or just add the new entry? */ 846 rmap = *rmapp; 847 if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */ 848 *rmapp = 0UL; 849 llist_add(&((*n_rmap)->list), (struct llist_head *) rmapp); 850 if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */ 851 (*n_rmap)->list.next = (struct llist_node *) rmap; 852 853 /* Set NULL so not freed by caller */ 854 *n_rmap = NULL; 855 } 856 857 static void kvmhv_update_nest_rmap_rc(struct kvm *kvm, u64 n_rmap, 858 unsigned long clr, unsigned long set, 859 unsigned long hpa, unsigned long mask) 860 { 861 unsigned long gpa; 862 unsigned int shift, lpid; 863 pte_t *ptep; 864 865 gpa = n_rmap & RMAP_NESTED_GPA_MASK; 866 lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT; 867 868 /* Find the pte */ 869 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift); 870 /* 871 * If the pte is present and the pfn is still the same, update the pte. 872 * If the pfn has changed then this is a stale rmap entry, the nested 873 * gpa actually points somewhere else now, and there is nothing to do. 874 * XXX A future optimisation would be to remove the rmap entry here. 875 */ 876 if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa)) { 877 __radix_pte_update(ptep, clr, set); 878 kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid); 879 } 880 } 881 882 /* 883 * For a given list of rmap entries, update the rc bits in all ptes in shadow 884 * page tables for nested guests which are referenced by the rmap list. 885 */ 886 void kvmhv_update_nest_rmap_rc_list(struct kvm *kvm, unsigned long *rmapp, 887 unsigned long clr, unsigned long set, 888 unsigned long hpa, unsigned long nbytes) 889 { 890 struct llist_node *entry = ((struct llist_head *) rmapp)->first; 891 struct rmap_nested *cursor; 892 unsigned long rmap, mask; 893 894 if ((clr | set) & ~(_PAGE_DIRTY | _PAGE_ACCESSED)) 895 return; 896 897 mask = PTE_RPN_MASK & ~(nbytes - 1); 898 hpa &= mask; 899 900 for_each_nest_rmap_safe(cursor, entry, &rmap) 901 kvmhv_update_nest_rmap_rc(kvm, rmap, clr, set, hpa, mask); 902 } 903 904 static void kvmhv_remove_nest_rmap(struct kvm *kvm, u64 n_rmap, 905 unsigned long hpa, unsigned long mask) 906 { 907 struct kvm_nested_guest *gp; 908 unsigned long gpa; 909 unsigned int shift, lpid; 910 pte_t *ptep; 911 912 gpa = n_rmap & RMAP_NESTED_GPA_MASK; 913 lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT; 914 gp = kvmhv_find_nested(kvm, lpid); 915 if (!gp) 916 return; 917 918 /* Find and invalidate the pte */ 919 ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift); 920 /* Don't spuriously invalidate ptes if the pfn has changed */ 921 if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa)) 922 kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid); 923 } 924 925 static void kvmhv_remove_nest_rmap_list(struct kvm *kvm, unsigned long *rmapp, 926 unsigned long hpa, unsigned long mask) 927 { 928 struct llist_node *entry = llist_del_all((struct llist_head *) rmapp); 929 struct rmap_nested *cursor; 930 unsigned long rmap; 931 932 for_each_nest_rmap_safe(cursor, entry, &rmap) { 933 kvmhv_remove_nest_rmap(kvm, rmap, hpa, mask); 934 kfree(cursor); 935 } 936 } 937 938 /* called with kvm->mmu_lock held */ 939 void kvmhv_remove_nest_rmap_range(struct kvm *kvm, 940 const struct kvm_memory_slot *memslot, 941 unsigned long gpa, unsigned long hpa, 942 unsigned long nbytes) 943 { 944 unsigned long gfn, end_gfn; 945 unsigned long addr_mask; 946 947 if (!memslot) 948 return; 949 gfn = (gpa >> PAGE_SHIFT) - memslot->base_gfn; 950 end_gfn = gfn + (nbytes >> PAGE_SHIFT); 951 952 addr_mask = PTE_RPN_MASK & ~(nbytes - 1); 953 hpa &= addr_mask; 954 955 for (; gfn < end_gfn; gfn++) { 956 unsigned long *rmap = &memslot->arch.rmap[gfn]; 957 kvmhv_remove_nest_rmap_list(kvm, rmap, hpa, addr_mask); 958 } 959 } 960 961 static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free) 962 { 963 unsigned long page; 964 965 for (page = 0; page < free->npages; page++) { 966 unsigned long rmap, *rmapp = &free->arch.rmap[page]; 967 struct rmap_nested *cursor; 968 struct llist_node *entry; 969 970 entry = llist_del_all((struct llist_head *) rmapp); 971 for_each_nest_rmap_safe(cursor, entry, &rmap) 972 kfree(cursor); 973 } 974 } 975 976 static bool kvmhv_invalidate_shadow_pte(struct kvm_vcpu *vcpu, 977 struct kvm_nested_guest *gp, 978 long gpa, int *shift_ret) 979 { 980 struct kvm *kvm = vcpu->kvm; 981 bool ret = false; 982 pte_t *ptep; 983 int shift; 984 985 spin_lock(&kvm->mmu_lock); 986 ptep = find_kvm_nested_guest_pte(kvm, gp->l1_lpid, gpa, &shift); 987 if (!shift) 988 shift = PAGE_SHIFT; 989 if (ptep && pte_present(*ptep)) { 990 kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid); 991 ret = true; 992 } 993 spin_unlock(&kvm->mmu_lock); 994 995 if (shift_ret) 996 *shift_ret = shift; 997 return ret; 998 } 999 1000 static inline int get_ric(unsigned int instr) 1001 { 1002 return (instr >> 18) & 0x3; 1003 } 1004 1005 static inline int get_prs(unsigned int instr) 1006 { 1007 return (instr >> 17) & 0x1; 1008 } 1009 1010 static inline int get_r(unsigned int instr) 1011 { 1012 return (instr >> 16) & 0x1; 1013 } 1014 1015 static inline int get_lpid(unsigned long r_val) 1016 { 1017 return r_val & 0xffffffff; 1018 } 1019 1020 static inline int get_is(unsigned long r_val) 1021 { 1022 return (r_val >> 10) & 0x3; 1023 } 1024 1025 static inline int get_ap(unsigned long r_val) 1026 { 1027 return (r_val >> 5) & 0x7; 1028 } 1029 1030 static inline long get_epn(unsigned long r_val) 1031 { 1032 return r_val >> 12; 1033 } 1034 1035 static int kvmhv_emulate_tlbie_tlb_addr(struct kvm_vcpu *vcpu, int lpid, 1036 int ap, long epn) 1037 { 1038 struct kvm *kvm = vcpu->kvm; 1039 struct kvm_nested_guest *gp; 1040 long npages; 1041 int shift, shadow_shift; 1042 unsigned long addr; 1043 1044 shift = ap_to_shift(ap); 1045 addr = epn << 12; 1046 if (shift < 0) 1047 /* Invalid ap encoding */ 1048 return -EINVAL; 1049 1050 addr &= ~((1UL << shift) - 1); 1051 npages = 1UL << (shift - PAGE_SHIFT); 1052 1053 gp = kvmhv_get_nested(kvm, lpid, false); 1054 if (!gp) /* No such guest -> nothing to do */ 1055 return 0; 1056 mutex_lock(&gp->tlb_lock); 1057 1058 /* There may be more than one host page backing this single guest pte */ 1059 do { 1060 kvmhv_invalidate_shadow_pte(vcpu, gp, addr, &shadow_shift); 1061 1062 npages -= 1UL << (shadow_shift - PAGE_SHIFT); 1063 addr += 1UL << shadow_shift; 1064 } while (npages > 0); 1065 1066 mutex_unlock(&gp->tlb_lock); 1067 kvmhv_put_nested(gp); 1068 return 0; 1069 } 1070 1071 static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu, 1072 struct kvm_nested_guest *gp, int ric) 1073 { 1074 struct kvm *kvm = vcpu->kvm; 1075 1076 mutex_lock(&gp->tlb_lock); 1077 switch (ric) { 1078 case 0: 1079 /* Invalidate TLB */ 1080 spin_lock(&kvm->mmu_lock); 1081 kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, 1082 gp->shadow_lpid); 1083 kvmhv_flush_lpid(gp->shadow_lpid); 1084 spin_unlock(&kvm->mmu_lock); 1085 break; 1086 case 1: 1087 /* 1088 * Invalidate PWC 1089 * We don't cache this -> nothing to do 1090 */ 1091 break; 1092 case 2: 1093 /* Invalidate TLB, PWC and caching of partition table entries */ 1094 kvmhv_flush_nested(gp); 1095 break; 1096 default: 1097 break; 1098 } 1099 mutex_unlock(&gp->tlb_lock); 1100 } 1101 1102 static void kvmhv_emulate_tlbie_all_lpid(struct kvm_vcpu *vcpu, int ric) 1103 { 1104 struct kvm *kvm = vcpu->kvm; 1105 struct kvm_nested_guest *gp; 1106 int i; 1107 1108 spin_lock(&kvm->mmu_lock); 1109 for (i = 0; i <= kvm->arch.max_nested_lpid; i++) { 1110 gp = kvm->arch.nested_guests[i]; 1111 if (gp) { 1112 spin_unlock(&kvm->mmu_lock); 1113 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric); 1114 spin_lock(&kvm->mmu_lock); 1115 } 1116 } 1117 spin_unlock(&kvm->mmu_lock); 1118 } 1119 1120 static int kvmhv_emulate_priv_tlbie(struct kvm_vcpu *vcpu, unsigned int instr, 1121 unsigned long rsval, unsigned long rbval) 1122 { 1123 struct kvm *kvm = vcpu->kvm; 1124 struct kvm_nested_guest *gp; 1125 int r, ric, prs, is, ap; 1126 int lpid; 1127 long epn; 1128 int ret = 0; 1129 1130 ric = get_ric(instr); 1131 prs = get_prs(instr); 1132 r = get_r(instr); 1133 lpid = get_lpid(rsval); 1134 is = get_is(rbval); 1135 1136 /* 1137 * These cases are invalid and are not handled: 1138 * r != 1 -> Only radix supported 1139 * prs == 1 -> Not HV privileged 1140 * ric == 3 -> No cluster bombs for radix 1141 * is == 1 -> Partition scoped translations not associated with pid 1142 * (!is) && (ric == 1 || ric == 2) -> Not supported by ISA 1143 */ 1144 if ((!r) || (prs) || (ric == 3) || (is == 1) || 1145 ((!is) && (ric == 1 || ric == 2))) 1146 return -EINVAL; 1147 1148 switch (is) { 1149 case 0: 1150 /* 1151 * We know ric == 0 1152 * Invalidate TLB for a given target address 1153 */ 1154 epn = get_epn(rbval); 1155 ap = get_ap(rbval); 1156 ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap, epn); 1157 break; 1158 case 2: 1159 /* Invalidate matching LPID */ 1160 gp = kvmhv_get_nested(kvm, lpid, false); 1161 if (gp) { 1162 kvmhv_emulate_tlbie_lpid(vcpu, gp, ric); 1163 kvmhv_put_nested(gp); 1164 } 1165 break; 1166 case 3: 1167 /* Invalidate ALL LPIDs */ 1168 kvmhv_emulate_tlbie_all_lpid(vcpu, ric); 1169 break; 1170 default: 1171 ret = -EINVAL; 1172 break; 1173 } 1174 1175 return ret; 1176 } 1177 1178 /* 1179 * This handles the H_TLB_INVALIDATE hcall. 1180 * Parameters are (r4) tlbie instruction code, (r5) rS contents, 1181 * (r6) rB contents. 1182 */ 1183 long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu) 1184 { 1185 int ret; 1186 1187 ret = kvmhv_emulate_priv_tlbie(vcpu, kvmppc_get_gpr(vcpu, 4), 1188 kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6)); 1189 if (ret) 1190 return H_PARAMETER; 1191 return H_SUCCESS; 1192 } 1193 1194 /* Used to convert a nested guest real address to a L1 guest real address */ 1195 static int kvmhv_translate_addr_nested(struct kvm_vcpu *vcpu, 1196 struct kvm_nested_guest *gp, 1197 unsigned long n_gpa, unsigned long dsisr, 1198 struct kvmppc_pte *gpte_p) 1199 { 1200 u64 fault_addr, flags = dsisr & DSISR_ISSTORE; 1201 int ret; 1202 1203 ret = kvmppc_mmu_walk_radix_tree(vcpu, n_gpa, gpte_p, gp->l1_gr_to_hr, 1204 &fault_addr); 1205 1206 if (ret) { 1207 /* We didn't find a pte */ 1208 if (ret == -EINVAL) { 1209 /* Unsupported mmu config */ 1210 flags |= DSISR_UNSUPP_MMU; 1211 } else if (ret == -ENOENT) { 1212 /* No translation found */ 1213 flags |= DSISR_NOHPTE; 1214 } else if (ret == -EFAULT) { 1215 /* Couldn't access L1 real address */ 1216 flags |= DSISR_PRTABLE_FAULT; 1217 vcpu->arch.fault_gpa = fault_addr; 1218 } else { 1219 /* Unknown error */ 1220 return ret; 1221 } 1222 goto forward_to_l1; 1223 } else { 1224 /* We found a pte -> check permissions */ 1225 if (dsisr & DSISR_ISSTORE) { 1226 /* Can we write? */ 1227 if (!gpte_p->may_write) { 1228 flags |= DSISR_PROTFAULT; 1229 goto forward_to_l1; 1230 } 1231 } else if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) { 1232 /* Can we execute? */ 1233 if (!gpte_p->may_execute) { 1234 flags |= SRR1_ISI_N_G_OR_CIP; 1235 goto forward_to_l1; 1236 } 1237 } else { 1238 /* Can we read? */ 1239 if (!gpte_p->may_read && !gpte_p->may_write) { 1240 flags |= DSISR_PROTFAULT; 1241 goto forward_to_l1; 1242 } 1243 } 1244 } 1245 1246 return 0; 1247 1248 forward_to_l1: 1249 vcpu->arch.fault_dsisr = flags; 1250 if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) { 1251 vcpu->arch.shregs.msr &= SRR1_MSR_BITS; 1252 vcpu->arch.shregs.msr |= flags; 1253 } 1254 return RESUME_HOST; 1255 } 1256 1257 static long kvmhv_handle_nested_set_rc(struct kvm_vcpu *vcpu, 1258 struct kvm_nested_guest *gp, 1259 unsigned long n_gpa, 1260 struct kvmppc_pte gpte, 1261 unsigned long dsisr) 1262 { 1263 struct kvm *kvm = vcpu->kvm; 1264 bool writing = !!(dsisr & DSISR_ISSTORE); 1265 u64 pgflags; 1266 long ret; 1267 1268 /* Are the rc bits set in the L1 partition scoped pte? */ 1269 pgflags = _PAGE_ACCESSED; 1270 if (writing) 1271 pgflags |= _PAGE_DIRTY; 1272 if (pgflags & ~gpte.rc) 1273 return RESUME_HOST; 1274 1275 spin_lock(&kvm->mmu_lock); 1276 /* Set the rc bit in the pte of our (L0) pgtable for the L1 guest */ 1277 ret = kvmppc_hv_handle_set_rc(kvm, false, writing, 1278 gpte.raddr, kvm->arch.lpid); 1279 if (!ret) { 1280 ret = -EINVAL; 1281 goto out_unlock; 1282 } 1283 1284 /* Set the rc bit in the pte of the shadow_pgtable for the nest guest */ 1285 ret = kvmppc_hv_handle_set_rc(kvm, true, writing, 1286 n_gpa, gp->l1_lpid); 1287 if (!ret) 1288 ret = -EINVAL; 1289 else 1290 ret = 0; 1291 1292 out_unlock: 1293 spin_unlock(&kvm->mmu_lock); 1294 return ret; 1295 } 1296 1297 static inline int kvmppc_radix_level_to_shift(int level) 1298 { 1299 switch (level) { 1300 case 2: 1301 return PUD_SHIFT; 1302 case 1: 1303 return PMD_SHIFT; 1304 default: 1305 return PAGE_SHIFT; 1306 } 1307 } 1308 1309 static inline int kvmppc_radix_shift_to_level(int shift) 1310 { 1311 if (shift == PUD_SHIFT) 1312 return 2; 1313 if (shift == PMD_SHIFT) 1314 return 1; 1315 if (shift == PAGE_SHIFT) 1316 return 0; 1317 WARN_ON_ONCE(1); 1318 return 0; 1319 } 1320 1321 /* called with gp->tlb_lock held */ 1322 static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu, 1323 struct kvm_nested_guest *gp) 1324 { 1325 struct kvm *kvm = vcpu->kvm; 1326 struct kvm_memory_slot *memslot; 1327 struct rmap_nested *n_rmap; 1328 struct kvmppc_pte gpte; 1329 pte_t pte, *pte_p; 1330 unsigned long mmu_seq; 1331 unsigned long dsisr = vcpu->arch.fault_dsisr; 1332 unsigned long ea = vcpu->arch.fault_dar; 1333 unsigned long *rmapp; 1334 unsigned long n_gpa, gpa, gfn, perm = 0UL; 1335 unsigned int shift, l1_shift, level; 1336 bool writing = !!(dsisr & DSISR_ISSTORE); 1337 bool kvm_ro = false; 1338 long int ret; 1339 1340 if (!gp->l1_gr_to_hr) { 1341 kvmhv_update_ptbl_cache(gp); 1342 if (!gp->l1_gr_to_hr) 1343 return RESUME_HOST; 1344 } 1345 1346 /* Convert the nested guest real address into a L1 guest real address */ 1347 1348 n_gpa = vcpu->arch.fault_gpa & ~0xF000000000000FFFULL; 1349 if (!(dsisr & DSISR_PRTABLE_FAULT)) 1350 n_gpa |= ea & 0xFFF; 1351 ret = kvmhv_translate_addr_nested(vcpu, gp, n_gpa, dsisr, &gpte); 1352 1353 /* 1354 * If the hardware found a translation but we don't now have a usable 1355 * translation in the l1 partition-scoped tree, remove the shadow pte 1356 * and let the guest retry. 1357 */ 1358 if (ret == RESUME_HOST && 1359 (dsisr & (DSISR_PROTFAULT | DSISR_BADACCESS | DSISR_NOEXEC_OR_G | 1360 DSISR_BAD_COPYPASTE))) 1361 goto inval; 1362 if (ret) 1363 return ret; 1364 1365 /* Failed to set the reference/change bits */ 1366 if (dsisr & DSISR_SET_RC) { 1367 ret = kvmhv_handle_nested_set_rc(vcpu, gp, n_gpa, gpte, dsisr); 1368 if (ret == RESUME_HOST) 1369 return ret; 1370 if (ret) 1371 goto inval; 1372 dsisr &= ~DSISR_SET_RC; 1373 if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE | 1374 DSISR_PROTFAULT))) 1375 return RESUME_GUEST; 1376 } 1377 1378 /* 1379 * We took an HISI or HDSI while we were running a nested guest which 1380 * means we have no partition scoped translation for that. This means 1381 * we need to insert a pte for the mapping into our shadow_pgtable. 1382 */ 1383 1384 l1_shift = gpte.page_shift; 1385 if (l1_shift < PAGE_SHIFT) { 1386 /* We don't support l1 using a page size smaller than our own */ 1387 pr_err("KVM: L1 guest page shift (%d) less than our own (%d)\n", 1388 l1_shift, PAGE_SHIFT); 1389 return -EINVAL; 1390 } 1391 gpa = gpte.raddr; 1392 gfn = gpa >> PAGE_SHIFT; 1393 1394 /* 1. Get the corresponding host memslot */ 1395 1396 memslot = gfn_to_memslot(kvm, gfn); 1397 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) { 1398 if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS)) { 1399 /* unusual error -> reflect to the guest as a DSI */ 1400 kvmppc_core_queue_data_storage(vcpu, ea, dsisr); 1401 return RESUME_GUEST; 1402 } 1403 1404 /* passthrough of emulated MMIO case */ 1405 return kvmppc_hv_emulate_mmio(vcpu, gpa, ea, writing); 1406 } 1407 if (memslot->flags & KVM_MEM_READONLY) { 1408 if (writing) { 1409 /* Give the guest a DSI */ 1410 kvmppc_core_queue_data_storage(vcpu, ea, 1411 DSISR_ISSTORE | DSISR_PROTFAULT); 1412 return RESUME_GUEST; 1413 } 1414 kvm_ro = true; 1415 } 1416 1417 /* 2. Find the host pte for this L1 guest real address */ 1418 1419 /* Used to check for invalidations in progress */ 1420 mmu_seq = kvm->mmu_notifier_seq; 1421 smp_rmb(); 1422 1423 /* See if can find translation in our partition scoped tables for L1 */ 1424 pte = __pte(0); 1425 spin_lock(&kvm->mmu_lock); 1426 pte_p = find_kvm_secondary_pte(kvm, gpa, &shift); 1427 if (!shift) 1428 shift = PAGE_SHIFT; 1429 if (pte_p) 1430 pte = *pte_p; 1431 spin_unlock(&kvm->mmu_lock); 1432 1433 if (!pte_present(pte) || (writing && !(pte_val(pte) & _PAGE_WRITE))) { 1434 /* No suitable pte found -> try to insert a mapping */ 1435 ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot, 1436 writing, kvm_ro, &pte, &level); 1437 if (ret == -EAGAIN) 1438 return RESUME_GUEST; 1439 else if (ret) 1440 return ret; 1441 shift = kvmppc_radix_level_to_shift(level); 1442 } 1443 /* Align gfn to the start of the page */ 1444 gfn = (gpa & ~((1UL << shift) - 1)) >> PAGE_SHIFT; 1445 1446 /* 3. Compute the pte we need to insert for nest_gpa -> host r_addr */ 1447 1448 /* The permissions is the combination of the host and l1 guest ptes */ 1449 perm |= gpte.may_read ? 0UL : _PAGE_READ; 1450 perm |= gpte.may_write ? 0UL : _PAGE_WRITE; 1451 perm |= gpte.may_execute ? 0UL : _PAGE_EXEC; 1452 /* Only set accessed/dirty (rc) bits if set in host and l1 guest ptes */ 1453 perm |= (gpte.rc & _PAGE_ACCESSED) ? 0UL : _PAGE_ACCESSED; 1454 perm |= ((gpte.rc & _PAGE_DIRTY) && writing) ? 0UL : _PAGE_DIRTY; 1455 pte = __pte(pte_val(pte) & ~perm); 1456 1457 /* What size pte can we insert? */ 1458 if (shift > l1_shift) { 1459 u64 mask; 1460 unsigned int actual_shift = PAGE_SHIFT; 1461 if (PMD_SHIFT < l1_shift) 1462 actual_shift = PMD_SHIFT; 1463 mask = (1UL << shift) - (1UL << actual_shift); 1464 pte = __pte(pte_val(pte) | (gpa & mask)); 1465 shift = actual_shift; 1466 } 1467 level = kvmppc_radix_shift_to_level(shift); 1468 n_gpa &= ~((1UL << shift) - 1); 1469 1470 /* 4. Insert the pte into our shadow_pgtable */ 1471 1472 n_rmap = kzalloc(sizeof(*n_rmap), GFP_KERNEL); 1473 if (!n_rmap) 1474 return RESUME_GUEST; /* Let the guest try again */ 1475 n_rmap->rmap = (n_gpa & RMAP_NESTED_GPA_MASK) | 1476 (((unsigned long) gp->l1_lpid) << RMAP_NESTED_LPID_SHIFT); 1477 rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn]; 1478 ret = kvmppc_create_pte(kvm, gp->shadow_pgtable, pte, n_gpa, level, 1479 mmu_seq, gp->shadow_lpid, rmapp, &n_rmap); 1480 kfree(n_rmap); 1481 if (ret == -EAGAIN) 1482 ret = RESUME_GUEST; /* Let the guest try again */ 1483 1484 return ret; 1485 1486 inval: 1487 kvmhv_invalidate_shadow_pte(vcpu, gp, n_gpa, NULL); 1488 return RESUME_GUEST; 1489 } 1490 1491 long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu) 1492 { 1493 struct kvm_nested_guest *gp = vcpu->arch.nested; 1494 long int ret; 1495 1496 mutex_lock(&gp->tlb_lock); 1497 ret = __kvmhv_nested_page_fault(vcpu, gp); 1498 mutex_unlock(&gp->tlb_lock); 1499 return ret; 1500 } 1501 1502 int kvmhv_nested_next_lpid(struct kvm *kvm, int lpid) 1503 { 1504 int ret = -1; 1505 1506 spin_lock(&kvm->mmu_lock); 1507 while (++lpid <= kvm->arch.max_nested_lpid) { 1508 if (kvm->arch.nested_guests[lpid]) { 1509 ret = lpid; 1510 break; 1511 } 1512 } 1513 spin_unlock(&kvm->mmu_lock); 1514 return ret; 1515 } 1516