Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1799705pxu; Tue, 24 Nov 2020 09:11:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJxPrIn/0jvxyLxhTCXqU4RhdRth3Z8gLcAdoUFrQnT2ID50+M7Il1wbC+SRdXb9V1xM7onj X-Received: by 2002:a17:906:4dc2:: with SMTP id f2mr4976562ejw.446.1606237865650; Tue, 24 Nov 2020 09:11:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606237865; cv=none; d=google.com; s=arc-20160816; b=V7LqupK2cCYSSohEJEhoPMshpFkPzTS3oIuEeIyfGjz06GqsXsqWS+2idpM/Qm6JIV KZ4R8zf7R2JQQTvojTJYp00/trZRchJq89Sud51Mf/e3VKzWz36Ek6QHzAXwda5nijRp /6DPPrdGbvV34NLbxXsxz6nYM4OwgTpWyl4PEoBl9g6l+n5tQrkqkEA+cIJVDxCLmHj8 OYVsjoyy8T2Us0xe7SLag90T2UAIx4l36RaBsPn7RAU+zrGmMmcLkHWsOiTpWTf+cfS4 70DduAdh0JICEkBje9cmIIrYrGPSDPVN4ULvJ5q+mXp2fCOFjD8q70cgMaXwrugDd8Hv qKxQ== 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=VVy/xX+hd+uke+LnhxDQ835QhIU9Q889rn2MT+mb/6A=; b=f59qVlc4+1bBxVHaBYjQmYh9EhweZ+r3PRYe9hCgcktYTJ4KYnz2maMTwo6bMcVpnJ 9hPtgzJyeph16w4LXeRnZOxtheZOwJLcPA5FDNDBvCatu3OVuz9k/I8Sy8W9StciYFHj D6yVNYNVDGqLE4S5EN3kce3Xe6fRMaXhyJ0/8504cn5UnZZ2QU5EpOtjrJJXQsanD3pt UCl6WanFnNbFkHxjHoFzHoSfaj1M5/gLUXArWjY6GzhTtdJUC4uw8w202tS2VCtg0a7B +ns8LCtWLeDKf4+dTF9ZzU4B6d3CYsK02I0pbFAm2Of2GqjvZYu0GTrhXmZJvbWAsn7E tgCw== 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 z13si8696670ejf.662.2020.11.24.09.10.40; Tue, 24 Nov 2020 09:11:05 -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 S2390621AbgKXRHx (ORCPT + 99 others); Tue, 24 Nov 2020 12:07:53 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:33399 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390461AbgKXRHw (ORCPT ); Tue, 24 Nov 2020 12:07:52 -0500 Received: by mail-wm1-f68.google.com with SMTP id p19so2458878wmg.0; Tue, 24 Nov 2020 09:07:51 -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=VVy/xX+hd+uke+LnhxDQ835QhIU9Q889rn2MT+mb/6A=; b=Q7Y+gaLwMP3reN6igDkuGSVnbDVXRwUqGKBAyUyv1ByiCxwvlIY8W61uDjY2yK6Kl4 jrxKmjJDamEQROjlh1rspW5hBt8I2d8Nc9BL8EnMsUSexah///UlGUK4yr269BSOM8/B cj5h7z1Y3v3b3xqria2okot7z/MnJzcjHeGuQRA9uAci0ouVzGIN5OHeNOA8m1Wbgowi nUWVcpKK7nr2N60lHvVdTGouh3hDDY0Ff3yOlsvJoqwnLUtvgVTIYdHMcEeDQwt1dusl g6jsNchZGZ/B58GQN6eLJuj0GB8RE5RPaYTKA7rFBkxF9ccUs+MRXLonT34NKALJpHW1 UFxQ== X-Gm-Message-State: AOAM5329TPuU17NMmIuHnFUJG07YsDlYIcRSCChDq0pbsd01ZwJmX71L PQb8GhJAL0+pbJ5y+i81my1xWnkM+g8= X-Received: by 2002:a1c:98cd:: with SMTP id a196mr5647237wme.42.1606237670128; Tue, 24 Nov 2020 09:07:50 -0800 (PST) Received: from liuwe-devbox-debian-v2.j3c5onc20sse1dnehy4noqpfcg.zx.internal.cloudapp.net ([51.145.34.42]) by smtp.gmail.com with ESMTPSA id v20sm6419874wmh.44.2020.11.24.09.07.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Nov 2020 09:07:49 -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 v3 02/17] x86/hyperv: detect if Linux is the root partition Date: Tue, 24 Nov 2020 17:07:29 +0000 Message-Id: <20201124170744.112180-3-wei.liu@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201124170744.112180-1-wei.liu@kernel.org> References: <20201124170744.112180-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 --- 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 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..f0b8c702c858 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). + * + * 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