qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PULL v1 0/3] Microblaze signed division overflows
@ 2025-10-29 14:17 Edgar E. Iglesias
  2025-10-29 14:17 ` [PULL v1 1/3] target/microblaze: Remove unused arg from check_divz() Edgar E. Iglesias
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Edgar E. Iglesias @ 2025-10-29 14:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: richard.henderson, edgar.iglesias

From: "Edgar E. Iglesias" <edgar.iglesias@amd.com>

The following changes since commit bc831f37398b51dfe65d99a67bcff9352f84a9d2:

  Merge tag 'hw-misc-20251028' of https://github.com/philmd/qemu into staging (2025-10-28 11:48:05 +0100)

are available in the Git repository at:

  https://gitlab.com/edgar.iglesias/qemu.git tags/edgar/xilinx-queue-2025-10-29.for-upstream

for you to fetch changes up to cfc1d54251d3b4c4cf21c4fa278c8aea2fe25a99:

  target/microblaze: Handle signed division overflows (2025-10-29 13:37:44 +0100)

----------------------------------------------------------------
Xilinx queue

----------------------------------------------------------------
Edgar E. Iglesias (3):
  target/microblaze: Remove unused arg from check_divz()
  target/microblaze: div: Break out raise_divzero()
  target/microblaze: Handle signed division overflows

 target/microblaze/cpu.h       |  1 +
 target/microblaze/op_helper.c | 53 ++++++++++++++++++++++-------------
 target/microblaze/translate.c | 12 ++------
 3 files changed, 36 insertions(+), 30 deletions(-)

-- 
2.43.0



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PULL v1 1/3] target/microblaze: Remove unused arg from check_divz()
  2025-10-29 14:17 [PULL v1 0/3] Microblaze signed division overflows Edgar E. Iglesias
@ 2025-10-29 14:17 ` Edgar E. Iglesias
  2025-10-29 14:17 ` [PULL v1 2/3] target/microblaze: div: Break out raise_divzero() Edgar E. Iglesias
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Edgar E. Iglesias @ 2025-10-29 14:17 UTC (permalink / raw)
  To: qemu-devel, Edgar E. Iglesias
  Cc: richard.henderson, edgar.iglesias, Philippe Mathieu-Daudé

From: "Edgar E. Iglesias" <edgar.iglesias@amd.com>

Remove unused arg from check_divz(). No functional change.

Signed-off-by: Edgar E. Iglesias <edgar.iglesias@amd.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/microblaze/op_helper.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/target/microblaze/op_helper.c b/target/microblaze/op_helper.c
index df93c4229d..886224df3c 100644
--- a/target/microblaze/op_helper.c
+++ b/target/microblaze/op_helper.c
@@ -69,7 +69,7 @@ void helper_raise_exception(CPUMBState *env, uint32_t index)
     cpu_loop_exit(cs);
 }
 
-static bool check_divz(CPUMBState *env, uint32_t a, uint32_t b, uintptr_t ra)
+static bool check_divz(CPUMBState *env, uint32_t b, uintptr_t ra)
 {
     if (unlikely(b == 0)) {
         env->msr |= MSR_DZ;
@@ -89,7 +89,7 @@ static bool check_divz(CPUMBState *env, uint32_t a, uint32_t b, uintptr_t ra)
 
 uint32_t helper_divs(CPUMBState *env, uint32_t a, uint32_t b)
 {
-    if (!check_divz(env, a, b, GETPC())) {
+    if (!check_divz(env, b, GETPC())) {
         return 0;
     }
     return (int32_t)a / (int32_t)b;
@@ -97,7 +97,7 @@ uint32_t helper_divs(CPUMBState *env, uint32_t a, uint32_t b)
 
 uint32_t helper_divu(CPUMBState *env, uint32_t a, uint32_t b)
 {
-    if (!check_divz(env, a, b, GETPC())) {
+    if (!check_divz(env, b, GETPC())) {
         return 0;
     }
     return a / b;
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PULL v1 2/3] target/microblaze: div: Break out raise_divzero()
  2025-10-29 14:17 [PULL v1 0/3] Microblaze signed division overflows Edgar E. Iglesias
  2025-10-29 14:17 ` [PULL v1 1/3] target/microblaze: Remove unused arg from check_divz() Edgar E. Iglesias
@ 2025-10-29 14:17 ` Edgar E. Iglesias
  2025-10-29 14:17 ` [PULL v1 3/3] target/microblaze: Handle signed division overflows Edgar E. Iglesias
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Edgar E. Iglesias @ 2025-10-29 14:17 UTC (permalink / raw)
  To: qemu-devel, Edgar E. Iglesias
  Cc: richard.henderson, edgar.iglesias, Philippe Mathieu-Daudé

