]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/s390/kvm/priv.c
s390/sclp: add sclp_get_ibc function
[mirror_ubuntu-bionic-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>
7c959e82 18#include <asm/asm-offsets.h>
e769ece3 19#include <asm/facility.h>
453423dc
CB
20#include <asm/current.h>
21#include <asm/debug.h>
22#include <asm/ebcdic.h>
23#include <asm/sysinfo.h>
69d0d3a3
CB
24#include <asm/pgtable.h>
25#include <asm/pgalloc.h>
26#include <asm/io.h>
48a3e950
CH
27#include <asm/ptrace.h>
28#include <asm/compat.h>
453423dc
CB
29#include "gaccess.h"
30#include "kvm-s390.h"
5786fffa 31#include "trace.h"
453423dc 32
6a3f95a6
TH
33/* Handle SCK (SET CLOCK) interception */
34static int handle_set_clock(struct kvm_vcpu *vcpu)
35{
36 struct kvm_vcpu *cpup;
37 s64 hostclk, val;
0e7a3f94 38 int i, rc;
6a3f95a6 39 u64 op2;
6a3f95a6
TH
40
41 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
42 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
43
44 op2 = kvm_s390_get_base_disp_s(vcpu);
45 if (op2 & 7) /* Operand must be on a doubleword boundary */
46 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0e7a3f94
HC
47 rc = read_guest(vcpu, op2, &val, sizeof(val));
48 if (rc)
49 return kvm_s390_inject_prog_cond(vcpu, rc);
6a3f95a6
TH
50
51 if (store_tod_clock(&hostclk)) {
52 kvm_s390_set_psw_cc(vcpu, 3);
53 return 0;
54 }
55 val = (val - hostclk) & ~0x3fUL;
56
57 mutex_lock(&vcpu->kvm->lock);
58 kvm_for_each_vcpu(i, cpup, vcpu->kvm)
59 cpup->arch.sie_block->epoch = val;
60 mutex_unlock(&vcpu->kvm->lock);
61
62 kvm_s390_set_psw_cc(vcpu, 0);
63 return 0;
64}
65
453423dc
CB
66static int handle_set_prefix(struct kvm_vcpu *vcpu)
67{
453423dc 68 u64 operand2;
665170cb
HC
69 u32 address;
70 int rc;
453423dc
CB
71
72 vcpu->stat.instruction_spx++;
73
5087dfa6
TH
74 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
75 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
76
b1c571a5 77 operand2 = kvm_s390_get_base_disp_s(vcpu);
453423dc
CB
78
79 /* must be word boundary */
db4a29cb
HC
80 if (operand2 & 3)
81 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc
CB
82
83 /* get the value */
665170cb
HC
84 rc = read_guest(vcpu, operand2, &address, sizeof(address));
85 if (rc)
86 return kvm_s390_inject_prog_cond(vcpu, rc);
87
88 address &= 0x7fffe000u;
89
90 /*
91 * Make sure the new value is valid memory. We only need to check the
92 * first page, since address is 8k aligned and memory pieces are always
93 * at least 1MB aligned and have at least a size of 1MB.
94 */
95 if (kvm_is_error_gpa(vcpu->kvm, address))
db4a29cb 96 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
453423dc 97
8d26cf7b 98 kvm_s390_set_prefix(vcpu, address);
453423dc
CB
99
100 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
5786fffa 101 trace_kvm_s390_handle_prefix(vcpu, 1, address);
453423dc
CB
102 return 0;
103}
104
105static int handle_store_prefix(struct kvm_vcpu *vcpu)
106{
453423dc
CB
107 u64 operand2;
108 u32 address;
f748f4a7 109 int rc;
453423dc
CB
110
111 vcpu->stat.instruction_stpx++;
b1c571a5 112
5087dfa6
TH
113 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
114 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
115
b1c571a5 116 operand2 = kvm_s390_get_base_disp_s(vcpu);
453423dc
CB
117
118 /* must be word boundary */
db4a29cb
HC
119 if (operand2 & 3)
120 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc
CB
121
122 address = vcpu->arch.sie_block->prefix;
123 address = address & 0x7fffe000u;
124
125 /* get the value */
f748f4a7
HC
126 rc = write_guest(vcpu, operand2, &address, sizeof(address));
127 if (rc)
128 return kvm_s390_inject_prog_cond(vcpu, rc);
453423dc
CB
129
130 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
5786fffa 131 trace_kvm_s390_handle_prefix(vcpu, 0, address);
453423dc
CB
132 return 0;
133}
134
135static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
136{
8b96de0e
HC
137 u16 vcpu_id = vcpu->vcpu_id;
138 u64 ga;
139 int rc;
453423dc
CB
140
141 vcpu->stat.instruction_stap++;
b1c571a5 142
5087dfa6
TH
143 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
144 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
145
8b96de0e 146 ga = kvm_s390_get_base_disp_s(vcpu);
453423dc 147
8b96de0e 148 if (ga & 1)
db4a29cb 149 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc 150
8b96de0e
HC
151 rc = write_guest(vcpu, ga, &vcpu_id, sizeof(vcpu_id));
152 if (rc)
153 return kvm_s390_inject_prog_cond(vcpu, rc);
453423dc 154
8b96de0e
HC
155 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", ga);
156 trace_kvm_s390_handle_stap(vcpu, ga);
453423dc
CB
157 return 0;
158}
159
693ffc08
DD
160static void __skey_check_enable(struct kvm_vcpu *vcpu)
161{
162 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
163 return;
164
165 s390_enable_skey();
166 trace_kvm_s390_skey_related_inst(vcpu);
167 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
168}
169
170
453423dc
CB
171static int handle_skey(struct kvm_vcpu *vcpu)
172{
693ffc08
DD
173 __skey_check_enable(vcpu);
174
453423dc 175 vcpu->stat.instruction_storage_key++;
5087dfa6
TH
176
177 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
178 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
179
dfcf7dc6
MS
180 vcpu->arch.sie_block->gpsw.addr =
181 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
453423dc
CB
182 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
183 return 0;
184}
185
8a242234
HC
186static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
187{
188 psw_t *psw = &vcpu->arch.sie_block->gpsw;
189
190 vcpu->stat.instruction_ipte_interlock++;
191 if (psw_bits(*psw).p)
192 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
193 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
194 psw->addr = __rewind_psw(*psw, 4);
195 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
196 return 0;
197}
198
aca84241
TH
199static int handle_test_block(struct kvm_vcpu *vcpu)
200{
aca84241
TH
201 gpa_t addr;
202 int reg2;
203
204 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
205 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
206
207 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
208 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
e45efa28
TH
209 addr = kvm_s390_logical_to_effective(vcpu, addr);
210 if (kvm_s390_check_low_addr_protection(vcpu, addr))
211 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
aca84241
TH
212 addr = kvm_s390_real_to_abs(vcpu, addr);
213
ef23e779 214 if (kvm_is_error_gpa(vcpu->kvm, addr))
aca84241
TH
215 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
216 /*
217 * We don't expect errors on modern systems, and do not care
218 * about storage keys (yet), so let's just clear the page.
219 */
ef23e779 220 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
aca84241
TH
221 return -EFAULT;
222 kvm_s390_set_psw_cc(vcpu, 0);
223 vcpu->run->s.regs.gprs[0] = 0;
224 return 0;
225}
226
fa6b7fe9 227static int handle_tpi(struct kvm_vcpu *vcpu)
453423dc 228{
fa6b7fe9 229 struct kvm_s390_interrupt_info *inti;
4799b557
HC
230 unsigned long len;
231 u32 tpi_data[3];
232 int cc, rc;
7c959e82 233 u64 addr;
fa6b7fe9 234
4799b557 235 rc = 0;
fa6b7fe9 236 addr = kvm_s390_get_base_disp_s(vcpu);
db4a29cb
HC
237 if (addr & 3)
238 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
7c959e82 239 cc = 0;
f092669e 240 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
7c959e82
HC
241 if (!inti)
242 goto no_interrupt;
243 cc = 1;
4799b557
HC
244 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
245 tpi_data[1] = inti->io.io_int_parm;
246 tpi_data[2] = inti->io.io_int_word;
7c959e82
HC
247 if (addr) {
248 /*
249 * Store the two-word I/O interruption code into the
250 * provided area.
251 */
4799b557
HC
252 len = sizeof(tpi_data) - 4;
253 rc = write_guest(vcpu, addr, &tpi_data, len);
254 if (rc)
255 return kvm_s390_inject_prog_cond(vcpu, rc);
7c959e82
HC
256 } else {
257 /*
258 * Store the three-word I/O interruption code into
259 * the appropriate lowcore area.
260 */
4799b557
HC
261 len = sizeof(tpi_data);
262 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len))
263 rc = -EFAULT;
7c959e82 264 }
2f32d4ea
CH
265 /*
266 * If we encounter a problem storing the interruption code, the
267 * instruction is suppressed from the guest's view: reinject the
268 * interrupt.
269 */
270 if (!rc)
271 kfree(inti);
272 else
273 kvm_s390_reinject_io_int(vcpu->kvm, inti);
7c959e82 274no_interrupt:
fa6b7fe9 275 /* Set condition code and we're done. */
4799b557
HC
276 if (!rc)
277 kvm_s390_set_psw_cc(vcpu, cc);
278 return rc ? -EFAULT : 0;
453423dc
CB
279}
280
fa6b7fe9
CH
281static int handle_tsch(struct kvm_vcpu *vcpu)
282{
283 struct kvm_s390_interrupt_info *inti;
284
285 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
286 vcpu->run->s.regs.gprs[1]);
287
288 /*
289 * Prepare exit to userspace.
290 * We indicate whether we dequeued a pending I/O interrupt
291 * so that userspace can re-inject it if the instruction gets
292 * a program check. While this may re-order the pending I/O
293 * interrupts, this is no problem since the priority is kept
294 * intact.
295 */
296 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
297 vcpu->run->s390_tsch.dequeued = !!inti;
298 if (inti) {
299 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
300 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
301 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
302 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
303 }
304 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
305 kfree(inti);
306 return -EREMOTE;
307}
308
309static int handle_io_inst(struct kvm_vcpu *vcpu)
310{
311 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
312
5087dfa6
TH
313 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
314 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
315
fa6b7fe9
CH
316 if (vcpu->kvm->arch.css_support) {
317 /*
318 * Most I/O instructions will be handled by userspace.
319 * Exceptions are tpi and the interrupt portion of tsch.
320 */
321 if (vcpu->arch.sie_block->ipa == 0xb236)
322 return handle_tpi(vcpu);
323 if (vcpu->arch.sie_block->ipa == 0xb235)
324 return handle_tsch(vcpu);
325 /* Handle in userspace. */
326 return -EOPNOTSUPP;
327 } else {
328 /*
b4a96015 329 * Set condition code 3 to stop the guest from issuing channel
fa6b7fe9
CH
330 * I/O instructions.
331 */
ea828ebf 332 kvm_s390_set_psw_cc(vcpu, 3);
fa6b7fe9
CH
333 return 0;
334 }
335}
336
453423dc
CB
337static int handle_stfl(struct kvm_vcpu *vcpu)
338{
453423dc
CB
339 int rc;
340
341 vcpu->stat.instruction_stfl++;
5087dfa6
TH
342
343 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
344 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
345
0f9701c6
HC
346 rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
347 vfacilities, 4);
dc5008b9 348 if (rc)
0f9701c6 349 return rc;
78c4b59f
MM
350 VCPU_EVENT(vcpu, 5, "store facility list value %x",
351 *(unsigned int *) vfacilities);
352 trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
453423dc
CB
353 return 0;
354}
355
48a3e950
CH
356static void handle_new_psw(struct kvm_vcpu *vcpu)
357{
358 /* Check whether the new psw is enabled for machine checks. */
359 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
360 kvm_s390_deliver_pending_machine_checks(vcpu);
361}
362
363#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
364#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
d21683ea 365#define PSW_ADDR_24 0x0000000000ffffffUL
48a3e950
CH
366#define PSW_ADDR_31 0x000000007fffffffUL
367
a3fb577e
TH
368int is_valid_psw(psw_t *psw)
369{
3736b874
HC
370 if (psw->mask & PSW_MASK_UNASSIGNED)
371 return 0;
372 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
373 if (psw->addr & ~PSW_ADDR_31)
374 return 0;
375 }
376 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
377 return 0;
378 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
379 return 0;
a3fb577e
TH
380 if (psw->addr & 1)
381 return 0;
3736b874
HC
382 return 1;
383}
384
48a3e950
CH
385int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
386{
3736b874 387 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
48a3e950 388 psw_compat_t new_psw;
3736b874 389 u64 addr;
2d8bcaed 390 int rc;
48a3e950 391
3736b874 392 if (gpsw->mask & PSW_MASK_PSTATE)
208dd756
TH
393 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
394
48a3e950 395 addr = kvm_s390_get_base_disp_s(vcpu);
6fd0fcc9
HC
396 if (addr & 7)
397 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
2d8bcaed
HC
398
399 rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
400 if (rc)
401 return kvm_s390_inject_prog_cond(vcpu, rc);
6fd0fcc9
HC
402 if (!(new_psw.mask & PSW32_MASK_BASE))
403 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
3736b874
HC
404 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
405 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
406 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
407 if (!is_valid_psw(gpsw))
6fd0fcc9 408 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
48a3e950 409 handle_new_psw(vcpu);
48a3e950
CH
410 return 0;
411}
412
413static int handle_lpswe(struct kvm_vcpu *vcpu)
414{
48a3e950 415 psw_t new_psw;
3736b874 416 u64 addr;
2d8bcaed 417 int rc;
48a3e950 418
5087dfa6
TH
419 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
420 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
421
48a3e950 422 addr = kvm_s390_get_base_disp_s(vcpu);
6fd0fcc9
HC
423 if (addr & 7)
424 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
2d8bcaed
HC
425 rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
426 if (rc)
427 return kvm_s390_inject_prog_cond(vcpu, rc);
3736b874
HC
428 vcpu->arch.sie_block->gpsw = new_psw;
429 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
6fd0fcc9 430 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
48a3e950 431 handle_new_psw(vcpu);
48a3e950
CH
432 return 0;
433}
434
453423dc
CB
435static int handle_stidp(struct kvm_vcpu *vcpu)
436{
7d777d78 437 u64 stidp_data = vcpu->arch.stidp_data;
453423dc 438 u64 operand2;
7d777d78 439 int rc;
453423dc
CB
440
441 vcpu->stat.instruction_stidp++;
b1c571a5 442
5087dfa6
TH
443 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
444 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
445
b1c571a5 446 operand2 = kvm_s390_get_base_disp_s(vcpu);
453423dc 447
db4a29cb
HC
448 if (operand2 & 7)
449 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc 450
7d777d78
HC
451 rc = write_guest(vcpu, operand2, &stidp_data, sizeof(stidp_data));
452 if (rc)
453 return kvm_s390_inject_prog_cond(vcpu, rc);
453423dc
CB
454
455 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
453423dc
CB
456 return 0;
457}
458
459static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
460{
453423dc
CB
461 int cpus = 0;
462 int n;
463
ff520a63 464 cpus = atomic_read(&vcpu->kvm->online_vcpus);
453423dc
CB
465
466 /* deal with other level 3 hypervisors */
caf757c6 467 if (stsi(mem, 3, 2, 2))
453423dc
CB
468 mem->count = 0;
469 if (mem->count < 8)
470 mem->count++;
471 for (n = mem->count - 1; n > 0 ; n--)
472 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
473
474 mem->vm[0].cpus_total = cpus;
475 mem->vm[0].cpus_configured = cpus;
476 mem->vm[0].cpus_standby = 0;
477 mem->vm[0].cpus_reserved = 0;
478 mem->vm[0].caf = 1000;
479 memcpy(mem->vm[0].name, "KVMguest", 8);
480 ASCEBC(mem->vm[0].name, 8);
481 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
482 ASCEBC(mem->vm[0].cpi, 16);
483}
484
485static int handle_stsi(struct kvm_vcpu *vcpu)
486{
5a32c1af
CB
487 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
488 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
489 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
c51f068c 490 unsigned long mem = 0;
453423dc 491 u64 operand2;
db4a29cb 492 int rc = 0;
453423dc
CB
493
494 vcpu->stat.instruction_stsi++;
495 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
496
5087dfa6
TH
497 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
498 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
499
87d41fb4 500 if (fc > 3) {
ea828ebf 501 kvm_s390_set_psw_cc(vcpu, 3);
87d41fb4
TH
502 return 0;
503 }
453423dc 504
87d41fb4
TH
505 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
506 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
453423dc
CB
507 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
508
87d41fb4 509 if (fc == 0) {
5a32c1af 510 vcpu->run->s.regs.gprs[0] = 3 << 28;
ea828ebf 511 kvm_s390_set_psw_cc(vcpu, 0);
453423dc 512 return 0;
87d41fb4
TH
513 }
514
515 operand2 = kvm_s390_get_base_disp_s(vcpu);
516
517 if (operand2 & 0xfff)
518 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
519
520 switch (fc) {
453423dc
CB
521 case 1: /* same handling for 1 and 2 */
522 case 2:
523 mem = get_zeroed_page(GFP_KERNEL);
524 if (!mem)
c51f068c 525 goto out_no_data;
caf757c6 526 if (stsi((void *) mem, fc, sel1, sel2))
c51f068c 527 goto out_no_data;
453423dc
CB
528 break;
529 case 3:
530 if (sel1 != 2 || sel2 != 2)
c51f068c 531 goto out_no_data;
453423dc
CB
532 mem = get_zeroed_page(GFP_KERNEL);
533 if (!mem)
c51f068c 534 goto out_no_data;
453423dc
CB
535 handle_stsi_3_2_2(vcpu, (void *) mem);
536 break;
453423dc
CB
537 }
538
645c5bc1
HC
539 rc = write_guest(vcpu, operand2, (void *)mem, PAGE_SIZE);
540 if (rc) {
541 rc = kvm_s390_inject_prog_cond(vcpu, rc);
542 goto out;
453423dc 543 }
5786fffa 544 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
453423dc 545 free_page(mem);
ea828ebf 546 kvm_s390_set_psw_cc(vcpu, 0);
5a32c1af 547 vcpu->run->s.regs.gprs[0] = 0;
453423dc 548 return 0;
c51f068c 549out_no_data:
ea828ebf 550 kvm_s390_set_psw_cc(vcpu, 3);
645c5bc1 551out:
c51f068c 552 free_page(mem);
db4a29cb 553 return rc;
453423dc
CB
554}
555
f379aae5 556static const intercept_handler_t b2_handlers[256] = {
453423dc 557 [0x02] = handle_stidp,
6a3f95a6 558 [0x04] = handle_set_clock,
453423dc
CB
559 [0x10] = handle_set_prefix,
560 [0x11] = handle_store_prefix,
561 [0x12] = handle_store_cpu_address,
8a242234 562 [0x21] = handle_ipte_interlock,
453423dc
CB
563 [0x29] = handle_skey,
564 [0x2a] = handle_skey,
565 [0x2b] = handle_skey,
aca84241 566 [0x2c] = handle_test_block,
f379aae5
CH
567 [0x30] = handle_io_inst,
568 [0x31] = handle_io_inst,
569 [0x32] = handle_io_inst,
570 [0x33] = handle_io_inst,
571 [0x34] = handle_io_inst,
572 [0x35] = handle_io_inst,
573 [0x36] = handle_io_inst,
574 [0x37] = handle_io_inst,
575 [0x38] = handle_io_inst,
576 [0x39] = handle_io_inst,
577 [0x3a] = handle_io_inst,
578 [0x3b] = handle_io_inst,
579 [0x3c] = handle_io_inst,
8a242234 580 [0x50] = handle_ipte_interlock,
f379aae5
CH
581 [0x5f] = handle_io_inst,
582 [0x74] = handle_io_inst,
583 [0x76] = handle_io_inst,
453423dc
CB
584 [0x7d] = handle_stsi,
585 [0xb1] = handle_stfl,
48a3e950 586 [0xb2] = handle_lpswe,
453423dc
CB
587};
588
70455a36 589int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
453423dc
CB
590{
591 intercept_handler_t handler;
592
70455a36 593 /*
5087dfa6
TH
594 * A lot of B2 instructions are priviledged. Here we check for
595 * the privileged ones, that we can handle in the kernel.
596 * Anything else goes to userspace.
597 */
f379aae5 598 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
5087dfa6
TH
599 if (handler)
600 return handler(vcpu);
601
b8e660b8 602 return -EOPNOTSUPP;
453423dc 603}
bb25b9ba 604
48a3e950
CH
605static int handle_epsw(struct kvm_vcpu *vcpu)
606{
607 int reg1, reg2;
608
aeb87c3c 609 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
48a3e950
CH
610
611 /* This basically extracts the mask half of the psw. */
843200e7 612 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
48a3e950
CH
613 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
614 if (reg2) {
843200e7 615 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
48a3e950 616 vcpu->run->s.regs.gprs[reg2] |=
843200e7 617 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
48a3e950
CH
618 }
619 return 0;
620}
621
69d0d3a3
CB
622#define PFMF_RESERVED 0xfffc0101UL
623#define PFMF_SK 0x00020000UL
624#define PFMF_CF 0x00010000UL
625#define PFMF_UI 0x00008000UL
626#define PFMF_FSC 0x00007000UL
627#define PFMF_NQ 0x00000800UL
628#define PFMF_MR 0x00000400UL
629#define PFMF_MC 0x00000200UL
630#define PFMF_KEY 0x000000feUL
631
632static int handle_pfmf(struct kvm_vcpu *vcpu)
633{
634 int reg1, reg2;
635 unsigned long start, end;
636
637 vcpu->stat.instruction_pfmf++;
638
639 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
640
641 if (!MACHINE_HAS_PFMF)
642 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
643
644 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
208dd756 645 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
69d0d3a3
CB
646
647 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
648 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
649
650 /* Only provide non-quiescing support if the host supports it */
e769ece3 651 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
69d0d3a3
CB
652 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
653
654 /* No support for conditional-SSKE */
655 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
656 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
657
658 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
fb34c603
TH
659 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
660 if (kvm_s390_check_low_addr_protection(vcpu, start))
661 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
662 }
663
69d0d3a3
CB
664 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
665 case 0x00000000:
666 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
667 break;
668 case 0x00001000:
669 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
670 break;
671 /* We dont support EDAT2
672 case 0x00002000:
673 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
674 break;*/
675 default:
676 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
677 }
678 while (start < end) {
fb34c603
TH
679 unsigned long useraddr, abs_addr;
680
681 /* Translate guest address to host address */
682 if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
683 abs_addr = kvm_s390_real_to_abs(vcpu, start);
684 else
685 abs_addr = start;
686 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
687 if (kvm_is_error_hva(useraddr))
69d0d3a3
CB
688 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
689
690 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
691 if (clear_user((void __user *)useraddr, PAGE_SIZE))
692 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
693 }
694
695 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
693ffc08 696 __skey_check_enable(vcpu);
69d0d3a3
CB
697 if (set_guest_storage_key(current->mm, useraddr,
698 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
699 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
700 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
701 }
702
703 start += PAGE_SIZE;
704 }
705 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
706 vcpu->run->s.regs.gprs[reg2] = end;
707 return 0;
708}
709
b31288fa
KW
710static int handle_essa(struct kvm_vcpu *vcpu)
711{
712 /* entries expected to be 1FF */
713 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
714 unsigned long *cbrlo, cbrle;
715 struct gmap *gmap;
716 int i;
717
718 VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
719 gmap = vcpu->arch.gmap;
720 vcpu->stat.instruction_essa++;
b31605c1 721 if (!kvm_s390_cmma_enabled(vcpu->kvm))
b31288fa
KW
722 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
723
724 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
725 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
726
727 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
728 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
729
730 /* Rewind PSW to repeat the ESSA instruction */
731 vcpu->arch.sie_block->gpsw.addr =
732 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
733 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
734 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
735 down_read(&gmap->mm->mmap_sem);
736 for (i = 0; i < entries; ++i) {
737 cbrle = cbrlo[i];
738 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
739 /* invalid entry */
740 break;
741 /* try to free backing */
742 __gmap_zap(cbrle, gmap);
743 }
744 up_read(&gmap->mm->mmap_sem);
745 if (i < entries)
746 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
747 return 0;
748}
749
48a3e950 750static const intercept_handler_t b9_handlers[256] = {
8a242234 751 [0x8a] = handle_ipte_interlock,
48a3e950 752 [0x8d] = handle_epsw,
8a242234
HC
753 [0x8e] = handle_ipte_interlock,
754 [0x8f] = handle_ipte_interlock,
b31288fa 755 [0xab] = handle_essa,
69d0d3a3 756 [0xaf] = handle_pfmf,
48a3e950
CH
757};
758
759int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
760{
761 intercept_handler_t handler;
762
763 /* This is handled just as for the B2 instructions. */
764 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
5087dfa6
TH
765 if (handler)
766 return handler(vcpu);
767
48a3e950
CH
768 return -EOPNOTSUPP;
769}
770
953ed88d
TH
771int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
772{
773 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
774 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
953ed88d
TH
775 u32 val = 0;
776 int reg, rc;
f987a3ee 777 u64 ga;
953ed88d
TH
778
779 vcpu->stat.instruction_lctl++;
780
781 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
782 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
783
f987a3ee 784 ga = kvm_s390_get_base_disp_rs(vcpu);
953ed88d 785
f987a3ee 786 if (ga & 3)
953ed88d
TH
787 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
788
f987a3ee
HC
789 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
790 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
953ed88d
TH
791
792 reg = reg1;
793 do {
f987a3ee 794 rc = read_guest(vcpu, ga, &val, sizeof(val));
953ed88d 795 if (rc)
f987a3ee 796 return kvm_s390_inject_prog_cond(vcpu, rc);
953ed88d
TH
797 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
798 vcpu->arch.sie_block->gcr[reg] |= val;
f987a3ee 799 ga += 4;
953ed88d
TH
800 if (reg == reg3)
801 break;
802 reg = (reg + 1) % 16;
803 } while (1);
804
805 return 0;
806}
807
aba07508
DH
808int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
809{
810 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
811 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
812 u64 ga;
813 u32 val;
814 int reg, rc;
815
816 vcpu->stat.instruction_stctl++;
817
818 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
819 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
820
821 ga = kvm_s390_get_base_disp_rs(vcpu);
822
823 if (ga & 3)
824 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
825
826 VCPU_EVENT(vcpu, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
827 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
828
829 reg = reg1;
830 do {
831 val = vcpu->arch.sie_block->gcr[reg] & 0x00000000fffffffful;
832 rc = write_guest(vcpu, ga, &val, sizeof(val));
833 if (rc)
834 return kvm_s390_inject_prog_cond(vcpu, rc);
835 ga += 4;
836 if (reg == reg3)
837 break;
838 reg = (reg + 1) % 16;
839 } while (1);
840
841 return 0;
842}
843
953ed88d
TH
844static int handle_lctlg(struct kvm_vcpu *vcpu)
845{
846 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
847 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
f987a3ee 848 u64 ga, val;
953ed88d
TH
849 int reg, rc;
850
851 vcpu->stat.instruction_lctlg++;
852
853 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
854 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
855
f987a3ee 856 ga = kvm_s390_get_base_disp_rsy(vcpu);
953ed88d 857
f987a3ee 858 if (ga & 7)
953ed88d
TH
859 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
860
861 reg = reg1;
862
f987a3ee
HC
863 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
864 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
953ed88d
TH
865
866 do {
f987a3ee 867 rc = read_guest(vcpu, ga, &val, sizeof(val));
953ed88d 868 if (rc)
f987a3ee
HC
869 return kvm_s390_inject_prog_cond(vcpu, rc);
870 vcpu->arch.sie_block->gcr[reg] = val;
871 ga += 8;
953ed88d
TH
872 if (reg == reg3)
873 break;
874 reg = (reg + 1) % 16;
875 } while (1);
876
877 return 0;
878}
879
aba07508
DH
880static int handle_stctg(struct kvm_vcpu *vcpu)
881{
882 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
883 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
884 u64 ga, val;
885 int reg, rc;
886
887 vcpu->stat.instruction_stctg++;
888
889 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
890 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
891
892 ga = kvm_s390_get_base_disp_rsy(vcpu);
893
894 if (ga & 7)
895 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
896
897 reg = reg1;
898
899 VCPU_EVENT(vcpu, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
900 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
901
902 do {
903 val = vcpu->arch.sie_block->gcr[reg];
904 rc = write_guest(vcpu, ga, &val, sizeof(val));
905 if (rc)
906 return kvm_s390_inject_prog_cond(vcpu, rc);
907 ga += 8;
908 if (reg == reg3)
909 break;
910 reg = (reg + 1) % 16;
911 } while (1);
912
913 return 0;
914}
915
f379aae5 916static const intercept_handler_t eb_handlers[256] = {
953ed88d 917 [0x2f] = handle_lctlg,
aba07508 918 [0x25] = handle_stctg,
f379aae5
CH
919};
920
953ed88d 921int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
f379aae5
CH
922{
923 intercept_handler_t handler;
924
f379aae5
CH
925 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
926 if (handler)
927 return handler(vcpu);
928 return -EOPNOTSUPP;
929}
930
bb25b9ba
CB
931static int handle_tprot(struct kvm_vcpu *vcpu)
932{
b1c571a5 933 u64 address1, address2;
bb25b9ba 934 struct vm_area_struct *vma;
1eddb85f 935 unsigned long user_address;
bb25b9ba
CB
936
937 vcpu->stat.instruction_tprot++;
938
f9f6bbc6
TH
939 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
940 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
941
b1c571a5
CH
942 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
943
bb25b9ba
CB
944 /* we only handle the Linux memory detection case:
945 * access key == 0
946 * guest DAT == off
947 * everything else goes to userspace. */
948 if (address2 & 0xf0)
949 return -EOPNOTSUPP;
950 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
951 return -EOPNOTSUPP;
952
bb25b9ba 953 down_read(&current->mm->mmap_sem);
59a1fa2d
HC
954 user_address = __gmap_translate(address1, vcpu->arch.gmap);
955 if (IS_ERR_VALUE(user_address))
956 goto out_inject;
1eddb85f 957 vma = find_vma(current->mm, user_address);
59a1fa2d
HC
958 if (!vma)
959 goto out_inject;
bb25b9ba
CB
960 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
961 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
962 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
963 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
964 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
965
966 up_read(&current->mm->mmap_sem);
967 return 0;
59a1fa2d
HC
968
969out_inject:
970 up_read(&current->mm->mmap_sem);
971 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
bb25b9ba
CB
972}
973
974int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
975{
976 /* For e5xx... instructions we only handle TPROT */
977 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
978 return handle_tprot(vcpu);
979 return -EOPNOTSUPP;
980}
981
8c3f61e2
CH
982static int handle_sckpf(struct kvm_vcpu *vcpu)
983{
984 u32 value;
985
986 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
208dd756 987 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
8c3f61e2
CH
988
989 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
990 return kvm_s390_inject_program_int(vcpu,
991 PGM_SPECIFICATION);
992
993 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
994 vcpu->arch.sie_block->todpr = value;
995
996 return 0;
997}
998
77975357 999static const intercept_handler_t x01_handlers[256] = {
8c3f61e2
CH
1000 [0x07] = handle_sckpf,
1001};
1002
1003int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1004{
1005 intercept_handler_t handler;
1006
1007 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1008 if (handler)
1009 return handler(vcpu);
1010 return -EOPNOTSUPP;
1011}