security: Add tracing for SecureOS measurements
Charles Tan [Fri, 26 Oct 2012 06:09:37 +0000 (23:09 -0700)]
Add new tracepoint events for SecureOS-related tracing.
The timestamp traces measure the number of cycles taken
to switch between normal world and secure world using the
CP15 cycle counter.

Bug 1042455

Change-Id: Ia7f0718a0fcc399875a175670b80dfe33f79b95e
Signed-off-by: Charles Tan <ctan@nvidia.com>
Reviewed-on: http://git-master/r/159367
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Antti Miettinen <amiettinen@nvidia.com>
Tested-by: Antti Miettinen <amiettinen@nvidia.com>
Reviewed-by: Bharat Nihalani <bnihalani@nvidia.com>

include/trace/events/nvsecurity.h [new file with mode: 0644]
security/tf_driver/tf_comm_tz.c
security/tf_driver/tf_device.c
security/tf_driver/tf_util.c
security/tf_driver/tf_util.h

diff --git a/include/trace/events/nvsecurity.h b/include/trace/events/nvsecurity.h
new file mode 100644 (file)
index 0000000..25e90a9
--- /dev/null
@@ -0,0 +1,152 @@
+/*
+ * include/trace/events/nvsecurity.h
+ *
+ * Security event logging to ftrace.
+ *
+ * Copyright (c) 2012, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM nvsecurity
+
+#if !defined(_TRACE_NVSECURITY_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_NVSECURITY_H
+
+#include <linux/ktime.h>
+#include <linux/tracepoint.h>
+
+extern u32 notrace tegra_read_usec_raw(void);
+
+DECLARE_EVENT_CLASS(usec_profiling,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state),
+
+       TP_STRUCT__entry(
+               __field(u32, counter)
+               __field(u32, state)
+       ),
+
+       TP_fast_assign(
+               __entry->counter = tegra_read_usec_raw();
+               __entry->state = state;
+       ),
+
+       TP_printk("counter=%lu, state=%lu",
+               (unsigned long)__entry->counter,
+               (unsigned long)__entry->state
+       )
+);
+
+DEFINE_EVENT(usec_profiling, smc_sleep_cpu,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state)
+);
+
+DEFINE_EVENT(usec_profiling, smc_sleep_core,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state)
+);
+
+DEFINE_EVENT(usec_profiling, smc_init_cache,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state)
+);
+
+DEFINE_EVENT(usec_profiling, smc_wake,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state)
+);
+
+DEFINE_EVENT(usec_profiling, secureos_init,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state)
+);
+
+extern u32 notrace tegra_read_cycle(void);
+
+DECLARE_EVENT_CLASS(cycle_profiling,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state),
+
+       TP_STRUCT__entry(
+               __field(u32, counter)
+               __field(u32, state)
+       ),
+
+       TP_fast_assign(
+               __entry->counter = tegra_read_cycle();
+               __entry->state = state;
+       ),
+
+       TP_printk("counter=%lu, state=%lu",
+               (unsigned long)__entry->counter,
+               (unsigned long)__entry->state
+       )
+);
+
+DEFINE_EVENT(cycle_profiling, invoke_client_command,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state)
+);
+
+DEFINE_EVENT(cycle_profiling, smc_generic_call,
+
+       TP_PROTO(unsigned long state),
+
+       TP_ARGS(state)
+);
+
+/* This file can get included multiple times, TRACE_HEADER_MULTI_READ at top */
+#ifndef _NVSEC_EVENT_AVOID_DOUBLE_DEFINING
+#define _NVSEC_EVENT_AVOID_DOUBLE_DEFINING
+
+enum {
+       NVSEC_SMC_START,
+       NVSEC_SMC_DONE
+};
+
+enum {
+       NVSEC_INVOKE_CMD_START,
+       NVSEC_INVOKE_CMD_DONE
+};
+
+enum {
+       NVSEC_SUSPEND_EXIT_START,
+       NVSEC_SUSPEND_EXIT_DONE
+};
+
+#endif
+#endif /* _TRACE_NVSECURITY_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
index 4628f24..a0e9941 100644 (file)
@@ -2,6 +2,8 @@
  * Copyright (c) 2011 Trusted Logic S.A.
  * All Rights Reserved.
  *
