linux kernel中的栈的介绍


★★★ 友情链接 : 个人博客导读首页—点击此处 ★★★

1、linux kernel中的中断irq的栈stack
(1)、arm32体系的irq的栈

如下结构体描述了IRQ mode的栈,很小,只有12bytes。那是因为在linux kernel arm32体系中,真正的中断处理都在svc mode。
发生中断时,先进入irq mode,再进入svc mode完成大部分工作.

(kernel-4.14/arch/arm/kernel/setup.c)
struct stack {
	u32 irq[3];
	u32 abt[3];
	u32 und[3];
	u32 fiq[3];
} ____cacheline_aligned;

#ifndef CONFIG_CPU_V7M
static struct stack stacks[NR_CPUS];
#endif

}

如下描述了调用cpu_init来设置栈的过程,

(kernel-4.14/arch/arm/kernel/setup.c)
/*
 * cpu_init - initialise one CPU.
 *
 * cpu_init sets up the per-CPU stacks.
 */
void notrace cpu_init(void)
{
#ifndef CONFIG_CPU_V7M
	unsigned int cpu = smp_processor_id();
	struct stack *stk = &stacks[cpu];

	if (cpu >= NR_CPUS) {
		pr_crit("CPU%u: bad primary CPU number\n", cpu);
		BUG();
	}

	/*
	 * This only works on resume and secondary cores. For booting on the
	 * boot cpu, smp_prepare_boot_cpu is called after percpu area setup.
	 */
	set_my_cpu_offset(per_cpu_offset(cpu));

	cpu_proc_init();

	/*
	 * Define the placement constraint for the inline asm directive below.
	 * In Thumb-2, msr with an immediate value is not allowed.
	 */
#ifdef CONFIG_THUMB2_KERNEL
#define PLC	"r"
#else
#define PLC	"I"
#endif

	/*
	 * setup stacks for re-entrant exception handlers
	 */
	__asm__ (
	"msr	cpsr_c, %1\n\t"
	"add	r14, %0, %2\n\t"
	"mov	sp, r14\n\t"
	"msr	cpsr_c, %3\n\t"
	"add	r14, %0, %4\n\t"
	"mov	sp, r14\n\t"
	"msr	cpsr_c, %5\n\t"
	"add	r14, %0, %6\n\t"
	"mov	sp, r14\n\t"
	"msr	cpsr_c, %7\n\t"
	"add	r14, %0, %8\n\t"
	"mov	sp, r14\n\t"
	"msr	cpsr_c, %9"
	    :
	    : "r" (stk),
	      PLC (PSR_F_BIT | PSR_I_BIT | IRQ_MODE),
	      "I" (offsetof(struct stack, irq[0])),
	      PLC (PSR_F_BIT | PSR_I_BIT | ABT_MODE),
	      "I" (offsetof(struct stack, abt[0])),
	      PLC (PSR_F_BIT | PSR_I_BIT | UND_MODE),
	      "I" (offsetof(struct stack, und[0])),
	      PLC (PSR_F_BIT | PSR_I_BIT | FIQ_MODE),
	      "I" (offsetof(struct stack, fiq[0])),
	      PLC (PSR_F_BIT | PSR_I_BIT | SVC_MODE)
	    : "r14");
#endif
}
(2)、arm64体系的irq的栈

先看下irq_handler的中断函数处理的过程

/*
 * Interrupt handling.
 */
	.macro	irq_handler
	ldr_l	x1, handle_arch_irq   -----将handle地址保存在x1
	mov	x0, sp
	irq_stack_entry   ------ 切换栈,也就是将svc栈切换程irq栈. 在此之前,SP还是EL1_SP,在此函数中,将EL1_SP保存,再将IRQ栈的地址写入到SP寄存器
	blr	x1     ——————执行中断处理函数
	irq_stack_exit   ------ 恢复EL1_SP(svc栈)
	.endm
	.macro	irq_stack_entry
	mov	x19, sp			// preserve the original sp    //将svc mode下的栈地址(也就是EL1_SP)保存到x19

	/*
	 * Compare sp with the base of the task stack.
	 * If the top ~(THREAD_SIZE - 1) bits match, we are on a task stack,
	 * and should switch to the irq stack.
	 */
