linux 启动内存的使用

来源:互联网 发布:mac版视频格式转换器 编辑:程序博客网 时间:2024/05/30 19:34

linux在启动的时候对内存的使用是通过原始的位图管理方式进行分配使用和管理的。

位图方式将整个内存空间分成固定大小的内存页,以页大小进行分配管理的,每一页用一个bit位来标识可用和不可用。整个内存空间的所有内存页就由许多位所组成的位图中的其中一个bit位来进行标识。一个内存页所对应的bit位是1则表示该页已分配,如果是0则表示该页未被分配,可以进行分配使用。

位图管理的是除内核所用内存以外的内存页,即位图的第0个bit位对应的是紧接着内核后的第一个页。内存空间的第0页和内存所使用的页在管理初始化时都将被保留而不能被分配使用。

位图管理的初始化是在void bootmem_init(unsigned int memsize)中完成的。

/* fixes paging routines for avalanche  (utilized in /arch/mips/kernel/setup.c) *///对RAM内存进行位映射管理进行初始化,参数memsize是RAM内存的大小void bootmem_init(unsigned int memsize){    unsigned int memory_end,memory_start;    char *memsize_str;

    /* if no memsize is passed follow the old method*/    if (memsize == 0)    {     memsize_str = prom_getenv("memsize");     if (!memsize_str) {      //如果参数为0,则取用户配置的      memsize = 0x02000000;//32MB     } else {      //如果用户未配置,则取默认值0x02000000(32MB)      memsize = simple_strtol(memsize_str, NULL, 0);        }    }

  //RAM内存的起始地址    memory_start = (unsigned long)PAGE_OFFSET + __MEMORY_START;    //RAM内存的最高地址  memory_end = memory_start + memsize;      /*     * Find the highest memory page frame number we have available      */     //RAM内存最高地址的页帧号    max_pfn = PFN_DOWN(__pa(memory_end));      /*     * Determine the low and high memory ranges      */    max_low_pfn = max_pfn;      /*     * Partially used pages are not usable - thus we are     * rounding upwards:     */     //以位管理内存的方法只对内核使用以外的内存进行位映射管理,start_pfn是需要管理的内存的起始页号。    start_pfn = PFN_UP(__pa(&_end));      /*     * Find a proper area for the bootmem bitmap. After this     * bootstrap step all allocations (until the page allocator is     * intact)  must be done via bootmem_alloc().     */     /*     找一块适当的内存做为位图。这之后内存的使用都必须经过bootmem_alloc进行分配使用     */    bootmap_size = init_bootmem_node(NODE_DATA(0), start_pfn,                               __MEMORY_START>>PAGE_SHIFT, max_low_pfn);

    /*      * Register fully available low RAM pages with the bootmem allocator.     */    {      unsigned long curr_pfn, last_pfn, pages;            /*         * We are rounding up the start address of usable memory:         */        curr_pfn = PFN_UP(__MEMORY_START);            /*         * ... and at the end of the usable range downwards:         */        last_pfn = PFN_DOWN(__pa(memory_end));           if (last_pfn > max_low_pfn)          last_pfn = max_low_pfn;                   pages = last_pfn - curr_pfn;

        free_bootmem_node(NODE_DATA(0), PFN_PHYS(curr_pfn),                             PFN_PHYS(pages));    }

    /*     * Reserve the kernel text and     * Reserve the bootmem bitmap. We do this in two steps (first step     * was init_bootmem()), because this catches the (definitely buggy)     * case of us accidentally initializing the bootmem allocator with     * an invalid RAM area.     */     /*     保留内核代码所用的内存页和位图所使用的页。     */    reserve_bootmem_node(NODE_DATA(0), __MEMORY_START+PAGE_SIZE,              (PFN_PHYS(start_pfn)+bootmap_size+PAGE_SIZE-1)-__MEMORY_START);            /*     * reserve physical page 0 - it's a special BIOS page on many boxes,     * enabling clean reboots, SMP operation, laptop functions.     */     /*     保留第0页内存     */    reserve_bootmem_node(NODE_DATA(0), __MEMORY_START, PAGE_SIZE);}

 

 

原创粉丝点击