blob: 1aeb9335f9e26e4ee1130d8d8bac3efb5cd00fe7 [file] [log] [blame]
Christian Borntraeger453423d2008-03-25 18:47:29 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling privileged instructions
Christian Borntraeger453423d2008-03-25 18:47:29 +01003 *
4 * Copyright IBM Corp. 2008
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 */
13
14#include <linux/kvm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
Christian Borntraeger453423d2008-03-25 18:47:29 +010016#include <linux/errno.h>
17#include <asm/current.h>
18#include <asm/debug.h>
19#include <asm/ebcdic.h>
20#include <asm/sysinfo.h>
21#include "gaccess.h"
22#include "kvm-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020023#include "trace.h"
Christian Borntraeger453423d2008-03-25 18:47:29 +010024
25static int handle_set_prefix(struct kvm_vcpu *vcpu)
26{
27 int base2 = vcpu->arch.sie_block->ipb >> 28;
28 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
29 u64 operand2;
30 u32 address = 0;
31 u8 tmp;
32
33 vcpu->stat.instruction_spx++;
34
35 operand2 = disp2;
36 if (base2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010037 operand2 += vcpu->run->s.regs.gprs[base2];
Christian Borntraeger453423d2008-03-25 18:47:29 +010038
39 /* must be word boundary */
40 if (operand2 & 3) {
41 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
42 goto out;
43 }
44
45 /* get the value */
46 if (get_guest_u32(vcpu, operand2, &address)) {
47 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
48 goto out;
49 }
50
51 address = address & 0x7fffe000u;
52
53 /* make sure that the new value is valid memory */
54 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
55 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
56 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
57 goto out;
58 }
59
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 +010064out:
65 return 0;
66}
67
68static int handle_store_prefix(struct kvm_vcpu *vcpu)
69{
70 int base2 = vcpu->arch.sie_block->ipb >> 28;
71 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
72 u64 operand2;
73 u32 address;
74
75 vcpu->stat.instruction_stpx++;
76 operand2 = disp2;
77 if (base2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010078 operand2 += vcpu->run->s.regs.gprs[base2];
Christian Borntraeger453423d2008-03-25 18:47:29 +010079
80 /* must be word boundary */
81 if (operand2 & 3) {
82 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
83 goto out;
84 }
85
86 address = vcpu->arch.sie_block->prefix;
87 address = address & 0x7fffe000u;
88
89 /* get the value */
90 if (put_guest_u32(vcpu, operand2, address)) {
91 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
92 goto out;
93 }
94
95 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020096 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010097out:
98 return 0;
99}
100
101static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
102{
103 int base2 = vcpu->arch.sie_block->ipb >> 28;
104 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
105 u64 useraddr;
106 int rc;
107
108 vcpu->stat.instruction_stap++;
109 useraddr = disp2;
110 if (base2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100111 useraddr += vcpu->run->s.regs.gprs[base2];
Christian Borntraeger453423d2008-03-25 18:47:29 +0100112
113 if (useraddr & 1) {
114 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
115 goto out;
116 }
117
118 rc = put_guest_u16(vcpu, useraddr, vcpu->vcpu_id);
119 if (rc == -EFAULT) {
120 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
121 goto out;
122 }
123
Heiko Carstens33e19112009-01-09 12:14:56 +0100124 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200125 trace_kvm_s390_handle_stap(vcpu, useraddr);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100126out:
127 return 0;
128}
129
130static int handle_skey(struct kvm_vcpu *vcpu)
131{
132 vcpu->stat.instruction_storage_key++;
133 vcpu->arch.sie_block->gpsw.addr -= 4;
134 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
135 return 0;
136}
137
138static int handle_stsch(struct kvm_vcpu *vcpu)
139{
140 vcpu->stat.instruction_stsch++;
141 VCPU_EVENT(vcpu, 4, "%s", "store subchannel - CC3");
142 /* condition code 3 */
143 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
144 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
145 return 0;
146}
147
148static int handle_chsc(struct kvm_vcpu *vcpu)
149{
150 vcpu->stat.instruction_chsc++;
151 VCPU_EVENT(vcpu, 4, "%s", "channel subsystem call - CC3");
152 /* condition code 3 */
153 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
154 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
155 return 0;
156}
157
Christian Borntraeger453423d2008-03-25 18:47:29 +0100158static int handle_stfl(struct kvm_vcpu *vcpu)
159{
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200160 unsigned int facility_list;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100161 int rc;
162
163 vcpu->stat.instruction_stfl++;
Christian Borntraegera0046b62008-08-29 13:29:45 +0200164 /* only pass the facility bits, which we can handle */
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200165 facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100166
167 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
168 &facility_list, sizeof(facility_list));
169 if (rc == -EFAULT)
170 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200171 else {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100172 VCPU_EVENT(vcpu, 5, "store facility list value %x",
173 facility_list);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200174 trace_kvm_s390_handle_stfl(vcpu, facility_list);
175 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100176 return 0;
177}
178
179static int handle_stidp(struct kvm_vcpu *vcpu)
180{
181 int base2 = vcpu->arch.sie_block->ipb >> 28;
182 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
183 u64 operand2;
184 int rc;
185
186 vcpu->stat.instruction_stidp++;
187 operand2 = disp2;
188 if (base2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100189 operand2 += vcpu->run->s.regs.gprs[base2];
Christian Borntraeger453423d2008-03-25 18:47:29 +0100190
191 if (operand2 & 7) {
192 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
193 goto out;
194 }
195
196 rc = put_guest_u64(vcpu, operand2, vcpu->arch.stidp_data);
197 if (rc == -EFAULT) {
198 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
199 goto out;
200 }
201
202 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
203out:
204 return 0;
205}
206
207static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
208{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200209 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100210 int cpus = 0;
211 int n;
212
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200213 spin_lock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100214 for (n = 0; n < KVM_MAX_VCPUS; n++)
215 if (fi->local_int[n])
216 cpus++;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200217 spin_unlock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100218
219 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200220 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100221 mem->count = 0;
222 if (mem->count < 8)
223 mem->count++;
224 for (n = mem->count - 1; n > 0 ; n--)
225 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
226
227 mem->vm[0].cpus_total = cpus;
228 mem->vm[0].cpus_configured = cpus;
229 mem->vm[0].cpus_standby = 0;
230 mem->vm[0].cpus_reserved = 0;
231 mem->vm[0].caf = 1000;
232 memcpy(mem->vm[0].name, "KVMguest", 8);
233 ASCEBC(mem->vm[0].name, 8);
234 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
235 ASCEBC(mem->vm[0].cpi, 16);
236}
237
238static int handle_stsi(struct kvm_vcpu *vcpu)
239{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100240 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
241 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
242 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100243 int base2 = vcpu->arch.sie_block->ipb >> 28;
244 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
245 u64 operand2;
246 unsigned long mem;
247
248 vcpu->stat.instruction_stsi++;
249 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
250
251 operand2 = disp2;
252 if (base2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100253 operand2 += vcpu->run->s.regs.gprs[base2];
Christian Borntraeger453423d2008-03-25 18:47:29 +0100254
255 if (operand2 & 0xfff && fc > 0)
256 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
257
258 switch (fc) {
259 case 0:
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100260 vcpu->run->s.regs.gprs[0] = 3 << 28;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100261 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
262 return 0;
263 case 1: /* same handling for 1 and 2 */
264 case 2:
265 mem = get_zeroed_page(GFP_KERNEL);
266 if (!mem)
267 goto out_fail;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200268 if (stsi((void *) mem, fc, sel1, sel2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100269 goto out_mem;
270 break;
271 case 3:
272 if (sel1 != 2 || sel2 != 2)
273 goto out_fail;
274 mem = get_zeroed_page(GFP_KERNEL);
275 if (!mem)
276 goto out_fail;
277 handle_stsi_3_2_2(vcpu, (void *) mem);
278 break;
279 default:
280 goto out_fail;
281 }
282
283 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
284 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
285 goto out_mem;
286 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200287 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100288 free_page(mem);
289 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100290 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100291 return 0;
292out_mem:
293 free_page(mem);
294out_fail:
295 /* condition code 3 */
296 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
297 return 0;
298}
299
Cornelia Huck77975352012-12-20 15:32:06 +0100300static const intercept_handler_t priv_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100301 [0x02] = handle_stidp,
302 [0x10] = handle_set_prefix,
303 [0x11] = handle_store_prefix,
304 [0x12] = handle_store_cpu_address,
305 [0x29] = handle_skey,
306 [0x2a] = handle_skey,
307 [0x2b] = handle_skey,
308 [0x34] = handle_stsch,
309 [0x5f] = handle_chsc,
310 [0x7d] = handle_stsi,
311 [0xb1] = handle_stfl,
312};
313
Christian Borntraeger70455a32009-01-22 10:28:29 +0100314int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100315{
316 intercept_handler_t handler;
317
Christian Borntraeger70455a32009-01-22 10:28:29 +0100318 /*
319 * a lot of B2 instructions are priviledged. We first check for
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300320 * the privileged ones, that we can handle in the kernel. If the
Christian Borntraeger70455a32009-01-22 10:28:29 +0100321 * kernel can handle this instruction, we check for the problem
322 * state bit and (a) handle the instruction or (b) send a code 2
323 * program check.
324 * Anything else goes to userspace.*/
Christian Borntraeger453423d2008-03-25 18:47:29 +0100325 handler = priv_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Christian Borntraeger70455a32009-01-22 10:28:29 +0100326 if (handler) {
327 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
328 return kvm_s390_inject_program_int(vcpu,
329 PGM_PRIVILEGED_OPERATION);
330 else
331 return handler(vcpu);
332 }
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100333 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100334}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200335
336static int handle_tprot(struct kvm_vcpu *vcpu)
337{
338 int base1 = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
339 int disp1 = (vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16;
340 int base2 = (vcpu->arch.sie_block->ipb & 0xf000) >> 12;
341 int disp2 = vcpu->arch.sie_block->ipb & 0x0fff;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100342 u64 address1 = disp1 + base1 ? vcpu->run->s.regs.gprs[base1] : 0;
343 u64 address2 = disp2 + base2 ? vcpu->run->s.regs.gprs[base2] : 0;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200344 struct vm_area_struct *vma;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100345 unsigned long user_address;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200346
347 vcpu->stat.instruction_tprot++;
348
349 /* we only handle the Linux memory detection case:
350 * access key == 0
351 * guest DAT == off
352 * everything else goes to userspace. */
353 if (address2 & 0xf0)
354 return -EOPNOTSUPP;
355 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
356 return -EOPNOTSUPP;
357
358
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100359 /* we must resolve the address without holding the mmap semaphore.
360 * This is ok since the userspace hypervisor is not supposed to change
361 * the mapping while the guest queries the memory. Otherwise the guest
362 * might crash or get wrong info anyway. */
363 user_address = (unsigned long) __guestaddr_to_user(vcpu, address1);
364
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200365 down_read(&current->mm->mmap_sem);
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100366 vma = find_vma(current->mm, user_address);
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200367 if (!vma) {
368 up_read(&current->mm->mmap_sem);
369 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
370 }
371
372 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
373 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
374 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
375 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
376 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
377
378 up_read(&current->mm->mmap_sem);
379 return 0;
380}
381
382int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
383{
384 /* For e5xx... instructions we only handle TPROT */
385 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
386 return handle_tprot(vcpu);
387 return -EOPNOTSUPP;
388}
389
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200390static int handle_sckpf(struct kvm_vcpu *vcpu)
391{
392 u32 value;
393
394 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
395 return kvm_s390_inject_program_int(vcpu,
396 PGM_PRIVILEGED_OPERATION);
397
398 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
399 return kvm_s390_inject_program_int(vcpu,
400 PGM_SPECIFICATION);
401
402 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
403 vcpu->arch.sie_block->todpr = value;
404
405 return 0;
406}
407
Cornelia Huck77975352012-12-20 15:32:06 +0100408static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200409 [0x07] = handle_sckpf,
410};
411
412int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
413{
414 intercept_handler_t handler;
415
416 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
417 if (handler)
418 return handler(vcpu);
419 return -EOPNOTSUPP;
420}