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