Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp837031pxb; Thu, 21 Apr 2022 11:23:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEVdMkpkFLyij5Oe9JM/yf06qf+eCk36KhLs0yknUjClbUJSVG9Z6r891Mhr+cY/+yMeqM X-Received: by 2002:a17:90a:28a4:b0:1d0:50f0:1e04 with SMTP id f33-20020a17090a28a400b001d050f01e04mr998654pjd.86.1650565435309; Thu, 21 Apr 2022 11:23:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650565435; cv=none; d=google.com; s=arc-20160816; b=sA4DHCEV5usptMBv5eOl7AJ6GEmNMDX9EtIr49w3y4un+d3gHbWhWiRBe4pFlycMyk Bmyz1Z0soVQV1KyKRESDAZxGptIE/6PtI/wTu+OTXnEwq8mkMU7Qxdfbg+R03BLAKjWq rzUxognwAMrNLzPtOTULpZqFXaxBBia8GnYy1N0MMGi6la1Kf0n6wwPpvafyZu7pVGCC pII5F15JsL7nWnViWZ3mTDLYuvnrkQMhERgWgPdrBd8R9mrCAAI/zs+dCimMfS6qwRuN B70BO+WyZSyCjn8I3mMAT5wiQ4ocaooNPlDGt2T+c7YxzunsTMqUMibpwDuBKSSaXNDG ho5A== 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; bh=9/lcJy5SUnkgCd5YmqUJZq9tbjD6YZpUbyMK4pnkVJE=; b=id2fAkwl7WoMxt17GA3APeE8bqe9p+NTUoRQFUr5OVXnZBkeAx/dcw6FnHQ7f8trwD mPOPtF+awI79QrAVUB+vA0kaJ/vkNBqedgxXC0wQaRG/GcZHCxKJU427j9bFNlMNlpc/ pJj5+cSpHAOBWGZfxvQRZ1bNmMekKuQesn1VTEb1Ypmj6Klnvb9745+pDFjjDCn6fIJ2 Ldb/3ZkQdK5hif2NGqqH/5uD6sCCnv6UtJjwqeXGc0QuUpyHnYwMyhfLjJQbVgerFR6R GrCOjE6ztFrUvCZreG+UiWh0EweHe4bGbn5uODA2R6pH7Gh9YnMKvw5XVqwbmt0y00qU Z27Q== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 20-20020a630d54000000b003aa2d16549fsi6133133pgn.428.2022.04.21.11.23.34; Thu, 21 Apr 2022 11:23:55 -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; 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348976AbiDSGpz (ORCPT + 99 others); Tue, 19 Apr 2022 02:45:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348966AbiDSGpw (ORCPT ); Tue, 19 Apr 2022 02:45:52 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CC1E30F7F; Mon, 18 Apr 2022 23:43:10 -0700 (PDT) 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 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII 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 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 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, -- 2.25.1