1ec8f24b7SThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only
2b61442dfSMasahiro Yamadainclude ../../../build/Build.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
353e06cdf1SAnup Patel# Set UNAME_M riscv compile/install to work
363e06cdf1SAnup Patelifeq ($(ARCH),riscv)
373e06cdf1SAnup Patel	UNAME_M := riscv
383e06cdf1SAnup Patelendif
3966d69e08SShuah Khan
4022721a56SMaciej S. SzmigieroLIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c
414c63c923SJim MattsonLIBKVM_x86_64 = lib/x86_64/apic.c 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
42250b8d6cSRaghavendra Rao AnantaLIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c lib/aarch64/handlers.S lib/aarch64/spinlock.c lib/aarch64/gic.c lib/aarch64/gic_v3.c lib/aarch64/vgic.c
43efaa83a3SCollin WallingLIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c
443e06cdf1SAnup PatelLIBKVM_riscv = lib/riscv/processor.c lib/riscv/ucall.c
45783e9e51SPaolo Bonzini
469e6d484fSVitaly KuznetsovTEST_GEN_PROGS_x86_64 = x86_64/cpuid_test
479e6d484fSVitaly KuznetsovTEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test
4877a3aa26SEmanuele Giuseppe EspositoTEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features
4918178ff8SVitaly KuznetsovTEST_GEN_PROGS_x86_64 += x86_64/evmcs_test
5039bbcc3aSAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/emulator_error_test
512c7f76b4SVitaly KuznetsovTEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
527edcb734SVitaly KuznetsovTEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
53e2e1cc1fSVitaly KuznetsovTEST_GEN_PROGS_x86_64 += x86_64/hyperv_features
5461fb1c54SOliver UptonTEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test
55ac4a4d6dSOliver UptonTEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test
569dba988eSAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test
57ef6a74b2SSean ChristophersonTEST_GEN_PROGS_x86_64 += x86_64/mmu_role_test
5861cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/platform_info_test
59bef9a701SJim MattsonTEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test
603df22524SEmanuele Giuseppe EspositoTEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id
6161cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test
6261cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/smm_test
6361cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/state_test
648d7fbf01SMakarand SonareTEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test
6594c4b76bSAndrew JonesTEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
661ad32105SMaxim LevitskyTEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_test
6761cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/sync_regs_test
6810e7a099SSean ChristophersonTEST_GEN_PROGS_x86_64 += x86_64/userspace_io_test
693cea1891SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/userspace_msr_exit_test
7097a71a54SJim MattsonTEST_GEN_PROGS_x86_64 += x86_64/vmx_apic_access_test
7161cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test
7209444420SPaolo BonziniTEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test
73e337f7e0SSean ChristophersonTEST_GEN_PROGS_x86_64 += x86_64/vmx_exception_with_invalid_guest_state
74ab1ef344SSean ChristophersonTEST_GEN_PROGS_x86_64 += x86_64/vmx_invalid_nested_guest_state
75da1e3071SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test
7661cfcd54SAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test
77efe58549SIlias StamatisTEST_GEN_PROGS_x86_64 += x86_64/vmx_nested_tsc_scaling_test
78678e90a3SPeter ShierTEST_GEN_PROGS_x86_64 += x86_64/xapic_ipi_test
79c90992bfSAaron LewisTEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test
80449aa906SPeter XuTEST_GEN_PROGS_x86_64 += x86_64/debug_regs
810c899c25SPaolo BonziniTEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test
82f88d4f2fSLike XuTEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_msrs_test
838d4e7e80SDavid WoodhouseTEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test
8423200b7aSJoao MartinsTEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test
856a581508SPeter GondaTEST_GEN_PROGS_x86_64 += x86_64/vmx_pi_mmio_test
866a581508SPeter GondaTEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests
87bf70636dSYang ZhongTEST_GEN_PROGS_x86_64 += x86_64/amx_test
88de1956f4SDavid MatlackTEST_GEN_PROGS_x86_64 += access_tracking_perf_test
89025eed7bSBen GardonTEST_GEN_PROGS_x86_64 += demand_paging_test
9094c4b76bSAndrew JonesTEST_GEN_PROGS_x86_64 += dirty_log_test
914fd94ec7SBen GardonTEST_GEN_PROGS_x86_64 += dirty_log_perf_test
921838b06bSIgnacio AlvaradoTEST_GEN_PROGS_x86_64 += hardware_disable_test
93fd4198bfSPaolo BonziniTEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
94b9c2bd50SYanan WangTEST_GEN_PROGS_x86_64 += kvm_page_table_test
95f73a3446SBen GardonTEST_GEN_PROGS_x86_64 += memslot_modification_stress_test
96cad347faSMaciej S. SzmigieroTEST_GEN_PROGS_x86_64 += memslot_perf_test
9761e52f16SSean ChristophersonTEST_GEN_PROGS_x86_64 += rseq_test
985b4f758fSSean ChristophersonTEST_GEN_PROGS_x86_64 += set_memory_region_test
9994c4b76bSAndrew JonesTEST_GEN_PROGS_x86_64 += steal_time
1000b45d587SJing ZhangTEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
1013f9808caSOliver UptonTEST_GEN_PROGS_x86_64 += system_counter_offset_test
102fff8dcd7SAndrew Jones
1034959d865SRaghavendra Rao AnantaTEST_GEN_PROGS_aarch64 += aarch64/arch_timer
1044f05223aSRicardo KollerTEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions
105fd02029aSAndrew JonesTEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
106cb97cf95SOliver UptonTEST_GEN_PROGS_aarch64 += aarch64/psci_cpu_on_test
107dc0e058eSEric AugerTEST_GEN_PROGS_aarch64 += aarch64/vgic_init
10850b020cdSRicardo KollerTEST_GEN_PROGS_aarch64 += aarch64/vgic_irq
109025eed7bSBen GardonTEST_GEN_PROGS_aarch64 += demand_paging_test
11094c4b76bSAndrew JonesTEST_GEN_PROGS_aarch64 += dirty_log_test
11187c5f35eSAndrew JonesTEST_GEN_PROGS_aarch64 += dirty_log_perf_test
11249fe9a5dSThomas HuthTEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
113b9c2bd50SYanan WangTEST_GEN_PROGS_aarch64 += kvm_page_table_test
114358928fdSRicardo KollerTEST_GEN_PROGS_aarch64 += memslot_modification_stress_test
115358928fdSRicardo KollerTEST_GEN_PROGS_aarch64 += memslot_perf_test
11661e52f16SSean ChristophersonTEST_GEN_PROGS_aarch64 += rseq_test
1175b4f758fSSean ChristophersonTEST_GEN_PROGS_aarch64 += set_memory_region_test
11894c4b76bSAndrew JonesTEST_GEN_PROGS_aarch64 += steal_time
1190b45d587SJing ZhangTEST_GEN_PROGS_aarch64 += kvm_binary_stats_test
120783e9e51SPaolo Bonzini
121be6f55a6SThomas HuthTEST_GEN_PROGS_s390x = s390x/memop
122b25d4cb4SJanosch FrankTEST_GEN_PROGS_s390x += s390x/resets
12394c4b76bSAndrew JonesTEST_GEN_PROGS_s390x += s390x/sync_regs_test
124*c7ef9ebbSJanis Schoetterl-GlauschTEST_GEN_PROGS_s390x += s390x/tprot
125025eed7bSBen GardonTEST_GEN_PROGS_s390x += demand_paging_test
12694c4b76bSAndrew JonesTEST_GEN_PROGS_s390x += dirty_log_test
12749fe9a5dSThomas HuthTEST_GEN_PROGS_s390x += kvm_create_max_vcpus
128b9c2bd50SYanan WangTEST_GEN_PROGS_s390x += kvm_page_table_test
12961e52f16SSean ChristophersonTEST_GEN_PROGS_s390x += rseq_test
1305b4f758fSSean ChristophersonTEST_GEN_PROGS_s390x += set_memory_region_test
1310b45d587SJing ZhangTEST_GEN_PROGS_s390x += kvm_binary_stats_test
132783e9e51SPaolo Bonzini
1333e06cdf1SAnup PatelTEST_GEN_PROGS_riscv += demand_paging_test
1343e06cdf1SAnup PatelTEST_GEN_PROGS_riscv += dirty_log_test
1353e06cdf1SAnup PatelTEST_GEN_PROGS_riscv += kvm_create_max_vcpus
1363e06cdf1SAnup PatelTEST_GEN_PROGS_riscv += kvm_page_table_test
1373e06cdf1SAnup PatelTEST_GEN_PROGS_riscv += set_memory_region_test
1383e06cdf1SAnup PatelTEST_GEN_PROGS_riscv += kvm_binary_stats_test
1393e06cdf1SAnup Patel
140783e9e51SPaolo BonziniTEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
141783e9e51SPaolo BonziniLIBKVM += $(LIBKVM_$(UNAME_M))
142783e9e51SPaolo Bonzini
143783e9e51SPaolo BonziniINSTALL_HDR_PATH = $(top_srcdir)/usr
144783e9e51SPaolo BonziniLINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
145cc68765dSAndrew JonesLINUX_TOOL_INCLUDE = $(top_srcdir)/tools/include
146b80db73dSVitaly Kuznetsovifeq ($(ARCH),x86_64)
147b80db73dSVitaly KuznetsovLINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include
148b80db73dSVitaly Kuznetsovelse
14966d69e08SShuah KhanLINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
150b80db73dSVitaly Kuznetsovendif
151319f6f97SThomas HuthCFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
152319f6f97SThomas Huth	-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
153a5543d34SSean Christopherson	-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
154788490e7SAnup Patel	-I$(<D) -Iinclude/$(UNAME_M) -I.. $(EXTRA_CFLAGS)
155c2390f16SPaolo Bonzini
156c2390f16SPaolo Bonzinino-pie-option := $(call try-run, echo 'int main() { return 0; }' | \
1576e06983dSShuah Khan        $(CC) -Werror -no-pie -x c - -o "$$TMP", -no-pie)
158c2390f16SPaolo Bonzini
1598343ba2dSChristian Borntraeger# On s390, build the testcases KVM-enabled
1608343ba2dSChristian Borntraegerpgste-option = $(call try-run, echo 'int main() { return 0; }' | \
1618343ba2dSChristian Borntraeger	$(CC) -Werror -Wl$(comma)--s390-pgste -x c - -o "$$TMP",-Wl$(comma)--s390-pgste)
1628343ba2dSChristian Borntraeger
1638343ba2dSChristian Borntraeger
1648343ba2dSChristian BorntraegerLDFLAGS += -pthread $(no-pie-option) $(pgste-option)
165783e9e51SPaolo Bonzini
166783e9e51SPaolo Bonzini# After inclusion, $(OUTPUT) is defined and
167783e9e51SPaolo Bonzini# $(TEST_GEN_PROGS) starts with $(OUTPUT)/
168783e9e51SPaolo Bonziniinclude ../lib.mk
169783e9e51SPaolo Bonzini
170783e9e51SPaolo BonziniSTATIC_LIBS := $(OUTPUT)/libkvm.a
17129faeb96SAaron LewisLIBKVM_C := $(filter %.c,$(LIBKVM))
17229faeb96SAaron LewisLIBKVM_S := $(filter %.S,$(LIBKVM))
17329faeb96SAaron LewisLIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
17429faeb96SAaron LewisLIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
17529faeb96SAaron LewisEXTRA_CLEAN += $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(STATIC_LIBS) cscope.*
176783e9e51SPaolo Bonzini
17729faeb96SAaron Lewisx := $(shell mkdir -p $(sort $(dir $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ))))
17829faeb96SAaron Lewis$(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c
179783e9e51SPaolo Bonzini	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
180783e9e51SPaolo Bonzini
18129faeb96SAaron Lewis$(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S
18229faeb96SAaron Lewis	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
18329faeb96SAaron Lewis
18429faeb96SAaron LewisLIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ)
18529faeb96SAaron Lewis$(OUTPUT)/libkvm.a: $(LIBKVM_OBJS)
186783e9e51SPaolo Bonzini	$(AR) crs $@ $^
187783e9e51SPaolo Bonzini
18866d69e08SShuah Khanx := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))
189b2d35fa5SAnders Roxellall: $(STATIC_LIBS)
190783e9e51SPaolo Bonzini$(TEST_GEN_PROGS): $(STATIC_LIBS)
191eea192bfSAndrew Jones
192eea192bfSAndrew Jonescscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib ..
193eea192bfSAndrew Jonescscope:
194eea192bfSAndrew Jones	$(RM) cscope.*
195eea192bfSAndrew Jones	(find $(include_paths) -name '*.h' \
196eea192bfSAndrew Jones		-exec realpath --relative-base=$(PWD) {} \;; \
197eea192bfSAndrew Jones	find . -name '*.c' \
198eea192bfSAndrew Jones		-exec realpath --relative-base=$(PWD) {} \;) | sort -u > cscope.files
199eea192bfSAndrew Jones	cscope -b
200