Linux Kernel Startup Process

Source: Internet
Author: User

Linux Kernel Startup Process
Linux Kernel Startup Process

Arch/arm/kernel/head-armv.S

This file is the first file executed by the kernel, including the initialization code between the kernel ENTRY (stext) and start_kernel,

The main function is to check the cpu id, Architecture Type, BSS initialization, and other operations, and jump to the start_kernel function. Before execution, the processor must meet the following conditions:

r0 - should be 0 
r1 - unique architecture number 
MMU - off 
I-cache - on or off 
D-cache – off 

 
1/* partial source code analysis */2/* kernel ENTRY point */3 ENTRY (stext) 4/* program status, disable FIQ, IRQ, set SVC mode */5 mov r0, # F_BIT | I _BIT | MODE_SVC @ make sure svc mode 6/* set the current program status register */7 msr cpsr_c, r0 @ and all irqs disabled 8/* determine the CPU type, check whether the running cpu id and Linux compilation supported ID values */9 bl _ lookup_processor_type 10/* jump to _ error */11 teq r10, #0 @ invalid processor? 12 moveq r0, # 'P' @ yes, error 'P' 13 beq _ error 14/* determine the system type, check whether the Architecture Type value of the R1 register supports */15 bl _ lookup_ubunture_type 16/* Not, skip to Error */17 teq r7, #0 @ invalid architecture? 18 moveq r0, # 'A' @ yes, error 'A' 19 beq _ error 20/* create core page table */21 bl _ create_page_tables 22 adr lr, _ ret @ return address 23 add pc, r10, #12 @ initialise processor 24/* jump to the start_kernel function */25 B start_kernel
1. start_kernel () function analysis

The following section analyzes the start_kernel () function and its related functions.

1.1 lock_kernel ()
 1 /* Getting the big kernel lock.  2 * This cannot happen asynchronously,  3 * so we only need to worry about other  4 * CPU's.  5 */  6 extern __inline__ void lock_kernel(void)  7 {  8     if (!++current->lock_depth)  9     spin_lock(&kernel_flag); 10 } 
 

Kernel_flag is a large kernel spin lock, which is used by all processes to migrate data to the kernel state.

Only the processor that obtains this large spin lock can enter the kernel, such as interrupt processing programs. In any pair of lock_kernel/unlock_kernel functions, at most one program can occupy the CPU.

The lock_depth Member of the process is initialized to-1 and set in the kerenl/fork. c file. When it is smaller than 0 (always-1), the process does not have the kernel lock; when it is greater than or equal to 0, the process gets the kernel lock.

1.2 setup_arch ()

The setup_arch () function initializes the system. The function is defined in the arch/arm/kernel/setup. c file and mainly involves the following main functions and codes.

Setup_processor ()
This function mainly uses

for (list = &__proc_info_begin; list < &__proc_info_end ; list++) if ((processor_id & list->cpu_mask) == list->cpu_val) break; 

In this loop, find the matched processor_id in the .proc.info segment, and set processor_id in the head_armv.S file.

1.2.2 setup_architecture (machine_arch_type)

