| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  * interrupt.c - handling kvm guest interrupts | 
 | 3 |  * | 
 | 4 |  * Copyright IBM Corp. 2008 | 
 | 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 |  */ | 
 | 12 |  | 
 | 13 | #include <asm/lowcore.h> | 
 | 14 | #include <asm/uaccess.h> | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 15 | #include <linux/hrtimer.h> | 
 | 16 | #include <linux/interrupt.h> | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 17 | #include <linux/kvm_host.h> | 
| Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 18 | #include <linux/signal.h> | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 19 | #include "kvm-s390.h" | 
 | 20 | #include "gaccess.h" | 
 | 21 |  | 
 | 22 | static int psw_extint_disabled(struct kvm_vcpu *vcpu) | 
 | 23 | { | 
 | 24 | 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT); | 
 | 25 | } | 
 | 26 |  | 
 | 27 | static int psw_interrupts_disabled(struct kvm_vcpu *vcpu) | 
 | 28 | { | 
 | 29 | 	if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) || | 
 | 30 | 	    (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) || | 
 | 31 | 	    (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT)) | 
 | 32 | 		return 0; | 
 | 33 | 	return 1; | 
 | 34 | } | 
 | 35 |  | 
 | 36 | static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu, | 
| Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 37 | 				      struct kvm_s390_interrupt_info *inti) | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 38 | { | 
 | 39 | 	switch (inti->type) { | 
 | 40 | 	case KVM_S390_INT_EMERGENCY: | 
 | 41 | 		if (psw_extint_disabled(vcpu)) | 
 | 42 | 			return 0; | 
 | 43 | 		if (vcpu->arch.sie_block->gcr[0] & 0x4000ul) | 
 | 44 | 			return 1; | 
 | 45 | 		return 0; | 
 | 46 | 	case KVM_S390_INT_SERVICE: | 
 | 47 | 		if (psw_extint_disabled(vcpu)) | 
 | 48 | 			return 0; | 
 | 49 | 		if (vcpu->arch.sie_block->gcr[0] & 0x200ul) | 
 | 50 | 			return 1; | 
 | 51 | 		return 0; | 
 | 52 | 	case KVM_S390_INT_VIRTIO: | 
 | 53 | 		if (psw_extint_disabled(vcpu)) | 
 | 54 | 			return 0; | 
 | 55 | 		if (vcpu->arch.sie_block->gcr[0] & 0x200ul) | 
 | 56 | 			return 1; | 
 | 57 | 		return 0; | 
 | 58 | 	case KVM_S390_PROGRAM_INT: | 
 | 59 | 	case KVM_S390_SIGP_STOP: | 
 | 60 | 	case KVM_S390_SIGP_SET_PREFIX: | 
 | 61 | 	case KVM_S390_RESTART: | 
 | 62 | 		return 1; | 
 | 63 | 	default: | 
 | 64 | 		BUG(); | 
 | 65 | 	} | 
 | 66 | 	return 0; | 
 | 67 | } | 
 | 68 |  | 
 | 69 | static void __set_cpu_idle(struct kvm_vcpu *vcpu) | 
 | 70 | { | 
 | 71 | 	BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1); | 
 | 72 | 	atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); | 
 | 73 | 	set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); | 
 | 74 | } | 
 | 75 |  | 
 | 76 | static void __unset_cpu_idle(struct kvm_vcpu *vcpu) | 
 | 77 | { | 
 | 78 | 	BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1); | 
 | 79 | 	atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); | 
 | 80 | 	clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); | 
 | 81 | } | 
 | 82 |  | 
 | 83 | static void __reset_intercept_indicators(struct kvm_vcpu *vcpu) | 
 | 84 | { | 
 | 85 | 	atomic_clear_mask(CPUSTAT_ECALL_PEND | | 
 | 86 | 		CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT, | 
 | 87 | 		&vcpu->arch.sie_block->cpuflags); | 
 | 88 | 	vcpu->arch.sie_block->lctl = 0x0000; | 
 | 89 | } | 
 | 90 |  | 
 | 91 | static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag) | 
 | 92 | { | 
 | 93 | 	atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags); | 
 | 94 | } | 
 | 95 |  | 
 | 96 | static void __set_intercept_indicator(struct kvm_vcpu *vcpu, | 
| Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 97 | 				      struct kvm_s390_interrupt_info *inti) | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 98 | { | 
 | 99 | 	switch (inti->type) { | 
 | 100 | 	case KVM_S390_INT_EMERGENCY: | 
 | 101 | 	case KVM_S390_INT_SERVICE: | 
 | 102 | 	case KVM_S390_INT_VIRTIO: | 
 | 103 | 		if (psw_extint_disabled(vcpu)) | 
 | 104 | 			__set_cpuflag(vcpu, CPUSTAT_EXT_INT); | 
 | 105 | 		else | 
 | 106 | 			vcpu->arch.sie_block->lctl |= LCTL_CR0; | 
 | 107 | 		break; | 
 | 108 | 	case KVM_S390_SIGP_STOP: | 
 | 109 | 		__set_cpuflag(vcpu, CPUSTAT_STOP_INT); | 
 | 110 | 		break; | 
 | 111 | 	default: | 
 | 112 | 		BUG(); | 
 | 113 | 	} | 
 | 114 | } | 
 | 115 |  | 
 | 116 | static void __do_deliver_interrupt(struct kvm_vcpu *vcpu, | 
| Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 117 | 				   struct kvm_s390_interrupt_info *inti) | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 118 | { | 
 | 119 | 	const unsigned short table[] = { 2, 4, 4, 6 }; | 
 | 120 | 	int rc, exception = 0; | 
 | 121 |  | 
 | 122 | 	switch (inti->type) { | 
 | 123 | 	case KVM_S390_INT_EMERGENCY: | 
 | 124 | 		VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg"); | 
 | 125 | 		vcpu->stat.deliver_emergency_signal++; | 
 | 126 | 		rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1201); | 
 | 127 | 		if (rc == -EFAULT) | 
 | 128 | 			exception = 1; | 
 | 129 |  | 
 | 130 | 		rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW, | 
 | 131 | 			 &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | 
 | 132 | 		if (rc == -EFAULT) | 
 | 133 | 			exception = 1; | 
 | 134 |  | 
 | 135 | 		rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, | 
 | 136 | 			__LC_EXT_NEW_PSW, sizeof(psw_t)); | 
 | 137 | 		if (rc == -EFAULT) | 
 | 138 | 			exception = 1; | 
 | 139 | 		break; | 
 | 140 |  | 
 | 141 | 	case KVM_S390_INT_SERVICE: | 
 | 142 | 		VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x", | 
 | 143 | 			   inti->ext.ext_params); | 
 | 144 | 		vcpu->stat.deliver_service_signal++; | 
 | 145 | 		rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2401); | 
 | 146 | 		if (rc == -EFAULT) | 
 | 147 | 			exception = 1; | 
 | 148 |  | 
 | 149 | 		rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW, | 
 | 150 | 			 &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | 
 | 151 | 		if (rc == -EFAULT) | 
 | 152 | 			exception = 1; | 
 | 153 |  | 
 | 154 | 		rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, | 
 | 155 | 			__LC_EXT_NEW_PSW, sizeof(psw_t)); | 
 | 156 | 		if (rc == -EFAULT) | 
 | 157 | 			exception = 1; | 
 | 158 |  | 
 | 159 | 		rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params); | 
 | 160 | 		if (rc == -EFAULT) | 
 | 161 | 			exception = 1; | 
 | 162 | 		break; | 
 | 163 |  | 
 | 164 | 	case KVM_S390_INT_VIRTIO: | 
