Q & A-address space in Linux

Source: Internet
Author: User
Confused-Linux address space (A) (reproduced) http://blogold.chinaunix.net/u3/94700/showart_2434805.html has such a series of problems, is it bothering you: user program compiled connection formed by the address space in what range? What is the scope of the address space after the kernel is compiled? What is the I/O address space for access to peripherals?
Answer the first question first. The most common executable file format in Linux is Elf (executable and linkable format ). In the executable code in ELF format, LD always schedules the "code segment" of the program starting from 0x8000000, which is true for each program. As for the actual address in the physical memory during program execution, the memory ing is temporarily allocated by the kernel. The specific address depends on the physical memory page allocated at that time.
We can use the Linux utility objdump to disassemble your program to know its address range.
For example, suppose we have a simple C program Hello. C.
# Include <stdio. h>
Greeting ()
{
Printf ("Hello, world! /N ");
}
Main ()
{
Greeting ();
}
This simple program is written into two functions to illustrate the instruction transfer process. We use GCC and LD to compile and connect them to get the executable code hello. Then, use the Linux utility objdump to disassemble it:
$ Objdump-d hello
The main snippets are as follows:
08048568 <greeting>:
8048568: pushl % EBP
8048569: movl % ESP, % EBP
804856b: pushl $0x809404
8048570: Call 8048474 <_ init + 0x84>
8048575: addl $0x4, % ESP
8048578: Leave
8048579: Ret
804857a: movl % ESI, % ESI
0804857c <main>:
804857c: pushl % EBP
804857d: movl % ESP, % EBP
804857f: Call 8048568 <greeting>
8048584: Leave
8048585: Ret
8048586: NOP
8048587: NOP

Where,Image08048568This is what we often call a virtual address.(This address actually exists, but it seems "virtual" because of the existence of the physical address ).

. Virtual Memory, kernel space, and user space

The Linux virtual memory size is 2 ^ 32 (on a 32-bit x86 machine). The kernel divides the 4G Byte space into two parts. The maximum 1G bytes (from the virtual address 0xc0000000 to 0 xffffffff) are used by the kernel.Kernel space". The lower 3G bytes (from the virtual address 0x00000000 to 0 xbfffffff) are used by each process.User space". Because each process can enter the kernel through a system call, the Linux kernel space is shared by all processes in the system. Therefore, from the perspective of a specific process, each process can have 4G bytesVirtual Address Space(Also calledVirtual Memory).

Each process has its own private user space (0 ~ 3G), this space is invisible to other processes in the system. The maximum 1 GB kernel space is shared by all processes and kernels. In addition,User space"Is also called"Address SpaceIn the subsequent descriptions, we will not distinguish the two terms.
User space is not shared by processes, but isolated by processes. Each process can have a maximum of 3 GB user space. A process's access to one of the addresses will never conflict with other processes's access to the same address. For example, a process can read an integer 8 from its user space address 0x1234abcd, while another process can read an integer 20 from its user space address 0x1234abcd, this depends on the logic of the process.
At any time point, only one process is running on one CPU. Therefore, for this CPU, there is only one 4 GB virtual address space in the system at this time, and this virtual address space is for this process. When a process is switched, the virtual address space also changes. It can be seen that each process has its own virtual address space, and its virtual address space is known only when the process is running. At other times, its virtual address space is unknown to the CPU. Therefore, although each process can have 4 GB of virtual address space, there is only one virtual address space in the eyes of the CPU. Changes in the virtual address space as the process switches.
From the above we know that the address space formed after a program is compiled and connected is a virtual address space, but the program will eventually run in the physical memory. Therefore, any virtual address provided by the application must be converted to a physical address. Therefore, the virtual address space must be mapped to the physical memory space, this ing relationship needs to be established through the data structure specified by the hardware architecture.This is what we call the segment descriptor table and page table,Linux mainly uses page tables for ing.
Therefore, we come to the conclusion that if the given page table is different, the CPU will convert the addresses in a virtual address space into physical addresses. Therefore, we create a page table for each process and map the virtual address space of each process to the physical address space as needed. Since only one process can be running on a certain CPU at a time, when the process is switched, the page table is also changed to the page table of the corresponding process, this allows each process to have its own virtual address space without affecting each other. Therefore, at any time, for a CPU, you only need to have the page table of the current process to convert its virtual address to the physical address.

