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