Reprint Annotated Source: http://www.cnblogs.com/lucasysfeng/p/4847662.html
Previous address: http://www.cnblogs.com/lucasysfeng/p/5059767.html
Project Address: Https://github.com/lucasysfeng/lucasOS
The last one tells us how to allocate physical memory to the physical memory. To simplify the problem, instead of using the partner memory allocation algorithm, we put the available memory on the page to the stack, and each allocation is taken from the stack.
The main code in this lecture is in Lib/pmm.c, and some of the code is given below. The content of this talk is not many, readers see the code can.
void init_pmm () {mmap_entry_t *mmap_start_addr = (mmap_entry_t *) glb_mboot_ptr->mmap_addr;mmap_entry_t *mmap_end_ Addr = (mmap_entry_t *) glb_mboot_ptr->mmap_addr+ glb_mboot_ptr->mmap_length;mmap_entry_t *map_entry;for (map_ Entry = MMAP_START_ADDR; Map_entry < mmap_end_addr; map_entry++) {//Type 1 indicates available memory, other means reserved area, refer to previous multiboot_t structure if (Map_entry->type = = 1 && map_entry->base_ Addr_low = = 0x100000) {//free memory to subtract the memory occupied by the kernel itself uint32_t page_addr = map_entry->base_addr_low+ (uint32_t) (kern_end-kern_ start); uint32_t length = Map_entry->base_addr_low + map_entry->length_low;while (page_addr < length && Page_addr <= pmm_max_size) {pmm_free_page (page_addr);p age_addr + = pmm_page_size;phy_page_count++;}}}
void Pmm_free_page (uint32_t p) {assert (Pmm_stack_top! = Page_max_size, "Out of Pmm_stack stack\n");p mm_stack[++pmm_ Stack_top] = p;}
Code acquisition
This series of GitHub addresses Https://github.com/lucasysfeng/lucasOS, the code of this lecture is CODE/CHAPTER5.
Write with me. Operating System (v)-Allocating physical memory