162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#
362306a36Sopenharmony_ci# Makefile for Kernel-based Virtual Machine module, HYP/nVHE part
462306a36Sopenharmony_ci#
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciasflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci# Tracepoint and MMIO logging symbols should not be visible at nVHE KVM as
962306a36Sopenharmony_ci# there is no way to execute them and any such MMIO access from nVHE KVM
1062306a36Sopenharmony_ci# will explode instantly (Words of Marc Zyngier). So introduce a generic flag
1162306a36Sopenharmony_ci# __DISABLE_TRACE_MMIO__ to disable MMIO tracing for nVHE KVM.
1262306a36Sopenharmony_ciccflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS -D__DISABLE_TRACE_MMIO__
1362306a36Sopenharmony_ciccflags-y += -fno-stack-protector	\
1462306a36Sopenharmony_ci	     -DDISABLE_BRANCH_PROFILING	\
1562306a36Sopenharmony_ci	     $(DISABLE_STACKLEAK_PLUGIN)
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cihostprogs := gen-hyprel
1862306a36Sopenharmony_ciHOST_EXTRACFLAGS += -I$(objtree)/include
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_cilib-objs := clear_page.o copy_page.o memcpy.o memset.o
2162306a36Sopenharmony_cilib-objs := $(addprefix ../../../lib/, $(lib-objs))
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cihyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \
2462306a36Sopenharmony_ci	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \
2562306a36Sopenharmony_ci	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
2662306a36Sopenharmony_cihyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
2762306a36Sopenharmony_ci	 ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o
2862306a36Sopenharmony_cihyp-obj-$(CONFIG_LIST_HARDENED) += list_debug.o
2962306a36Sopenharmony_cihyp-obj-y += $(lib-objs)
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci##
3262306a36Sopenharmony_ci## Build rules for compiling nVHE hyp code
3362306a36Sopenharmony_ci## Output of this folder is `kvm_nvhe.o`, a partially linked object
3462306a36Sopenharmony_ci## file containing all nVHE hyp code and data.
3562306a36Sopenharmony_ci##
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cihyp-obj := $(patsubst %.o,%.nvhe.o,$(hyp-obj-y))
3862306a36Sopenharmony_ciobj-y := kvm_nvhe.o
3962306a36Sopenharmony_citargets += $(hyp-obj) kvm_nvhe.tmp.o kvm_nvhe.rel.o hyp.lds hyp-reloc.S hyp-reloc.o
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci# 1) Compile all source files to `.nvhe.o` object files. The file extension
4262306a36Sopenharmony_ci#    avoids file name clashes for files shared with VHE.
4362306a36Sopenharmony_ci$(obj)/%.nvhe.o: $(src)/%.c FORCE
4462306a36Sopenharmony_ci	$(call if_changed_rule,cc_o_c)
4562306a36Sopenharmony_ci$(obj)/%.nvhe.o: $(src)/%.S FORCE
4662306a36Sopenharmony_ci	$(call if_changed_rule,as_o_S)
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci# 2) Compile linker script.
4962306a36Sopenharmony_ci$(obj)/hyp.lds: $(src)/hyp.lds.S FORCE
5062306a36Sopenharmony_ci	$(call if_changed_dep,cpp_lds_S)
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci# 3) Partially link all '.nvhe.o' files and apply the linker script.
5362306a36Sopenharmony_ci#    Prefixes names of ELF sections with '.hyp', eg. '.hyp.text'.
5462306a36Sopenharmony_ci#    Note: The following rule assumes that the 'ld' rule puts LDFLAGS before
5562306a36Sopenharmony_ci#          the list of dependencies to form '-T $(obj)/hyp.lds'. This is to
5662306a36Sopenharmony_ci#          keep the dependency on the target while avoiding an error from
5762306a36Sopenharmony_ci#          GNU ld if the linker script is passed to it twice.
5862306a36Sopenharmony_ciLDFLAGS_kvm_nvhe.tmp.o := -r -T
5962306a36Sopenharmony_ci$(obj)/kvm_nvhe.tmp.o: $(obj)/hyp.lds $(addprefix $(obj)/,$(hyp-obj)) FORCE
6062306a36Sopenharmony_ci	$(call if_changed,ld)
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci# 4) Generate list of hyp code/data positions that need to be relocated at
6362306a36Sopenharmony_ci#    runtime. Because the hypervisor is part of the kernel binary, relocations
6462306a36Sopenharmony_ci#    produce a kernel VA. We enumerate relocations targeting hyp at build time
6562306a36Sopenharmony_ci#    and convert the kernel VAs at those positions to hyp VAs.
6662306a36Sopenharmony_ci$(obj)/hyp-reloc.S: $(obj)/kvm_nvhe.tmp.o $(obj)/gen-hyprel FORCE
6762306a36Sopenharmony_ci	$(call if_changed,hyprel)
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci# 5) Compile hyp-reloc.S and link it into the existing partially linked object.
7062306a36Sopenharmony_ci#    The object file now contains a section with pointers to hyp positions that
7162306a36Sopenharmony_ci#    will contain kernel VAs at runtime. These pointers have relocations on them
7262306a36Sopenharmony_ci#    so that they get updated as the hyp object is linked into `vmlinux`.
7362306a36Sopenharmony_ciLDFLAGS_kvm_nvhe.rel.o := -r
7462306a36Sopenharmony_ci$(obj)/kvm_nvhe.rel.o: $(obj)/kvm_nvhe.tmp.o $(obj)/hyp-reloc.o FORCE
7562306a36Sopenharmony_ci	$(call if_changed,ld)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci# 6) Produce the final 'kvm_nvhe.o', ready to be linked into 'vmlinux'.
7862306a36Sopenharmony_ci#    Prefixes names of ELF symbols with '__kvm_nvhe_'.
7962306a36Sopenharmony_ci$(obj)/kvm_nvhe.o: $(obj)/kvm_nvhe.rel.o FORCE
8062306a36Sopenharmony_ci	$(call if_changed,hypcopy)
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci# The HYPREL command calls `gen-hyprel` to generate an assembly file with
8362306a36Sopenharmony_ci# a list of relocations targeting hyp code/data.
8462306a36Sopenharmony_ciquiet_cmd_hyprel = HYPREL  $@
8562306a36Sopenharmony_ci      cmd_hyprel = $(obj)/gen-hyprel $< > $@
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci# The HYPCOPY command uses `objcopy` to prefix all ELF symbol names
8862306a36Sopenharmony_ci# to avoid clashes with VHE code/data.
8962306a36Sopenharmony_ciquiet_cmd_hypcopy = HYPCOPY $@
9062306a36Sopenharmony_ci      cmd_hypcopy = $(OBJCOPY) --prefix-symbols=__kvm_nvhe_ $< $@
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci# Remove ftrace, Shadow Call Stack, and CFI CFLAGS.
9362306a36Sopenharmony_ci# This is equivalent to the 'notrace', '__noscs', and '__nocfi' annotations.
9462306a36Sopenharmony_ciKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) $(CC_FLAGS_CFI), $(KBUILD_CFLAGS))
9562306a36Sopenharmony_ci# Starting from 13.0.0 llvm emits SHT_REL section '.llvm.call-graph-profile'
9662306a36Sopenharmony_ci# when profile optimization is applied. gen-hyprel does not support SHT_REL and
9762306a36Sopenharmony_ci# causes a build failure. Remove profile optimization flags.
9862306a36Sopenharmony_ciKBUILD_CFLAGS := $(filter-out -fprofile-sample-use=% -fprofile-use=%, $(KBUILD_CFLAGS))
9962306a36Sopenharmony_ciKBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci# KVM nVHE code is run at a different exception code with a different map, so
10262306a36Sopenharmony_ci# compiler instrumentation that inserts callbacks or checks into the code may
10362306a36Sopenharmony_ci# cause crashes. Just disable it.
10462306a36Sopenharmony_ciGCOV_PROFILE	:= n
10562306a36Sopenharmony_ciKASAN_SANITIZE	:= n
10662306a36Sopenharmony_ciKCSAN_SANITIZE	:= n
10762306a36Sopenharmony_ciUBSAN_SANITIZE	:= n
10862306a36Sopenharmony_ciKCOV_INSTRUMENT	:= n
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci# Skip objtool checking for this directory because nVHE code is compiled with
11162306a36Sopenharmony_ci# non-standard build rules.
11262306a36Sopenharmony_ciOBJECT_FILES_NON_STANDARD := y
113