Message ID | 20230207142822.52172-1-xry111@xry111.site |
---|---|
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2388:b0:96:219d:e725 with SMTP id i8csp4043573dyf; Tue, 7 Feb 2023 06:32:11 -0800 (PST) X-Google-Smtp-Source: AK7set/RwkI4alpSEjsLK2YHMLJeJOGKmvNfwCcDgLyfaDjo2eZpxO3lFXqDasP3Owej6TIHxI46 X-Received: by 2002:a17:906:1101:b0:88d:33fe:3308 with SMTP id h1-20020a170906110100b0088d33fe3308mr3522908eja.9.1675780331252; Tue, 07 Feb 2023 06:32:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675780331; cv=none; d=google.com; s=arc-20160816; b=AIEmOsFhfvQs+9XLGN+Ryou+0yEEa4WhwW9HvjmJJ1gExW9/ScI6kDl3Ju0JVVwUeC 9w5oPt/OEuZWQRrhJL5n/2G1widMQ/aR/wQeBtneQ2ZIiykLsA2PU9uk6qrzB91AWCBt F8NMQ04kB9iq3cecf1LmUqLyRO5QYvZqLV7oSEGayG7drrv53xOE6SxpayTYkZzDnMBv MOGzaxeGK5F6MpXQP8PfWl47sj6AdChBTd0HWEURnSm8Irc67wVyxo26IxoCvaWq9lqw vQ1P++FijenBizTqQYzimpl8XHhzoldLUrfGu3YptvXPwBzUXuzoYIu0BvdiapH3JaT9 REJw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=09Rpjlx7kfRqXkpQEAEx8fnGu0W4rsCCEHmkMRDG5Gg=; b=pYPsA7egJrQnPEAs4rDTPwXrjN4rV6Eqig/tI1fdu7ZJbfSyihiIGS5PpgAnRI+a1E rCXDzCfB/OobQlaQWJCqWloWk35ey95u5HM5uP9yVuOhP5iN0Zrr1tARwNDT4rs05u0k RhU4KH9uWH7bUyZQoMSGx8oUGQYPB/WEDI5TUEGpUxQ0/EHmZI3y7wfMuTyo4FhEt3TQ Se2gqpkFT9xH6NbSMQ1Pv75varSnOpWyd8hPAyp6popG3whI2Z67gy93JA31tyfNONuI nOqme62jvO42v8/P7my88i1bGmP6VMsvV3BfWx2r+WDiM3Rs/u9JyjddRrTbBBkcFFsg ZtWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xry111.site header.s=default header.b=QQdLesPI; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=xry111.site Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u27-20020a170906109b00b0087d0121def7si21933206eju.248.2023.02.07.06.31.48; Tue, 07 Feb 2023 06:32:11 -0800 (PST) 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=@xry111.site header.s=default header.b=QQdLesPI; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=xry111.site Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232689AbjBGO3e (ORCPT <rfc822;kmanaouilinux@gmail.com> + 99 others); Tue, 7 Feb 2023 09:29:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232628AbjBGO3a (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 7 Feb 2023 09:29:30 -0500 Received: from xry111.site (xry111.site [89.208.246.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8273B213C for <linux-kernel@vger.kernel.org>; Tue, 7 Feb 2023 06:29:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1675780169; bh=nzxLFo92EKu8lqBicholQ9z2qhCzdQ0w+5WmPOQyusM=; h=From:To:Cc:Subject:Date:From; b=QQdLesPIv8wFuu49I5y+pGnKnaHKW7WFyZES2AAe3X6A5sA4orv+LHQ0U3QUB7uO/ SSMaqVbeQbFFS0joiy6/WbUQnCt1V8oZXPNOTb6bXgO1C8JQvnsTaLvwPm1FLN6KwU tZ1q4WJTf59d16RnMHQPA6XWoJYcLTuUMIvl+TL8= Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11cc:4100:dc73:854d:832e:2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id CD77565DC6; Tue, 7 Feb 2023 09:29:21 -0500 (EST) From: Xi Ruoyao <xry111@xry111.site> To: Youling Tang <tangyouling@loongson.cn>, Huacai Chen <chenhuacai@kernel.org>, Jinyang He <hejinyang@loongson.cn> Cc: Xuerui Wang <kernel@xen0n.name>, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, Xi Ruoyao <xry111@xry111.site> Subject: [PATCH v2 0/5] LoongArch: Add kernel relocation and KASLR support Date: Tue, 7 Feb 2023 22:28:17 +0800 Message-Id: <20230207142822.52172-1-xry111@xry111.site> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757183036471982558?= X-GMAIL-MSGID: =?utf-8?q?1757183036471982558?= |
Series |
LoongArch: Add kernel relocation and KASLR support
|
|
Message
Xi Ruoyao
Feb. 7, 2023, 2:28 p.m. UTC
This patch series to support kernel relocation and KASLR (only 64bit). I've only tested new toolchains (CONFIG_AS_HAS_EXPLICIT_RELOCS=y) consisted of Binutils-2.40, and GCC-12.2 heavily patched to support new relocs. Unfortunately I've purged my old toolchain installation (because it contained a buggy GCC-12.1 miscompiling some code). Please test the configuration with old toolchain. Test results with CONFIG_RANDOMIZE_BASE=y on a 3A5000-7A2000-EVB: First boot: $ sudo cat /proc/iomem | grep Kernel 010e0000-018fffff : Kernel code 01900000-01e4b5ff : Kernel data 01e4b600-01f56e9f : Kernel bss Second boot: $ sudo cat /proc/iomem | grep Kernel 019a0000-021bffff : Kernel code 021c0000-0270b5ff : Kernel data 0270b600-02816e9f : Kernel bss Changes from v1: - 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_LINK_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 | 37 +++++ arch/loongarch/Makefile | 5 + arch/loongarch/include/asm/page.h | 6 + arch/loongarch/include/asm/setup.h | 6 +- arch/loongarch/include/asm/stackframe.h | 13 +- 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 | 30 +++- arch/loongarch/kernel/relocate.c | 211 ++++++++++++++++++++++++ arch/loongarch/kernel/setup.c | 3 + arch/loongarch/kernel/traps.c | 138 +++++++++++++--- 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 +- 17 files changed, 543 insertions(+), 62 deletions(-) create mode 100644 arch/loongarch/kernel/relocate.c
Comments
Hi, Ruoyao On 02/07/2023 10:28 PM, Xi Ruoyao wrote: > This patch series to support kernel relocation and KASLR (only 64bit). > > I've only tested new toolchains (CONFIG_AS_HAS_EXPLICIT_RELOCS=y) > consisted of Binutils-2.40, and GCC-12.2 heavily patched to support new > relocs. Unfortunately I've purged my old toolchain installation (because > it contained a buggy GCC-12.1 miscompiling some code). Please test the > configuration with old toolchain. I tested it through QEMU. Testing in the new toolchains is successful, / # dmesg | head [ 0.000000] Linux version 6.2.0-rc7+ (root@bogon) (loongarch64-unknown-linux-gnu-gcc (GCC) 13.0.0 20220906 (experimental), GNU ld (GNU Binutils) 2.39.50.20220906) #67 SMP PREEMPT Wed Feb 8 09:42:49 CST 2023 / # cat /proc/iomem | head 00000000-0fffffff : System RAM 00000000-002c3fff : Reserved 002c4000-008c3fff : Reserved 02a50000-0399ffff : Kernel code 039a0000-0426d9ff : Kernel data 0426da00-0439ae37 : Kernel bss But testing in the old toolchains failed, stuck in the following position, [ 0.000000] Linux version 6.2.0-rc7+ (root@bogon) (loongarch64-unknown-linux-gnu-gcc (GCC) 12.1.0, GNU ld (GNU Binutils) 2.38.50.20220519) #68 SMP PREEMPT Wed Feb 8 09:52:49 CST 2023 ... [ 0.000000] rcu: srcu_init: Setting srcu_struct sizes based on contention. [ 0.000000] Constant clock event device register [ 0.000000] clocksource: Constant: mask: 0xffffffffffffffff max_cycles: 0x171024e7e0, max_idle_ns: 440795205315 ns [ 0.000289] sched_clock: 64 bits at 100MHz, resolution 10ns, wraps every 4398046511100ns [ 0.000715] Constant clock source device register In the case of "[Patch v2 2/5] LoongArch: use la.pcrel instenad of la.abs for exception handlerS", the above failure will occur. Patch2 may have certain problems when using the old toolchains. Youling. > > Test results with CONFIG_RANDOMIZE_BASE=y on a 3A5000-7A2000-EVB: > > First boot: > > $ sudo cat /proc/iomem | grep Kernel > 010e0000-018fffff : Kernel code > 01900000-01e4b5ff : Kernel data > 01e4b600-01f56e9f : Kernel bss > > Second boot: > > $ sudo cat /proc/iomem | grep Kernel > 019a0000-021bffff : Kernel code > 021c0000-0270b5ff : Kernel data > 0270b600-02816e9f : Kernel bss > > Changes from v1: > > - 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_LINK_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 | 37 +++++ > arch/loongarch/Makefile | 5 + > arch/loongarch/include/asm/page.h | 6 + > arch/loongarch/include/asm/setup.h | 6 +- > arch/loongarch/include/asm/stackframe.h | 13 +- > 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 | 30 +++- > arch/loongarch/kernel/relocate.c | 211 ++++++++++++++++++++++++ > arch/loongarch/kernel/setup.c | 3 + > arch/loongarch/kernel/traps.c | 138 +++++++++++++--- > 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 +- > 17 files changed, 543 insertions(+), 62 deletions(-) > create mode 100644 arch/loongarch/kernel/relocate.c >
On Wed, 2023-02-08 at 12:37 +0800, Youling Tang wrote: > In the case of "[Patch v2 2/5] LoongArch: use la.pcrel instenad of > la.abs for exception handlerS", the above failure will occur. > > Patch2 may have certain problems when using the old toolchains. > > Youling. Thanks for the test... The problem is: old toolchain uses pcaddu12i/ori/lu32i.d/lu52i.d/add.d for a 3-operand la.pcrel, while the new toolchain uses pcalau12i/addi.d/lu32i/lu52i/add.d. (I've somehow forgotten all the difference!) We can fix it with something like... > +void reloc_handler(unsigned long handler, struct handler_reloc *rel) > +{ > + if (!rel) > + return; > + > + for (unsigned long i = 0; i < rel->cnt; i++) { > + unsigned long pc = handler + rel->entries[i].offset; > + unsigned long v = rel->entries[i].sym; /* anchor etc. moved into do_reloc_pcalau12i */ > + union loongarch_instruction *insn = > + (union loongarch_instruction *)pc; switch insn[0]->reg1i20_format->reg1i20_format { case pcaddu12i_op: do_reloc_pcaddu12i(insn, pc, v); break; case pcalau12i_op: /* TODO: add it for asm/inst.h */ do_reloc_pcalau12i(insn, pc, v); break; default: panic("what the f**k"); } Alternatively, we can also emit the pcalau12i/addi.d/lu32i/lu52i sequence and overwrite the pcaddu12i/ori sequence generated by the old toolchain. Which way do you like?
On 02/08/2023 04:27 PM, Xi Ruoyao wrote: > On Wed, 2023-02-08 at 12:37 +0800, Youling Tang wrote: >> In the case of "[Patch v2 2/5] LoongArch: use la.pcrel instenad of >> la.abs for exception handlerS", the above failure will occur. >> >> Patch2 may have certain problems when using the old toolchains. >> >> Youling. > > Thanks for the test... > > The problem is: old toolchain uses pcaddu12i/ori/lu32i.d/lu52i.d/add.d > for a 3-operand la.pcrel, while the new toolchain uses > pcalau12i/addi.d/lu32i/lu52i/add.d. (I've somehow forgotten all the > difference!) > > We can fix it with something like... > >> +void reloc_handler(unsigned long handler, struct handler_reloc *rel) >> +{ >> + if (!rel) >> + return; >> + >> + for (unsigned long i = 0; i < rel->cnt; i++) { >> + unsigned long pc = handler + rel->entries[i].offset; >> + unsigned long v = rel->entries[i].sym; > > /* anchor etc. moved into do_reloc_pcalau12i */ > >> + union loongarch_instruction *insn = >> + (union loongarch_instruction *)pc; > > switch insn[0]->reg1i20_format->reg1i20_format { > case pcaddu12i_op: > do_reloc_pcaddu12i(insn, pc, v); > break; > case pcalau12i_op: /* TODO: add it for asm/inst.h */ > do_reloc_pcalau12i(insn, pc, v); > break; > default: > panic("what the f**k"); > } > > Alternatively, we can also emit the pcalau12i/addi.d/lu32i/lu52i > sequence and overwrite the pcaddu12i/ori sequence generated by the old > toolchain. > > Which way do you like? v3 tested successfully in both new and old toolchains. Youling. >