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