85359c
commit 5d7a77ede3e91948ee125bd82533d7e692543fff
85359c
Author: Jeff Law <law@redhat.com>
85359c
Date:   Mon Oct 2 13:43:01 2017 -0600
85359c
85359c
    aarch64 support
85359c
85359c
diff --git a/gcc/config/aarch64/.aarch64.c.rej.swp b/gcc/config/aarch64/.aarch64.c.rej.swp
85359c
new file mode 100644
85359c
index 00000000000..b899e21b855
85359c
Binary files /dev/null and b/gcc/config/aarch64/.aarch64.c.rej.swp differ
85359c
diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c
85359c
index cadf193cfcf..e08632ffa88 100644
85359c
--- a/gcc/config/aarch64/aarch64.c
85359c
+++ b/gcc/config/aarch64/aarch64.c
85359c
@@ -45,6 +45,8 @@
85359c
 #include "gimple.h"
85359c
 #include "optabs.h"
85359c
 #include "dwarf2.h"
85359c
+#include "params.h"
85359c
+#include "dumpfile.h"
85359c
 
85359c
 /* Classifies an address.
85359c
 
85359c
@@ -1696,7 +1698,14 @@ aarch64_output_probe_stack_range (rtx reg1, rtx reg2)
85359c
   output_asm_insn ("sub\t%0, %0, %1", xops);
85359c
 
85359c
   /* Probe at TEST_ADDR.  */
85359c
-  output_asm_insn ("str\txzr, [%0]", xops);
85359c
+  if (flag_stack_clash_protection)
85359c
+    {
85359c
+      gcc_assert (xops[0] == stack_pointer_rtx);
85359c
+      xops[1] = GEN_INT (PROBE_INTERVAL - 8);
85359c
+      output_asm_insn ("str\txzr, [%0, %1]", xops);
85359c
+    }
85359c
+  else
85359c
+    output_asm_insn ("str\txzr, [%0]", xops);
85359c
 
85359c
   /* Test if TEST_ADDR == LAST_ADDR.  */
85359c
   xops[1] = reg2;
85359c
@@ -2001,6 +2010,123 @@ aarch64_save_or_restore_callee_save_registers (HOST_WIDE_INT offset,
85359c
 				base_rtx, cfi_ops);
85359c
 }
85359c
 
