Skip to content

Commit

Permalink
toolchain: add llvm-bpf
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsnowwolf committed Apr 6, 2022
1 parent 6cb6036 commit 89e39d8
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 0 deletions.
83 changes: 83 additions & 0 deletions include/bpf.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
BPF_DEPENDS := @HAS_BPF_TOOLCHAIN
LLVM_VER:=

CLANG_MIN_VER:=12

ifneq ($(CONFIG_USE_LLVM_HOST),)
BPF_TOOLCHAIN_HOST_PATH:=$(call qstrip,$(CONFIG_BPF_TOOLCHAIN_HOST_PATH))
ifneq ($(BPF_TOOLCHAIN_HOST_PATH),)
BPF_PATH:=$(BPF_TOOLCHAIN_HOST_PATH)/bin:$(PATH)
else
BPF_PATH:=$(PATH)
endif
CLANG:=$(firstword $(shell PATH='$(BPF_PATH)' command -v clang clang-13 clang-12 clang-11))
LLVM_VER:=$(subst clang,,$(notdir $(CLANG)))
endif
ifneq ($(CONFIG_USE_LLVM_PREBUILT),)
CLANG:=$(TOPDIR)/llvm-bpf/bin/clang
endif
ifneq ($(CONFIG_USE_LLVM_BUILD),)
CLANG:=$(STAGING_DIR_HOST)/llvm-bpf/bin/clang
endif

LLVM_PATH:=$(dir $(CLANG))
LLVM_LLC:=$(LLVM_PATH)/llc$(LLVM_VER)
LLVM_DIS:=$(LLVM_PATH)/llvm-dis$(LLVM_VER)
LLVM_OPT:=$(LLVM_PATH)/opt$(LLVM_VER)
LLVM_STRIP:=$(LLVM_PATH)/llvm-strip$(LLVM_VER)

BPF_KARCH:=mips
BPF_ARCH:=mips$(if $(CONFIG_ARCH_64BIT),64)$(if $(CONFIG_BIG_ENDIAN),,el)
BPF_TARGET:=bpf$(if $(CONFIG_BIG_ENDIAN),eb,el)

BPF_HEADERS_DIR:=$(STAGING_DIR)/bpf-headers

BPF_KERNEL_INCLUDE := \
-nostdinc -isystem $(TOOLCHAIN_DIR)/include \
-I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include \
-I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include/asm/mach-generic \
-I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include/generated \
-I$(BPF_HEADERS_DIR)/include \
-I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include/uapi \
-I$(BPF_HEADERS_DIR)/arch/$(BPF_KARCH)/include/generated/uapi \
-I$(BPF_HEADERS_DIR)/include/uapi \
-I$(BPF_HEADERS_DIR)/include/generated/uapi \
-I$(BPF_HEADERS_DIR)/tools/lib \
-I$(BPF_HEADERS_DIR)/tools/testing/selftests \
-I$(BPF_HEADERS_DIR)/samples/bpf \
-include linux/kconfig.h -include asm_goto_workaround.h

BPF_CFLAGS := \
$(BPF_KERNEL_INCLUDE) -I$(PKG_BUILD_DIR) \
-D__KERNEL__ -D__BPF_TRACING__ -DCONFIG_GENERIC_CSUM \
-D__TARGET_ARCH_${BPF_KARCH} \
-m$(if $(CONFIG_BIG_ENDIAN),big,little)-endian \
-fno-stack-protector -Wall \
-Wno-unused-value -Wno-pointer-sign \
-Wno-compare-distinct-pointer-types \
-Wno-gnu-variable-sized-type-not-at-end \
-Wno-address-of-packed-member -Wno-tautological-compare \
-Wno-unknown-warning-option \
-fno-asynchronous-unwind-tables \
-Wno-uninitialized -Wno-unused-variable \
-Wno-unused-label \
-O2 -emit-llvm -Xclang -disable-llvm-passes

ifeq ($(DUMP),)
CLANG_VER:=$(shell $(CLANG) -dM -E - < /dev/null | grep __clang_major__ | cut -d' ' -f3)
CLANG_VER_VALID:=$(shell [ "$(CLANG_VER)" -ge "$(CLANG_MIN_VER)" ] && echo 1 )
ifeq ($(CLANG_VER_VALID),)
$(error ERROR: LLVM/clang version too old. Minimum required: $(CLANG_MIN_VER), found: $(CLANG_VER))
endif
endif

