Linux driver address space and hardware address space

Source: Internet
Author: User

 

Author: Li Qiang,Hua Qing vision embedded college lecturer.

When I was driving the course, I had been plagued by the following problems:

● UserProgramWhat is the scope of the address space formed by the compilation connection?
● In what scope is the address space after the kernel is compiled?
● What is the I/O address space for access to peripherals?

So we have this article.ArticleIn general, I would like to introduce some memory-related knowledge points to reduce the students' confusion about memory during the driver class.

Answer the first question first. The most common executable file format in Linux is Elf (executable and linkable format ). Executable in ELF formatCodeIn, LD always schedules the "code segment" of the program 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 OBJ 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 OBJ for disassembly:
$ Obj-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

Among them, an address like 08048568 is a virtual address that we often say (this address actually exists, but it seems "virtual" because of the existence of the physical address ).

Virtual Memory, kernel space, and user space(For some content, see the Chinese version of ulk V3)

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 1 GB (from the virtual address 0xc0000000 to 0 xffffffff) is used by the kernel, which is called the "kernel space ". The low 3G bytes (from the virtual address 0x00000000 to 0 xbfffffff) are used by various processes, which are called "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 4 GB of virtual address space (also called virtual 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, the process's "user space" is also called "address space". In the subsequent descriptions, we will not distinguish these 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. in Linux, the page table is used 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 from kernel space to physical memory

The kernel space and hardware memory address are the most mentioned in the driver. Next we will introduce the kernel space and the actual hardware physical address in detail.

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 command and symbol addresses are virtual addresses rather than physical addresses 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, 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.

To be continued ......

Reference http://www.kerneltravel.net/chenlj/lecture7.pdf

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.