x86/segment: Introduce savesegment_mem16() helper to write segment selectors to memory
PATCH v2 1/3 x86/segment: Introduce savesegment_mem16() helper to write segment selectors to memory
Introduce a new helper, savesegment_mem16(), that stores a segment selector directly into a u16 (or compatible) memory location without using an intermediate general-purpose register.
To support this, split the existing SAVE_SEGMENT macro into two parts:
SAVE_SEGMENT_VAR(): retains the current behavior of reading a segment register into an unsigned long via a register. SAVE_SEGMENT_PTR(): adds a new variant that writes the 16-bit selector directly to memory.
The combined SAVE_SEGMENT() macro now generates both helpers for each segment register.
The new savesegment_mem16() interface is preferred over savesegment() when the value only needs to be stored (e.g. into a struct field), avoiding an unnecessary register move and making the intent clearer.
No functional change for existing users of savesegment().
---
v2: Rename storesegment() to savesegment_mem16().
--- arch/x86/include/asm/segment.h | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-)
arch/x86/include/asm/segment.h +30 -1
@@ -344,7 +344,7 @@ static inline void __loadsegment_fs(u16 value)/* * Save a segment register away: */ -#define SAVE_SEGMENT (seg) \ +#define SAVE_SEGMENT_VAR (seg) \ static inline unsigned long __savesegment_##seg(void) \ { \ unsigned long v; \
@@ -352,6 +352,16 @@ static inline unsigned long __savesegment_##seg(void) \return v; \ } +#define SAVE_SEGMENT_PTR (seg) \ +static inline void __savesegment_##seg##_ptr(u16 *p) \ +{ \ + asm volatile("movw %%" #seg ",%0" : "=m" (*p)); \ +} + +#define SAVE_SEGMENT (seg) \ + SAVE_SEGMENT_VAR(seg) \ + SAVE_SEGMENT_PTR(seg) + SAVE_SEGMENT(cs) SAVE_SEGMENT(ss) SAVE_SEGMENT(ds)
@@ -359,10 +369,29 @@ SAVE_SEGMENT(es) SAVE_SEGMENT(fs) SAVE_SEGMENT(gs) +#undef SAVE_SEGMENT_VAR +#undef SAVE_SEGMENT_PTR#undef SAVE_SEGMENT +/* +* savesegment(seg, var) - Read a segment register into an unsigned long. +* +* Reads the segment selector via a general-purpose register into an +* unsigned long. Preferred when the value is needed in a register for +* subsequent arithmetic or comparison. +*/ #define savesegment (seg, var) ((var) = __savesegment_##seg()) +/* +* savesegment_mem16(seg, loc) - Store a segment register directly +* to u16 (or compatible) location. +* +* Writes the 16-bit segment selector directly to memory, bypassing any +* intermediate general-purpose register. Preferred over savesegment() +* for simply saving a segment register to a u16 (or compatible) location. +*/ +#define savesegment_mem16 (seg, loc) __savesegment_##seg##_ptr(&(loc)) +#endif /* !__ASSEMBLER__ */ #endif /* __KERNEL__ */
Signature · 1 line
2.53.0
PATCH v2 2/3 x86/process: Use savesegment_mem16() when saving segment selectors
Switch segment save sites in copy_thread() and save_fsgs() from savesegment() to the newly introduced savesegment_mem16() helper.
These call sites only store the segment selector into a u16 field and do not require the value in a general-purpose register. Using savesegment_mem16() avoids the unnecessary register intermediate and better matches the intended use.
The generated code is improved from, e.g.:
34f: 8c c0 mov %es,%eax
351: 66 89 83 08 0c 00 00 mov %ax,0xc08(%rbx)
to a single direct store:
34f: 8c 83 08 0c 00 00 mov %es,0xc08(%rbx)
This is a mechanical follow-up to the introduction of savesegment_mem16(), with no functional change.
--- arch/x86/kernel/process.c | 4 ++-- arch/x86/kernel/process_64.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/process.c +2 -2
@@ -197,8 +197,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.gsindex = current->thread.gsindex; p->thread.gsbase = current->thread.gsbase; -savesegment (es, p->thread.es); -savesegment (ds, p->thread.ds); +savesegment_mem16 (es, p->thread.es); +savesegment_mem16 (ds, p->thread.ds);if (p->mm && (clone_flags & (CLONE_VM |CLONE_VFORK )) ==CLONE_VM )set_bit (MM_CONTEXT_LOCK_LAM , &p->mm->context.flags);
arch/x86/kernel/process_64.c +2 -2
@@ -275,8 +275,8 @@ static __always_inline void save_base_legacy(struct task_struct *prev_p, static __always_inline void save_fsgs(struct task_struct *task) { -savesegment (fs, task->thread.fsindex); -savesegment (gs, task->thread.gsindex); +savesegment_mem16 (fs, task->thread.fsindex); +savesegment_mem16 (gs, task->thread.gsindex);if (static_cpu_has (X86_FEATURE_FSGSBASE )) {/* * If FSGSBASE is enabled, we can't make any useful guesses
Signature · 1 line
2.53.0
PATCH v2 3/3 x86/kexec: Use savesegment_mem16() for segment register save in crash_setup_regs()
In reply to Uros BizjakReplace open-coded inline assembly used to read segment registers (ss, cs, and on 32-bit ds/es) in crash_setup_regs() with savesegment_mem16() helper.
This function only stores the segment selector into a u16 field and does not require the value in a general-purpose register. Using savesegment_mem16() avoids the unnecessary register intermediate and better matches the intended use.
The generated code is improved from, e.g.:
131: 8c d0 mov %ss,%eax
133: 66 89 84 24 a0 00 00 mov %ax,0xa0(%rsp)
13a: 00to a single direct store:
131: 8c 94 24 a0 00 00 00 mov %ss,0xa0(%rsp)
This is a mechanical follow-up to the introduction of savesegment_mem16(), with no functional change.
---
v2: New patch.
--- arch/x86/include/asm/kexec.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/kexec.h +4 -4
@@ -106,11 +106,11 @@ static inline void crash_setup_regs(struct pt_regs *newregs, 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)); +savesegment_mem16 (ss, newregs->ss); +savesegment_mem16 (cs, newregs->cs);#ifdef CONFIG_X86_32 - asmvolatile ("mov %%ds,%k0" :"=a" (newregs->ds)); - asmvolatile ("mov %%es,%k0" :"=a" (newregs->es)); +savesegment_mem16 (ds, newregs->ds); +savesegment_mem16 (es, newregs->es);#endif asmvolatile ("pushf\n\t" "pop %0" :"=m" (newregs->flags));
Signature · 1 line
2.53.0