| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  * intercept.c - in-kernel handling for sie intercepts | 
 | 3 |  * | 
| Christian Ehrhardt | 628eb9b | 2009-05-25 13:40:51 +0200 | [diff] [blame] | 4 |  * Copyright IBM Corp. 2008,2009 | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 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_host.h> | 
 | 15 | #include <linux/errno.h> | 
 | 16 | #include <linux/pagemap.h> | 
 | 17 |  | 
 | 18 | #include <asm/kvm_host.h> | 
 | 19 |  | 
 | 20 | #include "kvm-s390.h" | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 21 | #include "gaccess.h" | 
 | 22 |  | 
| Christian Borntraeger | f5e10b0 | 2008-07-25 15:52:44 +0200 | [diff] [blame] | 23 | static int handle_lctlg(struct kvm_vcpu *vcpu) | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 24 | { | 
 | 25 | 	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | 
 | 26 | 	int reg3 = vcpu->arch.sie_block->ipa & 0x000f; | 
 | 27 | 	int base2 = vcpu->arch.sie_block->ipb >> 28; | 
 | 28 | 	int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) + | 
 | 29 | 			((vcpu->arch.sie_block->ipb & 0xff00) << 4); | 
 | 30 | 	u64 useraddr; | 
 | 31 | 	int reg, rc; | 
 | 32 |  | 
| Christian Borntraeger | f5e10b0 | 2008-07-25 15:52:44 +0200 | [diff] [blame] | 33 | 	vcpu->stat.instruction_lctlg++; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 34 | 	if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f) | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 35 | 		return -EOPNOTSUPP; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 36 |  | 
 | 37 | 	useraddr = disp2; | 
 | 38 | 	if (base2) | 
 | 39 | 		useraddr += vcpu->arch.guest_gprs[base2]; | 
 | 40 |  | 
| Christian Borntraeger | 5a00a5e | 2008-07-25 15:53:12 +0200 | [diff] [blame] | 41 | 	if (useraddr & 7) | 
 | 42 | 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | 
 | 43 |  | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 44 | 	reg = reg1; | 
 | 45 |  | 
| Christian Borntraeger | f5e10b0 | 2008-07-25 15:52:44 +0200 | [diff] [blame] | 46 | 	VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2, | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 47 | 		   disp2); | 
 | 48 |  | 
 | 49 | 	do { | 
 | 50 | 		rc = get_guest_u64(vcpu, useraddr, | 
 | 51 | 				   &vcpu->arch.sie_block->gcr[reg]); | 
 | 52 | 		if (rc == -EFAULT) { | 
 | 53 | 			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | 
 | 54 | 			break; | 
 | 55 | 		} | 
 | 56 | 		useraddr += 8; | 
 | 57 | 		if (reg == reg3) | 
 | 58 | 			break; | 
 | 59 | 		reg = (reg + 1) % 16; | 
 | 60 | 	} while (1); | 
 | 61 | 	return 0; | 
 | 62 | } | 
 | 63 |  | 
 | 64 | static int handle_lctl(struct kvm_vcpu *vcpu) | 
 | 65 | { | 
 | 66 | 	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | 
 | 67 | 	int reg3 = vcpu->arch.sie_block->ipa & 0x000f; | 
 | 68 | 	int base2 = vcpu->arch.sie_block->ipb >> 28; | 
 | 69 | 	int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16); | 
 | 70 | 	u64 useraddr; | 
 | 71 | 	u32 val = 0; | 
 | 72 | 	int reg, rc; | 
 | 73 |  | 
 | 74 | 	vcpu->stat.instruction_lctl++; | 
 | 75 |  | 
 | 76 | 	useraddr = disp2; | 
 | 77 | 	if (base2) | 
 | 78 | 		useraddr += vcpu->arch.guest_gprs[base2]; | 
 | 79 |  | 
