@samitouri / QOSamiQemu / commits / 473932c2eb

hw/i386: refactor x86_bios_rom_init for reuse in confidential guest reset

For confidential guests, bios image must be reinitialized upon reset. This is because bios memory is encrypted and hence once the old confidential kvm context is destroyed, it cannot be decrypted. It needs to be reinitilized. Towards that, this change refactors x86_bios_rom_init() code so that parts of it can be called during confidential guest reset. No functional chnage. Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Ani Sinha <anisinha@redhat.com> Link: https://lore.kernel.org/r/20260225035000.385950-13-anisinha@redhat.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Ani Sinha committed Feb 25, 2026 at 09:19 UTC 473932c2eb694c59044a7d68c553701602e05381
1 file changed +37 -13
hw/i386/x86-common.c
+37 -13
@@ -1020,17 +1020,11 @@ void x86_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *isa_memory,
1020 memory_region_set_readonly(isa_bios, read_only);
1021 }
1022
1023 -void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
1024 - MemoryRegion *rom_memory, bool isapc_ram_fw)
1023 +static int get_bios_size(X86MachineState *x86ms,
1024 + const char *bios_name, char *filename)
1025 {
1026 - const char *bios_name;
1027 - char *filename;
1026 int bios_size;
1029 - ssize_t ret;
1027
1031 - /* BIOS load */
1032 - bios_name = MACHINE(x86ms)->firmware ?: default_firmware;
1033 - filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1028 if (filename) {
1029 bios_size = get_image_size(filename, NULL);
1030 } else {
@@ -1040,6 +1034,21 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
1034 (bios_size % 65536) != 0) {
1035 goto bios_error;
1036 }
1037 +
1038 + return bios_size;
1039 +
1040 + bios_error:
1041 + fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
1042 + exit(1);
1043 +}
1044 +
1045 +static void load_bios_from_file(X86MachineState *x86ms, const char *bios_name,
1046 + char *filename, int bios_size,
1047 + bool isapc_ram_fw)
1048 +{
1049 + ssize_t ret;
1050 +
1051 + /* BIOS load */
1052 if (machine_require_guest_memfd(MACHINE(x86ms))) {
1053 memory_region_init_ram_guest_memfd(&x86ms->bios, NULL, "pc.bios",
1054 bios_size, &error_fatal);
@@ -1068,7 +1077,26 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
1077 goto bios_error;
1078 }
1079 }
1071 - g_free(filename);
1080 +
1081 + return;
1082 +
1083 + bios_error:
1084 + fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
1085 + exit(1);
1086 +}
1087 +
1088 +void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
1089 + MemoryRegion *rom_memory, bool isapc_ram_fw)
1090 +{
1091 + int bios_size;
1092 + const char *bios_name;
1093 + g_autofree char *filename;
1094 +
1095 + bios_name = MACHINE(x86ms)->firmware ?: default_firmware;
1096 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1097 +
1098 + bios_size = get_bios_size(x86ms, bios_name, filename);
1099 + load_bios_from_file(x86ms, bios_name, filename, bios_size, isapc_ram_fw);
1100
1101 if (!machine_require_guest_memfd(MACHINE(x86ms))) {
1102 /* map the last 128KB of the BIOS in ISA space */
@@ -1081,8 +1109,4 @@ void x86_bios_rom_init(X86MachineState *x86ms, const char *default_firmware,
1109 (uint32_t)(-bios_size),
1110 &x86ms->bios);
1111 return;
1084 -
1085 -bios_error:
1086 - fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
1087 - exit(1);
1112 }