prom-env-test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Test Open-Firmware-based machines.
  3. *
  4. * Copyright (c) 2016, 2017 Red Hat Inc.
  5. *
  6. * Author:
  7. * Thomas Huth <thuth@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2
  10. * or later. See the COPYING file in the top-level directory.
  11. *
  12. * This test is used to check that some Open Firmware based machines (i.e.
  13. * OpenBIOS or SLOF) can be started successfully in TCG mode. To do this, we
  14. * first put some Forth code into the "boot-command" Open Firmware environment
  15. * variable. This Forth code writes a well-known magic value to a known location
  16. * in memory. Then we start the guest so that the firmware can boot and finally
  17. * run the Forth code.
  18. * The testing code here then can finally check whether the value has been
  19. * successfully written into the guest memory.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "libqtest.h"
  23. #define MAGIC 0xcafec0de
  24. #define ADDRESS 0x4000
  25. static void check_guest_memory(QTestState *qts)
  26. {
  27. uint32_t signature;
  28. int i;
  29. /* Poll until code has run and modified memory. Wait at most 600 seconds */
  30. for (i = 0; i < 60000; ++i) {
  31. signature = qtest_readl(qts, ADDRESS);
  32. if (signature == MAGIC) {
  33. break;
  34. }
  35. g_usleep(10000);
  36. }
  37. g_assert_cmphex(signature, ==, MAGIC);
  38. }
  39. static void test_machine(const void *machine)
  40. {
  41. const char *extra_args = "";
  42. QTestState *qts;
  43. /*
  44. * The pseries firmware boots much faster without the default
  45. * devices, it also needs Spectre/Meltdown workarounds disabled to
  46. * avoid warnings with TCG
  47. */
  48. if (strcmp(machine, "pseries") == 0) {
  49. extra_args = "-nodefaults"
  50. " -machine cap-cfpc=broken,cap-sbbc=broken,cap-ibs=broken";
  51. }
  52. qts = qtest_initf("-M %s,accel=tcg %s -prom-env 'use-nvramrc?=true' "
  53. "-prom-env 'nvramrc=%x %x l!' ", (const char *)machine,
  54. extra_args, MAGIC, ADDRESS);
  55. check_guest_memory(qts);
  56. qtest_quit(qts);
  57. }
  58. static void add_tests(const char *machines[])
  59. {
  60. int i;
  61. char *name;
  62. for (i = 0; machines[i] != NULL; i++) {
  63. name = g_strdup_printf("prom-env/%s", machines[i]);
  64. qtest_add_data_func(name, machines[i], test_machine);
  65. g_free(name);
  66. }
  67. }
  68. int main(int argc, char *argv[])
  69. {
  70. const char *sparc_machines[] = { "SPARCbook", "Voyager", "SS-20", NULL };
  71. const char *sparc64_machines[] = { "sun4u", NULL };
  72. const char *ppc_machines[] = { "mac99", "g3beige", NULL };
  73. const char *arch = qtest_get_arch();
  74. g_test_init(&argc, &argv, NULL);
  75. if (!strcmp(arch, "ppc")) {
  76. add_tests(ppc_machines);
  77. } else if (!strcmp(arch, "ppc64")) {
  78. add_tests(ppc_machines);
  79. if (g_test_slow()) {
  80. qtest_add_data_func("prom-env/pseries", "pseries", test_machine);
  81. }
  82. } else if (!strcmp(arch, "sparc")) {
  83. add_tests(sparc_machines);
  84. } else if (!strcmp(arch, "sparc64")) {
  85. add_tests(sparc64_machines);
  86. } else {
  87. g_assert_not_reached();
  88. }
  89. return g_test_run();
  90. }