blob: 0ffe973535fab86194818cc5a86b8c1de084b1cb [file] [log] [blame]
Christian Borntraeger453423d2008-03-25 18:47:29 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling privileged instructions
Christian Borntraeger453423d2008-03-25 18:47:29 +01003 *
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02004 * Copyright IBM Corp. 2008, 2013
Christian Borntraeger453423d2008-03-25 18:47:29 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 */
13
14#include <linux/kvm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010016#include <linux/errno.h>
Heiko Carstensb13b5dc2013-03-25 17:22:55 +010017#include <linux/compat.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010018#include <linux/mm_types.h>
19
Heiko Carstens7c959e82013-03-05 13:14:46 +010020#include <asm/asm-offsets.h>
Heiko Carstense769ece2013-07-26 15:04:01 +020021#include <asm/facility.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010022#include <asm/current.h>
23#include <asm/debug.h>
24#include <asm/ebcdic.h>
25#include <asm/sysinfo.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020026#include <asm/pgtable.h>
27#include <asm/pgalloc.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010028#include <asm/gmap.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020029#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010030#include <asm/ptrace.h>
31#include <asm/compat.h>
David Hildenbranda7e19ab2016-05-10 09:50:21 +020032#include <asm/sclp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010033#include "gaccess.h"
34#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020035#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010036
Fan Zhang80cd8762016-08-15 04:53:22 +020037static int handle_ri(struct kvm_vcpu *vcpu)
38{
39 if (test_kvm_facility(vcpu->kvm, 64)) {
Christian Borntraeger4d5f2c02017-02-09 17:15:41 +010040 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
David Hildenbrand0c9d8682017-03-13 11:48:28 +010041 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
Fan Zhang80cd8762016-08-15 04:53:22 +020042 kvm_s390_retry_instr(vcpu);
43 return 0;
44 } else
45 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
46}
47
48int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
49{
50 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
51 return handle_ri(vcpu);
52 else
53 return -EOPNOTSUPP;
54}
55
Fan Zhang4e0b1ab2016-11-29 07:17:55 +010056static int handle_gs(struct kvm_vcpu *vcpu)
57{
58 if (test_kvm_facility(vcpu->kvm, 133)) {
59 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
60 preempt_disable();
61 __ctl_set_bit(2, 4);
62 current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
63 restore_gs_cb(current->thread.gs_cb);
64 preempt_enable();
65 vcpu->arch.sie_block->ecb |= ECB_GS;
66 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
67 vcpu->arch.gs_enabled = 1;
68 kvm_s390_retry_instr(vcpu);
69 return 0;
70 } else
71 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
72}
73
74int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
75{
76 int code = vcpu->arch.sie_block->ipb & 0xff;
77
78 if (code == 0x49 || code == 0x4d)
79 return handle_gs(vcpu);
80 else
81 return -EOPNOTSUPP;
82}
Thomas Huth6a3f95a2013-09-12 10:33:49 +020083/* Handle SCK (SET CLOCK) interception */
84static int handle_set_clock(struct kvm_vcpu *vcpu)
85{
David Hildenbrand25ed1672015-05-12 09:49:14 +020086 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +010087 u8 ar;
David Hildenbrand25ed1672015-05-12 09:49:14 +020088 u64 op2, val;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020089
90 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
91 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
92
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030093 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020094 if (op2 & 7) /* Operand must be on a doubleword boundary */
95 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +030096 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010097 if (rc)
98 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020099
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200100 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
David Hildenbrand25ed1672015-05-12 09:49:14 +0200101 kvm_s390_set_tod_clock(vcpu->kvm, val);
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200102
103 kvm_s390_set_psw_cc(vcpu, 0);
104 return 0;
105}
106
Christian Borntraeger453423d2008-03-25 18:47:29 +0100107static int handle_set_prefix(struct kvm_vcpu *vcpu)
108{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100109 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +0100110 u32 address;
111 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100112 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100113
114 vcpu->stat.instruction_spx++;
115
Thomas Huth5087dfa2013-06-20 17:22:01 +0200116 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
117 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
118
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300119 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100120
121 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100122 if (operand2 & 3)
123 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100124
125 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300126 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstens665170c2014-01-01 16:47:12 +0100127 if (rc)
128 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100129
Heiko Carstens665170c2014-01-01 16:47:12 +0100130 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100131
Heiko Carstens665170c2014-01-01 16:47:12 +0100132 /*
133 * Make sure the new value is valid memory. We only need to check the
134 * first page, since address is 8k aligned and memory pieces are always
135 * at least 1MB aligned and have at least a size of 1MB.
136 */
137 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100138 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100139
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100140 kvm_s390_set_prefix(vcpu, address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200141 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100142 return 0;
143}
144
145static int handle_store_prefix(struct kvm_vcpu *vcpu)
146{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100147 u64 operand2;
148 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100149 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100150 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100151
152 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100153
Thomas Huth5087dfa2013-06-20 17:22:01 +0200154 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
155 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
156
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300157 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100158
159 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100160 if (operand2 & 3)
161 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100162
Michael Muellerfda902c2014-05-13 16:58:30 +0200163 address = kvm_s390_get_prefix(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100164
165 /* get the value */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300166 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100167 if (rc)
168 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100169
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200170 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200171 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100172 return 0;
173}
174
175static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
176{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100177 u16 vcpu_id = vcpu->vcpu_id;
178 u64 ga;
179 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100180 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100181
182 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100183
Thomas Huth5087dfa2013-06-20 17:22:01 +0200184 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
185 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
186
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300187 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100188
Heiko Carstens8b96de02014-01-01 16:53:27 +0100189 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100190 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100191
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300192 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
Heiko Carstens8b96de02014-01-01 16:53:27 +0100193 if (rc)
194 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100195
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200196 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
Heiko Carstens8b96de02014-01-01 16:53:27 +0100197 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100198 return 0;
199}
200
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200201static int __skey_check_enable(struct kvm_vcpu *vcpu)
Dominik Dingel693ffc02014-01-14 18:11:14 +0100202{
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200203 int rc = 0;
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200204
205 trace_kvm_s390_skey_related_inst(vcpu);
Dominik Dingel693ffc02014-01-14 18:11:14 +0100206 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200207 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100208
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200209 rc = s390_enable_skey();
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200210 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
211 if (!rc)
212 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200213 return rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100214}
215
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200216static int try_handle_skey(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100217{
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200218 int rc;
Dominik Dingel693ffc02014-01-14 18:11:14 +0100219
David Hildenbrand11ddcd42016-05-10 09:40:09 +0200220 vcpu->stat.instruction_storage_key++;
221 rc = __skey_check_enable(vcpu);
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200222 if (rc)
223 return rc;
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200224 if (sclp.has_skey) {
225 /* with storage-key facility, SIE interprets it for us */
226 kvm_s390_retry_instr(vcpu);
227 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
228 return -EAGAIN;
229 }
Thomas Huth5087dfa2013-06-20 17:22:01 +0200230 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
231 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200232 return 0;
233}
Thomas Huth5087dfa2013-06-20 17:22:01 +0200234
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200235static int handle_iske(struct kvm_vcpu *vcpu)
236{
237 unsigned long addr;
238 unsigned char key;
239 int reg1, reg2;
240 int rc;
241
242 rc = try_handle_skey(vcpu);
243 if (rc)
244 return rc != -EAGAIN ? rc : 0;
245
246 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
247
248 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
249 addr = kvm_s390_logical_to_effective(vcpu, addr);
250 addr = kvm_s390_real_to_abs(vcpu, addr);
251 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
252 if (kvm_is_error_hva(addr))
253 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
254
255 down_read(&current->mm->mmap_sem);
256 rc = get_guest_storage_key(current->mm, addr, &key);
257 up_read(&current->mm->mmap_sem);
258 if (rc)
259 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
260 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
261 vcpu->run->s.regs.gprs[reg1] |= key;
262 return 0;
263}
264
265static int handle_rrbe(struct kvm_vcpu *vcpu)
266{
267 unsigned long addr;
268 int reg1, reg2;
269 int rc;
270
271 rc = try_handle_skey(vcpu);
272 if (rc)
273 return rc != -EAGAIN ? rc : 0;
274
275 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
276
277 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
278 addr = kvm_s390_logical_to_effective(vcpu, addr);
279 addr = kvm_s390_real_to_abs(vcpu, addr);
280 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
281 if (kvm_is_error_hva(addr))
282 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
283
284 down_read(&current->mm->mmap_sem);
285 rc = reset_guest_reference_bit(current->mm, addr);
286 up_read(&current->mm->mmap_sem);
287 if (rc < 0)
288 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
289
290 kvm_s390_set_psw_cc(vcpu, rc);
291 return 0;
292}
293
294#define SSKE_NQ 0x8
295#define SSKE_MR 0x4
296#define SSKE_MC 0x2
297#define SSKE_MB 0x1
298static int handle_sske(struct kvm_vcpu *vcpu)
299{
300 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
301 unsigned long start, end;
302 unsigned char key, oldkey;
303 int reg1, reg2;
304 int rc;
305
306 rc = try_handle_skey(vcpu);
307 if (rc)
308 return rc != -EAGAIN ? rc : 0;
309
310 if (!test_kvm_facility(vcpu->kvm, 8))
311 m3 &= ~SSKE_MB;
312 if (!test_kvm_facility(vcpu->kvm, 10))
313 m3 &= ~(SSKE_MC | SSKE_MR);
314 if (!test_kvm_facility(vcpu->kvm, 14))
315 m3 &= ~SSKE_NQ;
316
317 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
318
319 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
320 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
321 start = kvm_s390_logical_to_effective(vcpu, start);
322 if (m3 & SSKE_MB) {
323 /* start already designates an absolute address */
324 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
325 } else {
326 start = kvm_s390_real_to_abs(vcpu, start);
327 end = start + PAGE_SIZE;
328 }
329
330 while (start != end) {
331 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
332
333 if (kvm_is_error_hva(addr))
334 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
335
336 down_read(&current->mm->mmap_sem);
337 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
338 m3 & SSKE_NQ, m3 & SSKE_MR,
339 m3 & SSKE_MC);
340 up_read(&current->mm->mmap_sem);
341 if (rc < 0)
342 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
343 start += PAGE_SIZE;
Heiko Carstens0b925152017-01-02 08:51:02 +0100344 }
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200345
346 if (m3 & (SSKE_MC | SSKE_MR)) {
347 if (m3 & SSKE_MB) {
348 /* skey in reg1 is unpredictable */
349 kvm_s390_set_psw_cc(vcpu, 3);
350 } else {
351 kvm_s390_set_psw_cc(vcpu, rc);
352 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
353 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
354 }
355 }
356 if (m3 & SSKE_MB) {
357 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT)
358 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
359 else
360 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
361 end = kvm_s390_logical_to_effective(vcpu, end);
362 vcpu->run->s.regs.gprs[reg2] |= end;
363 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100364 return 0;
365}
366
Heiko Carstens8a2422342014-01-10 14:33:28 +0100367static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
368{
Heiko Carstens8a2422342014-01-10 14:33:28 +0100369 vcpu->stat.instruction_ipte_interlock++;
Thomas Huth04b41ac2014-11-12 17:13:29 +0100370 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
Heiko Carstens8a2422342014-01-10 14:33:28 +0100371 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
372 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100373 kvm_s390_retry_instr(vcpu);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100374 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
375 return 0;
376}
377
Thomas Huthaca84242013-09-12 10:33:48 +0200378static int handle_test_block(struct kvm_vcpu *vcpu)
379{
Thomas Huthaca84242013-09-12 10:33:48 +0200380 gpa_t addr;
381 int reg2;
382
383 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
384 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
385
386 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
387 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Huthe45efa22014-03-07 12:14:23 +0100388 addr = kvm_s390_logical_to_effective(vcpu, addr);
Alexander Yarygindd9e5b72015-03-03 14:26:14 +0300389 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
Thomas Huthe45efa22014-03-07 12:14:23 +0100390 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
Thomas Huthaca84242013-09-12 10:33:48 +0200391 addr = kvm_s390_real_to_abs(vcpu, addr);
392
Heiko Carstensef23e772014-01-01 16:53:49 +0100393 if (kvm_is_error_gpa(vcpu->kvm, addr))
Thomas Huthaca84242013-09-12 10:33:48 +0200394 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
395 /*
396 * We don't expect errors on modern systems, and do not care
397 * about storage keys (yet), so let's just clear the page.
398 */
Heiko Carstensef23e772014-01-01 16:53:49 +0100399 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
Thomas Huthaca84242013-09-12 10:33:48 +0200400 return -EFAULT;
401 kvm_s390_set_psw_cc(vcpu, 0);
402 vcpu->run->s.regs.gprs[0] = 0;
403 return 0;
404}
405
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100406static int handle_tpi(struct kvm_vcpu *vcpu)
407{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100408 struct kvm_s390_interrupt_info *inti;
Heiko Carstens4799b552014-01-01 16:55:48 +0100409 unsigned long len;
410 u32 tpi_data[3];
David Hildenbrand261520d2015-02-04 15:53:42 +0100411 int rc;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100412 u64 addr;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100413 u8 ar;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100414
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300415 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100416 if (addr & 3)
417 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
David Hildenbrand261520d2015-02-04 15:53:42 +0100418
Thomas Huthf0926692013-10-09 14:15:54 +0200419 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
David Hildenbrand261520d2015-02-04 15:53:42 +0100420 if (!inti) {
421 kvm_s390_set_psw_cc(vcpu, 0);
422 return 0;
423 }
424
Heiko Carstens4799b552014-01-01 16:55:48 +0100425 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
426 tpi_data[1] = inti->io.io_int_parm;
427 tpi_data[2] = inti->io.io_int_word;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100428 if (addr) {
429 /*
430 * Store the two-word I/O interruption code into the
431 * provided area.
432 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100433 len = sizeof(tpi_data) - 4;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300434 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
David Hildenbrand261520d2015-02-04 15:53:42 +0100435 if (rc) {
436 rc = kvm_s390_inject_prog_cond(vcpu, rc);
437 goto reinject_interrupt;
438 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100439 } else {
440 /*
441 * Store the three-word I/O interruption code into
442 * the appropriate lowcore area.
443 */
Heiko Carstens4799b552014-01-01 16:55:48 +0100444 len = sizeof(tpi_data);
David Hildenbrand261520d2015-02-04 15:53:42 +0100445 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
446 /* failed writes to the low core are not recoverable */
Heiko Carstens4799b552014-01-01 16:55:48 +0100447 rc = -EFAULT;
David Hildenbrand261520d2015-02-04 15:53:42 +0100448 goto reinject_interrupt;
449 }
Heiko Carstens7c959e82013-03-05 13:14:46 +0100450 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100451
452 /* irq was successfully handed to the guest */
453 kfree(inti);
454 kvm_s390_set_psw_cc(vcpu, 1);
455 return 0;
456reinject_interrupt:
Cornelia Huck2f32d4e2014-01-08 18:07:54 +0100457 /*
458 * If we encounter a problem storing the interruption code, the
459 * instruction is suppressed from the guest's view: reinject the
460 * interrupt.
461 */
David Hildenbrand15462e32015-02-04 15:59:11 +0100462 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
463 kfree(inti);
464 rc = -EFAULT;
465 }
David Hildenbrand261520d2015-02-04 15:53:42 +0100466 /* don't set the cc, a pgm irq was injected or we drop to user space */
Heiko Carstens4799b552014-01-01 16:55:48 +0100467 return rc ? -EFAULT : 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100468}
469
470static int handle_tsch(struct kvm_vcpu *vcpu)
471{
Jens Freimann6d3da242013-07-03 15:18:35 +0200472 struct kvm_s390_interrupt_info *inti = NULL;
473 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100474
Jens Freimann6d3da242013-07-03 15:18:35 +0200475 /* a valid schid has at least one bit set */
476 if (vcpu->run->s.regs.gprs[1])
477 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
478 vcpu->run->s.regs.gprs[1]);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100479
480 /*
481 * Prepare exit to userspace.
482 * We indicate whether we dequeued a pending I/O interrupt
483 * so that userspace can re-inject it if the instruction gets
484 * a program check. While this may re-order the pending I/O
485 * interrupts, this is no problem since the priority is kept
486 * intact.
487 */
488 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
489 vcpu->run->s390_tsch.dequeued = !!inti;
490 if (inti) {
491 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
492 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
493 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
494 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
495 }
496 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
497 kfree(inti);
498 return -EREMOTE;
499}
500
Cornelia Huckf379aae2012-12-20 15:32:10 +0100501static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100502{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100503 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100504
Thomas Huth5087dfa2013-06-20 17:22:01 +0200505 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
506 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
507
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100508 if (vcpu->kvm->arch.css_support) {
509 /*
510 * Most I/O instructions will be handled by userspace.
511 * Exceptions are tpi and the interrupt portion of tsch.
512 */
513 if (vcpu->arch.sie_block->ipa == 0xb236)
514 return handle_tpi(vcpu);
515 if (vcpu->arch.sie_block->ipa == 0xb235)
516 return handle_tsch(vcpu);
517 /* Handle in userspace. */
518 return -EOPNOTSUPP;
519 } else {
520 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100521 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100522 * I/O instructions.
523 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200524 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100525 return 0;
526 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100527}
528
Christian Borntraeger453423d2008-03-25 18:47:29 +0100529static int handle_stfl(struct kvm_vcpu *vcpu)
530{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100531 int rc;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100532 unsigned int fac;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100533
534 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200535
536 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
537 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
538
Michael Mueller9d8d5782015-02-02 15:42:51 +0100539 /*
540 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
541 * into a u32 memory representation. They will remain bits 0-31.
542 */
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100543 fac = *vcpu->kvm->arch.model.fac_list >> 32;
Heiko Carstensc667aea2015-12-31 10:29:00 +0100544 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
Michael Mueller9d8d5782015-02-02 15:42:51 +0100545 &fac, sizeof(fac));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100546 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100547 return rc;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200548 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100549 trace_kvm_s390_handle_stfl(vcpu, fac);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100550 return 0;
551}
552
Cornelia Huck48a3e952012-12-20 15:32:09 +0100553#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
554#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100555#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100556#define PSW_ADDR_31 0x000000007fffffffUL
557
Thomas Hutha3fb5772014-04-17 09:10:40 +0200558int is_valid_psw(psw_t *psw)
559{
Heiko Carstens3736b872013-03-25 17:22:52 +0100560 if (psw->mask & PSW_MASK_UNASSIGNED)
561 return 0;
562 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
563 if (psw->addr & ~PSW_ADDR_31)
564 return 0;
565 }
566 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
567 return 0;
568 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
569 return 0;
Thomas Hutha3fb5772014-04-17 09:10:40 +0200570 if (psw->addr & 1)
571 return 0;
Heiko Carstens3736b872013-03-25 17:22:52 +0100572 return 1;
573}
574
Cornelia Huck48a3e952012-12-20 15:32:09 +0100575int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
576{
Heiko Carstens3736b872013-03-25 17:22:52 +0100577 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100578 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100579 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100580 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100581 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100582
Heiko Carstens3736b872013-03-25 17:22:52 +0100583 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200584 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
585
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300586 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100587 if (addr & 7)
588 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100589
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300590 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100591 if (rc)
592 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100593 if (!(new_psw.mask & PSW32_MASK_BASE))
594 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100595 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
596 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
597 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
598 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100599 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100600 return 0;
601}
602
603static int handle_lpswe(struct kvm_vcpu *vcpu)
604{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100605 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100606 u64 addr;
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100607 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100608 u8 ar;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100609
Thomas Huth5087dfa2013-06-20 17:22:01 +0200610 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
611 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
612
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300613 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100614 if (addr & 7)
615 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300616 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
Heiko Carstens2d8bcae2014-01-01 16:57:42 +0100617 if (rc)
618 return kvm_s390_inject_prog_cond(vcpu, rc);
Heiko Carstens3736b872013-03-25 17:22:52 +0100619 vcpu->arch.sie_block->gpsw = new_psw;
620 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100621 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100622 return 0;
623}
624
Christian Borntraeger453423d2008-03-25 18:47:29 +0100625static int handle_stidp(struct kvm_vcpu *vcpu)
626{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200627 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100628 u64 operand2;
Heiko Carstens7d777d72014-01-01 16:58:16 +0100629 int rc;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100630 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100631
632 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100633
Thomas Huth5087dfa2013-06-20 17:22:01 +0200634 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
635 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
636
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300637 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100638
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100639 if (operand2 & 7)
640 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100641
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300642 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
Heiko Carstens7d777d72014-01-01 16:58:16 +0100643 if (rc)
644 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100645
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200646 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100647 return 0;
648}
649
650static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
651{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100652 int cpus = 0;
653 int n;
654
Jens Freimannff520a62014-02-24 10:11:41 +0100655 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100656
657 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200658 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100659 mem->count = 0;
660 if (mem->count < 8)
661 mem->count++;
662 for (n = mem->count - 1; n > 0 ; n--)
663 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
664
Ekaterina Tumanovab75f4c92015-03-03 09:54:41 +0100665 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
Christian Borntraeger453423d2008-03-25 18:47:29 +0100666 mem->vm[0].cpus_total = cpus;
667 mem->vm[0].cpus_configured = cpus;
668 mem->vm[0].cpus_standby = 0;
669 mem->vm[0].cpus_reserved = 0;
670 mem->vm[0].caf = 1000;
671 memcpy(mem->vm[0].name, "KVMguest", 8);
672 ASCEBC(mem->vm[0].name, 8);
673 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
674 ASCEBC(mem->vm[0].cpi, 16);
675}
676
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100677static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100678 u8 fc, u8 sel1, u16 sel2)
679{
680 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
681 vcpu->run->s390_stsi.addr = addr;
682 vcpu->run->s390_stsi.ar = ar;
683 vcpu->run->s390_stsi.fc = fc;
684 vcpu->run->s390_stsi.sel1 = sel1;
685 vcpu->run->s390_stsi.sel2 = sel2;
686}
687
Christian Borntraeger453423d2008-03-25 18:47:29 +0100688static int handle_stsi(struct kvm_vcpu *vcpu)
689{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100690 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
691 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
692 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100693 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100694 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100695 int rc = 0;
Christian Borntraeger27f67f82016-12-09 12:44:40 +0100696 u8 ar;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100697
698 vcpu->stat.instruction_stsi++;
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200699 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100700
Thomas Huth5087dfa2013-06-20 17:22:01 +0200701 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
702 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
703
Thomas Huth87d41fb2013-06-20 17:22:05 +0200704 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200705 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200706 return 0;
707 }
708
709 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
710 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
711 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
712
713 if (fc == 0) {
714 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200715 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200716 return 0;
717 }
718
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300719 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100720
Thomas Huth87d41fb2013-06-20 17:22:05 +0200721 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100722 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
723
724 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100725 case 1: /* same handling for 1 and 2 */
726 case 2:
727 mem = get_zeroed_page(GFP_KERNEL);
728 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100729 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200730 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100731 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100732 break;
733 case 3:
734 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100735 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100736 mem = get_zeroed_page(GFP_KERNEL);
737 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100738 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100739 handle_stsi_3_2_2(vcpu, (void *) mem);
740 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100741 }
742
Alexander Yarygin8ae04b82015-01-19 13:24:51 +0300743 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100744 if (rc) {
745 rc = kvm_s390_inject_prog_cond(vcpu, rc);
746 goto out;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100747 }
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100748 if (vcpu->kvm->arch.user_stsi) {
749 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
750 rc = -EREMOTE;
751 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200752 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100753 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200754 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100755 vcpu->run->s.regs.gprs[0] = 0;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100756 return rc;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100757out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200758 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstens645c5bc2014-01-01 16:58:59 +0100759out:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100760 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100761 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100762}
763
Cornelia Huckf379aae2012-12-20 15:32:10 +0100764static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100765 [0x02] = handle_stidp,
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200766 [0x04] = handle_set_clock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100767 [0x10] = handle_set_prefix,
768 [0x11] = handle_store_prefix,
769 [0x12] = handle_store_cpu_address,
David Hildenbranda3508fb2015-07-08 13:19:48 +0200770 [0x14] = kvm_s390_handle_vsie,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100771 [0x21] = handle_ipte_interlock,
David Hildenbranda7e19ab2016-05-10 09:50:21 +0200772 [0x29] = handle_iske,
773 [0x2a] = handle_rrbe,
774 [0x2b] = handle_sske,
Thomas Huthaca84242013-09-12 10:33:48 +0200775 [0x2c] = handle_test_block,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100776 [0x30] = handle_io_inst,
777 [0x31] = handle_io_inst,
778 [0x32] = handle_io_inst,
779 [0x33] = handle_io_inst,
780 [0x34] = handle_io_inst,
781 [0x35] = handle_io_inst,
782 [0x36] = handle_io_inst,
783 [0x37] = handle_io_inst,
784 [0x38] = handle_io_inst,
785 [0x39] = handle_io_inst,
786 [0x3a] = handle_io_inst,
787 [0x3b] = handle_io_inst,
788 [0x3c] = handle_io_inst,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100789 [0x50] = handle_ipte_interlock,
Christian Borntraegerc0a6bfd2017-02-27 21:14:47 +0100790 [0x56] = handle_sthyi,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100791 [0x5f] = handle_io_inst,
792 [0x74] = handle_io_inst,
793 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100794 [0x7d] = handle_stsi,
795 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100796 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100797};
798
Christian Borntraeger70455a32009-01-22 10:28:29 +0100799int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100800{
801 intercept_handler_t handler;
802
Christian Borntraeger70455a32009-01-22 10:28:29 +0100803 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200804 * A lot of B2 instructions are priviledged. Here we check for
805 * the privileged ones, that we can handle in the kernel.
806 * Anything else goes to userspace.
807 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100808 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200809 if (handler)
810 return handler(vcpu);
811
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100812 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100813}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200814
Cornelia Huck48a3e952012-12-20 15:32:09 +0100815static int handle_epsw(struct kvm_vcpu *vcpu)
816{
817 int reg1, reg2;
818
Thomas Huthaeb87c32013-06-12 13:54:57 +0200819 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100820
821 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200822 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100823 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
824 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200825 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100826 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200827 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100828 }
829 return 0;
830}
831
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200832#define PFMF_RESERVED 0xfffc0101UL
833#define PFMF_SK 0x00020000UL
834#define PFMF_CF 0x00010000UL
835#define PFMF_UI 0x00008000UL
836#define PFMF_FSC 0x00007000UL
837#define PFMF_NQ 0x00000800UL
838#define PFMF_MR 0x00000400UL
839#define PFMF_MC 0x00000200UL
840#define PFMF_KEY 0x000000feUL
841
842static int handle_pfmf(struct kvm_vcpu *vcpu)
843{
David Hildenbrand1824c722016-05-10 09:43:11 +0200844 bool mr = false, mc = false, nq;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200845 int reg1, reg2;
846 unsigned long start, end;
David Hildenbrand1824c722016-05-10 09:43:11 +0200847 unsigned char key;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200848
849 vcpu->stat.instruction_pfmf++;
850
851 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
852
Heiko Carstens03c02802015-11-13 13:31:58 +0100853 if (!test_kvm_facility(vcpu->kvm, 8))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200854 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
855
856 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200857 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200858
859 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
860 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
861
David Hildenbrandedc5b052016-03-04 11:08:09 +0100862 /* Only provide non-quiescing support if enabled for the guest */
863 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
864 !test_kvm_facility(vcpu->kvm, 14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200865 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
866
David Hildenbrand1824c722016-05-10 09:43:11 +0200867 /* Only provide conditional-SSKE support if enabled for the guest */
868 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
869 test_kvm_facility(vcpu->kvm, 10)) {
870 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
871 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
872 }
873
874 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
875 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200876 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
Thomas Hutha02689f2014-11-10 15:59:32 +0100877 start = kvm_s390_logical_to_effective(vcpu, start);
Thomas Huthfb34c6032013-09-09 17:58:38 +0200878
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200879 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
880 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
881 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
882 }
883
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200884 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
885 case 0x00000000:
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200886 /* only 4k frames specify a real address */
887 start = kvm_s390_real_to_abs(vcpu, start);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200888 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
889 break;
890 case 0x00001000:
891 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
892 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200893 case 0x00002000:
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100894 /* only support 2G frame size if EDAT2 is available and we are
895 not in 24-bit addressing mode */
896 if (!test_kvm_facility(vcpu->kvm, 78) ||
897 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
898 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200899 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100900 break;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200901 default:
902 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
903 }
Thomas Hutha02689f2014-11-10 15:59:32 +0100904
David Hildenbrand695be0e2016-05-12 14:07:05 +0200905 while (start != end) {
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200906 unsigned long useraddr;
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200907
Thomas Huthfb34c6032013-09-09 17:58:38 +0200908 /* Translate guest address to host address */
David Hildenbrand6164a2e2016-04-13 10:09:47 +0200909 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
Thomas Huthfb34c6032013-09-09 17:58:38 +0200910 if (kvm_is_error_hva(useraddr))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200911 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
912
913 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
914 if (clear_user((void __user *)useraddr, PAGE_SIZE))
915 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
916 }
917
918 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Dominik Dingel3ac8e382014-10-23 12:09:17 +0200919 int rc = __skey_check_enable(vcpu);
920
921 if (rc)
922 return rc;
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100923 down_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200924 rc = cond_set_guest_storage_key(current->mm, useraddr,
925 key, NULL, nq, mr, mc);
Martin Schwidefskyd3ed1ce2016-03-08 11:53:35 +0100926 up_read(&current->mm->mmap_sem);
David Hildenbrand1824c722016-05-10 09:43:11 +0200927 if (rc < 0)
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200928 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
929 }
930
931 start += PAGE_SIZE;
932 }
David Hildenbrand2c26d1d2016-04-13 15:47:21 +0200933 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
934 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
935 vcpu->run->s.regs.gprs[reg2] = end;
936 } else {
937 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
938 end = kvm_s390_logical_to_effective(vcpu, end);
939 vcpu->run->s.regs.gprs[reg2] |= end;
940 }
941 }
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200942 return 0;
943}
944
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200945static int handle_essa(struct kvm_vcpu *vcpu)
946{
947 /* entries expected to be 1FF */
948 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200949 unsigned long *cbrlo;
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200950 struct gmap *gmap;
951 int i;
952
Christian Borntraeger7cbde762015-07-21 12:44:57 +0200953 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200954 gmap = vcpu->arch.gmap;
955 vcpu->stat.instruction_essa++;
Dominik Dingele6db1d62015-05-07 15:41:57 +0200956 if (!vcpu->kvm->arch.use_cmma)
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200957 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
958
959 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
960 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
961
962 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
963 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
964
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100965 /* Retry the ESSA instruction */
966 kvm_s390_retry_instr(vcpu);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200967 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
968 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
969 down_read(&gmap->mm->mmap_sem);
David Hildenbrand4a5e7e32016-04-12 13:32:25 +0200970 for (i = 0; i < entries; ++i)
971 __gmap_zap(gmap, cbrlo[i]);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200972 up_read(&gmap->mm->mmap_sem);
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200973 return 0;
974}
975
Cornelia Huck48a3e952012-12-20 15:32:09 +0100976static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +0100977 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100978 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100979 [0x8e] = handle_ipte_interlock,
980 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200981 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200982 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100983};
984
985int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
986{
987 intercept_handler_t handler;
988
989 /* This is handled just as for the B2 instructions. */
990 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200991 if (handler)
992 return handler(vcpu);
993
Cornelia Huck48a3e952012-12-20 15:32:09 +0100994 return -EOPNOTSUPP;
995}
996
Thomas Huth953ed882013-06-20 17:22:04 +0200997int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
998{
999 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1000 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001001 int reg, rc, nr_regs;
1002 u32 ctl_array[16];
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001003 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001004 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001005
1006 vcpu->stat.instruction_lctl++;
1007
1008 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1009 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1010
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001011 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001012
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001013 if (ga & 3)
Thomas Huth953ed882013-06-20 17:22:04 +02001014 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1015
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001016 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001017 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001018
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001019 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001020 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001021 if (rc)
1022 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth953ed882013-06-20 17:22:04 +02001023 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001024 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001025 do {
Thomas Huth953ed882013-06-20 17:22:04 +02001026 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001027 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001028 if (reg == reg3)
1029 break;
1030 reg = (reg + 1) % 16;
1031 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001032 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001033 return 0;
1034}
1035
David Hildenbrandaba07502014-01-23 10:47:13 +01001036int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1037{
1038 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1039 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001040 int reg, rc, nr_regs;
1041 u32 ctl_array[16];
David Hildenbrandaba07502014-01-23 10:47:13 +01001042 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001043 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001044
1045 vcpu->stat.instruction_stctl++;
1046
1047 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1048 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1049
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001050 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001051
1052 if (ga & 3)
1053 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1054
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001055 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001056 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1057
1058 reg = reg1;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001059 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001060 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001061 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001062 if (reg == reg3)
1063 break;
1064 reg = (reg + 1) % 16;
1065 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001066 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001067 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001068}
1069
Thomas Huth953ed882013-06-20 17:22:04 +02001070static int handle_lctlg(struct kvm_vcpu *vcpu)
1071{
1072 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1073 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001074 int reg, rc, nr_regs;
1075 u64 ctl_array[16];
1076 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001077 u8 ar;
Thomas Huth953ed882013-06-20 17:22:04 +02001078
1079 vcpu->stat.instruction_lctlg++;
1080
1081 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1082 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1083
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001084 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
Thomas Huth953ed882013-06-20 17:22:04 +02001085
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001086 if (ga & 7)
Thomas Huth953ed882013-06-20 17:22:04 +02001087 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1088
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001089 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
Heiko Carstensf987a3e2014-01-01 16:59:21 +01001090 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
Thomas Huth953ed882013-06-20 17:22:04 +02001091
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001092 nr_regs = ((reg3 - reg1) & 0xf) + 1;
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001093 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001094 if (rc)
1095 return kvm_s390_inject_prog_cond(vcpu, rc);
1096 reg = reg1;
1097 nr_regs = 0;
Thomas Huth953ed882013-06-20 17:22:04 +02001098 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001099 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
Thomas Huth953ed882013-06-20 17:22:04 +02001100 if (reg == reg3)
1101 break;
1102 reg = (reg + 1) % 16;
1103 } while (1);
Christian Borntraeger2dca4852014-10-31 09:24:20 +01001104 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Thomas Huth953ed882013-06-20 17:22:04 +02001105 return 0;
1106}
1107
David Hildenbrandaba07502014-01-23 10:47:13 +01001108static int handle_stctg(struct kvm_vcpu *vcpu)
1109{
1110 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1111 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001112 int reg, rc, nr_regs;
1113 u64 ctl_array[16];
1114 u64 ga;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001115 u8 ar;
David Hildenbrandaba07502014-01-23 10:47:13 +01001116
1117 vcpu->stat.instruction_stctg++;
1118
1119 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1120 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1121
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001122 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
David Hildenbrandaba07502014-01-23 10:47:13 +01001123
1124 if (ga & 7)
1125 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1126
Christian Borntraeger7cbde762015-07-21 12:44:57 +02001127 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
David Hildenbrandaba07502014-01-23 10:47:13 +01001128 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1129
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001130 reg = reg1;
1131 nr_regs = 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001132 do {
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001133 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
David Hildenbrandaba07502014-01-23 10:47:13 +01001134 if (reg == reg3)
1135 break;
1136 reg = (reg + 1) % 16;
1137 } while (1);
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001138 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
Heiko Carstensfc56eb62014-10-29 10:07:16 +01001139 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
David Hildenbrandaba07502014-01-23 10:47:13 +01001140}
1141
Cornelia Huckf379aae2012-12-20 15:32:10 +01001142static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +02001143 [0x2f] = handle_lctlg,
David Hildenbrandaba07502014-01-23 10:47:13 +01001144 [0x25] = handle_stctg,
Fan Zhang80cd8762016-08-15 04:53:22 +02001145 [0x60] = handle_ri,
1146 [0x61] = handle_ri,
1147 [0x62] = handle_ri,
Cornelia Huckf379aae2012-12-20 15:32:10 +01001148};
1149
Thomas Huth953ed882013-06-20 17:22:04 +02001150int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +01001151{
1152 intercept_handler_t handler;
1153
Cornelia Huckf379aae2012-12-20 15:32:10 +01001154 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1155 if (handler)
1156 return handler(vcpu);
1157 return -EOPNOTSUPP;
1158}
1159
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001160static int handle_tprot(struct kvm_vcpu *vcpu)
1161{
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001162 u64 address1, address2;
Thomas Hutha0465f92014-02-04 14:48:07 +01001163 unsigned long hva, gpa;
1164 int ret = 0, cc = 0;
1165 bool writable;
Christian Borntraeger27f67f82016-12-09 12:44:40 +01001166 u8 ar;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001167
1168 vcpu->stat.instruction_tprot++;
1169
Thomas Huthf9f6bbc2013-06-20 17:22:00 +02001170 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1171 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1172
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001173 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
Cornelia Huckb1c571a2012-12-20 15:32:07 +01001174
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001175 /* we only handle the Linux memory detection case:
1176 * access key == 0
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001177 * everything else goes to userspace. */
1178 if (address2 & 0xf0)
1179 return -EOPNOTSUPP;
1180 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
Thomas Hutha0465f92014-02-04 14:48:07 +01001181 ipte_lock(vcpu);
David Hildenbrand92c96322015-11-16 15:42:11 +01001182 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
Thomas Hutha0465f92014-02-04 14:48:07 +01001183 if (ret == PGM_PROTECTION) {
1184 /* Write protected? Try again with read-only... */
1185 cc = 1;
David Hildenbrand92c96322015-11-16 15:42:11 +01001186 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1187 GACC_FETCH);
Thomas Hutha0465f92014-02-04 14:48:07 +01001188 }
1189 if (ret) {
1190 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1191 ret = kvm_s390_inject_program_int(vcpu, ret);
1192 } else if (ret > 0) {
1193 /* Translation not available */
1194 kvm_s390_set_psw_cc(vcpu, 3);
1195 ret = 0;
1196 }
1197 goto out_unlock;
1198 }
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001199
Thomas Hutha0465f92014-02-04 14:48:07 +01001200 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1201 if (kvm_is_error_hva(hva)) {
1202 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1203 } else {
1204 if (!writable)
1205 cc = 1; /* Write not permitted ==> read-only */
1206 kvm_s390_set_psw_cc(vcpu, cc);
1207 /* Note: CC2 only occurs for storage keys (not supported yet) */
1208 }
1209out_unlock:
1210 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1211 ipte_unlock(vcpu);
1212 return ret;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +02001213}
1214
1215int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1216{
1217 /* For e5xx... instructions we only handle TPROT */
1218 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1219 return handle_tprot(vcpu);
1220 return -EOPNOTSUPP;
1221}
1222
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001223static int handle_sckpf(struct kvm_vcpu *vcpu)
1224{
1225 u32 value;
1226
1227 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +02001228 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001229
1230 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1231 return kvm_s390_inject_program_int(vcpu,
1232 PGM_SPECIFICATION);
1233
1234 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1235 vcpu->arch.sie_block->todpr = value;
1236
1237 return 0;
1238}
1239
David Hildenbrand9acc3172016-07-18 09:18:13 +02001240static int handle_ptff(struct kvm_vcpu *vcpu)
1241{
1242 /* we don't emulate any control instructions yet */
1243 kvm_s390_set_psw_cc(vcpu, 3);
1244 return 0;
1245}
1246
Cornelia Huck77975352012-12-20 15:32:06 +01001247static const intercept_handler_t x01_handlers[256] = {
David Hildenbrand9acc3172016-07-18 09:18:13 +02001248 [0x04] = handle_ptff,
Cornelia Huck8c3f61e2012-04-24 09:24:44 +02001249 [0x07] = handle_sckpf,
1250};
1251
1252int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1253{
1254 intercept_handler_t handler;
1255
1256 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1257 if (handler)
1258 return handler(vcpu);
1259 return -EOPNOTSUPP;
1260}