0

I am trying to port a 32bit assembly function to 64bit. Its save_context and restore_restore. Basically porting from i386 to x86_64

So here is my porting

32bit version :

.align  4,0x90
.global save_context
.type   save_context,@function

save_context:
movl    4(%esp),%eax        

movl    %ebx, 12(%eax)      
movl    %esi, 16(%eax)      
movl    %edi, 20(%eax)      
movl    %ebp, 24(%eax)      
movl    %esp, 28(%eax)      

movl    0(%esp), %edx       
movl    %edx,  0(%eax)      

xorl    %eax,%eax       
incl    %eax
    ret

FUNCTION CALL IN MAIN :

u_int32_t context[10]; 

if (cpu_save_context(context) == TRUE) {
   // do something
}

gdb >> p context

$2 = {<some value>, 0, 0, <some value>, 0, 0, <some value>, <some value>, 0, 0}

64bit Version:

.align  4,0x90
.global save_context
.type   save_context,@function

cpu_save_context:

mov     %rdi,%rax               /* Get our context pointer from rdi */
                            /* Don't need to save A */
mov     %rbx, 24(%rax)      /* Save B */

mov     %r12, 32(%rax)      /* Save r12 */
mov     %r13, 40(%rax)      /* Save r13 (8*3+16)*/
mov     %r14, 48(%rax)      /* Save r13 */
mov     %r15, 56(%rax)      /* Save r13 */
mov     %rbp, 72(%rax)      /* Save frame pointer */
    mov     %rsp, 88(%rax)      /* Save stack pointer */

mov     8(%rbp), %r10       /* Fetch our return address */
mov     %r10,  8(%rax)      /* Save our return address */

xor    %rax,%rax            /* Construct return code of 1 */
inc     %rax

ret

FUNCTION CALL IN MAIN :

u_int64_t context[10];  // i have tried increasing 10->20 and 40//still save errors
if (cpu_save_context(context) == TRUE) {
     // do something
}

gdb >> p context

$2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

So the functions works for 32bit version, but for 64 bit no such luck. I have posted the similar question and they believe my porting of assembly code is fine. Segmentation fault in assembly code + C

Can someone help debugging this .

Community
  • 1
  • 1
Rags
  • 434
  • 2
  • 6
  • 20
  • IMHO you did not "port" the ABI (more specific the calling convention). In `x86` the call parameters are on the stack, but in `x86-64` in registers, different ones depending on OS. Take a look here: http://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI – rkhb May 20 '14 at 20:06
  • Sorry i dont get it .. Can you give me an example of an instruction and explain . Even after reading i see no errors in porting ?!?! – Rags May 20 '14 at 20:20
  • Also, `x86_64` has several more registers that need to be preserved... – twalberg May 20 '14 at 20:23
  • What does `mov 8(%rsp),%rax` do? There is no parameter on the stack! The address of `context` is passed presumably in `RDI`. I wonder why you don't get a segmentation fault. – rkhb May 20 '14 at 20:39
  • I have made changes to new 64bit porting. Can you verify that . Would be really helpful !!! – Rags May 20 '14 at 22:08
  • @rkhb : Got the new assembly code. Can you have a look – Rags May 27 '14 at 14:41

0 Answers0