interrupt.c 66 KB

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