周子轩创作品转载请注明出处  《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

gdb跟踪start_kernel()函数

start_kernel()为系统内核的入口函数,该函数主要是对环境和组件等进行初始化操作。 
课程中通过运行跟踪MenuOS来了解从start_kernel()做了哪些事情,以下为我跟踪调试的截图。后面将分析函数的执行过程。 


分析start_kernel()函数

start_kernel(): /linux-3.18.6/init/main.c

  1. asmlinkage __visible void __init start_kernel(void)
  2. {
  3. // ...
  4. trap_init();
  5. mm_init();
  6. /*
  7. * Set up the scheduler prior starting any interrupts (such as the
  8. * timer interrupt). Full topology setup happens at smp_init()
  9. * time - but meanwhile we still have a functioning scheduler.
  10. */
  11. sched_init();
  12. // ...
  13. /* Do the rest non-__init'ed, we're now alive */
  14. rest_init();
  15. }

这个函数的大部分代码被省略,留下关注的4个初始化函数,其实是3个,mm_init()是内存管理初始化,暂时不分析。

trap_init()

中断向量表的初始化函数,设置了很多中断门(Interrupt Gate),其中设置了后面会关注到的system_call 
trap_init(): /linux-3.18.6/arch/x86/kernel/traps.c

  1. void __init trap_init(void)
  2. {
  3. // ...
  4. #ifdef CONFIG_X86_32
  5. set_system_trap_gate(SYSCALL_VECTOR, &system_call);
  6. set_bit(SYSCALL_VECTOR, used_vectors);
  7. #endif
  8. // ...
  9. }

sched_init()

进程调度初始化函数,函数内做了很关键的一步初始化——对0号进程,即idle进程进行初始化

rest_init()

其他初始化函数,函数内将创建1号进程,即init进程。下面主要来分析该函数。


分析rest_init()函数

rest_init(): /linux-3.18.6/init/main.c

  1. static noinline void __init_refok rest_init(void)
  2. {
  3. int pid;
  4. rcu_scheduler_starting();
  5. /*
  6. * We need to spawn init first so that it obtains pid 1, however
  7. * the init task will end up wanting to create kthreads, which, if
  8. * we schedule it before we create kthreadd, will OOPS.
  9. */
  10. kernel_thread(kernel_init, NULL, CLONE_FS);
  11. numa_default_policy();
  12. pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
  13. rcu_read_lock();
  14. kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
  15. rcu_read_unlock();
  16. complete(&kthreadd_done);
  17. /*
  18. * The boot idle thread must execute schedule()
  19. * at least once to get things moving:
  20. */
  21. init_idle_bootup_task(current);
  22. schedule_preempt_disabled();
  23. /* Call into cpu_idle with preempt disabled */
  24. cpu_startup_entry(CPUHP_ONLINE);
  25. }

其中,创建了一个线程kernel_init,不过内核中并没有线程的概念,这里创建的是一个任务(进程)。如果把系统当成是一个大进程,那么init进程就相当于大进程中的一个线程,因为内核中的进程调度会为每一个像init进程分配时间片来执行。

  1. kernel_thread(kernel_init, NULL, CLONE_FS);

kernel_init(): /linux-3.18.6/init/main.c

  1. static int __ref kernel_init(void *unused)
  2. {
  3. int ret;
  4. kernel_init_freeable();
  5. /* need to finish all async __init code before freeing the memory */
  6. async_synchronize_full();
  7. free_initmem();
  8. mark_rodata_ro();
  9. system_state = SYSTEM_RUNNING;
  10. numa_default_policy();
  11. flush_delayed_fput();
  12. if (ramdisk_execute_command) {
  13. ret = run_init_process(ramdisk_execute_command);
  14. if (!ret)
  15. return 0;
  16. pr_err("Failed to execute %s (error %d)\n",
  17. ramdisk_execute_command, ret);
  18. }
  19. /*
  20. * We try each of these until one succeeds.
  21. *
  22. * The Bourne shell can be used instead of init if we are
  23. * trying to recover a really broken machine.
  24. */
  25. if (execute_command) {
  26. ret = run_init_process(execute_command);
  27. if (!ret)
  28. return 0;
  29. pr_err("Failed to execute %s (error %d). Attempting defaults...\n",
  30. execute_command, ret);
  31. }
  32. if (!try_to_run_init_process("/sbin/init") ||
  33. !try_to_run_init_process("/etc/init") ||
  34. !try_to_run_init_process("/bin/init") ||
  35. !try_to_run_init_process("/bin/sh"))
  36. return 0;
  37. panic("No working init found. Try passing init= option to kernel. "
  38. "See Linux Documentation/init.txt for guidance.");
  39. }

