简体   繁体   English

堆栈指针如何从虚拟内存转换为物理内存

[英]How are stack pointers converted from virtual to physical memory

I've been working with a lot of assembly, and reviewing virtual memory I've run into some new confusion. 我一直在处理大量程序集,并且在审查虚拟内存时遇到了一些新的困惑。

Briefly, I don't understand how an address in assembly, the code that interfaces with the processor directly, could be converted from a virtual address to a physical address. 简而言之,我不明白如何将汇编中的地址(直接与处理器连接的代码)从虚拟地址转换为物理地址。

I was always told that the operating system handled mapping from virtual to physical memory, but assembly directly references an address without any system calls, how could the OS intervene if it isn't called directly? 经常有人告诉我,操作系统处理了从虚拟内存到物理内存的映射,但是程序集直接引用一个地址,而没有任何系统调用,如果不直接调用OS,该如何干预?

Where does an address, (mov eax, [0xDEADBEEF]), get translated from the virtual address space to the physical address space using the page table in the OS without specifically calling the OS? 使用OS中的页表将地址(移动eax [0xDEADBEEF])从虚拟地址空间转换为物理地址空间的位置,而无需专门调用OS?

Simply because the CPU supports that kind of translation directly, using page tables . 仅仅是因为CPU使用页表直接支持这种转换。 OS sets up those page tables beforehand to tell CPU where to look when it references a memory address. 操作系统会预先设置这些页表,以告诉CPU在引用内存地址时在哪里查看。 That's how the translation happens transparently. 这就是透明地进行翻译的方式。

In assembly language you work with logical addresses. 在汇编语言中,您使用逻辑地址。 The operating system maps logical addresses to physical addresses using page tables. 操作系统使用页表将逻辑地址映射到物理地址。 The CPU automatically translates the local address to a physical address. CPU自动将本地地址转换为物理地址。

It is possible that a logical address will not have a physical address mapped to it. 逻辑地址可能没有映射的物理地址。 When the CPU encounters that condition it invokes the operating system's page fault handler. 当CPU遇到这种情况时,它将调用操作系统的页面错误处理程序。

The operating system has to maintain a copy the process's address space on secondary storage. 操作系统必须在辅助存储器上维护该进程的地址空间的副本。 This is the "virtual" memory. 这就是“虚拟”记忆。 When a page fault occurs, the operating system determines if the page being referenced exists in virtual memory. 发生页面错误时,操作系统将确定虚拟内存中是否存在被引用的页面。 If it does, the page fault handler reads the page into physical memory, alters the page tables to so the logical address maps to the correct physical address, then restarts the instruction. 如果是这样,则页面错误处理程序会将页面读入物理内存,将页面表更改为,以便逻辑地址映射到正确的物理地址,然后重新启动指令。

If the virtual page does not exist, the operating system raises an access violation exception. 如果虚拟页面不存在,则操作系统会引发访问冲突异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM