Saturday, December 5, 2009

Porting 32 bit appications to 64 bit in Linux

We when talk about 64bit we have two platforms amd64 and IA64. Here we are discussing only about amd64 , well known as x86_64. amd64 is the 64 bit extension of x86 developed initially by AMD, and now intel also produces it. IA64 is also known as Itanium architecture, is an entirely new architecture developed jointly by HP and Intel, theses are the processors used in HP's Integrity range of server.

In x86_64 Linux systems LP64 is the data model used and ILP32 is the model used in x86 systems. The following table will give you the difference,

Datatype
LP64
ILP64
LLP64
ILP32
char
8
8
8
8
short
16
16
16
16
int
32
64
32
32
long
64
64
32
32
long long
64
64
64
64
pointer
64
64
64
32

In 32bit and 64bit Linux systems, we are concerned only with ILP32 and LP64. long and pointer are the only difference between the two data models, so when we port applications from 32bit to 64 bit we must be concerned about part of the code which uses  long and pointer data types. If we can uses int data type instead of long then it is very easy to port applications from 32 bit to 64 bit.

Now we covered the higher level languages part, let's move on to assembly language.


The above table is will guide you to write instructions for GAS (GNU Assembler),  say push rax(64bit extension of eax register in x86) register, then the instruction is pushq %rax.

The register sets on a x86_64 platform is given below.




In x86_64, sixteen general purpose registers are available, i.e. rax, rbx, rcx, rdx, rdi, rsi, rbp, rsp and 8 additional registers r8-r15.

In x86 architecture we have segmentation, so we have difference between far call and near call.



Even though we have segmentation, we are using paging on top of flat memory model in x86 linux. By seeing this, in x86_64 we don't have segmentation, so linux uses only paging in linux. Since segmentation is not available in x86_64 platform, far call and near call are same, that means no need to push the CS segment register into the stack. One more thing to see is alignment in x86_64 platform, read more on alignment here.

x86_64 has different method of passing arguments during function calls, x86_64 uses mainly GP registers for passing arguments, in x86 we use stack for passing arguments.



x86_64 application binary interface is here. Best reference is AMD64 architecture Programmer's Manuals and Intel architecture system Programmer's Manuals.

Thursday, November 26, 2009

Blogging in First

I am starting this technical blog, hoping I can share something with people out there.