blob: 6bbd7b5a0bbee36d483e7deba484ce96e3176dfb [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 Carstensb13b5dc2013-03-25 17:22:55 +010017#include <linux/compat.h>
Heiko Carstens7c959e82013-03-05 13:14:46 +010018#include <asm/asm-offsets.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010019#include <asm/current.h>
20#include <asm/debug.h>
21#include <asm/ebcdic.h>
22#include <asm/sysinfo.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010023#include <asm/ptrace.h>
24#include <asm/compat.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010025#include "gaccess.h"
26#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020027#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010028
29static int handle_set_prefix(struct kvm_vcpu *vcpu)
30{
Christian Borntraeger453423d2008-03-25 18:47:29 +010031 u64 operand2;
32 u32 address = 0;
33 u8 tmp;
34
35 vcpu->stat.instruction_spx++;
36
Cornelia Huckb1c571a2012-12-20 15:32:07 +010037 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010038
39 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010040 if (operand2 & 3)
41 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010042
43 /* get the value */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010044 if (get_guest(vcpu, address, (u32 __user *) operand2))
45 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010046
47 address = address & 0x7fffe000u;
48
49 /* make sure that the new value is valid memory */
50 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010051 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)))
52 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010053
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010054 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010055
56 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020057 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010058 return 0;
59}
60
61static int handle_store_prefix(struct kvm_vcpu *vcpu)
62{
Christian Borntraeger453423d2008-03-25 18:47:29 +010063 u64 operand2;
64 u32 address;
65
66 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +010067
68 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010069
70 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010071 if (operand2 & 3)
72 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010073
74 address = vcpu->arch.sie_block->prefix;
75 address = address & 0x7fffe000u;
76
77 /* get the value */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010078 if (put_guest(vcpu, address, (u32 __user *)operand2))
79 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010080
81 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020082 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010083 return 0;
84}
85
86static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
87{
Christian Borntraeger453423d2008-03-25 18:47:29 +010088 u64 useraddr;
Christian Borntraeger453423d2008-03-25 18:47:29 +010089
90 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +010091
92 useraddr = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010093
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010094 if (useraddr & 1)
95 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010096
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010097 if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
98 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010099
Heiko Carstens33e19112009-01-09 12:14:56 +0100100 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200101 trace_kvm_s390_handle_stap(vcpu, useraddr);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100102 return 0;
103}
104
105static int handle_skey(struct kvm_vcpu *vcpu)
106{
107 vcpu->stat.instruction_storage_key++;
108 vcpu->arch.sie_block->gpsw.addr -= 4;
109 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
110 return 0;
111}
112
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100113static int handle_tpi(struct kvm_vcpu *vcpu)
114{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100115 struct kvm_s390_interrupt_info *inti;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100116 u64 addr;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100117 int cc;
118
119 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100120 if (addr & 3)
121 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100122 cc = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100123 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100124 if (!inti)
125 goto no_interrupt;
126 cc = 1;
127 if (addr) {
128 /*
129 * Store the two-word I/O interruption code into the
130 * provided area.
131 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100132 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) addr);
133 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) (addr + 2));
134 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) (addr + 4));
Heiko Carstens7c959e82013-03-05 13:14:46 +0100135 } else {
136 /*
137 * Store the three-word I/O interruption code into
138 * the appropriate lowcore area.
139 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100140 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
141 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
142 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
143 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100144 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100145 kfree(inti);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100146no_interrupt:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100147 /* Set condition code and we're done. */
148 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
149 vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44;
150 return 0;
151}
152
153static int handle_tsch(struct kvm_vcpu *vcpu)
154{
155 struct kvm_s390_interrupt_info *inti;
156
157 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
158 vcpu->run->s.regs.gprs[1]);
159
160 /*
161 * Prepare exit to userspace.
162 * We indicate whether we dequeued a pending I/O interrupt
163 * so that userspace can re-inject it if the instruction gets
164 * a program check. While this may re-order the pending I/O
165 * interrupts, this is no problem since the priority is kept
166 * intact.
167 */
168 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
169 vcpu->run->s390_tsch.dequeued = !!inti;
170 if (inti) {
171 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
172 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
173 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
174 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
175 }
176 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
177 kfree(inti);
178 return -EREMOTE;
179}
180
Cornelia Huckf379aae2012-12-20 15:32:10 +0100181static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100182{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100183 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100184
185 if (vcpu->kvm->arch.css_support) {
186 /*
187 * Most I/O instructions will be handled by userspace.
188 * Exceptions are tpi and the interrupt portion of tsch.
189 */
190 if (vcpu->arch.sie_block->ipa == 0xb236)
191 return handle_tpi(vcpu);
192 if (vcpu->arch.sie_block->ipa == 0xb235)
193 return handle_tsch(vcpu);
194 /* Handle in userspace. */
195 return -EOPNOTSUPP;
196 } else {
197 /*
198 * Set condition code 3 to stop the guest from issueing channel
199 * I/O instructions.
200 */
201 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
202 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
203 return 0;
204 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100205}
206
Christian Borntraeger453423d2008-03-25 18:47:29 +0100207static int handle_stfl(struct kvm_vcpu *vcpu)
208{
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200209 unsigned int facility_list;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100210 int rc;
211
212 vcpu->stat.instruction_stfl++;
Christian Borntraegera0046b62008-08-29 13:29:45 +0200213 /* only pass the facility bits, which we can handle */
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200214 facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100215
216 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
217 &facility_list, sizeof(facility_list));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100218 if (rc)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100219 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
220 VCPU_EVENT(vcpu, 5, "store facility list value %x", facility_list);
221 trace_kvm_s390_handle_stfl(vcpu, facility_list);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100222 return 0;
223}
224
Cornelia Huck48a3e952012-12-20 15:32:09 +0100225static void handle_new_psw(struct kvm_vcpu *vcpu)
226{
227 /* Check whether the new psw is enabled for machine checks. */
228 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
229 kvm_s390_deliver_pending_machine_checks(vcpu);
230}
231
232#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
233#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100234#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100235#define PSW_ADDR_31 0x000000007fffffffUL
236
Heiko Carstens3736b872013-03-25 17:22:52 +0100237static int is_valid_psw(psw_t *psw) {
238 if (psw->mask & PSW_MASK_UNASSIGNED)
239 return 0;
240 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
241 if (psw->addr & ~PSW_ADDR_31)
242 return 0;
243 }
244 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
245 return 0;
246 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
247 return 0;
248 return 1;
249}
250
Cornelia Huck48a3e952012-12-20 15:32:09 +0100251int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
252{
Heiko Carstens3736b872013-03-25 17:22:52 +0100253 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100254 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100255 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100256
Heiko Carstens3736b872013-03-25 17:22:52 +0100257 if (gpsw->mask & PSW_MASK_PSTATE)
Cornelia Huck48a3e952012-12-20 15:32:09 +0100258 return kvm_s390_inject_program_int(vcpu,
259 PGM_PRIVILEGED_OPERATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100260 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100261 if (addr & 7)
262 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100263 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
264 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100265 if (!(new_psw.mask & PSW32_MASK_BASE))
266 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100267 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
268 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
269 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
270 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100271 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100272 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100273 return 0;
274}
275
276static int handle_lpswe(struct kvm_vcpu *vcpu)
277{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100278 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100279 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100280
281 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100282 if (addr & 7)
283 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100284 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
285 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens3736b872013-03-25 17:22:52 +0100286 vcpu->arch.sie_block->gpsw = new_psw;
287 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100288 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100289 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100290 return 0;
291}
292
Christian Borntraeger453423d2008-03-25 18:47:29 +0100293static int handle_stidp(struct kvm_vcpu *vcpu)
294{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100295 u64 operand2;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100296
297 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100298
299 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100300
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100301 if (operand2 & 7)
302 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100303
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100304 if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
305 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100306
307 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
Christian Borntraeger453423d2008-03-25 18:47:29 +0100308 return 0;
309}
310
311static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
312{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200313 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100314 int cpus = 0;
315 int n;
316
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200317 spin_lock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100318 for (n = 0; n < KVM_MAX_VCPUS; n++)
319 if (fi->local_int[n])
320 cpus++;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200321 spin_unlock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100322
323 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200324 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100325 mem->count = 0;
326 if (mem->count < 8)
327 mem->count++;
328 for (n = mem->count - 1; n > 0 ; n--)
329 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
330
331 mem->vm[0].cpus_total = cpus;
332 mem->vm[0].cpus_configured = cpus;
333 mem->vm[0].cpus_standby = 0;
334 mem->vm[0].cpus_reserved = 0;
335 mem->vm[0].caf = 1000;
336 memcpy(mem->vm[0].name, "KVMguest", 8);
337 ASCEBC(mem->vm[0].name, 8);
338 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
339 ASCEBC(mem->vm[0].cpi, 16);
340}
341
342static int handle_stsi(struct kvm_vcpu *vcpu)
343{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100344 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
345 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
346 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100347 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100348 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100349 int rc = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100350
351 vcpu->stat.instruction_stsi++;
352 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
353
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100354 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100355
356 if (operand2 & 0xfff && fc > 0)
357 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
358
359 switch (fc) {
360 case 0:
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100361 vcpu->run->s.regs.gprs[0] = 3 << 28;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100362 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
363 return 0;
364 case 1: /* same handling for 1 and 2 */
365 case 2:
366 mem = get_zeroed_page(GFP_KERNEL);
367 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100368 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200369 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100370 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100371 break;
372 case 3:
373 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100374 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100375 mem = get_zeroed_page(GFP_KERNEL);
376 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100377 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100378 handle_stsi_3_2_2(vcpu, (void *) mem);
379 break;
380 default:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100381 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100382 }
383
384 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100385 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstensc51f0682013-03-25 17:22:54 +0100386 goto out_exception;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100387 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200388 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100389 free_page(mem);
390 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100391 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100392 return 0;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100393out_no_data:
Christian Borntraeger453423d2008-03-25 18:47:29 +0100394 /* condition code 3 */
395 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100396out_exception:
397 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100398 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100399}
400
Cornelia Huckf379aae2012-12-20 15:32:10 +0100401static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100402 [0x02] = handle_stidp,
403 [0x10] = handle_set_prefix,
404 [0x11] = handle_store_prefix,
405 [0x12] = handle_store_cpu_address,
406 [0x29] = handle_skey,
407 [0x2a] = handle_skey,
408 [0x2b] = handle_skey,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100409 [0x30] = handle_io_inst,
410 [0x31] = handle_io_inst,
411 [0x32] = handle_io_inst,
412 [0x33] = handle_io_inst,
413 [0x34] = handle_io_inst,
414 [0x35] = handle_io_inst,
415 [0x36] = handle_io_inst,
416 [0x37] = handle_io_inst,
417 [0x38] = handle_io_inst,
418 [0x39] = handle_io_inst,
419 [0x3a] = handle_io_inst,
420 [0x3b] = handle_io_inst,
421 [0x3c] = handle_io_inst,
422 [0x5f] = handle_io_inst,
423 [0x74] = handle_io_inst,
424 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100425 [0x7d] = handle_stsi,
426 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100427 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100428};
429
Christian Borntraeger70455a32009-01-22 10:28:29 +0100430int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100431{
432 intercept_handler_t handler;
433
Christian Borntraeger70455a32009-01-22 10:28:29 +0100434 /*
435 * a lot of B2 instructions are priviledged. We first check for
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300436 * the privileged ones, that we can handle in the kernel. If the
Christian Borntraeger70455a32009-01-22 10:28:29 +0100437 * kernel can handle this instruction, we check for the problem
438 * state bit and (a) handle the instruction or (b) send a code 2
439 * program check.
440 * Anything else goes to userspace.*/
Cornelia Huckf379aae2012-12-20 15:32:10 +0100441 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Christian Borntraeger70455a32009-01-22 10:28:29 +0100442 if (handler) {
443 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
444 return kvm_s390_inject_program_int(vcpu,
445 PGM_PRIVILEGED_OPERATION);
446 else
447 return handler(vcpu);
448 }
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100449 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100450}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200451
Cornelia Huck48a3e952012-12-20 15:32:09 +0100452static int handle_epsw(struct kvm_vcpu *vcpu)
453{
454 int reg1, reg2;
455
456 reg1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 24;
457 reg2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
458
459 /* This basically extracts the mask half of the psw. */
460 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000;
461 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
462 if (reg2) {
463 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000;
464 vcpu->run->s.regs.gprs[reg2] |=
465 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff;
466 }
467 return 0;
468}
469
470static const intercept_handler_t b9_handlers[256] = {
471 [0x8d] = handle_epsw,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100472 [0x9c] = handle_io_inst,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100473};
474
475int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
476{
477 intercept_handler_t handler;
478
479 /* This is handled just as for the B2 instructions. */
480 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
481 if (handler) {
482 if ((handler != handle_epsw) &&
483 (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 }
489 return -EOPNOTSUPP;
490}
491
Cornelia Huckf379aae2012-12-20 15:32:10 +0100492static const intercept_handler_t eb_handlers[256] = {
493 [0x8a] = handle_io_inst,
494};
495
496int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu)
497{
498 intercept_handler_t handler;
499
500 /* All eb instructions that end up here are privileged. */
501 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
502 return kvm_s390_inject_program_int(vcpu,
503 PGM_PRIVILEGED_OPERATION);
504 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
505 if (handler)
506 return handler(vcpu);
507 return -EOPNOTSUPP;
508}
509
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200510static int handle_tprot(struct kvm_vcpu *vcpu)
511{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100512 u64 address1, address2;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200513 struct vm_area_struct *vma;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100514 unsigned long user_address;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200515
516 vcpu->stat.instruction_tprot++;
517
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100518 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
519
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200520 /* we only handle the Linux memory detection case:
521 * access key == 0
522 * guest DAT == off
523 * everything else goes to userspace. */
524 if (address2 & 0xf0)
525 return -EOPNOTSUPP;
526 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
527 return -EOPNOTSUPP;
528
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200529 down_read(&current->mm->mmap_sem);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100530 user_address = __gmap_translate(address1, vcpu->arch.gmap);
531 if (IS_ERR_VALUE(user_address))
532 goto out_inject;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100533 vma = find_vma(current->mm, user_address);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100534 if (!vma)
535 goto out_inject;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200536 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
537 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
538 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
539 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
540 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
541
542 up_read(&current->mm->mmap_sem);
543 return 0;
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100544
545out_inject:
546 up_read(&current->mm->mmap_sem);
547 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200548}
549
550int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
551{
552 /* For e5xx... instructions we only handle TPROT */
553 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
554 return handle_tprot(vcpu);
555 return -EOPNOTSUPP;
556}
557
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200558static int handle_sckpf(struct kvm_vcpu *vcpu)
559{
560 u32 value;
561
562 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
563 return kvm_s390_inject_program_int(vcpu,
564 PGM_PRIVILEGED_OPERATION);
565
566 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
567 return kvm_s390_inject_program_int(vcpu,
568 PGM_SPECIFICATION);
569
570 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
571 vcpu->arch.sie_block->todpr = value;
572
573 return 0;
574}
575
Cornelia Huck77975352012-12-20 15:32:06 +0100576static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200577 [0x07] = handle_sckpf,
578};
579
580int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
581{
582 intercept_handler_t handler;
583
584 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
585 if (handler)
586 return handler(vcpu);
587 return -EOPNOTSUPP;
588}