David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * kvm nested virtualization support for s390x |
| 3 | * |
| 4 | * Copyright IBM Corp. 2016 |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License (version 2 only) |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com> |
| 11 | */ |
| 12 | #include <linux/vmalloc.h> |
| 13 | #include <linux/kvm_host.h> |
| 14 | #include <linux/bug.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/bitmap.h> |
| 17 | #include <asm/gmap.h> |
| 18 | #include <asm/mmu_context.h> |
| 19 | #include <asm/sclp.h> |
| 20 | #include <asm/nmi.h> |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame^] | 21 | #include <asm/dis.h> |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 22 | #include "kvm-s390.h" |
| 23 | #include "gaccess.h" |
| 24 | |
| 25 | struct vsie_page { |
| 26 | struct kvm_s390_sie_block scb_s; /* 0x0000 */ |
| 27 | /* the pinned originial scb */ |
| 28 | struct kvm_s390_sie_block *scb_o; /* 0x0200 */ |
| 29 | /* the shadow gmap in use by the vsie_page */ |
| 30 | struct gmap *gmap; /* 0x0208 */ |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame^] | 31 | __u8 reserved[0x0800 - 0x0210]; /* 0x0210 */ |
| 32 | __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */ |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 33 | } __packed; |
| 34 | |
| 35 | /* trigger a validity icpt for the given scb */ |
| 36 | static int set_validity_icpt(struct kvm_s390_sie_block *scb, |
| 37 | __u16 reason_code) |
| 38 | { |
| 39 | scb->ipa = 0x1000; |
| 40 | scb->ipb = ((__u32) reason_code) << 16; |
| 41 | scb->icptcode = ICPT_VALIDITY; |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | /* mark the prefix as unmapped, this will block the VSIE */ |
| 46 | static void prefix_unmapped(struct vsie_page *vsie_page) |
| 47 | { |
| 48 | atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20); |
| 49 | } |
| 50 | |
| 51 | /* mark the prefix as unmapped and wait until the VSIE has been left */ |
| 52 | static void prefix_unmapped_sync(struct vsie_page *vsie_page) |
| 53 | { |
| 54 | prefix_unmapped(vsie_page); |
| 55 | if (vsie_page->scb_s.prog0c & PROG_IN_SIE) |
| 56 | atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags); |
| 57 | while (vsie_page->scb_s.prog0c & PROG_IN_SIE) |
| 58 | cpu_relax(); |
| 59 | } |
| 60 | |
| 61 | /* mark the prefix as mapped, this will allow the VSIE to run */ |
| 62 | static void prefix_mapped(struct vsie_page *vsie_page) |
| 63 | { |
| 64 | atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20); |
| 65 | } |
| 66 | |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 67 | /* test if the prefix is mapped into the gmap shadow */ |
| 68 | static int prefix_is_mapped(struct vsie_page *vsie_page) |
| 69 | { |
| 70 | return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST); |
| 71 | } |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 72 | |
| 73 | /* copy the updated intervention request bits into the shadow scb */ |
| 74 | static void update_intervention_requests(struct vsie_page *vsie_page) |
| 75 | { |
| 76 | const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT; |
| 77 | int cpuflags; |
| 78 | |
| 79 | cpuflags = atomic_read(&vsie_page->scb_o->cpuflags); |
| 80 | atomic_andnot(bits, &vsie_page->scb_s.cpuflags); |
| 81 | atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags); |
| 82 | } |
| 83 | |
| 84 | /* shadow (filter and validate) the cpuflags */ |
| 85 | static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 86 | { |
| 87 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 88 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 89 | int newflags, cpuflags = atomic_read(&scb_o->cpuflags); |
| 90 | |
| 91 | /* we don't allow ESA/390 guests */ |
| 92 | if (!(cpuflags & CPUSTAT_ZARCH)) |
| 93 | return set_validity_icpt(scb_s, 0x0001U); |
| 94 | |
| 95 | if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS)) |
| 96 | return set_validity_icpt(scb_s, 0x0001U); |
| 97 | else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR)) |
| 98 | return set_validity_icpt(scb_s, 0x0007U); |
| 99 | |
| 100 | /* intervention requests will be set later */ |
| 101 | newflags = CPUSTAT_ZARCH; |
David Hildenbrand | 535ef81 | 2016-02-12 12:24:20 +0100 | [diff] [blame] | 102 | if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8)) |
| 103 | newflags |= CPUSTAT_GED; |
| 104 | if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) { |
| 105 | if (cpuflags & CPUSTAT_GED) |
| 106 | return set_validity_icpt(scb_s, 0x0001U); |
| 107 | newflags |= CPUSTAT_GED2; |
| 108 | } |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 109 | |
| 110 | atomic_set(&scb_s->cpuflags, newflags); |
| 111 | return 0; |
| 112 | } |
| 113 | |
David Hildenbrand | 3573602 | 2016-02-19 10:11:24 +0100 | [diff] [blame] | 114 | /* shadow (round up/down) the ibc to avoid validity icpt */ |
| 115 | static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 116 | { |
| 117 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 118 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 119 | __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU; |
| 120 | |
| 121 | scb_s->ibc = 0; |
| 122 | /* ibc installed in g2 and requested for g3 */ |
| 123 | if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) { |
| 124 | scb_s->ibc = scb_o->ibc & 0x0fffU; |
| 125 | /* takte care of the minimum ibc level of the machine */ |
| 126 | if (scb_s->ibc < min_ibc) |
| 127 | scb_s->ibc = min_ibc; |
| 128 | /* take care of the maximum ibc level set for the guest */ |
| 129 | if (scb_s->ibc > vcpu->kvm->arch.model.ibc) |
| 130 | scb_s->ibc = vcpu->kvm->arch.model.ibc; |
| 131 | } |
| 132 | } |
| 133 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 134 | /* unshadow the scb, copying parameters back to the real scb */ |
| 135 | static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 136 | { |
| 137 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 138 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 139 | |
| 140 | /* interception */ |
| 141 | scb_o->icptcode = scb_s->icptcode; |
| 142 | scb_o->icptstatus = scb_s->icptstatus; |
| 143 | scb_o->ipa = scb_s->ipa; |
| 144 | scb_o->ipb = scb_s->ipb; |
| 145 | scb_o->gbea = scb_s->gbea; |
| 146 | |
| 147 | /* timer */ |
| 148 | scb_o->cputm = scb_s->cputm; |
| 149 | scb_o->ckc = scb_s->ckc; |
| 150 | scb_o->todpr = scb_s->todpr; |
| 151 | |
| 152 | /* guest state */ |
| 153 | scb_o->gpsw = scb_s->gpsw; |
| 154 | scb_o->gg14 = scb_s->gg14; |
| 155 | scb_o->gg15 = scb_s->gg15; |
| 156 | memcpy(scb_o->gcr, scb_s->gcr, 128); |
| 157 | scb_o->pp = scb_s->pp; |
| 158 | |
| 159 | /* interrupt intercept */ |
| 160 | switch (scb_s->icptcode) { |
| 161 | case ICPT_PROGI: |
| 162 | case ICPT_INSTPROGI: |
| 163 | case ICPT_EXTINT: |
| 164 | memcpy((void *)((u64)scb_o + 0xc0), |
| 165 | (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0); |
| 166 | break; |
| 167 | case ICPT_PARTEXEC: |
| 168 | /* MVPG only */ |
| 169 | memcpy((void *)((u64)scb_o + 0xc0), |
| 170 | (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0); |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | if (scb_s->ihcpu != 0xffffU) |
| 175 | scb_o->ihcpu = scb_s->ihcpu; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Setup the shadow scb by copying and checking the relevant parts of the g2 |
| 180 | * provided scb. |
| 181 | * |
| 182 | * Returns: - 0 if the scb has been shadowed |
| 183 | * - > 0 if control has to be given to guest 2 |
| 184 | */ |
| 185 | static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 186 | { |
| 187 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 188 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 189 | unsigned long new_mso; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 190 | int rc; |
| 191 | |
| 192 | /* make sure we don't have any leftovers when reusing the scb */ |
| 193 | scb_s->icptcode = 0; |
| 194 | scb_s->eca = 0; |
| 195 | scb_s->ecb = 0; |
| 196 | scb_s->ecb2 = 0; |
| 197 | scb_s->ecb3 = 0; |
| 198 | scb_s->ecd = 0; |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame^] | 199 | scb_s->fac = 0; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 200 | |
| 201 | rc = prepare_cpuflags(vcpu, vsie_page); |
| 202 | if (rc) |
| 203 | goto out; |
| 204 | |
| 205 | /* timer */ |
| 206 | scb_s->cputm = scb_o->cputm; |
| 207 | scb_s->ckc = scb_o->ckc; |
| 208 | scb_s->todpr = scb_o->todpr; |
| 209 | scb_s->epoch = scb_o->epoch; |
| 210 | |
| 211 | /* guest state */ |
| 212 | scb_s->gpsw = scb_o->gpsw; |
| 213 | scb_s->gg14 = scb_o->gg14; |
| 214 | scb_s->gg15 = scb_o->gg15; |
| 215 | memcpy(scb_s->gcr, scb_o->gcr, 128); |
| 216 | scb_s->pp = scb_o->pp; |
| 217 | |
| 218 | /* interception / execution handling */ |
| 219 | scb_s->gbea = scb_o->gbea; |
| 220 | scb_s->lctl = scb_o->lctl; |
| 221 | scb_s->svcc = scb_o->svcc; |
| 222 | scb_s->ictl = scb_o->ictl; |
| 223 | /* |
| 224 | * SKEY handling functions can't deal with false setting of PTE invalid |
| 225 | * bits. Therefore we cannot provide interpretation and would later |
| 226 | * have to provide own emulation handlers. |
| 227 | */ |
| 228 | scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; |
| 229 | scb_s->icpua = scb_o->icpua; |
| 230 | |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 231 | new_mso = scb_o->mso & 0xfffffffffff00000UL; |
| 232 | /* if the hva of the prefix changes, we have to remap the prefix */ |
| 233 | if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix) |
| 234 | prefix_unmapped(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 235 | /* SIE will do mso/msl validity and exception checks for us */ |
| 236 | scb_s->msl = scb_o->msl & 0xfffffffffff00000UL; |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 237 | scb_s->mso = new_mso; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 238 | scb_s->prefix = scb_o->prefix; |
| 239 | |
| 240 | /* We have to definetly flush the tlb if this scb never ran */ |
| 241 | if (scb_s->ihcpu != 0xffffU) |
| 242 | scb_s->ihcpu = scb_o->ihcpu; |
| 243 | |
| 244 | /* MVPG and Protection Exception Interpretation are always available */ |
| 245 | scb_s->eca |= scb_o->eca & 0x01002000U; |
David Hildenbrand | 4ceafa9 | 2015-11-27 12:34:28 +0100 | [diff] [blame] | 246 | /* Host-protection-interruption introduced with ESOP */ |
| 247 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP)) |
| 248 | scb_s->ecb |= scb_o->ecb & 0x02U; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 249 | |
David Hildenbrand | 3573602 | 2016-02-19 10:11:24 +0100 | [diff] [blame] | 250 | prepare_ibc(vcpu, vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 251 | out: |
| 252 | if (rc) |
| 253 | unshadow_scb(vcpu, vsie_page); |
| 254 | return rc; |
| 255 | } |
| 256 | |
| 257 | void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start, |
| 258 | unsigned long end) |
| 259 | { |
| 260 | struct kvm *kvm = gmap->private; |
| 261 | struct vsie_page *cur; |
| 262 | unsigned long prefix; |
| 263 | struct page *page; |
| 264 | int i; |
| 265 | |
| 266 | if (!gmap_is_shadow(gmap)) |
| 267 | return; |
| 268 | if (start >= 1UL << 31) |
| 269 | /* We are only interested in prefix pages */ |
| 270 | return; |
| 271 | |
| 272 | /* |
| 273 | * Only new shadow blocks are added to the list during runtime, |
| 274 | * therefore we can safely reference them all the time. |
| 275 | */ |
| 276 | for (i = 0; i < kvm->arch.vsie.page_count; i++) { |
| 277 | page = READ_ONCE(kvm->arch.vsie.pages[i]); |
| 278 | if (!page) |
| 279 | continue; |
| 280 | cur = page_to_virt(page); |
| 281 | if (READ_ONCE(cur->gmap) != gmap) |
| 282 | continue; |
| 283 | prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT; |
| 284 | /* with mso/msl, the prefix lies at an offset */ |
| 285 | prefix += cur->scb_s.mso; |
| 286 | if (prefix <= end && start <= prefix + PAGE_SIZE - 1) |
| 287 | prefix_unmapped_sync(cur); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Map the first prefix page. |
| 293 | * |
| 294 | * The prefix will be protected, a gmap notifier will inform about unmaps. |
| 295 | * The shadow scb must not be executed until the prefix is remapped, this is |
| 296 | * guaranteed by properly handling PROG_REQUEST. |
| 297 | * |
| 298 | * Returns: - 0 on if successfully mapped or already mapped |
| 299 | * - > 0 if control has to be given to guest 2 |
| 300 | * - -EAGAIN if the caller can retry immediately |
| 301 | * - -ENOMEM if out of memory |
| 302 | */ |
| 303 | static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 304 | { |
| 305 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 306 | u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT; |
| 307 | int rc; |
| 308 | |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 309 | if (prefix_is_mapped(vsie_page)) |
| 310 | return 0; |
| 311 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 312 | /* mark it as mapped so we can catch any concurrent unmappers */ |
| 313 | prefix_mapped(vsie_page); |
| 314 | |
| 315 | /* with mso/msl, the prefix lies at offset *mso* */ |
| 316 | prefix += scb_s->mso; |
| 317 | |
| 318 | rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix); |
| 319 | /* |
| 320 | * We don't have to mprotect, we will be called for all unshadows. |
| 321 | * SIE will detect if protection applies and trigger a validity. |
| 322 | */ |
| 323 | if (rc) |
| 324 | prefix_unmapped(vsie_page); |
| 325 | if (rc > 0 || rc == -EFAULT) |
| 326 | rc = set_validity_icpt(scb_s, 0x0037U); |
| 327 | return rc; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Pin the guest page given by gpa and set hpa to the pinned host address. |
| 332 | * Will always be pinned writable. |
| 333 | * |
| 334 | * Returns: - 0 on success |
| 335 | * - -EINVAL if the gpa is not valid guest storage |
| 336 | * - -ENOMEM if out of memory |
| 337 | */ |
| 338 | static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa) |
| 339 | { |
| 340 | struct page *page; |
| 341 | hva_t hva; |
| 342 | int rc; |
| 343 | |
| 344 | hva = gfn_to_hva(kvm, gpa_to_gfn(gpa)); |
| 345 | if (kvm_is_error_hva(hva)) |
| 346 | return -EINVAL; |
| 347 | rc = get_user_pages_fast(hva, 1, 1, &page); |
| 348 | if (rc < 0) |
| 349 | return rc; |
| 350 | else if (rc != 1) |
| 351 | return -ENOMEM; |
| 352 | *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */ |
| 357 | static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa) |
| 358 | { |
| 359 | struct page *page; |
| 360 | |
| 361 | page = virt_to_page(hpa); |
| 362 | set_page_dirty_lock(page); |
| 363 | put_page(page); |
| 364 | /* mark the page always as dirty for migration */ |
| 365 | mark_page_dirty(kvm, gpa_to_gfn(gpa)); |
| 366 | } |
| 367 | |
| 368 | /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */ |
| 369 | static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 370 | { |
| 371 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 372 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 373 | hpa_t hpa; |
| 374 | gpa_t gpa; |
| 375 | |
| 376 | hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol; |
| 377 | if (hpa) { |
| 378 | gpa = scb_o->scaol & ~0xfUL; |
| 379 | unpin_guest_page(vcpu->kvm, gpa, hpa); |
| 380 | scb_s->scaol = 0; |
| 381 | scb_s->scaoh = 0; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Instead of shadowing some blocks, we can simply forward them because the |
| 387 | * addresses in the scb are 64 bit long. |
| 388 | * |
| 389 | * This works as long as the data lies in one page. If blocks ever exceed one |
| 390 | * page, we have to fall back to shadowing. |
| 391 | * |
| 392 | * As we reuse the sca, the vcpu pointers contained in it are invalid. We must |
| 393 | * therefore not enable any facilities that access these pointers (e.g. SIGPIF). |
| 394 | * |
| 395 | * Returns: - 0 if all blocks were pinned. |
| 396 | * - > 0 if control has to be given to guest 2 |
| 397 | * - -ENOMEM if out of memory |
| 398 | */ |
| 399 | static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 400 | { |
| 401 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 402 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 403 | hpa_t hpa; |
| 404 | gpa_t gpa; |
| 405 | int rc = 0; |
| 406 | |
| 407 | gpa = scb_o->scaol & ~0xfUL; |
| 408 | if (gpa) { |
| 409 | if (!(gpa & ~0x1fffUL)) |
| 410 | rc = set_validity_icpt(scb_s, 0x0038U); |
| 411 | else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu)) |
| 412 | rc = set_validity_icpt(scb_s, 0x0011U); |
| 413 | else if ((gpa & PAGE_MASK) != |
| 414 | ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK)) |
| 415 | rc = set_validity_icpt(scb_s, 0x003bU); |
| 416 | if (!rc) { |
| 417 | rc = pin_guest_page(vcpu->kvm, gpa, &hpa); |
| 418 | if (rc == -EINVAL) |
| 419 | rc = set_validity_icpt(scb_s, 0x0034U); |
| 420 | } |
| 421 | if (rc) |
| 422 | goto unpin; |
| 423 | scb_s->scaoh = (u32)((u64)hpa >> 32); |
| 424 | scb_s->scaol = (u32)(u64)hpa; |
| 425 | } |
| 426 | return 0; |
| 427 | unpin: |
| 428 | unpin_blocks(vcpu, vsie_page); |
| 429 | return rc; |
| 430 | } |
| 431 | |
| 432 | /* unpin the scb provided by guest 2, marking it as dirty */ |
| 433 | static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, |
| 434 | gpa_t gpa) |
| 435 | { |
| 436 | hpa_t hpa = (hpa_t) vsie_page->scb_o; |
| 437 | |
| 438 | if (hpa) |
| 439 | unpin_guest_page(vcpu->kvm, gpa, hpa); |
| 440 | vsie_page->scb_o = NULL; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o. |
| 445 | * |
| 446 | * Returns: - 0 if the scb was pinned. |
| 447 | * - > 0 if control has to be given to guest 2 |
| 448 | * - -ENOMEM if out of memory |
| 449 | */ |
| 450 | static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, |
| 451 | gpa_t gpa) |
| 452 | { |
| 453 | hpa_t hpa; |
| 454 | int rc; |
| 455 | |
| 456 | rc = pin_guest_page(vcpu->kvm, gpa, &hpa); |
| 457 | if (rc == -EINVAL) { |
| 458 | rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 459 | if (!rc) |
| 460 | rc = 1; |
| 461 | } |
| 462 | if (!rc) |
| 463 | vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa; |
| 464 | return rc; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Inject a fault into guest 2. |
| 469 | * |
| 470 | * Returns: - > 0 if control has to be given to guest 2 |
| 471 | * < 0 if an error occurred during injection. |
| 472 | */ |
| 473 | static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr, |
| 474 | bool write_flag) |
| 475 | { |
| 476 | struct kvm_s390_pgm_info pgm = { |
| 477 | .code = code, |
| 478 | .trans_exc_code = |
| 479 | /* 0-51: virtual address */ |
| 480 | (vaddr & 0xfffffffffffff000UL) | |
| 481 | /* 52-53: store / fetch */ |
| 482 | (((unsigned int) !write_flag) + 1) << 10, |
| 483 | /* 62-63: asce id (alway primary == 0) */ |
| 484 | .exc_access_id = 0, /* always primary */ |
| 485 | .op_access_id = 0, /* not MVPG */ |
| 486 | }; |
| 487 | int rc; |
| 488 | |
| 489 | if (code == PGM_PROTECTION) |
| 490 | pgm.trans_exc_code |= 0x4UL; |
| 491 | |
| 492 | rc = kvm_s390_inject_prog_irq(vcpu, &pgm); |
| 493 | return rc ? rc : 1; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Handle a fault during vsie execution on a gmap shadow. |
| 498 | * |
| 499 | * Returns: - 0 if the fault was resolved |
| 500 | * - > 0 if control has to be given to guest 2 |
| 501 | * - < 0 if an error occurred |
| 502 | */ |
| 503 | static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 504 | { |
| 505 | int rc; |
| 506 | |
| 507 | if (current->thread.gmap_int_code == PGM_PROTECTION) |
| 508 | /* we can directly forward all protection exceptions */ |
| 509 | return inject_fault(vcpu, PGM_PROTECTION, |
| 510 | current->thread.gmap_addr, 1); |
| 511 | |
| 512 | rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, |
| 513 | current->thread.gmap_addr); |
| 514 | if (rc > 0) { |
| 515 | rc = inject_fault(vcpu, rc, |
| 516 | current->thread.gmap_addr, |
| 517 | current->thread.gmap_write_flag); |
| 518 | } |
| 519 | return rc; |
| 520 | } |
| 521 | |
| 522 | static inline void clear_vsie_icpt(struct vsie_page *vsie_page) |
| 523 | { |
| 524 | vsie_page->scb_s.icptcode = 0; |
| 525 | } |
| 526 | |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame^] | 527 | /* rewind the psw and clear the vsie icpt, so we can retry execution */ |
| 528 | static void retry_vsie_icpt(struct vsie_page *vsie_page) |
| 529 | { |
| 530 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 531 | int ilen = insn_length(scb_s->ipa >> 8); |
| 532 | |
| 533 | /* take care of EXECUTE instructions */ |
| 534 | if (scb_s->icptstatus & 1) { |
| 535 | ilen = (scb_s->icptstatus >> 4) & 0x6; |
| 536 | if (!ilen) |
| 537 | ilen = 4; |
| 538 | } |
| 539 | scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen); |
| 540 | clear_vsie_icpt(vsie_page); |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * Try to shadow + enable the guest 2 provided facility list. |
| 545 | * Retry instruction execution if enabled for and provided by guest 2. |
| 546 | * |
| 547 | * Returns: - 0 if handled (retry or guest 2 icpt) |
| 548 | * - > 0 if control has to be given to guest 2 |
| 549 | */ |
| 550 | static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 551 | { |
| 552 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 553 | __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U; |
| 554 | |
| 555 | if (fac && test_kvm_facility(vcpu->kvm, 7)) { |
| 556 | retry_vsie_icpt(vsie_page); |
| 557 | if (read_guest_real(vcpu, fac, &vsie_page->fac, |
| 558 | sizeof(vsie_page->fac))) |
| 559 | return set_validity_icpt(scb_s, 0x1090U); |
| 560 | scb_s->fac = (__u32)(__u64) &vsie_page->fac; |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 565 | /* |
| 566 | * Run the vsie on a shadow scb and a shadow gmap, without any further |
| 567 | * sanity checks, handling SIE faults. |
| 568 | * |
| 569 | * Returns: - 0 everything went fine |
| 570 | * - > 0 if control has to be given to guest 2 |
| 571 | * - < 0 if an error occurred |
| 572 | */ |
| 573 | static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 574 | { |
| 575 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 576 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 577 | int rc; |
| 578 | |
| 579 | if (need_resched()) |
| 580 | schedule(); |
| 581 | if (test_cpu_flag(CIF_MCCK_PENDING)) |
| 582 | s390_handle_mcck(); |
| 583 | |
| 584 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 585 | local_irq_disable(); |
| 586 | kvm_guest_enter(); |
| 587 | local_irq_enable(); |
| 588 | |
| 589 | rc = sie64a(scb_s, vcpu->run->s.regs.gprs); |
| 590 | |
| 591 | local_irq_disable(); |
| 592 | kvm_guest_exit(); |
| 593 | local_irq_enable(); |
| 594 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 595 | |
| 596 | if (rc > 0) |
| 597 | rc = 0; /* we could still have an icpt */ |
| 598 | else if (rc == -EFAULT) |
| 599 | return handle_fault(vcpu, vsie_page); |
| 600 | |
| 601 | switch (scb_s->icptcode) { |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame^] | 602 | case ICPT_INST: |
| 603 | if (scb_s->ipa == 0xb2b0) |
| 604 | rc = handle_stfle(vcpu, vsie_page); |
| 605 | break; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 606 | case ICPT_STOP: |
| 607 | /* stop not requested by g2 - must have been a kick */ |
| 608 | if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT)) |
| 609 | clear_vsie_icpt(vsie_page); |
| 610 | break; |
| 611 | case ICPT_VALIDITY: |
| 612 | if ((scb_s->ipa & 0xf000) != 0xf000) |
| 613 | scb_s->ipa += 0x1000; |
| 614 | break; |
| 615 | } |
| 616 | return rc; |
| 617 | } |
| 618 | |
| 619 | static void release_gmap_shadow(struct vsie_page *vsie_page) |
| 620 | { |
| 621 | if (vsie_page->gmap) |
| 622 | gmap_put(vsie_page->gmap); |
| 623 | WRITE_ONCE(vsie_page->gmap, NULL); |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 624 | prefix_unmapped(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static int acquire_gmap_shadow(struct kvm_vcpu *vcpu, |
| 628 | struct vsie_page *vsie_page) |
| 629 | { |
| 630 | unsigned long asce; |
| 631 | union ctlreg0 cr0; |
| 632 | struct gmap *gmap; |
| 633 | int edat; |
| 634 | |
| 635 | asce = vcpu->arch.sie_block->gcr[1]; |
| 636 | cr0.val = vcpu->arch.sie_block->gcr[0]; |
| 637 | edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8); |
| 638 | edat += edat && test_kvm_facility(vcpu->kvm, 78); |
| 639 | |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 640 | /* |
| 641 | * ASCE or EDAT could have changed since last icpt, or the gmap |
| 642 | * we're holding has been unshadowed. If the gmap is still valid, |
| 643 | * we can safely reuse it. |
| 644 | */ |
| 645 | if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat)) |
| 646 | return 0; |
| 647 | |
| 648 | /* release the old shadow - if any, and mark the prefix as unmapped */ |
| 649 | release_gmap_shadow(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 650 | gmap = gmap_shadow(vcpu->arch.gmap, asce, edat); |
| 651 | if (IS_ERR(gmap)) |
| 652 | return PTR_ERR(gmap); |
| 653 | gmap->private = vcpu->kvm; |
| 654 | WRITE_ONCE(vsie_page->gmap, gmap); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Run the vsie on a shadowed scb, managing the gmap shadow, handling |
| 660 | * prefix pages and faults. |
| 661 | * |
| 662 | * Returns: - 0 if no errors occurred |
| 663 | * - > 0 if control has to be given to guest 2 |
| 664 | * - -ENOMEM if out of memory |
| 665 | */ |
| 666 | static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 667 | { |
| 668 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 669 | int rc = 0; |
| 670 | |
| 671 | while (1) { |
| 672 | rc = acquire_gmap_shadow(vcpu, vsie_page); |
| 673 | if (!rc) |
| 674 | rc = map_prefix(vcpu, vsie_page); |
| 675 | if (!rc) { |
| 676 | gmap_enable(vsie_page->gmap); |
| 677 | update_intervention_requests(vsie_page); |
| 678 | rc = do_vsie_run(vcpu, vsie_page); |
| 679 | gmap_enable(vcpu->arch.gmap); |
| 680 | } |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 681 | |
| 682 | if (rc == -EAGAIN) |
| 683 | rc = 0; |
| 684 | if (rc || scb_s->icptcode || signal_pending(current) || |
| 685 | kvm_s390_vcpu_has_irq(vcpu, 0)) |
| 686 | break; |
| 687 | }; |
| 688 | |
| 689 | if (rc == -EFAULT) { |
| 690 | /* |
| 691 | * Addressing exceptions are always presentes as intercepts. |
| 692 | * As addressing exceptions are suppressing and our guest 3 PSW |
| 693 | * points at the responsible instruction, we have to |
| 694 | * forward the PSW and set the ilc. If we can't read guest 3 |
| 695 | * instruction, we can use an arbitrary ilc. Let's always use |
| 696 | * ilen = 4 for now, so we can avoid reading in guest 3 virtual |
| 697 | * memory. (we could also fake the shadow so the hardware |
| 698 | * handles it). |
| 699 | */ |
| 700 | scb_s->icptcode = ICPT_PROGI; |
| 701 | scb_s->iprcc = PGM_ADDRESSING; |
| 702 | scb_s->pgmilc = 4; |
| 703 | scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4); |
| 704 | } |
| 705 | return rc; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * Get or create a vsie page for a scb address. |
| 710 | * |
| 711 | * Returns: - address of a vsie page (cached or new one) |
| 712 | * - NULL if the same scb address is already used by another VCPU |
| 713 | * - ERR_PTR(-ENOMEM) if out of memory |
| 714 | */ |
| 715 | static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) |
| 716 | { |
| 717 | struct vsie_page *vsie_page; |
| 718 | struct page *page; |
| 719 | int nr_vcpus; |
| 720 | |
| 721 | rcu_read_lock(); |
| 722 | page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); |
| 723 | rcu_read_unlock(); |
| 724 | if (page) { |
| 725 | if (page_ref_inc_return(page) == 2) |
| 726 | return page_to_virt(page); |
| 727 | page_ref_dec(page); |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * We want at least #online_vcpus shadows, so every VCPU can execute |
| 732 | * the VSIE in parallel. |
| 733 | */ |
| 734 | nr_vcpus = atomic_read(&kvm->online_vcpus); |
| 735 | |
| 736 | mutex_lock(&kvm->arch.vsie.mutex); |
| 737 | if (kvm->arch.vsie.page_count < nr_vcpus) { |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame^] | 738 | page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 739 | if (!page) { |
| 740 | mutex_unlock(&kvm->arch.vsie.mutex); |
| 741 | return ERR_PTR(-ENOMEM); |
| 742 | } |
| 743 | page_ref_inc(page); |
| 744 | kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page; |
| 745 | kvm->arch.vsie.page_count++; |
| 746 | } else { |
| 747 | /* reuse an existing entry that belongs to nobody */ |
| 748 | while (true) { |
| 749 | page = kvm->arch.vsie.pages[kvm->arch.vsie.next]; |
| 750 | if (page_ref_inc_return(page) == 2) |
| 751 | break; |
| 752 | page_ref_dec(page); |
| 753 | kvm->arch.vsie.next++; |
| 754 | kvm->arch.vsie.next %= nr_vcpus; |
| 755 | } |
| 756 | radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); |
| 757 | } |
| 758 | page->index = addr; |
| 759 | /* double use of the same address */ |
| 760 | if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) { |
| 761 | page_ref_dec(page); |
| 762 | mutex_unlock(&kvm->arch.vsie.mutex); |
| 763 | return NULL; |
| 764 | } |
| 765 | mutex_unlock(&kvm->arch.vsie.mutex); |
| 766 | |
| 767 | vsie_page = page_to_virt(page); |
| 768 | memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block)); |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 769 | release_gmap_shadow(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 770 | vsie_page->scb_s.ihcpu = 0xffffU; |
| 771 | return vsie_page; |
| 772 | } |
| 773 | |
| 774 | /* put a vsie page acquired via get_vsie_page */ |
| 775 | static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page) |
| 776 | { |
| 777 | struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT); |
| 778 | |
| 779 | page_ref_dec(page); |
| 780 | } |
| 781 | |
| 782 | int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu) |
| 783 | { |
| 784 | struct vsie_page *vsie_page; |
| 785 | unsigned long scb_addr; |
| 786 | int rc; |
| 787 | |
| 788 | vcpu->stat.instruction_sie++; |
| 789 | if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2)) |
| 790 | return -EOPNOTSUPP; |
| 791 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 792 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 793 | |
| 794 | BUILD_BUG_ON(sizeof(struct vsie_page) != 4096); |
| 795 | scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL); |
| 796 | |
| 797 | /* 512 byte alignment */ |
| 798 | if (unlikely(scb_addr & 0x1ffUL)) |
| 799 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 800 | |
| 801 | if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0)) |
| 802 | return 0; |
| 803 | |
| 804 | vsie_page = get_vsie_page(vcpu->kvm, scb_addr); |
| 805 | if (IS_ERR(vsie_page)) |
| 806 | return PTR_ERR(vsie_page); |
| 807 | else if (!vsie_page) |
| 808 | /* double use of sie control block - simply do nothing */ |
| 809 | return 0; |
| 810 | |
| 811 | rc = pin_scb(vcpu, vsie_page, scb_addr); |
| 812 | if (rc) |
| 813 | goto out_put; |
| 814 | rc = shadow_scb(vcpu, vsie_page); |
| 815 | if (rc) |
| 816 | goto out_unpin_scb; |
| 817 | rc = pin_blocks(vcpu, vsie_page); |
| 818 | if (rc) |
| 819 | goto out_unshadow; |
| 820 | rc = vsie_run(vcpu, vsie_page); |
| 821 | unpin_blocks(vcpu, vsie_page); |
| 822 | out_unshadow: |
| 823 | unshadow_scb(vcpu, vsie_page); |
| 824 | out_unpin_scb: |
| 825 | unpin_scb(vcpu, vsie_page, scb_addr); |
| 826 | out_put: |
| 827 | put_vsie_page(vcpu->kvm, vsie_page); |
| 828 | |
| 829 | return rc < 0 ? rc : 0; |
| 830 | } |
| 831 | |
| 832 | /* Init the vsie data structures. To be called when a vm is initialized. */ |
| 833 | void kvm_s390_vsie_init(struct kvm *kvm) |
| 834 | { |
| 835 | mutex_init(&kvm->arch.vsie.mutex); |
| 836 | INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL); |
| 837 | } |
| 838 | |
| 839 | /* Destroy the vsie data structures. To be called when a vm is destroyed. */ |
| 840 | void kvm_s390_vsie_destroy(struct kvm *kvm) |
| 841 | { |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 842 | struct vsie_page *vsie_page; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 843 | struct page *page; |
| 844 | int i; |
| 845 | |
| 846 | mutex_lock(&kvm->arch.vsie.mutex); |
| 847 | for (i = 0; i < kvm->arch.vsie.page_count; i++) { |
| 848 | page = kvm->arch.vsie.pages[i]; |
| 849 | kvm->arch.vsie.pages[i] = NULL; |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 850 | vsie_page = page_to_virt(page); |
| 851 | release_gmap_shadow(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 852 | /* free the radix tree entry */ |
| 853 | radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); |
| 854 | __free_page(page); |
| 855 | } |
| 856 | kvm->arch.vsie.page_count = 0; |
| 857 | mutex_unlock(&kvm->arch.vsie.mutex); |
| 858 | } |