blob: e9dd7efc57b8f5088c098175c679737bf48b7796 [file] [log] [blame]
Christian Borntraeger453423d2008-03-25 18:47:29 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling privileged instructions
Christian Borntraeger453423d2008-03-25 18:47:29 +01003 *
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02004 * Copyright IBM Corp. 2008, 2013
Christian Borntraeger453423d2008-03-25 18:47:29 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 */
13
14#include <linux/kvm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010016#include <linux/errno.h>
Heiko Carstensb13b5dc2013-03-25 17:22:55 +010017#include <linux/compat.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010018#include <linux/mm_types.h>
19
Heiko Carstens7c959e82013-03-05 13:14:46 +010020#include <asm/asm-offsets.h>
Heiko Carstense769ece2013-07-26 15:04:01 +020021#include <asm/facility.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010022#include <asm/current.h>
23#include <asm/debug.h>
24#include <asm/ebcdic.h>
25#include <asm/sysinfo.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020026#include <asm/pgtable.h>
27#include <asm/pgalloc.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010028#include <asm/gmap.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020029#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010030#include <asm/ptrace.h>
31#include <asm/compat.h>
David Hildenbranda7e19ab2016-05-10 09:50:21 +020032#include <asm/sclp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010033#include "gaccess.h"
34#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020035#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010036
Fan Zhang80cd8762016-08-15 04:53:22 +020037static int handle_ri(struct kvm_vcpu *vcpu)
38{
39 if (test_kvm_facility(vcpu->kvm, 64)) {
Christian Borntraeger4d5f2c02017-02-09 17:15:41 +010040 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
David Hildenbrand0c9d8682017-03-13 11:48:28 +010041 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
Fan Zhang80cd8762016-08-15 04:53:22 +020042 kvm_s390_retry_instr(vcpu);
43 return 0;
44 } else
45 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
46}
47
48int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
49{
50 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
51 return handle_ri(vcpu);
52 else
53 return -EOPNOTSUPP;
54}
55
Fan Zhang4e0b1ab2016-11-29 07:17:55 +010056static int handle_gs(struct kvm_vcpu *vcpu)
57{
58 if (test_kvm_facility(vcpu->kvm, 133)) {
59 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
60 preempt_disable();
61 __ctl_set_bit(2, 4);
62 current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
63 restore_gs_cb(current->thread.gs_cb);
64 preempt_enable();
65 vcpu->arch.sie_block->ecb |= ECB_GS;
66 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
67 vcpu->arch.gs_enabled = 1;
68 kvm_s390_retry_instr(vcpu);
69 return 0;
70 } else
71 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
72}
73
74int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
75{
76 int code = vcpu->arch.sie_block->ipb & 0xff;
77
78 if (code == 0x49 || code == 0x4d)
79 return handle_gs(vcpu);
80 else
81 return -EOPNOTSUPP;
82}
Thomas Huth6a3f95a2013-09-12 10:33:49 +020083/* Handle SCK (SET CLOCK) interception */
84static int handle_set_clock(struct kvm_vcpu *vcpu)
85{
David Hildenbrand25ed1672015-05-12 09:49:14 +020086 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +010087 u8 ar;
David Hildenbrand25ed1672015-05-12 09:49:14 +020088 u64 op2, val;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020089
90 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
91 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
92
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030093 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020094 if (op2 & 7) /* Operand must be on a doubleword boundary */
95 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030096 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010097 if (rc)
98 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020099
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200100 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
David Hildenbrand25ed1672015-05-12 09:49:14 +0200101 kvm_s390_set_tod_clock(vcpu->kvm, val);
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200102
103 kvm_s390_set_psw_cc(vcpu, 0);
104 return 0;
105}
106
Christian Borntraeger453423d2008-03-25 18:47:29 +0100107static int handle_set_prefix(struct kvm_vcpu *vcpu)
108{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100109 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +0100110 u32 address;
111 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100112 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100113
114 vcpu->stat.instruction_spx++;
115
Thomas Huth5087dfa2013-06-20 17:22:01 +0200116 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
117 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
118
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300119 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100120
121 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100122 if (operand2 & 3)
123 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100124
125 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300126 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstens665170c2014-01-01 16:47:12 +0100127 if (rc)
128 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100129
Heiko Carstens665170c2014-01-01 16:47:12 +0100130 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100131
Heiko Carstens665170c2014-01-01 16:47:12 +0100132 /*
133 * Make sure the new value is valid memory. We only need to check the
134 * first page, since address is 8k aligned and memory pieces are always
135 * at least 1MB aligned and have at least a size of 1MB.
136 */
137 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100138 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100139
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100140 kvm_s390_set_prefix(vcpu, address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200141 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100142 return 0;
143}
144
145static int handle_store_prefix(struct kvm_vcpu *vcpu)
146{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100147 u64 operand2;
148 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100149 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100150 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100151
152 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100153
Thomas Huth5087dfa2013-06-20 17:22:01 +0200154 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
155 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
156
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300157 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100158
159 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100160 if (operand2 & 3)
161 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100162
Michael Muellerfda902c2014-05-13 16:58:30 +0200163 address = kvm_s390_get_prefix(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100164
165 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300166 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100167 if (rc)
168 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100169
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200170 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200171 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100172 return 0;
173}
174
175static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
176{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100177 u16 vcpu_id = vcpu->vcpu_id;
178 u64 ga;
179 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100180 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100181
182 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100183
Thomas Huth5087dfa2013-06-20 17:22:01 +0200184 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
185 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
186
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300187 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100188
Heiko Carstens8b96de02014-01-01 16:53:27 +0100189 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100190 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100191
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300192 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
Heiko Carstens8b96de02014-01-01 16:53:27 +0100193 if (rc)
194 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100195
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200196 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
Heiko Carstens8b96de02014-01-01 16:53:27 +0100197 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100198 return 0;
199}
200
Farhan Ali730cd632017-02-24 16:12:56 -0500201int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu)
Dominik Dingel693ffc02014-01-14 18:11:14 +0100202{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200203 int rc = 0;
Farhan Ali730cd632017-02-24 16:12:56 -0500204 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200205
206 trace_kvm_s390_skey_related_inst(vcpu);
Farhan Ali730cd632017-02-24 16:12:56 -0500207 if (!(sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)) &&
208 !(atomic_read(&sie_block->cpuflags) & CPUSTAT_KSS))
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200209 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100210
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200211 rc = s390_enable_skey();
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200212 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
Farhan Ali730cd632017-02-24 16:12:56 -0500213 if (!rc) {
214 if (atomic_read(&sie_block->cpuflags) & CPUSTAT_KSS)
215 atomic_andnot(CPUSTAT_KSS, &sie_block->cpuflags);
216 else
217 sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE |
218 ICTL_RRBE);
219 }
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200220 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100221}
222
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200223static int try_handle_skey(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100224{
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200225 int rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100226
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200227 vcpu->stat.instruction_storage_key++;
Farhan Ali730cd632017-02-24 16:12:56 -0500228 rc = kvm_s390_skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200229 if (rc)
230 return rc;
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200231 if (sclp.has_skey) {
232 /* with storage-key facility, SIE interprets it for us */
233 kvm_s390_retry_instr(vcpu);
234 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
235 return -EAGAIN;
236 }
Thomas Huth5087dfa2013-06-20 17:22:01 +0200237 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
238 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200239 return 0;
240}
Thomas Huth5087dfa2013-06-20 17:22:01 +0200241
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200242static int handle_iske(struct kvm_vcpu *vcpu)
243{
244 unsigned long addr;
245 unsigned char key;
246 int reg1, reg2;
247 int rc;
248
249 rc = try_handle_skey(vcpu);
250 if (rc)
251 return rc != -EAGAIN ? rc : 0;
252
253 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
254
255 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
256 addr = kvm_s390_logical_to_effective(vcpu, addr);
257 addr = kvm_s390_real_to_abs(vcpu, addr);
258 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
259 if (kvm_is_error_hva(addr))
260 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
261
262 down_read(&current->mm->mmap_sem);
263 rc = get_guest_storage_key(current->mm, addr, &key);
264 up_read(&current->mm->mmap_sem);
265 if (rc)
266 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
267 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
268 vcpu->run->s.regs.gprs[reg1] |= key;
269 return 0;
270}
271
272static int handle_rrbe(struct kvm_vcpu *vcpu)
273{
274 unsigned long addr;
275 int reg1, reg2;
276 int rc;
277
278 rc = try_handle_skey(vcpu);
279 if (rc)
280 return rc != -EAGAIN ? rc : 0;
281
282 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
283
284 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
285 addr = kvm_s390_logical_to_effective(vcpu, addr);
286 addr = kvm_s390_real_to_abs(vcpu, addr);
287 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
288 if (kvm_is_error_hva(addr))
289 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
290
291 down_read(&current->mm->mmap_sem);
292 rc = reset_guest_reference_bit(current->mm, addr);
293 up_read(&current->mm->mmap_sem);
294 if (rc < 0)
295 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
296
297 kvm_s390_set_psw_cc(vcpu, rc);
298 return 0;
299}
300
301#define SSKE_NQ 0x8
302#define SSKE_MR 0x4
303#define SSKE_MC 0x2
304#define SSKE_MB 0x1
305static int handle_sske(struct kvm_vcpu *vcpu)
306{
307 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
308 unsigned long start, end;
309 unsigned char key, oldkey;
310 int reg1, reg2;
311 int rc;
312
313 rc = try_handle_skey(vcpu);
314 if (rc)
315 return rc != -EAGAIN ? rc : 0;
316
317 if (!test_kvm_facility(vcpu->kvm, 8))
318 m3 &= ~SSKE_MB;
319 if (!test_kvm_facility(vcpu->kvm, 10))
320 m3 &= ~(SSKE_MC | SSKE_MR);
321 if (!test_kvm_facility(vcpu->kvm, 14))
322 m3 &= ~SSKE_NQ;
323
324 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
325
326 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
327 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
328 start = kvm_s390_logical_to_effective(vcpu, start);
329 if (m3 & SSKE_MB) {
330 /* start already designates an absolute address */
331 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
332 } else {
333 start = kvm_s390_real_to_abs(vcpu, start);
334 end = start + PAGE_SIZE;
335 }
336
337 while (start != end) {
338 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
339
340 if (kvm_is_error_hva(addr))
341 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
342
343 down_read(&current->mm->mmap_sem);
344 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
345 m3 & SSKE_NQ, m3 & SSKE_MR,
346 m3 & SSKE_MC);
347 up_read(&current->mm->mmap_sem);
348 if (rc < 0)
349 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
350 start += PAGE_SIZE;
Heiko Carstens0b925152017-01-02 08:51:02 +0100351 }
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200352
353 if (m3 & (SSKE_MC | SSKE_MR)) {
354 if (m3 & SSKE_MB) {
355 /* skey in reg1 is unpredictable */
356 kvm_s390_set_psw_cc(vcpu, 3);
357 } else {
358 kvm_s390_set_psw_cc(vcpu, rc);
359 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
360 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
361 }
362 }
363 if (m3 & SSKE_MB) {
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200364 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200365 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
366 else
367 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
368 end = kvm_s390_logical_to_effective(vcpu, end);
369 vcpu->run->s.regs.gprs[reg2] |= end;
370 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100371 return 0;
372}
373
Heiko Carstens8a2422342014-01-10 14:33:28 +0100374static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
375{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100376 vcpu->stat.instruction_ipte_interlock++;
Thomas Huth04b41ac2014-11-12 17:13:29 +0100377 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100378 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
379 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100380 kvm_s390_retry_instr(vcpu);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100381 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
382 return 0;
383}
384
Thomas Huthaca84242013-09-12 10:33:48 +0200385static int handle_test_block(struct kvm_vcpu *vcpu)
386{
Thomas Huthaca84242013-09-12 10:33:48 +0200387 gpa_t addr;
388 int reg2;
389
390 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
391 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
392
393 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
394 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100395 addr = kvm_s390_logical_to_effective(vcpu, addr);
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300396 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
Thomas Huthe45efa22014-03-07 12:14:23 +0100397 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200398 addr = kvm_s390_real_to_abs(vcpu, addr);
399
Heiko Carstensef23e772014-01-01 16:53:49 +0100400 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200401 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
402 /*
403 * We don't expect errors on modern systems, and do not care
404 * about storage keys (yet), so let's just clear the page.
405 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100406 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200407 return -EFAULT;
408 kvm_s390_set_psw_cc(vcpu, 0);
409 vcpu->run->s.regs.gprs[0] = 0;
410 return 0;
411}
412
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100413static int handle_tpi(struct kvm_vcpu *vcpu)
414{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100415 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100416 unsigned long len;
417 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100418 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100419 u64 addr;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100420 u8 ar;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100421
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300422 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100423 if (addr & 3)
424 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100425
Thomas Huthf0926692013-10-09 14:15:54 +0200426 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100427 if (!inti) {
428 kvm_s390_set_psw_cc(vcpu, 0);
429 return 0;
430 }
431
Heiko Carstens4799b552014-01-01 16:55:48 +0100432 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
433 tpi_data[1] = inti->io.io_int_parm;
434 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100435 if (addr) {
436 /*
437 * Store the two-word I/O interruption code into the
438 * provided area.
439 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100440 len = sizeof(tpi_data) - 4;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300441 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100442 if (rc) {
443 rc = kvm_s390_inject_prog_cond(vcpu, rc);
444 goto reinject_interrupt;
445 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100446 } else {
447 /*
448 * Store the three-word I/O interruption code into
449 * the appropriate lowcore area.
450 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100451 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100452 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
453 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100454 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100455 goto reinject_interrupt;
456 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100457 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100458
459 /* irq was successfully handed to the guest */
460 kfree(inti);
461 kvm_s390_set_psw_cc(vcpu, 1);
462 return 0;
463reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100464 /*
465 * If we encounter a problem storing the interruption code, the
466 * instruction is suppressed from the guest's view: reinject the
467 * interrupt.
468 */
David Hildenbrand15462e32015-02-04 15:59:11 +0100469 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
470 kfree(inti);
471 rc = -EFAULT;
472 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100473 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100474 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100475}
476
477static int handle_tsch(struct kvm_vcpu *vcpu)
478{
Jens Freimann6d3da242013-07-03 15:18:35 +0200479 struct kvm_s390_interrupt_info *inti = NULL;
480 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100481
Jens Freimann6d3da242013-07-03 15:18:35 +0200482 /* a valid schid has at least one bit set */
483 if (vcpu->run->s.regs.gprs[1])
484 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
485 vcpu->run->s.regs.gprs[1]);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100486
487 /*
488 * Prepare exit to userspace.
489 * We indicate whether we dequeued a pending I/O interrupt
490 * so that userspace can re-inject it if the instruction gets
491 * a program check. While this may re-order the pending I/O
492 * interrupts, this is no problem since the priority is kept
493 * intact.
494 */
495 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
496 vcpu->run->s390_tsch.dequeued = !!inti;
497 if (inti) {
498 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
499 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
500 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
501 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
502 }
503 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
504 kfree(inti);
505 return -EREMOTE;
506}
507
Cornelia Huckf379aae2012-12-20 15:32:10 +0100508static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100509{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100510 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100511
Thomas Huth5087dfa2013-06-20 17:22:01 +0200512 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
513 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
514
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100515 if (vcpu->kvm->arch.css_support) {
516 /*
517 * Most I/O instructions will be handled by userspace.
518 * Exceptions are tpi and the interrupt portion of tsch.
519 */
520 if (vcpu->arch.sie_block->ipa == 0xb236)
521 return handle_tpi(vcpu);
522 if (vcpu->arch.sie_block->ipa == 0xb235)
523 return handle_tsch(vcpu);
524 /* Handle in userspace. */
525 return -EOPNOTSUPP;
526 } else {
527 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100528 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100529 * I/O instructions.
530 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200531 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100532 return 0;
533 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100534}
535
Christian Borntraeger453423d2008-03-25 18:47:29 +0100536static int handle_stfl(struct kvm_vcpu *vcpu)
537{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100538 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100539 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100540
541 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200542
543 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
544 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
545
Michael Mueller9d8d5782015-02-02 15:42:51 +0100546 /*
547 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
548 * into a u32 memory representation. They will remain bits 0-31.
549 */
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100550 fac = *vcpu->kvm->arch.model.fac_list >> 32;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100551 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100552 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100553 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100554 return rc;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200555 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100556 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100557 return 0;
558}
559
Cornelia Huck48a3e952012-12-20 15:32:09 +0100560#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
561#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100562#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100563#define PSW_ADDR_31 0x000000007fffffffUL
564
Thomas Hutha3fb5772014-04-17 09:10:40 +0200565int is_valid_psw(psw_t *psw)
566{
Heiko Carstens3736b872013-03-25 17:22:52 +0100567 if (psw->mask & PSW_MASK_UNASSIGNED)
568 return 0;
569 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
570 if (psw->addr & ~PSW_ADDR_31)
571 return 0;
572 }
573 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
574 return 0;
575 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
576 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200577 if (psw->addr & 1)
578 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100579 return 1;
580}
581
Cornelia Huck48a3e952012-12-20 15:32:09 +0100582int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
583{
Heiko Carstens3736b872013-03-25 17:22:52 +0100584 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100585 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100586 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100587 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100588 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100589
Heiko Carstens3736b872013-03-25 17:22:52 +0100590 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200591 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
592
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300593 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100594 if (addr & 7)
595 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100596
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300597 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100598 if (rc)
599 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100600 if (!(new_psw.mask & PSW32_MASK_BASE))
601 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100602 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
603 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
604 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
605 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100606 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100607 return 0;
608}
609
610static int handle_lpswe(struct kvm_vcpu *vcpu)
611{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100612 psw_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
Thomas Huth5087dfa2013-06-20 17:22:01 +0200617 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
618 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
619
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300620 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100621 if (addr & 7)
622 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300623 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100624 if (rc)
625 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100626 vcpu->arch.sie_block->gpsw = new_psw;
627 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100628 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100629 return 0;
630}
631
Christian Borntraeger453423d2008-03-25 18:47:29 +0100632static int handle_stidp(struct kvm_vcpu *vcpu)
633{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200634 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100635 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100636 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100637 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100638
639 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100640
Thomas Huth5087dfa2013-06-20 17:22:01 +0200641 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
642 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
643
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300644 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100645
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100646 if (operand2 & 7)
647 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100648
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300649 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
Heiko Carstens7d777d72014-01-01 16:58:16 +0100650 if (rc)
651 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100652
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200653 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100654 return 0;
655}
656
657static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
658{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100659 int cpus = 0;
660 int n;
661
Jens Freimannff520a62014-02-24 10:11:41 +0100662 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100663
664 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200665 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100666 mem->count = 0;
667 if (mem->count < 8)
668 mem->count++;
669 for (n = mem->count - 1; n > 0 ; n--)
670 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
671
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100672 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100673 mem->vm[0].cpus_total = cpus;
674 mem->vm[0].cpus_configured = cpus;
675 mem->vm[0].cpus_standby = 0;
676 mem->vm[0].cpus_reserved = 0;
677 mem->vm[0].caf = 1000;
678 memcpy(mem->vm[0].name, "KVMguest", 8);
679 ASCEBC(mem->vm[0].name, 8);
680 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
681 ASCEBC(mem->vm[0].cpi, 16);
682}
683
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100684static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100685 u8 fc, u8 sel1, u16 sel2)
686{
687 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
688 vcpu->run->s390_stsi.addr = addr;
689 vcpu->run->s390_stsi.ar = ar;
690 vcpu->run->s390_stsi.fc = fc;
691 vcpu->run->s390_stsi.sel1 = sel1;
692 vcpu->run->s390_stsi.sel2 = sel2;
693}
694
Christian Borntraeger453423d2008-03-25 18:47:29 +0100695static int handle_stsi(struct kvm_vcpu *vcpu)
696{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100697 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
698 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
699 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100700 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100701 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100702 int rc = 0;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100703 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100704
705 vcpu->stat.instruction_stsi++;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200706 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100707
Thomas Huth5087dfa2013-06-20 17:22:01 +0200708 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
709 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
710
Thomas Huth87d41fb2013-06-20 17:22:05 +0200711 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200712 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200713 return 0;
714 }
715
716 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
717 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
718 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
719
720 if (fc == 0) {
721 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200722 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200723 return 0;
724 }
725
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300726 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100727
Thomas Huth87d41fb2013-06-20 17:22:05 +0200728 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100729 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
730
731 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100732 case 1: /* same handling for 1 and 2 */
733 case 2:
734 mem = get_zeroed_page(GFP_KERNEL);
735 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100736 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200737 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100738 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100739 break;
740 case 3:
741 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100742 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100743 mem = get_zeroed_page(GFP_KERNEL);
744 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100745 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100746 handle_stsi_3_2_2(vcpu, (void *) mem);
747 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100748 }
749
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300750 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100751 if (rc) {
752 rc = kvm_s390_inject_prog_cond(vcpu, rc);
753 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100754 }
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100755 if (vcpu->kvm->arch.user_stsi) {
756 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
757 rc = -EREMOTE;
758 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200759 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100760 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200761 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100762 vcpu->run->s.regs.gprs[0] = 0;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100763 return rc;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100764out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200765 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100766out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100767 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100768 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100769}
770
Cornelia Huckf379aae2012-12-20 15:32:10 +0100771static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100772 [0x02] = handle_stidp,
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200773 [0x04] = handle_set_clock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100774 [0x10] = handle_set_prefix,
775 [0x11] = handle_store_prefix,
776 [0x12] = handle_store_cpu_address,
David Hildenbranda3508fb2015-07-08 13:19:48 +0200777 [0x14] = kvm_s390_handle_vsie,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100778 [0x21] = handle_ipte_interlock,
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200779 [0x29] = handle_iske,
780 [0x2a] = handle_rrbe,
781 [0x2b] = handle_sske,
Thomas Huthaca84242013-09-12 10:33:48 +0200782 [0x2c] = handle_test_block,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100783 [0x30] = handle_io_inst,
784 [0x31] = handle_io_inst,
785 [0x32] = handle_io_inst,
786 [0x33] = handle_io_inst,
787 [0x34] = handle_io_inst,
788 [0x35] = handle_io_inst,
789 [0x36] = handle_io_inst,
790 [0x37] = handle_io_inst,
791 [0x38] = handle_io_inst,
792 [0x39] = handle_io_inst,
793 [0x3a] = handle_io_inst,
794 [0x3b] = handle_io_inst,
795 [0x3c] = handle_io_inst,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100796 [0x50] = handle_ipte_interlock,
Christian Borntraegerc0a6bfd2017-02-27 21:14:47 +0100797 [0x56] = handle_sthyi,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100798 [0x5f] = handle_io_inst,
799 [0x74] = handle_io_inst,
800 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100801 [0x7d] = handle_stsi,
802 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100803 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100804};
805
Christian Borntraeger70455a32009-01-22 10:28:29 +0100806int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100807{
808 intercept_handler_t handler;
809
Christian Borntraeger70455a32009-01-22 10:28:29 +0100810 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200811 * A lot of B2 instructions are priviledged. Here we check for
812 * the privileged ones, that we can handle in the kernel.
813 * Anything else goes to userspace.
814 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100815 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200816 if (handler)
817 return handler(vcpu);
818
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100819 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100820}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200821
Cornelia Huck48a3e952012-12-20 15:32:09 +0100822static int handle_epsw(struct kvm_vcpu *vcpu)
823{
824 int reg1, reg2;
825
Thomas Huthaeb87c32013-06-12 13:54:57 +0200826 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100827
828 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200829 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100830 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
831 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200832 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100833 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200834 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100835 }
836 return 0;
837}
838
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200839#define PFMF_RESERVED 0xfffc0101UL
840#define PFMF_SK 0x00020000UL
841#define PFMF_CF 0x00010000UL
842#define PFMF_UI 0x00008000UL
843#define PFMF_FSC 0x00007000UL
844#define PFMF_NQ 0x00000800UL
845#define PFMF_MR 0x00000400UL
846#define PFMF_MC 0x00000200UL
847#define PFMF_KEY 0x000000feUL
848
849static int handle_pfmf(struct kvm_vcpu *vcpu)
850{
David Hildenbrand1824c722016-05-10 09:43:11 +0200851 bool mr = false, mc = false, nq;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200852 int reg1, reg2;
853 unsigned long start, end;
David Hildenbrand1824c722016-05-10 09:43:11 +0200854 unsigned char key;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200855
856 vcpu->stat.instruction_pfmf++;
857
858 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
859
Heiko Carstens03c02802015-11-13 13:31:58 +0100860 if (!test_kvm_facility(vcpu->kvm, 8))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200861 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
862
863 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200864 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200865
866 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
867 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
868
David Hildenbrandedc5b052016-03-04 11:08:09 +0100869 /* Only provide non-quiescing support if enabled for the guest */
870 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
871 !test_kvm_facility(vcpu->kvm, 14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200872 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
873
David Hildenbrand1824c722016-05-10 09:43:11 +0200874 /* Only provide conditional-SSKE support if enabled for the guest */
875 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
876 test_kvm_facility(vcpu->kvm, 10)) {
877 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
878 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
879 }
880
881 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
882 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200883 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100884 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200885
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200886 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
887 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
888 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
889 }
890
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200891 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
892 case 0x00000000:
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200893 /* only 4k frames specify a real address */
894 start = kvm_s390_real_to_abs(vcpu, start);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200895 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
896 break;
897 case 0x00001000:
898 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
899 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200900 case 0x00002000:
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100901 /* only support 2G frame size if EDAT2 is available and we are
902 not in 24-bit addressing mode */
903 if (!test_kvm_facility(vcpu->kvm, 78) ||
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200904 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100905 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200906 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100907 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200908 default:
909 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
910 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100911
David Hildenbrand695be0e2016-05-12 14:07:05 +0200912 while (start != end) {
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200913 unsigned long useraddr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200914
Thomas Huthfb34c6032013-09-09 17:58:38 +0200915 /* Translate guest address to host address */
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200916 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
Thomas Huthfb34c6032013-09-09 17:58:38 +0200917 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200918 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
919
920 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
921 if (clear_user((void __user *)useraddr, PAGE_SIZE))
922 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
923 }
924
925 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Farhan Ali730cd632017-02-24 16:12:56 -0500926 int rc = kvm_s390_skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200927
928 if (rc)
929 return rc;
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100930 down_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200931 rc = cond_set_guest_storage_key(current->mm, useraddr,
932 key, NULL, nq, mr, mc);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100933 up_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200934 if (rc < 0)
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200935 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
936 }
937
938 start += PAGE_SIZE;
939 }
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200940 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
Heiko Carstens8bb3fdd2017-06-03 10:19:55 +0200941 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200942 vcpu->run->s.regs.gprs[reg2] = end;
943 } else {
944 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
945 end = kvm_s390_logical_to_effective(vcpu, end);
946 vcpu->run->s.regs.gprs[reg2] |= end;
947 }
948 }
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200949 return 0;
950}
951
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200952static int handle_essa(struct kvm_vcpu *vcpu)
953{
954 /* entries expected to be 1FF */
955 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200956 unsigned long *cbrlo;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200957 struct gmap *gmap;
958 int i;
959
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200960 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200961 gmap = vcpu->arch.gmap;
962 vcpu->stat.instruction_essa++;
Dominik Dingele6db1d62015-05-07 15:41:57 +0200963 if (!vcpu->kvm->arch.use_cmma)
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200964 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
965
966 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
967 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
968
969 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
970 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
971
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100972 /* Retry the ESSA instruction */
973 kvm_s390_retry_instr(vcpu);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200974 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
975 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
976 down_read(&gmap->mm->mmap_sem);
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200977 for (i = 0; i < entries; ++i)
978 __gmap_zap(gmap, cbrlo[i]);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200979 up_read(&gmap->mm->mmap_sem);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200980 return 0;
981}
982
Cornelia Huck48a3e952012-12-20 15:32:09 +0100983static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +0100984 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100985 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100986 [0x8e] = handle_ipte_interlock,
987 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200988 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200989 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100990};
991
992int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
993{
994 intercept_handler_t handler;
995
996 /* This is handled just as for the B2 instructions. */
997 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200998 if (handler)
999 return handler(vcpu);
1000
Cornelia Huck48a3e952012-12-20 15:32:09 +01001001 return -EOPNOTSUPP;
1002}
1003
Thomas Huth953ed882013-06-20 17:22:04 +02001004int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1005{
1006 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1007 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001008 int reg, rc, nr_regs;
1009 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001010 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001011 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001012
1013 vcpu->stat.instruction_lctl++;
1014
1015 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1016 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1017
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001018 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001019
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001020 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +02001021 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1022
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001023 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001024 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001025
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001026 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001027 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001028 if (rc)
1029 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +02001030 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001031 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001032 do {
Thomas Huth953ed882013-06-20 17:22:04 +02001033 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001034 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001035 if (reg == reg3)
1036 break;
1037 reg = (reg + 1) % 16;
1038 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001039 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001040 return 0;
1041}
1042
David Hildenbrandaba07502014-01-23 10:47:13 +01001043int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1044{
1045 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1046 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001047 int reg, rc, nr_regs;
1048 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +01001049 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001050 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001051
1052 vcpu->stat.instruction_stctl++;
1053
1054 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1055 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1056
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001057 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001058
1059 if (ga & 3)
1060 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1061
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001062 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001063 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1064
1065 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001066 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001067 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001068 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001069 if (reg == reg3)
1070 break;
1071 reg = (reg + 1) % 16;
1072 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001073 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001074 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001075}
1076
Thomas Huth953ed882013-06-20 17:22:04 +02001077static int handle_lctlg(struct kvm_vcpu *vcpu)
1078{
1079 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1080 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001081 int reg, rc, nr_regs;
1082 u64 ctl_array[16];
1083 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001084 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001085
1086 vcpu->stat.instruction_lctlg++;
1087
1088 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1089 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1090
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001091 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001092
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001093 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +02001094 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1095
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001096 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001097 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001098
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001099 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001100 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001101 if (rc)
1102 return kvm_s390_inject_prog_cond(vcpu, rc);
1103 reg = reg1;
1104 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001105 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001106 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001107 if (reg == reg3)
1108 break;
1109 reg = (reg + 1) % 16;
1110 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001111 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001112 return 0;
1113}
1114
David Hildenbrandaba07502014-01-23 10:47:13 +01001115static int handle_stctg(struct kvm_vcpu *vcpu)
1116{
1117 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1118 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001119 int reg, rc, nr_regs;
1120 u64 ctl_array[16];
1121 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001122 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001123
1124 vcpu->stat.instruction_stctg++;
1125
1126 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1127 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1128
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001129 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001130
1131 if (ga & 7)
1132 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1133
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001134 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001135 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1136
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001137 reg = reg1;
1138 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001139 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001140 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001141 if (reg == reg3)
1142 break;
1143 reg = (reg + 1) % 16;
1144 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001145 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001146 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001147}
1148
Cornelia Huckf379aae2012-12-20 15:32:10 +01001149static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +02001150 [0x2f] = handle_lctlg,
David Hildenbrandaba07502014-01-23 10:47:13 +01001151 [0x25] = handle_stctg,
Fan Zhang80cd8762016-08-15 04:53:22 +02001152 [0x60] = handle_ri,
1153 [0x61] = handle_ri,
1154 [0x62] = handle_ri,
Cornelia Huckf379aae2012-12-20 15:32:10 +01001155};
1156
Thomas Huth953ed882013-06-20 17:22:04 +02001157int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +01001158{
1159 intercept_handler_t handler;
1160
Cornelia Huckf379aae2012-12-20 15:32:10 +01001161 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1162 if (handler)
1163 return handler(vcpu);
1164 return -EOPNOTSUPP;
1165}
1166
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001167static int handle_tprot(struct kvm_vcpu *vcpu)
1168{
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001169 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +01001170 unsigned long hva, gpa;
1171 int ret = 0, cc = 0;
1172 bool writable;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001173 u8 ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001174
1175 vcpu->stat.instruction_tprot++;
1176
Thomas Huthf9f6bbc2013-06-20 17:22:00 +02001177 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1178 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1179
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001180 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001181
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001182 /* we only handle the Linux memory detection case:
1183 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001184 * everything else goes to userspace. */
1185 if (address2 & 0xf0)
1186 return -EOPNOTSUPP;
1187 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +01001188 ipte_lock(vcpu);
David Hildenbrand92c96322015-11-16 15:42:11 +01001189 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
Thomas Hutha0465f92014-02-04 14:48:07 +01001190 if (ret == PGM_PROTECTION) {
1191 /* Write protected? Try again with read-only... */
1192 cc = 1;
David Hildenbrand92c96322015-11-16 15:42:11 +01001193 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1194 GACC_FETCH);
Thomas Hutha0465f92014-02-04 14:48:07 +01001195 }
1196 if (ret) {
1197 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1198 ret = kvm_s390_inject_program_int(vcpu, ret);
1199 } else if (ret > 0) {
1200 /* Translation not available */
1201 kvm_s390_set_psw_cc(vcpu, 3);
1202 ret = 0;
1203 }
1204 goto out_unlock;
1205 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001206
Thomas Hutha0465f92014-02-04 14:48:07 +01001207 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1208 if (kvm_is_error_hva(hva)) {
1209 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1210 } else {
1211 if (!writable)
1212 cc = 1; /* Write not permitted ==> read-only */
1213 kvm_s390_set_psw_cc(vcpu, cc);
1214 /* Note: CC2 only occurs for storage keys (not supported yet) */
1215 }
1216out_unlock:
1217 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1218 ipte_unlock(vcpu);
1219 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001220}
1221
1222int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1223{
1224 /* For e5xx... instructions we only handle TPROT */
1225 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1226 return handle_tprot(vcpu);
1227 return -EOPNOTSUPP;
1228}
1229
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001230static int handle_sckpf(struct kvm_vcpu *vcpu)
1231{
1232 u32 value;
1233
1234 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001235 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001236
1237 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1238 return kvm_s390_inject_program_int(vcpu,
1239 PGM_SPECIFICATION);
1240
1241 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1242 vcpu->arch.sie_block->todpr = value;
1243
1244 return 0;
1245}
1246
David Hildenbrand9acc3172016-07-18 09:18:13 +02001247static int handle_ptff(struct kvm_vcpu *vcpu)
1248{
1249 /* we don't emulate any control instructions yet */
1250 kvm_s390_set_psw_cc(vcpu, 3);
1251 return 0;
1252}
1253
Cornelia Huck77975352012-12-20 15:32:06 +01001254static const intercept_handler_t x01_handlers[256] = {
David Hildenbrand9acc3172016-07-18 09:18:13 +02001255 [0x04] = handle_ptff,
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001256 [0x07] = handle_sckpf,
1257};
1258
1259int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1260{
1261 intercept_handler_t handler;
1262
1263 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1264 if (handler)
1265 return handler(vcpu);
1266 return -EOPNOTSUPP;
1267}