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