From patchwork Wed Jan 22 20:23:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 11346347 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 0B59E92A for ; Wed, 22 Jan 2020 20:25:02 +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 DBC5624655 for ; Wed, 22 Jan 2020 20:25:01 +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="ek8LREQW" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DBC5624655 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 1iuMXf-0006FG-GE; Wed, 22 Jan 2020 20:23:59 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iuMXd-0006F4-W4 for xen-devel@lists.xenproject.org; Wed, 22 Jan 2020 20:23:58 +0000 X-Inumbo-ID: 18ff56fe-3d55-11ea-9fd7-bc764e2007e4 Received: from mail-wr1-x444.google.com (unknown [2a00:1450:4864:20::444]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 18ff56fe-3d55-11ea-9fd7-bc764e2007e4; Wed, 22 Jan 2020 20:23:49 +0000 (UTC) Received: by mail-wr1-x444.google.com with SMTP id c9so489391wrw.8 for ; Wed, 22 Jan 2020 12:23:49 -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=hpuNmbi3NIrtI9XWZDVNmn3N4Ap+/HsCumpfsYCUB1I=; b=ek8LREQWmkE4UZkuzuoFdRiExdWuOvPpFXnb/VaGIGsKeRHaFzssk0985Q0dL7MW1h 29jo8FjCpdspmZTenycoLce5Rs9Y5xaqUNivkmne0tPC4tN3Ai7awP+S//InxVUk8wS4 snPen59LdYtXNbAX44gqs5anvqHIz4ZEDkNePNzyDX8O0yP+jfWn0Sbqd58WfRfl+rF7 JVPrhAeW4ucnXoAExhm3SMS+O9ljvWX39Mn4nUhwYijPtPvTkpopxjaFg6O0W5CnK/uL UrABcI80LQf6vgKf56bHCiLePhNZtLFNp8EirB9vsqUsmQSH8zRpeYqgaR31ywfTiJ5M Q51A== 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=hpuNmbi3NIrtI9XWZDVNmn3N4Ap+/HsCumpfsYCUB1I=; b=Snrf4/hyeQCE2FNs/RMf+eXBfPeCtqVSEdo4nHgHWjZZQCe0crdst8wrWhyqGcZyJ4 9zqI767Epi96Abf/mJfO6PfON5GYz9WjC06Ossb02gnJQfTsX3e+hl7zcymjS5pV3X2q nkp2KRlWE5rY4qTIdySe0Xu7SWY63LizvzCWZ1i/BeyAzVlKKSoBBPNhBAx9S9bzToT1 X6B74FBFjgQow2pWh8cZfttaw0rmNQxv6wyhcCoIPDXlSaBY6wV82emCYE0kxTEfSuOz mQ6Ylt29URAlt+mXdHs99+UETWpJucQEA3JYEOsQ8fRtBp7Bzo8SslVLfTSsIv+Y/rw8 SHbQ== X-Gm-Message-State: APjAAAVHPjcC4mYRDkzIx8uv7Dkb0tcg+kGfRLY9gUZ0obS/uAkl8ehL ZN0eoUJP9OfsuiKlpGY0coI4OoAn8PrTwg== X-Google-Smtp-Source: APXvYqyioxSeD5rpVA3j5CgzuYV6XmhP+dr2avBDbBfm6ov1Sv/aGALiB9fl3Zui9KSiTbHvPy5bdg== X-Received: by 2002:a5d:6048:: with SMTP id j8mr13625476wrt.41.1579724628294; Wed, 22 Jan 2020 12:23:48 -0800 (PST) Received: from localhost.localdomain (41.142.6.51.dyn.plus.net. [51.6.142.41]) by smtp.gmail.com with ESMTPSA id r6sm60267992wrq.92.2020.01.22.12.23.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2020 12:23:47 -0800 (PST) From: Wei Liu X-Google-Original-From: Wei Liu To: Xen Development List Date: Wed, 22 Jan 2020 20:23:38 +0000 Message-Id: <20200122202343.5703-3-liuwe@microsoft.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200122202343.5703-1-liuwe@microsoft.com> References: <20200122202343.5703-1-liuwe@microsoft.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v4 2/7] 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 , Paul Durrant , 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" Use the top-most addressable page for that purpose. Adjust e820 code accordingly. We also need to register Xen's guest OS ID to Hyper-V. Use 0x300 as the OS type. Signed-off-by: Wei Liu --- XXX the decision on Xen's vendor ID is pending. v4: 1. Use fixmap 2. Follow routines listed in TLFS --- xen/arch/x86/e820.c | 41 +++++++++++++++---- xen/arch/x86/guest/hyperv/hyperv.c | 53 +++++++++++++++++++++++-- xen/include/asm-x86/guest/hyperv-tlfs.h | 5 ++- 3 files changed, 86 insertions(+), 13 deletions(-) diff --git a/xen/arch/x86/e820.c b/xen/arch/x86/e820.c index 082f9928a1..5a4ef27a0b 100644 --- a/xen/arch/x86/e820.c +++ b/xen/arch/x86/e820.c @@ -36,6 +36,22 @@ boolean_param("e820-verbose", e820_verbose); struct e820map e820; struct e820map __initdata e820_raw; +static unsigned int find_phys_addr_bits(void) +{ + uint32_t eax; + unsigned int phys_bits = 36; + + eax = cpuid_eax(0x80000000); + if ( (eax >> 16) == 0x8000 && eax >= 0x80000008 ) + { + phys_bits = (uint8_t)cpuid_eax(0x80000008); + if ( phys_bits > PADDR_BITS ) + phys_bits = PADDR_BITS; + } + + return phys_bits; +} + /* * This function checks if the entire range is mapped with type. * @@ -357,6 +373,21 @@ static unsigned long __init find_max_pfn(void) max_pfn = end; } +#ifdef CONFIG_HYPERV_GUEST + { + /* + * We reserve the top-most page for hypercall page. Adjust + * max_pfn if necessary. + */ + unsigned int phys_bits = find_phys_addr_bits(); + unsigned long hcall_pfn = + ((1ull << phys_bits) - 1) >> PAGE_SHIFT; + + if ( max_pfn >= hcall_pfn ) + max_pfn = hcall_pfn - 1; + } +#endif + return max_pfn; } @@ -420,7 +451,7 @@ static uint64_t __init mtrr_top_of_ram(void) { uint32_t eax, ebx, ecx, edx; uint64_t mtrr_cap, mtrr_def, addr_mask, base, mask, top; - unsigned int i, phys_bits = 36; + unsigned int i, phys_bits; /* By default we check only Intel systems. */ if ( e820_mtrr_clip == -1 ) @@ -446,13 +477,7 @@ static uint64_t __init mtrr_top_of_ram(void) return 0; /* Find the physical address size for this CPU. */ - eax = cpuid_eax(0x80000000); - if ( (eax >> 16) == 0x8000 && eax >= 0x80000008 ) - { - phys_bits = (uint8_t)cpuid_eax(0x80000008); - if ( phys_bits > PADDR_BITS ) - phys_bits = PADDR_BITS; - } + phys_bits = find_phys_addr_bits(); addr_mask = ((1ull << phys_bits) - 1) & ~((1ull << 12) - 1); rdmsrl(MSR_MTRRcap, mtrr_cap); diff --git a/xen/arch/x86/guest/hyperv/hyperv.c b/xen/arch/x86/guest/hyperv/hyperv.c index 8d38313d7a..f986c1a805 100644 --- a/xen/arch/x86/guest/hyperv/hyperv.c +++ b/xen/arch/x86/guest/hyperv/hyperv.c @@ -18,17 +18,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) +{ + uint64_t id = 0; + + id = (uint64_t)HV_XEN_VENDOR_ID << 48; + id |= (xen_major_version() << 16) | xen_minor_version(); + + return id; +} +static const struct hypervisor_ops ops; const struct hypervisor_ops *__init hyperv_probe(void) { uint32_t eax, ebx, ecx, edx; @@ -72,6 +82,43 @@ 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; + + 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 = ((1ull << paddr_bits) - 1) >> HV_HYP_PAGE_SHIFT; + 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; + } + + set_fixmap_x(FIX_X_HYPERV_HCALL, mfn << PAGE_SHIFT); +} + +static void __init setup(void) +{ + setup_hypercall_page(); +} + +static const struct hypervisor_ops ops = { + .name = "Hyper-V", + .setup = setup, +}; + /* * 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..5d37efd2d2 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 0x100, FreeBSD 0x200, Xen 0x300 * 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;