85359c
+/* Allocate SIZE bytes of stack space using SCRATCH_REG as a scratch
85359c
+   register.  */
85359c
+
85359c
+static void
85359c
+aarch64_allocate_and_probe_stack_space (int scratchreg, HOST_WIDE_INT size)
85359c
+{
85359c
+  HOST_WIDE_INT probe_interval
85359c
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL);
85359c
+  HOST_WIDE_INT guard_size
85359c
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE);
85359c
+  HOST_WIDE_INT guard_used_by_caller = 1024;
85359c
+
85359c
+  /* SIZE should be large enough to require probing here.  ie, it
85359c
+     must be larger than GUARD_SIZE - GUARD_USED_BY_CALLER.
85359c
+
85359c
+     We can allocate GUARD_SIZE - GUARD_USED_BY_CALLER as a single chunk
85359c
+     without any probing.  */
85359c
+  gcc_assert (size >= guard_size - guard_used_by_caller);
85359c
+  aarch64_sub_sp (scratchreg, guard_size - guard_used_by_caller, true);
85359c
+  HOST_WIDE_INT orig_size = size;
85359c
+  size -= (guard_size - guard_used_by_caller);
85359c
+
85359c
+  HOST_WIDE_INT rounded_size = size & -probe_interval;
85359c
+  HOST_WIDE_INT residual = size - rounded_size;
85359c
+
85359c
+  /* We can handle a small number of allocations/probes inline.  Otherwise
85359c
+     punt to a loop.  */
85359c
+  if (rounded_size && rounded_size <= 4 * probe_interval)
85359c
+    {
85359c
+      /* We don't use aarch64_sub_sp here because we don't want to
85359c
+	 repeatedly load SCRATCHREG.  */
85359c
+      rtx scratch_rtx = gen_rtx_REG (Pmode, scratchreg);
85359c
+      if (probe_interval > ARITH_FACTOR)
85359c
+	emit_move_insn (scratch_rtx, GEN_INT (-probe_interval));
85359c
+      else
85359c
+	scratch_rtx = GEN_INT (-probe_interval);
85359c
+
85359c
+      for (HOST_WIDE_INT i = 0; i < rounded_size; i += probe_interval)
85359c
+	{
85359c
+	  rtx insn = emit_insn (gen_add2_insn (stack_pointer_rtx, scratch_rtx));
85359c
+          add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
85359c
+
85359c
+	  if (probe_interval > ARITH_FACTOR)
85359c
+	    {
85359c
+	      RTX_FRAME_RELATED_P (insn) = 1;
85359c
+	      rtx adj = plus_constant (Pmode, stack_pointer_rtx, -probe_interval);
85359c
+	      add_reg_note (insn, REG_CFA_ADJUST_CFA,
85359c
+			    gen_rtx_SET (VOIDmode, stack_pointer_rtx, adj));
85359c
+	    }
85359c
+
85359c
+	  emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
85359c
+					   (probe_interval
85359c
+					    - GET_MODE_SIZE (word_mode))));
85359c
+	  emit_insn (gen_blockage ());
85359c
+	}
85359c
+      dump_stack_clash_frame_info (PROBE_INLINE, size != rounded_size);
85359c
+    }
85359c
+  else if (rounded_size)
85359c
+    {
85359c
+      /* Compute the ending address.  */
85359c
+      rtx temp = gen_rtx_REG (word_mode, scratchreg);
85359c
+      emit_move_insn (temp, GEN_INT (-rounded_size));
85359c
+      rtx insn = emit_insn (gen_add3_insn (temp, stack_pointer_rtx, temp));
85359c
+
85359c
+      /* For the initial allocation, we don't have a frame pointer
85359c
+	 set up, so we always need CFI notes.  If we're doing the
85359c
+	 final allocation, then we may have a frame pointer, in which
85359c
+	 case it is the CFA, otherwise we need CFI notes.
85359c
+
85359c
+	 We can determine which allocation we are doing by looking at
85359c
+	 the temporary register.  IP0 is the initial allocation, IP1
85359c
+	 is the final allocation.  */
85359c
+      if (scratchreg == IP0_REGNUM || !frame_pointer_needed)
85359c
+	{
85359c
+	  /* We want the CFA independent of the stack pointer for the
85359c
+	     duration of the loop.  */
85359c
+	  add_reg_note (insn, REG_CFA_DEF_CFA,
85359c
+			plus_constant (Pmode, temp,
85359c
+				       (rounded_size + (orig_size - size))));
85359c
+	  RTX_FRAME_RELATED_P (insn) = 1;
85359c
+	}
85359c
+
85359c
+      /* This allocates and probes the stack.
85359c
+
85359c
+	 It also probes at a 4k interval regardless of the value of
85359c
+	 PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL.  */
85359c
+      insn = emit_insn (gen_probe_stack_range (stack_pointer_rtx,
85359c
+					       stack_pointer_rtx, temp));
85359c
+
85359c
+      /* Now reset the CFA register if needed.  */
85359c
+      if (scratchreg == IP0_REGNUM || !frame_pointer_needed)
85359c
+	{
85359c
+	  add_reg_note (insn, REG_CFA_DEF_CFA,
85359c
+			plus_constant (Pmode, stack_pointer_rtx,
85359c
+				       (rounded_size + (orig_size - size))));
85359c
+	  RTX_FRAME_RELATED_P (insn) = 1;
85359c
+	}
85359c
+
85359c
+      emit_insn (gen_blockage ());
85359c
+      dump_stack_clash_frame_info (PROBE_LOOP, size != rounded_size);
85359c
+    }
85359c
+  else
85359c
+    dump_stack_clash_frame_info (PROBE_INLINE, size != rounded_size);
85359c
+
85359c
+  /* Handle any residuals.
85359c
+     Note that any residual must be probed.  */
85359c
+  if (residual)
85359c
+    {
85359c
+      aarch64_sub_sp (scratchreg, residual, true);
85359c
+      add_reg_note (get_last_insn (), REG_STACK_CHECK, const0_rtx);
85359c
+      emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
85359c
+				       (residual - GET_MODE_SIZE (word_mode))));
85359c
+      emit_insn (gen_blockage ());
85359c
+    }
85359c
+  return;
85359c
+}
85359c
+
85359c
 /* AArch64 stack frames generated by this compiler look like:
85359c
 
85359c
 	+-------------------------------+
85359c
@@ -2073,6 +2199,44 @@ aarch64_expand_prologue (void)
85359c
 	       - original_frame_size
85359c
 	       - cfun->machine->frame.saved_regs_size);
85359c
 
85359c
+  /* We do not fully protect aarch64 against stack clash style attacks
85359c
+     as doing so would be prohibitively expensive with less utility over
85359c
+     time as newer compilers are deployed.
85359c
+
85359c
+     We assume the guard is at least 64k.  Furthermore, we assume that
85359c
+     the caller has not pushed the stack pointer more than 1k into
85359c
+     the guard.  A caller that pushes the stack pointer than 1k into
85359c
+     the guard is considered invalid.
85359c
+
85359c
+     Note that the caller's ability to push the stack pointer into the
85359c
+     guard is a function of the number and size of outgoing arguments and/or
85359c
+     dynamic stack allocations due to the mandatory save of the link register
85359c
+     in the caller's frame.
85359c
+
85359c
+     With those assumptions the callee can allocate up to 63k of stack
85359c
+     space without probing.
85359c
+
85359c
+     When probing is needed, we emit a probe at the start of the prologue
85359c
+     and every PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes thereafter.
85359c
+
85359c
+     We have to track how much space has been allocated, but we do not
85359c
+     track stores into the stack as implicit probes except for the
85359c
+     fp/lr store.  */
85359c
+  HOST_WIDE_INT guard_size
85359c
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE);
85359c
+  HOST_WIDE_INT guard_used_by_caller = 1024;
85359c
+  HOST_WIDE_INT final_adjust = crtl->outgoing_args_size;
85359c
+  HOST_WIDE_INT initial_adjust = frame_size;
85359c
+
85359c
+  if (flag_stack_clash_protection)
85359c
+    {
85359c
+      if (initial_adjust == 0)
85359c
+	dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
85359c
+      else if (offset < guard_size - guard_used_by_caller
85359c
+	       && final_adjust < guard_size - guard_used_by_caller)
85359c
+	dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
85359c
+    }
85359c
+
85359c
   /* Store pairs and load pairs have a range only -512 to 504.  */
