Niravsinh Parmar
Niravsinh Parmar

Reputation: 117

What is ALIGN in arch/i386/kernel/head.S in linux source code

In the head.s file present in linux source code at path arch/i386/kernel/head.S, ALIGN is used as seen in code snippet given below after ret instruction. My question is that what is this ALIGN, as per my knowledge it is not instruction, not assembler directive, so what is this and why it is used here?

You can get the code of head.S at site given below:

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

Path: 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

Upvotes: 1

Views: 522

Answers (1)

Sam Protsenko
Sam Protsenko

Reputation: 14763

Actually ALIGN is just a macro, defined at include/linux/linkage.h file:

#ifdef __ASSEMBLY__
#define ALIGN __ALIGN

And __ALIGN definition depends on architecture. For x86 you have next definition (in kernel 2.4), in the same file:

#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

So in the end ALIGN macro is just .align asm directive, and it's either 4- or 16-bytes alignment (depending on CONFIG_X86_ALIGNMENT_16 option value).

You can figure out your CONFIG_X86_ALIGNMENT_16 option value from arch/i386/config.in file. This value actually depends on your processor family.


Another question is why such an alignment is needed at all. And my understanding is next. Usually CPU can access only aligned addresses on bus (for 32-bit bus the address usually should be aligned by 4 bytes, e.g. you can access 0x0, 0x4, 0x8 addresses etc., but you can't access 0x1, 0x3 addresses, because it would lead to unaligned access on bus).

But in your case I believe it's not the case, and alignment is done only for performance reasons. Basically this alignment allows CPU to fetch 1: section more quickly:

ALIGN

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

So it seems like this ALIGN is just some minor optimization.

See also next topics:

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

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

Upvotes: 5

Related Questions