blob: d715842f56ca122b9bab3c93895d608f8e478e94 [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{
Christian Borntraeger453423d2008-03-25 18:47:29 +010027 u64 operand2;
28 u32 address = 0;
29 u8 tmp;
30
31 vcpu->stat.instruction_spx++;
32
Cornelia Huckb1c571a2012-12-20 15:32:07 +010033 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010034
35 /* must be word boundary */
36 if (operand2 & 3) {
37 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
38 goto out;
39 }
40
41 /* get the value */
42 if (get_guest_u32(vcpu, operand2, &address)) {
43 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
44 goto out;
45 }
46
47 address = address & 0x7fffe000u;
48
49 /* make sure that the new value is valid memory */
50 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
51 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
52 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
53 goto out;
54 }
55
Christian Borntraeger8d26cf72012-01-11 11:19:32 +010056 kvm_s390_set_prefix(vcpu, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010057
58 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020059 trace_kvm_s390_handle_prefix(vcpu, 1, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010060out:
61 return 0;
62}
63
64static int handle_store_prefix(struct kvm_vcpu *vcpu)
65{
Christian Borntraeger453423d2008-03-25 18:47:29 +010066 u64 operand2;
67 u32 address;
68
69 vcpu->stat.instruction_stpx++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +010070
71 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +010072
73 /* must be word boundary */
74 if (operand2 & 3) {
75 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
76 goto out;
77 }
78
79 address = vcpu->arch.sie_block->prefix;
80 address = address & 0x7fffe000u;
81
82 /* get the value */
83 if (put_guest_u32(vcpu, operand2, address)) {
84 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
85 goto out;
86 }
87
88 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
Cornelia Huck5786fff2012-07-23 17:20:29 +020089 trace_kvm_s390_handle_prefix(vcpu, 0, address);
Christian Borntraeger453423d2008-03-25 18:47:29 +010090out:
91 return 0;
92}
93
94static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
95{
Christian Borntraeger453423d2008-03-25 18:47:29 +010096 u64 useraddr;
97 int rc;
98
99 vcpu->stat.instruction_stap++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100100
101 useraddr = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100102
103 if (useraddr & 1) {
104 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
105 goto out;
106 }
107
108 rc = put_guest_u16(vcpu, useraddr, vcpu->vcpu_id);
109 if (rc == -EFAULT) {
110 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
111 goto out;
112 }
113
Heiko Carstens33e19112009-01-09 12:14:56 +0100114 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200115 trace_kvm_s390_handle_stap(vcpu, useraddr);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100116out:
117 return 0;
118}
119
120static int handle_skey(struct kvm_vcpu *vcpu)
121{
122 vcpu->stat.instruction_storage_key++;
123 vcpu->arch.sie_block->gpsw.addr -= 4;
124 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
125 return 0;
126}
127
128static int handle_stsch(struct kvm_vcpu *vcpu)
129{
130 vcpu->stat.instruction_stsch++;
131 VCPU_EVENT(vcpu, 4, "%s", "store subchannel - CC3");
132 /* condition code 3 */
133 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
134 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
135 return 0;
136}
137
138static int handle_chsc(struct kvm_vcpu *vcpu)
139{
140 vcpu->stat.instruction_chsc++;
141 VCPU_EVENT(vcpu, 4, "%s", "channel subsystem call - 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
Christian Borntraeger453423d2008-03-25 18:47:29 +0100148static int handle_stfl(struct kvm_vcpu *vcpu)
149{
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200150 unsigned int facility_list;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100151 int rc;
152
153 vcpu->stat.instruction_stfl++;
Christian Borntraegera0046b62008-08-29 13:29:45 +0200154 /* only pass the facility bits, which we can handle */
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200155 facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100156
157 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
158 &facility_list, sizeof(facility_list));
159 if (rc == -EFAULT)
160 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200161 else {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100162 VCPU_EVENT(vcpu, 5, "store facility list value %x",
163 facility_list);
Cornelia Huck5786fff2012-07-23 17:20:29 +0200164 trace_kvm_s390_handle_stfl(vcpu, facility_list);
165 }
Christian Borntraeger453423d2008-03-25 18:47:29 +0100166 return 0;
167}
168
169static int handle_stidp(struct kvm_vcpu *vcpu)
170{
Christian Borntraeger453423d2008-03-25 18:47:29 +0100171 u64 operand2;
172 int rc;
173
174 vcpu->stat.instruction_stidp++;
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100175
176 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100177
178 if (operand2 & 7) {
179 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
180 goto out;
181 }
182
183 rc = put_guest_u64(vcpu, operand2, vcpu->arch.stidp_data);
184 if (rc == -EFAULT) {
185 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
186 goto out;
187 }
188
189 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
190out:
191 return 0;
192}
193
194static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
195{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200196 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100197 int cpus = 0;
198 int n;
199
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200200 spin_lock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100201 for (n = 0; n < KVM_MAX_VCPUS; n++)
202 if (fi->local_int[n])
203 cpus++;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200204 spin_unlock(&fi->lock);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100205
206 /* deal with other level 3 hypervisors */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200207 if (stsi(mem, 3, 2, 2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100208 mem->count = 0;
209 if (mem->count < 8)
210 mem->count++;
211 for (n = mem->count - 1; n > 0 ; n--)
212 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
213
214 mem->vm[0].cpus_total = cpus;
215 mem->vm[0].cpus_configured = cpus;
216 mem->vm[0].cpus_standby = 0;
217 mem->vm[0].cpus_reserved = 0;
218 mem->vm[0].caf = 1000;
219 memcpy(mem->vm[0].name, "KVMguest", 8);
220 ASCEBC(mem->vm[0].name, 8);
221 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
222 ASCEBC(mem->vm[0].cpi, 16);
223}
224
225static int handle_stsi(struct kvm_vcpu *vcpu)
226{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100227 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
228 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
229 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100230 u64 operand2;
231 unsigned long mem;
232
233 vcpu->stat.instruction_stsi++;
234 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
235
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100236 operand2 = kvm_s390_get_base_disp_s(vcpu);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100237
238 if (operand2 & 0xfff && fc > 0)
239 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
240
241 switch (fc) {
242 case 0:
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100243 vcpu->run->s.regs.gprs[0] = 3 << 28;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100244 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
245 return 0;
246 case 1: /* same handling for 1 and 2 */
247 case 2:
248 mem = get_zeroed_page(GFP_KERNEL);
249 if (!mem)
250 goto out_fail;
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200251 if (stsi((void *) mem, fc, sel1, sel2))
Christian Borntraeger453423d2008-03-25 18:47:29 +0100252 goto out_mem;
253 break;
254 case 3:
255 if (sel1 != 2 || sel2 != 2)
256 goto out_fail;
257 mem = get_zeroed_page(GFP_KERNEL);
258 if (!mem)
259 goto out_fail;
260 handle_stsi_3_2_2(vcpu, (void *) mem);
261 break;
262 default:
263 goto out_fail;
264 }
265
266 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
267 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
268 goto out_mem;
269 }
Cornelia Huck5786fff2012-07-23 17:20:29 +0200270 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
Christian Borntraeger453423d2008-03-25 18:47:29 +0100271 free_page(mem);
272 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100273 vcpu->run->s.regs.gprs[0] = 0;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100274 return 0;
275out_mem:
276 free_page(mem);
277out_fail:
278 /* condition code 3 */
279 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
280 return 0;
281}
282
Cornelia Huck77975352012-12-20 15:32:06 +0100283static const intercept_handler_t priv_handlers[256] = {
Christian Borntraeger453423d2008-03-25 18:47:29 +0100284 [0x02] = handle_stidp,
285 [0x10] = handle_set_prefix,
286 [0x11] = handle_store_prefix,
287 [0x12] = handle_store_cpu_address,
288 [0x29] = handle_skey,
289 [0x2a] = handle_skey,
290 [0x2b] = handle_skey,
291 [0x34] = handle_stsch,
292 [0x5f] = handle_chsc,
293 [0x7d] = handle_stsi,
294 [0xb1] = handle_stfl,
295};
296
Christian Borntraeger70455a32009-01-22 10:28:29 +0100297int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
Christian Borntraeger453423d2008-03-25 18:47:29 +0100298{
299 intercept_handler_t handler;
300
Christian Borntraeger70455a32009-01-22 10:28:29 +0100301 /*
302 * a lot of B2 instructions are priviledged. We first check for
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300303 * the privileged ones, that we can handle in the kernel. If the
Christian Borntraeger70455a32009-01-22 10:28:29 +0100304 * kernel can handle this instruction, we check for the problem
305 * state bit and (a) handle the instruction or (b) send a code 2
306 * program check.
307 * Anything else goes to userspace.*/
Christian Borntraeger453423d2008-03-25 18:47:29 +0100308 handler = priv_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
Christian Borntraeger70455a32009-01-22 10:28:29 +0100309 if (handler) {
310 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
311 return kvm_s390_inject_program_int(vcpu,
312 PGM_PRIVILEGED_OPERATION);
313 else
314 return handler(vcpu);
315 }
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100316 return -EOPNOTSUPP;
Christian Borntraeger453423d2008-03-25 18:47:29 +0100317}
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200318
319static int handle_tprot(struct kvm_vcpu *vcpu)
320{
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100321 u64 address1, address2;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200322 struct vm_area_struct *vma;
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100323 unsigned long user_address;
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200324
325 vcpu->stat.instruction_tprot++;
326
Cornelia Huckb1c571a2012-12-20 15:32:07 +0100327 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
328
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200329 /* we only handle the Linux memory detection case:
330 * access key == 0
331 * guest DAT == off
332 * everything else goes to userspace. */
333 if (address2 & 0xf0)
334 return -EOPNOTSUPP;
335 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
336 return -EOPNOTSUPP;
337
338
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100339 /* we must resolve the address without holding the mmap semaphore.
340 * This is ok since the userspace hypervisor is not supposed to change
341 * the mapping while the guest queries the memory. Otherwise the guest
342 * might crash or get wrong info anyway. */
343 user_address = (unsigned long) __guestaddr_to_user(vcpu, address1);
344
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200345 down_read(&current->mm->mmap_sem);
Christian Borntraeger1eddb852011-11-17 11:00:43 +0100346 vma = find_vma(current->mm, user_address);
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200347 if (!vma) {
348 up_read(&current->mm->mmap_sem);
349 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
350 }
351
352 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
353 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
354 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
355 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
356 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
357
358 up_read(&current->mm->mmap_sem);
359 return 0;
360}
361
362int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
363{
364 /* For e5xx... instructions we only handle TPROT */
365 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
366 return handle_tprot(vcpu);
367 return -EOPNOTSUPP;
368}
369
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200370static int handle_sckpf(struct kvm_vcpu *vcpu)
371{
372 u32 value;
373
374 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
375 return kvm_s390_inject_program_int(vcpu,
376 PGM_PRIVILEGED_OPERATION);
377
378 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
379 return kvm_s390_inject_program_int(vcpu,
380 PGM_SPECIFICATION);
381
382 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
383 vcpu->arch.sie_block->todpr = value;
384
385 return 0;
386}
387
Cornelia Huck77975352012-12-20 15:32:06 +0100388static const intercept_handler_t x01_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200389 [0x07] = handle_sckpf,
390};
391
392int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
393{
394 intercept_handler_t handler;
395
396 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
397 if (handler)
398 return handler(vcpu);
399 return -EOPNOTSUPP;
400}