Skip to contentSkip to conversation

x86/asm: Use 32bit xor to clear registers

PATCH Applied Uros Bizjak 4 participants 7 messages Jan 24, 2024 – Mar 1, 2024 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/asm: Use 32bit xor to clear registers

x86_64 zero extends 32bit operations, so for 64bit operands, XORL r32,r32 is functionally equal to XORQ r64,r64, but avoids a REX prefix byte when legacy registers are used.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/kernel/head_64.S         | 6 +++---
 arch/x86/kernel/sev_verify_cbit.S | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/head_64.S +3 -3
index cc3a81852e4a..ed287170c126 100644
@@ -171,7 +171,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 	ANNOTATE_NOENDBR
 
 	/* Clear %R15 which holds the boot_params pointer on the boot CPU */
-	xorq	%r15, %r15
+	xorl	%r15d, %r15d
 
 	/*
 	 * Retrieve the modifier (SME encryption mask if SME is active) to be
@@ -180,7 +180,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 #ifdef CONFIG_AMD_MEM_ENCRYPT
 	movq	sme_me_mask, %rax
 #else
-	xorq	%rax, %rax
+	xorl	%eax, %eax
 #endif
 
 	/* Form the CR3 value being sure to include the CR3 modifier */
@@ -297,7 +297,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 
 .Llookup_AP:
 	/* EAX contains the APIC ID of the current CPU */
-	xorq	%rcx, %rcx
+	xorl	%ecx, %ecx
 	leaq	cpuid_to_apicid(%rip), %rbx
 
 .Lfind_cpunr:
arch/x86/kernel/sev_verify_cbit.S +1 -1
index 3355e27c69eb..1ab65f6c6ae7 100644
@@ -77,7 +77,7 @@ SYM_FUNC_START(sev_verify_cbit)
 	 * The check failed, prevent any forward progress to prevent ROP
 	 * attacks, invalidate the stack and go into a hlt loop.
 	 */
-	xorq	%rsp, %rsp
+	xorl	%esp, %esp
 	subq	$0x1000, %rsp
 2:	hlt
 	jmp 2b
Signature · 1 line
2.31.1
merge bot Applied
#1 lore raw

[tip: x86/boot] x86/boot: Use 32-bit XOR to clear registers

The following commit has been merged into the x86/boot branch of tip:

The following commit has been merged into the x86/boot branch of tip:

Commit-ID:     721f791ce1cddfa5f2bf524ac14741bfa0f72697
Gitweb:        https://git.kernel.org/tip/721f791ce1cddfa5f2bf524ac14741bfa0f72697
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 24 Jan 2024 11:38:59 +01:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 01 Mar 2024 12:47:37 +01:00

x86/boot: Use 32-bit XOR to clear registers

x86_64 zero extends 32-bit operations, so for 64-bit operands, XORL r32,r32 is functionally equal to XORQ r64,r64, but avoids a REX prefix byte when legacy registers are used.

Slightly smaller code generated, no change in functionality.

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/kernel/head_64.S         | 6 +++---
 arch/x86/kernel/sev_verify_cbit.S | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/head_64.S +3 -3
index d295bf6..86136a7 100644
@@ -169,7 +169,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 	ANNOTATE_NOENDBR
 
 	/* Clear %R15 which holds the boot_params pointer on the boot CPU */
-	xorq	%r15, %r15
+	xorl	%r15d, %r15d
 
 	/*
 	 * Retrieve the modifier (SME encryption mask if SME is active) to be
@@ -178,7 +178,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 #ifdef CONFIG_AMD_MEM_ENCRYPT
 	movq	sme_me_mask, %rax
 #else
-	xorq	%rax, %rax
+	xorl	%eax, %eax
 #endif
 
 	/* Form the CR3 value being sure to include the CR3 modifier */
@@ -295,7 +295,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 
 .Llookup_AP:
 	/* EAX contains the APIC ID of the current CPU */
-	xorq	%rcx, %rcx
+	xorl	%ecx, %ecx
 	leaq	cpuid_to_apicid(%rip), %rbx
 
 .Lfind_cpunr:
arch/x86/kernel/sev_verify_cbit.S +1 -1
index 3355e27..1ab65f6 100644
@@ -77,7 +77,7 @@ SYM_FUNC_START(sev_verify_cbit)
 	 * The check failed, prevent any forward progress to prevent ROP
 	 * attacks, invalidate the stack and go into a hlt loop.
 	 */
