您的位置:首页 > 运维架构 > Linux

arm Linux系统启动之----reset_init,系统1号进程

2015-02-26 11:16 531 查看
先来看下一些基础概念

内核线程(thread)或叫守护进程(daemon),在操作系统中占据相当大的比例,当Linux操作系统启动以后,

尤其是Xwindow也启动以后,你可以用”ps”命令查看系统中的进程,这时会发现很多以”d”结尾的进程名,这些进程就是内核线程。

内核线程也可以叫内核任务,它们周期性地执行,例如,磁盘高速缓存的刷新,网络连接的维护,页面的换入换出等等。在Linux中,内核线程与普通进程有一些本质的区别,从以下几个方面可以看出二者之间的差异:

· 内核线程执行的是内核中的函数,而普通进程只有通过系统调用才能执行内核中的函数。

· 内核线程只运行在内核态,而普通进程既可以运行在用户态,也可以运行在内核态。

· 因为内核线程指只运行在内核态,因此,它只能使用大于PAGE_OFFSET(3G)的地址空间。另一方面,不管在用户态还是内核态,普通进程可以使用4GB的地址空间。

在系统start_kernel最后的一个函数,起来的是系统的1号线程kernel_init

[cpp] view
plaincopy

static noinline void __init_refok rest_init(void)

__releases(kernel_lock)

{

int pid;

//对于RCU机制可以看看上篇文章中对rcu引用的两篇博文

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 | CLONE_SIGHAND);

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);

unlock_kernel();

/*

* The boot idle thread must execute schedule()

* at least once to get things moving:

*/

init_idle_bootup_task(current);

preempt_enable_no_resched();

schedule();

preempt_disable();

/* Call into cpu_idle with preempt disabled */

cpu_idle();

}

这里我们来分析下kernel_thread(kernel_init, NULL, CLONE_FS | CLONE_SIGHAND)其中的kernel_init。这里是我们建立的init进程。

执行kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES)来启动内核线程kthreadd,它的工作是用来运行kthread_create_list全局链表中的kthread

然后创建idle线程来占用掉cpu空闲时的时间片。

现在我们先看看kernel_init

[cpp] view
plaincopy

static int __init kernel_init(void * unused)

{

/*

* Wait until kthreadd is all set-up.

*/

wait_for_completion(&kthreadd_done);

lock_kernel();

/*

* init can allocate pages on any node

*/

set_mems_allowed(node_states[N_HIGH_MEMORY]);

/*

* init can run on any cpu.

*/

set_cpus_allowed_ptr(current, cpu_all_mask);

/*

* Tell the world that we're going to be the grim

* reaper of innocent orphaned children.

*

* We don't want people to have to make incorrect

* assumptions about where in the task array this

* can be found.

*/

init_pid_ns.child_reaper = current;

cad_pid = task_pid(current);

smp_prepare_cpus(setup_max_cpus);

do_pre_smp_initcalls();

start_boot_trace();

smp_init();

sched_init_smp();

do_basic_setup();

/* Open the /dev/console on the rootfs, this should never fail */

if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)

printk(KERN_WARNING "Warning: unable to open an initial console./n");

(void) sys_dup(0);

(void) sys_dup(0);

/*

* check if there is an early userspace init. If yes, let it do all

* the work

*/

if (!ramdisk_execute_command)

ramdisk_execute_command = "/init";

if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {

ramdisk_execute_command = NULL;

prepare_namespace();

}

/*

* Ok, we have completed the initial bootup, and

* we're essentially up and running. Get rid of the

* initmem segments and start the user-mode stuff..

*/

init_post();

return 0;

}

进入kernel_init则在等待kthreadd_done的释放,这里就可以看见在reset_init中的对于thread的描述。

lock_kernel则是系统的大内核锁,紧接着系统进行一系列的初始化。

do_basic_setup是我们重点关注的函数

[c-sharp] view
plaincopy

/*

* Ok, the machine is now initialized. None of the devices

* have been touched yet, but the CPU subsystem is up and

* running, and memory and process management works.

*

* Now we can finally start doing some real work..

*/

static void __init do_basic_setup(void)

{

init_workqueues();

cpuset_init_smp();

usermodehelper_init();

init_tmpfs();

driver_init();

init_irq_proc();

do_ctors();

do_initcalls();

}

init_workqueues,初始化工作队列events,每个CPU一个,这里就是管理中断下半部的workqueue在这里初始化了

cpuset_init_smp因为没有配置CONFIG_CPUSETS,是个空函数。

usermodehelper_init,初始化工作队列khelper,每个CPU一个。

init_tmpfs,注册并安装tmpfs文件系统,它的file_system_type结构如下:

[cpp] view
plaincopy

static struct file_system_type tmpfs_fs_type = {

.owner = THIS_MODULE,

.name = "tmpfs",

.get_sb = shmem_get_sb,

.kill_sb = kill_litter_super,

};

driver_init,建立设备驱动模型sysfs的kset、kobject和subsystem结构,并向其中注册cpu、内存和总线的驱动。

init_irq_proc,向/proc文件系统中增加子目录irq来显示中断描述符表中的所有元素,在

系统运行的时候我们可以通过cat /proc/interrupts来查看注册的中断和当前产生的中断数。

