Linux0.11 Memory Management

Source: Internet
Author: User

Linux 0.11 memory management main content. 1: memory initialization linux 0.11 supports a maximum of 16 MB physical memory. The main and mem_init functions initialize the memory. Mem_map [] is used to mark whether memory pages are occupied. Memory_end is the actual memory size obtained by calling with BIOS interruption. If (memory_end> 16*1024*1024) memory_end = 16*1024*1024; # therefore, the maximum memory size is 16 Mb. if (memory_end> 12*1024*1024) buffer_memory_end = 4*1024*1024; # buffer_memory_end is the end address of the high-speed cache. The size of buffer_memory_end is else if (memory_end> 6*1024*1024) related to the total memory size of the machine) buffer_memory_end = 2*1024*1024 else bytes = 1*1024*1024 main_memory_start = buffer_memory_end; mem_init (main_memory_start, memory_end); mem_init will [] Array. In 1 MB ~ Between 16 MB, total (15*1024*1024)> 12 = 3840 pages. Define the array mem_map [3840] to correspond to each page of the memory segment. In the page between main_memory_start and memory_end, the value of the corresponding mem_map [I] is initialized to 0, indicating that it is not used, the remaining items are initialized to 100, indicating that they are occupied. 2: Basic page assignment functions include several basic page assignment and release functions. Get_free_page (): returns the physical address of an idle page. This function searches for items with a value of 0 in the mem_map array and converts them to the physical address of the page to return the address. Free_page (phy_addr): Release the page pointed to by phy_addr. The release operation is to reduce the value of phy_addr in mem_map by 1. Get_empty_page (line_addr): the parameter of this function specifies a linear address. You must obtain a page of physical memory and use line_addr to point to the page of memory. Void get_empty_page (unsigned long address) {unsigned long tmp; if (! (Tmp = get_free_page () |! Put_page (tmp, address) {free_page (tmp); oom (); # insufficient memory} Obviously this function calls get_free_page to obtain a page of physical memory, then, put_page is used to establish a ing between the physical address and the linear address of the physical memory on this page. The process of creating a ing: for 32-bit linear addresses, the height of 10 represents the page Directory Index, the middle 10 is the page table index, and the lower 12 bits is the page offset. Therefore, given a linear address, we can determine its page Directory items through the base address of the page Directory and the linear address height of 10 bits. In Linux 0.11, the base address of the page Directory is 0. Put_page: page_table = (unsigned long *) (line_addr> 20) & 0 xffc) # obtain the page Directory item pointer. If the page table pointed to by this page Directory item exists, locate the corresponding page table items in the page table using the 10-digit middle of the linear address, and save the physical address to create a ing. If the page table does not exist, use get_free_page to allocate a page as the page table and create a ing. Whether creating a ing or copying a page table, you must first consider the page Directory and then the page table. 3: The sys_fork and copy_page_tables functions call copy_process in sys_fork, And the copy_mem function is called in copy_process. This function copies the page table of the parent process to the child process, this is where the parent process and child process share the corresponding code and data segment. Only when the parent process or child process writes the shared memory will the child process be allocated with memory, that is, copy when writing. Copy_mem calls copy_page_table to copy the page table. Copy_page_table (old_data_base, new_data_base, data_limit) returns the linear address of the parent process old_data_base ~ Copy the page table corresponding to old_data_base + data_limit to the sub-process. During the copy process, set each page table item to read-only and execute the corresponding mem_map [I] ++ (equivalent to adding reference count) 4: page error handling there are two different page errors: I) the page to which the page table item points does not exist, that is, the existence bit value of the page table item is 0.ii) page protection mechanism. An error occurred while writing the read-only page. However, when a page error occurs, int 14 is interrupted. The system will execute the _ page_fault: exception handling code. This code will execute do_no_page and do_wp_page (error_code, address) based on two different page errors. This function mainly implements the copy-on-Write function. In copy_page_table, set the page tables of the Parent and Child processes to read-only. When one of the pages is accessed, the interruption is triggered and the do_wp_page function is executed. This function allocates a new page of physical memory and sets the corresponding page table items to read/write. Do_no_page (error_code, address) this function can handle two situations: I) when the application allocates memory, the kernel does not actually allocate physical memory, only when accessing the corresponding memory,. Ii) when executing the exec series functions, the file will be read only when the corresponding memory is accessed. In the first case, do_no_page directly calls the get_empty_page function to get one page of memory. In the second case, there are two steps. I) Call the pai_page function. The main purpose of this function is to share code and data segments. If an executable file already has a process instance running, the new process can share the code and Data Segment of the executable file with other processes. Ii) if the file is executed only once, call the get_free_page function first, read the file content into the memory, and then use the put_page function to create a ing.

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.