interrupt.c 17 KB

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