"Mr. Wang, let's talk about the memory and I/O access content today. I think it should be a little low first. This part of content is still a little annoying and difficult, I have no idea at all. I was afraid to scare Mr. Wang. I didn't bother to tell you that I was looking at this part at the time.
"Oh, what should we do? No, I'm not afraid of having a TAO brother here, "said Mr. Wang with confidence.
"Oh! I can't see it, but it also makes you comfort me. "looking at Wang's confident look, I have no worries. Let's take a deep breath and start today's course.
We know that in x86, there is an I/O space concept. I/O space is relative to the memory space. It is accessed through specific commands in and out. The port number identifies the peripheral Register address. Unfortunately, embedded controllers such as arm do not provide I/O space, so we will not consider it. We will focus on memory space.
The memory space can be accessed through the address and pointer. In C, the memory space is operated by the pointer, for example, in the 186 processor:
Unsigned char * P = (unsigned char *) 0xf000ff00;
* P = 11;
This sectionCodeIt means that the absolute address 0xf000 + 0xff00 (186 processor uses 16-bit segment address and 16-bit offset address) is written to 11. In arm, PPC, and other processors that do not use segment addresses, P points to the memory
The space is 0xf000ff00, and * P = 11 is written to 11 at this address.
After talking about the memory space, you have to talk about MMU (Memory Management Unit), which assists the operating system in memory management and provides ing between virtual addresses and physical addresses, memory Access permission protection, cache Cache control, and other hardware support. With MMU, the operating system kernel can make users feel likeProgramLarge memory space can be used, so that programmers do not need to consider the actual physical memory capacity in the computer when writing programs.
To understand the basic MMU operating principles, we first introduce several concepts:
1) TLB: Translation lookaside buffer, that is, the conversion bypass cache. It is also called a fast table, which is the cache of the conversion table. A small amount of virtual addresses in the cache correspond to physical addresses.
2) ttw: translation table walk, that is, the conversion table roaming. When TLB does not buffer the corresponding address translation relationship, you need to access the conversion table in the memory to obtain the ing between the virtual address and the physical address. After ttw is successful, the result should be written to TLB.
To illustrate the usage of MMU in memory access, I specially drew a flowchart. As follows:
Now, let's talk about MMU. for processing including MMU, the Linux system provides a complex storage management system that allows the process to access up to 4 GB of memory, this 4 GB is divided into two parts
--- User space and kernel space. The former is generally distributed to 0 ~ 3 GB (that is, page_offset, in 0x86 medium to 0xc0000000), the remaining 3 ~ 4 GB is the kernel space. Generally, a user process can access the kernel space only through system calls.
Mr. Wang, you have learned the C language and know that the malloc () function is used to dynamically apply for memory in the user space, and free is used for release. This is consistent in the use of various operating systems. This content, such as memory leakage, will not be discussed in detail. Our focus is on kernel space. How can we do this?
There is a knowledge point on the front side, which will be used later. You can refer to: Linux kernel space 3 ~ 4 GB is still available, from low to high is: physical memory ing area-> isolation band-> vmalloc virtual memory distributor-> Separation
Take off-> high-end memory ing area-> dedicated page TV zone-> reserved area.
The main functions involved in applying for memory in the Linux kernel space include kmalloc () ,__ get_free_pages (). [the two applied memories are stored continuously in the physical memory ing area, there is a simple conversion relationship with the physical memory] And vmalloc [it provides a continuous memory area in the virtual memory space, not necessarily in the physical memory, there is no simple conversion relationship with the physical memory. I will elaborate on the use of these three functions on the Internet. I have printed all of your functions for you, and you can simply read them.
We can say that there is a certain Conversion Relationship between virtual addresses and physical addresses. Specifically, you can use javas_to_phys () to convert virtual addresses to physical addresses. The code list is as follows:
# DEFINE _ Pa (x )((Unsigned Long) (X)-page_offset)ExternInlineUnsigned LongPerformance_to_phys (Volatile Void* Address ){Return_ Pa (Address );}
The corresponding function is phys_to_virt (), which is used to convert a physical address to a virtual address. The Code is as follows:
# DEFINE _ Pa (x )((Unsigned Long) (X) + page_offset)
ExternInlineUnsigned LongPerformance_to_phys (Volatile Void* Address)
{
Return_ Pa (Address );
}
It is worth noting that the above method only applies to the regular memory, and there is no such simple conversion relationship between the virtual address of the high-end memory and the physical address.
"I don't know how you listen. I have no idea what you said. This chapter is really difficult .."
"Mr. Tao, it's okay. You can rest assured. After so many times, I think I should be able to keep up with it, if you can't keep up with me, "Mr. Wang gave me the confidence to continue.