1. Problem Description
Through this week's study, we further study the directory structure linux kernel source code, based on linux kernel source code constructs a simple operating system MenuOS, while analyzing the startup process MenuOS using gdb trace debugging kernel from start_kernel to init process started, and analyze the code linux kernel boot of the function, further in-depth understanding of how linux kernel.
2. The resolution steps
2.1 linux kernel source description
Linux kernel source directory structure as shown in FIG.
Here important part directory
arch: arch is the architecture of the abbreviation. Kernel for each supported CPU architecture, in the directory has a corresponding subdirectory. Each CPU subdirectory, and further decomposed into boot, mm, kernel and other subdirectories, each comprising a boot control system, memory management, system calls and the like.
block: block device driver part.
crypto: encryption, compression, CRC checksum algorithm.
documentation: storing some documents kernel.
drivers: drive directory, which categorized the storage driver source code for all hardware devices supported by the Linux kernel.
fs: storing code that implements the various file systems. Each subdirectory corresponds to implement a file system.
include: kernel header files needed, storage of public (various CPU architectures common) headers.
init: kernel initialization code.
ipc: Inter-process communication implementation code.
kernel: linux most critical core functions are implemented in this directory.
lib: library code.
mm: mm files in the directory used to implement part of the memory management has nothing to do with architecture.
net: network protocol implementation code.
samples: some examples of kernel programming.
scripts: kernel configuration script.
2.2 construct a simple Linux kernel
Here the use of "experimental building" environment, linux kernel version is 3.18.6 laboratory building environment. Construction of a linux kernel using the following command.
cd LinuxKernel/
qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img
qemu simulation kernel; bzImage is vmLinux after gzip compressed file is compressed kernel image, "b" represents the "big" (bzImage for large kernel, zImage for small kernels). Root file system generally includes a memory root file system and disk file system. Here's root file system is relatively simple, just create a rootfs.img.
2.3 Debugging using gdb trace
Use gdb trace debug core, plus two parameters, one is -s (1234 port created on a gdb-server, can open another window, gdb with the kernel image in accordance with Table loaded in, and then connect gdb Server , set breakpoints, trace kernel.), and the other is -S (just freeze up before the initial CPU).
Effect as the kernel starts
to open a window, start GDB, the kernel is loaded, establishes the connection
using the following command
#加载符号表
file linux-3.18.6/vmlinux
#用1234这个端口进行连接
target remote:1234
Set a breakpoint at start_kernel at just a stop state, if it continues to perform according to "c", then the system started to perform, starting position start_kernel function to stop at a breakpoint
and then set a breakpoint rest_init, continue, stop off in at the point.
2.4 Analysis of the role of the kernel function start_kernel boot process
main.cc source file init directory is the starting point of the whole linux kernel boot, but its starting point is not the main function, but start_kernel function, start_kernel function in the Linux kernel initialization complete the whole system. The final step rest_init kernel initialization function init process is the ancestor started all this process.
2.4.1 start_kernel function
Start_kernel function is performed first, to complete a series of initialization module, clock, and other processes.
asmlinkage __visible void __init start_kernel(void)
{
…
set_task_stack_end_magic(&init_task);
printk(KERN_NOTICE"%s", linux_banner); /* 输出linux版本信息 */
setup_arch(&command_line); /* 设置与初始化硬件体系相关的环境并调用 */
sched_init() /* 初始化调度器,先于中断开始前 */
printk(boot_command_line); /* 提取分析核心启动参数过程(从bootloader中传递) */
parse_early_param();
parse_args
trap_init();
return
early_irq_init(); /* 中断初始化过程 */
init_IRQ();
init_timers(); /* 初始化定时器 */
timekeeping_init();
time_init(); /* 设置定时器及返回当前时间 */
console_init() /* 初步的初始化控制台 */
vmalloc_init();
vfs_caches_init_early();
mem_init(); /* 初始化内存并计算可用内存大小 */
kmem_cache_init(); /* 初始化SLAB缓存分配器 */
calibrate_delay(); /* 延迟校准 */
fork_init(num_physpages); /* 初始化max_threads,init_task参数为fork()提供参考 */
buffer_init(); /* 初始化块设备读写缓冲区 */
vfs_caches_init(num_physpages);
signals_init(); /* 初始化内核信号队列 */
rest_init(); /* 最后实际进入reset_init()函数,包括所有剩下的硬件驱动,线程初始化等过程,这也最终完成start_kernel的启动过程 */
}
2.4.2 rest_init function
New kernel_init and kthreadd kernel processes function by rest_init
kernel_thread(kernel_init, NULL, CLONE_FS); #创建一个内核线程,实际上是一个内核进程,其中pid=1。其中kernel_init只是一个函数
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
#在kthreadd函数中kthread_create_list全局链表中维护的内核线程。当调用kthread_create时,会创建一个kthread,并被添加到kthread_create_list链表中。当进程执行完毕后,就会被从链表中删除。
3. Summary
By learning this week, my initial understanding about the linux kernel boot process steps in the next study, more features will further learn linux system kernel.