Merge remote-tracking branch 'remotes/juanquintela/tags/check/20180827' into staging
[qemu.git] / tests / tpm-crb-test.c
blobd8f956920384d8e72c15a68ba1697691c87cd029
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 "tpm-emu.h"
21 #define TPM_CMD "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00"
23 static void tpm_crb_test(const void *data)
25 const TestState *s = data;
26 uint32_t intfid = readl(TPM_CRB_ADDR_BASE + A_CRB_INTF_ID);
27 uint32_t csize = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_SIZE);
28 uint64_t caddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR);
29 uint32_t rsize = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_SIZE);
30 uint64_t raddr = readq(TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR);
31 uint8_t locstate = readb(TPM_CRB_ADDR_BASE + A_CRB_LOC_STATE);
32 uint32_t locctrl = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL);
33 uint32_t locsts = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_STS);
34 uint32_t sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
36 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceType), ==, 1);
37 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceVersion), ==, 1);
38 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapLocality), ==, 0);
39 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapCRBIdleBypass), ==, 0);
40 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapDataXferSizeSupport),
41 ==, 3);
42 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapFIFO), ==, 0);
43 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, CapCRB), ==, 1);
44 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, InterfaceSelector), ==, 1);
45 g_assert_cmpint(FIELD_EX32(intfid, CRB_INTF_ID, RID), ==, 0);
47 g_assert_cmpint(csize, >=, 128);
48 g_assert_cmpint(rsize, >=, 128);
49 g_assert_cmpint(caddr, >, TPM_CRB_ADDR_BASE);
50 g_assert_cmpint(raddr, >, TPM_CRB_ADDR_BASE);
52 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmEstablished), ==, 1);
53 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, locAssigned), ==, 0);
54 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, activeLocality), ==, 0);
55 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, reserved), ==, 0);
56 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmRegValidSts), ==, 1);
58 g_assert_cmpint(locctrl, ==, 0);
60 g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, Granted), ==, 0);
61 g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, beenSeized), ==, 0);
63 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 1);
64 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0);
66 /* request access to locality 0 */
67 writeb(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1);
69 /* granted bit must be set now */
70 locsts = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_STS);
71 g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, Granted), ==, 1);
72 g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, beenSeized), ==, 0);
74 /* we must have an assigned locality */
75 locstate = readb(TPM_CRB_ADDR_BASE + A_CRB_LOC_STATE);
76 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmEstablished), ==, 1);
77 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, locAssigned), ==, 1);
78 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, activeLocality), ==, 0);
79 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, reserved), ==, 0);
80 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmRegValidSts), ==, 1);
82 /* set into ready state */
83 writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_REQ, 1);
85 /* TPM must not be in the idle state */
86 sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
87 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 0);
88 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0);
90 memwrite(caddr, TPM_CMD, sizeof(TPM_CMD));
92 uint32_t start = 1;
93 uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
94 writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start);
95 do {
96 start = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
97 if ((start & 1) == 0) {
98 break;
100 } while (g_get_monotonic_time() < end_time);
101 start = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
102 g_assert_cmpint(start & 1, ==, 0);
104 /* TPM must still not be in the idle state */
105 sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
106 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 0);
107 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0);
109 struct tpm_hdr tpm_msg;
110 memread(raddr, &tpm_msg, sizeof(tpm_msg));
111 g_assert_cmpmem(&tpm_msg, sizeof(tpm_msg), s->tpm_msg, sizeof(*s->tpm_msg));
113 /* set TPM into idle state */
114 writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_REQ, 2);
116 /* idle state must be indicated now */
117 sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
118 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 1);
119 g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0);
121 /* relinquish locality */
122 writel(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 2);
124 /* Granted flag must be cleared */
125 sts = readl(TPM_CRB_ADDR_BASE + A_CRB_LOC_STS);
126 g_assert_cmpint(FIELD_EX32(sts, CRB_LOC_STS, Granted), ==, 0);
127 g_assert_cmpint(FIELD_EX32(sts, CRB_LOC_STS, beenSeized), ==, 0);
129 /* no locality may be assigned */
130 locstate = readb(TPM_CRB_ADDR_BASE + A_CRB_LOC_STATE);
131 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmEstablished), ==, 1);
132 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, locAssigned), ==, 0);
133 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, activeLocality), ==, 0);
134 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, reserved), ==, 0);
135 g_assert_cmpint(FIELD_EX32(locstate, CRB_LOC_STATE, tpmRegValidSts), ==, 1);
139 int main(int argc, char **argv)
141 int ret;
142 char *args, *tmp_path = g_dir_make_tmp("qemu-tpm-crb-test.XXXXXX", NULL);
143 GThread *thread;
144 TestState test;
146 module_call_init(MODULE_INIT_QOM);
147 g_test_init(&argc, &argv, NULL);
149 test.addr = g_new0(SocketAddress, 1);
150 test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
151 test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
152 g_mutex_init(&test.data_mutex);
153 g_cond_init(&test.data_cond);
155 thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
156 tpm_emu_test_wait_cond(&test);
158 args = g_strdup_printf(
159 "-chardev socket,id=chr,path=%s "
160 "-tpmdev emulator,id=dev,chardev=chr "
161 "-device tpm-crb,tpmdev=dev",
162 test.addr->u.q_unix.path);
163 qtest_start(args);
165 qtest_add_data_func("/tpm-crb/test", &test, tpm_crb_test);
166 ret = g_test_run();
168 qtest_end();
170 g_thread_join(thread);
171 g_unlink(test.addr->u.q_unix.path);
172 qapi_free_SocketAddress(test.addr);
173 g_rmdir(tmp_path);
174 g_free(tmp_path);
175 g_free(args);
176 return ret;