From nobody Tue May 21 05:19:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1690191792; cv=none; d=zohomail.com; s=zohoarc; b=DQ4ni6AADM+8Ckow7pFfEZRcdqXaQAqcb3mkguiu45L9gsPNas/3EI++pg+MlywrXE5GsK1qHO+B3om5usQxgDeeRcbreXdb2IxxB+i/XFh86BUBax3TGO7+WuDTcA7xTZ7Uq64mBml3IZejnzpj0uROC40yubhrGT+ezofFNTo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690191792; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Nfcp7c7c/3n6CY6kNcbVjdnQ1GyqRTk1VyoAU9RnpnM=; b=oICTL379xX9wUehclvrWhDdQt2fZ+bfpJ+pFAOtgNzCkxMRhxuI/ErFfxWfxRaxOu0hvumAmycEEmvT+n58vEu2sAWb+VQlVsTdlV2MH0MMCOZ0HnIP8QkQXUjpNV4p2mOZbn26U4Z9qUSp1WrPwrgO02Hq+17+CNK3j4esAstc= 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 169019179236213.985503666810928; Mon, 24 Jul 2023 02:43:12 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568525.887913 (Exim 4.92) (envelope-from ) id 1qNs5Z-0005iX-Pg; Mon, 24 Jul 2023 09:42:49 +0000 Received: by outflank-mailman (output) from mailman id 568525.887913; Mon, 24 Jul 2023 09:42: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 1qNs5Z-0005iM-Lw; Mon, 24 Jul 2023 09:42:49 +0000 Received: by outflank-mailman (input) for mailman id 568525; Mon, 24 Jul 2023 09:42: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 1qNs5Y-0005hi-ST for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 09:42:48 +0000 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [2a00:1450:4864:20::134]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 7176ec9a-2a06-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 11:42:45 +0200 (CEST) Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-4fb5bcb9a28so6189322e87.3 for ; Mon, 24 Jul 2023 02:42:46 -0700 (PDT) Received: from fedora.. ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id k10-20020aa7c04a000000b0050488d1d376sm6020958edo.0.2023.07.24.02.42.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 02:42: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: 7176ec9a-2a06-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690191765; x=1690796565; 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=Nfcp7c7c/3n6CY6kNcbVjdnQ1GyqRTk1VyoAU9RnpnM=; b=Csan28GVnhjQUoXT1yMjYnfboJRQfsiggTVzjPbFpapz3fKWnv0vzSmzUc1EY+7oOC z36BGfFwcPVee9rEWkh0XaYxpIUKr5VUvvynqB3z+fYIa9YsDbuRFX4x+1LCFW4TXs+Z PGJred5yGMjnEoUZS0HJt8dOl8koS/OccWvg6rkbl5cKXvtYFc76seXYsHB2RgyfjadE 0qajHsS31XBdhlaV728LcMV3i8G2NmYNG2kUal+HCLYqgfxOFco/8cE3xlFuyTMzy4rG mhliVvUtHjMCAumQuvXOVRIlyEx6WKRd0V8sl/HReYBb07J5uVwYMCuChvoFu9jlQYGj HHvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690191765; x=1690796565; 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=Nfcp7c7c/3n6CY6kNcbVjdnQ1GyqRTk1VyoAU9RnpnM=; b=bTDYkjoJuAitv2H7e+MpbtsL4WiFGyEWYoTdX77sCREyiwvjl+Vi51JK4T/bWCJrsw Cfj24sGein3StDCTzcMOzN4gyddNlg8gpv/B081beRHYHfiM9rGS7yN+0lVCAHD887xT K21qnJn6X1FtUlmqgl6P1op7JXxS3zVar6OQEEdmC4G3ezRlyrQMlvYKZXMw5fDzR1ep 0mefr5wvdm865Wf3Jy37dqD7QBftxDr1O7dVFJR4j6B9DXSRnCbvK9yvYzU7GkdDU/se 8FqpSYlJoTKfqaT6jbKBsP2KP/cJjm8BqVMVm2QMZrKHwe1cApXtVEc3YrDTC4VDvOcR iFaQ== X-Gm-Message-State: ABy/qLbzVe+PSyOa5IcyCRgSXBAjjQdA7JzfZZdtXTOLUWfomde3ppKS yEZw9J2idpyDQyVjr7FfBhAChosfJAA= X-Google-Smtp-Source: APBJJlFtnpAKSRB9j61eraO6i0crSM/fmYILB1TIwDXPOcVEiyOaXs5GHgpCMPJAokcnijBiK865xQ== X-Received: by 2002:a05:6512:23a5:b0:4fd:d517:fbd8 with SMTP id c37-20020a05651223a500b004fdd517fbd8mr6682242lfv.9.1690191765227; Mon, 24 Jul 2023 02:42:45 -0700 (PDT) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , Oleksii Kurochko , Bob Eshleman , Alistair Francis , Connor Davis Subject: [PATCH v4 1/2] xen/riscv: introduce function for physical offset calculation Date: Mon, 24 Jul 2023 12:42:40 +0300 Message-ID: <19817eca0b7d4e8dee7eb5d5e7d3812133925eb3.1690191480.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1690191792676100001 Content-Type: text/plain; charset="utf-8" The function was introduced to calculate and save physical offset before MMU is enabled because access to start() is PC-relative and in case of linker_addr !=3D load_addr it will result in incorrect value in phys_offset. Signed-off-by: Oleksii Kurochko --- Changes in V4: - update the comment messages in head.S related to save/restore of a0/a1 r= egs. --- Changes in V3: - save/restore of a0/a1 registers before C first function call. --- Changes in V2: - add __ro_after_init for phys_offset variable. - remove double blank lines. - add __init for calc_phys_offset(). - update the commit message. - declaring variable phys_off as non static as it will be used in head.S. --- xen/arch/riscv/include/asm/mm.h | 2 ++ xen/arch/riscv/mm.c | 18 +++++++++++++++--- xen/arch/riscv/riscv64/head.S | 13 +++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index 5e3ac5cde3..d9c4205103 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -15,4 +15,6 @@ void setup_initial_pagetables(void); void enable_mmu(void); void cont_after_mmu_is_enabled(void); =20 +void calc_phys_offset(void); + #endif /* _ASM_RISCV_MM_H */ diff --git a/xen/arch/riscv/mm.c b/xen/arch/riscv/mm.c index fddb3cd0bd..c84a8a7c3c 100644 --- a/xen/arch/riscv/mm.c +++ b/xen/arch/riscv/mm.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */ =20 +#include #include #include #include @@ -19,9 +20,10 @@ struct mmu_desc { pte_t *pgtbl_base; }; =20 -#define PHYS_OFFSET ((unsigned long)_start - XEN_VIRT_START) -#define LOAD_TO_LINK(addr) ((addr) - PHYS_OFFSET) -#define LINK_TO_LOAD(addr) ((addr) + PHYS_OFFSET) +unsigned long __ro_after_init phys_offset; + +#define LOAD_TO_LINK(addr) ((unsigned long)(addr) - phys_offset) +#define LINK_TO_LOAD(addr) ((unsigned long)(addr) + phys_offset) =20 /* * It is expected that Xen won't be more then 2 MB. @@ -273,3 +275,13 @@ void __init noreturn noinline enable_mmu() switch_stack_and_jump((unsigned long)cpu0_boot_stack + STACK_SIZE, cont_after_mmu_is_enabled); } + +/* + * calc_phys_offset() should be used before MMU is enabled because access = to + * start() is PC-relative and in case when load_addr !=3D linker_addr phys= _offset + * will have an incorrect value + */ +void __init calc_phys_offset(void) +{ + phys_offset =3D (unsigned long)start - XEN_VIRT_START; +} diff --git a/xen/arch/riscv/riscv64/head.S b/xen/arch/riscv/riscv64/head.S index 2c0304646a..a28714e0ef 100644 --- a/xen/arch/riscv/riscv64/head.S +++ b/xen/arch/riscv/riscv64/head.S @@ -29,6 +29,19 @@ ENTRY(start) =20 jal reset_stack =20 + /* + * save hart_id ( bootcpu_id ) and dtb_base as a0 and a1 register = can + * be used by C code + */ + mv s0, a0 + mv s1, a1 + + jal calc_phys_offset + + /* restore hart_id ( bootcpu_id ) and dtb address */ + mv a0, s0 + mv a1, s1 + tail start_xen =20 .section .text, "ax", %progbits --=20 2.41.0 From nobody Tue May 21 05:19:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1690191798; cv=none; d=zohomail.com; s=zohoarc; b=XrxqQRO6E8Jr3wj0+oUnpe4jrJcVXPjycucJmh62bjeuy18/OU38wJULpv8jZUDIkzEqol8++JXn90LWcdo0tcYCl1aI6xoDg96iWyDuYkXF5UWzS2FsF9vSG6l4G8sAdEJic+c3ZMMJ1wNtiTeti/7mqQ0uuaUonMVE+P/52+M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690191798; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3A7jIfn7cd5zSr8lvNclLlaXmI+gimV3tunSWePZzuo=; b=NfgxHDgMXf6yVVCso+4SLe8/aI7KbPq+DmH6w1xEuWatG8txbL94y56xAx3QdXeRJvPfV1EEIsA19rwUNBo0BRl6Xecyc6GnYRm0m5pW4ihOjClYQSq8UP5U5DOb0FRnelPnrgcbTfZuxUmtTRdb8BJr7y3RIeC9eFYsu+Na16g= 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 16901917986251001.9812808730845; Mon, 24 Jul 2023 02:43:18 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568526.887923 (Exim 4.92) (envelope-from ) id 1qNs5b-0005yX-23; Mon, 24 Jul 2023 09:42:51 +0000 Received: by outflank-mailman (output) from mailman id 568526.887923; Mon, 24 Jul 2023 09:42:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNs5a-0005yI-VN; Mon, 24 Jul 2023 09:42:50 +0000 Received: by outflank-mailman (input) for mailman id 568526; Mon, 24 Jul 2023 09:42:49 +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 1qNs5Z-0005hi-IK for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 09:42:49 +0000 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [2a00:1450:4864:20::533]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 7188aa35-2a06-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 11:42:46 +0200 (CEST) Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-5217bb5ae05so5541090a12.0 for ; Mon, 24 Jul 2023 02:42:47 -0700 (PDT) Received: from fedora.. ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id k10-20020aa7c04a000000b0050488d1d376sm6020958edo.0.2023.07.24.02.42.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 02:42: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: 7188aa35-2a06-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690191766; x=1690796566; 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=3A7jIfn7cd5zSr8lvNclLlaXmI+gimV3tunSWePZzuo=; b=FjreHnYj84PURYQMOG0tUHg+XaO1B9OA3QVLQjya6LJldeYPkey2vp9IUaPHOh4NQy MT2iIAlfbtOO/34TpgSXH3aCtWK3c2vldGXFpGzPnrnr2ogTMq72dzamNad5ueGMkwAT 3myLua7hvHOW9CklzECY3YKzu+hBvQ+5TspzuGN//+h68Av93EIrXExoPmJVWyeXqqSb NUIm2vaDT/mlWHyRPu2CdgOC/yi6ssthGQ8T9hQKV7QrjMqV4C87/VbMcccXWjuWSACX DGBSlMMEORsIbZNE8gK0xdYh8PjH6sHyZ5W9yHxnk5VrD9irW4g2fomW+zZ4nnMiDJyQ IeEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690191766; x=1690796566; 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=3A7jIfn7cd5zSr8lvNclLlaXmI+gimV3tunSWePZzuo=; b=hUzEegW0N7UZcxX3bKnIqmYyfqeqtxC9eyE5IvrCx11xgtsGHuh5ybgDTQ/iJVmuG1 li0PBsb3sM1kBbRUuDt3lf7aAgyun2sWIYB+rG1eVpXiAjqeqMVCHgpl/xeEI+YAvtjP 1GLe6RwH8eFb8b0nOatFSpzfS8fnQSmbR7KeJM0QaOFbwz6rNg6/SwoERL52likxhKw+ gxTUKYfBKXaoRperjjY+JNynuUtrzwp7MbHZaq01b31+BuaNZJ4XGTGIq+3F3LWXLnKV gROCpl2vQG35OUiuuoJmSWOtR29CqZgtp4ZUvkyQ0EdslyJPCb0NTFy9TYcYI90d4s8U do/Q== X-Gm-Message-State: ABy/qLawgpy+0D7afEFdy73yx+kjC7nDtG7Ri81vI9WHd2WQLm9iqpsE dInUNtxrSCgkKutIy19CrZ5Q2MZ0vTg= X-Google-Smtp-Source: APBJJlG3Jq/VevmjGpaGSwcVLTeyfhAWXO+R3+Bc6NhCeh5J20ZztJ0zkfTfmbGo4ZDw8LV/OAY01Q== X-Received: by 2002:a05:6402:609:b0:51e:c82:6f3b with SMTP id n9-20020a056402060900b0051e0c826f3bmr7925509edv.16.1690191765947; Mon, 24 Jul 2023 02:42:45 -0700 (PDT) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , Oleksii Kurochko , Bob Eshleman , Alistair Francis , Connor Davis Subject: [PATCH v4 2/2] xen/riscv: introduce identity mapping Date: Mon, 24 Jul 2023 12:42:41 +0300 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1690191800866100001 Content-Type: text/plain; charset="utf-8" The way how switch to virtual address was implemented in the commit e66003e7be ("xen/riscv: introduce setup_initial_pages") isn't safe enough as: * enable_mmu() depends on hooking all exceptions and pagefault. * Any exception other than pagefault, or not taking a pagefault causes it to malfunction, which means you will fail to boot depending on where Xen was loaded into memory. Instead of the proposed way of switching to virtual addresses was decided to use identity mapping of the entrire Xen and after switching to virtual addresses identity mapping is removed from page-tables in the following way: recursively visit all ptes related to identity mapping and remove them. Fixes: e66003e7be ("xen/riscv: introduce setup_initial_pages") Signed-off-by: Oleksii Kurochko Suggested-by: Andrew Cooper --- Changes in V4: - remove definition of ARRAY_SIZE and ROUNDUP as was introd= uced where these macros are located now. - update definition of PGTBL_INITIAL_COUNT - update the commit message - update the algo of identity mapping removing --- Changes in V3: - remove unrelated to the patch changes ( SPDX tags in config.h ). - update definition of PGTBL_INITIAL_COUNT taking into account identity ma= pping. - refactor remove_identity_mapping() function. - add explanatory comments in xen.lds.S and mm.c. - update commit message. - move save/restore of a0/a1 registers to [PATCH v2 2/3] xen/riscv: introd= uce function for physical offset calculation. --- Changes in V2: - update definition of PGTBL_INITIAL_COUNT and the comment above. - code style fixes. - 1:1 mapping for entire Xen. - remove id_addrs array becase entire Xen is mapped. - reverse condition for cycle inside remove_identity_mapping(). - fix page table walk in remove_identity_mapping(). - update the commit message. - add Suggested-by: Andrew Cooper - save hart_id and dtb_addr before call MMU related C functions. - use phys_offset variable instead of doing calcultations to get phys off= set in head.S file. ( it can be easily done as entire Xen is 1:1 mapped ) - declare enable_muu() as __init. --- xen/arch/riscv/include/asm/mm.h | 3 +- xen/arch/riscv/mm.c | 101 ++++++++++++++++++++------------ xen/arch/riscv/riscv64/head.S | 22 +++++++ xen/arch/riscv/setup.c | 14 +---- xen/arch/riscv/xen.lds.S | 4 ++ 5 files changed, 93 insertions(+), 51 deletions(-) diff --git a/xen/arch/riscv/include/asm/mm.h b/xen/arch/riscv/include/asm/m= m.h index d9c4205103..085eaab7fb 100644 --- a/xen/arch/riscv/include/asm/mm.h +++ b/xen/arch/riscv/include/asm/mm.h @@ -13,7 +13,8 @@ extern unsigned char cpu0_boot_stack[]; void setup_initial_pagetables(void); =20 void enable_mmu(void); -void cont_after_mmu_is_enabled(void); + +void remove_identity_mapping(void); =20 void calc_phys_offset(void); =20 diff --git a/xen/arch/riscv/mm.c b/xen/arch/riscv/mm.c index c84a8a7c3c..aae24f3a54 100644 --- a/xen/arch/riscv/mm.c +++ b/xen/arch/riscv/mm.c @@ -4,6 +4,7 @@ #include #include #include +#include #include =20 #include @@ -35,8 +36,10 @@ unsigned long __ro_after_init phys_offset; * * It might be needed one more page table in case when Xen load address * isn't 2 MB aligned. + * + * CONFIG_PAGING_LEVELS page tables are needed for identity mapping. */ -#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) + 1) +#define PGTBL_INITIAL_COUNT ((CONFIG_PAGING_LEVELS - 1) * 2) + 1 =20 pte_t __section(".bss.page_aligned") __aligned(PAGE_SIZE) stage1_pgtbl_root[PAGETABLE_ENTRIES]; @@ -75,10 +78,11 @@ static void __init setup_initial_mapping(struct mmu_des= c *mmu_desc, unsigned int index; pte_t *pgtbl; unsigned long page_addr; + bool is_identity_mapping =3D map_start =3D=3D pa_start; =20 - if ( (unsigned long)_start % XEN_PT_LEVEL_SIZE(0) ) + if ( !IS_ALIGNED((unsigned long)_start, KB(4)) ) { - early_printk("(XEN) Xen should be loaded at 4k boundary\n"); + early_printk("(XEN) Xen should be loaded at 4KB boundary\n"); die(); } =20 @@ -108,16 +112,18 @@ static void __init setup_initial_mapping(struct mmu_d= esc *mmu_desc, { unsigned long paddr =3D (page_addr - map_start) + pa_start; unsigned int permissions =3D PTE_LEAF_DEFAULT; + unsigned long addr =3D is_identity_mapping + ? page_addr : LINK_TO_LOAD(page_addr); pte_t pte_to_be_written; =20 index =3D pt_index(0, page_addr); =20 - if ( is_kernel_text(LINK_TO_LOAD(page_addr)) || - is_kernel_inittext(LINK_TO_LOAD(page_addr)) ) - permissions =3D - PTE_EXECUTABLE | PTE_READABLE | PTE_VALID; + if ( is_kernel_text(addr) || + is_kernel_inittext(addr) ) + permissions =3D + PTE_EXECUTABLE | PTE_READABLE | PTE_VALID; =20 - if ( is_kernel_rodata(LINK_TO_LOAD(page_addr)) ) + if ( is_kernel_rodata(addr) ) permissions =3D PTE_READABLE | PTE_VALID; =20 pte_to_be_written =3D paddr_to_pte(paddr, permissions); @@ -211,6 +217,10 @@ void __init setup_initial_pagetables(void) unsigned long linker_start =3D LOAD_TO_LINK(load_start); unsigned long linker_end =3D LOAD_TO_LINK(load_end); =20 + /* + * If the overlapping check will be removed then remove_identity_mappi= ng() + * logic should be updated. + */ if ( (linker_start !=3D load_start) && (linker_start <=3D load_end) && (load_start <=3D linker_end) ) { @@ -232,22 +242,18 @@ void __init setup_initial_pagetables(void) linker_start, linker_end, load_start); + + if ( linker_start =3D=3D load_start ) + return; + + setup_initial_mapping(&mmu_desc, + load_start, + load_end, + load_start); } =20 -void __init noreturn noinline enable_mmu() +void __init enable_mmu(void) { - /* - * Calculate a linker time address of the mmu_is_enabled - * label and update CSR_STVEC with it. - * MMU is configured in a way where linker addresses are mapped - * on load addresses so in a case when linker addresses are not equal - * to load addresses, after MMU is enabled, it will cause - * an exception and jump to linker time addresses. - * Otherwise if load addresses are equal to linker addresses the code - * after mmu_is_enabled label will be executed without exception. - */ - csr_write(CSR_STVEC, LOAD_TO_LINK((unsigned long)&&mmu_is_enabled)); - /* Ensure page table writes precede loading the SATP */ sfence_vma(); =20 @@ -255,25 +261,44 @@ void __init noreturn noinline enable_mmu() csr_write(CSR_SATP, PFN_DOWN((unsigned long)stage1_pgtbl_root) | RV_STAGE1_MODE << SATP_MODE_SHIFT); +} =20 - asm volatile ( ".p2align 2" ); - mmu_is_enabled: - /* - * Stack should be re-inited as: - * 1. Right now an address of the stack is relative to load time - * addresses what will cause an issue in case of load start address - * isn't equal to linker start address. - * 2. Addresses in stack are all load time relative which can be an - * issue in case when load start address isn't equal to linker - * start address. - * - * We can't return to the caller because the stack was reseted - * and it may have stash some variable on the stack. - * Jump to a brand new function as the stack was reseted - */ +void __init remove_identity_mapping(void) +{ + static pte_t *pgtbl =3D stage1_pgtbl_root; + static unsigned long load_start =3D XEN_VIRT_START; + static unsigned int pt_level =3D CONFIG_PAGING_LEVELS - 1; + + unsigned long load_end =3D LINK_TO_LOAD(_end); + unsigned long xen_size; + unsigned long pt_level_size =3D XEN_PT_LEVEL_SIZE(pt_level); + unsigned long pte_nums; + + unsigned long virt_indx =3D pt_index(pt_level, XEN_VIRT_START); + unsigned long indx; + + if ( load_start =3D=3D XEN_VIRT_START ) + load_start =3D LINK_TO_LOAD(_start); + + xen_size =3D load_end - load_start; + pte_nums =3D ROUNDUP(xen_size, pt_level_size) / pt_level_size; + + while ( pte_nums-- ) + { + indx =3D pt_index(pt_level, load_start); =20 - switch_stack_and_jump((unsigned long)cpu0_boot_stack + STACK_SIZE, - cont_after_mmu_is_enabled); + if ( virt_indx !=3D indx ) + { + pgtbl[indx].pte =3D 0; + load_start +=3D XEN_PT_LEVEL_SIZE(pt_level); + } + else + { + pgtbl =3D (pte_t *)LOAD_TO_LINK(pte_to_paddr(pgtbl[indx])); + pt_level--; + remove_identity_mapping(); + } + } } =20 /* diff --git a/xen/arch/riscv/riscv64/head.S b/xen/arch/riscv/riscv64/head.S index a28714e0ef..d74412351e 100644 --- a/xen/arch/riscv/riscv64/head.S +++ b/xen/arch/riscv/riscv64/head.S @@ -38,6 +38,28 @@ ENTRY(start) =20 jal calc_phys_offset =20 + jal setup_initial_pagetables + + jal enable_mmu + + la t1, phys_offset + REG_L t1, (t1) + + /* Calculate proper VA after jump from 1:1 mapping */ + la t0, .L_primary_switched + sub t0, t0, t1 + + /* Jump from 1:1 mapping world */ + jr t0 + +.L_primary_switched: + /* + * cpu0_boot_stack address is 1:1 mapping related so it should be + * recalculated after jump from 1:1 mapping world as 1:1 mapping + * will be removed soon in start_xen(). + */ + jal reset_stack + /* restore hart_id ( bootcpu_id ) and dtb address */ mv a0, s0 mv a1, s1 diff --git a/xen/arch/riscv/setup.c b/xen/arch/riscv/setup.c index dde8fb898b..6593f601c1 100644 --- a/xen/arch/riscv/setup.c +++ b/xen/arch/riscv/setup.c @@ -13,20 +13,10 @@ unsigned char __initdata cpu0_boot_stack[STACK_SIZE] void __init noreturn start_xen(unsigned long bootcpu_id, paddr_t dtb_addr) { - early_printk("Hello from C env\n"); - - setup_initial_pagetables(); - - enable_mmu(); - - for ( ;; ) - asm volatile ("wfi"); + remove_identity_mapping(); =20 - unreachable(); -} + early_printk("Hello from C env\n"); =20 -void __init noreturn cont_after_mmu_is_enabled(void) -{ early_printk("All set up\n"); =20 for ( ;; ) diff --git a/xen/arch/riscv/xen.lds.S b/xen/arch/riscv/xen.lds.S index 9064852173..31ccebadcb 100644 --- a/xen/arch/riscv/xen.lds.S +++ b/xen/arch/riscv/xen.lds.S @@ -173,4 +173,8 @@ ASSERT(IS_ALIGNED(__bss_end, POINTER_ALIGN), "__= bss_end is misaligned") ASSERT(!SIZEOF(.got), ".got non-empty") ASSERT(!SIZEOF(.got.plt), ".got.plt non-empty") =20 +/* + * Changing the size of Xen binary can require an update of + * PGTBL_INITIAL_COUNT. + */ ASSERT(_end - _start <=3D MB(2), "Xen too large for early-boot assumptions= ") --=20 2.41.0