# How does the processing happen in a CPU when it uses Virtual Memory?

I am just trying to visualize how computers work with Virtual Memory/Address.

Assume there is a program on the disk that looks like this:

a b c d e f g h i j k l m n o p where the a .. p are instructions and their address is from 0 to 15.

What happens inside the CPU while executing this program? Assume that the operating system uses pages of 8 bytes.

To begin with, my understanding is that the operating system loads the program into physical memory in page-sized chunks and creates a mapping to the pages of the program numbered from 0..n which looks like:

# assume it is in reverse in the real memory (16-31)!

[i j k l m n o p] [a b c d e f g h]

# cpu's view of the program

page1 (24-31)     page2 (16-31)
[a b c d e f g h] [i j k l m n o p]
|
PC

1. Depending on the program size, program-size/page-size page's will be created. So here it would be 16B/8B = 2 pages numbered as page1 and page2.
2. Now, the Program Counter (PC) would be set to the Virtual Address of a which is 0 and the CPU will somehow resolve it and load the corresponding instruction from the real physical address of a which is 24 here.
3. But wait, how did CPU know that it has to look up into a certain page map, page1 in this case? The operating system knows that but how does the CPU know that? Is it something like the Virtual Address of a program corresponds to one and only one page map and that can be determined directly from the Virtual Address itself? For example, 0x00 automatically means to lookup in page1, 0x08 means to lookup in page2. Divide by 8 and look into the page{quotient} with the remainder indicating the offset to look into the real memory?
4. Suppose there is an instruction d that refers to the memory corresponding to o which is in page2, how will that be resolved? Is it using the same principle? If you know the Virtual Address, then you know the place to look up for the real address?
5. Now, when there are multiple programs how do we uniquely determine what is page1? Could another program have a page1 as well? Is it like page maps are unique (which I think is complex because I thought it has to be sequential and starting from 1 so that given a Virtual Address you can calculate the page map + offset) or is there a per process Page Table managed by the Operating System?
6. Suppose we finished executing the instruction h, the PC would automatically load the Virtual Address of i, which gets automatically resolved to page2 using the same above principle?
• Re, "...and the CPU will somehow resolve it..." Usually that is the responsibility of a Memory Management Unit (a.k.a., "MMU") which is considered to be a separate functional unit from the CPU. The MMU either translates the upper bits of the virtual address to a physical address to which the memory can respond, or else it signals a page fault to the CPU. It's the responsibility of the operating system to decide why a page fault happens and, how to proceed. – Solomon Slow Apr 4 '18 at 19:44