update seabios to 1.12
[qemu/kevin.git] / tests / tpm-tis-test.c
blobc8ec14888ffe7ddd04df689b4219a797d6ec54e7
1 /*
2 * QTest testcase for TPM TIS
4 * Copyright (c) 2018 Red Hat, Inc.
5 * Copyright (c) 2018 IBM Corporation
7 * Authors:
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.h"
21 #include "tpm-emu.h"
23 #define TIS_REG(LOCTY, REG) \
24 (TPM_TIS_ADDR_BASE + ((LOCTY) << 12) + REG)
26 #define DEBUG_TIS_TEST 0
28 #define DPRINTF(fmt, ...) do { \
29 if (DEBUG_TIS_TEST) { \
30 printf(fmt, ## __VA_ARGS__); \
31 } \
32 } while (0)
34 #define DPRINTF_ACCESS \
35 DPRINTF("%s: %d: locty=%d l=%d access=0x%02x pending_request_flag=0x%x\n", \
36 __func__, __LINE__, locty, l, access, pending_request_flag)
38 #define DPRINTF_STS \
39 DPRINTF("%s: %d: sts = 0x%08x\n", __func__, __LINE__, sts)
41 static const uint8_t TPM_CMD[12] =
42 "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00";
44 static void tpm_tis_test_check_localities(const void *data)
46 uint8_t locty;
47 uint8_t access;
48 uint32_t ifaceid;
49 uint32_t capability;
50 uint32_t didvid;
51 uint32_t rid;
53 for (locty = 0; locty < TPM_TIS_NUM_LOCALITIES; locty++) {
54 access = readb(TIS_REG(0, TPM_TIS_REG_ACCESS));
55 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
56 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
58 capability = readl(TIS_REG(locty, TPM_TIS_REG_INTF_CAPABILITY));
59 g_assert_cmpint(capability, ==, TPM_TIS_CAPABILITIES_SUPPORTED2_0);
61 ifaceid = readl(TIS_REG(locty, TPM_TIS_REG_INTERFACE_ID));
62 g_assert_cmpint(ifaceid, ==, TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0);
64 didvid = readl(TIS_REG(locty, TPM_TIS_REG_DID_VID));
65 g_assert_cmpint(didvid, !=, 0);
66 g_assert_cmpint(didvid, !=, 0xffffffff);
68 rid = readl(TIS_REG(locty, TPM_TIS_REG_RID));
69 g_assert_cmpint(rid, !=, 0);
70 g_assert_cmpint(rid, !=, 0xffffffff);
74 static void tpm_tis_test_check_access_reg(const void *data)
76 uint8_t locty;
77 uint8_t access;
79 /* do not test locality 4 (hw only) */
80 for (locty = 0; locty < TPM_TIS_NUM_LOCALITIES - 1; locty++) {
81 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
82 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
83 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
85 /* request use of locality */
86 writeb(TIS_REG(locty, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
88 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
89 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
90 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
91 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
93 /* release access */
94 writeb(TIS_REG(locty, TPM_TIS_REG_ACCESS),
95 TPM_TIS_ACCESS_ACTIVE_LOCALITY);
96 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
97 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
98 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
103 * Test case for seizing access by a higher number locality
105 static void tpm_tis_test_check_access_reg_seize(const void *data)
107 int locty, l;
108 uint8_t access;
109 uint8_t pending_request_flag;
111 /* do not test locality 4 (hw only) */
112 for (locty = 0; locty < TPM_TIS_NUM_LOCALITIES - 1; locty++) {
113 pending_request_flag = 0;
115 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
116 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
117 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
119 /* request use of locality */
120 writeb(TIS_REG(locty, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
121 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
122 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
123 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
124 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
126 /* lower localities cannot seize access */
127 for (l = 0; l < locty; l++) {
128 /* lower locality is not active */
129 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
130 DPRINTF_ACCESS;
131 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
132 pending_request_flag |
133 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
135 /* try to request use from 'l' */
136 writeb(TIS_REG(l, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
138 /* requesting use from 'l' was not possible;
139 we must see REQUEST_USE and possibly PENDING_REQUEST */
140 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
141 DPRINTF_ACCESS;
142 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
143 TPM_TIS_ACCESS_REQUEST_USE |
144 pending_request_flag |
145 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
147 /* locality 'locty' must be unchanged;
148 we must see PENDING_REQUEST */
149 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
150 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
151 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
152 TPM_TIS_ACCESS_PENDING_REQUEST |
153 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
155 /* try to seize from 'l' */
156 writeb(TIS_REG(l, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_SEIZE);
157 /* seize from 'l' was not possible */
158 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
159 DPRINTF_ACCESS;
160 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
161 TPM_TIS_ACCESS_REQUEST_USE |
162 pending_request_flag |
163 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
165 /* locality 'locty' must be unchanged */
166 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
167 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
168 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
169 TPM_TIS_ACCESS_PENDING_REQUEST |
170 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
172 /* on the next loop we will have a PENDING_REQUEST flag
173 set for locality 'l' */
174 pending_request_flag = TPM_TIS_ACCESS_PENDING_REQUEST;
177 /* higher localities can 'seize' access but not 'request use';
178 note: this will activate first l+1, then l+2 etc. */
179 for (l = locty + 1; l < TPM_TIS_NUM_LOCALITIES - 1; l++) {
180 /* try to 'request use' from 'l' */
181 writeb(TIS_REG(l, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
183 /* requesting use from 'l' was not possible; we should see
184 REQUEST_USE and may see PENDING_REQUEST */
185 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
186 DPRINTF_ACCESS;
187 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
188 TPM_TIS_ACCESS_REQUEST_USE |
189 pending_request_flag |
190 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
192 /* locality 'l-1' must be unchanged; we should always
193 see PENDING_REQUEST from 'l' requesting access */
194 access = readb(TIS_REG(l - 1, TPM_TIS_REG_ACCESS));
195 DPRINTF_ACCESS;
196 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
197 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
198 TPM_TIS_ACCESS_PENDING_REQUEST |
199 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
201 /* try to seize from 'l' */
202 writeb(TIS_REG(l, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_SEIZE);
204 /* seize from 'l' was possible */
205 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
206 DPRINTF_ACCESS;
207 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
208 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
209 pending_request_flag |
210 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
212 /* l - 1 should show that it has BEEN_SEIZED */
213 access = readb(TIS_REG(l - 1, TPM_TIS_REG_ACCESS));
214 DPRINTF_ACCESS;
215 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
216 TPM_TIS_ACCESS_BEEN_SEIZED |
217 pending_request_flag |
218 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
220 /* clear the BEEN_SEIZED flag and make sure it's gone */
221 writeb(TIS_REG(l - 1, TPM_TIS_REG_ACCESS),
222 TPM_TIS_ACCESS_BEEN_SEIZED);
224 access = readb(TIS_REG(l - 1, TPM_TIS_REG_ACCESS));
225 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
226 pending_request_flag |
227 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
230 /* PENDING_REQUEST will not be set if locty = 0 since all localities
231 were active; in case of locty = 1, locality 0 will be active
232 but no PENDING_REQUEST anywhere */
233 if (locty <= 1) {
234 pending_request_flag = 0;
237 /* release access from l - 1; this activates locty - 1 */
238 l--;
240 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
241 DPRINTF_ACCESS;
243 DPRINTF("%s: %d: relinquishing control on l = %d\n",
244 __func__, __LINE__, l);
245 writeb(TIS_REG(l, TPM_TIS_REG_ACCESS),
246 TPM_TIS_ACCESS_ACTIVE_LOCALITY);
248 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
249 DPRINTF_ACCESS;
250 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
251 pending_request_flag |
252 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
254 for (l = locty - 1; l >= 0; l--) {
255 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
256 DPRINTF_ACCESS;
257 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
258 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
259 pending_request_flag |
260 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
262 /* release this locality */
263 writeb(TIS_REG(l, TPM_TIS_REG_ACCESS),
264 TPM_TIS_ACCESS_ACTIVE_LOCALITY);
266 if (l == 1) {
267 pending_request_flag = 0;
271 /* no locality may be active now */
272 for (l = 0; l < TPM_TIS_NUM_LOCALITIES - 1; l++) {
273 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
274 DPRINTF_ACCESS;
275 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
276 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
282 * Test case for getting access when higher number locality relinquishes access
284 static void tpm_tis_test_check_access_reg_release(const void *data)
286 int locty, l;
287 uint8_t access;
288 uint8_t pending_request_flag;
290 /* do not test locality 4 (hw only) */
291 for (locty = TPM_TIS_NUM_LOCALITIES - 2; locty >= 0; locty--) {
292 pending_request_flag = 0;
294 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
295 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
296 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
298 /* request use of locality */
299 writeb(TIS_REG(locty, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
300 access = readb(TIS_REG(locty, TPM_TIS_REG_ACCESS));
301 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
302 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
303 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
305 /* request use of all other localities */
306 for (l = 0; l < TPM_TIS_NUM_LOCALITIES - 1; l++) {
307 if (l == locty) {
308 continue;
310 /* request use of locality 'l' -- we MUST see REQUEST USE and
311 may see PENDING_REQUEST */
312 writeb(TIS_REG(l, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
313 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
314 DPRINTF_ACCESS;
315 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
316 TPM_TIS_ACCESS_REQUEST_USE |
317 pending_request_flag |
318 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
319 pending_request_flag = TPM_TIS_ACCESS_PENDING_REQUEST;
321 /* release locality 'locty' */
322 writeb(TIS_REG(locty, TPM_TIS_REG_ACCESS),
323 TPM_TIS_ACCESS_ACTIVE_LOCALITY);
324 /* highest locality should now be active; release it and make sure the
325 next higest locality is active afterwards */
326 for (l = TPM_TIS_NUM_LOCALITIES - 2; l >= 0; l--) {
327 if (l == locty) {
328 continue;
330 /* 'l' should be active now */
331 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
332 DPRINTF_ACCESS;
333 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
334 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
335 pending_request_flag |
336 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
337 /* 'l' relinquishes access */
338 writeb(TIS_REG(l, TPM_TIS_REG_ACCESS),
339 TPM_TIS_ACCESS_ACTIVE_LOCALITY);
340 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
341 DPRINTF_ACCESS;
342 if (l == 1 || (locty <= 1 && l == 2)) {
343 pending_request_flag = 0;
345 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
346 pending_request_flag |
347 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
353 * Test case for transmitting packets
355 static void tpm_tis_test_check_transmit(const void *data)
357 const TestState *s = data;
358 uint8_t access;
359 uint32_t sts;
360 uint16_t bcount;
361 size_t i;
363 /* request use of locality 0 */
364 writeb(TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
365 access = readb(TIS_REG(0, TPM_TIS_REG_ACCESS));
366 g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |
367 TPM_TIS_ACCESS_ACTIVE_LOCALITY |
368 TPM_TIS_ACCESS_TPM_ESTABLISHMENT);
370 sts = readl(TIS_REG(0, TPM_TIS_REG_STS));
371 DPRINTF_STS;
373 g_assert_cmpint(sts & 0xff, ==, 0);
374 g_assert_cmpint(sts & TPM_TIS_STS_TPM_FAMILY_MASK, ==,
375 TPM_TIS_STS_TPM_FAMILY2_0);
377 bcount = (sts >> 8) & 0xffff;
378 g_assert_cmpint(bcount, >=, 128);
380 writel(TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_COMMAND_READY);
381 sts = readl(TIS_REG(0, TPM_TIS_REG_STS));
382 DPRINTF_STS;
383 g_assert_cmpint(sts & 0xff, ==, TPM_TIS_STS_COMMAND_READY);
385 /* transmit command */
386 for (i = 0; i < sizeof(TPM_CMD); i++) {
387 writeb(TIS_REG(0, TPM_TIS_REG_DATA_FIFO), TPM_CMD[i]);
388 sts = readl(TIS_REG(0, TPM_TIS_REG_STS));
389 DPRINTF_STS;
390 if (i < sizeof(TPM_CMD) - 1) {
391 g_assert_cmpint(sts & 0xff, ==,
392 TPM_TIS_STS_EXPECT | TPM_TIS_STS_VALID);
393 } else {
394 g_assert_cmpint(sts & 0xff, ==, TPM_TIS_STS_VALID);
396 g_assert_cmpint((sts >> 8) & 0xffff, ==, --bcount);
398 /* start processing */
399 writeb(TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_TPM_GO);
401 uint64_t end_time = g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND;
402 do {
403 sts = readl(TIS_REG(0, TPM_TIS_REG_STS));
404 if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) {
405 break;
407 } while (g_get_monotonic_time() < end_time);
409 sts = readl(TIS_REG(0, TPM_TIS_REG_STS));
410 DPRINTF_STS;
411 g_assert_cmpint(sts & 0xff, == ,
412 TPM_TIS_STS_VALID | TPM_TIS_STS_DATA_AVAILABLE);
413 bcount = (sts >> 8) & 0xffff;
415 /* read response */
416 uint8_t tpm_msg[sizeof(struct tpm_hdr)];
417 g_assert_cmpint(sizeof(tpm_msg), ==, bcount);
419 for (i = 0; i < sizeof(tpm_msg); i++) {
420 tpm_msg[i] = readb(TIS_REG(0, TPM_TIS_REG_DATA_FIFO));
421 sts = readl(TIS_REG(0, TPM_TIS_REG_STS));
422 DPRINTF_STS;
423 if (sts & TPM_TIS_STS_DATA_AVAILABLE) {
424 g_assert_cmpint((sts >> 8) & 0xffff, ==, --bcount);
427 g_assert_cmpmem(tpm_msg, sizeof(tpm_msg), s->tpm_msg, sizeof(*s->tpm_msg));
429 /* relinquish use of locality 0 */
430 writeb(TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_ACTIVE_LOCALITY);
431 access = readb(TIS_REG(0, TPM_TIS_REG_ACCESS));
434 int main(int argc, char **argv)
436 int ret;
437 char *args, *tmp_path = g_dir_make_tmp("qemu-tpm-tis-test.XXXXXX", NULL);
438 GThread *thread;
439 TestState test;
441 module_call_init(MODULE_INIT_QOM);
442 g_test_init(&argc, &argv, NULL);
444 test.addr = g_new0(SocketAddress, 1);
445 test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
446 test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
447 g_mutex_init(&test.data_mutex);
448 g_cond_init(&test.data_cond);
449 test.data_cond_signal = false;
451 thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
452 tpm_emu_test_wait_cond(&test);
454 args = g_strdup_printf(
455 "-chardev socket,id=chr,path=%s "
456 "-tpmdev emulator,id=dev,chardev=chr "
457 "-device tpm-tis,tpmdev=dev",
458 test.addr->u.q_unix.path);
459 qtest_start(args);
461 qtest_add_data_func("/tpm-tis/test_check_localities", &test,
462 tpm_tis_test_check_localities);
464 qtest_add_data_func("/tpm-tis/test_check_access_reg", &test,
465 tpm_tis_test_check_access_reg);
467 qtest_add_data_func("/tpm-tis/test_check_access_reg_seize", &test,
468 tpm_tis_test_check_access_reg_seize);
470 qtest_add_data_func("/tpm-tis/test_check_access_reg_release", &test,
471 tpm_tis_test_check_access_reg_release);
473 qtest_add_data_func("/tpm-tis/test_check_transmit", &test,
474 tpm_tis_test_check_transmit);
476 ret = g_test_run();
478 qtest_end();
480 g_thread_join(thread);
481 g_unlink(test.addr->u.q_unix.path);
482 qapi_free_SocketAddress(test.addr);
483 g_rmdir(tmp_path);
484 g_free(tmp_path);
485 g_free(args);
486 return ret;