From nobody Thu Feb 22 10:31:23 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TgTvb5Dw3z5CMGc; Thu, 22 Feb 2024 10:31:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TgTvb1wJ4z4ktw; Thu, 22 Feb 2024 10:31:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708597883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=S5ZRIYeImaBweCxrnuhMJOKa/e/Agqih8Mc3nXSAVDs=; b=VV+AJmN5V+CVsiHjHjKQQjaC82n36swI+5vjVVGLn4EXhHvvB4SOU0797n/VunRSYAVREO 9Dihi0hu9/TB0fPdEUaRGizxTWiUz7dx+XfJtE09v6/1JA7vDobFaAi4CD1SCgekyrMmZ/ DnNUMt9gTurNBOBNcyelYIUgzkwo7AWkNUfcSF2P7Takmr2J7xzP0fmtrZNOLmiFgAQnAR D4wpuPcTFfnG3sx1PN2z99vu1M8znh87d2o5nQ00g4iQsKvPuK6huk3H4bGJNRe+03iGRi OaG4Vz4gkIktPM9Eg+DzCKHOohzYm6RgeATbp93hNJeqARSuN6gq5iDOV7vX5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1708597883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=S5ZRIYeImaBweCxrnuhMJOKa/e/Agqih8Mc3nXSAVDs=; b=JeB4ybXFQnkfTEifE0lZK6A4tz/jKGrU9ADoOMIU3kV22UotaW5J7XyDT5YGgpIR9f69RK rKz/OUcFmL9C6UMPYUdPmsQqkp+ZLP2PQaMa3D5ZVgr/JJBSXbqjhX8aEbvxBWXzntwy5u NPIhF9TkElPsvrjKGLeb6MUCNZsgrMG3MjWcYQNJJBybJhm/V0RK6XAk9//r6/uLZhDaBa qyr5RiKJyVhkUteigv4eXlibTsZdqElrfp/qGBBehhzLnbvRWNt8OnLzrt73mnt9/hGCxr 364cBjET8768qwQ+7r1b5qatkyn1SoHLV6kGs5ZNmRR2ryhra0UPmBOUKgR8hQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1708597883; a=rsa-sha256; cv=none; b=Vcg9k5jEajIEO9wpwoGUiLbXgFJz4FPqzmWTUUjCOntDV2ynCb/OlqxphXmv51k8dQjvft iQpoVUVzkKVeVtvQo6FtzMNYac9O04zWlicZd8AILrGf9NLK+91RuOmPPXnU5yjlzMlizP 5p0g8a3mJMAa9/FHYxmodd4ricnvrSHTZmr9M+8OppFYtC9firGHpwbP04CIXzxl11H7zb nlNt68f9LribdCyP8zBdeMR1pjIboiqjeXx3gisvp2FReCASOmInSXWG0XOSmk++TlEO55 Xnj8EoHTkPiLoxCWTnT7WPZPmtiF/zqW7fnEmcc398TvrcH+0mpadFpWY5E/Hg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TgTvb0wsvzS5X; Thu, 22 Feb 2024 10:31:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 41MAVN7J016802; Thu, 22 Feb 2024 10:31:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 41MAVNLv016799; Thu, 22 Feb 2024 10:31:23 GMT (envelope-from git) Date: Thu, 22 Feb 2024 10:31:23 GMT Message-Id: <202402221031.41MAVNLv016799@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Roger Pau =?utf-8?Q?Monn=C3=A9?= Subject: git: 9a687d1fe3af - main - x86/xen: introduce a Xen early init function List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: royger X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 9a687d1fe3af460cdc39c3ed08d1e33cc99b8141 Auto-Submitted: auto-generated The branch main has been updated by royger: URL: https://cgit.FreeBSD.org/src/commit/?id=9a687d1fe3af460cdc39c3ed08d1e33cc99b8141 commit 9a687d1fe3af460cdc39c3ed08d1e33cc99b8141 Author: Roger Pau Monné AuthorDate: 2024-02-02 10:00:31 +0000 Commit: Roger Pau Monné CommitDate: 2024-02-22 10:08:04 +0000 x86/xen: introduce a Xen early init function Start by moving the hyeprcall setup to such function. The aim is to have a function that does all the required Xen early initialization for both HVM and PVH, instead of having it scattered across different paths. Sponsored by: Cloud Software Group Reviewed by: markj Differential revision: https://reviews.freebsd.org/D43932 --- sys/x86/include/xen/xen-os.h | 3 +++ sys/x86/xen/hvm.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ sys/x86/xen/pv.c | 9 ++++----- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/sys/x86/include/xen/xen-os.h b/sys/x86/include/xen/xen-os.h index 12942375be43..ec0d4b1ab9f1 100644 --- a/sys/x86/include/xen/xen-os.h +++ b/sys/x86/include/xen/xen-os.h @@ -96,6 +96,9 @@ xen_pv_nics_disabled(void) bool xen_has_iommu_maps(void); +/* (Very) early initialization. */ +void xen_early_init(void); + #endif /* !__ASSEMBLY__ */ #endif /* _MACHINE_X86_XEN_XEN_OS_H_ */ diff --git a/sys/x86/xen/hvm.c b/sys/x86/xen/hvm.c index 6411b790a2be..9d9a64dd29ea 100644 --- a/sys/x86/xen/hvm.c +++ b/sys/x86/xen/hvm.c @@ -41,8 +41,10 @@ #include +#include #include #include +#include #include #include @@ -184,6 +186,52 @@ out: return (0); } +/* + * Translate linear to physical address when still running on the bootloader + * created page-tables. + */ +static vm_paddr_t +early_init_vtop(void *addr) +{ + + /* + * Using a KASSERT won't print anything, as this is before console + * initialization. + */ + if (__predict_false((uintptr_t)addr < KERNBASE)) { + xc_printf("invalid linear address: %#lx\n", (uintptr_t)addr); + halt(); + } + + return ((uintptr_t)addr - KERNBASE +#ifdef __amd64__ + + kernphys - KERNLOAD +#endif + ); +} + +/* Early initialization when running as a Xen guest. */ +void +xen_early_init(void) +{ + uint32_t regs[4]; + + xen_cpuid_base = xen_hvm_cpuid_base(); + if (xen_cpuid_base == 0) + return; + + /* Find the hypercall pages. */ + do_cpuid(xen_cpuid_base + 2, regs); + if (regs[0] != 1) { + xc_printf("Invalid number of hypercall pages %u\n", + regs[0]); + vm_guest = VM_GUEST_VM; + return; + } + + wrmsr(regs[1], early_init_vtop(&hypercall_page)); +} + static void xen_hvm_init_shared_info_page(void) { diff --git a/sys/x86/xen/pv.c b/sys/x86/xen/pv.c index 75c345ff3a2c..3c22d9e5cf6f 100644 --- a/sys/x86/xen/pv.c +++ b/sys/x86/xen/pv.c @@ -162,14 +162,13 @@ hammer_time_xen(vm_paddr_t start_info_paddr) struct xen_add_to_physmap xatp; uint64_t physfree; char *kenv; - int rc; if (isxen()) { vm_guest = VM_GUEST_XEN; - rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY); - if (rc) { - xc_printf("ERROR: failed to initialize hypercall page: %d\n", - rc); + xen_early_init(); + if (xen_cpuid_base == 0) { + xc_printf( + "ERROR: failed to initialize hypercall page\n"); HYPERVISOR_shutdown(SHUTDOWN_crash); } }