priv.c 36.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 * handling privileged instructions
4
 *
5
 * Copyright IBM Corp. 2008, 2013
6 7 8 9 10 11
 *
 *    Author(s): Carsten Otte <cotte@de.ibm.com>
 *               Christian Borntraeger <borntraeger@de.ibm.com>
 */

#include <linux/kvm.h>
12
#include <linux/gfp.h>
13
#include <linux/errno.h>
14
#include <linux/compat.h>
15 16
#include <linux/mm_types.h>

17
#include <asm/asm-offsets.h>
18
#include <asm/facility.h>
19 20 21 22
#include <asm/current.h>
#include <asm/debug.h>
#include <asm/ebcdic.h>
#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 29
#include <asm/ptrace.h>
#include <asm/compat.h>
30
#include <asm/sclp.h>
31 32
#include "gaccess.h"
#include "kvm-s390.h"
33
#include "trace.h"
34

F
Fan Zhang 已提交
35 36 37
static int handle_ri(struct kvm_vcpu *vcpu)
{
	if (test_kvm_facility(vcpu->kvm, 64)) {
38
		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
39
		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
F
Fan Zhang 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
		kvm_s390_retry_instr(vcpu);
		return 0;
	} else
		return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
}

int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
{
	if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
		return handle_ri(vcpu);
	else
		return -EOPNOTSUPP;
}

F
Fan Zhang 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static int handle_gs(struct kvm_vcpu *vcpu)
{
	if (test_kvm_facility(vcpu->kvm, 133)) {
		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
		preempt_disable();
		__ctl_set_bit(2, 4);
		current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
		restore_gs_cb(current->thread.gs_cb);
		preempt_enable();
		vcpu->arch.sie_block->ecb |= ECB_GS;
		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
		vcpu->arch.gs_enabled = 1;
		kvm_s390_retry_instr(vcpu);
		return 0;
	} else
		return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
}

int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
{
	int code = vcpu->arch.sie_block->ipb & 0xff;

	if (code == 0x49 || code == 0x4d)
		return handle_gs(vcpu);
	else
		return -EOPNOTSUPP;
}
81 82 83
/* Handle SCK (SET CLOCK) interception */
static int handle_set_clock(struct kvm_vcpu *vcpu)
{
84
	int rc;
85
	u8 ar;
86
	u64 op2, val;
87 88 89 90

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

91
	op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
92 93
	if (op2 & 7)	/* Operand must be on a doubleword boundary */
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
94
	rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
95 96
	if (rc)
		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 102 103 104

	kvm_s390_set_psw_cc(vcpu, 0);
	return 0;
}

105 106 107
static int handle_set_prefix(struct kvm_vcpu *vcpu)
{
	u64 operand2;
108 109
	u32 address;
	int rc;
110
	u8 ar;
111 112 113

	vcpu->stat.instruction_spx++;

114 115 116
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

117
	operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
118 119

	/* must be word boundary */
120 121
	if (operand2 & 3)
		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 126 127 128 129 130 131 132 133 134 135
	if (rc)
		return kvm_s390_inject_prog_cond(vcpu, rc);

	address &= 0x7fffe000u;

	/*
	 * Make sure the new value is valid memory. We only need to check the
	 * first page, since address is 8k aligned and memory pieces are always
	 * at least 1MB aligned and have at least a size of 1MB.
	 */
	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 141 142 143 144 145 146
	return 0;
}

static int handle_store_prefix(struct kvm_vcpu *vcpu)
{
	u64 operand2;
	u32 address;
147
	int rc;
148
	u8 ar;
149 150

	vcpu->stat.instruction_stpx++;
151

152 153 154
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

155
	operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
156 157

	/* must be word boundary */
158 159
	if (operand2 & 3)
		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 166
	if (rc)
		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 171 172 173 174
	return 0;
}

static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
{
175 176 177
	u16 vcpu_id = vcpu->vcpu_id;
	u64 ga;
	int rc;
178
	u8 ar;
179 180

	vcpu->stat.instruction_stap++;
181

182 183 184
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

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 192
	if (rc)
		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 197 198
	return 0;
}

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 206
	if (!(sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)) &&
	    !(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 212 213 214 215 216 217
	if (!rc) {
		if (atomic_read(&sie_block->cpuflags) & CPUSTAT_KSS)
			atomic_andnot(CPUSTAT_KSS, &sie_block->cpuflags);
		else
			sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE |
					     ICTL_RRBE);
	}
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 228
	if (rc)
		return rc;
