1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * kvm nested virtualization support for s390x 4 * 5 * Copyright IBM Corp. 2016, 2018 6 * 7 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com> 8 */ 9 #include <linux/vmalloc.h> 10 #include <linux/kvm_host.h> 11 #include <linux/bug.h> 12 #include <linux/list.h> 13 #include <linux/bitmap.h> 14 #include <linux/sched/signal.h> 15 16 #include <asm/gmap.h> 17 #include <asm/mmu_context.h> 18 #include <asm/sclp.h> 19 #include <asm/nmi.h> 20 #include <asm/dis.h> 21 #include "kvm-s390.h" 22 #include "gaccess.h" 23 24 struct vsie_page { 25 struct kvm_s390_sie_block scb_s; /* 0x0000 */ 26 /* 27 * the backup info for machine check. ensure it's at 28 * the same offset as that in struct sie_page! 29 */ 30 struct mcck_volatile_info mcck_info; /* 0x0200 */ 31 /* 32 * The pinned original scb. Be aware that other VCPUs can modify 33 * it while we read from it. Values that are used for conditions or 34 * are reused conditionally, should be accessed via READ_ONCE. 35 */ 36 struct kvm_s390_sie_block *scb_o; /* 0x0218 */ 37 /* the shadow gmap in use by the vsie_page */ 38 struct gmap *gmap; /* 0x0220 */ 39 /* address of the last reported fault to guest2 */ 40 unsigned long fault_addr; /* 0x0228 */ 41 /* calculated guest addresses of satellite control blocks */ 42 gpa_t sca_gpa; /* 0x0230 */ 43 gpa_t itdba_gpa; /* 0x0238 */ 44 gpa_t gvrd_gpa; /* 0x0240 */ 45 gpa_t riccbd_gpa; /* 0x0248 */ 46 gpa_t sdnx_gpa; /* 0x0250 */ 47 __u8 reserved[0x0700 - 0x0258]; /* 0x0258 */ 48 struct kvm_s390_crypto_cb crycb; /* 0x0700 */ 49 __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */ 50 }; 51 52 /* trigger a validity icpt for the given scb */ 53 static int set_validity_icpt(struct kvm_s390_sie_block *scb, 54 __u16 reason_code) 55 { 56 scb->ipa = 0x1000; 57 scb->ipb = ((__u32) reason_code) << 16; 58 scb->icptcode = ICPT_VALIDITY; 59 return 1; 60 } 61 62 /* mark the prefix as unmapped, this will block the VSIE */ 63 static void prefix_unmapped(struct vsie_page *vsie_page) 64 { 65 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20); 66 } 67 68 /* mark the prefix as unmapped and wait until the VSIE has been left */ 69 static void prefix_unmapped_sync(struct vsie_page *vsie_page) 70 { 71 prefix_unmapped(vsie_page); 72 if (vsie_page->scb_s.prog0c & PROG_IN_SIE) 73 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags); 74 while (vsie_page->scb_s.prog0c & PROG_IN_SIE) 75 cpu_relax(); 76 } 77 78 /* mark the prefix as mapped, this will allow the VSIE to run */ 79 static void prefix_mapped(struct vsie_page *vsie_page) 80 { 81 atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20); 82 } 83 84 /* test if the prefix is mapped into the gmap shadow */ 85 static int prefix_is_mapped(struct vsie_page *vsie_page) 86 { 87 return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST); 88 } 89 90 /* copy the updated intervention request bits into the shadow scb */ 91 static void update_intervention_requests(struct vsie_page *vsie_page) 92 { 93 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT; 94 int cpuflags; 95 96 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags); 97 atomic_andnot(bits, &vsie_page->scb_s.cpuflags); 98 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags); 99 } 100 101 /* shadow (filter and validate) the cpuflags */ 102 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 103 { 104 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 105 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; 106 int newflags, cpuflags = atomic_read(&scb_o->cpuflags); 107 108 /* we don't allow ESA/390 guests */ 109 if (!(cpuflags & CPUSTAT_ZARCH)) 110 return set_validity_icpt(scb_s, 0x0001U); 111 112 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS)) 113 return set_validity_icpt(scb_s, 0x0001U); 114 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR)) 115 return set_validity_icpt(scb_s, 0x0007U); 116 117 /* intervention requests will be set later */ 118 newflags = CPUSTAT_ZARCH; 119 if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8)) 120 newflags |= CPUSTAT_GED; 121 if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) { 122 if (cpuflags & CPUSTAT_GED) 123 return set_validity_icpt(scb_s, 0x0001U); 124 newflags |= CPUSTAT_GED2; 125 } 126 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE)) 127 newflags |= cpuflags & CPUSTAT_P; 128 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS)) 129 newflags |= cpuflags & CPUSTAT_SM; 130 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS)) 131 newflags |= cpuflags & CPUSTAT_IBS; 132 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS)) 133 newflags |= cpuflags & CPUSTAT_KSS; 134 135 atomic_set(&scb_s->cpuflags, newflags); 136 return 0; 137 } 138 /* Copy to APCB FORMAT1 from APCB FORMAT0 */ 139 static int setup_apcb10(struct kvm_vcpu *vcpu, struct kvm_s390_apcb1 *apcb_s, 140 unsigned long apcb_o, struct kvm_s390_apcb1 *apcb_h) 141 { 142 struct kvm_s390_apcb0 tmp; 143 144 if (read_guest_real(vcpu, apcb_o, &tmp, sizeof(struct kvm_s390_apcb0))) 145 return -EFAULT; 146 147 apcb_s->apm[0] = apcb_h->apm[0] & tmp.apm[0]; 148 apcb_s->aqm[0] = apcb_h->aqm[0] & tmp.aqm[0] & 0xffff000000000000UL; 149 apcb_s->adm[0] = apcb_h->adm[0] & tmp.adm[0] & 0xffff000000000000UL; 150 151 return 0; 152 153 } 154 155 /** 156 * setup_apcb00 - Copy to APCB FORMAT0 from APCB FORMAT0 157 * @vcpu: pointer to the virtual CPU 158 * @apcb_s: pointer to start of apcb in the shadow crycb 159 * @apcb_o: pointer to start of original apcb in the guest2 160 * @apcb_h: pointer to start of apcb in the guest1 161 * 162 * Returns 0 and -EFAULT on error reading guest apcb 163 */ 164 static int setup_apcb00(struct kvm_vcpu *vcpu, unsigned long *apcb_s, 165 unsigned long apcb_o, unsigned long *apcb_h) 166 { 167 if (read_guest_real(vcpu, apcb_o, apcb_s, 168 sizeof(struct kvm_s390_apcb0))) 169 return -EFAULT; 170 171 bitmap_and(apcb_s, apcb_s, apcb_h, sizeof(struct kvm_s390_apcb0)); 172 173 return 0; 174 } 175 176 /** 177 * setup_apcb11 - Copy the FORMAT1 APCB from the guest to the shadow CRYCB 178 * @vcpu: pointer to the virtual CPU 179 * @apcb_s: pointer to start of apcb in the shadow crycb 180 * @apcb_o: pointer to start of original guest apcb 181 * @apcb_h: pointer to start of apcb in the host 182 * 183 * Returns 0 and -EFAULT on error reading guest apcb 184 */ 185 static int setup_apcb11(struct kvm_vcpu *vcpu, unsigned long *apcb_s, 186 unsigned long apcb_o, 187 unsigned long *apcb_h) 188 { 189 if (read_guest_real(vcpu, apcb_o, apcb_s, 190 sizeof(struct kvm_s390_apcb1))) 191 return -EFAULT; 192 193 bitmap_and(apcb_s, apcb_s, apcb_h, sizeof(struct kvm_s390_apcb1)); 194 195 return 0; 196 } 197 198 /** 199 * setup_apcb - Create a shadow copy of the apcb. 200 * @vcpu: pointer to the virtual CPU 201 * @crycb_s: pointer to shadow crycb 202 * @crycb_o: pointer to original guest crycb 203 * @crycb_h: pointer to the host crycb 204 * @fmt_o: format of the original guest crycb. 205 * @fmt_h: format of the host crycb. 206 * 207 * Checks the compatibility between the guest and host crycb and calls the 208 * appropriate copy function. 209 * 210 * Return 0 or an error number if the guest and host crycb are incompatible. 211 */ 212 static int setup_apcb(struct kvm_vcpu *vcpu, struct kvm_s390_crypto_cb *crycb_s, 213 const u32 crycb_o, 214 struct kvm_s390_crypto_cb *crycb_h, 215 int fmt_o, int fmt_h) 216 { 217 struct kvm_s390_crypto_cb *crycb; 218 219 crycb = (struct kvm_s390_crypto_cb *) (unsigned long)crycb_o; 220 221 switch (fmt_o) { 222 case CRYCB_FORMAT2: 223 if ((crycb_o & PAGE_MASK) != ((crycb_o + 256) & PAGE_MASK)) 224 return -EACCES; 225 if (fmt_h != CRYCB_FORMAT2) 226 return -EINVAL; 227 return setup_apcb11(vcpu, (unsigned long *)&crycb_s->apcb1, 228 (unsigned long) &crycb->apcb1, 229 (unsigned long *)&crycb_h->apcb1); 230 case CRYCB_FORMAT1: 231 switch (fmt_h) { 232 case CRYCB_FORMAT2: 233 return setup_apcb10(vcpu, &crycb_s->apcb1, 234 (unsigned long) &crycb->apcb0, 235 &crycb_h->apcb1); 236 case CRYCB_FORMAT1: 237 return setup_apcb00(vcpu, 238 (unsigned long *) &crycb_s->apcb0, 239 (unsigned long) &crycb->apcb0, 240 (unsigned long *) &crycb_h->apcb0); 241 } 242 break; 243 case CRYCB_FORMAT0: 244 if ((crycb_o & PAGE_MASK) != ((crycb_o + 32) & PAGE_MASK)) 245 return -EACCES; 246 247 switch (fmt_h) { 248 case CRYCB_FORMAT2: 249 return setup_apcb10(vcpu, &crycb_s->apcb1, 250 (unsigned long) &crycb->apcb0, 251 &crycb_h->apcb1); 252 case CRYCB_FORMAT1: 253 case CRYCB_FORMAT0: 254 return setup_apcb00(vcpu, 255 (unsigned long *) &crycb_s->apcb0, 256 (unsigned long) &crycb->apcb0, 257 (unsigned long *) &crycb_h->apcb0); 258 } 259 } 260 return -EINVAL; 261 } 262 263 /** 264 * shadow_crycb - Create a shadow copy of the crycb block 265 * @vcpu: a pointer to the virtual CPU 266 * @vsie_page: a pointer to internal date used for the vSIE 267 * 268 * Create a shadow copy of the crycb block and setup key wrapping, if 269 * requested for guest 3 and enabled for guest 2. 270 * 271 * We accept format-1 or format-2, but we convert format-1 into format-2 272 * in the shadow CRYCB. 273 * Using format-2 enables the firmware to choose the right format when 274 * scheduling the SIE. 275 * There is nothing to do for format-0. 276 * 277 * This function centralize the issuing of set_validity_icpt() for all 278 * the subfunctions working on the crycb. 279 * 280 * Returns: - 0 if shadowed or nothing to do 281 * - > 0 if control has to be given to guest 2 282 */ 283 static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 284 { 285 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 286 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; 287 const uint32_t crycbd_o = READ_ONCE(scb_o->crycbd); 288 const u32 crycb_addr = crycbd_o & 0x7ffffff8U; 289 unsigned long *b1, *b2; 290 u8 ecb3_flags; 291 int apie_h; 292 int key_msk = test_kvm_facility(vcpu->kvm, 76); 293 int fmt_o = crycbd_o & CRYCB_FORMAT_MASK; 294 int fmt_h = vcpu->arch.sie_block->crycbd & CRYCB_FORMAT_MASK; 295 int ret = 0; 296 297 scb_s->crycbd = 0; 298 299 apie_h = vcpu->arch.sie_block->eca & ECA_APIE; 300 if (!apie_h && !key_msk) 301 return 0; 302 303 if (!crycb_addr) 304 return set_validity_icpt(scb_s, 0x0039U); 305 306 if (fmt_o == CRYCB_FORMAT1) 307 if ((crycb_addr & PAGE_MASK) != 308 ((crycb_addr + 128) & PAGE_MASK)) 309 return set_validity_icpt(scb_s, 0x003CU); 310 311 if (apie_h && (scb_o->eca & ECA_APIE)) { 312 ret = setup_apcb(vcpu, &vsie_page->crycb, crycb_addr, 313 vcpu->kvm->arch.crypto.crycb, 314 fmt_o, fmt_h); 315 if (ret) 316 goto end; 317 scb_s->eca |= scb_o->eca & ECA_APIE; 318 } 319 320 /* we may only allow it if enabled for guest 2 */ 321 ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 & 322 (ECB3_AES | ECB3_DEA); 323 if (!ecb3_flags) 324 goto end; 325 326 /* copy only the wrapping keys */ 327 if (read_guest_real(vcpu, crycb_addr + 72, 328 vsie_page->crycb.dea_wrapping_key_mask, 56)) 329 return set_validity_icpt(scb_s, 0x0035U); 330 331 scb_s->ecb3 |= ecb3_flags; 332 333 /* xor both blocks in one run */ 334 b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask; 335 b2 = (unsigned long *) 336 vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask; 337 /* as 56%8 == 0, bitmap_xor won't overwrite any data */ 338 bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56); 339 end: 340 switch (ret) { 341 case -EINVAL: 342 return set_validity_icpt(scb_s, 0x0020U); 343 case -EFAULT: 344 return set_validity_icpt(scb_s, 0x0035U); 345 case -EACCES: 346 return set_validity_icpt(scb_s, 0x003CU); 347 } 348 scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2; 349 return 0; 350 } 351 352 /* shadow (round up/down) the ibc to avoid validity icpt */ 353 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 354 { 355 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 356 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; 357 /* READ_ONCE does not work on bitfields - use a temporary variable */ 358 const uint32_t __new_ibc = scb_o->ibc; 359 const uint32_t new_ibc = READ_ONCE(__new_ibc) & 0x0fffU; 360 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU; 361 362 scb_s->ibc = 0; 363 /* ibc installed in g2 and requested for g3 */ 364 if (vcpu->kvm->arch.model.ibc && new_ibc) { 365 scb_s->ibc = new_ibc; 366 /* takte care of the minimum ibc level of the machine */ 367 if (scb_s->ibc < min_ibc) 368 scb_s->ibc = min_ibc; 369 /* take care of the maximum ibc level set for the guest */ 370 if (scb_s->ibc > vcpu->kvm->arch.model.ibc) 371 scb_s->ibc = vcpu->kvm->arch.model.ibc; 372 } 373 } 374 375 /* unshadow the scb, copying parameters back to the real scb */ 376 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 377 { 378 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 379 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; 380 381 /* interception */ 382 scb_o->icptcode = scb_s->icptcode; 383 scb_o->icptstatus = scb_s->icptstatus; 384 scb_o->ipa = scb_s->ipa; 385 scb_o->ipb = scb_s->ipb; 386 scb_o->gbea = scb_s->gbea; 387 388 /* timer */ 389 scb_o->cputm = scb_s->cputm; 390 scb_o->ckc = scb_s->ckc; 391 scb_o->todpr = scb_s->todpr; 392 393 /* guest state */ 394 scb_o->gpsw = scb_s->gpsw; 395 scb_o->gg14 = scb_s->gg14; 396 scb_o->gg15 = scb_s->gg15; 397 memcpy(scb_o->gcr, scb_s->gcr, 128); 398 scb_o->pp = scb_s->pp; 399 400 /* branch prediction */ 401 if (test_kvm_facility(vcpu->kvm, 82)) { 402 scb_o->fpf &= ~FPF_BPBC; 403 scb_o->fpf |= scb_s->fpf & FPF_BPBC; 404 } 405 406 /* interrupt intercept */ 407 switch (scb_s->icptcode) { 408 case ICPT_PROGI: 409 case ICPT_INSTPROGI: 410 case ICPT_EXTINT: 411 memcpy((void *)((u64)scb_o + 0xc0), 412 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0); 413 break; 414 case ICPT_PARTEXEC: 415 /* MVPG only */ 416 memcpy((void *)((u64)scb_o + 0xc0), 417 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0); 418 break; 419 } 420 421 if (scb_s->ihcpu != 0xffffU) 422 scb_o->ihcpu = scb_s->ihcpu; 423 } 424 425 /* 426 * Setup the shadow scb by copying and checking the relevant parts of the g2 427 * provided scb. 428 * 429 * Returns: - 0 if the scb has been shadowed 430 * - > 0 if control has to be given to guest 2 431 */ 432 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 433 { 434 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; 435 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 436 /* READ_ONCE does not work on bitfields - use a temporary variable */ 437 const uint32_t __new_prefix = scb_o->prefix; 438 const uint32_t new_prefix = READ_ONCE(__new_prefix); 439 const bool wants_tx = READ_ONCE(scb_o->ecb) & ECB_TE; 440 bool had_tx = scb_s->ecb & ECB_TE; 441 unsigned long new_mso = 0; 442 int rc; 443 444 /* make sure we don't have any leftovers when reusing the scb */ 445 scb_s->icptcode = 0; 446 scb_s->eca = 0; 447 scb_s->ecb = 0; 448 scb_s->ecb2 = 0; 449 scb_s->ecb3 = 0; 450 scb_s->ecd = 0; 451 scb_s->fac = 0; 452 scb_s->fpf = 0; 453 454 rc = prepare_cpuflags(vcpu, vsie_page); 455 if (rc) 456 goto out; 457 458 /* timer */ 459 scb_s->cputm = scb_o->cputm; 460 scb_s->ckc = scb_o->ckc; 461 scb_s->todpr = scb_o->todpr; 462 scb_s->epoch = scb_o->epoch; 463 464 /* guest state */ 465 scb_s->gpsw = scb_o->gpsw; 466 scb_s->gg14 = scb_o->gg14; 467 scb_s->gg15 = scb_o->gg15; 468 memcpy(scb_s->gcr, scb_o->gcr, 128); 469 scb_s->pp = scb_o->pp; 470 471 /* interception / execution handling */ 472 scb_s->gbea = scb_o->gbea; 473 scb_s->lctl = scb_o->lctl; 474 scb_s->svcc = scb_o->svcc; 475 scb_s->ictl = scb_o->ictl; 476 /* 477 * SKEY handling functions can't deal with false setting of PTE invalid 478 * bits. Therefore we cannot provide interpretation and would later 479 * have to provide own emulation handlers. 480 */ 481 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS)) 482 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 483 484 scb_s->icpua = scb_o->icpua; 485 486 if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM)) 487 new_mso = READ_ONCE(scb_o->mso) & 0xfffffffffff00000UL; 488 /* if the hva of the prefix changes, we have to remap the prefix */ 489 if (scb_s->mso != new_mso || scb_s->prefix != new_prefix) 490 prefix_unmapped(vsie_page); 491 /* SIE will do mso/msl validity and exception checks for us */ 492 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL; 493 scb_s->mso = new_mso; 494 scb_s->prefix = new_prefix; 495 496 /* We have to definetly flush the tlb if this scb never ran */ 497 if (scb_s->ihcpu != 0xffffU) 498 scb_s->ihcpu = scb_o->ihcpu; 499 500 /* MVPG and Protection Exception Interpretation are always available */ 501 scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI); 502 /* Host-protection-interruption introduced with ESOP */ 503 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP)) 504 scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT; 505 /* transactional execution */ 506 if (test_kvm_facility(vcpu->kvm, 73) && wants_tx) { 507 /* remap the prefix is tx is toggled on */ 508 if (!had_tx) 509 prefix_unmapped(vsie_page); 510 scb_s->ecb |= ECB_TE; 511 } 512 /* branch prediction */ 513 if (test_kvm_facility(vcpu->kvm, 82)) 514 scb_s->fpf |= scb_o->fpf & FPF_BPBC; 515 /* SIMD */ 516 if (test_kvm_facility(vcpu->kvm, 129)) { 517 scb_s->eca |= scb_o->eca & ECA_VX; 518 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT; 519 } 520 /* Run-time-Instrumentation */ 521 if (test_kvm_facility(vcpu->kvm, 64)) 522 scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI; 523 /* Instruction Execution Prevention */ 524 if (test_kvm_facility(vcpu->kvm, 130)) 525 scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP; 526 /* Guarded Storage */ 527 if (test_kvm_facility(vcpu->kvm, 133)) { 528 scb_s->ecb |= scb_o->ecb & ECB_GS; 529 scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT; 530 } 531 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF)) 532 scb_s->eca |= scb_o->eca & ECA_SII; 533 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB)) 534 scb_s->eca |= scb_o->eca & ECA_IB; 535 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI)) 536 scb_s->eca |= scb_o->eca & ECA_CEI; 537 /* Epoch Extension */ 538 if (test_kvm_facility(vcpu->kvm, 139)) 539 scb_s->ecd |= scb_o->ecd & ECD_MEF; 540 541 /* etoken */ 542 if (test_kvm_facility(vcpu->kvm, 156)) 543 scb_s->ecd |= scb_o->ecd & ECD_ETOKENF; 544 545 scb_s->hpid = HPID_VSIE; 546 547 prepare_ibc(vcpu, vsie_page); 548 rc = shadow_crycb(vcpu, vsie_page); 549 out: 550 if (rc) 551 unshadow_scb(vcpu, vsie_page); 552 return rc; 553 } 554 555 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start, 556 unsigned long end) 557 { 558 struct kvm *kvm = gmap->private; 559 struct vsie_page *cur; 560 unsigned long prefix; 561 struct page *page; 562 int i; 563 564 if (!gmap_is_shadow(gmap)) 565 return; 566 if (start >= 1UL << 31) 567 /* We are only interested in prefix pages */ 568 return; 569 570 /* 571 * Only new shadow blocks are added to the list during runtime, 572 * therefore we can safely reference them all the time. 573 */ 574 for (i = 0; i < kvm->arch.vsie.page_count; i++) { 575 page = READ_ONCE(kvm->arch.vsie.pages[i]); 576 if (!page) 577 continue; 578 cur = page_to_virt(page); 579 if (READ_ONCE(cur->gmap) != gmap) 580 continue; 581 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT; 582 /* with mso/msl, the prefix lies at an offset */ 583 prefix += cur->scb_s.mso; 584 if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1) 585 prefix_unmapped_sync(cur); 586 } 587 } 588 589 /* 590 * Map the first prefix page and if tx is enabled also the second prefix page. 591 * 592 * The prefix will be protected, a gmap notifier will inform about unmaps. 593 * The shadow scb must not be executed until the prefix is remapped, this is 594 * guaranteed by properly handling PROG_REQUEST. 595 * 596 * Returns: - 0 on if successfully mapped or already mapped 597 * - > 0 if control has to be given to guest 2 598 * - -EAGAIN if the caller can retry immediately 599 * - -ENOMEM if out of memory 600 */ 601 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 602 { 603 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 604 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT; 605 int rc; 606 607 if (prefix_is_mapped(vsie_page)) 608 return 0; 609 610 /* mark it as mapped so we can catch any concurrent unmappers */ 611 prefix_mapped(vsie_page); 612 613 /* with mso/msl, the prefix lies at offset *mso* */ 614 prefix += scb_s->mso; 615 616 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix); 617 if (!rc && (scb_s->ecb & ECB_TE)) 618 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, 619 prefix + PAGE_SIZE); 620 /* 621 * We don't have to mprotect, we will be called for all unshadows. 622 * SIE will detect if protection applies and trigger a validity. 623 */ 624 if (rc) 625 prefix_unmapped(vsie_page); 626 if (rc > 0 || rc == -EFAULT) 627 rc = set_validity_icpt(scb_s, 0x0037U); 628 return rc; 629 } 630 631 /* 632 * Pin the guest page given by gpa and set hpa to the pinned host address. 633 * Will always be pinned writable. 634 * 635 * Returns: - 0 on success 636 * - -EINVAL if the gpa is not valid guest storage 637 */ 638 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa) 639 { 640 struct page *page; 641 642 page = gfn_to_page(kvm, gpa_to_gfn(gpa)); 643 if (is_error_page(page)) 644 return -EINVAL; 645 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK); 646 return 0; 647 } 648 649 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */ 650 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa) 651 { 652 kvm_release_pfn_dirty(hpa >> PAGE_SHIFT); 653 /* mark the page always as dirty for migration */ 654 mark_page_dirty(kvm, gpa_to_gfn(gpa)); 655 } 656 657 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */ 658 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 659 { 660 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 661 hpa_t hpa; 662 663 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol; 664 if (hpa) { 665 unpin_guest_page(vcpu->kvm, vsie_page->sca_gpa, hpa); 666 vsie_page->sca_gpa = 0; 667 scb_s->scaol = 0; 668 scb_s->scaoh = 0; 669 } 670 671 hpa = scb_s->itdba; 672 if (hpa) { 673 unpin_guest_page(vcpu->kvm, vsie_page->itdba_gpa, hpa); 674 vsie_page->itdba_gpa = 0; 675 scb_s->itdba = 0; 676 } 677 678 hpa = scb_s->gvrd; 679 if (hpa) { 680 unpin_guest_page(vcpu->kvm, vsie_page->gvrd_gpa, hpa); 681 vsie_page->gvrd_gpa = 0; 682 scb_s->gvrd = 0; 683 } 684 685 hpa = scb_s->riccbd; 686 if (hpa) { 687 unpin_guest_page(vcpu->kvm, vsie_page->riccbd_gpa, hpa); 688 vsie_page->riccbd_gpa = 0; 689 scb_s->riccbd = 0; 690 } 691 692 hpa = scb_s->sdnxo; 693 if (hpa) { 694 unpin_guest_page(vcpu->kvm, vsie_page->sdnx_gpa, hpa); 695 vsie_page->sdnx_gpa = 0; 696 scb_s->sdnxo = 0; 697 } 698 } 699 700 /* 701 * Instead of shadowing some blocks, we can simply forward them because the 702 * addresses in the scb are 64 bit long. 703 * 704 * This works as long as the data lies in one page. If blocks ever exceed one 705 * page, we have to fall back to shadowing. 706 * 707 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must 708 * therefore not enable any facilities that access these pointers (e.g. SIGPIF). 709 * 710 * Returns: - 0 if all blocks were pinned. 711 * - > 0 if control has to be given to guest 2 712 * - -ENOMEM if out of memory 713 */ 714 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 715 { 716 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; 717 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 718 hpa_t hpa; 719 gpa_t gpa; 720 int rc = 0; 721 722 gpa = READ_ONCE(scb_o->scaol) & ~0xfUL; 723 if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO)) 724 gpa |= (u64) READ_ONCE(scb_o->scaoh) << 32; 725 if (gpa) { 726 if (gpa < 2 * PAGE_SIZE) 727 rc = set_validity_icpt(scb_s, 0x0038U); 728 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu)) 729 rc = set_validity_icpt(scb_s, 0x0011U); 730 else if ((gpa & PAGE_MASK) != 731 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK)) 732 rc = set_validity_icpt(scb_s, 0x003bU); 733 if (!rc) { 734 rc = pin_guest_page(vcpu->kvm, gpa, &hpa); 735 if (rc) 736 rc = set_validity_icpt(scb_s, 0x0034U); 737 } 738 if (rc) 739 goto unpin; 740 vsie_page->sca_gpa = gpa; 741 scb_s->scaoh = (u32)((u64)hpa >> 32); 742 scb_s->scaol = (u32)(u64)hpa; 743 } 744 745 gpa = READ_ONCE(scb_o->itdba) & ~0xffUL; 746 if (gpa && (scb_s->ecb & ECB_TE)) { 747 if (gpa < 2 * PAGE_SIZE) { 748 rc = set_validity_icpt(scb_s, 0x0080U); 749 goto unpin; 750 } 751 /* 256 bytes cannot cross page boundaries */ 752 rc = pin_guest_page(vcpu->kvm, gpa, &hpa); 753 if (rc) { 754 rc = set_validity_icpt(scb_s, 0x0080U); 755 goto unpin; 756 } 757 vsie_page->itdba_gpa = gpa; 758 scb_s->itdba = hpa; 759 } 760 761 gpa = READ_ONCE(scb_o->gvrd) & ~0x1ffUL; 762 if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) { 763 if (gpa < 2 * PAGE_SIZE) { 764 rc = set_validity_icpt(scb_s, 0x1310U); 765 goto unpin; 766 } 767 /* 768 * 512 bytes vector registers cannot cross page boundaries 769 * if this block gets bigger, we have to shadow it. 770 */ 771 rc = pin_guest_page(vcpu->kvm, gpa, &hpa); 772 if (rc) { 773 rc = set_validity_icpt(scb_s, 0x1310U); 774 goto unpin; 775 } 776 vsie_page->gvrd_gpa = gpa; 777 scb_s->gvrd = hpa; 778 } 779 780 gpa = READ_ONCE(scb_o->riccbd) & ~0x3fUL; 781 if (gpa && (scb_s->ecb3 & ECB3_RI)) { 782 if (gpa < 2 * PAGE_SIZE) { 783 rc = set_validity_icpt(scb_s, 0x0043U); 784 goto unpin; 785 } 786 /* 64 bytes cannot cross page boundaries */ 787 rc = pin_guest_page(vcpu->kvm, gpa, &hpa); 788 if (rc) { 789 rc = set_validity_icpt(scb_s, 0x0043U); 790 goto unpin; 791 } 792 /* Validity 0x0044 will be checked by SIE */ 793 vsie_page->riccbd_gpa = gpa; 794 scb_s->riccbd = hpa; 795 } 796 if (((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) || 797 (scb_s->ecd & ECD_ETOKENF)) { 798 unsigned long sdnxc; 799 800 gpa = READ_ONCE(scb_o->sdnxo) & ~0xfUL; 801 sdnxc = READ_ONCE(scb_o->sdnxo) & 0xfUL; 802 if (!gpa || gpa < 2 * PAGE_SIZE) { 803 rc = set_validity_icpt(scb_s, 0x10b0U); 804 goto unpin; 805 } 806 if (sdnxc < 6 || sdnxc > 12) { 807 rc = set_validity_icpt(scb_s, 0x10b1U); 808 goto unpin; 809 } 810 if (gpa & ((1 << sdnxc) - 1)) { 811 rc = set_validity_icpt(scb_s, 0x10b2U); 812 goto unpin; 813 } 814 /* Due to alignment rules (checked above) this cannot 815 * cross page boundaries 816 */ 817 rc = pin_guest_page(vcpu->kvm, gpa, &hpa); 818 if (rc) { 819 rc = set_validity_icpt(scb_s, 0x10b0U); 820 goto unpin; 821 } 822 vsie_page->sdnx_gpa = gpa; 823 scb_s->sdnxo = hpa | sdnxc; 824 } 825 return 0; 826 unpin: 827 unpin_blocks(vcpu, vsie_page); 828 return rc; 829 } 830 831 /* unpin the scb provided by guest 2, marking it as dirty */ 832 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, 833 gpa_t gpa) 834 { 835 hpa_t hpa = (hpa_t) vsie_page->scb_o; 836 837 if (hpa) 838 unpin_guest_page(vcpu->kvm, gpa, hpa); 839 vsie_page->scb_o = NULL; 840 } 841 842 /* 843 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o. 844 * 845 * Returns: - 0 if the scb was pinned. 846 * - > 0 if control has to be given to guest 2 847 */ 848 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, 849 gpa_t gpa) 850 { 851 hpa_t hpa; 852 int rc; 853 854 rc = pin_guest_page(vcpu->kvm, gpa, &hpa); 855 if (rc) { 856 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 857 WARN_ON_ONCE(rc); 858 return 1; 859 } 860 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa; 861 return 0; 862 } 863 864 /* 865 * Inject a fault into guest 2. 866 * 867 * Returns: - > 0 if control has to be given to guest 2 868 * < 0 if an error occurred during injection. 869 */ 870 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr, 871 bool write_flag) 872 { 873 struct kvm_s390_pgm_info pgm = { 874 .code = code, 875 .trans_exc_code = 876 /* 0-51: virtual address */ 877 (vaddr & 0xfffffffffffff000UL) | 878 /* 52-53: store / fetch */ 879 (((unsigned int) !write_flag) + 1) << 10, 880 /* 62-63: asce id (alway primary == 0) */ 881 .exc_access_id = 0, /* always primary */ 882 .op_access_id = 0, /* not MVPG */ 883 }; 884 int rc; 885 886 if (code == PGM_PROTECTION) 887 pgm.trans_exc_code |= 0x4UL; 888 889 rc = kvm_s390_inject_prog_irq(vcpu, &pgm); 890 return rc ? rc : 1; 891 } 892 893 /* 894 * Handle a fault during vsie execution on a gmap shadow. 895 * 896 * Returns: - 0 if the fault was resolved 897 * - > 0 if control has to be given to guest 2 898 * - < 0 if an error occurred 899 */ 900 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 901 { 902 int rc; 903 904 if (current->thread.gmap_int_code == PGM_PROTECTION) 905 /* we can directly forward all protection exceptions */ 906 return inject_fault(vcpu, PGM_PROTECTION, 907 current->thread.gmap_addr, 1); 908 909 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, 910 current->thread.gmap_addr); 911 if (rc > 0) { 912 rc = inject_fault(vcpu, rc, 913 current->thread.gmap_addr, 914 current->thread.gmap_write_flag); 915 if (rc >= 0) 916 vsie_page->fault_addr = current->thread.gmap_addr; 917 } 918 return rc; 919 } 920 921 /* 922 * Retry the previous fault that required guest 2 intervention. This avoids 923 * one superfluous SIE re-entry and direct exit. 924 * 925 * Will ignore any errors. The next SIE fault will do proper fault handling. 926 */ 927 static void handle_last_fault(struct kvm_vcpu *vcpu, 928 struct vsie_page *vsie_page) 929 { 930 if (vsie_page->fault_addr) 931 kvm_s390_shadow_fault(vcpu, vsie_page->gmap, 932 vsie_page->fault_addr); 933 vsie_page->fault_addr = 0; 934 } 935 936 static inline void clear_vsie_icpt(struct vsie_page *vsie_page) 937 { 938 vsie_page->scb_s.icptcode = 0; 939 } 940 941 /* rewind the psw and clear the vsie icpt, so we can retry execution */ 942 static void retry_vsie_icpt(struct vsie_page *vsie_page) 943 { 944 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 945 int ilen = insn_length(scb_s->ipa >> 8); 946 947 /* take care of EXECUTE instructions */ 948 if (scb_s->icptstatus & 1) { 949 ilen = (scb_s->icptstatus >> 4) & 0x6; 950 if (!ilen) 951 ilen = 4; 952 } 953 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen); 954 clear_vsie_icpt(vsie_page); 955 } 956 957 /* 958 * Try to shadow + enable the guest 2 provided facility list. 959 * Retry instruction execution if enabled for and provided by guest 2. 960 * 961 * Returns: - 0 if handled (retry or guest 2 icpt) 962 * - > 0 if control has to be given to guest 2 963 */ 964 static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 965 { 966 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 967 __u32 fac = READ_ONCE(vsie_page->scb_o->fac) & 0x7ffffff8U; 968 969 if (fac && test_kvm_facility(vcpu->kvm, 7)) { 970 retry_vsie_icpt(vsie_page); 971 if (read_guest_real(vcpu, fac, &vsie_page->fac, 972 sizeof(vsie_page->fac))) 973 return set_validity_icpt(scb_s, 0x1090U); 974 scb_s->fac = (__u32)(__u64) &vsie_page->fac; 975 } 976 return 0; 977 } 978 979 /* 980 * Run the vsie on a shadow scb and a shadow gmap, without any further 981 * sanity checks, handling SIE faults. 982 * 983 * Returns: - 0 everything went fine 984 * - > 0 if control has to be given to guest 2 985 * - < 0 if an error occurred 986 */ 987 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 988 __releases(vcpu->kvm->srcu) 989 __acquires(vcpu->kvm->srcu) 990 { 991 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 992 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; 993 int guest_bp_isolation; 994 int rc = 0; 995 996 handle_last_fault(vcpu, vsie_page); 997 998 if (need_resched()) 999 schedule(); 1000 if (test_cpu_flag(CIF_MCCK_PENDING)) 1001 s390_handle_mcck(); 1002 1003 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); 1004 1005 /* save current guest state of bp isolation override */ 1006 guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST); 1007 1008 /* 1009 * The guest is running with BPBC, so we have to force it on for our 1010 * nested guest. This is done by enabling BPBC globally, so the BPBC 1011 * control in the SCB (which the nested guest can modify) is simply 1012 * ignored. 1013 */ 1014 if (test_kvm_facility(vcpu->kvm, 82) && 1015 vcpu->arch.sie_block->fpf & FPF_BPBC) 1016 set_thread_flag(TIF_ISOLATE_BP_GUEST); 1017 1018 local_irq_disable(); 1019 guest_enter_irqoff(); 1020 local_irq_enable(); 1021 1022 /* 1023 * Simulate a SIE entry of the VCPU (see sie64a), so VCPU blocking 1024 * and VCPU requests also hinder the vSIE from running and lead 1025 * to an immediate exit. kvm_s390_vsie_kick() has to be used to 1026 * also kick the vSIE. 1027 */ 1028 vcpu->arch.sie_block->prog0c |= PROG_IN_SIE; 1029 barrier(); 1030 if (!kvm_s390_vcpu_sie_inhibited(vcpu)) 1031 rc = sie64a(scb_s, vcpu->run->s.regs.gprs); 1032 barrier(); 1033 vcpu->arch.sie_block->prog0c &= ~PROG_IN_SIE; 1034 1035 local_irq_disable(); 1036 guest_exit_irqoff(); 1037 local_irq_enable(); 1038 1039 /* restore guest state for bp isolation override */ 1040 if (!guest_bp_isolation) 1041 clear_thread_flag(TIF_ISOLATE_BP_GUEST); 1042 1043 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 1044 1045 if (rc == -EINTR) { 1046 VCPU_EVENT(vcpu, 3, "%s", "machine check"); 1047 kvm_s390_reinject_machine_check(vcpu, &vsie_page->mcck_info); 1048 return 0; 1049 } 1050 1051 if (rc > 0) 1052 rc = 0; /* we could still have an icpt */ 1053 else if (rc == -EFAULT) 1054 return handle_fault(vcpu, vsie_page); 1055 1056 switch (scb_s->icptcode) { 1057 case ICPT_INST: 1058 if (scb_s->ipa == 0xb2b0) 1059 rc = handle_stfle(vcpu, vsie_page); 1060 break; 1061 case ICPT_STOP: 1062 /* stop not requested by g2 - must have been a kick */ 1063 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT)) 1064 clear_vsie_icpt(vsie_page); 1065 break; 1066 case ICPT_VALIDITY: 1067 if ((scb_s->ipa & 0xf000) != 0xf000) 1068 scb_s->ipa += 0x1000; 1069 break; 1070 } 1071 return rc; 1072 } 1073 1074 static void release_gmap_shadow(struct vsie_page *vsie_page) 1075 { 1076 if (vsie_page->gmap) 1077 gmap_put(vsie_page->gmap); 1078 WRITE_ONCE(vsie_page->gmap, NULL); 1079 prefix_unmapped(vsie_page); 1080 } 1081 1082 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu, 1083 struct vsie_page *vsie_page) 1084 { 1085 unsigned long asce; 1086 union ctlreg0 cr0; 1087 struct gmap *gmap; 1088 int edat; 1089 1090 asce = vcpu->arch.sie_block->gcr[1]; 1091 cr0.val = vcpu->arch.sie_block->gcr[0]; 1092 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8); 1093 edat += edat && test_kvm_facility(vcpu->kvm, 78); 1094 1095 /* 1096 * ASCE or EDAT could have changed since last icpt, or the gmap 1097 * we're holding has been unshadowed. If the gmap is still valid, 1098 * we can safely reuse it. 1099 */ 1100 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat)) 1101 return 0; 1102 1103 /* release the old shadow - if any, and mark the prefix as unmapped */ 1104 release_gmap_shadow(vsie_page); 1105 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat); 1106 if (IS_ERR(gmap)) 1107 return PTR_ERR(gmap); 1108 gmap->private = vcpu->kvm; 1109 WRITE_ONCE(vsie_page->gmap, gmap); 1110 return 0; 1111 } 1112 1113 /* 1114 * Register the shadow scb at the VCPU, e.g. for kicking out of vsie. 1115 */ 1116 static void register_shadow_scb(struct kvm_vcpu *vcpu, 1117 struct vsie_page *vsie_page) 1118 { 1119 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 1120 1121 WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s); 1122 /* 1123 * External calls have to lead to a kick of the vcpu and 1124 * therefore the vsie -> Simulate Wait state. 1125 */ 1126 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT); 1127 /* 1128 * We have to adjust the g3 epoch by the g2 epoch. The epoch will 1129 * automatically be adjusted on tod clock changes via kvm_sync_clock. 1130 */ 1131 preempt_disable(); 1132 scb_s->epoch += vcpu->kvm->arch.epoch; 1133 1134 if (scb_s->ecd & ECD_MEF) { 1135 scb_s->epdx += vcpu->kvm->arch.epdx; 1136 if (scb_s->epoch < vcpu->kvm->arch.epoch) 1137 scb_s->epdx += 1; 1138 } 1139 1140 preempt_enable(); 1141 } 1142 1143 /* 1144 * Unregister a shadow scb from a VCPU. 1145 */ 1146 static void unregister_shadow_scb(struct kvm_vcpu *vcpu) 1147 { 1148 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT); 1149 WRITE_ONCE(vcpu->arch.vsie_block, NULL); 1150 } 1151 1152 /* 1153 * Run the vsie on a shadowed scb, managing the gmap shadow, handling 1154 * prefix pages and faults. 1155 * 1156 * Returns: - 0 if no errors occurred 1157 * - > 0 if control has to be given to guest 2 1158 * - -ENOMEM if out of memory 1159 */ 1160 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) 1161 { 1162 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; 1163 int rc = 0; 1164 1165 while (1) { 1166 rc = acquire_gmap_shadow(vcpu, vsie_page); 1167 if (!rc) 1168 rc = map_prefix(vcpu, vsie_page); 1169 if (!rc) { 1170 gmap_enable(vsie_page->gmap); 1171 update_intervention_requests(vsie_page); 1172 rc = do_vsie_run(vcpu, vsie_page); 1173 gmap_enable(vcpu->arch.gmap); 1174 } 1175 atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20); 1176 1177 if (rc == -EAGAIN) 1178 rc = 0; 1179 if (rc || scb_s->icptcode || signal_pending(current) || 1180 kvm_s390_vcpu_has_irq(vcpu, 0) || 1181 kvm_s390_vcpu_sie_inhibited(vcpu)) 1182 break; 1183 } 1184 1185 if (rc == -EFAULT) { 1186 /* 1187 * Addressing exceptions are always presentes as intercepts. 1188 * As addressing exceptions are suppressing and our guest 3 PSW 1189 * points at the responsible instruction, we have to 1190 * forward the PSW and set the ilc. If we can't read guest 3 1191 * instruction, we can use an arbitrary ilc. Let's always use 1192 * ilen = 4 for now, so we can avoid reading in guest 3 virtual 1193 * memory. (we could also fake the shadow so the hardware 1194 * handles it). 1195 */ 1196 scb_s->icptcode = ICPT_PROGI; 1197 scb_s->iprcc = PGM_ADDRESSING; 1198 scb_s->pgmilc = 4; 1199 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4); 1200 } 1201 return rc; 1202 } 1203 1204 /* 1205 * Get or create a vsie page for a scb address. 1206 * 1207 * Returns: - address of a vsie page (cached or new one) 1208 * - NULL if the same scb address is already used by another VCPU 1209 * - ERR_PTR(-ENOMEM) if out of memory 1210 */ 1211 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) 1212 { 1213 struct vsie_page *vsie_page; 1214 struct page *page; 1215 int nr_vcpus; 1216 1217 rcu_read_lock(); 1218 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); 1219 rcu_read_unlock(); 1220 if (page) { 1221 if (page_ref_inc_return(page) == 2) 1222 return page_to_virt(page); 1223 page_ref_dec(page); 1224 } 1225 1226 /* 1227 * We want at least #online_vcpus shadows, so every VCPU can execute 1228 * the VSIE in parallel. 1229 */ 1230 nr_vcpus = atomic_read(&kvm->online_vcpus); 1231 1232 mutex_lock(&kvm->arch.vsie.mutex); 1233 if (kvm->arch.vsie.page_count < nr_vcpus) { 1234 page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA); 1235 if (!page) { 1236 mutex_unlock(&kvm->arch.vsie.mutex); 1237 return ERR_PTR(-ENOMEM); 1238 } 1239 page_ref_inc(page); 1240 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page; 1241 kvm->arch.vsie.page_count++; 1242 } else { 1243 /* reuse an existing entry that belongs to nobody */ 1244 while (true) { 1245 page = kvm->arch.vsie.pages[kvm->arch.vsie.next]; 1246 if (page_ref_inc_return(page) == 2) 1247 break; 1248 page_ref_dec(page); 1249 kvm->arch.vsie.next++; 1250 kvm->arch.vsie.next %= nr_vcpus; 1251 } 1252 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); 1253 } 1254 page->index = addr; 1255 /* double use of the same address */ 1256 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) { 1257 page_ref_dec(page); 1258 mutex_unlock(&kvm->arch.vsie.mutex); 1259 return NULL; 1260 } 1261 mutex_unlock(&kvm->arch.vsie.mutex); 1262 1263 vsie_page = page_to_virt(page); 1264 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block)); 1265 release_gmap_shadow(vsie_page); 1266 vsie_page->fault_addr = 0; 1267 vsie_page->scb_s.ihcpu = 0xffffU; 1268 return vsie_page; 1269 } 1270 1271 /* put a vsie page acquired via get_vsie_page */ 1272 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page) 1273 { 1274 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT); 1275 1276 page_ref_dec(page); 1277 } 1278 1279 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu) 1280 { 1281 struct vsie_page *vsie_page; 1282 unsigned long scb_addr; 1283 int rc; 1284 1285 vcpu->stat.instruction_sie++; 1286 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2)) 1287 return -EOPNOTSUPP; 1288 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1289 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1290 1291 BUILD_BUG_ON(sizeof(struct vsie_page) != PAGE_SIZE); 1292 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL); 1293 1294 /* 512 byte alignment */ 1295 if (unlikely(scb_addr & 0x1ffUL)) 1296 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1297 1298 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0) || 1299 kvm_s390_vcpu_sie_inhibited(vcpu)) 1300 return 0; 1301 1302 vsie_page = get_vsie_page(vcpu->kvm, scb_addr); 1303 if (IS_ERR(vsie_page)) 1304 return PTR_ERR(vsie_page); 1305 else if (!vsie_page) 1306 /* double use of sie control block - simply do nothing */ 1307 return 0; 1308 1309 rc = pin_scb(vcpu, vsie_page, scb_addr); 1310 if (rc) 1311 goto out_put; 1312 rc = shadow_scb(vcpu, vsie_page); 1313 if (rc) 1314 goto out_unpin_scb; 1315 rc = pin_blocks(vcpu, vsie_page); 1316 if (rc) 1317 goto out_unshadow; 1318 register_shadow_scb(vcpu, vsie_page); 1319 rc = vsie_run(vcpu, vsie_page); 1320 unregister_shadow_scb(vcpu); 1321 unpin_blocks(vcpu, vsie_page); 1322 out_unshadow: 1323 unshadow_scb(vcpu, vsie_page); 1324 out_unpin_scb: 1325 unpin_scb(vcpu, vsie_page, scb_addr); 1326 out_put: 1327 put_vsie_page(vcpu->kvm, vsie_page); 1328 1329 return rc < 0 ? rc : 0; 1330 } 1331 1332 /* Init the vsie data structures. To be called when a vm is initialized. */ 1333 void kvm_s390_vsie_init(struct kvm *kvm) 1334 { 1335 mutex_init(&kvm->arch.vsie.mutex); 1336 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL); 1337 } 1338 1339 /* Destroy the vsie data structures. To be called when a vm is destroyed. */ 1340 void kvm_s390_vsie_destroy(struct kvm *kvm) 1341 { 1342 struct vsie_page *vsie_page; 1343 struct page *page; 1344 int i; 1345 1346 mutex_lock(&kvm->arch.vsie.mutex); 1347 for (i = 0; i < kvm->arch.vsie.page_count; i++) { 1348 page = kvm->arch.vsie.pages[i]; 1349 kvm->arch.vsie.pages[i] = NULL; 1350 vsie_page = page_to_virt(page); 1351 release_gmap_shadow(vsie_page); 1352 /* free the radix tree entry */ 1353 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); 1354 __free_page(page); 1355 } 1356 kvm->arch.vsie.page_count = 0; 1357 mutex_unlock(&kvm->arch.vsie.mutex); 1358 } 1359 1360 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu) 1361 { 1362 struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block); 1363 1364 /* 1365 * Even if the VCPU lets go of the shadow sie block reference, it is 1366 * still valid in the cache. So we can safely kick it. 1367 */ 1368 if (scb) { 1369 atomic_or(PROG_BLOCK_SIE, &scb->prog20); 1370 if (scb->prog0c & PROG_IN_SIE) 1371 atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags); 1372 } 1373 } 1374