i386/tdx: refactor TDX firmware memory initialization code into a new function
A new helper function is introduced that refactors all firmware memory initialization code into a separate function. No functional change. Signed-off-by: Ani Sinha <anisinha@redhat.com> Link: https://lore.kernel.org/r/20260225035000.385950-17-anisinha@redhat.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Ani Sinha committed
Feb 25, 2026 at 09:19 UTC
b318bb6969d00439337abd30ac415d931c396494
1 file changed
+40
-33
target/i386/kvm/tdx.c
+40
-33
@@ -295,14 +295,51 @@ static void tdx_post_init_vcpus(void)
295
}
296
}
297
298
-static void tdx_finalize_vm(Notifier *notifier, void *unused)
298
+static void tdx_init_fw_mem_region(void)
299
{
300
TdxFirmware *tdvf = &tdx_guest->tdvf;
301
TdxFirmwareEntry *entry;
302
- RAMBlock *ram_block;
302
Error *local_err = NULL;
303
int r;
304
305
+ for_each_tdx_fw_entry(tdvf, entry) {
306
+ struct kvm_tdx_init_mem_region region;
307
+ uint32_t flags;
308
+
309
+ region = (struct kvm_tdx_init_mem_region) {
310
+ .source_addr = (uintptr_t)entry->mem_ptr,
311
+ .gpa = entry->address,
312
+ .nr_pages = entry->size >> 12,
313
+ };
314
+
315
+ flags = entry->attributes & TDVF_SECTION_ATTRIBUTES_MR_EXTEND ?
316
+ KVM_TDX_MEASURE_MEMORY_REGION : 0;
317
+
318
+ do {
319
+ error_free(local_err);
320
+ local_err = NULL;
321
+ r = tdx_vcpu_ioctl(first_cpu, KVM_TDX_INIT_MEM_REGION, flags,
322
+ ®ion, &local_err);
323
+ } while (r == -EAGAIN || r == -EINTR);
324
+ if (r < 0) {
325
+ error_report_err(local_err);
326
+ exit(1);
327
+ }
328
+
329
+ if (entry->type == TDVF_SECTION_TYPE_TD_HOB ||
330
+ entry->type == TDVF_SECTION_TYPE_TEMP_MEM) {
331
+ qemu_ram_munmap(-1, entry->mem_ptr, entry->size);
332
+ entry->mem_ptr = NULL;
333
+ }
334
+ }
335
+}
336
+
337
+static void tdx_finalize_vm(Notifier *notifier, void *unused)
338
+{
339
+ TdxFirmware *tdvf = &tdx_guest->tdvf;
340
+ TdxFirmwareEntry *entry;
341
+ RAMBlock *ram_block;
342
+
343
tdx_init_ram_entries();
344
345
for_each_tdx_fw_entry(tdvf, entry) {
@@ -339,37 +376,7 @@ static void tdx_finalize_vm(Notifier *notifier, void *unused)
376
tdvf_hob_create(tdx_guest, tdx_get_hob_entry(tdx_guest));
377
378
tdx_post_init_vcpus();
342
-
343
- for_each_tdx_fw_entry(tdvf, entry) {
344
- struct kvm_tdx_init_mem_region region;
345
- uint32_t flags;
346
-
347
- region = (struct kvm_tdx_init_mem_region) {
348
- .source_addr = (uintptr_t)entry->mem_ptr,
349
- .gpa = entry->address,
350
- .nr_pages = entry->size >> 12,
351
- };
352
-
353
- flags = entry->attributes & TDVF_SECTION_ATTRIBUTES_MR_EXTEND ?
354
- KVM_TDX_MEASURE_MEMORY_REGION : 0;
355
-
356
- do {
357
- error_free(local_err);
358
- local_err = NULL;
359
- r = tdx_vcpu_ioctl(first_cpu, KVM_TDX_INIT_MEM_REGION, flags,
360
- ®ion, &local_err);
361
- } while (r == -EAGAIN || r == -EINTR);
362
- if (r < 0) {
363
- error_report_err(local_err);
364
- exit(1);
365
- }
366
-
367
- if (entry->type == TDVF_SECTION_TYPE_TD_HOB ||
368
- entry->type == TDVF_SECTION_TYPE_TEMP_MEM) {
369
- qemu_ram_munmap(-1, entry->mem_ptr, entry->size);
370
- entry->mem_ptr = NULL;
371
- }
372
- }
379
+ tdx_init_fw_mem_region();
380
381
/*
382
* TDVF image has been copied into private region above via