gpt4 book ai didi

linux-kernel - ARM Linux 如何模拟 PTE 的脏位、访问位和文件位?

转载 作者:行者123 更新时间:2023-12-01 18:17:49 33 4
gpt4 key购买 nike

根据 pgtable-2-level.h 、ARM Linux有两个版本的PTE; Linux PTE 和 H/W PTE。 Linux PTE 存储在 1024 字节的偏移量以下。

handle_pte_fault 各种函数(如 pte_filepte_mkdirtypte_mkyoung)中处理页面错误时,使用版本 H/W PTE。

但实际上 ARM H/W 的 PTE 中不支持脏位、已访问位和文件位。

我的问题是它如何检查 H/W PTE 上页面的脏、已访问、文件位?理想情况下,它应该检查 Linux PTE 上存储在 1024 字节偏移以下的那些位?

最佳答案

My question is how does it check the dirty, accessed, file bit of a page on H/W PTE?

TL;DR - 它们是通过在初始访问时发生页面错误来模拟的。

答案在 pgtable-2-level.h 中给出,

The "dirty" bit is emulated by only granting hardware write permission iff the page is marked "writable" and "dirty" in the Linux PTE. This means that a write to a clean page will cause a permission fault, and the Linux MM layer will mark the page dirty via handle_pte_fault(). For the hardware to notice the permission change, the TLB entry must be flushed, and ptep_set_access_flags() does that for us.

为了应对情况,页面的初始 MMU 映射被标记为只读。当进程写入时,会生成页面错误。这是引用的handle_pte_fault,主要代码在 fault.c as do_page_fault并将调用最终以 handle_pte_fault 结尾的通用 handle_mm_fault 。你可以看到代码,

if (flags & FAULT_FLAG_WRITE) {
if (!pte_write(entry))
return do_wp_page(mm, vma, address,
pte, pmd, ptl, entry);
entry = pte_mkdirty(entry); /** Here is the dirty emulation. **/
}

因此,Linux 通用代码将检查页面的权限,查看该页面是否可写,并调用 pte_mkdirty 将页面标记为脏;整个过程是通过故障处理程序启动或模拟的。在 Linux PTE 中将页面标记为“脏”后,ARM PTE 将被标记为可写,因此后续写入不会导致错误。

访问的是相同的,只是读和写最初都会出错。 文件位也完全未映射,当发生错误时,会查询Linux PTE以查看它是否由文件支持,或者是否是完全未映射页面错误。

硬件表更新新的权限并完成记账后,用户态程序在故障指令处重新启动,除了处理故障的时间间隔外,它不会注意到任何差异。

<小时/>

ARM Linux 使用 4k 页,ARM 二级页表大小为 1k(256 个条目 * 4 字节)。来自 pgtable-2-level.h 评论,

Therefore, we tweak the implementation slightly - we tell Linux that we have 2048 entries in the first level, each of which is 8 bytes (iow, two hardware pointers to the second level.) The second level contains two hardware PTE tables arranged contiguously, preceded by Linux versions which contain the state information Linux needs. We, therefore, end up with 512 entries in the "PTE" level.

为了使用完整的 4K 页面,PTE 条目的结构如下:

  1. Linux PTE [n]
  2. Linux PTE [n+1]
  3. ARM PTE [n]
  4. ARM PTE [n+1]

四个 1k 项目组成一个完整的 4k 页面。这些页面集合必须按进程进行管理,以便为每个进程提供独特的内存 View ,并且共享一些信息以节省实际 RAM。函数cpu_set_pte_ext用于更改物理ARM条目。由于每个 ARM CPU 版本使用的表结构和功能略有不同,因此 processor function table 中有一个条目。它指向一个汇编程序。例如,cpu_v7_set_pte_ext是 ARMv7 或典型的原始 Cortex CPU 实现。该例程负责检查 Linux 标志并相应地更新硬件位。可以看出,在此例程结束时,r3 被写入 pte+2048(从 Linux PTE 到硬件 PTE 的偏移量)。汇编宏armv3_set_pte_ext许多较旧的 CPU 变体都使用 proc-marcos.S 中的内容。

参见:Tim's notes on ARM MM
Page table entry (PTE) descriptor in Linux kernel for ARM

关于linux-kernel - ARM Linux 如何模拟 PTE 的脏位、访问位和文件位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32943129/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com