Merge tpm 2026/06/01 v1
Subsystems (2)
The following changes since commit 5611a9268dae7b7ff99d478ed134052a9fc7e9f7:
Merge tag 'pull-9p-20260601' of https://github.com/cschoenebeck/qemu into staging (2026-06-01 08:43:53 -0400)
are available in the Git repository at:
https://github.com/stefanberger/qemu-tpm.git tags/pull-tpm-2026-06-01-1
for you to fetch changes up to f0602ce389ccda0e8df35ccaec3c1024478a0b10:
tpm_emulator: Disconnect if response exceeds negotiated buffer size (2026-06-01 19:44:39 +0000)
----------------------------------------------------------------
This series of patches
- Improves the buffer handling in the TPM emulator backend
- Implements chunking support for the TPM CRB device
- Adds support for saving & loading of VMSTATE_GBYTEARRAY
- Adds a swtpm test for the TPM I2C device
----------------------------------------------------------------
Arun Menon (8):
migration/vmstate: Add VMState support for GByteArray ui/vdagent: Use VMSTATE_GBYTEARRAY to safely migrate outbuf hw/tpm: Add TPM CRB chunking fields
hw/tpm: Refactor CRB_CTRL_START register access hw/tpm: Add internal buffer state for chunking hw/tpm: Implement TPM CRB chunking logic test/qtest: Add test for tpm crb chunking
hw/tpm: Add support for VM migration with TPM CRB chunking
Stefan Berger (8):
tests: Move TPM I2C bus read/write functions to common files
tests: Have TPM I2C read/write functions take QTestState as first
parameter
tests: Convert string arrays to byte arrays
tests: Rename id of tpmdev to tpm0
tests: Check whether the I2C master flag is set
tests: Add a TPM TIS I2C swtpm test
tpm_emulator: Reject a buffer size different than what was requested
tpm_emulator: Disconnect if response exceeds negotiated buffer size
backends/tpm/tpm_emulator.c | 35 +++- hw/core/machine.c | 2 + hw/tpm/tpm_crb.c | 253 +++++++++++++++++++++++++-- include/hw/acpi/tpm.h | 5 +- include/migration/vmstate.h | 10 ++ migration/vmstate-types.c | 28 +++ tests/qtest/meson.build | 7 +- tests/qtest/qtest_aspeed.c | 9 +- tests/qtest/tpm-crb-swtpm-test.c | 10 ++ tests/qtest/tpm-tests.c | 34 ++-- tests/qtest/tpm-tis-i2c-swtpm-test.c | 82 +++++++++ tests/qtest/tpm-tis-i2c-test.c | 221 ++++++++++------------- tests/qtest/tpm-tis-i2c-util.c | 102 +++++++++++ tests/qtest/tpm-tis-i2c-util.h | 34 ++++ tests/qtest/tpm-util.c | 162 +++++++++++++---- tests/qtest/tpm-util.h | 5 + ui/vdagent.c | 13 +- 17 files changed, 793 insertions(+), 219 deletions(-) create mode 100644 tests/qtest/tpm-tis-i2c-swtpm-test.c create mode 100644 tests/qtest/tpm-tis-i2c-util.c create mode 100644 tests/qtest/tpm-tis-i2c-util.h
Signature · 1 line
2.54.0
[PULL v1 03/16] tests: Convert string arrays to byte arrays
Convert the TPM command and response string arrays to byte arrays.
--- tests/qtest/tpm-tests.c | 30 ++++++++++++++++++--------- tests/qtest/tpm-util.c | 45 ++++++++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 26 deletions(-)
tests/qtest/tpm-tests.c +20 -10
@@ -59,11 +59,16 @@ void tpm_test_swtpm_test(const char *src_tpm_path, tx_func *tx,tpm_util_startup (s, tx);tpm_util_pcrextend (s, tx); -static const unsigned char tpm_pcrread_resp[] = -"\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00" -"\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85" -"\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89" -"\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde" ; +static const unsigned char tpm_pcrread_resp[] = { +0x80 ,0x01 ,0x00 ,0x00 ,0x00 ,0x3e ,0x00 ,0x00 , +0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x16 ,0x00 ,0x00 , +0x00 ,0x01 ,0x00 ,0x0b ,0x03 ,0x00 ,0x04 ,0x00 , +0x00 ,0x00 ,0x00 ,0x01 ,0x00 ,0x20 ,0xf6 ,0x85 , +0x98 ,0xe5 ,0x86 ,0x8d ,0xe6 ,0x8b ,0x97 ,0x29 , +0x99 ,0x60 ,0xf2 ,0x71 ,0x7d ,0x17 ,0x67 ,0x89 , +0xa4 ,0x2f ,0x9a ,0xae ,0xa8 ,0xc7 ,0xb7 ,0xaa , +0x79 ,0xa8 ,0x62 ,0x56 ,0xc1 ,0xde + };tpm_util_pcrread (s, tx, tpm_pcrread_resp,sizeof (tpm_pcrread_resp));
@@ -105,11 +110,16 @@ void tpm_test_swtpm_migration_test(const char *src_tpm_path,tpm_util_startup (src_qemu, tx);tpm_util_pcrextend (src_qemu, tx); -static const unsigned char tpm_pcrread_resp[] = -"\x80\x01\x00\x00\x00\x3e\x00\x00\x00\x00\x00\x00\x00\x16\x00\x00" -"\x00\x01\x00\x0b\x03\x00\x04\x00\x00\x00\x00\x01\x00\x20\xf6\x85" -"\x98\xe5\x86\x8d\xe6\x8b\x97\x29\x99\x60\xf2\x71\x7d\x17\x67\x89" -"\xa4\x2f\x9a\xae\xa8\xc7\xb7\xaa\x79\xa8\x62\x56\xc1\xde" ; +static const unsigned char tpm_pcrread_resp[] = { +0x80 ,0x01 ,0x00 ,0x00 ,0x00 ,0x3e ,0x00 ,0x00 , +0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x16 ,0x00 ,0x00 , +0x00 ,0x01 ,0x00 ,0x0b ,0x03 ,0x00 ,0x04 ,0x00 , +0x00 ,0x00 ,0x00 ,0x01 ,0x00 ,0x20 ,0xf6 ,0x85 , +0x98 ,0xe5 ,0x86 ,0x8d ,0xe6 ,0x8b ,0x97 ,0x29 , +0x99 ,0x60 ,0xf2 ,0x71 ,0x7d ,0x17 ,0x67 ,0x89 , +0xa4 ,0x2f ,0x9a ,0xae ,0xa8 ,0xc7 ,0xb7 ,0xaa , +0x79 ,0xa8 ,0x62 ,0x56 ,0xc1 ,0xde , + };tpm_util_pcrread (src_qemu, tx, tpm_pcrread_resp,sizeof (tpm_pcrread_resp));
tests/qtest/tpm-util.c +29 -16
64 lines not on this page · raw message
Signature · 1 line
2.54.0
[PULL v1 15/16] tpm_emulator: Reject a buffer size different than what was requested
In reply to Stefan BergerWhen the TIS, SPAPR, or CRB frontends negotiate a buffer size with the TPM backend, then the tpm_emulator (swtpm) could still adjust this size of the buffer to within bounds supported by swtpm+libtpms if the chosen size was outside the acceptable range. This could theoretically lead to the TPM 2 using a bigger buffer than what was requested and memory allocated for. In practice this would not happend since the requested size of 4096 bytes for TIS and SPAPR and 3968 bytes for CRB happen in the (currently) supported range of ~2.5kb to 4096 bytes. With PQC support the range will have an upper bound of 8kb and a lower bound that will support the (pre-PQC) CRB with 3968 bytes.
--- backends/tpm/tpm_emulator.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
backends/tpm/tpm_emulator.c +12 -1
@@ -364,6 +364,7 @@ static int tpm_emulator_set_buffer_size(TPMBackend *tb, {TPMEmulator *tpm_emu =TPM_EMULATOR (tb);ptm_setbuffersize psbs; +size_t tpm_buffersize;if (tpm_emulator_stop_tpm (tb, errp) <0 ) {return -1 ;
@@ -387,8 +388,18 @@ static int tpm_emulator_set_buffer_size(TPMBackend *tb,return -1 ; } + tpm_buffersize =be32_to_cpu (psbs.u.resp.buffersize); +/* Reject different buffer size used by the TPM than what was requested. */ +if (wanted_size !=0 && wanted_size != tpm_buffersize) { +error_setg (errp, +"tpm-emulator: TPM did not accept the requested buffer size " +"of %zu bytes but adjusted it to %zu bytes" , + wanted_size, tpm_buffersize); +return -1 ; + } +if (actual_size) { - *actual_size =be32_to_cpu (psbs.u.resp.buffersize); + *actual_size = tpm_buffersize; } trace_tpm_emulator_set_buffer_size(
Signature · 1 line
2.54.0
[PULL v1 12/16] hw/tpm: Implement TPM CRB chunking logic
In reply to Stefan BergerFrom: Arun Menon <[email protected]>
- Add logic to populate internal TPM command request and response buffers and to toggle the control registers after each operation.
- The chunk size is limited to CRB_CTRL_CMD_SIZE which is (TPM_CRB_ADDR_SIZE - A_CRB_DATA_BUFFER). This comes out as 3968 bytes (4096 - 128 or 0x1000 - 0x80), because 128 bytes are reserved for control and status registers. In other words, only 3968 bytes are available for the TPM data.
- With this feature, guests can send commands larger than 3968 bytes.
- Refer section 6.5.3.9 of [1] for implementation details.
--- hw/tpm/tpm_crb.c | 143 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 130 insertions(+), 13 deletions(-)
hw/tpm/tpm_crb.c +130 -13
@@ -17,6 +17,7 @@#include "qemu/osdep.h" #include "qemu/module.h" +#include "qemu/error-report.h" #include "qapi/error.h" #include "system/address-spaces.h" #include "hw/core/qdev-properties.h"
@@ -66,6 +67,7 @@ DECLARE_INSTANCE_CHECKER(CRBState, CRB,#define CRB_INTF_CAP_CRB_CHUNK 0b1#define CRB_CTRL_CMD_SIZE (TPM_CRB_ADDR_SIZE - A_CRB_DATA_BUFFER) +#define TPM_HEADER_SIZE 10enum crb_loc_ctrl {CRB_LOC_CTRL_REQUEST_ACCESS =BIT (0 ),
@@ -81,6 +83,8 @@ enum crb_ctrl_req {enum crb_start {CRB_START_INVOKE =BIT (0 ), +CRB_START_RSP_RETRY =BIT (1 ), +CRB_START_NEXT_CHUNK =BIT (2 ), };enum crb_cancel {
@@ -123,6 +127,69 @@ static uint8_t tpm_crb_get_active_locty(CRBState *s)return ARRAY_FIELD_EX32 (s->regs,CRB_LOC_STATE , activeLocality); } +static bool tpm_crb_append_command_request (CRBState *s) +{ +/* +* The linux guest writes the TPM command to the MMIO region in chunks. +* This function appends a chunk from the MMIO region to internal +* command_buffer. +*/ +void *mem =memory_region_get_ram_ptr (&s->cmdmem); +uint32_t to_copy =0 ; +uint32_t total_request_size =0 ; + +/* +* The initial call extracts the total TPM command size +* from its header. For the subsequent calls, the data already +* appended in the command_buffer is used to calculate the total +* size, as its header stays the same. +*/ +if (s->command_buffer->len ==0 ) { + total_request_size =tpm_cmd_get_size (mem); +if (total_request_size <TPM_HEADER_SIZE ) { +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_STS , tpmSts,1 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , Start,0 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); +tpm_crb_clear_internal_buffers (s); +error_report ("Command size %" PRIu32" less than " +"TPM header size %" PRIu32, + total_request_size, (uint32_t )TPM_HEADER_SIZE ); +return false; + } + }else { + total_request_size =tpm_cmd_get_size (s->command_buffer->data); + } + total_request_size =MIN (total_request_size, s->be_buffer_size); + +if (total_request_size > s->command_buffer->len) { +uint32_t remaining = total_request_size - s->command_buffer->len; + to_copy =MIN (remaining,CRB_CTRL_CMD_SIZE ); +g_byte_array_append (s->command_buffer, (guint8 *)mem, to_copy); + } +return true; +} + +static void tpm_crb_fill_command_response (CRBState *s) +{ +/* +* Response from the tpm backend will be stored in the internal +* response_buffer. This function will serve that accumulated response +* to the linux guest in chunks by writing it back to MMIO region. +*/ +void *mem =memory_region_get_ram_ptr (&s->cmdmem); +uint32_t remaining = s->response_buffer->len - s->response_offset; +uint32_t to_copy =MIN (CRB_CTRL_CMD_SIZE , remaining); + +memcpy (mem, s->response_buffer->data + s->response_offset, to_copy); + +if (to_copy <CRB_CTRL_CMD_SIZE ) { +memset ((guint8 *)mem + to_copy,0 ,CRB_CTRL_CMD_SIZE - to_copy); + } + + s->response_offset += to_copy; +memory_region_set_dirty (&s->cmdmem,0 ,CRB_CTRL_CMD_SIZE ); +} +static void tpm_crb_mmio_write (void *opaque,hwaddr addr,uint64_t val,unsigned size) {
@@ -153,20 +220,58 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr, }break ;case A_CRB_CTRL_START : -if (val ==CRB_START_INVOKE && - !(s->regs[R_CRB_CTRL_START ] &CRB_START_INVOKE ) && -tpm_crb_get_active_locty (s) == locty) { -void *mem =memory_region_get_ram_ptr (&s->cmdmem); - +if (tpm_crb_get_active_locty (s) != locty) { +break ; + } +if (s->regs[R_CRB_CTRL_START ] &CRB_START_INVOKE ) { +/* +* Backend TPM is busy processing a request. +*/ +break ; + } +if (val &CRB_START_INVOKE ) { +if (!tpm_crb_append_command_request (s)) { +break ; + }ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , Start,1 ); +g_byte_array_set_size (s->response_buffer, s->be_buffer_size); s->cmd = (TPMBackendCmd ) { - .in = mem, - .in_len =MIN (tpm_cmd_get_size (mem), s->be_buffer_size), - .out = mem, - .out_len = s->be_buffer_size, + .in = s->command_buffer->data, + .in_len = s->command_buffer->len, + .out = s->response_buffer->data, + .out_len = s->response_buffer->len, }; -tpm_backend_deliver_request (s->tpmbe, &s->cmd); + }else if (val &CRB_START_NEXT_CHUNK ) { +if (!s->cap_chunk) { +break ; + } +/* +* nextChunk is used both while sending and receiving data. +* To distinguish between the two, response_buffer is checked. +* If it does not have data, then that means we have not yet +* sent the command to the tpm backend, and therefore call +* tpm_crb_append_command_request(). +*/ +if (s->response_buffer->len >0 && + s->response_offset < s->response_buffer->len) { +tpm_crb_fill_command_response (s); + }else { +if (!tpm_crb_append_command_request (s)) { +break ; + } + } +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); + }else if (val &CRB_START_RSP_RETRY ) { +if (!s->cap_chunk) { +break ; + } +if (s->response_buffer->len >0 ) { + s->response_offset =0 ; +tpm_crb_fill_command_response (s); + } +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , crbRspRetry,0 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); }break ;case A_CRB_LOC_CTRL :
@@ -211,8 +316,21 @@ static void tpm_crb_request_completed(TPMIf *ti, int ret)if (ret !=0 ) {ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_STS , tpmSts,1 );/* fatal error */ +tpm_crb_clear_internal_buffers (s); + }else { +uint32_t actual_resp_size =tpm_cmd_get_size (s->response_buffer->data); +uint32_t total_resp_size =MIN (actual_resp_size, s->be_buffer_size); +g_byte_array_set_size (s->response_buffer, total_resp_size); + s->response_offset =0 ; } -memory_region_set_dirty (&s->cmdmem,0 ,CRB_CTRL_CMD_SIZE ); +/* +* Send the first chunk. Subsequent chunks will be sent +* on receiving nextChunk from the guest +*/ +tpm_crb_fill_command_response (s); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , crbRspRetry,0 ); +g_byte_array_set_size (s->command_buffer,0 ); }static enum TPMVersion tpm_crb_get_version (TPMIf *ti)
@@ -287,8 +405,7 @@ static void tpm_crb_reset(void *dev) s->regs[R_CRB_CTRL_RSP_SIZE ] =CRB_CTRL_CMD_SIZE ; s->regs[R_CRB_CTRL_RSP_ADDR ] =TPM_CRB_ADDR_BASE +A_CRB_DATA_BUFFER ; - s->be_buffer_size =MIN (tpm_backend_get_buffer_size (s->tpmbe), -CRB_CTRL_CMD_SIZE ); + s->be_buffer_size =tpm_backend_get_buffer_size (s->tpmbe);if (tpm_backend_startup_tpm (s->tpmbe, s->be_buffer_size) <0 ) {exit (1 );
Signature · 1 line
2.54.0
[PULL v1 09/16] hw/tpm: Add TPM CRB chunking fields
In reply to Stefan BergerFrom: Arun Menon <[email protected]>
- Add new fields to the CRB Interface Identifier and the CRB Control Start registers.
- CRB_CTRL_START now has 2 new settings, that can be toggled using the nextChunk and crbRspRetry bits.
- CapCRBChunk bit (10) was Reserved1 previously. The field is reused in this revision of the specification. Refer to section 6.4.2.2 of [1]
- Add hw_compat global property called cap-chunk because the chunking feature is only supported for machine type 11.1 and higher.
--- hw/core/machine.c | 1 + hw/tpm/tpm_crb.c | 6 ++++++ include/hw/acpi/tpm.h | 5 ++++- 3 files changed, 11 insertions(+), 1 deletion(-)
hw/core/machine.c +1 -0
@@ -41,6 +41,7 @@GlobalProperty hw_compat_11_0[] = { {"chardev-vc" ,"encoding" ,"cp437" }, + {"tpm-crb" ,"cap-chunk" ,"off" }, };const size_t hw_compat_11_0_len =G_N_ELEMENTS (hw_compat_11_0);
hw/tpm/tpm_crb.c +6 -0
@@ -44,6 +44,8 @@ struct CRBState {size_t be_buffer_size;TPMPPI ppi; + +bool cap_chunk; };typedef struct CRBState CRBState ;
@@ -58,6 +60,7 @@ DECLARE_INSTANCE_CHECKER(CRBState, CRB,#define CRB_INTF_CAP_FIFO_NOT_SUPPORTED 0b0#define CRB_INTF_CAP_CRB_SUPPORTED 0b1#define CRB_INTF_IF_SELECTOR_CRB 0b1 +#define CRB_INTF_CAP_CRB_CHUNK 0b1#define CRB_CTRL_CMD_SIZE (TPM_CRB_ADDR_SIZE - A_CRB_DATA_BUFFER)
@@ -227,6 +230,7 @@ static const VMStateDescription vmstate_tpm_crb = {static const Property tpm_crb_properties[] = {DEFINE_PROP_TPMBE ("tpmdev" , CRBState, tpmbe), +DEFINE_PROP_BOOL ("cap-chunk" , CRBState, cap_chunk, true), };static void tpm_crb_reset (void *dev)
@@ -258,6 +262,8 @@ static void tpm_crb_reset(void *dev) CapCRB, CRB_INTF_CAP_CRB_SUPPORTED);ARRAY_FIELD_DP32 (s->regs,CRB_INTF_ID , InterfaceSelector,CRB_INTF_IF_SELECTOR_CRB ); +ARRAY_FIELD_DP32 (s->regs,CRB_INTF_ID , + CapCRBChunk, s->cap_chunk ?CRB_INTF_CAP_CRB_CHUNK :0 );ARRAY_FIELD_DP32 (s->regs,CRB_INTF_ID ,RID ,0b0000 ); ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID2,
include/hw/acpi/tpm.h +4 -1
@@ -150,7 +150,7 @@ REG32(CRB_INTF_ID, 0x30) FIELD(CRB_INTF_ID, InterfaceVersion, 4, 4) FIELD(CRB_INTF_ID, CapLocality, 8, 1) FIELD(CRB_INTF_ID, CapCRBIdleBypass, 9, 1) - FIELD(CRB_INTF_ID, Reserved1, 10, 1) + FIELD(CRB_INTF_ID, CapCRBChunk, 10, 1) FIELD(CRB_INTF_ID, CapDataXferSizeSupport, 11, 2) FIELD(CRB_INTF_ID, CapFIFO, 13, 1)FIELD (CRB_INTF_ID , CapCRB,14 ,1 )
@@ -169,6 +169,9 @@ REG32(CRB_CTRL_STS, 0x44) FIELD(CRB_CTRL_STS, tpmIdle, 1, 1) REG32(CRB_CTRL_CANCEL, 0x48) REG32(CRB_CTRL_START, 0x4C) + FIELD(CRB_CTRL_START, Start, 0, 1) + FIELD(CRB_CTRL_START, crbRspRetry, 1, 1) + FIELD(CRB_CTRL_START, nextChunk, 2, 1) REG32(CRB_INT_ENABLED, 0x50) REG32(CRB_INT_STS, 0x54)REG32 (CRB_CTRL_CMD_SIZE ,0x58 )
Signature · 1 line
2.54.0
[PULL v1 08/16] ui/vdagent: Use VMSTATE_GBYTEARRAY to safely migrate outbuf
In reply to Stefan BergerFrom: Arun Menon <[email protected]>
Migrating a GLib GByteArray is now possible directly using the newly introduced VMSTATE_GBYTEARRAY. It uses the standard GLib API calls to create the array, or resize it.
This is safer than implementing a C struct and manually updating the data and len fields. This commit uses the VMSTATE_GBYTEARRAY in vdagent to store the outbuf variable.
--- ui/vdagent.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
ui/vdagent.c +1 -12
@@ -964,17 +964,6 @@ static const VMStateDescription vmstate_chunk = { } }; -static const VMStateDescription vmstate_vdba = { - .name ="vdagent/bytearray" , - .version_id =0 , - .minimum_version_id =0 , - .fields = (const VMStateField []) { -VMSTATE_UINT32 (len, GByteArray), -VMSTATE_VBUFFER_ALLOC_UINT32 (data, GByteArray,0 ,0 , len), -VMSTATE_END_OF_LIST () - } -}; -struct CBInfoArray {uint32_t n;QemuClipboardInfo cbinfo[QEMU_CLIPBOARD_SELECTION__COUNT ];
@@ -1064,7 +1053,7 @@ static const VMStateDescription vmstate_vdagent = {VMSTATE_UINT32 (xsize, VDAgentChardev),VMSTATE_UINT32 (xoff, VDAgentChardev),VMSTATE_VBUFFER_ALLOC_UINT32 (xbuf, VDAgentChardev,0 ,0 , xsize), -VMSTATE_STRUCT_POINTER (outbuf, VDAgentChardev, vmstate_vdba, GByteArray), +VMSTATE_GBYTEARRAY (outbuf, VDAgentChardev,0 ),VMSTATE_UINT32 (mouse_x, VDAgentChardev),VMSTATE_UINT32 (mouse_y, VDAgentChardev), VMSTATE_UINT32(mouse_btn, VDAgentChardev),
Signature · 1 line
2.54.0
[PULL v1 10/16] hw/tpm: Refactor CRB_CTRL_START register access
In reply to Stefan BergerFrom: Arun Menon <[email protected]>
Replace manual bitwise operations with ARRAY_FIELD_DP32 macros No functional changes.
--- hw/tpm/tpm_crb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/tpm/tpm_crb.c +2 -2
@@ -146,7 +146,7 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr, tpm_crb_get_active_locty(s) == locty) {void *mem =memory_region_get_ram_ptr (&s->cmdmem); - s->regs[R_CRB_CTRL_START ] |=CRB_START_INVOKE ; +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , Start,1 ); s->cmd = (TPMBackendCmd ) { .in = mem, .in_len =MIN (tpm_cmd_get_size (mem), s->be_buffer_size),
@@ -195,7 +195,7 @@ static void tpm_crb_request_completed(TPMIf *ti, int ret) {CRBState *s =CRB (ti); - s->regs[R_CRB_CTRL_START ] &= ~CRB_START_INVOKE ; +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , Start,0 );if (ret !=0 ) {ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_STS , tpmSts,1 );/* fatal error */
Signature · 1 line
2.54.0
[PULL v1 05/16] tests: Check whether the I2C master flag is set
In reply to Stefan BergerReplace the 'once' variable with a check for whether the master flag is set so that the flag can be set when needed.
--- tests/qtest/qtest_aspeed.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
tests/qtest/qtest_aspeed.c +6 -3
@@ -15,18 +15,21 @@#include "qtest_aspeed.h" #include "hw/i2c/aspeed_i2c.h" +static bool aspeed_i2c_is_master_enabled (QTestState *s,uint32_t baseaddr) +{ +return qtest_readl (s, baseaddr +A_I2CC_FUN_CTRL ) &A_I2CD_MASTER_EN ; +} +static void aspeed_i2c_startup (QTestState *s,uint32_t baseaddr,uint8_t slave_addr,uint8_t reg) {uint32_t v; -static int once; -if (!once) { +if (!aspeed_i2c_is_master_enabled (s, baseaddr)) {/* one time: enable master */ qtest_writel (s, baseaddr +A_I2CC_FUN_CTRL ,0 ); v =qtest_readl (s, baseaddr +A_I2CC_FUN_CTRL ) |A_I2CD_MASTER_EN ;qtest_writel (s, baseaddr +A_I2CC_FUN_CTRL , v); - once =1 ; }/* select device */
Signature · 1 line
2.54.0
[PULL v1 04/16] tests: Rename id of tpmdev to tpm0
In reply to Stefan BergerRename the id of the tpmdev from dev to tpm0 because this 'dev' cannot be used when the tpm-tis-i2c device is used.
--- tests/qtest/tpm-tests.c | 4 ++-- tests/qtest/tpm-util.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-)
tests/qtest/tpm-tests.c +2 -2
@@ -49,8 +49,8 @@ void tpm_test_swtpm_test(const char *src_tpm_path, tx_func *tx, args =g_strdup_printf ("%s " "-chardev socket,id=chr,path=%s " -"-tpmdev emulator,id=dev,chardev=chr " -"-device %s,tpmdev=dev" , +"-tpmdev emulator,id=tpm0,chardev=chr " +"-device %s,tpmdev=tpm0" , machine_options ? :"" , addr->u.q_unix.path, ifmodel); s =qtest_start (args);
tests/qtest/tpm-util.c +4 -4
@@ -240,8 +240,8 @@ void tpm_util_migration_start_qemu(QTestState **src_qemu, src_qemu_args =g_strdup_printf ("%s " "-chardev socket,id=chr,path=%s " -"-tpmdev emulator,id=dev,chardev=chr " -"-device %s,tpmdev=dev " , +"-tpmdev emulator,id=tpm0,chardev=chr " +"-device %s,tpmdev=tpm0 " , machine_options ? :"" , src_tpm_addr->u.q_unix.path, ifmodel); *src_qemu =qtest_init (src_qemu_args);
@@ -249,8 +249,8 @@ void tpm_util_migration_start_qemu(QTestState **src_qemu, dst_qemu_args = g_strdup_printf( "%s " "-chardev socket,id=chr,path=%s " - "-tpmdev emulator,id=dev,chardev=chr " - "-device %s,tpmdev=dev " + "-tpmdev emulator,id=tpm0,chardev=chr " + "-device %s,tpmdev=tpm0 " "-incoming %s", machine_options ? : "", dst_tpm_addr->u.q_unix.path,
Signature · 1 line
2.54.0
[PULL v1 11/16] hw/tpm: Add internal buffer state for chunking
In reply to Stefan BergerFrom: Arun Menon <[email protected]>
- Introduce GByteArray buffers to hold the command request and response data during chunked TPM CRB transactions.
- Add helper function to clean them.
--- hw/tpm/tpm_crb.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)
hw/tpm/tpm_crb.c +29 -4
@@ -38,10 +38,13 @@ struct CRBState {TPMBackend *tpmbe;TPMBackendCmd cmd;uint32_t regs[TPM_CRB_R_MAX ]; +size_t be_buffer_size;MemoryRegion mmio;MemoryRegion cmdmem; -size_t be_buffer_size; +GByteArray *command_buffer; +GByteArray *response_buffer; +uint32_t response_offset;TPMPPI ppi;
@@ -86,6 +89,13 @@ enum crb_cancel {#define TPM_CRB_NO_LOCALITY 0xff +static void tpm_crb_clear_internal_buffers (CRBState *s) +{ +g_byte_array_set_size (s->response_buffer,0 ); +g_byte_array_set_size (s->command_buffer,0 ); + s->response_offset =0 ; +} +static uint64_t tpm_crb_mmio_read (void *opaque,hwaddr addr,unsigned size) {
@@ -135,9 +145,11 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr, }break ;case A_CRB_CTRL_CANCEL : -if (val ==CRB_CANCEL_INVOKE && - s->regs[R_CRB_CTRL_START ] &CRB_START_INVOKE ) { -tpm_backend_cancel_cmd (s->tpmbe); +if (val ==CRB_CANCEL_INVOKE ) { +if (s->regs[R_CRB_CTRL_START ] &CRB_START_INVOKE ) { +tpm_backend_cancel_cmd (s->tpmbe); + } +tpm_crb_clear_internal_buffers (s); }break ;case A_CRB_CTRL_START :
@@ -239,6 +251,7 @@ static void tpm_crb_reset(void *dev)tpm_ppi_reset (&s->ppi);tpm_backend_reset (s->tpmbe); +tpm_crb_clear_internal_buffers (s);memset (s->regs,0 ,sizeof (s->regs));
@@ -305,6 +318,9 @@ static void tpm_crb_realize(DeviceState *dev, Error **errp)memory_region_add_subregion (get_system_memory (),TPM_CRB_ADDR_BASE +sizeof (s->regs), &s->cmdmem); + s->command_buffer =g_byte_array_new (); + s->response_buffer =g_byte_array_new (); +tpm_ppi_init (&s->ppi,get_system_memory (),TPM_PPI_ADDR_BASE ,OBJECT (s));
@@ -315,12 +331,21 @@ static void tpm_crb_realize(DeviceState *dev, Error **errp) } } +static void tpm_crb_unrealize (DeviceState *dev) +{ +CRBState *s =CRB (dev); + +g_clear_pointer (&s->command_buffer, g_byte_array_unref); +g_clear_pointer (&s->response_buffer, g_byte_array_unref); +} +static void tpm_crb_class_init (ObjectClass *klass,const void *data) {DeviceClass *dc =DEVICE_CLASS (klass);TPMIfClass *tc =TPM_IF_CLASS (klass); dc->realize = tpm_crb_realize; + dc->unrealize = tpm_crb_unrealize;device_class_set_props (dc, tpm_crb_properties); dc->vmsd = &vmstate_tpm_crb; dc->user_creatable = true;
Signature · 1 line
2.54.0
[PULL v1 14/16] hw/tpm: Add support for VM migration with TPM CRB chunking
In reply to Stefan BergerFrom: Arun Menon <[email protected]>
- Add subsection in VMState for TPM CRB with the newly introduced command and response buffer GByteArrays, along with a needed callback, so that newer QEMU only sends the buffers if it is necessary.
- Implement a migration blocker to prevent migration of the VM if the user manually enables chunking capability, cap-chunk, but the machine type does not support it, using a new hw_compat property called allow_chunk_migration.
- Add a post_load_errp hook so that during a migration, the buffers are validated before destination VM is started.
--- hw/core/machine.c | 1 + hw/tpm/tpm_crb.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)
hw/core/machine.c +1 -0
@@ -42,6 +42,7 @@GlobalProperty hw_compat_11_0[] = { {"chardev-vc" ,"encoding" ,"cp437" }, {"tpm-crb" ,"cap-chunk" ,"off" }, + {"tpm-crb" ,"x-allow-chunk-migration" ,"off" }, };const size_t hw_compat_11_0_len =G_N_ELEMENTS (hw_compat_11_0);
hw/tpm/tpm_crb.c +67 -0
115 lines not on this page · raw message
Signature · 1 line
2.54.0
[PULL v1 16/16] tpm_emulator: Disconnect if response exceeds negotiated buffer size
In reply to Stefan BergerDisconnect from the emulator if a response was to exceed the negotiated buffer size.
The TPM TIS and SPAPR use 4096 bytes and the CRB 3968 bytes. There are currently no TPM 2 responses using this size of a buffer and therefore no response will be sent that is exceeding this size.
--- backends/tpm/tpm_emulator.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
backends/tpm/tpm_emulator.c +20 -2
@@ -176,8 +176,10 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,bool *selftest_done, Error **errp) { - ssize_t ret; bool is_selftest = false; +Error *local_err =NULL ; +uint32_t to_read; +ssize_t ret;if (selftest_done) { *selftest_done = false;
@@ -195,9 +197,25 @@ static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,return -1 ; } +/* +* Size of response from emulator must be <= out_len (= negotiated buffer +* size) +*/ + to_read =tpm_cmd_get_size (out); +if (to_read > out_len) { +if (qio_channel_shutdown (tpm_emu->data_ioc,QIO_CHANNEL_SHUTDOWN_BOTH , + &local_err) <0 ) { +error_report_err (local_err); + } +error_setg (errp,"tpm-emulator: Disconnected after receiving " +"unacceptable large response (%u > %u)" , + to_read, out_len); +return -1 ; + } + ret =qio_channel_read_all (tpm_emu->data_ioc, (char *)out +sizeof (struct tpm_resp_hdr ), -tpm_cmd_get_size (out) -sizeof (struct tpm_resp_hdr ), errp); + to_read -sizeof (struct tpm_resp_hdr ), errp);if (ret !=0 ) {return -1 ; }
Signature · 1 line
2.54.0
[PULL v1 01/16] tests: Move TPM I2C bus read/write functions to common files
In reply to Stefan BergerMove functions for reading from and writing to the Aspeed I2C device into a file so they can be reused by other functions.
--- tests/qtest/meson.build | 2 +- tests/qtest/tpm-tis-i2c-test.c | 50 +------------------------- tests/qtest/tpm-tis-i2c-util.c | 64 ++++++++++++++++++++++++++++++++++ tests/qtest/tpm-tis-i2c-util.h | 30 ++++++++++++++++ 4 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 tests/qtest/tpm-tis-i2c-util.c create mode 100644 tests/qtest/tpm-tis-i2c-util.h
tests/qtest/meson.build +1 -1
@@ -401,7 +401,7 @@ qtests = { 'tpm-crb-test': [io, tpmemu_files], 'tpm-tis-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'], 'tpm-tis-test': [io, tpmemu_files, 'tpm-tis-util.c'], - 'tpm-tis-i2c-test': [io, tpmemu_files, 'qtest_aspeed.c'], + 'tpm-tis-i2c-test': [io, tpmemu_files, 'tpm-tis-i2c-util.c', 'qtest_aspeed.c'], 'tpm-tis-device-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'], 'tpm-tis-device-test': [io, tpmemu_files, 'tpm-tis-util.c'], 'virtio-net-failover': test_migration_files,
tests/qtest/tpm-tis-i2c-test.c +1 -49
65 lines not on this page · raw message
tests/qtest/tpm-tis-i2c-util.c new file +64 -0
64 lines not on this page · raw message
tests/qtest/tpm-tis-i2c-util.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* QTest TPM TIS I2C: Common test functions used for TPM I2C on Aspeed bus +* +* Copyright (c) 2026 IBM Corporation +* +* Authors: +* Stefan Berger <[email protected]> +* +*/ + +#ifndef TESTS_TPM_TIS_I2C_UTIL_H +#define TESTS_TPM_TIS_I2C_UTIL_H + +#include "qemu/osdep.h" + +extern uint32_t aspeed_bus_addr; + +#define I2C_SLAVE_ADDR 0x2e +#define I2C_DEV_BUS_NUM 10 + +uint8_t tpm_tis_i2c_readb (uint8_t locty,uint8_t reg); +uint16_t tpm_tis_i2c_readw (uint8_t locty,uint8_t reg); +uint32_t tpm_tis_i2c_readl (uint8_t locty,uint8_t reg); + +void tpm_tis_i2c_writeb (uint8_t locty,uint8_t reg,uint8_t v); +void tpm_tis_i2c_writel (uint8_t locty,uint8_t reg,uint32_t v); + +#endif /* TESTS_TPM_TIS_I2C_UTIL_H */
Signature · 1 line
2.54.0
[PULL v1 13/16] test/qtest: Add test for tpm crb chunking
In reply to Stefan BergerFrom: Arun Menon <[email protected]>
- New test case added to the swtpm test. Data is written and read from the buffer in chunks.
- The chunk size is dynamically calculated by reading the CRB_CTRL_CMD_SIZE address. This can be changed manually to test.
- Add a helper function tpm_wait_till_bit_clear()
- Note that this commit does not yet exercise the chunked read/write logic, as current transfer sizes remain small. Testing for large transfers is introduced in a subsequent patch: 'tests: Use ML-DSA-87 operations to cause large TPM transfers with CRB'
--- tests/qtest/tpm-crb-swtpm-test.c | 10 +++ tests/qtest/tpm-util.c | 109 ++++++++++++++++++++++++++----- tests/qtest/tpm-util.h | 5 ++ 3 files changed, 109 insertions(+), 15 deletions(-)
tests/qtest/tpm-crb-swtpm-test.c +10 -0
@@ -33,6 +33,14 @@ static void tpm_crb_swtpm_test(const void *data) "tpm-crb", NULL); } +static void tpm_crb_chunk_swtpm_test (const void *data) +{ +const TestState *ts = data; + +tpm_test_swtpm_test (ts->src_tpm_path, tpm_util_crb_chunk_transfer, +"tpm-crb" ,NULL ); +} +static void tpm_crb_swtpm_migration_test (const void *data) {const TestState *ts = data;
@@ -54,6 +62,8 @@ int main(int argc, char **argv)g_test_init (&argc, &argv,NULL );qtest_add_data_func ("/tpm/crb-swtpm/test" , &ts, tpm_crb_swtpm_test); +qtest_add_data_func ("/tpm/crb-chunk-swtpm/test" , &ts, + tpm_crb_chunk_swtpm_test);qtest_add_data_func ("/tpm/crb-swtpm-migration/test" , &ts, tpm_crb_swtpm_migration_test); ret =g_test_run ();
tests/qtest/tpm-util.c +94 -15
134 lines not on this page · raw message
tests/qtest/tpm-util.h +5 -0
@@ -24,10 +24,15 @@ typedef void (tx_func)(QTestState *s, const unsigned char *req, size_t req_size, unsigned char *rsp, size_t rsp_size); +void tpm_wait_till_bit_clear (QTestState *s,uint64_t addr,uint32_t mask);void tpm_util_crb_transfer (QTestState *s,const unsigned char *req,size_t req_size,unsigned char *rsp,size_t rsp_size); +void tpm_util_crb_chunk_transfer (QTestState *s, +const unsigned char *req,size_t req_size, +unsigned char *rsp,size_t rsp_size); +void tpm_util_startup (QTestState *s,tx_func *tx);void tpm_util_pcrextend (QTestState *s,tx_func *tx); void tpm_util_pcrread(QTestState *s, tx_func *tx,
Signature · 1 line
2.54.0
[PULL v1 02/16] tests: Have TPM I2C read/write functions take QTestState as first parameter
In reply to Stefan BergerPass the QTestState as first parameter to the TPM I2C functions. Use global_qtest in existing test cases.
--- tests/qtest/tpm-tis-i2c-test.c | 171 +++++++++++++++++---------------- tests/qtest/tpm-tis-i2c-util.c | 32 +++--- tests/qtest/tpm-tis-i2c-util.h | 10 +- 3 files changed, 110 insertions(+), 103 deletions(-)
tests/qtest/tpm-tis-i2c-test.c +89 -82
@@ -50,62 +50,64 @@ static void tpm_tis_i2c_test_basic(const void *data)* All register accesses below must work without locality 0 being the * active locality. Therefore, ensure access is released. */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY ); - access =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );/* read interrupt capability -- none are supported */ - v =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_INT_CAPABILITY ); + v =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_INT_CAPABILITY ); g_assert_cmpint(v, ==, 0);/* try to enable all interrupts */ -tpm_tis_i2c_writel (0 ,TPM_I2C_REG_INT_ENABLE ,0xffffffff ); - v =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_INT_ENABLE ); +tpm_tis_i2c_writel (global_qtest,0 ,TPM_I2C_REG_INT_ENABLE ,0xffffffff ); + v =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_INT_ENABLE );/* none could be enabled */ g_assert_cmpint(v, ==, 0);/* enable csum */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ,TPM_DATA_CSUM_ENABLED ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_ENABLE , +TPM_DATA_CSUM_ENABLED );/* check csum enable register has bit 0 set */ - v =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); + v =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); g_assert_cmpint(v, ==, TPM_DATA_CSUM_ENABLED);/* reading it as 32bit register returns same result */ - v =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); + v =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); g_assert_cmpint(v, ==, TPM_DATA_CSUM_ENABLED);/* disable csum */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ,0 ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ,0 );/* check csum enable register has bit 0 clear */ - v =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); + v =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); g_assert_cmpint(v, ==, 0);/* write to unsupported register '1' */ -tpm_tis_i2c_writel (0 ,1 ,0x12345678 ); - v =tpm_tis_i2c_readl (0 ,1 ); +tpm_tis_i2c_writel (global_qtest,0 ,1 ,0x12345678 ); + v =tpm_tis_i2c_readl (global_qtest,0 ,1 ); g_assert_cmpint(v, ==, 0xffffffff);/* request use of locality */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_ACCESS , +TPM_TIS_ACCESS_REQUEST_USE );/* read byte from STS + 3 */ - v =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_STS +3 ); + v =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_STS +3 ); g_assert_cmpint(v, ==, 0);/* check STS after writing to STS + 3 */ - v =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_STS ); -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_STS +3 ,0xf ); - v2 =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_STS ); + v =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_STS ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_STS +3 ,0xf ); + v2 =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_STS ); g_assert_cmpint(v, ==, v2);/* release access */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY );/* select locality 5 -- must not be possible */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_LOC_SEL ,5 ); - v =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_LOC_SEL ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_LOC_SEL ,5 ); + v =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_LOC_SEL ); g_assert_cmpint(v, ==, 0); }
@@ -118,11 +120,12 @@ static void tpm_tis_i2c_test_check_localities(const void *data)uint32_t rid;for (locty =0 ; locty <TPM_TIS_NUM_LOCALITIES ; locty++) { - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_TPM_ESTABLISHMENT ); - capability =tpm_tis_i2c_readl (locty,TPM_I2C_REG_INTF_CAPABILITY ); + capability =tpm_tis_i2c_readl (global_qtest, locty, +TPM_I2C_REG_INTF_CAPABILITY ); i2c_cap = (TPM_I2C_CAP_INTERFACE_TYPE | TPM_I2C_CAP_INTERFACE_VER | TPM_I2C_CAP_TPM2_FAMILY |
@@ -131,15 +134,15 @@ static void tpm_tis_i2c_test_check_localities(const void *data) TPM_I2C_CAP_DEV_ADDR_CHANGE); g_assert_cmpint(capability, ==, i2c_cap); - didvid =tpm_tis_i2c_readl (locty,TPM_I2C_REG_DID_VID ); + didvid =tpm_tis_i2c_readl (global_qtest, locty,TPM_I2C_REG_DID_VID ); g_assert_cmpint(didvid, ==, (1 << 16) | PCI_VENDOR_ID_IBM); - rid =tpm_tis_i2c_readl (locty,TPM_I2C_REG_RID ); + rid =tpm_tis_i2c_readl (global_qtest, locty,TPM_I2C_REG_RID ); g_assert_cmpint(rid, !=, 0); g_assert_cmpint(rid, !=, 0xffffffff);/* locality selection must be at locty */ - l =tpm_tis_i2c_readb (locty,TPM_I2C_REG_LOC_SEL ); + l =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_LOC_SEL ); g_assert_cmpint(l, ==, locty); } }
@@ -151,23 +154,23 @@ static void tpm_tis_i2c_test_check_access_reg(const void *data)/* do not test locality 4 (hw only) */ for (locty =0 ; locty <TPM_TIS_NUM_LOCALITIES -1 ; locty++) { - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );/* request use of locality */ -tpm_tis_i2c_writeb (locty,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, locty,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE ); - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_ACTIVE_LOCALITY |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );/* release access */ -tpm_tis_i2c_writeb (locty,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, locty,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY ); - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_TPM_ESTABLISHMENT ); }
@@ -186,14 +189,14 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data)for (locty =0 ; locty <TPM_TIS_NUM_LOCALITIES -1 ; locty++) { pending_request_flag =0 ; - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );/* request use of locality */ -tpm_tis_i2c_writeb (locty, +tpm_tis_i2c_writeb (global_qtest, locty,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE ); - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_ACTIVE_LOCALITY |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );
@@ -201,14 +204,14 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data)/* lower localities cannot seize access */ for (l =0 ; l < locty; l++) {/* lower locality is not active */ - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | pending_request_flag |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );/* try to request use from 'l' */ -tpm_tis_i2c_writeb (l, +tpm_tis_i2c_writeb (global_qtest, l,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE );
@@ -216,7 +219,7 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data)* requesting use from 'l' was not possible; * we must see REQUEST_USE and possibly PENDING_REQUEST */ - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_REQUEST_USE |
@@ -227,17 +230,17 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data)* locality 'locty' must be unchanged; * we must see PENDING_REQUEST */ - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_ACTIVE_LOCALITY |TPM_TIS_ACCESS_PENDING_REQUEST |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );/* try to seize from 'l' */ -tpm_tis_i2c_writeb (l, +tpm_tis_i2c_writeb (global_qtest, l,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_SEIZE );/* seize from 'l' was not possible */ - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_REQUEST_USE |
@@ -245,7 +248,7 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data) TPM_TIS_ACCESS_TPM_ESTABLISHMENT);/* locality 'locty' must be unchanged */ - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_ACTIVE_LOCALITY | TPM_TIS_ACCESS_PENDING_REQUEST |
@@ -264,14 +267,14 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data)*/ for (l = locty +1 ; l <TPM_TIS_NUM_LOCALITIES -1 ; l++) {/* try to 'request use' from 'l' */ -tpm_tis_i2c_writeb (l,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, l,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE );/* * requesting use from 'l' was not possible; we should see * REQUEST_USE and may see PENDING_REQUEST */ - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_REQUEST_USE |
@@ -282,7 +285,7 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data)* locality 'l-1' must be unchanged; we should always * see PENDING_REQUEST from 'l' requesting access */ - access =tpm_tis_i2c_readb (l -1 ,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l -1 ,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_ACTIVE_LOCALITY |
@@ -290,10 +293,11 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data) TPM_TIS_ACCESS_TPM_ESTABLISHMENT);/* try to seize from 'l' */ -tpm_tis_i2c_writeb (l,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_SEIZE ); +tpm_tis_i2c_writeb (global_qtest, l,TPM_I2C_REG_ACCESS , +TPM_TIS_ACCESS_SEIZE );/* seize from 'l' was possible */ - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_ACTIVE_LOCALITY |
@@ -301,7 +305,7 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data) TPM_TIS_ACCESS_TPM_ESTABLISHMENT);/* l - 1 should show that it has BEEN_SEIZED */ - access =tpm_tis_i2c_readb (l -1 ,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l -1 ,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_BEEN_SEIZED |
@@ -309,10 +313,10 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data) TPM_TIS_ACCESS_TPM_ESTABLISHMENT);/* clear the BEEN_SEIZED flag and make sure it's gone */ -tpm_tis_i2c_writeb (l -1 ,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, l -1 ,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_BEEN_SEIZED ); - access =tpm_tis_i2c_readb (l -1 ,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l -1 ,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | pending_request_flag |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );
@@ -330,22 +334,22 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data)/* release access from l - 1; this activates locty - 1 */ l--; - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ;DPRINTF ("%s: %d: relinquishing control on l = %d\n" , __func__, __LINE__, l); -tpm_tis_i2c_writeb (l,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, l,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY ); - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | pending_request_flag |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );for (l = locty -1 ; l >=0 ; l--) { - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_ACTIVE_LOCALITY |
@@ -353,7 +357,7 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data) TPM_TIS_ACCESS_TPM_ESTABLISHMENT);/* release this locality */ -tpm_tis_i2c_writeb (l,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, l,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY );if (l ==1 ) {
@@ -363,7 +367,7 @@ static void tpm_tis_i2c_test_check_access_reg_seize(const void *data)/* no locality may be active now */ for (l =0 ; l <TPM_TIS_NUM_LOCALITIES -1 ; l++) { - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );
@@ -384,14 +388,14 @@ static void tpm_tis_i2c_test_check_access_reg_release(const void *data)for (locty =TPM_TIS_NUM_LOCALITIES -2 ; locty >=0 ; locty--) { pending_request_flag =0 ; - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );/* request use of locality */ -tpm_tis_i2c_writeb (locty,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, locty,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE ); - access =tpm_tis_i2c_readb (locty,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, locty,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_ACTIVE_LOCALITY |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );
@@ -405,9 +409,9 @@ static void tpm_tis_i2c_test_check_access_reg_release(const void *data)* request use of locality 'l' -- we MUST see REQUEST USE and * may see PENDING_REQUEST */ -tpm_tis_i2c_writeb (l,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, l,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE ); - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS | TPM_TIS_ACCESS_REQUEST_USE |
@@ -416,7 +420,7 @@ static void tpm_tis_i2c_test_check_access_reg_release(const void *data) pending_request_flag =TPM_TIS_ACCESS_PENDING_REQUEST ; }/* release locality 'locty' */ -tpm_tis_i2c_writeb (locty,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, locty,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY );/* * highest locality should now be active; release it and make sure the
@@ -427,16 +431,16 @@ static void tpm_tis_i2c_test_check_access_reg_release(const void *data)continue ; }/* 'l' should be active now */ - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ; g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_ACTIVE_LOCALITY | pending_request_flag |TPM_TIS_ACCESS_TPM_ESTABLISHMENT );/* 'l' relinquishes access */ -tpm_tis_i2c_writeb (l,TPM_I2C_REG_ACCESS , +tpm_tis_i2c_writeb (global_qtest, l,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY ); - access =tpm_tis_i2c_readb (l,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest, l,TPM_I2C_REG_ACCESS );DPRINTF_ACCESS ;if (l ==1 || (locty <=1 && l ==2 )) { pending_request_flag =0 ;
@@ -460,22 +464,24 @@ static void tpm_tis_i2c_test_check_transmit(const void *data)size_t i;/* enable csum */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ,TPM_DATA_CSUM_ENABLED ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_ENABLE , +TPM_DATA_CSUM_ENABLED );/* check csum enable register has bit 0 set */ - v =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); + v =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); g_assert_cmpint(v, ==, TPM_DATA_CSUM_ENABLED);/* reading it as 32bit register returns same result */ - v =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); + v =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_ENABLE ); g_assert_cmpint(v, ==, TPM_DATA_CSUM_ENABLED);/* request use of locality 0 */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE ); - access =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_ACCESS ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_ACCESS , +TPM_TIS_ACCESS_REQUEST_USE ); + access =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_ACCESS ); g_assert_cmpint(access, ==, TPM_TIS_ACCESS_TPM_REG_VALID_STS |TPM_TIS_ACCESS_ACTIVE_LOCALITY |TPM_TIS_ACCESS_TPM_ESTABLISHMENT ); - sts =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_STS ); + sts =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_STS );DPRINTF_STS ; g_assert_cmpint(sts & 0xff, ==, 0);
@@ -484,21 +490,22 @@ static void tpm_tis_i2c_test_check_transmit(const void *data) g_assert_cmpint(bcount, >=, 128);/* read bcount from STS + 1 must work also */ - bcount2 =tpm_tis_i2c_readw (0 ,TPM_I2C_REG_STS +1 ); + bcount2 =tpm_tis_i2c_readw (global_qtest,0 ,TPM_I2C_REG_STS +1 ); g_assert_cmpint(bcount, ==, bcount2);/* ic2 must have bits 26-31 zero */ g_assert_cmpint(sts & (0x1f << 26), ==, 0); -tpm_tis_i2c_writel (0 ,TPM_I2C_REG_STS ,TPM_TIS_STS_COMMAND_READY ); - sts =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_STS ); +tpm_tis_i2c_writel (global_qtest,0 ,TPM_I2C_REG_STS , +TPM_TIS_STS_COMMAND_READY ); + sts =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_STS );DPRINTF_STS ; g_assert_cmpint(sts & 0xff, ==, TPM_TIS_STS_COMMAND_READY);/* transmit command */ for (i =0 ; i <sizeof (TPM_CMD ); i++) { -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_DATA_FIFO ,TPM_CMD [i]); - sts =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_STS ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_DATA_FIFO ,TPM_CMD [i]); + sts =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_STS );DPRINTF_STS ;if (i <sizeof (TPM_CMD ) -1 ) { g_assert_cmpint(sts & 0xff, ==,
@@ -509,21 +516,21 @@ static void tpm_tis_i2c_test_check_transmit(const void *data) g_assert_cmpint((sts >> 8) & 0xffff, ==, --bcount); }/* read the checksum */ - csum =tpm_tis_i2c_readw (0 ,TPM_I2C_REG_DATA_CSUM_GET ); + csum =tpm_tis_i2c_readw (global_qtest,0 ,TPM_I2C_REG_DATA_CSUM_GET ); g_assert_cmpint(csum, ==, 0x6733);/* start processing */ -tpm_tis_i2c_writeb (0 ,TPM_I2C_REG_STS ,TPM_TIS_STS_TPM_GO ); +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_STS ,TPM_TIS_STS_TPM_GO );uint64_t end_time =g_get_monotonic_time () +50 *G_TIME_SPAN_SECOND ;do { - sts =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_STS ); + sts =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_STS );if ((sts &TPM_TIS_STS_DATA_AVAILABLE ) !=0 ) {break ; } }while (g_get_monotonic_time () < end_time); - sts =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_STS ); + sts =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_STS );DPRINTF_STS ; g_assert_cmpint(sts & 0xff, == ,TPM_TIS_STS_VALID |TPM_TIS_STS_DATA_AVAILABLE );
@@ -534,8 +541,8 @@ static void tpm_tis_i2c_test_check_transmit(const void *data) g_assert_cmpint(sizeof(tpm_msg), ==, bcount);for (i =0 ; i <sizeof (tpm_msg); i++) { - tpm_msg[i] =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_DATA_FIFO ); - sts =tpm_tis_i2c_readl (0 ,TPM_I2C_REG_STS ); + tpm_msg[i] =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_DATA_FIFO ); + sts =tpm_tis_i2c_readl (global_qtest,0 ,TPM_I2C_REG_STS );DPRINTF_STS ;if (sts &TPM_TIS_STS_DATA_AVAILABLE ) { g_assert_cmpint((sts >> 8) & 0xffff, ==, --bcount);
@@ -544,9 +551,9 @@ static void tpm_tis_i2c_test_check_transmit(const void *data)g_assert_cmpmem (tpm_msg,sizeof (tpm_msg), s->tpm_msg,sizeof (*s->tpm_msg));/* relinquish use of locality 0 */ -tpm_tis_i2c_writeb (0 , +tpm_tis_i2c_writeb (global_qtest,0 ,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY ); - access =tpm_tis_i2c_readb (0 ,TPM_I2C_REG_ACCESS ); + access =tpm_tis_i2c_readb (global_qtest,0 ,TPM_I2C_REG_ACCESS ); }int main (int argc,char **argv)
tests/qtest/tpm-tis-i2c-util.c +16 -16
@@ -20,45 +20,45 @@ uint32_t aspeed_bus_addr;static uint8_t cur_locty =0xff ; -static void tpm_tis_i2c_set_locty (uint8_t locty) +static void tpm_tis_i2c_set_locty (QTestState *s,uint8_t locty) {if (cur_locty != locty) { cur_locty = locty; -aspeed_i2c_writeb (global_qtest, aspeed_bus_addr,I2C_SLAVE_ADDR , +aspeed_i2c_writeb (s, aspeed_bus_addr,I2C_SLAVE_ADDR ,TPM_I2C_REG_LOC_SEL , locty); } } -uint8_t tpm_tis_i2c_readb (uint8_t locty,uint8_t reg) +uint8_t tpm_tis_i2c_readb (QTestState *s,uint8_t locty,uint8_t reg) { -tpm_tis_i2c_set_locty (locty); -return aspeed_i2c_readb (global_qtest, aspeed_bus_addr,I2C_SLAVE_ADDR , reg); +tpm_tis_i2c_set_locty (s, locty); +return aspeed_i2c_readb (s, aspeed_bus_addr,I2C_SLAVE_ADDR , reg); } -uint16_t tpm_tis_i2c_readw (uint8_t locty,uint8_t reg) +uint16_t tpm_tis_i2c_readw (QTestState *s,uint8_t locty,uint8_t reg) { -tpm_tis_i2c_set_locty (locty); +tpm_tis_i2c_set_locty (s, locty);return aspeed_i2c_readw (global_qtest, aspeed_bus_addr,I2C_SLAVE_ADDR , reg); } -uint32_t tpm_tis_i2c_readl (uint8_t locty,uint8_t reg) +uint32_t tpm_tis_i2c_readl (QTestState *s,uint8_t locty,uint8_t reg) { -tpm_tis_i2c_set_locty (locty); -return aspeed_i2c_readl (global_qtest, aspeed_bus_addr,I2C_SLAVE_ADDR , reg); +tpm_tis_i2c_set_locty (s, locty); +return aspeed_i2c_readl (s, aspeed_bus_addr,I2C_SLAVE_ADDR , reg); } -void tpm_tis_i2c_writeb (uint8_t locty,uint8_t reg,uint8_t v) +void tpm_tis_i2c_writeb (QTestState *s,uint8_t locty,uint8_t reg,uint8_t v) {if (reg !=TPM_I2C_REG_LOC_SEL ) { -tpm_tis_i2c_set_locty (locty); +tpm_tis_i2c_set_locty (s, locty); } -aspeed_i2c_writeb (global_qtest, aspeed_bus_addr,I2C_SLAVE_ADDR , reg, v); +aspeed_i2c_writeb (s, aspeed_bus_addr,I2C_SLAVE_ADDR , reg, v); } -void tpm_tis_i2c_writel (uint8_t locty,uint8_t reg,uint32_t v) +void tpm_tis_i2c_writel (QTestState *s,uint8_t locty,uint8_t reg,uint32_t v) {if (reg !=TPM_I2C_REG_LOC_SEL ) { -tpm_tis_i2c_set_locty (locty); +tpm_tis_i2c_set_locty (s, locty); } -aspeed_i2c_writel (global_qtest, aspeed_bus_addr,I2C_SLAVE_ADDR , reg, v); +aspeed_i2c_writel (s, aspeed_bus_addr,I2C_SLAVE_ADDR , reg, v); }
tests/qtest/tpm-tis-i2c-util.h +5 -5
@@ -20,11 +20,11 @@ extern uint32_t aspeed_bus_addr;#define I2C_SLAVE_ADDR 0x2e#define I2C_DEV_BUS_NUM 10 -uint8_t tpm_tis_i2c_readb (uint8_t locty,uint8_t reg); -uint16_t tpm_tis_i2c_readw (uint8_t locty,uint8_t reg); -uint32_t tpm_tis_i2c_readl (uint8_t locty,uint8_t reg); +uint8_t tpm_tis_i2c_readb (QTestState *s,uint8_t locty,uint8_t reg); +uint16_t tpm_tis_i2c_readw (QTestState *s,uint8_t locty,uint8_t reg); +uint32_t tpm_tis_i2c_readl (QTestState *s,uint8_t locty,uint8_t reg); -void tpm_tis_i2c_writeb (uint8_t locty,uint8_t reg,uint8_t v); -void tpm_tis_i2c_writel (uint8_t locty,uint8_t reg,uint32_t v); +void tpm_tis_i2c_writeb (QTestState *s,uint8_t locty,uint8_t reg,uint8_t v); +void tpm_tis_i2c_writel (QTestState *s,uint8_t locty,uint8_t reg,uint32_t v);#endif /* TESTS_TPM_TIS_I2C_UTIL_H */
Signature · 1 line
2.54.0
[PULL v1 06/16] tests: Add a TPM TIS I2C swtpm test
In reply to Stefan BergerAdd a test case testing the TPM TIS over I2C with swtpm.
--- tests/qtest/meson.build | 5 +- tests/qtest/tpm-tis-i2c-swtpm-test.c | 82 ++++++++++++++++++++++++++++ tests/qtest/tpm-tis-i2c-util.c | 38 +++++++++++++ tests/qtest/tpm-tis-i2c-util.h | 4 ++ 4 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 tests/qtest/tpm-tis-i2c-swtpm-test.c
tests/qtest/meson.build +3 -2
@@ -244,7 +244,7 @@ qtests_arm = \ (config_all_devices.has_key('CONFIG_ASPEED_SOC') ? qtests_aspeed : []) + \ (config_all_devices.has_key('CONFIG_NPCM7XX') ? qtests_npcm7xx : []) + \ (config_all_devices.has_key('CONFIG_GENERIC_LOADER') ? ['hexloader-test'] : []) + \ - (config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test'] : []) + \ + (config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test', 'tpm-tis-i2c-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_VEXPRESS') ? ['test-arm-mptimer'] : []) + \ (config_all_devices.has_key('CONFIG_MICROBIT') ? ['microbit-test'] : []) + \ (config_all_devices.has_key('CONFIG_STM32L4X5_SOC') ? qtests_stm32l4x5 : []) + \
@@ -263,7 +263,7 @@ qtests_aarch64 = \ (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test', 'xlnx-versal-trng-test'] : []) + \ (config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test', 'bcm2835-i2c-test'] : []) + \ (config_all_accel.has_key('CONFIG_TCG') and \ - config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test'] : []) + \ + config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test', 'tpm-tis-i2c-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_ASPEED_SOC') ? qtests_aspeed64 : []) + \ (config_all_devices.has_key('CONFIG_NPCM8XX') ? qtests_npcm8xx : []) + \ (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
@@ -402,6 +402,7 @@ qtests = { 'tpm-tis-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'], 'tpm-tis-test': [io, tpmemu_files, 'tpm-tis-util.c'], 'tpm-tis-i2c-test': [io, tpmemu_files, 'tpm-tis-i2c-util.c', 'qtest_aspeed.c'], + 'tpm-tis-i2c-swtpm-test': [io, tpmemu_files, 'tpm-tis-i2c-util.c', 'qtest_aspeed.c'], 'tpm-tis-device-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'], 'tpm-tis-device-test': [io, tpmemu_files, 'tpm-tis-util.c'], 'virtio-net-failover': test_migration_files,
tests/qtest/tpm-tis-i2c-swtpm-test.c new file +82 -0
82 lines not on this page · raw message
tests/qtest/tpm-tis-i2c-util.c +38 -0
@@ -15,6 +15,7 @@#include "libqtest-single.h" #include "qtest_aspeed.h" #include "tpm-tis-i2c-util.h" +#include "tpm-emu.h" uint32_t aspeed_bus_addr;
@@ -62,3 +63,40 @@ void tpm_tis_i2c_writel(QTestState *s, uint8_t locty, uint8_t reg, uint32_t v) }aspeed_i2c_writel (s, aspeed_bus_addr,I2C_SLAVE_ADDR , reg, v); } + +void tpm_tis_i2c_transfer (QTestState *s, +const unsigned char *req,size_t req_size, +unsigned char *rsp,size_t rsp_size) +{ +uint32_t sts; +size_t i; + +/* request use of locality 0 */ +tpm_tis_i2c_writeb (s,0 ,TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_REQUEST_USE ); + +tpm_tis_i2c_writel (s,0 ,TPM_I2C_REG_STS ,TPM_TIS_STS_COMMAND_READY ); + +/* transmit command */ +for (i =0 ; i < req_size; i++) { +tpm_tis_i2c_writeb (s,0 ,TPM_I2C_REG_DATA_FIFO , req[i]); + } + +/* start processing */ +tpm_tis_i2c_writeb (s,0 ,TPM_I2C_REG_STS ,TPM_TIS_STS_TPM_GO ); + +uint64_t end_time =g_get_monotonic_time () +50 *G_TIME_SPAN_SECOND ; +do { + sts =tpm_tis_i2c_readl (s,0 ,TPM_I2C_REG_STS ); +if ((sts &TPM_TIS_STS_DATA_AVAILABLE ) !=0 ) { +break ; + } + }while (g_get_monotonic_time () < end_time); + +/* read response */ +for (i =0 ; i < rsp_size; i++) { + rsp[i] =tpm_tis_i2c_readb (s,0 ,TPM_I2C_REG_DATA_FIFO ); + } +/* relinquish use of locality 0 */ +tpm_tis_i2c_writeb (s,0 , +TPM_I2C_REG_ACCESS ,TPM_TIS_ACCESS_ACTIVE_LOCALITY ); +}
tests/qtest/tpm-tis-i2c-util.h +4 -0
@@ -27,4 +27,8 @@ uint32_t tpm_tis_i2c_readl(QTestState *s, uint8_t locty, uint8_t reg);void tpm_tis_i2c_writeb (QTestState *s,uint8_t locty,uint8_t reg,uint8_t v);void tpm_tis_i2c_writel (QTestState *s,uint8_t locty,uint8_t reg,uint32_t v); +void tpm_tis_i2c_transfer (QTestState *s, +const unsigned char *req,size_t req_size, +unsigned char *rsp,size_t rsp_size); +#endif /* TESTS_TPM_TIS_I2C_UTIL_H */
Signature · 1 line
2.54.0
[PULL v1 07/16] migration/vmstate: Add VMState support for GByteArray
In reply to Stefan BergerFrom: Arun Menon <[email protected]>
In GLib, GByteArray is an object managed by the library. Currently, migrating a GByteArray requires treating it as a raw C struct and using VMSTATE_VBUFFER_ALLOC_UINT32. For example, see vmstate_vdba in ui/vdagent.c
QEMU cannot pretend that GByteArray is a C struct and simply use VMS_ALLOC to g_malloc() the buffer. This is because, VMS_ALLOC blindly overwrites the data pointer with a newly allocated buffer, thereby leaking the previous memory. Besides, GLib tracks the array's capacity in a hidden alloc field. Bypassing GLib APIs leave this capacity out of sync with the newly allocated buffer, potentially leading to heap buffer overflows during subsequent g_byte_array_append() calls.
This commit introduces VMSTATE_GBYTEARRAY which uses specific library API calls (g_byte_array_set_size()) to safely resize and populate the buffer.
--- include/migration/vmstate.h | 10 ++++++++++ migration/vmstate-types.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)
include/migration/vmstate.h +10 -0
@@ -308,6 +308,7 @@ extern const VMStateInfo vmstate_info_bitmap;extern const VMStateInfo vmstate_info_qtailq;extern const VMStateInfo vmstate_info_gtree;extern const VMStateInfo vmstate_info_qlist; +extern const VMStateInfo vmstate_info_g_byte_array;#define type_check_2darray (t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)/*
@@ -957,6 +958,15 @@ extern const VMStateInfo vmstate_info_qlist; .start = offsetof(_type, _next), \ } +#define VMSTATE_GBYTEARRAY (_field, _state, _version) { \ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .size = sizeof(GByteArray), \ + .info = &vmstate_info_g_byte_array, \ + .flags = VMS_SINGLE, \ + .offset = vmstate_offset_pointer(_state, _field, GByteArray), \ +} +/* _f : field name _f_n : num of elements field_name _n : num of elements
migration/vmstate-types.c +28 -0
@@ -924,3 +924,31 @@ const VMStateInfo vmstate_info_qlist = { .load = load_qlist, .save = save_qlist, }; + +static int get_g_byte_array (QEMUFile *f,void *pv,size_t size, +const VMStateField *field) +{ +GByteArray *byte_array = *(GByteArray **)pv; +uint32_t len =qemu_get_be32 (f); + +g_byte_array_set_size (byte_array, len); +qemu_get_buffer (f, byte_array->data, len); +return 0 ; +} + +static int put_g_byte_array (QEMUFile *f,void *pv,size_t size, +const VMStateField *field,JSONWriter *vmdesc) +{ +GByteArray *byte_array = *(GByteArray **)pv; + +qemu_put_be32 (f, byte_array->len); +qemu_put_buffer (f, byte_array->data, byte_array->len); + +return 0 ; +} + +const VMStateInfo vmstate_info_g_byte_array = { + .name ="GByteArray" , + .get = get_g_byte_array, + .put = put_g_byte_array, +};
Signature · 1 line
2.54.0
Stefan Hajnoczi
AppliedApplied, thanks.
Please update the changelog at https://wiki.qemu.org/ChangeLog/11.1 for any user-visible changes.