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
- Depending on the program size,
program-size/page-size
page
's will be created. So here it would be16B/8B = 2
pages numbered as page1 and page2. - Now, the Program Counter (PC) would be set to the Virtual Address of
a
which is0
and the CPU will somehow resolve it and load the corresponding instruction from the real physical address ofa
which is24
here. - 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 inpage1
,0x08
means to lookup inpage2
. Divide by 8 and look into thepage{quotient}
with the remainder indicating the offset to look into the real memory? - Suppose there is an instruction
d
that refers to the memory corresponding too
which is inpage2
, 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? - Now, when there are multiple programs how do we uniquely determine
what is
page1
? Could another program have apage1
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? - Suppose we finished executing the instruction
h
, the PC would automatically load the Virtual Address ofi
, which gets automatically resolved topage2
using the same above principle?