blob: 93d6cde8c3eae04c7407aba070fa4e75013e17fd [file] [log] [blame]
Christian Borntraeger453423d2008-03-25 18:47:29 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling privileged instructions
Christian Borntraeger453423d2008-03-25 18:47:29 +01003 *
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02004 * Copyright IBM Corp. 2008, 2013
Christian Borntraeger453423d2008-03-25 18:47:29 +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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010016#include <linux/errno.h>
Heiko Carstensb13b5dc2013-03-25 17:22:55 +010017#include <linux/compat.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010018#include <linux/mm_types.h>
19
Heiko Carstens7c959e82013-03-05 13:14:46 +010020#include <asm/asm-offsets.h>
Heiko Carstense769ece2013-07-26 15:04:01 +020021#include <asm/facility.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010022#include <asm/current.h>
23#include <asm/debug.h>
24#include <asm/ebcdic.h>
25#include <asm/sysinfo.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020026#include <asm/pgtable.h>
27#include <asm/pgalloc.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010028#include <asm/gmap.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020029#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010030#include <asm/ptrace.h>
31#include <asm/compat.h>
David Hildenbranda7e19ab2016-05-10 09:50:21 +020032#include <asm/sclp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010033#include "gaccess.h"
34#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020035#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010036
Fan Zhang80cd8762016-08-15 04:53:22 +020037static int handle_ri(struct kvm_vcpu *vcpu)
38{
39 if (test_kvm_facility(vcpu->kvm, 64)) {
Christian Borntraeger4d5f2c02017-02-09 17:15:41 +010040 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
Fan Zhang80cd8762016-08-15 04:53:22 +020041 vcpu->arch.sie_block->ecb3 |= 0x01;
42 kvm_s390_retry_instr(vcpu);
43 return 0;
44 } else
45 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
46}
47
48int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
49{
50 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
51 return handle_ri(vcpu);
52 else
53 return -EOPNOTSUPP;
54}
55
Thomas Huth6a3f95a2013-09-12 10:33:49 +020056/* Handle SCK (SET CLOCK) interception */
57static int handle_set_clock(struct kvm_vcpu *vcpu)
58{
David Hildenbrand25ed1672015-05-12 09:49:14 +020059 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +010060 u8 ar;
David Hildenbrand25ed1672015-05-12 09:49:14 +020061 u64 op2, val;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020062
63 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
64 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
65
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030066 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020067 if (op2 & 7) /* Operand must be on a doubleword boundary */
68 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030069 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010070 if (rc)
71 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020072
Christian Borntraeger7cbde762015-07-21 12:44:57 +020073 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
David Hildenbrand25ed1672015-05-12 09:49:14 +020074 kvm_s390_set_tod_clock(vcpu->kvm, val);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020075
76 kvm_s390_set_psw_cc(vcpu, 0);
77 return 0;
78}
79
Christian Borntraeger453423d2008-03-25 18:47:29 +010080static int handle_set_prefix(struct kvm_vcpu *vcpu)
81{
Christian Borntraeger453423d2008-03-25 18:47:29 +010082 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +010083 u32 address;
84 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +010085 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +010086
87 vcpu->stat.instruction_spx++;
88
Thomas Huth5087dfa2013-06-20 17:22:01 +020089 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
90 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
91
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030092 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +010093
94 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010095 if (operand2 & 3)
96 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010097
98 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030099 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstens665170c2014-01-01 16:47:12 +0100100 if (rc)
101 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100102
Heiko Carstens665170c2014-01-01 16:47:12 +0100103 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100104
Heiko Carstens665170c2014-01-01 16:47:12 +0100105 /*
106 * Make sure the new value is valid memory. We only need to check the
107 * first page, since address is 8k aligned and memory pieces are always
108 * at least 1MB aligned and have at least a size of 1MB.
109 */
110 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100111 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100112
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100113 kvm_s390_set_prefix(vcpu, address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200114 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100115 return 0;
116}
117
118static int handle_store_prefix(struct kvm_vcpu *vcpu)
119{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100120 u64 operand2;
121 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100122 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100123 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100124
125 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100126
Thomas Huth5087dfa2013-06-20 17:22:01 +0200127 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
128 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
129
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300130 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100131
132 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100133 if (operand2 & 3)
134 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100135
Michael Muellerfda902c2014-05-13 16:58:30 +0200136 address = kvm_s390_get_prefix(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100137
138 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300139 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100140 if (rc)
141 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100142
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200143 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200144 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100145 return 0;
146}
147
148static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
149{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100150 u16 vcpu_id = vcpu->vcpu_id;
151 u64 ga;
152 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100153 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100154
155 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100156
Thomas Huth5087dfa2013-06-20 17:22:01 +0200157 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
158 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
159
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300160 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100161
Heiko Carstens8b96de02014-01-01 16:53:27 +0100162 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100163 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100164
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300165 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
Heiko Carstens8b96de02014-01-01 16:53:27 +0100166 if (rc)
167 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100168
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200169 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
Heiko Carstens8b96de02014-01-01 16:53:27 +0100170 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100171 return 0;
172}
173
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200174static int __skey_check_enable(struct kvm_vcpu *vcpu)
Dominik Dingel693ffc02014-01-14 18:11:14 +0100175{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200176 int rc = 0;
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200177
178 trace_kvm_s390_skey_related_inst(vcpu);
Dominik Dingel693ffc02014-01-14 18:11:14 +0100179 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200180 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100181
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200182 rc = s390_enable_skey();
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200183 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
184 if (!rc)
185 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200186 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100187}
188
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200189static int try_handle_skey(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100190{
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200191 int rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100192
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200193 vcpu->stat.instruction_storage_key++;
194 rc = __skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200195 if (rc)
196 return rc;
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200197 if (sclp.has_skey) {
198 /* with storage-key facility, SIE interprets it for us */
199 kvm_s390_retry_instr(vcpu);
200 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
201 return -EAGAIN;
202 }
Thomas Huth5087dfa2013-06-20 17:22:01 +0200203 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
204 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200205 return 0;
206}
Thomas Huth5087dfa2013-06-20 17:22:01 +0200207
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200208static int handle_iske(struct kvm_vcpu *vcpu)
209{
210 unsigned long addr;
211 unsigned char key;
212 int reg1, reg2;
213 int rc;
214
215 rc = try_handle_skey(vcpu);
216 if (rc)
217 return rc != -EAGAIN ? rc : 0;
218
219 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
220
221 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
222 addr = kvm_s390_logical_to_effective(vcpu, addr);
223 addr = kvm_s390_real_to_abs(vcpu, addr);
224 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
225 if (kvm_is_error_hva(addr))
226 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
227
228 down_read(&current->mm->mmap_sem);
229 rc = get_guest_storage_key(current->mm, addr, &key);
230 up_read(&current->mm->mmap_sem);
231 if (rc)
232 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
233 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
234 vcpu->run->s.regs.gprs[reg1] |= key;
235 return 0;
236}
237
238static int handle_rrbe(struct kvm_vcpu *vcpu)
239{
240 unsigned long addr;
241 int reg1, reg2;
242 int rc;
243
244 rc = try_handle_skey(vcpu);
245 if (rc)
246 return rc != -EAGAIN ? rc : 0;
247
248 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
249
250 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
251 addr = kvm_s390_logical_to_effective(vcpu, addr);
252 addr = kvm_s390_real_to_abs(vcpu, addr);
253 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
254 if (kvm_is_error_hva(addr))
255 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
256
257 down_read(&current->mm->mmap_sem);
258 rc = reset_guest_reference_bit(current->mm, addr);
259 up_read(&current->mm->mmap_sem);
260 if (rc < 0)
261 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
262
263 kvm_s390_set_psw_cc(vcpu, rc);
264 return 0;
265}
266
267#define SSKE_NQ 0x8
268#define SSKE_MR 0x4
269#define SSKE_MC 0x2
270#define SSKE_MB 0x1
271static int handle_sske(struct kvm_vcpu *vcpu)
272{
273 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
274 unsigned long start, end;
275 unsigned char key, oldkey;
276 int reg1, reg2;
277 int rc;
278
279 rc = try_handle_skey(vcpu);
280 if (rc)
281 return rc != -EAGAIN ? rc : 0;
282
283 if (!test_kvm_facility(vcpu->kvm, 8))
284 m3 &= ~SSKE_MB;
285 if (!test_kvm_facility(vcpu->kvm, 10))
286 m3 &= ~(SSKE_MC | SSKE_MR);
287 if (!test_kvm_facility(vcpu->kvm, 14))
288 m3 &= ~SSKE_NQ;
289
290 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
291
292 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
293 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
294 start = kvm_s390_logical_to_effective(vcpu, start);
295 if (m3 & SSKE_MB) {
296 /* start already designates an absolute address */
297 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
298 } else {
299 start = kvm_s390_real_to_abs(vcpu, start);
300 end = start + PAGE_SIZE;
301 }
302
303 while (start != end) {
304 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
305
306 if (kvm_is_error_hva(addr))
307 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
308
309 down_read(&current->mm->mmap_sem);
310 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
311 m3 & SSKE_NQ, m3 & SSKE_MR,
312 m3 & SSKE_MC);
313 up_read(&current->mm->mmap_sem);
314 if (rc < 0)
315 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
316 start += PAGE_SIZE;
Heiko Carstens0b925152017-01-02 08:51:02 +0100317 }
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200318
319 if (m3 & (SSKE_MC | SSKE_MR)) {
320 if (m3 & SSKE_MB) {
321 /* skey in reg1 is unpredictable */
322 kvm_s390_set_psw_cc(vcpu, 3);
323 } else {
324 kvm_s390_set_psw_cc(vcpu, rc);
325 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
326 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
327 }
328 }
329 if (m3 & SSKE_MB) {
330 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT)
331 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
332 else
333 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
334 end = kvm_s390_logical_to_effective(vcpu, end);
335 vcpu->run->s.regs.gprs[reg2] |= end;
336 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100337 return 0;
338}
339
Heiko Carstens8a2422342014-01-10 14:33:28 +0100340static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
341{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100342 vcpu->stat.instruction_ipte_interlock++;
Thomas Huth04b41ac2014-11-12 17:13:29 +0100343 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100344 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
345 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100346 kvm_s390_retry_instr(vcpu);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100347 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
348 return 0;
349}
350
Thomas Huthaca84242013-09-12 10:33:48 +0200351static int handle_test_block(struct kvm_vcpu *vcpu)
352{
Thomas Huthaca84242013-09-12 10:33:48 +0200353 gpa_t addr;
354 int reg2;
355
356 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
357 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
358
359 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
360 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100361 addr = kvm_s390_logical_to_effective(vcpu, addr);
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300362 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
Thomas Huthe45efa22014-03-07 12:14:23 +0100363 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200364 addr = kvm_s390_real_to_abs(vcpu, addr);
365
Heiko Carstensef23e772014-01-01 16:53:49 +0100366 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200367 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
368 /*
369 * We don't expect errors on modern systems, and do not care
370 * about storage keys (yet), so let's just clear the page.
371 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100372 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200373 return -EFAULT;
374 kvm_s390_set_psw_cc(vcpu, 0);
375 vcpu->run->s.regs.gprs[0] = 0;
376 return 0;
377}
378
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100379static int handle_tpi(struct kvm_vcpu *vcpu)
380{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100381 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100382 unsigned long len;
383 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100384 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100385 u64 addr;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100386 u8 ar;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100387
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300388 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100389 if (addr & 3)
390 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100391
Thomas Huthf0926692013-10-09 14:15:54 +0200392 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100393 if (!inti) {
394 kvm_s390_set_psw_cc(vcpu, 0);
395 return 0;
396 }
397
Heiko Carstens4799b552014-01-01 16:55:48 +0100398 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
399 tpi_data[1] = inti->io.io_int_parm;
400 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100401 if (addr) {
402 /*
403 * Store the two-word I/O interruption code into the
404 * provided area.
405 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100406 len = sizeof(tpi_data) - 4;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300407 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100408 if (rc) {
409 rc = kvm_s390_inject_prog_cond(vcpu, rc);
410 goto reinject_interrupt;
411 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100412 } else {
413 /*
414 * Store the three-word I/O interruption code into
415 * the appropriate lowcore area.
416 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100417 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100418 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
419 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100420 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100421 goto reinject_interrupt;
422 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100423 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100424
425 /* irq was successfully handed to the guest */
426 kfree(inti);
427 kvm_s390_set_psw_cc(vcpu, 1);
428 return 0;
429reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100430 /*
431 * If we encounter a problem storing the interruption code, the
432 * instruction is suppressed from the guest's view: reinject the
433 * interrupt.
434 */
David Hildenbrand15462e32015-02-04 15:59:11 +0100435 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
436 kfree(inti);
437 rc = -EFAULT;
438 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100439 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100440 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100441}
442
443static int handle_tsch(struct kvm_vcpu *vcpu)
444{
Jens Freimann6d3da242013-07-03 15:18:35 +0200445 struct kvm_s390_interrupt_info *inti = NULL;
446 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100447
Jens Freimann6d3da242013-07-03 15:18:35 +0200448 /* a valid schid has at least one bit set */
449 if (vcpu->run->s.regs.gprs[1])
450 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
451 vcpu->run->s.regs.gprs[1]);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100452
453 /*
454 * Prepare exit to userspace.
455 * We indicate whether we dequeued a pending I/O interrupt
456 * so that userspace can re-inject it if the instruction gets
457 * a program check. While this may re-order the pending I/O
458 * interrupts, this is no problem since the priority is kept
459 * intact.
460 */
461 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
462 vcpu->run->s390_tsch.dequeued = !!inti;
463 if (inti) {
464 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
465 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
466 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
467 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
468 }
469 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
470 kfree(inti);
471 return -EREMOTE;
472}
473
Cornelia Huckf379aae2012-12-20 15:32:10 +0100474static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100475{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100476 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100477
Thomas Huth5087dfa2013-06-20 17:22:01 +0200478 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
479 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
480
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100481 if (vcpu->kvm->arch.css_support) {
482 /*
483 * Most I/O instructions will be handled by userspace.
484 * Exceptions are tpi and the interrupt portion of tsch.
485 */
486 if (vcpu->arch.sie_block->ipa == 0xb236)
487 return handle_tpi(vcpu);
488 if (vcpu->arch.sie_block->ipa == 0xb235)
489 return handle_tsch(vcpu);
490 /* Handle in userspace. */
491 return -EOPNOTSUPP;
492 } else {
493 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100494 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100495 * I/O instructions.
496 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200497 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100498 return 0;
499 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100500}
501
Christian Borntraeger453423d2008-03-25 18:47:29 +0100502static int handle_stfl(struct kvm_vcpu *vcpu)
503{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100504 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100505 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100506
507 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200508
509 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
510 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
511
Michael Mueller9d8d5782015-02-02 15:42:51 +0100512 /*
513 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
514 * into a u32 memory representation. They will remain bits 0-31.
515 */
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100516 fac = *vcpu->kvm->arch.model.fac_list >> 32;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100517 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100518 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100519 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100520 return rc;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200521 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100522 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100523 return 0;
524}
525
Cornelia Huck48a3e952012-12-20 15:32:09 +0100526#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
527#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100528#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100529#define PSW_ADDR_31 0x000000007fffffffUL
530
Thomas Hutha3fb5772014-04-17 09:10:40 +0200531int is_valid_psw(psw_t *psw)
532{
Heiko Carstens3736b872013-03-25 17:22:52 +0100533 if (psw->mask & PSW_MASK_UNASSIGNED)
534 return 0;
535 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
536 if (psw->addr & ~PSW_ADDR_31)
537 return 0;
538 }
539 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
540 return 0;
541 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
542 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200543 if (psw->addr & 1)
544 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100545 return 1;
546}
547
Cornelia Huck48a3e952012-12-20 15:32:09 +0100548int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
549{
Heiko Carstens3736b872013-03-25 17:22:52 +0100550 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100551 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100552 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100553 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100554 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100555
Heiko Carstens3736b872013-03-25 17:22:52 +0100556 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200557 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
558
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300559 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100560 if (addr & 7)
561 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100562
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300563 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100564 if (rc)
565 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100566 if (!(new_psw.mask & PSW32_MASK_BASE))
567 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100568 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
569 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
570 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
571 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100572 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100573 return 0;
574}
575
576static int handle_lpswe(struct kvm_vcpu *vcpu)
577{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100578 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100579 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100580 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100581 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100582
Thomas Huth5087dfa2013-06-20 17:22:01 +0200583 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
584 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
585
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300586 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100587 if (addr & 7)
588 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300589 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100590 if (rc)
591 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100592 vcpu->arch.sie_block->gpsw = new_psw;
593 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100594 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100595 return 0;
596}
597
Christian Borntraeger453423d2008-03-25 18:47:29 +0100598static int handle_stidp(struct kvm_vcpu *vcpu)
599{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200600 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100601 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100602 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100603 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100604
605 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100606
Thomas Huth5087dfa2013-06-20 17:22:01 +0200607 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
608 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
609
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300610 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100611
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100612 if (operand2 & 7)
613 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100614
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300615 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
Heiko Carstens7d777d72014-01-01 16:58:16 +0100616 if (rc)
617 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100618
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200619 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100620 return 0;
621}
622
623static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
624{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100625 int cpus = 0;
626 int n;
627
Jens Freimannff520a62014-02-24 10:11:41 +0100628 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100629
630 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200631 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100632 mem->count = 0;
633 if (mem->count < 8)
634 mem->count++;
635 for (n = mem->count - 1; n > 0 ; n--)
636 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
637
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100638 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100639 mem->vm[0].cpus_total = cpus;
640 mem->vm[0].cpus_configured = cpus;
641 mem->vm[0].cpus_standby = 0;
642 mem->vm[0].cpus_reserved = 0;
643 mem->vm[0].caf = 1000;
644 memcpy(mem->vm[0].name, "KVMguest", 8);
645 ASCEBC(mem->vm[0].name, 8);
646 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
647 ASCEBC(mem->vm[0].cpi, 16);
648}
649
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100650static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100651 u8 fc, u8 sel1, u16 sel2)
652{
653 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
654 vcpu->run->s390_stsi.addr = addr;
655 vcpu->run->s390_stsi.ar = ar;
656 vcpu->run->s390_stsi.fc = fc;
657 vcpu->run->s390_stsi.sel1 = sel1;
658 vcpu->run->s390_stsi.sel2 = sel2;
659}
660
Christian Borntraeger453423d2008-03-25 18:47:29 +0100661static int handle_stsi(struct kvm_vcpu *vcpu)
662{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100663 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
664 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
665 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100666 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100667 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100668 int rc = 0;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100669 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100670
671 vcpu->stat.instruction_stsi++;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200672 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100673
Thomas Huth5087dfa2013-06-20 17:22:01 +0200674 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
675 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
676
Thomas Huth87d41fb2013-06-20 17:22:05 +0200677 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200678 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200679 return 0;
680 }
681
682 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
683 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
684 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
685
686 if (fc == 0) {
687 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200688 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200689 return 0;
690 }
691
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300692 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100693
Thomas Huth87d41fb2013-06-20 17:22:05 +0200694 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100695 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
696
697 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100698 case 1: /* same handling for 1 and 2 */
699 case 2:
700 mem = get_zeroed_page(GFP_KERNEL);
701 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100702 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200703 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100704 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100705 break;
706 case 3:
707 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100708 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100709 mem = get_zeroed_page(GFP_KERNEL);
710 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100711 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100712 handle_stsi_3_2_2(vcpu, (void *) mem);
713 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100714 }
715
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300716 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100717 if (rc) {
718 rc = kvm_s390_inject_prog_cond(vcpu, rc);
719 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100720 }
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100721 if (vcpu->kvm->arch.user_stsi) {
722 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
723 rc = -EREMOTE;
724 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200725 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100726 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200727 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100728 vcpu->run->s.regs.gprs[0] = 0;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100729 return rc;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100730out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200731 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100732out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100733 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100734 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100735}
736
Cornelia Huckf379aae2012-12-20 15:32:10 +0100737static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100738 [0x02] = handle_stidp,
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200739 [0x04] = handle_set_clock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100740 [0x10] = handle_set_prefix,
741 [0x11] = handle_store_prefix,
742 [0x12] = handle_store_cpu_address,
David Hildenbranda3508fb2015-07-08 13:19:48 +0200743 [0x14] = kvm_s390_handle_vsie,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100744 [0x21] = handle_ipte_interlock,
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200745 [0x29] = handle_iske,
746 [0x2a] = handle_rrbe,
747 [0x2b] = handle_sske,
Thomas Huthaca84242013-09-12 10:33:48 +0200748 [0x2c] = handle_test_block,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100749 [0x30] = handle_io_inst,
750 [0x31] = handle_io_inst,
751 [0x32] = handle_io_inst,
752 [0x33] = handle_io_inst,
753 [0x34] = handle_io_inst,
754 [0x35] = handle_io_inst,
755 [0x36] = handle_io_inst,
756 [0x37] = handle_io_inst,
757 [0x38] = handle_io_inst,
758 [0x39] = handle_io_inst,
759 [0x3a] = handle_io_inst,
760 [0x3b] = handle_io_inst,
761 [0x3c] = handle_io_inst,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100762 [0x50] = handle_ipte_interlock,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100763 [0x5f] = handle_io_inst,
764 [0x74] = handle_io_inst,
765 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100766 [0x7d] = handle_stsi,
767 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100768 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100769};
770
Christian Borntraeger70455a32009-01-22 10:28:29 +0100771int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100772{
773 intercept_handler_t handler;
774
Christian Borntraeger70455a32009-01-22 10:28:29 +0100775 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200776 * A lot of B2 instructions are priviledged. Here we check for
777 * the privileged ones, that we can handle in the kernel.
778 * Anything else goes to userspace.
779 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100780 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200781 if (handler)
782 return handler(vcpu);
783
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100784 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100785}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200786
Cornelia Huck48a3e952012-12-20 15:32:09 +0100787static int handle_epsw(struct kvm_vcpu *vcpu)
788{
789 int reg1, reg2;
790
Thomas Huthaeb87c32013-06-12 13:54:57 +0200791 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100792
793 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200794 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100795 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
796 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200797 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100798 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200799 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100800 }
801 return 0;
802}
803
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200804#define PFMF_RESERVED 0xfffc0101UL
805#define PFMF_SK 0x00020000UL
806#define PFMF_CF 0x00010000UL
807#define PFMF_UI 0x00008000UL
808#define PFMF_FSC 0x00007000UL
809#define PFMF_NQ 0x00000800UL
810#define PFMF_MR 0x00000400UL
811#define PFMF_MC 0x00000200UL
812#define PFMF_KEY 0x000000feUL
813
814static int handle_pfmf(struct kvm_vcpu *vcpu)
815{
David Hildenbrand1824c722016-05-10 09:43:11 +0200816 bool mr = false, mc = false, nq;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200817 int reg1, reg2;
818 unsigned long start, end;
David Hildenbrand1824c722016-05-10 09:43:11 +0200819 unsigned char key;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200820
821 vcpu->stat.instruction_pfmf++;
822
823 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
824
Heiko Carstens03c02802015-11-13 13:31:58 +0100825 if (!test_kvm_facility(vcpu->kvm, 8))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200826 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
827
828 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200829 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200830
831 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
832 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
833
David Hildenbrandedc5b052016-03-04 11:08:09 +0100834 /* Only provide non-quiescing support if enabled for the guest */
835 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
836 !test_kvm_facility(vcpu->kvm, 14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200837 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
838
David Hildenbrand1824c722016-05-10 09:43:11 +0200839 /* Only provide conditional-SSKE support if enabled for the guest */
840 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
841 test_kvm_facility(vcpu->kvm, 10)) {
842 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
843 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
844 }
845
846 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
847 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200848 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100849 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200850
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200851 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
852 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
853 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
854 }
855
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200856 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
857 case 0x00000000:
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200858 /* only 4k frames specify a real address */
859 start = kvm_s390_real_to_abs(vcpu, start);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200860 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
861 break;
862 case 0x00001000:
863 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
864 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200865 case 0x00002000:
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100866 /* only support 2G frame size if EDAT2 is available and we are
867 not in 24-bit addressing mode */
868 if (!test_kvm_facility(vcpu->kvm, 78) ||
869 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
870 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200871 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100872 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200873 default:
874 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
875 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100876
David Hildenbrand695be0e2016-05-12 14:07:05 +0200877 while (start != end) {
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200878 unsigned long useraddr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200879
Thomas Huthfb34c6032013-09-09 17:58:38 +0200880 /* Translate guest address to host address */
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200881 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
Thomas Huthfb34c6032013-09-09 17:58:38 +0200882 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200883 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
884
885 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
886 if (clear_user((void __user *)useraddr, PAGE_SIZE))
887 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
888 }
889
890 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200891 int rc = __skey_check_enable(vcpu);
892
893 if (rc)
894 return rc;
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100895 down_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200896 rc = cond_set_guest_storage_key(current->mm, useraddr,
897 key, NULL, nq, mr, mc);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100898 up_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200899 if (rc < 0)
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200900 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
901 }
902
903 start += PAGE_SIZE;
904 }
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200905 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
906 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
907 vcpu->run->s.regs.gprs[reg2] = end;
908 } else {
909 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
910 end = kvm_s390_logical_to_effective(vcpu, end);
911 vcpu->run->s.regs.gprs[reg2] |= end;
912 }
913 }
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200914 return 0;
915}
916
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200917static int handle_essa(struct kvm_vcpu *vcpu)
918{
919 /* entries expected to be 1FF */
920 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200921 unsigned long *cbrlo;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200922 struct gmap *gmap;
923 int i;
924
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200925 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200926 gmap = vcpu->arch.gmap;
927 vcpu->stat.instruction_essa++;
Dominik Dingele6db1d62015-05-07 15:41:57 +0200928 if (!vcpu->kvm->arch.use_cmma)
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200929 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
930
931 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
932 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
933
934 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
935 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
936
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100937 /* Retry the ESSA instruction */
938 kvm_s390_retry_instr(vcpu);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200939 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
940 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
941 down_read(&gmap->mm->mmap_sem);
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200942 for (i = 0; i < entries; ++i)
943 __gmap_zap(gmap, cbrlo[i]);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200944 up_read(&gmap->mm->mmap_sem);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200945 return 0;
946}
947
Cornelia Huck48a3e952012-12-20 15:32:09 +0100948static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +0100949 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100950 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100951 [0x8e] = handle_ipte_interlock,
952 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200953 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200954 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100955};
956
957int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
958{
959 intercept_handler_t handler;
960
961 /* This is handled just as for the B2 instructions. */
962 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200963 if (handler)
964 return handler(vcpu);
965
Cornelia Huck48a3e952012-12-20 15:32:09 +0100966 return -EOPNOTSUPP;
967}
968
Thomas Huth953ed882013-06-20 17:22:04 +0200969int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
970{
971 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
972 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100973 int reg, rc, nr_regs;
974 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100975 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100976 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +0200977
978 vcpu->stat.instruction_lctl++;
979
980 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
981 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
982
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300983 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +0200984
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100985 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +0200986 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
987
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200988 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100989 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +0200990
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100991 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300992 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100993 if (rc)
994 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +0200995 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100996 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +0200997 do {
Thomas Huth953ed882013-06-20 17:22:04 +0200998 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100999 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001000 if (reg == reg3)
1001 break;
1002 reg = (reg + 1) % 16;
1003 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001004 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001005 return 0;
1006}
1007
David Hildenbrandaba07502014-01-23 10:47:13 +01001008int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1009{
1010 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1011 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001012 int reg, rc, nr_regs;
1013 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +01001014 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001015 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001016
1017 vcpu->stat.instruction_stctl++;
1018
1019 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1020 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1021
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001022 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001023
1024 if (ga & 3)
1025 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1026
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001027 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001028 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1029
1030 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001031 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001032 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001033 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001034 if (reg == reg3)
1035 break;
1036 reg = (reg + 1) % 16;
1037 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001038 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001039 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001040}
1041
Thomas Huth953ed882013-06-20 17:22:04 +02001042static int handle_lctlg(struct kvm_vcpu *vcpu)
1043{
1044 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1045 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001046 int reg, rc, nr_regs;
1047 u64 ctl_array[16];
1048 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001049 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001050
1051 vcpu->stat.instruction_lctlg++;
1052
1053 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1054 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1055
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001056 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001057
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001058 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +02001059 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1060
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001061 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001062 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001063
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001064 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001065 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001066 if (rc)
1067 return kvm_s390_inject_prog_cond(vcpu, rc);
1068 reg = reg1;
1069 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001070 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001071 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001072 if (reg == reg3)
1073 break;
1074 reg = (reg + 1) % 16;
1075 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001076 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001077 return 0;
1078}
1079
David Hildenbrandaba07502014-01-23 10:47:13 +01001080static int handle_stctg(struct kvm_vcpu *vcpu)
1081{
1082 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1083 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001084 int reg, rc, nr_regs;
1085 u64 ctl_array[16];
1086 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001087 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001088
1089 vcpu->stat.instruction_stctg++;
1090
1091 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1092 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1093
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001094 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001095
1096 if (ga & 7)
1097 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1098
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001099 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001100 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1101
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001102 reg = reg1;
1103 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001104 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001105 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001106 if (reg == reg3)
1107 break;
1108 reg = (reg + 1) % 16;
1109 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001110 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001111 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001112}
1113
Cornelia Huckf379aae2012-12-20 15:32:10 +01001114static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +02001115 [0x2f] = handle_lctlg,
David Hildenbrandaba07502014-01-23 10:47:13 +01001116 [0x25] = handle_stctg,
Fan Zhang80cd8762016-08-15 04:53:22 +02001117 [0x60] = handle_ri,
1118 [0x61] = handle_ri,
1119 [0x62] = handle_ri,
Cornelia Huckf379aae2012-12-20 15:32:10 +01001120};
1121
Thomas Huth953ed882013-06-20 17:22:04 +02001122int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +01001123{
1124 intercept_handler_t handler;
1125
Cornelia Huckf379aae2012-12-20 15:32:10 +01001126 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1127 if (handler)
1128 return handler(vcpu);
1129 return -EOPNOTSUPP;
1130}
1131
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001132static int handle_tprot(struct kvm_vcpu *vcpu)
1133{
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001134 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +01001135 unsigned long hva, gpa;
1136 int ret = 0, cc = 0;
1137 bool writable;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001138 u8 ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001139
1140 vcpu->stat.instruction_tprot++;
1141
Thomas Huthf9f6bbc2013-06-20 17:22:00 +02001142 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1143 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1144
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001145 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001146
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001147 /* we only handle the Linux memory detection case:
1148 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001149 * everything else goes to userspace. */
1150 if (address2 & 0xf0)
1151 return -EOPNOTSUPP;
1152 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +01001153 ipte_lock(vcpu);
David Hildenbrand92c96322015-11-16 15:42:11 +01001154 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
Thomas Hutha0465f92014-02-04 14:48:07 +01001155 if (ret == PGM_PROTECTION) {
1156 /* Write protected? Try again with read-only... */
1157 cc = 1;
David Hildenbrand92c96322015-11-16 15:42:11 +01001158 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1159 GACC_FETCH);
Thomas Hutha0465f92014-02-04 14:48:07 +01001160 }
1161 if (ret) {
1162 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1163 ret = kvm_s390_inject_program_int(vcpu, ret);
1164 } else if (ret > 0) {
1165 /* Translation not available */
1166 kvm_s390_set_psw_cc(vcpu, 3);
1167 ret = 0;
1168 }
1169 goto out_unlock;
1170 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001171
Thomas Hutha0465f92014-02-04 14:48:07 +01001172 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1173 if (kvm_is_error_hva(hva)) {
1174 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1175 } else {
1176 if (!writable)
1177 cc = 1; /* Write not permitted ==> read-only */
1178 kvm_s390_set_psw_cc(vcpu, cc);
1179 /* Note: CC2 only occurs for storage keys (not supported yet) */
1180 }
1181out_unlock:
1182 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1183 ipte_unlock(vcpu);
1184 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001185}
1186
1187int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1188{
1189 /* For e5xx... instructions we only handle TPROT */
1190 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1191 return handle_tprot(vcpu);
1192 return -EOPNOTSUPP;
1193}
1194
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001195static int handle_sckpf(struct kvm_vcpu *vcpu)
1196{
1197 u32 value;
1198
1199 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001200 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001201
1202 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1203 return kvm_s390_inject_program_int(vcpu,
1204 PGM_SPECIFICATION);
1205
1206 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1207 vcpu->arch.sie_block->todpr = value;
1208
1209 return 0;
1210}
1211
David Hildenbrand9acc3172016-07-18 09:18:13 +02001212static int handle_ptff(struct kvm_vcpu *vcpu)
1213{
1214 /* we don't emulate any control instructions yet */
1215 kvm_s390_set_psw_cc(vcpu, 3);
1216 return 0;
1217}
1218
Cornelia Huck77975352012-12-20 15:32:06 +01001219static const intercept_handler_t x01_handlers[256] = {
David Hildenbrand9acc3172016-07-18 09:18:13 +02001220 [0x04] = handle_ptff,
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001221 [0x07] = handle_sckpf,
1222};
1223
1224int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1225{
1226 intercept_handler_t handler;
1227
1228 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1229 if (handler)
1230 return handler(vcpu);
1231 return -EOPNOTSUPP;
1232}