| Heiko Carstens | 33e1911 | 2009-01-09 12:14:56 +0100 | [diff] [blame] | 165 | 		VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx", | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 166 | 			   inti->ext.ext_params, inti->ext.ext_params2); | 
 | 167 | 		vcpu->stat.deliver_virtio_interrupt++; | 
 | 168 | 		rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2603); | 
 | 169 | 		if (rc == -EFAULT) | 
 | 170 | 			exception = 1; | 
 | 171 |  | 
 | 172 | 		rc = put_guest_u16(vcpu, __LC_CPU_ADDRESS, 0x0d00); | 
 | 173 | 		if (rc == -EFAULT) | 
 | 174 | 			exception = 1; | 
 | 175 |  | 
 | 176 | 		rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW, | 
 | 177 | 			 &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | 
 | 178 | 		if (rc == -EFAULT) | 
 | 179 | 			exception = 1; | 
 | 180 |  | 
 | 181 | 		rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, | 
 | 182 | 			__LC_EXT_NEW_PSW, sizeof(psw_t)); | 
 | 183 | 		if (rc == -EFAULT) | 
 | 184 | 			exception = 1; | 
 | 185 |  | 
 | 186 | 		rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params); | 
 | 187 | 		if (rc == -EFAULT) | 
 | 188 | 			exception = 1; | 
 | 189 |  | 
 | 190 | 		rc = put_guest_u64(vcpu, __LC_PFAULT_INTPARM, | 
 | 191 | 			inti->ext.ext_params2); | 
 | 192 | 		if (rc == -EFAULT) | 
 | 193 | 			exception = 1; | 
 | 194 | 		break; | 
 | 195 |  | 
 | 196 | 	case KVM_S390_SIGP_STOP: | 
 | 197 | 		VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop"); | 
 | 198 | 		vcpu->stat.deliver_stop_signal++; | 
 | 199 | 		__set_intercept_indicator(vcpu, inti); | 
 | 200 | 		break; | 
 | 201 |  | 
 | 202 | 	case KVM_S390_SIGP_SET_PREFIX: | 
 | 203 | 		VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", | 
 | 204 | 			   inti->prefix.address); | 
 | 205 | 		vcpu->stat.deliver_prefix_signal++; | 
 | 206 | 		vcpu->arch.sie_block->prefix = inti->prefix.address; | 
 | 207 | 		vcpu->arch.sie_block->ihcpu = 0xffff; | 
 | 208 | 		break; | 
 | 209 |  | 
 | 210 | 	case KVM_S390_RESTART: | 
 | 211 | 		VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart"); | 
 | 212 | 		vcpu->stat.deliver_restart_signal++; | 
 | 213 | 		rc = copy_to_guest(vcpu, offsetof(struct _lowcore, | 
 | 214 | 		  restart_old_psw), &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | 
 | 215 | 		if (rc == -EFAULT) | 
 | 216 | 			exception = 1; | 
 | 217 |  | 
 | 218 | 		rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, | 
 | 219 | 			offsetof(struct _lowcore, restart_psw), sizeof(psw_t)); | 
 | 220 | 		if (rc == -EFAULT) | 
 | 221 | 			exception = 1; | 
 | 222 | 		break; | 
 | 223 |  | 
 | 224 | 	case KVM_S390_PROGRAM_INT: | 
 | 225 | 		VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x", | 
 | 226 | 			   inti->pgm.code, | 
 | 227 | 			   table[vcpu->arch.sie_block->ipa >> 14]); | 
 | 228 | 		vcpu->stat.deliver_program_int++; | 
 | 229 | 		rc = put_guest_u16(vcpu, __LC_PGM_INT_CODE, inti->pgm.code); | 
 | 230 | 		if (rc == -EFAULT) | 
 | 231 | 			exception = 1; | 
 | 232 |  | 
 | 233 | 		rc = put_guest_u16(vcpu, __LC_PGM_ILC, | 
 | 234 | 			table[vcpu->arch.sie_block->ipa >> 14]); | 
 | 235 | 		if (rc == -EFAULT) | 
 | 236 | 			exception = 1; | 
 | 237 |  | 
 | 238 | 		rc = copy_to_guest(vcpu, __LC_PGM_OLD_PSW, | 
 | 239 | 			 &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | 
 | 240 | 		if (rc == -EFAULT) | 
 | 241 | 			exception = 1; | 
 | 242 |  | 
 | 243 | 		rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, | 
 | 244 | 			__LC_PGM_NEW_PSW, sizeof(psw_t)); | 
 | 245 | 		if (rc == -EFAULT) | 
 | 246 | 			exception = 1; | 
 | 247 | 		break; | 
 | 248 |  | 
 | 249 | 	default: | 
 | 250 | 		BUG(); | 
 | 251 | 	} | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 252 | 	if (exception) { | 
| Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 253 | 		printk("kvm: The guest lowcore is not mapped during interrupt " | 
 | 254 | 			"delivery, killing userspace\n"); | 
 | 255 | 		do_exit(SIGKILL); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 256 | 	} | 
 | 257 | } | 
 | 258 |  | 
 | 259 | static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu) | 
 | 260 | { | 
 | 261 | 	int rc, exception = 0; | 
 | 262 |  | 
 | 263 | 	if (psw_extint_disabled(vcpu)) | 
 | 264 | 		return 0; | 
 | 265 | 	if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul)) | 
 | 266 | 		return 0; | 
 | 267 | 	rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1004); | 
 | 268 | 	if (rc == -EFAULT) | 
 | 269 | 		exception = 1; | 
 | 270 | 	rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW, | 
 | 271 | 		 &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | 
 | 272 | 	if (rc == -EFAULT) | 
 | 273 | 		exception = 1; | 
 | 274 | 	rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, | 
 | 275 | 		__LC_EXT_NEW_PSW, sizeof(psw_t)); | 
 | 276 | 	if (rc == -EFAULT) | 
 | 277 | 		exception = 1; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 278 | 	if (exception) { | 
| Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 279 | 		printk("kvm: The guest lowcore is not mapped during interrupt " | 
 | 280 | 			"delivery, killing userspace\n"); | 
 | 281 | 		do_exit(SIGKILL); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 282 | 	} | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 283 | 	return 1; | 
 | 284 | } | 
 | 285 |  | 
