d8f365
From 0df8313a0a5d8533f2487e21d7b42e9adee28f18 Mon Sep 17 00:00:00 2001
d8f365
From: "H.J. Lu" <hjl.tools@gmail.com>
d8f365
Date: Wed, 27 Oct 2021 06:27:15 -0700
d8f365
Subject: [PATCH 2/4] x86: Add -mindirect-branch-cs-prefix
d8f365
d8f365
Add -mindirect-branch-cs-prefix to add CS prefix to call and jmp to
d8f365
indirect thunk with branch target in r8-r15 registers so that the call
d8f365
and jmp instruction length is 6 bytes to allow them to be replaced with
d8f365
"lfence; call *%r8-r15" or "lfence; jmp *%r8-r15" at run-time.
d8f365
d8f365
gcc/
d8f365
d8f365
	PR target/102952
d8f365
	* config/i386/i386.c (ix86_output_jmp_thunk_or_indirect): Emit
d8f365
	CS prefix for -mindirect-branch-cs-prefix.
d8f365
	(ix86_output_indirect_branch_via_reg): Likewise.
d8f365
	* config/i386/i386.opt: Add -mindirect-branch-cs-prefix.
d8f365
	* doc/invoke.texi: Document -mindirect-branch-cs-prefix.
d8f365
d8f365
gcc/testsuite/
d8f365
d8f365
	PR target/102952
d8f365
	* gcc.target/i386/indirect-thunk-cs-prefix-1.c: New test.
d8f365
	* gcc.target/i386/indirect-thunk-cs-prefix-2.c: Likewise.
d8f365
d8f365
(cherry picked from commit 2196a681d7810ad8b227bf983f38ba716620545e)
d8f365
---
d8f365
 gcc/config/i386/i386.c                            | 14 ++++++++++++--
d8f365
 gcc/config/i386/i386.opt                          |  4 ++++
d8f365
 gcc/doc/invoke.texi                               | 10 +++++++++-
d8f365
 .../gcc.target/i386/indirect-thunk-cs-prefix-1.c  | 14 ++++++++++++++
d8f365
 .../gcc.target/i386/indirect-thunk-cs-prefix-2.c  | 15 +++++++++++++++
d8f365
 5 files changed, 54 insertions(+), 3 deletions(-)
d8f365
 create mode 100644 gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-1.c
d8f365
 create mode 100644 gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-2.c
d8f365
d8f365
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
d8f365
index eb9303f8742..8442dd0daea 100644
d8f365
--- a/gcc/config/i386/i386.c
d8f365
+++ b/gcc/config/i386/i386.c
d8f365
@@ -28728,7 +28728,12 @@ ix86_output_jmp_thunk_or_indirect (const char *thunk_name,
d8f365
       if (need_prefix == indirect_thunk_prefix_bnd)
d8f365
 	fprintf (asm_out_file, "\tbnd jmp\t");
d8f365
       else
d8f365
-	fprintf (asm_out_file, "\tjmp\t");
d8f365
+	{
d8f365
+	  if (REX_INT_REGNO_P (regno)
d8f365
+	      && ix86_indirect_branch_cs_prefix)
d8f365
+	    fprintf (asm_out_file, "\tcs\n");
d8f365
+	  fprintf (asm_out_file, "\tjmp\t");
d8f365
+	}
d8f365
       assemble_name (asm_out_file, thunk_name);
d8f365
       putc ('\n', asm_out_file);
d8f365
       if ((ix86_harden_sls & harden_sls_indirect_branch))
d8f365
@@ -28787,7 +28792,12 @@ ix86_output_indirect_branch_via_reg (rtx call_op, bool sibcall_p)
d8f365
 	  if (need_prefix == indirect_thunk_prefix_bnd)
d8f365
 	    fprintf (asm_out_file, "\tbnd call\t");
d8f365
 	  else
d8f365
-	    fprintf (asm_out_file, "\tcall\t");
d8f365
+	    {
d8f365
+	      if (REX_INT_REGNO_P (regno)
d8f365
+		  && ix86_indirect_branch_cs_prefix)
d8f365
+		fprintf (asm_out_file, "\tcs\n");
d8f365
+	      fprintf (asm_out_file, "\tcall\t");
d8f365
+	    }
d8f365
 	  assemble_name (asm_out_file, thunk_name);
d8f365
 	  putc ('\n', asm_out_file);
d8f365
 	  return;
d8f365
diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt
d8f365
index 3ae48609e25..9f67ef558dc 100644
d8f365
--- a/gcc/config/i386/i386.opt
d8f365
+++ b/gcc/config/i386/i386.opt
d8f365
@@ -1044,6 +1044,10 @@ Enum(indirect_branch) String(thunk-inline) Value(indirect_branch_thunk_inline)
d8f365
 EnumValue
d8f365
 Enum(indirect_branch) String(thunk-extern) Value(indirect_branch_thunk_extern)
d8f365
 
d8f365
+mindirect-branch-cs-prefix
d8f365
+Target Var(ix86_indirect_branch_cs_prefix) Init(0)
d8f365
+Add CS prefix to call and jmp to indirect thunk with branch target in r8-r15 registers.
d8f365
+
d8f365
 mindirect-branch-register
d8f365
 Target Report Var(ix86_indirect_branch_register) Init(0)
d8f365
 Force indirect call and jump via register.
d8f365
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
d8f365
index 1e20efd6969..605cd4b93f1 100644
d8f365
--- a/gcc/doc/invoke.texi
d8f365
+++ b/gcc/doc/invoke.texi
d8f365
@@ -1284,7 +1284,8 @@ See RS/6000 and PowerPC Options.
d8f365
 -mstack-protector-guard-symbol=@var{symbol} -mmitigate-rop @gol
d8f365
 -mgeneral-regs-only -mcall-ms2sysv-xlogues @gol
d8f365
 -mindirect-branch=@var{choice} -mfunction-return=@var{choice} @gol
d8f365
--mindirect-branch-register -mharden-sls=@var{choice}}
d8f365
+-mindirect-branch-register -mharden-sls=@var{choice} @gol
d8f365
+-mindirect-branch-cs-prefix}
d8f365
 
d8f365
 @emph{x86 Windows Options}
d8f365
 @gccoptlist{-mconsole  -mcygwin  -mno-cygwin  -mdll @gol
d8f365
@@ -28044,6 +28045,13 @@ hardening.  @samp{return} enables SLS hardening for function return.
d8f365
 @samp{indirect-branch} enables SLS hardening for indirect branch.
d8f365
 @samp{all} enables all SLS hardening.
d8f365
 
d8f365
+@item -mindirect-branch-cs-prefix
d8f365
+@opindex mindirect-branch-cs-prefix
d8f365
+Add CS prefix to call and jmp to indirect thunk with branch target in
d8f365
+r8-r15 registers so that the call and jmp instruction length is 6 bytes
d8f365
+to allow them to be replaced with @samp{lfence; call *%r8-r15} or
d8f365
+@samp{lfence; jmp *%r8-r15} at run-time.
d8f365
+
d8f365
 @end table
d8f365
 
d8f365
 These @samp{-m} switches are supported in addition to the above
d8f365
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-1.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-1.c
d8f365
new file mode 100644
d8f365
index 00000000000..db2f3416823
d8f365
--- /dev/null
d8f365
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-1.c
d8f365
@@ -0,0 +1,14 @@
d8f365
+/* { dg-do compile { target { ! ia32 } } } */
d8f365
+/* { dg-options "-O2 -ffixed-rax -ffixed-rbx -ffixed-rcx -ffixed-rdx -ffixed-rdi -ffixed-rsi -mindirect-branch-cs-prefix -mindirect-branch=thunk-extern" } */
d8f365
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
d8f365
+
d8f365
+extern void (*fptr) (void);
d8f365
+
d8f365
+void
d8f365
+foo (void)
d8f365
+{
d8f365
+  fptr ();
d8f365
+}
d8f365
+
d8f365
+/* { dg-final { scan-assembler-times "jmp\[ \t\]+_?__x86_indirect_thunk_r\[0-9\]+" 1 } } */
d8f365
+/* { dg-final { scan-assembler-times "\tcs" 1 } } */
d8f365
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-2.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-2.c
d8f365
new file mode 100644
d8f365
index 00000000000..adfc39a49d4
d8f365
--- /dev/null
d8f365
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-2.c
d8f365
@@ -0,0 +1,15 @@
d8f365
+/* { dg-do compile { target { ! ia32 } } } */
d8f365
+/* { dg-options "-O2 -ffixed-rax -ffixed-rbx -ffixed-rcx -ffixed-rdx -ffixed-rdi -ffixed-rsi -mindirect-branch-cs-prefix -mindirect-branch=thunk-extern" } */
d8f365
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
d8f365
+
d8f365
+extern void (*bar) (void);
d8f365
+
d8f365
+int
d8f365
+foo (void)
d8f365
+{
d8f365
+  bar ();
d8f365
+  return 0;
d8f365
+}
d8f365
+
d8f365
+/* { dg-final { scan-assembler-times "call\[ \t\]+_?__x86_indirect_thunk_r\[0-9\]+" 1 } } */
d8f365
+/* { dg-final { scan-assembler-times "\tcs" 1 } } */
d8f365
-- 
d8f365
2.36.1
d8f365