I'm trying to understand more about process 0, such as, whether it has a memory descriptor (non-NULL task_struct->mm field) or not, and how is it related to the swap or idle process. It seems to me that a single 'process 0' is created on the boot cpu, and then an idle thread is created for every other cpu by idle_threads_init, but I didn't find where the first one( I assume that is the process 0) was created.
Update
In light of the live book that tychen referenced, here is my most up-to-date understanding regarding process 0 (for x86_64), can someone confirm/refute the items below?
- An
init_tasktypedtask_structis statically defined, with the task's kernel stackinit_task.stack = init_stack, memory descriptorinit_task.mm=NULLandinit_task.active_mm=&init_mm, where the stack areainit_stackandmm_structinit_mmare both statically defined. - The fact that only
active_mmis non-NULL meansprocess 0is a kernel process. Also,init_task.flags=PF_KTHREAD. - Not long after the uncompressed kernel image begins execution, boot cpu starts to use
init_stackas kernel stack. This makes thecurrentmacro meaningful (for the first time since machine boots up), which makesfork()possible. After this point, the kernel literally runs inprocess 0's conext. start_kernel->arch_call_rest_init->rest_init, and inside this function,process 1&2are forked. Within thekernel_initfunction which is scheduled forprocess 1, a new thread (withCLONE_VM) is made and hooked to a CPU's run queue'srq->idle, for every other logical CPU.- Interestingly, all idle threads share the same
tid 0(not onlytgid). Usually threads sharetgidbut have distincttid, which is really Linux'sprocess id. I guess it doesn't break anything because idle threads are locked to their own CPUs. kernel_initloads theinitexecutable (typically/sbin/init), and switches bothcurrent->mmandactive_mmto a non-NULLmm_struct, and clears thePF_KTHREADflag, which makesprocess 1a legitimate user space process. Whileprocess 2does not tweakmm, meaning it remains a kernel process, same asprocess 0.- At the end of
rest_init,do_idletakes over, which means all CPU has an idle process. - Something confused me before, but now becomes clear: the
init_*objects/labels such asinit_task/init_mm/init_stackare all used byprocess 0, and not theinit process, which isprocess 1.
