From patchwork Fri Jan 31 17:49:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 11360333 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 0D23992A for ; Fri, 31 Jan 2020 17:50:54 +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 DD60A206F0 for ; Fri, 31 Jan 2020 17:50:53 +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="Re3AWwiA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DD60A206F0 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 1ixaQc-000640-7e; Fri, 31 Jan 2020 17:50:02 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1ixaQa-0005yg-JA for xen-devel@lists.xenproject.org; Fri, 31 Jan 2020 17:50:00 +0000 X-Inumbo-ID: 0ef49c10-4452-11ea-ad98-bc764e2007e4 Received: from mail-wr1-x441.google.com (unknown [2a00:1450:4864:20::441]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 0ef49c10-4452-11ea-ad98-bc764e2007e4; Fri, 31 Jan 2020 17:49:42 +0000 (UTC) Received: by mail-wr1-x441.google.com with SMTP id y11so9664966wrt.6 for ; Fri, 31 Jan 2020 09:49:42 -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=rffWGupMx/0ss2WkO2hKOAHYYbHAdj5XTaScjilRkJo=; b=Re3AWwiAYDMHkhWreBGqdd2p8pQI6wvZKfsi/j3j4Dngm/Ld0aPvbr1/XbFEFJdU7H xxrD3ihqxY1PcxavR+lR/IUR9TlNINeAZT60jL/4KmN6QPunSvJjPbd9jkqv50dC8W4m Bn0y5L882CvLI54t/GXmk9YHfqv2f2ejeKsa8rKoNlt2G+MQUzII02hZosX4Yrv8n1eY qokl4f2ANzaVqb05GHj6ilj9h4agPYL/8+fCNDiUUYOe7zWWMc7cJ4q7zsXwBFvaxuTU NgLGXS9+rYqJhESXFeQiuK5zt8GNHGmoiLTIYDvg8nOjoOWIiUwyiRukR3ZsmPl5J2kh 6tFQ== 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=rffWGupMx/0ss2WkO2hKOAHYYbHAdj5XTaScjilRkJo=; b=K0xuDazG++TKaiX4gt7L/MwDDgwSM9Zmq2zaaogF1FqJq3j6ZA401nU1YLfQFRjckN Nvgtr9eifeq4liDfA4pBIzK9dne2ESP+swxYu6ezSGCZrItMtDoS6qxdnDjuGJ6a8QWa P8w7oaNVZIipTvfJ3DYh+QUTJ8cZWP1xuDxtZxpcW2zGdnPMRH/59dCXW1y8P8PnyBnL 3XCexLOQQtE8Q9jZzuKqkx5k93g2VZOs0Ooot1EsnGgDvHsz7FS6j0lQvUYbDh+KIiw4 EiGASMyZSGce1WvAPzvfr560OoMEKmltQSeimsFlA8KGffEVx72lbinTLQSKZe1ZNUTe brjg== X-Gm-Message-State: APjAAAXS1/Dr4qb1DbI6WCnj1+DeFHtE8dA8d20RcY8bXHpMqIE+BWWm Yb8AvcVKa5T7I+HCMD65BF14n48bqfg= X-Google-Smtp-Source: APXvYqxHMUW0mba/78Ye5t1QsiVHR3G4FDLX3+2C1XePNLGkYvqQG04xIx2jcdb5e34NMoCAaYQsMw== X-Received: by 2002:a5d:5283:: with SMTP id c3mr13719944wrv.148.1580492981122; Fri, 31 Jan 2020 09:49:41 -0800 (PST) Received: from localhost.localdomain (41.142.6.51.dyn.plus.net. [51.6.142.41]) by smtp.gmail.com with ESMTPSA id 133sm4364352wmd.5.2020.01.31.09.49.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jan 2020 09:49:40 -0800 (PST) From: Wei Liu X-Google-Original-From: Wei Liu To: Xen Development List Date: Fri, 31 Jan 2020 17:49:24 +0000 Message-Id: <20200131174930.31045-6-liuwe@microsoft.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200131174930.31045-1-liuwe@microsoft.com> References: <20200131174930.31045-1-liuwe@microsoft.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v6 05/11] 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 , =?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. 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 --- 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..7c2a96d70e 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 05c4044976..07db57b55f 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: