" #sym " %0 " #val : :-6ren">
gpt4 book ai didi

c - 试图从 linux 内核理解一段内联臂程序集

转载 作者:行者123 更新时间:2023-12-03 21:54:01 30 4
gpt4 key购买 nike

我试图了解这段内联汇编的作用,它是从 C DEFINE 生成的。宏:

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

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

它被用于做 AArch64 的一些魔法 smc电话在 linux-imx内核叉。上面C宏用来定义值的地方是 here :
// 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));
(...)
}

后来,这些定义用于汇编宏, available here (或者至少看起来是这样):
// 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宏。

任何想法它扩展到什么,或者如何成功编译它?

最佳答案

您会收到错误,因为该文件不应编译为对象。它仅用于生成汇编输出,然后对其进行处理以创建标题 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) # */

关于c - 试图从 linux 内核理解一段内联臂程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54926236/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com