docs: Build and install specs manual
[qemu/ar7.git] / tests / tpm-tis-test.c
blob92a7e95aad28a22c7005cbefa36fc0d8bcae830f
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 "qemu/module.h"
22 #include "tpm-emu.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__); \
32 } \
33 } while (0)
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)
39 #define DPRINTF_STS \
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)
47 uint8_t locty;
48 uint8_t access;
49 uint32_t ifaceid;
50 uint32_t capability;
51 uint32_t didvid;
52 uint32_t rid;
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)
77 uint8_t locty;
78 uint8_t access;
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);
94 /* release access */
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)
108 int locty, l;
109 uint8_t access;
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));
131 DPRINTF_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));
142 DPRINTF_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));
160 DPRINTF_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));
187 DPRINTF_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));
196 DPRINTF_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));
207 DPRINTF_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));
215 DPRINTF_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 */
234 if (locty <= 1) {
235 pending_request_flag = 0;
238 /* release access from l - 1; this activates locty - 1 */
239 l--;
241 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
242 DPRINTF_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));
250 DPRINTF_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));
257 DPRINTF_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);
267 if (l == 1) {
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));
275 DPRINTF_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)
287 int locty, l;
288 uint8_t access;
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++) {
308 if (l == locty) {
309 continue;
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));
315 DPRINTF_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--) {
328 if (l == locty) {
329 continue;
331 /* 'l' should be active now */
332 access = readb(TIS_REG(l, TPM_TIS_REG_ACCESS));
333 DPRINTF_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));
342 DPRINTF_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;
359 uint8_t access;
360 uint32_t sts;
361 uint16_t bcount;
362 size_t i;
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));
372 DPRINTF_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));
383 DPRINTF_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));
390 DPRINTF_STS;
391 if (i < sizeof(TPM_CMD) - 1) {
392 g_assert_cmpint(sts & 0xff, ==,
393 TPM_TIS_STS_EXPECT | TPM_TIS_STS_VALID);
394 } else {
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;
403 do {
404 sts = readl(TIS_REG(0, TPM_TIS_REG_STS));
405 if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) {
406 break;
408 } while (g_get_monotonic_time() < end_time);
410 sts = readl(TIS_REG(0, TPM_TIS_REG_STS));
411 DPRINTF_STS;
412 g_assert_cmpint(sts & 0xff, == ,
413 TPM_TIS_STS_VALID | TPM_TIS_STS_DATA_AVAILABLE);
414 bcount = (sts >> 8) & 0xffff;
416 /* read response */
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));
423 DPRINTF_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)
437 int ret;
438 char *args, *tmp_path = g_dir_make_tmp("qemu-tpm-tis-test.XXXXXX", NULL);
439 GThread *thread;
440 TestState test;
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);
460 qtest_start(args);
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);
477 ret = g_test_run();
479 qtest_end();
481 g_thread_join(thread);
482 g_unlink(test.addr->u.q_unix.path);
483 qapi_free_SocketAddress(test.addr);
484 g_rmdir(tmp_path);
485 g_free(tmp_path);
486 g_free(args);
487 return ret;