229 230 231 232 233 234 235 236
	if (sclp.has_skey) {
		/* with storage-key facility, SIE interprets it for us */
		kvm_s390_retry_instr(vcpu);
		VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
		return -EAGAIN;
	}
	return 0;
}
237

238 239 240 241 242 243 244
static int handle_iske(struct kvm_vcpu *vcpu)
{
	unsigned long addr;
	unsigned char key;
	int reg1, reg2;
	int rc;

245 246 247
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	rc = try_handle_skey(vcpu);
	if (rc)
		return rc != -EAGAIN ? rc : 0;

	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);

	addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
	addr = kvm_s390_logical_to_effective(vcpu, addr);
	addr = kvm_s390_real_to_abs(vcpu, addr);
	addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
	if (kvm_is_error_hva(addr))
		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);

	down_read(&current->mm->mmap_sem);
	rc = get_guest_storage_key(current->mm, addr, &key);
	up_read(&current->mm->mmap_sem);
	if (rc)
		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
	vcpu->run->s.regs.gprs[reg1] &= ~0xff;
	vcpu->run->s.regs.gprs[reg1] |= key;
	return 0;
}

static int handle_rrbe(struct kvm_vcpu *vcpu)
{
	unsigned long addr;
	int reg1, reg2;
	int rc;

277 278 279
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	rc = try_handle_skey(vcpu);
	if (rc)
		return rc != -EAGAIN ? rc : 0;

	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);

	addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
	addr = kvm_s390_logical_to_effective(vcpu, addr);
	addr = kvm_s390_real_to_abs(vcpu, addr);
	addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
	if (kvm_is_error_hva(addr))
		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);

	down_read(&current->mm->mmap_sem);
	rc = reset_guest_reference_bit(current->mm, addr);
	up_read(&current->mm->mmap_sem);
	if (rc < 0)
		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);

	kvm_s390_set_psw_cc(vcpu, rc);
	return 0;
}

#define SSKE_NQ 0x8
#define SSKE_MR 0x4
#define SSKE_MC 0x2
#define SSKE_MB 0x1
static int handle_sske(struct kvm_vcpu *vcpu)
{
	unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
	unsigned long start, end;
	unsigned char key, oldkey;
	int reg1, reg2;
	int rc;

315 316 317
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	rc = try_handle_skey(vcpu);
	if (rc)
		return rc != -EAGAIN ? rc : 0;

	if (!test_kvm_facility(vcpu->kvm, 8))
		m3 &= ~SSKE_MB;
	if (!test_kvm_facility(vcpu->kvm, 10))
		m3 &= ~(SSKE_MC | SSKE_MR);
	if (!test_kvm_facility(vcpu->kvm, 14))
		m3 &= ~SSKE_NQ;

	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);

	key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
	start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
	start = kvm_s390_logical_to_effective(vcpu, start);
	if (m3 & SSKE_MB) {
		/* start already designates an absolute address */
336
		end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	} else {
		start = kvm_s390_real_to_abs(vcpu, start);
		end = start + PAGE_SIZE;
	}

	while (start != end) {
		unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));

		if (kvm_is_error_hva(addr))
			return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);

		down_read(&current->mm->mmap_sem);
		rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
						m3 & SSKE_NQ, m3 & SSKE_MR,
						m3 & SSKE_MC);
		up_read(&current->mm->mmap_sem);
		if (rc < 0)
			return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
		start += PAGE_SIZE;
356
	}
357 358 359 360 361 362 363 364 365 366 367 368

	if (m3 & (SSKE_MC | SSKE_MR)) {
		if (m3 & SSKE_MB) {
			/* skey in reg1 is unpredictable */
			kvm_s390_set_psw_cc(vcpu, 3);
		} else {
			kvm_s390_set_psw_cc(vcpu, rc);
			vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
			vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
		}
	}
	if (m3 & SSKE_MB) {
H
Heiko Carstens 已提交
369
		if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
370 371 372 373 374 375
			vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
		else
			vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
		end = kvm_s390_logical_to_effective(vcpu, end);
		vcpu->run->s.regs.gprs[reg2] |= end;
	}
376 377 378
	return 0;
}

379 380 381
static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
{
	vcpu->stat.instruction_ipte_interlock++;
382
	if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
383 384
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
	wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
385
	kvm_s390_retry_instr(vcpu);
386 387 388 389
	VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
	return 0;
}

