From nobody Thu Oct 30 18:55:07 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=1753977524; cv=none; d=zohomail.com; s=zohoarc; b=LSrqEqlRKvQeUZbtOcYFhao5BLK6QAJ9a7CpisqE136FYuFtLDXNwsdUE3FHJBzu3mKJwIJrIG5NWAcZBRTveNwdjizT4YYMGB0QTQGfQZ9czrYC/PEPUhxBh7rbw7FY8jtSIJ6svb769fQdcTNAQx681VmEwWfBR3k0t7EnUCY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977524; 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=WQKgU+Lnj9fqS+ICL+XL3aDj77hg68+WgDppr1D0Eyg=; b=fiiwWbBAV58Co/aE3pdP3x9iTdijMjnynDoQqZ0rkRA367enoUBKAwgYeW+Knxr9Dfw5ZYpXJekPEtIom8Xa6tsclp9d0rgrjFTGAt9y1tBqrd7eH2cTBBGiZ+HtYxvyqK5Q55gXP8X2SakQRqdxnstq0eL2wS2i/R/zX6ahbjE= 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 1753977524856787.0079067377538; Thu, 31 Jul 2025 08:58:44 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065865.1431193 (Exim 4.92) (envelope-from ) id 1uhVfu-0001GR-Is; Thu, 31 Jul 2025 15:58:34 +0000 Received: by outflank-mailman (output) from mailman id 1065865.1431193; Thu, 31 Jul 2025 15:58:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVfu-0001GK-Dq; Thu, 31 Jul 2025 15:58:34 +0000 Received: by outflank-mailman (input) for mailman id 1065865; Thu, 31 Jul 2025 15:58:32 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVfs-0000zK-Pk for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:32 +0000 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [2a00:1450:4864:20::134]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3520d0fe-6e27-11f0-a320-13f23c93f187; Thu, 31 Jul 2025 17:58:32 +0200 (CEST) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-553d771435fso1122155e87.3 for ; Thu, 31 Jul 2025 08:58:32 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:30 -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: 3520d0fe-6e27-11f0-a320-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977512; x=1754582312; 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=WQKgU+Lnj9fqS+ICL+XL3aDj77hg68+WgDppr1D0Eyg=; b=HLjwoSxP0+eU6lRbcRVItyN1LYqfqruqlOhjxVBk/X2d2K2rWzjqxZwZFDmnRTJGu+ LuRp8kIsUHDzXthQoYsr1Zjb48p+ezVifjQHcBOHNN26CnJNW4TgcGnzyHtCLTXLkWI7 U1/zNfIm+soN6ELP3DCnmOWXl3WSLtDLtScy4698bxCaVbiJ/v/kso44rV9XjMm01nD6 esXdrdmKpjEoaFTpvc9/+yXnM5QT0GxwMwdfE7ijx9V758WVVxhlLM5mKdBoXZ2g2Z3Y 13gqT25o7+Ip82Ah5byiIFzfaopTkgY3cKbp7zRvLTRgqbWTXKp8/uj9bj/b83GDU/EL U2FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977512; x=1754582312; 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=WQKgU+Lnj9fqS+ICL+XL3aDj77hg68+WgDppr1D0Eyg=; b=uqPlUHIgRRzgWM3iq5xEiu2h6Ko5n/AOATZZ8mMrVHMmrblj3he7lujO7i2OSQzS8h OyK2MlI7gK7fm3ntOK2ud0HW+p5BE/6pNBgQSAAOHYDspo5ErscCynv8Qag9ouzuo2tc YESE1C80nSWHmpWB9YtNmkF5ewSXLddyVAxJXW4x75ph9c+hcjrWTkmQ7FmNSgnCM1Cw 61HhEd/gt9+e3Qr1tZ2tQhuPHA+zwFnWon/YBVQfrpGt/ZBGFwgt9yCOIEamNec904q7 pb17sGFX8HyJCA/FXvcOWvRR0AI6MpxS49sHrlMba8k3TXszmYmx2TXeLcqNbPmaaEUk 4fkQ== X-Gm-Message-State: AOJu0YwMVxPCwgCT4+ueFmHU1KnNfGoiqBrSKjckHIjNKboZ5wl++3Ob +e48susSMjtxK4XhAKiLXxvnT/XVicqPsgnS51Vv8iDzOPMa50BgRiOjNH4dbw== X-Gm-Gg: ASbGncvkFit8cwfPmZMZnbr7gzrk6hkMbm4WkSrzuy6jck/ZMIc40jYoH6B00oHR35k VeNS4Divuna5gYJLMbPJiXo52WyaPPJTmQH5Cy//DGn5QO3wleUVGVXSJCey8/+ML34TbffXG8/ 2S42RQqXNOAOTwQS+Mur4dqG+w4PGUgbrzbCnmvEamFbNpDoGg9OSuEz3BHafU4m0yep9sKv8La RF9svmRuXfAE22GdFIRd0uye+IWXVQL2EdzDORE7N2Wk86p3Py5jE/bnGDkGiLsVTlMNe16FNaV tx9TA7F6hByDTjGUBzmvNtAg5//ARzyuPfFMiHPLDMTKiiBr2ksK4XrAUqWT0AT9SX0Z6w1byqV vzhVj2Umjc1DnN72JUpWVQfqonw2at9HyuME+QVXfECQqcqhtnyglpteSAONlaw== X-Google-Smtp-Source: AGHT+IG3Y8cbrf+MhbaTj7HO65vD5t1UDH9sQeDXs8qbfyuCWEZXPeqRxAnmW6f7W2tcCeyZbHuJlA== X-Received: by 2002:a05:6512:ba1:b0:55b:8397:cffd with SMTP id 2adb3069b0e04-55b8397d195mr2182640e87.9.1753977511378; Thu, 31 Jul 2025 08:58:31 -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 v3 01/20] xen/riscv: implement sbi_remote_hfence_gvma() Date: Thu, 31 Jul 2025 17:58:00 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977526166116600 Content-Type: text/plain; charset="utf-8" Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of guest physical addresses between start_addr and start_addr + size for all VMIDs. The remote fence operation applies to the entire address space if either: - start_addr and size are both 0, or - size is equal to 2^XLEN-1. Signed-off-by: Oleksii Kurochko Acked-by: Jan Beulich --- Changes in V3: - Update the comment message above declaration of sbi_remote_hfence_gvma() and update the commit message in sync. - Drop ASSERT() in sbi_remote_hfence_gvma(). --- xen/arch/riscv/include/asm/sbi.h | 19 +++++++++++++++++++ xen/arch/riscv/sbi.c | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/xen/arch/riscv/include/asm/sbi.h b/xen/arch/riscv/include/asm/= sbi.h index 527d773277..0277aab747 100644 --- a/xen/arch/riscv/include/asm/sbi.h +++ b/xen/arch/riscv/include/asm/sbi.h @@ -89,6 +89,25 @@ bool sbi_has_rfence(void); int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, vaddr_t start, size_t size); =20 +/* + * Instructs the remote harts to execute one or more HFENCE.GVMA + * instructions, covering the range of guest physical addresses + * between start_addr and start_addr + size for all VMIDs. + * + * Returns 0 if IPI was sent to all the targeted harts successfully + * or negative value if start_addr or size is not valid. + * + * The remote fence operation applies to the entire address space if eithe= r: + * - start_addr and size are both 0, or + * - size is equal to 2^XLEN-1. + * + * @cpu_mask a cpu mask containing all the target CPUs (in Xen space). + * @param start virtual address start + * @param size virtual address range size + */ +int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start, + size_t size); + /* * Initialize SBI library * diff --git a/xen/arch/riscv/sbi.c b/xen/arch/riscv/sbi.c index 4209520389..1809f614c5 100644 --- a/xen/arch/riscv/sbi.c +++ b/xen/arch/riscv/sbi.c @@ -258,6 +258,13 @@ int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, v= addr_t start, cpu_mask, start, size, 0, 0); } =20 +int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start, + size_t size) +{ + return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA, + cpu_mask, start, size, 0, 0); +} + /* This function must always succeed. */ #define sbi_get_spec_version() \ sbi_ext_base_func(SBI_EXT_BASE_GET_SPEC_VERSION) --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977527; cv=none; d=zohomail.com; s=zohoarc; b=UFkMGj1Md202CrXRTqwgode9cnlWioH0x5p1JgwxsgWUPBRM32Gsh1hs2n8LMJnc2cyrVqYaA6KCLWalyE+eVxFcRpH5ZHDL/f8hvSqZCbrLsreGz0bhxW4+I5q1J06DQr9soNWMksJx1LS3h3jo57A4AddC4yrCv8kySP6r7iA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977527; 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=gl1Hk+jNRlMZzMMWZPcd3JgviGxNJ9ge4Ig02sPFSnM=; b=jpsutDHG92Axvuq99khdCvSe+Qu1tzFfuaEqwLxT1lvXSqj3t8pfvwvKPdymKRg8wynUYnOd/R4RuM2lIjyr9M/R4q799nc4fxL/t+ZcZHahL+3MbUrmuhxuJlTWxmC8rsFlpRVmpYjPa4ShPlXww8VOb8H7yietKnVYCCW7XOI= 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 17539775273981017.856144089387; Thu, 31 Jul 2025 08:58:47 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065867.1431202 (Exim 4.92) (envelope-from ) id 1uhVfv-0001O4-5Q; Thu, 31 Jul 2025 15:58:35 +0000 Received: by outflank-mailman (output) from mailman id 1065867.1431202; Thu, 31 Jul 2025 15:58:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVfu-0001NV-VJ; Thu, 31 Jul 2025 15:58:34 +0000 Received: by outflank-mailman (input) for mailman id 1065867; Thu, 31 Jul 2025 15:58:34 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVfu-0000zK-BG for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:34 +0000 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [2a00:1450:4864:20::133]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3606818c-6e27-11f0-a320-13f23c93f187; Thu, 31 Jul 2025 17:58:33 +0200 (CEST) Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-55b8bf653dfso513927e87.0 for ; Thu, 31 Jul 2025 08:58:33 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:32 -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: 3606818c-6e27-11f0-a320-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977513; x=1754582313; 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=gl1Hk+jNRlMZzMMWZPcd3JgviGxNJ9ge4Ig02sPFSnM=; b=SO7zGQ1YQEMBls2hAnykrzrCU4LP/mQSEPr44g/haVJ8x5QuF4VDGR6tjif0APtl9v dq5CB8fTYuYz2ffLqAQZOCNZMJ8dUAz9zSMe30yxAc9GtQe37JXqgPnzRJcRFxCcVo2K TrnEehmVwj0vNlhs+R+h0a9f7AMAc7m9rnhOfVtNpYFbinTo8lOYh1i53kCfkjAJF9oO iNnqHLBwt1nNc281xw+XqAzLYlzccHcs60IpfTIBqhr6jpvWeVoZUmvU9pbadlZzeeFR iRj8+7ibOTxF9ci3cWAC1R+CK+y8BELnmGeSaHpKDSsq8pKhTbJlpfSrrocHycVa/7H4 jPlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977513; x=1754582313; 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=gl1Hk+jNRlMZzMMWZPcd3JgviGxNJ9ge4Ig02sPFSnM=; b=wy+F3hMpoNxA8CQ0MZtFugs2DZWWedeAk8tyjkCha6BmdACVZrd97sSXVAov3AbMyJ Ze1OgMZuohZwQLINbwKzXTXXHByVWQ3zLKSZOH6KwzTaybayP56CSmosdvolEFhjF/l6 hD1FoCm/O2roF1edslNy1JXsJBYsYiMv2CUSjGIY/4pe4rRssh0LfHpzlTznMo0MYjyX tEkWJ5RV4CGDV1W/EDOOS+CwDK9PFA41heNbAUWb+NWwMFeaFtDKTjOXquifAiWfJJvu o+OOtZxjcDx5W0f4DeJXrobfouok3sM2CFibcIvuuswCX/NnAVIZTMqW8ZA+hsfOI8sM w2GA== X-Gm-Message-State: AOJu0Yzu19bIdF5XXXMSQFa45T33rvVEwR8XnwJ0jbkC6anboVt5h4md 9TOI8qmli7gkzOn7dvhucmnV2KqLpbkegqo/Fw85nFJEclrsrtbHth09xDmlOg== X-Gm-Gg: ASbGnct+3WXCfVUOx64GNDFYByVSbPVe2TqWjMB6V3oljCJhMR/ViTBs5pDOMJv/j9D Zc0uqIKkmF8tdDVnnyjme0hW9bSLf87wdE7n2lVKB9kSjhV6YVv9b3g5iPRU7Qr0+VepV/pwbwm eBFCOysiXaRW8rAOk+f1BmF5AQ3pX9yFehLa+hUdyNKRR7hJci/bS2dZrluUnSVvjEBLUa76B3+ I42Oqs9GftsTLB+WqsL0X1l9KDQY9WTaA1lZ5iOoirYe2h/hOj5TOUnakkOVJMa60anRjdyqeXG WzbDO9FhCs33JULLXUbtMDz3kMeA7vvbHVQgIFV+lV7fn/h+zMzK8Ckx+KW7wOTJiPd+DErz8Gw KZkQZEaFZZO0WQH7qyafCniV2m9aU7kKo27lk3cVwM1/cvlylubLTlTD0NeC9+3IygiErmicy X-Google-Smtp-Source: AGHT+IE0Fc3QUbhAdd8idvdotZ5yCFEp8zJuYhTWhOvWRzqB3iUQNwMHtAj0W+LSopnzVDwN9CCORA== X-Received: by 2002:a05:6512:63c9:10b0:55a:271e:9659 with SMTP id 2adb3069b0e04-55b7c085e8dmr1935607e87.27.1753977512819; Thu, 31 Jul 2025 08:58:32 -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 v3 02/20] xen/riscv: introduce sbi_remote_hfence_gvma_vmid() Date: Thu, 31 Jul 2025 17:58:01 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977528155116600 Content-Type: text/plain; charset="utf-8" It instructs the remote harts to execute one or more HFENCE.GVMA instructio= ns by making an SBI call, covering the range of guest physical addresses betwe= en start_addr and start_addr + size only for the given VMID. The remote fence operation applies to the entire address space if either: - start_addr and size are both 0, or - size is equal to 2^XLEN-1. Signed-off-by: Oleksii Kurochko Acked-by: Jan Beulich --- Changes in V3: - Drop ASSERT() in sbi_remote_hfence_gvma_vmid() as failure will happen an= yway if rfence isn't initialized. - Drop "This function call is only valid for harts implementing hypervisor extension." from the commit message and the comment above the declaration of sbi_remote_hfence_gvma_vmid(). - Use proper FID for sbi_remote_hfence_gvma_vmid(). --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/sbi.h | 13 +++++++++++++ xen/arch/riscv/sbi.c | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/xen/arch/riscv/include/asm/sbi.h b/xen/arch/riscv/include/asm/= sbi.h index 0277aab747..10930dea93 100644 --- a/xen/arch/riscv/include/asm/sbi.h +++ b/xen/arch/riscv/include/asm/sbi.h @@ -108,6 +108,19 @@ int sbi_remote_sfence_vma(const cpumask_t *cpu_mask, v= addr_t start, int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, vaddr_t start, size_t size); =20 +/* + * Instruct the remote harts to execute one or more HFENCE.GVMA instructio= ns, + * covering the range of guest physical addresses between start_addr and + * start_addr + size only for the given VMID. + * + * @cpu_mask a cpu mask containing all the target CPUs (in Xen space). + * @param start virtual address start + * @param size virtual address range size + * @param vmid virtual machine id + */ +int sbi_remote_hfence_gvma_vmid(const cpumask_t *cpu_mask, vaddr_t start, + size_t size, unsigned long vmid); + /* * Initialize SBI library * diff --git a/xen/arch/riscv/sbi.c b/xen/arch/riscv/sbi.c index 1809f614c5..425dce44c6 100644 --- a/xen/arch/riscv/sbi.c +++ b/xen/arch/riscv/sbi.c @@ -265,6 +265,13 @@ int sbi_remote_hfence_gvma(const cpumask_t *cpu_mask, = vaddr_t start, cpu_mask, start, size, 0, 0); } =20 +int sbi_remote_hfence_gvma_vmid(const cpumask_t *cpu_mask, vaddr_t start, + size_t size, unsigned long vmid) +{ + return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID, + cpu_mask, start, size, vmid, 0); +} + /* This function must always succeed. */ #define sbi_get_spec_version() \ sbi_ext_base_func(SBI_EXT_BASE_GET_SPEC_VERSION) --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977536; cv=none; d=zohomail.com; s=zohoarc; b=gCquhv+bLsQsts9eZoyFYXL4YoGN+yPgr2MREyB9r8cVbU92n1IHMOYiDm05P4tRa6JdzaKx20kF54Ute0Z9i6u3VX0pfmqtJ4YMqNBbkwaWNxlNkrWUR4b9lt7xt8dqftQlXj8SbXVcHFPJpRMhSJ5oBktOJIfVTuvX6frrlGs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977536; 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=YAaFHO3rUPEAzf/9yrT1X5pYejy4+Vy0hPa0YIS5wII=; b=TaGJ24Ly4V/nNxGZC09YSYHew8YD/pD10xTmRcMuwOLm4Hr7qnBsC9G5nowxGJzAJpJww4MYnArPhQhHB5o9c1ieXDAwfIStsmpVzyS+F+tBiajyudEtf0uXvCkvRDyd8q/0DCtiJXsKHGZndnOyEqe1o23UBoqyUUf+hl7H0Zo= 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 1753977536423589.32792959737; Thu, 31 Jul 2025 08:58:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065869.1431223 (Exim 4.92) (envelope-from ) id 1uhVfy-00021T-Do; Thu, 31 Jul 2025 15:58:38 +0000 Received: by outflank-mailman (output) from mailman id 1065869.1431223; Thu, 31 Jul 2025 15:58:38 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVfy-00021G-89; Thu, 31 Jul 2025 15:58:38 +0000 Received: by outflank-mailman (input) for mailman id 1065869; Thu, 31 Jul 2025 15:58:37 +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 1uhVfx-0001FB-04 for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:37 +0000 Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [2a00:1450:4864:20::22e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 36e93794-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:35 +0200 (CEST) Received: by mail-lj1-x22e.google.com with SMTP id 38308e7fff4ca-32b7113ed6bso11700921fa.1 for ; Thu, 31 Jul 2025 08:58:35 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:33 -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: 36e93794-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977514; x=1754582314; 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=YAaFHO3rUPEAzf/9yrT1X5pYejy4+Vy0hPa0YIS5wII=; b=Mu+5HaaUJGK74I0Ccav6fJAt0BT6wK3g9gdDax9PR9noX8AJmO/1MAjKqbK93wVQEb +oOqf1+g+ws5IlrjoQDkGYtA23HxPHUijp7D5zOPK7wb4XU6l2jNORGfRl5pudiimhpE 5COwh4ugOpdBgfwNo55RST0VRlUgGjO9E8OfVsSMw0Zj4YYr2tEg68edewQGDNGX5lMA NTB+bgT2zqPtwUZun0qBD0VSaXhb/Og7+u/BG0z3HYPVhxdUgEBx+AGuN1deRAtvIDDd w4wxHRPTdQnDWGVGlvxf9404oQkG1RlusZysl3KIK6d2CKHPOwn0K0vaOO0Xr+0YihSU NpRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977514; x=1754582314; 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=YAaFHO3rUPEAzf/9yrT1X5pYejy4+Vy0hPa0YIS5wII=; b=Dh8e+dO/34e+WMuxKmL4NOQE4TTe5lYPv804WfdfpSoEC0s0ny2fDnjEgJPYLluMvb HJT2kQGyp4sg3bPIZlhaiXf5obxe1XGfuAUFdL18iihHxfCr+kuFTxW2Sch+Ap/mnQK1 j2uT1G6aANbp7Oz/knmOK+SECsYbP6OmKLEcDxFBQGvPDwr24Guuxw1pX7WQcln4e5ze eYZ9RrUTEwtT8mUzrVGgJa8NXdCxOVSfCEWSP+q7ALrqcogjtqHBBOa9P1hrPYz+Rajb Qf1myMolQrzLflNfh17LDl1bGIRVhlYBUkqPlPC15tTgEszl2ARegPUc2+mFidPbv+wO 1pTg== X-Gm-Message-State: AOJu0YyWitFJeg/SE+Vid8OYGl20iAXbRq+/Zwu9hns2hcVRLmE7mMoy eaN3CJKMdFuKpeu3yi53RXR54H3wR9zlI52RuxOKEO0Ghommm0BPNl4dHqDHAQ== X-Gm-Gg: ASbGnctHvUm3vgbHC3HiElJKjncHsdPLydlq/KypErggXjAHEB9c+JrZlCr1MH5Mf1T SG8ptuRaT5WvKzPcmsQeqzZDYkZLYliD71WTPZwHiYUDC0xLzmRmkiBIXtoFcgDiayzzscY7Hjt eYfitdVZlZhKNo8f1TnAiiNDSapfL0XmD3y+MSQFcZdODfK4f1PmEPxCqAEWS0wbE3MGryaTJG2 BjVmkKHeOcYQDg1AKllc9/Ef1LPoG3ybHL7q6QUHDan2iiO4Uyt0NKI46h7vAFqtefywMEEAhrE WG9BdhsFg8IebTO3vN8dQYDwI9myAz6txgBRuaIgswP6mlATIsPr6kxJuLMjjA0H1gcj6UaiX/G eFY0Zd/sF3ldgz9U8ADRLfyQIz6ENCMVz7Ss2fSv4YNNeaRYONYVqvQRv6fBcVw== X-Google-Smtp-Source: AGHT+IE9CGjE6mb4ckGHh0x7f7Wnu2OSI7Gu6OQJWfpE3SEzcDkHVtbLTZ6EDRFXe6aj/MqflRzFmg== X-Received: by 2002:a05:651c:214f:b0:32b:4773:7aaf with SMTP id 38308e7fff4ca-33224be5d43mr14803431fa.35.1753977514052; Thu, 31 Jul 2025 08:58:34 -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 v3 03/20] xen/riscv: introduce VMID allocation and manegement Date: Thu, 31 Jul 2025 17:58:02 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977538472116600 Content-Type: text/plain; charset="utf-8" Current implementation is based on x86's way to allocate VMIDs: VMIDs partition the physical TLB. In the current implementation VMIDs are introduced to reduce the number of TLB flushes. Each time the guest's virtual address space changes, instead of flushing the TLB, a new VMID is assigned. This reduces the number of TLB flushes to at most 1/#VMIDs. The biggest advantage is that hot parts of the hypervisor's code and data retain in the TLB. VMIDs are a hart-local resource. As preemption of VMIDs is not possible, VMIDs are assigned in a round-robin scheme. To minimize the overhead of VMID invalidation, at the time of a TLB flush, VMIDs are tagged with a 64-bit generation. Only on a generation overflow the code needs to invalidate all VMID information stored at the VCPUs with are run on the specific physical processor. This overflow appears after about 2^80 host processor cycles, so we do not optimize this case, but simply disable VMID useage to retain correctness. Only minor changes are made compared to the x86 implementation. These include using RISC-V-specific terminology, adding a check to ensure the type used for storing the VMID has enough bits to hold VMIDLEN, and introducing a new function vmidlen_detect() to clarify the VMIDLEN value. Signed-off-by: Oleksii Kurochko --- Changes in V3: - Reimplemnt VMID allocation similar to what x86 has implemented. --- Changes in V2: - New patch. --- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/domain.h | 6 + xen/arch/riscv/include/asm/flushtlb.h | 5 + xen/arch/riscv/include/asm/vmid.h | 8 ++ xen/arch/riscv/setup.c | 3 + xen/arch/riscv/vmid.c | 165 ++++++++++++++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 xen/arch/riscv/include/asm/vmid.h create mode 100644 xen/arch/riscv/vmid.c diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile index e2b8aa42c8..745a85e116 100644 --- a/xen/arch/riscv/Makefile +++ b/xen/arch/riscv/Makefile @@ -16,6 +16,7 @@ obj-y +=3D smpboot.o obj-y +=3D stubs.o obj-y +=3D time.o obj-y +=3D traps.o +obj-y +=3D vmid.o obj-y +=3D vm_event.o =20 $(TARGET): $(TARGET)-syms diff --git a/xen/arch/riscv/include/asm/domain.h b/xen/arch/riscv/include/a= sm/domain.h index c3d965a559..aac1040658 100644 --- a/xen/arch/riscv/include/asm/domain.h +++ b/xen/arch/riscv/include/asm/domain.h @@ -5,6 +5,11 @@ #include #include =20 +struct vcpu_vmid { + uint64_t generation; + uint16_t vmid; +}; + struct hvm_domain { uint64_t params[HVM_NR_PARAMS]; @@ -14,6 +19,7 @@ struct arch_vcpu_io { }; =20 struct arch_vcpu { + struct vcpu_vmid vmid; }; =20 struct arch_domain { diff --git a/xen/arch/riscv/include/asm/flushtlb.h b/xen/arch/riscv/include= /asm/flushtlb.h index 51c8f753c5..f391ae6eb7 100644 --- a/xen/arch/riscv/include/asm/flushtlb.h +++ b/xen/arch/riscv/include/asm/flushtlb.h @@ -7,6 +7,11 @@ =20 #include =20 +static inline void local_hfence_gvma_all(void) +{ + asm volatile ( "hfence.gvma zero, zero" ::: "memory" ); +} + /* Flush TLB of local processor for address va. */ static inline void flush_tlb_one_local(vaddr_t va) { diff --git a/xen/arch/riscv/include/asm/vmid.h b/xen/arch/riscv/include/asm= /vmid.h new file mode 100644 index 0000000000..2f1f7ec9a2 --- /dev/null +++ b/xen/arch/riscv/include/asm/vmid.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef ASM_RISCV_VMID_H +#define ASM_RISCV_VMID_H + +void vmid_init(void); + +#endif /* ASM_RISCV_VMID_H */ diff --git a/xen/arch/riscv/setup.c b/xen/arch/riscv/setup.c index 483cdd7e17..549228d73f 100644 --- a/xen/arch/riscv/setup.c +++ b/xen/arch/riscv/setup.c @@ -25,6 +25,7 @@ #include #include #include +#include =20 /* Xen stack for bringing up the first CPU. */ unsigned char __initdata cpu0_boot_stack[STACK_SIZE] @@ -148,6 +149,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id, =20 console_init_postirq(); =20 + vmid_init(); + printk("All set up\n"); =20 machine_halt(); diff --git a/xen/arch/riscv/vmid.c b/xen/arch/riscv/vmid.c new file mode 100644 index 0000000000..7ad1b91ee2 --- /dev/null +++ b/xen/arch/riscv/vmid.c @@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* Xen command-line option to enable VMIDs */ +static bool __read_mostly opt_vmid_enabled =3D true; +boolean_param("vmid", opt_vmid_enabled); + +/* + * VMIDs partition the physical TLB. In the current implementation VMIDs a= re + * introduced to reduce the number of TLB flushes. Each time the guest's + * virtual address space changes, instead of flushing the TLB, a new VMID = is + * assigned. This reduces the number of TLB flushes to at most 1/#VMIDs. + * The biggest advantage is that hot parts of the hypervisor's code and da= ta + * retain in the TLB. + * + * Sketch of the Implementation: + * + * VMIDs are a hart-local resource. As preemption of VMIDs is not possibl= e, + * VMIDs are assigned in a round-robin scheme. To minimize the overhead of + * VMID invalidation, at the time of a TLB flush, VMIDs are tagged with a + * 64-bit generation. Only on a generation overflow the code needs to + * invalidate all VMID information stored at the VCPUs with are run on the + * specific physical processor. This overflow appears after about 2^80 + * host processor cycles, so we do not optimize this case, but simply disa= ble + * VMID useage to retain correctness. + */ + +/* Per-Hart VMID management. */ +struct vmid_data { + uint64_t hart_vmid_generation; + uint16_t next_vmid; + uint16_t max_vmid; + bool disabled; +}; + +static DEFINE_PER_CPU(struct vmid_data, vmid_data); + +static unsigned long vmidlen_detect(void) +{ + unsigned long vmid_bits; + unsigned long old; + + /* Figure-out number of VMID bits in HW */ + old =3D csr_read(CSR_HGATP); + + csr_write(CSR_HGATP, old | HGATP_VMID_MASK); + vmid_bits =3D csr_read(CSR_HGATP); + vmid_bits =3D MASK_EXTR(vmid_bits, HGATP_VMID_MASK); + vmid_bits =3D flsl(vmid_bits); + csr_write(CSR_HGATP, old); + + /* + * We polluted local TLB so flush all guest TLB as + * a speculative access can happen at any time. + */ + local_hfence_gvma_all(); + + return vmid_bits; +} + +void vmid_init(void) +{ + static bool g_disabled =3D false; + + unsigned long vmid_len =3D vmidlen_detect(); + struct vmid_data *data =3D &this_cpu(vmid_data); + unsigned long max_availalbe_bits =3D sizeof(data->max_vmid) << 3; + + if ( vmid_len > max_availalbe_bits ) + panic("%s: VMIDLEN is bigger then a type which represent VMID: %lu= (%lu)\n", + __func__, vmid_len, max_availalbe_bits); + + data->max_vmid =3D BIT(vmid_len, U) - 1; + data->disabled =3D !opt_vmid_enabled || (vmid_len <=3D 1); + + if ( g_disabled !=3D data->disabled ) + { + printk("%s: VMIDs %sabled.\n", __func__, + data->disabled ? "dis" : "en"); + if ( !g_disabled ) + g_disabled =3D data->disabled; + } + + /* Zero indicates 'invalid generation', so we start the count at one. = */ + data->hart_vmid_generation =3D 1; + + /* Zero indicates 'VMIDs disabled', so we start the count at one. */ + data->next_vmid =3D 1; +} + +void vcpu_vmid_flush_vcpu(struct vcpu *v) +{ + write_atomic(&v->arch.vmid.generation, 0); +} + +void vmid_flush_hart(void) +{ + struct vmid_data *data =3D &this_cpu(vmid_data); + + if ( data->disabled ) + return; + + if ( likely(++data->hart_vmid_generation !=3D 0) ) + return; + + /* + * VMID generations are 64 bit. Overflow of generations never happens. + * For safety, we simply disable ASIDs, so correctness is established;= it + * only runs a bit slower. + */ + printk("%s: VMID generation overrun. Disabling VMIDs.\n", __func__); + data->disabled =3D 1; +} + +bool vmid_handle_vmenter(struct vcpu_vmid *vmid) +{ + struct vmid_data *data =3D &this_cpu(vmid_data); + + /* Test if VCPU has valid VMID. */ + if ( read_atomic(&vmid->generation) =3D=3D data->hart_vmid_generation ) + return 0; + + /* If there are no free VMIDs, need to go to a new generation. */ + if ( unlikely(data->next_vmid > data->max_vmid) ) + { + vmid_flush_hart(); + data->next_vmid =3D 1; + if ( data->disabled ) + goto disabled; + } + + /* Now guaranteed to be a free VMID. */ + vmid->vmid =3D data->next_vmid++; + write_atomic(&vmid->generation, data->hart_vmid_generation); + + /* + * When we assign VMID 1, flush all TLB entries as we are starting a n= ew + * generation, and all old VMID allocations are now stale. + */ + return (vmid->vmid =3D=3D 1); + + disabled: + vmid->vmid =3D 0; + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977533; cv=none; d=zohomail.com; s=zohoarc; b=OcVA7+kZNat82LmYY80Tsvi8UvM0k/stL+w43fX6Mhy8npyEl7bdtNcruZGZtuDvwH0umaWQrmVth7g0dAVIGgknNpl0QYj65q1y3yJz7bUsGzYNoDdDdGo2+oV7OlkebrlAVQ3268IXkhCP1Of4nj33ZOzkPcYAG/pmZxAeeSc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977533; 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=SucK4GD77NILFw9saTv0jC1xXZTivx/o3TP9MUfpwV0=; b=m2SQ5/DxVomu8Su53yguWwZ0ADz7cCEucEn2RMx6WjMU1vqRZvHED2xOvnMD2kbR5OixrRlVKKy87mPlYLJYL2Pq6l1MRW/TLT8mqW7KEJv0fwRJ6fM126hq/EKyToanBC/5j+EJAdb0GB/JFCCK825CfTADdijNVXB0XSasIm0= 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 1753977533836823.909756114321; Thu, 31 Jul 2025 08:58:53 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065870.1431228 (Exim 4.92) (envelope-from ) id 1uhVfy-00024p-OW; Thu, 31 Jul 2025 15:58:38 +0000 Received: by outflank-mailman (output) from mailman id 1065870.1431228; Thu, 31 Jul 2025 15:58:38 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVfy-00023S-H1; Thu, 31 Jul 2025 15:58:38 +0000 Received: by outflank-mailman (input) for mailman id 1065870; Thu, 31 Jul 2025 15:58:37 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVfx-0000zK-Bw for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:37 +0000 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [2a00:1450:4864:20::136]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 37c956c3-6e27-11f0-a320-13f23c93f187; Thu, 31 Jul 2025 17:58:36 +0200 (CEST) Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-55b7454c6b0so579788e87.3 for ; Thu, 31 Jul 2025 08:58:36 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:35 -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: 37c956c3-6e27-11f0-a320-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977516; x=1754582316; 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=SucK4GD77NILFw9saTv0jC1xXZTivx/o3TP9MUfpwV0=; b=hchlt1IrYvSaL3wNuzpL+mgpJ1UN3CwQPiBjHVLNLd4juiD0hJmaOSNrKljVgQCHap nt3LE0LftOb5UziDxhHkEHidc5wtWzLTJeM2O+0NM6ONnLbPbRBL84/RkpXdaXB2f4wA Ut9YIItv2p9A7dSYMtax+yrPqczVdNGQ2NXa91In4XgfRFA/SND0PmJYPEFiT9GFRmuN XWE8yIvLbCO36wiwKo/KfJw452gWgoXJeqLfqnwDdEkQQ0PS1yP+Dkcq7+T4MN2EZbZH i+V9FwctyOl6H/PaUZROXkrNSfxoH2n3IsVwqx3ljLzZe7gXr+1hcxpmYip1LZ7VbNRB HhnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977516; x=1754582316; 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=SucK4GD77NILFw9saTv0jC1xXZTivx/o3TP9MUfpwV0=; b=dNuWWN6uLHeieAGBmJGk3zaLx1CRwB+ITi4h6zYNXQ2pEDDT/eYYP6AtwirCcGLuk/ IY1zh8p3xJ6/ohelZP9V4eHaOAbDlpeDX5KQKDnFeEYGGLMo2wHQpUIQqpQPAgI4uviN PfT/jlsxZAvhAK7zgnEkc4kkv5rZ8WoErEbpOpFNSoW5J0zCmM/0trivXBj1AwTuJsvU opg2+n13MfsGpPOf3842rjD5E5c37UNbLfhO4pQvngA5PErrdjetBO+NV3aZDxbu6fPE QY2gga76YKWSK203wqyV4RH39v8bxCQrcHtrb+t/C+lyHob/Ds3ZNh0Rp6oFC6IBCy7w bTGw== X-Gm-Message-State: AOJu0YzyxW1sm+c/Wfm04ORPwyignPkiX3rslpZgsGj5VVe7/eJonlgi ZeU3wPQJ6Io4qbRUXeQeHpGTLeqmJ9r7xTQ7ITMToaXMS171+rl2e50EsA0o7w== X-Gm-Gg: ASbGncunQ85Z/Oj23VlrnpnI5p7tXMZeF/+H/MhWscJcZVHWc5rghmpoxCgH9HPcE9L qorGwQ7SqoD/YaSlPNDqpIdWQg9ePb1RwdUjxu2eHarnxN+QX74csijCe2P2dq6yWZfy9OAVif6 aBZFZLwep8342+KlauWxcsYcm6KKq7OHtryVDlAOxXvk+RnLWnt/qpxR4WiXb1t0R3uccdWiztw dqMnAe96tOl1RN7ibzQe1puUbDyHX0V9yPpfR8TsuyGYFJLkIB0CpdJWLBmhujCHPYBeIT6CXGx AFDdoBJ6xkWwm50bZKlUC4P032tbglEcCGRBig4O75dk40cYvpvETaEPBoiMiGlnDFH8GOLsfJC YrIARJ/6rbIjyaKlC+D7OYuKc3CpiEa06ygj5iMDNSSCYUgmowrCH9JurnCdzLg== X-Google-Smtp-Source: AGHT+IG4YTKZOLh7LwJcLbr7QTUyE6vXkH4b9u7bZEQLBlWOEnweb49wNI2Ccau5lElsWPXq4CzGlg== X-Received: by 2002:a05:6512:1449:20b0:553:291f:92d with SMTP id 2adb3069b0e04-55b7c0a8599mr1947600e87.57.1753977515826; Thu, 31 Jul 2025 08:58:35 -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 v3 04/20] xen/riscv: introduce things necessary for p2m initialization Date: Thu, 31 Jul 2025 17:58:03 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977536612116600 Content-Type: text/plain; charset="utf-8" Introduce the following things: - Update p2m_domain structure, which describe per p2m-table state, with: - lock to protect updates to p2m. - pool with pages used to construct p2m. - clean_pte which indicate if it is requires to clean the cache when writing an entry. - back pointer to domain structure. - p2m_init() to initalize members introduced in p2m_domain structure. - Call of paging_domain_init() in p2m_init() to initlize paging spinlock and freelist head. Signed-off-by: Oleksii Kurochko --- Changes in V3: - s/p2m_type/p2m_types. - Drop init. of p2m->clean_pte in p2m_init() as CONFIG_HAS_PASSTHROUGH is going to be selected unconditionaly. Plus CONFIG_HAS_PASSTHROUGH isn't ready to be used for RISC-V. Add compilation error to not forget to init p2m->clean_pte. - Move defintion of p2m->domain up in p2m_init(). - Add iommu_use_hap_pt() when p2m->clean_pte is initialized. - Add the comment above p2m_types member of p2m_domain struct. - Add need_flush member to p2m_domain structure. - Move introduction of p2m_write_(un)lock() and p2m_tlb_flush_sync() to the patch where they are really used: xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFN - Add p2m member to arch_domain structure. - Drop p2m_types from struct p2m_domain as P2M type for PTE will be stored differently. - Drop default_access as it isn't going to be used for now. - Move defintion of p2m_is_write_locked() to "implement function to map me= mory in guest p2m" where it is really used. --- Changes in V2: - Use introduced erlier sbi_remote_hfence_gvma_vmid() for proper implement= ation of p2m_force_tlb_flush_sync() as TLB flushing needs to happen for each p= CPU which potentially has cached a mapping, what is tracked by d->dirty_cpum= ask. - Drop unnecessary blanks. - Fix code style for # of pre-processor directive. - Drop max_mapped_gfn and lowest_mapped_gfn as they aren't used now. - [p2m_init()] Set p2m->clean_pte=3Dfalse if CONFIG_HAS_PASSTHROUGH=3Dn. - [p2m_init()] Update the comment above p2m->domain =3D d; - Drop p2m->need_flush as it seems to be always true for RISC-V and as a consequence drop p2m_tlb_flush_sync(). - Move to separate patch an introduction of root page table allocation. --- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/domain.h | 5 +++++ xen/arch/riscv/include/asm/p2m.h | 34 +++++++++++++++++++++++++++++ xen/arch/riscv/p2m.c | 32 +++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 xen/arch/riscv/p2m.c diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile index 745a85e116..e2499210c8 100644 --- a/xen/arch/riscv/Makefile +++ b/xen/arch/riscv/Makefile @@ -7,6 +7,7 @@ obj-y +=3D intc.o obj-y +=3D irq.o obj-y +=3D mm.o obj-y +=3D pt.o +obj-y +=3D p2m.o obj-$(CONFIG_RISCV_64) +=3D riscv64/ obj-y +=3D sbi.o obj-y +=3D setup.o diff --git a/xen/arch/riscv/include/asm/domain.h b/xen/arch/riscv/include/a= sm/domain.h index aac1040658..e688980efa 100644 --- a/xen/arch/riscv/include/asm/domain.h +++ b/xen/arch/riscv/include/asm/domain.h @@ -5,6 +5,8 @@ #include #include =20 +#include + struct vcpu_vmid { uint64_t generation; uint16_t vmid; @@ -24,6 +26,9 @@ struct arch_vcpu { =20 struct arch_domain { struct hvm_domain hvm; + + /* Virtual MMU */ + struct p2m_domain p2m; }; =20 #include diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index 28f57a74f2..f8051ed893 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -3,11 +3,45 @@ #define ASM__RISCV__P2M_H =20 #include +#include +#include +#include =20 #include =20 #define paddr_bits PADDR_BITS =20 +/* Get host p2m table */ +#define p2m_get_hostp2m(d) (&(d)->arch.p2m) + +/* Per-p2m-table state */ +struct p2m_domain { + /* + * Lock that protects updates to the p2m. + */ + rwlock_t lock; + + /* Pages used to construct the p2m */ + struct page_list_head pages; + + /* Indicate if it is required to clean the cache when writing an entry= */ + bool clean_pte; + + /* Back pointer to domain */ + struct domain *domain; + + /* + * P2M updates may required TLBs to be flushed (invalidated). + * + * Flushes may be deferred by setting 'need_flush' and then flushing + * when the p2m write lock is released. + * + * If an immediate flush is required (e.g, if a super page is + * shattered), call p2m_tlb_flush_sync(). + */ + bool need_flush; +}; + /* * List of possible type for each page in the p2m entry. * The number of available bit per page in the pte for this purpose is 2 b= its. diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c new file mode 100644 index 0000000000..ae937e9bdd --- /dev/null +++ b/xen/arch/riscv/p2m.c @@ -0,0 +1,32 @@ +#include +#include +#include + +int p2m_init(struct domain *d) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + + /* + * "Trivial" initialisation is now complete. Set the backpointer so t= he + * users of p2m could get an access to domain structure. + */ + p2m->domain =3D d; + + rwlock_init(&p2m->lock); + INIT_PAGE_LIST_HEAD(&p2m->pages); + + /* + * Currently, the infrastructure required to enable CONFIG_HAS_PASSTHR= OUGH + * is not ready for RISC-V support. + * + * When CONFIG_HAS_PASSTHROUGH=3Dy, p2m->clean_pte must be properly + * initialized. + * At the moment, it defaults to false because the p2m structure is + * zero-initialized. + */ +#ifdef CONFIG_HAS_PASSTHROUGH +# error "Add init of p2m->clean_pte" +#endif + + return 0; +} --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977542; cv=none; d=zohomail.com; s=zohoarc; b=TAnSTRMCifxaI6iE/URAg0xKdEDrlnRYfvsHx/1Q68m54n3tXXmzGLJPuy8Wj85amBLSk/Y6IKNG8ImMH7Mv3Nt04HmagFsJfYrUHCibg8emY8jiI4aWdsnC8eUuaFyMb00IQQHF7Z5O1qQ5WUv1hkOP3UtSr+9Wkzjt/2jzJUE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977542; 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=pnLE+XT1CgwuNN+vswfBBQ3lSNBiPPpb65b2kzBfA5o=; b=K/0PsZtjj7mz+BSQ7Fgr2U4c2jAdlSuPrGT6y4GsUR912RS2ZTrTyApXIFG9egu+t5dp46/dW8u/VkB+Dua35msvHi2prRC0al+YXmYAPZ5VlW9N2twBI0rx+UA0X4xVd+s6WQDr/sT174yd5gqqbQtcXLsSGzbhRbxWISUVnaQ= 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 1753977541882113.13075977204983; Thu, 31 Jul 2025 08:59:01 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065871.1431242 (Exim 4.92) (envelope-from ) id 1uhVg2-0002cI-3e; Thu, 31 Jul 2025 15:58:42 +0000 Received: by outflank-mailman (output) from mailman id 1065871.1431242; Thu, 31 Jul 2025 15:58:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVg1-0002cA-Vd; Thu, 31 Jul 2025 15:58:41 +0000 Received: by outflank-mailman (input) for mailman id 1065871; Thu, 31 Jul 2025 15:58:39 +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 1uhVfz-0001FB-OZ for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:39 +0000 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [2a00:1450:4864:20::12f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 38975460-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:38 +0200 (CEST) Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-55b85d01b79so633216e87.3 for ; Thu, 31 Jul 2025 08:58:38 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:36 -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: 38975460-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977517; x=1754582317; 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=pnLE+XT1CgwuNN+vswfBBQ3lSNBiPPpb65b2kzBfA5o=; b=UABmbRH1Ul6ApApKh3hAK0HwL5oxa64vbbq0trqekCFggorCy5xO6yvTek+hkRO7JF RkC6y2BfTynZ5N9jEkC1etBjgkVseL4fiCoSVK6o7ZRU64E60dF7udSjOB637q2ZqoYH 4/9jBUP7fYA6cRLQAUHdAVBAtdNiu1bcwEvU+bBrrv78BpnPCJXKLxJH6OfOSkjfRGVR CuwP+vHMq+9friXIPcR2RhD2NZLFDmTt89VEYbm77J2BtIrTSTRVKkLwUFYbPUG7vULt 7xQfXM+iENkSjq9HxaITxY4WgNpE/zNA/14ltBN4vD/dC0LFeS0kcGch4lQuGEpqNGCk v2QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977517; x=1754582317; 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=pnLE+XT1CgwuNN+vswfBBQ3lSNBiPPpb65b2kzBfA5o=; b=W+sOCr2LtRCZiwTrdzFEb+liEnbvy546D8glTWYs/AJjmk4d4OLRfz9extYlAqQsO9 owNlKFgrWENzolOwm477O/39SHqB+92dZOYiy1+QlLGbksdINe4XVltUVIhwN7HqpdPK Y97XKUvy3pG/W7+OMqwhkgTaWfWfLaMbENuWzPrwHmiywxwOQMdxE0rHuWhp1xJ0x9rJ KyV0I/A0YynhJqVpyf/xwq88QHhwGsdCV7wJESoBICU+x+MHtyhn7g7LjeGFXbRxcAGb hyK1vZamMXZ0WrgTYC8FmY6bolpBgYHmhH+bqA6xKvgty+YauLo6TgxSRKMbB/5pju6B 8YoA== X-Gm-Message-State: AOJu0YwnW3xBMu9HMD4/kMkFjQiKEl7ixxDYJELL+9ZcFdVOkY+DcO04 7Vo/l1mzSKGs4jmeEsLfeMeTJhaL2tQmZ9ajcWZiyK1tPD/lK0lRQmEqwdrz1Q== X-Gm-Gg: ASbGncszlfvXTgkLdcPbQmqRnlhAenDRgOTXUG3BFEUUBgAABZhsy7aDNSTlGldoRYj dY26/8Z5WIlW0BP/+e7jXlp4m+Di+/izcc/McloTPoYVJ/jWxzBFOhWsjo+8hXvHysKHAib3y8W s1GAvHtTkifSJYvW5ZHk+SPM7l3RNkD5y3SGIoUdVpUVUAyHx9/6mHa7sybk5Jrr4JjAOScX6ju JmrG9GAeK73zdZF7EvILKY4woYN0+jIoG1JZDEfaXe4+dBGhgFc7AGGq0457TdKQzwD3wfLpMxw ggKRpaerZAbbUn9JQVjrxzxNfIhS8gSItIYxDdoMWLkBO98JOLm7EwAtXuWpGhUheXmKf57+Y6v a/cbTSyuDfV9HX1hzPnbBx7ARDFdjjzL/pwuS7k/zsG10cA0qpsxqseRD1cNAR5c8OMKLof2d X-Google-Smtp-Source: AGHT+IGGNK7XkZgMa4uBl846NX9o70JMkMjakDJ0RO9QkCwOCYW1rhNAS/kaM2451mhkaXfVsPkf8A== X-Received: by 2002:a05:6512:2313:b0:55b:880f:38b6 with SMTP id 2adb3069b0e04-55b880f3b48mr1149630e87.4.1753977517163; Thu, 31 Jul 2025 08:58:37 -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 v3 05/20] xen/riscv: construct the P2M pages pool for guests Date: Thu, 31 Jul 2025 17:58:04 +0200 Message-ID: <4e7de8ffc929c1bdfa28b40fdd31b814acef103c.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977544578116600 Content-Type: text/plain; charset="utf-8" Implement p2m_set_allocation() to construct p2m pages pool for guests based on required number of pages. This is implemented by: - Adding a `struct paging_domain` which contains a freelist, a counter variable and a spinlock to `struct arch_domain` to indicate the free p2m pages and the number of p2m total pages in the p2m pages pool. - Adding a helper `p2m_set_allocation` to set the p2m pages pool size. This helper should be called before allocating memory for a guest and is called from domain_p2m_set_allocation(), the latter is a part of common dom0less code. - Adding paging_freelist_init() to struct paging_domain. Signed-off-by: Oleksii Kurochko Acked-by: Jan Beulich --- Changes in v3: - Drop usage of p2m_ prefix inside struct paging_domain(). - Introduce paging_domain_init() to init paging struct. --- Changes in v2: - Drop the comment above inclusion of in riscv/p2m.c. - Use ACCESS_ONCE() for lhs and rhs for the expressions in p2m_set_allocation(). --- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/Makefile | 1 - xen/arch/riscv/include/asm/domain.h | 12 ++++++ xen/arch/riscv/include/asm/paging.h | 13 ++++++ xen/arch/riscv/p2m.c | 19 +++++++++ xen/arch/riscv/paging.c | 64 +++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 xen/arch/riscv/include/asm/paging.h create mode 100644 xen/arch/riscv/paging.c diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile index e2499210c8..6b912465b9 100644 --- a/xen/arch/riscv/Makefile +++ b/xen/arch/riscv/Makefile @@ -6,6 +6,7 @@ obj-y +=3D imsic.o obj-y +=3D intc.o obj-y +=3D irq.o obj-y +=3D mm.o +obj-y +=3D paging.o obj-y +=3D pt.o obj-y +=3D p2m.o obj-$(CONFIG_RISCV_64) +=3D riscv64/ diff --git a/xen/arch/riscv/include/asm/Makefile b/xen/arch/riscv/include/a= sm/Makefile index bfdf186c68..3824f31c39 100644 --- a/xen/arch/riscv/include/asm/Makefile +++ b/xen/arch/riscv/include/asm/Makefile @@ -6,7 +6,6 @@ generic-y +=3D hardirq.h generic-y +=3D hypercall.h generic-y +=3D iocap.h generic-y +=3D irq-dt.h -generic-y +=3D paging.h generic-y +=3D percpu.h generic-y +=3D perfc_defn.h generic-y +=3D random.h diff --git a/xen/arch/riscv/include/asm/domain.h b/xen/arch/riscv/include/a= sm/domain.h index e688980efa..316e7c6c84 100644 --- a/xen/arch/riscv/include/asm/domain.h +++ b/xen/arch/riscv/include/asm/domain.h @@ -2,6 +2,8 @@ #ifndef ASM__RISCV__DOMAIN_H #define ASM__RISCV__DOMAIN_H =20 +#include +#include #include #include =20 @@ -24,11 +26,21 @@ struct arch_vcpu { struct vcpu_vmid vmid; }; =20 +struct paging_domain { + spinlock_t lock; + /* Free pages from the pre-allocated pool */ + struct page_list_head freelist; + /* Number of pages from the pre-allocated pool */ + unsigned long total_pages; +}; + struct arch_domain { struct hvm_domain hvm; =20 /* Virtual MMU */ struct p2m_domain p2m; + + struct paging_domain paging; }; =20 #include diff --git a/xen/arch/riscv/include/asm/paging.h b/xen/arch/riscv/include/a= sm/paging.h new file mode 100644 index 0000000000..8fdaeeb2e4 --- /dev/null +++ b/xen/arch/riscv/include/asm/paging.h @@ -0,0 +1,13 @@ +#ifndef ASM_RISCV_PAGING_H +#define ASM_RISCV_PAGING_H + +#include + +struct domain; + +int paging_domain_init(struct domain *d); + +int paging_freelist_init(struct domain *d, unsigned long pages, + bool *preempted); + +#endif /* ASM_RISCV_PAGING_H */ diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index ae937e9bdd..214b4861d2 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -2,6 +2,8 @@ #include #include =20 +#include + int p2m_init(struct domain *d) { struct p2m_domain *p2m =3D p2m_get_hostp2m(d); @@ -12,6 +14,8 @@ int p2m_init(struct domain *d) */ p2m->domain =3D d; =20 + paging_domain_init(d); + rwlock_init(&p2m->lock); INIT_PAGE_LIST_HEAD(&p2m->pages); =20 @@ -30,3 +34,18 @@ int p2m_init(struct domain *d) =20 return 0; } + +/* + * Set the pool of pages to the required number of pages. + * Returns 0 for success, non-zero for failure. + * Call with d->arch.paging.lock held. + */ +int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preemp= ted) +{ + int rc; + + if ( (rc =3D paging_freelist_init(d, pages, preempted)) ) + return rc; + + return 0; +} diff --git a/xen/arch/riscv/paging.c b/xen/arch/riscv/paging.c new file mode 100644 index 0000000000..8882be5ac9 --- /dev/null +++ b/xen/arch/riscv/paging.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +int paging_freelist_init(struct domain *d, unsigned long pages, + bool *preempted) +{ + struct page_info *pg; + + ASSERT(spin_is_locked(&d->arch.paging.lock)); + + for ( ; ; ) + { + if ( d->arch.paging.total_pages < pages ) + { + /* Need to allocate more memory from domheap */ + pg =3D alloc_domheap_page(d, MEMF_no_owner); + if ( pg =3D=3D NULL ) + { + printk(XENLOG_ERR "Failed to allocate pages.\n"); + return -ENOMEM; + } + ACCESS_ONCE(d->arch.paging.total_pages)++; + page_list_add_tail(pg, &d->arch.paging.freelist); + } + else if ( d->arch.paging.total_pages > pages ) + { + /* Need to return memory to domheap */ + pg =3D page_list_remove_head(&d->arch.paging.freelist); + if ( pg ) + { + ACCESS_ONCE(d->arch.paging.total_pages)--; + free_domheap_page(pg); + } + else + { + printk(XENLOG_ERR + "Failed to free pages, freelist is empty.\n"); + return -ENOMEM; + } + } + else + break; + + /* Check to see if we need to yield and try again */ + if ( preempted && general_preempt_check() ) + { + *preempted =3D true; + return -ERESTART; + } + } + + return 0; +} +/* Domain paging struct initialization. */ +int paging_domain_init(struct domain *d) +{ + spin_lock_init(&d->arch.paging.lock); + INIT_PAGE_LIST_HEAD(&d->arch.paging.freelist); + + return 0; +} --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977537; cv=none; d=zohomail.com; s=zohoarc; b=ItXPkvdslbrAWsMleRz2Ryrv3/aXJyPWkLIYFq5MbFc20whgp2nxRBce6TnGLJo9MS+cnoDJUBGGIubezOy65t/HAMCpVkTOby6cIgg2M9sDLXp+jcO/PVyl1txq9HKymZ3prbuqrYuv1JmmlDONjXLrDkpSE2Yq32ElsGyx0fE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977537; 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=49cBhv+bsfX2NNvsCTVAGnNs0eP38Nq2mxipt6UO56Y=; b=nv0JL8EjL99WrXCncJe6VA3lVo8l/LcEefn46lKiZr88iNA1pbe7CIsgJ0HiIYMC3/0yCRw96Qq1l/XU2w4/lbUWBP2zEmUksIHRm6HEw+idIIDPMpPp9aiaEjKwOseCKI52jmXDyEXIhRhyNwmZCpEzXCtHDF+i1HT+IfxE5rI= 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 1753977537593928.7815577847165; Thu, 31 Jul 2025 08:58:57 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065872.1431247 (Exim 4.92) (envelope-from ) id 1uhVg2-0002hB-Kl; Thu, 31 Jul 2025 15:58:42 +0000 Received: by outflank-mailman (output) from mailman id 1065872.1431247; Thu, 31 Jul 2025 15:58:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVg2-0002gX-C2; Thu, 31 Jul 2025 15:58:42 +0000 Received: by outflank-mailman (input) for mailman id 1065872; Thu, 31 Jul 2025 15:58:41 +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 1uhVg1-0001FB-Bi for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:41 +0000 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [2a00:1450:4864:20::12e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 397bfc85-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:39 +0200 (CEST) Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-55b7454c6b0so579841e87.3 for ; Thu, 31 Jul 2025 08:58:39 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:38 -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: 397bfc85-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977519; x=1754582319; 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=49cBhv+bsfX2NNvsCTVAGnNs0eP38Nq2mxipt6UO56Y=; b=P+JdK2h6eqM2x/9/+OeQ7Us1gxwstPcVbRQIugKjQcRLeF0uHTXSCKKoDw1sP610Jd Sp+Q84JCsSbVL4IqQslqFoM0+zT5xm1ct0YhP4zcjZg0Sw7N8y5eWofiv9jxtchg94vi uFaRxxpCn9kfCc3+b1f7hRfGMmwOVx9Hmqb4rd0Q7lcoO9wmz0YLAlqGOHrV1maJRpiV EQ9MyKemWYHX/hRVs8YRSoW0Kyglq425v5GQi8mvSL8fdi22xS0AeJuyIkpuxejBh5qf upBrIMw685Rc29yk5I7t7lJssTnZCm4keKNTU86a2ljTNB4+o59/cv4CRoI4I9m5gtI5 r5xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977519; x=1754582319; 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=49cBhv+bsfX2NNvsCTVAGnNs0eP38Nq2mxipt6UO56Y=; b=wHTC0ZQs5H3dldIwyS9pEVZCGjky0znCQM65wf8yBnGkTQYcu27it/1koaFOiMJFQm CGK2/nwGETPazVXvc7g8Xqub6rpad1RRgJIiSyBdxjNIJ7tb5CSGVm6LwjIm/OEI8RtT MAgg1CW7ovjch3SOrT9Bbc0x8rEf9qBpZNOXEqNFQnykzPYztArXMVf1rb0U8WpRmUcT DUF8rrMorUQ/vweF0BG74COHK4E3Cdr0fxQnoz1jlspmAYTJN1Gl6PL+1AVmo4f+Riqc UUZmZjqjA5FpILSib5euPwBK/e/1TmM1JZYXFBwl2PgbshP3pS3Zi5W2i/B195lZ49b7 gS+w== X-Gm-Message-State: AOJu0YxV8m8C+JgfhkcuJa2LhRrJagXbJQhVr7ayxVdpzx5SWUGA3YPO Xl8Mdkim8xL2qfDf7nkD3MRSpbRH8uM5WQiMFAHQoILx1ILQ5hgtKFaVl23VDA== X-Gm-Gg: ASbGnct37lALH+rGLc4HIddUgY+c/H1UoUfCtLrUoeQxjhIVPUaTr/euJ3qVclNjGrO AqBB/EvQq0sqhZ2XEpQrReA/OjB5OfGbOhfv8BBMuFWJfjacwm66him7o+E6nZbceexMoGeHCcQ PNWKDxkYYMHg6YcxJcxKaWsyjGOO8D8zG5f5SqaJPF/BSYTArUMfJvNwBYpuWjql3IwJHdbNswj pbFBpfvMI0tKJQIXjh796W7UV7P16bYipPBlZaP58bnrPV1WZ0tGQtlfUgceoKD0oINNc4Ot/3a XlC8AiUaPCtR8I7izZHIYZaiIAdT4I6AUwzM8HVncCJ5e6PkhWLgUA6YcFvIMohfb8irx/CRxYy /jn3bie9U6boLDbCsRW/zjAgXRrwsukJGxIDHHpNBlnmPUqZ/SBOnNMhJm/qff10GcEiBgnvJ X-Google-Smtp-Source: AGHT+IEttbQXq7FEKRn1TaR0w+pfXFD3Zr+SAmyYNGaXXvKBmMp9ZcMs5JrzXtfLt7QjNaAmKN9Mrg== X-Received: by 2002:a05:6512:23a1:b0:553:297b:3d4e with SMTP id 2adb3069b0e04-55b7c0a1a35mr2788480e87.52.1753977518588; Thu, 31 Jul 2025 08:58:38 -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 v3 06/20] xen/riscv: add root page table allocation Date: Thu, 31 Jul 2025 17:58:05 +0200 Message-ID: <23c79f07221dee7eb782ebb160442f3a796a41b2.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977538537116600 Content-Type: text/plain; charset="utf-8" Introduce support for allocating and initializing the root page table required for RISC-V stage-2 address translation. To implement root page table allocation the following is introduced: - p2m_get_clean_page() and p2m_alloc_root_table(), p2m_allocate_root() helpers to allocate and zero a 16 KiB root page table, as mandated by the RISC-V privileged specification for Sv32x4/Sv39x4/Sv48x4/Sv57x4 modes. - Update p2m_init() to inititialize p2m_root_order. - Add maddr_to_page() and page_to_maddr() macros for easier address manipulation. - Introduce paging_ret_pages_to_domheap() to return some pages before allocate 16 KiB pages for root page table. - Allocate root p2m table after p2m pool is initialized. - Add construct_hgatp() to construct the hgatp register value based on p2m->root, p2m->hgatp_mode and VMID. Signed-off-by: Oleksii Kurochko --- Changes in v3: - Drop insterting of p2m->vmid in hgatp_from_page() as now vmid is allocat= ed per-CPU, not per-domain, so it will be inserted later somewhere in context_switch or before returning control to a guest. - use BIT() to init nr_pages in p2m_allocate_root() instead of open-code BIT() macros. - Fix order in clear_and_clean_page(). - s/panic("Specify more xen,domain-p2m-mem-mb\n")/return NULL. - Use lock around a procedure of returning back pages necessary for p2m root table. - Update the comment about allocation of page for root page table. - Update an argument of hgatp_from_page() to "struct page_info *p2m_root_p= age" to be consistent with the function name. - Use p2m_get_hostp2m(d) instead of open-coding it. - Update the comment above the call of p2m_alloc_root_table(). - Update the comments in p2m_allocate_root(). - Move part which returns some page to domheap before root page table allo= cation to paging.c. - Pass p2m_domain * instead of struct domain * for p2m_alloc_root_table(). - Introduce construct_hgatp() instead of hgatp_from_page(). - Add vmid and hgatp_mode member of struct p2m_domain. - Add explanatory comment above clean_dcache_va_range() in clear_and_clean_page(). - Introduce P2M_ROOT_ORDER and P2M_ROOT_PAGES. - Drop vmid member from p2m_domain as now we are using per-pCPU VMID allocation. - Update a declaration of construct_hgatp() to recieve VMID as it isn't per-VM anymore. - Drop hgatp member of p2m_domain struct as with the new VMID scheme allocation construction of hgatp will be needed more often. - Drop is_hardware_domain() case in p2m_allocate_root(), just always allocate root using p2m pool pages. - Refactor p2m_alloc_root_table() and p2m_alloc_table(). --- Changes in v2: - This patch was created from "xen/riscv: introduce things necessary for p= 2m initialization" with the following changes: - [clear_and_clean_page()] Add missed call of clean_dcache_va_range(). - Drop p2m_get_clean_page() as it is going to be used only once to alloc= ate root page table. Open-code it explicittly in p2m_allocate_root(). Also, it will help avoid duplication of the code connected to order and nr_p= ages of p2m root page table. - Instead of using order 2 for alloc_domheap_pages(), use get_order_from_bytes(KB(16)). - Clear and clean a proper amount of allocated pages in p2m_allocate_roo= t(). - Drop _info from the function name hgatp_from_page_info() and its argum= ent page_info. - Introduce HGATP_MODE_MASK and use MASK_INSR() instead of shift to calc= ulate value of hgatp. - Drop unnecessary parentheses in definition of page_to_maddr(). - Add support of VMID. - Drop TLB flushing in p2m_alloc_root_table() and do that once when VMID is re-used. [Look at p2m_alloc_vmid()] - Allocate p2m root table after p2m pool is fully initialized: first return pages to p2m pool them allocate p2m root table. --- xen/arch/riscv/include/asm/mm.h | 4 + xen/arch/riscv/include/asm/p2m.h | 12 +++ xen/arch/riscv/include/asm/paging.h | 2 + xen/arch/riscv/include/asm/riscv_encoding.h | 6 ++ xen/arch/riscv/p2m.c | 90 +++++++++++++++++++++ xen/arch/riscv/paging.c | 30 +++++++ 6 files changed, 144 insertions(+) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index 9283616c02..dd8cdc9782 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -167,6 +167,10 @@ extern struct page_info *frametable_virt_start; #define mfn_to_page(mfn) (frametable_virt_start + mfn_x(mfn)) #define page_to_mfn(pg) _mfn((pg) - frametable_virt_start) =20 +/* Convert between machine addresses and page-info structures. */ +#define maddr_to_page(ma) mfn_to_page(maddr_to_mfn(ma)) +#define page_to_maddr(pg) mfn_to_maddr(page_to_mfn(pg)) + static inline void *page_to_virt(const struct page_info *pg) { return mfn_to_virt(mfn_x(page_to_mfn(pg))); diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index f8051ed893..3c37a708db 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -9,6 +9,10 @@ =20 #include =20 +extern unsigned int p2m_root_order; +#define P2M_ROOT_ORDER p2m_root_order +#define P2M_ROOT_PAGES BIT(P2M_ROOT_ORDER, U) + #define paddr_bits PADDR_BITS =20 /* Get host p2m table */ @@ -24,6 +28,12 @@ struct p2m_domain { /* Pages used to construct the p2m */ struct page_list_head pages; =20 + /* The root of the p2m tree. May be concatenated */ + struct page_info *root; + + /* G-stage (stage-2) address translation mode */ + unsigned long hgatp_mode; + /* Indicate if it is required to clean the cache when writing an entry= */ bool clean_pte; =20 @@ -127,6 +137,8 @@ static inline void p2m_altp2m_check(struct vcpu *v, uin= t16_t idx) /* Not supported on RISCV. */ } =20 +unsigned long construct_hgatp(struct p2m_domain *p2m, uint16_t vmid); + #endif /* ASM__RISCV__P2M_H */ =20 /* diff --git a/xen/arch/riscv/include/asm/paging.h b/xen/arch/riscv/include/a= sm/paging.h index 8fdaeeb2e4..557fbd1abc 100644 --- a/xen/arch/riscv/include/asm/paging.h +++ b/xen/arch/riscv/include/asm/paging.h @@ -10,4 +10,6 @@ int paging_domain_init(struct domain *d); int paging_freelist_init(struct domain *d, unsigned long pages, bool *preempted); =20 +bool paging_ret_pages_to_domheap(struct domain *d, unsigned int nr_pages); + #endif /* ASM_RISCV_PAGING_H */ diff --git a/xen/arch/riscv/include/asm/riscv_encoding.h b/xen/arch/riscv/i= nclude/asm/riscv_encoding.h index 6cc8f4eb45..8362df8784 100644 --- a/xen/arch/riscv/include/asm/riscv_encoding.h +++ b/xen/arch/riscv/include/asm/riscv_encoding.h @@ -133,11 +133,13 @@ #define HGATP_MODE_SV48X4 _UL(9) =20 #define HGATP32_MODE_SHIFT 31 +#define HGATP32_MODE_MASK _UL(0x80000000) #define HGATP32_VMID_SHIFT 22 #define HGATP32_VMID_MASK _UL(0x1FC00000) #define HGATP32_PPN _UL(0x003FFFFF) =20 #define HGATP64_MODE_SHIFT 60 +#define HGATP64_MODE_MASK _ULL(0xF000000000000000) #define HGATP64_VMID_SHIFT 44 #define HGATP64_VMID_MASK _ULL(0x03FFF00000000000) #define HGATP64_PPN _ULL(0x00000FFFFFFFFFFF) @@ -170,6 +172,7 @@ #define HGATP_VMID_SHIFT HGATP64_VMID_SHIFT #define HGATP_VMID_MASK HGATP64_VMID_MASK #define HGATP_MODE_SHIFT HGATP64_MODE_SHIFT +#define HGATP_MODE_MASK HGATP64_MODE_MASK #else #define MSTATUS_SD MSTATUS32_SD #define SSTATUS_SD SSTATUS32_SD @@ -181,8 +184,11 @@ #define HGATP_VMID_SHIFT HGATP32_VMID_SHIFT #define HGATP_VMID_MASK HGATP32_VMID_MASK #define HGATP_MODE_SHIFT HGATP32_MODE_SHIFT +#define HGATP_MODE_MASK HGATP32_MODE_MASK #endif =20 +#define GUEST_ROOT_PAGE_TABLE_SIZE KB(16) + #define TOPI_IID_SHIFT 16 #define TOPI_IID_MASK 0xfff #define TOPI_IPRIO_MASK 0xff diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 214b4861d2..cac07c51c9 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -1,8 +1,86 @@ +#include #include #include #include =20 #include +#include +#include + +unsigned int __read_mostly p2m_root_order; + +static void clear_and_clean_page(struct page_info *page) +{ + clear_domain_page(page_to_mfn(page)); + + /* + * If the IOMMU doesn't support coherent walks and the p2m tables are + * shared between the CPU and IOMMU, it is necessary to clean the + * d-cache. + */ + clean_dcache_va_range(page, PAGE_SIZE); +} + +static struct page_info *p2m_allocate_root(struct domain *d) +{ + struct page_info *page; + + /* + * As mentioned in the Priviliged Architecture Spec (version 20240411) + * in Section 18.5.1, for the paged virtual-memory schemes (Sv32x4, + * Sv39x4, Sv48x4, and Sv57x4), the root page table is 16 KiB and must + * be aligned to a 16-KiB boundary. + */ + page =3D alloc_domheap_pages(d, P2M_ROOT_ORDER, MEMF_no_owner); + if ( !page ) + return NULL; + + for ( unsigned int i =3D 0; i < P2M_ROOT_PAGES; i++ ) + clear_and_clean_page(page + i); + + return page; +} + +unsigned long construct_hgatp(struct p2m_domain *p2m, uint16_t vmid) +{ + unsigned long ppn; + + ppn =3D PFN_DOWN(page_to_maddr(p2m->root)) & HGATP_PPN; + + /* TODO: add detection of hgatp_mode instead of hard-coding it. */ +#if RV_STAGE1_MODE =3D=3D SATP_MODE_SV39 + p2m->hgatp_mode =3D HGATP_MODE_SV39X4; +#elif RV_STAGE1_MODE =3D=3D SATP_MODE_SV48 + p2m->hgatp_mode =3D HGATP_MODE_SV48X4; +#else +# error "add HGATP_MODE" +#endif + + return ppn | MASK_INSR(p2m->hgatp_mode, HGATP_MODE_MASK) | + MASK_INSR(vmid, HGATP_VMID_MASK); +} + +static int p2m_alloc_root_table(struct p2m_domain *p2m) +{ + struct domain *d =3D p2m->domain; + struct page_info *page; + const unsigned int nr_root_pages =3D P2M_ROOT_PAGES; + + /* + * Return back nr_root_pages to assure the root table memory is also + * accounted against the P2M pool of the domain. + */ + if ( !paging_ret_pages_to_domheap(d, nr_root_pages) ) + return -ENOMEM; + + page =3D p2m_allocate_root(d); + if ( !page ) + return -ENOMEM; + + p2m->root =3D page; + + return 0; +} =20 int p2m_init(struct domain *d) { @@ -32,6 +110,8 @@ int p2m_init(struct domain *d) # error "Add init of p2m->clean_pte" #endif =20 + p2m_root_order =3D get_order_from_bytes(GUEST_ROOT_PAGE_TABLE_SIZE); + return 0; } =20 @@ -42,10 +122,20 @@ int p2m_init(struct domain *d) */ int p2m_set_allocation(struct domain *d, unsigned long pages, bool *preemp= ted) { + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); int rc; =20 if ( (rc =3D paging_freelist_init(d, pages, preempted)) ) return rc; =20 + /* + * First, initialize p2m pool. Then allocate the root + * table so that the necessary pages can be returned from the p2m pool, + * since the root table must be allocated using alloc_domheap_pages(..= .) + * to meet its specific requirements. + */ + if ( !p2m->root ) + p2m_alloc_root_table(p2m); + return 0; } diff --git a/xen/arch/riscv/paging.c b/xen/arch/riscv/paging.c index 8882be5ac9..bbe1186900 100644 --- a/xen/arch/riscv/paging.c +++ b/xen/arch/riscv/paging.c @@ -54,6 +54,36 @@ int paging_freelist_init(struct domain *d, unsigned long= pages, =20 return 0; } + +bool paging_ret_pages_to_domheap(struct domain *d, unsigned int nr_pages) +{ + struct page_info *page; + + ASSERT(spin_is_locked(&d->arch.paging.lock)); + + if ( ACCESS_ONCE(d->arch.paging.total_pages) < nr_pages ) + return false; + + for ( unsigned int i =3D 0; i < nr_pages; i++ ) + { + /* Return memory to domheap. */ + page =3D page_list_remove_head(&d->arch.paging.freelist); + if( page ) + { + ACCESS_ONCE(d->arch.paging.total_pages)--; + free_domheap_page(page); + } + else + { + printk(XENLOG_ERR + "Failed to free P2M pages, P2M freelist is empty.\n"); + return false; + } + } + + return true; +} + /* Domain paging struct initialization. */ int paging_domain_init(struct domain *d) { --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977542; cv=none; d=zohomail.com; s=zohoarc; b=BqPBKlogBmSYgsRZf7Ck0W1l/DJlJgZXGse0eoe48pTS5JGub8mzd6Q/j2GrWzX8XThBPqbQqBOV/PWtwvWNahzrzC/AM++g2eFmIC95VZxkbEXFi2lIaVVbaUcF18cWnJiiUnmUHAMbjqEOYJ5+anoONYZc8aZ4L5Z8Wdrynsg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977542; 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=U7ABKQNzNNhVtyIOsZzusvptv27vOsEVorOK0u4RX9Y=; b=PAok41mT2IMNYkCeP056i9iIdUuhufoZvpbFaKH+cYMFoOPBQfRR6iEL9J0X6Ou6VGBLrgI5+3wSkhFZ7a6XS4y18zwT6qNgcNFgQ6Tdb0yDeZWhS8R3r4Bh8oGgo8nI8WNi7AgYCIGrEc+6vOibM6tUGTsFJy9daIjcPoGXtdU= 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 1753977542923956.2675651055235; Thu, 31 Jul 2025 08:59:02 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065874.1431261 (Exim 4.92) (envelope-from ) id 1uhVg5-0003IQ-3B; Thu, 31 Jul 2025 15:58:45 +0000 Received: by outflank-mailman (output) from mailman id 1065874.1431261; Thu, 31 Jul 2025 15:58:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVg4-0003Hm-V1; Thu, 31 Jul 2025 15:58:44 +0000 Received: by outflank-mailman (input) for mailman id 1065874; Thu, 31 Jul 2025 15:58:42 +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 1uhVg2-0001FB-M6 for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:42 +0000 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [2a00:1450:4864:20::12e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3a6ede05-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:41 +0200 (CEST) Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-55b81b58eb7so1089760e87.2 for ; Thu, 31 Jul 2025 08:58:41 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:39 -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: 3a6ede05-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977520; x=1754582320; 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=U7ABKQNzNNhVtyIOsZzusvptv27vOsEVorOK0u4RX9Y=; b=lC6IvenN/MykcX/nn6mTFrY9NDOw9x+h7i514hyX9ZuP0JWfJmq0xb+e8Nbb8bQ5bT tC0g2qtCQPvyG5wzB2QyJdJsJZsAOKbVWdmeeP/IOswxuVJpr2/oiIICB6qj2H+OUHHQ oGFpqMc+xZQ0FQUvNWAM8IXpPrClY0N3xHkBH2x02S8y4BBUkqGY3e9MpECbzJzbdF0m kBn6cImyhvFHdSzv446yPRte9UFTZ36n9bI/numWm+m4+c/ImGFdhA7+eRThALu82CD5 uKQPjf4L3xT2kLx32jhxNDE9F5AMCIctcFrhDbUrolYl3RaqS4itpTSIRx4o58DOrpwc BYmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977520; x=1754582320; 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=U7ABKQNzNNhVtyIOsZzusvptv27vOsEVorOK0u4RX9Y=; b=vhyoNxmexCEpbGWl3fZy5TL02JTOzqf7obRgpl3zHEbK0fNDwNWflUYrfC6IZ24I4C i3DRAeAD3fNLk8YM5AUTMgUA+UAHIqOc2otDUt9ktoYEI5nCTMqfssLZRbt7rtbgHpfh IOdRUUPT9TpMq2uwvHNTrgjfju4FJ6m1kssEgbvjG54KYYouMhtkmsskOIfI2qcmui7F 5w81JMyBbwfunEC5v/PI8plZNEiND8xwsQA6dp6g6GgoHe/fpRKMSeIO24UzV3p5F0Mb x0eCG+8lp4WXLVdWdoj+qqzsAPUnuSrTZuqBgFVSdiP936LQDIg23WpBmxnEN9Nl7Y53 kxgA== X-Gm-Message-State: AOJu0YxZZyiV80+mLIYEncfU3dgB5rgmhadKE584AugZJoSbFU1O6OGy UWvNsPPjSRBhMe3EcBr0L1OZvm1R1WyO1PAaRT+ThV9TsSsDYL6H82zKz0gaeA== X-Gm-Gg: ASbGncvmFdTJMZM2x439mVImBaBWGgbfm14tk2qHs6OkJGqbQsul2CE8atpCyZXTcqV Ohy0E3UZNKhPd0kIhNxib+PBz4tHDPcm3xVaLMctxiDZmzHStFW7AOOjeIBzpZCBm4g+oU9UkqS g30t1uoBz/JUCxv940nWWVH52jwZ4mV8AfJF3pEwucrEVqh2x+iu2gvjF29ilv1cOITJY/D5BkA O9zMCTIMmKJwHVTzOyCRbx5D+w6+sG0LoMbFP+r9YG/5h7YB3TVJImqimWpNkUDdqx8Vs1M4yCz +k7tnE5K7Xjn7dBRaphOv7t308vD3oFEbhhb1gWzPRHLVN42jN+BbwVQsyWhIEDRtYurxfnlyvd wwio/ugSOlgWQEYg99zcx410oMtz1oJtvr2jAtESwX00sPGiyskaM4z/NqVSUH5u9nHNK0tV1 X-Google-Smtp-Source: AGHT+IFZ9/P5txhHOZcIdYQLMQy0yd9U4fQ1grk2NEjMr6QyX768RyqZ0vB1YRo0Kb7qktheL/olvw== X-Received: by 2002:a05:6512:304f:b0:550:d4f3:8491 with SMTP id 2adb3069b0e04-55b7c08e2c0mr2408270e87.41.1753977520212; Thu, 31 Jul 2025 08:58:40 -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 v3 07/20] xen/riscv: introduce pte_{set,get}_mfn() Date: Thu, 31 Jul 2025 17:58:06 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977544307116600 Content-Type: text/plain; charset="utf-8" Introduce helpers pte_{set,get}_mfn() to simplify setting and getting of mfn. Also, introduce PTE_PPN_MASK and add BUILD_BUG_ON() to be sure that PTE_PPN_MASK remains the same for all MMU modes except Sv32. Signed-off-by: Oleksii Kurochko Acked-by: Jan Beulich --- Changes in V3: - Add Acked-by: Jan Beulich . --- Changes in V2: - Patch "[PATCH v1 4/6] xen/riscv: define pt_t and pt_walk_t structures" w= as renamed to xen/riscv: introduce pte_{set,get}_mfn() as after dropping of bitfields for PTE structure, this patch introduce only pte_{set,get}_mfn= (). - As pt_t and pt_walk_t were dropped, update implementation of pte_{set,get}_mfn() to use bit operations and shifts instead of bitfield= s. - Introduce PTE_PPN_MASK to be able to use MASK_INSR for setting/getting P= PN. - Add BUILD_BUG_ON(RV_STAGE1_MODE > SATP_MODE_SV57) to be sure that when new MMU mode will be added, someone checks that PPN is still bits 53:10. --- xen/arch/riscv/include/asm/page.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/xen/arch/riscv/include/asm/page.h b/xen/arch/riscv/include/asm= /page.h index ddcc4da0a3..66cb192316 100644 --- a/xen/arch/riscv/include/asm/page.h +++ b/xen/arch/riscv/include/asm/page.h @@ -112,6 +112,30 @@ typedef struct { #endif } pte_t; =20 +#if RV_STAGE1_MODE !=3D SATP_MODE_SV32 +#define PTE_PPN_MASK _UL(0x3FFFFFFFFFFC00) +#else +#define PTE_PPN_MASK _U(0xFFFFFC00) +#endif + +static inline void pte_set_mfn(pte_t *p, mfn_t mfn) +{ + /* + * At the moment spec provides Sv32 - Sv57. + * If one day new MMU mode will be added it will be needed + * to check that PPN mask still continue to cover bits 53:10. + */ + BUILD_BUG_ON(RV_STAGE1_MODE > SATP_MODE_SV57); + + p->pte &=3D ~PTE_PPN_MASK; + p->pte |=3D MASK_INSR(mfn_x(mfn), PTE_PPN_MASK); +} + +static inline mfn_t pte_get_mfn(pte_t p) +{ + return _mfn(MASK_EXTR(p.pte, PTE_PPN_MASK)); +} + static inline bool pte_is_valid(pte_t p) { return p.pte & PTE_VALID; --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977548; cv=none; d=zohomail.com; s=zohoarc; b=hodvxKat3XARTi0phKTL//PR0zCa+qYSCdzbwMyvC6dtYfqswhoFKWtqrE3tu991owUwO8OqWJ0dLoMWF4KW9T65zNv6e01LHF1hU8wAs2GRWIbOc/l24MHKkIKYFGQ2tsAUSmOfsIuKfax1pI2UVo/WQmlZJ6iJdIrYllK286w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977548; 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=AvVQ0FbSOnQU5awNmCzNnO6W0RjQ/JOFOltcFhlpheU=; b=KLJ50HFhSjg6aUckNmxdyGuakXSdr6pGaY5Yq9XEfAV/NEZTNy/NMIWi8GgahjTneaPftRAq9QcZrJLMZMmu1G0UAXpXl9me78qRPecFGW5d2necqF67ccl6oefI5o6WBC1gzkwlfu4A9quvgDkv0YA8mnHydkHcdORSc6QRWHs= 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 175397754816490.19538464636685; Thu, 31 Jul 2025 08:59:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065878.1431272 (Exim 4.92) (envelope-from ) id 1uhVg6-0003VC-GJ; Thu, 31 Jul 2025 15:58:46 +0000 Received: by outflank-mailman (output) from mailman id 1065878.1431272; Thu, 31 Jul 2025 15:58:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVg6-0003TA-4C; Thu, 31 Jul 2025 15:58:46 +0000 Received: by outflank-mailman (input) for mailman id 1065878; Thu, 31 Jul 2025 15:58:44 +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 1uhVg4-0001FB-C2 for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:44 +0000 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [2a00:1450:4864:20::134]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3b333a37-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:42 +0200 (CEST) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-55b859545c3so1221538e87.0 for ; Thu, 31 Jul 2025 08:58:42 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:41 -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: 3b333a37-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977522; x=1754582322; 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=AvVQ0FbSOnQU5awNmCzNnO6W0RjQ/JOFOltcFhlpheU=; b=a8DFdFzCG9ILeRY0e/54ye3QQQi7FefMM+fb3O/4ITx37S+nRLubkYHplsXsPH+5Wt Aalz0uLotDPLcwLvqe+EI8LbJWLbBNEXFTeMKTF8gVceyElFK2Yth705jE9M3p2/sdbP zn9J4BXKcK8ZkmfIbaQtFyke13s4gyCs+QcZplA8As6JzXnOXkRsLR94JSH0g5XYZX3g DuN54eG1V5n5f0GJLw6c+XFChAHf09rQYBBQ5kzgalFfFE+qApipgeBpOY68Q28rrFlf zUmlLREyIYAET1q/wNyxwolnAm2vvPVR9pjuHws1n8+yJYjx1UigycNmecqHi0iSAHki ufXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977522; x=1754582322; 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=AvVQ0FbSOnQU5awNmCzNnO6W0RjQ/JOFOltcFhlpheU=; b=DXeYsLflDLTK6z57pDE/TDFT80KqopjjV2fCKMlZcm3QGbgW8VNZ6hRcpAYAdMA+uQ xhznWFBLtU2mIaOc8TGA4kvErHVUcw3sac4x884jKrzl1IBNHyz7mPrqXcviQzBhQ612 9pVNKegGqqh1qU0HAHhcT4UCnz8LmbaToPjzzCLjMW6ee9+pe9sU0ENwUDplB8TXrE22 JVychysI5b3imzesqlyC0/+o6KJ6XKCO36qC7W2+tGrEeSg9uSjLBI80UfPl871qUHyO uOPcBIdnMGeGgh+BlYbQgEN2/o1iAPtj1S1vyQ+1J9M/7ObwXq1WuWKiRTR4X8DZsDpC VRxg== X-Gm-Message-State: AOJu0YzhGbiO8r0i5SXZ4UXYd/1bdT3ePOidUR8VL5OcRZYwAHI4W2LI p6RxcbCSHUQi5bSzsq/eXentVruo/IihsAVCoPfArrpcV18Aq1XWAXjJ+97Fcw== X-Gm-Gg: ASbGnctOZ2mAWsW8b2E4brTV0baTpiD57U36qAbwIBVxUceVSKnMwXGhCCxa0saVBuD cTND6GOZlo7CZVM4qZ2A79m8N4NqMGbZl8qLo2qpISl3Z+eOJ9Gl/hDCrF9aUFgD5wtCQH8TpAG bRGrgByrUxUJiQNH9v8RNuwR0m4ixMg5tWg7gUdkikuCvo6NbR8qAGY63yYNq5szSVpoCDLpgLh 1/yNrtYyCptfME6M8nsZVmZ3al+uAAiICo25FV7cGc67gKkIqqH3jsZNFaVH8IsYgzKyujQArz5 RIFj7JEOnWrHvDSg6Q/ICM4Mcv76YBBtwMyLo9DBVurw/QwIp/qfMulQo4Eu4X4fP5q7af2S/VY +qw+21BcfGMwHfLMZLa4y7E/4ob3XKAysIHF8zlPLq2u6QTfHOHzgl9gQJYBTJw== X-Google-Smtp-Source: AGHT+IGfhYrqfOW245zy9Jhh4v3hYQUoQNMXIvYeE7OKblKWd/4uJrpdvGZVRg1RWVeYvGobZBqyNA== X-Received: by 2002:a05:6512:1383:b0:55b:7ff4:bb3b with SMTP id 2adb3069b0e04-55b88c8809emr893317e87.21.1753977521586; Thu, 31 Jul 2025 08:58:41 -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 v3 08/20] xen/riscv: add new p2m types and helper macros for type classification Date: Thu, 31 Jul 2025 17:58:07 +0200 Message-ID: <3d35b6f4bb79048647020ed4e7b222585ca3a9a3.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977550338116600 Content-Type: text/plain; charset="utf-8" - Extended p2m_type_t with additional types: p2m_mmio_direct, p2m_grant_map_{rw,ro}. - Added macros to classify memory types: P2M_RAM_TYPES, P2M_GRANT_TYPES. - Introduced helper predicates: p2m_is_ram(), p2m_is_any_ram(). - Define p2m_mmio_direct to tell handle_passthrough_prop() from common code how to map device memory. Signed-off-by: Oleksii Kurochko --- Changes in V3: - Drop p2m_ram_ro. - Rename p2m_mmio_direct_dev to p2m_mmio_direct_io to make it more RISC-V = specicific. - s/p2m_mmio_direct_dev/p2m_mmio_direct_io. --- Changes in V2: - Drop stuff connected to foreign mapping as it isn't necessary for RISC-V right now. --- xen/arch/riscv/include/asm/p2m.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index 3c37a708db..5f253da1dd 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -62,8 +62,30 @@ struct p2m_domain { typedef enum { p2m_invalid =3D 0, /* Nothing mapped here */ p2m_ram_rw, /* Normal read/write domain RAM */ + p2m_mmio_direct_io, /* Read/write mapping of genuine Device MMIO area, + PTE_PBMT_IO will be used for such mappings */ + p2m_ext_storage, /* Following types'll be stored outsude PTE bits: = */ + p2m_grant_map_rw, /* Read/write grant mapping */ + p2m_grant_map_ro, /* Read-only grant mapping */ } p2m_type_t; =20 +#define p2m_mmio_direct p2m_mmio_direct_io + +/* We use bitmaps and mask to handle groups of types */ +#define p2m_to_mask(t_) BIT(t_, UL) + +/* RAM types, which map to real machine frames */ +#define P2M_RAM_TYPES (p2m_to_mask(p2m_ram_rw)) + +/* Grant mapping types, which map to a real frame in another VM */ +#define P2M_GRANT_TYPES (p2m_to_mask(p2m_grant_map_rw) | \ + p2m_to_mask(p2m_grant_map_ro)) + +/* Useful predicates */ +#define p2m_is_ram(t_) (p2m_to_mask(t_) & P2M_RAM_TYPES) +#define p2m_is_any_ram(t_) (p2m_to_mask(t_) & \ + (P2M_RAM_TYPES | P2M_GRANT_TYPES)) + #include =20 static inline int get_page_and_type(struct page_info *page, --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977785; cv=none; d=zohomail.com; s=zohoarc; b=UsxwagM3BbgiSGi9GqoT6cxzePwRR80SmNQ5qknpl0wkKVF5aj5eeoQnpwitFDOidAlwx4+7RfqrMC07Xf6QXpVUpceaZgVCcKXhWeepgG/BYlJQNc+YvQ9qdVnQyjqUn3SbnJ8JUOpi8j7jNyj1nY57Ko11szsjjExk9j++VHE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977785; 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=pEOkzQdBxpbC9Okf9MXfHH5aCeAaO0EgwJT3GQlkxY4=; b=DfTQUwwJsLKZxgPgHtc5CpJikXl/2FV7vn9Hkk/V7iSxI+Y6ptKbX0bSotpm01+6igaT0BHbF26nfgiUjgeRdUJ+FV1yHicbAlPnT5MnCy3hzCd15m9cX+dNcAOSd8EPGnZB9tzJYGeD9KAwnTD7PrBTVXAHMY0m19jns1y+YqA= 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 1753977785830226.32383402874757; Thu, 31 Jul 2025 09:03:05 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065993.1431392 (Exim 4.92) (envelope-from ) id 1uhVk6-0005Cv-Kc; Thu, 31 Jul 2025 16:02:54 +0000 Received: by outflank-mailman (output) from mailman id 1065993.1431392; Thu, 31 Jul 2025 16:02:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVk6-0005Cm-GN; Thu, 31 Jul 2025 16:02:54 +0000 Received: by outflank-mailman (input) for mailman id 1065993; Thu, 31 Jul 2025 16:02:53 +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 1uhVg6-0001FB-CR for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:46 +0000 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [2a00:1450:4864:20::12d]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3bee2649-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:43 +0200 (CEST) Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-55b85413019so600238e87.0 for ; Thu, 31 Jul 2025 08:58:43 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:42 -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: 3bee2649-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977523; x=1754582323; 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=pEOkzQdBxpbC9Okf9MXfHH5aCeAaO0EgwJT3GQlkxY4=; b=YMKEAMhjuWtxzRUxAneynLhqIVice5DnDqxQLQ9PET6hiML/XzAmi46VWrMsVCvru8 UiNes28DBwPfJUU5HdW1EEC+JUvsuG1PzqCesS/dGJcGNSqYXp/XOcLks3pEYDR/mJRb uoEK3AznXY3hQ1kiwPtzDP+lcWDQxlSdw4EJV1qnPZeAQz93nNPdmiHfuMhBCGMIIl8B EEGiMvSEtU5GjB/BEdKpYsWUR7Q0OP86HZOXzwWRCGwSoXl5zYqHUEaxa5SwGsvlEdKC 1yFRj7wyUxWvdYZN8malKe4pFL9aIxCnorZpYGM2C57KEwsutSiVgkpLecW9vvKnTZug R7XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977523; x=1754582323; 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=pEOkzQdBxpbC9Okf9MXfHH5aCeAaO0EgwJT3GQlkxY4=; b=kHT5W+crpdEDTf1O4S3YmiDOpsJd3eZbJ2QWXkKl3liyNannwqQ0dwOF5m2A+NVp9A inzS9uUKcsInAJSUxAN4KTXwuuxckwrvu7WWwxMNmx6KpkTGFMSDnbjBSA1JliAJevTA JoRngTx/q3teaOQxuw5nV6DlZw4onRqjoPLw1TQDO/76i9cecqU4Umfngk6od9OmkPWI cElrmMlzTs73kCuli6VuLK1WyweLCMpGOP664IZbqsDI92u3e/PjagV7Gw3kowiLSFiw l5KKXo4P3jAPNfa/1Q1LhrikAO/EBkR11jWm8iccnwl0M3gKtAIvim6CJBc5tdwKrfz3 5lBQ== X-Gm-Message-State: AOJu0YyzEHnU7RKaO97nR2fx/9Olt12mnPETE6gXOhljUPjirZUYRFN6 zwObndqd2yKW4KeYompu/HNptSm3+wzPy9kNXy8WQ1l1f45zy8jkoUIfF2v8dg== X-Gm-Gg: ASbGnctyniT0zI4z1ObHCFuhwviRRowzsN/iqQ7cb2X0O7XZffAct8N38KB0ET62T40 0aXfkGb0EAWdSw/0PiAt3nSmOejAO0PqHKtGVt+oSeuHy5hb3FjbH2KOQvjnY/jUlS/YJ/6wtY7 2Fknv59k1MlHQZtNAx3p5N2BV0KBfBP4wP/99oDJw521IFGcgWWaFttHWMCm0mnMAnnOsbk+Tbu 49QYXSem1z/wMwWpviyq8+PJFJri9C0L4bGzS8l2Z8gZXZiYLcZGrLjT53IcJr/+enUp5tqdIkZ +JpCDNdYICtjqz3U15ZuRC2/g1v3g5coUjwfPjLLdkdkZJ3+MIf7F2+JLEcPdjpo9P07YlDGM8t SQYs1RTReZ3Wwxsua0qqWx4JHrIgQy57k2KStwCfAHzuzbbidjtU0h7xmTWWCpw== X-Google-Smtp-Source: AGHT+IFLTOuKuRf2T7tNGueoS/xGQR2k3DSUUMxZkxu+W21mtiv+Yw78Y6yFbwkuLfLx9uu1tuhAbA== X-Received: by 2002:a05:6512:3ba7:b0:554:f72c:819d with SMTP id 2adb3069b0e04-55b7c08ec37mr2819368e87.43.1753977522774; Thu, 31 Jul 2025 08:58:42 -0700 (PDT) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Oleksii Kurochko , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Jan Beulich Subject: [PATCH v3 09/20] xen/dom0less: abstract Arm-specific p2m type name for device MMIO mappings Date: Thu, 31 Jul 2025 17:58:08 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 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: 1753977787882116600 Rename `p2m_mmio_direct_dev` to a more architecture-neutral alias `p2m_mmio_direct` to avoid leaking Arm-specific naming into common Xen code, such as dom0less passthrough property handling. This helps reduce platform-specific terminology in shared logic and improves clarity for future non-Arm ports (e.g. RISC-V or PowerPC). No functional changes =E2=80=94 the definition is preserved via a macro ali= as for Arm. Suggested-by: Jan Beulich Signed-off-by: Oleksii Kurochko --- Changes in v3: - New patch --- xen/arch/arm/include/asm/p2m.h | 2 ++ xen/common/device-tree/dom0less-build.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/include/asm/p2m.h b/xen/arch/arm/include/asm/p2m.h index 2d53bf9b61..bade1eb71b 100644 --- a/xen/arch/arm/include/asm/p2m.h +++ b/xen/arch/arm/include/asm/p2m.h @@ -137,6 +137,8 @@ typedef enum { p2m_max_real_type, /* Types after this won't be store in the p2m */ } p2m_type_t; =20 +#define p2m_mmio_direct p2m_mmio_direct_dev + /* We use bitmaps and mask to handle groups of types */ #define p2m_to_mask(_t) (1UL << (_t)) =20 diff --git a/xen/common/device-tree/dom0less-build.c b/xen/common/device-tr= ee/dom0less-build.c index 6bb038111d..5b97bf0343 100644 --- a/xen/common/device-tree/dom0less-build.c +++ b/xen/common/device-tree/dom0less-build.c @@ -185,7 +185,7 @@ static int __init handle_passthrough_prop(struct kernel= _info *kinfo, gaddr_to_gfn(gstart), PFN_DOWN(size), maddr_to_mfn(mstart), - p2m_mmio_direct_dev); + p2m_mmio_direct); if ( res < 0 ) { printk(XENLOG_ERR --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977718; cv=none; d=zohomail.com; s=zohoarc; b=nmL6lr3DBmVuTuQMrLgI1Whad/Si+JTCq4WSEDu0G9PPN/HGoB8pb2KwSK4lZY79J6br2pkboJ7kluu2jNoZVtM24L+JU4b3o8MfWl7kbsN1TvJG1g5srsajoAx1Y0W+leXgPjSCjLjXiCCLNsUmmz21fXsLXumxOmd1INkAF3o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977718; 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=+nbgAo/EOUFfdJLvmZKiVtMFCYW00seKKM+9UoUaOzw=; b=iLBdE3YyoJzrK4Et+M422YWEV7TOYlnhSRQdKNhcXgF9HUAFEgXfp1Mx7x+nyICgyDrROVfEWwOtqLqsvEEDcrBVpSh7iK00Yoijg2NaBsMNqMqv6diZbjMDZVJsU6zst2djqvzhFNrVedEJ+B0vz6i8MIq8N49hvvzTnjP6HRQ= 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 1753977718689495.64133663798157; Thu, 31 Jul 2025 09:01:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065957.1431333 (Exim 4.92) (envelope-from ) id 1uhVit-00028o-Vd; Thu, 31 Jul 2025 16:01:39 +0000 Received: by outflank-mailman (output) from mailman id 1065957.1431333; Thu, 31 Jul 2025 16:01:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVit-00028g-RS; Thu, 31 Jul 2025 16:01:39 +0000 Received: by outflank-mailman (input) for mailman id 1065957; Thu, 31 Jul 2025 16:01:38 +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 1uhVg7-0001FB-CN for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:47 +0000 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [2a00:1450:4864:20::136]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3cf6b234-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:45 +0200 (CEST) Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-55b797ad392so1255264e87.3 for ; Thu, 31 Jul 2025 08:58:45 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:43 -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: 3cf6b234-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977525; x=1754582325; 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=+nbgAo/EOUFfdJLvmZKiVtMFCYW00seKKM+9UoUaOzw=; b=LLsrJZtqlmLun+dyhAOWMHM3D44R6aJGCC0ZaoZzBgk9Wy2mYpQYvRvCj1vD2GS9Uo GUSlSEskoKh+3KnbLb8P1TZkFfCppGzPwTsjg0xiTMiwCzsj9iRcO8maWg/kMsQquncg ZKavP0s6E1uUiBUUgGe+p9iIv7MevgxZev4yKMY4NeBhp7xQo8EZ8m/axAaaW8EfG0bb c3xJxSof8vKsi03AjoeRzxBVhf+7HeMGOJRXbI6pE3vWuDpqQvootwTFvC7EGLlRGs0G kOx4WiCkq9XG/LTPbL6Ih/wy5jpa7jjeVKzgojUsXDiB2T9IT0V3pLinO4Wfpk2YTRyl EFmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977525; x=1754582325; 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=+nbgAo/EOUFfdJLvmZKiVtMFCYW00seKKM+9UoUaOzw=; b=vhQxT+EcC9F+CrODX81SbDC8XxycJXi/i9AjwUcQ4qDaFytV3XWHRyKHexI5ry6zPA UpV2Cc8SioO3wyirJQKtK9bNtCse4GqBjcTGgIHVv6bwYI68QroqIaEomOkq+yrxRalS 0Nf45XzGldDJGn3jFLlnNRSCUkcoCz7AxgTWdOZCV3nsZQzU//NGblfsqXG+AApfZroi dJm6Bu0a9AKl0+P/J2Y8qIDnPtSlojCPinqI2zc6VT2NztXNJDNLy5bcRdgxORuEPnUA 34ZFQcZu78mOZQYIX5BlAODZHo6JUytGL+nra+gMZ8PqUlKzaRmYk3FZiFqwIE8VKQJa UhMg== X-Gm-Message-State: AOJu0YzShPvdWvyIjy0yQwAxu9a7yuk22G+phmNabWDjakKib3ky2faX 2n71IOm9UltSeJj2mXG8fWTWzPkYvzFdoIMCxxt17V7izn2htEnNT1UNULdGEA== X-Gm-Gg: ASbGncuUZng4+RWGpBhfVj7kA+Hvz7YVSV9nYGUmRS/vigMxqsC1t7tp3WrXUywMAFg ayr2YwhNJCdkLGaQ+lbEeguCOROBitMPtOYWDyyFA5Asu6jUvRco58/K5hSrKOQV8Bi/xeoRfbQ pTJVd8cgeGgVmYCr2U41uWZs3VWzicgLhJoJxoThAbyC4KzzFVg6jPdRKap8Cq3k0KUbOuxNK26 hNXgxb4785te/6hhuev//rZPzs9I7ueuK2IYQcTXyTAh0b3gaUTSrzHP4c6SaT+IFt4dj3W1ruv gfejD+8TDaug3ExBEI394snbgmFltib9ooLLSluftELajZ6zcqjR3znh9c7jndkLUznkhHBMD4z vnn6ZdNo8mMDYcsvp1uYbLcC1y4v6JcX1Xi59s+XdqL9FSTkO2yAKegH3MO6ghw== X-Google-Smtp-Source: AGHT+IEfPnxxstawg0t0+hxbihwE+m/Yi+Rgy3vIvrVw8z8BawDEexyvy7KHMbrundlf7tSR0wwAxQ== X-Received: by 2002:a05:6512:3ba8:b0:55b:8205:b1f2 with SMTP id 2adb3069b0e04-55b8205b426mr2435374e87.47.1753977524538; Thu, 31 Jul 2025 08:58:44 -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 v3 10/20] xen/riscv: introduce page_{get,set}_xenheap_gfn() Date: Thu, 31 Jul 2025 17:58:09 +0200 Message-ID: <5aa3beb7fc0b7fc7f4d6d07ae25f37d44fc38858.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977720983116600 Content-Type: text/plain; charset="utf-8" Introduce page_set_xenheap_gfn() to encode the GFN associated with a Xen he= ap page directly into the type_info field of struct page_info. Introduce page_get_xenheap_gfn() to retrieve the GFN from a Xen heap page. Reserve the upper 10 bits of type_info for the usage counter and frame type; use the remaining lower bits to store the grant table frame GFN. This is sufficient for all supported RISC-V MMU modes: Sv32 uses 22-bit GFN= s, while Sv39, Sv47, and Sv57 use up to 44-bit GFNs. Define PGT_gfn_mask and PGT_gfn_width to ensure a consistent bit layout across all RISC-V MMU modes, avoiding the need for mode-specific ifdefs. Signed-off-by: Oleksii Kurochko --- Changes in v3: - Update the comment above defintions of PGT_gfn_width, PGT_gfn_mask. - Add page_get_xenheap_gfn(). - Make commit message clearer. --- Changes in v2: - This changes were part of "xen/riscv: implement p2m mapping functionalit= y". No additional changes were done. --- xen/arch/riscv/include/asm/mm.h | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index dd8cdc9782..7950d132c1 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -12,6 +12,7 @@ #include #include =20 +#include #include #include =20 @@ -247,9 +248,17 @@ static inline bool arch_mfns_in_directmap(unsigned lon= g mfn, unsigned long nr) #define PGT_writable_page PG_mask(1, 1) /* has writable mappings? = */ #define PGT_type_mask PG_mask(1, 1) /* Bits 31 or 63. = */ =20 -/* Count of uses of this frame as its current type. */ -#define PGT_count_width PG_shift(2) -#define PGT_count_mask ((1UL << PGT_count_width) - 1) + /* 9-bit count of uses of this frame as its current type. */ +#define PGT_count_mask PG_mask(0x3FF, 10) + +/* + * Stored in bits [22:0] (Sv32) or [44:0] (Sv39,48,57) GFN if page is + * xenheap page. + */ +#define PGT_gfn_width PG_shift(10) +#define PGT_gfn_mask (BIT(PGT_gfn_width, UL) - 1) + +#define PGT_INVALID_XENHEAP_GFN _gfn(PGT_gfn_mask) =20 /* * Page needs to be scrubbed. Since this bit can only be set on a page tha= t is @@ -301,6 +310,34 @@ static inline bool arch_mfns_in_directmap(unsigned lon= g mfn, unsigned long nr) =20 #define PFN_ORDER(pg) ((pg)->v.free.order) =20 +/* + * All accesses to the GFN portion of type_info field should always be + * protected by the P2M lock. In case when it is not feasible to satisfy + * that requirement (risk of deadlock, lock inversion, etc) it is important + * to make sure that all non-protected updates to this field are atomic. + */ +static inline gfn_t page_get_xenheap_gfn(const struct page_info *p) +{ + gfn_t gfn =3D _gfn(ACCESS_ONCE(p->u.inuse.type_info) & PGT_gfn_mask); + + ASSERT(is_xen_heap_page(p)); + + return gfn_eq(gfn, PGT_INVALID_XENHEAP_GFN) ? INVALID_GFN : gfn; +} + +static inline void page_set_xenheap_gfn(struct page_info *p, gfn_t gfn) +{ + gfn_t gfn_ =3D gfn_eq(gfn, INVALID_GFN) ? PGT_INVALID_XENHEAP_GFN : gf= n; + unsigned long x, nx, y =3D p->u.inuse.type_info; + + ASSERT(is_xen_heap_page(p)); + + do { + x =3D y; + nx =3D (x & ~PGT_gfn_mask) | gfn_x(gfn_); + } while ( (y =3D cmpxchg(&p->u.inuse.type_info, x, nx)) !=3D x ); +} + extern unsigned char cpu0_boot_stack[]; =20 void setup_initial_pagetables(void); --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977788; cv=none; d=zohomail.com; s=zohoarc; b=K/Pn3jXL/f3Ts1Vz0boVcR6iFz+YtDMZOyzGmsoo90kPybKn+sCfUFqC+I2oiyOJGy34M/RZWNlIV1iIM39nITxhlFsdtj910pako0cNzQRA/JP6AdUWDPLhoeYP66bvOZ+7BjTtzk/It76vJAUvoTK/tUQtMFlPqlWkf6oYy0Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977788; 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=H9A8ULyozJc/nKuEPNSmZGMU66I2uIkMKGf8g4aTjZc=; b=hPD14BiFrXAOTRTDQHdsFV/gO78S30eehOUQ2i8x05Xu3migA6kwSE6ycrXuwoSxiDvK5NSBWMvz9WQjPrMG0UYB+X+A85Mpa3bdbEz/Y8zyNLsp0fPLronL7BMVZFqY3AGLvyVcv5ZZUFnJZ8IdfHAaHNPnhzlQdHbIS9dBt5w= 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 1753977788216313.10178529853715; Thu, 31 Jul 2025 09:03:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065995.1431397 (Exim 4.92) (envelope-from ) id 1uhVk6-0005GG-UH; Thu, 31 Jul 2025 16:02:54 +0000 Received: by outflank-mailman (output) from mailman id 1065995.1431397; Thu, 31 Jul 2025 16:02:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVk6-0005G5-PM; Thu, 31 Jul 2025 16:02:54 +0000 Received: by outflank-mailman (input) for mailman id 1065995; Thu, 31 Jul 2025 16:02:53 +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 1uhVg8-0001FB-MK for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:48 +0000 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [2a00:1450:4864:20::133]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3df0b852-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:47 +0200 (CEST) Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-55b8b8e00caso569926e87.3 for ; Thu, 31 Jul 2025 08:58:47 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:45 -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: 3df0b852-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977526; x=1754582326; 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=H9A8ULyozJc/nKuEPNSmZGMU66I2uIkMKGf8g4aTjZc=; b=niSnpS1EAKNw49D51QjWah0OHUjGKtctuZ56p2P6RP2lYOGO05muqvQl/yPwasd0Ox CiHcZvbvQE3phmnBK0XykmIkyXBNixPYC91OpTL+47AxTDl6LNGXd3rQ9k7ym1DlvoJt JX8/LQARLv3orAdL+O0PEaYBIKpRJ6JhsTByggUvk67jhwpkJp3jxy24KSzM3GTG3ikP CSwXUpMODf6W2oivMpuaig5JdKUKU5qgudEferfInH5+sdyz852duCGbwdXalqQf2v7R AQuE2P5IUj//70qvxqonWZsiU3xLYP3Ac/GmA5vWHIQ4FxvGIr6mT87cznZZBt1oH4F/ S4fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977526; x=1754582326; 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=H9A8ULyozJc/nKuEPNSmZGMU66I2uIkMKGf8g4aTjZc=; b=taWu8yoTER+rjRrvPyZT+TxQR+nSVgMeh8BdPSs1/rkbxUa1k0j8ZAF8p7banHE8a6 XbQFAdIFMyRoPfHongybugKQhATk2b0Av8/gPNK7AsDqKsYCeZB08TQqT8cdGFDTJdrC ymF54hkmnbS1YaLgjUx3eyuA442ZNur39Ily3emh+Ubn1q2CmJZeosQi/mr5mwphKR8a EnngOv80NxnC/kDXUnHaPnapSfnyeAaeQzylm2ELWD36khm/F+lQpU/CJ2HJZZVEC/h6 5XP7sE3JFQAt7WzF+LHQrSWsI3EzVvYlCaBiEhYJbs9b+4vsidLGJEdqLbqHf+egabbZ CQfQ== X-Gm-Message-State: AOJu0Yzbt5wo8OO0q3j0E2J7FZaNQfh2PgDpWQZTKkLB/qwJq6uBAofj gUPbXLhj1aXF3VeXNtJ3P+i6r/S2+JoKITRwRRNjFVbmf8QKD4W/5h2vs2XohA== X-Gm-Gg: ASbGnctNYMyABM2K/b0qGK+yIfV4jEhdWxzVLlfOBV7RK5hTA06xbr/zuOLL+re42fv xbDbqJ6X1itNyvSH0eEa2l2LSzR+9hVeet1LuSCHkb88Aaotgu/ocHJV0/SN9Mj43ncenN9Q8dC i5xi3cCTtGOLa8smiDcghTGoQjTwjlX8X+C8XR0LR5oR3BLIP2f4GtaD7MHnGdk2QaIhaT+2eN0 EZwMCBMY3yWgOPSgOl2K3mbj//Hnn0D4w9ic/5lCNb3ASUw0VKsqbpSJWftg0nS9D3P8zCz5arh QuejByimFaw6LN9ueKj2IkLzj4ObIPAgjpQWzZfzs8/24qgn6pVsuoMsNl1zSH2jKl8ivELnpCZ vQ5W4bbQm6bAexi/y138sB1HJ7PjMOlewv3XCo1yaWJa+V/3M3kl5NKnElOo5dQ== X-Google-Smtp-Source: AGHT+IG3rQ2MDLV+jzyMChDNS/ZKIl//GIGUS3fiRb4942UTVk8U82i3aEykhbwBVDrOU94hPB+pGA== X-Received: by 2002:a05:6512:108b:b0:55a:4bfe:bf78 with SMTP id 2adb3069b0e04-55b7c00d417mr2483333e87.2.1753977526148; Thu, 31 Jul 2025 08:58:46 -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 v3 11/20] xen/riscv: implement function to map memory in guest p2m Date: Thu, 31 Jul 2025 17:58:10 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977790007116600 Content-Type: text/plain; charset="utf-8" Implement map_regions_p2mt() to map a region in the guest p2m with a specific p2m type. The memory attributes will be derived from the p2m type. This function is going to be called from dom0less common code. To implement it, introduce: - p2m_write_(un)lock() to ensure safe concurrent updates to the P2M. As part of this change, introduce p2m_tlb_flush_sync() and p2m_force_tlb_flush_sync(). - A stub for p2m_set_range() to map a range of GFNs to MFNs. - p2m_insert_mapping(). - p2m_is_write_locked(). Drop guest_physmap_add_entry() and call map_regions_p2mt() directly from guest_physmap_add_page(), making guest_physmap_add_entry() unnecessary. Signed-off-by: Oleksii Kurochko --- Changes in v3: - Introudce p2m_write_lock() and p2m_is_write_locked(). - Introduce p2m_force_tlb_flush_sync() and p2m_flush_tlb() to flush TLBs after p2m table update. - Change an argument of p2m_insert_mapping() from struct domain *d to p2m_domain *p2m. - Drop guest_physmap_add_entry() and use map_regions_p2mt() to define guest_physmap_add_page(). - Add declaration of map_regions_p2mt() to asm/p2m.h. - Rewrite commit message and subject. - Drop p2m_access_t related stuff. - Add defintion of p2m_is_write_locked(). --- Changes in v2: - This changes were part of "xen/riscv: implement p2m mapping functionalit= y". No additional signigicant changes were done. --- xen/arch/riscv/include/asm/p2m.h | 31 ++++++++++----- xen/arch/riscv/p2m.c | 65 ++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index 5f253da1dd..ada3c398b4 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -121,21 +121,22 @@ static inline int guest_physmap_mark_populate_on_dema= nd(struct domain *d, return -EOPNOTSUPP; } =20 -static inline int guest_physmap_add_entry(struct domain *d, - gfn_t gfn, mfn_t mfn, - unsigned long page_order, - p2m_type_t t) -{ - BUG_ON("unimplemented"); - return -EINVAL; -} +/* + * Map a region in the guest p2m with a specific p2m type. + * The memory attributes will be derived from the p2m type. + */ +int map_regions_p2mt(struct domain *d, + gfn_t gfn, + unsigned long nr, + mfn_t mfn, + p2m_type_t p2mt); =20 /* Untyped version for RAM only, for compatibility */ static inline int __must_check guest_physmap_add_page(struct domain *d, gfn_t gfn, mfn_t mfn, unsigned int page_order) { - return guest_physmap_add_entry(d, gfn, mfn, page_order, p2m_ram_rw); + return map_regions_p2mt(d, gfn, BIT(page_order, UL), mfn, p2m_ram_rw); } =20 static inline mfn_t gfn_to_mfn(struct domain *d, gfn_t gfn) @@ -159,6 +160,18 @@ static inline void p2m_altp2m_check(struct vcpu *v, ui= nt16_t idx) /* Not supported on RISCV. */ } =20 +static inline void p2m_write_lock(struct p2m_domain *p2m) +{ + write_lock(&p2m->lock); +} + +void p2m_write_unlock(struct p2m_domain *p2m); + +static inline int p2m_is_write_locked(struct p2m_domain *p2m) +{ + return rw_is_write_locked(&p2m->lock); +} + unsigned long construct_hgatp(struct p2m_domain *p2m, uint16_t vmid); =20 #endif /* ASM__RISCV__P2M_H */ diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index cac07c51c9..7cfcf76f24 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -9,6 +9,41 @@ =20 unsigned int __read_mostly p2m_root_order; =20 +/* + * Force a synchronous P2M TLB flush. + * + * Must be called with the p2m lock held. + */ +static void p2m_force_tlb_flush_sync(struct p2m_domain *p2m) +{ + struct domain *d =3D p2m->domain; + + ASSERT(p2m_is_write_locked(p2m)); + + sbi_remote_hfence_gvma(d->dirty_cpumask, 0, 0); + + p2m->need_flush =3D false; +} + +void p2m_tlb_flush_sync(struct p2m_domain *p2m) +{ + if ( p2m->need_flush ) + p2m_force_tlb_flush_sync(p2m); +} + +/* Unlock the flush and do a P2M TLB flush if necessary */ +void p2m_write_unlock(struct p2m_domain *p2m) +{ + /* + * The final flush is done with the P2M write lock taken to avoid + * someone else modifying the P2M wbefore the TLB invalidation has + * completed. + */ + p2m_tlb_flush_sync(p2m); + + write_unlock(&p2m->lock); +} + static void clear_and_clean_page(struct page_info *page) { clear_domain_page(page_to_mfn(page)); @@ -139,3 +174,33 @@ int p2m_set_allocation(struct domain *d, unsigned long= pages, bool *preempted) =20 return 0; } + +static int p2m_set_range(struct p2m_domain *p2m, + gfn_t sgfn, + unsigned long nr, + mfn_t smfn, + p2m_type_t t) +{ + return -EOPNOTSUPP; +} + +static int p2m_insert_mapping(struct p2m_domain *p2m, gfn_t start_gfn, + unsigned long nr, mfn_t mfn, p2m_type_t t) +{ + int rc; + + p2m_write_lock(p2m); + rc =3D p2m_set_range(p2m, start_gfn, nr, mfn, t); + p2m_write_unlock(p2m); + + return rc; +} + +int map_regions_p2mt(struct domain *d, + gfn_t gfn, + unsigned long nr, + mfn_t mfn, + p2m_type_t p2mt) +{ + return p2m_insert_mapping(p2m_get_hostp2m(d), gfn, nr, mfn, p2mt); +} --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977668; cv=none; d=zohomail.com; s=zohoarc; b=ZPKgOcAHlIxgOG8FB6awecQLNpjQspzZ0J+PZw8sFf4XVgumv3XNyA+UNpxUNdDkov+t83ibrLUvLKRlh5wtKfigznBpErIZ6JybrEQTHMDmBzM/gi8UIRMS+gXblPLpOwFQnUEar2TxEIJKni00OhODNvoV745a27IkKQotOxs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977668; 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=nDoQIzI2S7GR+bSHxAC8/G+9LMVK6bHSqsbe1u3enIo=; b=RTRlt59nUzLVPifqvGg0QWWK1kIQdGZg2OIV0hk7GCx2Jm4+Yf76hmg5sY1DCnujEHGOUCCEA7J0hdVa8dAvvJqck5H3O57yGGrBj7cnnLsgaSr1x0irt0j5MswFerswnbFf7inbB1zX21r+DwY7evoKiS/0qj56794oZ4A2A5A= 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 175397766874481.38784721233765; Thu, 31 Jul 2025 09:01:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065919.1431292 (Exim 4.92) (envelope-from ) id 1uhVi6-0008LP-Rf; Thu, 31 Jul 2025 16:00:50 +0000 Received: by outflank-mailman (output) from mailman id 1065919.1431292; Thu, 31 Jul 2025 16:00:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVi6-0008LI-Oj; Thu, 31 Jul 2025 16:00:50 +0000 Received: by outflank-mailman (input) for mailman id 1065919; Thu, 31 Jul 2025 16:00:49 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVgA-0000zK-Oz for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:50 +0000 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [2a00:1450:4864:20::12b]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3ef59a14-6e27-11f0-a320-13f23c93f187; Thu, 31 Jul 2025 17:58:48 +0200 (CEST) Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-55b82db8fd4so1220142e87.2 for ; Thu, 31 Jul 2025 08:58:48 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58: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: 3ef59a14-6e27-11f0-a320-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977528; x=1754582328; 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=nDoQIzI2S7GR+bSHxAC8/G+9LMVK6bHSqsbe1u3enIo=; b=A9nX/T2LzyYhvjhgjHzpQOFqfjdp1YpTtRK2PPOVmdQHJh27HLmrSLxQsXvIdWWfL5 DCEghB7qmezJyhPUGNzOOxeApgLKQCF2EXrd3QGVWhf6uZDs9wQGTiiXO41Sp7JcK4Ur 0nv3IHvQNOc55DEhx6NEHdPq+ylkf1yEUjIu386Geag8ppQGHclQzfKNsij96AunxbnL QOEbvvrssOOIHgkHACCd34ateTjDNaPXvMR7SoL85e92wMUlBgSIRhP08ZKSt6pa/Cyo gD/BtxCUCA1zgXH1eH6rTT2w9tmtg330zZ52/nyfIBNmA9T0el6Zw/pA1gkE/IaHsiVb 2Wbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977528; x=1754582328; 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=nDoQIzI2S7GR+bSHxAC8/G+9LMVK6bHSqsbe1u3enIo=; b=EwetYyOo5G30xh05/gfODqHNCcobHLrMr8COwE58uadk5wCj+VqxgL46OtXH+1go/g ruvdys8Wzyl5OHNA+FZSxkQ3o0eqvuiQj9G1LJfpN3c1IbcEKCwOCMPC8Gcqkd4krg8X p9JVm/I8OgWzwdW9d+gUwAeyVR2zQ2QTygbOZoeZxvYFNsRHHnRS7/i7UI7usW29x8n8 vp2PQccjYLVzpRKSqcmtsJvdoSK+mpGdW8kwCuShJHfvrDJs37jx7mJc21r08kcU/WEl n1pXNjIUTlTkZjcAUG0m7A3ZEQD/f5x9EUzMn43FHtIwk3BYdOkSGpfsgXotCBTHXVL/ ecYA== X-Gm-Message-State: AOJu0YzmvdqpkW+d5wFK3p5ACgKzG5ELUg0CYJCYQfYlYkY4ZcLf2HMX GIbxGNyNtHNSRRRd9x3bp/RWGTnhALWDTjciMuT6E9/hPlO+8Or9J+9HoSGViw== X-Gm-Gg: ASbGncsFMkJKlgTuVb2CYsGtcDVv1+7smfSU4AgUYZ0N6Cgb6Xvz4+jdDGrRyi8Kr9P s+Hx3zSoR/AI5WOsSE8zKxetvHIGbKXge667/S1ltiz8RLopvChqB2QqJ939fTWTkYwvRcWejss uzyj97gASzVsu6ek8r70omvg3o8GTtQSFkEEhzjXgTnj8rzUkdzRVDbmFlz4HsusiHM/3LgWyg2 ppAjGSf4CxCP5A0CX9SOKl1vdwrIwHMK5q5sE8F9WOe54iyPLP4aSowxAZxW5WqA4b9Fwv50OAA WpijFOnC8KjuThBzApUFTKwtSEHpa+lR4uNGTPxtaAmDMpEPyEvl/cnkH0bzVd7WSkHTJuyzlX0 yi+Xef5jW1lJVs1QWcVKIPiH1kRZu0t3aVRSEw+6SYycBNx71utCopdeGAKWNoA== X-Google-Smtp-Source: AGHT+IEsoRjnCH5J/XCiLOdthErnQOm9hvMya+nx5aWP4rvVgFpdGsFIjLh0CQm+q4NtXYtRX2yDpA== X-Received: by 2002:a05:6512:3190:b0:55b:840c:5636 with SMTP id 2adb3069b0e04-55b840c58b9mr1996653e87.50.1753977527760; Thu, 31 Jul 2025 08:58:47 -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 v3 12/20] xen/riscv: implement p2m_set_range() Date: Thu, 31 Jul 2025 17:58:11 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 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: 1753977670484116600 This patch introduces p2m_set_range() and its core helper p2m_set_entry() f= or RISC-V, based loosely on the Arm implementation, with several RISC-V-specif= ic modifications. The main changes are: - Simplification of Break-Before-Make (BBM) approach as according to RISC-V spec: It is permitted for multiple address-translation cache entries to co-ex= ist for the same address. This represents the fact that in a conventional TLB hierarchy, it is possible for multiple entries to match a single address if, for example, a page is upgraded to a superpage without first clearing the original non-leaf PTE=E2=80=99s valid bit and executing an= SFENCE.VMA with rs1=3Dx0, or if multiple TLBs exist in parallel at a given level o= f the hierarchy. In this case, just as if an SFENCE.VMA is not executed betwe= en a write to the memory-management tables and subsequent implicit read of= the same address: it is unpredictable whether the old non-leaf PTE or the n= ew leaf PTE is used, but the behavior is otherwise well defined. In contrast to the Arm architecture, where BBM is mandatory and failing to use it in some cases can lead to CPU instability, RISC-V guarantees stability, and the behavior remains safe =E2=80=94 though unpredictable i= n terms of which translation will be used. - Unlike Arm, the valid bit is not repurposed for other uses in this implementation. Instead, entry validity is determined based solely on P2M PTE's valid bit. The main functionality is in p2m_set_entry(), which handles mappings aligned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity). p2m_set_range() breaks a region down into block-aligned mappings and calls p2m_set_entry() accordingly. Stub implementations (to be completed later) include: - p2m_free_subtree() - p2m_next_level() - p2m_pte_from_mfn() Note: Support for shattering block entries is not implemented in this patch and will be added separately. Additionally, some straightforward helper functions are now implemented: - p2m_write_pte() - p2m_remove_pte() - p2m_get_root_pointer() Signed-off-by: Oleksii Kurochko --- Changes in V3: - Drop p2m_access_t connected stuff as it isn't going to be used, at least now. - Move defintion of P2M_ROOT_ORDER and P2M_ROOT_PAGES to earlier patches. - Update the comment above lowest_mapped_gfn declaration. - Update the comment above p2m_get_root_pointer(): s/"...ofset of the root table"/"...ofset into root table". - s/p2m_remove_pte/p2m_clean_pte. - Use plain 0 instead of 0x00 in p2m_clean_pte(). - s/p2m_entry_from_mfn/p2m_pte_from_mfn. - s/GUEST_TABLE_*/P2M_TABLE_*. - Update the comment above p2m_next_level(): "GFN entry" -> "corresponding the entry corresponding to the GFN". - s/__p2m_set_entry/_p2m_set_entry. - drop "s" for sgfn and smfn prefixes of _p2m_set_entry()'s arguments as this function work only with one GFN and one MFN. - Return correct return code when p2m_next_level() faild in _p2m_set_entry= (), also drop "else" and just handle case (rc !=3D P2M_TABLE_NORMAL) separat= ely. - Code style fixes. - Use unsigned int for "order" in p2m_set_entry(). - s/p2m_set_entry/p2m_free_subtree. - Update ASSERT() in __p2m_set_enty() to check that page_order is propertly aligned. - Return -EACCES instead of -ENOMEM in the chase when domain is dying and someone called p2m_set_entry. - s/p2m_set_entry/p2m_set_range. - s/__p2m_set_entry/p2m_set_entry - s/p2me_is_valid/p2m_is_valid() - Return a number of successfully mapped GFNs in case if not all were mapp= ed in p2m_set_range(). - Use BIT(order, UL) instead of 1 << order. - Drop IOMMU flushing code from p2m_set_entry(). - set p2m->need_flush=3Dtrue when entry in p2m_set_entry() is changed. - Introduce p2m_mapping_order() to support superpages. - Drop p2m_is_valid() and use pte_is_valid() instead as there is no tricks with copying of valid bit anymore. - Update p2m_pte_from_mfn() prototype: drop p2m argument. --- Changes in V2: - New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller. - Update the way when p2m TLB is flushed: - RISC-V does't require BBM so there is no need to remove PTE before making new so drop 'if /*pte_is_valid(orig_pte) */' and remove PTE only removing has been requested. - Drop p2m->need_flush |=3D !!pte_is_valid(orig_pte); for the case when PTE's removing is happening as RISC-V could cache invalid PTE and thereby it requires to do a flush each time and it doesn't matter if PTE is valid or not at the moment when PTE removing is happening. - Drop a check if PTE is valid in case of PTE is modified as it was mentio= ned above as BBM isn't required so TLB flushing could be defered and there is no need to do it before modifying of PTE. - Drop p2m->need_flush as it seems like it will be always true. - Drop foreign mapping things as it isn't necessary for RISC-V right now. - s/p2m_is_valid/p2me_is_valid. - Move definition and initalization of p2m->{max_mapped_gfn,lowest_mapped_= gfn} to this patch. --- xen/arch/riscv/include/asm/p2m.h | 12 ++ xen/arch/riscv/p2m.c | 250 ++++++++++++++++++++++++++++++- 2 files changed, 261 insertions(+), 1 deletion(-) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index ada3c398b4..26ad87b8df 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -7,11 +7,13 @@ #include #include =20 +#include #include =20 extern unsigned int p2m_root_order; #define P2M_ROOT_ORDER p2m_root_order #define P2M_ROOT_PAGES BIT(P2M_ROOT_ORDER, U) +#define P2M_ROOT_LEVEL HYP_PT_ROOT_LEVEL =20 #define paddr_bits PADDR_BITS =20 @@ -50,6 +52,16 @@ struct p2m_domain { * shattered), call p2m_tlb_flush_sync(). */ bool need_flush; + + /* Highest guest frame that's ever been mapped in the p2m */ + gfn_t max_mapped_gfn; + + /* + * Lowest mapped gfn in the p2m. When releasing mapped gfn's in a + * preemptible manner this is updated to track where to resume + * the search. Apart from during teardown this can only decrease. + */ + gfn_t lowest_mapped_gfn; }; =20 /* diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 7cfcf76f24..6c99719c66 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -3,6 +3,7 @@ #include #include =20 +#include #include #include #include @@ -132,6 +133,9 @@ int p2m_init(struct domain *d) rwlock_init(&p2m->lock); INIT_PAGE_LIST_HEAD(&p2m->pages); =20 + p2m->max_mapped_gfn =3D _gfn(0); + p2m->lowest_mapped_gfn =3D _gfn(ULONG_MAX); + /* * Currently, the infrastructure required to enable CONFIG_HAS_PASSTHR= OUGH * is not ready for RISC-V support. @@ -175,13 +179,257 @@ int p2m_set_allocation(struct domain *d, unsigned lo= ng pages, bool *preempted) return 0; } =20 +/* + * Find and map the root page table. The caller is responsible for + * unmapping the table. + * + * The function will return NULL if the offset into the root table is + * invalid. + */ +static pte_t *p2m_get_root_pointer(struct p2m_domain *p2m, gfn_t gfn) +{ + unsigned long root_table_indx; + + root_table_indx =3D gfn_x(gfn) >> XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL); + if ( root_table_indx >=3D P2M_ROOT_PAGES ) + return NULL; + + return __map_domain_page(p2m->root + root_table_indx); +} + +static inline void p2m_write_pte(pte_t *p, pte_t pte, bool clean_pte) +{ + write_pte(p, pte); + if ( clean_pte ) + clean_dcache_va_range(p, sizeof(*p)); +} + +static inline void p2m_clean_pte(pte_t *p, bool clean_pte) +{ + pte_t pte; + + memset(&pte, 0, sizeof(pte)); + p2m_write_pte(p, pte, clean_pte); +} + +static pte_t p2m_pte_from_mfn(mfn_t mfn, p2m_type_t t) +{ + panic("%s: hasn't been implemented yet\n", __func__); + + return (pte_t) { .pte =3D 0 }; +} + +#define P2M_TABLE_MAP_NONE 0 +#define P2M_TABLE_MAP_NOMEM 1 +#define P2M_TABLE_SUPER_PAGE 2 +#define P2M_TABLE_NORMAL 3 + +/* + * Take the currently mapped table, find the corresponding the entry + * corresponding to the GFN, and map the next table, if available. + * The previous table will be unmapped if the next level was mapped + * (e.g P2M_TABLE_NORMAL returned). + * + * `alloc_tbl` parameter indicates whether intermediate tables should + * be allocated when not present. + * + * Return values: + * P2M_TABLE_MAP_NONE: a table allocation isn't permitted. + * P2M_TABLE_MAP_NOMEM: allocating a new page failed. + * P2M_TABLE_SUPER_PAGE: next level or leaf mapped normally. + * P2M_TABLE_NORMAL: The next entry points to a superpage. + */ +static int p2m_next_level(struct p2m_domain *p2m, bool alloc_tbl, + unsigned int level, pte_t **table, + unsigned int offset) +{ + panic("%s: hasn't been implemented yet\n", __func__); + + return P2M_TABLE_MAP_NONE; +} + +/* Free pte sub-tree behind an entry */ +static void p2m_free_subtree(struct p2m_domain *p2m, + pte_t entry, unsigned int level) +{ + panic("%s: hasn't been implemented yet\n", __func__); +} + +/* + * Insert an entry in the p2m. This should be called with a mapping + * equal to a page/superpage. + */ +static int p2m_set_entry(struct p2m_domain *p2m, + gfn_t gfn, + unsigned long page_order, + mfn_t mfn, + p2m_type_t t) +{ + unsigned int level; + unsigned int target =3D page_order / PAGETABLE_ORDER; + pte_t *entry, *table, orig_pte; + int rc; + /* A mapping is removed if the MFN is invalid. */ + bool removing_mapping =3D mfn_eq(mfn, INVALID_MFN); + DECLARE_OFFSETS(offsets, gfn_to_gaddr(gfn)); + + ASSERT(p2m_is_write_locked(p2m)); + + /* + * Check if the level target is valid: we only support + * 4K - 2M - 1G mapping. + */ + ASSERT((target <=3D 2) && !(page_order % PAGETABLE_ORDER)); + + table =3D p2m_get_root_pointer(p2m, gfn); + if ( !table ) + return -EINVAL; + + for ( level =3D P2M_ROOT_LEVEL; level > target; level-- ) + { + /* + * Don't try to allocate intermediate page table if the mapping + * is about to be removed. + */ + rc =3D p2m_next_level(p2m, !removing_mapping, + level, &table, offsets[level]); + if ( (rc =3D=3D P2M_TABLE_MAP_NONE) || (rc =3D=3D P2M_TABLE_MAP_NO= MEM) ) + { + rc =3D (rc =3D=3D P2M_TABLE_MAP_NONE) ? -ENOENT : -ENOMEM; + /* + * We are here because p2m_next_level has failed to map + * the intermediate page table (e.g the table does not exist + * and they p2m tree is read-only). It is a valid case + * when removing a mapping as it may not exist in the + * page table. In this case, just ignore it. + */ + rc =3D removing_mapping ? 0 : rc; + goto out; + } + + if ( rc !=3D P2M_TABLE_NORMAL ) + break; + } + + entry =3D table + offsets[level]; + + /* + * If we are here with level > target, we must be at a leaf node, + * and we need to break up the superpage. + */ + if ( level > target ) + { + panic("Shattering isn't implemented\n"); + } + + /* + * We should always be there with the correct level because all the + * intermediate tables have been installed if necessary. + */ + ASSERT(level =3D=3D target); + + orig_pte =3D *entry; + + if ( removing_mapping ) + p2m_clean_pte(entry, p2m->clean_pte); + else + { + pte_t pte =3D p2m_pte_from_mfn(mfn, t); + + p2m_write_pte(entry, pte, p2m->clean_pte); + + p2m->max_mapped_gfn =3D gfn_max(p2m->max_mapped_gfn, + gfn_add(gfn, BIT(page_order, UL) - 1= )); + p2m->lowest_mapped_gfn =3D gfn_min(p2m->lowest_mapped_gfn, gfn); + } + + p2m->need_flush =3D true; + + /* + * Currently, the infrastructure required to enable CONFIG_HAS_PASSTHR= OUGH + * is not ready for RISC-V support. + * + * When CONFIG_HAS_PASSTHROUGH=3Dy, iommu_iotlb_flush() should be done + * here. + */ +#ifdef CONFIG_HAS_PASSTHROUGH +# error "add code to flush IOMMU TLB" +#endif + + rc =3D 0; + + /* + * Free the entry only if the original pte was valid and the base + * is different (to avoid freeing when permission is changed). + */ + if ( pte_is_valid(orig_pte) && + !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) ) + p2m_free_subtree(p2m, orig_pte, level); + + out: + unmap_domain_page(table); + + return rc; +} + +/* Return mapping order for given gfn, mfn and nr */ +static unsigned long p2m_mapping_order(gfn_t gfn, mfn_t mfn, unsigned long= nr) +{ + unsigned long mask; + /* 1gb, 2mb, 4k mappings are supported */ + unsigned int level =3D min(P2M_ROOT_LEVEL, 2); + unsigned long order =3D 0; + + mask =3D !mfn_eq(mfn, INVALID_MFN) ? mfn_x(mfn) : 0; + mask |=3D gfn_x(gfn); + + for ( ; level !=3D 0; level-- ) + { + if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(level), UL) - 1)) && + (nr >=3D BIT(XEN_PT_LEVEL_ORDER(level), UL)) ) + { + order =3D XEN_PT_LEVEL_ORDER(level); + break; + } + } + + return order; +} + static int p2m_set_range(struct p2m_domain *p2m, gfn_t sgfn, unsigned long nr, mfn_t smfn, p2m_type_t t) { - return -EOPNOTSUPP; + int rc =3D 0; + unsigned long left =3D nr; + + /* + * Any reference taken by the P2M mappings (e.g. foreign mapping) will + * be dropped in relinquish_p2m_mapping(). As the P2M will still + * be accessible after, we need to prevent mapping to be added when the + * domain is dying. + */ + if ( unlikely(p2m->domain->is_dying) ) + return -EACCES; + + while ( left ) + { + unsigned long order =3D p2m_mapping_order(sgfn, smfn, left); + + rc =3D p2m_set_entry(p2m, sgfn, order, smfn, t); + if ( rc ) + break; + + sgfn =3D gfn_add(sgfn, BIT(order, UL)); + if ( !mfn_eq(smfn, INVALID_MFN) ) + smfn =3D mfn_add(smfn, BIT(order, UL)); + + left -=3D BIT(order, UL); + } + + return !left ? 0 : left =3D=3D nr ? rc : (nr - left); } =20 static int p2m_insert_mapping(struct p2m_domain *p2m, gfn_t start_gfn, --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977667; cv=none; d=zohomail.com; s=zohoarc; b=EHU8iskupBl86qdGRLM8VyIqaLL7UFuhTwcqOF6+j3W04qB03o5sQG6tlY+ZYG0bAXKG3JH8V7YeiaRUpw9kwO1/e/mgptgQXLCAiwD2rz+/fX2/QVrBMUCONxW0i9wCXicbqB3PxB6gW0JVPMj9PDQc5k56TG4Vhw+G9T+v2Ew= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977667; 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=StTBO6QPLNBjKleSGjDNqc33RY5wJ7PjLuBf0sj/FWM=; b=dAXodPptYtdjMY+G9GWbBdw2+mjFQ6W/NKVGtb20fcTbHzAhxXCFJDCoWQUCh+ucBQzQlLYgDyP0RNlWAnjTtyJ6hNhXYuBdkhRnZYlcw14eBfoDxWrT0u07+GQa4PJCAG/DtjebY7Q+m3YVoDmq+AMYA6HKyhJmV2JNnuMGx4k= 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 1753977667494936.1757655889473; Thu, 31 Jul 2025 09:01:07 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065922.1431302 (Exim 4.92) (envelope-from ) id 1uhViA-0000K9-5A; Thu, 31 Jul 2025 16:00:54 +0000 Received: by outflank-mailman (output) from mailman id 1065922.1431302; Thu, 31 Jul 2025 16:00:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhViA-0000K2-27; Thu, 31 Jul 2025 16:00:54 +0000 Received: by outflank-mailman (input) for mailman id 1065922; Thu, 31 Jul 2025 16:00:52 +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 1uhVgB-0001FB-Mi for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:51 +0000 Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [2a00:1450:4864:20::132]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3fbad327-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:50 +0200 (CEST) Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-555024588b1so1294000e87.1 for ; Thu, 31 Jul 2025 08:58:50 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:48 -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: 3fbad327-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977529; x=1754582329; 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=StTBO6QPLNBjKleSGjDNqc33RY5wJ7PjLuBf0sj/FWM=; b=LVR7NiMbmoAwkTBfz6lKXP2OfexGvgAvxRIIqe2c9s8eyTX9OvN9ts/bqBHd4IVX0m +Hfv4SCY8fecb4QDEc2Hc6Ig0GxVI0e63TkijLVC2vP7FvDcIwQfd5T1wkpL6g67dqyA edYVVYxmzKk2oqEtAuZrvJAi828DnIfnkRT812hoGHPxka5F2Wu+LXoLc3gTl3KJvl3s BckPl7acusUI6eOkPMUNUc+aCAait+CJXhACidBWHTqM/lcWl9KOx8XSD4diSQq4JFxH 3DUx1UV3K5BQ8fjSKtZNT8UVKm/VszVqTzSXutd7ETTM54IMhSBM8u4tLRKaC7kUj2/4 2QWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977529; x=1754582329; 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=StTBO6QPLNBjKleSGjDNqc33RY5wJ7PjLuBf0sj/FWM=; b=ELXB35eY8Mnz8xdXtPXVoFSEcek5Cmuh2cYk/cx+odsxxCwf73z8UYdgnL8Ir5lPXs sz1bDfv4OsII06OMNGt2wnfbBUACI7pt1Yeyrmn5cFjQ+VJTeAEBJu6AqxqoOaj6JWbH 1WusH61OtTVu6kcZo1b3P+BvIOHRvy8RaR83ZwJnDb63J3El08IjEXd4vEvFC0CGiJxy EeXK+PtT630+FUsq2Vfdx7N1V2U9OG8/06h3mL5BQLx7w3Q15nr/e8iUtFj8ryh7T39D gCajqC0Kz/gXQaaWxHlUiC9irHVwWwLF6yn+EWJDzq4HN5WNOda1oOU7r4/XuKgab887 5sWg== X-Gm-Message-State: AOJu0YxtM+j6YyleHUDhQHonXlKycY8566He834XovsN20BGM4/1jn0J hyWigRXMevkMkwmxl5/nL0PnS1D2KLiEGW1PQqihiWWpcRpSaohisrNbn5XCCg== X-Gm-Gg: ASbGncvRVmcRXpnUhw1Xwy/shUyhjGuSt3g8BbYEMM27873sKxWTfJBlsVp2nf5lToe pSF/WtKIf0tFmCqSRx6UIogqkXuu2GNj/lRNMTEP6pgHXqJysBbjn97+mEkF+VLoJDuyA5iR4Vw jo6a3TK0Ek9QMnNX9U7w6qd1PITlVKYL5q4rL+FuG7CYQT+pVdKX55/SG0Gd9L0uP5jopc3QeQv sg0lc8cmYNjusMnPv+XoMf7heRmp2K2ROJthkcLz3PvCy3lg31VufNVBs2Lr/P/bipT/hUvBPBp cwVEe7R0gNgydUtSv9gjMvbsENMEA8hHd/D7rhEVRwIJZIn7/T8ORwjdI9C7MDVnRnUd9gvNRIX f40yaWs1qiGYXuYT0L+NdlHEcxL0GV1+XoeK2s/5+fj/gUfZdRLDjcZpAz89h9eBQ4mDa/oX7 X-Google-Smtp-Source: AGHT+IFPZNBVWzDAkCO16wEd9+4o4LZMpnG6Ew5Mfti5hJlpZGh0FHkTWJfx8oFmIsWfmGud0tXF9Q== X-Received: by 2002:a05:6512:a95:b0:553:510d:f46b with SMTP id 2adb3069b0e04-55b7c071824mr3451131e87.32.1753977529096; Thu, 31 Jul 2025 08:58:49 -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 v3 13/20] xen/riscv: Implement p2m_free_subtree() and related helpers Date: Thu, 31 Jul 2025 17:58:12 +0200 Message-ID: <41845723a7b0e3efd09095d13e57aace6f7747ef.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 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: 1753977668466116600 This patch introduces a working implementation of p2m_free_subtree() for RI= SC-V based on ARM's implementation of p2m_free_entry(), enabling proper cleanup of page table entries in the P2M (physical-to-machine) mapping. Only few things are changed: - Introduce and use p2m_get_type() to get a type of p2m entry as RISC-V's PTE doesn't have enough space to store all necessary types so a type is stored outside PTE. But, at the moment, handle only types which fit into PTE's bits. Key additions include: - p2m_free_subtree(): Recursively frees page table entries at all levels. It handles both regular and superpage mappings and ensures that TLB entries are flushed before freeing intermediate tables. - p2m_put_page() and helpers: - p2m_put_4k_page(): Clears GFN from xenheap pages if applicable. - p2m_put_2m_superpage(): Releases foreign page references in a 2MB superpage. - p2m_get_type(): Extracts the stored p2m_type from the PTE bits. - p2m_free_page(): Returns a page to a domain's freelist. - Introduce p2m_is_foreign() and connected to it things. Defines XEN_PT_ENTRIES in asm/page.h to simplify loops over page table entries. Signed-off-by: Oleksii Kurochko --- Changes in V3: - Use p2m_tlb_flush_sync(p2m) instead of p2m_force_tlb_flush_sync() in p2m_free_subtree(). - Drop p2m_is_valid() implementation as pte_is_valid() is going to be used instead. - Drop p2m_is_superpage() and introduce pte_is_superpage() instead. - s/p2m_free_entry/p2m_free_subtree. - s/p2m_type_radix_get/p2m_get_type. - Update implementation of p2m_get_type() to get type both from PTE bits, other cases will be covered in a separate patch. This requires an introduction of new P2M_TYPE_PTE_BITS_MASK macros. - Drop p2m argument of p2m_get_type() as it isn't needed anymore. - Put cheapest checks first in p2m_is_superpage(). - Use switch() in p2m_put_page(). - Update the comment in p2m_put_foreign_page(). - Code style fixes. - Move p2m_foreign stuff to this commit. - Drop p2m argument of p2m_put_page() as itsn't used anymore. --- Changes in V2: - New patch. It was a part of 2ma big patch "xen/riscv: implement p2m mapp= ing functionality" which was splitted to smaller. - s/p2m_is_superpage/p2me_is_superpage. --- xen/arch/riscv/include/asm/p2m.h | 18 +++- xen/arch/riscv/include/asm/page.h | 6 ++ xen/arch/riscv/include/asm/paging.h | 2 + xen/arch/riscv/p2m.c | 137 +++++++++++++++++++++++++++- xen/arch/riscv/paging.c | 7 ++ 5 files changed, 168 insertions(+), 2 deletions(-) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index 26ad87b8df..fbc73448a7 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -79,10 +79,20 @@ typedef enum { p2m_ext_storage, /* Following types'll be stored outsude PTE bits: = */ p2m_grant_map_rw, /* Read/write grant mapping */ p2m_grant_map_ro, /* Read-only grant mapping */ + p2m_map_foreign_rw, /* Read/write RAM pages from foreign domain */ + p2m_map_foreign_ro, /* Read-only RAM pages from foreign domain */ } p2m_type_t; =20 #define p2m_mmio_direct p2m_mmio_direct_io =20 +/* + * Bits 8 and 9 are reserved for use by supervisor software; + * the implementation shall ignore this field. + * We are going to use to save in these bits frequently used types to avoid + * get/set of a type from radix tree. + */ +#define P2M_TYPE_PTE_BITS_MASK 0x300 + /* We use bitmaps and mask to handle groups of types */ #define p2m_to_mask(t_) BIT(t_, UL) =20 @@ -93,10 +103,16 @@ typedef enum { #define P2M_GRANT_TYPES (p2m_to_mask(p2m_grant_map_rw) | \ p2m_to_mask(p2m_grant_map_ro)) =20 + /* Foreign mappings types */ +#define P2M_FOREIGN_TYPES (p2m_to_mask(p2m_map_foreign_rw) | \ + p2m_to_mask(p2m_map_foreign_ro)) + /* Useful predicates */ #define p2m_is_ram(t_) (p2m_to_mask(t_) & P2M_RAM_TYPES) #define p2m_is_any_ram(t_) (p2m_to_mask(t_) & \ - (P2M_RAM_TYPES | P2M_GRANT_TYPES)) + (P2M_RAM_TYPES | P2M_GRANT_TYPES | \ + P2M_FOREIGN_TYPES)) +#define p2m_is_foreign(t_) (p2m_to_mask(t_) & P2M_FOREIGN_TYPES) =20 #include =20 diff --git a/xen/arch/riscv/include/asm/page.h b/xen/arch/riscv/include/asm= /page.h index 66cb192316..cb303af0c0 100644 --- a/xen/arch/riscv/include/asm/page.h +++ b/xen/arch/riscv/include/asm/page.h @@ -20,6 +20,7 @@ #define XEN_PT_LEVEL_SIZE(lvl) (_AT(paddr_t, 1) << XEN_PT_LEVEL_SHIFT= (lvl)) #define XEN_PT_LEVEL_MAP_MASK(lvl) (~(XEN_PT_LEVEL_SIZE(lvl) - 1)) #define XEN_PT_LEVEL_MASK(lvl) (VPN_MASK << XEN_PT_LEVEL_SHIFT(lvl)) +#define XEN_PT_ENTRIES (_AT(unsigned int, 1) << PAGETABLE_ORD= ER) =20 /* * PTE format: @@ -182,6 +183,11 @@ static inline bool pte_is_mapping(pte_t p) return (p.pte & PTE_VALID) && (p.pte & PTE_ACCESS_MASK); } =20 +static inline bool pte_is_superpage(pte_t p, unsigned int level) +{ + return (level > 0) && pte_is_mapping(p); +} + static inline int clean_and_invalidate_dcache_va_range(const void *p, unsigned long size) { diff --git a/xen/arch/riscv/include/asm/paging.h b/xen/arch/riscv/include/a= sm/paging.h index 557fbd1abc..c9063b7f76 100644 --- a/xen/arch/riscv/include/asm/paging.h +++ b/xen/arch/riscv/include/asm/paging.h @@ -12,4 +12,6 @@ int paging_freelist_init(struct domain *d, unsigned long = pages, =20 bool paging_ret_pages_to_domheap(struct domain *d, unsigned int nr_pages); =20 +void paging_free_page(struct domain *d, struct page_info *pg); + #endif /* ASM_RISCV_PAGING_H */ diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 6c99719c66..2467e459cc 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -197,6 +197,16 @@ 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 p2m_type_t p2m_get_type(const pte_t pte) +{ + p2m_type_t type =3D MASK_EXTR(pte.pte, P2M_TYPE_PTE_BITS_MASK); + + if ( type =3D=3D p2m_ext_storage ) + panic("unimplemented\n"); + + return type; +} + static inline void p2m_write_pte(pte_t *p, pte_t pte, bool clean_pte) { write_pte(p, pte); @@ -248,11 +258,136 @@ static int p2m_next_level(struct p2m_domain *p2m, bo= ol alloc_tbl, return P2M_TABLE_MAP_NONE; } =20 +static void p2m_put_foreign_page(struct page_info *pg) +{ + /* + * It=E2=80=99s safe to call put_page() here because arch_flush_tlb_ma= sk() + * will be invoked if the page is reallocated before the end of + * this loop, which will trigger a flush of the guest TLBs. + */ + put_page(pg); +} + +/* Put any references on the single 4K page referenced by mfn. */ +static void p2m_put_4k_page(mfn_t mfn, p2m_type_t type) +{ + /* TODO: Handle other p2m types */ + + if ( p2m_is_foreign(type) ) + { + ASSERT(mfn_valid(mfn)); + p2m_put_foreign_page(mfn_to_page(mfn)); + } + + /* + * Detect the xenheap page and mark the stored GFN as invalid. + * We don't free the underlying page until the guest requested to do s= o. + * So we only need to tell the page is not mapped anymore in the P2M by + * marking the stored GFN as invalid. + */ + if ( p2m_is_ram(type) && is_xen_heap_mfn(mfn) ) + page_set_xenheap_gfn(mfn_to_page(mfn), INVALID_GFN); +} + +/* Put any references on the superpage referenced by mfn. */ +static void p2m_put_2m_superpage(mfn_t mfn, p2m_type_t type) +{ + struct page_info *pg; + unsigned int i; + + ASSERT(mfn_valid(mfn)); + + pg =3D mfn_to_page(mfn); + + for ( i =3D 0; i < XEN_PT_ENTRIES; i++, pg++ ) + p2m_put_foreign_page(pg); +} + +/* Put any references on the page referenced by pte. */ +static void p2m_put_page(const pte_t pte, unsigned int level) +{ + mfn_t mfn =3D pte_get_mfn(pte); + p2m_type_t p2m_type =3D p2m_get_type(pte); + + ASSERT(pte_is_valid(pte)); + + /* + * TODO: Currently we don't handle level 2 super-page, Xen is not + * preemptible and therefore some work is needed to handle such + * superpages, for which at some point Xen might end up freeing memory + * and therefore for such a big mapping it could end up in a very long + * operation. + */ + switch ( level ) + { + case 1: + return p2m_put_2m_superpage(mfn, p2m_type); + + case 0: + return p2m_put_4k_page(mfn, p2m_type); + } +} + +static void p2m_free_page(struct p2m_domain *p2m, struct page_info *pg) +{ + page_list_del(pg, &p2m->pages); + + paging_free_page(p2m->domain, pg); +} + /* Free pte sub-tree behind an entry */ static void p2m_free_subtree(struct p2m_domain *p2m, pte_t entry, unsigned int level) { - panic("%s: hasn't been implemented yet\n", __func__); + unsigned int i; + pte_t *table; + mfn_t mfn; + struct page_info *pg; + + /* Nothing to do if the entry is invalid. */ + if ( !pte_is_valid(entry) ) + return; + + if ( pte_is_superpage(entry, level) || (level =3D=3D 0) ) + { +#ifdef CONFIG_IOREQ_SERVER + /* + * If this gets called then either the entry was replaced by an en= try + * with a different base (valid case) or the shattering of a super= page + * has failed (error case). + * So, at worst, the spurious mapcache invalidation might be sent. + */ + if ( p2m_is_ram(p2m_get_type(p2m, entry)) && + domain_has_ioreq_server(p2m->domain) ) + ioreq_request_mapcache_invalidate(p2m->domain); +#endif + + p2m_put_page(entry, level); + + return; + } + + table =3D map_domain_page(pte_get_mfn(entry)); + for ( i =3D 0; i < XEN_PT_ENTRIES; i++ ) + p2m_free_subtree(p2m, table[i], level - 1); + + unmap_domain_page(table); + + /* + * Make sure all the references in the TLB have been removed before + * freing the intermediate page table. + * XXX: Should we defer the free of the page table to avoid the + * flush? + */ + p2m_tlb_flush_sync(p2m); + + mfn =3D pte_get_mfn(entry); + ASSERT(mfn_valid(mfn)); + + pg =3D mfn_to_page(mfn); + + page_list_del(pg, &p2m->pages); + p2m_free_page(p2m, pg); } =20 /* diff --git a/xen/arch/riscv/paging.c b/xen/arch/riscv/paging.c index bbe1186900..853e0e14c6 100644 --- a/xen/arch/riscv/paging.c +++ b/xen/arch/riscv/paging.c @@ -84,6 +84,13 @@ bool paging_ret_pages_to_domheap(struct domain *d, unsig= ned int nr_pages) return true; } =20 +void paging_free_page(struct domain *d, struct page_info *pg) +{ + spin_lock(&d->arch.paging.lock); + page_list_add_tail(pg, &d->arch.paging.freelist); + spin_unlock(&d->arch.paging.lock); +} + /* Domain paging struct initialization. */ int paging_domain_init(struct domain *d) { --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977670; cv=none; d=zohomail.com; s=zohoarc; b=V853DiRpKIzd9+1V7lwbzaWhSjsMQrNcljxd9hLcpj+dQbXyGjBVGYBTUCWAog3oXZun7aMR1LXN1taBluzZEtdtU5LcQ1PzlsGfg25yYtK/fHv+tEfl7cbYf4DYEQuAqzclbxmJJm89GPYEK9yyyhFcYX71KlZ+ndeixOWaCCM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977670; 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=VGuaeyIsrA+iYqDJovnTvqJCdpSjIdCxUfSdeY8yfW4=; b=MPb24s9HNzENfQHESyINsMvjCJhFICBWr/+5bjIXlOBcevXaTZAYi3FVgZy1UINKV1xWVpLfyiwjPheuKLbIeL7lIMKOmAPylt+oM581SbwincoU35B8LN/LQj4tjB4Imvf+whOu2cD0bN4D9rqvzICLCyqeWNlSeWj2//dmvnQ= 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 1753977670189132.40571166215216; Thu, 31 Jul 2025 09:01:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065923.1431306 (Exim 4.92) (envelope-from ) id 1uhViA-0000MZ-Ce; Thu, 31 Jul 2025 16:00:54 +0000 Received: by outflank-mailman (output) from mailman id 1065923.1431306; Thu, 31 Jul 2025 16:00:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhViA-0000ME-8O; Thu, 31 Jul 2025 16:00:54 +0000 Received: by outflank-mailman (input) for mailman id 1065923; Thu, 31 Jul 2025 16:00:52 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVgF-0000zK-Pz for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:55 +0000 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [2a00:1450:4864:20::136]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 40911406-6e27-11f0-a320-13f23c93f187; Thu, 31 Jul 2025 17:58:51 +0200 (CEST) Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-55b8a7a505cso806966e87.0 for ; Thu, 31 Jul 2025 08:58:51 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:50 -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: 40911406-6e27-11f0-a320-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977531; x=1754582331; 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=VGuaeyIsrA+iYqDJovnTvqJCdpSjIdCxUfSdeY8yfW4=; b=JPQ/BupyJjw/cBjREdS4LQ0+sWRUly5ld7QzK5DueG17Au+WxEInCdYBt2qyiPM8RF nUs5DQVNyGPACBvBVRuDGKSE4fJXjjcr00MZD3BnSOiTLTbVz0l5FXCSuclilGGDtuON Kzk1MzXF/cRXEVKA4Rf+4b586ZcXboA6veW5A6j8hSUqahwDKKeJUC9kGxet6k5BJD+a 0QFW+sdsSv8uaIL/PPk73SDsUsio/HkCjX4M1vBVtosZR4MftZqimuo9azdYAnKOAkG1 h0WvLo/Icd/IQWfEeHP1qwsmhWSHxluSFTrQEu6jA1Hk9gheB0w+euB+tFnOe6CjIH35 TJFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977531; x=1754582331; 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=VGuaeyIsrA+iYqDJovnTvqJCdpSjIdCxUfSdeY8yfW4=; b=sUEBfGh5zM+//eeyWIOGwMJxQd2o7eMau+xri1mFZEpYNJjhPGW9deSIHMmcJNrRJU DwMwjMHUvCWs1dQ0gO18HTGHHpuJrtFnnK97sFGxQEhKzpNBiS0aNYs1FIacxJsIk80Y eNNYJiNfw5uJ9OVo2FH/bXNCYD+VLEQrbPNnmgrn+SSN36RP8WmyBaXxZuA0qap4fRvZ 4eqwVG0R+wqyvHo3MW02qsEnjGxNzLUYjtSX62puIRg34qnep8RHNSivtKYmQqiYuWys HMm+9J3HZzWnrXYYZw46W3awNlRbKaYro8SVObfEFVPjRIMqTfrE4lZKGkovBQc429pR 5Q1g== X-Gm-Message-State: AOJu0YzYXzke1uY8ANeF7pG6g9eLuCxMTcGwMkWYf2DTnrepDyHFqUH6 qXZvZVzSBZ0dDa8rXTx9QvRMk2ArIGBvzpohBQmzUsBpDSXY/A8RXBGfjUhZMw== X-Gm-Gg: ASbGncsovFPLl1MVHMoNvJyhEJoTlet8+cD95NhEijO191GECO8nYIlt6G+7MhkGdk0 rRfqdXcqz8XMgXBBf3TpnAMxx27FQtS1rtHMD4WE0vWabaigPA76H7fndV2HhMkMEBZr8oEJ9ic qsxZ+E8FuDuf8IDijPh0wXjurrlrWexqOTJL2ZOOxK8ZrBgKJoHjR+87YBk/R1fD1CJWghVCigW HwyqA86XRY6zeSmymwZPnHLCnLKIyP4AGoOH6GMHSlSu2KWIIE5gHZL6AslnJggWuGcfsiywJ1q 9oGL0HsiFntfdlx3fDEfleoPJof4MNLiZwfX0+yUgiz+RWHeSVocZrMqHC652Vm/aVn94oZfgM1 wyfpZGxLsUTr8rH6L3c+KRDIfGQSQnT8vmx5OM/v3WJYn5apMlofxhSqgKQ2OVA== X-Google-Smtp-Source: AGHT+IEsoeeagjmyHtu3wRT1JJsdymImNNNZGlYuJEOxoAoCVJ5nrkWRU3SLoRJ4U+L/sovz48KQDg== X-Received: by 2002:a05:6512:401f:b0:553:37a7:b217 with SMTP id 2adb3069b0e04-55b7c05ab12mr3091160e87.35.1753977530514; Thu, 31 Jul 2025 08:58:50 -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 v3 14/20] xen/riscv: Implement p2m_pte_from_mfn() and support PBMT configuration Date: Thu, 31 Jul 2025 17:58:13 +0200 Message-ID: <1370613cd6f52e90591b15ad52c95d7c81908cf6.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977672195116600 Content-Type: text/plain; charset="utf-8" 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. 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 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/include/asm/page.h | 8 +++ xen/arch/riscv/p2m.c | 81 +++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 4 deletions(-) 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 2467e459cc..efc7320619 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -197,6 +198,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_ext_storage ) + 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); @@ -222,11 +235,71 @@ 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; + + switch ( t ) + { + case p2m_grant_map_rw: + case p2m_ram_rw: + e->pte |=3D PTE_READABLE | PTE_WRITABLE; + break; + + case p2m_ext_storage: + case p2m_mmio_direct_io: + e->pte |=3D PTE_ACCESS_MASK; + break; + + case p2m_invalid: + e->pte &=3D ~(PTE_ACCESS_MASK | PTE_VALID); + break; + + case p2m_grant_map_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)); + + if ( !is_table ) + { + p2m_set_permission(&e, t); + + if ( t < p2m_ext_storage ) + p2m_set_type(&e, t); + else + panic("unimplemeted\n"); + } + 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 @@ -469,7 +542,7 @@ static int p2m_set_entry(struct p2m_domain *p2m, p2m_clean_pte(entry, p2m->clean_pte); 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_pte); =20 --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977710; cv=none; d=zohomail.com; s=zohoarc; b=l70PDt5Zy1j4O5gouq6nU0f5NA/8CXIMtni/k5qWtexpwdDXzCO1jKU0uL3wDDoRgMUQfaIWVPl8MhrDGRyYWl2Isy4fzh/dOdVD5PZ9tx7F60XLyT4ZrcYCG2Wf95ZjkpDFgflPiwB4u/l9wViIZwplctQ4iIvYiaXpSCOSR4M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977710; 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=6WvQAbd5W6c1J3vMXeOaOWZxLDZn187RMIM0P4l0lSA=; b=iK5Q4lmELdxXDPHq9HOJ45DjOfB3ZuagZaXVfMW6acxh1EW3pvPmw80jCV8XMGarb5oHTry2Y6BZwg1668Z7zNSv1o26OegTvklH/04mTpPwOlWVB/1im8GPOxhP51Dl1sgq0y9b85OBJpdDH05SdP63E4iTKTSGzxtogl4gbFc= 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 1753977710497535.582622982289; Thu, 31 Jul 2025 09:01:50 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065958.1431338 (Exim 4.92) (envelope-from ) id 1uhViu-0002Bu-94; Thu, 31 Jul 2025 16:01:40 +0000 Received: by outflank-mailman (output) from mailman id 1065958.1431338; Thu, 31 Jul 2025 16:01:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhViu-0002AO-1c; Thu, 31 Jul 2025 16:01:40 +0000 Received: by outflank-mailman (input) for mailman id 1065958; Thu, 31 Jul 2025 16:01:38 +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 1uhVgF-0001FB-1F for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:55 +0000 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [2a00:1450:4864:20::135]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 415b75fa-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:52 +0200 (CEST) Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-55b80332144so1151450e87.1 for ; Thu, 31 Jul 2025 08:58:52 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:51 -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: 415b75fa-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977532; x=1754582332; 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=6WvQAbd5W6c1J3vMXeOaOWZxLDZn187RMIM0P4l0lSA=; b=UY5fqwjXLAZNhxIvtTB5apeqMKTWSalM+VwXxr93XwQn4N5HL2YbfoOKJmOH8Tj0ZT AP+xgkJYWyCx0j6OeBiuE1GnY3Rfu4ikmI0BmR0+M+VYw2CR84FBdsnpavKPQS6Dk7DP /rbhGxCdS1a0CiOP+kyeoDKb/QjHmJbnt8AsjoEClWAjkGItG1YNb08dMabaIoq/njLq PZZhwsb2cDgKxvdHwN2EbHuQ3cs0K0GiWV94lQaqrmfedjsjttUgtAQBrjh/sc+7xz9L MxYeInOVoIZWniOilOa/qlatpY6X2oD/evAWebeRFgh+kPPh6hts6gJcOmt0ozbLufh/ oXOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977532; x=1754582332; 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=6WvQAbd5W6c1J3vMXeOaOWZxLDZn187RMIM0P4l0lSA=; b=PCF4Xp1iTMvK2CeFauXbabfQiAWGwNgAjQK5w3Jja5zX2BtCAlmo/MB69UmkygsO/Z uBb4Gzs5CkjATs7nW4xtMnGRFunRcy88p+BA6DWJeHTYPtuIZGoPgEPF97Lh5t1On2Ic N38Tdi6vhGk9MHzK3lmtErZ/nwb8iI4x6gNEOxnJUPFRmfNOeosvejdpilIeyerH9sfd BUiyFDbDHe7iMiNM2T36qXPXCfnB0t9k/0Psc3neVIjgxP5bxW92fGa07qQZcYJvRtYX BJEWCw8H1sxBXyqAPlC4/7vgw0ZW5XuF1zIet4PZrDoa9ia/Lg/S/FGnHNDglPWN6d4w K/Lg== X-Gm-Message-State: AOJu0Yz+QfrfKXl6HMm1c/wuCQn6KdApBIy3s3M093s7bYDMG7CzVDIj PQT0njZXI7A/Kb4b9VE7yPhDVDfElQTNrwSZnadlIubU+91D2ZEtVenQxbsB/w== X-Gm-Gg: ASbGncvdcGwpBMnq2eJL5ea/Eug2j0PNy6K/JZip406UO+pI3BeubPYte/LnjZVNWwV RkbjchvGYrp1ELMytBom6X73Re1zuE5ousbdoh4Oqo80jPBKPNhBIma0dLE47b+eftCJ8KPTCkZ SHPFTRGYaZ7gnjz9j9zQnnwBqkBrh6mxKfGCXZ+ZSKtsp5y07ThRIaOtD10cLMUpzM1yCK8kBKq UP/DiK56do5pNS4jNTK1xU+1qrog3WE8LQ3YCcpQQjA8TUUiBJsYkknlU1lUOl0qbQvJ6VaIL/e HmhQOGHqw3vinsVAa6Ak79OZk8pMSiizesAja+9uekds2VUlfldEvuFJPUpu8Kc60kniBPJJWmX PX8n63bDPp/CTWgnAP0GSC4wTLEeg/xT6MzaZiWVJGN++ypxDuXqCxu+FOZScRA== X-Google-Smtp-Source: AGHT+IFUL+r0vELSRPXlMJpUW6Bf4l26MhjYDT+k1OvitanoT2IEWz2lxt4Du+S5xPPH0p+SKix/tw== X-Received: by 2002:a05:6512:239e:b0:553:2cc1:2bb4 with SMTP id 2adb3069b0e04-55b7c00ba56mr2871988e87.12.1753977531883; Thu, 31 Jul 2025 08:58:51 -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 v3 15/20] xen/riscv: implement p2m_next_level() Date: Thu, 31 Jul 2025 17:58:14 +0200 Message-ID: <8d8953fb00b4ac64bb2e82d827da4fbbe0c29918.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977713174116600 Content-Type: text/plain; charset="utf-8" Implement the p2m_next_level() function, which enables traversal and dynamic allocation of intermediate levels (if necessary) in the RISC-V p2m (physical-to-machine) page table hierarchy. To support this, the following helpers are introduced: - page_to_p2m_table(): Constructs non-leaf PTEs pointing to next-level page tables with correct attributes. - p2m_alloc_page(): Allocates page table pages, supporting both hardware and guest domains. - p2m_create_table(): Allocates and initializes a new page table page and installs it into the hierarchy. Signed-off-by: Oleksii Kurochko --- Changes in V3: - s/p2me_is_mapping/p2m_is_mapping to be in syc with other p2m_is_*() func= tions. - clear_and_clean_page() in p2m_create_table() instead of clear_page() to = be sure that page is cleared and d-cache is flushed for it. - Move ASSERT(level !=3D 0) in p2m_next_level() ahead of trying to allocat= e a page table. - Update p2m_create_table() to allocate metadata page to store p2m type in= it for each entry of page table. - Introduce paging_alloc_page() and use it inside p2m_alloc_page(). - Add allocated page to p2m->pages list in p2m_alloc_page() to simplify a caller code a little bit. - Drop p2m_is_mapping() and use pte_is_mapping() instead as P2M PTE's valid bit doesn't have another purpose anymore. - Update an implementation and prototype of page_to_p2m_table(), it is eno= ugh to pass only a page as an argument. --- Changes in V2: - New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller. - s/p2m_is_mapping/p2m_is_mapping. --- xen/arch/riscv/include/asm/paging.h | 2 + xen/arch/riscv/p2m.c | 80 ++++++++++++++++++++++++++++- xen/arch/riscv/paging.c | 11 ++++ 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/xen/arch/riscv/include/asm/paging.h b/xen/arch/riscv/include/a= sm/paging.h index c9063b7f76..3642bcfc7a 100644 --- a/xen/arch/riscv/include/asm/paging.h +++ b/xen/arch/riscv/include/asm/paging.h @@ -14,4 +14,6 @@ bool paging_ret_pages_to_domheap(struct domain *d, unsign= ed int nr_pages); =20 void paging_free_page(struct domain *d, struct page_info *pg); =20 +struct page_info * paging_alloc_page(struct domain *d); + #endif /* ASM_RISCV_PAGING_H */ diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index efc7320619..e04cfde8c7 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -302,6 +302,48 @@ static pte_t p2m_pte_from_mfn(mfn_t mfn, p2m_type_t t,= bool is_table) return e; } =20 +/* Generate table entry with correct attributes. */ +static pte_t page_to_p2m_table(struct page_info *page) +{ + /* + * p2m_invalid will be ignored inside p2m_pte_from_mfn() as is_table is + * set to true and p2m_type_t shouldn't be applied for PTEs which + * describe an intermidiate table. + */ + return p2m_pte_from_mfn(page_to_mfn(page), p2m_invalid, true); +} + +static struct page_info *p2m_alloc_page(struct p2m_domain *p2m) +{ + struct page_info *pg =3D paging_alloc_page(p2m->domain); + + if ( pg ) + page_list_add(pg, &p2m->pages); + + return pg; +} + +/* + * Allocate a new page table page with an extra metadata page and hook it + * in via the given entry. + */ +static int p2m_create_table(struct p2m_domain *p2m, pte_t *entry) +{ + struct page_info *page; + + ASSERT(!pte_is_valid(*entry)); + + page =3D p2m_alloc_page(p2m); + if ( page =3D=3D NULL ) + return -ENOMEM; + + clear_and_clean_page(page); + + p2m_write_pte(entry, page_to_p2m_table(page), p2m->clean_pte); + + return 0; +} + #define P2M_TABLE_MAP_NONE 0 #define P2M_TABLE_MAP_NOMEM 1 #define P2M_TABLE_SUPER_PAGE 2 @@ -326,9 +368,43 @@ static int p2m_next_level(struct p2m_domain *p2m, bool= alloc_tbl, unsigned int level, pte_t **table, unsigned int offset) { - panic("%s: hasn't been implemented yet\n", __func__); + pte_t *entry; + int ret; + mfn_t mfn; + + /* The function p2m_next_level() is never called at the last level */ + ASSERT(level !=3D 0); + + entry =3D *table + offset; + + if ( !pte_is_valid(*entry) ) + { + if ( !alloc_tbl ) + return P2M_TABLE_MAP_NONE; + + ret =3D p2m_create_table(p2m, entry); + if ( ret ) + return P2M_TABLE_MAP_NOMEM; + } + + /* The function p2m_next_level() is never called at the last level */ + ASSERT(level !=3D 0); + if ( pte_is_mapping(*entry) ) + return P2M_TABLE_SUPER_PAGE; + + mfn =3D mfn_from_pte(*entry); + + unmap_domain_page(*table); + + /* + * TODO: There's an inefficiency here: + * In p2m_create_table(), the page is mapped to clear it. + * Then that mapping is torn down in p2m_create_table(), + * only to be re-established here. + */ + *table =3D map_domain_page(mfn); =20 - return P2M_TABLE_MAP_NONE; + return P2M_TABLE_NORMAL; } =20 static void p2m_put_foreign_page(struct page_info *pg) diff --git a/xen/arch/riscv/paging.c b/xen/arch/riscv/paging.c index 853e0e14c6..72ff183260 100644 --- a/xen/arch/riscv/paging.c +++ b/xen/arch/riscv/paging.c @@ -91,6 +91,17 @@ void paging_free_page(struct domain *d, struct page_info= *pg) spin_unlock(&d->arch.paging.lock); } =20 +struct page_info * paging_alloc_page(struct domain *d) +{ + struct page_info *pg; + + spin_lock(&d->arch.paging.lock); + pg =3D page_list_remove_head(&d->arch.paging.freelist); + spin_unlock(&d->arch.paging.lock); + + return pg; +} + /* Domain paging struct initialization. */ int paging_domain_init(struct domain *d) { --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977760; cv=none; d=zohomail.com; s=zohoarc; b=Ehpw30vdD8qI7UYpouMJJ8ohqHrJMmVa5u+f8PoUGDKggJeWbhz8RdQOrlomqTGfxLfEsbMYgeF4DqkcEmxG0mb0KF5rqfuyt5+3EV112v444M9ysIi9WWmkP7bBOtaUCIl6fIhrdG4f2ts3Kg7aKKagGf+9LW2m+0lehld84K0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977760; 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=jC83dOB30X6PShO+Qx3bRZ1eAKfCHiOihl7DCHI89Rg=; b=Ey3j/FRfg0q4MUY8TLgRpIHzS6CL/+MDCA55XaZUERe2LrEUbAxVvgKJitis3C29Bh4ypsPkKaT7vCqvmElRofBNdiDBRjuT4IRK3Ez3dDm6l8VyNOTWubxyQvjDmgvnwfsYSbRo5Z6GMwhNhtWkN3T+M3xdcJLxxp3bxt/dI6w= 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 1753977760760265.87877239781767; Thu, 31 Jul 2025 09:02:40 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065984.1431362 (Exim 4.92) (envelope-from ) id 1uhVjh-0003xL-PD; Thu, 31 Jul 2025 16:02:29 +0000 Received: by outflank-mailman (output) from mailman id 1065984.1431362; Thu, 31 Jul 2025 16:02:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVjh-0003xE-Lk; Thu, 31 Jul 2025 16:02:29 +0000 Received: by outflank-mailman (input) for mailman id 1065984; Thu, 31 Jul 2025 16:02:27 +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 1uhVgG-0001FB-SC for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:56 +0000 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [2a00:1450:4864:20::133]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 42c038b9-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:55 +0200 (CEST) Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-55b797ad392so1255418e87.3 for ; Thu, 31 Jul 2025 08:58:55 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:52 -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: 42c038b9-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977534; x=1754582334; 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=jC83dOB30X6PShO+Qx3bRZ1eAKfCHiOihl7DCHI89Rg=; b=DuxV2xYlMgqCAdu1QlrUMqrMaEJyw222kGBSWFdnu2kK/bBgmfJhJjKuP+9ZCtcbL4 mjdKDFMpinZWeeAIJ3lVsdaVNvLuRqtjGMTvP2AFFd6B5BXU0dotAv4CFwap4xeP4aXH GCoyGUXgh90/3ICyXwd6g+EmO22ywThpdenqu5updfRHShuH4El/qlxra4a6sH+8Zirp /4rLA7q47Ww9hIiWROqa8sakIdaV1gDJ+hnLcpVX9EGC9rVlRCPVk10QN/TJIH5eBg17 Wk/w9GqYld7gy755HJ9ShxCfKj2XqMNROMfV9qh5yRDJrO0KmYLfr1RhEubQv8Fz0fel ekFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977534; x=1754582334; 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=jC83dOB30X6PShO+Qx3bRZ1eAKfCHiOihl7DCHI89Rg=; b=g0+4Q6wDDWN5nV5gWKGv+MHSpdZRDjK3frVBB9e68LfvPjNmtNKfU7bIV0OYjT5BtC N0Q2rgdZtphBITG2d7gl0QbbaX0PZ3AOoEquDW/iD96AKwBX/46PPuOVWqsWschZ+aeT zrQ0Q2sb9R4uyW8/CBlE57fzsZcUwQf7rREF4blV8uMRNqQz82n5LUhUSrbkEdDRNFkn 7jeD/EIR80EAMkb68N67tnfrg1QUSsqesRCYDQ8FC1vadh+ws1pOKafFtSTgzADo50P/ jdBiB6nS7d3hSD1ki4wCpFk0i/Eq323kx6D3l8EvrQHiHLYMBGlcrt4WIltaGizBA5nd rAgA== X-Gm-Message-State: AOJu0YwsTIUteHSGJ/dHEeaH9h7uNLf0vEbiDGzpRcKpvL4cRtJXxjX/ YOcuIqJHWjYELkVYszy4915s7ytMLAkpHSjGdH0wEDHECTeXKKD+bvDGifBU6w== X-Gm-Gg: ASbGncuLNw/GH/uSoQab2sEmTEzLpL9TUye4+jx68iw1DpJkU6OdJIpCYWWvk0xynN6 Z9E3aAIada3nyk3x5YPevyF7kjhKwRKhrTpgA5J86WSjobqjo7bTCHdoBZiiTTeKjUTh85bO09/ PBGIeJxh6BNSeO87hL3DArvTij8zSIW8yNWIiJ1cwIwh6Mn9x5UZf80HKo+JzjQw+9BfKiY+Q67 tMv6HQg6EfcfzseVn9tawXr4BtEi7Q5OaqftNWBtigklG1Sb7qJ41NfNyrWTpMBBe0aeVG8YZGo Si+lxKbeHSA26bRnrpOn2+LlZ9PEZy75HwroNqHnQO5xI5p7/Vra6emrkPQ3WM/l+m2vT44n3ND w9CqL/a5h3yfq06zYsCQ0VOFPDkH63eFeCeOz+DIvq/Q5u2PHxq9t3AIN+ZqkXw== X-Google-Smtp-Source: AGHT+IHl86cJn6eEwaEmMR51hDkdErwFOdWHxda1zYGRfB7ptkNhfSwqjKkOryMIGf9vdN+g40172w== X-Received: by 2002:a05:6512:693:b0:553:da39:37d with SMTP id 2adb3069b0e04-55b7c012815mr2352425e87.1.1753977533183; Thu, 31 Jul 2025 08:58:53 -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 v3 16/20] xen/riscv: Implement superpage splitting for p2m mappings Date: Thu, 31 Jul 2025 17:58:15 +0200 Message-ID: <6890f77e577493194ea94834989dc0841d18eed0.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 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: 1753977761874116600 Add support for down large memory mappings ("superpages") in the RISC-V p2m mapping so that smaller, more precise mappings ("finer-grained entries") can be inserted into lower levels of the page table hierarchy. To implement that the following is done: - Introduce p2m_split_superpage(): Recursively shatters a superpage into smaller page table entries down to the target level, preserving original permissions and attributes. - p2m_set_entry() updated to invoke superpage splitting when inserting entries at lower levels within a superpage-mapped region. This implementation is based on the ARM code, with modifications to the part that follows the BBM (break-before-make) approach, some parts are simplified as according to RISC-V spec: It is permitted for multiple address-translation cache entries to co-exist for the same address. This represents the fact that in a conventional TLB hierarchy, it is possible for multiple entries to match a single address if, for example, a page is upgraded to a superpage without first clearing the original non-leaf PTE=E2=80=99s valid bit and executing an S= FENCE.VMA with rs1=3Dx0, or if multiple TLBs exist in parallel at a given level of = the hierarchy. In this case, just as if an SFENCE.VMA is not executed between a write to the memory-management tables and subsequent implicit read of t= he same address: it is unpredictable whether the old non-leaf PTE or the new leaf PTE is used, but the behavior is otherwise well defined. In contrast to the Arm architecture, where BBM is mandatory and failing to use it in some cases can lead to CPU instability, RISC-V guarantees stability, and the behavior remains safe =E2=80=94 though unpredictable in = terms of which translation will be used. Additionally, the page table walk logic has been adjusted, as ARM uses the opposite number of levels compared to RISC-V. Signed-off-by: Oleksii Kurochko --- Changes in V3: - Move page_list_add(page, &p2m->pages) inside p2m_alloc_page(). - Use 'unsigned long' for local vairiable 'i' in p2m_split_superpage(). - Update the comment above if ( next_level !=3D target ) in p2m_split_supe= rpage(). - Reverse cycle to iterate through page table levels in p2m_set_entry(). - Update p2m_split_superpage() with the same changes which are done in the patch "P2M: Don't try to free the existing PTE if we can't allocate a ne= w table". --- Changes in V2: - New patch. It was a part of a big patch "xen/riscv: implement p2m mapping functionality" which was splitted to smaller. - Update the commit above the cycle which creates new page table as RISC-V travserse page tables in an opposite to ARM order. - RISC-V doesn't require BBM so there is no needed for invalidating and TLB flushing before updating PTE. --- xen/arch/riscv/p2m.c | 118 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index e04cfde8c7..e9e6818da2 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -539,6 +539,91 @@ static void p2m_free_subtree(struct p2m_domain *p2m, p2m_free_page(p2m, pg); } =20 +static bool p2m_split_superpage(struct p2m_domain *p2m, pte_t *entry, + unsigned int level, unsigned int target, + const unsigned int *offsets) +{ + struct page_info *page; + unsigned long i; + pte_t pte, *table; + bool rv =3D true; + + /* Convenience aliases */ + mfn_t mfn =3D pte_get_mfn(*entry); + unsigned int next_level =3D level - 1; + unsigned int level_order =3D XEN_PT_LEVEL_ORDER(next_level); + + /* + * This should only be called with target !=3D level and the entry is + * a superpage. + */ + ASSERT(level > target); + ASSERT(pte_is_superpage(*entry, level)); + + page =3D p2m_alloc_page(p2m->domain); + if ( !page ) + { + /* + * The caller is in charge to free the sub-tree. + * As we didn't manage to allocate anything, just tell the + * caller there is nothing to free by invalidating the PTE. + */ + memset(entry, 0, sizeof(*entry)); + return false; + } + + table =3D __map_domain_page(page); + + /* + * We are either splitting a second level 1G page into 512 first level + * 2M pages, or a first level 2M page into 512 zero level 4K pages. + */ + for ( i =3D 0; i < XEN_PT_ENTRIES; i++ ) + { + pte_t *new_entry =3D table + i; + + /* + * Use the content of the superpage entry and override + * the necessary fields. So the correct permission are kept. + */ + pte =3D *entry; + pte_set_mfn(&pte, mfn_add(mfn, i << level_order)); + + write_pte(new_entry, pte); + } + + /* + * Shatter superpage in the page to the level we want to make the + * changes. + * This is done outside the loop to avoid checking the offset + * for every entry to know whether the entry should be shattered. + */ + if ( next_level !=3D target ) + rv =3D p2m_split_superpage(p2m, table + offsets[next_level], + level - 1, target, offsets); + + if ( p2m->clean_pte ) + clean_dcache_va_range(table, PAGE_SIZE); + + /* + * TODO: an inefficiency here: the caller almost certainly wants to map + * the same page again, to update the one entry that caused the + * request to shatter the page. + */ + unmap_domain_page(table); + + /* + * Even if we failed, we should (according to the current implemetation + * of a way how sub-tree is freed if p2m_split_superpage hasn't been + * finished fully) install the newly allocated PTE + * entry. + * The caller will be in charge to free the sub-tree. + */ + p2m_write_pte(entry, page_to_p2m_table(page), p2m->clean_pte); + + return rv; +} + /* * Insert an entry in the p2m. This should be called with a mapping * equal to a page/superpage. @@ -603,7 +688,38 @@ static int p2m_set_entry(struct p2m_domain *p2m, */ if ( level > target ) { - panic("Shattering isn't implemented\n"); + /* We need to split the original page. */ + pte_t split_pte =3D *entry; + + ASSERT(pte_is_superpage(*entry, level)); + + if ( !p2m_split_superpage(p2m, &split_pte, level, target, offsets)= ) + { + /* Free the allocated sub-tree */ + p2m_free_subtree(p2m, split_pte, level); + + rc =3D -ENOMEM; + goto out; + } + + p2m_write_pte(entry, split_pte, p2m->clean_pte); + + p2m->need_flush =3D true; + + /* Then move to the level we want to make real changes */ + for ( ; level > target; level-- ) + { + rc =3D p2m_next_level(p2m, true, level, &table, offsets[level]= ); + + /* + * The entry should be found and either be a table + * or a superpage if level 0 is not targeted + */ + ASSERT(rc =3D=3D P2M_TABLE_NORMAL || + (rc =3D=3D P2M_TABLE_SUPER_PAGE && target > 0)); + } + + entry =3D table + offsets[level]; } =20 /* --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977692; cv=none; d=zohomail.com; s=zohoarc; b=ZTTBZsjSRjpynHKt+zEqInxEhprujoCe9Yl4jrX7myI2TGPBzc3gTOhwYIRTeXvZIf8o8yW6WUUCSCv1RyRVuWm6xjbgaOHt6W73aXKo8yc2Mz6rtju7dXGvoULFD8vOiJ5nxPfTMlFkPzw4gqBR79VjIDURwBOy1GiEEEMh0do= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977692; 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=e2OLoR+tK71eYXjQc2Rg1scLe+IVcYggQesNhZh0GIk=; b=ae8+J/YcvpGGKlAGJTV7neY830b8plEApO7EY+9+Dq76a0xD5MVFnZGNPErCrnbS540OC6uTkS+wVrAs1QyUb+Wx0vU6jnbAlG3PGbtPWqkzXc6B2Qu8FV0fEtqCPpl9Sy/wEI3PrW0MrM//FVbcWSt4PUG/bvjCpfVdt0ZLpiE= 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 1753977692363378.21665157838856; Thu, 31 Jul 2025 09:01:32 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065942.1431322 (Exim 4.92) (envelope-from ) id 1uhViZ-0001U6-In; Thu, 31 Jul 2025 16:01:19 +0000 Received: by outflank-mailman (output) from mailman id 1065942.1431322; Thu, 31 Jul 2025 16:01:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhViZ-0001Tz-Fh; Thu, 31 Jul 2025 16:01:19 +0000 Received: by outflank-mailman (input) for mailman id 1065942; Thu, 31 Jul 2025 16:01:17 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVgM-0000zK-RF for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:59:02 +0000 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [2a00:1450:4864:20::134]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 435e9932-6e27-11f0-a320-13f23c93f187; Thu, 31 Jul 2025 17:58:56 +0200 (CEST) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-55502821bd2so573430e87.2 for ; Thu, 31 Jul 2025 08:58:56 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:54 -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: 435e9932-6e27-11f0-a320-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977535; x=1754582335; 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=e2OLoR+tK71eYXjQc2Rg1scLe+IVcYggQesNhZh0GIk=; b=PzglZtZYT/Z51T+dsYkMiIoojpRlEe4m6XCzSIf7TPNEk42pYw26sFYXNqSPJcNjWo I4y4sO/PhCSznacHLC0b60jtAhzMl032eBhDS8qsNWYec9uK07KI00F1pzqLi6XB+Ntn L9xuY5CYLK066S/J+/SKqvdeQdVMosmfCOjZv/bItRfTDeIuoJs6QiWD/qnMPghBwvk7 Pxtp9I4vpAaq/yhMOUSD4HsIj2okNEa5f7VIKIyviI8Ya6fSzWGJmbNrMliWPmmeeeov RJrzQC3M34tesQIpgATHfOL5CvFB1VUy9mqu2mfWThCnbewEXbQANB7EEu8A7Q9tkATE ND8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977535; x=1754582335; 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=e2OLoR+tK71eYXjQc2Rg1scLe+IVcYggQesNhZh0GIk=; b=pnjl+RSGTpihOs45/zDrTV3/nOBF9amLS2Ce1UaohK6cDdC264gb+XzulDps+HxKX8 0BrJ4sqv+ztxLnT9f572N5tvdEnSEBavK34L/QSbD5NZdj2zv+MT3prPaTBtJS5t4QBV no7e6Agtt8jCGkt+oKMU9nzWSAZHN43Yb9bzHQ7bL3daT5CKao8nPl6eQcYHybnie3jE Tw2N2g/UbYT3+TlejLsOBal+21t0Slsug5HsudbI0PobPMaembHlVw77S6kZcVPmob38 Qcy99N0mxSeMXKIF+SaKr/9Lqr+IvlUEZwdNEIunLJ4UP7zPDtU/en32KhWkWuWlBtyj wTKg== X-Gm-Message-State: AOJu0Yzs/SNByaHBblIS/95nlR4Lgm8cnwQsuAoYTeIL/ScPX04vmhN1 EkPNexYh5enkINLbPVdNIvfzZd91W7kjyTOCxGP52XVinid+0FvnzjCWB2/EhA== X-Gm-Gg: ASbGncsE1GuldXNzvmsjEH5Ry9dZoQ9kFjpPyByjCT/Mdvl37ZXH5raHKGdnyalseKS oDvc5jmESKbHgHa+y5OGxRJJwL8+xehX0DgiSMH5jOANRamICazsj07MmQK+dK7NFGT5VcnfYec NPu82lD/kxml+Ynl9/O5Sj6QEwIE24dl9YpLxX7t7O18oIo0RUoeN7NYj8HXnDa2kM8MNdge+OU Cq4F/0JvNt8MeaFKbp0xEbuHDEi/d9fhCtUGkTF+gE3prTpKkyZ9GXmo2VvbAhmSJqS/6aVwp8z qsRTUcTCL8SN7+vwWHLXFZGW17S2FZ7PqbQKswGDua9SpRtNxhaeRJgAdTCfXHk8avcFek9qHVl MqxrPyNH4Qz4XGLl7feY9wm0iH9oelJPd37uuMiiF+o71Ar1OoIF9NKDlNho1FQ== X-Google-Smtp-Source: AGHT+IG7GGWG+vOdvrR3OUlZ8DihhlUeopPc4FsJLRdYIqBYPodyrGIAAPy/B54IPSj3h4hc0qsd+w== X-Received: by 2002:a05:6512:1256:b0:55b:838c:d900 with SMTP id 2adb3069b0e04-55b838cdc70mr1621101e87.19.1753977535312; Thu, 31 Jul 2025 08:58:55 -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 v3 17/20] xen/riscv: implement put_page() Date: Thu, 31 Jul 2025 17:58:16 +0200 Message-ID: <7a7c07d431fc943e655216d389322638a640b2cd.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977694635116600 Content-Type: text/plain; charset="utf-8" Implement put_page(), as it will be used by p2m_put_code(). Although CONFIG_STATIC_MEMORY has not yet been introduced for RISC-V, a stub for PGC_static is added to avoid cluttering the code of put_page_nr() with #ifdefs. Signed-off-by: Oleksii Kurochko --- xen/arch/riscv/include/asm/mm.h | 7 +++++++ xen/arch/riscv/mm.c | 25 ++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index 7950d132c1..b914813e52 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -273,6 +273,13 @@ static inline bool arch_mfns_in_directmap(unsigned lon= g mfn, unsigned long nr) /* Page is Xen heap? */ #define _PGC_xen_heap PG_shift(2) #define PGC_xen_heap PG_mask(1, 2) +#ifdef CONFIG_STATIC_MEMORY +/* Page is static memory */ +#define _PGC_static PG_shift(3) +#define PGC_static PG_mask(1, 3) +#else +#define PGC_static 0 +#endif /* Page is broken? */ #define _PGC_broken PG_shift(7) #define PGC_broken PG_mask(1, 7) diff --git a/xen/arch/riscv/mm.c b/xen/arch/riscv/mm.c index 1ef015f179..3cac16f1b7 100644 --- a/xen/arch/riscv/mm.c +++ b/xen/arch/riscv/mm.c @@ -362,11 +362,6 @@ unsigned long __init calc_phys_offset(void) return phys_offset; } =20 -void put_page(struct page_info *page) -{ - BUG_ON("unimplemented"); -} - void arch_dump_shared_mem_info(void) { BUG_ON("unimplemented"); @@ -627,3 +622,23 @@ void flush_page_to_ram(unsigned long mfn, bool sync_ic= ache) if ( sync_icache ) invalidate_icache(); } + +void put_page(struct page_info *page) +{ + unsigned long nx, x, y =3D page->count_info; + + do { + ASSERT((y & PGC_count_mask) >=3D 1); + x =3D y; + nx =3D x - 1; + } + while ( unlikely((y =3D cmpxchg(&page->count_info, x, nx)) !=3D x) ); + + if ( unlikely((nx & PGC_count_mask) =3D=3D 0) ) + { + if ( unlikely(nx & PGC_static) ) + free_domstatic_page(page); + else + free_domheap_page(page); + } +} --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977765; cv=none; d=zohomail.com; s=zohoarc; b=hwHjs56qNm09o94WFF/gH9KFLMI6fEqria5HJhf6rhvv2yMBZbNpvz45dR8dbx0XNglRCfpgqtu30ImmFH7f21K3izrInrBtvx37UsiZ4O33W3ntQIBup5PD+Hg7j3m4aIxAT9ikCcnthktGmB2/KTumkfMEx+d6WuNX8VwWMJQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977765; 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=mxvRILGbsHos6OERRrUZU2kvL3C38QZb4m5E0yQHCno=; b=k4CiwYa7l7YoBY1U7pEUxA2G7XQ8zNN5MY3VKHULjs+/CcThlT0MrKYPnd5RpqDqA4fuOBk/RNRXu7syvxMfeCixWMaPEhuhOFkRjeuUYD8dqRbBWwVzMIrE1PR870Q7ntVdjCLvxIlvkCNevAeoZQ0623SWHeb6SSDKI74gvbQ= 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 1753977765835418.53355592131265; Thu, 31 Jul 2025 09:02:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065989.1431372 (Exim 4.92) (envelope-from ) id 1uhVjm-0004Gq-0N; Thu, 31 Jul 2025 16:02:34 +0000 Received: by outflank-mailman (output) from mailman id 1065989.1431372; Thu, 31 Jul 2025 16:02:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVjl-0004Gj-Tg; Thu, 31 Jul 2025 16:02:33 +0000 Received: by outflank-mailman (input) for mailman id 1065989; Thu, 31 Jul 2025 16:02:32 +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 1uhVgJ-0001FB-6w for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:58:59 +0000 Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [2a00:1450:4864:20::22e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 444282a6-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:57 +0200 (CEST) Received: by mail-lj1-x22e.google.com with SMTP id 38308e7fff4ca-3322f5413easo8495901fa.2 for ; Thu, 31 Jul 2025 08:58:57 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:56 -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: 444282a6-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977537; x=1754582337; 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=mxvRILGbsHos6OERRrUZU2kvL3C38QZb4m5E0yQHCno=; b=VEqZVkLbUl727cMdi4A+GZ9Pr+cB3NUnjnOBZVdArOOtKrFdcfCI6TjMKbNZMWIn90 GY7Tyaowat34eeRsdX0qPlld0iQo1T6B9hOi2g88lJS5UID9q01soAeVoYmJ8cJNNGH2 OaeRnt7R1hzTsQ6c5sNL/NuLX0oOxLt8y9/ZgRFa5+IV8Vc6TXvnQzcACdsUYMT9dPEe +bm5G5HU7vaWBWSm8goFztHabb35j0xU+8xHHV5CNIDyRCUqBKp0+Z74WW2PdPESnhqC U1dihwB0cUEp3LGT0UERIPkQrBivIfvmpVtuDGTb0opZdK7o2exdQ4VRRJBKtTVNGeuy dz/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977537; x=1754582337; 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=mxvRILGbsHos6OERRrUZU2kvL3C38QZb4m5E0yQHCno=; b=L21hXa0R9kP+eEKsV+VOMqd7M8KfaJF5MlJIU0FtOg7lTnQqiUoc6naLOcpf/vklAk J1Md/9azvTLAoElhaUtr9JfNQYH2XuIwUS9oy0QgtUf/X+vbyYMhLS6/oPeTx4XMkd1V yfRVPgdVB0Y4c29r/dbysL4a4PZBQLtJU3KokCtdtaN6VXIhkxskvSk3XNCZ4SWVpnuM kZ37yMrQ81rQKHSmtI6/u9kDI3lxrjx2BXiRoID7IgkOqD69f/Z0m8Wz7givZXv3nOYU EQDZV3M0Nzv9WMpFE4vpzsx/RS8WH+JahDmzq6Y0gsK/O9hVnbypXitqxstzMspX2Yi9 izzg== X-Gm-Message-State: AOJu0YyCfIOh3cbdEn40ObWCFcThLp092T5P7TK3COEZ7fl0EOJ7lAn7 Yxb1ZylmnTyQOP1qWBnXdu5UOc5v+V3bQLer3VmUGI3YZvI7UyB1GjnzLLBr4A== X-Gm-Gg: ASbGnctXTRxaN+iNm2liaWMYOQBTox5eDAsdq2K0v818W7terpP9TjiWesRxa3Xsxwg fQKudj9iV0j9wZeqeeJNSH4KJc/GbRmHxOdMkmzdb1fwqKEcJGjjGFWSWpTw6OkTNdg2zZwE6pC LwSjf3ZaZfIS8SOv2g8aiwEUsUnly7K8dcuYSeo+LATVZH0PzdG+xK7dKDQITaWlhQJ3fmp7QJD lLtHengDftyd/p8vnioT2VSPqWCugJSNqsL75bdQZOzz9Sv9ZaS1CPcm74ymnwJttHZp4/a3NF+ HZWjJqtbFrfq21eWj0C6IGoMpTKejgwIFkNUQ2M5/gUzQ9HQpeKIWooJjkTWOKCm2IQ3w3dMjv7 JJDKJ3fKemicm3Ksdxp3X8BlpU433hXE5Shvz+NNya0Z7egmPd+Yprow93wsht6SSniLENyEd X-Google-Smtp-Source: AGHT+IHTzoICYIk7JdW6+FqXoo6u269QOmtq3RTyi7yaQF2Oo3cJRSEqt7PGiiVjcKxqkcCoqHxRBQ== X-Received: by 2002:a05:6512:32ca:b0:55a:3358:a7d3 with SMTP id 2adb3069b0e04-55b7c0b71aamr2249462e87.54.1753977536809; Thu, 31 Jul 2025 08:58:56 -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 v3 18/20] xen/riscv: implement mfn_valid() and page reference, ownership handling helpers Date: Thu, 31 Jul 2025 17:58:17 +0200 Message-ID: X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977767859116600 Content-Type: text/plain; charset="utf-8" Implement the mfn_valid() macro to verify whether a given MFN is valid by checking that it falls within the range [start_page, max_page). These bounds are initialized based on the start and end addresses of RAM. As part of this patch, start_page is introduced and initialized with the PFN of the first RAM page. Also, initialize pdx_group_valid() by calling set_pdx_range() when memory banks are being mapped. Also, after providing a non-stub implementation of the mfn_valid() macro, the following compilation errors started to occur: riscv64-linux-gnu-ld: prelink.o: in function `__next_node': /build/xen/./include/xen/nodemask.h:202: undefined reference to `page_is_= ram_type' riscv64-linux-gnu-ld: prelink.o: in function `get_free_buddy': /build/xen/common/page_alloc.c:881: undefined reference to `page_is_ram_t= ype' riscv64-linux-gnu-ld: prelink.o: in function `alloc_heap_pages': /build/xen/common/page_alloc.c:1043: undefined reference to `page_get_own= er_and_reference' riscv64-linux-gnu-ld: /build/xen/common/page_alloc.c:1098: undefined refe= rence to `page_is_ram_type' riscv64-linux-gnu-ld: prelink.o: in function `ns16550_interrupt': /build/xen/drivers/char/ns16550.c:205: undefined reference to `get_page' riscv64-linux-gnu-ld: ./.xen-syms.0: hidden symbol `page_get_owner_and_re= ference' isn't defined riscv64-linux-gnu-ld: final link failed: bad value make[2]: *** [arch/riscv/Makefile:35: xen-syms] Error 1 To resolve these errors, the following functions have also been introduced, based on their Arm counterparts: - page_get_owner_and_reference() and its variant to safely acquire a reference to a page and retrieve its owner. - A stub for page_is_ram_type() that currently always returns 0 and asserts unreachable, as RAM type checking is not yet implemented. Signed-off-by: Oleksii Kurochko Acked-by: Jan Beulich --- Changes in V3: - Update defintion of mfn_valid(). - Use __ro_after_init for variable start_page. - Drop ASSERT_UNREACHABLE() in page_get_owner_and_nr_reference(). - Update the comment inside do/while in page_get_owner_and_nr_reference(). - Define _PGC_static and drop "#ifdef CONFIG_STATIC_MEMORY" in put_page_nr= (). - Initialize pdx_group_valid() by calling set_pdx_range() when memory bank= s are mapped. - Drop page_get_owner_and_nr_reference() and implement page_get_owner_and_= reference() without reusing of a page_get_owner_and_nr_reference() to avoid potentia= l dead code. - Move defintion of get_page() to "xen/riscv: add support of page lookup b= y GFN", where it is really used. --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/mm.h | 9 +++++++-- xen/arch/riscv/mm.c | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index b914813e52..d5be328906 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 @@ -309,8 +310,12 @@ static inline bool arch_mfns_in_directmap(unsigned lon= g mfn, unsigned long nr) #define page_get_owner(p) (p)->v.inuse.domain #define page_set_owner(p, d) ((p)->v.inuse.domain =3D (d)) =20 -/* TODO: implement */ -#define mfn_valid(mfn) ({ (void)(mfn); 0; }) +extern unsigned long start_page; + +#define mfn_valid(mfn) ({ \ + unsigned long mfn__ =3D mfn_x(mfn); \ + likely((mfn__ >=3D start_page)) && likely(__mfn_valid(mfn__)); \ +}) =20 #define domain_set_alloc_bitsize(d) ((void)(d)) #define domain_clamp_alloc_bitsize(d, b) ((void)(d), (b)) diff --git a/xen/arch/riscv/mm.c b/xen/arch/riscv/mm.c index 3cac16f1b7..3ad2b9cf93 100644 --- a/xen/arch/riscv/mm.c +++ b/xen/arch/riscv/mm.c @@ -521,6 +521,8 @@ static void __init setup_directmap_mappings(unsigned lo= ng base_mfn, #error setup_{directmap,frametable}_mapping() should be implemented for RV= _32 #endif =20 +unsigned long __ro_after_init start_page; + /* * Setup memory management * @@ -570,9 +572,13 @@ void __init setup_mm(void) ram_end =3D max(ram_end, bank_end); =20 setup_directmap_mappings(PFN_DOWN(bank_start), PFN_DOWN(bank_size)= ); + + set_pdx_range(paddr_to_pfn(bank_start), paddr_to_pfn(bank_end)); } =20 setup_frametable_mappings(ram_start, ram_end); + + start_page =3D PFN_DOWN(ram_start); max_page =3D PFN_DOWN(ram_end); } =20 @@ -642,3 +648,32 @@ void put_page(struct page_info *page) free_domheap_page(page); } } + +int page_is_ram_type(unsigned long mfn, unsigned long mem_type) +{ + ASSERT_UNREACHABLE(); + + return 0; +} + +struct domain *page_get_owner_and_reference(struct page_info *page) +{ + unsigned long x, y =3D page->count_info; + struct domain *owner; + + do { + x =3D y; + /* + * Count =3D=3D 0: Page is not allocated, so we cannot take a ref= erence. + * Count =3D=3D -1: Reference count would wrap, which is invalid. + */ + if ( unlikely(((x + 1) & PGC_count_mask) <=3D 1) ) + return NULL; + } + while ( (y =3D cmpxchg(&page->count_info, x, x + 1)) !=3D x ); + + owner =3D page_get_owner(page); + ASSERT(owner); + + return owner; +} --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977768; cv=none; d=zohomail.com; s=zohoarc; b=QXkWrST1oxb6373Tsxwz3on6lnPr2EUCVmZwI2B53U0ceHDd/FbrsKbYONa+DdWtU8bGeQbzA3Evv2RDANglds1XMo3Qzbw1I+NUNEki7Gj/bOjvtFDqMeWjRkblD8jlwko/dZAVCnPAz4rrtMll+ISjHeRsOJCPYXqRLYWapnI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977768; 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=mHdZIbsvWTRsRgXgstuZ4GoswoaWyGKYiWGLJB9LKTI=; b=ja+0Z7xF5mPBXGhyMh4lGvoSycdlPKgQOLB1EOB7e73Mz1wc2b8S4UbnWlJyhcR8ix6/7suJCQe+nR8smf2BrEEHI3bj8xLUiikRzz+keJzP7CpAoIkA0hwFblz0/gAVcWZ6YOMMWjVgoZJ27aoT8Ja68Rjyv3KL7pb1rXQOigk= 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 1753977767876573.3313496554462; Thu, 31 Jul 2025 09:02:47 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065991.1431382 (Exim 4.92) (envelope-from ) id 1uhVjo-0004YA-80; Thu, 31 Jul 2025 16:02:36 +0000 Received: by outflank-mailman (output) from mailman id 1065991.1431382; Thu, 31 Jul 2025 16:02:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVjo-0004Y0-49; Thu, 31 Jul 2025 16:02:36 +0000 Received: by outflank-mailman (input) for mailman id 1065991; Thu, 31 Jul 2025 16:02:34 +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 1uhVgK-0001FB-Q3 for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:59:00 +0000 Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [2a00:1450:4864:20::229]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 45197ecb-6e27-11f0-b895-0df219b8e170; Thu, 31 Jul 2025 17:58:59 +0200 (CEST) Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-32f3ef6178bso7952881fa.1 for ; Thu, 31 Jul 2025 08:58:59 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:57 -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: 45197ecb-6e27-11f0-b895-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977538; x=1754582338; 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=mHdZIbsvWTRsRgXgstuZ4GoswoaWyGKYiWGLJB9LKTI=; b=X300Aey748fyobD/lydRA1rPHmWpCLNk/cJdO9KA/v/4kUvk4CwgcBEJ3rPpB5MPiT oUVeChU6wiKa7bpvd3+S5BoeTJpn9ZFGgMCHEJPTus07N1mYckjXIzCz09FiCovnO4A8 jbe05iSRvq/YWhHCmAR1rdvUzaU/mKk+wFdsbjvpD1uNTmqOVkq+wn8AFJzJiub9zUfe rZrgfA2rbUSW0qnbxuS4jV+jfpQwIHUI4k4Qxof3J2vsBrchuDaibkZl4aoQm8r7mQpU yK5erXsN0PLXArKKvgmeUSxs1Hg8ySjWLjXEis60HmRcFIg2i773oFgYDETD4dEjkKJq +yRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977538; x=1754582338; 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=mHdZIbsvWTRsRgXgstuZ4GoswoaWyGKYiWGLJB9LKTI=; b=nl528yehfktMcs4ZJjQiX5hgYqAFAsmadwAygGbPTXI2SlWpNlLsA1du7xt2tT8dqa OQ598nmdrW2RC65S7ATkGNLUqMM72vZ+gM4MQTSxdMP4Mu5pQ3m6lPC7V+dSG2tC3ar8 zpq8Ge8zWeIChSfRLkzzUNlX5I1T7FGiHru1slHK3g5SF1a8d+66NRiVYGzgSaPJDD86 jvZc0RjBArIqzUKov46SWQ/J4Yxz84/Lf7ErFnr8szP62xmaJM6ZFcVKXp5QmeVrQJlI seYmxme9AYihLQY2HKqub5qL7/MgQTCUr97NedBPQFs7RdGbjQcULhg35kUEhyue+DsH t2HQ== X-Gm-Message-State: AOJu0YzFCjlgJAkfIYY5pnN0Y0su53hdcMfglqAOcL0JcC10dxTEgZGN teiYb8G4AgClgXIREGCRqFR65iqDOUAhRt4EjA+jLTXH6dkoNtTGe4e7RnKY+Q== X-Gm-Gg: ASbGnctODV1X+MiWBKG3anrycpw1NBDfpNOsI6z6Je8MljqOBynywJE7Bx3ZvQWAmLe HxY4VZCZ9QodwzH94O2ArrQVWKo4gi6ZVg478V8EDeHImFSHH+ZqBT+i0AuaS5c54FA6gm9RicJ x+AUPupqfVHlL/29xG5YUeKNXx7l4LVUNFg++pRxEcp5CMkMbOYfxo8qQaI/OdKXVztiTSGCE+E bT0ClECPCEhDBNDHLpDQEpYv97huRYRetp79AIxb6IKPiWLmIxTKN6VnILA1rf7Vo8dwcVIBAtE sG8GFQMmvEv9Xrt/AIGIJdVvJ0ihnaiNOORvZ0ANaG0eG+sOfxM72Noy5ev/AVqCfjwBsGJCQCF AARWxbjoK9/NFnYLDIcJ0ijnzuiJqhKs+VIfLpafayW3JcHhQ1sAhfsKi9nFsBA== X-Google-Smtp-Source: AGHT+IGlySXEHdEtSz8EaRKHSDBj85/VOFUIC/a5zK7apwJQgwQockzR3cbVXadBWIaLBzaJ6tlNkQ== X-Received: by 2002:a05:6512:4025:b0:553:2357:288c with SMTP id 2adb3069b0e04-55b7c025681mr3128805e87.17.1753977538163; Thu, 31 Jul 2025 08:58:58 -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 v3 19/20] xen/riscv: add support of page lookup by GFN Date: Thu, 31 Jul 2025 17:58:18 +0200 Message-ID: <24bb8ca6ad9d325f48d0c64b0fa461db5f0d0cc5.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1753977769772116600 Content-Type: text/plain; charset="utf-8" Introduce helper functions for safely querying the P2M (physical-to-machine) mapping: - add p2m_read_lock(), p2m_read_unlock(), and p2m_is_locked() for managing P2M lock state. - Implement p2m_get_entry() to retrieve mapping details for a given GFN, including MFN, page order, and validity. - Add p2m_lookup() to encapsulate read-locked MFN retrieval. - Introduce p2m_get_page_from_gfn() to convert a GFN into a page_info pointer, acquiring a reference to the page if valid. - Introduce get_page(). Implementations are based on Arm's functions with some minor modifications: - p2m_get_entry(): - Reverse traversal of page tables, as RISC-V uses the opposite level numbering compared to Arm. - Removed the return of p2m_access_t from p2m_get_entry() since mem_access_settings is not introduced for RISC-V. - Updated BUILD_BUG_ON() to check using the level 0 mask, which correspon= ds to Arm's THIRD_MASK. - Replaced open-coded bit shifts with the BIT() macro. - Other minor changes, such as using RISC-V-specific functions to validate P2M PTEs, and replacing Arm-specific GUEST_* macros with their RISC-V equivalents. Signed-off-by: Oleksii Kurochko --- Changes in V3: - Add is_p2m_foreign() macro and connected stuff. - Change struct domain *d argument of p2m_get_page_from_gfn() to struct p2m_domain. - Update the comment above p2m_get_entry(). - s/_t/p2mt for local variable in p2m_get_entry(). - Drop local variable addr in p2m_get_entry() and use gfn_to_gaddr(gfn) to define offsets array. - Code style fixes. - Update a check of rc code from p2m_next_level() in p2m_get_entry() and drop "else" case. - Do not call p2m_get_type() if p2m_get_entry()'s t argument is NULL. - Use struct p2m_domain instead of struct domain for p2m_lookup() and p2m_get_page_from_gfn(). - Move defintion of get_page() from "xen/riscv: implement mfn_valid() and = page reference, ownership handling helpers" --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/p2m.h | 18 ++++ xen/arch/riscv/mm.c | 13 +++ xen/arch/riscv/p2m.c | 136 +++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index fbc73448a7..dc3a77cc15 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -202,6 +202,24 @@ static inline int p2m_is_write_locked(struct p2m_domai= n *p2m) =20 unsigned long construct_hgatp(struct p2m_domain *p2m, uint16_t vmid); =20 +static inline void p2m_read_lock(struct p2m_domain *p2m) +{ + read_lock(&p2m->lock); +} + +static inline void p2m_read_unlock(struct p2m_domain *p2m) +{ + read_unlock(&p2m->lock); +} + +static inline int p2m_is_locked(struct p2m_domain *p2m) +{ + return rw_is_locked(&p2m->lock); +} + +struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, + p2m_type_t *t); + #endif /* ASM__RISCV__P2M_H */ =20 /* diff --git a/xen/arch/riscv/mm.c b/xen/arch/riscv/mm.c index 3ad2b9cf93..5e09d46a75 100644 --- a/xen/arch/riscv/mm.c +++ b/xen/arch/riscv/mm.c @@ -677,3 +677,16 @@ struct domain *page_get_owner_and_reference(struct pag= e_info *page) =20 return owner; } + +bool get_page(struct page_info *page, const struct domain *domain) +{ + const struct domain *owner =3D page_get_owner_and_reference(page); + + if ( likely(owner =3D=3D domain) ) + return true; + + if ( owner !=3D NULL ) + put_page(page); + + return false; +} diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index e9e6818da2..24a09d4537 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -852,3 +852,139 @@ int map_regions_p2mt(struct domain *d, { return p2m_insert_mapping(p2m_get_hostp2m(d), gfn, nr, mfn, p2mt); } + +/* + * Get the details of a given gfn. + * + * If the entry is present, the associated MFN will be returned type fille= d up. + * The page_order will correspond to the order of the mapping in the page + * table (i.e it could be a superpage). + * + * If the entry is not present, INVALID_MFN will be returned and the + * page_order will be set according to the order of the invalid range. + * + * valid will contain the value of bit[0] (e.g valid bit) of the + * entry. + */ +static mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, + p2m_type_t *t, + unsigned int *page_order, + bool *valid) +{ + unsigned int level =3D 0; + pte_t entry, *table; + int rc; + mfn_t mfn =3D INVALID_MFN; + DECLARE_OFFSETS(offsets, gfn_to_gaddr(gfn)); + + ASSERT(p2m_is_locked(p2m)); + BUILD_BUG_ON(XEN_PT_LEVEL_MAP_MASK(0) !=3D PAGE_MASK); + + if ( valid ) + *valid =3D false; + + /* XXX: Check if the mapping is lower than the mapped gfn */ + + /* This gfn is higher than the highest the p2m map currently holds */ + if ( gfn_x(gfn) > gfn_x(p2m->max_mapped_gfn) ) + { + for ( level =3D P2M_ROOT_LEVEL; level; level-- ) + if ( (gfn_x(gfn) & (XEN_PT_LEVEL_MASK(level) >> PAGE_SHIFT)) > + gfn_x(p2m->max_mapped_gfn) ) + break; + + goto out; + } + + table =3D p2m_get_root_pointer(p2m, gfn); + + /* + * the table should always be non-NULL because the gfn is below + * p2m->max_mapped_gfn and the root table pages are always present. + */ + if ( !table ) + { + ASSERT_UNREACHABLE(); + level =3D P2M_ROOT_LEVEL; + goto out; + } + + for ( level =3D P2M_ROOT_LEVEL; level; level-- ) + { + rc =3D p2m_next_level(p2m, true, level, &table, offsets[level]); + if ( (rc =3D=3D P2M_TABLE_MAP_NONE) || (rc =3D=3D P2M_TABLE_MAP_NO= MEM) ) + goto out_unmap; + + if ( rc !=3D P2M_TABLE_NORMAL ) + break; + } + + entry =3D table[offsets[level]]; + + if ( pte_is_valid(entry) ) + { + if ( t ) + *t =3D p2m_get_type(entry); + + mfn =3D pte_get_mfn(entry); + /* + * The entry may point to a superpage. Find the MFN associated + * to the GFN. + */ + mfn =3D mfn_add(mfn, + gfn_x(gfn) & (BIT(XEN_PT_LEVEL_ORDER(level), UL) - 1= )); + + if ( valid ) + *valid =3D pte_is_valid(entry); + } + + out_unmap: + unmap_domain_page(table); + + out: + if ( page_order ) + *page_order =3D XEN_PT_LEVEL_ORDER(level); + + return mfn; +} + +static mfn_t p2m_lookup(struct p2m_domain *p2m, gfn_t gfn, p2m_type_t *t) +{ + mfn_t mfn; + + p2m_read_lock(p2m); + mfn =3D p2m_get_entry(p2m, gfn, t, NULL, NULL); + p2m_read_unlock(p2m); + + return mfn; +} + +struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, + p2m_type_t *t) +{ + struct page_info *page; + p2m_type_t p2mt =3D p2m_invalid; + mfn_t mfn =3D p2m_lookup(p2m, gfn, t); + + if ( !mfn_valid(mfn) ) + return NULL; + + if ( t ) + p2mt =3D *t; + + page =3D mfn_to_page(mfn); + + /* + * get_page won't work on foreign mapping because the page doesn't + * belong to the current domain. + */ + if ( p2m_is_foreign(p2mt) ) + { + struct domain *fdom =3D page_get_owner_and_reference(page); + ASSERT(fdom !=3D NULL); + ASSERT(fdom !=3D p2m->domain); + return page; + } + + return get_page(page, p2m->domain) ? page : NULL; +} --=20 2.50.1 From nobody Thu Oct 30 18:55:07 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=1753977738; cv=none; d=zohomail.com; s=zohoarc; b=RtmzyBZUSCnXZbM2Mk9inj7v0Q4cBfVjs7kZoii7PBDEV514xMiJHh7G2FgNAGTCMjauE2Ks4QN9qePFVqk9gUt9n2/vflK290vP1i2983F4PBydDSCP+pGD9VcBZaKozLu0qUQRQlekafVlQ3Tlwtf9cVVUPJJQ/jtqKU+joms= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1753977738; 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=EX/IvnaSg+TBaVH3h6HU80ocSKilMokIToU2WpARmXk=; b=GNUDoqBjBYi7FRPT1UtI3+gZAd2z7mDf+kevBrjdaW2BSSNc7IUyRqoasQ8fJZutM85y5VDdh5ySq7vvG4Vq183FSLc3VgDwCO2OSWZRuvwRk3yPUTjAdk/b12E2Gb/5GtG/9wYBy21qHASQLrDXBtInkx2gLd/zZ+NIJlwoMZU= 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 1753977738199235.3215716393023; Thu, 31 Jul 2025 09:02:18 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1065970.1431352 (Exim 4.92) (envelope-from ) id 1uhVjI-0003AE-Cf; Thu, 31 Jul 2025 16:02:04 +0000 Received: by outflank-mailman (output) from mailman id 1065970.1431352; Thu, 31 Jul 2025 16:02:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVjI-0003A7-9t; Thu, 31 Jul 2025 16:02:04 +0000 Received: by outflank-mailman (input) for mailman id 1065970; Thu, 31 Jul 2025 16:02:03 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uhVgQ-0000zK-SU for xen-devel@lists.xenproject.org; Thu, 31 Jul 2025 15:59:06 +0000 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [2a00:1450:4864:20::135]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 46575398-6e27-11f0-a320-13f23c93f187; Thu, 31 Jul 2025 17:59:01 +0200 (CEST) Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-55b797ad392so1255519e87.3 for ; Thu, 31 Jul 2025 08:59:01 -0700 (PDT) Received: from fedora (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b88c9931bsm278746e87.101.2025.07.31.08.58.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Jul 2025 08:58:59 -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: 46575398-6e27-11f0-a320-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753977540; x=1754582340; 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=EX/IvnaSg+TBaVH3h6HU80ocSKilMokIToU2WpARmXk=; b=Qd+EFpX5xdyLZTZV45LcQXZQXq7urY1sfh932BYxpMFywyvP/RFckz9Kda/DAjh8p0 Z78hI+vuUQcrK9wQsHfrgXqZ4lWa0G1tz40BA9KTsl/ROOqyGXLVoyEdZcfujwmH1I41 649CXm+Rr2EhpMKiQh3cj2BsKR2N2G0H4jyEkmvrrJzUHEQPWKg6oXi6j/iSTCGjk/RD zr47CpIoZrmQJR7dlaJX+gwk/I2YKNo8g71K6itlHwawK1capjksFFGhfU4ZzDwzrHSs pIPA0VsFw5dNur+Qi4fKvd55fztoPZMp0yX7uj5IMVQxo6P4FqswvoDURAa1lKLUkE0r f/UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753977540; x=1754582340; 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=EX/IvnaSg+TBaVH3h6HU80ocSKilMokIToU2WpARmXk=; b=opO+0BaKaLPpB1MArIcjbwAiEom911R6MmX1Z/rz+q2OR8bG5U1I1tndqFu5Denuyl ODXjxhFUzoVoWZ2a0mVwEcy0LZr5hSBrBGmeSH8UbvpbM/mOnw/CHvqrSorv/gAqvqtD +wvscv7iGPy6gb8tMjmZpgWrA59QYUqHFXLSIxDb+t0jk22ZrsvgLuAa8kLFvnssdt3c CM7i9HRm3E9TtARb4vBp8nEkNA0dluDTek8l1nblCs+bElCo6PMccRsmWxp3zb0t/pjx WTm3lOOMsrdPUmnBbF3qYn7TNtMsN6nqw20jQu1Qo9Lu2EEeoXmGi8x50p38YuVMNR7l Ww8g== X-Gm-Message-State: AOJu0Ywv0Ln0TaRyqmsXWULxNK3knx1G1SV2jZv+jmTL3DZMVvwMi2B8 3+ngloWN2llCbTliEiB1duDfoofLJzmY8clx7/WkYnIOph1tOFEspQab7u98wg== X-Gm-Gg: ASbGncvSFKb2ux7ZyLl0iNmVtD/Wdklh1eODkBBjZKsgxmPxwInS/Vjn5i7b+zCOqaf Miv81Qqv/9neB3/lyhKDxYiWXvvFX7QFI4w1P6U1Gbe4BREy1gdpgc9/bbLh5HMhO2rCgStM6lE ZAQ2TewELM0Q9Q264/9mOqED3W9xxdcHHl/fN7vzCORvgTHryfIyETN1qGlVHsmiUmK45MqENuJ ZGsgaeQEVsgM545506fr5G3fIIbNisdXYlpC5gfbiw94nbayr4Qr9Ah4/Jr7qwd3cOQdhWeDWyP a6DGDenMlvwGdCJ6M9bm/SLIxebKNaF34ESHy8cs9V2nrmGKZIKoy5D5QAjRwrlAJg8voouW/x+ Kn5a9EXgxjy96164bj2bZ/eqVgfVt5aC6/tk6Oq3pXKlE9B6q4X04zfEbgWLD5lwXZlBXP/RK X-Google-Smtp-Source: AGHT+IF5zHEapqut2Mu0868zzfYS/rrg1qZ8aVIR8wjeA9Qh5d4+ZIRYdg18vbUfn9OCZVvedaco3w== X-Received: by 2002:a05:6512:3050:b0:55b:8a28:3cbc with SMTP id 2adb3069b0e04-55b8a283d8cmr810801e87.51.1753977540151; Thu, 31 Jul 2025 08:59:00 -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 v3 20/20] xen/riscv: introduce metadata table to store P2M type Date: Thu, 31 Jul 2025 17:58:19 +0200 Message-ID: <802d757c167121c0a5ae2e529f5a365f779c5e59.1753973161.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.50.1 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: 1753977739396116600 RISC-V's PTE has only two available bits that can be used to store the P2M type. This is insufficient to represent all the current RISC-V P2M types. Therefore, some P2M types must be stored outside the PTE bits. To address this, a metadata table is introduced to store P2M types that cannot fit in the PTE itself. Not all P2M types are stored in the metadata table=E2=80=94only those that require it. The metadata table is linked to the intermediate page table via the `struct page_info`'s list field of the corresponding intermediate page. To simplify the allocation and linking of intermediate and metadata page tables, `p2m_{alloc,free}_table()` functions are implemented. These changes impact `p2m_split_superpage()`, since when a superpage is split, it is necessary to update the metadata table of the new intermediate page table =E2=80=94 if the entry being split has its P2M type= set to `p2m_ext_storage` in its `P2M_TYPES` bits. In addition to updating the metadata of the new intermediate page table, the corresponding entry in the metadata for the original superpage is invalidated. Also, update p2m_{get,set}_type to work with P2M types which don't fit into PTE bits. Signed-off-by: Oleksii Kurochko --- Changes in V3: - Add is_p2m_foreign() macro and connected stuff. - Change struct domain *d argument of p2m_get_page_from_gfn() to struct p2m_domain. - Update the comment above p2m_get_entry(). - s/_t/p2mt for local variable in p2m_get_entry(). - Drop local variable addr in p2m_get_entry() and use gfn_to_gaddr(gfn) to define offsets array. - Code style fixes. - Update a check of rc code from p2m_next_level() in p2m_get_entry() and drop "else" case. - Do not call p2m_get_type() if p2m_get_entry()'s t argument is NULL. - Use struct p2m_domain instead of struct domain for p2m_lookup() and p2m_get_page_from_gfn(). - Move defintion of get_page() from "xen/riscv: implement mfn_valid() and = page reference, ownership handling helpers" --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/mm.h | 9 ++ xen/arch/riscv/p2m.c | 205 +++++++++++++++++++++++++------- 2 files changed, 170 insertions(+), 44 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index d5be328906..7cf0988f44 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -150,6 +150,15 @@ struct page_info /* Order-size of the free chunk this page is the head of. */ unsigned int order; } free; + + /* Page is used to store metadata: p2m type. */ + struct { + /* + * Pointer to a page which store metadata for an intermediate = page + * table. + */ + struct page_info *metadata; + } md; } v; =20 union { diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 24a09d4537..a909db654a 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -101,7 +101,16 @@ static int p2m_alloc_root_table(struct p2m_domain *p2m) { struct domain *d =3D p2m->domain; struct page_info *page; - const unsigned int nr_root_pages =3D P2M_ROOT_PAGES; + /* + * If the root page table starts at Level <=3D 2, and since only 1GB, = 2MB, + * and 4KB mappings are supported (as enforced by the ASSERT() in + * p2m_set_entry()), it is necessary to allocate P2M_ROOT_PAGES for + * the root page table itself, plus an additional P2M_ROOT_PAGES for + * metadata storage. This is because only two free bits are available = in + * the PTE, which are not sufficient to represent all possible P2M typ= es. + */ + const unsigned int nr_root_pages =3D P2M_ROOT_PAGES * + ((P2M_ROOT_LEVEL <=3D 2) ? 2 : 1); =20 /* * Return back nr_root_pages to assure the root table memory is also @@ -114,6 +123,23 @@ static int p2m_alloc_root_table(struct p2m_domain *p2m) if ( !page ) return -ENOMEM; =20 + if ( P2M_ROOT_LEVEL <=3D 2 ) + { + /* + * In the case where P2M_ROOT_LEVEL <=3D 2, it is necessary to all= ocate + * a page of the same size as that used for the root page table. + * Therefore, p2m_allocate_root() can be safely reused. + */ + struct page_info *metadata =3D p2m_allocate_root(d); + if ( !metadata ) + { + free_domheap_pages(page, P2M_ROOT_ORDER); + return -ENOMEM; + } + + page->v.md.metadata =3D metadata; + } + p2m->root =3D page; =20 return 0; @@ -198,24 +224,25 @@ 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) +static void p2m_set_type(pte_t *pte, const p2m_type_t t, const unsigned in= t i) { - int rc =3D 0; - if ( t > p2m_ext_storage ) - panic("unimplemeted\n"); + { + ASSERT(pte); + + pte[i].pte =3D t; + } else pte->pte |=3D MASK_INSR(t, P2M_TYPE_PTE_BITS_MASK); - - return rc; } =20 -static p2m_type_t p2m_get_type(const pte_t pte) +static p2m_type_t p2m_get_type(const pte_t pte, const pte_t *metadata, + const unsigned int i) { p2m_type_t type =3D MASK_EXTR(pte.pte, P2M_TYPE_PTE_BITS_MASK); =20 if ( type =3D=3D p2m_ext_storage ) - panic("unimplemented\n"); + type =3D metadata[i].pte; =20 return type; } @@ -265,7 +292,10 @@ static void p2m_set_permission(pte_t *e, p2m_type_t t) } } =20 -static pte_t p2m_pte_from_mfn(mfn_t mfn, p2m_type_t t, bool is_table) +static pte_t p2m_pte_from_mfn(mfn_t mfn, p2m_type_t t, + struct page_info *metadata_pg, + const unsigned int indx, + bool is_table) { pte_t e =3D (pte_t) { PTE_VALID }; =20 @@ -285,12 +315,21 @@ static pte_t p2m_pte_from_mfn(mfn_t mfn, p2m_type_t t= , bool is_table) =20 if ( !is_table ) { + pte_t *metadata =3D __map_domain_page(metadata_pg); + p2m_set_permission(&e, t); =20 + metadata[indx].pte =3D p2m_invalid; + if ( t < p2m_ext_storage ) - p2m_set_type(&e, t); + p2m_set_type(&e, t, indx); else - panic("unimplemeted\n"); + { + e.pte |=3D MASK_INSR(p2m_ext_storage, P2M_TYPE_PTE_BITS_MASK); + p2m_set_type(metadata, t, indx); + } + + unmap_domain_page(metadata); } else /* @@ -309,8 +348,10 @@ static pte_t page_to_p2m_table(struct page_info *page) * p2m_invalid will be ignored inside p2m_pte_from_mfn() as is_table is * set to true and p2m_type_t shouldn't be applied for PTEs which * describe an intermidiate table. + * That it also a reason why `metadata` and `indx` argument of + * p2m_pte_from_mfn() are NULL. */ - return p2m_pte_from_mfn(page_to_mfn(page), p2m_invalid, true); + return p2m_pte_from_mfn(page_to_mfn(page), p2m_invalid, NULL, 0, true); } =20 static struct page_info *p2m_alloc_page(struct p2m_domain *p2m) @@ -323,22 +364,71 @@ static struct page_info *p2m_alloc_page(struct p2m_do= main *p2m) return pg; } =20 +static void p2m_free_page(struct p2m_domain *p2m, struct page_info *pg); + +/* + * Allocate a page table with an additional extra page to store + * metadata for each entry of the page table. + * Link this metadata page to page table page's list field. + */ +static struct page_info * p2m_alloc_table(struct p2m_domain *p2m) +{ + enum table_type + { + INTERMEDIATE_TABLE=3D0, + /* + * At the moment, metadata is going to store P2M type + * for each PTE of page table. + */ + METADATA_TABLE, + TABLE_MAX + }; + + struct page_info *tables[TABLE_MAX]; + + for ( unsigned int i =3D 0; i < TABLE_MAX; i++ ) + { + tables[i] =3D p2m_alloc_page(p2m); + + if ( !tables[i] ) + goto out; + + clear_and_clean_page(tables[i]); + } + + tables[INTERMEDIATE_TABLE]->v.md.metadata =3D tables[METADATA_TABLE]; + + return tables[INTERMEDIATE_TABLE]; + + out: + for ( unsigned int i =3D 0; i < TABLE_MAX; i++ ) + if ( tables[i] ) + p2m_free_page(p2m, tables[i]); + + return NULL; +} + +/* + * Free page table's page and metadata page linked to page table's page. + */ +static void p2m_free_table(struct p2m_domain *p2m, struct page_info *tbl_p= g) +{ + ASSERT(tbl_pg->v.md.metadata); + + p2m_free_page(p2m, tbl_pg->v.md.metadata); + p2m_free_page(p2m, tbl_pg); +} + /* * Allocate a new page table page with an extra metadata page and hook it * in via the given entry. */ static int p2m_create_table(struct p2m_domain *p2m, pte_t *entry) { - struct page_info *page; + struct page_info *page =3D p2m_alloc_table(p2m); =20 ASSERT(!pte_is_valid(*entry)); =20 - page =3D p2m_alloc_page(p2m); - if ( page =3D=3D NULL ) - return -ENOMEM; - - clear_and_clean_page(page); - p2m_write_pte(entry, page_to_p2m_table(page), p2m->clean_pte); =20 return 0; @@ -453,10 +543,9 @@ static void p2m_put_2m_superpage(mfn_t mfn, p2m_type_t= type) } =20 /* Put any references on the page referenced by pte. */ -static void p2m_put_page(const pte_t pte, unsigned int level) +static void p2m_put_page(const pte_t pte, unsigned int level, p2m_type_t p= 2mt) { mfn_t mfn =3D pte_get_mfn(pte); - p2m_type_t p2m_type =3D p2m_get_type(pte); =20 ASSERT(pte_is_valid(pte)); =20 @@ -470,10 +559,10 @@ static void p2m_put_page(const pte_t pte, unsigned in= t level) switch ( level ) { case 1: - return p2m_put_2m_superpage(mfn, p2m_type); + return p2m_put_2m_superpage(mfn, p2mt); =20 case 0: - return p2m_put_4k_page(mfn, p2m_type); + return p2m_put_4k_page(mfn, p2mt); } } =20 @@ -486,10 +575,11 @@ static void p2m_free_page(struct p2m_domain *p2m, str= uct page_info *pg) =20 /* Free pte sub-tree behind an entry */ static void p2m_free_subtree(struct p2m_domain *p2m, - pte_t entry, unsigned int level) + pte_t entry, unsigned int level, + const pte_t *metadata, const unsigned int ind= ex) { unsigned int i; - pte_t *table; + pte_t *table, *tmp_metadata; mfn_t mfn; struct page_info *pg; =20 @@ -499,6 +589,8 @@ static void p2m_free_subtree(struct p2m_domain *p2m, =20 if ( pte_is_superpage(entry, level) || (level =3D=3D 0) ) { + p2m_type_t p2mt =3D p2m_get_type(entry, metadata, index); + #ifdef CONFIG_IOREQ_SERVER /* * If this gets called then either the entry was replaced by an en= try @@ -511,15 +603,21 @@ static void p2m_free_subtree(struct p2m_domain *p2m, ioreq_request_mapcache_invalidate(p2m->domain); #endif =20 - p2m_put_page(entry, level); + p2m_put_page(entry, level, p2mt); =20 return; } =20 - table =3D map_domain_page(pte_get_mfn(entry)); + mfn =3D pte_get_mfn(entry); + ASSERT(mfn_valid(mfn)); + table =3D map_domain_page(mfn); + pg =3D mfn_to_page(mfn); + tmp_metadata =3D __map_domain_page(pg->v.md.metadata); + for ( i =3D 0; i < XEN_PT_ENTRIES; i++ ) - p2m_free_subtree(p2m, table[i], level - 1); + p2m_free_subtree(p2m, table[i], level - 1, tmp_metadata, i); =20 + unmap_domain_page(tmp_metadata); unmap_domain_page(table); =20 /* @@ -530,23 +628,19 @@ static void p2m_free_subtree(struct p2m_domain *p2m, */ p2m_tlb_flush_sync(p2m); =20 - mfn =3D pte_get_mfn(entry); - ASSERT(mfn_valid(mfn)); - - pg =3D mfn_to_page(mfn); - - page_list_del(pg, &p2m->pages); - p2m_free_page(p2m, pg); + p2m_free_table(p2m, pg); } =20 static bool p2m_split_superpage(struct p2m_domain *p2m, pte_t *entry, unsigned int level, unsigned int target, - const unsigned int *offsets) + const unsigned int *offsets, + pte_t *metadata_tbl) { struct page_info *page; unsigned long i; pte_t pte, *table; bool rv =3D true; + pte_t *tmp_metadata_tbl; =20 /* Convenience aliases */ mfn_t mfn =3D pte_get_mfn(*entry); @@ -560,7 +654,7 @@ static bool p2m_split_superpage(struct p2m_domain *p2m,= pte_t *entry, ASSERT(level > target); ASSERT(pte_is_superpage(*entry, level)); =20 - page =3D p2m_alloc_page(p2m->domain); + page =3D p2m_alloc_table(p2m); if ( !page ) { /* @@ -572,6 +666,8 @@ static bool p2m_split_superpage(struct p2m_domain *p2m,= pte_t *entry, return false; } =20 + tmp_metadata_tbl =3D __map_domain_page(page->v.md.metadata); + table =3D __map_domain_page(page); =20 /* @@ -589,6 +685,9 @@ static bool p2m_split_superpage(struct p2m_domain *p2m,= pte_t *entry, pte =3D *entry; pte_set_mfn(&pte, mfn_add(mfn, i << level_order)); =20 + if ( MASK_EXTR(pte.pte, P2M_TYPE_PTE_BITS_MASK) =3D=3D p2m_ext_sto= rage ) + tmp_metadata_tbl[i] =3D metadata_tbl[offsets[level]]; + write_pte(new_entry, pte); } =20 @@ -600,7 +699,7 @@ static bool p2m_split_superpage(struct p2m_domain *p2m,= pte_t *entry, */ if ( next_level !=3D target ) rv =3D p2m_split_superpage(p2m, table + offsets[next_level], - level - 1, target, offsets); + level - 1, target, offsets, tmp_metadata_= tbl); =20 if ( p2m->clean_pte ) clean_dcache_va_range(table, PAGE_SIZE); @@ -612,6 +711,8 @@ static bool p2m_split_superpage(struct p2m_domain *p2m,= pte_t *entry, */ unmap_domain_page(table); =20 + unmap_domain_page(tmp_metadata_tbl); + /* * Even if we failed, we should (according to the current implemetation * of a way how sub-tree is freed if p2m_split_superpage hasn't been @@ -690,18 +791,23 @@ static int p2m_set_entry(struct p2m_domain *p2m, { /* We need to split the original page. */ pte_t split_pte =3D *entry; + struct page_info *metadata =3D virt_to_page(table)->v.md.metadata; + pte_t *metadata_tbl =3D __map_domain_page(metadata); =20 ASSERT(pte_is_superpage(*entry, level)); =20 - if ( !p2m_split_superpage(p2m, &split_pte, level, target, offsets)= ) + if ( !p2m_split_superpage(p2m, &split_pte, level, target, offsets, + metadata_tbl) ) { /* Free the allocated sub-tree */ - p2m_free_subtree(p2m, split_pte, level); + p2m_free_subtree(p2m, split_pte, level, metadata_tbl, offsets[= level]); =20 rc =3D -ENOMEM; goto out; } =20 + unmap_domain_page(metadata_tbl); + p2m_write_pte(entry, split_pte, p2m->clean_pte); =20 p2m->need_flush =3D true; @@ -734,7 +840,8 @@ static int p2m_set_entry(struct p2m_domain *p2m, p2m_clean_pte(entry, p2m->clean_pte); else { - pte_t pte =3D p2m_pte_from_mfn(mfn, t, false); + pte_t pte =3D p2m_pte_from_mfn(mfn, t, virt_to_page(table)->v.md.m= etadata, + offsets[level], false); =20 p2m_write_pte(entry, pte, p2m->clean_pte); =20 @@ -764,7 +871,12 @@ static int p2m_set_entry(struct p2m_domain *p2m, */ if ( pte_is_valid(orig_pte) && !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) ) - p2m_free_subtree(p2m, orig_pte, level); + { + struct page_info *metadata =3D virt_to_page(table)->v.md.metadata; + pte_t *metadata_tbl =3D __map_domain_page(metadata); + p2m_free_subtree(p2m, orig_pte, level, metadata_tbl, offsets[level= ]); + unmap_domain_page(metadata_tbl); + } =20 out: unmap_domain_page(table); @@ -924,7 +1036,12 @@ static mfn_t p2m_get_entry(struct p2m_domain *p2m, gf= n_t gfn, if ( pte_is_valid(entry) ) { if ( t ) - *t =3D p2m_get_type(entry); + { + struct page_info *metadata_pg =3D virt_to_page(table)->v.md.me= tadata; + pte_t *metadata =3D __map_domain_page(metadata_pg); + *t =3D p2m_get_type(entry, metadata, offsets[level]); + unmap_domain_page(metadata); + } =20 mfn =3D pte_get_mfn(entry); /* --=20 2.50.1