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> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
| 18 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 19 | #include <asm/gmap.h> |
| 20 | #include <asm/mmu_context.h> |
| 21 | #include <asm/sclp.h> |
| 22 | #include <asm/nmi.h> |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame] | 23 | #include <asm/dis.h> |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 24 | #include "kvm-s390.h" |
| 25 | #include "gaccess.h" |
| 26 | |
| 27 | struct vsie_page { |
| 28 | struct kvm_s390_sie_block scb_s; /* 0x0000 */ |
| 29 | /* the pinned originial scb */ |
| 30 | struct kvm_s390_sie_block *scb_o; /* 0x0200 */ |
| 31 | /* the shadow gmap in use by the vsie_page */ |
| 32 | struct gmap *gmap; /* 0x0208 */ |
David Hildenbrand | 1b7029b | 2015-07-08 13:25:31 +0200 | [diff] [blame] | 33 | /* address of the last reported fault to guest2 */ |
| 34 | unsigned long fault_addr; /* 0x0210 */ |
| 35 | __u8 reserved[0x0700 - 0x0218]; /* 0x0218 */ |
David Hildenbrand | bbeaa58 | 2015-11-26 13:11:42 +0100 | [diff] [blame] | 36 | struct kvm_s390_crypto_cb crycb; /* 0x0700 */ |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame] | 37 | __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */ |
Martin Schwidefsky | 1cae025 | 2017-06-21 16:49:15 +0200 | [diff] [blame^] | 38 | }; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 39 | |
| 40 | /* trigger a validity icpt for the given scb */ |
| 41 | static int set_validity_icpt(struct kvm_s390_sie_block *scb, |
| 42 | __u16 reason_code) |
| 43 | { |
| 44 | scb->ipa = 0x1000; |
| 45 | scb->ipb = ((__u32) reason_code) << 16; |
| 46 | scb->icptcode = ICPT_VALIDITY; |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | /* mark the prefix as unmapped, this will block the VSIE */ |
| 51 | static void prefix_unmapped(struct vsie_page *vsie_page) |
| 52 | { |
| 53 | atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20); |
| 54 | } |
| 55 | |
| 56 | /* mark the prefix as unmapped and wait until the VSIE has been left */ |
| 57 | static void prefix_unmapped_sync(struct vsie_page *vsie_page) |
| 58 | { |
| 59 | prefix_unmapped(vsie_page); |
| 60 | if (vsie_page->scb_s.prog0c & PROG_IN_SIE) |
| 61 | atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags); |
| 62 | while (vsie_page->scb_s.prog0c & PROG_IN_SIE) |
| 63 | cpu_relax(); |
| 64 | } |
| 65 | |
| 66 | /* mark the prefix as mapped, this will allow the VSIE to run */ |
| 67 | static void prefix_mapped(struct vsie_page *vsie_page) |
| 68 | { |
| 69 | atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20); |
| 70 | } |
| 71 | |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 72 | /* test if the prefix is mapped into the gmap shadow */ |
| 73 | static int prefix_is_mapped(struct vsie_page *vsie_page) |
| 74 | { |
| 75 | return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST); |
| 76 | } |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 77 | |
| 78 | /* copy the updated intervention request bits into the shadow scb */ |
| 79 | static void update_intervention_requests(struct vsie_page *vsie_page) |
| 80 | { |
| 81 | const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT; |
| 82 | int cpuflags; |
| 83 | |
| 84 | cpuflags = atomic_read(&vsie_page->scb_o->cpuflags); |
| 85 | atomic_andnot(bits, &vsie_page->scb_s.cpuflags); |
| 86 | atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags); |
| 87 | } |
| 88 | |
| 89 | /* shadow (filter and validate) the cpuflags */ |
| 90 | static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 91 | { |
| 92 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 93 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 94 | int newflags, cpuflags = atomic_read(&scb_o->cpuflags); |
| 95 | |
| 96 | /* we don't allow ESA/390 guests */ |
| 97 | if (!(cpuflags & CPUSTAT_ZARCH)) |
| 98 | return set_validity_icpt(scb_s, 0x0001U); |
| 99 | |
| 100 | if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS)) |
| 101 | return set_validity_icpt(scb_s, 0x0001U); |
| 102 | else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR)) |
| 103 | return set_validity_icpt(scb_s, 0x0007U); |
| 104 | |
| 105 | /* intervention requests will be set later */ |
| 106 | newflags = CPUSTAT_ZARCH; |
David Hildenbrand | 535ef81 | 2016-02-12 12:24:20 +0100 | [diff] [blame] | 107 | if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8)) |
| 108 | newflags |= CPUSTAT_GED; |
| 109 | if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) { |
| 110 | if (cpuflags & CPUSTAT_GED) |
| 111 | return set_validity_icpt(scb_s, 0x0001U); |
| 112 | newflags |= CPUSTAT_GED2; |
| 113 | } |
David Hildenbrand | 77d18f6 | 2015-11-24 16:32:35 +0100 | [diff] [blame] | 114 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE)) |
| 115 | newflags |= cpuflags & CPUSTAT_P; |
David Hildenbrand | a1b7b9b | 2015-11-24 16:41:33 +0100 | [diff] [blame] | 116 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS)) |
| 117 | newflags |= cpuflags & CPUSTAT_SM; |
David Hildenbrand | 7fd7f39 | 2015-11-24 16:56:23 +0100 | [diff] [blame] | 118 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS)) |
| 119 | newflags |= cpuflags & CPUSTAT_IBS; |
Farhan Ali | 730cd63 | 2017-02-24 16:12:56 -0500 | [diff] [blame] | 120 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS)) |
| 121 | newflags |= cpuflags & CPUSTAT_KSS; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 122 | |
| 123 | atomic_set(&scb_s->cpuflags, newflags); |
| 124 | return 0; |
| 125 | } |
| 126 | |
David Hildenbrand | bbeaa58 | 2015-11-26 13:11:42 +0100 | [diff] [blame] | 127 | /* |
| 128 | * Create a shadow copy of the crycb block and setup key wrapping, if |
| 129 | * requested for guest 3 and enabled for guest 2. |
| 130 | * |
| 131 | * We only accept format-1 (no AP in g2), but convert it into format-2 |
| 132 | * There is nothing to do for format-0. |
| 133 | * |
| 134 | * Returns: - 0 if shadowed or nothing to do |
| 135 | * - > 0 if control has to be given to guest 2 |
| 136 | */ |
| 137 | static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 138 | { |
| 139 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 140 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 141 | u32 crycb_addr = scb_o->crycbd & 0x7ffffff8U; |
| 142 | unsigned long *b1, *b2; |
| 143 | u8 ecb3_flags; |
| 144 | |
| 145 | scb_s->crycbd = 0; |
| 146 | if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1)) |
| 147 | return 0; |
| 148 | /* format-1 is supported with message-security-assist extension 3 */ |
| 149 | if (!test_kvm_facility(vcpu->kvm, 76)) |
| 150 | return 0; |
| 151 | /* we may only allow it if enabled for guest 2 */ |
| 152 | ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 & |
| 153 | (ECB3_AES | ECB3_DEA); |
| 154 | if (!ecb3_flags) |
| 155 | return 0; |
| 156 | |
| 157 | if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK)) |
| 158 | return set_validity_icpt(scb_s, 0x003CU); |
| 159 | else if (!crycb_addr) |
| 160 | return set_validity_icpt(scb_s, 0x0039U); |
| 161 | |
| 162 | /* copy only the wrapping keys */ |
| 163 | if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56)) |
| 164 | return set_validity_icpt(scb_s, 0x0035U); |
| 165 | |
| 166 | scb_s->ecb3 |= ecb3_flags; |
| 167 | scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 | |
| 168 | CRYCB_FORMAT2; |
| 169 | |
| 170 | /* xor both blocks in one run */ |
| 171 | b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask; |
| 172 | b2 = (unsigned long *) |
| 173 | vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask; |
| 174 | /* as 56%8 == 0, bitmap_xor won't overwrite any data */ |
| 175 | bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56); |
| 176 | return 0; |
| 177 | } |
| 178 | |
David Hildenbrand | 3573602 | 2016-02-19 10:11:24 +0100 | [diff] [blame] | 179 | /* shadow (round up/down) the ibc to avoid validity icpt */ |
| 180 | static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 181 | { |
| 182 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 183 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 184 | __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU; |
| 185 | |
| 186 | scb_s->ibc = 0; |
| 187 | /* ibc installed in g2 and requested for g3 */ |
| 188 | if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) { |
| 189 | scb_s->ibc = scb_o->ibc & 0x0fffU; |
| 190 | /* takte care of the minimum ibc level of the machine */ |
| 191 | if (scb_s->ibc < min_ibc) |
| 192 | scb_s->ibc = min_ibc; |
| 193 | /* take care of the maximum ibc level set for the guest */ |
| 194 | if (scb_s->ibc > vcpu->kvm->arch.model.ibc) |
| 195 | scb_s->ibc = vcpu->kvm->arch.model.ibc; |
| 196 | } |
| 197 | } |
| 198 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 199 | /* unshadow the scb, copying parameters back to the real scb */ |
| 200 | static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 201 | { |
| 202 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 203 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 204 | |
| 205 | /* interception */ |
| 206 | scb_o->icptcode = scb_s->icptcode; |
| 207 | scb_o->icptstatus = scb_s->icptstatus; |
| 208 | scb_o->ipa = scb_s->ipa; |
| 209 | scb_o->ipb = scb_s->ipb; |
| 210 | scb_o->gbea = scb_s->gbea; |
| 211 | |
| 212 | /* timer */ |
| 213 | scb_o->cputm = scb_s->cputm; |
| 214 | scb_o->ckc = scb_s->ckc; |
| 215 | scb_o->todpr = scb_s->todpr; |
| 216 | |
| 217 | /* guest state */ |
| 218 | scb_o->gpsw = scb_s->gpsw; |
| 219 | scb_o->gg14 = scb_s->gg14; |
| 220 | scb_o->gg15 = scb_s->gg15; |
| 221 | memcpy(scb_o->gcr, scb_s->gcr, 128); |
| 222 | scb_o->pp = scb_s->pp; |
| 223 | |
| 224 | /* interrupt intercept */ |
| 225 | switch (scb_s->icptcode) { |
| 226 | case ICPT_PROGI: |
| 227 | case ICPT_INSTPROGI: |
| 228 | case ICPT_EXTINT: |
| 229 | memcpy((void *)((u64)scb_o + 0xc0), |
| 230 | (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0); |
| 231 | break; |
| 232 | case ICPT_PARTEXEC: |
| 233 | /* MVPG only */ |
| 234 | memcpy((void *)((u64)scb_o + 0xc0), |
| 235 | (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0); |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | if (scb_s->ihcpu != 0xffffU) |
| 240 | scb_o->ihcpu = scb_s->ihcpu; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Setup the shadow scb by copying and checking the relevant parts of the g2 |
| 245 | * provided scb. |
| 246 | * |
| 247 | * Returns: - 0 if the scb has been shadowed |
| 248 | * - > 0 if control has to be given to guest 2 |
| 249 | */ |
| 250 | static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 251 | { |
| 252 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 253 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 254 | bool had_tx = scb_s->ecb & ECB_TE; |
David Hildenbrand | a1b7b9b | 2015-11-24 16:41:33 +0100 | [diff] [blame] | 255 | unsigned long new_mso = 0; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 256 | int rc; |
| 257 | |
| 258 | /* make sure we don't have any leftovers when reusing the scb */ |
| 259 | scb_s->icptcode = 0; |
| 260 | scb_s->eca = 0; |
| 261 | scb_s->ecb = 0; |
| 262 | scb_s->ecb2 = 0; |
| 263 | scb_s->ecb3 = 0; |
| 264 | scb_s->ecd = 0; |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame] | 265 | scb_s->fac = 0; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 266 | |
| 267 | rc = prepare_cpuflags(vcpu, vsie_page); |
| 268 | if (rc) |
| 269 | goto out; |
| 270 | |
| 271 | /* timer */ |
| 272 | scb_s->cputm = scb_o->cputm; |
| 273 | scb_s->ckc = scb_o->ckc; |
| 274 | scb_s->todpr = scb_o->todpr; |
| 275 | scb_s->epoch = scb_o->epoch; |
| 276 | |
| 277 | /* guest state */ |
| 278 | scb_s->gpsw = scb_o->gpsw; |
| 279 | scb_s->gg14 = scb_o->gg14; |
| 280 | scb_s->gg15 = scb_o->gg15; |
| 281 | memcpy(scb_s->gcr, scb_o->gcr, 128); |
| 282 | scb_s->pp = scb_o->pp; |
| 283 | |
| 284 | /* interception / execution handling */ |
| 285 | scb_s->gbea = scb_o->gbea; |
| 286 | scb_s->lctl = scb_o->lctl; |
| 287 | scb_s->svcc = scb_o->svcc; |
| 288 | scb_s->ictl = scb_o->ictl; |
| 289 | /* |
| 290 | * SKEY handling functions can't deal with false setting of PTE invalid |
| 291 | * bits. Therefore we cannot provide interpretation and would later |
| 292 | * have to provide own emulation handlers. |
| 293 | */ |
Farhan Ali | 730cd63 | 2017-02-24 16:12:56 -0500 | [diff] [blame] | 294 | if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS)) |
| 295 | scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; |
| 296 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 297 | scb_s->icpua = scb_o->icpua; |
| 298 | |
David Hildenbrand | a1b7b9b | 2015-11-24 16:41:33 +0100 | [diff] [blame] | 299 | if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM)) |
| 300 | new_mso = scb_o->mso & 0xfffffffffff00000UL; |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 301 | /* if the hva of the prefix changes, we have to remap the prefix */ |
| 302 | if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix) |
| 303 | prefix_unmapped(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 304 | /* SIE will do mso/msl validity and exception checks for us */ |
| 305 | scb_s->msl = scb_o->msl & 0xfffffffffff00000UL; |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 306 | scb_s->mso = new_mso; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 307 | scb_s->prefix = scb_o->prefix; |
| 308 | |
| 309 | /* We have to definetly flush the tlb if this scb never ran */ |
| 310 | if (scb_s->ihcpu != 0xffffU) |
| 311 | scb_s->ihcpu = scb_o->ihcpu; |
| 312 | |
| 313 | /* MVPG and Protection Exception Interpretation are always available */ |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 314 | scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI); |
David Hildenbrand | 4ceafa9 | 2015-11-27 12:34:28 +0100 | [diff] [blame] | 315 | /* Host-protection-interruption introduced with ESOP */ |
| 316 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 317 | scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT; |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 318 | /* transactional execution */ |
| 319 | if (test_kvm_facility(vcpu->kvm, 73)) { |
| 320 | /* remap the prefix is tx is toggled on */ |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 321 | if ((scb_o->ecb & ECB_TE) && !had_tx) |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 322 | prefix_unmapped(vsie_page); |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 323 | scb_s->ecb |= scb_o->ecb & ECB_TE; |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 324 | } |
David Hildenbrand | c9bc1ea | 2015-11-25 11:08:32 +0100 | [diff] [blame] | 325 | /* SIMD */ |
| 326 | if (test_kvm_facility(vcpu->kvm, 129)) { |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 327 | scb_s->eca |= scb_o->eca & ECA_VX; |
| 328 | scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT; |
David Hildenbrand | c9bc1ea | 2015-11-25 11:08:32 +0100 | [diff] [blame] | 329 | } |
David Hildenbrand | 588438c | 2016-01-26 12:51:06 +0100 | [diff] [blame] | 330 | /* Run-time-Instrumentation */ |
| 331 | if (test_kvm_facility(vcpu->kvm, 64)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 332 | scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI; |
Janosch Frank | cd1836f | 2016-08-04 09:57:36 +0200 | [diff] [blame] | 333 | /* Instruction Execution Prevention */ |
| 334 | if (test_kvm_facility(vcpu->kvm, 130)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 335 | scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 336 | /* Guarded Storage */ |
| 337 | if (test_kvm_facility(vcpu->kvm, 133)) { |
| 338 | scb_s->ecb |= scb_o->ecb & ECB_GS; |
| 339 | scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT; |
| 340 | } |
David Hildenbrand | 0615a32 | 2015-11-25 09:59:49 +0100 | [diff] [blame] | 341 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 342 | scb_s->eca |= scb_o->eca & ECA_SII; |
David Hildenbrand | 5630a8e | 2015-11-24 16:53:51 +0100 | [diff] [blame] | 343 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 344 | scb_s->eca |= scb_o->eca & ECA_IB; |
David Hildenbrand | 13ee3f6 | 2015-11-24 16:54:37 +0100 | [diff] [blame] | 345 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI)) |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 346 | scb_s->eca |= scb_o->eca & ECA_CEI; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 347 | |
David Hildenbrand | 3573602 | 2016-02-19 10:11:24 +0100 | [diff] [blame] | 348 | prepare_ibc(vcpu, vsie_page); |
David Hildenbrand | bbeaa58 | 2015-11-26 13:11:42 +0100 | [diff] [blame] | 349 | rc = shadow_crycb(vcpu, vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 350 | out: |
| 351 | if (rc) |
| 352 | unshadow_scb(vcpu, vsie_page); |
| 353 | return rc; |
| 354 | } |
| 355 | |
| 356 | void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start, |
| 357 | unsigned long end) |
| 358 | { |
| 359 | struct kvm *kvm = gmap->private; |
| 360 | struct vsie_page *cur; |
| 361 | unsigned long prefix; |
| 362 | struct page *page; |
| 363 | int i; |
| 364 | |
| 365 | if (!gmap_is_shadow(gmap)) |
| 366 | return; |
| 367 | if (start >= 1UL << 31) |
| 368 | /* We are only interested in prefix pages */ |
| 369 | return; |
| 370 | |
| 371 | /* |
| 372 | * Only new shadow blocks are added to the list during runtime, |
| 373 | * therefore we can safely reference them all the time. |
| 374 | */ |
| 375 | for (i = 0; i < kvm->arch.vsie.page_count; i++) { |
| 376 | page = READ_ONCE(kvm->arch.vsie.pages[i]); |
| 377 | if (!page) |
| 378 | continue; |
| 379 | cur = page_to_virt(page); |
| 380 | if (READ_ONCE(cur->gmap) != gmap) |
| 381 | continue; |
| 382 | prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT; |
| 383 | /* with mso/msl, the prefix lies at an offset */ |
| 384 | prefix += cur->scb_s.mso; |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 385 | if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1) |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 386 | prefix_unmapped_sync(cur); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 391 | * Map the first prefix page and if tx is enabled also the second prefix page. |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 392 | * |
| 393 | * The prefix will be protected, a gmap notifier will inform about unmaps. |
| 394 | * The shadow scb must not be executed until the prefix is remapped, this is |
| 395 | * guaranteed by properly handling PROG_REQUEST. |
| 396 | * |
| 397 | * Returns: - 0 on if successfully mapped or already mapped |
| 398 | * - > 0 if control has to be given to guest 2 |
| 399 | * - -EAGAIN if the caller can retry immediately |
| 400 | * - -ENOMEM if out of memory |
| 401 | */ |
| 402 | static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 403 | { |
| 404 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 405 | u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT; |
| 406 | int rc; |
| 407 | |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 408 | if (prefix_is_mapped(vsie_page)) |
| 409 | return 0; |
| 410 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 411 | /* mark it as mapped so we can catch any concurrent unmappers */ |
| 412 | prefix_mapped(vsie_page); |
| 413 | |
| 414 | /* with mso/msl, the prefix lies at offset *mso* */ |
| 415 | prefix += scb_s->mso; |
| 416 | |
| 417 | rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix); |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 418 | if (!rc && (scb_s->ecb & ECB_TE)) |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 419 | rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, |
| 420 | prefix + PAGE_SIZE); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 421 | /* |
| 422 | * We don't have to mprotect, we will be called for all unshadows. |
| 423 | * SIE will detect if protection applies and trigger a validity. |
| 424 | */ |
| 425 | if (rc) |
| 426 | prefix_unmapped(vsie_page); |
| 427 | if (rc > 0 || rc == -EFAULT) |
| 428 | rc = set_validity_icpt(scb_s, 0x0037U); |
| 429 | return rc; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Pin the guest page given by gpa and set hpa to the pinned host address. |
| 434 | * Will always be pinned writable. |
| 435 | * |
| 436 | * Returns: - 0 on success |
| 437 | * - -EINVAL if the gpa is not valid guest storage |
| 438 | * - -ENOMEM if out of memory |
| 439 | */ |
| 440 | static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa) |
| 441 | { |
| 442 | struct page *page; |
| 443 | hva_t hva; |
| 444 | int rc; |
| 445 | |
| 446 | hva = gfn_to_hva(kvm, gpa_to_gfn(gpa)); |
| 447 | if (kvm_is_error_hva(hva)) |
| 448 | return -EINVAL; |
| 449 | rc = get_user_pages_fast(hva, 1, 1, &page); |
| 450 | if (rc < 0) |
| 451 | return rc; |
| 452 | else if (rc != 1) |
| 453 | return -ENOMEM; |
| 454 | *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */ |
| 459 | static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa) |
| 460 | { |
| 461 | struct page *page; |
| 462 | |
| 463 | page = virt_to_page(hpa); |
| 464 | set_page_dirty_lock(page); |
| 465 | put_page(page); |
| 466 | /* mark the page always as dirty for migration */ |
| 467 | mark_page_dirty(kvm, gpa_to_gfn(gpa)); |
| 468 | } |
| 469 | |
| 470 | /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */ |
| 471 | static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 472 | { |
| 473 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 474 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 475 | hpa_t hpa; |
| 476 | gpa_t gpa; |
| 477 | |
| 478 | hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol; |
| 479 | if (hpa) { |
| 480 | gpa = scb_o->scaol & ~0xfUL; |
David Hildenbrand | 19c439b | 2015-11-25 11:02:26 +0100 | [diff] [blame] | 481 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO)) |
| 482 | gpa |= (u64) scb_o->scaoh << 32; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 483 | unpin_guest_page(vcpu->kvm, gpa, hpa); |
| 484 | scb_s->scaol = 0; |
| 485 | scb_s->scaoh = 0; |
| 486 | } |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 487 | |
| 488 | hpa = scb_s->itdba; |
| 489 | if (hpa) { |
| 490 | gpa = scb_o->itdba & ~0xffUL; |
| 491 | unpin_guest_page(vcpu->kvm, gpa, hpa); |
| 492 | scb_s->itdba = 0; |
| 493 | } |
David Hildenbrand | c9bc1ea | 2015-11-25 11:08:32 +0100 | [diff] [blame] | 494 | |
| 495 | hpa = scb_s->gvrd; |
| 496 | if (hpa) { |
| 497 | gpa = scb_o->gvrd & ~0x1ffUL; |
| 498 | unpin_guest_page(vcpu->kvm, gpa, hpa); |
| 499 | scb_s->gvrd = 0; |
| 500 | } |
David Hildenbrand | 588438c | 2016-01-26 12:51:06 +0100 | [diff] [blame] | 501 | |
| 502 | hpa = scb_s->riccbd; |
| 503 | if (hpa) { |
| 504 | gpa = scb_o->riccbd & ~0x3fUL; |
| 505 | unpin_guest_page(vcpu->kvm, gpa, hpa); |
| 506 | scb_s->riccbd = 0; |
| 507 | } |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 508 | |
| 509 | hpa = scb_s->sdnxo; |
| 510 | if (hpa) { |
| 511 | gpa = scb_o->sdnxo; |
| 512 | unpin_guest_page(vcpu->kvm, gpa, hpa); |
| 513 | scb_s->sdnxo = 0; |
| 514 | } |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Instead of shadowing some blocks, we can simply forward them because the |
| 519 | * addresses in the scb are 64 bit long. |
| 520 | * |
| 521 | * This works as long as the data lies in one page. If blocks ever exceed one |
| 522 | * page, we have to fall back to shadowing. |
| 523 | * |
| 524 | * As we reuse the sca, the vcpu pointers contained in it are invalid. We must |
| 525 | * therefore not enable any facilities that access these pointers (e.g. SIGPIF). |
| 526 | * |
| 527 | * Returns: - 0 if all blocks were pinned. |
| 528 | * - > 0 if control has to be given to guest 2 |
| 529 | * - -ENOMEM if out of memory |
| 530 | */ |
| 531 | static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 532 | { |
| 533 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 534 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 535 | hpa_t hpa; |
| 536 | gpa_t gpa; |
| 537 | int rc = 0; |
| 538 | |
| 539 | gpa = scb_o->scaol & ~0xfUL; |
David Hildenbrand | 19c439b | 2015-11-25 11:02:26 +0100 | [diff] [blame] | 540 | if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO)) |
| 541 | gpa |= (u64) scb_o->scaoh << 32; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 542 | if (gpa) { |
| 543 | if (!(gpa & ~0x1fffUL)) |
| 544 | rc = set_validity_icpt(scb_s, 0x0038U); |
| 545 | else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu)) |
| 546 | rc = set_validity_icpt(scb_s, 0x0011U); |
| 547 | else if ((gpa & PAGE_MASK) != |
| 548 | ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK)) |
| 549 | rc = set_validity_icpt(scb_s, 0x003bU); |
| 550 | if (!rc) { |
| 551 | rc = pin_guest_page(vcpu->kvm, gpa, &hpa); |
| 552 | if (rc == -EINVAL) |
| 553 | rc = set_validity_icpt(scb_s, 0x0034U); |
| 554 | } |
| 555 | if (rc) |
| 556 | goto unpin; |
| 557 | scb_s->scaoh = (u32)((u64)hpa >> 32); |
| 558 | scb_s->scaol = (u32)(u64)hpa; |
| 559 | } |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 560 | |
| 561 | gpa = scb_o->itdba & ~0xffUL; |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 562 | if (gpa && (scb_s->ecb & ECB_TE)) { |
David Hildenbrand | 166ecb3 | 2015-11-25 11:13:32 +0100 | [diff] [blame] | 563 | if (!(gpa & ~0x1fffU)) { |
| 564 | rc = set_validity_icpt(scb_s, 0x0080U); |
| 565 | goto unpin; |
| 566 | } |
| 567 | /* 256 bytes cannot cross page boundaries */ |
| 568 | rc = pin_guest_page(vcpu->kvm, gpa, &hpa); |
| 569 | if (rc == -EINVAL) |
| 570 | rc = set_validity_icpt(scb_s, 0x0080U); |
| 571 | if (rc) |
| 572 | goto unpin; |
| 573 | scb_s->itdba = hpa; |
| 574 | } |
David Hildenbrand | c9bc1ea | 2015-11-25 11:08:32 +0100 | [diff] [blame] | 575 | |
| 576 | gpa = scb_o->gvrd & ~0x1ffUL; |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 577 | if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) { |
David Hildenbrand | c9bc1ea | 2015-11-25 11:08:32 +0100 | [diff] [blame] | 578 | if (!(gpa & ~0x1fffUL)) { |
| 579 | rc = set_validity_icpt(scb_s, 0x1310U); |
| 580 | goto unpin; |
| 581 | } |
| 582 | /* |
| 583 | * 512 bytes vector registers cannot cross page boundaries |
| 584 | * if this block gets bigger, we have to shadow it. |
| 585 | */ |
| 586 | rc = pin_guest_page(vcpu->kvm, gpa, &hpa); |
| 587 | if (rc == -EINVAL) |
| 588 | rc = set_validity_icpt(scb_s, 0x1310U); |
| 589 | if (rc) |
| 590 | goto unpin; |
| 591 | scb_s->gvrd = hpa; |
| 592 | } |
David Hildenbrand | 588438c | 2016-01-26 12:51:06 +0100 | [diff] [blame] | 593 | |
| 594 | gpa = scb_o->riccbd & ~0x3fUL; |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 595 | if (gpa && (scb_s->ecb3 & ECB3_RI)) { |
David Hildenbrand | 588438c | 2016-01-26 12:51:06 +0100 | [diff] [blame] | 596 | if (!(gpa & ~0x1fffUL)) { |
| 597 | rc = set_validity_icpt(scb_s, 0x0043U); |
| 598 | goto unpin; |
| 599 | } |
| 600 | /* 64 bytes cannot cross page boundaries */ |
| 601 | rc = pin_guest_page(vcpu->kvm, gpa, &hpa); |
| 602 | if (rc == -EINVAL) |
| 603 | rc = set_validity_icpt(scb_s, 0x0043U); |
| 604 | /* Validity 0x0044 will be checked by SIE */ |
| 605 | if (rc) |
| 606 | goto unpin; |
David Hildenbrand | 4d21cef3 | 2016-09-02 12:33:49 +0200 | [diff] [blame] | 607 | scb_s->riccbd = hpa; |
David Hildenbrand | 588438c | 2016-01-26 12:51:06 +0100 | [diff] [blame] | 608 | } |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 609 | if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) { |
| 610 | unsigned long sdnxc; |
| 611 | |
| 612 | gpa = scb_o->sdnxo & ~0xfUL; |
| 613 | sdnxc = scb_o->sdnxo & 0xfUL; |
| 614 | if (!gpa || !(gpa & ~0x1fffUL)) { |
| 615 | rc = set_validity_icpt(scb_s, 0x10b0U); |
| 616 | goto unpin; |
| 617 | } |
| 618 | if (sdnxc < 6 || sdnxc > 12) { |
| 619 | rc = set_validity_icpt(scb_s, 0x10b1U); |
| 620 | goto unpin; |
| 621 | } |
| 622 | if (gpa & ((1 << sdnxc) - 1)) { |
| 623 | rc = set_validity_icpt(scb_s, 0x10b2U); |
| 624 | goto unpin; |
| 625 | } |
| 626 | /* Due to alignment rules (checked above) this cannot |
| 627 | * cross page boundaries |
| 628 | */ |
| 629 | rc = pin_guest_page(vcpu->kvm, gpa, &hpa); |
| 630 | if (rc == -EINVAL) |
| 631 | rc = set_validity_icpt(scb_s, 0x10b0U); |
| 632 | if (rc) |
| 633 | goto unpin; |
Christian Borntraeger | fe722d1 | 2017-04-07 14:23:13 +0200 | [diff] [blame] | 634 | scb_s->sdnxo = hpa | sdnxc; |
Fan Zhang | 4e0b1ab | 2016-11-29 07:17:55 +0100 | [diff] [blame] | 635 | } |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 636 | return 0; |
| 637 | unpin: |
| 638 | unpin_blocks(vcpu, vsie_page); |
| 639 | return rc; |
| 640 | } |
| 641 | |
| 642 | /* unpin the scb provided by guest 2, marking it as dirty */ |
| 643 | static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, |
| 644 | gpa_t gpa) |
| 645 | { |
| 646 | hpa_t hpa = (hpa_t) vsie_page->scb_o; |
| 647 | |
| 648 | if (hpa) |
| 649 | unpin_guest_page(vcpu->kvm, gpa, hpa); |
| 650 | vsie_page->scb_o = NULL; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o. |
| 655 | * |
| 656 | * Returns: - 0 if the scb was pinned. |
| 657 | * - > 0 if control has to be given to guest 2 |
| 658 | * - -ENOMEM if out of memory |
| 659 | */ |
| 660 | static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, |
| 661 | gpa_t gpa) |
| 662 | { |
| 663 | hpa_t hpa; |
| 664 | int rc; |
| 665 | |
| 666 | rc = pin_guest_page(vcpu->kvm, gpa, &hpa); |
| 667 | if (rc == -EINVAL) { |
| 668 | rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 669 | if (!rc) |
| 670 | rc = 1; |
| 671 | } |
| 672 | if (!rc) |
| 673 | vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa; |
| 674 | return rc; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Inject a fault into guest 2. |
| 679 | * |
| 680 | * Returns: - > 0 if control has to be given to guest 2 |
| 681 | * < 0 if an error occurred during injection. |
| 682 | */ |
| 683 | static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr, |
| 684 | bool write_flag) |
| 685 | { |
| 686 | struct kvm_s390_pgm_info pgm = { |
| 687 | .code = code, |
| 688 | .trans_exc_code = |
| 689 | /* 0-51: virtual address */ |
| 690 | (vaddr & 0xfffffffffffff000UL) | |
| 691 | /* 52-53: store / fetch */ |
| 692 | (((unsigned int) !write_flag) + 1) << 10, |
| 693 | /* 62-63: asce id (alway primary == 0) */ |
| 694 | .exc_access_id = 0, /* always primary */ |
| 695 | .op_access_id = 0, /* not MVPG */ |
| 696 | }; |
| 697 | int rc; |
| 698 | |
| 699 | if (code == PGM_PROTECTION) |
| 700 | pgm.trans_exc_code |= 0x4UL; |
| 701 | |
| 702 | rc = kvm_s390_inject_prog_irq(vcpu, &pgm); |
| 703 | return rc ? rc : 1; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * Handle a fault during vsie execution on a gmap shadow. |
| 708 | * |
| 709 | * Returns: - 0 if the fault was resolved |
| 710 | * - > 0 if control has to be given to guest 2 |
| 711 | * - < 0 if an error occurred |
| 712 | */ |
| 713 | static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 714 | { |
| 715 | int rc; |
| 716 | |
| 717 | if (current->thread.gmap_int_code == PGM_PROTECTION) |
| 718 | /* we can directly forward all protection exceptions */ |
| 719 | return inject_fault(vcpu, PGM_PROTECTION, |
| 720 | current->thread.gmap_addr, 1); |
| 721 | |
| 722 | rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, |
| 723 | current->thread.gmap_addr); |
| 724 | if (rc > 0) { |
| 725 | rc = inject_fault(vcpu, rc, |
| 726 | current->thread.gmap_addr, |
| 727 | current->thread.gmap_write_flag); |
David Hildenbrand | 1b7029b | 2015-07-08 13:25:31 +0200 | [diff] [blame] | 728 | if (rc >= 0) |
| 729 | vsie_page->fault_addr = current->thread.gmap_addr; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 730 | } |
| 731 | return rc; |
| 732 | } |
| 733 | |
David Hildenbrand | 1b7029b | 2015-07-08 13:25:31 +0200 | [diff] [blame] | 734 | /* |
| 735 | * Retry the previous fault that required guest 2 intervention. This avoids |
| 736 | * one superfluous SIE re-entry and direct exit. |
| 737 | * |
| 738 | * Will ignore any errors. The next SIE fault will do proper fault handling. |
| 739 | */ |
| 740 | static void handle_last_fault(struct kvm_vcpu *vcpu, |
| 741 | struct vsie_page *vsie_page) |
| 742 | { |
| 743 | if (vsie_page->fault_addr) |
| 744 | kvm_s390_shadow_fault(vcpu, vsie_page->gmap, |
| 745 | vsie_page->fault_addr); |
| 746 | vsie_page->fault_addr = 0; |
| 747 | } |
| 748 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 749 | static inline void clear_vsie_icpt(struct vsie_page *vsie_page) |
| 750 | { |
| 751 | vsie_page->scb_s.icptcode = 0; |
| 752 | } |
| 753 | |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame] | 754 | /* rewind the psw and clear the vsie icpt, so we can retry execution */ |
| 755 | static void retry_vsie_icpt(struct vsie_page *vsie_page) |
| 756 | { |
| 757 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 758 | int ilen = insn_length(scb_s->ipa >> 8); |
| 759 | |
| 760 | /* take care of EXECUTE instructions */ |
| 761 | if (scb_s->icptstatus & 1) { |
| 762 | ilen = (scb_s->icptstatus >> 4) & 0x6; |
| 763 | if (!ilen) |
| 764 | ilen = 4; |
| 765 | } |
| 766 | scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen); |
| 767 | clear_vsie_icpt(vsie_page); |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Try to shadow + enable the guest 2 provided facility list. |
| 772 | * Retry instruction execution if enabled for and provided by guest 2. |
| 773 | * |
| 774 | * Returns: - 0 if handled (retry or guest 2 icpt) |
| 775 | * - > 0 if control has to be given to guest 2 |
| 776 | */ |
| 777 | static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 778 | { |
| 779 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 780 | __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U; |
| 781 | |
| 782 | if (fac && test_kvm_facility(vcpu->kvm, 7)) { |
| 783 | retry_vsie_icpt(vsie_page); |
| 784 | if (read_guest_real(vcpu, fac, &vsie_page->fac, |
| 785 | sizeof(vsie_page->fac))) |
| 786 | return set_validity_icpt(scb_s, 0x1090U); |
| 787 | scb_s->fac = (__u32)(__u64) &vsie_page->fac; |
| 788 | } |
| 789 | return 0; |
| 790 | } |
| 791 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 792 | /* |
| 793 | * Run the vsie on a shadow scb and a shadow gmap, without any further |
| 794 | * sanity checks, handling SIE faults. |
| 795 | * |
| 796 | * Returns: - 0 everything went fine |
| 797 | * - > 0 if control has to be given to guest 2 |
| 798 | * - < 0 if an error occurred |
| 799 | */ |
| 800 | static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 801 | { |
| 802 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 803 | struct kvm_s390_sie_block *scb_o = vsie_page->scb_o; |
| 804 | int rc; |
| 805 | |
David Hildenbrand | 1b7029b | 2015-07-08 13:25:31 +0200 | [diff] [blame] | 806 | handle_last_fault(vcpu, vsie_page); |
| 807 | |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 808 | if (need_resched()) |
| 809 | schedule(); |
| 810 | if (test_cpu_flag(CIF_MCCK_PENDING)) |
| 811 | s390_handle_mcck(); |
| 812 | |
| 813 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 814 | local_irq_disable(); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 815 | guest_enter_irqoff(); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 816 | local_irq_enable(); |
| 817 | |
| 818 | rc = sie64a(scb_s, vcpu->run->s.regs.gprs); |
| 819 | |
| 820 | local_irq_disable(); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 821 | guest_exit_irqoff(); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 822 | local_irq_enable(); |
| 823 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 824 | |
| 825 | if (rc > 0) |
| 826 | rc = 0; /* we could still have an icpt */ |
| 827 | else if (rc == -EFAULT) |
| 828 | return handle_fault(vcpu, vsie_page); |
| 829 | |
| 830 | switch (scb_s->icptcode) { |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame] | 831 | case ICPT_INST: |
| 832 | if (scb_s->ipa == 0xb2b0) |
| 833 | rc = handle_stfle(vcpu, vsie_page); |
| 834 | break; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 835 | case ICPT_STOP: |
| 836 | /* stop not requested by g2 - must have been a kick */ |
| 837 | if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT)) |
| 838 | clear_vsie_icpt(vsie_page); |
| 839 | break; |
| 840 | case ICPT_VALIDITY: |
| 841 | if ((scb_s->ipa & 0xf000) != 0xf000) |
| 842 | scb_s->ipa += 0x1000; |
| 843 | break; |
| 844 | } |
| 845 | return rc; |
| 846 | } |
| 847 | |
| 848 | static void release_gmap_shadow(struct vsie_page *vsie_page) |
| 849 | { |
| 850 | if (vsie_page->gmap) |
| 851 | gmap_put(vsie_page->gmap); |
| 852 | WRITE_ONCE(vsie_page->gmap, NULL); |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 853 | prefix_unmapped(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static int acquire_gmap_shadow(struct kvm_vcpu *vcpu, |
| 857 | struct vsie_page *vsie_page) |
| 858 | { |
| 859 | unsigned long asce; |
| 860 | union ctlreg0 cr0; |
| 861 | struct gmap *gmap; |
| 862 | int edat; |
| 863 | |
| 864 | asce = vcpu->arch.sie_block->gcr[1]; |
| 865 | cr0.val = vcpu->arch.sie_block->gcr[0]; |
| 866 | edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8); |
| 867 | edat += edat && test_kvm_facility(vcpu->kvm, 78); |
| 868 | |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 869 | /* |
| 870 | * ASCE or EDAT could have changed since last icpt, or the gmap |
| 871 | * we're holding has been unshadowed. If the gmap is still valid, |
| 872 | * we can safely reuse it. |
| 873 | */ |
| 874 | if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat)) |
| 875 | return 0; |
| 876 | |
| 877 | /* release the old shadow - if any, and mark the prefix as unmapped */ |
| 878 | release_gmap_shadow(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 879 | gmap = gmap_shadow(vcpu->arch.gmap, asce, edat); |
| 880 | if (IS_ERR(gmap)) |
| 881 | return PTR_ERR(gmap); |
| 882 | gmap->private = vcpu->kvm; |
| 883 | WRITE_ONCE(vsie_page->gmap, gmap); |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* |
David Hildenbrand | adbf169 | 2016-05-27 22:03:52 +0200 | [diff] [blame] | 888 | * Register the shadow scb at the VCPU, e.g. for kicking out of vsie. |
| 889 | */ |
| 890 | static void register_shadow_scb(struct kvm_vcpu *vcpu, |
| 891 | struct vsie_page *vsie_page) |
| 892 | { |
David Hildenbrand | 91473b4 | 2015-10-29 10:30:36 +0100 | [diff] [blame] | 893 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 894 | |
David Hildenbrand | adbf169 | 2016-05-27 22:03:52 +0200 | [diff] [blame] | 895 | WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s); |
David Hildenbrand | b917ae5 | 2015-07-07 20:39:35 +0200 | [diff] [blame] | 896 | /* |
| 897 | * External calls have to lead to a kick of the vcpu and |
| 898 | * therefore the vsie -> Simulate Wait state. |
| 899 | */ |
| 900 | atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
David Hildenbrand | 91473b4 | 2015-10-29 10:30:36 +0100 | [diff] [blame] | 901 | /* |
| 902 | * We have to adjust the g3 epoch by the g2 epoch. The epoch will |
| 903 | * automatically be adjusted on tod clock changes via kvm_sync_clock. |
| 904 | */ |
| 905 | preempt_disable(); |
| 906 | scb_s->epoch += vcpu->kvm->arch.epoch; |
| 907 | preempt_enable(); |
David Hildenbrand | adbf169 | 2016-05-27 22:03:52 +0200 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* |
| 911 | * Unregister a shadow scb from a VCPU. |
| 912 | */ |
| 913 | static void unregister_shadow_scb(struct kvm_vcpu *vcpu) |
| 914 | { |
David Hildenbrand | b917ae5 | 2015-07-07 20:39:35 +0200 | [diff] [blame] | 915 | atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
David Hildenbrand | adbf169 | 2016-05-27 22:03:52 +0200 | [diff] [blame] | 916 | WRITE_ONCE(vcpu->arch.vsie_block, NULL); |
| 917 | } |
| 918 | |
| 919 | /* |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 920 | * Run the vsie on a shadowed scb, managing the gmap shadow, handling |
| 921 | * prefix pages and faults. |
| 922 | * |
| 923 | * Returns: - 0 if no errors occurred |
| 924 | * - > 0 if control has to be given to guest 2 |
| 925 | * - -ENOMEM if out of memory |
| 926 | */ |
| 927 | static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) |
| 928 | { |
| 929 | struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; |
| 930 | int rc = 0; |
| 931 | |
| 932 | while (1) { |
| 933 | rc = acquire_gmap_shadow(vcpu, vsie_page); |
| 934 | if (!rc) |
| 935 | rc = map_prefix(vcpu, vsie_page); |
| 936 | if (!rc) { |
| 937 | gmap_enable(vsie_page->gmap); |
| 938 | update_intervention_requests(vsie_page); |
| 939 | rc = do_vsie_run(vcpu, vsie_page); |
| 940 | gmap_enable(vcpu->arch.gmap); |
| 941 | } |
David Hildenbrand | adbf169 | 2016-05-27 22:03:52 +0200 | [diff] [blame] | 942 | atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 943 | |
| 944 | if (rc == -EAGAIN) |
| 945 | rc = 0; |
| 946 | if (rc || scb_s->icptcode || signal_pending(current) || |
| 947 | kvm_s390_vcpu_has_irq(vcpu, 0)) |
| 948 | break; |
Heiko Carstens | 0b92515 | 2017-01-02 08:51:02 +0100 | [diff] [blame] | 949 | } |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 950 | |
| 951 | if (rc == -EFAULT) { |
| 952 | /* |
| 953 | * Addressing exceptions are always presentes as intercepts. |
| 954 | * As addressing exceptions are suppressing and our guest 3 PSW |
| 955 | * points at the responsible instruction, we have to |
| 956 | * forward the PSW and set the ilc. If we can't read guest 3 |
| 957 | * instruction, we can use an arbitrary ilc. Let's always use |
| 958 | * ilen = 4 for now, so we can avoid reading in guest 3 virtual |
| 959 | * memory. (we could also fake the shadow so the hardware |
| 960 | * handles it). |
| 961 | */ |
| 962 | scb_s->icptcode = ICPT_PROGI; |
| 963 | scb_s->iprcc = PGM_ADDRESSING; |
| 964 | scb_s->pgmilc = 4; |
| 965 | scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4); |
| 966 | } |
| 967 | return rc; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Get or create a vsie page for a scb address. |
| 972 | * |
| 973 | * Returns: - address of a vsie page (cached or new one) |
| 974 | * - NULL if the same scb address is already used by another VCPU |
| 975 | * - ERR_PTR(-ENOMEM) if out of memory |
| 976 | */ |
| 977 | static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr) |
| 978 | { |
| 979 | struct vsie_page *vsie_page; |
| 980 | struct page *page; |
| 981 | int nr_vcpus; |
| 982 | |
| 983 | rcu_read_lock(); |
| 984 | page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9); |
| 985 | rcu_read_unlock(); |
| 986 | if (page) { |
| 987 | if (page_ref_inc_return(page) == 2) |
| 988 | return page_to_virt(page); |
| 989 | page_ref_dec(page); |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * We want at least #online_vcpus shadows, so every VCPU can execute |
| 994 | * the VSIE in parallel. |
| 995 | */ |
| 996 | nr_vcpus = atomic_read(&kvm->online_vcpus); |
| 997 | |
| 998 | mutex_lock(&kvm->arch.vsie.mutex); |
| 999 | if (kvm->arch.vsie.page_count < nr_vcpus) { |
David Hildenbrand | 66b630d | 2015-11-26 14:11:19 +0100 | [diff] [blame] | 1000 | page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 1001 | if (!page) { |
| 1002 | mutex_unlock(&kvm->arch.vsie.mutex); |
| 1003 | return ERR_PTR(-ENOMEM); |
| 1004 | } |
| 1005 | page_ref_inc(page); |
| 1006 | kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page; |
| 1007 | kvm->arch.vsie.page_count++; |
| 1008 | } else { |
| 1009 | /* reuse an existing entry that belongs to nobody */ |
| 1010 | while (true) { |
| 1011 | page = kvm->arch.vsie.pages[kvm->arch.vsie.next]; |
| 1012 | if (page_ref_inc_return(page) == 2) |
| 1013 | break; |
| 1014 | page_ref_dec(page); |
| 1015 | kvm->arch.vsie.next++; |
| 1016 | kvm->arch.vsie.next %= nr_vcpus; |
| 1017 | } |
| 1018 | radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); |
| 1019 | } |
| 1020 | page->index = addr; |
| 1021 | /* double use of the same address */ |
| 1022 | if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) { |
| 1023 | page_ref_dec(page); |
| 1024 | mutex_unlock(&kvm->arch.vsie.mutex); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | mutex_unlock(&kvm->arch.vsie.mutex); |
| 1028 | |
| 1029 | vsie_page = page_to_virt(page); |
| 1030 | memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block)); |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 1031 | release_gmap_shadow(vsie_page); |
David Hildenbrand | 1b7029b | 2015-07-08 13:25:31 +0200 | [diff] [blame] | 1032 | vsie_page->fault_addr = 0; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 1033 | vsie_page->scb_s.ihcpu = 0xffffU; |
| 1034 | return vsie_page; |
| 1035 | } |
| 1036 | |
| 1037 | /* put a vsie page acquired via get_vsie_page */ |
| 1038 | static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page) |
| 1039 | { |
| 1040 | struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT); |
| 1041 | |
| 1042 | page_ref_dec(page); |
| 1043 | } |
| 1044 | |
| 1045 | int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu) |
| 1046 | { |
| 1047 | struct vsie_page *vsie_page; |
| 1048 | unsigned long scb_addr; |
| 1049 | int rc; |
| 1050 | |
| 1051 | vcpu->stat.instruction_sie++; |
| 1052 | if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2)) |
| 1053 | return -EOPNOTSUPP; |
| 1054 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 1055 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 1056 | |
| 1057 | BUILD_BUG_ON(sizeof(struct vsie_page) != 4096); |
| 1058 | scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL); |
| 1059 | |
| 1060 | /* 512 byte alignment */ |
| 1061 | if (unlikely(scb_addr & 0x1ffUL)) |
| 1062 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 1063 | |
| 1064 | if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0)) |
| 1065 | return 0; |
| 1066 | |
| 1067 | vsie_page = get_vsie_page(vcpu->kvm, scb_addr); |
| 1068 | if (IS_ERR(vsie_page)) |
| 1069 | return PTR_ERR(vsie_page); |
| 1070 | else if (!vsie_page) |
| 1071 | /* double use of sie control block - simply do nothing */ |
| 1072 | return 0; |
| 1073 | |
| 1074 | rc = pin_scb(vcpu, vsie_page, scb_addr); |
| 1075 | if (rc) |
| 1076 | goto out_put; |
| 1077 | rc = shadow_scb(vcpu, vsie_page); |
| 1078 | if (rc) |
| 1079 | goto out_unpin_scb; |
| 1080 | rc = pin_blocks(vcpu, vsie_page); |
| 1081 | if (rc) |
| 1082 | goto out_unshadow; |
David Hildenbrand | adbf169 | 2016-05-27 22:03:52 +0200 | [diff] [blame] | 1083 | register_shadow_scb(vcpu, vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 1084 | rc = vsie_run(vcpu, vsie_page); |
David Hildenbrand | adbf169 | 2016-05-27 22:03:52 +0200 | [diff] [blame] | 1085 | unregister_shadow_scb(vcpu); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 1086 | unpin_blocks(vcpu, vsie_page); |
| 1087 | out_unshadow: |
| 1088 | unshadow_scb(vcpu, vsie_page); |
| 1089 | out_unpin_scb: |
| 1090 | unpin_scb(vcpu, vsie_page, scb_addr); |
| 1091 | out_put: |
| 1092 | put_vsie_page(vcpu->kvm, vsie_page); |
| 1093 | |
| 1094 | return rc < 0 ? rc : 0; |
| 1095 | } |
| 1096 | |
| 1097 | /* Init the vsie data structures. To be called when a vm is initialized. */ |
| 1098 | void kvm_s390_vsie_init(struct kvm *kvm) |
| 1099 | { |
| 1100 | mutex_init(&kvm->arch.vsie.mutex); |
| 1101 | INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL); |
| 1102 | } |
| 1103 | |
| 1104 | /* Destroy the vsie data structures. To be called when a vm is destroyed. */ |
| 1105 | void kvm_s390_vsie_destroy(struct kvm *kvm) |
| 1106 | { |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 1107 | struct vsie_page *vsie_page; |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 1108 | struct page *page; |
| 1109 | int i; |
| 1110 | |
| 1111 | mutex_lock(&kvm->arch.vsie.mutex); |
| 1112 | for (i = 0; i < kvm->arch.vsie.page_count; i++) { |
| 1113 | page = kvm->arch.vsie.pages[i]; |
| 1114 | kvm->arch.vsie.pages[i] = NULL; |
David Hildenbrand | 06d68a6 | 2016-04-22 13:50:09 +0200 | [diff] [blame] | 1115 | vsie_page = page_to_virt(page); |
| 1116 | release_gmap_shadow(vsie_page); |
David Hildenbrand | a3508fb | 2015-07-08 13:19:48 +0200 | [diff] [blame] | 1117 | /* free the radix tree entry */ |
| 1118 | radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9); |
| 1119 | __free_page(page); |
| 1120 | } |
| 1121 | kvm->arch.vsie.page_count = 0; |
| 1122 | mutex_unlock(&kvm->arch.vsie.mutex); |
| 1123 | } |
David Hildenbrand | adbf169 | 2016-05-27 22:03:52 +0200 | [diff] [blame] | 1124 | |
| 1125 | void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu) |
| 1126 | { |
| 1127 | struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block); |
| 1128 | |
| 1129 | /* |
| 1130 | * Even if the VCPU lets go of the shadow sie block reference, it is |
| 1131 | * still valid in the cache. So we can safely kick it. |
| 1132 | */ |
| 1133 | if (scb) { |
| 1134 | atomic_or(PROG_BLOCK_SIE, &scb->prog20); |
| 1135 | if (scb->prog0c & PROG_IN_SIE) |
| 1136 | atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags); |
| 1137 | } |
| 1138 | } |