From patchwork Thu Nov 21 18:50:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 11256845 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 5E7366C1 for ; Thu, 21 Nov 2019 18:52:19 +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 394AF2067D for ; Thu, 21 Nov 2019 18:52:19 +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="mgv2U2M5" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 394AF2067D 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 1iXrYF-0007O9-95; Thu, 21 Nov 2019 18:51:35 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iXrYE-0007Nc-Ic for xen-devel@lists.xenproject.org; Thu, 21 Nov 2019 18:51:34 +0000 X-Inumbo-ID: dc966b44-0c8f-11ea-adbe-bc764e2007e4 Received: from mail-wr1-x443.google.com (unknown [2a00:1450:4864:20::443]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id dc966b44-0c8f-11ea-adbe-bc764e2007e4; Thu, 21 Nov 2019 18:51:01 +0000 (UTC) Received: by mail-wr1-x443.google.com with SMTP id s5so5768534wrw.2 for ; Thu, 21 Nov 2019 10:51:01 -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=8Y3wEFzVrOaDE1M8t+K3RG5EMlTBpIvMPtQ9f0XWpzE=; b=mgv2U2M5vAudGESEew+t5Hpz66OiCpH5eY2YEE65QpaWJf3Z2VnBPNz0Pc7jCzGqtJ iIZigNpf2PaeKxWuIEnRVpntFPvVBMYqAHjFlui7XiN+d6Kllho+VwZyvVtV+fjVracL f+8j7iKQDiOEEjoPxNX+0YhQtKZ2uCvhpelrvr035v8A4sCS5jTtclnsVaG/h3FniZaD 2jqBiR1CRepjHzL7wZZSUNn6NopeuJzqb3DwI2dpRiVLB/mSSjRz7/WwIH4w+rHbEEJ3 mU0TIhrN08txT6V1G898MZRVKikxUF/z6tCf4u4BkUYCG+IjNXsLlW56KA+3+OC8khD8 Olwg== 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=8Y3wEFzVrOaDE1M8t+K3RG5EMlTBpIvMPtQ9f0XWpzE=; b=ScQbU6WDY4jcRFmZ4BLy/gdgFEN6I12tswhVWtZBQV3AdDBtUmpchG0lTPcKqOOVxb XN+abQh1kwUlWUmhdkrMBucCELjh0PTkZ44LTPOuSJdHxQ2ngQMlLJWr3pSp+BY49S/V MkgoC8CQxorJrcqgYLTCXF1MKxwd7m1WrTtHl5Awtmn7ohUvU/JkPUvNuG+0/7FsBA7Z ckHFAVDc6SK4T7AKfGOb/HbADs8IXuXyC/GbT6jvwZn/qbXz8aplKurCoGLvrZGGDgQA rwe1c+nhBtT6z7/W+Ku5SmwjFi/MlIod2NUyBBgipv9C7dVqwG9COj4kiq7NMuQSlN/4 9CnQ== X-Gm-Message-State: APjAAAVpSNyhBHPp0Go/Y2FmXp2cNfldbPwyh/58a8RW/IGRqufdIzSg 5RfRqDJMGCKZKrUtD9wjO4snVu9qXOOLPw== X-Google-Smtp-Source: APXvYqxKquMkLF9FGNgpMrZx4pKWQDMoNlBDz04Yi7jWgfkP/vEw9VKhOjUiu2xe81y72e5r6i1Klw== X-Received: by 2002:adf:db8e:: with SMTP id u14mr3743435wri.274.1574362260037; Thu, 21 Nov 2019 10:51:00 -0800 (PST) Received: from debian.mshome.net (74.162.147.147.dyn.plus.net. [147.147.162.74]) by smtp.gmail.com with ESMTPSA id f24sm535776wmb.37.2019.11.21.10.50.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2019 10:50:59 -0800 (PST) From: Wei Liu X-Google-Original-From: Wei Liu To: Xen Development List Date: Thu, 21 Nov 2019 18:50:49 +0000 Message-Id: <20191121185049.16666-9-liuwe@microsoft.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191121185049.16666-1-liuwe@microsoft.com> References: <20191121185049.16666-1-liuwe@microsoft.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v4 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 , 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. Take the chance to fix XEN_GUEST in Kconfig. Signed-off-by: Wei Liu Reviewed-by: Paul Durrant Acked-by: Jan Beulich --- Changes in V4: 1. Add comment regarding order of probe functions. 2. Adapt to changes in previous patches. --- xen/arch/x86/Kconfig | 11 ++++-- 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, 117 insertions(+), 2 deletions(-) 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 867de857e8..0a02b6ee3f 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -164,10 +164,17 @@ 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 - def_bool n + bool "Xen Guest" select GUEST - prompt "Xen Guest" ---help--- Support for Xen detecting when it is running under Xen. 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 a067cacecb..c293e185cc 100644 --- a/xen/arch/x86/guest/hypervisor.c +++ b/xen/arch/x86/guest/hypervisor.c @@ -39,6 +39,14 @@ const struct hypervisor_ops *hypervisor_probe(void) if ( hops ) goto out; + /* + * Detection of Hyper-V must come after Xen to avoid false positive due + * to viridian support + */ + hops = hyperv_probe(); + if ( hops ) + goto out; + out: return hops; } 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: + */