Linux 内核中断处理程序中传递函数参数(从 asm 到 C)

问题描述 投票:0回答:2

当我阅读Linux内核源代码时,我遇到了这段代码:

__visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)   
{
    struct pt_regs *old_regs = set_irq_regs(regs);

    entering_ack_irq();
    local_apic_timer_interrupt();
    exiting_irq();

    set_irq_regs(old_regs);
}

函数

smp_apic_timer_interrupt()
采用一个参数。这个函数的调用是通过一段汇编语言代码来实现的:

ENTRY(apic_timer_interrupt)
     RING0_INT_FRAME;     
     ASM_CLAC;           
     pushl_cfi $~(0xef);
     SAVE_ALL;         
     TRACE_IRQS_OFF
     movl %esp,%eax;
     call smp_apic_timer_interrupt; // <------call high level C function       
     jmp ret_from_intr;      
     CFI_ENDPROC;           
ENDPROC(apic_timer_interrupt)

我无法弄清楚高级C函数

smp_apic_timer_interrupt()
如何获取其参数(通过哪个寄存器)?

assembly linux-kernel x86 interrupt-handling calling-convention
2个回答
9
投票

您可能正在考虑正常的调用约定(堆栈上的参数)。现代 Linux 内核(32 位变体)将寄存器中的前 3 个参数(按顺序 EAXEDXECX)传递作为优化。根据内核的不同,此约定被指定为使用

__attribute__(regparm(3))
的函数的属性修饰符,或者现代版本的内核在命令行上将
-mregparm=3
选项传递给 GCCGCC documentation 关于该选项/属性的说明如下:

regparm (number)
On the Intel 386, the regparm attribute causes the compiler to pass up to
number integer arguments in registers EAX, EDX, and ECX instead of on the
stack. Functions that take a variable number of arguments will continue to
be passed all of their arguments on the stack. 

在古代内核中,正常的 32 位 ABI(以及堆栈上的参数约定)是常态。最终,内核配置支持寄存器中的参数通过内核构建配置中的CONFIG_REGPARM设置支持正常堆栈约定:

config REGPARM
    bool "Use register arguments"
    default y
    help
    Compile the kernel with -mregparm=3. This instructs gcc to use
    a more efficient function call ABI which passes the first three
    arguments of a function call via registers, which results in denser
    and faster code.
    If this option is disabled, then the default ABI of passing
    arguments via the stack is used.
    If unsure, say Y.

Linux 内核维护者在 2006 年通过这个 kernel commit 摆脱了这个选项:

-mregparm=3 has been enabled by default for some time on i386, and AFAIK
there aren't any problems with it left.
This patch removes the REGPARM config option and sets -mregparm=3
unconditionally.

基于这些知识,我们可以查看您提供的代码,并假设我们所在的内核默认在寄存器中传递前 3 个参数。对于你的情况:

 __visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)

one 参数,因此它被传入 EAX 。调用 smp_apic_timer_interrupt 的代码如下所示:

ENTRY(apic_timer_interrupt)
     RING0_INT_FRAME;     
     ASM_CLAC;           
     pushl_cfi $~(0xef);
     SAVE_ALL;         
     TRACE_IRQS_OFF
     movl %esp,%eax;
     call smp_apic_timer_interrupt; // <------call high level C function       
     jmp ret_from_intr;      
     CFI_ENDPROC;           
ENDPROC(apic_timer_interrupt)

重要的部分是SAVE_ALL宏调用将所有需要的寄存器压入堆栈。它会因内核版本的不同而有所不同,但将寄存器压入堆栈的主要效果是相似的(为了简洁起见,我删除了 DWARF 条目):

.macro SAVE_ALL
         cld
         PUSH_GS
         pushl_cfi %fs
         pushl_cfi %es
         pushl_cfi %ds
         pushl_cfi %eax
         pushl_cfi %ebp
         pushl_cfi %edi
         pushl_cfi %esi
         pushl_cfi %edx
         pushl_cfi %ecx
         pushl_cfi %ebx
         movl $(__USER_DS), %edx
         movl %edx, %ds
         movl %edx, %es
         movl $(__KERNEL_PERCPU), %edx
         movl %edx, %fs
         SET_KERNEL_GS %edx
.endm

完成后ESP将指向最后一个寄存器被压入的位置。该地址通过 movl %esp,%eax 复制到

EAX
,并且 EAX 成为
struct pt_regs *regs
的指针。堆栈上所有压入的寄存器都成为实际的 pt_regs 数据结构,并且 EAX 现在指向它。

对于那些需要以传统方式在堆栈上传递参数的函数,可以在内核中找到

asmlinkage
宏。它的定义如下:

#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))

其中

regparm(0)
表示不会通过寄存器传递任何参数。

人们确实必须知道构建选项是什么,以及所使用的内核版本,以便对所使用的约定进行准确评估。


3
投票

引用自https://www.safaribooksonline.com/library/view/understanding-the-linux/0596005652/ch04s06.html

SAVE_ALL
宏扩展为以下片段:

cld
push %es
push %ds
pushl %eax
pushl %ebp
pushl %edi
pushl %esi
pushl %edx
pushl %ecx
pushl %ebx
movl $ _ _USER_DS,%edx
movl %edx,%ds
movl %edx,%es

保存寄存器后,当前堆栈顶部位置的地址保存在

eax
寄存器中[与
movl %esp,%eax
,因此]
eax
指向包含由
SAVE_ALL
压入的最后一个寄存器值的堆栈位置

所以

eax
寄存器是
smp_apic_timer_interrupt
接收
pt_regs
指针的寄存器。

© www.soinside.com 2019 - 2024. All rights reserved.