x86/kexec: Merge x86_32 and x86_64 code using macros from asm.h
PATCH v2 1/1 x86/kexec: Merge x86_32 and x86_64 code using macros from asm.h
Merge common x86_32 and x86_64 code in crash_setup_regs() using macros from asm/asm.h.
The compiled object files before and after the patch are unchanged.
---
v2: Remove size suffixes from instructions.
--- arch/x86/include/asm/kexec.h | 58 ++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 33 deletions(-)
arch/x86/include/asm/kexec.h +25 -33
@@ -18,6 +18,7 @@#include <linux/string.h> #include <linux/kernel.h> +#include <asm/asm.h> #include <asm/page.h> #include <asm/ptrace.h>
@@ -71,41 +72,32 @@ static inline void crash_setup_regs(struct pt_regs *newregs,if (oldregs) {memcpy (newregs, oldregs,sizeof (*newregs)); }else { + asmvolatile ("mov %%" _ASM_BX",%0" :"=m" (newregs->bx)); + asmvolatile ("mov %%" _ASM_CX",%0" :"=m" (newregs->cx)); + asmvolatile ("mov %%" _ASM_DX",%0" :"=m" (newregs->dx)); + asmvolatile ("mov %%" _ASM_SI",%0" :"=m" (newregs->si)); + asmvolatile ("mov %%" _ASM_DI",%0" :"=m" (newregs->di)); + asmvolatile ("mov %%" _ASM_BP",%0" :"=m" (newregs->bp)); + asmvolatile ("mov %%" _ASM_AX",%0" :"=m" (newregs->ax)); + asmvolatile ("mov %%" _ASM_SP",%0" :"=m" (newregs->sp)); +#ifdef CONFIG_X86_64 + asmvolatile ("mov %%r8,%0" :"=m" (newregs->r8)); + asmvolatile ("mov %%r9,%0" :"=m" (newregs->r9)); + asmvolatile ("mov %%r10,%0" :"=m" (newregs->r10)); + asmvolatile ("mov %%r11,%0" :"=m" (newregs->r11)); + asmvolatile ("mov %%r12,%0" :"=m" (newregs->r12)); + asmvolatile ("mov %%r13,%0" :"=m" (newregs->r13)); + asmvolatile ("mov %%r14,%0" :"=m" (newregs->r14)); + asmvolatile ("mov %%r15,%0" :"=m" (newregs->r15)); +#endif + asmvolatile ("mov %%ss,%k0" :"=a" (newregs->ss)); + asmvolatile ("mov %%cs,%k0" :"=a" (newregs->cs));#ifdef CONFIG_X86_32 - asmvolatile ("movl %%ebx,%0" :"=m" (newregs->bx)); - asmvolatile ("movl %%ecx,%0" :"=m" (newregs->cx)); - asmvolatile ("movl %%edx,%0" :"=m" (newregs->dx)); - asmvolatile ("movl %%esi,%0" :"=m" (newregs->si)); - asmvolatile ("movl %%edi,%0" :"=m" (newregs->di)); - asmvolatile ("movl %%ebp,%0" :"=m" (newregs->bp)); - asmvolatile ("movl %%eax,%0" :"=m" (newregs->ax)); - asmvolatile ("movl %%esp,%0" :"=m" (newregs->sp)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("movl %%ds, %%eax;" :"=a" (newregs->ds)); - asmvolatile ("movl %%es, %%eax;" :"=a" (newregs->es)); - asmvolatile ("pushfl; popl %0" :"=m" (newregs->flags)); -#else - asmvolatile ("movq %%rbx,%0" :"=m" (newregs->bx)); - asmvolatile ("movq %%rcx,%0" :"=m" (newregs->cx)); - asmvolatile ("movq %%rdx,%0" :"=m" (newregs->dx)); - asmvolatile ("movq %%rsi,%0" :"=m" (newregs->si)); - asmvolatile ("movq %%rdi,%0" :"=m" (newregs->di)); - asmvolatile ("movq %%rbp,%0" :"=m" (newregs->bp)); - asmvolatile ("movq %%rax,%0" :"=m" (newregs->ax)); - asmvolatile ("movq %%rsp,%0" :"=m" (newregs->sp)); - asmvolatile ("movq %%r8,%0" :"=m" (newregs->r8)); - asmvolatile ("movq %%r9,%0" :"=m" (newregs->r9)); - asmvolatile ("movq %%r10,%0" :"=m" (newregs->r10)); - asmvolatile ("movq %%r11,%0" :"=m" (newregs->r11)); - asmvolatile ("movq %%r12,%0" :"=m" (newregs->r12)); - asmvolatile ("movq %%r13,%0" :"=m" (newregs->r13)); - asmvolatile ("movq %%r14,%0" :"=m" (newregs->r14)); - asmvolatile ("movq %%r15,%0" :"=m" (newregs->r15)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("pushfq; popq %0" :"=m" (newregs->flags)); + asmvolatile ("mov %%ds,%k0" :"=a" (newregs->ds)); + asmvolatile ("mov %%es,%k0" :"=a" (newregs->es));#endif + asmvolatile ("pushf\n\t" +"pop %0" :"=m" (newregs->flags)); newregs->ip = _THIS_IP_; } }
Signature · 1 line
2.48.1
[tip: x86/asm] x86/kexec: Merge x86_32 and x86_64 code using macros from <asm/asm.h>
The following commit has been merged into the x86/asm branch of tip:
The following commit has been merged into the x86/asm branch of tip:
Commit-ID: aa3942d4d12ef57f031faa2772fe410c24191e36 Gitweb: https://git.kernel.org/tip/aa3942d4d12ef57f031faa2772fe410c24191e36 Author: Uros Bizjak <[email protected]> AuthorDate: Thu, 06 Mar 2025 15:52:11 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 06 Mar 2025 22:04:48 +01:00
x86/kexec: Merge x86_32 and x86_64 code using macros from <asm/asm.h>
Merge common x86_32 and x86_64 code in crash_setup_regs() using macros from <asm/asm.h>.
The compiled object files before and after the patch are unchanged.
--- arch/x86/include/asm/kexec.h | 58 +++++++++++++++-------------------- 1 file changed, 25 insertions(+), 33 deletions(-)
arch/x86/include/asm/kexec.h +25 -33
@@ -18,6 +18,7 @@#include <linux/string.h> #include <linux/kernel.h> +#include <asm/asm.h> #include <asm/page.h> #include <asm/ptrace.h>
@@ -71,41 +72,32 @@ static inline void crash_setup_regs(struct pt_regs *newregs,if (oldregs) {memcpy (newregs, oldregs,sizeof (*newregs)); }else { + asmvolatile ("mov %%" _ASM_BX",%0" :"=m" (newregs->bx)); + asmvolatile ("mov %%" _ASM_CX",%0" :"=m" (newregs->cx)); + asmvolatile ("mov %%" _ASM_DX",%0" :"=m" (newregs->dx)); + asmvolatile ("mov %%" _ASM_SI",%0" :"=m" (newregs->si)); + asmvolatile ("mov %%" _ASM_DI",%0" :"=m" (newregs->di)); + asmvolatile ("mov %%" _ASM_BP",%0" :"=m" (newregs->bp)); + asmvolatile ("mov %%" _ASM_AX",%0" :"=m" (newregs->ax)); + asmvolatile ("mov %%" _ASM_SP",%0" :"=m" (newregs->sp)); +#ifdef CONFIG_X86_64 + asmvolatile ("mov %%r8,%0" :"=m" (newregs->r8)); + asmvolatile ("mov %%r9,%0" :"=m" (newregs->r9)); + asmvolatile ("mov %%r10,%0" :"=m" (newregs->r10)); + asmvolatile ("mov %%r11,%0" :"=m" (newregs->r11)); + asmvolatile ("mov %%r12,%0" :"=m" (newregs->r12)); + asmvolatile ("mov %%r13,%0" :"=m" (newregs->r13)); + asmvolatile ("mov %%r14,%0" :"=m" (newregs->r14)); + asmvolatile ("mov %%r15,%0" :"=m" (newregs->r15)); +#endif + asmvolatile ("mov %%ss,%k0" :"=a" (newregs->ss)); + asmvolatile ("mov %%cs,%k0" :"=a" (newregs->cs));#ifdef CONFIG_X86_32 - asmvolatile ("movl %%ebx,%0" :"=m" (newregs->bx)); - asmvolatile ("movl %%ecx,%0" :"=m" (newregs->cx)); - asmvolatile ("movl %%edx,%0" :"=m" (newregs->dx)); - asmvolatile ("movl %%esi,%0" :"=m" (newregs->si)); - asmvolatile ("movl %%edi,%0" :"=m" (newregs->di)); - asmvolatile ("movl %%ebp,%0" :"=m" (newregs->bp)); - asmvolatile ("movl %%eax,%0" :"=m" (newregs->ax)); - asmvolatile ("movl %%esp,%0" :"=m" (newregs->sp)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("movl %%ds, %%eax;" :"=a" (newregs->ds)); - asmvolatile ("movl %%es, %%eax;" :"=a" (newregs->es)); - asmvolatile ("pushfl; popl %0" :"=m" (newregs->flags)); -#else - asmvolatile ("movq %%rbx,%0" :"=m" (newregs->bx)); - asmvolatile ("movq %%rcx,%0" :"=m" (newregs->cx)); - asmvolatile ("movq %%rdx,%0" :"=m" (newregs->dx)); - asmvolatile ("movq %%rsi,%0" :"=m" (newregs->si)); - asmvolatile ("movq %%rdi,%0" :"=m" (newregs->di)); - asmvolatile ("movq %%rbp,%0" :"=m" (newregs->bp)); - asmvolatile ("movq %%rax,%0" :"=m" (newregs->ax)); - asmvolatile ("movq %%rsp,%0" :"=m" (newregs->sp)); - asmvolatile ("movq %%r8,%0" :"=m" (newregs->r8)); - asmvolatile ("movq %%r9,%0" :"=m" (newregs->r9)); - asmvolatile ("movq %%r10,%0" :"=m" (newregs->r10)); - asmvolatile ("movq %%r11,%0" :"=m" (newregs->r11)); - asmvolatile ("movq %%r12,%0" :"=m" (newregs->r12)); - asmvolatile ("movq %%r13,%0" :"=m" (newregs->r13)); - asmvolatile ("movq %%r14,%0" :"=m" (newregs->r14)); - asmvolatile ("movq %%r15,%0" :"=m" (newregs->r15)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("pushfq; popq %0" :"=m" (newregs->flags)); + asmvolatile ("mov %%ds,%k0" :"=a" (newregs->ds)); + asmvolatile ("mov %%es,%k0" :"=a" (newregs->es));#endif + asmvolatile ("pushf\n\t" +"pop %0" :"=m" (newregs->flags)); newregs->ip = _THIS_IP_; } }
H. Peter Anvin
reviewerRe: [tip: x86/asm] x86/kexec: Merge x86_32 and x86_64 code using macros from <asm/asm.h>
Quoted 97 lines · On March 6, 2025 1:33:43 PM PST, tip-bot2 for Uros Bizjak…
The following commit has been merged into the x86/asm branch of tip:
Commit-ID: aa3942d4d12ef57f031faa2772fe410c24191e36 Gitweb: https://git.kernel.org/tip/aa3942d4d12ef57f031faa2772fe410c24191e36 Author: Uros Bizjak <[email protected]> AuthorDate: Thu, 06 Mar 2025 15:52:11 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 06 Mar 2025 22:04:48 +01:00x86/kexec: Merge
x86_32andx86_64code using macros from <asm/asm.h>Merge common
x86_32andx86_64code incrash_setup_regs()using macros from <asm/asm.h>.The compiled object files before and after the patch are unchanged.
Signed-off-by Uros Bizjak <[email protected]>Signed-off-by Ingo Molnar <[email protected]>Cc David Woodhouse <[email protected]>Cc Baoquan He <[email protected]>Cc Vivek Goyal <[email protected]>Cc Dave Young <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc "H. Peter Anvin" <[email protected]>---
arch/x86/include/asm/kexec.h| 58 +++++++++++++++--------------------1 file changed, 25 insertions(+), 33 deletions(-)arch/x86/include/asm/kexec.h +21
-31@@ -18,6 +18,7 @@#include <linux/string.h> #include <linux/kernel.h> +#include <asm/asm.h> #include <asm/page.h> #include <asm/ptrace.h> @@ -71,41 +72,32 @@ static inline void crash_setup_regs(struct pt_regs *newregs,if (oldregs) {memcpy (newregs, oldregs,sizeof (*newregs)); }else { + asmvolatile ("mov %%" _ASM_BX",%0" :"=m" (newregs->bx)); + asmvolatile ("mov %%" _ASM_CX",%0" :"=m" (newregs->cx)); + asmvolatile ("mov %%" _ASM_DX",%0" :"=m" (newregs->dx)); + asmvolatile ("mov %%" _ASM_SI",%0" :"=m" (newregs->si)); + asmvolatile ("mov %%" _ASM_DI",%0" :"=m" (newregs->di)); + asmvolatile ("mov %%" _ASM_BP",%0" :"=m" (newregs->bp)); + asmvolatile ("mov %%" _ASM_AX",%0" :"=m" (newregs->ax)); + asmvolatile ("mov %%" _ASM_SP",%0" :"=m" (newregs->sp)); +#ifdef CONFIG_X86_64 + asmvolatile ("mov %%r8,%0" :"=m" (newregs->r8)); + asmvolatile ("mov %%r9,%0" :"=m" (newregs->r9)); + asmvolatile ("mov %%r10,%0" :"=m" (newregs->r10)); + asmvolatile ("mov %%r11,%0" :"=m" (newregs->r11)); + asmvolatile ("mov %%r12,%0" :"=m" (newregs->r12)); + asmvolatile ("mov %%r13,%0" :"=m" (newregs->r13)); + asmvolatile ("mov %%r14,%0" :"=m" (newregs->r14)); + asmvolatile ("mov %%r15,%0" :"=m" (newregs->r15)); +#endif + asmvolatile ("mov %%ss,%k0" :"=a" (newregs->ss)); + asmvolatile ("mov %%cs,%k0" :"=a" (newregs->cs));#ifdef CONFIG_X86_32 - asmvolatile ("movl %%ebx,%0" :"=m" (newregs->bx)); - asmvolatile ("movl %%ecx,%0" :"=m" (newregs->cx)); - asmvolatile ("movl %%edx,%0" :"=m" (newregs->dx)); - asmvolatile ("movl %%esi,%0" :"=m" (newregs->si)); - asmvolatile ("movl %%edi,%0" :"=m" (newregs->di)); - asmvolatile ("movl %%ebp,%0" :"=m" (newregs->bp)); - asmvolatile ("movl %%eax,%0" :"=m" (newregs->ax)); - asmvolatile ("movl %%esp,%0" :"=m" (newregs->sp)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("movl %%ds, %%eax;" :"=a" (newregs->ds)); - asmvolatile ("movl %%es, %%eax;" :"=a" (newregs->es)); - asmvolatile ("pushfl; popl %0" :"=m" (newregs->flags)); -#else - asmvolatile ("movq %%rbx,%0" :"=m" (newregs->bx)); - asmvolatile ("movq %%rcx,%0" :"=m" (newregs->cx)); - asmvolatile ("movq %%rdx,%0" :"=m" (newregs->dx)); - asmvolatile ("movq %%rsi,%0" :"=m" (newregs->si)); - asmvolatile ("movq %%rdi,%0" :"=m" (newregs->di)); - asmvolatile ("movq %%rbp,%0" :"=m" (newregs->bp)); - asmvolatile ("movq %%rax,%0" :"=m" (newregs->ax)); - asmvolatile ("movq %%rsp,%0" :"=m" (newregs->sp)); - asmvolatile ("movq %%r8,%0" :"=m" (newregs->r8)); - asmvolatile ("movq %%r9,%0" :"=m" (newregs->r9)); - asmvolatile ("movq %%r10,%0" :"=m" (newregs->r10)); - asmvolatile ("movq %%r11,%0" :"=m" (newregs->r11)); - asmvolatile ("movq %%r12,%0" :"=m" (newregs->r12)); - asmvolatile ("movq %%r13,%0" :"=m" (newregs->r13)); - asmvolatile ("movq %%r14,%0" :"=m" (newregs->r14)); - asmvolatile ("movq %%r15,%0" :"=m" (newregs->r15)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss));
arch/x86/include/asm/kexec.h static inline void crash_setup_regs(struct pt_regs *newregs, · line 103
Quoted 10 lines
arch/x86/include/asm/kexec.h +4
-2- asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("pushfq; popq %0" :"=m" (newregs->flags)); + asmvolatile ("mov %%ds,%k0" :"=a" (newregs->ds)); + asmvolatile ("mov %%es,%k0" :"=a" (newregs->es));#endif + asmvolatile ("pushf\n\t" +"pop %0" :"=m" (newregs->flags)); newregs->ip = _THIS_IP_; } }
Incidentally, doing this in C code is obviously completely broken, especially doing it in multiple statements. You have no idea what the compiler has messed with before you get there.
Re: [tip: x86/asm] x86/kexec: Merge x86_32 and x86_64 code using macros from <asm/asm.h>
Quoted 103 lines · H. Peter Anvin
Quoted 101 lines · On March 6, 2025 1:33:43 PM PST, tip-bot2 for Uros Bizjak…
The following commit has been merged into the x86/asm branch of tip:
Commit-ID: aa3942d4d12ef57f031faa2772fe410c24191e36 Gitweb: https://git.kernel.org/tip/aa3942d4d12ef57f031faa2772fe410c24191e36 Author: Uros Bizjak <[email protected]> AuthorDate: Thu, 06 Mar 2025 15:52:11 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 06 Mar 2025 22:04:48 +01:00x86/kexec: Merge
x86_32andx86_64code using macros from <asm/asm.h>Merge common
x86_32andx86_64code incrash_setup_regs()using macros from <asm/asm.h>.The compiled object files before and after the patch are unchanged.
Signed-off-by Uros Bizjak <[email protected]>Signed-off-by Ingo Molnar <[email protected]>Cc David Woodhouse <[email protected]>Cc Baoquan He <[email protected]>Cc Vivek Goyal <[email protected]>Cc Dave Young <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc "H. Peter Anvin" <[email protected]>---
arch/x86/include/asm/kexec.h| 58 +++++++++++++++--------------------1 file changed, 25 insertions(+), 33 deletions(-)arch/x86/include/asm/kexec.h +23
-33@@ -18,6 +18,7 @@#include <linux/string.h> #include <linux/kernel.h> +#include <asm/asm.h> #include <asm/page.h> #include <asm/ptrace.h> @@ -71,41 +72,32 @@ static inline void crash_setup_regs(struct pt_regs *newregs,if (oldregs) {memcpy (newregs, oldregs,sizeof (*newregs)); }else { + asmvolatile ("mov %%" _ASM_BX",%0" :"=m" (newregs->bx)); + asmvolatile ("mov %%" _ASM_CX",%0" :"=m" (newregs->cx)); + asmvolatile ("mov %%" _ASM_DX",%0" :"=m" (newregs->dx)); + asmvolatile ("mov %%" _ASM_SI",%0" :"=m" (newregs->si)); + asmvolatile ("mov %%" _ASM_DI",%0" :"=m" (newregs->di)); + asmvolatile ("mov %%" _ASM_BP",%0" :"=m" (newregs->bp)); + asmvolatile ("mov %%" _ASM_AX",%0" :"=m" (newregs->ax)); + asmvolatile ("mov %%" _ASM_SP",%0" :"=m" (newregs->sp)); +#ifdef CONFIG_X86_64 + asmvolatile ("mov %%r8,%0" :"=m" (newregs->r8)); + asmvolatile ("mov %%r9,%0" :"=m" (newregs->r9)); + asmvolatile ("mov %%r10,%0" :"=m" (newregs->r10)); + asmvolatile ("mov %%r11,%0" :"=m" (newregs->r11)); + asmvolatile ("mov %%r12,%0" :"=m" (newregs->r12)); + asmvolatile ("mov %%r13,%0" :"=m" (newregs->r13)); + asmvolatile ("mov %%r14,%0" :"=m" (newregs->r14)); + asmvolatile ("mov %%r15,%0" :"=m" (newregs->r15)); +#endif + asmvolatile ("mov %%ss,%k0" :"=a" (newregs->ss)); + asmvolatile ("mov %%cs,%k0" :"=a" (newregs->cs));#ifdef CONFIG_X86_32 - asmvolatile ("movl %%ebx,%0" :"=m" (newregs->bx)); - asmvolatile ("movl %%ecx,%0" :"=m" (newregs->cx)); - asmvolatile ("movl %%edx,%0" :"=m" (newregs->dx)); - asmvolatile ("movl %%esi,%0" :"=m" (newregs->si)); - asmvolatile ("movl %%edi,%0" :"=m" (newregs->di)); - asmvolatile ("movl %%ebp,%0" :"=m" (newregs->bp)); - asmvolatile ("movl %%eax,%0" :"=m" (newregs->ax)); - asmvolatile ("movl %%esp,%0" :"=m" (newregs->sp)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("movl %%ds, %%eax;" :"=a" (newregs->ds)); - asmvolatile ("movl %%es, %%eax;" :"=a" (newregs->es)); - asmvolatile ("pushfl; popl %0" :"=m" (newregs->flags)); -#else - asmvolatile ("movq %%rbx,%0" :"=m" (newregs->bx)); - asmvolatile ("movq %%rcx,%0" :"=m" (newregs->cx)); - asmvolatile ("movq %%rdx,%0" :"=m" (newregs->dx)); - asmvolatile ("movq %%rsi,%0" :"=m" (newregs->si)); - asmvolatile ("movq %%rdi,%0" :"=m" (newregs->di)); - asmvolatile ("movq %%rbp,%0" :"=m" (newregs->bp)); - asmvolatile ("movq %%rax,%0" :"=m" (newregs->ax)); - asmvolatile ("movq %%rsp,%0" :"=m" (newregs->sp)); - asmvolatile ("movq %%r8,%0" :"=m" (newregs->r8)); - asmvolatile ("movq %%r9,%0" :"=m" (newregs->r9)); - asmvolatile ("movq %%r10,%0" :"=m" (newregs->r10)); - asmvolatile ("movq %%r11,%0" :"=m" (newregs->r11)); - asmvolatile ("movq %%r12,%0" :"=m" (newregs->r12)); - asmvolatile ("movq %%r13,%0" :"=m" (newregs->r13)); - asmvolatile ("movq %%r14,%0" :"=m" (newregs->r14)); - asmvolatile ("movq %%r15,%0" :"=m" (newregs->r15)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("pushfq; popq %0" :"=m" (newregs->flags)); + asmvolatile ("mov %%ds,%k0" :"=a" (newregs->ds)); + asmvolatile ("mov %%es,%k0" :"=a" (newregs->es));
Quoted 8 lines
arch/x86/include/asm/kexec.h static inline void crash_setup_regs(struct pt_regs *newregs, · line 103
Quoted 6 lines
arch/x86/include/asm/kexec.h +2
-0#endif + asmvolatile ("pushf\n\t" +"pop %0" :"=m" (newregs->flags)); newregs->ip = _THIS_IP_; } }Incidentally, doing this in C code is obviously completely broken, especially doing it in multiple statements. You have no idea what the compiler has messed with before you get there.
These are "asm volatile" statemets, so at least they won't be scheduled in a different way. OTOH, please note that the patch is very carefully written to not change code flow, usage of hardregs in the inline asm is usually the sign of fragile code.
Uros.
H. Peter Anvin
reviewerRe: [tip: x86/asm] x86/kexec: Merge x86_32 and x86_64 code using macros from <asm/asm.h>
Quoted 113 lines · Uros Bizjak
Quoted 103 lines · H. Peter Anvin
Quoted 101 lines · On March 6, 2025 1:33:43 PM PST, tip-bot2 for Uros Bizjak…
The following commit has been merged into the x86/asm branch of tip:
Commit-ID: aa3942d4d12ef57f031faa2772fe410c24191e36 Gitweb: https://git.kernel.org/tip/aa3942d4d12ef57f031faa2772fe410c24191e36 Author: Uros Bizjak <[email protected]> AuthorDate: Thu, 06 Mar 2025 15:52:11 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 06 Mar 2025 22:04:48 +01:00x86/kexec: Merge
x86_32andx86_64code using macros from <asm/asm.h>Merge common
x86_32andx86_64code incrash_setup_regs()using macros from <asm/asm.h>.The compiled object files before and after the patch are unchanged.
Signed-off-by Uros Bizjak <[email protected]>Signed-off-by Ingo Molnar <[email protected]>Cc David Woodhouse <[email protected]>Cc Baoquan He <[email protected]>Cc Vivek Goyal <[email protected]>Cc Dave Young <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc "H. Peter Anvin" <[email protected]>---
arch/x86/include/asm/kexec.h| 58 +++++++++++++++--------------------1 file changed, 25 insertions(+), 33 deletions(-)arch/x86/include/asm/kexec.h +23
-33@@ -18,6 +18,7 @@#include <linux/string.h> #include <linux/kernel.h> +#include <asm/asm.h> #include <asm/page.h> #include <asm/ptrace.h> @@ -71,41 +72,32 @@ static inline void crash_setup_regs(struct pt_regs *newregs,if (oldregs) {memcpy (newregs, oldregs,sizeof (*newregs)); }else { + asmvolatile ("mov %%" _ASM_BX",%0" :"=m" (newregs->bx)); + asmvolatile ("mov %%" _ASM_CX",%0" :"=m" (newregs->cx)); + asmvolatile ("mov %%" _ASM_DX",%0" :"=m" (newregs->dx)); + asmvolatile ("mov %%" _ASM_SI",%0" :"=m" (newregs->si)); + asmvolatile ("mov %%" _ASM_DI",%0" :"=m" (newregs->di)); + asmvolatile ("mov %%" _ASM_BP",%0" :"=m" (newregs->bp)); + asmvolatile ("mov %%" _ASM_AX",%0" :"=m" (newregs->ax)); + asmvolatile ("mov %%" _ASM_SP",%0" :"=m" (newregs->sp)); +#ifdef CONFIG_X86_64 + asmvolatile ("mov %%r8,%0" :"=m" (newregs->r8)); + asmvolatile ("mov %%r9,%0" :"=m" (newregs->r9)); + asmvolatile ("mov %%r10,%0" :"=m" (newregs->r10)); + asmvolatile ("mov %%r11,%0" :"=m" (newregs->r11)); + asmvolatile ("mov %%r12,%0" :"=m" (newregs->r12)); + asmvolatile ("mov %%r13,%0" :"=m" (newregs->r13)); + asmvolatile ("mov %%r14,%0" :"=m" (newregs->r14)); + asmvolatile ("mov %%r15,%0" :"=m" (newregs->r15)); +#endif + asmvolatile ("mov %%ss,%k0" :"=a" (newregs->ss)); + asmvolatile ("mov %%cs,%k0" :"=a" (newregs->cs));#ifdef CONFIG_X86_32 - asmvolatile ("movl %%ebx,%0" :"=m" (newregs->bx)); - asmvolatile ("movl %%ecx,%0" :"=m" (newregs->cx)); - asmvolatile ("movl %%edx,%0" :"=m" (newregs->dx)); - asmvolatile ("movl %%esi,%0" :"=m" (newregs->si)); - asmvolatile ("movl %%edi,%0" :"=m" (newregs->di)); - asmvolatile ("movl %%ebp,%0" :"=m" (newregs->bp)); - asmvolatile ("movl %%eax,%0" :"=m" (newregs->ax)); - asmvolatile ("movl %%esp,%0" :"=m" (newregs->sp)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("movl %%ds, %%eax;" :"=a" (newregs->ds)); - asmvolatile ("movl %%es, %%eax;" :"=a" (newregs->es)); - asmvolatile ("pushfl; popl %0" :"=m" (newregs->flags)); -#else - asmvolatile ("movq %%rbx,%0" :"=m" (newregs->bx)); - asmvolatile ("movq %%rcx,%0" :"=m" (newregs->cx)); - asmvolatile ("movq %%rdx,%0" :"=m" (newregs->dx)); - asmvolatile ("movq %%rsi,%0" :"=m" (newregs->si)); - asmvolatile ("movq %%rdi,%0" :"=m" (newregs->di)); - asmvolatile ("movq %%rbp,%0" :"=m" (newregs->bp)); - asmvolatile ("movq %%rax,%0" :"=m" (newregs->ax)); - asmvolatile ("movq %%rsp,%0" :"=m" (newregs->sp)); - asmvolatile ("movq %%r8,%0" :"=m" (newregs->r8)); - asmvolatile ("movq %%r9,%0" :"=m" (newregs->r9)); - asmvolatile ("movq %%r10,%0" :"=m" (newregs->r10)); - asmvolatile ("movq %%r11,%0" :"=m" (newregs->r11)); - asmvolatile ("movq %%r12,%0" :"=m" (newregs->r12)); - asmvolatile ("movq %%r13,%0" :"=m" (newregs->r13)); - asmvolatile ("movq %%r14,%0" :"=m" (newregs->r14)); - asmvolatile ("movq %%r15,%0" :"=m" (newregs->r15)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("pushfq; popq %0" :"=m" (newregs->flags)); + asmvolatile ("mov %%ds,%k0" :"=a" (newregs->ds)); + asmvolatile ("mov %%es,%k0" :"=a" (newregs->es));Quoted 8 lines
arch/x86/include/asm/kexec.h static inline void crash_setup_regs(struct pt_regs *newregs, · line 103
Quoted 6 lines
arch/x86/include/asm/kexec.h +2
-0#endif + asmvolatile ("pushf\n\t" +"pop %0" :"=m" (newregs->flags)); newregs->ip = _THIS_IP_; } }Incidentally, doing this in C code is obviously completely broken, especially doing it in multiple statements. You have no idea what the compiler has messed with before you get there.
Quoted 7 lines
These are "asm volatile" statemets, so at least they won't be scheduled in a different way. OTOH, please note that the patch is very carefully written to not change code flow, usage of hardregs in the inline asm is usually the sign of fragile code.
Uros.
That doesn't matter, though; that only means they can't be moved relatively to each other, but the compiler is perfectly capable of inserting code before, or in between.
Your patch might be a functional null, but the code is broken on a deep and fundamental basis.
[tip: x86/core] x86/kexec: Merge x86_32 and x86_64 code using macros from <asm/asm.h>
In reply to Uros BizjakThe following commit has been merged into the x86/core branch of tip:
The following commit has been merged into the x86/core branch of tip:
Commit-ID: a9deda695972cb53fe815e175b7d66757964764e Gitweb: https://git.kernel.org/tip/a9deda695972cb53fe815e175b7d66757964764e Author: Uros Bizjak <[email protected]> AuthorDate: Thu, 06 Mar 2025 15:52:11 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 19 Mar 2025 11:26:24 +01:00
x86/kexec: Merge x86_32 and x86_64 code using macros from <asm/asm.h>
Merge common x86_32 and x86_64 code in crash_setup_regs() using macros from <asm/asm.h>.
The compiled object files before and after the patch are unchanged.
--- arch/x86/include/asm/kexec.h | 58 +++++++++++++++-------------------- 1 file changed, 25 insertions(+), 33 deletions(-)
arch/x86/include/asm/kexec.h +25 -33
@@ -18,6 +18,7 @@#include <linux/string.h> #include <linux/kernel.h> +#include <asm/asm.h> #include <asm/page.h> #include <asm/ptrace.h>
@@ -71,41 +72,32 @@ static inline void crash_setup_regs(struct pt_regs *newregs,if (oldregs) {memcpy (newregs, oldregs,sizeof (*newregs)); }else { + asmvolatile ("mov %%" _ASM_BX",%0" :"=m" (newregs->bx)); + asmvolatile ("mov %%" _ASM_CX",%0" :"=m" (newregs->cx)); + asmvolatile ("mov %%" _ASM_DX",%0" :"=m" (newregs->dx)); + asmvolatile ("mov %%" _ASM_SI",%0" :"=m" (newregs->si)); + asmvolatile ("mov %%" _ASM_DI",%0" :"=m" (newregs->di)); + asmvolatile ("mov %%" _ASM_BP",%0" :"=m" (newregs->bp)); + asmvolatile ("mov %%" _ASM_AX",%0" :"=m" (newregs->ax)); + asmvolatile ("mov %%" _ASM_SP",%0" :"=m" (newregs->sp)); +#ifdef CONFIG_X86_64 + asmvolatile ("mov %%r8,%0" :"=m" (newregs->r8)); + asmvolatile ("mov %%r9,%0" :"=m" (newregs->r9)); + asmvolatile ("mov %%r10,%0" :"=m" (newregs->r10)); + asmvolatile ("mov %%r11,%0" :"=m" (newregs->r11)); + asmvolatile ("mov %%r12,%0" :"=m" (newregs->r12)); + asmvolatile ("mov %%r13,%0" :"=m" (newregs->r13)); + asmvolatile ("mov %%r14,%0" :"=m" (newregs->r14)); + asmvolatile ("mov %%r15,%0" :"=m" (newregs->r15)); +#endif + asmvolatile ("mov %%ss,%k0" :"=a" (newregs->ss)); + asmvolatile ("mov %%cs,%k0" :"=a" (newregs->cs));#ifdef CONFIG_X86_32 - asmvolatile ("movl %%ebx,%0" :"=m" (newregs->bx)); - asmvolatile ("movl %%ecx,%0" :"=m" (newregs->cx)); - asmvolatile ("movl %%edx,%0" :"=m" (newregs->dx)); - asmvolatile ("movl %%esi,%0" :"=m" (newregs->si)); - asmvolatile ("movl %%edi,%0" :"=m" (newregs->di)); - asmvolatile ("movl %%ebp,%0" :"=m" (newregs->bp)); - asmvolatile ("movl %%eax,%0" :"=m" (newregs->ax)); - asmvolatile ("movl %%esp,%0" :"=m" (newregs->sp)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("movl %%ds, %%eax;" :"=a" (newregs->ds)); - asmvolatile ("movl %%es, %%eax;" :"=a" (newregs->es)); - asmvolatile ("pushfl; popl %0" :"=m" (newregs->flags)); -#else - asmvolatile ("movq %%rbx,%0" :"=m" (newregs->bx)); - asmvolatile ("movq %%rcx,%0" :"=m" (newregs->cx)); - asmvolatile ("movq %%rdx,%0" :"=m" (newregs->dx)); - asmvolatile ("movq %%rsi,%0" :"=m" (newregs->si)); - asmvolatile ("movq %%rdi,%0" :"=m" (newregs->di)); - asmvolatile ("movq %%rbp,%0" :"=m" (newregs->bp)); - asmvolatile ("movq %%rax,%0" :"=m" (newregs->ax)); - asmvolatile ("movq %%rsp,%0" :"=m" (newregs->sp)); - asmvolatile ("movq %%r8,%0" :"=m" (newregs->r8)); - asmvolatile ("movq %%r9,%0" :"=m" (newregs->r9)); - asmvolatile ("movq %%r10,%0" :"=m" (newregs->r10)); - asmvolatile ("movq %%r11,%0" :"=m" (newregs->r11)); - asmvolatile ("movq %%r12,%0" :"=m" (newregs->r12)); - asmvolatile ("movq %%r13,%0" :"=m" (newregs->r13)); - asmvolatile ("movq %%r14,%0" :"=m" (newregs->r14)); - asmvolatile ("movq %%r15,%0" :"=m" (newregs->r15)); - asmvolatile ("movl %%ss, %%eax;" :"=a" (newregs->ss)); - asmvolatile ("movl %%cs, %%eax;" :"=a" (newregs->cs)); - asmvolatile ("pushfq; popq %0" :"=m" (newregs->flags)); + asmvolatile ("mov %%ds,%k0" :"=a" (newregs->ds)); + asmvolatile ("mov %%es,%k0" :"=a" (newregs->es));#endif + asmvolatile ("pushf\n\t" +"pop %0" :"=m" (newregs->flags)); newregs->ip = _THIS_IP_; } }