Linux0.11 kernel--memory management 1. Initialization

Source: Internet
Author: User

"All rights reserved, please specify the source of the reprint." Source: http://www.cnblogs.com/joey-hua/p/5597705.html "

The Linux kernel is relatively well understood because it uses a memory paging mechanism. Because memory paging is to facilitate the management of memory.

When it comes to memory paging, the root is the page catalog table, Head.h:

extern unsigned long pg_dir[1024];//memory page directory array. Each directory entry is 4 bytes. Starting from physical address 0.

And then look at Head.s:

/** Head.s contains 32-bit boot code. * NOTE!!! The 32-bit boot code starts with the absolute address 0x00000000, which is also where the page directory will exist, * so the startup code here will be overwritten by the page directory. The */.text.globl _idt,_gdt,_pg_dir,_tmp_floppy_area_pg_dir:# page directory will be stored here. ...

The page directory is stored in the absolute address 0 beginning, the next Allocation page table:

/* Linus put the kernel's memory page table directly behind the page directory, using 4 tables to address the physical memory of Mb. * If you have more than Mb of memory, you will need to make the expansion changes here. */# each page table has a length of 4 Kb bytes (1 pages of memory), and each page table entry requires 4 bytes, so a page table can hold a total of # 1024 table entries. If a page table entry addresses 4 Kb of address space, a page table can be addressed to 4 Mb of physical memory. # page table entries are in the format: the first 0-11 bits of the item hold some flags, such as whether it is in memory (P bit 0), read-write permission (r/w bit 1), # Normal user or Superuser (u/s bit 2), whether it has been modified (dirty) (D bit 6), etc., and the table entry bit 12-31 is the # Used to indicate the physical starting address of a page of memory: Org 0x1000# starting at offset 0x1000 is the 1th page table (the page table directory will be placed at the beginning of offset 0). pg0:.org 0x2000pg1:.org 0x3000pg2:.org 0x4000pg3: ...

4 Page Table space is allocated, because a page table entry corresponds to a page that is 4K, so a page table is 4k*1024=4m, there are 4 page table so it is 16M. And notice that this is starting with. org 0x1000, and the front 4K space is reserved for the entire page directory.

Here are the properties that set the four page catalog entries, because there are only 4 page tables, so the corresponding is 4 page catalog entries:

# The following 4 sentences set the items in the page catalog table, because we (the kernel) have a total of 4 page tables so just set 4 items. # The structure of a page catalog item is the same as the structure of an item in a page table, with 4 bytes of 1 items. See instructions under 113 lines above. # "$pg 0+7" means: 0x00001007, which is the 1th item in the page catalog table. # The 1th page table is located at the address = 0x00001007 & 0xfffff000 = 0x1000;# The 1th page of the table's property flags = 0x00001007 & 0x00000fff = 0x07, indicating that the page exists and that the user can read and write. MOVL $pg 0+7,_pg_dir/* Set present Bit/user r/w */movl $pg 1+7,_pg_dir+4/*  ---------""---------*/MOVL $pg 2+7,_pg_dir +8/*  ---------""---------*/movl $pg 3+7,_pg_dir+12/*  ---------""---------*/

Note that here +7 means, first look at the PDE page directory entry format , the 0-11 bits of each page catalog item is a property, 12-31 is the base address, 7 corresponds to the binary is 111, that is, p, r/w, u/s bits are 1.

OK, now the page directory and the 4 page table are all assigned. Next go to the initialization program Mem_init (Main_memory_start, memory_end); in Main.c and memory.c:

