Re: [tip: x86/cleanups] x86/segment: Use MOVL when reading segment registers
From: Uros Bizjak
Date: Thu Mar 12 2026 - 15:50:46 EST
On Thu, Mar 12, 2026 at 8:12 PM H. Peter Anvin <hpa@xxxxxxxxx> wrote:
>
> Note: I personally *hate* the savesegment() syntax. For one thing, I would
> much rather see a more C-like syntax, even if that means making a bunch of
> inline functions. Replacing the macro with something that is actually
> type-safe would be a bloody good thing, too.
>
> Something like:
>
> /* Generates register operation */
> static inline unsigned int ds(void)
> {
> unsigned int v;
> asm volatile("mov %%ds,%0" : "=r" (v));
> return v;
> }
Yes, this would be much better, but should be unsigned long, because
move from segment register zero-extends all the way to 64-bits:
static inline unsigned long read_ds(void)
{
unsigned long v;
asm volatile("mov %%ds,%k0" : "=r" (v));
return v;
}
Uros.
> /* Generates memory operation */
> static inline void save_ds(u16 *dst)
> {
> asm volatile("mov %%ds,%0" : "=m" (dst));
> }
>
> Generating those inlines can be macroized if we really feel the need to, of
> course...
>
> -hpa
>