ARMV8 Linux上下文开关

发布于 2025-02-05 08:44:55 字数 981 浏览 4 评论 0原文

我正在研究以下ARMV8上的Linux上下文开关。

代码

ENTRY(cpu_switch_to)
    mov x10, #THREAD_CPU_CONTEXT
    add x8, x0, x10
    mov x9, sp
    stp x19, x20, [x8], #16     // store callee-saved registers
    stp x21, x22, [x8], #16
    stp x23, x24, [x8], #16
    stp x25, x26, [x8], #16
    stp x27, x28, [x8], #16
    stp x29, x9, [x8], #16
    str lr, [x8]
    add x8, x1, x10
    ldp x19, x20, [x8], #16     // restore callee-saved registers
    ldp x21, x22, [x8], #16
    ldp x23, x24, [x8], #16
    ldp x25, x26, [x8], #16
    ldp x27, x28, [x8], #16
    ldp x29, x9, [x8], #16
    ldr lr, [x8]
    mov sp, x9
    msr sp_el0, x1
    ret
ENDPROC(cpu_switch_to)

问题1: 仅Callee寄存器(X19〜X29,链路寄存器,SP)就足以容纳上下文开关。为什么其余的寄存器(x0〜x18)不参与使用堆栈的强度和恢复上下文? 任务上下文是功能序列。那么,Callee寄存器足以用于上下文开关吗?

问题2: PC(程序计数器)寄存器不参与使用堆栈的强大和恢复上下文。这是因为当此Callee功能返回时,PC已恢复?当时,链接寄存器已复制到PC中?

问题3: PSTATE寄存器不参与使用堆栈的强大和恢复上下文。有什么理由这样做吗?我认为任务上下文应包含PSTATE寄存器。

如果有人回答我的问题。我会很感激。

I am studying about Linux Context Switch on the ARMv8

Below is the codes

ENTRY(cpu_switch_to)
    mov x10, #THREAD_CPU_CONTEXT
    add x8, x0, x10
    mov x9, sp
    stp x19, x20, [x8], #16     // store callee-saved registers
    stp x21, x22, [x8], #16
    stp x23, x24, [x8], #16
    stp x25, x26, [x8], #16
    stp x27, x28, [x8], #16
    stp x29, x9, [x8], #16
    str lr, [x8]
    add x8, x1, x10
    ldp x19, x20, [x8], #16     // restore callee-saved registers
    ldp x21, x22, [x8], #16
    ldp x23, x24, [x8], #16
    ldp x25, x26, [x8], #16
    ldp x27, x28, [x8], #16
    ldp x29, x9, [x8], #16
    ldr lr, [x8]
    mov sp, x9
    msr sp_el0, x1
    ret
ENDPROC(cpu_switch_to)

Question 1:
Just Callee Registers (X19 ~ X29, Link Register, SP) are enough for Context Switch. Why the rest of registers (X0 ~ X18) are not involved in Strong and Restoring of context using stack?
The task context is kind of sequence of function. So, Callee Registers are enough for context switch?

Question 2:
PC (Program Counter) Register is not involved in Strong and Restoring of context using stack. This is because the pc is restored when this callee function has return? At that time link register is copied into PC?

Question 3:
PSTATE Register is not involved in Strong and Restoring of context using stack. Is there any reason to do like this? I think that task context should contain PSTATE Register.

If somebody answers my question. I would be grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2025-02-12 08:44:56

并非只有某些寄存器保存在上下文开关中。这是因为可能出于多种原因(包括页面故障)发生上下文开关,显然,如果您访问内存的任何时间,某些寄存器可能会丢失。

通常,寄存器和其他状态在进入中断例程后将存储在堆栈中,并在出口时恢复。这是与您提到的不同的代码,这是一个内部线程开关。通常,任务切换所涉及的功能称为irq_handler,因为上下文开关通常是由中断引起的,并且在ARM64上,我相信返回用户空间的代码称为asm_exit_to_to_user_mode之类的东西

您可以阅读 armv8文档/a>了解更多。

It isn't the case that only some registers are saved in a context switch. That's because a context switch may occur for any number of reasons, including a page fault, and obviously it would be unsuitable if any time you accessed memory some of your registers could be lost.

Typically the registers and other state are stored on the stack upon entry to the interrupt routine and restored on exit. This is a different piece of code from what you've mentioned, which is an internal thread switch. Typically the functions involved in task switching are called something like irq_handler because a context switch is often caused by an interrupt, and on ARM64, I believe the code to return to userspace is called something like asm_exit_to_user_mode.

You can read the ARMv8 documentation on context switches to learn more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文