[sheepdog] [PATCH 1/2] add some macros to check cpuinfo

Liu Yuan namei.unix at gmail.com
Thu Sep 5 12:47:51 CEST 2013


This is a preparation patch.

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 include/compiler.h |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/include/compiler.h b/include/compiler.h
index f495819..8277367 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -16,6 +16,7 @@
 #include <unistd.h>
 #include <time.h>
 #include <signal.h>
+#include <stdint.h>
 
 #include "config.h"
 
@@ -24,6 +25,8 @@
 
 #define __packed __attribute((packed))
 
+#define asmlinkage  __attribute__((regparm(0)))
+
 #define __printf(a, b) __attribute__((format(printf, a, b)))
 
 /* Force a compilation error if the condition is true */
@@ -107,4 +110,59 @@ static inline int fallocate(int fd, int mode, __off_t offset, __off_t len)
 }
 #endif
 
+#define X86_FEATURE_SSSE3	(4 * 32 + 9) /* Supplemental SSE-3 */
+#define X86_FEATURE_OSXSAVE	(4 * 32 + 27) /* "" XSAVE enabled in the OS */
+#define X86_FEATURE_AVX	(4 * 32 + 28) /* Advanced Vector Extensions */
+
+#define XSTATE_FP	0x1
+#define XSTATE_SSE	0x2
+#define XSTATE_YMM	0x4
+
+#define XCR_XFEATURE_ENABLED_MASK	0x00000000
+
+#ifdef __x86_64__
+
+static inline int cpu_has(int flag)
+{
+	uint32_t eax, ebx, ecx, edx;
+
+	eax = (flag & 0x100) ? 7 :
+		(flag & 0x20) ? 0x80000001 : 1;
+	ecx = 0;
+
+	asm volatile("cpuid"
+		     : "+a" (eax), "=b" (ebx), "=d" (edx), "+c" (ecx));
+
+	return ((flag & 0x100 ? ebx :
+		 (flag & 0x80) ? ecx : edx) >> (flag & 31)) & 1;
+}
+
+static inline uint64_t xgetbv(uint32_t idx)
+{
+	uint32_t eax, edx;
+
+	asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
+		     : "=a" (eax), "=d" (edx)
+		     : "c" (idx));
+	return eax + ((uint64_t)edx << 32);
+}
+
+#else
+
+static inline int cpu_has(int flag)
+{
+	return 0;
+}
+
+static inline uint64_t xgetbv(uint32_t idx)
+{
+	return 0;
+}
+
+#endif /* __x86_64__ */
+
+#define cpu_has_ssse3           cpu_has(X86_FEATURE_SSSE3)
+#define cpu_has_avx		cpu_has(X86_FEATURE_AVX)
+#define cpu_has_osxsave		cpu_has(X86_FEATURE_OSXSAVE)
+
 #endif	/* SD_COMPILER_H */
-- 
1.7.9.5




More information about the sheepdog mailing list