blob: 683036c1c92a8f9428622c8ba5b897b86cd3e6ae [file] [log] [blame]
Greg Kroah-Hartmand809aa22017-11-24 15:00:33 +01001// SPDX-License-Identifier: GPL-2.0
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01002/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * handling interprocessor communication
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01004 *
Thomas Huthb13d3582013-11-21 16:01:48 +01005 * Copyright IBM Corp. 2008, 2013
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01006 *
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01007 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02009 * Christian Ehrhardt <ehrhardt@de.ibm.com>
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010010 */
11
12#include <linux/kvm.h>
13#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Heiko Carstensa9ae32c2012-06-04 12:55:15 +020015#include <asm/sigp.h>
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010016#include "gaccess.h"
17#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020018#include "trace.h"
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010019
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010020static int __sigp_sense(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010021 u64 *reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010022{
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +010023 const bool stopped = kvm_s390_test_cpuflags(dst_vcpu, CPUSTAT_STOPPED);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010024 int rc;
David Hildenbrandea5f4962014-10-14 15:29:30 +020025 int ext_call_pending;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010026
David Hildenbrandea5f4962014-10-14 15:29:30 +020027 ext_call_pending = kvm_s390_ext_call_pending(dst_vcpu);
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +010028 if (!stopped && !ext_call_pending)
Cornelia Huck21b26c02012-06-26 16:06:41 +020029 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
30 else {
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010031 *reg &= 0xffffffff00000000UL;
David Hildenbrandea5f4962014-10-14 15:29:30 +020032 if (ext_call_pending)
Cornelia Huck21b26c02012-06-26 16:06:41 +020033 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +010034 if (stopped)
Cornelia Huck21b26c02012-06-26 16:06:41 +020035 *reg |= SIGP_STATUS_STOPPED;
Heiko Carstensea1918d2012-06-26 16:06:40 +020036 rc = SIGP_CC_STATUS_STORED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010037 }
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010038
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010039 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", dst_vcpu->vcpu_id,
40 rc);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010041 return rc;
42}
43
David Hildenbrand07b03032014-06-06 10:24:15 +020044static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
45 struct kvm_vcpu *dst_vcpu)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010046{
Jens Freimann383d0b02014-07-29 15:11:49 +020047 struct kvm_s390_irq irq = {
Jens Freimann22ff4a32014-05-14 17:14:46 +020048 .type = KVM_S390_INT_EMERGENCY,
Jens Freimann383d0b02014-07-29 15:11:49 +020049 .u.emerg.code = vcpu->vcpu_id,
Jens Freimann22ff4a32014-05-14 17:14:46 +020050 };
Jens Freimann22ff4a32014-05-14 17:14:46 +020051 int rc = 0;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010052
Jens Freimann383d0b02014-07-29 15:11:49 +020053 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
Jens Freimann22ff4a32014-05-14 17:14:46 +020054 if (!rc)
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010055 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x",
56 dst_vcpu->vcpu_id);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010057
Jens Freimann22ff4a32014-05-14 17:14:46 +020058 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020059}
60
David Hildenbrand07b03032014-06-06 10:24:15 +020061static int __sigp_emergency(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
62{
63 return __inject_sigp_emergency(vcpu, dst_vcpu);
64}
65
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010066static int __sigp_conditional_emergency(struct kvm_vcpu *vcpu,
67 struct kvm_vcpu *dst_vcpu,
Thomas Huthb13d3582013-11-21 16:01:48 +010068 u16 asn, u64 *reg)
69{
Thomas Huthb13d3582013-11-21 16:01:48 +010070 const u64 psw_int_mask = PSW_MASK_IO | PSW_MASK_EXT;
71 u16 p_asn, s_asn;
72 psw_t *psw;
David Hildenbrand94a15de2016-02-18 10:15:43 +010073 bool idle;
Thomas Huthb13d3582013-11-21 16:01:48 +010074
David Hildenbrand94a15de2016-02-18 10:15:43 +010075 idle = is_vcpu_idle(vcpu);
Thomas Huthb13d3582013-11-21 16:01:48 +010076 psw = &dst_vcpu->arch.sie_block->gpsw;
77 p_asn = dst_vcpu->arch.sie_block->gcr[4] & 0xffff; /* Primary ASN */
78 s_asn = dst_vcpu->arch.sie_block->gcr[3] & 0xffff; /* Secondary ASN */
79
David Hildenbrand07b03032014-06-06 10:24:15 +020080 /* Inject the emergency signal? */
David Hildenbrand94a15de2016-02-18 10:15:43 +010081 if (!is_vcpu_stopped(vcpu)
Thomas Huthb13d3582013-11-21 16:01:48 +010082 || (psw->mask & psw_int_mask) != psw_int_mask
David Hildenbrand94a15de2016-02-18 10:15:43 +010083 || (idle && psw->addr != 0)
84 || (!idle && (asn == p_asn || asn == s_asn))) {
David Hildenbrand07b03032014-06-06 10:24:15 +020085 return __inject_sigp_emergency(vcpu, dst_vcpu);
Thomas Huthb13d3582013-11-21 16:01:48 +010086 } else {
87 *reg &= 0xffffffff00000000UL;
88 *reg |= SIGP_STATUS_INCORRECT_STATE;
89 return SIGP_CC_STATUS_STORED;
90 }
91}
92
David Hildenbrand3d95c7d2014-03-19 16:59:39 +010093static int __sigp_external_call(struct kvm_vcpu *vcpu,
David Hildenbrandea5f4962014-10-14 15:29:30 +020094 struct kvm_vcpu *dst_vcpu, u64 *reg)
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020095{
Jens Freimann383d0b02014-07-29 15:11:49 +020096 struct kvm_s390_irq irq = {
Jens Freimann22ff4a32014-05-14 17:14:46 +020097 .type = KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +020098 .u.extcall.code = vcpu->vcpu_id,
Jens Freimann22ff4a32014-05-14 17:14:46 +020099 };
Jens Freimann22ff4a32014-05-14 17:14:46 +0200100 int rc;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200101
Jens Freimann383d0b02014-07-29 15:11:49 +0200102 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
David Hildenbrandea5f4962014-10-14 15:29:30 +0200103 if (rc == -EBUSY) {
104 *reg &= 0xffffffff00000000UL;
105 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
106 return SIGP_CC_STATUS_STORED;
107 } else if (rc == 0) {
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100108 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x",
109 dst_vcpu->vcpu_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +0200110 }
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200111
Jens Freimann22ff4a32014-05-14 17:14:46 +0200112 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100113}
114
David Hildenbranda6cc3102014-06-06 10:25:09 +0200115static int __sigp_stop(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100116{
David Hildenbrand6cddd432014-10-15 16:48:53 +0200117 struct kvm_s390_irq irq = {
118 .type = KVM_S390_SIGP_STOP,
119 };
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100120 int rc;
121
David Hildenbrand6cddd432014-10-15 16:48:53 +0200122 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
123 if (rc == -EBUSY)
124 rc = SIGP_CC_BUSY;
125 else if (rc == 0)
126 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x",
127 dst_vcpu->vcpu_id);
Thomas Huthe8798922013-11-06 15:46:33 +0100128
David Hildenbranda6cc3102014-06-06 10:25:09 +0200129 return rc;
130}
131
132static int __sigp_stop_and_store_status(struct kvm_vcpu *vcpu,
133 struct kvm_vcpu *dst_vcpu, u64 *reg)
134{
David Hildenbrand6cddd432014-10-15 16:48:53 +0200135 struct kvm_s390_irq irq = {
136 .type = KVM_S390_SIGP_STOP,
137 .u.stop.flags = KVM_S390_STOP_FLAG_STORE_STATUS,
138 };
David Hildenbranda6cc3102014-06-06 10:25:09 +0200139 int rc;
140
David Hildenbrand6cddd432014-10-15 16:48:53 +0200141 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
142 if (rc == -EBUSY)
143 rc = SIGP_CC_BUSY;
144 else if (rc == 0)
145 VCPU_EVENT(vcpu, 4, "sent sigp stop and store status to cpu %x",
146 dst_vcpu->vcpu_id);
Thomas Huthe8798922013-11-06 15:46:33 +0100147
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100148 return rc;
149}
150
Jason J. Herneb697e432017-02-24 10:01:30 -0500151static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter,
152 u64 *status_reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100153{
Dominik Dingel3c038e62013-10-07 17:11:48 +0200154 unsigned int i;
155 struct kvm_vcpu *v;
Jason J. Herneb697e432017-02-24 10:01:30 -0500156 bool all_stopped = true;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100157
Jason J. Herneb697e432017-02-24 10:01:30 -0500158 kvm_for_each_vcpu(i, v, vcpu->kvm) {
159 if (v == vcpu)
160 continue;
161 if (!is_vcpu_stopped(v))
162 all_stopped = false;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100163 }
Jason J. Herneb697e432017-02-24 10:01:30 -0500164
165 *status_reg &= 0xffffffff00000000UL;
166
167 /* Reject set arch order, with czam we're always in z/Arch mode. */
168 *status_reg |= (all_stopped ? SIGP_STATUS_INVALID_PARAMETER :
169 SIGP_STATUS_INCORRECT_STATE);
170 return SIGP_CC_STATUS_STORED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100171}
172
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100173static int __sigp_set_prefix(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
174 u32 address, u64 *reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100175{
David Hildenbranda3a9c592014-10-14 09:44:55 +0200176 struct kvm_s390_irq irq = {
177 .type = KVM_S390_SIGP_SET_PREFIX,
178 .u.prefix.address = address & 0x7fffe000u,
179 };
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100180 int rc;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100181
Heiko Carstens665170c2014-01-01 16:47:12 +0100182 /*
183 * Make sure the new value is valid memory. We only need to check the
184 * first page, since address is 8k aligned and memory pieces are always
185 * at least 1MB aligned and have at least a size of 1MB.
186 */
David Hildenbranda3a9c592014-10-14 09:44:55 +0200187 if (kvm_is_error_gpa(vcpu->kvm, irq.u.prefix.address)) {
Heiko Carstens07444262012-06-26 16:06:39 +0200188 *reg &= 0xffffffff00000000UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200189 *reg |= SIGP_STATUS_INVALID_PARAMETER;
Heiko Carstensea1918d2012-06-26 16:06:40 +0200190 return SIGP_CC_STATUS_STORED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100191 }
192
David Hildenbranda3a9c592014-10-14 09:44:55 +0200193 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
194 if (rc == -EBUSY) {
Heiko Carstens07444262012-06-26 16:06:39 +0200195 *reg &= 0xffffffff00000000UL;
196 *reg |= SIGP_STATUS_INCORRECT_STATE;
David Hildenbranda3a9c592014-10-14 09:44:55 +0200197 return SIGP_CC_STATUS_STORED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100198 }
199
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100200 return rc;
201}
202
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100203static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu,
204 struct kvm_vcpu *dst_vcpu,
205 u32 addr, u64 *reg)
Thomas Huth00e9e4352013-11-13 20:48:51 +0100206{
Thomas Huth00e9e4352013-11-13 20:48:51 +0100207 int rc;
208
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +0100209 if (!kvm_s390_test_cpuflags(dst_vcpu, CPUSTAT_STOPPED)) {
Thomas Huth00e9e4352013-11-13 20:48:51 +0100210 *reg &= 0xffffffff00000000UL;
211 *reg |= SIGP_STATUS_INCORRECT_STATE;
212 return SIGP_CC_STATUS_STORED;
213 }
214
215 addr &= 0x7ffffe00;
216 rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
217 if (rc == -EFAULT) {
218 *reg &= 0xffffffff00000000UL;
219 *reg |= SIGP_STATUS_INVALID_PARAMETER;
220 rc = SIGP_CC_STATUS_STORED;
221 }
222 return rc;
223}
224
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100225static int __sigp_sense_running(struct kvm_vcpu *vcpu,
226 struct kvm_vcpu *dst_vcpu, u64 *reg)
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100227{
228 int rc;
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100229
David Hildenbrandbd50e8e2016-03-04 12:23:55 +0100230 if (!test_kvm_facility(vcpu->kvm, 9)) {
231 *reg &= 0xffffffff00000000UL;
232 *reg |= SIGP_STATUS_INVALID_ORDER;
233 return SIGP_CC_STATUS_STORED;
234 }
235
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +0100236 if (kvm_s390_test_cpuflags(dst_vcpu, CPUSTAT_RUNNING)) {
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100237 /* running */
238 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
239 } else {
240 /* not running */
241 *reg &= 0xffffffff00000000UL;
242 *reg |= SIGP_STATUS_NOT_RUNNING;
243 rc = SIGP_CC_STATUS_STORED;
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100244 }
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100245
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100246 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x",
247 dst_vcpu->vcpu_id, rc);
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100248
249 return rc;
250}
251
David Hildenbrandb8983832014-05-23 12:22:56 +0200252static int __prepare_sigp_re_start(struct kvm_vcpu *vcpu,
253 struct kvm_vcpu *dst_vcpu, u8 order_code)
Jens Freimann151104a2012-02-08 08:28:29 +0100254{
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100255 struct kvm_s390_local_interrupt *li = &dst_vcpu->arch.local_int;
David Hildenbrandb8983832014-05-23 12:22:56 +0200256 /* handle (RE)START in user space */
257 int rc = -EOPNOTSUPP;
Jens Freimann151104a2012-02-08 08:28:29 +0100258
David Hildenbrand6cddd432014-10-15 16:48:53 +0200259 /* make sure we don't race with STOP irq injection */
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200260 spin_lock(&li->lock);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200261 if (kvm_s390_is_stop_irq_pending(dst_vcpu))
Heiko Carstensea1918d2012-06-26 16:06:40 +0200262 rc = SIGP_CC_BUSY;
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200263 spin_unlock(&li->lock);
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100264
Jens Freimann151104a2012-02-08 08:28:29 +0100265 return rc;
266}
267
David Hildenbrandb8983832014-05-23 12:22:56 +0200268static int __prepare_sigp_cpu_reset(struct kvm_vcpu *vcpu,
269 struct kvm_vcpu *dst_vcpu, u8 order_code)
270{
271 /* handle (INITIAL) CPU RESET in user space */
272 return -EOPNOTSUPP;
273}
274
275static int __prepare_sigp_unknown(struct kvm_vcpu *vcpu,
276 struct kvm_vcpu *dst_vcpu)
277{
278 /* handle unknown orders in user space */
279 return -EOPNOTSUPP;
280}
281
David Hildenbrand3526a662014-03-19 14:57:49 +0100282static int handle_sigp_dst(struct kvm_vcpu *vcpu, u8 order_code,
283 u16 cpu_addr, u32 parameter, u64 *status_reg)
284{
285 int rc;
David Hildenbrand152e9f62015-11-05 09:06:06 +0100286 struct kvm_vcpu *dst_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100287
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100288 if (!dst_vcpu)
289 return SIGP_CC_NOT_OPERATIONAL;
David Hildenbrand3526a662014-03-19 14:57:49 +0100290
291 switch (order_code) {
292 case SIGP_SENSE:
293 vcpu->stat.instruction_sigp_sense++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100294 rc = __sigp_sense(vcpu, dst_vcpu, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100295 break;
296 case SIGP_EXTERNAL_CALL:
297 vcpu->stat.instruction_sigp_external_call++;
David Hildenbrandea5f4962014-10-14 15:29:30 +0200298 rc = __sigp_external_call(vcpu, dst_vcpu, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100299 break;
300 case SIGP_EMERGENCY_SIGNAL:
301 vcpu->stat.instruction_sigp_emergency++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100302 rc = __sigp_emergency(vcpu, dst_vcpu);
David Hildenbrand3526a662014-03-19 14:57:49 +0100303 break;
304 case SIGP_STOP:
305 vcpu->stat.instruction_sigp_stop++;
David Hildenbranda6cc3102014-06-06 10:25:09 +0200306 rc = __sigp_stop(vcpu, dst_vcpu);
David Hildenbrand3526a662014-03-19 14:57:49 +0100307 break;
308 case SIGP_STOP_AND_STORE_STATUS:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200309 vcpu->stat.instruction_sigp_stop_store_status++;
David Hildenbranda6cc3102014-06-06 10:25:09 +0200310 rc = __sigp_stop_and_store_status(vcpu, dst_vcpu, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100311 break;
312 case SIGP_STORE_STATUS_AT_ADDRESS:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200313 vcpu->stat.instruction_sigp_store_status++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100314 rc = __sigp_store_status_at_addr(vcpu, dst_vcpu, parameter,
David Hildenbrand3526a662014-03-19 14:57:49 +0100315 status_reg);
316 break;
317 case SIGP_SET_PREFIX:
318 vcpu->stat.instruction_sigp_prefix++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100319 rc = __sigp_set_prefix(vcpu, dst_vcpu, parameter, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100320 break;
321 case SIGP_COND_EMERGENCY_SIGNAL:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200322 vcpu->stat.instruction_sigp_cond_emergency++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100323 rc = __sigp_conditional_emergency(vcpu, dst_vcpu, parameter,
David Hildenbrand3526a662014-03-19 14:57:49 +0100324 status_reg);
325 break;
326 case SIGP_SENSE_RUNNING:
327 vcpu->stat.instruction_sigp_sense_running++;
David Hildenbrand3d95c7d2014-03-19 16:59:39 +0100328 rc = __sigp_sense_running(vcpu, dst_vcpu, status_reg);
David Hildenbrand3526a662014-03-19 14:57:49 +0100329 break;
330 case SIGP_START:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200331 vcpu->stat.instruction_sigp_start++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200332 rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
David Hildenbrand3526a662014-03-19 14:57:49 +0100333 break;
334 case SIGP_RESTART:
335 vcpu->stat.instruction_sigp_restart++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200336 rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
337 break;
338 case SIGP_INITIAL_CPU_RESET:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200339 vcpu->stat.instruction_sigp_init_cpu_reset++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200340 rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
341 break;
342 case SIGP_CPU_RESET:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200343 vcpu->stat.instruction_sigp_cpu_reset++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200344 rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
David Hildenbrand3526a662014-03-19 14:57:49 +0100345 break;
346 default:
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200347 vcpu->stat.instruction_sigp_unknown++;
David Hildenbrandb8983832014-05-23 12:22:56 +0200348 rc = __prepare_sigp_unknown(vcpu, dst_vcpu);
David Hildenbrand3526a662014-03-19 14:57:49 +0100349 }
350
David Hildenbrandb8983832014-05-23 12:22:56 +0200351 if (rc == -EOPNOTSUPP)
352 VCPU_EVENT(vcpu, 4,
353 "sigp order %u -> cpu %x: handled in user space",
354 order_code, dst_vcpu->vcpu_id);
355
David Hildenbrand3526a662014-03-19 14:57:49 +0100356 return rc;
357}
358
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200359static int handle_sigp_order_in_user_space(struct kvm_vcpu *vcpu, u8 order_code,
360 u16 cpu_addr)
David Hildenbrand2444b352014-10-09 14:10:13 +0200361{
362 if (!vcpu->kvm->arch.user_sigp)
363 return 0;
364
365 switch (order_code) {
366 case SIGP_SENSE:
367 case SIGP_EXTERNAL_CALL:
368 case SIGP_EMERGENCY_SIGNAL:
369 case SIGP_COND_EMERGENCY_SIGNAL:
370 case SIGP_SENSE_RUNNING:
371 return 0;
372 /* update counters as we're directly dropping to user space */
373 case SIGP_STOP:
374 vcpu->stat.instruction_sigp_stop++;
375 break;
376 case SIGP_STOP_AND_STORE_STATUS:
377 vcpu->stat.instruction_sigp_stop_store_status++;
378 break;
379 case SIGP_STORE_STATUS_AT_ADDRESS:
380 vcpu->stat.instruction_sigp_store_status++;
381 break;
Eric Farmancd7b4b62015-02-12 09:06:34 -0500382 case SIGP_STORE_ADDITIONAL_STATUS:
383 vcpu->stat.instruction_sigp_store_adtl_status++;
384 break;
David Hildenbrand2444b352014-10-09 14:10:13 +0200385 case SIGP_SET_PREFIX:
386 vcpu->stat.instruction_sigp_prefix++;
387 break;
388 case SIGP_START:
389 vcpu->stat.instruction_sigp_start++;
390 break;
391 case SIGP_RESTART:
392 vcpu->stat.instruction_sigp_restart++;
393 break;
394 case SIGP_INITIAL_CPU_RESET:
395 vcpu->stat.instruction_sigp_init_cpu_reset++;
396 break;
397 case SIGP_CPU_RESET:
398 vcpu->stat.instruction_sigp_cpu_reset++;
399 break;
400 default:
401 vcpu->stat.instruction_sigp_unknown++;
402 }
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200403 VCPU_EVENT(vcpu, 3, "SIGP: order %u for CPU %d handled in userspace",
404 order_code, cpu_addr);
David Hildenbrand2444b352014-10-09 14:10:13 +0200405
406 return 1;
407}
408
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100409int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
410{
411 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
412 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100413 u32 parameter;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100414 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100415 u8 order_code;
416 int rc;
417
Christian Borntraeger3eb77d52008-11-17 14:50:35 +0100418 /* sigp in userspace can exit */
419 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200420 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger3eb77d52008-11-17 14:50:35 +0100421
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300422 order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200423 if (handle_sigp_order_in_user_space(vcpu, order_code, cpu_addr))
David Hildenbrand2444b352014-10-09 14:10:13 +0200424 return -EOPNOTSUPP;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100425
426 if (r1 % 2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100427 parameter = vcpu->run->s.regs.gprs[r1];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100428 else
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100429 parameter = vcpu->run->s.regs.gprs[r1 + 1];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100430
Cornelia Huck5786fff2012-07-23 17:20:29 +0200431 trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100432 switch (order_code) {
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200433 case SIGP_SET_ARCHITECTURE:
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100434 vcpu->stat.instruction_sigp_arch++;
Jason J. Herneb697e432017-02-24 10:01:30 -0500435 rc = __sigp_set_arch(vcpu, parameter,
436 &vcpu->run->s.regs.gprs[r1]);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100437 break;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100438 default:
David Hildenbrand3526a662014-03-19 14:57:49 +0100439 rc = handle_sigp_dst(vcpu, order_code, cpu_addr,
440 parameter,
441 &vcpu->run->s.regs.gprs[r1]);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100442 }
443
444 if (rc < 0)
445 return rc;
446
Thomas Huth949c0072013-11-26 12:27:16 +0100447 kvm_s390_set_psw_cc(vcpu, rc);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100448 return 0;
449}
David Hildenbrand49539192014-02-21 08:59:59 +0100450
451/*
452 * Handle SIGP partial execution interception.
453 *
454 * This interception will occur at the source cpu when a source cpu sends an
455 * external call to a target cpu and the target cpu has the WAIT bit set in
456 * its cpuflags. Interception will occurr after the interrupt indicator bits at
457 * the target cpu have been set. All error cases will lead to instruction
458 * interception, therefore nothing is to be checked or prepared.
459 */
460int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu)
461{
462 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
463 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
464 struct kvm_vcpu *dest_vcpu;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300465 u8 order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100466
467 trace_kvm_s390_handle_sigp_pei(vcpu, order_code, cpu_addr);
468
469 if (order_code == SIGP_EXTERNAL_CALL) {
David Hildenbrand152e9f62015-11-05 09:06:06 +0100470 dest_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
David Hildenbrand49539192014-02-21 08:59:59 +0100471 BUG_ON(dest_vcpu == NULL);
472
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200473 kvm_s390_vcpu_wakeup(dest_vcpu);
David Hildenbrand49539192014-02-21 08:59:59 +0100474 kvm_s390_set_psw_cc(vcpu, SIGP_CC_ORDER_CODE_ACCEPTED);
475 return 0;
476 }
477
478 return -EOPNOTSUPP;
479}