From: Dave Martin <dave.martin@linaro.org> To: linux-arm-kernel@lists.infradead.org Cc: Jean Pihet <j-pihet@ti.com>, linux-omap@vger.kernel.org Subject: [PATCH 1/1] ARM: Thumb-2: Symbol manipulation macros for function body copying Date: Tue, 11 Jan 2011 18:02:31 -0600 [thread overview] Message-ID: <1294790551-17069-2-git-send-email-dave.martin@linaro.org> (raw) In-Reply-To: <1294790551-17069-1-git-send-email-dave.martin@linaro.org> In low-level board support code, there is sometimes a need to copy a function body to another location at run-time. A straightforward call to memcpy doesn't work in Thumb-2, because bit 0 of external Thumb function symbols is set to 1, indicating that the function is Thumb. Without corrective measures, this will cause an off-by-one copy, and the copy may be called using the wrong instruction set. This patch adds macros to help with such cases. Particular care is needed, because C doesn't guarantee any defined behaviour when casting a function pointer to any other type. This has been observed to lead to strange optimisation side-effects when doing the arithmetic which is required in order to copy/move function bodies correctly in Thumb-2. Signed-off-by: Dave Martin <dave.martin@linaro.org> --- KernelVersion: next-20110111 arch/arm/include/asm/unified.h | 26 ++++++++++++++++++++++++++ 1 files changed, 26 insertions(+), 0 deletions(-) diff --git a/arch/arm/include/asm/unified.h b/arch/arm/include/asm/unified.h index bc63116..636a765 100644 --- a/arch/arm/include/asm/unified.h +++ b/arch/arm/include/asm/unified.h @@ -24,6 +24,32 @@ .syntax unified #endif +#ifndef __ASSEMBLY__ +#include <linux/types.h> +#define __funcp_to_uint(funcp) ({ \ + uintptr_t __result; \ + \ + asm("" : "=r" (__result) : "0" (funcp)); \ + __result; \ + }) +#define __uint_to_funcp(i, funcp) ({ \ + typeof(funcp) __result; \ + \ + asm("" : "=r" (__result) : "0" (i)); \ + __result; \ + }) +#define FSYM_REBASE(funcp, dest_buf) \ + __uint_to_funcp((uintptr_t)(dest_buf) | FSYM_TYPE(funcp), funcp) + +#ifdef CONFIG_THUMB2_KERNEL +#define FSYM_BASE(funcp) ((void *)(__funcp_to_uint(funcp) & ~(uintptr_t)1)) +#define FSYM_TYPE(funcp) (__funcp_to_uint(funcp) & 1) +#else /* !CONFIG_THUMB2_KERNEL */ +#define FSYM_BASE(funcp) ((void *)__funcp_to_uint(funcp)) +#define FSYM_TYPE(funcp) 0 +#endif /* !CONFIG_THUMB2_KERNEL */ +#endif /* !__ASSEMBLY__ */ + #ifdef CONFIG_THUMB2_KERNEL #if __GNUC__ < 4 -- 1.7.1
WARNING: multiple messages have this Message-ID (diff)
From: dave.martin@linaro.org (Dave Martin) To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 1/1] ARM: Thumb-2: Symbol manipulation macros for function body copying Date: Tue, 11 Jan 2011 18:02:31 -0600 [thread overview] Message-ID: <1294790551-17069-2-git-send-email-dave.martin@linaro.org> (raw) In-Reply-To: <1294790551-17069-1-git-send-email-dave.martin@linaro.org> In low-level board support code, there is sometimes a need to copy a function body to another location at run-time. A straightforward call to memcpy doesn't work in Thumb-2, because bit 0 of external Thumb function symbols is set to 1, indicating that the function is Thumb. Without corrective measures, this will cause an off-by-one copy, and the copy may be called using the wrong instruction set. This patch adds macros to help with such cases. Particular care is needed, because C doesn't guarantee any defined behaviour when casting a function pointer to any other type. This has been observed to lead to strange optimisation side-effects when doing the arithmetic which is required in order to copy/move function bodies correctly in Thumb-2. Signed-off-by: Dave Martin <dave.martin@linaro.org> --- KernelVersion: next-20110111 arch/arm/include/asm/unified.h | 26 ++++++++++++++++++++++++++ 1 files changed, 26 insertions(+), 0 deletions(-) diff --git a/arch/arm/include/asm/unified.h b/arch/arm/include/asm/unified.h index bc63116..636a765 100644 --- a/arch/arm/include/asm/unified.h +++ b/arch/arm/include/asm/unified.h @@ -24,6 +24,32 @@ .syntax unified #endif +#ifndef __ASSEMBLY__ +#include <linux/types.h> +#define __funcp_to_uint(funcp) ({ \ + uintptr_t __result; \ + \ + asm("" : "=r" (__result) : "0" (funcp)); \ + __result; \ + }) +#define __uint_to_funcp(i, funcp) ({ \ + typeof(funcp) __result; \ + \ + asm("" : "=r" (__result) : "0" (i)); \ + __result; \ + }) +#define FSYM_REBASE(funcp, dest_buf) \ + __uint_to_funcp((uintptr_t)(dest_buf) | FSYM_TYPE(funcp), funcp) + +#ifdef CONFIG_THUMB2_KERNEL +#define FSYM_BASE(funcp) ((void *)(__funcp_to_uint(funcp) & ~(uintptr_t)1)) +#define FSYM_TYPE(funcp) (__funcp_to_uint(funcp) & 1) +#else /* !CONFIG_THUMB2_KERNEL */ +#define FSYM_BASE(funcp) ((void *)__funcp_to_uint(funcp)) +#define FSYM_TYPE(funcp) 0 +#endif /* !CONFIG_THUMB2_KERNEL */ +#endif /* !__ASSEMBLY__ */ + #ifdef CONFIG_THUMB2_KERNEL #if __GNUC__ < 4 -- 1.7.1
next prev parent reply other threads:[~2011-01-12 0:02 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2011-01-12 0:02 [PATCH 0/0] RFC: ARM: Thumb-2: Symbol manipulation macros for function body copying Dave Martin 2011-01-12 0:02 ` Dave Martin 2011-01-12 0:02 ` Dave Martin [this message] 2011-01-12 0:02 ` [PATCH 1/1] " Dave Martin 2011-01-12 9:32 ` [PATCH 0/0] RFC: " Russell King - ARM Linux 2011-01-12 9:32 ` Russell King - ARM Linux 2011-01-12 16:00 ` Dave Martin 2011-01-12 16:00 ` Dave Martin 2011-01-12 16:11 ` Russell King - ARM Linux 2011-01-12 16:11 ` Russell King - ARM Linux 2011-01-12 16:55 ` Dave Martin 2011-01-12 16:55 ` Dave Martin
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=1294790551-17069-2-git-send-email-dave.martin@linaro.org \ --to=dave.martin@linaro.org \ --cc=j-pihet@ti.com \ --cc=linux-arm-kernel@lists.infradead.org \ --cc=linux-omap@vger.kernel.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.