Re: [PATCH v5] x86: use builtins to read eflags
From: Andrew Cooper
Date: Fri Mar 18 2022 - 17:48:35 EST
On 18/03/2022 18:19, Linus Torvalds wrote:
> Side note and kind of related: we do have this in the kernel:
>
> register unsigned long current_stack_pointer asm(_ASM_SP);
> #define ASM_CALL_CONSTRAINT "+r" (current_stack_pointer)
>
> which *might* also solve the redzoning issue.
Sadly not. https://godbolt.org/z/cGx74sKE3
Given:
int pushf(void)
{
unsigned long register sp asm("rsp");
unsigned long x, y;
asm ("movq $1, %0" : "=m" (x));
asm ("pushf\n\tpop %0": "=r" (y), "+r" (sp));
return x + y;
}
the generated code is:
pushf:
movq $1, -8(%rsp)
pushf
pop %rax
addl -8(%rsp), %eax
ret
so the rsp clobber doesn't prevent the push/pop pair from trashing x in
the red zone.
The builtin does cause a stack frame to be fully set up, and x to be
allocated within it, rather than in the red zone.
Experimenting with rsp clobbers leads to https://godbolt.org/z/s9scxre19
which demonstrates (for gcc at least) it does change the position of
when a stack frame gets set up (in the case that there is a path not
otherwise needing a stack frame) but doesn't unilaterally force a stack
frame to be set up.
As such, I'm not sure how current_stack_pointer can work as intended in
all cases...
~Andrew