KVM: s390,perf: Detect if perf samples belong to KVM host or guest
[deliverable/linux.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)
48a3e950
CH
262 return kvm_s390_inject_program_int(vcpu,
263 PGM_PRIVILEGED_OPERATION);
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,
449 PGM_PRIVILEGED_OPERATION);
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
460 reg1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 24;
461 reg2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
462
463 /* This basically extracts the mask half of the psw. */
464 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000;
465 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
466 if (reg2) {
467 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000;
468 vcpu->run->s.regs.gprs[reg2] |=
469 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff;
470 }
471 return 0;
472}
473
69d0d3a3
CB
474#define PFMF_RESERVED 0xfffc0101UL
475#define PFMF_SK 0x00020000UL
476#define PFMF_CF 0x00010000UL
477#define PFMF_UI 0x00008000UL
478#define PFMF_FSC 0x00007000UL
479#define PFMF_NQ 0x00000800UL
480#define PFMF_MR 0x00000400UL
481#define PFMF_MC 0x00000200UL
482#define PFMF_KEY 0x000000feUL
483
484static int handle_pfmf(struct kvm_vcpu *vcpu)
485{
486 int reg1, reg2;
487 unsigned long start, end;
488
489 vcpu->stat.instruction_pfmf++;
490
491 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
492
493 if (!MACHINE_HAS_PFMF)
494 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
495
496 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
497 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OPERATION);
498
499 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
500 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
501
502 /* Only provide non-quiescing support if the host supports it */
503 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
504 S390_lowcore.stfl_fac_list & 0x00020000)
505 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
506
507 /* No support for conditional-SSKE */
508 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
509 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
510
511 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
512 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
513 case 0x00000000:
514 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
515 break;
516 case 0x00001000:
517 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
518 break;
519 /* We dont support EDAT2
520 case 0x00002000:
521 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
522 break;*/
523 default:
524 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
525 }
526 while (start < end) {
527 unsigned long useraddr;
528
529 useraddr = gmap_translate(start, vcpu->arch.gmap);
530 if (IS_ERR((void *)useraddr))
531 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
532
533 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
534 if (clear_user((void __user *)useraddr, PAGE_SIZE))
535 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
536 }
537
538 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
539 if (set_guest_storage_key(current->mm, useraddr,
540 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
541 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
542 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
543 }
544
545 start += PAGE_SIZE;
546 }
547 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
548 vcpu->run->s.regs.gprs[reg2] = end;
549 return 0;
550}
551
48a3e950
CH
552static const intercept_handler_t b9_handlers[256] = {
553 [0x8d] = handle_epsw,
f379aae5 554 [0x9c] = handle_io_inst,
69d0d3a3 555 [0xaf] = handle_pfmf,
48a3e950
CH
556};
557
558int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
559{
560 intercept_handler_t handler;
561
562 /* This is handled just as for the B2 instructions. */
563 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
564 if (handler) {
565 if ((handler != handle_epsw) &&
566 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE))
567 return kvm_s390_inject_program_int(vcpu,
568 PGM_PRIVILEGED_OPERATION);
569 else
570 return handler(vcpu);
571 }
572 return -EOPNOTSUPP;
573}
574
f379aae5
CH
575static const intercept_handler_t eb_handlers[256] = {
576 [0x8a] = handle_io_inst,
577};
578
579int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu)
580{
581 intercept_handler_t handler;
582
583 /* All eb instructions that end up here are privileged. */
584 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
585 return kvm_s390_inject_program_int(vcpu,
586 PGM_PRIVILEGED_OPERATION);
587 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
588 if (handler)
589 return handler(vcpu);
590 return -EOPNOTSUPP;
591}
592
bb25b9ba
CB
593static int handle_tprot(struct kvm_vcpu *vcpu)
594{
b1c571a5 595 u64 address1, address2;
bb25b9ba 596 struct vm_area_struct *vma;
1eddb85f 597 unsigned long user_address;
bb25b9ba
CB
598
599 vcpu->stat.instruction_tprot++;
600
b1c571a5
CH
601 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
602
bb25b9ba
CB
603 /* we only handle the Linux memory detection case:
604 * access key == 0
605 * guest DAT == off
606 * everything else goes to userspace. */
607 if (address2 & 0xf0)
608 return -EOPNOTSUPP;
609 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
610 return -EOPNOTSUPP;
611
bb25b9ba 612 down_read(&current->mm->mmap_sem);
59a1fa2d
HC
613 user_address = __gmap_translate(address1, vcpu->arch.gmap);
614 if (IS_ERR_VALUE(user_address))
615 goto out_inject;
1eddb85f 616 vma = find_vma(current->mm, user_address);
59a1fa2d
HC
617 if (!vma)
618 goto out_inject;
bb25b9ba
CB
619 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
620 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
621 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
622 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
623 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
624
625 up_read(&current->mm->mmap_sem);
626 return 0;
59a1fa2d
HC
627
628out_inject:
629 up_read(&current->mm->mmap_sem);
630 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
bb25b9ba
CB
631}
632
633int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
634{
635 /* For e5xx... instructions we only handle TPROT */
636 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
637 return handle_tprot(vcpu);
638 return -EOPNOTSUPP;
639}
640
8c3f61e2
CH
641static int handle_sckpf(struct kvm_vcpu *vcpu)
642{
643 u32 value;
644
645 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
646 return kvm_s390_inject_program_int(vcpu,
647 PGM_PRIVILEGED_OPERATION);
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}
This page took 0.477297 seconds and 5 git commands to generate.