微视linux swapper进程

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值