Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp692986pxf; Thu, 1 Apr 2021 11:03:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4AzDs1JNHLF46N3WmjMMLj4fnJ/QzgTEkWrcFxsSX8Z0RtNPaG5N1ybOnO2QJWIk+bIEs X-Received: by 2002:a92:730a:: with SMTP id o10mr7673825ilc.160.1617300236623; Thu, 01 Apr 2021 11:03:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617300236; cv=none; d=google.com; s=arc-20160816; b=0SxtsfZdXjcY4Zja84lDKP4jnjCMcM2nTHJ8KsYO72RWyWRK00PKgEme8vo4S/YtLd rXR/nFNFy4ROOGL7To5BljqdEFgZCC1lFjgaalxNPQwInh4ZA7moEjXg/AFii1uYkFfV KkeWBncUggRGccl/THSM8jdXdAk8hsjO8r/1et+q12M7FGQWMq6qVUW41cdQNM6Bs9CL 7nwyadJ1nBMIS7kWK8ODJ6GlC1UA/3fHnBHH1pNNukaTOwWjop4go+0GknC1fXGaZb4x baIil2KR+qL3IiDOtxM8+yWnlkP9nV/EPfl6rRAEIGQige4wD09Q8ArRZi1gMD9t1h2O oVqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=WP/uJphQHhkR5xanYkbaPSj//DmPvwcF5xnqx2P5GiA=; b=AhmnGmEsKIL9kyWStTsNd79fzENeMGW/V6qV/xScuYQCGmViK0uldWZyq+zlZ1j51o MKjZ7fyZzwU2AGgOvlw6vtSYl7i/LWka+bjgt8HWP0uI9PUDRI8zVi7vSPwWEBcLFIup jnXzsTEcXraB5YaZU6tLyohX1KmLoE7bHeaUhZ9Fwber5yY9+6ITAk/iAFR9F2Cf97tt DNAU0fWTRxZ2n2+X7+mr4ZG4WKFxzrVkzWaqGuyYQZIBf1KvoAUya+tJzXTlxn6xcchl jtxMCgx+3qScLVK8cLOWgvbSxnAfIgHFCLF87UFK7XV+0cOkRpxA3N+xBeEy7qqrsL/C +1qQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q13si5076317ilv.154.2021.04.01.11.03.42; Thu, 01 Apr 2021 11:03:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234591AbhDASBL (ORCPT + 99 others); Thu, 1 Apr 2021 14:01:11 -0400 Received: from mail.loongson.cn ([114.242.206.163]:43918 "EHLO loongson.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S234640AbhDARq4 (ORCPT ); Thu, 1 Apr 2021 13:46:56 -0400 Received: from loongson.localdomain (unknown [113.200.148.30]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9DxX8s0ymVgtFkDAA--.7389S5; Thu, 01 Apr 2021 21:27:18 +0800 (CST) From: Jinyang He To: Thomas Bogendoerfer , Paul Burton Cc: linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 3/3] MIPS: relocate: Add support to relocate kernel auto Date: Thu, 1 Apr 2021 21:27:13 +0800 Message-Id: <1617283633-18598-4-git-send-email-hejinyang@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1617283633-18598-1-git-send-email-hejinyang@loongson.cn> References: <1617283633-18598-1-git-send-email-hejinyang@loongson.cn> X-CM-TRANSID: AQAAf9DxX8s0ymVgtFkDAA--.7389S5 X-Coremail-Antispam: 1U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xUvIeHDUUUU X-CM-SenderInfo: pkhmx0p1dqwqxorr0wxvrqhubq/ Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org These asm code is based on relocate.c. And in this stage, only do relocate for _text and ex_table. Signed-off-by: Jinyang He --- arch/mips/kernel/head.S | 149 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/arch/mips/kernel/head.S b/arch/mips/kernel/head.S index a25af1d..dc59b11 100644 --- a/arch/mips/kernel/head.S +++ b/arch/mips/kernel/head.S @@ -13,6 +13,7 @@ * Kevin Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com * Copyright (C) 2000 MIPS Technologies, Inc. All rights reserved. */ + #include #include @@ -26,6 +27,152 @@ #include + .macro do_relocate_kernel + .set push +#ifdef CONFIG_RELOCATABLE +#define R_MIPS_32 2 +#define R_MIPS_26 4 +#define R_MIPS_HI16 5 +#define R_MIPS_64 18 + /* Save args */ + move s0, a0 + move s1, a1 + move s2, a2 + move s3, a3 + + /* Whether the PC meets expectation */ + bal 1f +1: move a0, ra + PTR_LA a1, 1b + PTR_SUB a0, a0, a1 + beqz a0, 999f + + /* Whether offset 64KB aligned */ + and t0, a0, 0xffff + bnez t0, 999f + + /* Whether current _text and _end are in the PC region */ + PTR_LA t0, _end + PTR_ADDU t1, t0, a0 + PTR_SRL t1, 28 + PTR_SRL t0, 28 + bne t0, t1, 999f + + PTR_LA t0, _text + PTR_ADDU t1, t0, a0 + move a3, t1 + PTR_SRL t1, 28 + PTR_SRL t0, 28 + bne t0, t1, 999f + + /* get current __start/stop___ex_table address */ + PTR_LA a1, __start___ex_table + PTR_ADDU a1, a0 + PTR_LA a2, __stop___ex_table + PTR_ADDU a2, a0 + + /* + * a0: offset + * a1: current __start___ex_table + * a2: current __stop___ex_table + */ +1: beq a1, a2, 998f + PTR_L t0, 0(a1) + PTR_ADDU t0, a0 + PTR_S t0, 0(a1) + LONG_ADDIU a1, PTRSIZE + b 1b + +998: + /* get current _relocation_start address */ + PTR_LA a1, _relocation_start + PTR_ADDU a1, a0 + + /* + * a0: offset + * a1: _relocation_start[] + * a2: offset >> 16, to relocate R_MIPS_HI16 + * a3: current _text + * t0: to load _relocation_start[] + * t1: relocation type + * t2: current relocate position + * t3: temporarily + * t8: 0x00ffffff, mask, to get relocate position + * t9: offset >> 2, to relocate R_MIPS_26 + */ + li t8, 0x00ffffff + LONG_SRL t9, a0, 2 + LONG_SRL a2, a0, 16 +1: lw t0, 0(a1) + beqz t0, 900f + LONG_SRL t1, t0, 24 + and t1, 0xff + and t2, t0, t8 + LONG_SLL t2, 2 + PTR_ADDU t2, a3 + + li t3, R_MIPS_64 + beq t1, t3, 964f + li t3, R_MIPS_32 + beq t1, t3, 932f + li t3, R_MIPS_26 + beq t1, t3, 926f + li t3, R_MIPS_HI16 + beq t1, t3, 916f + b 999f + +964: +#ifdef CONFIG_64BIT + ld t3, 0(t2) + LONG_ADDU t3, a0 + sd t3, 0(t2) +#endif + LONG_ADDIU a1, 4 + b 1b + +932: + lw t3, 0(t2) + LONG_ADDU t3, a0 + sw t3, 0(t2) + LONG_ADDIU a1, 4 + b 1b + +926: + lw t3, 0(t2) + LONG_ADDU t3, t9 + sw t3, 0(t2) + LONG_ADDIU a1, 4 + b 1b + +916: + lw t3, 0(t2) + LONG_ADDU t3, a2 + sw t3, 0(t2) + LONG_ADDIU a1, 4 + b 1b + +900: + /* Complete! And flush i-cache */ + PTR_LA a0, _text + PTR_LA a1, _end + synci 0(a0) + rdhwr t0, $1 + beqz t0, 999f + PTR_ADDU a0, t0 + PTR_SUBU t0, a1, a0 + bgtz t0, 900b + sync + +999: + /* Restore args */ + move a0, s0 + move a1, s1 + move a2, s2 + move a3, s3 +#endif /* CONFIG_RELOCATABLE */ + .set pop + .endm + /* * For the moment disable interrupts, mark the kernel mode and * set ST0_KX so that the CPU does not spit fire when using @@ -83,6 +230,8 @@ FEXPORT(__kernel_entry) NESTED(kernel_entry, 16, sp) # kernel entry point + do_relocate_kernel + kernel_entry_setup # cpu specific setup setup_c0_status_pri -- 2.1.0