X86: Alias isa-bios area and clean up
cover X86: Alias isa-bios area and clean up
This series changes the "isa-bios" MemoryRegion to be an alias rather than a copy in the pflash case. This fixes issuing pflash commands in the isa-bios region which matches real hardware and which some real-world legacy bioses I'm running rely on. Furthermore, aliasing in the isa-bios area is already the current behavior in the bios (a.k.a. ROM) case, so this series consolidates behavior.
The consolidateion results in duplicate code which is resolved in the second half (patches 3 and 4) in this series.
Question: AFAIU, patch 2 changes the behavior for SEV-enabled guests since the isa-bios area is now encrypted. Does this need compat machinery or is it a bugfix?
Testing done:
* make check with qemu-system-x86_64 (QEMU 8.2.2) installed. All tests including migration tests pass.
* make check-avocado
Best regards,
Bernhard
Bernhard Beschow (4):
hw/i386/pc_sysfw: Remove unused parameter from pc_isa_bios_init() hw/i386/pc_sysfw: Alias rather than copy isa-bios region hw/i386/x86: Eliminate two if statements in x86_bios_rom_init() hw/i386: Consolidate isa-bios creation
include/hw/i386/x86.h | 2 ++ hw/i386/pc_sysfw.c | 38 ++++---------------------------------- hw/i386/x86.c | 35 +++++++++++++++++++---------------- 3 files changed, 25 insertions(+), 50 deletions(-)
Signature · 1 line
2.44.0
PATCH 1/4 hw/i386/pc_sysfw: Remove unused parameter from pc_isa_bios_init()
--- hw/i386/pc_sysfw.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
hw/i386/pc_sysfw.c +2 -3
@@ -41,8 +41,7 @@#define FLASH_SECTOR_SIZE 4096static void pc_isa_bios_init (MemoryRegion *rom_memory, -MemoryRegion *flash_mem, -int ram_size) +MemoryRegion *flash_mem) {int isa_bios_size;MemoryRegion *isa_bios;
@@ -186,7 +185,7 @@ static void pc_system_flash_map(PCMachineState *pcms,if (i ==0 ) { flash_mem =pflash_cfi01_get_memory (system_flash); -pc_isa_bios_init (rom_memory, flash_mem, size); +pc_isa_bios_init (rom_memory, flash_mem);/* Encrypt the pflash boot ROM */ if (sev_enabled ()) {
Signature · 1 line
2.44.0
Re: [PATCH 1/4] hw/i386/pc_sysfw: Remove unused parameter from pc_isa_bios_init()
Quoted 4 lines · Bernhard Beschow
Signed-off-by Bernhard Beschow <[email protected]>--- hw/i386/pc_sysfw.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
PATCH 2/4 hw/i386/pc_sysfw: Alias rather than copy isa-bios region
In reply to Bernhard BeschowIn the -bios case the "isa-bios" memory region is an alias to the BIOS mapped to the top of the 4G memory boundary. Do the same in the -pflash case to have common behavior. This also makes pflash commands work in the "isa-bios" region which some real-world legacy bioses rely on.
Note that in the sev_enabled() case, the "isa-bios" memory region in the -pflash case will now also point to encrypted memory, just like it already does in the -bios case.
--- hw/i386/pc_sysfw.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)
hw/i386/pc_sysfw.c +2 -11
@@ -46,27 +46,18 @@ static void pc_isa_bios_init(MemoryRegion *rom_memory,int isa_bios_size;MemoryRegion *isa_bios;uint64_t flash_size; -void *flash_ptr, *isa_bios_ptr; flash_size =memory_region_size (flash_mem);/* map the last 128KB of the BIOS in ISA space */ isa_bios_size =MIN (flash_size,128 * KiB); isa_bios =g_malloc (sizeof (*isa_bios)); -memory_region_init_ram (isa_bios,NULL ,"isa-bios" , isa_bios_size, - &error_fatal); +memory_region_init_alias (isa_bios,NULL ,"isa-bios" , flash_mem, + flash_size - isa_bios_size, isa_bios_size);memory_region_add_subregion_overlap (rom_memory,0x100000 - isa_bios_size, isa_bios,1 ); - -/* copy ISA rom image from top of flash memory */ - flash_ptr =memory_region_get_ram_ptr (flash_mem); - isa_bios_ptr =memory_region_get_ram_ptr (isa_bios); -memcpy (isa_bios_ptr, - ((uint8_t *)flash_ptr) + (flash_size - isa_bios_size), - isa_bios_size); -memory_region_set_readonly (isa_bios, true); }
Signature · 1 line
2.44.0
PATCH 3/4 hw/i386/x86: Eliminate two if statements in x86_bios_rom_init()
In reply to Bernhard BeschowGiven that memory_region_set_readonly() is a no-op when the readonlyness is already as requested it is possible to simplify the pattern
if (condition) {
foo(true);
}to
foo(condition);
which is shorter and allows to see the invariant of the code more easily.
--- hw/i386/x86.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
hw/i386/x86.c +2 -6
@@ -1171,9 +1171,7 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware,load_image_size (filename, ptr, bios_size);x86_firmware_configure (ptr, bios_size); }else { -if (!isapc_ram_fw) { -memory_region_set_readonly (bios, true); - } +memory_region_set_readonly (bios, !isapc_ram_fw); ret =rom_add_file_fixed (bios_name, (uint32_t )(-bios_size),-1 );if (ret !=0 ) { goto bios_error;
@@ -1190,9 +1188,7 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware,0x100000 - isa_bios_size, isa_bios, 1); -if (!isapc_ram_fw) { -memory_region_set_readonly (isa_bios, true); - } +memory_region_set_readonly (isa_bios, !isapc_ram_fw);/* map all the bios at the top of memory */ memory_region_add_subregion(rom_memory,
Signature · 1 line
2.44.0
Re: [PATCH 3/4] hw/i386/x86: Eliminate two if statements in x86_bios_rom_init()
Quoted 9 lines · Bernhard Beschow
Given that
memory_region_set_readonly()is a no-op when the readonlyness is already as requested it is possible to simplify the patternif (condition) { foo(true); }to
Quoted 8 lines
foo(condition);which is shorter and allows to see the invariant of the code more easily.
Signed-off-by Bernhard Beschow <[email protected]>--- hw/i386/x86.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
PATCH 4/4 hw/i386: Consolidate isa-bios creation
In reply to Bernhard BeschowNow that the -bios and -pflash code paths work the same it is possible to have a common implementation.
While at it convert the magic number 0x100000 (== 1MiB) to increase readability.
--- include/hw/i386/x86.h | 2 ++ hw/i386/pc_sysfw.c | 28 ++++------------------------ hw/i386/x86.c | 29 ++++++++++++++++++----------- 3 files changed, 24 insertions(+), 35 deletions(-)
include/hw/i386/x86.h +2 -0
@@ -116,6 +116,8 @@ void x86_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,void x86_cpu_unplug_cb (HotplugHandler *hotplug_dev,DeviceState *dev,Error **errp); +void x86_isa_bios_init (MemoryRegion *rom_memory,MemoryRegion *bios, +bool isapc_ram_fw);void x86_bios_rom_init (MachineState *ms,const char *default_firmware,MemoryRegion *rom_memory,bool isapc_ram_fw);
hw/i386/pc_sysfw.c +4 -24
@@ -28,7 +28,6 @@#include "sysemu/block-backend.h" #include "qemu/error-report.h" #include "qemu/option.h" -#include "qemu/units.h" #include "hw/sysbus.h" #include "hw/i386/x86.h" #include "hw/i386/pc.h"
@@ -40,27 +39,6 @@#define FLASH_SECTOR_SIZE 4096 -static void pc_isa_bios_init (MemoryRegion *rom_memory, -MemoryRegion *flash_mem) -{ -int isa_bios_size; -MemoryRegion *isa_bios; -uint64_t flash_size; - - flash_size =memory_region_size (flash_mem); - -/* map the last 128KB of the BIOS in ISA space */ - isa_bios_size =MIN (flash_size,128 * KiB); - isa_bios =g_malloc (sizeof (*isa_bios)); -memory_region_init_alias (isa_bios,NULL ,"isa-bios" , flash_mem, - flash_size - isa_bios_size, isa_bios_size); -memory_region_add_subregion_overlap (rom_memory, -0x100000 - isa_bios_size, - isa_bios, -1 ); -memory_region_set_readonly (isa_bios, true); -} -static PFlashCFI01 *pc_pflash_create (PCMachineState *pcms,const char *name,const char *alias_prop_name)
@@ -121,7 +99,7 @@ void pc_system_flash_cleanup_unused(PCMachineState *pcms)* pcms->max_fw_size. * * If pcms->flash[0] has a block backend, its memory is passed to -* pc_isa_bios_init(). Merging several flash devices for isa-bios is +* x86_isa_bios_init(). Merging several flash devices for isa-bios is * not supported. */ static void pc_system_flash_map(PCMachineState *pcms,
@@ -176,7 +154,9 @@ static void pc_system_flash_map(PCMachineState *pcms,if (i ==0 ) { flash_mem =pflash_cfi01_get_memory (system_flash); -pc_isa_bios_init (rom_memory, flash_mem); + +/* Map the last 128KB of the BIOS in ISA space */ +x86_isa_bios_init (rom_memory, flash_mem, false);/* Encrypt the pflash boot ROM */ if (sev_enabled ()) {
hw/i386/x86.c +18 -11
@@ -1136,13 +1136,28 @@ void x86_load_linux(X86MachineState *x86ms, nb_option_roms++; } +void x86_isa_bios_init (MemoryRegion *rom_memory,MemoryRegion *bios, +bool isapc_ram_fw) +{ +int bios_size =memory_region_size (bios); +int isa_bios_size =MIN (bios_size,128 * KiB); +MemoryRegion *isa_bios; + + isa_bios =g_malloc (sizeof (*isa_bios)); +memory_region_init_alias (isa_bios,NULL ,"isa-bios" , bios, + bios_size - isa_bios_size, isa_bios_size); +memory_region_add_subregion_overlap (rom_memory,1 * MiB - isa_bios_size, + isa_bios,1 ); +memory_region_set_readonly (isa_bios, !isapc_ram_fw); +} +void x86_bios_rom_init (MachineState *ms,const char *default_firmware,MemoryRegion *rom_memory,bool isapc_ram_fw) {const char *bios_name;char *filename; -MemoryRegion *bios, *isa_bios; -int bios_size, isa_bios_size; +MemoryRegion *bios; +int bios_size;ssize_t ret;/* BIOS load */
@@ -1180,15 +1195,7 @@ void x86_bios_rom_init(MachineState *ms, const char *default_firmware,g_free (filename);/* map the last 128KB of the BIOS in ISA space */ - isa_bios_size =MIN (bios_size,128 * KiB); - isa_bios =g_malloc (sizeof (*isa_bios)); -memory_region_init_alias (isa_bios,NULL ,"isa-bios" , bios, - bios_size - isa_bios_size, isa_bios_size); -memory_region_add_subregion_overlap (rom_memory, -0x100000 - isa_bios_size, - isa_bios, -1 ); -memory_region_set_readonly (isa_bios, !isapc_ram_fw); +x86_isa_bios_init (rom_memory, bios, isapc_ram_fw);/* map all the bios at the top of memory */ memory_region_add_subregion(rom_memory,
Signature · 1 line
2.44.0
Re: [PATCH 4/4] hw/i386: Consolidate isa-bios creation
Hi Bernhard,
Quoted 57 lines · Bernhard Beschow
Now that the -bios and -pflash code paths work the same it is possible to have a common implementation.
While at it convert the magic number
0x100000(== 1MiB) to increase readability.Signed-off-by Bernhard Beschow <[email protected]>--- include/hw/i386/x86.h | 2 ++ hw/i386/pc_sysfw.c | 28 ++++------------------------ hw/i386/x86.c | 29 ++++++++++++++++++----------- 3 files changed, 24 insertions(+), 35 deletions(-)hw/i386/pc_sysfw.c +0
-21@@ -28,7 +28,6 @@#include "sysemu/block-backend.h" #include "qemu/error-report.h" #include "qemu/option.h" -#include "qemu/units.h" #include "hw/sysbus.h" #include "hw/i386/x86.h" #include "hw/i386/pc.h" @@ -40,27 +39,6 @@#define FLASH_SECTOR_SIZE 4096 -static void pc_isa_bios_init (MemoryRegion *rom_memory, -MemoryRegion *flash_mem) -{ -int isa_bios_size; -MemoryRegion *isa_bios; -uint64_t flash_size; - - flash_size =memory_region_size (flash_mem); - -/* map the last 128KB of the BIOS in ISA space */ - isa_bios_size =MIN (flash_size,128 * KiB); - isa_bios =g_malloc (sizeof (*isa_bios)); -memory_region_init_alias (isa_bios,NULL ,"isa-bios" , flash_mem, - flash_size - isa_bios_size, isa_bios_size); -memory_region_add_subregion_overlap (rom_memory, -0x100000 - isa_bios_size, - isa_bios, -1 ); -memory_region_set_readonly (isa_bios, true); -}hw/i386/x86.c +0
-0@@ -1136,13 +1136,28 @@ void x86_load_linux(X86MachineState *x86ms, nb_option_roms++;
hw/i386/x86.c void x86_load_linux(X86MachineState *x86ms, · line 1146
Quoted 10 lines
hw/i386/x86.c +8
-0} +void x86_isa_bios_init (MemoryRegion *rom_memory,MemoryRegion *bios, +bool isapc_ram_fw) +{ +int bios_size =memory_region_size (bios); +int isa_bios_size =MIN (bios_size,128 * KiB); +MemoryRegion *isa_bios; + + isa_bios =g_malloc (sizeof (*isa_bios));
Pre-existing, but we shouldn't leak MR like that.
Probably better to pass pre-allocated as argument, smth like:
/**
* x86_isa_bios_init: ... at fixed addr ...
* @isa_bios: MR to initialize
* @isa_mr: ISA bus
* @bios: BIOS MR to map on ISA bus
* @read_only: Map the BIOS as read-only */
void x86_isa_bios_init(MemoryRegion *isa_bios,
const MemoryRegion *isa_mr, const MemoryRegion *bios, bool read_only);
Quoted 6 lines
hw/i386/x86.c +6
-0+memory_region_init_alias (isa_bios,NULL ,"isa-bios" , bios, + bios_size - isa_bios_size, isa_bios_size); +memory_region_add_subregion_overlap (rom_memory,1 * MiB - isa_bios_size, + isa_bios,1 ); +memory_region_set_readonly (isa_bios, !isapc_ram_fw); +}
Re: [PATCH 4/4] hw/i386: Consolidate isa-bios creation
Am 25. April 2024 07:19:27 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:
Quoted 73 lines
Hi Bernhard,
Quoted 54 lines · Bernhard Beschow
Now that the -bios and -pflash code paths work the same it is possible to have a common implementation.
While at it convert the magic number
0x100000(== 1MiB) to increase readability.Signed-off-by Bernhard Beschow <[email protected]>--- include/hw/i386/x86.h | 2 ++ hw/i386/pc_sysfw.c | 28 ++++------------------------ hw/i386/x86.c | 29 ++++++++++++++++++----------- 3 files changed, 24 insertions(+), 35 deletions(-)hw/i386/pc_sysfw.c +0
-20@@ -28,7 +28,6 @@#include "sysemu/block-backend.h" #include "qemu/error-report.h" #include "qemu/option.h" -#include "qemu/units.h" #include "hw/sysbus.h" #include "hw/i386/x86.h" #include "hw/i386/pc.h" @@ -40,27 +39,6 @@#define FLASH_SECTOR_SIZE 4096 -static void pc_isa_bios_init(MemoryRegion *rom_memory, -MemoryRegion *flash_mem) -{ -int isa_bios_size; -MemoryRegion *isa_bios; -uint64_t flash_size; - - flash_size =memory_region_size (flash_mem); - -/* map the last 128KB of the BIOS in ISA space */ - isa_bios_size =MIN (flash_size,128 * KiB); - isa_bios =g_malloc (sizeof (*isa_bios)); -memory_region_init_alias (isa_bios,NULL ,"isa-bios" , flash_mem, - flash_size - isa_bios_size, isa_bios_size); -memory_region_add_subregion_overlap (rom_memory, -0x100000 - isa_bios_size, - isa_bios, -1 ); -memory_region_set_readonly (isa_bios, true); -}hw/i386/x86.c +0
-0hw/i386/x86.c void x86_load_linux(X86MachineState *x86ms, · line 1145
Quoted 10 lines
hw/i386/x86.c +7
-0@@ -1136,13 +1136,28 @@ void x86_load_linux(X86MachineState *x86ms, nb_option_roms++; } +void x86_isa_bios_init(MemoryRegion *rom_memory, MemoryRegion *bios, + bool isapc_ram_fw) +{ +int bios_size =memory_region_size (bios); +int isa_bios_size =MIN (bios_size,128 * KiB); +MemoryRegion *isa_bios; + + isa_bios =g_malloc (sizeof (*isa_bios));Pre-existing, but we shouldn't leak MR like that.
Probably better to pass pre-allocated as argument, smth like:
Quoted 12 lines
/**
*x86_isa_bios_init: ... at fixed addr ...
* @isa_bios: MR to initialize
* @isa_mr: ISA bus
* @bios: BIOS MR to map on ISA bus
* @read_only: Map the BIOS as read-only */
void x86_isa_bios_init(MemoryRegion*isa_bios,
const MemoryRegion*isa_mr, const MemoryRegion *bios, boolread_only);
Same would apply for the "pc.bios" region. I'll fix both then.
Best regards,
Bernhard
Quoted 7 lines
Quoted 6 lines
hw/i386/x86.c +6
-0+memory_region_init_alias (isa_bios,NULL ,"isa-bios" , bios, + bios_size - isa_bios_size, isa_bios_size); +memory_region_add_subregion_overlap (rom_memory,1 * MiB - isa_bios_size, + isa_bios,1 ); +memory_region_set_readonly (isa_bios, !isapc_ram_fw); +}
+Michael
Am 22. April 2024 20:06:21 UTC schrieb Bernhard Beschow <[email protected]>:
Quoted 71 lines
This series changes the "isa-bios" MemoryRegion to be an alias rather than a
copy in the pflash case. This fixes issuing pflash commands in the isa-bios
region which matches real hardware and which some real-world legacy bioses I'm
running rely on. Furthermore, aliasing in the isa-bios area is already the
current behavior in the bios (a.k.a. ROM) case, so this series consolidates
behavior.
The consolidateion results in duplicate code which is resolved in the second
half (patches 3 and 4) in this series.
Question: AFAIU, patch 2 changes the behavior for SEV-enabled guests since the
isa-bios area is now encrypted. Does this need compat machinery or is it a
bugfix?
Testing done:
*
make checkwith qemu-system-x86_64 (QEMU 8.2.2) installed. All testsincluding migration tests pass.
*
make check-avocadoBest regards,
Bernhard
Bernhard Beschow (4):
hw/i386/pc_sysfw: Remove unused parameter from
pc_isa_bios_init()hw/i386/pc_sysfw: Alias rather than copy isa-bios region
hw/i386/x86: Eliminate two if statements in
x86_bios_rom_init()hw/i386: Consolidate isa-bios creation
include/hw/i386/x86.h| 2 ++
hw/i386/pc_sysfw.c| 38 ++++----------------------------------
hw/i386/x86.c| 35 +++++++++++++++++++----------------3 files changed, 25 insertions(+), 50 deletions(-)-- >
2.44.0
Quoted 10 lines · Bernhard Beschow
This series changes the "isa-bios" MemoryRegion to be an alias rather than a copy in the pflash case. This fixes issuing pflash commands in the isa-bios region which matches real hardware and which some real-world legacy bioses I'm running rely on. Furthermore, aliasing in the isa-bios area is already the current behavior in the bios (a.k.a. ROM) case, so this series consolidates behavior.
Bernhard Beschow (4):
hw/i386/pc_sysfw: Remove unused parameter frompc_isa_bios_init()
To reduce respin churn, I'm queuing the first patch via hw-misc.
Am 25. April 2024 08:07:43 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:
Quoted 9 lines · Bernhard Beschow
Quoted 6 lines
This series changes the "isa-bios" MemoryRegion to be an alias rather than a copy in the pflash case. This fixes issuing pflash commands in the isa-bios region which matches real hardware and which some real-world legacy bioses I'm running rely on. Furthermore, aliasing in the isa-bios area is already the current behavior in the bios (a.k.a. ROM) case, so this series consolidates behavior.
Quoted 4 lines
Quoted 2 lines
Bernhard Beschow (4):
hw/i386/pc_sysfw: Remove unused parameter frompc_isa_bios_init()To reduce respin churn, I'm queuing the first patch via hw-misc.
Nice!
Quoted 7 lines · Bernhard Beschow
This series changes the "isa-bios" MemoryRegion to be an alias rather than a copy in the pflash case. This fixes issuing pflash commands in the isa-bios region which matches real hardware and which some real-world legacy bioses I'm running rely on. Furthermore, aliasing in the isa-bios area is already the current behavior in the bios (a.k.a. ROM) case, so this series consolidates behavior.
Quoted 6 lines
The consolidateion results in duplicate code which is resolved in the second half (patches 3 and 4) in this series.
Question: AFAIU, patch 2 changes the behavior for SEV-enabled guests since the isa-bios area is now encrypted. Does this need compat machinery or is it a bugfix?
When in doubt, do a compat thing.
Quoted 22 lines
Testing done:
*make checkwith qemu-system-x86_64 (QEMU 8.2.2) installed. All tests including migration tests pass.
*make check-avocadoBest regards,
BernhardBernhard Beschow (4):
hw/i386/pc_sysfw: Remove unused parameter frompc_isa_bios_init()hw/i386/pc_sysfw: Alias rather than copy isa-bios region hw/i386/x86: Eliminate two if statements inx86_bios_rom_init()hw/i386: Consolidate isa-bios creationinclude/hw/i386/x86.h | 2 ++ hw/i386/pc_sysfw.c | 38 ++++---------------------------------- hw/i386/x86.c | 35 +++++++++++++++++++---------------- 3 files changed, 25 insertions(+), 50 deletions(-)Signature · 1 line
2.44.0
Am 25. April 2024 10:16:10 UTC schrieb "Michael S. Tsirkin" <[email protected]>:
Quoted 8 lines · Bernhard Beschow
Quoted 7 lines
This series changes the "isa-bios" MemoryRegion to be an alias rather than a copy in the pflash case. This fixes issuing pflash commands in the isa-bios region which matches real hardware and which some real-world legacy bioses I'm running rely on. Furthermore, aliasing in the isa-bios area is already the current behavior in the bios (a.k.a. ROM) case, so this series consolidates behavior.
Quoted 8 lines
Quoted 6 lines
The consolidateion results in duplicate code which is resolved in the second half (patches 3 and 4) in this series.
Question: AFAIU, patch 2 changes the behavior for SEV-enabled guests since the isa-bios area is now encrypted. Does this need compat machinery or is it a bugfix?
When in doubt, do a compat thing.
Will do.
Thanks,
Bernhard
Quoted 24 lines
Quoted 23 lines
Testing done:
*make checkwith qemu-system-x86_64 (QEMU 8.2.2) installed. All tests including migration tests pass.
*make check-avocadoBest regards,
BernhardBernhard Beschow (4):
hw/i386/pc_sysfw: Remove unused parameter frompc_isa_bios_init()hw/i386/pc_sysfw: Alias rather than copy isa-bios region hw/i386/x86: Eliminate two if statements inx86_bios_rom_init()hw/i386: Consolidate isa-bios creationinclude/hw/i386/x86.h | 2 ++ hw/i386/pc_sysfw.c | 38 ++++---------------------------------- hw/i386/x86.c | 35 +++++++++++++++++++---------------- 3 files changed, 25 insertions(+), 50 deletions(-)Signature · 1 line
2.44.0