| Christian Borntraeger | 5a00a5e | 2008-07-25 15:53:12 +0200 | [diff] [blame] | 80 | 	if (useraddr & 3) | 
 | 81 | 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | 
 | 82 |  | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 83 | 	VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2, | 
 | 84 | 		   disp2); | 
 | 85 |  | 
 | 86 | 	reg = reg1; | 
 | 87 | 	do { | 
 | 88 | 		rc = get_guest_u32(vcpu, useraddr, &val); | 
 | 89 | 		if (rc == -EFAULT) { | 
 | 90 | 			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | 
 | 91 | 			break; | 
 | 92 | 		} | 
 | 93 | 		vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; | 
 | 94 | 		vcpu->arch.sie_block->gcr[reg] |= val; | 
 | 95 | 		useraddr += 4; | 
 | 96 | 		if (reg == reg3) | 
 | 97 | 			break; | 
 | 98 | 		reg = (reg + 1) % 16; | 
 | 99 | 	} while (1); | 
 | 100 | 	return 0; | 
 | 101 | } | 
 | 102 |  | 
 | 103 | static intercept_handler_t instruction_handlers[256] = { | 
| Christian Borntraeger | e28acfe | 2008-03-25 18:47:34 +0100 | [diff] [blame] | 104 | 	[0x83] = kvm_s390_handle_diag, | 
| Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 105 | 	[0xae] = kvm_s390_handle_sigp, | 
| Christian Borntraeger | 70455a3 | 2009-01-22 10:28:29 +0100 | [diff] [blame] | 106 | 	[0xb2] = kvm_s390_handle_b2, | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 107 | 	[0xb7] = handle_lctl, | 
| Christian Borntraeger | f5e10b0 | 2008-07-25 15:52:44 +0200 | [diff] [blame] | 108 | 	[0xeb] = handle_lctlg, | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 109 | }; | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 110 |  | 
 | 111 | static int handle_noop(struct kvm_vcpu *vcpu) | 
 | 112 | { | 
 | 113 | 	switch (vcpu->arch.sie_block->icptcode) { | 
| Christian Borntraeger | 0eaeafa | 2008-05-07 09:22:53 +0200 | [diff] [blame] | 114 | 	case 0x0: | 
 | 115 | 		vcpu->stat.exit_null++; | 
 | 116 | 		break; | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 117 | 	case 0x10: | 
 | 118 | 		vcpu->stat.exit_external_request++; | 
 | 119 | 		break; | 
 | 120 | 	case 0x14: | 
 | 121 | 		vcpu->stat.exit_external_interrupt++; | 
 | 122 | 		break; | 
 | 123 | 	default: | 
 | 124 | 		break; /* nothing */ | 
 | 125 | 	} | 
 | 126 | 	return 0; | 
 | 127 | } | 
 | 128 |  | 
 | 129 | static int handle_stop(struct kvm_vcpu *vcpu) | 
 | 130 | { | 
| Christian Ehrhardt | 9ace903 | 2009-05-20 15:34:55 +0200 | [diff] [blame] | 131 | 	int rc = 0; | 
| Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 132 |  | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 133 | 	vcpu->stat.exit_stop_request++; | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 134 | 	atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); | 
| Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 135 | 	spin_lock_bh(&vcpu->arch.local_int.lock); | 
 | 136 | 	if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) { | 
 | 137 | 		vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP; | 
 | 138 | 		rc = __kvm_s390_vcpu_store_status(vcpu, | 
 | 139 | 						  KVM_S390_STORE_STATUS_NOADDR); | 
 | 140 | 		if (rc >= 0) | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 141 | 			rc = -EOPNOTSUPP; | 
| Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 142 | 	} | 
 | 143 |  | 
| Christian Ehrhardt | 9ace903 | 2009-05-20 15:34:55 +0200 | [diff] [blame] | 144 | 	if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) { | 
 | 145 | 		vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP; | 
 | 146 | 		rc = SIE_INTERCEPT_RERUNVCPU; | 
 | 147 | 		vcpu->run->exit_reason = KVM_EXIT_INTR; | 
 | 148 | 	} | 
 | 149 |  | 
| Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 150 | 	if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) { | 
 | 151 | 		vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP; | 
 | 152 | 		VCPU_EVENT(vcpu, 3, "%s", "cpu stopped"); | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 153 | 		rc = -EOPNOTSUPP; | 
| Christian Ehrhardt | 9ace903 | 2009-05-20 15:34:55 +0200 | [diff] [blame] | 154 | 	} | 
 | 155 |  | 