-	xorq	%rsp, %rsp
+	xorl	%esp, %esp
 	subq	$0x1000, %rsp
 2:	hlt
 	jmp 2b
#2 lore raw

Re: [tip: x86/boot] x86/boot: Use 32-bit XOR to clear registers

Quoted 10 lines · On Fri, 1 Mar 2024 at 13:39, tip-bot2 for Uros Bizjak <[email protected]>…

The following commit has been merged into the x86/boot branch of tip:

Commit-ID:     721f791ce1cddfa5f2bf524ac14741bfa0f72697
Gitweb:        https://git.kernel.org/tip/721f791ce1cddfa5f2bf524ac14741bfa0f72697
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 24 Jan 2024 11:38:59 +01:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 01 Mar 2024 12:47:37 +01:00
Quoted 6 lines

x86/boot: Use 32-bit XOR to clear registers

x86_64 zero extends 32-bit operations, so for 64-bit operands, XORL r32,r32 is functionally equal to XORQ r64,r64, but avoids a REX prefix byte when legacy registers are used.

... and so this change is pointless churn when not using legacy registers, right?

Quoted 22 lines

Slightly smaller code generated, no change in functionality.

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/kernel/head_64.S         | 6 +++---
 arch/x86/kernel/sev_verify_cbit.S | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/head_64.S +0 -0
index d295bf6..86136a7 100644

arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 173

Quoted 6 lines
arch/x86/kernel/head_64.S +1 -1
@@ -169,7 +169,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
        ANNOTATE_NOENDBR
 
        /* Clear %R15 which holds the boot_params pointer on the boot CPU */
-       xorq    %r15, %r15
+       xorl    %r15d, %r15d
 
   0: 4d 31 ff              xor    %r15,%r15
   3: 45 31 ff              xor    %r15d,%r15d

arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 183

Quoted 10 lines
arch/x86/kernel/head_64.S +1 -1
        /*
         * Retrieve the modifier (SME encryption mask if SME is active) to be
@@ -178,7 +178,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 #ifdef CONFIG_AMD_MEM_ENCRYPT
        movq    sme_me_mask, %rax
 #else
-       xorq    %rax, %rax
+       xorl    %eax, %eax
 #endif
 

This conflicts with my RIP-relative boot cleanup series.

Quoted 23 lines
arch/x86/kernel/head_64.S +1 -1
        /* Form the CR3 value being sure to include the CR3 modifier */
@@ -295,7 +295,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 
 .Llookup_AP:
        /* EAX contains the APIC ID of the current CPU */
-       xorq    %rcx, %rcx
+       xorl    %ecx, %ecx
        leaq    cpuid_to_apicid(%rip), %rbx
 
 .Lfind_cpunr:
arch/x86/kernel/sev_verify_cbit.S +1 -1
index 3355e27..1ab65f6 100644
@@ -77,7 +77,7 @@ SYM_FUNC_START(sev_verify_cbit)
         * The check failed, prevent any forward progress to prevent ROP
         * attacks, invalidate the stack and go into a hlt loop.
         */
-       xorq    %rsp, %rsp
+       xorl    %esp, %esp
        subq    $0x1000, %rsp
 2:     hlt
        jmp 2b
#3 lore raw

Re: [tip: x86/boot] x86/boot: Use 32-bit XOR to clear registers

Quoted 15 lines · Ard Biesheuvel
Quoted 12 lines · On Fri, 1 Mar 2024 at 13:39, tip-bot2 for Uros Bizjak <[email protected]>…

The following commit has been merged into the x86/boot branch of tip:

Commit-ID:     721f791ce1cddfa5f2bf524ac14741bfa0f72697
Gitweb:        https://git.kernel.org/tip/721f791ce1cddfa5f2bf524ac14741bfa0f72697
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 24 Jan 2024 11:38:59 +01:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 01 Mar 2024 12:47:37 +01:00

x86/boot: Use 32-bit XOR to clear registers

Quoted 7 lines
Quoted 4 lines

x86_64 zero extends 32-bit operations, so for 64-bit operands, XORL r32,r32 is functionally equal to XORQ r64,r64, but avoids a REX prefix byte when legacy registers are used.

