linux中的进程栈

1、linux中的user mode的进程栈

在thread_info.h中,设置进程栈的大小为16k

#define THREAD_SIZE		16384
#define THREAD_START_SP		(THREAD_SIZE - 16)

在head.S中, 在进程切换的时候:
将内核这个进程栈的栈顶写入到了sp中,sp = init_thread_union + THREAD_SIZE
将init_task写入到了sp_el0中,设置用户空间的程序栈

__mmap_switched:
......
#ifdef CONFIG_THREAD_INFO_IN_TASK
        adrp    x4, init_thread_union
        add     sp, x4, #THREAD_SIZE
        adr_l   x5, init_task
        msr     sp_el0, x5                      // Save thread_info
#else
	adr_l	sp, initial_sp, x4
	mov	x4, sp
	and	x4, x4, #~(THREAD_SIZE - 1)
	msr	sp_el0, x4			// Save thread_info
#endif

在linux 中每一个应用程序,在内核中都有一个task_struct结构体,每个task_struct代码一个内核进程,都有一个进程栈,该栈的栈顶写入到了sp
另外,每一个应用程序,在userspace空间,都有一个程序栈,该栈的栈顶写入到了sp_el0中

总结:每个应用程序有两个栈,一个是在linux kernel中的task_struct结构体中指向的进程栈、一个是user程序中的栈

猜你喜欢

转载自blog.csdn.net/weixin_42135087/article/details/107523095