blob: 8fe034beb623217f42bc990c58dea9984426ef30 [file] [log] [blame]
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * in-kernel handling for sie intercepts
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01003 *
Thomas Huth9a558ee2014-02-03 10:42:30 +01004 * Copyright IBM Corp. 2008, 2014
Christian Borntraeger8f2abe62008-03-25 18:47:23 +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_host.h>
15#include <linux/errno.h>
16#include <linux/pagemap.h>
17
18#include <asm/kvm_host.h>
Michael Muellera86dcc242014-03-13 19:29:09 +010019#include <asm/asm-offsets.h>
Thomas Huthf14d82e2014-01-15 16:46:07 +010020#include <asm/irq.h>
QingFeng Haob7c92f12017-09-29 12:41:50 +020021#include <asm/sysinfo.h>
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010022
23#include "kvm-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010024#include "gaccess.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020025#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020026#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010027
Cornelia Huckf379aae2012-12-20 15:32:10 +010028
Cornelia Huck77975352012-12-20 15:32:06 +010029static const intercept_handler_t instruction_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +020030 [0x01] = kvm_s390_handle_01,
Cornelia Huck48a3e952012-12-20 15:32:09 +010031 [0x82] = kvm_s390_handle_lpsw,
Christian Borntraegere28acfe2008-03-25 18:47:34 +010032 [0x83] = kvm_s390_handle_diag,
Fan Zhang80cd8762016-08-15 04:53:22 +020033 [0xaa] = kvm_s390_handle_aa,
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010034 [0xae] = kvm_s390_handle_sigp,
Christian Borntraeger70455a32009-01-22 10:28:29 +010035 [0xb2] = kvm_s390_handle_b2,
David Hildenbrandaba07502014-01-23 10:47:13 +010036 [0xb6] = kvm_s390_handle_stctl,
Thomas Huth953ed882013-06-20 17:22:04 +020037 [0xb7] = kvm_s390_handle_lctl,
Cornelia Huck48a3e952012-12-20 15:32:09 +010038 [0xb9] = kvm_s390_handle_b9,
Fan Zhang4e0b1ab2016-11-29 07:17:55 +010039 [0xe3] = kvm_s390_handle_e3,
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +020040 [0xe5] = kvm_s390_handle_e5,
Thomas Huth953ed882013-06-20 17:22:04 +020041 [0xeb] = kvm_s390_handle_eb,
Carsten Otteba5c1e92008-03-25 18:47:26 +010042};
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010043
David Hildenbrand0e8bc062015-11-04 13:47:58 +010044u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
Thomas Huth04b41ac2014-11-12 17:13:29 +010045{
46 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
David Hildenbrand0e8bc062015-11-04 13:47:58 +010047 u8 ilen = 0;
Thomas Huth04b41ac2014-11-12 17:13:29 +010048
David Hildenbrand0e8bc062015-11-04 13:47:58 +010049 switch (vcpu->arch.sie_block->icptcode) {
50 case ICPT_INST:
51 case ICPT_INSTPROGI:
52 case ICPT_OPEREXC:
53 case ICPT_PARTEXEC:
54 case ICPT_IOINST:
55 /* instruction only stored for these icptcodes */
56 ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
57 /* Use the length of the EXECUTE instruction if necessary */
58 if (sie_block->icptstatus & 1) {
59 ilen = (sie_block->icptstatus >> 4) & 0x6;
60 if (!ilen)
61 ilen = 4;
62 }
63 break;
64 case ICPT_PROGI:
65 /* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
66 ilen = vcpu->arch.sie_block->pgmilc & 0x6;
67 break;
Thomas Huth04b41ac2014-11-12 17:13:29 +010068 }
David Hildenbrand0e8bc062015-11-04 13:47:58 +010069 return ilen;
Thomas Huth04b41ac2014-11-12 17:13:29 +010070}
71
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010072static int handle_noop(struct kvm_vcpu *vcpu)
73{
74 switch (vcpu->arch.sie_block->icptcode) {
75 case 0x10:
76 vcpu->stat.exit_external_request++;
77 break;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010078 default:
79 break; /* nothing */
80 }
81 return 0;
82}
83
84static int handle_stop(struct kvm_vcpu *vcpu)
85{
David Hildenbrand6cddd432014-10-15 16:48:53 +020086 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +020087 int rc = 0;
David Hildenbrand6cddd432014-10-15 16:48:53 +020088 uint8_t flags, stop_pending;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010089
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010090 vcpu->stat.exit_stop_request++;
Cornelia Huckade38c32012-07-23 17:20:30 +020091
David Hildenbrand9a022062014-08-05 17:40:47 +020092 /* delay the stop if any non-stop irq is pending */
93 if (kvm_s390_vcpu_has_irq(vcpu, 1))
94 return 0;
95
David Hildenbrand6cddd432014-10-15 16:48:53 +020096 /* avoid races with the injection/SIGP STOP code */
97 spin_lock(&li->lock);
98 flags = li->irq.stop.flags;
99 stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
100 spin_unlock(&li->lock);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200101
David Hildenbrand6cddd432014-10-15 16:48:53 +0200102 trace_kvm_s390_stop_request(stop_pending, flags);
103 if (!stop_pending)
David Hildenbrand32f5ff632014-04-14 12:40:03 +0200104 return 0;
105
David Hildenbrand6cddd432014-10-15 16:48:53 +0200106 if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
Jens Freimann9e0d5472012-02-06 10:59:03 +0100107 rc = kvm_s390_vcpu_store_status(vcpu,
108 KVM_S390_STORE_STATUS_NOADDR);
David Hildenbrand32f5ff632014-04-14 12:40:03 +0200109 if (rc)
110 return rc;
111 }
112
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200113 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
114 kvm_s390_vcpu_stop(vcpu);
David Hildenbrand32f5ff632014-04-14 12:40:03 +0200115 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100116}
117
118static int handle_validity(struct kvm_vcpu *vcpu)
119{
120 int viwhy = vcpu->arch.sie_block->ipb >> 16;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200121
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100122 vcpu->stat.exit_validity++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200123 trace_kvm_s390_intercept_validity(vcpu, viwhy);
Christian Borntraegera5efb6b2016-09-28 16:18:47 +0200124 KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%pK)", viwhy,
125 current->pid, vcpu->kvm);
126
127 /* do not warn on invalid runtime instrumentation mode */
128 WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n",
129 viwhy);
130 return -EINVAL;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100131}
132
Carsten Otteba5c1e92008-03-25 18:47:26 +0100133static int handle_instruction(struct kvm_vcpu *vcpu)
134{
135 intercept_handler_t handler;
136
137 vcpu->stat.exit_instruction++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200138 trace_kvm_s390_intercept_instruction(vcpu,
139 vcpu->arch.sie_block->ipa,
140 vcpu->arch.sie_block->ipb);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100141 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
142 if (handler)
143 return handler(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100144 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100145}
146
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100147static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
David Hildenbrand439716a2014-03-03 10:54:33 +0100148{
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100149 struct kvm_s390_pgm_info pgm_info = {
150 .code = vcpu->arch.sie_block->iprcc,
David Hildenbrandeaa4f412015-11-04 16:46:55 +0100151 /* the PSW has already been rewound */
152 .flags = KVM_S390_PGM_FLAGS_NO_REWIND,
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100153 };
David Hildenbrand439716a2014-03-03 10:54:33 +0100154
155 switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
156 case PGM_AFX_TRANSLATION:
157 case PGM_ASX_TRANSLATION:
158 case PGM_EX_TRANSLATION:
159 case PGM_LFX_TRANSLATION:
160 case PGM_LSTE_SEQUENCE:
161 case PGM_LSX_TRANSLATION:
162 case PGM_LX_TRANSLATION:
163 case PGM_PRIMARY_AUTHORITY:
164 case PGM_SECONDARY_AUTHORITY:
165 case PGM_SPACE_SWITCH:
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100166 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
David Hildenbrand439716a2014-03-03 10:54:33 +0100167 break;
168 case PGM_ALEN_TRANSLATION:
169 case PGM_ALE_SEQUENCE:
170 case PGM_ASTE_INSTANCE:
171 case PGM_ASTE_SEQUENCE:
172 case PGM_ASTE_VALIDITY:
173 case PGM_EXTENDED_AUTHORITY:
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100174 pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
David Hildenbrand439716a2014-03-03 10:54:33 +0100175 break;
176 case PGM_ASCE_TYPE:
177 case PGM_PAGE_TRANSLATION:
178 case PGM_REGION_FIRST_TRANS:
179 case PGM_REGION_SECOND_TRANS:
180 case PGM_REGION_THIRD_TRANS:
181 case PGM_SEGMENT_TRANSLATION:
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100182 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
183 pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
184 pgm_info.op_access_id = vcpu->arch.sie_block->oai;
David Hildenbrand439716a2014-03-03 10:54:33 +0100185 break;
186 case PGM_MONITOR:
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100187 pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn;
188 pgm_info.mon_code = vcpu->arch.sie_block->tecmc;
David Hildenbrand439716a2014-03-03 10:54:33 +0100189 break;
Eric Farman403c8642015-02-02 15:01:06 -0500190 case PGM_VECTOR_PROCESSING:
David Hildenbrand439716a2014-03-03 10:54:33 +0100191 case PGM_DATA:
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100192 pgm_info.data_exc_code = vcpu->arch.sie_block->dxc;
David Hildenbrand439716a2014-03-03 10:54:33 +0100193 break;
194 case PGM_PROTECTION:
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100195 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
196 pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
David Hildenbrand439716a2014-03-03 10:54:33 +0100197 break;
198 default:
199 break;
200 }
201
202 if (vcpu->arch.sie_block->iprcc & PGM_PER) {
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100203 pgm_info.per_code = vcpu->arch.sie_block->perc;
204 pgm_info.per_atmid = vcpu->arch.sie_block->peratmid;
205 pgm_info.per_address = vcpu->arch.sie_block->peraddr;
206 pgm_info.per_access_id = vcpu->arch.sie_block->peraid;
David Hildenbrand439716a2014-03-03 10:54:33 +0100207 }
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100208 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
David Hildenbrand439716a2014-03-03 10:54:33 +0100209}
210
Michael Muellere325fe62014-03-13 12:16:45 +0100211/*
212 * restore ITDB to program-interruption TDB in guest lowcore
213 * and set TX abort indication if required
214*/
215static int handle_itdb(struct kvm_vcpu *vcpu)
216{
217 struct kvm_s390_itdb *itdb;
218 int rc;
219
220 if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
221 return 0;
222 if (current->thread.per_flags & PER_FLAG_NO_TE)
223 return 0;
224 itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
225 rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
226 if (rc)
227 return rc;
228 memset(itdb, 0, sizeof(*itdb));
229
230 return 0;
231}
232
David Hildenbrand27291e22014-01-23 12:26:52 +0100233#define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
234
Carsten Otteba5c1e92008-03-25 18:47:26 +0100235static int handle_prog(struct kvm_vcpu *vcpu)
236{
Thomas Huth684135e2014-04-17 09:57:10 +0200237 psw_t psw;
Heiko Carstens0040e7d2014-01-01 16:37:47 +0100238 int rc;
239
Carsten Otteba5c1e92008-03-25 18:47:26 +0100240 vcpu->stat.exit_program_interruption++;
Michael Mueller7feb6bb2013-06-28 13:30:24 +0200241
David Hildenbrand27291e22014-01-23 12:26:52 +0100242 if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
David Hildenbranda69cbe82016-05-24 12:40:11 +0200243 rc = kvm_s390_handle_per_event(vcpu);
244 if (rc)
245 return rc;
David Hildenbrand27291e22014-01-23 12:26:52 +0100246 /* the interrupt might have been filtered out completely */
247 if (vcpu->arch.sie_block->iprcc == 0)
248 return 0;
249 }
250
Michael Muellere325fe62014-03-13 12:16:45 +0100251 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
Thomas Huth684135e2014-04-17 09:57:10 +0200252 if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
253 rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
254 if (rc)
255 return rc;
256 /* Avoid endless loops of specification exceptions */
257 if (!is_valid_psw(&psw))
258 return -EOPNOTSUPP;
259 }
Michael Muellere325fe62014-03-13 12:16:45 +0100260 rc = handle_itdb(vcpu);
Heiko Carstens0040e7d2014-01-01 16:37:47 +0100261 if (rc)
262 return rc;
David Hildenbrand439716a2014-03-03 10:54:33 +0100263
David Hildenbrandf6af84e2015-11-04 16:22:19 +0100264 return inject_prog_on_prog_intercept(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100265}
266
Thomas Huth9a558ee2014-02-03 10:42:30 +0100267/**
Thomas Huthf14d82e2014-01-15 16:46:07 +0100268 * handle_external_interrupt - used for external interruption interceptions
269 *
270 * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
271 * the new PSW does not have external interrupts disabled. In the first case,
272 * we've got to deliver the interrupt manually, and in the second case, we
273 * drop to userspace to handle the situation there.
274 */
275static int handle_external_interrupt(struct kvm_vcpu *vcpu)
276{
277 u16 eic = vcpu->arch.sie_block->eic;
Jens Freimann383d0b02014-07-29 15:11:49 +0200278 struct kvm_s390_irq irq;
Thomas Huthf14d82e2014-01-15 16:46:07 +0100279 psw_t newpsw;
280 int rc;
281
282 vcpu->stat.exit_external_interrupt++;
283
284 rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
285 if (rc)
286 return rc;
287 /* We can not handle clock comparator or timer interrupt with bad PSW */
288 if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
289 (newpsw.mask & PSW_MASK_EXT))
290 return -EOPNOTSUPP;
291
292 switch (eic) {
293 case EXT_IRQ_CLK_COMP:
294 irq.type = KVM_S390_INT_CLOCK_COMP;
295 break;
296 case EXT_IRQ_CPU_TIMER:
297 irq.type = KVM_S390_INT_CPU_TIMER;
298 break;
299 case EXT_IRQ_EXTERNAL_CALL:
300 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Jens Freimann383d0b02014-07-29 15:11:49 +0200301 irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
David Hildenbrandea5f4962014-10-14 15:29:30 +0200302 rc = kvm_s390_inject_vcpu(vcpu, &irq);
303 /* ignore if another external call is already pending */
304 if (rc == -EBUSY)
305 return 0;
306 return rc;
Thomas Huthf14d82e2014-01-15 16:46:07 +0100307 default:
308 return -EOPNOTSUPP;
309 }
310
311 return kvm_s390_inject_vcpu(vcpu, &irq);
312}
313
314/**
Thomas Huth9a558ee2014-02-03 10:42:30 +0100315 * Handle MOVE PAGE partial execution interception.
316 *
317 * This interception can only happen for guests with DAT disabled and
318 * addresses that are currently not mapped in the host. Thus we try to
319 * set up the mappings for the corresponding user pages here (or throw
320 * addressing exceptions in case of illegal guest addresses).
321 */
322static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
323{
Thomas Huthf22166d2014-05-07 11:44:17 +0200324 unsigned long srcaddr, dstaddr;
Thomas Huth9a558ee2014-02-03 10:42:30 +0100325 int reg1, reg2, rc;
326
327 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Thomas Huth9a558ee2014-02-03 10:42:30 +0100328
329 /* Make sure that the source is paged-in */
Thomas Huth3cfad022015-01-07 16:27:02 +0100330 rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg2],
David Hildenbrand92c96322015-11-16 15:42:11 +0100331 reg2, &srcaddr, GACC_FETCH);
Thomas Huth3cfad022015-01-07 16:27:02 +0100332 if (rc)
333 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huthf22166d2014-05-07 11:44:17 +0200334 rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
335 if (rc != 0)
Thomas Huth9a558ee2014-02-03 10:42:30 +0100336 return rc;
337
338 /* Make sure that the destination is paged-in */
Thomas Huth3cfad022015-01-07 16:27:02 +0100339 rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg1],
David Hildenbrand92c96322015-11-16 15:42:11 +0100340 reg1, &dstaddr, GACC_STORE);
Thomas Huth3cfad022015-01-07 16:27:02 +0100341 if (rc)
342 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huthf22166d2014-05-07 11:44:17 +0200343 rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
344 if (rc != 0)
Thomas Huth9a558ee2014-02-03 10:42:30 +0100345 return rc;
346
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100347 kvm_s390_retry_instr(vcpu);
Thomas Huth9a558ee2014-02-03 10:42:30 +0100348
349 return 0;
350}
351
352static int handle_partial_execution(struct kvm_vcpu *vcpu)
353{
Alexander Yarygin9ec6de12016-05-06 16:33:06 +0300354 vcpu->stat.exit_pei++;
355
Thomas Huth9a558ee2014-02-03 10:42:30 +0100356 if (vcpu->arch.sie_block->ipa == 0xb254) /* MVPG */
357 return handle_mvpg_pei(vcpu);
David Hildenbrand49539192014-02-21 08:59:59 +0100358 if (vcpu->arch.sie_block->ipa >> 8 == 0xae) /* SIGP */
359 return kvm_s390_handle_sigp_pei(vcpu);
Thomas Huth9a558ee2014-02-03 10:42:30 +0100360
361 return -EOPNOTSUPP;
362}
363
QingFeng Haob7c92f12017-09-29 12:41:50 +0200364/*
365 * Handle the sthyi instruction that provides the guest with system
366 * information, like current CPU resources available at each level of
367 * the machine.
368 */
369int handle_sthyi(struct kvm_vcpu *vcpu)
370{
371 int reg1, reg2, r = 0;
372 u64 code, addr, cc = 0, rc = 0;
373 struct sthyi_sctns *sctns = NULL;
374
375 if (!test_kvm_facility(vcpu->kvm, 74))
376 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
377
QingFeng Haob7c92f12017-09-29 12:41:50 +0200378 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
379 code = vcpu->run->s.regs.gprs[reg1];
380 addr = vcpu->run->s.regs.gprs[reg2];
381
382 vcpu->stat.instruction_sthyi++;
383 VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
384 trace_kvm_s390_handle_sthyi(vcpu, code, addr);
385
386 if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
387 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
388
389 if (code & 0xffff) {
390 cc = 3;
391 rc = 4;
392 goto out;
393 }
394
395 if (addr & ~PAGE_MASK)
396 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
397
398 sctns = (void *)get_zeroed_page(GFP_KERNEL);
399 if (!sctns)
400 return -ENOMEM;
401
402 cc = sthyi_fill(sctns, &rc);
403
404out:
405 if (!cc) {
406 r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
407 if (r) {
408 free_page((unsigned long)sctns);
409 return kvm_s390_inject_prog_cond(vcpu, r);
410 }
411 }
412
413 free_page((unsigned long)sctns);
414 vcpu->run->s.regs.gprs[reg2 + 1] = rc;
415 kvm_s390_set_psw_cc(vcpu, cc);
416 return r;
417}
418
Janosch Franka011eeb2016-05-09 14:14:01 +0200419static int handle_operexc(struct kvm_vcpu *vcpu)
420{
Christian Borntraegerfb7dc1d2017-01-26 20:45:33 +0100421 psw_t oldpsw, newpsw;
422 int rc;
423
Janosch Franka011eeb2016-05-09 14:14:01 +0200424 vcpu->stat.exit_operation_exception++;
425 trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
426 vcpu->arch.sie_block->ipb);
427
Christian Borntraegerc0a6bfd2017-02-27 21:14:47 +0100428 if (vcpu->arch.sie_block->ipa == 0xb256)
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200429 return handle_sthyi(vcpu);
430
David Hildenbrand6502a342016-06-21 14:19:51 +0200431 if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
432 return -EOPNOTSUPP;
Christian Borntraegerfb7dc1d2017-01-26 20:45:33 +0100433 rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &newpsw, sizeof(psw_t));
434 if (rc)
435 return rc;
436 /*
437 * Avoid endless loops of operation exceptions, if the pgm new
438 * PSW will cause a new operation exception.
439 * The heuristic checks if the pgm new psw is within 6 bytes before
440 * the faulting psw address (with same DAT, AS settings) and the
441 * new psw is not a wait psw and the fault was not triggered by
442 * problem state.
443 */
444 oldpsw = vcpu->arch.sie_block->gpsw;
445 if (oldpsw.addr - newpsw.addr <= 6 &&
446 !(newpsw.mask & PSW_MASK_WAIT) &&
447 !(oldpsw.mask & PSW_MASK_PSTATE) &&
448 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
449 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT))
450 return -EOPNOTSUPP;
David Hildenbrand6502a342016-06-21 14:19:51 +0200451
Janosch Franka011eeb2016-05-09 14:14:01 +0200452 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
453}
454
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100455int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
456{
David Hildenbrand5ffe4662016-05-24 12:10:27 +0200457 int rc, per_rc = 0;
458
David Hildenbrand71f116b2015-10-19 16:24:28 +0200459 if (kvm_is_ucontrol(vcpu->kvm))
460 return -EOPNOTSUPP;
461
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200462 switch (vcpu->arch.sie_block->icptcode) {
Farhan Ali947b8972017-03-17 09:59:32 -0400463 case ICPT_EXTREQ:
464 case ICPT_IOREQ:
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200465 return handle_noop(vcpu);
Farhan Ali947b8972017-03-17 09:59:32 -0400466 case ICPT_INST:
David Hildenbrand5ffe4662016-05-24 12:10:27 +0200467 rc = handle_instruction(vcpu);
468 break;
Farhan Ali947b8972017-03-17 09:59:32 -0400469 case ICPT_PROGI:
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200470 return handle_prog(vcpu);
Farhan Ali947b8972017-03-17 09:59:32 -0400471 case ICPT_EXTINT:
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200472 return handle_external_interrupt(vcpu);
Farhan Ali947b8972017-03-17 09:59:32 -0400473 case ICPT_WAIT:
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200474 return kvm_s390_handle_wait(vcpu);
Farhan Ali947b8972017-03-17 09:59:32 -0400475 case ICPT_VALIDITY:
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200476 return handle_validity(vcpu);
Farhan Ali947b8972017-03-17 09:59:32 -0400477 case ICPT_STOP:
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200478 return handle_stop(vcpu);
Farhan Ali947b8972017-03-17 09:59:32 -0400479 case ICPT_OPEREXC:
David Hildenbrand5ffe4662016-05-24 12:10:27 +0200480 rc = handle_operexc(vcpu);
481 break;
Farhan Ali947b8972017-03-17 09:59:32 -0400482 case ICPT_PARTEXEC:
David Hildenbrand5ffe4662016-05-24 12:10:27 +0200483 rc = handle_partial_execution(vcpu);
484 break;
Farhan Ali730cd632017-02-24 16:12:56 -0500485 case ICPT_KSS:
486 rc = kvm_s390_skey_check_enable(vcpu);
487 break;
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200488 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100489 return -EOPNOTSUPP;
Christian Borntraeger46b708e2015-07-23 15:24:03 +0200490 }
David Hildenbrand5ffe4662016-05-24 12:10:27 +0200491
492 /* process PER, also if the instrution is processed in user space */
493 if (vcpu->arch.sie_block->icptstatus & 0x02 &&
494 (!rc || rc == -EOPNOTSUPP))
495 per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
496 return per_rc ? per_rc : rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100497}