swapper是linux第一个线程,也就是0号线程。很多书里面都有详细介绍。
为什么linux启动之后,swapper线程就开始运行了呢?
我认为主要有两点:
第一点:静态部分。
swapper线程通过静态初始化,设置的地址空间是内核高1G的空间。也就是说其使用的正文段,数据段和linux使用的正文段和数据段是完全一致的。
第二点:动态部分。
堆栈寄存器等寄存器在内核启动阶段通过汇编语句设置。堆栈寄存器等寄存器在内核启动阶段通过汇编语句设置。
其中,最重要的是堆栈,定义好的堆栈部分不会被别的模块非法访问;另外一点就是,可以调用别的函数,建立栈帧,在函数返回时可以正常返回。
以powerpc为例:
start_here:
/* ptr to current */
lis r2,init_task@h
ori r2,r2,init_task@l //根据E500 ABI,r2保存当前进程的进程描述符地址
/* Set up for using our exception vectors */
/* ptr to phys current thread */
tophys(r4,r2)
addi r4,r4,THREAD /* init task's THREAD */
CLR_TOP32(r4)
mtspr SPRN_SPRG_THREAD,r4 //SPRN_SPRG_THREAD在E500系列是SPRG3,保存thread的虚拟地址
li r3,0
mtspr SPRN_SPRG_RTAS,r3 /* 0 => not in RTAS */
/* stack */
lis r1,init_thread_union@ha
addi r1,r1,init_thread_union@l
li r0,0
stwu r0,THREAD_SIZE-STACK_FRAME_OVERHEAD(r1) //根据E500 ABI,r1指向栈底-16.16代表预留一个最小栈帧
以x86为例:
.data
ENTRY(stack_start)
.long init_thread_union+THREAD_SIZE
.long __BOOT_DS
ready: .byte 0
--------------------
/*
* Enable paging
*/
movl $pa(swapper_pg_dir),%eax
movl %eax,%cr3 /* set the page table pointer.. */
movl %cr0,%eax
orl $X86_CR0_PG,%eax
movl %eax,%cr0 /* ..and set paging (PG) bit */
ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
1:
/* Set up the stack pointer */
lss stack_start,%esp //esp寄存器指向栈底, SS寄存器指向__BOOT_DS
/*
* Initialize eflags. Some BIOS's leave bits like NT set. This would
* confuse the debugger if this code is traced.
* XXX - best to initialize before switching to protected mode.
*/
pushl $0
popfl
#ifdef CONFIG_SMP
cmpb $0, ready
jz 1f /* Initial CPU cleans BSS */
jmp checkCPUtype
1:
#endif /* CONFIG_SMP */
/*
* start system 32-bit setup. We need to re-do some of the things done
* in 16-bit mode for the "real" operations.
*/
call setup_idt
linux源码分析(三)-start_kernel
https://www.cnblogs.com/yjf512/p/5999532.html
linux调度器源码分析 - 初始化(二)
https://www.cnblogs.com/tolimit/p/4311404.html
深入理解Linux启动过程 && 0号进程,1号进程
https://blog.csdn.net/ZAhqc_IT/article/details/81702067