Skip to contentSkip to conversation

i386 emulate fixes

PATCH In review Bernhard Beschow 4 participants 8 messages Feb 14 – Feb 18

lore
Share & tools
lore
#0 lore raw

cover i386 emulate fixes

When testing Mohamed's recent x86_64 WHPX updates [1] I encountered two compilation issues under MSYS2/GCC which are fixed in this series. The last commit fixes a missed optimization opportunity in MSHV code.

The MSHV code will be reused in WHPX [1] which allowed me to test it. I didn't notice any performance benefits, though in theory there should be some due to less guest memory accesses.

Testing done:
* Use a Linux VM for a couple of hours

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Bernhard Beschow (3):
target/i386/emulate/x86_decode: Fix compiler warning target/i386/hvf/x86_mmu: Fix compiler warning target/i386/emulate/x86_decode: Actually use stream in
decode_instruction_stream()

 target/i386/emulate/x86_decode.c | 6 +++---
 target/i386/hvf/x86_mmu.c        | 6 ++++--
 2 files changed, 7 insertions(+), 5 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 target/i386/emulate/x86_decode: Fix compiler warning

When compiling for i386-softmmu under MSYS2, GCC emits the following warning:

In function 'get_reg_val',

      inlined from 'calc_modrm_operand64' at ../src/target/i386/emulate/x86_decode.c:1796:15:
  ../src/target/i386/emulate/x86_decode.c:1703:5: error: 'memcpy' forming offset [4, 7] is out of the bounds [0, 4] of object 'val' with type 'target_ulong' {aka 'unsigned int'} [-Werror=array-bounds=]
   1703 |     memcpy(&val,
        |     ^~~~~~~~~~~~
   1704 |            get_reg_ref(env, reg, rex_present, is_extended, size),
        |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   1705 |            size);
        |            ~~~~~

../src/target/i386/emulate/x86_decode.c: In function 'calc_modrm_operand64':
../src/target/i386/emulate/x86_decode.c:1702:18: note: 'val' declared here
1702 | target_ulong val = 0;
| ^~~

In the calc_modrm_operand64() case the compiler sees size == 8 to be mem-copied to a target_ulong variable which is only 4 bytes wide in case of i386-softmmu. Note that when size != 1, get_reg_ref() always returns a pointer to an 8 byte register, regardless of the target_ulong size. Fix the compiler warning by always providing 8 bytes of storage by means of uint64_t.

Fixes 77a2dba45cc9 ("target/i386/emulate: stop overloading decode->op[N].ptr")
cc qemu-stable
---
 target/i386/emulate/x86_decode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/emulate/x86_decode.c +1 -1
