Blame SOURCES/0030-shim-Rework-pause-functions-and-add-read_counter.patch

00e791
From 1b382ef850de5a6c59b192c146a0e8d898d2d961 Mon Sep 17 00:00:00 2001
00e791
From: Peter Jones <pjones@redhat.com>
00e791
Date: Tue, 23 Oct 2018 18:17:57 -0400
00e791
Subject: [PATCH 30/62] shim: Rework pause functions and add read_counter()
00e791
00e791
Signed-off-by: Peter Jones <pjones@redhat.com>
00e791
Upstream-commit-id: fc6b0bca84e
00e791
---
00e791
 shim.c             |   4 +-
00e791
 include/asm.h      |  59 +++++++++++++++++
00e791
 include/compiler.h | 156 +++++++++++++++++++++++++++++++++++++++++++++
00e791
 shim.h             |   1 +
00e791
 4 files changed, 217 insertions(+), 3 deletions(-)
00e791
 create mode 100644 include/asm.h
00e791
 create mode 100644 include/compiler.h
00e791
00e791
diff --git a/shim.c b/shim.c
00e791
index d4ed332f901..f69e69487fc 100644
00e791
--- a/shim.c
00e791
+++ b/shim.c
00e791
@@ -2543,16 +2543,14 @@ debug_hook(void)
00e791
 #if defined(__x86_64__) || defined(__i386__) || defined(__i686__)
00e791
 		if (x > 4294967294ULL)
00e791
 			break;
00e791
-		__asm__ __volatile__("pause");
00e791
 #elif defined(__aarch64__)
00e791
 		if (x > 1000)
00e791
 			break;
00e791
-		__asm__ __volatile__("wfi");
00e791
 #else
00e791
 		if (x > 12000)
00e791
 			break;
00e791
-		msleep(5000);
00e791
 #endif
00e791
+		pause();
00e791
 	}
00e791
 	x = 1;
00e791
 }
