News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

AVX and AVX2 for DOS applications

Started by Gunther, April 04, 2015, 11:39:04 AM

Previous topic - Next topic

Gunther

Well, okay it follows a bit DOS system programming. So I'll explain a few things here in detail. We had in the past some interesting discussions about using the new AVX and AVX2 instructions under Win-32, for example here and here. The intermediate result was, that an application can use AVX, if an appropriate CPU is available (Sandy Bridge, Ivy Bridge, Haswell, Bulldozer, Piledriver, Jaguar, Steamroller) and the Operating System (Windows 7 with SP1 and above) must support the new instruction set. For example, there's no chance to use AVX with Windows XP.

And now DOS? Yes, there is good news: It is possible to use AVX and AVX2 with a DOS application. After a long discussion with some old friends in Geneva (CERN) plus reading the appropriate Intel manuals again and again, here is my solution.

First of all, plain DOS (without fancy Multitaskers like JEMM, EMM386 and the like) is a single tasking Operating System. No task switches and context changes are necessary. That's the chance. There are 4 steps to do and all applications in a current DOS session can use the new instructions.

  • Verify the processor supports the XSAVE/XRSTOR/XSETBV/XGETBV instructions.
  • Verify the processor supports YMM state. Verify also the SSE support, because the lower 128-bits of an YMM register are aliased to an XMM register.
  • Set Bit 18 of the CR4 register (privileged instruction) to enable the use of XSETBV/XGETBV instructions.
  • Supply an appropriate mask via EDX:EAX to execute XSETBV (privileged instruction) : Bit 0 = save FPU, Bit 1 = save XMM, Bit 2 = save YMM (EDX = 0h, EAX = 7h)
The tricky part is step 4. The Intel manual says:
Quote
This instruction must be executed at privilege level 0 or in real-address mode; otherwise, a general protection exception #GP(0) is generated.
So, before we can make the described 4 steps above, we have to check 2 things:

  • Is the program running inside the Windows DOS emulation? If so, terminate right away, because nothing is necessary. Either Windows supports the commands or not. Furthermore, we're a ring 3 application.
  • Is the program running under control of a Multitasker? If so, terminate right away, because a GPF would be the final result.
The attached archive avx.zip (sources included) contains the program avx.com which checks these 2 things and after that makes the 4 steps, described above. Therefore your CONFIG.SYS file should look similar like this (plain DOS):
Quote
DEVICE=C:\DOS\HIMEM.SYS
DEVICE=C:\HXCD-ROM\CDROM.SYS /D:MSCD000
LASTDRIVE=Z
FILES=40
BUFFERS=10
You can find some hints for preparing a bootable USB stick here.

I've included the application cpu.exe from this thread into the archive for testing purposes. The application avx.com will run under plain DOS and as virtual machine under VMWare. It doesn't work under VirtualBox, because they are emulating only an old CPU (SSE3) for the DOS VM.

You can test it so. Start cpu.exe first. You should see the following screen:
Quote
      Supported Features
      ==================

Vendor String: GenuineIntel
Brand  String: Intel(R)Core(TM)i7-3770CPU@3.40GHz

      Instruction Sets
      ----------------

MMX  SSE  SSE2  SSE3  SSSE3  SSE4.1  SSE4.2

      Supported Special Instructions
      ------------------------------

Conditional Moves
FXSAVE and FXSTOR
XSAVE and XSTOR for processor extended state management.
POPCNT
RDRAND
AES (Advanced Encryption Standard) Instruction Set
16-bit floating-point Conversion Instructions

Please, press enter to end the application ...

Start now avx.com:
Quote
This program checks the usage of the AVX instruction
set for a plain DOS application.

Please press enter to continue ...

AVX is supported by the CPU.
Extended State Management is supported by the CPU.

Program ends now!

After a new start of cpu.exe, you should see the following screen:
Quote
      Supported Features
      ==================

