Hi,
Sounds like you got the idea behind it, but will add a few more details.
Why user process can use up to 4 GiB while we only have 1 GiB RAM?: The
present bit tells us if a page is "in memory" or not. When this bit is clear, we can use the remainder fields however way we want to by introducing
Software PTE types - such as to implement
page swapping which allows us to do this.
Assumptions... I am going to assume for now that all that code is at 1MB and that we only need to set up Identity Space, in which case you will probably be fine with just mapping the first page directory entry (first 4MB). Don't worry about mapping anything else. Even if you want a higher half kernel (
we can discuss that later), you'll still want the first 4MB as Identity Space anyways (with perhaps an exception for the Zero Page.) From what I can see, I believe this should be fine for your system.
Note on mapping all of memory... The code you posted appears to be trying to map all of the address space -- which isn't what you want to do. The goal is to set up Identity Space and Kernel Space (and possibly others). Nothing else needs to be mapped to memory.
For reference, here is our code for setting up Identity Space. I edited out the irrelevant parts for what you should focus on:
Code: Select all
KernelPageDirectory = MmGetFreeFrame();
IdentitySpacePageTable = MmGetFreeFrame();
/* Page Directory. */
PageDirectoryEntry = (MMPDE*) MM_FROM_PFN (KernelPageDirectory);
PageDirectoryEntry [0].u.device.valid = 1;
PageDirectoryEntry [0].u.device.pageFrameNumber = IdentitySpacePageTable;
/* Identity Space. */
PageTableEntry = (MMPTE*) MM_FROM_PFN(IdentitySpacePageTable);
for (Index = 0; Index < 1024; Index++) {
PageTableEntry[Index].u.device.valid = 1;
PageTableEntry[Index].u.device.pageFrameNumber = Index;
}
HalWritePdbr (PageDirectoryEntry);
That's it. Your first initial paging code shouldn't be any more complex (you would just need to add an extra set of tables for the PDPT.)
Note on vmm_alloc_page... Your
vmm_alloc_page calls
kheap_temp. This is
backwards. When setting up paging, you only need free
Page Frame Numbers (PFN)'s from the frame allocator, nothing else. The heap allocator should call the VMM AllocPage if needed and AllocPage should call the PMM GetFreeFrame if it needs to map something into memory. Until the VMM is set up, you should only use the PMM FreeFrame.
Note on bit fields... Compilers can handle bit fields in different ways. Be absolutely sure the compiler is treating bit fields as you expect them too.