From: "Edgar E. Iglesias" <edgar.iglesias@amd.com>

Break out raise_divzero() and take the opportunity to rename
and reorder function args to better match with spec and
pseudo code.

No functional change.

Signed-off-by: Edgar E. Iglesias <edgar.iglesias@amd.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/microblaze/op_helper.c | 38 +++++++++++++++++------------------
 target/microblaze/translate.c | 12 ++---------
 2 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/target/microblaze/op_helper.c b/target/microblaze/op_helper.c
index 886224df3c..d3f688e375 100644
--- a/target/microblaze/op_helper.c
+++ b/target/microblaze/op_helper.c
@@ -69,38 +69,36 @@ void helper_raise_exception(CPUMBState *env, uint32_t index)
     cpu_loop_exit(cs);
 }
 
-static bool check_divz(CPUMBState *env, uint32_t b, uintptr_t ra)
+/* Raises ESR_EC_DIVZERO if exceptions are enabled.  */
+static void raise_divzero(CPUMBState *env, uint32_t esr, uintptr_t unwind_pc)
 {
-    if (unlikely(b == 0)) {
-        env->msr |= MSR_DZ;
-
-        if ((env->msr & MSR_EE) &&
-            env_archcpu(env)->cfg.div_zero_exception) {
-            CPUState *cs = env_cpu(env);
-
-            env->esr = ESR_EC_DIVZERO;
-            cs->exception_index = EXCP_HW_EXCP;
-            cpu_loop_exit_restore(cs, ra);
-        }
-        return false;
+    env->msr |= MSR_DZ;
+
+    if ((env->msr & MSR_EE) && env_archcpu(env)->cfg.div_zero_exception) {
+        CPUState *cs = env_cpu(env);
+
+        env->esr = esr;
+        cs->exception_index = EXCP_HW_EXCP;
+        cpu_loop_exit_restore(cs, unwind_pc);
     }
-    return true;
 }
 
-uint32_t helper_divs(CPUMBState *env, uint32_t a, uint32_t b)
+uint32_t helper_divs(CPUMBState *env, uint32_t ra, uint32_t rb)
 {
-    if (!check_divz(env, b, GETPC())) {
+    if (!ra) {
+        raise_divzero(env, ESR_EC_DIVZERO, GETPC());
         return 0;
     }
-    return (int32_t)a / (int32_t)b;
+    return (int32_t)rb / (int32_t)ra;
 }
 
-uint32_t helper_divu(CPUMBState *env, uint32_t a, uint32_t b)
+uint32_t helper_divu(CPUMBState *env, uint32_t ra, uint32_t rb)
 {
-    if (!check_divz(env, b, GETPC())) {
+    if (!ra) {
+        raise_divzero(env, ESR_EC_DIVZERO, GETPC());
         return 0;
     }
-    return a / b;
+    return rb / ra;
 }
 
 /* raise FPU exception.  */
diff --git a/target/microblaze/translate.c b/target/microblaze/translate.c
index 6442a250c5..fefe5cb552 100644
--- a/target/microblaze/translate.c
+++ b/target/microblaze/translate.c
@@ -450,16 +450,8 @@ DO_TYPEA0_CFG(flt, use_fpu >= 2, true, gen_flt)
 DO_TYPEA0_CFG(fint, use_fpu >= 2, true, gen_fint)
 DO_TYPEA0_CFG(fsqrt, use_fpu >= 2, true, gen_fsqrt)
 
-/* Does not use ENV_WRAPPER3, because arguments are swapped as well. */
-static void gen_idiv(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
-{
-    gen_helper_divs(out, tcg_env, inb, ina);
-}
-
-static void gen_idivu(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
-{
-    gen_helper_divu(out, tcg_env, inb, ina);
-}
+ENV_WRAPPER3(gen_idiv, gen_helper_divs)
+ENV_WRAPPER3(gen_idivu, gen_helper_divu)
 
 DO_TYPEA_CFG(idiv, use_div, true, gen_idiv)
 DO_TYPEA_CFG(idivu, use_div, true, gen_idivu)
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PULL v1 3/3] target/microblaze: Handle signed division overflows
  2025-10-29 14:17 [PULL v1 0/3] Microblaze signed division overflows Edgar E. Iglesias
  2025-10-29 14:17 ` [PULL v1 1/3] target/microblaze: Remove unused arg from check_divz() Edgar E. Iglesias
  2025-10-29 14:17 ` [PULL v1 2/3] target/microblaze: div: Break out raise_divzero() Edgar E. Iglesias
@ 2025-10-29 14:17 ` Edgar E. Iglesias
  2025-10-31  9:25 ` [PULL v1 0/3] Microblaze " Richard Henderson
  2025-10-31 10:23 ` Michael Tokarev
  4 siblings, 0 replies; 8+ messages in thread
From: Edgar E. Iglesias @ 2025-10-29 14:17 UTC (permalink / raw)
  To: qemu-devel, Edgar E. Iglesias; +Cc: richard.henderson, edgar.iglesias

From: "Edgar E. Iglesias" <edgar.iglesias@amd.com>

Handle signed division overflows as specified in UG984:
https://docs.amd.com/r/en-US/ug984-vivado-microblaze-ref/idiv

Signed-off-by: Edgar E. Iglesias <edgar.iglesias@amd.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 target/microblaze/cpu.h       |  1 +
 target/microblaze/op_helper.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/target/microblaze/cpu.h b/target/microblaze/cpu.h
index 14b107876a..d26b933b6d 100644
--- a/target/microblaze/cpu.h
+++ b/target/microblaze/cpu.h
@@ -87,6 +87,7 @@ typedef struct CPUArchState CPUMBState;
 #define          ESR_ESS_FSL_OFFSET     5
 
 #define          ESR_ESS_MASK  (0x7f << 5)
+#define          ESR_ESS_DEC_OF  (1 << 11) /* DEC: 0=DBZ, 1=OF */
 
 #define          ESR_EC_FSL             0
 #define          ESR_EC_UNALIGNED_DATA  1
diff --git a/target/microblaze/op_helper.c b/target/microblaze/op_helper.c
index d3f688e375..31da2c7c3a 100644
--- a/target/microblaze/op_helper.c
+++ b/target/microblaze/op_helper.c
@@ -89,6 +89,21 @@ uint32_t helper_divs(CPUMBState *env, uint32_t ra, uint32_t rb)
         raise_divzero(env, ESR_EC_DIVZERO, GETPC());
         return 0;
     }
+
+    /*
+     * Check for division overflows.
+     *
+     * Spec: https://docs.amd.com/r/en-US/ug984-vivado-microblaze-ref/idiv
+     * UG984, Chapter 5 MicroBlaze Instruction Set Architecture, idiv.
+     *
+     * If the U bit is clear, the value of rA is -1, and the value of rB is
+     * -2147483648 (divide overflow), the DZO bit in MSR will be set and
+     * the value in rD will be -2147483648, unless an exception is generated.
+     */
+    if ((int32_t)ra == -1 && (int32_t)rb == INT32_MIN) {
+        raise_divzero(env, ESR_EC_DIVZERO | ESR_ESS_DEC_OF, GETPC());
+        return INT32_MIN;
+    }
     return (int32_t)rb / (int32_t)ra;
 }
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PULL v1 0/3] Microblaze signed division overflows
  2025-10-29 14:17 [PULL v1 0/3] Microblaze signed division overflows Edgar E. Iglesias
                   ` (2 preceding siblings ...)
  2025-10-29 14:17 ` [PULL v1 3/3] target/microblaze: Handle signed division overflows Edgar E. Iglesias
@ 2025-10-31  9:25 ` Richard Henderson
  2025-10-31 10:23 ` Michael Tokarev
  4 siblings, 0 replies; 8+ messages in thread
From: Richard Henderson @ 2025-10-31  9:25 UTC (permalink / raw)
  To: Edgar E. Iglesias, qemu-devel; +Cc: edgar.iglesias

On 10/29/25 15:17, Edgar E. Iglesias wrote:
> From: "Edgar E. Iglesias"<edgar.iglesias@amd.com>
> 
> The following changes since commit bc831f37398b51dfe65d99a67bcff9352f84a9d2:
> 
>    Merge tag 'hw-misc-20251028' ofhttps://github.com/philmd/qemu into staging (2025-10-28 11:48:05 +0100)
> 
> are available in the Git repository at:
> 
>    https://gitlab.com/edgar.iglesias/qemu.git tags/edgar/xilinx-queue-2025-10-29.for-upstream
> 
> for you to fetch changes up to cfc1d54251d3b4c4cf21c4fa278c8aea2fe25a99:
> 
>    target/microblaze: Handle signed division overflows (2025-10-29 13:37:44 +0100)
> 
> ----------------------------------------------------------------
> Xilinx queue

Applied, thanks.  Please update https://wiki.qemu.org/ChangeLog/10.2 as appropriate.

r~


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PULL v1 0/3] Microblaze signed division overflows
  2025-10-29 14:17 [PULL v1 0/3] Microblaze signed division overflows Edgar E. Iglesias
                   ` (3 preceding siblings ...)
  2025-10-31  9:25 ` [PULL v1 0/3] Microblaze " Richard Henderson
@ 2025-10-31 10:23 ` Michael Tokarev
  2025-10-31 14:11   ` Edgar E. Iglesias
  4 siblings, 1 reply; 8+ messages in thread
From: Michael Tokarev @ 2025-10-31 10:23 UTC (permalink / raw)
  To: Edgar E. Iglesias, qemu-devel
  Cc: richard.henderson, edgar.iglesias, qemu-stable

On 10/29/25 17:17, Edgar E. Iglesias wrote:
..
>    target/microblaze: Remove unused arg from check_divz()
>    target/microblaze: div: Break out raise_divzero()
>    target/microblaze: Handle signed division overflows
> 
>   target/microblaze/cpu.h       |  1 +
>   target/microblaze/op_helper.c | 53 ++++++++++++++++++++++-------------
>   target/microblaze/translate.c | 12 ++------
>   3 files changed, 36 insertions(+), 30 deletions(-)

Is it a qemu-stable material?  It feels like it is, even
if it's a sort-of "new feature".

Thanks,

/mjt


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PULL v1 0/3] Microblaze signed division overflows
  2025-10-31 10:23 ` Michael Tokarev
@ 2025-10-31 14:11   ` Edgar E. Iglesias
  2025-10-31 21:53     ` Michael Tokarev
  0 siblings, 1 reply; 8+ messages in thread
From: Edgar E. Iglesias @ 2025-10-31 14:11 UTC (permalink / raw)
  To: Michael Tokarev
  Cc: Edgar E. Iglesias, qemu-devel, richard.henderson, qemu-stable

On Fri, Oct 31, 2025 at 01:23:38PM +0300, Michael Tokarev wrote:
> On 10/29/25 17:17, Edgar E. Iglesias wrote:
> ..
> >    target/microblaze: Remove unused arg from check_divz()
> >    target/microblaze: div: Break out raise_divzero()
> >    target/microblaze: Handle signed division overflows
> > 
> >   target/microblaze/cpu.h       |  1 +
> >   target/microblaze/op_helper.c | 53 ++++++++++++++++++++++-------------
> >   target/microblaze/translate.c | 12 ++------
> >   3 files changed, 36 insertions(+), 30 deletions(-)
> 
> Is it a qemu-stable material?  It feels like it is, even
> if it's a sort-of "new feature".
>

Yes, if no one opposes I'm in favor of backporting it.

Will you pick it up Michael? Or do you need anything more from me?

Cheers,
Edgar


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PULL v1 0/3] Microblaze signed division overflows
  2025-10-31 14:11   ` Edgar E. Iglesias
@ 2025-10-31 21:53     ` Michael Tokarev
  0 siblings, 0 replies; 8+ messages in thread
From: Michael Tokarev @ 2025-10-31 21:53 UTC (permalink / raw)
  To: Edgar E. Iglesias
  Cc: Edgar E. Iglesias, qemu-devel, richard.henderson, qemu-stable

On 10/31/25 17:11, Edgar E. Iglesias wrote:
> On Fri, Oct 31, 2025 at 01:23:38PM +0300, Michael Tokarev wrote:
..>> Is it a qemu-stable material?  It feels like it is, even
>> if it's a sort-of "new feature".
> 
> Yes, if no one opposes I'm in favor of backporting it.
> 
> Will you pick it up Michael? Or do you need anything more from me?

The only question is whenever microblaze is "useful enough", so a
stable qemu series is of interest to microblaze users.

I picked all 3 (since they stack on each other) for 10.0.x and 10.1.x,
they apply cleanly and seems to work fine (so far anyway).  But I don't
expect much breakage in this area.

Thank you for confirming!

/mjt


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-10-31 21:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-29 14:17 [PULL v1 0/3] Microblaze signed division overflows Edgar E. Iglesias
2025-10-29 14:17 ` [PULL v1 1/3] target/microblaze: Remove unused arg from check_divz() Edgar E. Iglesias
2025-10-29 14:17 ` [PULL v1 2/3] target/microblaze: div: Break out raise_divzero() Edgar E. Iglesias
2025-10-29 14:17 ` [PULL v1 3/3] target/microblaze: Handle signed division overflows Edgar E. Iglesias
2025-10-31  9:25 ` [PULL v1 0/3] Microblaze " Richard Henderson
2025-10-31 10:23 ` Michael Tokarev
2025-10-31 14:11   ` Edgar E. Iglesias
2025-10-31 21:53     ` Michael Tokarev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).