Basic concepts :

Logical address : In the computer with address transformation function , Operands given by access instructions .

Physical address : For memory chip level unit addressing , And CPU Corresponding to the address bus connected .

Linear address : Middle layer of logical address and physical address translation , Address before page conversion of hardware platform .

We all know that any program running independently needs the system to allocate separate memory space , In most cases, the work is done by the system , Convenient program access to variables , The program does not need to care about the physical address of the variable . So modern operating systems provide an abstraction of memory management , Virtual memory . Logical address template of process using virtual memory , The operating system helps to convert to a real physical address .

Virtual storage space distribution
stay 32 On the processor platform of bit ,linux The address range of both storage space and virtual storage space is from 0x00000000 reach 0xFFFFFFFF
common 4G, But the layout of physical storage space and virtual storage space is totally different .

Each process can be assigned to a division 1G Out of kernel space 3G Independent memory space of , Code snippet , Data segments and heaps are from low address to high address , Stack from high address to low address 4K The size of is a cell label , It's called a page .

Real mode and protection mode

CPU There are three ways to work : Real mode , Protection mode and virtual 8086 pattern . It's real mode when it's just started , Access only 1M The following memory ; When the operating system runs, it switches to protection mode , all-out 32 Address bus valid , Addressable 4G Physical address space of .
with 8086 take as an example , Arithmetic unit (ALU) The maximum number of addresses for is 16, Address bus is 20, Memory capacity is 1M. Addressing mode is Physical address = Base address + Offset , Base address =
16 Segment address of bit << 4; with 80386 take as an example ,CPU Same size as address bus , Memory capacity is 4G. Addressing mode   Linear address = Base address + Offset ( Logical address
), In general, the logical address is consistent with the linear address , adopt Page_Tables Get physical address .

Page management system address conversion
linux Store virtual memory in _ The corresponding relationship of physical memory pages is in the home directory /proc/pid/pagemap file , The following figure shows the address conversion method of page management system ,p
Represents the logical page number ,f Indicates the physical page number ,d Indicates the address on the page .

The function of page table is to realize the address mapping from logical page number to physical page number , The physical page number of the page can be obtained by retrieving the page number of the logical address ; At the same time, the page address d And the physical page number are spliced into the actual physical address .
use c Language to realize the conversion of logical address to physical address
#include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h>
#include <assert.h> #include <fcntl.h> #include <stdint.h> int add(int a,int b)
{ return 0; } int g = 10; /* * Logical address Mapping results Physical address * Parameters are Logical address */ uint64_t
vir_to_phy(unsigned long int vaddr) { int pagesize = getpagesize(); int index =
vaddr/pagesize;// Logical page number p int offset = vaddr%pagesize;// In page offset d int fileoffset =
index*8;//pagemap Read in file Physical page number int fd = open("/proc/self/pagemap",O_RDONLY); if(fd
== -1) { return 0; } lseek(fd,fileoffset,SEEK_SET); uint64_t val = 0;
read(fd,&val,sizeof(val)); uint64_t phy = 0; if(val & (uint64_t)1<<63) { val &=
(((uint64_t)1<<55)-1); phy = val*pagesize+offset;// Splicing physical address return phy; } return
0; } int main() { int a = 0; printf(" local variable a = %x\n",&a); printf(" global variable g =
%x\n",&g); printf(" Global function add = %x\n",add); printf(" Main program entry main = %x\n",main); int
*p = (int*)malloc(1024*1024*1024);// apply 1G Resources for pid_t pid = fork(); if(pid == 0) {
//memset(p,0,1024*1024*1024); } // Mapping in parent-child processes int b = vir_to_phy(&p);
printf("virtul a = %x\n",&p); printf("physical a = %x\n",b); free(p); exit(0); }
Running the program we found , When the requested resource is not used , Logical address returned by parent-child process , Same physical address ; After the memory assigned to the subprocess request , The physical address returned by the parent-child process is not the same . in other words , In progress fork() After replication, the parent and child processes share memory , But when one of the processes modifies one of the data structures , System will generate page missing interrupt , Allocate new space for it .

reference material : Convert logical address to physical address _ Baidu Wenku
( turn )Linux Physical and virtual storage layout in - View theme • Ubuntu Chinese Forum
Virtual memory introduction and MMU working principle ( One ) - CSDN Blog
Logical address , The relationship between linear address and physical address - CSDN Blog
Real mode and protection mode - CSDN Blog <>