Re: [PATCH v5] x86: use builtins to read eflags

From: Linus Torvalds
Date: Thu Mar 17 2022 - 21:52:09 EST


On Thu, Mar 17, 2022 at 6:21 PM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> Now, compare that to just using inline asm: it's trivial, and we've
> used it basically unchanged for three decades.

Ok, so going _really_ far back, we used to have them literally written out:

__asm__ __volatile__("pushfl ; popl %0 ; cli":"=r" (flags));

in random code, and then in October 1992 switched those nasty things
to instead use

#define cli() __asm__ __volatile__ ("cli"::)

#define save_flags(x) \
__asm__ __volatile__("pushfl ; popl %0":"=r" (x))

and the code was changed to actually use

save_flags(flags);
cli();

instead of that open-coded raw asm.

And the "memory" clobber was added early June, 1993:

#define save_flags(x) \
-__asm__ __volatile__("pushfl ; popl %0":"=r" (x))
+__asm__ __volatile__("pushfl ; popl %0":"=r" (x)::"memory")

so that thing really has existed in pretty much that exact form for
almost 30 years.

There's been tweaks since (the "=r" became "=g" before becoming "=rm",
comments have been added, "pushfl" became just "pushf" with x86-64,
and the thing has moved around and is now called "native_save_fl()" in
a completely different header file etc)

But at no point was it ever as buggy as the actual gcc intrinsic seems
to be today, nor have we needed to check for compiler versions etc.

Linus