Commit | Line | Data |
---|---|---|
453423dc | 1 | /* |
a53c8fab | 2 | * handling privileged instructions |
453423dc | 3 | * |
69d0d3a3 | 4 | * Copyright IBM Corp. 2008, 2013 |
453423dc CB |
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> |
e769ece3 | 19 | #include <asm/facility.h> |
453423dc CB |
20 | #include <asm/current.h> |
21 | #include <asm/debug.h> | |
22 | #include <asm/ebcdic.h> | |
23 | #include <asm/sysinfo.h> | |
69d0d3a3 CB |
24 | #include <asm/pgtable.h> |
25 | #include <asm/pgalloc.h> | |
26 | #include <asm/io.h> | |
48a3e950 CH |
27 | #include <asm/ptrace.h> |
28 | #include <asm/compat.h> | |
453423dc CB |
29 | #include "gaccess.h" |
30 | #include "kvm-s390.h" | |
5786fffa | 31 | #include "trace.h" |
453423dc CB |
32 | |
33 | static int handle_set_prefix(struct kvm_vcpu *vcpu) | |
34 | { | |
453423dc CB |
35 | u64 operand2; |
36 | u32 address = 0; | |
37 | u8 tmp; | |
38 | ||
39 | vcpu->stat.instruction_spx++; | |
40 | ||
5087dfa6 TH |
41 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
42 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
43 | ||
b1c571a5 | 44 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
453423dc CB |
45 | |
46 | /* must be word boundary */ | |
db4a29cb HC |
47 | if (operand2 & 3) |
48 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
453423dc CB |
49 | |
50 | /* get the value */ | |
db4a29cb HC |
51 | if (get_guest(vcpu, address, (u32 __user *) operand2)) |
52 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
453423dc CB |
53 | |
54 | address = address & 0x7fffe000u; | |
55 | ||
56 | /* make sure that the new value is valid memory */ | |
57 | if (copy_from_guest_absolute(vcpu, &tmp, address, 1) || | |
db4a29cb HC |
58 | (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) |
59 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
453423dc | 60 | |
8d26cf7b | 61 | kvm_s390_set_prefix(vcpu, address); |
453423dc CB |
62 | |
63 | VCPU_EVENT(vcpu, 5, "setting prefix to %x", address); | |
5786fffa | 64 | trace_kvm_s390_handle_prefix(vcpu, 1, address); |
453423dc CB |
65 | return 0; |
66 | } | |
67 | ||
68 | static int handle_store_prefix(struct kvm_vcpu *vcpu) | |
69 | { | |
453423dc CB |
70 | u64 operand2; |
71 | u32 address; | |
72 | ||
73 | vcpu->stat.instruction_stpx++; | |
b1c571a5 | 74 | |
5087dfa6 TH |
75 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
76 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
77 | ||
b1c571a5 | 78 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
453423dc CB |
79 | |
80 | /* must be word boundary */ | |
db4a29cb HC |
81 | if (operand2 & 3) |
82 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
453423dc CB |
83 | |
84 | address = vcpu->arch.sie_block->prefix; | |
85 | address = address & 0x7fffe000u; | |
86 | ||
87 | /* get the value */ | |
db4a29cb HC |
88 | if (put_guest(vcpu, address, (u32 __user *)operand2)) |
89 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
453423dc CB |
90 | |
91 | VCPU_EVENT(vcpu, 5, "storing prefix to %x", address); | |
5786fffa | 92 | trace_kvm_s390_handle_prefix(vcpu, 0, address); |
453423dc CB |
93 | return 0; |
94 | } | |
95 | ||
96 | static int handle_store_cpu_address(struct kvm_vcpu *vcpu) | |
97 | { | |
453423dc | 98 | u64 useraddr; |
453423dc CB |
99 | |
100 | vcpu->stat.instruction_stap++; | |
b1c571a5 | 101 | |
5087dfa6 TH |
102 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
103 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
104 | ||
b1c571a5 | 105 | useraddr = kvm_s390_get_base_disp_s(vcpu); |
453423dc | 106 | |
db4a29cb HC |
107 | if (useraddr & 1) |
108 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
453423dc | 109 | |
db4a29cb HC |
110 | if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr)) |
111 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
453423dc | 112 | |
33e19115 | 113 | VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr); |
5786fffa | 114 | trace_kvm_s390_handle_stap(vcpu, useraddr); |
453423dc CB |
115 | return 0; |
116 | } | |
117 | ||
118 | static int handle_skey(struct kvm_vcpu *vcpu) | |
119 | { | |
120 | vcpu->stat.instruction_storage_key++; | |
5087dfa6 TH |
121 | |
122 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
123 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
124 | ||
dfcf7dc6 MS |
125 | vcpu->arch.sie_block->gpsw.addr = |
126 | __rewind_psw(vcpu->arch.sie_block->gpsw, 4); | |
453423dc CB |
127 | VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation"); |
128 | return 0; | |
129 | } | |
130 | ||
aca84241 TH |
131 | static int handle_test_block(struct kvm_vcpu *vcpu) |
132 | { | |
133 | unsigned long hva; | |
134 | gpa_t addr; | |
135 | int reg2; | |
136 | ||
137 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
138 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
139 | ||
140 | kvm_s390_get_regs_rre(vcpu, NULL, ®2); | |
141 | addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; | |
142 | addr = kvm_s390_real_to_abs(vcpu, addr); | |
143 | ||
144 | hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr)); | |
145 | if (kvm_is_error_hva(hva)) | |
146 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
147 | /* | |
148 | * We don't expect errors on modern systems, and do not care | |
149 | * about storage keys (yet), so let's just clear the page. | |
150 | */ | |
151 | if (clear_user((void __user *)hva, PAGE_SIZE) != 0) | |
152 | return -EFAULT; | |
153 | kvm_s390_set_psw_cc(vcpu, 0); | |
154 | vcpu->run->s.regs.gprs[0] = 0; | |
155 | return 0; | |
156 | } | |
157 | ||
fa6b7fe9 | 158 | static int handle_tpi(struct kvm_vcpu *vcpu) |
453423dc | 159 | { |
fa6b7fe9 | 160 | struct kvm_s390_interrupt_info *inti; |
7c959e82 | 161 | u64 addr; |
fa6b7fe9 CH |
162 | int cc; |
163 | ||
164 | addr = kvm_s390_get_base_disp_s(vcpu); | |
db4a29cb HC |
165 | if (addr & 3) |
166 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
7c959e82 | 167 | cc = 0; |
fa6b7fe9 | 168 | inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0); |
7c959e82 HC |
169 | if (!inti) |
170 | goto no_interrupt; | |
171 | cc = 1; | |
172 | if (addr) { | |
173 | /* | |
174 | * Store the two-word I/O interruption code into the | |
175 | * provided area. | |
176 | */ | |
133608f3 TH |
177 | if (put_guest(vcpu, inti->io.subchannel_id, (u16 __user *)addr) |
178 | || put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *)(addr + 2)) | |
179 | || put_guest(vcpu, inti->io.io_int_parm, (u32 __user *)(addr + 4))) | |
180 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
7c959e82 HC |
181 | } else { |
182 | /* | |
183 | * Store the three-word I/O interruption code into | |
184 | * the appropriate lowcore area. | |
185 | */ | |
0a75ca27 HC |
186 | put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID); |
187 | put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR); | |
188 | put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM); | |
189 | put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD); | |
7c959e82 | 190 | } |
fa6b7fe9 | 191 | kfree(inti); |
7c959e82 | 192 | no_interrupt: |
fa6b7fe9 | 193 | /* Set condition code and we're done. */ |
ea828ebf | 194 | kvm_s390_set_psw_cc(vcpu, cc); |
453423dc CB |
195 | return 0; |
196 | } | |
197 | ||
fa6b7fe9 CH |
198 | static int handle_tsch(struct kvm_vcpu *vcpu) |
199 | { | |
200 | struct kvm_s390_interrupt_info *inti; | |
201 | ||
202 | inti = kvm_s390_get_io_int(vcpu->kvm, 0, | |
203 | vcpu->run->s.regs.gprs[1]); | |
204 | ||
205 | /* | |
206 | * Prepare exit to userspace. | |
207 | * We indicate whether we dequeued a pending I/O interrupt | |
208 | * so that userspace can re-inject it if the instruction gets | |
209 | * a program check. While this may re-order the pending I/O | |
210 | * interrupts, this is no problem since the priority is kept | |
211 | * intact. | |
212 | */ | |
213 | vcpu->run->exit_reason = KVM_EXIT_S390_TSCH; | |
214 | vcpu->run->s390_tsch.dequeued = !!inti; | |
215 | if (inti) { | |
216 | vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id; | |
217 | vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr; | |
218 | vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm; | |
219 | vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word; | |
220 | } | |
221 | vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb; | |
222 | kfree(inti); | |
223 | return -EREMOTE; | |
224 | } | |
225 | ||
226 | static int handle_io_inst(struct kvm_vcpu *vcpu) | |
227 | { | |
228 | VCPU_EVENT(vcpu, 4, "%s", "I/O instruction"); | |
229 | ||
5087dfa6 TH |
230 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
231 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
232 | ||
fa6b7fe9 CH |
233 | if (vcpu->kvm->arch.css_support) { |
234 | /* | |
235 | * Most I/O instructions will be handled by userspace. | |
236 | * Exceptions are tpi and the interrupt portion of tsch. | |
237 | */ | |
238 | if (vcpu->arch.sie_block->ipa == 0xb236) | |
239 | return handle_tpi(vcpu); | |
240 | if (vcpu->arch.sie_block->ipa == 0xb235) | |
241 | return handle_tsch(vcpu); | |
242 | /* Handle in userspace. */ | |
243 | return -EOPNOTSUPP; | |
244 | } else { | |
245 | /* | |
246 | * Set condition code 3 to stop the guest from issueing channel | |
247 | * I/O instructions. | |
248 | */ | |
ea828ebf | 249 | kvm_s390_set_psw_cc(vcpu, 3); |
fa6b7fe9 CH |
250 | return 0; |
251 | } | |
252 | } | |
253 | ||
453423dc CB |
254 | static int handle_stfl(struct kvm_vcpu *vcpu) |
255 | { | |
453423dc CB |
256 | int rc; |
257 | ||
258 | vcpu->stat.instruction_stfl++; | |
5087dfa6 TH |
259 | |
260 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
261 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
262 | ||
453423dc | 263 | rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list), |
78c4b59f | 264 | vfacilities, 4); |
dc5008b9 | 265 | if (rc) |
db4a29cb | 266 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
78c4b59f MM |
267 | VCPU_EVENT(vcpu, 5, "store facility list value %x", |
268 | *(unsigned int *) vfacilities); | |
269 | trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities); | |
453423dc CB |
270 | return 0; |
271 | } | |
272 | ||
48a3e950 CH |
273 | static void handle_new_psw(struct kvm_vcpu *vcpu) |
274 | { | |
275 | /* Check whether the new psw is enabled for machine checks. */ | |
276 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK) | |
277 | kvm_s390_deliver_pending_machine_checks(vcpu); | |
278 | } | |
279 | ||
280 | #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA) | |
281 | #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL | |
d21683ea | 282 | #define PSW_ADDR_24 0x0000000000ffffffUL |
48a3e950 CH |
283 | #define PSW_ADDR_31 0x000000007fffffffUL |
284 | ||
3736b874 HC |
285 | static int is_valid_psw(psw_t *psw) { |
286 | if (psw->mask & PSW_MASK_UNASSIGNED) | |
287 | return 0; | |
288 | if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) { | |
289 | if (psw->addr & ~PSW_ADDR_31) | |
290 | return 0; | |
291 | } | |
292 | if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24)) | |
293 | return 0; | |
294 | if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA) | |
295 | return 0; | |
296 | return 1; | |
297 | } | |
298 | ||
48a3e950 CH |
299 | int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) |
300 | { | |
3736b874 | 301 | psw_t *gpsw = &vcpu->arch.sie_block->gpsw; |
48a3e950 | 302 | psw_compat_t new_psw; |
3736b874 | 303 | u64 addr; |
48a3e950 | 304 | |
3736b874 | 305 | if (gpsw->mask & PSW_MASK_PSTATE) |
208dd756 TH |
306 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
307 | ||
48a3e950 | 308 | addr = kvm_s390_get_base_disp_s(vcpu); |
6fd0fcc9 HC |
309 | if (addr & 7) |
310 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
6fd0fcc9 HC |
311 | if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw))) |
312 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
6fd0fcc9 HC |
313 | if (!(new_psw.mask & PSW32_MASK_BASE)) |
314 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
3736b874 HC |
315 | gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32; |
316 | gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE; | |
317 | gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE; | |
318 | if (!is_valid_psw(gpsw)) | |
6fd0fcc9 | 319 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
48a3e950 | 320 | handle_new_psw(vcpu); |
48a3e950 CH |
321 | return 0; |
322 | } | |
323 | ||
324 | static int handle_lpswe(struct kvm_vcpu *vcpu) | |
325 | { | |
48a3e950 | 326 | psw_t new_psw; |
3736b874 | 327 | u64 addr; |
48a3e950 | 328 | |
5087dfa6 TH |
329 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
330 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
331 | ||
48a3e950 | 332 | addr = kvm_s390_get_base_disp_s(vcpu); |
6fd0fcc9 HC |
333 | if (addr & 7) |
334 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
6fd0fcc9 HC |
335 | if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw))) |
336 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
3736b874 HC |
337 | vcpu->arch.sie_block->gpsw = new_psw; |
338 | if (!is_valid_psw(&vcpu->arch.sie_block->gpsw)) | |
6fd0fcc9 | 339 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
48a3e950 | 340 | handle_new_psw(vcpu); |
48a3e950 CH |
341 | return 0; |
342 | } | |
343 | ||
453423dc CB |
344 | static int handle_stidp(struct kvm_vcpu *vcpu) |
345 | { | |
453423dc | 346 | u64 operand2; |
453423dc CB |
347 | |
348 | vcpu->stat.instruction_stidp++; | |
b1c571a5 | 349 | |
5087dfa6 TH |
350 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
351 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
352 | ||
b1c571a5 | 353 | operand2 = kvm_s390_get_base_disp_s(vcpu); |
453423dc | 354 | |
db4a29cb HC |
355 | if (operand2 & 7) |
356 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
453423dc | 357 | |
db4a29cb HC |
358 | if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2)) |
359 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
453423dc CB |
360 | |
361 | VCPU_EVENT(vcpu, 5, "%s", "store cpu id"); | |
453423dc CB |
362 | return 0; |
363 | } | |
364 | ||
365 | static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem) | |
366 | { | |
180c12fb | 367 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
453423dc CB |
368 | int cpus = 0; |
369 | int n; | |
370 | ||
b037a4f3 | 371 | spin_lock(&fi->lock); |
453423dc CB |
372 | for (n = 0; n < KVM_MAX_VCPUS; n++) |
373 | if (fi->local_int[n]) | |
374 | cpus++; | |
b037a4f3 | 375 | spin_unlock(&fi->lock); |
453423dc CB |
376 | |
377 | /* deal with other level 3 hypervisors */ | |
caf757c6 | 378 | if (stsi(mem, 3, 2, 2)) |
453423dc CB |
379 | mem->count = 0; |
380 | if (mem->count < 8) | |
381 | mem->count++; | |
382 | for (n = mem->count - 1; n > 0 ; n--) | |
383 | memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0])); | |
384 | ||
385 | mem->vm[0].cpus_total = cpus; | |
386 | mem->vm[0].cpus_configured = cpus; | |
387 | mem->vm[0].cpus_standby = 0; | |
388 | mem->vm[0].cpus_reserved = 0; | |
389 | mem->vm[0].caf = 1000; | |
390 | memcpy(mem->vm[0].name, "KVMguest", 8); | |
391 | ASCEBC(mem->vm[0].name, 8); | |
392 | memcpy(mem->vm[0].cpi, "KVM/Linux ", 16); | |
393 | ASCEBC(mem->vm[0].cpi, 16); | |
394 | } | |
395 | ||
396 | static int handle_stsi(struct kvm_vcpu *vcpu) | |
397 | { | |
5a32c1af CB |
398 | int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28; |
399 | int sel1 = vcpu->run->s.regs.gprs[0] & 0xff; | |
400 | int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff; | |
c51f068c | 401 | unsigned long mem = 0; |
453423dc | 402 | u64 operand2; |
db4a29cb | 403 | int rc = 0; |
453423dc CB |
404 | |
405 | vcpu->stat.instruction_stsi++; | |
406 | VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2); | |
407 | ||
5087dfa6 TH |
408 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
409 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
410 | ||
87d41fb4 | 411 | if (fc > 3) { |
ea828ebf | 412 | kvm_s390_set_psw_cc(vcpu, 3); |
87d41fb4 TH |
413 | return 0; |
414 | } | |
453423dc | 415 | |
87d41fb4 TH |
416 | if (vcpu->run->s.regs.gprs[0] & 0x0fffff00 |
417 | || vcpu->run->s.regs.gprs[1] & 0xffff0000) | |
453423dc CB |
418 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
419 | ||
87d41fb4 | 420 | if (fc == 0) { |
5a32c1af | 421 | vcpu->run->s.regs.gprs[0] = 3 << 28; |
ea828ebf | 422 | kvm_s390_set_psw_cc(vcpu, 0); |
453423dc | 423 | return 0; |
87d41fb4 TH |
424 | } |
425 | ||
426 | operand2 = kvm_s390_get_base_disp_s(vcpu); | |
427 | ||
428 | if (operand2 & 0xfff) | |
429 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
430 | ||
431 | switch (fc) { | |
453423dc CB |
432 | case 1: /* same handling for 1 and 2 */ |
433 | case 2: | |
434 | mem = get_zeroed_page(GFP_KERNEL); | |
435 | if (!mem) | |
c51f068c | 436 | goto out_no_data; |
caf757c6 | 437 | if (stsi((void *) mem, fc, sel1, sel2)) |
c51f068c | 438 | goto out_no_data; |
453423dc CB |
439 | break; |
440 | case 3: | |
441 | if (sel1 != 2 || sel2 != 2) | |
c51f068c | 442 | goto out_no_data; |
453423dc CB |
443 | mem = get_zeroed_page(GFP_KERNEL); |
444 | if (!mem) | |
c51f068c | 445 | goto out_no_data; |
453423dc CB |
446 | handle_stsi_3_2_2(vcpu, (void *) mem); |
447 | break; | |
453423dc CB |
448 | } |
449 | ||
450 | if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) { | |
db4a29cb | 451 | rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); |
c51f068c | 452 | goto out_exception; |
453423dc | 453 | } |
5786fffa | 454 | trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2); |
453423dc | 455 | free_page(mem); |
ea828ebf | 456 | kvm_s390_set_psw_cc(vcpu, 0); |
5a32c1af | 457 | vcpu->run->s.regs.gprs[0] = 0; |
453423dc | 458 | return 0; |
c51f068c | 459 | out_no_data: |
ea828ebf | 460 | kvm_s390_set_psw_cc(vcpu, 3); |
c51f068c HC |
461 | out_exception: |
462 | free_page(mem); | |
db4a29cb | 463 | return rc; |
453423dc CB |
464 | } |
465 | ||
f379aae5 | 466 | static const intercept_handler_t b2_handlers[256] = { |
453423dc CB |
467 | [0x02] = handle_stidp, |
468 | [0x10] = handle_set_prefix, | |
469 | [0x11] = handle_store_prefix, | |
470 | [0x12] = handle_store_cpu_address, | |
471 | [0x29] = handle_skey, | |
472 | [0x2a] = handle_skey, | |
473 | [0x2b] = handle_skey, | |
aca84241 | 474 | [0x2c] = handle_test_block, |
f379aae5 CH |
475 | [0x30] = handle_io_inst, |
476 | [0x31] = handle_io_inst, | |
477 | [0x32] = handle_io_inst, | |
478 | [0x33] = handle_io_inst, | |
479 | [0x34] = handle_io_inst, | |
480 | [0x35] = handle_io_inst, | |
481 | [0x36] = handle_io_inst, | |
482 | [0x37] = handle_io_inst, | |
483 | [0x38] = handle_io_inst, | |
484 | [0x39] = handle_io_inst, | |
485 | [0x3a] = handle_io_inst, | |
486 | [0x3b] = handle_io_inst, | |
487 | [0x3c] = handle_io_inst, | |
488 | [0x5f] = handle_io_inst, | |
489 | [0x74] = handle_io_inst, | |
490 | [0x76] = handle_io_inst, | |
453423dc CB |
491 | [0x7d] = handle_stsi, |
492 | [0xb1] = handle_stfl, | |
48a3e950 | 493 | [0xb2] = handle_lpswe, |
453423dc CB |
494 | }; |
495 | ||
70455a36 | 496 | int kvm_s390_handle_b2(struct kvm_vcpu *vcpu) |
453423dc CB |
497 | { |
498 | intercept_handler_t handler; | |
499 | ||
70455a36 | 500 | /* |
5087dfa6 TH |
501 | * A lot of B2 instructions are priviledged. Here we check for |
502 | * the privileged ones, that we can handle in the kernel. | |
503 | * Anything else goes to userspace. | |
504 | */ | |
f379aae5 | 505 | handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; |
5087dfa6 TH |
506 | if (handler) |
507 | return handler(vcpu); | |
508 | ||
b8e660b8 | 509 | return -EOPNOTSUPP; |
453423dc | 510 | } |
bb25b9ba | 511 | |
48a3e950 CH |
512 | static int handle_epsw(struct kvm_vcpu *vcpu) |
513 | { | |
514 | int reg1, reg2; | |
515 | ||
aeb87c3c | 516 | kvm_s390_get_regs_rre(vcpu, ®1, ®2); |
48a3e950 CH |
517 | |
518 | /* This basically extracts the mask half of the psw. */ | |
843200e7 | 519 | vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL; |
48a3e950 CH |
520 | vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32; |
521 | if (reg2) { | |
843200e7 | 522 | vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL; |
48a3e950 | 523 | vcpu->run->s.regs.gprs[reg2] |= |
843200e7 | 524 | vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL; |
48a3e950 CH |
525 | } |
526 | return 0; | |
527 | } | |
528 | ||
69d0d3a3 CB |
529 | #define PFMF_RESERVED 0xfffc0101UL |
530 | #define PFMF_SK 0x00020000UL | |
531 | #define PFMF_CF 0x00010000UL | |
532 | #define PFMF_UI 0x00008000UL | |
533 | #define PFMF_FSC 0x00007000UL | |
534 | #define PFMF_NQ 0x00000800UL | |
535 | #define PFMF_MR 0x00000400UL | |
536 | #define PFMF_MC 0x00000200UL | |
537 | #define PFMF_KEY 0x000000feUL | |
538 | ||
539 | static int handle_pfmf(struct kvm_vcpu *vcpu) | |
540 | { | |
541 | int reg1, reg2; | |
542 | unsigned long start, end; | |
543 | ||
544 | vcpu->stat.instruction_pfmf++; | |
545 | ||
546 | kvm_s390_get_regs_rre(vcpu, ®1, ®2); | |
547 | ||
548 | if (!MACHINE_HAS_PFMF) | |
549 | return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); | |
550 | ||
551 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
208dd756 | 552 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
69d0d3a3 CB |
553 | |
554 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED) | |
555 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
556 | ||
557 | /* Only provide non-quiescing support if the host supports it */ | |
e769ece3 | 558 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14)) |
69d0d3a3 CB |
559 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
560 | ||
561 | /* No support for conditional-SSKE */ | |
562 | if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC)) | |
563 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
564 | ||
565 | start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; | |
566 | switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) { | |
567 | case 0x00000000: | |
568 | end = (start + (1UL << 12)) & ~((1UL << 12) - 1); | |
569 | break; | |
570 | case 0x00001000: | |
571 | end = (start + (1UL << 20)) & ~((1UL << 20) - 1); | |
572 | break; | |
573 | /* We dont support EDAT2 | |
574 | case 0x00002000: | |
575 | end = (start + (1UL << 31)) & ~((1UL << 31) - 1); | |
576 | break;*/ | |
577 | default: | |
578 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
579 | } | |
580 | while (start < end) { | |
581 | unsigned long useraddr; | |
582 | ||
583 | useraddr = gmap_translate(start, vcpu->arch.gmap); | |
584 | if (IS_ERR((void *)useraddr)) | |
585 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
586 | ||
587 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) { | |
588 | if (clear_user((void __user *)useraddr, PAGE_SIZE)) | |
589 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
590 | } | |
591 | ||
592 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) { | |
593 | if (set_guest_storage_key(current->mm, useraddr, | |
594 | vcpu->run->s.regs.gprs[reg1] & PFMF_KEY, | |
595 | vcpu->run->s.regs.gprs[reg1] & PFMF_NQ)) | |
596 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
597 | } | |
598 | ||
599 | start += PAGE_SIZE; | |
600 | } | |
601 | if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) | |
602 | vcpu->run->s.regs.gprs[reg2] = end; | |
603 | return 0; | |
604 | } | |
605 | ||
48a3e950 CH |
606 | static const intercept_handler_t b9_handlers[256] = { |
607 | [0x8d] = handle_epsw, | |
f379aae5 | 608 | [0x9c] = handle_io_inst, |
69d0d3a3 | 609 | [0xaf] = handle_pfmf, |
48a3e950 CH |
610 | }; |
611 | ||
612 | int kvm_s390_handle_b9(struct kvm_vcpu *vcpu) | |
613 | { | |
614 | intercept_handler_t handler; | |
615 | ||
616 | /* This is handled just as for the B2 instructions. */ | |
617 | handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; | |
5087dfa6 TH |
618 | if (handler) |
619 | return handler(vcpu); | |
620 | ||
48a3e950 CH |
621 | return -EOPNOTSUPP; |
622 | } | |
623 | ||
953ed88d TH |
624 | int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu) |
625 | { | |
626 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | |
627 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; | |
628 | u64 useraddr; | |
629 | u32 val = 0; | |
630 | int reg, rc; | |
631 | ||
632 | vcpu->stat.instruction_lctl++; | |
633 | ||
634 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
635 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
636 | ||
637 | useraddr = kvm_s390_get_base_disp_rs(vcpu); | |
638 | ||
639 | if (useraddr & 3) | |
640 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
641 | ||
642 | VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, | |
643 | useraddr); | |
644 | trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr); | |
645 | ||
646 | reg = reg1; | |
647 | do { | |
648 | rc = get_guest(vcpu, val, (u32 __user *) useraddr); | |
649 | if (rc) | |
650 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
651 | vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; | |
652 | vcpu->arch.sie_block->gcr[reg] |= val; | |
653 | useraddr += 4; | |
654 | if (reg == reg3) | |
655 | break; | |
656 | reg = (reg + 1) % 16; | |
657 | } while (1); | |
658 | ||
659 | return 0; | |
660 | } | |
661 | ||
662 | static int handle_lctlg(struct kvm_vcpu *vcpu) | |
663 | { | |
664 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | |
665 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; | |
666 | u64 useraddr; | |
667 | int reg, rc; | |
668 | ||
669 | vcpu->stat.instruction_lctlg++; | |
670 | ||
671 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
672 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
673 | ||
674 | useraddr = kvm_s390_get_base_disp_rsy(vcpu); | |
675 | ||
676 | if (useraddr & 7) | |
677 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
678 | ||
679 | reg = reg1; | |
680 | ||
681 | VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, | |
682 | useraddr); | |
683 | trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr); | |
684 | ||
685 | do { | |
686 | rc = get_guest(vcpu, vcpu->arch.sie_block->gcr[reg], | |
687 | (u64 __user *) useraddr); | |
688 | if (rc) | |
689 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
690 | useraddr += 8; | |
691 | if (reg == reg3) | |
692 | break; | |
693 | reg = (reg + 1) % 16; | |
694 | } while (1); | |
695 | ||
696 | return 0; | |
697 | } | |
698 | ||
f379aae5 | 699 | static const intercept_handler_t eb_handlers[256] = { |
953ed88d | 700 | [0x2f] = handle_lctlg, |
f379aae5 CH |
701 | [0x8a] = handle_io_inst, |
702 | }; | |
703 | ||
953ed88d | 704 | int kvm_s390_handle_eb(struct kvm_vcpu *vcpu) |
f379aae5 CH |
705 | { |
706 | intercept_handler_t handler; | |
707 | ||
f379aae5 CH |
708 | handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff]; |
709 | if (handler) | |
710 | return handler(vcpu); | |
711 | return -EOPNOTSUPP; | |
712 | } | |
713 | ||
bb25b9ba CB |
714 | static int handle_tprot(struct kvm_vcpu *vcpu) |
715 | { | |
b1c571a5 | 716 | u64 address1, address2; |
bb25b9ba | 717 | struct vm_area_struct *vma; |
1eddb85f | 718 | unsigned long user_address; |
bb25b9ba CB |
719 | |
720 | vcpu->stat.instruction_tprot++; | |
721 | ||
f9f6bbc6 TH |
722 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) |
723 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); | |
724 | ||
b1c571a5 CH |
725 | kvm_s390_get_base_disp_sse(vcpu, &address1, &address2); |
726 | ||
bb25b9ba CB |
727 | /* we only handle the Linux memory detection case: |
728 | * access key == 0 | |
729 | * guest DAT == off | |
730 | * everything else goes to userspace. */ | |
731 | if (address2 & 0xf0) | |
732 | return -EOPNOTSUPP; | |
733 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT) | |
734 | return -EOPNOTSUPP; | |
735 | ||
bb25b9ba | 736 | down_read(¤t->mm->mmap_sem); |
59a1fa2d HC |
737 | user_address = __gmap_translate(address1, vcpu->arch.gmap); |
738 | if (IS_ERR_VALUE(user_address)) | |
739 | goto out_inject; | |
1eddb85f | 740 | vma = find_vma(current->mm, user_address); |
59a1fa2d HC |
741 | if (!vma) |
742 | goto out_inject; | |
bb25b9ba CB |
743 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); |
744 | if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ)) | |
745 | vcpu->arch.sie_block->gpsw.mask |= (1ul << 44); | |
746 | if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ)) | |
747 | vcpu->arch.sie_block->gpsw.mask |= (2ul << 44); | |
748 | ||
749 | up_read(¤t->mm->mmap_sem); | |
750 | return 0; | |
59a1fa2d HC |
751 | |
752 | out_inject: | |
753 | up_read(¤t->mm->mmap_sem); | |
754 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
bb25b9ba CB |
755 | } |
756 | ||
757 | int kvm_s390_handle_e5(struct kvm_vcpu *vcpu) | |
758 | { | |
759 | /* For e5xx... instructions we only handle TPROT */ | |
760 | if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01) | |
761 | return handle_tprot(vcpu); | |
762 | return -EOPNOTSUPP; | |
763 | } | |
764 | ||
8c3f61e2 CH |
765 | static int handle_sckpf(struct kvm_vcpu *vcpu) |
766 | { | |
767 | u32 value; | |
768 | ||
769 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
208dd756 | 770 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
8c3f61e2 CH |
771 | |
772 | if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000) | |
773 | return kvm_s390_inject_program_int(vcpu, | |
774 | PGM_SPECIFICATION); | |
775 | ||
776 | value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff; | |
777 | vcpu->arch.sie_block->todpr = value; | |
778 | ||
779 | return 0; | |
780 | } | |
781 | ||
77975357 | 782 | static const intercept_handler_t x01_handlers[256] = { |
8c3f61e2 CH |
783 | [0x07] = handle_sckpf, |
784 | }; | |
785 | ||
786 | int kvm_s390_handle_01(struct kvm_vcpu *vcpu) | |
787 | { | |
788 | intercept_handler_t handler; | |
789 | ||
790 | handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff]; | |
791 | if (handler) | |
792 | return handler(vcpu); | |
793 | return -EOPNOTSUPP; | |
794 | } |