From patchwork Sat Nov 30 11:57:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 11267945 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 887A6112B for ; Sat, 30 Nov 2019 11:59:09 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 63BDB2075A for ; Sat, 30 Nov 2019 11:59:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ODKR0+BN" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 63BDB2075A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ib1OJ-000527-NT; Sat, 30 Nov 2019 11:58:23 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ib1OI-00051O-LW for xen-devel@lists.xenproject.org; Sat, 30 Nov 2019 11:58:22 +0000 X-Inumbo-ID: a149a7c4-1368-11ea-a55d-bc764e2007e4 Received: from mail-wm1-x344.google.com (unknown [2a00:1450:4864:20::344]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a149a7c4-1368-11ea-a55d-bc764e2007e4; Sat, 30 Nov 2019 11:57:49 +0000 (UTC) Received: by mail-wm1-x344.google.com with SMTP id t14so167089wmi.5 for ; Sat, 30 Nov 2019 03:57:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zO5WWvofCaNtb+2iRPgQhDe+6eaidUu+sZbVOEdcssc=; b=ODKR0+BNmiPZgEqo7USKmQh5uehoxv03b1nHXJPXxBVkttgyYbHmwqolLj93GMCIgL 2NgDlbWnndb5Ehwbx6CW/MJPgb/BLdcAqRKzY5Ww9NC9plraIyP7vBR0ToyYETSNBGOo Wf9sJw7UdCYErvimYoTgBLzdlxjzc/TZg0NNgBnvY7og5hqwkYhrnk51A+wtnFvrOFdA fvNXuTueXjXxstVpNDz8u6yczE5x5w0BQByjqg6syE1B6yrt2ZNgbbR8aY9nRtuMgyVD VZ1m+6DppSbQiZg4k4RII74SN+lmZnKnPHATUNHLhAEPHhp3zvHJK+EKIakUXm2ndMJv 6jUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=zO5WWvofCaNtb+2iRPgQhDe+6eaidUu+sZbVOEdcssc=; b=G2ivv7C9lFRgvMfZf+HDL8/Ekm5nbAzNvIciTrTTyJZeEsV8wB+9UX8A6i7QJUPiug Kx2XTrysU8zSZidB6yK3Km9JEJu35litlJh60FjC/XPgZmrVecnU1B9Fr3TMAVkPEqCg /h4nXbIvnflLZl4kr+iu6LlxR6pAZMCs3/Vhd50SQWnz7Hne9j9ziuY1P4YjSJBIxTti nIIm64koFU2Fav+fQbRMNK5oDttsCt4kT+UEmFyo5AklOcEvyZIwpNdRNHoJN3zX9muj RKgov3BozLkhZgN3Ycso06dhZ9fx/36pG4OBb8oCTOYqQPkP1G+SmaURtL8IwuvVnb2e V1EQ== X-Gm-Message-State: APjAAAUroQ4bLXPmIeFG1vc3b4Ch1acLgCY3J8XsYiSAWw6rHDCIQJ3F HAIO/q2ii68dsYZAusUGoRzozOn+384= X-Google-Smtp-Source: APXvYqycnsB24gZj4V1Nb6OAewI3Wgjl5XU9kBaXl9EbAdASbR/2MX6MHotcZY3Pul4e2cvt01gKrA== X-Received: by 2002:a1c:486:: with SMTP id 128mr15863668wme.163.1575115068582; Sat, 30 Nov 2019 03:57:48 -0800 (PST) Received: from debian.mshome.net (122.163.200.146.dyn.plus.net. [146.200.163.122]) by smtp.gmail.com with ESMTPSA id z11sm2227769wrt.82.2019.11.30.03.57.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Nov 2019 03:57:48 -0800 (PST) From: Wei Liu X-Google-Original-From: Wei Liu To: Xen Development List Date: Sat, 30 Nov 2019 11:57:37 +0000 Message-Id: <20191130115737.15752-9-liuwe@microsoft.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191130115737.15752-1-liuwe@microsoft.com> References: <20191130115737.15752-1-liuwe@microsoft.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v5 8/8] x86: introduce CONFIG_HYPERV and detection code X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Wei Liu , Wei Liu , Andrew Cooper , Paul Durrant , Michael Kelley , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" We use the same code structure as we did for Xen. As starters, detect Hyper-V in probe routine. More complex functionalities will be added later. Signed-off-by: Wei Liu Reviewed-by: Paul Durrant Acked-by: Jan Beulich --- xen/arch/x86/Kconfig | 8 +++++ xen/arch/x86/guest/Makefile | 1 + xen/arch/x86/guest/hyperv/Makefile | 1 + xen/arch/x86/guest/hyperv/hyperv.c | 54 ++++++++++++++++++++++++++++++ xen/arch/x86/guest/hypervisor.c | 8 +++++ xen/include/asm-x86/guest.h | 1 + xen/include/asm-x86/guest/hyperv.h | 43 ++++++++++++++++++++++++ 7 files changed, 116 insertions(+) create mode 100644 xen/arch/x86/guest/hyperv/Makefile create mode 100644 xen/arch/x86/guest/hyperv/hyperv.c create mode 100644 xen/include/asm-x86/guest/hyperv.h diff --git a/xen/arch/x86/Kconfig b/xen/arch/x86/Kconfig index 18c8f6fd26..0a02b6ee3f 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -164,6 +164,14 @@ endchoice config GUEST bool +config HYPERV_GUEST + bool "Hyper-V Guest" + select GUEST + ---help--- + Support for Xen detecting when it is running under Hyper-V. + + If unsure, say N. + config XEN_GUEST bool "Xen Guest" select GUEST diff --git a/xen/arch/x86/guest/Makefile b/xen/arch/x86/guest/Makefile index f63d64bbee..f164196772 100644 --- a/xen/arch/x86/guest/Makefile +++ b/xen/arch/x86/guest/Makefile @@ -1,3 +1,4 @@ obj-y += hypervisor.o +subdir-$(CONFIG_HYPERV_GUEST) += hyperv subdir-$(CONFIG_XEN_GUEST) += xen diff --git a/xen/arch/x86/guest/hyperv/Makefile b/xen/arch/x86/guest/hyperv/Makefile new file mode 100644 index 0000000000..68170109a9 --- /dev/null +++ b/xen/arch/x86/guest/hyperv/Makefile @@ -0,0 +1 @@ +obj-y += hyperv.o diff --git a/xen/arch/x86/guest/hyperv/hyperv.c b/xen/arch/x86/guest/hyperv/hyperv.c new file mode 100644 index 0000000000..916e08ff89 --- /dev/null +++ b/xen/arch/x86/guest/hyperv/hyperv.c @@ -0,0 +1,54 @@ +/****************************************************************************** + * arch/x86/guest/hyperv/hyperv.c + * + * Support for detecting and running under Hyper-V. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; If not, see . + * + * Copyright (c) 2019 Microsoft. + */ +#include + +#include + +static const struct hypervisor_ops hyperv_ops = { + .name = "Hyper-V", +}; + +const struct hypervisor_ops * __init hyperv_probe(void) +{ + uint32_t eax, ebx, ecx, edx; + + cpuid(0x40000000, &eax, &ebx, &ecx, &edx); + if ( !((ebx == 0x7263694d) && /* "Micr" */ + (ecx == 0x666f736f) && /* "osof" */ + (edx == 0x76482074)) ) /* "t Hv" */ + return NULL; + + cpuid(0x40000001, &eax, &ebx, &ecx, &edx); + if ( eax != 0x31237648 ) /* Hv#1 */ + return NULL; + + return &hyperv_ops; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/x86/guest/hypervisor.c b/xen/arch/x86/guest/hypervisor.c index 6cd86d0c02..65c25115e0 100644 --- a/xen/arch/x86/guest/hypervisor.c +++ b/xen/arch/x86/guest/hypervisor.c @@ -38,6 +38,14 @@ const char *__init hypervisor_probe(void) if ( ops ) return ops->name; + /* + * Detection of Hyper-V must come after Xen to avoid false positive due + * to viridian support + */ + ops = hyperv_probe(); + if ( ops ) + return ops->name; + return NULL; } diff --git a/xen/include/asm-x86/guest.h b/xen/include/asm-x86/guest.h index 8e167165ae..94448606d4 100644 --- a/xen/include/asm-x86/guest.h +++ b/xen/include/asm-x86/guest.h @@ -20,6 +20,7 @@ #define __X86_GUEST_H__ #include +#include #include #include #include diff --git a/xen/include/asm-x86/guest/hyperv.h b/xen/include/asm-x86/guest/hyperv.h new file mode 100644 index 0000000000..3f88b94c77 --- /dev/null +++ b/xen/include/asm-x86/guest/hyperv.h @@ -0,0 +1,43 @@ +/****************************************************************************** + * asm-x86/guest/hyperv.h + * + * This program is free software; you can redistribute it and/or + * modify it under the terms and conditions of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; If not, see . + * + * Copyright (c) 2019 Microsoft. + */ + +#ifndef __X86_GUEST_HYPERV_H__ +#define __X86_GUEST_HYPERV_H__ + +#ifdef CONFIG_HYPERV_GUEST + +#include + +const struct hypervisor_ops *hyperv_probe(void); + +#else + +static inline const struct hypervisor_ops *hyperv_probe(void) { return NULL; } + +#endif /* CONFIG_HYPERV_GUEST */ +#endif /* __X86_GUEST_HYPERV_H__ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */