Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp635942pxb; Thu, 5 Nov 2020 09:04:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJwZjsioAXHQmwNGdIy1XTBmFTlUow2tlMLfowsQx9TLifBAsGW74MyLhCCN3nTLkIjp1naA X-Received: by 2002:a05:6402:22af:: with SMTP id cx15mr2716646edb.207.1604595849711; Thu, 05 Nov 2020 09:04:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604595849; cv=none; d=google.com; s=arc-20160816; b=u5zRc5ox/NKPWHjggYmIcbY/a6St7kOQUBWj4omHH1DW+N5aQpD0j5OK3v05vb5Vce LEAosiERnup5NqHtsEBq2lZmsLZ2rQa+YMER+qMkHmTHTIWS3stU0NtL9WiKw07sGQcA yi5T2i4FWevIOUCd2597UnZ8Nx92cgnrku5fokGbBBB9LnTuIxGD1lt/8YnE4iFxNf2A BV1uiXrT5zhZihRkwwqncRZbUDRpdTyZWFmOfbQgFJTnwr6uQJR6BDxyN4nEygUjJjsI BY17658INO7LjL1HnInTUCfdJQcAcgRHa7nxNjsfXFOCWYXEiZx6NJhRWV0iqk8kC+vk cfeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=rKgjBq0HU0oQ1dSGXORF2WP2Pm6kp1V0XCMd0GgvNN0=; b=z0lgmj/r+PfRpposTjPnthT3Iztik0sSdMbCm17ml35h7AI3ubX/aUvUN6hz7bhHs+ ZOmyR14YuIyEvS3bnRdh/fHRFRRYVaGNjGmA0KaSj58oWsof482cVY/nI5gMIm4IUR3p tEBVDiokkuwTWEK2S+GxiJZNSG5C5R+w9w85RRfOP0ip6E0YrtNaI+pfYj0Tur/wxsst myQYEZFwa4gDbdl4ni9kwF5rN+d0gsAfM9sx/9i15Te3gx96VLY8oXP2oSm3sMpVHC/O txvq9izOtQ5UR5KKYIKdXG/FapNvQhSmjWpozduBOT9cgPZwh8IJ3qq2SORORhQ7qmYN R5Eg== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce29si1761604edb.220.2020.11.05.09.03.44; Thu, 05 Nov 2020 09:04:09 -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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731675AbgKEQ6Y (ORCPT + 99 others); Thu, 5 Nov 2020 11:58:24 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:38047 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731518AbgKEQ6V (ORCPT ); Thu, 5 Nov 2020 11:58:21 -0500 Received: by mail-wm1-f67.google.com with SMTP id h62so2290637wme.3; Thu, 05 Nov 2020 08:58:20 -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:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rKgjBq0HU0oQ1dSGXORF2WP2Pm6kp1V0XCMd0GgvNN0=; b=aQX0Hh6+lded7wx86pMmdnNINITwCvcSTwGgkwXkMWtWCFqvJZnrjESDOlv6iYavXz HF7X5bHERueMD4A0SlEGdFYnyX1WHqd12XE+csw9P4nylDcZH1ldFAT2MJYIWlQRzoYK 9YpfHyU2VMhuDmmRpQX4r5tlqT3GE1sFt/nyWHTbJIHjb+uR/BVTdh8WAoPO3lfWF5vU TWrccIrpubRBywcGkrsO5ZApi7z6jlcSgIMirQl6RPph+wrrFJK9MUrtWKKsDL8IfNEf 4UyQUiczzuCWp416BbTI1A81Wrg25tTpzfox+E1gd50vCYN2sGQXd5daPQrgt3rqIaYb 5c8Q== X-Gm-Message-State: AOAM530isMZ2RVDjSqy1mDlLyvrVxo2somNzdCU+xb5pmIwHsCKUqo3N LlbJ6SzG9qHDQF+jISggZBT8ILef1jQ= X-Received: by 2002:a1c:ed15:: with SMTP id l21mr3939442wmh.26.1604595499326; Thu, 05 Nov 2020 08:58:19 -0800 (PST) Received: from liuwe-devbox-debian-v2.j3c5onc20sse1dnehy4noqpfcg.zx.internal.cloudapp.net ([51.145.34.42]) by smtp.gmail.com with ESMTPSA id z5sm3350729wrw.87.2020.11.05.08.58.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2020 08:58:18 -0800 (PST) From: Wei Liu To: 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 , x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)), "H. Peter Anvin" Subject: [PATCH v2 02/17] x86/hyperv: detect if Linux is the root partition Date: Thu, 5 Nov 2020 16:57:59 +0000 Message-Id: <20201105165814.29233-3-wei.liu@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201105165814.29233-1-wei.liu@kernel.org> References: <20201105165814.29233-1-wei.liu@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); + 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; + #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) { + hv_root_partition = true; + pr_info("Hyper-V: running as root partition\n"); + } + /* * Extract host information. */ -- 2.20.1