Linux kernel porting (vi)--c language start-up section analysis (i)

Source: Internet
Author: User

One: Source

Before parsing, kernel boot is from HESD. s, After creating the section page table,

ldrr13, __switch_data jump to __switch_data ,

__switch_data:.long__mmap_switched[email protected] r4[email protected] r5[email protected] r6[email protected] r7[  Email protected] r4[email protected] r5[email protected] r6[email protected] r7.longinit_thread_union + thread_start_sp @ Sp


This thing can actually be understood as a function pointer array, and do the following things

(1) analysis that the next step to execute the __mmap_switched function, in this function through

Bstart_kernel jumps to the C language run Phase.

(2) Copy the data segment, clear the BSS segment (to build the C language operating Environment)

(3) Save the first address of CPU ID number, Machine code, tag to pass the Parameter.


Second: Analysis of C language running stage

(1) function Name: Start_kernel (); This is similar to Uboot's function name Start_armboot starting the second stage, because Uboot itself is a copy of Kernel.

(2) smp_setup_processor_id ();

SMP (symmetric multi-processor). For a single-core soc, MPIDR = 0; So when there is only one cpu, this function does nothing, but if there is more than one cpu, it returns the ID number of the CPU when it is Started.

(3) Lockdep_init ();

Initialize LOCKDEP hash table

Lockdep: Deadlock Detection Module

A deadlock is a state in which multiple processes (threads) are stuck because they wait for a resource that has been occupied by another process for a long time. When the waiting resource is not released, the deadlock will Continue. Once a deadlock occurs, the program itself can not be solved, only rely on external forces to enable the program to resume operation, such as restart, door dog reset.

(4) Debug_objects_early_init (); Initializing debug Kernel correlation

(5) boot_init_stack_canary ();

the stack_canary is a stack with protection against stack Overflow attacks. See http://www.cloud-sec.org/CC_STACKPROTECTOR_patch_Analysis

(6) cgroup_init_early ();

Cgroup initialization, Cgroup is a modern Linux kernel. it provides a performance control mechanism for processes and their subsequent sub-processes, as described In: http://linux.chinaunix.net/techdoc/net/2008/12/23/1054425.shtml

(7) local_irq_disable ();

Turn off interrupts for the current CPU

(8) Early_boot_irqs_off ();

is also associated with CPU Interrupts.

(9) Early_init_irq_lock_class ();

Initialization of IRQ interrupts

(+) Lock_kernel ();

(one) Tick_init (); // Initialize the tick control function to register the clockevents framework .

(a) Boot_cpu_init ();

For a CPU core system, set the first CPU core as active CPU Core. For a single-cpu core system, set the CPU core as active CPU core

(+) Page_address_init ();

A non-empty function when a CONFIG_HIGHMEM macro is defined and no want_page_virtual macro is Defined. The other case is an empty Function. Note: ARM9 does not support high-end addresses (larger than 896M), and typically embedded products do not use high-end addresses, so in the arm architecture, this function is empty

(+) PRINTK (kern_notice "%s", linux_banner);

Print the contents of the Linux_banner to the log_buf buffer and wait until the serial port or other terminal is initialized and print to the terminal at once KERN_NOTICE This macro represents the level of printing, and only the print level of the console (which is equal to the possible line) can be printed at the end of the print

Linux_banner is the information of the Linux kernel, mainly the version, the time of compile generation, etc.

(setup_arch) (&command_line);

In fact, This function is used to determine the current kernel of our machines (arch, machine).

Our Linux kernel supports the operation of a CPU, and the cpu+ Development Board identifies a hardware platform

Then our currently configured kernel will run on this platform. The machine code mentioned earlier is a fixed code for this hardware platform to characterize this platform. The machine code supported by the current kernel and some definitions related to the hardware platform are handled in this Function.


The function analysis of the call inside this function

The Setup_processor function is used to find the CPU information, which can be analyzed with the information of serial port Printing.


