From nobody Thu Oct 30 23:24:24 2025 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=1758146203; cv=none; d=zohomail.com; s=zohoarc; b=WBtqaG3zplioRrs6ojpjjSEV3bk2uBfrtipU5/7ymmOL0c8kkv1PLJxbwR9+agN8BDCwQhpY6V5RI6irZKO4MYtERzy3qv65cwNTgoUFGNqQ8UdyUftLKr9YMzVBoct7HWmJOBZ6rrN0KiQivnEFmsEUAa+e7F6fNa0GHSEk0Eo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1758146203; h=Content-Type: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=Vixp4rZSilRtfznTmIcEdZHTZIUxpbam21u9aaI4Ljs=; b=jAAlcIc+LPgGj0V77ozlM+hqn9KRiRJKhoin0KFIU4MI/6IlOPedkX2sU6kWWixDFqPNIf+00dgOYsAa14gmIGX83y9NOgI8lIsH/J+LN+ZDSY6NMDOZvyf21GfpG6d/4wGXGdV5m4kGAqj2Uu8FAzazEWVhLR3IBSS7Y6qGTLU= 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 1758146202991910.4798864669925; Wed, 17 Sep 2025 14:56:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1125595.1467593 (Exim 4.92) (envelope-from ) id 1uz08L-0002RZ-6I; Wed, 17 Sep 2025 21:56:13 +0000 Received: by outflank-mailman (output) from mailman id 1125595.1467593; Wed, 17 Sep 2025 21:56:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uz08K-0002PQ-P5; Wed, 17 Sep 2025 21:56:12 +0000 Received: by outflank-mailman (input) for mailman id 1125595; Wed, 17 Sep 2025 21:56:10 +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 1uz08I-0007Lt-2I for xen-devel@lists.xenproject.org; Wed, 17 Sep 2025 21:56:10 +0000 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [2607:f8b0:4864:20::634]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 1d213bd6-9411-11f0-9809-7dc792cee155; Wed, 17 Sep 2025 23:56:08 +0200 (CEST) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2677a4d4ce3so3389945ad.0 for ; Wed, 17 Sep 2025 14:56:08 -0700 (PDT) Received: from fedora ([149.199.65.200]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-26980053da4sm5538095ad.20.2025.09.17.14.56.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Sep 2025 14:56:05 -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: 1d213bd6-9411-11f0-9809-7dc792cee155 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758146166; x=1758750966; 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=Vixp4rZSilRtfznTmIcEdZHTZIUxpbam21u9aaI4Ljs=; b=EI/Klr43q7NVgGkfblRBupGvdhyPP1NI7/WTxAhz3GkEIETzlPsBIrpPIGtkdDJN5i AKZYko9nq778uA3FKmfg7/+DnT4aXKSJsEPyFkuvsPMTxGJdlnUQFIzPGyuf7HfmSgLk XwKej4xFk0AdO9l/cTguqTuBTQKvU91dXiV4yZKZ99lI+EKklcaIOblBvV7J05fdBkQH wwlo3uzwfb5UmnCQ0pcwqLe0ktf2K202zww7wwnCJfAYrfRZhSzfpqoIE8205u4UFnxi jdp9YFD7hWpdAEIRteCWMgZv2MGvsgCedQgKzj3Y6iUgDXx5HF4fBPb1jkyBxSeowsr8 V2yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758146166; x=1758750966; 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=Vixp4rZSilRtfznTmIcEdZHTZIUxpbam21u9aaI4Ljs=; b=oBjM+y5fxNirYofj9+BWDzbtMuO1hHDA4pZ6fHg6FQ9KbazOlemYDwutrW0KXzaT/p F1GwhvZc7aH5sxdDFgXJoD7oaOaXhneZL9EC/ClW4WXKCniCPIvuI+5PKD5T0KyAKmIm 2+SPWDin0YW1L7WEkGnBMjvNC+vV9FWYd8gN6FwqdFeeSh+Mu0JJPhCi2ArlRLTmoVJ2 4kG0S6LiDblRAT7Yor+GyED50gmmo98Gp4vuzj0s4zRerzVCKXajCYbESo7Aio2Y54gD 1F5W1tk9hlks9n7M92sseomTz+vtuaxoQSKOGQmafixiKV6cdH2II8eF2VQHl+9V+wrK df1w== X-Gm-Message-State: AOJu0Yxi5cgYfVNH+BENzfQGf5ltEoQ3+PqOyM7f8bSQiO/YeaIi71+V npl+0Z3CNCx69mlRQV89AvqaVym307/rN3TH1tGUDC+JPlelKnhGXrea6zHmwJMArsg= X-Gm-Gg: ASbGncui01tujQBX1vJV6BspCG5la5J8lotTWcrnB9WI1vgAE7y17aK1uADA9fb27dr Kvqf2SLBdrArOXjkaheREo16SdU23XadCIY54hMhaQcApaSPf2obXWN8o4zQ9J9U3uzS0Z6ZQVS k462BLIFdMpra02c70wBlBLyPVBmvDDKbk6E7Lj4tLRv5y6zRazdFCEiYWO80vt9e949ZLyRlQE hNfCDRHSZCJnRx3HD6hGgyMWieYVNnxW93Bt2IRXP0XMe78uGQ/2zGd2lT06tSTaq/X0bjQjKtI pqu0pRViNucm76qklfPXneXAhqpwYChJRfaqdth0zqcQ5m9vmhZS6dFcIdP/ztn9wcIkOPGqd+g ToDVzVP9yTqcpldDvnHqiJWoiQ8mPcaefIU8YaLl8uvYX X-Google-Smtp-Source: AGHT+IFNJ3k2Jd6YqvnePVPezggYrFvdtN2cprYFtJsXb3luDpwyEXMpH57sl8ziAFha5F2VyERQWQ== X-Received: by 2002:a17:903:1983:b0:24c:cca1:7cfc with SMTP id d9443c01a7336-26813f01439mr46507205ad.59.1758146166263; Wed, 17 Sep 2025 14:56:06 -0700 (PDT) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Oleksii Kurochko , Alistair Francis , Bob Eshleman , Connor Davis , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v4 12/18] xen/riscv: Implement p2m_pte_from_mfn() and support PBMT configuration Date: Wed, 17 Sep 2025 23:55:32 +0200 Message-ID: <4495c8103548447f9a11963574a4cb9e01090e7a.1758145428.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1758146203912116600 This patch adds the initial logic for constructing PTEs from MFNs in the RI= SC-V p2m subsystem. It includes: - Implementation of p2m_pte_from_mfn(): Generates a valid PTE using the given MFN, p2m_type_t, including permission encoding and PBMT attribute setup. - New helper p2m_set_permission(): Encodes access rights (r, w, x) into the PTE based on both p2m type and access permissions. - p2m_set_type(): Stores the p2m type in PTE's bits. The storage of types, which don't fit PTE bits, will be implemented separately later. - Add detection of Svade extension to properly handle a possible page-fault if A and D bits aren't set. PBMT type encoding support: - Introduces an enum pbmt_type_t to represent the PBMT field values. - Maps types like p2m_mmio_direct_dev to p2m_mmio_direct_io, others default to pbmt_pma. Signed-off-by: Oleksii Kurochko --- Changes in V4: - p2m_set_permission() updates: - Update permissions for p2m_ram_rw case, make it also executable. - Add pernissions setting for p2m_map_foreign_* types. - Drop setting peromissions for p2m_ext_storage. - Only turn off PTE_VALID bit for p2m_invalid, don't touch other bits. - p2m_pte_from_mfn() updates: - Update ASSERT(), add a check that mfn isn't INVALID_MFN (1) explicitly to avoid the case when PADDR_MASK isn't narrow enough to catch the case (1). - Drop unnessary check around call of p2m_set_type() as this check is already included inside p2m_set_type(). - Introduce new p2m type p2m_first_external to detect that passed type is stored in external storage. - Add handling of PTE's A and D bits in pm2_set_permission. Also, set PTE_USER bit. For this cpufeatures.{h and c} were updated to be able to detect availability of Svade extension. - Drop grant table related code as it isn't going to be used at the moment. --- Changes in V3: - s/p2m_entry_from_mfn/p2m_pte_from_mfn. - s/pbmt_type_t/pbmt_type. - s/pbmt_max/pbmt_count. - s/p2m_type_radix_set/p2m_set_type. - Rework p2m_set_type() to handle only types which are fited into PTEs bit= s. Other types will be covered separately. Update arguments of p2m_set_type(): there is no any reason for p2m anymo= re. - p2m_set_permissions() updates: - Update the code in p2m_set_permission() for cases p2m_raw_rw and p2m_mmio_direct_io to set proper type permissions. - Add cases for p2m_grant_map_rw and p2m_grant_map_ro. - Use ASSERT_UNEACHABLE() instead of BUG() in switch cases of p2m_set_permissions. - Add blank lines non-fall-through case blocks in switch cases. - Set MFN before permissions are set in p2m_pte_from_mfn(). - Update prototype of p2m_entry_from_mfn(). --- Changes in V2: - New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller. --- xen/arch/riscv/cpufeature.c | 1 + xen/arch/riscv/include/asm/cpufeature.h | 1 + xen/arch/riscv/include/asm/page.h | 8 ++ xen/arch/riscv/p2m.c | 97 ++++++++++++++++++++++++- 4 files changed, 103 insertions(+), 4 deletions(-) diff --git a/xen/arch/riscv/cpufeature.c b/xen/arch/riscv/cpufeature.c index b846a106a3..02b68aeaa4 100644 --- a/xen/arch/riscv/cpufeature.c +++ b/xen/arch/riscv/cpufeature.c @@ -138,6 +138,7 @@ const struct riscv_isa_ext_data __initconst riscv_isa_e= xt[] =3D { RISCV_ISA_EXT_DATA(zbs), RISCV_ISA_EXT_DATA(smaia), RISCV_ISA_EXT_DATA(ssaia), + RISCV_ISA_EXT_DATA(svade), RISCV_ISA_EXT_DATA(svpbmt), }; =20 diff --git a/xen/arch/riscv/include/asm/cpufeature.h b/xen/arch/riscv/inclu= de/asm/cpufeature.h index 768b84b769..5f756c76db 100644 --- a/xen/arch/riscv/include/asm/cpufeature.h +++ b/xen/arch/riscv/include/asm/cpufeature.h @@ -37,6 +37,7 @@ enum riscv_isa_ext_id { RISCV_ISA_EXT_zbs, RISCV_ISA_EXT_smaia, RISCV_ISA_EXT_ssaia, + RISCV_ISA_EXT_svade, RISCV_ISA_EXT_svpbmt, RISCV_ISA_EXT_MAX }; diff --git a/xen/arch/riscv/include/asm/page.h b/xen/arch/riscv/include/asm= /page.h index cb303af0c0..4fa0556073 100644 --- a/xen/arch/riscv/include/asm/page.h +++ b/xen/arch/riscv/include/asm/page.h @@ -74,6 +74,14 @@ #define PTE_SMALL BIT(10, UL) #define PTE_POPULATE BIT(11, UL) =20 +enum pbmt_type { + pbmt_pma, + pbmt_nc, + pbmt_io, + pbmt_rsvd, + pbmt_count, +}; + #define PTE_ACCESS_MASK (PTE_READABLE | PTE_WRITABLE | PTE_EXECUTABLE) =20 #define PTE_PBMT_MASK (PTE_PBMT_NOCACHE | PTE_PBMT_IO) diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 10acfa0a9c..2d4433360d 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -10,6 +10,7 @@ #include #include =20 +#include #include #include #include @@ -288,6 +289,18 @@ static pte_t *p2m_get_root_pointer(struct p2m_domain *= p2m, gfn_t gfn) return __map_domain_page(p2m->root + root_table_indx); } =20 +static int p2m_set_type(pte_t *pte, p2m_type_t t) +{ + int rc =3D 0; + + if ( t > p2m_first_external ) + panic("unimplemeted\n"); + else + pte->pte |=3D MASK_INSR(t, P2M_TYPE_PTE_BITS_MASK); + + return rc; +} + static p2m_type_t p2m_get_type(const pte_t pte) { p2m_type_t type =3D MASK_EXTR(pte.pte, P2M_TYPE_PTE_BITS_MASK); @@ -318,11 +331,87 @@ static inline void p2m_clean_pte(pte_t *p, bool clean= _pte) p2m_write_pte(p, pte, clean_pte); } =20 -static pte_t p2m_pte_from_mfn(mfn_t mfn, p2m_type_t t) +static void p2m_set_permission(pte_t *e, p2m_type_t t) { - panic("%s: hasn't been implemented yet\n", __func__); + e->pte &=3D ~PTE_ACCESS_MASK; + + e->pte |=3D PTE_USER; + + /* + * Two schemes to manage the A and D bits are defined: + * =E2=80=A2 The Svade extension: when a virtual page is accessed an= d the A bit + * is clear, or is written and the D bit is clear, a page-fault + * exception is raised. + * =E2=80=A2 When the Svade extension is not implemented, the follow= ing scheme + * applies. + * When a virtual page is accessed and the A bit is clear, the PTE= is + * updated to set the A bit. When the virtual page is written and = the + * D bit is clear, the PTE is updated to set the D bit. When G-sta= ge + * address translation is in use and is not Bare, the G-stage virt= ual + * pages may be accessed or written by implicit accesses to VS-lev= el + * memory management data structures, such as page tables. + * Thereby to avoid a page-fault in case of Svade is available, it is + * necesssary to set A and D bits. + */ + if ( riscv_isa_extension_available(NULL, RISCV_ISA_EXT_svade) ) + e->pte |=3D PTE_ACCESSED | PTE_DIRTY; + + switch ( t ) + { + case p2m_map_foreign_rw: + case p2m_mmio_direct_io: + e->pte |=3D PTE_READABLE | PTE_WRITABLE; + break; + + case p2m_ram_rw: + e->pte |=3D PTE_ACCESS_MASK; + break; + + case p2m_invalid: + e->pte &=3D ~PTE_VALID; + break; + + case p2m_map_foreign_ro: + e->pte |=3D PTE_READABLE; + break; + + default: + ASSERT_UNREACHABLE(); + break; + } +} + +static pte_t p2m_pte_from_mfn(mfn_t mfn, p2m_type_t t, bool is_table) +{ + pte_t e =3D (pte_t) { PTE_VALID }; + + switch ( t ) + { + case p2m_mmio_direct_io: + e.pte |=3D PTE_PBMT_IO; + break; + + default: + break; + } + + pte_set_mfn(&e, mfn); + + ASSERT(!(mfn_to_maddr(mfn) & ~PADDR_MASK) || mfn_eq(mfn, INVALID_MFN)); + + if ( !is_table ) + { + p2m_set_permission(&e, t); + p2m_set_type(&e, t); + } + else + /* + * According to the spec and table "Encoding of PTE R/W/X fields": + * X=3DW=3DR=3D0 -> Pointer to next level of page table. + */ + e.pte &=3D ~PTE_ACCESS_MASK; =20 - return (pte_t) { .pte =3D 0 }; + return e; } =20 #define P2M_TABLE_MAP_NONE 0 @@ -580,7 +669,7 @@ static int p2m_set_entry(struct p2m_domain *p2m, p2m_clean_pte(entry, p2m->clean_dcache); else { - pte_t pte =3D p2m_pte_from_mfn(mfn, t); + pte_t pte =3D p2m_pte_from_mfn(mfn, t, false); =20 p2m_write_pte(entry, pte, p2m->clean_dcache); =20 --=20 2.51.0