# Paths may be overridden for an explicit kernel/header tree and staging area. KERNEL_UNAME ?= $(shell uname -r) KERNEL_SOURCES ?= $(or $(KDIR),$(SYSSRC),/lib/modules/$(KERNEL_UNAME)/build) KERNEL_OUTPUT ?= $(if $(SYSOUT),$(SYSOUT),$(KERNEL_SOURCES)) # A kernel built with clang (CONFIG_CC_IS_CLANG=y, e.g. CachyOS) hands clang-only flags to # external module builds, so gcc cannot compile the C shims. Default to LLVM=1 for such kernels; # an explicit LLVM= on the command line or in the environment is left alone. ifeq ($(origin LLVM),undefined) ifneq ($(shell grep -q '^CONFIG_CC_IS_CLANG=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),) export LLVM := 1 endif endif ifeq ($(LLVM),1) ifeq ($(origin LD),default) LD := $(or $(shell command -v ld.lld 2>/dev/null),ld) endif ifeq ($(origin AR),default) AR := $(or $(shell command -v llvm-ar 2>/dev/null),ar) endif NM ?= $(or $(shell command -v llvm-nm 2>/dev/null),nm) OBJCOPY ?= $(or $(shell command -v llvm-objcopy 2>/dev/null),objcopy) else OBJCOPY ?= objcopy endif # 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=2024 --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 NV_RS_KERNEL_ARM64_BTI ?= $(if $(shell grep -q '^CONFIG_ARM64_BTI_KERNEL=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) NV_RS_KERNEL_ARM64_PAC ?= $(if $(shell grep -q '^CONFIG_ARM64_PTR_AUTH_KERNEL=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) ifeq ($(NV_RS_KERNEL_ARM64_BTI)-$(NV_RS_KERNEL_ARM64_PAC),1-1) RUST_KERNEL_FLAGS += -Z branch-protection=bti,pac-ret export RUSTC_BOOTSTRAP := 1 else ifeq ($(NV_RS_KERNEL_ARM64_BTI),1) RUST_KERNEL_FLAGS += -Z branch-protection=bti export RUSTC_BOOTSTRAP := 1 else ifeq ($(NV_RS_KERNEL_ARM64_PAC),1) RUST_KERNEL_FLAGS += -Z branch-protection=pac-ret export RUSTC_BOOTSTRAP := 1 endif NV_RS_KERNEL_SCS ?= $(if $(shell grep -q '^CONFIG_SHADOW_CALL_STACK=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) ifeq ($(NV_RS_KERNEL_SCS),1) RUST_KERNEL_FLAGS += -Z sanitizer=shadow-call-stack -C target-feature=+reserve-x18 export RUSTC_BOOTSTRAP := 1 endif NV_RS_KERNEL_KCFI ?= $(if $(shell grep -q '^CONFIG_CFI_CLANG=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) ifeq ($(NV_RS_KERNEL_KCFI),1) RUST_KERNEL_FLAGS += -Z sanitizer=kcfi export RUSTC_BOOTSTRAP := 1 endif 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=2024 --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. 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 # Auto-enable Spectre-v2 return-thunk & retpoline flags when the target kernel enables them # (avoids objtool 'naked return' / indirect-call warnings on mitigated kernels, issue #78). NV_RS_KERNEL_RETHUNK ?= $(if $(shell grep -Eq '^CONFIG_MITIGATION_RETHUNK=y$$|^CONFIG_RETHUNK=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) ifeq ($(NV_RS_KERNEL_RETHUNK),1) RUST_KERNEL_FLAGS += -Z function-return=thunk-extern export RUSTC_BOOTSTRAP := 1 endif NV_RS_KERNEL_RETPOLINE ?= $(if $(shell grep -Eq '^CONFIG_MITIGATION_RETPOLINE=y$$|^CONFIG_RETPOLINE=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) ifeq ($(NV_RS_KERNEL_RETPOLINE),1) RUST_KERNEL_FLAGS += -Z retpoline-external-thunk -C jump-tables=no -C unsafe-allow-abi-mismatch=retpoline,retpoline-external-thunk export RUSTC_BOOTSTRAP := 1 endif # Auto-enable Straight-Line Speculation (SLS), kCFI, call padding, and 16B function alignment when configured. NV_RS_KERNEL_SLS ?= $(if $(shell grep -Eq '^CONFIG_MITIGATION_SLS=y$$|^CONFIG_SLS=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) ifeq ($(NV_RS_KERNEL_SLS),1) RUST_KERNEL_FLAGS += -C target-feature=+harden-sls-ret,+harden-sls-ijmp endif NV_RS_KERNEL_KCFI ?= $(if $(shell grep -q '^CONFIG_CFI_CLANG=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) ifeq ($(NV_RS_KERNEL_KCFI),1) RUST_KERNEL_FLAGS += -Z sanitizer=kcfi export RUSTC_BOOTSTRAP := 1 endif # Match arch/x86/Makefile CONFIG_CALL_PADDING (required by objtool --prefix=N on call-depth-tracking kernels). NV_RS_KERNEL_CALL_PADDING ?= $(if $(shell grep -q '^CONFIG_CALL_PADDING=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) NV_RS_KERNEL_CALL_PADDING_BYTES ?= $(or $(shell sed -n 's/^CONFIG_FUNCTION_PADDING_BYTES=\([0-9][0-9]*\)$$/\1/p' $(KERNEL_OUTPUT)/.config 2>/dev/null | head -n 1),16) ifeq ($(NV_RS_KERNEL_CALL_PADDING),1) RUST_KERNEL_FLAGS += -Z patchable-function-entry=$(NV_RS_KERNEL_CALL_PADDING_BYTES),$(NV_RS_KERNEL_CALL_PADDING_BYTES) export RUSTC_BOOTSTRAP := 1 endif NV_RS_KERNEL_FN_ALIGN16 ?= $(if $(shell grep -q '^CONFIG_FUNCTION_ALIGNMENT_16B=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0) ifeq ($(NV_RS_KERNEL_FN_ALIGN16),1) RUST_KERNEL_FLAGS += -C llvm-args=-align-all-functions=4 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)