Skip to content

Commit ad8f9e6

Browse files
Maxim Levitskybonzini
authored andcommitted
KVM: x86: emulator: update the emulation mode after CR0 write
Update the emulation mode when handling writes to CR0, because toggling CR0.PE switches between Real and Protected Mode, and toggling CR0.PG when EFER.LME=1 switches between Long and Protected Mode. This is likely a benign bug because there is no writeback of state, other than the RIP increment, and when toggling CR0.PE, the CPU has to execute code from a very low memory address. Signed-off-by: Maxim Levitsky <[email protected]> Message-Id: <[email protected]> Cc: [email protected] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 055f37f commit ad8f9e6

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

arch/x86/kvm/emulate.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3641,11 +3641,25 @@ static int em_movbe(struct x86_emulate_ctxt *ctxt)
36413641

36423642
static int em_cr_write(struct x86_emulate_ctxt *ctxt)
36433643
{
3644-
if (ctxt->ops->set_cr(ctxt, ctxt->modrm_reg, ctxt->src.val))
3644+
int cr_num = ctxt->modrm_reg;
3645+
int r;
3646+
3647+
if (ctxt->ops->set_cr(ctxt, cr_num, ctxt->src.val))
36453648
return emulate_gp(ctxt, 0);
36463649

36473650
/* Disable writeback. */
36483651
ctxt->dst.type = OP_NONE;
3652+
3653+
if (cr_num == 0) {
3654+
/*
3655+
* CR0 write might have updated CR0.PE and/or CR0.PG
3656+
* which can affect the cpu's execution mode.
3657+
*/
3658+
r = emulator_recalc_and_set_mode(ctxt);
3659+
if (r != X86EMUL_CONTINUE)
3660+
return r;
3661+
}
3662+
36493663
return X86EMUL_CONTINUE;
36503664
}
36513665

0 commit comments

Comments
 (0)