blob: b8327b8fdb8f9da7be04828acbc44e9b953aa436 [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>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010026#include <asm/gmap.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020027#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010028#include <asm/ptrace.h>
29#include <asm/compat.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010030#include "gaccess.h"
31#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020032#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010033
Thomas Huth6a3f95a2013-09-12 10:33:49 +020034/* Handle SCK (SET CLOCK) interception */
35static int handle_set_clock(struct kvm_vcpu *vcpu)
36{
David Hildenbrand25ed1672015-05-12 09:49:14 +020037 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030038 ar_t ar;
David Hildenbrand25ed1672015-05-12 09:49:14 +020039 u64 op2, val;
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
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030044 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020045 if (op2 & 7) /* Operand must be on a doubleword boundary */
46 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030047 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010048 if (rc)
49 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020050
Christian Borntraeger7cbde762015-07-21 12:44:57 +020051 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
David Hildenbrand25ed1672015-05-12 09:49:14 +020052 kvm_s390_set_tod_clock(vcpu->kvm, val);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020053
54 kvm_s390_set_psw_cc(vcpu, 0);
55 return 0;
56}
57
Christian Borntraeger453423d2008-03-25 18:47:29 +010058static int handle_set_prefix(struct kvm_vcpu *vcpu)
59{
Christian Borntraeger453423d2008-03-25 18:47:29 +010060 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +010061 u32 address;
62 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030063 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +010064
65 vcpu->stat.instruction_spx++;
66
Thomas Huth5087dfa2013-06-20 17:22:01 +020067 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
68 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
69
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030070 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +010071
72 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010073 if (operand2 & 3)
74 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010075
76 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030077 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstens665170c2014-01-01 16:47:12 +010078 if (rc)
79 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +010080
Heiko Carstens665170c2014-01-01 16:47:12 +010081 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +010082
Heiko Carstens665170c2014-01-01 16:47:12 +010083 /*
84 * Make sure the new value is valid memory. We only need to check the
85 * first page, since address is 8k aligned and memory pieces are always
86 * at least 1MB aligned and have at least a size of 1MB.
87 */
88 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010089 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010090
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010091 kvm_s390_set_prefix(vcpu, address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020092 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010093 return 0;
94}
95
96static int handle_store_prefix(struct kvm_vcpu *vcpu)
97{
Christian Borntraeger453423d2008-03-25 18:47:29 +010098 u64 operand2;
99 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100100 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300101 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100102
103 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100104
Thomas Huth5087dfa2013-06-20 17:22:01 +0200105 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
106 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
107
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300108 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100109
110 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100111 if (operand2 & 3)
112 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100113
Michael Muellerfda902c2014-05-13 16:58:30 +0200114 address = kvm_s390_get_prefix(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100115
116 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300117 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100118 if (rc)
119 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100120
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200121 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200122 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100123 return 0;
124}
125
126static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
127{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100128 u16 vcpu_id = vcpu->vcpu_id;
129 u64 ga;
130 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300131 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100132
133 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100134
Thomas Huth5087dfa2013-06-20 17:22:01 +0200135 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
136 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
137
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300138 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100139
Heiko Carstens8b96de02014-01-01 16:53:27 +0100140 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100141 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100142
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300143 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
Heiko Carstens8b96de02014-01-01 16:53:27 +0100144 if (rc)
145 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100146
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200147 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
Heiko Carstens8b96de02014-01-01 16:53:27 +0100148 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100149 return 0;
150}
151
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200152static int __skey_check_enable(struct kvm_vcpu *vcpu)
Dominik Dingel693ffc02014-01-14 18:11:14 +0100153{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200154 int rc = 0;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100155 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200156 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100157
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200158 rc = s390_enable_skey();
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200159 VCPU_EVENT(vcpu, 3, "%s", "enabling storage keys for guest");
Dominik Dingel693ffc02014-01-14 18:11:14 +0100160 trace_kvm_s390_skey_related_inst(vcpu);
161 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200162 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100163}
164
165
Christian Borntraeger453423d2008-03-25 18:47:29 +0100166static int handle_skey(struct kvm_vcpu *vcpu)
167{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200168 int rc = __skey_check_enable(vcpu);
Dominik Dingel693ffc02014-01-14 18:11:14 +0100169
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200170 if (rc)
171 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100172 vcpu->stat.instruction_storage_key++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200173
174 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
175 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
176
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100177 kvm_s390_retry_instr(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100178 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
179 return 0;
180}
181
Heiko Carstens8a2422342014-01-10 14:33:28 +0100182static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
183{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100184 vcpu->stat.instruction_ipte_interlock++;
Thomas Huth04b41ac2014-11-12 17:13:29 +0100185 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100186 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
187 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100188 kvm_s390_retry_instr(vcpu);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100189 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
190 return 0;
191}
192
Thomas Huthaca84242013-09-12 10:33:48 +0200193static int handle_test_block(struct kvm_vcpu *vcpu)
194{
Thomas Huthaca84242013-09-12 10:33:48 +0200195 gpa_t addr;
196 int reg2;
197
198 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
199 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
200
201 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
202 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100203 addr = kvm_s390_logical_to_effective(vcpu, addr);
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300204 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
Thomas Huthe45efa22014-03-07 12:14:23 +0100205 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200206 addr = kvm_s390_real_to_abs(vcpu, addr);
207
Heiko Carstensef23e772014-01-01 16:53:49 +0100208 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200209 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
210 /*
211 * We don't expect errors on modern systems, and do not care
212 * about storage keys (yet), so let's just clear the page.
213 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100214 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200215 return -EFAULT;
216 kvm_s390_set_psw_cc(vcpu, 0);
217 vcpu->run->s.regs.gprs[0] = 0;
218 return 0;
219}
220
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100221static int handle_tpi(struct kvm_vcpu *vcpu)
222{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100223 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100224 unsigned long len;
225 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100226 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100227 u64 addr;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300228 ar_t ar;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100229
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300230 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100231 if (addr & 3)
232 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100233
Thomas Huthf0926692013-10-09 14:15:54 +0200234 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100235 if (!inti) {
236 kvm_s390_set_psw_cc(vcpu, 0);
237 return 0;
238 }
239
Heiko Carstens4799b552014-01-01 16:55:48 +0100240 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
241 tpi_data[1] = inti->io.io_int_parm;
242 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100243 if (addr) {
244 /*
245 * Store the two-word I/O interruption code into the
246 * provided area.
247 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100248 len = sizeof(tpi_data) - 4;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300249 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100250 if (rc) {
251 rc = kvm_s390_inject_prog_cond(vcpu, rc);
252 goto reinject_interrupt;
253 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100254 } else {
255 /*
256 * Store the three-word I/O interruption code into
257 * the appropriate lowcore area.
258 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100259 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100260 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
261 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100262 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100263 goto reinject_interrupt;
264 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100265 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100266
267 /* irq was successfully handed to the guest */
268 kfree(inti);
269 kvm_s390_set_psw_cc(vcpu, 1);
270 return 0;
271reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100272 /*
273 * If we encounter a problem storing the interruption code, the
274 * instruction is suppressed from the guest's view: reinject the
275 * interrupt.
276 */
David Hildenbrand15462e32015-02-04 15:59:11 +0100277 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
278 kfree(inti);
279 rc = -EFAULT;
280 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100281 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100282 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100283}
284
285static int handle_tsch(struct kvm_vcpu *vcpu)
286{
Jens Freimann6d3da242013-07-03 15:18:35 +0200287 struct kvm_s390_interrupt_info *inti = NULL;
288 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100289
Jens Freimann6d3da242013-07-03 15:18:35 +0200290 /* a valid schid has at least one bit set */
291 if (vcpu->run->s.regs.gprs[1])
292 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
293 vcpu->run->s.regs.gprs[1]);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100294
295 /*
296 * Prepare exit to userspace.
297 * We indicate whether we dequeued a pending I/O interrupt
298 * so that userspace can re-inject it if the instruction gets
299 * a program check. While this may re-order the pending I/O
300 * interrupts, this is no problem since the priority is kept
301 * intact.
302 */
303 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
304 vcpu->run->s390_tsch.dequeued = !!inti;
305 if (inti) {
306 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
307 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
308 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
309 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
310 }
311 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
312 kfree(inti);
313 return -EREMOTE;
314}
315
Cornelia Huckf379aae2012-12-20 15:32:10 +0100316static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100317{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100318 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100319
Thomas Huth5087dfa2013-06-20 17:22:01 +0200320 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
321 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
322
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100323 if (vcpu->kvm->arch.css_support) {
324 /*
325 * Most I/O instructions will be handled by userspace.
326 * Exceptions are tpi and the interrupt portion of tsch.
327 */
328 if (vcpu->arch.sie_block->ipa == 0xb236)
329 return handle_tpi(vcpu);
330 if (vcpu->arch.sie_block->ipa == 0xb235)
331 return handle_tsch(vcpu);
332 /* Handle in userspace. */
333 return -EOPNOTSUPP;
334 } else {
335 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100336 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100337 * I/O instructions.
338 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200339 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100340 return 0;
341 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100342}
343
Christian Borntraeger453423d2008-03-25 18:47:29 +0100344static int handle_stfl(struct kvm_vcpu *vcpu)
345{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100346 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100347 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100348
349 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200350
351 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
352 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
353
Michael Mueller9d8d5782015-02-02 15:42:51 +0100354 /*
355 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
356 * into a u32 memory representation. They will remain bits 0-31.
357 */
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100358 fac = *vcpu->kvm->arch.model.fac_list >> 32;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100359 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100360 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100361 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100362 return rc;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200363 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100364 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100365 return 0;
366}
367
Cornelia Huck48a3e952012-12-20 15:32:09 +0100368#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
369#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100370#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100371#define PSW_ADDR_31 0x000000007fffffffUL
372
Thomas Hutha3fb5772014-04-17 09:10:40 +0200373int is_valid_psw(psw_t *psw)
374{
Heiko Carstens3736b872013-03-25 17:22:52 +0100375 if (psw->mask & PSW_MASK_UNASSIGNED)
376 return 0;
377 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
378 if (psw->addr & ~PSW_ADDR_31)
379 return 0;
380 }
381 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
382 return 0;
383 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
384 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200385 if (psw->addr & 1)
386 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100387 return 1;
388}
389
Cornelia Huck48a3e952012-12-20 15:32:09 +0100390int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
391{
Heiko Carstens3736b872013-03-25 17:22:52 +0100392 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100393 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100394 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100395 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300396 ar_t ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100397
Heiko Carstens3736b872013-03-25 17:22:52 +0100398 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200399 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
400
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300401 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100402 if (addr & 7)
403 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100404
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300405 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100406 if (rc)
407 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100408 if (!(new_psw.mask & PSW32_MASK_BASE))
409 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100410 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
411 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
412 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
413 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100414 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100415 return 0;
416}
417
418static int handle_lpswe(struct kvm_vcpu *vcpu)
419{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100420 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100421 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100422 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300423 ar_t ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100424
Thomas Huth5087dfa2013-06-20 17:22:01 +0200425 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
426 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
427
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300428 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100429 if (addr & 7)
430 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300431 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100432 if (rc)
433 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100434 vcpu->arch.sie_block->gpsw = new_psw;
435 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100436 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100437 return 0;
438}
439
Christian Borntraeger453423d2008-03-25 18:47:29 +0100440static int handle_stidp(struct kvm_vcpu *vcpu)
441{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200442 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100443 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100444 int rc;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300445 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100446
447 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100448
Thomas Huth5087dfa2013-06-20 17:22:01 +0200449 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
450 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
451
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300452 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100453
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100454 if (operand2 & 7)
455 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100456
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300457 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
Heiko Carstens7d777d72014-01-01 16:58:16 +0100458 if (rc)
459 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100460
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200461 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100462 return 0;
463}
464
465static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
466{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100467 int cpus = 0;
468 int n;
469
Jens Freimannff520a62014-02-24 10:11:41 +0100470 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100471
472 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200473 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100474 mem->count = 0;
475 if (mem->count < 8)
476 mem->count++;
477 for (n = mem->count - 1; n > 0 ; n--)
478 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
479
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100480 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100481 mem->vm[0].cpus_total = cpus;
482 mem->vm[0].cpus_configured = cpus;
483 mem->vm[0].cpus_standby = 0;
484 mem->vm[0].cpus_reserved = 0;
485 mem->vm[0].caf = 1000;
486 memcpy(mem->vm[0].name, "KVMguest", 8);
487 ASCEBC(mem->vm[0].name, 8);
488 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
489 ASCEBC(mem->vm[0].cpi, 16);
490}
491
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100492static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, ar_t ar,
493 u8 fc, u8 sel1, u16 sel2)
494{
495 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
496 vcpu->run->s390_stsi.addr = addr;
497 vcpu->run->s390_stsi.ar = ar;
498 vcpu->run->s390_stsi.fc = fc;
499 vcpu->run->s390_stsi.sel1 = sel1;
500 vcpu->run->s390_stsi.sel2 = sel2;
501}
502
Christian Borntraeger453423d2008-03-25 18:47:29 +0100503static int handle_stsi(struct kvm_vcpu *vcpu)
504{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100505 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
506 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
507 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100508 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100509 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100510 int rc = 0;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300511 ar_t ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100512
513 vcpu->stat.instruction_stsi++;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200514 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100515
Thomas Huth5087dfa2013-06-20 17:22:01 +0200516 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
517 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
518
Thomas Huth87d41fb2013-06-20 17:22:05 +0200519 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200520 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200521 return 0;
522 }
523
524 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
525 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
526 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
527
528 if (fc == 0) {
529 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200530 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200531 return 0;
532 }
533
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300534 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100535
Thomas Huth87d41fb2013-06-20 17:22:05 +0200536 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100537 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
538
539 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100540 case 1: /* same handling for 1 and 2 */
541 case 2:
542 mem = get_zeroed_page(GFP_KERNEL);
543 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100544 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200545 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100546 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100547 break;
548 case 3:
549 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100550 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100551 mem = get_zeroed_page(GFP_KERNEL);
552 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100553 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100554 handle_stsi_3_2_2(vcpu, (void *) mem);
555 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100556 }
557
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300558 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100559 if (rc) {
560 rc = kvm_s390_inject_prog_cond(vcpu, rc);
561 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100562 }
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100563 if (vcpu->kvm->arch.user_stsi) {
564 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
565 rc = -EREMOTE;
566 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200567 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100568 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200569 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100570 vcpu->run->s.regs.gprs[0] = 0;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100571 return rc;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100572out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200573 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100574out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100575 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100576 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100577}
578
Cornelia Huckf379aae2012-12-20 15:32:10 +0100579static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100580 [0x02] = handle_stidp,
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200581 [0x04] = handle_set_clock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100582 [0x10] = handle_set_prefix,
583 [0x11] = handle_store_prefix,
584 [0x12] = handle_store_cpu_address,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100585 [0x21] = handle_ipte_interlock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100586 [0x29] = handle_skey,
587 [0x2a] = handle_skey,
588 [0x2b] = handle_skey,
Thomas Huthaca84242013-09-12 10:33:48 +0200589 [0x2c] = handle_test_block,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100590 [0x30] = handle_io_inst,
591 [0x31] = handle_io_inst,
592 [0x32] = handle_io_inst,
593 [0x33] = handle_io_inst,
594 [0x34] = handle_io_inst,
595 [0x35] = handle_io_inst,
596 [0x36] = handle_io_inst,
597 [0x37] = handle_io_inst,
598 [0x38] = handle_io_inst,
599 [0x39] = handle_io_inst,
600 [0x3a] = handle_io_inst,
601 [0x3b] = handle_io_inst,
602 [0x3c] = handle_io_inst,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100603 [0x50] = handle_ipte_interlock,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100604 [0x5f] = handle_io_inst,
605 [0x74] = handle_io_inst,
606 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100607 [0x7d] = handle_stsi,
608 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100609 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100610};
611
Christian Borntraeger70455a32009-01-22 10:28:29 +0100612int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100613{
614 intercept_handler_t handler;
615
Christian Borntraeger70455a32009-01-22 10:28:29 +0100616 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200617 * A lot of B2 instructions are priviledged. Here we check for
618 * the privileged ones, that we can handle in the kernel.
619 * Anything else goes to userspace.
620 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100621 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200622 if (handler)
623 return handler(vcpu);
624
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100625 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100626}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200627
Cornelia Huck48a3e952012-12-20 15:32:09 +0100628static int handle_epsw(struct kvm_vcpu *vcpu)
629{
630 int reg1, reg2;
631
Thomas Huthaeb87c32013-06-12 13:54:57 +0200632 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100633
634 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200635 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100636 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
637 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200638 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100639 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200640 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100641 }
642 return 0;
643}
644
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200645#define PFMF_RESERVED 0xfffc0101UL
646#define PFMF_SK 0x00020000UL
647#define PFMF_CF 0x00010000UL
648#define PFMF_UI 0x00008000UL
649#define PFMF_FSC 0x00007000UL
650#define PFMF_NQ 0x00000800UL
651#define PFMF_MR 0x00000400UL
652#define PFMF_MC 0x00000200UL
653#define PFMF_KEY 0x000000feUL
654
655static int handle_pfmf(struct kvm_vcpu *vcpu)
656{
David Hildenbrand1824c722016-05-10 09:43:11 +0200657 bool mr = false, mc = false, nq;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200658 int reg1, reg2;
659 unsigned long start, end;
David Hildenbrand1824c722016-05-10 09:43:11 +0200660 unsigned char key;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200661
662 vcpu->stat.instruction_pfmf++;
663
664 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
665
Heiko Carstens03c02802015-11-13 13:31:58 +0100666 if (!test_kvm_facility(vcpu->kvm, 8))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200667 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
668
669 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200670 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200671
672 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
673 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
674
David Hildenbrandedc5b052016-03-04 11:08:09 +0100675 /* Only provide non-quiescing support if enabled for the guest */
676 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
677 !test_kvm_facility(vcpu->kvm, 14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200678 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
679
David Hildenbrand1824c722016-05-10 09:43:11 +0200680 /* Only provide conditional-SSKE support if enabled for the guest */
681 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
682 test_kvm_facility(vcpu->kvm, 10)) {
683 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
684 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
685 }
686
687 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
688 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200689 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100690 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200691
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200692 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
693 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
694 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
695 }
696
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200697 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
698 case 0x00000000:
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200699 /* only 4k frames specify a real address */
700 start = kvm_s390_real_to_abs(vcpu, start);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200701 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
702 break;
703 case 0x00001000:
704 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
705 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200706 case 0x00002000:
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100707 /* only support 2G frame size if EDAT2 is available and we are
708 not in 24-bit addressing mode */
709 if (!test_kvm_facility(vcpu->kvm, 78) ||
710 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
711 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200712 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100713 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200714 default:
715 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
716 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100717
David Hildenbrand695be0e2016-05-12 14:07:05 +0200718 while (start != end) {
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200719 unsigned long useraddr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200720
Thomas Huthfb34c6032013-09-09 17:58:38 +0200721 /* Translate guest address to host address */
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200722 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
Thomas Huthfb34c6032013-09-09 17:58:38 +0200723 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200724 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
725
726 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
727 if (clear_user((void __user *)useraddr, PAGE_SIZE))
728 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
729 }
730
731 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200732 int rc = __skey_check_enable(vcpu);
733
734 if (rc)
735 return rc;
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100736 down_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200737 rc = cond_set_guest_storage_key(current->mm, useraddr,
738 key, NULL, nq, mr, mc);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100739 up_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200740 if (rc < 0)
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200741 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
742 }
743
744 start += PAGE_SIZE;
745 }
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200746 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
747 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
748 vcpu->run->s.regs.gprs[reg2] = end;
749 } else {
750 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
751 end = kvm_s390_logical_to_effective(vcpu, end);
752 vcpu->run->s.regs.gprs[reg2] |= end;
753 }
754 }
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200755 return 0;
756}
757
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200758static int handle_essa(struct kvm_vcpu *vcpu)
759{
760 /* entries expected to be 1FF */
761 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200762 unsigned long *cbrlo;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200763 struct gmap *gmap;
764 int i;
765
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200766 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200767 gmap = vcpu->arch.gmap;
768 vcpu->stat.instruction_essa++;
Dominik Dingele6db1d62015-05-07 15:41:57 +0200769 if (!vcpu->kvm->arch.use_cmma)
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200770 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
771
772 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
773 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
774
775 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
776 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
777
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100778 /* Retry the ESSA instruction */
779 kvm_s390_retry_instr(vcpu);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200780 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
781 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
782 down_read(&gmap->mm->mmap_sem);
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200783 for (i = 0; i < entries; ++i)
784 __gmap_zap(gmap, cbrlo[i]);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200785 up_read(&gmap->mm->mmap_sem);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200786 return 0;
787}
788
Cornelia Huck48a3e952012-12-20 15:32:09 +0100789static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +0100790 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100791 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100792 [0x8e] = handle_ipte_interlock,
793 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200794 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200795 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100796};
797
798int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
799{
800 intercept_handler_t handler;
801
802 /* This is handled just as for the B2 instructions. */
803 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200804 if (handler)
805 return handler(vcpu);
806
Cornelia Huck48a3e952012-12-20 15:32:09 +0100807 return -EOPNOTSUPP;
808}
809
Thomas Huth953ed882013-06-20 17:22:04 +0200810int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
811{
812 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
813 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100814 int reg, rc, nr_regs;
815 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100816 u64 ga;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300817 ar_t ar;
Thomas Huth953ed882013-06-20 17:22:04 +0200818
819 vcpu->stat.instruction_lctl++;
820
821 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
822 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
823
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300824 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +0200825
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100826 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +0200827 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
828
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200829 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100830 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +0200831
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100832 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300833 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100834 if (rc)
835 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +0200836 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100837 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +0200838 do {
Thomas Huth953ed882013-06-20 17:22:04 +0200839 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100840 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +0200841 if (reg == reg3)
842 break;
843 reg = (reg + 1) % 16;
844 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +0100845 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +0200846 return 0;
847}
848
David Hildenbrandaba07502014-01-23 10:47:13 +0100849int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
850{
851 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
852 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100853 int reg, rc, nr_regs;
854 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +0100855 u64 ga;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300856 ar_t ar;
David Hildenbrandaba07502014-01-23 10:47:13 +0100857
858 vcpu->stat.instruction_stctl++;
859
860 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
861 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
862
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300863 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +0100864
865 if (ga & 3)
866 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
867
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200868 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +0100869 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
870
871 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100872 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100873 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100874 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +0100875 if (reg == reg3)
876 break;
877 reg = (reg + 1) % 16;
878 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300879 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100880 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100881}
882
Thomas Huth953ed882013-06-20 17:22:04 +0200883static int handle_lctlg(struct kvm_vcpu *vcpu)
884{
885 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
886 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100887 int reg, rc, nr_regs;
888 u64 ctl_array[16];
889 u64 ga;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300890 ar_t ar;
Thomas Huth953ed882013-06-20 17:22:04 +0200891
892 vcpu->stat.instruction_lctlg++;
893
894 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
895 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
896
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300897 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +0200898
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100899 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +0200900 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
901
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200902 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +0100903 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +0200904
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100905 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300906 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100907 if (rc)
908 return kvm_s390_inject_prog_cond(vcpu, rc);
909 reg = reg1;
910 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +0200911 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100912 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +0200913 if (reg == reg3)
914 break;
915 reg = (reg + 1) % 16;
916 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +0100917 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +0200918 return 0;
919}
920
David Hildenbrandaba07502014-01-23 10:47:13 +0100921static int handle_stctg(struct kvm_vcpu *vcpu)
922{
923 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
924 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100925 int reg, rc, nr_regs;
926 u64 ctl_array[16];
927 u64 ga;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300928 ar_t ar;
David Hildenbrandaba07502014-01-23 10:47:13 +0100929
930 vcpu->stat.instruction_stctg++;
931
932 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
933 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
934
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300935 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +0100936
937 if (ga & 7)
938 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
939
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200940 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +0100941 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
942
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100943 reg = reg1;
944 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100945 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100946 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +0100947 if (reg == reg3)
948 break;
949 reg = (reg + 1) % 16;
950 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300951 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +0100952 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +0100953}
954
Cornelia Huckf379aae2012-12-20 15:32:10 +0100955static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +0200956 [0x2f] = handle_lctlg,
David Hildenbrandaba07502014-01-23 10:47:13 +0100957 [0x25] = handle_stctg,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100958};
959
Thomas Huth953ed882013-06-20 17:22:04 +0200960int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +0100961{
962 intercept_handler_t handler;
963
Cornelia Huckf379aae2012-12-20 15:32:10 +0100964 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
965 if (handler)
966 return handler(vcpu);
967 return -EOPNOTSUPP;
968}
969
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200970static int handle_tprot(struct kvm_vcpu *vcpu)
971{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100972 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +0100973 unsigned long hva, gpa;
974 int ret = 0, cc = 0;
975 bool writable;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300976 ar_t ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200977
978 vcpu->stat.instruction_tprot++;
979
Thomas Huthf9f6bbc2013-06-20 17:22:00 +0200980 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
981 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
982
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300983 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100984
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200985 /* we only handle the Linux memory detection case:
986 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200987 * everything else goes to userspace. */
988 if (address2 & 0xf0)
989 return -EOPNOTSUPP;
990 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +0100991 ipte_lock(vcpu);
David Hildenbrand92c96322015-11-16 15:42:11 +0100992 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
Thomas Hutha0465f92014-02-04 14:48:07 +0100993 if (ret == PGM_PROTECTION) {
994 /* Write protected? Try again with read-only... */
995 cc = 1;
David Hildenbrand92c96322015-11-16 15:42:11 +0100996 ret = guest_translate_address(vcpu, address1, ar, &gpa,
997 GACC_FETCH);
Thomas Hutha0465f92014-02-04 14:48:07 +0100998 }
999 if (ret) {
1000 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1001 ret = kvm_s390_inject_program_int(vcpu, ret);
1002 } else if (ret > 0) {
1003 /* Translation not available */
1004 kvm_s390_set_psw_cc(vcpu, 3);
1005 ret = 0;
1006 }
1007 goto out_unlock;
1008 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001009
Thomas Hutha0465f92014-02-04 14:48:07 +01001010 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1011 if (kvm_is_error_hva(hva)) {
1012 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1013 } else {
1014 if (!writable)
1015 cc = 1; /* Write not permitted ==> read-only */
1016 kvm_s390_set_psw_cc(vcpu, cc);
1017 /* Note: CC2 only occurs for storage keys (not supported yet) */
1018 }
1019out_unlock:
1020 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1021 ipte_unlock(vcpu);
1022 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001023}
1024
1025int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1026{
1027 /* For e5xx... instructions we only handle TPROT */
1028 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1029 return handle_tprot(vcpu);
1030 return -EOPNOTSUPP;
1031}
1032
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001033static int handle_sckpf(struct kvm_vcpu *vcpu)
1034{
1035 u32 value;
1036
1037 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001038 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001039
1040 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1041 return kvm_s390_inject_program_int(vcpu,
1042 PGM_SPECIFICATION);
1043
1044 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1045 vcpu->arch.sie_block->todpr = value;
1046
1047 return 0;
1048}
1049
Cornelia Huck77975352012-12-20 15:32:06 +01001050static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001051 [0x07] = handle_sckpf,
1052};
1053
1054int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1055{
1056 intercept_handler_t handler;
1057
1058 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1059 if (handler)
1060 return handler(vcpu);
1061 return -EOPNOTSUPP;
1062}