Linux0.11 kernel-Memory Management 1. initialization, linux0.11 Memory Management

Source: Internet
Author: User

Linux0.11 kernel-Memory Management 1. initialization, linux0.11 Memory Management

[All Rights Reserved. For more information, see the source. Source: http://www.cnblogs.com/joey-hua/p/5597705.html]

The Linux kernel uses the memory paging mechanism, so it is easier to understand. Memory paging is used to facilitate memory management.

When it comes to memory paging, the root part is the page Directory table, in head. h:

Extern unsigned long pg_dir [1024]; // memory page Directory array. Each directory item is 4 bytes. Starting from the physical address 0.

Then let's look at head. s:

/** Head. s contains 32-bit startup code. * Note !!! The 32-bit startup code starts from the absolute address 0x00000000, which is also the place where the page Directory will exist. * therefore, the startup code here will be overwritten by the page Directory. */. Text. globl _ idt, _ gdt, _ pg_dir, _ tmp_floppy_area_pg_dir: # The page Directory will be stored here ....

The page Directory starts with absolute address 0. Next, allocate the page table:

/* Linus places the kernel memory page table directly in the page Directory and uses four tables to address 16 Mb of physical memory. * If you have more than 16 Mb of memory, you need to modify it here. */# Each page table is 4 Kb (one page memory page), and each page table item requires 4 bytes. Therefore, a page table can store #1024 table items. If a page table item addresses 4 Kb of address space, a page table can address 4 Mb of physical memory. # The format of the page table item is: the first 0-11 bits of the item store some flags, for example, whether it is in memory (P-Bit 0), read/write permission (R/W-bit 1) # normal users or Super Users (U/S bits 2), whether modified (dirty) (D bits 6), and so on; 12-31 is # The page box address, which is used to point out the physical start address of a page of memory .. Org 0x1000 #1000 page tables starting from 0 x 1st offset (the page table directory will be stored at the beginning of 0 offset ). Pg0:. org 0x2000pg1:. org 0x3000pg2:. org 0x4000pg3 :...

4 page table space is allocated. Because a page table item corresponds to a page, that is, 4 K, a page table is 4 K * 1024 = 4 M, there are 4 page tables, so it is 16 Mb. Note that this is from. org 0x1000, and the space of the previous 4 K is left to the entire page Directory.

The following describes how to set the attributes of four page Directory items. Because there are only four page tables, the corresponding four page Directory items are:

# In the following four statements, set items in the page Directory table. Because we (kernel) have four page tables, we only need to set four items. # The structure of the page Directory items is the same as that of the items in the page table. The four bytes are one item. See the description in the above 113 rows. # "$ Pg0 + 7" indicates 0x00001007, which is the first item in the page Directory table. # The address of the 1st page tables = 0x00001007 & 0xfffff000 = 0x1000; # The attribute flag of the 1st page tables = 0x00001007 & 0x00000fff = 0x07, indicates that the page exists and the user can read and write. Movl $ pg0 + 7, _ pg_dir/* set present bit/user r/w */movl $ pg1 + 7, _ pg_dir + 4/* --------- "" --------- */movl $ pg2 + 7, _ pg_dir + 8/* --------- "" --------- */movl $ pg3 + 7, _ pg_dir + 12 /*---------""---------*/

Note that + 7 indicates that we should first look at the pdpa.Page Directory item formatWe can see that the 0-11 bits of each page Directory item are properties, the 12-31 bits are the base address, and the binary value of 7 is 111, that is, the bits of P, R/W, and U/S are 1.

Okay. Now the page Directory and four page tables are allocated. Next, enter the initialization program mem_init (main_memory_start, memory_end); in main. c and memory. c:

Static long memory_end = 0; // physical memory (in bytes) of the machine ). Static long buffer_memory_end = 0; // The end address of the high-speed buffer. Static long main_memory_start = 0; // the location where the primary memory (used for paging) starts. Memory_end = (1 <20) + (EXT_MEM_K <10); // memory size = 1 Mb + extended memory (k) * 1024 bytes. Memory_end & = 0xfffff000; // The number of memories in less than 4 kb (1 page) is ignored. If (memory_end> 16*1024*1024) // if the memory exceeds 16 Mb, 16 Mb is used. Memory_end = 16*1024*1024; if (memory_end> 12*1024*1024) // if the memory is greater than 12 Mb, set the buffer end to 4 Mb buffer_memory_end = 4*1024*1024; else if (memory_end> 6*1024*1024) // otherwise, if the memory is greater than 6 Mb, set the buffer end to 2 Mb buffer_memory_end = 2*1024*1024; else buffer_memory_end = 1*1024*1024; // otherwise, set the buffer end to 1 Mb main_memory_start = buffer_memory_end; // primary memory start position = buffer end; // if the memory virtual disk is defined, the virtual disk is initialized. At this time, the main memory will be reduced. See kernel/blk_drv/ramdisk. c. # Ifdef RAMDISK // if the memory virtual disk is defined, the primary memory will be reduced. Main_memory_start + = rd_init (main_memory_start, RAMDISK * 1024); # endif mem_init (main_memory_start, memory_end );

Here is a reference:

If a memory virtual disk is defined, the start position of the primary memory zone main_memory_start starts from the end of the virtual disk; otherwise, it starts from the end of the high-speed buffer zone; and the end of the primary memory zone is 16 Mb.

/* If the following definition needs to be modified, it must be consistent with head. the related information in files such as s is changed together * // The maximum memory size supported by the linux 0.11 kernel by default is 16 Mb. You can modify these definitions to fit more memory. # Define LOW_MEM 0x100000 // low-end memory (1 MB ). # Define PAGING_MEMORY (15*1024*1024) // The paging memory is 15 MB. The master memory zone can be up to 15 MB. # Define PAGING_PAGES (PAGING_MEMORY> 12) // The number of physical memory pages after pagination. It is equivalent to dividing by 4096 # define MAP_NR (addr)-LOW_MEM)> 12) // The specified memory address is mapped to the page number. # Define USED 100 // indicates that the page is occupied. For details, see row 405. Static long HIGH_MEMORY = 0; // global variable, which stores the highest-end address of the actual physical memory. // Memory ing byte chart (1 byte represents 1 page memory). The Bytes corresponding to each page are used to indicate the number of times the page is currently referenced (occupied. Static unsigned char mem_map [PAGING_PAGES] = {0,}; // initialize the physical memory. // Parameter: start_mem-start position of the physical memory used for paging (the memory occupied by RAMDISK has been removed ). // End_mem-the maximum actual physical memory address. // In the linux kernel of this version, a maximum of 16 Mb of memory can be used. Memory larger than 16 Mb is not considered. // 0-1 Mb memory space is used for the kernel system (in fact, it is 0-640Kb ). Voidmem_init (long start_mem, long end_mem) {int I; HIGH_MEMORY = end_mem; // you can specify the maximum memory size. For (I = 0; I <PAGING_PAGES; I ++) // first, set all the pages to the occupied (USED = 100) status, and mem_map [I] = USED; // set all the page ing arrays to USED. I = MAP_NR (start_mem); // then calculate the page number that can use the starting memory. End_mem-= start_mem; // calculate the size of memory blocks that can be processed by page. End_mem> = 12; // calculate the number of pages that can be used for paging. While (end_mem --> 0) // finally clears the page ing array corresponding to these available pages. Mem_map [I ++] = 0 ;}

First, pay attention to PAGING_MEMORY. Because the memory space below 1 MB is used by the kernel and does not participate in paging, the maximum size of the main memory is 15 MB. Therefore, PAGING_PAGES refers to the total memory page size divided by 4096 (4096 bytes for one page.

Therefore, mem_map is used for Memory Page ing.

Here, pay attention to the macro MAP_NR (start_mem). Use the starting address of the main memory area to subtract 1 MB of space not involved in paging to obtain the capacity starting from the memory address that can be used for paging, divide by 4096 (one page is 4096 bytes) to get the page number.

Then, the size of the primary memory area is calculated, and the page number starting from start_mem in mem_map is set to 0.

So far, the initialization of memory management is over!

Related Article

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.