vgic_init.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * vgic init sequence tests
  4. *
  5. * Copyright (C) 2020, Red Hat, Inc.
  6. */
  7. #include <linux/kernel.h>
  8. #include <sys/syscall.h>
  9. #include <asm/kvm.h>
  10. #include <asm/kvm_para.h>
  11. #include "test_util.h"
  12. #include "kvm_util.h"
  13. #include "processor.h"
  14. #include "vgic.h"
  15. #define NR_VCPUS 4
  16. #define REG_OFFSET(vcpu, offset) (((uint64_t)vcpu << 32) | offset)
  17. #define GICR_TYPER 0x8
  18. #define VGIC_DEV_IS_V2(_d) ((_d) == KVM_DEV_TYPE_ARM_VGIC_V2)
  19. #define VGIC_DEV_IS_V3(_d) ((_d) == KVM_DEV_TYPE_ARM_VGIC_V3)
  20. struct vm_gic {
  21. struct kvm_vm *vm;
  22. int gic_fd;
  23. uint32_t gic_dev_type;
  24. };
  25. static uint64_t max_phys_size;
  26. /*
  27. * Helpers to access a redistributor register and verify the ioctl() failed or
  28. * succeeded as expected, and provided the correct value on success.
  29. */
  30. static void v3_redist_reg_get_errno(int gicv3_fd, int vcpu, int offset,
  31. int want, const char *msg)
  32. {
  33. uint32_t ignored_val;
  34. int ret = __kvm_device_attr_get(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
  35. REG_OFFSET(vcpu, offset), &ignored_val);
  36. TEST_ASSERT(ret && errno == want, "%s; want errno = %d", msg, want);
  37. }
  38. static void v3_redist_reg_get(int gicv3_fd, int vcpu, int offset, uint32_t want,
  39. const char *msg)
  40. {
  41. uint32_t val;
  42. kvm_device_attr_get(gicv3_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
  43. REG_OFFSET(vcpu, offset), &val);
  44. TEST_ASSERT(val == want, "%s; want '0x%x', got '0x%x'", msg, want, val);
  45. }
  46. /* dummy guest code */
  47. static void guest_code(void)
  48. {
  49. GUEST_SYNC(0);
  50. GUEST_SYNC(1);
  51. GUEST_SYNC(2);
  52. GUEST_DONE();
  53. }
  54. /* we don't want to assert on run execution, hence that helper */
  55. static int run_vcpu(struct kvm_vcpu *vcpu)
  56. {
  57. return __vcpu_run(vcpu) ? -errno : 0;
  58. }
  59. static struct vm_gic vm_gic_create_with_vcpus(uint32_t gic_dev_type,
  60. uint32_t nr_vcpus,
  61. struct kvm_vcpu *vcpus[])
  62. {
  63. struct vm_gic v;
  64. v.gic_dev_type = gic_dev_type;
  65. v.vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus);
  66. v.gic_fd = kvm_create_device(v.vm, gic_dev_type);
  67. return v;
  68. }
  69. static struct vm_gic vm_gic_create_barebones(uint32_t gic_dev_type)
  70. {
  71. struct vm_gic v;
  72. v.gic_dev_type = gic_dev_type;
  73. v.vm = vm_create_barebones();
  74. v.gic_fd = kvm_create_device(v.vm, gic_dev_type);
  75. return v;
  76. }
  77. static void vm_gic_destroy(struct vm_gic *v)
  78. {
  79. close(v->gic_fd);
  80. kvm_vm_free(v->vm);
  81. }
  82. struct vgic_region_attr {
  83. uint64_t attr;
  84. uint64_t size;
  85. uint64_t alignment;
  86. };
  87. struct vgic_region_attr gic_v3_dist_region = {
  88. .attr = KVM_VGIC_V3_ADDR_TYPE_DIST,
  89. .size = 0x10000,
  90. .alignment = 0x10000,
  91. };
  92. struct vgic_region_attr gic_v3_redist_region = {
  93. .attr = KVM_VGIC_V3_ADDR_TYPE_REDIST,
  94. .size = NR_VCPUS * 0x20000,
  95. .alignment = 0x10000,
  96. };
  97. struct vgic_region_attr gic_v2_dist_region = {
  98. .attr = KVM_VGIC_V2_ADDR_TYPE_DIST,
  99. .size = 0x1000,
  100. .alignment = 0x1000,
  101. };
  102. struct vgic_region_attr gic_v2_cpu_region = {
  103. .attr = KVM_VGIC_V2_ADDR_TYPE_CPU,
  104. .size = 0x2000,
  105. .alignment = 0x1000,
  106. };
  107. /**
  108. * Helper routine that performs KVM device tests in general. Eventually the
  109. * ARM_VGIC (GICv2 or GICv3) device gets created with an overlapping
  110. * DIST/REDIST (or DIST/CPUIF for GICv2). Assumption is 4 vcpus are going to be
  111. * used hence the overlap. In the case of GICv3, A RDIST region is set at @0x0
  112. * and a DIST region is set @0x70000. The GICv2 case sets a CPUIF @0x0 and a
  113. * DIST region @0x1000.
  114. */
  115. static void subtest_dist_rdist(struct vm_gic *v)
  116. {
  117. int ret;
  118. uint64_t addr;
  119. struct vgic_region_attr rdist; /* CPU interface in GICv2*/
  120. struct vgic_region_attr dist;
  121. rdist = VGIC_DEV_IS_V3(v->gic_dev_type) ? gic_v3_redist_region
  122. : gic_v2_cpu_region;
  123. dist = VGIC_DEV_IS_V3(v->gic_dev_type) ? gic_v3_dist_region
  124. : gic_v2_dist_region;
  125. /* Check existing group/attributes */
  126. kvm_has_device_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, dist.attr);
  127. kvm_has_device_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, rdist.attr);
  128. /* check non existing attribute */
  129. ret = __kvm_has_device_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, -1);
  130. TEST_ASSERT(ret && errno == ENXIO, "attribute not supported");
  131. /* misaligned DIST and REDIST address settings */
  132. addr = dist.alignment / 0x10;
  133. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  134. dist.attr, &addr);
  135. TEST_ASSERT(ret && errno == EINVAL, "GIC dist base not aligned");
  136. addr = rdist.alignment / 0x10;
  137. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  138. rdist.attr, &addr);
  139. TEST_ASSERT(ret && errno == EINVAL, "GIC redist/cpu base not aligned");
  140. /* out of range address */
  141. addr = max_phys_size;
  142. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  143. dist.attr, &addr);
  144. TEST_ASSERT(ret && errno == E2BIG, "dist address beyond IPA limit");
  145. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  146. rdist.attr, &addr);
  147. TEST_ASSERT(ret && errno == E2BIG, "redist address beyond IPA limit");
  148. /* Space for half a rdist (a rdist is: 2 * rdist.alignment). */
  149. addr = max_phys_size - dist.alignment;
  150. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  151. rdist.attr, &addr);
  152. TEST_ASSERT(ret && errno == E2BIG,
  153. "half of the redist is beyond IPA limit");
  154. /* set REDIST base address @0x0*/
  155. addr = 0x00000;
  156. kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  157. rdist.attr, &addr);
  158. /* Attempt to create a second legacy redistributor region */
  159. addr = 0xE0000;
  160. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  161. rdist.attr, &addr);
  162. TEST_ASSERT(ret && errno == EEXIST, "GIC redist base set again");
  163. ret = __kvm_has_device_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  164. KVM_VGIC_V3_ADDR_TYPE_REDIST);
  165. if (!ret) {
  166. /* Attempt to mix legacy and new redistributor regions */
  167. addr = REDIST_REGION_ATTR_ADDR(NR_VCPUS, 0x100000, 0, 0);
  168. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  169. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  170. TEST_ASSERT(ret && errno == EINVAL,
  171. "attempt to mix GICv3 REDIST and REDIST_REGION");
  172. }
  173. /*
  174. * Set overlapping DIST / REDIST, cannot be detected here. Will be detected
  175. * on first vcpu run instead.
  176. */
  177. addr = rdist.size - rdist.alignment;
  178. kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  179. dist.attr, &addr);
  180. }
  181. /* Test the new REDIST region API */
  182. static void subtest_v3_redist_regions(struct vm_gic *v)
  183. {
  184. uint64_t addr, expected_addr;
  185. int ret;
  186. ret = __kvm_has_device_attr(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  187. KVM_VGIC_V3_ADDR_TYPE_REDIST);
  188. TEST_ASSERT(!ret, "Multiple redist regions advertised");
  189. addr = REDIST_REGION_ATTR_ADDR(NR_VCPUS, 0x100000, 2, 0);
  190. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  191. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  192. TEST_ASSERT(ret && errno == EINVAL, "redist region attr value with flags != 0");
  193. addr = REDIST_REGION_ATTR_ADDR(0, 0x100000, 0, 0);
  194. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  195. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  196. TEST_ASSERT(ret && errno == EINVAL, "redist region attr value with count== 0");
  197. addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 1);
  198. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  199. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  200. TEST_ASSERT(ret && errno == EINVAL,
  201. "attempt to register the first rdist region with index != 0");
  202. addr = REDIST_REGION_ATTR_ADDR(2, 0x201000, 0, 1);
  203. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  204. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  205. TEST_ASSERT(ret && errno == EINVAL, "rdist region with misaligned address");
  206. addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 0);
  207. kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  208. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  209. addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 1);
  210. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  211. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  212. TEST_ASSERT(ret && errno == EINVAL, "register an rdist region with already used index");
  213. addr = REDIST_REGION_ATTR_ADDR(1, 0x210000, 0, 2);
  214. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  215. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  216. TEST_ASSERT(ret && errno == EINVAL,
  217. "register an rdist region overlapping with another one");
  218. addr = REDIST_REGION_ATTR_ADDR(1, 0x240000, 0, 2);
  219. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  220. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  221. TEST_ASSERT(ret && errno == EINVAL, "register redist region with index not +1");
  222. addr = REDIST_REGION_ATTR_ADDR(1, 0x240000, 0, 1);
  223. kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  224. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  225. addr = REDIST_REGION_ATTR_ADDR(1, max_phys_size, 0, 2);
  226. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  227. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  228. TEST_ASSERT(ret && errno == E2BIG,
  229. "register redist region with base address beyond IPA range");
  230. /* The last redist is above the pa range. */
  231. addr = REDIST_REGION_ATTR_ADDR(2, max_phys_size - 0x30000, 0, 2);
  232. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  233. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  234. TEST_ASSERT(ret && errno == E2BIG,
  235. "register redist region with top address beyond IPA range");
  236. addr = 0x260000;
  237. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  238. KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr);
  239. TEST_ASSERT(ret && errno == EINVAL,
  240. "Mix KVM_VGIC_V3_ADDR_TYPE_REDIST and REDIST_REGION");
  241. /*
  242. * Now there are 2 redist regions:
  243. * region 0 @ 0x200000 2 redists
  244. * region 1 @ 0x240000 1 redist
  245. * Attempt to read their characteristics
  246. */
  247. addr = REDIST_REGION_ATTR_ADDR(0, 0, 0, 0);
  248. expected_addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 0);
  249. ret = __kvm_device_attr_get(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  250. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  251. TEST_ASSERT(!ret && addr == expected_addr, "read characteristics of region #0");
  252. addr = REDIST_REGION_ATTR_ADDR(0, 0, 0, 1);
  253. expected_addr = REDIST_REGION_ATTR_ADDR(1, 0x240000, 0, 1);
  254. ret = __kvm_device_attr_get(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  255. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  256. TEST_ASSERT(!ret && addr == expected_addr, "read characteristics of region #1");
  257. addr = REDIST_REGION_ATTR_ADDR(0, 0, 0, 2);
  258. ret = __kvm_device_attr_get(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  259. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  260. TEST_ASSERT(ret && errno == ENOENT, "read characteristics of non existing region");
  261. addr = 0x260000;
  262. kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  263. KVM_VGIC_V3_ADDR_TYPE_DIST, &addr);
  264. addr = REDIST_REGION_ATTR_ADDR(1, 0x260000, 0, 2);
  265. ret = __kvm_device_attr_set(v->gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  266. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  267. TEST_ASSERT(ret && errno == EINVAL, "register redist region colliding with dist");
  268. }
  269. /*
  270. * VGIC KVM device is created and initialized before the secondary CPUs
  271. * get created
  272. */
  273. static void test_vgic_then_vcpus(uint32_t gic_dev_type)
  274. {
  275. struct kvm_vcpu *vcpus[NR_VCPUS];
  276. struct vm_gic v;
  277. int ret, i;
  278. v = vm_gic_create_with_vcpus(gic_dev_type, 1, vcpus);
  279. subtest_dist_rdist(&v);
  280. /* Add the rest of the VCPUs */
  281. for (i = 1; i < NR_VCPUS; ++i)
  282. vcpus[i] = vm_vcpu_add(v.vm, i, guest_code);
  283. ret = run_vcpu(vcpus[3]);
  284. TEST_ASSERT(ret == -EINVAL, "dist/rdist overlap detected on 1st vcpu run");
  285. vm_gic_destroy(&v);
  286. }
  287. /* All the VCPUs are created before the VGIC KVM device gets initialized */
  288. static void test_vcpus_then_vgic(uint32_t gic_dev_type)
  289. {
  290. struct kvm_vcpu *vcpus[NR_VCPUS];
  291. struct vm_gic v;
  292. int ret;
  293. v = vm_gic_create_with_vcpus(gic_dev_type, NR_VCPUS, vcpus);
  294. subtest_dist_rdist(&v);
  295. ret = run_vcpu(vcpus[3]);
  296. TEST_ASSERT(ret == -EINVAL, "dist/rdist overlap detected on 1st vcpu run");
  297. vm_gic_destroy(&v);
  298. }
  299. #define KVM_VGIC_V2_ATTR(offset, cpu) \
  300. (FIELD_PREP(KVM_DEV_ARM_VGIC_OFFSET_MASK, offset) | \
  301. FIELD_PREP(KVM_DEV_ARM_VGIC_CPUID_MASK, cpu))
  302. #define GIC_CPU_CTRL 0x00
  303. static void test_v2_uaccess_cpuif_no_vcpus(void)
  304. {
  305. struct vm_gic v;
  306. u64 val = 0;
  307. int ret;
  308. v = vm_gic_create_barebones(KVM_DEV_TYPE_ARM_VGIC_V2);
  309. subtest_dist_rdist(&v);
  310. ret = __kvm_has_device_attr(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
  311. KVM_VGIC_V2_ATTR(GIC_CPU_CTRL, 0));
  312. TEST_ASSERT(ret && errno == EINVAL,
  313. "accessed non-existent CPU interface, want errno: %i",
  314. EINVAL);
  315. ret = __kvm_device_attr_get(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
  316. KVM_VGIC_V2_ATTR(GIC_CPU_CTRL, 0), &val);
  317. TEST_ASSERT(ret && errno == EINVAL,
  318. "accessed non-existent CPU interface, want errno: %i",
  319. EINVAL);
  320. ret = __kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
  321. KVM_VGIC_V2_ATTR(GIC_CPU_CTRL, 0), &val);
  322. TEST_ASSERT(ret && errno == EINVAL,
  323. "accessed non-existent CPU interface, want errno: %i",
  324. EINVAL);
  325. vm_gic_destroy(&v);
  326. }
  327. static void test_v3_new_redist_regions(void)
  328. {
  329. struct kvm_vcpu *vcpus[NR_VCPUS];
  330. void *dummy = NULL;
  331. struct vm_gic v;
  332. uint64_t addr;
  333. int ret;
  334. v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus);
  335. subtest_v3_redist_regions(&v);
  336. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  337. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  338. ret = run_vcpu(vcpus[3]);
  339. TEST_ASSERT(ret == -ENXIO, "running without sufficient number of rdists");
  340. vm_gic_destroy(&v);
  341. /* step2 */
  342. v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus);
  343. subtest_v3_redist_regions(&v);
  344. addr = REDIST_REGION_ATTR_ADDR(1, 0x280000, 0, 2);
  345. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  346. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  347. ret = run_vcpu(vcpus[3]);
  348. TEST_ASSERT(ret == -EBUSY, "running without vgic explicit init");
  349. vm_gic_destroy(&v);
  350. /* step 3 */
  351. v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus);
  352. subtest_v3_redist_regions(&v);
  353. ret = __kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  354. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, dummy);
  355. TEST_ASSERT(ret && errno == EFAULT,
  356. "register a third region allowing to cover the 4 vcpus");
  357. addr = REDIST_REGION_ATTR_ADDR(1, 0x280000, 0, 2);
  358. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  359. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  360. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  361. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  362. ret = run_vcpu(vcpus[3]);
  363. TEST_ASSERT(!ret, "vcpu run");
  364. vm_gic_destroy(&v);
  365. }
  366. static void test_v3_typer_accesses(void)
  367. {
  368. struct vm_gic v;
  369. uint64_t addr;
  370. int ret, i;
  371. v.vm = vm_create(NR_VCPUS);
  372. (void)vm_vcpu_add(v.vm, 0, guest_code);
  373. v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3);
  374. (void)vm_vcpu_add(v.vm, 3, guest_code);
  375. v3_redist_reg_get_errno(v.gic_fd, 1, GICR_TYPER, EINVAL,
  376. "attempting to read GICR_TYPER of non created vcpu");
  377. (void)vm_vcpu_add(v.vm, 1, guest_code);
  378. v3_redist_reg_get_errno(v.gic_fd, 1, GICR_TYPER, EBUSY,
  379. "read GICR_TYPER before GIC initialized");
  380. (void)vm_vcpu_add(v.vm, 2, guest_code);
  381. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  382. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  383. for (i = 0; i < NR_VCPUS ; i++) {
  384. v3_redist_reg_get(v.gic_fd, i, GICR_TYPER, i * 0x100,
  385. "read GICR_TYPER before rdist region setting");
  386. }
  387. addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 0);
  388. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  389. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  390. /* The 2 first rdists should be put there (vcpu 0 and 3) */
  391. v3_redist_reg_get(v.gic_fd, 0, GICR_TYPER, 0x0, "read typer of rdist #0");
  392. v3_redist_reg_get(v.gic_fd, 3, GICR_TYPER, 0x310, "read typer of rdist #1");
  393. addr = REDIST_REGION_ATTR_ADDR(10, 0x100000, 0, 1);
  394. ret = __kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  395. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  396. TEST_ASSERT(ret && errno == EINVAL, "collision with previous rdist region");
  397. v3_redist_reg_get(v.gic_fd, 1, GICR_TYPER, 0x100,
  398. "no redist region attached to vcpu #1 yet, last cannot be returned");
  399. v3_redist_reg_get(v.gic_fd, 2, GICR_TYPER, 0x200,
  400. "no redist region attached to vcpu #2, last cannot be returned");
  401. addr = REDIST_REGION_ATTR_ADDR(10, 0x20000, 0, 1);
  402. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  403. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  404. v3_redist_reg_get(v.gic_fd, 1, GICR_TYPER, 0x100, "read typer of rdist #1");
  405. v3_redist_reg_get(v.gic_fd, 2, GICR_TYPER, 0x210,
  406. "read typer of rdist #1, last properly returned");
  407. vm_gic_destroy(&v);
  408. }
  409. static struct vm_gic vm_gic_v3_create_with_vcpuids(int nr_vcpus,
  410. uint32_t vcpuids[])
  411. {
  412. struct vm_gic v;
  413. int i;
  414. v.vm = vm_create(nr_vcpus);
  415. for (i = 0; i < nr_vcpus; i++)
  416. vm_vcpu_add(v.vm, vcpuids[i], guest_code);
  417. v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3);
  418. return v;
  419. }
  420. /**
  421. * Test GICR_TYPER last bit with new redist regions
  422. * rdist regions #1 and #2 are contiguous
  423. * rdist region #0 @0x100000 2 rdist capacity
  424. * rdists: 0, 3 (Last)
  425. * rdist region #1 @0x240000 2 rdist capacity
  426. * rdists: 5, 4 (Last)
  427. * rdist region #2 @0x200000 2 rdist capacity
  428. * rdists: 1, 2
  429. */
  430. static void test_v3_last_bit_redist_regions(void)
  431. {
  432. uint32_t vcpuids[] = { 0, 3, 5, 4, 1, 2 };
  433. struct vm_gic v;
  434. uint64_t addr;
  435. v = vm_gic_v3_create_with_vcpuids(ARRAY_SIZE(vcpuids), vcpuids);
  436. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  437. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  438. addr = REDIST_REGION_ATTR_ADDR(2, 0x100000, 0, 0);
  439. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  440. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  441. addr = REDIST_REGION_ATTR_ADDR(2, 0x240000, 0, 1);
  442. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  443. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  444. addr = REDIST_REGION_ATTR_ADDR(2, 0x200000, 0, 2);
  445. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  446. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr);
  447. v3_redist_reg_get(v.gic_fd, 0, GICR_TYPER, 0x000, "read typer of rdist #0");
  448. v3_redist_reg_get(v.gic_fd, 1, GICR_TYPER, 0x100, "read typer of rdist #1");
  449. v3_redist_reg_get(v.gic_fd, 2, GICR_TYPER, 0x200, "read typer of rdist #2");
  450. v3_redist_reg_get(v.gic_fd, 3, GICR_TYPER, 0x310, "read typer of rdist #3");
  451. v3_redist_reg_get(v.gic_fd, 5, GICR_TYPER, 0x500, "read typer of rdist #5");
  452. v3_redist_reg_get(v.gic_fd, 4, GICR_TYPER, 0x410, "read typer of rdist #4");
  453. vm_gic_destroy(&v);
  454. }
  455. /* Test last bit with legacy region */
  456. static void test_v3_last_bit_single_rdist(void)
  457. {
  458. uint32_t vcpuids[] = { 0, 3, 5, 4, 1, 2 };
  459. struct vm_gic v;
  460. uint64_t addr;
  461. v = vm_gic_v3_create_with_vcpuids(ARRAY_SIZE(vcpuids), vcpuids);
  462. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  463. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  464. addr = 0x10000;
  465. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  466. KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr);
  467. v3_redist_reg_get(v.gic_fd, 0, GICR_TYPER, 0x000, "read typer of rdist #0");
  468. v3_redist_reg_get(v.gic_fd, 3, GICR_TYPER, 0x300, "read typer of rdist #1");
  469. v3_redist_reg_get(v.gic_fd, 5, GICR_TYPER, 0x500, "read typer of rdist #2");
  470. v3_redist_reg_get(v.gic_fd, 1, GICR_TYPER, 0x100, "read typer of rdist #3");
  471. v3_redist_reg_get(v.gic_fd, 2, GICR_TYPER, 0x210, "read typer of rdist #3");
  472. vm_gic_destroy(&v);
  473. }
  474. /* Uses the legacy REDIST region API. */
  475. static void test_v3_redist_ipa_range_check_at_vcpu_run(void)
  476. {
  477. struct kvm_vcpu *vcpus[NR_VCPUS];
  478. struct vm_gic v;
  479. int ret, i;
  480. uint64_t addr;
  481. v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, 1, vcpus);
  482. /* Set space for 3 redists, we have 1 vcpu, so this succeeds. */
  483. addr = max_phys_size - (3 * 2 * 0x10000);
  484. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  485. KVM_VGIC_V3_ADDR_TYPE_REDIST, &addr);
  486. addr = 0x00000;
  487. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  488. KVM_VGIC_V3_ADDR_TYPE_DIST, &addr);
  489. /* Add the rest of the VCPUs */
  490. for (i = 1; i < NR_VCPUS; ++i)
  491. vcpus[i] = vm_vcpu_add(v.vm, i, guest_code);
  492. kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  493. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
  494. /* Attempt to run a vcpu without enough redist space. */
  495. ret = run_vcpu(vcpus[2]);
  496. TEST_ASSERT(ret && errno == EINVAL,
  497. "redist base+size above PA range detected on 1st vcpu run");
  498. vm_gic_destroy(&v);
  499. }
  500. static void test_v3_its_region(void)
  501. {
  502. struct kvm_vcpu *vcpus[NR_VCPUS];
  503. struct vm_gic v;
  504. uint64_t addr;
  505. int its_fd, ret;
  506. v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus);
  507. its_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_ITS);
  508. addr = 0x401000;
  509. ret = __kvm_device_attr_set(its_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  510. KVM_VGIC_ITS_ADDR_TYPE, &addr);
  511. TEST_ASSERT(ret && errno == EINVAL,
  512. "ITS region with misaligned address");
  513. addr = max_phys_size;
  514. ret = __kvm_device_attr_set(its_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  515. KVM_VGIC_ITS_ADDR_TYPE, &addr);
  516. TEST_ASSERT(ret && errno == E2BIG,
  517. "register ITS region with base address beyond IPA range");
  518. addr = max_phys_size - 0x10000;
  519. ret = __kvm_device_attr_set(its_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  520. KVM_VGIC_ITS_ADDR_TYPE, &addr);
  521. TEST_ASSERT(ret && errno == E2BIG,
  522. "Half of ITS region is beyond IPA range");
  523. /* This one succeeds setting the ITS base */
  524. addr = 0x400000;
  525. kvm_device_attr_set(its_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  526. KVM_VGIC_ITS_ADDR_TYPE, &addr);
  527. addr = 0x300000;
  528. ret = __kvm_device_attr_set(its_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  529. KVM_VGIC_ITS_ADDR_TYPE, &addr);
  530. TEST_ASSERT(ret && errno == EEXIST, "ITS base set again");
  531. close(its_fd);
  532. vm_gic_destroy(&v);
  533. }
  534. /*
  535. * Returns 0 if it's possible to create GIC device of a given type (V2 or V3).
  536. */
  537. int test_kvm_device(uint32_t gic_dev_type)
  538. {
  539. struct kvm_vcpu *vcpus[NR_VCPUS];
  540. struct vm_gic v;
  541. uint32_t other;
  542. int ret;
  543. v.vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
  544. /* try to create a non existing KVM device */
  545. ret = __kvm_test_create_device(v.vm, 0);
  546. TEST_ASSERT(ret && errno == ENODEV, "unsupported device");
  547. /* trial mode */
  548. ret = __kvm_test_create_device(v.vm, gic_dev_type);
  549. if (ret)
  550. return ret;
  551. v.gic_fd = kvm_create_device(v.vm, gic_dev_type);
  552. ret = __kvm_create_device(v.vm, gic_dev_type);
  553. TEST_ASSERT(ret < 0 && errno == EEXIST, "create GIC device twice");
  554. /* try to create the other gic_dev_type */
  555. other = VGIC_DEV_IS_V2(gic_dev_type) ? KVM_DEV_TYPE_ARM_VGIC_V3
  556. : KVM_DEV_TYPE_ARM_VGIC_V2;
  557. if (!__kvm_test_create_device(v.vm, other)) {
  558. ret = __kvm_create_device(v.vm, other);
  559. TEST_ASSERT(ret < 0 && (errno == EINVAL || errno == EEXIST),
  560. "create GIC device while other version exists");
  561. }
  562. vm_gic_destroy(&v);
  563. return 0;
  564. }
  565. void run_tests(uint32_t gic_dev_type)
  566. {
  567. test_vcpus_then_vgic(gic_dev_type);
  568. test_vgic_then_vcpus(gic_dev_type);
  569. if (VGIC_DEV_IS_V2(gic_dev_type))
  570. test_v2_uaccess_cpuif_no_vcpus();
  571. if (VGIC_DEV_IS_V3(gic_dev_type)) {
  572. test_v3_new_redist_regions();
  573. test_v3_typer_accesses();
  574. test_v3_last_bit_redist_regions();
  575. test_v3_last_bit_single_rdist();
  576. test_v3_redist_ipa_range_check_at_vcpu_run();
  577. test_v3_its_region();
  578. }
  579. }
  580. int main(int ac, char **av)
  581. {
  582. int ret;
  583. int pa_bits;
  584. int cnt_impl = 0;
  585. pa_bits = vm_guest_mode_params[VM_MODE_DEFAULT].pa_bits;
  586. max_phys_size = 1ULL << pa_bits;
  587. ret = test_kvm_device(KVM_DEV_TYPE_ARM_VGIC_V3);
  588. if (!ret) {
  589. pr_info("Running GIC_v3 tests.\n");
  590. run_tests(KVM_DEV_TYPE_ARM_VGIC_V3);
  591. cnt_impl++;
  592. }
  593. ret = test_kvm_device(KVM_DEV_TYPE_ARM_VGIC_V2);
  594. if (!ret) {
  595. pr_info("Running GIC_v2 tests.\n");
  596. run_tests(KVM_DEV_TYPE_ARM_VGIC_V2);
  597. cnt_impl++;
  598. }
  599. if (!cnt_impl) {
  600. print_skip("No GICv2 nor GICv3 support");
  601. exit(KSFT_SKIP);
  602. }
  603. return 0;
  604. }