1# SPDX-License-Identifier: GPL-2.0
2# Makefile for vm selftests
3uname_M := $(shell uname -m 2>/dev/null || echo not)
4MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/')
5
6# Without this, failed build products remain, with up-to-date timestamps,
7# thus tricking Make (and you!) into believing that All Is Well, in subsequent
8# make invocations:
9.DELETE_ON_ERROR:
10
11# Avoid accidental wrong builds, due to built-in rules working just a little
12# bit too well--but not quite as well as required for our situation here.
13#
14# In other words, "make userfaultfd" is supposed to fail to build at all,
15# because this Makefile only supports either "make" (all), or "make /full/path".
16# However,  the built-in rules, if not suppressed, will pick up CFLAGS and the
17# initial LDLIBS (but not the target-specific LDLIBS, because those are only
18# set for the full path target!). This causes it to get pretty far into building
19# things despite using incorrect values such as an *occasionally* incomplete
20# LDLIBS.
21MAKEFLAGS += --no-builtin-rules
22
23CFLAGS = -Wall -I ../../../../usr/include $(EXTRA_CFLAGS)
24LDLIBS = -lrt
25TEST_GEN_FILES = compaction_test
26TEST_GEN_FILES += gup_benchmark
27TEST_GEN_FILES += hmm-tests
28TEST_GEN_FILES += hugepage-mmap
29TEST_GEN_FILES += hugepage-shm
30TEST_GEN_FILES += map_hugetlb
31TEST_GEN_FILES += map_fixed_noreplace
32TEST_GEN_FILES += map_populate
33TEST_GEN_FILES += mlock-random-test
34TEST_GEN_FILES += mlock2-tests
35TEST_GEN_FILES += mremap_dontunmap
36TEST_GEN_FILES += on-fault-limit
37TEST_GEN_FILES += thuge-gen
38TEST_GEN_FILES += transhuge-stress
39TEST_GEN_FILES += userfaultfd
40TEST_GEN_FILES += khugepaged
41
42ifeq ($(ARCH),x86_64)
43CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_32bit_program.c -m32)
44CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_64bit_program.c)
45CAN_BUILD_WITH_NOPIE := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_program.c -no-pie)
46
47TARGETS := protection_keys
48BINARIES_32 := $(TARGETS:%=%_32)
49BINARIES_64 := $(TARGETS:%=%_64)
50
51ifeq ($(CAN_BUILD_WITH_NOPIE),1)
52CFLAGS += -no-pie
53endif
54
55ifeq ($(CAN_BUILD_I386),1)
56TEST_GEN_FILES += $(BINARIES_32)
57endif
58
59ifeq ($(CAN_BUILD_X86_64),1)
60TEST_GEN_FILES += $(BINARIES_64)
61endif
62else
63
64ifneq (,$(findstring $(ARCH),powerpc))
65TEST_GEN_FILES += protection_keys
66endif
67
68endif
69
70ifneq (,$(filter $(MACHINE),arm64 ia64 mips64 parisc64 ppc64 ppc64le riscv64 s390x sh64 sparc64 x86_64))
71TEST_GEN_FILES += va_128TBswitch
72TEST_GEN_FILES += virtual_address_range
73TEST_GEN_FILES += write_to_hugetlbfs
74endif
75
76TEST_PROGS := run_vmtests
77
78TEST_FILES := test_vmalloc.sh
79
80KSFT_KHDR_INSTALL := 1
81include ../lib.mk
82
83$(OUTPUT)/hmm-tests: LDLIBS += -lhugetlbfs -lpthread
84
85ifeq ($(ARCH),x86_64)
86BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
87BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
88
89define gen-target-rule-32
90$(1) $(1)_32: $(OUTPUT)/$(1)_32
91.PHONY: $(1) $(1)_32
92endef
93
94define gen-target-rule-64
95$(1) $(1)_64: $(OUTPUT)/$(1)_64
96.PHONY: $(1) $(1)_64
97endef
98
99ifeq ($(CAN_BUILD_I386),1)
100$(BINARIES_32): CFLAGS += -m32
101$(BINARIES_32): LDLIBS += -lrt -ldl -lm
102$(BINARIES_32): %_32: %.c
103	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
104$(foreach t,$(TARGETS),$(eval $(call gen-target-rule-32,$(t))))
105endif
106
107ifeq ($(CAN_BUILD_X86_64),1)
108$(BINARIES_64): CFLAGS += -m64
109$(BINARIES_64): LDLIBS += -lrt -ldl
110$(BINARIES_64): %_64: %.c
111	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
112$(foreach t,$(TARGETS),$(eval $(call gen-target-rule-64,$(t))))
113endif
114
115# x86_64 users should be encouraged to install 32-bit libraries
116ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01)
117all: warn_32bit_failure
118
119warn_32bit_failure:
120	@echo "Warning: you seem to have a broken 32-bit build" 2>&1;		\
121	echo  "environment. This will reduce test coverage of 64-bit" 2>&1;	\
122	echo  "kernels. If you are using a Debian-like distribution," 2>&1;	\
123	echo  "try:"; 2>&1;							\
124	echo  "";								\
125	echo  "  apt-get install gcc-multilib libc6-i386 libc6-dev-i386";	\
126	echo  "";								\
127	echo  "If you are using a Fedora-like distribution, try:";		\
128	echo  "";								\
129	echo  "  yum install glibc-devel.*i686";				\
130	exit 0;
131endif
132endif
133
134$(OUTPUT)/userfaultfd: LDLIBS += -lpthread
135
136$(OUTPUT)/mlock-random-test: LDLIBS += -lcap
137