Linux kernel analysis-initialization

Source: Internet
Author: User
Article title: Linux kernel analysis ---- initialization. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: e4gle
  
Refer to the three files I provided: bootsect.txt,head.txt,setup.txt
For x86 boot, follow these steps:
1. the cpu initializes itself and executes a command at a fixed position.
2. this command line is transferred to bios.
3. bios finds the boot device and obtains the mbr, which points to our lilo
4. bios load and give control to lilo
5. compress the kernel and decompress it.
To put it simply, the cpu becomes the boot program of the kernel boot program, West.
At this time, the kernel will jump to start_kernel, which is the key function of/init/main. c. Many main. c functions are defined for this function Service. here I will briefly introduce the initialization process of this function.
Initialize the kernel:
Start system initialization from the start_kernel function (/init/main. c:
Start with the function:
# Ifdef _ SMP __
Static int boot_cpu = 1;
/* "Current" has been set up, we need to load it now * // * defines dual-processor */
If (! Boot_cpu)
Initialize_secondary ();
Boot_cpu = 0;
# Endif
  
Define a dual-processor.
  
Printk (linux_banner);/* print linux banner */
Print the kernel title information.
  
Start initializing some of its components (including memory, hardware terminals, scheduling, etc.). I will analyze the functions one by one:
Setup_arch (& command_line, & memory_start, & memory_end);/* initialize memory */
Returns the kernel parameters and the available physical address range of the kernel.
The function prototype is as follows:
Setup_arch (char **, unsigned long *, unsigned long *);
Returned memory start address:
Memory_start = paging_init (memory_start, memory_end );
Let's take a look at the definition of paging_init, which is the initialization request page:
Paging_init (unsigned long start_mem, unsigned long end_mem) (this will be detailed in future memory management subsystem analysis)
{
Int I;
Struct memclust_struct * cluster;
Struct memdesc_struct * memdesc;
  
/* Initialize mem_map [] */
Start_mem = free_area_init (start_mem, end_mem);/* traverse the idle page for searching memory */
  
/* Find free clusters, update mem_map [] accordingly */
Memdesc = (struct memdesc_struct *)
(Hwrpb-> mddt_offset + (unsigned long) hwrpb );
Cluster = memdesc-> cluster;
For (I = memdesc-> numclusters; I> 0; I --, cluster ++ ){
Unsigned long pfn, nr;
  
/* Bit 0 is console/PALcode reserved. Bit 1 is
Non-volatile memory -- we might want to mark
This for later */
If (cluster-> usage & 3)
Continue;
Pfn = cluster-> start_pfn;
If (pfn> = MAP_NR (end_mem)/* if we overrode mem size */
Continue;
Nr = cluster-> numpages;
If (pfn + nr)> MAP_NR (end_mem)/* if override in cluster */
Nr = MAP_NR (end_mem)-pfn;
  
While (nr --)
Clear_bit (PG_reserved, & mem_map [pfn ++]. flags );
}
  
Memset (void *) ZERO_PAGE (0), 0, PAGE_SIZE );
  
Return start_mem;
}
  
Trap_init (); initialize hardware interruption
This function is defined in the/arch/i386/kernel/traps. c file.
  
Sched_init () initialization scheduling
The/kernel/sched. c file contains detailed scheduling algorithms (these will be detailed in the structure analysis of process management and scheduling in the future)
  
Parse_options (command_line) analyzes various options passed to the kernel (details will be given later)
  
Memory_start = lele_init (memory_start, memory_end) initialize the console
  
Memory_start = kmem_cache_init (memory_start, memory_end) initializes the kernel memory cache (similarly, this function will be introduced in future memory management analysis)
  
Sti (); hardware interruption
  
Kernel_thread (init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND );
Current-> need_resched = 1; the need_resched flag is added and schedule is called (details will be provided in the scheduling)
Cpu_idle (NULL) enters the idle loop to consume idle cpu time slices
  
The kernel initialization has been basically completed, and a small amount of responsibilities to be completed have been passed to init. the local work is just entering the idle loop to consume idle cpu time slices. Therefore, cpu_idle (NULL) is called here, and it never returns. Therefore, this function will be preemptible when it is working properly.
  
Parse_options function:
Static void _ init parse_options (char * line)/* parameters are collected in a long command line. The kernel is assigned a pointer to the header of the command line */
{
Char * next;
Char * quote;
Int args, envs;
  
If (! * Line)
Return;
Args = 0;
Envs = 1;/* TERM is set to 'Linux 'by default */
Next = line;
While (line = next )! = NULL ){
  
Quote = strchr (line ,'"');
Next = strchr (line ,'');
While (next! = NULL & quote! = NULL & quote <next ){
  
Next = strchr (quote + 1 ,'"');
If (next! = NULL ){
Quote = strchr (next + 1 ,'"');
Next = strchr (next + 1 ,'');
}
}
If (next! = NULL)
* Next ++ = 0;
/*
* Check for kernel options first ..
*/
If (! Strcmp (line, "ro ")){
Root_mountflags | = MS_RDONLY;
Continue;
}
If (! Strcmp (line, "rw ")){
Root_mountflags & = ~ MS_RDONLY;
Continue;
}
If (! Strcmp (line, "debug ")){
Lele_loglevel = 10;
Continue;
}
If (! Strcmp (line, "quiet ")){
Lele_loglevel = 4;
Continue;
}
If (! Strncmp (line, "init =", 5 )){
Line + = 5;
Execute_command = line;
Args = 0;
Continue;
}
If (checksetup (line ))
Continue;
  
If (strchr (line, '= ')){
If (envs> = MAX_INIT_ENVS)
Break;
Envp_init [++ envs] = line;
} Else {
If (args> = MAX_INIT_ARGS)
Break;
Argv_init [++ args] = line;
}
}
Argv_init [args + 1] = NULL;
Envp_init [envs + 1] = NULL;
}
  
