From patchwork Wed Mar 29 05:29:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13191918 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 C9ECFC74A5B for ; Wed, 29 Mar 2023 05:34:26 +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: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=U7CisFJnBq1673WG/ru7z4tq3MRLSWIMPhnnhRt4jd4=; b=0z40S5nmN3+VB9 rGhSTdcR/84YpUt3BFAl+DFBHR+aKxc/8MdGlzkTzQkDJbFcR77eXvKp4MBU1leQGA1thjRRguLzl 5DHPHT+P+n+VcqR27gElKWUN+WS4dJMutUnu3abegmr0XJB5/0ThsHA3/npYJ4c+oDFKU39Wle13t xCX7uqMveodZCS+JZZR3Jo0etNdLwjD/9prQHt3C7adWDSjdmYcbUvtpDlOY2s4k5HRTgRQLqQ6kL VWOAEa2wTA6GU+zT6VimE8ErmlwYJmcdmx64pMP5mkzEZB+BrzQrXflUwF8ArLlDfWfAtcEsIsQox iMx1CVmME8ymR7prarEQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phORI-00GhA5-1Y; Wed, 29 Mar 2023 05:33:40 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phORF-00Gh8b-0D for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2023 05:33:38 +0000 Received: by mail-wm1-x32e.google.com with SMTP id bg16-20020a05600c3c9000b003eb34e21bdfso10704269wmb.0 for ; Tue, 28 Mar 2023 22:33:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680068016; 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=g9AZET+Y+Ubd7JaCED2QPLhADiInjk5s5J91QKVSZ4k=; b=3zKhJk4p5gD71igAWtDX+1AQYoKoKXyB010oklo5qec7Q7rzwb7Z/8rmApcXytNFkB qwJfaC/1+iSlSoI/1ZIlyNDiFqwLfGihQprXFafbkJ4pmIafAxVK3ClKuEhqHprDkwiX kMcblA0WbF/w2EneXqc1ebhBfXp1cfr1vEo86a94VoR3OH9i7zgOoJQtWeFYZcpbu2Gw ZDcVXp8S1KoCXaKy4A9R5DUYLIbQMbM6O/k3fa2sxPP7di6FcRgUNz9rNAP0TUEEYEz4 z0HSE77/jBFKtGbxF1gDvuvWXSKEUJkCwlv9BcWTHxM8NPBntW4/xF5l7xv+kamnA6TW +xXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680068016; 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=g9AZET+Y+Ubd7JaCED2QPLhADiInjk5s5J91QKVSZ4k=; b=IvIpdJ4GoqB4FVqfTuIwcDATEmo/ngLbLIFcmY98XVyT5VBiHJ8coGk0fl8oodeJP3 8Qn+3cP/GmbTZu8AUSO+yKJ9qyQe5X36FeTcFfK8gmsQXVnCRrBMYo2xsrrEWAHxAAGy utU7dyFs4uJngqCjBsgQ4ca9Jo9yeaBKaKTsYTvbrtpYKLg6LeEHyR2S9Is3hl236Bse AFlS/dDebK5AAC0enQwkpvcY604j/a18xyOMb+oBrHXoC1uBrsT2oDbJYP+DUSpSczh0 r8vYW2CFcrAMVeVTXgBhWkfJEhvqOj1GkLTjMBB40uuaKa2RHXugwqRnyc/LHbq7mRNE MaFg== X-Gm-Message-State: AO0yUKW07II2ggjEicoPTyqkYnNmepLNftyPnDu1/eWCa8K0FgDbOamY mBVKRkhI/TAw4adaC/XCTr0mCA== X-Google-Smtp-Source: AK7set9LKzHC4m9zJD6QlkzNGWUSHa5ooY+SVzmZs/Ul21Y/QbX7qIXjNm1AraEG6VzRdd1yv2suPw== X-Received: by 2002:a1c:4b06:0:b0:3ed:316d:668d with SMTP id y6-20020a1c4b06000000b003ed316d668dmr14254078wma.5.1680068015741; Tue, 28 Mar 2023 22:33:35 -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 o5-20020a05600c510500b003ef5bb63f13sm915242wms.10.2023.03.28.22.33.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 22:33:35 -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 4/4] riscv: libstub: Implement KASLR by using generic functions Date: Wed, 29 Mar 2023 07:29:26 +0200 Message-Id: <20230329052926.69632-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230329052926.69632-1-alexghiti@rivosinc.com> References: <20230329052926.69632-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_223337_099425_30223CE4 X-CRM114-Status: GOOD ( 17.46 ) 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 can now use arm64 functions to handle the move of the kernel physical mapping: if KASLR is enabled, we will try to get a random seed from the firmware, if not possible, the kernel will be moved to a location that suits its alignment constraints. Signed-off-by: Alexandre Ghiti --- arch/riscv/kernel/image-vars.h | 1 + drivers/firmware/efi/libstub/arm64.c | 5 ++++ .../firmware/efi/libstub/efi-stub-helper.c | 2 +- drivers/firmware/efi/libstub/efistub.h | 2 ++ drivers/firmware/efi/libstub/riscv-stub.c | 28 +++++++------------ drivers/firmware/efi/libstub/riscv.c | 5 ++++ 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/arch/riscv/kernel/image-vars.h b/arch/riscv/kernel/image-vars.h index 15616155008c..ea1a10355ce9 100644 --- a/arch/riscv/kernel/image-vars.h +++ b/arch/riscv/kernel/image-vars.h @@ -27,6 +27,7 @@ __efistub__start = _start; __efistub__start_kernel = _start_kernel; __efistub__end = _end; __efistub__edata = _edata; +__efistub___init_text_end = __init_text_end; __efistub_screen_info = screen_info; #endif diff --git a/drivers/firmware/efi/libstub/arm64.c b/drivers/firmware/efi/libstub/arm64.c index 399770266372..48bc7428d6df 100644 --- a/drivers/firmware/efi/libstub/arm64.c +++ b/drivers/firmware/efi/libstub/arm64.c @@ -108,3 +108,8 @@ void __noreturn efi_enter_kernel(unsigned long entrypoint, enter_kernel = (void *)entrypoint + primary_entry_offset(); enter_kernel(fdt_addr, 0, 0, 0); } + +void efi_icache_sync(unsigned long start, unsigned long end) +{ + caches_clean_inval_pou(start, end); +} diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 40ac2625949c..63c7275d42e7 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -865,7 +865,7 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, memcpy((void *)*reserve_addr, (void *)*image_addr, kernel_size); *image_addr = *reserve_addr; - caches_clean_inval_pou(*image_addr, *image_addr + kernel_codesize); + efi_icache_sync(*image_addr, *image_addr + kernel_codesize); return status; } diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index 64273371ce96..9ce114e04b23 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -1107,4 +1107,6 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, u32 phys_seed); u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle); +void efi_icache_sync(unsigned long start, unsigned long end); + #endif diff --git a/drivers/firmware/efi/libstub/riscv-stub.c b/drivers/firmware/efi/libstub/riscv-stub.c index 145c9f0ba217..7af07b33a993 100644 --- a/drivers/firmware/efi/libstub/riscv-stub.c +++ b/drivers/firmware/efi/libstub/riscv-stub.c @@ -30,32 +30,24 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, efi_loaded_image_t *image, efi_handle_t image_handle) { - unsigned long kernel_size = 0; - unsigned long preferred_addr; + unsigned long kernel_size, kernel_codesize, kernel_memsize; efi_status_t status; kernel_size = _edata - _start; + kernel_codesize = __init_text_end - _start; + kernel_memsize = kernel_size + (_end - _edata); *image_addr = (unsigned long)_start; - *image_size = kernel_size + (_end - _edata); - - /* - * RISC-V kernel maps PAGE_OFFSET virtual address to the same physical - * address where kernel is booted. That's why kernel should boot from - * as low as possible to avoid wastage of memory. Currently, dram_base - * is occupied by the firmware. So the preferred address for kernel to - * boot is next aligned address. If preferred address is not available, - * relocate_kernel will fall back to efi_low_alloc_above to allocate - * lowest possible memory region as long as the address and size meets - * the alignment constraints. - */ - preferred_addr = EFI_KIMG_PREFERRED_ADDRESS; - status = efi_relocate_kernel(image_addr, kernel_size, *image_size, - preferred_addr, efi_get_kimg_min_align(), - 0x0); + *image_size = kernel_memsize; + *reserve_size = *image_size; + status = efi_kaslr_relocate_kernel(image_addr, + reserve_addr, reserve_size, + kernel_size, kernel_codesize, kernel_memsize, + efi_kaslr_get_phys_seed(image_handle)); if (status != EFI_SUCCESS) { efi_err("Failed to relocate kernel\n"); *image_size = 0; } + return status; } diff --git a/drivers/firmware/efi/libstub/riscv.c b/drivers/firmware/efi/libstub/riscv.c index 8022b104c3e6..365f891e598f 100644 --- a/drivers/firmware/efi/libstub/riscv.c +++ b/drivers/firmware/efi/libstub/riscv.c @@ -96,3 +96,8 @@ void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt, csr_write(CSR_SATP, 0); jump_kernel(hartid, fdt); } + +void efi_icache_sync(unsigned long start, unsigned long end) +{ + asm volatile ("fence.i" ::: "memory"); +}