본문 바로가기
코드/64bit

X64 Calling

by WeZZ 2010. 3. 26.

Computers with 64 bit processors are becoming popular, at least in enterprise circles. Also the X64 version of Windows Vista is more popular than Windows XP 64 bit edition.

 

2 Types of 64 bit architecture


Yes, unlike 32 bit (aka X32) there are 2 64 bit architectures:

  1. AMD's X64 ; also known as X32-64 & AMD64
  2. Intel's IA-64; also known as Itanium
The subject of this post is AMD's 64 bit chip, which is commonly referred to as X64

So, whats the difference ?


The X64 architecture is a super set of  X32 architecture :

  • 64 bit versions of the the existing 32 bit registers
    • So X32's 32 bit registers EAX, EBX, ECX etc becomes 64 bit RAX, RBX, RCX etc in X64
  • 8 new 64 bit general purpose registers (R8, R9...R15)
  • 8 new 128 bit XMM registers
To know more about the architecture goto http://www.amd.com/us-en/Processors/DevelopWithAMD/0,,30_2252_875_7044,00.html

And how is programing different ?


A lot has been written about porting existing 32 bit code to 64 bit. A lot of these deals with change in sizeof pointers (from 32 bit to 64 bit) and change in the sizeof some of the basic data types (this depends on the compiler which you are using)

Links:

Porting device drivers to AMD64
[http://www.amd.com/us-en/assets/content_type/DownloadableAssets/dwamd_Porting_Win_DD_to_AMD64_Sept24.pdf]

64 bit driver guidelines
[http://www.microsoft.com/whdc/driver/64bitguide.mspx]

20 issues of porting C++ code on the 64-bit platform
[http://www.viva64.com/articles/20_issues_of_porting_C++_code_on_the_64-bit_platform.html]

..assembly programming ?


Not many program in assembly languages these days. But if it occurs to you there are a few things to keep in mind.

Source Link : http://www.quequero.org/X64_Assembly

Win32 on X32 provided us with many calling conventions (function calling conventions : fastcall, stdcall etc). In X64 there is no choice. There is only one calling convention:

The first parameter is the rcx register, the second one rdx, the third r8 and the fourth r9. Saying that the parameters registers are part of the stack frame, makes it also clear that any function that calls another child function has to initialize the stack providing space for these four registers, even if the parameters passed to the child function are less than four.
The initialization of the stack pointer is done only in the prologue of a function, it has to be large enough to hold all the arguments passed to child functions and it's always a duty of the caller to clean the stack. Now, the most important thing to understand how the space is provided in the stack frame is that the stack has to be 16-byte aligned.
In fact, the return address has to be aligned to 16 bytes. So, the stack space will always be something like 16n + 8, where n depends on the number of parameters. Here's a small figure of a stack frame:

Stack Parameters (5th param onwards)
Register Parameters (Space for 4 Reg params)
Return IP address (RIP)
Local Variables of the function


If you see the disassembly of a 64 bit program, you can see that the stack pointer (RSP) is not messed with throughout the function body. Necessary stack is reserverd ( Sub RSP, 0x[ReqSize] ) in the function prolog.

Another important thing to note is that even though the first 4 parameters are passed via registerd (RCX, RDX, R8 and R9) they must be given scratch storage space in the stack (Register Parameters in the above figure/call stack).

So while porting asm from 32bit to 64bit, if you have a void routine

call MyRoutine

must be changed as:

sub rsp, 20h      ; Reserve space for register parameters

call MyRoutine

add rsp, 20h


Another difference found was in X64 the luxury of PUSHA/PUSHD POPA/POPD (Push/Pop all registers and flags) is not available.

Footnotes

  • MS Visual Studio 2005 lets you build 64 bit applications. The 64 bit compiler modules are not included in the installation by default.
  • VS 2005 has options to turn on 64bit compile warning (probable errors) for your 32 bit code; so you can check whether your 32bit code is 64bit ready
  • Windows Server 2003 DDK and above comes with MASM64 for writing asm modules.
  • 32 bit applications can run over 64 bit windows (WOW64 http://en.wikipedia.org/wiki/WOW64)
  • 32 bit drivers cannot be used to 64 bit windows.



출처 : http://geekswithblogs.net/kernelmode/archive/2008/03/06/120337.aspx