85359c
   if (offset >= 512)
85359c
     {
85359c
@@ -2089,7 +2253,10 @@ aarch64_expand_prologue (void)
85359c
       frame_size -= (offset + crtl->outgoing_args_size);
85359c
       fp_offset = 0;
85359c
 
85359c
-      if (frame_size >= 0x1000000)
85359c
+      if (flag_stack_clash_protection
85359c
+	  && frame_size >= guard_size - guard_used_by_caller)
85359c
+	aarch64_allocate_and_probe_stack_space (IP0_REGNUM, frame_size);
85359c
+      else if (frame_size >= 0x1000000)
85359c
 	{
85359c
 	  rtx op0 = gen_rtx_REG (Pmode, IP0_REGNUM);
85359c
 	  emit_move_insn (op0, GEN_INT (-frame_size));
85359c
@@ -2206,10 +2373,30 @@ aarch64_expand_prologue (void)
85359c
     {
85359c
       if (crtl->outgoing_args_size > 0)
85359c
 	{
85359c
-	  insn = emit_insn (gen_add2_insn
85359c
-			    (stack_pointer_rtx,
85359c
-			     GEN_INT (- crtl->outgoing_args_size)));
85359c
-	  RTX_FRAME_RELATED_P (insn) = 1;
85359c
+	  if (flag_stack_clash_protection)
85359c
+	    {
85359c
+	      /* First probe if the final adjustment is larger than the
85359c
+		 guard size less the amount of guard reserved for use by
85359c
+		 the caller's outgoing args.  */
85359c
+	      if (final_adjust >= guard_size - guard_used_by_caller)
85359c
+		aarch64_allocate_and_probe_stack_space (IP1_REGNUM,
85359c
+						        final_adjust);
85359c
+	      else
85359c
+		aarch64_sub_sp (IP1_REGNUM, final_adjust, !frame_pointer_needed);
85359c
+
85359c
+	      /* We must also probe if the final adjustment is larger than the
85359c
+		 guard that is assumed used by the caller.  This may be
85359c
+		 sub-optimal.  */
85359c
+	      if (final_adjust >= guard_used_by_caller)
85359c
+		{
85359c
+		  if (dump_file)
85359c
+		    fprintf (dump_file,
85359c
+			     "Stack clash aarch64 large outgoing arg, probing\n");
85359c
+		  emit_stack_probe (stack_pointer_rtx);
85359c
+		}
85359c
+	    }
85359c
+	  else
85359c
+	    aarch64_sub_sp (IP1_REGNUM, final_adjust, !frame_pointer_needed);
85359c
 	}
85359c
     }
85359c
 }
85359c
@@ -5088,6 +5275,12 @@ aarch64_override_options (void)
85359c
 #endif
85359c
     }
85359c
 
85359c
+  /* We assume the guard page is 64k.  */
85359c
+  maybe_set_param_value (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE,
85359c
+			 16,
85359c
+			 global_options.x_param_values,
85359c
+			 global_options_set.x_param_values);
85359c
+
85359c
   aarch64_override_options_after_change ();
85359c
 }
85359c
 
