joncampbell123 / doslib

Hackipedia DOSLIB, a general collection of useful libraries for writing MS-DOS software
GNU Lesser General Public License v2.1
199 stars 17 forks source link

Windows 95 EMM386.EXE V86 monitor does not allow RDTSC instruction #30

Open joncampbell123 opened 6 years ago

joncampbell123 commented 6 years ago

Here's a disappointing revelation.

Apparently Windows 95's EMM386.EXE doesn't like it when 16-bit DOS applications try to use RDTSC. It seems to hang the system (or perhaps it's in an infinite Invalid Opcode exception loop). 32-bit DOS programs (under DOS4GW/DOS32) are not affected.

I would like to know if this has been a consistent problem across MS-DOS through Windows 98 SE, or if it's just the particular version of Windows 95 I'm testing right now (4.0.1111 Windows 95 SP1).

Until then, I'm going to have to have RDTSC-related code disable itself unless known versions of MS-DOS and EMM386.EXE are running :(

joncampbell123 commented 6 years ago

I can confirm that if I manually enter the RDTSC opcode in DOS DEBUG and try to single-step it, that the system hangs in the same way.

joncampbell123 commented 6 years ago

Because of this issue, the CPU library now has a flag to indicate not to use the RDTSC instruction. For now, that flag is set if the build is 16-bit and virtual 8086 mode is active.

joncampbell123 commented 6 years ago

I will update that code to not set the flag if it can recognize whether or not the v86 monitor permits RDTSC. Perhaps FreeDOS has a better implementation. Perhaps MS-DOS 6.22 and earlier don't have this issue, or perhaps Microsoft fixed this in Windows 98 DOS mode. I'll need to test this.

joncampbell123 commented 6 years ago

This was observed on an old Pentium 133MHz test system with Windows 95 DOS mode on a CF card.

joncampbell123 commented 6 years ago

TODO: Which MSR controls whether user-space can access RDTSC, by the way? I understand there's another one for whether or not user-space can use CPUID.

joncampbell123 commented 6 years ago

A-ha:

https://en.wikipedia.org/wiki/Control_register#CR4

darkstar commented 6 years ago

The P133 does not support rdtsc in v86 mode: http://www.os2museum.com/wp/undocumented-rdtsc/