1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * handling privileged instructions
4 *
5 * Copyright IBM Corp. 2008, 2018
6 *
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
9 */
10
11 #include <linux/kvm.h>
12 #include <linux/gfp.h>
13 #include <linux/errno.h>
14 #include <linux/compat.h>
15 #include <linux/mm_types.h>
16
17 #include <asm/asm-offsets.h>
18 #include <asm/facility.h>
19 #include <asm/current.h>
20 #include <asm/debug.h>
21 #include <asm/ebcdic.h>
22 #include <asm/sysinfo.h>
23 #include <asm/pgtable.h>
24 #include <asm/page-states.h>
25 #include <asm/pgalloc.h>
26 #include <asm/gmap.h>
27 #include <asm/io.h>
28 #include <asm/ptrace.h>
29 #include <asm/sclp.h>
30 #include "gaccess.h"
31 #include "kvm-s390.h"
32 #include "trace.h"
33
handle_ri(struct kvm_vcpu * vcpu)34 static int handle_ri(struct kvm_vcpu *vcpu)
35 {
36 vcpu->stat.instruction_ri++;
37
38 if (test_kvm_facility(vcpu->kvm, 64)) {
39 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
40 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
41 kvm_s390_retry_instr(vcpu);
42 return 0;
43 } else
44 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
45 }
46
kvm_s390_handle_aa(struct kvm_vcpu * vcpu)47 int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
48 {
49 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
50 return handle_ri(vcpu);
51 else
52 return -EOPNOTSUPP;
53 }
54
handle_gs(struct kvm_vcpu * vcpu)55 static int handle_gs(struct kvm_vcpu *vcpu)
56 {
57 vcpu->stat.instruction_gs++;
58
59 if (test_kvm_facility(vcpu->kvm, 133)) {
60 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
61 preempt_disable();
62 __ctl_set_bit(2, 4);
63 current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
64 restore_gs_cb(current->thread.gs_cb);
65 preempt_enable();
66 vcpu->arch.sie_block->ecb |= ECB_GS;
67 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
68 vcpu->arch.gs_enabled = 1;
69 kvm_s390_retry_instr(vcpu);
70 return 0;
71 } else
72 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
73 }
74
kvm_s390_handle_e3(struct kvm_vcpu * vcpu)75 int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
76 {
77 int code = vcpu->arch.sie_block->ipb & 0xff;
78
79 if (code == 0x49 || code == 0x4d)
80 return handle_gs(vcpu);
81 else
82 return -EOPNOTSUPP;
83 }
84 /* Handle SCK (SET CLOCK) interception */
handle_set_clock(struct kvm_vcpu * vcpu)85 static int handle_set_clock(struct kvm_vcpu *vcpu)
86 {
87 struct kvm_s390_vm_tod_clock gtod = { 0 };
88 int rc;
89 u8 ar;
90 u64 op2;
91
92 vcpu->stat.instruction_sck++;
93
94 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
95 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
96
97 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
98 if (op2 & 7) /* Operand must be on a doubleword boundary */
99 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
100 rc = read_guest(vcpu, op2, ar, >od.tod, sizeof(gtod.tod));
101 if (rc)
102 return kvm_s390_inject_prog_cond(vcpu, rc);
103
104 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);
105 kvm_s390_set_tod_clock(vcpu->kvm, >od);
106
107 kvm_s390_set_psw_cc(vcpu, 0);
108 return 0;
109 }
110
handle_set_prefix(struct kvm_vcpu * vcpu)111 static int handle_set_prefix(struct kvm_vcpu *vcpu)
112 {
113 u64 operand2;
114 u32 address;
115 int rc;
116 u8 ar;
117
118 vcpu->stat.instruction_spx++;
119
120 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
121 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
122
123 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
124
125 /* must be word boundary */
126 if (operand2 & 3)
127 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
128
129 /* get the value */
130 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
131 if (rc)
132 return kvm_s390_inject_prog_cond(vcpu, rc);
133
134 address &= 0x7fffe000u;
135
136 /*
137 * Make sure the new value is valid memory. We only need to check the
138 * first page, since address is 8k aligned and memory pieces are always
139 * at least 1MB aligned and have at least a size of 1MB.
140 */
141 if (kvm_is_error_gpa(vcpu->kvm, address))
142 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
143
144 kvm_s390_set_prefix(vcpu, address);
145 trace_kvm_s390_handle_prefix(vcpu, 1, address);
146 return 0;
147 }
148
handle_store_prefix(struct kvm_vcpu * vcpu)149 static int handle_store_prefix(struct kvm_vcpu *vcpu)
150 {
151 u64 operand2;
152 u32 address;
153 int rc;
154 u8 ar;
155
156 vcpu->stat.instruction_stpx++;
157
158 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
159 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
160
161 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
162
163 /* must be word boundary */
164 if (operand2 & 3)
165 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
166
167 address = kvm_s390_get_prefix(vcpu);
168
169 /* get the value */
170 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
171 if (rc)
172 return kvm_s390_inject_prog_cond(vcpu, rc);
173
174 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
175 trace_kvm_s390_handle_prefix(vcpu, 0, address);
176 return 0;
177 }
178
handle_store_cpu_address(struct kvm_vcpu * vcpu)179 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
180 {
181 u16 vcpu_id = vcpu->vcpu_id;
182 u64 ga;
183 int rc;
184 u8 ar;
185
186 vcpu->stat.instruction_stap++;
187
188 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
189 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
190
191 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
192
193 if (ga & 1)
194 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
195
196 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
197 if (rc)
198 return kvm_s390_inject_prog_cond(vcpu, rc);
199
200 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
201 trace_kvm_s390_handle_stap(vcpu, ga);
202 return 0;
203 }
204
kvm_s390_skey_check_enable(struct kvm_vcpu * vcpu)205 int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu)
206 {
207 int rc;
208
209 trace_kvm_s390_skey_related_inst(vcpu);
210 /* Already enabled? */
211 if (vcpu->arch.skey_enabled)
212 return 0;
213
214 rc = s390_enable_skey();
215 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
216 if (rc)
217 return rc;
218
219 if (kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
220 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_KSS);
221 if (!vcpu->kvm->arch.use_skf)
222 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
223 else
224 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
225 vcpu->arch.skey_enabled = true;
226 return 0;
227 }
228
try_handle_skey(struct kvm_vcpu * vcpu)229 static int try_handle_skey(struct kvm_vcpu *vcpu)
230 {
231 int rc;
232
233 rc = kvm_s390_skey_check_enable(vcpu);
234 if (rc)
235 return rc;
236 if (vcpu->kvm->arch.use_skf) {
237 /* with storage-key facility, SIE interprets it for us */
238 kvm_s390_retry_instr(vcpu);
239 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
240 return -EAGAIN;
241 }
242 return 0;
243 }
244
handle_iske(struct kvm_vcpu * vcpu)245 static int handle_iske(struct kvm_vcpu *vcpu)
246 {
247 unsigned long gaddr, vmaddr;
248 unsigned char key;
249 int reg1, reg2;
250 bool unlocked;
251 int rc;
252
253 vcpu->stat.instruction_iske++;
254
255 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
256 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
257
258 rc = try_handle_skey(vcpu);
259 if (rc)
260 return rc != -EAGAIN ? rc : 0;
261
262 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
263
264 gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
265 gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
266 gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
267 vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
268 if (kvm_is_error_hva(vmaddr))
269 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
270 retry:
271 unlocked = false;
272 down_read(¤t->mm->mmap_sem);
273 rc = get_guest_storage_key(current->mm, vmaddr, &key);
274
275 if (rc) {
276 rc = fixup_user_fault(current, current->mm, vmaddr,
277 FAULT_FLAG_WRITE, &unlocked);
278 if (!rc) {
279 up_read(¤t->mm->mmap_sem);
280 goto retry;
281 }
282 }
283 up_read(¤t->mm->mmap_sem);
284 if (rc == -EFAULT)
285 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
286 if (rc < 0)
287 return rc;
288 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
289 vcpu->run->s.regs.gprs[reg1] |= key;
290 return 0;
291 }
292
handle_rrbe(struct kvm_vcpu * vcpu)293 static int handle_rrbe(struct kvm_vcpu *vcpu)
294 {
295 unsigned long vmaddr, gaddr;
296 int reg1, reg2;
297 bool unlocked;
298 int rc;
299
300 vcpu->stat.instruction_rrbe++;
301
302 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
303 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
304
305 rc = try_handle_skey(vcpu);
306 if (rc)
307 return rc != -EAGAIN ? rc : 0;
308
309 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
310
311 gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
312 gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
313 gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
314 vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
315 if (kvm_is_error_hva(vmaddr))
316 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
317 retry:
318 unlocked = false;
319 down_read(¤t->mm->mmap_sem);
320 rc = reset_guest_reference_bit(current->mm, vmaddr);
321 if (rc < 0) {
322 rc = fixup_user_fault(current, current->mm, vmaddr,
323 FAULT_FLAG_WRITE, &unlocked);
324 if (!rc) {
325 up_read(¤t->mm->mmap_sem);
326 goto retry;
327 }
328 }
329 up_read(¤t->mm->mmap_sem);
330 if (rc == -EFAULT)
331 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
332 if (rc < 0)
333 return rc;
334 kvm_s390_set_psw_cc(vcpu, rc);
335 return 0;
336 }
337
338 #define SSKE_NQ 0x8
339 #define SSKE_MR 0x4
340 #define SSKE_MC 0x2
341 #define SSKE_MB 0x1
handle_sske(struct kvm_vcpu * vcpu)342 static int handle_sske(struct kvm_vcpu *vcpu)
343 {
344 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
345 unsigned long start, end;
346 unsigned char key, oldkey;
347 int reg1, reg2;
348 bool unlocked;
349 int rc;
350
351 vcpu->stat.instruction_sske++;
352
353 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
354 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
355
356 rc = try_handle_skey(vcpu);
357 if (rc)
358 return rc != -EAGAIN ? rc : 0;
359
360 if (!test_kvm_facility(vcpu->kvm, 8))
361 m3 &= ~SSKE_MB;
362 if (!test_kvm_facility(vcpu->kvm, 10))
363 m3 &= ~(SSKE_MC | SSKE_MR);
364 if (!test_kvm_facility(vcpu->kvm, 14))
365 m3 &= ~SSKE_NQ;
366
367 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
368
369 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
370 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
371 start = kvm_s390_logical_to_effective(vcpu, start);
372 if (m3 & SSKE_MB) {
373 /* start already designates an absolute address */
374 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
375 } else {
376 start = kvm_s390_real_to_abs(vcpu, start);
377 end = start + PAGE_SIZE;
378 }
379
380 while (start != end) {
381 unsigned long vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
382 unlocked = false;
383
384 if (kvm_is_error_hva(vmaddr))
385 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
386
387 down_read(¤t->mm->mmap_sem);
388 rc = cond_set_guest_storage_key(current->mm, vmaddr, key, &oldkey,
389 m3 & SSKE_NQ, m3 & SSKE_MR,
390 m3 & SSKE_MC);
391
392 if (rc < 0) {
393 rc = fixup_user_fault(current, current->mm, vmaddr,
394 FAULT_FLAG_WRITE, &unlocked);
395 rc = !rc ? -EAGAIN : rc;
396 }
397 up_read(¤t->mm->mmap_sem);
398 if (rc == -EFAULT)
399 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
400 if (rc < 0)
401 return rc;
402 start += PAGE_SIZE;
403 }
404
405 if (m3 & (SSKE_MC | SSKE_MR)) {
406 if (m3 & SSKE_MB) {
407 /* skey in reg1 is unpredictable */
408 kvm_s390_set_psw_cc(vcpu, 3);
409 } else {
410 kvm_s390_set_psw_cc(vcpu, rc);
411 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
412 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
413 }
414 }
415 if (m3 & SSKE_MB) {
416 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
417 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
418 else
419 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
420 end = kvm_s390_logical_to_effective(vcpu, end);
421 vcpu->run->s.regs.gprs[reg2] |= end;
422 }
423 return 0;
424 }
425
handle_ipte_interlock(struct kvm_vcpu * vcpu)426 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
427 {
428 vcpu->stat.instruction_ipte_interlock++;
429 if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
430 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
431 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
432 kvm_s390_retry_instr(vcpu);
433 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
434 return 0;
435 }
436
handle_test_block(struct kvm_vcpu * vcpu)437 static int handle_test_block(struct kvm_vcpu *vcpu)
438 {
439 gpa_t addr;
440 int reg2;
441
442 vcpu->stat.instruction_tb++;
443
444 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
445 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
446
447 kvm_s390_get_regs_rre(vcpu, NULL, ®2);
448 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
449 addr = kvm_s390_logical_to_effective(vcpu, addr);
450 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
451 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
452 addr = kvm_s390_real_to_abs(vcpu, addr);
453
454 if (kvm_is_error_gpa(vcpu->kvm, addr))
455 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
456 /*
457 * We don't expect errors on modern systems, and do not care
458 * about storage keys (yet), so let's just clear the page.
459 */
460 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
461 return -EFAULT;
462 kvm_s390_set_psw_cc(vcpu, 0);
463 vcpu->run->s.regs.gprs[0] = 0;
464 return 0;
465 }
466
handle_tpi(struct kvm_vcpu * vcpu)467 static int handle_tpi(struct kvm_vcpu *vcpu)
468 {
469 struct kvm_s390_interrupt_info *inti;
470 unsigned long len;
471 u32 tpi_data[3];
472 int rc;
473 u64 addr;
474 u8 ar;
475
476 vcpu->stat.instruction_tpi++;
477
478 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
479 if (addr & 3)
480 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
481
482 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
483 if (!inti) {
484 kvm_s390_set_psw_cc(vcpu, 0);
485 return 0;
486 }
487
488 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
489 tpi_data[1] = inti->io.io_int_parm;
490 tpi_data[2] = inti->io.io_int_word;
491 if (addr) {
492 /*
493 * Store the two-word I/O interruption code into the
494 * provided area.
495 */
496 len = sizeof(tpi_data) - 4;
497 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
498 if (rc) {
499 rc = kvm_s390_inject_prog_cond(vcpu, rc);
500 goto reinject_interrupt;
501 }
502 } else {
503 /*
504 * Store the three-word I/O interruption code into
505 * the appropriate lowcore area.
506 */
507 len = sizeof(tpi_data);
508 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
509 /* failed writes to the low core are not recoverable */
510 rc = -EFAULT;
511 goto reinject_interrupt;
512 }
513 }
514
515 /* irq was successfully handed to the guest */
516 kfree(inti);
517 kvm_s390_set_psw_cc(vcpu, 1);
518 return 0;
519 reinject_interrupt:
520 /*
521 * If we encounter a problem storing the interruption code, the
522 * instruction is suppressed from the guest's view: reinject the
523 * interrupt.
524 */
525 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
526 kfree(inti);
527 rc = -EFAULT;
528 }
529 /* don't set the cc, a pgm irq was injected or we drop to user space */
530 return rc ? -EFAULT : 0;
531 }
532
handle_tsch(struct kvm_vcpu * vcpu)533 static int handle_tsch(struct kvm_vcpu *vcpu)
534 {
535 struct kvm_s390_interrupt_info *inti = NULL;
536 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
537
538 vcpu->stat.instruction_tsch++;
539
540 /* a valid schid has at least one bit set */
541 if (vcpu->run->s.regs.gprs[1])
542 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
543 vcpu->run->s.regs.gprs[1]);
544
545 /*
546 * Prepare exit to userspace.
547 * We indicate whether we dequeued a pending I/O interrupt
548 * so that userspace can re-inject it if the instruction gets
549 * a program check. While this may re-order the pending I/O
550 * interrupts, this is no problem since the priority is kept
551 * intact.
552 */
553 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
554 vcpu->run->s390_tsch.dequeued = !!inti;
555 if (inti) {
556 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
557 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
558 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
559 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
560 }
561 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
562 kfree(inti);
563 return -EREMOTE;
564 }
565
handle_io_inst(struct kvm_vcpu * vcpu)566 static int handle_io_inst(struct kvm_vcpu *vcpu)
567 {
568 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
569
570 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
571 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
572
573 if (vcpu->kvm->arch.css_support) {
574 /*
575 * Most I/O instructions will be handled by userspace.
576 * Exceptions are tpi and the interrupt portion of tsch.
577 */
578 if (vcpu->arch.sie_block->ipa == 0xb236)
579 return handle_tpi(vcpu);
580 if (vcpu->arch.sie_block->ipa == 0xb235)
581 return handle_tsch(vcpu);
582 /* Handle in userspace. */
583 vcpu->stat.instruction_io_other++;
584 return -EOPNOTSUPP;
585 } else {
586 /*
587 * Set condition code 3 to stop the guest from issuing channel
588 * I/O instructions.
589 */
590 kvm_s390_set_psw_cc(vcpu, 3);
591 return 0;
592 }
593 }
594
handle_stfl(struct kvm_vcpu * vcpu)595 static int handle_stfl(struct kvm_vcpu *vcpu)
596 {
597 int rc;
598 unsigned int fac;
599
600 vcpu->stat.instruction_stfl++;
601
602 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
603 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
604
605 /*
606 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
607 * into a u32 memory representation. They will remain bits 0-31.
608 */
609 fac = *vcpu->kvm->arch.model.fac_list >> 32;
610 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
611 &fac, sizeof(fac));
612 if (rc)
613 return rc;
614 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
615 trace_kvm_s390_handle_stfl(vcpu, fac);
616 return 0;
617 }
618
619 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
620 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
621 #define PSW_ADDR_24 0x0000000000ffffffUL
622 #define PSW_ADDR_31 0x000000007fffffffUL
623
is_valid_psw(psw_t * psw)624 int is_valid_psw(psw_t *psw)
625 {
626 if (psw->mask & PSW_MASK_UNASSIGNED)
627 return 0;
628 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
629 if (psw->addr & ~PSW_ADDR_31)
630 return 0;
631 }
632 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
633 return 0;
634 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
635 return 0;
636 if (psw->addr & 1)
637 return 0;
638 return 1;
639 }
640
kvm_s390_handle_lpsw(struct kvm_vcpu * vcpu)641 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
642 {
643 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
644 psw_compat_t new_psw;
645 u64 addr;
646 int rc;
647 u8 ar;
648
649 vcpu->stat.instruction_lpsw++;
650
651 if (gpsw->mask & PSW_MASK_PSTATE)
652 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
653
654 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
655 if (addr & 7)
656 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
657
658 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
659 if (rc)
660 return kvm_s390_inject_prog_cond(vcpu, rc);
661 if (!(new_psw.mask & PSW32_MASK_BASE))
662 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
663 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
664 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
665 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
666 if (!is_valid_psw(gpsw))
667 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
668 return 0;
669 }
670
handle_lpswe(struct kvm_vcpu * vcpu)671 static int handle_lpswe(struct kvm_vcpu *vcpu)
672 {
673 psw_t new_psw;
674 u64 addr;
675 int rc;
676 u8 ar;
677
678 vcpu->stat.instruction_lpswe++;
679
680 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
681 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
682
683 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
684 if (addr & 7)
685 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
686 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
687 if (rc)
688 return kvm_s390_inject_prog_cond(vcpu, rc);
689 vcpu->arch.sie_block->gpsw = new_psw;
690 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
691 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
692 return 0;
693 }
694
handle_stidp(struct kvm_vcpu * vcpu)695 static int handle_stidp(struct kvm_vcpu *vcpu)
696 {
697 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
698 u64 operand2;
699 int rc;
700 u8 ar;
701
702 vcpu->stat.instruction_stidp++;
703
704 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
705 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
706
707 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
708
709 if (operand2 & 7)
710 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
711
712 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
713 if (rc)
714 return kvm_s390_inject_prog_cond(vcpu, rc);
715
716 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
717 return 0;
718 }
719
handle_stsi_3_2_2(struct kvm_vcpu * vcpu,struct sysinfo_3_2_2 * mem)720 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
721 {
722 int cpus = 0;
723 int n;
724
725 cpus = atomic_read(&vcpu->kvm->online_vcpus);
726
727 /* deal with other level 3 hypervisors */
728 if (stsi(mem, 3, 2, 2))
729 mem->count = 0;
730 if (mem->count < 8)
731 mem->count++;
732 for (n = mem->count - 1; n > 0 ; n--)
733 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
734
735 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
736 mem->vm[0].cpus_total = cpus;
737 mem->vm[0].cpus_configured = cpus;
738 mem->vm[0].cpus_standby = 0;
739 mem->vm[0].cpus_reserved = 0;
740 mem->vm[0].caf = 1000;
741 memcpy(mem->vm[0].name, "KVMguest", 8);
742 ASCEBC(mem->vm[0].name, 8);
743 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
744 ASCEBC(mem->vm[0].cpi, 16);
745 }
746
insert_stsi_usr_data(struct kvm_vcpu * vcpu,u64 addr,u8 ar,u8 fc,u8 sel1,u16 sel2)747 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
748 u8 fc, u8 sel1, u16 sel2)
749 {
750 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
751 vcpu->run->s390_stsi.addr = addr;
752 vcpu->run->s390_stsi.ar = ar;
753 vcpu->run->s390_stsi.fc = fc;
754 vcpu->run->s390_stsi.sel1 = sel1;
755 vcpu->run->s390_stsi.sel2 = sel2;
756 }
757
handle_stsi(struct kvm_vcpu * vcpu)758 static int handle_stsi(struct kvm_vcpu *vcpu)
759 {
760 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
761 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
762 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
763 unsigned long mem = 0;
764 u64 operand2;
765 int rc = 0;
766 u8 ar;
767
768 vcpu->stat.instruction_stsi++;
769 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
770
771 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
772 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
773
774 if (fc > 3) {
775 kvm_s390_set_psw_cc(vcpu, 3);
776 return 0;
777 }
778
779 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
780 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
781 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
782
783 if (fc == 0) {
784 vcpu->run->s.regs.gprs[0] = 3 << 28;
785 kvm_s390_set_psw_cc(vcpu, 0);
786 return 0;
787 }
788
789 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
790
791 if (operand2 & 0xfff)
792 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
793
794 switch (fc) {
795 case 1: /* same handling for 1 and 2 */
796 case 2:
797 mem = get_zeroed_page(GFP_KERNEL);
798 if (!mem)
799 goto out_no_data;
800 if (stsi((void *) mem, fc, sel1, sel2))
801 goto out_no_data;
802 break;
803 case 3:
804 if (sel1 != 2 || sel2 != 2)
805 goto out_no_data;
806 mem = get_zeroed_page(GFP_KERNEL);
807 if (!mem)
808 goto out_no_data;
809 handle_stsi_3_2_2(vcpu, (void *) mem);
810 break;
811 }
812
813 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
814 if (rc) {
815 rc = kvm_s390_inject_prog_cond(vcpu, rc);
816 goto out;
817 }
818 if (vcpu->kvm->arch.user_stsi) {
819 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
820 rc = -EREMOTE;
821 }
822 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
823 free_page(mem);
824 kvm_s390_set_psw_cc(vcpu, 0);
825 vcpu->run->s.regs.gprs[0] = 0;
826 return rc;
827 out_no_data:
828 kvm_s390_set_psw_cc(vcpu, 3);
829 out:
830 free_page(mem);
831 return rc;
832 }
833
kvm_s390_handle_b2(struct kvm_vcpu * vcpu)834 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
835 {
836 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
837 case 0x02:
838 return handle_stidp(vcpu);
839 case 0x04:
840 return handle_set_clock(vcpu);
841 case 0x10:
842 return handle_set_prefix(vcpu);
843 case 0x11:
844 return handle_store_prefix(vcpu);
845 case 0x12:
846 return handle_store_cpu_address(vcpu);
847 case 0x14:
848 return kvm_s390_handle_vsie(vcpu);
849 case 0x21:
850 case 0x50:
851 return handle_ipte_interlock(vcpu);
852 case 0x29:
853 return handle_iske(vcpu);
854 case 0x2a:
855 return handle_rrbe(vcpu);
856 case 0x2b:
857 return handle_sske(vcpu);
858 case 0x2c:
859 return handle_test_block(vcpu);
860 case 0x30:
861 case 0x31:
862 case 0x32:
863 case 0x33:
864 case 0x34:
865 case 0x35:
866 case 0x36:
867 case 0x37:
868 case 0x38:
869 case 0x39:
870 case 0x3a:
871 case 0x3b:
872 case 0x3c:
873 case 0x5f:
874 case 0x74:
875 case 0x76:
876 return handle_io_inst(vcpu);
877 case 0x56:
878 return handle_sthyi(vcpu);
879 case 0x7d:
880 return handle_stsi(vcpu);
881 case 0xb1:
882 return handle_stfl(vcpu);
883 case 0xb2:
884 return handle_lpswe(vcpu);
885 default:
886 return -EOPNOTSUPP;
887 }
888 }
889
handle_epsw(struct kvm_vcpu * vcpu)890 static int handle_epsw(struct kvm_vcpu *vcpu)
891 {
892 int reg1, reg2;
893
894 vcpu->stat.instruction_epsw++;
895
896 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
897
898 /* This basically extracts the mask half of the psw. */
899 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
900 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
901 if (reg2) {
902 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
903 vcpu->run->s.regs.gprs[reg2] |=
904 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
905 }
906 return 0;
907 }
908
909 #define PFMF_RESERVED 0xfffc0101UL
910 #define PFMF_SK 0x00020000UL
911 #define PFMF_CF 0x00010000UL
912 #define PFMF_UI 0x00008000UL
913 #define PFMF_FSC 0x00007000UL
914 #define PFMF_NQ 0x00000800UL
915 #define PFMF_MR 0x00000400UL
916 #define PFMF_MC 0x00000200UL
917 #define PFMF_KEY 0x000000feUL
918
handle_pfmf(struct kvm_vcpu * vcpu)919 static int handle_pfmf(struct kvm_vcpu *vcpu)
920 {
921 bool mr = false, mc = false, nq;
922 int reg1, reg2;
923 unsigned long start, end;
924 unsigned char key;
925
926 vcpu->stat.instruction_pfmf++;
927
928 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
929
930 if (!test_kvm_facility(vcpu->kvm, 8))
931 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
932
933 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
934 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
935
936 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
937 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
938
939 /* Only provide non-quiescing support if enabled for the guest */
940 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
941 !test_kvm_facility(vcpu->kvm, 14))
942 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
943
944 /* Only provide conditional-SSKE support if enabled for the guest */
945 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
946 test_kvm_facility(vcpu->kvm, 10)) {
947 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
948 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
949 }
950
951 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
952 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
953 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
954 start = kvm_s390_logical_to_effective(vcpu, start);
955
956 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
957 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
958 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
959 }
960
961 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
962 case 0x00000000:
963 /* only 4k frames specify a real address */
964 start = kvm_s390_real_to_abs(vcpu, start);
965 end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
966 break;
967 case 0x00001000:
968 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
969 break;
970 case 0x00002000:
971 /* only support 2G frame size if EDAT2 is available and we are
972 not in 24-bit addressing mode */
973 if (!test_kvm_facility(vcpu->kvm, 78) ||
974 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
975 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
976 end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1);
977 break;
978 default:
979 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
980 }
981
982 while (start != end) {
983 unsigned long vmaddr;
984 bool unlocked = false;
985
986 /* Translate guest address to host address */
987 vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
988 if (kvm_is_error_hva(vmaddr))
989 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
990
991 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
992 if (kvm_clear_guest(vcpu->kvm, start, PAGE_SIZE))
993 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
994 }
995
996 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
997 int rc = kvm_s390_skey_check_enable(vcpu);
998
999 if (rc)
1000 return rc;
1001 down_read(¤t->mm->mmap_sem);
1002 rc = cond_set_guest_storage_key(current->mm, vmaddr,
1003 key, NULL, nq, mr, mc);
1004 if (rc < 0) {
1005 rc = fixup_user_fault(current, current->mm, vmaddr,
1006 FAULT_FLAG_WRITE, &unlocked);
1007 rc = !rc ? -EAGAIN : rc;
1008 }
1009 up_read(¤t->mm->mmap_sem);
1010 if (rc == -EFAULT)
1011 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1012 if (rc == -EAGAIN)
1013 continue;
1014 if (rc < 0)
1015 return rc;
1016 }
1017 start += PAGE_SIZE;
1018 }
1019 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
1020 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
1021 vcpu->run->s.regs.gprs[reg2] = end;
1022 } else {
1023 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
1024 end = kvm_s390_logical_to_effective(vcpu, end);
1025 vcpu->run->s.regs.gprs[reg2] |= end;
1026 }
1027 }
1028 return 0;
1029 }
1030
1031 /*
1032 * Must be called with relevant read locks held (kvm->mm->mmap_sem, kvm->srcu)
1033 */
__do_essa(struct kvm_vcpu * vcpu,const int orc)1034 static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc)
1035 {
1036 int r1, r2, nappended, entries;
1037 unsigned long gfn, hva, res, pgstev, ptev;
1038 unsigned long *cbrlo;
1039
1040 /*
1041 * We don't need to set SD.FPF.SK to 1 here, because if we have a
1042 * machine check here we either handle it or crash
1043 */
1044
1045 kvm_s390_get_regs_rre(vcpu, &r1, &r2);
1046 gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
1047 hva = gfn_to_hva(vcpu->kvm, gfn);
1048 entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1049
1050 if (kvm_is_error_hva(hva))
1051 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1052
1053 nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
1054 if (nappended < 0) {
1055 res = orc ? 0x10 : 0;
1056 vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
1057 return 0;
1058 }
1059 res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
1060 /*
1061 * Set the block-content state part of the result. 0 means resident, so
1062 * nothing to do if the page is valid. 2 is for preserved pages
1063 * (non-present and non-zero), and 3 for zero pages (non-present and
1064 * zero).
1065 */
1066 if (ptev & _PAGE_INVALID) {
1067 res |= 2;
1068 if (pgstev & _PGSTE_GPS_ZERO)
1069 res |= 1;
1070 }
1071 if (pgstev & _PGSTE_GPS_NODAT)
1072 res |= 0x20;
1073 vcpu->run->s.regs.gprs[r1] = res;
1074 /*
1075 * It is possible that all the normal 511 slots were full, in which case
1076 * we will now write in the 512th slot, which is reserved for host use.
1077 * In both cases we let the normal essa handling code process all the
1078 * slots, including the reserved one, if needed.
1079 */
1080 if (nappended > 0) {
1081 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
1082 cbrlo[entries] = gfn << PAGE_SHIFT;
1083 }
1084
1085 if (orc) {
1086 struct kvm_memory_slot *ms = gfn_to_memslot(vcpu->kvm, gfn);
1087
1088 /* Increment only if we are really flipping the bit */
1089 if (ms && !test_and_set_bit(gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
1090 atomic64_inc(&vcpu->kvm->arch.cmma_dirty_pages);
1091 }
1092
1093 return nappended;
1094 }
1095
handle_essa(struct kvm_vcpu * vcpu)1096 static int handle_essa(struct kvm_vcpu *vcpu)
1097 {
1098 /* entries expected to be 1FF */
1099 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1100 unsigned long *cbrlo;
1101 struct gmap *gmap;
1102 int i, orc;
1103
1104 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
1105 gmap = vcpu->arch.gmap;
1106 vcpu->stat.instruction_essa++;
1107 if (!vcpu->kvm->arch.use_cmma)
1108 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1109
1110 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1111 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1112 /* Check for invalid operation request code */
1113 orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
1114 /* ORCs 0-6 are always valid */
1115 if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
1116 : ESSA_SET_STABLE_IF_RESIDENT))
1117 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1118
1119 if (!vcpu->kvm->arch.migration_mode) {
1120 /*
1121 * CMMA is enabled in the KVM settings, but is disabled in
1122 * the SIE block and in the mm_context, and we are not doing
1123 * a migration. Enable CMMA in the mm_context.
1124 * Since we need to take a write lock to write to the context
1125 * to avoid races with storage keys handling, we check if the
1126 * value really needs to be written to; if the value is
1127 * already correct, we do nothing and avoid the lock.
1128 */
1129 if (vcpu->kvm->mm->context.uses_cmm == 0) {
1130 down_write(&vcpu->kvm->mm->mmap_sem);
1131 vcpu->kvm->mm->context.uses_cmm = 1;
1132 up_write(&vcpu->kvm->mm->mmap_sem);
1133 }
1134 /*
1135 * If we are here, we are supposed to have CMMA enabled in
1136 * the SIE block. Enabling CMMA works on a per-CPU basis,
1137 * while the context use_cmma flag is per process.
1138 * It's possible that the context flag is enabled and the
1139 * SIE flag is not, so we set the flag always; if it was
1140 * already set, nothing changes, otherwise we enable it
1141 * on this CPU too.
1142 */
1143 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
1144 /* Retry the ESSA instruction */
1145 kvm_s390_retry_instr(vcpu);
1146 } else {
1147 int srcu_idx;
1148
1149 down_read(&vcpu->kvm->mm->mmap_sem);
1150 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1151 i = __do_essa(vcpu, orc);
1152 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1153 up_read(&vcpu->kvm->mm->mmap_sem);
1154 if (i < 0)
1155 return i;
1156 /* Account for the possible extra cbrl entry */
1157 entries += i;
1158 }
1159 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
1160 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
1161 down_read(&gmap->mm->mmap_sem);
1162 for (i = 0; i < entries; ++i)
1163 __gmap_zap(gmap, cbrlo[i]);
1164 up_read(&gmap->mm->mmap_sem);
1165 return 0;
1166 }
1167
kvm_s390_handle_b9(struct kvm_vcpu * vcpu)1168 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
1169 {
1170 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1171 case 0x8a:
1172 case 0x8e:
1173 case 0x8f:
1174 return handle_ipte_interlock(vcpu);
1175 case 0x8d:
1176 return handle_epsw(vcpu);
1177 case 0xab:
1178 return handle_essa(vcpu);
1179 case 0xaf:
1180 return handle_pfmf(vcpu);
1181 default:
1182 return -EOPNOTSUPP;
1183 }
1184 }
1185
kvm_s390_handle_lctl(struct kvm_vcpu * vcpu)1186 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1187 {
1188 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1189 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1190 int reg, rc, nr_regs;
1191 u32 ctl_array[16];
1192 u64 ga;
1193 u8 ar;
1194
1195 vcpu->stat.instruction_lctl++;
1196
1197 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1198 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1199
1200 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1201
1202 if (ga & 3)
1203 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1204
1205 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1206 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
1207
1208 nr_regs = ((reg3 - reg1) & 0xf) + 1;
1209 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1210 if (rc)
1211 return kvm_s390_inject_prog_cond(vcpu, rc);
1212 reg = reg1;
1213 nr_regs = 0;
1214 do {
1215 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
1216 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
1217 if (reg == reg3)
1218 break;
1219 reg = (reg + 1) % 16;
1220 } while (1);
1221 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1222 return 0;
1223 }
1224
kvm_s390_handle_stctl(struct kvm_vcpu * vcpu)1225 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1226 {
1227 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1228 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1229 int reg, rc, nr_regs;
1230 u32 ctl_array[16];
1231 u64 ga;
1232 u8 ar;
1233
1234 vcpu->stat.instruction_stctl++;
1235
1236 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1237 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1238
1239 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1240
1241 if (ga & 3)
1242 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1243
1244 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1245 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1246
1247 reg = reg1;
1248 nr_regs = 0;
1249 do {
1250 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1251 if (reg == reg3)
1252 break;
1253 reg = (reg + 1) % 16;
1254 } while (1);
1255 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1256 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1257 }
1258
handle_lctlg(struct kvm_vcpu * vcpu)1259 static int handle_lctlg(struct kvm_vcpu *vcpu)
1260 {
1261 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1262 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1263 int reg, rc, nr_regs;
1264 u64 ctl_array[16];
1265 u64 ga;
1266 u8 ar;
1267
1268 vcpu->stat.instruction_lctlg++;
1269
1270 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1271 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1272
1273 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1274
1275 if (ga & 7)
1276 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1277
1278 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1279 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
1280
1281 nr_regs = ((reg3 - reg1) & 0xf) + 1;
1282 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1283 if (rc)
1284 return kvm_s390_inject_prog_cond(vcpu, rc);
1285 reg = reg1;
1286 nr_regs = 0;
1287 do {
1288 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
1289 if (reg == reg3)
1290 break;
1291 reg = (reg + 1) % 16;
1292 } while (1);
1293 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1294 return 0;
1295 }
1296
handle_stctg(struct kvm_vcpu * vcpu)1297 static int handle_stctg(struct kvm_vcpu *vcpu)
1298 {
1299 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1300 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1301 int reg, rc, nr_regs;
1302 u64 ctl_array[16];
1303 u64 ga;
1304 u8 ar;
1305
1306 vcpu->stat.instruction_stctg++;
1307
1308 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1309 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1310
1311 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1312
1313 if (ga & 7)
1314 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1315
1316 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1317 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1318
1319 reg = reg1;
1320 nr_regs = 0;
1321 do {
1322 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1323 if (reg == reg3)
1324 break;
1325 reg = (reg + 1) % 16;
1326 } while (1);
1327 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1328 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1329 }
1330
kvm_s390_handle_eb(struct kvm_vcpu * vcpu)1331 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
1332 {
1333 switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
1334 case 0x25:
1335 return handle_stctg(vcpu);
1336 case 0x2f:
1337 return handle_lctlg(vcpu);
1338 case 0x60:
1339 case 0x61:
1340 case 0x62:
1341 return handle_ri(vcpu);
1342 default:
1343 return -EOPNOTSUPP;
1344 }
1345 }
1346
handle_tprot(struct kvm_vcpu * vcpu)1347 static int handle_tprot(struct kvm_vcpu *vcpu)
1348 {
1349 u64 address1, address2;
1350 unsigned long hva, gpa;
1351 int ret = 0, cc = 0;
1352 bool writable;
1353 u8 ar;
1354
1355 vcpu->stat.instruction_tprot++;
1356
1357 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1358 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1359
1360 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
1361
1362 /* we only handle the Linux memory detection case:
1363 * access key == 0
1364 * everything else goes to userspace. */
1365 if (address2 & 0xf0)
1366 return -EOPNOTSUPP;
1367 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1368 ipte_lock(vcpu);
1369 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
1370 if (ret == PGM_PROTECTION) {
1371 /* Write protected? Try again with read-only... */
1372 cc = 1;
1373 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1374 GACC_FETCH);
1375 }
1376 if (ret) {
1377 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1378 ret = kvm_s390_inject_program_int(vcpu, ret);
1379 } else if (ret > 0) {
1380 /* Translation not available */
1381 kvm_s390_set_psw_cc(vcpu, 3);
1382 ret = 0;
1383 }
1384 goto out_unlock;
1385 }
1386
1387 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1388 if (kvm_is_error_hva(hva)) {
1389 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1390 } else {
1391 if (!writable)
1392 cc = 1; /* Write not permitted ==> read-only */
1393 kvm_s390_set_psw_cc(vcpu, cc);
1394 /* Note: CC2 only occurs for storage keys (not supported yet) */
1395 }
1396 out_unlock:
1397 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1398 ipte_unlock(vcpu);
1399 return ret;
1400 }
1401
kvm_s390_handle_e5(struct kvm_vcpu * vcpu)1402 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1403 {
1404 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1405 case 0x01:
1406 return handle_tprot(vcpu);
1407 default:
1408 return -EOPNOTSUPP;
1409 }
1410 }
1411
handle_sckpf(struct kvm_vcpu * vcpu)1412 static int handle_sckpf(struct kvm_vcpu *vcpu)
1413 {
1414 u32 value;
1415
1416 vcpu->stat.instruction_sckpf++;
1417
1418 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1419 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1420
1421 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1422 return kvm_s390_inject_program_int(vcpu,
1423 PGM_SPECIFICATION);
1424
1425 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1426 vcpu->arch.sie_block->todpr = value;
1427
1428 return 0;
1429 }
1430
handle_ptff(struct kvm_vcpu * vcpu)1431 static int handle_ptff(struct kvm_vcpu *vcpu)
1432 {
1433 vcpu->stat.instruction_ptff++;
1434
1435 /* we don't emulate any control instructions yet */
1436 kvm_s390_set_psw_cc(vcpu, 3);
1437 return 0;
1438 }
1439
kvm_s390_handle_01(struct kvm_vcpu * vcpu)1440 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1441 {
1442 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1443 case 0x04:
1444 return handle_ptff(vcpu);
1445 case 0x07:
1446 return handle_sckpf(vcpu);
1447 default:
1448 return -EOPNOTSUPP;
1449 }
1450 }
1451