One, virtual memory
Let's take a look at a picture (from the full anatomy of the Linux kernel), as follows:
Fragmentation mechanism: It is divided into code segments, data segments, stack segments. Each memory segment is associated with a privilege level, that is, 0~3,0 has the highest privileged level (kernel), 3 is the lowest privilege level (user), and each time a program attempts to access (the permissions are divided into readable, writable, and executable) a segment, the current privilege level CPL will be compared with the privilege level of the segment , To determine whether you have permission to access it. Each privilege level has its own stack, and when a program switches from one privileged level to another, the stack segment changes to the new level of the stack.
Segment selector: Each segment has a segment selector. The segment descriptor indicates the size of the segment, access rights, and the privileged level of the segment, the segment type, and the position of the first byte of the segment in the linear address space (called the base address of the segment). The segment selector is used to index the segment descriptor in the Descriptor table.
Virtual address: The offset portion of the virtual address and the base site of the segment can be used to locate the position of a byte in the segment, that is, the address in the linear address space.
Paging mechanism: When the paging mechanism is used, each segment is divided into pages (usually 4KB in size per page) and the page is stored in physical memory or on the hard disk. If the paging mechanism is disabled, then the linear address space is the physical address space.
When a program tries to access an address location on a linear address space, the following actions occur:
C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28
|
|
if (data in physical memory) { Virtual address converted to physical address Read data } Else { if (data on disk) { If (physical memory is still idle) { Read data from disk to physical memory Virtual address converted to physical address Read data } Else { To deposit data from a page in physical memory to disk Read the data from the disk to the physical memory of the page Virtual address converted to physical address Read data } } Else { Error } } |
where MMU is responsible for translating the virtual address to the physical address, and the segmentation and paging operations use the Cong and page tables residing in memory to specify their respective exchange information. If the user program wants to access a virtual address, the MMU checks the unauthorized access (privilege level), theMMU generates an exception, the CPU switches from user mode to privileged mode, jumps to the kernel code to execute the Exception service program, the kernel interprets the exception as a segment error, and terminates the process that caused the exception.
Second, the Linux process address space
As can be known from the front, the process has 4G of addressing space, wherein the first part of the "User space" to map its entire process space (0x0000 0000-0xbfff FFFF) that is 3G bytes of virtual address, the second part of the "system Space" for mapping (0xc000 0000-0xffff FFFF) 1G bytes of virtual address. As
Show it in more detail as follows:
Program path: full absolute path string such as "/home/simba/code/asm/simple"
Environment variables: Environment variables like path,home under Linux, which inherit the environment variables of the parent process.
Command-line arguments: Like Ls-l,-L is the command-line argument, and LS is an executable program.
Stack: Is the stack, the program needs to do data operations here, storage of temporary data, open function stacks and so on. Under Linux, the stack is high address to low address growth.
For the function stack, the function is finished to release memory, for example, the return to say, has been open down the function stack, and then regained from the bottom, so recursive too many layers will likely cause stack overflow.
Local variables (which do not contain static variables), and local readable variables (const) are allocated on the stack.
Shared library and Mmap memory map area: For example, many programs will use printf, function shared library PRINTF.O fixed in a physical memory location, so many process mappings to share . Mmap is a system function, you can map a portion of a disk file directly to memory, so that the location of the file directly has a corresponding memory address, read and write to the file can be done directly with pointers do not need to read/write function. In addition, calling malloc when normal is called BRK system call allocates memory, under certain conditions is called Mmap to map physical memory to the process address space.
Heap: The memory requested by malloc, freed with free, and released at the end of the process if it is not actively released.
Text Segment: Executable (binary) (. text), global initialization of read-only variables (const) (. rodata), string constants (. Rodata), all assigned here.
Data Segment: Global variables (initialized in. data, uninitialized in. BSS), static variables (global and local)(initialized in. data, uninitialized in. BSS), and global uninitialized read-only variables (. BSS);
Linux process address space and virtual memory