interrupt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * 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. #include "trace-s390.h"
  22. #define IOINT_SCHID_MASK 0x0000ffff
  23. #define IOINT_SSID_MASK 0x00030000
  24. #define IOINT_CSSID_MASK 0x03fc0000
  25. #define IOINT_AI_MASK 0x04000000
  26. static int is_ioint(u64 type)
  27. {
  28. return ((type & 0xfffe0000u) != 0xfffe0000u);
  29. }
  30. static int psw_extint_disabled(struct kvm_vcpu *vcpu)
  31. {
  32. return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
  33. }
  34. static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
  35. {
  36. return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
  37. }
  38. static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
  39. {
  40. return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
  41. }
  42. static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
  43. {
  44. if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
  45. (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
  46. (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
  47. return 0;
  48. return 1;
  49. }
  50. static u64 int_word_to_isc_bits(u32 int_word)
  51. {
  52. u8 isc = (int_word & 0x38000000) >> 27;
  53. return (0x80 >> isc) << 24;
  54. }
  55. static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
  56. struct kvm_s390_interrupt_info *inti)
  57. {
  58. switch (inti->type) {
  59. case KVM_S390_INT_EXTERNAL_CALL:
  60. if (psw_extint_disabled(vcpu))
  61. return 0;
  62. if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
  63. return 1;
  64. case KVM_S390_INT_EMERGENCY:
  65. if (psw_extint_disabled(vcpu))
  66. return 0;
  67. if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
  68. return 1;
  69. return 0;
  70. case KVM_S390_INT_SERVICE:
  71. if (psw_extint_disabled(vcpu))
  72. return 0;
  73. if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
  74. return 1;
  75. return 0;
  76. case KVM_S390_INT_VIRTIO:
  77. if (psw_extint_disabled(vcpu))
  78. return 0;
  79. if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
  80. return 1;
  81. return 0;
  82. case KVM_S390_PROGRAM_INT:
  83. case KVM_S390_SIGP_STOP:
  84. case KVM_S390_SIGP_SET_PREFIX:
  85. case KVM_S390_RESTART:
  86. return 1;
  87. case KVM_S390_MCHK:
  88. if (psw_mchk_disabled(vcpu))
  89. return 0;
  90. if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
  91. return 1;
  92. return 0;
  93. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  94. if (psw_ioint_disabled(vcpu))
  95. return 0;
  96. if (vcpu->arch.sie_block->gcr[6] &
  97. int_word_to_isc_bits(inti->io.io_int_word))
  98. return 1;
  99. return 0;
  100. default:
  101. printk(KERN_WARNING "illegal interrupt type %llx\n",
  102. inti->type);
  103. BUG();
  104. }
  105. return 0;
  106. }
  107. static void __set_cpu_idle(struct kvm_vcpu *vcpu)
  108. {
  109. BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
  110. atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
  111. set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
  112. }
  113. static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
  114. {
  115. BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
  116. atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
  117. clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
  118. }
  119. static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
  120. {
  121. atomic_clear_mask(CPUSTAT_ECALL_PEND |
  122. CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
  123. &vcpu->arch.sie_block->cpuflags);
  124. vcpu->arch.sie_block->lctl = 0x0000;
  125. vcpu->arch.sie_block->ictl &= ~ICTL_LPSW;
  126. }
  127. static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
  128. {
  129. atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
  130. }
  131. static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
  132. struct kvm_s390_interrupt_info *inti)
  133. {
  134. switch (inti->type) {
  135. case KVM_S390_INT_EXTERNAL_CALL:
  136. case KVM_S390_INT_EMERGENCY:
  137. case KVM_S390_INT_SERVICE:
  138. case KVM_S390_INT_VIRTIO:
  139. if (psw_extint_disabled(vcpu))
  140. __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
  141. else
  142. vcpu->arch.sie_block->lctl |= LCTL_CR0;
  143. break;
  144. case KVM_S390_SIGP_STOP:
  145. __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
  146. break;
  147. case KVM_S390_MCHK:
  148. if (psw_mchk_disabled(vcpu))
  149. vcpu->arch.sie_block->ictl |= ICTL_LPSW;
  150. else
  151. vcpu->arch.sie_block->lctl |= LCTL_CR14;
  152. break;
  153. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  154. if (psw_ioint_disabled(vcpu))
  155. __set_cpuflag(vcpu, CPUSTAT_IO_INT);
  156. else
  157. vcpu->arch.sie_block->lctl |= LCTL_CR6;
  158. break;
  159. default:
  160. BUG();
  161. }
  162. }
  163. static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
  164. struct kvm_s390_interrupt_info *inti)
  165. {
  166. const unsigned short table[] = { 2, 4, 4, 6 };
  167. int rc = 0;
  168. switch (inti->type) {
  169. case KVM_S390_INT_EMERGENCY:
  170. VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
  171. vcpu->stat.deliver_emergency_signal++;
  172. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  173. inti->emerg.code, 0);
  174. rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1201);
  175. rc |= put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->emerg.code);
  176. rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
  177. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  178. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  179. __LC_EXT_NEW_PSW, sizeof(psw_t));
  180. break;
  181. case KVM_S390_INT_EXTERNAL_CALL:
  182. VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
  183. vcpu->stat.deliver_external_call++;
  184. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  185. inti->extcall.code, 0);
  186. rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1202);
  187. rc |= put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->extcall.code);
  188. rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
  189. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  190. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  191. __LC_EXT_NEW_PSW, sizeof(psw_t));
  192. break;
  193. case KVM_S390_INT_SERVICE:
  194. VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
  195. inti->ext.ext_params);
  196. vcpu->stat.deliver_service_signal++;
  197. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  198. inti->ext.ext_params, 0);
  199. rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2401);
  200. rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
  201. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  202. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  203. __LC_EXT_NEW_PSW, sizeof(psw_t));
  204. rc |= put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
  205. break;
  206. case KVM_S390_INT_VIRTIO:
  207. VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
  208. inti->ext.ext_params, inti->ext.ext_params2);
  209. vcpu->stat.deliver_virtio_interrupt++;
  210. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  211. inti->ext.ext_params,
  212. inti->ext.ext_params2);
  213. rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2603);
  214. rc |= put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, 0x0d00);
  215. rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
  216. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  217. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  218. __LC_EXT_NEW_PSW, sizeof(psw_t));
  219. rc |= put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
  220. rc |= put_guest_u64(vcpu, __LC_EXT_PARAMS2,
  221. inti->ext.ext_params2);
  222. break;
  223. case KVM_S390_SIGP_STOP:
  224. VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
  225. vcpu->stat.deliver_stop_signal++;
  226. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  227. 0, 0);
  228. __set_intercept_indicator(vcpu, inti);
  229. break;
  230. case KVM_S390_SIGP_SET_PREFIX:
  231. VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
  232. inti->prefix.address);
  233. vcpu->stat.deliver_prefix_signal++;
  234. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  235. inti->prefix.address, 0);
  236. kvm_s390_set_prefix(vcpu, inti->prefix.address);
  237. break;
  238. case KVM_S390_RESTART:
  239. VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
  240. vcpu->stat.deliver_restart_signal++;
  241. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  242. 0, 0);
  243. rc = copy_to_guest(vcpu,
  244. offsetof(struct _lowcore, restart_old_psw),
  245. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  246. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  247. offsetof(struct _lowcore, restart_psw),
  248. sizeof(psw_t));
  249. atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
  250. break;
  251. case KVM_S390_PROGRAM_INT:
  252. VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
  253. inti->pgm.code,
  254. table[vcpu->arch.sie_block->ipa >> 14]);
  255. vcpu->stat.deliver_program_int++;
  256. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  257. inti->pgm.code, 0);
  258. rc = put_guest_u16(vcpu, __LC_PGM_INT_CODE, inti->pgm.code);
  259. rc |= put_guest_u16(vcpu, __LC_PGM_ILC,
  260. table[vcpu->arch.sie_block->ipa >> 14]);
  261. rc |= copy_to_guest(vcpu, __LC_PGM_OLD_PSW,
  262. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  263. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  264. __LC_PGM_NEW_PSW, sizeof(psw_t));
  265. break;
  266. case KVM_S390_MCHK:
  267. VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
  268. inti->mchk.mcic);
  269. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  270. inti->mchk.cr14,
  271. inti->mchk.mcic);
  272. rc = kvm_s390_vcpu_store_status(vcpu,
  273. KVM_S390_STORE_STATUS_PREFIXED);
  274. rc |= put_guest_u64(vcpu, __LC_MCCK_CODE, inti->mchk.mcic);
  275. rc |= copy_to_guest(vcpu, __LC_MCK_OLD_PSW,
  276. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  277. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  278. __LC_MCK_NEW_PSW, sizeof(psw_t));
  279. break;
  280. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  281. {
  282. __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
  283. inti->io.subchannel_nr;
  284. __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
  285. inti->io.io_int_word;
  286. VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
  287. vcpu->stat.deliver_io_int++;
  288. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
  289. param0, param1);
  290. rc = put_guest_u16(vcpu, __LC_SUBCHANNEL_ID,
  291. inti->io.subchannel_id);
  292. rc |= put_guest_u16(vcpu, __LC_SUBCHANNEL_NR,
  293. inti->io.subchannel_nr);
  294. rc |= put_guest_u32(vcpu, __LC_IO_INT_PARM,
  295. inti->io.io_int_parm);
  296. rc |= put_guest_u32(vcpu, __LC_IO_INT_WORD,
  297. inti->io.io_int_word);
  298. rc |= copy_to_guest(vcpu, __LC_IO_OLD_PSW,
  299. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  300. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  301. __LC_IO_NEW_PSW, sizeof(psw_t));
  302. break;
  303. }
  304. default:
  305. BUG();
  306. }
  307. if (rc) {
  308. printk("kvm: The guest lowcore is not mapped during interrupt "
  309. "delivery, killing userspace\n");
  310. do_exit(SIGKILL);
  311. }
  312. }
  313. static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
  314. {
  315. int rc;
  316. if (psw_extint_disabled(vcpu))
  317. return 0;
  318. if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
  319. return 0;
  320. rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1004);
  321. rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
  322. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  323. rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
  324. __LC_EXT_NEW_PSW, sizeof(psw_t));
  325. if (rc) {
  326. printk("kvm: The guest lowcore is not mapped during interrupt "
  327. "delivery, killing userspace\n");
  328. do_exit(SIGKILL);
  329. }
  330. return 1;
  331. }
  332. static int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
  333. {
  334. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  335. struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
  336. struct kvm_s390_interrupt_info *inti;
  337. int rc = 0;
  338. if (atomic_read(&li->active)) {
  339. spin_lock_bh(&li->lock);
  340. list_for_each_entry(inti, &li->list, list)
  341. if (__interrupt_is_deliverable(vcpu, inti)) {
  342. rc = 1;
  343. break;
  344. }
  345. spin_unlock_bh(&li->lock);
  346. }
  347. if ((!rc) && atomic_read(&fi->active)) {
  348. spin_lock(&fi->lock);
  349. list_for_each_entry(inti, &fi->list, list)
  350. if (__interrupt_is_deliverable(vcpu, inti)) {
  351. rc = 1;
  352. break;
  353. }
  354. spin_unlock(&fi->lock);
  355. }
  356. if ((!rc) && (vcpu->arch.sie_block->ckc <
  357. get_tod_clock() + vcpu->arch.sie_block->epoch)) {
  358. if ((!psw_extint_disabled(vcpu)) &&
  359. (vcpu->arch.sie_block->gcr[0] & 0x800ul))
  360. rc = 1;
  361. }
  362. return rc;
  363. }
  364. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  365. {
  366. return 0;
  367. }
  368. int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
  369. {
  370. u64 now, sltime;
  371. DECLARE_WAITQUEUE(wait, current);
  372. vcpu->stat.exit_wait_state++;
  373. if (kvm_cpu_has_interrupt(vcpu))
  374. return 0;
  375. __set_cpu_idle(vcpu);
  376. spin_lock_bh(&vcpu->arch.local_int.lock);
  377. vcpu->arch.local_int.timer_due = 0;
  378. spin_unlock_bh(&vcpu->arch.local_int.lock);
  379. if (psw_interrupts_disabled(vcpu)) {
  380. VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
  381. __unset_cpu_idle(vcpu);
  382. return -EOPNOTSUPP; /* disabled wait */
  383. }
  384. if (psw_extint_disabled(vcpu) ||
  385. (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
  386. VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
  387. goto no_timer;
  388. }
  389. now = get_tod_clock() + vcpu->arch.sie_block->epoch;
  390. if (vcpu->arch.sie_block->ckc < now) {
  391. __unset_cpu_idle(vcpu);
  392. return 0;
  393. }
  394. sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
  395. hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
  396. VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
  397. no_timer:
  398. spin_lock(&vcpu->arch.local_int.float_int->lock);
  399. spin_lock_bh(&vcpu->arch.local_int.lock);
  400. add_wait_queue(&vcpu->arch.local_int.wq, &wait);
  401. while (list_empty(&vcpu->arch.local_int.list) &&
  402. list_empty(&vcpu->arch.local_int.float_int->list) &&
  403. (!vcpu->arch.local_int.timer_due) &&
  404. !signal_pending(current)) {
  405. set_current_state(TASK_INTERRUPTIBLE);
  406. spin_unlock_bh(&vcpu->arch.local_int.lock);
  407. spin_unlock(&vcpu->arch.local_int.float_int->lock);
  408. schedule();
  409. spin_lock(&vcpu->arch.local_int.float_int->lock);
  410. spin_lock_bh(&vcpu->arch.local_int.lock);
  411. }
  412. __unset_cpu_idle(vcpu);
  413. __set_current_state(TASK_RUNNING);
  414. remove_wait_queue(&vcpu->arch.local_int.wq, &wait);
  415. spin_unlock_bh(&vcpu->arch.local_int.lock);
  416. spin_unlock(&vcpu->arch.local_int.float_int->lock);
  417. hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
  418. return 0;
  419. }
  420. void kvm_s390_tasklet(unsigned long parm)
  421. {
  422. struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
  423. spin_lock(&vcpu->arch.local_int.lock);
  424. vcpu->arch.local_int.timer_due = 1;
  425. if (waitqueue_active(&vcpu->arch.local_int.wq))
  426. wake_up_interruptible(&vcpu->arch.local_int.wq);
  427. spin_unlock(&vcpu->arch.local_int.lock);
  428. }
  429. /*
  430. * low level hrtimer wake routine. Because this runs in hardirq context
  431. * we schedule a tasklet to do the real work.
  432. */
  433. enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
  434. {
  435. struct kvm_vcpu *vcpu;
  436. vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
  437. tasklet_schedule(&vcpu->arch.tasklet);
  438. return HRTIMER_NORESTART;
  439. }
  440. void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
  441. {
  442. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  443. struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
  444. struct kvm_s390_interrupt_info *n, *inti = NULL;
  445. int deliver;
  446. __reset_intercept_indicators(vcpu);
  447. if (atomic_read(&li->active)) {
  448. do {
  449. deliver = 0;
  450. spin_lock_bh(&li->lock);
  451. list_for_each_entry_safe(inti, n, &li->list, list) {
  452. if (__interrupt_is_deliverable(vcpu, inti)) {
  453. list_del(&inti->list);
  454. deliver = 1;
  455. break;
  456. }
  457. __set_intercept_indicator(vcpu, inti);
  458. }
  459. if (list_empty(&li->list))
  460. atomic_set(&li->active, 0);
  461. spin_unlock_bh(&li->lock);
  462. if (deliver) {
  463. __do_deliver_interrupt(vcpu, inti);
  464. kfree(inti);
  465. }
  466. } while (deliver);
  467. }
  468. if ((vcpu->arch.sie_block->ckc <
  469. get_tod_clock() + vcpu->arch.sie_block->epoch))
  470. __try_deliver_ckc_interrupt(vcpu);
  471. if (atomic_read(&fi->active)) {
  472. do {
  473. deliver = 0;
  474. spin_lock(&fi->lock);
  475. list_for_each_entry_safe(inti, n, &fi->list, list) {
  476. if (__interrupt_is_deliverable(vcpu, inti)) {
  477. list_del(&inti->list);
  478. deliver = 1;
  479. break;
  480. }
  481. __set_intercept_indicator(vcpu, inti);
  482. }
  483. if (list_empty(&fi->list))
  484. atomic_set(&fi->active, 0);
  485. spin_unlock(&fi->lock);
  486. if (deliver) {
  487. __do_deliver_interrupt(vcpu, inti);
  488. kfree(inti);
  489. }
  490. } while (deliver);
  491. }
  492. }
  493. void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
  494. {
  495. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  496. struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
  497. struct kvm_s390_interrupt_info *n, *inti = NULL;
  498. int deliver;
  499. __reset_intercept_indicators(vcpu);
  500. if (atomic_read(&li->active)) {
  501. do {
  502. deliver = 0;
  503. spin_lock_bh(&li->lock);
  504. list_for_each_entry_safe(inti, n, &li->list, list) {
  505. if ((inti->type == KVM_S390_MCHK) &&
  506. __interrupt_is_deliverable(vcpu, inti)) {
  507. list_del(&inti->list);
  508. deliver = 1;
  509. break;
  510. }
  511. __set_intercept_indicator(vcpu, inti);
  512. }
  513. if (list_empty(&li->list))
  514. atomic_set(&li->active, 0);
  515. spin_unlock_bh(&li->lock);
  516. if (deliver) {
  517. __do_deliver_interrupt(vcpu, inti);
  518. kfree(inti);
  519. }
  520. } while (deliver);
  521. }
  522. if (atomic_read(&fi->active)) {
  523. do {
  524. deliver = 0;
  525. spin_lock(&fi->lock);
  526. list_for_each_entry_safe(inti, n, &fi->list, list) {
  527. if ((inti->type == KVM_S390_MCHK) &&
  528. __interrupt_is_deliverable(vcpu, inti)) {
  529. list_del(&inti->list);
  530. deliver = 1;
  531. break;
  532. }
  533. __set_intercept_indicator(vcpu, inti);
  534. }
  535. if (list_empty(&fi->list))
  536. atomic_set(&fi->active, 0);
  537. spin_unlock(&fi->lock);
  538. if (deliver) {
  539. __do_deliver_interrupt(vcpu, inti);
  540. kfree(inti);
  541. }
  542. } while (deliver);
  543. }
  544. }
  545. int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
  546. {
  547. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  548. struct kvm_s390_interrupt_info *inti;
  549. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  550. if (!inti)
  551. return -ENOMEM;
  552. inti->type = KVM_S390_PROGRAM_INT;
  553. inti->pgm.code = code;
  554. VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
  555. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
  556. spin_lock_bh(&li->lock);
  557. list_add(&inti->list, &li->list);
  558. atomic_set(&li->active, 1);
  559. BUG_ON(waitqueue_active(&li->wq));
  560. spin_unlock_bh(&li->lock);
  561. return 0;
  562. }
  563. struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
  564. u64 cr6, u64 schid)
  565. {
  566. struct kvm_s390_float_interrupt *fi;
  567. struct kvm_s390_interrupt_info *inti, *iter;
  568. if ((!schid && !cr6) || (schid && cr6))
  569. return NULL;
  570. mutex_lock(&kvm->lock);
  571. fi = &kvm->arch.float_int;
  572. spin_lock(&fi->lock);
  573. inti = NULL;
  574. list_for_each_entry(iter, &fi->list, list) {
  575. if (!is_ioint(iter->type))
  576. continue;
  577. if (cr6 &&
  578. ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
  579. continue;
  580. if (schid) {
  581. if (((schid & 0x00000000ffff0000) >> 16) !=
  582. iter->io.subchannel_id)
  583. continue;
  584. if ((schid & 0x000000000000ffff) !=
  585. iter->io.subchannel_nr)
  586. continue;
  587. }
  588. inti = iter;
  589. break;
  590. }
  591. if (inti)
  592. list_del_init(&inti->list);
  593. if (list_empty(&fi->list))
  594. atomic_set(&fi->active, 0);
  595. spin_unlock(&fi->lock);
  596. mutex_unlock(&kvm->lock);
  597. return inti;
  598. }
  599. int kvm_s390_inject_vm(struct kvm *kvm,
  600. struct kvm_s390_interrupt *s390int)
  601. {
  602. struct kvm_s390_local_interrupt *li;
  603. struct kvm_s390_float_interrupt *fi;
  604. struct kvm_s390_interrupt_info *inti, *iter;
  605. int sigcpu;
  606. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  607. if (!inti)
  608. return -ENOMEM;
  609. switch (s390int->type) {
  610. case KVM_S390_INT_VIRTIO:
  611. VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
  612. s390int->parm, s390int->parm64);
  613. inti->type = s390int->type;
  614. inti->ext.ext_params = s390int->parm;
  615. inti->ext.ext_params2 = s390int->parm64;
  616. break;
  617. case KVM_S390_INT_SERVICE:
  618. VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
  619. inti->type = s390int->type;
  620. inti->ext.ext_params = s390int->parm;
  621. break;
  622. case KVM_S390_PROGRAM_INT:
  623. case KVM_S390_SIGP_STOP:
  624. case KVM_S390_INT_EXTERNAL_CALL:
  625. case KVM_S390_INT_EMERGENCY:
  626. kfree(inti);
  627. return -EINVAL;
  628. case KVM_S390_MCHK:
  629. VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
  630. s390int->parm64);
  631. inti->type = s390int->type;
  632. inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
  633. inti->mchk.mcic = s390int->parm64;
  634. break;
  635. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  636. if (s390int->type & IOINT_AI_MASK)
  637. VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
  638. else
  639. VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
  640. s390int->type & IOINT_CSSID_MASK,
  641. s390int->type & IOINT_SSID_MASK,
  642. s390int->type & IOINT_SCHID_MASK);
  643. inti->type = s390int->type;
  644. inti->io.subchannel_id = s390int->parm >> 16;
  645. inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
  646. inti->io.io_int_parm = s390int->parm64 >> 32;
  647. inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
  648. break;
  649. default:
  650. kfree(inti);
  651. return -EINVAL;
  652. }
  653. trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
  654. 2);
  655. mutex_lock(&kvm->lock);
  656. fi = &kvm->arch.float_int;
  657. spin_lock(&fi->lock);
  658. if (!is_ioint(inti->type))
  659. list_add_tail(&inti->list, &fi->list);
  660. else {
  661. u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
  662. /* Keep I/O interrupts sorted in isc order. */
  663. list_for_each_entry(iter, &fi->list, list) {
  664. if (!is_ioint(iter->type))
  665. continue;
  666. if (int_word_to_isc_bits(iter->io.io_int_word)
  667. <= isc_bits)
  668. continue;
  669. break;
  670. }
  671. list_add_tail(&inti->list, &iter->list);
  672. }
  673. atomic_set(&fi->active, 1);
  674. sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
  675. if (sigcpu == KVM_MAX_VCPUS) {
  676. do {
  677. sigcpu = fi->next_rr_cpu++;
  678. if (sigcpu == KVM_MAX_VCPUS)
  679. sigcpu = fi->next_rr_cpu = 0;
  680. } while (fi->local_int[sigcpu] == NULL);
  681. }
  682. li = fi->local_int[sigcpu];
  683. spin_lock_bh(&li->lock);
  684. atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
  685. if (waitqueue_active(&li->wq))
  686. wake_up_interruptible(&li->wq);
  687. spin_unlock_bh(&li->lock);
  688. spin_unlock(&fi->lock);
  689. mutex_unlock(&kvm->lock);
  690. return 0;
  691. }
  692. int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
  693. struct kvm_s390_interrupt *s390int)
  694. {
  695. struct kvm_s390_local_interrupt *li;
  696. struct kvm_s390_interrupt_info *inti;
  697. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  698. if (!inti)
  699. return -ENOMEM;
  700. switch (s390int->type) {
  701. case KVM_S390_PROGRAM_INT:
  702. if (s390int->parm & 0xffff0000) {
  703. kfree(inti);
  704. return -EINVAL;
  705. }
  706. inti->type = s390int->type;
  707. inti->pgm.code = s390int->parm;
  708. VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
  709. s390int->parm);
  710. break;
  711. case KVM_S390_SIGP_SET_PREFIX:
  712. inti->prefix.address = s390int->parm;
  713. inti->type = s390int->type;
  714. VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
  715. s390int->parm);
  716. break;
  717. case KVM_S390_SIGP_STOP:
  718. case KVM_S390_RESTART:
  719. VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
  720. inti->type = s390int->type;
  721. break;
  722. case KVM_S390_INT_EXTERNAL_CALL:
  723. if (s390int->parm & 0xffff0000) {
  724. kfree(inti);
  725. return -EINVAL;
  726. }
  727. VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
  728. s390int->parm);
  729. inti->type = s390int->type;
  730. inti->extcall.code = s390int->parm;
  731. break;
  732. case KVM_S390_INT_EMERGENCY:
  733. if (s390int->parm & 0xffff0000) {
  734. kfree(inti);
  735. return -EINVAL;
  736. }
  737. VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
  738. inti->type = s390int->type;
  739. inti->emerg.code = s390int->parm;
  740. break;
  741. case KVM_S390_MCHK:
  742. VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
  743. s390int->parm64);
  744. inti->type = s390int->type;
  745. inti->mchk.mcic = s390int->parm64;
  746. break;
  747. case KVM_S390_INT_VIRTIO:
  748. case KVM_S390_INT_SERVICE:
  749. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  750. default:
  751. kfree(inti);
  752. return -EINVAL;
  753. }
  754. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
  755. s390int->parm64, 2);
  756. mutex_lock(&vcpu->kvm->lock);
  757. li = &vcpu->arch.local_int;
  758. spin_lock_bh(&li->lock);
  759. if (inti->type == KVM_S390_PROGRAM_INT)
  760. list_add(&inti->list, &li->list);
  761. else
  762. list_add_tail(&inti->list, &li->list);
  763. atomic_set(&li->active, 1);
  764. if (inti->type == KVM_S390_SIGP_STOP)
  765. li->action_bits |= ACTION_STOP_ON_STOP;
  766. atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
  767. if (waitqueue_active(&li->wq))
  768. wake_up_interruptible(&vcpu->arch.local_int.wq);
  769. spin_unlock_bh(&li->lock);
  770. mutex_unlock(&vcpu->kvm->lock);
  771. return 0;
  772. }