Top Analysis of mapping relationship between Linux virtual address and Physical address "go"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/ordeder/article/details/41630945

Copyright NOTICE: This article for Bo Master (http://blog.csdn.net/ordeder) original article, without Bo Master permission not reproduced.

Directory (?) [-]

    1. Virtual space
    2. Organization of the process virtual address
      1. 1 Virtual Space User space
      2. 2 Memory Interval
    3. Organization of the system physical address
      1. 1 User Space page directory mapping relationships
      2. 2 Mapping of user space
      3. 3 Mapping of kernel space virtual addresses
    4. Relational graph of related data structures

Ordeder original article, original link: http://blog.csdn.net/ordeder/article/details/41630945

Source Version 2.4.0

1. Virtual Space

0-3G User space 0x00000000 ~ 0XBFFFFFFF

3-4G kernel space 0xc0000000 ~ 0xFFFFFFFF

Each user process has a separate user space (virtual address 0-3), and the kernel space is unique (equivalent to sharing)

The user space for each process is described by mm_struct, which is task_struct.mm.

2. Organization of the process virtual address 2.1 virtual space, user space

[CPP]View PlainCopy 
  1. struct Mm_struct {
  2. struct vm_area_struct * MMAP; / * List of VMAs * /
  3. ...
  4. pgd_t * PGD; //For address mapping
  5. atomic_t mm_users; / * How many users with user space? * /
  6. atomic_t Mm_count; /* How many references to "struct Mm_struct" (Users count as 1) */
  7. int map_count; /* Number of VMAs * /
  8. ...
  9. //Description segment distribution of user space: Data segment, code snippet, stack segment
  10. Unsigned long start_code, End_code, Start_data, End_data;
  11. Unsigned long start_brk, BRK, Start_stack;
  12. Unsigned long arg_start, Arg_end, Env_start, env_end;
  13. Unsigned long rss, TOTAL_VM, LOCKED_VM;
  14. ...
  15. };
The above structure describes the structure of the process's user space, where
Pgd_t is used when the process user-space address is mapped to a physical address
Vm_area_struct is the virtual address range that the process user space has mapped to the physical space, and Mmap is a linked list of that space block.

void in virtual space: chunks that have not been mapped in the virtual space (i.e. not used), then there is no vm_area_struct structure

2.2 Memory Interval

[CPP]View PlainCopy 
  1. /*
  2. * This struct defines a memory of the VMM memory area. There is one of the These
  3. * per Vm-area/task. A VM area was any part of the process virtual memory
  4. * Space that have a special rule for the Page-fault handlers (ie a shared
  5. * Library, the executable area etc).
  6. */
  7. struct Vm_area_struct {
  8. struct mm_struct * VM_MM; / * VM Area parameters * /
  9. unsigned long vm_start; //Virtual space start address
  10. unsigned long vm_end; //Termination address
  11. / * Linked list of VMS areas per task, sorted by address * /
  12. struct vm_area_struct *vm_next;
  13. //Permissions and flags for this zone
  14. pgprot_t Vm_page_prot;
  15. unsigned long vm_flags;
  16. //links to some Vm_area
  17. ...
  18. struct vm_operations_struct * vm_ops;
  19. unsigned long vm_pgoff; / * Offset in page_size units, *not* page_cache_size * /
  20. struct file * vm_file; //For mapping disk files to user space
  21. ...
  22. };

In the description of the virtual space interval:
Vm_start/vm_end is the starting and ending address of the block
Vm_file is used in file mapping, commonly used mmap (FD,...) function, which simply says that the virtual space is mapped to a file in the kernel buffer, then this time access to the virtual space will be different from the PGD mapping.
Vm_operations_struct is the operation of this virtual interval, where the Nopage function pointer is used to handle memory pages. For a common memory map, this page-handling function maps virtual addresses to physical addresses (anonymous mappings) for Do_no_page (): Allocates physical pages & sets PGD & Pte.
And for MMAP operation related virtual address, its pages processing function will be related to the file system's fault function, Filemap_nopage (), the file system's fault from the disk to load the relevant file block, such as kernel buffer.

[CPP]View PlainCopy 
    1. struct Vm_operations_struct {
    2. void (*open) (struct vm_area_struct * area);
    3. void (*close) (struct vm_area_struct * area);
    4. struct page * (*nopage) (struct vm_area_struct * area, unsigned long address, int write_access); //pages Operation
    5. };


3. Organization of the physical address of the system

The kernel organizes the physical addresses into pages, and the struct page describes the physical pages of the system, but the data content of the pages is not in the structure. The system has a global data struct page mem_map[], which is used to record each physical page.
The page size is 4KB, which is embodied in the source code (Page_shift = 12)

[CPP]View PlainCopy 
  1. /*
  2. * Try to keep the most commonly accessed fields in a single cache lines
  3. * Here (bytes or greater). This ordering should is particularly
  4. * Beneficial on 32-bit processors.
  5. *
  6. * The first line was data used in page cache lookup, the second line
  7. * is used for linear searches (eg. clock algorithm scans).
  8. */
  9. typedef struct Page {
  10. struct List_head list;
  11. struct Address_space *mapping;
  12. unsigned long index;
  13. struct page *next_hash;
  14. atomic_t count;
  15. unsigned long flags; / * Atomic flags, some possibly updated asynchronously * *
  16. struct List_head LRU;
  17. unsigned long age;
  18. wait_queue_head_t wait;
  19. struct page **pprev_hash;
  20. struct Buffer_head * buffers;
  21. void *virtual; / * non-null if kmapped * /
  22. struct zone_struct *zone;
  23. } mem_map_t;

The struct page is used to describe a physical page, which is simply a description, meaning that the 4KB data of the page is stored in a contiguous 4kb physical space (as determined by the MMU, see below). which
LRU page buffering scheduling policy (minimum use preference)

Off Topic:
Page can also be used for file buffering, related parameters and functions:
Buffer_head is a device file-related operation, for example, in a filesystem, a page of file has 4 blocks that are stored in the memory specified by the Buffer_head list.
Index is the page number in the file system that is used for file buffering.

3.1 User Space page directory (mapping Relationship)

In the virtual space description of the process, the PGD is used for the mapping of the page store. When a process switch occurs in the kernel, the PGD unit in the CPU is loaded into the CR3 register, and the Mmu cell in the processor is mapped on the page based on the CR3 register.

PGD,PMD and PTEs can be seen as arrays that implement mappings for the address space of the process to the physical space. The high address of the virtual address determines the PGD, the middle segment address determines PMD, and the low address determines that PTE,PTE is "page table entry".
A pointer to the corresponding physical page is stored in the final located Pte. [CPP]View PlainCopy  
    1. typedef struct {unsigned long pte;} pte_t;
    2. typedef struct {unsigned long PMD;} pmd_t;
    3. typedef struct {unsigned long pgd;} pgd_t;
    4. typedef struct {unsigned long pgprot;} pgprot_t; //Operation Mark


3.2 Mapping of user space:

1. Virtual address of user space vaddr find the corresponding page table entry X (i.e. physical address) via MMU (PGD,PMD,PTE)
2. The high 20 bits of the page table entry x are physical, the physical page number is index = x >> page_shift, and the same is the first address of the physical page table after index 12 0.
3. Through the physical page number, we can again find the description of the physical page in the kernel pointer Mem_map[index], of course, this pointer is a virtual address, page structure see above.


3.3 Mapping of kernel space virtual addresses:

There is a direct mapping between the kernel space and the physical address, without the need to pass the MMU (PGD) to the user space. System spatial Mapping (3G start) to physical space 0G start:
For example:
The virtual address loaded by the system kernel image is the starting address of 3g+1m, then the corresponding physical address is 1M.
Immediately thereafter the allocation of the virtual address (physical address 2-9m) of the 8M is allocated at 3G+2M for PDG
The 16M space is then reserved for storage with DMA.
The mem_page[] array of the global page structure starts at 0xc1000000.
So the conversion of the virtual address of the kernel space to the physical address is:

[CPP]View PlainCopy 
    1. Page_offset = 3GB
    2. Vitr_to_phys (Kadd)
    3. return Vadd-page_offset
    4. The virtual address of the kernel space vaddr is the page structure that finds its corresponding physical address in the following way:
    5. Vitr_to_page (Vadd)
    6. index = Virt_to_phys (kadd) >> Page_shift
    7. return Mem_map[index]

4. Related data structure diagram

Description

1. Black + Red Arrows show the mapping between virtual address space and physical space

2. The blue arrow involves the mapping operation of the file Mmap (), compared to the anonymous mapping, the file map is more than the file layer of disk IO.

Top Analysis of mapping relationship between Linux virtual address and Physical address "go"

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.