This function obtains the system structure information and returns the pointer to the machine struct defined in the mach-xxx/arch. c file, which contains the following content:

 
MACHINE_START (xxx, “xxx”) MAINTAINER ("xxx" BOOT_MEM (xxx, xxx, xxx) FIXUP (xxx) MAPIO (xxx) INITIRQ (xxx) MACHINE_END 
1.2.3 memory setting code
if (meminfo.nr_banks == 0) {     meminfo.nr_banks = 1;     meminfo.bank[0].start = PHYS_OFFSET;     meminfo.bank[0].size = MEM_SIZE; } 
The meminfo structure indicates the memory condition, which is the default initialization of the physical memory structure meminfo.

Nr_banks specifies the number of memory blocks, bank specifies the range of memory blocks, PHYS _ OFFSET specifies the starting address of a memory block, and MEM_SIZE specifies the length of a memory block. Both PHYS _ OFFSET and MEM_SIZE are defined in include/asm-armnommu/arch-XXX/memory. in the H file, PHYS _ OFFSET indicates the start address of the memory, and MEM_SIZE indicates the end address of the memory.

This structure plays an important role in the subsequent memory initialization code.

1.2.4 kernel memory space management
Init_mm.start_code = (unsigned long) & _ text; kernel code segment starts init_mm.end_code = (unsigned long) & _ etext; kernel code segment ends init_mm.end_data = (unsigned long) & _ edata; kernel data segment start init_mm.brk = (unsigned long) & _ end; kernel data segment end

 

Each task has a mm_struct structure to manage its memory space. init_mm is the kernel's mm_struct.

Set the member variable * mmap to point to itself, which means that the kernel has only one memory management structure. Set pgd = swapper_pg_dir, and swapper_pg_dir is the page Directory of the kernel, the directory size on the kernel page of the ARM architecture is defined as 16 kb. Init_mm defines the memory space of the entire kernel. The kernel thread belongs to the kernel code and uses the kernel space. It has the same permission to access the memory space as the kernel.

1.2.5 memory structure Initialization

The bootmem_init (& meminfo) function initializes the memory structure based on meminfo.

Call the reserve_node_zero (bootmap_pfn, bootmap_pages) function in the bootmem_init (& meminfo) function. This function is used to reserve some memory so that it cannot be dynamically allocated.

These memory blocks include:

Reserve_bootmem_node (pgdat, _ pa (& _ stext), & _ end-& _ stext);/* address space occupied by the kernel */reserve_bootmem_node (pgdat, bootmap_pfn <PAGE_SHIFT, bootmap_pages <PAGE_SHIFT)/* address space occupied by the bootmem structure */
1.2.6 paging_init (& meminfo, mdesc)

Create a kernel page table to map all physical memory and I/O space. For different processors, this function varies greatly.

The following describes the storage system of the ARM architecture and the concepts related to MMU.
In the ARM storage system, the memory management unit (MMU) is used to map virtual addresses to actual physical addresses.

MMU can be used to fully map the addresses of SDRAM to a contiguous address space starting with 0x0, and map the FLASH or ROM occupied by this space to other non-conflicting storage space locations.

For example, the FLASH address ranges from 0x0000 to 0000 ~ 0x00FFFFFF, while the address range of SDRAM is 0x3000 0000 ~ 0x3lFFFFFF, You can map the SDRAM address to 0x0000 0000 ~ 0 xlFFFFFF, and the FLASH address can be mapped to 0x9000 0000 ~ 0x90FFFFFF (the address space is idle and is not occupied ). After the ing is completed, if the processor encounters an exception, assume that it is still IRQ interrupted, and the PC Pointer Points to the address at 0xl8, in this case, the PC actually reads commands from 0x3000 of the physical address.

Through MMU ing, the program can be fully run in SDRAM. In actual application, two discontinuous physical address spaces may be allocated to SDRAM. In the operating system, we are used to contiguous the space of SDRAM to facilitate memory management. When applications apply for large volumes of memory, the operating system kernel can also be conveniently allocated. Through MMU, discontinuous physical address spaces can be mapped to consecutive virtual address spaces. The operating system kernel or some key code is generally not intended to be accessed by user applications. MMU can control the access permissions of the address space to prevent these codes from being damaged.

The implementation process of MMU is actually a process of table ing. Creating a page table is an indispensable step to implement the MMU function. The page table is located in the system memory. Each item in the page table corresponds to a ing from a virtual address to a physical address. The length of each item is the length of a word (in ARM, the length of a word is defined as 4 Bytes ). In addition to ing virtual addresses to physical addresses, page table items also define access permissions and buffering features.
MMU ing is divided into two types: first-level page table transformation and second-level page table transformation. The difference between the two is that the size of the transformed address space is different.
The first-level page table transformation supports 1 MB Storage space ing, while the second-level table conversion supports ing between 64 kB, 4 kB, and 1 kB address space.

The size of a dynamic table (page table) = the number of table items * The number of digits required for each table item. That is, when an index table is created in the entire memory space, there is much space to store the index table itself.
Table items = Virtual Address Space/size per page
Number of digits required for each table item = Log (actual number of page tables) + appropriate number of digits
Actual number of page tables = physical address space/size of each page

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • 3
  • 4
  • Next Page

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.