define CompileBPF
$(CLANG) -g -target $(BPF_ARCH)-linux-gnu $(BPF_CFLAGS) $(2) \
-c $(1) -o $(patsubst %.c,%.bc,$(1))
$(LLVM_OPT) -O2 -mtriple=$(BPF_TARGET) < $(patsubst %.c,%.bc,$(1)) > $(patsubst %.c,%.opt,$(1))
$(LLVM_DIS) < $(patsubst %.c,%.opt,$(1)) > $(patsubst %.c,%.S,$(1))
$(LLVM_LLC) -march=$(BPF_TARGET) -mcpu=v3 -filetype=obj -o $(patsubst %.c,%.o,$(1)) < $(patsubst %.c,%.S,$(1))
$(CP) $(patsubst %.c,%.o,$(1)) $(patsubst %.c,%.debug.o,$(1))
$(LLVM_STRIP) --strip-debug $(patsubst %.c,%.o,$(1))
endef

114 changes: 114 additions & 0 deletions package/kernel/bpf-headers/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#
# Copyright (C) 2006-2009 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk

override QUILT:=
override HOST_QUILT:=

include $(INCLUDE_DIR)/kernel.mk


PKG_NAME:=linux
PKG_PATCHVER:=5.15
# Manually include kernel version and hash from kernel details file
include $(INCLUDE_DIR)/kernel-$(PKG_PATCHVER)

PKG_VERSION:=$(PKG_PATCHVER)$(strip $(LINUX_VERSION-$(PKG_PATCHVER)))
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_URL:=$(LINUX_SITE)
PKG_HASH:=$(LINUX_KERNEL_HASH-$(strip $(PKG_VERSION)))
PKG_BUILD_DIR:=$(KERNEL_BUILD_DIR)/bpf-headers/$(PKG_NAME)-$(PKG_VERSION)

GENERIC_PLATFORM_DIR := $(CURDIR)/../../../target/linux/generic
GENERIC_BACKPORT_DIR := $(GENERIC_PLATFORM_DIR)/backport$(if $(wildcard $(GENERIC_PLATFORM_DIR)/backport-$(PKG_PATCHVER)),-$(PKG_PATCHVER))
GENERIC_PATCH_DIR := $(GENERIC_PLATFORM_DIR)/pending$(if $(wildcard $(GENERIC_PLATFORM_DIR)/pending-$(PKG_PATCHVER)),-$(PKG_PATCHVER))
GENERIC_HACK_DIR := $(GENERIC_PLATFORM_DIR)/hack$(if $(wildcard $(GENERIC_PLATFORM_DIR)/hack-$(PKG_PATCHVER)),-$(PKG_PATCHVER))
GENERIC_FILES_DIR := $(foreach dir,$(wildcard $(GENERIC_PLATFORM_DIR)/files $(GENERIC_PLATFORM_DIR)/files-$(PKG_PATCHVER)),"$(dir)")
PATCH_DIR := $(CURDIR)/patches
FILES_DIR :=

REAL_LINUX_DIR := $(LINUX_DIR)
LINUX_DIR := $(PKG_BUILD_DIR)

include $(INCLUDE_DIR)/bpf.mk
include $(INCLUDE_DIR)/package.mk

define Package/bpf-headers
SECTION:=kernel
CATEGORY:=Kernel
TITLE:=eBPF kernel headers
BUILDONLY:=1
HIDDEN:=1
endef

PKG_CONFIG_PATH:=

export HOST_EXTRACFLAGS=-I$(STAGING_DIR_HOST)/include

KERNEL_MAKE := \
$(MAKE) -C $(PKG_BUILD_DIR) \
ARCH=$(BPF_KARCH) \
CROSS_COMPILE=$(BPF_ARCH)-linux- \
LLVM=1 CC="$(CLANG)" LD="$(TARGET_CROSS)ld" \
HOSTCC="$(HOSTCC)" \
HOSTCXX="$(HOSTCXX)" \
HOST_LOADLIBES="-L$(STAGING_DIR_HOST)/lib" \
KBUILD_HOSTLDLIBS="-L$(STAGING_DIR_HOST)/lib" \
CONFIG_SHELL="$(BASH)" \
INSTALL_HDR_PATH="$(PKG_BUILD_DIR)/user_headers"

define Build/Patch
$(Kernel/Patch/Default)
endef

BPF_DOC = $(PKG_BUILD_DIR)/scripts/bpf_doc.py

define Build/Configure/64
echo 'CONFIG_CPU_MIPS64_R2=y' >> $(PKG_BUILD_DIR)/.config
echo 'CONFIG_64BIT=y' >> $(PKG_BUILD_DIR)/.config
endef

define Build/Configure
grep -vE 'CONFIG_(CPU_.*ENDIAN|HZ)' $(PKG_BUILD_DIR)/arch/mips/configs/generic_defconfig > $(PKG_BUILD_DIR)/.config
echo 'CONFIG_CPU_$(if $(CONFIG_BIG_ENDIAN),BIG,LITTLE)_ENDIAN=y' >> $(PKG_BUILD_DIR)/.config
$(if $(CONFIG_ARCH_64BIT),$(Build/Configure/64))
grep CONFIG_HZ $(REAL_LINUX_DIR)/.config >> $(PKG_BUILD_DIR)/.config
yes '' | $(KERNEL_MAKE) oldconfig
grep 'CONFIG_HZ=' $(REAL_LINUX_DIR)/.config | \
cut -d= -f2 | \
bc -q $(LINUX_DIR)/kernel/time/timeconst.bc \
> $(LINUX_DIR)/include/generated/timeconst.h
$(BPF_DOC) --header \
--file $(LINUX_DIR)/tools/include/uapi/linux/bpf.h \
> $(PKG_BUILD_DIR)/tools/lib/bpf/bpf_helper_defs.h
endef

define Build/Compile
$(KERNEL_MAKE) archprepare headers_install
endef

define Build/InstallDev
mkdir -p $(1)/bpf-headers/arch $(1)/bpf-headers/tools
$(CP) \
$(PKG_BUILD_DIR)/arch/$(BPF_KARCH) \
$(1)/bpf-headers/arch/
$(CP) \
$(PKG_BUILD_DIR)/tools/lib \
$(PKG_BUILD_DIR)/tools/testing \
$(1)/bpf-headers/tools/
$(CP) \
$(PKG_BUILD_DIR)/include \
$(PKG_BUILD_DIR)/samples \
$(PKG_BUILD_DIR)/scripts \
$(PKG_BUILD_DIR)/user_headers \
$(1)/bpf-headers
$(CP) \
$(CURDIR)/files/stdarg.h \
$(1)/bpf-headers/include
endef

$(eval $(call BuildPackage,bpf-headers))
19 changes: 19 additions & 0 deletions package/kernel/bpf-headers/files/stdarg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _STDARG_H
#define _STDARG_H

#ifdef __cplusplus
extern "C" {
#endif

typedef __builtin_va_list va_list;

#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#define va_copy(d,s) __builtin_va_copy(d,s)

#ifdef __cplusplus
}
#endif

#endif
20 changes: 20 additions & 0 deletions package/kernel/bpf-headers/patches/100-support_hz_300.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--- a/arch/mips/Kconfig
+++ b/arch/mips/Kconfig
@@ -2988,6 +2988,9 @@ choice
config HZ_256
bool "256 HZ" if SYS_SUPPORTS_256HZ || SYS_SUPPORTS_ARBIT_HZ

+ config HZ_300
+ bool "300 HZ" if SYS_SUPPORTS_ARBIT_HZ
+
config HZ_1000
bool "1000 HZ" if SYS_SUPPORTS_1000HZ || SYS_SUPPORTS_ARBIT_HZ

@@ -3039,6 +3042,7 @@ config HZ
default 128 if HZ_128
default 250 if HZ_250
default 256 if HZ_256
+ default 300 if HZ_300
default 1000 if HZ_1000
default 1024 if HZ_1024

14 changes: 14 additions & 0 deletions package/kernel/bpf-headers/src/include/generated/bounds.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __LINUX_BOUNDS_H__
#define __LINUX_BOUNDS_H__
/*
* DO NOT MODIFY.
*
* This file was generated by Kbuild
*/

#define NR_PAGEFLAGS 23 /* __NR_PAGEFLAGS */
#define MAX_NR_ZONES 4 /* __MAX_NR_ZONES */
#define NR_CPUS_BITS 1 /* ilog2(CONFIG_NR_CPUS) */
#define SPINLOCK_SIZE 64 /* sizeof(spinlock_t) */

#endif
30 changes: 30 additions & 0 deletions target/llvm-bpf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright (C) 2021 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

override MAKEFLAGS=

LLVM_VERSION := $(shell cat $(STAGING_DIR_HOST)/llvm-bpf/.llvm-version)

LLVM_BPF_PREFIX := llvm-bpf-$(LLVM_VERSION).$(HOST_OS)-$(HOST_ARCH)
LLVM_TAR := $(BIN_DIR)/$(LLVM_BPF_PREFIX).tar.xz

