blob: 36c34cf228897cd774fc006c243a6a203bea4f4b [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
Thomas Huth6a3f95a2013-09-12 10:33:49 +020033/* Handle SCK (SET CLOCK) interception */
34static int handle_set_clock(struct kvm_vcpu *vcpu)
35{
36 struct kvm_vcpu *cpup;
37 s64 hostclk, val;
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010038 int i, rc;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020039 u64 op2;
Thomas Huth6a3f95a2013-09-12 10:33:49 +020040
41 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
42 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
43
44 op2 = kvm_s390_get_base_disp_s(vcpu);
45 if (op2 & 7) /* Operand must be on a doubleword boundary */
46 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens0e7a3f92014-01-01 16:50:11 +010047 rc = read_guest(vcpu, op2, &val, sizeof(val));
48 if (rc)
49 return kvm_s390_inject_prog_cond(vcpu, rc);
Thomas Huth6a3f95a2013-09-12 10:33:49 +020050
51 if (store_tod_clock(&hostclk)) {
52 kvm_s390_set_psw_cc(vcpu, 3);
53 return 0;
54 }
55 val = (val - hostclk) & ~0x3fUL;
56
57 mutex_lock(&vcpu->kvm->lock);
58 kvm_for_each_vcpu(i, cpup, vcpu->kvm)
59 cpup->arch.sie_block->epoch = val;
60 mutex_unlock(&vcpu->kvm->lock);
61
62 kvm_s390_set_psw_cc(vcpu, 0);
63 return 0;
64}
65
Christian Borntraeger453423d2008-03-25 18:47:29 +010066static int handle_set_prefix(struct kvm_vcpu *vcpu)
67{
Christian Borntraeger453423d2008-03-25 18:47:29 +010068 u64 operand2;
Heiko Carstens665170c2014-01-01 16:47:12 +010069 u32 address;
70 int rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +010071
72 vcpu->stat.instruction_spx++;
73
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 /* get the value */
Heiko Carstens665170c2014-01-01 16:47:12 +010084 rc = read_guest(vcpu, operand2, &address, sizeof(address));
85 if (rc)
86 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +010087
Heiko Carstens665170c2014-01-01 16:47:12 +010088 address &= 0x7fffe000u;
Christian Borntraeger453423d2008-03-25 18:47:29 +010089
Heiko Carstens665170c2014-01-01 16:47:12 +010090 /*
91 * Make sure the new value is valid memory. We only need to check the
92 * first page, since address is 8k aligned and memory pieces are always
93 * at least 1MB aligned and have at least a size of 1MB.
94 */
95 if (kvm_is_error_gpa(vcpu->kvm, address))
Heiko Carstensdb4a29c2013-03-25 17:22:53 +010096 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +010097
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010098 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010099
100 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200101 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100102 return 0;
103}
104
105static int handle_store_prefix(struct kvm_vcpu *vcpu)
106{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100107 u64 operand2;
108 u32 address;
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100109 int rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100110
111 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100112
Thomas Huth5087dfa2013-06-20 17:22:01 +0200113 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
114 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
115
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100116 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100117
118 /* must be word boundary */
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100119 if (operand2 & 3)
120 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100121
122 address = vcpu->arch.sie_block->prefix;
123 address = address & 0x7fffe000u;
124
125 /* get the value */
Heiko Carstensf748f4a2014-01-01 16:52:47 +0100126 rc = write_guest(vcpu, operand2, &address, sizeof(address));
127 if (rc)
128 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100129
130 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200131 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100132 return 0;
133}
134
135static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
136{
Heiko Carstens8b96de02014-01-01 16:53:27 +0100137 u16 vcpu_id = vcpu->vcpu_id;
138 u64 ga;
139 int rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100140
141 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100142
Thomas Huth5087dfa2013-06-20 17:22:01 +0200143 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
144 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
145
Heiko Carstens8b96de02014-01-01 16:53:27 +0100146 ga = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100147
Heiko Carstens8b96de02014-01-01 16:53:27 +0100148 if (ga & 1)
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100149 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100150
Heiko Carstens8b96de02014-01-01 16:53:27 +0100151 rc = write_guest(vcpu, ga, &vcpu_id, sizeof(vcpu_id));
152 if (rc)
153 return kvm_s390_inject_prog_cond(vcpu, rc);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100154
Heiko Carstens8b96de02014-01-01 16:53:27 +0100155 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", ga);
156 trace_kvm_s390_handle_stap(vcpu, ga);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100157 return 0;
158}
159
Dominik Dingel693ffc02014-01-14 18:11:14 +0100160static void __skey_check_enable(struct kvm_vcpu *vcpu)
161{
162 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
163 return;
164
165 s390_enable_skey();
166 trace_kvm_s390_skey_related_inst(vcpu);
167 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
168}
169
170
Christian Borntraeger453423d2008-03-25 18:47:29 +0100171static int handle_skey(struct kvm_vcpu *vcpu)
172{
Dominik Dingel693ffc02014-01-14 18:11:14 +0100173 __skey_check_enable(vcpu);
174
Christian Borntraeger453423d2008-03-25 18:47:29 +0100175 vcpu->stat.instruction_storage_key++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200176
177 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
178 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
179
Martin Schwidefskydfcf7dc2013-05-17 14:41:32 +0200180 vcpu->arch.sie_block->gpsw.addr =
181 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100182 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
183 return 0;
184}
185
Heiko Carstens8a2422342014-01-10 14:33:28 +0100186static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
187{
188 psw_t *psw = &vcpu->arch.sie_block->gpsw;
189
190 vcpu->stat.instruction_ipte_interlock++;
191 if (psw_bits(*psw).p)
192 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
193 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
194 psw->addr = __rewind_psw(*psw, 4);
195 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
196 return 0;
197}
198
Thomas Huthaca84242013-09-12 10:33:48 +0200199static int handle_test_block(struct kvm_vcpu *vcpu)
200{
201 unsigned long hva;
202 gpa_t addr;
203 int reg2;
204
205 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
206 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
207
208 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
209 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
210 addr = kvm_s390_real_to_abs(vcpu, addr);
211
212 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
213 if (kvm_is_error_hva(hva))
214 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
215 /*
216 * We don't expect errors on modern systems, and do not care
217 * about storage keys (yet), so let's just clear the page.
218 */
219 if (clear_user((void __user *)hva, PAGE_SIZE) != 0)
220 return -EFAULT;
221 kvm_s390_set_psw_cc(vcpu, 0);
222 vcpu->run->s.regs.gprs[0] = 0;
223 return 0;
224}
225
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100226static int handle_tpi(struct kvm_vcpu *vcpu)
227{
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100228 struct kvm_s390_interrupt_info *inti;
Heiko Carstens7c959e82013-03-05 13:14:46 +0100229 u64 addr;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100230 int cc;
231
232 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100233 if (addr & 3)
234 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100235 cc = 0;
Thomas Huthf0926692013-10-09 14:15:54 +0200236 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100237 if (!inti)
238 goto no_interrupt;
239 cc = 1;
240 if (addr) {
241 /*
242 * Store the two-word I/O interruption code into the
243 * provided area.
244 */
Thomas Huth133608f2013-06-20 17:22:03 +0200245 if (put_guest(vcpu, inti->io.subchannel_id, (u16 __user *)addr)
246 || put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *)(addr + 2))
247 || put_guest(vcpu, inti->io.io_int_parm, (u32 __user *)(addr + 4)))
248 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100249 } else {
250 /*
251 * Store the three-word I/O interruption code into
252 * the appropriate lowcore area.
253 */
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100254 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
255 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
256 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
257 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100258 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100259 kfree(inti);
Heiko Carstens7c959e82013-03-05 13:14:46 +0100260no_interrupt:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100261 /* Set condition code and we're done. */
Thomas Huthea828eb2013-07-26 15:04:06 +0200262 kvm_s390_set_psw_cc(vcpu, cc);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100263 return 0;
264}
265
266static int handle_tsch(struct kvm_vcpu *vcpu)
267{
268 struct kvm_s390_interrupt_info *inti;
269
270 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
271 vcpu->run->s.regs.gprs[1]);
272
273 /*
274 * Prepare exit to userspace.
275 * We indicate whether we dequeued a pending I/O interrupt
276 * so that userspace can re-inject it if the instruction gets
277 * a program check. While this may re-order the pending I/O
278 * interrupts, this is no problem since the priority is kept
279 * intact.
280 */
281 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
282 vcpu->run->s390_tsch.dequeued = !!inti;
283 if (inti) {
284 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
285 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
286 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
287 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
288 }
289 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
290 kfree(inti);
291 return -EREMOTE;
292}
293
Cornelia Huckf379aae2012-12-20 15:32:10 +0100294static int handle_io_inst(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100295{
Cornelia Huckf379aae2012-12-20 15:32:10 +0100296 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100297
Thomas Huth5087dfa2013-06-20 17:22:01 +0200298 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
299 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
300
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100301 if (vcpu->kvm->arch.css_support) {
302 /*
303 * Most I/O instructions will be handled by userspace.
304 * Exceptions are tpi and the interrupt portion of tsch.
305 */
306 if (vcpu->arch.sie_block->ipa == 0xb236)
307 return handle_tpi(vcpu);
308 if (vcpu->arch.sie_block->ipa == 0xb235)
309 return handle_tsch(vcpu);
310 /* Handle in userspace. */
311 return -EOPNOTSUPP;
312 } else {
313 /*
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100314 * Set condition code 3 to stop the guest from issuing channel
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100315 * I/O instructions.
316 */
Thomas Huthea828eb2013-07-26 15:04:06 +0200317 kvm_s390_set_psw_cc(vcpu, 3);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100318 return 0;
319 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100320}
321
Christian Borntraeger453423d2008-03-25 18:47:29 +0100322static int handle_stfl(struct kvm_vcpu *vcpu)
323{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100324 int rc;
325
326 vcpu->stat.instruction_stfl++;
Thomas Huth5087dfa2013-06-20 17:22:01 +0200327
328 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
329 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
330
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100331 rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
332 vfacilities, 4);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100333 if (rc)
Heiko Carstens0f9701c2014-01-01 16:56:41 +0100334 return rc;
Michael Mueller78c4b592013-07-26 15:04:04 +0200335 VCPU_EVENT(vcpu, 5, "store facility list value %x",
336 *(unsigned int *) vfacilities);
337 trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100338 return 0;
339}
340
Cornelia Huck48a3e952012-12-20 15:32:09 +0100341static void handle_new_psw(struct kvm_vcpu *vcpu)
342{
343 /* Check whether the new psw is enabled for machine checks. */
344 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
345 kvm_s390_deliver_pending_machine_checks(vcpu);
346}
347
348#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
349#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
Heiko Carstensd21683e2013-03-25 17:22:49 +0100350#define PSW_ADDR_24 0x0000000000ffffffUL
Cornelia Huck48a3e952012-12-20 15:32:09 +0100351#define PSW_ADDR_31 0x000000007fffffffUL
352
Heiko Carstens3736b872013-03-25 17:22:52 +0100353static int is_valid_psw(psw_t *psw) {
354 if (psw->mask & PSW_MASK_UNASSIGNED)
355 return 0;
356 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
357 if (psw->addr & ~PSW_ADDR_31)
358 return 0;
359 }
360 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
361 return 0;
362 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
363 return 0;
364 return 1;
365}
366
Cornelia Huck48a3e952012-12-20 15:32:09 +0100367int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
368{
Heiko Carstens3736b872013-03-25 17:22:52 +0100369 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100370 psw_compat_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100371 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100372
Heiko Carstens3736b872013-03-25 17:22:52 +0100373 if (gpsw->mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200374 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
375
Cornelia Huck48a3e952012-12-20 15:32:09 +0100376 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100377 if (addr & 7)
378 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100379 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
380 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100381 if (!(new_psw.mask & PSW32_MASK_BASE))
382 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens3736b872013-03-25 17:22:52 +0100383 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
384 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
385 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
386 if (!is_valid_psw(gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100387 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100388 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100389 return 0;
390}
391
392static int handle_lpswe(struct kvm_vcpu *vcpu)
393{
Cornelia Huck48a3e952012-12-20 15:32:09 +0100394 psw_t new_psw;
Heiko Carstens3736b872013-03-25 17:22:52 +0100395 u64 addr;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100396
Thomas Huth5087dfa2013-06-20 17:22:01 +0200397 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
398 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
399
Cornelia Huck48a3e952012-12-20 15:32:09 +0100400 addr = kvm_s390_get_base_disp_s(vcpu);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100401 if (addr & 7)
402 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100403 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
404 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstens3736b872013-03-25 17:22:52 +0100405 vcpu->arch.sie_block->gpsw = new_psw;
406 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
Heiko Carstens6fd0fcc2013-03-25 17:22:51 +0100407 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100408 handle_new_psw(vcpu);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100409 return 0;
410}
411
Christian Borntraeger453423d2008-03-25 18:47:29 +0100412static int handle_stidp(struct kvm_vcpu *vcpu)
413{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100414 u64 operand2;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100415
416 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100417
Thomas Huth5087dfa2013-06-20 17:22:01 +0200418 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
419 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
420
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100421 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100422
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100423 if (operand2 & 7)
424 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100425
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100426 if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
427 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100428
429 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
Christian Borntraeger453423d2008-03-25 18:47:29 +0100430 return 0;
431}
432
433static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
434{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100435 int cpus = 0;
436 int n;
437
Jens Freimannff520a62014-02-24 10:11:41 +0100438 cpus = atomic_read(&vcpu->kvm->online_vcpus);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100439
440 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200441 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100442 mem->count = 0;
443 if (mem->count < 8)
444 mem->count++;
445 for (n = mem->count - 1; n > 0 ; n--)
446 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
447
448 mem->vm[0].cpus_total = cpus;
449 mem->vm[0].cpus_configured = cpus;
450 mem->vm[0].cpus_standby = 0;
451 mem->vm[0].cpus_reserved = 0;
452 mem->vm[0].caf = 1000;
453 memcpy(mem->vm[0].name, "KVMguest", 8);
454 ASCEBC(mem->vm[0].name, 8);
455 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
456 ASCEBC(mem->vm[0].cpi, 16);
457}
458
459static int handle_stsi(struct kvm_vcpu *vcpu)
460{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100461 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
462 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
463 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100464 unsigned long mem = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100465 u64 operand2;
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100466 int rc = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100467
468 vcpu->stat.instruction_stsi++;
469 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
470
Thomas Huth5087dfa2013-06-20 17:22:01 +0200471 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
472 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
473
Thomas Huth87d41fb2013-06-20 17:22:05 +0200474 if (fc > 3) {
Thomas Huthea828eb2013-07-26 15:04:06 +0200475 kvm_s390_set_psw_cc(vcpu, 3);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200476 return 0;
477 }
478
479 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
480 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
481 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
482
483 if (fc == 0) {
484 vcpu->run->s.regs.gprs[0] = 3 << 28;
Thomas Huthea828eb2013-07-26 15:04:06 +0200485 kvm_s390_set_psw_cc(vcpu, 0);
Thomas Huth87d41fb2013-06-20 17:22:05 +0200486 return 0;
487 }
488
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100489 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100490
Thomas Huth87d41fb2013-06-20 17:22:05 +0200491 if (operand2 & 0xfff)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100492 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
493
494 switch (fc) {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100495 case 1: /* same handling for 1 and 2 */
496 case 2:
497 mem = get_zeroed_page(GFP_KERNEL);
498 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100499 goto out_no_data;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200500 if (stsi((void *) mem, fc, sel1, sel2))
Heiko Carstensc51f0682013-03-25 17:22:54 +0100501 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100502 break;
503 case 3:
504 if (sel1 != 2 || sel2 != 2)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100505 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100506 mem = get_zeroed_page(GFP_KERNEL);
507 if (!mem)
Heiko Carstensc51f0682013-03-25 17:22:54 +0100508 goto out_no_data;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100509 handle_stsi_3_2_2(vcpu, (void *) mem);
510 break;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100511 }
512
513 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100514 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Heiko Carstensc51f0682013-03-25 17:22:54 +0100515 goto out_exception;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100516 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200517 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100518 free_page(mem);
Thomas Huthea828eb2013-07-26 15:04:06 +0200519 kvm_s390_set_psw_cc(vcpu, 0);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100520 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100521 return 0;
Heiko Carstensc51f0682013-03-25 17:22:54 +0100522out_no_data:
Thomas Huthea828eb2013-07-26 15:04:06 +0200523 kvm_s390_set_psw_cc(vcpu, 3);
Heiko Carstensc51f0682013-03-25 17:22:54 +0100524out_exception:
525 free_page(mem);
Heiko Carstensdb4a29c2013-03-25 17:22:53 +0100526 return rc;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100527}
528
Cornelia Huckf379aae2012-12-20 15:32:10 +0100529static const intercept_handler_t b2_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100530 [0x02] = handle_stidp,
Thomas Huth6a3f95a2013-09-12 10:33:49 +0200531 [0x04] = handle_set_clock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100532 [0x10] = handle_set_prefix,
533 [0x11] = handle_store_prefix,
534 [0x12] = handle_store_cpu_address,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100535 [0x21] = handle_ipte_interlock,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100536 [0x29] = handle_skey,
537 [0x2a] = handle_skey,
538 [0x2b] = handle_skey,
Thomas Huthaca84242013-09-12 10:33:48 +0200539 [0x2c] = handle_test_block,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100540 [0x30] = handle_io_inst,
541 [0x31] = handle_io_inst,
542 [0x32] = handle_io_inst,
543 [0x33] = handle_io_inst,
544 [0x34] = handle_io_inst,
545 [0x35] = handle_io_inst,
546 [0x36] = handle_io_inst,
547 [0x37] = handle_io_inst,
548 [0x38] = handle_io_inst,
549 [0x39] = handle_io_inst,
550 [0x3a] = handle_io_inst,
551 [0x3b] = handle_io_inst,
552 [0x3c] = handle_io_inst,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100553 [0x50] = handle_ipte_interlock,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100554 [0x5f] = handle_io_inst,
555 [0x74] = handle_io_inst,
556 [0x76] = handle_io_inst,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100557 [0x7d] = handle_stsi,
558 [0xb1] = handle_stfl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100559 [0xb2] = handle_lpswe,
Christian Borntraeger453423d2008-03-25 18:47:29 +0100560};
561
Christian Borntraeger70455a32009-01-22 10:28:29 +0100562int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100563{
564 intercept_handler_t handler;
565
Christian Borntraeger70455a32009-01-22 10:28:29 +0100566 /*
Thomas Huth5087dfa2013-06-20 17:22:01 +0200567 * A lot of B2 instructions are priviledged. Here we check for
568 * the privileged ones, that we can handle in the kernel.
569 * Anything else goes to userspace.
570 */
Cornelia Huckf379aae2012-12-20 15:32:10 +0100571 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200572 if (handler)
573 return handler(vcpu);
574
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100575 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100576}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200577
Cornelia Huck48a3e952012-12-20 15:32:09 +0100578static int handle_epsw(struct kvm_vcpu *vcpu)
579{
580 int reg1, reg2;
581
Thomas Huthaeb87c32013-06-12 13:54:57 +0200582 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100583
584 /* This basically extracts the mask half of the psw. */
Thomas Huth843200e2013-07-26 15:04:05 +0200585 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100586 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
587 if (reg2) {
Thomas Huth843200e2013-07-26 15:04:05 +0200588 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100589 vcpu->run->s.regs.gprs[reg2] |=
Thomas Huth843200e2013-07-26 15:04:05 +0200590 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100591 }
592 return 0;
593}
594
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200595#define PFMF_RESERVED 0xfffc0101UL
596#define PFMF_SK 0x00020000UL
597#define PFMF_CF 0x00010000UL
598#define PFMF_UI 0x00008000UL
599#define PFMF_FSC 0x00007000UL
600#define PFMF_NQ 0x00000800UL
601#define PFMF_MR 0x00000400UL
602#define PFMF_MC 0x00000200UL
603#define PFMF_KEY 0x000000feUL
604
605static int handle_pfmf(struct kvm_vcpu *vcpu)
606{
607 int reg1, reg2;
608 unsigned long start, end;
609
610 vcpu->stat.instruction_pfmf++;
611
612 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
613
614 if (!MACHINE_HAS_PFMF)
615 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
616
617 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200618 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200619
620 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
621 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
622
623 /* Only provide non-quiescing support if the host supports it */
Heiko Carstense769ece2013-07-26 15:04:01 +0200624 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200625 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
626
627 /* No support for conditional-SSKE */
628 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
629 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
630
631 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
632 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
633 case 0x00000000:
634 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
635 break;
636 case 0x00001000:
637 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
638 break;
639 /* We dont support EDAT2
640 case 0x00002000:
641 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
642 break;*/
643 default:
644 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
645 }
646 while (start < end) {
647 unsigned long useraddr;
648
649 useraddr = gmap_translate(start, vcpu->arch.gmap);
650 if (IS_ERR((void *)useraddr))
651 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
652
653 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
654 if (clear_user((void __user *)useraddr, PAGE_SIZE))
655 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
656 }
657
658 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
Dominik Dingel693ffc02014-01-14 18:11:14 +0100659 __skey_check_enable(vcpu);
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200660 if (set_guest_storage_key(current->mm, useraddr,
661 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
662 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
663 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
664 }
665
666 start += PAGE_SIZE;
667 }
668 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
669 vcpu->run->s.regs.gprs[reg2] = end;
670 return 0;
671}
672
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200673static int handle_essa(struct kvm_vcpu *vcpu)
674{
675 /* entries expected to be 1FF */
676 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
677 unsigned long *cbrlo, cbrle;
678 struct gmap *gmap;
679 int i;
680
681 VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
682 gmap = vcpu->arch.gmap;
683 vcpu->stat.instruction_essa++;
Dominik Dingelb31605c2014-03-25 13:47:11 +0100684 if (!kvm_s390_cmma_enabled(vcpu->kvm))
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200685 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
686
687 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
688 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
689
690 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
691 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
692
693 /* Rewind PSW to repeat the ESSA instruction */
694 vcpu->arch.sie_block->gpsw.addr =
695 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
696 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
697 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
698 down_read(&gmap->mm->mmap_sem);
699 for (i = 0; i < entries; ++i) {
700 cbrle = cbrlo[i];
701 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
702 /* invalid entry */
703 break;
704 /* try to free backing */
705 __gmap_zap(cbrle, gmap);
706 }
707 up_read(&gmap->mm->mmap_sem);
708 if (i < entries)
709 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
710 return 0;
711}
712
Cornelia Huck48a3e952012-12-20 15:32:09 +0100713static const intercept_handler_t b9_handlers[256] = {
Heiko Carstens8a2422342014-01-10 14:33:28 +0100714 [0x8a] = handle_ipte_interlock,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100715 [0x8d] = handle_epsw,
Heiko Carstens8a2422342014-01-10 14:33:28 +0100716 [0x8e] = handle_ipte_interlock,
717 [0x8f] = handle_ipte_interlock,
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200718 [0xab] = handle_essa,
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +0200719 [0xaf] = handle_pfmf,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100720};
721
722int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
723{
724 intercept_handler_t handler;
725
726 /* This is handled just as for the B2 instructions. */
727 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Thomas Huth5087dfa2013-06-20 17:22:01 +0200728 if (handler)
729 return handler(vcpu);
730
Cornelia Huck48a3e952012-12-20 15:32:09 +0100731 return -EOPNOTSUPP;
732}
733
Thomas Huth953ed882013-06-20 17:22:04 +0200734int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
735{
736 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
737 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
738 u64 useraddr;
739 u32 val = 0;
740 int reg, rc;
741
742 vcpu->stat.instruction_lctl++;
743
744 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
745 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
746
747 useraddr = kvm_s390_get_base_disp_rs(vcpu);
748
749 if (useraddr & 3)
750 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
751
752 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
753 useraddr);
754 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
755
756 reg = reg1;
757 do {
758 rc = get_guest(vcpu, val, (u32 __user *) useraddr);
759 if (rc)
760 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
761 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
762 vcpu->arch.sie_block->gcr[reg] |= val;
763 useraddr += 4;
764 if (reg == reg3)
765 break;
766 reg = (reg + 1) % 16;
767 } while (1);
768
769 return 0;
770}
771
772static int handle_lctlg(struct kvm_vcpu *vcpu)
773{
774 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
775 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
776 u64 useraddr;
777 int reg, rc;
778
779 vcpu->stat.instruction_lctlg++;
780
781 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
782 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
783
784 useraddr = kvm_s390_get_base_disp_rsy(vcpu);
785
786 if (useraddr & 7)
787 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
788
789 reg = reg1;
790
791 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
792 useraddr);
793 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
794
795 do {
796 rc = get_guest(vcpu, vcpu->arch.sie_block->gcr[reg],
797 (u64 __user *) useraddr);
798 if (rc)
799 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
800 useraddr += 8;
801 if (reg == reg3)
802 break;
803 reg = (reg + 1) % 16;
804 } while (1);
805
806 return 0;
807}
808
Cornelia Huckf379aae2012-12-20 15:32:10 +0100809static const intercept_handler_t eb_handlers[256] = {
Thomas Huth953ed882013-06-20 17:22:04 +0200810 [0x2f] = handle_lctlg,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100811};
812
Thomas Huth953ed882013-06-20 17:22:04 +0200813int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
Cornelia Huckf379aae2012-12-20 15:32:10 +0100814{
815 intercept_handler_t handler;
816
Cornelia Huckf379aae2012-12-20 15:32:10 +0100817 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
818 if (handler)
819 return handler(vcpu);
820 return -EOPNOTSUPP;
821}
822
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200823static int handle_tprot(struct kvm_vcpu *vcpu)
824{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100825 u64 address1, address2;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200826 struct vm_area_struct *vma;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100827 unsigned long user_address;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200828
829 vcpu->stat.instruction_tprot++;
830
Thomas Huthf9f6bbc2013-06-20 17:22:00 +0200831 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
832 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
833
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100834 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
835
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200836 /* we only handle the Linux memory detection case:
837 * access key == 0
838 * guest DAT == off
839 * everything else goes to userspace. */
840 if (address2 & 0xf0)
841 return -EOPNOTSUPP;
842 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
843 return -EOPNOTSUPP;
844
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200845 down_read(&current->mm->mmap_sem);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100846 user_address = __gmap_translate(address1, vcpu->arch.gmap);
847 if (IS_ERR_VALUE(user_address))
848 goto out_inject;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100849 vma = find_vma(current->mm, user_address);
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100850 if (!vma)
851 goto out_inject;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200852 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
853 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
854 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
855 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
856 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
857
858 up_read(&current->mm->mmap_sem);
859 return 0;
Heiko Carstens59a1fa22013-03-05 13:14:42 +0100860
861out_inject:
862 up_read(&current->mm->mmap_sem);
863 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200864}
865
866int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
867{
868 /* For e5xx... instructions we only handle TPROT */
869 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
870 return handle_tprot(vcpu);
871 return -EOPNOTSUPP;
872}
873
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200874static int handle_sckpf(struct kvm_vcpu *vcpu)
875{
876 u32 value;
877
878 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
Thomas Huth208dd752013-06-20 17:21:59 +0200879 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200880
881 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
882 return kvm_s390_inject_program_int(vcpu,
883 PGM_SPECIFICATION);
884
885 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
886 vcpu->arch.sie_block->todpr = value;
887
888 return 0;
889}
890
Cornelia Huck77975352012-12-20 15:32:06 +0100891static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200892 [0x07] = handle_sckpf,
893};
894
895int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
896{
897 intercept_handler_t handler;
898
899 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
900 if (handler)
901 return handler(vcpu);
902 return -EOPNOTSUPP;
903}