From patchwork Mon Jan 20 16:54:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksii Kurochko X-Patchwork-Id: 13945282 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E5DD3C02185 for ; Mon, 20 Jan 2025 16:55:08 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.875027.1285367 (Exim 4.92) (envelope-from ) id 1tZv3B-0005tW-M8; Mon, 20 Jan 2025 16:54:57 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 875027.1285367; Mon, 20 Jan 2025 16:54:57 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tZv3B-0005tP-JF; Mon, 20 Jan 2025 16:54:57 +0000 Received: by outflank-mailman (input) for mailman id 875027; Mon, 20 Jan 2025 16:54:56 +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 1tZv3A-0005fS-LH for xen-devel@lists.xenproject.org; Mon, 20 Jan 2025 16:54:56 +0000 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [2a00:1450:4864:20::231]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 466665e7-d74f-11ef-a0e4-8be0dac302b0; Mon, 20 Jan 2025 17:54:55 +0100 (CET) Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-30219437e63so57884471fa.1 for ; Mon, 20 Jan 2025 08:54:55 -0800 (PST) Received: from fedora.. ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-3072a35aec4sm16581501fa.56.2025.01.20.08.54.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 08:54:54 -0800 (PST) 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: 466665e7-d74f-11ef-a0e4-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737392095; x=1737996895; 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=QAci7/y9aPonTonAxMRXFGNKbw9P1cakqYuBwvG636Q=; b=Yx9aellsnkuED7G5txVPAKyxUjdWwSBSaeF06qlWva4/2lnAFmigc/FlGtYvBOxyHw 9XMJZoRJ2d/JsfVACxOxrucZ1oCIwNremymLKUrzSMVpaJlPbh8aWO/BNmhcdRHc65Uk 94PNC+kkq+h1SjPPG9/7dPHDVXGa4+EsEcES9B0Pqr/6BzAoiWrpxcGxORVjksDqy0kp qYeo5/oxLlCm1j9Yb9NAupKhYY2KKiZTD8u+4g7ICdr9g3KS8W0M03+OdvbUmJKCc+2u UdHsm40wFyinIt05zVK+iDuh2/Spv61Y/gIRdHhJq9Lr28JmF44c5TydfIlAo7voeKoH Vj6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737392095; x=1737996895; 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=QAci7/y9aPonTonAxMRXFGNKbw9P1cakqYuBwvG636Q=; b=YK920muqcRi+GRta8R6NAEFvATV4WyYzLEuMO6IgzNtnODP1pGxl/rFkTbePMlaiJQ SIQ2w9OZGzFW6s1pRwVzlrthBAD29YOcG1QnTXdCMwIFVQokJV6CfX/0LEtV0fn+NOHV aTTKreWI0LD9Jg/U/qA0A4fg6QvoWUKGUewfwxhmLbwzN3+b2nFYCxy9L8c2zsImfcAk AeCJOsynAnk8O2eIvqgAR420OkW9ipgcopnpaB4aF31hL4ZBPSBCCl1t0JjGXGTE2RN2 m96SPEGjkW28ytvNA1Lnvw0SgA/VUj3VCpiNABQiXt3pAZ8+/KuZp8oVnuVZxlo3UEfY 2RFA== X-Gm-Message-State: AOJu0Yyuct7Qdt5Otv3rcK+cDv0wMqTTRRNPMPfSeX8jbYm9PmSSCuKv nEACggM0sNkZ4eIDUWqRE+3Ksg7VMeDomdxF4LAO35JK/mn/XSfkeyamCw== X-Gm-Gg: ASbGnct1zBqavo2E7AyfxhA4jTf7Hk8h0nTXqr7c2BAQQPtMlVXecZZkHOl7/npVVSr opMLMw7uEhKM5zdAQG5gVwfEF8G/o0OKZvHLcxHENJ6mMEu6TjnXW195QKwmyxboGR7q9TzO1Se T3FYxzux/ivISa0eokJyOdCXAtTMXb2CIzEpgVea5iBI6rHMq0HbxWZAVspZJsAOS4e9rQsvsFQ hKAY5w7tMjLjEU39bnsZkSkM3sQ4ToNiIAmR4EiIQe0bRsVvNMIVNN378+GfNbAxeJkNis= X-Google-Smtp-Source: AGHT+IFz5Cwm7/8fr/VJmijwGV+DRnd1DmMRxxqBYFi+G0+4wzYO6HCvGfCX2bJrTTWWBIyVVv0Mqw== X-Received: by 2002:a2e:a912:0:b0:306:d69:904b with SMTP id 38308e7fff4ca-3072d11346dmr48975511fa.8.1737392094574; Mon, 20 Jan 2025 08:54:54 -0800 (PST) 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_Pau_Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v1 1/3] xen/riscv: implement software page table walking Date: Mon, 20 Jan 2025 17:54:47 +0100 Message-ID: <00dfc71569bc9971b53e29b36a80e9e020ac61ac.1737391102.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: MIME-Version: 1.0 RISC-V doesn't have hardware feature to ask MMU to translate virtual address to physical address ( like Arm has, for example ), so software page table walking in implemented. Signed-off-by: Oleksii Kurochko --- xen/arch/riscv/include/asm/mm.h | 2 ++ xen/arch/riscv/pt.c | 56 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/mm.h index 292aa48fc1..d46018c132 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -15,6 +15,8 @@ extern vaddr_t directmap_virt_start; +paddr_t pt_walk(vaddr_t va); + #define pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT) #define paddr_to_pfn(pa) ((unsigned long)((pa) >> PAGE_SHIFT)) diff --git a/xen/arch/riscv/pt.c b/xen/arch/riscv/pt.c index a703e0f1bd..865d60d1af 100644 --- a/xen/arch/riscv/pt.c +++ b/xen/arch/riscv/pt.c @@ -274,6 +274,62 @@ static int pt_update_entry(mfn_t root, vaddr_t virt, return rc; } +paddr_t pt_walk(vaddr_t va) +{ + const mfn_t root = get_root_page(); + /* + * In pt_walk() only XEN_TALE_MAP_NONE and XEN_TABLE_SUPER_PAGE are + * handled ( as they are only possible for page table walking ), so + * initialize `ret` with "impossible" XEN_TABLE_MAP_NOMEM. + */ + int ret = XEN_TABLE_MAP_NOMEM; + unsigned int level = HYP_PT_ROOT_LEVEL; + paddr_t pa = 0; + pte_t *table; + + DECLARE_OFFSETS(offsets, va); + + table = map_table(root); + + /* + * Find `pa` of an entry which corresponds to `va` by iterating for each + * page level and checking if the entry points to a next page table or + * to a page. + * + * Two cases are possible: + * - ret == XEN_TABLE_SUPER_PAGE means that the entry was find; + * (Despite of the name) XEN_TABLE_SUPER_PAGE covers 4k mapping too. + * - ret == XEN_TABLE_MAP_NONE means that requested `va` wasn't actually + * mapped. + */ + while ( (ret != XEN_TABLE_MAP_NONE) && (ret != XEN_TABLE_SUPER_PAGE) ) + { + /* + * This case shouldn't really occur as it will mean that for table + * level 0 a pointer to next page table has been written, but at + * level 0 it could be only a pointer to 4k page. + */ + ASSERT(level <= HYP_PT_ROOT_LEVEL); + + ret = pt_next_level(false, &table, offsets[level]); + level--; + } + + if ( ret == XEN_TABLE_MAP_NONE ) + dprintk(XENLOG_WARNING, "Is va(%#lx) really mapped?\n", va); + else if ( ret == XEN_TABLE_SUPER_PAGE ) + pa = pte_to_paddr(*(table + offsets[level + 1])); + + /* + * There is no need for unmap_table() after each pt_next_level() call as + * pt_next_level() will do unmap_table() for the previous table before + * returning next level table. + */ + unmap_table(table); + + return pa; +} + /* Return the level where mapping should be done */ static int pt_mapping_level(unsigned long vfn, mfn_t mfn, unsigned long nr, unsigned int flags) From patchwork Mon Jan 20 16:54:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksii Kurochko X-Patchwork-Id: 13945283 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A4C46C02182 for ; Mon, 20 Jan 2025 16:55:09 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.875028.1285378 (Exim 4.92) (envelope-from ) id 1tZv3D-000685-0u; Mon, 20 Jan 2025 16:54:59 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 875028.1285378; Mon, 20 Jan 2025 16:54:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tZv3C-00067w-Tr; Mon, 20 Jan 2025 16:54:58 +0000 Received: by outflank-mailman (input) for mailman id 875028; Mon, 20 Jan 2025 16:54:58 +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 1tZv3C-0005fM-2d for xen-devel@lists.xenproject.org; Mon, 20 Jan 2025 16:54:58 +0000 Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [2a00:1450:4864:20::22b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 46f996f7-d74f-11ef-99a4-01e77a169b0f; Mon, 20 Jan 2025 17:54:56 +0100 (CET) Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-305d843d925so40162631fa.2 for ; Mon, 20 Jan 2025 08:54:56 -0800 (PST) Received: from fedora.. ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-3072a35aec4sm16581501fa.56.2025.01.20.08.54.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 08:54:55 -0800 (PST) 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: 46f996f7-d74f-11ef-99a4-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737392096; x=1737996896; 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=UXq8oJNFxqFqbhkJfspIF9HTbKUSA1pwy167cwYkY18=; b=SoxvpOZwRY0kgsrDtA96pC4HCJtvIOkWZ8+bnldF/K3FhoE9FlsNB4kFhfiCvacE9R ILKFgGvidmEk+JcAYB9tTP/jGw1p0Ss7/N/nWvAVwcLPdwsGQLI/hIZR/7gy3dSHUle4 5GqB3zKXrbXle0lKbNBpYp0iblyJIC3ybvfx0IkktLhIoJp4hHQjiTq6gW+7pPRVetyp iS+VkLBV7wbLJ8rqBQCZvqWcp/9iQSNo02uqkcHpHWWFoDmxoy6+7DSsoVXgtyIPWXy7 vmrvxTMXtCbntnqlJclglGreSu0isaaLxHjf0iBwhF/Edu3GTDtmrbQmjNKOTZZqXm8s zguA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737392096; x=1737996896; 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=UXq8oJNFxqFqbhkJfspIF9HTbKUSA1pwy167cwYkY18=; b=t9TX38jjqHdol/bU1VMp7tPgS8AdVThDDOo0mc95JMfWoAc9h6QrY9TAFjHSr5XWRX yRer7Rdkkp0V0CZ/zLTz38E0k8Dl73PiIv1edg7lI0Mct1nCZgXzrjt+8L7gw1P+865q agiAS1IzIPxfvukByYjQTuhfl51E+EVwzO9H5cSiBHIVIkmxWvVob27aRaImK2m12t7+ ZWtH7FDy10A9wv8iiwPge5cGS1y4PDfL4zWbd3MOivMI0O03Bzp+C/4D1qfsOpC8J8+p t98qM3xOP2b4e0p/NByYgnlvHAarLJ8cXTul8AjJoYE2QheXlaelrm1JSlHLIur7QS/y A9bw== X-Gm-Message-State: AOJu0YwOLUamRN5RneR+PX6FohU4LKh+S3cI9UnvDOQF7ozsvzbNSbZt yNU7lC3Oop3NavzKDH0p9IDX9HAmQK36O0huJxk4S2/BK8Klij2p/2YAcQ== X-Gm-Gg: ASbGncsn13tHXRIvuweRx0k1YnCzhnAnJk7UY/skR43PCig36OGHlGAw6Ao5yDhMJze mFe+Wlugoo/BIXBn8I/DhwfBPcsdfdl6jd7gGi/4y6kPtHvmMny9owCyltuuD6vZUQcArkuG0eV Dy++UezcAGuguUWcxuDc6yYcpIRjJUGwBuErgqOSLs0GK72ZVyJQ2b22ulRS9geaGX58fAuoqk6 snyuscNuFBV+cbzt5unrtATcPQvujhCskAChA6Q1ZUtBw71uArYCpEqDaRh8SvaEXIrMh8= X-Google-Smtp-Source: AGHT+IG+yeo267gkP4ELBuOOmKSTVwh7M1OX0XAwHuYDvtkxZzwBI37tWNfMYNAWGF0DAjQMih51bA== X-Received: by 2002:a2e:a54a:0:b0:300:3a15:8f19 with SMTP id 38308e7fff4ca-3072cb3c7a9mr56218221fa.32.1737392095702; Mon, 20 Jan 2025 08:54:55 -0800 (PST) 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_Pau_Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v1 2/3] xen/riscv: update defintion of vmap_to_mfn() Date: Mon, 20 Jan 2025 17:54:48 +0100 Message-ID: X-Mailer: git-send-email 2.48.1 In-Reply-To: References: MIME-Version: 1.0 vmap_to_mfn() uses virt_to_maddr(), which is designed to work with VA from either the direct map region or Xen's linkage region (XEN_VIRT_START). An assertion will occur if it is used with other regions, in particular for the VMAP region. Since RISC-V lacks a hardware feature to request the MMU to translate a VA to a PA (as Arm does, for example), software page table walking (pt_walk()) is used for the VMAP region to obtain the PA. Fixes: 7db8d2bd9b ("xen/riscv: add minimal stuff to mm.h to build full Xen") Signed-off-by: Oleksii Kurochko --- xen/arch/riscv/include/asm/mm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/mm.h index d46018c132..528e1e257f 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -25,7 +25,7 @@ paddr_t pt_walk(vaddr_t va); #define gaddr_to_gfn(ga) _gfn(paddr_to_pfn(ga)) #define mfn_to_maddr(mfn) pfn_to_paddr(mfn_x(mfn)) #define maddr_to_mfn(ma) _mfn(paddr_to_pfn(ma)) -#define vmap_to_mfn(va) maddr_to_mfn(virt_to_maddr((vaddr_t)(va))) +#define vmap_to_mfn(va) maddr_to_mfn(pt_walk((vaddr_t)(va))) #define vmap_to_page(va) mfn_to_page(vmap_to_mfn(va)) static inline void *maddr_to_virt(paddr_t ma) From patchwork Mon Jan 20 16:54:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksii Kurochko X-Patchwork-Id: 13945284 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EFDB8C0218B for ; Mon, 20 Jan 2025 16:55:09 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.875029.1285388 (Exim 4.92) (envelope-from ) id 1tZv3F-0006Oy-8b; Mon, 20 Jan 2025 16:55:01 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 875029.1285388; Mon, 20 Jan 2025 16:55:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tZv3F-0006On-5Z; Mon, 20 Jan 2025 16:55:01 +0000 Received: by outflank-mailman (input) for mailman id 875029; Mon, 20 Jan 2025 16:54:59 +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 1tZv3D-0005fM-4Z for xen-devel@lists.xenproject.org; Mon, 20 Jan 2025 16:54:59 +0000 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [2a00:1450:4864:20::233]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 478ffdb4-d74f-11ef-99a4-01e77a169b0f; Mon, 20 Jan 2025 17:54:57 +0100 (CET) Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-3003c0c43c0so50073791fa.1 for ; Mon, 20 Jan 2025 08:54:57 -0800 (PST) Received: from fedora.. ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-3072a35aec4sm16581501fa.56.2025.01.20.08.54.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2025 08:54:56 -0800 (PST) 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: 478ffdb4-d74f-11ef-99a4-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737392097; x=1737996897; 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=eKyZpIY7f8FG3QEkOJcyw71ibKhausbsni6UpIbgyKc=; b=AU+DWBbSlZyR+59P4cd4Kl82WOplfekxfVnXeZHtTKsNooQTeQmh/ShlQbI+mNc03S kIcwl28JbWoHaa8DrdjC0yKCqBT1hvoVAwVfN7AAdBMxgTYeeF246haaBxHl1GpoJYE7 BNvSZE2hCw1ge7NSLbJdFpO++OXN3p9AJ9e66uyldMzpWUyeP6WFigz10aBoPJyihdBO FN2ODuptdIyD+aUyuwcU8scoB+0Tat/B0bD+0LNafIXSN8MphpFDx0QId3tQdqytT7nZ 6J8cNeUwHmTj2JObavgig6WRJH5jYHRKnHFYyef5LalOTsEuDLktB0xmhqs8glOPPg/X ZH4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737392097; x=1737996897; 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=eKyZpIY7f8FG3QEkOJcyw71ibKhausbsni6UpIbgyKc=; b=LnI1FWux9JLTpaRblI9GcbZOm4NvtV6apfOuqKhUl/TSVpO3MTSxl6lQQ7hMJBpm2N Zy7IZ/v3RupwIGbVmQlhwF1i/xm5oIFPUurC93/dOXTia8KxAPlLpGsgxfLyuMMnudUx RY1+86P9IXc5asIT9i2ql5W7sl8x5ndQOsS2bYjP4vU+MkRmmgmXQhHsDgwlQ4Rzobnc e+AC0uOx4Xc6a/1WGKr95Wf/O2ZtgFOO20AJGp1B74Ig7FANHfwO5KkcvzVydEE59mgl teeqxFKz3Z6W7DfTzTizFbIIzaYt0V/crvPMtNE8I4w+HjE8chJwANTEyvKKR2tQyuby TcnA== X-Gm-Message-State: AOJu0YxX4vYQbo9qJ6v7xDz0IwT606Vbly+QX7G0Jg6+vJe7D8eYiYOW 5cAKJQG9TSRUqBXQ/F9uOWVscGRi3vqnwTThtPWlPn+KY17xD22sodhnQg== X-Gm-Gg: ASbGnctT5JrLnY8wubiQzLC6SthtEa4KdIbPP/RAvRM5fGJArw5Sl5V0AmRUjEiH00y hQEOKnABom6fz+h9SRq6pjhp+OPzESEn0YkuMwffGMMYK5RvuULh3zIR9Z5n2+LLBfO/7LwLbNf uY09RzeLKETDnHskhQZXXpBQ9WpAq8q8X3g8kmmhHdR/0QOUxgHQTUxUx/e9Qqr7weODzE7NfGV XEuzHmL45ISOmzLN4Rf82Wh4Sh87425TIBQ2wFHZZr0Q9lG24CY+9hJoYg/aqBr2tLKINw= X-Google-Smtp-Source: AGHT+IH/viGUbRyRw/B7XR/62gU1OPKwbDr47PSryCh5/zBWHLDg91Hg7oKnyLuGqHaJ/yivTGWjHQ== X-Received: by 2002:a05:651c:b27:b0:2ff:c86b:5b4f with SMTP id 38308e7fff4ca-3072ca9c953mr52322211fa.21.1737392096663; Mon, 20 Jan 2025 08:54:56 -0800 (PST) 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_Pau_Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v1 3/3] xen/riscv: update mfn calculation in pt_mapping_level() Date: Mon, 20 Jan 2025 17:54:49 +0100 Message-ID: X-Mailer: git-send-email 2.48.1 In-Reply-To: References: MIME-Version: 1.0 When pt_update() is called with arguments (..., INVALID_MFN, ..., 0 or 1), it indicates that a mapping is being destroyed/modifyed. `mfn` should be set properly in cases when modifying/destroying a mapping to ensure the correct page table `level` is returned. In the case of `mfn` == INVALID_MFN, the `mask` will take into account only `vfn` and could accidentally return an incorrect level. For example, if `vfn` is page table level 1 aligned, but it was mapped as page table level 0, then without the check below it will return `level` = 1 because only `vfn`, which is page table level 1 aligned, is taken into account when `mfn` == INVALID_MFN. To address this issue, during the destruction/modification of a mapping, physical address is calculated for provided `va`. This ensures that the appropriate mask is generated, resulting in the correct calculation of the level. Fixes: c2f1ded524 ("xen/riscv: page table handling") Signed-off-by: Oleksii Kurochko --- xen/arch/riscv/pt.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/xen/arch/riscv/pt.c b/xen/arch/riscv/pt.c index 865d60d1af..c8bc6f7e37 100644 --- a/xen/arch/riscv/pt.c +++ b/xen/arch/riscv/pt.c @@ -346,9 +346,33 @@ static int pt_mapping_level(unsigned long vfn, mfn_t mfn, unsigned long nr, return level; /* - * Don't take into account the MFN when removing mapping (i.e - * MFN_INVALID) to calculate the correct target order. + * `mfn` should be set properly in cases when modifying/destroying a + * mapping to ensure the correct page table `level` is received. In the + * case of `mfn` == INVALID_MFN, the `mask` will take into account only + * `vfn` and could accidentally return an incorrect level. For example, + * if `vfn` is page table level 1 aligned, but it was mapped as page table + * level 0, then without the check below it will return `level` = 1 + * because only `vfn`, which is page table level 1 aligned, is taken into + * account when `mfn` == INVALID_MFN. * + * POPULATE shouldn't be considered as `va` hasn't been mapped yet. + */ + if ( mfn_eq(mfn, INVALID_MFN) && !(flags & PTE_POPULATE) ) + { + vaddr_t va = vfn << PAGE_SHIFT; + paddr_t pa; + unsigned long xen_virt_end = (XEN_VIRT_START + XEN_VIRT_SIZE - 1); + + if ( ((va >= DIRECTMAP_VIRT_START) && (va <= DIRECTMAP_VIRT_END)) || + ((va >= XEN_VIRT_START) && (va <= xen_virt_end)) ) + pa = virt_to_maddr(va); + else + pa = pt_walk(va); + + mfn = _mfn(paddr_to_pfn(pa)); + } + + /* * `vfn` and `mfn` must be both superpage aligned. * They are or-ed together and then checked against the size of * each level.