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>
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);
32 vcpu
->stat
.instruction_spx
++;
36 operand2
+= vcpu
->arch
.guest_gprs
[base2
];
38 /* must be word boundary */
40 kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
45 if (get_guest_u32(vcpu
, operand2
, &address
)) {
46 kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
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
);
59 vcpu
->arch
.sie_block
->prefix
= address
;
60 vcpu
->arch
.sie_block
->ihcpu
= 0xffff;
62 VCPU_EVENT(vcpu
, 5, "setting prefix to %x", address
);
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);
74 vcpu
->stat
.instruction_stpx
++;
77 operand2
+= vcpu
->arch
.guest_gprs
[base2
];
79 /* must be word boundary */
81 kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
85 address
= vcpu
->arch
.sie_block
->prefix
;
86 address
= address
& 0x7fffe000u
;
89 if (put_guest_u32(vcpu
, operand2
, address
)) {
90 kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
94 VCPU_EVENT(vcpu
, 5, "storing prefix to %x", address
);
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);
106 vcpu
->stat
.instruction_stap
++;
109 useraddr
+= vcpu
->arch
.guest_gprs
[base2
];
112 kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
116 rc
= put_guest_u16(vcpu
, useraddr
, vcpu
->vcpu_id
);
118 kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
122 VCPU_EVENT(vcpu
, 5, "storing cpu address to %llx", useraddr
);
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");
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;
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;
155 static int handle_stfl(struct kvm_vcpu
*vcpu
)
157 unsigned int facility_list
;
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
));
167 kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
169 VCPU_EVENT(vcpu
, 5, "store facility list value %x",
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);
181 vcpu
->stat
.instruction_stidp
++;
184 operand2
+= vcpu
->arch
.guest_gprs
[base2
];
187 kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
191 rc
= put_guest_u64(vcpu
, operand2
, vcpu
->arch
.stidp_data
);
193 kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
197 VCPU_EVENT(vcpu
, 5, "%s", "store cpu id");
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
;
208 spin_lock(&fi
->lock
);
209 for (n
= 0; n
< KVM_MAX_VCPUS
; n
++)
210 if (fi
->local_int
[n
])
212 spin_unlock(&fi
->lock
);
214 /* deal with other level 3 hypervisors */
215 if (stsi(mem
, 3, 2, 2) == -ENOSYS
)
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);
243 vcpu
->stat
.instruction_stsi
++;
244 VCPU_EVENT(vcpu
, 4, "stsi: fc: %x sel1: %x sel2: %x", fc
, sel1
, sel2
);
248 operand2
+= vcpu
->arch
.guest_gprs
[base2
];
250 if (operand2
& 0xfff && fc
> 0)
251 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
255 vcpu
->arch
.guest_gprs
[0] = 3 << 28;
256 vcpu
->arch
.sie_block
->gpsw
.mask
&= ~(3ul << 44);
258 case 1: /* same handling for 1 and 2 */
260 mem
= get_zeroed_page(GFP_KERNEL
);
263 if (stsi((void *) mem
, fc
, sel1
, sel2
) == -ENOSYS
)
267 if (sel1
!= 2 || sel2
!= 2)
269 mem
= get_zeroed_page(GFP_KERNEL
);
272 handle_stsi_3_2_2(vcpu
, (void *) mem
);
278 if (copy_to_guest_absolute(vcpu
, operand2
, (void *) mem
, PAGE_SIZE
)) {
279 kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
283 vcpu
->arch
.sie_block
->gpsw
.mask
&= ~(3ul << 44);
284 vcpu
->arch
.guest_gprs
[0] = 0;
289 /* condition code 3 */
290 vcpu
->arch
.sie_block
->gpsw
.mask
|= 3ul << 44;
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
318 * Anything else goes to userspace.*/
319 handler
= priv_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
321 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
322 return kvm_s390_inject_program_int(vcpu
,
323 PGM_PRIVILEGED_OPERATION
);
325 return handler(vcpu
);