【问题标题】:What is ALIGN in arch/i386/kernel/head.S in linux source codelinux源代码中arch/i386/kernel/head.S中的ALIGN是什么
【发布时间】:2016-01-09 03:11:42
【问题描述】:

在位于路径arch/i386/kernel/head.S 的linux 源代码中的head.s 文件中,使用ALIGN,如下面的ret 指令后给出的代码sn-p 所示。我的问题是这个ALIGN 是什么,据我所知它不是指令,也不是汇编指令,那么这是什么以及为什么在这里使用它?

您可以在以下网站获取head.S的代码:

http://kneuro.net/cgi-bin/lxr/http/source/arch/i386/kernel/head.S?v=2.4.0

路径:arch/i386/kernel/head.S

/*
 * We depend on ET to be correct. This checks for 287/387.
 */
check_x87:
    movb $0,X86_HARD_MATH
    clts
    fninit
    fstsw %ax
    cmpb $0,%al
    je 1f
    movl %cr0,%eax
    xorl $4,%eax
    movl %eax,%cr0
    ret
    ALIGN            /* why ALIGN is used and what it is? */

1:  movb $1,X86_HARD_MATH
    .byte 0xDB,0xE4
    ret

【问题讨论】:

  • 强制下一条指令与下一个字边界对齐?
  • @user3528438 不是真正的 word 边界,它更复杂。在现代 CPU 上,通常是 16 字节对齐。

标签: linux-kernel


【解决方案1】:

其实ALIGN只是一个宏,定义在include/linux/linkage.h文件:

#ifdef __ASSEMBLY__
#define ALIGN __ALIGN

__ALIGN 的定义取决于架构。对于x86,您在the same file 中有下一个定义(在内核2.4 中):

#if defined(__i386__) && defined(CONFIG_X86_ALIGNMENT_16)
#define __ALIGN .align 16,0x90
#define __ALIGN_STR ".align 16,0x90"
#else
#define __ALIGN .align 4,0x90
#define __ALIGN_STR ".align 4,0x90"
#endif

所以最后ALIGN 宏只是.align asm 指令,它是4 或16 字节对齐(取决于CONFIG_X86_ALIGNMENT_16 选项值)。

您可以从arch/i386/config.in 文件中找出您的CONFIG_X86_ALIGNMENT_16 选项值。该值实际上取决于您的处理器系列。


另一个问题是为什么需要这种对齐方式。接下来是我的理解。通常 CPU 只能访问总线上对齐的地址(对于 32 位总线,地址通常应该对齐 4 个字节,例如您可以访问 0x0、0x4、0x8 地址等,但您 不能访问 0x1、0x3 地址,因为这会导致unaligned access on bus)。

但在您的情况下,我认为情况并非如此,并且仅出于性能原因进行对齐。基本上这种对齐方式允许 CPU 更快地获取 1: 部分:

ALIGN

1:  movb $1,X86_HARD_MATH
    .byte 0xDB,0xE4
    ret

所以看起来这个ALIGN 只是一些小的优化。

另请参阅下一个主题:

[1]Why should code be aligned to even-address boundaries on x86?

[2]Performance optimisations of x86-64 assembly - Alignment and branch prediction

【讨论】:

  • __ALIGN 很重要,但我说的只是 ALIGN
  • 如果 ALIGN 也是 macor 我在哪里可以获得 macor ALIGN 的源代码
  • 是的,ALIGN 也是一个宏,它变成了__ALIGN。请参阅我的回答中的第一个#define。所以ALIGN 基本上 __ALIGNALIGN__ALIGN 的源代码可以在 include/linux/linkage.h 找到,正如我在上面的回答中提到的那样。
猜你喜欢
  • 2014-03-21
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 2023-01-29
  • 2018-01-14
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多