Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / s390 / kvm / priv.c
blobc02286c6a9315efcd4936e79a9798f2c16bf256f
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/errno.h>
16 #include <asm/current.h>
17 #include <asm/debug.h>
18 #include <asm/ebcdic.h>
19 #include <asm/sysinfo.h>
20 #include "gaccess.h"
21 #include "kvm-s390.h"
23 static int handle_set_prefix(struct kvm_vcpu *vcpu)
25 int base2 = vcpu->arch.sie_block->ipb >> 28;
26 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
27 u64 operand2;
28 u32 address = 0;
29 u8 tmp;
31 vcpu->stat.instruction_spx++;
33 operand2 = disp2;
34 if (base2)
35 operand2 += vcpu->arch.guest_gprs[base2];
37 /* must be word boundary */
38 if (operand2 & 3) {
39 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
40 goto out;
43 /* get the value */
44 if (get_guest_u32(vcpu, operand2, &address)) {
45 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
46 goto out;
49 address = address & 0x7fffe000u;
51 /* make sure that the new value is valid memory */
52 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
53 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
54 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
55 goto out;
58 vcpu->arch.sie_block->prefix = address;
59 vcpu->arch.sie_block->ihcpu = 0xffff;
61 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
62 out:
63 return 0;
66 static int handle_store_prefix(struct kvm_vcpu *vcpu)
68 int base2 = vcpu->arch.sie_block->ipb >> 28;
69 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
70 u64 operand2;
71 u32 address;
73 vcpu->stat.instruction_stpx++;
74 operand2 = disp2;
75 if (base2)
76 operand2 += vcpu->arch.guest_gprs[base2];
78 /* must be word boundary */
79 if (operand2 & 3) {
80 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
81 goto out;
84 address = vcpu->arch.sie_block->prefix;
85 address = address & 0x7fffe000u;
87 /* get the value */
88 if (put_guest_u32(vcpu, operand2, address)) {
89 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
90 goto out;
93 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
94 out:
95 return 0;
98 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
100 int base2 = vcpu->arch.sie_block->ipb >> 28;
101 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
102 u64 useraddr;
103 int rc;
105 vcpu->stat.instruction_stap++;
106 useraddr = disp2;
107 if (base2)
108 useraddr += vcpu->arch.guest_gprs[base2];
110 if (useraddr & 1) {
111 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
112 goto out;
115 rc = put_guest_u16(vcpu, useraddr, vcpu->vcpu_id);
116 if (rc == -EFAULT) {
117 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
118 goto out;
121 VCPU_EVENT(vcpu, 5, "storing cpu address to %lx", useraddr);
122 out:
123 return 0;
126 static int handle_skey(struct kvm_vcpu *vcpu)
128 vcpu->stat.instruction_storage_key++;
129 vcpu->arch.sie_block->gpsw.addr -= 4;
130 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
131 return 0;
134 static int handle_stsch(struct kvm_vcpu *vcpu)
136 vcpu->stat.instruction_stsch++;
137 VCPU_EVENT(vcpu, 4, "%s", "store subchannel - CC3");
138 /* condition code 3 */
139 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
140 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
141 return 0;
144 static int handle_chsc(struct kvm_vcpu *vcpu)
146 vcpu->stat.instruction_chsc++;
147 VCPU_EVENT(vcpu, 4, "%s", "channel subsystem call - CC3");
148 /* condition code 3 */
149 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
150 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
151 return 0;
154 static int handle_stfl(struct kvm_vcpu *vcpu)
156 unsigned int facility_list = stfl();
157 int rc;
159 vcpu->stat.instruction_stfl++;
160 facility_list &= ~(1UL<<24); /* no stfle */
162 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
163 &facility_list, sizeof(facility_list));
164 if (rc == -EFAULT)
165 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
166 else
167 VCPU_EVENT(vcpu, 5, "store facility list value %x",
168 facility_list);
169 return 0;
172 static int handle_stidp(struct kvm_vcpu *vcpu)
174 int base2 = vcpu->arch.sie_block->ipb >> 28;
175 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
176 u64 operand2;
177 int rc;
179 vcpu->stat.instruction_stidp++;
180 operand2 = disp2;
181 if (base2)
182 operand2 += vcpu->arch.guest_gprs[base2];
184 if (operand2 & 7) {
185 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
186 goto out;
189 rc = put_guest_u64(vcpu, operand2, vcpu->arch.stidp_data);
190 if (rc == -EFAULT) {
191 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
192 goto out;
195 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
196 out:
197 return 0;
200 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
202 struct float_interrupt *fi = &vcpu->kvm->arch.float_int;
203 int cpus = 0;
204 int n;
206 spin_lock_bh(&fi->lock);
207 for (n = 0; n < KVM_MAX_VCPUS; n++)
208 if (fi->local_int[n])
209 cpus++;
210 spin_unlock_bh(&fi->lock);
212 /* deal with other level 3 hypervisors */
213 if (stsi(mem, 3, 2, 2) == -ENOSYS)
214 mem->count = 0;
215 if (mem->count < 8)
216 mem->count++;
217 for (n = mem->count - 1; n > 0 ; n--)
218 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
220 mem->vm[0].cpus_total = cpus;
221 mem->vm[0].cpus_configured = cpus;
222 mem->vm[0].cpus_standby = 0;
223 mem->vm[0].cpus_reserved = 0;
224 mem->vm[0].caf = 1000;
225 memcpy(mem->vm[0].name, "KVMguest", 8);
226 ASCEBC(mem->vm[0].name, 8);
227 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
228 ASCEBC(mem->vm[0].cpi, 16);
231 static int handle_stsi(struct kvm_vcpu *vcpu)
233 int fc = (vcpu->arch.guest_gprs[0] & 0xf0000000) >> 28;
234 int sel1 = vcpu->arch.guest_gprs[0] & 0xff;
235 int sel2 = vcpu->arch.guest_gprs[1] & 0xffff;
236 int base2 = vcpu->arch.sie_block->ipb >> 28;
237 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
238 u64 operand2;
239 unsigned long mem;
241 vcpu->stat.instruction_stsi++;
242 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
244 operand2 = disp2;
245 if (base2)
246 operand2 += vcpu->arch.guest_gprs[base2];
248 if (operand2 & 0xfff && fc > 0)
249 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
251 switch (fc) {
252 case 0:
253 vcpu->arch.guest_gprs[0] = 3 << 28;
254 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
255 return 0;
256 case 1: /* same handling for 1 and 2 */
257 case 2:
258 mem = get_zeroed_page(GFP_KERNEL);
259 if (!mem)
260 goto out_fail;
261 if (stsi((void *) mem, fc, sel1, sel2) == -ENOSYS)
262 goto out_mem;
263 break;
264 case 3:
265 if (sel1 != 2 || sel2 != 2)
266 goto out_fail;
267 mem = get_zeroed_page(GFP_KERNEL);
268 if (!mem)
269 goto out_fail;
270 handle_stsi_3_2_2(vcpu, (void *) mem);
271 break;
272 default:
273 goto out_fail;
276 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
277 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
278 goto out_mem;
280 free_page(mem);
281 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
282 vcpu->arch.guest_gprs[0] = 0;
283 return 0;
284 out_mem:
285 free_page(mem);
286 out_fail:
287 /* condition code 3 */
288 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
289 return 0;
292 static intercept_handler_t priv_handlers[256] = {
293 [0x02] = handle_stidp,
294 [0x10] = handle_set_prefix,
295 [0x11] = handle_store_prefix,
296 [0x12] = handle_store_cpu_address,
297 [0x29] = handle_skey,
298 [0x2a] = handle_skey,
299 [0x2b] = handle_skey,
300 [0x34] = handle_stsch,
301 [0x5f] = handle_chsc,
302 [0x7d] = handle_stsi,
303 [0xb1] = handle_stfl,
306 int kvm_s390_handle_priv(struct kvm_vcpu *vcpu)
308 intercept_handler_t handler;
310 handler = priv_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
311 if (handler)
312 return handler(vcpu);
313 return -ENOTSUPP;