Linux Kernel code learning notes (2.6.21.7 arm)-kernel startup function start_kernel

Source: Internet
Author: User

In factCodeI have been analyzing it for a long time. I have long wanted to write something for myself. From today on, I will continue my blog!

I have been dealing with Linux over the past few years, and I think I really fall in love with Linux. So write something about Linux!

Linux has many things. As far as the kernel is concerned, it is impossible for a single person to understand all the mechanisms and details. But fortunately, the source code can be obtained at any time. As long as you are familiar with and understand some basic features of the kernel, you can easily get started!

Next, I will write some of my learning experiences and share them with you!

First, the kernel code contains files with the names of both uppercase and lowercase. Therefore, after downloading the kernel source code package from the Internet, you cannot decompress the package directly in windows, otherwise, some files will be overwritten by names with duplicate cases! Primarily network-related. I don't want to know network-related content for the moment, so I will first use sourceinsight in Windows to view the code! If you want to see all the code, decompress the code in Linux!

In addition, I am not interested in the code of the X86 architecture for the moment. I should start with arm first. However, the relationship between different architectures is not very big. After understanding some mechanisms, most of them will be the same!

Start with the first function:

E: \ projects \ kernel \ linux-2.6.21.1.src \ init \ main. C (501/825)

Asmlinkage Void _ Init start_kernel ( Void );

This function is guided by the kernelProgramAfter the boot, the first function executed after being decompressed by the self-extracting program can be considered as the entry function of the entire kernel. All the code I analyzed will start from this function!

This function is relatively simple, that is, linear initialization of some basic kernel mechanisms, such as interrupt, memory management, process management, signal, file system, Ko, etc! Start an init thread and read the INIT program in the file system as the first process in the system!

 

In fact, the start_kernel function is 0, which is used as the existence of the process no. It is finally idling the CPU:

Code

/*
* The idle thread. We try to conserve power, while trying to keep
* Overall Latency Low. The architecture specific idle is passed
* A value to indicate the level of "Idleness" of the system.
  */
Void Cpu_idle ( Void )
{
Local_fiq_enable ();

/* endless idle loop with no priority at all */
while ( 1 ) {
void ( * idle) ( void ) = pm_idle;

# Ifdef config_hotplug_cpu
If(Cpu_is_offline (smp_processor_id ())){
Leds_event (led_idle_start );
Cpu_die ();
}
# Endif

If ( ! Idle)
Idle = Default_idle;
Leds_event (led_idle_start );
While ( ! Need_resched ())
Idle ();
Leds_event (led_idle_end );
Preempt_enable_no_resched ();
Schedule ();
Preempt_disable ();
}
}

 

Before it, the init thread started is to run the INIT program in the file system:

Code

/* This is a non _ init function. force it to be noinline otherwise gcc
* Makes it inline to Init () and it becomes part of init. Text Section
  */
Static   Int Noinline init_post ( Void )
{
Free_initmem ();
Unlock_kernel ();
Mark_rodata_ro ();
System_state = System_running;
Numa_default_policy ();

If (Sys_open (( Const   Char _ User * ) " /Dev/console " , O_rdwr, 0 ) <   0 )
Printk (kern_warning " Warning: Unable to open an initial console. \ n " );

( void ) sys_dup ( 0 );
( void ) sys_dup ( 0 );

If(Ramdisk_execute_command ){
Run_init_process (ramdisk_execute_command );
Printk (kern_warning"Failed to execute % s \ n",
Ramdisk_execute_command );
}

/*
* We try each of these until one succeeds.
*
* The Bourne shell can be used instead of init if we are
* Trying to recover a really broken machine.
*/
If (Execute_command ){ /* If a startup parameter is used as an execution command, execute */
Run_init_process (execute_command );
Printk (kern_warning " Failed to execute % S. Attempting "
" Defaults... \ n " , Execute_command );
} /* Query the INIT program in the file system in sequence. The run_init_process function cannot be returned. Otherwise, the startup fails */
Run_init_process ( " /Sbin/init " );
Run_init_process ( " /Etc/init " );
Run_init_process ( " /Bin/init " );
Run_init_process ( " /Bin/sh " );

Panic ("No init found. Try passing init = option to kernel.");
}

 

Because the kernel implements specific functions for CPU of many architectures, a function may be implemented in files with the same name in many directories. The specific architecture of a function to be queried. If there are no relevant functions in the corresponding architecture, you can use a common function or a function in a general architecture!

 

 

 

Related Article

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.