Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the memory addressing mode of Linux?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the memory addressing method of Linux". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what is the memory addressing method of Linux" together!

Why Memory Management:

Early programs run directly on physical addresses, which means that the space required by the program does not exceed the physical memory of the machine, but the actual scene is multitasking, multi-process, this physical address reserved for each process is unreliable. For example, if there are 3 programs a,b,c, a needs 10M, b needs 100M, c needs 20M, the total memory is 120M, according to the previous allocation method, the first 10M to a, 10M-110M to b, the system still has 10M, but c needs 20M, obviously the remaining memory is not enough for c. What to do?

1. efficiency problem

You may think that when the c program runs, write the b program data to disk, and then write the data back from disk when running b. Not to mention that it cannot meet the needs of the b and c programs running in parallel, even the time-consuming problems caused by frequent io operations cannot be received.

2. process address isolation problem

In addition to efficiency issues, reserved space for processes crashes if it needs to be accessed by another process. For example, the space accessed by process a is the first 10M, but if a piece of code in program a accesses 10-110M, it may cause the crash of program b, so the address spaces of the processes need to be isolated from each other.

3. relocation problem

In reality, it is impossible for a single task to run in a well-distributed memory. When multiple tasks run in parallel, it is possible to apply for addresses in other processes when dynamically applying for memory release. At this time, it is necessary to relocate to a new address.

Memory management is nothing more than trying to solve the above three problems, how to improve the efficiency of memory use? How do I isolate the address space of a process? How do I fix run-time relocation problems?

How memory management maps from virtual addresses to physical addresses:

Memory management The process of mapping virtual addresses to physical addresses is the process of solving the above three problems. Memory management solves the above three problems with segmentation mechanism and paging mechanism respectively, as shown in the following figure:

Staging mechanism:

As long as the program is segmented and the entire segment is translated to any position, the address within the segment is unchanged relative to the segment base. No matter what the segment base is, as long as the offset address within the segment is given, the CPU can access the correct instruction. Thus, when the user program is loaded, the program can run accurately by copying the contents of the entire segment to the new location and changing the address in the segment base register to that address, because the program uses an intra-segment offset address, and the contents at that offset address are still the same relative to the new segment base address.

It can be seen that the segmentation mechanism solves the problem of isolation and relocation between processes. This action is done in hardware, but some hardware does not have a segmentation mechanism. As a cross-platform linux uses a more general paging mechanism to solve the translation from linear address to virtual address to physical address.

Pagination mechanism:

You can refer to "How does CPU access memory?" To understand the concept of a page table, linux in order to be compatible with 32-bit and 64-bit, usually four page tables, page global directory, page superior directory, page middle directory, page table:

This does not explain in detail how linux uses four-level page tables to translate linear addresses (virtual addresses) to physical addresses. There are many online, recommend https://www.cnblogs.com/linhaostudy/p/10038100.html#autoid-2-2-0.

When the process switches, it finds the pgd field in mm_struct according to task_struct, obtains the page global directory of the new process, and then fills it into CR3 register to complete the page switch.

Take a look at the MMU paging process:

Top Code:

