blob: 9d32c56fb02ce7f9dab80923d9e5d02bcd43f47a [file] [log] [blame]
Christian Borntraeger453423d2008-03-25 18:47:29 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling privileged instructions
Christian Borntraeger453423d2008-03-25 18:47:29 +01003 *
4 * Copyright IBM Corp. 2008
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010016#include <linux/errno.h>
Heiko Carstens7c959e82013-03-05 13:14:46 +010017#include <asm/asm-offsets.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010018#include <asm/current.h>
19#include <asm/debug.h>
20#include <asm/ebcdic.h>
21#include <asm/sysinfo.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010022#include <asm/ptrace.h>
23#include <asm/compat.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010024#include "gaccess.h"
25#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020026#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010027
28static int handle_set_prefix(struct kvm_vcpu *vcpu)
29{
Christian Borntraeger453423d2008-03-25 18:47:29 +010030 u64 operand2;
31 u32 address = 0;
32 u8 tmp;
33
34 vcpu->stat.instruction_spx++;
35
Cornelia Huckb1c571a2012-12-20 15:32:07 +010036 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010037
38 /* must be word boundary */
39 if (operand2 & 3) {
40 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
41 goto out;
42 }
43
44 /* get the value */
Heiko Carstens0a75ca22013-03-05 13:14:47 +010045 if (get_guest(vcpu, address, (u32 __user *) operand2)) {
Christian Borntraeger453423d2008-03-25 18:47:29 +010046 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
47 goto out;
48 }
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) ||
54 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
55 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
56 goto out;
57 }
58
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010059 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010060
61 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020062 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010063out:
64 return 0;
65}
66
67static int handle_store_prefix(struct kvm_vcpu *vcpu)
68{
Christian Borntraeger453423d2008-03-25 18:47:29 +010069 u64 operand2;
70 u32 address;
71
72 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +010073
74 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010075
76 /* must be word boundary */
77 if (operand2 & 3) {
78 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
79 goto out;
80 }
81
82 address = vcpu->arch.sie_block->prefix;
83 address = address & 0x7fffe000u;
84
85 /* get the value */
Heiko Carstens0a75ca22013-03-05 13:14:47 +010086 if (put_guest(vcpu, address, (u32 __user *)operand2)) {
Christian Borntraeger453423d2008-03-25 18:47:29 +010087 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
88 goto out;
89 }
90
91 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020092 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010093out:
94 return 0;
95}
96
97static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
98{
Christian Borntraeger453423d2008-03-25 18:47:29 +010099 u64 useraddr;
100 int rc;
101
102 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100103
104 useraddr = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100105
106 if (useraddr & 1) {
107 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
108 goto out;
109 }
110
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100111 rc = put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100112 if (rc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100113 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
114 goto out;
115 }
116
Heiko Carstens33e19112009-01-09 12:14:56 +0100117 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200118 trace_kvm_s390_handle_stap(vcpu, useraddr);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100119out:
120 return 0;
121}
122
123static int handle_skey(struct kvm_vcpu *vcpu)
124{
125 vcpu->stat.instruction_storage_key++;
126 vcpu->arch.sie_block->gpsw.addr -= 4;
127 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
128 return 0;
129}
130
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100131static int handle_tpi(struct kvm_vcpu *vcpu)
132{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100133 struct kvm_s390_interrupt_info *inti;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100134 u64 addr;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100135 int cc;
136
137 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100138 if (addr & 3) {
139 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
140 goto out;
141 }
142 cc = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100143 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100144 if (!inti)
145 goto no_interrupt;
146 cc = 1;
147 if (addr) {
148 /*
149 * Store the two-word I/O interruption code into the
150 * provided area.
151 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100152 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) addr);
153 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) (addr + 2));
154 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) (addr + 4));
Heiko Carstens7c959e82013-03-05 13:14:46 +0100155 } else {
156 /*
157 * Store the three-word I/O interruption code into
158 * the appropriate lowcore area.
159 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100160 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
161 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
162 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
163 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100164 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100165 kfree(inti);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100166no_interrupt:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100167 /* Set condition code and we're done. */
168 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
169 vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100170out:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100171 return 0;
172}
173
174static int handle_tsch(struct kvm_vcpu *vcpu)
175{
176 struct kvm_s390_interrupt_info *inti;
177
178 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
179 vcpu->run->s.regs.gprs[1]);
180
181 /*
182 * Prepare exit to userspace.
183 * We indicate whether we dequeued a pending I/O interrupt
184 * so that userspace can re-inject it if the instruction gets
185 * a program check. While this may re-order the pending I/O
186 * interrupts, this is no problem since the priority is kept
187 * intact.
188 */
189 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
190 vcpu->run->s390_tsch.dequeued = !!inti;
191 if (inti) {
192 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
193 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
194 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
195 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
196 }
197 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
198 kfree(inti);
199 return -EREMOTE;
200}
201
Cornelia Huckf379aae2012-12-20 15:32:10 +0100202static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100203{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100204 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100205
206 if (vcpu->kvm->arch.css_support) {
207 /*
208 * Most I/O instructions will be handled by userspace.
209 * Exceptions are tpi and the interrupt portion of tsch.
210 */
211 if (vcpu->arch.sie_block->ipa == 0xb236)
212 return handle_tpi(vcpu);
213 if (vcpu->arch.sie_block->ipa == 0xb235)
214 return handle_tsch(vcpu);
215 /* Handle in userspace. */
216 return -EOPNOTSUPP;
217 } else {
218 /*
219 * Set condition code 3 to stop the guest from issueing channel
220 * I/O instructions.
221 */
222 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
223 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
224 return 0;
225 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100226}
227
Christian Borntraeger453423d2008-03-25 18:47:29 +0100228static int handle_stfl(struct kvm_vcpu *vcpu)
229{
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200230 unsigned int facility_list;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100231 int rc;
232
233 vcpu->stat.instruction_stfl++;
Christian Borntraegera0046b62008-08-29 13:29:45 +0200234 /* only pass the facility bits, which we can handle */
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200235 facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100236
237 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
238 &facility_list, sizeof(facility_list));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100239 if (rc)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100240 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200241 else {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100242 VCPU_EVENT(vcpu, 5, "store facility list value %x",
243 facility_list);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200244 trace_kvm_s390_handle_stfl(vcpu, facility_list);
245 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100246 return 0;
247}
248
Cornelia Huck48a3e952012-12-20 15:32:09 +0100249static void handle_new_psw(struct kvm_vcpu *vcpu)
250{
251 /* Check whether the new psw is enabled for machine checks. */
252 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
253 kvm_s390_deliver_pending_machine_checks(vcpu);
254}
255
256#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
257#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100258#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100259#define PSW_ADDR_31 0x000000007fffffffUL
260
261int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
262{
263 u64 addr;
264 psw_compat_t new_psw;
265
266 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
267 return kvm_s390_inject_program_int(vcpu,
268 PGM_PRIVILEGED_OPERATION);
269
270 addr = kvm_s390_get_base_disp_s(vcpu);
271
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100272 if (addr & 7)
273 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100274
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100275 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
276 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100277
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100278 if (!(new_psw.mask & PSW32_MASK_BASE))
279 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100280
281 vcpu->arch.sie_block->gpsw.mask =
282 (new_psw.mask & ~PSW32_MASK_BASE) << 32;
Heiko Carstensace50582013-03-25 17:22:50 +0100283 vcpu->arch.sie_block->gpsw.mask |= new_psw.addr & PSW32_ADDR_AMODE;
284 vcpu->arch.sie_block->gpsw.addr = new_psw.addr & ~PSW32_ADDR_AMODE;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100285
286 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_UNASSIGNED) ||
287 (!(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) &&
288 (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_24)) ||
289 ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) ==
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100290 PSW_MASK_EA))
291 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100292
293 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100294 return 0;
295}
296
297static int handle_lpswe(struct kvm_vcpu *vcpu)
298{
299 u64 addr;
300 psw_t new_psw;
301
302 addr = kvm_s390_get_base_disp_s(vcpu);
303
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100304 if (addr & 7)
305 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100306
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100307 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
308 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100309
310 vcpu->arch.sie_block->gpsw.mask = new_psw.mask;
311 vcpu->arch.sie_block->gpsw.addr = new_psw.addr;
312
313 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_UNASSIGNED) ||
314 (((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) ==
315 PSW_MASK_BA) &&
316 (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_31)) ||
317 (!(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) &&
318 (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_24)) ||
319 ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) ==
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100320 PSW_MASK_EA))
321 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100322
323 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100324 return 0;
325}
326
Christian Borntraeger453423d2008-03-25 18:47:29 +0100327static int handle_stidp(struct kvm_vcpu *vcpu)
328{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100329 u64 operand2;
330 int rc;
331
332 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100333
334 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100335
336 if (operand2 & 7) {
337 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
338 goto out;
339 }
340
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100341 rc = put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100342 if (rc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100343 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
344 goto out;
345 }
346
347 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
348out:
349 return 0;
350}
351
352static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
353{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200354 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100355 int cpus = 0;
356 int n;
357
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200358 spin_lock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100359 for (n = 0; n < KVM_MAX_VCPUS; n++)
360 if (fi->local_int[n])
361 cpus++;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200362 spin_unlock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100363
364 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200365 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100366 mem->count = 0;
367 if (mem->count < 8)
368 mem->count++;
369 for (n = mem->count - 1; n > 0 ; n--)
370 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
371
372 mem->vm[0].cpus_total = cpus;
373 mem->vm[0].cpus_configured = cpus;
374 mem->vm[0].cpus_standby = 0;
375 mem->vm[0].cpus_reserved = 0;
376 mem->vm[0].caf = 1000;
377 memcpy(mem->vm[0].name, "KVMguest", 8);
378 ASCEBC(mem->vm[0].name, 8);
379 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
380 ASCEBC(mem->vm[0].cpi, 16);
381}
382
383static int handle_stsi(struct kvm_vcpu *vcpu)
384{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100385 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
386 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
387 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100388 u64 operand2;
389 unsigned long mem;
390
391 vcpu->stat.instruction_stsi++;
392 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
393
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100394 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100395
396 if (operand2 & 0xfff && fc > 0)
397 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
398
399 switch (fc) {
400 case 0:
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100401 vcpu->run->s.regs.gprs[0] = 3 << 28;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100402 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
403 return 0;
404 case 1: /* same handling for 1 and 2 */
405 case 2:
406 mem = get_zeroed_page(GFP_KERNEL);
407 if (!mem)
408 goto out_fail;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200409 if (stsi((void *) mem, fc, sel1, sel2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100410 goto out_mem;
411 break;
412 case 3:
413 if (sel1 != 2 || sel2 != 2)
414 goto out_fail;
415 mem = get_zeroed_page(GFP_KERNEL);
416 if (!mem)
417 goto out_fail;
418 handle_stsi_3_2_2(vcpu, (void *) mem);
419 break;
420 default:
421 goto out_fail;
422 }
423
424 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
425 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
426 goto out_mem;
427 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200428 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100429 free_page(mem);
430 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100431 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100432 return 0;
433out_mem:
434 free_page(mem);
435out_fail:
436 /* condition code 3 */
437 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
438 return 0;
439}
440
Cornelia Huckf379aae2012-12-20 15:32:10 +0100441static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100442 [0x02] = handle_stidp,
443 [0x10] = handle_set_prefix,
444 [0x11] = handle_store_prefix,
445 [0x12] = handle_store_cpu_address,
446 [0x29] = handle_skey,
447 [0x2a] = handle_skey,
448 [0x2b] = handle_skey,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100449 [0x30] = handle_io_inst,
450 [0x31] = handle_io_inst,
451 [0x32] = handle_io_inst,
452 [0x33] = handle_io_inst,
453 [0x34] = handle_io_inst,
454 [0x35] = handle_io_inst,
455 [0x36] = handle_io_inst,
456 [0x37] = handle_io_inst,
457 [0x38] = handle_io_inst,
458 [0x39] = handle_io_inst,
459 [0x3a] = handle_io_inst,
460 [0x3b] = handle_io_inst,
461 [0x3c] = handle_io_inst,
462 [0x5f] = handle_io_inst,
463 [0x74] = handle_io_inst,
464 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100465 [0x7d] = handle_stsi,
466 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100467 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100468};
469
Christian Borntraeger70455a32009-01-22 10:28:29 +0100470int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100471{
472 intercept_handler_t handler;
473
Christian Borntraeger70455a32009-01-22 10:28:29 +0100474 /*
475 * a lot of B2 instructions are priviledged. We first check for
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300476 * the privileged ones, that we can handle in the kernel. If the
Christian Borntraeger70455a32009-01-22 10:28:29 +0100477 * kernel can handle this instruction, we check for the problem
478 * state bit and (a) handle the instruction or (b) send a code 2
479 * program check.
480 * Anything else goes to userspace.*/
Cornelia Huckf379aae2012-12-20 15:32:10 +0100481 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Christian Borntraeger70455a32009-01-22 10:28:29 +0100482 if (handler) {
483 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
484 return kvm_s390_inject_program_int(vcpu,
485 PGM_PRIVILEGED_OPERATION);
486 else
487 return handler(vcpu);
488 }
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100489 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100490}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200491
Cornelia Huck48a3e952012-12-20 15:32:09 +0100492static int handle_epsw(struct kvm_vcpu *vcpu)
493{
494 int reg1, reg2;
495
496 reg1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 24;
497 reg2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
498
499 /* This basically extracts the mask half of the psw. */
500 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000;
501 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
502 if (reg2) {
503 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000;
504 vcpu->run->s.regs.gprs[reg2] |=
505 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff;
506 }
507 return 0;
508}
509
510static const intercept_handler_t b9_handlers[256] = {
511 [0x8d] = handle_epsw,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100512 [0x9c] = handle_io_inst,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100513};
514
515int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
516{
517 intercept_handler_t handler;
518
519 /* This is handled just as for the B2 instructions. */
520 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
521 if (handler) {
522 if ((handler != handle_epsw) &&
523 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE))
524 return kvm_s390_inject_program_int(vcpu,
525 PGM_PRIVILEGED_OPERATION);
526 else
527 return handler(vcpu);
528 }
529 return -EOPNOTSUPP;
530}
531
Cornelia Huckf379aae2012-12-20 15:32:10 +0100532static const intercept_handler_t eb_handlers[256] = {
533 [0x8a] = handle_io_inst,
534};
535
536int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu)
537{
538 intercept_handler_t handler;
539
540 /* All eb instructions that end up here are privileged. */
541 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
542 return kvm_s390_inject_program_int(vcpu,
543 PGM_PRIVILEGED_OPERATION);
544 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
545 if (handler)
546 return handler(vcpu);
547 return -EOPNOTSUPP;
548}
549
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200550static int handle_tprot(struct kvm_vcpu *vcpu)
551{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100552 u64 address1, address2;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200553 struct vm_area_struct *vma;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100554 unsigned long user_address;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200555
556 vcpu->stat.instruction_tprot++;
557
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100558 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
559
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200560 /* we only handle the Linux memory detection case:
561 * access key == 0
562 * guest DAT == off
563 * everything else goes to userspace. */
564 if (address2 & 0xf0)
565 return -EOPNOTSUPP;
566 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
567 return -EOPNOTSUPP;
568
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200569 down_read(&current->mm->mmap_sem);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100570 user_address = __gmap_translate(address1, vcpu->arch.gmap);
571 if (IS_ERR_VALUE(user_address))
572 goto out_inject;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100573 vma = find_vma(current->mm, user_address);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100574 if (!vma)
575 goto out_inject;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200576 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
577 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
578 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
579 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
580 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
581
582 up_read(&current->mm->mmap_sem);
583 return 0;
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100584
585out_inject:
586 up_read(&current->mm->mmap_sem);
587 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200588}
589
590int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
591{
592 /* For e5xx... instructions we only handle TPROT */
593 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
594 return handle_tprot(vcpu);
595 return -EOPNOTSUPP;
596}
597
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200598static int handle_sckpf(struct kvm_vcpu *vcpu)
599{
600 u32 value;
601
602 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
603 return kvm_s390_inject_program_int(vcpu,
604 PGM_PRIVILEGED_OPERATION);
605
606 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
607 return kvm_s390_inject_program_int(vcpu,
608 PGM_SPECIFICATION);
609
610 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
611 vcpu->arch.sie_block->todpr = value;
612
613 return 0;
614}
615
Cornelia Huck77975352012-12-20 15:32:06 +0100616static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200617 [0x07] = handle_sckpf,
618};
619
620int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
621{
622 intercept_handler_t handler;
623
624 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
625 if (handler)
626 return handler(vcpu);
627 return -EOPNOTSUPP;
628}