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