00e791
diff --git a/include/asm.h b/include/asm.h
00e791
new file mode 100644
00e791
index 00000000000..5e8f9ed9d7c
00e791
--- /dev/null
00e791
+++ b/include/asm.h
00e791
@@ -0,0 +1,59 @@
00e791
+/*
00e791
+ * asm.h
00e791
+ * Copyright 2018 Peter Jones <pjones@redhat.com>
00e791
+ */
00e791
+
00e791
+#ifndef SHIM_ASM_H_
00e791
+#define SHIM_ASM_H_
00e791
+
00e791
+#define __stringify_1(x...)     #x
00e791
+#define __stringify(x...)       __stringify_1(x)
00e791
+
00e791
+static inline uint64_t read_counter(void)
00e791
+{
00e791
+        uint64_t val;
00e791
+#if defined (__x86_64__)
00e791
+        unsigned long low, high;
00e791
+        __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high));
00e791
+        val = (low) | (high) << 32;
00e791
+#elif defined(__i386__) || defined(__i686__)
00e791
+        __asm__ __volatile__("rdtsc" : "=A" (val));
00e791
+#elif defined(__aarch64__)
00e791
+        __asm__ __volatile__ ("mrs %0, pmccntr_el0" : "=r" (val));
00e791
+#elif defined(__arm__)
00e791
+        __asm__ __volatile__ ("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
00e791
+#else
00e791
+#error unsupported arch
00e791
+#endif
00e791
+        return val;
00e791
+}
00e791
+
00e791
+#if defined(__x86_64__) || defined(__i386__) || defined(__i686__)
00e791
+static inline void pause(void)
00e791
+{
00e791
+	__asm__ __volatile__("pause");
00e791
+}
00e791
+#elif defined(__aarch64__)
00e791
+static inline void pause(void)
00e791
+{
00e791
+		__asm__ __volatile__("wfi");
00e791
+}
00e791
+#else
00e791
+static inline void pause(void)
00e791
+{
00e791
+        uint64_t a, b;
00e791
+        int x;
00e791
+        extern void msleep(unsigned long msecs);
00e791
+
00e791
+        a = read_counter();
00e791
+        for (x = 0; x < 1000; x++) {
00e791
+                msleep(1000);
00e791
+                b = read_counter();
00e791
+                if (a != b)
00e791
+                        break;
00e791
+        }
00e791
+}
00e791
+#endif
00e791
+
00e791
+#endif /* !SHIM_ASM_H_ */
00e791
+// vim:fenc=utf-8:tw=75:et
00e791
diff --git a/include/compiler.h b/include/compiler.h
00e791
new file mode 100644
00e791
index 00000000000..a2a0859379f
00e791
--- /dev/null
00e791
+++ b/include/compiler.h
00e791
@@ -0,0 +1,156 @@
00e791
+/*
00e791
+ * compiler.h
00e791
+ * Copyright 2019 Peter Jones <pjones@redhat.com>
00e791
+ */
00e791
+
00e791
+#ifndef COMPILER_H_
00e791
+#define COMPILER_H_
00e791
+
00e791
+#ifndef UNUSED
00e791
+#define UNUSED __attribute__((__unused__))
00e791
+#endif
00e791
+#ifndef HIDDEN
00e791
+#define HIDDEN __attribute__((__visibility__ ("hidden")))
00e791
+#endif
00e791
+#ifndef PUBLIC
00e791
+#define PUBLIC __attribute__((__visibility__ ("default")))
00e791
+#endif
00e791
+#ifndef DESTRUCTOR
00e791
+#define DESTRUCTOR __attribute__((destructor))
00e791
+#endif
00e791
+#ifndef CONSTRUCTOR
00e791
+#define CONSTRUCTOR __attribute__((constructor))
00e791
+#endif
00e791
+#ifndef ALIAS
00e791
+#define ALIAS(x) __attribute__((weak, alias (#x)))
00e791
+#endif
00e791
+#ifndef NONNULL
00e791
+#endif
00e791
+#define NONNULL(first, args...) __attribute__((__nonnull__(first, ## args)))
00e791
+#ifndef PRINTF
00e791
+#define PRINTF(first, args...) __attribute__((__format__(printf, first, ## args)))
00e791
+#endif
00e791
+#ifndef FLATTEN
00e791
+#define FLATTEN __attribute__((__flatten__))
00e791
+#endif
00e791
+#ifndef PACKED
00e791
+#define PACKED __attribute__((__packed__))
00e791
+#endif
00e791
+#ifndef VERSION
00e791
+#define VERSION(sym, ver) __asm__(".symver " # sym "," # ver)
00e791
+#endif
00e791
+#ifndef NORETURN
00e791
+#define NORETURN __attribute__((__noreturn__))
00e791
+#endif
00e791
+#ifndef ALIGNED
00e791
+#define ALIGNED(n) __attribute__((__aligned__(n)))
00e791
+#endif
00e791
+#ifndef CLEANUP_FUNC
00e791
+#define CLEANUP_FUNC(x) __attribute__((__cleanup__(x)))
00e791
+#endif
00e791
+#ifndef USED
00e791
+#define USED __attribute__((__used__))
00e791
+#endif
00e791
+#ifndef SECTION
00e791
+#define SECTION(x) __attribute__((__section__(x)))
00e791
+#endif
00e791
+#ifndef OPTIMIZE
00e791
+#define OPTIMIZE(x) __attribute__((__optimize__(x)))
00e791
+#endif
00e791
+
00e791
+#ifndef __CONCAT
00e791
+#define __CONCAT3(a, b, c) a ## b ## c
00e791
+#endif
00e791
+#ifndef CAT
00e791
+#define CAT(a, b) __CONCAT(a, b)
00e791
+#endif
00e791
+#ifndef CAT3
00e791
+#define CAT3(a, b, c) __CONCAT3(a, b, c)
00e791
+#endif
00e791
+#ifndef STRING
00e791
+#define STRING(x) __STRING(x)
00e791
+#endif
00e791
+
00e791
+#ifndef WRITE_ONCE
00e791
+#define WRITE_ONCE(var, val) \
00e791
+        (*((volatile typeof(val) *)(&(var))) = (val))
00e791
+#endif
00e791
+
00e791
+#ifndef READ_ONCE
00e791
+#define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
00e791
+#endif
00e791
+
00e791
+#ifndef likely
00e791
+#define likely(x)	__builtin_expect(!!(x), 1)
00e791
+#endif
00e791
+
00e791
+#ifndef unlikely
00e791
+#define unlikely(x)	__builtin_expect(!!(x), 0)
00e791
+#endif
00e791
+
00e791
+/* Are two types/vars the same type (ignoring qualifiers)? */
00e791
+#ifndef __same_type
00e791
+#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
00e791
+#endif
00e791
+
00e791
+/* Compile time object size, -1 for unknown */
00e791
+#ifndef __compiletime_object_size
00e791
+# define __compiletime_object_size(obj) -1
00e791
+#endif
00e791
+#ifndef __compiletime_warning
00e791
+# define __compiletime_warning(message)
00e791
+#endif
00e791
+#ifndef __compiletime_error
00e791
+# define __compiletime_error(message)
00e791
+#endif
00e791
+
00e791
+#ifndef __compiletime_assert
00e791
+#define __compiletime_assert(condition, msg, prefix, suffix)		\
00e791
+	do {								\
00e791
+		extern void prefix ## suffix(void) __compiletime_error(msg); \
00e791
+		if (!(condition))					\
00e791
+			prefix ## suffix();				\
00e791
+	} while (0)
00e791
+#endif
00e791
+
00e791
+#ifndef _compiletime_assert
00e791
+#define _compiletime_assert(condition, msg, prefix, suffix) \
00e791
+	__compiletime_assert(condition, msg, prefix, suffix)
00e791
+#endif
00e791
+
00e791
+/**
00e791
+ * compiletime_assert - break build and emit msg if condition is false
00e791
+ * @condition: a compile-time constant condition to check
00e791
+ * @msg:       a message to emit if condition is false
00e791
+ *
00e791
+ * In tradition of POSIX assert, this macro will break the build if the
00e791
+ * supplied condition is *false*, emitting the supplied error message if the
00e791
+ * compiler has support to do so.
00e791
+ */
00e791
+#ifndef compiletime_assert
00e791
+#define compiletime_assert(condition, msg) \
00e791
+	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
00e791
+#endif
00e791
+
00e791
+/**
00e791
+ * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied
00e791
+ *		      error message.
00e791
+ * @condition: the condition which the compiler should know is false.
00e791
+ *
00e791
+ * See BUILD_BUG_ON for description.
00e791
+ */
00e791
+#ifndef BUILD_BUG_ON_MSG
00e791
+#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
00e791
+#endif
00e791
+
00e791
+#ifndef ALIGN
00e791
+#define __ALIGN_MASK(x, mask)   (((x) + (mask)) & ~(mask))
00e791
+#define __ALIGN(x, a)           __ALIGN_MASK(x, (typeof(x))(a) - 1)
00e791
+#define ALIGN(x, a)             __ALIGN((x), (a))
00e791
+#endif
00e791
+#ifndef ALIGN_DOWN
00e791
+#define ALIGN_DOWN(x, a)        __ALIGN((x) - ((a) - 1), (a))
00e791
+#endif
00e791
+
00e791
+#endif /* !COMPILER_H_ */
00e791
+// vim:fenc=utf-8:tw=75:et
00e791
diff --git a/shim.h b/shim.h
00e791
index e4d40505f09..a0fa5a75e7e 100644
00e791
--- a/shim.h
00e791
+++ b/shim.h
00e791
@@ -97,6 +97,7 @@
00e791
 #define FALLBACK L"\\fb" EFI_ARCH L".efi"
00e791
 #define MOK_MANAGER L"\\mm" EFI_ARCH L".efi"
00e791
 
00e791
+#include "include/asm.h"
00e791
 #include "include/configtable.h"
00e791
 #include "include/console.h"
00e791
 #include "include/crypt_blowfish.h"
00e791
-- 
00e791
2.26.2
00e791