目錄
-
-
-
- 1、linux kernel中的中斷irq的棧stack
-
- (1)、arm32體系的irq的棧
- (2)、arm64體系的irq的棧
- 2、linux kernel中的棧stack
-
- (1)、概念介紹:核心棧、核心空間程序棧、使用者空間程序棧
- (2)、核心棧 的實作
- (2)、核心程序棧、使用者程序棧 的實作
- 3、總結
-
-
★★★ 友情連結 : 個人部落格導讀首頁—點選此處 ★★★
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棧