... and so this change is pointless churn when not using legacy registers, right?

Although there is no code size change with REX registers, it would look weird to use XORQ with REX registers and XORL with legacy regs. Please see arch/x86/kvm/{vmx,svm}/vmenter.S where this approach is also used.

Uros.

Quoted 69 lines
Quoted 22 lines

Slightly smaller code generated, no change in functionality.

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Ard Biesheuvel <[email protected]>
---
 arch/x86/kernel/head_64.S         | 6 +++---
 arch/x86/kernel/sev_verify_cbit.S | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/head_64.S +0 -0
index d295bf6..86136a7 100644

arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 173

Quoted 6 lines
arch/x86/kernel/head_64.S +1 -1
@@ -169,7 +169,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
        ANNOTATE_NOENDBR
 
        /* Clear %R15 which holds the boot_params pointer on the boot CPU */
-       xorq    %r15, %r15
+       xorl    %r15d, %r15d
 
   0: 4d 31 ff              xor    %r15,%r15
   3: 45 31 ff              xor    %r15d,%r15d

arch/x86/kernel/head_64.S SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) · line 183

Quoted 10 lines
arch/x86/kernel/head_64.S +1 -1
        /*
         * Retrieve the modifier (SME encryption mask if SME is active) to be
@@ -178,7 +178,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 #ifdef CONFIG_AMD_MEM_ENCRYPT
        movq    sme_me_mask, %rax
 #else
-       xorq    %rax, %rax
+       xorl    %eax, %eax
 #endif
 

This conflicts with my RIP-relative boot cleanup series.

Quoted 23 lines
arch/x86/kernel/head_64.S +1 -1
        /* Form the CR3 value being sure to include the CR3 modifier */
@@ -295,7 +295,7 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
 
 .Llookup_AP:
        /* EAX contains the APIC ID of the current CPU */
-       xorq    %rcx, %rcx
+       xorl    %ecx, %ecx
        leaq    cpuid_to_apicid(%rip), %rbx
 
 .Lfind_cpunr:
arch/x86/kernel/sev_verify_cbit.S +1 -1
index 3355e27..1ab65f6 100644
@@ -77,7 +77,7 @@ SYM_FUNC_START(sev_verify_cbit)
         * The check failed, prevent any forward progress to prevent ROP
         * attacks, invalidate the stack and go into a hlt loop.
         */
-       xorq    %rsp, %rsp
+       xorl    %esp, %esp
        subq    $0x1000, %rsp
 2:     hlt
        jmp 2b
#4 lore raw

Re: [tip: x86/boot] x86/boot: Use 32-bit XOR to clear registers

Quoted 22 lines · Uros Bizjak
Quoted 20 lines · Ard Biesheuvel
Quoted 16 lines · On Fri, 1 Mar 2024 at 13:39, tip-bot2 for Uros Bizjak <[email protected]>…

The following commit has been merged into the x86/boot branch of tip:

Commit-ID:     721f791ce1cddfa5f2bf524ac14741bfa0f72697
Gitweb:        https://git.kernel.org/tip/721f791ce1cddfa5f2bf524ac14741bfa0f72697
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 24 Jan 2024 11:38:59 +01:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 01 Mar 2024 12:47:37 +01:00

x86/boot: Use 32-bit XOR to clear registers

x86_64 zero extends 32-bit operations, so for 64-bit operands, XORL r32,r32 is functionally equal to XORQ r64,r64, but avoids a REX prefix byte when legacy registers are used.

Quoted 5 lines
Quoted 2 lines

... and so this change is pointless churn when not using legacy registers, right?

Although there is no code size change with REX registers, it would look weird to use XORQ with REX registers and XORL with legacy regs.

You are changing an isolated occurrence of XORQ into XORL on the basis that XORQ 'looks weird', and would produce a longer opcode if the occurrence in question would be using a different register than it actually uses.

Apologies for the bluntness, but in my book, this really falls firmly into the 'pointless churn' territory. The startup code is not performance critical, neither in terms of size nor in speed, and so I'd prefer to avoid these kinds of changes. Just my 2c, though - Ingo has already merged the patch.

#5 lore raw

Re: [tip: x86/boot] x86/boot: Use 32-bit XOR to clear registers

