intercept.c 10.6 KB
Newer Older
1
/*
2
 * in-kernel handling for sie intercepts
3
 *
4
 * Copyright IBM Corp. 2008, 2014
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (version 2 only)
 * as published by the Free Software Foundation.
 *
 *    Author(s): Carsten Otte <cotte@de.ibm.com>
 *               Christian Borntraeger <borntraeger@de.ibm.com>
 */

#include <linux/kvm_host.h>
#include <linux/errno.h>
#include <linux/pagemap.h>

#include <asm/kvm_host.h>
19
#include <asm/asm-offsets.h>
20
#include <asm/irq.h>
21 22

#include "kvm-s390.h"
23
#include "gaccess.h"
24
#include "trace.h"
25
#include "trace-s390.h"
26

27

28
static const intercept_handler_t instruction_handlers[256] = {
29
	[0x01] = kvm_s390_handle_01,
30
	[0x82] = kvm_s390_handle_lpsw,
31
	[0x83] = kvm_s390_handle_diag,
F
Fan Zhang 已提交
32
	[0xaa] = kvm_s390_handle_aa,
33
	[0xae] = kvm_s390_handle_sigp,
34
	[0xb2] = kvm_s390_handle_b2,
35
	[0xb6] = kvm_s390_handle_stctl,
36
	[0xb7] = kvm_s390_handle_lctl,
37
	[0xb9] = kvm_s390_handle_b9,
38
	[0xe5] = kvm_s390_handle_e5,
39
	[0xeb] = kvm_s390_handle_eb,
40
};
41

42
u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
43 44
{
	struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
45
	u8 ilen = 0;
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	switch (vcpu->arch.sie_block->icptcode) {
	case ICPT_INST:
	case ICPT_INSTPROGI:
	case ICPT_OPEREXC:
	case ICPT_PARTEXEC:
	case ICPT_IOINST:
		/* instruction only stored for these icptcodes */
		ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
		/* Use the length of the EXECUTE instruction if necessary */
		if (sie_block->icptstatus & 1) {
			ilen = (sie_block->icptstatus >> 4) & 0x6;
			if (!ilen)
				ilen = 4;
		}
		break;
	case ICPT_PROGI:
		/* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
		ilen = vcpu->arch.sie_block->pgmilc & 0x6;
		break;
66
	}
67
	return ilen;
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83
static int handle_noop(struct kvm_vcpu *vcpu)
{
	switch (vcpu->arch.sie_block->icptcode) {
	case 0x10:
		vcpu->stat.exit_external_request++;
		break;
	default:
		break; /* nothing */
	}
	return 0;
}

static int handle_stop(struct kvm_vcpu *vcpu)
{
84
	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
85
	int rc = 0;
86
	uint8_t flags, stop_pending;
87

88
	vcpu->stat.exit_stop_request++;
89

90 91 92 93
	/* delay the stop if any non-stop irq is pending */
	if (kvm_s390_vcpu_has_irq(vcpu, 1))
		return 0;

94 95 96 97 98
	/* avoid races with the injection/SIGP STOP code */
	spin_lock(&li->lock);
	flags = li->irq.stop.flags;
	stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
	spin_unlock(&li->lock);
99

100 101
	trace_kvm_s390_stop_request(stop_pending, flags);
	if (!stop_pending)
102
		return 0;
103

104
	if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
105 106
		rc = kvm_s390_vcpu_store_status(vcpu,
						KVM_S390_STORE_STATUS_NOADDR);
107 108 109 110
		if (rc)
			return rc;
	}

111 112
	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
		kvm_s390_vcpu_stop(vcpu);
113
	return -EOPNOTSUPP;
114 115 116 117 118
}

static int handle_validity(struct kvm_vcpu *vcpu)
{
	int viwhy = vcpu->arch.sie_block->ipb >> 16;
119

120
	vcpu->stat.exit_validity++;
121
	trace_kvm_s390_intercept_validity(vcpu, viwhy);
122 123 124 125 126 127 128
	KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%pK)", viwhy,
		  current->pid, vcpu->kvm);

	/* do not warn on invalid runtime instrumentation mode */
	WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n",
		  viwhy);
	return -EINVAL;
129 130
}

131 132 133 134 135
static int handle_instruction(struct kvm_vcpu *vcpu)
{
	intercept_handler_t handler;

	vcpu->stat.exit_instruction++;
136 137 138
	trace_kvm_s390_intercept_instruction(vcpu,
					     vcpu->arch.sie_block->ipa,
					     vcpu->arch.sie_block->ipb);
139 140 141
	handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
	if (handler)
		return handler(vcpu);
142
	return -EOPNOTSUPP;
143 144
}

