1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# Makefile for Kernel-based Virtual Machine module, HYP/nVHE part 4*4882a593Smuzhiyun# 5*4882a593Smuzhiyun 6*4882a593Smuzhiyunasflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS 7*4882a593Smuzhiyunccflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS -D__DISABLE_TRACE_MMIO__ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyunhostprogs := gen-hyprel 10*4882a593SmuzhiyunHOST_EXTRACFLAGS += -I$(objtree)/include 11*4882a593Smuzhiyun 12*4882a593Smuzhiyunlib-objs := clear_page.o copy_page.o memcpy.o memset.o 13*4882a593Smuzhiyunlib-objs := $(addprefix ../../../lib/, $(lib-objs)) 14*4882a593Smuzhiyun 15*4882a593Smuzhiyunobj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ 16*4882a593Smuzhiyun hyp-main.o hyp-smp.o psci-relay.o early_alloc.o stub.o page_alloc.o \ 17*4882a593Smuzhiyun cache.o setup.o mm.o mem_protect.o 18*4882a593Smuzhiyunobj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ 19*4882a593Smuzhiyun ../fpsimd.o ../hyp-entry.o ../exception.o ../pgtable.o 20*4882a593Smuzhiyunobj-y += $(lib-objs) 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun## 23*4882a593Smuzhiyun## Build rules for compiling nVHE hyp code 24*4882a593Smuzhiyun## Output of this folder is `kvm_nvhe.o`, a partially linked object 25*4882a593Smuzhiyun## file containing all nVHE hyp code and data. 26*4882a593Smuzhiyun## 27*4882a593Smuzhiyun 28*4882a593Smuzhiyunhyp-obj := $(patsubst %.o,%.nvhe.o,$(obj-y)) 29*4882a593Smuzhiyunobj-y := kvm_nvhe.o 30*4882a593Smuzhiyunextra-y := $(hyp-obj) kvm_nvhe.tmp.o kvm_nvhe.rel.o hyp.lds hyp-reloc.S hyp-reloc.o 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun# 1) Compile all source files to `.nvhe.o` object files. The file extension 33*4882a593Smuzhiyun# avoids file name clashes for files shared with VHE. 34*4882a593Smuzhiyun$(obj)/%.nvhe.o: $(src)/%.c FORCE 35*4882a593Smuzhiyun $(call if_changed_rule,cc_o_c) 36*4882a593Smuzhiyun$(obj)/%.nvhe.o: $(src)/%.S FORCE 37*4882a593Smuzhiyun $(call if_changed_rule,as_o_S) 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun# 2) Compile linker script. 40*4882a593Smuzhiyun$(obj)/hyp.lds: $(src)/hyp.lds.S FORCE 41*4882a593Smuzhiyun $(call if_changed_dep,cpp_lds_S) 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun# 3) Partially link all '.nvhe.o' files and apply the linker script. 44*4882a593Smuzhiyun# Prefixes names of ELF sections with '.hyp', eg. '.hyp.text'. 45*4882a593Smuzhiyun# Note: The following rule assumes that the 'ld' rule puts LDFLAGS before 46*4882a593Smuzhiyun# the list of dependencies to form '-T $(obj)/hyp.lds'. This is to 47*4882a593Smuzhiyun# keep the dependency on the target while avoiding an error from 48*4882a593Smuzhiyun# GNU ld if the linker script is passed to it twice. 49*4882a593SmuzhiyunLDFLAGS_kvm_nvhe.tmp.o := -r -T 50*4882a593Smuzhiyun$(obj)/kvm_nvhe.tmp.o: $(obj)/hyp.lds $(addprefix $(obj)/,$(hyp-obj)) FORCE 51*4882a593Smuzhiyun $(call if_changed,ld) 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun# 4) Generate list of hyp code/data positions that need to be relocated at 54*4882a593Smuzhiyun# runtime. Because the hypervisor is part of the kernel binary, relocations 55*4882a593Smuzhiyun# produce a kernel VA. We enumerate relocations targeting hyp at build time 56*4882a593Smuzhiyun# and convert the kernel VAs at those positions to hyp VAs. 57*4882a593Smuzhiyun$(obj)/hyp-reloc.S: $(obj)/kvm_nvhe.tmp.o $(obj)/gen-hyprel 58*4882a593Smuzhiyun $(call if_changed,hyprel) 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun# 5) Compile hyp-reloc.S and link it into the existing partially linked object. 61*4882a593Smuzhiyun# The object file now contains a section with pointers to hyp positions that 62*4882a593Smuzhiyun# will contain kernel VAs at runtime. These pointers have relocations on them 63*4882a593Smuzhiyun# so that they get updated as the hyp object is linked into `vmlinux`. 64*4882a593SmuzhiyunLDFLAGS_kvm_nvhe.rel.o := -r 65*4882a593Smuzhiyun$(obj)/kvm_nvhe.rel.o: $(obj)/kvm_nvhe.tmp.o $(obj)/hyp-reloc.o FORCE 66*4882a593Smuzhiyun $(call if_changed,ld) 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun# 6) Produce the final 'kvm_nvhe.o', ready to be linked into 'vmlinux'. 69*4882a593Smuzhiyun# Prefixes names of ELF symbols with '__kvm_nvhe_'. 70*4882a593Smuzhiyun$(obj)/kvm_nvhe.o: $(obj)/kvm_nvhe.rel.o FORCE 71*4882a593Smuzhiyun $(call if_changed,hypcopy) 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun# The HYPREL command calls `gen-hyprel` to generate an assembly file with 74*4882a593Smuzhiyun# a list of relocations targeting hyp code/data. 75*4882a593Smuzhiyunquiet_cmd_hyprel = HYPREL $@ 76*4882a593Smuzhiyun cmd_hyprel = $(obj)/gen-hyprel $< > $@ 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun# The HYPCOPY command uses `objcopy` to prefix all ELF symbol names 79*4882a593Smuzhiyun# to avoid clashes with VHE code/data. 80*4882a593Smuzhiyunquiet_cmd_hypcopy = HYPCOPY $@ 81*4882a593Smuzhiyun cmd_hypcopy = $(OBJCOPY) --prefix-symbols=__kvm_nvhe_ $< $@ 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun# Remove ftrace, Shadow Call Stack and CFI CFLAGS. 84*4882a593Smuzhiyun# This is equivalent to the 'notrace', '__noscs' and '__nocfi' annotations. 85*4882a593SmuzhiyunKBUILD_CFLAGS := $(filter-out $(CC_FLAGS_FTRACE) $(CC_FLAGS_SCS) $(CC_FLAGS_CFI), $(KBUILD_CFLAGS)) 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun# KVM nVHE code is run at a different exception code with a different map, so 88*4882a593Smuzhiyun# compiler instrumentation that inserts callbacks or checks into the code may 89*4882a593Smuzhiyun# cause crashes. Just disable it. 90*4882a593SmuzhiyunGCOV_PROFILE := n 91*4882a593SmuzhiyunKASAN_SANITIZE := n 92*4882a593SmuzhiyunUBSAN_SANITIZE := n 93*4882a593SmuzhiyunKCOV_INSTRUMENT := n 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun# Skip objtool checking for this directory because nVHE code is compiled with 96*4882a593Smuzhiyun# non-standard build rules. 97*4882a593SmuzhiyunOBJECT_FILES_NON_STANDARD := y 98