> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man4/intro.4.md).

# intro.4

`intro` — 设备和设备驱动简介

## 名称

`intro`

## 描述

本节包含与设备、设备驱动程序及各种硬件相关的信息。

### 设备抽象

“设备”一词主要用于指代系统中归属于硬件相关的对象，如磁盘、打印机或带键盘的图形显示器。还有所谓的*伪设备*，即设备驱动程序在软件中模拟设备行为，而无任何特定底层硬件。后者的典型示例是 **`/dev/mem`**，这是一种可使用文件访问语义访问物理内存的机制。

设备抽象通常提供一组通用的系统调用，由内核上层将其分派给相应的设备驱动程序。可用于设备的系统调用集合选自 open(2)、close(2)、read(2)、write(2)、ioctl(2)、select(2) 和 mmap(2)。并非所有驱动程序都实现所有系统调用；例如，在键盘设备上调用 mmap(2) 不太可能有用。

在 FreeBSD 中，过去二十年来设备抽象的诸多方面发生了显著变化。“历史说明”小节描述了一些较为重要的差异。

### 访问设备

FreeBSD 中的大多数设备通过*设备节点*（有时也称为*特殊文件*）访问。它们位于 [devfs(4)](/man/man4/devfs.4.md) 文件系统的实例中，按惯例挂载在文件系统层次结构的 **`/dev`** 目录下（另见 [hier(7)](/man/man7/hier.7.md)）。

[devfs(4)](/man/man4/devfs.4.md) 文件系统根据任何给定时间识别为存在的物理硬件自动创建或删除设备节点。对于伪设备，可根据设备的性质按需动态创建和删除设备节点。

对设备节点的访问限制通常受设备节点条目的常规文件权限约束，而非由内核中的驱动程序直接强制执行。但由于设备节点在重启之间不持久存储，这些文件权限在引导时由 [devfs.conf(5)](/man/man5/devfs.conf.5.md) 中指定的规则设置，或按 [devfs.rules(5)](/man/man5/devfs.rules.5.md) 中定义的规则动态设置，或使用 devfs(8) 命令设置。对于后一种情况，可使用不同规则在不同的 [devfs(4)](/man/man4/devfs.4.md) 文件系统实例中显示不同的设备集合，例如，可用于防止 Jail 子系统访问不安全的设备。仍然可以手动更改设备节点权限，但不会持久保留。

### 没有设备节点的驱动程序

网络设备的驱动程序不使用设备节点进行访问。它们的选择基于内核内的其他决策，并且不通过调用 open(2) 来使用，引入网络设备通常通过系统调用 socket(2) 完成。

### 将驱动程序配置进内核

每个内核都有一个配置文件，用作选择该内核的设施和驱动程序以及调优若干选项的基础。详见 [config(8)](/man/man8/config.8.md) 中关于相关文件的详细说明。本节中各手册页的概要部分提供了配置文件的示例行。另见文件 **`/usr/src/sys/conf/NOTES`** 和 **`/usr/src/sys/${ARCH}/conf/NOTES`**。

驱动程序不必静态编译进内核；它们也可以作为模块加载，在这种情况下，它们提供的任何设备节点只有在模块加载后（如果适用，还需附加到合适的硬件上）才会出现。

### 历史说明

在 FreeBSD 6.0 之前，设备节点可以按传统方式作为文件系统中的持久条目创建。虽然仍可创建此类条目，但它们不再用于访问设备。

在 FreeBSD 5.0 之前，磁盘和磁带驱动器的设备存在两种变体，即*块*设备和*字符*设备，或用更恰当的术语来说，缓冲设备和未缓冲（原始）设备。传统名称由“`ls -l`”输出中作为文件类型标识的字母“`b`”和“`c`”反映。原始设备传统上以“`r`”前缀命名，例如 **`/dev/rda0`** 表示其缓冲设备为 **`/dev/da0`** 的磁盘的原始版本。*这种情况已不复存在；* 所有磁盘设备现在在传统意义上都是“原始”的，即使它们没有“`r`”前缀，并且“缓冲”设备已完全不存在。

缓冲设备通过操作系统维护的缓冲区缓存进行访问；从历史上看，这是系统的主要磁盘缓存，但在 FreeBSD 中，统一虚拟内存管理的引入使其过时。缓冲设备可在任意字节位置读取或写入，由缓冲机制处理磁盘块的读写。相比之下，原始磁盘设备只能在作为底层设备块大小倍数的位置和长度上读取或写入，并且 write(2) 调用是*同步的*，只有在数据已交给设备后才返回给调用者。

## 参见

close(2), ioctl(2), mmap(2), open(2), read(2), select(2), socket(2), write(2), [devfs(4)](/man/man4/devfs.4.md), [hier(7)](/man/man7/hier.7.md), [config(8)](/man/man8/config.8.md)

## 历史

本手册页首次出现于 FreeBSD 2.1。

## 作者

本手册页由 Andrew Gierth 自早期版本重写，早期版本由 J(:org Wunsch 在 David E. O'Brien 的初步输入下编写。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man4/intro.4.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
