[PATCH] target/i386/tcg: fix lea address calculation for CS register

Mark Cave-Ayland posted 1 patch 1 month, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260927123807.492352-1-mark.cave-ayland@ilande.co.uk
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>
target/i386/tcg/translate.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
[PATCH] target/i386/tcg: fix lea address calculation for CS register
Posted by Mark Cave-Ayland 1 month, 3 weeks ago
When the effective address calculation was simplified in commit 55a15eb3504
("target/i386/tcg: simplify effective address calculation") the refactoring
accidentally introduced the ADDSEG() macro into the CS base address inclusion
test.

Fix the logic in gen_lea_v_seg_dest() to use CODE64() instead of ADDSEG() for
the CS base address inclusion test, and at the same time split it into a switch
statement to make the logic easier to follow.

Fixes: 55a15eb3504 ("target/i386/tcg: simplify effective address calculation")
Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
---
 target/i386/tcg/translate.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c
index 192cf43215e..2d3fffeab7b 100644
--- a/target/i386/tcg/translate.c
+++ b/target/i386/tcg/translate.c
@@ -605,7 +605,25 @@ static void gen_lea_v_seg_dest(DisasContext *s, MemOp aflag, TCGv dest, TCGv a0,
         ovr_seg = def_seg;
     }
 
-    has_base = ovr_seg >= R_FS || (ovr_seg >= 0 && ADDSEG(s));
+    switch (ovr_seg) {
+    case R_DS:
+    case R_ES:
+    case R_SS:
+        has_base = ADDSEG(s);
+        break;
+    case R_CS:
+        has_base = !CODE64(s);
+        break;
+    case R_FS:
+    case R_GS:
+        has_base = true;
+        break;
+    default:
+        assert(ovr_seg < 0);
+        has_base = false;
+        break;
+    }
+
     easize = CODE64(s) ? MO_64 : MO_32;
 
     if (has_base) {
-- 
2.47.3
Re: [PATCH] target/i386/tcg: fix lea address calculation for CS register
Posted by Paolo Bonzini 2 weeks, 5 days ago
Queued, thanks.

Paolo
Re: [PATCH] target/i386/tcg: fix lea address calculation for CS register
Posted by Mark Cave-Ayland 1 month, 1 week ago
On 27/09/2026 13:36, Mark Cave-Ayland wrote:

> When the effective address calculation was simplified in commit 55a15eb3504
> ("target/i386/tcg: simplify effective address calculation") the refactoring
> accidentally introduced the ADDSEG() macro into the CS base address inclusion
> test.
> 
> Fix the logic in gen_lea_v_seg_dest() to use CODE64() instead of ADDSEG() for
> the CS base address inclusion test, and at the same time split it into a switch
> statement to make the logic easier to follow.
> 
> Fixes: 55a15eb3504 ("target/i386/tcg: simplify effective address calculation")
> Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
> ---
>   target/i386/tcg/translate.c | 20 +++++++++++++++++++-
>   1 file changed, 19 insertions(+), 1 deletion(-)
> 
> diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c
> index 192cf43215e..2d3fffeab7b 100644
> --- a/target/i386/tcg/translate.c
> +++ b/target/i386/tcg/translate.c
> @@ -605,7 +605,25 @@ static void gen_lea_v_seg_dest(DisasContext *s, MemOp aflag, TCGv dest, TCGv a0,
>           ovr_seg = def_seg;
>       }
>   
> -    has_base = ovr_seg >= R_FS || (ovr_seg >= 0 && ADDSEG(s));
> +    switch (ovr_seg) {
> +    case R_DS:
> +    case R_ES:
> +    case R_SS:
> +        has_base = ADDSEG(s);
> +        break;
> +    case R_CS:
> +        has_base = !CODE64(s);
> +        break;
> +    case R_FS:
> +    case R_GS:
> +        has_base = true;
> +        break;
> +    default:
> +        assert(ovr_seg < 0);
> +        has_base = false;
> +        break;
> +    }
> +
>       easize = CODE64(s) ? MO_64 : MO_32;
>   
>       if (has_base) {

Paolo/Richard - is one of you able to take this patch via either the TCG or x86 queue?


ATB,

Mark.
Re: [PATCH] target/i386/tcg: fix lea address calculation for CS register
Posted by Richard Henderson 1 month, 3 weeks ago
On 9/27/26 05:36, Mark Cave-Ayland wrote:
> When the effective address calculation was simplified in commit 55a15eb3504
> ("target/i386/tcg: simplify effective address calculation") the refactoring
> accidentally introduced the ADDSEG() macro into the CS base address inclusion
> test.
> 
> Fix the logic in gen_lea_v_seg_dest() to use CODE64() instead of ADDSEG() for
> the CS base address inclusion test, and at the same time split it into a switch
> statement to make the logic easier to follow.
> 
> Fixes: 55a15eb3504 ("target/i386/tcg: simplify effective address calculation")
> Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
> ---
>   target/i386/tcg/translate.c | 20 +++++++++++++++++++-
>   1 file changed, 19 insertions(+), 1 deletion(-)
> 
> diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c
> index 192cf43215e..2d3fffeab7b 100644
> --- a/target/i386/tcg/translate.c
> +++ b/target/i386/tcg/translate.c
> @@ -605,7 +605,25 @@ static void gen_lea_v_seg_dest(DisasContext *s, MemOp aflag, TCGv dest, TCGv a0,
>           ovr_seg = def_seg;
>       }
>   
> -    has_base = ovr_seg >= R_FS || (ovr_seg >= 0 && ADDSEG(s));
> +    switch (ovr_seg) {
> +    case R_DS:
> +    case R_ES:
> +    case R_SS:
> +        has_base = ADDSEG(s);
> +        break;
> +    case R_CS:
> +        has_base = !CODE64(s);

I suppose one slight improvement could be

     has_base = s->cs_base != 0;

since we know the value.  It's more rearrangement than it's worth to add 
a non-zero constant instead of the seg_base temp.

Anyway,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


> +        break;
> +    case R_FS:
> +    case R_GS:
> +        has_base = true;
> +        break;
> +    default:
> +        assert(ovr_seg < 0);
> +        has_base = false;
> +        break;
> +    }
> +
>       easize = CODE64(s) ? MO_64 : MO_32;
>   
>       if (has_base) {