1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * handling diagnose instructions 4 * 5 * Copyright IBM Corp. 2008, 2011 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> 17 #include <asm/pgalloc.h> 18 #include <asm/gmap.h> 19 #include <asm/virtio-ccw.h> 20 #include "kvm-s390.h" 21 #include "trace.h" 22 #include "trace-s390.h" 23 #include "gaccess.h" 24 25 static int diag_release_pages(struct kvm_vcpu *vcpu) 26 { 27 unsigned long start, end; 28 unsigned long prefix = kvm_s390_get_prefix(vcpu); 29 30 start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4]; 31 end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + PAGE_SIZE; 32 vcpu->stat.diagnose_10++; 33 34 if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end 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); 39 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) { 45 gmap_discard(vcpu->arch.gmap, start, end); 46 } else { 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) 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); 58 gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end); 59 } 60 return 0; 61 } 62 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); 79 80 VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx", 81 vcpu->run->s.regs.gprs[rx]); 82 vcpu->stat.diagnose_258++; 83 if (vcpu->run->s.regs.gprs[rx] & 7) 84 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 85 rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm)); 86 if (rc) 87 return kvm_s390_inject_prog_cond(vcpu, rc); 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 */ 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); 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 110 if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr)) 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 */ 125 VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr); 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 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++; 154 kvm_vcpu_on_spin(vcpu, true); 155 return 0; 156 } 157 158 static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu) 159 { 160 struct kvm_vcpu *tcpu; 161 int tid; 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 170 tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid); 171 if (tcpu) 172 kvm_vcpu_yield_to(tcpu); 173 return 0; 174 } 175 176 static int __diag_ipl_functions(struct kvm_vcpu *vcpu) 177 { 178 unsigned int reg = vcpu->arch.sie_block->ipa & 0xf; 179 unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff; 180 181 VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode); 182 vcpu->stat.diagnose_308++; 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: 191 return -EOPNOTSUPP; 192 } 193 194 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 195 kvm_s390_vcpu_stop(vcpu); 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; 200 VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx", 201 vcpu->run->s390_reset_flags); 202 trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags); 203 return -EREMOTE; 204 } 205 206 static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu) 207 { 208 int ret; 209 210 vcpu->stat.diagnose_500++; 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 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 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) 225 * - gpr 4 contains the index on the bus (optionally) 226 */ 227 ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS, 228 vcpu->run->s.regs.gprs[2] & 0xffffffff, 229 8, &vcpu->run->s.regs.gprs[3], 230 vcpu->run->s.regs.gprs[4]); 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. */ 239 return ret < 0 ? ret : 0; 240 } 241 242 int kvm_s390_handle_diag(struct kvm_vcpu *vcpu) 243 { 244 int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff; 245 246 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 247 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 248 249 trace_kvm_s390_handle_diag(vcpu, code); 250 switch (code) { 251 case 0x10: 252 return diag_release_pages(vcpu); 253 case 0x44: 254 return __diag_time_slice_end(vcpu); 255 case 0x9c: 256 return __diag_time_slice_end_directed(vcpu); 257 case 0x258: 258 return __diag_page_ref_service(vcpu); 259 case 0x308: 260 return __diag_ipl_functions(vcpu); 261 case 0x500: 262 return __diag_virtio_hypercall(vcpu); 263 default: 264 return -EOPNOTSUPP; 265 } 266 } 267