The MASM Forum Archive 2004 to 2012

General Forums => The Soap Box => Topic started by: qWord on March 21, 2012, 07:11:10 PM

Title: x64 code in 32 bit process
Post by: qWord on March 21, 2012, 07:11:10 PM
While googling, I found some very interesting site, which pointed out that you can call 64 bit code within a 32 Bit applications. This can be done by making a far call using a (undocumented) code selector, which is used by Windows to made transactions between 32 bit code and the  kernel.
Admittedly there is not much use of this, because all API's in the process are 32Bit, thus you can't made any API call from 64 bit code - but maybe it is useful for fooling debuggers  :bg

I find this very intersting, so I share the following example, which requires jwasm and polink. (tested on Win7, x64  :dance:)

regards, qWord
Title: Re: x64 code in 32 bit process
Post by: sinsi on March 22, 2012, 02:22:38 AM
Works in win8 consumer preview too.
Title: Re: x64 code in 32 bit process
Post by: Ficko on March 23, 2012, 11:56:37 AM
Just a question.

Did you used polink maybe for link it ?

I am getting "mix32_64.obj : fatal error LNK1190: invalid fixup found, type 0x0001".  :eek
Title: Re: x64 code in 32 bit process
Post by: qWord on March 23, 2012, 12:02:25 PM
Quote from: Ficko on March 23, 2012, 11:56:37 AMDid you used polink maybe for link it ?
yes
Title: Re: x64 code in 32 bit process
Post by: Ghostly on April 05, 2012, 03:23:15 AM
Reminds me of something I read about Linux a while back, apparently they have created a special ABI called x32 which allows programs to use 32-bit pointers while still being able to utilize the 64-bit extended registers from standard 64bit mode. Using 64-bit address references makes the code larger and thus fills up the cpu caches faster, so by using 32-bit adress references while still using the extra registers from 64-bit mode they were apparently able to get the same code to run ~15% faster in x32 abi mode compared to pure 64bit mode. Obviously this limits each program to using a maximum of 4gb addressable ram. Likely this is mostly targeted at the embedded Linux world but I still found it interesting.
Title: Re: x64 code in 32 bit process
Post by: qWord on April 05, 2012, 06:41:42 PM
Quote from: Ghostly on April 05, 2012, 03:23:15 AM
Reminds me of something I read about Linux a while back, apparently they have created a special ABI called x32 which allows programs to use 32-bit pointers while still being able to utilize the 64-bit extended registers from standard 64bit mode.
you can still use 32bit pointers in x64 by design: the pointers then are signed extend to 64 bits. For windows it is also no problem to address code using 32 Bit pointers, because common kernel- and user-code is placed in the range (2^31-1) to (-2^31).
Title: Re: x64 code in 32 bit process
Post by: jj2007 on April 06, 2012, 06:01:00 AM
Quote from: qWord on April 05, 2012, 06:41:42 PM
you can still use 32bit pointers in x64 by design: the pointers then are signed extend to 64 bits. For windows it is also no problem to address code using 32 Bit pointers, because common kernel- and user-code is placed in the range (2^31-1) to (-2^31).

Hey, you are calling Raymond Chen (http://blogs.msdn.com/b/oldnewthing/archive/2008/10/20/9006720.aspx) a liar :bg
Title: Re: x64 code in 32 bit process
Post by: sinsi on April 06, 2012, 06:33:35 AM
jj, "mov rax,[esi]" is still valid in long mode.
Title: Re: x64 code in 32 bit process
Post by: jj2007 on April 06, 2012, 08:19:03 AM
Sinsi,

I haven't yet started with 64-bit, not only because my OS is 32-bit, but one thing is for sure: What Raymond Chen writes will not contradict M$'s decision to drop support for older code, be it 16 or 32bit :bg
Title: Re: x64 code in 32 bit process
Post by: sinsi on April 06, 2012, 08:52:42 AM
I think Mr Chen is talking about two way communication, 32 to 64 is easy but the opposite is hard.
Title: Re: x64 code in 32 bit process
Post by: qWord on April 06, 2012, 11:02:41 AM
Quote from: jj2007 on April 06, 2012, 08:19:03 AMM$'s decision to drop support for older code, be it 16 or 32bit :bg
that's not their decision, because they follow the hadware requirements. Allowing 32 bit code to be execute in a 64 bit environment without any adaption would mean to kick the advantage of the larger address space.
You can use 32 Bit code as long as it's pointers only points inside your module (e.g. .data,.data?,...). For dynamic allocation and most API code an adaption to x64 is required...