Linux 内核源代码树

最后更新: 2026-01-28 12:55:15 +0800 +0800
原文以英文撰写。您正在阅读AI翻译版本。

Linux 源码 tree separates architecture-independent subsystems from architecture ports, public 用户空间 interfaces, 生成的 构建 state, tools, and tests. Directory names 是 a navigation aid rather than an execution model: Kconfig selects configuration, Kbuild selects objects, 和 the selected objects determine what reaches a particular 内核 image.

顶层子系统边界 #

principal 源码 directories are:

PathResponsibility
arch/Architecture ports, boot code, MMU 和 interrupt entry, 和 low-level ABI support.
block/, fs/, mm/, net/block layer; VFS 和 filesystem implementations; virtual 和 physical 内存 management; 和 networking stacks.
kernel/, init/, ipc/, security/Core scheduling, locking, signals, tracing, 和 other 内核 facilities; early initialization; IPC; 和 Linux Security Module infrastructure.
drivers/, sound/, virt/Device drivers, the sound subsystem, 和 virtualization support such 作为 KVM. 一个 driver need 不 expose a struct file_operations; subsystem-specific buses 和 callbacks 是 also common.
crypto/, lib/, io_uring/, rust/Shared cryptographic 和 library support, asynchronous I/O, 和 Rust 内核 support.
include/, Documentation/, scripts/Headers, maintained documentation, 和 Kbuild/Kconfig plus generation 和 maintenance utilities.
tools/, samples/, usr/Userspace tools such 作为 perf, example programs, 和 built-in initramfs generation.

Other top-level material such 作为 certs/ 和 LICENSES/ 支持 signing, licensing, 或 构建 policy. 一个 源码 checkout changes over time, so inspect its top-level README, MAINTAINERS, 和 Documentation/ instead 的 assuming every release 包含 the same directories.

Kbuild 聚合与最终链接 #

Kbuild reads Makefile 或 Kbuild files 和 evaluates obj-$(CONFIG_FOO) entries. y selects a built-in object, m selects a loadable module, 和 an unset symbol selects neither. As the 构建 descends 通过 selected directories, it combines obj-y objects 到 a thin built-in.a archive. archives 和 other selected objects 是 later linked 到 vmlinux; modules follow a separate link path.

Order 是 significant because the Kbuild documentation defines the archive 和 final-link behavior, including initialization ordering. Consequently, a 目录 是 不 a self-contained “module” merely because it has a built-in.a: 配置 和 parent Makefiles control whether the 目录 is visited 和 whether its objects reach the final image.

Trace a feature 通过 both 配置 和 构建 rules:

sh
git grep -n 'CONFIG_FOO'
git grep -n 'obj-$(CONFIG_FOO)'

Read the surrounding Kconfig 和 Makefile entries 在……之前 drawing conclusions from a symbol search, because conditional compilation, 生成的 headers, architecture overrides, 和 link order 可以 select another implementation.

头文件族与用户空间 ABI #

Kernel-private, userspace-visible, architecture-specific, 和 生成的 头文件 are kept separate:

PathPurpose
include/Architecture-independent kernel-internal headers.
include/uapi/Architecture-independent 用户空间 ABI headers.
arch/<arch>/include/Architecture-specific kernel-internal headers.
arch/<arch>/include/uapi/Architecture-specific 用户空间 ABI headers.
include/generated/ 和 arch/<arch>/include/generated/Build-generated headers.

make headers_install exports a sanitized UAPI header set. Applications 应当 use the 已安装的 头文件 supplied 由 their operating 系统 或 toolchain, 不 arbitrary internal 头文件 来自 a 内核 checkout; structures such 作为 task_struct 是 not stable 用户空间 interfaces. header-install documentation defines the supported export operation.

生成状态与输出树 #

Configuration writes .config 和 produces 生成的 头文件 在……之前 compilation. Kbuild 可以 put 生成的 files 和 objects in a separate output directory:

sh
make O="$PWD/out" defconfig
make O="$PWD/out" -j"$(nproc)"

An out-of-tree 构建 keeps 生成的 state out 的 the checkout, but the 源码 and output trees remain coupled 通过 configuration, 生成的 headers, 和 dependency metadata. Reconfigure 在……之后 moving 或 partially copying an output 目录 rather than relying 在 an incremental build’s old 依赖 state.

查找代码并准备索引 #

MAINTAINERS records subsystem ownership 和 preferred patch-routing information. Follow the affected subsystem’s 进程 和 文档 instead 的 treating the tree 作为 one centrally owned program; the maintainer handbook collects subsystem-specific guidance.

Start 实现 discovery,使用 declarations 和 subsystem documentation, then search definitions:

sh
git grep -n 'SYSCALL_DEFINE.*openat'
git grep -n 'struct file_operations'

An editor index benefits 来自 deliberate exclusions because tools/, generated output, 和 构建 products 可以 obscure kernel-space results. For example:

json
{
  "C_Cpp.default.includePath": ["${workspaceFolder}/include"],
  "search.exclude": {
    "**/tools/**": true,
    "**/include/generated/**": true,
    "**/out/**": true
  }
}

Do 不 exclude a path blindly 当 investigating a 用户空间 tool 或 generated interface. exclusion 是 an indexing choice, 同时 the Kconfig 和 Kbuild path remains the evidence 用于 what a 内核 构建 contains.

评论

在 GitHub 上阅读或评论(发表评论需要登录)