red-zone

Why do we need stack allocation when we have a red zone?

南楼画角 提交于 2019-11-28 03:49:25
问题 I have the following doubts: As we know System V x86-64 ABI gives us about a fixed-size area (128 bytes) in the stack frame, so called redzone. So, as a result we don't need to use, for example, sub rsp, 12 . Just make mov [rsp-12], X and that's all. But I cannot grasp idea of that. Why does it matter? Is it necessary to sub rsp, 12 without redzone? After all, stack size is limited at the beginning so why sub rsp, 12 is important? I know that it makes possible us to follow the top of the

Why does the compiler reserve a little stack space but not the whole array size?

浪尽此生 提交于 2019-11-27 07:31:18
问题 The following code int main() { int arr[120]; return arr[0]; } Compiles into this: sub rsp, 360 mov eax, DWORD PTR [rsp-480] add rsp, 360 ret Knowing the ints are 4 bytes and the array is size 120, the array should take 480 bytes, but only 360 bytes are subtracted from ESP... Why is this? 回答1: Below the stack area used by a function, there is a 128-byte red zone that is reserved for program use. Since main calls no other function, it has no need to move the stack pointer by more than it needs

Where exactly is the red zone on x86-64?

血红的双手。 提交于 2019-11-27 06:35:45
问题 From Wikipedia: In computing, a red zone is a fixed-size area in a function's stack frame beyond the return address which is not preserved by that function. The callee function may use the red zone for storing local variables without the extra overhead of modifying the stack pointer. This region of memory is not to be modified by interrupt/exception/signal handlers. The x86-64 ABI used by System V mandates a 128-byte red zone, which begins directly after the return address and includes the

Why can't kernel code use a Red Zone

笑着哭i 提交于 2019-11-27 05:01:39
It is highly recommended when creating a 64-bit kernel (for x86_64 platform), to instruct the compiler not to use the 128-byte Red Zone that the user-space ABI does. (For GCC the compiler flag is -mno-red-zone ). The kernel would not be interrupt-safe if it is enabled. But why is that? Quoting from the AMD64 ABI: The 128-byte area beyond the location pointed to by %rsp is considered to be reserved and shall not be modified by signal or interrupt handlers. Therefore, functions may use this area for temporary data that is not needed across function calls. In particular, leaf functions may use

Inline assembly that clobbers the red zone

三世轮回 提交于 2019-11-26 20:52:34
问题 I'm writing a cryptography program, and the core (a wide multiply routine) is written in x86-64 assembly, both for speed and because it extensively uses instructions like adc that are not easily accessible from C. I don't want to inline this function, because it's big and it's called several times in the inner loop. Ideally I would also like to define a custom calling convention for this function, because internally it uses all the registers (except rsp ), doesn't clobber its arguments, and

Why can't kernel code use a Red Zone

江枫思渺然 提交于 2019-11-26 09:53:28
问题 It is highly recommended when creating a 64-bit kernel (for x86_64 platform), to instruct the compiler not to use the 128-byte Red Zone that the user-space ABI does. (For GCC the compiler flag is -mno-red-zone ). The kernel would not be interrupt-safe if it is enabled. But why is that? 回答1: Quoting from the AMD64 ABI: The 128-byte area beyond the location pointed to by %rsp is considered to be reserved and shall not be modified by signal or interrupt handlers. Therefore, functions may use

Using base pointer register in C++ inline asm

≯℡__Kan透↙ 提交于 2019-11-25 22:38:35
问题 I want to be able to use the base pointer register ( %rbp ) within inline asm. A toy example of this is like so: void Foo(int &x) { asm volatile (\"pushq %%rbp;\" // \'prologue\' \"movq %%rsp, %%rbp;\" // \'prologue\' \"subq $12, %%rsp;\" // make room \"movl $5, -12(%%rbp);\" // some asm instruction \"movq %%rbp, %%rsp;\" // \'epilogue\' \"popq %%rbp;\" // \'epilogue\' : : : ); x = 5; } int main() { int x; Foo(x); return 0; } I hoped that, since I am using the usual prologue/epilogue function