intercept.c 5.7 KB
Newer Older
1 2 3
/*
 * intercept.c - in-kernel handling for sie intercepts
 *
4
 * Copyright IBM Corp. 2008,2009
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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>

#include "kvm-s390.h"
21 22
#include "gaccess.h"

23
static int handle_lctlg(struct kvm_vcpu *vcpu)
24 25 26 27 28 29 30 31 32
{
	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
	int base2 = vcpu->arch.sie_block->ipb >> 28;
	int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
			((vcpu->arch.sie_block->ipb & 0xff00) << 4);
	u64 useraddr;
	int reg, rc;

33
	vcpu->stat.instruction_lctlg++;
34
	if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f)
35
		return -EOPNOTSUPP;
36 37 38 39 40

	useraddr = disp2;
	if (base2)
		useraddr += vcpu->arch.guest_gprs[base2];

41 42 43
	if (useraddr & 7)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

44 45
	reg = reg1;

46
	VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
47 48 49 50 51 52 53 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
		   disp2);

	do {
		rc = get_guest_u64(vcpu, useraddr,
				   &vcpu->arch.sie_block->gcr[reg]);
		if (rc == -EFAULT) {
			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
			break;
		}
		useraddr += 8;
		if (reg == reg3)
			break;
		reg = (reg + 1) % 16;
	} while (1);
	return 0;
}

static int handle_lctl(struct kvm_vcpu *vcpu)
{
	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
	int base2 = vcpu->arch.sie_block->ipb >> 28;
	int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
	u64 useraddr;
	u32 val = 0;
	int reg, rc;

	vcpu->stat.instruction_lctl++;

	useraddr = disp2;
	if (base2)
		useraddr += vcpu->arch.guest_gprs[base2];

80 81 82
	if (useraddr & 3)
		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
		   disp2);

	reg = reg1;
	do {
		rc = get_guest_u32(vcpu, useraddr, &val);
		if (rc == -EFAULT) {
			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
			break;
		}
		vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
		vcpu->arch.sie_block->gcr[reg] |= val;
		useraddr += 4;
		if (reg == reg3)
			break;
		reg = (reg + 1) % 16;
	} while (1);
	return 0;
}

static intercept_handler_t instruction_handlers[256] = {
104
	[0x83] = kvm_s390_handle_diag,
105
	[0xae] = kvm_s390_handle_sigp,
106
	[0xb2] = kvm_s390_handle_b2,
107
	[0xb7] = handle_lctl,
108
	[0xe5] = kvm_s390_handle_e5,
109
	[0xeb] = handle_lctlg,
110
};
111 112 113 114

static int handle_noop(struct kvm_vcpu *vcpu)
{
	switch (vcpu->arch.sie_block->icptcode) {
115 116 117
	case 0x0:
		vcpu->stat.exit_null++;
		break;
118 119 120 121 122 123 124 125 126 127 128 129 130 131
	case 0x10:
		vcpu->stat.exit_external_request++;
		break;
	case 0x14:
		vcpu->stat.exit_external_interrupt++;
		break;
	default:
		break; /* nothing */
	}
	return 0;
}

static int handle_stop(struct kvm_vcpu *vcpu)
{
132
	int rc = 0;
133

134 135
	vcpu->stat.exit_stop_request++;
	atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
136 137 138
	spin_lock_bh(&vcpu->arch.local_int.lock);
	if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
		vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
139
		rc = kvm_s390_vcpu_store_status(vcpu,
140 141
						  KVM_S390_STORE_STATUS_NOADDR);
		if (rc >= 0)
142
			rc = -EOPNOTSUPP;
143 144
	}

145 146 147 148 149 150
	if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
		vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
		rc = SIE_INTERCEPT_RERUNVCPU;
		vcpu->run->exit_reason = KVM_EXIT_INTR;
	}

151 152 153
	if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
		vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
		VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
154
		rc = -EOPNOTSUPP;
155 156
	}

157 158
	spin_unlock_bh(&vcpu->arch.local_int.lock);
	return rc;
159 160 161 162 163
}

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

166
	vcpu->stat.exit_validity++;
167
	if ((viwhy == 0x37) && (vcpu->arch.sie_block->prefix
168
		<= kvm_s390_vcpu_get_memsize(vcpu) - 2*PAGE_SIZE)) {
169
		rc = fault_in_pages_writeable((char __user *)
170
			 vcpu->arch.sie_block->gmsor +
171 172 173 174
			 vcpu->arch.sie_block->prefix,
			 2*PAGE_SIZE);
		if (rc)
			/* user will receive sigsegv, exit to user */
175
			rc = -EOPNOTSUPP;
176
	} else
177
		rc = -EOPNOTSUPP;
178 179 180 181 182

	if (rc)
		VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
			   viwhy);
	return rc;
183 184
}

185 186 187 188 189 190 191 192
static int handle_instruction(struct kvm_vcpu *vcpu)
{
	intercept_handler_t handler;

	vcpu->stat.exit_instruction++;
	handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
	if (handler)
		return handler(vcpu);
193
	return -EOPNOTSUPP;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

static int handle_prog(struct kvm_vcpu *vcpu)
{
	vcpu->stat.exit_program_interruption++;
	return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
}

static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
{
	int rc, rc2;

	vcpu->stat.exit_instr_and_program++;
	rc = handle_instruction(vcpu);
	rc2 = handle_prog(vcpu);

210
	if (rc == -EOPNOTSUPP)
211 212 213 214 215 216
		vcpu->arch.sie_block->icptcode = 0x04;
	if (rc)
		return rc;
	return rc2;
}

217
static const intercept_handler_t intercept_funcs[] = {
218
	[0x00 >> 2] = handle_noop,
219 220 221
	[0x04 >> 2] = handle_instruction,
	[0x08 >> 2] = handle_prog,
	[0x0C >> 2] = handle_instruction_and_prog,
222 223
	[0x10 >> 2] = handle_noop,
	[0x14 >> 2] = handle_noop,
224
	[0x1C >> 2] = kvm_s390_handle_wait,
225 226 227 228 229 230 231 232 233
	[0x20 >> 2] = handle_validity,
	[0x28 >> 2] = handle_stop,
};

int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
{
	intercept_handler_t func;
	u8 code = vcpu->arch.sie_block->icptcode;

234
	if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
235
		return -EOPNOTSUPP;
236 237 238
	func = intercept_funcs[code >> 2];
	if (func)
		return func(vcpu);
239
	return -EOPNOTSUPP;
240
}