$(LLVM_TAR): $(STAGING_DIR_HOST)/llvm-bpf/.llvm-version
tar -C $(STAGING_DIR_HOST) \
-I '$(STAGING_DIR_HOST)/bin/xz -7e -T$(if $(filter 1,$(NPROC)),2,0)' \
$(if $(SOURCE_DATE_EPOCH),--mtime="@$(SOURCE_DATE_EPOCH)") \
-cf $@.tmp llvm-bpf $(LLVM_BPF_PREFIX)
mv $@.tmp $@

download:
prepare:
compile: $(LLVM_TAR)
install: compile

clean:
rm -f $(LLVM_TAR)
53 changes: 53 additions & 0 deletions toolchain/Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,39 @@ menuconfig TARGET_OPTIONS

Most people will answer N.

choice BPF_TOOLCHAIN
prompt "BPF toolchain" if DEVEL
default BPF_TOOLCHAIN_BUILD_LLVM if BUILDBOT
default BPF_TOOLCHAIN_PREBUILT if HAS_PREBUILT_LLVM_TOOLCHAIN
default BPF_TOOLCHAIN_NONE

config BPF_TOOLCHAIN_NONE
bool "None"

config BPF_TOOLCHAIN_PREBUILT
bool "Use prebuilt LLVM toolchain"
depends on HAS_PREBUILT_LLVM_TOOLCHAIN
select USE_LLVM_PREBUILT

config BPF_TOOLCHAIN_HOST
select USE_LLVM_HOST
bool "Use host LLVM toolchain"

config BPF_TOOLCHAIN_BUILD_LLVM
select USE_LLVM_BUILD
bool "Build LLVM toolchain for eBPF"
help
If enabled, a LLVM toolchain for building eBPF binaries will be built.
If this is not enabled, eBPF packages can only be built if the host
has a suitable toolchain
endchoice

config BPF_TOOLCHAIN_HOST_PATH
string
depends on BPF_TOOLCHAIN_HOST
prompt "Host LLVM toolchain path (prefix)" if DEVEL
default "/usr/local/opt/llvm" if HOST_OS_MACOS
default ""

menuconfig EXTERNAL_TOOLCHAIN
bool
Expand Down Expand Up @@ -266,6 +299,26 @@ config GDB_PYTHON

help
Enable the python bindings for GDB to allow using python in the gdb shell.

config HAS_BPF_TOOLCHAIN
bool

config HAS_PREBUILT_LLVM_TOOLCHAIN
def_bool $(shell, [ -f llvm-bpf/.llvm-version ] && echo y || echo n)

config USE_LLVM_HOST
select HAS_BPF_TOOLCHAIN
bool

config USE_LLVM_PREBUILT
select HAS_BPF_TOOLCHAIN
default y if !DEVEL && !BUILDBOT && HAS_PREBUILT_LLVM_TOOLCHAIN
bool

config USE_LLVM_BUILD
default y if !DEVEL && BUILDBOT
select HAS_BPF_TOOLCHAIN
bool

config USE_GLIBC
default y if !TOOLCHAINOPTS && !EXTERNAL_TOOLCHAIN && !NATIVE_TOOLCHAIN && (arc)
Expand Down
2 changes: 2 additions & 0 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tools-$(CONFIG_TARGET_mxs) += elftosb sdimage
tools-$(CONFIG_TARGET_tegra) += cbootimage cbootimage-configs
tools-$(CONFIG_USES_MINOR) += kernel2minor
tools-$(CONFIG_USE_SPARSE) += sparse
tools-$(CONFIG_USE_LLVM_BUILD) += llvm-bpf

# builddir dependencies
$(curdir)/autoconf/compile := $(curdir)/m4/compile
Expand All @@ -57,6 +58,7 @@ $(curdir)/isl/compile := $(curdir)/gmp/compile
$(curdir)/libressl/compile := $(curdir)/pkgconf/compile
$(curdir)/libtool/compile := $(curdir)/m4/compile $(curdir)/autoconf/compile $(curdir)/automake/compile $(curdir)/missing-macros/compile
$(curdir)/lzma-old/compile := $(curdir)/zlib/compile
$(curdir)/llvm-bpf/compile := $(curdir)/cmake/compile
$(curdir)/meson/compile := $(curdir)/ninja/compile
$(curdir)/make-ext4fs/compile := $(curdir)/zlib/compile
$(curdir)/missing-macros/compile := $(curdir)/autoconf/compile
Expand Down
Loading

0 comments on commit 89e39d8

Please sign in to comment.