vgic_irq.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * vgic_irq.c - Test userspace injection of IRQs
  4. *
  5. * This test validates the injection of IRQs from userspace using various
  6. * methods (e.g., KVM_IRQ_LINE) and modes (e.g., EOI). The guest "asks" the
  7. * host to inject a specific intid via a GUEST_SYNC call, and then checks that
  8. * it received it.
  9. */
  10. #include <asm/kvm.h>
  11. #include <asm/kvm_para.h>
  12. #include <sys/eventfd.h>
  13. #include <linux/sizes.h>
  14. #include "processor.h"
  15. #include "test_util.h"
  16. #include "kvm_util.h"
  17. #include "gic.h"
  18. #include "gic_v3.h"
  19. #include "vgic.h"
  20. /*
  21. * Stores the user specified args; it's passed to the guest and to every test
  22. * function.
  23. */
  24. struct test_args {
  25. uint32_t nr_irqs; /* number of KVM supported IRQs. */
  26. bool eoi_split; /* 1 is eoir+dir, 0 is eoir only */
  27. bool level_sensitive; /* 1 is level, 0 is edge */
  28. int kvm_max_routes; /* output of KVM_CAP_IRQ_ROUTING */
  29. bool kvm_supports_irqfd; /* output of KVM_CAP_IRQFD */
  30. uint32_t shared_data;
  31. };
  32. /*
  33. * KVM implements 32 priority levels:
  34. * 0x00 (highest priority) - 0xF8 (lowest priority), in steps of 8
  35. *
  36. * Note that these macros will still be correct in the case that KVM implements
  37. * more priority levels. Also note that 32 is the minimum for GICv3 and GICv2.
  38. */
  39. #define KVM_NUM_PRIOS 32
  40. #define KVM_PRIO_SHIFT 3 /* steps of 8 = 1 << 3 */
  41. #define KVM_PRIO_STEPS (1 << KVM_PRIO_SHIFT) /* 8 */
  42. #define LOWEST_PRIO (KVM_NUM_PRIOS - 1)
  43. #define CPU_PRIO_MASK (LOWEST_PRIO << KVM_PRIO_SHIFT) /* 0xf8 */
  44. #define IRQ_DEFAULT_PRIO (LOWEST_PRIO - 1)
  45. #define IRQ_DEFAULT_PRIO_REG (IRQ_DEFAULT_PRIO << KVM_PRIO_SHIFT) /* 0xf0 */
  46. /*
  47. * The kvm_inject_* utilities are used by the guest to ask the host to inject
  48. * interrupts (e.g., using the KVM_IRQ_LINE ioctl).
  49. */
  50. typedef enum {
  51. KVM_INJECT_EDGE_IRQ_LINE = 1,
  52. KVM_SET_IRQ_LINE,
  53. KVM_SET_IRQ_LINE_HIGH,
  54. KVM_SET_LEVEL_INFO_HIGH,
  55. KVM_INJECT_IRQFD,
  56. KVM_WRITE_ISPENDR,
  57. KVM_WRITE_ISACTIVER,
  58. } kvm_inject_cmd;
  59. struct kvm_inject_args {
  60. kvm_inject_cmd cmd;
  61. uint32_t first_intid;
  62. uint32_t num;
  63. int level;
  64. bool expect_failure;
  65. };
  66. /* Used on the guest side to perform the hypercall. */
  67. static void kvm_inject_call(kvm_inject_cmd cmd, uint32_t first_intid,
  68. uint32_t num, int level, bool expect_failure);
  69. /* Used on the host side to get the hypercall info. */
  70. static void kvm_inject_get_call(struct kvm_vm *vm, struct ucall *uc,
  71. struct kvm_inject_args *args);
  72. #define _KVM_INJECT_MULTI(cmd, intid, num, expect_failure) \
  73. kvm_inject_call(cmd, intid, num, -1 /* not used */, expect_failure)
  74. #define KVM_INJECT_MULTI(cmd, intid, num) \
  75. _KVM_INJECT_MULTI(cmd, intid, num, false)
  76. #define _KVM_INJECT(cmd, intid, expect_failure) \
  77. _KVM_INJECT_MULTI(cmd, intid, 1, expect_failure)
  78. #define KVM_INJECT(cmd, intid) \
  79. _KVM_INJECT_MULTI(cmd, intid, 1, false)
  80. #define KVM_ACTIVATE(cmd, intid) \
  81. kvm_inject_call(cmd, intid, 1, 1, false);
  82. struct kvm_inject_desc {
  83. kvm_inject_cmd cmd;
  84. /* can inject PPIs, PPIs, and/or SPIs. */
  85. bool sgi, ppi, spi;
  86. };
  87. static struct kvm_inject_desc inject_edge_fns[] = {
  88. /* sgi ppi spi */
  89. { KVM_INJECT_EDGE_IRQ_LINE, false, false, true },
  90. { KVM_INJECT_IRQFD, false, false, true },
  91. { KVM_WRITE_ISPENDR, true, false, true },
  92. { 0, },
  93. };
  94. static struct kvm_inject_desc inject_level_fns[] = {
  95. /* sgi ppi spi */
  96. { KVM_SET_IRQ_LINE_HIGH, false, true, true },
  97. { KVM_SET_LEVEL_INFO_HIGH, false, true, true },
  98. { KVM_INJECT_IRQFD, false, false, true },
  99. { KVM_WRITE_ISPENDR, false, true, true },
  100. { 0, },
  101. };
  102. static struct kvm_inject_desc set_active_fns[] = {
  103. /* sgi ppi spi */
  104. { KVM_WRITE_ISACTIVER, true, true, true },
  105. { 0, },
  106. };
  107. #define for_each_inject_fn(t, f) \
  108. for ((f) = (t); (f)->cmd; (f)++)
  109. #define for_each_supported_inject_fn(args, t, f) \
  110. for_each_inject_fn(t, f) \
  111. if ((args)->kvm_supports_irqfd || (f)->cmd != KVM_INJECT_IRQFD)
  112. #define for_each_supported_activate_fn(args, t, f) \
  113. for_each_supported_inject_fn((args), (t), (f))
  114. /* Shared between the guest main thread and the IRQ handlers. */
  115. volatile uint64_t irq_handled;
  116. volatile uint32_t irqnr_received[MAX_SPI + 1];
  117. static void reset_stats(void)
  118. {
  119. int i;
  120. irq_handled = 0;
  121. for (i = 0; i <= MAX_SPI; i++)
  122. irqnr_received[i] = 0;
  123. }
  124. static uint64_t gic_read_ap1r0(void)
  125. {
  126. uint64_t reg = read_sysreg_s(SYS_ICC_AP1R0_EL1);
  127. dsb(sy);
  128. return reg;
  129. }
  130. static void gic_write_ap1r0(uint64_t val)
  131. {
  132. write_sysreg_s(val, SYS_ICC_AP1R0_EL1);
  133. isb();
  134. }
  135. static void guest_set_irq_line(uint32_t intid, uint32_t level);
  136. static void guest_irq_generic_handler(bool eoi_split, bool level_sensitive)
  137. {
  138. uint32_t intid = gic_get_and_ack_irq();
  139. if (intid == IAR_SPURIOUS)
  140. return;
  141. GUEST_ASSERT(gic_irq_get_active(intid));
  142. if (!level_sensitive)
  143. GUEST_ASSERT(!gic_irq_get_pending(intid));
  144. if (level_sensitive)
  145. guest_set_irq_line(intid, 0);
  146. GUEST_ASSERT(intid < MAX_SPI);
  147. irqnr_received[intid] += 1;
  148. irq_handled += 1;
  149. gic_set_eoi(intid);
  150. GUEST_ASSERT_EQ(gic_read_ap1r0(), 0);
  151. if (eoi_split)
  152. gic_set_dir(intid);
  153. GUEST_ASSERT(!gic_irq_get_active(intid));
  154. GUEST_ASSERT(!gic_irq_get_pending(intid));
  155. }
  156. static void kvm_inject_call(kvm_inject_cmd cmd, uint32_t first_intid,
  157. uint32_t num, int level, bool expect_failure)
  158. {
  159. struct kvm_inject_args args = {
  160. .cmd = cmd,
  161. .first_intid = first_intid,
  162. .num = num,
  163. .level = level,
  164. .expect_failure = expect_failure,
  165. };
  166. GUEST_SYNC(&args);
  167. }
  168. #define GUEST_ASSERT_IAR_EMPTY() \
  169. do { \
  170. uint32_t _intid; \
  171. _intid = gic_get_and_ack_irq(); \
  172. GUEST_ASSERT(_intid == IAR_SPURIOUS); \
  173. } while (0)
  174. #define CAT_HELPER(a, b) a ## b
  175. #define CAT(a, b) CAT_HELPER(a, b)
  176. #define PREFIX guest_irq_handler_
  177. #define GUEST_IRQ_HANDLER_NAME(split, lev) CAT(PREFIX, CAT(split, lev))
  178. #define GENERATE_GUEST_IRQ_HANDLER(split, lev) \
  179. static void CAT(PREFIX, CAT(split, lev))(struct ex_regs *regs) \
  180. { \
  181. guest_irq_generic_handler(split, lev); \
  182. }
  183. GENERATE_GUEST_IRQ_HANDLER(0, 0);
  184. GENERATE_GUEST_IRQ_HANDLER(0, 1);
  185. GENERATE_GUEST_IRQ_HANDLER(1, 0);
  186. GENERATE_GUEST_IRQ_HANDLER(1, 1);
  187. static void (*guest_irq_handlers[2][2])(struct ex_regs *) = {
  188. {GUEST_IRQ_HANDLER_NAME(0, 0), GUEST_IRQ_HANDLER_NAME(0, 1),},
  189. {GUEST_IRQ_HANDLER_NAME(1, 0), GUEST_IRQ_HANDLER_NAME(1, 1),},
  190. };
  191. static void reset_priorities(struct test_args *args)
  192. {
  193. int i;
  194. for (i = 0; i < args->nr_irqs; i++)
  195. gic_set_priority(i, IRQ_DEFAULT_PRIO_REG);
  196. }
  197. static void guest_set_irq_line(uint32_t intid, uint32_t level)
  198. {
  199. kvm_inject_call(KVM_SET_IRQ_LINE, intid, 1, level, false);
  200. }
  201. static void test_inject_fail(struct test_args *args,
  202. uint32_t intid, kvm_inject_cmd cmd)
  203. {
  204. reset_stats();
  205. _KVM_INJECT(cmd, intid, true);
  206. /* no IRQ to handle on entry */
  207. GUEST_ASSERT_EQ(irq_handled, 0);
  208. GUEST_ASSERT_IAR_EMPTY();
  209. }
  210. static void guest_inject(struct test_args *args,
  211. uint32_t first_intid, uint32_t num,
  212. kvm_inject_cmd cmd)
  213. {
  214. uint32_t i;
  215. reset_stats();
  216. /* Cycle over all priorities to make things more interesting. */
  217. for (i = first_intid; i < num + first_intid; i++)
  218. gic_set_priority(i, (i % (KVM_NUM_PRIOS - 1)) << 3);
  219. asm volatile("msr daifset, #2" : : : "memory");
  220. KVM_INJECT_MULTI(cmd, first_intid, num);
  221. while (irq_handled < num) {
  222. wfi();
  223. local_irq_enable();
  224. isb(); /* handle IRQ */
  225. local_irq_disable();
  226. }
  227. local_irq_enable();
  228. GUEST_ASSERT_EQ(irq_handled, num);
  229. for (i = first_intid; i < num + first_intid; i++)
  230. GUEST_ASSERT_EQ(irqnr_received[i], 1);
  231. GUEST_ASSERT_IAR_EMPTY();
  232. reset_priorities(args);
  233. }
  234. /*
  235. * Restore the active state of multiple concurrent IRQs (given by
  236. * concurrent_irqs). This does what a live-migration would do on the
  237. * destination side assuming there are some active IRQs that were not
  238. * deactivated yet.
  239. */
  240. static void guest_restore_active(struct test_args *args,
  241. uint32_t first_intid, uint32_t num,
  242. kvm_inject_cmd cmd)
  243. {
  244. uint32_t prio, intid, ap1r;
  245. int i;
  246. /*
  247. * Set the priorities of the first (KVM_NUM_PRIOS - 1) IRQs
  248. * in descending order, so intid+1 can preempt intid.
  249. */
  250. for (i = 0, prio = (num - 1) * 8; i < num; i++, prio -= 8) {
  251. GUEST_ASSERT(prio >= 0);
  252. intid = i + first_intid;
  253. gic_set_priority(intid, prio);
  254. }
  255. /*
  256. * In a real migration, KVM would restore all GIC state before running
  257. * guest code.
  258. */
  259. for (i = 0; i < num; i++) {
  260. intid = i + first_intid;
  261. KVM_ACTIVATE(cmd, intid);
  262. ap1r = gic_read_ap1r0();
  263. ap1r |= 1U << i;
  264. gic_write_ap1r0(ap1r);
  265. }
  266. /* This is where the "migration" would occur. */
  267. /* finish handling the IRQs starting with the highest priority one. */
  268. for (i = 0; i < num; i++) {
  269. intid = num - i - 1 + first_intid;
  270. gic_set_eoi(intid);
  271. if (args->eoi_split)
  272. gic_set_dir(intid);
  273. }
  274. for (i = 0; i < num; i++)
  275. GUEST_ASSERT(!gic_irq_get_active(i + first_intid));
  276. GUEST_ASSERT_EQ(gic_read_ap1r0(), 0);
  277. GUEST_ASSERT_IAR_EMPTY();
  278. }
  279. /*
  280. * Polls the IAR until it's not a spurious interrupt.
  281. *
  282. * This function should only be used in test_inject_preemption (with IRQs
  283. * masked).
  284. */
  285. static uint32_t wait_for_and_activate_irq(void)
  286. {
  287. uint32_t intid;
  288. do {
  289. asm volatile("wfi" : : : "memory");
  290. intid = gic_get_and_ack_irq();
  291. } while (intid == IAR_SPURIOUS);
  292. return intid;
  293. }
  294. /*
  295. * Inject multiple concurrent IRQs (num IRQs starting at first_intid) and
  296. * handle them without handling the actual exceptions. This is done by masking
  297. * interrupts for the whole test.
  298. */
  299. static void test_inject_preemption(struct test_args *args,
  300. uint32_t first_intid, int num,
  301. const unsigned long *exclude,
  302. kvm_inject_cmd cmd)
  303. {
  304. uint32_t intid, prio, step = KVM_PRIO_STEPS;
  305. int i;
  306. /* Set the priorities of the first (KVM_NUM_PRIOS - 1) IRQs
  307. * in descending order, so intid+1 can preempt intid.
  308. */
  309. for (i = 0, prio = (num - 1) * step; i < num; i++, prio -= step) {
  310. GUEST_ASSERT(prio >= 0);
  311. intid = i + first_intid;
  312. gic_set_priority(intid, prio);
  313. }
  314. local_irq_disable();
  315. for (i = 0; i < num; i++) {
  316. uint32_t tmp;
  317. intid = i + first_intid;
  318. if (exclude && test_bit(i, exclude))
  319. continue;
  320. KVM_INJECT(cmd, intid);
  321. /* Each successive IRQ will preempt the previous one. */
  322. tmp = wait_for_and_activate_irq();
  323. GUEST_ASSERT_EQ(tmp, intid);
  324. if (args->level_sensitive)
  325. guest_set_irq_line(intid, 0);
  326. }
  327. /* finish handling the IRQs starting with the highest priority one. */
  328. for (i = 0; i < num; i++) {
  329. intid = num - i - 1 + first_intid;
  330. if (exclude && test_bit(intid - first_intid, exclude))
  331. continue;
  332. gic_set_eoi(intid);
  333. }
  334. if (args->eoi_split) {
  335. for (i = 0; i < num; i++) {
  336. intid = i + first_intid;
  337. if (exclude && test_bit(i, exclude))
  338. continue;
  339. if (args->eoi_split)
  340. gic_set_dir(intid);
  341. }
  342. }
  343. local_irq_enable();
  344. for (i = 0; i < num; i++) {
  345. if (exclude && test_bit(i, exclude))
  346. continue;
  347. GUEST_ASSERT(!gic_irq_get_active(i + first_intid));
  348. }
  349. GUEST_ASSERT_EQ(gic_read_ap1r0(), 0);
  350. GUEST_ASSERT_IAR_EMPTY();
  351. reset_priorities(args);
  352. }
  353. static void test_injection(struct test_args *args, struct kvm_inject_desc *f)
  354. {
  355. uint32_t nr_irqs = args->nr_irqs;
  356. if (f->sgi) {
  357. guest_inject(args, MIN_SGI, 1, f->cmd);
  358. guest_inject(args, 0, 16, f->cmd);
  359. }
  360. if (f->ppi)
  361. guest_inject(args, MIN_PPI, 1, f->cmd);
  362. if (f->spi) {
  363. guest_inject(args, MIN_SPI, 1, f->cmd);
  364. guest_inject(args, nr_irqs - 1, 1, f->cmd);
  365. guest_inject(args, MIN_SPI, nr_irqs - MIN_SPI, f->cmd);
  366. }
  367. }
  368. static void test_injection_failure(struct test_args *args,
  369. struct kvm_inject_desc *f)
  370. {
  371. uint32_t bad_intid[] = { args->nr_irqs, 1020, 1024, 1120, 5120, ~0U, };
  372. int i;
  373. for (i = 0; i < ARRAY_SIZE(bad_intid); i++)
  374. test_inject_fail(args, bad_intid[i], f->cmd);
  375. }
  376. static void test_preemption(struct test_args *args, struct kvm_inject_desc *f)
  377. {
  378. /* Timer PPIs cannot be injected from userspace */
  379. static const unsigned long ppi_exclude = (BIT(27 - MIN_PPI) |
  380. BIT(30 - MIN_PPI) |
  381. BIT(28 - MIN_PPI) |
  382. BIT(26 - MIN_PPI));
  383. if (f->sgi)
  384. test_inject_preemption(args, MIN_SGI, 16, NULL, f->cmd);
  385. if (f->ppi)
  386. test_inject_preemption(args, MIN_PPI, 16, &ppi_exclude, f->cmd);
  387. if (f->spi)
  388. test_inject_preemption(args, MIN_SPI, 31, NULL, f->cmd);
  389. }
  390. static void test_restore_active(struct test_args *args, struct kvm_inject_desc *f)
  391. {
  392. if (f->sgi)
  393. guest_restore_active(args, MIN_SGI, 16, f->cmd);
  394. if (f->ppi)
  395. guest_restore_active(args, MIN_PPI, 16, f->cmd);
  396. if (f->spi)
  397. guest_restore_active(args, MIN_SPI, 31, f->cmd);
  398. }
  399. static void guest_code(struct test_args *args)
  400. {
  401. uint32_t i, nr_irqs = args->nr_irqs;
  402. bool level_sensitive = args->level_sensitive;
  403. struct kvm_inject_desc *f, *inject_fns;
  404. gic_init(GIC_V3, 1);
  405. for (i = MIN_SPI; i < nr_irqs; i++)
  406. gic_irq_set_config(i, !level_sensitive);
  407. for (i = 0; i < nr_irqs; i++)
  408. gic_irq_enable(i);
  409. gic_set_eoi_split(args->eoi_split);
  410. reset_priorities(args);
  411. gic_set_priority_mask(CPU_PRIO_MASK);
  412. inject_fns = level_sensitive ? inject_level_fns
  413. : inject_edge_fns;
  414. local_irq_enable();
  415. /* Start the tests. */
  416. for_each_supported_inject_fn(args, inject_fns, f) {
  417. test_injection(args, f);
  418. test_preemption(args, f);
  419. test_injection_failure(args, f);
  420. }
  421. /*
  422. * Restore the active state of IRQs. This would happen when live
  423. * migrating IRQs in the middle of being handled.
  424. */
  425. for_each_supported_activate_fn(args, set_active_fns, f)
  426. test_restore_active(args, f);
  427. GUEST_DONE();
  428. }
  429. static void kvm_irq_line_check(struct kvm_vm *vm, uint32_t intid, int level,
  430. struct test_args *test_args, bool expect_failure)
  431. {
  432. int ret;
  433. if (!expect_failure) {
  434. kvm_arm_irq_line(vm, intid, level);
  435. } else {
  436. /* The interface doesn't allow larger intid's. */
  437. if (intid > KVM_ARM_IRQ_NUM_MASK)
  438. return;
  439. ret = _kvm_arm_irq_line(vm, intid, level);
  440. TEST_ASSERT(ret != 0 && errno == EINVAL,
  441. "Bad intid %i did not cause KVM_IRQ_LINE "
  442. "error: rc: %i errno: %i", intid, ret, errno);
  443. }
  444. }
  445. void kvm_irq_set_level_info_check(int gic_fd, uint32_t intid, int level,
  446. bool expect_failure)
  447. {
  448. if (!expect_failure) {
  449. kvm_irq_set_level_info(gic_fd, intid, level);
  450. } else {
  451. int ret = _kvm_irq_set_level_info(gic_fd, intid, level);
  452. /*
  453. * The kernel silently fails for invalid SPIs and SGIs (which
  454. * are not level-sensitive). It only checks for intid to not
  455. * spill over 1U << 10 (the max reserved SPI). Also, callers
  456. * are supposed to mask the intid with 0x3ff (1023).
  457. */
  458. if (intid > VGIC_MAX_RESERVED)
  459. TEST_ASSERT(ret != 0 && errno == EINVAL,
  460. "Bad intid %i did not cause VGIC_GRP_LEVEL_INFO "
  461. "error: rc: %i errno: %i", intid, ret, errno);
  462. else
  463. TEST_ASSERT(!ret, "KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO "
  464. "for intid %i failed, rc: %i errno: %i",
  465. intid, ret, errno);
  466. }
  467. }
  468. static void kvm_set_gsi_routing_irqchip_check(struct kvm_vm *vm,
  469. uint32_t intid, uint32_t num, uint32_t kvm_max_routes,
  470. bool expect_failure)
  471. {
  472. struct kvm_irq_routing *routing;
  473. int ret;
  474. uint64_t i;
  475. assert(num <= kvm_max_routes && kvm_max_routes <= KVM_MAX_IRQ_ROUTES);
  476. routing = kvm_gsi_routing_create();
  477. for (i = intid; i < (uint64_t)intid + num; i++)
  478. kvm_gsi_routing_irqchip_add(routing, i - MIN_SPI, i - MIN_SPI);
  479. if (!expect_failure) {
  480. kvm_gsi_routing_write(vm, routing);
  481. } else {
  482. ret = _kvm_gsi_routing_write(vm, routing);
  483. /* The kernel only checks e->irqchip.pin >= KVM_IRQCHIP_NUM_PINS */
  484. if (((uint64_t)intid + num - 1 - MIN_SPI) >= KVM_IRQCHIP_NUM_PINS)
  485. TEST_ASSERT(ret != 0 && errno == EINVAL,
  486. "Bad intid %u did not cause KVM_SET_GSI_ROUTING "
  487. "error: rc: %i errno: %i", intid, ret, errno);
  488. else
  489. TEST_ASSERT(ret == 0, "KVM_SET_GSI_ROUTING "
  490. "for intid %i failed, rc: %i errno: %i",
  491. intid, ret, errno);
  492. }
  493. }
  494. static void kvm_irq_write_ispendr_check(int gic_fd, uint32_t intid,
  495. struct kvm_vcpu *vcpu,
  496. bool expect_failure)
  497. {
  498. /*
  499. * Ignore this when expecting failure as invalid intids will lead to
  500. * either trying to inject SGIs when we configured the test to be
  501. * level_sensitive (or the reverse), or inject large intids which
  502. * will lead to writing above the ISPENDR register space (and we
  503. * don't want to do that either).
  504. */
  505. if (!expect_failure)
  506. kvm_irq_write_ispendr(gic_fd, intid, vcpu);
  507. }
  508. static void kvm_routing_and_irqfd_check(struct kvm_vm *vm,
  509. uint32_t intid, uint32_t num, uint32_t kvm_max_routes,
  510. bool expect_failure)
  511. {
  512. int fd[MAX_SPI];
  513. uint64_t val;
  514. int ret, f;
  515. uint64_t i;
  516. /*
  517. * There is no way to try injecting an SGI or PPI as the interface
  518. * starts counting from the first SPI (above the private ones), so just
  519. * exit.
  520. */
  521. if (INTID_IS_SGI(intid) || INTID_IS_PPI(intid))
  522. return;
  523. kvm_set_gsi_routing_irqchip_check(vm, intid, num,
  524. kvm_max_routes, expect_failure);
  525. /*
  526. * If expect_failure, then just to inject anyway. These
  527. * will silently fail. And in any case, the guest will check
  528. * that no actual interrupt was injected for those cases.
  529. */
  530. for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++)
  531. fd[f] = kvm_new_eventfd();
  532. for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) {
  533. assert(i <= (uint64_t)UINT_MAX);
  534. kvm_assign_irqfd(vm, i - MIN_SPI, fd[f]);
  535. }
  536. for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++) {
  537. val = 1;
  538. ret = write(fd[f], &val, sizeof(uint64_t));
  539. TEST_ASSERT(ret == sizeof(uint64_t),
  540. __KVM_SYSCALL_ERROR("write()", ret));
  541. }
  542. for (f = 0, i = intid; i < (uint64_t)intid + num; i++, f++)
  543. kvm_close(fd[f]);
  544. }
  545. /* handles the valid case: intid=0xffffffff num=1 */
  546. #define for_each_intid(first, num, tmp, i) \
  547. for ((tmp) = (i) = (first); \
  548. (tmp) < (uint64_t)(first) + (uint64_t)(num); \
  549. (tmp)++, (i)++)
  550. static void run_guest_cmd(struct kvm_vcpu *vcpu, int gic_fd,
  551. struct kvm_inject_args *inject_args,
  552. struct test_args *test_args)
  553. {
  554. kvm_inject_cmd cmd = inject_args->cmd;
  555. uint32_t intid = inject_args->first_intid;
  556. uint32_t num = inject_args->num;
  557. int level = inject_args->level;
  558. bool expect_failure = inject_args->expect_failure;
  559. struct kvm_vm *vm = vcpu->vm;
  560. uint64_t tmp;
  561. uint32_t i;
  562. /* handles the valid case: intid=0xffffffff num=1 */
  563. assert(intid < UINT_MAX - num || num == 1);
  564. switch (cmd) {
  565. case KVM_INJECT_EDGE_IRQ_LINE:
  566. for_each_intid(intid, num, tmp, i)
  567. kvm_irq_line_check(vm, i, 1, test_args,
  568. expect_failure);
  569. for_each_intid(intid, num, tmp, i)
  570. kvm_irq_line_check(vm, i, 0, test_args,
  571. expect_failure);
  572. break;
  573. case KVM_SET_IRQ_LINE:
  574. for_each_intid(intid, num, tmp, i)
  575. kvm_irq_line_check(vm, i, level, test_args,
  576. expect_failure);
  577. break;
  578. case KVM_SET_IRQ_LINE_HIGH:
  579. for_each_intid(intid, num, tmp, i)
  580. kvm_irq_line_check(vm, i, 1, test_args,
  581. expect_failure);
  582. break;
  583. case KVM_SET_LEVEL_INFO_HIGH:
  584. for_each_intid(intid, num, tmp, i)
  585. kvm_irq_set_level_info_check(gic_fd, i, 1,
  586. expect_failure);
  587. break;
  588. case KVM_INJECT_IRQFD:
  589. kvm_routing_and_irqfd_check(vm, intid, num,
  590. test_args->kvm_max_routes,
  591. expect_failure);
  592. break;
  593. case KVM_WRITE_ISPENDR:
  594. for (i = intid; i < intid + num; i++)
  595. kvm_irq_write_ispendr_check(gic_fd, i, vcpu,
  596. expect_failure);
  597. break;
  598. case KVM_WRITE_ISACTIVER:
  599. for (i = intid; i < intid + num; i++)
  600. kvm_irq_write_isactiver(gic_fd, i, vcpu);
  601. break;
  602. default:
  603. break;
  604. }
  605. }
  606. static void kvm_inject_get_call(struct kvm_vm *vm, struct ucall *uc,
  607. struct kvm_inject_args *args)
  608. {
  609. struct kvm_inject_args *kvm_args_hva;
  610. vm_vaddr_t kvm_args_gva;
  611. kvm_args_gva = uc->args[1];
  612. kvm_args_hva = (struct kvm_inject_args *)addr_gva2hva(vm, kvm_args_gva);
  613. memcpy(args, kvm_args_hva, sizeof(struct kvm_inject_args));
  614. }
  615. static void print_args(struct test_args *args)
  616. {
  617. printf("nr-irqs=%d level-sensitive=%d eoi-split=%d\n",
  618. args->nr_irqs, args->level_sensitive,
  619. args->eoi_split);
  620. }
  621. static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)
  622. {
  623. struct ucall uc;
  624. int gic_fd;
  625. struct kvm_vcpu *vcpu;
  626. struct kvm_vm *vm;
  627. struct kvm_inject_args inject_args;
  628. vm_vaddr_t args_gva;
  629. struct test_args args = {
  630. .nr_irqs = nr_irqs,
  631. .level_sensitive = level_sensitive,
  632. .eoi_split = eoi_split,
  633. .kvm_max_routes = kvm_check_cap(KVM_CAP_IRQ_ROUTING),
  634. .kvm_supports_irqfd = kvm_check_cap(KVM_CAP_IRQFD),
  635. };
  636. print_args(&args);
  637. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  638. vm_init_descriptor_tables(vm);
  639. vcpu_init_descriptor_tables(vcpu);
  640. /* Setup the guest args page (so it gets the args). */
  641. args_gva = vm_vaddr_alloc_page(vm);
  642. memcpy(addr_gva2hva(vm, args_gva), &args, sizeof(args));
  643. vcpu_args_set(vcpu, 1, args_gva);
  644. gic_fd = vgic_v3_setup(vm, 1, nr_irqs);
  645. vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT,
  646. guest_irq_handlers[args.eoi_split][args.level_sensitive]);
  647. while (1) {
  648. vcpu_run(vcpu);
  649. switch (get_ucall(vcpu, &uc)) {
  650. case UCALL_SYNC:
  651. kvm_inject_get_call(vm, &uc, &inject_args);
  652. run_guest_cmd(vcpu, gic_fd, &inject_args, &args);
  653. break;
  654. case UCALL_ABORT:
  655. REPORT_GUEST_ASSERT(uc);
  656. break;
  657. case UCALL_DONE:
  658. goto done;
  659. default:
  660. TEST_FAIL("Unknown ucall %lu", uc.cmd);
  661. }
  662. }
  663. done:
  664. close(gic_fd);
  665. kvm_vm_free(vm);
  666. }
  667. static void guest_code_asym_dir(struct test_args *args, int cpuid)
  668. {
  669. gic_init(GIC_V3, 2);
  670. gic_set_eoi_split(1);
  671. gic_set_priority_mask(CPU_PRIO_MASK);
  672. if (cpuid == 0) {
  673. uint32_t intid;
  674. local_irq_disable();
  675. gic_set_priority(MIN_PPI, IRQ_DEFAULT_PRIO);
  676. gic_irq_enable(MIN_SPI);
  677. gic_irq_set_pending(MIN_SPI);
  678. intid = wait_for_and_activate_irq();
  679. GUEST_ASSERT_EQ(intid, MIN_SPI);
  680. gic_set_eoi(intid);
  681. isb();
  682. WRITE_ONCE(args->shared_data, MIN_SPI);
  683. dsb(ishst);
  684. do {
  685. dsb(ishld);
  686. } while (READ_ONCE(args->shared_data) == MIN_SPI);
  687. GUEST_ASSERT(!gic_irq_get_active(MIN_SPI));
  688. } else {
  689. do {
  690. dsb(ishld);
  691. } while (READ_ONCE(args->shared_data) != MIN_SPI);
  692. gic_set_dir(MIN_SPI);
  693. isb();
  694. WRITE_ONCE(args->shared_data, 0);
  695. dsb(ishst);
  696. }
  697. GUEST_DONE();
  698. }
  699. static void guest_code_group_en(struct test_args *args, int cpuid)
  700. {
  701. uint32_t intid;
  702. gic_init(GIC_V3, 2);
  703. gic_set_eoi_split(0);
  704. gic_set_priority_mask(CPU_PRIO_MASK);
  705. /* SGI0 is G0, which is disabled */
  706. gic_irq_set_group(0, 0);
  707. /* Configure all SGIs with decreasing priority */
  708. for (intid = 0; intid < MIN_PPI; intid++) {
  709. gic_set_priority(intid, (intid + 1) * 8);
  710. gic_irq_enable(intid);
  711. gic_irq_set_pending(intid);
  712. }
  713. /* Ack and EOI all G1 interrupts */
  714. for (int i = 1; i < MIN_PPI; i++) {
  715. intid = wait_for_and_activate_irq();
  716. GUEST_ASSERT(intid < MIN_PPI);
  717. gic_set_eoi(intid);
  718. isb();
  719. }
  720. /*
  721. * Check that SGI0 is still pending, inactive, and that we cannot
  722. * ack anything.
  723. */
  724. GUEST_ASSERT(gic_irq_get_pending(0));
  725. GUEST_ASSERT(!gic_irq_get_active(0));
  726. GUEST_ASSERT_IAR_EMPTY();
  727. GUEST_ASSERT(read_sysreg_s(SYS_ICC_IAR0_EL1) == IAR_SPURIOUS);
  728. /* Open the G0 gates, and verify we can ack SGI0 */
  729. write_sysreg_s(1, SYS_ICC_IGRPEN0_EL1);
  730. isb();
  731. do {
  732. intid = read_sysreg_s(SYS_ICC_IAR0_EL1);
  733. } while (intid == IAR_SPURIOUS);
  734. GUEST_ASSERT(intid == 0);
  735. GUEST_DONE();
  736. }
  737. static void guest_code_timer_spi(struct test_args *args, int cpuid)
  738. {
  739. uint32_t intid;
  740. u64 val;
  741. gic_init(GIC_V3, 2);
  742. gic_set_eoi_split(1);
  743. gic_set_priority_mask(CPU_PRIO_MASK);
  744. /* Add a pending SPI so that KVM starts trapping DIR */
  745. gic_set_priority(MIN_SPI + cpuid, IRQ_DEFAULT_PRIO);
  746. gic_irq_set_pending(MIN_SPI + cpuid);
  747. /* Configure the timer with a higher priority, make it pending */
  748. gic_set_priority(27, IRQ_DEFAULT_PRIO - 8);
  749. isb();
  750. val = read_sysreg(cntvct_el0);
  751. write_sysreg(val, cntv_cval_el0);
  752. write_sysreg(1, cntv_ctl_el0);
  753. isb();
  754. GUEST_ASSERT(gic_irq_get_pending(27));
  755. /* Enable both interrupts */
  756. gic_irq_enable(MIN_SPI + cpuid);
  757. gic_irq_enable(27);
  758. /* The timer must fire */
  759. intid = wait_for_and_activate_irq();
  760. GUEST_ASSERT(intid == 27);
  761. /* Check that we can deassert it */
  762. write_sysreg(0, cntv_ctl_el0);
  763. isb();
  764. GUEST_ASSERT(!gic_irq_get_pending(27));
  765. /*
  766. * Priority drop, deactivation -- we expect that the host
  767. * deactivation will have been effective
  768. */
  769. gic_set_eoi(27);
  770. gic_set_dir(27);
  771. GUEST_ASSERT(!gic_irq_get_active(27));
  772. /* Do it one more time */
  773. isb();
  774. val = read_sysreg(cntvct_el0);
  775. write_sysreg(val, cntv_cval_el0);
  776. write_sysreg(1, cntv_ctl_el0);
  777. isb();
  778. GUEST_ASSERT(gic_irq_get_pending(27));
  779. /* The timer must fire again */
  780. intid = wait_for_and_activate_irq();
  781. GUEST_ASSERT(intid == 27);
  782. GUEST_DONE();
  783. }
  784. static void *test_vcpu_run(void *arg)
  785. {
  786. struct kvm_vcpu *vcpu = arg;
  787. struct ucall uc;
  788. while (1) {
  789. vcpu_run(vcpu);
  790. switch (get_ucall(vcpu, &uc)) {
  791. case UCALL_ABORT:
  792. REPORT_GUEST_ASSERT(uc);
  793. break;
  794. case UCALL_DONE:
  795. return NULL;
  796. default:
  797. TEST_FAIL("Unknown ucall %lu", uc.cmd);
  798. }
  799. }
  800. return NULL;
  801. }
  802. static void test_vgic_two_cpus(void *gcode)
  803. {
  804. pthread_t thr[2];
  805. struct kvm_vcpu *vcpus[2];
  806. struct test_args args = {};
  807. struct kvm_vm *vm;
  808. vm_vaddr_t args_gva;
  809. int gic_fd, ret;
  810. vm = vm_create_with_vcpus(2, gcode, vcpus);
  811. vm_init_descriptor_tables(vm);
  812. vcpu_init_descriptor_tables(vcpus[0]);
  813. vcpu_init_descriptor_tables(vcpus[1]);
  814. /* Setup the guest args page (so it gets the args). */
  815. args_gva = vm_vaddr_alloc_page(vm);
  816. memcpy(addr_gva2hva(vm, args_gva), &args, sizeof(args));
  817. vcpu_args_set(vcpus[0], 2, args_gva, 0);
  818. vcpu_args_set(vcpus[1], 2, args_gva, 1);
  819. gic_fd = vgic_v3_setup(vm, 2, 64);
  820. ret = pthread_create(&thr[0], NULL, test_vcpu_run, vcpus[0]);
  821. if (ret)
  822. TEST_FAIL("Can't create thread for vcpu 0 (%d)\n", ret);
  823. ret = pthread_create(&thr[1], NULL, test_vcpu_run, vcpus[1]);
  824. if (ret)
  825. TEST_FAIL("Can't create thread for vcpu 1 (%d)\n", ret);
  826. pthread_join(thr[0], NULL);
  827. pthread_join(thr[1], NULL);
  828. close(gic_fd);
  829. kvm_vm_free(vm);
  830. }
  831. static void help(const char *name)
  832. {
  833. printf(
  834. "\n"
  835. "usage: %s [-n num_irqs] [-e eoi_split] [-l level_sensitive]\n", name);
  836. printf(" -n: specify number of IRQs to setup the vgic with. "
  837. "It has to be a multiple of 32 and between 64 and 1024.\n");
  838. printf(" -e: if 1 then EOI is split into a write to DIR on top "
  839. "of writing EOI.\n");
  840. printf(" -l: specify whether the IRQs are level-sensitive (1) or not (0).");
  841. puts("");
  842. exit(1);
  843. }
  844. int main(int argc, char **argv)
  845. {
  846. uint32_t nr_irqs = 64;
  847. bool default_args = true;
  848. bool level_sensitive = false;
  849. int opt;
  850. bool eoi_split = false;
  851. TEST_REQUIRE(kvm_supports_vgic_v3());
  852. test_disable_default_vgic();
  853. while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
  854. switch (opt) {
  855. case 'n':
  856. nr_irqs = atoi_non_negative("Number of IRQs", optarg);
  857. if (nr_irqs > 1024 || nr_irqs % 32)
  858. help(argv[0]);
  859. break;
  860. case 'e':
  861. eoi_split = (bool)atoi_paranoid(optarg);
  862. default_args = false;
  863. break;
  864. case 'l':
  865. level_sensitive = (bool)atoi_paranoid(optarg);
  866. default_args = false;
  867. break;
  868. case 'h':
  869. default:
  870. help(argv[0]);
  871. break;
  872. }
  873. }
  874. /*
  875. * If the user just specified nr_irqs and/or gic_version, then run all
  876. * combinations.
  877. */
  878. if (default_args) {
  879. test_vgic(nr_irqs, false /* level */, false /* eoi_split */);
  880. test_vgic(nr_irqs, false /* level */, true /* eoi_split */);
  881. test_vgic(nr_irqs, true /* level */, false /* eoi_split */);
  882. test_vgic(nr_irqs, true /* level */, true /* eoi_split */);
  883. test_vgic_two_cpus(guest_code_asym_dir);
  884. test_vgic_two_cpus(guest_code_group_en);
  885. test_vgic_two_cpus(guest_code_timer_spi);
  886. } else {
  887. test_vgic(nr_irqs, level_sensitive, eoi_split);
  888. }
  889. return 0;
  890. }