blob: be7138e8435144e0188d6a827188823479291e16 [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>
Heiko Carstens7c959e82013-03-05 13:14:46 +010018#include <asm/asm-offsets.h>
Heiko Carstense769ece2013-07-26 15:04:01 +020019#include <asm/facility.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010020#include <asm/current.h>
21#include <asm/debug.h>
22#include <asm/ebcdic.h>
23#include <asm/sysinfo.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020024#include <asm/pgtable.h>
25#include <asm/pgalloc.h>
26#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010027#include <asm/ptrace.h>
28#include <asm/compat.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010029#include "gaccess.h"
30#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020031#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010032
Thomas Huth6a3f95a2013-09-12 10:33:49 +020033/* Handle SCK (SET CLOCK) interception */
34static int handle_set_clock(struct kvm_vcpu *vcpu)
35{
36 struct kvm_vcpu *cpup;
37 s64 hostclk, val;
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010038 int i, rc;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020039 u64 op2;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020040
41 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
42 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
43
44 op2 = kvm_s390_get_base_disp_s(vcpu);
45 if (op2 & 7) /* Operand must be on a doubleword boundary */
46 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010047 rc = read_guest(vcpu, op2, &val, sizeof(val));
48 if (rc)
49 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020050
51 if (store_tod_clock(&hostclk)) {
52 kvm_s390_set_psw_cc(vcpu, 3);
53 return 0;
54 }
55 val = (val - hostclk) & ~0x3fUL;
56
57 mutex_lock(&vcpu->kvm->lock);
58 kvm_for_each_vcpu(i, cpup, vcpu->kvm)
59 cpup->arch.sie_block->epoch = val;
60 mutex_unlock(&vcpu->kvm->lock);
61
62 kvm_s390_set_psw_cc(vcpu, 0);
63 return 0;
64}
65
Christian Borntraeger453423d2008-03-25 18:47:29 +010066static int handle_set_prefix(struct kvm_vcpu *vcpu)
67{
Christian Borntraeger453423d2008-03-25 18:47:29 +010068 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +010069 u32 address;
70 int rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +010071
72 vcpu->stat.instruction_spx++;
73
Thomas Huth5087dfa2013-06-20 17:22:01 +020074 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
75 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
76
Cornelia Huckb1c571a2012-12-20 15:32:07 +010077 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010078
79 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010080 if (operand2 & 3)
81 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010082
83 /* get the value */
Heiko Carstens665170c2014-01-01 16:47:12 +010084 rc = read_guest(vcpu, operand2, &address, sizeof(address));
85 if (rc)
86 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +010087
Heiko Carstens665170c2014-01-01 16:47:12 +010088 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +010089
Heiko Carstens665170c2014-01-01 16:47:12 +010090 /*
91 * Make sure the new value is valid memory. We only need to check the
92 * first page, since address is 8k aligned and memory pieces are always
93 * at least 1MB aligned and have at least a size of 1MB.
94 */
95 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010096 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010097
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010098 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010099
100 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200101 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100102 return 0;
103}
104
105static int handle_store_prefix(struct kvm_vcpu *vcpu)
106{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100107 u64 operand2;
108 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100109 int rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100110
111 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100112
Thomas Huth5087dfa2013-06-20 17:22:01 +0200113 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
114 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
115
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100116 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100117
118 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100119 if (operand2 & 3)
120 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100121
Michael Muellerfda902c2014-05-13 16:58:30 +0200122 address = kvm_s390_get_prefix(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100123
124 /* get the value */
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100125 rc = write_guest(vcpu, operand2, &address, sizeof(address));
126 if (rc)
127 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100128
129 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200130 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100131 return 0;
132}
133
134static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
135{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100136 u16 vcpu_id = vcpu->vcpu_id;
137 u64 ga;
138 int rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100139
140 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100141
Thomas Huth5087dfa2013-06-20 17:22:01 +0200142 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
143 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
144
Heiko Carstens8b96de02014-01-01 16:53:27 +0100145 ga = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100146
Heiko Carstens8b96de02014-01-01 16:53:27 +0100147 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100148 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100149
Heiko Carstens8b96de02014-01-01 16:53:27 +0100150 rc = write_guest(vcpu, ga, &vcpu_id, sizeof(vcpu_id));
151 if (rc)
152 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100153
Heiko Carstens8b96de02014-01-01 16:53:27 +0100154 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", ga);
155 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100156 return 0;
157}
158
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200159static int __skey_check_enable(struct kvm_vcpu *vcpu)
Dominik Dingel693ffc02014-01-14 18:11:14 +0100160{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200161 int rc = 0;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100162 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200163 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100164
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200165 rc = s390_enable_skey();
Dominik Dingel693ffc02014-01-14 18:11:14 +0100166 trace_kvm_s390_skey_related_inst(vcpu);
167 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200168 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100169}
170
171
Christian Borntraeger453423d2008-03-25 18:47:29 +0100172static int handle_skey(struct kvm_vcpu *vcpu)
173{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200174 int rc = __skey_check_enable(vcpu);
Dominik Dingel693ffc02014-01-14 18:11:14 +0100175
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200176 if (rc)
177 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100178 vcpu->stat.instruction_storage_key++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200179
180 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
181 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
182
Thomas Huth04b41ac2014-11-12 17:13:29 +0100183 kvm_s390_rewind_psw(vcpu, 4);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100184 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
185 return 0;
186}
187
Heiko Carstens8a2422342014-01-10 14:33:28 +0100188static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
189{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100190 vcpu->stat.instruction_ipte_interlock++;
Thomas Huth04b41ac2014-11-12 17:13:29 +0100191 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100192 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
193 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
Thomas Huth04b41ac2014-11-12 17:13:29 +0100194 kvm_s390_rewind_psw(vcpu, 4);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100195 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
196 return 0;
197}
198
Thomas Huthaca84242013-09-12 10:33:48 +0200199static int handle_test_block(struct kvm_vcpu *vcpu)
200{
Thomas Huthaca84242013-09-12 10:33:48 +0200201 gpa_t addr;
202 int reg2;
203
204 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
205 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
206
207 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
208 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100209 addr = kvm_s390_logical_to_effective(vcpu, addr);
210 if (kvm_s390_check_low_addr_protection(vcpu, addr))
211 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200212 addr = kvm_s390_real_to_abs(vcpu, addr);
213
Heiko Carstensef23e772014-01-01 16:53:49 +0100214 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200215 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
216 /*
217 * We don't expect errors on modern systems, and do not care
218 * about storage keys (yet), so let's just clear the page.
219 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100220 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200221 return -EFAULT;
222 kvm_s390_set_psw_cc(vcpu, 0);
223 vcpu->run->s.regs.gprs[0] = 0;
224 return 0;
225}
226
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100227static int handle_tpi(struct kvm_vcpu *vcpu)
228{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100229 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100230 unsigned long len;
231 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100232 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100233 u64 addr;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100234
235 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100236 if (addr & 3)
237 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100238
Thomas Huthf0926692013-10-09 14:15:54 +0200239 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100240 if (!inti) {
241 kvm_s390_set_psw_cc(vcpu, 0);
242 return 0;
243 }
244
Heiko Carstens4799b552014-01-01 16:55:48 +0100245 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
246 tpi_data[1] = inti->io.io_int_parm;
247 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100248 if (addr) {
249 /*
250 * Store the two-word I/O interruption code into the
251 * provided area.
252 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100253 len = sizeof(tpi_data) - 4;
254 rc = write_guest(vcpu, addr, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100255 if (rc) {
256 rc = kvm_s390_inject_prog_cond(vcpu, rc);
257 goto reinject_interrupt;
258 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100259 } else {
260 /*
261 * Store the three-word I/O interruption code into
262 * the appropriate lowcore area.
263 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100264 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100265 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
266 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100267 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100268 goto reinject_interrupt;
269 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100270 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100271
272 /* irq was successfully handed to the guest */
273 kfree(inti);
274 kvm_s390_set_psw_cc(vcpu, 1);
275 return 0;
276reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100277 /*
278 * If we encounter a problem storing the interruption code, the
279 * instruction is suppressed from the guest's view: reinject the
280 * interrupt.
281 */
David Hildenbrand261520d2015-02-04 15:53:42 +0100282 kvm_s390_reinject_io_int(vcpu->kvm, inti);
283 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100284 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100285}
286
287static int handle_tsch(struct kvm_vcpu *vcpu)
288{
289 struct kvm_s390_interrupt_info *inti;
290
291 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
292 vcpu->run->s.regs.gprs[1]);
293
294 /*
295 * Prepare exit to userspace.
296 * We indicate whether we dequeued a pending I/O interrupt
297 * so that userspace can re-inject it if the instruction gets
298 * a program check. While this may re-order the pending I/O
299 * interrupts, this is no problem since the priority is kept
300 * intact.
301 */
302 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
303 vcpu->run->s390_tsch.dequeued = !!inti;
304 if (inti) {
305 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
306 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
307 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
308 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
309 }
310 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
311 kfree(inti);
312 return -EREMOTE;
313}
314
Cornelia Huckf379aae2012-12-20 15:32:10 +0100315static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100316{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100317 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100318
Thomas Huth5087dfa2013-06-20 17:22:01 +0200319 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
320 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
321
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100322 if (vcpu->kvm->arch.css_support) {
323 /*
324 * Most I/O instructions will be handled by userspace.
325 * Exceptions are tpi and the interrupt portion of tsch.
326 */
327 if (vcpu->arch.sie_block->ipa == 0xb236)
328 return handle_tpi(vcpu);
329 if (vcpu->arch.sie_block->ipa == 0xb235)
330 return handle_tsch(vcpu);
331 /* Handle in userspace. */
332 return -EOPNOTSUPP;
333 } else {
334 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100335 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100336 * I/O instructions.
337 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200338 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100339 return 0;
340 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100341}
342
Christian Borntraeger453423d2008-03-25 18:47:29 +0100343static int handle_stfl(struct kvm_vcpu *vcpu)
344{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100345 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100346 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100347
348 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200349
350 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
351 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
352
Michael Mueller9d8d5782015-02-02 15:42:51 +0100353 /*
354 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
355 * into a u32 memory representation. They will remain bits 0-31.
356 */
Michael Mueller981467c2015-02-24 13:51:04 +0100357 fac = *vcpu->kvm->arch.model.fac->list >> 32;
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100358 rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100359 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100360 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100361 return rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100362 VCPU_EVENT(vcpu, 5, "store facility list value %x", fac);
363 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100364 return 0;
365}
366
Cornelia Huck48a3e952012-12-20 15:32:09 +0100367#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
368#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100369#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100370#define PSW_ADDR_31 0x000000007fffffffUL
371
Thomas Hutha3fb5772014-04-17 09:10:40 +0200372int is_valid_psw(psw_t *psw)
373{
Heiko Carstens3736b872013-03-25 17:22:52 +0100374 if (psw->mask & PSW_MASK_UNASSIGNED)
375 return 0;
376 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
377 if (psw->addr & ~PSW_ADDR_31)
378 return 0;
379 }
380 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
381 return 0;
382 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
383 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200384 if (psw->addr & 1)
385 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100386 return 1;
387}
388
Cornelia Huck48a3e952012-12-20 15:32:09 +0100389int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
390{
Heiko Carstens3736b872013-03-25 17:22:52 +0100391 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100392 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100393 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100394 int rc;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100395
Heiko Carstens3736b872013-03-25 17:22:52 +0100396 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200397 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
398
Cornelia Huck48a3e952012-12-20 15:32:09 +0100399 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100400 if (addr & 7)
401 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100402
403 rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
404 if (rc)
405 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100406 if (!(new_psw.mask & PSW32_MASK_BASE))
407 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100408 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
409 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
410 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
411 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100412 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100413 return 0;
414}
415
416static int handle_lpswe(struct kvm_vcpu *vcpu)
417{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100418 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100419 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100420 int rc;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100421
Thomas Huth5087dfa2013-06-20 17:22:01 +0200422 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
423 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
424
Cornelia Huck48a3e952012-12-20 15:32:09 +0100425 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100426 if (addr & 7)
427 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100428 rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
429 if (rc)
430 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100431 vcpu->arch.sie_block->gpsw = new_psw;
432 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100433 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100434 return 0;
435}
436
Christian Borntraeger453423d2008-03-25 18:47:29 +0100437static int handle_stidp(struct kvm_vcpu *vcpu)
438{
Heiko Carstens7d777d72014-01-01 16:58:16 +0100439 u64 stidp_data = vcpu->arch.stidp_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100440 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100441 int rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100442
443 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100444
Thomas Huth5087dfa2013-06-20 17:22:01 +0200445 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
446 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
447
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100448 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100449
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100450 if (operand2 & 7)
451 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100452
Heiko Carstens7d777d72014-01-01 16:58:16 +0100453 rc = write_guest(vcpu, operand2, &stidp_data, sizeof(stidp_data));
454 if (rc)
455 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100456
457 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
Christian Borntraeger453423d2008-03-25 18:47:29 +0100458 return 0;
459}
460
461static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
462{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100463 int cpus = 0;
464 int n;
465
Jens Freimannff520a62014-02-24 10:11:41 +0100466 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100467
468 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200469 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100470 mem->count = 0;
471 if (mem->count < 8)
472 mem->count++;
473 for (n = mem->count - 1; n > 0 ; n--)
474 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
475
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100476 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100477 mem->vm[0].cpus_total = cpus;
478 mem->vm[0].cpus_configured = cpus;
479 mem->vm[0].cpus_standby = 0;
480 mem->vm[0].cpus_reserved = 0;
481 mem->vm[0].caf = 1000;
482 memcpy(mem->vm[0].name, "KVMguest", 8);
483 ASCEBC(mem->vm[0].name, 8);
484 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
485 ASCEBC(mem->vm[0].cpi, 16);
486}
487
488static int handle_stsi(struct kvm_vcpu *vcpu)
489{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100490 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
491 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
492 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100493 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100494 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100495 int rc = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100496
497 vcpu->stat.instruction_stsi++;
498 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
499
Thomas Huth5087dfa2013-06-20 17:22:01 +0200500 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
501 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
502
Thomas Huth87d41fb2013-06-20 17:22:05 +0200503 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200504 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200505 return 0;
506 }
507
508 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
509 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
510 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
511
512 if (fc == 0) {
513 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200514 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200515 return 0;
516 }
517
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100518 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100519
Thomas Huth87d41fb2013-06-20 17:22:05 +0200520 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100521 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
522
523 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100524 case 1: /* same handling for 1 and 2 */
525 case 2:
526 mem = get_zeroed_page(GFP_KERNEL);
527 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100528 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200529 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100530 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100531 break;
532 case 3:
533 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100534 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100535 mem = get_zeroed_page(GFP_KERNEL);
536 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100537 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100538 handle_stsi_3_2_2(vcpu, (void *) mem);
539 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100540 }
541
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100542 rc = write_guest(vcpu, operand2, (void *)mem, PAGE_SIZE);
543 if (rc) {
544 rc = kvm_s390_inject_prog_cond(vcpu, rc);
545 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100546 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200547 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100548 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200549 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100550 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100551 return 0;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100552out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200553 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100554out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100555 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100556 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100557}
558
Cornelia Huckf379aae2012-12-20 15:32:10 +0100559static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100560 [0x02] = handle_stidp,
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200561 [0x04] = handle_set_clock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100562 [0x10] = handle_set_prefix,
563 [0x11] = handle_store_prefix,
564 [0x12] = handle_store_cpu_address,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100565 [0x21] = handle_ipte_interlock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100566 [0x29] = handle_skey,
567 [0x2a] = handle_skey,
568 [0x2b] = handle_skey,
Thomas Huthaca84242013-09-12 10:33:48 +0200569 [0x2c] = handle_test_block,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100570 [0x30] = handle_io_inst,
571 [0x31] = handle_io_inst,
572 [0x32] = handle_io_inst,
573 [0x33] = handle_io_inst,
574 [0x34] = handle_io_inst,
575 [0x35] = handle_io_inst,
576 [0x36] = handle_io_inst,
577 [0x37] = handle_io_inst,
578 [0x38] = handle_io_inst,
579 [0x39] = handle_io_inst,
580 [0x3a] = handle_io_inst,
581 [0x3b] = handle_io_inst,
582 [0x3c] = handle_io_inst,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100583 [0x50] = handle_ipte_interlock,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100584 [0x5f] = handle_io_inst,
585 [0x74] = handle_io_inst,
586 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100587 [0x7d] = handle_stsi,
588 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100589 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100590};
591
Christian Borntraeger70455a32009-01-22 10:28:29 +0100592int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100593{
594 intercept_handler_t handler;
595
Christian Borntraeger70455a32009-01-22 10:28:29 +0100596 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200597 * A lot of B2 instructions are priviledged. Here we check for
598 * the privileged ones, that we can handle in the kernel.
599 * Anything else goes to userspace.
600 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100601 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200602 if (handler)
603 return handler(vcpu);
604
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100605 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100606}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200607
Cornelia Huck48a3e952012-12-20 15:32:09 +0100608static int handle_epsw(struct kvm_vcpu *vcpu)
609{
610 int reg1, reg2;
611
Thomas Huthaeb87c32013-06-12 13:54:57 +0200612 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100613
614 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200615 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100616 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
617 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200618 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100619 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200620 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100621 }
622 return 0;
623}
624
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200625#define PFMF_RESERVED 0xfffc0101UL
626#define PFMF_SK 0x00020000UL
627#define PFMF_CF 0x00010000UL
628#define PFMF_UI 0x00008000UL
629#define PFMF_FSC 0x00007000UL
630#define PFMF_NQ 0x00000800UL
631#define PFMF_MR 0x00000400UL
632#define PFMF_MC 0x00000200UL
633#define PFMF_KEY 0x000000feUL
634
635static int handle_pfmf(struct kvm_vcpu *vcpu)
636{
637 int reg1, reg2;
638 unsigned long start, end;
639
640 vcpu->stat.instruction_pfmf++;
641
642 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
643
644 if (!MACHINE_HAS_PFMF)
645 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
646
647 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200648 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200649
650 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
651 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
652
653 /* Only provide non-quiescing support if the host supports it */
Heiko Carstense769ece2013-07-26 15:04:01 +0200654 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200655 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
656
657 /* No support for conditional-SSKE */
658 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
659 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
660
661 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100662 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200663
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200664 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
665 case 0x00000000:
666 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
667 break;
668 case 0x00001000:
669 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
670 break;
671 /* We dont support EDAT2
672 case 0x00002000:
673 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
674 break;*/
675 default:
676 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
677 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100678
679 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
680 if (kvm_s390_check_low_addr_protection(vcpu, start))
681 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
682 }
683
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200684 while (start < end) {
Thomas Huthfb34c6032013-09-09 17:58:38 +0200685 unsigned long useraddr, abs_addr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200686
Thomas Huthfb34c6032013-09-09 17:58:38 +0200687 /* Translate guest address to host address */
688 if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
689 abs_addr = kvm_s390_real_to_abs(vcpu, start);
690 else
691 abs_addr = start;
692 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
693 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200694 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
695
696 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
697 if (clear_user((void __user *)useraddr, PAGE_SIZE))
698 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
699 }
700
701 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200702 int rc = __skey_check_enable(vcpu);
703
704 if (rc)
705 return rc;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200706 if (set_guest_storage_key(current->mm, useraddr,
707 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
708 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
709 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
710 }
711
712 start += PAGE_SIZE;
713 }
714 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
715 vcpu->run->s.regs.gprs[reg2] = end;
716 return 0;
717}
718
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200719static int handle_essa(struct kvm_vcpu *vcpu)
720{
721 /* entries expected to be 1FF */
722 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
723 unsigned long *cbrlo, cbrle;
724 struct gmap *gmap;
725 int i;
726
727 VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
728 gmap = vcpu->arch.gmap;
729 vcpu->stat.instruction_essa++;
Dominik Dingelb31605c2014-03-25 13:47:11 +0100730 if (!kvm_s390_cmma_enabled(vcpu->kvm))
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200731 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
732
733 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
734 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
735
736 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
737 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
738
739 /* Rewind PSW to repeat the ESSA instruction */
Thomas Huth04b41ac2014-11-12 17:13:29 +0100740 kvm_s390_rewind_psw(vcpu, 4);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200741 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
742 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
743 down_read(&gmap->mm->mmap_sem);
744 for (i = 0; i < entries; ++i) {
745 cbrle = cbrlo[i];
746 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
747 /* invalid entry */
748 break;
749 /* try to free backing */
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200750 __gmap_zap(gmap, cbrle);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200751 }
752 up_read(&gmap->mm->mmap_sem);
753 if (i < entries)
754 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
755 return 0;
756}
757
Cornelia Huck48a3e952012-12-20 15:32:09 +0100758static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +0100759 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100760 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100761 [0x8e] = handle_ipte_interlock,
762 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200763 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200764 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100765};
766
767int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
768{
769 intercept_handler_t handler;
770
771 /* This is handled just as for the B2 instructions. */
772 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200773 if (handler)
774 return handler(vcpu);
775
Cornelia Huck48a3e952012-12-20 15:32:09 +0100776 return -EOPNOTSUPP;
777}
778
Thomas Huth953ed882013-06-20 17:22:04 +0200779int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
780{
781 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
782 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100783 int reg, rc, nr_regs;
784 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100785 u64 ga;
Thomas Huth953ed882013-06-20 17:22:04 +0200786
787 vcpu->stat.instruction_lctl++;
788
789 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
790 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
791
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100792 ga = kvm_s390_get_base_disp_rs(vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +0200793
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100794 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +0200795 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
796
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100797 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
798 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +0200799
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100800 nr_regs = ((reg3 - reg1) & 0xf) + 1;
801 rc = read_guest(vcpu, ga, ctl_array, nr_regs * sizeof(u32));
802 if (rc)
803 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +0200804 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100805 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +0200806 do {
Thomas Huth953ed882013-06-20 17:22:04 +0200807 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100808 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +0200809 if (reg == reg3)
810 break;
811 reg = (reg + 1) % 16;
812 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +0100813 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +0200814 return 0;
815}
816
David Hildenbrandaba07502014-01-23 10:47:13 +0100817int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
818{
819 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
820 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100821 int reg, rc, nr_regs;
822 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +0100823 u64 ga;
David Hildenbrandaba07502014-01-23 10:47:13 +0100824
825 vcpu->stat.instruction_stctl++;
826
827 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
828 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
829
830 ga = kvm_s390_get_base_disp_rs(vcpu);
831
832 if (ga & 3)
833 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
834
835 VCPU_EVENT(vcpu, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
836 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
837
838 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100839 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100840 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100841 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +0100842 if (reg == reg3)
843 break;
844 reg = (reg + 1) % 16;
845 } while (1);
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100846 rc = write_guest(vcpu, ga, ctl_array, nr_regs * sizeof(u32));
847 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100848}
849
Thomas Huth953ed882013-06-20 17:22:04 +0200850static int handle_lctlg(struct kvm_vcpu *vcpu)
851{
852 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
853 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100854 int reg, rc, nr_regs;
855 u64 ctl_array[16];
856 u64 ga;
Thomas Huth953ed882013-06-20 17:22:04 +0200857
858 vcpu->stat.instruction_lctlg++;
859
860 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
861 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
862
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100863 ga = kvm_s390_get_base_disp_rsy(vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +0200864
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100865 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +0200866 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
867
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100868 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
869 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +0200870
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100871 nr_regs = ((reg3 - reg1) & 0xf) + 1;
872 rc = read_guest(vcpu, ga, ctl_array, nr_regs * sizeof(u64));
873 if (rc)
874 return kvm_s390_inject_prog_cond(vcpu, rc);
875 reg = reg1;
876 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +0200877 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100878 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +0200879 if (reg == reg3)
880 break;
881 reg = (reg + 1) % 16;
882 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +0100883 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +0200884 return 0;
885}
886
David Hildenbrandaba07502014-01-23 10:47:13 +0100887static int handle_stctg(struct kvm_vcpu *vcpu)
888{
889 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
890 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100891 int reg, rc, nr_regs;
892 u64 ctl_array[16];
893 u64 ga;
David Hildenbrandaba07502014-01-23 10:47:13 +0100894
895 vcpu->stat.instruction_stctg++;
896
897 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
898 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
899
900 ga = kvm_s390_get_base_disp_rsy(vcpu);
901
902 if (ga & 7)
903 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
904
David Hildenbrandaba07502014-01-23 10:47:13 +0100905 VCPU_EVENT(vcpu, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
906 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
907
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100908 reg = reg1;
909 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100910 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100911 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +0100912 if (reg == reg3)
913 break;
914 reg = (reg + 1) % 16;
915 } while (1);
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100916 rc = write_guest(vcpu, ga, ctl_array, nr_regs * sizeof(u64));
917 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100918}
919
Cornelia Huckf379aae2012-12-20 15:32:10 +0100920static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +0200921 [0x2f] = handle_lctlg,
David Hildenbrandaba07502014-01-23 10:47:13 +0100922 [0x25] = handle_stctg,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100923};
924
Thomas Huth953ed882013-06-20 17:22:04 +0200925int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +0100926{
927 intercept_handler_t handler;
928
Cornelia Huckf379aae2012-12-20 15:32:10 +0100929 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
930 if (handler)
931 return handler(vcpu);
932 return -EOPNOTSUPP;
933}
934
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200935static int handle_tprot(struct kvm_vcpu *vcpu)
936{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100937 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +0100938 unsigned long hva, gpa;
939 int ret = 0, cc = 0;
940 bool writable;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200941
942 vcpu->stat.instruction_tprot++;
943
Thomas Huthf9f6bbc2013-06-20 17:22:00 +0200944 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
945 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
946
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100947 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
948
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200949 /* we only handle the Linux memory detection case:
950 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200951 * everything else goes to userspace. */
952 if (address2 & 0xf0)
953 return -EOPNOTSUPP;
954 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +0100955 ipte_lock(vcpu);
956 ret = guest_translate_address(vcpu, address1, &gpa, 1);
957 if (ret == PGM_PROTECTION) {
958 /* Write protected? Try again with read-only... */
959 cc = 1;
960 ret = guest_translate_address(vcpu, address1, &gpa, 0);
961 }
962 if (ret) {
963 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
964 ret = kvm_s390_inject_program_int(vcpu, ret);
965 } else if (ret > 0) {
966 /* Translation not available */
967 kvm_s390_set_psw_cc(vcpu, 3);
968 ret = 0;
969 }
970 goto out_unlock;
971 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200972
Thomas Hutha0465f92014-02-04 14:48:07 +0100973 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
974 if (kvm_is_error_hva(hva)) {
975 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
976 } else {
977 if (!writable)
978 cc = 1; /* Write not permitted ==> read-only */
979 kvm_s390_set_psw_cc(vcpu, cc);
980 /* Note: CC2 only occurs for storage keys (not supported yet) */
981 }
982out_unlock:
983 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
984 ipte_unlock(vcpu);
985 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200986}
987
988int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
989{
990 /* For e5xx... instructions we only handle TPROT */
991 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
992 return handle_tprot(vcpu);
993 return -EOPNOTSUPP;
994}
995
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200996static int handle_sckpf(struct kvm_vcpu *vcpu)
997{
998 u32 value;
999
1000 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001001 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001002
1003 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1004 return kvm_s390_inject_program_int(vcpu,
1005 PGM_SPECIFICATION);
1006
1007 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1008 vcpu->arch.sie_block->todpr = value;
1009
1010 return 0;
1011}
1012
Cornelia Huck77975352012-12-20 15:32:06 +01001013static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001014 [0x07] = handle_sckpf,
1015};
1016
1017int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1018{
1019 intercept_handler_t handler;
1020
1021 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1022 if (handler)
1023 return handler(vcpu);
1024 return -EOPNOTSUPP;
1025}