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

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