85359c
@@ -8161,6 +8354,28 @@ aarch64_vectorize_vec_perm_const_ok (enum machine_mode vmode,
85359c
   return ret;
85359c
 }
85359c
 
85359c
+/* It has been decided that to allow up to 1kb of outgoing argument
85359c
+   space to be allocated w/o probing.  If more than 1kb of outgoing
85359c
+   argment space is allocated, then it must be probed and the last
85359c
+   probe must occur no more than 1kbyte away from the end of the
85359c
+   allocated space.
85359c
+
85359c
+   This implies that the residual part of an alloca allocation may
85359c
+   need probing in cases where the generic code might not otherwise
85359c
+   think a probe is needed.
85359c
+
85359c
+   This target hook returns TRUE when allocating RESIDUAL bytes of
85359c
+   alloca space requires an additional probe, otherwise FALSE is
85359c
+   returned.  */
85359c
+
85359c
+static bool
85359c
+aarch64_stack_clash_protection_final_dynamic_probe (rtx residual)
85359c
+{
85359c
+  return (residual == CONST0_RTX (Pmode)
85359c
+	  || GET_CODE (residual) != CONST_INT
85359c
+	  || INTVAL (residual) >= 1024);
85359c
+}
85359c
+
85359c
 #undef TARGET_ADDRESS_COST
85359c
 #define TARGET_ADDRESS_COST aarch64_address_cost
85359c
 
85359c
@@ -8378,6 +8593,10 @@ aarch64_vectorize_vec_perm_const_ok (enum machine_mode vmode,
85359c
 #undef TARGET_FIXED_CONDITION_CODE_REGS
85359c
 #define TARGET_FIXED_CONDITION_CODE_REGS aarch64_fixed_condition_code_regs
85359c
 
85359c
+#undef TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE
85359c
+#define TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE \
85359c
+  aarch64_stack_clash_protection_final_dynamic_probe
85359c
+
85359c
 struct gcc_target targetm = TARGET_INITIALIZER;
85359c
 
85359c
 #include "gt-aarch64.h"
85359c
diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
85359c
index a085c6acaf5..5485a5f70b1 100644
85359c
--- a/gcc/config/aarch64/aarch64.md
85359c
+++ b/gcc/config/aarch64/aarch64.md
85359c
@@ -3401,7 +3401,7 @@
85359c
 )
85359c
 
