Basic concepts:

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

Physical address: For memory chip level unit addressing, AndCPU 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
stay32 On the processor platform of bit,linux The address range of both storage space and virtual storage space is from 0x00000000 reach 0xFFFFFFFF
common4G, But the layout of physical storage space and virtual storage space is totally different.

Each process can be assigned to a division1G Out of kernel space3G Independent memory space of, Code segment, Data segments and heaps are from low address to high address, Stack from high address to low address4K The size of is a cell label, Called page.

Real mode and protection mode

CPU There are three ways to work: Real mode, Protection mode and virtual8086 Pattern. It's real mode when it's just started, Only access1M The following memory; When the operating system is running, it switches to protection mode, All-out32 Address bus valid, Addressable4G Physical address space of.
with8086 take as an example, Arithmetic unit(ALU) The maximum number of addresses for is16, Address bus is20, Memory capacity is1M. Addressing mode is Physical address = Base address + Offset, Base address =
16 Segment address of bit << 4; with80386 take as an example,CPU Same size as address bus, Memory capacity is4G. Addressing mode   Linear address = Base address + Offset( Logical address
), In general, the logical address is consistent with the linear address, adoptPage_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 addressd And the physical page number are spliced into the actual physical address.
usec 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 obtained Physical address * Parameter is 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);// Apply1G Resources 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 progressfork() 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 topics • Ubuntu Chinese Forum
Virtual memory introduction andMMU Working principle( One) - CSDN Blog
Logical address, The relationship between linear address and physical address - CSDN Blog
Real mode and protection mode - CSDN Blog <>