interrupt.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. /*
  2. * handling kvm guest interrupts
  3. *
  4. * Copyright IBM Corp. 2008, 2015
  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/mmu_context.h>
  16. #include <linux/signal.h>
  17. #include <linux/slab.h>
  18. #include <linux/bitmap.h>
  19. #include <linux/vmalloc.h>
  20. #include <asm/asm-offsets.h>
  21. #include <asm/dis.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/sclp.h>
  24. #include <asm/isc.h>
  25. #include "kvm-s390.h"
  26. #include "gaccess.h"
  27. #include "trace-s390.h"
  28. #define IOINT_SCHID_MASK 0x0000ffff
  29. #define IOINT_SSID_MASK 0x00030000
  30. #define IOINT_CSSID_MASK 0x03fc0000
  31. #define PFAULT_INIT 0x0600
  32. #define PFAULT_DONE 0x0680
  33. #define VIRTIO_PARAM 0x0d00
  34. int psw_extint_disabled(struct kvm_vcpu *vcpu)
  35. {
  36. return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
  37. }
  38. static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
  39. {
  40. return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
  41. }
  42. static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
  43. {
  44. return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
  45. }
  46. static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
  47. {
  48. return psw_extint_disabled(vcpu) &&
  49. psw_ioint_disabled(vcpu) &&
  50. psw_mchk_disabled(vcpu);
  51. }
  52. static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
  53. {
  54. if (psw_extint_disabled(vcpu) ||
  55. !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
  56. return 0;
  57. if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
  58. /* No timer interrupts when single stepping */
  59. return 0;
  60. return 1;
  61. }
  62. static int ckc_irq_pending(struct kvm_vcpu *vcpu)
  63. {
  64. if (vcpu->arch.sie_block->ckc >= kvm_s390_get_tod_clock_fast(vcpu->kvm))
  65. return 0;
  66. return ckc_interrupts_enabled(vcpu);
  67. }
  68. static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
  69. {
  70. return !psw_extint_disabled(vcpu) &&
  71. (vcpu->arch.sie_block->gcr[0] & 0x400ul);
  72. }
  73. static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
  74. {
  75. return (vcpu->arch.sie_block->cputm >> 63) &&
  76. cpu_timer_interrupts_enabled(vcpu);
  77. }
  78. static inline int is_ioirq(unsigned long irq_type)
  79. {
  80. return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
  81. (irq_type <= IRQ_PEND_IO_ISC_7));
  82. }
  83. static uint64_t isc_to_isc_bits(int isc)
  84. {
  85. return (0x80 >> isc) << 24;
  86. }
  87. static inline u8 int_word_to_isc(u32 int_word)
  88. {
  89. return (int_word & 0x38000000) >> 27;
  90. }
  91. static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
  92. {
  93. return vcpu->kvm->arch.float_int.pending_irqs |
  94. vcpu->arch.local_int.pending_irqs;
  95. }
  96. static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
  97. unsigned long active_mask)
  98. {
  99. int i;
  100. for (i = 0; i <= MAX_ISC; i++)
  101. if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
  102. active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
  103. return active_mask;
  104. }
  105. static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
  106. {
  107. unsigned long active_mask;
  108. active_mask = pending_irqs(vcpu);
  109. if (!active_mask)
  110. return 0;
  111. if (psw_extint_disabled(vcpu))
  112. active_mask &= ~IRQ_PEND_EXT_MASK;
  113. if (psw_ioint_disabled(vcpu))
  114. active_mask &= ~IRQ_PEND_IO_MASK;
  115. else
  116. active_mask = disable_iscs(vcpu, active_mask);
  117. if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
  118. __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
  119. if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
  120. __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
  121. if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
  122. __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
  123. if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
  124. __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
  125. if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
  126. __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
  127. if (psw_mchk_disabled(vcpu))
  128. active_mask &= ~IRQ_PEND_MCHK_MASK;
  129. if (!(vcpu->arch.sie_block->gcr[14] &
  130. vcpu->kvm->arch.float_int.mchk.cr14))
  131. __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
  132. /*
  133. * STOP irqs will never be actively delivered. They are triggered via
  134. * intercept requests and cleared when the stop intercept is performed.
  135. */
  136. __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
  137. return active_mask;
  138. }
  139. static void __set_cpu_idle(struct kvm_vcpu *vcpu)
  140. {
  141. atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
  142. set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
  143. }
  144. static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
  145. {
  146. atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
  147. clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
  148. }
  149. static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
  150. {
  151. atomic_andnot(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
  152. &vcpu->arch.sie_block->cpuflags);
  153. vcpu->arch.sie_block->lctl = 0x0000;
  154. vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
  155. if (guestdbg_enabled(vcpu)) {
  156. vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
  157. LCTL_CR10 | LCTL_CR11);
  158. vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
  159. }
  160. }
  161. static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
  162. {
  163. atomic_or(flag, &vcpu->arch.sie_block->cpuflags);
  164. }
  165. static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
  166. {
  167. if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
  168. return;
  169. else if (psw_ioint_disabled(vcpu))
  170. __set_cpuflag(vcpu, CPUSTAT_IO_INT);
  171. else
  172. vcpu->arch.sie_block->lctl |= LCTL_CR6;
  173. }
  174. static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
  175. {
  176. if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
  177. return;
  178. if (psw_extint_disabled(vcpu))
  179. __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
  180. else
  181. vcpu->arch.sie_block->lctl |= LCTL_CR0;
  182. }
  183. static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
  184. {
  185. if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
  186. return;
  187. if (psw_mchk_disabled(vcpu))
  188. vcpu->arch.sie_block->ictl |= ICTL_LPSW;
  189. else
  190. vcpu->arch.sie_block->lctl |= LCTL_CR14;
  191. }
  192. static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
  193. {
  194. if (kvm_s390_is_stop_irq_pending(vcpu))
  195. __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
  196. }
  197. /* Set interception request for non-deliverable interrupts */
  198. static void set_intercept_indicators(struct kvm_vcpu *vcpu)
  199. {
  200. set_intercept_indicators_io(vcpu);
  201. set_intercept_indicators_ext(vcpu);
  202. set_intercept_indicators_mchk(vcpu);
  203. set_intercept_indicators_stop(vcpu);
  204. }
  205. static u16 get_ilc(struct kvm_vcpu *vcpu)
  206. {
  207. switch (vcpu->arch.sie_block->icptcode) {
  208. case ICPT_INST:
  209. case ICPT_INSTPROGI:
  210. case ICPT_OPEREXC:
  211. case ICPT_PARTEXEC:
  212. case ICPT_IOINST:
  213. /* last instruction only stored for these icptcodes */
  214. return insn_length(vcpu->arch.sie_block->ipa >> 8);
  215. case ICPT_PROGI:
  216. return vcpu->arch.sie_block->pgmilc;
  217. default:
  218. return 0;
  219. }
  220. }
  221. static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
  222. {
  223. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  224. int rc;
  225. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
  226. 0, 0);
  227. rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
  228. (u16 *)__LC_EXT_INT_CODE);
  229. rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
  230. rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
  231. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  232. rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
  233. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  234. clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
  235. return rc ? -EFAULT : 0;
  236. }
  237. static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
  238. {
  239. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  240. int rc;
  241. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
  242. 0, 0);
  243. rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
  244. (u16 __user *)__LC_EXT_INT_CODE);
  245. rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
  246. rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
  247. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  248. rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
  249. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  250. clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
  251. return rc ? -EFAULT : 0;
  252. }
  253. static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
  254. {
  255. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  256. struct kvm_s390_ext_info ext;
  257. int rc;
  258. spin_lock(&li->lock);
  259. ext = li->irq.ext;
  260. clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
  261. li->irq.ext.ext_params2 = 0;
  262. spin_unlock(&li->lock);
  263. VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
  264. ext.ext_params2);
  265. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
  266. KVM_S390_INT_PFAULT_INIT,
  267. 0, ext.ext_params2);
  268. rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
  269. rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
  270. rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
  271. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  272. rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
  273. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  274. rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
  275. return rc ? -EFAULT : 0;
  276. }
  277. static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
  278. {
  279. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  280. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  281. struct kvm_s390_mchk_info mchk = {};
  282. unsigned long adtl_status_addr;
  283. int deliver = 0;
  284. int rc = 0;
  285. spin_lock(&fi->lock);
  286. spin_lock(&li->lock);
  287. if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
  288. test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
  289. /*
  290. * If there was an exigent machine check pending, then any
  291. * repressible machine checks that might have been pending
  292. * are indicated along with it, so always clear bits for
  293. * repressible and exigent interrupts
  294. */
  295. mchk = li->irq.mchk;
  296. clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
  297. clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
  298. memset(&li->irq.mchk, 0, sizeof(mchk));
  299. deliver = 1;
  300. }
  301. /*
  302. * We indicate floating repressible conditions along with
  303. * other pending conditions. Channel Report Pending and Channel
  304. * Subsystem damage are the only two and and are indicated by
  305. * bits in mcic and masked in cr14.
  306. */
  307. if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
  308. mchk.mcic |= fi->mchk.mcic;
  309. mchk.cr14 |= fi->mchk.cr14;
  310. memset(&fi->mchk, 0, sizeof(mchk));
  311. deliver = 1;
  312. }
  313. spin_unlock(&li->lock);
  314. spin_unlock(&fi->lock);
  315. if (deliver) {
  316. VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
  317. mchk.mcic);
  318. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
  319. KVM_S390_MCHK,
  320. mchk.cr14, mchk.mcic);
  321. rc = kvm_s390_vcpu_store_status(vcpu,
  322. KVM_S390_STORE_STATUS_PREFIXED);
  323. rc |= read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR,
  324. &adtl_status_addr,
  325. sizeof(unsigned long));
  326. rc |= kvm_s390_vcpu_store_adtl_status(vcpu,
  327. adtl_status_addr);
  328. rc |= put_guest_lc(vcpu, mchk.mcic,
  329. (u64 __user *) __LC_MCCK_CODE);
  330. rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
  331. (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
  332. rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
  333. &mchk.fixed_logout,
  334. sizeof(mchk.fixed_logout));
  335. rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
  336. &vcpu->arch.sie_block->gpsw,
  337. sizeof(psw_t));
  338. rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
  339. &vcpu->arch.sie_block->gpsw,
  340. sizeof(psw_t));
  341. }
  342. return rc ? -EFAULT : 0;
  343. }
  344. static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
  345. {
  346. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  347. int rc;
  348. VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
  349. vcpu->stat.deliver_restart_signal++;
  350. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
  351. rc = write_guest_lc(vcpu,
  352. offsetof(struct _lowcore, restart_old_psw),
  353. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  354. rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
  355. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  356. clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
  357. return rc ? -EFAULT : 0;
  358. }
  359. static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
  360. {
  361. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  362. struct kvm_s390_prefix_info prefix;
  363. spin_lock(&li->lock);
  364. prefix = li->irq.prefix;
  365. li->irq.prefix.address = 0;
  366. clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
  367. spin_unlock(&li->lock);
  368. vcpu->stat.deliver_prefix_signal++;
  369. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
  370. KVM_S390_SIGP_SET_PREFIX,
  371. prefix.address, 0);
  372. kvm_s390_set_prefix(vcpu, prefix.address);
  373. return 0;
  374. }
  375. static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
  376. {
  377. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  378. int rc;
  379. int cpu_addr;
  380. spin_lock(&li->lock);
  381. cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
  382. clear_bit(cpu_addr, li->sigp_emerg_pending);
  383. if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
  384. clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
  385. spin_unlock(&li->lock);
  386. VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
  387. vcpu->stat.deliver_emergency_signal++;
  388. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
  389. cpu_addr, 0);
  390. rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
  391. (u16 *)__LC_EXT_INT_CODE);
  392. rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
  393. rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
  394. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  395. rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
  396. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  397. return rc ? -EFAULT : 0;
  398. }
  399. static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
  400. {
  401. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  402. struct kvm_s390_extcall_info extcall;
  403. int rc;
  404. spin_lock(&li->lock);
  405. extcall = li->irq.extcall;
  406. li->irq.extcall.code = 0;
  407. clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
  408. spin_unlock(&li->lock);
  409. VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
  410. vcpu->stat.deliver_external_call++;
  411. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
  412. KVM_S390_INT_EXTERNAL_CALL,
  413. extcall.code, 0);
  414. rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
  415. (u16 *)__LC_EXT_INT_CODE);
  416. rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
  417. rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
  418. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  419. rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
  420. sizeof(psw_t));
  421. return rc ? -EFAULT : 0;
  422. }
  423. static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
  424. {
  425. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  426. struct kvm_s390_pgm_info pgm_info;
  427. int rc = 0, nullifying = false;
  428. u16 ilc = get_ilc(vcpu);
  429. spin_lock(&li->lock);
  430. pgm_info = li->irq.pgm;
  431. clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
  432. memset(&li->irq.pgm, 0, sizeof(pgm_info));
  433. spin_unlock(&li->lock);
  434. VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilc:%d",
  435. pgm_info.code, ilc);
  436. vcpu->stat.deliver_program_int++;
  437. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
  438. pgm_info.code, 0);
  439. switch (pgm_info.code & ~PGM_PER) {
  440. case PGM_AFX_TRANSLATION:
  441. case PGM_ASX_TRANSLATION:
  442. case PGM_EX_TRANSLATION:
  443. case PGM_LFX_TRANSLATION:
  444. case PGM_LSTE_SEQUENCE:
  445. case PGM_LSX_TRANSLATION:
  446. case PGM_LX_TRANSLATION:
  447. case PGM_PRIMARY_AUTHORITY:
  448. case PGM_SECONDARY_AUTHORITY:
  449. nullifying = true;
  450. /* fall through */
  451. case PGM_SPACE_SWITCH:
  452. rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
  453. (u64 *)__LC_TRANS_EXC_CODE);
  454. break;
  455. case PGM_ALEN_TRANSLATION:
  456. case PGM_ALE_SEQUENCE:
  457. case PGM_ASTE_INSTANCE:
  458. case PGM_ASTE_SEQUENCE:
  459. case PGM_ASTE_VALIDITY:
  460. case PGM_EXTENDED_AUTHORITY:
  461. rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
  462. (u8 *)__LC_EXC_ACCESS_ID);
  463. nullifying = true;
  464. break;
  465. case PGM_ASCE_TYPE:
  466. case PGM_PAGE_TRANSLATION:
  467. case PGM_REGION_FIRST_TRANS:
  468. case PGM_REGION_SECOND_TRANS:
  469. case PGM_REGION_THIRD_TRANS:
  470. case PGM_SEGMENT_TRANSLATION:
  471. rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
  472. (u64 *)__LC_TRANS_EXC_CODE);
  473. rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
  474. (u8 *)__LC_EXC_ACCESS_ID);
  475. rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
  476. (u8 *)__LC_OP_ACCESS_ID);
  477. nullifying = true;
  478. break;
  479. case PGM_MONITOR:
  480. rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
  481. (u16 *)__LC_MON_CLASS_NR);
  482. rc |= put_guest_lc(vcpu, pgm_info.mon_code,
  483. (u64 *)__LC_MON_CODE);
  484. break;
  485. case PGM_VECTOR_PROCESSING:
  486. case PGM_DATA:
  487. rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
  488. (u32 *)__LC_DATA_EXC_CODE);
  489. break;
  490. case PGM_PROTECTION:
  491. rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
  492. (u64 *)__LC_TRANS_EXC_CODE);
  493. rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
  494. (u8 *)__LC_EXC_ACCESS_ID);
  495. break;
  496. case PGM_STACK_FULL:
  497. case PGM_STACK_EMPTY:
  498. case PGM_STACK_SPECIFICATION:
  499. case PGM_STACK_TYPE:
  500. case PGM_STACK_OPERATION:
  501. case PGM_TRACE_TABEL:
  502. case PGM_CRYPTO_OPERATION:
  503. nullifying = true;
  504. break;
  505. }
  506. if (pgm_info.code & PGM_PER) {
  507. rc |= put_guest_lc(vcpu, pgm_info.per_code,
  508. (u8 *) __LC_PER_CODE);
  509. rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
  510. (u8 *)__LC_PER_ATMID);
  511. rc |= put_guest_lc(vcpu, pgm_info.per_address,
  512. (u64 *) __LC_PER_ADDRESS);
  513. rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
  514. (u8 *) __LC_PER_ACCESS_ID);
  515. }
  516. if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST)
  517. kvm_s390_rewind_psw(vcpu, ilc);
  518. rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
  519. rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
  520. (u64 *) __LC_LAST_BREAK);
  521. rc |= put_guest_lc(vcpu, pgm_info.code,
  522. (u16 *)__LC_PGM_INT_CODE);
  523. rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
  524. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  525. rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
  526. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  527. return rc ? -EFAULT : 0;
  528. }
  529. static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
  530. {
  531. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  532. struct kvm_s390_ext_info ext;
  533. int rc = 0;
  534. spin_lock(&fi->lock);
  535. if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
  536. spin_unlock(&fi->lock);
  537. return 0;
  538. }
  539. ext = fi->srv_signal;
  540. memset(&fi->srv_signal, 0, sizeof(ext));
  541. clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
  542. spin_unlock(&fi->lock);
  543. VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
  544. ext.ext_params);
  545. vcpu->stat.deliver_service_signal++;
  546. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
  547. ext.ext_params, 0);
  548. rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
  549. rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
  550. rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
  551. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  552. rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
  553. &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
  554. rc |= put_guest_lc(vcpu, ext.ext_params,
  555. (u32 *)__LC_EXT_PARAMS);
  556. return rc ? -EFAULT : 0;
  557. }
  558. static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
  559. {
  560. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  561. struct kvm_s390_interrupt_info *inti;
  562. int rc = 0;
  563. spin_lock(&fi->lock);
  564. inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
  565. struct kvm_s390_interrupt_info,
  566. list);
  567. if (inti) {
  568. list_del(&inti->list);
  569. fi->counters[FIRQ_CNTR_PFAULT] -= 1;
  570. }
  571. if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
  572. clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
  573. spin_unlock(&fi->lock);
  574. if (inti) {
  575. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
  576. KVM_S390_INT_PFAULT_DONE, 0,
  577. inti->ext.ext_params2);
  578. VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
  579. inti->ext.ext_params2);
  580. rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
  581. (u16 *)__LC_EXT_INT_CODE);
  582. rc |= put_guest_lc(vcpu, PFAULT_DONE,
  583. (u16 *)__LC_EXT_CPU_ADDR);
  584. rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
  585. &vcpu->arch.sie_block->gpsw,
  586. sizeof(psw_t));
  587. rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
  588. &vcpu->arch.sie_block->gpsw,
  589. sizeof(psw_t));
  590. rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
  591. (u64 *)__LC_EXT_PARAMS2);
  592. kfree(inti);
  593. }
  594. return rc ? -EFAULT : 0;
  595. }
  596. static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
  597. {
  598. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  599. struct kvm_s390_interrupt_info *inti;
  600. int rc = 0;
  601. spin_lock(&fi->lock);
  602. inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
  603. struct kvm_s390_interrupt_info,
  604. list);
  605. if (inti) {
  606. VCPU_EVENT(vcpu, 4,
  607. "deliver: virtio parm: 0x%x,parm64: 0x%llx",
  608. inti->ext.ext_params, inti->ext.ext_params2);
  609. vcpu->stat.deliver_virtio_interrupt++;
  610. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
  611. inti->type,
  612. inti->ext.ext_params,
  613. inti->ext.ext_params2);
  614. list_del(&inti->list);
  615. fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
  616. }
  617. if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
  618. clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
  619. spin_unlock(&fi->lock);
  620. if (inti) {
  621. rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
  622. (u16 *)__LC_EXT_INT_CODE);
  623. rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
  624. (u16 *)__LC_EXT_CPU_ADDR);
  625. rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
  626. &vcpu->arch.sie_block->gpsw,
  627. sizeof(psw_t));
  628. rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
  629. &vcpu->arch.sie_block->gpsw,
  630. sizeof(psw_t));
  631. rc |= put_guest_lc(vcpu, inti->ext.ext_params,
  632. (u32 *)__LC_EXT_PARAMS);
  633. rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
  634. (u64 *)__LC_EXT_PARAMS2);
  635. kfree(inti);
  636. }
  637. return rc ? -EFAULT : 0;
  638. }
  639. static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
  640. unsigned long irq_type)
  641. {
  642. struct list_head *isc_list;
  643. struct kvm_s390_float_interrupt *fi;
  644. struct kvm_s390_interrupt_info *inti = NULL;
  645. int rc = 0;
  646. fi = &vcpu->kvm->arch.float_int;
  647. spin_lock(&fi->lock);
  648. isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
  649. inti = list_first_entry_or_null(isc_list,
  650. struct kvm_s390_interrupt_info,
  651. list);
  652. if (inti) {
  653. VCPU_EVENT(vcpu, 4, "deliver: I/O 0x%llx", inti->type);
  654. vcpu->stat.deliver_io_int++;
  655. trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
  656. inti->type,
  657. ((__u32)inti->io.subchannel_id << 16) |
  658. inti->io.subchannel_nr,
  659. ((__u64)inti->io.io_int_parm << 32) |
  660. inti->io.io_int_word);
  661. list_del(&inti->list);
  662. fi->counters[FIRQ_CNTR_IO] -= 1;
  663. }
  664. if (list_empty(isc_list))
  665. clear_bit(irq_type, &fi->pending_irqs);
  666. spin_unlock(&fi->lock);
  667. if (inti) {
  668. rc = put_guest_lc(vcpu, inti->io.subchannel_id,
  669. (u16 *)__LC_SUBCHANNEL_ID);
  670. rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
  671. (u16 *)__LC_SUBCHANNEL_NR);
  672. rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
  673. (u32 *)__LC_IO_INT_PARM);
  674. rc |= put_guest_lc(vcpu, inti->io.io_int_word,
  675. (u32 *)__LC_IO_INT_WORD);
  676. rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
  677. &vcpu->arch.sie_block->gpsw,
  678. sizeof(psw_t));
  679. rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
  680. &vcpu->arch.sie_block->gpsw,
  681. sizeof(psw_t));
  682. kfree(inti);
  683. }
  684. return rc ? -EFAULT : 0;
  685. }
  686. typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
  687. static const deliver_irq_t deliver_irq_funcs[] = {
  688. [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
  689. [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
  690. [IRQ_PEND_PROG] = __deliver_prog,
  691. [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
  692. [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
  693. [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
  694. [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
  695. [IRQ_PEND_RESTART] = __deliver_restart,
  696. [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
  697. [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
  698. [IRQ_PEND_EXT_SERVICE] = __deliver_service,
  699. [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
  700. [IRQ_PEND_VIRTIO] = __deliver_virtio,
  701. };
  702. /* Check whether an external call is pending (deliverable or not) */
  703. int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
  704. {
  705. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  706. uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
  707. if (!sclp.has_sigpif)
  708. return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
  709. return (sigp_ctrl & SIGP_CTRL_C) &&
  710. (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND);
  711. }
  712. int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
  713. {
  714. if (deliverable_irqs(vcpu))
  715. return 1;
  716. if (kvm_cpu_has_pending_timer(vcpu))
  717. return 1;
  718. /* external call pending and deliverable */
  719. if (kvm_s390_ext_call_pending(vcpu) &&
  720. !psw_extint_disabled(vcpu) &&
  721. (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
  722. return 1;
  723. if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
  724. return 1;
  725. return 0;
  726. }
  727. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  728. {
  729. return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
  730. }
  731. int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
  732. {
  733. u64 now, sltime;
  734. vcpu->stat.exit_wait_state++;
  735. /* fast path */
  736. if (kvm_arch_vcpu_runnable(vcpu))
  737. return 0;
  738. if (psw_interrupts_disabled(vcpu)) {
  739. VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
  740. return -EOPNOTSUPP; /* disabled wait */
  741. }
  742. if (!ckc_interrupts_enabled(vcpu)) {
  743. VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
  744. __set_cpu_idle(vcpu);
  745. goto no_timer;
  746. }
  747. now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
  748. sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
  749. /* underflow */
  750. if (vcpu->arch.sie_block->ckc < now)
  751. return 0;
  752. __set_cpu_idle(vcpu);
  753. hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
  754. VCPU_EVENT(vcpu, 4, "enabled wait via clock comparator: %llu ns", sltime);
  755. no_timer:
  756. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  757. kvm_vcpu_block(vcpu);
  758. __unset_cpu_idle(vcpu);
  759. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  760. hrtimer_cancel(&vcpu->arch.ckc_timer);
  761. return 0;
  762. }
  763. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
  764. {
  765. if (waitqueue_active(&vcpu->wq)) {
  766. /*
  767. * The vcpu gave up the cpu voluntarily, mark it as a good
  768. * yield-candidate.
  769. */
  770. vcpu->preempted = true;
  771. wake_up_interruptible(&vcpu->wq);
  772. vcpu->stat.halt_wakeup++;
  773. }
  774. }
  775. enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
  776. {
  777. struct kvm_vcpu *vcpu;
  778. u64 now, sltime;
  779. vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
  780. now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
  781. sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
  782. /*
  783. * If the monotonic clock runs faster than the tod clock we might be
  784. * woken up too early and have to go back to sleep to avoid deadlocks.
  785. */
  786. if (vcpu->arch.sie_block->ckc > now &&
  787. hrtimer_forward_now(timer, ns_to_ktime(sltime)))
  788. return HRTIMER_RESTART;
  789. kvm_s390_vcpu_wakeup(vcpu);
  790. return HRTIMER_NORESTART;
  791. }
  792. void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
  793. {
  794. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  795. spin_lock(&li->lock);
  796. li->pending_irqs = 0;
  797. bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
  798. memset(&li->irq, 0, sizeof(li->irq));
  799. spin_unlock(&li->lock);
  800. /* clear pending external calls set by sigp interpretation facility */
  801. atomic_andnot(CPUSTAT_ECALL_PEND, li->cpuflags);
  802. vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0;
  803. }
  804. int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
  805. {
  806. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  807. deliver_irq_t func;
  808. int rc = 0;
  809. unsigned long irq_type;
  810. unsigned long irqs;
  811. __reset_intercept_indicators(vcpu);
  812. /* pending ckc conditions might have been invalidated */
  813. clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
  814. if (ckc_irq_pending(vcpu))
  815. set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
  816. /* pending cpu timer conditions might have been invalidated */
  817. clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
  818. if (cpu_timer_irq_pending(vcpu))
  819. set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
  820. while ((irqs = deliverable_irqs(vcpu)) && !rc) {
  821. /* bits are in the order of interrupt priority */
  822. irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
  823. if (is_ioirq(irq_type)) {
  824. rc = __deliver_io(vcpu, irq_type);
  825. } else {
  826. func = deliver_irq_funcs[irq_type];
  827. if (!func) {
  828. WARN_ON_ONCE(func == NULL);
  829. clear_bit(irq_type, &li->pending_irqs);
  830. continue;
  831. }
  832. rc = func(vcpu);
  833. }
  834. }
  835. set_intercept_indicators(vcpu);
  836. return rc;
  837. }
  838. static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
  839. {
  840. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  841. VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
  842. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
  843. irq->u.pgm.code, 0);
  844. if (irq->u.pgm.code == PGM_PER) {
  845. li->irq.pgm.code |= PGM_PER;
  846. /* only modify PER related information */
  847. li->irq.pgm.per_address = irq->u.pgm.per_address;
  848. li->irq.pgm.per_code = irq->u.pgm.per_code;
  849. li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
  850. li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
  851. } else if (!(irq->u.pgm.code & PGM_PER)) {
  852. li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
  853. irq->u.pgm.code;
  854. /* only modify non-PER information */
  855. li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
  856. li->irq.pgm.mon_code = irq->u.pgm.mon_code;
  857. li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
  858. li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
  859. li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
  860. li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
  861. } else {
  862. li->irq.pgm = irq->u.pgm;
  863. }
  864. set_bit(IRQ_PEND_PROG, &li->pending_irqs);
  865. return 0;
  866. }
  867. static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
  868. {
  869. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  870. VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
  871. irq->u.ext.ext_params2);
  872. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
  873. irq->u.ext.ext_params,
  874. irq->u.ext.ext_params2);
  875. li->irq.ext = irq->u.ext;
  876. set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
  877. atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
  878. return 0;
  879. }
  880. static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id)
  881. {
  882. unsigned char new_val, old_val;
  883. uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
  884. new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK);
  885. old_val = *sigp_ctrl & ~SIGP_CTRL_C;
  886. if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) {
  887. /* another external call is pending */
  888. return -EBUSY;
  889. }
  890. atomic_or(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
  891. return 0;
  892. }
  893. static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
  894. {
  895. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  896. struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
  897. uint16_t src_id = irq->u.extcall.code;
  898. VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
  899. src_id);
  900. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
  901. src_id, 0);
  902. /* sending vcpu invalid */
  903. if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
  904. return -EINVAL;
  905. if (sclp.has_sigpif)
  906. return __inject_extcall_sigpif(vcpu, src_id);
  907. if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
  908. return -EBUSY;
  909. *extcall = irq->u.extcall;
  910. atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
  911. return 0;
  912. }
  913. static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
  914. {
  915. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  916. struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
  917. VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
  918. irq->u.prefix.address);
  919. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
  920. irq->u.prefix.address, 0);
  921. if (!is_vcpu_stopped(vcpu))
  922. return -EBUSY;
  923. *prefix = irq->u.prefix;
  924. set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
  925. return 0;
  926. }
  927. #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
  928. static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
  929. {
  930. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  931. struct kvm_s390_stop_info *stop = &li->irq.stop;
  932. int rc = 0;
  933. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
  934. if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
  935. return -EINVAL;
  936. if (is_vcpu_stopped(vcpu)) {
  937. if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
  938. rc = kvm_s390_store_status_unloaded(vcpu,
  939. KVM_S390_STORE_STATUS_NOADDR);
  940. return rc;
  941. }
  942. if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
  943. return -EBUSY;
  944. stop->flags = irq->u.stop.flags;
  945. __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
  946. return 0;
  947. }
  948. static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
  949. struct kvm_s390_irq *irq)
  950. {
  951. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  952. VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
  953. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
  954. set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
  955. return 0;
  956. }
  957. static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
  958. struct kvm_s390_irq *irq)
  959. {
  960. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  961. VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
  962. irq->u.emerg.code);
  963. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
  964. irq->u.emerg.code, 0);
  965. /* sending vcpu invalid */
  966. if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
  967. return -EINVAL;
  968. set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
  969. set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
  970. atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
  971. return 0;
  972. }
  973. static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
  974. {
  975. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  976. struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
  977. VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
  978. irq->u.mchk.mcic);
  979. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
  980. irq->u.mchk.mcic);
  981. /*
  982. * Because repressible machine checks can be indicated along with
  983. * exigent machine checks (PoP, Chapter 11, Interruption action)
  984. * we need to combine cr14, mcic and external damage code.
  985. * Failing storage address and the logout area should not be or'ed
  986. * together, we just indicate the last occurrence of the corresponding
  987. * machine check
  988. */
  989. mchk->cr14 |= irq->u.mchk.cr14;
  990. mchk->mcic |= irq->u.mchk.mcic;
  991. mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
  992. mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
  993. memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
  994. sizeof(mchk->fixed_logout));
  995. if (mchk->mcic & MCHK_EX_MASK)
  996. set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
  997. else if (mchk->mcic & MCHK_REP_MASK)
  998. set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
  999. return 0;
  1000. }
  1001. static int __inject_ckc(struct kvm_vcpu *vcpu)
  1002. {
  1003. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  1004. VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
  1005. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
  1006. 0, 0);
  1007. set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
  1008. atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
  1009. return 0;
  1010. }
  1011. static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
  1012. {
  1013. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  1014. VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
  1015. trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
  1016. 0, 0);
  1017. set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
  1018. atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
  1019. return 0;
  1020. }
  1021. static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
  1022. int isc, u32 schid)
  1023. {
  1024. struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
  1025. struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
  1026. struct kvm_s390_interrupt_info *iter;
  1027. u16 id = (schid & 0xffff0000U) >> 16;
  1028. u16 nr = schid & 0x0000ffffU;
  1029. spin_lock(&fi->lock);
  1030. list_for_each_entry(iter, isc_list, list) {
  1031. if (schid && (id != iter->io.subchannel_id ||
  1032. nr != iter->io.subchannel_nr))
  1033. continue;
  1034. /* found an appropriate entry */
  1035. list_del_init(&iter->list);
  1036. fi->counters[FIRQ_CNTR_IO] -= 1;
  1037. if (list_empty(isc_list))
  1038. clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
  1039. spin_unlock(&fi->lock);
  1040. return iter;
  1041. }
  1042. spin_unlock(&fi->lock);
  1043. return NULL;
  1044. }
  1045. /*
  1046. * Dequeue and return an I/O interrupt matching any of the interruption
  1047. * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
  1048. */
  1049. struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
  1050. u64 isc_mask, u32 schid)
  1051. {
  1052. struct kvm_s390_interrupt_info *inti = NULL;
  1053. int isc;
  1054. for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
  1055. if (isc_mask & isc_to_isc_bits(isc))
  1056. inti = get_io_int(kvm, isc, schid);
  1057. }
  1058. return inti;
  1059. }
  1060. #define SCCB_MASK 0xFFFFFFF8
  1061. #define SCCB_EVENT_PENDING 0x3
  1062. static int __inject_service(struct kvm *kvm,
  1063. struct kvm_s390_interrupt_info *inti)
  1064. {
  1065. struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
  1066. spin_lock(&fi->lock);
  1067. fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
  1068. /*
  1069. * Early versions of the QEMU s390 bios will inject several
  1070. * service interrupts after another without handling a
  1071. * condition code indicating busy.
  1072. * We will silently ignore those superfluous sccb values.
  1073. * A future version of QEMU will take care of serialization
  1074. * of servc requests
  1075. */
  1076. if (fi->srv_signal.ext_params & SCCB_MASK)
  1077. goto out;
  1078. fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
  1079. set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
  1080. out:
  1081. spin_unlock(&fi->lock);
  1082. kfree(inti);
  1083. return 0;
  1084. }
  1085. static int __inject_virtio(struct kvm *kvm,
  1086. struct kvm_s390_interrupt_info *inti)
  1087. {
  1088. struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
  1089. spin_lock(&fi->lock);
  1090. if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
  1091. spin_unlock(&fi->lock);
  1092. return -EBUSY;
  1093. }
  1094. fi->counters[FIRQ_CNTR_VIRTIO] += 1;
  1095. list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
  1096. set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
  1097. spin_unlock(&fi->lock);
  1098. return 0;
  1099. }
  1100. static int __inject_pfault_done(struct kvm *kvm,
  1101. struct kvm_s390_interrupt_info *inti)
  1102. {
  1103. struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
  1104. spin_lock(&fi->lock);
  1105. if (fi->counters[FIRQ_CNTR_PFAULT] >=
  1106. (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
  1107. spin_unlock(&fi->lock);
  1108. return -EBUSY;
  1109. }
  1110. fi->counters[FIRQ_CNTR_PFAULT] += 1;
  1111. list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
  1112. set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
  1113. spin_unlock(&fi->lock);
  1114. return 0;
  1115. }
  1116. #define CR_PENDING_SUBCLASS 28
  1117. static int __inject_float_mchk(struct kvm *kvm,
  1118. struct kvm_s390_interrupt_info *inti)
  1119. {
  1120. struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
  1121. spin_lock(&fi->lock);
  1122. fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
  1123. fi->mchk.mcic |= inti->mchk.mcic;
  1124. set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
  1125. spin_unlock(&fi->lock);
  1126. kfree(inti);
  1127. return 0;
  1128. }
  1129. static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
  1130. {
  1131. struct kvm_s390_float_interrupt *fi;
  1132. struct list_head *list;
  1133. int isc;
  1134. fi = &kvm->arch.float_int;
  1135. spin_lock(&fi->lock);
  1136. if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
  1137. spin_unlock(&fi->lock);
  1138. return -EBUSY;
  1139. }
  1140. fi->counters[FIRQ_CNTR_IO] += 1;
  1141. isc = int_word_to_isc(inti->io.io_int_word);
  1142. list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
  1143. list_add_tail(&inti->list, list);
  1144. set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
  1145. spin_unlock(&fi->lock);
  1146. return 0;
  1147. }
  1148. /*
  1149. * Find a destination VCPU for a floating irq and kick it.
  1150. */
  1151. static void __floating_irq_kick(struct kvm *kvm, u64 type)
  1152. {
  1153. struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
  1154. struct kvm_s390_local_interrupt *li;
  1155. struct kvm_vcpu *dst_vcpu;
  1156. int sigcpu, online_vcpus, nr_tries = 0;
  1157. online_vcpus = atomic_read(&kvm->online_vcpus);
  1158. if (!online_vcpus)
  1159. return;
  1160. /* find idle VCPUs first, then round robin */
  1161. sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
  1162. if (sigcpu == online_vcpus) {
  1163. do {
  1164. sigcpu = fi->next_rr_cpu;
  1165. fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
  1166. /* avoid endless loops if all vcpus are stopped */
  1167. if (nr_tries++ >= online_vcpus)
  1168. return;
  1169. } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
  1170. }
  1171. dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
  1172. /* make the VCPU drop out of the SIE, or wake it up if sleeping */
  1173. li = &dst_vcpu->arch.local_int;
  1174. spin_lock(&li->lock);
  1175. switch (type) {
  1176. case KVM_S390_MCHK:
  1177. atomic_or(CPUSTAT_STOP_INT, li->cpuflags);
  1178. break;
  1179. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  1180. atomic_or(CPUSTAT_IO_INT, li->cpuflags);
  1181. break;
  1182. default:
  1183. atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
  1184. break;
  1185. }
  1186. spin_unlock(&li->lock);
  1187. kvm_s390_vcpu_wakeup(dst_vcpu);
  1188. }
  1189. static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
  1190. {
  1191. u64 type = READ_ONCE(inti->type);
  1192. int rc;
  1193. switch (type) {
  1194. case KVM_S390_MCHK:
  1195. rc = __inject_float_mchk(kvm, inti);
  1196. break;
  1197. case KVM_S390_INT_VIRTIO:
  1198. rc = __inject_virtio(kvm, inti);
  1199. break;
  1200. case KVM_S390_INT_SERVICE:
  1201. rc = __inject_service(kvm, inti);
  1202. break;
  1203. case KVM_S390_INT_PFAULT_DONE:
  1204. rc = __inject_pfault_done(kvm, inti);
  1205. break;
  1206. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  1207. rc = __inject_io(kvm, inti);
  1208. break;
  1209. default:
  1210. rc = -EINVAL;
  1211. }
  1212. if (rc)
  1213. return rc;
  1214. __floating_irq_kick(kvm, type);
  1215. return 0;
  1216. }
  1217. int kvm_s390_inject_vm(struct kvm *kvm,
  1218. struct kvm_s390_interrupt *s390int)
  1219. {
  1220. struct kvm_s390_interrupt_info *inti;
  1221. int rc;
  1222. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  1223. if (!inti)
  1224. return -ENOMEM;
  1225. inti->type = s390int->type;
  1226. switch (inti->type) {
  1227. case KVM_S390_INT_VIRTIO:
  1228. VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
  1229. s390int->parm, s390int->parm64);
  1230. inti->ext.ext_params = s390int->parm;
  1231. inti->ext.ext_params2 = s390int->parm64;
  1232. break;
  1233. case KVM_S390_INT_SERVICE:
  1234. VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
  1235. inti->ext.ext_params = s390int->parm;
  1236. break;
  1237. case KVM_S390_INT_PFAULT_DONE:
  1238. inti->ext.ext_params2 = s390int->parm64;
  1239. break;
  1240. case KVM_S390_MCHK:
  1241. VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
  1242. s390int->parm64);
  1243. inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
  1244. inti->mchk.mcic = s390int->parm64;
  1245. break;
  1246. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  1247. if (inti->type & KVM_S390_INT_IO_AI_MASK)
  1248. VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
  1249. else
  1250. VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
  1251. s390int->type & IOINT_CSSID_MASK,
  1252. s390int->type & IOINT_SSID_MASK,
  1253. s390int->type & IOINT_SCHID_MASK);
  1254. inti->io.subchannel_id = s390int->parm >> 16;
  1255. inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
  1256. inti->io.io_int_parm = s390int->parm64 >> 32;
  1257. inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
  1258. break;
  1259. default:
  1260. kfree(inti);
  1261. return -EINVAL;
  1262. }
  1263. trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
  1264. 2);
  1265. rc = __inject_vm(kvm, inti);
  1266. if (rc)
  1267. kfree(inti);
  1268. return rc;
  1269. }
  1270. int kvm_s390_reinject_io_int(struct kvm *kvm,
  1271. struct kvm_s390_interrupt_info *inti)
  1272. {
  1273. return __inject_vm(kvm, inti);
  1274. }
  1275. int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
  1276. struct kvm_s390_irq *irq)
  1277. {
  1278. irq->type = s390int->type;
  1279. switch (irq->type) {
  1280. case KVM_S390_PROGRAM_INT:
  1281. if (s390int->parm & 0xffff0000)
  1282. return -EINVAL;
  1283. irq->u.pgm.code = s390int->parm;
  1284. break;
  1285. case KVM_S390_SIGP_SET_PREFIX:
  1286. irq->u.prefix.address = s390int->parm;
  1287. break;
  1288. case KVM_S390_SIGP_STOP:
  1289. irq->u.stop.flags = s390int->parm;
  1290. break;
  1291. case KVM_S390_INT_EXTERNAL_CALL:
  1292. if (s390int->parm & 0xffff0000)
  1293. return -EINVAL;
  1294. irq->u.extcall.code = s390int->parm;
  1295. break;
  1296. case KVM_S390_INT_EMERGENCY:
  1297. if (s390int->parm & 0xffff0000)
  1298. return -EINVAL;
  1299. irq->u.emerg.code = s390int->parm;
  1300. break;
  1301. case KVM_S390_MCHK:
  1302. irq->u.mchk.mcic = s390int->parm64;
  1303. break;
  1304. }
  1305. return 0;
  1306. }
  1307. int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
  1308. {
  1309. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  1310. return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
  1311. }
  1312. void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
  1313. {
  1314. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  1315. spin_lock(&li->lock);
  1316. li->irq.stop.flags = 0;
  1317. clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
  1318. spin_unlock(&li->lock);
  1319. }
  1320. static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
  1321. {
  1322. int rc;
  1323. switch (irq->type) {
  1324. case KVM_S390_PROGRAM_INT:
  1325. rc = __inject_prog(vcpu, irq);
  1326. break;
  1327. case KVM_S390_SIGP_SET_PREFIX:
  1328. rc = __inject_set_prefix(vcpu, irq);
  1329. break;
  1330. case KVM_S390_SIGP_STOP:
  1331. rc = __inject_sigp_stop(vcpu, irq);
  1332. break;
  1333. case KVM_S390_RESTART:
  1334. rc = __inject_sigp_restart(vcpu, irq);
  1335. break;
  1336. case KVM_S390_INT_CLOCK_COMP:
  1337. rc = __inject_ckc(vcpu);
  1338. break;
  1339. case KVM_S390_INT_CPU_TIMER:
  1340. rc = __inject_cpu_timer(vcpu);
  1341. break;
  1342. case KVM_S390_INT_EXTERNAL_CALL:
  1343. rc = __inject_extcall(vcpu, irq);
  1344. break;
  1345. case KVM_S390_INT_EMERGENCY:
  1346. rc = __inject_sigp_emergency(vcpu, irq);
  1347. break;
  1348. case KVM_S390_MCHK:
  1349. rc = __inject_mchk(vcpu, irq);
  1350. break;
  1351. case KVM_S390_INT_PFAULT_INIT:
  1352. rc = __inject_pfault_init(vcpu, irq);
  1353. break;
  1354. case KVM_S390_INT_VIRTIO:
  1355. case KVM_S390_INT_SERVICE:
  1356. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  1357. default:
  1358. rc = -EINVAL;
  1359. }
  1360. return rc;
  1361. }
  1362. int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
  1363. {
  1364. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  1365. int rc;
  1366. spin_lock(&li->lock);
  1367. rc = do_inject_vcpu(vcpu, irq);
  1368. spin_unlock(&li->lock);
  1369. if (!rc)
  1370. kvm_s390_vcpu_wakeup(vcpu);
  1371. return rc;
  1372. }
  1373. static inline void clear_irq_list(struct list_head *_list)
  1374. {
  1375. struct kvm_s390_interrupt_info *inti, *n;
  1376. list_for_each_entry_safe(inti, n, _list, list) {
  1377. list_del(&inti->list);
  1378. kfree(inti);
  1379. }
  1380. }
  1381. static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
  1382. struct kvm_s390_irq *irq)
  1383. {
  1384. irq->type = inti->type;
  1385. switch (inti->type) {
  1386. case KVM_S390_INT_PFAULT_INIT:
  1387. case KVM_S390_INT_PFAULT_DONE:
  1388. case KVM_S390_INT_VIRTIO:
  1389. irq->u.ext = inti->ext;
  1390. break;
  1391. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  1392. irq->u.io = inti->io;
  1393. break;
  1394. }
  1395. }
  1396. void kvm_s390_clear_float_irqs(struct kvm *kvm)
  1397. {
  1398. struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
  1399. int i;
  1400. spin_lock(&fi->lock);
  1401. fi->pending_irqs = 0;
  1402. memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
  1403. memset(&fi->mchk, 0, sizeof(fi->mchk));
  1404. for (i = 0; i < FIRQ_LIST_COUNT; i++)
  1405. clear_irq_list(&fi->lists[i]);
  1406. for (i = 0; i < FIRQ_MAX_COUNT; i++)
  1407. fi->counters[i] = 0;
  1408. spin_unlock(&fi->lock);
  1409. };
  1410. static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
  1411. {
  1412. struct kvm_s390_interrupt_info *inti;
  1413. struct kvm_s390_float_interrupt *fi;
  1414. struct kvm_s390_irq *buf;
  1415. struct kvm_s390_irq *irq;
  1416. int max_irqs;
  1417. int ret = 0;
  1418. int n = 0;
  1419. int i;
  1420. if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
  1421. return -EINVAL;
  1422. /*
  1423. * We are already using -ENOMEM to signal
  1424. * userspace it may retry with a bigger buffer,
  1425. * so we need to use something else for this case
  1426. */
  1427. buf = vzalloc(len);
  1428. if (!buf)
  1429. return -ENOBUFS;
  1430. max_irqs = len / sizeof(struct kvm_s390_irq);
  1431. fi = &kvm->arch.float_int;
  1432. spin_lock(&fi->lock);
  1433. for (i = 0; i < FIRQ_LIST_COUNT; i++) {
  1434. list_for_each_entry(inti, &fi->lists[i], list) {
  1435. if (n == max_irqs) {
  1436. /* signal userspace to try again */
  1437. ret = -ENOMEM;
  1438. goto out;
  1439. }
  1440. inti_to_irq(inti, &buf[n]);
  1441. n++;
  1442. }
  1443. }
  1444. if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
  1445. if (n == max_irqs) {
  1446. /* signal userspace to try again */
  1447. ret = -ENOMEM;
  1448. goto out;
  1449. }
  1450. irq = (struct kvm_s390_irq *) &buf[n];
  1451. irq->type = KVM_S390_INT_SERVICE;
  1452. irq->u.ext = fi->srv_signal;
  1453. n++;
  1454. }
  1455. if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
  1456. if (n == max_irqs) {
  1457. /* signal userspace to try again */
  1458. ret = -ENOMEM;
  1459. goto out;
  1460. }
  1461. irq = (struct kvm_s390_irq *) &buf[n];
  1462. irq->type = KVM_S390_MCHK;
  1463. irq->u.mchk = fi->mchk;
  1464. n++;
  1465. }
  1466. out:
  1467. spin_unlock(&fi->lock);
  1468. if (!ret && n > 0) {
  1469. if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
  1470. ret = -EFAULT;
  1471. }
  1472. vfree(buf);
  1473. return ret < 0 ? ret : n;
  1474. }
  1475. static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1476. {
  1477. int r;
  1478. switch (attr->group) {
  1479. case KVM_DEV_FLIC_GET_ALL_IRQS:
  1480. r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
  1481. attr->attr);
  1482. break;
  1483. default:
  1484. r = -EINVAL;
  1485. }
  1486. return r;
  1487. }
  1488. static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
  1489. u64 addr)
  1490. {
  1491. struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
  1492. void *target = NULL;
  1493. void __user *source;
  1494. u64 size;
  1495. if (get_user(inti->type, (u64 __user *)addr))
  1496. return -EFAULT;
  1497. switch (inti->type) {
  1498. case KVM_S390_INT_PFAULT_INIT:
  1499. case KVM_S390_INT_PFAULT_DONE:
  1500. case KVM_S390_INT_VIRTIO:
  1501. case KVM_S390_INT_SERVICE:
  1502. target = (void *) &inti->ext;
  1503. source = &uptr->u.ext;
  1504. size = sizeof(inti->ext);
  1505. break;
  1506. case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
  1507. target = (void *) &inti->io;
  1508. source = &uptr->u.io;
  1509. size = sizeof(inti->io);
  1510. break;
  1511. case KVM_S390_MCHK:
  1512. target = (void *) &inti->mchk;
  1513. source = &uptr->u.mchk;
  1514. size = sizeof(inti->mchk);
  1515. break;
  1516. default:
  1517. return -EINVAL;
  1518. }
  1519. if (copy_from_user(target, source, size))
  1520. return -EFAULT;
  1521. return 0;
  1522. }
  1523. static int enqueue_floating_irq(struct kvm_device *dev,
  1524. struct kvm_device_attr *attr)
  1525. {
  1526. struct kvm_s390_interrupt_info *inti = NULL;
  1527. int r = 0;
  1528. int len = attr->attr;
  1529. if (len % sizeof(struct kvm_s390_irq) != 0)
  1530. return -EINVAL;
  1531. else if (len > KVM_S390_FLIC_MAX_BUFFER)
  1532. return -EINVAL;
  1533. while (len >= sizeof(struct kvm_s390_irq)) {
  1534. inti = kzalloc(sizeof(*inti), GFP_KERNEL);
  1535. if (!inti)
  1536. return -ENOMEM;
  1537. r = copy_irq_from_user(inti, attr->addr);
  1538. if (r) {
  1539. kfree(inti);
  1540. return r;
  1541. }
  1542. r = __inject_vm(dev->kvm, inti);
  1543. if (r) {
  1544. kfree(inti);
  1545. return r;
  1546. }
  1547. len -= sizeof(struct kvm_s390_irq);
  1548. attr->addr += sizeof(struct kvm_s390_irq);
  1549. }
  1550. return r;
  1551. }
  1552. static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
  1553. {
  1554. if (id >= MAX_S390_IO_ADAPTERS)
  1555. return NULL;
  1556. return kvm->arch.adapters[id];
  1557. }
  1558. static int register_io_adapter(struct kvm_device *dev,
  1559. struct kvm_device_attr *attr)
  1560. {
  1561. struct s390_io_adapter *adapter;
  1562. struct kvm_s390_io_adapter adapter_info;
  1563. if (copy_from_user(&adapter_info,
  1564. (void __user *)attr->addr, sizeof(adapter_info)))
  1565. return -EFAULT;
  1566. if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
  1567. (dev->kvm->arch.adapters[adapter_info.id] != NULL))
  1568. return -EINVAL;
  1569. adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
  1570. if (!adapter)
  1571. return -ENOMEM;
  1572. INIT_LIST_HEAD(&adapter->maps);
  1573. init_rwsem(&adapter->maps_lock);
  1574. atomic_set(&adapter->nr_maps, 0);
  1575. adapter->id = adapter_info.id;
  1576. adapter->isc = adapter_info.isc;
  1577. adapter->maskable = adapter_info.maskable;
  1578. adapter->masked = false;
  1579. adapter->swap = adapter_info.swap;
  1580. dev->kvm->arch.adapters[adapter->id] = adapter;
  1581. return 0;
  1582. }
  1583. int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
  1584. {
  1585. int ret;
  1586. struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
  1587. if (!adapter || !adapter->maskable)
  1588. return -EINVAL;
  1589. ret = adapter->masked;
  1590. adapter->masked = masked;
  1591. return ret;
  1592. }
  1593. static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
  1594. {
  1595. struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
  1596. struct s390_map_info *map;
  1597. int ret;
  1598. if (!adapter || !addr)
  1599. return -EINVAL;
  1600. map = kzalloc(sizeof(*map), GFP_KERNEL);
  1601. if (!map) {
  1602. ret = -ENOMEM;
  1603. goto out;
  1604. }
  1605. INIT_LIST_HEAD(&map->list);
  1606. map->guest_addr = addr;
  1607. map->addr = gmap_translate(kvm->arch.gmap, addr);
  1608. if (map->addr == -EFAULT) {
  1609. ret = -EFAULT;
  1610. goto out;
  1611. }
  1612. ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
  1613. if (ret < 0)
  1614. goto out;
  1615. BUG_ON(ret != 1);
  1616. down_write(&adapter->maps_lock);
  1617. if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
  1618. list_add_tail(&map->list, &adapter->maps);
  1619. ret = 0;
  1620. } else {
  1621. put_page(map->page);
  1622. ret = -EINVAL;
  1623. }
  1624. up_write(&adapter->maps_lock);
  1625. out:
  1626. if (ret)
  1627. kfree(map);
  1628. return ret;
  1629. }
  1630. static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
  1631. {
  1632. struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
  1633. struct s390_map_info *map, *tmp;
  1634. int found = 0;
  1635. if (!adapter || !addr)
  1636. return -EINVAL;
  1637. down_write(&adapter->maps_lock);
  1638. list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
  1639. if (map->guest_addr == addr) {
  1640. found = 1;
  1641. atomic_dec(&adapter->nr_maps);
  1642. list_del(&map->list);
  1643. put_page(map->page);
  1644. kfree(map);
  1645. break;
  1646. }
  1647. }
  1648. up_write(&adapter->maps_lock);
  1649. return found ? 0 : -EINVAL;
  1650. }
  1651. void kvm_s390_destroy_adapters(struct kvm *kvm)
  1652. {
  1653. int i;
  1654. struct s390_map_info *map, *tmp;
  1655. for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
  1656. if (!kvm->arch.adapters[i])
  1657. continue;
  1658. list_for_each_entry_safe(map, tmp,
  1659. &kvm->arch.adapters[i]->maps, list) {
  1660. list_del(&map->list);
  1661. put_page(map->page);
  1662. kfree(map);
  1663. }
  1664. kfree(kvm->arch.adapters[i]);
  1665. }
  1666. }
  1667. static int modify_io_adapter(struct kvm_device *dev,
  1668. struct kvm_device_attr *attr)
  1669. {
  1670. struct kvm_s390_io_adapter_req req;
  1671. struct s390_io_adapter *adapter;
  1672. int ret;
  1673. if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
  1674. return -EFAULT;
  1675. adapter = get_io_adapter(dev->kvm, req.id);
  1676. if (!adapter)
  1677. return -EINVAL;
  1678. switch (req.type) {
  1679. case KVM_S390_IO_ADAPTER_MASK:
  1680. ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
  1681. if (ret > 0)
  1682. ret = 0;
  1683. break;
  1684. case KVM_S390_IO_ADAPTER_MAP:
  1685. ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
  1686. break;
  1687. case KVM_S390_IO_ADAPTER_UNMAP:
  1688. ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
  1689. break;
  1690. default:
  1691. ret = -EINVAL;
  1692. }
  1693. return ret;
  1694. }
  1695. static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
  1696. {
  1697. int r = 0;
  1698. unsigned int i;
  1699. struct kvm_vcpu *vcpu;
  1700. switch (attr->group) {
  1701. case KVM_DEV_FLIC_ENQUEUE:
  1702. r = enqueue_floating_irq(dev, attr);
  1703. break;
  1704. case KVM_DEV_FLIC_CLEAR_IRQS:
  1705. kvm_s390_clear_float_irqs(dev->kvm);
  1706. break;
  1707. case KVM_DEV_FLIC_APF_ENABLE:
  1708. dev->kvm->arch.gmap->pfault_enabled = 1;
  1709. break;
  1710. case KVM_DEV_FLIC_APF_DISABLE_WAIT:
  1711. dev->kvm->arch.gmap->pfault_enabled = 0;
  1712. /*
  1713. * Make sure no async faults are in transition when
  1714. * clearing the queues. So we don't need to worry
  1715. * about late coming workers.
  1716. */
  1717. synchronize_srcu(&dev->kvm->srcu);
  1718. kvm_for_each_vcpu(i, vcpu, dev->kvm)
  1719. kvm_clear_async_pf_completion_queue(vcpu);
  1720. break;
  1721. case KVM_DEV_FLIC_ADAPTER_REGISTER:
  1722. r = register_io_adapter(dev, attr);
  1723. break;
  1724. case KVM_DEV_FLIC_ADAPTER_MODIFY:
  1725. r = modify_io_adapter(dev, attr);
  1726. break;
  1727. default:
  1728. r = -EINVAL;
  1729. }
  1730. return r;
  1731. }
  1732. static int flic_create(struct kvm_device *dev, u32 type)
  1733. {
  1734. if (!dev)
  1735. return -EINVAL;
  1736. if (dev->kvm->arch.flic)
  1737. return -EINVAL;
  1738. dev->kvm->arch.flic = dev;
  1739. return 0;
  1740. }
  1741. static void flic_destroy(struct kvm_device *dev)
  1742. {
  1743. dev->kvm->arch.flic = NULL;
  1744. kfree(dev);
  1745. }
  1746. /* s390 floating irq controller (flic) */
  1747. struct kvm_device_ops kvm_flic_ops = {
  1748. .name = "kvm-flic",
  1749. .get_attr = flic_get_attr,
  1750. .set_attr = flic_set_attr,
  1751. .create = flic_create,
  1752. .destroy = flic_destroy,
  1753. };
  1754. static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
  1755. {
  1756. unsigned long bit;
  1757. bit = bit_nr + (addr % PAGE_SIZE) * 8;
  1758. return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
  1759. }
  1760. static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
  1761. u64 addr)
  1762. {
  1763. struct s390_map_info *map;
  1764. if (!adapter)
  1765. return NULL;
  1766. list_for_each_entry(map, &adapter->maps, list) {
  1767. if (map->guest_addr == addr)
  1768. return map;
  1769. }
  1770. return NULL;
  1771. }
  1772. static int adapter_indicators_set(struct kvm *kvm,
  1773. struct s390_io_adapter *adapter,
  1774. struct kvm_s390_adapter_int *adapter_int)
  1775. {
  1776. unsigned long bit;
  1777. int summary_set, idx;
  1778. struct s390_map_info *info;
  1779. void *map;
  1780. info = get_map_info(adapter, adapter_int->ind_addr);
  1781. if (!info)
  1782. return -1;
  1783. map = page_address(info->page);
  1784. bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
  1785. set_bit(bit, map);
  1786. idx = srcu_read_lock(&kvm->srcu);
  1787. mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
  1788. set_page_dirty_lock(info->page);
  1789. info = get_map_info(adapter, adapter_int->summary_addr);
  1790. if (!info) {
  1791. srcu_read_unlock(&kvm->srcu, idx);
  1792. return -1;
  1793. }
  1794. map = page_address(info->page);
  1795. bit = get_ind_bit(info->addr, adapter_int->summary_offset,
  1796. adapter->swap);
  1797. summary_set = test_and_set_bit(bit, map);
  1798. mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
  1799. set_page_dirty_lock(info->page);
  1800. srcu_read_unlock(&kvm->srcu, idx);
  1801. return summary_set ? 0 : 1;
  1802. }
  1803. /*
  1804. * < 0 - not injected due to error
  1805. * = 0 - coalesced, summary indicator already active
  1806. * > 0 - injected interrupt
  1807. */
  1808. static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
  1809. struct kvm *kvm, int irq_source_id, int level,
  1810. bool line_status)
  1811. {
  1812. int ret;
  1813. struct s390_io_adapter *adapter;
  1814. /* We're only interested in the 0->1 transition. */
  1815. if (!level)
  1816. return 0;
  1817. adapter = get_io_adapter(kvm, e->adapter.adapter_id);
  1818. if (!adapter)
  1819. return -1;
  1820. down_read(&adapter->maps_lock);
  1821. ret = adapter_indicators_set(kvm, adapter, &e->adapter);
  1822. up_read(&adapter->maps_lock);
  1823. if ((ret > 0) && !adapter->masked) {
  1824. struct kvm_s390_interrupt s390int = {
  1825. .type = KVM_S390_INT_IO(1, 0, 0, 0),
  1826. .parm = 0,
  1827. .parm64 = (adapter->isc << 27) | 0x80000000,
  1828. };
  1829. ret = kvm_s390_inject_vm(kvm, &s390int);
  1830. if (ret == 0)
  1831. ret = 1;
  1832. }
  1833. return ret;
  1834. }
  1835. int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
  1836. const struct kvm_irq_routing_entry *ue)
  1837. {
  1838. int ret;
  1839. switch (ue->type) {
  1840. case KVM_IRQ_ROUTING_S390_ADAPTER:
  1841. e->set = set_adapter_int;
  1842. e->adapter.summary_addr = ue->u.adapter.summary_addr;
  1843. e->adapter.ind_addr = ue->u.adapter.ind_addr;
  1844. e->adapter.summary_offset = ue->u.adapter.summary_offset;
  1845. e->adapter.ind_offset = ue->u.adapter.ind_offset;
  1846. e->adapter.adapter_id = ue->u.adapter.adapter_id;
  1847. ret = 0;
  1848. break;
  1849. default:
  1850. ret = -EINVAL;
  1851. }
  1852. return ret;
  1853. }
  1854. int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
  1855. int irq_source_id, int level, bool line_status)
  1856. {
  1857. return -EINVAL;
  1858. }
  1859. int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
  1860. {
  1861. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  1862. struct kvm_s390_irq *buf;
  1863. int r = 0;
  1864. int n;
  1865. buf = vmalloc(len);
  1866. if (!buf)
  1867. return -ENOMEM;
  1868. if (copy_from_user((void *) buf, irqstate, len)) {
  1869. r = -EFAULT;
  1870. goto out_free;
  1871. }
  1872. /*
  1873. * Don't allow setting the interrupt state
  1874. * when there are already interrupts pending
  1875. */
  1876. spin_lock(&li->lock);
  1877. if (li->pending_irqs) {
  1878. r = -EBUSY;
  1879. goto out_unlock;
  1880. }
  1881. for (n = 0; n < len / sizeof(*buf); n++) {
  1882. r = do_inject_vcpu(vcpu, &buf[n]);
  1883. if (r)
  1884. break;
  1885. }
  1886. out_unlock:
  1887. spin_unlock(&li->lock);
  1888. out_free:
  1889. vfree(buf);
  1890. return r;
  1891. }
  1892. static void store_local_irq(struct kvm_s390_local_interrupt *li,
  1893. struct kvm_s390_irq *irq,
  1894. unsigned long irq_type)
  1895. {
  1896. switch (irq_type) {
  1897. case IRQ_PEND_MCHK_EX:
  1898. case IRQ_PEND_MCHK_REP:
  1899. irq->type = KVM_S390_MCHK;
  1900. irq->u.mchk = li->irq.mchk;
  1901. break;
  1902. case IRQ_PEND_PROG:
  1903. irq->type = KVM_S390_PROGRAM_INT;
  1904. irq->u.pgm = li->irq.pgm;
  1905. break;
  1906. case IRQ_PEND_PFAULT_INIT:
  1907. irq->type = KVM_S390_INT_PFAULT_INIT;
  1908. irq->u.ext = li->irq.ext;
  1909. break;
  1910. case IRQ_PEND_EXT_EXTERNAL:
  1911. irq->type = KVM_S390_INT_EXTERNAL_CALL;
  1912. irq->u.extcall = li->irq.extcall;
  1913. break;
  1914. case IRQ_PEND_EXT_CLOCK_COMP:
  1915. irq->type = KVM_S390_INT_CLOCK_COMP;
  1916. break;
  1917. case IRQ_PEND_EXT_CPU_TIMER:
  1918. irq->type = KVM_S390_INT_CPU_TIMER;
  1919. break;
  1920. case IRQ_PEND_SIGP_STOP:
  1921. irq->type = KVM_S390_SIGP_STOP;
  1922. irq->u.stop = li->irq.stop;
  1923. break;
  1924. case IRQ_PEND_RESTART:
  1925. irq->type = KVM_S390_RESTART;
  1926. break;
  1927. case IRQ_PEND_SET_PREFIX:
  1928. irq->type = KVM_S390_SIGP_SET_PREFIX;
  1929. irq->u.prefix = li->irq.prefix;
  1930. break;
  1931. }
  1932. }
  1933. int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
  1934. {
  1935. uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
  1936. unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
  1937. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  1938. unsigned long pending_irqs;
  1939. struct kvm_s390_irq irq;
  1940. unsigned long irq_type;
  1941. int cpuaddr;
  1942. int n = 0;
  1943. spin_lock(&li->lock);
  1944. pending_irqs = li->pending_irqs;
  1945. memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
  1946. sizeof(sigp_emerg_pending));
  1947. spin_unlock(&li->lock);
  1948. for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
  1949. memset(&irq, 0, sizeof(irq));
  1950. if (irq_type == IRQ_PEND_EXT_EMERGENCY)
  1951. continue;
  1952. if (n + sizeof(irq) > len)
  1953. return -ENOBUFS;
  1954. store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
  1955. if (copy_to_user(&buf[n], &irq, sizeof(irq)))
  1956. return -EFAULT;
  1957. n += sizeof(irq);
  1958. }
  1959. if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
  1960. for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
  1961. memset(&irq, 0, sizeof(irq));
  1962. if (n + sizeof(irq) > len)
  1963. return -ENOBUFS;
  1964. irq.type = KVM_S390_INT_EMERGENCY;
  1965. irq.u.emerg.code = cpuaddr;
  1966. if (copy_to_user(&buf[n], &irq, sizeof(irq)))
  1967. return -EFAULT;
  1968. n += sizeof(irq);
  1969. }
  1970. }
  1971. if ((sigp_ctrl & SIGP_CTRL_C) &&
  1972. (atomic_read(&vcpu->arch.sie_block->cpuflags) &
  1973. CPUSTAT_ECALL_PEND)) {
  1974. if (n + sizeof(irq) > len)
  1975. return -ENOBUFS;
  1976. memset(&irq, 0, sizeof(irq));
  1977. irq.type = KVM_S390_INT_EXTERNAL_CALL;
  1978. irq.u.extcall.code = sigp_ctrl & SIGP_CTRL_SCN_MASK;
  1979. if (copy_to_user(&buf[n], &irq, sizeof(irq)))
  1980. return -EFAULT;
  1981. n += sizeof(irq);
  1982. }
  1983. return n;
  1984. }