blob: eb0eb60c7be6a26677f8ed20509aba88df6da337 [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>
David Hildenbranda7e19ab2016-05-10 09:50:21 +020029#include <asm/sclp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010030#include "gaccess.h"
31#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020032#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010033
Fan Zhang80cd8762016-08-15 04:53:22 +020034static int handle_ri(struct kvm_vcpu *vcpu)
35{
Christian Borntraegera37cb072018-01-23 13:28:40 +010036 vcpu->stat.instruction_ri++;
37
Fan Zhang80cd8762016-08-15 04:53:22 +020038 if (test_kvm_facility(vcpu->kvm, 64)) {
Christian Borntraeger4d5f2c02017-02-09 17:15:41 +010039 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
David Hildenbrand0c9d8682017-03-13 11:48:28 +010040 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
Fan Zhang80cd8762016-08-15 04:53:22 +020041 kvm_s390_retry_instr(vcpu);
42 return 0;
43 } else
44 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
45}
46
47int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
48{
49 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
50 return handle_ri(vcpu);
51 else
52 return -EOPNOTSUPP;
53}
54
Fan Zhang4e0b1ab2016-11-29 07:17:55 +010055static int handle_gs(struct kvm_vcpu *vcpu)
56{
Christian Borntraegera37cb072018-01-23 13:28:40 +010057 vcpu->stat.instruction_gs++;
58
Fan Zhang4e0b1ab2016-11-29 07:17:55 +010059 if (test_kvm_facility(vcpu->kvm, 133)) {
60 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
61 preempt_disable();
62 __ctl_set_bit(2, 4);
63 current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
64 restore_gs_cb(current->thread.gs_cb);
65 preempt_enable();
66 vcpu->arch.sie_block->ecb |= ECB_GS;
67 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
68 vcpu->arch.gs_enabled = 1;
69 kvm_s390_retry_instr(vcpu);
70 return 0;
71 } else
72 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
73}
74
75int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
76{
77 int code = vcpu->arch.sie_block->ipb & 0xff;
78
79 if (code == 0x49 || code == 0x4d)
80 return handle_gs(vcpu);
81 else
82 return -EOPNOTSUPP;
83}
Thomas Huth6a3f95a2013-09-12 10:33:49 +020084/* Handle SCK (SET CLOCK) interception */
85static int handle_set_clock(struct kvm_vcpu *vcpu)
86{
David Hildenbrand0e7def52018-02-07 12:46:43 +010087 struct kvm_s390_vm_tod_clock gtod = { 0 };
David Hildenbrand25ed1672015-05-12 09:49:14 +020088 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +010089 u8 ar;
David Hildenbrand0e7def52018-02-07 12:46:43 +010090 u64 op2;
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);
David Hildenbrand0e7def52018-02-07 12:46:43 +0100100 rc = read_guest(vcpu, op2, ar, &gtod.tod, sizeof(gtod.tod));
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
David Hildenbrand0e7def52018-02-07 12:46:43 +0100104 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);
105 kvm_s390_set_tod_clock(vcpu->kvm, &gtod);
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{
Janosch Frank55531b72018-02-15 16:33:47 +0100207 int rc;
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);
Janosch Frank55531b72018-02-15 16:33:47 +0100211 /* Already enabled? */
212 if (vcpu->kvm->arch.use_skf &&
213 !(sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)) &&
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +0100214 !kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
Janosch Frank55531b72018-02-15 16:33:47 +0100215 return 0;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100216
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200217 rc = s390_enable_skey();
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200218 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
Janosch Frank55531b72018-02-15 16:33:47 +0100219 if (rc)
220 return rc;
221
222 if (kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
223 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_KSS);
224 if (!vcpu->kvm->arch.use_skf)
225 sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
226 else
227 sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
228 return 0;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100229}
230
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200231static int try_handle_skey(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100232{
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200233 int rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100234
Farhan Ali730cd632017-02-24 16:12:56 -0500235 rc = kvm_s390_skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200236 if (rc)
237 return rc;
Janosch Frank55531b72018-02-15 16:33:47 +0100238 if (vcpu->kvm->arch.use_skf) {
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200239 /* with storage-key facility, SIE interprets it for us */
240 kvm_s390_retry_instr(vcpu);
241 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
242 return -EAGAIN;
243 }
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200244 return 0;
245}
Thomas Huth5087dfa2013-06-20 17:22:01 +0200246
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200247static int handle_iske(struct kvm_vcpu *vcpu)
248{
249 unsigned long addr;
250 unsigned char key;
251 int reg1, reg2;
252 int rc;
253
Christian Borntraegera37cb072018-01-23 13:28:40 +0100254 vcpu->stat.instruction_iske++;
255
Janosch Frankca76ec92017-12-04 12:19:11 +0100256 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
257 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
258
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200259 rc = try_handle_skey(vcpu);
260 if (rc)
261 return rc != -EAGAIN ? rc : 0;
262
263 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
264
265 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
266 addr = kvm_s390_logical_to_effective(vcpu, addr);
267 addr = kvm_s390_real_to_abs(vcpu, addr);
268 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
269 if (kvm_is_error_hva(addr))
270 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
271
272 down_read(&current->mm->mmap_sem);
273 rc = get_guest_storage_key(current->mm, addr, &key);
274 up_read(&current->mm->mmap_sem);
275 if (rc)
276 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
277 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
278 vcpu->run->s.regs.gprs[reg1] |= key;
279 return 0;
280}
281
282static int handle_rrbe(struct kvm_vcpu *vcpu)
283{
284 unsigned long addr;
285 int reg1, reg2;
286 int rc;
287
Christian Borntraegera37cb072018-01-23 13:28:40 +0100288 vcpu->stat.instruction_rrbe++;
289
Janosch Frankca76ec92017-12-04 12:19:11 +0100290 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
291 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
292
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200293 rc = try_handle_skey(vcpu);
294 if (rc)
295 return rc != -EAGAIN ? rc : 0;
296
297 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
298
299 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
300 addr = kvm_s390_logical_to_effective(vcpu, addr);
301 addr = kvm_s390_real_to_abs(vcpu, addr);
302 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
303 if (kvm_is_error_hva(addr))
304 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
305
306 down_read(&current->mm->mmap_sem);
307 rc = reset_guest_reference_bit(current->mm, addr);
308 up_read(&current->mm->mmap_sem);
309 if (rc < 0)
310 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
311
312 kvm_s390_set_psw_cc(vcpu, rc);
313 return 0;
314}
315
316#define SSKE_NQ 0x8
317#define SSKE_MR 0x4
318#define SSKE_MC 0x2
319#define SSKE_MB 0x1
320static int handle_sske(struct kvm_vcpu *vcpu)
321{
322 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
323 unsigned long start, end;
324 unsigned char key, oldkey;
325 int reg1, reg2;
326 int rc;
327
Christian Borntraegera37cb072018-01-23 13:28:40 +0100328 vcpu->stat.instruction_sske++;
329
Janosch Frankca76ec92017-12-04 12:19:11 +0100330 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
331 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
332
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200333 rc = try_handle_skey(vcpu);
334 if (rc)
335 return rc != -EAGAIN ? rc : 0;
336
337 if (!test_kvm_facility(vcpu->kvm, 8))
338 m3 &= ~SSKE_MB;
339 if (!test_kvm_facility(vcpu->kvm, 10))
340 m3 &= ~(SSKE_MC | SSKE_MR);
341 if (!test_kvm_facility(vcpu->kvm, 14))
342 m3 &= ~SSKE_NQ;
343
344 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
345
346 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
347 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
348 start = kvm_s390_logical_to_effective(vcpu, start);
349 if (m3 & SSKE_MB) {
350 /* start already designates an absolute address */
Heiko Carstens58cdf5e2017-07-05 07:37:14 +0200351 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200352 } else {
353 start = kvm_s390_real_to_abs(vcpu, start);
354 end = start + PAGE_SIZE;
355 }
356
357 while (start != end) {
358 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
359
360 if (kvm_is_error_hva(addr))
361 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
362
363 down_read(&current->mm->mmap_sem);
364 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
365 m3 & SSKE_NQ, m3 & SSKE_MR,
366 m3 & SSKE_MC);
367 up_read(&current->mm->mmap_sem);
368 if (rc < 0)
369 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
370 start += PAGE_SIZE;
Heiko Carstens0b925152017-01-02 08:51:02 +0100371 }
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200372
373 if (m3 & (SSKE_MC | SSKE_MR)) {
374 if (m3 & SSKE_MB) {
375 /* skey in reg1 is unpredictable */
376 kvm_s390_set_psw_cc(vcpu, 3);
377 } else {
378 kvm_s390_set_psw_cc(vcpu, rc);
379 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
380 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
381 }
382 }
383 if (m3 & SSKE_MB) {
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200384 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200385 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
386 else
387 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
388 end = kvm_s390_logical_to_effective(vcpu, end);
389 vcpu->run->s.regs.gprs[reg2] |= end;
390 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100391 return 0;
392}
393
Heiko Carstens8a2422342014-01-10 14:33:28 +0100394static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
395{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100396 vcpu->stat.instruction_ipte_interlock++;
Heiko Carstensa7525982017-06-03 10:56:07 +0200397 if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100398 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
399 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100400 kvm_s390_retry_instr(vcpu);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100401 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
402 return 0;
403}
404
Thomas Huthaca84242013-09-12 10:33:48 +0200405static int handle_test_block(struct kvm_vcpu *vcpu)
406{
Thomas Huthaca84242013-09-12 10:33:48 +0200407 gpa_t addr;
408 int reg2;
409
Christian Borntraegera37cb072018-01-23 13:28:40 +0100410 vcpu->stat.instruction_tb++;
411
Thomas Huthaca84242013-09-12 10:33:48 +0200412 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
413 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
414
415 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
416 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100417 addr = kvm_s390_logical_to_effective(vcpu, addr);
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300418 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
Thomas Huthe45efa22014-03-07 12:14:23 +0100419 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200420 addr = kvm_s390_real_to_abs(vcpu, addr);
421
Heiko Carstensef23e772014-01-01 16:53:49 +0100422 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200423 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
424 /*
425 * We don't expect errors on modern systems, and do not care
426 * about storage keys (yet), so let's just clear the page.
427 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100428 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200429 return -EFAULT;
430 kvm_s390_set_psw_cc(vcpu, 0);
431 vcpu->run->s.regs.gprs[0] = 0;
432 return 0;
433}
434
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100435static int handle_tpi(struct kvm_vcpu *vcpu)
436{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100437 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100438 unsigned long len;
439 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100440 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100441 u64 addr;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100442 u8 ar;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100443
Christian Borntraegera37cb072018-01-23 13:28:40 +0100444 vcpu->stat.instruction_tpi++;
445
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300446 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100447 if (addr & 3)
448 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100449
Thomas Huthf0926692013-10-09 14:15:54 +0200450 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100451 if (!inti) {
452 kvm_s390_set_psw_cc(vcpu, 0);
453 return 0;
454 }
455
Heiko Carstens4799b552014-01-01 16:55:48 +0100456 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
457 tpi_data[1] = inti->io.io_int_parm;
458 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100459 if (addr) {
460 /*
461 * Store the two-word I/O interruption code into the
462 * provided area.
463 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100464 len = sizeof(tpi_data) - 4;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300465 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100466 if (rc) {
467 rc = kvm_s390_inject_prog_cond(vcpu, rc);
468 goto reinject_interrupt;
469 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100470 } else {
471 /*
472 * Store the three-word I/O interruption code into
473 * the appropriate lowcore area.
474 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100475 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100476 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
477 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100478 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100479 goto reinject_interrupt;
480 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100481 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100482
483 /* irq was successfully handed to the guest */
484 kfree(inti);
485 kvm_s390_set_psw_cc(vcpu, 1);
486 return 0;
487reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100488 /*
489 * If we encounter a problem storing the interruption code, the
490 * instruction is suppressed from the guest's view: reinject the
491 * interrupt.
492 */
David Hildenbrand15462e32015-02-04 15:59:11 +0100493 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
494 kfree(inti);
495 rc = -EFAULT;
496 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100497 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100498 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100499}
500
501static int handle_tsch(struct kvm_vcpu *vcpu)
502{
Jens Freimann6d3da242013-07-03 15:18:35 +0200503 struct kvm_s390_interrupt_info *inti = NULL;
504 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100505
Christian Borntraegera37cb072018-01-23 13:28:40 +0100506 vcpu->stat.instruction_tsch++;
507
Jens Freimann6d3da242013-07-03 15:18:35 +0200508 /* a valid schid has at least one bit set */
509 if (vcpu->run->s.regs.gprs[1])
510 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
511 vcpu->run->s.regs.gprs[1]);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100512
513 /*
514 * Prepare exit to userspace.
515 * We indicate whether we dequeued a pending I/O interrupt
516 * so that userspace can re-inject it if the instruction gets
517 * a program check. While this may re-order the pending I/O
518 * interrupts, this is no problem since the priority is kept
519 * intact.
520 */
521 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
522 vcpu->run->s390_tsch.dequeued = !!inti;
523 if (inti) {
524 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
525 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
526 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
527 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
528 }
529 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
530 kfree(inti);
531 return -EREMOTE;
532}
533
Cornelia Huckf379aae2012-12-20 15:32:10 +0100534static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100535{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100536 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100537
Thomas Huth5087dfa2013-06-20 17:22:01 +0200538 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
539 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
540
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100541 if (vcpu->kvm->arch.css_support) {
542 /*
543 * Most I/O instructions will be handled by userspace.
544 * Exceptions are tpi and the interrupt portion of tsch.
545 */
546 if (vcpu->arch.sie_block->ipa == 0xb236)
547 return handle_tpi(vcpu);
548 if (vcpu->arch.sie_block->ipa == 0xb235)
549 return handle_tsch(vcpu);
550 /* Handle in userspace. */
Christian Borntraegera37cb072018-01-23 13:28:40 +0100551 vcpu->stat.instruction_io_other++;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100552 return -EOPNOTSUPP;
553 } else {
554 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100555 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100556 * I/O instructions.
557 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200558 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100559 return 0;
560 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100561}
562
Christian Borntraeger453423d2008-03-25 18:47:29 +0100563static int handle_stfl(struct kvm_vcpu *vcpu)
564{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100565 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100566 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100567
568 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200569
570 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
571 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
572
Michael Mueller9d8d5782015-02-02 15:42:51 +0100573 /*
574 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
575 * into a u32 memory representation. They will remain bits 0-31.
576 */
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100577 fac = *vcpu->kvm->arch.model.fac_list >> 32;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100578 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100579 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100580 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100581 return rc;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200582 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100583 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100584 return 0;
585}
586
Cornelia Huck48a3e952012-12-20 15:32:09 +0100587#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
588#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100589#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100590#define PSW_ADDR_31 0x000000007fffffffUL
591
Thomas Hutha3fb5772014-04-17 09:10:40 +0200592int is_valid_psw(psw_t *psw)
593{
Heiko Carstens3736b872013-03-25 17:22:52 +0100594 if (psw->mask & PSW_MASK_UNASSIGNED)
595 return 0;
596 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
597 if (psw->addr & ~PSW_ADDR_31)
598 return 0;
599 }
600 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
601 return 0;
602 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
603 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200604 if (psw->addr & 1)
605 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100606 return 1;
607}
608
Cornelia Huck48a3e952012-12-20 15:32:09 +0100609int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
610{
Heiko Carstens3736b872013-03-25 17:22:52 +0100611 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100612 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100613 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100614 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100615 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100616
Christian Borntraegera37cb072018-01-23 13:28:40 +0100617 vcpu->stat.instruction_lpsw++;
618
Heiko Carstens3736b872013-03-25 17:22:52 +0100619 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200620 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
621
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300622 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100623 if (addr & 7)
624 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100625
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300626 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100627 if (rc)
628 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100629 if (!(new_psw.mask & PSW32_MASK_BASE))
630 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100631 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
632 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
633 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
634 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100635 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100636 return 0;
637}
638
639static int handle_lpswe(struct kvm_vcpu *vcpu)
640{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100641 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100642 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100643 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100644 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100645
Christian Borntraegera37cb072018-01-23 13:28:40 +0100646 vcpu->stat.instruction_lpswe++;
647
Thomas Huth5087dfa2013-06-20 17:22:01 +0200648 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
649 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
650
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300651 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100652 if (addr & 7)
653 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300654 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100655 if (rc)
656 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100657 vcpu->arch.sie_block->gpsw = new_psw;
658 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100659 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100660 return 0;
661}
662
Christian Borntraeger453423d2008-03-25 18:47:29 +0100663static int handle_stidp(struct kvm_vcpu *vcpu)
664{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200665 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100666 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100667 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100668 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100669
670 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100671
Thomas Huth5087dfa2013-06-20 17:22:01 +0200672 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
673 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
674
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300675 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100676
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100677 if (operand2 & 7)
678 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100679
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300680 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
Heiko Carstens7d777d72014-01-01 16:58:16 +0100681 if (rc)
682 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100683
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200684 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100685 return 0;
686}
687
688static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
689{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100690 int cpus = 0;
691 int n;
692
Jens Freimannff520a62014-02-24 10:11:41 +0100693 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100694
695 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200696 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100697 mem->count = 0;
698 if (mem->count < 8)
699 mem->count++;
700 for (n = mem->count - 1; n > 0 ; n--)
701 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
702
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100703 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100704 mem->vm[0].cpus_total = cpus;
705 mem->vm[0].cpus_configured = cpus;
706 mem->vm[0].cpus_standby = 0;
707 mem->vm[0].cpus_reserved = 0;
708 mem->vm[0].caf = 1000;
709 memcpy(mem->vm[0].name, "KVMguest", 8);
710 ASCEBC(mem->vm[0].name, 8);
711 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
712 ASCEBC(mem->vm[0].cpi, 16);
713}
714
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100715static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100716 u8 fc, u8 sel1, u16 sel2)
717{
718 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
719 vcpu->run->s390_stsi.addr = addr;
720 vcpu->run->s390_stsi.ar = ar;
721 vcpu->run->s390_stsi.fc = fc;
722 vcpu->run->s390_stsi.sel1 = sel1;
723 vcpu->run->s390_stsi.sel2 = sel2;
724}
725
Christian Borntraeger453423d2008-03-25 18:47:29 +0100726static int handle_stsi(struct kvm_vcpu *vcpu)
727{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100728 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
729 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
730 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100731 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100732 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100733 int rc = 0;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100734 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100735
736 vcpu->stat.instruction_stsi++;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200737 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100738
Thomas Huth5087dfa2013-06-20 17:22:01 +0200739 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
740 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
741
Thomas Huth87d41fb2013-06-20 17:22:05 +0200742 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200743 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200744 return 0;
745 }
746
747 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
748 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
749 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
750
751 if (fc == 0) {
752 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200753 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200754 return 0;
755 }
756
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300757 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100758
Thomas Huth87d41fb2013-06-20 17:22:05 +0200759 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100760 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
761
762 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100763 case 1: /* same handling for 1 and 2 */
764 case 2:
765 mem = get_zeroed_page(GFP_KERNEL);
766 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100767 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200768 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100769 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100770 break;
771 case 3:
772 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100773 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100774 mem = get_zeroed_page(GFP_KERNEL);
775 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100776 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100777 handle_stsi_3_2_2(vcpu, (void *) mem);
778 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100779 }
780
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300781 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100782 if (rc) {
783 rc = kvm_s390_inject_prog_cond(vcpu, rc);
784 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100785 }
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100786 if (vcpu->kvm->arch.user_stsi) {
787 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
788 rc = -EREMOTE;
789 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200790 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100791 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200792 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100793 vcpu->run->s.regs.gprs[0] = 0;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100794 return rc;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100795out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200796 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100797out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100798 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100799 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100800}
801
Christian Borntraeger70455a32009-01-22 10:28:29 +0100802int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100803{
Christian Borntraeger6db42632016-04-08 17:52:39 +0200804 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
805 case 0x02:
806 return handle_stidp(vcpu);
807 case 0x04:
808 return handle_set_clock(vcpu);
809 case 0x10:
810 return handle_set_prefix(vcpu);
811 case 0x11:
812 return handle_store_prefix(vcpu);
813 case 0x12:
814 return handle_store_cpu_address(vcpu);
815 case 0x14:
816 return kvm_s390_handle_vsie(vcpu);
817 case 0x21:
818 case 0x50:
819 return handle_ipte_interlock(vcpu);
820 case 0x29:
821 return handle_iske(vcpu);
822 case 0x2a:
823 return handle_rrbe(vcpu);
824 case 0x2b:
825 return handle_sske(vcpu);
826 case 0x2c:
827 return handle_test_block(vcpu);
828 case 0x30:
829 case 0x31:
830 case 0x32:
831 case 0x33:
832 case 0x34:
833 case 0x35:
834 case 0x36:
835 case 0x37:
836 case 0x38:
837 case 0x39:
838 case 0x3a:
839 case 0x3b:
840 case 0x3c:
841 case 0x5f:
842 case 0x74:
843 case 0x76:
844 return handle_io_inst(vcpu);
845 case 0x56:
846 return handle_sthyi(vcpu);
847 case 0x7d:
848 return handle_stsi(vcpu);
849 case 0xb1:
850 return handle_stfl(vcpu);
851 case 0xb2:
852 return handle_lpswe(vcpu);
853 default:
854 return -EOPNOTSUPP;
855 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100856}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200857
Cornelia Huck48a3e952012-12-20 15:32:09 +0100858static int handle_epsw(struct kvm_vcpu *vcpu)
859{
860 int reg1, reg2;
861
Christian Borntraegera37cb072018-01-23 13:28:40 +0100862 vcpu->stat.instruction_epsw++;
863
Thomas Huthaeb87c32013-06-12 13:54:57 +0200864 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100865
866 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200867 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100868 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
869 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200870 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100871 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200872 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100873 }
874 return 0;
875}
876
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200877#define PFMF_RESERVED 0xfffc0101UL
878#define PFMF_SK 0x00020000UL
879#define PFMF_CF 0x00010000UL
880#define PFMF_UI 0x00008000UL
881#define PFMF_FSC 0x00007000UL
882#define PFMF_NQ 0x00000800UL
883#define PFMF_MR 0x00000400UL
884#define PFMF_MC 0x00000200UL
885#define PFMF_KEY 0x000000feUL
886
887static int handle_pfmf(struct kvm_vcpu *vcpu)
888{
David Hildenbrand1824c722016-05-10 09:43:11 +0200889 bool mr = false, mc = false, nq;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200890 int reg1, reg2;
891 unsigned long start, end;
David Hildenbrand1824c722016-05-10 09:43:11 +0200892 unsigned char key;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200893
894 vcpu->stat.instruction_pfmf++;
895
896 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
897
Heiko Carstens03c02802015-11-13 13:31:58 +0100898 if (!test_kvm_facility(vcpu->kvm, 8))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200899 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
900
901 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200902 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200903
904 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
905 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
906
David Hildenbrandedc5b052016-03-04 11:08:09 +0100907 /* Only provide non-quiescing support if enabled for the guest */
908 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
909 !test_kvm_facility(vcpu->kvm, 14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200910 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
911
David Hildenbrand1824c722016-05-10 09:43:11 +0200912 /* Only provide conditional-SSKE support if enabled for the guest */
913 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
914 test_kvm_facility(vcpu->kvm, 10)) {
915 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
916 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
917 }
918
919 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
920 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200921 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100922 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200923
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200924 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
925 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
926 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
927 }
928
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200929 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
930 case 0x00000000:
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200931 /* only 4k frames specify a real address */
932 start = kvm_s390_real_to_abs(vcpu, start);
Heiko Carstens58cdf5e2017-07-05 07:37:14 +0200933 end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200934 break;
935 case 0x00001000:
Heiko Carstens58cdf5e2017-07-05 07:37:14 +0200936 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200937 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200938 case 0x00002000:
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100939 /* only support 2G frame size if EDAT2 is available and we are
940 not in 24-bit addressing mode */
941 if (!test_kvm_facility(vcpu->kvm, 78) ||
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200942 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100943 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens58cdf5e2017-07-05 07:37:14 +0200944 end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1);
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100945 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200946 default:
947 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
948 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100949
David Hildenbrand695be0e2016-05-12 14:07:05 +0200950 while (start != end) {
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200951 unsigned long useraddr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200952
Thomas Huthfb34c6032013-09-09 17:58:38 +0200953 /* Translate guest address to host address */
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200954 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
Thomas Huthfb34c6032013-09-09 17:58:38 +0200955 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200956 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
957
958 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
959 if (clear_user((void __user *)useraddr, PAGE_SIZE))
960 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
961 }
962
963 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Farhan Ali730cd632017-02-24 16:12:56 -0500964 int rc = kvm_s390_skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200965
966 if (rc)
967 return rc;
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100968 down_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200969 rc = cond_set_guest_storage_key(current->mm, useraddr,
970 key, NULL, nq, mr, mc);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100971 up_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200972 if (rc < 0)
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200973 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
974 }
975
976 start += PAGE_SIZE;
977 }
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200978 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200979 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200980 vcpu->run->s.regs.gprs[reg2] = end;
981 } else {
982 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
983 end = kvm_s390_logical_to_effective(vcpu, end);
984 vcpu->run->s.regs.gprs[reg2] |= end;
985 }
986 }
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200987 return 0;
988}
989
Claudio Imbrenda190df4a2016-08-04 17:54:42 +0200990static inline int do_essa(struct kvm_vcpu *vcpu, const int orc)
991{
992 struct kvm_s390_migration_state *ms = vcpu->kvm->arch.migration_state;
993 int r1, r2, nappended, entries;
994 unsigned long gfn, hva, res, pgstev, ptev;
995 unsigned long *cbrlo;
996
997 /*
998 * We don't need to set SD.FPF.SK to 1 here, because if we have a
999 * machine check here we either handle it or crash
1000 */
1001
1002 kvm_s390_get_regs_rre(vcpu, &r1, &r2);
1003 gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
1004 hva = gfn_to_hva(vcpu->kvm, gfn);
1005 entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1006
1007 if (kvm_is_error_hva(hva))
1008 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1009
1010 nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
1011 if (nappended < 0) {
1012 res = orc ? 0x10 : 0;
1013 vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
1014 return 0;
1015 }
1016 res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
1017 /*
1018 * Set the block-content state part of the result. 0 means resident, so
1019 * nothing to do if the page is valid. 2 is for preserved pages
1020 * (non-present and non-zero), and 3 for zero pages (non-present and
1021 * zero).
1022 */
1023 if (ptev & _PAGE_INVALID) {
1024 res |= 2;
1025 if (pgstev & _PGSTE_GPS_ZERO)
1026 res |= 1;
1027 }
Claudio Imbrenda1bab1c02016-08-29 15:56:55 +02001028 if (pgstev & _PGSTE_GPS_NODAT)
1029 res |= 0x20;
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001030 vcpu->run->s.regs.gprs[r1] = res;
1031 /*
1032 * It is possible that all the normal 511 slots were full, in which case
1033 * we will now write in the 512th slot, which is reserved for host use.
1034 * In both cases we let the normal essa handling code process all the
1035 * slots, including the reserved one, if needed.
1036 */
1037 if (nappended > 0) {
1038 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
1039 cbrlo[entries] = gfn << PAGE_SHIFT;
1040 }
1041
Christian Borntraegerc2cf2652017-12-21 09:18:22 +01001042 if (orc && gfn < ms->bitmap_size) {
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001043 /* increment only if we are really flipping the bit to 1 */
1044 if (!test_and_set_bit(gfn, ms->pgste_bitmap))
1045 atomic64_inc(&ms->dirty_pages);
1046 }
1047
1048 return nappended;
1049}
1050
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001051static int handle_essa(struct kvm_vcpu *vcpu)
1052{
1053 /* entries expected to be 1FF */
1054 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
David Hildenbrand4a5e7e32016-04-12 13:32:25 +02001055 unsigned long *cbrlo;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001056 struct gmap *gmap;
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001057 int i, orc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001058
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001059 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001060 gmap = vcpu->arch.gmap;
1061 vcpu->stat.instruction_essa++;
Dominik Dingele6db1d62015-05-07 15:41:57 +02001062 if (!vcpu->kvm->arch.use_cmma)
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001063 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1064
1065 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1066 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001067 /* Check for invalid operation request code */
1068 orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
Claudio Imbrenda1bab1c02016-08-29 15:56:55 +02001069 /* ORCs 0-6 are always valid */
1070 if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
1071 : ESSA_SET_STABLE_IF_RESIDENT))
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001072 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1073
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001074 if (likely(!vcpu->kvm->arch.migration_state)) {
1075 /*
1076 * CMMA is enabled in the KVM settings, but is disabled in
1077 * the SIE block and in the mm_context, and we are not doing
1078 * a migration. Enable CMMA in the mm_context.
1079 * Since we need to take a write lock to write to the context
1080 * to avoid races with storage keys handling, we check if the
1081 * value really needs to be written to; if the value is
1082 * already correct, we do nothing and avoid the lock.
1083 */
Janosch Frankc9f0a2b2018-02-16 12:16:14 +01001084 if (vcpu->kvm->mm->context.uses_cmm == 0) {
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001085 down_write(&vcpu->kvm->mm->mmap_sem);
Janosch Frankc9f0a2b2018-02-16 12:16:14 +01001086 vcpu->kvm->mm->context.uses_cmm = 1;
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001087 up_write(&vcpu->kvm->mm->mmap_sem);
1088 }
1089 /*
1090 * If we are here, we are supposed to have CMMA enabled in
1091 * the SIE block. Enabling CMMA works on a per-CPU basis,
1092 * while the context use_cmma flag is per process.
1093 * It's possible that the context flag is enabled and the
1094 * SIE flag is not, so we set the flag always; if it was
1095 * already set, nothing changes, otherwise we enable it
1096 * on this CPU too.
1097 */
1098 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
1099 /* Retry the ESSA instruction */
1100 kvm_s390_retry_instr(vcpu);
1101 } else {
1102 /* Account for the possible extra cbrl entry */
1103 i = do_essa(vcpu, orc);
1104 if (i < 0)
1105 return i;
1106 entries += i;
1107 }
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001108 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
1109 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
1110 down_read(&gmap->mm->mmap_sem);
David Hildenbrand4a5e7e32016-04-12 13:32:25 +02001111 for (i = 0; i < entries; ++i)
1112 __gmap_zap(gmap, cbrlo[i]);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001113 up_read(&gmap->mm->mmap_sem);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001114 return 0;
1115}
1116
Cornelia Huck48a3e952012-12-20 15:32:09 +01001117int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
1118{
Christian Borntraeger6db42632016-04-08 17:52:39 +02001119 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1120 case 0x8a:
1121 case 0x8e:
1122 case 0x8f:
1123 return handle_ipte_interlock(vcpu);
1124 case 0x8d:
1125 return handle_epsw(vcpu);
1126 case 0xab:
1127 return handle_essa(vcpu);
1128 case 0xaf:
1129 return handle_pfmf(vcpu);
1130 default:
1131 return -EOPNOTSUPP;
1132 }
Cornelia Huck48a3e952012-12-20 15:32:09 +01001133}
1134
Thomas Huth953ed882013-06-20 17:22:04 +02001135int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1136{
1137 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1138 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001139 int reg, rc, nr_regs;
1140 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001141 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001142 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001143
1144 vcpu->stat.instruction_lctl++;
1145
1146 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1147 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1148
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001149 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001150
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001151 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +02001152 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1153
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001154 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001155 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001156
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001157 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001158 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001159 if (rc)
1160 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +02001161 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001162 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001163 do {
Thomas Huth953ed882013-06-20 17:22:04 +02001164 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001165 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001166 if (reg == reg3)
1167 break;
1168 reg = (reg + 1) % 16;
1169 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001170 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001171 return 0;
1172}
1173
David Hildenbrandaba07502014-01-23 10:47:13 +01001174int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1175{
1176 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1177 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001178 int reg, rc, nr_regs;
1179 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +01001180 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001181 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001182
1183 vcpu->stat.instruction_stctl++;
1184
1185 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1186 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1187
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001188 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001189
1190 if (ga & 3)
1191 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1192
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001193 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001194 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1195
1196 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001197 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001198 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001199 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001200 if (reg == reg3)
1201 break;
1202 reg = (reg + 1) % 16;
1203 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001204 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001205 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001206}
1207
Thomas Huth953ed882013-06-20 17:22:04 +02001208static int handle_lctlg(struct kvm_vcpu *vcpu)
1209{
1210 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1211 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001212 int reg, rc, nr_regs;
1213 u64 ctl_array[16];
1214 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001215 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001216
1217 vcpu->stat.instruction_lctlg++;
1218
1219 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1220 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1221
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001222 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001223
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001224 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +02001225 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1226
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001227 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001228 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001229
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001230 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001231 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001232 if (rc)
1233 return kvm_s390_inject_prog_cond(vcpu, rc);
1234 reg = reg1;
1235 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001236 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001237 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001238 if (reg == reg3)
1239 break;
1240 reg = (reg + 1) % 16;
1241 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001242 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001243 return 0;
1244}
1245
David Hildenbrandaba07502014-01-23 10:47:13 +01001246static int handle_stctg(struct kvm_vcpu *vcpu)
1247{
1248 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1249 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001250 int reg, rc, nr_regs;
1251 u64 ctl_array[16];
1252 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001253 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001254
1255 vcpu->stat.instruction_stctg++;
1256
1257 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1258 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1259
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001260 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001261
1262 if (ga & 7)
1263 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1264
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001265 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001266 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1267
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001268 reg = reg1;
1269 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001270 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001271 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001272 if (reg == reg3)
1273 break;
1274 reg = (reg + 1) % 16;
1275 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001276 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001277 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001278}
1279
Thomas Huth953ed882013-06-20 17:22:04 +02001280int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +01001281{
Christian Borntraeger6db42632016-04-08 17:52:39 +02001282 switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
1283 case 0x25:
1284 return handle_stctg(vcpu);
1285 case 0x2f:
1286 return handle_lctlg(vcpu);
1287 case 0x60:
1288 case 0x61:
1289 case 0x62:
1290 return handle_ri(vcpu);
1291 default:
1292 return -EOPNOTSUPP;
1293 }
Cornelia Huckf379aae2012-12-20 15:32:10 +01001294}
1295
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001296static int handle_tprot(struct kvm_vcpu *vcpu)
1297{
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001298 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +01001299 unsigned long hva, gpa;
1300 int ret = 0, cc = 0;
1301 bool writable;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001302 u8 ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001303
1304 vcpu->stat.instruction_tprot++;
1305
Thomas Huthf9f6bbc2013-06-20 17:22:00 +02001306 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1307 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1308
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001309 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001310
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001311 /* we only handle the Linux memory detection case:
1312 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001313 * everything else goes to userspace. */
1314 if (address2 & 0xf0)
1315 return -EOPNOTSUPP;
1316 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +01001317 ipte_lock(vcpu);
David Hildenbrand92c96322015-11-16 15:42:11 +01001318 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
Thomas Hutha0465f92014-02-04 14:48:07 +01001319 if (ret == PGM_PROTECTION) {
1320 /* Write protected? Try again with read-only... */
1321 cc = 1;
David Hildenbrand92c96322015-11-16 15:42:11 +01001322 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1323 GACC_FETCH);
Thomas Hutha0465f92014-02-04 14:48:07 +01001324 }
1325 if (ret) {
1326 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1327 ret = kvm_s390_inject_program_int(vcpu, ret);
1328 } else if (ret > 0) {
1329 /* Translation not available */
1330 kvm_s390_set_psw_cc(vcpu, 3);
1331 ret = 0;
1332 }
1333 goto out_unlock;
1334 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001335
Thomas Hutha0465f92014-02-04 14:48:07 +01001336 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1337 if (kvm_is_error_hva(hva)) {
1338 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1339 } else {
1340 if (!writable)
1341 cc = 1; /* Write not permitted ==> read-only */
1342 kvm_s390_set_psw_cc(vcpu, cc);
1343 /* Note: CC2 only occurs for storage keys (not supported yet) */
1344 }
1345out_unlock:
1346 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1347 ipte_unlock(vcpu);
1348 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001349}
1350
1351int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1352{
Christian Borntraeger6db42632016-04-08 17:52:39 +02001353 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1354 case 0x01:
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001355 return handle_tprot(vcpu);
Christian Borntraeger6db42632016-04-08 17:52:39 +02001356 default:
1357 return -EOPNOTSUPP;
1358 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001359}
1360
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001361static int handle_sckpf(struct kvm_vcpu *vcpu)
1362{
1363 u32 value;
1364
Christian Borntraegera37cb072018-01-23 13:28:40 +01001365 vcpu->stat.instruction_sckpf++;
1366
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001367 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001368 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001369
1370 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1371 return kvm_s390_inject_program_int(vcpu,
1372 PGM_SPECIFICATION);
1373
1374 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1375 vcpu->arch.sie_block->todpr = value;
1376
1377 return 0;
1378}
1379
David Hildenbrand9acc3172016-07-18 09:18:13 +02001380static int handle_ptff(struct kvm_vcpu *vcpu)
1381{
Christian Borntraegera37cb072018-01-23 13:28:40 +01001382 vcpu->stat.instruction_ptff++;
1383
David Hildenbrand9acc3172016-07-18 09:18:13 +02001384 /* we don't emulate any control instructions yet */
1385 kvm_s390_set_psw_cc(vcpu, 3);
1386 return 0;
1387}
1388
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001389int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1390{
Christian Borntraeger6db42632016-04-08 17:52:39 +02001391 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1392 case 0x04:
1393 return handle_ptff(vcpu);
1394 case 0x07:
1395 return handle_sckpf(vcpu);
1396 default:
1397 return -EOPNOTSUPP;
1398 }
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001399}