Calculating number of page table entries given bits of virtual & physical memory addresses & each virtual page? - virtual-memory

Is there any sort of formula to calculate the number of page table entries if I am given:
number of bits in address of virutal memory
number of bits in address of physical memory
size of each virtual page in KB
I think it is 2^some number but is there a formula to find that 'some number'?

Related

Am I reading the virtual addresses correctly?

I have been given a page table for a system with 12-bit virtual ad physical addresses and 256-byte pages.
Say I am given an virtual address (in hexadecimal) that reads 0x3E5.
Am I reading this correctly by saying the first hexadecimal, in this case 3, is the page number and the other 2 hexadecimals are the offset?
This because the page sizes, 256-bytes, which is 2^8, meaning 8 bits are for the page size.
Yes, but only if it is a system with byte addressing. Otherwise, you would divide the page size (256 bytes) by the word size to determine the number of addresses within a page, log2 of which would give you the number of page offset bits.

Calculating The NO of pages in 32-bit Virtual machine

Consider a 32-bit virtual and physical address space with physical page size of 4KB. Assume that a process has
just been created and its program copied into memory. Size of program is 1 KB. How much memory will be
required to store the page tables of this process?
I did this :
.
PTE size for 32bit physical address = 32 bits
= 4 bytes.
no of PTEs in single level page table = virtual pages= virtual address space / page = 2^32 / 2^ = 2^20
so no of pages = 2^20.
But in the solution they have also written as:
"#Pages in single level page table = #PTEs x PTE size / page size
= 2^20 x 4 bytes / 2^12 bytes
= 2^10. "
as far as I understand page table is an array of page table entries ,one per virtual page . So no of page table entries should be equal to no of Pages.
so then why #Pages is equal to 2^10??
Another thing what is "with physical page size of 4KB." ?? Pages are virtual address so does that mean a page occupy 4KB of space on physical memory??
Any sort of help will be appreciated.
For a single level of page tables; the virtual address space is 4 GiB and a page is 4 KiB so you will need "4 GiB/4 KiB = 1048576 pages = 1048576 page table entries". This is the answer you came up with (2^20 page table entries).
If there are 1048576 page table entries and you have no idea how large a page table entry is, then the total amount of memory needed for all page table entries will be "1048576 * unknown = unknowable".
However, with a 32-bit physical address space size and 4 KiB page size we know that 12 bits of an address will be used for "offset within page" and the remaining 20 bits of an address will be used for "page number"; therefore we know that a page table entry must be at least 20 bits, so we know that the total amount of memory needed for all page table entries will be at least "1048576 * 20 bits = 20971520 bits = 2560 KiB".
If we make random potentially false assumptions (based on educated guesses) and decide that maybe a page table entry is 4 bytes, we could say that the total amount of memory needed for all page table entries will probably be "1048576 * 4 bytes = 4 MiB".
If the total amount of memory needed for all page table entries is 4 MiB and a physical page is 4 KiB; then you will need "4 MiB / 4 KiB = 1024 pages" to store all the page table entries. This is the answer they came up with (2^10 pages).
Another thing what is "with physical page size of 4KB." ?? Pages are virtual address so does that mean a page occupy 4KB of space on physical memory??
Pages aren't virtual addresses; and there are both virtual pages and physical pages.
The basic idea is to split a virtual address into two parts (with masking/AND and shifting) so that "(virtual_page_number << K) + offset_within_page = virtual_address", then use "virtual_page_number" (and page tables) to find "physical_page_number", then do "physical_address = (physical_page_number << K) + offset_within_page".
For this to work, the size of a virtual page must be the same as the size of a physical page; so the words "page size" are used to refer to both the virtual page size and the physical page size.

Can a Page Number of a given virtual address be equals to 0 in memory mapping?

Say you were given a Page Size of 6 KB (6144 bytes) and a Virtual Address value of 2309 using the formula: Page = Virtual Address / Page size, which is 0,376 or just 0 is this possible in a real-world situation, thanks in advance?
Pages are integer entities, so there are consecutive page addresses from 0 to maximum number of pages, identical to virtual addresses that exists from 0 to maximum memory size (ignoring canonicality) - Virtual address 2309 is the byte number 2309 in page number 0.
Having said that, virtual page doesn't have to be present and doesn't have to be mapped to physical address, so it is not necessary possible to access any random address in the virtual address range.

converting virtual address to physical address

Wanted to know how should i be processing the information of converting the virtual address
(0x10002400)
which contains the value floor(n/2^10)
to physical address
Details given are 32 bit address bus
512 KB physical memory and
page size of 32 KB
Contents of a memory location does not impact the virtual to physical address translation.
Your virtual address got 8 hex values, which indicates this is a 32 bit virtual address.
Your physical memory size is 512KB, which means there are 2^19 bytes (512 * 1024 Bytes).
In this case, virtual to physical mapping involves mapping a 32 bit address to a 19 bit address.
In your example, the page size is 32KB, which means there are 16 physical pages (512/32). We need 4 bits to index 16 physical pages.
From the 32 bit virtual address, we use the last 4 bits to index into a physical page. We can use the remainder 28 bits to do the comparison.
There is an structure called "page table" which holds this information.
This is basically a mapping of leading 28 bits (32-4) of the virtual address to the leading 15 bits (19-4) physical address.
In the given example, the virtual address is 0x10002400.
Last 4 bits are represented by hex 0. Therefore the index is 0. So we take the 0th entry from the page table. Then we check if the virtual tag in the page table matches with our virtual tag which is 1000240 (Note: The last hex value is dropped). If the virtual tag in the entry matches 1000240, then we use the physical tag from the 0th entry and construct our physical address by appending the same index, which is zero.

When will the virtual page number have more bits than the physical frame number?

I have the following question on a practice exam:
For a computer system that implements paging, under what circumstance will the VPN (virtual page number) have more bits than the PFN (physical frame number)?
I am trying to argue that:
The number of bits to represent the virtual page number and physical frame number equal are equal. Even if the system doesn't have enough memory available to fill the entire physical address space, the same number of bits will be used.
On the 80386 processor, 20 bits are used for the virtual page number and there are 20 bits are used to represent physical frame numbers.
Is there a circumstance where the VPN will have more bits than the PFN?
You are asking when the can the virtual address space be greater than the physical address space.
The answer is almost always these days.
Few virtual memory systems support as much physical memory as virtual memory.

Resources