]>
Commit | Line | Data |
---|---|---|
453423dc | 1 | /* |
a53c8fab | 2 | * handling privileged instructions |
453423dc CB |
3 | * |
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> | |
5a0e3ad6 | 15 | #include <linux/gfp.h> |
453423dc | 16 | #include <linux/errno.h> |
7c959e82 | 17 | #include <asm/asm-offsets.h> |
453423dc CB |
18 | #include <asm/current.h> |
19 | #include <asm/debug.h> | |
20 | #include <asm/ebcdic.h> | |
21 | #include <asm/sysinfo.h> | |
48a3e950 CH |
22 | #include <asm/ptrace.h> |
23 | #include <asm/compat.h> | |
453423dc CB |
24 | #include "gaccess.h" |
25 | #include "kvm-s390.h" | |
5786fffa | 26 | #include "trace.h" |
453423dc CB |
27 | |
28 | static int handle_set_prefix(struct kvm_vcpu *vcpu) | |
29 | { | |
453423dc CB |
30 | u64 operand2; |
31 | u32 address = 0; | |
32 | u8 tmp; | |
33 | ||
34 | vcpu->stat.instruction_spx++; | |
35 | ||
b1c571a5 | 36 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
453423dc CB |
37 | |
38 | /* must be word boundary */ | |
39 | if (operand2 & 3) { | |
40 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
41 | goto out; | |
42 | } | |
43 | ||
44 | /* get the value */ | |
0a75ca27 | 45 | if (get_guest(vcpu, address, (u32 __user *) operand2)) { |
453423dc CB |
46 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
47 | goto out; | |
48 | } | |
49 | ||
50 | address = address & 0x7fffe000u; | |
51 | ||
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; | |
57 | } | |
58 | ||
8d26cf7b | 59 | kvm_s390_set_prefix(vcpu, address); |
453423dc CB |
60 | |
61 | VCPU_EVENT(vcpu, 5, "setting prefix to %x", address); | |
5786fffa | 62 | trace_kvm_s390_handle_prefix(vcpu, 1, address); |
453423dc CB |
63 | out: |
64 | return 0; | |
65 | } | |
66 | ||
67 | static int handle_store_prefix(struct kvm_vcpu *vcpu) | |
68 | { | |
453423dc CB |
69 | u64 operand2; |
70 | u32 address; | |
71 | ||
72 | vcpu->stat.instruction_stpx++; | |
b1c571a5 CH |
73 | |
74 | operand2 = kvm_s390_get_base_disp_s(vcpu); | |
453423dc CB |
75 | |
76 | /* must be word boundary */ | |
77 | if (operand2 & 3) { | |
78 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
79 | goto out; | |
80 | } | |
81 | ||
82 | address = vcpu->arch.sie_block->prefix; | |
83 | address = address & 0x7fffe000u; | |
84 | ||
85 | /* get the value */ | |
0a75ca27 | 86 | if (put_guest(vcpu, address, (u32 __user *)operand2)) { |
453423dc CB |
87 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
88 | goto out; | |
89 | } | |
90 | ||
91 | VCPU_EVENT(vcpu, 5, "storing prefix to %x", address); | |
5786fffa | 92 | trace_kvm_s390_handle_prefix(vcpu, 0, address); |
453423dc CB |
93 | out: |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int handle_store_cpu_address(struct kvm_vcpu *vcpu) | |
98 | { | |
453423dc CB |
99 | u64 useraddr; |
100 | int rc; | |
101 | ||
102 | vcpu->stat.instruction_stap++; | |
b1c571a5 CH |
103 | |
104 | useraddr = kvm_s390_get_base_disp_s(vcpu); | |
453423dc CB |
105 | |
106 | if (useraddr & 1) { | |
107 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
108 | goto out; | |
109 | } | |
110 | ||
0a75ca27 | 111 | rc = put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr); |
dc5008b9 | 112 | if (rc) { |
453423dc CB |
113 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
114 | goto out; | |
115 | } | |
116 | ||
33e19115 | 117 | VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr); |
5786fffa | 118 | trace_kvm_s390_handle_stap(vcpu, useraddr); |
453423dc CB |
119 | out: |
120 | return 0; | |
121 | } | |
122 | ||
123 | static int handle_skey(struct kvm_vcpu *vcpu) | |
124 | { | |
125 | vcpu->stat.instruction_storage_key++; | |
126 | vcpu->arch.sie_block->gpsw.addr -= 4; | |
127 | VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation"); | |
128 | return 0; | |
129 | } | |
130 | ||
fa6b7fe9 | 131 | static int handle_tpi(struct kvm_vcpu *vcpu) |
453423dc | 132 | { |
fa6b7fe9 | 133 | struct kvm_s390_interrupt_info *inti; |
7c959e82 | 134 | u64 addr; |
fa6b7fe9 CH |
135 | int cc; |
136 | ||
137 | addr = kvm_s390_get_base_disp_s(vcpu); | |
7c959e82 HC |
138 | if (addr & 3) { |
139 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
140 | goto out; | |
141 | } | |
142 | cc = 0; | |
fa6b7fe9 | 143 | inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0); |
7c959e82 HC |
144 | if (!inti) |
145 | goto no_interrupt; | |
146 | cc = 1; | |
147 | if (addr) { | |
148 | /* | |
149 | * Store the two-word I/O interruption code into the | |
150 | * provided area. | |
151 | */ | |
0a75ca27 HC |
152 | put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) addr); |
153 | put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) (addr + 2)); | |
154 | put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) (addr + 4)); | |
7c959e82 HC |
155 | } else { |
156 | /* | |
157 | * Store the three-word I/O interruption code into | |
158 | * the appropriate lowcore area. | |
159 | */ | |
0a75ca27 HC |
160 | put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID); |
161 | put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR); | |
162 | put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM); | |
163 | put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD); | |
7c959e82 | 164 | } |
fa6b7fe9 | 165 | kfree(inti); |
7c959e82 | 166 | no_interrupt: |
fa6b7fe9 | 167 | /* Set condition code and we're done. */ |
453423dc | 168 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
fa6b7fe9 | 169 | vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44; |
7c959e82 | 170 | out: |
453423dc CB |
171 | return 0; |
172 | } | |
173 | ||
fa6b7fe9 CH |
174 | static int handle_tsch(struct kvm_vcpu *vcpu) |
175 | { | |
176 | struct kvm_s390_interrupt_info *inti; | |
177 | ||
178 | inti = kvm_s390_get_io_int(vcpu->kvm, 0, | |
179 | vcpu->run->s.regs.gprs[1]); | |
180 | ||
181 | /* | |
182 | * Prepare exit to userspace. | |
183 | * We indicate whether we dequeued a pending I/O interrupt | |
184 | * so that userspace can re-inject it if the instruction gets | |
185 | * a program check. While this may re-order the pending I/O | |
186 | * interrupts, this is no problem since the priority is kept | |
187 | * intact. | |
188 | */ | |
189 | vcpu->run->exit_reason = KVM_EXIT_S390_TSCH; | |
190 | vcpu->run->s390_tsch.dequeued = !!inti; | |
191 | if (inti) { | |
192 | vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id; | |
193 | vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr; | |
194 | vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm; | |
195 | vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word; | |
196 | } | |
197 | vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb; | |
198 | kfree(inti); | |
199 | return -EREMOTE; | |
200 | } | |
201 | ||
202 | static int handle_io_inst(struct kvm_vcpu *vcpu) | |
203 | { | |
204 | VCPU_EVENT(vcpu, 4, "%s", "I/O instruction"); | |
205 | ||
206 | if (vcpu->kvm->arch.css_support) { | |
207 | /* | |
208 | * Most I/O instructions will be handled by userspace. | |
209 | * Exceptions are tpi and the interrupt portion of tsch. | |
210 | */ | |
211 | if (vcpu->arch.sie_block->ipa == 0xb236) | |
212 | return handle_tpi(vcpu); | |
213 | if (vcpu->arch.sie_block->ipa == 0xb235) | |
214 | return handle_tsch(vcpu); | |
215 | /* Handle in userspace. */ | |
216 | return -EOPNOTSUPP; | |
217 | } else { | |
218 | /* | |
219 | * Set condition code 3 to stop the guest from issueing channel | |
220 | * I/O instructions. | |
221 | */ | |
222 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); | |
223 | vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44; | |
224 | return 0; | |
225 | } | |
226 | } | |
227 | ||
453423dc CB |
228 | static int handle_stfl(struct kvm_vcpu *vcpu) |
229 | { | |
14375bc4 | 230 | unsigned int facility_list; |
453423dc CB |
231 | int rc; |
232 | ||
233 | vcpu->stat.instruction_stfl++; | |
a0046b6d | 234 | /* only pass the facility bits, which we can handle */ |
14375bc4 | 235 | facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3; |
453423dc CB |
236 | |
237 | rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list), | |
238 | &facility_list, sizeof(facility_list)); | |
dc5008b9 | 239 | if (rc) |
453423dc | 240 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
5786fffa | 241 | else { |
453423dc CB |
242 | VCPU_EVENT(vcpu, 5, "store facility list value %x", |
243 | facility_list); | |
5786fffa CH |
244 | trace_kvm_s390_handle_stfl(vcpu, facility_list); |
245 | } | |
453423dc CB |
246 | return 0; |
247 | } | |
248 | ||
48a3e950 CH |
249 | static void handle_new_psw(struct kvm_vcpu *vcpu) |
250 | { | |
251 | /* Check whether the new psw is enabled for machine checks. */ | |
252 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK) | |
253 | kvm_s390_deliver_pending_machine_checks(vcpu); | |
254 | } | |
255 | ||
256 | #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA) | |
257 | #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL | |
d21683ea | 258 | #define PSW_ADDR_24 0x0000000000ffffffUL |
48a3e950 CH |
259 | #define PSW_ADDR_31 0x000000007fffffffUL |
260 | ||
261 | int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) | |
262 | { | |
263 | u64 addr; | |
264 | psw_compat_t new_psw; | |
265 | ||
266 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
267 | return kvm_s390_inject_program_int(vcpu, | |
268 | PGM_PRIVILEGED_OPERATION); | |
269 | ||
270 | addr = kvm_s390_get_base_disp_s(vcpu); | |
271 | ||
272 | if (addr & 7) { | |
273 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
274 | goto out; | |
275 | } | |
276 | ||
277 | if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw))) { | |
278 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
279 | goto out; | |
280 | } | |
281 | ||
282 | if (!(new_psw.mask & PSW32_MASK_BASE)) { | |
283 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
284 | goto out; | |
285 | } | |
286 | ||
287 | vcpu->arch.sie_block->gpsw.mask = | |
288 | (new_psw.mask & ~PSW32_MASK_BASE) << 32; | |
ace50587 HC |
289 | vcpu->arch.sie_block->gpsw.mask |= new_psw.addr & PSW32_ADDR_AMODE; |
290 | vcpu->arch.sie_block->gpsw.addr = new_psw.addr & ~PSW32_ADDR_AMODE; | |
48a3e950 CH |
291 | |
292 | if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_UNASSIGNED) || | |
293 | (!(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) && | |
294 | (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_24)) || | |
295 | ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) == | |
296 | PSW_MASK_EA)) { | |
297 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
298 | goto out; | |
299 | } | |
300 | ||
301 | handle_new_psw(vcpu); | |
302 | out: | |
303 | return 0; | |
304 | } | |
305 | ||
306 | static int handle_lpswe(struct kvm_vcpu *vcpu) | |
307 | { | |
308 | u64 addr; | |
309 | psw_t new_psw; | |
310 | ||
311 | addr = kvm_s390_get_base_disp_s(vcpu); | |
312 | ||
313 | if (addr & 7) { | |
314 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
315 | goto out; | |
316 | } | |
317 | ||
318 | if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw))) { | |
319 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
320 | goto out; | |
321 | } | |
322 | ||
323 | vcpu->arch.sie_block->gpsw.mask = new_psw.mask; | |
324 | vcpu->arch.sie_block->gpsw.addr = new_psw.addr; | |
325 | ||
326 | if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_UNASSIGNED) || | |
327 | (((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) == | |
328 | PSW_MASK_BA) && | |
329 | (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_31)) || | |
330 | (!(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) && | |
331 | (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_24)) || | |
332 | ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) == | |
333 | PSW_MASK_EA)) { | |
334 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
335 | goto out; | |
336 | } | |
337 | ||
338 | handle_new_psw(vcpu); | |
339 | out: | |
340 | return 0; | |
341 | } | |
342 | ||
453423dc CB |
343 | static int handle_stidp(struct kvm_vcpu *vcpu) |
344 | { | |
453423dc CB |
345 | u64 operand2; |
346 | int rc; | |
347 | ||
348 | vcpu->stat.instruction_stidp++; | |
b1c571a5 CH |
349 | |
350 | operand2 = kvm_s390_get_base_disp_s(vcpu); | |
453423dc CB |
351 | |
352 | if (operand2 & 7) { | |
353 | kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
354 | goto out; | |
355 | } | |
356 | ||
0a75ca27 | 357 | rc = put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2); |
dc5008b9 | 358 | if (rc) { |
453423dc CB |
359 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
360 | goto out; | |
361 | } | |
362 | ||
363 | VCPU_EVENT(vcpu, 5, "%s", "store cpu id"); | |
364 | out: | |
365 | return 0; | |
366 | } | |
367 | ||
368 | static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem) | |
369 | { | |
180c12fb | 370 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
453423dc CB |
371 | int cpus = 0; |
372 | int n; | |
373 | ||
b037a4f3 | 374 | spin_lock(&fi->lock); |
453423dc CB |
375 | for (n = 0; n < KVM_MAX_VCPUS; n++) |
376 | if (fi->local_int[n]) | |
377 | cpus++; | |
b037a4f3 | 378 | spin_unlock(&fi->lock); |
453423dc CB |
379 | |
380 | /* deal with other level 3 hypervisors */ | |
caf757c6 | 381 | if (stsi(mem, 3, 2, 2)) |
453423dc CB |
382 | mem->count = 0; |
383 | if (mem->count < 8) | |
384 | mem->count++; | |
385 | for (n = mem->count - 1; n > 0 ; n--) | |
386 | memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0])); | |
387 | ||
388 | mem->vm[0].cpus_total = cpus; | |
389 | mem->vm[0].cpus_configured = cpus; | |
390 | mem->vm[0].cpus_standby = 0; | |
391 | mem->vm[0].cpus_reserved = 0; | |
392 | mem->vm[0].caf = 1000; | |
393 | memcpy(mem->vm[0].name, "KVMguest", 8); | |
394 | ASCEBC(mem->vm[0].name, 8); | |
395 | memcpy(mem->vm[0].cpi, "KVM/Linux ", 16); | |
396 | ASCEBC(mem->vm[0].cpi, 16); | |
397 | } | |
398 | ||
399 | static int handle_stsi(struct kvm_vcpu *vcpu) | |
400 | { | |
5a32c1af CB |
401 | int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28; |
402 | int sel1 = vcpu->run->s.regs.gprs[0] & 0xff; | |
403 | int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff; | |
453423dc CB |
404 | u64 operand2; |
405 | unsigned long mem; | |
406 | ||
407 | vcpu->stat.instruction_stsi++; | |
408 | VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2); | |
409 | ||
b1c571a5 | 410 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
453423dc CB |
411 | |
412 | if (operand2 & 0xfff && fc > 0) | |
413 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
414 | ||
415 | switch (fc) { | |
416 | case 0: | |
5a32c1af | 417 | vcpu->run->s.regs.gprs[0] = 3 << 28; |
453423dc CB |
418 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
419 | return 0; | |
420 | case 1: /* same handling for 1 and 2 */ | |
421 | case 2: | |
422 | mem = get_zeroed_page(GFP_KERNEL); | |
423 | if (!mem) | |
424 | goto out_fail; | |
caf757c6 | 425 | if (stsi((void *) mem, fc, sel1, sel2)) |
453423dc CB |
426 | goto out_mem; |
427 | break; | |
428 | case 3: | |
429 | if (sel1 != 2 || sel2 != 2) | |
430 | goto out_fail; | |
431 | mem = get_zeroed_page(GFP_KERNEL); | |
432 | if (!mem) | |
433 | goto out_fail; | |
434 | handle_stsi_3_2_2(vcpu, (void *) mem); | |
435 | break; | |
436 | default: | |
437 | goto out_fail; | |
438 | } | |
439 | ||
440 | if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) { | |
441 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
442 | goto out_mem; | |
443 | } | |
5786fffa | 444 | trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2); |
453423dc CB |
445 | free_page(mem); |
446 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); | |
5a32c1af | 447 | vcpu->run->s.regs.gprs[0] = 0; |
453423dc CB |
448 | return 0; |
449 | out_mem: | |
450 | free_page(mem); | |
451 | out_fail: | |
452 | /* condition code 3 */ | |
453 | vcpu->arch.sie_block->gpsw.mask |= 3ul << 44; | |
454 | return 0; | |
455 | } | |
456 | ||
f379aae5 | 457 | static const intercept_handler_t b2_handlers[256] = { |
453423dc CB |
458 | [0x02] = handle_stidp, |
459 | [0x10] = handle_set_prefix, | |
460 | [0x11] = handle_store_prefix, | |
461 | [0x12] = handle_store_cpu_address, | |
462 | [0x29] = handle_skey, | |
463 | [0x2a] = handle_skey, | |
464 | [0x2b] = handle_skey, | |
f379aae5 CH |
465 | [0x30] = handle_io_inst, |
466 | [0x31] = handle_io_inst, | |
467 | [0x32] = handle_io_inst, | |
468 | [0x33] = handle_io_inst, | |
469 | [0x34] = handle_io_inst, | |
470 | [0x35] = handle_io_inst, | |
471 | [0x36] = handle_io_inst, | |
472 | [0x37] = handle_io_inst, | |
473 | [0x38] = handle_io_inst, | |
474 | [0x39] = handle_io_inst, | |
475 | [0x3a] = handle_io_inst, | |
476 | [0x3b] = handle_io_inst, | |
477 | [0x3c] = handle_io_inst, | |
478 | [0x5f] = handle_io_inst, | |
479 | [0x74] = handle_io_inst, | |
480 | [0x76] = handle_io_inst, | |
453423dc CB |
481 | [0x7d] = handle_stsi, |
482 | [0xb1] = handle_stfl, | |
48a3e950 | 483 | [0xb2] = handle_lpswe, |
453423dc CB |
484 | }; |
485 | ||
70455a36 | 486 | int kvm_s390_handle_b2(struct kvm_vcpu *vcpu) |
453423dc CB |
487 | { |
488 | intercept_handler_t handler; | |
489 | ||
70455a36 CB |
490 | /* |
491 | * a lot of B2 instructions are priviledged. We first check for | |
25985edc | 492 | * the privileged ones, that we can handle in the kernel. If the |
70455a36 CB |
493 | * kernel can handle this instruction, we check for the problem |
494 | * state bit and (a) handle the instruction or (b) send a code 2 | |
495 | * program check. | |
496 | * Anything else goes to userspace.*/ | |
f379aae5 | 497 | handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; |
70455a36 CB |
498 | if (handler) { |
499 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
500 | return kvm_s390_inject_program_int(vcpu, | |
501 | PGM_PRIVILEGED_OPERATION); | |
502 | else | |
503 | return handler(vcpu); | |
504 | } | |
b8e660b8 | 505 | return -EOPNOTSUPP; |
453423dc | 506 | } |
bb25b9ba | 507 | |
48a3e950 CH |
508 | static int handle_epsw(struct kvm_vcpu *vcpu) |
509 | { | |
510 | int reg1, reg2; | |
511 | ||
512 | reg1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 24; | |
513 | reg2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16; | |
514 | ||
515 | /* This basically extracts the mask half of the psw. */ | |
516 | vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000; | |
517 | vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32; | |
518 | if (reg2) { | |
519 | vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000; | |
520 | vcpu->run->s.regs.gprs[reg2] |= | |
521 | vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff; | |
522 | } | |
523 | return 0; | |
524 | } | |
525 | ||
526 | static const intercept_handler_t b9_handlers[256] = { | |
527 | [0x8d] = handle_epsw, | |
f379aae5 | 528 | [0x9c] = handle_io_inst, |
48a3e950 CH |
529 | }; |
530 | ||
531 | int kvm_s390_handle_b9(struct kvm_vcpu *vcpu) | |
532 | { | |
533 | intercept_handler_t handler; | |
534 | ||
535 | /* This is handled just as for the B2 instructions. */ | |
536 | handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; | |
537 | if (handler) { | |
538 | if ((handler != handle_epsw) && | |
539 | (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)) | |
540 | return kvm_s390_inject_program_int(vcpu, | |
541 | PGM_PRIVILEGED_OPERATION); | |
542 | else | |
543 | return handler(vcpu); | |
544 | } | |
545 | return -EOPNOTSUPP; | |
546 | } | |
547 | ||
f379aae5 CH |
548 | static const intercept_handler_t eb_handlers[256] = { |
549 | [0x8a] = handle_io_inst, | |
550 | }; | |
551 | ||
552 | int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu) | |
553 | { | |
554 | intercept_handler_t handler; | |
555 | ||
556 | /* All eb instructions that end up here are privileged. */ | |
557 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
558 | return kvm_s390_inject_program_int(vcpu, | |
559 | PGM_PRIVILEGED_OPERATION); | |
560 | handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff]; | |
561 | if (handler) | |
562 | return handler(vcpu); | |
563 | return -EOPNOTSUPP; | |
564 | } | |
565 | ||
bb25b9ba CB |
566 | static int handle_tprot(struct kvm_vcpu *vcpu) |
567 | { | |
b1c571a5 | 568 | u64 address1, address2; |
bb25b9ba | 569 | struct vm_area_struct *vma; |
1eddb85f | 570 | unsigned long user_address; |
bb25b9ba CB |
571 | |
572 | vcpu->stat.instruction_tprot++; | |
573 | ||
b1c571a5 CH |
574 | kvm_s390_get_base_disp_sse(vcpu, &address1, &address2); |
575 | ||
bb25b9ba CB |
576 | /* we only handle the Linux memory detection case: |
577 | * access key == 0 | |
578 | * guest DAT == off | |
579 | * everything else goes to userspace. */ | |
580 | if (address2 & 0xf0) | |
581 | return -EOPNOTSUPP; | |
582 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT) | |
583 | return -EOPNOTSUPP; | |
584 | ||
bb25b9ba | 585 | down_read(¤t->mm->mmap_sem); |
59a1fa2d HC |
586 | user_address = __gmap_translate(address1, vcpu->arch.gmap); |
587 | if (IS_ERR_VALUE(user_address)) | |
588 | goto out_inject; | |
1eddb85f | 589 | vma = find_vma(current->mm, user_address); |
59a1fa2d HC |
590 | if (!vma) |
591 | goto out_inject; | |
bb25b9ba CB |
592 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
593 | if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ)) | |
594 | vcpu->arch.sie_block->gpsw.mask |= (1ul << 44); | |
595 | if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ)) | |
596 | vcpu->arch.sie_block->gpsw.mask |= (2ul << 44); | |
597 | ||
598 | up_read(¤t->mm->mmap_sem); | |
599 | return 0; | |
59a1fa2d HC |
600 | |
601 | out_inject: | |
602 | up_read(¤t->mm->mmap_sem); | |
603 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
bb25b9ba CB |
604 | } |
605 | ||
606 | int kvm_s390_handle_e5(struct kvm_vcpu *vcpu) | |
607 | { | |
608 | /* For e5xx... instructions we only handle TPROT */ | |
609 | if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01) | |
610 | return handle_tprot(vcpu); | |
611 | return -EOPNOTSUPP; | |
612 | } | |
613 | ||
8c3f61e2 CH |
614 | static int handle_sckpf(struct kvm_vcpu *vcpu) |
615 | { | |
616 | u32 value; | |
617 | ||
618 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
619 | return kvm_s390_inject_program_int(vcpu, | |
620 | PGM_PRIVILEGED_OPERATION); | |
621 | ||
622 | if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000) | |
623 | return kvm_s390_inject_program_int(vcpu, | |
624 | PGM_SPECIFICATION); | |
625 | ||
626 | value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff; | |
627 | vcpu->arch.sie_block->todpr = value; | |
628 | ||
629 | return 0; | |
630 | } | |
631 | ||
77975357 | 632 | static const intercept_handler_t x01_handlers[256] = { |
8c3f61e2 CH |
633 | [0x07] = handle_sckpf, |
634 | }; | |
635 | ||
636 | int kvm_s390_handle_01(struct kvm_vcpu *vcpu) | |
637 | { | |
638 | intercept_handler_t handler; | |
639 | ||
640 | handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; | |
641 | if (handler) | |
642 | return handler(vcpu); | |
643 | return -EOPNOTSUPP; | |
644 | } |