Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV

Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


kernel floating operations directly

zky02zky02 Member Posts: 10

im doing some floating operations in kernel and seems its cause a lot of problems and had to use KeSaveExtendedProcessorState XSTATE_MASK_LEGACY_FLOATING_POINT

isn't kernel handle it directly on x64 ?

Comments

  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 9,160

    Hmmmm.... No?

    You're using the old MMX/x87 registers in your 64-bit kernel-mode code? That's not allowed. At all. Never has been.

    When you perform floating point operations in C, the compiler generates SSE instructions.

    Peter Viscarola
    OSR
    @OSRDrivers

  • MBond2MBond2 Member Posts: 706

    Echoing Peter's comments - no it is not handled automatically

    Using floating point types in KM is uncommon and probably wrong most of the time. Saving and restoring their state has a performance cost. So why do it all of the time and automatically? In the rare case when it is required, there is an explicit API to do it, so use it.

  • zky02zky02 Member Posts: 10
    we can use fxsave64?
  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 9,160

    I really thought that page I referenced was pretty clear:

    x64 kernel mode code is not allowed to access the MMX/x87 registers.

    Peter Viscarola
    OSR
    @OSRDrivers

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 14,832

    Why is this even an issue? If you are using floating point operations in C or C++ code, then the compiler should be doing what is expected. If you are using assembly sequences, well, that was a Bad Idea to begin with.

    Tim Roberts, [email protected]
    Software Wizard Emeritus

  • zky02zky02 Member Posts: 10
    edited January 28

    yes using fxsave64 did the trick

        alignas(16) char fxStateBuffer[512];
    
        double ret = -1.0;
    
    
    _fxsave64(fxStateBuffer);
    
        __try
        {
            ret = our floating-point operation here
        }
        __except (EXCEPTION_EXECUTE_HANDLER)
        {
            ret = -1.0;
        }
    
        // Restore the floating-point state
        _fxrstor64(fxStateBuffer);
    
        return ret;
    

    works amazing thanks guys ! there also no performance hit in my testing which also ++

  • zky02zky02 Member Posts: 10

    more optimized using _xsaveopt even though fxsave64 does amazing job !

    alignas(64) char xsaveBuffer[4096]; 
        unsigned long long xfeatureMask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
    
        double ret = -1.0;
    
    
        _xsaveopt(xsaveBuffer, xfeatureMask);
    
        __try
        {
            ret = our floating-point operation here
        }
        __except (EXCEPTION_EXECUTE_HANDLER)
        {
            ret = -1.0;
        }
    
        // Restore the extended state
        _xrstor(xsaveBuffer, xfeatureMask);
    
        return ret;
    
  • Tim_RobertsTim_Roberts Member - All Emails Posts: 14,832

    Just so you aware, "it worked once for me" does not mean "it is guaranteed to work everywhere".

    Tim Roberts, [email protected]
    Software Wizard Emeritus

  • MBond2MBond2 Member Posts: 706

    Asking for advice and then ignoring it in a flagrant way doesn't seem like a good idea in general.

    In this case, you are ignoring established documentation as well. And have failed to write good SEH

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Kernel Debugging 13-17 May 2024 Live, Online
Developing Minifilters 1-5 Apr 2024 Live, Online
Internals & Software Drivers 11-15 Mar 2024 Live, Online
Writing WDF Drivers 20-24 May 2024 Live, Online