注意:我没有读过/看过这本书。
用于教育材料;如果作者用所有细节准确地描述现实,读者只会感到困惑,无法学习。为了解决这个问题,作者在引入不同概念的同时简化(省略细节并忽略现实),以便读者能够一次学习每个概念,同时积累理解现实复杂性所需的知识。
问题在于,不同的简化在不同的阶段有意义,而且作者是人(不完美),所以有时在某一时刻(在一章中)有益的简化与在以后的某个时刻有益的简化(在不同的章节)。
例如,我可能(最初)告诉某人“每次从虚拟内存访问都涉及从 RAM 中获取第二次内存以确定转换”,以帮助他们了解页表的工作原理以及涉及的(潜在)性能问题(两倍的内存访问)。然后我可能会介绍“翻译后备缓冲区”的概念(在读者了解页表的工作原理并了解 TLB 旨在解决的问题之后)。然后我可能会解释说,实际系统通常有多个级别的页表(例如,在 64 位 80x86 上它是四个级别,可能涉及 4 次内存访问以确定翻译)并且可能涉及更高级别的缓存/缓冲区(而不仅仅是缓存最终翻译的 TLB)。在这种情况下,我最初的陈述(“每次从虚拟内存访问都涉及从 RAM 中提取第二次内存以确定翻译”)是故意撒谎(简化)以避免像“每次从虚拟内存访问可能或者可能不涉及从某些或所有级别的页表中进行一个或多个额外的提取”(这对初学者来说太混乱了,因为它会产生很多他们还没有答案的问题)。
我无法理解为什么作者建议在没有页面错误的情况下只需要一次内存访问。
一个现实是(对于一个真正的 80x86 CPU 处于长模式,但不是所有 80x86 CPU 处于长模式,而不是任何 80x86 处于其他模式,如果没有使用虚拟化),从虚拟内存读取不会导致页面错误,如果访问没有错位/跨页面边界拆分(CPU 必须全部执行两次才能从 2 个不同的页面获取字节并合并字节):
* if the translation is not in the TLB, then:
* if the area is not in the "page directory cache"
* fetch the PML4 entry to determine address of PDPT (try L1 cache, then L2 cache, then L3 cache, then RAM)
* do access checks based on flags in PML4 entry
* fetch the PDPT entry to determine address of PD (try L1 cache, then L2 cache, then L3 cache, then RAM)
* do access checks based on flags in PDPT entry
* insert data into "page directory cache"
* if the area is in the "page directory cache"
* do access checks based on flags in "page directory cache entry"
* fetch the PD entry to determine address of PT (try L1 cache, then L2 cache, then L3 cache, then RAM)
* do access checks based on flags in PD entry
* fetch the PT entry to determine address of page (try L1 cache, then L2 cache, then L3 cache, then RAM)
* do access checks based on flags in PT entry
* insert data into TLB (including setting the "accessed" flag in the page table entry)
* if the translation is in the TLB, then:
* do access checks based on flags in "TLB entry"
* do the "physical address = physical address of page + offset in page" calculation
* read the data for the physical address (try L1 cache, then L2 cache, then L3 cache, then RAM)
对于这个现实(有提到的限制);从 RAM 中提取的次数可以是 0 到 5 之间的任意值。
你能明白为什么作者(在试图解释页面错误而不是试图解释翻译成本时)可能希望避免显示这样的内容并可能会简化(假设只需要一次提取,因为翻译在TLB) 代替?