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
...

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.