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