Linux Kernel (4)

Source: Internet
Author: User

Embedded Linux Startup Process:

Bootloader: After the hardware is powered on, it jumps to a fixed position to execute the corresponding code, initializes the corresponding device, loads the kernel code to the memory, and jumps to the starting position of the kernel code for execution;

Kernel: Self-extracting kernel, initializing the driver module for static compiling into the kernel, attaching the root file system, and directly executing the first user space program;

The first user space program: configure the user environment and execute the service process.

 

1. system startup and initialization

On intel-based systems, when loadlin.exe or lilo loads the kernel into the memory and passes control to the kernel, the kernel starts to start. ARCH/i386/kernel/head. s to set the specific structure, and then jump to the main () Routine of init/Main. C.

2. Memory Management

The memory management code is mainly in/mm, but the code of the specific structure is in arch/*/mm. The code for page disconnection processing is in mm/memory. C, while the code for memory ing and page cache is in mm/filemap. C. Buffer high-speed cache is implemented in mm/buffer. C, while switching high-speed cache is implemented in mm/swap_state.c and mm/swapfile. C.

3. Kernel

In the kernel, the code of the specific structure is in arch/*/kernel, and the scheduler is in kernel/sched. c. The fork code is in the kernel/fork. c. The task_struct data structure is in include/Linux/sched. h.

4. PCI

The PCI pseudo driver is in drivers/PCI. C, which is defined in include/Linux/PCI. h. Each structure has some specific pci bios code, Intel in arch/alpha/kernel/bios32.c.

5. inter-process communication

All System v ipc object permissions are included in the ipc_perm data structure, which can be found in include/Linux/IPC. h. The System V message is in IPC/MSG. implemented in C, shared with IPC/SHM. in C, semaphores are stored in IPC/SEM. in C, the pipeline is in IPC/pipe. c.

6. interrupt handling

The Interrupt Processing code of the kernel is unique to almost all microprocessors. The interrupt handling code is defined in arch/i386/kernel/IRQ. c In include/asm-i386/IRQ. h.

7. Device Drivers

All the source code of the Linux driver is stored in/driver. The following types can be further divided:

/Block

Block device drivers such as IDE (in IDE. C ). If you want to see how all devices that contain a file system are initialized, you should check drivers/block/genhd. in C, device_setup () and device_setup () not only initialize the hard disk, but also initialize the network when an NFS file system is installed on a network. Block devices include devices based on IDE and SCSI.

/Char

This is where the driver program of character devices (such as tty, serial port, and mouse) is located.

/CDROM

All the CDROM code in Linux is here. For example, you can find the soundblaster CDROM driver. Note that the driver of the ide cd is a ide-cd.c, placed in drivers/block, the driver of the scsi cd is SCSI. C, and placed in drivers/SCSI.

/PCI

This is the source code of the PCI pseudo driver. here we can see how the PCI subsystem is mapped and initialized.

/SCSI

We can find all the SCSI codes and all the device drivers of Linux-supported SCSI devices.

/Net

Here you can find the network device driver, such as the decchip 21040 PCI Ethernet driver in tulip. C.

/Sound

This is where all sound card drivers are located.

8. File System

The source code of the ext2 file system is all in the FS/ext2/directory, and its data structure is defined in include/Linux/ext2_fs.h, ext2_fs_ I .h and ext2_fs_sb.h. The data structure of the virtual file system is described in include/Linux/fs. h, and the code is in FS. Buffer cache and kernel update daemon are implemented in FS/buffer. C.

8. Network

Network code is stored in/net. Most of the include files are included in include/net, and the BSD set of port code is stored in net/socket. in C, IP 4th sets the port code in net/IPv4/af_inet.c. The common protocol supports Code (including sk_buff processing routines). Under Net/core, the TCP/IP network code is under net/IPv4, and the network device driver is under/Drivers/net.

9. Module

The Code part of the kernel module is in the kernel, and the part is in the module package. The former is all in the kernel/modules. c, and the data structure and kernel daemon kerneld information are in include/Linux/module. H and include/Linux/kerneld. h. If you want to see the structure of the elf target file, it is located in include/Linux/elf. h.

 

 

