2
0

i440fx-test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * qtest I440FX test case
  3. *
  4. * Copyright IBM, Corp. 2012-2013
  5. * Copyright Red Hat, Inc. 2013
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Laszlo Ersek <lersek@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12. * See the COPYING file in the top-level directory.
  13. */
  14. #include "qemu/osdep.h"
  15. #include "libqtest-single.h"
  16. #include "libqos/pci.h"
  17. #include "libqos/pci-pc.h"
  18. #include "hw/pci/pci_regs.h"
  19. #define BROKEN 1
  20. typedef struct TestData
  21. {
  22. int num_cpus;
  23. } TestData;
  24. typedef struct FirmwareTestFixture {
  25. /* decides whether we're testing -bios or -pflash */
  26. bool is_bios;
  27. } FirmwareTestFixture;
  28. static QPCIBus *test_start_get_bus(const TestData *s)
  29. {
  30. char *cmdline;
  31. cmdline = g_strdup_printf("-smp %d", s->num_cpus);
  32. qtest_start(cmdline);
  33. g_free(cmdline);
  34. return qpci_new_pc(global_qtest, NULL);
  35. }
  36. static void test_i440fx_defaults(gconstpointer opaque)
  37. {
  38. const TestData *s = opaque;
  39. QPCIBus *bus;
  40. QPCIDevice *dev;
  41. uint32_t value;
  42. bus = test_start_get_bus(s);
  43. dev = qpci_device_find(bus, QPCI_DEVFN(0, 0));
  44. g_assert(dev != NULL);
  45. /* 3.2.2 */
  46. g_assert_cmpint(qpci_config_readw(dev, PCI_VENDOR_ID), ==, 0x8086);
  47. /* 3.2.3 */
  48. g_assert_cmpint(qpci_config_readw(dev, PCI_DEVICE_ID), ==, 0x1237);
  49. #ifndef BROKEN
  50. /* 3.2.4 */
  51. g_assert_cmpint(qpci_config_readw(dev, PCI_COMMAND), ==, 0x0006);
  52. /* 3.2.5 */
  53. g_assert_cmpint(qpci_config_readw(dev, PCI_STATUS), ==, 0x0280);
  54. #endif
  55. /* 3.2.7 */
  56. g_assert_cmpint(qpci_config_readb(dev, PCI_CLASS_PROG), ==, 0x00);
  57. g_assert_cmpint(qpci_config_readw(dev, PCI_CLASS_DEVICE), ==, 0x0600);
  58. /* 3.2.8 */
  59. g_assert_cmpint(qpci_config_readb(dev, PCI_LATENCY_TIMER), ==, 0x00);
  60. /* 3.2.9 */
  61. g_assert_cmpint(qpci_config_readb(dev, PCI_HEADER_TYPE), ==, 0x00);
  62. /* 3.2.10 */
  63. g_assert_cmpint(qpci_config_readb(dev, PCI_BIST), ==, 0x00);
  64. /* 3.2.11 */
  65. value = qpci_config_readw(dev, 0x50); /* PMCCFG */
  66. if (s->num_cpus == 1) { /* WPE */
  67. g_assert(!(value & (1 << 15)));
  68. } else {
  69. g_assert((value & (1 << 15)));
  70. }
  71. g_assert(!(value & (1 << 6))); /* EPTE */
  72. /* 3.2.12 */
  73. g_assert_cmpint(qpci_config_readb(dev, 0x52), ==, 0x00); /* DETURBO */
  74. /* 3.2.13 */
  75. #ifndef BROKEN
  76. g_assert_cmpint(qpci_config_readb(dev, 0x53), ==, 0x80); /* DBC */
  77. #endif
  78. /* 3.2.14 */
  79. g_assert_cmpint(qpci_config_readb(dev, 0x54), ==, 0x00); /* AXC */
  80. /* 3.2.15 */
  81. g_assert_cmpint(qpci_config_readw(dev, 0x55), ==, 0x0000); /* DRT */
  82. #ifndef BROKEN
  83. /* 3.2.16 */
  84. g_assert_cmpint(qpci_config_readb(dev, 0x57), ==, 0x01); /* DRAMC */
  85. /* 3.2.17 */
  86. g_assert_cmpint(qpci_config_readb(dev, 0x58), ==, 0x10); /* DRAMT */
  87. #endif
  88. /* 3.2.18 */
  89. g_assert_cmpint(qpci_config_readb(dev, 0x59), ==, 0x00); /* PAM0 */
  90. g_assert_cmpint(qpci_config_readb(dev, 0x5A), ==, 0x00); /* PAM1 */
  91. g_assert_cmpint(qpci_config_readb(dev, 0x5B), ==, 0x00); /* PAM2 */
  92. g_assert_cmpint(qpci_config_readb(dev, 0x5C), ==, 0x00); /* PAM3 */
  93. g_assert_cmpint(qpci_config_readb(dev, 0x5D), ==, 0x00); /* PAM4 */
  94. g_assert_cmpint(qpci_config_readb(dev, 0x5E), ==, 0x00); /* PAM5 */
  95. g_assert_cmpint(qpci_config_readb(dev, 0x5F), ==, 0x00); /* PAM6 */
  96. #ifndef BROKEN
  97. /* 3.2.19 */
  98. g_assert_cmpint(qpci_config_readb(dev, 0x60), ==, 0x01); /* DRB0 */
  99. g_assert_cmpint(qpci_config_readb(dev, 0x61), ==, 0x01); /* DRB1 */
  100. g_assert_cmpint(qpci_config_readb(dev, 0x62), ==, 0x01); /* DRB2 */
  101. g_assert_cmpint(qpci_config_readb(dev, 0x63), ==, 0x01); /* DRB3 */
  102. g_assert_cmpint(qpci_config_readb(dev, 0x64), ==, 0x01); /* DRB4 */
  103. g_assert_cmpint(qpci_config_readb(dev, 0x65), ==, 0x01); /* DRB5 */
  104. g_assert_cmpint(qpci_config_readb(dev, 0x66), ==, 0x01); /* DRB6 */
  105. g_assert_cmpint(qpci_config_readb(dev, 0x67), ==, 0x01); /* DRB7 */
  106. #endif
  107. /* 3.2.20 */
  108. g_assert_cmpint(qpci_config_readb(dev, 0x68), ==, 0x00); /* FDHC */
  109. /* 3.2.21 */
  110. g_assert_cmpint(qpci_config_readb(dev, 0x70), ==, 0x00); /* MTT */
  111. #ifndef BROKEN
  112. /* 3.2.22 */
  113. g_assert_cmpint(qpci_config_readb(dev, 0x71), ==, 0x10); /* CLT */
  114. #endif
  115. /* 3.2.23 */
  116. g_assert_cmpint(qpci_config_readb(dev, 0x72), ==, 0x02); /* SMRAM */
  117. /* 3.2.24 */
  118. g_assert_cmpint(qpci_config_readb(dev, 0x90), ==, 0x00); /* ERRCMD */
  119. /* 3.2.25 */
  120. g_assert_cmpint(qpci_config_readb(dev, 0x91), ==, 0x00); /* ERRSTS */
  121. /* 3.2.26 */
  122. g_assert_cmpint(qpci_config_readb(dev, 0x93), ==, 0x00); /* TRC */
  123. g_free(dev);
  124. qpci_free_pc(bus);
  125. qtest_end();
  126. }
  127. #define PAM_RE 1
  128. #define PAM_WE 2
  129. static void pam_set(QPCIDevice *dev, int index, int flags)
  130. {
  131. int regno = 0x59 + (index / 2);
  132. uint8_t reg;
  133. reg = qpci_config_readb(dev, regno);
  134. if (index & 1) {
  135. reg = (reg & 0x0F) | (flags << 4);
  136. } else {
  137. reg = (reg & 0xF0) | flags;
  138. }
  139. qpci_config_writeb(dev, regno, reg);
  140. }
  141. static gboolean verify_area(uint32_t start, uint32_t end, uint8_t value)
  142. {
  143. uint32_t size = end - start + 1;
  144. gboolean ret = TRUE;
  145. uint8_t *data;
  146. int i;
  147. data = g_malloc0(size);
  148. memread(start, data, size);
  149. g_test_message("verify_area: data[0] = 0x%x", data[0]);
  150. for (i = 0; i < size; i++) {
  151. if (data[i] != value) {
  152. ret = FALSE;
  153. break;
  154. }
  155. }
  156. g_free(data);
  157. return ret;
  158. }
  159. static void write_area(uint32_t start, uint32_t end, uint8_t value)
  160. {
  161. uint32_t size = end - start + 1;
  162. uint8_t *data;
  163. data = g_malloc(size);
  164. memset(data, value, size);
  165. memwrite(start, data, size);
  166. g_free(data);
  167. }
  168. static void test_i440fx_pam(gconstpointer opaque)
  169. {
  170. const TestData *s = opaque;
  171. QPCIBus *bus;
  172. QPCIDevice *dev;
  173. int i;
  174. static struct {
  175. uint32_t start;
  176. uint32_t end;
  177. } pam_area[] = {
  178. { 0, 0 }, /* Reserved */
  179. { 0xF0000, 0xFFFFF }, /* BIOS Area */
  180. { 0xC0000, 0xC3FFF }, /* Option ROM */
  181. { 0xC4000, 0xC7FFF }, /* Option ROM */
  182. { 0xC8000, 0xCBFFF }, /* Option ROM */
  183. { 0xCC000, 0xCFFFF }, /* Option ROM */
  184. { 0xD0000, 0xD3FFF }, /* Option ROM */
  185. { 0xD4000, 0xD7FFF }, /* Option ROM */
  186. { 0xD8000, 0xDBFFF }, /* Option ROM */
  187. { 0xDC000, 0xDFFFF }, /* Option ROM */
  188. { 0xE0000, 0xE3FFF }, /* BIOS Extension */
  189. { 0xE4000, 0xE7FFF }, /* BIOS Extension */
  190. { 0xE8000, 0xEBFFF }, /* BIOS Extension */
  191. { 0xEC000, 0xEFFFF }, /* BIOS Extension */
  192. };
  193. bus = test_start_get_bus(s);
  194. dev = qpci_device_find(bus, QPCI_DEVFN(0, 0));
  195. g_assert(dev != NULL);
  196. for (i = 0; i < ARRAY_SIZE(pam_area); i++) {
  197. if (pam_area[i].start == pam_area[i].end) {
  198. continue;
  199. }
  200. g_test_message("Checking area 0x%05x..0x%05x",
  201. pam_area[i].start, pam_area[i].end);
  202. /* Switch to RE for the area */
  203. pam_set(dev, i, PAM_RE);
  204. /* Verify the RAM is all zeros */
  205. g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0));
  206. /* Switch to WE for the area */
  207. pam_set(dev, i, PAM_RE | PAM_WE);
  208. /* Write out a non-zero mask to the full area */
  209. write_area(pam_area[i].start, pam_area[i].end, 0x42);
  210. #ifndef BROKEN
  211. /* QEMU only supports a limited form of PAM */
  212. /* Switch to !RE for the area */
  213. pam_set(dev, i, PAM_WE);
  214. /* Verify the area is not our mask */
  215. g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x42));
  216. #endif
  217. /* Verify the area is our new mask */
  218. g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0x42));
  219. /* Write out a new mask */
  220. write_area(pam_area[i].start, pam_area[i].end, 0x82);
  221. #ifndef BROKEN
  222. /* QEMU only supports a limited form of PAM */
  223. /* Verify the area is not our mask */
  224. g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x82));
  225. /* Switch to RE for the area */
  226. pam_set(dev, i, PAM_RE | PAM_WE);
  227. #endif
  228. /* Verify the area is our new mask */
  229. g_assert(verify_area(pam_area[i].start, pam_area[i].end, 0x82));
  230. /* Reset area */
  231. pam_set(dev, i, 0);
  232. /* Verify the area is not our new mask */
  233. g_assert(!verify_area(pam_area[i].start, pam_area[i].end, 0x82));
  234. }
  235. g_free(dev);
  236. qpci_free_pc(bus);
  237. qtest_end();
  238. }
  239. #define BLOB_SIZE ((size_t)65536)
  240. #define ISA_BIOS_MAXSZ ((size_t)(128 * 1024))
  241. /* Create a blob file, and return its absolute pathname as a dynamically
  242. * allocated string.
  243. * The file is closed before the function returns.
  244. * In case of error, NULL is returned. The function prints the error message.
  245. */
  246. static char *create_blob_file(void)
  247. {
  248. int ret, fd;
  249. char *pathname;
  250. GError *error = NULL;
  251. ret = -1;
  252. fd = g_file_open_tmp("blob_XXXXXX", &pathname, &error);
  253. if (fd == -1) {
  254. fprintf(stderr, "unable to create blob file: %s\n", error->message);
  255. g_error_free(error);
  256. } else {
  257. if (ftruncate(fd, BLOB_SIZE) == -1) {
  258. fprintf(stderr, "ftruncate(\"%s\", %zu): %s\n", pathname,
  259. BLOB_SIZE, strerror(errno));
  260. } else {
  261. void *buf;
  262. buf = mmap(NULL, BLOB_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
  263. if (buf == MAP_FAILED) {
  264. fprintf(stderr, "mmap(\"%s\", %zu): %s\n", pathname, BLOB_SIZE,
  265. strerror(errno));
  266. } else {
  267. size_t i;
  268. for (i = 0; i < BLOB_SIZE; ++i) {
  269. ((uint8_t *)buf)[i] = i;
  270. }
  271. munmap(buf, BLOB_SIZE);
  272. ret = 0;
  273. }
  274. }
  275. close(fd);
  276. if (ret == -1) {
  277. unlink(pathname);
  278. g_free(pathname);
  279. }
  280. }
  281. return ret == -1 ? NULL : pathname;
  282. }
  283. static void test_i440fx_firmware(FirmwareTestFixture *fixture,
  284. gconstpointer user_data)
  285. {
  286. char *fw_pathname, *cmdline;
  287. uint8_t *buf;
  288. size_t i, isa_bios_size;
  289. fw_pathname = create_blob_file();
  290. g_assert(fw_pathname != NULL);
  291. /* Better hope the user didn't put metacharacters in TMPDIR and co. */
  292. cmdline = g_strdup_printf("-S %s%s", fixture->is_bios
  293. ? "-bios "
  294. : "-drive if=pflash,format=raw,file=",
  295. fw_pathname);
  296. g_test_message("qemu cmdline: %s", cmdline);
  297. qtest_start(cmdline);
  298. g_free(cmdline);
  299. /* QEMU has loaded the firmware (because qtest_start() only returns after
  300. * the QMP handshake completes). We must unlink the firmware blob right
  301. * here, because any assertion firing below would leak it in the
  302. * filesystem. This is also the reason why we recreate the blob every time
  303. * this function is invoked.
  304. */
  305. unlink(fw_pathname);
  306. g_free(fw_pathname);
  307. /* check below 4G */
  308. buf = g_malloc0(BLOB_SIZE);
  309. memread(0x100000000ULL - BLOB_SIZE, buf, BLOB_SIZE);
  310. for (i = 0; i < BLOB_SIZE; ++i) {
  311. g_assert_cmphex(buf[i], ==, (uint8_t)i);
  312. }
  313. /* check in ISA space too */
  314. memset(buf, 0, BLOB_SIZE);
  315. isa_bios_size = ISA_BIOS_MAXSZ < BLOB_SIZE ? ISA_BIOS_MAXSZ : BLOB_SIZE;
  316. memread(0x100000 - isa_bios_size, buf, isa_bios_size);
  317. for (i = 0; i < isa_bios_size; ++i) {
  318. g_assert_cmphex(buf[i], ==,
  319. (uint8_t)((BLOB_SIZE - isa_bios_size) + i));
  320. }
  321. g_free(buf);
  322. qtest_end();
  323. }
  324. static void add_firmware_test(const char *testpath,
  325. void (*setup_fixture)(FirmwareTestFixture *f,
  326. gconstpointer test_data))
  327. {
  328. qtest_add(testpath, FirmwareTestFixture, NULL, setup_fixture,
  329. test_i440fx_firmware, NULL);
  330. }
  331. static void request_bios(FirmwareTestFixture *fixture,
  332. gconstpointer user_data)
  333. {
  334. fixture->is_bios = true;
  335. }
  336. static void request_pflash(FirmwareTestFixture *fixture,
  337. gconstpointer user_data)
  338. {
  339. fixture->is_bios = false;
  340. }
  341. int main(int argc, char **argv)
  342. {
  343. TestData data;
  344. g_test_init(&argc, &argv, NULL);
  345. data.num_cpus = 1;
  346. qtest_add_data_func("i440fx/defaults", &data, test_i440fx_defaults);
  347. qtest_add_data_func("i440fx/pam", &data, test_i440fx_pam);
  348. add_firmware_test("i440fx/firmware/bios", request_bios);
  349. add_firmware_test("i440fx/firmware/pflash", request_pflash);
  350. return g_test_run();
  351. }