From patchwork Wed Oct 6 17:34:28 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 66949 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 59685B6EEE for ; Thu, 7 Oct 2010 04:43:34 +1100 (EST) Received: from localhost ([127.0.0.1]:37352 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P3Y1a-0002kK-VS for incoming@patchwork.ozlabs.org; Wed, 06 Oct 2010 13:43:31 -0400 Received: from [140.186.70.92] (port=35939 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P3Xy8-0001NZ-Dv for qemu-devel@nongnu.org; Wed, 06 Oct 2010 13:39:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P3Xy6-00078x-7L for qemu-devel@nongnu.org; Wed, 06 Oct 2010 13:39:56 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34022) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P3Xy5-00078h-Vk for qemu-devel@nongnu.org; Wed, 06 Oct 2010 13:39:54 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o96HdqtH022402 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 6 Oct 2010 13:39:52 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o96HdpDf030650; Wed, 6 Oct 2010 13:39:52 -0400 Received: from amt.cnet (vpn-8-65.rdu.redhat.com [10.11.8.65]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o96Hdo77016090; Wed, 6 Oct 2010 13:39:50 -0400 Received: from amt.cnet (localhost.localdomain [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 8502C68A0A8; Wed, 6 Oct 2010 14:38:52 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.4/8.14.4/Submit) id o96HcosM007266; Wed, 6 Oct 2010 14:38:50 -0300 Message-Id: <20101006173609.474173698@redhat.com> User-Agent: quilt/0.47-1 Date: Wed, 06 Oct 2010 14:34:28 -0300 From: Marcelo Tosatti To: kvm@vger.kernel.org, qemu-devel@nongnu.org References: <20101006173424.425523999@redhat.com> Content-Disposition: inline; filename=mce X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Dean Nelson , Marcelo Tosatti , Huang Ying Subject: [Qemu-devel] [patch uq/master 4/8] kvm: x86: add mce support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Port qemu-kvm's MCE support commit c68b2374c9048812f488e00ffb95db66c0bc07a7 Author: Huang Ying Date: Mon Jul 20 10:00:53 2009 +0800 Add MCE simulation support to qemu/kvm KVM ioctls are used to initialize MCE simulation and inject MCE. The real MCE simulation is implemented in Linux kernel. The Kernel part has been merged. Signed-off-by: Marcelo Tosatti Index: qemu/target-i386/helper.c =================================================================== --- qemu.orig/target-i386/helper.c +++ qemu/target-i386/helper.c @@ -27,6 +27,7 @@ #include "exec-all.h" #include "qemu-common.h" #include "kvm.h" +#include "kvm_x86.h" //#define DEBUG_MMU @@ -1030,6 +1031,11 @@ void cpu_inject_x86_mce(CPUState *cenv, if (bank >= bank_num || !(status & MCI_STATUS_VAL)) return; + if (kvm_enabled()) { + kvm_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc); + return; + } + /* * if MSR_MCG_CTL is not all 1s, the uncorrected error * reporting is disabled Index: qemu/target-i386/kvm.c =================================================================== --- qemu.orig/target-i386/kvm.c +++ qemu/target-i386/kvm.c @@ -27,6 +27,7 @@ #include "hw/pc.h" #include "hw/apic.h" #include "ioport.h" +#include "kvm_x86.h" #ifdef CONFIG_KVM_PARA #include @@ -167,6 +168,67 @@ static int get_para_features(CPUState *e } #endif +#ifdef KVM_CAP_MCE +static int kvm_get_mce_cap_supported(KVMState *s, uint64_t *mce_cap, + int *max_banks) +{ + int r; + + r = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_MCE); + if (r > 0) { + *max_banks = r; + return kvm_ioctl(s, KVM_X86_GET_MCE_CAP_SUPPORTED, mce_cap); + } + return -ENOSYS; +} + +static int kvm_setup_mce(CPUState *env, uint64_t *mcg_cap) +{ + return kvm_vcpu_ioctl(env, KVM_X86_SETUP_MCE, mcg_cap); +} + +static int kvm_set_mce(CPUState *env, struct kvm_x86_mce *m) +{ + return kvm_vcpu_ioctl(env, KVM_X86_SET_MCE, m); +} + +struct kvm_x86_mce_data +{ + CPUState *env; + struct kvm_x86_mce *mce; +}; + +static void kvm_do_inject_x86_mce(void *_data) +{ + struct kvm_x86_mce_data *data = _data; + int r; + + r = kvm_set_mce(data->env, data->mce); + if (r < 0) + perror("kvm_set_mce FAILED"); +} +#endif + +void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status, + uint64_t mcg_status, uint64_t addr, uint64_t misc) +{ +#ifdef KVM_CAP_MCE + struct kvm_x86_mce mce = { + .bank = bank, + .status = status, + .mcg_status = mcg_status, + .addr = addr, + .misc = misc, + }; + struct kvm_x86_mce_data data = { + .env = cenv, + .mce = &mce, + }; + + run_on_cpu(cenv, kvm_do_inject_x86_mce, &data); +#endif +} + int kvm_arch_init_vcpu(CPUState *env) { struct { @@ -274,6 +336,28 @@ int kvm_arch_init_vcpu(CPUState *env) cpuid_data.cpuid.nent = cpuid_i; +#ifdef KVM_CAP_MCE + if (((env->cpuid_version >> 8)&0xF) >= 6 + && (env->cpuid_features&(CPUID_MCE|CPUID_MCA)) == (CPUID_MCE|CPUID_MCA) + && kvm_check_extension(env->kvm_state, KVM_CAP_MCE) > 0) { + uint64_t mcg_cap; + int banks; + + if (kvm_get_mce_cap_supported(env->kvm_state, &mcg_cap, &banks)) + perror("kvm_get_mce_cap_supported FAILED"); + else { + if (banks > MCE_BANKS_DEF) + banks = MCE_BANKS_DEF; + mcg_cap &= MCE_CAP_DEF; + mcg_cap |= banks; + if (kvm_setup_mce(env, &mcg_cap)) + perror("kvm_setup_mce FAILED"); + else + env->mcg_cap = mcg_cap; + } + } +#endif + return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data); } Index: qemu/target-i386/kvm_x86.h =================================================================== --- /dev/null +++ qemu/target-i386/kvm_x86.h @@ -0,0 +1,21 @@ +/* + * QEMU KVM support + * + * Copyright (C) 2009 Red Hat Inc. + * Copyright IBM, Corp. 2008 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef __KVM_X86_H__ +#define __KVM_X86_H__ + +void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status, + uint64_t mcg_status, uint64_t addr, uint64_t misc); + +#endif