From patchwork Wed Mar 29 05:29:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13191909 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EEEDDC6FD18 for ; Wed, 29 Mar 2023 05:30:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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:In-Reply-To:References: List-Owner; bh=mBJbckY+Ttu2NylGl9At044H0PpbQhLadXDWiAJChI8=; b=HNg0KacqvhybpN uan4JX51ENgV7QiDN13XWY6nC77PWl/2MmOd13t3g0HpTUNM2cKy8g0XM1tJRcjEgvuMo/UFqItJY JMAUeiDsbMWeAoMgstUkmhzylZq6ukXEqmb38ZgkTyRrQsc0JRbpVXAJhjHYUBCFs5YaNrGdZO2p/ Gok/L+yvLdJmt41keFbFk7s1rya+Yvj+gWHNwfRQ3hvgCOX4nmsBUXXdg6Uq4BivtORX4XK77nEa4 qcgyhPTs5prI2BGjO8N4fGNC7PGbZJd8qi/lajgpDDQad3MZSnDJ9VH6bpUrbWYLmvmj05PZl4fvt RsdlyXrz0s+XVQ7jy3mw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phONO-00GfvW-2R; Wed, 29 Mar 2023 05:29:38 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phONK-00Gftk-2Y for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2023 05:29:36 +0000 Received: by mail-wr1-x42d.google.com with SMTP id v1so14386750wrv.1 for ; Tue, 28 Mar 2023 22:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680067771; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=H0nKREiU5apoP9AERkcSfoCMtcJ2QiDWhd7LmZsVvuQ=; b=b4oOAK2vzMWROSK9CPdsTAH7ZN/tAeYeBXukc0zQcNww3+rA3dyiqsmIVHemW0qdqU htlzmbuOyfGr47cqTYtjKgU/lrLCuCmryoyrryfOw1NndFn3ah41a23JgdrOz7/ZpbMn wZB6MIqbnTRX7TiHeFSUmFCBCGOF27cCnEGFMAMFNMxScIQmtYYLo33QKxJZywdRy6al /OdvqKoumD++Rv0OXKYfeYmVd6TdAd6C0nsRizMZl220EoVWin0FC93RTb8xzkWwJaSX bb3qtbnxZqJFG0W4wU/7bQl77eFG93/mOHevexnD+37DgCHFoD/enVEPwI58BfrRrGAI 4ZOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680067771; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=H0nKREiU5apoP9AERkcSfoCMtcJ2QiDWhd7LmZsVvuQ=; b=ci+Q7VQ8r0acKMmg71unpwPFMAWVsNOJxNetOfASSdrO28yqTSmmSDCx3bP9alGqVZ g6OJgMf3Gas5EfVDEkHRsOlTWY/ERYh7EqEX/k7EbAmoLlaGvPFOeiJgYtzFpxnck96f Z5EHsBCVd0uhVPtBNuVX711decir4HSpo7lM9QjgTypKuLSTZxo4IYdpfOBSUtMsDeUy Uz+lkvtnd7zyaCuC8fWE+D4PVqlgLHM5zfxohnRVB4qJFv44c+YZLDNTcSpFpeujwaS5 8+xqCVoiIyuIuTy6cK+6KrbIzZngf14kykxyxHm+3uvzaRfvl14d1MYfqNObi2h/0fbH 8BDA== X-Gm-Message-State: AAQBX9dBZ50T2YHL7fOpd9xqfX2RHRYMezerzzWISXz9oLjQnWLX0Nhm Ug1I5RuDP4JwdrlzEprw9Y4dBA== X-Google-Smtp-Source: AKy350bEq5NqVvP0FW42rtbPv8tMVOKg3WWaFwubyVFWEP6t3QqmPu/EBQ1qtrwUuEQVOE9V/NpKlg== X-Received: by 2002:adf:fc8e:0:b0:2d9:16f9:aee3 with SMTP id g14-20020adffc8e000000b002d916f9aee3mr15858960wrr.14.1680067771579; Tue, 28 Mar 2023 22:29:31 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id e9-20020adffc49000000b002be5bdbe40csm29201205wrs.27.2023.03.28.22.29.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 22:29:31 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Ard Biesheuvel , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Alexandre Ghiti Subject: [PATCH v2 0/4] riscv: Introduce KASLR Date: Wed, 29 Mar 2023 07:29:22 +0200 Message-Id: <20230329052926.69632-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_222934_856036_874BB6DA X-CRM114-Status: GOOD ( 17.68 ) 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 The following KASLR implementation allows to randomize the kernel mapping: - virtually: we expect the bootloader to provide a seed in the device-tree - physically: only implemented in the EFI stub, it relies on the firmware to provide a seed using EFI_RNG_PROTOCOL. arm64 has a similar implementation hence the patch 3 factorizes KASLR related functions for riscv to take advantage. The new virtual kernel location is limited by the early page table that only has one PUD and with the PMD alignment constraint, the kernel can only take < 512 positions. Note that the patch "riscv: Use PUD/P4D/PGD pages for the linear mapping" is necessary to retrieve the memory below the physical kernel address, so that the EFI stub does not have to try to relocate the kernel as close as possible to the start of dram. This patchset is rebased on top of: riscv: Allow to downgrade paging mode from the command line (https://patchwork.kernel.org/project/linux-riscv/list/?series=734849) base-commit-tag: v6.3-rc1 Changes in v2: * Rebase on top of 6.3-rc1 * Add a riscv cache sync after memcpying the kernel * Add kaslr_offset implementation for KCOV * Add forward declaration to quiet LLVM Alexandre Ghiti (4): riscv: Introduce virtual kernel mapping KASLR riscv: Dump out kernel offset information on panic arm64: libstub: Move KASLR handling functions to efi-stub-helper.c riscv: libstub: Implement KASLR by using generic functions arch/riscv/Kconfig | 18 +++ arch/riscv/include/asm/page.h | 3 + arch/riscv/kernel/image-vars.h | 1 + arch/riscv/kernel/pi/Makefile | 2 +- arch/riscv/kernel/pi/cmdline_early.c | 13 ++ arch/riscv/kernel/pi/fdt_early.c | 30 ++++ arch/riscv/kernel/setup.c | 25 +++ arch/riscv/mm/init.c | 36 ++++- drivers/firmware/efi/libstub/arm64-stub.c | 111 +------------ drivers/firmware/efi/libstub/arm64.c | 5 + .../firmware/efi/libstub/efi-stub-helper.c | 151 ++++++++++++++++++ drivers/firmware/efi/libstub/efistub.h | 10 ++ drivers/firmware/efi/libstub/riscv-stub.c | 28 ++-- drivers/firmware/efi/libstub/riscv.c | 5 + 14 files changed, 315 insertions(+), 123 deletions(-) create mode 100644 arch/riscv/kernel/pi/fdt_early.c