So for the last several years I've been on/off working on my OS and have recently decided to restart it due to having forgotten what I was doing on the last incarnation of it (a 13 month break does not do favours to development for me). However the point in which I always seem to get stuck and confused with is with paging.
Usually the problem arises for me around the handling page directories/tables and physical and linear addresses. However this time I think I've been able to get past that particular problem. However now I'm getting a new problem, which I'm having difficulty understanding, or at least building a picture of in my head. That is the Page Fault interrupt. I understand that it will get triggered and the faulting linear address will be placed inside CR2. I can get that and work out which page needs to be allocated/created. However the part I'm having difficulty getting my head around (and maybe I'm over complicating things here) is how do I go about allocating this space?
I'll give a bit of information about the kernel thus far.
- Located at 0x00100000.
- The entire first 4MiB of memory (BIOS/Kernel/etc) is identity mapped.
- I plan to use 4MiB to 16MiB as kernel heap space. The VM will detect who is requesting memory, or what purpose it is for and assign a linear address accordingly.
- User land programs will ultimately exist from 0x10000000 (for no other reason that its a nice round number)
- Reference unmapped linear address (i.e. 0x10000000)
- Page Fault Occurs
- Calculate Page Directory Entry ((cr2 >> 22) & 0x3ff) = 0x40
- Calculate Page Table Entry ((cr2 >> 12) & 0x3ff) = 0x00
- page = pm_alloc_page() - Allocates the next available physical page in memory. Returns physical address.
- vm_alloc_page(page, pde, pte) - simply allocates the page table entry
However I have a couple of concerns. They are:
- What happens if the page lies inside a page table that hasn't been setup yet?
- If I allocate a page table through pm_alloc_page() directly the page table won't be directly usable when paging is enabled will it?