Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/workflows/vmlinux.h.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: vmlinux.h

on:
pull_request:
push:
branches:
- main
workflow_dispatch:

jobs:
gen-headers:
name: Generate vmlinux.h
runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v4

- name: Download Linux source
shell: bash
run: ./scripts/download-latest-linux-release.sh

- name: Install dependencies
shell: bash
run: |
./scripts/install-dependencies.sh
./scripts/install-pahole.sh
./scripts/install-bpftool.sh

- name: x86_64/vmlinux.h
shell: bash
run: ./scripts/gen-vmlinux-header.sh x86_64

- name: aarch64/vmlinux.h
shell: bash
run: ./scripts/gen-vmlinux-header.sh aarch64

- name: arm/vmlinux.h
shell: bash
run: ./scripts/gen-vmlinux-header.sh arm

- name: loongarch64/vmlinux.h
shell: bash
run: ./scripts/gen-vmlinux-header.sh loongarch64

- name: ppc64le/vmlinux.h
shell: bash
run: ./scripts/gen-vmlinux-header.sh ppc64le

- name: riscv64/vmlinux.h
shell: bash
run: ./scripts/gen-vmlinux-header.sh riscv64

- name: s390x/vmlinux.h
shell: bash
run: ./scripts/gen-vmlinux-header.sh s390x

- name: Upload headers
uses: actions/upload-artifact@v4
with:
name: vmlinux.h
if-no-files-found: error
path: ./vmlinux.h
1 change: 1 addition & 0 deletions kconfigs/config.aarch64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_64BIT=y
18 changes: 18 additions & 0 deletions kconfigs/config.common
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CONFIG_BPF=y
CONFIG_BPF_EVENTS=y
CONFIG_BPF_JIT=y
CONFIG_BPF_SYSCALL=y
CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_INFO_BTF=y
CONFIG_DEBUG_INFO_DWARF4=y
CONFIG_FPROBE=y
CONFIG_FTRACE=y
CONFIG_FUNCTION_TRACER=y
CONFIG_KPROBES=y
CONFIG_KPROBE_EVENTS=y
CONFIG_MMU=y
CONFIG_MODULES=y
CONFIG_PERF_EVENTS=y
CONFIG_TRACEPOINTS=y
CONFIG_TRACING=y
CONFIG_UPROBE_EVENTS=y
1 change: 1 addition & 0 deletions kconfigs/config.loongarch64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_64BIT=y
1 change: 1 addition & 0 deletions kconfigs/config.ppc64le
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_64BIT=y
1 change: 1 addition & 0 deletions kconfigs/config.riscv64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_64BIT=y
1 change: 1 addition & 0 deletions kconfigs/config.s390x
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_64BIT=y
1 change: 1 addition & 0 deletions kconfigs/config.x86_64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_64BIT=y
16 changes: 16 additions & 0 deletions scripts/download-latest-linux-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -eux

sudo apt install -y curl jq tar xz-utils

url=$(curl -s https://www.kernel.org/releases.json \
| jq -r '.releases[] | select(.moniker == "mainline") | .source')

curl -LO "$url"
tar -xf $(basename "$url")

dir=$(basename "$url" | sed 's/\.tar\.[gx]z$//')
mv $dir linux

rm $(basename "$url")
92 changes: 92 additions & 0 deletions scripts/gen-vmlinux-header.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash

usage () {
echo "USAGE: ./gen-vmlinux-headers.sh <arch> [<linux-repo-path> <kconfig-path>]"
exit 1
}

set -eu

source $(dirname "$0")/helpers.sh

WORKSPACE=$(pwd)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that the script can be run in project root only ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that the script can be run in project root only ?

Yeah, well realisticly this is the way it's going to run almost always. There are other assumptions, like the location of kconfigs relative to workspace.

I don't think it's worth it right now to rewrite the script to be location agnostic.

BUILD_DIR="${BUILD_DIR:-$WORKSPACE/build}"
GCC_VERSION=${GCC_VERSION:-"13"}
OUTPUT_DIR="${OUTPUT_DIR:-$WORKSPACE/vmlinux.h}"

TARGET_ARCH=$1
LINUX_REPO=${2:-$WORKSPACE/linux}
KCONFIG=${3:-}

if [ -z "${TARGET_ARCH}" ]; then
echo "Error: Target architecture is not set"
usage
fi

if [ ! -d "${LINUX_REPO}" ]; then
echo "Error: Linux repo path is not found, LINUX_REPO=$LINUX_REPO"
usage
fi

