blob: 481e1fe6095f3121d9d9cf135b95a6810054cf70 [file] [log] [blame]
Greg Kroah-Hartmand809aa22017-11-24 15:00:33 +01001// SPDX-License-Identifier: GPL-2.0
Christian Borntraegere28acfe2008-03-25 18:47:34 +01002/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * handling diagnose instructions
Christian Borntraegere28acfe2008-03-25 18:47:34 +01004 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 2008, 2011
Christian Borntraegere28acfe2008-03-25 18:47:34 +01006 *
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 Schwidefskydeedabb2013-05-21 17:29:52 +020017#include <asm/pgalloc.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010018#include <asm/gmap.h>
Cornelia Huck10ccaa12013-02-28 12:33:21 +010019#include <asm/virtio-ccw.h>
Christian Borntraegere28acfe2008-03-25 18:47:34 +010020#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020021#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020022#include "trace-s390.h"
Dominik Dingel3c038e62013-10-07 17:11:48 +020023#include "gaccess.h"
Christian Borntraegere28acfe2008-03-25 18:47:34 +010024
Christian Borntraeger388186b2011-10-30 15:17:03 +010025static int diag_release_pages(struct kvm_vcpu *vcpu)
26{
27 unsigned long start, end;
Michael Muellerfda902c2014-05-13 16:58:30 +020028 unsigned long prefix = kvm_s390_get_prefix(vcpu);
Christian Borntraeger388186b2011-10-30 15:17:03 +010029
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010030 start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
Heiko Carstens58cdf5e2017-07-05 07:37:14 +020031 end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + PAGE_SIZE;
Christian Borntraeger175a5c92015-07-07 15:19:32 +020032 vcpu->stat.diagnose_10++;
Christian Borntraeger388186b2011-10-30 15:17:03 +010033
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020034 if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
Christian Borntraeger388186b2011-10-30 15:17:03 +010035 || 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 Borntraeger388186b2011-10-30 15:17:03 +010039
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020040 /*
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 Schwidefsky6e0a0432014-04-29 09:34:41 +020045 gmap_discard(vcpu->arch.gmap, start, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010046 } else {
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020047 /*
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 Carstens58cdf5e2017-07-05 07:37:14 +020055 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 Borntraegerf7a960a2014-09-03 21:23:13 +020058 gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010059 }
60 return 0;
61}
62
Dominik Dingel3c038e62013-10-07 17:11:48 +020063static 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 Dingel3c038e62013-10-07 17:11:48 +020079
Christian Borntraeger15e8b5d2015-07-09 13:43:41 +020080 VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
81 vcpu->run->s.regs.gprs[rx]);
Christian Borntraeger175a5c92015-07-07 15:19:32 +020082 vcpu->stat.diagnose_258++;
Dominik Dingel3c038e62013-10-07 17:11:48 +020083 if (vcpu->run->s.regs.gprs[rx] & 7)
84 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030085 rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
Heiko Carstens81480cc2014-01-01 16:36:07 +010086 if (rc)
87 return kvm_s390_inject_prog_cond(vcpu, rc);
Dominik Dingel3c038e62013-10-07 17:11:48 +020088 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 Borntraegerab7090a2015-07-16 10:24:07 +020093 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 Dingel3c038e62013-10-07 17:11:48 +020096 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 Carstens81480cc2014-01-01 16:36:07 +0100110 if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
Dominik Dingel3c038e62013-10-07 17:11:48 +0200111 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 Borntraegerab7090a2015-07-16 10:24:07 +0200125 VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200126 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 Borntraegere28acfe2008-03-25 18:47:34 +0100150static 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)0546c632017-08-08 12:05:34 +0800154 kvm_vcpu_on_spin(vcpu, true);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100155 return 0;
156}
157
Konstantin Weitz41628d32012-04-25 15:30:38 +0200158static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
159{
Konstantin Weitz41628d32012-04-25 15:30:38 +0200160 struct kvm_vcpu *tcpu;
161 int tid;
Konstantin Weitz41628d32012-04-25 15:30:38 +0200162
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 Hildenbrande09fefd2015-11-05 09:03:50 +0100170 tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid);
171 if (tcpu)
172 kvm_vcpu_yield_to(tcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200173 return 0;
174}
175
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100176static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
177{
178 unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100179 unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100180
Christian Borntraeger15e8b5d2015-07-09 13:43:41 +0200181 VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
Christian Borntraeger175a5c92015-07-07 15:19:32 +0200182 vcpu->stat.diagnose_308++;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100183 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 Carstensb8e660b2010-02-26 22:37:41 +0100191 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100192 }
193
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200194 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
195 kvm_s390_vcpu_stop(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100196 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 Carstens33e19112009-01-09 12:14:56 +0100200 VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100201 vcpu->run->s390_reset_flags);
Cornelia Huckade38c32012-07-23 17:20:30 +0200202 trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100203 return -EREMOTE;
204}
205
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100206static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
207{
Thomas Huth800c1062013-09-12 10:33:45 +0200208 int ret;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100209
Christian Borntraeger175a5c92015-07-07 15:19:32 +0200210 vcpu->stat.diagnose_500++;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100211 /* 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 Borntraeger1bb78d12016-06-07 09:57:08 +0200216 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 Huck10ccaa12013-02-28 12:33:21 +0100221 /*
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 Huck85dfe872013-07-03 16:30:54 +0200225 * - gpr 4 contains the index on the bus (optionally)
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100226 */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000227 ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
Dominik Dingelff1f3cb2013-12-09 18:30:01 +0100228 vcpu->run->s.regs.gprs[2] & 0xffffffff,
Cornelia Huck85dfe872013-07-03 16:30:54 +0200229 8, &vcpu->run->s.regs.gprs[3],
230 vcpu->run->s.regs.gprs[4]);
Cornelia Huck85dfe872013-07-03 16:30:54 +0200231
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 Huck10ccaa12013-02-28 12:33:21 +0100239 return ret < 0 ? ret : 0;
240}
241
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100242int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
243{
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300244 int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100245
Thomas Huth93e17502013-06-20 17:22:02 +0200246 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
247 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
248
Cornelia Huck5786fff2012-07-23 17:20:29 +0200249 trace_kvm_s390_handle_diag(vcpu, code);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100250 switch (code) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100251 case 0x10:
252 return diag_release_pages(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100253 case 0x44:
254 return __diag_time_slice_end(vcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200255 case 0x9c:
256 return __diag_time_slice_end_directed(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200257 case 0x258:
258 return __diag_page_ref_service(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100259 case 0x308:
260 return __diag_ipl_functions(vcpu);
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100261 case 0x500:
262 return __diag_virtio_hypercall(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100263 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100264 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100265 }
266}