From patchwork Mon May 17 14:57:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 12262255 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98EE0C433B4 for ; Mon, 17 May 2021 15:02:41 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 27F8661029 for ; Mon, 17 May 2021 15:02:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 27F8661029 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=eW1CZZJt5/t81vWtZx5hODzm5izfh5Qbyo+ukIv5lT8=; b=YX6Ipfd5jxMdcv65Ow8VHhC/y QLM3tfibk1mDlQeoxxgJ+26UKk4Wgc+SJyFW1/fz4kGgJThbWOp2OoQeZmWoYplVoPGqYvETRIeGr PKO1IjdcXZNBDPGJOD11VbzrIgovpiA3AaYF0xOmRluaBcaJPyBlASZIR1MDZm0Cz09r521mCN6Ql Fl/JiIt027OFeerbvER62u9MbBYHrLAfSEWaJj+UpXSYsyAhM+SH6kggo2I4Elu0DxFAF/EWbAcQV QDvb+IWfO3aqFIVgIeHyzLDscwFR/0pXE3cAytQYVwnLfqiH1fnE4Lw9KcKfR+RB+7ehUy47YmQ3m R7NxWwjXA==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1liejm-00FLE8-Be; Mon, 17 May 2021 15:00:54 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1liejh-00FLDJ-IG for linux-arm-kernel@desiato.infradead.org; Mon, 17 May 2021 15:00:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=a1Icj33cVQz5+rlV9zDpEQ19nt6FhzyUrJNZ0Mb9Rt8=; b=GNIYmjeKhOEyDtTy9TYmvVLJQp Z+rLmeFH8Fw+BRkeOBdAurlP8i7NQO7EvSWmbQ1RbIfIxuOY1394MazPbwVszxgyu9JcmtBCm3ZQT BPVRqwBmZ6ZaERCVtQTTE+ZzFVIvP/UYUxfYSN3JIQZzfSKrDok5IjlaBf7HBn1cYd5M3B3zqTAzU vkaoMPgqcltj2AJanlrhrxjj/LCvfo+4DH4SXWRMtg2O3RIK09eotpE1Zr6qMDM53pKiciTHnA3iG GZA2mgfvsaANKRlBVwr8NZJ16n2kSpxigUsPJQ90OKtYSqofsC/2QVKj9f6qEPsiEtywHLnX+mOwg GK+x8ysg==; Received: from mail-lf1-x134.google.com ([2a00:1450:4864:20::134]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1liejc-00DtQh-GT for linux-arm-kernel@lists.infradead.org; Mon, 17 May 2021 15:00:48 +0000 Received: by mail-lf1-x134.google.com with SMTP id a2so9242484lfc.9 for ; Mon, 17 May 2021 08:00:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=a1Icj33cVQz5+rlV9zDpEQ19nt6FhzyUrJNZ0Mb9Rt8=; b=Gq5smhoEQaLkyC08FFbW7+susPJ9jzbGPnU4U5Q2fARZ+pvmwrUaqTC3noXomUsu+W 1p1YIgIz4qKLt6nsguBYuwc71mc9lZjedAez4VjDosUBwfw52K7nIqon6mF0nE4DlA+E pU6eFYAj+V/A2kGrP6c1ODTjoRVl/3jd9B7wGlPIW0II/X/WhR+2st6yeZp0hoZVK+vS XS3hPNSRAu0J4CVVWswmwn5ZoqGwbCV/2xcOtytGZnK5dX9vT2Ct2LQQphbos7bmFItv +sbYcVp2N+jtJohojoyDkgFzLn88cPQYoM5RME8+x90E2Y57MREvsYhPxEXuk7m06SVP 6Lng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a1Icj33cVQz5+rlV9zDpEQ19nt6FhzyUrJNZ0Mb9Rt8=; b=K3bSmMsaJsI6CTAzjb6LPSLk4jQhpQS9I2NglUtQY2p2TQ1AkeF4L0wmRNI8jGiAsw yW6wgkJapJ1FC2slt+cEoUEpSw/IAXLXuaencVyCD5nDIDsGrBcA7PajFiM8UuL3cZ9I Gv1ermDSF6CyGmk1NKhJXxzyE550Iwz8ASOTPRH0JvJx6JmzodswXyFsnMPiJfsgBNM8 KSXEFjLKRkSRDEhUKPIXR+BKnJFRBKuajNzkVUl2cvBE+JAaYzq/raOS2Rkr5qQxcmfX yuWYK1u6tf3aTCibGD34jw8tc8RG4g21OVxRCVNskPge218YbHWBD1jhMXAh3XjWD/Hz YGXg== X-Gm-Message-State: AOAM5303xXgDKCp6agoRlVPCD33W7r5soepqsJaLzWgBavhGI1PEUB48 hRiAhTowyk97aW1tJzYEkGYTuwQ8vSgGeg== X-Google-Smtp-Source: ABdhPJwIHwXc5A5hAyR4DOBOatNUoy+S1qH3bV9vpCEwCW3jiseERex4soi5p447z7aFLV4npq//MA== X-Received: by 2002:ac2:4dac:: with SMTP id h12mr229994lfe.308.1621263642317; Mon, 17 May 2021 08:00:42 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id h13sm2863080lji.102.2021.05.17.08.00.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 08:00:42 -0700 (PDT) From: Linus Walleij To: linux-arm-kernel@lists.infradead.org, Russell King , Arnd Bergmann Cc: Florian Fainelli , Geert Uytterhoeven , Ard Biesheuvel , Linus Walleij Subject: [PATCH 1/3] ARM: Split KERNEL_OFFSET from PAGE_OFFSET Date: Mon, 17 May 2021 16:57:17 +0200 Message-Id: <20210517145719.110966-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517145719.110966-1-linus.walleij@linaro.org> References: <20210517145719.110966-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210517_080044_630224_C31B9CC7 X-CRM114-Status: GOOD ( 20.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org We want to be able to compile the kernel into an address different from PAGE_OFFSET (start of lowmem) + TEXT_OFFSET, so start to pry apart the address of where the kernel is located from the address where the lowmem is located by defining and using KERNEL_OFFSET in a few key places. Signed-off-by: Linus Walleij --- arch/arm/include/asm/memory.h | 8 +++++++- arch/arm/kernel/head.S | 3 +-- arch/arm/kernel/vmlinux.lds.S | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/arm/include/asm/memory.h b/arch/arm/include/asm/memory.h index a711322d9f40..a5f1d1826a98 100644 --- a/arch/arm/include/asm/memory.h +++ b/arch/arm/include/asm/memory.h @@ -20,8 +20,14 @@ #endif #include -/* PAGE_OFFSET - the virtual address of the start of the kernel image */ +/* + * PAGE_OFFSET: the virtual address of the start of lowmem, memory above + * the virtual address range for userspace. + * KERNEL_OFFSET: the virtual address of the start of the kernel image. + * we may further offset this with TEXT_OFFSET in practice. + */ #define PAGE_OFFSET UL(CONFIG_PAGE_OFFSET) +#define KERNEL_OFFSET (PAGE_OFFSET) #ifdef CONFIG_MMU diff --git a/arch/arm/kernel/head.S b/arch/arm/kernel/head.S index 7f62c5eccdf3..4e2daaa7636a 100644 --- a/arch/arm/kernel/head.S +++ b/arch/arm/kernel/head.S @@ -23,7 +23,6 @@ #if defined(CONFIG_DEBUG_LL) && !defined(CONFIG_DEBUG_SEMIHOSTING) #include CONFIG_DEBUG_LL_INCLUDE #endif - /* * swapper_pg_dir is the virtual address of the initial page table. * We place the page tables 16K below KERNEL_RAM_VADDR. Therefore, we must @@ -31,7 +30,7 @@ * the least significant 16 bits to be 0x8000, but we could probably * relax this restriction to KERNEL_RAM_VADDR >= PAGE_OFFSET + 0x4000. */ -#define KERNEL_RAM_VADDR (PAGE_OFFSET + TEXT_OFFSET) +#define KERNEL_RAM_VADDR (KERNEL_OFFSET + TEXT_OFFSET) #if (KERNEL_RAM_VADDR & 0xffff) != 0x8000 #error KERNEL_RAM_VADDR must start at 0xXXXX8000 #endif diff --git a/arch/arm/kernel/vmlinux.lds.S b/arch/arm/kernel/vmlinux.lds.S index f7f4620d59c3..20c4f6d20c7a 100644 --- a/arch/arm/kernel/vmlinux.lds.S +++ b/arch/arm/kernel/vmlinux.lds.S @@ -47,7 +47,7 @@ SECTIONS #endif } - . = PAGE_OFFSET + TEXT_OFFSET; + . = KERNEL_OFFSET + TEXT_OFFSET; .head.text : { _text = .; HEAD_TEXT From patchwork Mon May 17 14:57:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 12262257 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC1F0C43460 for ; Mon, 17 May 2021 15:02:51 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5205F61019 for ; Mon, 17 May 2021 15:02:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5205F61019 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=F/dcR9VtZfVjQ0+bNXiBtAZdc/hKp6RcdzjoQStAogg=; b=Az/1ElSqu5D0KurK9+B9SWOI1 sezcu6UZrouXviC6+aXrTyft5V3rcHv/rzf5+3wwTByKYxxzAAk4udDkhOO0PXSYRVriZ7KF5+YVX TTD9fuVdH5qf5ZsRLQyG0T3t8mkUx2SPlLRzRCwmXhS5gcPRk0UorDktUm/ZxvhGpQLn+sBNf8/F0 4NYPpxaI2Rg0U52p5O5XNmzPI9MhK3rP7GEE9HkXOF/e8MMLhx9ynA2xdLy4Q3TpFl6lj1SS5SMnN W+CX4lhebT3/iUF2jTyCF6ESOPmkqo1KfDdK8mvYxAjl3efIubmdj91T9FBoDvN5KpMvbXkStaWYK J3FYY8blg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1liejw-00FLFz-UZ; Mon, 17 May 2021 15:01:05 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1liejj-00FLDr-TZ for linux-arm-kernel@desiato.infradead.org; Mon, 17 May 2021 15:00:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=Zk629r6AyRIib1Jb23k9ic0+RwK6DtrYkCA9W0DTKO4=; b=rQFNJ44c5QAM5iP1r729VyNVgE aPskfW5wHYqCXNyDnV2wQi6QCtxZAk+k2Iw3+3ct15pbKw/ndjPeU0LFm6n21zVPi584Ye36T+S8C AzU8VKb4xrIrsjF9xK5E7WMCvyzLKYW3CYe1NrPyFkZAVSMemnoKoDwpKoCwVAm3IxF3Ar/OCWPnD Fh2TjdgYPSUY10NynoTljOIKJXFF9N3ykFL5C4jELpLHnI7W8d3UUPrnZ+wpcpASwjX92RzCk7GVJ 0x4zWe7FdWpf73yNtWwGGmSZRbtsCBz4TK1n+B+d8SVqDoV3kO+hdhDbodNeT6UUH4t7UXONz/eS8 mMkfB5FA==; Received: from mail-lj1-x233.google.com ([2a00:1450:4864:20::233]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lieje-00DtQy-7j for linux-arm-kernel@lists.infradead.org; Mon, 17 May 2021 15:00:50 +0000 Received: by mail-lj1-x233.google.com with SMTP id b12so402479ljp.1 for ; Mon, 17 May 2021 08:00:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Zk629r6AyRIib1Jb23k9ic0+RwK6DtrYkCA9W0DTKO4=; b=tYTBnGQVaxqaSHJ/6gTMmAJwi05z67Nrb/PDWORyd9cK8i95Wh6YUMer3nEV6Mpt8w VXkA1VNpVcQddi+ILnm7ykBHskEbdIpjwzuQsbYyGredgeq7RVh5x8iGL5ypJcDhHdPq aiwbv49IlJRo0gK1TNfLRZAHSTkSvWdQ3gB4IebwJhQQ2GKRp5+3/BVArGUQR1pFPSXb na6mia7KcVMiOQBhSBbVqgtcWxxlbKC4YOFS0kcuBLF7Acp2BhMw4/q8ZTXembGvy0q7 rOSQ4meNO5xV0UZ5sGK+gW4UlbJPzHjpSx/vo5BMRPFHoeLwAKbbInItc7Xm8RS4TqCJ wZfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Zk629r6AyRIib1Jb23k9ic0+RwK6DtrYkCA9W0DTKO4=; b=m3TOzChnwiSbNQ2OXTixK/IRKkbCKGA5aPLLCGkVECyxPXc4M4S+EXJcEPbhvTOKI8 K/xZZRH4y9JIsUIz2xrALy4sVS6lpipiiqm0hli+D92nHmqQ80TeL17AeHLsokBXMYv+ 0BKdpP18iiI66UTsEsNZgD4pzzYkSmwQRd/w6Shj1C311uGlcFfy994KEr3P8Z2Oprti hpoY4HPcT2VTk814gICXP+tOyr8w7YrpAYa/BoDoHFoy8ROQu5LrvOjZP+CZLJ1LgXI/ vlqT6vzo3vn9kEcOY7F5l2R8v7nw6jzHITC59yBTs/kJ8pUL7ZHfGO8mG50bq5PetP9v mYag== X-Gm-Message-State: AOAM533Dt4JnkHzG4kEvOVyxQ8FXZA+31iqyv6h0RoiAr/U2eemamMny csrDOmttODwVvYHzNzd7tWM4Xi0jLdGpXg== X-Google-Smtp-Source: ABdhPJzm98FRbuu5JZwlXYleREu/Ko71xac7/UH2L7zMad7Fr6RZvip6ixkzWYT6Zxy5kpTwu/M2hA== X-Received: by 2002:a05:651c:210:: with SMTP id y16mr47466516ljn.279.1621263644020; Mon, 17 May 2021 08:00:44 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id h13sm2863080lji.102.2021.05.17.08.00.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 08:00:43 -0700 (PDT) From: Linus Walleij To: linux-arm-kernel@lists.infradead.org, Russell King , Arnd Bergmann Cc: Florian Fainelli , Geert Uytterhoeven , Ard Biesheuvel , Linus Walleij Subject: [PATCH 2/3] ARM: Define kernel physical section start and end Date: Mon, 17 May 2021 16:57:18 +0200 Message-Id: <20210517145719.110966-3-linus.walleij@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517145719.110966-1-linus.walleij@linaro.org> References: <20210517145719.110966-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210517_080046_297553_FA86F7E6 X-CRM114-Status: GOOD ( 20.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When we are mapping the initial sections in head.S we know very well where the start and end of the kernel image in physical memory is placed. Later on it gets hard to determine this. Save the information into two variables named kernel_sec_start and kernel_sec_end for convenience for later work involving the physical start and end of the kernel. These variables are section-aligned corresponding to the early section mappings set up in head.S. Signed-off-by: Linus Walleij --- arch/arm/include/asm/memory.h | 7 +++++++ arch/arm/kernel/head.S | 27 ++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/arch/arm/include/asm/memory.h b/arch/arm/include/asm/memory.h index a5f1d1826a98..ca213d7d095f 100644 --- a/arch/arm/include/asm/memory.h +++ b/arch/arm/include/asm/memory.h @@ -158,6 +158,13 @@ extern unsigned long vectors_base; #ifndef __ASSEMBLY__ +/* + * Physical start and end address of the kernel sections. These addresses are + * 2MB-aligned to match the section mappings placed over the kernel. + */ +extern phys_addr_t kernel_sec_start; +extern phys_addr_t kernel_sec_end; + /* * Physical vs virtual RAM address space conversion. These are * private definitions which should NOT be used outside memory.h diff --git a/arch/arm/kernel/head.S b/arch/arm/kernel/head.S index 4e2daaa7636a..9eb0b4dbcc12 100644 --- a/arch/arm/kernel/head.S +++ b/arch/arm/kernel/head.S @@ -47,6 +47,20 @@ .globl swapper_pg_dir .equ swapper_pg_dir, KERNEL_RAM_VADDR - PG_DIR_SIZE + /* + * This needs to be assigned at runtime when the linker symbols are + * resolved. + */ + .pushsection .data + .align 2 + .globl kernel_sec_start + .globl kernel_sec_end +kernel_sec_start: + .long 0 +kernel_sec_end: + .long 0 + .popsection + .macro pgtbl, rd, phys add \rd, \phys, #TEXT_OFFSET sub \rd, \rd, #PG_DIR_SIZE @@ -229,16 +243,23 @@ __create_page_tables: blo 1b /* - * Map our RAM from the start to the end of the kernel .bss section. + * The main matter: map in the kernel using section mappings, and + * set two variables to indicate the physical start and end of the + * kernel. */ - add r0, r4, #PAGE_OFFSET >> (SECTION_SHIFT - PMD_ORDER) + add r0, r4, #KERNEL_OFFSET >> (SECTION_SHIFT - PMD_ORDER) ldr r6, =(_end - 1) - orr r3, r8, r7 + adr_l r5, kernel_sec_start @ _pa(kernel_sec_start) + str r8, [r5] @ Save physical start of kernel + orr r3, r8, r7 @ Add the MMU flags add r6, r4, r6, lsr #(SECTION_SHIFT - PMD_ORDER) 1: str r3, [r0], #1 << PMD_ORDER add r3, r3, #1 << SECTION_SHIFT cmp r0, r6 bls 1b + eor r3, r3, r7 @ Remove the MMU flags + adr_l r5, kernel_sec_end @ _pa(kernel_sec_end) + str r3, [r5] @ Save physical end of kernel #ifdef CONFIG_XIP_KERNEL /* From patchwork Mon May 17 14:57:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 12262259 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 389F4C433B4 for ; Mon, 17 May 2021 15:03:02 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BCCE061019 for ; Mon, 17 May 2021 15:03:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BCCE061019 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=/knn3HYfXxb4UmYcKnRsoHuMT0lVYkN9tUsMzp6sqAQ=; b=Yh0kqYtHkbvBoFMgKe5jGJUis FX6vaTznbCvfX3xDXKP6fmKvQkV16Nusdc4D84WyPzY2HQ6GLkuiBdEebijWH/INqWQqt43OF1Yec qhP5OXLsYG2hvfOza5gKtsmJCZeFoq9f4vPApsr0n9T6b/sownsrCLJh6tHM0hpNVfDunmgpVV7YB caA7xRIemzsT4xcrCkK4L5BHLc/0ksumS1anjDUnd9yav0k2SRldB6IFxMKo7vH8tPkZCEzt/vsrD Llq3eUqKvWLylOXtGGVaFq4ZMXMh0E59wo/UjYSkAPO8H2h0UTZXTCfnbj7+jqNgNNrmTZQxvmksD kKPjkRHSQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1liek8-00FLHs-8c; Mon, 17 May 2021 15:01:16 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1liejl-00FLE1-Tx for linux-arm-kernel@desiato.infradead.org; Mon, 17 May 2021 15:00:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=o2WJ6GTqeIEaIeAQ9XdCAYlW6U/ZPJCkS9OHmjc2S2M=; b=dj2tJlV8M1rPGlDlmme9iU1Th1 lI4/RxZP8HanUqYQuhzJZ3srnWISRnxQM+5yqkaq2KKwZHOUJyejYjMxtd1NLFYejtAiAf4NlKajC DFMqOkseXLrnbtV8X0ilu3xbZJUsXd8IFD+blXOuViRu8KQqotgVMGyTA46SFA9tmfddBjuWbzm7Z FGbguCULUS6waAlK+bVBy4+gFgOmDrcvJH0+D1JayNHFvZ7fYNFM28RzTs592/vjVCjvB3fnRSJ32 gw8jz5Buoy68sR4M5DoaLNLrj2ujTmRYaiLm5fi0EBbPETVG0jDUuToFpQb5RX1OJenXvqgwBrl9P JrU7RYKg==; Received: from mail-lj1-x235.google.com ([2a00:1450:4864:20::235]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1liejf-00DtRH-QP for linux-arm-kernel@lists.infradead.org; Mon, 17 May 2021 15:00:52 +0000 Received: by mail-lj1-x235.google.com with SMTP id 131so7644865ljj.3 for ; Mon, 17 May 2021 08:00:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o2WJ6GTqeIEaIeAQ9XdCAYlW6U/ZPJCkS9OHmjc2S2M=; b=ice+evPDd2cM6xFcrftbW6SnNGOI97dNDXFiRLzM+lA1k60pDn1ypuJ1vqo9OCHuUV wNW075jfD2kICuTp/dNihvMxn9VCkbn6ZdY7I/685tH3KKDQYbbMVzTGtezqwbx6toT6 DAMxunRxmbmUUJrhMmTz359NAU71PqBlHHquStcXiBX/pV7Nn8r0Ho3dKJ4SVS9OLPug ByERaWJDZ2xhk6NlTNsStoJcmufquusZ0s/Vn8z0FS7aMzCrrmNxYp6TAVtLJWLJePhj nrvPMXS305rycNaHYbL7RpbCLuDdPrxuO54svvtGGvvH7rZ7fNEJYL5gISRzEbHZRcqc cNLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=o2WJ6GTqeIEaIeAQ9XdCAYlW6U/ZPJCkS9OHmjc2S2M=; b=An3u+DIXciOEyfCJeC1GiXrKW0csz0KZdEw7yl59TjfxZxMJEHNVcBM5pMY5u2IWTh scwu/2nGgQCFDhUo9qytoJjZHXIwx3hzBsYPnTR9K49iv1a/LOPphsoI/pj4RIw2pohf ty4C7lhyuYX2bgj1PJfitnJwKzNYxNxPK4BSQ00sBeg4RFgkxzfB0hd/oU8PbF5hV1gX uRs3t9l5MPUlquGa/4bRWaC14fPJyvcXuyayc7QEEBvSnLLrqxAUIziG4Dyo493YRLZl jGWX+GedpTq5EYFCkcxm592Bpw/siLZWhmTk5Gz/LanEPfoD56e5SfR2GiQpbEL1mUDV RzWA== X-Gm-Message-State: AOAM532tpPCKQKsHcv5RlnD9eD4V1kdTT72Whwk+XF7mymt4PpxAa2HF aOpScR8CW1a7n5LXUavFOLaY0F1rOqDc4A== X-Google-Smtp-Source: ABdhPJyF9/o1C+8GD+U0gY+yVusAd7BXfJbkUNTk+pj9Hh9aZHHpmAT18lnCHx7INju2cY7N53VeTA== X-Received: by 2002:a2e:9b51:: with SMTP id o17mr48040135ljj.320.1621263645611; Mon, 17 May 2021 08:00:45 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id h13sm2863080lji.102.2021.05.17.08.00.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 08:00:45 -0700 (PDT) From: Linus Walleij To: linux-arm-kernel@lists.infradead.org, Russell King , Arnd Bergmann Cc: Florian Fainelli , Geert Uytterhoeven , Ard Biesheuvel , Linus Walleij Subject: [PATCH 3/3] ARM: Map the lowmem and kernel separately Date: Mon, 17 May 2021 16:57:19 +0200 Message-Id: <20210517145719.110966-4-linus.walleij@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517145719.110966-1-linus.walleij@linaro.org> References: <20210517145719.110966-1-linus.walleij@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210517_080047_883841_E5A2A46D X-CRM114-Status: GOOD ( 35.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Using our knowledge of where the physical kernel sections start and end we can split mapping of lowmem and kernel apart. This is helpful when you want to place the kernel independently from lowmem and not be limited to putting it into lowmem only, but also into places such as the VMALLOC area. We extensively rewrite the lowmem mapping code to account for all cases where the kernel image overlaps with the lowmem in different ways. This is helpful to handle situations which occur when the kernel is loaded in different places and makes it possible to place the kernel in a more random manner which is done with e.g. KASLR. We sprinkle some comments with illustrations and pr_debug() over it so it is also very evident to readers what is happening. We now use the kernel_sec_start and kernel_sec_end instead of relying on __pa() (phys_to_virt) to provide this. This is helpful if we want to resolve physical-to-virtual and virtual-to-physical mappings at runtime rather than compiletime, especially if we are not using patch phys to virt. Signed-off-by: Linus Walleij --- arch/arm/mm/mmu.c | 144 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 111 insertions(+), 33 deletions(-) diff --git a/arch/arm/mm/mmu.c b/arch/arm/mm/mmu.c index c1e12aab67b8..ac37959507b7 100644 --- a/arch/arm/mm/mmu.c +++ b/arch/arm/mm/mmu.c @@ -1457,8 +1457,6 @@ static void __init kmap_init(void) static void __init map_lowmem(void) { - phys_addr_t kernel_x_start = round_down(__pa(KERNEL_START), SECTION_SIZE); - phys_addr_t kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE); phys_addr_t start, end; u64 i; @@ -1466,55 +1464,126 @@ static void __init map_lowmem(void) for_each_mem_range(i, &start, &end) { struct map_desc map; + pr_debug("map lowmem start: 0x%08llx, end: 0x%08llx\n", + (long long)start, (long long)end); if (end > arm_lowmem_limit) end = arm_lowmem_limit; if (start >= end) break; - if (end < kernel_x_start) { - map.pfn = __phys_to_pfn(start); - map.virtual = __phys_to_virt(start); - map.length = end - start; - map.type = MT_MEMORY_RWX; + /* + * If our kernel image is in the VMALLOC area we need to remove + * the kernel physical memory from lowmem since the kernel will + * be mapped separately. + * + * The kernel will typically be at the very start of lowmem, + * but any placement relative to memory ranges is possible. + * + * If the memblock contains the kernel, we have to chisel out + * the kernel memory from it and map each part separately. We + * get 6 different theoretical cases: + * + * +--------+ +--------+ + * +-- start --+ +--------+ | Kernel | | Kernel | + * | | | Kernel | | case 2 | | case 5 | + * | | | case 1 | +--------+ | | +--------+ + * | Memory | +--------+ | | | Kernel | + * | range | +--------+ | | | case 6 | + * | | | Kernel | +--------+ | | +--------+ + * | | | case 3 | | Kernel | | | + * +-- end ----+ +--------+ | case 4 | | | + * +--------+ +--------+ + */ - create_mapping(&map); - } else if (start >= kernel_x_end) { - map.pfn = __phys_to_pfn(start); - map.virtual = __phys_to_virt(start); - map.length = end - start; - map.type = MT_MEMORY_RW; + /* Case 5: kernel covers range, don't map anything, should be rare */ + if ((start > kernel_sec_start) && (end < kernel_sec_end)) + break; - create_mapping(&map); - } else { - /* This better cover the entire kernel */ - if (start < kernel_x_start) { + /* Cases where the kernel is starting inside the range */ + if ((kernel_sec_start >= start) && (kernel_sec_start <= end)) { + /* Case 6: kernel is embedded in the range, we need two mappings */ + if ((start < kernel_sec_start) && (end > kernel_sec_end)) { + /* Map memory below the kernel */ map.pfn = __phys_to_pfn(start); map.virtual = __phys_to_virt(start); - map.length = kernel_x_start - start; + map.length = kernel_sec_start - start; map.type = MT_MEMORY_RW; - create_mapping(&map); - } - - map.pfn = __phys_to_pfn(kernel_x_start); - map.virtual = __phys_to_virt(kernel_x_start); - map.length = kernel_x_end - kernel_x_start; - map.type = MT_MEMORY_RWX; - - create_mapping(&map); - - if (kernel_x_end < end) { - map.pfn = __phys_to_pfn(kernel_x_end); - map.virtual = __phys_to_virt(kernel_x_end); - map.length = end - kernel_x_end; + /* Map memory above the kernel */ + map.pfn = __phys_to_pfn(kernel_sec_end); + map.virtual = __phys_to_virt(kernel_sec_end); + map.length = end - kernel_sec_end; map.type = MT_MEMORY_RW; - create_mapping(&map); + break; } + /* Case 1: kernel and range start at the same address, should be common */ + if (kernel_sec_start == start) + start = kernel_sec_end; + /* Case 3: kernel and range end at the same address, should be rare */ + if (kernel_sec_end == end) + end = kernel_sec_start; + } else if ((kernel_sec_start < start) && (kernel_sec_end > start) && (kernel_sec_end < end)) { + /* Case 2: kernel ends inside range, starts below it */ + start = kernel_sec_end; + } else if ((kernel_sec_start > start) && (kernel_sec_start < end) && (kernel_sec_end > end)) { + /* Case 4: kernel starts inside range, ends above it */ + end = kernel_sec_start; } + map.pfn = __phys_to_pfn(start); + map.virtual = __phys_to_virt(start); + map.length = end - start; + map.type = MT_MEMORY_RW; + create_mapping(&map); } } +static void __init map_kernel(void) +{ + /* + * We use the well known kernel section start and end and split the area in the + * middle like this: + * . . + * | RW memory | + * +----------------+ kernel_x_start + * | Executable | + * | kernel memory | + * +----------------+ kernel_x_end / kernel_nx_start + * | Non-executable | + * | kernel memory | + * +----------------+ kernel_nx_end + * | RW memory | + * . . + * + * Notice that we are dealing with section sized mappings here so all of this + * will be bumped to the closest section boundary. This means that some of the + * non-executable part of the kernel memory is actually mapped as executable. + * This will only persist until we turn on proper memory management later on + * and we remap the whole kernel with page granularity. + */ + phys_addr_t kernel_x_start = kernel_sec_start; + phys_addr_t kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE); + phys_addr_t kernel_nx_start = kernel_x_end; + phys_addr_t kernel_nx_end = kernel_sec_end; + struct map_desc map; + + map.pfn = __phys_to_pfn(kernel_x_start); + map.virtual = __phys_to_virt(kernel_x_start); + map.length = kernel_x_end - kernel_x_start; + map.type = MT_MEMORY_RWX; + create_mapping(&map); + + /* If the nx part is small it may end up covered by the tail of the RWX section */ + if (kernel_x_end == kernel_nx_end) + return; + + map.pfn = __phys_to_pfn(kernel_nx_start); + map.virtual = __phys_to_virt(kernel_nx_start); + map.length = kernel_nx_end - kernel_nx_start; + map.type = MT_MEMORY_RW; + create_mapping(&map); +} + #ifdef CONFIG_ARM_PV_FIXUP typedef void pgtables_remap(long long offset, unsigned long pgd); pgtables_remap lpae_pgtables_remap_asm; @@ -1645,9 +1714,18 @@ void __init paging_init(const struct machine_desc *mdesc) { void *zero_page; + pr_debug("physical kernel sections: 0x%08x-0x%08x\n", + kernel_sec_start, kernel_sec_end); + prepare_page_table(); map_lowmem(); memblock_set_current_limit(arm_lowmem_limit); + pr_debug("lowmem limit is %08llx\n", (long long)arm_lowmem_limit); + /* + * After this point early_alloc(), i.e. the memblock allocator, can + * be used + */ + map_kernel(); dma_contiguous_remap(); early_fixmap_shutdown(); devicemaps_init(mdesc);