Page 1 of 1
How and when to use XSAVE feature set?
Posted: Fri Oct 29, 2021 12:21 pm
by Ethin
I'm thinking of implementing this in my kernel sometime soon, but I'm confused on exactly how to use it. For example, should I use XSAVES/XRSTORS or the normal XSAVE/XSAVEOPT/XSAVEC and XRSTOR? Should I use it in every interrupt handler? And how do I go about allocating the memory area?
Re: How and when to use XSAVE feature set?
Posted: Fri Oct 29, 2021 8:20 pm
by deadmutex
XSAVES is similar to XSAVEC except it also saves the supervisor state as specified in the XSS MSR.
Ethin wrote:Should I use it in every interrupt handler?
You'd typically use it before a task switch or if your kernel modifies the SSE/AVX registers.
Re: How and when to use XSAVE feature set?
Posted: Fri Oct 29, 2021 8:28 pm
by Octocontrabass
Ethin wrote:For example, should I use XSAVES/XRSTORS or the normal XSAVE/XSAVEOPT/XSAVEC and XRSTOR?
All of these instructions are meant for context switches, but XSAVES/XRSTORS works for privileged context. If you want that privileged context to be unique for each thread, using XSAVES/XRSTORS would be the best way to do it.
I've seen some hints that XSAVES might be faster than XSAVEOPT, but I don't know of any benchmarks to compare the two.
...And of course, whichever one you choose limits you to CPUs that support it.
Ethin wrote:Should I use it in every interrupt handler?
Saving and restoring extended state can be pretty expensive. Usually interrupt handlers are written to avoid touching any extended state so that it doesn't need to be saved and restored. Plus, if your interrupt handler wants to use any extended state, it probably needs the extended state to be initialized to reasonable values, which adds even more time on top of saving and restoring.
Ethin wrote:And how do I go about allocating the memory area?
Allocate it alongside the rest of the data your kernel needs to track for each thread. The manuals explain how to figure out how much memory to allocate and what you need to put into that memory before you can use it for XSAVE. The specifics depend on which one you're using.