[RFC PATCH 03/10] x86/fpu: Calculate xsave state addr on fly

From: Jiaxun Yang
Date: Thu Dec 02 2021 - 19:36:55 EST


As we are going to have xsave buffer with different xcomp_bv,
the compact layout of those buffers can be different.

Calculate state address on fly instead of cache them on boot.

Signed-off-by: Jiaxun Yang <j.yang-87@xxxxxxxxxxxx>
---
arch/x86/kernel/fpu/xstate.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index 353c661f8027..06f6214e9dd7 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -1006,12 +1006,32 @@ void fpu__resume_cpu(void)
*/
static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
{
+ int i;
+ unsigned int offset;
+
if (!xfeature_enabled(xfeature_nr)) {
WARN_ON_FPU(1);
return NULL;
}

- return (void *)xsave + xstate_comp_offsets[xfeature_nr];
+ /* Hanlde legacy states and standard format ext states */
+ if (xfeature_nr < FIRST_EXTENDED_XFEATURE ||
+ !(xsave->header.xcomp_bv & XCOMP_BV_COMPACTED_FORMAT))
+ return (void *)xsave + xstate_offsets[xfeature_nr];
+
+ /* Calculate compact ext state offsets */
+ offset = FXSAVE_SIZE + XSAVE_HDR_SIZE;
+ for_each_extended_xfeature(i, xsave->header.xcomp_bv) {
+ if (xfeature_is_aligned(i))
+ offset = ALIGN(offset, 64);
+
+ if (i == xfeature_nr)
+ return (void *)xsave + offset;
+
+ offset += xstate_sizes[i];
+ }
+
+ return NULL;
}
/*
* Given the xsave area and a state inside, this function returns the
--
2.30.2