Linux user space and kernel space __linux

Source: Internet
Author: User

a.introduction (32-bit system)



Linux simplifies the segmentation mechanism so that virtual addresses (logical addresses) are always consistent with linear addresses, so Linux's virtual address space is also 0~4g (2^32).

The Linux kernel divides the 4G bytes of space into two parts. The highest 1G bytes (from the virtual address 0xc0000000 to 0xFFFFFFFF) are used for the kernel, called "kernel space." The lower 3G bytes (from the virtual address 0x00000000 to 0xBFFFFFFF) are used for each process, called "User space." Because each process can enter the kernel through system calls, the Linux kernel is shared by all processes within the system. Thus, from the point of view of a specific process, each process can have 4G bytes of virtual space.


* Process Addressing space 0~4g
* Process can only access 0~3g in user state, only enter kernel state to access 3g~4g
* Process enters kernel state via system call
* The 3g~4g part of each process virtual space is the same
* Process from user state into kernel state does not cause CR3 changes but it can cause stack changes

















About Virtual kernel space (logical address) mapping to physical space:

When the kernel module code or thread accesses memory, the memory address in the code is a logical address, and the corresponding to the real physical memory address requires a one-to-one mapping of the address, such as the logical address 0xc0000003 corresponding to the physical address of 0x 3,0xc0000004 the corresponding physical address is 0x4, ..., the relationship between the logical address and the physical address is

Physical Address = logical address –0xc0000000

Logical Address Physical memory Address
0xc0000000 0x0
0xc0000001 0x1
0xc0000002 0x2
0xc0000003 0x3
... ...
0xe0000000 0x20000000
... ...
0xFFFFFFFF 0x40000000??

For kernel space, its address mapping is a very simple linear mapping, 0xc0000000 is the physical address and linear address between the amount of displacement, in the Linux code is called Page_offset.

B.outline


First, Linux user space and kernel space

Refer:http://blog.chinaunix.net/uid-15007890-id-3415331.html
Second, a step-by-step inquiry into Linux process address space refer:http://soft.chinabyte.com/os/51/12324551.shtml




C.contents


first, Linux user space and kernel space

Linux operating systems and drivers running in the kernel space, the application running in user space, the two can not simply use the pointer to pass data, because Linux uses the virtual memory mechanism, user space data may be swapped out, when the kernel space using user space pointers, the corresponding data may not be in memory.

We know that the Linux operating system each process of the address space is independent, in fact, the Independent is said to be independent of the physical space.

1. Linux kernel address mapping model

The x86 CPU uses a Segment-page address mapping model. The address in the process code is a logical address, and the physical memory is actually accessed after a section of page address mapping.

The section-page mechanism is shown below.

2.Linux Kernel address space partition

Typically, the 32-bit Linux kernel address space is divided into 0~3g for user space and 3~4g for kernel space. Note that this is a 32-bit kernel address space partition, and 64-bit kernel address space partitioning is different.

The origin of high-end memory in 3.Linux kernel

When the kernel module code or thread accesses memory, the memory address in the code is a logical address, and the corresponding to the real physical memory address requires a one-to-one mapping of the address, such as the logical address 0xc0000003 corresponding to the physical address of 0x 3,0xc0000004 the corresponding physical address is 0x4, ..., the relationship between the logical address and the physical address is

Physical Address = logical address –0xc0000000

Logical Address Physical memory Address
0xc0000000 0x0
0xc0000001 0x1
0xc0000002 0x2
0xc0000003 0x3
... ...
0xe0000000 0x20000000
... ...
0xFFFFFFFF 0x40000000??

Assuming that the above simple address mapping relationship, then the kernel logical address space access to 0xc0000000 ~ 0xFFFFFFFF, then the corresponding physical memory range is 0x0 ~ 0x40000000, that is, only 1G physical memory access. If the machine installs 8G physical memory, then the kernel can only access the former 1G physical memory, the later 7G physical memory will not be able to access, because the kernel's address space has already mapped all to the physical memory address range 0x0 ~ 0x40000000. Even if 8G physical memory is installed, then the physical address of the 0x40000001 memory, how the kernel to access it. Code must have a memory logical address, 0xc0000000 ~ 0xFFFFFFFF address space has been used up, so can not access the physical address 0x40000000 memory.

It is obvious that the kernel address space 0xc0000000 ~ 0XFFFFFFF is not all used for simple address mapping. So the x86 architecture divides the kernel address space into three parts: ZONE_DMA, Zone_normal, and Zone_highmem. Zone_highmem is the high-end memory, which is the memory of the concept of high-end memory.


In the x86 structure, the three types of zones are as follows:

16MB of ZONE_DMA memory start

Zone_normal 16MB~896MB