145
static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
146
{
147 148
	struct kvm_s390_pgm_info pgm_info = {
		.code = vcpu->arch.sie_block->iprcc,
149 150
		/* the PSW has already been rewound */
		.flags = KVM_S390_PGM_FLAGS_NO_REWIND,
151
	};
152 153 154 155 156 157 158 159 160 161 162 163

	switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
	case PGM_AFX_TRANSLATION:
	case PGM_ASX_TRANSLATION:
	case PGM_EX_TRANSLATION:
	case PGM_LFX_TRANSLATION:
	case PGM_LSTE_SEQUENCE:
	case PGM_LSX_TRANSLATION:
	case PGM_LX_TRANSLATION:
	case PGM_PRIMARY_AUTHORITY:
	case PGM_SECONDARY_AUTHORITY:
	case PGM_SPACE_SWITCH:
164
		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
165 166 167 168 169 170 171
		break;
	case PGM_ALEN_TRANSLATION:
	case PGM_ALE_SEQUENCE:
	case PGM_ASTE_INSTANCE:
	case PGM_ASTE_SEQUENCE:
	case PGM_ASTE_VALIDITY:
	case PGM_EXTENDED_AUTHORITY:
172
		pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
173 174 175 176 177 178 179
		break;
	case PGM_ASCE_TYPE:
	case PGM_PAGE_TRANSLATION:
	case PGM_REGION_FIRST_TRANS:
	case PGM_REGION_SECOND_TRANS:
	case PGM_REGION_THIRD_TRANS:
	case PGM_SEGMENT_TRANSLATION:
180 181 182
		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
		pgm_info.exc_access_id  = vcpu->arch.sie_block->eai;
		pgm_info.op_access_id  = vcpu->arch.sie_block->oai;
183 184
		break;
	case PGM_MONITOR:
185 186
		pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn;
		pgm_info.mon_code = vcpu->arch.sie_block->tecmc;
187
		break;
E
Eric Farman 已提交
188
	case PGM_VECTOR_PROCESSING:
189
	case PGM_DATA:
190
		pgm_info.data_exc_code = vcpu->arch.sie_block->dxc;
191 192
		break;
	case PGM_PROTECTION:
193 194
		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
		pgm_info.exc_access_id  = vcpu->arch.sie_block->eai;
195 196 197 198 199 200
		break;
	default:
		break;
	}

	if (vcpu->arch.sie_block->iprcc & PGM_PER) {
201 202 203 204
		pgm_info.per_code = vcpu->arch.sie_block->perc;
		pgm_info.per_atmid = vcpu->arch.sie_block->peratmid;
		pgm_info.per_address = vcpu->arch.sie_block->peraddr;
		pgm_info.per_access_id = vcpu->arch.sie_block->peraid;
205
	}
206
	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/*
 * restore ITDB to program-interruption TDB in guest lowcore
 * and set TX abort indication if required
*/
static int handle_itdb(struct kvm_vcpu *vcpu)
{
	struct kvm_s390_itdb *itdb;
	int rc;

	if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
		return 0;
	if (current->thread.per_flags & PER_FLAG_NO_TE)
		return 0;
	itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
	rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
	if (rc)
		return rc;
	memset(itdb, 0, sizeof(*itdb));

	return 0;
}

231 232
#define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)

233 234
static int handle_prog(struct kvm_vcpu *vcpu)
{
235
	psw_t psw;
236 237
	int rc;

238
	vcpu->stat.exit_program_interruption++;
239

240 241 242 243 244 245 246
	if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
		kvm_s390_handle_per_event(vcpu);
		/* the interrupt might have been filtered out completely */
		if (vcpu->arch.sie_block->iprcc == 0)
			return 0;
	}

247
	trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
248 249 250 251 252 253 254 255
	if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
		rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
		if (rc)
			return rc;
		/* Avoid endless loops of specification exceptions */
		if (!is_valid_psw(&psw))
			return -EOPNOTSUPP;
	}
256
	rc = handle_itdb(vcpu);
257 258
	if (rc)
		return rc;
259

260
	return inject_prog_on_prog_intercept(vcpu);
261 262
}

263 264 265 266 267 268 269 270 271 272 273
/**
 * handle_external_interrupt - used for external interruption interceptions
 *
 * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
 * the new PSW does not have external interrupts disabled. In the first case,
 * we've got to deliver the interrupt manually, and in the second case, we
 * drop to userspace to handle the situation there.
 */
