Question SSE floating kernel

I created a new thread using PsCreateSystemThread to avoid the complications of performing floating-point operations in the main kernel context.
Managed the floating-point environment manually, saving and restoring state using MASM instructions, like this:

stmxcsr dword ptr [rsp - 54h]  
ldmxcsr dword ptr gs:[180h]   

movaps [rsp - 10h], xmm0
movaps [rsp + 0h], xmm1
movaps [rsp + 10h], xmm2

The thread executes floating-point operations.

The Problem:
Despite the setup, the results are inconsistent or erroneous. It seems like the floating-point state isn’t being managed properly, or there’s interference with other parts of the kernel.

Is there a specific way to correctly set up the floating-point environment in a kernel thread?
Are there pitfalls I need to watch out for when using PsCreateSystemThread for this purpose?
Should I be using MMX/SSE or other registers differently to ensure correctness?

You should be using SSE. The issue is not interfering with other routines, the issue is that some of the floating point state is not saved/restored at interrupt time, so other threads (user or kernel) can interfere with YOUR results.

1 Like

i fix it and works now :slight_smile: for hours using LLVM its dirty fix but does the job

__m128 xmm0_backup, xmm1_backup, xmm2_backup;

 __declspec(align(16)) __m128 xmm_backup_stack[3];


    _asm {
        push r8
        push r9
        push r10
    }

    _asm {
        movaps xmm0_backup, xmm0
        movaps xmm1_backup, xmm1
        movaps xmm2_backup, xmm2
    }