85359c
 (define_insn "probe_stack_range"
85359c
-  [(set (match_operand:DI 0 "register_operand" "=r")
85359c
+  [(set (match_operand:DI 0 "register_operand" "=rk")
85359c
 	(unspec_volatile:DI [(match_operand:DI 1 "register_operand" "0")
85359c
 			     (match_operand:DI 2 "register_operand" "r")]
85359c
 			      UNSPECV_PROBE_STACK_RANGE))]
85359c
diff --git a/gcc/testsuite/gcc.target/aarch64/stack-check-12.c b/gcc/testsuite/gcc.target/aarch64/stack-check-12.c
85359c
new file mode 100644
85359c
index 00000000000..2ce38483b6b
85359c
--- /dev/null
85359c
+++ b/gcc/testsuite/gcc.target/aarch64/stack-check-12.c
85359c
@@ -0,0 +1,20 @@
85359c
+/* { dg-do compile } */
85359c
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
85359c
+/* { dg-require-effective-target supports_stack_clash_protection } */
85359c
+
85359c
+extern void arf (unsigned long int *, unsigned long int *);
85359c
+void
85359c
+frob ()
85359c
+{
85359c
+  unsigned long int num[1000];
85359c
+  unsigned long int den[1000];
85359c
+  arf (den, num);
85359c
+}
85359c
+
85359c
+/* This verifies that the scheduler did not break the dependencies
85359c
+   by adjusting the offsets within the probe and that the scheduler
85359c
+   did not reorder around the stack probes.  */
85359c
+/* { dg-final { scan-assembler-times "sub\\tsp, sp, #4096\\n\\tstr\\txzr, .sp, 4088." 3 } } */
85359c
+
85359c
+
85359c
+
85359c
diff --git a/gcc/testsuite/gcc.target/aarch64/stack-check-13.c b/gcc/testsuite/gcc.target/aarch64/stack-check-13.c
85359c
new file mode 100644
85359c
index 00000000000..d8886835989
85359c
--- /dev/null
85359c
+++ b/gcc/testsuite/gcc.target/aarch64/stack-check-13.c
85359c
@@ -0,0 +1,28 @@
85359c
+/* { dg-do compile } */
85359c
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
85359c
+/* { dg-require-effective-target supports_stack_clash_protection } */
85359c
+
85359c
+#define ARG32(X) X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X
85359c
+#define ARG192(X) ARG32(X),ARG32(X),ARG32(X),ARG32(X),ARG32(X),ARG32(X)
85359c
+void out1(ARG192(__int128));
85359c
+int t1(int);
85359c
+
85359c
+int t3(int x)
85359c
+{
85359c
+  if (x < 1000)
85359c
+    return t1 (x) + 1;
85359c
+
85359c
+  out1 (ARG192(1));
85359c
+  return 0;
85359c
+}
85359c
+
85359c
+
85359c
+
85359c
+/* This test creates a large (> 1k) outgoing argument area that needs
85359c
+   to be probed.  We don't test the exact size of the space or the
85359c
+   exact offset to make the test a little less sensitive to trivial
85359c
+   output changes.  */
85359c
+/* { dg-final { scan-assembler-times "sub\\tsp, sp, #....\\n\\tstr\\txzr, \\\[sp" 1 } } */
85359c
+
85359c
+
85359c
+
85359c
diff --git a/gcc/testsuite/gcc.target/aarch64/stack-check-14.c b/gcc/testsuite/gcc.target/aarch64/stack-check-14.c
85359c
new file mode 100644
85359c
index 00000000000..59ffe01376d
85359c
--- /dev/null
85359c
+++ b/gcc/testsuite/gcc.target/aarch64/stack-check-14.c
85359c
@@ -0,0 +1,25 @@
85359c
+/* { dg-do compile } */
85359c
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
85359c
+/* { dg-require-effective-target supports_stack_clash_protection } */
85359c
+
85359c
+int t1(int);
85359c
+
85359c
+int t2(int x)
85359c
+{
85359c
+  char *p = __builtin_alloca (4050);
85359c
+  x = t1 (x);
85359c
+  return p[x];
85359c
+}
85359c
+
85359c
+
85359c
+/* This test has a constant sized alloca that is smaller than the
85359c
+   probe interval.  But it actually requires two probes instead
85359c
+   of one because of the optimistic assumptions we made in the
85359c
+   aarch64 prologue code WRT probing state. 
85359c
+
85359c
+   The form can change quite a bit so we just check for two
85359c
+   probes without looking at the actual address.  */
85359c
+/* { dg-final { scan-assembler-times "str\\txzr," 2 } } */
85359c
+
85359c
+
85359c
+
85359c
diff --git a/gcc/testsuite/gcc.target/aarch64/stack-check-15.c b/gcc/testsuite/gcc.target/aarch64/stack-check-15.c
85359c
new file mode 100644
85359c
index 00000000000..e06db6dc2f0
85359c
--- /dev/null
85359c
+++ b/gcc/testsuite/gcc.target/aarch64/stack-check-15.c
85359c
@@ -0,0 +1,24 @@
85359c
+/* { dg-do compile } */
85359c
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
85359c
+/* { dg-require-effective-target supports_stack_clash_protection } */
85359c
+
85359c
+int t1(int);
85359c
+
85359c
+int t2(int x)
85359c
+{
85359c
+  char *p = __builtin_alloca (x);
85359c
+  x = t1 (x);
85359c
+  return p[x];
85359c
+}
85359c
+
85359c
+
85359c
+/* This test has a variable sized alloca.  It requires 3 probes.
85359c
+   One in the loop, one for the residual and at the end of the
85359c
+   alloca area. 
85359c
+
85359c
+   The form can change quite a bit so we just check for two
85359c
+   probes without looking at the actual address.  */
85359c
+/* { dg-final { scan-assembler-times "str\\txzr," 3 } } */
85359c
+
85359c
+
85359c
+
85359c
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
85359c
index aba99513ed0..a8451c98b08 100644
85359c
--- a/gcc/testsuite/lib/target-supports.exp
85359c
+++ b/gcc/testsuite/lib/target-supports.exp
85359c
@@ -5420,14 +5420,9 @@ proc check_effective_target_autoincdec { } {
85359c
 # 
85359c
 proc check_effective_target_supports_stack_clash_protection { } {
85359c
 
85359c
-   # Temporary until the target bits are fully ACK'd.
85359c
-#  if { [istarget aarch*-*-*] } {
85359c
-#	return 1
85359c
-#  }
85359c
-
85359c
     if { [istarget x86_64-*-*] || [istarget i?86-*-*] 
85359c
 	  || [istarget powerpc*-*-*] || [istarget rs6000*-*-*]
85359c
-	  || [istarget s390*-*-*] } {
85359c
+	  || [istarget aarch64*-**] || [istarget s390*-*-*] } {
85359c
 	return 1
85359c
     }
85359c
   return 0