blob: f4fe02e84326de148af291d43a5859fd6b04b0ca [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;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030039 ar_t ar;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020040 u64 op2;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020041
42 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
43 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
44
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030045 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020046 if (op2 & 7) /* Operand must be on a doubleword boundary */
47 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030048 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010049 if (rc)
50 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020051
52 if (store_tod_clock(&hostclk)) {
53 kvm_s390_set_psw_cc(vcpu, 3);
54 return 0;
55 }
56 val = (val - hostclk) & ~0x3fUL;
57
58 mutex_lock(&vcpu->kvm->lock);
59 kvm_for_each_vcpu(i, cpup, vcpu->kvm)
60 cpup->arch.sie_block->epoch = val;
61 mutex_unlock(&vcpu->kvm->lock);
62
63 kvm_s390_set_psw_cc(vcpu, 0);
64 return 0;
65}
66
Christian Borntraeger453423d2008-03-25 18:47:29 +010067static int handle_set_prefix(struct kvm_vcpu *vcpu)
68{
Christian Borntraeger453423d2008-03-25 18:47:29 +010069 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +010070 u32 address;
71 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030072 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +010073
74 vcpu->stat.instruction_spx++;
75
Thomas Huth5087dfa2013-06-20 17:22:01 +020076 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
77 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
78
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030079 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +010080
81 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010082 if (operand2 & 3)
83 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010084
85 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030086 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstens665170c2014-01-01 16:47:12 +010087 if (rc)
88 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +010089
Heiko Carstens665170c2014-01-01 16:47:12 +010090 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +010091
Heiko Carstens665170c2014-01-01 16:47:12 +010092 /*
93 * Make sure the new value is valid memory. We only need to check the
94 * first page, since address is 8k aligned and memory pieces are always
95 * at least 1MB aligned and have at least a size of 1MB.
96 */
97 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010098 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010099
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100100 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100101
102 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200103 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100104 return 0;
105}
106
107static int handle_store_prefix(struct kvm_vcpu *vcpu)
108{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100109 u64 operand2;
110 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100111 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300112 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100113
114 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100115
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
Michael Muellerfda902c2014-05-13 16:58:30 +0200125 address = kvm_s390_get_prefix(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100126
127 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300128 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100129 if (rc)
130 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100131
132 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200133 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100134 return 0;
135}
136
137static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
138{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100139 u16 vcpu_id = vcpu->vcpu_id;
140 u64 ga;
141 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300142 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100143
144 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100145
Thomas Huth5087dfa2013-06-20 17:22:01 +0200146 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
147 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
148
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300149 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100150
Heiko Carstens8b96de02014-01-01 16:53:27 +0100151 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100152 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100153
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300154 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
Heiko Carstens8b96de02014-01-01 16:53:27 +0100155 if (rc)
156 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100157
Heiko Carstens8b96de02014-01-01 16:53:27 +0100158 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", ga);
159 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100160 return 0;
161}
162
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200163static int __skey_check_enable(struct kvm_vcpu *vcpu)
Dominik Dingel693ffc02014-01-14 18:11:14 +0100164{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200165 int rc = 0;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100166 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200167 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100168
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200169 rc = s390_enable_skey();
Dominik Dingel693ffc02014-01-14 18:11:14 +0100170 trace_kvm_s390_skey_related_inst(vcpu);
171 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200172 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100173}
174
175
Christian Borntraeger453423d2008-03-25 18:47:29 +0100176static int handle_skey(struct kvm_vcpu *vcpu)
177{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200178 int rc = __skey_check_enable(vcpu);
Dominik Dingel693ffc02014-01-14 18:11:14 +0100179
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200180 if (rc)
181 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100182 vcpu->stat.instruction_storage_key++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200183
184 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
185 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
186
Thomas Huth04b41ac2014-11-12 17:13:29 +0100187 kvm_s390_rewind_psw(vcpu, 4);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100188 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
189 return 0;
190}
191
Heiko Carstens8a2422342014-01-10 14:33:28 +0100192static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
193{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100194 vcpu->stat.instruction_ipte_interlock++;
Thomas Huth04b41ac2014-11-12 17:13:29 +0100195 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100196 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
197 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
Thomas Huth04b41ac2014-11-12 17:13:29 +0100198 kvm_s390_rewind_psw(vcpu, 4);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100199 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
200 return 0;
201}
202
Thomas Huthaca84242013-09-12 10:33:48 +0200203static int handle_test_block(struct kvm_vcpu *vcpu)
204{
Thomas Huthaca84242013-09-12 10:33:48 +0200205 gpa_t addr;
206 int reg2;
207
208 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
209 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
210
211 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
212 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100213 addr = kvm_s390_logical_to_effective(vcpu, addr);
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300214 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
Thomas Huthe45efa22014-03-07 12:14:23 +0100215 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200216 addr = kvm_s390_real_to_abs(vcpu, addr);
217
Heiko Carstensef23e772014-01-01 16:53:49 +0100218 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200219 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
220 /*
221 * We don't expect errors on modern systems, and do not care
222 * about storage keys (yet), so let's just clear the page.
223 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100224 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200225 return -EFAULT;
226 kvm_s390_set_psw_cc(vcpu, 0);
227 vcpu->run->s.regs.gprs[0] = 0;
228 return 0;
229}
230
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100231static int handle_tpi(struct kvm_vcpu *vcpu)
232{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100233 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100234 unsigned long len;
235 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100236 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100237 u64 addr;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300238 ar_t ar;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100239
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300240 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100241 if (addr & 3)
242 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100243
Thomas Huthf0926692013-10-09 14:15:54 +0200244 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100245 if (!inti) {
246 kvm_s390_set_psw_cc(vcpu, 0);
247 return 0;
248 }
249
Heiko Carstens4799b552014-01-01 16:55:48 +0100250 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
251 tpi_data[1] = inti->io.io_int_parm;
252 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100253 if (addr) {
254 /*
255 * Store the two-word I/O interruption code into the
256 * provided area.
257 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100258 len = sizeof(tpi_data) - 4;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300259 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100260 if (rc) {
261 rc = kvm_s390_inject_prog_cond(vcpu, rc);
262 goto reinject_interrupt;
263 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100264 } else {
265 /*
266 * Store the three-word I/O interruption code into
267 * the appropriate lowcore area.
268 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100269 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100270 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
271 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100272 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100273 goto reinject_interrupt;
274 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100275 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100276
277 /* irq was successfully handed to the guest */
278 kfree(inti);
279 kvm_s390_set_psw_cc(vcpu, 1);
280 return 0;
281reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100282 /*
283 * If we encounter a problem storing the interruption code, the
284 * instruction is suppressed from the guest's view: reinject the
285 * interrupt.
286 */
David Hildenbrand15462e32015-02-04 15:59:11 +0100287 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
288 kfree(inti);
289 rc = -EFAULT;
290 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100291 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100292 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100293}
294
295static int handle_tsch(struct kvm_vcpu *vcpu)
296{
297 struct kvm_s390_interrupt_info *inti;
298
299 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
300 vcpu->run->s.regs.gprs[1]);
301
302 /*
303 * Prepare exit to userspace.
304 * We indicate whether we dequeued a pending I/O interrupt
305 * so that userspace can re-inject it if the instruction gets
306 * a program check. While this may re-order the pending I/O
307 * interrupts, this is no problem since the priority is kept
308 * intact.
309 */
310 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
311 vcpu->run->s390_tsch.dequeued = !!inti;
312 if (inti) {
313 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
314 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
315 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
316 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
317 }
318 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
319 kfree(inti);
320 return -EREMOTE;
321}
322
Cornelia Huckf379aae2012-12-20 15:32:10 +0100323static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100324{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100325 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100326
Thomas Huth5087dfa2013-06-20 17:22:01 +0200327 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
328 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
329
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100330 if (vcpu->kvm->arch.css_support) {
331 /*
332 * Most I/O instructions will be handled by userspace.
333 * Exceptions are tpi and the interrupt portion of tsch.
334 */
335 if (vcpu->arch.sie_block->ipa == 0xb236)
336 return handle_tpi(vcpu);
337 if (vcpu->arch.sie_block->ipa == 0xb235)
338 return handle_tsch(vcpu);
339 /* Handle in userspace. */
340 return -EOPNOTSUPP;
341 } else {
342 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100343 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100344 * I/O instructions.
345 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200346 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100347 return 0;
348 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100349}
350
Christian Borntraeger453423d2008-03-25 18:47:29 +0100351static int handle_stfl(struct kvm_vcpu *vcpu)
352{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100353 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100354 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100355
356 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200357
358 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
359 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
360
Michael Mueller9d8d5782015-02-02 15:42:51 +0100361 /*
362 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
363 * into a u32 memory representation. They will remain bits 0-31.
364 */
Michael Mueller981467c2015-02-24 13:51:04 +0100365 fac = *vcpu->kvm->arch.model.fac->list >> 32;
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100366 rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100367 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100368 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100369 return rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100370 VCPU_EVENT(vcpu, 5, "store facility list value %x", fac);
371 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100372 return 0;
373}
374
Cornelia Huck48a3e952012-12-20 15:32:09 +0100375#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
376#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100377#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100378#define PSW_ADDR_31 0x000000007fffffffUL
379
Thomas Hutha3fb5772014-04-17 09:10:40 +0200380int is_valid_psw(psw_t *psw)
381{
Heiko Carstens3736b872013-03-25 17:22:52 +0100382 if (psw->mask & PSW_MASK_UNASSIGNED)
383 return 0;
384 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
385 if (psw->addr & ~PSW_ADDR_31)
386 return 0;
387 }
388 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
389 return 0;
390 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
391 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200392 if (psw->addr & 1)
393 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100394 return 1;
395}
396
Cornelia Huck48a3e952012-12-20 15:32:09 +0100397int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
398{
Heiko Carstens3736b872013-03-25 17:22:52 +0100399 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100400 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100401 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100402 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300403 ar_t ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100404
Heiko Carstens3736b872013-03-25 17:22:52 +0100405 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200406 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
407
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300408 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100409 if (addr & 7)
410 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100411
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300412 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100413 if (rc)
414 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100415 if (!(new_psw.mask & PSW32_MASK_BASE))
416 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100417 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
418 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
419 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
420 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100421 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100422 return 0;
423}
424
425static int handle_lpswe(struct kvm_vcpu *vcpu)
426{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100427 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100428 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100429 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300430 ar_t ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100431
Thomas Huth5087dfa2013-06-20 17:22:01 +0200432 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
433 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
434
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300435 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100436 if (addr & 7)
437 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300438 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100439 if (rc)
440 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100441 vcpu->arch.sie_block->gpsw = new_psw;
442 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100443 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100444 return 0;
445}
446
Christian Borntraeger453423d2008-03-25 18:47:29 +0100447static int handle_stidp(struct kvm_vcpu *vcpu)
448{
Heiko Carstens7d777d72014-01-01 16:58:16 +0100449 u64 stidp_data = vcpu->arch.stidp_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100450 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100451 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300452 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100453
454 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100455
Thomas Huth5087dfa2013-06-20 17:22:01 +0200456 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
457 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
458
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300459 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100460
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100461 if (operand2 & 7)
462 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100463
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300464 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
Heiko Carstens7d777d72014-01-01 16:58:16 +0100465 if (rc)
466 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100467
468 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
Christian Borntraeger453423d2008-03-25 18:47:29 +0100469 return 0;
470}
471
472static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
473{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100474 int cpus = 0;
475 int n;
476
Jens Freimannff520a62014-02-24 10:11:41 +0100477 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100478
479 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200480 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100481 mem->count = 0;
482 if (mem->count < 8)
483 mem->count++;
484 for (n = mem->count - 1; n > 0 ; n--)
485 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
486
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100487 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100488 mem->vm[0].cpus_total = cpus;
489 mem->vm[0].cpus_configured = cpus;
490 mem->vm[0].cpus_standby = 0;
491 mem->vm[0].cpus_reserved = 0;
492 mem->vm[0].caf = 1000;
493 memcpy(mem->vm[0].name, "KVMguest", 8);
494 ASCEBC(mem->vm[0].name, 8);
495 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
496 ASCEBC(mem->vm[0].cpi, 16);
497}
498
499static int handle_stsi(struct kvm_vcpu *vcpu)
500{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100501 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
502 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
503 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100504 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100505 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100506 int rc = 0;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300507 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100508
509 vcpu->stat.instruction_stsi++;
510 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
511
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
Thomas Huth87d41fb2013-06-20 17:22:05 +0200515 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200516 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200517 return 0;
518 }
519
520 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
521 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
522 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
523
524 if (fc == 0) {
525 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200526 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200527 return 0;
528 }
529
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300530 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100531
Thomas Huth87d41fb2013-06-20 17:22:05 +0200532 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100533 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
534
535 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100536 case 1: /* same handling for 1 and 2 */
537 case 2:
538 mem = get_zeroed_page(GFP_KERNEL);
539 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100540 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200541 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100542 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100543 break;
544 case 3:
545 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100546 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100547 mem = get_zeroed_page(GFP_KERNEL);
548 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100549 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100550 handle_stsi_3_2_2(vcpu, (void *) mem);
551 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100552 }
553
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300554 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100555 if (rc) {
556 rc = kvm_s390_inject_prog_cond(vcpu, rc);
557 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100558 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200559 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100560 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200561 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100562 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100563 return 0;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100564out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200565 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100566out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100567 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100568 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100569}
570
Cornelia Huckf379aae2012-12-20 15:32:10 +0100571static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100572 [0x02] = handle_stidp,
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200573 [0x04] = handle_set_clock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100574 [0x10] = handle_set_prefix,
575 [0x11] = handle_store_prefix,
576 [0x12] = handle_store_cpu_address,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100577 [0x21] = handle_ipte_interlock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100578 [0x29] = handle_skey,
579 [0x2a] = handle_skey,
580 [0x2b] = handle_skey,
Thomas Huthaca84242013-09-12 10:33:48 +0200581 [0x2c] = handle_test_block,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100582 [0x30] = handle_io_inst,
583 [0x31] = handle_io_inst,
584 [0x32] = handle_io_inst,
585 [0x33] = handle_io_inst,
586 [0x34] = handle_io_inst,
587 [0x35] = handle_io_inst,
588 [0x36] = handle_io_inst,
589 [0x37] = handle_io_inst,
590 [0x38] = handle_io_inst,
591 [0x39] = handle_io_inst,
592 [0x3a] = handle_io_inst,
593 [0x3b] = handle_io_inst,
594 [0x3c] = handle_io_inst,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100595 [0x50] = handle_ipte_interlock,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100596 [0x5f] = handle_io_inst,
597 [0x74] = handle_io_inst,
598 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100599 [0x7d] = handle_stsi,
600 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100601 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100602};
603
Christian Borntraeger70455a32009-01-22 10:28:29 +0100604int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100605{
606 intercept_handler_t handler;
607
Christian Borntraeger70455a32009-01-22 10:28:29 +0100608 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200609 * A lot of B2 instructions are priviledged. Here we check for
610 * the privileged ones, that we can handle in the kernel.
611 * Anything else goes to userspace.
612 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100613 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200614 if (handler)
615 return handler(vcpu);
616
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100617 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100618}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200619
Cornelia Huck48a3e952012-12-20 15:32:09 +0100620static int handle_epsw(struct kvm_vcpu *vcpu)
621{
622 int reg1, reg2;
623
Thomas Huthaeb87c32013-06-12 13:54:57 +0200624 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100625
626 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200627 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100628 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
629 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200630 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100631 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200632 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100633 }
634 return 0;
635}
636
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200637#define PFMF_RESERVED 0xfffc0101UL
638#define PFMF_SK 0x00020000UL
639#define PFMF_CF 0x00010000UL
640#define PFMF_UI 0x00008000UL
641#define PFMF_FSC 0x00007000UL
642#define PFMF_NQ 0x00000800UL
643#define PFMF_MR 0x00000400UL
644#define PFMF_MC 0x00000200UL
645#define PFMF_KEY 0x000000feUL
646
647static int handle_pfmf(struct kvm_vcpu *vcpu)
648{
649 int reg1, reg2;
650 unsigned long start, end;
651
652 vcpu->stat.instruction_pfmf++;
653
654 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
655
656 if (!MACHINE_HAS_PFMF)
657 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
658
659 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200660 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200661
662 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
663 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
664
665 /* Only provide non-quiescing support if the host supports it */
Heiko Carstense769ece2013-07-26 15:04:01 +0200666 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200667 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
668
669 /* No support for conditional-SSKE */
670 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
671 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
672
673 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100674 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200675
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200676 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
677 case 0x00000000:
678 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
679 break;
680 case 0x00001000:
681 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
682 break;
683 /* We dont support EDAT2
684 case 0x00002000:
685 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
686 break;*/
687 default:
688 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
689 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100690
691 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300692 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
Thomas Hutha02689f2014-11-10 15:59:32 +0100693 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
694 }
695
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200696 while (start < end) {
Thomas Huthfb34c6032013-09-09 17:58:38 +0200697 unsigned long useraddr, abs_addr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200698
Thomas Huthfb34c6032013-09-09 17:58:38 +0200699 /* Translate guest address to host address */
700 if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
701 abs_addr = kvm_s390_real_to_abs(vcpu, start);
702 else
703 abs_addr = start;
704 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
705 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200706 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
707
708 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
709 if (clear_user((void __user *)useraddr, PAGE_SIZE))
710 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
711 }
712
713 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200714 int rc = __skey_check_enable(vcpu);
715
716 if (rc)
717 return rc;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200718 if (set_guest_storage_key(current->mm, useraddr,
719 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
720 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
721 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
722 }
723
724 start += PAGE_SIZE;
725 }
726 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
727 vcpu->run->s.regs.gprs[reg2] = end;
728 return 0;
729}
730
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200731static int handle_essa(struct kvm_vcpu *vcpu)
732{
733 /* entries expected to be 1FF */
734 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
735 unsigned long *cbrlo, cbrle;
736 struct gmap *gmap;
737 int i;
738
739 VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
740 gmap = vcpu->arch.gmap;
741 vcpu->stat.instruction_essa++;
Dominik Dingelb31605c2014-03-25 13:47:11 +0100742 if (!kvm_s390_cmma_enabled(vcpu->kvm))
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200743 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
744
745 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
746 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
747
748 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
749 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
750
751 /* Rewind PSW to repeat the ESSA instruction */
Thomas Huth04b41ac2014-11-12 17:13:29 +0100752 kvm_s390_rewind_psw(vcpu, 4);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200753 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
754 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
755 down_read(&gmap->mm->mmap_sem);
756 for (i = 0; i < entries; ++i) {
757 cbrle = cbrlo[i];
758 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
759 /* invalid entry */
760 break;
761 /* try to free backing */
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +0200762 __gmap_zap(gmap, cbrle);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200763 }
764 up_read(&gmap->mm->mmap_sem);
765 if (i < entries)
766 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
767 return 0;
768}
769
Cornelia Huck48a3e952012-12-20 15:32:09 +0100770static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +0100771 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100772 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100773 [0x8e] = handle_ipte_interlock,
774 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200775 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200776 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100777};
778
779int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
780{
781 intercept_handler_t handler;
782
783 /* This is handled just as for the B2 instructions. */
784 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200785 if (handler)
786 return handler(vcpu);
787
Cornelia Huck48a3e952012-12-20 15:32:09 +0100788 return -EOPNOTSUPP;
789}
790
Thomas Huth953ed882013-06-20 17:22:04 +0200791int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
792{
793 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
794 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100795 int reg, rc, nr_regs;
796 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100797 u64 ga;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300798 ar_t ar;
Thomas Huth953ed882013-06-20 17:22:04 +0200799
800 vcpu->stat.instruction_lctl++;
801
802 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
803 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
804
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300805 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +0200806
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100807 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +0200808 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
809
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100810 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
811 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +0200812
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100813 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300814 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100815 if (rc)
816 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +0200817 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100818 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +0200819 do {
Thomas Huth953ed882013-06-20 17:22:04 +0200820 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100821 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +0200822 if (reg == reg3)
823 break;
824 reg = (reg + 1) % 16;
825 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +0100826 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +0200827 return 0;
828}
829
David Hildenbrandaba07502014-01-23 10:47:13 +0100830int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
831{
832 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
833 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100834 int reg, rc, nr_regs;
835 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +0100836 u64 ga;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300837 ar_t ar;
David Hildenbrandaba07502014-01-23 10:47:13 +0100838
839 vcpu->stat.instruction_stctl++;
840
841 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
842 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
843
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300844 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +0100845
846 if (ga & 3)
847 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
848
849 VCPU_EVENT(vcpu, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
850 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
851
852 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100853 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100854 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100855 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +0100856 if (reg == reg3)
857 break;
858 reg = (reg + 1) % 16;
859 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300860 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100861 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100862}
863
Thomas Huth953ed882013-06-20 17:22:04 +0200864static int handle_lctlg(struct kvm_vcpu *vcpu)
865{
866 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
867 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100868 int reg, rc, nr_regs;
869 u64 ctl_array[16];
870 u64 ga;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300871 ar_t ar;
Thomas Huth953ed882013-06-20 17:22:04 +0200872
873 vcpu->stat.instruction_lctlg++;
874
875 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
876 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
877
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300878 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +0200879
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100880 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +0200881 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
882
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100883 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
884 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +0200885
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100886 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300887 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100888 if (rc)
889 return kvm_s390_inject_prog_cond(vcpu, rc);
890 reg = reg1;
891 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +0200892 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100893 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +0200894 if (reg == reg3)
895 break;
896 reg = (reg + 1) % 16;
897 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +0100898 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +0200899 return 0;
900}
901
David Hildenbrandaba07502014-01-23 10:47:13 +0100902static int handle_stctg(struct kvm_vcpu *vcpu)
903{
904 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
905 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100906 int reg, rc, nr_regs;
907 u64 ctl_array[16];
908 u64 ga;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300909 ar_t ar;
David Hildenbrandaba07502014-01-23 10:47:13 +0100910
911 vcpu->stat.instruction_stctg++;
912
913 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
914 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
915
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300916 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +0100917
918 if (ga & 7)
919 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
920
David Hildenbrandaba07502014-01-23 10:47:13 +0100921 VCPU_EVENT(vcpu, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
922 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
923
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100924 reg = reg1;
925 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100926 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100927 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +0100928 if (reg == reg3)
929 break;
930 reg = (reg + 1) % 16;
931 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300932 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100933 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100934}
935
Cornelia Huckf379aae2012-12-20 15:32:10 +0100936static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +0200937 [0x2f] = handle_lctlg,
David Hildenbrandaba07502014-01-23 10:47:13 +0100938 [0x25] = handle_stctg,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100939};
940
Thomas Huth953ed882013-06-20 17:22:04 +0200941int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +0100942{
943 intercept_handler_t handler;
944
Cornelia Huckf379aae2012-12-20 15:32:10 +0100945 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
946 if (handler)
947 return handler(vcpu);
948 return -EOPNOTSUPP;
949}
950
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200951static int handle_tprot(struct kvm_vcpu *vcpu)
952{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100953 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +0100954 unsigned long hva, gpa;
955 int ret = 0, cc = 0;
956 bool writable;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300957 ar_t ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200958
959 vcpu->stat.instruction_tprot++;
960
Thomas Huthf9f6bbc2013-06-20 17:22:00 +0200961 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
962 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
963
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300964 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100965
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200966 /* we only handle the Linux memory detection case:
967 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200968 * everything else goes to userspace. */
969 if (address2 & 0xf0)
970 return -EOPNOTSUPP;
971 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +0100972 ipte_lock(vcpu);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300973 ret = guest_translate_address(vcpu, address1, ar, &gpa, 1);
Thomas Hutha0465f92014-02-04 14:48:07 +0100974 if (ret == PGM_PROTECTION) {
975 /* Write protected? Try again with read-only... */
976 cc = 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300977 ret = guest_translate_address(vcpu, address1, ar, &gpa, 0);
Thomas Hutha0465f92014-02-04 14:48:07 +0100978 }
979 if (ret) {
980 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
981 ret = kvm_s390_inject_program_int(vcpu, ret);
982 } else if (ret > 0) {
983 /* Translation not available */
984 kvm_s390_set_psw_cc(vcpu, 3);
985 ret = 0;
986 }
987 goto out_unlock;
988 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200989
Thomas Hutha0465f92014-02-04 14:48:07 +0100990 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
991 if (kvm_is_error_hva(hva)) {
992 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
993 } else {
994 if (!writable)
995 cc = 1; /* Write not permitted ==> read-only */
996 kvm_s390_set_psw_cc(vcpu, cc);
997 /* Note: CC2 only occurs for storage keys (not supported yet) */
998 }
999out_unlock:
1000 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1001 ipte_unlock(vcpu);
1002 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001003}
1004
1005int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1006{
1007 /* For e5xx... instructions we only handle TPROT */
1008 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1009 return handle_tprot(vcpu);
1010 return -EOPNOTSUPP;
1011}
1012
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001013static int handle_sckpf(struct kvm_vcpu *vcpu)
1014{
1015 u32 value;
1016
1017 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001018 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001019
1020 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1021 return kvm_s390_inject_program_int(vcpu,
1022 PGM_SPECIFICATION);
1023
1024 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1025 vcpu->arch.sie_block->todpr = value;
1026
1027 return 0;
1028}
1029
Cornelia Huck77975352012-12-20 15:32:06 +01001030static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001031 [0x07] = handle_sckpf,
1032};
1033
1034int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1035{
1036 intercept_handler_t handler;
1037
1038 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1039 if (handler)
1040 return handler(vcpu);
1041 return -EOPNOTSUPP;
1042}