When the computer is started, it is powered on first, then hardware detection and boot the initialization program of the operating system, and then the initialization program of the operating system is responsible for reading the system kernel and creating the operating environment of the production system. this process is relatively complex and related to the CPU architecture. Here we will give a detailed description of this process through Linux and the i386 architecture.
I. Hardware Detection
When
After the machine is powered on, it first executes the code in BIOS (Basic Input and Output System), BIOS first executes the power-on self-check program (post), and the hardware is started when the self-check is passed. Post Program
Check the memory and other hardware devices to determine the existence of the hardware and perform operations correctly. BIOS is a program that is solidified in the chip. It generally takes several seconds to execute this process. After self-check is completed
The BIOS searches for a valid boot drive in the boot sequence set in the system Coms (Here we use the hard disk as an example), reads the System Boot Sector, and gives the system control to the boot program.
2. load and execute the Pilot Program
The system boot program loads the system kernel to the memory. The boot disk must contain boot records on the first logical track. The 512-byte sector is also called the Boot Sector. After the system completes power-on self-check,
The BIOS reads the boot sector from the boot disk into the memory. The Boot Record contains some Disk Physical characteristics parameters. After the Boot Sector is read into the memory, the BIOS can read the physical
Parameters. Once the Boot Record is loaded, the BIOS will hand over the execution control of the system and jump to the boot program.
. The Boot Record starts with an unconditional transfer instruction and will immediately jump to address 0x03e to execute the boot program, in the boot sector, this boot program will read several more complex programs from the disk and load the system kernel.
Linux
The Bootstrap program is generated by the assembly code file ARCH/i386/boot/bootsect. S. It calls the BIOS function
The setup. s file and kernel image under ARCH/i386/boot/are loaded into the memory. The CPU of the i386 architecture is divided into two protection modes: real mode and real mode.
640k memory. When the system loads the boot program, the CPU is in the real mode, and the current kernel image files generally exceed the limit of KB. Even if it is a compressed kernel image, this kernel
The image file is usually bzimage, which is usually used when compiling the kernel. Because bzimage exceeds the limit of KB
A
Bootsect_helper subroutine (defined in arch/i386/boot/setup. s). The Bootstrap program calls bootsect_helper cyclically.
Load the kernel image into memory. After the kernel is loaded, the system jumps to setup. start execution at the starting position of S, setup. S is still running in real mode. The main function is to set system parameters.
(Such as memory and disk), and prepares for entering the protection mode, and finally enters the protection mode and jumps to the header of the kernel image file to start kernel execution. For more information about Linux
Bootstrap program
Lilo, grub, Lilo, and grub can boot multiple systems. They are generally used if multiple systems are to be installed on the machine, and the boot program is also stored in the boot sector or in the primary boot.
Record (MBR), Lilo, and grub allow users to configure their own. They created a table on the position of the system kernel occupied disk data blocks during system installation. After the user chooses to start the Linux system, it also jumps to setup. s to run.
Iii. kernel Initialization
When setup. after S is executed, the CPU implements the protection mode and starts executing the kernel. If the kernel is compressed, first run the head under the arch/i386/boot/compressed directory. s. Create a stack, decompress the kernel image file, and transfer it to the head under ARCH/i386/kernel. s. If there is no compression, go directly to head. s under ARCH/i386/kernel to start execution. The arch/i386/kernel/head. s program initializes the Data Partition (BBS), interrupt description table (IDT), segment description table (gdt), page table, and register. Enter the start_kernel () module.
In this case, the system runs in kernel mode (Level 0) and is transferred to start_kernel () in init/Main. C (). Start_kernel () continues initialization in other aspects. It mainly initializes the core data structure of the system, including:
Setup_arch (): executes the settings related to the architecture.
Trap_init (): set various entry addresses.
Init_irq (): initializes the IRQ interrupt handling mechanism.
Sched_init (): Sets and starts the first process init_task ().
Softirq_init (): initializes the Soft Interrupt subsystem.
Lele_init (): initializes the console and display.
Init_modules (): Initialize kernel_module.
Fork_init (): defines the maximum number of processes in the system.
Finally, enter the rest_init () function and call kernel_thread () to create the init kernel thread for system configuration.
The first entry in the process description table occupied by the init kernel thread is used to create other processes that complete the system initialization.
The init kernel thread first needs to pin the kernel, and then calls do_basic_setup () to initialize the external device and load the driver. Master
The initialization tasks include:
PCI bus initialization.
Network initialization.
File system initialization.
Load the file system.
After do_basic_setup () is called, INIT () releases the memory occupied by the initialization function and enables/dev/console
The device redirects to the console and calls execve to execute the user State Program/sbin/init. Now, Linux kernel Initialization is complete.
The following work is done by the/sbin/INIT program in user mode. The INIT program reads the/etc/inittab file to determine its specific work. The most important items in inittab are:
ID: 5: initdefault determines the default execution level when the operating system is started (here we will talk about the system running level, which is different from the CPU level)
Si: sysinit:/etc/rc. d/rc. sysinit: run the/etc/rc. d/rc. sysinit script. RC. sysinit is mainly used to activate swap partitions, check disks, and load hardware modules.
1: 2345: respawn:/sbin/mingetty tty1 display logon 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.