Merge remote-tracking branch 's5p/for-next'
[linux-2.6/next.git] / arch / s390 / kvm / priv.c
blob391626361084e363cfd2f0f4251dd652ebc16a58
1 /*
2 * priv.c - handling privileged instructions
4 * Copyright IBM Corp. 2008
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.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
14 #include <linux/kvm.h>
15 #include <linux/gfp.h>
16 #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"
24 static int handle_set_prefix(struct kvm_vcpu *vcpu)
26 int base2 = vcpu->arch.sie_block->ipb >> 28;
27 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
28 u64 operand2;
29 u32 address = 0;
30 u8 tmp;
32 vcpu->stat.instruction_spx++;
34 operand2 = disp2;
35 if (base2)
36 operand2 += vcpu->arch.guest_gprs[base2];
38 /* must be word boundary */
39 if (operand2 & 3) {
40 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
41 goto out;
44 /* get the value */
45 if (get_guest_u32(vcpu, operand2, &address)) {
46 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
47 goto out;
50 address = address & 0x7fffe000u;
52 /* make sure that the new value is valid memory */
53 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
54 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
55 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
56 goto out;
59 vcpu->arch.sie_block->prefix = address;
60 vcpu->arch.sie_block->ihcpu = 0xffff;
62 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
63 out:
64 return 0;
67 static int handle_store_prefix(struct kvm_vcpu *vcpu)
69 int base2 = vcpu->arch.sie_block->ipb >> 28;
70 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
71 u64 operand2;
72 u32 address;
74 vcpu->stat.instruction_stpx++;
75 operand2 = disp2;
76 if (base2)
77 operand2 += vcpu->arch.guest_gprs[base2];
79 /* must be word boundary */
80 if (operand2 & 3) {
81 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
82 goto out;
85 address = vcpu->arch.sie_block->prefix;
86 address = address & 0x7fffe000u;
88 /* get the value */
89 if (put_guest_u32(vcpu, operand2, address)) {
90 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
91 goto out;
94 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
95 out:
96 return 0;
99 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
101 int base2 = vcpu->arch.sie_block->ipb >> 28;
102 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
103 u64 useraddr;
104 int rc;
106 vcpu->stat.instruction_stap++;
107 useraddr = disp2;
108 if (base2)
109 useraddr += vcpu->arch.guest_gprs[base2];
111 if (useraddr & 1) {
112 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
113 goto out;
116 rc = put_guest_u16(vcpu, useraddr, vcpu->vcpu_id);
117 if (rc == -EFAULT) {
118 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
119 goto out;
122 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
123 out:
124 return 0;
127 static int handle_skey(struct kvm_vcpu *vcpu)
129 vcpu->stat.instruction_storage_key++;
130 vcpu->arch.sie_block->gpsw.addr -= 4;
131 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
132 return 0;
135 static int handle_stsch(struct kvm_vcpu *vcpu)
137 vcpu->stat.instruction_stsch++;
138 VCPU_EVENT(vcpu, 4, "%s", "store subchannel - CC3");
139 /* condition code 3 */
140 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
141 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
142 return 0;
145 static int handle_chsc(struct kvm_vcpu *vcpu)
147 vcpu->stat.instruction_chsc++;
148 VCPU_EVENT(vcpu, 4, "%s", "channel subsystem call - CC3");
149 /* condition code 3 */
150 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
151 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
152 return 0;
155 static int handle_stfl(struct kvm_vcpu *vcpu)
157 unsigned int facility_list;
158 int rc;
160 vcpu->stat.instruction_stfl++;
161 /* only pass the facility bits, which we can handle */
162 facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
164 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
165 &facility_list, sizeof(facility_list));
166 if (rc == -EFAULT)
167 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
168 else
169 VCPU_EVENT(vcpu, 5, "store facility list value %x",
170 facility_list);
171 return 0;
174 static int handle_stidp(struct kvm_vcpu *vcpu)
176 int base2 = vcpu->arch.sie_block->ipb >> 28;
177 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
178 u64 operand2;
179 int rc;
181 vcpu->stat.instruction_stidp++;
182 operand2 = disp2;
183 if (base2)
184 operand2 += vcpu->arch.guest_gprs[base2];
186 if (operand2 & 7) {
187 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
188 goto out;
191 rc = put_guest_u64(vcpu, operand2, vcpu->arch.stidp_data);
192 if (rc == -EFAULT) {
193 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
194 goto out;
197 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
198 out:
199 return 0;
202 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
204 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
205 int cpus = 0;
206 int n;
208 spin_lock(&fi->lock);
209 for (n = 0; n < KVM_MAX_VCPUS; n++)
210 if (fi->local_int[n])
211 cpus++;
212 spin_unlock(&fi->lock);
214 /* deal with other level 3 hypervisors */
215 if (stsi(mem, 3, 2, 2) == -ENOSYS)
216 mem->count = 0;
217 if (mem->count < 8)
218 mem->count++;
219 for (n = mem->count - 1; n > 0 ; n--)
220 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
222 mem->vm[0].cpus_total = cpus;
223 mem->vm[0].cpus_configured = cpus;
224 mem->vm[0].cpus_standby = 0;
225 mem->vm[0].cpus_reserved = 0;
226 mem->vm[0].caf = 1000;
227 memcpy(mem->vm[0].name, "KVMguest", 8);
228 ASCEBC(mem->vm[0].name, 8);
229 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
230 ASCEBC(mem->vm[0].cpi, 16);
233 static int handle_stsi(struct kvm_vcpu *vcpu)
235 int fc = (vcpu->arch.guest_gprs[0] & 0xf0000000) >> 28;
236 int sel1 = vcpu->arch.guest_gprs[0] & 0xff;
237 int sel2 = vcpu->arch.guest_gprs[1] & 0xffff;
238 int base2 = vcpu->arch.sie_block->ipb >> 28;
239 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
240 u64 operand2;
241 unsigned long mem;
243 vcpu->stat.instruction_stsi++;
244 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
246 operand2 = disp2;
247 if (base2)
248 operand2 += vcpu->arch.guest_gprs[base2];
250 if (operand2 & 0xfff && fc > 0)
251 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
253 switch (fc) {
254 case 0:
255 vcpu->arch.guest_gprs[0] = 3 << 28;
256 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
257 return 0;
258 case 1: /* same handling for 1 and 2 */
259 case 2:
260 mem = get_zeroed_page(GFP_KERNEL);
261 if (!mem)
262 goto out_fail;
263 if (stsi((void *) mem, fc, sel1, sel2) == -ENOSYS)
264 goto out_mem;
265 break;
266 case 3:
267 if (sel1 != 2 || sel2 != 2)
268 goto out_fail;
269 mem = get_zeroed_page(GFP_KERNEL);
270 if (!mem)
271 goto out_fail;
272 handle_stsi_3_2_2(vcpu, (void *) mem);
273 break;
274 default:
275 goto out_fail;
278 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
279 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
280 goto out_mem;
282 free_page(mem);
283 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
284 vcpu->arch.guest_gprs[0] = 0;
285 return 0;
286 out_mem:
287 free_page(mem);
288 out_fail:
289 /* condition code 3 */
290 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
291 return 0;
294 static intercept_handler_t priv_handlers[256] = {
295 [0x02] = handle_stidp,
296 [0x10] = handle_set_prefix,
297 [0x11] = handle_store_prefix,
298 [0x12] = handle_store_cpu_address,
299 [0x29] = handle_skey,
300 [0x2a] = handle_skey,
301 [0x2b] = handle_skey,
302 [0x34] = handle_stsch,
303 [0x5f] = handle_chsc,
304 [0x7d] = handle_stsi,
305 [0xb1] = handle_stfl,
308 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
310 intercept_handler_t handler;
313 * a lot of B2 instructions are priviledged. We first check for
314 * the privileged ones, that we can handle in the kernel. If the
315 * kernel can handle this instruction, we check for the problem
316 * state bit and (a) handle the instruction or (b) send a code 2
317 * program check.
318 * Anything else goes to userspace.*/
319 handler = priv_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
320 if (handler) {
321 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
322 return kvm_s390_inject_program_int(vcpu,
323 PGM_PRIVILEGED_OPERATION);
324 else
325 return handler(vcpu);
327 return -EOPNOTSUPP;
330 static int handle_tprot(struct kvm_vcpu *vcpu)
332 int base1 = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
333 int disp1 = (vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16;
334 int base2 = (vcpu->arch.sie_block->ipb & 0xf000) >> 12;
335 int disp2 = vcpu->arch.sie_block->ipb & 0x0fff;
336 u64 address1 = disp1 + base1 ? vcpu->arch.guest_gprs[base1] : 0;
337 u64 address2 = disp2 + base2 ? vcpu->arch.guest_gprs[base2] : 0;
338 struct vm_area_struct *vma;
340 vcpu->stat.instruction_tprot++;
342 /* we only handle the Linux memory detection case:
343 * access key == 0
344 * guest DAT == off
345 * everything else goes to userspace. */
346 if (address2 & 0xf0)
347 return -EOPNOTSUPP;
348 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
349 return -EOPNOTSUPP;
352 down_read(&current->mm->mmap_sem);
353 vma = find_vma(current->mm,
354 (unsigned long) __guestaddr_to_user(vcpu, address1));
355 if (!vma) {
356 up_read(&current->mm->mmap_sem);
357 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
360 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
361 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
362 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
363 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
364 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
366 up_read(&current->mm->mmap_sem);
367 return 0;
370 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
372 /* For e5xx... instructions we only handle TPROT */
373 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
374 return handle_tprot(vcpu);
375 return -EOPNOTSUPP;