162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ciall:
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciinclude ../lib.mk
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci.PHONY: all all_32 all_64 warn_32bit_failure clean
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciUNAME_M := $(shell uname -m)
962306a36Sopenharmony_ciCAN_BUILD_I386 := $(shell ./check_cc.sh "$(CC)" trivial_32bit_program.c -m32)
1062306a36Sopenharmony_ciCAN_BUILD_X86_64 := $(shell ./check_cc.sh "$(CC)" trivial_64bit_program.c)
1162306a36Sopenharmony_ciCAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh "$(CC)" trivial_program.c -no-pie)
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciTARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \
1462306a36Sopenharmony_ci			check_initial_reg_state sigreturn iopl ioperm \
1562306a36Sopenharmony_ci			test_vsyscall mov_ss_trap \
1662306a36Sopenharmony_ci			syscall_arg_fault fsgsbase_restore sigaltstack
1762306a36Sopenharmony_ciTARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \
1862306a36Sopenharmony_ci			test_FCMOV test_FCOMI test_FISTTP \
1962306a36Sopenharmony_ci			vdso_restorer
2062306a36Sopenharmony_ciTARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
2162306a36Sopenharmony_ci			corrupt_xstate_header amx lam test_shadow_stack
2262306a36Sopenharmony_ci# Some selftests require 32bit support enabled also on 64bit systems
2362306a36Sopenharmony_ciTARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ciTARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY) $(TARGETS_C_32BIT_NEEDED)
2662306a36Sopenharmony_ciTARGETS_C_64BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_64BIT_ONLY)
2762306a36Sopenharmony_ciifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),11)
2862306a36Sopenharmony_ciTARGETS_C_64BIT_ALL += $(TARGETS_C_32BIT_NEEDED)
2962306a36Sopenharmony_ciendif
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciBINARIES_32 := $(TARGETS_C_32BIT_ALL:%=%_32)
3262306a36Sopenharmony_ciBINARIES_64 := $(TARGETS_C_64BIT_ALL:%=%_64)
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciBINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32))
3562306a36Sopenharmony_ciBINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ciCFLAGS := -O2 -g -std=gnu99 -pthread -Wall $(KHDR_INCLUDES)
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci# call32_from_64 in thunks.S uses absolute addresses.
4062306a36Sopenharmony_ciifeq ($(CAN_BUILD_WITH_NOPIE),1)
4162306a36Sopenharmony_ciCFLAGS += -no-pie
4262306a36Sopenharmony_ciendif
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cidefine gen-target-rule-32
4562306a36Sopenharmony_ci$(1) $(1)_32: $(OUTPUT)/$(1)_32
4662306a36Sopenharmony_ci.PHONY: $(1) $(1)_32
4762306a36Sopenharmony_ciendef
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cidefine gen-target-rule-64
5062306a36Sopenharmony_ci$(1) $(1)_64: $(OUTPUT)/$(1)_64
5162306a36Sopenharmony_ci.PHONY: $(1) $(1)_64
5262306a36Sopenharmony_ciendef
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ciifeq ($(CAN_BUILD_I386),1)
5562306a36Sopenharmony_ciall: all_32
5662306a36Sopenharmony_ciTEST_PROGS += $(BINARIES_32)
5762306a36Sopenharmony_ciEXTRA_CFLAGS += -DCAN_BUILD_32
5862306a36Sopenharmony_ci$(foreach t,$(TARGETS_C_32BIT_ALL),$(eval $(call gen-target-rule-32,$(t))))
5962306a36Sopenharmony_ciendif
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciifeq ($(CAN_BUILD_X86_64),1)
6262306a36Sopenharmony_ciall: all_64
6362306a36Sopenharmony_ciTEST_PROGS += $(BINARIES_64)
6462306a36Sopenharmony_ciEXTRA_CFLAGS += -DCAN_BUILD_64
6562306a36Sopenharmony_ci$(foreach t,$(TARGETS_C_64BIT_ALL),$(eval $(call gen-target-rule-64,$(t))))
6662306a36Sopenharmony_ciendif
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciall_32: $(BINARIES_32)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciall_64: $(BINARIES_64)
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciEXTRA_CLEAN := $(BINARIES_32) $(BINARIES_64)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci$(BINARIES_32): $(OUTPUT)/%_32: %.c helpers.h
7562306a36Sopenharmony_ci	$(CC) -m32 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl -lm
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci$(BINARIES_64): $(OUTPUT)/%_64: %.c helpers.h
7862306a36Sopenharmony_ci	$(CC) -m64 -o $@ $(CFLAGS) $(EXTRA_CFLAGS) $^ -lrt -ldl
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci# x86_64 users should be encouraged to install 32-bit libraries
8162306a36Sopenharmony_ciifeq ($(CAN_BUILD_I386)$(CAN_BUILD_X86_64),01)
8262306a36Sopenharmony_ciall: warn_32bit_failure
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciwarn_32bit_failure:
8562306a36Sopenharmony_ci	@echo "Warning: you seem to have a broken 32-bit build" 2>&1; 	\
8662306a36Sopenharmony_ci	echo "environment.  This will reduce test coverage of 64-bit" 2>&1; \
8762306a36Sopenharmony_ci	echo "kernels.  If you are using a Debian-like distribution," 2>&1; \
8862306a36Sopenharmony_ci	echo "try:"; 2>&1; \
8962306a36Sopenharmony_ci	echo "";							\
9062306a36Sopenharmony_ci	echo "  apt-get install gcc-multilib libc6-i386 libc6-dev-i386"; \
9162306a36Sopenharmony_ci	echo "";							\
9262306a36Sopenharmony_ci	echo "If you are using a Fedora-like distribution, try:";	\
9362306a36Sopenharmony_ci	echo "";							\
9462306a36Sopenharmony_ci	echo "  yum install glibc-devel.*i686";				\
9562306a36Sopenharmony_ci	echo "";							\
9662306a36Sopenharmony_ci	echo "If you are using a SUSE-like distribution, try:";		\
9762306a36Sopenharmony_ci	echo "";							\
9862306a36Sopenharmony_ci	echo "  zypper install gcc-32bit glibc-devel-static-32bit";	\
9962306a36Sopenharmony_ci	exit 0;
10062306a36Sopenharmony_ciendif
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci# Some tests have additional dependencies.
10362306a36Sopenharmony_ci$(OUTPUT)/sysret_ss_attrs_64: thunks.S
10462306a36Sopenharmony_ci$(OUTPUT)/ptrace_syscall_32: raw_syscall_helper_32.S
10562306a36Sopenharmony_ci$(OUTPUT)/test_syscall_vdso_32: thunks_32.S
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci# check_initial_reg_state is special: it needs a custom entry, and it
10862306a36Sopenharmony_ci# needs to be static so that its interpreter doesn't destroy its initial
10962306a36Sopenharmony_ci# state.
11062306a36Sopenharmony_ci$(OUTPUT)/check_initial_reg_state_32: CFLAGS += -Wl,-ereal_start -static
11162306a36Sopenharmony_ci$(OUTPUT)/check_initial_reg_state_64: CFLAGS += -Wl,-ereal_start -static
112