1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * handling privileged instructions
4 *
5 * Copyright IBM Corp. 2008, 2020
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 #include <linux/pgtable.h>
17
18 #include <asm/asm-offsets.h>
19 #include <asm/facility.h>
20 #include <asm/current.h>
21 #include <asm/debug.h>
22 #include <asm/ebcdic.h>
23 #include <asm/sysinfo.h>
24 #include <asm/page-states.h>
25 #include <asm/gmap.h>
26 #include <asm/io.h>
27 #include <asm/ptrace.h>
28 #include <asm/sclp.h>
29 #include <asm/ap.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 mmap_read_lock(current->mm);
273 rc = get_guest_storage_key(current->mm, vmaddr, &key);
274
275 if (rc) {
276 rc = fixup_user_fault(current->mm, vmaddr,
277 FAULT_FLAG_WRITE, &unlocked);
278 if (!rc) {
279 mmap_read_unlock(current->mm);
280 goto retry;
281 }
282 }
283 mmap_read_unlock(current->mm);
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 mmap_read_lock(current->mm);
320 rc = reset_guest_reference_bit(current->mm, vmaddr);
321 if (rc < 0) {
322 rc = fixup_user_fault(current->mm, vmaddr,
323 FAULT_FLAG_WRITE, &unlocked);
324 if (!rc) {
325 mmap_read_unlock(current->mm);
326 goto retry;
327 }
328 }
329 mmap_read_unlock(current->mm);
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 mmap_read_lock(current->mm);
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->mm, vmaddr,
394 FAULT_FLAG_WRITE, &unlocked);
395 rc = !rc ? -EAGAIN : rc;
396 }
397 mmap_read_unlock(current->mm);
398 if (rc == -EFAULT)
399 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
400 if (rc == -EAGAIN)
401 continue;
402 if (rc < 0)
403 return rc;
404 start += PAGE_SIZE;
405 }
406
407 if (m3 & (SSKE_MC | SSKE_MR)) {
408 if (m3 & SSKE_MB) {
409 /* skey in reg1 is unpredictable */
410 kvm_s390_set_psw_cc(vcpu, 3);
411 } else {
412 kvm_s390_set_psw_cc(vcpu, rc);
413 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
414 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
415 }
416 }
417 if (m3 & SSKE_MB) {
418 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
419 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
420 else
421 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
422 end = kvm_s390_logical_to_effective(vcpu, end);
423 vcpu->run->s.regs.gprs[reg2] |= end;
424 }
425 return 0;
426 }
427
handle_ipte_interlock(struct kvm_vcpu * vcpu)428 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
429 {
430 vcpu->stat.instruction_ipte_interlock++;
431 if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
432 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
433 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
434 kvm_s390_retry_instr(vcpu);
435 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
436 return 0;
437 }
438
handle_test_block(struct kvm_vcpu * vcpu)439 static int handle_test_block(struct kvm_vcpu *vcpu)
440 {
441 gpa_t addr;
442 int reg2;
443
444 vcpu->stat.instruction_tb++;
445
446 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
447 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
448
449 kvm_s390_get_regs_rre(vcpu, NULL, ®2);
450 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
451 addr = kvm_s390_logical_to_effective(vcpu, addr);
452 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
453 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
454 addr = kvm_s390_real_to_abs(vcpu, addr);
455
456 if (kvm_is_error_gpa(vcpu->kvm, addr))
457 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
458 /*
459 * We don't expect errors on modern systems, and do not care
460 * about storage keys (yet), so let's just clear the page.
461 */
462 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
463 return -EFAULT;
464 kvm_s390_set_psw_cc(vcpu, 0);
465 vcpu->run->s.regs.gprs[0] = 0;
466 return 0;
467 }
468
handle_tpi(struct kvm_vcpu * vcpu)469 static int handle_tpi(struct kvm_vcpu *vcpu)
470 {
471 struct kvm_s390_interrupt_info *inti;
472 unsigned long len;
473 u32 tpi_data[3];
474 int rc;
475 u64 addr;
476 u8 ar;
477
478 vcpu->stat.instruction_tpi++;
479
480 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
481 if (addr & 3)
482 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
483
484 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
485 if (!inti) {
486 kvm_s390_set_psw_cc(vcpu, 0);
487 return 0;
488 }
489
490 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
491 tpi_data[1] = inti->io.io_int_parm;
492 tpi_data[2] = inti->io.io_int_word;
493 if (addr) {
494 /*
495 * Store the two-word I/O interruption code into the
496 * provided area.
497 */
498 len = sizeof(tpi_data) - 4;
499 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
500 if (rc) {
501 rc = kvm_s390_inject_prog_cond(vcpu, rc);
502 goto reinject_interrupt;
503 }
504 } else {
505 /*
506 * Store the three-word I/O interruption code into
507 * the appropriate lowcore area.
508 */
509 len = sizeof(tpi_data);
510 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
511 /* failed writes to the low core are not recoverable */
512 rc = -EFAULT;
513 goto reinject_interrupt;
514 }
515 }
516
517 /* irq was successfully handed to the guest */
518 kfree(inti);
519 kvm_s390_set_psw_cc(vcpu, 1);
520 return 0;
521 reinject_interrupt:
522 /*
523 * If we encounter a problem storing the interruption code, the
524 * instruction is suppressed from the guest's view: reinject the
525 * interrupt.
526 */
527 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
528 kfree(inti);
529 rc = -EFAULT;
530 }
531 /* don't set the cc, a pgm irq was injected or we drop to user space */
532 return rc ? -EFAULT : 0;
533 }
534
handle_tsch(struct kvm_vcpu * vcpu)535 static int handle_tsch(struct kvm_vcpu *vcpu)
536 {
537 struct kvm_s390_interrupt_info *inti = NULL;
538 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
539
540 vcpu->stat.instruction_tsch++;
541
542 /* a valid schid has at least one bit set */
543 if (vcpu->run->s.regs.gprs[1])
544 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
545 vcpu->run->s.regs.gprs[1]);
546
547 /*
548 * Prepare exit to userspace.
549 * We indicate whether we dequeued a pending I/O interrupt
550 * so that userspace can re-inject it if the instruction gets
551 * a program check. While this may re-order the pending I/O
552 * interrupts, this is no problem since the priority is kept
553 * intact.
554 */
555 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
556 vcpu->run->s390_tsch.dequeued = !!inti;
557 if (inti) {
558 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
559 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
560 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
561 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
562 }
563 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
564 kfree(inti);
565 return -EREMOTE;
566 }
567
handle_io_inst(struct kvm_vcpu * vcpu)568 static int handle_io_inst(struct kvm_vcpu *vcpu)
569 {
570 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
571
572 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
573 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
574
575 if (vcpu->kvm->arch.css_support) {
576 /*
577 * Most I/O instructions will be handled by userspace.
578 * Exceptions are tpi and the interrupt portion of tsch.
579 */
580 if (vcpu->arch.sie_block->ipa == 0xb236)
581 return handle_tpi(vcpu);
582 if (vcpu->arch.sie_block->ipa == 0xb235)
583 return handle_tsch(vcpu);
584 /* Handle in userspace. */
585 vcpu->stat.instruction_io_other++;
586 return -EOPNOTSUPP;
587 } else {
588 /*
589 * Set condition code 3 to stop the guest from issuing channel
590 * I/O instructions.
591 */
592 kvm_s390_set_psw_cc(vcpu, 3);
593 return 0;
594 }
595 }
596
597 /*
598 * handle_pqap: Handling pqap interception
599 * @vcpu: the vcpu having issue the pqap instruction
600 *
601 * We now support PQAP/AQIC instructions and we need to correctly
602 * answer the guest even if no dedicated driver's hook is available.
603 *
604 * The intercepting code calls a dedicated callback for this instruction
605 * if a driver did register one in the CRYPTO satellite of the
606 * SIE block.
607 *
608 * If no callback is available, the queues are not available, return this
609 * response code to the caller and set CC to 3.
610 * Else return the response code returned by the callback.
611 */
handle_pqap(struct kvm_vcpu * vcpu)612 static int handle_pqap(struct kvm_vcpu *vcpu)
613 {
614 struct ap_queue_status status = {};
615 unsigned long reg0;
616 int ret;
617 uint8_t fc;
618
619 /* Verify that the AP instruction are available */
620 if (!ap_instructions_available())
621 return -EOPNOTSUPP;
622 /* Verify that the guest is allowed to use AP instructions */
623 if (!(vcpu->arch.sie_block->eca & ECA_APIE))
624 return -EOPNOTSUPP;
625 /*
626 * The only possibly intercepted functions when AP instructions are
627 * available for the guest are AQIC and TAPQ with the t bit set
628 * since we do not set IC.3 (FIII) we currently will only intercept
629 * the AQIC function code.
630 * Note: running nested under z/VM can result in intercepts for other
631 * function codes, e.g. PQAP(QCI). We do not support this and bail out.
632 */
633 reg0 = vcpu->run->s.regs.gprs[0];
634 fc = (reg0 >> 24) & 0xff;
635 if (fc != 0x03)
636 return -EOPNOTSUPP;
637
638 /* PQAP instruction is allowed for guest kernel only */
639 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
640 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
641
642 /* Common PQAP instruction specification exceptions */
643 /* bits 41-47 must all be zeros */
644 if (reg0 & 0x007f0000UL)
645 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
646 /* APFT not install and T bit set */
647 if (!test_kvm_facility(vcpu->kvm, 15) && (reg0 & 0x00800000UL))
648 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
649 /* APXA not installed and APID greater 64 or APQI greater 16 */
650 if (!(vcpu->kvm->arch.crypto.crycbd & 0x02) && (reg0 & 0x0000c0f0UL))
651 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
652
653 /* AQIC function code specific exception */
654 /* facility 65 not present for AQIC function code */
655 if (!test_kvm_facility(vcpu->kvm, 65))
656 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
657
658 /*
659 * Verify that the hook callback is registered, lock the owner
660 * and call the hook.
661 */
662 if (vcpu->kvm->arch.crypto.pqap_hook) {
663 if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
664 return -EOPNOTSUPP;
665 ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
666 module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
667 if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
668 kvm_s390_set_psw_cc(vcpu, 3);
669 return ret;
670 }
671 /*
672 * A vfio_driver must register a hook.
673 * No hook means no driver to enable the SIE CRYCB and no queues.
674 * We send this response to the guest.
675 */
676 status.response_code = 0x01;
677 memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
678 kvm_s390_set_psw_cc(vcpu, 3);
679 return 0;
680 }
681
handle_stfl(struct kvm_vcpu * vcpu)682 static int handle_stfl(struct kvm_vcpu *vcpu)
683 {
684 int rc;
685 unsigned int fac;
686
687 vcpu->stat.instruction_stfl++;
688
689 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
690 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
691
692 /*
693 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
694 * into a u32 memory representation. They will remain bits 0-31.
695 */
696 fac = *vcpu->kvm->arch.model.fac_list >> 32;
697 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
698 &fac, sizeof(fac));
699 if (rc)
700 return rc;
701 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
702 trace_kvm_s390_handle_stfl(vcpu, fac);
703 return 0;
704 }
705
706 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
707 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
708 #define PSW_ADDR_24 0x0000000000ffffffUL
709 #define PSW_ADDR_31 0x000000007fffffffUL
710
is_valid_psw(psw_t * psw)711 int is_valid_psw(psw_t *psw)
712 {
713 if (psw->mask & PSW_MASK_UNASSIGNED)
714 return 0;
715 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
716 if (psw->addr & ~PSW_ADDR_31)
717 return 0;
718 }
719 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
720 return 0;
721 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
722 return 0;
723 if (psw->addr & 1)
724 return 0;
725 return 1;
726 }
727
kvm_s390_handle_lpsw(struct kvm_vcpu * vcpu)728 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
729 {
730 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
731 psw_compat_t new_psw;
732 u64 addr;
733 int rc;
734 u8 ar;
735
736 vcpu->stat.instruction_lpsw++;
737
738 if (gpsw->mask & PSW_MASK_PSTATE)
739 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
740
741 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
742 if (addr & 7)
743 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
744
745 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
746 if (rc)
747 return kvm_s390_inject_prog_cond(vcpu, rc);
748 if (!(new_psw.mask & PSW32_MASK_BASE))
749 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
750 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
751 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
752 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
753 if (!is_valid_psw(gpsw))
754 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
755 return 0;
756 }
757
handle_lpswe(struct kvm_vcpu * vcpu)758 static int handle_lpswe(struct kvm_vcpu *vcpu)
759 {
760 psw_t new_psw;
761 u64 addr;
762 int rc;
763 u8 ar;
764
765 vcpu->stat.instruction_lpswe++;
766
767 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
768 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
769
770 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
771 if (addr & 7)
772 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
773 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
774 if (rc)
775 return kvm_s390_inject_prog_cond(vcpu, rc);
776 vcpu->arch.sie_block->gpsw = new_psw;
777 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
778 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
779 return 0;
780 }
781
handle_stidp(struct kvm_vcpu * vcpu)782 static int handle_stidp(struct kvm_vcpu *vcpu)
783 {
784 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
785 u64 operand2;
786 int rc;
787 u8 ar;
788
789 vcpu->stat.instruction_stidp++;
790
791 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
792 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
793
794 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
795
796 if (operand2 & 7)
797 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
798
799 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
800 if (rc)
801 return kvm_s390_inject_prog_cond(vcpu, rc);
802
803 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
804 return 0;
805 }
806
handle_stsi_3_2_2(struct kvm_vcpu * vcpu,struct sysinfo_3_2_2 * mem)807 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
808 {
809 int cpus = 0;
810 int n;
811
812 cpus = atomic_read(&vcpu->kvm->online_vcpus);
813
814 /* deal with other level 3 hypervisors */
815 if (stsi(mem, 3, 2, 2))
816 mem->count = 0;
817 if (mem->count < 8)
818 mem->count++;
819 for (n = mem->count - 1; n > 0 ; n--)
820 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
821
822 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
823 mem->vm[0].cpus_total = cpus;
824 mem->vm[0].cpus_configured = cpus;
825 mem->vm[0].cpus_standby = 0;
826 mem->vm[0].cpus_reserved = 0;
827 mem->vm[0].caf = 1000;
828 memcpy(mem->vm[0].name, "KVMguest", 8);
829 ASCEBC(mem->vm[0].name, 8);
830 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
831 ASCEBC(mem->vm[0].cpi, 16);
832 }
833
insert_stsi_usr_data(struct kvm_vcpu * vcpu,u64 addr,u8 ar,u8 fc,u8 sel1,u16 sel2)834 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
835 u8 fc, u8 sel1, u16 sel2)
836 {
837 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
838 vcpu->run->s390_stsi.addr = addr;
839 vcpu->run->s390_stsi.ar = ar;
840 vcpu->run->s390_stsi.fc = fc;
841 vcpu->run->s390_stsi.sel1 = sel1;
842 vcpu->run->s390_stsi.sel2 = sel2;
843 }
844
handle_stsi(struct kvm_vcpu * vcpu)845 static int handle_stsi(struct kvm_vcpu *vcpu)
846 {
847 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
848 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
849 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
850 unsigned long mem = 0;
851 u64 operand2;
852 int rc = 0;
853 u8 ar;
854
855 vcpu->stat.instruction_stsi++;
856 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
857
858 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
859 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
860
861 if (fc > 3) {
862 kvm_s390_set_psw_cc(vcpu, 3);
863 return 0;
864 }
865
866 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
867 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
868 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
869
870 if (fc == 0) {
871 vcpu->run->s.regs.gprs[0] = 3 << 28;
872 kvm_s390_set_psw_cc(vcpu, 0);
873 return 0;
874 }
875
876 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
877
878 if (!kvm_s390_pv_cpu_is_protected(vcpu) && (operand2 & 0xfff))
879 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
880
881 switch (fc) {
882 case 1: /* same handling for 1 and 2 */
883 case 2:
884 mem = get_zeroed_page(GFP_KERNEL);
885 if (!mem)
886 goto out_no_data;
887 if (stsi((void *) mem, fc, sel1, sel2))
888 goto out_no_data;
889 break;
890 case 3:
891 if (sel1 != 2 || sel2 != 2)
892 goto out_no_data;
893 mem = get_zeroed_page(GFP_KERNEL);
894 if (!mem)
895 goto out_no_data;
896 handle_stsi_3_2_2(vcpu, (void *) mem);
897 break;
898 }
899 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
900 memcpy((void *)sida_origin(vcpu->arch.sie_block), (void *)mem,
901 PAGE_SIZE);
902 rc = 0;
903 } else {
904 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
905 }
906 if (rc) {
907 rc = kvm_s390_inject_prog_cond(vcpu, rc);
908 goto out;
909 }
910 if (vcpu->kvm->arch.user_stsi) {
911 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
912 rc = -EREMOTE;
913 }
914 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
915 free_page(mem);
916 kvm_s390_set_psw_cc(vcpu, 0);
917 vcpu->run->s.regs.gprs[0] = 0;
918 return rc;
919 out_no_data:
920 kvm_s390_set_psw_cc(vcpu, 3);
921 out:
922 free_page(mem);
923 return rc;
924 }
925
kvm_s390_handle_b2(struct kvm_vcpu * vcpu)926 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
927 {
928 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
929 case 0x02:
930 return handle_stidp(vcpu);
931 case 0x04:
932 return handle_set_clock(vcpu);
933 case 0x10:
934 return handle_set_prefix(vcpu);
935 case 0x11:
936 return handle_store_prefix(vcpu);
937 case 0x12:
938 return handle_store_cpu_address(vcpu);
939 case 0x14:
940 return kvm_s390_handle_vsie(vcpu);
941 case 0x21:
942 case 0x50:
943 return handle_ipte_interlock(vcpu);
944 case 0x29:
945 return handle_iske(vcpu);
946 case 0x2a:
947 return handle_rrbe(vcpu);
948 case 0x2b:
949 return handle_sske(vcpu);
950 case 0x2c:
951 return handle_test_block(vcpu);
952 case 0x30:
953 case 0x31:
954 case 0x32:
955 case 0x33:
956 case 0x34:
957 case 0x35:
958 case 0x36:
959 case 0x37:
960 case 0x38:
961 case 0x39:
962 case 0x3a:
963 case 0x3b:
964 case 0x3c:
965 case 0x5f:
966 case 0x74:
967 case 0x76:
968 return handle_io_inst(vcpu);
969 case 0x56:
970 return handle_sthyi(vcpu);
971 case 0x7d:
972 return handle_stsi(vcpu);
973 case 0xaf:
974 return handle_pqap(vcpu);
975 case 0xb1:
976 return handle_stfl(vcpu);
977 case 0xb2:
978 return handle_lpswe(vcpu);
979 default:
980 return -EOPNOTSUPP;
981 }
982 }
983
handle_epsw(struct kvm_vcpu * vcpu)984 static int handle_epsw(struct kvm_vcpu *vcpu)
985 {
986 int reg1, reg2;
987
988 vcpu->stat.instruction_epsw++;
989
990 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
991
992 /* This basically extracts the mask half of the psw. */
993 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
994 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
995 if (reg2) {
996 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
997 vcpu->run->s.regs.gprs[reg2] |=
998 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
999 }
1000 return 0;
1001 }
1002
1003 #define PFMF_RESERVED 0xfffc0101UL
1004 #define PFMF_SK 0x00020000UL
1005 #define PFMF_CF 0x00010000UL
1006 #define PFMF_UI 0x00008000UL
1007 #define PFMF_FSC 0x00007000UL
1008 #define PFMF_NQ 0x00000800UL
1009 #define PFMF_MR 0x00000400UL
1010 #define PFMF_MC 0x00000200UL
1011 #define PFMF_KEY 0x000000feUL
1012
handle_pfmf(struct kvm_vcpu * vcpu)1013 static int handle_pfmf(struct kvm_vcpu *vcpu)
1014 {
1015 bool mr = false, mc = false, nq;
1016 int reg1, reg2;
1017 unsigned long start, end;
1018 unsigned char key;
1019
1020 vcpu->stat.instruction_pfmf++;
1021
1022 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
1023
1024 if (!test_kvm_facility(vcpu->kvm, 8))
1025 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1026
1027 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1028 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1029
1030 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
1031 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1032
1033 /* Only provide non-quiescing support if enabled for the guest */
1034 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
1035 !test_kvm_facility(vcpu->kvm, 14))
1036 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1037
1038 /* Only provide conditional-SSKE support if enabled for the guest */
1039 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
1040 test_kvm_facility(vcpu->kvm, 10)) {
1041 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
1042 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
1043 }
1044
1045 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
1046 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
1047 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
1048 start = kvm_s390_logical_to_effective(vcpu, start);
1049
1050 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
1051 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
1052 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
1053 }
1054
1055 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
1056 case 0x00000000:
1057 /* only 4k frames specify a real address */
1058 start = kvm_s390_real_to_abs(vcpu, start);
1059 end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1060 break;
1061 case 0x00001000:
1062 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
1063 break;
1064 case 0x00002000:
1065 /* only support 2G frame size if EDAT2 is available and we are
1066 not in 24-bit addressing mode */
1067 if (!test_kvm_facility(vcpu->kvm, 78) ||
1068 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
1069 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1070 end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1);
1071 break;
1072 default:
1073 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1074 }
1075
1076 while (start != end) {
1077 unsigned long vmaddr;
1078 bool unlocked = false;
1079
1080 /* Translate guest address to host address */
1081 vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
1082 if (kvm_is_error_hva(vmaddr))
1083 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1084
1085 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
1086 if (kvm_clear_guest(vcpu->kvm, start, PAGE_SIZE))
1087 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1088 }
1089
1090 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
1091 int rc = kvm_s390_skey_check_enable(vcpu);
1092
1093 if (rc)
1094 return rc;
1095 mmap_read_lock(current->mm);
1096 rc = cond_set_guest_storage_key(current->mm, vmaddr,
1097 key, NULL, nq, mr, mc);
1098 if (rc < 0) {
1099 rc = fixup_user_fault(current->mm, vmaddr,
1100 FAULT_FLAG_WRITE, &unlocked);
1101 rc = !rc ? -EAGAIN : rc;
1102 }
1103 mmap_read_unlock(current->mm);
1104 if (rc == -EFAULT)
1105 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1106 if (rc == -EAGAIN)
1107 continue;
1108 if (rc < 0)
1109 return rc;
1110 }
1111 start += PAGE_SIZE;
1112 }
1113 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
1114 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
1115 vcpu->run->s.regs.gprs[reg2] = end;
1116 } else {
1117 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
1118 end = kvm_s390_logical_to_effective(vcpu, end);
1119 vcpu->run->s.regs.gprs[reg2] |= end;
1120 }
1121 }
1122 return 0;
1123 }
1124
1125 /*
1126 * Must be called with relevant read locks held (kvm->mm->mmap_lock, kvm->srcu)
1127 */
__do_essa(struct kvm_vcpu * vcpu,const int orc)1128 static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc)
1129 {
1130 int r1, r2, nappended, entries;
1131 unsigned long gfn, hva, res, pgstev, ptev;
1132 unsigned long *cbrlo;
1133
1134 /*
1135 * We don't need to set SD.FPF.SK to 1 here, because if we have a
1136 * machine check here we either handle it or crash
1137 */
1138
1139 kvm_s390_get_regs_rre(vcpu, &r1, &r2);
1140 gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
1141 hva = gfn_to_hva(vcpu->kvm, gfn);
1142 entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1143
1144 if (kvm_is_error_hva(hva))
1145 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1146
1147 nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
1148 if (nappended < 0) {
1149 res = orc ? 0x10 : 0;
1150 vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
1151 return 0;
1152 }
1153 res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
1154 /*
1155 * Set the block-content state part of the result. 0 means resident, so
1156 * nothing to do if the page is valid. 2 is for preserved pages
1157 * (non-present and non-zero), and 3 for zero pages (non-present and
1158 * zero).
1159 */
1160 if (ptev & _PAGE_INVALID) {
1161 res |= 2;
1162 if (pgstev & _PGSTE_GPS_ZERO)
1163 res |= 1;
1164 }
1165 if (pgstev & _PGSTE_GPS_NODAT)
1166 res |= 0x20;
1167 vcpu->run->s.regs.gprs[r1] = res;
1168 /*
1169 * It is possible that all the normal 511 slots were full, in which case
1170 * we will now write in the 512th slot, which is reserved for host use.
1171 * In both cases we let the normal essa handling code process all the
1172 * slots, including the reserved one, if needed.
1173 */
1174 if (nappended > 0) {
1175 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
1176 cbrlo[entries] = gfn << PAGE_SHIFT;
1177 }
1178
1179 if (orc) {
1180 struct kvm_memory_slot *ms = gfn_to_memslot(vcpu->kvm, gfn);
1181
1182 /* Increment only if we are really flipping the bit */
1183 if (ms && !test_and_set_bit(gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
1184 atomic64_inc(&vcpu->kvm->arch.cmma_dirty_pages);
1185 }
1186
1187 return nappended;
1188 }
1189
handle_essa(struct kvm_vcpu * vcpu)1190 static int handle_essa(struct kvm_vcpu *vcpu)
1191 {
1192 /* entries expected to be 1FF */
1193 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1194 unsigned long *cbrlo;
1195 struct gmap *gmap;
1196 int i, orc;
1197
1198 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
1199 gmap = vcpu->arch.gmap;
1200 vcpu->stat.instruction_essa++;
1201 if (!vcpu->kvm->arch.use_cmma)
1202 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1203
1204 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1205 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1206 /* Check for invalid operation request code */
1207 orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
1208 /* ORCs 0-6 are always valid */
1209 if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
1210 : ESSA_SET_STABLE_IF_RESIDENT))
1211 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1212
1213 if (!vcpu->kvm->arch.migration_mode) {
1214 /*
1215 * CMMA is enabled in the KVM settings, but is disabled in
1216 * the SIE block and in the mm_context, and we are not doing
1217 * a migration. Enable CMMA in the mm_context.
1218 * Since we need to take a write lock to write to the context
1219 * to avoid races with storage keys handling, we check if the
1220 * value really needs to be written to; if the value is
1221 * already correct, we do nothing and avoid the lock.
1222 */
1223 if (vcpu->kvm->mm->context.uses_cmm == 0) {
1224 mmap_write_lock(vcpu->kvm->mm);
1225 vcpu->kvm->mm->context.uses_cmm = 1;
1226 mmap_write_unlock(vcpu->kvm->mm);
1227 }
1228 /*
1229 * If we are here, we are supposed to have CMMA enabled in
1230 * the SIE block. Enabling CMMA works on a per-CPU basis,
1231 * while the context use_cmma flag is per process.
1232 * It's possible that the context flag is enabled and the
1233 * SIE flag is not, so we set the flag always; if it was
1234 * already set, nothing changes, otherwise we enable it
1235 * on this CPU too.
1236 */
1237 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
1238 /* Retry the ESSA instruction */
1239 kvm_s390_retry_instr(vcpu);
1240 } else {
1241 int srcu_idx;
1242
1243 mmap_read_lock(vcpu->kvm->mm);
1244 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1245 i = __do_essa(vcpu, orc);
1246 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1247 mmap_read_unlock(vcpu->kvm->mm);
1248 if (i < 0)
1249 return i;
1250 /* Account for the possible extra cbrl entry */
1251 entries += i;
1252 }
1253 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
1254 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
1255 mmap_read_lock(gmap->mm);
1256 for (i = 0; i < entries; ++i)
1257 __gmap_zap(gmap, cbrlo[i]);
1258 mmap_read_unlock(gmap->mm);
1259 return 0;
1260 }
1261
kvm_s390_handle_b9(struct kvm_vcpu * vcpu)1262 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
1263 {
1264 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1265 case 0x8a:
1266 case 0x8e:
1267 case 0x8f:
1268 return handle_ipte_interlock(vcpu);
1269 case 0x8d:
1270 return handle_epsw(vcpu);
1271 case 0xab:
1272 return handle_essa(vcpu);
1273 case 0xaf:
1274 return handle_pfmf(vcpu);
1275 default:
1276 return -EOPNOTSUPP;
1277 }
1278 }
1279
kvm_s390_handle_lctl(struct kvm_vcpu * vcpu)1280 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1281 {
1282 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1283 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1284 int reg, rc, nr_regs;
1285 u32 ctl_array[16];
1286 u64 ga;
1287 u8 ar;
1288
1289 vcpu->stat.instruction_lctl++;
1290
1291 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1292 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1293
1294 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1295
1296 if (ga & 3)
1297 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1298
1299 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1300 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
1301
1302 nr_regs = ((reg3 - reg1) & 0xf) + 1;
1303 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1304 if (rc)
1305 return kvm_s390_inject_prog_cond(vcpu, rc);
1306 reg = reg1;
1307 nr_regs = 0;
1308 do {
1309 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
1310 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
1311 if (reg == reg3)
1312 break;
1313 reg = (reg + 1) % 16;
1314 } while (1);
1315 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1316 return 0;
1317 }
1318
kvm_s390_handle_stctl(struct kvm_vcpu * vcpu)1319 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1320 {
1321 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1322 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1323 int reg, rc, nr_regs;
1324 u32 ctl_array[16];
1325 u64 ga;
1326 u8 ar;
1327
1328 vcpu->stat.instruction_stctl++;
1329
1330 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1331 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1332
1333 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1334
1335 if (ga & 3)
1336 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1337
1338 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1339 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1340
1341 reg = reg1;
1342 nr_regs = 0;
1343 do {
1344 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1345 if (reg == reg3)
1346 break;
1347 reg = (reg + 1) % 16;
1348 } while (1);
1349 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1350 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1351 }
1352
handle_lctlg(struct kvm_vcpu * vcpu)1353 static int handle_lctlg(struct kvm_vcpu *vcpu)
1354 {
1355 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1356 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1357 int reg, rc, nr_regs;
1358 u64 ctl_array[16];
1359 u64 ga;
1360 u8 ar;
1361
1362 vcpu->stat.instruction_lctlg++;
1363
1364 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1365 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1366
1367 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1368
1369 if (ga & 7)
1370 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1371
1372 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1373 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
1374
1375 nr_regs = ((reg3 - reg1) & 0xf) + 1;
1376 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1377 if (rc)
1378 return kvm_s390_inject_prog_cond(vcpu, rc);
1379 reg = reg1;
1380 nr_regs = 0;
1381 do {
1382 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
1383 if (reg == reg3)
1384 break;
1385 reg = (reg + 1) % 16;
1386 } while (1);
1387 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1388 return 0;
1389 }
1390
handle_stctg(struct kvm_vcpu * vcpu)1391 static int handle_stctg(struct kvm_vcpu *vcpu)
1392 {
1393 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1394 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1395 int reg, rc, nr_regs;
1396 u64 ctl_array[16];
1397 u64 ga;
1398 u8 ar;
1399
1400 vcpu->stat.instruction_stctg++;
1401
1402 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1403 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1404
1405 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1406
1407 if (ga & 7)
1408 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1409
1410 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1411 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1412
1413 reg = reg1;
1414 nr_regs = 0;
1415 do {
1416 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1417 if (reg == reg3)
1418 break;
1419 reg = (reg + 1) % 16;
1420 } while (1);
1421 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1422 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1423 }
1424
kvm_s390_handle_eb(struct kvm_vcpu * vcpu)1425 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
1426 {
1427 switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
1428 case 0x25:
1429 return handle_stctg(vcpu);
1430 case 0x2f:
1431 return handle_lctlg(vcpu);
1432 case 0x60:
1433 case 0x61:
1434 case 0x62:
1435 return handle_ri(vcpu);
1436 default:
1437 return -EOPNOTSUPP;
1438 }
1439 }
1440
handle_tprot(struct kvm_vcpu * vcpu)1441 static int handle_tprot(struct kvm_vcpu *vcpu)
1442 {
1443 u64 address1, address2;
1444 unsigned long hva, gpa;
1445 int ret = 0, cc = 0;
1446 bool writable;
1447 u8 ar;
1448
1449 vcpu->stat.instruction_tprot++;
1450
1451 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1452 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1453
1454 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
1455
1456 /* we only handle the Linux memory detection case:
1457 * access key == 0
1458 * everything else goes to userspace. */
1459 if (address2 & 0xf0)
1460 return -EOPNOTSUPP;
1461 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1462 ipte_lock(vcpu);
1463 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
1464 if (ret == PGM_PROTECTION) {
1465 /* Write protected? Try again with read-only... */
1466 cc = 1;
1467 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1468 GACC_FETCH);
1469 }
1470 if (ret) {
1471 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1472 ret = kvm_s390_inject_program_int(vcpu, ret);
1473 } else if (ret > 0) {
1474 /* Translation not available */
1475 kvm_s390_set_psw_cc(vcpu, 3);
1476 ret = 0;
1477 }
1478 goto out_unlock;
1479 }
1480
1481 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1482 if (kvm_is_error_hva(hva)) {
1483 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1484 } else {
1485 if (!writable)
1486 cc = 1; /* Write not permitted ==> read-only */
1487 kvm_s390_set_psw_cc(vcpu, cc);
1488 /* Note: CC2 only occurs for storage keys (not supported yet) */
1489 }
1490 out_unlock:
1491 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1492 ipte_unlock(vcpu);
1493 return ret;
1494 }
1495
kvm_s390_handle_e5(struct kvm_vcpu * vcpu)1496 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1497 {
1498 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1499 case 0x01:
1500 return handle_tprot(vcpu);
1501 default:
1502 return -EOPNOTSUPP;
1503 }
1504 }
1505
handle_sckpf(struct kvm_vcpu * vcpu)1506 static int handle_sckpf(struct kvm_vcpu *vcpu)
1507 {
1508 u32 value;
1509
1510 vcpu->stat.instruction_sckpf++;
1511
1512 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1513 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1514
1515 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1516 return kvm_s390_inject_program_int(vcpu,
1517 PGM_SPECIFICATION);
1518
1519 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1520 vcpu->arch.sie_block->todpr = value;
1521
1522 return 0;
1523 }
1524
handle_ptff(struct kvm_vcpu * vcpu)1525 static int handle_ptff(struct kvm_vcpu *vcpu)
1526 {
1527 vcpu->stat.instruction_ptff++;
1528
1529 /* we don't emulate any control instructions yet */
1530 kvm_s390_set_psw_cc(vcpu, 3);
1531 return 0;
1532 }
1533
kvm_s390_handle_01(struct kvm_vcpu * vcpu)1534 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1535 {
1536 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1537 case 0x04:
1538 return handle_ptff(vcpu);
1539 case 0x07:
1540 return handle_sckpf(vcpu);
1541 default:
1542 return -EOPNOTSUPP;
1543 }
1544 }
1545