tests/linux: make user code more like kernel code
Compile the module and user programs with similar CFLAGS, use "pause"
for cpu_relax(), and have a halfway decent ndelay. (Note linux's
default ndelay is udelay(), but at least it's a busyloop and not a
sleep).
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
diff --git a/tests/linux/Makefile b/tests/linux/Makefile
index c8f1e59..bbb51be 100644
--- a/tests/linux/Makefile
+++ b/tests/linux/Makefile
@@ -10,10 +10,42 @@
# overridden. Also these are used if we were called directly instead of from
# the top-level makefile.
HOSTCC ?= gcc
-HOSTCFLAGS ?= -Wall -Wno-char-subscripts -Wmissing-prototypes \
- -Wstrict-prototypes -O2 -fomit-frame-pointer
HOSTLD ?= ld
+# Rough attempt to match Akaros's userspace flags, plus turn off the annoying
+# warnings from more recent GCCs. Note that akaros's CFLAGS_USER has
+# omit-frame-pointer, but the libraries (parlib) don't.
+HOSTCFLAGS = -Wall -Werror -Wreturn-type \
+ -Wno-format -Wno-char-subscripts -Wno-unused -Wno-comment \
+ -std=gnu99 -fno-stack-protector -fgnu89-inline \
+ -O2 -fno-omit-frame-pointer -g
+
+# These are the flags Linux uses for modules that are applicable to userspace.
+# Note that this overloads the CFLAGS above. Just pick one based on whichever
+# comparison you're doing
+HOSTCFLAGS = \
+ -fno-strict-aliasing \
+ -fno-common \
+ -fshort-wchar \
+ -std=gnu99 \
+ -fno-PIE \
+ -m64 \
+ -falign-jumps=1 \
+ -falign-loops=1 \
+ -mskip-rax-setup \
+ -mtune=generic \
+ -mno-red-zone \
+ -funit-at-a-time \
+ -pipe \
+ -fno-asynchronous-unwind-tables \
+ -fno-delete-null-pointer-checks \
+ -O2 \
+ --param=allow-store-data-races=0 \
+ -fstack-protector-strong \
+ -fno-omit-frame-pointer \
+ -fno-optimize-sibling-calls \
+ -fno-var-tracking-assignments \
+ -g
USERDIR = ../../user
# override to our local obj/
@@ -71,12 +103,12 @@
$(OBJDIR)/%: $(tests_lddepends_c)
@echo + cc [LINUX_TESTS] $<
@mkdir -p $(@D)
- $(Q)$(HOSTCC) -static -O2 $< $(tests_c_deps) -o $@ $(tests_ldlibs)
+ $(Q)$(HOSTCC) -static -O2 $(HOSTCFLAGS) $< $(tests_c_deps) -o $@ $(tests_ldlibs)
$(OBJDIR)/%: $(sel_progs_lddepends_c)
@echo + cc [LINUX_TESTS] $<
@mkdir -p $(@D)
- $(Q)$(HOSTCC) -static -O2 $< $(tests_c_deps) -o $@ $(tests_ldlibs)
+ $(Q)$(HOSTCC) -static -O2 $(HOSTCFLAGS) $< $(tests_c_deps) -o $@ $(tests_ldlibs)
all: mods $(progs)
@:
diff --git a/tests/linux/misc-compat.h b/tests/linux/misc-compat.h
index bf9a04b..60cd80a 100644
--- a/tests/linux/misc-compat.h
+++ b/tests/linux/misc-compat.h
@@ -14,22 +14,29 @@
#include <sys/param.h> /* MIN/MAX */
#include <unistd.h>
+#include "../../user/parlib/include/parlib/tsc-compat.h"
-/* not quite, since akaros udelay is a busy wait */
-#define udelay(usec) usleep(usec)
-#define ndelay(nsec) \
-{ \
- struct timespec ts = {0, 0}; \
- ts.tv_nsec = (nsec); \
- nanosleep(&ts, 0); \
-}
-
-/* not quite a normal relax, which also pauses, but this works for all archs */
+/* arch-specific... */
static inline void cpu_relax(void)
{
- asm volatile("" : : : "memory");
+ asm volatile("pause" : : : "memory");
}
+static inline uint64_t ndelay(uint64_t nsec)
+{
+ uint64_t start, end, now;
+
+ start = read_tsc();
+ end = start + (get_tsc_freq() * nsec) / 1000000000;
+ do {
+ cpu_relax();
+ now = read_tsc();
+ } while (now < end || (now > start && end < start));
+ return tsc2nsec(now);
+}
+
+#define udelay(usec) ndelay(usec * 1000)
+
#define pthread_id() (pthread_self())
#define vcore_id() (-1)
diff --git a/tests/linux/modules/Kbuild b/tests/linux/modules/Kbuild
index 4517f23..ac38b41 100644
--- a/tests/linux/modules/Kbuild
+++ b/tests/linux/modules/Kbuild
@@ -1 +1,2 @@
+#CFLAGS_mcs.o += -fno-stack-protector
obj-m += mcs.o