public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Andre Vieira (lists)" <Andre.SimoesDiasVieira@arm.com>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH 2/7, GCC, ARM, V8M] Handling ARMv8-M Security Extension's cmse_nonsecure_entry attribute
Date: Mon, 25 Jul 2016 13:21:00 -0000	[thread overview]
Message-ID: <5796123D.6020702@arm.com> (raw)
In-Reply-To: <5796116C.6010100@arm.com>

[-- Attachment #1: Type: text/plain, Size: 1044 bytes --]

This patch adds support for the ARMv8-M Security Extensions
'cmse_nonsecure_entry' attribute. In this patch we implement the
attribute handling and diagnosis around the attribute. See Section 5.4
of ARM®v8-M Security Extensions
(http://infocenter.arm.com/help/topic/com.arm.doc.ecm0359818/index.html).

*** gcc/ChangeLog ***
2016-07-25  Andre Vieira        <andre.simoesdiasvieira@arm.com>
            Thomas Preud'homme  <thomas.preudhomme@arm.com>

        * config/arm/arm.c (arm_handle_cmse_nonsecure_entry): New.
        (arm_attribute_table): Added cmse_nonsecure_entry
        (arm_compute_func_type): Handle cmse_nonsecure_entry.
        (cmse_func_args_or_return_in_stack): New.
        (arm_handle_cmse_nonsecure_entry): New.
        * config/arm/arm.h (ARM_FT_CMSE_ENTRY): New macro define.
        (IS_CMSE_ENTRY): Likewise.

*** gcc/testsuite/ChangeLog ***
2016-07-25  Andre Vieira        <andre.simoesdiasvieira@arm.com>
            Thomas Preud'homme  <thomas.preudhomme@arm.com>

        * gcc.target/arm/cmse/cmse-3.c: New.

[-- Attachment #2: diff2 --]
[-- Type: text/plain, Size: 7315 bytes --]

diff --git a/gcc/config/arm/arm.h b/gcc/config/arm/arm.h
index e3697bbcb425999db31ac2b4f47e14bb3f2ffa89..5307ec8f904230db5ea44150ef471d928926ab6d 100644
--- a/gcc/config/arm/arm.h
+++ b/gcc/config/arm/arm.h
@@ -1373,6 +1373,7 @@ enum reg_class
 #define ARM_FT_VOLATILE		(1 << 4) /* Does not return.  */
 #define ARM_FT_NESTED		(1 << 5) /* Embedded inside another func.  */
 #define ARM_FT_STACKALIGN	(1 << 6) /* Called with misaligned stack.  */
+#define ARM_FT_CMSE_ENTRY	(1 << 7) /* ARMv8-M non-secure entry function.  */
 
 /* Some macros to test these flags.  */
 #define ARM_FUNC_TYPE(t)	(t & ARM_FT_TYPE_MASK)
@@ -1381,6 +1382,7 @@ enum reg_class
 #define IS_NAKED(t)        	(t & ARM_FT_NAKED)
 #define IS_NESTED(t)       	(t & ARM_FT_NESTED)
 #define IS_STACKALIGN(t)       	(t & ARM_FT_STACKALIGN)
+#define IS_CMSE_ENTRY(t)	(t & ARM_FT_CMSE_ENTRY)
 
 
 /* Structure used to hold the function stack frame layout.  Offsets are
diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c
index 9903d9cd8c5ff68a2318a643bdf31cf48016eba4..11417ab3c2f7101866ee5d6b100913480e5c336e 100644
--- a/gcc/config/arm/arm.c
+++ b/gcc/config/arm/arm.c
@@ -134,6 +134,7 @@ static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
 #endif
+static tree arm_handle_cmse_nonsecure_entry (tree *, tree, tree, int, bool *);
 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
 static int arm_comp_type_attributes (const_tree, const_tree);
@@ -343,6 +344,9 @@ static const struct attribute_spec arm_attribute_table[] =
   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute,
     false },
 #endif
+  /* ARMv8-M Security Extensions support.  */
+  { "cmse_nonsecure_entry", 0, 0, true, false, false,
+    arm_handle_cmse_nonsecure_entry, false },
   { NULL,           0, 0, false, false, false, NULL, false }
 };
 \f
@@ -3633,6 +3637,9 @@ arm_compute_func_type (void)
   else
     type |= arm_isr_value (TREE_VALUE (a));
 
+  if (lookup_attribute ("cmse_nonsecure_entry", attr))
+    type |= ARM_FT_CMSE_ENTRY;
+
   return type;
 }
 
@@ -6634,6 +6641,110 @@ arm_handle_notshared_attribute (tree *node,
 }
 #endif
 
+/* This function returns true if a function with declaration FNDECL, name
+   NAME and type FNTYPE uses the stack to pass arguments or return variables
+   and false otherwise.  This is used for functions with the attributes
+   'cmse_nonsecure_call' or 'cmse_nonsecure_entry' and this function will issue
+   diagnostic messages if the stack is used.  */
+
+static bool
+cmse_func_args_or_return_in_stack (tree fndecl, tree name, tree fntype)
+{
+  function_args_iterator args_iter;
+  CUMULATIVE_ARGS args_so_far_v;
+  cumulative_args_t args_so_far;
+  bool first_param = true;
+  tree arg_type, prev_arg_type = NULL_TREE, ret_type;
+
+  /* Error out if any argument is passed on the stack.  */
+  arm_init_cumulative_args (&args_so_far_v, fntype, NULL_RTX, fndecl);
+  args_so_far = pack_cumulative_args (&args_so_far_v);
+  FOREACH_FUNCTION_ARGS (fntype, arg_type, args_iter)
+    {
+      rtx arg_rtx;
+      machine_mode arg_mode = TYPE_MODE (arg_type);
+
+      prev_arg_type = arg_type;
+      if (VOID_TYPE_P (arg_type))
+	continue;
+
+      if (!first_param)
+	arm_function_arg_advance (args_so_far, arg_mode, arg_type, true);
+      arg_rtx = arm_function_arg (args_so_far, arg_mode, arg_type, true);
+      if (!arg_rtx
+	  || arm_arg_partial_bytes (args_so_far, arg_mode, arg_type, true))
+	{
+	  error ("%qE attribute not available to functions with arguments "
+		 "passed on the stack", name);
+	  return true;
+	}
+      first_param = false;
+    }
+
+  /* Error out for variadic functions since we cannot control how many
+     arguments will be passed and thus stack could be used.  stdarg_p () is not
+     used for the checking to avoid browsing arguments twice.  */
+  if (prev_arg_type != NULL_TREE && !VOID_TYPE_P (prev_arg_type))
+    {
+      error ("%qE attribute not available to functions with variable number "
+	     "of arguments", name);
+      return true;
+    }
+
+  /* Error out if return value is passed on the stack.  */
+  ret_type = TREE_TYPE (fntype);
+  if (arm_return_in_memory (ret_type, fntype))
+    {
+      error ("%qE attribute not available to functions that return value on "
+	     "the stack", name);
+      return true;
+    }
+  return false;
+}
+
+/* Called upon detection of the use of the cmse_nonsecure_entry attribute, this
+   function will check whether the attribute is allowed here and will add the
+   attribute to the function declaration tree or otherwise issue a warning.  */
+
+static tree
+arm_handle_cmse_nonsecure_entry (tree *node, tree name,
+				 tree /* args */,
+				 int /* flags */,
+				 bool *no_add_attrs)
+{
+  tree fndecl;
+
+  if (!use_cmse)
+    {
+      *no_add_attrs = true;
+      return NULL_TREE;
+    }
+
+  /* Ignore attribute for function types.  */
+  if (TREE_CODE (*node) != FUNCTION_DECL)
+    {
+      warning (OPT_Wattributes, "%qE attribute only applies to functions",
+	       name);
+      *no_add_attrs = true;
+      return NULL_TREE;
+    }
+
+  fndecl = *node;
+
+  /* Warn for static linkage functions.  */
+  if (!TREE_PUBLIC (fndecl))
+    {
+      warning (OPT_Wattributes, "%qE attribute has no effect on functions "
+	       "with static linkage", name);
+      *no_add_attrs = true;
+      return NULL_TREE;
+    }
+
+  *no_add_attrs |= cmse_func_args_or_return_in_stack (fndecl, name,
+						TREE_TYPE (fndecl));
+  return NULL_TREE;
+}
+
 /* Return 0 if the attributes for two types are incompatible, 1 if they
    are compatible, and 2 if they are nearly compatible (which causes a
    warning to be generated).  */
diff --git a/gcc/testsuite/gcc.target/arm/cmse/cmse-3.c b/gcc/testsuite/gcc.target/arm/cmse/cmse-3.c
new file mode 100644
index 0000000000000000000000000000000000000000..2c2920e1dc310106d83203eb51e1a68a275d0152
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/cmse/cmse-3.c
@@ -0,0 +1,37 @@
+/* { dg-do compile } */
+/* { dg-options "-mcmse" }  */
+
+struct span {
+  int a, b;
+};
+struct span2 {
+  float a, b, c, d;
+};
+
+union test_union
+{
+  long long a;
+  int b;
+  struct span2 c;
+} test_union;
+
+void __attribute__ ((cmse_nonsecure_entry))
+foo (long long a, int b, long long c) {} /* { dg-error "not available to functions with arguments passed on the stack" } */
+
+void __attribute__ ((cmse_nonsecure_entry))
+bar (long long a, int b, struct span c) {} /* { dg-error "not available to functions with arguments passed on the stack" } */
+
+void __attribute__ ((cmse_nonsecure_entry))
+baz (int a, ...) {} /* { dg-error "not available to functions with variable number of arguments" } */
+
+struct span __attribute__ ((cmse_nonsecure_entry))
+qux (void) { /* { dg-error "not available to functions that return value on the stack" } */
+  struct span ret = {0, 0};
+  return ret;
+}
+
+void __attribute__ ((cmse_nonsecure_entry))
+norf (struct span2 a) {}
+
+void __attribute__ ((cmse_nonsecure_entry))
+foo2 (long long a, int b, union test_union c) {} /* { dg-error "not available to functions with arguments passed on the stack" } */

  parent reply	other threads:[~2016-07-25 13:21 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-25 13:17 [PATCH 0/7, GCC, V8M] ARMv8-M Security Extensions Andre Vieira (lists)
2016-07-25 13:20 ` [PATCH 1/7, GCC, ARM, V8M] Add support for ARMv8-M's Secure Extensions flag and intrinsics Andre Vieira (lists)
2016-08-24 11:00   ` [PATCHv2 " Andre Vieira (lists)
2016-10-25 16:26     ` Andre Vieira (lists)
2016-10-26  9:13       ` Kyrill Tkachov
2016-10-26 13:00         ` Kyrill Tkachov
2016-10-27  9:54           ` Andre Vieira (lists)
2016-10-27 10:01             ` Kyrill Tkachov
2016-12-05 11:27               ` [arm-embedded][committed][PATCH 1/7] " Andre Vieira (lists)
2016-07-25 13:21 ` Andre Vieira (lists) [this message]
2016-08-24 11:01   ` [PATCHv2 2/7, GCC, ARM, V8M] Handling ARMv8-M Security Extension's cmse_nonsecure_entry attribute Andre Vieira (lists)
2016-10-25 16:28     ` Andre Vieira (lists)
2016-10-26  9:33       ` Kyrill Tkachov
2016-10-26 16:28         ` Andre Vieira (lists)
2016-10-26 16:28           ` Kyrill Tkachov
2016-10-27  9:54             ` Andre Vieira (lists)
2016-10-27 10:19               ` Kyrill Tkachov
2016-12-05 11:31                 ` [arm-embedded][committed][PATCH 2/7] " Andre Vieira (lists)
2016-07-25 13:23 ` [PATCH 3/7, GCC, ARM, V8M] ARMv8-M Security Extension's cmse_nonsecure_entry: __acle_se label and bxns return Andre Vieira (lists)
2016-10-25 16:29   ` Andre Vieira (lists)
2016-10-26 10:03     ` Kyrill Tkachov
2016-10-27  9:55       ` Andre Vieira (lists)
2016-12-05 11:34         ` [arm-embedded][committed][PATCH 3/7] " Andre Vieira (lists)
2016-07-25 13:24 ` [PATCH 4/7, GCC, ARM, V8M] ARMv8-M Security Extension's cmse_nonsecure_entry: clear registers Andre Vieira (lists)
2016-08-24 11:01   ` [PATCHv2 " Andre Vieira (lists)
2016-10-25 16:29     ` Andre Vieira (lists)
2016-10-26 12:51       ` Kyrill Tkachov
2016-10-26 16:26         ` Andre Vieira (lists)
2016-10-26 16:30           ` Kyrill Tkachov
2016-10-27 10:00             ` Andre Vieira (lists)
2016-10-27 10:44               ` Kyrill Tkachov
2016-10-28 16:08                 ` Andre Vieira (lists)
2016-11-08 12:16                   ` Kyrill Tkachov
2016-11-23 11:52                     ` Andre Vieira (lists)
2016-11-30 15:32                       ` Andre Vieira (lists)
2016-11-30 17:22                         ` Kyrill Tkachov
2016-12-05 11:36                           ` [arm-embedded][committed][PATCH 4/7] " Andre Vieira (lists)
2016-10-26 14:14       ` [PATCHv2 4/7, GCC, ARM, V8M] " Kyrill Tkachov
2016-07-25 13:25 ` [PATCH 5/7, GCC, ARM, V8M] Handling ARMv8-M Security Extension's cmse_nonsecure_call attribute Andre Vieira (lists)
2016-08-24 11:01   ` [PATCHv2 " Andre Vieira (lists)
2016-10-25 16:29     ` Andre Vieira (lists)
2016-10-27 10:00       ` Andre Vieira (lists)
2016-11-04  9:30       ` Kyrill Tkachov
2016-11-30 12:05         ` [PATCHv3 " Andre Vieira (lists)
2016-11-30 17:22           ` Kyrill Tkachov
2016-12-05 11:38             ` [PATCHv3 5/7, GCC[arm-embedded][committed][PATCH 5/7] Handling ARMv8-M Security Extension's cmse_nonsecure_call attribute, " Andre Vieira (lists)
2016-07-25 13:26 ` [PATCH 6/7, GCC, ARM, V8M] ARMv8-M Security Extension's cmse_nonsecure_call: use __gnu_cmse_nonsecure_call Andre Vieira (lists)
2016-08-24 11:02   ` [PATCHv2 " Andre Vieira (lists)
2016-10-25 16:30     ` Andre Vieira (lists)
2016-10-27 10:01       ` Andre Vieira (lists)
2016-11-09 14:54         ` Andre Vieira (lists)
2016-11-11 11:52           ` Kyrill Tkachov
2016-11-11 16:15             ` Andre Vieira (lists)
2016-11-11 16:19               ` Kyrill Tkachov
2016-11-11 16:19                 ` Kyrill Tkachov
2016-11-23 11:54                   ` Andre Vieira (lists)
2016-12-02 13:36                     ` Andre Vieira (lists)
2016-12-02 13:42                       ` Kyrill Tkachov
2016-12-05 11:44                         ` [arm-embedded][committed][PATCH 6/7] " Andre Vieira (lists)
2016-07-25 13:29 ` [PATCH 7/7, GCC, ARM, V8M] Added support for ARMV8-M Security Extension cmse_nonsecure_caller intrinsic Andre Vieira (lists)
2016-08-24 11:02   ` Andre Vieira (lists)
2016-10-25 16:30     ` Andre Vieira (lists)
2016-11-09 10:27       ` Kyrill Tkachov
2016-11-09 14:53         ` Andre Vieira (lists)
2016-11-30 12:06           ` Andre Vieira (lists)
2016-12-05 11:46             ` [arm-embedded][committed][PATCH 7/7] " Andre Vieira (lists)
2016-08-08  4:20 ` [PATCH 0/7, GCC, V8M] ARMv8-M Security Extensions Sandra Loosemore
2016-08-09 12:01   ` Andre Vieira (lists)
2016-08-09 16:47     ` Sandra Loosemore
2016-08-10  8:09       ` Andre Vieira (lists)
2016-08-24 11:02         ` Andre Vieira (lists)
2016-10-24 16:14           ` Kyrill Tkachov

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=5796123D.6020702@arm.com \
    --to=andre.simoesdiasvieira@arm.com \
    --cc=gcc-patches@gcc.gnu.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).