vgic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * Copyright (C) 2015, 2016 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/kvm.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/list_sort.h>
  21. #include <linux/nospec.h>
  22. #include <asm/kvm_hyp.h>
  23. #include "vgic.h"
  24. #define CREATE_TRACE_POINTS
  25. #include "trace.h"
  26. #ifdef CONFIG_DEBUG_SPINLOCK
  27. #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
  28. #else
  29. #define DEBUG_SPINLOCK_BUG_ON(p)
  30. #endif
  31. struct vgic_global kvm_vgic_global_state __ro_after_init = {
  32. .gicv3_cpuif = STATIC_KEY_FALSE_INIT,
  33. };
  34. /*
  35. * Locking order is always:
  36. * kvm->lock (mutex)
  37. * its->cmd_lock (mutex)
  38. * its->its_lock (mutex)
  39. * vgic_cpu->ap_list_lock
  40. * kvm->lpi_list_lock
  41. * vgic_irq->irq_lock
  42. *
  43. * If you need to take multiple locks, always take the upper lock first,
  44. * then the lower ones, e.g. first take the its_lock, then the irq_lock.
  45. * If you are already holding a lock and need to take a higher one, you
  46. * have to drop the lower ranking lock first and re-aquire it after having
  47. * taken the upper one.
  48. *
  49. * When taking more than one ap_list_lock at the same time, always take the
  50. * lowest numbered VCPU's ap_list_lock first, so:
  51. * vcpuX->vcpu_id < vcpuY->vcpu_id:
  52. * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
  53. * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
  54. *
  55. * Since the VGIC must support injecting virtual interrupts from ISRs, we have
  56. * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
  57. * spinlocks for any lock that may be taken while injecting an interrupt.
  58. */
  59. /*
  60. * Iterate over the VM's list of mapped LPIs to find the one with a
  61. * matching interrupt ID and return a reference to the IRQ structure.
  62. */
  63. static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
  64. {
  65. struct vgic_dist *dist = &kvm->arch.vgic;
  66. struct vgic_irq *irq = NULL;
  67. spin_lock(&dist->lpi_list_lock);
  68. list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
  69. if (irq->intid != intid)
  70. continue;
  71. /*
  72. * This increases the refcount, the caller is expected to
  73. * call vgic_put_irq() later once it's finished with the IRQ.
  74. */
  75. vgic_get_irq_kref(irq);
  76. goto out_unlock;
  77. }
  78. irq = NULL;
  79. out_unlock:
  80. spin_unlock(&dist->lpi_list_lock);
  81. return irq;
  82. }
  83. /*
  84. * This looks up the virtual interrupt ID to get the corresponding
  85. * struct vgic_irq. It also increases the refcount, so any caller is expected
  86. * to call vgic_put_irq() once it's finished with this IRQ.
  87. */
  88. struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
  89. u32 intid)
  90. {
  91. /* SGIs and PPIs */
  92. if (intid <= VGIC_MAX_PRIVATE) {
  93. intid = array_index_nospec(intid, VGIC_MAX_PRIVATE);
  94. return &vcpu->arch.vgic_cpu.private_irqs[intid];
  95. }
  96. /* SPIs */
  97. if (intid <= VGIC_MAX_SPI) {
  98. intid = array_index_nospec(intid, VGIC_MAX_SPI);
  99. return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
  100. }
  101. /* LPIs */
  102. if (intid >= VGIC_MIN_LPI)
  103. return vgic_get_lpi(kvm, intid);
  104. WARN(1, "Looking up struct vgic_irq for reserved INTID");
  105. return NULL;
  106. }
  107. /*
  108. * We can't do anything in here, because we lack the kvm pointer to
  109. * lock and remove the item from the lpi_list. So we keep this function
  110. * empty and use the return value of kref_put() to trigger the freeing.
  111. */
  112. static void vgic_irq_release(struct kref *ref)
  113. {
  114. }
  115. void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
  116. {
  117. struct vgic_dist *dist = &kvm->arch.vgic;
  118. if (irq->intid < VGIC_MIN_LPI)
  119. return;
  120. spin_lock(&dist->lpi_list_lock);
  121. if (!kref_put(&irq->refcount, vgic_irq_release)) {
  122. spin_unlock(&dist->lpi_list_lock);
  123. return;
  124. };
  125. list_del(&irq->lpi_list);
  126. dist->lpi_list_count--;
  127. spin_unlock(&dist->lpi_list_lock);
  128. kfree(irq);
  129. }
  130. void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending)
  131. {
  132. WARN_ON(irq_set_irqchip_state(irq->host_irq,
  133. IRQCHIP_STATE_PENDING,
  134. pending));
  135. }
  136. bool vgic_get_phys_line_level(struct vgic_irq *irq)
  137. {
  138. bool line_level;
  139. BUG_ON(!irq->hw);
  140. if (irq->get_input_level)
  141. return irq->get_input_level(irq->intid);
  142. WARN_ON(irq_get_irqchip_state(irq->host_irq,
  143. IRQCHIP_STATE_PENDING,
  144. &line_level));
  145. return line_level;
  146. }
  147. /* Set/Clear the physical active state */
  148. void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active)
  149. {
  150. BUG_ON(!irq->hw);
  151. WARN_ON(irq_set_irqchip_state(irq->host_irq,
  152. IRQCHIP_STATE_ACTIVE,
  153. active));
  154. }
  155. /**
  156. * kvm_vgic_target_oracle - compute the target vcpu for an irq
  157. *
  158. * @irq: The irq to route. Must be already locked.
  159. *
  160. * Based on the current state of the interrupt (enabled, pending,
  161. * active, vcpu and target_vcpu), compute the next vcpu this should be
  162. * given to. Return NULL if this shouldn't be injected at all.
  163. *
  164. * Requires the IRQ lock to be held.
  165. */
  166. static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq)
  167. {
  168. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  169. /* If the interrupt is active, it must stay on the current vcpu */
  170. if (irq->active)
  171. return irq->vcpu ? : irq->target_vcpu;
  172. /*
  173. * If the IRQ is not active but enabled and pending, we should direct
  174. * it to its configured target VCPU.
  175. * If the distributor is disabled, pending interrupts shouldn't be
  176. * forwarded.
  177. */
  178. if (irq->enabled && irq_is_pending(irq)) {
  179. if (unlikely(irq->target_vcpu &&
  180. !irq->target_vcpu->kvm->arch.vgic.enabled))
  181. return NULL;
  182. return irq->target_vcpu;
  183. }
  184. /* If neither active nor pending and enabled, then this IRQ should not
  185. * be queued to any VCPU.
  186. */
  187. return NULL;
  188. }
  189. /*
  190. * The order of items in the ap_lists defines how we'll pack things in LRs as
  191. * well, the first items in the list being the first things populated in the
  192. * LRs.
  193. *
  194. * A hard rule is that active interrupts can never be pushed out of the LRs
  195. * (and therefore take priority) since we cannot reliably trap on deactivation
  196. * of IRQs and therefore they have to be present in the LRs.
  197. *
  198. * Otherwise things should be sorted by the priority field and the GIC
  199. * hardware support will take care of preemption of priority groups etc.
  200. *
  201. * Return negative if "a" sorts before "b", 0 to preserve order, and positive
  202. * to sort "b" before "a".
  203. */
  204. static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
  205. {
  206. struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
  207. struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
  208. bool penda, pendb;
  209. int ret;
  210. spin_lock(&irqa->irq_lock);
  211. spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING);
  212. if (irqa->active || irqb->active) {
  213. ret = (int)irqb->active - (int)irqa->active;
  214. goto out;
  215. }
  216. penda = irqa->enabled && irq_is_pending(irqa);
  217. pendb = irqb->enabled && irq_is_pending(irqb);
  218. if (!penda || !pendb) {
  219. ret = (int)pendb - (int)penda;
  220. goto out;
  221. }
  222. /* Both pending and enabled, sort by priority */
  223. ret = irqa->priority - irqb->priority;
  224. out:
  225. spin_unlock(&irqb->irq_lock);
  226. spin_unlock(&irqa->irq_lock);
  227. return ret;
  228. }
  229. /* Must be called with the ap_list_lock held */
  230. static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)
  231. {
  232. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  233. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  234. list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
  235. }
  236. /*
  237. * Only valid injection if changing level for level-triggered IRQs or for a
  238. * rising edge, and in-kernel connected IRQ lines can only be controlled by
  239. * their owner.
  240. */
  241. static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner)
  242. {
  243. if (irq->owner != owner)
  244. return false;
  245. switch (irq->config) {
  246. case VGIC_CONFIG_LEVEL:
  247. return irq->line_level != level;
  248. case VGIC_CONFIG_EDGE:
  249. return level;
  250. }
  251. return false;
  252. }
  253. /*
  254. * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
  255. * Do the queuing if necessary, taking the right locks in the right order.
  256. * Returns true when the IRQ was queued, false otherwise.
  257. *
  258. * Needs to be entered with the IRQ lock already held, but will return
  259. * with all locks dropped.
  260. */
  261. bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
  262. unsigned long flags)
  263. {
  264. struct kvm_vcpu *vcpu;
  265. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  266. retry:
  267. vcpu = vgic_target_oracle(irq);
  268. if (irq->vcpu || !vcpu) {
  269. /*
  270. * If this IRQ is already on a VCPU's ap_list, then it
  271. * cannot be moved or modified and there is no more work for
  272. * us to do.
  273. *
  274. * Otherwise, if the irq is not pending and enabled, it does
  275. * not need to be inserted into an ap_list and there is also
  276. * no more work for us to do.
  277. */
  278. spin_unlock_irqrestore(&irq->irq_lock, flags);
  279. /*
  280. * We have to kick the VCPU here, because we could be
  281. * queueing an edge-triggered interrupt for which we
  282. * get no EOI maintenance interrupt. In that case,
  283. * while the IRQ is already on the VCPU's AP list, the
  284. * VCPU could have EOI'ed the original interrupt and
  285. * won't see this one until it exits for some other
  286. * reason.
  287. */
  288. if (vcpu) {
  289. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  290. kvm_vcpu_kick(vcpu);
  291. }
  292. return false;
  293. }
  294. /*
  295. * We must unlock the irq lock to take the ap_list_lock where
  296. * we are going to insert this new pending interrupt.
  297. */
  298. spin_unlock_irqrestore(&irq->irq_lock, flags);
  299. /* someone can do stuff here, which we re-check below */
  300. spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  301. spin_lock(&irq->irq_lock);
  302. /*
  303. * Did something change behind our backs?
  304. *
  305. * There are two cases:
  306. * 1) The irq lost its pending state or was disabled behind our
  307. * backs and/or it was queued to another VCPU's ap_list.
  308. * 2) Someone changed the affinity on this irq behind our
  309. * backs and we are now holding the wrong ap_list_lock.
  310. *
  311. * In both cases, drop the locks and retry.
  312. */
  313. if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) {
  314. spin_unlock(&irq->irq_lock);
  315. spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  316. spin_lock_irqsave(&irq->irq_lock, flags);
  317. goto retry;
  318. }
  319. /*
  320. * Grab a reference to the irq to reflect the fact that it is
  321. * now in the ap_list.
  322. */
  323. vgic_get_irq_kref(irq);
  324. list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
  325. irq->vcpu = vcpu;
  326. spin_unlock(&irq->irq_lock);
  327. spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
  328. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  329. kvm_vcpu_kick(vcpu);
  330. return true;
  331. }
  332. /**
  333. * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic
  334. * @kvm: The VM structure pointer
  335. * @cpuid: The CPU for PPIs
  336. * @intid: The INTID to inject a new state to.
  337. * @level: Edge-triggered: true: to trigger the interrupt
  338. * false: to ignore the call
  339. * Level-sensitive true: raise the input signal
  340. * false: lower the input signal
  341. * @owner: The opaque pointer to the owner of the IRQ being raised to verify
  342. * that the caller is allowed to inject this IRQ. Userspace
  343. * injections will have owner == NULL.
  344. *
  345. * The VGIC is not concerned with devices being active-LOW or active-HIGH for
  346. * level-sensitive interrupts. You can think of the level parameter as 1
  347. * being HIGH and 0 being LOW and all devices being active-HIGH.
  348. */
  349. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
  350. bool level, void *owner)
  351. {
  352. struct kvm_vcpu *vcpu;
  353. struct vgic_irq *irq;
  354. unsigned long flags;
  355. int ret;
  356. trace_vgic_update_irq_pending(cpuid, intid, level);
  357. ret = vgic_lazy_init(kvm);
  358. if (ret)
  359. return ret;
  360. vcpu = kvm_get_vcpu(kvm, cpuid);
  361. if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
  362. return -EINVAL;
  363. irq = vgic_get_irq(kvm, vcpu, intid);
  364. if (!irq)
  365. return -EINVAL;
  366. spin_lock_irqsave(&irq->irq_lock, flags);
  367. if (!vgic_validate_injection(irq, level, owner)) {
  368. /* Nothing to see here, move along... */
  369. spin_unlock_irqrestore(&irq->irq_lock, flags);
  370. vgic_put_irq(kvm, irq);
  371. return 0;
  372. }
  373. if (irq->config == VGIC_CONFIG_LEVEL)
  374. irq->line_level = level;
  375. else
  376. irq->pending_latch = true;
  377. vgic_queue_irq_unlock(kvm, irq, flags);
  378. vgic_put_irq(kvm, irq);
  379. return 0;
  380. }
  381. /* @irq->irq_lock must be held */
  382. static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
  383. unsigned int host_irq,
  384. bool (*get_input_level)(int vindid))
  385. {
  386. struct irq_desc *desc;
  387. struct irq_data *data;
  388. /*
  389. * Find the physical IRQ number corresponding to @host_irq
  390. */
  391. desc = irq_to_desc(host_irq);
  392. if (!desc) {
  393. kvm_err("%s: no interrupt descriptor\n", __func__);
  394. return -EINVAL;
  395. }
  396. data = irq_desc_get_irq_data(desc);
  397. while (data->parent_data)
  398. data = data->parent_data;
  399. irq->hw = true;
  400. irq->host_irq = host_irq;
  401. irq->hwintid = data->hwirq;
  402. irq->get_input_level = get_input_level;
  403. return 0;
  404. }
  405. /* @irq->irq_lock must be held */
  406. static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
  407. {
  408. irq->hw = false;
  409. irq->hwintid = 0;
  410. irq->get_input_level = NULL;
  411. }
  412. int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
  413. u32 vintid, bool (*get_input_level)(int vindid))
  414. {
  415. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  416. unsigned long flags;
  417. int ret;
  418. BUG_ON(!irq);
  419. spin_lock_irqsave(&irq->irq_lock, flags);
  420. ret = kvm_vgic_map_irq(vcpu, irq, host_irq, get_input_level);
  421. spin_unlock_irqrestore(&irq->irq_lock, flags);
  422. vgic_put_irq(vcpu->kvm, irq);
  423. return ret;
  424. }
  425. /**
  426. * kvm_vgic_reset_mapped_irq - Reset a mapped IRQ
  427. * @vcpu: The VCPU pointer
  428. * @vintid: The INTID of the interrupt
  429. *
  430. * Reset the active and pending states of a mapped interrupt. Kernel
  431. * subsystems injecting mapped interrupts should reset their interrupt lines
  432. * when we are doing a reset of the VM.
  433. */
  434. void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid)
  435. {
  436. struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  437. unsigned long flags;
  438. if (!irq->hw)
  439. goto out;
  440. spin_lock_irqsave(&irq->irq_lock, flags);
  441. irq->active = false;
  442. irq->pending_latch = false;
  443. irq->line_level = false;
  444. spin_unlock_irqrestore(&irq->irq_lock, flags);
  445. out:
  446. vgic_put_irq(vcpu->kvm, irq);
  447. }
  448. int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid)
  449. {
  450. struct vgic_irq *irq;
  451. unsigned long flags;
  452. if (!vgic_initialized(vcpu->kvm))
  453. return -EAGAIN;
  454. irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  455. BUG_ON(!irq);
  456. spin_lock_irqsave(&irq->irq_lock, flags);
  457. kvm_vgic_unmap_irq(irq);
  458. spin_unlock_irqrestore(&irq->irq_lock, flags);
  459. vgic_put_irq(vcpu->kvm, irq);
  460. return 0;
  461. }
  462. /**
  463. * kvm_vgic_set_owner - Set the owner of an interrupt for a VM
  464. *
  465. * @vcpu: Pointer to the VCPU (used for PPIs)
  466. * @intid: The virtual INTID identifying the interrupt (PPI or SPI)
  467. * @owner: Opaque pointer to the owner
  468. *
  469. * Returns 0 if intid is not already used by another in-kernel device and the
  470. * owner is set, otherwise returns an error code.
  471. */
  472. int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
  473. {
  474. struct vgic_irq *irq;
  475. unsigned long flags;
  476. int ret = 0;
  477. if (!vgic_initialized(vcpu->kvm))
  478. return -EAGAIN;
  479. /* SGIs and LPIs cannot be wired up to any device */
  480. if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid))
  481. return -EINVAL;
  482. irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
  483. spin_lock_irqsave(&irq->irq_lock, flags);
  484. if (irq->owner && irq->owner != owner)
  485. ret = -EEXIST;
  486. else
  487. irq->owner = owner;
  488. spin_unlock_irqrestore(&irq->irq_lock, flags);
  489. return ret;
  490. }
  491. /**
  492. * vgic_prune_ap_list - Remove non-relevant interrupts from the list
  493. *
  494. * @vcpu: The VCPU pointer
  495. *
  496. * Go over the list of "interesting" interrupts, and prune those that we
  497. * won't have to consider in the near future.
  498. */
  499. static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
  500. {
  501. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  502. struct vgic_irq *irq, *tmp;
  503. unsigned long flags;
  504. retry:
  505. spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
  506. list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
  507. struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
  508. bool target_vcpu_needs_kick = false;
  509. spin_lock(&irq->irq_lock);
  510. BUG_ON(vcpu != irq->vcpu);
  511. target_vcpu = vgic_target_oracle(irq);
  512. if (!target_vcpu) {
  513. /*
  514. * We don't need to process this interrupt any
  515. * further, move it off the list.
  516. */
  517. list_del(&irq->ap_list);
  518. irq->vcpu = NULL;
  519. spin_unlock(&irq->irq_lock);
  520. /*
  521. * This vgic_put_irq call matches the
  522. * vgic_get_irq_kref in vgic_queue_irq_unlock,
  523. * where we added the LPI to the ap_list. As
  524. * we remove the irq from the list, we drop
  525. * also drop the refcount.
  526. */
  527. vgic_put_irq(vcpu->kvm, irq);
  528. continue;
  529. }
  530. if (target_vcpu == vcpu) {
  531. /* We're on the right CPU */
  532. spin_unlock(&irq->irq_lock);
  533. continue;
  534. }
  535. /* This interrupt looks like it has to be migrated. */
  536. spin_unlock(&irq->irq_lock);
  537. spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
  538. /*
  539. * Ensure locking order by always locking the smallest
  540. * ID first.
  541. */
  542. if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
  543. vcpuA = vcpu;
  544. vcpuB = target_vcpu;
  545. } else {
  546. vcpuA = target_vcpu;
  547. vcpuB = vcpu;
  548. }
  549. spin_lock_irqsave(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
  550. spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
  551. SINGLE_DEPTH_NESTING);
  552. spin_lock(&irq->irq_lock);
  553. /*
  554. * If the affinity has been preserved, move the
  555. * interrupt around. Otherwise, it means things have
  556. * changed while the interrupt was unlocked, and we
  557. * need to replay this.
  558. *
  559. * In all cases, we cannot trust the list not to have
  560. * changed, so we restart from the beginning.
  561. */
  562. if (target_vcpu == vgic_target_oracle(irq)) {
  563. struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
  564. list_del(&irq->ap_list);
  565. irq->vcpu = target_vcpu;
  566. list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
  567. target_vcpu_needs_kick = true;
  568. }
  569. spin_unlock(&irq->irq_lock);
  570. spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
  571. spin_unlock_irqrestore(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
  572. if (target_vcpu_needs_kick) {
  573. kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
  574. kvm_vcpu_kick(target_vcpu);
  575. }
  576. goto retry;
  577. }
  578. spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
  579. }
  580. static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
  581. {
  582. if (kvm_vgic_global_state.type == VGIC_V2)
  583. vgic_v2_fold_lr_state(vcpu);
  584. else
  585. vgic_v3_fold_lr_state(vcpu);
  586. }
  587. /* Requires the irq_lock to be held. */
  588. static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
  589. struct vgic_irq *irq, int lr)
  590. {
  591. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
  592. if (kvm_vgic_global_state.type == VGIC_V2)
  593. vgic_v2_populate_lr(vcpu, irq, lr);
  594. else
  595. vgic_v3_populate_lr(vcpu, irq, lr);
  596. }
  597. static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
  598. {
  599. if (kvm_vgic_global_state.type == VGIC_V2)
  600. vgic_v2_clear_lr(vcpu, lr);
  601. else
  602. vgic_v3_clear_lr(vcpu, lr);
  603. }
  604. static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
  605. {
  606. if (kvm_vgic_global_state.type == VGIC_V2)
  607. vgic_v2_set_underflow(vcpu);
  608. else
  609. vgic_v3_set_underflow(vcpu);
  610. }
  611. /* Requires the ap_list_lock to be held. */
  612. static int compute_ap_list_depth(struct kvm_vcpu *vcpu,
  613. bool *multi_sgi)
  614. {
  615. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  616. struct vgic_irq *irq;
  617. int count = 0;
  618. *multi_sgi = false;
  619. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  620. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  621. int w;
  622. spin_lock(&irq->irq_lock);
  623. /* GICv2 SGIs can count for more than one... */
  624. w = vgic_irq_get_lr_count(irq);
  625. spin_unlock(&irq->irq_lock);
  626. count += w;
  627. *multi_sgi |= (w > 1);
  628. }
  629. return count;
  630. }
  631. /* Requires the VCPU's ap_list_lock to be held. */
  632. static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
  633. {
  634. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  635. struct vgic_irq *irq;
  636. int count;
  637. bool multi_sgi;
  638. u8 prio = 0xff;
  639. DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
  640. count = compute_ap_list_depth(vcpu, &multi_sgi);
  641. if (count > kvm_vgic_global_state.nr_lr || multi_sgi)
  642. vgic_sort_ap_list(vcpu);
  643. count = 0;
  644. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  645. spin_lock(&irq->irq_lock);
  646. /*
  647. * If we have multi-SGIs in the pipeline, we need to
  648. * guarantee that they are all seen before any IRQ of
  649. * lower priority. In that case, we need to filter out
  650. * these interrupts by exiting early. This is easy as
  651. * the AP list has been sorted already.
  652. */
  653. if (multi_sgi && irq->priority > prio) {
  654. spin_unlock(&irq->irq_lock);
  655. break;
  656. }
  657. if (likely(vgic_target_oracle(irq) == vcpu)) {
  658. vgic_populate_lr(vcpu, irq, count++);
  659. if (irq->source)
  660. prio = irq->priority;
  661. }
  662. spin_unlock(&irq->irq_lock);
  663. if (count == kvm_vgic_global_state.nr_lr) {
  664. if (!list_is_last(&irq->ap_list,
  665. &vgic_cpu->ap_list_head))
  666. vgic_set_underflow(vcpu);
  667. break;
  668. }
  669. }
  670. vcpu->arch.vgic_cpu.used_lrs = count;
  671. /* Nuke remaining LRs */
  672. for ( ; count < kvm_vgic_global_state.nr_lr; count++)
  673. vgic_clear_lr(vcpu, count);
  674. }
  675. static inline bool can_access_vgic_from_kernel(void)
  676. {
  677. /*
  678. * GICv2 can always be accessed from the kernel because it is
  679. * memory-mapped, and VHE systems can access GICv3 EL2 system
  680. * registers.
  681. */
  682. return !static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) || has_vhe();
  683. }
  684. static inline void vgic_save_state(struct kvm_vcpu *vcpu)
  685. {
  686. if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  687. vgic_v2_save_state(vcpu);
  688. else
  689. __vgic_v3_save_state(vcpu);
  690. }
  691. /* Sync back the hardware VGIC state into our emulation after a guest's run. */
  692. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
  693. {
  694. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  695. WARN_ON(vgic_v4_sync_hwstate(vcpu));
  696. /* An empty ap_list_head implies used_lrs == 0 */
  697. if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
  698. return;
  699. if (can_access_vgic_from_kernel())
  700. vgic_save_state(vcpu);
  701. if (vgic_cpu->used_lrs)
  702. vgic_fold_lr_state(vcpu);
  703. vgic_prune_ap_list(vcpu);
  704. }
  705. static inline void vgic_restore_state(struct kvm_vcpu *vcpu)
  706. {
  707. if (!static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
  708. vgic_v2_restore_state(vcpu);
  709. else
  710. __vgic_v3_restore_state(vcpu);
  711. }
  712. /* Flush our emulation state into the GIC hardware before entering the guest. */
  713. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
  714. {
  715. WARN_ON(vgic_v4_flush_hwstate(vcpu));
  716. /*
  717. * If there are no virtual interrupts active or pending for this
  718. * VCPU, then there is no work to do and we can bail out without
  719. * taking any lock. There is a potential race with someone injecting
  720. * interrupts to the VCPU, but it is a benign race as the VCPU will
  721. * either observe the new interrupt before or after doing this check,
  722. * and introducing additional synchronization mechanism doesn't change
  723. * this.
  724. */
  725. if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head))
  726. return;
  727. DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
  728. spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
  729. vgic_flush_lr_state(vcpu);
  730. spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
  731. if (can_access_vgic_from_kernel())
  732. vgic_restore_state(vcpu);
  733. }
  734. void kvm_vgic_load(struct kvm_vcpu *vcpu)
  735. {
  736. if (unlikely(!vgic_initialized(vcpu->kvm)))
  737. return;
  738. if (kvm_vgic_global_state.type == VGIC_V2)
  739. vgic_v2_load(vcpu);
  740. else
  741. vgic_v3_load(vcpu);
  742. }
  743. void kvm_vgic_put(struct kvm_vcpu *vcpu)
  744. {
  745. if (unlikely(!vgic_initialized(vcpu->kvm)))
  746. return;
  747. if (kvm_vgic_global_state.type == VGIC_V2)
  748. vgic_v2_put(vcpu);
  749. else
  750. vgic_v3_put(vcpu);
  751. }
  752. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  753. {
  754. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  755. struct vgic_irq *irq;
  756. bool pending = false;
  757. unsigned long flags;
  758. if (!vcpu->kvm->arch.vgic.enabled)
  759. return false;
  760. if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last)
  761. return true;
  762. spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
  763. list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
  764. spin_lock(&irq->irq_lock);
  765. pending = irq_is_pending(irq) && irq->enabled;
  766. spin_unlock(&irq->irq_lock);
  767. if (pending)
  768. break;
  769. }
  770. spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
  771. return pending;
  772. }
  773. void vgic_kick_vcpus(struct kvm *kvm)
  774. {
  775. struct kvm_vcpu *vcpu;
  776. int c;
  777. /*
  778. * We've injected an interrupt, time to find out who deserves
  779. * a good kick...
  780. */
  781. kvm_for_each_vcpu(c, vcpu, kvm) {
  782. if (kvm_vgic_vcpu_pending_irq(vcpu)) {
  783. kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
  784. kvm_vcpu_kick(vcpu);
  785. }
  786. }
  787. }
  788. bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid)
  789. {
  790. struct vgic_irq *irq;
  791. bool map_is_active;
  792. unsigned long flags;
  793. if (!vgic_initialized(vcpu->kvm))
  794. return false;
  795. irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
  796. spin_lock_irqsave(&irq->irq_lock, flags);
  797. map_is_active = irq->hw && irq->active;
  798. spin_unlock_irqrestore(&irq->irq_lock, flags);
  799. vgic_put_irq(vcpu->kvm, irq);
  800. return map_is_active;
  801. }