Accesses using linear addresses may cause page-fault exceptions (#PF; exception 14). An…
- User
Accesses using linear addresses may cause page-fault exceptions (#PF; exception 14). An access to a linear address may cause a page-fault exception for either of two reasons: (1) there is no translation for the linear address; or (2) there is a translation for the linear address, but its access rights do not permit the access. As noted in Section 5.3, Section 5.4.2, and Section 5.5, there is no translation for a linear address if the translation process for that address would use a paging-structure entry in which the P flag (bit 0) is 0 or one that sets a reserved bit.1 If there is a translation for a linear address, its access rights are determined as specified in Section 5.6. When Intel® Software Guard Extensions (Intel® SGX) are enabled, the processor may deliver exception 14 for reasons unrelated to paging. See Section 36.3, “Access-control Requirements,” and Section 36.20, “Enclave Page Cache Map (EPCM),” in Chapter 36, “Enclave Access Control and Data Structures.” Such an exception is called an SGX-induced page fault. The processor uses the error code to distinguish SGX-induced page faults from ordinary page faults. When a page fault occurs, the processor loads the CR2 register with the linear address that generated the exception. If linear-address masking had been in effect (Section 4.4), the address recorded reflects the result of that masking and does not contain any masked metadata. If the page-fault exception occurred during execution of an instruction in enclave mode (and not during delivery of an event incident to enclave mode), bits 11:0 of the address are cleared.
逐句解释此段话
- Assistant
- User
Page-Fault Error Code一般存在哪里
- Assistant
好问题!之前我们说过出错线性地址会被放在 CR2 寄存器里,但光有地址还不够,CPU 还要告诉操作系统“为啥出错”。这个“为啥”的详细信息就是Page-Fault Error Code。现在我来详细说明:
Page-Fault Error Code 存在哪里?
- 当 #PF(页错误异常,异常号 14) 发生时,CPU 会启动 中断/异常处理流程。
- 在进入异常处理程序(也就是操作系统定义好的 #PF handler)的时候,CPU 会:
- 自动压入一些寄存器到当前栈(比如
EFLAGS,CS,EIP等), - 然后还会压入一个 32 位或者 64 位的“错误码”(error code),这个就是 Page-Fault Error Code。
- 自动压入一些寄存器到当前栈(比如
换句话说:
➡️ Page-Fault Error Code 并不是放在某个特殊的固定寄存器里,而是由CPU在触发异常时压栈,供异常处理程序读取。
OS 是怎么拿到它的?
- 操作系统在为 #PF 设置异常处理函数时,知道栈里保存的布局。
- 当进入 page fault handler 时,OS 从栈顶位置就能读到这个 error code。
- 处理完之后,操作系统通常会用它(加上 CR2 里的地址)决定下一步,比如:
- 如果是缺页(not present),尝试加载页面到内存。
- 如果是权限错误,可能直接杀掉进程。
Page-Fault Error Code 里都有什么?
就像一个“小标签集合”,其中的每一位告诉你错在哪:
- bit 0 (P): =0 表示“页不存在”,=1 表示“权限问题”。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.