. Ing of kernel space to physical memory

The kernel space is shared with all processes, where kernel code and data are stored, and the user space of the Process stores the code and data of the user program, whether it is a kernel program or a user program, after they are compiled and connected, the resulting commands and symbol addresses areVirtual Address(See the example in section 2.5), instead of the physical address in the physical memory.
Although the kernel space occupies a maximum of 1 GB in each virtual space, the ing to physical memory always starts from the lowest address (0x00000000), as shown in 4.2, it is used to establish a simple linear ing relationship between the kernel space and the physical memory. 3 GB (0xc0000000) is the displacement between the physical address and the virtual address. It is called page_offset in Linux code.

Let's take a look at the description and definition of address ing in the kernel space in the include/ASM/i386/page. h header file:

# DEFINE _ page_offset (0xc0000000)
......
# Define page_offset (unsigned long) _ page_offset)
# DEFINE _ Pa (x) (unsigned long) (x)-page_offset)
# DEFINE _ VA (x) (void *) (unsigned long) (x) + page_offset ))
For kernel space, given a virtual address X, its physical address is "X-page_offset". Given a physical address X, its virtual address is "x + page_offset ".

It is explained again that the macro _ Pa () only maps the virtual address of a kernel space to a physical address, but it is not applicable to the user space. The address ing of the user space is much more complex, it is done through the paging mechanism.

Confusing-Linux kernel space (2)(Reprinted) Figure 1 1 1 GB address space starting from page_offset

First, describe the meaning of the symbol in the figure:
Page_offset: 0xc0000000, that is, 3 GB
High_memory: the literal meaning of this variable is high-end memory. What is high-end memory,
[Url = javascript:;]Linux[/Url]
The kernel stipulates that the first 896 of Ram is the so-called low-end memory, while the first 896 ~ A total of MB of 1 GB memory is high-end memory. If your memory is 512 M, What Is high_memory? Is 3 GB + 512, that is, the physical address x
In the source code, the mem_init function has the following line:
High_memory = (void *) _ VA (max_low_pfn * page_size );
Max_low_pfn indicates the maximum number of pages in the physical memory.
Therefore, in the figure, the physical memory ing is between page_offset and high_memory. There is only a simple linear relationship between the physical address and the virtual address.
It should also be noted that the kmalloc () function is called to allocate memory during this period. Conversely, the physical pages of the memory allocated through kmalloc () are continuous.

Vmalloc_start: the starting address of the discontinuous zone.
Vmalloc_end: end address of the discontinuous Area
In the discontinuous area, there is an 8 Mb security zone between the end of the physical memory ing and the first vmalloc to "capture" out-of-bounds access to the memory. For the same reason, insert the other 4 kb security zone to isolate the discontinuous zone.

The vmalloc () function is called for the allocation of discontinuous zones.

Both vmalloc () and kmalloc () are functions used to allocate memory in kernel code, but what is the difference between them?
We can see from the previous introduction that the memory allocated by these two functions is in the kernel space, that is, from 3 GB ~ 4 GB; but the location is different, the memory allocated by kmalloc () is 3 GB ~ High_memory: the kernel space and physical memory are mapped one by one, and vmalloc_start ~ exists in the vmalloc () allocation ~ Between 4 GB, the non-contiguous memory area mapped to the physical memory may also be non-consecutive.
Vmalloc () works in a similar way as kmalloc (). The main difference is that the physical address allocated by the former does not need to be consecutive, the latter ensures that pages are physically consecutive (virtual addresses are also consecutive ).
Although physical contiguous memory blocks are only required in some cases, many kernel code calls kmalloc () instead of vmalloc () to obtain the memory. This is mainly because of performance considerations. The vmalloc () function must create a page table to convert a physically discontinuous page into a continuous page in a virtual address space. Also, the pages obtained through vmalloc () must be mapped one by one (because they are not physically consecutive), which leads to a buffer refresh that is much larger than direct memory ing. For these reasons, vmalloc () is used only when absolutely necessary-typically to obtain a large memory, for example, when a module is dynamically inserted into the kernel, load the module to the memory allocated by vmalloc.
The vmalloc () function is easy to use:
Char * Buf;
Buf = vmalloc (16 * page_size);/* get 16 pages */
If (! Buf)
/* Error! Memory cannot be allocated */
After using the allocated memory, you must release it:
Vfree (BUF );

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.