get-reg-list.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Check for KVM_GET_REG_LIST regressions.
  4. *
  5. * Copyright (C) 2020, Red Hat, Inc.
  6. *
  7. * When attempting to migrate from a host with an older kernel to a host
  8. * with a newer kernel we allow the newer kernel on the destination to
  9. * list new registers with get-reg-list. We assume they'll be unused, at
  10. * least until the guest reboots, and so they're relatively harmless.
  11. * However, if the destination host with the newer kernel is missing
  12. * registers which the source host with the older kernel has, then that's
  13. * a regression in get-reg-list. This test checks for that regression by
  14. * checking the current list against a blessed list. We should never have
  15. * missing registers, but if new ones appear then they can probably be
  16. * added to the blessed list. A completely new blessed list can be created
  17. * by running the test with the --list command line argument.
  18. *
  19. * The blessed list should be created from the oldest possible kernel.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include "kvm_util.h"
  28. #include "test_util.h"
  29. #include "processor.h"
  30. static struct kvm_reg_list *reg_list;
  31. static __u64 *blessed_reg, blessed_n;
  32. extern struct vcpu_reg_list *vcpu_configs[];
  33. extern int vcpu_configs_n;
  34. #define for_each_reg(i) \
  35. for ((i) = 0; (i) < reg_list->n; ++(i))
  36. #define for_each_reg_filtered(i) \
  37. for_each_reg(i) \
  38. if (!filter_reg(reg_list->reg[i]))
  39. #define for_each_missing_reg(i) \
  40. for ((i) = 0; (i) < blessed_n; ++(i)) \
  41. if (!find_reg(reg_list->reg, reg_list->n, blessed_reg[i])) \
  42. if (check_supported_reg(vcpu, blessed_reg[i]))
  43. #define for_each_new_reg(i) \
  44. for_each_reg_filtered(i) \
  45. if (!find_reg(blessed_reg, blessed_n, reg_list->reg[i]))
  46. #define for_each_present_blessed_reg(i) \
  47. for_each_reg(i) \
  48. if (find_reg(blessed_reg, blessed_n, reg_list->reg[i]))
  49. static const char *config_name(struct vcpu_reg_list *c)
  50. {
  51. struct vcpu_reg_sublist *s;
  52. int len = 0;
  53. if (c->name)
  54. return c->name;
  55. for_each_sublist(c, s)
  56. len += strlen(s->name) + 1;
  57. c->name = malloc(len);
  58. len = 0;
  59. for_each_sublist(c, s) {
  60. if (!strcmp(s->name, "base"))
  61. continue;
  62. if (len)
  63. c->name[len++] = '+';
  64. strcpy(c->name + len, s->name);
  65. len += strlen(s->name);
  66. }
  67. c->name[len] = '\0';
  68. return c->name;
  69. }
  70. bool __weak check_supported_reg(struct kvm_vcpu *vcpu, __u64 reg)
  71. {
  72. return true;
  73. }
  74. bool __weak filter_reg(__u64 reg)
  75. {
  76. return false;
  77. }
  78. static bool find_reg(__u64 regs[], __u64 nr_regs, __u64 reg)
  79. {
  80. int i;
  81. for (i = 0; i < nr_regs; ++i)
  82. if (reg == regs[i])
  83. return true;
  84. return false;
  85. }
  86. void __weak print_reg(const char *prefix, __u64 id)
  87. {
  88. printf("\t0x%llx,\n", id);
  89. }
  90. bool __weak check_reject_set(int err)
  91. {
  92. return true;
  93. }
  94. void __weak finalize_vcpu(struct kvm_vcpu *vcpu, struct vcpu_reg_list *c)
  95. {
  96. }
  97. #ifdef __aarch64__
  98. static void prepare_vcpu_init(struct kvm_vm *vm, struct vcpu_reg_list *c,
  99. struct kvm_vcpu_init *init)
  100. {
  101. struct vcpu_reg_sublist *s;
  102. vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, init);
  103. for_each_sublist(c, s)
  104. if (s->capability)
  105. init->features[s->feature / 32] |= 1 << (s->feature % 32);
  106. }
  107. static struct kvm_vcpu *vcpu_config_get_vcpu(struct vcpu_reg_list *c, struct kvm_vm *vm)
  108. {
  109. struct kvm_vcpu_init init;
  110. struct kvm_vcpu *vcpu;
  111. prepare_vcpu_init(vm, c, &init);
  112. vcpu = __vm_vcpu_add(vm, 0);
  113. aarch64_vcpu_setup(vcpu, &init);
  114. return vcpu;
  115. }
  116. #else
  117. static struct kvm_vcpu *vcpu_config_get_vcpu(struct vcpu_reg_list *c, struct kvm_vm *vm)
  118. {
  119. return __vm_vcpu_add(vm, 0);
  120. }
  121. #endif
  122. static void check_supported(struct vcpu_reg_list *c)
  123. {
  124. struct vcpu_reg_sublist *s;
  125. for_each_sublist(c, s) {
  126. if (!s->capability)
  127. continue;
  128. __TEST_REQUIRE(kvm_has_cap(s->capability),
  129. "%s: %s not available, skipping tests",
  130. config_name(c), s->name);
  131. }
  132. }
  133. static bool print_list;
  134. static bool print_filtered;
  135. static void run_test(struct vcpu_reg_list *c)
  136. {
  137. int new_regs = 0, missing_regs = 0, i, n;
  138. int failed_get = 0, failed_set = 0, failed_reject = 0;
  139. int skipped_set = 0;
  140. struct kvm_vcpu *vcpu;
  141. struct kvm_vm *vm;
  142. struct vcpu_reg_sublist *s;
  143. check_supported(c);
  144. vm = vm_create_barebones();
  145. vcpu = vcpu_config_get_vcpu(c, vm);
  146. finalize_vcpu(vcpu, c);
  147. reg_list = vcpu_get_reg_list(vcpu);
  148. if (print_list || print_filtered) {
  149. putchar('\n');
  150. for_each_reg(i) {
  151. __u64 id = reg_list->reg[i];
  152. if ((print_list && !filter_reg(id)) ||
  153. (print_filtered && filter_reg(id)))
  154. print_reg(config_name(c), id);
  155. }
  156. putchar('\n');
  157. return;
  158. }
  159. for_each_sublist(c, s)
  160. blessed_n += s->regs_n;
  161. blessed_reg = calloc(blessed_n, sizeof(__u64));
  162. n = 0;
  163. for_each_sublist(c, s) {
  164. for (i = 0; i < s->regs_n; ++i)
  165. blessed_reg[n++] = s->regs[i];
  166. }
  167. /*
  168. * We only test that we can get the register and then write back the
  169. * same value. Some registers may allow other values to be written
  170. * back, but others only allow some bits to be changed, and at least
  171. * for ID registers set will fail if the value does not exactly match
  172. * what was returned by get. If registers that allow other values to
  173. * be written need to have the other values tested, then we should
  174. * create a new set of tests for those in a new independent test
  175. * executable.
  176. *
  177. * Only do the get/set tests on present, blessed list registers,
  178. * since we don't know the capabilities of any new registers.
  179. */
  180. for_each_present_blessed_reg(i) {
  181. uint8_t addr[2048 / 8];
  182. struct kvm_one_reg reg = {
  183. .id = reg_list->reg[i],
  184. .addr = (__u64)&addr,
  185. };
  186. bool reject_reg = false, skip_reg = false;
  187. int ret;
  188. ret = __vcpu_get_reg(vcpu, reg_list->reg[i], &addr);
  189. if (ret) {
  190. printf("%s: Failed to get ", config_name(c));
  191. print_reg(config_name(c), reg.id);
  192. putchar('\n');
  193. ++failed_get;
  194. }
  195. for_each_sublist(c, s) {
  196. /* rejects_set registers are rejected for set operation */
  197. if (s->rejects_set && find_reg(s->rejects_set, s->rejects_set_n, reg.id)) {
  198. reject_reg = true;
  199. ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
  200. if (ret != -1 || !check_reject_set(errno)) {
  201. printf("%s: Failed to reject (ret=%d, errno=%d) ", config_name(c), ret, errno);
  202. print_reg(config_name(c), reg.id);
  203. putchar('\n');
  204. ++failed_reject;
  205. }
  206. break;
  207. }
  208. /* skips_set registers are skipped for set operation */
  209. if (s->skips_set && find_reg(s->skips_set, s->skips_set_n, reg.id)) {
  210. skip_reg = true;
  211. ++skipped_set;
  212. break;
  213. }
  214. }
  215. if (!reject_reg && !skip_reg) {
  216. ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
  217. if (ret) {
  218. printf("%s: Failed to set ", config_name(c));
  219. print_reg(config_name(c), reg.id);
  220. putchar('\n');
  221. ++failed_set;
  222. }
  223. }
  224. }
  225. for_each_new_reg(i)
  226. ++new_regs;
  227. for_each_missing_reg(i)
  228. ++missing_regs;
  229. if (new_regs || missing_regs) {
  230. n = 0;
  231. for_each_reg_filtered(i)
  232. ++n;
  233. printf("%s: Number blessed registers: %5lld\n", config_name(c), blessed_n);
  234. printf("%s: Number registers: %5lld (includes %lld filtered registers)\n",
  235. config_name(c), reg_list->n, reg_list->n - n);
  236. }
  237. if (new_regs) {
  238. printf("\n%s: There are %d new registers.\n"
  239. "Consider adding them to the blessed reg "
  240. "list with the following lines:\n\n", config_name(c), new_regs);
  241. for_each_new_reg(i)
  242. print_reg(config_name(c), reg_list->reg[i]);
  243. putchar('\n');
  244. }
  245. if (missing_regs) {
  246. printf("\n%s: There are %d missing registers.\n"
  247. "The following lines are missing registers:\n\n", config_name(c), missing_regs);
  248. for_each_missing_reg(i)
  249. print_reg(config_name(c), blessed_reg[i]);
  250. putchar('\n');
  251. }
  252. TEST_ASSERT(!missing_regs && !failed_get && !failed_set && !failed_reject,
  253. "%s: There are %d missing registers; %d registers failed get; "
  254. "%d registers failed set; %d registers failed reject; %d registers skipped set",
  255. config_name(c), missing_regs, failed_get, failed_set, failed_reject, skipped_set);
  256. pr_info("%s: PASS\n", config_name(c));
  257. blessed_n = 0;
  258. free(blessed_reg);
  259. free(reg_list);
  260. kvm_vm_free(vm);
  261. }
  262. static void help(void)
  263. {
  264. struct vcpu_reg_list *c;
  265. int i;
  266. printf(
  267. "\n"
  268. "usage: get-reg-list [--config=<selection>] [--list] [--list-filtered]\n\n"
  269. " --config=<selection> Used to select a specific vcpu configuration for the test/listing\n"
  270. " '<selection>' may be\n");
  271. for (i = 0; i < vcpu_configs_n; ++i) {
  272. c = vcpu_configs[i];
  273. printf(
  274. " '%s'\n", config_name(c));
  275. }
  276. printf(
  277. "\n"
  278. " --list Print the register list rather than test it (requires --config)\n"
  279. " --list-filtered Print registers that would normally be filtered out (requires --config)\n"
  280. "\n"
  281. );
  282. }
  283. static struct vcpu_reg_list *parse_config(const char *config)
  284. {
  285. struct vcpu_reg_list *c = NULL;
  286. int i;
  287. if (config[8] != '=')
  288. help(), exit(1);
  289. for (i = 0; i < vcpu_configs_n; ++i) {
  290. c = vcpu_configs[i];
  291. if (strcmp(config_name(c), &config[9]) == 0)
  292. break;
  293. }
  294. if (i == vcpu_configs_n)
  295. help(), exit(1);
  296. return c;
  297. }
  298. int main(int ac, char **av)
  299. {
  300. struct vcpu_reg_list *c, *sel = NULL;
  301. int i, ret = 0;
  302. pid_t pid;
  303. for (i = 1; i < ac; ++i) {
  304. if (strncmp(av[i], "--config", 8) == 0)
  305. sel = parse_config(av[i]);
  306. else if (strcmp(av[i], "--list") == 0)
  307. print_list = true;
  308. else if (strcmp(av[i], "--list-filtered") == 0)
  309. print_filtered = true;
  310. else if (strcmp(av[i], "--help") == 0 || strcmp(av[1], "-h") == 0)
  311. help(), exit(0);
  312. else
  313. help(), exit(1);
  314. }
  315. if (print_list || print_filtered) {
  316. /*
  317. * We only want to print the register list of a single config.
  318. */
  319. if (!sel)
  320. help(), exit(1);
  321. }
  322. for (i = 0; i < vcpu_configs_n; ++i) {
  323. c = vcpu_configs[i];
  324. if (sel && c != sel)
  325. continue;
  326. pid = fork();
  327. if (!pid) {
  328. run_test(c);
  329. exit(0);
  330. } else {
  331. int wstatus;
  332. pid_t wpid = wait(&wstatus);
  333. TEST_ASSERT(wpid == pid && WIFEXITED(wstatus), "wait: Unexpected return");
  334. if (WEXITSTATUS(wstatus) && WEXITSTATUS(wstatus) != KSFT_SKIP)
  335. ret = KSFT_FAIL;
  336. }
  337. }
  338. return ret;
  339. }