asm-generic: fncpy: Add function copying macros

Under certain arches (ARM) function pointers cannot be
used naively. Specifically, for thumb functions, their 0 bit
is set, but they are contained on a word aligned address.

Add a fncpy macro to perform function copies correctly
along with two helpers, fnptr_to_address, and fnptr_translate.

Signed-off-by: Russ Dill <Russ.Dill@ti.com>
This commit is contained in:
Russ Dill 2013-09-06 15:03:56 -07:00 committed by 黄涛
commit 49d083bd72
32 changed files with 154 additions and 57 deletions

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -17,16 +17,12 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __ASM_FNCPY_H
#define __ASM_FNCPY_H
#include <linux/types.h>
/*
* These macros are intended for use when there is a need to copy a low-level
* function body into special memory.
*
* For example, when reconfiguring the SDRAM controller, the code doing the
* reconfiguration may need to run from SRAM.
*
* NOTE: that the copied function body must be entirely self-contained and
* position-independent in order for this to work properly.
*
* NOTE: in order for embedded literals and data to get referenced correctly,
* the alignment of functions must be preserved when copying. To ensure this,
* the source and destination addresses for fncpy() must be aligned to a
@ -34,61 +30,29 @@
* You will typically need a ".align 3" directive in the assembler where the
* function to be copied is defined, and ensure that your allocator for the
* destination buffer returns 8-byte-aligned pointers.
*
* Typical usage example:
*
* extern int f(args);
* extern uint32_t size_of_f;
* int (*copied_f)(args);
* void *sram_buffer;
*
* copied_f = fncpy(sram_buffer, &f, size_of_f);
*
* ... later, call the function: ...
*
* copied_f(args);
*
* The size of the function to be copied can't be determined from C:
* this must be determined by other means, such as adding assmbler directives
* in the file where f is defined.
*/
*/
#define ARCH_FNCPY_ALIGN 3
#ifndef __ASM_FNCPY_H
#define __ASM_FNCPY_H
#include <linux/types.h>
#include <linux/string.h>
#include <asm/bug.h>
#include <asm/cacheflush.h>
/*
* Minimum alignment requirement for the source and destination addresses
* for function copying.
*/
#define FNCPY_ALIGN 8
#define fncpy(dest_buf, funcp, size) ({ \
/* Clear the Thumb bit */
#define fnptr_to_addr(funcp) ({ \
uintptr_t __funcp_address; \
typeof(funcp) __result; \
\
asm("" : "=r" (__funcp_address) : "0" (funcp)); \
__funcp_address & ~1; \
})
/* Put the Thumb bit back */
#define fnptr_translate(orig_funcp, new_addr) ({ \
uintptr_t __funcp_address; \
typeof(orig_funcp) __result; \
\
/* \
* Ensure alignment of source and destination addresses, \
* disregarding the function's Thumb bit: \
*/ \
BUG_ON((uintptr_t)(dest_buf) & (FNCPY_ALIGN - 1) || \
(__funcp_address & ~(uintptr_t)1 & (FNCPY_ALIGN - 1))); \
\
memcpy(dest_buf, (void const *)(__funcp_address & ~1), size); \
flush_icache_range((unsigned long)(dest_buf), \
(unsigned long)(dest_buf) + (size)); \
\
asm("" : "=r" (__funcp_address) : "0" (orig_funcp)); \
asm("" : "=r" (__result) \
: "0" ((uintptr_t)(dest_buf) | (__funcp_address & 1))); \
: "0" ((uintptr_t)(new_addr) | (__funcp_address & 1))); \
\
__result; \
})
#include <asm-generic/fncpy.h>
#endif /* !__ASM_FNCPY_H */

View file

@ -54,7 +54,7 @@ void *omap_sram_push_address(unsigned long size)
}
new_ceil -= size;
new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
new_ceil = ROUND_DOWN(new_ceil, 1 << ARCH_FNCPY_ALIGN);
omap_sram_ceil = IOMEM(new_ceil);
return (void *)omap_sram_ceil;

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

View file

@ -0,0 +1 @@
#include <asm-generic/fncpy.h>

104
include/asm-generic/fncpy.h Normal file
View file

@ -0,0 +1,104 @@
/*
* include/asm-generic/fncpy.h - helper macros for function body copying
*
* Copyright (C) 2011 Linaro Limited
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* These macros are intended for use when there is a need to copy a low-level
* function body into special memory.
*
* For example, when reconfiguring the SDRAM controller, the code doing the
* reconfiguration may need to run from SRAM.
*
* NOTE: that the copied function body must be entirely self-contained and
* position-independent in order for this to work properly.
*
* Typical usage example:
*
* extern int f(args);
* extern uint32_t size_of_f;
* int (*copied_f)(args);
* void *sram_buffer;
*
* copied_f = fncpy(sram_buffer, &f, size_of_f);
*
* ... later, call the function: ...
*
* copied_f(args);
*
* The size of the function to be copied can't be determined from C:
* this must be determined by other means, such as adding assmbler directives
* in the file where f is defined.
*/
#ifndef __ASM_GENERIC_FNCPY_H
#define __ASM_GENERIC_FNCPY_H
#include <linux/types.h>
#include <linux/string.h>
#include <asm/bug.h>
#include <asm/cacheflush.h>
/*
* Minimum alignment requirement for the source and destination addresses
* for function copying.
*/
#ifndef ARCH_FNCPY_ALIGN
#define ARCH_FNCPY_ALIGN 0
#endif
#define ARCH_FNCPY_MASK ((1 << (ARCH_FNCPY_ALIGN)) - 1)
#ifndef fnptr_to_addr
#define fnptr_to_addr(funcp) ({ \
(uintptr_t) (funcp); \
})
#endif
#ifndef fnptr_translate
#define fnptr_translate(orig_funcp, new_addr) ({ \
(typeof(orig_funcp)) (new_addr); \
})
#endif
/* Ensure alignment of source and destination addresses */
#ifndef fn_dest_invalid
#define fn_dest_invalid(funcp, dest_buf) ({ \
uintptr_t __funcp_address; \
\
__funcp_address = fnptr_to_addr(funcp); \
\
((uintptr_t)(dest_buf) & ARCH_FNCPY_MASK) || \
(__funcp_address & ARCH_FNCPY_MASK); \
})
#endif
#ifndef fncpy
#define fncpy(dest_buf, funcp, size) ({ \
BUG_ON(fn_dest_invalid(funcp, dest_buf)); \
\
memcpy(dest_buf, (void const *)(funcp), size); \
flush_icache_range((unsigned long)(dest_buf), \
(unsigned long)(dest_buf) + (size)); \
\
fnptr_translate(funcp, dest_buf); \
})
#endif
#endif /* !__ASM_GENERIC_FNCPY_H */