Skip to contentSkip to conversation

x86/speculation: Simplify and make CALL_NOSPEC consistent

PATCH Superseded Pawan Gupta 2 participants 8 messages Feb 26, 2025 – Feb 28, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pawan Gupta

author reviewer
#0 lore raw

PATCH 1/1 x86/speculation: Simplify and make CALL_NOSPEC consistent

CALL_NOSPEC macro is used to generate Spectre-v2 mitigation friendly indirect branches. At compile time the macro defaults to indirect branch, and at runtime those can be patched to thunk based mitigations.

This approach is opposite of what is done for the rest of the kernel, where the compile time default is to replace indirect calls with retpoline thunk calls.

Make CALL_NOSPEC consistent with the rest of the kernel, default to retpoline thunk at compile time when CONFIG_MITIGATION_RETPOLINE is enabled.

Also add the missing __CS_PREFIX to the CALL_NOSPEC macro.

---
 arch/x86/include/asm/nospec-branch.h | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
arch/x86/include/asm/nospec-branch.h +19 -11
index 7e8bf78c03d5..f12f83e7deb6 100644
@@ -200,7 +200,7 @@
 /*
  * 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 RAX prefix byte to encode. Also see apply_retpolines().
+ * 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
@@ -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().
+ */
+#define __CS_PREFIX(reg)				\
+	.irp rs,r8,r9,r10,r11,r12,r13,r14,r15;		\
+	.ifc \\rs, \reg;				\
+	.byte 0x2e;					\
+	.endif;						\
+	.endr;
+
 /*
  * Inline asm uses the %V modifier which is only in newer GCC
  * which is ensured when CONFIG_MITIGATION_RETPOLINE is defined.
  */
-# define CALL_NOSPEC						\
-	ALTERNATIVE_2(						\
-	ANNOTATE_RETPOLINE_SAFE					\
-	"call *%[thunk_target]\n",				\
-	"call __x86_indirect_thunk_%V[thunk_target]\n",		\
-	X86_FEATURE_RETPOLINE,					\
-	"lfence;\n"						\
-	ANNOTATE_RETPOLINE_SAFE					\
-	"call *%[thunk_target]\n",				\
-	X86_FEATURE_RETPOLINE_LFENCE)
+#ifdef CONFIG_MITIGATION_RETPOLINE
+#define CALL_NOSPEC	__stringify(__CS_PREFIX(%V[thunk_target]))	\
+			"call __x86_indirect_thunk_%V[thunk_target]\n"
+#else
+#define CALL_NOSPEC	"call *%[thunk_target]\n"
+#endif
 
 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
 

---
base-commit: d082ecbc71e9e0bf49883ee4afd435a77a5101b6
change-id: 20250226-call-nospec-b94808f0dc75

Best regards,

Signature · 2 lines
Thanks,
Pawan
#1 lore raw

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 427

Quoted 9 lines · Pawan Gupta
arch/x86/include/asm/nospec-branch.h +5 -0
@@ -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.

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 430

Quoted 3 lines
arch/x86/include/asm/nospec-branch.h +3 -0
+#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?

~Andrew

author reviewer
#2 lore raw
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Pawan Gupta
arch/x86/include/asm/nospec-branch.h +0 -0
@@ -420,20 +420,28 @@ static inline void call_depth_return_thunk(void) {}
 
 #ifdef CONFIG_X86_64
 
Quoted 9 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 427

