Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * handling diagnose instructions |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 3 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2008, 2011 |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 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): Carsten Otte <cotte@de.ibm.com> |
| 11 | * Christian Borntraeger <borntraeger@de.ibm.com> |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kvm.h> |
| 15 | #include <linux/kvm_host.h> |
Martin Schwidefsky | deedabb | 2013-05-21 17:29:52 +0200 | [diff] [blame] | 16 | #include <asm/pgalloc.h> |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 17 | #include <asm/virtio-ccw.h> |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 18 | #include "kvm-s390.h" |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 19 | #include "trace.h" |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 20 | #include "trace-s390.h" |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 21 | #include "gaccess.h" |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 22 | |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 23 | static int diag_release_pages(struct kvm_vcpu *vcpu) |
| 24 | { |
| 25 | unsigned long start, end; |
Michael Mueller | fda902c | 2014-05-13 16:58:30 +0200 | [diff] [blame] | 26 | unsigned long prefix = kvm_s390_get_prefix(vcpu); |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 27 | |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 28 | start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4]; |
| 29 | end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096; |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 30 | |
Christian Borntraeger | f7a960a | 2014-09-03 21:23:13 +0200 | [diff] [blame] | 31 | if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 32 | || start < 2 * PAGE_SIZE) |
| 33 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 34 | |
| 35 | VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end); |
| 36 | vcpu->stat.diagnose_10++; |
| 37 | |
Christian Borntraeger | f7a960a | 2014-09-03 21:23:13 +0200 | [diff] [blame] | 38 | /* |
| 39 | * We checked for start >= end above, so lets check for the |
| 40 | * fast path (no prefix swap page involved) |
| 41 | */ |
| 42 | if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) { |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 43 | gmap_discard(vcpu->arch.gmap, start, end); |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 44 | } else { |
Christian Borntraeger | f7a960a | 2014-09-03 21:23:13 +0200 | [diff] [blame] | 45 | /* |
| 46 | * This is slow path. gmap_discard will check for start |
| 47 | * so lets split this into before prefix, prefix, after |
| 48 | * prefix and let gmap_discard make some of these calls |
| 49 | * NOPs. |
| 50 | */ |
| 51 | gmap_discard(vcpu->arch.gmap, start, prefix); |
| 52 | if (start <= prefix) |
| 53 | gmap_discard(vcpu->arch.gmap, 0, 4096); |
| 54 | if (end > prefix + 4096) |
| 55 | gmap_discard(vcpu->arch.gmap, 4096, 8192); |
| 56 | gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end); |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 61 | static int __diag_page_ref_service(struct kvm_vcpu *vcpu) |
| 62 | { |
| 63 | struct prs_parm { |
| 64 | u16 code; |
| 65 | u16 subcode; |
| 66 | u16 parm_len; |
| 67 | u16 parm_version; |
| 68 | u64 token_addr; |
| 69 | u64 select_mask; |
| 70 | u64 compare_mask; |
| 71 | u64 zarch; |
| 72 | }; |
| 73 | struct prs_parm parm; |
| 74 | int rc; |
| 75 | u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4; |
| 76 | u16 ry = (vcpu->arch.sie_block->ipa & 0x0f); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 77 | |
| 78 | if (vcpu->run->s.regs.gprs[rx] & 7) |
| 79 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 80 | rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm)); |
Heiko Carstens | 81480cc | 2014-01-01 16:36:07 +0100 | [diff] [blame] | 81 | if (rc) |
| 82 | return kvm_s390_inject_prog_cond(vcpu, rc); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 83 | if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258) |
| 84 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 85 | |
| 86 | switch (parm.subcode) { |
| 87 | case 0: /* TOKEN */ |
| 88 | if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) { |
| 89 | /* |
| 90 | * If the pagefault handshake is already activated, |
| 91 | * the token must not be changed. We have to return |
| 92 | * decimal 8 instead, as mandated in SC24-6084. |
| 93 | */ |
| 94 | vcpu->run->s.regs.gprs[ry] = 8; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | if ((parm.compare_mask & parm.select_mask) != parm.compare_mask || |
| 99 | parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL) |
| 100 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 101 | |
Heiko Carstens | 81480cc | 2014-01-01 16:36:07 +0100 | [diff] [blame] | 102 | if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr)) |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 103 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
| 104 | |
| 105 | vcpu->arch.pfault_token = parm.token_addr; |
| 106 | vcpu->arch.pfault_select = parm.select_mask; |
| 107 | vcpu->arch.pfault_compare = parm.compare_mask; |
| 108 | vcpu->run->s.regs.gprs[ry] = 0; |
| 109 | rc = 0; |
| 110 | break; |
| 111 | case 1: /* |
| 112 | * CANCEL |
| 113 | * Specification allows to let already pending tokens survive |
| 114 | * the cancel, therefore to reduce code complexity, we assume |
| 115 | * all outstanding tokens are already pending. |
| 116 | */ |
| 117 | if (parm.token_addr || parm.select_mask || |
| 118 | parm.compare_mask || parm.zarch) |
| 119 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 120 | |
| 121 | vcpu->run->s.regs.gprs[ry] = 0; |
| 122 | /* |
| 123 | * If the pfault handling was not established or is already |
| 124 | * canceled SC24-6084 requests to return decimal 4. |
| 125 | */ |
| 126 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
| 127 | vcpu->run->s.regs.gprs[ry] = 4; |
| 128 | else |
| 129 | vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; |
| 130 | |
| 131 | rc = 0; |
| 132 | break; |
| 133 | default: |
| 134 | rc = -EOPNOTSUPP; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | return rc; |
| 139 | } |
| 140 | |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 141 | static int __diag_time_slice_end(struct kvm_vcpu *vcpu) |
| 142 | { |
| 143 | VCPU_EVENT(vcpu, 5, "%s", "diag time slice end"); |
| 144 | vcpu->stat.diagnose_44++; |
Christian Borntraeger | 8733ac3 | 2012-04-25 15:30:39 +0200 | [diff] [blame] | 145 | kvm_vcpu_on_spin(vcpu); |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Konstantin Weitz | 41628d3 | 2012-04-25 15:30:38 +0200 | [diff] [blame] | 149 | static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu) |
| 150 | { |
| 151 | struct kvm *kvm = vcpu->kvm; |
| 152 | struct kvm_vcpu *tcpu; |
| 153 | int tid; |
| 154 | int i; |
| 155 | |
| 156 | tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4]; |
| 157 | vcpu->stat.diagnose_9c++; |
| 158 | VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid); |
| 159 | |
| 160 | if (tid == vcpu->vcpu_id) |
| 161 | return 0; |
| 162 | |
| 163 | kvm_for_each_vcpu(i, tcpu, kvm) |
| 164 | if (tcpu->vcpu_id == tid) { |
| 165 | kvm_vcpu_yield_to(tcpu); |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 172 | static int __diag_ipl_functions(struct kvm_vcpu *vcpu) |
| 173 | { |
| 174 | unsigned int reg = vcpu->arch.sie_block->ipa & 0xf; |
Christian Borntraeger | 5a32c1a | 2012-01-11 11:20:32 +0100 | [diff] [blame] | 175 | unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff; |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 176 | |
| 177 | VCPU_EVENT(vcpu, 5, "diag ipl functions, subcode %lx", subcode); |
| 178 | switch (subcode) { |
| 179 | case 3: |
| 180 | vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR; |
| 181 | break; |
| 182 | case 4: |
| 183 | vcpu->run->s390_reset_flags = 0; |
| 184 | break; |
| 185 | default: |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 186 | return -EOPNOTSUPP; |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 187 | } |
| 188 | |
David Hildenbrand | 6352e4d | 2014-04-10 17:35:00 +0200 | [diff] [blame] | 189 | if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) |
| 190 | kvm_s390_vcpu_stop(vcpu); |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 191 | vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM; |
| 192 | vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL; |
| 193 | vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT; |
| 194 | vcpu->run->exit_reason = KVM_EXIT_S390_RESET; |
Heiko Carstens | 33e1911 | 2009-01-09 12:14:56 +0100 | [diff] [blame] | 195 | VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx", |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 196 | vcpu->run->s390_reset_flags); |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 197 | trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags); |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 198 | return -EREMOTE; |
| 199 | } |
| 200 | |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 201 | static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu) |
| 202 | { |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 203 | int ret; |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 204 | |
| 205 | /* No virtio-ccw notification? Get out quickly. */ |
| 206 | if (!vcpu->kvm->arch.css_support || |
| 207 | (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY)) |
| 208 | return -EOPNOTSUPP; |
| 209 | |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 210 | /* |
| 211 | * The layout is as follows: |
| 212 | * - gpr 2 contains the subchannel id (passed as addr) |
| 213 | * - gpr 3 contains the virtqueue index (passed as datamatch) |
Cornelia Huck | 85dfe87 | 2013-07-03 16:30:54 +0200 | [diff] [blame] | 214 | * - gpr 4 contains the index on the bus (optionally) |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 215 | */ |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 216 | ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS, |
Dominik Dingel | ff1f3cb | 2013-12-09 18:30:01 +0100 | [diff] [blame] | 217 | vcpu->run->s.regs.gprs[2] & 0xffffffff, |
Cornelia Huck | 85dfe87 | 2013-07-03 16:30:54 +0200 | [diff] [blame] | 218 | 8, &vcpu->run->s.regs.gprs[3], |
| 219 | vcpu->run->s.regs.gprs[4]); |
Cornelia Huck | 85dfe87 | 2013-07-03 16:30:54 +0200 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * Return cookie in gpr 2, but don't overwrite the register if the |
| 223 | * diagnose will be handled by userspace. |
| 224 | */ |
| 225 | if (ret != -EOPNOTSUPP) |
| 226 | vcpu->run->s.regs.gprs[2] = ret; |
| 227 | /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */ |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 228 | return ret < 0 ? ret : 0; |
| 229 | } |
| 230 | |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 231 | int kvm_s390_handle_diag(struct kvm_vcpu *vcpu) |
| 232 | { |
Alexander Yarygin | 8ae04b8 | 2015-01-19 13:24:51 +0300 | [diff] [blame] | 233 | int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff; |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 234 | |
Thomas Huth | 93e1750 | 2013-06-20 17:22:02 +0200 | [diff] [blame] | 235 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
| 236 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
| 237 | |
Cornelia Huck | 5786fff | 2012-07-23 17:20:29 +0200 | [diff] [blame] | 238 | trace_kvm_s390_handle_diag(vcpu, code); |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 239 | switch (code) { |
Christian Borntraeger | 388186b | 2011-10-30 15:17:03 +0100 | [diff] [blame] | 240 | case 0x10: |
| 241 | return diag_release_pages(vcpu); |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 242 | case 0x44: |
| 243 | return __diag_time_slice_end(vcpu); |
Konstantin Weitz | 41628d3 | 2012-04-25 15:30:38 +0200 | [diff] [blame] | 244 | case 0x9c: |
| 245 | return __diag_time_slice_end_directed(vcpu); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 246 | case 0x258: |
| 247 | return __diag_page_ref_service(vcpu); |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 248 | case 0x308: |
| 249 | return __diag_ipl_functions(vcpu); |
Cornelia Huck | 10ccaa1 | 2013-02-28 12:33:21 +0100 | [diff] [blame] | 250 | case 0x500: |
| 251 | return __diag_virtio_hypercall(vcpu); |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 252 | default: |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 253 | return -EOPNOTSUPP; |
Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 254 | } |
| 255 | } |