Re: [PATCH] KVM: x86: dynamically allocate large struct in em_fxrstor

From: Paolo Bonzini
Date: Thu May 25 2017 - 10:07:26 EST




On 25/05/2017 03:36, Nick Desaulniers wrote:
>>> if (ctxt->mode < X86EMUL_MODE_PROT64)
>>> - rc = fxrstor_fixup(ctxt, &fx_state);
>>> + rc = fxrstor_fixup(ctxt, fx_state);
>> Ah, fxrstor_fixup most likely got inlined and both of them put ~512 byte
>> fxregs_state on the stack ... noinline attribute should solve the
>> warning too.
> While that would change fewer lines, doesn't the problem still exist in
> the case of `ctxt->mode < X86EMUL_MODE_PROT64`, just split across two
> stack frames? As in shouldn't we still dynamically allocate fx_state?

I think we should do the fixup backwards.

That is:

- first do get_fpu

- if the fixup is necessary, i.e. ctxt->mode < X86EMUL_MODE_PROT64, do
fxsave into &fxstate.

- then do segmented_read_std with the correct size, which is
- offsetof(struct fxregs_state, xmm_space[16]), i.e. 416
if ctxt->mode == X86EMUL_MODE_PROT64
- offsetof(struct fxregs_state, xmm_space[8]), i.e. 288
if ctxt->mode < X86EMUL_MODE_PROT64 and CR4.OSFXSR=1
- offsetof(struct fxregs_state, xmm_space[0]), i.e. 160
if ctxt->mode < X86EMUL_MODE_PROT64 and CR4.OSFXSR=0

- then check fx_state.mxcsr

- then do fxrstor

- finally do put_fpu

This will remove one of the two fxregs_state structs.

Paolo