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