Easy way to use Amd64 commands under 32-bit Windows?

For some processor-bound code using 64-bit variables, it is useful to use the Amd64 instruction set rather than x86. How can this be done on 32-bit Windows (for example, Windows XP SP3)? Of course, I assume a modern processor with support for Amd64. I exclude a working but heavyweight method: running a full-blown 64-bit OS as a virtual machine, for example. Ubuntu for Amd64 under Virtualbox.

I understand that assembly is necessary, and there are limitations, in particular, for more memory than for 32-bit Windows. But I think of purely computational tasks that require only a moderate amount of memory and do not call external functions.

+6
source share
2 answers

It is not possible to use Amd64 instructions (long mode) on a 32-bit general-purpose operating system (without modifying the kernel / special drivers / hypervisors).

This is because:

1) to use your own 64-bit instructions, you need to switch to long mode. This is a privileged action. The 32-bit kernel of the operating system cannot continue to work if the CPU switches to 64-bit mode, so you must switch back before entering the kernel

2) But the kernel is often called asynchronously, for a timer (scheduler) and other hardware interrupts (drivers). It will not save 64-bit registers and does not change the mode from long to protected.

Maybe you can write a special driver that will perform 64-bit tasks in a 32-bit OS, but such a driver is more like a 64-bit kernel and a dynamic kernel patcher. I do not know such a solution.

You can use MMX, SSE, SSE2, SSE3, AVX to access 64-bit ALUs and registers of your processor when working in 32-bit OS.

I can say that Linux, some BSD, Mac OS X have a mode when the 64-bit kernel is used, but the user space software is 32-bit. In this case, it will be possible to run both 32-bit and 64-bit applications, because the kernel knows about 64-bit mode and can access 64-bit registers to switch tasks. As far as I know, MS Windows does not have such a mode (W7 emulates 32-bit mode, but this is called by my MS as a simulator, so I assume that this is not a built-in function).

Another possibility (better, your processor supports hardware virtualization), is to use a 64-bit hypervisor (VMware / Xen, other high-end solutions) with 32-bit and 64-bit guest OSs. VirtualBox is another use case for the hypervisor, and it can be used freely.

+9
source

In general, running 64-bit code in the 32-bit kernel of the OS will be practically impossible for the following reasons:

  • The 32-bit OS is not aware of the additional 64-bit registers (and the upper 32-bit of the existing registers) and will not save them in all task switches.
  • A 32-bit OS is not ready to execute 64-bit code. Enabling the execution of 64-bit code means switching to IA-32e paging (which requires a completely different page table format) and setting CS.L = 1 and CS.D = 0 in the code segment descriptor in GDT (or LDT). (See IA-32 Manuals, vol 3a / 3b 5.2.1)

In principle, you can get around both problems by writing a new HAL for Windows, which works in IA-32e mode, and switches to the 64-bit trampoline code segment to save and restore 64-bit registers. This is a rather difficult task; Take a look at the Windows DDK for details. You can also use the emulation approach, as VirtualBox and friends do, if your processor supports VMX. But it would be easier to just use a 64-bit OS from the start.

+1
source

Source: https://habr.com/ru/post/895495/


All Articles