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