CSCE611-OS-Project03
Github Link: https://github.com/tamu-edu-students/CSCE410-611-Spring2024-Hanzhong_Liu
Files I modified:
- cont_frame_pool.H/C
- page table.H/C
- copykernel.sh
- Modified to fit Mac OS

Step 1 Init memory pools:
We have two pools : kernel pool and process pool. Kernel pool is used for store infomations from kernel like page table. Process pool is to allocate memory to processes. When a page fault is triggered, the page fault handle will get a frame from process pool and map it from the address that fault happened.
Step 2 add page fault handle
1 | class PageFault_Handler : public ExceptionHandler { |
In this code, we add a handle for fault 14, which is page fault. The page fault will be processed by the static function PageTable::handle_fault().
Step 3 Init paging
1 | PageTable::init_paging(&kernel_mem_pool, |
In this code, we first init the PageTable’s static variables by pass two memory pools to it (init_paging). In init_paging, we, first apply one frame from kernel pool for page directory. Than, we map the first 4MB of memory to it physical address(use another frame as the first page table). This can help us able to access kernel pool’s frames directly.
This is the menory structure:

After that, we use pt.load to anable paging:
1 | void PageTable::load() |
Step 4 process page faluts

Firstly, get fault virtual memory address from cr2”
1 | unsigned long faulting_address = read_cr2(); |
Get page dir index and page table index from address. pd_index is the top 10 bits and pt_index can get from bits from 12 to 22.
1 | unsigned long pd_index = faulting_address >> 22; // index in page dir |
For two level page table, may the page table is not exist. We can determine it by check the first bit of pd[pd_index].
1 | if (!(pd[pd_index] & 1)) { |
In the end, we apply a new frame from process pool and add it to page table.
1 | unsigned long new_frame = process_mem_pool->get_frames(1); |
Result: