Author Topic: Linux 64Bit Proc/Prologue question  (Read 11471 times)

Offline Gunner

  • Jr. Member
  • *
  • Posts: 74
  • Country: us
    • Gunners Software
Linux 64Bit Proc/Prologue question
« on: December 27, 2013, 08:49:19 PM »
I am going over to the 64 Bit world and am confused about the prologue.  The following works without issue, but is it "correct"?  What is this "Red Zone"?  I am confused about that.

Code: [Select]
SetIPInfoState:
    push    r13
   
    mov     rdi, [oIPCheck]
    call    gtk_toggle_button_get_active
    mov     r13, rax 
   
    mov     rsi, rax
    mov     rdi, [oIPInfoDB]
    call    gtk_widget_set_sensitive   
   
    mov     rsi, r13
    mov     rdi, [oFlag]
    call    gtk_widget_set_visible
   
    pop     r13
    ret

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Linux 64Bit Proc/Prologue question
« Reply #1 on: December 28, 2013, 05:38:39 PM »
What is this "Red Zone"?  I am confused about that.

Hi Gunner,

At the bottom of page 16 in the document http://www.x86-64.org/documentation_folder/abi-0.99.pdf you'll find your answer.

Basically, it's just 128 bytes of stack space following any local stack variables that the OS won't touch during a signal or interrupt handler so you're free to use it as additional local stack space, ie: [ rsp - X ] where X can be up to 128 bytes.

You can make use of it to eliminate the function prologue and epilogue if your leaf function uses less than that amount.

Thus, for example, you would no longer need these lines:
Code: [Select]
push rbp
mov rbp, rsp    ; frame reference
sub rsp, 128    ; set up local stack space
.
.  ; your code
.
mov rsp, rbp
pop rbp

It should result in a nice reduction in overall code size as well as improvement in execution speed for leaf functions.

Edit for further info:
Yes, your function is correct.  The push r13 properly aligns the stack and you make no use of local stack space.  Obviously this would fail if you made use of the redzone for local variable storage and subsequently made additional functions calls since doing so would overwrite your stack space.  That's why this only works for leaf functions - those that make no additional function calls.
« Last Edit: December 28, 2013, 05:55:22 PM by Rob Neff »