blob: 2a32438e09cebaa698a8935c4aec03bf1f2cbc58 [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 *
Janosch Frankfe28c7862019-05-15 13:24:30 +02005 * Copyright IBM Corp. 2008, 2020
Christian Borntraegere28acfe2008-03-25 18:47:34 +01006 *
Christian Borntraegere28acfe2008-03-25 18:47:34 +01007 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
9 */
10
11#include <linux/kvm.h>
12#include <linux/kvm_host.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010013#include <asm/gmap.h>
Cornelia Huck10ccaa12013-02-28 12:33:21 +010014#include <asm/virtio-ccw.h>
Christian Borntraegere28acfe2008-03-25 18:47:34 +010015#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020016#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020017#include "trace-s390.h"
Dominik Dingel3c038e62013-10-07 17:11:48 +020018#include "gaccess.h"
Christian Borntraegere28acfe2008-03-25 18:47:34 +010019
Christian Borntraeger388186b2011-10-30 15:17:03 +010020static int diag_release_pages(struct kvm_vcpu *vcpu)
21{
22 unsigned long start, end;
Michael Muellerfda902c2014-05-13 16:58:30 +020023 unsigned long prefix = kvm_s390_get_prefix(vcpu);
Christian Borntraeger388186b2011-10-30 15:17:03 +010024
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010025 start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
Heiko Carstens58cdf5e2017-07-05 07:37:14 +020026 end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + PAGE_SIZE;
Christian Borntraegerbb000f642021-07-26 17:01:08 +020027 vcpu->stat.instruction_diagnose_10++;
Christian Borntraeger388186b2011-10-30 15:17:03 +010028
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020029 if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
Christian Borntraeger388186b2011-10-30 15:17:03 +010030 || start < 2 * PAGE_SIZE)
31 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
32
33 VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010034
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020035 /*
36 * We checked for start >= end above, so lets check for the
37 * fast path (no prefix swap page involved)
38 */
39 if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +020040 gmap_discard(vcpu->arch.gmap, start, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010041 } else {
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020042 /*
43 * This is slow path. gmap_discard will check for start
44 * so lets split this into before prefix, prefix, after
45 * prefix and let gmap_discard make some of these calls
46 * NOPs.
47 */
48 gmap_discard(vcpu->arch.gmap, start, prefix);
49 if (start <= prefix)
Heiko Carstens58cdf5e2017-07-05 07:37:14 +020050 gmap_discard(vcpu->arch.gmap, 0, PAGE_SIZE);
51 if (end > prefix + PAGE_SIZE)
52 gmap_discard(vcpu->arch.gmap, PAGE_SIZE, 2 * PAGE_SIZE);
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020053 gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010054 }
55 return 0;
56}
57
Dominik Dingel3c038e62013-10-07 17:11:48 +020058static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
59{
60 struct prs_parm {
61 u16 code;
62 u16 subcode;
63 u16 parm_len;
64 u16 parm_version;
65 u64 token_addr;
66 u64 select_mask;
67 u64 compare_mask;
68 u64 zarch;
69 };
70 struct prs_parm parm;
71 int rc;
72 u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
73 u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
Dominik Dingel3c038e62013-10-07 17:11:48 +020074
Christian Borntraeger15e8b5d2015-07-09 13:43:41 +020075 VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
76 vcpu->run->s.regs.gprs[rx]);
Christian Borntraegerbb000f642021-07-26 17:01:08 +020077 vcpu->stat.instruction_diagnose_258++;
Dominik Dingel3c038e62013-10-07 17:11:48 +020078 if (vcpu->run->s.regs.gprs[rx] & 7)
79 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030080 rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
Heiko Carstens81480cc2014-01-01 16:36:07 +010081 if (rc)
82 return kvm_s390_inject_prog_cond(vcpu, rc);
Dominik Dingel3c038e62013-10-07 17:11:48 +020083 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 */
Christian Borntraegerab7090a2015-07-16 10:24:07 +020088 VCPU_EVENT(vcpu, 3, "pageref token addr 0x%llx "
89 "select mask 0x%llx compare mask 0x%llx",
90 parm.token_addr, parm.select_mask, parm.compare_mask);
Dominik Dingel3c038e62013-10-07 17:11:48 +020091 if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
92 /*
93 * If the pagefault handshake is already activated,
94 * the token must not be changed. We have to return
95 * decimal 8 instead, as mandated in SC24-6084.
96 */
97 vcpu->run->s.regs.gprs[ry] = 8;
98 return 0;
99 }
100
101 if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
102 parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
103 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
104
Sean Christopherson9e7325a2024-02-15 15:29:03 +0000105 if (!kvm_is_gpa_in_memslot(vcpu->kvm, parm.token_addr))
Dominik Dingel3c038e62013-10-07 17:11:48 +0200106 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
107
108 vcpu->arch.pfault_token = parm.token_addr;
109 vcpu->arch.pfault_select = parm.select_mask;
110 vcpu->arch.pfault_compare = parm.compare_mask;
111 vcpu->run->s.regs.gprs[ry] = 0;
112 rc = 0;
113 break;
114 case 1: /*
115 * CANCEL
116 * Specification allows to let already pending tokens survive
117 * the cancel, therefore to reduce code complexity, we assume
118 * all outstanding tokens are already pending.
119 */
Christian Borntraegerab7090a2015-07-16 10:24:07 +0200120 VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200121 if (parm.token_addr || parm.select_mask ||
122 parm.compare_mask || parm.zarch)
123 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
124
125 vcpu->run->s.regs.gprs[ry] = 0;
126 /*
127 * If the pfault handling was not established or is already
128 * canceled SC24-6084 requests to return decimal 4.
129 */
130 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
131 vcpu->run->s.regs.gprs[ry] = 4;
132 else
133 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
134
135 rc = 0;
136 break;
137 default:
138 rc = -EOPNOTSUPP;
139 break;
140 }
141
142 return rc;
143}
144
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100145static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
146{
147 VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
Christian Borntraegerbb000f642021-07-26 17:01:08 +0200148 vcpu->stat.instruction_diagnose_44++;
Longpeng(Mike)0546c632017-08-08 12:05:34 +0800149 kvm_vcpu_on_spin(vcpu, true);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100150 return 0;
151}
152
Pierre Morel87e28a12020-09-07 15:26:07 +0200153static int forward_cnt;
154static unsigned long cur_slice;
155
156static int diag9c_forwarding_overrun(void)
157{
158 /* Reset the count on a new slice */
159 if (time_after(jiffies, cur_slice)) {
160 cur_slice = jiffies;
161 forward_cnt = diag9c_forwarding_hz / HZ;
162 }
163 return forward_cnt-- <= 0 ? 1 : 0;
164}
165
Konstantin Weitz41628d32012-04-25 15:30:38 +0200166static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
167{
Konstantin Weitz41628d32012-04-25 15:30:38 +0200168 struct kvm_vcpu *tcpu;
Christian Borntraeger0bc380b2023-05-15 10:42:34 +0200169 int tcpu_cpu;
Konstantin Weitz41628d32012-04-25 15:30:38 +0200170 int tid;
Konstantin Weitz41628d32012-04-25 15:30:38 +0200171
172 tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
Christian Borntraegerbb000f642021-07-26 17:01:08 +0200173 vcpu->stat.instruction_diagnose_9c++;
Konstantin Weitz41628d32012-04-25 15:30:38 +0200174
Christian Borntraeger8474e5c2019-02-15 13:47:20 +0100175 /* yield to self */
Konstantin Weitz41628d32012-04-25 15:30:38 +0200176 if (tid == vcpu->vcpu_id)
Christian Borntraeger8474e5c2019-02-15 13:47:20 +0100177 goto no_yield;
Konstantin Weitz41628d32012-04-25 15:30:38 +0200178
Christian Borntraeger8474e5c2019-02-15 13:47:20 +0100179 /* yield to invalid */
David Hildenbrande09fefd2015-11-05 09:03:50 +0100180 tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid);
Christian Borntraeger8474e5c2019-02-15 13:47:20 +0100181 if (!tcpu)
182 goto no_yield;
183
Pierre Morel87e28a12020-09-07 15:26:07 +0200184 /* target guest VCPU already running */
Christian Borntraeger0bc380b2023-05-15 10:42:34 +0200185 tcpu_cpu = READ_ONCE(tcpu->cpu);
186 if (tcpu_cpu >= 0) {
Pierre Morel87e28a12020-09-07 15:26:07 +0200187 if (!diag9c_forwarding_hz || diag9c_forwarding_overrun())
188 goto no_yield;
189
190 /* target host CPU already running */
Christian Borntraeger0bc380b2023-05-15 10:42:34 +0200191 if (!vcpu_is_preempted(tcpu_cpu))
Pierre Morel87e28a12020-09-07 15:26:07 +0200192 goto no_yield;
Christian Borntraeger0bc380b2023-05-15 10:42:34 +0200193 smp_yield_cpu(tcpu_cpu);
Pierre Morel87e28a12020-09-07 15:26:07 +0200194 VCPU_EVENT(vcpu, 5,
195 "diag time slice end directed to %d: yield forwarded",
196 tid);
Christian Borntraegerbb000f642021-07-26 17:01:08 +0200197 vcpu->stat.diag_9c_forward++;
Pierre Morel87e28a12020-09-07 15:26:07 +0200198 return 0;
199 }
Christian Borntraegerc7b7de62019-02-15 13:47:20 +0100200
Christian Borntraeger8474e5c2019-02-15 13:47:20 +0100201 if (kvm_vcpu_yield_to(tcpu) <= 0)
202 goto no_yield;
203
204 VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d: done", tid);
205 return 0;
206no_yield:
207 VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d: ignored", tid);
Christian Borntraegerbb000f642021-07-26 17:01:08 +0200208 vcpu->stat.diag_9c_ignored++;
Konstantin Weitz41628d32012-04-25 15:30:38 +0200209 return 0;
210}
211
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100212static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
213{
214 unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100215 unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100216
Christian Borntraeger15e8b5d2015-07-09 13:43:41 +0200217 VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
Christian Borntraegerbb000f642021-07-26 17:01:08 +0200218 vcpu->stat.instruction_diagnose_308++;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100219 switch (subcode) {
220 case 3:
221 vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
222 break;
223 case 4:
224 vcpu->run->s390_reset_flags = 0;
225 break;
226 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100227 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100228 }
229
Janosch Frankfe28c7862019-05-15 13:24:30 +0200230 /*
231 * no need to check the return value of vcpu_stop as it can only have
232 * an error for protvirt, but protvirt means user cpu state
233 */
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200234 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
235 kvm_s390_vcpu_stop(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100236 vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
237 vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
238 vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
239 vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
Heiko Carstens33e19112009-01-09 12:14:56 +0100240 VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100241 vcpu->run->s390_reset_flags);
Cornelia Huckade38c32012-07-23 17:20:30 +0200242 trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100243 return -EREMOTE;
244}
245
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100246static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
247{
Thomas Huth800c1062013-09-12 10:33:45 +0200248 int ret;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100249
Christian Borntraegerbb000f642021-07-26 17:01:08 +0200250 vcpu->stat.instruction_diagnose_500++;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100251 /* No virtio-ccw notification? Get out quickly. */
252 if (!vcpu->kvm->arch.css_support ||
253 (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
254 return -EOPNOTSUPP;
255
Christian Borntraeger1bb78d12016-06-07 09:57:08 +0200256 VCPU_EVENT(vcpu, 4, "diag 0x500 schid 0x%8.8x queue 0x%x cookie 0x%llx",
257 (u32) vcpu->run->s.regs.gprs[2],
258 (u32) vcpu->run->s.regs.gprs[3],
259 vcpu->run->s.regs.gprs[4]);
260
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100261 /*
262 * The layout is as follows:
263 * - gpr 2 contains the subchannel id (passed as addr)
264 * - gpr 3 contains the virtqueue index (passed as datamatch)
Cornelia Huck85dfe872013-07-03 16:30:54 +0200265 * - gpr 4 contains the index on the bus (optionally)
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100266 */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000267 ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
Dominik Dingelff1f3cb2013-12-09 18:30:01 +0100268 vcpu->run->s.regs.gprs[2] & 0xffffffff,
Cornelia Huck85dfe872013-07-03 16:30:54 +0200269 8, &vcpu->run->s.regs.gprs[3],
270 vcpu->run->s.regs.gprs[4]);
Cornelia Huck85dfe872013-07-03 16:30:54 +0200271
272 /*
273 * Return cookie in gpr 2, but don't overwrite the register if the
274 * diagnose will be handled by userspace.
275 */
276 if (ret != -EOPNOTSUPP)
277 vcpu->run->s.regs.gprs[2] = ret;
278 /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100279 return ret < 0 ? ret : 0;
280}
281
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100282int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
283{
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300284 int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100285
Thomas Huth93e17502013-06-20 17:22:02 +0200286 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
287 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
288
Cornelia Huck5786fff2012-07-23 17:20:29 +0200289 trace_kvm_s390_handle_diag(vcpu, code);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100290 switch (code) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100291 case 0x10:
292 return diag_release_pages(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100293 case 0x44:
294 return __diag_time_slice_end(vcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200295 case 0x9c:
296 return __diag_time_slice_end_directed(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200297 case 0x258:
298 return __diag_page_ref_service(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100299 case 0x308:
300 return __diag_ipl_functions(vcpu);
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100301 case 0x500:
302 return __diag_virtio_hypercall(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100303 default:
Christian Borntraegerbb000f642021-07-26 17:01:08 +0200304 vcpu->stat.instruction_diagnose_other++;
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100305 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100306 }
307}