Page Frame Allocation
Posted: Sun Mar 07, 2021 2:58 am
For the last week or so I am trying to write a PFA(Page Frame Allocator) and I through of one algorithm, but I think it is a bit messy and reading some other posts on the forum I think maybe I even have wrong understanding of what PFA should do.
So here is my understanding of PFA, please correct me if i am wrong - the pages that are used to manage the virtual memory should be mapped to physical memory and the task of the PFA is to give out pointer to 4096 bytes that are free so the page can be mapped to them. Also the PFA should take into account that the kernel area and areas that are marked as reserved in memory map should not be used, also I have thought that there should be a function that would let drivers to also reserve space in the physical RAM so PFA won't touch it.
NOTE: I use a byte map not a bitmap, so for each page there are 8 status bits and so 8 flags could be used for each page(flags like free/reserved...)
Now here is my algorithm:
-- Initialization
1. Receive the start address for the first frame(start of page frame allocation memory space - -0x0 on x86)
2. Receive the end address of the last frame(end of PFA allocation space)
* The end address should be rounded DOWN(if it is not 4096 aligned) to the nearest number that is 4096 aligned
3. Mark all of the frames in which unfree zones are located as reserved(using the memory map and driver requests)
4. Mark frames where kernel is located as reserved using variables in the linker script(start and end)
5. Find an address to store the bitmap
1. Calculate the amount of frames needed to store the bitmap; The bitmap should be the size of - `RAM / PAGE_FRAME_SIZE`
2. Search for the first place where the right amount of frames is free(they should be one after another)
3. Mark frames as used by PMM and record the address of the byte/bitmap
4. Zero out all of the frames where the bit/byte map is located
-- Functionality
1. palloc()
1. Search for the first status that equals to FRAME_TYPE_FREE in the bytemap
2. Find the address of the frame that correspond to the bit/bytemap entry
3. Mark found page frame as FRAME_TYPE_ALLOCATED
4. Return the start address of the page frame
2. pfree(address_tt frame_start_address)
1. Use the start address of the page frame supplied to function to find the entry in byte/bitmap(using formulas)
2. Change the entry status to FRAME_TYPE_FREE
3. And also there would be functions for allocating pages that are reserved, freeing pages that are reserved, function to allocate RAM for PMM use
To allocate multiple frames one after another at a time I thought of just putting a special bit flag in the byte-map entry for the page frame that would indicate that the frame is a part of several frames(so they are counted as one space) in a row so the pfree would free all of them
Is my understanding of PFA correct?
Is my algorithm very bad, because I certainly think there are much easier and cleaner ways to do it(especially when OS is only at the start of it's life like mine)?
I heard there is a linked list method of page frame allocation, but can't really understand the algorithm and also how would I make sure that reserved areas aren't touched when using this method?
What methods could I use for PFA(they should be fairly easy to implement as I have only started with OSdev)?
So here is my understanding of PFA, please correct me if i am wrong - the pages that are used to manage the virtual memory should be mapped to physical memory and the task of the PFA is to give out pointer to 4096 bytes that are free so the page can be mapped to them. Also the PFA should take into account that the kernel area and areas that are marked as reserved in memory map should not be used, also I have thought that there should be a function that would let drivers to also reserve space in the physical RAM so PFA won't touch it.
NOTE: I use a byte map not a bitmap, so for each page there are 8 status bits and so 8 flags could be used for each page(flags like free/reserved...)
Now here is my algorithm:
-- Initialization
1. Receive the start address for the first frame(start of page frame allocation memory space - -0x0 on x86)
2. Receive the end address of the last frame(end of PFA allocation space)
* The end address should be rounded DOWN(if it is not 4096 aligned) to the nearest number that is 4096 aligned
3. Mark all of the frames in which unfree zones are located as reserved(using the memory map and driver requests)
4. Mark frames where kernel is located as reserved using variables in the linker script(start and end)
5. Find an address to store the bitmap
1. Calculate the amount of frames needed to store the bitmap; The bitmap should be the size of - `RAM / PAGE_FRAME_SIZE`
2. Search for the first place where the right amount of frames is free(they should be one after another)
3. Mark frames as used by PMM and record the address of the byte/bitmap
4. Zero out all of the frames where the bit/byte map is located
-- Functionality
1. palloc()
1. Search for the first status that equals to FRAME_TYPE_FREE in the bytemap
2. Find the address of the frame that correspond to the bit/bytemap entry
3. Mark found page frame as FRAME_TYPE_ALLOCATED
4. Return the start address of the page frame
2. pfree(address_tt frame_start_address)
1. Use the start address of the page frame supplied to function to find the entry in byte/bitmap(using formulas)
2. Change the entry status to FRAME_TYPE_FREE
3. And also there would be functions for allocating pages that are reserved, freeing pages that are reserved, function to allocate RAM for PMM use
To allocate multiple frames one after another at a time I thought of just putting a special bit flag in the byte-map entry for the page frame that would indicate that the frame is a part of several frames(so they are counted as one space) in a row so the pfree would free all of them
Is my understanding of PFA correct?
Is my algorithm very bad, because I certainly think there are much easier and cleaner ways to do it(especially when OS is only at the start of it's life like mine)?
I heard there is a linked list method of page frame allocation, but can't really understand the algorithm and also how would I make sure that reserved areas aren't touched when using this method?
What methods could I use for PFA(they should be fairly easy to implement as I have only started with OSdev)?