Commit 9d643f63 authored by Nick Desaulniers's avatar Nick Desaulniers Committed by Paolo Bonzini

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: default avatarNick Desaulniers <nick.desaulniers@gmail.com>
[Clean up calculation of offsets and fix it for 64-bit mode. - Paolo]
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 7461fbc4
...@@ -3940,6 +3940,25 @@ static int check_fxsr(struct x86_emulate_ctxt *ctxt) ...@@ -3940,6 +3940,25 @@ static int check_fxsr(struct x86_emulate_ctxt *ctxt)
return X86EMUL_CONTINUE; return X86EMUL_CONTINUE;
} }
/*
* Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but does save
* and restore MXCSR.
*/
static size_t __fxstate_size(int nregs)
{
return offsetof(struct fxregs_state, xmm_space[0]) + nregs * 16;
}
static inline size_t fxstate_size(struct x86_emulate_ctxt *ctxt)
{
bool cr4_osfxsr;
if (ctxt->mode == X86EMUL_MODE_PROT64)
return __fxstate_size(16);
cr4_osfxsr = ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR;
return __fxstate_size(cr4_osfxsr ? 8 : 0);
}
/* /*
* FXSAVE and FXRSTOR have 4 different formats depending on execution mode, * FXSAVE and FXRSTOR have 4 different formats depending on execution mode,
* 1) 16 bit mode * 1) 16 bit mode
...@@ -3961,7 +3980,6 @@ static int check_fxsr(struct x86_emulate_ctxt *ctxt) ...@@ -3961,7 +3980,6 @@ static int check_fxsr(struct x86_emulate_ctxt *ctxt)
static int em_fxsave(struct x86_emulate_ctxt *ctxt) static int em_fxsave(struct x86_emulate_ctxt *ctxt)
{ {
struct fxregs_state fx_state; struct fxregs_state fx_state;
size_t size;
int rc; int rc;
rc = check_fxsr(ctxt); rc = check_fxsr(ctxt);
...@@ -3977,68 +3995,42 @@ static int em_fxsave(struct x86_emulate_ctxt *ctxt) ...@@ -3977,68 +3995,42 @@ static int em_fxsave(struct x86_emulate_ctxt *ctxt)
if (rc != X86EMUL_CONTINUE) if (rc != X86EMUL_CONTINUE)
return rc; return rc;
if (ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR) return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state,
size = offsetof(struct fxregs_state, xmm_space[8 * 16/4]); fxstate_size(ctxt));
else
size = offsetof(struct fxregs_state, xmm_space[0]);
return segmented_write_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
}
static int fxrstor_fixup(struct x86_emulate_ctxt *ctxt,
struct fxregs_state *new)
{
int rc = X86EMUL_CONTINUE;
struct fxregs_state old;
rc = asm_safe("fxsave %[fx]", , [fx] "+m"(old));
if (rc != X86EMUL_CONTINUE)
return rc;
/*
* 64 bit host will restore XMM 8-15, which is not correct on non-64
* bit guests. Load the current values in order to preserve 64 bit
* XMMs after fxrstor.
*/
#ifdef CONFIG_X86_64
/* XXX: accessing XMM 8-15 very awkwardly */
memcpy(&new->xmm_space[8 * 16/4], &old.xmm_space[8 * 16/4], 8 * 16);
#endif
/*
* Hardware doesn't save and restore XMM 0-7 without CR4.OSFXSR, but
* does save and restore MXCSR.
*/
if (!(ctxt->ops->get_cr(ctxt, 4) & X86_CR4_OSFXSR))
memcpy(new->xmm_space, old.xmm_space, 8 * 16);
return rc;
} }
static int em_fxrstor(struct x86_emulate_ctxt *ctxt) static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
{ {
struct fxregs_state fx_state; struct fxregs_state fx_state;
int rc; int rc;
size_t size;
rc = check_fxsr(ctxt); rc = check_fxsr(ctxt);
if (rc != X86EMUL_CONTINUE) if (rc != X86EMUL_CONTINUE)
return rc; return rc;
rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, 512); ctxt->ops->get_fpu(ctxt);
if (rc != X86EMUL_CONTINUE)
return rc;
if (fx_state.mxcsr >> 16) size = fxstate_size(ctxt);
return emulate_gp(ctxt, 0); if (size < __fxstate_size(16)) {
rc = asm_safe("fxsave %[fx]", , [fx] "+m"(fx_state));
if (rc != X86EMUL_CONTINUE)
goto out;
}
ctxt->ops->get_fpu(ctxt); rc = segmented_read_std(ctxt, ctxt->memop.addr.mem, &fx_state, size);
if (rc != X86EMUL_CONTINUE)
goto out;
if (ctxt->mode < X86EMUL_MODE_PROT64) if (fx_state.mxcsr >> 16) {
rc = fxrstor_fixup(ctxt, &fx_state); rc = emulate_gp(ctxt, 0);
goto out;
}
if (rc == X86EMUL_CONTINUE) if (rc == X86EMUL_CONTINUE)
rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state)); rc = asm_safe("fxrstor %[fx]", : [fx] "m"(fx_state));
out:
ctxt->ops->put_fpu(ctxt); ctxt->ops->put_fpu(ctxt);
return rc; return rc;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment