hw/tpm: CRB chunking capability to handle PQC
v2 cover hw/tpm: CRB chunking capability to handle PQC
The move to Post Quantum Cryptography (PQC) changes how we manage memory buffers. Unlike classic crypto algorithms like RSA or ECC which used small keys and signatures, PQC algorithms require larger buffers.
The new version of TCG TPM v185 (currently under review [1]) supports sending data/commands in chunks for the CRB (Command Response Buffer) interface. This is in line with the initiative to support PQC algorithms.
This series implements the logic to send and receive data from the linux guest to the TPM backend in chunks, thereby allowing the guest to send larger data buffers. We introduce 2 new control registers called nextChunk and crbRspRetry that will control the START. We also add the CRB Interface Identifier called CapCRBChunk that is set to 1 indicating that the device supports chunking. The default maximum chunk/buffer size is 3968 (4096 - 128) bytes.
During a send operation, the guest driver places data in the CRB buffer and signals nextChunk for each segment until the final chunk is reached. Upon receiving the START signal, QEMU appends the final chunk to its internal buffer and dispatches the complete command to the TPM backend.
For responses, the backend's output is buffered. The guest consumes the first chunk once the START bit is cleared. Subsequent chunks are retrieved by the guest toggling the nextChunk bit, which advances the internal buffer offset and populates the CRB data window.
For this to work, the linux guest tpm driver will also have to
a) probe if CRB chunking is supported
b) send data in chunks if the command length exceeds the chunk size.
c) receive data in chunks by sending a nextChunk signal and accumulate.
The included test demonstrates functional correctness for standard buffer sizes. However, validation of PQC-sized payloads was performed via manual buffer-size overrides.
v2
--
- Add the VM migration support.
- Increase the TIS TPM interface max buffer size to 8192.
Arun Menon (7):
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 hw/tpm: Increase TPM TIS max buffer size to 8192
hw/core/machine.c | 1 + hw/tpm/tpm_crb.c | 291 ++++++++++++++++++++++++++++--- hw/tpm/tpm_tis.h | 2 +- include/hw/acpi/tpm.h | 5 +- tests/qtest/tpm-crb-swtpm-test.c | 10 ++ tests/qtest/tpm-util.c | 106 +++++++++-- tests/qtest/tpm-util.h | 5 + 7 files changed, 382 insertions(+), 38 deletions(-)
Signature · 1 line
2.53.0
RFC v2 1/7 hw/tpm: Add TPM CRB chunking fields
- 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]
--- hw/tpm/tpm_crb.c | 3 +++ include/hw/acpi/tpm.h | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-)
hw/tpm/tpm_crb.c +3 -0
@@ -59,6 +59,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)
@@ -262,6 +263,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,CRB_INTF_CAP_CRB_CHUNK );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
@@ -149,7 +149,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 )
@@ -168,6 +168,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, invoke, 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.53.0
RFC v2 2/7 hw/tpm: Refactor CRB_CTRL_START register access
In reply to Arun MenonReplace 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
@@ -145,7 +145,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 , invoke,1 ); s->cmd = (TPMBackendCmd ) { .in = mem, .in_len =MIN (tpm_cmd_get_size (mem), s->be_buffer_size),
@@ -194,7 +194,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 , invoke,0 );if (ret !=0 ) {ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_STS , tpmSts,1 );/* fatal error */
Signature · 1 line
2.53.0
RFC v2 3/7 hw/tpm: Add internal buffer state for chunking
In reply to Arun Menon- 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 | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-)
hw/tpm/tpm_crb.c +20 -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; +size_t response_offset;bool ppi_enabled;TPMPPI ppi;
@@ -85,6 +88,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) {
@@ -134,9 +144,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 :
@@ -240,6 +252,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));
@@ -306,6 +319,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 (); +if (s->ppi_enabled) {tpm_ppi_init (&s->ppi,get_system_memory (),TPM_PPI_ADDR_BASE ,OBJECT (s));
Signature · 1 line
2.53.0
Re: [RFC v2 3/7] hw/tpm: Add internal buffer state for chunking
On Thu, 19 Mar 2026 19:23:12 +0530, Arun Menon <[email protected]> wrote:
Hi
Quoted 4 lines
- Introduce GByteArray buffers to hold the command request and response data during chunked TPM CRB transactions.
- Add helper function to clean them.
I would introduce them where they are actually used, but ok
hw/tpm/tpm_crb.c static void tpm_crb_realize(DeviceState *dev, Error **errp) · line 323
Quoted 12 lines
hw/tpm/tpm_crb.c +2
-0@@ -306,6 +319,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 ();
These buffers are allocated here but never freed. Please add an unrealize or finalize handler that calls g_byte_array_unref() on both buffers.
Signature · 1 line
Marc-André Lureau <[email protected]>
RFC v2 4/7 hw/tpm: Implement TPM CRB chunking logic
In reply to Arun Menon- 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 | 148 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 132 insertions(+), 16 deletions(-)
hw/tpm/tpm_crb.c +132 -16
@@ -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"
@@ -65,6 +66,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 ),
@@ -80,6 +82,8 @@ enum crb_ctrl_req {enum crb_start {CRB_START_INVOKE =BIT (0 ), +CRB_START_RESP_RETRY =BIT (1 ), +CRB_START_NEXT_CHUNK =BIT (2 ), };enum crb_cancel {
@@ -122,6 +126,68 @@ 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 , invoke,0 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); +tpm_crb_clear_internal_buffers (s); +error_report ("Command size '%d' less than TPM header size '%d'" , + total_request_size,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) {
@@ -152,20 +218,48 @@ 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); - -ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , invoke,1 ); - 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, - }; - -tpm_backend_deliver_request (s->tpmbe, &s->cmd); +if (tpm_crb_get_active_locty (s) != locty) { +break ; + } +if (val &CRB_START_INVOKE ) { +if (!(s->regs[R_CRB_CTRL_START ] &CRB_START_INVOKE )) { +if (!tpm_crb_append_command_request (s)) { +break ; + } +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , invoke,1 ); +g_byte_array_set_size (s->response_buffer, s->be_buffer_size); + s->cmd = (TPMBackendCmd ) { + .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 ) { +/* +* 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_RESP_RETRY ) { +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 :
@@ -205,13 +299,36 @@ static const MemoryRegionOps tpm_crb_memory_ops = {static void tpm_crb_request_completed (TPMIf *ti,int ret) {CRBState *s =CRB (ti); +void *mem =memory_region_get_ram_ptr (&s->cmdmem);ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , invoke,0 );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 ; + +/* +* Send the first chunk. Subsequent chunks will be sent using +* tpm_crb_fill_command_response() +*/ +uint32_t to_copy =MIN (CRB_CTRL_CMD_SIZE , s->response_buffer->len); +memcpy (mem, s->response_buffer->data, 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 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , invoke,0 ); +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)
@@ -288,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.53.0
Re: [RFC v2 4/7] hw/tpm: Implement TPM CRB chunking logic
On Thu, 19 Mar 2026 19:23:13 +0530, Arun Menon <[email protected]> wrote:
Hi,
hw/tpm/tpm_crb.c enum crb_ctrl_req { · line 85
Quoted 10 lines
hw/tpm/tpm_crb.c +1
-0@@ -80,6 +82,8 @@ enum crb_ctrl_req {enum crb_start {CRB_START_INVOKE =BIT (0 ), +CRB_START_RESP_RETRY =BIT (1 ),
Hmm, maybe we should rename that field, since it is "Start" in the spec (it changed?).
hw/tpm/tpm_crb.c enum crb_ctrl_req { · line 87
Quoted 2 lines
hw/tpm/tpm_crb.c +1
-0+ CRB_START_NEXT_CHUNK = BIT(2), };
And follow the spec naming here "RspRetry" -> RSP_RETRY
hw/tpm/tpm_crb.c static uint8_t tpm_crb_get_active_locty(CRBState *s) · line 154
Quoted 8 lines
hw/tpm/tpm_crb.c +0
-0@@ -122,6 +126,68 @@ static uint8_t tpm_crb_get_active_locty(CRBState *s)[ ... skip 23 lines ... ]
hw/tpm/tpm_crb.c +6
-0+ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_STS , tpmSts,1 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , invoke,0 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); +tpm_crb_clear_internal_buffers (s); +error_report ("Command size '%d' less than TPM header size '%d'" , + total_request_size,TPM_HEADER_SIZE );
Use PRIu32 to avoid sign sign-mismatch
hw/tpm/tpm_crb.c static uint8_t tpm_crb_get_active_locty(CRBState *s) · line 188
Quoted 7 lines
[ ... skip 28 lines ... ]
hw/tpm/tpm_crb.c +6
-0+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 );
Those lines are repeated below and could be factorized.
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 230
Quoted 8 lines
hw/tpm/tpm_crb.c +0
-0@@ -152,20 +218,48 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr,[ ... skip 21 lines ... ]
hw/tpm/tpm_crb.c +6
-0+if (!(s->regs[R_CRB_CTRL_START ] &CRB_START_INVOKE )) { +if (!tpm_crb_append_command_request (s)) { +break ; + } +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , invoke,1 ); +g_byte_array_set_size (s->response_buffer, s->be_buffer_size);
This pre-allocates the response buffer before the backend has written anything. Since response_buffer->len > 0 now, a subsequent nextChunk from the guest would enter tpm_crb_fill_command_response() and serve uninitialized data.
Before this patch we would serve guest input data, which wasn't great either.
We could add a separate flag to track response readiness instead. This will need to be migrated too.
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 242
Quoted 12 lines
hw/tpm/tpm_crb.c +12
-0+ s->cmd = (TPMBackendCmd ) { + .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 ) { +/* +* nextChunk is used both while sending and receiving data. +* To distinguish between the two, response_buffer is checked
Missing .
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 249
Quoted 7 lines
hw/tpm/tpm_crb.c +7
-0+* 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);
Indentation <4
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 257
Quoted 8 lines
hw/tpm/tpm_crb.c +8
-0+ }else { +if (!tpm_crb_append_command_request (s)) { +break ; + } + } +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); + }else if (val &CRB_START_RESP_RETRY ) { +if (s->response_buffer->len >0 ) {
I'd suggest adding a trace here.
hw/tpm/tpm_crb.c static const MemoryRegionOps tpm_crb_memory_ops = { · line 319
Quoted 8 lines
hw/tpm/tpm_crb.c +0
-0@@ -205,13 +299,36 @@ static const MemoryRegionOps tpm_crb_memory_ops = {[ ... skip 15 lines ... ]
hw/tpm/tpm_crb.c +6
-0+ +/* +* Send the first chunk. Subsequent chunks will be sent using +* tpm_crb_fill_command_response() +*/ +uint32_t to_copy =MIN (CRB_CTRL_CMD_SIZE , s->response_buffer->len);
QEMU coding style: declaration not mixed with statements.
hw/tpm/tpm_crb.c static const MemoryRegionOps tpm_crb_memory_ops = { · line 327
Quoted 8 lines
hw/tpm/tpm_crb.c +6
-0+memcpy (mem, s->response_buffer->data, 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 );
Consider factorizing
Quoted 1 line
+
ARRAY_FIELD_DP32(s->regs, CRB_CTRL_START, invoke, 0);
redundant clear
Signature · 1 line
Marc-André Lureau <[email protected]>
Re: [RFC v2 4/7] hw/tpm: Implement TPM CRB chunking logic
Hi Marc-André,
Thank you for the review.
Quoted 11 lines · On Thu, Mar 26, 2026 at 03:27:02PM +0400, [email protected] wrote:
On Thu, 19 Mar 2026 19:23:13 +0530, Arun Menon <[email protected]> wrote:
Hi,
Quoted 7 lines
hw/tpm/tpm_crb.c +0
-0@@ -80,6 +82,8 @@ enum crb_ctrl_req {
Quoted 5 lines
hw/tpm/tpm_crb.c enum crb_ctrl_req { · line 85
Quoted 3 lines
hw/tpm/tpm_crb.c +1
-0enum crb_start {CRB_START_INVOKE =BIT (0 ), +CRB_START_RESP_RETRY =BIT (1 ),Hmm, maybe we should rename that field, since it is "Start" in the spec (it changed?).
The Start has not changed from before.
Yes, I shall make changes to the other 2.
Quoted 6 lines
hw/tpm/tpm_crb.c enum crb_ctrl_req { · line 87
Quoted 3 lines
hw/tpm/tpm_crb.c +1
-0+ CRB_START_NEXT_CHUNK = BIT(2), };And follow the spec naming here "RspRetry" ->
RSP_RETRY
Quoted 10 lines
hw/tpm/tpm_crb.c static uint8_t tpm_crb_get_active_locty(CRBState *s) · line 154
Quoted 8 lines
hw/tpm/tpm_crb.c +0
-0@@ -122,6 +126,68 @@ static uint8_t tpm_crb_get_active_locty(CRBState *s)[ ... skip 23 lines ... ]
hw/tpm/tpm_crb.c +6
-0+ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_STS , tpmSts,1 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , invoke,0 ); +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); +tpm_crb_clear_internal_buffers (s); +error_report ("Command size '%d' less than TPM header size '%d'" , + total_request_size,TPM_HEADER_SIZE );Use PRIu32 to avoid sign sign-mismatch
Yes. Will do.
Quoted 10 lines
hw/tpm/tpm_crb.c static uint8_t tpm_crb_get_active_locty(CRBState *s) · line 188
Quoted 8 lines
[ ... skip 28 lines ... ]
hw/tpm/tpm_crb.c +6
-0+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 );Those lines are repeated below and could be factorized.
Yes, I will move it into a separate function
Quoted 15 lines
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 230
Quoted 9 lines
hw/tpm/tpm_crb.c +0
-0@@ -152,20 +218,48 @@ static void tpm_crb_mmio_write(void *opaque, hwaddr addr,[ ... skip 21 lines ... ]
hw/tpm/tpm_crb.c +6
-0+if (!(s->regs[R_CRB_CTRL_START ] &CRB_START_INVOKE )) { +if (!tpm_crb_append_command_request (s)) { +break ; + } +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , invoke,1 ); +g_byte_array_set_size (s->response_buffer, s->be_buffer_size);This pre-allocates the response buffer before the backend has written anything. Since
response_buffer->len> 0 now, a subsequent nextChunk from the guest would entertpm_crb_fill_command_response()and serve uninitialized data.
Quoted 5 lines
Before this patch we would serve guest input data, which wasn't great either.
We could add a separate flag to track response readiness instead. This will need to be migrated too.
Good catch. I did not think of what happens when the guest issues a subsequent nextChunk while the tpm backend has not yet processed the command.
I am wondering, checking if the start invoke bit is set will alone prevent such a situation in the first place.
For example adding the following at the beginning,
case A_CRB_CTRL_START:
if (s->regs[R_CRB_CTRL_START] & CRB_START_INVOKE) {
break;
}That way, a subsequent nextChunk or rspRetry will not be processed in the first place. Do you see any problem with this approach? This will also allow us to keep the migration logic as is.
Quoted 14 lines
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 242
Quoted 13 lines
hw/tpm/tpm_crb.c +12
-0+ s->cmd = (TPMBackendCmd ) { + .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 ) { +/* +* nextChunk is used both while sending and receiving data. +* To distinguish between the two, response_buffer is checked
Quoted 1 line
Missing .
Will correct.
Quoted 10 lines
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 249
Quoted 8 lines
hw/tpm/tpm_crb.c +7
-0+* 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);Indentation <4
Yes.
Quoted 11 lines
hw/tpm/tpm_crb.c static void tpm_crb_mmio_write(void *opaque, hwaddr addr, · line 257
Quoted 9 lines
hw/tpm/tpm_crb.c +8
-0+ }else { +if (!tpm_crb_append_command_request (s)) { +break ; + } + } +ARRAY_FIELD_DP32 (s->regs,CRB_CTRL_START , nextChunk,0 ); + }else if (val &CRB_START_RESP_RETRY ) { +if (s->response_buffer->len >0 ) {I'd suggest adding a trace here.
Sure, will add.
Quoted 11 lines
hw/tpm/tpm_crb.c static const MemoryRegionOps tpm_crb_memory_ops = { · line 319
Quoted 9 lines
hw/tpm/tpm_crb.c +0
-0@@ -205,13 +299,36 @@ static const MemoryRegionOps tpm_crb_memory_ops = {[ ... skip 15 lines ... ]
hw/tpm/tpm_crb.c +6
-0+ +/* +* Send the first chunk. Subsequent chunks will be sent using +* tpm_crb_fill_command_response() +*/ +uint32_t to_copy =MIN (CRB_CTRL_CMD_SIZE , s->response_buffer->len);QEMU coding style: declaration not mixed with statements.
I will add this to the top of the block.
Quoted 10 lines
hw/tpm/tpm_crb.c static const MemoryRegionOps tpm_crb_memory_ops = { · line 327
Quoted 9 lines
hw/tpm/tpm_crb.c +6
-0+memcpy (mem, s->response_buffer->data, 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 );
Quoted 5 lines
Consider factorizing
Quoted 1 line
+
ARRAY_FIELD_DP32(s->regs, CRB_CTRL_START, invoke, 0);redundant clear
yes, shall remove.
Quoted 5 lines
Signature · 2 lines
-- Marc-André Lureau <[email protected]>
Regards,
Arun Menon
RFC v2 5/7 test/qtest: Add test for tpm crb chunking
In reply to Arun Menon- 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()
--- tests/qtest/tpm-crb-swtpm-test.c | 10 +++ tests/qtest/tpm-util.c | 106 ++++++++++++++++++++++++++----- tests/qtest/tpm-util.h | 5 ++ 3 files changed, 106 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 +91 -15
131 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.53.0
Re: [RFC v2 5/7] test/qtest: Add test for tpm crb chunking
On Thu, 19 Mar 2026 19:23:14 +0530, Arun Menon <[email protected]> wrote:
Hi
tests/qtest/tpm-util.c line 17
Quoted 10 lines
tests/qtest/tpm-util.c +1
-0@@ -14,16 +14,42 @@#include "qemu/osdep.h" #include <glib/gstdio.h> +#include "system/memory.h"
Use qemu/bswap.h instead
tests/qtest/tpm-util.c line 26
Quoted 9 lines
tests/qtest/tpm-util.c +3
-0#include "hw/acpi/tpm.h" #include "libqtest.h" #include "tpm-util.h" #include "qobject/qdict.h" +#define CRB_ADDR_START (TPM_CRB_ADDR_BASE + A_CRB_CTRL_START) +#define CRB_CTRL_STS (TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS) +#define CRB_CTRL_CMD_SIZE (TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_SIZE)
Shadows tpm.h macro. Use CRB_ADDR_CTRL_STS ?
tests/qtest/tpm-util.c line 28
Quoted 2 lines
tests/qtest/tpm-util.c +2
-0+ +#define BIT_START_INVOKE (1 << 0)
Similar (although in tpm_crb.c unit)
tests/qtest/tpm-util.c line 45
Quoted 7 lines
[ ... skip 11 lines ... ]
tests/qtest/tpm-util.c +6
-0+break ; + } +if (g_get_monotonic_time () >= end_time) { +break ; + } + }
Consider adding a g_assert() here
tests/qtest/tpm-util.c void tpm_util_crb_transfer(QTestState *s, · line 85
Quoted 8 lines
tests/qtest/tpm-util.c +0
-0@@ -31,24 +57,74 @@ void tpm_util_crb_transfer(QTestState *s,[ ... skip 30 lines ... ]
tests/qtest/tpm-util.c +6
-0+ +qtest_writeb (s,TPM_CRB_ADDR_BASE +A_CRB_LOC_CTRL ,1 ); + +for (size_t i =0 ; i < req_size; i += chunk_size) { +bool last_chunk = false; +size_t current_chunk_size = chunk_size ;
Trailing whitespace before the semicolon.
tests/qtest/tpm-util.c void tpm_util_crb_transfer(QTestState *s, · line 95
Quoted 12 lines
tests/qtest/tpm-util.c +9
-2+ +if (i + chunk_size > req_size) { + last_chunk = true; + current_chunk_size = req_size - i; } -if (g_get_monotonic_time () >= end_time) { -break ; + +qtest_memwrite (s, caddr, req + i, current_chunk_size); + +if (last_chunk) { +qtest_writel (s,CRB_ADDR_START ,BIT_START_INVOKE );
Trailing whitespace before the comma.
Style: should be tpm_sts per QEMU naming conventions.
Style: declarations should be at the top of the function.
Same: declaration mixed with statements.
Style: should be tpm_sts per QEMU naming conventions.
Style: declarations should be at the top of the function.
Same: declaration mixed with statements.
Signature · 1 line
Marc-André Lureau <[email protected]>
Re: [RFC v2 5/7] test/qtest: Add test for tpm crb chunking
Hi
Quoted 71 lines · On Thu, Mar 26, 2026 at 3:27 PM <[email protected]> wrote:
On Thu, 19 Mar 2026 19:23:14 +0530, Arun Menon <[email protected]> wrote:
Hi
tests/qtest/tpm-util.c line 17
Quoted 10 lines
tests/qtest/tpm-util.c +1
-0@@ -14,16 +14,42 @@#include "qemu/osdep.h" #include <glib/gstdio.h> +#include "system/memory.h" Use
qemu/bswap.hinsteadtests/qtest/tpm-util.c line 26
Quoted 9 lines
tests/qtest/tpm-util.c +3
-0#include "hw/acpi/tpm.h" #include "libqtest.h" #include "tpm-util.h" #include "qobject/qdict.h" +#define CRB_ADDR_START (TPM_CRB_ADDR_BASE + A_CRB_CTRL_START) +#define CRB_CTRL_STS (TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS) +#define CRB_CTRL_CMD_SIZE (TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_SIZE)Shadows
tpm.hmacro. UseCRB_ADDR_CTRL_STS?tests/qtest/tpm-util.c line 28
Quoted 2 lines
tests/qtest/tpm-util.c +2
-0+ +#define BIT_START_INVOKE (1 << 0)Similar (although in
tpm_crb.cunit)tests/qtest/tpm-util.c line 45
Quoted 7 lines
[ ... skip 11 lines ... ]
tests/qtest/tpm-util.c +6
-0+break ; + } +if (g_get_monotonic_time () >= end_time) { +break ; + } + }Consider adding a
g_assert()heretests/qtest/tpm-util.c void tpm_util_crb_transfer(QTestState *s, · line 85
Quoted 8 lines
tests/qtest/tpm-util.c +0
-0@@ -31,24 +57,74 @@ void tpm_util_crb_transfer(QTestState *s,[ ... skip 30 lines ... ]
tests/qtest/tpm-util.c +6
-0+ +qtest_writeb (s,TPM_CRB_ADDR_BASE +A_CRB_LOC_CTRL ,1 ); + +for (size_t i =0 ; i < req_size; i += chunk_size) { +bool last_chunk = false; +size_t current_chunk_size = chunk_size ;Trailing whitespace before the semicolon.
tests/qtest/tpm-util.c void tpm_util_crb_transfer(QTestState *s, · line 95
Quoted 12 lines
tests/qtest/tpm-util.c +9
-2+ +if (i + chunk_size > req_size) { + last_chunk = true; + current_chunk_size = req_size - i; } -if (g_get_monotonic_time () >= end_time) { -break ; + +qtest_memwrite (s, caddr, req + i, current_chunk_size); + +if (last_chunk) { +qtest_writel (s,CRB_ADDR_START ,BIT_START_INVOKE );Trailing whitespace before the comma.
Quoted 8 lines
Style: should be
tpm_stsper QEMU naming conventions.
Style: declarations should be at the top of the function.
Same: declaration mixed with statements.Style: should be
tpm_stsper QEMU naming conventions.
Style: declarations should be at the top of the function.
Same: declaration mixed with statements.
This is the first time I used "b4 review tui", this is strange. I'll check what happened :)
I'd recommend others to try it, especially those like me who lack a good "all-in-terminal" workflow for reviews.
Re: [RFC v2 5/7] test/qtest: Add test for tpm crb chunking
In reply to Arun MenonQuoted 7 lines · Arun Menon
- 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 theCRB_CTRL_CMD_SIZEaddress. This can be changed manually to test.
- Add a helper functiontpm_wait_till_bit_clear()Signed-off-by Arun Menon <[email protected]>
FYI: Based on this test here I have now created two more patches that:
- allow starting swtpm with a profile
- extends your test case to check whether default-v2 profile (with ML-DSA support) is available
- creates an ML-DSA-87 key
- WIP: the test case will have to do a signing operation with the key to receive the >4k signature; I don't have a tool yet to create the necessary command byte stream(s), so this is work-in-progress
https://github.com/stefanberger/qemu-tpm/commits/crb-chunking/
RFC v2 6/7 hw/tpm: Add support for VM migration with TPM CRB chunking
In reply to Arun Menon- Add subsection in VMState for TPM CRB with the newly introduced command and response buffers, along with a needed callback, so that newer QEMU only sends the buffers if it is necessary.
- Add hw_compat blocker because the feature is only supported for machine type 11.0 and higher.
- If the VM has no pending chunked TPM commands in the internal buffers during a VM migration, or if the machine type does not support newly introduced buffers, then the needed callback will return false, as it checks the hw_compat blocker and thus the subsection will not be sent to the destination host.
- Since the original command and response buffers are of type GByteArray, they are serialized in pre-save hook before sending them to the destination host and then restored back into original buffer using the post-load hook.
--- hw/core/machine.c | 1 + hw/tpm/tpm_crb.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+)
hw/core/machine.c +1 -0
@@ -40,6 +40,7 @@GlobalProperty hw_compat_10_2[] = { {"scsi-block" ,"migrate-pr" ,"off" }, + {"tpm-crb" ,"migrate-buffers" ,"off" }, };const size_t hw_compat_10_2_len =G_N_ELEMENTS (hw_compat_10_2);
hw/tpm/tpm_crb.c +114 -0
144 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [RFC v2 6/7] hw/tpm: Add support for VM migration with TPM CRB chunking
On Thu, 19 Mar 2026 19:23:15 +0530, Arun Menon <[email protected]> wrote:
Hi
hw/core/machine.c line 41
Quoted 8 lines
hw/core/machine.c +0
-0@@ -40,6 +40,7 @@GlobalProperty hw_compat_10_2[] = {
Let's not forget to update this to 11.0 for 11.1 :)
hw/tpm/tpm_crb.c line 39
Quoted 13 lines
hw/tpm/tpm_crb.c +4
-0@@ -33,6 +33,17 @@#include "trace.h" #include "qom/object.h" +/* command and response buffers; part of VM state when migrating */ +typedef struct TPMCRBMigState { + uint32_t cmd_size; + uint8_t *cmd_tmp;
Could we name them after the name of the state fields? command_buffer_len && command_buffer_data ?
hw/tpm/tpm_crb.c line 42
Quoted 3 lines
hw/tpm/tpm_crb.c +3
-0+ +uint32_t rsp_size; +uint8_t *rsp_tmp;
(response_buffer_len ..)
Actually, you should not need an extra MigState at all. Check how GByteArray are handled in in ui/vdagent.c for example. (I have a doubt that it may eventually leak if loading for a running state, where data != NULL though, I would have to check - we may want to use realloc for buffers)
hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 396
Quoted 8 lines
hw/tpm/tpm_crb.c +0
-0@@ -347,18 +361,118 @@ static int tpm_crb_pre_save(void *opaque)[ ... skip 30 lines ... ]
hw/tpm/tpm_crb.c +6
-0+ }else { + s->mig.rsp_tmp =NULL ; + s->mig.rsp_size =0 ; + } + s->mig.rsp_offset = (uint32_t )s->response_offset; +return 0 ;
If you get rid of MigState, the original response_offset field will proably have to be uint32_t too.
hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 416
Quoted 7 lines
[ ... skip 14 lines ... ]
hw/tpm/tpm_crb.c +6
-0+g_free (s->mig.cmd_tmp); + s->mig.cmd_tmp =NULL ; +g_free (s->mig.rsp_tmp); + s->mig.rsp_tmp =NULL ; +return false; + }
Suggesting g_clear_pointer(&ptr, g_free)
hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 421
Quoted 5 lines
hw/tpm/tpm_crb.c +5
-0+ +if (s->mig.cmd_tmp) { +if (s->command_buffer) { +g_byte_array_unref (s->command_buffer); + }
Slightly neater:g_clear_pointer(&s->command_buffer, g_byte_array_unref);
hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 433
Quoted 12 lines
hw/tpm/tpm_crb.c +12
-0+ s->command_buffer =g_byte_array_new_take (s->mig.cmd_tmp, + s->mig.cmd_size); + s->mig.cmd_tmp =NULL ; + }else { +if (s->command_buffer) { +g_byte_array_set_size (s->command_buffer,0 ); + } + } +if (s->mig.rsp_tmp) { +if (s->response_buffer) { +g_byte_array_unref (s->response_buffer); + }
Or you could simply without condition:
g_clear_pointer(&s->command_buffer, g_byte_array_unref); s->command_buffer = g_byte_array_new_take(s->mig.cmd_tmp, s->mig.cmd_size);
This will also reset the size to 0 if cmd_tmp is NULL and cmd_size is 0.
hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 437
Quoted 4 lines
hw/tpm/tpm_crb.c +4
-0+ s->response_buffer =g_byte_array_new_take (s->mig.rsp_tmp, + s->mig.rsp_size); + s->mig.rsp_tmp =NULL ; + }
Missing
s->response_offset = s->mig.rsp_offset;
hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 448
Quoted 11 lines
hw/tpm/tpm_crb.c +11
-0+return true; +} + +static const VMStateDescription vmstate_tpm_crb_chunk = { + .name ="tpm-crb/chunk" , + .version_id =1 , + .needed = tpm_crb_chunk_needed, + .pre_save = tpm_crb_chunk_pre_save, + .post_load_errp = tpm_crb_chunk_post_load, + .fields = (const VMStateField []) { +VMSTATE_UINT32 (mig.cmd_size, CRBState),
Could be version 0, I think.
Signature · 1 line
Marc-André Lureau <[email protected]>
Re: [RFC v2 6/7] hw/tpm: Add support for VM migration with TPM CRB chunking
Quoted 11 lines · On Thu, Mar 26, 2026 at 03:27:02PM +0400, [email protected] wrote:
On Thu, 19 Mar 2026 19:23:15 +0530, Arun Menon <[email protected]> wrote:
Hi
Quoted 7 lines
hw/core/machine.c +0
-0@@ -40,6 +40,7 @@
Quoted 3 lines
hw/core/machine.c line 41
Quoted 1 line
hw/core/machine.c +0
-0GlobalProperty hw_compat_10_2[] = {Let's not forget to update this to 11.0 for 11.1 :)
Yes I shall base it on top of [email protected]
Quoted 10 lines
Quoted 10 lines
hw/tpm/tpm_crb.c +0
-0@@ -33,6 +33,17 @@#include "trace.h" #include "qom/object.h"
Quoted 7 lines
hw/tpm/tpm_crb.c line 39
Quoted 4 lines
hw/tpm/tpm_crb.c +4
-0+/* command and response buffers; part of VM state when migrating */ +typedef struct TPMCRBMigState { + uint32_t cmd_size; + uint8_t *cmd_tmp;Could we name them after the name of the state fields?
command_buffer_len&&command_buffer_data?
This will be not needed.
Quoted 5 lines
hw/tpm/tpm_crb.c line 42
Quoted 4 lines
hw/tpm/tpm_crb.c +3
-0+ +uint32_t rsp_size; +uint8_t *rsp_tmp;
Quoted 7 lines
(
response_buffer_len..)Actually, you should not need an extra MigState at all. Check how GByteArray are handled in in
ui/vdagent.cfor example. (I have a doubt that it may eventually leak if loading for a running state, where data != NULL though, I would have to check - we may want to use realloc for buffers)
I am thinking of adding a separate VMStateInfo for GByteArray. The .get and .put functions will call actual GLib API to set the size of the buffer or create a new one if the existing data == NULL.
const VMStateInfo vmstate_info_g_byte_array = {
.name = "GByteArray",
.get = get_g_byte_array, // Creates a new GLib object iff data!=NULL .put = put_g_byte_array,
}
We will not require any pre-save and post load hooks in that case. We can also use it in the ui/vdagent.c.
Quoted 60 lines
hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 396
Quoted 9 lines
hw/tpm/tpm_crb.c +0
-0@@ -347,18 +361,118 @@ static int tpm_crb_pre_save(void *opaque)[ ... skip 30 lines ... ]
hw/tpm/tpm_crb.c +6
-0+ }else { + s->mig.rsp_tmp =NULL ; + s->mig.rsp_size =0 ; + } + s->mig.rsp_offset = (uint32_t )s->response_offset; +return 0 ;If you get rid of MigState, the original
response_offsetfield will proably have to beuint32_ttoo.hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 416
Quoted 7 lines
[ ... skip 14 lines ... ]
hw/tpm/tpm_crb.c +6
-0+g_free (s->mig.cmd_tmp); + s->mig.cmd_tmp =NULL ; +g_free (s->mig.rsp_tmp); + s->mig.rsp_tmp =NULL ; +return false; + }Suggesting
g_clear_pointer(&ptr, g_free)hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 421
Quoted 5 lines
hw/tpm/tpm_crb.c +5
-0+ +if (s->mig.cmd_tmp) { +if (s->command_buffer) { +g_byte_array_unref (s->command_buffer); + }Slightly neater:
g_clear_pointer(&s->command_buffer, g_byte_array_unref);hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 433
Quoted 12 lines
hw/tpm/tpm_crb.c +12
-0+ s->command_buffer =g_byte_array_new_take (s->mig.cmd_tmp, + s->mig.cmd_size); + s->mig.cmd_tmp =NULL ; + }else { +if (s->command_buffer) { +g_byte_array_set_size (s->command_buffer,0 ); + } + } +if (s->mig.rsp_tmp) { +if (s->response_buffer) { +g_byte_array_unref (s->response_buffer); + }Or you could simply without condition:
g_clear_pointer(&s->command_buffer, g_byte_array_unref);s->command_buffer=g_byte_array_new_take(s->mig.cmd_tmp, s->mig.cmd_size);This will also reset the size to 0 if
cmd_tmpis NULL andcmd_sizeis 0.hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 437
Quoted 4 lines
hw/tpm/tpm_crb.c +4
-0+ s->response_buffer =g_byte_array_new_take (s->mig.rsp_tmp, + s->mig.rsp_size); + s->mig.rsp_tmp =NULL ; + }Missing
s->response_offset = s->mig.rsp_offset;
Quoted 13 lines
hw/tpm/tpm_crb.c static int tpm_crb_pre_save(void *opaque) · line 448
Quoted 11 lines
hw/tpm/tpm_crb.c +11
-0+return true; +} + +static const VMStateDescription vmstate_tpm_crb_chunk = { + .name ="tpm-crb/chunk" , + .version_id =1 , + .needed = tpm_crb_chunk_needed, + .pre_save = tpm_crb_chunk_pre_save, + .post_load_errp = tpm_crb_chunk_post_load, + .fields = (const VMStateField []) { +VMSTATE_UINT32 (mig.cmd_size, CRBState),Could be version 0, I think.
Yes, will change it
Quoted 4 lines
Signature · 2 lines
-- Marc-André Lureau <[email protected]>
Regards,
Arun Menon
RFC v2 7/7 hw/tpm: Increase TPM TIS max buffer size to 8192
In reply to Arun Menon- Double the size from 4096 to 8192 so that we can have bigger buffer enabling support for PQC algorithms in the TPM TIS interface.
- v185 of TCG TPM rolls out PQC algorithm support. [1]
[1] section 46 https://members.trustedcomputinggroup.org/wg/TCG/document/previewpdf/45151
--- hw/tpm/tpm_tis.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.h +1 -1
@@ -33,7 +33,7 @@#define TPM_TIS_IS_VALID_LOCTY (x) ((x) < TPM_TIS_NUM_LOCALITIES) -#define TPM_TIS_BUFFER_MAX 4096 +#define TPM_TIS_BUFFER_MAX 8192 typedef enum { TPM_TIS_STATE_IDLE = 0,
Signature · 1 line
2.53.0
Re: [RFC v2 7/7] hw/tpm: Increase TPM TIS max buffer size to 8192
Quoted 16 lines · Arun Menon
- Double the size from 4096 to 8192 so that we can have bigger buffer enabling support for PQC algorithms in the TPM TIS interface.
- v185 of TCG TPM rolls out PQC algorithm support. [1][1] section 46 https://members.trustedcomputinggroup.org/wg/TCG/document/previewpdf/45151
Signed-off-by Arun Menon <[email protected]>--- hw/tpm/tpm_tis.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/tpm/tpm_tis.h +0
-0
hw/tpm/tpm_tis.h line 36
Quoted 5 lines
hw/tpm/tpm_tis.h +1
-1@@ -33,7 +33,7 @@#define TPM_TIS_IS_VALID_LOCTY (x) ((x) < TPM_TIS_NUM_LOCALITIES) -#define TPM_TIS_BUFFER_MAX 4096 +#define TPM_TIS_BUFFER_MAX 8192
Unfortunately TIS uses a fixed-size buffer that would now become bigger:
typedef struct TPMState {
MemoryRegion mmio;
unsigned char buffer[TPM_TIS_BUFFER_MAX]; <-- now 8192; before 4096static const VMStateDescription vmstate_tpm_tis_isa = {
.name = "tpm-tis",
.version_id = 0,
.pre_save = tpm_tis_pre_save_isa,
.fields = (const VMStateField[]) {
VMSTATE_BUFFER(state.buffer, TPMStateISA), <-- now 8192; before 4096
VMSTATE_UINT16(state.rw_offset, TPMStateISA),
Problem would be if an older version of the TIS (with size 4096) then receives this 8192 buffer, we would (probably) get a buffer overflow.
Quoted 3 lines
hw/tpm/tpm_tis.h +0
-0typedef enum { TPM_TIS_STATE_IDLE = 0,
Re: [RFC v2 7/7] hw/tpm: Increase TPM TIS max buffer size to 8192
Quoted 31 lines · Stefan Berger
Quoted 16 lines · Arun Menon
- Double the size from 4096 to 8192 so that we can have bigger buffer enabling support for PQC algorithms in the TPM TIS interface.
- v185 of TCG TPM rolls out PQC algorithm support. [1][1] section 46 https://members.trustedcomputinggroup.org/wg/TCG/ document/previewpdf/45151
Signed-off-by Arun Menon <[email protected]>---
hw/tpm/tpm_tis.h| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/tpm/tpm_tis.h +0
-0hw/tpm/tpm_tis.h line 34
Quoted 3 lines
hw/tpm/tpm_tis.h +1
-1@@ -33,7 +33,7 @@ #define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES) -#define TPM_TIS_BUFFER_MAX 4096 +#define TPM_TIS_BUFFER_MAX 8192Unfortunately TIS uses a fixed-size buffer that would now become bigger:
typedef struct TPMState {
MemoryRegion mmio;unsigned char buffer[TPM_TIS_BUFFER_MAX]; <-- now 8192; before 4096
Quoted 7 lines
static const VMStateDescription
vmstate_tpm_tis_isa= {
.name = "tpm-tis",
.version_id = 0,
.pre_save =tpm_tis_pre_save_isa,
.fields = (const VMStateField[]) {VMSTATE_BUFFER(state.buffer, TPMStateISA), <-- now 8192;
before 4096
This will have to become VMSTATE_PARTIAL_BUFFER and the rest is saved with VMSTATE_BUFFER_START_MIDDLE if necessary.
Quoted 3 lines
VMSTATE_UINT16(state.rw_offset, TPMStateISA),Problem would be if an older version of the TIS (with size 4096) then receives this 8192 buffer, we would (probably) get a buffer overflow.
I created 2 more patches for the TIS. It's now also in my branch here:
https://github.com/stefanberger/qemu-tpm/commits/crb-chunking/
Both TIS and CRB can now transfer >4096 bytes packets.
Quoted 7 lines
Quoted 5 lines
hw/tpm/tpm_tis.h +0
-0typedef enum { TPM_TIS_STATE_IDLE = 0,
Re: [RFC v2 7/7] hw/tpm: Increase TPM TIS max buffer size to 8192
Hi Stefan,
Thank you for looking into this and providing with the additional patches to handle TIS interface.
Quoted 46 lines · Stefan Berger
Quoted 32 lines · Stefan Berger
Quoted 17 lines · Arun Menon
- Double the size from 4096 to 8192 so that we can have bigger buffer enabling support for PQC algorithms in the TPM TIS interface.
- v185 of TCG TPM rolls out PQC algorithm support. [1][1] section 46 https://members.trustedcomputinggroup.org/wg/TCG/
document/previewpdf/45151Signed-off-by Arun Menon <[email protected]>---
hw/tpm/tpm_tis.h| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/tpm/tpm_tis.h +0
-0hw/tpm/tpm_tis.h line 34
Quoted 3 lines
hw/tpm/tpm_tis.h +1
-1@@ -33,7 +33,7 @@ #define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES) -#define TPM_TIS_BUFFER_MAX 4096 +#define TPM_TIS_BUFFER_MAX 8192Unfortunately TIS uses a fixed-size buffer that would now become bigger:
typedef struct TPMState {
MemoryRegion mmio;unsigned char buffer[TPM_TIS_BUFFER_MAX]; <-- now 8192; before 4096
Quoted 7 lines
static const VMStateDescription
vmstate_tpm_tis_isa= {
.name = "tpm-tis",
.version_id = 0,
.pre_save =tpm_tis_pre_save_isa,
.fields = (const VMStateField[]) {VMSTATE_BUFFER(state.buffer, TPMStateISA), <-- now 8192;
before 4096This will have to become
VMSTATE_PARTIAL_BUFFERand the rest is saved withVMSTATE_BUFFER_START_MIDDLEif necessary.
Quoted 8 lines
Quoted 4 lines
VMSTATE_UINT16(state.rw_offset, TPMStateISA),Problem would be if an older version of the TIS (with size 4096) then receives this 8192 buffer, we would (probably) get a buffer overflow.
I created 2 more patches for the TIS. It's now also in my branch here:
https://github.com/stefanberger/qemu-tpm/commits/crb-chunking/
Looks good to me.
Quoted 11 lines
Both TIS and CRB can now transfer >4096 bytes packets.
Quoted 7 lines
Quoted 5 lines
hw/tpm/tpm_tis.h +0
-0typedef enum { TPM_TIS_STATE_IDLE = 0,
Is it okay if I incorporate your commits, including the tests and profile enabling support into my next revision of this series?
Regards,
Arun Menon
Re: [RFC v2 7/7] hw/tpm: Increase TPM TIS max buffer size to 8192
Quoted 69 lines · Arun Menon
Hi Stefan,
Thank you for looking into this and providing with the additional patches to handle TIS interface.
Quoted 46 lines · Stefan Berger
Quoted 32 lines · Stefan Berger
Quoted 17 lines · Arun Menon
- Double the size from 4096 to 8192 so that we can have bigger buffer enabling support for PQC algorithms in the TPM TIS interface.
- v185 of TCG TPM rolls out PQC algorithm support. [1][1] section 46 https://members.trustedcomputinggroup.org/wg/TCG/
document/previewpdf/45151Signed-off-by Arun Menon <[email protected]>--- hw/tpm/tpm_tis.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/tpm/tpm_tis.h +0
-0hw/tpm/tpm_tis.h line 34
Quoted 3 lines
hw/tpm/tpm_tis.h +1
-1@@ -33,7 +33,7 @@ #define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES) -#define TPM_TIS_BUFFER_MAX 4096 +#define TPM_TIS_BUFFER_MAX 8192Unfortunately TIS uses a fixed-size buffer that would now become bigger:
typedef struct TPMState {
MemoryRegion mmio;unsigned char buffer[TPM_TIS_BUFFER_MAX]; <-- now 8192; before 4096
Quoted 7 lines
static const VMStateDescription
vmstate_tpm_tis_isa= {
.name = "tpm-tis",
.version_id = 0,
.pre_save =tpm_tis_pre_save_isa,
.fields = (const VMStateField[]) {VMSTATE_BUFFER(state.buffer, TPMStateISA), <-- now 8192;
before 4096This will have to become
VMSTATE_PARTIAL_BUFFERand the rest is saved withVMSTATE_BUFFER_START_MIDDLEif necessary.Quoted 8 lines
Quoted 4 lines
VMSTATE_UINT16(state.rw_offset, TPMStateISA),Problem would be if an older version of the TIS (with size 4096) then receives this 8192 buffer, we would (probably) get a buffer overflow.
I created 2 more patches for the TIS. It's now also in my branch here:
https://github.com/stefanberger/qemu-tpm/commits/crb-chunking/
Looks good to me.
Quoted 6 lines
Both TIS and CRB can now transfer >4096 bytes packets.
Quoted 3 lines
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
hw/tpm/tpm_tis.h line 39
Quoted 2 lines
hw/tpm/tpm_tis.h +0
-0typedef enum { TPM_TIS_STATE_IDLE = 0,Is it okay if I incorporate your commits, including the tests and profile enabling support into my next revision of this series?
Yes, that's okay. Unfortunately the test will not work for most people right now. The default-v2 profile is available via swtpm in git master but it doesn't support ML-DSA since libtpms code with PQC is not public, yet...
Quoted 6 lines
Regards,
Arun Menon
Re: [RFC v2 7/7] hw/tpm: Increase TPM TIS max buffer size to 8192
Quoted 78 lines · Stefan Berger
Quoted 75 lines · Arun Menon
Hi Stefan,
Thank you for looking into this and providing with the additional patches to handle TIS interface.
Quoted 46 lines · Stefan Berger
Quoted 32 lines · Stefan Berger
Quoted 17 lines · Arun Menon
- Double the size from 4096 to 8192 so that we can have bigger buffer enabling support for PQC algorithms in the TPM TIS interface.
- v185 of TCG TPM rolls out PQC algorithm support. [1][1] section 46 https://members.trustedcomputinggroup.org/wg/TCG/
document/previewpdf/45151Signed-off-by Arun Menon <[email protected]>---
hw/tpm/tpm_tis.h| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/tpm/tpm_tis.h +0
-0hw/tpm/tpm_tis.h line 34
Quoted 3 lines
hw/tpm/tpm_tis.h +1
-1@@ -33,7 +33,7 @@ #define TPM_TIS_IS_VALID_LOCTY(x) ((x) < TPM_TIS_NUM_LOCALITIES) -#define TPM_TIS_BUFFER_MAX 4096 +#define TPM_TIS_BUFFER_MAX 8192Unfortunately TIS uses a fixed-size buffer that would now become bigger:
typedef struct TPMState {
MemoryRegion mmio;unsigned char buffer[TPM_TIS_BUFFER_MAX]; <-- now 8192; before 4096
Quoted 7 lines
static const VMStateDescription
vmstate_tpm_tis_isa= { .name = "tpm-tis",
.version_id = 0,
.pre_save =tpm_tis_pre_save_isa,
.fields = (const VMStateField[]) {VMSTATE_BUFFER(state.buffer, TPMStateISA), <-- now 8192; before 4096This will have to become
VMSTATE_PARTIAL_BUFFERand the rest is saved withVMSTATE_BUFFER_START_MIDDLEif necessary.Quoted 8 lines
Quoted 4 lines
VMSTATE_UINT16(state.rw_offset, TPMStateISA),Problem would be if an older version of the TIS (with size 4096) then receives this 8192 buffer, we would (probably) get a buffer overflow.
I created 2 more patches for the TIS. It's now also in my branch here:
https://github.com/stefanberger/qemu-tpm/commits/crb-chunking/
Looks good to me.
Quoted 11 lines
Both TIS and CRB can now transfer >4096 bytes packets.
Quoted 7 lines
Quoted 5 lines
hw/tpm/tpm_tis.h +0
-0typedef enum { TPM_TIS_STATE_IDLE = 0,
Quoted 4 lines
Quoted 2 lines
Is it okay if I incorporate your commits, including the tests and profile enabling support into my next revision of this series?
Yes, that's okay. Unfortunately the test will not work for most people right now. The default-v2 profile is available via swtpm in git master but it doesn't support ML-DSA since libtpms code with PQC is not public, yet...
I added a 2nd search criterion for the profile content to check whether 'ml-dsa' is supported. Fixed the style mistakes. Pushed. You can take them from there now.
Quoted 9 lines
Quoted 7 lines
Regards,
Arun Menon