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