blob: 59200ee275e568ae99d593484b5575be320b79b8 [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>
Heiko Carstense769ece2013-07-26 15:04:01 +020019#include <asm/facility.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010020#include <asm/current.h>
21#include <asm/debug.h>
22#include <asm/ebcdic.h>
23#include <asm/sysinfo.h>
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020024#include <asm/pgtable.h>
25#include <asm/pgalloc.h>
26#include <asm/io.h>
Cornelia Huck48a3e952012-12-20 15:32:09 +010027#include <asm/ptrace.h>
28#include <asm/compat.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010029#include "gaccess.h"
30#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020031#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010032
33static int handle_set_prefix(struct kvm_vcpu *vcpu)
34{
Christian Borntraeger453423d2008-03-25 18:47:29 +010035 u64 operand2;
36 u32 address = 0;
37 u8 tmp;
38
39 vcpu->stat.instruction_spx++;
40
Thomas Huth5087dfa2013-06-20 17:22:01 +020041 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
42 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
43
Cornelia Huckb1c571a2012-12-20 15:32:07 +010044 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010045
46 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010047 if (operand2 & 3)
48 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010049
50 /* get the value */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010051 if (get_guest(vcpu, address, (u32 __user *) operand2))
52 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010053
54 address = address & 0x7fffe000u;
55
56 /* make sure that the new value is valid memory */
57 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010058 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)))
59 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010060
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010061 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010062
63 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020064 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010065 return 0;
66}
67
68static int handle_store_prefix(struct kvm_vcpu *vcpu)
69{
Christian Borntraeger453423d2008-03-25 18:47:29 +010070 u64 operand2;
71 u32 address;
72
73 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +010074
Thomas Huth5087dfa2013-06-20 17:22:01 +020075 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
76 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
77
Cornelia Huckb1c571a2012-12-20 15:32:07 +010078 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010079
80 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010081 if (operand2 & 3)
82 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +010083
84 address = vcpu->arch.sie_block->prefix;
85 address = address & 0x7fffe000u;
86
87 /* get the value */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010088 if (put_guest(vcpu, address, (u32 __user *)operand2))
89 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010090
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 +010093 return 0;
94}
95
96static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
97{
Christian Borntraeger453423d2008-03-25 18:47:29 +010098 u64 useraddr;
Christian Borntraeger453423d2008-03-25 18:47:29 +010099
100 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100101
Thomas Huth5087dfa2013-06-20 17:22:01 +0200102 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
103 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
104
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100105 useraddr = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100106
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100107 if (useraddr & 1)
108 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100109
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100110 if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
111 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100112
Heiko Carstens33e19112009-01-09 12:14:56 +0100113 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200114 trace_kvm_s390_handle_stap(vcpu, useraddr);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100115 return 0;
116}
117
118static int handle_skey(struct kvm_vcpu *vcpu)
119{
120 vcpu->stat.instruction_storage_key++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200121
122 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
123 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
124
Martin Schwidefskydfcf7dc2013-05-17 14:41:32 +0200125 vcpu->arch.sie_block->gpsw.addr =
126 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100127 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 Carstensdb4a29c2013-03-25 17:22:53 +0100138 if (addr & 3)
139 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100140 cc = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100141 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100142 if (!inti)
143 goto no_interrupt;
144 cc = 1;
145 if (addr) {
146 /*
147 * Store the two-word I/O interruption code into the
148 * provided area.
149 */
Thomas Huth133608f2013-06-20 17:22:03 +0200150 if (put_guest(vcpu, inti->io.subchannel_id, (u16 __user *)addr)
151 || put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *)(addr + 2))
152 || put_guest(vcpu, inti->io.io_int_parm, (u32 __user *)(addr + 4)))
153 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100154 } else {
155 /*
156 * Store the three-word I/O interruption code into
157 * the appropriate lowcore area.
158 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100159 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
160 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
161 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
162 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100163 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100164 kfree(inti);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100165no_interrupt:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100166 /* Set condition code and we're done. */
Thomas Huthea828eb2013-07-26 15:04:06 +0200167 kvm_s390_set_psw_cc(vcpu, cc);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100168 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 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200222 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100223 return 0;
224 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100225}
226
Christian Borntraeger453423d2008-03-25 18:47:29 +0100227static int handle_stfl(struct kvm_vcpu *vcpu)
228{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100229 int rc;
230
231 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200232
233 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
234 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
235
Christian Borntraeger453423d2008-03-25 18:47:29 +0100236 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
Michael Mueller78c4b592013-07-26 15:04:04 +0200237 vfacilities, 4);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100238 if (rc)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100239 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Michael Mueller78c4b592013-07-26 15:04:04 +0200240 VCPU_EVENT(vcpu, 5, "store facility list value %x",
241 *(unsigned int *) vfacilities);
242 trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100243 return 0;
244}
245
Cornelia Huck48a3e952012-12-20 15:32:09 +0100246static void handle_new_psw(struct kvm_vcpu *vcpu)
247{
248 /* Check whether the new psw is enabled for machine checks. */
249 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
250 kvm_s390_deliver_pending_machine_checks(vcpu);
251}
252
253#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
254#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100255#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100256#define PSW_ADDR_31 0x000000007fffffffUL
257
Heiko Carstens3736b872013-03-25 17:22:52 +0100258static int is_valid_psw(psw_t *psw) {
259 if (psw->mask & PSW_MASK_UNASSIGNED)
260 return 0;
261 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
262 if (psw->addr & ~PSW_ADDR_31)
263 return 0;
264 }
265 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
266 return 0;
267 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
268 return 0;
269 return 1;
270}
271
Cornelia Huck48a3e952012-12-20 15:32:09 +0100272int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
273{
Heiko Carstens3736b872013-03-25 17:22:52 +0100274 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100275 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100276 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100277
Heiko Carstens3736b872013-03-25 17:22:52 +0100278 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200279 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
280
Cornelia Huck48a3e952012-12-20 15:32:09 +0100281 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 Carstens6fd0fcc2013-03-25 17:22:51 +0100286 if (!(new_psw.mask & PSW32_MASK_BASE))
287 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100288 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
289 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
290 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
291 if (!is_valid_psw(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
297static int handle_lpswe(struct kvm_vcpu *vcpu)
298{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100299 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100300 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100301
Thomas Huth5087dfa2013-06-20 17:22:01 +0200302 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
303 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
304
Cornelia Huck48a3e952012-12-20 15:32:09 +0100305 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100306 if (addr & 7)
307 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100308 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
309 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens3736b872013-03-25 17:22:52 +0100310 vcpu->arch.sie_block->gpsw = new_psw;
311 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100312 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100313 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100314 return 0;
315}
316
Christian Borntraeger453423d2008-03-25 18:47:29 +0100317static int handle_stidp(struct kvm_vcpu *vcpu)
318{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100319 u64 operand2;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100320
321 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100322
Thomas Huth5087dfa2013-06-20 17:22:01 +0200323 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
324 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
325
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100326 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100327
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100328 if (operand2 & 7)
329 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100330
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100331 if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
332 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100333
334 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
Christian Borntraeger453423d2008-03-25 18:47:29 +0100335 return 0;
336}
337
338static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
339{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200340 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100341 int cpus = 0;
342 int n;
343
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200344 spin_lock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100345 for (n = 0; n < KVM_MAX_VCPUS; n++)
346 if (fi->local_int[n])
347 cpus++;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200348 spin_unlock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100349
350 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200351 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100352 mem->count = 0;
353 if (mem->count < 8)
354 mem->count++;
355 for (n = mem->count - 1; n > 0 ; n--)
356 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
357
358 mem->vm[0].cpus_total = cpus;
359 mem->vm[0].cpus_configured = cpus;
360 mem->vm[0].cpus_standby = 0;
361 mem->vm[0].cpus_reserved = 0;
362 mem->vm[0].caf = 1000;
363 memcpy(mem->vm[0].name, "KVMguest", 8);
364 ASCEBC(mem->vm[0].name, 8);
365 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
366 ASCEBC(mem->vm[0].cpi, 16);
367}
368
369static int handle_stsi(struct kvm_vcpu *vcpu)
370{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100371 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
372 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
373 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100374 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100375 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100376 int rc = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100377
378 vcpu->stat.instruction_stsi++;
379 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
380
Thomas Huth5087dfa2013-06-20 17:22:01 +0200381 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
382 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
383
Thomas Huth87d41fb2013-06-20 17:22:05 +0200384 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200385 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200386 return 0;
387 }
388
389 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
390 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
391 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
392
393 if (fc == 0) {
394 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200395 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200396 return 0;
397 }
398
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100399 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100400
Thomas Huth87d41fb2013-06-20 17:22:05 +0200401 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100402 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
403
404 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100405 case 1: /* same handling for 1 and 2 */
406 case 2:
407 mem = get_zeroed_page(GFP_KERNEL);
408 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100409 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200410 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100411 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100412 break;
413 case 3:
414 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100415 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100416 mem = get_zeroed_page(GFP_KERNEL);
417 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100418 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100419 handle_stsi_3_2_2(vcpu, (void *) mem);
420 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100421 }
422
423 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100424 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstensc51f0682013-03-25 17:22:54 +0100425 goto out_exception;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100426 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200427 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100428 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200429 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100430 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100431 return 0;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100432out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200433 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstensc51f0682013-03-25 17:22:54 +0100434out_exception:
435 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100436 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100437}
438
Cornelia Huckf379aae2012-12-20 15:32:10 +0100439static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100440 [0x02] = handle_stidp,
441 [0x10] = handle_set_prefix,
442 [0x11] = handle_store_prefix,
443 [0x12] = handle_store_cpu_address,
444 [0x29] = handle_skey,
445 [0x2a] = handle_skey,
446 [0x2b] = handle_skey,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100447 [0x30] = handle_io_inst,
448 [0x31] = handle_io_inst,
449 [0x32] = handle_io_inst,
450 [0x33] = handle_io_inst,
451 [0x34] = handle_io_inst,
452 [0x35] = handle_io_inst,
453 [0x36] = handle_io_inst,
454 [0x37] = handle_io_inst,
455 [0x38] = handle_io_inst,
456 [0x39] = handle_io_inst,
457 [0x3a] = handle_io_inst,
458 [0x3b] = handle_io_inst,
459 [0x3c] = handle_io_inst,
460 [0x5f] = handle_io_inst,
461 [0x74] = handle_io_inst,
462 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100463 [0x7d] = handle_stsi,
464 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100465 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100466};
467
Christian Borntraeger70455a32009-01-22 10:28:29 +0100468int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100469{
470 intercept_handler_t handler;
471
Christian Borntraeger70455a32009-01-22 10:28:29 +0100472 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200473 * A lot of B2 instructions are priviledged. Here we check for
474 * the privileged ones, that we can handle in the kernel.
475 * Anything else goes to userspace.
476 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100477 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200478 if (handler)
479 return handler(vcpu);
480
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100481 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100482}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200483
Cornelia Huck48a3e952012-12-20 15:32:09 +0100484static int handle_epsw(struct kvm_vcpu *vcpu)
485{
486 int reg1, reg2;
487
Thomas Huthaeb87c32013-06-12 13:54:57 +0200488 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100489
490 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200491 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100492 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
493 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200494 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100495 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200496 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100497 }
498 return 0;
499}
500
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200501#define PFMF_RESERVED 0xfffc0101UL
502#define PFMF_SK 0x00020000UL
503#define PFMF_CF 0x00010000UL
504#define PFMF_UI 0x00008000UL
505#define PFMF_FSC 0x00007000UL
506#define PFMF_NQ 0x00000800UL
507#define PFMF_MR 0x00000400UL
508#define PFMF_MC 0x00000200UL
509#define PFMF_KEY 0x000000feUL
510
511static int handle_pfmf(struct kvm_vcpu *vcpu)
512{
513 int reg1, reg2;
514 unsigned long start, end;
515
516 vcpu->stat.instruction_pfmf++;
517
518 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
519
520 if (!MACHINE_HAS_PFMF)
521 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
522
523 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200524 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200525
526 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
527 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
528
529 /* Only provide non-quiescing support if the host supports it */
Heiko Carstense769ece2013-07-26 15:04:01 +0200530 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200531 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
532
533 /* No support for conditional-SSKE */
534 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
535 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
536
537 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
538 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
539 case 0x00000000:
540 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
541 break;
542 case 0x00001000:
543 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
544 break;
545 /* We dont support EDAT2
546 case 0x00002000:
547 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
548 break;*/
549 default:
550 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
551 }
552 while (start < end) {
553 unsigned long useraddr;
554
555 useraddr = gmap_translate(start, vcpu->arch.gmap);
556 if (IS_ERR((void *)useraddr))
557 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
558
559 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
560 if (clear_user((void __user *)useraddr, PAGE_SIZE))
561 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
562 }
563
564 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
565 if (set_guest_storage_key(current->mm, useraddr,
566 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
567 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
568 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
569 }
570
571 start += PAGE_SIZE;
572 }
573 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
574 vcpu->run->s.regs.gprs[reg2] = end;
575 return 0;
576}
577
Cornelia Huck48a3e952012-12-20 15:32:09 +0100578static const intercept_handler_t b9_handlers[256] = {
579 [0x8d] = handle_epsw,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100580 [0x9c] = handle_io_inst,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200581 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100582};
583
584int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
585{
586 intercept_handler_t handler;
587
588 /* This is handled just as for the B2 instructions. */
589 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200590 if (handler)
591 return handler(vcpu);
592
Cornelia Huck48a3e952012-12-20 15:32:09 +0100593 return -EOPNOTSUPP;
594}
595
Thomas Huth953ed882013-06-20 17:22:04 +0200596int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
597{
598 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
599 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
600 u64 useraddr;
601 u32 val = 0;
602 int reg, rc;
603
604 vcpu->stat.instruction_lctl++;
605
606 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
607 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
608
609 useraddr = kvm_s390_get_base_disp_rs(vcpu);
610
611 if (useraddr & 3)
612 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
613
614 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
615 useraddr);
616 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
617
618 reg = reg1;
619 do {
620 rc = get_guest(vcpu, val, (u32 __user *) useraddr);
621 if (rc)
622 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
623 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
624 vcpu->arch.sie_block->gcr[reg] |= val;
625 useraddr += 4;
626 if (reg == reg3)
627 break;
628 reg = (reg + 1) % 16;
629 } while (1);
630
631 return 0;
632}
633
634static int handle_lctlg(struct kvm_vcpu *vcpu)
635{
636 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
637 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
638 u64 useraddr;
639 int reg, rc;
640
641 vcpu->stat.instruction_lctlg++;
642
643 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
644 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
645
646 useraddr = kvm_s390_get_base_disp_rsy(vcpu);
647
648 if (useraddr & 7)
649 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
650
651 reg = reg1;
652
653 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
654 useraddr);
655 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
656
657 do {
658 rc = get_guest(vcpu, vcpu->arch.sie_block->gcr[reg],
659 (u64 __user *) useraddr);
660 if (rc)
661 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
662 useraddr += 8;
663 if (reg == reg3)
664 break;
665 reg = (reg + 1) % 16;
666 } while (1);
667
668 return 0;
669}
670
Cornelia Huckf379aae2012-12-20 15:32:10 +0100671static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +0200672 [0x2f] = handle_lctlg,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100673 [0x8a] = handle_io_inst,
674};
675
Thomas Huth953ed882013-06-20 17:22:04 +0200676int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +0100677{
678 intercept_handler_t handler;
679
Cornelia Huckf379aae2012-12-20 15:32:10 +0100680 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
681 if (handler)
682 return handler(vcpu);
683 return -EOPNOTSUPP;
684}
685
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200686static int handle_tprot(struct kvm_vcpu *vcpu)
687{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100688 u64 address1, address2;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200689 struct vm_area_struct *vma;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100690 unsigned long user_address;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200691
692 vcpu->stat.instruction_tprot++;
693
Thomas Huthf9f6bbc2013-06-20 17:22:00 +0200694 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
695 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
696
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100697 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
698
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200699 /* we only handle the Linux memory detection case:
700 * access key == 0
701 * guest DAT == off
702 * everything else goes to userspace. */
703 if (address2 & 0xf0)
704 return -EOPNOTSUPP;
705 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
706 return -EOPNOTSUPP;
707
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200708 down_read(&current->mm->mmap_sem);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100709 user_address = __gmap_translate(address1, vcpu->arch.gmap);
710 if (IS_ERR_VALUE(user_address))
711 goto out_inject;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100712 vma = find_vma(current->mm, user_address);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100713 if (!vma)
714 goto out_inject;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200715 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
716 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
717 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
718 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
719 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
720
721 up_read(&current->mm->mmap_sem);
722 return 0;
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100723
724out_inject:
725 up_read(&current->mm->mmap_sem);
726 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200727}
728
729int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
730{
731 /* For e5xx... instructions we only handle TPROT */
732 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
733 return handle_tprot(vcpu);
734 return -EOPNOTSUPP;
735}
736
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200737static int handle_sckpf(struct kvm_vcpu *vcpu)
738{
739 u32 value;
740
741 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200742 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200743
744 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
745 return kvm_s390_inject_program_int(vcpu,
746 PGM_SPECIFICATION);
747
748 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
749 vcpu->arch.sie_block->todpr = value;
750
751 return 0;
752}
753
Cornelia Huck77975352012-12-20 15:32:06 +0100754static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200755 [0x07] = handle_sckpf,
756};
757
758int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
759{
760 intercept_handler_t handler;
761
762 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
763 if (handler)
764 return handler(vcpu);
765 return -EOPNOTSUPP;
766}