代码中有3个if,这三个if分别以三种不同方式来启动init,但只会有1个init会被启动

  1. 如果ramdisk_execute_command被设置,执行-initrd指定的rootfs中的init
  2. 如果execute_command有值,执行命令行传入的init(猜测)
  3. 最后再在系统文件中的/sbin/init; /etc/init; /bin/init; /bin/sh查找文件是否存在,存在即作为1号进程启动

到此1号进程的启动分析完成 
下面分析0号idle进程从哪里启动

  1. static noinline void __init_refok rest_init(void)
  2. {
  3. // ...
  4. /* Call into cpu_idle with preempt disabled */
  5. cpu_startup_entry(CPUHP_ONLINE);
  6. }

rest_init()函数的末尾,0号进程idle就是在这里启动的。 
cpu_startup_entry(): /linux-3.18.6/kernel/sched/idle.c

  1. void cpu_startup_entry(enum cpuhp_state state)
  2. {
  3. /*
  4. * This #ifdef needs to die, but it's too late in the cycle to
  5. * make this generic (arm and sh have never invoked the canary
  6. * init for the non boot cpus!). Will be fixed in 3.11
  7. */
  8. #ifdef CONFIG_X86
  9. /*
  10. * If we're the non-boot CPU, nothing set the stack canary up
  11. * for us. The boot CPU already has it initialized but no harm
  12. * in doing it again. This is a good place for updating it, as
  13. * we wont ever return from this function (so the invalid
  14. * canaries already on the stack wont ever trigger).
  15. */
  16. boot_init_stack_canary();
  17. #endif
  18. arch_cpu_idle_prepare();
  19. cpu_idle_loop();
  20. }
  21. static void cpu_idle_loop(void)
  22. {
  23. while (1) {
  24. /*
  25. * If the arch has a polling bit, we maintain an invariant:
  26. *
  27. * Our polling bit is clear if we're not scheduled (i.e. if
  28. * rq->curr != rq->idle). This means that, if rq->idle has
  29. * the polling bit set, then setting need_resched is
  30. * guaranteed to cause the cpu to reschedule.
  31. */
  32. __current_set_polling();
  33. tick_nohz_idle_enter();
  34. while (!need_resched()) {
  35. check_pgt_cache();
  36. rmb();
  37. if (cpu_is_offline(smp_processor_id()))
  38. arch_cpu_idle_dead();
  39. local_irq_disable();
  40. arch_cpu_idle_enter();
  41. /*
  42. * In poll mode we reenable interrupts and spin.
  43. *
  44. * Also if we detected in the wakeup from idle
  45. * path that the tick broadcast device expired
  46. * for us, we don't want to go deep idle as we
  47. * know that the IPI is going to arrive right
  48. * away
  49. */
  50. if (cpu_idle_force_poll || tick_check_broadcast_expired())
  51. cpu_idle_poll();
  52. else
  53. cpuidle_idle_call();
  54. arch_cpu_idle_exit();
  55. }
  56. /*
  57. * Since we fell out of the loop above, we know
  58. * TIF_NEED_RESCHED must be set, propagate it into
  59. * PREEMPT_NEED_RESCHED.
  60. *
  61. * This is required because for polling idle loops we will
  62. * not have had an IPI to fold the state for us.
  63. */
  64. preempt_set_need_resched();
  65. tick_nohz_idle_exit();
  66. __current_clr_polling();
  67. /*
  68. * We promise to call sched_ttwu_pending and reschedule
  69. * if need_resched is set while polling is set. That
  70. * means that clearing polling needs to be visible
  71. * before doing these things.
  72. */
  73. smp_mb__after_atomic();
  74. sched_ttwu_pending();
  75. schedule_preempt_disabled();
  76. }
  77. }