if [ ! -f "${KCONFIG}" ]; then
KCONFIG=$(mktemp /tmp/kconfig.XXXX)
cp $WORKSPACE/kconfigs/config.common $KCONFIG
arch_conf="$WORKSPACE/kconfigs/config.${TARGET_ARCH}"
if [ -f "$arch_conf" ]; then
cat "$arch_conf" >> $KCONFIG
fi
echo "==== Using KCONFIG=$KCONFIG for kernel build"
cat $KCONFIG
echo "==== end of KCONFIG=$KCONFIG"
fi

mkdir -p "$BUILD_DIR"
LINUX_REPO=$(realpath "$LINUX_REPO")
KCONFIG=$(realpath "$KCONFIG")

# Install the cross-compiler
if [ "${TARGET_ARCH}" != "x86_64" ]; then
compiler_id=$(arch_compiler_id "$TARGET_ARCH")
sudo apt install -y \
"gcc-${GCC_VERSION}-${compiler_id}" \
"binutils-${compiler_id}"
sudo update-alternatives --install \
/usr/bin/${compiler_id}-gcc \
${compiler_id}-gcc \
/usr/bin/${compiler_id}-gcc-${GCC_VERSION} 100
sudo update-alternatives --set \
${compiler_id}-gcc \
/usr/bin/${compiler_id}-gcc-${GCC_VERSION}
fi

build_arch(){
local arch="$1"
local kbuild_output="$(realpath $BUILD_DIR/$arch)"
mkdir -p "$kbuild_output"
local arch_slug=$(arch_slug "$arch")
local compiler_id=$(arch_compiler_id "$arch")

echo "Building $arch ($arch_slug) into $kbuild_output..."
(
cd "$LINUX_REPO"
make O="$kbuild_output" \
ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \
tinyconfig
"$LINUX_REPO/scripts/kconfig/merge_config.sh" -m \
-O "$kbuild_output" \
"$kbuild_output/.config" "${KCONFIG}"
make O="$kbuild_output" \
ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \
olddefconfig

make O="$kbuild_output" \
ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \
-j$(nproc) all

mkdir -p "$OUTPUT_DIR/$arch"
bpftool btf dump file "$kbuild_output/vmlinux" format c \
> "$OUTPUT_DIR/$arch/vmlinux.h"
)
}

build_arch $TARGET_ARCH
28 changes: 28 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

arch_slug() {
printf "$arch" \
| sed 's/x86_64/x86/' \
| sed 's/i686/x86/' \
| sed 's/aarch64/arm64/' \
| sed 's/ppc64le/powerpc/' \
| sed 's/riscv64/riscv/' \
| sed 's/s390x/s390/' \
| sed 's/loongarch64/loongarch/'
}

arch_compiler_id() {
local arch="$1"
case "$arch" in
arm)
echo "arm-linux-gnueabi"
;;
ppc64le)
echo "powerpc64le-linux-gnu"
;;
*)
echo "${arch}-linux-gnu"
;;
esac
}

16 changes: 16 additions & 0 deletions scripts/install-bpftool.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -eux

LINUX_REPO=${1:-"./linux"}

if [ ! -d "${LINUX_REPO}" ]; then
echo "Error: Linux repo path is not found, LINUX_REPO=$LINUX_REPO"
exit 1
fi

sudo apt install -y llvm libcap-dev libbfd-dev zlib1g-dev

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we install all dependencies in scripts/install-dependencies.sh, this won't be necessary. The same for pahole.

If we install dependencies here, DEBIAN_FRONTEND=noninteractive is missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to make install-bpftool.sh and install-pahole.sh independently runnable, that's why I put their specific dependencies here.

DEBIAN_FRONTEND=noninteractive is really needed only for tzdata. Why do you think it is necessary here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know whether it is necessary for tzdata only.


cd "$LINUX_REPO/tools/bpf/bpftool"
make -j$(nproc)
sudo make install
10 changes: 10 additions & 0 deletions scripts/install-dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -eu

# Assume Ubuntu/Debian x86_64

sudo apt update -y
DEBIAN_FRONTEND=noninteractive sudo -E apt install -y tzdata
sudo apt install -y bc bison build-essential elfutils flex libdw-dev libelf-dev make ncurses-dev python3-docutils zstd

22 changes: 22 additions & 0 deletions scripts/install-pahole.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -eux

PAHOLE_ORIGIN=${PAHOLE_ORIGIN:-https://git.kernel.org/pub/scm/devel/pahole/pahole.git}
PAHOLE_REVISION=${PAHOLE_REVISION:-next}

sudo apt -y install cmake git libdw-dev libelf-dev

WORKSPACE=$(mktemp -d -t pahole.XXXXXX)
pushd "$WORKSPACE"
git clone ${PAHOLE_ORIGIN} \
--branch "${PAHOLE_REVISION}" \
--depth=1 \
--single-branch
cd pahole
mkdir -p build
cmake -B=build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr
make -C build -j$(nproc)
sudo make -C build install
popd
rm -rf "$WORKSPACE"