Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp1114650rwo; Sat, 22 Jul 2023 06:55:52 -0700 (PDT) X-Google-Smtp-Source: APBJJlHsRJsdtAQueRbg0YhSqIoxAqgBOCS35QOqUHbcR4bieTlg7CugxJe8GcyE6hv2zpqVslTO X-Received: by 2002:a05:6a20:13c8:b0:137:a3c9:aaa9 with SMTP id ho8-20020a056a2013c800b00137a3c9aaa9mr2839417pzc.16.1690034152367; Sat, 22 Jul 2023 06:55:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690034152; cv=none; d=google.com; s=arc-20160816; b=Dus0H1kMkfEZH2iCQ/HH7Zfbya1h75S9mbKGqWf3sPTxezF2ZvUiUf5P5SVBm0YSWE PuDMvgcFeb+tIV/Td7BEtkdgMqj8QTxekhnaTdxQ8NIiXF2/HKX9HChwPyBlVi1THkj4 DyEGnP93jDuoieTj4pBReA/yGFvnqd13tXCcp1VpFV5DPrt8UKnRIUZ9Ff/kFftvUPVF bb9xWeqJSAfgqRm3RH5jte/q+b/FDVBdU1rTMSaACKlXKiqg+W2IPUb8K/zmtFgpXhV2 AMPocp5aRwmo11wcTzePjXT44zzmV/qNsY4MPUKS0t8qb7ELlhtSOOBWpl4oj+HkJLTy M2BA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=YeCObNb4PmolJTFe26id2IecwNBs81NuN62SQUhE5h0=; fh=ftizTztA0Oz+jn5nCCiKoDD6oRw7mqIVVly9vFmvJ6M=; b=OU+9RwDJFGk5eaKLAqRuUt+ikBvpSk8t1y2o/ZokqVBbcRxPcfuM5T8xaXgObD4RGr vzbsUb+u5x3OTIlX7RzrlMkSX/M1Ww/fKW78HqdAECkzMjiN8NpEvl/hwCQg9QGs6NgX DfYRvTmMuL+qxtD7/SPc7KiolvPfmOqw/1OIud8lU/1iG+8lkrOERT9/ihvb7dN7aZzc wPM8lNm+XMEA169aoBdT7/ifrWX40h/q7UhLd6I1Be8MVkmlCugmds5fgitxZt68V/5f 4GF3a8ad8d8qE4AT5tHNKTEuv4FskrF9Ja7hnEVLUq7aM27lKETLa0D+0cs7I9swi/bs 4baA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=pUM+520v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q24-20020a637518000000b00553851380besi5355463pgc.397.2023.07.22.06.55.40; Sat, 22 Jul 2023 06:55:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=pUM+520v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230426AbjGVMoG (ORCPT + 99 others); Sat, 22 Jul 2023 08:44:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229552AbjGVMoF (ORCPT ); Sat, 22 Jul 2023 08:44:05 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81C90268E for ; Sat, 22 Jul 2023 05:44:03 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3fbc54cab6fso22550815e9.0 for ; Sat, 22 Jul 2023 05:44:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1690029842; x=1690634642; 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=YeCObNb4PmolJTFe26id2IecwNBs81NuN62SQUhE5h0=; b=pUM+520vb2S92A6ynS7MBf+a+TvlSycieG6x/eLAxB9vkNf2NAAk3hKz2lVbEhN6Km 9D+OpTxN358e0ePT4GlQNjD4y8JMqJED6JHYZ1vhglRCGL+/9vv8wgBa/4HC0Fg3s8wl rq+o2tb+8mmawwLyIZAoIgVi0mtbElZCDvcWyHCsAUhpD8yih7TyKqRWkFD9vcKvJ5l5 To5s+Yl8KRng8QyCvHUYBfFXpVY39ST6SArgtFX6PcsvC0376cKqK/E6lDNOuZdou2k4 ZmWsNfaeoEOrbmUCy2hQGQ6pkeaaasNFlU+Xmn7w+FgnOZcFNhGwUveLYe1tubOKHwSl xSJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690029842; x=1690634642; 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=YeCObNb4PmolJTFe26id2IecwNBs81NuN62SQUhE5h0=; b=FRBFbrfZeZIgNRKi3upvN7os+H4/wjZAmz7x9l4UppJwxzrD5GfrlDYjPEHkAOL0Ze cfESzbXuw5uyhdC0MqcXqAazLWDBhLW3NeqHd1G4ICpwn5GAqWFroi5RSmcvCBvWgw02 t8h3owYycw2VRJOIfNRZikTfCl0VM7BPap6hwh5PMBNgghQ/twF+0uRtcH5CxTNBBXmP FR/2WjubzuII4JTdRiY3gJYvOMVkmIdo1CNj82x+afjjl7tSRn6lUbKlXKR1w4ZHksfl Oyq2w5kKjamt+DNnwtUmN49Fm31D0C6om2+lQ62E+Z5InYaMQFUHF497g2SEN3TOLY1F 1SAw== X-Gm-Message-State: ABy/qLbo2aCUfKj/RDcR2HjoHdyGNJkv7BKvT8CWu+4W17kjfCAIUEwH DyYcNZIbTztQqWFW+gOnwdwRhw== X-Received: by 2002:a05:600c:21cf:b0:3fc:e00:5275 with SMTP id x15-20020a05600c21cf00b003fc0e005275mr2809721wmj.2.1690029841797; Sat, 22 Jul 2023 05:44:01 -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 n11-20020a7bcbcb000000b003fba92fad35sm7985875wmi.26.2023.07.22.05.44.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Jul 2023 05:44:01 -0700 (PDT) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Ard Biesheuvel , Kees Cook , 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 v6 5/5] riscv: libstub: Implement KASLR by using generic functions Date: Sat, 22 Jul 2023 14:38:50 +0200 Message-Id: <20230722123850.634544-6-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230722123850.634544-1-alexghiti@rivosinc.com> References: <20230722123850.634544-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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/include/asm/efi.h | 2 ++ arch/riscv/kernel/image-vars.h | 1 + drivers/firmware/efi/libstub/Makefile | 2 +- drivers/firmware/efi/libstub/riscv-stub.c | 33 +++++++++++------------ 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/arch/riscv/include/asm/efi.h b/arch/riscv/include/asm/efi.h index 29e9a0d84b16..00b24ba55035 100644 --- a/arch/riscv/include/asm/efi.h +++ b/arch/riscv/include/asm/efi.h @@ -51,4 +51,6 @@ void efi_virtmap_unload(void); unsigned long stext_offset(void); +void efi_icache_sync(unsigned long start, unsigned long end); + #endif /* _ASM_EFI_H */ 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/Makefile b/drivers/firmware/efi/libstub/Makefile index 11aba8a041ec..dc90a31b189f 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -88,7 +88,7 @@ lib-$(CONFIG_EFI_GENERIC_STUB) += efi-stub.o string.o intrinsics.o systable.o \ lib-$(CONFIG_ARM) += arm32-stub.o lib-$(CONFIG_ARM64) += kaslr.o arm64.o arm64-stub.o smbios.o lib-$(CONFIG_X86) += x86-stub.o -lib-$(CONFIG_RISCV) += riscv.o riscv-stub.o +lib-$(CONFIG_RISCV) += kaslr.o riscv.o riscv-stub.o lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) diff --git a/drivers/firmware/efi/libstub/riscv-stub.c b/drivers/firmware/efi/libstub/riscv-stub.c index 145c9f0ba217..c96d6dcee86c 100644 --- a/drivers/firmware/efi/libstub/riscv-stub.c +++ b/drivers/firmware/efi/libstub/riscv-stub.c @@ -30,32 +30,29 @@ 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; } + +void efi_icache_sync(unsigned long start, unsigned long end) +{ + asm volatile ("fence.i" ::: "memory"); +} -- 2.39.2