#include #include #include #include #include #include #include #include MODULE_DESCRIPTION("virtual address to physics address"); static int pid; static unsigned long va; module_param(pid,int,0644); //parameters passed from command line (variables, types, permissions) module_param(va,ulong,0644); //va indicates virtual address static int find_pgd_init(void) { unsigned long pa = 0; //physical address represented by pa struct task_struct *pcb_tmp = NULL; pgd_t *pgd_tmp = NULL; pud_t *pud_tmp = NULL; pmd_t *pmd_tmp = NULL; pte_t *pte_tmp = NULL; printk(KERN_INFO"PAGE_OFFSET = 0x%lx\n",PAGE_OFFSET); //How many items are in the page table /* How many bits do pud, pmd, etc. occupy in a linear address */ printk(KERN_INFO"PGDIR_SHIFT = %d\n",PGDIR_SHIFT); //Note: PGD and PUD are the same in 32-bit systems printk(KERN_INFO"PUD_SHIFT = %d\n",PUD_SHIFT); printk(KERN_INFO"PMD_SHIFT = %d\n",PMD_SHIFT); printk(KERN_INFO"PAGE_SHIFT = %d\n",PAGE_SHIFT); printk(KERN_INFO"PTRS_PER_PGD = %d\n",PTRS_PER_PGD); //How many ptrs are in each PGD printk(KERN_INFO"PTRS_PER_PUD = %d\n",PTRS_PER_PUD); printk(KERN_INFO"PTRS_PER_PMD = %d\n",PTRS_PER_PMD); //How many items are in PMD printk(KERN_INFO"PTRS_PER_PTE = %d\n",PTRS_PER_PTE); printk(KERN_INFO"PAGE_MASK = 0x%lx\n",PAGE_MASK); //Mask of page struct pid *p = NULL; p = find_vpid(pid); //find struct pid by process pid number pcb_tmp = pid_task(p,PIDTYPE_PID); //Find the task struct of the process through the structure of pid printk(KERN_INFO"pgd = 0x%p\n",pcb_tmp->mm->pgd); //determine if the given address va is legal (vamm,va)){ printk(KERN_INFO"virt_addr 0x%lx not available.\ n",va); return 0; } pgd_tmp = pgd_offset(pcb_tmp->mm,va); //Returns the linear address va of the corresponding entry in the page global directory printk(KERN_INFO"pgd_tmp = 0x%p\n",pgd_tmp); //pgd_val Get the page global directory entry referred to by pgd_tmp //pgd_val is to print out the values in pgd_tmp printk(KERN_INFO"pgd_val(*pgd_tmp) = 0x%lx\n",pgd_val(*pgd_tmp)); if(pgd_none(*pgd_tmp)){ //Determine if pgd has mapping printk(KERN_INFO"Not mapped in pgd.\ n"); return 0; } pud_tmp = pud_offset(pgd_tmp,va); //Returns the linear address of the parent directory entry of the page corresponding to va printk(KERN_INFO"pud_tmp = 0x%p\n",pud_tmp); printk(KERN_INFO"pud_val(*pud_tmp) = 0x%lx\n",pud_val(*pud_tmp)); if(pud_none(*pud_tmp)){ printk(KERN_INFO"Not mapped in pud.\ n"); return 0; } pmd_tmp = pmd_offset(pud_tmp,va); //Returns va the linear address of the corresponding entry in the middle directory of the page printk(KERN_INFO"pmd_tmp = 0x%p\n",pmd_tmp); printk(KERN_INFO"pmd_val(*pmd_tmp) = 0x%lx\n",pmd_val(*pmd_tmp)); if(pmd_none(*pmd_tmp)){ printk(KERN_INFO"Not mapped in pmd.\ n"); return 0; } //Here, pte_offset_map() is changed to pte_offset_kernel pte_tmp = pte_offset_kernel(pmd_tmp,va); printk(KERN_INFO"pte_tmp = 0x%p\n",pte_tmp); printk(KERN_INFO"pte_val(*pte_tmp) = 0x%lx\n",pte_val(*pte_tmp)); if(pte_none(*pte_tmp)) printk(KERN_INFO"Not mapped in pte.\ n"); return 0; } if(! pte_present(*pte_tmp)){ printk(KERN_INFO"pte not in RAM.\ n"); return 0; } pa = (pte_val(*pte_tmp) & PAGE_MASK) ;//Calculation method of physical address printk(KERN_INFO"virt_addr 0x%lx in RAM Page is 0x%lx .\ n",va,pa); //printk(KERN_INFO"contect in 0x%lx is 0x%lx\n",pa,*(unsigned long *)((char *)pa + PAGE_OFFSET)); return 0; } static void __exit find_pgd_exit(void) { printk(KERN_INFO"Goodbye!\ n"); } module_init(find_pgd_init); module_exit(find_pgd_exit); MODULE_LICENSE("GPL");

The virtual address ffff 99b488d48000 corresponds to the physical address 8000000c8 d48000. This process is also MMU.

Thank you for reading, the above is "Linux memory addressing method is what" content, after the study of this article, I believe we have a deeper understanding of Linux memory addressing method is what this problem, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report