]>
Commit | Line | Data |
---|---|---|
4ab6cb4c MAL |
1 | /* |
2 | * QTest testcase for TPM CRB | |
3 | * | |
4 | * Copyright (c) 2018 Red Hat, Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Marc-André Lureau <[email protected]> | |
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 | #include "qemu/osdep.h" | |
14 | #include <glib/gstdio.h> | |
15 | ||
16 | #include "hw/acpi/tpm.h" | |
4ab6cb4c MAL |
17 | #include "io/channel-socket.h" |
18 | #include "libqtest.h" | |
0e6ca954 | 19 | #include "tpm-emu.h" |
4ab6cb4c MAL |
20 | |
21 | #define TPM_CMD "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00" | |
22 | ||
23 | static void tpm_crb_test(const void *data) | |
24 | { | |
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); | |
4d0d1c07 SB |
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); | |
4ab6cb4c MAL |
35 | |
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); | |
46 | ||
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); | |
51 | ||
4d0d1c07 SB |
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); | |
57 | ||
58 | g_assert_cmpint(locctrl, ==, 0); | |
59 | ||
60 | g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, Granted), ==, 0); | |
61 | g_assert_cmpint(FIELD_EX32(locsts, CRB_LOC_STS, beenSeized), ==, 0); | |
62 | ||
63 | g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 1); | |
64 | g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0); | |
65 | ||
66 | /* request access to locality 0 */ | |
67 | writeb(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1); | |
68 | ||
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); | |
73 | ||
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); | |
81 | ||
82 | /* set into ready state */ | |
83 | writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_REQ, 1); | |
84 | ||
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); | |
89 | ||
4ab6cb4c MAL |
90 | memwrite(caddr, TPM_CMD, sizeof(TPM_CMD)); |
91 | ||
4d0d1c07 | 92 | uint32_t start = 1; |
4ab6cb4c MAL |
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; | |
99 | } | |
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); | |
4d0d1c07 SB |
103 | |
104 | /* TPM must still not be in the idle state */ | |
4ab6cb4c | 105 | sts = readl(TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS); |
4d0d1c07 SB |
106 | g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmIdle), ==, 0); |
107 | g_assert_cmpint(FIELD_EX32(sts, CRB_CTRL_STS, tpmSts), ==, 0); | |
4ab6cb4c MAL |
108 | |
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)); | |
4d0d1c07 SB |
112 | |
113 | /* set TPM into idle state */ | |
114 | writel(TPM_CRB_ADDR_BASE + A_CRB_CTRL_REQ, 2); | |
115 | ||
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); | |
120 | ||
121 | /* relinquish locality */ | |
122 | writel(TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 2); | |
123 | ||
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); | |
128 | ||
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); | |
136 | ||
4ab6cb4c MAL |
137 | } |
138 | ||
139 | int main(int argc, char **argv) | |
140 | { | |
141 | int ret; | |
142 | char *args, *tmp_path = g_dir_make_tmp("qemu-tpm-crb-test.XXXXXX", NULL); | |
143 | GThread *thread; | |
144 | TestState test; | |
145 | ||
146 | module_call_init(MODULE_INIT_QOM); | |
147 | g_test_init(&argc, &argv, NULL); | |
148 | ||
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); | |
2271b75f | 154 | test.data_cond_signal = false; |
4ab6cb4c | 155 | |
0e6ca954 SB |
156 | thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test); |
157 | tpm_emu_test_wait_cond(&test); | |
4ab6cb4c MAL |
158 | |
159 | args = g_strdup_printf( | |
160 | "-chardev socket,id=chr,path=%s " | |
161 | "-tpmdev emulator,id=dev,chardev=chr " | |
162 | "-device tpm-crb,tpmdev=dev", | |
163 | test.addr->u.q_unix.path); | |
164 | qtest_start(args); | |
165 | ||
166 | qtest_add_data_func("/tpm-crb/test", &test, tpm_crb_test); | |
167 | ret = g_test_run(); | |
168 | ||
169 | qtest_end(); | |
170 | ||
171 | g_thread_join(thread); | |
172 | g_unlink(test.addr->u.q_unix.path); | |
173 | qapi_free_SocketAddress(test.addr); | |
174 | g_rmdir(tmp_path); | |
175 | g_free(tmp_path); | |
176 | g_free(args); | |
177 | return ret; | |
178 | } |