T
Thomas Huth 已提交
390 391 392 393 394 395 396 397 398 399
static int handle_test_block(struct kvm_vcpu *vcpu)
{
	gpa_t addr;
	int reg2;

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

	kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
	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);
T
Thomas Huth 已提交
403 404
	addr = kvm_s390_real_to_abs(vcpu, addr);

405
	if (kvm_is_error_gpa(vcpu->kvm, addr))
T
Thomas Huth 已提交
406 407 408 409 410
		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
	/*
	 * We don't expect errors on modern systems, and do not care
	 * about storage keys (yet), so let's just clear the page.
	 */
411
	if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
T
Thomas Huth 已提交
412 413 414 415 416 417
		return -EFAULT;
	kvm_s390_set_psw_cc(vcpu, 0);
	vcpu->run->s.regs.gprs[0] = 0;
	return 0;
}

418
static int handle_tpi(struct kvm_vcpu *vcpu)
419
{
420
	struct kvm_s390_interrupt_info *inti;
H
Heiko Carstens 已提交
421 422
	unsigned long len;
	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 429
	if (addr & 3)
		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 433 434 435 436
	if (!inti) {
		kvm_s390_set_psw_cc(vcpu, 0);
		return 0;
	}

H
Heiko Carstens 已提交
437 438 439
	tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
	tpi_data[1] = inti->io.io_int_parm;
	tpi_data[2] = inti->io.io_int_word;
440 441 442 443 444
	if (addr) {
		/*
		 * Store the two-word I/O interruption code into the
		 * provided area.
		 */
H
Heiko Carstens 已提交
445
		len = sizeof(tpi_data) - 4;
446
		rc = write_guest(vcpu, addr, ar, &tpi_data, len);
447 448 449 450
		if (rc) {
			rc = kvm_s390_inject_prog_cond(vcpu, rc);
			goto reinject_interrupt;
		}
451 452 453 454 455
	} else {
		/*
		 * Store the three-word I/O interruption code into
		 * the appropriate lowcore area.
		 */
H
Heiko Carstens 已提交
456
		len = sizeof(tpi_data);
457 458
		if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
			/* failed writes to the low core are not recoverable */
H
Heiko Carstens 已提交
459
			rc = -EFAULT;
460 461
			goto reinject_interrupt;
		}
462
	}
463 464 465 466 467 468

	/* irq was successfully handed to the guest */
	kfree(inti);
	kvm_s390_set_psw_cc(vcpu, 1);
	return 0;
reinject_interrupt:
469 470 471 472 473
	/*
	 * If we encounter a problem storing the interruption code, the
	 * instruction is suppressed from the guest's view: reinject the
	 * interrupt.
	 */
474 475 476 477
	if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
		kfree(inti);
		rc = -EFAULT;
	}
478
	/* don't set the cc, a pgm irq was injected or we drop to user space */
H
Heiko Carstens 已提交
479
	return rc ? -EFAULT : 0;
480 481
}

482 483
static int handle_tsch(struct kvm_vcpu *vcpu)
{
484 485
	struct kvm_s390_interrupt_info *inti = NULL;
	const u64 isc_mask = 0xffUL << 24; /* all iscs set */
486

487 488 489 490
	/* a valid schid has at least one bit set */
	if (vcpu->run->s.regs.gprs[1])
		inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
					   vcpu->run->s.regs.gprs[1]);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

	/*
	 * Prepare exit to userspace.
	 * We indicate whether we dequeued a pending I/O interrupt
	 * so that userspace can re-inject it if the instruction gets
	 * a program check. While this may re-order the pending I/O
	 * interrupts, this is no problem since the priority is kept
	 * intact.
	 */
	vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
	vcpu->run->s390_tsch.dequeued = !!inti;
	if (inti) {
		vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
		vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
		vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
		vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
	}
	vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
	kfree(inti);
	return -EREMOTE;
}

static int handle_io_inst(struct kvm_vcpu *vcpu)
{
	VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");

517 518 519
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

520 521 522 523 524 525 526 527 528 529 530 531 532
	if (vcpu->kvm->arch.css_support) {
		/*
		 * Most I/O instructions will be handled by userspace.
		 * Exceptions are tpi and the interrupt portion of tsch.
		 */
		if (vcpu->arch.sie_block->ipa == 0xb236)
			return handle_tpi(vcpu);
		if (vcpu->arch.sie_block->ipa == 0xb235)
			return handle_tsch(vcpu);
		/* Handle in userspace. */
		return -EOPNOTSUPP;
	} else {
		/*
533
		 * Set condition code 3 to stop the guest from issuing channel
534 535
		 * I/O instructions.
		 */
536
		kvm_s390_set_psw_cc(vcpu, 3);
537 538 539 540
		return 0;
	}
}

541 542 543
static int handle_stfl(struct kvm_vcpu *vcpu)
{
	int rc;
544
	unsigned int fac;
545 546

	vcpu->stat.instruction_stfl++;
547 548 549 550

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

551 552 553 554
	/*
	 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
	 * into a u32 memory representation. They will remain bits 0-31.
	 */
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 563 564
	return 0;
}

565 566
#define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
#define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
567
#define PSW_ADDR_24 0x0000000000ffffffUL
568 569
#define PSW_ADDR_31 0x000000007fffffffUL

T
Thomas Huth 已提交
570 571
int is_valid_psw(psw_t *psw)
{
572 573 574 575 576 577 578 579 580 581
	if (psw->mask & PSW_MASK_UNASSIGNED)
		return 0;
	if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
		if (psw->addr & ~PSW_ADDR_31)
			return 0;
	}
	if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
		return 0;
	if ((psw->mask & PSW_MASK_ADDR_MODE) ==  PSW_MASK_EA)
		return 0;
T
Thomas Huth 已提交
582 583
	if (psw->addr & 1)
		return 0;
584 585 586
	return 1;
}

587 588
int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
{
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 597
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

598
	addr = kvm_s390_get_base_disp_s(vcpu, &ar);
599 600
	if (addr & 7)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
601

602
	rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
603 604
	if (rc)
		return kvm_s390_inject_prog_cond(vcpu, rc);
605 606
	if (!(new_psw.mask & PSW32_MASK_BASE))
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
607 608 609 610
	gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
	gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
	gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
	if (!is_valid_psw(gpsw))
611
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
612 613 614 615 616 617
	return 0;
}

static int handle_lpswe(struct kvm_vcpu *vcpu)
{
	psw_t new_psw;
618
	u64 addr;
619
	int rc;
620
	u8 ar;
621

622 623 624
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

625
	addr = kvm_s390_get_base_disp_s(vcpu, &ar);
626 627
	if (addr & 7)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
628
	rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
629 630
	if (rc)
		return kvm_s390_inject_prog_cond(vcpu, rc);
631 632
	vcpu->arch.sie_block->gpsw = new_psw;
	if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
633
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
634 635 636
	return 0;
}

637 638
static int handle_stidp(struct kvm_vcpu *vcpu)
{
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 647 648
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

649
	operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
650

651 652
	if (operand2 & 7)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
653

654
	rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
655 656
	if (rc)
		return kvm_s390_inject_prog_cond(vcpu, rc);
657

658
	VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
659 660 661 662 663 664 665 666
	return 0;
}

static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
{
	int cpus = 0;
	int n;

667
	cpus = atomic_read(&vcpu->kvm->online_vcpus);
668 669

	/* deal with other level 3 hypervisors */
670
	if (stsi(mem, 3, 2, 2))
671 672 673 674 675 676
		mem->count = 0;
	if (mem->count < 8)
		mem->count++;
	for (n = mem->count - 1; n > 0 ; n--)
		memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));

677
	memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
678 679 680 681 682 683 684 685 686 687 688
	mem->vm[0].cpus_total = cpus;
	mem->vm[0].cpus_configured = cpus;
	mem->vm[0].cpus_standby = 0;
	mem->vm[0].cpus_reserved = 0;
	mem->vm[0].caf = 1000;
	memcpy(mem->vm[0].name, "KVMguest", 8);
	ASCEBC(mem->vm[0].name, 8);
	memcpy(mem->vm[0].cpi, "KVM/Linux       ", 16);
	ASCEBC(mem->vm[0].cpi, 16);
}

689
static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
690 691 692 693 694 695 696 697 698 699
				 u8 fc, u8 sel1, u16 sel2)
{
	vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
	vcpu->run->s390_stsi.addr = addr;
	vcpu->run->s390_stsi.ar = ar;
	vcpu->run->s390_stsi.fc = fc;
	vcpu->run->s390_stsi.sel1 = sel1;
	vcpu->run->s390_stsi.sel2 = sel2;
}

700 701
static int handle_stsi(struct kvm_vcpu *vcpu)
{
702 703 704
	int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
	int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
	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 714 715
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

716
	if (fc > 3) {
717
		kvm_s390_set_psw_cc(vcpu, 3);
718 719
		return 0;
	}
720

721 722
	if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
	    || vcpu->run->s.regs.gprs[1] & 0xffff0000)
723 724
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

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 734 735 736

	if (operand2 & 0xfff)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

	switch (fc) {
737 738 739 740
	case 1: /* same handling for 1 and 2 */
	case 2:
		mem = get_zeroed_page(GFP_KERNEL);
		if (!mem)
741
			goto out_no_data;
742
		if (stsi((void *) mem, fc, sel1, sel2))
743
			goto out_no_data;
744 745 746
		break;
	case 3:
		if (sel1 != 2 || sel2 != 2)
747
			goto out_no_data;
748 749
		mem = get_zeroed_page(GFP_KERNEL);
		if (!mem)
750
			goto out_no_data;
751 752 753 754
		handle_stsi_3_2_2(vcpu, (void *) mem);
		break;
	}

755
	rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
756 757 758
	if (rc) {
		rc = kvm_s390_inject_prog_cond(vcpu, rc);
		goto out;
759
	}
760 761 762 763
	if (vcpu->kvm->arch.user_stsi) {
		insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
		rc = -EREMOTE;
	}
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 780 781
	[0x10] = handle_set_prefix,
	[0x11] = handle_store_prefix,
	[0x12] = handle_store_cpu_address,
782
	[0x14] = kvm_s390_handle_vsie,
783
	[0x21] = handle_ipte_interlock,
784 785 786
	[0x29] = handle_iske,
	[0x2a] = handle_rrbe,
	[0x2b] = handle_sske,
T
Thomas Huth 已提交
787
	[0x2c] = handle_test_block,
788 789 790 791 792 793 794 795 796 797 798 799 800
	[0x30] = handle_io_inst,
	[0x31] = handle_io_inst,
	[0x32] = handle_io_inst,
	[0x33] = handle_io_inst,
	[0x34] = handle_io_inst,
	[0x35] = handle_io_inst,
	[0x36] = handle_io_inst,
	[0x37] = handle_io_inst,
	[0x38] = handle_io_inst,
	[0x39] = handle_io_inst,
	[0x3a] = handle_io_inst,
	[0x3b] = handle_io_inst,
	[0x3c] = handle_io_inst,
801
	[0x50] = handle_ipte_interlock,
802
	[0x56] = handle_sthyi,
803 804 805
	[0x5f] = handle_io_inst,
	[0x74] = handle_io_inst,
	[0x76] = handle_io_inst,
806 807
	[0x7d] = handle_stsi,
	[0xb1] = handle_stfl,
808
	[0xb2] = handle_lpswe,
809 810
};

811
int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
812 813 814
{
	intercept_handler_t handler;

815
	/*
816 817 818 819
	 * A lot of B2 instructions are priviledged. Here we check for
	 * the privileged ones, that we can handle in the kernel.
	 * Anything else goes to userspace.
	 */
820
	handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
821 822 823
	if (handler)
		return handler(vcpu);

824
	return -EOPNOTSUPP;
825
}
826

827 828 829 830
static int handle_epsw(struct kvm_vcpu *vcpu)
{
	int reg1, reg2;

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 836
	vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
	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 842 843
	}
	return 0;
}

844 845 846 847 848 849 850 851 852 853 854 855
#define PFMF_RESERVED   0xfffc0101UL
#define PFMF_SK         0x00020000UL
#define PFMF_CF         0x00010000UL
#define PFMF_UI         0x00008000UL
#define PFMF_FSC        0x00007000UL
#define PFMF_NQ         0x00000800UL
#define PFMF_MR         0x00000400UL
#define PFMF_MC         0x00000200UL
#define PFMF_KEY        0x000000feUL

static int handle_pfmf(struct kvm_vcpu *vcpu)
{
856
	bool mr = false, mc = false, nq;
857 858
	int reg1, reg2;
	unsigned long start, end;
859
	unsigned char key;
860 861 862 863 864

	vcpu->stat.instruction_pfmf++;

	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);

865
	if (!test_kvm_facility(vcpu->kvm, 8))
866 867 868
		return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
869
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
870 871 872 873

	if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

