Analysis of mapping relationship between Linux virtual address and Physical address

Source: Internet
Author: User

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

struct Mm_struct {struct vm_area_struct * mmap;/* List of VMAs */...pgd_t * pgd;//for address mapping atomic_t mm_users;/* how many user s with user space? */atomic_t mm_count;/* How many references to "struct Mm_struct" (Users count as 1) */int map_count;/* number of VMAs */. .//Describes the segment distribution of user space: Data segment, code snippet, stack segment unsigned long start_code, End_code, Start_data, end_data;unsigned long start_brk, BRK, Start_ stack;unsigned long Arg_start, Arg_end, Env_start, env_end;unsigned long rss, TOTAL_VM, LOCKED_VM, ...};
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

/* * This struct defines a memory VMM memory area. There is one of these * per Vm-area/task.  A VM area was any part of the process virtual memory * space that had a special rule for the Page-fault handlers (ie a shar Ed * library, the executable area etc). */struct vm_area_struct {struct mm_struct * vm_mm;/* vm area parameters */unsigned long vm_start;//virtual space start address unsigned long V m_end;//Termination address/* Linked list of VMS areas per task, sorted by address */struct vm_area_struct *vm_next;//the permission and flags of the zone pgprot_t VMS _page_prot;unsigned long vm_flags;//Some vm_area links ... struct vm_operations_struct * vm_ops;unsigned long vm_pgoff;/* Offset in page_size units, *not* page_cache_size */struct file * vm_file;//is used to map disk files to user space ...};

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.

struct Vm_operations_struct {void (*open) (struct vm_area_struct * area), void (*close) (struct vm_area_struct * area); struct page * (*nopage) (struct vm_area_struct * area, unsigned long address, int write_access); Pages operation};


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)

/* * Try to keep the most commonly accessed fields in single cache lines * Here (bytes or greater).  This ordering should is particularly * beneficial on 32-bit processors. * * The first line was data used in page cache lookup, the second line * is used for linear searches (eg. clock algorithm s cans).  */typedef struct Page {struct List_head list;struct address_space *mapping;unsigned long index;struct page *next_hash;ato  mic_t count;unsigned Long flags;/* atomic flags, some possibly updated asynchronously */struct List_head lru;unsigned long age;wait_queue_head_t wait;struct page **pprev_hash;struct buffer_head * buffers;void *virtual; /* non-null if kmapped */struct zone_struct *zone;} 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.
typedef struct {unsigned long pte;} pte_t;typedef struct {unsigned long PMD;} pmd_t;typedef struct {unsigned long PGD ; } pgd_t;typedef struct {unsigned long Pgprot;} pgprot_t;//Action Flag


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:

Page_offset = 3gbvitr_to_phys (kadd)  return Vadd-page_offset the  virtual address of the kernel space vaddr is to find its corresponding physical address in the PAGE structure as follows: Vitr_to _page (vadd) index = Virt_to_phys (kadd) >> Page_shiftreturn Mem_map[index]











Analysis of mapping relationship between Linux virtual address and Physical address

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.