How and when to use XSAVE feature set?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

How and when to use XSAVE feature set?

Post 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?
User avatar
deadmutex
Member
Member
Posts: 85
Joined: Wed Sep 28, 2005 11:00 pm

Re: How and when to use XSAVE feature set?

Post 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.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How and when to use XSAVE feature set?

Post 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.
Post Reply