1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2e28acfeaSChristian Borntraeger /*
3a53c8fabSHeiko Carstens * handling diagnose instructions
4e28acfeaSChristian Borntraeger *
5fe28c786SJanosch Frank * Copyright IBM Corp. 2008, 2020
6e28acfeaSChristian Borntraeger *
7e28acfeaSChristian Borntraeger * Author(s): Carsten Otte <cotte@de.ibm.com>
8e28acfeaSChristian Borntraeger * Christian Borntraeger <borntraeger@de.ibm.com>
9e28acfeaSChristian Borntraeger */
10e28acfeaSChristian Borntraeger
11e28acfeaSChristian Borntraeger #include <linux/kvm.h>
12e28acfeaSChristian Borntraeger #include <linux/kvm_host.h>
131e133ab2SMartin Schwidefsky #include <asm/gmap.h>
1410ccaa1eSCornelia Huck #include <asm/virtio-ccw.h>
15e28acfeaSChristian Borntraeger #include "kvm-s390.h"
165786fffaSCornelia Huck #include "trace.h"
17ade38c31SCornelia Huck #include "trace-s390.h"
183c038e6bSDominik Dingel #include "gaccess.h"
19e28acfeaSChristian Borntraeger
diag_release_pages(struct kvm_vcpu * vcpu)20388186bcSChristian Borntraeger static int diag_release_pages(struct kvm_vcpu *vcpu)
21388186bcSChristian Borntraeger {
22388186bcSChristian Borntraeger unsigned long start, end;
23fda902cbSMichael Mueller unsigned long prefix = kvm_s390_get_prefix(vcpu);
24388186bcSChristian Borntraeger
255a32c1afSChristian Borntraeger start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
2658cdf5ebSHeiko Carstens end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + PAGE_SIZE;
27*bb000f64SChristian Borntraeger vcpu->stat.instruction_diagnose_10++;
28388186bcSChristian Borntraeger
29f7a960afSChristian Borntraeger if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
30388186bcSChristian Borntraeger || start < 2 * PAGE_SIZE)
31388186bcSChristian Borntraeger return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
32388186bcSChristian Borntraeger
33388186bcSChristian Borntraeger VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
34388186bcSChristian Borntraeger
35f7a960afSChristian Borntraeger /*
36f7a960afSChristian Borntraeger * We checked for start >= end above, so lets check for the
37f7a960afSChristian Borntraeger * fast path (no prefix swap page involved)
38f7a960afSChristian Borntraeger */
39f7a960afSChristian Borntraeger if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
406e0a0431SMartin Schwidefsky gmap_discard(vcpu->arch.gmap, start, end);
41388186bcSChristian Borntraeger } else {
42f7a960afSChristian Borntraeger /*
43f7a960afSChristian Borntraeger * This is slow path. gmap_discard will check for start
44f7a960afSChristian Borntraeger * so lets split this into before prefix, prefix, after
45f7a960afSChristian Borntraeger * prefix and let gmap_discard make some of these calls
46f7a960afSChristian Borntraeger * NOPs.
47f7a960afSChristian Borntraeger */
486e0a0431SMartin Schwidefsky gmap_discard(vcpu->arch.gmap, start, prefix);
49f7a960afSChristian Borntraeger if (start <= prefix)
5058cdf5ebSHeiko Carstens gmap_discard(vcpu->arch.gmap, 0, PAGE_SIZE);
5158cdf5ebSHeiko Carstens if (end > prefix + PAGE_SIZE)
5258cdf5ebSHeiko Carstens gmap_discard(vcpu->arch.gmap, PAGE_SIZE, 2 * PAGE_SIZE);
53f7a960afSChristian Borntraeger gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
54388186bcSChristian Borntraeger }
55388186bcSChristian Borntraeger return 0;
56388186bcSChristian Borntraeger }
57388186bcSChristian Borntraeger
__diag_page_ref_service(struct kvm_vcpu * vcpu)583c038e6bSDominik Dingel static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
593c038e6bSDominik Dingel {
603c038e6bSDominik Dingel struct prs_parm {
613c038e6bSDominik Dingel u16 code;
623c038e6bSDominik Dingel u16 subcode;
633c038e6bSDominik Dingel u16 parm_len;
643c038e6bSDominik Dingel u16 parm_version;
653c038e6bSDominik Dingel u64 token_addr;
663c038e6bSDominik Dingel u64 select_mask;
673c038e6bSDominik Dingel u64 compare_mask;
683c038e6bSDominik Dingel u64 zarch;
693c038e6bSDominik Dingel };
703c038e6bSDominik Dingel struct prs_parm parm;
713c038e6bSDominik Dingel int rc;
723c038e6bSDominik Dingel u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
733c038e6bSDominik Dingel u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
743c038e6bSDominik Dingel
7515e8b5daSChristian Borntraeger VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
7615e8b5daSChristian Borntraeger vcpu->run->s.regs.gprs[rx]);
77*bb000f64SChristian Borntraeger vcpu->stat.instruction_diagnose_258++;
783c038e6bSDominik Dingel if (vcpu->run->s.regs.gprs[rx] & 7)
793c038e6bSDominik Dingel return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
808ae04b8fSAlexander Yarygin rc = read_guest_real(vcpu, vcpu->run->s.regs.gprs[rx], &parm, sizeof(parm));
8181480cc1SHeiko Carstens if (rc)
8281480cc1SHeiko Carstens return kvm_s390_inject_prog_cond(vcpu, rc);
833c038e6bSDominik Dingel if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
843c038e6bSDominik Dingel return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
853c038e6bSDominik Dingel
863c038e6bSDominik Dingel switch (parm.subcode) {
873c038e6bSDominik Dingel case 0: /* TOKEN */
88ab7090a6SChristian Borntraeger VCPU_EVENT(vcpu, 3, "pageref token addr 0x%llx "
89ab7090a6SChristian Borntraeger "select mask 0x%llx compare mask 0x%llx",
90ab7090a6SChristian Borntraeger parm.token_addr, parm.select_mask, parm.compare_mask);
913c038e6bSDominik Dingel if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
923c038e6bSDominik Dingel /*
933c038e6bSDominik Dingel * If the pagefault handshake is already activated,
943c038e6bSDominik Dingel * the token must not be changed. We have to return
953c038e6bSDominik Dingel * decimal 8 instead, as mandated in SC24-6084.
963c038e6bSDominik Dingel */
973c038e6bSDominik Dingel vcpu->run->s.regs.gprs[ry] = 8;
983c038e6bSDominik Dingel return 0;
993c038e6bSDominik Dingel }
1003c038e6bSDominik Dingel
1013c038e6bSDominik Dingel if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
1023c038e6bSDominik Dingel parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
1033c038e6bSDominik Dingel return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1043c038e6bSDominik Dingel
10581480cc1SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
1063c038e6bSDominik Dingel return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1073c038e6bSDominik Dingel
1083c038e6bSDominik Dingel vcpu->arch.pfault_token = parm.token_addr;
1093c038e6bSDominik Dingel vcpu->arch.pfault_select = parm.select_mask;
1103c038e6bSDominik Dingel vcpu->arch.pfault_compare = parm.compare_mask;
1113c038e6bSDominik Dingel vcpu->run->s.regs.gprs[ry] = 0;
1123c038e6bSDominik Dingel rc = 0;
1133c038e6bSDominik Dingel break;
1143c038e6bSDominik Dingel case 1: /*
1153c038e6bSDominik Dingel * CANCEL
1163c038e6bSDominik Dingel * Specification allows to let already pending tokens survive
1173c038e6bSDominik Dingel * the cancel, therefore to reduce code complexity, we assume
1183c038e6bSDominik Dingel * all outstanding tokens are already pending.
1193c038e6bSDominik Dingel */
120ab7090a6SChristian Borntraeger VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
1213c038e6bSDominik Dingel if (parm.token_addr || parm.select_mask ||
1223c038e6bSDominik Dingel parm.compare_mask || parm.zarch)
1233c038e6bSDominik Dingel return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1243c038e6bSDominik Dingel
1253c038e6bSDominik Dingel vcpu->run->s.regs.gprs[ry] = 0;
1263c038e6bSDominik Dingel /*
1273c038e6bSDominik Dingel * If the pfault handling was not established or is already
1283c038e6bSDominik Dingel * canceled SC24-6084 requests to return decimal 4.
1293c038e6bSDominik Dingel */
1303c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1313c038e6bSDominik Dingel vcpu->run->s.regs.gprs[ry] = 4;
1323c038e6bSDominik Dingel else
1333c038e6bSDominik Dingel vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1343c038e6bSDominik Dingel
1353c038e6bSDominik Dingel rc = 0;
1363c038e6bSDominik Dingel break;
1373c038e6bSDominik Dingel default:
1383c038e6bSDominik Dingel rc = -EOPNOTSUPP;
1393c038e6bSDominik Dingel break;
1403c038e6bSDominik Dingel }
1413c038e6bSDominik Dingel
1423c038e6bSDominik Dingel return rc;
1433c038e6bSDominik Dingel }
1443c038e6bSDominik Dingel
__diag_time_slice_end(struct kvm_vcpu * vcpu)145e28acfeaSChristian Borntraeger static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
146e28acfeaSChristian Borntraeger {
147e28acfeaSChristian Borntraeger VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
148*bb000f64SChristian Borntraeger vcpu->stat.instruction_diagnose_44++;
1490546c63dSLongpeng(Mike) kvm_vcpu_on_spin(vcpu, true);
150e28acfeaSChristian Borntraeger return 0;
151e28acfeaSChristian Borntraeger }
152e28acfeaSChristian Borntraeger
15387e28a15SPierre Morel static int forward_cnt;
15487e28a15SPierre Morel static unsigned long cur_slice;
15587e28a15SPierre Morel
diag9c_forwarding_overrun(void)15687e28a15SPierre Morel static int diag9c_forwarding_overrun(void)
15787e28a15SPierre Morel {
15887e28a15SPierre Morel /* Reset the count on a new slice */
15987e28a15SPierre Morel if (time_after(jiffies, cur_slice)) {
16087e28a15SPierre Morel cur_slice = jiffies;
16187e28a15SPierre Morel forward_cnt = diag9c_forwarding_hz / HZ;
16287e28a15SPierre Morel }
16387e28a15SPierre Morel return forward_cnt-- <= 0 ? 1 : 0;
16487e28a15SPierre Morel }
16587e28a15SPierre Morel
__diag_time_slice_end_directed(struct kvm_vcpu * vcpu)16641628d33SKonstantin Weitz static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
16741628d33SKonstantin Weitz {
16841628d33SKonstantin Weitz struct kvm_vcpu *tcpu;
16941628d33SKonstantin Weitz int tcpu_cpu;
17041628d33SKonstantin Weitz int tid;
17141628d33SKonstantin Weitz
172*bb000f64SChristian Borntraeger tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
17341628d33SKonstantin Weitz vcpu->stat.instruction_diagnose_9c++;
1748474e5caSChristian Borntraeger
17541628d33SKonstantin Weitz /* yield to self */
1768474e5caSChristian Borntraeger if (tid == vcpu->vcpu_id)
17741628d33SKonstantin Weitz goto no_yield;
1788474e5caSChristian Borntraeger
179e09fefdeSDavid Hildenbrand /* yield to invalid */
1808474e5caSChristian Borntraeger tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid);
1818474e5caSChristian Borntraeger if (!tcpu)
1828474e5caSChristian Borntraeger goto no_yield;
18387e28a15SPierre Morel
18487e28a15SPierre Morel /* target guest VCPU already running */
18587e28a15SPierre Morel tcpu_cpu = READ_ONCE(tcpu->cpu);
186c7b7de63SChristian Borntraeger if (tcpu_cpu >= 0) {
187c7b7de63SChristian Borntraeger if (!diag9c_forwarding_hz || diag9c_forwarding_overrun())
18887e28a15SPierre Morel goto no_yield;
18987e28a15SPierre Morel
19087e28a15SPierre Morel /* target host CPU already running */
19187e28a15SPierre Morel if (!vcpu_is_preempted(tcpu_cpu))
19287e28a15SPierre Morel goto no_yield;
19387e28a15SPierre Morel smp_yield_cpu(tcpu_cpu);
19487e28a15SPierre Morel VCPU_EVENT(vcpu, 5,
195*bb000f64SChristian Borntraeger "diag time slice end directed to %d: yield forwarded",
19687e28a15SPierre Morel tid);
19787e28a15SPierre Morel vcpu->stat.diag_9c_forward++;
19887e28a15SPierre Morel return 0;
1998474e5caSChristian Borntraeger }
2008474e5caSChristian Borntraeger
2018474e5caSChristian Borntraeger if (kvm_vcpu_yield_to(tcpu) <= 0)
2028474e5caSChristian Borntraeger goto no_yield;
2038474e5caSChristian Borntraeger
2048474e5caSChristian Borntraeger VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d: done", tid);
2058474e5caSChristian Borntraeger return 0;
206*bb000f64SChristian Borntraeger no_yield:
20741628d33SKonstantin Weitz VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d: ignored", tid);
20841628d33SKonstantin Weitz vcpu->stat.diag_9c_ignored++;
20941628d33SKonstantin Weitz return 0;
210e28acfeaSChristian Borntraeger }
211e28acfeaSChristian Borntraeger
__diag_ipl_functions(struct kvm_vcpu * vcpu)212e28acfeaSChristian Borntraeger static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
2135a32c1afSChristian Borntraeger {
214e28acfeaSChristian Borntraeger unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
21515e8b5daSChristian Borntraeger unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
216*bb000f64SChristian Borntraeger
217e28acfeaSChristian Borntraeger VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
218e28acfeaSChristian Borntraeger vcpu->stat.instruction_diagnose_308++;
219e28acfeaSChristian Borntraeger switch (subcode) {
220e28acfeaSChristian Borntraeger case 3:
221e28acfeaSChristian Borntraeger vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
222e28acfeaSChristian Borntraeger break;
223e28acfeaSChristian Borntraeger case 4:
224e28acfeaSChristian Borntraeger vcpu->run->s390_reset_flags = 0;
225b8e660b8SHeiko Carstens break;
226e28acfeaSChristian Borntraeger default:
227e28acfeaSChristian Borntraeger return -EOPNOTSUPP;
228fe28c786SJanosch Frank }
229fe28c786SJanosch Frank
230fe28c786SJanosch Frank /*
231fe28c786SJanosch Frank * no need to check the return value of vcpu_stop as it can only have
2326352e4d2SDavid Hildenbrand * an error for protvirt, but protvirt means user cpu state
2336852d7b6SDavid Hildenbrand */
234e28acfeaSChristian Borntraeger if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
235e28acfeaSChristian Borntraeger kvm_s390_vcpu_stop(vcpu);
236e28acfeaSChristian Borntraeger vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
237e28acfeaSChristian Borntraeger vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
23833e19115SHeiko Carstens vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
239e28acfeaSChristian Borntraeger vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
240ade38c31SCornelia Huck VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
241e28acfeaSChristian Borntraeger vcpu->run->s390_reset_flags);
242e28acfeaSChristian Borntraeger trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
243e28acfeaSChristian Borntraeger return -EREMOTE;
24410ccaa1eSCornelia Huck }
24510ccaa1eSCornelia Huck
__diag_virtio_hypercall(struct kvm_vcpu * vcpu)246800c1065SThomas Huth static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
24710ccaa1eSCornelia Huck {
248*bb000f64SChristian Borntraeger int ret;
24910ccaa1eSCornelia Huck
25010ccaa1eSCornelia Huck vcpu->stat.instruction_diagnose_500++;
25110ccaa1eSCornelia Huck /* No virtio-ccw notification? Get out quickly. */
25210ccaa1eSCornelia Huck if (!vcpu->kvm->arch.css_support ||
25310ccaa1eSCornelia Huck (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
2541bb78d16SChristian Borntraeger return -EOPNOTSUPP;
2551bb78d16SChristian Borntraeger
2561bb78d16SChristian Borntraeger VCPU_EVENT(vcpu, 4, "diag 0x500 schid 0x%8.8x queue 0x%x cookie 0x%llx",
2571bb78d16SChristian Borntraeger (u32) vcpu->run->s.regs.gprs[2],
2581bb78d16SChristian Borntraeger (u32) vcpu->run->s.regs.gprs[3],
25910ccaa1eSCornelia Huck vcpu->run->s.regs.gprs[4]);
26010ccaa1eSCornelia Huck
26110ccaa1eSCornelia Huck /*
26210ccaa1eSCornelia Huck * The layout is as follows:
26385dfe87eSCornelia Huck * - gpr 2 contains the subchannel id (passed as addr)
26410ccaa1eSCornelia Huck * - gpr 3 contains the virtqueue index (passed as datamatch)
265e32edf4fSNikolay Nikolaev * - gpr 4 contains the index on the bus (optionally)
266ff1f3cb4SDominik Dingel */
26785dfe87eSCornelia Huck ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
26885dfe87eSCornelia Huck vcpu->run->s.regs.gprs[2] & 0xffffffff,
26985dfe87eSCornelia Huck 8, &vcpu->run->s.regs.gprs[3],
27085dfe87eSCornelia Huck vcpu->run->s.regs.gprs[4]);
27185dfe87eSCornelia Huck
27285dfe87eSCornelia Huck /*
27385dfe87eSCornelia Huck * Return cookie in gpr 2, but don't overwrite the register if the
27485dfe87eSCornelia Huck * diagnose will be handled by userspace.
27585dfe87eSCornelia Huck */
27685dfe87eSCornelia Huck if (ret != -EOPNOTSUPP)
27710ccaa1eSCornelia Huck vcpu->run->s.regs.gprs[2] = ret;
27810ccaa1eSCornelia Huck /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
27910ccaa1eSCornelia Huck return ret < 0 ? ret : 0;
280e28acfeaSChristian Borntraeger }
281e28acfeaSChristian Borntraeger
kvm_s390_handle_diag(struct kvm_vcpu * vcpu)2828ae04b8fSAlexander Yarygin int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
283e28acfeaSChristian Borntraeger {
28493e1750fSThomas Huth int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
28593e1750fSThomas Huth
28693e1750fSThomas Huth if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
2875786fffaSCornelia Huck return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
288e28acfeaSChristian Borntraeger
289388186bcSChristian Borntraeger trace_kvm_s390_handle_diag(vcpu, code);
290388186bcSChristian Borntraeger switch (code) {
291e28acfeaSChristian Borntraeger case 0x10:
292e28acfeaSChristian Borntraeger return diag_release_pages(vcpu);
29341628d33SKonstantin Weitz case 0x44:
29441628d33SKonstantin Weitz return __diag_time_slice_end(vcpu);
2953c038e6bSDominik Dingel case 0x9c:
2963c038e6bSDominik Dingel return __diag_time_slice_end_directed(vcpu);
297e28acfeaSChristian Borntraeger case 0x258:
298e28acfeaSChristian Borntraeger return __diag_page_ref_service(vcpu);
29910ccaa1eSCornelia Huck case 0x308:
30010ccaa1eSCornelia Huck return __diag_ipl_functions(vcpu);
301e28acfeaSChristian Borntraeger case 0x500:
302*bb000f64SChristian Borntraeger return __diag_virtio_hypercall(vcpu);
303b8e660b8SHeiko Carstens default:
304e28acfeaSChristian Borntraeger vcpu->stat.instruction_diagnose_other++;
305e28acfeaSChristian Borntraeger return -EOPNOTSUPP;
306 }
307 }
308