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