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