//////////////////////////////////////// ////////////////////////////
// Setup.txt
// Copyright (C) 2001, Feiyun Wang
//////////////////////////////////////// ////////////////////////////
// Analysis on linux/arch/i386/boot/setup. S (for Linux 2.2.17)
//////////////////////////////////////// ////////////////////////////
//////////////////////////////////////// ////////////////////////////
// Export the margin tags for. text,. data and. bss
{
. Text
Begtext:
  
. Data
Begdata:
  
. Bss
Begbss:
}
  
//////////////////////////////////////// ////////////////////////////
. Text
Start ()
SYSSEG = 0x1000
SETUPSEG = 0x9020
Modelist = end of. text:
{
// If loaded by bootsect. S,
// You can assume CS = SETUPSEG (= 0x9020), otherwise...
Goto start_of_setup ();
  
/*
Http://lxr.linux.no/source/Documentation/i386/boot.txt
Offset/Size Proto Name Meaning
01F1/1 ALL setup_sects The size of the setup in sectors
01F2/2 ALL root_flags If set, the root is mounted readonly
01F4/2 ALL syssize do not use-for bootsect. S use only
01F6/2 ALL swap_dev do not use-obsolete
01F8/2 ALL ram_size do not use-for bootsect. S use only
01FA/2 ALL vid_mode Video mode control
01FC/2 ALL root_dev Default root device number
01FE/2 ALL boot_flag 0xAA55 magic number
0200/2 2.00 + jump Jump instruction
0202/4 2.00 + header Magic signature "HdrS"
0206/2 2.00 + version Boot protocol version supported
0208/4 2.00 + realmode_swtch Boot loader hook
020C/4 2.00 + start_sys_seg Points to kernel version string
0210/1 2.00 + type_of_loader Boot loader identifier
0211/1 2.00 + loadflags Boot protocol option flags
0212/2 2.00 + setup_move_size Move to high memory size (used with hooks)
0214/4 2.00 + code32_start Boot loader hook
0218/4 2.00 + ramdisk_ima

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.