interrupt.c 17 KB

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