finarfin wrote:I have to request a new 4k page physically or logically?
You have to request a new 4K page
physically. The value in the
CR3 register is a physical address. All addresses in the page directory and page tables are physical addresses.
finarfin wrote:If i request a physical page how I can initialize it? I map it temporary on the current pagedir?
If you are currently operating inside a virtual address space then you would be _required_ to have the physical page mapped into your address space using some means in order to initialize it.
finarfin wrote:What exactly are the steps to follow to allocate a new pagedir?
- Allocate a new physical memory page. (Or, find one and set it as used)
- Map the new physical map so you can access it.
- Initialize the new physical memory page.
A simple setup that I have seen most people pick is to reserved a certain region of virtual memory as kernel space. I would pick this region to contain your current kernel. Then you create two sets of functions or a manner of choosing between allocating memory for kernel or user space.
The idea is kernel space is a range of tables in the page directory of every process that remain the same. You essentially use the same tables in this range for all directories. This range is the kernel space. This should allow you to allocate new page directory from physical memory and map them into kernel space so that all page directory (for all processes) are accessible in kernel space from ring zero(supervisor).
Code: Select all
0x0 0x5000000 0xffffffff
[ kernel space ][ user space ]
I acquired the value
0x5000000 by choosing the 21st page directory entry, or also known as the 21st page table. So I choose to reserve the 1st to the 21st page table for kernel space. The 22nd and 1024th table are user space. Also to realize is that the 21st entry has an index of 20.
Code: Select all
directory[0] = 1st entry
directory[1] = 2nd entry
....
directory[20] = 21st entry
And, if you remember parts of a virtual address in binary (which are):
AAAAAAAAAABBBBBBBBBBCCCCCCCCCCCC
Where
A is the page directory index,
B is the page table index, and C is the byte index in the page. We can find the directory and table index from the value
0x5000000 above by doing:
Code: Select all
directory_index = 0x5000000 >> (10 + 12);
table_index = (0x5000000 << 10) >> 12; /* shift to the left to clear the directory index, and back to the right to clear the byte index */