blob: bf9ed34c2bcd84af9a196f9a372c60de1c42a7d8 [file] [log] [blame]
Christian Borntraegere28acfe2008-03-25 18:47:34 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling diagnose instructions
Christian Borntraegere28acfe2008-03-25 18:47:34 +01003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2008, 2011
Christian Borntraegere28acfe2008-03-25 18:47:34 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 */
13
14#include <linux/kvm.h>
15#include <linux/kvm_host.h>
Cornelia Huck10ccaa12013-02-28 12:33:21 +010016#include <asm/virtio-ccw.h>
Christian Borntraegere28acfe2008-03-25 18:47:34 +010017#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020018#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020019#include "trace-s390.h"
Dominik Dingel3c038e62013-10-07 17:11:48 +020020#include "gaccess.h"
Christian Borntraegere28acfe2008-03-25 18:47:34 +010021
Christian Borntraeger388186b2011-10-30 15:17:03 +010022static int diag_release_pages(struct kvm_vcpu *vcpu)
23{
24 unsigned long start, end;
25 unsigned long prefix = vcpu->arch.sie_block->prefix;
26
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010027 start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
28 end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
Christian Borntraeger388186b2011-10-30 15:17:03 +010029
30 if (start & ~PAGE_MASK || end & ~PAGE_MASK || start > end
31 || start < 2 * PAGE_SIZE)
32 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
33
34 VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
35 vcpu->stat.diagnose_10++;
36
37 /* we checked for start > end above */
38 if (end < prefix || start >= prefix + 2 * PAGE_SIZE) {
39 gmap_discard(start, end, vcpu->arch.gmap);
40 } else {
41 if (start < prefix)
42 gmap_discard(start, prefix, vcpu->arch.gmap);
43 if (end >= prefix)
44 gmap_discard(prefix + 2 * PAGE_SIZE,
45 end, vcpu->arch.gmap);
46 }
47 return 0;
48}
49
Dominik Dingel3c038e62013-10-07 17:11:48 +020050static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
51{
52 struct prs_parm {
53 u16 code;
54 u16 subcode;
55 u16 parm_len;
56 u16 parm_version;
57 u64 token_addr;
58 u64 select_mask;
59 u64 compare_mask;
60 u64 zarch;
61 };
62 struct prs_parm parm;
63 int rc;
64 u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
65 u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
66 unsigned long hva_token = KVM_HVA_ERR_BAD;
67
68 if (vcpu->run->s.regs.gprs[rx] & 7)
69 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
70 if (copy_from_guest(vcpu, &parm, vcpu->run->s.regs.gprs[rx], sizeof(parm)))
71 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
72 if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
73 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
74
75 switch (parm.subcode) {
76 case 0: /* TOKEN */
77 if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
78 /*
79 * If the pagefault handshake is already activated,
80 * the token must not be changed. We have to return
81 * decimal 8 instead, as mandated in SC24-6084.
82 */
83 vcpu->run->s.regs.gprs[ry] = 8;
84 return 0;
85 }
86
87 if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
88 parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
89 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
90
91 hva_token = gfn_to_hva(vcpu->kvm, gpa_to_gfn(parm.token_addr));
92 if (kvm_is_error_hva(hva_token))
93 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
94
95 vcpu->arch.pfault_token = parm.token_addr;
96 vcpu->arch.pfault_select = parm.select_mask;
97 vcpu->arch.pfault_compare = parm.compare_mask;
98 vcpu->run->s.regs.gprs[ry] = 0;
99 rc = 0;
100 break;
101 case 1: /*
102 * CANCEL
103 * Specification allows to let already pending tokens survive
104 * the cancel, therefore to reduce code complexity, we assume
105 * all outstanding tokens are already pending.
106 */
107 if (parm.token_addr || parm.select_mask ||
108 parm.compare_mask || parm.zarch)
109 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
110
111 vcpu->run->s.regs.gprs[ry] = 0;
112 /*
113 * If the pfault handling was not established or is already
114 * canceled SC24-6084 requests to return decimal 4.
115 */
116 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
117 vcpu->run->s.regs.gprs[ry] = 4;
118 else
119 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
120
121 rc = 0;
122 break;
123 default:
124 rc = -EOPNOTSUPP;
125 break;
126 }
127
128 return rc;
129}
130
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100131static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
132{
133 VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
134 vcpu->stat.diagnose_44++;
Christian Borntraeger8733ac32012-04-25 15:30:39 +0200135 kvm_vcpu_on_spin(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100136 return 0;
137}
138
Konstantin Weitz41628d32012-04-25 15:30:38 +0200139static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
140{
141 struct kvm *kvm = vcpu->kvm;
142 struct kvm_vcpu *tcpu;
143 int tid;
144 int i;
145
146 tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
147 vcpu->stat.diagnose_9c++;
148 VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
149
150 if (tid == vcpu->vcpu_id)
151 return 0;
152
153 kvm_for_each_vcpu(i, tcpu, kvm)
154 if (tcpu->vcpu_id == tid) {
155 kvm_vcpu_yield_to(tcpu);
156 break;
157 }
158
159 return 0;
160}
161
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100162static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
163{
164 unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100165 unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100166
167 VCPU_EVENT(vcpu, 5, "diag ipl functions, subcode %lx", subcode);
168 switch (subcode) {
169 case 3:
170 vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
171 break;
172 case 4:
173 vcpu->run->s390_reset_flags = 0;
174 break;
175 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100176 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100177 }
178
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100179 atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100180 vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
181 vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
182 vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
183 vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
Heiko Carstens33e19112009-01-09 12:14:56 +0100184 VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100185 vcpu->run->s390_reset_flags);
Cornelia Huckade38c32012-07-23 17:20:30 +0200186 trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100187 return -EREMOTE;
188}
189
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100190static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
191{
Thomas Huth800c1062013-09-12 10:33:45 +0200192 int ret;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100193
194 /* No virtio-ccw notification? Get out quickly. */
195 if (!vcpu->kvm->arch.css_support ||
196 (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
197 return -EOPNOTSUPP;
198
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100199 /*
200 * The layout is as follows:
201 * - gpr 2 contains the subchannel id (passed as addr)
202 * - gpr 3 contains the virtqueue index (passed as datamatch)
Cornelia Huck85dfe872013-07-03 16:30:54 +0200203 * - gpr 4 contains the index on the bus (optionally)
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100204 */
Cornelia Huck85dfe872013-07-03 16:30:54 +0200205 ret = kvm_io_bus_write_cookie(vcpu->kvm, KVM_VIRTIO_CCW_NOTIFY_BUS,
Dominik Dingelff1f3cb2013-12-09 18:30:01 +0100206 vcpu->run->s.regs.gprs[2] & 0xffffffff,
Cornelia Huck85dfe872013-07-03 16:30:54 +0200207 8, &vcpu->run->s.regs.gprs[3],
208 vcpu->run->s.regs.gprs[4]);
Cornelia Huck85dfe872013-07-03 16:30:54 +0200209
210 /*
211 * Return cookie in gpr 2, but don't overwrite the register if the
212 * diagnose will be handled by userspace.
213 */
214 if (ret != -EOPNOTSUPP)
215 vcpu->run->s.regs.gprs[2] = ret;
216 /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100217 return ret < 0 ? ret : 0;
218}
219
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100220int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
221{
Heiko Carstens743db272013-11-11 13:56:47 +0100222 int code = kvm_s390_get_base_disp_rs(vcpu) & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100223
Thomas Huth93e17502013-06-20 17:22:02 +0200224 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
225 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
226
Cornelia Huck5786fff2012-07-23 17:20:29 +0200227 trace_kvm_s390_handle_diag(vcpu, code);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100228 switch (code) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100229 case 0x10:
230 return diag_release_pages(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100231 case 0x44:
232 return __diag_time_slice_end(vcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200233 case 0x9c:
234 return __diag_time_slice_end_directed(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200235 case 0x258:
236 return __diag_page_ref_service(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100237 case 0x308:
238 return __diag_ipl_functions(vcpu);
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100239 case 0x500:
240 return __diag_virtio_hypercall(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100241 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100242 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100243 }
244}