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

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