2018-2019-1 20189205 《Linux内核原理与分析》 第四周作业

MenuOS的构造

Linux内核

本周学习了Linux内核的基本目录结构,通过qemu构建了简单的Linux内核,并利用gdb工具进行调试,了解了内核的启动过程。

  • Linux的目录结构
  • 关键的目录
    arch:与体系结构相关的子目录列表。
    block:存放Linux存储体系中关于块设备管理的代码。
    crypto:存放常见的加密算法的C语言代码。
    documentation:存放一些文档。
    drivers:驱动目录,里面分门别类地存放了Linux内核支持的所有硬件设备的驱动源代码。
    firmware:固件。
    fs:文件系统,里面列出了Linux支持的各种文件系统的实现。
    include:头文件目录,存放公共的头文件。
    init:init是初始化的意思,存放Linux内核启动时的初始化代码。
    ipc:存放Linux支持的IPC的代码实现。
    kernel:存放内核本身需要的一些核心代码文件。其中有很多关键代码,包括pid--进程号等。
    lib:公用的库文件,里面是一些公用的库函数。
    mm:存放LInux的内存管理代码。
    net:网络相关的代码,譬如TCP/IP协议栈等。

构建简单的Linux内核

按照书上说写,首先在网络上下载Linux的内核源代码并解压编译,而后再制作好根文件系统,即可在qemu中运行不带调试信息的Linux内核和MenuOS

跟踪调试Linux内核的启动过程

本过程在实验楼中完成:

通过qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img -S -s指令,我们打开含有调试功能的内核窗口,并在打开新的终端通过gdb调试内核。
在gdb中输入
file linux-3.18.6/vmlinux 打开vmlinux文件
target remote:1234 连接到内核的1234端口
这样就可以开始对内核进行调试

在gdb中,我们可以插入函数断点来查看start_kernel()等函数的上下文情况。

代码分析

start_kernel() 函数

asmlinkage __visible void __init start_kernel(void)
{
    char *command_line;
    char *after_dashes;

    /*
     * Need to run as early as possible, to initialize the
     * lockdep hash:
     */
    lockdep_init();
    set_task_stack_end_magic(&init_task);
    smp_setup_processor_id();
    debug_objects_early_init();

    /*
     * Set up the the initial canary ASAP:
     */
    boot_init_stack_canary();

    cgroup_init_early();

    local_irq_disable();
    early_boot_irqs_disabled = true;

/*
 * Interrupts are still disabled. Do necessary setups, then
 * enable them
 */
    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,
                  -1, -1, &unknown_bootoption);
    if (!IS_ERR_OR_NULL(after_dashes))
        parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
               set_init_arg);

    jump_label_init();

    /*
     * These use large bootmem allocations and must precede
     * kmem_cache_init()
     */
    setup_log_buf(0);
    pidhash_init();
    vfs_caches_init_early();
    sort_main_extable();
    trap_init();
    mm_init();

    /*
     * Set up the scheduler prior starting any interrupts (such as the
     * timer interrupt). Full topology setup happens at smp_init()
     * time - but meanwhile we still have a functioning scheduler.
     */
    sched_init();
    /*
     * Disable preemption - early bootup scheduling is extremely
     * fragile until we cpu_idle() for the first time.
     */
    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();

    /*
     * HACK ALERT! This is early. We're enabling the console before
     * we've done PCI setups etc, and console_init() must be aware of
     * this. But we do want output early, in case something goes wrong.
     */
    console_init();
    if (panic_later)
        panic("Too many boot %s vars at `%s'", panic_later,
              panic_param);

    lockdep_info();

    /*
     * Need to run this when irqs are enabled, because it wants
     * to self-test [hard/soft]-irqs on/off lock inversion bugs
     * too:
     */
    locking_selftest();
}

start_kernel() 函数在main.c中充当着C语言中main函数的作用,打开系统后,内核将会首先调用汇编语言编写的硬件系统的初始化工作程序,为C代码配置好运行环境,而后首先调用的就是start_kernel() 函数。start_kernel() 函数的运行过程中将调用各个内核模块的初始化函数,其中包括但不限于trap_init()中断向量初始化,mm_init()内存管理初始化,sched_init()调度模块初始化等。在初始化的过程中,函数将会首先生成一个init_task进程(进程0),由0进程负责内核模块的初始化;而后在初始化的过程中,0进程将会生成kernel_init线程(内核线程1)和kthreadd线程(内核线程2),这两个线程将分别衍生用户进程以及其他内核进程,在初始化完成后,0进程将转化为idle空进程。

问题与解决

  • 在主机中配置调试信息时,在make的过程中gcc报错。

    错误原因是内存不足,以至于调试信息未写入vmlinux文件,导致gdb无法对Linux内核启动程序进行调试。

    在网上查阅后说可以通过扩大stack size解决,但是调整后问题任无法解决,因此调试部分在实验楼中完成。

猜你喜欢

转载自www.cnblogs.com/hzj20189205/p/9903632.html
今日推荐