+ * Copyright (C) 2011-2012 NVIDIA Corporation.
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * version 2 as published by the Free Software Foundation.
@@ -27,6 +29,8 @@
 #include <linux/vmalloc.h>
 #include <linux/jiffies.h>
 
+#include <trace/events/nvsecurity.h>
+
 #include "tf_defs.h"
 #include "tf_comm.h"
 #include "tf_protocol.h"
@@ -63,6 +67,8 @@ static inline void tf_smc_generic_call(
                dprintk(KERN_ERR "sched_setaffinity #1 -> 0x%lX", ret);
 #endif
 
+       trace_smc_generic_call(NVSEC_SMC_START);
+
        __asm__ volatile(
                "mov r0, %2\n"
                "mov r1, %3\n"
@@ -78,6 +84,8 @@ static inline void tf_smc_generic_call(
                  "r" (generic_smc->reg4)
                : "r0", "r1", "r2", "r3", "r4");
 
+       trace_smc_generic_call(NVSEC_SMC_DONE);
+
 #ifdef CONFIG_SMP
                ret = sched_setaffinity(0, &saved_cpu_mask);
                if (ret != 0)
index 1bdf5ac..51dbb37 100644 (file)
@@ -2,6 +2,8 @@
  * Copyright (c) 2011 Trusted Logic S.A.
  * All Rights Reserved.
  *
+ * Copyright (C) 2011-2012 NVIDIA Corporation.
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * version 2 as published by the Free Software Foundation.
@@ -46,6 +48,9 @@
 
 #include "s_version.h"
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/nvsecurity.h>
+
 /*----------------------------------------------------------------------------
  * Forward Declarations
  *----------------------------------------------------------------------------*/
@@ -733,8 +738,10 @@ static long tf_device_ioctl(struct file *file, unsigned int ioctl_num,
                        break;
 
                case TF_MESSAGE_TYPE_INVOKE_CLIENT_COMMAND:
+                       trace_invoke_client_command(NVSEC_INVOKE_CMD_START);
                        result = tf_invoke_client_command(connection,
                                &command, &answer);
+                       trace_invoke_client_command(NVSEC_INVOKE_CMD_DONE);
                        break;
 
                case TF_MESSAGE_TYPE_CANCEL_CLIENT_COMMAND:
index 78f90bf..936f8e1 100644 (file)
@@ -2,6 +2,8 @@
  * Copyright (c) 2011 Trusted Logic S.A.
  * All Rights Reserved.
  *
+ * Copyright (C) 2011-2012 NVIDIA Corporation.
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * version 2 as published by the Free Software Foundation.
 #include "tf_util.h"
 
 /*----------------------------------------------------------------------------
+ * Tegra-specific routines
+ *----------------------------------------------------------------------------*/
+
+u32 notrace tegra_read_cycle(void)
+{
+       u32 cycle_count;
+
+       asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(cycle_count));
+
+       return cycle_count;
+}
+
+/*----------------------------------------------------------------------------
  * Debug printing routines
  *----------------------------------------------------------------------------*/
 #ifdef CONFIG_TF_DRIVER_DEBUG_SUPPORT
@@ -1139,5 +1154,3 @@ void internal_page_cache_release(struct page *page)
 
        page_cache_release(page);
 }
-
-
index 14bc789..2fabf01 100644 (file)
@@ -2,6 +2,8 @@
  * Copyright (c) 2011 Trusted Logic S.A.
  * All Rights Reserved.
  *
+ * Copyright (C) 2011-2012 NVIDIA Corporation.
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * version 2 as published by the Free Software Foundation.
 #include "tf_defs.h"
 
 /*----------------------------------------------------------------------------
+ * Tegra-specific routines
+ *----------------------------------------------------------------------------*/
+
+u32 notrace tegra_read_cycle(void);
+
+/*----------------------------------------------------------------------------
  * Debug printing routines
  *----------------------------------------------------------------------------*/
 
@@ -119,4 +127,3 @@ int internal_get_user_pages(
 void internal_get_page(struct page *page);
 void internal_page_cache_release(struct page *page);
 #endif  /* __TF_UTIL_H__ */
-