0.11linux Memory Management

Source: Internet
Author: User

The Linux memory management program has two main functions: 1. Implement the segmentation and paging mechanisms. 2. Addressing protection mechanism of memory.

Segment virtual addresses are converted to linear addresses, and physical addresses are converted to linear addresses by paging linear addresses.

A system can have multiple page Directory tables, but only one page Directory table is available at a certain time. The current page Directory table is determined by the CPU's, it stores the physical address corresponding to the current page Directory table. (The 0.11 kernel only uses one page Directory table ). A page Directory table occupies a page and uses 4 bytes to address a page Directory item. This way, a page Directory table can address 1024 page Directory items. Page Directory items can also address 1024 pages. Such a page Directory table can address 1024*1024*4096 = 4 GB memory space.

After the segment conversion mechanism, the kernel code and data segment are located at the first 16 MB of the linear address space. After the paging mechanism, the kernel code and data segment are mapped to 16 MB of physical space. Therefore, for the kernel segment, A linear address is a physical address.

For task 0, the page table has a total of four pages after the page Directory items. For applications, the page table uses the memory applied to the memory management program when the process was created, in the primary memory zone.

Concepts:

1. Page box address: the page box address specifies the physical start address of a page of memory. Because the Memory Page is on the 4 K boundary, its 12-bit low is zero. The table Item Page address in a page Directory table specifies the starting address of a page table. The page box address of the page table item specifies the starting address of the physical memory area to be operated.

2. main Memory zone: The 0.11 kernel supports a maximum of 16 MB of physical memory. The kernel code and Data Segment occupy the first part (Task 0 is also included), and then the buffer zone is 4 MB, the remaining memory is the primary memory and the process operations of the memory management program.

In the 0.11 kernel, page. S is used to handle page missing exception interruptions and call the do_no_page () and do_wp_page () functions. The first function is used for page missing exception handling. The required page is retrieved from the block device to the corresponding memory. The second function is used to copy the page when writing and Cancel Page sharing.

3. Linux linear Address Allocation

Currnet-> BRK = ex. a_bass + (currnt-> end_data = ex. a_data + (currnet-> end_code = ex. a_text ));

The linear address of each task starts at nr * 64 MB (nr is), and the size of the logical address space occupied is also 64 MB. The Environment Parameter occupies 128 K, and the left side is the stack pointer of the city (the dynamic memory allocation area in the city ).

 

Finally, page error exception handling: Replication at write time and loading required technology.

 

In memory management, the primary memory is used to remove (0. In the 16 M memory of 11, 1 m is used for the kernel, and 15 m is used for the high-speed cache Virtual Disk and main memory area ).

# Define low_mem 0x10000

# Define paging_memory (15*1024*1024)

# Define paging_pages (paging_momery> 12)

# Define map_nr (ADDR)-low_mem)> 12) // obtain the page number to be mapped.

Physical memory ing diagram: 1 byte represents one page of memory, and the number of mappings corresponding to each page represents the number of references on this page.

Static unsigned char mem_map [pagine_pages] = {0 ,};

 

Refresh page transform Cache

# Define invalidate ()/

_ ASM _ ("movl % eax, % Cr": "A" (0 ))

Note: To speed up page conversion, the CPU stores recently used page table data in the high-speed cache of the chip. After modifying the page table information, refresh the cache. Because only one page Directory table (starting from address 0) is used in 0.11, and the base address of the page Directory is also stored in the control register of, that is, eax.

Get_page (): This function only finds a used page table by checking the mem_map ing table and returns the starting address. However, it does not map the page table to the corresponding linear address, put_page.

Free_page_table Function

Int free_page_table (unsigned long from, unsigned Long SIZE)

{

Unsigned long * page_table; // used to save the page table address

Unsigned long * Dir, NR; // DIR is used to save the base address of the page Directory.

If (from & 0x3fffff)

Panic ("..");

If (! Form)

Panic ("..");

Size = (size + 0x3fffff)> 22; // size + 0x3fffff is used to add 1 if the remainder exists, because one page table can manage 4 MB physical memory (1024*4096), the 22-bit right shift will get the number of page tables to be released.

Dir = (unsigned long) (from> 20) & 0 xffc); // first, from> 20 is used to obtain the base address of the corresponding page Directory through from, because the page table is located in the same directory as the page table, a page Directory table contains 1024 page Directory items, thus indexing 1024 page tables, so the from field shifts 20 places to the right. Then, block the index of the two lower pages from the above 0xffc.

// Now we get the number of page tables to be released by size, and the base address of the Dir page Directory. The corresponding release operation is performed below.

For (; size --> 0; Dir ++)

If (! (1 & * DIR) // check the low position of the page Directory to determine whether the directory item is valid

Continue;

Page_table = (unsigned long *) (0xfffff000 & * DIR); // obtain the base address of the table item corresponding to the page Directory

For (Nr = 0; NR <1024; NR ++)

If (1 & * page_table) // release if the corresponding page table item is valid

Free_page (0xfffff000 & * page_table); // 0xfffff000 & * page_table returns the physical address of the corresponding page table item.

* Page_table = 0; // the corresponding page and table items are cleared.

Page_table ++; // perform the next page table

Free_page (0xfffff000 & * DIR); // release the page table items corresponding to the page Directory

* Dir = 0; // The page Directory items are cleared.

 

Invalidtae (); // refresh the page to swap the cache;

Return 0;

}

Note:

Size = (size + 0x3fffff)> 22; number of released page tables

Dir = (unsigned long *) (from> 20) & 0 xffc); // page Directory start pointer

 

Page_table = (unsigned long *) (0xfffff000 & * DIR); // obtain the base address of the page table of the corresponding directory

Free_page (0xfffff000 & * page_table); // release the physical address of the corresponding page table item

Why should I use 0xfffff000 for operations? Because this is the address indicated by the page frame (20 characters in height)

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.