gpt4 book ai didi

linux - Linux arm64 如何在 AArch32 和 AArch64 之间切换

转载 作者:行者123 更新时间:2023-12-03 09:58:37 27 4
gpt4 key购买 nike

Linux 支持运行 32 位应用程序,只要

  • 内核启用 CONFIG_COMPAT
  • 硬件支持 AArch32

  • 我假设 32 位应用程序必须在 arm AArch32 执行状态下运行,并且环境是否有 32 位应用程序和 64 位应用程序。

    32位应用进程->arm状态为AArch32

    64 位应用进程和内核 -> arm 状态为 AArch64

    这是正确的吗?

    如果是这样的话,
    Linux 如何处理 AArch32 和 AArch64 开关?
    内核是否知道正在运行的进程是 32 位还是 64 位?

    最佳答案

    链接 https://community.arm.com/developer/ip-products/processors/f/cortex-a-forum/6706/in-aarch32-state-what-is-the-mechanism-to-switch-to-aarch64-in-software发表在评论中 0andriy ( kernel developer ) 解释了 Martin Weidmann 在 AArch32 用户空间进程和 AArch64 linux 内核之间切换. 32->64 模式切换是在异常情况下完成的;并且 64->32 切换在异常返回时完成。

    If you are currently running one of the 32-bit apps and you take an exception (e.g. IRQ, SVC from a system call, abort from a page fault,....) you enter the 64-bit OS. So a AArch32 --> AArch64 transition. When the OS performs an exception return back into the app, that's an AArch64-->AArch32 transition. ... Any exception type in AArch32 state could potentially lead to Execution state changing to AArch64. ... For exceptions returns the reverse is true. An exception return in AArch64 might cause execution state to change to AArch32.
    For both exceptions and exception returns, a change of Execution state can only occur if there is also a change in EL. That is an exception from EL0 to EL1 could lead to a change in Execution state. But an exception from EL1 to EL1 could not.



    https://community.arm.com/developer/ip-products/processors/f/cortex-a-forum/6706/in-aarch32-state-what-is-the-mechanism-to-switch-to-aarch64-in-software线程有一些更多的细节。或者在 https://medium.com/@om.nara/aarch64-exception-levels-60d3a74280e6 中有更简单的解释在“在 AArch32 和 AArch64 之间移动”

    On taking an exception, if the Exception level changes, the Execution state can: Remain unchanged, OR Change from AArch32 to AArch64.

    On returning from an exception, if the Exception level changes, the Execution state can: Remain unchanged, OR Change from AArch64 to AArch32.



    同样在 https://events.static.linuxfound.org/images/stories/pdf/lcna_co2012_marinas.pdf演示文稿(幻灯片 5)或在 https://developer.arm.com/architectures/learn-the-architecture/exception-model/execution-and-security-states或在 https://www.realworldtech.com/arm64/2/ :

    AArch64 Exception Model

    • Privilege levels: EL3 – highest, EL0 – lowest
    • Transition to higher levels via exceptions

    • Register width cannot be higher in lower levels

      • E.g. no 64-bit EL0 with 32-bit EL1
    • Transition between AArch32 and AArch64 via exceptions
      • AArch32/AArch64 interworking not possible


    现在回答您的问题:

    how does the Linux handle the AArch32 and AArch64 switch?



    通过使用具有不同 PSTATE 值的 EL0/EL1 开关的异常处理(和返回)硬件功能。

    Does the kernel know the running process is 32-bit or 64-bit?



    是的,在 64 位内核(兼容系统调用)上检查 32 位进程(“任务”)的内核: arch/arm64/kernel/syscall.c
    static long do_ni_syscall(struct pt_regs *regs, int scno)
    {
    #ifdef CONFIG_COMPAT
    long ret;
    if (is_compat_task()) {
    ret = compat_arm_syscall(regs, scno);
    if (ret != -ENOSYS)
    return ret;
    }
    #endif

    return sys_ni_syscall();
    }

    测试在 include/asm/compat.h 中定义和 arch/arm64/include/asm/thread_info.h作为
    #define TIF_32BIT       22  /* 32bit process */
    static inline int is_compat_task(void)
    {
    return test_thread_flag(TIF_32BIT);
    }

    TIF_32BIT 由 fs/compat_binfmt_elf.c 中的 32 位 elf 加载设置,带有几个个性宏:

    https://elixir.bootlin.com/linux/v4.19.107/source/arch/arm64/include/asm/elf.h#L208
    /*
    * Unlike the native SET_PERSONALITY macro, the compat version maintains
    * READ_IMPLIES_EXEC across an execve() since this is the behaviour on
    * arch/arm/.
    */
    #define COMPAT_SET_PERSONALITY(ex) \
    ({ \
    set_thread_flag(TIF_32BIT); \
    })

    https://elixir.bootlin.com/linux/v4.19.107/source/fs/compat_binfmt_elf.c#L104
     #define    SET_PERSONALITY     COMPAT_SET_PERSONALITY

    https://elixir.bootlin.com/linux/v4.19.107/source/fs/binfmt_elf.c#L690
    #define SET_PERSONALITY2(ex, state) \
    SET_PERSONALITY(ex)
    static int load_elf_binary(struct linux_binprm *bprm)
    SET_PERSONALITY2(loc->elf_ex, &arch_state);

    关于linux - Linux arm64 如何在 AArch32 和 AArch64 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60220759/

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