From mboxrd@z Thu Jan 1 00:00:00 1970 From: Anthony Liguori Subject: Re: [PATCH] QEMU-KVM: MCE: Relay UCR MCE to guest Date: Mon, 07 Sep 2009 15:48:07 -0500 Message-ID: <4AA57187.5020502@us.ibm.com> References: <1252312353.14648.731.camel@yhuang-dev.sh.intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: Avi Kivity , Andi Kleen , "kvm@vger.kernel.org" To: Huang Ying Return-path: Received: from e2.ny.us.ibm.com ([32.97.182.142]:51480 "EHLO e2.ny.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751689AbZIGUsH (ORCPT ); Mon, 7 Sep 2009 16:48:07 -0400 Received: from d01relay02.pok.ibm.com (d01relay02.pok.ibm.com [9.56.227.234]) by e2.ny.us.ibm.com (8.14.3/8.13.1) with ESMTP id n87KfkgU024663 for ; Mon, 7 Sep 2009 16:41:46 -0400 Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by d01relay02.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id n87Km9cf248946 for ; Mon, 7 Sep 2009 16:48:09 -0400 Received: from d01av04.pok.ibm.com (loopback [127.0.0.1]) by d01av04.pok.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n87Km9Np028691 for ; Mon, 7 Sep 2009 16:48:09 -0400 In-Reply-To: <1252312353.14648.731.camel@yhuang-dev.sh.intel.com> Sender: kvm-owner@vger.kernel.org List-ID: Hi Huang, Huang Ying wrote: > UCR (uncorrected recovery) MCE is supported in recent Intel CPUs, > where some hardware error such as some memory error can be reported > without PCC (processor context corrupted). To recover from such MCE, > the corresponding memory will be unmapped, and all processes accessing > the memory will be killed via SIGBUS. > > For KVM, if QEMU/KVM is killed, all guest processes will be killed > too. So we relay SIGBUS from host OS to guest system via a UCR MCE > injection. Then guest OS can isolate corresponding memory and kill > necessary guest processes only. SIGBUS sent to main thread (not VCPU > threads) will be broadcast to all VCPU threads as UCR MCE. > > Signed-off-by: Huang Ying > > --- > qemu-kvm.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++---- > target-i386/cpu.h | 20 +++++- > 2 files changed, 181 insertions(+), 12 deletions(-) > > --- a/qemu-kvm.c > +++ b/qemu-kvm.c > @@ -27,10 +27,23 @@ > #include > #include > #include > +#include > +#include > > #define false 0 > #define true 1 > > +#ifndef PR_MCE_KILL > +#define PR_MCE_KILL 33 > +#endif > + > +#ifndef BUS_MCEERR_AR > +#define BUS_MCEERR_AR 4 > +#endif > +#ifndef BUS_MCEERR_AO > +#define BUS_MCEERR_AO 5 > +#endif > + > #define EXPECTED_KVM_API_VERSION 12 > > #if EXPECTED_KVM_API_VERSION != KVM_API_VERSION > @@ -702,6 +715,24 @@ int kvm_get_dirty_pages_range(kvm_contex > return 0; > } > > +static int kvm_addr_userspace_to_phys(unsigned long userspace_addr, > + unsigned long *phys_addr) > +{ > + int i; > + struct slot_info *slot; > + > + for (i = 0; i < KVM_MAX_NUM_MEM_REGIONS; ++i) { > + slot = &slots[i]; > + if (slot->len && slot->userspace_addr <= userspace_addr && > + (slot->userspace_addr + slot->len) > userspace_addr) { > + *phys_addr = userspace_addr - slot->userspace_addr + > + slot->phys_addr; > + return 0; > + } > + } > + return -1; > +} > + > The slot mapping is actually a copy of the qemu's ram_blocks structure (see exec.c). If you base your check on that, it will Just Work for QEMU too. > #ifdef KVM_CAP_IRQCHIP > > int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status) > @@ -1515,6 +1546,38 @@ static void sig_ipi_handler(int n) > { > } > > +static void sigbus_handler(int n, struct signalfd_siginfo *siginfo, void *ctx) > +{ > + if (siginfo->ssi_code == BUS_MCEERR_AO) { > + uint64_t status; > + unsigned long paddr; > + CPUState *cenv; > + > + /* Hope we are lucky for AO MCE */ > Even if the error was limited to guest memory, it could have been generated by either the kernel or userspace reading guest memory, no? Does this potentially open a security hole for us? Consider the following: 1) We happen to read guest memory and that causes an MCE. For instance, say we're in virtio.c and we read the virtio ring. 2) That should trigger the kernel to generate a sigbus. 3) We catch sigbus, and queue an MCE for delivery. 4) After sigbus handler completes, we're back in virtio.c, what was the value of the memory operation we just completed? If the instruction gets skipped, we may be leaking host memory because the access never happened. -- Regards, Anthony Liguori