x86/emul: Remove fallback path from SWAPGS
PATCH 1/1 x86/emul: Remove fallback path from SWAPGS
In real hardware, accesses to the registers cannot fail. The error paths are just an artefact of the hook functions needing to return something.
The best effort unwind is also something that doesn't exist in real hardware, and complicates following the logic.
Instead, use an ASSERT_UNREACHABLE() with a fallback of injecting #DF. Hitting this path is an error in Xen.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
Tested using LKGS's extention of the test emulator for SWAPGS.
--- xen/arch/x86/x86_emulate/0f01.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
xen/arch/x86/x86_emulate/0f01.c +13 -10
@@ -192,18 +192,21 @@ int x86emul_0f01(struct x86_emulate_state *s,if ( (rc = ops->read_segment (x86_seg_gs, &sreg, ctxt)) !=X86EMUL_OKAY || (rc = ops->read_msr (MSR_SHADOW_GS_BASE , &msr_val, - ctxt)) !=X86EMUL_OKAY || - (rc = ops->write_msr (MSR_SHADOW_GS_BASE , sreg.base, - ctxt, false)) !=X86EMUL_OKAY ) + ctxt)) !=X86EMUL_OKAY ) goto done; - sreg.base = msr_val; -if ( (rc = ops->write_segment (x86_seg_gs, &sreg, - ctxt)) !=X86EMUL_OKAY ) +if ( (rc = ops->write_msr (MSR_SHADOW_GS_BASE , sreg.base, + ctxt, false)) !=X86EMUL_OKAY || + (sreg.base = msr_val, + (rc = ops->write_segment (x86_seg_gs, &sreg, + ctxt)) !=X86EMUL_OKAY ) ) { -/* Best effort unwind (i.e. no real error checking). */ -if ( ops->write_msr (MSR_SHADOW_GS_BASE , msr_val, - ctxt, false) ==X86EMUL_EXCEPTION ) -x86_emul_reset_event (ctxt); +/* +* In real hardware, access to the registers cannot fail. It is +* an error in Xen if the writes fail given that both MSRs have +* equivalent checks. +*/ +ASSERT_UNREACHABLE (); +generate_exception (X86_EXC_DF ); goto done; }break ;
Signature · 1 line
2.39.5
Quoted 14 lines · Andrew Cooper
xen/arch/x86/x86_emulate/0f01.c +1
-6@@ -192,18 +192,21 @@ int x86emul_0f01(struct x86_emulate_state *s,if ( (rc = ops->read_segment (x86_seg_gs, &sreg, ctxt)) !=X86EMUL_OKAY || (rc = ops->read_msr (MSR_SHADOW_GS_BASE , &msr_val, - ctxt)) !=X86EMUL_OKAY || - (rc = ops->write_msr (MSR_SHADOW_GS_BASE , sreg.base, - ctxt, false)) !=X86EMUL_OKAY ) + ctxt)) !=X86EMUL_OKAY ) goto done; - sreg.base = msr_val; -if ( (rc = ops->write_segment (x86_seg_gs, &sreg, - ctxt)) !=X86EMUL_OKAY )
xen/arch/x86/x86_emulate/0f01.c int x86emul_0f01(struct x86_emulate_state *s, · line 206
Quoted 10 lines
xen/arch/x86/x86_emulate/0f01.c +5
-4+if ( (rc = ops->write_msr (MSR_SHADOW_GS_BASE , sreg.base, + ctxt, false)) !=X86EMUL_OKAY || + (sreg.base = msr_val, + (rc = ops->write_segment (x86_seg_gs, &sreg, + ctxt)) !=X86EMUL_OKAY ) ) { -/* Best effort unwind (i.e. no real error checking). */ -if ( ops->write_msr (MSR_SHADOW_GS_BASE , msr_val, - ctxt, false) ==X86EMUL_EXCEPTION ) -x86_emul_reset_event (ctxt);
I don't think this can be dropped. If (for whatever reason) ->write_msr() failed with X86EMUL_EXCEPTION, it would have recorded an exception. x86_emul_hw_exception() unconditionally checks there's none. Of course
...
xen/arch/x86/x86_emulate/0f01.c int x86emul_0f01(struct x86_emulate_state *s, · line 208
Quoted 6 lines
xen/arch/x86/x86_emulate/0f01.c +6
-0+/* +* In real hardware, access to the registers cannot fail. It is +* an error in Xen if the writes fail given that both MSRs have +* equivalent checks. +*/ +ASSERT_UNREACHABLE ();
... this and the ASSERT() there will both be present / absent at the same time, so in both release and debug builds the wanted effect is achieved, yet I think we'd set a bad precedent if we didn't x86_emul_reset_event() here first. (Also, technically it ought to be legitimate to convert any individual assertion to BUG_ON(), without strong need to look at any other assertions.) Alternatively ...
xen/arch/x86/x86_emulate/0f01.c int x86emul_0f01(struct x86_emulate_state *s, · line 209
Quoted 1 line
xen/arch/x86/x86_emulate/0f01.c +1
-0+generate_exception (X86_EXC_DF );
... we may want to consider to relax the ASSERT() there, e.g. to always permit #DF to override what's already there (if not already #DF).
I also think we'd better explicitly specify an error code (of 0) here. mkec() copes with the form above, yes, but afaics we never actually leverage this actively. Iirc it was merely meant to act as a safety net.
Jan
Quoted 40 lines · Andrew Cooper
In real hardware, accesses to the registers cannot fail. The error paths are just an artefact of the hook functions needing to return something.
The best effort unwind is also something that doesn't exist in real hardware, and complicates following the logic.
Instead, use an
ASSERT_UNREACHABLE()with a fallback of injecting #DF. Hitting this path is an error in Xen.Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>Tested using LKGS's extention of the test emulator for SWAPGS.
--- xen/arch/x86/x86_emulate/0f01.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)xen/arch/x86/x86_emulate/0f01.c +6
-6@@ -192,18 +192,21 @@ int x86emul_0f01(struct x86_emulate_state *s,if ( (rc = ops->read_segment (x86_seg_gs, &sreg, ctxt)) !=X86EMUL_OKAY || (rc = ops->read_msr (MSR_SHADOW_GS_BASE , &msr_val, - ctxt)) !=X86EMUL_OKAY || - (rc = ops->write_msr (MSR_SHADOW_GS_BASE , sreg.base, - ctxt, false)) !=X86EMUL_OKAY ) + ctxt)) !=X86EMUL_OKAY ) goto done; - sreg.base = msr_val; -if ( (rc = ops->write_segment (x86_seg_gs, &sreg, - ctxt)) !=X86EMUL_OKAY ) +if ( (rc = ops->write_msr (MSR_SHADOW_GS_BASE , sreg.base, + ctxt, false)) !=X86EMUL_OKAY || + (sreg.base = msr_val, + (rc = ops->write_segment (x86_seg_gs, &sreg, + ctxt)) !=X86EMUL_OKAY ) )
xen/arch/x86/x86_emulate/0f01.c int x86emul_0f01(struct x86_emulate_state *s, · line 207
Quoted 10 lines
xen/arch/x86/x86_emulate/0f01.c +5
-4{ -/* Best effort unwind (i.e. no real error checking). */ -if ( ops->write_msr (MSR_SHADOW_GS_BASE , msr_val, - ctxt, false) ==X86EMUL_EXCEPTION ) -x86_emul_reset_event (ctxt); +/* +* In real hardware, access to the registers cannot fail. It is +* an error in Xen if the writes fail given that both MSRs have +* equivalent checks. +*/
While copying the comment to the LKGS patch, I wondered: What "both MSRs" is this talking about? Both here and for LKGS it's ->write_msr() followed by ->write_segment(). This hence might be alluding to your further plan to avoid ->write_segment() on these paths?
Further, both having equivalent checks is either only a justification for the latter not failing, or only for the former not failing because it writes a value read from the other MSR.
It's not quite clear to me though how to best re-word things.
xen/arch/x86/x86_emulate/0f01.c int x86emul_0f01(struct x86_emulate_state *s, · line 210
Quoted 3 lines
xen/arch/x86/x86_emulate/0f01.c +2
-0+ASSERT_UNREACHABLE (); +generate_exception (X86_EXC_DF ); goto done;
While mirroring the change, it also occurred to me that this goto can be dropped, for being unreachable after generate_exception().
Jan