1# SPDX-License-Identifier: GPL-2.0 2all: 3 4include ../lib.mk 5 6.PHONY: all all_32 all_64 warn_32bit_failure clean 7 8UNAME_M := $(shell uname -m) 9CAN_BUILD_I386 := $(shell ./check_cc.sh "$(CC)" trivial_32bit_program.c -m32) 10CAN_BUILD_X86_64 := $(shell ./check_cc.sh "$(CC)" trivial_64bit_program.c) 11CAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh "$(CC)" trivial_program.c -no-pie) 12 13TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \ 14 check_initial_reg_state sigreturn iopl ioperm \ 15 test_vsyscall mov_ss_trap sigtrap_loop \ 16 syscall_arg_fault fsgsbase_restore sigaltstack int_signal 17TARGETS_C_BOTHBITS += nx_stack 18TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ 19 test_FCMOV test_FCOMI test_FISTTP \ 20 vdso_restorer 21TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \ 22 corrupt_xstate_header amx lam test_shadow_stack avx apx \ 23 sigframe_fpu_portability 24# Some selftests require 32bit support enabled also on 64bit systems 25TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall 26 27TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY) $(TARGETS_C_32BIT_NEEDED) 28TARGETS_C_64BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_64BIT_ONLY) 29ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),11) 30TARGETS_C_64BIT_ALL += $(TARGETS_C_32BIT_NEEDED) 31endif 32 33BINARIES_32 := $(TARGETS_C_32BIT_ALL:%=%_32) 34BINARIES_64 := $(TARGETS_C_64BIT_ALL:%=%_64) 35 36BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32)) 37BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64)) 38 39CFLAGS := -O2 -g -std=gnu99 -pthread -Wall $(KHDR_INCLUDES) 40CFLAGS += -I $(top_srcdir)/tools/testing/selftests/ 41 42# call32_from_64 in thunks.S uses absolute addresses. 43ifeq ($(CAN_BUILD_WITH_NOPIE),1) 44CFLAGS += -no-pie 45 46ifneq ($(LLVM),) 47# clang only wants to see -no-pie during linking. Here, we don't have a separate 48# linking stage, so a compiler warning is unavoidable without (wastefully) 49# restructuring the Makefile. Avoid this by simply disabling that warning. 50CFLAGS += -Wno-unused-command-line-argument 51endif 52endif 53 54define gen-target-rule-32 55$(1) $(1)_32: $(OUTPUT)/$(1)_32 56.PHONY: $(1) $(1)_32 57endef 58 59define gen-target-rule-64 60$(1) $(1)_64: $(OUTPUT)/$(1)_64 61.PHONY: $(1) $(1)_64 62endef 63 64ifeq ($(CAN_BUILD_I386),1) 65all: all_32 66TEST_PROGS += $(BINARIES_32) 67EXTRA_CFLAGS += -DCAN_BUILD_32 68$(foreach t,$(TARGETS_C_32BIT_ALL),$(eval $(call gen-target-rule-32,$(t)))) 69endif 70 71ifeq ($(CAN_BUILD_X86_64),1) 72all: all_64 73TEST_PROGS += $(BINARIES_64) 74EXTRA_CFLAGS += -DCAN_BUILD_64 75$(foreach t,$(TARGETS_C_64BIT_ALL),$(eval $(call gen-target-rule-64,$(t)))) 76endif 77 78all_32: $(BINARIES_32) 79 80all_64: $(BINARIES_64) 81 82EXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64) srso 83 84$(BINARIES_32): $(OUTPUT)/%_32: %.c helpers.h 85 $(CC) -m32 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $< $(EXTRA_FILES) -lrt -ldl -lm 86 87$(BINARIES_64): $(OUTPUT)/%_64: %.c helpers.h 88 $(CC) -m64 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $< $(EXTRA_FILES) -lrt -ldl 89 90# x86_64 users should be encouraged to install 32-bit libraries 91ifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01) 92all: warn_32bit_failure 93 94warn_32bit_failure: 95 @echo "Warning: you seem to have a broken 32-bit build" 2>&1; \ 96 echo "environment. This will reduce test coverage of 64-bit" 2>&1; \ 97 echo "kernels. If you are using a Debian-like distribution," 2>&1; \ 98 echo "try:"; 2>&1; \ 99 echo ""; \ 100 echo " apt-get install gcc-multilib libc6-i386 libc6-dev-i386"; \ 101 echo ""; \ 102 echo "If you are using a Fedora-like distribution, try:"; \ 103 echo ""; \ 104 echo " yum install glibc-devel.*i686"; \ 105 echo ""; \ 106 echo "If you are using a SUSE-like distribution, try:"; \ 107 echo ""; \ 108 echo " zypper install gcc-32bit glibc-devel-static-32bit"; \ 109 exit 0; 110endif 111 112# Add an additional file to the source file list for a given target, and also 113# add a Makefile dependency on that same file. However, do these separately, so 114# that the compiler invocation ("$(CC) file1.c file2.S") is not combined with 115# the dependencies ("header3.h"), because clang, unlike gcc, will not accept 116# header files as an input to the compiler invocation. 117define extra-files 118$(OUTPUT)/$(1): EXTRA_FILES := $(2) 119$(OUTPUT)/$(1): $(2) 120endef 121 122$(eval $(call extra-files,sysret_ss_attrs_64,thunks.S)) 123$(eval $(call extra-files,ptrace_syscall_32,raw_syscall_helper_32.S)) 124$(eval $(call extra-files,test_syscall_vdso_32,thunks_32.S)) 125$(eval $(call extra-files,fsgsbase_restore_64,clang_helpers_64.S)) 126$(eval $(call extra-files,fsgsbase_restore_32,clang_helpers_32.S)) 127$(eval $(call extra-files,sysret_rip_64,clang_helpers_64.S)) 128 129# check_initial_reg_state is special: it needs a custom entry, and it 130# needs to be static so that its interpreter doesn't destroy its initial 131# state. 132$(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static 133$(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static 134 135$(OUTPUT)/nx_stack_32: CFLAGS += -Wl,-z,noexecstack 136$(OUTPUT)/nx_stack_64: CFLAGS += -Wl,-z,noexecstack 137 138$(OUTPUT)/avx_64: CFLAGS += -mno-avx -mno-avx512f 139$(OUTPUT)/amx_64: EXTRA_FILES += xstate.c 140$(OUTPUT)/avx_64: EXTRA_FILES += xstate.c 141$(OUTPUT)/apx_64: EXTRA_FILES += xstate.c 142 143$(OUTPUT)/sigframe_fpu_portability_64: CFLAGS += -mno-avx -mno-avx512f 144