874 875 876
	/* Only provide non-quiescing support if enabled for the guest */
	if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
	    !test_kvm_facility(vcpu->kvm, 14))
877 878
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

879 880 881 882 883 884 885 886 887
	/* Only provide conditional-SSKE support if enabled for the guest */
	if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
	    test_kvm_facility(vcpu->kvm, 10)) {
		mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
		mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
	}

	nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
	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);
T
Thomas Huth 已提交
890

891 892 893 894 895
	if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
		if (kvm_s390_check_low_addr_prot_real(vcpu, start))
			return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
	}

896 897
	switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
	case 0x00000000:
898 899
		/* only 4k frames specify a real address */
		start = kvm_s390_real_to_abs(vcpu, start);
900
		end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
901 902
		break;
	case 0x00001000:
903
		end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
904 905
		break;
	case 0x00002000:
906 907 908
		/* only support 2G frame size if EDAT2 is available and we are
		   not in 24-bit addressing mode */
		if (!test_kvm_facility(vcpu->kvm, 78) ||
H
Heiko Carstens 已提交
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 914 915
	default:
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
	}
916

917
	while (start != end) {
918
		unsigned long useraddr;
T
Thomas Huth 已提交
919 920

		/* Translate guest address to host address */
921
		useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
T
Thomas Huth 已提交
922
		if (kvm_is_error_hva(useraddr))
923 924 925 926 927 928 929 930
			return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);

		if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
			if (clear_user((void __user *)useraddr, PAGE_SIZE))
				return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
		}

		if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
931
			int rc = kvm_s390_skey_check_enable(vcpu);
932 933 934

			if (rc)
				return rc;
935
			down_read(&current->mm->mmap_sem);
936 937
			rc = cond_set_guest_storage_key(current->mm, useraddr,
							key, NULL, nq, mr, mc);
938
			up_read(&current->mm->mmap_sem);
939
			if (rc < 0)
940 941 942 943 944
				return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
		}

		start += PAGE_SIZE;
	}
945
	if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
H
Heiko Carstens 已提交
946
		if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
947 948 949 950 951 952 953
			vcpu->run->s.regs.gprs[reg2] = end;
		} else {
			vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
			end = kvm_s390_logical_to_effective(vcpu, end);
			vcpu->run->s.regs.gprs[reg2] |= end;
		}
	}
954 955 956
	return 0;
}

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
static inline int do_essa(struct kvm_vcpu *vcpu, const int orc)
{
	struct kvm_s390_migration_state *ms = vcpu->kvm->arch.migration_state;
	int r1, r2, nappended, entries;
	unsigned long gfn, hva, res, pgstev, ptev;
	unsigned long *cbrlo;

	/*
	 * We don't need to set SD.FPF.SK to 1 here, because if we have a
	 * machine check here we either handle it or crash
	 */

	kvm_s390_get_regs_rre(vcpu, &r1, &r2);
	gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
	hva = gfn_to_hva(vcpu->kvm, gfn);
	entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;

	if (kvm_is_error_hva(hva))
		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);

	nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
	if (nappended < 0) {
		res = orc ? 0x10 : 0;
		vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
		return 0;
	}
	res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
	/*
	 * Set the block-content state part of the result. 0 means resident, so
	 * nothing to do if the page is valid. 2 is for preserved pages
	 * (non-present and non-zero), and 3 for zero pages (non-present and
	 * zero).
	 */
	if (ptev & _PAGE_INVALID) {
		res |= 2;
		if (pgstev & _PGSTE_GPS_ZERO)
			res |= 1;
	}
995 996
	if (pgstev & _PGSTE_GPS_NODAT)
		res |= 0x20;
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	vcpu->run->s.regs.gprs[r1] = res;
	/*
	 * It is possible that all the normal 511 slots were full, in which case
	 * we will now write in the 512th slot, which is reserved for host use.
	 * In both cases we let the normal essa handling code process all the
	 * slots, including the reserved one, if needed.
	 */
	if (nappended > 0) {
		cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
		cbrlo[entries] = gfn << PAGE_SHIFT;
	}

	if (orc) {
		/* increment only if we are really flipping the bit to 1 */
		if (!test_and_set_bit(gfn, ms->pgste_bitmap))
			atomic64_inc(&ms->dirty_pages);
	}

	return nappended;
}

