kernel floating operations directly

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 ?

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.

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.

we can use fxsave64?

I really thought that page I referenced was pretty clear:

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

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.

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 ++

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;

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

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