Linux内核分析3
周子轩创作品转载请注明出处 《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
asmlinkage __visible void __init start_kernel(void)
{
// ...
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();
// ...
/* Do the rest non-__init'ed, we're now alive */
rest_init();
}
这个函数的大部分代码被省略,留下关注的4个初始化函数,其实是3个,mm_init()是内存管理初始化,暂时不分析。
trap_init()
中断向量表的初始化函数,设置了很多中断门(Interrupt Gate),其中设置了后面会关注到的system_call
trap_init(): /linux-3.18.6/arch/x86/kernel/traps.c
void __init trap_init(void)
{
// ...
#ifdef CONFIG_X86_32
set_system_trap_gate(SYSCALL_VECTOR, &system_call);
set_bit(SYSCALL_VECTOR, used_vectors);
#endif
// ...
}
sched_init()
进程调度初始化函数,函数内做了很关键的一步初始化——对0号进程,即idle进程进行初始化
rest_init()
其他初始化函数,函数内将创建1号进程,即init进程。下面主要来分析该函数。
分析rest_init()函数
rest_init(): /linux-3.18.6/init/main.c
static noinline void __init_refok rest_init(void)
{
int pid;
rcu_scheduler_starting();
/*
* We need to spawn init first so that it obtains pid 1, however
* the init task will end up wanting to create kthreads, which, if
* we schedule it before we create kthreadd, will OOPS.
*/
kernel_thread(kernel_init, NULL, CLONE_FS);
numa_default_policy();
pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
rcu_read_lock();
kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
rcu_read_unlock();
complete(&kthreadd_done);
/*
* The boot idle thread must execute schedule()
* at least once to get things moving:
*/
init_idle_bootup_task(current);
schedule_preempt_disabled();
/* Call into cpu_idle with preempt disabled */
cpu_startup_entry(CPUHP_ONLINE);
}
其中,创建了一个线程kernel_init,不过内核中并没有线程的概念,这里创建的是一个任务(进程)。如果把系统当成是一个大进程,那么init进程就相当于大进程中的一个线程,因为内核中的进程调度会为每一个像init进程分配时间片来执行。
kernel_thread(kernel_init, NULL, CLONE_FS);
kernel_init(): /linux-3.18.6/init/main.c
static int __ref kernel_init(void *unused)
{
int ret;
kernel_init_freeable();
/* need to finish all async __init code before freeing the memory */
async_synchronize_full();
free_initmem();
mark_rodata_ro();
system_state = SYSTEM_RUNNING;
numa_default_policy();
flush_delayed_fput();
if (ramdisk_execute_command) {
ret = run_init_process(ramdisk_execute_command);
if (!ret)
return 0;
pr_err("Failed to execute %s (error %d)\n",
ramdisk_execute_command, ret);
}
/*
* We try each of these until one succeeds.
*
* The Bourne shell can be used instead of init if we are
* trying to recover a really broken machine.
*/
if (execute_command) {
ret = run_init_process(execute_command);
if (!ret)
return 0;
pr_err("Failed to execute %s (error %d). Attempting defaults...\n",
execute_command, ret);
}
if (!try_to_run_init_process("/sbin/init") ||
!try_to_run_init_process("/etc/init") ||
!try_to_run_init_process("/bin/init") ||
!try_to_run_init_process("/bin/sh"))
return 0;
panic("No working init found. Try passing init= option to kernel. "
"See Linux Documentation/init.txt for guidance.");
}
代码中有3个if,这三个if分别以三种不同方式来启动init,但只会有1个init会被启动
- 如果ramdisk_execute_command被设置,执行-initrd指定的rootfs中的init
- 如果execute_command有值,执行命令行传入的init(猜测)
- 最后再在系统文件中的/sbin/init; /etc/init; /bin/init; /bin/sh查找文件是否存在,存在即作为1号进程启动
到此1号进程的启动分析完成
下面分析0号idle进程从哪里启动
static noinline void __init_refok rest_init(void)
{
// ...
/* Call into cpu_idle with preempt disabled */
cpu_startup_entry(CPUHP_ONLINE);
}
在rest_init()函数的末尾,0号进程idle就是在这里启动的。
cpu_startup_entry(): /linux-3.18.6/kernel/sched/idle.c
void cpu_startup_entry(enum cpuhp_state state)
{
/*
* This #ifdef needs to die, but it's too late in the cycle to
* make this generic (arm and sh have never invoked the canary
* init for the non boot cpus!). Will be fixed in 3.11
*/
#ifdef CONFIG_X86
/*
* If we're the non-boot CPU, nothing set the stack canary up
* for us. The boot CPU already has it initialized but no harm
* in doing it again. This is a good place for updating it, as
* we wont ever return from this function (so the invalid
* canaries already on the stack wont ever trigger).
*/
boot_init_stack_canary();
#endif
arch_cpu_idle_prepare();
cpu_idle_loop();
}
static void cpu_idle_loop(void)
{
while (1) {
/*
* If the arch has a polling bit, we maintain an invariant:
*
* Our polling bit is clear if we're not scheduled (i.e. if
* rq->curr != rq->idle). This means that, if rq->idle has
* the polling bit set, then setting need_resched is
* guaranteed to cause the cpu to reschedule.
*/
__current_set_polling();
tick_nohz_idle_enter();
while (!need_resched()) {
check_pgt_cache();
rmb();
if (cpu_is_offline(smp_processor_id()))
arch_cpu_idle_dead();
local_irq_disable();
arch_cpu_idle_enter();
/*
* In poll mode we reenable interrupts and spin.
*
* Also if we detected in the wakeup from idle
* path that the tick broadcast device expired
* for us, we don't want to go deep idle as we
* know that the IPI is going to arrive right
* away
*/
if (cpu_idle_force_poll || tick_check_broadcast_expired())
cpu_idle_poll();
else
cpuidle_idle_call();
arch_cpu_idle_exit();
}
/*
* Since we fell out of the loop above, we know
* TIF_NEED_RESCHED must be set, propagate it into
* PREEMPT_NEED_RESCHED.
*
* This is required because for polling idle loops we will
* not have had an IPI to fold the state for us.
*/
preempt_set_need_resched();
tick_nohz_idle_exit();
__current_clr_polling();
/*
* We promise to call sched_ttwu_pending and reschedule
* if need_resched is set while polling is set. That
* means that clearing polling needs to be visible
* before doing these things.
*/
smp_mb__after_atomic();
sched_ttwu_pending();
schedule_preempt_disabled();
}
}
Linux内核分析3的更多相关文章
- linux内核分析作业8:理解进程调度时机跟踪分析进程调度与进程切换的过程
1. 实验目的 选择一个系统调用(13号系统调用time除外),系统调用列表,使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 分析汇编代码调用系统调用的工作过程,特别是参数的传递的方 ...
- Linux内核分析作业7:Linux内核如何装载和启动一个可执行程序
1.可执行文件的格式 在 Linux 平台下主要有以下三种可执行文件格式: 1.a.out(assembler and link editor output 汇编器和链接编辑器的输出) ...
- linux内核分析作业6:分析Linux内核创建一个新进程的过程
task_struct结构: struct task_struct { volatile long state;进程状态 void *stack; 堆栈 pid_t pid; 进程标识符 u ...
- linux内核分析作业5:分析system_call中断处理过程
1.增加 Menu 内核命令行 调试系统调用. 步骤:删除menu git clone (tab) make rootfs 这就是我们将 fork 函数写入 Menu 系统内核后的效果, ...
- linux内核分析作业:以一简单C程序为例,分析汇编代码理解计算机如何工作
一.实验 使用gcc –S –o main.s main.c -m32 命令编译成汇编代码,如下代码中的数字请自行修改以防与他人雷同 int g(int x) { return x + 3; } in ...
- linux内核分析作业:操作系统是如何工作的进行:完成一个简单的时间片轮转多道程序内核代码
计算机如何工作 三个法宝:存储程序计算机.函数调用堆栈.中断机制. 堆栈 函数调用框架 传递参数 保存返回地址 提供局部变量空间 堆栈相关的寄存器 Esp 堆栈指针 (stack pointer) ...
- linux内核分析作业3:跟踪分析Linux内核的启动过程
内核源码目录 1. arch:录下x86重点关注 2. init:目录下main.c中的start_kernel是启动内核的起点 3. ipc:进程间通信的目录 实验 使用实验楼的虚拟机打开shell ...
- linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
系统调用:库函数封装了系统调用,通过库函数和系统调用打交道 用户态:低级别执行状态,代码的掌控范围会受到限制. 内核态:高执行级别,代码可移植性特权指令,访问任意物理地址 为什么划分级别:如果全部特权 ...
- 《Linux内核分析》期末总结
Linux内核设计期中总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 前八周博客汇总及总结 Linux内核设计第一周——从汇编语言出发理解计算机工作原理 我们学习了汇编语言的基础知识,这一 ...
- 《Linux及安全》期中总结&《Linux内核分析》期终总结
[5216 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK NINE ...
随机推荐
- MySQL数据库之数据类型和完整性约束
补充: select * from mysql.user #显示出来乱了 select * from mysql.user\G #加了\G后一行一行显示了 一.数据类型:分不同种类去存不同类型的数据 ...
- selenium webdriver API详解(一)
本系列主要讲解webdriver常用的API使用方法(注意:使用前请确认环境是否安装成功,浏览器驱动是否与谷歌浏览器版本对应) 一:打开某个网址:get() from selenium import ...
- Lua学习笔记(5): 表
表的初始化方式 表的索引类型一般有两种,一种是通过标识符访问,一种是通过数字访问 --通过标识符访问的表的初始化 table1 = {key_1 = "haha", key_2 = ...
- Linux系统进程管理
Linux系统进程管理 什么是进程 进程是已启动的可执行程序的运行实例,进程有以下组成部分: 分配内存, 已分配内存的地址空间 安全属性, 进程的运行身份和权限 进程代码, 运行一个或多个的线程 进程 ...
- curl和file_get_contents 区别以及各自的优劣
PHP中fopen,file_get_contents,curl函数的区别: 1.fopen /file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存.但是CUR ...
- Android:有关菜单的学习(供自己参考)
Android:有关==菜单==的学习 上下文菜单 上下文菜单就是手机中对某一项进行==点击一定时间==后弹出的针对该项处理的菜单. context_menu.xml: <?xml versio ...
- 团队介绍 you i
我们团队一共四个人,我们足够了解对方的优缺点,能够很好的进行交流沟通.对于一些问题也能有好的方法去解决,我做事情比较讲究高效和尽可能的完美,或者说要做到我自己觉得完美,才会停下来.对于一件事情,我有自 ...
- 寒假学习计划(C++)
课程 1,计算机程序设计(C++)-西安交通大学(中国大学mooc)课程链接 2,面向对象程序设计-C++-浙大-翁恺(网易云课堂)课程链接 理由 1西安交大的C++慕课从零基础教起,更注重基础,重点 ...
- C++寒假计划
课程 西北工业大学的c++程序设计 理由 这个课程里的内容都比较详细,能比较全面的讲解C++,我们是从C过渡到C++的,所以我之前看了阚道洪的面向对象程序设计的课程,他讲解了两者的差别,还有C++对C ...
- Alpha 冲刺(4/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前后端接口的开发 测试项目运行的服务器环 ...