blob: a74578cdd3f3a8ddd7be14ba9b7e7a234b2f7c91 [file] [log] [blame]
Greg Kroah-Hartmand809aa22017-11-24 15:00:33 +01001// SPDX-License-Identifier: GPL-2.0
Christian Borntraeger453423d2008-03-25 18:47:29 +01002/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * handling privileged instructions
Christian Borntraeger453423d2008-03-25 18:47:29 +01004 *
Christian Borntraegera37cb072018-01-23 13:28:40 +01005 * Copyright IBM Corp. 2008, 2018
Christian Borntraeger453423d2008-03-25 18:47:29 +01006 *
Christian Borntraeger453423d2008-03-25 18:47:29 +01007 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
9 */
10
11#include <linux/kvm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010013#include <linux/errno.h>
Heiko Carstensb13b5dc2013-03-25 17:22:55 +010014#include <linux/compat.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010015#include <linux/mm_types.h>
16
Heiko Carstens7c959e82013-03-05 13:14:46 +010017#include <asm/asm-offsets.h>
Heiko Carstense769ece2013-07-26 15:04:01 +020018#include <asm/facility.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010019#include <asm/current.h>
20#include <asm/debug.h>
21#include <asm/ebcdic.h>
22#include <asm/sysinfo.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020023#include <asm/pgtable.h>
Claudio Imbrenda190df4a2016-08-04 17:54:42 +020024#include <asm/page-states.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020025#include <asm/pgalloc.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010026#include <asm/gmap.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020027#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010028#include <asm/ptrace.h>
29#include <asm/compat.h>
David Hildenbranda7e19ab2016-05-10 09:50:21 +020030#include <asm/sclp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010031#include "gaccess.h"
32#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020033#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010034
Fan Zhang80cd8762016-08-15 04:53:22 +020035static int handle_ri(struct kvm_vcpu *vcpu)
36{
Christian Borntraegera37cb072018-01-23 13:28:40 +010037 vcpu->stat.instruction_ri++;
38
Fan Zhang80cd8762016-08-15 04:53:22 +020039 if (test_kvm_facility(vcpu->kvm, 64)) {
Christian Borntraeger4d5f2c02017-02-09 17:15:41 +010040 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
David Hildenbrand0c9d8682017-03-13 11:48:28 +010041 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
Fan Zhang80cd8762016-08-15 04:53:22 +020042 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
Fan Zhang4e0b1ab2016-11-29 07:17:55 +010056static int handle_gs(struct kvm_vcpu *vcpu)
57{
Christian Borntraegera37cb072018-01-23 13:28:40 +010058 vcpu->stat.instruction_gs++;
59
Fan Zhang4e0b1ab2016-11-29 07:17:55 +010060 if (test_kvm_facility(vcpu->kvm, 133)) {
61 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
62 preempt_disable();
63 __ctl_set_bit(2, 4);
64 current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
65 restore_gs_cb(current->thread.gs_cb);
66 preempt_enable();
67 vcpu->arch.sie_block->ecb |= ECB_GS;
68 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
69 vcpu->arch.gs_enabled = 1;
70 kvm_s390_retry_instr(vcpu);
71 return 0;
72 } else
73 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
74}
75
76int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
77{
78 int code = vcpu->arch.sie_block->ipb & 0xff;
79
80 if (code == 0x49 || code == 0x4d)
81 return handle_gs(vcpu);
82 else
83 return -EOPNOTSUPP;
84}
Thomas Huth6a3f95a2013-09-12 10:33:49 +020085/* Handle SCK (SET CLOCK) interception */
86static int handle_set_clock(struct kvm_vcpu *vcpu)
87{
David Hildenbrand25ed1672015-05-12 09:49:14 +020088 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +010089 u8 ar;
David Hildenbrand25ed1672015-05-12 09:49:14 +020090 u64 op2, val;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020091
Christian Borntraegera37cb072018-01-23 13:28:40 +010092 vcpu->stat.instruction_sck++;
93
Thomas Huth6a3f95a2013-09-12 10:33:49 +020094 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
95 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
96
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030097 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020098 if (op2 & 7) /* Operand must be on a doubleword boundary */
99 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300100 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
Heiko Carstens0e7a3f92014-01-01 16:50:11 +0100101 if (rc)
102 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200103
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200104 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
David Hildenbrand25ed1672015-05-12 09:49:14 +0200105 kvm_s390_set_tod_clock(vcpu->kvm, val);
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200106
107 kvm_s390_set_psw_cc(vcpu, 0);
108 return 0;
109}
110
Christian Borntraeger453423d2008-03-25 18:47:29 +0100111static int handle_set_prefix(struct kvm_vcpu *vcpu)
112{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100113 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +0100114 u32 address;
115 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100116 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100117
118 vcpu->stat.instruction_spx++;
119
Thomas Huth5087dfa2013-06-20 17:22:01 +0200120 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
121 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
122
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300123 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100124
125 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100126 if (operand2 & 3)
127 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100128
129 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300130 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstens665170c2014-01-01 16:47:12 +0100131 if (rc)
132 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100133
Heiko Carstens665170c2014-01-01 16:47:12 +0100134 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100135
Heiko Carstens665170c2014-01-01 16:47:12 +0100136 /*
137 * Make sure the new value is valid memory. We only need to check the
138 * first page, since address is 8k aligned and memory pieces are always
139 * at least 1MB aligned and have at least a size of 1MB.
140 */
141 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100142 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100143
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100144 kvm_s390_set_prefix(vcpu, address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200145 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100146 return 0;
147}
148
149static int handle_store_prefix(struct kvm_vcpu *vcpu)
150{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100151 u64 operand2;
152 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100153 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100154 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100155
156 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100157
Thomas Huth5087dfa2013-06-20 17:22:01 +0200158 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
159 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
160
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300161 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100162
163 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100164 if (operand2 & 3)
165 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100166
Michael Muellerfda902c2014-05-13 16:58:30 +0200167 address = kvm_s390_get_prefix(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100168
169 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300170 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100171 if (rc)
172 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100173
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200174 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200175 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100176 return 0;
177}
178
179static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
180{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100181 u16 vcpu_id = vcpu->vcpu_id;
182 u64 ga;
183 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100184 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100185
186 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100187
Thomas Huth5087dfa2013-06-20 17:22:01 +0200188 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
189 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
190
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300191 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100192
Heiko Carstens8b96de02014-01-01 16:53:27 +0100193 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100194 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100195
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300196 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
Heiko Carstens8b96de02014-01-01 16:53:27 +0100197 if (rc)
198 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100199
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200200 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
Heiko Carstens8b96de02014-01-01 16:53:27 +0100201 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100202 return 0;
203}
204
Farhan Ali730cd632017-02-24 16:12:56 -0500205int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu)
Dominik Dingel693ffc02014-01-14 18:11:14 +0100206{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200207 int rc = 0;
Farhan Ali730cd632017-02-24 16:12:56 -0500208 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200209
210 trace_kvm_s390_skey_related_inst(vcpu);
Farhan Ali730cd632017-02-24 16:12:56 -0500211 if (!(sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)) &&
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +0100212 !kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200213 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100214
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200215 rc = s390_enable_skey();
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200216 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
Farhan Ali730cd632017-02-24 16:12:56 -0500217 if (!rc) {
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +0100218 if (kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
David Hildenbrand9daecfc2018-01-23 18:05:30 +0100219 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_KSS);
Farhan Ali730cd632017-02-24 16:12:56 -0500220 else
221 sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE |
222 ICTL_RRBE);
223 }
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200224 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100225}
226
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200227static int try_handle_skey(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100228{
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200229 int rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100230
Farhan Ali730cd632017-02-24 16:12:56 -0500231 rc = kvm_s390_skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200232 if (rc)
233 return rc;
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200234 if (sclp.has_skey) {
235 /* with storage-key facility, SIE interprets it for us */
236 kvm_s390_retry_instr(vcpu);
237 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
238 return -EAGAIN;
239 }
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200240 return 0;
241}
Thomas Huth5087dfa2013-06-20 17:22:01 +0200242
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200243static int handle_iske(struct kvm_vcpu *vcpu)
244{
245 unsigned long addr;
246 unsigned char key;
247 int reg1, reg2;
248 int rc;
249
Christian Borntraegera37cb072018-01-23 13:28:40 +0100250 vcpu->stat.instruction_iske++;
251
Janosch Frankca76ec92017-12-04 12:19:11 +0100252 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
253 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
254
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200255 rc = try_handle_skey(vcpu);
256 if (rc)
257 return rc != -EAGAIN ? rc : 0;
258
259 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
260
261 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
262 addr = kvm_s390_logical_to_effective(vcpu, addr);
263 addr = kvm_s390_real_to_abs(vcpu, addr);
264 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
265 if (kvm_is_error_hva(addr))
266 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
267
268 down_read(&current->mm->mmap_sem);
269 rc = get_guest_storage_key(current->mm, addr, &key);
270 up_read(&current->mm->mmap_sem);
271 if (rc)
272 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
273 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
274 vcpu->run->s.regs.gprs[reg1] |= key;
275 return 0;
276}
277
278static int handle_rrbe(struct kvm_vcpu *vcpu)
279{
280 unsigned long addr;
281 int reg1, reg2;
282 int rc;
283
Christian Borntraegera37cb072018-01-23 13:28:40 +0100284 vcpu->stat.instruction_rrbe++;
285
Janosch Frankca76ec92017-12-04 12:19:11 +0100286 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
287 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
288
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200289 rc = try_handle_skey(vcpu);
290 if (rc)
291 return rc != -EAGAIN ? rc : 0;
292
293 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
294
295 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
296 addr = kvm_s390_logical_to_effective(vcpu, addr);
297 addr = kvm_s390_real_to_abs(vcpu, addr);
298 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
299 if (kvm_is_error_hva(addr))
300 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
301
302 down_read(&current->mm->mmap_sem);
303 rc = reset_guest_reference_bit(current->mm, addr);
304 up_read(&current->mm->mmap_sem);
305 if (rc < 0)
306 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
307
308 kvm_s390_set_psw_cc(vcpu, rc);
309 return 0;
310}
311
312#define SSKE_NQ 0x8
313#define SSKE_MR 0x4
314#define SSKE_MC 0x2
315#define SSKE_MB 0x1
316static int handle_sske(struct kvm_vcpu *vcpu)
317{
318 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
319 unsigned long start, end;
320 unsigned char key, oldkey;
321 int reg1, reg2;
322 int rc;
323
Christian Borntraegera37cb072018-01-23 13:28:40 +0100324 vcpu->stat.instruction_sske++;
325
Janosch Frankca76ec92017-12-04 12:19:11 +0100326 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
327 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
328
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200329 rc = try_handle_skey(vcpu);
330 if (rc)
331 return rc != -EAGAIN ? rc : 0;
332
333 if (!test_kvm_facility(vcpu->kvm, 8))
334 m3 &= ~SSKE_MB;
335 if (!test_kvm_facility(vcpu->kvm, 10))
336 m3 &= ~(SSKE_MC | SSKE_MR);
337 if (!test_kvm_facility(vcpu->kvm, 14))
338 m3 &= ~SSKE_NQ;
339
340 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
341
342 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
343 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
344 start = kvm_s390_logical_to_effective(vcpu, start);
345 if (m3 & SSKE_MB) {
346 /* start already designates an absolute address */
Heiko Carstens58cdf5e2017-07-05 07:37:14 +0200347 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200348 } else {
349 start = kvm_s390_real_to_abs(vcpu, start);
350 end = start + PAGE_SIZE;
351 }
352
353 while (start != end) {
354 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
355
356 if (kvm_is_error_hva(addr))
357 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
358
359 down_read(&current->mm->mmap_sem);
360 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
361 m3 & SSKE_NQ, m3 & SSKE_MR,
362 m3 & SSKE_MC);
363 up_read(&current->mm->mmap_sem);
364 if (rc < 0)
365 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
366 start += PAGE_SIZE;
Heiko Carstens0b925152017-01-02 08:51:02 +0100367 }
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200368
369 if (m3 & (SSKE_MC | SSKE_MR)) {
370 if (m3 & SSKE_MB) {
371 /* skey in reg1 is unpredictable */
372 kvm_s390_set_psw_cc(vcpu, 3);
373 } else {
374 kvm_s390_set_psw_cc(vcpu, rc);
375 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
376 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
377 }
378 }
379 if (m3 & SSKE_MB) {
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200380 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200381 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
382 else
383 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
384 end = kvm_s390_logical_to_effective(vcpu, end);
385 vcpu->run->s.regs.gprs[reg2] |= end;
386 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100387 return 0;
388}
389
Heiko Carstens8a2422342014-01-10 14:33:28 +0100390static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
391{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100392 vcpu->stat.instruction_ipte_interlock++;
Heiko Carstensa7525982017-06-03 10:56:07 +0200393 if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100394 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
395 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100396 kvm_s390_retry_instr(vcpu);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100397 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
398 return 0;
399}
400
Thomas Huthaca84242013-09-12 10:33:48 +0200401static int handle_test_block(struct kvm_vcpu *vcpu)
402{
Thomas Huthaca84242013-09-12 10:33:48 +0200403 gpa_t addr;
404 int reg2;
405
Christian Borntraegera37cb072018-01-23 13:28:40 +0100406 vcpu->stat.instruction_tb++;
407
Thomas Huthaca84242013-09-12 10:33:48 +0200408 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
409 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
410
411 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
412 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100413 addr = kvm_s390_logical_to_effective(vcpu, addr);
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300414 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
Thomas Huthe45efa22014-03-07 12:14:23 +0100415 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200416 addr = kvm_s390_real_to_abs(vcpu, addr);
417
Heiko Carstensef23e772014-01-01 16:53:49 +0100418 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200419 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
420 /*
421 * We don't expect errors on modern systems, and do not care
422 * about storage keys (yet), so let's just clear the page.
423 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100424 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200425 return -EFAULT;
426 kvm_s390_set_psw_cc(vcpu, 0);
427 vcpu->run->s.regs.gprs[0] = 0;
428 return 0;
429}
430
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100431static int handle_tpi(struct kvm_vcpu *vcpu)
432{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100433 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100434 unsigned long len;
435 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100436 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100437 u64 addr;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100438 u8 ar;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100439
Christian Borntraegera37cb072018-01-23 13:28:40 +0100440 vcpu->stat.instruction_tpi++;
441
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300442 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100443 if (addr & 3)
444 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100445
Thomas Huthf0926692013-10-09 14:15:54 +0200446 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100447 if (!inti) {
448 kvm_s390_set_psw_cc(vcpu, 0);
449 return 0;
450 }
451
Heiko Carstens4799b552014-01-01 16:55:48 +0100452 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
453 tpi_data[1] = inti->io.io_int_parm;
454 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100455 if (addr) {
456 /*
457 * Store the two-word I/O interruption code into the
458 * provided area.
459 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100460 len = sizeof(tpi_data) - 4;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300461 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100462 if (rc) {
463 rc = kvm_s390_inject_prog_cond(vcpu, rc);
464 goto reinject_interrupt;
465 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100466 } else {
467 /*
468 * Store the three-word I/O interruption code into
469 * the appropriate lowcore area.
470 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100471 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100472 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
473 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100474 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100475 goto reinject_interrupt;
476 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100477 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100478
479 /* irq was successfully handed to the guest */
480 kfree(inti);
481 kvm_s390_set_psw_cc(vcpu, 1);
482 return 0;
483reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100484 /*
485 * If we encounter a problem storing the interruption code, the
486 * instruction is suppressed from the guest's view: reinject the
487 * interrupt.
488 */
David Hildenbrand15462e32015-02-04 15:59:11 +0100489 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
490 kfree(inti);
491 rc = -EFAULT;
492 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100493 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100494 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100495}
496
497static int handle_tsch(struct kvm_vcpu *vcpu)
498{
Jens Freimann6d3da242013-07-03 15:18:35 +0200499 struct kvm_s390_interrupt_info *inti = NULL;
500 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100501
Christian Borntraegera37cb072018-01-23 13:28:40 +0100502 vcpu->stat.instruction_tsch++;
503
Jens Freimann6d3da242013-07-03 15:18:35 +0200504 /* a valid schid has at least one bit set */
505 if (vcpu->run->s.regs.gprs[1])
506 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
507 vcpu->run->s.regs.gprs[1]);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100508
509 /*
510 * Prepare exit to userspace.
511 * We indicate whether we dequeued a pending I/O interrupt
512 * so that userspace can re-inject it if the instruction gets
513 * a program check. While this may re-order the pending I/O
514 * interrupts, this is no problem since the priority is kept
515 * intact.
516 */
517 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
518 vcpu->run->s390_tsch.dequeued = !!inti;
519 if (inti) {
520 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
521 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
522 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
523 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
524 }
525 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
526 kfree(inti);
527 return -EREMOTE;
528}
529
Cornelia Huckf379aae2012-12-20 15:32:10 +0100530static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100531{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100532 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100533
Thomas Huth5087dfa2013-06-20 17:22:01 +0200534 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
535 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
536
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100537 if (vcpu->kvm->arch.css_support) {
538 /*
539 * Most I/O instructions will be handled by userspace.
540 * Exceptions are tpi and the interrupt portion of tsch.
541 */
542 if (vcpu->arch.sie_block->ipa == 0xb236)
543 return handle_tpi(vcpu);
544 if (vcpu->arch.sie_block->ipa == 0xb235)
545 return handle_tsch(vcpu);
546 /* Handle in userspace. */
Christian Borntraegera37cb072018-01-23 13:28:40 +0100547 vcpu->stat.instruction_io_other++;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100548 return -EOPNOTSUPP;
549 } else {
550 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100551 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100552 * I/O instructions.
553 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200554 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100555 return 0;
556 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100557}
558
Christian Borntraeger453423d2008-03-25 18:47:29 +0100559static int handle_stfl(struct kvm_vcpu *vcpu)
560{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100561 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100562 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100563
564 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200565
566 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
567 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
568
Michael Mueller9d8d5782015-02-02 15:42:51 +0100569 /*
570 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
571 * into a u32 memory representation. They will remain bits 0-31.
572 */
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100573 fac = *vcpu->kvm->arch.model.fac_list >> 32;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100574 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100575 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100576 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100577 return rc;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200578 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100579 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100580 return 0;
581}
582
Cornelia Huck48a3e952012-12-20 15:32:09 +0100583#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
584#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100585#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100586#define PSW_ADDR_31 0x000000007fffffffUL
587
Thomas Hutha3fb5772014-04-17 09:10:40 +0200588int is_valid_psw(psw_t *psw)
589{
Heiko Carstens3736b872013-03-25 17:22:52 +0100590 if (psw->mask & PSW_MASK_UNASSIGNED)
591 return 0;
592 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
593 if (psw->addr & ~PSW_ADDR_31)
594 return 0;
595 }
596 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
597 return 0;
598 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
599 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200600 if (psw->addr & 1)
601 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100602 return 1;
603}
604
Cornelia Huck48a3e952012-12-20 15:32:09 +0100605int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
606{
Heiko Carstens3736b872013-03-25 17:22:52 +0100607 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100608 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100609 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100610 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100611 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100612
Christian Borntraegera37cb072018-01-23 13:28:40 +0100613 vcpu->stat.instruction_lpsw++;
614
Heiko Carstens3736b872013-03-25 17:22:52 +0100615 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200616 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
617
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300618 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100619 if (addr & 7)
620 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100621
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300622 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100623 if (rc)
624 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100625 if (!(new_psw.mask & PSW32_MASK_BASE))
626 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100627 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
628 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
629 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
630 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100631 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100632 return 0;
633}
634
635static int handle_lpswe(struct kvm_vcpu *vcpu)
636{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100637 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100638 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100639 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100640 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100641
Christian Borntraegera37cb072018-01-23 13:28:40 +0100642 vcpu->stat.instruction_lpswe++;
643
Thomas Huth5087dfa2013-06-20 17:22:01 +0200644 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
645 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
646
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300647 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100648 if (addr & 7)
649 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300650 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100651 if (rc)
652 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100653 vcpu->arch.sie_block->gpsw = new_psw;
654 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100655 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100656 return 0;
657}
658
Christian Borntraeger453423d2008-03-25 18:47:29 +0100659static int handle_stidp(struct kvm_vcpu *vcpu)
660{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200661 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100662 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100663 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100664 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100665
666 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100667
Thomas Huth5087dfa2013-06-20 17:22:01 +0200668 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
669 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
670
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300671 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100672
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100673 if (operand2 & 7)
674 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100675
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300676 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
Heiko Carstens7d777d72014-01-01 16:58:16 +0100677 if (rc)
678 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100679
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200680 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100681 return 0;
682}
683
684static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
685{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100686 int cpus = 0;
687 int n;
688
Jens Freimannff520a62014-02-24 10:11:41 +0100689 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100690
691 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200692 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100693 mem->count = 0;
694 if (mem->count < 8)
695 mem->count++;
696 for (n = mem->count - 1; n > 0 ; n--)
697 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
698
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100699 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100700 mem->vm[0].cpus_total = cpus;
701 mem->vm[0].cpus_configured = cpus;
702 mem->vm[0].cpus_standby = 0;
703 mem->vm[0].cpus_reserved = 0;
704 mem->vm[0].caf = 1000;
705 memcpy(mem->vm[0].name, "KVMguest", 8);
706 ASCEBC(mem->vm[0].name, 8);
707 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
708 ASCEBC(mem->vm[0].cpi, 16);
709}
710
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100711static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100712 u8 fc, u8 sel1, u16 sel2)
713{
714 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
715 vcpu->run->s390_stsi.addr = addr;
716 vcpu->run->s390_stsi.ar = ar;
717 vcpu->run->s390_stsi.fc = fc;
718 vcpu->run->s390_stsi.sel1 = sel1;
719 vcpu->run->s390_stsi.sel2 = sel2;
720}
721
Christian Borntraeger453423d2008-03-25 18:47:29 +0100722static int handle_stsi(struct kvm_vcpu *vcpu)
723{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100724 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
725 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
726 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100727 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100728 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100729 int rc = 0;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100730 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100731
732 vcpu->stat.instruction_stsi++;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200733 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100734
Thomas Huth5087dfa2013-06-20 17:22:01 +0200735 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
736 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
737
Thomas Huth87d41fb2013-06-20 17:22:05 +0200738 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200739 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200740 return 0;
741 }
742
743 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
744 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
745 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
746
747 if (fc == 0) {
748 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200749 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200750 return 0;
751 }
752
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300753 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100754
Thomas Huth87d41fb2013-06-20 17:22:05 +0200755 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100756 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
757
758 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100759 case 1: /* same handling for 1 and 2 */
760 case 2:
761 mem = get_zeroed_page(GFP_KERNEL);
762 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100763 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200764 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100765 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100766 break;
767 case 3:
768 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100769 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100770 mem = get_zeroed_page(GFP_KERNEL);
771 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100772 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100773 handle_stsi_3_2_2(vcpu, (void *) mem);
774 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100775 }
776
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300777 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100778 if (rc) {
779 rc = kvm_s390_inject_prog_cond(vcpu, rc);
780 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100781 }
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100782 if (vcpu->kvm->arch.user_stsi) {
783 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
784 rc = -EREMOTE;
785 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200786 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100787 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200788 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100789 vcpu->run->s.regs.gprs[0] = 0;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100790 return rc;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100791out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200792 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100793out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100794 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100795 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100796}
797
Christian Borntraeger70455a32009-01-22 10:28:29 +0100798int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100799{
Christian Borntraeger6db42632016-04-08 17:52:39 +0200800 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
801 case 0x02:
802 return handle_stidp(vcpu);
803 case 0x04:
804 return handle_set_clock(vcpu);
805 case 0x10:
806 return handle_set_prefix(vcpu);
807 case 0x11:
808 return handle_store_prefix(vcpu);
809 case 0x12:
810 return handle_store_cpu_address(vcpu);
811 case 0x14:
812 return kvm_s390_handle_vsie(vcpu);
813 case 0x21:
814 case 0x50:
815 return handle_ipte_interlock(vcpu);
816 case 0x29:
817 return handle_iske(vcpu);
818 case 0x2a:
819 return handle_rrbe(vcpu);
820 case 0x2b:
821 return handle_sske(vcpu);
822 case 0x2c:
823 return handle_test_block(vcpu);
824 case 0x30:
825 case 0x31:
826 case 0x32:
827 case 0x33:
828 case 0x34:
829 case 0x35:
830 case 0x36:
831 case 0x37:
832 case 0x38:
833 case 0x39:
834 case 0x3a:
835 case 0x3b:
836 case 0x3c:
837 case 0x5f:
838 case 0x74:
839 case 0x76:
840 return handle_io_inst(vcpu);
841 case 0x56:
842 return handle_sthyi(vcpu);
843 case 0x7d:
844 return handle_stsi(vcpu);
845 case 0xb1:
846 return handle_stfl(vcpu);
847 case 0xb2:
848 return handle_lpswe(vcpu);
849 default:
850 return -EOPNOTSUPP;
851 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100852}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200853
Cornelia Huck48a3e952012-12-20 15:32:09 +0100854static int handle_epsw(struct kvm_vcpu *vcpu)
855{
856 int reg1, reg2;
857
Christian Borntraegera37cb072018-01-23 13:28:40 +0100858 vcpu->stat.instruction_epsw++;
859
Thomas Huthaeb87c32013-06-12 13:54:57 +0200860 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100861
862 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200863 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100864 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
865 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200866 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100867 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200868 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100869 }
870 return 0;
871}
872
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200873#define PFMF_RESERVED 0xfffc0101UL
874#define PFMF_SK 0x00020000UL
875#define PFMF_CF 0x00010000UL
876#define PFMF_UI 0x00008000UL
877#define PFMF_FSC 0x00007000UL
878#define PFMF_NQ 0x00000800UL
879#define PFMF_MR 0x00000400UL
880#define PFMF_MC 0x00000200UL
881#define PFMF_KEY 0x000000feUL
882
883static int handle_pfmf(struct kvm_vcpu *vcpu)
884{
David Hildenbrand1824c722016-05-10 09:43:11 +0200885 bool mr = false, mc = false, nq;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200886 int reg1, reg2;
887 unsigned long start, end;
David Hildenbrand1824c722016-05-10 09:43:11 +0200888 unsigned char key;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200889
890 vcpu->stat.instruction_pfmf++;
891
892 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
893
Heiko Carstens03c02802015-11-13 13:31:58 +0100894 if (!test_kvm_facility(vcpu->kvm, 8))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200895 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
896
897 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200898 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200899
900 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
901 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
902
David Hildenbrandedc5b052016-03-04 11:08:09 +0100903 /* Only provide non-quiescing support if enabled for the guest */
904 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
905 !test_kvm_facility(vcpu->kvm, 14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200906 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
907
David Hildenbrand1824c722016-05-10 09:43:11 +0200908 /* Only provide conditional-SSKE support if enabled for the guest */
909 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
910 test_kvm_facility(vcpu->kvm, 10)) {
911 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
912 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
913 }
914
915 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
916 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200917 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100918 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200919
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200920 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
921 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
922 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
923 }
924
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200925 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
926 case 0x00000000:
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200927 /* only 4k frames specify a real address */
928 start = kvm_s390_real_to_abs(vcpu, start);
Heiko Carstens58cdf5e2017-07-05 07:37:14 +0200929 end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200930 break;
931 case 0x00001000:
Heiko Carstens58cdf5e2017-07-05 07:37:14 +0200932 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200933 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200934 case 0x00002000:
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100935 /* only support 2G frame size if EDAT2 is available and we are
936 not in 24-bit addressing mode */
937 if (!test_kvm_facility(vcpu->kvm, 78) ||
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200938 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100939 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens58cdf5e2017-07-05 07:37:14 +0200940 end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1);
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100941 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200942 default:
943 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
944 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100945
David Hildenbrand695be0e2016-05-12 14:07:05 +0200946 while (start != end) {
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200947 unsigned long useraddr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200948
Thomas Huthfb34c6032013-09-09 17:58:38 +0200949 /* Translate guest address to host address */
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200950 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
Thomas Huthfb34c6032013-09-09 17:58:38 +0200951 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200952 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
953
954 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
955 if (clear_user((void __user *)useraddr, PAGE_SIZE))
956 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
957 }
958
959 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Farhan Ali730cd632017-02-24 16:12:56 -0500960 int rc = kvm_s390_skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200961
962 if (rc)
963 return rc;
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100964 down_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200965 rc = cond_set_guest_storage_key(current->mm, useraddr,
966 key, NULL, nq, mr, mc);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100967 up_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200968 if (rc < 0)
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200969 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
970 }
971
972 start += PAGE_SIZE;
973 }
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200974 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200975 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200976 vcpu->run->s.regs.gprs[reg2] = end;
977 } else {
978 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
979 end = kvm_s390_logical_to_effective(vcpu, end);
980 vcpu->run->s.regs.gprs[reg2] |= end;
981 }
982 }
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200983 return 0;
984}
985
Claudio Imbrenda190df4a2016-08-04 17:54:42 +0200986static inline int do_essa(struct kvm_vcpu *vcpu, const int orc)
987{
988 struct kvm_s390_migration_state *ms = vcpu->kvm->arch.migration_state;
989 int r1, r2, nappended, entries;
990 unsigned long gfn, hva, res, pgstev, ptev;
991 unsigned long *cbrlo;
992
993 /*
994 * We don't need to set SD.FPF.SK to 1 here, because if we have a
995 * machine check here we either handle it or crash
996 */
997
998 kvm_s390_get_regs_rre(vcpu, &r1, &r2);
999 gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
1000 hva = gfn_to_hva(vcpu->kvm, gfn);
1001 entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1002
1003 if (kvm_is_error_hva(hva))
1004 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1005
1006 nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
1007 if (nappended < 0) {
1008 res = orc ? 0x10 : 0;
1009 vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
1010 return 0;
1011 }
1012 res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
1013 /*
1014 * Set the block-content state part of the result. 0 means resident, so
1015 * nothing to do if the page is valid. 2 is for preserved pages
1016 * (non-present and non-zero), and 3 for zero pages (non-present and
1017 * zero).
1018 */
1019 if (ptev & _PAGE_INVALID) {
1020 res |= 2;
1021 if (pgstev & _PGSTE_GPS_ZERO)
1022 res |= 1;
1023 }
Claudio Imbrenda1bab1c02016-08-29 15:56:55 +02001024 if (pgstev & _PGSTE_GPS_NODAT)
1025 res |= 0x20;
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001026 vcpu->run->s.regs.gprs[r1] = res;
1027 /*
1028 * It is possible that all the normal 511 slots were full, in which case
1029 * we will now write in the 512th slot, which is reserved for host use.
1030 * In both cases we let the normal essa handling code process all the
1031 * slots, including the reserved one, if needed.
1032 */
1033 if (nappended > 0) {
1034 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
1035 cbrlo[entries] = gfn << PAGE_SHIFT;
1036 }
1037
Christian Borntraegerc2cf2652017-12-21 09:18:22 +01001038 if (orc && gfn < ms->bitmap_size) {
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001039 /* increment only if we are really flipping the bit to 1 */
1040 if (!test_and_set_bit(gfn, ms->pgste_bitmap))
1041 atomic64_inc(&ms->dirty_pages);
1042 }
1043
1044 return nappended;
1045}
1046
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001047static int handle_essa(struct kvm_vcpu *vcpu)
1048{
1049 /* entries expected to be 1FF */
1050 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
David Hildenbrand4a5e7e32016-04-12 13:32:25 +02001051 unsigned long *cbrlo;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001052 struct gmap *gmap;
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001053 int i, orc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001054
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001055 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001056 gmap = vcpu->arch.gmap;
1057 vcpu->stat.instruction_essa++;
Dominik Dingele6db1d62015-05-07 15:41:57 +02001058 if (!vcpu->kvm->arch.use_cmma)
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001059 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1060
1061 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1062 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001063 /* Check for invalid operation request code */
1064 orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
Claudio Imbrenda1bab1c02016-08-29 15:56:55 +02001065 /* ORCs 0-6 are always valid */
1066 if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
1067 : ESSA_SET_STABLE_IF_RESIDENT))
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001068 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1069
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001070 if (likely(!vcpu->kvm->arch.migration_state)) {
1071 /*
1072 * CMMA is enabled in the KVM settings, but is disabled in
1073 * the SIE block and in the mm_context, and we are not doing
1074 * a migration. Enable CMMA in the mm_context.
1075 * Since we need to take a write lock to write to the context
1076 * to avoid races with storage keys handling, we check if the
1077 * value really needs to be written to; if the value is
1078 * already correct, we do nothing and avoid the lock.
1079 */
1080 if (vcpu->kvm->mm->context.use_cmma == 0) {
1081 down_write(&vcpu->kvm->mm->mmap_sem);
1082 vcpu->kvm->mm->context.use_cmma = 1;
1083 up_write(&vcpu->kvm->mm->mmap_sem);
1084 }
1085 /*
1086 * If we are here, we are supposed to have CMMA enabled in
1087 * the SIE block. Enabling CMMA works on a per-CPU basis,
1088 * while the context use_cmma flag is per process.
1089 * It's possible that the context flag is enabled and the
1090 * SIE flag is not, so we set the flag always; if it was
1091 * already set, nothing changes, otherwise we enable it
1092 * on this CPU too.
1093 */
1094 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
1095 /* Retry the ESSA instruction */
1096 kvm_s390_retry_instr(vcpu);
1097 } else {
1098 /* Account for the possible extra cbrl entry */
1099 i = do_essa(vcpu, orc);
1100 if (i < 0)
1101 return i;
1102 entries += i;
1103 }
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001104 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
1105 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
1106 down_read(&gmap->mm->mmap_sem);
David Hildenbrand4a5e7e32016-04-12 13:32:25 +02001107 for (i = 0; i < entries; ++i)
1108 __gmap_zap(gmap, cbrlo[i]);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001109 up_read(&gmap->mm->mmap_sem);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001110 return 0;
1111}
1112
Cornelia Huck48a3e952012-12-20 15:32:09 +01001113int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
1114{
Christian Borntraeger6db42632016-04-08 17:52:39 +02001115 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1116 case 0x8a:
1117 case 0x8e:
1118 case 0x8f:
1119 return handle_ipte_interlock(vcpu);
1120 case 0x8d:
1121 return handle_epsw(vcpu);
1122 case 0xab:
1123 return handle_essa(vcpu);
1124 case 0xaf:
1125 return handle_pfmf(vcpu);
1126 default:
1127 return -EOPNOTSUPP;
1128 }
Cornelia Huck48a3e952012-12-20 15:32:09 +01001129}
1130
Thomas Huth953ed882013-06-20 17:22:04 +02001131int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1132{
1133 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1134 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001135 int reg, rc, nr_regs;
1136 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001137 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001138 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001139
1140 vcpu->stat.instruction_lctl++;
1141
1142 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 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001146
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001147 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +02001148 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1149
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001150 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001151 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001152
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001153 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001154 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001155 if (rc)
1156 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +02001157 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001158 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001159 do {
Thomas Huth953ed882013-06-20 17:22:04 +02001160 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001161 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001162 if (reg == reg3)
1163 break;
1164 reg = (reg + 1) % 16;
1165 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001166 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001167 return 0;
1168}
1169
David Hildenbrandaba07502014-01-23 10:47:13 +01001170int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1171{
1172 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1173 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001174 int reg, rc, nr_regs;
1175 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +01001176 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001177 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001178
1179 vcpu->stat.instruction_stctl++;
1180
1181 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1182 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1183
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001184 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001185
1186 if (ga & 3)
1187 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1188
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001189 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001190 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1191
1192 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001193 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001194 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001195 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001196 if (reg == reg3)
1197 break;
1198 reg = (reg + 1) % 16;
1199 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001200 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001201 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001202}
1203
Thomas Huth953ed882013-06-20 17:22:04 +02001204static int handle_lctlg(struct kvm_vcpu *vcpu)
1205{
1206 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1207 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001208 int reg, rc, nr_regs;
1209 u64 ctl_array[16];
1210 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001211 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001212
1213 vcpu->stat.instruction_lctlg++;
1214
1215 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1216 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1217
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001218 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001219
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001220 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +02001221 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1222
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001223 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001224 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001225
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001226 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001227 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001228 if (rc)
1229 return kvm_s390_inject_prog_cond(vcpu, rc);
1230 reg = reg1;
1231 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001232 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001233 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001234 if (reg == reg3)
1235 break;
1236 reg = (reg + 1) % 16;
1237 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001238 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001239 return 0;
1240}
1241
David Hildenbrandaba07502014-01-23 10:47:13 +01001242static int handle_stctg(struct kvm_vcpu *vcpu)
1243{
1244 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1245 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001246 int reg, rc, nr_regs;
1247 u64 ctl_array[16];
1248 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001249 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001250
1251 vcpu->stat.instruction_stctg++;
1252
1253 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1254 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1255
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001256 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001257
1258 if (ga & 7)
1259 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1260
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001261 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001262 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1263
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001264 reg = reg1;
1265 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001266 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001267 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001268 if (reg == reg3)
1269 break;
1270 reg = (reg + 1) % 16;
1271 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001272 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001273 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001274}
1275
Thomas Huth953ed882013-06-20 17:22:04 +02001276int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +01001277{
Christian Borntraeger6db42632016-04-08 17:52:39 +02001278 switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
1279 case 0x25:
1280 return handle_stctg(vcpu);
1281 case 0x2f:
1282 return handle_lctlg(vcpu);
1283 case 0x60:
1284 case 0x61:
1285 case 0x62:
1286 return handle_ri(vcpu);
1287 default:
1288 return -EOPNOTSUPP;
1289 }
Cornelia Huckf379aae2012-12-20 15:32:10 +01001290}
1291
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001292static int handle_tprot(struct kvm_vcpu *vcpu)
1293{
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001294 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +01001295 unsigned long hva, gpa;
1296 int ret = 0, cc = 0;
1297 bool writable;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001298 u8 ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001299
1300 vcpu->stat.instruction_tprot++;
1301
Thomas Huthf9f6bbc2013-06-20 17:22:00 +02001302 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1303 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1304
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001305 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001306
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001307 /* we only handle the Linux memory detection case:
1308 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001309 * everything else goes to userspace. */
1310 if (address2 & 0xf0)
1311 return -EOPNOTSUPP;
1312 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +01001313 ipte_lock(vcpu);
David Hildenbrand92c96322015-11-16 15:42:11 +01001314 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
Thomas Hutha0465f92014-02-04 14:48:07 +01001315 if (ret == PGM_PROTECTION) {
1316 /* Write protected? Try again with read-only... */
1317 cc = 1;
David Hildenbrand92c96322015-11-16 15:42:11 +01001318 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1319 GACC_FETCH);
Thomas Hutha0465f92014-02-04 14:48:07 +01001320 }
1321 if (ret) {
1322 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1323 ret = kvm_s390_inject_program_int(vcpu, ret);
1324 } else if (ret > 0) {
1325 /* Translation not available */
1326 kvm_s390_set_psw_cc(vcpu, 3);
1327 ret = 0;
1328 }
1329 goto out_unlock;
1330 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001331
Thomas Hutha0465f92014-02-04 14:48:07 +01001332 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1333 if (kvm_is_error_hva(hva)) {
1334 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1335 } else {
1336 if (!writable)
1337 cc = 1; /* Write not permitted ==> read-only */
1338 kvm_s390_set_psw_cc(vcpu, cc);
1339 /* Note: CC2 only occurs for storage keys (not supported yet) */
1340 }
1341out_unlock:
1342 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1343 ipte_unlock(vcpu);
1344 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001345}
1346
1347int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1348{
Christian Borntraeger6db42632016-04-08 17:52:39 +02001349 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1350 case 0x01:
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001351 return handle_tprot(vcpu);
Christian Borntraeger6db42632016-04-08 17:52:39 +02001352 default:
1353 return -EOPNOTSUPP;
1354 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001355}
1356
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001357static int handle_sckpf(struct kvm_vcpu *vcpu)
1358{
1359 u32 value;
1360
Christian Borntraegera37cb072018-01-23 13:28:40 +01001361 vcpu->stat.instruction_sckpf++;
1362
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001363 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001364 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001365
1366 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1367 return kvm_s390_inject_program_int(vcpu,
1368 PGM_SPECIFICATION);
1369
1370 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1371 vcpu->arch.sie_block->todpr = value;
1372
1373 return 0;
1374}
1375
David Hildenbrand9acc3172016-07-18 09:18:13 +02001376static int handle_ptff(struct kvm_vcpu *vcpu)
1377{
Christian Borntraegera37cb072018-01-23 13:28:40 +01001378 vcpu->stat.instruction_ptff++;
1379
David Hildenbrand9acc3172016-07-18 09:18:13 +02001380 /* we don't emulate any control instructions yet */
1381 kvm_s390_set_psw_cc(vcpu, 3);
1382 return 0;
1383}
1384
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001385int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1386{
Christian Borntraeger6db42632016-04-08 17:52:39 +02001387 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1388 case 0x04:
1389 return handle_ptff(vcpu);
1390 case 0x07:
1391 return handle_sckpf(vcpu);
1392 default:
1393 return -EOPNOTSUPP;
1394 }
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001395}