xref: /openbmc/qemu/tests/qtest/pvpanic-pci-test.c (revision a6caeee8)
1 /*
2  * QTest testcase for PV Panic PCI device
3  *
4  * Copyright (C) 2020 Oracle
5  *
6  * Authors:
7  *     Mihai Carabas <mihai.carabas@oracle.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "libqtest.h"
16 #include "qapi/qmp/qdict.h"
17 #include "libqos/pci.h"
18 #include "libqos/pci-pc.h"
19 #include "hw/pci/pci_regs.h"
20 
21 static void test_panic_nopause(void)
22 {
23     uint8_t val;
24     QDict *response, *data;
25     QTestState *qts;
26     QPCIBus *pcibus;
27     QPCIDevice *dev;
28     QPCIBar bar;
29 
30     qts = qtest_init("-device pvpanic-pci,addr=04.0 -action panic=none");
31     pcibus = qpci_new_pc(qts, NULL);
32     dev = qpci_device_find(pcibus, QPCI_DEVFN(0x4, 0x0));
33     qpci_device_enable(dev);
34     bar = qpci_iomap(dev, 0, NULL);
35 
36     qpci_memread(dev, bar, 0, &val, sizeof(val));
37     g_assert_cmpuint(val, ==, 3);
38 
39     val = 1;
40     qpci_memwrite(dev, bar, 0, &val, sizeof(val));
41 
42     response = qtest_qmp_eventwait_ref(qts, "GUEST_PANICKED");
43     g_assert(qdict_haskey(response, "data"));
44     data = qdict_get_qdict(response, "data");
45     g_assert(qdict_haskey(data, "action"));
46     g_assert_cmpstr(qdict_get_str(data, "action"), ==, "run");
47     qobject_unref(response);
48 
49     g_free(dev);
50     qpci_free_pc(pcibus);
51     qtest_quit(qts);
52 }
53 
54 static void test_panic(void)
55 {
56     uint8_t val;
57     QDict *response, *data;
58     QTestState *qts;
59     QPCIBus *pcibus;
60     QPCIDevice *dev;
61     QPCIBar bar;
62 
63     qts = qtest_init("-device pvpanic-pci,addr=04.0 -action panic=pause");
64     pcibus = qpci_new_pc(qts, NULL);
65     dev = qpci_device_find(pcibus, QPCI_DEVFN(0x4, 0x0));
66     qpci_device_enable(dev);
67     bar = qpci_iomap(dev, 0, NULL);
68 
69     qpci_memread(dev, bar, 0, &val, sizeof(val));
70     g_assert_cmpuint(val, ==, 3);
71 
72     val = 1;
73     qpci_memwrite(dev, bar, 0, &val, sizeof(val));
74 
75     response = qtest_qmp_eventwait_ref(qts, "GUEST_PANICKED");
76     g_assert(qdict_haskey(response, "data"));
77     data = qdict_get_qdict(response, "data");
78     g_assert(qdict_haskey(data, "action"));
79     g_assert_cmpstr(qdict_get_str(data, "action"), ==, "pause");
80     qobject_unref(response);
81 
82     g_free(dev);
83     qpci_free_pc(pcibus);
84     qtest_quit(qts);
85 }
86 
87 int main(int argc, char **argv)
88 {
89     int ret;
90 
91     g_test_init(&argc, &argv, NULL);
92     qtest_add_func("/pvpanic-pci/panic", test_panic);
93     qtest_add_func("/pvpanic-pci/panic-nopause", test_panic_nopause);
94 
95     ret = g_test_run();
96 
97     return ret;
98 }
99