__arch_hweight32/64 x86
Subsystems (2)
Hello kernel developers,
I recently stumbled over a bug in the inline assembly of '__arch_hweight32' and '__arch_hweight64' in 'arch/x86/include/asm/arch_hweight.h' (present in 6.4 as well):
! asm (ALTERNATIVE("call __sw_hweight64", "popcntq %1, %0", ! X86_FEATURE_POPCNT)
! : "="REG_OUT (res)
! : REG_IN (w));
The 'ALTERNATIVE' macro checks for the popcnt feature. In case this fails the '__sw_hweight' C-function is called from inline assembly with rax and rdi as intput/output operands. However, the code does not contain a clobber list of any callee safe registers that might be touched by the '__sw_height64' C-function. Therefore, these registers will not be restored upon function return by the compiler. This leads to varying exceptions/bad behavior caused by the thus corrupted registers later on.
Kind regards,
Sebastian
Signature · 7 lines
Sebastian Sumpf Genode Labs http://www.genode-labs.com · http://genode.org Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth
Borislav Petkov
maintainerHello,
Quoted 5 lines · Sebastian Sumpf
The 'ALTERNATIVE' macro checks for the popcnt feature. In case this fails the '
__sw_hweight' C-function is called from inline assembly with rax and rdi as intput/output operands. However, the code does not contain a clobber list of any callee safe registers that might be touched by the '__sw_height64' C-function.
Which registers are those? Can you be more specific?
Quoted 4 lines
Therefore, these registers will not be restored
upon function return by the compiler. This leads to varying exceptions/bad behavior caused by the thus corrupted registers later on.
How do I reproduce what you're observing so that I can take a look?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteHello Borislav,
Quoted 10 lines · Borislav Petkov
Hello,
Quoted 7 lines · Sebastian Sumpf
The 'ALTERNATIVE' macro checks for the popcnt feature. In case this fails the '
__sw_hweight' C-function is called from inline assembly with rax and rdi as intput/output operands. However, the code does not contain a clobber list of any callee safe registers that might be touched by the '__sw_height64' C-function.Which registers are those? Can you be more specific?
Registers that are free to use by a called C-function are: rcx, rdx, rsi, rdi, and r8-r11 for x86_64. See the "System V Application Binary Interface AMD64 Architecture Processor Supplement" [1] page 21. x86_32 I would have to look up.
Quoted 6 lines
Quoted 4 lines
Therefore, these registers will not be restored
upon function return by the compiler. This leads to varying exceptions/bad behavior caused by the thus corrupted registers later on.How do I reproduce what you're observing so that I can take a look?
This is hard to tell, I would disable the " X86_FEATURE_POPCNT" feature and use the ' CONFIG_ARCH_HAS_FAST_MULTIPLIER' option in order to use the multiplier implementation in '__sw_hweight64' in 'lib/hweight.c' At least that is what triggered it here.
Regards,
Sebastian
[1] http://www.uclibc.org/docs/psABI-x86_64.pdf
Signature · 7 lines
Sebastian Sumpf Genode Labs http://www.genode-labs.com · http://genode.org Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth
Quoted 6 lines · Sebastian Sumpf
Quoted 6 lines
Quoted 5 lines
Therefore, these registers will not be restored
upon function return by the compiler. This leads to varying exceptions/bad behavior caused by the thus corrupted registers later on.
Quoted 6 lines
Quoted 1 line
How do I reproduce what you're observing so that I can take a look?
This is hard to tell, I would disable the "
X86_FEATURE_POPCNT" feature and use the 'CONFIG_ARCH_HAS_FAST_MULTIPLIER' option in order to use the multiplier implementation in '__sw_hweight64' in 'lib/hweight.c' At least that is what triggered it here.
Looks like you'd also have to be using UML:
$ grep hweight lib/Makefile obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o $ grep -r GENERIC_HWEIGHT arch/x86 arch/x86/um/Kconfig:config GENERIC_HWEIGHT
I'm not even sure that UML needs GENERIC_HWEIGHT. From a quick glance, it looks like x86 used to use GENERIC_HWEIGHT, but got arch-specific versions later. UML just never moved over to the arch-specific versions.
I _think_ the attached patch might just fix the problems with the C version and bring the x86/UML implementation back in line with the rest of x86.
Thoughts?
arch/x86/um/Kconfig +0 -3
@@ -44,6 +44,3 @@ config ARCH_HAS_SC_SIGNALS config ARCH_REUSE_HOST_VSYSCALL_AREA def_bool !64BIT - -config GENERIC_HWEIGHT - def_bool y
Borislav Petkov
maintainerQuoted 3 lines · Dave Hansen
I'm not even sure that UML needs
GENERIC_HWEIGHT. From a quick glance, it looks like x86 used to useGENERIC_HWEIGHT, but got arch-specific versions later. UML just never moved over to the arch-specific versions.
Thanks - that could very well be the explanation.
That bug report made me blink a couple of times since I did take extra precaution to not clobber regs in arch/x86/lib/hweight.S as this was part of the whole pain back then with calling a function from asm where gcc doesn't even know we're calling a function, see:
f5967101e9de ("x86/hweight: Get rid of the special calling convention")
Quoted 11 lines
I _think_ the attached patch might just fix the problems with the C version and bring the x86/UML implementation back in line with the rest of x86.
Thoughts?
arch/x86/um/Kconfig +0
-0
arch/x86/um/Kconfig config ARCH_HAS_SC_SIGNALS · line 49
Quoted 6 lines
arch/x86/um/Kconfig +0
-3@@ -44,6 +44,3 @@ config ARCH_HAS_SC_SIGNALS config ARCH_REUSE_HOST_VSYSCALL_AREA def_bool !64BIT - -config GENERIC_HWEIGHT - def_bool y
Yeah, we should do it. UML should not do anything different wrt calling conventions so it should be able to handle the arch/x86/lib/hweight.S versions just fine.
Richi?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 26 lines · Borislav Petkov
Quoted 3 lines · Dave Hansen
I'm not even sure that UML needs
GENERIC_HWEIGHT. From a quick glance, it looks like x86 used to useGENERIC_HWEIGHT, but got arch-specific versions later. UML just never moved over to the arch-specific versions.Thanks - that could very well be the explanation.
That bug report made me blink a couple of times since I did take extra precaution to not clobber regs in
arch/x86/lib/hweight.Sas this was part of the whole pain back then with calling a function from asm where gcc doesn't even know we're calling a function, see:
f5967101e9de("x86/hweight: Get rid of the special calling convention")Quoted 12 lines
I _think_ the attached patch might just fix the problems with the C version and bring the x86/UML implementation back in line with the rest of x86.
Thoughts?
arch/x86/um/Kconfig +0
-0@@ -44,6 +44,3 @@ config ARCH_HAS_SC_SIGNALS
Quoted 9 lines
arch/x86/um/Kconfig config ARCH_HAS_SC_SIGNALS · line 49
Quoted 5 lines
arch/x86/um/Kconfig +0
-3config ARCH_REUSE_HOST_VSYSCALL_AREA def_bool !64BIT - -config GENERIC_HWEIGHT - def_bool yYeah, we should do it. UML should not do anything different wrt calling conventions so it should be able to handle the
arch/x86/lib/hweight.Sversions just fine.
Thank you for the quick resolution! It fixes the problem for me and sorry for not explaining well enough.
Quoted 2 lines
Richi?
Signature · 7 lines
Sebastian Sumpf Genode Labs http://www.genode-labs.com · http://genode.org Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth
From: Borislav Petkov
Quoted 7 lines
Sent: 27 June 2023 05:26
Quoted 3 lines · Dave Hansen
I'm not even sure that UML needs
GENERIC_HWEIGHT. From a quick glance, it looks like x86 used to useGENERIC_HWEIGHT, but got arch-specific versions later. UML just never moved over to the arch-specific versions.
Quoted 6 lines
Thanks - that could very well be the explanation.
That bug report made me blink a couple of times since I did take extra precaution to not clobber regs in
arch/x86/lib/hweight.Sas this was part of the whole pain back then with calling a function from asm where gcc doesn't even know we're calling a function, see:
Perhaps the asm called function should use a different global label than the one in the C file.
Then it wouldn't be as fragile.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)