Linux Kernel development and Linux kernel development
1.1 virtual memory
Linux is a multi-task system. If each task occupies memory independently, the actual physical memory will soon be exhausted. In fact, for a task running on the foreground, there is not much memory required, and many tasks do not need to be run, so there is no need to keep occupying the memory. The virtual memory technology uses hard disks to act as part of the memory, when the memory is insufficient, the data that does not need to be used in the memory is moved to the hard disk. When the task needs to run, the data in the hard disk is moved back to the physical memory.
The virtual memory technology not only protects the operating system, but also enables the user program to use a larger address space than the actual physical memory, shielding the impact of the actual physical memory on the user address space.
1.2 division of address space
For a process, the Linux system divides the virtual address space into user space and kernel space, and the user space occupies 3 GB (0x0 ~ 0 xbfffff), while the kernel space occupies 1 GB (0xC0000000 ~ 0 xFFFFFFFF ).
Linux is a multi-user system. Each user is a process and has an independent address space, but they share the kernel space. When switching between different processes, the kernel space remains unchanged.
1.3 user space
Linux applications run in user space. The process switches from user State to kernel state only when an interruption occurs or when a system call occurs. After the interrupted operation or system call is completed, return to the user State.
1.4 kernel space
Linux's core program runs in the kernel space. This is to protect the core program from crashing due to poor applications and to protect data. Because the application does not have the permission to directly control the hardware, only the kernel program can have the highest permission of the system.
1.5 kernel memory allocation and release
When the fork () and dynamic non-configuration memory malloc () processes are created, the allocated memory is only virtual memory, not physical memory. Then, when you actually access the allocated virtual address, the "please page mechanism" will generate a "page missing" exception and enter the program in the actual allocation page box.
The "page missing" exception is the basis for the survival of the virtual memory. It will tell the kernel to allocate physical pages to the process and create a page table. Then, the virtual address is mapped to the actual physical address.
In applications, malloc is used to dynamically allocate memory, while kmalloc is used in the kernel to allocate memory. The prototype of kmalloc is
# Include <linux/slab. h>
# Include <linux/gfp. h>
Void * kmalloc (size_t size, int flags );
Parameters:
Size: The size of memory to be allocated.
Flags: The allocation flag that controls the behavior of kmalloc.
The following are possible flages signs.
The most common GFP_KERNEL, which indicates memory allocation (get_free_pages is always called to realize actual allocation, which is the origin of the green code prefix) is the process that runs in the kernel space. GFP_KERNEL allows kmalloc to allow the current process to sleep and wait when idle memory is allocated. Therefore, the assignment function must be reentrant. If the current process should not sleep outside the process context, such as interrupt handler, tasklet, and kernel timer, the driver should use GFP_ATOMIC.
Used to allocate memory from code other than interrupt processing and process context. never sleep.
Normal kernel memory allocation. May be sleep.
Used to allocate memory for user space pages; it may sleep.
Like GFP_USER, but allocated from high-end memory, if there is. There is a subsection description in the high-end.
Similar to GFP_KERNEL, but disable any I/O Initialization
Similar to GFP_KERNEL, but cannot execute any file system call
This flag requires that it be allocated in the memory zone that can be DMA. The exact meaning is platform-dependent and will be explained in the following sections.
This indicator indicates that the allocated memory can be located in the high-end memory.
This flag identifies a high-priority request that is allowed to consume or even be retained by the kernel to the final memory page of the emergency.
When it has difficulty meeting an allocation. _ GFP_REPEAT, it means "try more" by repeating the attempt-but the allocation may still fail.
Tell the distributor not to fail; it tries its best to meet the requirements. It is strongly not recommended to use _ GFP_NOFAIL.
Inform the allocator to immediately discard the memory that cannot be requested.
1.6 distribute and release by PAGE
If the kernel module needs to allocate a large block of memory, it will be better to use page-oriented allocation technology.
# Include <linux/gfp. h>
- Get_zeroed_page (unsigned int flags)
Return the pointer to the new page and clear the page.
- _ Get_free_page (unsigned int flags );
Apply for a page and return the pointer to the new page, but the page is not cleared.
- _ Get_free_pages (unsigned int flags, unsigned int order)
Allocate and return the pointer of the first byte in the memory area of a carton. The memory area may be one or more pages long, but it is not cleared (physically consecutive ).
Order is the base-2 logarithm of the number of pages you requested or released (that is, log2N ). for example, if you want a page order to 0, If you request 8 pages, it is 3. if order is too large (no contiguous area of that size is available), page allocation fails. the get_order function uses an integer parameter to extract an order (which must be a power of 2) from a size value to the host platform. the maximum value allowed by order is 10 or 11 (corresponding to page 1024 or page 2048), depending on the system. however, an order-10 allocation has a low chance of success in addition to a system with a lot of memory just started.
- Void free_pages (unsigned long addr, unsigned long order );
Release the released page memory.
- Void free_page (unsigned long addr );
Release the released page memory. Note that system errors may occur if the released system memory is inconsistent with the allocated memory.
1.7 kernel space memory distribution
The kernel space is mapped by the kernel, which does not change with the process and is fixed.
What is high-end memory:
In the x86 structure, the kernel is divided into three blocks, with the following regional distribution (similar to Linux and x86 ):
16 MB of ZONE_DMA memory
ZONE_NORMAL 16 MB ~ 896 MB
ZONE_HIGHMEM 896 MB ~ End
More than 896M is called high-end memory, and less than 896M is called low-end memory.
A maximum of 896 of memory areas starting from 0xC0000000 (3G) are called direct ing areas because of the direct linear Conversion Relationship between the linear addresses of these areas and physical systems:
Linear address = 0xc0000000 + physical address
For example, the physical address is 0x100000 ~ 0x200000 linear address is 0xc0100000 ~ 0xc0200000
Memory in the direct ing area can be directly allocated through kmalloc.
The addresses in this region are allocated through vmalloc. Note that the linear addresses in the memory area allocated by vmalloc are continuous, but the physical memory area is not necessarily continuous. It connects idle pages by means of page tables, so the efficiency is much lower than kmalloc. However, the address allocated by vmalloc may be out of high-end memory or low-end memory.
- Permanent memory ing zone (PKMap Region)
High-end memory is accessible in this region. You can use alloc_page (_ GFP_HIGHMEM) to allocate a high-end memory page or use the kmap function to map the allocated high-end memory to this area.
Common global variables in the permanent ing area:
PKMAP_BASE: Start address of the permanent ing space. The permanent ing space is 4 MB. Therefore, it can map up to 4 M/4 K = 1024 pages.
Pkmap_page_table: Page Directory of the permanent ing space. Let's take a look at its initialization:
Pkmap_page_table= Pte_offset_kernel (pmd_offset (pgd_offset_k
(PKMAP_BASE), PKMAP_BASE), PKMAP_BASE );
In fact, it is the PTE of PKMAP_BASE.
LAST_PKMAP: Number of pages that can be mapped to a permanent ing space. If PAE is not enabled, it is defined as 1024.
Highmem_start_page: Starting page of high-end memory
Pkmap_count [PKMAP]: Reference count of each item for the corresponding ing area
There is only a 4 K isolation band at the top of the region and 4G, and each of its addresses serves a specific purpose, such as ACPI_BASE.
Linux Kernel development books
The three most classic types:
Design and Implementation of Linux kernel version 2 by Robert Love
Deep understanding of Linux kernel
Scenario Analysis
Let's take a look at shen, which focuses on principles, and Linux kernel design and implementation. You 'd better read the operating system principles book first. After reading it several times, you can view the situation analysis. It is best to view it in "deep. The two documents show that "deep" is the outline and "Emotion" is the object. Finally, go deep into the code.
We are paying less attention to the Chinese New Year ~
Linux Kernel Development
I saw a book 21 days to learn linux programming in Xinhua bookstore a few days ago. Although it is a bit exaggerated, it only takes 210 days to multiply the number by 10. I believe you can do it if you work hard enough!