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