do_ctors?????

最关心的do_initcalls()下面详细分析

[cpp] view
plaincopy

static void __init do_initcalls(void)

{

initcall_t *fn;

for (fn = __early_initcall_end; fn < __initcall_end; fn++)

do_one_initcall(*fn);

/* Make sure there is no pending stuff from the initcall sequence */

flush_scheduled_work();

}

这里牵扯到内核中的init段。先看看module_init这个常用的接口函数(include/init.h)

[cpp] view
plaincopy

#define __define_initcall(level,fn,id) /

static initcall_t __initcall_##fn##id __used /

__attribute__((__section__(".initcall" level ".init"))) = fn

#define __initcall(fn) device_initcall(fn)

#define module_init(x) __initcall(x);

可以看见上述宏定义,最终指向init段

对于__early_initcall_end的定义可以参考(include/asm-generic/vmlinux.lds.h)

[cpp] view
plaincopy

#define INITCALLS /

*(.initcallearly.init) /

VMLINUX_SYMBOL(__early_initcall_end) = .; /

*(.initcall0.init) /

*(.initcall0s.init) /

*(.initcall1.init) /

*(.initcall1s.init) /

*(.initcall2.init) /

*(.initcall2s.init) /

*(.initcall3.init) /

*(.initcall3s.init) /

*(.initcall4.init) /

*(.initcall4s.init) /

*(.initcall5.init) /

*(.initcall5s.init) /

*(.initcallrootfs.init) /

*(.initcall6.init) /

*(.initcall6s.init) /

*(.initcall7.init) /

*(.initcall7s.init)

这里在看

[cpp] view
plaincopy

#define pure_initcall(fn) __define_initcall("0",fn,0)

#define core_initcall(fn) __define_initcall("1",fn,1)

#define core_initcall_sync(fn) __define_initcall("1s",fn,1s)

#define postcore_initcall(fn) __define_initcall("2",fn,2)

#define postcore_initcall_sync(fn) __define_initcall("2s",fn,2s)

#define arch_initcall(fn) __define_initcall("3",fn,3)

#define arch_initcall_sync(fn) __define_initcall("3s",fn,3s)

#define subsys_initcall(fn) __define_initcall("4",fn,4)

#define subsys_initcall_sync(fn) __define_initcall("4s",fn,4s)

#define fs_initcall(fn) __define_initcall("5",fn,5)

#define fs_initcall_sync(fn) __define_initcall("5s",fn,5s)

#define rootfs_initcall(fn) __define_initcall("rootfs",fn,rootfs)

#define device_initcall(fn) __define_initcall("6",fn,6)

#define device_initcall_sync(fn) __define_initcall("6s",fn,6s)

#define late_initcall(fn) __define_initcall("7",fn,7)

#define late_initcall_sync(fn) __define_initcall("7s",fn,7s)

就应该明白在内核中的init的初始化的调用顺序了吧。

在这里系统里所有的initcall就会被调用了,这是为了避免把所有代码集合到一块的一个方法。但是同一级别的init顺序则是由编译的链接顺序所决定的。

好了,相关的初始化完成之后,最后在kernel_init执行的是

[cpp] view
plaincopy

/*

* check if there is an early userspace init. If yes, let it do all

* the work

*/

if (!ramdisk_execute_command)

ramdisk_execute_command = "/init";

if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {

ramdisk_execute_command = NULL;

prepare_namespace();

}

这段代码检查是否有必要mount根文件系统,如果vmlinuz中带有initfamfs,而且其中已经有init,那么就不这么做了(我现在工作用的目标系统就是这样的,里面有个init),否则的话内核还要mount init所在的(也是所有用户态进程的最除根文件系统)根文件系统,挂在根文件系统和执行init是linux启动过程最后要做的事情

最后执行init_post

[cpp] view
plaincopy

static noinline int init_post(void)

__releases(kernel_lock)

{

/* need to finish all async __init code before freeing the memory */

async_synchronize_full();

imv_unref_core_init();

free_initmem();

unlock_kernel();

mark_rodata_ro();

system_state = SYSTEM_RUNNING;

numa_default_policy();

log_boot("Kernel_init_done");

current->signal->flags |= SIGNAL_UNKILLABLE;

if (ramdisk_execute_command) {

run_init_process(ramdisk_execute_command);

printk(KERN_WARNING "Failed to execute %s/n",

ramdisk_execute_command);

}

/*

* 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) {

run_init_process(execute_command);

printk(KERN_WARNING "Failed to execute %s. Attempting "

"defaults.../n", execute_command);

}

run_init_process("/sbin/init");

run_init_process("/etc/init");

run_init_process("/bin/init");

run_init_process("/bin/sh");

panic("No init found. Try passing init= option to kernel. "

"See Linux Documentation/init.txt for guidance.");

}

在这个函数可以看到释放了__init空间,释放了大内核锁,开始启动下一个init咯。

log_boot("Kernel_init_done");,so,kernel init到了这里也就告一段落了。

从启动过程可以看到涉及到内存管理,文件管理,时钟管理,进程管理,中断,调试,还有相关的机制等等。

路漫漫呀。。。

转载地址:http://blog.csdn.net/skywalkzf/article/details/6415708
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: