From nobody Fri Nov 22 07:07:59 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1728481616; cv=none; d=zohomail.com; s=zohoarc; b=O9cVBX6cZM21Q+Zq/Wm5bx8gy8fgtumwCVpwH8LSEY+U6OWRxXEUuoyVALNQX0/bj6GM4XeCZ/HnpWTvdszMs6XbHQ2mo+RyZZGQaP6t/qIQVWpDThAQ8TU7E1lr7WG1LZQkhdhMwlIQHCjkMmowCHYLk2UznFoO3cMoAosr1dg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1728481616; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eKN+iZaDpNy8yOn7LaIk1Lnug9+dT93sjGvELvafAMY=; b=hWnftbiT04qABpDLhGnhoqwkgh67ArKmDJKbWvTPS1q7sCPAqzlfg1waJxzm+jSiupWvU+tJyNDnFLZ8Mi1INc2HReW97H70//z6lxBPyUVESxfAFY07Mn1tjCkNnltTS08DgCstX5TzduxhtJoNNvF6Fxw/7wPPXdx+l1CGbKQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1728481616243546.9019725888091; Wed, 9 Oct 2024 06:46:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.814531.1228112 (Exim 4.92) (envelope-from ) id 1syX0i-0005PN-Cf; Wed, 09 Oct 2024 13:45:52 +0000 Received: by outflank-mailman (output) from mailman id 814531.1228112; Wed, 09 Oct 2024 13:45:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1syX0i-0005P4-8s; Wed, 09 Oct 2024 13:45:52 +0000 Received: by outflank-mailman (input) for mailman id 814531; Wed, 09 Oct 2024 13:45:51 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1syX0h-0005Gw-Lj for xen-devel@lists.xenproject.org; Wed, 09 Oct 2024 13:45:51 +0000 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [2a00:1450:4864:20::52e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id cb0bbab1-8644-11ef-99a2-01e77a169b0f; Wed, 09 Oct 2024 15:45:49 +0200 (CEST) Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-5c89f3e8a74so9244150a12.0 for ; Wed, 09 Oct 2024 06:45:49 -0700 (PDT) Received: from fedora.. ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c8e0598947sm5435334a12.11.2024.10.09.06.45.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Oct 2024 06:45:47 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: cb0bbab1-8644-11ef-99a2-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728481549; x=1729086349; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eKN+iZaDpNy8yOn7LaIk1Lnug9+dT93sjGvELvafAMY=; b=XSiyFcyRw7MPZ0zG4eDOTCV3nEzyALTa+OQO5xfybV/Wd9k8LFqPsDHNohbHsrRGJl KS8PAQZv+NfK71TGti6yXF2bv3hGZalTLhiFFz74DkQCKwQOrgksrMZCzVo6DiAE1ZSh 8CmajU1XFJ1DkDQU6Mqc6LuJcRHbtX5n3xX2RgaL5XjW75xlxhjKrdXMihoTMWox/p0I RVWdiP1rC65vSdOURoKnT4d1KOt6s2l6gWkzDsqEmzP/9+9wofMD1dgU/2uWH0OqtAQ2 OMkG7XAnEQr4ke97jb/1HdOtjKtURrziaZJcvoqOPo+kdHcFVavMBycAXhzH8QuUo/NT nGhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728481549; x=1729086349; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eKN+iZaDpNy8yOn7LaIk1Lnug9+dT93sjGvELvafAMY=; b=mmHOcDHNsQhtxjxfW6M0gL50KxXevEIZ8oREE8gPIB+oDYvZVdQw/4M4kuriq/UuA9 CcQOedRf8+EdpiwpZT8iQT3GpaP3DB3d/Trs6nWkkQXhOOL4AzPKk9Nh0hqKThKojtnq Id8rOToy38fFiFmz0NFvcCannd2ZQIDv+rnHLn6btp8VxzJc63ZeCOFosZw/IeuzW97O 3U+CY2QH2LTV7RliLA2YWJvNdjFprolaFVSK6HL9GnxuOVRQdnCGtatPi0JLEo4v1SC2 L0BGRC3o4vT/C3DvkQ0DO3gv+Vjw1TjFEIBi1BDqUvnwLIvhxtLS3dn0uDqVp4AQvl5y inPQ== X-Gm-Message-State: AOJu0YyUPNkYsbERGZ2eAnGYadWdePJPyc+TVV1TM9WsahOACWvOHsWc sC+CoBvzquWBDO+4wsgrEkigH5WjkW3UWNuvoP4ODFyEjLJn0XAKONIjPQ== X-Google-Smtp-Source: AGHT+IFFr1X0wG6rMgUWf8llcjPefsJc4bQAC4Pnrsp7GHMqAJvAqYBIqUr91an8k5hG50KpyBmKwA== X-Received: by 2002:a05:6402:3582:b0:5c8:bb09:b417 with SMTP id 4fb4d7f45d1cf-5c91d4062bbmr1982190a12.0.1728481548326; Wed, 09 Oct 2024 06:45:48 -0700 (PDT) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Oleksii Kurochko , Alistair Francis , Bob Eshleman , Connor Davis , Andrew Cooper , Jan Beulich , Julien Grall , Stefano Stabellini Subject: [PATCH v3 1/3] xen/riscv: implement virt_to_maddr() Date: Wed, 9 Oct 2024 15:45:39 +0200 Message-ID: <9f7d9ebde6fc7224490d1fd565676e051c57a80d.1728472163.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1728481618003116600 Content-Type: text/plain; charset="utf-8" Implement the virt_to_maddr() function to convert virtual addresses to machine addresses. The function includes checks for valid address ranges, specifically the direct mapping region (DIRECTMAP_VIRT_START) and the Xen's Linkage (XEN_VIRT_START) region. If the virtual address falls outside of these regions, an assertion will trigger. To implement this, the phys_offset variable is made accessible outside of riscv/mm.c. Signed-off-by: Oleksii Kurochko Acked-by: Jan Beulich --- Changes in v3: - Drop ASSERT(va >=3D (DIRECTMAP_VIRT_START + DIRECTMAP_SIZE)) as it is en= ough to have 'if (...)' for directmap region and ASSERT() for checking that virtual address is from Xen's linkage virtual space. - Add the comment above virt_to_maddr(). - Update the commit message. - Add the comment for declaration and defoition of phys_offset: /* =3D load_start - XEN_VIRT_START */. --- Changes in V2: - Drop casts in virt_to_maddr() for ASSERT which checks that VA is in the range of where Xen is located. - Add UL suffix for or XEN_VIRT_START by using _AC(..., UL) and add inclu= sion of - Add the comment above return which explains why there is no need to do " - XEN_VIRT_START. --- xen/arch/riscv/include/asm/config.h | 4 ++++ xen/arch/riscv/include/asm/mm.h | 22 +++++++++++++++++++++- xen/arch/riscv/mm.c | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/xen/arch/riscv/include/asm/config.h b/xen/arch/riscv/include/a= sm/config.h index ef68281653..ad75871283 100644 --- a/xen/arch/riscv/include/asm/config.h +++ b/xen/arch/riscv/include/asm/config.h @@ -155,6 +155,10 @@ =20 #define IDENT_AREA_SIZE 64 =20 +#ifndef __ASSEMBLY__ +extern unsigned long phys_offset; /* =3D load_start - XEN_VIRT_START */ +#endif + #endif /* ASM__RISCV__CONFIG_H */ /* * Local variables: diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index 5c79f3def3..ebb142502e 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -5,6 +5,7 @@ =20 #include #include +#include #include #include #include @@ -28,7 +29,26 @@ static inline void *maddr_to_virt(paddr_t ma) return NULL; } =20 -#define virt_to_maddr(va) ({ BUG_ON("unimplemented"); 0; }) +/* + * virt_to_maddr() is expected to work with virtual addresses from either + * the directmap region or Xen's linkage (XEN_VIRT_START) region. + * Therefore, it is sufficient to check only these regions and assert if `= va` + * is not within the directmap or Xen's linkage region. + */ +static inline unsigned long virt_to_maddr(unsigned long va) +{ + if ((va >=3D DIRECTMAP_VIRT_START) && + (va < (DIRECTMAP_VIRT_START + DIRECTMAP_SIZE))) + return directmapoff_to_maddr(va - DIRECTMAP_VIRT_START); + + BUILD_BUG_ON(XEN_VIRT_SIZE !=3D MB(2)); + ASSERT((va >> (PAGETABLE_ORDER + PAGE_SHIFT)) =3D=3D + (_AC(XEN_VIRT_START, UL) >> (PAGETABLE_ORDER + PAGE_SHIFT))); + + /* phys_offset =3D load_start - XEN_VIRT_START */ + return phys_offset + va; +} +#define virt_to_maddr(va) virt_to_maddr((unsigned long)(va)) =20 /* Convert between Xen-heap virtual addresses and machine frame numbers. */ #define __virt_to_mfn(va) mfn_x(maddr_to_mfn(virt_to_maddr(va))) diff --git a/xen/arch/riscv/mm.c b/xen/arch/riscv/mm.c index 4a628aef83..daa02b2c60 100644 --- a/xen/arch/riscv/mm.c +++ b/xen/arch/riscv/mm.c @@ -26,7 +26,7 @@ struct mmu_desc { pte_t *pgtbl_base; }; =20 -static unsigned long __ro_after_init phys_offset; +unsigned long __ro_after_init phys_offset; /* =3D load_start - XEN_VIRT_ST= ART */ =20 #define LOAD_TO_LINK(addr) ((unsigned long)(addr) - phys_offset) #define LINK_TO_LOAD(addr) ((unsigned long)(addr) + phys_offset) --=20 2.46.2