| Gleb Natapov | a1b3710 | 2009-07-09 15:33:52 +0300 | [diff] [blame] | 286 | static int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu) | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 287 | { | 
| Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 288 | 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | 
 | 289 | 	struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; | 
 | 290 | 	struct kvm_s390_interrupt_info  *inti; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 291 | 	int rc = 0; | 
 | 292 |  | 
 | 293 | 	if (atomic_read(&li->active)) { | 
 | 294 | 		spin_lock_bh(&li->lock); | 
 | 295 | 		list_for_each_entry(inti, &li->list, list) | 
 | 296 | 			if (__interrupt_is_deliverable(vcpu, inti)) { | 
 | 297 | 				rc = 1; | 
 | 298 | 				break; | 
 | 299 | 			} | 
 | 300 | 		spin_unlock_bh(&li->lock); | 
 | 301 | 	} | 
 | 302 |  | 
 | 303 | 	if ((!rc) && atomic_read(&fi->active)) { | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 304 | 		spin_lock(&fi->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 305 | 		list_for_each_entry(inti, &fi->list, list) | 
 | 306 | 			if (__interrupt_is_deliverable(vcpu, inti)) { | 
 | 307 | 				rc = 1; | 
 | 308 | 				break; | 
 | 309 | 			} | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 310 | 		spin_unlock(&fi->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 311 | 	} | 
 | 312 |  | 
 | 313 | 	if ((!rc) && (vcpu->arch.sie_block->ckc < | 
 | 314 | 		get_clock() + vcpu->arch.sie_block->epoch)) { | 
 | 315 | 		if ((!psw_extint_disabled(vcpu)) && | 
 | 316 | 			(vcpu->arch.sie_block->gcr[0] & 0x800ul)) | 
 | 317 | 			rc = 1; | 
 | 318 | 	} | 
 | 319 |  | 
 | 320 | 	return rc; | 
 | 321 | } | 
 | 322 |  | 
| Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 323 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) | 
 | 324 | { | 
 | 325 | 	return 0; | 
 | 326 | } | 
 | 327 |  | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 328 | int kvm_s390_handle_wait(struct kvm_vcpu *vcpu) | 
 | 329 | { | 
 | 330 | 	u64 now, sltime; | 
 | 331 | 	DECLARE_WAITQUEUE(wait, current); | 
 | 332 |  | 
 | 333 | 	vcpu->stat.exit_wait_state++; | 
 | 334 | 	if (kvm_cpu_has_interrupt(vcpu)) | 
 | 335 | 		return 0; | 
 | 336 |  | 
| Carsten Otte | e52b2af | 2008-05-21 13:37:44 +0200 | [diff] [blame] | 337 | 	__set_cpu_idle(vcpu); | 
 | 338 | 	spin_lock_bh(&vcpu->arch.local_int.lock); | 
 | 339 | 	vcpu->arch.local_int.timer_due = 0; | 
 | 340 | 	spin_unlock_bh(&vcpu->arch.local_int.lock); | 
 | 341 |  | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 342 | 	if (psw_interrupts_disabled(vcpu)) { | 
 | 343 | 		VCPU_EVENT(vcpu, 3, "%s", "disabled wait"); | 
 | 344 | 		__unset_cpu_idle(vcpu); | 
 | 345 | 		return -ENOTSUPP; /* disabled wait */ | 
 | 346 | 	} | 
 | 347 |  | 
 | 348 | 	if (psw_extint_disabled(vcpu) || | 
 | 349 | 	    (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) { | 
 | 350 | 		VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer"); | 
 | 351 | 		goto no_timer; | 
 | 352 | 	} | 
 | 353 |  | 
 | 354 | 	now = get_clock() + vcpu->arch.sie_block->epoch; | 
 | 355 | 	if (vcpu->arch.sie_block->ckc < now) { | 
 | 356 | 		__unset_cpu_idle(vcpu); | 
 | 357 | 		return 0; | 
 | 358 | 	} | 
 | 359 |  | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 360 | 	sltime = ((vcpu->arch.sie_block->ckc - now)*125)>>9; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 361 |  | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 362 | 	hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL); | 
 | 363 | 	VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 364 | no_timer: | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 365 | 	spin_lock(&vcpu->arch.local_int.float_int->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 366 | 	spin_lock_bh(&vcpu->arch.local_int.lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 367 | 	add_wait_queue(&vcpu->arch.local_int.wq, &wait); | 
 | 368 | 	while (list_empty(&vcpu->arch.local_int.list) && | 
 | 369 | 		list_empty(&vcpu->arch.local_int.float_int->list) && | 
 | 370 | 		(!vcpu->arch.local_int.timer_due) && | 
 | 371 | 		!signal_pending(current)) { | 
 | 372 | 		set_current_state(TASK_INTERRUPTIBLE); | 
 | 373 | 		spin_unlock_bh(&vcpu->arch.local_int.lock); | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 374 | 		spin_unlock(&vcpu->arch.local_int.float_int->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 375 | 		vcpu_put(vcpu); | 
 | 376 | 		schedule(); | 
 | 377 | 		vcpu_load(vcpu); | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 378 | 		spin_lock(&vcpu->arch.local_int.float_int->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 379 | 		spin_lock_bh(&vcpu->arch.local_int.lock); | 
 | 380 | 	} | 
 | 381 | 	__unset_cpu_idle(vcpu); | 
 | 382 | 	__set_current_state(TASK_RUNNING); | 
| Christian Borntraeger | d3bc2f9 | 2009-07-16 17:17:37 +0200 | [diff] [blame] | 383 | 	remove_wait_queue(&vcpu->arch.local_int.wq, &wait); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 384 | 	spin_unlock_bh(&vcpu->arch.local_int.lock); | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 385 | 	spin_unlock(&vcpu->arch.local_int.float_int->lock); | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 386 | 	hrtimer_try_to_cancel(&vcpu->arch.ckc_timer); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 387 | 	return 0; | 
 | 388 | } | 
 | 389 |  | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 390 | void kvm_s390_tasklet(unsigned long parm) | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 391 | { | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 392 | 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 393 |  | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 394 | 	spin_lock(&vcpu->arch.local_int.lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 395 | 	vcpu->arch.local_int.timer_due = 1; | 
 | 396 | 	if (waitqueue_active(&vcpu->arch.local_int.wq)) | 
 | 397 | 		wake_up_interruptible(&vcpu->arch.local_int.wq); | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 398 | 	spin_unlock(&vcpu->arch.local_int.lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 399 | } | 
 | 400 |  | 
| Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 401 | /* | 
 | 402 |  * low level hrtimer wake routine. Because this runs in hardirq context | 
 | 403 |  * we schedule a tasklet to do the real work. | 
 | 404 |  */ | 
 | 405 | enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer) | 
 | 406 | { | 
 | 407 | 	struct kvm_vcpu *vcpu; | 
 | 408 |  | 
 | 409 | 	vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer); | 
 | 410 | 	tasklet_schedule(&vcpu->arch.tasklet); | 
 | 411 |  | 
 | 412 | 	return HRTIMER_NORESTART; | 
 | 413 | } | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 414 |  | 
 | 415 | void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu) | 
 | 416 | { | 
| Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 417 | 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | 
 | 418 | 	struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; | 
 | 419 | 	struct kvm_s390_interrupt_info  *n, *inti = NULL; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 420 | 	int deliver; | 
 | 421 |  | 
 | 422 | 	__reset_intercept_indicators(vcpu); | 
 | 423 | 	if (atomic_read(&li->active)) { | 
 | 424 | 		do { | 
 | 425 | 			deliver = 0; | 
 | 426 | 			spin_lock_bh(&li->lock); | 
 | 427 | 			list_for_each_entry_safe(inti, n, &li->list, list) { | 
 | 428 | 				if (__interrupt_is_deliverable(vcpu, inti)) { | 
 | 429 | 					list_del(&inti->list); | 
 | 430 | 					deliver = 1; | 
 | 431 | 					break; | 
 | 432 | 				} | 
 | 433 | 				__set_intercept_indicator(vcpu, inti); | 
 | 434 | 			} | 
 | 435 | 			if (list_empty(&li->list)) | 
 | 436 | 				atomic_set(&li->active, 0); | 
 | 437 | 			spin_unlock_bh(&li->lock); | 
 | 438 | 			if (deliver) { | 
 | 439 | 				__do_deliver_interrupt(vcpu, inti); | 
 | 440 | 				kfree(inti); | 
 | 441 | 			} | 
 | 442 | 		} while (deliver); | 
 | 443 | 	} | 
 | 444 |  | 
 | 445 | 	if ((vcpu->arch.sie_block->ckc < | 
 | 446 | 		get_clock() + vcpu->arch.sie_block->epoch)) | 
 | 447 | 		__try_deliver_ckc_interrupt(vcpu); | 
 | 448 |  | 
 | 449 | 	if (atomic_read(&fi->active)) { | 
 | 450 | 		do { | 
 | 451 | 			deliver = 0; | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 452 | 			spin_lock(&fi->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 453 | 			list_for_each_entry_safe(inti, n, &fi->list, list) { | 
 | 454 | 				if (__interrupt_is_deliverable(vcpu, inti)) { | 
 | 455 | 					list_del(&inti->list); | 
 | 456 | 					deliver = 1; | 
 | 457 | 					break; | 
 | 458 | 				} | 
 | 459 | 				__set_intercept_indicator(vcpu, inti); | 
 | 460 | 			} | 
 | 461 | 			if (list_empty(&fi->list)) | 
 | 462 | 				atomic_set(&fi->active, 0); | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 463 | 			spin_unlock(&fi->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 464 | 			if (deliver) { | 
 | 465 | 				__do_deliver_interrupt(vcpu, inti); | 
 | 466 | 				kfree(inti); | 
 | 467 | 			} | 
 | 468 | 		} while (deliver); | 
 | 469 | 	} | 
 | 470 | } | 
 | 471 |  | 
 | 472 | int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code) | 
 | 473 | { | 
| Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 474 | 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | 
 | 475 | 	struct kvm_s390_interrupt_info *inti; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 476 |  | 
 | 477 | 	inti = kzalloc(sizeof(*inti), GFP_KERNEL); | 
 | 478 | 	if (!inti) | 
 | 479 | 		return -ENOMEM; | 
 | 480 |  | 
| Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 481 | 	inti->type = KVM_S390_PROGRAM_INT; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 482 | 	inti->pgm.code = code; | 
 | 483 |  | 
 | 484 | 	VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code); | 
 | 485 | 	spin_lock_bh(&li->lock); | 
 | 486 | 	list_add(&inti->list, &li->list); | 
 | 487 | 	atomic_set(&li->active, 1); | 
 | 488 | 	BUG_ON(waitqueue_active(&li->wq)); | 
 | 489 | 	spin_unlock_bh(&li->lock); | 
 | 490 | 	return 0; | 
 | 491 | } | 
 | 492 |  | 
 | 493 | int kvm_s390_inject_vm(struct kvm *kvm, | 
 | 494 | 		       struct kvm_s390_interrupt *s390int) | 
 | 495 | { | 
| Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 496 | 	struct kvm_s390_local_interrupt *li; | 
 | 497 | 	struct kvm_s390_float_interrupt *fi; | 
 | 498 | 	struct kvm_s390_interrupt_info *inti; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 499 | 	int sigcpu; | 
 | 500 |  | 
 | 501 | 	inti = kzalloc(sizeof(*inti), GFP_KERNEL); | 
 | 502 | 	if (!inti) | 
 | 503 | 		return -ENOMEM; | 
 | 504 |  | 
 | 505 | 	switch (s390int->type) { | 
 | 506 | 	case KVM_S390_INT_VIRTIO: | 
| Heiko Carstens | 33e1911 | 2009-01-09 12:14:56 +0100 | [diff] [blame] | 507 | 		VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx", | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 508 | 			 s390int->parm, s390int->parm64); | 
 | 509 | 		inti->type = s390int->type; | 
 | 510 | 		inti->ext.ext_params = s390int->parm; | 
 | 511 | 		inti->ext.ext_params2 = s390int->parm64; | 
 | 512 | 		break; | 
 | 513 | 	case KVM_S390_INT_SERVICE: | 
 | 514 | 		VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm); | 
 | 515 | 		inti->type = s390int->type; | 
 | 516 | 		inti->ext.ext_params = s390int->parm; | 
 | 517 | 		break; | 
 | 518 | 	case KVM_S390_PROGRAM_INT: | 
 | 519 | 	case KVM_S390_SIGP_STOP: | 
 | 520 | 	case KVM_S390_INT_EMERGENCY: | 
 | 521 | 	default: | 
 | 522 | 		kfree(inti); | 
 | 523 | 		return -EINVAL; | 
 | 524 | 	} | 
 | 525 |  | 
 | 526 | 	mutex_lock(&kvm->lock); | 
 | 527 | 	fi = &kvm->arch.float_int; | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 528 | 	spin_lock(&fi->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 529 | 	list_add_tail(&inti->list, &fi->list); | 
 | 530 | 	atomic_set(&fi->active, 1); | 
 | 531 | 	sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS); | 
 | 532 | 	if (sigcpu == KVM_MAX_VCPUS) { | 
 | 533 | 		do { | 
 | 534 | 			sigcpu = fi->next_rr_cpu++; | 
 | 535 | 			if (sigcpu == KVM_MAX_VCPUS) | 
 | 536 | 				sigcpu = fi->next_rr_cpu = 0; | 
 | 537 | 		} while (fi->local_int[sigcpu] == NULL); | 
 | 538 | 	} | 
 | 539 | 	li = fi->local_int[sigcpu]; | 
 | 540 | 	spin_lock_bh(&li->lock); | 
 | 541 | 	atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); | 
 | 542 | 	if (waitqueue_active(&li->wq)) | 
 | 543 | 		wake_up_interruptible(&li->wq); | 
 | 544 | 	spin_unlock_bh(&li->lock); | 
| Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 545 | 	spin_unlock(&fi->lock); | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 546 | 	mutex_unlock(&kvm->lock); | 
 | 547 | 	return 0; | 
 | 548 | } | 
 | 549 |  | 
 | 550 | int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, | 
 | 551 | 			 struct kvm_s390_interrupt *s390int) | 
 | 552 | { | 
| Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 553 | 	struct kvm_s390_local_interrupt *li; | 
 | 554 | 	struct kvm_s390_interrupt_info *inti; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 555 |  | 
 | 556 | 	inti = kzalloc(sizeof(*inti), GFP_KERNEL); | 
 | 557 | 	if (!inti) | 
 | 558 | 		return -ENOMEM; | 
 | 559 |  | 
 | 560 | 	switch (s390int->type) { | 
 | 561 | 	case KVM_S390_PROGRAM_INT: | 
 | 562 | 		if (s390int->parm & 0xffff0000) { | 
 | 563 | 			kfree(inti); | 
 | 564 | 			return -EINVAL; | 
 | 565 | 		} | 
 | 566 | 		inti->type = s390int->type; | 
 | 567 | 		inti->pgm.code = s390int->parm; | 
 | 568 | 		VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)", | 
 | 569 | 			   s390int->parm); | 
 | 570 | 		break; | 
| Christian Borntraeger | b7e6e4d | 2009-01-22 10:29:08 +0100 | [diff] [blame] | 571 | 	case KVM_S390_SIGP_SET_PREFIX: | 
 | 572 | 		inti->prefix.address = s390int->parm; | 
 | 573 | 		inti->type = s390int->type; | 
 | 574 | 		VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)", | 
 | 575 | 			   s390int->parm); | 
 | 576 | 		break; | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 577 | 	case KVM_S390_SIGP_STOP: | 
 | 578 | 	case KVM_S390_RESTART: | 
| Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 579 | 	case KVM_S390_INT_EMERGENCY: | 
 | 580 | 		VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type); | 
 | 581 | 		inti->type = s390int->type; | 
 | 582 | 		break; | 
 | 583 | 	case KVM_S390_INT_VIRTIO: | 
 | 584 | 	case KVM_S390_INT_SERVICE: | 
 | 585 | 	default: | 
 | 586 | 		kfree(inti); | 
 | 587 | 		return -EINVAL; | 
 | 588 | 	} | 
 | 589 |  | 
 | 590 | 	mutex_lock(&vcpu->kvm->lock); | 
 | 591 | 	li = &vcpu->arch.local_int; | 
 | 592 | 	spin_lock_bh(&li->lock); | 
 | 593 | 	if (inti->type == KVM_S390_PROGRAM_INT) | 
 | 594 | 		list_add(&inti->list, &li->list); | 
 | 595 | 	else | 
 | 596 | 		list_add_tail(&inti->list, &li->list); | 
 | 597 | 	atomic_set(&li->active, 1); | 
 | 598 | 	if (inti->type == KVM_S390_SIGP_STOP) | 
 | 599 | 		li->action_bits |= ACTION_STOP_ON_STOP; | 
 | 600 | 	atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); | 
 | 601 | 	if (waitqueue_active(&li->wq)) | 
 | 602 | 		wake_up_interruptible(&vcpu->arch.local_int.wq); | 
 | 603 | 	spin_unlock_bh(&li->lock); | 
 | 604 | 	mutex_unlock(&vcpu->kvm->lock); | 
 | 605 | 	return 0; | 
 | 606 | } |