In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what the user space and kernel space of Linux means. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
one。 The Linux operating system and drivers run in kernel space, and applications run in user space. Both cannot simply use pointers to pass data, because of the virtual memory mechanism used by Linux, user-space data may be swapped out, and when kernel space uses user-space pointers, the corresponding data may not be in memory. The memory mapping of user space is segmented page type, while kernel space has its own rules; the purpose of this paper is to explore the address mapping of kernel space. Os allocates an independent, contiguous, virtual address memory space to each process, which is generally 4G (32-bit operating system, that is, 32 to the power of 2), in which the memory space with high address value is allocated to os, linux os occupies 1G and window os occupies 2G, and the rest of the memory address space is allocated to processes. Typically, the 32-bit Linux kernel virtual address space is divided into user space (3G) and kernel space (note that only 1G linear addresses can be used by the kernel). Note that this is the 32-bit kernel address space partition, while the 64-bit kernel address space partition is different.
Process addressing space: process addressing 4G processes can only access 3G in user mode. Only when the 3G~4G process enters the kernel state can it be accessed. The 3G~4G part of each process virtual space that enters the kernel state through system calls is the same. The process moving from the user state to the kernel state will not cause changes in the CR3 but will cause changes in the stack. Linux kernel high-end memory 1. Origin
When kernel module code or thread accesses memory, the memory addresses in the code are logical addresses, and corresponding to the real physical memory addresses, one-to-one mapping of addresses is required. For example, the physical address corresponding to the logical address 0xc0000003 is 0 × 3, and the physical address corresponding to 0xc0000004 is 0 × 4,... ... The relationship between the logical address and the physical address is
Physical address = logical address-0xC0000000: this is the address translation relationship of the kernel address space. Note that the virtual address of the kernel is at the "high end", but the physical memory address of the ta map is at the low end.
Logical address physical memory address 0xc00000000 × 00xc00000010 × 10xc00000020 × 20xc00000030 × 3... 0xe00000000 x 20000000. 0xffffffff0 x 40000000?
Assuming that according to the above simple address mapping relationship, then the kernel logical address space access is 0xc0000000 ~ 0xffffffff, then the corresponding physical memory range is 0 × 0 ~ 0 × 40000000, that is, only 1 GB of physical memory can be accessed. If 8 GB of physical memory is installed in the machine, the kernel can only access the first 1 G of physical memory, and the latter 7 GB of physical memory will not be accessible, because the address space of the kernel has been fully mapped to the physical memory address range of 0 × 0 ~ 0 × 40000000. Even if 8 gigabytes of physical memory is installed, how can the kernel access the memory with a physical address of 0 × 40000001? There must be a memory logical address in the code. The address space of 0xc0000000 ~ 0xffffffff has been used up, so the memory after the physical address 0 × 40000000 cannot be accessed.
Obviously, the kernel address space 0xc0000000 ~ 0xfffffff cannot be used entirely for simple address mapping. Therefore, the x86 architecture divides the kernel address space into three parts: ZONE_DMA, ZONE_NORMAL, and ZONE_HIGHMEM. ZONE_HIGHMEM is high-end memory, which is the origin of the concept of high-end memory.
In the x86 structure, the three types of areas (starting with 3G) are as follows:
16MB starting from ZONE_DMA memory
ZONE_NORMAL 16MB~896MB
ZONE_HIGHMEM 896MB ~ end (1G)
two。 Understand
Previously 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 access all physical memory with the help of 128MB high-end memory address space?
When the kernel wants to access memory higher than the 896MB physical address, find a logical address space of the corresponding size from the 0xF8000000-0xFFFFFFFF address space and borrow it for a while. Borrow this logical address space, create a map to the physical memory you want to access (that is, fill the kernel PTE page table), temporarily use it for a while, and return it after use. In this way, others can also use this address space to access other physical memory, using a limited address space to access all physical memory. As shown in the following picture.
For example, if the kernel wants to access a piece of physical memory with the size of 1MB starting from 2G, that is, the physical address range is 0 × 80000000 ~ 0x800FFFFF. Before access, find a section of free address space of 1MB size. Assume that the free address space found is 0xF8700000 ~ 0xF87FFFFF, and use the logical address space of this 1MB to map to the memory of physical address space 0 × 80000000 ~ 0x800FFFFF. The mapping relationship is as follows:
Logical address physical memory address 0xF87000000 × 800000000xF87000010 × 800000010xF87000020 × 80000002... 0xF87FFFFF0x800FFFFF
When the kernel accesses 0 × 80000000 ~ 0x800FFFFF physical memory, the 0xF8700000 ~ 0xF87FFFFF kernel linear space is freed. In this way, other processes or code can also use the address 0xF8700000 ~ 0xF87FFFFF to access other physical memory.
From the above description, we can know the most basic idea of high-end memory: borrow a section of address space, establish a temporary address mapping, and release it after use, so that this address space can be recycled to access all physical memory.
Seeing this, some people can't help but ask: what if a kernel process or module keeps occupying a certain logical address space and doesn't release it? If this happens, the high-end memory address space of the kernel will become more and more tight, and if it is occupied and not released, it will be inaccessible without a mapping to physical memory.
3. Division
The kernel divides high-end memory into three parts: VMALLOC_START~VMALLOC_END, KMAP_BASE~FIXADDR_START, and FIXADDR_START~4G.
For high-end memory, you can get the corresponding page through alloc_page () or other functions, but to access the actual physical memory, you have to convert the page to a linear address (why? Think about how MMU accesses physical memory, that is, we need to find a linear space for the page corresponding to high-end memory, a process called high-end memory mapping.
Corresponding to the three parts of high-end memory, there are three ways to map high-end memory:
Map to the Kernel dynamic Mapping Space (noncontiguous memory allocation)
This approach is simple, because with vmalloc (), when requesting memory in the "kernel dynamic mapping space", it is possible to get pages from the high-end memory (see the implementation of vmalloc), so it is possible that the high-end memory is mapped to the "kernel dynamic mapping space".
Persistent kernel mapping (permanent kernel mapping)
If you get the page corresponding to the high-end memory through alloc_page (), how do you find a linear space for it?
The kernel sets aside a linear space for this purpose, from PKMAP_BASE to FIXADDR_START, to map high-end memory. On the 2. 6 kernel, this address range is from 4G-8M to 4G-4M. This space is called "kernel permanent mapping space" or "permanent kernel mapping space". This space uses the same page catalog table as other spaces, which for the kernel is swapper_pg_dir, and for ordinary processes, it is pointed to through the CR3 register. Typically, this space is 4m in size, so you only need a page table, which the kernel looks for by pkmap_page_table. With kmap (), you can map a page to this space. Because this space is 4m, it can map up to 1024 page at the same time. Therefore, for unused page, and when released from this space (that is, de-mapping), a linear address corresponding to page can be released from this space through kunmap ().
Temporary mapping (temporary kernel mapping)
The kernel reserves some linear space between FIXADDR_START and FIXADDR_TOP for special needs. This space is called "fixed mapping space". In this space, part of it is used for temporary mapping of high-end memory.
This space has the following characteristics:
(1) each CPU takes up a piece of space
(2) in the space occupied by each CPU, it is divided into several small spaces, each small space size is 1 page, and each small space is used for a purpose, which is defined in the km_type in kmap_types.h.
When you want to make a temporary mapping, you need to specify the purpose of the mapping. according to the purpose of the mapping, you can find the corresponding small space, and then use the address of this space as the mapping address. This means that a temporary mapping causes the previous mapping to be overwritten. Temporary mapping can be achieved through kmap_atomic ().
three。 Other
1. Is there a high-end memory concept in user space (process)?
User processes do not have the concept of high-end memory. High-end memory exists only in kernel space. User processes can only access 3G physical memory at most, while kernel processes can access all physical memory.
2. Is there high-end memory in the 64-bit kernel?
In reality, there is no high-end memory in the 64-bit Linux kernel because the 64-bit kernel can support more than 512GB memory. If the physical memory installed on the machine exceeds the range of the kernel address space, there will be high-end memory.
3. How much physical memory can the user process access? How much physical memory can kernel code access?
32-bit system user processes can access up to 3GB, and kernel code can access all physical memory.
64-bit system user processes can access up to more than 512GB, and kernel code can access all physical memory.
4. what is the relationship between high-end memory and physical address, logical address and linear address?
High-end memory is only related to logical addresses, not directly related to logical addresses and physical addresses.
The above is all the content of the article "what is the user space and kernel space of Linux?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.