尝试理解 Linux 内核中的一段内联 Arm 汇编代码

5

我正在尝试理解这段内嵌汇编的作用,它是由C语言DEFINE宏生成的:

#define DEFINE(sym, val) asm volatile("\n->" #sym " %0 " #val : : "i" (val))

#define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)

这是在linux-imx内核分支中用于进行AArch64 smc调用的魔法代码。上述C宏定义值的位置在这里

// arch/arm64/kernel/asm-offsets.c

int main(void)
{
    (...)
      DEFINE(ARM_SMCCC_RES_X0_OFFS,     offsetof(struct arm_smccc_res, a0));
      DEFINE(ARM_SMCCC_RES_X2_OFFS,     offsetof(struct arm_smccc_res, a2));
      DEFINE(ARM_SMCCC_QUIRK_ID_OFFS,   offsetof(struct arm_smccc_quirk, id));
      DEFINE(ARM_SMCCC_QUIRK_STATE_OFFS,    offsetof(struct arm_smccc_quirk, state));
    (...)
}

后来,这些定义被用于汇编宏中,这里提供(至少看起来是这样)。
// arch/arm64/kernel/smccc-call.S

    .macro SMCCC instr
    .cfi_startproc
    \instr  #0
    ldr x4, [sp]
    stp x0, x1, [x4, #ARM_SMCCC_RES_X0_OFFS]
    stp x2, x3, [x4, #ARM_SMCCC_RES_X2_OFFS]
    ldr x4, [sp, #8]
    cbz x4, 1f /* no quirk structure */
    ldr x9, [x4, #ARM_SMCCC_QUIRK_ID_OFFS]
    cmp x9, #ARM_SMCCC_QUIRK_QCOM_A6
    b.ne    1f
    str x6, [x4, ARM_SMCCC_QUIRK_STATE_OFFS]
1:  ret
    .cfi_endproc
    .endm

ENTRY(__arm_smccc_smc)
    SMCCC   smc
ENDPROC(__arm_smccc_smc)

最终,它会在实践中使用,例如在gpc-psci driver中。

我试图将代码隔离到一个单独的裸机AArch64程序中,以查看宏如何扩展和在实践中工作,但我得到的只是编译错误。

// main.c
// the struct arm_smccc_res and others are inserted here to satisfy the compiler

int
main()
{
        DEFINE(ARM_SMCCC_RES_X0_OFFS,      offsetof(struct arm_smccc_res, a0));
        DEFINE(ARM_SMCCC_RES_X2_OFFS,      offsetof(struct arm_smccc_res, a2));
        DEFINE(ARM_SMCCC_QUIRK_ID_OFFS,    offsetof(struct arm_smccc_quirk, id));
        DEFINE(ARM_SMCCC_QUIRK_STATE_OFFS, offsetof(struct arm_smccc_quirk, state));
        return 0;
}

// gcc output

/tmp/cccqaaY3.s: Assembler messages:
/tmp/cccqaaY3.s:459: Error: junk at end of line, first unrecognized character is `-'
/tmp/cccqaaY3.s:464: Error: junk at end of line, first unrecognized character is `-'
/tmp/cccqaaY3.s:469: Error: junk at end of line, first unrecognized character is `-'
/tmp/cccqaaY3.s:474: Error: junk at end of line, first unrecognized character is `-'
offsetof宏很容易理解,但我不太明白在DEFINE宏中stringization和->的用法。你有什么想法吗?这会被展开成什么,或者如何成功编译这个宏?

1
[宏]标签是其中最不重要的...无法适应内联汇编。 - Antti Haapala -- Слава Україні
1
使用gcc -S命令对源文件进行编译,生成该文件的汇编代码。假设DEFINE只在汇编文件中有意义-这就是asm volatile的作用,以确保它们不会被移动... - Antti Haapala -- Слава Україні
1个回答

6

您遇到错误是因为该文件不应当被编译成对象。这个文件只是用来生成汇编输出,然后由此生成一个头文件asm_offsets.h。这个头文件会在其他地方被引用。您可以在include/generated目录下找到它。生成这个头文件的规则在顶层的KBuild文件中:

# Kbuild for top-level directory of the kernel
# This file takes care of the following:
# 1) Generate bounds.h
# 2) Generate timeconst.h
# 3) Generate asm-offsets.h (may need bounds.h and timeconst.h)
# 4) Check for missing system calls
# 5) Generate constants.py (may need bounds.h)

# Default sed regexp - multiline due to syntax constraints
define sed-y
        "/^->/{s:->#\(.*\):/* \1 */:; \
        s:^->\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 \2 /* \3 */:; \
        s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
        s:->::; p;}"
endef

你可以看到它使用了 sed 将汇编输出转换为头文件。临时的汇编文件可以在 arch/<arch>/kernel/asm-offsets.s 找到。示例行如下:
->pt_regs_bx $40 offsetof(struct pt_regs, bx)   #
->pt_regs_cx $88 offsetof(struct pt_regs, cx)   #
->pt_regs_dx $96 offsetof(struct pt_regs, dx)   #

请注意,这不是有效的汇编语法,但编译器并不关心这一点,它会毫不考虑地发出你在asm块中放置的任何内容,并进行参数代换。然后sed命令将其转换为与头文件中匹配的行,如下所示:
#define pt_regs_bx 40 /* offsetof(struct pt_regs, bx)   # */
#define pt_regs_cx 88 /* offsetof(struct pt_regs, cx)   # */
#define pt_regs_dx 96 /* offsetof(struct pt_regs, dx)   # */

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接