The parameter of the Setup_machine function is the machine code number, and the Machine_arch_type symbol is defined in 32039-32050 lines of INCLUDE/GENERATED/MACH-TYPES.H. After analysis, it is determined that the parameter value is 2456. the function is to find the MACHINE_DESC descriptor corresponding to the machine code by passing in the machine code number, and return the pointer to this Descriptor. In fact, the function of real work is lookup_machine_type, find this function found in Head-common. In s, the function of real work is __lookup_machine_type. The __lookup_machine_type function works: when the kernel is established, it organizes the information of various CPU architectures into a single instance of the Machine_desc structure, Then both give a segment Attribute. arch.info.init, the link will ensure that these descriptors are concatenated together. __lookup_machine_type went to the descriptor where the descriptors were iterated through each descriptor, which was the same as looking at the machine Code.

The basic cmdline processing of the Setup_arch function

(1) The cmdline mentioned here refers to the command-line startup parameters that are passed when uboot to kernel, and is Uboot's bootargs. .

(2) There are several related variables to be aware of:

Default_command_line: look at the name is the default command-line arguments, is actually a global variable character array, this character array can be used to store things.

Config_cmdline: defined in the. config file (you can change the settings in Make menuconfig), which represents a default command-line parameter for the Kernel.

(3) the kernel to cmdline the idea is: The kernel itself maintains a default cmdline (that is, the one configured in. config), and uboot can also pass a tag to kernel and then a CMDLINE. If the Uboot CmdLine succeeds then the kernel will take precedence over the one passed by uboot, and the kernel will use its default cmdline if Uboot does not pass cmdline to the kernel or fails to pass the Argument. The above-mentioned approach is implemented in the Setup_arch function .


CmdLine determination of kernel by experimental verification

(1) to verify the Idea: first configure the kernel configuration of a basic cmdline, and then uboot boot the kernel when the Uboot set a bootargs, and then start the kernel to see the printed cmdline and Uboot when the Same.

(2) Remove the Bootargs in the uboot, and then start the kernel again to see if the printed cmdline is the same as the default CmdLine set in the Kernel.


Note: the cmdline of Uboot to the kernel is very important and will affect the operation of the kernel, so be cautious. Sometimes the kernel boot has a problem, you can analyze whether the Uboot Bootargs setting is Incorrect.

Summary: 2 things to do in Setup_arch: machine code architecture lookups and execution of schema-related hardware initialization, uboot to the Kernel's cmdline.


(+) Mm_init_owner (&init_mm, &init_task);

Memory-management-related initialization

(+) Setup_command_line (command_line);

The command-related function handles Commands-line-first functions, allocates memory to Saved_command_line and static_command_line, and copies Boot_command_line and command_line, These two things are in fact setup_machine in the Default_command_line content, or uboot the Ubootargs

(printk) (kern_notice "Kernel Command line:%s\n", boot_command_line);

Formally Print kernel command line information

(+) Parse_early_param and Parse_args

Parse CmdLine and other parameters, that is, the information and details of the CMDLINE. For example cmdline:console=ttysac2,115200 root=/dev/mmcblk0p2 rw INIT=/LINUXRC rootfstype=ext3, Then the parsed content is a string array, The array stores a set of project information in Sequence.

Console=ttysac2,115200 A

OOT=/DEV/MMCBLK0P2 RW One

INIT=/LINUXRC A

Rootfstype=ext3 A

That is, the cmdline is parsed into four strings, each of which corresponds to a set item in the future, and each setting has an effect on the operation of Kernel. This is just a parse, and it's not going to Work. This means that the long string is parsed into a short string, up to the kernel control of the corresponding function of the variable hook, but not to Execute. The code that executes is in the Code section of the respective module Initialization.


(trap_init) Set Exception vector table

(+) mm_init Memory Management module initialization

Sched_init Kernel Dispatch system initialization

(EARLY_IRQ_INIT&INIT_IRQ Interrupt Initialization)

(console_init) Console Initialization


Summary: the Start_kernel function calls a number of Xx_init functions, all of which are the initialization functions of the module that the kernel works. After these initializations, the kernel has a basic working condition.

If the kernel is likened to a complex machine, then the Start_kernel function is to assemble many parts of the machine together to form the machine, allowing him to have basic conditions for Work.

When these parts are initialized, they run to the last function of Start_kernel rest_init ()



Linux kernel porting (vi)--c language start-up section analysis (i)

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.