selftests/x86: Fix sysret_rip assertion failure on FRED systems
PATCH 1/1 selftests/x86: Fix sysret_rip assertion failure on FRED systems
The existing 'sysret_rip' selftest asserts that 'regs->r11 == regs->flags'. This check relies on the behavior of the SYSCALL instruction on legacy x86_64, which saves 'RFLAGS' into 'R11'.
However, on systems with FRED (Flexible Return and Event Delivery) enabled, instead of using registers, all state is saved onto the stack. Consequently, 'R11' retains its userspace value, causing the assertion to fail.
Fix this by detecting FRED support via CPUID (Leaf 0x7, Subleaf 0x1, EAX bit 17) and skipping the register assertion if FRED is present.
--- tools/testing/selftests/x86/sysret_rip.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
tools/testing/selftests/x86/sysret_rip.c +13 -3
@@ -21,6 +21,7 @@#include <sys/user.h> #include <sys/mman.h> #include <assert.h> +#include <cpuid.h> #include "helpers.h"
@@ -64,9 +65,18 @@ static void sigusr1(int sig, siginfo_t *info, void *ctx_void) ctx->uc_mcontext.gregs[REG_RIP ] = rip; ctx->uc_mcontext.gregs[REG_RCX ] = rip; -/* R11 and EFLAGS should already match. */ -assert (ctx->uc_mcontext.gregs[REG_EFL ] == - ctx->uc_mcontext.gregs[REG_R11 ]); +/* +* SYSCALL works differently on FRED, it does not save RIP and RFLAGS +* to RCX and R11. +*/ +unsigned int eax, ebx, ecx, edx; + +__cpuid_count (0x7 ,0x1 , eax, ebx, ecx, edx); +if (!(eax & (1 <<17 ))) { +/* R11 and EFLAGS should already match. */ +assert (ctx->uc_mcontext.gregs[REG_EFL ] == + ctx->uc_mcontext.gregs[REG_R11 ]); + }sethandler (SIGSEGV , sigsegv_for_sigreturn_test,SA_RESETHAND ); }
Signature · 1 line
2.43.0
tools/testing/selftests/x86/sysret_rip.c static void sigusr1(int sig, siginfo_t *info, void *ctx_void) · line 79
Quoted 12 lines · Yi Lai
tools/testing/selftests/x86/sysret_rip.c +12
-0+/* +* SYSCALL works differently on FRED, it does not save RIP and RFLAGS +* to RCX and R11. +*/ +unsigned int eax, ebx, ecx, edx; + +__cpuid_count (0x7 ,0x1 , eax, ebx, ecx, edx); +if (!(eax & (1 <<17 ))) { +/* R11 and EFLAGS should already match. */ +assert (ctx->uc_mcontext.gregs[REG_EFL ] == + ctx->uc_mcontext.gregs[REG_R11 ]); + }
Could of things:
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best. This at _least_ needs to be commented. Could you check around for other cases like this in the x86 selftests and see what the existing solutions are?
Second, this is selftests/, but I feel like we can be a bit more disciplined than doing raw CPUID calls and sprinkling magic numbers all about. The least that can be done here is giving bit 17 a name and making the "is FRED supported" into a helper function.
But it does look like something that needs to get fixed either way!
Quoted 3 lines
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best.
A reliable way to distinguish IDT and FRED mode is to:
1) Load $3 into %fs (x86_64) or %gs (i386) (i.e. whichever isn't thread local stoage)
2) execute a breakpoint, ignore the signal
3) Look to see whether %fs/%gs holds 3 or 0
IRET has a fun behaviour where it zeroes NULL selectors even if they had a non-zero RPL.
ERETU doesn't do this; Andy Luto and I asked for this minor information leak to be removed, and Intel agreed as it served no purpose anyone could identify.
As a consequence, you can use it to determine whether the kernel used IRET or ERET to return back to userspace.
~Andrew
Quoted 13 lines · Andrew Cooper
Quoted 3 lines
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best.
A reliable way to distinguish IDT and FRED mode is to:
1) Load $3 into %fs (
x86_64) or %gs (i386) (i.e. whichever isn't thread local stoage)
2) execute a breakpoint, ignore the signal
3) Look to see whether %fs/%gs holds 3 or 0IRET has a fun behaviour where it zeroes NULL selectors even if they had a non-zero RPL.
Quoted 6 lines
ERETU doesn't do this; Andy Luto and I asked for this minor information leak to be removed, and Intel agreed as it served no purpose anyone could identify.
As a consequence, you can use it to determine whether the kernel used IRET or ERET to return back to userspace.
I was thinking of just grepping /proc/cpuinfo for "fred", but that sounds much more fun! :)
Quoted 22 lines · Dave Hansen
Quoted 17 lines · Andrew Cooper
Quoted 3 lines
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best.
A reliable way to distinguish IDT and FRED mode is to:
1) Load $3 into %fs (
x86_64) or %gs (i386) (i.e. whichever isn't thread local stoage)
2) execute a breakpoint, ignore the signal
3) Look to see whether %fs/%gs holds 3 or 0IRET has a fun behaviour where it zeroes NULL selectors even if they had a non-zero RPL.
ERETU doesn't do this; Andy Luto and I asked for this minor information leak to be removed, and Intel agreed as it served no purpose anyone could identify.
Quoted 5 lines
Quoted 2 lines
As a consequence, you can use it to determine whether the kernel used IRET or ERET to return back to userspace.
I was thinking of just grepping
/proc/cpuinfofor "fred", but that sounds much more fun! :)
+1 :)
This serves as a key architectural differentiator between FRED and the legacy IDT framework.
For additional context, here is a fix to user segment selector values:
https://lore.kernel.org/all/174069328263.10177.6796873487608898067.tip-bot2@tip-bot2/
It’s worth noting that there was an attempt to fix this bug roughly three years ago:
https://lore.kernel.org/lkml/[email protected]/
Quoted 33 lines · Xin Li
Quoted 22 lines · Dave Hansen
Quoted 17 lines · Andrew Cooper
Quoted 3 lines
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best.
A reliable way to distinguish IDT and FRED mode is to:
1) Load $3 into %fs (
x86_64) or %gs (i386) (i.e. whichever isn't thread local stoage)
2) execute a breakpoint, ignore the signal
3) Look to see whether %fs/%gs holds 3 or 0IRET has a fun behaviour where it zeroes NULL selectors even if they had a non-zero RPL.
ERETU doesn't do this; Andy Luto and I asked for this minor information leak to be removed, and Intel agreed as it served no purpose anyone could identify.
Quoted 5 lines
Quoted 2 lines
As a consequence, you can use it to determine whether the kernel used IRET or ERET to return back to userspace.
I was thinking of just grepping
/proc/cpuinfofor "fred", but that sounds much more fun! :)+1 :)
This serves as a key architectural differentiator between FRED and the legacy IDT framework.
Quoted 7 lines
For additional context, here is a fix to user segment selector values:
https://lore.kernel.org/all/174069328263.10177.6796873487608898067.tip-bot2@tip-bot2/It’s worth noting that there was an attempt to fix this bug roughly three years ago:
https://lore.kernel.org/lkml/[email protected]/
Thanks for the context and links, Xin.
My patch is a simple, focused fix to unblock current regression in 'sysret_rip'. The goal is just to ensure it doesn't falsely fail on FRED systems by skipping the IDT assertion (R11==RFLAGS).
Checking Ammar's patch series, it indeed looks like a more comprehensive solution that not only handles the difference but also adds specific test coverage for consistency. Is there a specific reason it didn't land in the mainline kernel? If you think that is the preferred direction, I would be happy to contribute to that effort collaboratively.
Quoted 3 lines
Quoted 7 lines
Checking Ammar's patch series, it indeed looks like a more comprehensive solution that not only handles the difference but also adds specific test coverage for consistency. Is there a specific reason it didn't land in the mainline kernel? If you think that is the preferred direction, I would be happy to contribute to that effort collaboratively.
I prefer the approach Andrew suggested.
Ammar disappeared so that patch series landed nowhere.
H. Peter Anvin
reviewerQuoted 7 lines · Yi
Thanks for the context and links, Xin.
My patch is a simple, focused fix to unblock current regression in '
sysret_rip'. The goal is just to ensure it doesn't falsely fail on FRED systems by skipping the IDT assertion (R11==RFLAGS).
Quoted 7 lines
Checking Ammar's patch series, it indeed looks like a more comprehensive solution that not only handles the difference but also adds specific test coverage for consistency. Is there a specific reason it didn't land in the mainline kernel? If you think that is the preferred direction, I would be happy to contribute to that effort collaboratively.
So for what it's worth, I have used this sequence to probe for FRED, but it is basically probing for the effect you are truing to test for here:
#include <inttypes.h>
#include <stdbool.h>
#include <linux/unistd.h>
bool fred_is_on(void)
{
const uint64_t sentinel1 = 0xfeedfacedeadbeef;
const uint64_t sentinel2 = 0xbeeffeeb1ef00d1e;
register uint64_t rax asm("rax") = __NR_getppid;
register uint64_t rcx asm("rcx") = sentinel1;
register uint64_t r11 asm("r11") = sentinel2;
asm volatile("syscall" : "+r" (rax), "+r" (rcx), "+r" (r11));
return !((rcx ^ sentinel1) | (r11 ^ sentinel2));
}-hpa
Quoted 35 lines · H. Peter Anvin
Quoted 6 lines · Yi
Thanks for the context and links, Xin.
My patch is a simple, focused fix to unblock current regression in '
sysret_rip'. The goal is just to ensure it doesn't falsely fail on FRED systems by skipping the IDT assertion (R11==RFLAGS).Quoted 7 lines
Checking Ammar's patch series, it indeed looks like a more comprehensive solution that not only handles the difference but also adds specific test coverage for consistency. Is there a specific reason it didn't land in the mainline kernel? If you think that is the preferred direction, I would be happy to contribute to that effort collaboratively.
So for what it's worth, I have used this sequence to probe for FRED, but it is basically probing for the effect you are truing to test for here:
#include <
inttypes.h>
#include <stdbool.h>
#include <linux/unistd.h>bool
fred_is_on(void){ const uint64_t sentinel1 = 0xfeedfacedeadbeef; const uint64_t sentinel2 = 0xbeeffeeb1ef00d1e; register uint64_t rax asm("rax") = __NR_getppid; register uint64_t rcx asm("rcx") = sentinel1; register uint64_t r11 asm("r11") = sentinel2; asm volatile("syscall" : "+r" (rax), "+r" (rcx), "+r" (r11)); return !((rcx ^ sentinel1) | (r11 ^ sentinel2)); }
Quoted 1 line
-hpa
This doesn't really test whether FRED is active. It tests whether the OS is not providing strict backwards compatibility, and I think will malfunction when there's a hypervisor above Linux providing strict backwards compatibility.
~Andrew
H. Peter Anvin
reviewerQuoted 6 lines · Andrew Cooper
This doesn't really test whether FRED is active. It tests whether the OS is not providing strict backwards compatibility, and I think will malfunction when there's a hypervisor above Linux providing strict backwards compatibility.
But that applies equally to IRET, no? If the hypervisor clobbers the segment selector like IRET would in the interest of compatibility then you have the same issue.
If the guest is running a FRED kernel and the hypervisor goes in and mucks with the state after ERETU behind the kernel's back I think you have a much, much bigger problem.
-hpa
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Andrew Cooper
This doesn't really test whether FRED is active. It tests whether the OS is not providing strict backwards compatibility, and I think will malfunction when there's a hypervisor above Linux providing strict backwards compatibility.
But that applies equally to IRET, no? If the hypervisor clobbers the segment selector like IRET would in the interest of compatibility then you have the same issue.
I suppose. I for one don't care to provide that level of compatibility.
But for SYSCALL, what are Linux's plans for CRIU or RR ? I had to fix SYSCALL legacy behaviour in Xen for the following case:
* PV guest issues SYSCALL on FRED system. %rcx/%r11 not clobbered
* Migrate to a non-FRED system
* Xen uses a real SYSRET instruction to resume execution
Here, the guest continues executing at whichever dead variable is in %rcx.
CRIU/RR won't be exactly the same, but will suffer the same class of problem when moving between FRED and non-FRED systems.
~Andrew
H. Peter Anvin
reviewerQuoted 21 lines · Andrew Cooper
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Andrew Cooper
This doesn't really test whether FRED is active. It tests whether the OS is not providing strict backwards compatibility, and I think will malfunction when there's a hypervisor above Linux providing strict backwards compatibility.
But that applies equally to IRET, no? If the hypervisor clobbers the segment selector like IRET would in the interest of compatibility then you have the same issue.
I suppose. I for one don't care to provide that level of compatibility.
But for SYSCALL, what are Linux's plans for CRIU or RR ? I had to fix SYSCALL legacy behaviour in Xen for the following case:
* PV guest issues SYSCALL on FRED system. %rcx/%r11 not clobbered
* Migrate to a non-FRED system
* Xen uses a real SYSRET instruction to resume execution
Quoted 6 lines
Here, the guest continues executing at whichever dead variable is in %rcx.
CRIU/RR won't be exactly the same, but will suffer the same class of problem when moving between FRED and non-FRED systems.
~Andrew
"Doctor, it hurts when I PV?"
Quoted 21 lines · H. Peter Anvin
Quoted 20 lines · Andrew Cooper
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Andrew Cooper
This doesn't really test whether FRED is active. It tests whether the OS is not providing strict backwards compatibility, and I think will malfunction when there's a hypervisor above Linux providing strict backwards compatibility.
But that applies equally to IRET, no? If the hypervisor clobbers the segment selector like IRET would in the interest of compatibility then you have the same issue.
I suppose. I for one don't care to provide that level of compatibility.
But for SYSCALL, what are Linux's plans for CRIU or RR ? I had to fix SYSCALL legacy behaviour in Xen for the following case:
* PV guest issues SYSCALL on FRED system. %rcx/%r11 not clobbered
* Migrate to a non-FRED system
* Xen uses a real SYSRET instruction to resume execution
Quoted 7 lines
Quoted 6 lines
Here, the guest continues executing at whichever dead variable is in %rcx.
CRIU/RR won't be exactly the same, but will suffer the same class of problem when moving between FRED and non-FRED systems.
~Andrew
"Doctor, it hurts when I PV?"
I'm asking straight up, what is Linux doing to fix this same issue for CRIU/RR?
~Andrew
P.S. It's rhetorical, seeing as it's taken between Linux 6.9 and now (2 whole years) for anyone to even run the x86 selftests on a FRED system.
H. Peter Anvin
reviewerQuoted 30 lines · Andrew Cooper
Quoted 21 lines · H. Peter Anvin
Quoted 20 lines · Andrew Cooper
Quoted 9 lines · H. Peter Anvin
Quoted 5 lines · Andrew Cooper
This doesn't really test whether FRED is active. It tests whether the OS is not providing strict backwards compatibility, and I think will malfunction when there's a hypervisor above Linux providing strict backwards compatibility.
But that applies equally to IRET, no? If the hypervisor clobbers the segment selector like IRET would in the interest of compatibility then you have the same issue.
I suppose. I for one don't care to provide that level of compatibility.
But for SYSCALL, what are Linux's plans for CRIU or RR ? I had to fix SYSCALL legacy behaviour in Xen for the following case:
* PV guest issues SYSCALL on FRED system. %rcx/%r11 not clobbered
* Migrate to a non-FRED system
* Xen uses a real SYSRET instruction to resume executionQuoted 7 lines
Quoted 6 lines
Here, the guest continues executing at whichever dead variable is in %rcx.
CRIU/RR won't be exactly the same, but will suffer the same class of problem when moving between FRED and non-FRED systems.
~Andrew
"Doctor, it hurts when I PV?"
Quoted 7 lines
I'm asking straight up, what is Linux doing to fix this same issue for CRIU/RR?
~Andrew
P.S. It's rhetorical, seeing as it's taken between Linux 6.9 and now (2 whole years) for anyone to even run the x86 selftests on a FRED system.
That's not true, and in fact *this exact bug was fixed once already*; apparently someone "fixed the fix?"
I'm well and truly confused by it.
H. Peter Anvin
reviewerQuoted 18 lines · Dave Hansen
Quoted 17 lines · Andrew Cooper
Quoted 3 lines
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best.
A reliable way to distinguish IDT and FRED mode is to:
1) Load $3 into %fs (
x86_64) or %gs (i386) (i.e. whichever isn't thread local stoage)
2) execute a breakpoint, ignore the signal
3) Look to see whether %fs/%gs holds 3 or 0IRET has a fun behaviour where it zeroes NULL selectors even if they had a non-zero RPL.
ERETU doesn't do this; Andy Luto and I asked for this minor information leak to be removed, and Intel agreed as it served no purpose anyone could identify.
Quoted 5 lines
Quoted 2 lines
As a consequence, you can use it to determine whether the kernel used IRET or ERET to return back to userspace.
I was thinking of just grepping
/proc/cpuinfofor "fred", but that sounds much more fun! :)
This is a great example of why we need the patchset to clear compiled-out flags.
Quoted 18 lines · Dave Hansen
Quoted 17 lines · Andrew Cooper
Quoted 3 lines
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best.
A reliable way to distinguish IDT and FRED mode is to:
1) Load $3 into %fs (
x86_64) or %gs (i386) (i.e. whichever isn't thread local stoage)
2) execute a breakpoint, ignore the signal
3) Look to see whether %fs/%gs holds 3 or 0IRET has a fun behaviour where it zeroes NULL selectors even if they had a non-zero RPL.
ERETU doesn't do this; Andy Luto and I asked for this minor information leak to be removed, and Intel agreed as it served no purpose anyone could identify.
Quoted 5 lines
Quoted 2 lines
As a consequence, you can use it to determine whether the kernel used IRET or ERET to return back to userspace.
I was thinking of just grepping
/proc/cpuinfofor "fred", but that sounds much more fun! :)
Thank you both for the review and suggestions. The behavioral difference between IRET and ERETU is a more robust way to detect FRED activation than checking CPUID.
How about the following implementation to add a helper function to determine if FRED is enabled at runtime:
static void empty_handler(int sig, siginfo_t *info, void *ctx_void) {
}
static bool is_fred_enabled(void)
{
unsigned short gs_val;
sethandler(SIGTRAP, empty_handler, 0);
/*
* Distinguish IDT and FRED mode by loading GS with a non-zero RPL and
* triggering an exception:
* IDT (IRET) clears RPL bits of NULL selectors.
* FRED (ERETU) preserves them.
*
* If GS is loaded with 3 (Index=0, RPL=3), and we trigger an exception:
* Legacy should restore GS as 0.
* FRED should preserve GS as 3.
*/
asm volatile(
"mov $3, %%ax\n\t"
"mov %%ax, %%gs\n\t"
"int3\n\t"
"mov %%gs, %%ax\n\t"
"mov %%ax, %0\n\t"
: "=r" (gs_val)
:
: "ax", "memory"
);
clearhandler(SIGTRAP);
return gs_val == 3;
}Quoted 34 lines · Yi
Quoted 14 lines · Dave Hansen
Quoted 13 lines · Andrew Cooper
Quoted 3 lines
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best.
A reliable way to distinguish IDT and FRED mode is to:
1) Load $3 into %fs (
x86_64) or %gs (i386) (i.e. whichever isn't thread local stoage)
2) execute a breakpoint, ignore the signal
3) Look to see whether %fs/%gs holds 3 or 0IRET has a fun behaviour where it zeroes NULL selectors even if they had a non-zero RPL.
Quoted 8 lines
Quoted 6 lines
ERETU doesn't do this; Andy Luto and I asked for this minor information leak to be removed, and Intel agreed as it served no purpose anyone could identify.
As a consequence, you can use it to determine whether the kernel used IRET or ERET to return back to userspace.
I was thinking of just grepping
/proc/cpuinfofor "fred", but that sounds much more fun! :)Thank you both for the review and suggestions. The behavioral difference between IRET and ERETU is a more robust way to detect FRED activation than checking CPUID.
How about the following implementation to add a helper function to determine if FRED is enabled at runtime:
static void
empty_handler(int sig, siginfo_t *info, void *ctx_void){
}
Quoted 5 lines
static bool
is_fred_enabled(void){ unsigned short gs_val; sethandler(SIGTRAP, empty_handler, 0);
What about setting SIG_IGN instead?
Quoted 13 lines
/* * Distinguish IDT and FRED mode by loading GS with a non-zero RPL and * triggering an exception: * IDT (IRET) clears RPL bits of NULL selectors. * FRED (ERETU) preserves them. * * If GS is loaded with 3 (Index=0, RPL=3), and we trigger an exception: * Legacy should restore GS as 0. * FRED should preserve GS as 3. */ asm volatile( "mov $3, %%ax\n\t"
Quoted 8 lines
"mov %%ax, %%gs\n\t" "int3\n\t" "mov %%gs, %%ax\n\t" "mov %%ax, %0\n\t" : "=r" (gs_val) : : "ax", "memory" );
asm volatile (
"mov %[rpl3], %%gs\n\t"
"int3\n\t"
"mov %%gs, %[res]"
: [res] "=r" (gs_val)
: [rpl3] "r" (3));
No need for any clobbers. Let the compiler do the hard work for you.
~Andrew
Quoted 35 lines · Andrew Cooper
Quoted 34 lines · Yi
Quoted 14 lines · Dave Hansen
Quoted 13 lines · Andrew Cooper
Quoted 3 lines
First, CPUID doesn't tell you if FRED is in use. Is it even on by default yet? There might not be a better way to do this than checking CPUID, but checking CPUID is imprecise at best.
A reliable way to distinguish IDT and FRED mode is to:
1) Load $3 into %fs (
x86_64) or %gs (i386) (i.e. whichever isn't thread local stoage)
2) execute a breakpoint, ignore the signal
3) Look to see whether %fs/%gs holds 3 or 0IRET has a fun behaviour where it zeroes NULL selectors even if they had a non-zero RPL.
Quoted 8 lines
Quoted 6 lines
ERETU doesn't do this; Andy Luto and I asked for this minor information leak to be removed, and Intel agreed as it served no purpose anyone could identify.
As a consequence, you can use it to determine whether the kernel used IRET or ERET to return back to userspace.
I was thinking of just grepping
/proc/cpuinfofor "fred", but that sounds much more fun! :)Thank you both for the review and suggestions. The behavioral difference between IRET and ERETU is a more robust way to detect FRED activation than checking CPUID.
How about the following implementation to add a helper function to determine if FRED is enabled at runtime:
static void
empty_handler(int sig, siginfo_t *info, void *ctx_void){
}
Quoted 8 lines
Quoted 5 lines
static bool
is_fred_enabled(void){ unsigned short gs_val; sethandler(SIGTRAP, empty_handler, 0);What about setting
SIG_IGNinstead?
I tested setting signal(SIGTRAP, SIG_IGN), but it causes the test to terminate with a trace/breakpoint trap (core dump) when the 'int3' instruction initiates the hardware exception. Therefore, I will keep the empty_handler() implementation to ensure it can safely resume execution.
Quoted 22 lines
Quoted 13 lines
/* * Distinguish IDT and FRED mode by loading GS with a non-zero RPL and * triggering an exception: * IDT (IRET) clears RPL bits of NULL selectors. * FRED (ERETU) preserves them. * * If GS is loaded with 3 (Index=0, RPL=3), and we trigger an exception: * Legacy should restore GS as 0. * FRED should preserve GS as 3. */ asm volatile( "mov $3, %%ax\n\t"Quoted 8 lines
"mov %%ax, %%gs\n\t" "int3\n\t" "mov %%gs, %%ax\n\t" "mov %%ax, %0\n\t" : "=r" (gs_val) : : "ax", "memory" );
Quoted 9 lines
asm volatile (
"mov %[rpl3], %%gs\n\t"
"int3\n\t"
"mov %%gs, %[res]"
: [res] "=r" (gs_val)
: [rpl3] "r" (3));No need for any clobbers. Let the compiler do the hard work for you.
Thank you for the improved assembly block and it's indeed cleaner. I will adopt your suggestion and send out v2.
Regards,
Yi Lai
Quoted 1 line
~Andrew
Quoted 11 lines · On Fri, 20 Mar 2026 14:33:01 +0800 Yi Lai <[email protected]> wrote:
The existing '
sysret_rip' selftest asserts that 'regs->r11==regs->flags'. This check relies on the behavior of the SYSCALL instruction on legacyx86_64, which saves 'RFLAGS' into 'R11'.However, on systems with FRED (Flexible Return and Event Delivery) enabled, instead of using registers, all state is saved onto the stack. Consequently, 'R11' retains its userspace value, causing the assertion to fail.
Fix this by detecting FRED support via CPUID (Leaf
0x7, Subleaf0x1, EAX bit 17) and skipping the register assertion if FRED is present.
For the selftest can't you just allow for R11 being unchanged? That would be much simpler.
It isn't as though the test is doing much more than checking that R11 is corrupted by being overwritten by EFLAGS.
David
Quoted 40 lines
Signed-off-by Yi Lai <[email protected]>--- tools/testing/selftests/x86/sysret_rip.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)tools/testing/selftests/x86/sysret_rip.c +13
-3@@ -21,6 +21,7 @@#include <sys/user.h> #include <sys/mman.h> #include <assert.h> +#include <cpuid.h> #include "helpers.h" @@ -64,9 +65,18 @@ static void sigusr1(int sig, siginfo_t *info, void *ctx_void) ctx->uc_mcontext.gregs[REG_RIP ] = rip; ctx->uc_mcontext.gregs[REG_RCX ] = rip; -/* R11 and EFLAGS should already match. */ -assert (ctx->uc_mcontext.gregs[REG_EFL ] == - ctx->uc_mcontext.gregs[REG_R11 ]); +/* +* SYSCALL works differently on FRED, it does not save RIP and RFLAGS +* to RCX and R11. +*/ +unsigned int eax, ebx, ecx, edx; + +__cpuid_count (0x7 ,0x1 , eax, ebx, ecx, edx); +if (!(eax & (1 <<17 ))) { +/* R11 and EFLAGS should already match. */ +assert (ctx->uc_mcontext.gregs[REG_EFL ] == + ctx->uc_mcontext.gregs[REG_R11 ]); + }sethandler (SIGSEGV , sigsegv_for_sigreturn_test,SA_RESETHAND ); }
H. Peter Anvin
reviewerQuoted 60 lines · David Laight
Quoted 11 lines · On Fri, 20 Mar 2026 14:33:01 +0800 Yi Lai <[email protected]> wrote:
The existing '
sysret_rip' selftest asserts that 'regs->r11==regs->flags'. This check relies on the behavior of the SYSCALL instruction on legacyx86_64, which saves 'RFLAGS' into 'R11'.However, on systems with FRED (Flexible Return and Event Delivery) enabled, instead of using registers, all state is saved onto the stack. Consequently, 'R11' retains its userspace value, causing the assertion to fail.
Fix this by detecting FRED support via CPUID (Leaf
0x7, Subleaf0x1, EAX bit 17) and skipping the register assertion if FRED is present.For the selftest can't you just allow for R11 being unchanged? That would be much simpler.
It isn't as though the test is doing much more than checking that R11 is corrupted by being overwritten by EFLAGS.David
Quoted 38 lines
Signed-off-by Yi Lai <[email protected]>--- tools/testing/selftests/x86/sysret_rip.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)tools/testing/selftests/x86/sysret_rip.c +13
-3@@ -21,6 +21,7 @@#include <sys/user.h> #include <sys/mman.h> #include <assert.h> +#include <cpuid.h> #include "helpers.h" @@ -64,9 +65,18 @@ static void sigusr1(int sig, siginfo_t *info, void *ctx_void) ctx->uc_mcontext.gregs[REG_RIP ] = rip; ctx->uc_mcontext.gregs[REG_RCX ] = rip; -/* R11 and EFLAGS should already match. */ -assert (ctx->uc_mcontext.gregs[REG_EFL ] == - ctx->uc_mcontext.gregs[REG_R11 ]); +/* +* SYSCALL works differently on FRED, it does not save RIP and RFLAGS +* to RCX and R11. +*/ +unsigned int eax, ebx, ecx, edx; + +__cpuid_count (0x7 ,0x1 , eax, ebx, ecx, edx); +if (!(eax & (1 <<17 ))) { +/* R11 and EFLAGS should already match. */ +assert (ctx->uc_mcontext.gregs[REG_EFL ] == + ctx->uc_mcontext.gregs[REG_R11 ]); + }
Quoted 3 lines
tools/testing/selftests/x86/sysret_rip.c static void sigusr1(int sig, siginfo_t *info, void *ctx_void) · line 82
Quoted 2 lines
tools/testing/selftests/x86/sysret_rip.c +0
-0sethandler (SIGSEGV , sigsegv_for_sigreturn_test,SA_RESETHAND ); }
I thought we already made it do that a while ago...