blob: a226c459809bf0d16657cec3b578c4fe43cd729b [file] [log] [blame]
Christian Borntraeger453423d2008-03-25 18:47:29 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling privileged instructions
Christian Borntraeger453423d2008-03-25 18:47:29 +01003 *
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02004 * Copyright IBM Corp. 2008, 2013
Christian Borntraeger453423d2008-03-25 18:47:29 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 */
13
14#include <linux/kvm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010016#include <linux/errno.h>
Heiko Carstensb13b5dc2013-03-25 17:22:55 +010017#include <linux/compat.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010018#include <linux/mm_types.h>
19
Heiko Carstens7c959e82013-03-05 13:14:46 +010020#include <asm/asm-offsets.h>
Heiko Carstense769ece2013-07-26 15:04:01 +020021#include <asm/facility.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010022#include <asm/current.h>
23#include <asm/debug.h>
24#include <asm/ebcdic.h>
25#include <asm/sysinfo.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020026#include <asm/pgtable.h>
Claudio Imbrenda190df4a2016-08-04 17:54:42 +020027#include <asm/page-states.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020028#include <asm/pgalloc.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010029#include <asm/gmap.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020030#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010031#include <asm/ptrace.h>
32#include <asm/compat.h>
David Hildenbranda7e19ab2016-05-10 09:50:21 +020033#include <asm/sclp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010034#include "gaccess.h"
35#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020036#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010037
Fan Zhang80cd8762016-08-15 04:53:22 +020038static int handle_ri(struct kvm_vcpu *vcpu)
39{
40 if (test_kvm_facility(vcpu->kvm, 64)) {
Christian Borntraeger4d5f2c02017-02-09 17:15:41 +010041 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
David Hildenbrand0c9d8682017-03-13 11:48:28 +010042 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
Fan Zhang80cd8762016-08-15 04:53:22 +020043 kvm_s390_retry_instr(vcpu);
44 return 0;
45 } else
46 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
47}
48
49int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
50{
51 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
52 return handle_ri(vcpu);
53 else
54 return -EOPNOTSUPP;
55}
56
Fan Zhang4e0b1ab2016-11-29 07:17:55 +010057static int handle_gs(struct kvm_vcpu *vcpu)
58{
59 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 Hildenbrand25ed1672015-05-12 09:49:14 +020087 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +010088 u8 ar;
David Hildenbrand25ed1672015-05-12 09:49:14 +020089 u64 op2, val;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020090
91 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
92 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
93
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030094 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020095 if (op2 & 7) /* Operand must be on a doubleword boundary */
96 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030097 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010098 if (rc)
99 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200100
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200101 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
David Hildenbrand25ed1672015-05-12 09:49:14 +0200102 kvm_s390_set_tod_clock(vcpu->kvm, val);
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200103
104 kvm_s390_set_psw_cc(vcpu, 0);
105 return 0;
106}
107
Christian Borntraeger453423d2008-03-25 18:47:29 +0100108static int handle_set_prefix(struct kvm_vcpu *vcpu)
109{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100110 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +0100111 u32 address;
112 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100113 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100114
115 vcpu->stat.instruction_spx++;
116
Thomas Huth5087dfa2013-06-20 17:22:01 +0200117 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
118 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
119
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300120 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100121
122 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100123 if (operand2 & 3)
124 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100125
126 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300127 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstens665170c2014-01-01 16:47:12 +0100128 if (rc)
129 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100130
Heiko Carstens665170c2014-01-01 16:47:12 +0100131 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100132
Heiko Carstens665170c2014-01-01 16:47:12 +0100133 /*
134 * Make sure the new value is valid memory. We only need to check the
135 * first page, since address is 8k aligned and memory pieces are always
136 * at least 1MB aligned and have at least a size of 1MB.
137 */
138 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100139 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100140
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100141 kvm_s390_set_prefix(vcpu, address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200142 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100143 return 0;
144}
145
146static int handle_store_prefix(struct kvm_vcpu *vcpu)
147{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100148 u64 operand2;
149 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100150 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100151 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100152
153 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100154
Thomas Huth5087dfa2013-06-20 17:22:01 +0200155 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
156 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
157
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300158 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100159
160 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100161 if (operand2 & 3)
162 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100163
Michael Muellerfda902c2014-05-13 16:58:30 +0200164 address = kvm_s390_get_prefix(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100165
166 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300167 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100168 if (rc)
169 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100170
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200171 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200172 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100173 return 0;
174}
175
176static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
177{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100178 u16 vcpu_id = vcpu->vcpu_id;
179 u64 ga;
180 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100181 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100182
183 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100184
Thomas Huth5087dfa2013-06-20 17:22:01 +0200185 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
186 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
187
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300188 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100189
Heiko Carstens8b96de02014-01-01 16:53:27 +0100190 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100191 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100192
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300193 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
Heiko Carstens8b96de02014-01-01 16:53:27 +0100194 if (rc)
195 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100196
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200197 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
Heiko Carstens8b96de02014-01-01 16:53:27 +0100198 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100199 return 0;
200}
201
Farhan Ali730cd632017-02-24 16:12:56 -0500202int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu)
Dominik Dingel693ffc02014-01-14 18:11:14 +0100203{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200204 int rc = 0;
Farhan Ali730cd632017-02-24 16:12:56 -0500205 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200206
207 trace_kvm_s390_skey_related_inst(vcpu);
Farhan Ali730cd632017-02-24 16:12:56 -0500208 if (!(sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)) &&
209 !(atomic_read(&sie_block->cpuflags) & CPUSTAT_KSS))
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200210 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100211
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200212 rc = s390_enable_skey();
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200213 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
Farhan Ali730cd632017-02-24 16:12:56 -0500214 if (!rc) {
215 if (atomic_read(&sie_block->cpuflags) & CPUSTAT_KSS)
216 atomic_andnot(CPUSTAT_KSS, &sie_block->cpuflags);
217 else
218 sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE |
219 ICTL_RRBE);
220 }
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200221 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100222}
223
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200224static int try_handle_skey(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100225{
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200226 int rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100227
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200228 vcpu->stat.instruction_storage_key++;
Farhan Ali730cd632017-02-24 16:12:56 -0500229 rc = kvm_s390_skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200230 if (rc)
231 return rc;
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200232 if (sclp.has_skey) {
233 /* with storage-key facility, SIE interprets it for us */
234 kvm_s390_retry_instr(vcpu);
235 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
236 return -EAGAIN;
237 }
Thomas Huth5087dfa2013-06-20 17:22:01 +0200238 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
239 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
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
250 rc = try_handle_skey(vcpu);
251 if (rc)
252 return rc != -EAGAIN ? rc : 0;
253
254 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
255
256 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
257 addr = kvm_s390_logical_to_effective(vcpu, addr);
258 addr = kvm_s390_real_to_abs(vcpu, addr);
259 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
260 if (kvm_is_error_hva(addr))
261 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
262
263 down_read(&current->mm->mmap_sem);
264 rc = get_guest_storage_key(current->mm, addr, &key);
265 up_read(&current->mm->mmap_sem);
266 if (rc)
267 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
268 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
269 vcpu->run->s.regs.gprs[reg1] |= key;
270 return 0;
271}
272
273static int handle_rrbe(struct kvm_vcpu *vcpu)
274{
275 unsigned long addr;
276 int reg1, reg2;
277 int rc;
278
279 rc = try_handle_skey(vcpu);
280 if (rc)
281 return rc != -EAGAIN ? rc : 0;
282
283 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
284
285 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
286 addr = kvm_s390_logical_to_effective(vcpu, addr);
287 addr = kvm_s390_real_to_abs(vcpu, addr);
288 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
289 if (kvm_is_error_hva(addr))
290 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
291
292 down_read(&current->mm->mmap_sem);
293 rc = reset_guest_reference_bit(current->mm, addr);
294 up_read(&current->mm->mmap_sem);
295 if (rc < 0)
296 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
297
298 kvm_s390_set_psw_cc(vcpu, rc);
299 return 0;
300}
301
302#define SSKE_NQ 0x8
303#define SSKE_MR 0x4
304#define SSKE_MC 0x2
305#define SSKE_MB 0x1
306static int handle_sske(struct kvm_vcpu *vcpu)
307{
308 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
309 unsigned long start, end;
310 unsigned char key, oldkey;
311 int reg1, reg2;
312 int rc;
313
314 rc = try_handle_skey(vcpu);
315 if (rc)
316 return rc != -EAGAIN ? rc : 0;
317
318 if (!test_kvm_facility(vcpu->kvm, 8))
319 m3 &= ~SSKE_MB;
320 if (!test_kvm_facility(vcpu->kvm, 10))
321 m3 &= ~(SSKE_MC | SSKE_MR);
322 if (!test_kvm_facility(vcpu->kvm, 14))
323 m3 &= ~SSKE_NQ;
324
325 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
326
327 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
328 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
329 start = kvm_s390_logical_to_effective(vcpu, start);
330 if (m3 & SSKE_MB) {
331 /* start already designates an absolute address */
332 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
333 } else {
334 start = kvm_s390_real_to_abs(vcpu, start);
335 end = start + PAGE_SIZE;
336 }
337
338 while (start != end) {
339 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
340
341 if (kvm_is_error_hva(addr))
342 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
343
344 down_read(&current->mm->mmap_sem);
345 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
346 m3 & SSKE_NQ, m3 & SSKE_MR,
347 m3 & SSKE_MC);
348 up_read(&current->mm->mmap_sem);
349 if (rc < 0)
350 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
351 start += PAGE_SIZE;
Heiko Carstens0b925152017-01-02 08:51:02 +0100352 }
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200353
354 if (m3 & (SSKE_MC | SSKE_MR)) {
355 if (m3 & SSKE_MB) {
356 /* skey in reg1 is unpredictable */
357 kvm_s390_set_psw_cc(vcpu, 3);
358 } else {
359 kvm_s390_set_psw_cc(vcpu, rc);
360 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
361 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
362 }
363 }
364 if (m3 & SSKE_MB) {
365 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT)
366 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
367 else
368 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
369 end = kvm_s390_logical_to_effective(vcpu, end);
370 vcpu->run->s.regs.gprs[reg2] |= end;
371 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100372 return 0;
373}
374
Heiko Carstens8a2422342014-01-10 14:33:28 +0100375static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
376{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100377 vcpu->stat.instruction_ipte_interlock++;
Thomas Huth04b41ac2014-11-12 17:13:29 +0100378 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100379 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
380 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100381 kvm_s390_retry_instr(vcpu);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100382 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
383 return 0;
384}
385
Thomas Huthaca84242013-09-12 10:33:48 +0200386static int handle_test_block(struct kvm_vcpu *vcpu)
387{
Thomas Huthaca84242013-09-12 10:33:48 +0200388 gpa_t addr;
389 int reg2;
390
391 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
392 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
393
394 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
395 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100396 addr = kvm_s390_logical_to_effective(vcpu, addr);
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300397 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
Thomas Huthe45efa22014-03-07 12:14:23 +0100398 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200399 addr = kvm_s390_real_to_abs(vcpu, addr);
400
Heiko Carstensef23e772014-01-01 16:53:49 +0100401 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200402 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
403 /*
404 * We don't expect errors on modern systems, and do not care
405 * about storage keys (yet), so let's just clear the page.
406 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100407 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200408 return -EFAULT;
409 kvm_s390_set_psw_cc(vcpu, 0);
410 vcpu->run->s.regs.gprs[0] = 0;
411 return 0;
412}
413
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100414static int handle_tpi(struct kvm_vcpu *vcpu)
415{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100416 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100417 unsigned long len;
418 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100419 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100420 u64 addr;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100421 u8 ar;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100422
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300423 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100424 if (addr & 3)
425 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100426
Thomas Huthf0926692013-10-09 14:15:54 +0200427 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100428 if (!inti) {
429 kvm_s390_set_psw_cc(vcpu, 0);
430 return 0;
431 }
432
Heiko Carstens4799b552014-01-01 16:55:48 +0100433 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
434 tpi_data[1] = inti->io.io_int_parm;
435 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100436 if (addr) {
437 /*
438 * Store the two-word I/O interruption code into the
439 * provided area.
440 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100441 len = sizeof(tpi_data) - 4;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300442 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100443 if (rc) {
444 rc = kvm_s390_inject_prog_cond(vcpu, rc);
445 goto reinject_interrupt;
446 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100447 } else {
448 /*
449 * Store the three-word I/O interruption code into
450 * the appropriate lowcore area.
451 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100452 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100453 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
454 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100455 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100456 goto reinject_interrupt;
457 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100458 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100459
460 /* irq was successfully handed to the guest */
461 kfree(inti);
462 kvm_s390_set_psw_cc(vcpu, 1);
463 return 0;
464reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100465 /*
466 * If we encounter a problem storing the interruption code, the
467 * instruction is suppressed from the guest's view: reinject the
468 * interrupt.
469 */
David Hildenbrand15462e32015-02-04 15:59:11 +0100470 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
471 kfree(inti);
472 rc = -EFAULT;
473 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100474 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100475 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100476}
477
478static int handle_tsch(struct kvm_vcpu *vcpu)
479{
Jens Freimann6d3da242013-07-03 15:18:35 +0200480 struct kvm_s390_interrupt_info *inti = NULL;
481 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100482
Jens Freimann6d3da242013-07-03 15:18:35 +0200483 /* a valid schid has at least one bit set */
484 if (vcpu->run->s.regs.gprs[1])
485 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
486 vcpu->run->s.regs.gprs[1]);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100487
488 /*
489 * Prepare exit to userspace.
490 * We indicate whether we dequeued a pending I/O interrupt
491 * so that userspace can re-inject it if the instruction gets
492 * a program check. While this may re-order the pending I/O
493 * interrupts, this is no problem since the priority is kept
494 * intact.
495 */
496 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
497 vcpu->run->s390_tsch.dequeued = !!inti;
498 if (inti) {
499 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
500 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
501 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
502 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
503 }
504 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
505 kfree(inti);
506 return -EREMOTE;
507}
508
Cornelia Huckf379aae2012-12-20 15:32:10 +0100509static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100510{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100511 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100512
Thomas Huth5087dfa2013-06-20 17:22:01 +0200513 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
514 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
515
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100516 if (vcpu->kvm->arch.css_support) {
517 /*
518 * Most I/O instructions will be handled by userspace.
519 * Exceptions are tpi and the interrupt portion of tsch.
520 */
521 if (vcpu->arch.sie_block->ipa == 0xb236)
522 return handle_tpi(vcpu);
523 if (vcpu->arch.sie_block->ipa == 0xb235)
524 return handle_tsch(vcpu);
525 /* Handle in userspace. */
526 return -EOPNOTSUPP;
527 } else {
528 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100529 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100530 * I/O instructions.
531 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200532 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100533 return 0;
534 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100535}
536
Christian Borntraeger453423d2008-03-25 18:47:29 +0100537static int handle_stfl(struct kvm_vcpu *vcpu)
538{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100539 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100540 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100541
542 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200543
544 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
545 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
546
Michael Mueller9d8d5782015-02-02 15:42:51 +0100547 /*
548 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
549 * into a u32 memory representation. They will remain bits 0-31.
550 */
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100551 fac = *vcpu->kvm->arch.model.fac_list >> 32;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100552 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100553 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100554 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100555 return rc;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200556 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100557 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100558 return 0;
559}
560
Cornelia Huck48a3e952012-12-20 15:32:09 +0100561#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
562#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100563#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100564#define PSW_ADDR_31 0x000000007fffffffUL
565
Thomas Hutha3fb5772014-04-17 09:10:40 +0200566int is_valid_psw(psw_t *psw)
567{
Heiko Carstens3736b872013-03-25 17:22:52 +0100568 if (psw->mask & PSW_MASK_UNASSIGNED)
569 return 0;
570 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
571 if (psw->addr & ~PSW_ADDR_31)
572 return 0;
573 }
574 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
575 return 0;
576 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
577 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200578 if (psw->addr & 1)
579 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100580 return 1;
581}
582
Cornelia Huck48a3e952012-12-20 15:32:09 +0100583int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
584{
Heiko Carstens3736b872013-03-25 17:22:52 +0100585 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100586 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100587 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100588 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100589 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100590
Heiko Carstens3736b872013-03-25 17:22:52 +0100591 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200592 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
593
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300594 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100595 if (addr & 7)
596 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100597
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300598 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100599 if (rc)
600 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100601 if (!(new_psw.mask & PSW32_MASK_BASE))
602 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100603 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
604 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
605 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
606 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100607 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100608 return 0;
609}
610
611static int handle_lpswe(struct kvm_vcpu *vcpu)
612{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100613 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100614 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100615 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100616 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100617
Thomas Huth5087dfa2013-06-20 17:22:01 +0200618 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
619 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
620
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300621 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100622 if (addr & 7)
623 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300624 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100625 if (rc)
626 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100627 vcpu->arch.sie_block->gpsw = new_psw;
628 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100629 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100630 return 0;
631}
632
Christian Borntraeger453423d2008-03-25 18:47:29 +0100633static int handle_stidp(struct kvm_vcpu *vcpu)
634{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200635 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100636 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100637 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100638 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100639
640 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100641
Thomas Huth5087dfa2013-06-20 17:22:01 +0200642 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
643 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
644
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300645 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100646
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100647 if (operand2 & 7)
648 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100649
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300650 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
Heiko Carstens7d777d72014-01-01 16:58:16 +0100651 if (rc)
652 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100653
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200654 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100655 return 0;
656}
657
658static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
659{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100660 int cpus = 0;
661 int n;
662
Jens Freimannff520a62014-02-24 10:11:41 +0100663 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100664
665 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200666 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100667 mem->count = 0;
668 if (mem->count < 8)
669 mem->count++;
670 for (n = mem->count - 1; n > 0 ; n--)
671 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
672
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100673 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100674 mem->vm[0].cpus_total = cpus;
675 mem->vm[0].cpus_configured = cpus;
676 mem->vm[0].cpus_standby = 0;
677 mem->vm[0].cpus_reserved = 0;
678 mem->vm[0].caf = 1000;
679 memcpy(mem->vm[0].name, "KVMguest", 8);
680 ASCEBC(mem->vm[0].name, 8);
681 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
682 ASCEBC(mem->vm[0].cpi, 16);
683}
684
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100685static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100686 u8 fc, u8 sel1, u16 sel2)
687{
688 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
689 vcpu->run->s390_stsi.addr = addr;
690 vcpu->run->s390_stsi.ar = ar;
691 vcpu->run->s390_stsi.fc = fc;
692 vcpu->run->s390_stsi.sel1 = sel1;
693 vcpu->run->s390_stsi.sel2 = sel2;
694}
695
Christian Borntraeger453423d2008-03-25 18:47:29 +0100696static int handle_stsi(struct kvm_vcpu *vcpu)
697{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100698 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
699 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
700 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100701 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100702 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100703 int rc = 0;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100704 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100705
706 vcpu->stat.instruction_stsi++;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200707 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100708
Thomas Huth5087dfa2013-06-20 17:22:01 +0200709 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
710 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
711
Thomas Huth87d41fb2013-06-20 17:22:05 +0200712 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200713 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200714 return 0;
715 }
716
717 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
718 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
719 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
720
721 if (fc == 0) {
722 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200723 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200724 return 0;
725 }
726
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300727 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100728
Thomas Huth87d41fb2013-06-20 17:22:05 +0200729 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100730 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
731
732 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100733 case 1: /* same handling for 1 and 2 */
734 case 2:
735 mem = get_zeroed_page(GFP_KERNEL);
736 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100737 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200738 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100739 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100740 break;
741 case 3:
742 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100743 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100744 mem = get_zeroed_page(GFP_KERNEL);
745 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100746 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100747 handle_stsi_3_2_2(vcpu, (void *) mem);
748 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100749 }
750
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300751 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100752 if (rc) {
753 rc = kvm_s390_inject_prog_cond(vcpu, rc);
754 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100755 }
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100756 if (vcpu->kvm->arch.user_stsi) {
757 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
758 rc = -EREMOTE;
759 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200760 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100761 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200762 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100763 vcpu->run->s.regs.gprs[0] = 0;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100764 return rc;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100765out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200766 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100767out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100768 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100769 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100770}
771
Cornelia Huckf379aae2012-12-20 15:32:10 +0100772static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100773 [0x02] = handle_stidp,
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200774 [0x04] = handle_set_clock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100775 [0x10] = handle_set_prefix,
776 [0x11] = handle_store_prefix,
777 [0x12] = handle_store_cpu_address,
David Hildenbranda3508fb2015-07-08 13:19:48 +0200778 [0x14] = kvm_s390_handle_vsie,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100779 [0x21] = handle_ipte_interlock,
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200780 [0x29] = handle_iske,
781 [0x2a] = handle_rrbe,
782 [0x2b] = handle_sske,
Thomas Huthaca84242013-09-12 10:33:48 +0200783 [0x2c] = handle_test_block,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100784 [0x30] = handle_io_inst,
785 [0x31] = handle_io_inst,
786 [0x32] = handle_io_inst,
787 [0x33] = handle_io_inst,
788 [0x34] = handle_io_inst,
789 [0x35] = handle_io_inst,
790 [0x36] = handle_io_inst,
791 [0x37] = handle_io_inst,
792 [0x38] = handle_io_inst,
793 [0x39] = handle_io_inst,
794 [0x3a] = handle_io_inst,
795 [0x3b] = handle_io_inst,
796 [0x3c] = handle_io_inst,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100797 [0x50] = handle_ipte_interlock,
Christian Borntraegerc0a6bfd2017-02-27 21:14:47 +0100798 [0x56] = handle_sthyi,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100799 [0x5f] = handle_io_inst,
800 [0x74] = handle_io_inst,
801 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100802 [0x7d] = handle_stsi,
803 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100804 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100805};
806
Christian Borntraeger70455a32009-01-22 10:28:29 +0100807int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100808{
809 intercept_handler_t handler;
810
Christian Borntraeger70455a32009-01-22 10:28:29 +0100811 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200812 * A lot of B2 instructions are priviledged. Here we check for
813 * the privileged ones, that we can handle in the kernel.
814 * Anything else goes to userspace.
815 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100816 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200817 if (handler)
818 return handler(vcpu);
819
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100820 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100821}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200822
Cornelia Huck48a3e952012-12-20 15:32:09 +0100823static int handle_epsw(struct kvm_vcpu *vcpu)
824{
825 int reg1, reg2;
826
Thomas Huthaeb87c32013-06-12 13:54:57 +0200827 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100828
829 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200830 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100831 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
832 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200833 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100834 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200835 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100836 }
837 return 0;
838}
839
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200840#define PFMF_RESERVED 0xfffc0101UL
841#define PFMF_SK 0x00020000UL
842#define PFMF_CF 0x00010000UL
843#define PFMF_UI 0x00008000UL
844#define PFMF_FSC 0x00007000UL
845#define PFMF_NQ 0x00000800UL
846#define PFMF_MR 0x00000400UL
847#define PFMF_MC 0x00000200UL
848#define PFMF_KEY 0x000000feUL
849
850static int handle_pfmf(struct kvm_vcpu *vcpu)
851{
David Hildenbrand1824c722016-05-10 09:43:11 +0200852 bool mr = false, mc = false, nq;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200853 int reg1, reg2;
854 unsigned long start, end;
David Hildenbrand1824c722016-05-10 09:43:11 +0200855 unsigned char key;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200856
857 vcpu->stat.instruction_pfmf++;
858
859 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
860
Heiko Carstens03c02802015-11-13 13:31:58 +0100861 if (!test_kvm_facility(vcpu->kvm, 8))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200862 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
863
864 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200865 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200866
867 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
868 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
869
David Hildenbrandedc5b052016-03-04 11:08:09 +0100870 /* Only provide non-quiescing support if enabled for the guest */
871 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
872 !test_kvm_facility(vcpu->kvm, 14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200873 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
874
David Hildenbrand1824c722016-05-10 09:43:11 +0200875 /* Only provide conditional-SSKE support if enabled for the guest */
876 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
877 test_kvm_facility(vcpu->kvm, 10)) {
878 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
879 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
880 }
881
882 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
883 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200884 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100885 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200886
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200887 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
888 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
889 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
890 }
891
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200892 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
893 case 0x00000000:
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200894 /* only 4k frames specify a real address */
895 start = kvm_s390_real_to_abs(vcpu, start);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200896 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
897 break;
898 case 0x00001000:
899 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
900 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200901 case 0x00002000:
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100902 /* only support 2G frame size if EDAT2 is available and we are
903 not in 24-bit addressing mode */
904 if (!test_kvm_facility(vcpu->kvm, 78) ||
905 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
906 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200907 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100908 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200909 default:
910 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
911 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100912
David Hildenbrand695be0e2016-05-12 14:07:05 +0200913 while (start != end) {
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200914 unsigned long useraddr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200915
Thomas Huthfb34c6032013-09-09 17:58:38 +0200916 /* Translate guest address to host address */
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200917 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
Thomas Huthfb34c6032013-09-09 17:58:38 +0200918 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200919 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
920
921 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
922 if (clear_user((void __user *)useraddr, PAGE_SIZE))
923 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
924 }
925
926 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Farhan Ali730cd632017-02-24 16:12:56 -0500927 int rc = kvm_s390_skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200928
929 if (rc)
930 return rc;
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100931 down_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200932 rc = cond_set_guest_storage_key(current->mm, useraddr,
933 key, NULL, nq, mr, mc);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100934 up_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200935 if (rc < 0)
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200936 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
937 }
938
939 start += PAGE_SIZE;
940 }
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200941 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
942 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
943 vcpu->run->s.regs.gprs[reg2] = end;
944 } else {
945 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
946 end = kvm_s390_logical_to_effective(vcpu, end);
947 vcpu->run->s.regs.gprs[reg2] |= end;
948 }
949 }
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200950 return 0;
951}
952
Claudio Imbrenda190df4a2016-08-04 17:54:42 +0200953static inline int do_essa(struct kvm_vcpu *vcpu, const int orc)
954{
955 struct kvm_s390_migration_state *ms = vcpu->kvm->arch.migration_state;
956 int r1, r2, nappended, entries;
957 unsigned long gfn, hva, res, pgstev, ptev;
958 unsigned long *cbrlo;
959
960 /*
961 * We don't need to set SD.FPF.SK to 1 here, because if we have a
962 * machine check here we either handle it or crash
963 */
964
965 kvm_s390_get_regs_rre(vcpu, &r1, &r2);
966 gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
967 hva = gfn_to_hva(vcpu->kvm, gfn);
968 entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
969
970 if (kvm_is_error_hva(hva))
971 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
972
973 nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
974 if (nappended < 0) {
975 res = orc ? 0x10 : 0;
976 vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
977 return 0;
978 }
979 res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
980 /*
981 * Set the block-content state part of the result. 0 means resident, so
982 * nothing to do if the page is valid. 2 is for preserved pages
983 * (non-present and non-zero), and 3 for zero pages (non-present and
984 * zero).
985 */
986 if (ptev & _PAGE_INVALID) {
987 res |= 2;
988 if (pgstev & _PGSTE_GPS_ZERO)
989 res |= 1;
990 }
991 vcpu->run->s.regs.gprs[r1] = res;
992 /*
993 * It is possible that all the normal 511 slots were full, in which case
994 * we will now write in the 512th slot, which is reserved for host use.
995 * In both cases we let the normal essa handling code process all the
996 * slots, including the reserved one, if needed.
997 */
998 if (nappended > 0) {
999 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
1000 cbrlo[entries] = gfn << PAGE_SHIFT;
1001 }
1002
1003 if (orc) {
1004 /* increment only if we are really flipping the bit to 1 */
1005 if (!test_and_set_bit(gfn, ms->pgste_bitmap))
1006 atomic64_inc(&ms->dirty_pages);
1007 }
1008
1009 return nappended;
1010}
1011
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001012static int handle_essa(struct kvm_vcpu *vcpu)
1013{
1014 /* entries expected to be 1FF */
1015 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
David Hildenbrand4a5e7e32016-04-12 13:32:25 +02001016 unsigned long *cbrlo;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001017 struct gmap *gmap;
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001018 int i, orc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001019
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001020 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001021 gmap = vcpu->arch.gmap;
1022 vcpu->stat.instruction_essa++;
Dominik Dingele6db1d62015-05-07 15:41:57 +02001023 if (!vcpu->kvm->arch.use_cmma)
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001024 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1025
1026 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1027 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001028 /* Check for invalid operation request code */
1029 orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
1030 if (orc > ESSA_MAX)
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001031 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1032
Claudio Imbrenda190df4a2016-08-04 17:54:42 +02001033 if (likely(!vcpu->kvm->arch.migration_state)) {
1034 /*
1035 * CMMA is enabled in the KVM settings, but is disabled in
1036 * the SIE block and in the mm_context, and we are not doing
1037 * a migration. Enable CMMA in the mm_context.
1038 * Since we need to take a write lock to write to the context
1039 * to avoid races with storage keys handling, we check if the
1040 * value really needs to be written to; if the value is
1041 * already correct, we do nothing and avoid the lock.
1042 */
1043 if (vcpu->kvm->mm->context.use_cmma == 0) {
1044 down_write(&vcpu->kvm->mm->mmap_sem);
1045 vcpu->kvm->mm->context.use_cmma = 1;
1046 up_write(&vcpu->kvm->mm->mmap_sem);
1047 }
1048 /*
1049 * If we are here, we are supposed to have CMMA enabled in
1050 * the SIE block. Enabling CMMA works on a per-CPU basis,
1051 * while the context use_cmma flag is per process.
1052 * It's possible that the context flag is enabled and the
1053 * SIE flag is not, so we set the flag always; if it was
1054 * already set, nothing changes, otherwise we enable it
1055 * on this CPU too.
1056 */
1057 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
1058 /* Retry the ESSA instruction */
1059 kvm_s390_retry_instr(vcpu);
1060 } else {
1061 /* Account for the possible extra cbrl entry */
1062 i = do_essa(vcpu, orc);
1063 if (i < 0)
1064 return i;
1065 entries += i;
1066 }
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001067 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
1068 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
1069 down_read(&gmap->mm->mmap_sem);
David Hildenbrand4a5e7e32016-04-12 13:32:25 +02001070 for (i = 0; i < entries; ++i)
1071 __gmap_zap(gmap, cbrlo[i]);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001072 up_read(&gmap->mm->mmap_sem);
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001073 return 0;
1074}
1075
Cornelia Huck48a3e952012-12-20 15:32:09 +01001076static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +01001077 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +01001078 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +01001079 [0x8e] = handle_ipte_interlock,
1080 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001081 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001082 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +01001083};
1084
1085int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
1086{
1087 intercept_handler_t handler;
1088
1089 /* This is handled just as for the B2 instructions. */
1090 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +02001091 if (handler)
1092 return handler(vcpu);
1093
Cornelia Huck48a3e952012-12-20 15:32:09 +01001094 return -EOPNOTSUPP;
1095}
1096
Thomas Huth953ed882013-06-20 17:22:04 +02001097int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1098{
1099 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1100 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001101 int reg, rc, nr_regs;
1102 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001103 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001104 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001105
1106 vcpu->stat.instruction_lctl++;
1107
1108 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1109 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1110
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001111 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001112
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001113 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +02001114 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1115
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001116 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001117 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001118
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001119 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001120 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001121 if (rc)
1122 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +02001123 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001124 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001125 do {
Thomas Huth953ed882013-06-20 17:22:04 +02001126 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001127 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001128 if (reg == reg3)
1129 break;
1130 reg = (reg + 1) % 16;
1131 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001132 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001133 return 0;
1134}
1135
David Hildenbrandaba07502014-01-23 10:47:13 +01001136int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1137{
1138 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1139 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001140 int reg, rc, nr_regs;
1141 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +01001142 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001143 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001144
1145 vcpu->stat.instruction_stctl++;
1146
1147 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1148 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1149
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001150 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001151
1152 if (ga & 3)
1153 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1154
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001155 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001156 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1157
1158 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001159 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001160 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001161 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001162 if (reg == reg3)
1163 break;
1164 reg = (reg + 1) % 16;
1165 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001166 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001167 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001168}
1169
Thomas Huth953ed882013-06-20 17:22:04 +02001170static int handle_lctlg(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 u64 ctl_array[16];
1176 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001177 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001178
1179 vcpu->stat.instruction_lctlg++;
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_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001185
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001186 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +02001187 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1188
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001189 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001190 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001191
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001192 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001193 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001194 if (rc)
1195 return kvm_s390_inject_prog_cond(vcpu, rc);
1196 reg = reg1;
1197 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001198 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001199 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001200 if (reg == reg3)
1201 break;
1202 reg = (reg + 1) % 16;
1203 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001204 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001205 return 0;
1206}
1207
David Hildenbrandaba07502014-01-23 10:47:13 +01001208static int handle_stctg(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;
David Hildenbrandaba07502014-01-23 10:47:13 +01001216
1217 vcpu->stat.instruction_stctg++;
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);
David Hildenbrandaba07502014-01-23 10:47:13 +01001223
1224 if (ga & 7)
1225 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1226
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001227 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001228 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1229
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001230 reg = reg1;
1231 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001232 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001233 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001234 if (reg == reg3)
1235 break;
1236 reg = (reg + 1) % 16;
1237 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001238 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001239 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001240}
1241
Cornelia Huckf379aae2012-12-20 15:32:10 +01001242static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +02001243 [0x2f] = handle_lctlg,
David Hildenbrandaba07502014-01-23 10:47:13 +01001244 [0x25] = handle_stctg,
Fan Zhang80cd8762016-08-15 04:53:22 +02001245 [0x60] = handle_ri,
1246 [0x61] = handle_ri,
1247 [0x62] = handle_ri,
Cornelia Huckf379aae2012-12-20 15:32:10 +01001248};
1249
Thomas Huth953ed882013-06-20 17:22:04 +02001250int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +01001251{
1252 intercept_handler_t handler;
1253
Cornelia Huckf379aae2012-12-20 15:32:10 +01001254 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1255 if (handler)
1256 return handler(vcpu);
1257 return -EOPNOTSUPP;
1258}
1259
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001260static int handle_tprot(struct kvm_vcpu *vcpu)
1261{
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001262 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +01001263 unsigned long hva, gpa;
1264 int ret = 0, cc = 0;
1265 bool writable;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001266 u8 ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001267
1268 vcpu->stat.instruction_tprot++;
1269
Thomas Huthf9f6bbc2013-06-20 17:22:00 +02001270 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1271 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1272
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001273 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001274
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001275 /* we only handle the Linux memory detection case:
1276 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001277 * everything else goes to userspace. */
1278 if (address2 & 0xf0)
1279 return -EOPNOTSUPP;
1280 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +01001281 ipte_lock(vcpu);
David Hildenbrand92c96322015-11-16 15:42:11 +01001282 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
Thomas Hutha0465f92014-02-04 14:48:07 +01001283 if (ret == PGM_PROTECTION) {
1284 /* Write protected? Try again with read-only... */
1285 cc = 1;
David Hildenbrand92c96322015-11-16 15:42:11 +01001286 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1287 GACC_FETCH);
Thomas Hutha0465f92014-02-04 14:48:07 +01001288 }
1289 if (ret) {
1290 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1291 ret = kvm_s390_inject_program_int(vcpu, ret);
1292 } else if (ret > 0) {
1293 /* Translation not available */
1294 kvm_s390_set_psw_cc(vcpu, 3);
1295 ret = 0;
1296 }
1297 goto out_unlock;
1298 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001299
Thomas Hutha0465f92014-02-04 14:48:07 +01001300 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1301 if (kvm_is_error_hva(hva)) {
1302 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1303 } else {
1304 if (!writable)
1305 cc = 1; /* Write not permitted ==> read-only */
1306 kvm_s390_set_psw_cc(vcpu, cc);
1307 /* Note: CC2 only occurs for storage keys (not supported yet) */
1308 }
1309out_unlock:
1310 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1311 ipte_unlock(vcpu);
1312 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001313}
1314
1315int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1316{
1317 /* For e5xx... instructions we only handle TPROT */
1318 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1319 return handle_tprot(vcpu);
1320 return -EOPNOTSUPP;
1321}
1322
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001323static int handle_sckpf(struct kvm_vcpu *vcpu)
1324{
1325 u32 value;
1326
1327 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001328 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001329
1330 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1331 return kvm_s390_inject_program_int(vcpu,
1332 PGM_SPECIFICATION);
1333
1334 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1335 vcpu->arch.sie_block->todpr = value;
1336
1337 return 0;
1338}
1339
David Hildenbrand9acc3172016-07-18 09:18:13 +02001340static int handle_ptff(struct kvm_vcpu *vcpu)
1341{
1342 /* we don't emulate any control instructions yet */
1343 kvm_s390_set_psw_cc(vcpu, 3);
1344 return 0;
1345}
1346
Cornelia Huck77975352012-12-20 15:32:06 +01001347static const intercept_handler_t x01_handlers[256] = {
David Hildenbrand9acc3172016-07-18 09:18:13 +02001348 [0x04] = handle_ptff,
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001349 [0x07] = handle_sckpf,
1350};
1351
1352int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1353{
1354 intercept_handler_t handler;
1355
1356 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1357 if (handler)
1358 return handler(vcpu);
1359 return -EOPNOTSUPP;
1360}