Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect rdtscp instruction at runtime #51

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
meltdown
*.o
rdtscp.h
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions detect_rdtscp.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 2 additions & 23 deletions meltdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

#include <x86intrin.h>

#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"
Expand Down Expand Up @@ -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];
Expand Down