From: "Gabriel L. Somlo" Subject: Re: [PATCH v2] kvm: x86: emulate monitor and mwait instructions as nop Date: Wed, 4 Jun 2014 15:43:34 -0400 Message-ID: <20140604194334.GK1653@ERROL.INI.CMU.EDU> References: <20140507205210.GA30030@ERROL.INI.CMU.EDU> <20140602192530.GC1653@ERROL.INI.CMU.EDU> <538D92BC.4060203@redhat.com> <20140604143941.GF1653@ERROL.INI.CMU.EDU> <538F30BD.5000501@suse.de> <538F4A7C.7070202@redhat.com> <9767B286-A7EB-4BED-B500-589E62D0433E@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Paolo Bonzini , Alexander Graf , kvm@vger.kernel.org, mst@redhat.com, Borislav Petkov To: Nadav Amit Return-path: Received: from mail-qa0-f53.google.com ([209.85.216.53]:62967 "EHLO mail-qa0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751132AbaFDTnh (ORCPT ); Wed, 4 Jun 2014 15:43:37 -0400 Received: by mail-qa0-f53.google.com with SMTP id x12so7770021qac.40 for ; Wed, 04 Jun 2014 12:43:36 -0700 (PDT) Content-Disposition: inline In-Reply-To: <9767B286-A7EB-4BED-B500-589E62D0433E@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: On Wed, Jun 04, 2014 at 10:12:39PM +0300, Nadav Amit wrote: > Regardless to the whole discussion of what the guest is informed about, I think it might be better to implement mwait and monitor correctly according to the spec and let the instructions to be fully emulated. > Both mwait and monitor may encounter exceptions (#GP, #PF, regardless of #UD), so this behaviour should be correct. > If you want me, I?ll send my version which looks something like: > > static int em_monitor(struct x86_emulate_ctxt *ctxt) > { > int rc; > struct segmented_address addr; > u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); > u64 rax = reg_read(ctxt, VCPU_REGS_RAX); > u8 byte; > > rc = check_mwait_supported(ctxt); > if (rc != X86EMUL_CONTINUE) > return rc; > > if (ctxt->mode != X86EMUL_MODE_PROT64) > rcx = (u32)rcx; > > if (rcx != 0) > return emulate_gp(ctxt, 0); > > addr.seg = seg_override(ctxt); > addr.ea = ctxt->ad_bytes == 8 ? rax : (u32)rax; > > rc = segmented_read(ctxt, addr, &byte, 1); > if (rc != X86EMUL_CONTINUE) > return rc; > > return X86EMUL_CONTINUE; > } > > static int em_mwait(struct x86_emulate_ctxt *ctxt) > { > u64 rcx = reg_read(ctxt, VCPU_REGS_RCX); > int rc = check_mwait_supported(ctxt); > if (rc != X86EMUL_CONTINUE) > return rc; > if (ctxt->mode != X86EMUL_MODE_PROT64) > rcx = (u32)rcx; > > if ((rcx & ~(u64)1) != 0) > return emulate_gp(ctxt, 0); > > if (rcx & 1) { > /* Interrupt as break event */ > u32 ebx, ecx, edx, eax; > eax = 5; > ecx = 0; > ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx); > if (!(ecx & 1)) > return emulate_gp(ctxt, 0); > } > return X86EMUL_CONTINUE; > } I'd be curious how you're dealing with the "hidden" CPU state which tells MWAIT to sleep until someone or something writes to the monitored memory area set up by a corresponding MONITOR instruction. Thanks, --Gabriel