blob: 26f30bab9c2f4ce3cb2cd085b80efad8ac2bf5ed [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,
Christian Borntraegerc0a6bfd2017-02-27 21:14:47 +0100763 [0x56] = handle_sthyi,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100764 [0x5f] = handle_io_inst,
765 [0x74] = handle_io_inst,
766 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100767 [0x7d] = handle_stsi,
768 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100769 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100770};
771
Christian Borntraeger70455a32009-01-22 10:28:29 +0100772int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100773{
774 intercept_handler_t handler;
775
Christian Borntraeger70455a32009-01-22 10:28:29 +0100776 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200777 * A lot of B2 instructions are priviledged. Here we check for
778 * the privileged ones, that we can handle in the kernel.
779 * Anything else goes to userspace.
780 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100781 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200782 if (handler)
783 return handler(vcpu);
784
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100785 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100786}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200787
Cornelia Huck48a3e952012-12-20 15:32:09 +0100788static int handle_epsw(struct kvm_vcpu *vcpu)
789{
790 int reg1, reg2;
791
Thomas Huthaeb87c32013-06-12 13:54:57 +0200792 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100793
794 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200795 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100796 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
797 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200798 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100799 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200800 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100801 }
802 return 0;
803}
804
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200805#define PFMF_RESERVED 0xfffc0101UL
806#define PFMF_SK 0x00020000UL
807#define PFMF_CF 0x00010000UL
808#define PFMF_UI 0x00008000UL
809#define PFMF_FSC 0x00007000UL
810#define PFMF_NQ 0x00000800UL
811#define PFMF_MR 0x00000400UL
812#define PFMF_MC 0x00000200UL
813#define PFMF_KEY 0x000000feUL
814
815static int handle_pfmf(struct kvm_vcpu *vcpu)
816{
David Hildenbrand1824c722016-05-10 09:43:11 +0200817 bool mr = false, mc = false, nq;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200818 int reg1, reg2;
819 unsigned long start, end;
David Hildenbrand1824c722016-05-10 09:43:11 +0200820 unsigned char key;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200821
822 vcpu->stat.instruction_pfmf++;
823
824 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
825
Heiko Carstens03c02802015-11-13 13:31:58 +0100826 if (!test_kvm_facility(vcpu->kvm, 8))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200827 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
828
829 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200830 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200831
832 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
833 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
834
David Hildenbrandedc5b052016-03-04 11:08:09 +0100835 /* Only provide non-quiescing support if enabled for the guest */
836 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
837 !test_kvm_facility(vcpu->kvm, 14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200838 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
839
David Hildenbrand1824c722016-05-10 09:43:11 +0200840 /* Only provide conditional-SSKE support if enabled for the guest */
841 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
842 test_kvm_facility(vcpu->kvm, 10)) {
843 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
844 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
845 }
846
847 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
848 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200849 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100850 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200851
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200852 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
853 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
854 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
855 }
856
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200857 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
858 case 0x00000000:
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200859 /* only 4k frames specify a real address */
860 start = kvm_s390_real_to_abs(vcpu, start);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200861 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
862 break;
863 case 0x00001000:
864 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
865 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200866 case 0x00002000:
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100867 /* only support 2G frame size if EDAT2 is available and we are
868 not in 24-bit addressing mode */
869 if (!test_kvm_facility(vcpu->kvm, 78) ||
870 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
871 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200872 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100873 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200874 default:
875 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
876 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100877
David Hildenbrand695be0e2016-05-12 14:07:05 +0200878 while (start != end) {
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200879 unsigned long useraddr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200880
Thomas Huthfb34c6032013-09-09 17:58:38 +0200881 /* Translate guest address to host address */
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200882 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
Thomas Huthfb34c6032013-09-09 17:58:38 +0200883 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200884 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
885
886 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
887 if (clear_user((void __user *)useraddr, PAGE_SIZE))
888 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
889 }
890
891 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200892 int rc = __skey_check_enable(vcpu);
893
894 if (rc)
895 return rc;
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100896 down_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200897 rc = cond_set_guest_storage_key(current->mm, useraddr,
898 key, NULL, nq, mr, mc);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100899 up_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200900 if (rc < 0)
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200901 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
902 }
903
904 start += PAGE_SIZE;
905 }
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200906 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
907 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
908 vcpu->run->s.regs.gprs[reg2] = end;
909 } else {
910 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
911 end = kvm_s390_logical_to_effective(vcpu, end);
912 vcpu->run->s.regs.gprs[reg2] |= end;
913 }
914 }
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200915 return 0;
916}
917
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200918static int handle_essa(struct kvm_vcpu *vcpu)
919{
920 /* entries expected to be 1FF */
921 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200922 unsigned long *cbrlo;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200923 struct gmap *gmap;
924 int i;
925
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200926 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200927 gmap = vcpu->arch.gmap;
928 vcpu->stat.instruction_essa++;
Dominik Dingele6db1d62015-05-07 15:41:57 +0200929 if (!vcpu->kvm->arch.use_cmma)
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200930 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
931
932 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
933 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
934
935 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
936 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
937
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100938 /* Retry the ESSA instruction */
939 kvm_s390_retry_instr(vcpu);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200940 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
941 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
942 down_read(&gmap->mm->mmap_sem);
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200943 for (i = 0; i < entries; ++i)
944 __gmap_zap(gmap, cbrlo[i]);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200945 up_read(&gmap->mm->mmap_sem);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200946 return 0;
947}
948
Cornelia Huck48a3e952012-12-20 15:32:09 +0100949static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +0100950 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100951 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100952 [0x8e] = handle_ipte_interlock,
953 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200954 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200955 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100956};
957
958int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
959{
960 intercept_handler_t handler;
961
962 /* This is handled just as for the B2 instructions. */
963 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200964 if (handler)
965 return handler(vcpu);
966
Cornelia Huck48a3e952012-12-20 15:32:09 +0100967 return -EOPNOTSUPP;
968}
969
Thomas Huth953ed882013-06-20 17:22:04 +0200970int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
971{
972 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
973 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100974 int reg, rc, nr_regs;
975 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100976 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100977 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +0200978
979 vcpu->stat.instruction_lctl++;
980
981 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
982 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
983
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300984 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +0200985
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100986 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +0200987 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
988
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200989 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100990 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +0200991
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100992 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300993 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100994 if (rc)
995 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +0200996 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100997 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +0200998 do {
Thomas Huth953ed882013-06-20 17:22:04 +0200999 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001000 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001001 if (reg == reg3)
1002 break;
1003 reg = (reg + 1) % 16;
1004 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001005 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001006 return 0;
1007}
1008
David Hildenbrandaba07502014-01-23 10:47:13 +01001009int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1010{
1011 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1012 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001013 int reg, rc, nr_regs;
1014 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +01001015 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001016 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001017
1018 vcpu->stat.instruction_stctl++;
1019
1020 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1021 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1022
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001023 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001024
1025 if (ga & 3)
1026 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1027
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001028 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001029 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1030
1031 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001032 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001033 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001034 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001035 if (reg == reg3)
1036 break;
1037 reg = (reg + 1) % 16;
1038 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001039 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001040 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001041}
1042
Thomas Huth953ed882013-06-20 17:22:04 +02001043static int handle_lctlg(struct kvm_vcpu *vcpu)
1044{
1045 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1046 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001047 int reg, rc, nr_regs;
1048 u64 ctl_array[16];
1049 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001050 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001051
1052 vcpu->stat.instruction_lctlg++;
1053
1054 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1055 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1056
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001057 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001058
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001059 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +02001060 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1061
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001062 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001063 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001064
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001065 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001066 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001067 if (rc)
1068 return kvm_s390_inject_prog_cond(vcpu, rc);
1069 reg = reg1;
1070 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001071 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001072 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001073 if (reg == reg3)
1074 break;
1075 reg = (reg + 1) % 16;
1076 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001077 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001078 return 0;
1079}
1080
David Hildenbrandaba07502014-01-23 10:47:13 +01001081static int handle_stctg(struct kvm_vcpu *vcpu)
1082{
1083 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1084 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001085 int reg, rc, nr_regs;
1086 u64 ctl_array[16];
1087 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001088 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001089
1090 vcpu->stat.instruction_stctg++;
1091
1092 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1093 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1094
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001095 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001096
1097 if (ga & 7)
1098 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1099
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001100 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001101 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1102
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001103 reg = reg1;
1104 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001105 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001106 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001107 if (reg == reg3)
1108 break;
1109 reg = (reg + 1) % 16;
1110 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001111 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001112 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001113}
1114
Cornelia Huckf379aae2012-12-20 15:32:10 +01001115static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +02001116 [0x2f] = handle_lctlg,
David Hildenbrandaba07502014-01-23 10:47:13 +01001117 [0x25] = handle_stctg,
Fan Zhang80cd8762016-08-15 04:53:22 +02001118 [0x60] = handle_ri,
1119 [0x61] = handle_ri,
1120 [0x62] = handle_ri,
Cornelia Huckf379aae2012-12-20 15:32:10 +01001121};
1122
Thomas Huth953ed882013-06-20 17:22:04 +02001123int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +01001124{
1125 intercept_handler_t handler;
1126
Cornelia Huckf379aae2012-12-20 15:32:10 +01001127 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1128 if (handler)
1129 return handler(vcpu);
1130 return -EOPNOTSUPP;
1131}
1132
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001133static int handle_tprot(struct kvm_vcpu *vcpu)
1134{
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001135 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +01001136 unsigned long hva, gpa;
1137 int ret = 0, cc = 0;
1138 bool writable;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001139 u8 ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001140
1141 vcpu->stat.instruction_tprot++;
1142
Thomas Huthf9f6bbc2013-06-20 17:22:00 +02001143 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1144 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1145
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001146 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001147
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001148 /* we only handle the Linux memory detection case:
1149 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001150 * everything else goes to userspace. */
1151 if (address2 & 0xf0)
1152 return -EOPNOTSUPP;
1153 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +01001154 ipte_lock(vcpu);
David Hildenbrand92c96322015-11-16 15:42:11 +01001155 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
Thomas Hutha0465f92014-02-04 14:48:07 +01001156 if (ret == PGM_PROTECTION) {
1157 /* Write protected? Try again with read-only... */
1158 cc = 1;
David Hildenbrand92c96322015-11-16 15:42:11 +01001159 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1160 GACC_FETCH);
Thomas Hutha0465f92014-02-04 14:48:07 +01001161 }
1162 if (ret) {
1163 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1164 ret = kvm_s390_inject_program_int(vcpu, ret);
1165 } else if (ret > 0) {
1166 /* Translation not available */
1167 kvm_s390_set_psw_cc(vcpu, 3);
1168 ret = 0;
1169 }
1170 goto out_unlock;
1171 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001172
Thomas Hutha0465f92014-02-04 14:48:07 +01001173 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1174 if (kvm_is_error_hva(hva)) {
1175 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1176 } else {
1177 if (!writable)
1178 cc = 1; /* Write not permitted ==> read-only */
1179 kvm_s390_set_psw_cc(vcpu, cc);
1180 /* Note: CC2 only occurs for storage keys (not supported yet) */
1181 }
1182out_unlock:
1183 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1184 ipte_unlock(vcpu);
1185 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001186}
1187
1188int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1189{
1190 /* For e5xx... instructions we only handle TPROT */
1191 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1192 return handle_tprot(vcpu);
1193 return -EOPNOTSUPP;
1194}
1195
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001196static int handle_sckpf(struct kvm_vcpu *vcpu)
1197{
1198 u32 value;
1199
1200 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001201 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001202
1203 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1204 return kvm_s390_inject_program_int(vcpu,
1205 PGM_SPECIFICATION);
1206
1207 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1208 vcpu->arch.sie_block->todpr = value;
1209
1210 return 0;
1211}
1212
David Hildenbrand9acc3172016-07-18 09:18:13 +02001213static int handle_ptff(struct kvm_vcpu *vcpu)
1214{
1215 /* we don't emulate any control instructions yet */
1216 kvm_s390_set_psw_cc(vcpu, 3);
1217 return 0;
1218}
1219
Cornelia Huck77975352012-12-20 15:32:06 +01001220static const intercept_handler_t x01_handlers[256] = {
David Hildenbrand9acc3172016-07-18 09:18:13 +02001221 [0x04] = handle_ptff,
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001222 [0x07] = handle_sckpf,
1223};
1224
1225int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1226{
1227 intercept_handler_t handler;
1228
1229 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1230 if (handler)
1231 return handler(vcpu);
1232 return -EOPNOTSUPP;
1233}