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?
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?
Simply because the CPU supports that kind of translation directly, using page tables. OS sets up those page tables beforehand to tell CPU where to look when it references a memory address. 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.
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.
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.
User contributions licensed under CC BY-SA 3.0