1018 1019 1020 1021
static int handle_essa(struct kvm_vcpu *vcpu)
{
	/* entries expected to be 1FF */
	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 1028
	gmap = vcpu->arch.gmap;
	vcpu->stat.instruction_essa++;
1029
	if (!vcpu->kvm->arch.use_cmma)
1030 1031 1032 1033
		return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1034 1035
	/* Check for invalid operation request code */
	orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
1036 1037 1038
	/* ORCs 0-6 are always valid */
	if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
						: ESSA_SET_STABLE_IF_RESIDENT))
1039 1040
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	if (likely(!vcpu->kvm->arch.migration_state)) {
		/*
		 * CMMA is enabled in the KVM settings, but is disabled in
		 * the SIE block and in the mm_context, and we are not doing
		 * a migration. Enable CMMA in the mm_context.
		 * Since we need to take a write lock to write to the context
		 * to avoid races with storage keys handling, we check if the
		 * value really needs to be written to; if the value is
		 * already correct, we do nothing and avoid the lock.
		 */
		if (vcpu->kvm->mm->context.use_cmma == 0) {
			down_write(&vcpu->kvm->mm->mmap_sem);
			vcpu->kvm->mm->context.use_cmma = 1;
			up_write(&vcpu->kvm->mm->mmap_sem);
		}
		/*
		 * If we are here, we are supposed to have CMMA enabled in
		 * the SIE block. Enabling CMMA works on a per-CPU basis,
		 * while the context use_cmma flag is per process.
		 * It's possible that the context flag is enabled and the
		 * SIE flag is not, so we set the flag always; if it was
		 * already set, nothing changes, otherwise we enable it
		 * on this CPU too.
		 */
		vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
		/* Retry the ESSA instruction */
		kvm_s390_retry_instr(vcpu);
	} else {
		/* Account for the possible extra cbrl entry */
		i = do_essa(vcpu, orc);
		if (i < 0)
			return i;
		entries += i;
	}
1075 1076 1077
	vcpu->arch.sie_block->cbrlo &= PAGE_MASK;	/* reset nceo */
	cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
	down_read(&gmap->mm->mmap_sem);
1078 1079
	for (i = 0; i < entries; ++i)
		__gmap_zap(gmap, cbrlo[i]);
1080 1081 1082 1083
	up_read(&gmap->mm->mmap_sem);
	return 0;
}

1084
static const intercept_handler_t b9_handlers[256] = {
1085
	[0x8a] = handle_ipte_interlock,
1086
	[0x8d] = handle_epsw,
1087 1088
	[0x8e] = handle_ipte_interlock,
	[0x8f] = handle_ipte_interlock,
1089
	[0xab] = handle_essa,
1090
	[0xaf] = handle_pfmf,
1091 1092 1093 1094 1095 1096 1097 1098
};

int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
{
	intercept_handler_t handler;

	/* This is handled just as for the B2 instructions. */
	handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1099 1100 1101
	if (handler)
		return handler(vcpu);

1102 1103 1104
	return -EOPNOTSUPP;
}

1105 1106 1107 1108
int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
{
	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1109 1110
	int reg, rc, nr_regs;
	u32 ctl_array[16];
1111
	u64 ga;
1112
	u8 ar;
1113 1114 1115 1116 1117 1118

	vcpu->stat.instruction_lctl++;

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

1119
	ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1120

1121
	if (ga & 3)
1122 1123
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

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 1130
	if (rc)
		return kvm_s390_inject_prog_cond(vcpu, rc);
1131
	reg = reg1;
1132
	nr_regs = 0;
1133 1134
	do {
		vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
1135
		vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
1136 1137 1138 1139
		if (reg == reg3)
			break;
		reg = (reg + 1) % 16;
	} while (1);
1140
	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1141 1142 1143
	return 0;
}

1144 1145 1146 1147
int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
{
	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1148 1149
	int reg, rc, nr_regs;
	u32 ctl_array[16];
1150
	u64 ga;
1151
	u8 ar;
1152 1153 1154 1155 1156 1157

	vcpu->stat.instruction_stctl++;

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

1158
	ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1159 1160 1161 1162

	if (ga & 3)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

1163
	VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1164 1165 1166
	trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);

	reg = reg1;
1167
	nr_regs = 0;
1168
	do {
1169
		ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1170 1171 1172 1173
		if (reg == reg3)
			break;
		reg = (reg + 1) % 16;
	} 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 1179 1180 1181
static int handle_lctlg(struct kvm_vcpu *vcpu)
{
	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1182 1183 1184
	int reg, rc, nr_regs;
	u64 ctl_array[16];
	u64 ga;
1185
	u8 ar;
1186 1187 1188 1189 1190 1191

	vcpu->stat.instruction_lctlg++;

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

1192
	ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1193

1194
	if (ga & 7)
1195 1196
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

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 1203 1204 1205
	if (rc)
		return kvm_s390_inject_prog_cond(vcpu, rc);
	reg = reg1;
	nr_regs = 0;
1206
	do {
1207
		vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
1208 1209 1210 1211
		if (reg == reg3)
			break;
		reg = (reg + 1) % 16;
	} while (1);
1212
	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1213 1214 1215
	return 0;
}

1216 1217 1218 1219
static int handle_stctg(struct kvm_vcpu *vcpu)
{
	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1220 1221 1222
	int reg, rc, nr_regs;
	u64 ctl_array[16];
	u64 ga;
1223
	u8 ar;
1224 1225 1226 1227 1228 1229

	vcpu->stat.instruction_stctg++;

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

1230
	ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1231 1232 1233 1234

	if (ga & 7)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

1235
	VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1236 1237
	trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);

1238 1239
	reg = reg1;
	nr_regs = 0;
1240
	do {
1241
		ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1242 1243 1244 1245
		if (reg == reg3)
			break;
		reg = (reg + 1) % 16;
	} 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,
F
Fan Zhang 已提交
1253 1254 1255
	[0x60] = handle_ri,
	[0x61] = handle_ri,
	[0x62] = handle_ri,
1256 1257
};

1258
int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
1259 1260 1261 1262 1263 1264 1265 1266 1267
{
	intercept_handler_t handler;

	handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
	if (handler)
		return handler(vcpu);
	return -EOPNOTSUPP;
}

1268 1269
static int handle_tprot(struct kvm_vcpu *vcpu)
{
1270
	u64 address1, address2;
1271 1272 1273
	unsigned long hva, gpa;
	int ret = 0, cc = 0;
	bool writable;
1274
	u8 ar;
1275 1276 1277

	vcpu->stat.instruction_tprot++;

1278 1279 1280
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);

1281
	kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
1282

1283 1284 1285 1286 1287 1288
	/* we only handle the Linux memory detection case:
	 * access key == 0
	 * everything else goes to userspace. */
	if (address2 & 0xf0)
		return -EOPNOTSUPP;
	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 1292 1293
	if (ret == PGM_PROTECTION) {
		/* Write protected? Try again with read-only... */
		cc = 1;
1294 1295
		ret = guest_translate_address(vcpu, address1, ar, &gpa,
					      GACC_FETCH);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	}
	if (ret) {
		if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
			ret = kvm_s390_inject_program_int(vcpu, ret);
		} else if (ret > 0) {
			/* Translation not available */
			kvm_s390_set_psw_cc(vcpu, 3);
			ret = 0;
		}
		goto out_unlock;
	}
1307

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
	if (kvm_is_error_hva(hva)) {
		ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
	} else {
		if (!writable)
			cc = 1;		/* Write not permitted ==> read-only */
		kvm_s390_set_psw_cc(vcpu, cc);
		/* Note: CC2 only occurs for storage keys (not supported yet) */
	}
out_unlock:
	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
		ipte_unlock(vcpu);
	return ret;
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
}

int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
{
	/* For e5xx... instructions we only handle TPROT */
	if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
		return handle_tprot(vcpu);
	return -EOPNOTSUPP;
}

1331 1332 1333 1334 1335
static int handle_sckpf(struct kvm_vcpu *vcpu)
{
	u32 value;

	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1336
		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

	if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
		return kvm_s390_inject_program_int(vcpu,
						   PGM_SPECIFICATION);

	value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
	vcpu->arch.sie_block->todpr = value;

	return 0;
}

1348 1349 1350 1351 1352 1353 1354
static int handle_ptff(struct kvm_vcpu *vcpu)
{
	/* we don't emulate any control instructions yet */
	kvm_s390_set_psw_cc(vcpu, 3);
	return 0;
}

1355
static const intercept_handler_t x01_handlers[256] = {
1356
	[0x04] = handle_ptff,
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	[0x07] = handle_sckpf,
};

int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
{
	intercept_handler_t handler;

	handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
	if (handler)
		return handler(vcpu);
	return -EOPNOTSUPP;
}