index d037ed1142..6ad03b71b0 100644
@@ -1699,7 +1699,7 @@ void *get_reg_ref(CPUX86State *env, int reg, int rex_present,
 target_ulong get_reg_val(CPUX86State *env, int reg, int rex_present,
                          int is_extended, int size)
 {
-    target_ulong val = 0;
+    uint64_t val = 0;
     memcpy(&val,
            get_reg_ref(env, reg, rex_present, is_extended, size),
            size);
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/3 target/i386/hvf/x86_mmu: Fix compiler warning

In reply to Bernhard Beschow

When reusing the code in WHPX, GCC emits the following warning when compiling for i386-softmmu under MSYS2:

In file included from ../src/target/i386/emulate/x86_mmu.c:20:
../src/target/i386/emulate/x86_mmu.c: In function 'vmx_write_mem':
../src/target/i386/emulate/x86_mmu.c:251:25: error: format '%llx' expects argument of type 'long long unsigned int', but argument 3 has type 'target_ulong' {aka 'unsigned int'} [-Werror=format=]

    251 |             VM_PANIC_EX("%s: mmu_gva_to_gpa %llx failed\n", __func__, gva);
        |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~            ~~~
        |                                                                       |
        |                                                                       target_ulong {aka unsigned int}
  ../src/target/i386/emulate/panic.h:34:12: note: in definition of macro 'VM_PANIC_EX'
     34 |     printf(__VA_ARGS__); \
        |            ^~~~~~~~~~~
  ../src/target/i386/emulate/x86_mmu.c:251:48: note: format string is defined here
    251 |             VM_PANIC_EX("%s: mmu_gva_to_gpa %llx failed\n", __func__, gva);
        |                                             ~~~^
        |                                                |
        |                                                long long unsigned int
        |                                             %x

Fix the warning by reusing the target-specific macro TARGET_FMT_lx which exists for this exact purpose.

Fixes c97d6d2cdf97 ("i386: hvf: add code base from Google's QEMU repository")
cc qemu-stable
---
 target/i386/hvf/x86_mmu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/i386/hvf/x86_mmu.c +4 -2
index afc5c17d5d..fe44d2edf4 100644
@@ -244,7 +244,8 @@ void vmx_write_mem(CPUState *cpu, target_ulong gva, void *data, int bytes)
         int copy = MIN(bytes, 0x1000 - (gva & 0xfff));
 
         if (!mmu_gva_to_gpa(cpu, gva, &gpa)) {
-            VM_PANIC_EX("%s: mmu_gva_to_gpa %llx failed\n", __func__, gva);
+            VM_PANIC_EX("%s: mmu_gva_to_gpa " TARGET_FMT_lx " failed\n",
+                        __func__, gva);
         } else {
             address_space_write(&address_space_memory, gpa,
                                 MEMTXATTRS_UNSPECIFIED, data, copy);
@@ -265,7 +266,8 @@ void vmx_read_mem(CPUState *cpu, void *data, target_ulong gva, int bytes)
         int copy = MIN(bytes, 0x1000 - (gva & 0xfff));
 
         if (!mmu_gva_to_gpa(cpu, gva, &gpa)) {
-            VM_PANIC_EX("%s: mmu_gva_to_gpa %llx failed\n", __func__, gva);
+            VM_PANIC_EX("%s: mmu_gva_to_gpa " TARGET_FMT_lx " failed\n",
+                        __func__, gva);
         }
         address_space_read(&address_space_memory, gpa, MEMTXATTRS_UNSPECIFIED,
                            data, copy);
Signature · 1 line
2.53.0
Reviewed-by on v1 2/3
#3 lore raw

Re: [PATCH 2/3] target/i386/hvf/x86_mmu: Fix compiler warning

Quoted 23 lines · Bernhard Beschow

When reusing the code in WHPX, GCC emits the following warning when compiling for i386-softmmu under MSYS2:

   In file included from ../src/target/i386/emulate/x86_mmu.c:20:
   ../src/target/i386/emulate/x86_mmu.c: In function 'vmx_write_mem':
   ../src/target/i386/emulate/x86_mmu.c:251:25: error: format '%llx' expects argument of type 'long long unsigned int', but argument 3 has type 'target_ulong' {aka 'unsigned int'} [-Werror=format=]
     251 |             VM_PANIC_EX("%s: mmu_gva_to_gpa %llx failed\n", __func__, gva);
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~            ~~~
         |                                                                       |
         |                                                                       target_ulong {aka unsigned int}
   ../src/target/i386/emulate/panic.h:34:12: note: in definition of macro 'VM_PANIC_EX'
      34 |     printf(__VA_ARGS__); \
         |            ^~~~~~~~~~~
   ../src/target/i386/emulate/x86_mmu.c:251:48: note: format string is defined here
     251 |             VM_PANIC_EX("%s: mmu_gva_to_gpa %llx failed\n", __func__, gva);
         |                                             ~~~^
         |                                                |
         |                                                long long unsigned int
         |                                             %x

Fix the warning by reusing the target-specific macro TARGET_FMT_lx which exists for this exact purpose.

Quoted 6 lines
Fixes c97d6d2cdf97 ("i386: hvf: add code base from Google's QEMU repository")
cc qemu-stable
---
  target/i386/hvf/x86_mmu.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
#4 lore raw

PATCH 3/3 target/i386/emulate/x86_decode: Actually use stream in decode_instruction_stream()

In reply to Bernhard Beschow

Compared to decode_instruction(), decode_instruction_stream() has an additional stream parameter which avoids some guest memory accesses during instruction decoding. Both functions defer the actual work to decode_opcode() which would set the stream pointer to zero such that decode_instruction_stream() essentially behaved like decode_instruction(). Given that all callers of decode_instruction_stream() properly zero-initialize the decode parameter, the memset() call can be moved into decode_instruction() which is the only other user of decode_opcode(). This preserves the non-zero stream pointer which avoids extra guest memory accesses.

Fixes 1e25327b244a ("target/i386/emulate: Allow instruction decoding from stream")
cc qemu-stable
---
 target/i386/emulate/x86_decode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/i386/emulate/x86_decode.c +2 -2
index 6ad03b71b0..7bbcd2a9a2 100644
@@ -2088,8 +2088,6 @@ static void decode_opcodes(CPUX86State *env, struct x86_decode *decode)
 
 static uint32_t decode_opcode(CPUX86State *env, struct x86_decode *decode)
 {
-    memset(decode, 0, sizeof(*decode));
-
     decode_prefix(env, decode);
     set_addressing_size(env, decode);
     set_operand_size(env, decode);
@@ -2101,6 +2099,8 @@ static uint32_t decode_opcode(CPUX86State *env, struct x86_decode *decode)
 
 uint32_t decode_instruction(CPUX86State *env, struct x86_decode *decode)
 {
+    memset(decode, 0, sizeof(*decode));
+
     return decode_opcode(env, decode);
 }
 
Signature · 1 line
2.53.0
Reviewed-by on the v1 series
#5 lore raw
In reply to Bernhard Beschow
Quoted 15 lines · Bernhard Beschow

When testing Mohamed's recent x86_64 WHPX updates [1] I encountered two compilation issues under MSYS2/GCC which are fixed in this series. The last commit fixes a missed optimization opportunity in MSHV code.

The MSHV code will be reused in WHPX [1] which allowed me to test it. I didn't notice any performance benefits, though in theory there should be some due to less guest memory accesses.

Testing done:
* Use a Linux VM for a couple of hours

Quoted 8 lines

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Bernhard Beschow (3):
target/i386/emulate/x86_decode: Fix compiler warning target/i386/hvf/x86_mmu: Fix compiler warning target/i386/emulate/x86_decode: Actually use stream in
decode_instruction_stream()

For the series:

Reviewed-by Mohamed Mediouni <[email protected] <mailto:[email protected]>>
Quoted 7 lines

target/i386/emulate/x86_decode.c | 6 +++---
target/i386/hvf/x86_mmu.c | 6 ++++--

2 files changed, 7 insertions(+), 5 deletions(-)
Signature · 1 line
2.53.0
Reviewed-by on the v1 series
#6 lore raw
In reply to Bernhard Beschow
Quoted 11 lines · Bernhard Beschow

When testing Mohamed's recent x86_64 WHPX updates [1] I encountered two compilation issues under MSYS2/GCC which are fixed in this series. The last commit fixes a missed optimization opportunity in MSHV code.

The MSHV code will be reused in WHPX [1] which allowed me to test it. I didn't notice any performance benefits, though in theory there should be some due to less guest memory accesses.

Testing done:
* Use a Linux VM for a couple of hours

Quoted 7 lines

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Bernhard Beschow (3):
target/i386/emulate/x86_decode: Fix compiler warning target/i386/hvf/x86_mmu: Fix compiler warning target/i386/emulate/x86_decode: Actually use stream in
decode_instruction_stream()

#7 lore raw

Am 18. Februar 2026 06:31:56 UTC schrieb Wei Liu <[email protected]>:

Quoted 14 lines · Bernhard Beschow
Quoted 13 lines

When testing Mohamed's recent x86_64 WHPX updates [1] I encountered two compilation issues under MSYS2/GCC which are fixed in this series. The last commit fixes a missed optimization opportunity in MSHV code.

The MSHV code will be reused in WHPX [1] which allowed me to test it. I didn't notice any performance benefits, though in theory there should be some due to less guest memory accesses.

Testing done:
* Use a Linux VM for a couple of hours

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 7 lines
Quoted 5 lines

Bernhard Beschow (3):
target/i386/emulate/x86_decode: Fix compiler warning target/i386/hvf/x86_mmu: Fix compiler warning target/i386/emulate/x86_decode: Actually use stream in
decode_instruction_stream()

Hi Wei,

thanks for the tag! Note that there is a new series including this one: https://lore.kernel.org/qemu-devel/[email protected]/ So for the tag to be considered it should be added to the individual patches there.

Thanks,
Bernhard