Skip to contentSkip to conversation

target/ppc: Implement ISA v3.1 wait variants

PATCH In review Nicholas Piggin 2 participants 4 messages May 17, 2021 – May 24, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/ppc: Implement ISA v3.1 wait variants

ISA v3.1 adds new variations of wait, specified by the WC field. These are not compatible with the wait 0 implementation, because they add additional conditions that cause the processor to resume, which can cause software to hang or run very slowly.

Add the new wait variants with a trivial no-op implementation, which is allowed, as explained in comments: software must not depend on any particular architected WC condition having caused resumption of execution, therefore a no-op implementation is architecturally correct.

---
Implementing cpu_relax() in Linux with wait 2,0 (pause_short) causes a hang on boot without this patch.

 target/ppc/translate.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)
target/ppc/translate.c +33 -6
index a6381208a5..80db450cab 100644
@@ -3619,12 +3619,39 @@ static void gen_sync(DisasContext *ctx)
 /* wait */
 static void gen_wait(DisasContext *ctx)
 {
-    TCGv_i32 t0 = tcg_const_i32(1);
-    tcg_gen_st_i32(t0, cpu_env,
-                   -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
-    tcg_temp_free_i32(t0);
-    /* Stop translation, as the CPU is supposed to sleep from now */
-    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
+    uint32_t wc = (ctx->opcode >> 21) & 3;
+
+    /*
+     * wait 0 waits for an exception to occur.
+     */
+    if (wc == 0) {
+        TCGv_i32 t0 = tcg_const_i32(1);
+        tcg_gen_st_i32(t0, cpu_env,
+                       -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
+        tcg_temp_free_i32(t0);
+        /* Stop translation, as the CPU is supposed to sleep from now */
+        gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
+    }
+
+    /*
+     * Other wait types must not wait until an exception occurs because
+     * ignoring their other wake-up conditions could cause a hang.
+     *
+     * wait 1 (waitrsv) waits for an exception or a reservation to be lost.
+     * This can happen for implementation specific reasons, so it can be
+     * implemented as a no-op.
+     *
+     * wait 2 waits for an exception or an amount of time to pass. This is
+     * implementation specific so it can be implemented as a no-op.
+     *
+     * wait 3 is reserved, so it may be implemented as a no-op.
+     *
+     * ISA v3.1 does allow for execution to resume "in the rare case of
+     * an implementation-dependent event", so in any case software must
+     * not depend on the architected resumption condition to become
+     * true, so no-op implementations are architecturally correct (if
+     * suboptimal).
+     */
 }
 
 #if defined(TARGET_PPC64)
Signature · 1 line
2.23.0
#1 lore raw
Quoted 11 lines · Nicholas Piggin

ISA v3.1 adds new variations of wait, specified by the WC field. These are not compatible with the wait 0 implementation, because they add additional conditions that cause the processor to resume, which can cause software to hang or run very slowly.

Add the new wait variants with a trivial no-op implementation, which is allowed, as explained in comments: software must not depend on any particular architected WC condition having caused resumption of execution, therefore a no-op implementation is architecturally correct.

Logic looks fine. There is no test on the CPU's features or model here, though, so this will change behaviour for pre-3.1 CPUs as well.

What would invoking these wait variants (presumably reserved) on earlier CPUs do?

Quoted 57 lines

---
Implementing cpu_relax() in Linux with wait 2,0 (pause_short) causes a hang on boot without this patch.

 target/ppc/translate.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)
target/ppc/translate.c +33 -6
index a6381208a5..80db450cab 100644
@@ -3619,12 +3619,39 @@ static void gen_sync(DisasContext *ctx)
 /* wait */
 static void gen_wait(DisasContext *ctx)
 {
-    TCGv_i32 t0 = tcg_const_i32(1);
-    tcg_gen_st_i32(t0, cpu_env,
-                   -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
-    tcg_temp_free_i32(t0);
-    /* Stop translation, as the CPU is supposed to sleep from now */
-    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
+    uint32_t wc = (ctx->opcode >> 21) & 3;
+
+    /*
+     * wait 0 waits for an exception to occur.
+     */
+    if (wc == 0) {
+        TCGv_i32 t0 = tcg_const_i32(1);
+        tcg_gen_st_i32(t0, cpu_env,
+                       -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
+        tcg_temp_free_i32(t0);
+        /* Stop translation, as the CPU is supposed to sleep from now */
+        gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
+    }
+
+    /*
+     * Other wait types must not wait until an exception occurs because
+     * ignoring their other wake-up conditions could cause a hang.
+     *
+     * wait 1 (waitrsv) waits for an exception or a reservation to be lost.
+     * This can happen for implementation specific reasons, so it can be
+     * implemented as a no-op.
+     *
+     * wait 2 waits for an exception or an amount of time to pass. This is
+     * implementation specific so it can be implemented as a no-op.
+     *
+     * wait 3 is reserved, so it may be implemented as a no-op.
+     *
+     * ISA v3.1 does allow for execution to resume "in the rare case of
+     * an implementation-dependent event", so in any case software must
+     * not depend on the architected resumption condition to become
+     * true, so no-op implementations are architecturally correct (if
+     * suboptimal).
+     */
 }
 
 #if defined(TARGET_PPC64)
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#2 lore raw

Excerpts from David Gibson's message of May 17, 2021 3:39 pm:

Quoted 11 lines · Nicholas Piggin
Quoted 10 lines

ISA v3.1 adds new variations of wait, specified by the WC field. These are not compatible with the wait 0 implementation, because they add additional conditions that cause the processor to resume, which can cause software to hang or run very slowly.

Add the new wait variants with a trivial no-op implementation, which is allowed, as explained in comments: software must not depend on any particular architected WC condition having caused resumption of execution, therefore a no-op implementation is architecturally correct.

Quoted 4 lines
Quoted 1 line

Logic looks fine. There is no test on the CPU's features or model here, though, so this will change behaviour for pre-3.1 CPUs as well.

Huh. 2.06-2.07 has very similar WC bits as 3.1, but 3.0 removed them and made them reserved. I should have looked back but I'd assumed they weren't there either.

Existing code treats WC != 0 as invalid on pre-3.0 processors AFAIKS, so that's not quite right for 2.06-7 (they should look more like 3.1).

But before that it looks like it was just wait with no WC field.

Quoted 2 lines

What would invoking these wait variants (presumably reserved) on earlier CPUs do?

Prior to 2.06, it looks like there is no WC field, and so they should generate a program check. So that just leaves the incorrect program checks for 2.06-7, something like this should do it:

-GEN_HANDLER_E(wait, 0x1F, 0x1E, 0x00, 0x039FF801, PPC_NONE, PPC2_ISA300), +GEN_HANDLER_E(wait, 0x1F, 0x1E, 0x00, 0x039FF801, PPC_NONE, PPC2_ISA206),

2.06-3.1 should all be fine with this patch, AFAIKS they all have words to the effect that WC != 0 is subject to implementation defined behaviour and may be treated as a no-op or not implemented.

Thanks,
Nick

#3 lore raw
Quoted 33 lines · Nicholas Piggin

Excerpts from David Gibson's message of May 17, 2021 3:39 pm:

Quoted 11 lines · Nicholas Piggin
Quoted 10 lines

ISA v3.1 adds new variations of wait, specified by the WC field. These are not compatible with the wait 0 implementation, because they add additional conditions that cause the processor to resume, which can cause software to hang or run very slowly.

Add the new wait variants with a trivial no-op implementation, which is allowed, as explained in comments: software must not depend on any particular architected WC condition having caused resumption of execution, therefore a no-op implementation is architecturally correct.

Quoted 4 lines
Quoted 1 line

Logic looks fine. There is no test on the CPU's features or model here, though, so this will change behaviour for pre-3.1 CPUs as well.

Huh. 2.06-2.07 has very similar WC bits as 3.1, but 3.0 removed them and made them reserved. I should have looked back but I'd assumed they weren't there either.

Existing code treats WC != 0 as invalid on pre-3.0 processors AFAIKS, so that's not quite right for 2.06-7 (they should look more like 3.1).

But before that it looks like it was just wait with no WC field.

Quoted 2 lines

What would invoking these wait variants (presumably reserved) on earlier CPUs do?

Prior to 2.06, it looks like there is no WC field, and so they should generate a program check. So that just leaves the incorrect program checks for 2.06-7, something like this should do it:

Review

Quoted 2 lines
(quoted diff) +1 -1
-GEN_HANDLER_E(wait, 0x1F, 0x1E, 0x00, 0x039FF801, PPC_NONE, PPC2_ISA300),
+GEN_HANDLER_E(wait, 0x1F, 0x1E, 0x00, 0x039FF801, PPC_NONE, PPC2_ISA206),

Ok, can you update with such a change, and put some of this explanation of the history in a comment.

Quoted 3 lines

2.06-3.1 should all be fine with this patch, AFAIKS they all have words to the effect that WC != 0 is subject to implementation defined behaviour and may be treated as a no-op or not implemented.

Ok. Note that we do try to match specific CPU behaviour, not just the architecture, although the architecture is obviously more important.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson