tests/linux: make user code more like kernel code
authorBarret Rhoden <brho@cs.berkeley.edu>
Thu, 25 Jun 2020 17:58:28 +0000 (13:58 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 25 Jun 2020 17:58:28 +0000 (13:58 -0400)
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>
tests/linux/Makefile
tests/linux/misc-compat.h
tests/linux/modules/Kbuild

index c8f1e59..bbb51be 100644 (file)
 # 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 @@ tests_ldlibs := -lpthread -lm
 $(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)
        @:
index bf9a04b..60cd80a 100644 (file)
 
 #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);                                                     \
+/* arch-specific... */
+static inline void cpu_relax(void)
+{
+       asm volatile("pause" : : : "memory");
 }
 
-/* not quite a normal relax, which also pauses, but this works for all archs */
-static inline void cpu_relax(void)
+static inline uint64_t ndelay(uint64_t nsec)
 {
-       asm volatile("" : : : "memory");
+       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)
index 4517f23..ac38b41 100644 (file)
@@ -1 +1,2 @@
+#CFLAGS_mcs.o += -fno-stack-protector
 obj-m += mcs.o