]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/s390/kvm/priv.c
Merge remote-tracking branch 's390/guarded-storage' into kvms390/next
[mirror_ubuntu-jammy-kernel.git] / arch / s390 / kvm / priv.c
CommitLineData
453423dc 1/*
a53c8fab 2 * handling privileged instructions
453423dc 3 *
69d0d3a3 4 * Copyright IBM Corp. 2008, 2013
453423dc
CB
5 *
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>
5a0e3ad6 15#include <linux/gfp.h>
453423dc 16#include <linux/errno.h>
b13b5dc7 17#include <linux/compat.h>
589ee628
IM
18#include <linux/mm_types.h>
19
7c959e82 20#include <asm/asm-offsets.h>
e769ece3 21#include <asm/facility.h>
453423dc
CB
22#include <asm/current.h>
23#include <asm/debug.h>
24#include <asm/ebcdic.h>
25#include <asm/sysinfo.h>
69d0d3a3
CB
26#include <asm/pgtable.h>
27#include <asm/pgalloc.h>
1e133ab2 28#include <asm/gmap.h>
69d0d3a3 29#include <asm/io.h>
48a3e950
CH
30#include <asm/ptrace.h>
31#include <asm/compat.h>
a7e19ab5 32#include <asm/sclp.h>
453423dc
CB
33#include "gaccess.h"
34#include "kvm-s390.h"
5786fffa 35#include "trace.h"
453423dc 36
80cd8763
FZ
37static int handle_ri(struct kvm_vcpu *vcpu)
38{
39 if (test_kvm_facility(vcpu->kvm, 64)) {
4d5f2c04 40 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
0c9d8683 41 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
80cd8763
FZ
42 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
6a3f95a6
TH
56/* Handle SCK (SET CLOCK) interception */
57static int handle_set_clock(struct kvm_vcpu *vcpu)
58{
25ed1675 59 int rc;
27f67f87 60 u8 ar;
25ed1675 61 u64 op2, val;
6a3f95a6
TH
62
63 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
64 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
65
8ae04b8f 66 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
6a3f95a6
TH
67 if (op2 & 7) /* Operand must be on a doubleword boundary */
68 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
8ae04b8f 69 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
0e7a3f94
HC
70 if (rc)
71 return kvm_s390_inject_prog_cond(vcpu, rc);
6a3f95a6 72
7cbde76b 73 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
25ed1675 74 kvm_s390_set_tod_clock(vcpu->kvm, val);
6a3f95a6
TH
75
76 kvm_s390_set_psw_cc(vcpu, 0);
77 return 0;
78}
79
453423dc
CB
80static int handle_set_prefix(struct kvm_vcpu *vcpu)
81{
453423dc 82 u64 operand2;
665170cb
HC
83 u32 address;
84 int rc;
27f67f87 85 u8 ar;
453423dc
CB
86
87 vcpu->stat.instruction_spx++;
88
5087dfa6
TH
89 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
90 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
91
8ae04b8f 92 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
453423dc
CB
93
94 /* must be word boundary */
db4a29cb
HC
95 if (operand2 & 3)
96 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc
CB
97
98 /* get the value */
8ae04b8f 99 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
665170cb
HC
100 if (rc)
101 return kvm_s390_inject_prog_cond(vcpu, rc);
102
103 address &= 0x7fffe000u;
104
105 /*
106 * Make sure the new value is valid memory. We only need to check the
107 * first page, since address is 8k aligned and memory pieces are always
108 * at least 1MB aligned and have at least a size of 1MB.
109 */
110 if (kvm_is_error_gpa(vcpu->kvm, address))
db4a29cb 111 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
453423dc 112
8d26cf7b 113 kvm_s390_set_prefix(vcpu, address);
5786fffa 114 trace_kvm_s390_handle_prefix(vcpu, 1, address);
453423dc
CB
115 return 0;
116}
117
118static int handle_store_prefix(struct kvm_vcpu *vcpu)
119{
453423dc
CB
120 u64 operand2;
121 u32 address;
f748f4a7 122 int rc;
27f67f87 123 u8 ar;
453423dc
CB
124
125 vcpu->stat.instruction_stpx++;
b1c571a5 126
5087dfa6
TH
127 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
128 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
129
8ae04b8f 130 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
453423dc
CB
131
132 /* must be word boundary */
db4a29cb
HC
133 if (operand2 & 3)
134 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc 135
fda902cb 136 address = kvm_s390_get_prefix(vcpu);
453423dc
CB
137
138 /* get the value */
8ae04b8f 139 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
f748f4a7
HC
140 if (rc)
141 return kvm_s390_inject_prog_cond(vcpu, rc);
453423dc 142
7cbde76b 143 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
5786fffa 144 trace_kvm_s390_handle_prefix(vcpu, 0, address);
453423dc
CB
145 return 0;
146}
147
148static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
149{
8b96de0e
HC
150 u16 vcpu_id = vcpu->vcpu_id;
151 u64 ga;
152 int rc;
27f67f87 153 u8 ar;
453423dc
CB
154
155 vcpu->stat.instruction_stap++;
b1c571a5 156
5087dfa6
TH
157 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
158 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
159
8ae04b8f 160 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
453423dc 161
8b96de0e 162 if (ga & 1)
db4a29cb 163 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc 164
8ae04b8f 165 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
8b96de0e
HC
166 if (rc)
167 return kvm_s390_inject_prog_cond(vcpu, rc);
453423dc 168
7cbde76b 169 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
8b96de0e 170 trace_kvm_s390_handle_stap(vcpu, ga);
453423dc
CB
171 return 0;
172}
173
3ac8e380 174static int __skey_check_enable(struct kvm_vcpu *vcpu)
693ffc08 175{
3ac8e380 176 int rc = 0;
11ddcd41
DH
177
178 trace_kvm_s390_skey_related_inst(vcpu);
693ffc08 179 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
3ac8e380 180 return rc;
693ffc08 181
3ac8e380 182 rc = s390_enable_skey();
11ddcd41
DH
183 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
184 if (!rc)
185 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
3ac8e380 186 return rc;
693ffc08
DD
187}
188
a7e19ab5 189static int try_handle_skey(struct kvm_vcpu *vcpu)
453423dc 190{
11ddcd41 191 int rc;
693ffc08 192
11ddcd41
DH
193 vcpu->stat.instruction_storage_key++;
194 rc = __skey_check_enable(vcpu);
3ac8e380
DD
195 if (rc)
196 return rc;
a7e19ab5
DH
197 if (sclp.has_skey) {
198 /* with storage-key facility, SIE interprets it for us */
199 kvm_s390_retry_instr(vcpu);
200 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
201 return -EAGAIN;
202 }
5087dfa6
TH
203 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
204 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
a7e19ab5
DH
205 return 0;
206}
5087dfa6 207
a7e19ab5
DH
208static int handle_iske(struct kvm_vcpu *vcpu)
209{
210 unsigned long addr;
211 unsigned char key;
212 int reg1, reg2;
213 int rc;
214
215 rc = try_handle_skey(vcpu);
216 if (rc)
217 return rc != -EAGAIN ? rc : 0;
218
219 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
220
221 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
222 addr = kvm_s390_logical_to_effective(vcpu, addr);
223 addr = kvm_s390_real_to_abs(vcpu, addr);
224 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
225 if (kvm_is_error_hva(addr))
226 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
227
228 down_read(&current->mm->mmap_sem);
229 rc = get_guest_storage_key(current->mm, addr, &key);
230 up_read(&current->mm->mmap_sem);
231 if (rc)
232 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
233 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
234 vcpu->run->s.regs.gprs[reg1] |= key;
235 return 0;
236}
237
238static int handle_rrbe(struct kvm_vcpu *vcpu)
239{
240 unsigned long addr;
241 int reg1, reg2;
242 int rc;
243
244 rc = try_handle_skey(vcpu);
245 if (rc)
246 return rc != -EAGAIN ? rc : 0;
247
248 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
249
250 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
251 addr = kvm_s390_logical_to_effective(vcpu, addr);
252 addr = kvm_s390_real_to_abs(vcpu, addr);
253 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
254 if (kvm_is_error_hva(addr))
255 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
256
257 down_read(&current->mm->mmap_sem);
258 rc = reset_guest_reference_bit(current->mm, addr);
259 up_read(&current->mm->mmap_sem);
260 if (rc < 0)
261 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
262
263 kvm_s390_set_psw_cc(vcpu, rc);
264 return 0;
265}
266
267#define SSKE_NQ 0x8
268#define SSKE_MR 0x4
269#define SSKE_MC 0x2
270#define SSKE_MB 0x1
271static int handle_sske(struct kvm_vcpu *vcpu)
272{
273 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
274 unsigned long start, end;
275 unsigned char key, oldkey;
276 int reg1, reg2;
277 int rc;
278
279 rc = try_handle_skey(vcpu);
280 if (rc)
281 return rc != -EAGAIN ? rc : 0;
282
283 if (!test_kvm_facility(vcpu->kvm, 8))
284 m3 &= ~SSKE_MB;
285 if (!test_kvm_facility(vcpu->kvm, 10))
286 m3 &= ~(SSKE_MC | SSKE_MR);
287 if (!test_kvm_facility(vcpu->kvm, 14))
288 m3 &= ~SSKE_NQ;
289
290 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
291
292 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
293 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
294 start = kvm_s390_logical_to_effective(vcpu, start);
295 if (m3 & SSKE_MB) {
296 /* start already designates an absolute address */
297 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
298 } else {
299 start = kvm_s390_real_to_abs(vcpu, start);
300 end = start + PAGE_SIZE;
301 }
302
303 while (start != end) {
304 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
305
306 if (kvm_is_error_hva(addr))
307 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
308
309 down_read(&current->mm->mmap_sem);
310 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
311 m3 & SSKE_NQ, m3 & SSKE_MR,
312 m3 & SSKE_MC);
313 up_read(&current->mm->mmap_sem);
314 if (rc < 0)
315 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
316 start += PAGE_SIZE;
0b925159 317 }
a7e19ab5
DH
318
319 if (m3 & (SSKE_MC | SSKE_MR)) {
320 if (m3 & SSKE_MB) {
321 /* skey in reg1 is unpredictable */
322 kvm_s390_set_psw_cc(vcpu, 3);
323 } else {
324 kvm_s390_set_psw_cc(vcpu, rc);
325 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
326 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
327 }
328 }
329 if (m3 & SSKE_MB) {
330 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT)
331 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
332 else
333 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
334 end = kvm_s390_logical_to_effective(vcpu, end);
335 vcpu->run->s.regs.gprs[reg2] |= end;
336 }
453423dc
CB
337 return 0;
338}
339
8a242234
HC
340static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
341{
8a242234 342 vcpu->stat.instruction_ipte_interlock++;
04b41acd 343 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
8a242234
HC
344 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
345 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
0e8bc06a 346 kvm_s390_retry_instr(vcpu);
8a242234
HC
347 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
348 return 0;
349}
350
aca84241
TH
351static int handle_test_block(struct kvm_vcpu *vcpu)
352{
aca84241
TH
353 gpa_t addr;
354 int reg2;
355
356 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
357 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
358
359 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
360 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
e45efa28 361 addr = kvm_s390_logical_to_effective(vcpu, addr);
dd9e5b7b 362 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
e45efa28 363 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
aca84241
TH
364 addr = kvm_s390_real_to_abs(vcpu, addr);
365
ef23e779 366 if (kvm_is_error_gpa(vcpu->kvm, addr))
aca84241
TH
367 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
368 /*
369 * We don't expect errors on modern systems, and do not care
370 * about storage keys (yet), so let's just clear the page.
371 */
ef23e779 372 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
aca84241
TH
373 return -EFAULT;
374 kvm_s390_set_psw_cc(vcpu, 0);
375 vcpu->run->s.regs.gprs[0] = 0;
376 return 0;
377}
378
fa6b7fe9 379static int handle_tpi(struct kvm_vcpu *vcpu)
453423dc 380{
fa6b7fe9 381 struct kvm_s390_interrupt_info *inti;
4799b557
HC
382 unsigned long len;
383 u32 tpi_data[3];
261520dc 384 int rc;
7c959e82 385 u64 addr;
27f67f87 386 u8 ar;
fa6b7fe9 387
8ae04b8f 388 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
db4a29cb
HC
389 if (addr & 3)
390 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
261520dc 391
f092669e 392 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
261520dc
DH
393 if (!inti) {
394 kvm_s390_set_psw_cc(vcpu, 0);
395 return 0;
396 }
397
4799b557
HC
398 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
399 tpi_data[1] = inti->io.io_int_parm;
400 tpi_data[2] = inti->io.io_int_word;
7c959e82
HC
401 if (addr) {
402 /*
403 * Store the two-word I/O interruption code into the
404 * provided area.
405 */
4799b557 406 len = sizeof(tpi_data) - 4;
8ae04b8f 407 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
261520dc
DH
408 if (rc) {
409 rc = kvm_s390_inject_prog_cond(vcpu, rc);
410 goto reinject_interrupt;
411 }
7c959e82
HC
412 } else {
413 /*
414 * Store the three-word I/O interruption code into
415 * the appropriate lowcore area.
416 */
4799b557 417 len = sizeof(tpi_data);
261520dc
DH
418 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
419 /* failed writes to the low core are not recoverable */
4799b557 420 rc = -EFAULT;
261520dc
DH
421 goto reinject_interrupt;
422 }
7c959e82 423 }
261520dc
DH
424
425 /* irq was successfully handed to the guest */
426 kfree(inti);
427 kvm_s390_set_psw_cc(vcpu, 1);
428 return 0;
429reinject_interrupt:
2f32d4ea
CH
430 /*
431 * If we encounter a problem storing the interruption code, the
432 * instruction is suppressed from the guest's view: reinject the
433 * interrupt.
434 */
15462e37
DH
435 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
436 kfree(inti);
437 rc = -EFAULT;
438 }
261520dc 439 /* don't set the cc, a pgm irq was injected or we drop to user space */
4799b557 440 return rc ? -EFAULT : 0;
453423dc
CB
441}
442
fa6b7fe9
CH
443static int handle_tsch(struct kvm_vcpu *vcpu)
444{
6d3da241
JF
445 struct kvm_s390_interrupt_info *inti = NULL;
446 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
fa6b7fe9 447
6d3da241
JF
448 /* a valid schid has at least one bit set */
449 if (vcpu->run->s.regs.gprs[1])
450 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
451 vcpu->run->s.regs.gprs[1]);
fa6b7fe9
CH
452
453 /*
454 * Prepare exit to userspace.
455 * We indicate whether we dequeued a pending I/O interrupt
456 * so that userspace can re-inject it if the instruction gets
457 * a program check. While this may re-order the pending I/O
458 * interrupts, this is no problem since the priority is kept
459 * intact.
460 */
461 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
462 vcpu->run->s390_tsch.dequeued = !!inti;
463 if (inti) {
464 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
465 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
466 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
467 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
468 }
469 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
470 kfree(inti);
471 return -EREMOTE;
472}
473
474static int handle_io_inst(struct kvm_vcpu *vcpu)
475{
476 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
477
5087dfa6
TH
478 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
479 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
480
fa6b7fe9
CH
481 if (vcpu->kvm->arch.css_support) {
482 /*
483 * Most I/O instructions will be handled by userspace.
484 * Exceptions are tpi and the interrupt portion of tsch.
485 */
486 if (vcpu->arch.sie_block->ipa == 0xb236)
487 return handle_tpi(vcpu);
488 if (vcpu->arch.sie_block->ipa == 0xb235)
489 return handle_tsch(vcpu);
490 /* Handle in userspace. */
491 return -EOPNOTSUPP;
492 } else {
493 /*
b4a96015 494 * Set condition code 3 to stop the guest from issuing channel
fa6b7fe9
CH
495 * I/O instructions.
496 */
ea828ebf 497 kvm_s390_set_psw_cc(vcpu, 3);
fa6b7fe9
CH
498 return 0;
499 }
500}
501
453423dc
CB
502static int handle_stfl(struct kvm_vcpu *vcpu)
503{
453423dc 504 int rc;
9d8d5786 505 unsigned int fac;
453423dc
CB
506
507 vcpu->stat.instruction_stfl++;
5087dfa6
TH
508
509 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
510 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
511
9d8d5786
MM
512 /*
513 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
514 * into a u32 memory representation. They will remain bits 0-31.
515 */
c54f0d6a 516 fac = *vcpu->kvm->arch.model.fac_list >> 32;
c667aeac 517 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
9d8d5786 518 &fac, sizeof(fac));
dc5008b9 519 if (rc)
0f9701c6 520 return rc;
7cbde76b 521 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
9d8d5786 522 trace_kvm_s390_handle_stfl(vcpu, fac);
453423dc
CB
523 return 0;
524}
525
48a3e950
CH
526#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
527#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
d21683ea 528#define PSW_ADDR_24 0x0000000000ffffffUL
48a3e950
CH
529#define PSW_ADDR_31 0x000000007fffffffUL
530
a3fb577e
TH
531int is_valid_psw(psw_t *psw)
532{
3736b874
HC
533 if (psw->mask & PSW_MASK_UNASSIGNED)
534 return 0;
535 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
536 if (psw->addr & ~PSW_ADDR_31)
537 return 0;
538 }
539 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
540 return 0;
541 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
542 return 0;
a3fb577e
TH
543 if (psw->addr & 1)
544 return 0;
3736b874
HC
545 return 1;
546}
547
48a3e950
CH
548int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
549{
3736b874 550 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
48a3e950 551 psw_compat_t new_psw;
3736b874 552 u64 addr;
2d8bcaed 553 int rc;
27f67f87 554 u8 ar;
48a3e950 555
3736b874 556 if (gpsw->mask & PSW_MASK_PSTATE)
208dd756
TH
557 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
558
8ae04b8f 559 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
6fd0fcc9
HC
560 if (addr & 7)
561 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
2d8bcaed 562
8ae04b8f 563 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
2d8bcaed
HC
564 if (rc)
565 return kvm_s390_inject_prog_cond(vcpu, rc);
6fd0fcc9
HC
566 if (!(new_psw.mask & PSW32_MASK_BASE))
567 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
3736b874
HC
568 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
569 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
570 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
571 if (!is_valid_psw(gpsw))
6fd0fcc9 572 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
48a3e950
CH
573 return 0;
574}
575
576static int handle_lpswe(struct kvm_vcpu *vcpu)
577{
48a3e950 578 psw_t new_psw;
3736b874 579 u64 addr;
2d8bcaed 580 int rc;
27f67f87 581 u8 ar;
48a3e950 582
5087dfa6
TH
583 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
584 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
585
8ae04b8f 586 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
6fd0fcc9
HC
587 if (addr & 7)
588 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
8ae04b8f 589 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
2d8bcaed
HC
590 if (rc)
591 return kvm_s390_inject_prog_cond(vcpu, rc);
3736b874
HC
592 vcpu->arch.sie_block->gpsw = new_psw;
593 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
6fd0fcc9 594 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
48a3e950
CH
595 return 0;
596}
597
453423dc
CB
598static int handle_stidp(struct kvm_vcpu *vcpu)
599{
9bb0ec09 600 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
453423dc 601 u64 operand2;
7d777d78 602 int rc;
27f67f87 603 u8 ar;
453423dc
CB
604
605 vcpu->stat.instruction_stidp++;
b1c571a5 606
5087dfa6
TH
607 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
608 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
609
8ae04b8f 610 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
453423dc 611
db4a29cb
HC
612 if (operand2 & 7)
613 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc 614
8ae04b8f 615 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
7d777d78
HC
616 if (rc)
617 return kvm_s390_inject_prog_cond(vcpu, rc);
453423dc 618
7cbde76b 619 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
453423dc
CB
620 return 0;
621}
622
623static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
624{
453423dc
CB
625 int cpus = 0;
626 int n;
627
ff520a63 628 cpus = atomic_read(&vcpu->kvm->online_vcpus);
453423dc
CB
629
630 /* deal with other level 3 hypervisors */
caf757c6 631 if (stsi(mem, 3, 2, 2))
453423dc
CB
632 mem->count = 0;
633 if (mem->count < 8)
634 mem->count++;
635 for (n = mem->count - 1; n > 0 ; n--)
636 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
637
b75f4c9a 638 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
453423dc
CB
639 mem->vm[0].cpus_total = cpus;
640 mem->vm[0].cpus_configured = cpus;
641 mem->vm[0].cpus_standby = 0;
642 mem->vm[0].cpus_reserved = 0;
643 mem->vm[0].caf = 1000;
644 memcpy(mem->vm[0].name, "KVMguest", 8);
645 ASCEBC(mem->vm[0].name, 8);
646 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
647 ASCEBC(mem->vm[0].cpi, 16);
648}
649
27f67f87 650static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
e44fc8c9
ET
651 u8 fc, u8 sel1, u16 sel2)
652{
653 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
654 vcpu->run->s390_stsi.addr = addr;
655 vcpu->run->s390_stsi.ar = ar;
656 vcpu->run->s390_stsi.fc = fc;
657 vcpu->run->s390_stsi.sel1 = sel1;
658 vcpu->run->s390_stsi.sel2 = sel2;
659}
660
453423dc
CB
661static int handle_stsi(struct kvm_vcpu *vcpu)
662{
5a32c1af
CB
663 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
664 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
665 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
c51f068c 666 unsigned long mem = 0;
453423dc 667 u64 operand2;
db4a29cb 668 int rc = 0;
27f67f87 669 u8 ar;
453423dc
CB
670
671 vcpu->stat.instruction_stsi++;
7cbde76b 672 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
453423dc 673
5087dfa6
TH
674 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
675 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
676
87d41fb4 677 if (fc > 3) {
ea828ebf 678 kvm_s390_set_psw_cc(vcpu, 3);
87d41fb4
TH
679 return 0;
680 }
453423dc 681
87d41fb4
TH
682 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
683 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
453423dc
CB
684 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
685
87d41fb4 686 if (fc == 0) {
5a32c1af 687 vcpu->run->s.regs.gprs[0] = 3 << 28;
ea828ebf 688 kvm_s390_set_psw_cc(vcpu, 0);
453423dc 689 return 0;
87d41fb4
TH
690 }
691
8ae04b8f 692 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
87d41fb4
TH
693
694 if (operand2 & 0xfff)
695 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
696
697 switch (fc) {
453423dc
CB
698 case 1: /* same handling for 1 and 2 */
699 case 2:
700 mem = get_zeroed_page(GFP_KERNEL);
701 if (!mem)
c51f068c 702 goto out_no_data;
caf757c6 703 if (stsi((void *) mem, fc, sel1, sel2))
c51f068c 704 goto out_no_data;
453423dc
CB
705 break;
706 case 3:
707 if (sel1 != 2 || sel2 != 2)
c51f068c 708 goto out_no_data;
453423dc
CB
709 mem = get_zeroed_page(GFP_KERNEL);
710 if (!mem)
c51f068c 711 goto out_no_data;
453423dc
CB
712 handle_stsi_3_2_2(vcpu, (void *) mem);
713 break;
453423dc
CB
714 }
715
8ae04b8f 716 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
645c5bc1
HC
717 if (rc) {
718 rc = kvm_s390_inject_prog_cond(vcpu, rc);
719 goto out;
453423dc 720 }
e44fc8c9
ET
721 if (vcpu->kvm->arch.user_stsi) {
722 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
723 rc = -EREMOTE;
724 }
5786fffa 725 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
453423dc 726 free_page(mem);
ea828ebf 727 kvm_s390_set_psw_cc(vcpu, 0);
5a32c1af 728 vcpu->run->s.regs.gprs[0] = 0;
e44fc8c9 729 return rc;
c51f068c 730out_no_data:
ea828ebf 731 kvm_s390_set_psw_cc(vcpu, 3);
645c5bc1 732out:
c51f068c 733 free_page(mem);
db4a29cb 734 return rc;
453423dc
CB
735}
736
f379aae5 737static const intercept_handler_t b2_handlers[256] = {
453423dc 738 [0x02] = handle_stidp,
6a3f95a6 739 [0x04] = handle_set_clock,
453423dc
CB
740 [0x10] = handle_set_prefix,
741 [0x11] = handle_store_prefix,
742 [0x12] = handle_store_cpu_address,
a3508fbe 743 [0x14] = kvm_s390_handle_vsie,
8a242234 744 [0x21] = handle_ipte_interlock,
a7e19ab5
DH
745 [0x29] = handle_iske,
746 [0x2a] = handle_rrbe,
747 [0x2b] = handle_sske,
aca84241 748 [0x2c] = handle_test_block,
f379aae5
CH
749 [0x30] = handle_io_inst,
750 [0x31] = handle_io_inst,
751 [0x32] = handle_io_inst,
752 [0x33] = handle_io_inst,
753 [0x34] = handle_io_inst,
754 [0x35] = handle_io_inst,
755 [0x36] = handle_io_inst,
756 [0x37] = handle_io_inst,
757 [0x38] = handle_io_inst,
758 [0x39] = handle_io_inst,
759 [0x3a] = handle_io_inst,
760 [0x3b] = handle_io_inst,
761 [0x3c] = handle_io_inst,
8a242234 762 [0x50] = handle_ipte_interlock,
c0a6bfdc 763 [0x56] = handle_sthyi,
f379aae5
CH
764 [0x5f] = handle_io_inst,
765 [0x74] = handle_io_inst,
766 [0x76] = handle_io_inst,
453423dc
CB
767 [0x7d] = handle_stsi,
768 [0xb1] = handle_stfl,
48a3e950 769 [0xb2] = handle_lpswe,
453423dc
CB
770};
771
70455a36 772int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
453423dc
CB
773{
774 intercept_handler_t handler;
775
70455a36 776 /*
5087dfa6
TH
777 * A lot of B2 instructions are priviledged. Here we check for
778 * the privileged ones, that we can handle in the kernel.
779 * Anything else goes to userspace.
780 */
f379aae5 781 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
5087dfa6
TH
782 if (handler)
783 return handler(vcpu);
784
b8e660b8 785 return -EOPNOTSUPP;
453423dc 786}
bb25b9ba 787
48a3e950
CH
788static int handle_epsw(struct kvm_vcpu *vcpu)
789{
790 int reg1, reg2;
791
aeb87c3c 792 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
48a3e950
CH
793
794 /* This basically extracts the mask half of the psw. */
843200e7 795 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
48a3e950
CH
796 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
797 if (reg2) {
843200e7 798 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
48a3e950 799 vcpu->run->s.regs.gprs[reg2] |=
843200e7 800 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
48a3e950
CH
801 }
802 return 0;
803}
804
69d0d3a3
CB
805#define PFMF_RESERVED 0xfffc0101UL
806#define PFMF_SK 0x00020000UL
807#define PFMF_CF 0x00010000UL
808#define PFMF_UI 0x00008000UL
809#define PFMF_FSC 0x00007000UL
810#define PFMF_NQ 0x00000800UL
811#define PFMF_MR 0x00000400UL
812#define PFMF_MC 0x00000200UL
813#define PFMF_KEY 0x000000feUL
814
815static int handle_pfmf(struct kvm_vcpu *vcpu)
816{
1824c723 817 bool mr = false, mc = false, nq;
69d0d3a3
CB
818 int reg1, reg2;
819 unsigned long start, end;
1824c723 820 unsigned char key;
69d0d3a3
CB
821
822 vcpu->stat.instruction_pfmf++;
823
824 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
825
03c02807 826 if (!test_kvm_facility(vcpu->kvm, 8))
69d0d3a3
CB
827 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
828
829 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
208dd756 830 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
69d0d3a3
CB
831
832 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
833 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
834
edc5b055
DH
835 /* Only provide non-quiescing support if enabled for the guest */
836 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
837 !test_kvm_facility(vcpu->kvm, 14))
69d0d3a3
CB
838 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
839
1824c723
DH
840 /* Only provide conditional-SSKE support if enabled for the guest */
841 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
842 test_kvm_facility(vcpu->kvm, 10)) {
843 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
844 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
845 }
846
847 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
848 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
69d0d3a3 849 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
a02689fe 850 start = kvm_s390_logical_to_effective(vcpu, start);
fb34c603 851
6164a2e9
DH
852 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
853 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
854 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
855 }
856
69d0d3a3
CB
857 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
858 case 0x00000000:
6164a2e9
DH
859 /* only 4k frames specify a real address */
860 start = kvm_s390_real_to_abs(vcpu, start);
69d0d3a3
CB
861 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
862 break;
863 case 0x00001000:
864 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
865 break;
69d0d3a3 866 case 0x00002000:
53df84f8
GH
867 /* only support 2G frame size if EDAT2 is available and we are
868 not in 24-bit addressing mode */
869 if (!test_kvm_facility(vcpu->kvm, 78) ||
870 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
871 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
69d0d3a3 872 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
53df84f8 873 break;
69d0d3a3
CB
874 default:
875 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
876 }
a02689fe 877
695be0e7 878 while (start != end) {
6164a2e9 879 unsigned long useraddr;
fb34c603
TH
880
881 /* Translate guest address to host address */
6164a2e9 882 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
fb34c603 883 if (kvm_is_error_hva(useraddr))
69d0d3a3
CB
884 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
885
886 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
887 if (clear_user((void __user *)useraddr, PAGE_SIZE))
888 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
889 }
890
891 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
3ac8e380
DD
892 int rc = __skey_check_enable(vcpu);
893
894 if (rc)
895 return rc;
d3ed1cee 896 down_read(&current->mm->mmap_sem);
1824c723
DH
897 rc = cond_set_guest_storage_key(current->mm, useraddr,
898 key, NULL, nq, mr, mc);
d3ed1cee 899 up_read(&current->mm->mmap_sem);
1824c723 900 if (rc < 0)
69d0d3a3
CB
901 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
902 }
903
904 start += PAGE_SIZE;
905 }
2c26d1d2
DH
906 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
907 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
908 vcpu->run->s.regs.gprs[reg2] = end;
909 } else {
910 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
911 end = kvm_s390_logical_to_effective(vcpu, end);
912 vcpu->run->s.regs.gprs[reg2] |= end;
913 }
914 }
69d0d3a3
CB
915 return 0;
916}
917
b31288fa
KW
918static int handle_essa(struct kvm_vcpu *vcpu)
919{
920 /* entries expected to be 1FF */
921 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
4a5e7e38 922 unsigned long *cbrlo;
b31288fa
KW
923 struct gmap *gmap;
924 int i;
925
7cbde76b 926 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
b31288fa
KW
927 gmap = vcpu->arch.gmap;
928 vcpu->stat.instruction_essa++;
e6db1d61 929 if (!vcpu->kvm->arch.use_cmma)
b31288fa
KW
930 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
931
932 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
933 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
934
935 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
936 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
937
0e8bc06a
DH
938 /* Retry the ESSA instruction */
939 kvm_s390_retry_instr(vcpu);
b31288fa
KW
940 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
941 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
942 down_read(&gmap->mm->mmap_sem);
4a5e7e38
DH
943 for (i = 0; i < entries; ++i)
944 __gmap_zap(gmap, cbrlo[i]);
b31288fa 945 up_read(&gmap->mm->mmap_sem);
b31288fa
KW
946 return 0;
947}
948
48a3e950 949static const intercept_handler_t b9_handlers[256] = {
8a242234 950 [0x8a] = handle_ipte_interlock,
48a3e950 951 [0x8d] = handle_epsw,
8a242234
HC
952 [0x8e] = handle_ipte_interlock,
953 [0x8f] = handle_ipte_interlock,
b31288fa 954 [0xab] = handle_essa,
69d0d3a3 955 [0xaf] = handle_pfmf,
48a3e950
CH
956};
957
958int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
959{
960 intercept_handler_t handler;
961
962 /* This is handled just as for the B2 instructions. */
963 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
5087dfa6
TH
964 if (handler)
965 return handler(vcpu);
966
48a3e950
CH
967 return -EOPNOTSUPP;
968}
969
953ed88d
TH
970int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
971{
972 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
973 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
fc56eb66
HC
974 int reg, rc, nr_regs;
975 u32 ctl_array[16];
f987a3ee 976 u64 ga;
27f67f87 977 u8 ar;
953ed88d
TH
978
979 vcpu->stat.instruction_lctl++;
980
981 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
982 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
983
8ae04b8f 984 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
953ed88d 985
f987a3ee 986 if (ga & 3)
953ed88d
TH
987 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
988
7cbde76b 989 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
f987a3ee 990 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
953ed88d 991
fc56eb66 992 nr_regs = ((reg3 - reg1) & 0xf) + 1;
8ae04b8f 993 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
fc56eb66
HC
994 if (rc)
995 return kvm_s390_inject_prog_cond(vcpu, rc);
953ed88d 996 reg = reg1;
fc56eb66 997 nr_regs = 0;
953ed88d 998 do {
953ed88d 999 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
fc56eb66 1000 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
953ed88d
TH
1001 if (reg == reg3)
1002 break;
1003 reg = (reg + 1) % 16;
1004 } while (1);
2dca485f 1005 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
953ed88d
TH
1006 return 0;
1007}
1008
aba07508
DH
1009int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1010{
1011 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1012 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
fc56eb66
HC
1013 int reg, rc, nr_regs;
1014 u32 ctl_array[16];
aba07508 1015 u64 ga;
27f67f87 1016 u8 ar;
aba07508
DH
1017
1018 vcpu->stat.instruction_stctl++;
1019
1020 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1021 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1022
8ae04b8f 1023 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
aba07508
DH
1024
1025 if (ga & 3)
1026 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1027
7cbde76b 1028 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
aba07508
DH
1029 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1030
1031 reg = reg1;
fc56eb66 1032 nr_regs = 0;
aba07508 1033 do {
fc56eb66 1034 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
aba07508
DH
1035 if (reg == reg3)
1036 break;
1037 reg = (reg + 1) % 16;
1038 } while (1);
8ae04b8f 1039 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
fc56eb66 1040 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
aba07508
DH
1041}
1042
953ed88d
TH
1043static int handle_lctlg(struct kvm_vcpu *vcpu)
1044{
1045 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1046 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
fc56eb66
HC
1047 int reg, rc, nr_regs;
1048 u64 ctl_array[16];
1049 u64 ga;
27f67f87 1050 u8 ar;
953ed88d
TH
1051
1052 vcpu->stat.instruction_lctlg++;
1053
1054 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1055 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1056
8ae04b8f 1057 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
953ed88d 1058
f987a3ee 1059 if (ga & 7)
953ed88d
TH
1060 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1061
7cbde76b 1062 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
f987a3ee 1063 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
953ed88d 1064
fc56eb66 1065 nr_regs = ((reg3 - reg1) & 0xf) + 1;
8ae04b8f 1066 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
fc56eb66
HC
1067 if (rc)
1068 return kvm_s390_inject_prog_cond(vcpu, rc);
1069 reg = reg1;
1070 nr_regs = 0;
953ed88d 1071 do {
fc56eb66 1072 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
953ed88d
TH
1073 if (reg == reg3)
1074 break;
1075 reg = (reg + 1) % 16;
1076 } while (1);
2dca485f 1077 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
953ed88d
TH
1078 return 0;
1079}
1080
aba07508
DH
1081static int handle_stctg(struct kvm_vcpu *vcpu)
1082{
1083 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1084 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
fc56eb66
HC
1085 int reg, rc, nr_regs;
1086 u64 ctl_array[16];
1087 u64 ga;
27f67f87 1088 u8 ar;
aba07508
DH
1089
1090 vcpu->stat.instruction_stctg++;
1091
1092 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1093 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1094
8ae04b8f 1095 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
aba07508
DH
1096
1097 if (ga & 7)
1098 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1099
7cbde76b 1100 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
aba07508
DH
1101 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1102
fc56eb66
HC
1103 reg = reg1;
1104 nr_regs = 0;
aba07508 1105 do {
fc56eb66 1106 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
aba07508
DH
1107 if (reg == reg3)
1108 break;
1109 reg = (reg + 1) % 16;
1110 } while (1);
8ae04b8f 1111 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
fc56eb66 1112 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
aba07508
DH
1113}
1114
f379aae5 1115static const intercept_handler_t eb_handlers[256] = {
953ed88d 1116 [0x2f] = handle_lctlg,
aba07508 1117 [0x25] = handle_stctg,
80cd8763
FZ
1118 [0x60] = handle_ri,
1119 [0x61] = handle_ri,
1120 [0x62] = handle_ri,
f379aae5
CH
1121};
1122
953ed88d 1123int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
f379aae5
CH
1124{
1125 intercept_handler_t handler;
1126
f379aae5
CH
1127 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1128 if (handler)
1129 return handler(vcpu);
1130 return -EOPNOTSUPP;
1131}
1132
bb25b9ba
CB
1133static int handle_tprot(struct kvm_vcpu *vcpu)
1134{
b1c571a5 1135 u64 address1, address2;
a0465f9a
TH
1136 unsigned long hva, gpa;
1137 int ret = 0, cc = 0;
1138 bool writable;
27f67f87 1139 u8 ar;
bb25b9ba
CB
1140
1141 vcpu->stat.instruction_tprot++;
1142
f9f6bbc6
TH
1143 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1144 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1145
8ae04b8f 1146 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
b1c571a5 1147
bb25b9ba
CB
1148 /* we only handle the Linux memory detection case:
1149 * access key == 0
bb25b9ba
CB
1150 * everything else goes to userspace. */
1151 if (address2 & 0xf0)
1152 return -EOPNOTSUPP;
1153 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
a0465f9a 1154 ipte_lock(vcpu);
92c96321 1155 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
a0465f9a
TH
1156 if (ret == PGM_PROTECTION) {
1157 /* Write protected? Try again with read-only... */
1158 cc = 1;
92c96321
DH
1159 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1160 GACC_FETCH);
a0465f9a
TH
1161 }
1162 if (ret) {
1163 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1164 ret = kvm_s390_inject_program_int(vcpu, ret);
1165 } else if (ret > 0) {
1166 /* Translation not available */
1167 kvm_s390_set_psw_cc(vcpu, 3);
1168 ret = 0;
1169 }
1170 goto out_unlock;
1171 }
59a1fa2d 1172
a0465f9a
TH
1173 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1174 if (kvm_is_error_hva(hva)) {
1175 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1176 } else {
1177 if (!writable)
1178 cc = 1; /* Write not permitted ==> read-only */
1179 kvm_s390_set_psw_cc(vcpu, cc);
1180 /* Note: CC2 only occurs for storage keys (not supported yet) */
1181 }
1182out_unlock:
1183 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1184 ipte_unlock(vcpu);
1185 return ret;
bb25b9ba
CB
1186}
1187
1188int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1189{
1190 /* For e5xx... instructions we only handle TPROT */
1191 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1192 return handle_tprot(vcpu);
1193 return -EOPNOTSUPP;
1194}
1195
8c3f61e2
CH
1196static int handle_sckpf(struct kvm_vcpu *vcpu)
1197{
1198 u32 value;
1199
1200 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
208dd756 1201 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
8c3f61e2
CH
1202
1203 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1204 return kvm_s390_inject_program_int(vcpu,
1205 PGM_SPECIFICATION);
1206
1207 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1208 vcpu->arch.sie_block->todpr = value;
1209
1210 return 0;
1211}
1212
9acc317b
DH
1213static int handle_ptff(struct kvm_vcpu *vcpu)
1214{
1215 /* we don't emulate any control instructions yet */
1216 kvm_s390_set_psw_cc(vcpu, 3);
1217 return 0;
1218}
1219
77975357 1220static const intercept_handler_t x01_handlers[256] = {
9acc317b 1221 [0x04] = handle_ptff,
8c3f61e2
CH
1222 [0x07] = handle_sckpf,
1223};
1224
1225int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1226{
1227 intercept_handler_t handler;
1228
1229 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1230 if (handler)
1231 return handler(vcpu);
1232 return -EOPNOTSUPP;
1233}