Quoted 28 lines · Ard Biesheuvel
Quoted 14 lines · Uros Bizjak
Quoted 13 lines · Ard Biesheuvel
Quoted 11 lines · On Fri, 1 Mar 2024 at 13:39, tip-bot2 for Uros Bizjak <[email protected]>…

The following commit has been merged into the x86/boot branch of tip:

Commit-ID:     721f791ce1cddfa5f2bf524ac14741bfa0f72697
Gitweb:        https://git.kernel.org/tip/721f791ce1cddfa5f2bf524ac14741bfa0f72697
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 24 Jan 2024 11:38:59 +01:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 01 Mar 2024 12:47:37 +01:00

x86/boot: Use 32-bit XOR to clear registers

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

x86_64 zero extends 32-bit operations, so for 64-bit operands, XORL r32,r32 is functionally equal to XORQ r64,r64, but avoids a REX prefix byte when legacy registers are used.

... and so this change is pointless churn when not using legacy registers, right?

Although there is no code size change with REX registers, it would look weird to use XORQ with REX registers and XORL with legacy regs.

You are changing an isolated occurrence of XORQ into XORL on the basis that XORQ 'looks weird', and would produce a longer opcode if the occurrence in question would be using a different register than it actually uses.

Quoted 5 lines

Apologies for the bluntness, but in my book, this really falls firmly into the 'pointless churn' territory. The startup code is not performance critical, neither in terms of size nor in speed, and so I'd prefer to avoid these kinds of changes. Just my 2c, though - Ingo has already merged the patch.

Without trying to get into an argument here...

The better reason is that Silvermont Atoms don't recognise the 64bit form as a zeroing idiom.  They only recognise the 32bit form of the idiom.

Therefore in fastpaths it is (marginally) important to xorl %r15d, %r15d rather than xorq %r15, %r15.

But this instance is not a fastpath, and it also doesn't save any encoding space, so I'm not sure it was really worth changing.

~Andrew

#6 lore raw

Re: [tip: x86/boot] x86/boot: Use 32-bit XOR to clear registers

Quoted 30 lines · Andrew Cooper
Quoted 28 lines · Ard Biesheuvel
Quoted 14 lines · Uros Bizjak
Quoted 13 lines · Ard Biesheuvel
Quoted 11 lines · On Fri, 1 Mar 2024 at 13:39, tip-bot2 for Uros Bizjak <[email protected]>…

The following commit has been merged into the x86/boot branch of tip:

Commit-ID:     721f791ce1cddfa5f2bf524ac14741bfa0f72697
Gitweb:        https://git.kernel.org/tip/721f791ce1cddfa5f2bf524ac14741bfa0f72697
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 24 Jan 2024 11:38:59 +01:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 01 Mar 2024 12:47:37 +01:00

x86/boot: Use 32-bit XOR to clear registers

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

x86_64 zero extends 32-bit operations, so for 64-bit operands, XORL r32,r32 is functionally equal to XORQ r64,r64, but avoids a REX prefix byte when legacy registers are used.

... and so this change is pointless churn when not using legacy registers, right?

Although there is no code size change with REX registers, it would look weird to use XORQ with REX registers and XORL with legacy regs.

You are changing an isolated occurrence of XORQ into XORL on the basis that XORQ 'looks weird', and would produce a longer opcode if the occurrence in question would be using a different register than it actually uses.

Quoted 8 lines
Quoted 5 lines

Apologies for the bluntness, but in my book, this really falls firmly into the 'pointless churn' territory. The startup code is not performance critical, neither in terms of size nor in speed, and so I'd prefer to avoid these kinds of changes. Just my 2c, though - Ingo has already merged the patch.

Without trying to get into an argument here...

No worries, we're all friends here :-)

Quoted 9 lines

The better reason is that Silvermont Atoms don't recognise the 64bit form as a zeroing idiom. They only recognise the 32bit form of the idiom.

Therefore in fastpaths it is (marginally) important to xorl %r15d, %r15d rather than xorq %r15, %r15.

But this instance is not a fastpath, and it also doesn't save any encoding space, so I'm not sure it was really worth changing.

Yeah, that is my point, really. But let's move on. And apologies to Uros for the tone - it didn't sound as grumpy in my head when I typed it as it does now reading back the thread.