Quoted 5 lines
arch/x86/include/asm/nospec-branch.h +5 -0
+/*
+ * 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

Quoted 10 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 430

Quoted 3 lines
arch/x86/include/asm/nospec-branch.h +3 -0
+#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. :(

#3 lore raw
Quoted 51 lines · Pawan Gupta
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Pawan Gupta
arch/x86/include/asm/nospec-branch.h +0 -0
@@ -420,20 +420,28 @@ static inline void call_depth_return_thunk(void) {}
 
 #ifdef CONFIG_X86_64
 
Quoted 8 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 427

Quoted 5 lines
arch/x86/include/asm/nospec-branch.h +5 -0
+/*
+ * 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

Quoted 9 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 430

Quoted 3 lines
arch/x86/include/asm/nospec-branch.h +3 -0
+#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":

Quoted 8 lines
	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).

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.

~Andrew

author reviewer
#4 lore raw
Quoted 56 lines · Andrew Cooper
Quoted 55 lines · Pawan Gupta
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Pawan Gupta
arch/x86/include/asm/nospec-branch.h +0 -0
@@ -420,20 +420,28 @@ static inline void call_depth_return_thunk(void) {}
 
 #ifdef CONFIG_X86_64
 
Quoted 8 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 427

Quoted 5 lines
arch/x86/include/asm/nospec-branch.h +5 -0
+/*
+ * 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

Quoted 9 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 430

Quoted 3 lines
arch/x86/include/asm/nospec-branch.h +3 -0
+#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:
Quoted 8 lines
Quoted 4 lines
	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.

Quoted 5 lines

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));
author reviewer
#5 lore raw
Quoted 74 lines · Pawan Gupta
Quoted 56 lines · Andrew Cooper
Quoted 55 lines · Pawan Gupta
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Pawan Gupta
arch/x86/include/asm/nospec-branch.h +0 -0
@@ -420,20 +420,28 @@ static inline void call_depth_return_thunk(void) {}
 
 #ifdef CONFIG_X86_64
 
Quoted 8 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 427

Quoted 5 lines
arch/x86/include/asm/nospec-branch.h +5 -0
+/*
+ * 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

Quoted 9 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 430

Quoted 3 lines
arch/x86/include/asm/nospec-branch.h +3 -0
+#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:
Quoted 8 lines
Quoted 4 lines
	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.

Quoted 5 lines

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.

Quoted 6 lines

Using \\ for reg like this:

.ifc \\rs, \\reg

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

Getting rid of one layer of macro makes it less magical:

#define __CS_PREFIX(reg)				\
            ".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n"	\
            ".ifc \\rs," reg "\n"                       \
            ".byte 0x2e\n"                              \
            ".endif\n"                                  \
            ".endr\n"
#define CALL_NOSPEC    __CS_PREFIX("%V[thunk_target]")  \
                        "call __x86_indirect_thunk_%V[thunk_target]\n"

#else
#define CALL_NOSPEC "call *%[thunk_target]\n"
#endif

Preprocessor output is as expected, and this emits the CS prefix correctly:

	".ifc \\rs, %V[thunk_target]"

Full version:

	asm("push %[flags]; popf; " ".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n" ".ifc \\rs," "%V[thunk_target]" "\n" ".byte 0x2e\n" ".endif\n" ".endr\n" "call __x86_indirect_thunk_%V[thunk_target]\n"
	: "=a"(rc), "+r" (current_stack_pointer) : [thunk_target]"r"(fop), [flags]"r"(flags));
#6 lore raw
Quoted 93 lines · Pawan Gupta
Quoted 69 lines · Pawan Gupta
Quoted 56 lines · Andrew Cooper
Quoted 55 lines · Pawan Gupta
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Pawan Gupta
arch/x86/include/asm/nospec-branch.h +0 -0
@@ -420,20 +420,28 @@ static inline void call_depth_return_thunk(void) {}
 
 #ifdef CONFIG_X86_64
 
Quoted 8 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 427

Quoted 5 lines
arch/x86/include/asm/nospec-branch.h +5 -0
+/*
+ * 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

Quoted 9 lines

arch/x86/include/asm/nospec-branch.h static inline void call_depth_return_thunk(void) {} · line 430

Quoted 3 lines
arch/x86/include/asm/nospec-branch.h +3 -0
+#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:
Quoted 7 lines
Quoted 4 lines
	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.

Quoted 3 lines

After AS's macro expansion, \reg becomes \% which is not a valid escape character, so the \ gets left intact.

Quoted 8 lines
Quoted 2 lines

\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.

Getting rid of one layer of macro makes it less magical:

#define __CS_PREFIX(reg)				\
            ".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n"	\
            ".ifc \\rs," reg "\n"                       \
            ".byte 0x2e\n"                              \
            ".endif\n"                                  \
            ".endr\n"
#define CALL_NOSPEC    __CS_PREFIX("%V[thunk_target]")  \
                        "call __x86_indirect_thunk_%V[thunk_target]\n"

#else
#define CALL_NOSPEC "call *%[thunk_target]\n"
#endif

Quoted 8 lines

Preprocessor output is as expected, and this emits the CS prefix correctly:

	".ifc \\rs, %V[thunk_target]"

Full version:

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

Yeah, I think that's a lot better.

I still cant spot precisely what's wrong with the prior \\reg.

~Andrew

author reviewer
#7 lore raw
Quoted 14 lines · Andrew Cooper
Quoted 14 lines

Getting rid of one layer of macro makes it less magical:

#define __CS_PREFIX(reg)				\
            ".irp rs,r8,r9,r10,r11,r12,r13,r14,r15\n"	\
            ".ifc \\rs," reg "\n"                       \
            ".byte 0x2e\n"                              \
            ".endif\n"                                  \
            ".endr\n"
#define CALL_NOSPEC    __CS_PREFIX("%V[thunk_target]")  \
                        "call __x86_indirect_thunk_%V[thunk_target]\n"

#else
#define CALL_NOSPEC "call *%[thunk_target]\n"
#endif

[...]

Quoted 1 line

Yeah, I think that's a lot better.

Ok, I will go with this approach for now.