Files
nvdriver/utils.mk
Eric Hartford c5d51ec8f7 build: export RUSTC_BOOTSTRAP=1 when NV_RS_KERNEL_IBT=1 (fixes #31)
Stock 580.178.04 nv-kernel.o_binary unconditionally emits endbr64
(f3 0f 1e fa) prologues so CONFIG_X86_KERNEL_IBT=y kernels do not raise
#CP on indirect calls. utils.mk already enables -Z cf-protection=branch
-C jump-tables=no on IBT kernels, but stable/distro rustc (including
rust-toolchain.toml 1.100.0, Arch rust, Gentoo dev-lang/rust) rejected
-Z without RUSTC_BOOTSTRAP=1.

Exporting RUSTC_BOOTSTRAP=1 inside the NV_RS_KERNEL_IBT=1 block allows
stable rustc to emit ENDBR64 prologues directly (verified on stable
rustc 1.100.0: check_kernel_module.py passed=true, largest_stack=1000,
data_relocation_targets_without_endbr=0,
data_relocations_pointing_inside_a_function=0).
2026-12-01 05:28:22 +00:00

56 lines
3.7 KiB
Makefile

# Paths may be overridden for an explicit kernel/header tree and staging area.
KERNEL_UNAME ?= $(shell uname -r)
KERNEL_SOURCES ?= $(if $(SYSSRC),$(SYSSRC),/lib/modules/$(KERNEL_UNAME)/build)
KERNEL_OUTPUT ?= $(if $(SYSOUT),$(SYSOUT),$(KERNEL_SOURCES))
OBJCOPY ?= objcopy
# Resolve rustc via rustup (including ~/.cargo/bin when non-interactive PATH omits it),
# falling back to ~/.cargo/bin/rustc or PATH rustc. Override with RUSTC=... as before.
RUSTC ?= $(or $(shell PATH="$(HOME)/.cargo/bin:$$PATH" rustup which rustc 2>/dev/null),$(wildcard $(HOME)/.cargo/bin/rustc),$(shell command -v rustc))
NV_RS_ARCH ?= $(if $(ARCH),$(ARCH),$(shell uname -m))
ifneq ($(filter arm64 aarch64,$(NV_RS_ARCH)),)
RUST_KERNEL_TARGET := aarch64-unknown-none
# ARM64 kernel target (RK3588 / Ampere Altra / Apple Silicon Linux): no NEON/FP in kernel mode.
RUST_KERNEL_FLAGS := --edition=2021 --crate-type=lib --target=$(RUST_KERNEL_TARGET) \
--emit=obj -C opt-level=2 -C panic=abort -C relocation-model=static \
-C code-model=small -C target-feature=-neon,-fp-armv8 -C force-frame-pointers=yes \
-C overflow-checks=yes -C debuginfo=1
else
RUST_KERNEL_TARGET := x86_64-unknown-none
# Kernel target supplies a soft-float, no-SSE core; explicit flags document ABI.
RUST_KERNEL_FLAGS := --edition=2021 --crate-type=lib --target=$(RUST_KERNEL_TARGET) \
--emit=obj -C opt-level=2 -C panic=abort -C relocation-model=static \
-C code-model=kernel -C no-redzone=yes -C force-frame-pointers=yes \
-C overflow-checks=yes -C debuginfo=1
# Auto-enable IBT flags when the target kernel has CONFIG_X86_KERNEL_IBT=y (override with NV_RS_KERNEL_IBT=0/1).
# -Z cf-protection=branch emits ENDBR64 on indirect-call targets (matching stock nv-kernel.o_binary);
# -C jump-tables=no avoids .rodata.<fn> switch tables that objtool rejects with CONFIG_OBJTOOL_WERROR=y.
# Export RUSTC_BOOTSTRAP=1 so stable/distro rustc (and rust-toolchain.toml 1.100.0) accepts
# -Z cf-protection=branch without requiring users to install a nightly toolchain.
NV_RS_KERNEL_IBT ?= $(if $(shell grep -q '^CONFIG_X86_KERNEL_IBT=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0)
ifeq ($(NV_RS_KERNEL_IBT),1)
RUST_KERNEL_FLAGS += -Z cf-protection=branch -C jump-tables=no
export RUSTC_BOOTSTRAP := 1
endif
endif
# Matching580 conftest product selectors: ANY nonempty value enables a role.
# Export passes both inherited environment and explicit `make NAME=value` as data. No CPUID/Xen detection.
export VGX_BUILD VGX_KVM_BUILD VGX_DEVICE_VM_BUILD
RUST_BUILD_PRODUCT_RECEIPT := $(ROOT)/target/build-product/config.json
# GNU Make's $(shell) does not receive newly exported command-line variables.
# Single-quote each resolved Make value, including literal quotes/dollars/newlines.
nv_rs_shell_quote = '$(subst ','"'"',$(1))'
RUST_BUILD_PRODUCT_FLAGS := $(shell VGX_BUILD=$(call nv_rs_shell_quote,$(VGX_BUILD)) VGX_KVM_BUILD=$(call nv_rs_shell_quote,$(VGX_KVM_BUILD)) VGX_DEVICE_VM_BUILD=$(call nv_rs_shell_quote,$(VGX_DEVICE_VM_BUILD)) python3 $(ROOT)/tools/timer-timeout-build-product.py --receipt $(RUST_BUILD_PRODUCT_RECEIPT))
ifeq ($(strip $(RUST_BUILD_PRODUCT_FLAGS)),)
$(error Cannot resolve Rust build-product configuration)
endif
RUST_KERNEL_FLAGS += $(RUST_BUILD_PRODUCT_FLAGS)
# Explicit absolute targets work before OUT is assigned by src/nvidia/Makefile.
# EXTRA_PREREQS preserves the original first prerequisite used by rustc $<.
# A changed product receipt invalidates every Rust artifact using these cfgs.
NV_RS_PRIOR_DEFAULT_GOAL := $(.DEFAULT_GOAL)
$(ROOT)/kernel-open/nvidia/libnv_rs_abi.rlib \
$(ROOT)/kernel-open/nvidia/libnv_rs_core.rlib \
$(ROOT)/kernel-open/nvidia/libnv_rs_kernel.a: .EXTRA_PREREQS := $(RUST_BUILD_PRODUCT_RECEIPT)
.DEFAULT_GOAL := $(NV_RS_PRIOR_DEFAULT_GOAL)