x86_64的对齐栈和恢复,而不保存寄存器 [英] x86_64 align stack and recover without saving registers
问题描述
我写的中断处理例程x86_64的。在ABI指定调用C函数之前,我必须对齐堆栈16个字节。 64位x86 ISA规定,在进入中断服务程序,我堆栈8字节对齐。我需要调整我的堆栈指针为16个字节,因此。问题是,从我的C函数返回时,我必须恢复(潜在的)未对齐堆栈指针,这样我可以从我的中断正常返回。
I'm writing interrupt handling routines for x86_64. The ABI specifies that before calling a C function I must align the stack to 16 bytes. The x86_64 ISA specifies that on entry to an ISR, my stack is 8 byte aligned. I need to align my stack pointer to 16 bytes therefore. The issue is that on return from my C function, I must recover the (potentially) unaligned stack pointer so that I can return from my interrupt correctly.
我不知道是否有一种方法可以做到这一点,而无需使用通用寄存器?
I wonder if there is a way to do this without using a general purpose register?
推荐答案
下面是我的解决问题的办法是放:
Here's my solution to the question as put:
pushq %rsp
pushq (%rsp)
andq $-0x10, %rsp
call function
movl 8(%rsp), %rsp
这两个推离开堆栈的相同路线它原本,和原来的%RSP
的副本(%RSP)
和 8(%RSP)
。该和Q
然后对齐堆栈 - 如果它已经是16字节对齐没有什么变化,如果是8字节对齐然后减去8从%RSP
,这意味着原来的%RSP
正处在 8(%RSP)
和 16(%RSP)
。因此,我们可以无条件地从恢复 8(%RSP)
。
The two pushes leave the stack with the same alignment it had originally, and a copy of the original %rsp
at (%rsp)
and 8(%rsp)
. The andq
then aligns the stack - if it was already 16 byte aligned nothing changes, if it was 8 byte aligned then it subtracts 8 from %rsp
, meaning that the original %rsp
is now at 8(%rsp)
and 16(%rsp)
. So we can unconditionally restore it from 8(%rsp)
.
这篇关于x86_64的对齐栈和恢复,而不保存寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!