| Christian Borntraeger | 5288fbf | 2008-03-25 18:47:31 +0100 | [diff] [blame] | 156 | 	spin_unlock_bh(&vcpu->arch.local_int.lock); | 
 | 157 | 	return rc; | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 158 | } | 
 | 159 |  | 
 | 160 | static int handle_validity(struct kvm_vcpu *vcpu) | 
 | 161 | { | 
 | 162 | 	int viwhy = vcpu->arch.sie_block->ipb >> 16; | 
| Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 163 | 	int rc; | 
 | 164 |  | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 165 | 	vcpu->stat.exit_validity++; | 
| Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 166 | 	if ((viwhy == 0x37) && (vcpu->arch.sie_block->prefix | 
| Christian Ehrhardt | 628eb9b | 2009-05-25 13:40:51 +0200 | [diff] [blame] | 167 | 		<= kvm_s390_vcpu_get_memsize(vcpu) - 2*PAGE_SIZE)) { | 
| Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 168 | 		rc = fault_in_pages_writeable((char __user *) | 
| Christian Ehrhardt | 628eb9b | 2009-05-25 13:40:51 +0200 | [diff] [blame] | 169 | 			 vcpu->arch.sie_block->gmsor + | 
| Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 170 | 			 vcpu->arch.sie_block->prefix, | 
 | 171 | 			 2*PAGE_SIZE); | 
 | 172 | 		if (rc) | 
 | 173 | 			/* user will receive sigsegv, exit to user */ | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 174 | 			rc = -EOPNOTSUPP; | 
| Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 175 | 	} else | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 176 | 		rc = -EOPNOTSUPP; | 
| Carsten Otte | 3edbcff | 2009-05-12 17:21:52 +0200 | [diff] [blame] | 177 |  | 
 | 178 | 	if (rc) | 
 | 179 | 		VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d", | 
 | 180 | 			   viwhy); | 
 | 181 | 	return rc; | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 182 | } | 
 | 183 |  | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 184 | static int handle_instruction(struct kvm_vcpu *vcpu) | 
 | 185 | { | 
 | 186 | 	intercept_handler_t handler; | 
 | 187 |  | 
 | 188 | 	vcpu->stat.exit_instruction++; | 
 | 189 | 	handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8]; | 
 | 190 | 	if (handler) | 
 | 191 | 		return handler(vcpu); | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 192 | 	return -EOPNOTSUPP; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 193 | } | 
 | 194 |  | 
 | 195 | static int handle_prog(struct kvm_vcpu *vcpu) | 
 | 196 | { | 
 | 197 | 	vcpu->stat.exit_program_interruption++; | 
 | 198 | 	return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc); | 
 | 199 | } | 
 | 200 |  | 
 | 201 | static int handle_instruction_and_prog(struct kvm_vcpu *vcpu) | 
 | 202 | { | 
 | 203 | 	int rc, rc2; | 
 | 204 |  | 
 | 205 | 	vcpu->stat.exit_instr_and_program++; | 
 | 206 | 	rc = handle_instruction(vcpu); | 
 | 207 | 	rc2 = handle_prog(vcpu); | 
 | 208 |  | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 209 | 	if (rc == -EOPNOTSUPP) | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 210 | 		vcpu->arch.sie_block->icptcode = 0x04; | 
 | 211 | 	if (rc) | 
 | 212 | 		return rc; | 
 | 213 | 	return rc2; | 
 | 214 | } | 
 | 215 |  | 
| Christian Borntraeger | 062d5e9 | 2010-01-21 12:19:07 +0100 | [diff] [blame] | 216 | static const intercept_handler_t intercept_funcs[] = { | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 217 | 	[0x00 >> 2] = handle_noop, | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 218 | 	[0x04 >> 2] = handle_instruction, | 
 | 219 | 	[0x08 >> 2] = handle_prog, | 
 | 220 | 	[0x0C >> 2] = handle_instruction_and_prog, | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 221 | 	[0x10 >> 2] = handle_noop, | 
 | 222 | 	[0x14 >> 2] = handle_noop, | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 223 | 	[0x1C >> 2] = kvm_s390_handle_wait, | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 224 | 	[0x20 >> 2] = handle_validity, | 
 | 225 | 	[0x28 >> 2] = handle_stop, | 
 | 226 | }; | 
 | 227 |  | 
 | 228 | int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu) | 
 | 229 | { | 
 | 230 | 	intercept_handler_t func; | 
 | 231 | 	u8 code = vcpu->arch.sie_block->icptcode; | 
 | 232 |  | 
| Christian Borntraeger | 062d5e9 | 2010-01-21 12:19:07 +0100 | [diff] [blame] | 233 | 	if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs)) | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 234 | 		return -EOPNOTSUPP; | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 235 | 	func = intercept_funcs[code >> 2]; | 
 | 236 | 	if (func) | 
 | 237 | 		return func(vcpu); | 
| Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 238 | 	return -EOPNOTSUPP; | 
| Christian Borntraeger | 8f2abe6 | 2008-03-25 18:47:23 +0100 | [diff] [blame] | 239 | } |