Vendor String: GenuineIntel
Brand  String: Intel(R)Core(TM)i7-3770CPU@3.40GHz

      Instruction Sets
      ----------------

MMX  SSE  SSE2  SSE3  SSSE3  SSE4.1  SSE4.2 AVX

      Supported Special Instructions
      ------------------------------

Conditional Moves
FXSAVE and FXSTOR
XSAVE and XSTOR for processor extended state management.
POPCNT
RDRAND
AES (Advanced Encryption Standard) Instruction Set
16-bit floating-point Conversion Instructions

Please, press enter to end the application ...

Since jWasm for DOS isn't up to date, I've written the sources for YASM (NASM should also do the job).

Some test results under other configurations would be fine.

Gunther
You have to know the facts before you can distort them.

sinsi

Booting to MS-DOS 7, plain DOS - no config.sys or autoexec.bat, second run of cpu.exe after avx.exe

Supported Features
==================

Vendor String: AuthenticAMD
Brand  String: AMDA10-7850KAPUwithRadeon(TM)R7Graphics

Instruction Sets
----------------

MMX  SSE  SSE2  SSE3  SSSE3  SSE4.1  SSE4.2  AVX

Supported Special Instructions
------------------------------

Conditional Moves
FXSAVE and FXSTOR
XSAVE and XSTOR for processor extended state management.
POPCNT
AES (Advanced Encryption Standard) Instruction Set
FMA (Fused Multiply Add) extensions using YMM state.
16-bit floating-point Conversion Instructions

Please, press enter to end the application ...
🍺🍺🍺

Gunther

Thank you sinsi.  :t A config.sys with HIMEM.SYS will work, too (that's tested). You can start it also inside VMWare (that's tested, too).

Gunther
You have to know the facts before you can distort them.

nidud

#3
deleted

Gunther

Hi nidud,

Quote from: nidud on April 05, 2015, 03:33:30 AM
The CheckWindows functions works in Windows but not in DOSBox or Oracle VM.

Testing the true version for 0005h may indicate DOSBox, but on other VM's this may be a problem.

thank you for your hint. But at the moment it's not a problem. DOSBox emulates an 80486; there's no chance to use AVX. VirtualBox (Oracle) emulates only a CPU with SSE3, therefore no chance to use AVX.

Gunther
You have to know the facts before you can distort them.

Gunther

Hi nidud,

only some supplements. It is not the question to check, if we're running under Windows. For example, avx.com runs fine as virtual machine under VMware Player with Windows 7-64 as Host and FreeDOS as guest operating system. It seems possible that it'll run in the future under VirtualBox. But at the moment emulates VirtualBox only a CPU with maximal SSE3 support. The same would be true for future versions of DOSBox.

But it would be problematic if the program runs - let's say - under Windows 7-32 inside cmd.exe. That's the point. For that case, CheckWindows makes the job and that's good enough.

Gunther
You have to know the facts before you can distort them.

nidud

#6
deleted

Gunther

Hi nidud,

Quote from: nidud on April 05, 2015, 08:23:44 PM
Yes, the programs works well emulated where the CPU test will handle this issue, so it may not be that important.

I use this test in the DOS version of the file manager for the idle functions, but this only works in Windows.

did you test avx.com under DOs or inside a VM?

Gunther
You have to know the facts before you can distort them.

nidud

#8
deleted

Gunther

Quote from: nidud on April 06, 2015, 10:26:11 AM
I tested AVX.COM and CPU.EXE in XP, DOSBox, and VirtualBox.

They work well on all of these, but the CheckWindows() only works in XP. SSE3 was detected in VirtualBox (CPU.EXE) and AVX.COM exited with the "Program runs under V86 Mode and terminates!" message.

No test was done in real DOS in this case.

By using FreeDOS, avx.com will run under VMware Player (if you have an appropriate CPU, of course) and with plain DOS (only HIMEM.SYS is allowed).

Gunther
You have to know the facts before you can distort them.