Linux内核分析3的更多相关文章

  1. linux内核分析作业8:理解进程调度时机跟踪分析进程调度与进程切换的过程

    1. 实验目的 选择一个系统调用(13号系统调用time除外),系统调用列表,使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 分析汇编代码调用系统调用的工作过程,特别是参数的传递的方 ...

  2. Linux内核分析作业7:Linux内核如何装载和启动一个可执行程序

            1.可执行文件的格式 在 Linux 平台下主要有以下三种可执行文件格式: 1.a.out(assembler and link editor output 汇编器和链接编辑器的输出) ...

  3. linux内核分析作业6:分析Linux内核创建一个新进程的过程

    task_struct结构: struct task_struct {   volatile long state;进程状态  void *stack; 堆栈  pid_t pid; 进程标识符  u ...

  4. linux内核分析作业5:分析system_call中断处理过程

    1.增加 Menu 内核命令行 调试系统调用. 步骤:删除menu git clone        (tab) make rootfs 这就是我们将 fork 函数写入 Menu 系统内核后的效果, ...

  5. linux内核分析作业:以一简单C程序为例,分析汇编代码理解计算机如何工作

    一.实验 使用gcc –S –o main.s main.c -m32 命令编译成汇编代码,如下代码中的数字请自行修改以防与他人雷同 int g(int x) { return x + 3; } in ...

  6. linux内核分析作业:操作系统是如何工作的进行:完成一个简单的时间片轮转多道程序内核代码

    计算机如何工作 三个法宝:存储程序计算机.函数调用堆栈.中断机制. 堆栈 函数调用框架 传递参数 保存返回地址 提供局部变量空间 堆栈相关的寄存器 Esp 堆栈指针  (stack pointer) ...

  7. linux内核分析作业3:跟踪分析Linux内核的启动过程

    内核源码目录 1. arch:录下x86重点关注 2. init:目录下main.c中的start_kernel是启动内核的起点 3. ipc:进程间通信的目录 实验 使用实验楼的虚拟机打开shell ...

  8. linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

    系统调用:库函数封装了系统调用,通过库函数和系统调用打交道 用户态:低级别执行状态,代码的掌控范围会受到限制. 内核态:高执行级别,代码可移植性特权指令,访问任意物理地址 为什么划分级别:如果全部特权 ...

  9. 《Linux内核分析》期末总结

    Linux内核设计期中总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 前八周博客汇总及总结 Linux内核设计第一周——从汇编语言出发理解计算机工作原理 我们学习了汇编语言的基础知识,这一 ...

  10. 《Linux及安全》期中总结&《Linux内核分析》期终总结

    [5216 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK NINE ...

随机推荐

  1. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计

    Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...

  2. fail-fast 机制 思考

    HashMap的迭代器(Iterator)是fail-fast迭代器,而Hashtable的enumerator迭代器不是fail-fast的. Iterator支持fail-fast机制,而Enum ...

  3. HTML(1)简介

    "超"文本标记语言--HTML 文本,是指书面语言的表现形式. 百度百科 说白了,文本就是你能看得到的字,不论是纸上的还是屏幕上的,都是文本.文本就是用来记录信息一种形式. 那么, ...

  4. sklearn中的交叉验证(Cross-Validation)

    这个repo 用来记录一些python技巧.书籍.学习链接等,欢迎stargithub地址sklearn是利用python进行机器学习中一个非常全面和好用的第三方库,用过的都说好.今天主要记录一下sk ...

  5. java 乐观锁 vs 悲观锁

    在数据库的锁机制中介绍过,数据库管理系统(DBMS)中的并发控制的任务是确保在多个事务同时存取数据库中同一数据时不破坏事务的隔离性和统一性以及数据库的统一性. 悲观锁其实就是 完全同步 比如 sync ...

  6. Centos上搭建git服务

    1.安装Git $ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel $ yum ...

  7. 从一个app开始学iOS

    在大学上了4年学,老师一直给灌输的思想就是,从细微处着手最后看到整体.举个网站的例子.第一个学期老师安排一门课java语言,期末考试就是考试java语言的知识.第二学期java web,第一次课配置j ...

  8. Hash开散列 拉链法

    #include<iostream> #include<cstdio> using namespace std; const int maxn=1000007; struct ...

  9. struts2文件上传突破2M限制

    struts配置文件 <action name="upload" class="strutsFileUpload"> <result name ...

  10. 缓存-System.Web.Caching.Cache

    实现 Web 应用程序的缓存. 每个应用程序域创建一个此类的实例,只要应用程序域将保持活动状态,保持有效. 有关此类的实例的信息,请通过Cache的属性HttpContext对象或Cache属性的Pa ...