]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/s390/kvm/priv.c
KVM: s390: Privileged operation check for TPROT
[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>
7c959e82 18#include <asm/asm-offsets.h>
453423dc
CB
19#include <asm/current.h>
20#include <asm/debug.h>
21#include <asm/ebcdic.h>
22#include <asm/sysinfo.h>
69d0d3a3
CB
23#include <asm/pgtable.h>
24#include <asm/pgalloc.h>
25#include <asm/io.h>
48a3e950
CH
26#include <asm/ptrace.h>
27#include <asm/compat.h>
453423dc
CB
28#include "gaccess.h"
29#include "kvm-s390.h"
5786fffa 30#include "trace.h"
453423dc
CB
31
32static int handle_set_prefix(struct kvm_vcpu *vcpu)
33{
453423dc
CB
34 u64 operand2;
35 u32 address = 0;
36 u8 tmp;
37
38 vcpu->stat.instruction_spx++;
39
b1c571a5 40 operand2 = kvm_s390_get_base_disp_s(vcpu);
453423dc
CB
41
42 /* must be word boundary */
db4a29cb
HC
43 if (operand2 & 3)
44 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc
CB
45
46 /* get the value */
db4a29cb
HC
47 if (get_guest(vcpu, address, (u32 __user *) operand2))
48 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
453423dc
CB
49
50 address = address & 0x7fffe000u;
51
52 /* make sure that the new value is valid memory */
53 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
db4a29cb
HC
54 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)))
55 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
453423dc 56
8d26cf7b 57 kvm_s390_set_prefix(vcpu, address);
453423dc
CB
58
59 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
5786fffa 60 trace_kvm_s390_handle_prefix(vcpu, 1, address);
453423dc
CB
61 return 0;
62}
63
64static int handle_store_prefix(struct kvm_vcpu *vcpu)
65{
453423dc
CB
66 u64 operand2;
67 u32 address;
68
69 vcpu->stat.instruction_stpx++;
b1c571a5
CH
70
71 operand2 = kvm_s390_get_base_disp_s(vcpu);
453423dc
CB
72
73 /* must be word boundary */
db4a29cb
HC
74 if (operand2 & 3)
75 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc
CB
76
77 address = vcpu->arch.sie_block->prefix;
78 address = address & 0x7fffe000u;
79
80 /* get the value */
db4a29cb
HC
81 if (put_guest(vcpu, address, (u32 __user *)operand2))
82 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
453423dc
CB
83
84 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
5786fffa 85 trace_kvm_s390_handle_prefix(vcpu, 0, address);
453423dc
CB
86 return 0;
87}
88
89static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
90{
453423dc 91 u64 useraddr;
453423dc
CB
92
93 vcpu->stat.instruction_stap++;
b1c571a5
CH
94
95 useraddr = kvm_s390_get_base_disp_s(vcpu);
453423dc 96
db4a29cb
HC
97 if (useraddr & 1)
98 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc 99
db4a29cb
HC
100 if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
101 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
453423dc 102
33e19115 103 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
5786fffa 104 trace_kvm_s390_handle_stap(vcpu, useraddr);
453423dc
CB
105 return 0;
106}
107
108static int handle_skey(struct kvm_vcpu *vcpu)
109{
110 vcpu->stat.instruction_storage_key++;
dfcf7dc6
MS
111 vcpu->arch.sie_block->gpsw.addr =
112 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
453423dc
CB
113 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
114 return 0;
115}
116
fa6b7fe9 117static int handle_tpi(struct kvm_vcpu *vcpu)
453423dc 118{
fa6b7fe9 119 struct kvm_s390_interrupt_info *inti;
7c959e82 120 u64 addr;
fa6b7fe9
CH
121 int cc;
122
123 addr = kvm_s390_get_base_disp_s(vcpu);
db4a29cb
HC
124 if (addr & 3)
125 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
7c959e82 126 cc = 0;
fa6b7fe9 127 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
7c959e82
HC
128 if (!inti)
129 goto no_interrupt;
130 cc = 1;
131 if (addr) {
132 /*
133 * Store the two-word I/O interruption code into the
134 * provided area.
135 */
0a75ca27
HC
136 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) addr);
137 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) (addr + 2));
138 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) (addr + 4));
7c959e82
HC
139 } else {
140 /*
141 * Store the three-word I/O interruption code into
142 * the appropriate lowcore area.
143 */
0a75ca27
HC
144 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
145 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
146 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
147 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
7c959e82 148 }
fa6b7fe9 149 kfree(inti);
7c959e82 150no_interrupt:
fa6b7fe9 151 /* Set condition code and we're done. */
453423dc 152 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
fa6b7fe9 153 vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44;
453423dc
CB
154 return 0;
155}
156
fa6b7fe9
CH
157static int handle_tsch(struct kvm_vcpu *vcpu)
158{
159 struct kvm_s390_interrupt_info *inti;
160
161 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
162 vcpu->run->s.regs.gprs[1]);
163
164 /*
165 * Prepare exit to userspace.
166 * We indicate whether we dequeued a pending I/O interrupt
167 * so that userspace can re-inject it if the instruction gets
168 * a program check. While this may re-order the pending I/O
169 * interrupts, this is no problem since the priority is kept
170 * intact.
171 */
172 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
173 vcpu->run->s390_tsch.dequeued = !!inti;
174 if (inti) {
175 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
176 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
177 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
178 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
179 }
180 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
181 kfree(inti);
182 return -EREMOTE;
183}
184
185static int handle_io_inst(struct kvm_vcpu *vcpu)
186{
187 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
188
189 if (vcpu->kvm->arch.css_support) {
190 /*
191 * Most I/O instructions will be handled by userspace.
192 * Exceptions are tpi and the interrupt portion of tsch.
193 */
194 if (vcpu->arch.sie_block->ipa == 0xb236)
195 return handle_tpi(vcpu);
196 if (vcpu->arch.sie_block->ipa == 0xb235)
197 return handle_tsch(vcpu);
198 /* Handle in userspace. */
199 return -EOPNOTSUPP;
200 } else {
201 /*
202 * Set condition code 3 to stop the guest from issueing channel
203 * I/O instructions.
204 */
205 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
206 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
207 return 0;
208 }
209}
210
453423dc
CB
211static int handle_stfl(struct kvm_vcpu *vcpu)
212{
14375bc4 213 unsigned int facility_list;
453423dc
CB
214 int rc;
215
216 vcpu->stat.instruction_stfl++;
a0046b6d 217 /* only pass the facility bits, which we can handle */
69d0d3a3 218 facility_list = S390_lowcore.stfl_fac_list & 0xff82fff3;
453423dc
CB
219
220 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
221 &facility_list, sizeof(facility_list));
dc5008b9 222 if (rc)
db4a29cb
HC
223 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
224 VCPU_EVENT(vcpu, 5, "store facility list value %x", facility_list);
225 trace_kvm_s390_handle_stfl(vcpu, facility_list);
453423dc
CB
226 return 0;
227}
228
48a3e950
CH
229static void handle_new_psw(struct kvm_vcpu *vcpu)
230{
231 /* Check whether the new psw is enabled for machine checks. */
232 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
233 kvm_s390_deliver_pending_machine_checks(vcpu);
234}
235
236#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
237#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
d21683ea 238#define PSW_ADDR_24 0x0000000000ffffffUL
48a3e950
CH
239#define PSW_ADDR_31 0x000000007fffffffUL
240
3736b874
HC
241static int is_valid_psw(psw_t *psw) {
242 if (psw->mask & PSW_MASK_UNASSIGNED)
243 return 0;
244 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
245 if (psw->addr & ~PSW_ADDR_31)
246 return 0;
247 }
248 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
249 return 0;
250 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
251 return 0;
252 return 1;
253}
254
48a3e950
CH
255int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
256{
3736b874 257 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
48a3e950 258 psw_compat_t new_psw;
3736b874 259 u64 addr;
48a3e950 260
3736b874 261 if (gpsw->mask & PSW_MASK_PSTATE)
208dd756
TH
262 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
263
48a3e950 264 addr = kvm_s390_get_base_disp_s(vcpu);
6fd0fcc9
HC
265 if (addr & 7)
266 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
6fd0fcc9
HC
267 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
268 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
6fd0fcc9
HC
269 if (!(new_psw.mask & PSW32_MASK_BASE))
270 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
3736b874
HC
271 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
272 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
273 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
274 if (!is_valid_psw(gpsw))
6fd0fcc9 275 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
48a3e950 276 handle_new_psw(vcpu);
48a3e950
CH
277 return 0;
278}
279
280static int handle_lpswe(struct kvm_vcpu *vcpu)
281{
48a3e950 282 psw_t new_psw;
3736b874 283 u64 addr;
48a3e950
CH
284
285 addr = kvm_s390_get_base_disp_s(vcpu);
6fd0fcc9
HC
286 if (addr & 7)
287 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
6fd0fcc9
HC
288 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
289 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
3736b874
HC
290 vcpu->arch.sie_block->gpsw = new_psw;
291 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
6fd0fcc9 292 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
48a3e950 293 handle_new_psw(vcpu);
48a3e950
CH
294 return 0;
295}
296
453423dc
CB
297static int handle_stidp(struct kvm_vcpu *vcpu)
298{
453423dc 299 u64 operand2;
453423dc
CB
300
301 vcpu->stat.instruction_stidp++;
b1c571a5
CH
302
303 operand2 = kvm_s390_get_base_disp_s(vcpu);
453423dc 304
db4a29cb
HC
305 if (operand2 & 7)
306 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
453423dc 307
db4a29cb
HC
308 if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
309 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
453423dc
CB
310
311 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
453423dc
CB
312 return 0;
313}
314
315static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
316{
180c12fb 317 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
453423dc
CB
318 int cpus = 0;
319 int n;
320
b037a4f3 321 spin_lock(&fi->lock);
453423dc
CB
322 for (n = 0; n < KVM_MAX_VCPUS; n++)
323 if (fi->local_int[n])
324 cpus++;
b037a4f3 325 spin_unlock(&fi->lock);
453423dc
CB
326
327 /* deal with other level 3 hypervisors */
caf757c6 328 if (stsi(mem, 3, 2, 2))
453423dc
CB
329 mem->count = 0;
330 if (mem->count < 8)
331 mem->count++;
332 for (n = mem->count - 1; n > 0 ; n--)
333 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
334
335 mem->vm[0].cpus_total = cpus;
336 mem->vm[0].cpus_configured = cpus;
337 mem->vm[0].cpus_standby = 0;
338 mem->vm[0].cpus_reserved = 0;
339 mem->vm[0].caf = 1000;
340 memcpy(mem->vm[0].name, "KVMguest", 8);
341 ASCEBC(mem->vm[0].name, 8);
342 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
343 ASCEBC(mem->vm[0].cpi, 16);
344}
345
346static int handle_stsi(struct kvm_vcpu *vcpu)
347{
5a32c1af
CB
348 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
349 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
350 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
c51f068c 351 unsigned long mem = 0;
453423dc 352 u64 operand2;
db4a29cb 353 int rc = 0;
453423dc
CB
354
355 vcpu->stat.instruction_stsi++;
356 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
357
b1c571a5 358 operand2 = kvm_s390_get_base_disp_s(vcpu);
453423dc
CB
359
360 if (operand2 & 0xfff && fc > 0)
361 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
362
363 switch (fc) {
364 case 0:
5a32c1af 365 vcpu->run->s.regs.gprs[0] = 3 << 28;
453423dc
CB
366 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
367 return 0;
368 case 1: /* same handling for 1 and 2 */
369 case 2:
370 mem = get_zeroed_page(GFP_KERNEL);
371 if (!mem)
c51f068c 372 goto out_no_data;
caf757c6 373 if (stsi((void *) mem, fc, sel1, sel2))
c51f068c 374 goto out_no_data;
453423dc
CB
375 break;
376 case 3:
377 if (sel1 != 2 || sel2 != 2)
c51f068c 378 goto out_no_data;
453423dc
CB
379 mem = get_zeroed_page(GFP_KERNEL);
380 if (!mem)
c51f068c 381 goto out_no_data;
453423dc
CB
382 handle_stsi_3_2_2(vcpu, (void *) mem);
383 break;
384 default:
c51f068c 385 goto out_no_data;
453423dc
CB
386 }
387
388 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
db4a29cb 389 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
c51f068c 390 goto out_exception;
453423dc 391 }
5786fffa 392 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
453423dc
CB
393 free_page(mem);
394 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
5a32c1af 395 vcpu->run->s.regs.gprs[0] = 0;
453423dc 396 return 0;
c51f068c 397out_no_data:
453423dc
CB
398 /* condition code 3 */
399 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
c51f068c
HC
400out_exception:
401 free_page(mem);
db4a29cb 402 return rc;
453423dc
CB
403}
404
f379aae5 405static const intercept_handler_t b2_handlers[256] = {
453423dc
CB
406 [0x02] = handle_stidp,
407 [0x10] = handle_set_prefix,
408 [0x11] = handle_store_prefix,
409 [0x12] = handle_store_cpu_address,
410 [0x29] = handle_skey,
411 [0x2a] = handle_skey,
412 [0x2b] = handle_skey,
f379aae5
CH
413 [0x30] = handle_io_inst,
414 [0x31] = handle_io_inst,
415 [0x32] = handle_io_inst,
416 [0x33] = handle_io_inst,
417 [0x34] = handle_io_inst,
418 [0x35] = handle_io_inst,
419 [0x36] = handle_io_inst,
420 [0x37] = handle_io_inst,
421 [0x38] = handle_io_inst,
422 [0x39] = handle_io_inst,
423 [0x3a] = handle_io_inst,
424 [0x3b] = handle_io_inst,
425 [0x3c] = handle_io_inst,
426 [0x5f] = handle_io_inst,
427 [0x74] = handle_io_inst,
428 [0x76] = handle_io_inst,
453423dc
CB
429 [0x7d] = handle_stsi,
430 [0xb1] = handle_stfl,
48a3e950 431 [0xb2] = handle_lpswe,
453423dc
CB
432};
433
70455a36 434int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
453423dc
CB
435{
436 intercept_handler_t handler;
437
70455a36
CB
438 /*
439 * a lot of B2 instructions are priviledged. We first check for
25985edc 440 * the privileged ones, that we can handle in the kernel. If the
70455a36
CB
441 * kernel can handle this instruction, we check for the problem
442 * state bit and (a) handle the instruction or (b) send a code 2
443 * program check.
444 * Anything else goes to userspace.*/
f379aae5 445 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
70455a36
CB
446 if (handler) {
447 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
448 return kvm_s390_inject_program_int(vcpu,
208dd756 449 PGM_PRIVILEGED_OP);
70455a36
CB
450 else
451 return handler(vcpu);
452 }
b8e660b8 453 return -EOPNOTSUPP;
453423dc 454}
bb25b9ba 455
48a3e950
CH
456static int handle_epsw(struct kvm_vcpu *vcpu)
457{
458 int reg1, reg2;
459
aeb87c3c 460 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
48a3e950
CH
461
462 /* This basically extracts the mask half of the psw. */
463 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000;
464 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
465 if (reg2) {
466 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000;
467 vcpu->run->s.regs.gprs[reg2] |=
468 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff;
469 }
470 return 0;
471}
472
69d0d3a3
CB
473#define PFMF_RESERVED 0xfffc0101UL
474#define PFMF_SK 0x00020000UL
475#define PFMF_CF 0x00010000UL
476#define PFMF_UI 0x00008000UL
477#define PFMF_FSC 0x00007000UL
478#define PFMF_NQ 0x00000800UL
479#define PFMF_MR 0x00000400UL
480#define PFMF_MC 0x00000200UL
481#define PFMF_KEY 0x000000feUL
482
483static int handle_pfmf(struct kvm_vcpu *vcpu)
484{
485 int reg1, reg2;
486 unsigned long start, end;
487
488 vcpu->stat.instruction_pfmf++;
489
490 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
491
492 if (!MACHINE_HAS_PFMF)
493 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
494
495 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
208dd756 496 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
69d0d3a3
CB
497
498 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
499 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
500
501 /* Only provide non-quiescing support if the host supports it */
502 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
503 S390_lowcore.stfl_fac_list & 0x00020000)
504 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
505
506 /* No support for conditional-SSKE */
507 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
508 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
509
510 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
511 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
512 case 0x00000000:
513 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
514 break;
515 case 0x00001000:
516 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
517 break;
518 /* We dont support EDAT2
519 case 0x00002000:
520 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
521 break;*/
522 default:
523 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
524 }
525 while (start < end) {
526 unsigned long useraddr;
527
528 useraddr = gmap_translate(start, vcpu->arch.gmap);
529 if (IS_ERR((void *)useraddr))
530 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
531
532 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
533 if (clear_user((void __user *)useraddr, PAGE_SIZE))
534 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
535 }
536
537 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
538 if (set_guest_storage_key(current->mm, useraddr,
539 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
540 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
541 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
542 }
543
544 start += PAGE_SIZE;
545 }
546 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
547 vcpu->run->s.regs.gprs[reg2] = end;
548 return 0;
549}
550
48a3e950
CH
551static const intercept_handler_t b9_handlers[256] = {
552 [0x8d] = handle_epsw,
f379aae5 553 [0x9c] = handle_io_inst,
69d0d3a3 554 [0xaf] = handle_pfmf,
48a3e950
CH
555};
556
557int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
558{
559 intercept_handler_t handler;
560
561 /* This is handled just as for the B2 instructions. */
562 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
563 if (handler) {
564 if ((handler != handle_epsw) &&
565 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE))
566 return kvm_s390_inject_program_int(vcpu,
208dd756 567 PGM_PRIVILEGED_OP);
48a3e950
CH
568 else
569 return handler(vcpu);
570 }
571 return -EOPNOTSUPP;
572}
573
f379aae5
CH
574static const intercept_handler_t eb_handlers[256] = {
575 [0x8a] = handle_io_inst,
576};
577
578int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu)
579{
580 intercept_handler_t handler;
581
582 /* All eb instructions that end up here are privileged. */
583 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
208dd756 584 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
f379aae5
CH
585 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
586 if (handler)
587 return handler(vcpu);
588 return -EOPNOTSUPP;
589}
590
bb25b9ba
CB
591static int handle_tprot(struct kvm_vcpu *vcpu)
592{
b1c571a5 593 u64 address1, address2;
bb25b9ba 594 struct vm_area_struct *vma;
1eddb85f 595 unsigned long user_address;
bb25b9ba
CB
596
597 vcpu->stat.instruction_tprot++;
598
f9f6bbc6
TH
599 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
600 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
601
b1c571a5
CH
602 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
603
bb25b9ba
CB
604 /* we only handle the Linux memory detection case:
605 * access key == 0
606 * guest DAT == off
607 * everything else goes to userspace. */
608 if (address2 & 0xf0)
609 return -EOPNOTSUPP;
610 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
611 return -EOPNOTSUPP;
612
bb25b9ba 613 down_read(&current->mm->mmap_sem);
59a1fa2d
HC
614 user_address = __gmap_translate(address1, vcpu->arch.gmap);
615 if (IS_ERR_VALUE(user_address))
616 goto out_inject;
1eddb85f 617 vma = find_vma(current->mm, user_address);
59a1fa2d
HC
618 if (!vma)
619 goto out_inject;
bb25b9ba
CB
620 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
621 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
622 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
623 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
624 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
625
626 up_read(&current->mm->mmap_sem);
627 return 0;
59a1fa2d
HC
628
629out_inject:
630 up_read(&current->mm->mmap_sem);
631 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
bb25b9ba
CB
632}
633
634int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
635{
636 /* For e5xx... instructions we only handle TPROT */
637 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
638 return handle_tprot(vcpu);
639 return -EOPNOTSUPP;
640}
641
8c3f61e2
CH
642static int handle_sckpf(struct kvm_vcpu *vcpu)
643{
644 u32 value;
645
646 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
208dd756 647 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
8c3f61e2
CH
648
649 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
650 return kvm_s390_inject_program_int(vcpu,
651 PGM_SPECIFICATION);
652
653 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
654 vcpu->arch.sie_block->todpr = value;
655
656 return 0;
657}
658
77975357 659static const intercept_handler_t x01_handlers[256] = {
8c3f61e2
CH
660 [0x07] = handle_sckpf,
661};
662
663int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
664{
665 intercept_handler_t handler;
666
667 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
668 if (handler)
669 return handler(vcpu);
670 return -EOPNOTSUPP;
671}