2 * QTest testcase for TPM TIS
4 * Copyright (c) 2018 Red Hat, Inc.
5 * Copyright (c) 2018 IBM Corporation
8 * Marc-André Lureau <marcandre.lureau@redhat.com>
9 * Stefan Berger <stefanb@linux.vnet.ibm.com>
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.
15 #include "qemu/osdep.h"
16 #include <glib/gstdio.h>
18 #include "hw/acpi/tpm.h"
19 #include "io/channel-socket.h"
20 #include "libqtest-single.h"
21 #include "qemu/module.h"
24 #define TIS_REG(LOCTY, REG) \
25 (TPM_TIS_ADDR_BASE + ((LOCTY) << 12) + REG)
27 #define DEBUG_TIS_TEST 0
29 #define DPRINTF(fmt, ...) do { \
30 if (DEBUG_TIS_TEST) { \
31 printf(fmt, ## __VA_ARGS__); \
35 #define DPRINTF_ACCESS \
36 DPRINTF("%s: %d: locty=%d l=%d access=0x%02x pending_request_flag=0x%x\n", \
37 __func__, __LINE__, locty, l, access, pending_request_flag)
40 DPRINTF("%s: %d: sts = 0x%08x\n", __func__, __LINE__, sts)
42 static const uint8_t TPM_CMD
[12] =
43 "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00";
45 static void tpm_tis_test_check_localities(const void *data
)
54 for (locty
= 0; locty
< TPM_TIS_NUM_LOCALITIES
; locty
++) {
55 access
= readb(TIS_REG(0, TPM_TIS_REG_ACCESS
));
56 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
57 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
59 capability
= readl(TIS_REG(locty
, TPM_TIS_REG_INTF_CAPABILITY
));
60 g_assert_cmpint(capability
, ==, TPM_TIS_CAPABILITIES_SUPPORTED2_0
);
62 ifaceid
= readl(TIS_REG(locty
, TPM_TIS_REG_INTERFACE_ID
));
63 g_assert_cmpint(ifaceid
, ==, TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0
);
65 didvid
= readl(TIS_REG(locty
, TPM_TIS_REG_DID_VID
));
66 g_assert_cmpint(didvid
, !=, 0);
67 g_assert_cmpint(didvid
, !=, 0xffffffff);
69 rid
= readl(TIS_REG(locty
, TPM_TIS_REG_RID
));
70 g_assert_cmpint(rid
, !=, 0);
71 g_assert_cmpint(rid
, !=, 0xffffffff);
75 static void tpm_tis_test_check_access_reg(const void *data
)
80 /* do not test locality 4 (hw only) */
81 for (locty
= 0; locty
< TPM_TIS_NUM_LOCALITIES
- 1; locty
++) {
82 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
83 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
84 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
86 /* request use of locality */
87 writeb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_REQUEST_USE
);
89 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
90 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
91 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
92 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
95 writeb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
),
96 TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
97 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
98 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
99 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
104 * Test case for seizing access by a higher number locality
106 static void tpm_tis_test_check_access_reg_seize(const void *data
)
110 uint8_t pending_request_flag
;
112 /* do not test locality 4 (hw only) */
113 for (locty
= 0; locty
< TPM_TIS_NUM_LOCALITIES
- 1; locty
++) {
114 pending_request_flag
= 0;
116 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
117 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
118 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
120 /* request use of locality */
121 writeb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_REQUEST_USE
);
122 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
123 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
124 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
125 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
127 /* lower localities cannot seize access */
128 for (l
= 0; l
< locty
; l
++) {
129 /* lower locality is not active */
130 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
132 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
133 pending_request_flag
|
134 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
136 /* try to request use from 'l' */
137 writeb(TIS_REG(l
, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_REQUEST_USE
);
139 /* requesting use from 'l' was not possible;
140 we must see REQUEST_USE and possibly PENDING_REQUEST */
141 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
143 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
144 TPM_TIS_ACCESS_REQUEST_USE
|
145 pending_request_flag
|
146 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
148 /* locality 'locty' must be unchanged;
149 we must see PENDING_REQUEST */
150 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
151 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
152 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
153 TPM_TIS_ACCESS_PENDING_REQUEST
|
154 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
156 /* try to seize from 'l' */
157 writeb(TIS_REG(l
, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_SEIZE
);
158 /* seize from 'l' was not possible */
159 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
161 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
162 TPM_TIS_ACCESS_REQUEST_USE
|
163 pending_request_flag
|
164 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
166 /* locality 'locty' must be unchanged */
167 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
168 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
169 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
170 TPM_TIS_ACCESS_PENDING_REQUEST
|
171 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
173 /* on the next loop we will have a PENDING_REQUEST flag
174 set for locality 'l' */
175 pending_request_flag
= TPM_TIS_ACCESS_PENDING_REQUEST
;
178 /* higher localities can 'seize' access but not 'request use';
179 note: this will activate first l+1, then l+2 etc. */
180 for (l
= locty
+ 1; l
< TPM_TIS_NUM_LOCALITIES
- 1; l
++) {
181 /* try to 'request use' from 'l' */
182 writeb(TIS_REG(l
, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_REQUEST_USE
);
184 /* requesting use from 'l' was not possible; we should see
185 REQUEST_USE and may see PENDING_REQUEST */
186 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
188 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
189 TPM_TIS_ACCESS_REQUEST_USE
|
190 pending_request_flag
|
191 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
193 /* locality 'l-1' must be unchanged; we should always
194 see PENDING_REQUEST from 'l' requesting access */
195 access
= readb(TIS_REG(l
- 1, TPM_TIS_REG_ACCESS
));
197 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
198 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
199 TPM_TIS_ACCESS_PENDING_REQUEST
|
200 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
202 /* try to seize from 'l' */
203 writeb(TIS_REG(l
, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_SEIZE
);
205 /* seize from 'l' was possible */
206 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
208 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
209 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
210 pending_request_flag
|
211 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
213 /* l - 1 should show that it has BEEN_SEIZED */
214 access
= readb(TIS_REG(l
- 1, TPM_TIS_REG_ACCESS
));
216 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
217 TPM_TIS_ACCESS_BEEN_SEIZED
|
218 pending_request_flag
|
219 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
221 /* clear the BEEN_SEIZED flag and make sure it's gone */
222 writeb(TIS_REG(l
- 1, TPM_TIS_REG_ACCESS
),
223 TPM_TIS_ACCESS_BEEN_SEIZED
);
225 access
= readb(TIS_REG(l
- 1, TPM_TIS_REG_ACCESS
));
226 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
227 pending_request_flag
|
228 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
231 /* PENDING_REQUEST will not be set if locty = 0 since all localities
232 were active; in case of locty = 1, locality 0 will be active
233 but no PENDING_REQUEST anywhere */
235 pending_request_flag
= 0;
238 /* release access from l - 1; this activates locty - 1 */
241 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
244 DPRINTF("%s: %d: relinquishing control on l = %d\n",
245 __func__
, __LINE__
, l
);
246 writeb(TIS_REG(l
, TPM_TIS_REG_ACCESS
),
247 TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
249 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
251 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
252 pending_request_flag
|
253 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
255 for (l
= locty
- 1; l
>= 0; l
--) {
256 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
258 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
259 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
260 pending_request_flag
|
261 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
263 /* release this locality */
264 writeb(TIS_REG(l
, TPM_TIS_REG_ACCESS
),
265 TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
268 pending_request_flag
= 0;
272 /* no locality may be active now */
273 for (l
= 0; l
< TPM_TIS_NUM_LOCALITIES
- 1; l
++) {
274 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
276 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
277 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
283 * Test case for getting access when higher number locality relinquishes access
285 static void tpm_tis_test_check_access_reg_release(const void *data
)
289 uint8_t pending_request_flag
;
291 /* do not test locality 4 (hw only) */
292 for (locty
= TPM_TIS_NUM_LOCALITIES
- 2; locty
>= 0; locty
--) {
293 pending_request_flag
= 0;
295 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
296 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
297 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
299 /* request use of locality */
300 writeb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_REQUEST_USE
);
301 access
= readb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
));
302 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
303 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
304 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
306 /* request use of all other localities */
307 for (l
= 0; l
< TPM_TIS_NUM_LOCALITIES
- 1; l
++) {
311 /* request use of locality 'l' -- we MUST see REQUEST USE and
312 may see PENDING_REQUEST */
313 writeb(TIS_REG(l
, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_REQUEST_USE
);
314 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
316 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
317 TPM_TIS_ACCESS_REQUEST_USE
|
318 pending_request_flag
|
319 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
320 pending_request_flag
= TPM_TIS_ACCESS_PENDING_REQUEST
;
322 /* release locality 'locty' */
323 writeb(TIS_REG(locty
, TPM_TIS_REG_ACCESS
),
324 TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
325 /* highest locality should now be active; release it and make sure the
326 next higest locality is active afterwards */
327 for (l
= TPM_TIS_NUM_LOCALITIES
- 2; l
>= 0; l
--) {
331 /* 'l' should be active now */
332 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
334 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
335 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
336 pending_request_flag
|
337 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
338 /* 'l' relinquishes access */
339 writeb(TIS_REG(l
, TPM_TIS_REG_ACCESS
),
340 TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
341 access
= readb(TIS_REG(l
, TPM_TIS_REG_ACCESS
));
343 if (l
== 1 || (locty
<= 1 && l
== 2)) {
344 pending_request_flag
= 0;
346 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
347 pending_request_flag
|
348 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
354 * Test case for transmitting packets
356 static void tpm_tis_test_check_transmit(const void *data
)
358 const TestState
*s
= data
;
364 /* request use of locality 0 */
365 writeb(TIS_REG(0, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_REQUEST_USE
);
366 access
= readb(TIS_REG(0, TPM_TIS_REG_ACCESS
));
367 g_assert_cmpint(access
, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS
|
368 TPM_TIS_ACCESS_ACTIVE_LOCALITY
|
369 TPM_TIS_ACCESS_TPM_ESTABLISHMENT
);
371 sts
= readl(TIS_REG(0, TPM_TIS_REG_STS
));
374 g_assert_cmpint(sts
& 0xff, ==, 0);
375 g_assert_cmpint(sts
& TPM_TIS_STS_TPM_FAMILY_MASK
, ==,
376 TPM_TIS_STS_TPM_FAMILY2_0
);
378 bcount
= (sts
>> 8) & 0xffff;
379 g_assert_cmpint(bcount
, >=, 128);
381 writel(TIS_REG(0, TPM_TIS_REG_STS
), TPM_TIS_STS_COMMAND_READY
);
382 sts
= readl(TIS_REG(0, TPM_TIS_REG_STS
));
384 g_assert_cmpint(sts
& 0xff, ==, TPM_TIS_STS_COMMAND_READY
);
386 /* transmit command */
387 for (i
= 0; i
< sizeof(TPM_CMD
); i
++) {
388 writeb(TIS_REG(0, TPM_TIS_REG_DATA_FIFO
), TPM_CMD
[i
]);
389 sts
= readl(TIS_REG(0, TPM_TIS_REG_STS
));
391 if (i
< sizeof(TPM_CMD
) - 1) {
392 g_assert_cmpint(sts
& 0xff, ==,
393 TPM_TIS_STS_EXPECT
| TPM_TIS_STS_VALID
);
395 g_assert_cmpint(sts
& 0xff, ==, TPM_TIS_STS_VALID
);
397 g_assert_cmpint((sts
>> 8) & 0xffff, ==, --bcount
);
399 /* start processing */
400 writeb(TIS_REG(0, TPM_TIS_REG_STS
), TPM_TIS_STS_TPM_GO
);
402 uint64_t end_time
= g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND
;
404 sts
= readl(TIS_REG(0, TPM_TIS_REG_STS
));
405 if ((sts
& TPM_TIS_STS_DATA_AVAILABLE
) != 0) {
408 } while (g_get_monotonic_time() < end_time
);
410 sts
= readl(TIS_REG(0, TPM_TIS_REG_STS
));
412 g_assert_cmpint(sts
& 0xff, == ,
413 TPM_TIS_STS_VALID
| TPM_TIS_STS_DATA_AVAILABLE
);
414 bcount
= (sts
>> 8) & 0xffff;
417 uint8_t tpm_msg
[sizeof(struct tpm_hdr
)];
418 g_assert_cmpint(sizeof(tpm_msg
), ==, bcount
);
420 for (i
= 0; i
< sizeof(tpm_msg
); i
++) {
421 tpm_msg
[i
] = readb(TIS_REG(0, TPM_TIS_REG_DATA_FIFO
));
422 sts
= readl(TIS_REG(0, TPM_TIS_REG_STS
));
424 if (sts
& TPM_TIS_STS_DATA_AVAILABLE
) {
425 g_assert_cmpint((sts
>> 8) & 0xffff, ==, --bcount
);
428 g_assert_cmpmem(tpm_msg
, sizeof(tpm_msg
), s
->tpm_msg
, sizeof(*s
->tpm_msg
));
430 /* relinquish use of locality 0 */
431 writeb(TIS_REG(0, TPM_TIS_REG_ACCESS
), TPM_TIS_ACCESS_ACTIVE_LOCALITY
);
432 access
= readb(TIS_REG(0, TPM_TIS_REG_ACCESS
));
435 int main(int argc
, char **argv
)
438 char *args
, *tmp_path
= g_dir_make_tmp("qemu-tpm-tis-test.XXXXXX", NULL
);
442 module_call_init(MODULE_INIT_QOM
);
443 g_test_init(&argc
, &argv
, NULL
);
445 test
.addr
= g_new0(SocketAddress
, 1);
446 test
.addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
447 test
.addr
->u
.q_unix
.path
= g_build_filename(tmp_path
, "sock", NULL
);
448 g_mutex_init(&test
.data_mutex
);
449 g_cond_init(&test
.data_cond
);
450 test
.data_cond_signal
= false;
452 thread
= g_thread_new(NULL
, tpm_emu_ctrl_thread
, &test
);
453 tpm_emu_test_wait_cond(&test
);
455 args
= g_strdup_printf(
456 "-chardev socket,id=chr,path=%s "
457 "-tpmdev emulator,id=dev,chardev=chr "
458 "-device tpm-tis,tpmdev=dev",
459 test
.addr
->u
.q_unix
.path
);
462 qtest_add_data_func("/tpm-tis/test_check_localities", &test
,
463 tpm_tis_test_check_localities
);
465 qtest_add_data_func("/tpm-tis/test_check_access_reg", &test
,
466 tpm_tis_test_check_access_reg
);
468 qtest_add_data_func("/tpm-tis/test_check_access_reg_seize", &test
,
469 tpm_tis_test_check_access_reg_seize
);
471 qtest_add_data_func("/tpm-tis/test_check_access_reg_release", &test
,
472 tpm_tis_test_check_access_reg_release
);
474 qtest_add_data_func("/tpm-tis/test_check_transmit", &test
,
475 tpm_tis_test_check_transmit
);
481 g_thread_join(thread
);
482 g_unlink(test
.addr
->u
.q_unix
.path
);
483 qapi_free_SocketAddress(test
.addr
);