|  | 
|  | 1 | +# SPDX-License-Identifier: GPL-2.0 | 
|  | 2 | + | 
|  | 3 | +KERNEL_DIR ?= /lib/modules/`uname -r`/build | 
|  | 4 | +KERNEL_BUID_OUTPUT ?=$(KERNEL_DIR) | 
|  | 5 | +srctree ?=$(KERNEL_DIR) | 
|  | 6 | + | 
|  | 7 | +BPF_SAMPLES_PATH ?= $(KERNEL_DIR)/samples/bpf | 
|  | 8 | +TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools | 
|  | 9 | + | 
|  | 10 | +# List of programs to build | 
|  | 11 | +tprogs-y := demo | 
|  | 12 | +tprogs-y += vmexit | 
|  | 13 | + | 
|  | 14 | +# Libbpf dependencies | 
|  | 15 | +LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a | 
|  | 16 | + | 
|  | 17 | +demo-objs := trace_helpers.o | 
|  | 18 | +demo-objs += demo_user.o | 
|  | 19 | + | 
|  | 20 | +vmexit-objs := trace_helpers.o | 
|  | 21 | +vmexit-objs += vmexit_user.o | 
|  | 22 | + | 
|  | 23 | +# Tell kbuild to always build the programs | 
|  | 24 | +always-y := $(tprogs-y) | 
|  | 25 | +always-y += demo_kern.o | 
|  | 26 | +always-y += vmexit_kern.o | 
|  | 27 | + | 
|  | 28 | +ifeq ($(ARCH), arm) | 
|  | 29 | +# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux | 
|  | 30 | +# headers when arm instruction set identification is requested. | 
|  | 31 | +ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS)) | 
|  | 32 | +BPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR) | 
|  | 33 | +TPROGS_CFLAGS += $(ARM_ARCH_SELECTOR) | 
|  | 34 | +endif | 
|  | 35 | + | 
|  | 36 | +ifeq ($(ARCH), mips) | 
|  | 37 | +TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__ | 
|  | 38 | +ifdef CONFIG_MACH_LOONGSON64 | 
|  | 39 | +BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64 | 
|  | 40 | +BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic | 
|  | 41 | +endif | 
|  | 42 | +endif | 
|  | 43 | + | 
|  | 44 | +TPROGS_CFLAGS += -Wall -O2 | 
|  | 45 | +TPROGS_CFLAGS += -Wmissing-prototypes | 
|  | 46 | +TPROGS_CFLAGS += -Wstrict-prototypes | 
|  | 47 | + | 
|  | 48 | +TPROGS_CFLAGS += -I$(objtree)/usr/include | 
|  | 49 | +TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/ | 
|  | 50 | +TPROGS_CFLAGS += -I$(srctree)/tools/lib/ | 
|  | 51 | +TPROGS_CFLAGS += -I$(srctree)/tools/include | 
|  | 52 | +TPROGS_CFLAGS += -I$(srctree)/tools/perf | 
|  | 53 | +TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0 | 
|  | 54 | + | 
|  | 55 | +ifdef SYSROOT | 
|  | 56 | +TPROGS_CFLAGS += --sysroot=$(SYSROOT) | 
|  | 57 | +TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib | 
|  | 58 | +endif | 
|  | 59 | + | 
|  | 60 | +TPROGS_LDLIBS			+= $(LIBBPF) -lelf -lz | 
|  | 61 | +TPROGLDLIBS_tracex4		+= -lrt | 
|  | 62 | +TPROGLDLIBS_trace_output	+= -lrt | 
|  | 63 | +TPROGLDLIBS_map_perf_test	+= -lrt | 
|  | 64 | +TPROGLDLIBS_test_overhead	+= -lrt | 
|  | 65 | +TPROGLDLIBS_xdpsock		+= -pthread -lcap | 
|  | 66 | +TPROGLDLIBS_xsk_fwd		+= -pthread | 
|  | 67 | + | 
|  | 68 | +# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: | 
|  | 69 | +# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang | 
|  | 70 | +LLC ?= llc | 
|  | 71 | +CLANG ?= clang | 
|  | 72 | +OPT ?= opt | 
|  | 73 | +LLVM_DIS ?= llvm-dis | 
|  | 74 | +LLVM_OBJCOPY ?= llvm-objcopy | 
|  | 75 | +BTF_PAHOLE ?= pahole | 
|  | 76 | + | 
|  | 77 | +# Detect that we're cross compiling and use the cross compiler | 
|  | 78 | +ifdef CROSS_COMPILE | 
|  | 79 | +CLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%)) | 
|  | 80 | +endif | 
|  | 81 | + | 
|  | 82 | +# Don't evaluate probes and warnings if we need to run make recursively | 
|  | 83 | +ifneq ($(src),) | 
|  | 84 | +HDR_PROBE := $(shell printf "\#include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \ | 
|  | 85 | +	$(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \ | 
|  | 86 | +	-o /dev/null 2>/dev/null && echo okay) | 
|  | 87 | + | 
|  | 88 | +ifeq ($(HDR_PROBE),) | 
|  | 89 | +$(warning WARNING: Detected possible issues with include path.) | 
|  | 90 | +$(warning WARNING: Please install kernel headers locally (make headers_install).) | 
|  | 91 | +endif | 
|  | 92 | + | 
|  | 93 | +BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) | 
|  | 94 | +BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) | 
|  | 95 | +BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm') | 
|  | 96 | +BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ | 
|  | 97 | +			  $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \ | 
|  | 98 | +			  readelf -S ./llvm_btf_verify.o | grep BTF; \ | 
|  | 99 | +			  /bin/rm -f ./llvm_btf_verify.o) | 
|  | 100 | + | 
|  | 101 | +BPF_EXTRA_CFLAGS += -fno-stack-protector | 
|  | 102 | +ifneq ($(BTF_LLVM_PROBE),) | 
|  | 103 | +	BPF_EXTRA_CFLAGS += -g | 
|  | 104 | +else | 
|  | 105 | +ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),) | 
|  | 106 | +	BPF_EXTRA_CFLAGS += -g | 
|  | 107 | +	LLC_FLAGS += -mattr=dwarfris | 
|  | 108 | +	DWARF2BTF = y | 
|  | 109 | +endif | 
|  | 110 | +endif | 
|  | 111 | +endif | 
|  | 112 | + | 
|  | 113 | +# Trick to allow make to be run from this directory | 
|  | 114 | +all: | 
|  | 115 | +	$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) BPF_SAMPLES_PATH=$(BPF_SAMPLES_PATH) | 
|  | 116 | + | 
|  | 117 | +clean: | 
|  | 118 | +	$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean | 
|  | 119 | +	@find $(CURDIR) -type f -name '*~' -delete | 
|  | 120 | + | 
|  | 121 | +$(LIBBPF): FORCE | 
|  | 122 | +# Fix up variables inherited from Kbuild that tools/ build system won't like | 
|  | 123 | +	$(MAKE) -C $(dir $@) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \ | 
|  | 124 | +		LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ O= | 
|  | 125 | + | 
|  | 126 | +$(obj)/syscall_nrs.h:	$(obj)/syscall_nrs.s FORCE | 
|  | 127 | +	$(call filechk,offsets,__SYSCALL_NRS_H__) | 
|  | 128 | + | 
|  | 129 | +targets += syscall_nrs.s | 
|  | 130 | +clean-files += syscall_nrs.h | 
|  | 131 | + | 
|  | 132 | +FORCE: | 
|  | 133 | + | 
|  | 134 | + | 
|  | 135 | +# Verify LLVM compiler tools are available and bpf target is supported by llc | 
|  | 136 | +.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) | 
|  | 137 | + | 
|  | 138 | +verify_cmds: $(CLANG) $(LLC) | 
|  | 139 | +	@for TOOL in $^ ; do \ | 
|  | 140 | +		if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \ | 
|  | 141 | +			echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\ | 
|  | 142 | +			exit 1; \ | 
|  | 143 | +		else true; fi; \ | 
|  | 144 | +	done | 
|  | 145 | + | 
|  | 146 | +verify_target_bpf: verify_cmds | 
|  | 147 | +	@if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \ | 
|  | 148 | +		echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\ | 
|  | 149 | +		echo "   NOTICE: LLVM version >= 3.7.1 required" ;\ | 
|  | 150 | +		exit 2; \ | 
|  | 151 | +	else true; fi | 
|  | 152 | + | 
|  | 153 | +$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF) | 
|  | 154 | +$(src)/*.c: verify_target_bpf $(LIBBPF) | 
|  | 155 | + | 
|  | 156 | +$(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h | 
|  | 157 | +$(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h | 
|  | 158 | +$(obj)/hbm.o: $(src)/hbm.h | 
|  | 159 | +$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h | 
|  | 160 | + | 
|  | 161 | +-include $(BPF_SAMPLES_PATH)/Makefile.target | 
|  | 162 | + | 
|  | 163 | +# asm/sysreg.h - inline assembly used by it is incompatible with llvm. | 
|  | 164 | +# But, there is no easy way to fix it, so just exclude it since it is | 
|  | 165 | +# useless for BPF samples. | 
|  | 166 | +# below we use long chain of commands, clang | opt | llvm-dis | llc, | 
|  | 167 | +# to generate final object file. 'clang' compiles the source into IR | 
|  | 168 | +# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin | 
|  | 169 | +# processing (llvm12) and IR optimizations. 'llvm-dis' converts | 
|  | 170 | +# 'opt' output to IR, and finally 'llc' generates bpf byte code. | 
|  | 171 | +$(obj)/%.o: $(src)/%.c | 
|  | 172 | +	@echo "  CLANG-bpf " $@ | 
|  | 173 | +	$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \ | 
|  | 174 | +		-I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \ | 
|  | 175 | +		-I$(srctree)/tools/lib/ \ | 
|  | 176 | +		-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ | 
|  | 177 | +		-D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \ | 
|  | 178 | +		-Wno-gnu-variable-sized-type-not-at-end \ | 
|  | 179 | +		-Wno-address-of-packed-member -Wno-tautological-compare \ | 
|  | 180 | +		-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ | 
|  | 181 | +		-I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \ | 
|  | 182 | +		-O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \ | 
|  | 183 | +		$(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \ | 
|  | 184 | +		$(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@ | 
|  | 185 | +ifeq ($(DWARF2BTF),y) | 
|  | 186 | +	$(BTF_PAHOLE) -J $@ | 
|  | 187 | +endif | 
0 commit comments