mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: x86@kernel.org, Josh Poimboeuf <jpoimboe@kernel.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] x86/speculation: Simplify and make CALL_NOSPEC consistent
Date: Thu, 27 Feb 2025 15:13:42 -0800	[thread overview]
Message-ID: <20250227231342.jh67quujcd3tgmft@desk> (raw)
In-Reply-To: <15253834-fb89-408f-8269-65413ad29f7a@citrix.com>

On Thu, Feb 27, 2025 at 06:57:37PM +0000, Andrew Cooper wrote:
> On 27/02/2025 6:41 pm, Pawan Gupta wrote:
> > On Thu, Feb 27, 2025 at 12:49:48AM +0000, Andrew Cooper wrote:
> >> On 26/02/2025 9:03 pm, Pawan Gupta wrote:
> >>> @@ -420,20 +420,28 @@ static inline void call_depth_return_thunk(void) {}
> >>>  
> >>>  #ifdef CONFIG_X86_64
> >>>  
> >>> +/*
> >>> + * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
> >>> + * to the retpoline thunk with a CS prefix when the register requires
> >>> + * a REX prefix byte to encode. Also see apply_retpolines().
> >>> + */
> >> Technically, both comments aren't quite accurate.  __CS_PREFIX() emits a
> >> conditional CS prefix in a manner compatible with
> >> -mindirect-branch-cs-prefix, not the full 5/6 byte jmp/call.
> > You are right, I will update the comment, and also the ASSEMBLY version
> > where this comment came from:
> >
> >   /*
> >    * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
> >    * to the retpoline thunk with a CS prefix when the register requires
> >    * a REX prefix byte to encode. Also see apply_retpolines().
> >    */
> >   .macro __CS_PREFIX reg:req
> >           .irp rs,r8,r9,r10,r11,r12,r13,r14,r15
> >           .ifc \reg,\rs
> >           .byte 0x2e
> >           .endif
> >           .endr
> >   .endm
> >
> >>> +#define __CS_PREFIX(reg)				\
> >>> +	.irp rs,r8,r9,r10,r11,r12,r13,r14,r15;		\
> >>> +	.ifc \\rs, \reg;				\
> >> Why are these escaped differently?  Given they're all \r of some form or
> >> another, I guess something is going wonky with __stringify(), but its
> >> still weird for them to be different.
> >>
> >> Do you have a fully pre-processed source to hand to see how CALL_NOSPEC
> >> ends up?
> > Below is the pre-processed source for test_cc() generated with "make arch/x86/kvm/emulate.i".
> >
> > - This is with double backslash in ".ifc \\rs, \reg":
> >
> > 	asm("push %[flags]; popf; " ".irp rs,r8,r9,r10,r11,r12,r13,r14,r15; .ifc \\rs, \%V[thunk_target]; .byte 0x2e; .endif; .endr;" "call __x86_indirect_thunk_%V[thunk_target]\n"
> >                                                                                   ^
> > 	This ends up emitting the CS prefix byte correctly:
> >
> > 	2e e8 51 c9 32 01       cs call ffffffff824289e0
> >
> > - This is with single backslash in ".ifc \\rs, \reg":
> >
> > 	asm("push %[flags]; popf; " ".irp rs,r8,r9,r10,r11,r12,r13,r14,r15; .ifc \rs, \%V[thunk_target]; .byte 0x2e; .endif; .endr;" "c      all __x86_indirect_thunk_%V[thunk_target]\n"
> >                                                                                   ^
> > 	This version does not emit the CS prefix byte:
> >
> > 	e8 52 c9 32 01          call   ffffffff824289e0
> >
> > I tried looking in gcc inline assembly documentation but could not find
> > anything that would explain this. :(
> 
> It's because it's about plain C strings.
> 
> \r (from \rs) is Carriage Return (ASCII 0x0d).

Ah, right.

> After AS's macro expansion, \reg becomes \% which is not a valid escape
> character, so the \ gets left intact.
> 
> \reg should become \\reg or you'll probably get a compiler complaining
> eventually.

Using \\ for reg like this:

 .ifc \\rs, \\reg

is not emitting the CS prefix. I am trying to wrap my head around the
magic.

Below is the pre-processor output:

	asm("push %[flags]; popf; " ".irp rs,r8,rax,r9,r10,r11,r12,r13,r14,r15; .ifc \\rs, \\%V[thunk_target]; .byte 0x2e; .endif; .end      r;" "call __x86_indirect_thunk_%V[thunk_target]\n"
	: "=a"(rc), "+r" (current_stack_pointer) : [thunk_target]"r"(fop), [flags]"r"(flags));

  reply	other threads:[~2025-02-27 23:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-26 21:03 Pawan Gupta
2025-02-27  0:49 ` Andrew Cooper
2025-02-27 18:41   ` Pawan Gupta
2025-02-27 18:57     ` Andrew Cooper
2025-02-27 23:13       ` Pawan Gupta [this message]
2025-02-28  0:31         ` Pawan Gupta
2025-02-28  0:36           ` Andrew Cooper
2025-02-28  1:06             ` Pawan Gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250227231342.jh67quujcd3tgmft@desk \
    --to=pawan.kumar.gupta@linux.intel.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®