migration/savevm: move non SaveStateEntry condition check out of iteration
[qemu/ar7.git] / tests / tpm-crb-test.c
bloba139caa51df17e48a171c2f7198eec3dcf6cb1cb
1 /*
2 * QTest testcase for TPM CRB
4 * Copyright (c) 2018 Red Hat, Inc.
6 * Authors:
7 * Marc-André Lureau <marcandre.lureau@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include <glib/gstdio.h>
16 #include "hw/acpi/tpm.h"
17 #include "io/channel-socket.h"
18 #include "libqtest.h"
19 #include "qemu/module.h"
20 #include "tpm-emu.h"
22 #define TPM_CMD "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00"
24 static void tpm_crb_test(const void *data)
26 const TestState *s = data;
27 uint32_t intfid = readl(TPM_CRB_ADDR_BASE + A_CRB_INTF_ID);
28 uint32_t csize = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_SIZE);
29 uint64_t caddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR);
30 uint32_t rsize = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_SIZE);
31 uint64_t raddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR);
32 uint8_t locstate = readb(TPM_CRB_ADDR_BASE + A_CRB_LOC_STATE);
33 uint32_t locctrl = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL);
34 uint32_t locsts = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_STS);
35 uint32_t sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
37 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceType), ==, 1);
38 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceVersion), ==, 1);
39 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapLocality), ==, 0);
40 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapCRBIdleBypass), ==, 0);
41 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapDataXferSizeSupport),
42 ==, 3);
43 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapFIFO), ==, 0);
44 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapCRB), ==, 1);
45 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceSelector), ==, 1);
46 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, RID), ==, 0);
48 g_assert_cmpint(csize, >=, 128);
49 g_assert_cmpint(rsize, >=, 128);
50 g_assert_cmpint(caddr, >, TPM_CRB_ADDR_BASE);
51 g_assert_cmpint(raddr, >, TPM_CRB_ADDR_BASE);
53 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmEstablished), ==, 1);
54 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, locAssigned), ==, 0);
55 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, activeLocality), ==, 0);
56 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, reserved), ==, 0);
57 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmRegValidSts), ==, 1);
59 g_assert_cmpint(locctrl, ==, 0);
61 g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, Granted), ==, 0);
62 g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, beenSeized), ==, 0);
64 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 1);
65 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0);
67 /* request access to locality 0 */
68 writeb(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1);
70 /* granted bit must be set now */
71 locsts = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_STS);
72 g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, Granted), ==, 1);
73 g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, beenSeized), ==, 0);
75 /* we must have an assigned locality */
76 locstate = readb(TPM_CRB_ADDR_BASE + A_CRB_LOC_STATE);
77 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmEstablished), ==, 1);
78 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, locAssigned), ==, 1);
79 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, activeLocality), ==, 0);
80 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, reserved), ==, 0);
81 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmRegValidSts), ==, 1);
83 /* set into ready state */
84 writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_REQ, 1);
86 /* TPM must not be in the idle state */
87 sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
88 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 0);
89 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0);
91 memwrite(caddr, TPM_CMD, sizeof(TPM_CMD));
93 uint32_t start = 1;
94 uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
95 writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start);
96 do {
97 start = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
98 if ((start & 1) == 0) {
99 break;
101 } while (g_get_monotonic_time() < end_time);
102 start = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
103 g_assert_cmpint(start & 1, ==, 0);
105 /* TPM must still not be in the idle state */
106 sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
107 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 0);
108 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0);
110 struct tpm_hdr tpm_msg;
111 memread(raddr, &tpm_msg, sizeof(tpm_msg));
112 g_assert_cmpmem(&tpm_msg, sizeof(tpm_msg), s->tpm_msg, sizeof(*s->tpm_msg));
114 /* set TPM into idle state */
115 writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_REQ, 2);
117 /* idle state must be indicated now */
118 sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
119 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 1);
120 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0);
122 /* relinquish locality */
123 writel(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 2);
125 /* Granted flag must be cleared */
126 sts = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_STS);
127 g_assert_cmpint(FIELD_EX32(sts, CRB_LOC_STS, Granted), ==, 0);
128 g_assert_cmpint(FIELD_EX32(sts, CRB_LOC_STS, beenSeized), ==, 0);
130 /* no locality may be assigned */
131 locstate = readb(TPM_CRB_ADDR_BASE + A_CRB_LOC_STATE);
132 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmEstablished), ==, 1);
133 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, locAssigned), ==, 0);
134 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, activeLocality), ==, 0);
135 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, reserved), ==, 0);
136 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmRegValidSts), ==, 1);
140 int main(int argc, char **argv)
142 int ret;
143 char *args, *tmp_path = g_dir_make_tmp("qemu-tpm-crb-test.XXXXXX", NULL);
144 GThread *thread;
145 TestState test;
147 module_call_init(MODULE_INIT_QOM);
148 g_test_init(&argc, &argv, NULL);
150 test.addr = g_new0(SocketAddress, 1);
151 test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
152 test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
153 g_mutex_init(&test.data_mutex);
154 g_cond_init(&test.data_cond);
155 test.data_cond_signal = false;
157 thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
158 tpm_emu_test_wait_cond(&test);
160 args = g_strdup_printf(
161 "-chardev socket,id=chr,path=%s "
162 "-tpmdev emulator,id=dev,chardev=chr "
163 "-device tpm-crb,tpmdev=dev",
164 test.addr->u.q_unix.path);
165 qtest_start(args);
167 qtest_add_data_func("/tpm-crb/test", &test, tpm_crb_test);
168 ret = g_test_run();
170 qtest_end();
172 g_thread_join(thread);
173 g_unlink(test.addr->u.q_unix.path);
174 qapi_free_SocketAddress(test.addr);
175 g_rmdir(tmp_path);
176 g_free(tmp_path);
177 g_free(args);
178 return ret;