Linux内核设计第三周
——构造一个简单的Linux系统
一、知识点总结
计算机三个法宝:
操作系统两把宝剑:
linux内核源代码分析
二、实验过程
图1 构建Menu系统的过程
其中rootfs.img 为根文件系统,目前只支持help、version、quit功能。
图2 使用gdb进行调试
关于-s和-S选项的说明:
-S 在启动前冻结cpu(使用 ’c’ 以开始执行)
-s 在端口1234上创建一个tcp连接
若不想使用1234端口,则可以使用-gdb tcp:xxxx来取代-s选项
图3 将断点设置到start_kernal
图4 将断点设置到rest_init
三、分析start_kernel函数的执行过程
在init目录下的main.c有函数 ,其中包含start_kernel函数
基本所有模块,都需要start_kernel来进行初始化。asmlinkage __visible void __init start_kernel(void)
其中,有init_ task,set_ task_ stack_ end_ magic(&init_task);这个是手工创建的PCB,0号进程,即最终的idle进程。trap_init();//中断向量初始化
mm_init();//内存管理模块初始化
sched_init();//调度模块初始化
console_init();//控制模块初始化
rest_init(); //其他模块初始化pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
//创建了线程
在rest_init中,各部分启动完毕后,
/* Call into cpu_idle with preempt disabled */cpu_startup_entry(CPUHP_ONLINE);
调用static void cpu_idle_loop(void);
里面有个while(1)
也就是在系统没有进程需要执行时就调度idle进程
总结下来:在start_ kernel启动后,rest_ init的中0号进程会一直存在。
如图所示,第500行代码有start_kernel函数
四、总结
阐明对“Linux系统启动过程”的理解。
①在start_ kernel启动后,rest_ init的中0号进程会一直存在。
②道生一(startkernel....cpuidle),一生二(kernel_init和kthreadd),二生三(即前面0、1和2三个进程),三生万物(1号进程是所有用户态进程的祖先,2号进程是所有内核线程的祖先)
五、实验中遇到的问题
1、在gdb调试的过程中,出现了如下图所示的问题,即输入“fil e linux-3.18.6/vmlinux”后,提示“没有那个文件或目录”。
解决:需要先进入到linux-3.18.6目录下,然后再进入gdb中。
六、附录:
start_kernel函数的源代码如下:
500asmlinkage __visible void __init start_kernel(void)
{
char *command_line;
char *after_dashes;
/*
506 * Need to run as early as possible, to initialize the
507 * lockdep hash:
508 */
lockdep_init();
set_task_stack_end_magic(&init_task);
smp_setup_processor_id();
debug_objects_early_init();
/*
515 * Set up the the initial canary ASAP:
516 */
boot_init_stack_canary();
cgroup_init_early();
local_irq_disable();
early_boot_irqs_disabled = true;
/*
525 * Interrupts are still disabled. Do necessary setups, then
526 * enable them
527 */
boot_cpu_init();
page_address_init();
pr_notice("%s", linux_banner);
setup_arch(&command_line);
mm_init_cpumask(&init_mm);
setup_command_line(command_line);
setup_nr_cpu_ids();
setup_per_cpu_areas();
smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
build_all_zonelists(NULL, NULL);
page_alloc_init();
pr_notice("Kernel command line: %s\n", boot_command_line);
parse_early_param();
after_dashes = parse_args("Booting kernel",
static_command_line, __start___param,
__stop___param - __start___param,
-, -, &unknown_bootoption);
if (!IS_ERR_OR_NULL(after_dashes))
parse_args("Setting init args", after_dashes, NULL, , -, -,
set_init_arg);
jump_label_init();
/*
554 * These use large bootmem allocations and must precede
555 * kmem_cache_init()
556 */
setup_log_buf();
pidhash_init();
vfs_caches_init_early();
sort_main_extable();
trap_init(); //中断向量初始化
mm_init(); //内存管理模块初始化
/*
565 * Set up the scheduler prior starting any interrupts (such as the
566 * timer interrupt). Full topology setup happens at smp_init()
567 * time - but meanwhile we still have a functioning scheduler.
568 */
sched_init(); //调度模块初始化
/*
571 * Disable preemption - early bootup scheduling is extremely
572 * fragile until we cpu_idle() for the first time.
573 */
preempt_disable();
if (WARN(!irqs_disabled(),
"Interrupts were enabled *very* early, fixing it\n"))
local_irq_disable();
idr_init_cache();
rcu_init();
context_tracking_init();
radix_tree_init();
/* init some links before init_ISA_irqs() */
early_irq_init();
init_IRQ();
tick_init();
rcu_init_nohz();
init_timers();
hrtimers_init();
softirq_init();
timekeeping_init();
time_init();
sched_clock_postinit();
perf_event_init();
profile_init();
call_function_init();
WARN(!irqs_disabled(), "Interrupts were enabled early\n");
early_boot_irqs_disabled = false;
local_irq_enable();
kmem_cache_init_late();
/*
603 * HACK ALERT! This is early. We're enabling the console before
604 * we've done PCI setups etc, and console_init() must be aware of
605 * this. But we do want output early, in case something goes wrong.
606 */
console_init();
if (panic_later)
panic("Too many boot %s vars at `%s'", panic_later,
panic_param);
lockdep_info();
/*
615 * Need to run this when irqs are enabled, because it wants
616 * to self-test [hard/soft]-irqs on/off lock inversion bugs
617 * too:
618 */
locking_selftest();
#ifdef CONFIG_BLK_DEV_INITRD
if (initrd_start && !initrd_below_start_ok &&
page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
page_to_pfn(virt_to_page((void *)initrd_start)),
min_low_pfn);
initrd_start = ;
}
#endif
page_cgroup_init();
debug_objects_mem_init();
kmemleak_init();
setup_per_cpu_pageset();
numa_policy_init();
if (late_time_init)
late_time_init();
sched_clock_init();
calibrate_delay();
pidmap_init();
anon_vma_init();
acpi_early_init();
#ifdef CONFIG_X86
if (efi_enabled(EFI_RUNTIME_SERVICES))
efi_enter_virtual_mode();
#endif
#ifdef CONFIG_X86_ESPFIX64
/* Should be run before the first non-init thread is created */
init_espfix_bsp();
#endif
thread_info_cache_init();
cred_init();
fork_init(totalram_pages);
proc_caches_init();
buffer_init();
key_init();
security_init();
dbg_late_init();
vfs_caches_init(totalram_pages);
signals_init();
/* rootfs populating might need page-writeback */
page_writeback_init();
proc_root_init();
cgroup_init();
cpuset_init();
taskstats_init_early();
delayacct_init();
check_bugs();
sfi_init_late();
if (efi_enabled(EFI_RUNTIME_SERVICES)) {
efi_late_init();
efi_free_boot_services();
}
ftrace_init();
/* Do the rest non-__init'ed, we're now alive */
rest_init(); //其他模块初始化
}