Skip to content

Commit 9d643f6

Browse files
nickdesaulniersbonzini
authored andcommitted
KVM: x86: avoid large stack allocations in em_fxrstor
em_fxstor previously called fxstor_fixup. Both created instances of struct fxregs_state on the stack, which triggered the warning: arch/x86/kvm/emulate.c:4018:12: warning: stack frame size of 1080 bytes in function 'em_fxrstor' [-Wframe-larger-than=] static int em_fxrstor(struct x86_emulate_ctxt *ctxt) ^ with CONFIG_FRAME_WARN set to 1024. This patch does the fixup in em_fxstor now, avoiding one additional struct fxregs_state, and now fxstor_fixup can be removed as it has no other call sites. Further, the calculation for offsets into xmm_space can be shared between em_fxstor and em_fxsave. Signed-off-by: Nick Desaulniers <nick.desaulniers@gmail.com> [Clean up calculation of offsets and fix it for 64-bit mode. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 7461fbc commit 9d643f6

File tree

1 file changed

+37
-45
lines changed

1 file changed

+37
-45
lines changed

arch/x86/kvm/emulate.c

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,6 +3940,25 @@ static int check_fxsr(struct x86_emulate_ctxt *ctxt)
39403940
return X86EMUL_CONTINUE;
39413941
}
39423942

3943+
/*
3944+
* Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
3945+
* and restore MXCSR.
3946+
*/
3947+
static size_t __fxstate_size(int nregs)
3948+
{
3949+
return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
3950+
}
3951+
3952+
static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
3953+
{
3954+
bool cr4_osfxsr;
3955+
if (ctxt->mode == X86EMUL_MODE_PROT64)
3956+
return __fxstate_size(16);
3957+
3958+
cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
3959+
return __fxstate_size(cr4_osfxsr ? 8 : 0);
3960+
}
3961+
39433962
/*
39443963
* FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
39453964
* 1) 16 bit mode
@@ -3961,7 +3980,6 @@ static int check_fxsr(struct x86_emulate_ctxt *ctxt)
39613980
static int em_fxsave(struct x86_emulate_ctxt *ctxt)
39623981
{
39633982
struct fxregs_state fx_state;
3964-
size_t size;
39653983
int rc;
39663984

39673985
rc = check_fxsr(ctxt);
@@ -3977,68 +3995,42 @@ static int em_fxsave(struct x86_emulate_ctxt *ctxt)
39773995
if (rc != X86EMUL_CONTINUE)
39783996
return rc;
39793997

3980-
if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR)
3981-
size = offsetof(struct fxregs_state, xmm_space[8 * 16/4]);
3982-
else
3983-
size = offsetof(struct fxregs_state, xmm_space[0]);
3984-
3985-
return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
3986-
}
3987-
3988-
static int fxrstor_fixup(struct x86_emulate_ctxt *ctxt,
3989-
struct fxregs_state *new)
3990-
{
3991-
int rc = X86EMUL_CONTINUE;
3992-
struct fxregs_state old;
3993-
3994-
rc = asm_safe("fxsave %[fx]", , [fx] "+m"(old));
3995-
if (rc != X86EMUL_CONTINUE)
3996-
return rc;
3997-
3998-
/*
3999-
* 64 bit host will restore XMM 8-15, which is not correct on non-64
4000-
* bit guests. Load the current values in order to preserve 64 bit
4001-
* XMMs after fxrstor.
4002-
*/
4003-
#ifdef CONFIG_X86_64
4004-
/* XXX: accessing XMM 8-15 very awkwardly */
4005-
memcpy(&new->xmm_space[8 * 16/4], &old.xmm_space[8 * 16/4], 8 * 16);
4006-
#endif
4007-
4008-
/*
4009-
* Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but
4010-
* does save and restore MXCSR.
4011-
*/
4012-
if (!(ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))
4013-
memcpy(new->xmm_space, old.xmm_space, 8 * 16);
4014-
4015-
return rc;
3998+
return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
3999+
fxstate_size(ctxt));
40164000
}
40174001

40184002
static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
40194003
{
40204004
struct fxregs_state fx_state;
40214005
int rc;
4006+
size_t size;
40224007

40234008
rc = check_fxsr(ctxt);
40244009
if (rc != X86EMUL_CONTINUE)
40254010
return rc;
40264011

4027-
rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, 512);
4028-
if (rc != X86EMUL_CONTINUE)
4029-
return rc;
4012+
ctxt->ops->get_fpu(ctxt);
40304013

4031-
if (fx_state.mxcsr >> 16)
4032-
return emulate_gp(ctxt, 0);
4014+
size = fxstate_size(ctxt);
4015+
if (size < __fxstate_size(16)) {
4016+
rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
4017+
if (rc != X86EMUL_CONTINUE)
4018+
goto out;
4019+
}
40334020

4034-
ctxt->ops->get_fpu(ctxt);
4021+
rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
4022+
if (rc != X86EMUL_CONTINUE)
4023+
goto out;
40354024

4036-
if (ctxt->mode < X86EMUL_MODE_PROT64)
4037-
rc = fxrstor_fixup(ctxt, &fx_state);
4025+
if (fx_state.mxcsr >> 16) {
4026+
rc = emulate_gp(ctxt, 0);
4027+
goto out;
4028+
}
40384029

40394030
if (rc == X86EMUL_CONTINUE)
40404031
rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
40414032

4033+
out:
40424034
ctxt->ops->put_fpu(ctxt);
40434035

40444036
return rc;

0 commit comments

Comments
 (0)