blob: 697e34b59036cdd322f08b460fa34a1e83f78b53 [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
Thomas Huth5087dfa2013-06-20 17:22:01 +020040 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
41 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
42
Cornelia Huckb1c571a2012-12-20 15:32:07 +010043 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010044
45 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010046 if (operand2 & 3)
47 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010048
49 /* get the value */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010050 if (get_guest(vcpu, address, (u32 __user *) operand2))
51 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010052
53 address = address & 0x7fffe000u;
54
55 /* make sure that the new value is valid memory */
56 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010057 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)))
58 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010059
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010060 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010061
62 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020063 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010064 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
Thomas Huth5087dfa2013-06-20 17:22:01 +020074 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
75 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
76
Cornelia Huckb1c571a2012-12-20 15:32:07 +010077 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010078
79 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010080 if (operand2 & 3)
81 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010082
83 address = vcpu->arch.sie_block->prefix;
84 address = address & 0x7fffe000u;
85
86 /* get the value */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010087 if (put_guest(vcpu, address, (u32 __user *)operand2))
88 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010089
90 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020091 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010092 return 0;
93}
94
95static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
96{
Christian Borntraeger453423d2008-03-25 18:47:29 +010097 u64 useraddr;
Christian Borntraeger453423d2008-03-25 18:47:29 +010098
99 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100100
Thomas Huth5087dfa2013-06-20 17:22:01 +0200101 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
102 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
103
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100104 useraddr = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100105
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100106 if (useraddr & 1)
107 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100108
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100109 if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
110 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100111
Heiko Carstens33e19112009-01-09 12:14:56 +0100112 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200113 trace_kvm_s390_handle_stap(vcpu, useraddr);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100114 return 0;
115}
116
117static int handle_skey(struct kvm_vcpu *vcpu)
118{
119 vcpu->stat.instruction_storage_key++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200120
121 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
122 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
123
Martin Schwidefskydfcf7dc2013-05-17 14:41:32 +0200124 vcpu->arch.sie_block->gpsw.addr =
125 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100126 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
127 return 0;
128}
129
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100130static int handle_tpi(struct kvm_vcpu *vcpu)
131{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100132 struct kvm_s390_interrupt_info *inti;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100133 u64 addr;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100134 int cc;
135
136 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100137 if (addr & 3)
138 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100139 cc = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100140 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100141 if (!inti)
142 goto no_interrupt;
143 cc = 1;
144 if (addr) {
145 /*
146 * Store the two-word I/O interruption code into the
147 * provided area.
148 */
Thomas Huth133608f2013-06-20 17:22:03 +0200149 if (put_guest(vcpu, inti->io.subchannel_id, (u16 __user *)addr)
150 || put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *)(addr + 2))
151 || put_guest(vcpu, inti->io.io_int_parm, (u32 __user *)(addr + 4)))
152 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100153 } else {
154 /*
155 * Store the three-word I/O interruption code into
156 * the appropriate lowcore area.
157 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100158 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
159 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
160 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
161 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100162 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100163 kfree(inti);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100164no_interrupt:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100165 /* Set condition code and we're done. */
166 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
167 vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44;
168 return 0;
169}
170
171static int handle_tsch(struct kvm_vcpu *vcpu)
172{
173 struct kvm_s390_interrupt_info *inti;
174
175 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
176 vcpu->run->s.regs.gprs[1]);
177
178 /*
179 * Prepare exit to userspace.
180 * We indicate whether we dequeued a pending I/O interrupt
181 * so that userspace can re-inject it if the instruction gets
182 * a program check. While this may re-order the pending I/O
183 * interrupts, this is no problem since the priority is kept
184 * intact.
185 */
186 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
187 vcpu->run->s390_tsch.dequeued = !!inti;
188 if (inti) {
189 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
190 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
191 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
192 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
193 }
194 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
195 kfree(inti);
196 return -EREMOTE;
197}
198
Cornelia Huckf379aae2012-12-20 15:32:10 +0100199static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100200{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100201 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100202
Thomas Huth5087dfa2013-06-20 17:22:01 +0200203 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
204 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
205
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100206 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{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100230 int rc;
231
232 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200233
234 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
235 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
236
Christian Borntraeger453423d2008-03-25 18:47:29 +0100237 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
Michael Mueller78c4b592013-07-26 15:04:04 +0200238 vfacilities, 4);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100239 if (rc)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100240 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Michael Mueller78c4b592013-07-26 15:04:04 +0200241 VCPU_EVENT(vcpu, 5, "store facility list value %x",
242 *(unsigned int *) vfacilities);
243 trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100244 return 0;
245}
246
Cornelia Huck48a3e952012-12-20 15:32:09 +0100247static void handle_new_psw(struct kvm_vcpu *vcpu)
248{
249 /* Check whether the new psw is enabled for machine checks. */
250 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
251 kvm_s390_deliver_pending_machine_checks(vcpu);
252}
253
254#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
255#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100256#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100257#define PSW_ADDR_31 0x000000007fffffffUL
258
Heiko Carstens3736b872013-03-25 17:22:52 +0100259static int is_valid_psw(psw_t *psw) {
260 if (psw->mask & PSW_MASK_UNASSIGNED)
261 return 0;
262 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
263 if (psw->addr & ~PSW_ADDR_31)
264 return 0;
265 }
266 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
267 return 0;
268 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
269 return 0;
270 return 1;
271}
272
Cornelia Huck48a3e952012-12-20 15:32:09 +0100273int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
274{
Heiko Carstens3736b872013-03-25 17:22:52 +0100275 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100276 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100277 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100278
Heiko Carstens3736b872013-03-25 17:22:52 +0100279 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200280 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
281
Cornelia Huck48a3e952012-12-20 15:32:09 +0100282 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100283 if (addr & 7)
284 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100285 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
286 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100287 if (!(new_psw.mask & PSW32_MASK_BASE))
288 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100289 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
290 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
291 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
292 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100293 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100294 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100295 return 0;
296}
297
298static int handle_lpswe(struct kvm_vcpu *vcpu)
299{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100300 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100301 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100302
Thomas Huth5087dfa2013-06-20 17:22:01 +0200303 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
304 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
305
Cornelia Huck48a3e952012-12-20 15:32:09 +0100306 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100307 if (addr & 7)
308 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100309 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
310 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens3736b872013-03-25 17:22:52 +0100311 vcpu->arch.sie_block->gpsw = new_psw;
312 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100313 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100314 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100315 return 0;
316}
317
Christian Borntraeger453423d2008-03-25 18:47:29 +0100318static int handle_stidp(struct kvm_vcpu *vcpu)
319{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100320 u64 operand2;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100321
322 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100323
Thomas Huth5087dfa2013-06-20 17:22:01 +0200324 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
325 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
326
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100327 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100328
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100329 if (operand2 & 7)
330 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100331
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100332 if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
333 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100334
335 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
Christian Borntraeger453423d2008-03-25 18:47:29 +0100336 return 0;
337}
338
339static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
340{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200341 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100342 int cpus = 0;
343 int n;
344
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200345 spin_lock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100346 for (n = 0; n < KVM_MAX_VCPUS; n++)
347 if (fi->local_int[n])
348 cpus++;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200349 spin_unlock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100350
351 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200352 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100353 mem->count = 0;
354 if (mem->count < 8)
355 mem->count++;
356 for (n = mem->count - 1; n > 0 ; n--)
357 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
358
359 mem->vm[0].cpus_total = cpus;
360 mem->vm[0].cpus_configured = cpus;
361 mem->vm[0].cpus_standby = 0;
362 mem->vm[0].cpus_reserved = 0;
363 mem->vm[0].caf = 1000;
364 memcpy(mem->vm[0].name, "KVMguest", 8);
365 ASCEBC(mem->vm[0].name, 8);
366 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
367 ASCEBC(mem->vm[0].cpi, 16);
368}
369
370static int handle_stsi(struct kvm_vcpu *vcpu)
371{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100372 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
373 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
374 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100375 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100376 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100377 int rc = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100378
379 vcpu->stat.instruction_stsi++;
380 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
381
Thomas Huth5087dfa2013-06-20 17:22:01 +0200382 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
383 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
384
Thomas Huth87d41fb2013-06-20 17:22:05 +0200385 if (fc > 3) {
386 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44; /* cc 3 */
387 return 0;
388 }
389
390 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
391 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
392 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
393
394 if (fc == 0) {
395 vcpu->run->s.regs.gprs[0] = 3 << 28;
396 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); /* cc 0 */
397 return 0;
398 }
399
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100400 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100401
Thomas Huth87d41fb2013-06-20 17:22:05 +0200402 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100403 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
404
405 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100406 case 1: /* same handling for 1 and 2 */
407 case 2:
408 mem = get_zeroed_page(GFP_KERNEL);
409 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100410 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200411 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100412 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100413 break;
414 case 3:
415 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100416 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100417 mem = get_zeroed_page(GFP_KERNEL);
418 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100419 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100420 handle_stsi_3_2_2(vcpu, (void *) mem);
421 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100422 }
423
424 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100425 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstensc51f0682013-03-25 17:22:54 +0100426 goto out_exception;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100427 }
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;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100433out_no_data:
Christian Borntraeger453423d2008-03-25 18:47:29 +0100434 /* condition code 3 */
435 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100436out_exception:
437 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100438 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100439}
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 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200475 * A lot of B2 instructions are priviledged. Here we check for
476 * the privileged ones, that we can handle in the kernel.
477 * Anything else goes to userspace.
478 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100479 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200480 if (handler)
481 return handler(vcpu);
482
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100483 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100484}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200485
Cornelia Huck48a3e952012-12-20 15:32:09 +0100486static int handle_epsw(struct kvm_vcpu *vcpu)
487{
488 int reg1, reg2;
489
Thomas Huthaeb87c32013-06-12 13:54:57 +0200490 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100491
492 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200493 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100494 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
495 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200496 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100497 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200498 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100499 }
500 return 0;
501}
502
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200503#define PFMF_RESERVED 0xfffc0101UL
504#define PFMF_SK 0x00020000UL
505#define PFMF_CF 0x00010000UL
506#define PFMF_UI 0x00008000UL
507#define PFMF_FSC 0x00007000UL
508#define PFMF_NQ 0x00000800UL
509#define PFMF_MR 0x00000400UL
510#define PFMF_MC 0x00000200UL
511#define PFMF_KEY 0x000000feUL
512
513static int handle_pfmf(struct kvm_vcpu *vcpu)
514{
515 int reg1, reg2;
516 unsigned long start, end;
517
518 vcpu->stat.instruction_pfmf++;
519
520 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
521
522 if (!MACHINE_HAS_PFMF)
523 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
524
525 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200526 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200527
528 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
529 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
530
531 /* Only provide non-quiescing support if the host supports it */
532 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
533 S390_lowcore.stfl_fac_list & 0x00020000)
534 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
535
536 /* No support for conditional-SSKE */
537 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
538 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
539
540 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
541 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
542 case 0x00000000:
543 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
544 break;
545 case 0x00001000:
546 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
547 break;
548 /* We dont support EDAT2
549 case 0x00002000:
550 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
551 break;*/
552 default:
553 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
554 }
555 while (start < end) {
556 unsigned long useraddr;
557
558 useraddr = gmap_translate(start, vcpu->arch.gmap);
559 if (IS_ERR((void *)useraddr))
560 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
561
562 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
563 if (clear_user((void __user *)useraddr, PAGE_SIZE))
564 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
565 }
566
567 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
568 if (set_guest_storage_key(current->mm, useraddr,
569 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
570 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
571 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
572 }
573
574 start += PAGE_SIZE;
575 }
576 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
577 vcpu->run->s.regs.gprs[reg2] = end;
578 return 0;
579}
580
Cornelia Huck48a3e952012-12-20 15:32:09 +0100581static const intercept_handler_t b9_handlers[256] = {
582 [0x8d] = handle_epsw,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100583 [0x9c] = handle_io_inst,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200584 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100585};
586
587int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
588{
589 intercept_handler_t handler;
590
591 /* This is handled just as for the B2 instructions. */
592 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200593 if (handler)
594 return handler(vcpu);
595
Cornelia Huck48a3e952012-12-20 15:32:09 +0100596 return -EOPNOTSUPP;
597}
598
Thomas Huth953ed882013-06-20 17:22:04 +0200599int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
600{
601 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
602 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
603 u64 useraddr;
604 u32 val = 0;
605 int reg, rc;
606
607 vcpu->stat.instruction_lctl++;
608
609 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
610 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
611
612 useraddr = kvm_s390_get_base_disp_rs(vcpu);
613
614 if (useraddr & 3)
615 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
616
617 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
618 useraddr);
619 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
620
621 reg = reg1;
622 do {
623 rc = get_guest(vcpu, val, (u32 __user *) useraddr);
624 if (rc)
625 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
626 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
627 vcpu->arch.sie_block->gcr[reg] |= val;
628 useraddr += 4;
629 if (reg == reg3)
630 break;
631 reg = (reg + 1) % 16;
632 } while (1);
633
634 return 0;
635}
636
637static int handle_lctlg(struct kvm_vcpu *vcpu)
638{
639 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
640 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
641 u64 useraddr;
642 int reg, rc;
643
644 vcpu->stat.instruction_lctlg++;
645
646 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
647 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
648
649 useraddr = kvm_s390_get_base_disp_rsy(vcpu);
650
651 if (useraddr & 7)
652 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
653
654 reg = reg1;
655
656 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
657 useraddr);
658 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
659
660 do {
661 rc = get_guest(vcpu, vcpu->arch.sie_block->gcr[reg],
662 (u64 __user *) useraddr);
663 if (rc)
664 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
665 useraddr += 8;
666 if (reg == reg3)
667 break;
668 reg = (reg + 1) % 16;
669 } while (1);
670
671 return 0;
672}
673
Cornelia Huckf379aae2012-12-20 15:32:10 +0100674static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +0200675 [0x2f] = handle_lctlg,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100676 [0x8a] = handle_io_inst,
677};
678
Thomas Huth953ed882013-06-20 17:22:04 +0200679int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +0100680{
681 intercept_handler_t handler;
682
Cornelia Huckf379aae2012-12-20 15:32:10 +0100683 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
684 if (handler)
685 return handler(vcpu);
686 return -EOPNOTSUPP;
687}
688
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200689static int handle_tprot(struct kvm_vcpu *vcpu)
690{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100691 u64 address1, address2;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200692 struct vm_area_struct *vma;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100693 unsigned long user_address;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200694
695 vcpu->stat.instruction_tprot++;
696
Thomas Huthf9f6bbc2013-06-20 17:22:00 +0200697 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
698 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
699
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100700 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
701
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200702 /* we only handle the Linux memory detection case:
703 * access key == 0
704 * guest DAT == off
705 * everything else goes to userspace. */
706 if (address2 & 0xf0)
707 return -EOPNOTSUPP;
708 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
709 return -EOPNOTSUPP;
710
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200711 down_read(&current->mm->mmap_sem);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100712 user_address = __gmap_translate(address1, vcpu->arch.gmap);
713 if (IS_ERR_VALUE(user_address))
714 goto out_inject;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100715 vma = find_vma(current->mm, user_address);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100716 if (!vma)
717 goto out_inject;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200718 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
719 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
720 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
721 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
722 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
723
724 up_read(&current->mm->mmap_sem);
725 return 0;
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100726
727out_inject:
728 up_read(&current->mm->mmap_sem);
729 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200730}
731
732int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
733{
734 /* For e5xx... instructions we only handle TPROT */
735 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
736 return handle_tprot(vcpu);
737 return -EOPNOTSUPP;
738}
739
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200740static int handle_sckpf(struct kvm_vcpu *vcpu)
741{
742 u32 value;
743
744 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200745 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200746
747 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
748 return kvm_s390_inject_program_int(vcpu,
749 PGM_SPECIFICATION);
750
751 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
752 vcpu->arch.sie_block->todpr = value;
753
754 return 0;
755}
756
Cornelia Huck77975352012-12-20 15:32:06 +0100757static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200758 [0x07] = handle_sckpf,
759};
760
761int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
762{
763 intercept_handler_t handler;
764
765 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
766 if (handler)
767 return handler(vcpu);
768 return -EOPNOTSUPP;
769}