blob: 04dc4a1439647c237b54ac4f991753646d204fb6 [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 *
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02004 * Copyright IBM Corp. 2008, 2013
Christian Borntraeger453423d2008-03-25 18:47:29 +01005 *
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>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020023#include <asm/pgtable.h>
24#include <asm/pgalloc.h>
25#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010026#include <asm/ptrace.h>
27#include <asm/compat.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010028#include "gaccess.h"
29#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020030#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010031
32static int handle_set_prefix(struct kvm_vcpu *vcpu)
33{
Christian Borntraeger453423d2008-03-25 18:47:29 +010034 u64 operand2;
35 u32 address = 0;
36 u8 tmp;
37
38 vcpu->stat.instruction_spx++;
39
Cornelia Huckb1c571a2012-12-20 15:32:07 +010040 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010041
42 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010043 if (operand2 & 3)
44 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010045
46 /* get the value */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010047 if (get_guest(vcpu, address, (u32 __user *) operand2))
48 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010049
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) ||
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010054 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)))
55 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010056
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010057 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010058
59 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020060 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010061 return 0;
62}
63
64static int handle_store_prefix(struct kvm_vcpu *vcpu)
65{
Christian Borntraeger453423d2008-03-25 18:47:29 +010066 u64 operand2;
67 u32 address;
68
69 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +010070
71 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010072
73 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010074 if (operand2 & 3)
75 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010076
77 address = vcpu->arch.sie_block->prefix;
78 address = address & 0x7fffe000u;
79
80 /* get the value */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010081 if (put_guest(vcpu, address, (u32 __user *)operand2))
82 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010083
84 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020085 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010086 return 0;
87}
88
89static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
90{
Christian Borntraeger453423d2008-03-25 18:47:29 +010091 u64 useraddr;
Christian Borntraeger453423d2008-03-25 18:47:29 +010092
93 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +010094
95 useraddr = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010096
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010097 if (useraddr & 1)
98 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010099
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100100 if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
101 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100102
Heiko Carstens33e19112009-01-09 12:14:56 +0100103 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200104 trace_kvm_s390_handle_stap(vcpu, useraddr);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100105 return 0;
106}
107
108static int handle_skey(struct kvm_vcpu *vcpu)
109{
110 vcpu->stat.instruction_storage_key++;
Martin Schwidefskydfcf7dc2013-05-17 14:41:32 +0200111 vcpu->arch.sie_block->gpsw.addr =
112 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100113 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
114 return 0;
115}
116
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100117static int handle_tpi(struct kvm_vcpu *vcpu)
118{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100119 struct kvm_s390_interrupt_info *inti;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100120 u64 addr;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100121 int cc;
122
123 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100124 if (addr & 3)
125 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100126 cc = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100127 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100128 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 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100136 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));
Heiko Carstens7c959e82013-03-05 13:14:46 +0100139 } else {
140 /*
141 * Store the three-word I/O interruption code into
142 * the appropriate lowcore area.
143 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100144 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);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100148 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100149 kfree(inti);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100150no_interrupt:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100151 /* Set condition code and we're done. */
152 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
153 vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44;
154 return 0;
155}
156
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
Cornelia Huckf379aae2012-12-20 15:32:10 +0100185static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100186{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100187 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100188
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 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100209}
210
Christian Borntraeger453423d2008-03-25 18:47:29 +0100211static int handle_stfl(struct kvm_vcpu *vcpu)
212{
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200213 unsigned int facility_list;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100214 int rc;
215
216 vcpu->stat.instruction_stfl++;
Christian Borntraegera0046b62008-08-29 13:29:45 +0200217 /* only pass the facility bits, which we can handle */
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200218 facility_list = S390_lowcore.stfl_fac_list & 0xff82fff3;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100219
220 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
221 &facility_list, sizeof(facility_list));
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100222 if (rc)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100223 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);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100226 return 0;
227}
228
Cornelia Huck48a3e952012-12-20 15:32:09 +0100229static 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
Heiko Carstensd21683e2013-03-25 17:22:49 +0100238#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100239#define PSW_ADDR_31 0x000000007fffffffUL
240
Heiko Carstens3736b872013-03-25 17:22:52 +0100241static 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
Cornelia Huck48a3e952012-12-20 15:32:09 +0100255int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
256{
Heiko Carstens3736b872013-03-25 17:22:52 +0100257 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100258 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100259 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100260
Heiko Carstens3736b872013-03-25 17:22:52 +0100261 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200262 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
263
Cornelia Huck48a3e952012-12-20 15:32:09 +0100264 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100265 if (addr & 7)
266 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100267 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
268 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100269 if (!(new_psw.mask & PSW32_MASK_BASE))
270 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100271 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))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100275 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100276 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100277 return 0;
278}
279
280static int handle_lpswe(struct kvm_vcpu *vcpu)
281{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100282 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100283 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100284
285 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100286 if (addr & 7)
287 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100288 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
289 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens3736b872013-03-25 17:22:52 +0100290 vcpu->arch.sie_block->gpsw = new_psw;
291 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100292 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100293 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100294 return 0;
295}
296
Christian Borntraeger453423d2008-03-25 18:47:29 +0100297static int handle_stidp(struct kvm_vcpu *vcpu)
298{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100299 u64 operand2;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100300
301 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100302
303 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100304
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100305 if (operand2 & 7)
306 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100307
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100308 if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
309 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100310
311 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
Christian Borntraeger453423d2008-03-25 18:47:29 +0100312 return 0;
313}
314
315static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
316{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200317 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100318 int cpus = 0;
319 int n;
320
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200321 spin_lock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100322 for (n = 0; n < KVM_MAX_VCPUS; n++)
323 if (fi->local_int[n])
324 cpus++;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200325 spin_unlock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100326
327 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200328 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100329 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{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100348 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;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100351 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100352 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100353 int rc = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100354
355 vcpu->stat.instruction_stsi++;
356 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
357
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100358 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100359
360 if (operand2 & 0xfff && fc > 0)
361 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
362
363 switch (fc) {
364 case 0:
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100365 vcpu->run->s.regs.gprs[0] = 3 << 28;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100366 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)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100372 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200373 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100374 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100375 break;
376 case 3:
377 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100378 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100379 mem = get_zeroed_page(GFP_KERNEL);
380 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100381 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100382 handle_stsi_3_2_2(vcpu, (void *) mem);
383 break;
384 default:
Heiko Carstensc51f0682013-03-25 17:22:54 +0100385 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100386 }
387
388 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100389 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstensc51f0682013-03-25 17:22:54 +0100390 goto out_exception;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100391 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200392 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100393 free_page(mem);
394 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100395 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100396 return 0;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100397out_no_data:
Christian Borntraeger453423d2008-03-25 18:47:29 +0100398 /* condition code 3 */
399 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100400out_exception:
401 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100402 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100403}
404
Cornelia Huckf379aae2012-12-20 15:32:10 +0100405static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100406 [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,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100413 [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,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100429 [0x7d] = handle_stsi,
430 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100431 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100432};
433
Christian Borntraeger70455a32009-01-22 10:28:29 +0100434int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100435{
436 intercept_handler_t handler;
437
Christian Borntraeger70455a32009-01-22 10:28:29 +0100438 /*
439 * a lot of B2 instructions are priviledged. We first check for
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300440 * the privileged ones, that we can handle in the kernel. If the
Christian Borntraeger70455a32009-01-22 10:28:29 +0100441 * 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.*/
Cornelia Huckf379aae2012-12-20 15:32:10 +0100445 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Christian Borntraeger70455a32009-01-22 10:28:29 +0100446 if (handler) {
447 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
448 return kvm_s390_inject_program_int(vcpu,
Thomas Huth208dd752013-06-20 17:21:59 +0200449 PGM_PRIVILEGED_OP);
Christian Borntraeger70455a32009-01-22 10:28:29 +0100450 else
451 return handler(vcpu);
452 }
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100453 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100454}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200455
Cornelia Huck48a3e952012-12-20 15:32:09 +0100456static int handle_epsw(struct kvm_vcpu *vcpu)
457{
458 int reg1, reg2;
459
Thomas Huthaeb87c32013-06-12 13:54:57 +0200460 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100461
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
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200473#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)
Thomas Huth208dd752013-06-20 17:21:59 +0200496 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200497
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
Cornelia Huck48a3e952012-12-20 15:32:09 +0100551static const intercept_handler_t b9_handlers[256] = {
552 [0x8d] = handle_epsw,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100553 [0x9c] = handle_io_inst,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200554 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100555};
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,
Thomas Huth208dd752013-06-20 17:21:59 +0200567 PGM_PRIVILEGED_OP);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100568 else
569 return handler(vcpu);
570 }
571 return -EOPNOTSUPP;
572}
573
Cornelia Huckf379aae2012-12-20 15:32:10 +0100574static 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)
Thomas Huth208dd752013-06-20 17:21:59 +0200584 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huckf379aae2012-12-20 15:32:10 +0100585 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
586 if (handler)
587 return handler(vcpu);
588 return -EOPNOTSUPP;
589}
590
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200591static int handle_tprot(struct kvm_vcpu *vcpu)
592{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100593 u64 address1, address2;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200594 struct vm_area_struct *vma;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100595 unsigned long user_address;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200596
597 vcpu->stat.instruction_tprot++;
598
Thomas Huthf9f6bbc2013-06-20 17:22:00 +0200599 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
600 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
601
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100602 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
603
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200604 /* 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
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200613 down_read(&current->mm->mmap_sem);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100614 user_address = __gmap_translate(address1, vcpu->arch.gmap);
615 if (IS_ERR_VALUE(user_address))
616 goto out_inject;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100617 vma = find_vma(current->mm, user_address);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100618 if (!vma)
619 goto out_inject;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200620 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;
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100628
629out_inject:
630 up_read(&current->mm->mmap_sem);
631 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200632}
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
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200642static int handle_sckpf(struct kvm_vcpu *vcpu)
643{
644 u32 value;
645
646 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200647 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200648
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
Cornelia Huck77975352012-12-20 15:32:06 +0100659static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200660 [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}