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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DECE0C636CD for ; Fri, 10 Feb 2023 08:47:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231591AbjBJIro (ORCPT ); Fri, 10 Feb 2023 03:47:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231342AbjBJIrl (ORCPT ); Fri, 10 Feb 2023 03:47:41 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D4E277B176 for ; Fri, 10 Feb 2023 00:47:39 -0800 (PST) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8CxYvCqBOZjUcAQAA--.33663S3; Fri, 10 Feb 2023 16:47:38 +0800 (CST) Received: from bogon.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8AxX+SpBOZjLUYwAA--.56353S2; Fri, 10 Feb 2023 16:47:37 +0800 (CST) From: Youling Tang To: Huacai Chen , Xi Ruoyao , Jinyang He Cc: Xuerui Wang , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 0/5] LoongArch: Add kernel relocation and KASLR support Date: Fri, 10 Feb 2023 16:47:31 +0800 Message-Id: <1676018856-26520-1-git-send-email-tangyouling@loongson.cn> X-Mailer: git-send-email 2.1.0 X-CM-TRANSID: AQAAf8AxX+SpBOZjLUYwAA--.56353S2 X-CM-SenderInfo: 5wdqw5prxox03j6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoWxZryUGrWUXF1fCFWfWFWktFb_yoW5Xrykpa 4Yyr98JF48Gr1fXwsxt3y5ur15t3Z7Kr1agFsFyryFkr1agF1UZr10vwnrXFyUtw4rXrWI gFn5KF13K3W7AaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU b7AYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_Jrv_JF1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVW8JVW5JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4 x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4UJVWxJr1l e2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2xF0cIa020Ex4CE44I27wAqx4xG64xvF2 IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_JrI_JrylYx0Ex4A2jsIE14v26r1j6r4U McvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCF04k20xvY0x0EwIxGrwCFx2 IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v2 6r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67 AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IY s7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Jr 0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU8j-e5UUUUU== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch series to support kernel relocation and KASLR (only 64bit). Tested the kernel images built with new toolchain (Binutils-2.40 + patched GCC-12.2) and old toolchain (kernel.org cross toolchain [1]) on a 3A5000-7A2000-EVB. With CONFIG_RANDOMIZE_BASE=y, the results are: 1. first boot, new toolchain: $ sudo cat /proc/iomem | grep Kernel 01080000-0189ffff : Kernel code 018a0000-01deb5ff : Kernel data 01deb600-01ef6e9f : Kernel bss 2. second boot, new toolchain: $ sudo cat /proc/iomem | grep Kernel 012f0000-01b0ffff : Kernel code 01b10000-0205b5ff : Kernel data 0205b600-02166e9f : Kernel bss 3. first boot, old toolchain: 010e0000-018fffff : Kernel code 01900000-01e591ff : Kernel data 01e59200-01f56dcf : Kernel bss 4. second boot, old toolchain: 010b0000-018cffff : Kernel code 018d0000-01e291ff : Kernel data 01e29200-01f26dcf : Kernel bss Changes from v3: - JUMP_LINK_ADDR renamed to JUMP_VIRT_ADDR, and use the way of parameter passing. - Reimplement kernel relocation, when the link address and load address are different, realize the effect of adaptive relocation (one of the usage scenarios is kdump operation). - Reimplement KASLR. Changes from v2: - Correctly fixup pcaddi12i/ori/lu32i.d/lu52i.d sequence generated by GNU as <= 2.39 for la.pcrel. Changes from v1 to v2: - Relocate the handlers instead of using a trampoline, to avoid performance issue on NUMA systems. - Fix compiler warnings. Xi Ruoyao (2): LoongArch: Use la.pcrel instead of la.abs when it's trivially possible LoongArch: Use la.pcrel instead of la.abs for exception handlers Youling Tang (3): LoongArch: Add JUMP_VIRT_ADDR macro implementation to avoid using la.abs LoongArch: Add support for kernel relocation LoongArch: Add support for kernel address space layout randomization (KASLR) arch/loongarch/Kconfig | 38 +++++ arch/loongarch/Makefile | 5 + arch/loongarch/include/asm/inst.h | 1 + arch/loongarch/include/asm/setup.h | 6 +- arch/loongarch/include/asm/stackframe.h | 14 +- arch/loongarch/include/asm/uaccess.h | 1 - arch/loongarch/kernel/Makefile | 2 + arch/loongarch/kernel/entry.S | 2 +- arch/loongarch/kernel/genex.S | 40 ++++- arch/loongarch/kernel/head.S | 31 +++- arch/loongarch/kernel/relocate.c | 215 ++++++++++++++++++++++++ arch/loongarch/kernel/traps.c | 158 ++++++++++++++--- arch/loongarch/kernel/vmlinux.lds.S | 11 +- arch/loongarch/mm/tlb.c | 23 +-- arch/loongarch/mm/tlbex.S | 72 +++++++- arch/loongarch/power/suspend_asm.S | 5 +- 16 files changed, 560 insertions(+), 64 deletions(-) create mode 100644 arch/loongarch/kernel/relocate.c -- 2.37.3