From patchwork Tue Feb 4 15:36:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 11364883 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D2A7F92A for ; Tue, 4 Feb 2020 15:38:32 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id AF58320674 for ; Tue, 4 Feb 2020 15:38:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oWFuSr55" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AF58320674 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iz0Gf-0001TL-1N; Tue, 04 Feb 2020 15:37:37 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iz0Gd-0001Sd-AF for xen-devel@lists.xenproject.org; Tue, 04 Feb 2020 15:37:35 +0000 X-Inumbo-ID: 379c1b88-4764-11ea-b211-bc764e2007e4 Received: from mail-wr1-x443.google.com (unknown [2a00:1450:4864:20::443]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 379c1b88-4764-11ea-b211-bc764e2007e4; Tue, 04 Feb 2020 15:37:14 +0000 (UTC) Received: by mail-wr1-x443.google.com with SMTP id z3so23679672wru.3 for ; Tue, 04 Feb 2020 07:37:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2o+ZbfiA0a6Wu3YZ4de0M249TmV6XmlurF8HVqhhL2Q=; b=oWFuSr55Wpmndzs8SN9c2Lm8sz56GbSC8F/2KL3FZRjz8eEfcSd5uhjMc65fB5slqy SXgmCXzheWUP4Rwm4Vd6z/Z0P7L++XeY7X0NFg7e09LkpDSN2ac6DWTP2/rJju+YQEDk btML7Rs+qc+07Gu5fwV5iTlINZNTOz1yPcFAxL/ExNUWbMza8ZEV36MwENVZqHWGFeQN GOiLG6anKwQZd+DgI8PsScz5svgaEm8t0PqIRv+wXRF7yIGSzrYaUmtKlt9wLK5ZFJti r820oVZz5jEE06eQ/Hwmv7b+jHCYRN13oW8IehDHIGVYSmLkZfRj5m/mAg3o3cE123mg k7ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=2o+ZbfiA0a6Wu3YZ4de0M249TmV6XmlurF8HVqhhL2Q=; b=bGpNQ/DVt8+m64F2tm5uLlfskhcanslspMyBQs0R48+RKBDkc2CbiFzwnK/3ETA6mK SdVFua9glMfZenbPAGWvLrXLwURaE+lv77YEUTogCs5FCW1AQWAWlS3A0MAUngYd7yQY TIRLVVeQhvUEiRJi9L2dh4IV0g0/DVIQNxsWa1BFmbemsPRI0FuK4AFKjZ2Nat6Mv9H9 Iy88+VTRObnUu3eCRP/VvDrVrFN+H3AJMPHxkMGQbsNS+dcArbHxRIywZpo7TyF//QS3 1jVK3bXhp3u/uTcRFWTmyBkz6mqscJMDMBWy3VvDO5KasIZQxCi5e+auuRB6SOmt+1vo kPwg== X-Gm-Message-State: APjAAAVSM7MaB61npQte4e68KhH2R9hmz80x9njy0kwAugkOuYrkUhUX Tjk2Bpqbyekuwf7oU/SKkeRrcQkxGTM= X-Google-Smtp-Source: APXvYqzufoR5C7xLbPZeGeTwiN703d6qgtrbLKyDC4fFTo3mtRKYZor7Q+HtnQQG/l8VfX+zhoY2XQ== X-Received: by 2002:adf:cd92:: with SMTP id q18mr22223567wrj.261.1580830633811; Tue, 04 Feb 2020 07:37:13 -0800 (PST) Received: from localhost.localdomain (41.142.6.51.dyn.plus.net. [51.6.142.41]) by smtp.gmail.com with ESMTPSA id p26sm4067143wmc.24.2020.02.04.07.37.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2020 07:37:13 -0800 (PST) From: Wei Liu X-Google-Original-From: Wei Liu To: Xen Development List Date: Tue, 4 Feb 2020 15:36:59 +0000 Message-Id: <20200204153704.15934-6-liuwe@microsoft.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200204153704.15934-1-liuwe@microsoft.com> References: <20200204153704.15934-1-liuwe@microsoft.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v7 05/10] x86/hyperv: setup hypercall page X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Wei Liu , Wei Liu , Andrew Cooper , Paul Durrant , Michael Kelley , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Hyper-V uses a technique called overlay page for its hypercall page. It will insert a backing page to the guest when the hypercall functionality is enabled. That means we can use a page that is not backed by real memory for hypercall page. To avoid shattering L0 superpages and treading on any MMIO areas residing in low addresses, use the top-most addressable page for that purpose. Adjust e820 map accordingly. We also need to register Xen's guest OS ID to Hyper-V. Use 0x3 as the vendor ID. Fix the comment in hyperv-tlfs.h while at it. Signed-off-by: Wei Liu Reviewed-by: Roger Pau Monné Acked-by: Paul Durrant --- v7: 1. Fix a style issue 2. Initialise ID to 0 3. Update commit message v6: 1. Use hv_guest_os_id 2. Use new e820_fixup hook 3. Add a BUILD_BUG_ON v5: 1. use hypervisor_reserve_top_pages 2. add a macro for hypercall page mfn 3. address other misc comments v4: 1. Use fixmap 2. Follow routines listed in TLFS --- xen/arch/x86/guest/hyperv/hyperv.c | 69 +++++++++++++++++++++++-- xen/include/asm-x86/guest/hyperv-tlfs.h | 5 +- xen/include/asm-x86/guest/hyperv.h | 3 ++ 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/xen/arch/x86/guest/hyperv/hyperv.c b/xen/arch/x86/guest/hyperv/hyperv.c index 8d38313d7a..2e20a96f30 100644 --- a/xen/arch/x86/guest/hyperv/hyperv.c +++ b/xen/arch/x86/guest/hyperv/hyperv.c @@ -19,15 +19,27 @@ * Copyright (c) 2019 Microsoft. */ #include +#include +#include #include #include +#include struct ms_hyperv_info __read_mostly ms_hyperv; -static const struct hypervisor_ops ops = { - .name = "Hyper-V", -}; +static uint64_t generate_guest_id(void) +{ + union hv_guest_os_id id = {}; + + id.vendor = HV_XEN_VENDOR_ID; + id.major = xen_major_version(); + id.minor = xen_minor_version(); + + return id.raw; +} + +static const struct hypervisor_ops ops; const struct hypervisor_ops *__init hyperv_probe(void) { @@ -72,6 +84,57 @@ const struct hypervisor_ops *__init hyperv_probe(void) return &ops; } +static void __init setup_hypercall_page(void) +{ + union hv_x64_msr_hypercall_contents hypercall_msr; + union hv_guest_os_id guest_id; + unsigned long mfn; + + BUILD_BUG_ON(HV_HYP_PAGE_SHIFT != PAGE_SHIFT); + + rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw); + if ( !guest_id.raw ) + { + guest_id.raw = generate_guest_id(); + wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id.raw); + } + + rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); + if ( !hypercall_msr.enable ) + { + mfn = HV_HCALL_MFN; + hypercall_msr.enable = 1; + hypercall_msr.guest_physical_address = mfn; + wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); + } + else + mfn = hypercall_msr.guest_physical_address; + + rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); + BUG_ON(!hypercall_msr.enable); + + set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT); +} + +static void __init setup(void) +{ + setup_hypercall_page(); +} + +static void __init e820_fixup(struct e820map *e820) +{ + uint64_t s = HV_HCALL_MFN << PAGE_SHIFT; + + if ( !e820_add_range(e820, s, s + PAGE_SIZE, E820_RESERVED) ) + panic("Unable to reserve Hyper-V hypercall range\n"); +} + +static const struct hypervisor_ops ops = { + .name = "Hyper-V", + .setup = setup, + .e820_fixup = e820_fixup, +}; + /* * Local variables: * mode: C diff --git a/xen/include/asm-x86/guest/hyperv-tlfs.h b/xen/include/asm-x86/guest/hyperv-tlfs.h index 091e25cdd1..0a0f3398c1 100644 --- a/xen/include/asm-x86/guest/hyperv-tlfs.h +++ b/xen/include/asm-x86/guest/hyperv-tlfs.h @@ -318,15 +318,16 @@ struct ms_hyperv_tsc_page { * * Bit(s) * 63 - Indicates if the OS is Open Source or not; 1 is Open Source - * 62:56 - Os Type; Linux is 0x100 + * 62:56 - Os Type; Linux 0x1, FreeBSD 0x2, Xen 0x3 * 55:48 - Distro specific identification - * 47:16 - Linux kernel version number + * 47:16 - Guest OS version number * 15:0 - Distro specific identification * * */ #define HV_LINUX_VENDOR_ID 0x8100 +#define HV_XEN_VENDOR_ID 0x8300 union hv_guest_os_id { uint64_t raw; diff --git a/xen/include/asm-x86/guest/hyperv.h b/xen/include/asm-x86/guest/hyperv.h index c7a7f32bd5..1a1b47831c 100644 --- a/xen/include/asm-x86/guest/hyperv.h +++ b/xen/include/asm-x86/guest/hyperv.h @@ -21,6 +21,9 @@ #include +/* Use top-most MFN for hypercall page */ +#define HV_HCALL_MFN (((1ull << paddr_bits) - 1) >> HV_HYP_PAGE_SHIFT) + /* * The specification says: "The partition reference time is computed * by the following formula: