# 14.9 设备资源提示

设备资源提示文件（**device.hints**）是 FreeBSD 引导过程中的配置文件。系统启动时，loader(8) 读取该文件，其内容被传递给内核，用于控制内核的引导行为，但也可包含任何内核可调参数值。

**device.hints** 存储了内核启动时使用的配置变量，通常称作“设备提示（device hints）”，供设备驱动程序在设备配置过程中使用。

配置文件按层级结构构建，**/boot/device.hints** 用于构建动态内核链接器安装的静态文件。

## device.hints 文件结构

```sh
/
├── boot/ 操作系统引导过程中使用的程序和配置文件
│    └── device.hints 设备资源提示文件
└── sys/
     └── ARCH/ 某架构，具体参见内核
          └── conf/ 内核配置相关文件
               ├── GENERIC.hints GENERIC 内核的设备资源提示示例
               └── NOTES 关于内核配置文件和设备资源提示的说明
```

## 基本系统中的 device.hints

基本系统中 **device.hints** 文件的默认内容根据架构的不同而变化：

```ini
# 下面的驱动大多数已被现代计算机淘汰，或在个人 PC 上较为罕见

# AT 键盘控制器驱动 atkbdc(4) AT 机，1980 年代产物
hint.atkbdc.0.at="isa"  # at：指定设备所连接的总线
hint.atkbdc.0.port="0x060"  # port：即指定设备将使用的 I/O Port 起始地址
hint.atkbd.0.at="atkbdc"
hint.atkbd.0.irq="1"  # irq：要使用的中断线路编号

# PS/2 外设 IBM 兼容鼠标驱动 psm(4)，1980 年代产物

#isa
# └── atkbdc0
#       ├── atkbd0
#       └── psm0

hint.psm.0.at="atkbdc"
hint.psm.0.irq="12"

# syscons(4) 传统控制台驱动
hint.sc.0.at="isa"
hint.sc.0.flags="0x100"  # flags：为设备设置标志位

# 串口驱动 uart(4)
hint.uart.0.at="acpi"  # 即设置 COM1
hint.uart.0.port="0x3F8"
hint.uart.0.flags="0x10"
hint.uart.1.at="acpi"  # 即设置 COM2
hint.uart.1.port="0x2F8"

# RTC 驱动（实时时钟 atrtc(4)）
hint.atrtc.0.at="isa"
hint.atrtc.0.port="0x70"
hint.atrtc.0.irq="8"

# i8254 可编程间隔定时器（AT 定时器）驱动 attimer(4)
hint.attimer.0.at="isa"
hint.attimer.0.port="0x40"
hint.attimer.0.irq="0"

# 禁用 ACPI CPU throttle 驱动，参见 cpufreq(4)
hint.acpi_throttle.0.disabled="1"  # disabled：设置为 “1” 意味着禁用该设备

# 禁用 Pentium 4 热控制，参见 cpufreq(4)
hint.p4tcc.0.disabled="1"
```

根据源代码分析，[sys/amd64/conf/GENERIC.hints](https://github.com/freebsd/freebsd-src/blob/main/sys/amd64/conf/GENERIC.hints) 即为 amd64 架构默认的 **device.hints** 文件。

文件版本：[amd64 GENERIC: Switch uart hints from "isa" to "acpi"](https://github.com/freebsd/freebsd-src/commit/9cc06bf7aa2846c35483de567779bb8afc289f53)。

## **device.hints** 语法

**device.hints** 采用每行一个变量的格式，语法如下：

```ini
hint.驱动.单元编号.关键字="值"
```

该行用于为驱动程序的指定单元编号设备实例设置资源或属性（注释标记为 `#`）。

其中，`驱动` 为设备驱动名称，`单元编号` 为设备驱动的单元编号，`关键字` 为提示关键字。关键字可以是以下选项：

* `at`：指定设备所连接的总线。
* `port`：指定要使用的 I/O 起始地址。
* `portsize`：指定设备使用的 I/O 端口数量。
* `irq`：指定要使用的中断请求号。
* `drq`：指定 DMA 通道号。
* `maddr`：指定设备所占用的物理内存地址。
* `msize`：指定设备所占用的物理内存大小。
* `flags`：为设备设置各种标志位。
* `disabled`：如果设置为 `1`，则禁用该设备。

解释：

```sh
hint.atkbdc.0.at="isa"
```

将驱动 atkbdc（AT 键盘控制器）的设备实例 0 附加（attach）到 ISA 总线，即指定 0 号 atkbdc 设备位于 ISA 总线上。

## 第 3 阶段引导加载器

可以在第 3 阶段引导加载器提示符处指定设备提示，将覆盖 **/boot/device.hints** 中的变量。引导加载器中输入的设备提示仅在当次启动会话中生效，重启后即失效。

第 3 阶段引导加载器的语法为：

```sh
set hint.驱动.单元编号.关键字=值
```

使用 set 添加变量，用 unset 删除变量，用 show 查看变量。

## 参考文献

* FreeBSD Project. device.hints(5)\[EB/OL]. \[2026-04-17]. <https://man.freebsd.org/cgi/man.cgi?query=device.hints&sektion=5>. 设备资源提示文件手册页。
* FreeBSD Project. loader(8)\[EB/OL]. \[2026-04-17]. <https://man.freebsd.org/cgi/man.cgi?query=loader&sektion=8>. 系统引导加载程序手册页。
* FreeBSD Project. atkbdc -- AT keyboard controller driver\[EB/OL]. \[2026-04-17]. <https://man.freebsd.org/cgi/man.cgi?query=atkbdc&sektion=4>. AT 键盘控制器驱动手册页。

## 课后习题

1. 查阅 `device.hints` 中某个已禁用设备（如 `hint.acpi_throttle.0.disabled`）的源代码实现，分析 `disabled` 标志在设备探测流程中的作用机制。
2. 创建一个自定义 `device.hints` 文件，为某个虚拟设备设置资源提示，验证其是否被内核正确读取，记录提示参数的传递路径。
3. 对比 `device.hints` 文件与 `loader.conf` 文件中内核可调参数的加载时机差异，在两个文件中设置同一参数并观察哪个生效，分析加载顺序的优先级规则。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/di-14-zhang-xi-tong-guan-li/di-14.9-jie-she-bei-zi-yuan-ti-shi-wen-jian-device-hints.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
