From nobody Fri Oct 31 03:56:15 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=1749560755; cv=none; d=zohomail.com; s=zohoarc; b=A1FmoHudUNwQm/RrbgyTI4ZzrLLHGaNyYLPUDF4Ala8OGNjqBeeEbLqpQELw+sJWf3b1yXvhI7k4N1n/pVNQ6EirJepCiWWee7DcFdboh/qlm97o9nMxWnd43nEGfW9zJLi3wTC2EqWwOcqwkSeZgLW+nHwvhBj4oWMB3DXbg2A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560755; 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=MQZA1jhqfJhA3+R1Io64wtcp1umLfxpBKyDauABhC/w=; b=jt8nzM0+MwxOFnKJaiQoP6WVdOKZSg0U0eaykSEbJzYWsgZMQTQ4/PPz/wyQanTC8/G8JN4MbgPmrxIZHQhLpkYLhZA6fYwKZyK6t6oTjgu9dLgIGRARaQCjDzJuwc+XkPyPMrzhwggaaOAX2mkjhQ7n+8YYWUp9SeVZWAQNJF4= 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 1749560755536785.1939471521234; Tue, 10 Jun 2025 06:05:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010783.1388989 (Exim 4.92) (envelope-from ) id 1uOyff-0004y4-8F; Tue, 10 Jun 2025 13:05:43 +0000 Received: by outflank-mailman (output) from mailman id 1010783.1388989; Tue, 10 Jun 2025 13:05:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyff-0004xp-3l; Tue, 10 Jun 2025 13:05:43 +0000 Received: by outflank-mailman (input) for mailman id 1010783; Tue, 10 Jun 2025 13:05:42 +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 1uOyfe-0004iH-3H for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:42 +0000 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [2a00:1450:4864:20::635]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9c9454f3-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:41 +0200 (CEST) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-ad891bb0957so885512666b.3 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: 9c9454f3-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560741; x=1750165541; 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=MQZA1jhqfJhA3+R1Io64wtcp1umLfxpBKyDauABhC/w=; b=hqsK6wcf3uNnk+qcB8JhRNx37mrZrDxuAx8GEne1YIIhAC4WM9DovmlBiMiKHqkbSY CyRIjXHrnwzbIMhahCaFVQiBIXem1v7xwplLRQthOk7saho/8kKC5B6q8yo7koJxXhxl wbhmY/0GLJUpklk0eDlUD008py5TF9AI0YinoH/n81PKP/iFn5PRByjbPAcvQ4CK92Es KSGAkGzwd8NOAs0tQqzmzCkZOPYahgxaj7/XSJDySgBQCiysWlcrCr7dGWiWEIG12+E3 9mSFD+h/zFZTm2eBlOyRQUiwprPAqD+E7RA8nUXQ7HqYnv/W4vUAP1i+yV3R+5fxS/oy Ph7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560741; x=1750165541; 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=MQZA1jhqfJhA3+R1Io64wtcp1umLfxpBKyDauABhC/w=; b=hHV0qSGhhD6T34IfYvLgxqxNrrZO901JriMNg7ZRULD33B60EtDJbRxeTqBkaVrudx IIiSID/bF7ENoM2xQifBqPoZ7VRFa+WtJ2qqfGLCu+68YY0WoVlNpkyrCflSolqhAkfd QOX+p37ZUXFjuo063ptVBEYH/ZzMBAGh5H6qxsLj7ab75yK/H/jqimDCW5PkHkfBWbIC yDSNS6A4BlxhLW7EbzZhlA1Iin1Pn18Y0tjSq6uPZJ21PzP0Cmi85C5BMR9T2aSsajkf WqOhMEhTxZ3xFkJpbYj0EI5+6iQkosGa3TYuIvFMph1MqV3YCS7Pdg+B5e/Q/SxOm7bI OPZA== X-Gm-Message-State: AOJu0Yzz+mrz9ukwOtJozRu6BMQj3xBuMm2uUmv+4JutnItgrz/pDdTq ug7Ri9vMNoplovv90Trnh26my53fKT7JIF49JBqAVAdViL1Kby00+I1nMz8g/A== X-Gm-Gg: ASbGncs/+XaH6nbxMqYRPqbxzQFys0owwA+SgtSqZKthbErOV/C4D8ybKs1JT8LEHdH tJ8sqEHLlW6fN/Uiv5IAFVdeTDbyRPzyZmoqQGp+lf0QnHAAiRBMyLdkd/604uLnFx0CsI18f29 +SS4mTmNaRNTuIcu58Ar/jCO4rHGj9SKCFClXvAx57tQfGC/WFe6BO6+gsQChQkH9gJyptrbtsC i/vrRAeePIUUv05VylNu/EiKDC/LlJdK8mAtqxo2Fwz6cXxB/hXAeYZ4bcLHcDVBAaPNS6GjfIW cKhvcxpq4zMgercV97/Z9SWP44AGG+zUFS5Qsv2A+4DAebzjJ3+7BA4t31ZWZ0AL72//D4Y5MA/ P5SKrGn0dup4OsPs+vGtu4z2lPw3D X-Google-Smtp-Source: AGHT+IHhI45EB7m1z/pBXSWPkP26t50qUS1diC2USA5GzGIHtA5mdJJ3bH2fCeA/V2zkhHzk3RL+tg== X-Received: by 2002:a17:907:d24:b0:ad8:9a3b:b274 with SMTP id a640c23a62f3a-ade7ad31b03mr241390966b.52.1749560740226; Tue, 10 Jun 2025 06:05: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 v2 01/17] xen/riscv: implement sbi_remote_hfence_gvma() Date: Tue, 10 Jun 2025 15:05:16 +0200 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560756599124100 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 the guests. 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 --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/sbi.h | 21 +++++++++++++++++++++ xen/arch/riscv/sbi.c | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/xen/arch/riscv/include/asm/sbi.h b/xen/arch/riscv/include/asm/= sbi.h index 527d773277..8e346347af 100644 --- a/xen/arch/riscv/include/asm/sbi.h +++ b/xen/arch/riscv/include/asm/sbi.h @@ -89,6 +89,27 @@ 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 the guests. + * This function call is only valid for harts implementing + * hypervisor extension. + * + * 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..0613ad1cb0 100644 --- a/xen/arch/riscv/sbi.c +++ b/xen/arch/riscv/sbi.c @@ -258,6 +258,15 @@ 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) +{ + ASSERT(sbi_rfence); + + 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.49.0 From nobody Fri Oct 31 03:56:15 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=1749560760; cv=none; d=zohomail.com; s=zohoarc; b=Vzd9sFiitJsMvLZJMYBrHeatxs6R4JOglomCo3xhGx2ojUD5hTuH5+VVcBgApzr8CAc7ykPQl01RWyJhuXhcOwacZm78LcJ5bNZMI5dGcC7EGIYFyHupOalmbNlYdJCWCWKJ8hqUylKHx6vT3gQiw0YHObMBlsNWfmtgqZizlrU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560760; 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=CAFeKVvcyVUEFDWmR2Xc6msb8cPqppULN4b4iFqQNWg=; b=N/o9+UbIBUf474852C+HCZIpk39kjVZSajJoRw+q/+My3aeTmCcMhRn7serlBX8RC894a9hTPZRQs4qO3Pg1ztzjgJoJrLNcjYa3C/gU5kDB8BcsMGN0U6kEU4osUToeCuRNvISTawC5dKBbBG/bTRRkzfSgVR/ci+1iH5GFCj4= 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 1749560760566978.2860301087751; Tue, 10 Jun 2025 06:06:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010784.1389000 (Exim 4.92) (envelope-from ) id 1uOyfg-0005Ck-Ez; Tue, 10 Jun 2025 13:05:44 +0000 Received: by outflank-mailman (output) from mailman id 1010784.1389000; Tue, 10 Jun 2025 13:05:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfg-0005Cd-Af; Tue, 10 Jun 2025 13:05:44 +0000 Received: by outflank-mailman (input) for mailman id 1010784; Tue, 10 Jun 2025 13:05:43 +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 1uOyff-0004iH-2y for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:43 +0000 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [2a00:1450:4864:20::631]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9d2d16e2-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:42 +0200 (CEST) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-adb2bb25105so866686166b.0 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: 9d2d16e2-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560742; x=1750165542; 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=CAFeKVvcyVUEFDWmR2Xc6msb8cPqppULN4b4iFqQNWg=; b=N/dhPzTebFgNqD2AE+nSlfRJE5UoXHfP4LZkN86b3qdQ/n7+/Door439K/Scd4OJs9 jE0SdWpaQ8okUxCsUMynvzbt16AOzp4j+oRQMo2ge2IZdAxcSL0rR/uSACOIwraqAqHo ZY6xaViOYvEqum4qN24LWQuouGLWue7DRJPQDsdhXmIzsVjeIUBnUpYxOqwPpIV1we0Q ZRS46GiLYNon0S510qSlYgrz2AcTtJwDi8Z4KMo09dB5lt4h/GcYOtupxLGzmjahhsUT L+aS7bXgSPSvHddsJVnSiTSMhZJzEfk7iHBogAUtKLmQeCATOeovPKPeVQn3x0dzEwOd BDCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560742; x=1750165542; 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=CAFeKVvcyVUEFDWmR2Xc6msb8cPqppULN4b4iFqQNWg=; b=fvP5pqC4RCIE7li21xGZwmLEs2inmiCwiC7Hxtn7n9i+94rshUHMPxCN2X/d3FjAME 06p+FFlHuhA0yh/RSu9osvpE9vTzQ4UvtLtRctxfyEHfvyvlcwc+BnEHDYm3M9HbX3I9 ABlcKMqCwea61zx+TcV5K+ycknN53PR/KuwLDpu6qzgduTI1iNlzLVzJye3rvuDYdtUs /9t7CMcRT61/ClzjbH1kDJ0f+DocZpF9ft24J516fs8Udj7bbq37AelY3UCGiqoiiaUh ZULOBPq/Lq+LaBmyNNEBhoH1h0EFDYButTwDMyp7YDDu2nHUg0MCPY86by4lSsUix2M2 qg/Q== X-Gm-Message-State: AOJu0YzHepDHBXwahLFAi6eCW0J1VRhbKrYwI/WTybrMawWcn+VghsWY guT79cBteAYc9wLED8+wHdsvCgvwz4UnMkbGjRpzQ4GPOg94xkRN1sw5VH8MaA== X-Gm-Gg: ASbGncv9NU7OIMMW/wGbSkOvX5TUorMindN1dkntUkMMCN/ZNviyN4TaRYu+3VW5lNj CQvcldiT2gvMxAk/OGua9Rd/42ktKeGxFQYIp0mHUywU3o/en0lMLs6BcRdxeeWtiqSZ8FZIIre gDTTnquPzgm+eONQhJlpb/+a0bQ4VQo+CJwZ59+cfZwb7CBpG20K/6Mx7XWqkSs3pmqifHNp3B/ GNzgQCw/N/5tP/QO65ZDzaZ9ChMndXR1tMgFob+CvEpdHq56vspFzyyhoKxM275CyEnuxXcRsci CCzSyL4xlxP04URnLLMabnmR5BUml7PUVaY6xHu3fvakQAKKU+jsYbvAN9ldPmxiOdj0l5Dg7o+ YS4PtA+ODMTTBFweWu01ZEUZZ0tlD X-Google-Smtp-Source: AGHT+IHUecsZq3dhiCGc3K0O1u9og5INZT4pEdguTRCMHQSuZgHcV8qFojdIwwPPw3t0uFrzvAeBnA== X-Received: by 2002:a17:907:5c1:b0:ad8:8efe:31fb with SMTP id a640c23a62f3a-ade1abc5eb0mr1502731266b.54.1749560741282; Tue, 10 Jun 2025 06:05: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 v2 02/17] xen/riscv: introduce sbi_remote_hfence_gvma_vmid() Date: Tue, 10 Jun 2025 15:05:17 +0200 Message-ID: <5c614593a1710c737f5509ebb165efd8f857df30.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560762686124100 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. This function call is only valid for harts implementing hypervisor extensio= n. 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 --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/sbi.h | 17 +++++++++++++++++ xen/arch/riscv/sbi.c | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/xen/arch/riscv/include/asm/sbi.h b/xen/arch/riscv/include/asm/= sbi.h index 8e346347af..2644833eb4 100644 --- a/xen/arch/riscv/include/asm/sbi.h +++ b/xen/arch/riscv/include/asm/sbi.h @@ -110,6 +110,23 @@ 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. This function call is only + * valid for harts implementing hypervisor extension. + * 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 + * @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 0613ad1cb0..bfd1193509 100644 --- a/xen/arch/riscv/sbi.c +++ b/xen/arch/riscv/sbi.c @@ -267,6 +267,15 @@ 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) +{ + ASSERT(sbi_rfence); + + return sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA, + 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.49.0 From nobody Fri Oct 31 03:56:15 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=1749560760; cv=none; d=zohomail.com; s=zohoarc; b=XwLxCHy2G+4u+2HMAKvc7nZa6Q2Sa4CX5oUseLIV5b65hItt8iwfm9QNizYWFka5ntSvlUfvRcHx78VDQQv5NIhGg4OghC5gM3doK5TvZB6zNR4xQlnows05YnKjJLyJuMJhbSca7OiIfzzzF+xyLR1NcR9FHGfrTzNof6XXgyA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560760; 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=dYwCKpM2AdDgMkM5pUxy/3g1cY3Ox3ZYugCCj6NxUk4=; b=QAA7TTyPVxvi2ae5axGaPcERCMambZ4yG5a2qSR9eQeJPn/jj5Untibsc1qF2rkCNqtOKZQ9w8UFXXzc/jtNYay7pS2hPkhO95hQRrrYohcH4DMEGwAjI9CBHaRu03X/UYt6bXwIQk64mHSbZIu3+MPteWp/o+mFhF1uRL8Eldk= 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 174956076044112.427320019188983; Tue, 10 Jun 2025 06:06:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010785.1389010 (Exim 4.92) (envelope-from ) id 1uOyfi-0005UG-Qr; Tue, 10 Jun 2025 13:05:46 +0000 Received: by outflank-mailman (output) from mailman id 1010785.1389010; Tue, 10 Jun 2025 13:05: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 1uOyfi-0005U1-LB; Tue, 10 Jun 2025 13:05:46 +0000 Received: by outflank-mailman (input) for mailman id 1010785; Tue, 10 Jun 2025 13:05:44 +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 1uOyfg-0004iH-Mr for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:44 +0000 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [2a00:1450:4864:20::633]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9e20c52a-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:44 +0200 (CEST) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-ade30256175so732529066b.1 for ; Tue, 10 Jun 2025 06:05:44 -0700 (PDT) Received: from fedora.. (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: 9e20c52a-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560743; x=1750165543; 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=dYwCKpM2AdDgMkM5pUxy/3g1cY3Ox3ZYugCCj6NxUk4=; b=ELZTfzinQqlZl4eH2hI2YrpentarNS8Of+PA9YGgUP+bQwuom/E6ceIxthdcD2o6tg GN37gQ2sxPN8nUtAIylf4SHGB94dhGd9qpgqmHrgB6ZGL3A9HU+EU7aKin/nbR2LfsEu y1irOlz4pRAQfX4q34s5Jd1iyhqjJiw9B+RNZilfZGL6dfwqwWF1x0A3YFwW5VfbSuNk 5RjTnOBWcP2lVCKOYzjgVH5IeMqXVRiVVZb2ujlPF+ONodSfvZdWqQqVTnlC5ENygRvH omZtrLyinsyclr3N/Q3ms3U54QxyKxzB1tenaQjzFlY11G0gkDQjgfLiEnK/nvuLDxhe KdNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560743; x=1750165543; 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=dYwCKpM2AdDgMkM5pUxy/3g1cY3Ox3ZYugCCj6NxUk4=; b=ShfG/svXT5Ft4JvpWSLbUHb4Pvl+qFowRrEdYVZRHxf8HWBFPsdvIErPV9QJ8dic3G F546+jVRO1EgR4762gdZIlx6Z4NT0+YcKVLbjyTQqFtiUPVkNnHOarA8Lv1mWftMxlhD Yt6QFF9dLRl3icAMncnsVWU47oSXFrJNV6YWXUfgyElNtN2NOTH0hMDkaCLa0woLFewU oiMFftwtSN1k7jQ5ThxqqRBPJJX1enhTzqRfPsmIcqFHo7FbBJwTgmD58h2WbKhF6pgg 6y08YWPOyHojdz37iuBdSZ9fEprZ2plYKBjiEZEh6GIPWHKEc5iR7XmrH0+FNfJZazDz BW1A== X-Gm-Message-State: AOJu0YyEZkb5GJGB4v0HAbCZGj5eStci2YEiF7ihJOCrxdHURQtQV9Xy wSe+o+bj/fuHrMDwNLsN8YpJ7QqUwPMCRC2hPU57OFKSdJUqn1Ng5p+geXNIkA== X-Gm-Gg: ASbGncvODGBvKWk8+bs9wthQS/87ZvdN11YBBqF6qz4NDivdrtZjl0uUG+KJlbyNUmi pfUrnrcPsjNI/EnlFGJGHq/eCdbNlzvF2EtM2uZ3t5xc7YEF01GG/iV7/8KBt6feuRxBjW0XhQi FnxRYi2Wv7ZJDKxZb9bpX2Gb7AtjSzEdaqYqeM7TtXKNenM8+wa9w2rHNAV3b3xnVgbYDVf76u3 pOPRAPsG6AfUvc4SiiNql1Ebjtu4LoAJzvSb3OZl6HQrNWpK/LxP+ahS5jYhviNMxXfC4b4DGPf WJH5jsYlPYalbYOihxtU62Tt3j2GKCwllpvKKkBySJHHk2zsZCbFlCpCOyExcXRx3B8LVX4l/Wk FqLQb16jWrcMrUKFhPL7hN9G4qHRW X-Google-Smtp-Source: AGHT+IEjMi5KqzHuFyzfiyMJz8eCJb0GUi4tJPNejv6sm92QokiGnhk6HzV5tiFCfi0GjnHS+iO74A== X-Received: by 2002:a17:907:7289:b0:adb:229a:f8bd with SMTP id a640c23a62f3a-ade1a978096mr1571669266b.29.1749560742692; Tue, 10 Jun 2025 06:05:42 -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 v2 03/17] xen/riscv: introduce guest domain's VMID allocation and manegement Date: Tue, 10 Jun 2025 15:05:18 +0200 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560762804124100 Content-Type: text/plain; charset="utf-8" Implementation is based on Arm code with some minor changes: - Re-define INVALID_VMID. - Re-define MAX_VMID. - Add TLB flushing when VMID is re-used. Also, as a part of this path structure p2m_domain is introduced with vmid member inside it. It is necessary for VMID management functions. Add a bitmap-based allocator to manage VMID space, supporting up to 127 VMIDs on RV32 and 16,383 on RV64 platforms, in accordance with the architecture's hgatp VMID field (RV32 - 7 bit long, others - 14 bit long). Reserve the highest VMID as INVALID_VMID to ensure it's not reused. Implement p2m_alloc_vmid() and p2m_free_vmid() for dynamic allocation and release of VMIDs per domain. Integrate VMID initialization into p2m_init() and ensured domain-specific TLB flushes on VMID release using sbi_remote_hfence_gvma_vmid(). Signed-off-by: Oleksii Kurochko --- Changes in V2: - New patch. --- xen/arch/riscv/Makefile | 1 + xen/arch/riscv/include/asm/domain.h | 4 + xen/arch/riscv/include/asm/p2m.h | 14 ++++ xen/arch/riscv/p2m.c | 115 ++++++++++++++++++++++++++++ xen/arch/riscv/setup.c | 3 + 5 files changed, 137 insertions(+) create mode 100644 xen/arch/riscv/p2m.c diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile index a1c145c506..1034f2c9cd 100644 --- a/xen/arch/riscv/Makefile +++ b/xen/arch/riscv/Makefile @@ -6,6 +6,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 c3d965a559..b9a03e91c5 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 hvm_domain { uint64_t params[HVM_NR_PARAMS]; @@ -18,6 +20,8 @@ struct arch_vcpu { =20 struct arch_domain { struct hvm_domain hvm; + + 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..359408e1be 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -3,11 +3,21 @@ #define ASM__RISCV__P2M_H =20 #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 { + /* Current VMID in use */ + uint16_t vmid; +}; + /* * 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. @@ -93,6 +103,10 @@ static inline void p2m_altp2m_check(struct vcpu *v, uin= t16_t idx) /* Not supported on RISCV. */ } =20 +void p2m_vmid_allocator_init(void); + +int p2m_init(struct domain *d); + #endif /* ASM__RISCV__P2M_H */ =20 /* diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c new file mode 100644 index 0000000000..9f7fd8290a --- /dev/null +++ b/xen/arch/riscv/p2m.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include + +#include +#include + +static spinlock_t vmid_alloc_lock =3D SPIN_LOCK_UNLOCKED; + +/* + * hgatp's VMID field is 7 or 14 bits. RV64 may support 14-bit VMID. + * Using a bitmap here limits us to 127 (2^7 - 1) or 16383 (2^14 - 1) + * concurrent domains. The bitmap space will be allocated dynamically + * based on whether 7 or 14 bit VMIDs are supported. + */ +static unsigned long *vmid_mask; +static unsigned long *vmid_flushing_needed; + +/* + * -2 here because: + * - -1 is needed to get the maximal possible VMID + * - -1 is reserved for beinng used as INVALID_VMID + */ +#ifdef CONFIG_RISCV_32 +#define MAX_VMID (BIT(7, U) - 2) +#else +#define MAX_VMID (BIT(14, U) - 2) +#endif + +/* Reserve the max possible VMID to be INVALID. */ +#define INVALID_VMID (MAX_VMID + 1) + +void p2m_vmid_allocator_init(void) +{ + /* + * Allocate space for vmid_mask and vmid_flushing_needed + * based on INVALID_VMID as it is the max possible VMID which just + * was reserved to be INVALID_VMID. + */ + vmid_mask =3D xvzalloc_array(unsigned long, BITS_TO_LONGS(INVALID_VMID= )); + vmid_flushing_needed =3D + xvzalloc_array(unsigned long, BITS_TO_LONGS(INVALID_VMID)); + + if ( !vmid_mask || !vmid_flushing_needed ) + panic("Could not allocate VMID bitmap space or VMID flushing map\n= "); + + set_bit(INVALID_VMID, vmid_mask); +} + +int p2m_alloc_vmid(struct domain *d) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + + int rc, nr; + + spin_lock(&vmid_alloc_lock); + + nr =3D find_first_zero_bit(vmid_mask, MAX_VMID); + + ASSERT(nr !=3D INVALID_VMID); + + if ( nr =3D=3D MAX_VMID ) + { + rc =3D -EBUSY; + printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain= _id); + goto out; + } + + set_bit(nr, vmid_mask); + + if ( test_bit(p2m->vmid, vmid_flushing_needed) ) + { + clear_bit(p2m->vmid, vmid_flushing_needed); + sbi_remote_hfence_gvma_vmid(d->dirty_cpumask, 0, 0, p2m->vmid); + } + + p2m->vmid =3D nr; + + rc =3D 0; + +out: + spin_unlock(&vmid_alloc_lock); + return rc; +} + +void p2m_free_vmid(struct domain *d) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + + spin_lock(&vmid_alloc_lock); + + if ( p2m->vmid !=3D INVALID_VMID ) + { + clear_bit(p2m->vmid, vmid_mask); + set_bit(p2m->vmid, vmid_flushing_needed); + } + + spin_unlock(&vmid_alloc_lock); +} + +int p2m_init(struct domain *d) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + int rc; + + p2m->vmid =3D INVALID_VMID; + + rc =3D p2m_alloc_vmid(d); + if ( rc ) + return rc; + + return 0; +} diff --git a/xen/arch/riscv/setup.c b/xen/arch/riscv/setup.c index 8bcd19218d..aa8f5646ea 100644 --- a/xen/arch/riscv/setup.c +++ b/xen/arch/riscv/setup.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -134,6 +135,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id, =20 intc_preinit(); =20 + p2m_vmid_allocator_init(); + printk("All set up\n"); =20 machine_halt(); --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560763; cv=none; d=zohomail.com; s=zohoarc; b=nED5rnlVqjEp5DRC5Hdc8bZSHYype5SdXxvMZzKrBH36pScrall9OK4PTP8KN4lTBzVIcUq2nXD7yZChFxlJ7EadLg2b9WLOz79A+6v4q/m65NRKCiaNq7TLhc2Fl36N8RXeRQJbSa6NOUHq6qMaRSedX3lagaY0OCCYnwLsQMs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560763; 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=O5cGIp6lzcilrw8ShbCVkPARsAjo5L5Ne3xaR6XcYW4=; b=QxHzNdPFPpFqptswnjEXipUysJAp9uDZt4u6tlKoNkzVyRs0Q+G+CF4v9akPkl0fboOKEEgYn6HI2q27ZaUHZ5Iba/JGYHn/lNksDEGULRddI1jZreXbcq2e6Po9KKl3AA+rgykydffT2msxQl89aOWLXJpBgmtqFg2CuVJ+g8I= 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 1749560763848269.86394744757854; Tue, 10 Jun 2025 06:06:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010786.1389013 (Exim 4.92) (envelope-from ) id 1uOyfj-0005XC-4y; Tue, 10 Jun 2025 13:05:47 +0000 Received: by outflank-mailman (output) from mailman id 1010786.1389013; Tue, 10 Jun 2025 13:05:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfi-0005WI-TX; Tue, 10 Jun 2025 13:05:46 +0000 Received: by outflank-mailman (input) for mailman id 1010786; Tue, 10 Jun 2025 13:05:45 +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 1uOyfh-0004iH-BP for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:45 +0000 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [2a00:1450:4864:20::631]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9e90be84-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:44 +0200 (CEST) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-acb5ec407b1so978046866b.1 for ; Tue, 10 Jun 2025 06:05:44 -0700 (PDT) Received: from fedora.. (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: 9e90be84-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560744; x=1750165544; 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=O5cGIp6lzcilrw8ShbCVkPARsAjo5L5Ne3xaR6XcYW4=; b=OFSzIbgKWbtgOUiRHZyf64fFHOHxgcwdnXvLJDaxPuTXYnXkALBtg3zB2pXbuDoQx5 rB5ImVzsiIDRnvOpWDKvTsz1pLHEvl+2/cyvsCouopsFceEj+721fiYC5bS68D7Ybq8a yIPRKUqDMu9LeQJPSSqWMVAzKKnRuTcQwSH/lVKqyEiXloJtK1OsBq35fKx8WZvyrhFs IcqOHV0GlsTSjUcgFNuoyE543dUMV33rdCyD32kV+qDILJovc+EyCyYyDuEDBvi4Tnmw fiNG+zApYCWoUj2sQVYz6FFA5Hg2OJ7vdsSNKaYl443WpcS/p80Px2iZdolnHS54K6Rs 62HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560744; x=1750165544; 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=O5cGIp6lzcilrw8ShbCVkPARsAjo5L5Ne3xaR6XcYW4=; b=DIp35M450VG8W/4wo7osPNSBsvmcUhoAWOw9Hpn07/LsTxKUycgk92I6IqRc3gOuGQ pHzthnAijKh1Y43e1Hj2/xUfGkZCpFR9rgGIc27F4NhLIYoJFuJQ0KZx3tGuiqPFdkiz VlT4+3D92/Mw5Hl7Fzd9cW131pbr7Bm5lxcPuHgVNT75qmFRSxPs1bbWQQUokK0t6Yn2 dK11n7/qOUB2ErD/IUZZLD+KqmDEqwKq9W5kcxg1uxonwAzyl38XbGz9Gaq6EEGCA8Se 3x5nEW4IURQQrO4hR45Q8hkhrwVyGxrRAXtQj4SIIbQho3L2DLA+R+Z/okc8yMUV3PGj hICg== X-Gm-Message-State: AOJu0YymSJtSei0x/6HSXvUS63wv0fqHj+JtKYWa2UgMSeo6rqnNeLgG 9RQWKX3WltPgRgfFbRiBwQRNSFdGlnlcfmgyzNkNc0fuB9W/rml8KbqXFLmlWw== X-Gm-Gg: ASbGncvA6S5nKOGDGZEEOlodGT5qF45yqlcLCdPllb1Lfoqx1d989lvMNuAOMhqQG6N aYmQzPY2CpUXOVE3Nvt4vvD5NYTO3WLaSNr+dRFJMGox0qSIiYVnTx0St+4gmyKeqbxzgXqwlQp HYGYEu8Hsw8OgRcyiMIZ75LzxREWDEHEOywDY/Ko3dVdrB+B/Pc2Ef9q/7Usl2dnLT2nc0oakx3 T+Cz0vxOn8Gn6N7uUGfgv2F3u96wq+p2fAO1miZ37Zu5+mW8ImARsq9Cp9m8QER2GbHG4UVUSoz l3R7oqvIRUF8MQ5FshipndgLRMyAp5sZTHU24mHTRkq9dPagMUIGKIHA69jnAF8cMP/nYawb4pC kSqWHM9WCBhth+Uog7JtSPfhR1j97 X-Google-Smtp-Source: AGHT+IG+GuksJhWm+r89SaPyXR4MgjeFB20puLjU92lWbLXtuoPxVX1Hd0Ryeej0kxstXLeuped14g== X-Received: by 2002:a17:907:c22:b0:ad8:87a0:62aa with SMTP id a640c23a62f3a-ade1aab90f1mr1740126066b.27.1749560743862; Tue, 10 Jun 2025 06:05:43 -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 v2 04/17] xen/riscv: construct the P2M pages pool for guests Date: Tue, 10 Jun 2025 15:05:19 +0200 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560788624124100 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. Signed-off-by: Oleksii Kurochko --- 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/include/asm/domain.h | 12 ++++++ xen/arch/riscv/p2m.c | 59 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/xen/arch/riscv/include/asm/domain.h b/xen/arch/riscv/include/a= sm/domain.h index b9a03e91c5..b818127f9f 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 @@ -18,10 +20,20 @@ struct arch_vcpu_io { struct arch_vcpu { }; =20 +struct paging_domain { + spinlock_t lock; + /* Free P2M pages from the pre-allocated P2M pool */ + struct page_list_head p2m_freelist; + /* Number of pages from the pre-allocated P2M pool */ + unsigned long p2m_total_pages; +}; + struct arch_domain { struct hvm_domain hvm; =20 struct p2m_domain p2m; + + struct paging_domain paging; }; =20 #include diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 9f7fd8290a..f33c7147ff 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -105,6 +106,9 @@ int p2m_init(struct domain *d) struct p2m_domain *p2m =3D p2m_get_hostp2m(d); int rc; =20 + spin_lock_init(&d->arch.paging.lock); + INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist); + p2m->vmid =3D INVALID_VMID; =20 rc =3D p2m_alloc_vmid(d); @@ -113,3 +117,58 @@ 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) +{ + struct page_info *pg; + + ASSERT(spin_is_locked(&d->arch.paging.lock)); + + for ( ; ; ) + { + if ( d->arch.paging.p2m_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 P2M pages.\n"); + return -ENOMEM; + } + ACCESS_ONCE(d->arch.paging.p2m_total_pages)++; + page_list_add_tail(pg, &d->arch.paging.p2m_freelist); + } + else if ( d->arch.paging.p2m_total_pages > pages ) + { + /* Need to return memory to domheap */ + pg =3D page_list_remove_head(&d->arch.paging.p2m_freelist); + if( pg ) + { + ACCESS_ONCE(d->arch.paging.p2m_total_pages)--; + free_domheap_page(pg); + } + else + { + printk(XENLOG_ERR + "Failed to free P2M pages, P2M 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; +} --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560766; cv=none; d=zohomail.com; s=zohoarc; b=l/8cOGovzb/EU+95y7O7240X0mWtGf8/umGZ6nbY+vE8v9NwfcPwGa76FgzVr74lCIt9WS7/57F4p7Aj3MR920t5xF1z0mSJVDFjhu/wOGeG9fqGkwwj1AJNH6FZmbbIaYgNOU8ai0Tshez4uF0TzngFbdTLsv+BFArYO196K6o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560766; 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=EXarRpVA36z28lQ7RkHAs0DFKKfiVvul0XZ2J2qt7Hc=; b=hgVGfibEccTVK1fBX+/CM3i7rVT3UbWXbqQtxuZ+tb2k/Xu/fPdTxokTOQ6lfJontmMfzwsz+6xQNponaiC7r5xCrVxZb0a9qHilYYKKzsi/nwFSh/i+3G0EuqQM9AXeqGsZ+gq5HQJ4TczvT/dipMSefMtK8KrpoZzdR32pBdY= 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 1749560766318694.3429010952577; Tue, 10 Jun 2025 06:06:06 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010787.1389029 (Exim 4.92) (envelope-from ) id 1uOyfl-00061x-Ca; Tue, 10 Jun 2025 13:05:49 +0000 Received: by outflank-mailman (output) from mailman id 1010787.1389029; Tue, 10 Jun 2025 13:05:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfl-00061h-8N; Tue, 10 Jun 2025 13:05:49 +0000 Received: by outflank-mailman (input) for mailman id 1010787; Tue, 10 Jun 2025 13:05:48 +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 1uOyfk-0004Sm-9I for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:48 +0000 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [2a00:1450:4864:20::635]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 9f62feb6-45fb-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 15:05:46 +0200 (CEST) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-ad572ba1347so792286266b.1 for ; Tue, 10 Jun 2025 06:05:46 -0700 (PDT) Received: from fedora.. (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05:44 -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: 9f62feb6-45fb-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560746; x=1750165546; 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=EXarRpVA36z28lQ7RkHAs0DFKKfiVvul0XZ2J2qt7Hc=; b=fVYEyRUTdngjtzd2Povt507A8TYl0D8f8YxlkeceNZ2XiFNkCyKSJpdqVrE30ayccy m0QxdQGH8WR17XAzKeeh0FpqmSjaWsCF8KLt1t47xrCORgKR2tPwz08XjDXtos05NGKC +AGyvvhjqgqQpiVHXUS8/5mYpYQKXpqWRhgf/LfgoeA68APnYwrG8Z9XshcJLEj0YN8r npHOf0k93NbHD+oSIuPYuLJXFBtZ4Iix/fMMQvxD6ydF5suqJwQNsPQKrCh7CAVIf492 nLjLmg+HLzadMKomUTGV1Xd15HcJGVhkS0j1SGfPpC3Wdx9T1CJ7Vd2PuHDfLbrvm4Mo qRDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560746; x=1750165546; 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=EXarRpVA36z28lQ7RkHAs0DFKKfiVvul0XZ2J2qt7Hc=; b=AbGEe0Z7OR8CiDg6Rdc3Q4KBZ/vVlG8MkgLB9+LaptC2+EE00kznR+4l3ps8vxtleZ Ia8Wh/4m8kE9+7g/nJwP4pRtlMyfI/835FUrrosp5pFnSXdbZjq0zLH+sc5z81aa1WKW rGA4MAcceNKvmgx9b2U0yC6nLddMc7nbpOU0rhhocy4QBH5YcQw0k8VZqKwg17xEojf6 cW5sHnuASaIC00+2P3Rcg21hEm1CQ/+TDl0izAge9MvwyUf0fRoCe6cOw7OGO9AUZBiA afKWjXQjWO8yv2XfGcW7ySxlZiihcyCrMvbu7wjzqdk4U2V265DFpfQ/ECb7ZVc8/BXH TCMg== X-Gm-Message-State: AOJu0YwchGeNSS1uYFHJoSPItlTHctNStFyDX1+ttqDs/U7orKVaovoT 32ZCj9Ka9YoV1UpzSehnl5IVGZO+c672Yi2XecYmwB5pwblH9niud3R/JfzlGg== X-Gm-Gg: ASbGncug+w1Au9KKMryODUuRIJ6tFQ4Y3Up+owr2Vbx+2mzecl8RvVuB7UfbZciVn3m LuhfEfn3MjwnBEDL/P792rO5l1Y97KTUWgu3UkEZi/TrNvGpan1SAl8pYVv9cr0z0r7Ps+MQsk6 cKTrwImCvloVNIGhAWB+WEnAvQ8edT9kqkJnXA9Tsxkih6KLH9LBn2tUu8gI9esdX/TUKYtLiy8 o63bsxvckQL1N6bNvsYNTGio34S0V+lS3I0GXvNpWegEeAMtiE3nBNvtt28nZXgY264rRsSzYxe GqdaaX+hCH/obvIuUuoZHnLd8GSJ2mDaQu3OKL8JYVOXp35JszHzNJrigwI7gFetMG5/OvqBRtF 3/QXUPQUzAIBfrU6b9Bu6nc5bL8Hy X-Google-Smtp-Source: AGHT+IHwyPiGJS37+PX0GdCY66Zwx/DKdObjhSV3s5pHoY4KpTJ/uOq0y2ZsmygVOv78BL+hNDPXxA== X-Received: by 2002:a17:907:7fa9:b0:ad8:9257:5733 with SMTP id a640c23a62f3a-ade1a905a8cmr1303884166b.20.1749560744967; Tue, 10 Jun 2025 06:05: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 v2 05/17] xen/riscv: introduce things necessary for p2m initialization Date: Tue, 10 Jun 2025 15:05:20 +0200 Message-ID: <443cb3566a60dcb5d5440c72410ff6d76a010a58.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560788681124100 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. - radix tree to store p2m type as PTE doesn't have enough free bits to store type. - default_access to store p2m access type for each page in the domain. - back pointer to domain structure. - p2m_init() to initalize members introduced in p2m_domain structure. - Introudce p2m_write_lock() and p2m_is_write_locked(). - Introduce p2m_force_tlb_flush_sync() to flush TLBs after p2m table update. Signed-off-by: Oleksii Kurochko --- 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/include/asm/p2m.h | 39 +++++++++++++++++++++ xen/arch/riscv/p2m.c | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index 359408e1be..9570eff014 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -3,6 +3,10 @@ #define ASM__RISCV__P2M_H =20 #include +#include +#include +#include +#include #include =20 #include @@ -14,6 +18,29 @@ =20 /* 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; + + struct radix_tree_root p2m_type; + + /* + * Default P2M access type for each page in the the domain: new pages, + * swapped in pages, cleared pages, and pages that are ambiguously + * retyped get this access type. See definition of p2m_access_t. + */ + p2m_access_t default_access; + + /* Back pointer to domain */ + struct domain *domain; + /* Current VMID in use */ uint16_t vmid; }; @@ -107,6 +134,18 @@ void p2m_vmid_allocator_init(void); =20 int p2m_init(struct domain *d); =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); +} + #endif /* ASM__RISCV__P2M_H */ =20 /* diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index f33c7147ff..e409997499 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -1,13 +1,46 @@ #include +#include #include +#include #include +#include +#include +#include #include #include #include =20 +#include #include #include =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_vmid(d->dirty_cpumask, 0, 0, p2m->vmid); +} + +/* 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_force_tlb_flush_sync(p2m); + + write_unlock(&p2m->lock); +} + static spinlock_t vmid_alloc_lock =3D SPIN_LOCK_UNLOCKED; =20 /* @@ -109,8 +142,33 @@ int p2m_init(struct domain *d) spin_lock_init(&d->arch.paging.lock); INIT_PAGE_LIST_HEAD(&d->arch.paging.p2m_freelist); =20 + rwlock_init(&p2m->lock); + INIT_PAGE_LIST_HEAD(&p2m->pages); + p2m->vmid =3D INVALID_VMID; =20 + p2m->default_access =3D p2m_access_rwx; + + radix_tree_init(&p2m->p2m_type); + +#ifdef CONFIG_HAS_PASSTHROUGH + /* + * Some IOMMUs don't support coherent PT walk. When the p2m is + * shared with the CPU, Xen has to make sure that the PT changes have + * reached the memory + */ + p2m->clean_pte =3D is_iommu_enabled(d) && + !iommu_has_feature(d, IOMMU_FEAT_COHERENT_WALK); +#else + p2m->clean_pte =3D false; +#endif + + /* + * "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; + rc =3D p2m_alloc_vmid(d); if ( rc ) return rc; --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560770; cv=none; d=zohomail.com; s=zohoarc; b=N+R1VidW0zCfVJUTjuTdZPmJZjotBefn3anWuSPT+DO0QuTbt8E7xPa4E+VXzDbTtepdyUUtr6blav3SYQRHYOQXbhqlWLr6ejwHTCToUX/ZzOC3cJXMxZy9AJxu6EH0nXGvEniWr22/T1PAq/8oZh7PHfTN7jW7re58GMBsE3w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560770; 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=G9hLIUfPcNIKsmfZjWqaz9gsdigw2uPwEp+WXF0YeaY=; b=DcbTnG4V8WSEvYVXFLtzw1lxqrzb4aQN9kMYVGy5inusk1n/ahtYROhRjW9uzGTXU6KeBEQZid8Yh72i/FAamy/erhhBRFPkUm9TvuCEaFRPizCKV4OENpOSyAWsPAmz3oC6zYd3jxTh4kzy0ddNlPhXnvFXOYd+rCjWgfpgOBw= 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 1749560770941134.64959792991397; Tue, 10 Jun 2025 06:06:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010788.1389039 (Exim 4.92) (envelope-from ) id 1uOyfm-0006JZ-Us; Tue, 10 Jun 2025 13:05:50 +0000 Received: by outflank-mailman (output) from mailman id 1010788.1389039; Tue, 10 Jun 2025 13:05: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 1uOyfm-0006JF-Oh; Tue, 10 Jun 2025 13:05:50 +0000 Received: by outflank-mailman (input) for mailman id 1010788; Tue, 10 Jun 2025 13:05: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 1uOyfl-0004iH-17 for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:49 +0000 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [2a00:1450:4864:20::62a]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a09ba216-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:48 +0200 (CEST) Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-addfe17ec0bso1197577466b.1 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: a09ba216-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560747; x=1750165547; 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=G9hLIUfPcNIKsmfZjWqaz9gsdigw2uPwEp+WXF0YeaY=; b=RHpBVplvWGQO4tqk0h9Q6ECwXkuaG2kfCFS5NDDbNpRRBsKUY5NZC3qDbqia7zbB2q 9yGtIy65Fj/U2fnsNwOGZwZ5yy7Q0HWJAoPpVuUCveFXwDH8QAW5EBX9i6ZBS5FMuNFp z00mhkIx+5G0WeqHX0QEqhGn2ljDQku9sgvmlYMVzLz8yOK2T2NHYh4zWM1s3vzAUIbO Dru4p716LQw1si6v0H5c/1MdDmRW8UDnJitRgMTXzwWDZO0PlM2p35C3/3f8O89qufIf p9TEYTzX3l6LpVJnO1IGbQR1kezQaOfxJt01et/5fOPAPkLOADN2SD6x4YGwjbB7fQJn wA8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560747; x=1750165547; 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=G9hLIUfPcNIKsmfZjWqaz9gsdigw2uPwEp+WXF0YeaY=; b=SaVwJru/ffUn2JRnpuiEF3pa/FVGwZpd+3iYXILbQVlkQovEqTtJouR9NF9Lp/SjXK Mr0/xJKP+tVz4KrvrouXC9I5rmLZfazyvYvA6dDuWlk1Kutt/xigkKIVQGbCOgZzoPnZ K8n/9xr6UdR5fjBUM8EWNWIaqi0sZpejDk0Rh4NRY2rFVKVZupUgA+9UuAwmqeLVso8n zr/gD3gpu1UKDDEgZo0SaDRTQXh58Ag+VArj2fycwdr4MDZ13JWkRONa24KWMH+4zWi4 aX3j2/EI5b3yv+yOscu48LyLd9ezFNrlOBtUbxWNnC102NAZIk+q3b4Tx4L/jVr7MOPC ZmgA== X-Gm-Message-State: AOJu0YzCSVFMgK5ao9QfrMc2LJphAx/GEHvVkoNrTDYvMbggsymS2A+a wEsd95976WCguUsQ+jQC5c0MmipZCU9mNnQlSPWsQM315Bx4ns6/xjTHQDedlA== X-Gm-Gg: ASbGncuXaDqAPatycN0yBo+EHRcIaaRuaffRiPtnpAdE4yHrPis366jCR3PRoiBza3q RJ/d3LFH2D3cITPpm4KzbiPFlM5snOixbH3j+fUNJTEVBhQRQBFxcXzEOqERecA+JZ/Pe3M/qqA 1DfBGGAONEjgMBgRVTFO8IomA3hg9WYiN+pEL0dtgevrKT+VTLwsv0V02kMoqcYFZaRGBNixi9y ussyro2fq5Rm5M3h/oZYrRM9b8Wii0ZpZnX00Jv/KW4R4hEw8yzg9+59gCmz6tUOCog0v0JR11W /JiN+JUeZ40HjyB5KJlok2/RUk2YDDLD6/jCYcXsT2U2OKPDjPAX+vnS5mGlhsVSqLTOW01A6DM /ll7LrVJPgX9PkI45g+pLByU4ZtmT X-Google-Smtp-Source: AGHT+IFihu1rI2wMBHoXh5YoFH0UgxCCFKSkuzc1RFwjRhSWVuUdAk/0XgNrOroqtrfUkLA8y16lQQ== X-Received: by 2002:a17:907:7ea2:b0:ad8:6dc0:6a8a with SMTP id a640c23a62f3a-ade771274a6mr309558066b.1.1749560746070; Tue, 10 Jun 2025 06:05: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 v2 06/17] xen/riscv: add root page table allocation Date: Tue, 10 Jun 2025 15:05:21 +0200 Message-ID: <76675ddb7517e4cceb63472c94944046b255da01.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560772901124100 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_allocate_root() helpers to allocate and zero a 16 KiB root page table, as mandated by the RISC-V privileged specification for Sv39x4/Sv48x4 modes. - Add hgatp_from_page() to construct the hgatp register value from the allocated root page. - Update p2m_init() to allocate the root table and initialize p2m->root and p2m->hgatp. - Add maddr_to_page() and page_to_maddr() macros for easier address manipulation. - Allocate root p2m table after p2m pool is initialized. Signed-off-by: Oleksii Kurochko --- 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 | 6 ++ xen/arch/riscv/include/asm/riscv_encoding.h | 4 + xen/arch/riscv/p2m.c | 94 +++++++++++++++++++++ 4 files changed, 108 insertions(+) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index 01bbd92a06..912bc79e1b 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -149,6 +149,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 9570eff014..a31b05bd50 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -26,6 +26,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; + + /* Address Translation Table for the p2m */ + paddr_t hgatp; + /* Indicate if it is required to clean the cache when writing an entry= */ bool clean_pte; =20 diff --git a/xen/arch/riscv/include/asm/riscv_encoding.h b/xen/arch/riscv/i= nclude/asm/riscv_encoding.h index 6cc8f4eb45..a71b7546ef 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,6 +184,7 @@ #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 TOPI_IID_SHIFT 16 diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index e409997499..2419a61d8c 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -41,6 +41,91 @@ void p2m_write_unlock(struct p2m_domain *p2m) write_unlock(&p2m->lock); } =20 +static void clear_and_clean_page(struct page_info *page) +{ + clean_dcache_va_range(page, PAGE_SIZE); + clear_domain_page(page_to_mfn(page)); +} + +static struct page_info *p2m_allocate_root(struct domain *d) +{ + struct page_info *page; + unsigned int order =3D get_order_from_bytes(KB(16)); + unsigned int nr_pages =3D _AC(1,U) << order; + + /* Return back nr_pages necessary for p2m root table. */ + + if ( ACCESS_ONCE(d->arch.paging.p2m_total_pages) < nr_pages ) + panic("Specify more xen,domain-p2m-mem-mb\n"); + + for ( unsigned int i =3D 0; i < nr_pages; i++ ) + { + /* Return memory to domheap. */ + page =3D page_list_remove_head(&d->arch.paging.p2m_freelist); + if( page ) + { + ACCESS_ONCE(d->arch.paging.p2m_total_pages)--; + free_domheap_page(page); + } + else + { + printk(XENLOG_ERR + "Failed to free P2M pages, P2M freelist is empty.\n"); + return NULL; + } + } + + /* Allocate memory for p2m root table. */ + + /* + * As mentioned in the Priviliged Architecture Spec (version 20240411) + * As explained 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, order, MEMF_no_owner); + if ( page =3D=3D NULL ) + return NULL; + + for ( unsigned int i =3D 0; i < nr_pages; i++ ) + clear_and_clean_page(page + i); + + return page; +} + +static unsigned long hgatp_from_page(struct p2m_domain *p2m) +{ + struct page_info *p2m_root_page =3D p2m->root; + unsigned long ppn; + unsigned long hgatp_mode; + + ppn =3D PFN_DOWN(page_to_maddr(p2m_root_page)) & HGATP_PPN; + +#if RV_STAGE1_MODE =3D=3D SATP_MODE_SV39 + hgatp_mode =3D HGATP_MODE_SV39X4; +#elif RV_STAGE1_MODE =3D=3D SATP_MODE_SV48 + hgatp_mode =3D HGATP_MODE_SV48X4; +#else +# error "add HGATP_MODE" +#endif + + return ppn | MASK_INSR(p2m->vmid, HGATP_VMID_MASK) | + MASK_INSR(hgatp_mode, HGATP_MODE_MASK); +} + +static int p2m_alloc_root_table(struct domain *d) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + + p2m->root =3D p2m_allocate_root(d); + if ( !p2m->root ) + return -ENOMEM; + + p2m->hgatp =3D hgatp_from_page(p2m); + + return 0; +} + static spinlock_t vmid_alloc_lock =3D SPIN_LOCK_UNLOCKED; =20 /* @@ -228,5 +313,14 @@ int p2m_set_allocation(struct domain *d, unsigned long= pages, bool *preempted) } } =20 + /* + * First, wait for the p2m pool to be initialized. Then allocate the ro= ot + * 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 ( !d->arch.p2m.root ) + p2m_alloc_root_table(d); + return 0; } --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560769; cv=none; d=zohomail.com; s=zohoarc; b=N3DlCDMzX2g3bem2AsNzE+ds5Tes1USHgZZMAyheviKK09j2M/UHVVG93KkcKGKwNqgBtF+mfb9IQpHFhWLzVim6iYNeRUHUWOmEoFwAxI+UOc0JO0e/+KnzCyzj6ynISLsNtQ6Ub0qnuaPg4s78z6NH9jtFBO5CAjbFumjwJ9A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560769; 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=vgDItj/Bt+Pokzd4VeTK3tNuj4yPdCLljiPQUCHOaVk=; b=UHaPo0u0abjhmL1qSuGmVsbUUyF0bVm/sdAHClb4WxPRnchRcyRFM5WTBs1anJMWg3kz5t8nCrcY8eoouRDMUnbrdR+8Wq1XCoDcYiK9JeasNkQ6HcS95R6ChcDSbumlAG6Iq0beYxD9X0P9PZQBk6Z/ZqGfaLojoVpll5oczdk= 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 1749560769015589.5114093884903; Tue, 10 Jun 2025 06:06:09 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010789.1389049 (Exim 4.92) (envelope-from ) id 1uOyfp-0006gq-Ap; Tue, 10 Jun 2025 13:05:53 +0000 Received: by outflank-mailman (output) from mailman id 1010789.1389049; Tue, 10 Jun 2025 13:05:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfp-0006g1-3j; Tue, 10 Jun 2025 13:05:53 +0000 Received: by outflank-mailman (input) for mailman id 1010789; Tue, 10 Jun 2025 13:05:51 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfm-0004Sm-TA for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:50 +0000 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a127a942-45fb-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 15:05:49 +0200 (CEST) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-ad88eb71eb5so745252066b.0 for ; Tue, 10 Jun 2025 06:05:49 -0700 (PDT) Received: from fedora.. (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: a127a942-45fb-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560748; x=1750165548; 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=vgDItj/Bt+Pokzd4VeTK3tNuj4yPdCLljiPQUCHOaVk=; b=DZXqmF8yw+n8vHvFRijSCXknuC2VvQWVn++eiYu+oj42DewjK3jojqcLnew3D7qfBy N+eFPQaWKku3yl6QZMo8IgCtkla/RE2FAnkItjzdig4qO0e8RTMQGf96VBX2rpWDIlrM hw+r5Ma/9i6lH7ObwK0DqWtxNS4T0Z0ig6RNQYz+VuZ9ffbjJAHSGBNqM9AXORb3+eQs zBOydzOYx+t1Di7aqZpbdTimYPdA/FDX6+Ddu8NvPCmPpvwfLZgmkmRnylZXutVONx97 cp72BM3vgfj6fMnDLe8LloFDJvhtAi6X+s01krFzfvqCguyDgExIgAT/lG/r8bjzEIF8 kyIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560748; x=1750165548; 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=vgDItj/Bt+Pokzd4VeTK3tNuj4yPdCLljiPQUCHOaVk=; b=hfbAlK9P231waWe8rwFqcnhfktbr6HaaXpYWHbHwdpwYdvb1b3xV4jC9fbr5W1yptu UqcPylvYZZ0FUGWcJ3MVUie9Q6wMM/8eiV1++Y88usVC051LP/cPnD1L8uRA8xUpGXaD hG+c9mV5WYn2LlU7N5oya9qdV38K05uQbWrGo7UpANEwrYZZ0aUKIA2Qdf+PTCYYhiIl LZYm/1Rhmyz5j19zGQ8Mc1n+GW2szkYmMN3a3JEkKpwZ7/UHBMiRtdCll698AwWlJ/3a mo43KETx082Q/CeylibeYz0bja5a0RcO4q7FQ4DYgz+cINP+xu8LPl/n3uvBKFAeAA2f 7Jwg== X-Gm-Message-State: AOJu0YxxboOUVlI3CIKVsOdp09dzzcQ95VkKWt969YQcqWoXj6CvjJsP XxjHQk6RHGfkUmxozJyMfpTLqTEmw4Jca8OjaF8QaYnoyITWivojmaBphoZFnw== X-Gm-Gg: ASbGnctXZZ9KBuYjpTcRNuQFSLV8xsGtD3PVtQOeQjp0/mESXvgJYgMwdvLj+iL61UH hH86FJ70QBum7WUQRg9ci2NRRGeAoTEun2OBg8m+Xt9dCt9xj5kpwgEoifLb/mlQX6eE1RmeZbu 58U1HauCbp4zh2vcmwlZk+uRckH1JNnZee5B6InotJHis7G6ha8oOGMaGHV1riljRsVpURR68aq d/EOhFrb0iGS88+6J5vyRvaqOUfZt7rC9jJnSFG8jrVGAoh3FWjf6kGyd4eKo26ZUE0B61ZcANx Pz6Ed8FrhV1e19oGF5+vud/nq6ijkgl3vRwdPZvfk/exeyO3m2k/ITYwK6n3GxygFTlbqaTxg+R pmKIwH/HfjU7M5upbjrHLjxhvYGcJ X-Google-Smtp-Source: AGHT+IHyp9el8zq0YS8Dtc+4LmQcBKrv1C8HmiuR5ZqVVMf5an/ANNzub+p9XkEnawpv2328xnzCVw== X-Received: by 2002:a17:907:6d12:b0:ad8:a684:a1f7 with SMTP id a640c23a62f3a-ade7acdf570mr245655766b.30.1749560748353; Tue, 10 Jun 2025 06:05:48 -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 v2 07/17] xen/riscv: introduce pte_{set,get}_mfn() Date: Tue, 10 Jun 2025 15:05:22 +0200 Message-ID: <5e2f0cea49ac1e0669be15811b1426bf4b4d2fec.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560770771124100 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 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 4cb0179648..1b8b145663 100644 --- a/xen/arch/riscv/include/asm/page.h +++ b/xen/arch/riscv/include/asm/page.h @@ -114,6 +114,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 pte_t paddr_to_pte(paddr_t paddr, unsigned int permissions) { --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560772; cv=none; d=zohomail.com; s=zohoarc; b=mhha6Gpc51OgwLMPf3rKaSk6aq9yp+S6hiJbWxVV5UHCaJbtBhnCqT1qSYYm6C4D8wjIunCXVFC37ReywdjTorn79BJJZNmEJC4gejaK4if9xrB/r+ehSffXCw18RO+yHT0JypWDFf5XxCRzO2Bd2hZ36CEalNZM9Znysr3Oyxc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560772; 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=wljNhX5Z+KEFF48phAFs/NmPQwz56AjKrfG07RWLiVw=; b=LeqkNtpoeD/nR2abisgu7xFeYWRq3+Nvy5uRurXON5XORwm2juxYpSIUBCM/fLKmiB35PkWGAXC94RTuftREJEPmJTAauNwLhz1C1XmmiiBE9RaDSTxIeAFJnEAWR7ZqjpiCq1SyNaHkRIkNZxYwSx/lr9qB3CzV0jfAg4/aM/Y= 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 1749560772982293.7164443056946; Tue, 10 Jun 2025 06:06:12 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010790.1389054 (Exim 4.92) (envelope-from ) id 1uOyfp-0006mg-T8; Tue, 10 Jun 2025 13:05:53 +0000 Received: by outflank-mailman (output) from mailman id 1010790.1389054; Tue, 10 Jun 2025 13:05:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfp-0006l6-JQ; Tue, 10 Jun 2025 13:05:53 +0000 Received: by outflank-mailman (input) for mailman id 1010790; Tue, 10 Jun 2025 13:05: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 1uOyfo-0004Sm-2h for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:52 +0000 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a1d624fb-45fb-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 15:05:50 +0200 (CEST) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-acae7e7587dso823661566b.2 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05:49 -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: a1d624fb-45fb-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560750; x=1750165550; 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=wljNhX5Z+KEFF48phAFs/NmPQwz56AjKrfG07RWLiVw=; b=Vn1/yWEBMYmo+Z/CrH+R8PICecpmbyqJHEGPrtvN8I723o0L0qIy09wOgkD0/r/d9E Xie3LqYjqr4appOhIUE4mm2ZGmxvCejmPH+HU3CzQIz2ZXFdpc+bIEvrz3V4Y6DxweuU Z3rPY074N/0bnv9w1/QsxGgCMjRbJL4B2iS5DsBQHa9lAFH0XYSoSoxZGOUiZOFynKRZ BnCwEiTIYI5dOY4mBfK3tBZqc573w6SZ1aEsnu1QsQSsazPGanpdtiQr/ShttTZrPeXF qNUBzfwewKawX+4B77odSYNQTFtddAFK9gGypPVROLDboS2G/MuhJ/r09It7ffqoM3RH h87A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560750; x=1750165550; 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=wljNhX5Z+KEFF48phAFs/NmPQwz56AjKrfG07RWLiVw=; b=lWSfSGnAahX1OmFtX80pzh2pVkGzrE26R11qitcn/AQ1O45gH6ZAMILp7tM2nPuRXB 4rAKn0AbTQUmFre42DBNFNgXUVFQYcUYijWSX8bz0kAReEZZCfe2lViR96mltNTot3WV rxELeQmm/8NZnewrzFtXtyiWWG4tvA+zZAHHFD1Ji6io1GyiPDbQVT3UNN072Pohjk4m zHdxMZ1QwIn0XR/+58Fdxw0PzcD0mkIE5pl0xkaeXp2RnkFgKxQb7EsLgPMHkd0G9BLp 4lAx5JLcHwQw6MzfBZqe7c1auNDX0XcOAOfv5PNZOn75TEBZbuM4i3qs8Z+1twC0afme jPMA== X-Gm-Message-State: AOJu0YzAIYwNlISlFRaNFMBmP2S1Sp2x0wRUiPRIwpHs5bQY0sw77VKh dIVIa7bgOREzIIkdqlj3BKGQ3q1QH1Brw3tdztTFPdRpujyzXZZhOAKd7u/SZQ== X-Gm-Gg: ASbGncudktniiDyI763MF8i6scDwyVU9KpnangAYNlDdG3MZ0pzFBl68dHV8UkyXzWy 2DyLYBZFUINDAMj8mi2tBKQp/sz99Z8xOq13TNrjKbW2AHM+7asViHqzMcSLIh2uCIZj0TewhDm 2UWFD95YmbWR9HpdoN9SN9bpfb0jOqM/am21HE7sVVPBPvc9DV5c6I96yD5lTqJWty8YvB+c27C /28gOFXXofMorwiXQD5omosRlcgnRtkSi8LWoAjVOPtMZpMm3mEAtEUTZtp/lHXVtNHi3qowlae BhRyOXlQf/dlWI6vMsB2XyukukIQxCEJdsknRFTIef+XVPEmNw8JQG4dP2PxZ/VxcOniG0o8DMR jU5Wgx4aIhZY01Dre1iPbIWlYPXC5 X-Google-Smtp-Source: AGHT+IGFjTXdFlS6sZvD06fXM72cWhnM/gD4j/C7i7+WrXUDNN4lIYkxwD7jGkhRmCOFEo4b6nz+FA== X-Received: by 2002:a17:907:6ea8:b0:ade:450a:695a with SMTP id a640c23a62f3a-ade450a70e9mr993218466b.61.1749560749329; Tue, 10 Jun 2025 06:05: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 v2 08/17] xen/riscv: add new p2m types and helper macros for type classification Date: Tue, 10 Jun 2025 15:05:23 +0200 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560774786124100 Content-Type: text/plain; charset="utf-8" - Extended p2m_type_t with additional types: p2m_ram_ro, p2m_mmio_direct_de= v, 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(). Signed-off-by: Oleksii Kurochko --- 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 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index a31b05bd50..0c05b58992 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -61,8 +61,28 @@ struct p2m_domain { typedef enum { p2m_invalid =3D 0, /* Nothing mapped here */ p2m_ram_rw, /* Normal read/write domain RAM */ + p2m_ram_ro, /* Read-only; writes are silently dropped */ + p2m_mmio_direct_dev,/* Read/write mapping of genuine Device MMIO area = */ + p2m_grant_map_rw, /* Read/write grant mapping */ + p2m_grant_map_ro, /* Read-only grant mapping */ } p2m_type_t; =20 +/* 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) | \ + p2m_to_mask(p2m_ram_ro)) + +/* 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.49.0 From nobody Fri Oct 31 03:56:15 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=1749560775; cv=none; d=zohomail.com; s=zohoarc; b=M5RJMgOwjGv8wLBhEk0eSmv0Ds7iW0viFGe9wiGTWIgUc47kHoDetQuZbbVzzFZIj9o390FJw7wEfyvtUlmE2l99T00frUlRABMu2q+Od6XaM1VZyxReraV09OuWWCm4IJ6OU2b+yHJK6sCMscO7PGJcYOkxKMwYa6letjCguMw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560775; 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=oVaSdEzC4kuCiD3ZiuCTTzD/g+ai4zWkigRCI0Mr+vI=; b=iKLzkV7fXT7JCKNwcnA9rN2WxesWs0AW/JWdld677Su3VkeZ6qVSIQ0gbFmKJLqgLQO11+s0dibDOCmT0qsklF0+eCGpakb1GB9HbUUNgkUmLADtkwe0qcwHF8Iwseo6Bpeusq8jazDStfGiAnlW35NdgZVn7raVQKagPRuTd3w= 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 1749560775427190.06228574716636; Tue, 10 Jun 2025 06:06:15 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010792.1389074 (Exim 4.92) (envelope-from ) id 1uOyfs-0007H7-9J; Tue, 10 Jun 2025 13:05:56 +0000 Received: by outflank-mailman (output) from mailman id 1010792.1389074; Tue, 10 Jun 2025 13:05:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfr-0007Fz-Mb; Tue, 10 Jun 2025 13:05:55 +0000 Received: by outflank-mailman (input) for mailman id 1010792; Tue, 10 Jun 2025 13:05: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 1uOyfp-0004Sm-E0 for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:53 +0000 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [2a00:1450:4864:20::631]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a2a0ba8a-45fb-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 15:05:51 +0200 (CEST) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-ade30256175so732559266b.1 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: a2a0ba8a-45fb-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560751; x=1750165551; 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=oVaSdEzC4kuCiD3ZiuCTTzD/g+ai4zWkigRCI0Mr+vI=; b=MWJGlQtF4LJiNfUNvmSLhz2vdmMcmBxnWqAjqQvbzm87PH6H9SnKpmbzXBbjahBj5R UtiwxXYLsSLKYZGDsYuCfrnZj7y8gIEm9kP9ggwHkBHOQU3BeE5DCZZWWfAbhnPP1rNq PONijfB0kTAz05H82T7jDDnP2QboPZbsSN+B6IFsj2kK3MEJsmsn12pVNFG7yeqv25k/ ZEbslCg9jRKJp77S50eUa+CEZeSt6O3wZsaXo8jhDIJk6OrdQLzIMuirHLz5tCbtKJrx VCDgv+n5Pmq5PeGQ9BeSrudVLB0oaM2WE5XDBFgJPh/z1+ayB0QE1BD1BZYRYpcj2YQ9 g4Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560751; x=1750165551; 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=oVaSdEzC4kuCiD3ZiuCTTzD/g+ai4zWkigRCI0Mr+vI=; b=fRvR4XvaUyg5zIn3vqjM8i/0mGrYNdokxduW2uBUbCwfLsDmQiATf/Xn7LnEhcoK7F IS6Nrd8OCt1CNgCuLhJlc9gtpNSWIrBCTDgTXOLnpfS9RTEMya5UN1uw516tKXAiVNoY p0HxiZL5vIX8qthvmoxO52jJNKWpvKG23iY/Odh8e8bNpe5CWSrX7TP8ucWxPqMYvvFC 05yvfYlMrtJ0r+nZJC0TJzI9SXyU2aNrAhtWY2vUr8zbcevNnqPD7wC17Z76w0RCloEm 5VXxHdkLnfZYCuOTSaH5gswLxB3NeV/hlJHeRO7x1JBPFGLQR7SXIXyE8dgbMABDGLPJ ZjfA== X-Gm-Message-State: AOJu0YwPA4Up0/rqQxF6gfMqUqEEA9Z3vudrvWKq9atVZsDUA9tqvFC0 Yhx7iHigu4kZeIzXXPhVctKm9aF0x2/aM2ovfSPCgnjtrbv7ipKQlxXwfy1G8g== X-Gm-Gg: ASbGnctxsKx7td2q2cKr2y8fidJAzStxlsPeTh2Z7VFA5g21vyqrTXZGAC65X/vpAAb WrV2YTv0azGGYZ9mTBS/muuzTSH738MYrIQW8iXNL0Nz2Pm39HLhD0Ntm4VUxikWdnCGhegxDAJ /Z/7LL0cBWPmV8zHDTYhQsRTn9C0VS+V8WAZSuqeOcuifnaOCXaHjoKgX/QVBbcJwPjzjS/91Rv Q3zklYPvxDW94aLIxf+gj/+79i5ivmKIZR4vumOhi2pvEcffgRPso1L/gaSZ/dYIHdxxcZiU6Bx uHtZ94m0OkJirr+XSWXbLl3PwMC3oXWzLZK5sM0Dptahs+dq/aKZbJc8+B6L5wIEMwq3TrVI93E loVdU4mCHU6E7UzRBpDOtpM/9aHiG X-Google-Smtp-Source: AGHT+IE57NqUZYYdmSJcp44+MSwcLpDkmkm3jzcquze2ekyofX7OKjw2jn8+5NYp/0z4UB7CpLhpug== X-Received: by 2002:a17:907:c15:b0:adb:3272:7df5 with SMTP id a640c23a62f3a-ade1a93bb89mr1797332166b.16.1749560750536; Tue, 10 Jun 2025 06:05: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 v2 09/17] xen/riscv: introduce page_set_xenheap_gfn() Date: Tue, 10 Jun 2025 15:05:24 +0200 Message-ID: <186e4a778a6dfab205428dfb4d0c59584a162a9f.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560776908124100 Content-Type: text/plain; charset="utf-8" Introduce page_set_xenheap_gfn() helper to encode the GFN associated with a Xen heap page directly into the type_info field of struct page_info. Introduce a GFN field in the type_info of a Xen heap page by reserving 10 bits (sufficient for both Sv32 and Sv39+ modes), and define PGT_gfn_mask and PGT_gfn_width accordingly. This ensures a consistent bit layout across all RISC-V MMU modes, avoiding the need for mode-specific ifdefs. Signed-off-by: Oleksii Kurochko --- 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 | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index 912bc79e1b..41bf9002d7 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 =20 extern vaddr_t directmap_virt_start; @@ -229,9 +230,21 @@ 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) + +/* + * Sv32 has 22-bit GFN. Sv{39, 48, 57} have 44-bit GFN. + * Thereby we can use for `type_info` 10 bits for all modes, having the sa= me + * amount of bits for `type_info` for all MMU modes let us avoid introduci= ng + * an extra #ifdef to that header: + * if we go with maximum possible bits for count on each configuration + * we would need to have a set of PGT_count_* and PGT_gfn_*). + */ +#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 @@ -283,6 +296,19 @@ static inline bool arch_mfns_in_directmap(unsigned lon= g mfn, unsigned long nr) =20 #define PFN_ORDER(pg) ((pg)->v.free.order) =20 +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.49.0 From nobody Fri Oct 31 03:56:15 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=1749560769; cv=none; d=zohomail.com; s=zohoarc; b=GEFxThxnRnOxj+zb2YBKO2+VzQkf6JPGuvBFv0eDtmbmkGI+mSMI6vZUGl1H496+bXYBvrEJAwxeI6tYKjRgv1bBs6SsqUov68CO0kn9fUx4C2UnMlxMi7VWq+P5WI8bQYMXzAHEKUzddFTl5S3RzmLl0AbzKbyw84fY+e+NOFc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560769; 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=gNkxZ42334+z6GmMxmxS+7HSKZw12YWQKs0/M8Vj5io=; b=Pu/k5Cyx35e183Kv3aM+sGCw0EAq5+5zaoqGlEmUvKPzsJ7ho8qS0EAEg1LQLBtdte7w2XoWTy8IPbXp2RQVEtmHVCb2eU0cWlUVhb22FZMbRBxrqJaVWB0+xndWOenvjkw2mAfA9nN7zsGhwPAL2AIHEM1wb3VotJZ8x6K6Ids= 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 1749560769644143.00060980755575; Tue, 10 Jun 2025 06:06:09 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010791.1389067 (Exim 4.92) (envelope-from ) id 1uOyfr-0007Cf-IA; Tue, 10 Jun 2025 13:05:55 +0000 Received: by outflank-mailman (output) from mailman id 1010791.1389067; Tue, 10 Jun 2025 13:05:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfr-0007BD-8R; Tue, 10 Jun 2025 13:05:55 +0000 Received: by outflank-mailman (input) for mailman id 1010791; Tue, 10 Jun 2025 13:05:53 +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 1uOyfp-0004iH-5G for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:53 +0000 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [2a00:1450:4864:20::52c]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a3336bcd-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:52 +0200 (CEST) Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-60779962c00so5213372a12.0 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: a3336bcd-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560752; x=1750165552; 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=gNkxZ42334+z6GmMxmxS+7HSKZw12YWQKs0/M8Vj5io=; b=VXGKD+3BuvSfjXDuVxFLUGSX/qdS+TkT9hIZOt5rs6BrZaMMYU3mLvV6mlqefJja7l n+XYMCFxUHGLsTB4L86733oc/5PfGcPh5mquKlLF+etXoiAMBGfr3lWX9GkQlNik5V/r rUmz3rzn3JMA9kc2Lmve4DdYEXThVRQk5msgRMcggnxe3zVDMvDXq/YaVijQ26R2pBw/ G16s/YUGOyRojK4OHDBQzegbfRBBwHYpZS/lJOxYva0weBaMWMSI22O+7s/s8+E/WO5R QYL+P9oRcmDlqUFrULQHACAG20dxdynJaf6YCeDuvAPwcE4yqHIGYIGR5FF8rXDuJuSQ RHSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560752; x=1750165552; 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=gNkxZ42334+z6GmMxmxS+7HSKZw12YWQKs0/M8Vj5io=; b=BbsvKjPZKSFve0fqlJm6ATTN5IpTUAid6+sVt0GQhGf4zC4os82M0wTIoHs4V6/8XZ J14exGyFZrxcr8u4e9bXKIJdoap2VvN81SMQrtPGhPOE4oPnLp/JZajn7xVqCnFiJ9j9 BYtDJHrFmoKUGS+aQ3h0KTNAK6fjmuScrHsnLXJDoRqMpXVqxL2wW6oE2E5R6O3iZkUS Vfe6GKL3frf3B2Nn0mQwokcM8LQ1VKuCoR2t/K918oRbbuLlaA7bzHcLrIHp3AfbUg+i PwwH2x3K/pcfhZXtgFL1WvLnPxWgpLnJqOUh5xDZ+5IsylMIAxxLSiUAu/YSiW96G+c3 wAXw== X-Gm-Message-State: AOJu0Yz6xb9nstFqwbX+yjfaiNNulmPOASU4GEZTcz+BbsLieuBIZi8r io+iEQtWkOBLi+w06ar9VMI+Gli6nqzou6ezIXng9vlgyPI3QNhQ6JPlgz87Cw== X-Gm-Gg: ASbGncsOiyY6ulD8EfGY0R5JCWWX2gmq5zF/lOsA8vkq4KI4qfHpl57ojpzyt9zPl0V U240fYchdxPi+VS6gMzbnDChWlQtBeTQQVr6QBhTzf7w8gDktv0punB0+jz+YwkLYr4QG9CwbFQ 2dWFKs7FS5+vVGwlCLFIUv4j44HxqDsEWTdhEriCkHkwR6z3Dlvvyr/v7dhBoNQsV+vtzX5Iifj BxFeT53aK94185MhnX0G7ur19z7+rzhf2w5OB38MUFi5rGVXr6eXNYlDTFbYkLf6B8e0ktZ66sD HzJQRb2hVo3o7VaClcJI1aOenuSx0Gecusc0TzCFY3MxG8PvclAzrIMluBWhWlv6pGTX0fY6peC QBerDTnWltJzb/ufbwL24ogLStaI4 X-Google-Smtp-Source: AGHT+IEwUk/N6nuaSDBPQ9NVq1w1pYsClr/9Kn8IdxMkNjnhzTDXDYOmLNhr3sE9Kt5YGObsT2BNMQ== X-Received: by 2002:a17:907:9349:b0:ad2:5499:7599 with SMTP id a640c23a62f3a-ade1a93baf1mr1471045166b.18.1749560751517; Tue, 10 Jun 2025 06:05: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 v2 10/17] xen/riscv: implement guest_physmap_add_entry() for mapping GFNs to MFNs Date: Tue, 10 Jun 2025 15:05:25 +0200 Message-ID: <0b1f7ead7eb1b7c8687d388cca50b46eefb8e408.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560770799124100 Content-Type: text/plain; charset="utf-8" Introduce an initial implementation of guest_physmap_add_entry() on RISC-V by adding a basic framework to insert guest physical memory mappings. This allows mapping a range of GFNs to MFNs using a placeholder p2m_set_entry() function, which currently returns -EOPNOTSUPP. Changes included: - Promoting guest_physmap_add_entry() from a stub to a functional interface calling a new p2m_insert_mapping() helper. - Adding map_regions_p2mt() for generic mapping purposes. - Introducing p2m_insert_mapping() and a skeleton for p2m_set_entry() to prepare for future support of actual page table manipulation. - Enclosing the actual mapping logic within p2m_write_lock() / p2m_write_unlock() to ensure safe concurrent updates to the P2M. Signed-off-by: Oleksii Kurochko --- 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 | 12 ++++------ xen/arch/riscv/p2m.c | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index 0c05b58992..af2025b9fd 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -118,14 +118,10 @@ 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; -} +int guest_physmap_add_entry(struct domain *d, + gfn_t gfn, mfn_t mfn, + unsigned long page_order, + p2m_type_t t); =20 /* Untyped version for RAM only, for compatibility */ static inline int __must_check diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 2419a61d8c..cea37c8bda 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -324,3 +324,44 @@ int p2m_set_allocation(struct domain *d, unsigned long= pages, bool *preempted) =20 return 0; } + +static int p2m_set_entry(struct p2m_domain *p2m, + gfn_t sgfn, + unsigned long nr, + mfn_t smfn, + p2m_type_t t, + p2m_access_t a) +{ + return -EOPNOTSUPP; +} + +static int p2m_insert_mapping(struct domain *d, gfn_t start_gfn, + unsigned long nr, mfn_t mfn, p2m_type_t t) +{ + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + int rc; + + p2m_write_lock(p2m); + rc =3D p2m_set_entry(p2m, start_gfn, nr, mfn, t, p2m->default_access); + 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(d, gfn, nr, mfn, p2mt); +} + +int guest_physmap_add_entry(struct domain *d, + gfn_t gfn, + mfn_t mfn, + unsigned long page_order, + p2m_type_t t) +{ + return p2m_insert_mapping(d, gfn, (1 << page_order), mfn, t); +} --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560773; cv=none; d=zohomail.com; s=zohoarc; b=B9dy7/oW/PRoSxjtMdHGYFeOHzC6y5O66bTJ/pFt+iooKXmsW3tUp+Qyf8Kkld0bBDNJIQ83wtMvKiTnvEzOeQz/8V+owge+eq0npXNUMp0ldR+ezaTj/FK+ChQEHBOQGZNqBnAbmsPhpptdTAyddClOqb7uiZAT0cZb3vucQB0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560773; 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=QKgMuqwtGlngKUtxs7WkqY46iwjdHqw/qZ4SKOhC8y4=; b=XtJzGWlSWK39B41XejmEQl19C+ewppV/1BJ4nBn5P1Sb0Q0BVSReJi0niH7BG0m3TOD5xG1W+uU/71x1tKgUJuZSSVqps/ulBWatF9B1bs775PRkyQOpN4xb5N7Rroqf/G4H9J57FDi1OpW7V08RB8wOCm69GflkUSoIZ3GEYQ8= 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 1749560773379426.680865541553; Tue, 10 Jun 2025 06:06:13 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010794.1389086 (Exim 4.92) (envelope-from ) id 1uOyfu-0007vB-PT; Tue, 10 Jun 2025 13:05:58 +0000 Received: by outflank-mailman (output) from mailman id 1010794.1389086; Tue, 10 Jun 2025 13:05: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 1uOyfu-0007to-Gh; Tue, 10 Jun 2025 13:05:58 +0000 Received: by outflank-mailman (input) for mailman id 1010794; Tue, 10 Jun 2025 13:05:56 +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 1uOyfr-0004Sm-Qm for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:55 +0000 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [2a00:1450:4864:20::62f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a3dcafd2-45fb-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 15:05:53 +0200 (CEST) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-ad8826c05f2so1032951666b.3 for ; Tue, 10 Jun 2025 06:05:53 -0700 (PDT) Received: from fedora.. (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: a3dcafd2-45fb-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560753; x=1750165553; 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=QKgMuqwtGlngKUtxs7WkqY46iwjdHqw/qZ4SKOhC8y4=; b=FCYOwUWE5szHaOoORZcTt73x45EFNkV4QLHrSyKYkZbG3w5SHTWV4FpAqhPyAmYvNg rCCpTdiHizF6Iny8XLhDnYEBmda18G5L8B74IruRMx/i6q1o8ngfziI4BXc8YDraI4Tj 0wnu3KliP8nvFke7MOA9qrF817Bih7HylSab13tBv+bytU2/i0iiIbBUExdvNtGdhmSl 8kUCT9XjCaCLGTbf9x5j2FUOKTmTt8BkonCt/XogV3yfk05a04N5aCH5oW9WPstOYRzS 8Ow1u7oBbzjaAzUsex6AVE1d+jkGkkvmaNN7zpdnx2F1t/bUbC2/rdk3te5XZv3K0Tka E4sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560753; x=1750165553; 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=QKgMuqwtGlngKUtxs7WkqY46iwjdHqw/qZ4SKOhC8y4=; b=ndbSuFoOLR8Nlp4eNG+nR1fFSNalnlI3ZdBcndB/Ew/GIqj27Zu/ZgHmlcZDItyb/I B6LpLZhGAi8KlGuPFn1GLfb4nXNSN3X0Ck9U2Wel3M4sqY1jfJdw0yEHLwYE2nJR5ODQ PNLU3grAI28L5ipg2Kp/XnUwYCofaMSBiG03+xMNpJT8beGilZnjYiOPJA9fRvXnAVsZ zb9L6mUGdn8riBmW7+BbXrw+oHd5oKHEvW2kh01rsVUT3fnIhTT/vP3GUvsqvOO4igjp grByP+f+0XdplANrbHNAiDUL/Ymda3Z9ANrDtGzrZlJjvV8LS7RvZaFnGLeUdh8bTkbC SK8A== X-Gm-Message-State: AOJu0YxQCmFfXU8rkrv3Q9OU4Z0bBBGy34lrx0QOB0IjhnG/DNx+ZwwU 82aPmg/CpblRyqo4i2/A97mfA6RsKWei7+HbsloaLOKYH8VD/xewaQK7yqSg+w== X-Gm-Gg: ASbGncvfl+NAmvZ19tXQCMqHhZpiDgTvv/YgW/r3Op9nqz/ZP04IpaeQ6lcK0DJr9H+ aBz5PF6ZRZovXeD1Or6pJmFcjUkAmHEkzAh5JWKcfGgpuwo06oXgRah3uFpzTd+kGdSOgo+yebY R5V/6z+U/sZkJqXlQlJNWPm5Nmp4dKlBrQf3q0StYqKmoCIp9/2NeQ3kdbGjw9tuqYzXA7usLH8 rATYD6swroaHwZUTNT8zfhftr7UlQNYMdKEDsFv9GIv4shFLHtjEG8BnTA6gzxkduQsOZv+8Q9f YBek2xmlxRDrJYblqlSUbANW2sXD7UVRYcQSWjMSF+m7dwBQWIsOrWB6ezAYdPXvsyAz3oNOaZi bKPmQhsSG9VjwIRMQPqFQ1vTBP5s0 X-Google-Smtp-Source: AGHT+IE/MrshjTl9H2fGWoqTEfbobX3NFO8EvJMxaYOPyEN628kL3PsU5h5YcCUYk6w2ymkbH+oNww== X-Received: by 2002:a17:907:d16:b0:ad9:f54f:70a2 with SMTP id a640c23a62f3a-ade1aa07b5fmr1528915966b.22.1749560752677; Tue, 10 Jun 2025 06:05:52 -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 v2 11/17] xen/riscv: implement p2m_set_entry() and __p2m_set_entry() Date: Tue, 10 Jun 2025 15:05:26 +0200 Message-ID: <7826732d2f6aa5888758942165f0f1e73fa84ffa.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560774900124100 Content-Type: text/plain; charset="utf-8" This patch introduces p2m_set_entry() and its core helper __p2m_set_entry()= for RISC-V, based loosely on the Arm implementation, with several RISC-V-specif= ic modifications. Key differences include: - TLB Flushing: RISC-V allows caching of invalid PTEs and does not require break-before-make (BBM). As a result, the flushing logic is simplified. TLB invalidation can be deferred until p2m_write_unlock() is called. Consequently, the p2m->need_flush flag is always considered true and is removed. - Page Table Traversal: The order of walking the page tables differs from A= rm, and this implementation reflects that reversed traversal. - Macro Adjustments: The macros P2M_ROOT_LEVEL, P2M_ROOT_ORDER, and P2M_ROOT_PAGES are updated to align with the new RISC-V implementation. The main functionality is in __p2m_set_entry(), which handles mappings alig= ned to page table block entries (e.g., 1GB, 2MB, or 4KB with 4KB granularity). p2m_set_entry() breaks a region down into block-aligned mappings and calls __p2m_set_entry() accordingly. Stub implementations (to be completed later) include: - p2m_free_entry() - p2m_next_level() - p2m_entry_from_mfn() - p2me_is_valid() 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 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 | 16 ++ xen/arch/riscv/p2m.c | 260 ++++++++++++++++++++++++++++++- 2 files changed, 275 insertions(+), 1 deletion(-) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index af2025b9fd..fdebd18356 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -9,8 +9,13 @@ #include #include =20 +#include #include =20 +#define P2M_ROOT_LEVEL HYP_PT_ROOT_LEVEL +#define P2M_ROOT_ORDER XEN_PT_LEVEL_ORDER(P2M_ROOT_LEVEL) +#define P2M_ROOT_PAGES BIT(P2M_ROOT_ORDER, U) + #define paddr_bits PADDR_BITS =20 /* Get host p2m table */ @@ -49,6 +54,17 @@ struct p2m_domain { =20 /* Current VMID in use */ uint16_t vmid; + + /* 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 update to track recall 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 cea37c8bda..27499a86bb 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -231,6 +231,8 @@ int p2m_init(struct domain *d) INIT_PAGE_LIST_HEAD(&p2m->pages); =20 p2m->vmid =3D INVALID_VMID; + p2m->max_mapped_gfn =3D _gfn(0); + p2m->lowest_mapped_gfn =3D _gfn(ULONG_MAX); =20 p2m->default_access =3D p2m_access_rwx; =20 @@ -325,6 +327,214 @@ int p2m_set_allocation(struct domain *d, unsigned lon= g 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 of 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 bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte) +{ + panic("%s: isn't implemented for now\n", __func__); + + return false; +} + +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_remove_pte(pte_t *p, bool clean_pte) +{ + pte_t pte; + + memset(&pte, 0x00, sizeof(pte)); + p2m_write_pte(p, pte, clean_pte); +} + +static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, + p2m_type_t t, p2m_access_t a) +{ + panic("%s: hasn't been implemented yet\n", __func__); + + return (pte_t) { .pte =3D 0 }; +} + +#define GUEST_TABLE_MAP_NONE 0 +#define GUEST_TABLE_MAP_NOMEM 1 +#define GUEST_TABLE_SUPER_PAGE 2 +#define GUEST_TABLE_NORMAL 3 + +/* + * Take the currently mapped table, find the corresponding GFN entry, + * and map the next table, if available. The previous table will be + * unmapped if the next level was mapped (e.g GUEST_TABLE_NORMAL + * returned). + * + * `alloc_tbl` parameter indicates whether intermediate tables should + * be allocated when not present. + * + * Return values: + * GUEST_TABLE_MAP_NONE: a table allocation isn't permitted. + * GUEST_TABLE_MAP_NOMEM: allocating a new page failed. + * GUEST_TABLE_SUPER_PAGE: next level or leaf mapped normally. + * GUEST_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 GUEST_TABLE_MAP_NONE; +} + +/* Free pte sub-tree behind an entry */ +static void p2m_free_entry(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 sgfn, + unsigned int page_order, + mfn_t smfn, + p2m_type_t t, + p2m_access_t a) +{ + 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(smfn, INVALID_MFN); + DECLARE_OFFSETS(offsets, gfn_to_gaddr(sgfn)); + + ASSERT(p2m_is_write_locked(p2m)); + + /* + * Check if the level target is valid: we only support + * 4K - 2M - 1G mapping. + */ + ASSERT(target <=3D 2); + + table =3D p2m_get_root_pointer(p2m, sgfn); + 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 GUEST_TABLE_MAP_NONE) || (rc =3D=3D GUEST_TABLE_MA= P_NOMEM) ) + { + /* + * 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 : -ENOENT; + goto out; + } + else if ( rc !=3D GUEST_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; + + /* + * The access type should always be p2m_access_rwx when the mapping + * is removed. + */ + ASSERT(!mfn_eq(INVALID_MFN, smfn) || (a =3D=3D p2m_access_rwx)); + + if ( removing_mapping ) + p2m_remove_pte(entry, p2m->clean_pte); + else { + pte_t pte =3D p2m_entry_from_mfn(p2m, smfn, t, a); + + p2m_write_pte(entry, pte, p2m->clean_pte); + + p2m->max_mapped_gfn =3D gfn_max(p2m->max_mapped_gfn, + gfn_add(sgfn, (1UL << page_order) - = 1)); + p2m->lowest_mapped_gfn =3D gfn_min(p2m->lowest_mapped_gfn, sgfn); + } + +#ifdef CONFIG_HAS_PASSTHROUGH + if ( is_iommu_enabled(p2m->domain) && + (pte_is_valid(orig_pte) || pte_is_valid(*entry)) ) + { + unsigned int flush_flags =3D 0; + + if ( pte_is_valid(orig_pte) ) + flush_flags |=3D IOMMU_FLUSHF_modified; + if ( pte_is_valid(*entry) ) + flush_flags |=3D IOMMU_FLUSHF_added; + + rc =3D iommu_iotlb_flush(p2m->domain, _dfn(gfn_x(sgfn)), + 1UL << page_order, flush_flags); + } + else +#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 ( p2me_is_valid(p2m, orig_pte) && + !mfn_eq(pte_get_mfn(*entry), pte_get_mfn(orig_pte)) ) + p2m_free_entry(p2m, orig_pte, level); + +out: + unmap_domain_page(table); + + return rc; +} + static int p2m_set_entry(struct p2m_domain *p2m, gfn_t sgfn, unsigned long nr, @@ -332,7 +542,55 @@ static int p2m_set_entry(struct p2m_domain *p2m, p2m_type_t t, p2m_access_t a) { - return -EOPNOTSUPP; + int rc =3D 0; + + /* + * 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 -ENOMEM; + + while ( nr ) + { + unsigned long mask; + unsigned long order =3D 0; + /* 1gb, 2mb, 4k mappings are supported */ + unsigned int i =3D ( P2M_ROOT_LEVEL > 2 ) ? 2 : P2M_ROOT_LEVEL; + + /* + * Don't take into account the MFN when removing mapping (i.e + * MFN_INVALID) to calculate the correct target order. + * + * XXX: Support superpage mappings if nr is not aligned to a + * superpage size. + */ + mask =3D !mfn_eq(smfn, INVALID_MFN) ? mfn_x(smfn) : 0; + mask |=3D gfn_x(sgfn) | nr; + + for ( ; i !=3D 0; i-- ) + { + if ( !(mask & (BIT(XEN_PT_LEVEL_ORDER(i), UL) - 1)) ) + { + order =3D XEN_PT_LEVEL_ORDER(i); + break; + } + } + + rc =3D __p2m_set_entry(p2m, sgfn, order, smfn, t, a); + if ( rc ) + break; + + sgfn =3D gfn_add(sgfn, (1 << order)); + if ( !mfn_eq(smfn, INVALID_MFN) ) + smfn =3D mfn_add(smfn, (1 << order)); + + nr -=3D (1 << order); + } + + return rc; } =20 static int p2m_insert_mapping(struct domain *d, gfn_t start_gfn, --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560778; cv=none; d=zohomail.com; s=zohoarc; b=FoqpStWv170PM3lCP3FVImQM/GUNWr6lvxeDFvyJEvNUdgKnK6NMThuFftIiAiqHULJ6o2wPGFgQKBr1djC3Fc2B0AlhEawpdP5KFtr4Q787swFrMF1MTourkGNAsvC0nPQ34FYHJvfClaHMjSdHRsUHUrij2YUSGetUgNMuAtw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560778; 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=isW7H3XxNDJXOKkXKD+BdxkPdas0tvZOP6WE8bHGH6A=; b=Tjw5S9lvkPK/MTai5hxjnplAMpcn5wtNK5oOC5qNTunLo1uqDTZp9SK6WTEJoa6jeKenVC5t/h7MCgffMouNMDQOHzucWbe+nlObOfDwynU8rxqCRfJ1eILsAEXkUOf7iU14jbwkPMacelDTQhPMI2HrAMX587OlHbQ7q6IGgTI= 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 1749560778620734.9935984550907; Tue, 10 Jun 2025 06:06:18 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010796.1389094 (Exim 4.92) (envelope-from ) id 1uOyfv-00083l-Nx; Tue, 10 Jun 2025 13:05:59 +0000 Received: by outflank-mailman (output) from mailman id 1010796.1389094; Tue, 10 Jun 2025 13:05:59 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfv-00081x-Bb; Tue, 10 Jun 2025 13:05:59 +0000 Received: by outflank-mailman (input) for mailman id 1010796; Tue, 10 Jun 2025 13:05:57 +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 1uOyfs-0004Sm-Qb for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:56 +0000 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [2a00:1450:4864:20::62d]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a4859e46-45fb-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 15:05:54 +0200 (CEST) Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-ade76b8356cso198040066b.2 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05:53 -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: a4859e46-45fb-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560754; x=1750165554; 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=isW7H3XxNDJXOKkXKD+BdxkPdas0tvZOP6WE8bHGH6A=; b=J8Y4UlPzelSPjdd0gYIr+u56Vnc7QtThbAhLkFEQf5yZ4xEnez1elnd7+T4pIdvvjk LzGQjSAzQljlLcfeb5ywMyRHw6sA7GAKrMmtVUVK8/S+LGHFMhesK5CMJkBdodCd35+a R9Xog8JXZxpdixUJQ4pNToPvDQJaFaJWw0zajk1wcctoNMKq8Y2C9yKxDozRQ6ioQtro RyQX4hDtq4zGBMOXZ8rz27VQvLB2LS5HAalREPhCdmHROI7veb3sADsbEEfUX+ptT7Av iRjvDk4RlO7vYjFGC24z0RuBkDjGDT9sFA3V/q+wGPWvAF6b1WJvswCOxvsIm4dID1XY 0C2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560754; x=1750165554; 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=isW7H3XxNDJXOKkXKD+BdxkPdas0tvZOP6WE8bHGH6A=; b=T9lpwnK3Rl9zirpfe3/eLONaZWcszJZKupGh6h4Eshj57MGpkhYlvooqJREITRN2LI +CHOhIouIMxhKGSvndIewbIp2CykwxQ489tbgidEfPysp1wWP23OQHI4Kz4RjlBL6oAi DhJC+x1A2rN1Z0HVfaVYQ33v0sA/3mPJY4l+Q2CDWIH6PB/QX95jdsugS9eGp8FSH7B2 y3EfBbSh/Zt3Ksfrvug+oO17UcIxae533WsJfzbaLhG5kYX+VGIrzI3NGqTnbMcP5r+z SAbaUnxu3pFDPQaRMD7KBu6q0he9fnQlpUTIVsjrLozF3XrC2HM35sshIBQ/lcdwQMGu g7Dw== X-Gm-Message-State: AOJu0YwXeLLXJz2sXSgWY8kceDwW93NUIKdtc5BDZAgoDdCrhSSb86N5 OKMXl0Bsh+fIJEWiyy6zEIf/hrH/x0sEKMSHfq5vc6nMECqPJRmb9lOIbpWhEA== X-Gm-Gg: ASbGncsI/euk7tuhEvOiqFKZ43r7vsPl2TY6LI3B/RgGg6YLNr5lkURl1KYwBDkkKgf C7BznwatN+dbuZ0+9E22xAGI/+iNiZBxSIGVkkmISBHaHBmf2gwfrMd1Dyw0gXieIZmQ5eKv57u Eqcu3BG4cKvGHjWgE1yDEuDcZDNi4qa1gJ7z6I5f6Lna7jrbIsHKYp7p04fBKVnz291x06NkrbL 0fYx0BO37/Hvr6gFBZLVfZH77RdXFIj+myczfNpdVR964ENKlZaJtReDmd42eX1D7w4214FcMIF 7eqWUl9Kf6/XiXIKAun9ud2m3iwV+ox3u015Jtyyj4+J+7u1QbXXieoeodr35fH8CPzNq9qpNI1 IhSiGH6/T7SCGW/H33LTjL/zL0r2B X-Google-Smtp-Source: AGHT+IEBQT2r9op963zJAFCCDKLZ1on4ycT0PLvFnyWKzzTEm0ROTgDFTDJZYhRR0a9pcYdIZ+ajbQ== X-Received: by 2002:a17:907:1c8e:b0:ad8:a41a:3cd2 with SMTP id a640c23a62f3a-ade1a9058e7mr1682029166b.16.1749560753609; Tue, 10 Jun 2025 06:05: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 v2 12/17] xen/riscv: Implement p2m_free_entry() and related helpers Date: Tue, 10 Jun 2025 15:05:27 +0200 Message-ID: <67148a7cceffdcbc5d8a51cd6af0a0c83e5b93bc.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560780948124100 Content-Type: text/plain; charset="utf-8" This patch introduces a working implementation of p2m_free_entry() for RISC= -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: - Use p2m_force_flush_sync() instead of p2m_tlb_flush_sync() as latter isn't implemented on RISC-V. - Introduce and use p2m_type_radix_get() 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 in a radix tree. Key additions include: - p2m_free_entry(): 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_type_radix_get(): Extracts the stored p2m_type from the radix tree using the PTE. - p2m_free_page(): Returns a page either to the domain's freelist or to the domheap, depending on whether the domain is hardware-backed. Defines XEN_PT_ENTRIES in asm/page.h to simplify loops over page table entries. Signed-off-by: Oleksii Kurochko --- 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_superpage/p2me_is_superpage. --- xen/arch/riscv/include/asm/page.h | 1 + xen/arch/riscv/p2m.c | 144 +++++++++++++++++++++++++++++- 2 files changed, 142 insertions(+), 3 deletions(-) diff --git a/xen/arch/riscv/include/asm/page.h b/xen/arch/riscv/include/asm= /page.h index 1b8b145663..c67b9578c9 100644 --- a/xen/arch/riscv/include/asm/page.h +++ b/xen/arch/riscv/include/asm/page.h @@ -22,6 +22,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: diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 27499a86bb..6b11e87b22 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -345,11 +345,33 @@ 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_type_radix_get(struct p2m_domain *p2m, pte_t pte) +{ + void *ptr; + gfn_t gfn =3D mfn_to_gfn(p2m->domain, mfn_from_pte(pte)); + + ptr =3D radix_tree_lookup(&p2m->p2m_type, gfn_x(gfn)); + + if ( !ptr ) + return p2m_invalid; + + return radix_tree_ptr_to_int(ptr); +} + +/* + * In the case of the P2M, the valid bit is used for other purpose. Use + * the type to check whether an entry is valid. + */ static inline bool p2me_is_valid(struct p2m_domain *p2m, pte_t pte) { - panic("%s: isn't implemented for now\n", __func__); + return p2m_type_radix_get(p2m, pte) !=3D p2m_invalid; +} =20 - return false; +static inline bool p2me_is_superpage(struct p2m_domain *p2m, pte_t pte, + unsigned int level) +{ + return p2me_is_valid(p2m, pte) && (pte.pte & PTE_ACCESS_MASK) && + (level > 0); } =20 static inline void p2m_write_pte(pte_t *p, pte_t pte, bool clean_pte) @@ -404,11 +426,127 @@ static int p2m_next_level(struct p2m_domain *p2m, bo= ol alloc_tbl, return GUEST_TABLE_MAP_NONE; } =20 +static void p2m_put_foreign_page(struct page_info *pg) +{ + /* + * It's safe to do the put_page here because page_alloc will + * flush the TLBs if the page is reallocated before the end of + * this loop. + */ + 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 */ + + /* Detect the xenheap page and mark 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(struct p2m_domain *p2m, const pte_t pte, + unsigned int level) +{ + mfn_t mfn =3D pte_get_mfn(pte); + p2m_type_t p2m_type =3D p2m_type_radix_get(p2m, pte); + + ASSERT(p2me_is_valid(p2m, 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. + */ + if ( level =3D=3D 1 ) + return p2m_put_2m_superpage(mfn, p2m_type); + else if ( level =3D=3D 0 ) + return p2m_put_4k_page(mfn, p2m_type); +} + +static void p2m_free_page(struct domain *d, struct page_info *pg) +{ + if ( is_hardware_domain(d) ) + free_domheap_page(pg); + else + { + spin_lock(&d->arch.paging.lock); + page_list_add_tail(pg, &d->arch.paging.p2m_freelist); + spin_unlock(&d->arch.paging.lock); + } +} + /* Free pte sub-tree behind an entry */ static void p2m_free_entry(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 ( !p2me_is_valid(p2m, entry) ) + return; + + if ( p2me_is_superpage(p2m, 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_type_radix_get(p2m, entry)) && + domain_has_ioreq_server(p2m->domain) ) + ioreq_request_mapcache_invalidate(p2m->domain); +#endif + + p2m_put_page(p2m, entry, level); + + return; + } + + table =3D map_domain_page(pte_get_mfn(entry)); + for ( i =3D 0; i < XEN_PT_ENTRIES; i++ ) + p2m_free_entry(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_force_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->domain, pg); } =20 /* --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560773; cv=none; d=zohomail.com; s=zohoarc; b=aZX2TxE9LZJiAIBEe8kXDlNLKxqHHetuoEXuJ3iQZt+m+ow0qzmJjd/26UEkyzQW6s19roiDGzxpWqWanAiL2HjCs8o/BXEXmfV0uRwXu/OmIIwiXH0l3f1wbiaxK/zV9FxTNNnLXagi4DHbfmC2XPDqltlLBJulUTX+iA9svU0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560773; 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=tkn+ztvbcliHnyvY4DdG5JO4oKukWMaE/1eHKVZZcwQ=; b=d2OD7ClHhH5GlBglMoD29hgkZnaWqgdIRwb2QnqOEtMbebzFxNlnR49WSvx8hjn4owDXFyM0ja6c1nWQk29m/W21Q9LX8W2P2C/H5SqLAQhXyVf60O24Kc5sQyN9Fgp+H7DXCpOhg6d0Eg1CQ9wiZU96cn1Gw6S+ZyTZZdEW3rU= 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 1749560773595257.7424315116184; Tue, 10 Jun 2025 06:06:13 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010797.1389101 (Exim 4.92) (envelope-from ) id 1uOyfw-0008Da-Ig; Tue, 10 Jun 2025 13:06:00 +0000 Received: by outflank-mailman (output) from mailman id 1010797.1389101; Tue, 10 Jun 2025 13:06:00 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfv-0008CX-WA; Tue, 10 Jun 2025 13:05:59 +0000 Received: by outflank-mailman (input) for mailman id 1010797; Tue, 10 Jun 2025 13:05:57 +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 1uOyft-0004Sm-Qc for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:57 +0000 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [2a00:1450:4864:20::629]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a4ee2047-45fb-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 15:05:55 +0200 (CEST) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-ade4679fba7so484721166b.2 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: a4ee2047-45fb-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560755; x=1750165555; 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=tkn+ztvbcliHnyvY4DdG5JO4oKukWMaE/1eHKVZZcwQ=; b=OEBIKFzvPJhORclc8ZoqwHQENJQ/lL5gZVFXsCrdCtxIYA329GGRHT1p7nGbKkBy7E EZ4w+QTGjLF+Es5WD1wrJpQ2WsFwMeFKhr2j5Om/NAJKW+ZInkgciIioVa154z2zzu51 fd+7DKcZo1QaYXDsBRH6Uus0GZw/bXTbrdlS/dxlGJI4WiuswpHdXPDCtm8nRp0cHPdH xUqrNnt0pq2H/f7GE9Ph/eSehOmGI9CaELTijdsQy/FHeDbaSnXMbNxuGEAah4ZaPgRE jRUrSVZQjl42PmTQK025Y/hG8/plIZb8yDmSGfTv7JFISiEMbArcyBioJo1s3medrdG5 J4aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560755; x=1750165555; 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=tkn+ztvbcliHnyvY4DdG5JO4oKukWMaE/1eHKVZZcwQ=; b=Hv9uuiYjPFh+576Cl9e6xOFE555iJfLged3SUOAnYpwd/nF5p3JD6GfY0UqywbZ/nt S04jf0eADKUaDsP4F4qknGHZBosHk52dOLQG6zKwkDxPuGK6AWgKl+JXlD+UsYniY7Md EFfMzmWXFdCZDu488hq2M1jLJSo7VG4mKpoWs8M7q5PovlpriWi4cVz5oLqDxExQuu7b 74MFvWOogfQOFQNW0d6er4Ab64TD1p+ECYhOsrd/FQaltPXFNH2S+oQ+RQGxnn8t7b28 3j9pMFvSS921tGfMyg/r+kWPkZ+4d0ui9N2MrPW4uFYsb1a3dYqkyzCg2a8LsC9GPi4N 3uVQ== X-Gm-Message-State: AOJu0YzrlqA7Odzr5pXhZXzoPf5XoeIO1LtK1KtxcZ2AZWJNCJx0tTr+ tnFaS/UWoEbNYhLi9DAskmY3gWzHK1vqqWsYRc00J/86u3AB3Ge7H9uHADjdBQ== X-Gm-Gg: ASbGncuHf9KhMwg0TRJb8w2Gn0U+jZyS8KbuHtTiiu7ARSnVGIHRvEkhmPLJS96pfr4 5w2fErqdjJ+nYXYRDmOjSz+OCFdksW3EZacmP3aKdgPNwYn89A8qNdwnEk8O5ZxN8B40CpwQ7TA 4t6bsKuGYqZ8jPP2cwcS1nOvqR0S8KGQjksQu0PgJRph7Ru4jNwZS3KugG6Uqb1js6gY3OPWB53 2Qoi4iQgZ533aIxkCBnuDD1V30rzVCHCmpW7On1FEByjZ8EzXpZgpNFyUMBFWttfsFf02YvPzSi rp06hkY2H9c4DSDlG1CORi3YwiSe+0/whhiJvQTxSUcKM5deKpAKgEV5rS3y6Qzm3QBBplr2nPJ VU4nfW3aXBt9L4uX7W4wwiWLW9n0f X-Google-Smtp-Source: AGHT+IFOKSIAXyNkA2Zfv91wJ8RYYuzvX7isb+GzFl8frSbbvBaTOWNsZDjJc4chk24vwzsmEl4ICg== X-Received: by 2002:a17:907:9721:b0:ad8:af1f:938d with SMTP id a640c23a62f3a-ade1ab1fa84mr1436480066b.37.1749560754641; Tue, 10 Jun 2025 06:05:54 -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 v2 13/17] xen/riscv: Implement p2m_entry_from_mfn() and support PBMT configuration Date: Tue, 10 Jun 2025 15:05:28 +0200 Message-ID: <994ab3dd6822c4cd02a6a576041da115abeff6ed.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560774836124100 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_entry_from_mfn(): Generates a valid PTE using the given MFN, p2m_type_t, and p2m_access_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_type_radix_set(): Stores the p2m type in a radix tree keyed by the PTE for later retrieval. PBMT type encoding support: - Introduces an enum pbmt_type_t to represent the PBMT field values. - Maps types like p2m_mmio_direct_dev to pbmt_io, others default to pbmt_pm= a. Signed-off-by: Oleksii Kurochko --- 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 | 103 ++++++++++++++++++++++++++++-- 2 files changed, 107 insertions(+), 4 deletions(-) diff --git a/xen/arch/riscv/include/asm/page.h b/xen/arch/riscv/include/asm= /page.h index c67b9578c9..1d1054fa5c 100644 --- a/xen/arch/riscv/include/asm/page.h +++ b/xen/arch/riscv/include/asm/page.h @@ -76,6 +76,14 @@ #define PTE_SMALL BIT(10, UL) #define PTE_POPULATE BIT(11, UL) =20 +enum pbmt_type_t { + pbmt_pma, + pbmt_nc, + pbmt_io, + pbmt_rsvd, + pbmt_max, +}; + #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 6b11e87b22..cba04acf38 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -345,6 +345,26 @@ 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_type_radix_set(struct p2m_domain *p2m, pte_t pte, p2m_type_= t t) +{ + int rc; + gfn_t gfn =3D mfn_to_gfn(p2m->domain, mfn_from_pte(pte)); + + rc =3D radix_tree_insert(&p2m->p2m_type, gfn_x(gfn), + radix_tree_int_to_ptr(t)); + if ( rc =3D=3D -EEXIST ) + { + /* If a setting already exists, change it to the new one */ + radix_tree_replace_slot( + radix_tree_lookup_slot( + &p2m->p2m_type, gfn_x(gfn)), + radix_tree_int_to_ptr(t)); + rc =3D 0; + } + + return rc; +} + static p2m_type_t p2m_type_radix_get(struct p2m_domain *p2m, pte_t pte) { void *ptr; @@ -389,12 +409,87 @@ static inline void p2m_remove_pte(pte_t *p, bool clea= n_pte) p2m_write_pte(p, pte, clean_pte); } =20 -static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, - p2m_type_t t, p2m_access_t a) +static void p2m_set_permission(pte_t *e, p2m_type_t t, p2m_access_t a) { - panic("%s: hasn't been implemented yet\n", __func__); + /* First apply type permissions */ + switch ( t ) + { + case p2m_ram_rw: + e->pte |=3D PTE_ACCESS_MASK; + break; + + case p2m_mmio_direct_dev: + e->pte |=3D (PTE_READABLE | PTE_WRITABLE); + e->pte &=3D ~PTE_EXECUTABLE; + break; + + case p2m_invalid: + e->pte &=3D ~PTE_ACCESS_MASK; + break; + + default: + BUG(); + break; + } + + /* Then restrict with access permissions */ + switch ( a ) + { + case p2m_access_rwx: + break; + case p2m_access_wx: + e->pte &=3D ~PTE_READABLE; + break; + case p2m_access_rw: + e->pte &=3D ~PTE_EXECUTABLE; + break; + case p2m_access_w: + e->pte &=3D ~(PTE_READABLE | PTE_EXECUTABLE); + e->pte &=3D ~PTE_EXECUTABLE; + break; + case p2m_access_rx: + case p2m_access_rx2rw: + e->pte &=3D ~PTE_WRITABLE; + break; + case p2m_access_x: + e->pte &=3D ~(PTE_READABLE | PTE_WRITABLE); + break; + case p2m_access_r: + e->pte &=3D ~(PTE_WRITABLE | PTE_EXECUTABLE); + break; + case p2m_access_n: + case p2m_access_n2rwx: + e->pte &=3D ~PTE_ACCESS_MASK; + break; + default: + BUG(); + break; + } +} + +static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m, mfn_t mfn, p2m_typ= e_t t, p2m_access_t a) +{ + pte_t e =3D (pte_t) { 1 }; + + switch ( t ) + { + case p2m_mmio_direct_dev: + e.pte |=3D PTE_PBMT_IO; + break; + + default: + break; + } + + p2m_set_permission(&e, t, a); + + ASSERT(!(mfn_to_maddr(mfn) & ~PADDR_MASK)); + + pte_set_mfn(&e, mfn); + + BUG_ON(p2m_type_radix_set(p2m, e, t)); =20 - return (pte_t) { .pte =3D 0 }; + return e; } =20 #define GUEST_TABLE_MAP_NONE 0 --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560781; cv=none; d=zohomail.com; s=zohoarc; b=ZrfjCr9mrmqMGYOo/vYXmIdGzHsbzSRFw3ZmzUHoOc6Ar+F97pmV1QyawsQgYizw2eNDdGENmFr3nRX8XVy9NUEf0SDpJen0A7gHgEZTIfLg7ZJ1sMQgLUunT1dEqITbHt+uxtjz8ZKMHVyEEicYE73W1gqRgxIgP+BikvDP/Fw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560781; 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=lH1ZHh1K+bIKxqRJUfRmKzme+LOvg3rfA9LFTdq0giM=; b=YYqv0FwE0ICQGSLmqLlUC9EbpBrrVaaSgzP/P9+Wnc3wv/qIvECWzsm2Ui0A00TTBkHZcjMZcVnqc0tEkcw/oGozxnJLp2optowjgQFhIbiYtYMPIHndMs8EvH7MYpEo1qjQ3XwxZmaSqm14+ChTYEMiTPcDVJhK7k4auwtQlFM= 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 1749560781041249.60615635718966; Tue, 10 Jun 2025 06:06:21 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010798.1389106 (Exim 4.92) (envelope-from ) id 1uOyfx-0008Uj-MS; Tue, 10 Jun 2025 13:06:01 +0000 Received: by outflank-mailman (output) from mailman id 1010798.1389106; Tue, 10 Jun 2025 13:06: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 1uOyfx-0008S9-61; Tue, 10 Jun 2025 13:06:01 +0000 Received: by outflank-mailman (input) for mailman id 1010798; Tue, 10 Jun 2025 13:05:58 +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 1uOyft-0004iH-Ue for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:57 +0000 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [2a00:1450:4864:20::52a]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a5fb739d-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:57 +0200 (CEST) Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-60179d8e65fso5067639a12.0 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05:55 -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: a5fb739d-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560757; x=1750165557; 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=lH1ZHh1K+bIKxqRJUfRmKzme+LOvg3rfA9LFTdq0giM=; b=URDUhvExcyXwBpvSfXo2fFeGw8yLDIyhxsYPT3iOQ3XL8BHuLeLZiUigj54B2SKEGd qcKRetIVCbYw5LF/+3oT7xf4hBe3t5jgxQ55tDElhXJI1w8mLd+Y3XBu1LtLnaRBeILJ 6mUPXUUSXExw7bFGiP2OfmoteVmb3LNZzrmcubWGOddqkydsMTRbavwi1f36cQDwMaB9 UWRW3ezjb7qlCK22EbPswmhcInvXw+YsNUspg7yFEUVRJKLf2M92UM3bChZBrFbZa9Ee UCTikrJSn/TepScasCE3xh5bFW181GxB5Eia0uomeShixEOq2XCBx3N9EraT+vgAUrZi ptNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560757; x=1750165557; 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=lH1ZHh1K+bIKxqRJUfRmKzme+LOvg3rfA9LFTdq0giM=; b=UkJZIK4jgLd1W0gjhDM7/aAAyyU1er48Pc1ualwcl8FL2UPho+rQW2LJfQjKtZ2B+A LjAq9ljE9TRHiyn7XNvNy05FKtYa0BBcWzJfXemto2riVwCDGz6Se+ElmXHl3WFZAdt5 MFidSjiEGEA59rAek6t1X7iPRUE2ie1CyLfp5fR5CwY5CnSA3QTs9X1oxgO2bpL5iLns pBLKtfOMhaFFZitcWE6D0HtV4mnsOLVWlm4BRazIHopEAIAaVB0E2kTdNYk6YQy5tOyl zbZudC1W+2DtRApvZaGvbG3HMiyw92dY1pTLzl+kGA3u8LeHyRRmww6Q+2uc73rVYMHa I0AA== X-Gm-Message-State: AOJu0Yxek2ZPd+q4j7InoHWASPgdTgJal443iLetrUv2hQS+z9fY9UJD 1+yT+CLJJxx6oCgf+THgIdAb7t8opsmwOYqDIxdMEQ2FAbgrwYAYCYJkPaHd3g== X-Gm-Gg: ASbGnctcdMEXUq4jYw9/4NK9Bo/FPuPjHKdP8RANoduMBnEEcskFrPs6IKCIm+nB6yM Rg4D4JQwMNguMEqwtjRXCDcAkbBjkxPsWqa6Zb231BCLjTp6C7DNtplwhfhoMYoyM84jqKZ/yxI GoMYZ8Wk+hGLHjI9b+TztPJvoGSh5MbqmoCr7aSIa7thk3xawXULVfns5rnCNzA/tFAsTqvRgGx fCQbo6HbEZOOGsJhHJlt6MGCBj6ZAUJQBLtp1vc3Cz0h/ToPvnjrfqauZq1uN7Vf82ompBMTHIA MQwwGjIO4VXehqk6K3p8KBDVO5Z6MNbu1ygy47hZu6/5b801wyED5KkxjMz6mrD5g5RDg1ptXnD ddP0VvSNV0i0Et2UI631jLFGnmthX X-Google-Smtp-Source: AGHT+IE6DUsF2JRpbY+BbpzpDedxFbnMl0ETK6qltI4CDqfked9uZsa0lA6xeKZ2cn5AVxbQOlyzvg== X-Received: by 2002:a17:907:7fa2:b0:ade:9fb:b07d with SMTP id a640c23a62f3a-ade7ac2cd8emr266695966b.4.1749560755700; Tue, 10 Jun 2025 06:05: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 v2 14/17] xen/riscv: implement p2m_next_level() Date: Tue, 10 Jun 2025 15:05:29 +0200 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560782914124100 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: - p2me_is_mapping(): Determines whether a PTE represents a valid mapping. - 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 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/p2me_is_mapping. --- xen/arch/riscv/p2m.c | 103 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index cba04acf38..87dd636b80 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -387,6 +387,17 @@ static inline bool p2me_is_valid(struct p2m_domain *p2= m, pte_t pte) return p2m_type_radix_get(p2m, pte) !=3D p2m_invalid; } =20 +/* + * pte_is_* helpers are checking the valid bit set in the + * PTE but we have to check p2m_type instead (look at the comment above + * p2me_is_valid()) + * Provide our own overlay to check the valid bit. + */ +static inline bool p2me_is_mapping(struct p2m_domain *p2m, pte_t pte) +{ + return p2me_is_valid(p2m, pte) && (pte.pte & PTE_ACCESS_MASK); +} + static inline bool p2me_is_superpage(struct p2m_domain *p2m, pte_t pte, unsigned int level) { @@ -492,6 +503,70 @@ static pte_t p2m_entry_from_mfn(struct p2m_domain *p2m= , mfn_t mfn, p2m_type_t t, return e; } =20 +/* Generate table entry with correct attributes. */ +static pte_t page_to_p2m_table(struct p2m_domain *p2m, struct page_info *p= age) +{ + /* + * Since this function generates a table entry, according to "Encoding + * of PTE R/W/X fields," the entry's r, w, and x fields must be set to= 0 + * to point to the next level of the page table. + * Therefore, to ensure that an entry is a page table entry, + * `p2m_access_n2rwx` is passed to `mfn_to_p2m_entry()` as the access = value, + * which overrides whatever was passed as `p2m_type_t` and guarantees = that + * the entry is a page table entry by setting r =3D w =3D x =3D 0. + */ + return p2m_entry_from_mfn(p2m, page_to_mfn(page), p2m_ram_rw, p2m_acce= ss_n2rwx); +} + +static struct page_info *p2m_alloc_page(struct domain *d) +{ + struct page_info *pg; + + /* + * For hardware domain, there should be no limit in the number of page= s that + * can be allocated, so that the kernel may take advantage of the exte= nded + * regions. Hence, allocate p2m pages for hardware domains from heap. + */ + if ( is_hardware_domain(d) ) + { + pg =3D alloc_domheap_page(d, MEMF_no_owner); + if ( pg =3D=3D NULL ) + printk(XENLOG_G_ERR "Failed to allocate P2M pages for hwdom.\n= "); + } + else + { + spin_lock(&d->arch.paging.lock); + pg =3D page_list_remove_head(&d->arch.paging.p2m_freelist); + spin_unlock(&d->arch.paging.lock); + } + + return pg; +} + +/* Allocate a new page table 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; + pte_t *p; + + ASSERT(!p2me_is_valid(p2m, *entry)); + + page =3D p2m_alloc_page(p2m->domain); + if ( page =3D=3D NULL ) + return -ENOMEM; + + page_list_add(page, &p2m->pages); + + p =3D __map_domain_page(page); + clear_page(p); + + unmap_domain_page(p); + + p2m_write_pte(entry, page_to_p2m_table(p2m, page), p2m->clean_pte); + + return 0; +} + #define GUEST_TABLE_MAP_NONE 0 #define GUEST_TABLE_MAP_NOMEM 1 #define GUEST_TABLE_SUPER_PAGE 2 @@ -516,9 +591,33 @@ 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; + + entry =3D *table + offset; + + if ( !p2me_is_valid(p2m, *entry) ) + { + if ( !alloc_tbl ) + return GUEST_TABLE_MAP_NONE; + + ret =3D p2m_create_table(p2m, entry); + if ( ret ) + return GUEST_TABLE_MAP_NOMEM; + } + + /* The function p2m_next_level() is never called at the last level */ + ASSERT(level !=3D 0); + if ( p2me_is_mapping(p2m, *entry) ) + return GUEST_TABLE_SUPER_PAGE; + + mfn =3D mfn_from_pte(*entry); + + unmap_domain_page(*table); + *table =3D map_domain_page(mfn); =20 - return GUEST_TABLE_MAP_NONE; + return GUEST_TABLE_NORMAL; } =20 static void p2m_put_foreign_page(struct page_info *pg) --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749560778; cv=none; d=zohomail.com; s=zohoarc; b=kJzVttVelnitYP/E5+8xUW/pTng8BXgFJNgbyGaPvTS0emmkSz1685bbX00ZVq94aSy6W7FUHwQCA3AJTNpoTSBlbIhypi2GTkmFmhUHAaNQb0yfnF9hfTov98Zd6W2QdSxnDruz45NASLEzAsz0V92xbPeuLpoA0NkY6ZNvhOI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749560778; 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=XAO1HvfCg38B6ugq8JOzW8h4WZl3XzGZna0ImJ4d1TY=; b=KjBvi0pO6V7W479FlzA6dzni+941V2kT4kgQ+F0nW5PVNBvGCwRkuxoEiQvbekOLbWfnfdgayw12fmUQondBSGpve8YcXfmYmZGD1atDTsKCcn6tDHUMz6CIYLwDlCwps2GHyPRNyWzrJaAY3hQQpjTXZXx16KuVHgUizmhDZhQ= 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 1749560778699426.19022551051285; Tue, 10 Jun 2025 06:06:18 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010799.1389115 (Exim 4.92) (envelope-from ) id 1uOyfy-0000BC-QH; Tue, 10 Jun 2025 13:06:02 +0000 Received: by outflank-mailman (output) from mailman id 1010799.1389115; Tue, 10 Jun 2025 13:06:02 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyfx-00007P-Qw; Tue, 10 Jun 2025 13:06:01 +0000 Received: by outflank-mailman (input) for mailman id 1010799; Tue, 10 Jun 2025 13:05:59 +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 1uOyfu-0004iH-Ue for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:05:58 +0000 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [2a00:1450:4864:20::632]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a644dc88-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:57 +0200 (CEST) Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-ad89c32a7b5so876016066b.2 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: a644dc88-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560757; x=1750165557; 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=XAO1HvfCg38B6ugq8JOzW8h4WZl3XzGZna0ImJ4d1TY=; b=fBxc0i1wS9brnVIhIAvDXazFzIUXzfh93Nd2CBGTZ/5zDbtKF3QdjtraieLH4LmdqV 4dA0AoP2U5kXuP+Tz6fterU5QOiAmZf+L/jwxCZ7jC0IzzFFJy8vfCofS2qvejLANteK RA9DU7/zz8tZquuy3fgMZcR2AO5zUSbmJ0a95Ye3SwCLFREp9ltUHyXKwcRQdlzOIuSy dLQCCP0a68Y/FKGBHsq7rSCfu8RKx/LpE136mhEm4slMHp5Qb3Um8sy20dj1PZnQNqaZ rhP59g2eRS00M1/ewawTFFzU8C/MdcTjo4BMbOUajs91XqVFcPkqEsH8eOtlIZUG0ogi Rcwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560757; x=1750165557; 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=XAO1HvfCg38B6ugq8JOzW8h4WZl3XzGZna0ImJ4d1TY=; b=qQbTqYUskgMsZ+kcMPacmSALjPf4hQwfNmA9QS3X15Tt9i9gu6l5CaDYbP/dHGshWH Z+PDHnx2Ig5OQz9nmaAFgfppgA1fWLNEAtR6Ntcle81Bb3tYS/MNUv41WcAMzK1xl/Wi 1w6r5uZkOErRjzG/FDWzJiKsy99jjkNEUb6kgd0U+X9W7CG52udZFRuD49wizEebttPM 2wYpNaif5l7o7+n/IZowc4v0OkDLo+FFUr+VhicI6ZJJkYEpjCooln6Q4stzxQxyVtUY TDa+W694Zaerdl4gEtqtVG6dZVr/MSQ5/hAoRLISYz2huZqxtElmlkOPfasPyH2AZXQC rkuQ== X-Gm-Message-State: AOJu0YzxVmUnM4OVdu3ks/OKwmvi1xuRS9TjY+Te9iOaH0h1pt8+m3Zf Emn9LLCzv+hEGVFaRmZ7laY4pq3T48NdWoxR2sur9yARHyPqdOCeTkHODMxzsg== X-Gm-Gg: ASbGnctVScqG3Eh8ToxlRphiWuq8vq4tRMM9zzaeFTUnLwVr1FVgSNQ0nZ66grqnLHH B3LQxaOho6XYBJHZ2PSEpMQ1nIPT38jMHWVoftlOlA38ggXkX3JD7cGH7naN2s1x3SJpTkTIo/c nC+MfdrPs+6RmSnqNGaOEiv5kgo91i4/Q0znDX+SDeHYSUdA1+MdRC8VJuDsbHv4BZPKjgxX3ni VUdfisBmKNnMpnPOFPVwer/F9XZVF0DSOJaT+gpDpWK5h3S8DUlJCAsKhqjpFG9+p1r6e8sZJun QtcJtNXESSBZEauhUOUlKOY1bQgfY6M4UPpFIM8sYCk886U/eNLGv9JYS7M/bC0PwZkkD2KvazD zFJdwRnNsuD1TWhMAiQtVmnR2rey6Q3Wv39Vch+c= X-Google-Smtp-Source: AGHT+IH4jCCiEUNIHRyARWXyMcMwfkq8+JiAqUWpBRX5B6mdVZuFz6Pi1kIjhQ7UNLLrFtiggx+b2Q== X-Received: by 2002:a17:906:fe08:b0:ade:4304:6b7d with SMTP id a640c23a62f3a-ade7acfab2bmr229932666b.49.1749560756791; Tue, 10 Jun 2025 06:05: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 v2 15/17] xen/riscv: Implement superpage splitting for p2m mappings Date: Tue, 10 Jun 2025 15:05:30 +0200 Message-ID: <7cdd3272f2eba32dfa00be4fd72da6921eac243d.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749560780895124100 Content-Type: text/plain; charset="utf-8" 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. Unlike ARM, RISC-V does not require BBM, so there is no need to invalidate the PTE and flush the TLB before updating it with the newly created, split page table. Additionally, the page table walk logic has been adjusted, as ARM uses the opposite walk order compared to RISC-V. Signed-off-by: Oleksii Kurochko --- 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 | 102 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 87dd636b80..79c4473f1f 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -743,6 +743,77 @@ static void p2m_free_entry(struct p2m_domain *p2m, p2m_free_page(p2m->domain, 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 int 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(p2me_is_superpage(p2m, *entry, level)); + + page =3D p2m_alloc_page(p2m->domain); + if ( !page ) + return false; + + page_list_add(page, &p2m->pages); + 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 to + * know whether the entry should be shattered for every entry. + */ + if ( next_level !=3D target ) + rv =3D p2m_split_superpage(p2m, table + offsets[next_level], + level - 1, target, offsets); + + /* TODO: why it is necessary to have clean here? Not somewhere in the = caller */ + if ( p2m->clean_pte ) + clean_dcache_va_range(table, PAGE_SIZE); + + unmap_domain_page(table); + + /* + * Even if we failed, we should 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(p2m, page), p2m->clean_pte); + + return rv; +} + /* * Insert an entry in the p2m. This should be called with a mapping * equal to a page/superpage. @@ -806,7 +877,36 @@ 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(p2me_is_superpage(p2m, *entry, level)); + + if ( !p2m_split_superpage(p2m, &split_pte, level, target, offsets)= ) + { + /* Free the allocated sub-tree */ + p2m_free_entry(p2m, split_pte, level); + + rc =3D -ENOMEM; + goto out; + } + + p2m_write_pte(entry, split_pte, p2m->clean_pte); + + /* 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 GUEST_TABLE_NORMAL || + (rc =3D=3D GUEST_TABLE_SUPER_PAGE && target > 0)); + } + + entry =3D table + offsets[level]; } =20 /* --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749561157; cv=none; d=zohomail.com; s=zohoarc; b=A6trwW/5WkHLm5BH0eHbUDVZaDq8nk1Iz7jQlKQ7VE3mh0h7fPsJkayEuHkzOKitf0NR5JRIyYHSaaSegVzau2s26WlsISrexue2ufZsaJI/wwLXZTFgpXs0oKZt2IHwHQ2Jiw6nuiGctoijrKHUgqZlojE+/OeeZWggowDeeCQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749561157; 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=oLkVEHfi1jrO45zgqffaLAKyuwJWGqpdSuo4c300uoo=; b=mD/EE4KAYaImoRYUkUlcum/I2nup/BYllRl9BGFaSC71/IkoVaY4BimtSoB6EXonpJZEYJgjIyQPyzEZvSYs8rBq+i5abhH1nN7DvbUWa+vSAQxwEVDeHb/L55SDuO+RqkF0kP8C/uq1aIkW+cEbzy+6UwTxWMup9ZE8NDTnh/M= 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 17495611573121013.9176656219938; Tue, 10 Jun 2025 06:12:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010862.1389139 (Exim 4.92) (envelope-from ) id 1uOym6-0006EU-BO; Tue, 10 Jun 2025 13:12:22 +0000 Received: by outflank-mailman (output) from mailman id 1010862.1389139; Tue, 10 Jun 2025 13:12:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOym6-0006EN-88; Tue, 10 Jun 2025 13:12:22 +0000 Received: by outflank-mailman (input) for mailman id 1010862; Tue, 10 Jun 2025 13:12:20 +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 1uOyfw-0004iH-VI for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:06:00 +0000 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [2a00:1450:4864:20::62a]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a76f8c09-45fb-11f0-a306-13f23c93f187; Tue, 10 Jun 2025 15:05:59 +0200 (CEST) Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-ad56cbc7b07so794163866b.0 for ; Tue, 10 Jun 2025 06:05: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 a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05: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: a76f8c09-45fb-11f0-a306-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560759; x=1750165559; 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=oLkVEHfi1jrO45zgqffaLAKyuwJWGqpdSuo4c300uoo=; b=a4WV+ObV8bS++SVIR1CCtOtfhlq9ARhXV62KFMUFzqhWqfVQJYFVq0zAdqr/wkijhJ JoCBo9nOmL9hm5R84LYsp/v+BTWkUiNmGcuLNJqxD+ad6LWfVC4eSbjzAxmvyRegsN4g WyN6jWZea9EAZic1TBbnu9N4Ayqz/TLpeOywjFtXGW8CcA86qNI6f+rrMzvVp718F26A I4urpZDz5DfcDZMTJGdkSikMa8Gs6ltYr6i+xAc/fuaJnNYu7Cr2nAOnYyTsXdUjnUGM JPH9/KPF3TNQ9CN0BXYprFnn5v8hN1JdeDKk7t726PSjsBh86cc/CyEYSbi7uYDebM/a DAaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560759; x=1750165559; 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=oLkVEHfi1jrO45zgqffaLAKyuwJWGqpdSuo4c300uoo=; b=WR0VGNwOSoK+bwqEKX7tBLZKa/pUpJzcTa/cqJrPhj/oD08G8gQr6cVJG/ZSTNC8CW n/myCJmtKnf8IaFVzDm3tHYnWEDXdKcVP4/Zw4jdF8x4ecTvqO0XKaAsdS+qdzSABbpC zMTX5swdt9Y5icgznaZBzst4/ts+r3WfodfIGLAmfj0g+qyY4ckygr6K7/BMVq+UcAZv GgdS11KWcil9Eiw4CZKy5WTm+9XUJr0epRFIe09PW5F+GNIrdQueA3yuB6/BqUps+3cx dWCvVfdvMzZ6RVObkmj6nLJayRPHzyDfXNMRUFdUO9BZtNPOxUIcAiEbwujVTpN6X7bC Ksng== X-Gm-Message-State: AOJu0Yzrh5VbQrYtagzXM0j0NapFOIAbON71moIOaS4TdUAoJ5/kkUf+ BqvEkN5RkBPtQgFf42cS6IGQzNUr4JiQF6xy+I+wLQYUlYVT1ULyjJ7ZoZ9Quw== X-Gm-Gg: ASbGncuqKt5GFAZz7hMhwblM8SJRfMskay+qkLHzcPrRcSo4CqrggEgTxpBXeMfw7HL hfIi6OHeq9eKgUd5q2x61FBBh7kvcR6RDbNclzlMZOSLxWfcizvQ1hJuuzL4W/Ax89U2EVXOOM6 6vEMGEuBkbWbeCiPEsllWeG8eSAaXZq69jLFguD1v+bUrQK8r7roXKzKy/MWaZpHxfYL1b8/nhy PaZ+IeOgjKtn15xDYOihCBXttW+AuVqeKPNo3GsJSqvgEuy5TodVppk+mgfgcjWz7HttJLqrJNz Ly99BLLgnoaWctDiNN5oosIYQH5o7Iy9hjFMjeJ8BDDGIAPePGM+q98yaBVsPTVmM7XNFSLbitq lYZ+cnV/W7mw1lb6GsLiWc5NznaP5VJPqbRoCByo= X-Google-Smtp-Source: AGHT+IEBaIt1MD6aKhJxCYZ43RfPWThnVB8CtvWfUM39Lk0b4i2B38h1vrfVFizytmU7ODufwOjrjQ== X-Received: by 2002:a17:907:3f23:b0:ad8:9c97:c2fa with SMTP id a640c23a62f3a-ade1a8da8f7mr1694147666b.4.1749560757808; Tue, 10 Jun 2025 06:05:57 -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 v2 16/17] xen/riscv: implement mfn_valid() and page reference, ownership handling helpers Date: Tue, 10 Jun 2025 15:05:31 +0200 Message-ID: <50159d05e75c14ca62ee6cab5a4d30645981827a.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749561158912124100 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, 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. - put_page() and put_page_nr() to release page references and free the page when the count drops to zero. For put_page_nr(), code related to static memory configuration is wrapped with CONFIG_STATIC_MEMORY, as this configuration has not yet been moved to common code. Therefore, PGC_static and free_domstatic_page() are not introduced for RISC-V. However, since this configuration could be useful in the future, the relevant code is retained and conditionally compiled. - 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 --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/mm.h | 9 ++- xen/arch/riscv/mm.c | 97 +++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index 41bf9002d7..bd8511e5f9 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 @@ -288,8 +289,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) && (mfn__ < max_page)); \ +}) =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 4047d67c0e..c88908d4f0 100644 --- a/xen/arch/riscv/mm.c +++ b/xen/arch/riscv/mm.c @@ -361,11 +361,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"); @@ -525,6 +520,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 __read_mostly start_page; + /* * Setup memory management * @@ -577,6 +574,8 @@ void __init setup_mm(void) } =20 setup_frametable_mappings(ram_start, ram_end); + + start_page =3D PFN_DOWN(ram_start); max_page =3D PFN_DOWN(ram_end); } =20 @@ -613,3 +612,91 @@ void __iomem *ioremap(paddr_t pa, size_t len) { return ioremap_attr(pa, len, PAGE_HYPERVISOR_NOCACHE); } + +int page_is_ram_type(unsigned long mfn, unsigned long mem_type) +{ + ASSERT_UNREACHABLE(); + + return 0; +} + +static struct domain *page_get_owner_and_nr_reference(struct page_info *pa= ge, + unsigned long nr) +{ + unsigned long x, y =3D page->count_info; + struct domain *owner; + + /* Restrict nr to avoid "double" overflow */ + if ( nr >=3D PGC_count_mask ) + { + ASSERT_UNREACHABLE(); + return NULL; + } + + 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 + nr) & PGC_count_mask) <=3D nr) ) + return NULL; + } + while ( (y =3D cmpxchg(&page->count_info, x, x + nr)) !=3D x ); + + owner =3D page_get_owner(page); + ASSERT(owner); + + return owner; +} + +struct domain *page_get_owner_and_reference(struct page_info *page) +{ + return page_get_owner_and_nr_reference(page, 1); +} + +void put_page_nr(struct page_info *page, unsigned long nr) +{ + unsigned long nx, x, y =3D page->count_info; + + do { + ASSERT((y & PGC_count_mask) >=3D nr); + x =3D y; + nx =3D x - nr; + } + while ( unlikely((y =3D cmpxchg(&page->count_info, x, nx)) !=3D x) ); + + if ( unlikely((nx & PGC_count_mask) =3D=3D 0) ) + { +#ifdef CONFIG_STATIC_MEMORY + if ( unlikely(nx & PGC_static) ) + free_domstatic_page(page); + else +#endif + free_domheap_page(page); + } +} + +void put_page(struct page_info *page) +{ + put_page_nr(page, 1); +} + +bool get_page_nr(struct page_info *page, const struct domain *domain, + unsigned long nr) +{ + const struct domain *owner =3D page_get_owner_and_nr_reference(page, n= r); + + if ( likely(owner =3D=3D domain) ) + return true; + + if ( owner !=3D NULL ) + put_page_nr(page, nr); + + return false; +} + +bool get_page(struct page_info *page, const struct domain *domain) +{ + return get_page_nr(page, domain, 1); +} --=20 2.49.0 From nobody Fri Oct 31 03:56:15 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=1749561275; cv=none; d=zohomail.com; s=zohoarc; b=bo9qeYRXcPpYu6A1FXCr3trRafTkgGPwsSgPkPjjtwjRmXIIzZ+Jxj+IfMVHvszcyyhZnMSnQ2Ao2niE7WGx1sHKbcNZU+pW8BAeWsRE+I18ZZG/9qW0N7rOPE4PaocvoKqpDUrwdKx0ntBtMHAJwhPCc6aDoZvIPXYDaMILMw4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749561275; 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=OxKP9f8ngOMBgx9560upayt7lSd7zzCQxJwGN9s2GJk=; b=AjryXIzYVSfdrv9YDELeEL6K5CVTUJl6UnoHeUMfeRvGckNquHlB+n8tjwHCmHOt8HYROGkUp4FV50bomWGrJiCWxce9CCOqFDH7Y3AtTSaYUJunwtnZ6whD4PsAX+JjshoKUoUu1D9gGKuWNJ2qAEhdd2K+1dEDJGAs1QMRS1o= 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 1749561275168562.5360602008259; Tue, 10 Jun 2025 06:14:35 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1010902.1389159 (Exim 4.92) (envelope-from ) id 1uOyo4-0007uY-0W; Tue, 10 Jun 2025 13:14:24 +0000 Received: by outflank-mailman (output) from mailman id 1010902.1389159; Tue, 10 Jun 2025 13:14:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uOyo3-0007uG-So; Tue, 10 Jun 2025 13:14:23 +0000 Received: by outflank-mailman (input) for mailman id 1010902; Tue, 10 Jun 2025 13:14:22 +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 1uOyfx-0004Sm-S1 for xen-devel@lists.xenproject.org; Tue, 10 Jun 2025 13:06:01 +0000 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [2a00:1450:4864:20::633]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a79c32ec-45fb-11f0-b894-0df219b8e170; Tue, 10 Jun 2025 15:06:00 +0200 (CEST) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-adb2e9fd208so1005453066b.3 for ; Tue, 10 Jun 2025 06:06:00 -0700 (PDT) Received: from fedora.. (user-109-243-64-38.play-internet.pl. [109.243.64.38]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade32c42770sm626465466b.41.2025.06.10.06.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 06:05:58 -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: a79c32ec-45fb-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749560759; x=1750165559; 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=OxKP9f8ngOMBgx9560upayt7lSd7zzCQxJwGN9s2GJk=; b=m1ovLthoevvFCTP+/FCbMSh7wbea1ENmx1tXO32LyFI/RlpTLezvYSCGmN6ZnTFRhn iqoa3mlawgCHTENDdO66G6BITW53nAMxvbtd2BsbvUFSiTV7pcFGTzy3UofVxfFo0hqS M4NuFkkENFhtpOlLergJltgqjTEAdwQbQlWEMbmlisdtO59DMLphH7ZHBtOMGlDjpWvn eTVH8Fl/pUuZPNzVy4aADgvTJYV6DJM9HiRs4vdiDPXWZSHqPL0dApBtPpXmoRdGixVM Q6MbUTl82/7ezeQpaZlOM6eTyCWp3BH5mK+o6cHtvi5MWbE6r3XpUV1gyR4M0J20DLtO qpyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749560759; x=1750165559; 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=OxKP9f8ngOMBgx9560upayt7lSd7zzCQxJwGN9s2GJk=; b=iFOKX4zrUZwqiwLB3PdS33UxRr6moIipwqiMt6lgYgEDi7LOAsNqhJREXF40rSEYxR iNUdopvJhOTgWC2InEM/6h06iAK3M9EYbCUrYsLdRhy1nIIqD50ztjnivMWCbWocyZCC h62xNs4XaOwnEzn0uYz/WNjJ0WLqae3AvjEWDx6WHCK8wN3Pr3DKgF9lDoYR08QSpLvh j+BRMOlvPKpyXwesCpdiIeH4Zi0sxhKXH28e1wl1GznhNQkgRhxoZkXgxtyTmA7PlYLy sfr+Q0X/CmzwbHb3XO7vzDIuswmJ2d7oCdv0Iuewc5H8yowIbwm1LGKDfMhp1XM5oL/j 9P/g== X-Gm-Message-State: AOJu0YzVW+dkGP/jMUnUI7b9Oz+oSBOuPnNI6utTIkcblF85iBQW6ajb 6RSrIRRc/e2ccKS+VEu7XpUuqr3Cr3J7UxIB+2cX/IfP/BDXrQen39KCH3C8Gg== X-Gm-Gg: ASbGncviwSBNZPIT1gJfprb0kRJDuAvGV7ZBBJWidvnuu/sPn6+RnoKw/jGmRzPQT+3 pp4oe2bfGZKCq4YMHTMuNXizPBC/xcM6SRoocRcOM46wySjBD+rsu6jPRJih62mELfoERCsEDKw W79J9Df/5xhhwwdphp3zzqM6HoiexM1WkMMXzxyQpJFa4Eeskte92VLTOUeywfJsQ3Or/naB8m5 WZB0vyO+rm86We1lB/b+ZMUb6FMMASEVBHbX7Vv71Pqv575rT+QsCRvDvzut1Nzla5OY9iYxlax Yd0CLrcpHbLDqhdPalYWNH0GXZu4CAIV03Yn2hgn4uAZ2ZKSnz5oNbgXK4mVnNl0mAFA6FzssFW vT/lvJZM95mZJMV9Bt4aUL19AnBI7mkYUPDgJt2Y= X-Google-Smtp-Source: AGHT+IHWl8myTiCOtSxMJB1ph23F+y6qfnmNmU/sbGUTwauxrcWLbzCTqhwYVY34jrYRSFX6Z5wKoA== X-Received: by 2002:a17:907:7248:b0:ad8:9257:5735 with SMTP id a640c23a62f3a-ade1aa0fc41mr1574508766b.3.1749560759079; Tue, 10 Jun 2025 06:05:59 -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 v2 17/17] xen/riscv: add support of page lookup by GFN Date: Tue, 10 Jun 2025 15:05:32 +0200 Message-ID: <6281efb0dc9b0a9ab4dd8cee7952bff2a7745963.1749555949.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749561277539124100 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. 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 order 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. - p2m_get_page_from_gfn(): - Removed p2m_is_foreign() and related logic, as this functionality is not implemented for RISC-V. Signed-off-by: Oleksii Kurochko --- Changes in V2: - New patch. --- xen/arch/riscv/include/asm/p2m.h | 18 +++++ xen/arch/riscv/p2m.c | 131 +++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) diff --git a/xen/arch/riscv/include/asm/p2m.h b/xen/arch/riscv/include/asm/= p2m.h index fdebd18356..96e0790dbc 100644 --- a/xen/arch/riscv/include/asm/p2m.h +++ b/xen/arch/riscv/include/asm/p2m.h @@ -184,6 +184,24 @@ static inline int p2m_is_write_locked(struct p2m_domai= n *p2m) return rw_is_write_locked(&p2m->lock); } =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 domain *d, gfn_t gfn, + p2m_type_t *t); + #endif /* ASM__RISCV__P2M_H */ =20 /* diff --git a/xen/arch/riscv/p2m.c b/xen/arch/riscv/p2m.c index 79c4473f1f..034b1888c5 100644 --- a/xen/arch/riscv/p2m.c +++ b/xen/arch/riscv/p2m.c @@ -1055,3 +1055,134 @@ int guest_physmap_add_entry(struct domain *d, { return p2m_insert_mapping(d, gfn, (1 << page_order), mfn, t); } + +/* + * Get the details of a given gfn. + * + * If the entry is present, the associated MFN will be returned and the + * access and type filled 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) +{ + paddr_t addr =3D gfn_to_gaddr(gfn); + unsigned int level =3D 0; + pte_t entry, *table; + int rc; + mfn_t mfn =3D INVALID_MFN; + p2m_type_t _t; + DECLARE_OFFSETS(offsets, addr); + + ASSERT(p2m_is_locked(p2m)); + BUILD_BUG_ON(XEN_PT_LEVEL_MAP_MASK(0) !=3D PAGE_MASK); + + /* Allow t to be NULL */ + t =3D t ?: &_t; + + *t =3D p2m_invalid; + + 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 GUEST_TABLE_MAP_NONE) && (rc !=3D GUEST_TABLE_MAP_= NOMEM) ) + goto out_unmap; + else if ( rc !=3D GUEST_TABLE_NORMAL ) + break; + } + + entry =3D table[offsets[level]]; + + if ( p2me_is_valid(p2m, entry) ) + { + *t =3D p2m_type_radix_get(p2m, 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 domain *d, gfn_t gfn, p2m_type_t *t) +{ + mfn_t mfn; + struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + + 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 domain *d, gfn_t gfn, + p2m_type_t *t) +{ + p2m_type_t p2mt =3D {0}; + struct page_info *page; + + mfn_t mfn =3D p2m_lookup(d, gfn, &p2mt); + + if ( t ) + *t =3D p2mt; + + if ( !mfn_valid(mfn) ) + return NULL; + + page =3D mfn_to_page(mfn); + + return get_page(page, d) ? page : NULL; +} --=20 2.49.0