static int handle_external_interrupt(struct kvm_vcpu *vcpu)
{
	u16 eic = vcpu->arch.sie_block->eic;
274
	struct kvm_s390_irq irq;
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	psw_t newpsw;
	int rc;

	vcpu->stat.exit_external_interrupt++;

	rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
	if (rc)
		return rc;
	/* We can not handle clock comparator or timer interrupt with bad PSW */
	if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
	    (newpsw.mask & PSW_MASK_EXT))
		return -EOPNOTSUPP;

	switch (eic) {
	case EXT_IRQ_CLK_COMP:
		irq.type = KVM_S390_INT_CLOCK_COMP;
		break;
	case EXT_IRQ_CPU_TIMER:
		irq.type = KVM_S390_INT_CPU_TIMER;
		break;
	case EXT_IRQ_EXTERNAL_CALL:
		irq.type = KVM_S390_INT_EXTERNAL_CALL;
297
		irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
298 299 300 301 302
		rc = kvm_s390_inject_vcpu(vcpu, &irq);
		/* ignore if another external call is already pending */
		if (rc == -EBUSY)
			return 0;
		return rc;
303 304 305 306 307 308 309
	default:
		return -EOPNOTSUPP;
	}

	return kvm_s390_inject_vcpu(vcpu, &irq);
}

310 311 312 313 314 315 316 317 318 319
/**
 * Handle MOVE PAGE partial execution interception.
 *
 * This interception can only happen for guests with DAT disabled and
 * addresses that are currently not mapped in the host. Thus we try to
 * set up the mappings for the corresponding user pages here (or throw
 * addressing exceptions in case of illegal guest addresses).
 */
static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
{
320
	unsigned long srcaddr, dstaddr;
321 322 323 324 325
	int reg1, reg2, rc;

	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);

	/* Make sure that the source is paged-in */
326
	rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg2],
327
				     reg2, &srcaddr, GACC_FETCH);
328 329
	if (rc)
		return kvm_s390_inject_prog_cond(vcpu, rc);
330 331
	rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
	if (rc != 0)
332 333 334
		return rc;

	/* Make sure that the destination is paged-in */
335
	rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg1],
336
				     reg1, &dstaddr, GACC_STORE);
337 338
	if (rc)
		return kvm_s390_inject_prog_cond(vcpu, rc);
339 340
	rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
	if (rc != 0)
341 342
		return rc;

343
	kvm_s390_retry_instr(vcpu);
344 345 346 347 348 349

	return 0;
}

static int handle_partial_execution(struct kvm_vcpu *vcpu)
{
350 351
	vcpu->stat.exit_pei++;

352 353
	if (vcpu->arch.sie_block->ipa == 0xb254)	/* MVPG */
		return handle_mvpg_pei(vcpu);
354 355
	if (vcpu->arch.sie_block->ipa >> 8 == 0xae)	/* SIGP */
		return kvm_s390_handle_sigp_pei(vcpu);
356 357 358 359

	return -EOPNOTSUPP;
}

360 361 362 363 364 365
static int handle_operexc(struct kvm_vcpu *vcpu)
{
	vcpu->stat.exit_operation_exception++;
	trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
				      vcpu->arch.sie_block->ipb);

J
Janosch Frank 已提交
366 367 368 369
	if (vcpu->arch.sie_block->ipa == 0xb256 &&
	    test_kvm_facility(vcpu->kvm, 74))
		return handle_sthyi(vcpu);

370 371 372
	if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
		return -EOPNOTSUPP;

373 374 375
	return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
}

376 377
int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
{
378 379
	int rc, per_rc = 0;

380 381 382
	if (kvm_is_ucontrol(vcpu->kvm))
		return -EOPNOTSUPP;

383 384 385 386 387
	switch (vcpu->arch.sie_block->icptcode) {
	case 0x10:
	case 0x18:
		return handle_noop(vcpu);
	case 0x04:
388 389
		rc = handle_instruction(vcpu);
		break;
390 391 392 393 394 395 396 397 398 399
	case 0x08:
		return handle_prog(vcpu);
	case 0x14:
		return handle_external_interrupt(vcpu);
	case 0x1c:
		return kvm_s390_handle_wait(vcpu);
	case 0x20:
		return handle_validity(vcpu);
	case 0x28:
		return handle_stop(vcpu);
400
	case 0x2c:
401 402
		rc = handle_operexc(vcpu);
		break;
403
	case 0x38:
404 405
		rc = handle_partial_execution(vcpu);
		break;
406
	default:
407
		return -EOPNOTSUPP;
408
	}
409 410 411 412 413 414

	/* process PER, also if the instrution is processed in user space */
	if (vcpu->arch.sie_block->icptstatus & 0x02 &&
	    (!rc || rc == -EOPNOTSUPP))
		per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
	return per_rc ? per_rc : rc;
415
}