Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp337034pxb; Wed, 20 Jan 2021 08:09:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJxjPAY9kxE2LLjwbBrSpZ4wL2KX1IY0PVL+XBxuNNVBUgEGEJ5XQBvk5WEg35bNE117m6Ab X-Received: by 2002:a17:906:1701:: with SMTP id c1mr6497192eje.395.1611158970898; Wed, 20 Jan 2021 08:09:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611158970; cv=none; d=google.com; s=arc-20160816; b=L2bp9POMXs2AmgsxerohDXl6f3EJYGyFDihJ8FiTuIo8FEL2IESf3+nhYZ5X6Gqtw7 7fk5RcHXDVnaJgM5INd8k57tY4A1lIco/KZHz9yxW0K+M6Z62AfInVN9EZ+DIEFW7Lgl n4eB2gSZHFYppuMP/poI/dHaBiRbZHt49NVLf/umegjt76SWMAnXKz4WrzD2DXDTISrq Egmhz5CZeUJDWfvqn/CblT01fxVjxEKtnRKL3dhDBC24VBDdFjUXttRk6Y9Q56UVxBV1 EkJLMvGPPrzBKzZTn5dSQCnFDZd5b3brzjCdMQ2dCdbqiywuai5yK5ilbTF7gKQp4Hiz yklw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=qaBbcFFWtwH73KKkHPV2dAAEp83v3RAFbPyaNgV/jIA=; b=YPZcit6CVcOsqci7lXhlDrJbmWkKekXp+gLnnUbg3MF3sTpFo+kV1knCz0wvpm9YAo lcTLZJg/qjJM7NesTGOW7X5lkkoPxevrVEpekiod2J34eF9ab+mN7FhX/kdCRdIzEF86 mtaKAJpQTrwciCgTJDFg8pBapoX19OAOX8HFW9jTevAOpBvVbrmsBuB4FXEkCeFVJhHs Gto/u8Ydro4NjUhA2V9aQOyjWyAF+tKCrgkhrCldt3DZ5RfzyCu0rPSrHP9VRl9V4sT/ MR+TsWPCg+a+In78A4G3Rfeq0LxFNFw39BWCNPq1DZRa+WyOrUFojfYxuLXV8SqqcqBY PS/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b=on1TzcIQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b13si1016151eds.375.2021.01.20.08.08.57; Wed, 20 Jan 2021 08:09:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b=on1TzcIQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729113AbhATQFg (ORCPT + 99 others); Wed, 20 Jan 2021 11:05:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726460AbhATQEf (ORCPT ); Wed, 20 Jan 2021 11:04:35 -0500 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43982C0613CF for ; Wed, 20 Jan 2021 08:03:55 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id g3so14871191ejb.6 for ; Wed, 20 Jan 2021 08:03:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qaBbcFFWtwH73KKkHPV2dAAEp83v3RAFbPyaNgV/jIA=; b=on1TzcIQfB0S+jMkItagEJtX8hz1tNnjBcnUtaJWlF9V6USnFvBrmlsf+hgKeuoHzg fHXarQilEi/Z1srXltN0diMlHMbSDa0P1lRdEw2fafXT9lYdDcUCNJCdWXc9aXmvvjie BRVYlaez6uS7Xpon2IXP3iVJMV1PHrWsQXZ6PAA0SdZLYh31u/j2HbzbttRSevVbCqn+ nV8DpomSlRxtSnh4hLOliPHOECsNLhUPi2JYLCTO8X+jnNqakzuTardr31/bn0B45K6K jm0UELIHadLwshBArvF3Layo+XQsGrbgjxf7OiPKuwCJKVR/XQjK2YcWIJM1l5VjgATz ksiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qaBbcFFWtwH73KKkHPV2dAAEp83v3RAFbPyaNgV/jIA=; b=HGZj7VFuXNMHxCbT5RdLrx/vfh20TwE4RnrDwKc/hcuSDAxx+/KNZVc8FaPV20zZX7 wpAMoPP71NjKZU0twed9l0ws9ARUFzaOTC10BaOFqXiB7qbtVVZ8piaao0RfoXqEVgRg kRhgOwmReBmpIokXpF6Vl6WwKH5PKdG5/7VryDwLZyPKwhRzpw6vNSr0IJrpitGaHNZm tkrb5pz3DuixF+uNxm0ncPQ7iS92/3mhteNRj6EDkWcYOPC+q9zxVPD55hoVt3EUlNsA jKkQLKcOx3s6DHq1e5GtrxMQkiVIX13NwJmr2U7JTJhL/A+3TQPNu79zoljQJq5DE8gE pKNQ== X-Gm-Message-State: AOAM533oS6eu1b5+vfPulIxwMzBIVsqXNfBtpktr6svxIRATGU7xcrE+ 1WyjdyFyc+eNp/9nyaqXWO6F2tg3ybVYUCWAvKSaQA== X-Received: by 2002:a17:906:eb95:: with SMTP id mh21mr6793469ejb.175.1611158633954; Wed, 20 Jan 2021 08:03:53 -0800 (PST) MIME-Version: 1.0 References: <20210120120058.29138-1-wei.liu@kernel.org> <20210120120058.29138-3-wei.liu@kernel.org> In-Reply-To: <20210120120058.29138-3-wei.liu@kernel.org> From: Pavel Tatashin Date: Wed, 20 Jan 2021 11:03:18 -0500 Message-ID: Subject: Re: [PATCH v5 02/16] x86/hyperv: detect if Linux is the root partition To: Wei Liu Cc: Linux on Hyper-V List , virtualization@lists.linux-foundation.org, Linux Kernel List , Michael Kelley , Vineeth Pillai , Sunil Muthuswamy , Nuno Das Neves , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , "H. Peter Anvin" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 20, 2021 at 7:01 AM Wei Liu wrote: > > For now we can use the privilege flag to check. Stash the value to be > used later. > > Put in a bunch of defines for future use when we want to have more > fine-grained detection. > > Signed-off-by: Wei Liu > --- > v3: move hv_root_partition to mshyperv.c > --- > arch/x86/include/asm/hyperv-tlfs.h | 10 ++++++++++ > arch/x86/include/asm/mshyperv.h | 2 ++ > arch/x86/kernel/cpu/mshyperv.c | 20 ++++++++++++++++++++ > 3 files changed, 32 insertions(+) > > diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hyperv-tlfs.h > index 6bf42aed387e..204010350604 100644 > --- a/arch/x86/include/asm/hyperv-tlfs.h > +++ b/arch/x86/include/asm/hyperv-tlfs.h > @@ -21,6 +21,7 @@ > #define HYPERV_CPUID_FEATURES 0x40000003 > #define HYPERV_CPUID_ENLIGHTMENT_INFO 0x40000004 > #define HYPERV_CPUID_IMPLEMENT_LIMITS 0x40000005 > +#define HYPERV_CPUID_CPU_MANAGEMENT_FEATURES 0x40000007 > #define HYPERV_CPUID_NESTED_FEATURES 0x4000000A > > #define HYPERV_CPUID_VIRT_STACK_INTERFACE 0x40000081 > @@ -110,6 +111,15 @@ > /* Recommend using enlightened VMCS */ > #define HV_X64_ENLIGHTENED_VMCS_RECOMMENDED BIT(14) > > +/* > + * CPU management features identification. > + * These are HYPERV_CPUID_CPU_MANAGEMENT_FEATURES.EAX bits. > + */ > +#define HV_X64_START_LOGICAL_PROCESSOR BIT(0) > +#define HV_X64_CREATE_ROOT_VIRTUAL_PROCESSOR BIT(1) > +#define HV_X64_PERFORMANCE_COUNTER_SYNC BIT(2) > +#define HV_X64_RESERVED_IDENTITY_BIT BIT(31) > + > /* > * Virtual processor will never share a physical core with another virtual > * processor, except for virtual processors that are reported as sibling SMT > diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h > index ffc289992d1b..ac2b0d110f03 100644 > --- a/arch/x86/include/asm/mshyperv.h > +++ b/arch/x86/include/asm/mshyperv.h > @@ -237,6 +237,8 @@ int hyperv_fill_flush_guest_mapping_list( > struct hv_guest_mapping_flush_list *flush, > u64 start_gfn, u64 end_gfn); > > +extern bool hv_root_partition; > + > #ifdef CONFIG_X86_64 > void hv_apic_init(void); > void __init hv_init_spinlocks(void); > diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c > index f628e3dc150f..c376d191a260 100644 > --- a/arch/x86/kernel/cpu/mshyperv.c > +++ b/arch/x86/kernel/cpu/mshyperv.c > @@ -32,6 +32,10 @@ > #include > #include > > +/* Is Linux running as the root partition? */ > +bool hv_root_partition; > +EXPORT_SYMBOL_GPL(hv_root_partition); > + > struct ms_hyperv_info ms_hyperv; > EXPORT_SYMBOL_GPL(ms_hyperv); > > @@ -237,6 +241,22 @@ static void __init ms_hyperv_init_platform(void) > pr_debug("Hyper-V: max %u virtual processors, %u logical processors\n", > ms_hyperv.max_vp_index, ms_hyperv.max_lp_index); > > + /* > + * Check CPU management privilege. > + * > + * To mirror what Windows does we should extract CPU management > + * features and use the ReservedIdentityBit to detect if Linux is the > + * root partition. But that requires negotiating CPU management > + * interface (a process to be finalized). Is this comment relevant? Do we have to mirror what Windows does? > + * > + * For now, use the privilege flag as the indicator for running as > + * root. > + */ > + if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_CPU_MANAGEMENT) { > + hv_root_partition = true; > + pr_info("Hyper-V: running as root partition\n"); > + } > + Reviewed-by: Pavel Tatashin > /* > * Extract host information. > */ > -- > 2.20.1 >