[kvm-unit-tests PATCH 05/14] x86: Force the compiler to retrieve exception info from per-cpu area

Sean Christopherson
2021-04-22T03:05:31Z

Tag the exception vector/error code/flags inline asm as volatile so that
it's not elided by the compiler. Without "volatile", the compiler may
omit the instruction if it inlines the helper and observes that the
memory isn't modified between the store in TRY_CATCH() and the load in
the helper.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
lib/x86/desc.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/x86/desc.c b/lib/x86/desc.c
index 983d4d8..9c1228c 100644
--- a/lib/x86/desc.c
+++ b/lib/x86/desc.c
@@ -249,7 +249,7 @@ unsigned exception_vector(void)
{
unsigned char vector;

- asm("movb %%gs:4, %0" : "=q"(vector));
+ asm volatile("movb %%gs:4, %0" : "=q"(vector));
return vector;
}

@@ -265,7 +265,7 @@ unsigned exception_error_code(void)
{
unsigned short error_code;

- asm("mov %%gs:6, %0" : "=r"(error_code));
+ asm volatile("mov %%gs:6, %0" : "=r"(error_code));
return error_code;
}

@@ -273,7 +273,7 @@ bool exception_rflags_rf(void)
{
unsigned char rf_flag;

- asm("movb %%gs:5, %b0" : "=q"(rf_flag));
+ asm volatile("movb %%gs:5, %b0" : "=q"(rf_flag));
return rf_flag & 1;
}