From patchwork Tue Apr 19 07:01:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mawupeng X-Patchwork-Id: 12817443 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C443C433EF for ; Tue, 19 Apr 2022 06:43:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5DD18D0050; Tue, 19 Apr 2022 02:43:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BDB88D0047; Tue, 19 Apr 2022 02:43:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7751C8D0050; Tue, 19 Apr 2022 02:43:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 5E7E78D0047 for ; Tue, 19 Apr 2022 02:43:12 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 34B73221DE for ; Tue, 19 Apr 2022 06:43:12 +0000 (UTC) X-FDA: 79372686624.16.4702C55 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf16.hostedemail.com (Postfix) with ESMTP id 9D321180007 for ; Tue, 19 Apr 2022 06:43:10 +0000 (UTC) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4KjDlB2KG7zhXb2; Tue, 19 Apr 2022 14:43:02 +0800 (CST) Received: from dggpemm500014.china.huawei.com (7.185.36.153) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 19 Apr 2022 14:43:08 +0800 Received: from localhost.localdomain (10.175.112.125) by dggpemm500014.china.huawei.com (7.185.36.153) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 19 Apr 2022 14:43:07 +0800 From: Wupeng Ma To: , , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH 2/2] arm64/boot: Add support to relocate kernel image to mirrored region without kaslr Date: Tue, 19 Apr 2022 15:01:50 +0800 Message-ID: <20220419070150.254377-3-mawupeng1@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220419070150.254377-1-mawupeng1@huawei.com> References: <20220419070150.254377-1-mawupeng1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500014.china.huawei.com (7.185.36.153) X-CFilter-Loop: Reflected Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf16.hostedemail.com: domain of mawupeng1@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=mawupeng1@huawei.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 9D321180007 X-Stat-Signature: mn1ymndpdonb7z61eitpar84n1ooe71f X-HE-Tag: 1650350590-912355 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Ma Wupeng Add support to relocate kernel image to mirrord regions if KASLR doesn't work. If a suiable mirrored slot if found, call efi_random_alloc() with random_seed be zero to relocate kernel code and data to the first slot available. Signed-off-by: Ma Wupeng --- drivers/firmware/efi/libstub/arm64-stub.c | 10 ++++++++++ drivers/firmware/efi/libstub/randomalloc.c | 1 + 2 files changed, 11 insertions(+) diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c index 39b774853b93..851a8948cafb 100644 --- a/drivers/firmware/efi/libstub/arm64-stub.c +++ b/drivers/firmware/efi/libstub/arm64-stub.c @@ -190,6 +190,16 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, } if (status != EFI_SUCCESS) { + if (efi_mirror_found) { + status = efi_random_alloc(*reserve_size, min_kimg_align, + reserve_addr, 0, + efi_mirror_found); + if (status == EFI_SUCCESS) + goto out; + + efi_err("Failed to relocate kernel to mirrored region\n"); + } + if (!check_image_region((u64)_text, kernel_memsize)) { efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n"); } else if (IS_ALIGNED((u64)_text, min_kimg_align)) { diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmware/efi/libstub/randomalloc.c index dd81d6c3c406..d5f4249943a7 100644 --- a/drivers/firmware/efi/libstub/randomalloc.c +++ b/drivers/firmware/efi/libstub/randomalloc.c @@ -50,6 +50,7 @@ unsigned long get_entry_num_slots(efi_memory_desc_t *md, */ #define MD_NUM_SLOTS(md) ((md)->virt_addr) +/* random_seed == 0 means alloc mem from the first suitable slot */ efi_status_t efi_random_alloc(unsigned long size, unsigned long align, unsigned long *addr,