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