Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2348369pxk; Mon, 14 Sep 2020 10:53:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQ/63AA/RrjK71wXlX8VcBY9odSKMPi1i1qTbZnwnBr7E7vzozunSVfXKNoY02PnvHTMFQ X-Received: by 2002:a50:8d57:: with SMTP id t23mr18732158edt.308.1600106015587; Mon, 14 Sep 2020 10:53:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600106015; cv=none; d=google.com; s=arc-20160816; b=igohPRweGjn4n5dUlL7RNTkSjv/u0xiVlBpnFSdHFs/iXG5O87IjcSlbvz8faf1lFR 0EQ9fE71fstX+JSXlJANu63qTahs+yRykCzo/qDLoaFHhqQOv0n25V4pMONAf4JzgUXH lO3wCzMcaBkeKk78Ykn7ElyZELPsjIGtI26+g3Ycq/nGcohVXyuzU+8ZCmO33x/HOnBQ 0Pwc6rlN3afz3VFcs0GZFTPwXHDJWremD1KQwFdzf8rkBxIILWIwGAjcSSUBFT6sHBpc 8soiBGzcPgtvTdMqAzA4VA8oRkywfWVpsxL+KF7tqi9iGvZ2/K57iLOSIWWHBOMIz3in N8Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=gy81tDRIYMaqrShjpbYnWa6qUW0GqByGNBBg1HW1d6U=; b=XcKhJlG+8zoOcSKY07Q02ZPftPA1y70N8YRh8GfNd1ognNnNzovBdbCLsGo3y+FOfL OYJZSpkMTAWmOHJNXIrrFdUY1x+ivw67aEfxRSIUGPy+0Fe4wISeIs5IBfjbcwEfBPhp CRcV8NTLkdgg8ITDA1v41c7mr2hJVViV1vyO4AubqqkBkgL+e2aGZS9qzYj34HQ3PDAC Zs1sGj80weJB8LbMEd6f0ehtP+zO/4+mrCdy3l133hdCb5Ecoi2uv/7LBZEoVdPMqP83 2+1RNZMU0MkwpQ4oFbuMsN7+ka+SpdNdQ+M74/7kGqiTBYrEF2OK+b72ygAFPDOObys1 dseg== 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 qx23si7339706ejb.355.2020.09.14.10.53.13; Mon, 14 Sep 2020 10:53:35 -0700 (PDT) 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 S1726053AbgINRuo (ORCPT + 99 others); Mon, 14 Sep 2020 13:50:44 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:52039 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725951AbgINL2P (ORCPT ); Mon, 14 Sep 2020 07:28:15 -0400 Received: by mail-wm1-f66.google.com with SMTP id w2so10266656wmi.1; Mon, 14 Sep 2020 04:28:13 -0700 (PDT) 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=gy81tDRIYMaqrShjpbYnWa6qUW0GqByGNBBg1HW1d6U=; b=qzj8UwOvCfS/78OEdWd1qQlAnl6fdDO6nlmeCtr1DSfjzGt6wjpGNJQrRzU4G5uwh8 ywgWEDDPa0PsoUdlc9TItQ1wt1S9OeWt1bxxHhPcqW32NooFF7ZcHz4kv/vtmau5+ebJ 1jH/PpMjdrJjBx+M1D25/sKK1SE8JErdgSEo7bpxg0IujSPLmYufg5HyfXYxVuywGgMS oqwoSf0KYXuVsJ5uJtcYGlyXtwhjARbApulQv0e3tah/VOIoeOc+/mcg+hQtQ8fN5CRY wZc04aYI3PAbXwlcA74u5W387prjeOlEUXtHKfZnccjbrpa+ONAtnsiJFIDaAvK/Z5Vy RZ3w== X-Gm-Message-State: AOAM533tK0ylHHWW/epM5/bTtKQVZJKB+7ASEJADU3kegwkrL8HI9sCe d/VMk6D+VGPZ0+tXB2/KW5yzHOXKJHo= X-Received: by 2002:a1c:35c5:: with SMTP id c188mr14903752wma.11.1600082892846; Mon, 14 Sep 2020 04:28:12 -0700 (PDT) Received: from liuwe-devbox-debian-v2.j3c5onc20sse1dnehy4noqpfcg.zx.internal.cloudapp.net ([51.145.34.42]) by smtp.gmail.com with ESMTPSA id s12sm12024606wmd.20.2020.09.14.04.28.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Sep 2020 04:28:12 -0700 (PDT) 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 RFC v1 02/18] x86/hyperv: detect if Linux is the root partition Date: Mon, 14 Sep 2020 11:27:46 +0000 Message-Id: <20200914112802.80611-3-wei.liu@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200914112802.80611-1-wei.liu@kernel.org> References: <20200914112802.80611-1-wei.liu@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org 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 6035df1b49e1..cac8e4c56261 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 7a4d2062385c..20d628c1ed50 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 @@ -136,6 +137,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 60b944dd2df1..2a2cc81beac6 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -224,6 +224,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 af94f05a5c66..1bf57d310f78 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -232,6 +232,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