blob: 28ea0cab1f1b50c3f3b12ce63055f4c30ebc5293 [file] [log] [blame]
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling interprocessor communication
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01003 *
Thomas Huthb13d3582013-11-21 16:01:48 +01004 * Copyright IBM Corp. 2008, 2013
Christian Borntraeger5288fbf2008-03-25 18:47:31 +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>
Christian Ehrhardt9ace9032009-05-20 15:34:55 +020012 * Christian Ehrhardt <ehrhardt@de.ibm.com>
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010013 */
14
15#include <linux/kvm.h>
16#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Heiko Carstensa9ae32c2012-06-04 12:55:15 +020018#include <asm/sigp.h>
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010019#include "gaccess.h"
20#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020021#include "trace.h"
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010022
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010023static int __sigp_sense(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010024 u64 *reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010025{
Jens Freimann1ee0bc52014-02-25 15:36:45 +010026 struct kvm_s390_local_interrupt *li;
Jens Freimann1ee0bc52014-02-25 15:36:45 +010027 int cpuflags;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010028 int rc;
David Hildenbrandea5f4962014-10-14 15:29:30 +020029 int ext_call_pending;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010030
Jens Freimann1ee0bc52014-02-25 15:36:45 +010031 li = &dst_vcpu->arch.local_int;
32
33 cpuflags = atomic_read(li->cpuflags);
David Hildenbrandea5f4962014-10-14 15:29:30 +020034 ext_call_pending = kvm_s390_ext_call_pending(dst_vcpu);
35 if (!(cpuflags & CPUSTAT_STOPPED) && !ext_call_pending)
Cornelia Huck21b26c02012-06-26 16:06:41 +020036 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
37 else {
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010038 *reg &= 0xffffffff00000000UL;
David Hildenbrandea5f4962014-10-14 15:29:30 +020039 if (ext_call_pending)
Cornelia Huck21b26c02012-06-26 16:06:41 +020040 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
Jens Freimann1ee0bc52014-02-25 15:36:45 +010041 if (cpuflags & CPUSTAT_STOPPED)
Cornelia Huck21b26c02012-06-26 16:06:41 +020042 *reg |= SIGP_STATUS_STOPPED;
Heiko Carstensea1918d2012-06-26 16:06:40 +020043 rc = SIGP_CC_STATUS_STORED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010044 }
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010045
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010046 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", dst_vcpu->vcpu_id,
47 rc);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010048 return rc;
49}
50
David Hildenbrand07b03032014-06-06 10:24:15 +020051static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
52 struct kvm_vcpu *dst_vcpu)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010053{
Jens Freimann383d0b02014-07-29 15:11:49 +020054 struct kvm_s390_irq irq = {
Jens Freimann22ff4a32014-05-14 17:14:46 +020055 .type = KVM_S390_INT_EMERGENCY,
Jens Freimann383d0b02014-07-29 15:11:49 +020056 .u.emerg.code = vcpu->vcpu_id,
Jens Freimann22ff4a32014-05-14 17:14:46 +020057 };
Jens Freimann22ff4a32014-05-14 17:14:46 +020058 int rc = 0;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010059
Jens Freimann383d0b02014-07-29 15:11:49 +020060 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
Jens Freimann22ff4a32014-05-14 17:14:46 +020061 if (!rc)
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010062 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x",
63 dst_vcpu->vcpu_id);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010064
Jens Freimann22ff4a32014-05-14 17:14:46 +020065 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020066}
67
David Hildenbrand07b03032014-06-06 10:24:15 +020068static int __sigp_emergency(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
69{
70 return __inject_sigp_emergency(vcpu, dst_vcpu);
71}
72
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010073static int __sigp_conditional_emergency(struct kvm_vcpu *vcpu,
74 struct kvm_vcpu *dst_vcpu,
Thomas Huthb13d3582013-11-21 16:01:48 +010075 u16 asn, u64 *reg)
76{
Thomas Huthb13d3582013-11-21 16:01:48 +010077 const u64 psw_int_mask = PSW_MASK_IO | PSW_MASK_EXT;
78 u16 p_asn, s_asn;
79 psw_t *psw;
80 u32 flags;
81
Thomas Huthb13d3582013-11-21 16:01:48 +010082 flags = atomic_read(&dst_vcpu->arch.sie_block->cpuflags);
83 psw = &dst_vcpu->arch.sie_block->gpsw;
84 p_asn = dst_vcpu->arch.sie_block->gcr[4] & 0xffff; /* Primary ASN */
85 s_asn = dst_vcpu->arch.sie_block->gcr[3] & 0xffff; /* Secondary ASN */
86
David Hildenbrand07b03032014-06-06 10:24:15 +020087 /* Inject the emergency signal? */
Thomas Huthb13d3582013-11-21 16:01:48 +010088 if (!(flags & CPUSTAT_STOPPED)
89 || (psw->mask & psw_int_mask) != psw_int_mask
90 || ((flags & CPUSTAT_WAIT) && psw->addr != 0)
91 || (!(flags & CPUSTAT_WAIT) && (asn == p_asn || asn == s_asn))) {
David Hildenbrand07b03032014-06-06 10:24:15 +020092 return __inject_sigp_emergency(vcpu, dst_vcpu);
Thomas Huthb13d3582013-11-21 16:01:48 +010093 } else {
94 *reg &= 0xffffffff00000000UL;
95 *reg |= SIGP_STATUS_INCORRECT_STATE;
96 return SIGP_CC_STATUS_STORED;
97 }
98}
99
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100100static int __sigp_external_call(struct kvm_vcpu *vcpu,
David Hildenbrandea5f4962014-10-14 15:29:30 +0200101 struct kvm_vcpu *dst_vcpu, u64 *reg)
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200102{
Jens Freimann383d0b02014-07-29 15:11:49 +0200103 struct kvm_s390_irq irq = {
Jens Freimann22ff4a32014-05-14 17:14:46 +0200104 .type = KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200105 .u.extcall.code = vcpu->vcpu_id,
Jens Freimann22ff4a32014-05-14 17:14:46 +0200106 };
Jens Freimann22ff4a32014-05-14 17:14:46 +0200107 int rc;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200108
Jens Freimann383d0b02014-07-29 15:11:49 +0200109 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
David Hildenbrandea5f4962014-10-14 15:29:30 +0200110 if (rc == -EBUSY) {
111 *reg &= 0xffffffff00000000UL;
112 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
113 return SIGP_CC_STATUS_STORED;
114 } else if (rc == 0) {
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100115 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x",
116 dst_vcpu->vcpu_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +0200117 }
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200118
Jens Freimann22ff4a32014-05-14 17:14:46 +0200119 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100120}
121
David Hildenbranda6cc3102014-06-06 10:25:09 +0200122static int __sigp_stop(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100123{
David Hildenbrand6cddd432014-10-15 16:48:53 +0200124 struct kvm_s390_irq irq = {
125 .type = KVM_S390_SIGP_STOP,
126 };
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100127 int rc;
128
David Hildenbrand6cddd432014-10-15 16:48:53 +0200129 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
130 if (rc == -EBUSY)
131 rc = SIGP_CC_BUSY;
132 else if (rc == 0)
133 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x",
134 dst_vcpu->vcpu_id);
Thomas Huthe8798922013-11-06 15:46:33 +0100135
David Hildenbranda6cc3102014-06-06 10:25:09 +0200136 return rc;
137}
138
139static int __sigp_stop_and_store_status(struct kvm_vcpu *vcpu,
140 struct kvm_vcpu *dst_vcpu, u64 *reg)
141{
David Hildenbrand6cddd432014-10-15 16:48:53 +0200142 struct kvm_s390_irq irq = {
143 .type = KVM_S390_SIGP_STOP,
144 .u.stop.flags = KVM_S390_STOP_FLAG_STORE_STATUS,
145 };
David Hildenbranda6cc3102014-06-06 10:25:09 +0200146 int rc;
147
David Hildenbrand6cddd432014-10-15 16:48:53 +0200148 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
149 if (rc == -EBUSY)
150 rc = SIGP_CC_BUSY;
151 else if (rc == 0)
152 VCPU_EVENT(vcpu, 4, "sent sigp stop and store status to cpu %x",
153 dst_vcpu->vcpu_id);
Thomas Huthe8798922013-11-06 15:46:33 +0100154
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100155 return rc;
156}
157
158static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
159{
160 int rc;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200161 unsigned int i;
162 struct kvm_vcpu *v;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100163
164 switch (parameter & 0xff) {
165 case 0:
Heiko Carstensea1918d2012-06-26 16:06:40 +0200166 rc = SIGP_CC_NOT_OPERATIONAL;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100167 break;
168 case 1:
169 case 2:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200170 kvm_for_each_vcpu(i, v, vcpu->kvm) {
171 v->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
172 kvm_clear_async_pf_completion_queue(v);
173 }
174
Heiko Carstensea1918d2012-06-26 16:06:40 +0200175 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100176 break;
177 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100178 rc = -EOPNOTSUPP;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100179 }
180 return rc;
181}
182
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100183static int __sigp_set_prefix(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
184 u32 address, u64 *reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100185{
David Hildenbranda3a9c592014-10-14 09:44:55 +0200186 struct kvm_s390_irq irq = {
187 .type = KVM_S390_SIGP_SET_PREFIX,
188 .u.prefix.address = address & 0x7fffe000u,
189 };
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100190 int rc;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100191
Heiko Carstens665170c2014-01-01 16:47:12 +0100192 /*
193 * Make sure the new value is valid memory. We only need to check the
194 * first page, since address is 8k aligned and memory pieces are always
195 * at least 1MB aligned and have at least a size of 1MB.
196 */
David Hildenbranda3a9c592014-10-14 09:44:55 +0200197 if (kvm_is_error_gpa(vcpu->kvm, irq.u.prefix.address)) {
Heiko Carstens07444262012-06-26 16:06:39 +0200198 *reg &= 0xffffffff00000000UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200199 *reg |= SIGP_STATUS_INVALID_PARAMETER;
Heiko Carstensea1918d2012-06-26 16:06:40 +0200200 return SIGP_CC_STATUS_STORED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100201 }
202
David Hildenbranda3a9c592014-10-14 09:44:55 +0200203 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
204 if (rc == -EBUSY) {
Heiko Carstens07444262012-06-26 16:06:39 +0200205 *reg &= 0xffffffff00000000UL;
206 *reg |= SIGP_STATUS_INCORRECT_STATE;
David Hildenbranda3a9c592014-10-14 09:44:55 +0200207 return SIGP_CC_STATUS_STORED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100208 }
209
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100210 return rc;
211}
212
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100213static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu,
214 struct kvm_vcpu *dst_vcpu,
215 u32 addr, u64 *reg)
Thomas Huth00e9e432013-11-13 20:48:51 +0100216{
Thomas Huth00e9e432013-11-13 20:48:51 +0100217 int flags;
218 int rc;
219
Thomas Huth00e9e432013-11-13 20:48:51 +0100220 flags = atomic_read(dst_vcpu->arch.local_int.cpuflags);
Thomas Huth00e9e432013-11-13 20:48:51 +0100221 if (!(flags & CPUSTAT_STOPPED)) {
222 *reg &= 0xffffffff00000000UL;
223 *reg |= SIGP_STATUS_INCORRECT_STATE;
224 return SIGP_CC_STATUS_STORED;
225 }
226
227 addr &= 0x7ffffe00;
228 rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
229 if (rc == -EFAULT) {
230 *reg &= 0xffffffff00000000UL;
231 *reg |= SIGP_STATUS_INVALID_PARAMETER;
232 rc = SIGP_CC_STATUS_STORED;
233 }
234 return rc;
235}
236
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100237static int __sigp_sense_running(struct kvm_vcpu *vcpu,
238 struct kvm_vcpu *dst_vcpu, u64 *reg)
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100239{
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100240 struct kvm_s390_local_interrupt *li;
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100241 int rc;
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100242
David Hildenbrandbd50e8e2016-03-04 12:23:55 +0100243 if (!test_kvm_facility(vcpu->kvm, 9)) {
244 *reg &= 0xffffffff00000000UL;
245 *reg |= SIGP_STATUS_INVALID_ORDER;
246 return SIGP_CC_STATUS_STORED;
247 }
248
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100249 li = &dst_vcpu->arch.local_int;
250 if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
251 /* running */
252 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
253 } else {
254 /* not running */
255 *reg &= 0xffffffff00000000UL;
256 *reg |= SIGP_STATUS_NOT_RUNNING;
257 rc = SIGP_CC_STATUS_STORED;
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100258 }
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100259
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100260 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x",
261 dst_vcpu->vcpu_id, rc);
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100262
263 return rc;
264}
265
David Hildenbrandb8983832014-05-23 12:22:56 +0200266static int __prepare_sigp_re_start(struct kvm_vcpu *vcpu,
267 struct kvm_vcpu *dst_vcpu, u8 order_code)
Jens Freimann151104a2012-02-08 08:28:29 +0100268{
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100269 struct kvm_s390_local_interrupt *li = &dst_vcpu->arch.local_int;
David Hildenbrandb8983832014-05-23 12:22:56 +0200270 /* handle (RE)START in user space */
271 int rc = -EOPNOTSUPP;
Jens Freimann151104a2012-02-08 08:28:29 +0100272
David Hildenbrand6cddd432014-10-15 16:48:53 +0200273 /* make sure we don't race with STOP irq injection */
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200274 spin_lock(&li->lock);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200275 if (kvm_s390_is_stop_irq_pending(dst_vcpu))
Heiko Carstensea1918d2012-06-26 16:06:40 +0200276 rc = SIGP_CC_BUSY;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200277 spin_unlock(&li->lock);
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100278
Jens Freimann151104a2012-02-08 08:28:29 +0100279 return rc;
280}
281
David Hildenbrandb8983832014-05-23 12:22:56 +0200282static int __prepare_sigp_cpu_reset(struct kvm_vcpu *vcpu,
283 struct kvm_vcpu *dst_vcpu, u8 order_code)
284{
285 /* handle (INITIAL) CPU RESET in user space */
286 return -EOPNOTSUPP;
287}
288
289static int __prepare_sigp_unknown(struct kvm_vcpu *vcpu,
290 struct kvm_vcpu *dst_vcpu)
291{
292 /* handle unknown orders in user space */
293 return -EOPNOTSUPP;
294}
295
David Hildenbrand3526a662014-03-19 14:57:49 +0100296static int handle_sigp_dst(struct kvm_vcpu *vcpu, u8 order_code,
297 u16 cpu_addr, u32 parameter, u64 *status_reg)
298{
299 int rc;
David Hildenbrand152e9f62015-11-05 09:06:06 +0100300 struct kvm_vcpu *dst_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100301
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100302 if (!dst_vcpu)
303 return SIGP_CC_NOT_OPERATIONAL;
David Hildenbrand3526a662014-03-19 14:57:49 +0100304
305 switch (order_code) {
306 case SIGP_SENSE:
307 vcpu->stat.instruction_sigp_sense++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100308 rc = __sigp_sense(vcpu, dst_vcpu, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100309 break;
310 case SIGP_EXTERNAL_CALL:
311 vcpu->stat.instruction_sigp_external_call++;
David Hildenbrandea5f4962014-10-14 15:29:30 +0200312 rc = __sigp_external_call(vcpu, dst_vcpu, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100313 break;
314 case SIGP_EMERGENCY_SIGNAL:
315 vcpu->stat.instruction_sigp_emergency++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100316 rc = __sigp_emergency(vcpu, dst_vcpu);
David Hildenbrand3526a662014-03-19 14:57:49 +0100317 break;
318 case SIGP_STOP:
319 vcpu->stat.instruction_sigp_stop++;
David Hildenbranda6cc3102014-06-06 10:25:09 +0200320 rc = __sigp_stop(vcpu, dst_vcpu);
David Hildenbrand3526a662014-03-19 14:57:49 +0100321 break;
322 case SIGP_STOP_AND_STORE_STATUS:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200323 vcpu->stat.instruction_sigp_stop_store_status++;
David Hildenbranda6cc3102014-06-06 10:25:09 +0200324 rc = __sigp_stop_and_store_status(vcpu, dst_vcpu, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100325 break;
326 case SIGP_STORE_STATUS_AT_ADDRESS:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200327 vcpu->stat.instruction_sigp_store_status++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100328 rc = __sigp_store_status_at_addr(vcpu, dst_vcpu, parameter,
David Hildenbrand3526a662014-03-19 14:57:49 +0100329 status_reg);
330 break;
331 case SIGP_SET_PREFIX:
332 vcpu->stat.instruction_sigp_prefix++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100333 rc = __sigp_set_prefix(vcpu, dst_vcpu, parameter, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100334 break;
335 case SIGP_COND_EMERGENCY_SIGNAL:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200336 vcpu->stat.instruction_sigp_cond_emergency++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100337 rc = __sigp_conditional_emergency(vcpu, dst_vcpu, parameter,
David Hildenbrand3526a662014-03-19 14:57:49 +0100338 status_reg);
339 break;
340 case SIGP_SENSE_RUNNING:
341 vcpu->stat.instruction_sigp_sense_running++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100342 rc = __sigp_sense_running(vcpu, dst_vcpu, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100343 break;
344 case SIGP_START:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200345 vcpu->stat.instruction_sigp_start++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200346 rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
David Hildenbrand3526a662014-03-19 14:57:49 +0100347 break;
348 case SIGP_RESTART:
349 vcpu->stat.instruction_sigp_restart++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200350 rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
351 break;
352 case SIGP_INITIAL_CPU_RESET:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200353 vcpu->stat.instruction_sigp_init_cpu_reset++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200354 rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
355 break;
356 case SIGP_CPU_RESET:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200357 vcpu->stat.instruction_sigp_cpu_reset++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200358 rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
David Hildenbrand3526a662014-03-19 14:57:49 +0100359 break;
360 default:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200361 vcpu->stat.instruction_sigp_unknown++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200362 rc = __prepare_sigp_unknown(vcpu, dst_vcpu);
David Hildenbrand3526a662014-03-19 14:57:49 +0100363 }
364
David Hildenbrandb8983832014-05-23 12:22:56 +0200365 if (rc == -EOPNOTSUPP)
366 VCPU_EVENT(vcpu, 4,
367 "sigp order %u -> cpu %x: handled in user space",
368 order_code, dst_vcpu->vcpu_id);
369
David Hildenbrand3526a662014-03-19 14:57:49 +0100370 return rc;
371}
372
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200373static int handle_sigp_order_in_user_space(struct kvm_vcpu *vcpu, u8 order_code,
374 u16 cpu_addr)
David Hildenbrand2444b352014-10-09 14:10:13 +0200375{
376 if (!vcpu->kvm->arch.user_sigp)
377 return 0;
378
379 switch (order_code) {
380 case SIGP_SENSE:
381 case SIGP_EXTERNAL_CALL:
382 case SIGP_EMERGENCY_SIGNAL:
383 case SIGP_COND_EMERGENCY_SIGNAL:
384 case SIGP_SENSE_RUNNING:
385 return 0;
386 /* update counters as we're directly dropping to user space */
387 case SIGP_STOP:
388 vcpu->stat.instruction_sigp_stop++;
389 break;
390 case SIGP_STOP_AND_STORE_STATUS:
391 vcpu->stat.instruction_sigp_stop_store_status++;
392 break;
393 case SIGP_STORE_STATUS_AT_ADDRESS:
394 vcpu->stat.instruction_sigp_store_status++;
395 break;
Eric Farmancd7b4b62015-02-12 09:06:34 -0500396 case SIGP_STORE_ADDITIONAL_STATUS:
397 vcpu->stat.instruction_sigp_store_adtl_status++;
398 break;
David Hildenbrand2444b352014-10-09 14:10:13 +0200399 case SIGP_SET_PREFIX:
400 vcpu->stat.instruction_sigp_prefix++;
401 break;
402 case SIGP_START:
403 vcpu->stat.instruction_sigp_start++;
404 break;
405 case SIGP_RESTART:
406 vcpu->stat.instruction_sigp_restart++;
407 break;
408 case SIGP_INITIAL_CPU_RESET:
409 vcpu->stat.instruction_sigp_init_cpu_reset++;
410 break;
411 case SIGP_CPU_RESET:
412 vcpu->stat.instruction_sigp_cpu_reset++;
413 break;
414 default:
415 vcpu->stat.instruction_sigp_unknown++;
416 }
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200417 VCPU_EVENT(vcpu, 3, "SIGP: order %u for CPU %d handled in userspace",
418 order_code, cpu_addr);
David Hildenbrand2444b352014-10-09 14:10:13 +0200419
420 return 1;
421}
422
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100423int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
424{
425 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
426 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100427 u32 parameter;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100428 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100429 u8 order_code;
430 int rc;
431
Christian Borntraeger3eb77d52008-11-17 14:50:35 +0100432 /* sigp in userspace can exit */
433 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200434 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger3eb77d52008-11-17 14:50:35 +0100435
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300436 order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200437 if (handle_sigp_order_in_user_space(vcpu, order_code, cpu_addr))
David Hildenbrand2444b352014-10-09 14:10:13 +0200438 return -EOPNOTSUPP;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100439
440 if (r1 % 2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100441 parameter = vcpu->run->s.regs.gprs[r1];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100442 else
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100443 parameter = vcpu->run->s.regs.gprs[r1 + 1];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100444
Cornelia Huck5786fff2012-07-23 17:20:29 +0200445 trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100446 switch (order_code) {
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200447 case SIGP_SET_ARCHITECTURE:
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100448 vcpu->stat.instruction_sigp_arch++;
449 rc = __sigp_set_arch(vcpu, parameter);
450 break;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100451 default:
David Hildenbrand3526a662014-03-19 14:57:49 +0100452 rc = handle_sigp_dst(vcpu, order_code, cpu_addr,
453 parameter,
454 &vcpu->run->s.regs.gprs[r1]);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100455 }
456
457 if (rc < 0)
458 return rc;
459
Thomas Huth949c0072013-11-26 12:27:16 +0100460 kvm_s390_set_psw_cc(vcpu, rc);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100461 return 0;
462}
David Hildenbrand49539192014-02-21 08:59:59 +0100463
464/*
465 * Handle SIGP partial execution interception.
466 *
467 * This interception will occur at the source cpu when a source cpu sends an
468 * external call to a target cpu and the target cpu has the WAIT bit set in
469 * its cpuflags. Interception will occurr after the interrupt indicator bits at
470 * the target cpu have been set. All error cases will lead to instruction
471 * interception, therefore nothing is to be checked or prepared.
472 */
473int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu)
474{
475 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
476 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
477 struct kvm_vcpu *dest_vcpu;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300478 u8 order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100479
480 trace_kvm_s390_handle_sigp_pei(vcpu, order_code, cpu_addr);
481
482 if (order_code == SIGP_EXTERNAL_CALL) {
David Hildenbrand152e9f62015-11-05 09:06:06 +0100483 dest_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
David Hildenbrand49539192014-02-21 08:59:59 +0100484 BUG_ON(dest_vcpu == NULL);
485
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200486 kvm_s390_vcpu_wakeup(dest_vcpu);
David Hildenbrand49539192014-02-21 08:59:59 +0100487 kvm_s390_set_psw_cc(vcpu, SIGP_CC_ORDER_CODE_ACCEPTED);
488 return 0;
489 }
490
491 return -EOPNOTSUPP;
492}