blob: 05f7de9869a9f1786a8b996edd10fcdb280bd7c0 [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>
Martin Schwidefskydeedabb2013-05-21 17:29:52 +020016#include <asm/pgalloc.h>
Cornelia Huck10ccaa12013-02-28 12:33:21 +010017#include <asm/virtio-ccw.h>
Christian Borntraegere28acfe2008-03-25 18:47:34 +010018#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020019#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020020#include "trace-s390.h"
Dominik Dingel3c038e62013-10-07 17:11:48 +020021#include "gaccess.h"
Christian Borntraegere28acfe2008-03-25 18:47:34 +010022
Christian Borntraeger388186b2011-10-30 15:17:03 +010023static int diag_release_pages(struct kvm_vcpu *vcpu)
24{
25 unsigned long start, end;
Michael Muellerfda902c2014-05-13 16:58:30 +020026 unsigned long prefix = kvm_s390_get_prefix(vcpu);
Christian Borntraeger388186b2011-10-30 15:17:03 +010027
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010028 start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
29 end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
Christian Borntraeger175a5c92015-07-07 15:19:32 +020030 vcpu->stat.diagnose_10++;
Christian Borntraeger388186b2011-10-30 15:17:03 +010031
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020032 if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
Christian Borntraeger388186b2011-10-30 15:17:03 +010033 || start < 2 * PAGE_SIZE)
34 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
35
36 VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010037
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020038 /*
39 * We checked for start >= end above, so lets check for the
40 * fast path (no prefix swap page involved)
41 */
42 if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +020043 gmap_discard(vcpu->arch.gmap, start, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010044 } else {
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020045 /*
46 * This is slow path. gmap_discard will check for start
47 * so lets split this into before prefix, prefix, after
48 * prefix and let gmap_discard make some of these calls
49 * NOPs.
50 */
51 gmap_discard(vcpu->arch.gmap, start, prefix);
52 if (start <= prefix)
53 gmap_discard(vcpu->arch.gmap, 0, 4096);
54 if (end > prefix + 4096)
55 gmap_discard(vcpu->arch.gmap, 4096, 8192);
56 gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010057 }
58 return 0;
59}
60
Dominik Dingel3c038e62013-10-07 17:11:48 +020061static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
62{
63 struct prs_parm {
64 u16 code;
65 u16 subcode;
66 u16 parm_len;
67 u16 parm_version;
68 u64 token_addr;
69 u64 select_mask;
70 u64 compare_mask;
71 u64 zarch;
72 };
73 struct prs_parm parm;
74 int rc;
75 u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
76 u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
Dominik Dingel3c038e62013-10-07 17:11:48 +020077
Christian Borntraeger15e8b5d2015-07-09 13:43:41 +020078 VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
79 vcpu->run->s.regs.gprs[rx]);
Christian Borntraeger175a5c92015-07-07 15:19:32 +020080 vcpu->stat.diagnose_258++;
Dominik Dingel3c038e62013-10-07 17:11:48 +020081 if (vcpu->run->s.regs.gprs[rx] & 7)
82 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030083 rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
Heiko Carstens81480cc2014-01-01 16:36:07 +010084 if (rc)
85 return kvm_s390_inject_prog_cond(vcpu, rc);
Dominik Dingel3c038e62013-10-07 17:11:48 +020086 if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
87 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
88
89 switch (parm.subcode) {
90 case 0: /* TOKEN */
Christian Borntraegerab7090a2015-07-16 10:24:07 +020091 VCPU_EVENT(vcpu, 3, "pageref token addr 0x%llx "
92 "select mask 0x%llx compare mask 0x%llx",
93 parm.token_addr, parm.select_mask, parm.compare_mask);
Dominik Dingel3c038e62013-10-07 17:11:48 +020094 if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
95 /*
96 * If the pagefault handshake is already activated,
97 * the token must not be changed. We have to return
98 * decimal 8 instead, as mandated in SC24-6084.
99 */
100 vcpu->run->s.regs.gprs[ry] = 8;
101 return 0;
102 }
103
104 if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
105 parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
106 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
107
Heiko Carstens81480cc2014-01-01 16:36:07 +0100108 if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
Dominik Dingel3c038e62013-10-07 17:11:48 +0200109 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
110
111 vcpu->arch.pfault_token = parm.token_addr;
112 vcpu->arch.pfault_select = parm.select_mask;
113 vcpu->arch.pfault_compare = parm.compare_mask;
114 vcpu->run->s.regs.gprs[ry] = 0;
115 rc = 0;
116 break;
117 case 1: /*
118 * CANCEL
119 * Specification allows to let already pending tokens survive
120 * the cancel, therefore to reduce code complexity, we assume
121 * all outstanding tokens are already pending.
122 */
Christian Borntraegerab7090a2015-07-16 10:24:07 +0200123 VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200124 if (parm.token_addr || parm.select_mask ||
125 parm.compare_mask || parm.zarch)
126 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
127
128 vcpu->run->s.regs.gprs[ry] = 0;
129 /*
130 * If the pfault handling was not established or is already
131 * canceled SC24-6084 requests to return decimal 4.
132 */
133 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
134 vcpu->run->s.regs.gprs[ry] = 4;
135 else
136 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
137
138 rc = 0;
139 break;
140 default:
141 rc = -EOPNOTSUPP;
142 break;
143 }
144
145 return rc;
146}
147
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100148static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
149{
150 VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
151 vcpu->stat.diagnose_44++;
Christian Borntraeger8733ac32012-04-25 15:30:39 +0200152 kvm_vcpu_on_spin(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100153 return 0;
154}
155
Konstantin Weitz41628d32012-04-25 15:30:38 +0200156static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
157{
Konstantin Weitz41628d32012-04-25 15:30:38 +0200158 struct kvm_vcpu *tcpu;
159 int tid;
Konstantin Weitz41628d32012-04-25 15:30:38 +0200160
161 tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
162 vcpu->stat.diagnose_9c++;
163 VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
164
165 if (tid == vcpu->vcpu_id)
166 return 0;
167
David Hildenbrande09fefd2015-11-05 09:03:50 +0100168 tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid);
169 if (tcpu)
170 kvm_vcpu_yield_to(tcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200171 return 0;
172}
173
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100174static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
175{
176 unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100177 unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100178
Christian Borntraeger15e8b5d2015-07-09 13:43:41 +0200179 VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
Christian Borntraeger175a5c92015-07-07 15:19:32 +0200180 vcpu->stat.diagnose_308++;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100181 switch (subcode) {
182 case 3:
183 vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
184 break;
185 case 4:
186 vcpu->run->s390_reset_flags = 0;
187 break;
188 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100189 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100190 }
191
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200192 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
193 kvm_s390_vcpu_stop(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100194 vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
195 vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
196 vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
197 vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
Heiko Carstens33e19112009-01-09 12:14:56 +0100198 VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100199 vcpu->run->s390_reset_flags);
Cornelia Huckade38c32012-07-23 17:20:30 +0200200 trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100201 return -EREMOTE;
202}
203
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100204static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
205{
Thomas Huth800c1062013-09-12 10:33:45 +0200206 int ret;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100207
Christian Borntraeger175a5c92015-07-07 15:19:32 +0200208 vcpu->stat.diagnose_500++;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100209 /* No virtio-ccw notification? Get out quickly. */
210 if (!vcpu->kvm->arch.css_support ||
211 (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
212 return -EOPNOTSUPP;
213
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100214 /*
215 * The layout is as follows:
216 * - gpr 2 contains the subchannel id (passed as addr)
217 * - gpr 3 contains the virtqueue index (passed as datamatch)
Cornelia Huck85dfe872013-07-03 16:30:54 +0200218 * - gpr 4 contains the index on the bus (optionally)
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100219 */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000220 ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
Dominik Dingelff1f3cb2013-12-09 18:30:01 +0100221 vcpu->run->s.regs.gprs[2] & 0xffffffff,
Cornelia Huck85dfe872013-07-03 16:30:54 +0200222 8, &vcpu->run->s.regs.gprs[3],
223 vcpu->run->s.regs.gprs[4]);
Cornelia Huck85dfe872013-07-03 16:30:54 +0200224
225 /*
226 * Return cookie in gpr 2, but don't overwrite the register if the
227 * diagnose will be handled by userspace.
228 */
229 if (ret != -EOPNOTSUPP)
230 vcpu->run->s.regs.gprs[2] = ret;
231 /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100232 return ret < 0 ? ret : 0;
233}
234
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100235int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
236{
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300237 int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100238
Thomas Huth93e17502013-06-20 17:22:02 +0200239 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
240 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
241
Cornelia Huck5786fff2012-07-23 17:20:29 +0200242 trace_kvm_s390_handle_diag(vcpu, code);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100243 switch (code) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100244 case 0x10:
245 return diag_release_pages(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100246 case 0x44:
247 return __diag_time_slice_end(vcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200248 case 0x9c:
249 return __diag_time_slice_end_directed(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200250 case 0x258:
251 return __diag_page_ref_service(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100252 case 0x308:
253 return __diag_ipl_functions(vcpu);
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100254 case 0x500:
255 return __diag_virtio_hypercall(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100256 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100257 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100258 }
259}