#ifdef CONFIG_THREAD_INFO_IN_TASK
	ldr	x25, [tsk, TSK_STACK]
	eor	x25, x25, x19
	and	x25, x25, #~(THREAD_SIZE - 1)
	cbnz	x25, 9998f
#else
	and	x25, x19, #~(THREAD_SIZE - 1)
	cmp	x25, tsk
	b.ne	9998f
#endif

	adr_this_cpu x25, irq_stack, x26
	mov	x26, #IRQ_STACK_START_SP     //IRQ_STACK_START_SP这是irq mode的栈地址
	add	x26, x25, x26

	/* switch to the irq stack */
	mov	sp, x26     //将irq栈地址,写入到sp

	/*
	 * Add a dummy stack frame, this non-standard format is fixed up
	 * by unwind_frame()
	 */
	stp     x29, x19, [sp, #-16]!
	mov	x29, sp

9998:
	.endm
/*
 * x19 should be preserved between irq_stack_entry and
 * irq_stack_exit.
 */
.macro	irq_stack_exit
mov	sp, x19     //x19保存着svc mode下的栈,也就是EL1_SP
.endm

那么irq的栈在哪设置的,多大呢?

在irq.h中定义了,irq栈的地址和size

#define IRQ_STACK_SIZE			THREAD_SIZE
#define IRQ_STACK_START_SP		THREAD_START_SP

thread_info.h中定义了大小

#define THREAD_SIZE		16384     //也就是irq栈的大小大概15k
#define THREAD_START_SP		(THREAD_SIZE - 16)    //也就是irq栈的首地址是从"0地址+15k"这个地方开始的
2、linux kernel中的栈stack
(1)、概念介绍:内核栈、内核空间进程栈、用户空间进程栈

用户空间进程栈、内核空间进程栈
对于一个应用程序而言,可以运行在用户空间,也可以通过系统调用进入内核空间。在用户空间,使用的是用户栈,也就是我们软件工程师编写用户空间程序的时候,保存局部变量的stack。陷入内核后,当然不能用用户栈了,这时候就需要使用到内核栈。所谓内核栈其实就是处于SVC mode时候使用的栈。

内核栈
在linux最开始启动的时候,系统只有一个进程(更准确的说是kernel thread),就是PID等于0的那个进程,叫做swapper进程(或者叫做idle进程)。

(2)、内核栈 的实现

内核栈是静态定义的,如下:

(kernel-4.14/arch/arm/include/asm/thread_info.h)
  #define init_thread_info	(init_thread_union.thread_info)
  #define init_stack		(init_thread_union.stack)
(kernel-4.14/include/linux/sched.h)
union thread_union {
#ifndef CONFIG_THREAD_INFO_IN_TASK
	struct thread_info thread_info;
#endif
	unsigned long stack[THREAD_SIZE/sizeof(long)];
};
(2)、内核进程栈、用户进程栈 的实现

Linux kernel在内核线程,或用户线程时都会分配一个page frame,具体代码如下:

(kernel-4.14/kernel/fork.c)
static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
{
......
	stack = alloc_thread_stack_node(tsk, node);
	if (!stack)
		goto free_tsk;
......
}

底部是struct thread_info数据结构,顶部(高地址)就是该进程的栈了。
当进程切换的时候,整个硬件和软件的上下文都会进行切换,这里就包括了svc mode的sp寄存器的值被切换到调度算法选定的新的进程的内核栈上来

3、总结

linux kernel arm32中定义的irq栈,其实就在一个static struct stack结构体变量中,大小为12bytes. irq_hander使用svc栈
linux kernel arm64中定义的irq栈,在内存"首地址"处,大小16k. irq_hander使用irq栈

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码改变世界ctw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值