Author Topic: AVX and AVX2 for DOS applications  (Read 4053 times)

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
AVX and AVX2 for DOS applications
« on: April 04, 2015, 11:39:04 AM »
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
Get your facts first, and then you can distort them.

sinsi

  • Member
  • *****
  • Posts: 1004
Re: AVX and AVX2 for DOS applications
« Reply #1 on: April 04, 2015, 04:41:24 PM »
Booting to MS-DOS 7, plain DOS - no config.sys or autoexec.bat, second run of cpu.exe after avx.exe
Code: [Select]
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 ...
I can walk on water but stagger on beer.

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: AVX and AVX2 for DOS applications
« Reply #2 on: April 04, 2015, 10:47:13 PM »
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
Get your facts first, and then you can distort them.

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: AVX and AVX2 for DOS applications
« Reply #3 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.

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: AVX and AVX2 for DOS applications
« Reply #4 on: April 05, 2015, 04:04:35 AM »
Hi nidud,

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
Get your facts first, and then you can distort them.

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: AVX and AVX2 for DOS applications
« Reply #5 on: April 05, 2015, 09:05:05 AM »
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
Get your facts first, and then you can distort them.

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: AVX and AVX2 for DOS applications
« Reply #6 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.

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: AVX and AVX2 for DOS applications
« Reply #7 on: April 06, 2015, 09:53:42 AM »
Hi nidud,

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
Get your facts first, and then you can distort them.

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: AVX and AVX2 for DOS applications
« Reply #8 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.

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: AVX and AVX2 for DOS applications
« Reply #9 on: April 06, 2015, 10:59:25 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
Get your facts first, and then you can distort them.