Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp430025pxb; Thu, 12 Nov 2020 07:18:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJyMC87UYUVYWcJX5BbK/7fJ6vPrmMX+FR8jaNuO5u/V4slDfr9m8ENmwSFlM//KaF+IXZxH X-Received: by 2002:a17:906:1f13:: with SMTP id w19mr30300249ejj.39.1605194315616; Thu, 12 Nov 2020 07:18:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605194315; cv=none; d=google.com; s=arc-20160816; b=WZ7aK8zXojRIx2g/Jiad2qrP+XQjmsOcVIxJ0vrD3NYa3Mvwwi487Q3l5lqhLVw4FO IrXdcl8ETE7epeDZzd6+MY1wYETXv8bAixj8ogVDUWUoA88qXbwyliPQ05NZfxusCH8o r1gI5gfw/krxSaXcv994HJ4idgOdSmP3T6VyyJG7kHuTjxkNQJAEDoaH3OWyFdp9130/ oEBZu/1yyCqeOBN4OuaQYYW4YeoxKww425JgaB4ac5ViU3vrck2cryi3t5Jv8CwTKNyF /Xp58wUa/RL+zQU7FxW9Q8S4iJN7+mHcdY71axLNyGz7EytBHvRpaqFwlt1t1Aoaz5qC PLAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=mgdXZHtHPywKX/a9hS8LUB6+ABxOjrBQbybE/K2MyA4=; b=eLWzAcpMCN7o+5TymX8Ah/dPuh56zEOeuw8+qAxgQnmH6gR9V6pTaW1X+8cfk/mo9M OARoU+TMxoqhvcas9easUbp9/7yKaA6SNqClDBMSgA7ic1cGHPBKil7/8AxOo3ir0o7a KWXkOZf87GD0De4YQE6CCdLLeG/oP0ppGmAAaNZAjzBu1NUspA2TWdt9UPiGWLPnAXBB 0qTyWuvq27d5ode/T4MzycTElEV0qouMYohCXgMWSPU/SD01M42aP1xI7hwSHKc9Oao5 dDNsy3O7L+QmTbKSHEM0f7HGkR1iKJukCJ0AO5755GqVJJzuCfUjfV2jF/Qy2DQFMtgK /PQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cykmF34j; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j17si3921358ejv.267.2020.11.12.07.18.07; Thu, 12 Nov 2020 07:18:35 -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=@redhat.com header.s=mimecast20190719 header.b=cykmF34j; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727035AbgKLPQk (ORCPT + 99 others); Thu, 12 Nov 2020 10:16:40 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:46501 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727796AbgKLPQh (ORCPT ); Thu, 12 Nov 2020 10:16:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605194196; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=mgdXZHtHPywKX/a9hS8LUB6+ABxOjrBQbybE/K2MyA4=; b=cykmF34jlNHNBZVvhoSww9Es23tWUCsSYO6XS0Sq4RE2aoD2IqMZOJHsknTKVPaAzwS/6v BrEuJRhe0JFySqSESnbv/rQ7dz+wJ8Jqwhyr/5aTxV60GJvq+ssLMn0BzHY0DRIjX8W6CG O4zFp0oR74EGGXv+DtCi6vkdWDtBpVI= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-514-rpYu9UVUMN-F_aNSV4HytQ-1; Thu, 12 Nov 2020 10:16:34 -0500 X-MC-Unique: rpYu9UVUMN-F_aNSV4HytQ-1 Received: by mail-wm1-f72.google.com with SMTP id 8so1854334wmg.6 for ; Thu, 12 Nov 2020 07:16:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=mgdXZHtHPywKX/a9hS8LUB6+ABxOjrBQbybE/K2MyA4=; b=Imi5cmjOn4+co+lieMWXqD0AV+l8/SMjMThQCzXTAzkx+2iTq6r//yfTBfsFwmqh/N zpRIa5LQHQYWsY6yV3lHZRzjgUWijgOCujFRmFqrhAiZqekFyb62yx4wavLG1b1gskF6 +3gIi/UZd7VbkEomp+9P1aWgZghMOgTUJOdlLDnULr5vUPIu+VA+LCF1g6Wds6wBmOJ8 isGINQQS5/p4thku4s0mjDHlLQKr2AhXjDzANqn9Z/jWlHJskCJmfxhAGungf8SQDBJR AprSBHW5jaNN15mlruESxpmSbqdD1NJS+GkqslLgK2t7mCAMkMHGxxT15huD0pmTyDts ti9Q== X-Gm-Message-State: AOAM533eooDzICoY5wVCx34nWh3P0nFLzmSWRZblodj9mrvVwLuaspz9 +J1+WkWxrI7zYfrLC8yOkFVIjq0W8EdyHmKege0aBrHFlvfuAKEC6Tea8EPatmAbJdtqQZB6vNr RgfXI108Chm7JiOPmGBcxGXGG X-Received: by 2002:a05:6000:c7:: with SMTP id q7mr3765131wrx.137.1605194193098; Thu, 12 Nov 2020 07:16:33 -0800 (PST) X-Received: by 2002:a05:6000:c7:: with SMTP id q7mr3765087wrx.137.1605194192885; Thu, 12 Nov 2020 07:16:32 -0800 (PST) Received: from vitty.brq.redhat.com (g-server-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id y11sm6415716wmj.36.2020.11.12.07.16.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Nov 2020 07:16:32 -0800 (PST) From: Vitaly Kuznetsov To: Wei Liu , Linux on Hyper-V List Cc: virtualization@lists.linux-foundation.org, Linux Kernel List , Michael Kelley , Vineeth Pillai , Sunil Muthuswamy , Nuno Das Neves , Wei Liu , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , "H. Peter Anvin" Subject: Re: [PATCH v2 02/17] x86/hyperv: detect if Linux is the root partition In-Reply-To: <20201105165814.29233-3-wei.liu@kernel.org> References: <20201105165814.29233-1-wei.liu@kernel.org> <20201105165814.29233-3-wei.liu@kernel.org> Date: Thu, 12 Nov 2020 16:16:30 +0100 Message-ID: <87lff6y59t.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Wei Liu writes: > 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 > --- > arch/x86/hyperv/hv_init.c | 4 ++++ > arch/x86/include/asm/hyperv-tlfs.h | 10 ++++++++++ > arch/x86/include/asm/mshyperv.h | 2 ++ > arch/x86/kernel/cpu/mshyperv.c | 16 ++++++++++++++++ > 4 files changed, 32 insertions(+) > > diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c > index e04d90af4c27..533fe9e887f2 100644 > --- a/arch/x86/hyperv/hv_init.c > +++ b/arch/x86/hyperv/hv_init.c > @@ -26,6 +26,10 @@ > #include > #include > > +/* Is Linux running as the root partition? */ > +bool hv_root_partition; > +EXPORT_SYMBOL_GPL(hv_root_partition); (Nitpick and rather a personal preference): I'd prefer 'hv_partition_is_root' for a boolean. > + > void *hv_hypercall_pg; > EXPORT_SYMBOL_GPL(hv_hypercall_pg); > > diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hyperv-tlfs.h > index 0ed20e8bba9e..41b628b9fb15 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_HYPERVISOR_PRESENT_BIT 0x80000000 > @@ -103,6 +104,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; Eventually this is not going to be an x86 only thing I believe? > + > #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 05ef1f4550cb..f7633e1e4c82 100644 > --- a/arch/x86/kernel/cpu/mshyperv.c > +++ b/arch/x86/kernel/cpu/mshyperv.c > @@ -237,6 +237,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). > + * > + * For now, use the privilege flag as the indicator for running as > + * root. > + */ > + if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_CPU_MANAGEMENT) { We may want to cache cpuid_ebx(HYPERV_CPUID_FEATURES) somewhere but we already had a discussion regading naming for these caches and decided to wait until TLFS for ARM is out so we don't need to rename again. > + hv_root_partition = true; > + pr_info("Hyper-V: running as root partition\n"); > + } > + > /* > * Extract host information. > */ Reviewed-by: Vitaly Kuznetsov -- Vitaly