get-reg-list.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 vcpu_reg_list *c, struct kvm_vcpu_init *init)
  99. {
  100. struct vcpu_reg_sublist *s;
  101. for_each_sublist(c, s)
  102. if (s->capability)
  103. init->features[s->feature / 32] |= 1 << (s->feature % 32);
  104. }
  105. static struct kvm_vcpu *vcpu_config_get_vcpu(struct vcpu_reg_list *c, struct kvm_vm *vm)
  106. {
  107. struct kvm_vcpu_init init = { .target = -1, };
  108. struct kvm_vcpu *vcpu;
  109. prepare_vcpu_init(c, &init);
  110. vcpu = __vm_vcpu_add(vm, 0);
  111. aarch64_vcpu_setup(vcpu, &init);
  112. return vcpu;
  113. }
  114. #else
  115. static struct kvm_vcpu *vcpu_config_get_vcpu(struct vcpu_reg_list *c, struct kvm_vm *vm)
  116. {
  117. return __vm_vcpu_add(vm, 0);
  118. }
  119. #endif
  120. static void check_supported(struct vcpu_reg_list *c)
  121. {
  122. struct vcpu_reg_sublist *s;
  123. for_each_sublist(c, s) {
  124. if (!s->capability)
  125. continue;
  126. __TEST_REQUIRE(kvm_has_cap(s->capability),
  127. "%s: %s not available, skipping tests",
  128. config_name(c), s->name);
  129. }
  130. }
  131. static bool print_list;
  132. static bool print_filtered;
  133. static void run_test(struct vcpu_reg_list *c)
  134. {
  135. int new_regs = 0, missing_regs = 0, i, n;
  136. int failed_get = 0, failed_set = 0, failed_reject = 0;
  137. int skipped_set = 0;
  138. struct kvm_vcpu *vcpu;
  139. struct kvm_vm *vm;
  140. struct vcpu_reg_sublist *s;
  141. check_supported(c);
  142. vm = vm_create_barebones();
  143. vcpu = vcpu_config_get_vcpu(c, vm);
  144. finalize_vcpu(vcpu, c);
  145. reg_list = vcpu_get_reg_list(vcpu);
  146. if (print_list || print_filtered) {
  147. putchar('\n');
  148. for_each_reg(i) {
  149. __u64 id = reg_list->reg[i];
  150. if ((print_list && !filter_reg(id)) ||
  151. (print_filtered && filter_reg(id)))
  152. print_reg(config_name(c), id);
  153. }
  154. putchar('\n');
  155. return;
  156. }
  157. for_each_sublist(c, s)
  158. blessed_n += s->regs_n;
  159. blessed_reg = calloc(blessed_n, sizeof(__u64));
  160. n = 0;
  161. for_each_sublist(c, s) {
  162. for (i = 0; i < s->regs_n; ++i)
  163. blessed_reg[n++] = s->regs[i];
  164. }
  165. /*
  166. * We only test that we can get the register and then write back the
  167. * same value. Some registers may allow other values to be written
  168. * back, but others only allow some bits to be changed, and at least
  169. * for ID registers set will fail if the value does not exactly match
  170. * what was returned by get. If registers that allow other values to
  171. * be written need to have the other values tested, then we should
  172. * create a new set of tests for those in a new independent test
  173. * executable.
  174. *
  175. * Only do the get/set tests on present, blessed list registers,
  176. * since we don't know the capabilities of any new registers.
  177. */
  178. for_each_present_blessed_reg(i) {
  179. uint8_t addr[2048 / 8];
  180. struct kvm_one_reg reg = {
  181. .id = reg_list->reg[i],
  182. .addr = (__u64)&addr,
  183. };
  184. bool reject_reg = false, skip_reg = false;
  185. int ret;
  186. ret = __vcpu_get_reg(vcpu, reg_list->reg[i], &addr);
  187. if (ret) {
  188. printf("%s: Failed to get ", config_name(c));
  189. print_reg(config_name(c), reg.id);
  190. putchar('\n');
  191. ++failed_get;
  192. }
  193. for_each_sublist(c, s) {
  194. /* rejects_set registers are rejected for set operation */
  195. if (s->rejects_set && find_reg(s->rejects_set, s->rejects_set_n, reg.id)) {
  196. reject_reg = true;
  197. ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
  198. if (ret != -1 || !check_reject_set(errno)) {
  199. printf("%s: Failed to reject (ret=%d, errno=%d) ", config_name(c), ret, errno);
  200. print_reg(config_name(c), reg.id);
  201. putchar('\n');
  202. ++failed_reject;
  203. }
  204. break;
  205. }
  206. /* skips_set registers are skipped for set operation */
  207. if (s->skips_set && find_reg(s->skips_set, s->skips_set_n, reg.id)) {
  208. skip_reg = true;
  209. ++skipped_set;
  210. break;
  211. }
  212. }
  213. if (!reject_reg && !skip_reg) {
  214. ret = __vcpu_ioctl(vcpu, KVM_SET_ONE_REG, &reg);
  215. if (ret) {
  216. printf("%s: Failed to set ", config_name(c));
  217. print_reg(config_name(c), reg.id);
  218. putchar('\n');
  219. ++failed_set;
  220. }
  221. }
  222. }
  223. for_each_new_reg(i)
  224. ++new_regs;
  225. for_each_missing_reg(i)
  226. ++missing_regs;
  227. if (new_regs || missing_regs) {
  228. n = 0;
  229. for_each_reg_filtered(i)
  230. ++n;
  231. printf("%s: Number blessed registers: %5lld\n", config_name(c), blessed_n);
  232. printf("%s: Number registers: %5lld (includes %lld filtered registers)\n",
  233. config_name(c), reg_list->n, reg_list->n - n);
  234. }
  235. if (new_regs) {
  236. printf("\n%s: There are %d new registers.\n"
  237. "Consider adding them to the blessed reg "
  238. "list with the following lines:\n\n", config_name(c), new_regs);
  239. for_each_new_reg(i)
  240. print_reg(config_name(c), reg_list->reg[i]);
  241. putchar('\n');
  242. }
  243. if (missing_regs) {
  244. printf("\n%s: There are %d missing registers.\n"
  245. "The following lines are missing registers:\n\n", config_name(c), missing_regs);
  246. for_each_missing_reg(i)
  247. print_reg(config_name(c), blessed_reg[i]);
  248. putchar('\n');
  249. }
  250. TEST_ASSERT(!missing_regs && !failed_get && !failed_set && !failed_reject,
  251. "%s: There are %d missing registers; %d registers failed get; "
  252. "%d registers failed set; %d registers failed reject; %d registers skipped set",
  253. config_name(c), missing_regs, failed_get, failed_set, failed_reject, skipped_set);
  254. pr_info("%s: PASS\n", config_name(c));
  255. blessed_n = 0;
  256. free(blessed_reg);
  257. free(reg_list);
  258. kvm_vm_free(vm);
  259. }
  260. static void help(void)
  261. {
  262. struct vcpu_reg_list *c;
  263. int i;
  264. printf(
  265. "\n"
  266. "usage: get-reg-list [--config=<selection>] [--list] [--list-filtered]\n\n"
  267. " --config=<selection> Used to select a specific vcpu configuration for the test/listing\n"
  268. " '<selection>' may be\n");
  269. for (i = 0; i < vcpu_configs_n; ++i) {
  270. c = vcpu_configs[i];
  271. printf(
  272. " '%s'\n", config_name(c));
  273. }
  274. printf(
  275. "\n"
  276. " --list Print the register list rather than test it (requires --config)\n"
  277. " --list-filtered Print registers that would normally be filtered out (requires --config)\n"
  278. "\n"
  279. );
  280. }
  281. static struct vcpu_reg_list *parse_config(const char *config)
  282. {
  283. struct vcpu_reg_list *c = NULL;
  284. int i;
  285. if (config[8] != '=')
  286. help(), exit(1);
  287. for (i = 0; i < vcpu_configs_n; ++i) {
  288. c = vcpu_configs[i];
  289. if (strcmp(config_name(c), &config[9]) == 0)
  290. break;
  291. }
  292. if (i == vcpu_configs_n)
  293. help(), exit(1);
  294. return c;
  295. }
  296. int main(int ac, char **av)
  297. {
  298. struct vcpu_reg_list *c, *sel = NULL;
  299. int i, ret = 0;
  300. pid_t pid;
  301. for (i = 1; i < ac; ++i) {
  302. if (strncmp(av[i], "--config", 8) == 0)
  303. sel = parse_config(av[i]);
  304. else if (strcmp(av[i], "--list") == 0)
  305. print_list = true;
  306. else if (strcmp(av[i], "--list-filtered") == 0)
  307. print_filtered = true;
  308. else if (strcmp(av[i], "--help") == 0 || strcmp(av[1], "-h") == 0)
  309. help(), exit(0);
  310. else
  311. help(), exit(1);
  312. }
  313. if (print_list || print_filtered) {
  314. /*
  315. * We only want to print the register list of a single config.
  316. */
  317. if (!sel)
  318. help(), exit(1);
  319. }
  320. for (i = 0; i < vcpu_configs_n; ++i) {
  321. c = vcpu_configs[i];
  322. if (sel && c != sel)
  323. continue;
  324. pid = fork();
  325. if (!pid) {
  326. run_test(c);
  327. exit(0);
  328. } else {
  329. int wstatus;
  330. pid_t wpid = wait(&wstatus);
  331. TEST_ASSERT(wpid == pid && WIFEXITED(wstatus), "wait: Unexpected return");
  332. if (WEXITSTATUS(wstatus) && WEXITSTATUS(wstatus) != KSFT_SKIP)
  333. ret = KSFT_FAIL;
  334. }
  335. }
  336. return ret;
  337. }