blob: ce865bd4f81d9696e7c9907d7bcaedede8f7cf43 [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>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010017#include <asm/gmap.h>
Cornelia Huck10ccaa12013-02-28 12:33:21 +010018#include <asm/virtio-ccw.h>
Christian Borntraegere28acfe2008-03-25 18:47:34 +010019#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020020#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020021#include "trace-s390.h"
Dominik Dingel3c038e62013-10-07 17:11:48 +020022#include "gaccess.h"
Christian Borntraegere28acfe2008-03-25 18:47:34 +010023
Christian Borntraeger388186b2011-10-30 15:17:03 +010024static int diag_release_pages(struct kvm_vcpu *vcpu)
25{
26 unsigned long start, end;
Michael Muellerfda902c2014-05-13 16:58:30 +020027 unsigned long prefix = kvm_s390_get_prefix(vcpu);
Christian Borntraeger388186b2011-10-30 15:17:03 +010028
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010029 start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
30 end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
Christian Borntraeger175a5c92015-07-07 15:19:32 +020031 vcpu->stat.diagnose_10++;
Christian Borntraeger388186b2011-10-30 15:17:03 +010032
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020033 if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
Christian Borntraeger388186b2011-10-30 15:17:03 +010034 || start < 2 * PAGE_SIZE)
35 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
36
37 VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010038
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020039 /*
40 * We checked for start >= end above, so lets check for the
41 * fast path (no prefix swap page involved)
42 */
43 if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +020044 gmap_discard(vcpu->arch.gmap, start, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010045 } else {
Christian Borntraegerf7a960a2014-09-03 21:23:13 +020046 /*
47 * This is slow path. gmap_discard will check for start
48 * so lets split this into before prefix, prefix, after
49 * prefix and let gmap_discard make some of these calls
50 * NOPs.
51 */
52 gmap_discard(vcpu->arch.gmap, start, prefix);
53 if (start <= prefix)
54 gmap_discard(vcpu->arch.gmap, 0, 4096);
55 if (end > prefix + 4096)
56 gmap_discard(vcpu->arch.gmap, 4096, 8192);
57 gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
Christian Borntraeger388186b2011-10-30 15:17:03 +010058 }
59 return 0;
60}
61
Dominik Dingel3c038e62013-10-07 17:11:48 +020062static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
63{
64 struct prs_parm {
65 u16 code;
66 u16 subcode;
67 u16 parm_len;
68 u16 parm_version;
69 u64 token_addr;
70 u64 select_mask;
71 u64 compare_mask;
72 u64 zarch;
73 };
74 struct prs_parm parm;
75 int rc;
76 u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
77 u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
Dominik Dingel3c038e62013-10-07 17:11:48 +020078
Christian Borntraeger15e8b5d2015-07-09 13:43:41 +020079 VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
80 vcpu->run->s.regs.gprs[rx]);
Christian Borntraeger175a5c92015-07-07 15:19:32 +020081 vcpu->stat.diagnose_258++;
Dominik Dingel3c038e62013-10-07 17:11:48 +020082 if (vcpu->run->s.regs.gprs[rx] & 7)
83 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030084 rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
Heiko Carstens81480cc2014-01-01 16:36:07 +010085 if (rc)
86 return kvm_s390_inject_prog_cond(vcpu, rc);
Dominik Dingel3c038e62013-10-07 17:11:48 +020087 if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
88 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
89
90 switch (parm.subcode) {
91 case 0: /* TOKEN */
Christian Borntraegerab7090a2015-07-16 10:24:07 +020092 VCPU_EVENT(vcpu, 3, "pageref token addr 0x%llx "
93 "select mask 0x%llx compare mask 0x%llx",
94 parm.token_addr, parm.select_mask, parm.compare_mask);
Dominik Dingel3c038e62013-10-07 17:11:48 +020095 if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
96 /*
97 * If the pagefault handshake is already activated,
98 * the token must not be changed. We have to return
99 * decimal 8 instead, as mandated in SC24-6084.
100 */
101 vcpu->run->s.regs.gprs[ry] = 8;
102 return 0;
103 }
104
105 if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
106 parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
107 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
108
Heiko Carstens81480cc2014-01-01 16:36:07 +0100109 if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
Dominik Dingel3c038e62013-10-07 17:11:48 +0200110 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
111
112 vcpu->arch.pfault_token = parm.token_addr;
113 vcpu->arch.pfault_select = parm.select_mask;
114 vcpu->arch.pfault_compare = parm.compare_mask;
115 vcpu->run->s.regs.gprs[ry] = 0;
116 rc = 0;
117 break;
118 case 1: /*
119 * CANCEL
120 * Specification allows to let already pending tokens survive
121 * the cancel, therefore to reduce code complexity, we assume
122 * all outstanding tokens are already pending.
123 */
Christian Borntraegerab7090a2015-07-16 10:24:07 +0200124 VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200125 if (parm.token_addr || parm.select_mask ||
126 parm.compare_mask || parm.zarch)
127 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
128
129 vcpu->run->s.regs.gprs[ry] = 0;
130 /*
131 * If the pfault handling was not established or is already
132 * canceled SC24-6084 requests to return decimal 4.
133 */
134 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
135 vcpu->run->s.regs.gprs[ry] = 4;
136 else
137 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
138
139 rc = 0;
140 break;
141 default:
142 rc = -EOPNOTSUPP;
143 break;
144 }
145
146 return rc;
147}
148
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100149static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
150{
151 VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
152 vcpu->stat.diagnose_44++;
Christian Borntraeger8733ac32012-04-25 15:30:39 +0200153 kvm_vcpu_on_spin(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100154 return 0;
155}
156
Konstantin Weitz41628d32012-04-25 15:30:38 +0200157static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
158{
Konstantin Weitz41628d32012-04-25 15:30:38 +0200159 struct kvm_vcpu *tcpu;
160 int tid;
Konstantin Weitz41628d32012-04-25 15:30:38 +0200161
162 tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
163 vcpu->stat.diagnose_9c++;
164 VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
165
166 if (tid == vcpu->vcpu_id)
167 return 0;
168
David Hildenbrande09fefd2015-11-05 09:03:50 +0100169 tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid);
170 if (tcpu)
171 kvm_vcpu_yield_to(tcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200172 return 0;
173}
174
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100175static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
176{
177 unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100178 unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100179
Christian Borntraeger15e8b5d2015-07-09 13:43:41 +0200180 VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
Christian Borntraeger175a5c92015-07-07 15:19:32 +0200181 vcpu->stat.diagnose_308++;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100182 switch (subcode) {
183 case 3:
184 vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
185 break;
186 case 4:
187 vcpu->run->s390_reset_flags = 0;
188 break;
189 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100190 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100191 }
192
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200193 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
194 kvm_s390_vcpu_stop(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100195 vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
196 vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
197 vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
198 vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
Heiko Carstens33e19112009-01-09 12:14:56 +0100199 VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100200 vcpu->run->s390_reset_flags);
Cornelia Huckade38c32012-07-23 17:20:30 +0200201 trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100202 return -EREMOTE;
203}
204
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100205static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
206{
Thomas Huth800c1062013-09-12 10:33:45 +0200207 int ret;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100208
Christian Borntraeger175a5c92015-07-07 15:19:32 +0200209 vcpu->stat.diagnose_500++;
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100210 /* No virtio-ccw notification? Get out quickly. */
211 if (!vcpu->kvm->arch.css_support ||
212 (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
213 return -EOPNOTSUPP;
214
Christian Borntraeger1bb78d12016-06-07 09:57:08 +0200215 VCPU_EVENT(vcpu, 4, "diag 0x500 schid 0x%8.8x queue 0x%x cookie 0x%llx",
216 (u32) vcpu->run->s.regs.gprs[2],
217 (u32) vcpu->run->s.regs.gprs[3],
218 vcpu->run->s.regs.gprs[4]);
219
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100220 /*
221 * The layout is as follows:
222 * - gpr 2 contains the subchannel id (passed as addr)
223 * - gpr 3 contains the virtqueue index (passed as datamatch)
Cornelia Huck85dfe872013-07-03 16:30:54 +0200224 * - gpr 4 contains the index on the bus (optionally)
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100225 */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +0000226 ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
Dominik Dingelff1f3cb2013-12-09 18:30:01 +0100227 vcpu->run->s.regs.gprs[2] & 0xffffffff,
Cornelia Huck85dfe872013-07-03 16:30:54 +0200228 8, &vcpu->run->s.regs.gprs[3],
229 vcpu->run->s.regs.gprs[4]);
Cornelia Huck85dfe872013-07-03 16:30:54 +0200230
231 /*
232 * Return cookie in gpr 2, but don't overwrite the register if the
233 * diagnose will be handled by userspace.
234 */
235 if (ret != -EOPNOTSUPP)
236 vcpu->run->s.regs.gprs[2] = ret;
237 /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100238 return ret < 0 ? ret : 0;
239}
240
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100241int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
242{
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300243 int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100244
Thomas Huth93e17502013-06-20 17:22:02 +0200245 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
246 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
247
Cornelia Huck5786fff2012-07-23 17:20:29 +0200248 trace_kvm_s390_handle_diag(vcpu, code);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100249 switch (code) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100250 case 0x10:
251 return diag_release_pages(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100252 case 0x44:
253 return __diag_time_slice_end(vcpu);
Konstantin Weitz41628d32012-04-25 15:30:38 +0200254 case 0x9c:
255 return __diag_time_slice_end_directed(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200256 case 0x258:
257 return __diag_page_ref_service(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100258 case 0x308:
259 return __diag_ipl_functions(vcpu);
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100260 case 0x500:
261 return __diag_virtio_hypercall(vcpu);
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100262 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100263 return -EOPNOTSUPP;
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100264 }
265}