> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 构建和安装 RT 内核

Qualcomm<sup>®</sup> Linux RT 内核使用与标准内核相同的 Yocto 工具链构建。`meta-qcom` 层提供了专用的 Bitbake 配方，可自动获取 LTS RT 内核源代码、应用 `PREEMPT_RT` 补丁集并启用所需的 Kconfig 选项。

## 选择正确的 RT 配方

根据目标分支，有两个 RT 配方可用：

| **配方**                      |   **内核轨道**  | **适用场景**             |
| :-------------------------- | :---------: | :------------------- |
| `linux-qcom-rt_6.18.bb`     |  LTS 6.18.x | 为 QLI 发行版构建（稳定、生产就绪） |
| `linux-qcom-next-rt_git.bb` | `qcom-next` | 从最新的开发树构建            |

两个配方均位于 `meta-qcom/recipes-kernel/linux/` 中。本页所有示例均使用 LTS 配方。

## 构建 RT 内核镜像

### 1. 同步工作区

在发布标签处克隆 `meta-qcom`，并在构建前同步所有 Qualcomm Linux meta 层。有关工作区设置说明，请参阅[使用 Yocto 构建内核](./build-kernel-yocto)。

### 2. 为 RT 内核打开 kas shell

```bash theme={null}
kas shell meta-qcom/ci/qcs6490-rb3gen2-core-kit.yml:meta-qcom/ci/linux-qcom-rt-6.18.yml:meta-qcom/ci/qcom-distro-kvm.yml
```

将 `qcs6490-rb3gen2-core-kit.yml` 替换为您目标开发板的 kas 机器文件。

### 3. 构建镜像

```bash theme={null}
bitbake qcom-multimedia-image
```

构建输出位于：

```text theme={null}
build/tmp/deploy/images/<Machine>/<Image>-*.rootfs.qcomflash/
```

### 4. 刷写到设备

```bash theme={null}
cd build/tmp/deploy/images/<Machine>/<Image>-*.rootfs.qcomflash/
fastboot flash efi efi.bin
fastboot flash dtb_a dtb.bin
fastboot reboot
```

## 自定义 RT 内核

### 添加补丁

1. 将补丁文件放入 `recipes-kernel/linux/linux-qcom-6.18/`：

   ```text theme={null}
   meta-qcom/recipes-kernel/linux/linux-qcom-6.18/your_patch.patch
   ```

2. 将其追加到 `linux-qcom-rt_6.18.bb` 中的 `SRC_URI`：

   ```bitbake theme={null}
   SRC_URI += " \
       file://your_patch.patch \
   "
   ```

### 添加 Kconfig 配置片段

1. 创建包含所需 `CONFIG_*` 符号的 `.cfg` 文件：

   ```text theme={null}
   meta-qcom/recipes-kernel/linux/linux-qcom-6.18/configs/qcom_rt_custom.cfg
   ```

   示例内容：

   ```text theme={null}
   CONFIG_CPU_ISOLATION=y
   CONFIG_HZ_1000=y
   # CONFIG_TRANSPARENT_HUGEPAGE is not set
   ```

2. 将其追加到 `linux-qcom-rt_6.18.bb` 中的 `SRC_URI`：

   ```bitbake theme={null}
   SRC_URI += " \
       file://configs/qcom_rt_custom.cfg \
   "
   ```

### 修改内核命令行

使用 `KERNEL_CMDLINE_EXTRA` 变量在 `meta-qcom/ci/base.yml` 中添加或覆盖命令行参数：

```yaml theme={null}
KERNEL_CMDLINE_EXTRA:append: " isolcpus=7 nohz_full=7 rcu_nocbs=7"
```

对于特定于平台的命令行调优变量，请在 `meta-qcom/conf/machine/<machine-name>.conf` 中设置：

```text theme={null}
QCOM_RT_CPU        = "7"
QCOM_IRQAFF        = "0-6"
QCOM_RCU_NOCBS     = "7"
QCOM_RCU_EXPEDITED = "1"
QCOM_CPUIDLE_OFF   = "1"
```

这些变量在平台内核命令行上配置 RT 核心隔离、IRQ 亲和性和 RCU 回调排除。

## 验证 RT 内核已安装

刷写并重启后，确认 RT 内核已激活：

```bash theme={null}
uname -r
# Expected: a version string with the -rt suffix, e.g. 6.18.0-rt1+

zcat /proc/config.gz | grep CONFIG_PREEMPT_RT
# Expected: CONFIG_PREEMPT_RT=y
```

有关安装后的延迟验证，请参阅 [RT 验证和已知限制](./rt-validation-and-known-limitations)。
