1ec8f24b7SThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only
2c2390f16SPaolo Bonziniinclude ../../../../scripts/Kbuild.include
3c2390f16SPaolo Bonzini
4783e9e51SPaolo Bonziniall:
5783e9e51SPaolo Bonzini
6cc68765dSAndrew Jonestop_srcdir = ../../../..
7211929fdSShuah KhanKSFT_KHDR_INSTALL := 1
866d69e08SShuah Khan
966d69e08SShuah Khan# For cross-builds to work, UNAME_M has to map to ARCH and arch specific
1066d69e08SShuah Khan# directories and targets in this Makefile. "uname -m" doesn't map to
1166d69e08SShuah Khan# arch specific sub-directory names.
1266d69e08SShuah Khan#
1366d69e08SShuah Khan# UNAME_M variable to used to run the compiles pointing to the right arch
1466d69e08SShuah Khan# directories and build the right targets for these supported architectures.
1566d69e08SShuah Khan#
1666d69e08SShuah Khan# TEST_GEN_PROGS and LIBKVM are set using UNAME_M variable.
1766d69e08SShuah Khan# LINUX_TOOL_ARCH_INCLUDE is set using ARCH variable.
1866d69e08SShuah Khan#
1966d69e08SShuah Khan# x86_64 targets are named to include x86_64 as a suffix and directories
2066d69e08SShuah Khan# for includes are in x86_64 sub-directory. s390x and aarch64 follow the
2166d69e08SShuah Khan# same convention. "uname -m" doesn't result in the correct mapping for
2266d69e08SShuah Khan# s390x and aarch64.
2366d69e08SShuah Khan#
2466d69e08SShuah Khan# No change necessary for x86_64
25783e9e51SPaolo BonziniUNAME_M := $(shell uname -m)
26783e9e51SPaolo Bonzini
2766d69e08SShuah Khan# Set UNAME_M for arm64 compile/install to work
2866d69e08SShuah Khanifeq ($(ARCH),arm64)
2966d69e08SShuah Khan	UNAME_M := aarch64
3066d69e08SShuah Khanendif
3166d69e08SShuah Khan# Set UNAME_M s390x compile/install to work
3266d69e08SShuah Khanifeq ($(ARCH),s390)
3366d69e08SShuah Khan	UNAME_M := s390x
3466d69e08SShuah Khanendif
3566d69e08SShuah Khan
36b268b6f0SAndrew JonesLIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c
3729faeb96SAaron LewisLIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S
382040f414SThomas HuthLIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c
39efaa83a3SCollin WallingLIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c
40783e9e51SPaolo Bonzini
4161cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test
4218178ff8SVitaly KuznetsovTEST_GEN_PROGS_x86_64 += x86_64/evmcs_test
43fb18d053SVitaly KuznetsovTEST_GEN_PROGS_x86_64 += x86_64/get_cpuid_test
447edcb734SVitaly KuznetsovTEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
45ac4a4d6dSOliver UptonTEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test
469dba988eSAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test
4761cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/platform_info_test
4861cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test
4961cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/smm_test
5061cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/state_test
518d7fbf01SMakarand SonareTEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test
5294c4b76bSAndrew JonesTEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
5361cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/sync_regs_test
543cea1891SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/userspace_msr_exit_test
5597a71a54SJim MattsonTEST_GEN_PROGS_x86_64 += x86_64/vmx_apic_access_test
5661cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test
5709444420SPaolo BonziniTEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test
58da1e3071SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test
5961cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test
60678e90a3SPeter ShierTEST_GEN_PROGS_x86_64 += x86_64/xapic_ipi_test
61c90992bfSAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test
62449aa906SPeter XuTEST_GEN_PROGS_x86_64 += x86_64/debug_regs
630c899c25SPaolo BonziniTEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test
64f88d4f2fSLike XuTEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_msrs_test
65*8d4e7e80SDavid WoodhouseTEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test
6623200b7aSJoao MartinsTEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test
67025eed7bSBen GardonTEST_GEN_PROGS_x86_64 += demand_paging_test
6894c4b76bSAndrew JonesTEST_GEN_PROGS_x86_64 += dirty_log_test
694fd94ec7SBen GardonTEST_GEN_PROGS_x86_64 += dirty_log_perf_test
70fd4198bfSPaolo BonziniTEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
71f73a3446SBen GardonTEST_GEN_PROGS_x86_64 += memslot_modification_stress_test
725b4f758fSSean ChristophersonTEST_GEN_PROGS_x86_64 += set_memory_region_test
7394c4b76bSAndrew JonesTEST_GEN_PROGS_x86_64 += steal_time
74fff8dcd7SAndrew Jones
75fd02029aSAndrew JonesTEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
7631d21295SAndrew JonesTEST_GEN_PROGS_aarch64 += aarch64/get-reg-list-sve
77025eed7bSBen GardonTEST_GEN_PROGS_aarch64 += demand_paging_test
7894c4b76bSAndrew JonesTEST_GEN_PROGS_aarch64 += dirty_log_test
7987c5f35eSAndrew JonesTEST_GEN_PROGS_aarch64 += dirty_log_perf_test
8049fe9a5dSThomas HuthTEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
815b4f758fSSean ChristophersonTEST_GEN_PROGS_aarch64 += set_memory_region_test
8294c4b76bSAndrew JonesTEST_GEN_PROGS_aarch64 += steal_time
83783e9e51SPaolo Bonzini
84be6f55a6SThomas HuthTEST_GEN_PROGS_s390x = s390x/memop
85b25d4cb4SJanosch FrankTEST_GEN_PROGS_s390x += s390x/resets
8694c4b76bSAndrew JonesTEST_GEN_PROGS_s390x += s390x/sync_regs_test
87025eed7bSBen GardonTEST_GEN_PROGS_s390x += demand_paging_test
8894c4b76bSAndrew JonesTEST_GEN_PROGS_s390x += dirty_log_test
8949fe9a5dSThomas HuthTEST_GEN_PROGS_s390x += kvm_create_max_vcpus
905b4f758fSSean ChristophersonTEST_GEN_PROGS_s390x += set_memory_region_test
91783e9e51SPaolo Bonzini
92783e9e51SPaolo BonziniTEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
93783e9e51SPaolo BonziniLIBKVM += $(LIBKVM_$(UNAME_M))
94783e9e51SPaolo Bonzini
95783e9e51SPaolo BonziniINSTALL_HDR_PATH = $(top_srcdir)/usr
96783e9e51SPaolo BonziniLINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
97cc68765dSAndrew JonesLINUX_TOOL_INCLUDE = $(top_srcdir)/tools/include
98b80db73dSVitaly Kuznetsovifeq ($(ARCH),x86_64)
99b80db73dSVitaly KuznetsovLINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include
100b80db73dSVitaly Kuznetsovelse
10166d69e08SShuah KhanLINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
102b80db73dSVitaly Kuznetsovendif
103319f6f97SThomas HuthCFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
104319f6f97SThomas Huth	-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
105a5543d34SSean Christopherson	-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
106a5543d34SSean Christopherson	-I$(<D) -Iinclude/$(UNAME_M) -I..
107c2390f16SPaolo Bonzini
108c2390f16SPaolo Bonzinino-pie-option := $(call try-run, echo 'int main() { return 0; }' | \
1096e06983dSShuah Khan        $(CC) -Werror -no-pie -x c - -o "$$TMP", -no-pie)
110c2390f16SPaolo Bonzini
1118343ba2dSChristian Borntraeger# On s390, build the testcases KVM-enabled
1128343ba2dSChristian Borntraegerpgste-option = $(call try-run, echo 'int main() { return 0; }' | \
1138343ba2dSChristian Borntraeger	$(CC) -Werror -Wl$(comma)--s390-pgste -x c - -o "$$TMP",-Wl$(comma)--s390-pgste)
1148343ba2dSChristian Borntraeger
1158343ba2dSChristian Borntraeger
1168343ba2dSChristian BorntraegerLDFLAGS += -pthread $(no-pie-option) $(pgste-option)
117783e9e51SPaolo Bonzini
118783e9e51SPaolo Bonzini# After inclusion, $(OUTPUT) is defined and
119783e9e51SPaolo Bonzini# $(TEST_GEN_PROGS) starts with $(OUTPUT)/
120783e9e51SPaolo Bonziniinclude ../lib.mk
121783e9e51SPaolo Bonzini
122783e9e51SPaolo BonziniSTATIC_LIBS := $(OUTPUT)/libkvm.a
12329faeb96SAaron LewisLIBKVM_C := $(filter %.c,$(LIBKVM))
12429faeb96SAaron LewisLIBKVM_S := $(filter %.S,$(LIBKVM))
12529faeb96SAaron LewisLIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
12629faeb96SAaron LewisLIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
12729faeb96SAaron LewisEXTRA_CLEAN += $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(STATIC_LIBS) cscope.*
128783e9e51SPaolo Bonzini
12929faeb96SAaron Lewisx := $(shell mkdir -p $(sort $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))
13029faeb96SAaron Lewis$(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c
131783e9e51SPaolo Bonzini	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
132783e9e51SPaolo Bonzini
13329faeb96SAaron Lewis$(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S
13429faeb96SAaron Lewis	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
13529faeb96SAaron Lewis
13629faeb96SAaron LewisLIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ)
13729faeb96SAaron Lewis$(OUTPUT)/libkvm.a: $(LIBKVM_OBJS)
138783e9e51SPaolo Bonzini	$(AR) crs $@ $^
139783e9e51SPaolo Bonzini
14066d69e08SShuah Khanx := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))
141b2d35fa5SAnders Roxellall: $(STATIC_LIBS)
142783e9e51SPaolo Bonzini$(TEST_GEN_PROGS): $(STATIC_LIBS)
143eea192bfSAndrew Jones
144eea192bfSAndrew Jonescscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib ..
145eea192bfSAndrew Jonescscope:
146eea192bfSAndrew Jones	$(RM) cscope.*
147eea192bfSAndrew Jones	(find $(include_paths) -name '*.h' \
148eea192bfSAndrew Jones		-exec realpath --relative-base=$(PWD) {} \;; \
149eea192bfSAndrew Jones	find . -name '*.c' \
150eea192bfSAndrew Jones		-exec realpath --relative-base=$(PWD) {} \;) | sort -u > cscope.files
151eea192bfSAndrew Jones	cscope -b
152