zone_highmem 896MB ~ End

4.Linux Kernel high-end memory understanding

Earlier we explained the origin of high-end memory. Linux divides the kernel address space into three parts ZONE_DMA, Zone_normal, and Zone_highmem, and the high end memory High_mem address space ranges from 0xf8000000 to 0xFFFFFFFF (896MB~1024MB). So how does the kernel use the 128MB high-end memory address space to achieve access to all of the physical memory .

When the kernel wants to access more than 896MB Physical address memory, from the 0xf8000000 ~ 0xFFFFFFFF address space to find a corresponding size of the free logical address space, borrow for a while. Using this logical address space, create a map to the part of the physical memory you want to visit (that is, populate the Kernel Pte page table), temporarily for a while, and then return it. So that others can also borrow this address space to access other physical memory, to achieve the use of a limited address space, access to all of the physical memory. The following figure.

For example, the kernel would like to access the first 2G of physical memory with a size of 1MB, that is, the physical address range is 0x80000000 ~ 0x800fffff. Access to a 1MB size of the free address space before the visit, assuming that the free address space found is 0xf8700000 ~ 0xf87fffff, with this 1MB logical address space map to the physical address space 0x80000000 ~ 0X800FFFFF memory. The mapping relationship is as follows:

Logical Address Physical memory Address
0xf8700000 0x80000000
0xf8700001 0x80000001
0xf8700002 0x80000002
... ...
0xf87fffff 0x800fffff

When the kernel accesses the 0x80000000 ~ 0x800fffff physical Memory, it will release 0xf8700000 ~ 0XF87FFFFF kernel linear space. This allows other processes or code to access other physical memory using the 0xf8700000 ~ 0xf87fffff address.

From the above description, we can know the most basic idea of high-end memory : to borrow a paragraph of address space, set up a temporary address mapping, after the release, to reach this address space can be reused, access to all physical memory.

See here, can not help but someone will ask: In case there is a kernel process or module to occupy a segment of the logical address space does not release, how to do. If this happens, the kernel's high-end memory address space becomes more and more tense, and if all is occupied and not released, no mapping to physical memory can be made accessible.

In some office buildings in Tsim Sha Tsui, Hong Kong, toilets are few and locked. If the client wants to go to the restroom, he can get the key to the front desk and return the key to the front desk after the convenience. In this way, although there is only one toilet, but can meet the needs of all customers to go to the bathroom. If a customer has been using the bathroom and the key is not returned, then other clients will not be able to use the bathroom. The idea of high-end memory management in the Linux kernel is similar.


5.Linux Kernel high-end memory partitioning
The kernel divides high-end memory into 3 parts: Vmalloc_start~vmalloc_end, Kmap_base~fixaddr_start, and fixaddr_start~4g.


For high-end memory, you can get the corresponding page by Alloc_page () or other functions, but to access the actual physical memory, you have to convert the page to a linear address. Think about how MMU accesses physical memory, that is, we need to find a linear space for the page corresponding to the high-end memory, a process called a high-end memory map.

For 3 parts of high-end memory, there are three ways to high-end memory mapping:
Map to "Kernel Dynamic mapping Space" (noncontiguous memory allocation)
This approach is simple because it is possible to get a page from a high-end memory (see the implementation of the Vmalloc) by Vmalloc (), while the "kernel Dynamic mapping Space" is requesting memory, so that high-end memory may be mapped to "kernel dynamic mapping Space".


Persistent kernel mappings (permanent kernel mapping)
If you get the high end memory page by Alloc_page (), how to find a linear space for it.
The kernel specifically leaves a linear space, from Pkmap_base to Fixaddr_start, for mapping high-end memory. On the 2.6 kernel, this address range is 4g-8m to 4g-4m. This space is called "Kernel permanent Mapping Space" or "permanent kernel mapping Space". This space and other space use the same page table of contents, for the kernel, is swapper_pg_dir, for ordinary processes, through the CR3 register point. Typically, this space is 4M in size, so you just need a page table, and the kernel uses it to pkmap_page_table to find the page table. by Kmap (), you can map a page to this space. Because this space is 4M in size, you can map up to 1024 page at a time. Therefore, for a page that is not used, and should be released from this space (that is, the mapping relationship), through Kunmap (), a page corresponding to the linear address can be released from this space.


Temporary mappings (Temporary kernel mapping)
The kernel retains some linear space between the Fixaddr_start and the fixaddr_top for special needs. This space is called a "fixed mapping space" in which part of the temporary mapping of high-end memory.

This space has the following characteristics:
(1) Each CPU occupies a space
(2) in the space occupied by each CPU, divided into several small spaces, each small space size is 1 page, each small space for a purpose, these purposes defined in the Kmap_types.h In the Km_type.

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.