static long memory_end = The number of physical memory (bytes) that the 0;//machine has. static long Buffer_memory_end = 0;//high-speed buffer zone end address.  static long Main_memory_start = The location where the 0;//main memory (which will be used for paging) begins.  Memory_end = (1 <<) + (Ext_mem_k << 10);//Memory size =1MB bytes + extended memory (K) * 1024 bytes.  Memory_end &= 0xfffff000;//ignores the number of memory that is less than 4Kb (1 pages).    if (Memory_end > 16 * 1024 * 1024)//If the memory is more than 16Mb, then the 16Mb meter.  Memory_end = 16 * 1024 * 1024;  if (Memory_end > 12 * 1024 * 1024)//If memory >12MB, set buffer end =4MB Buffer_memory_end = 4 * 1024 * 1024;  else if (Memory_end > 6 * 1024 * 1024)//Otherwise if memory >6MB, then set buffer end =2MB Buffer_memory_end = 2 * 1024 * 1024;  else Buffer_memory_end = 1 * 1024 * 1024;//Otherwise set the buffer end =1MB Main_memory_start = buffer_memory_end;//main memory Start position = Buffer end; If a memory virtual disk is defined, the virtual disk is initialized. The main memory is reduced at this time. See KERNEL/BLK_DRV/RAMDISK.C.  #ifdef ramdisk//If the memory virtual disk is defined, the main memory will be reduced.  Main_memory_start + = Rd_init (Main_memory_start, RAMDISK * 1024x768); #endif mem_init (Main_memory_start, memory_end); 

Here is a reference:

If a memory virtual disk is defined, the main memory area start position Main_memory_start starts at the end of the virtual disk, otherwise it starts at the end of the buffer zone, and the main memory area ends with a memory_end of 16M.

/* The following definitions, if required, need to be changed with relevant information in files such as Head.s the maximum memory capacity supported by the *///Linux 0.11 kernel By default is 16M, and these definitions can be modified to fit more memory. #define LOW_MEM 0x100000//Low-end memory (1MB). #define PAGING_MEMORY (15*1024*1024)//Paging memory 15MB. The main memory area is up to 15M. #define PAGING_PAGES (PAGING_MEMORY&GT;&GT;12)//number of physical memory pages after paging. The equivalent of dividing by 4096#define Map_nr (addr) (((addr)-low_mem) >>12)//Specifies that the memory address is mapped to a page number. #define USED 100//page is occupied flag, see line 405. The static long high_memory = 0;//global variable that holds the actual physical memory highest-end address. Memory mapped byte graph (1 bytes for 1 pages of memory), each page corresponding to the number of bytes used to flag the page is currently referenced (occupied). static unsigned char mem_map[paging_pages] = {0,};////physical memory initialization. Parameters: Start_mem-The starting position of physical memory that can be used as a paging process (the amount of memory space that has been removed from RAMDisk, etc.). End_mem-The actual physical memory maximum address. In this version of the Linux kernel, can use up to 16Mb of memory, more than 16Mb of memory will not be considered, discarded. 0-1MB memory space for the kernel system (in fact, 0-640kb).  Voidmem_init (Long start_mem, long end_mem) {int i;  High_memory = end_mem;//to set the highest memory end.  for (i = 0; i < paging_pages; i++)//First place all pages in occupied (used=100) state, mem_map[i] = used;//The page map array is all set to used.  i = MAP_NR (START_MEM);//Then calculates the page number that can use the starting memory.  End_mem-= start_mem;//calculates the size of the memory block that can be paged. End_mem >>= 12;//to calculate the available paging processingNumber of pages.    while (end_mem--> 0)//Finally, the page map array corresponding to these available pages is zeroed. mem_map[i++] = 0;}

First note paging_memory, because the memory space below 1M is used by the kernel and does not participate in paging, so the main memory size is up to 15M. So Paging_pages is the main memory size divided by 4096 (one page is 4096 bytes) is the total number of memory pages.

So Mem_map's role is memory page mapping.

Notice here that Map_nr (START_MEM) is a macro that uses the main memory area start address minus the 1M space that does not participate in paging, gets the capacity from the memory address that can be used for paging, and divides it by 4096 (one page is 4096 bytes) to get the page number.

It then calculates the size of the main memory area and resets the page number starting in Mem_map from the main memory area Start_mem to 0.

So far, the initialization of memory management is over!

Linux0.11 kernel--memory management 1. Initialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.