I hope I am posting in a the correct spot. We are trying to work with the ChibiOS POSIX port and trying to get it translated to be 64 bit compatible. But we are having trouble understanding the assembly code used to perform the context switch in chcore.c. Would any one be able to help us understand this code better?
So far, our thoughts are as follows.
In the GCC fastcall calling convention the first two parameters are passed in the ECX and EDX registers. In this function those represent the thread pointers ntp and otp respectively. The code in this case saves all the callee save registers (EBP, EBX, EDI and ESI) by pushing them to the stack, then it saves the stack pointer by moving it to the old thread struc (otp, EDX). The new thread struct (ntp, ECX) is moved to the stack pointer and its context is loaded from the new stack by popping the callee save registers off the stack.12(%EDX) in this case refers to the forth element of the old thread struct, which is a struct containing the stack of the old context. 12(%ECX) refers to the stack used for the new context.
The calling convention for x86-64 has the parameters passed in RDI, RSI, RDX, RCX, R8, R9 and XMM0-7. The callee save registers are RBP, RBX, and R12-R15. I believe
movl %esp, 12(%edx)
needs to be replaced by
movl %rsp, 24(%rsi)
and
movl 12(%ecx), %esp
by
movl 24(%rdi), %rsp.
The change from 12 off the address to 24 off the address is to account for the fact that the struct will be aligned on 8 bytes instead of 4, because pointers are 8 bytes in x86-64.The pushing and popping should also be changed to pushing and popping RBP, R12-R15, and RBX instead of EBP, ESI, EDI and EBX.
It would be great if some one could point to us if we are on the right track or give any kind of insight!