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:
parent
6e701bfc17
commit
49d083bd72
32 changed files with 154 additions and 57 deletions
1
arch/alpha/include/asm/fncpy.h
Normal file
1
arch/alpha/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/arc/include/asm/fncpy.h
Normal file
1
arch/arc/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
1
arch/arm64/include/asm/fncpy.h
Normal file
1
arch/arm64/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/avr32/include/asm/fncpy.h
Normal file
1
arch/avr32/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/blackfin/include/asm/fncpy.h
Normal file
1
arch/blackfin/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/c6x/include/asm/fncpy.h
Normal file
1
arch/c6x/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/cris/include/asm/fncpy.h
Normal file
1
arch/cris/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/frv/include/asm/fncpy.h
Normal file
1
arch/frv/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/h8300/include/asm/fncpy.h
Normal file
1
arch/h8300/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/hexagon/include/asm/fncpy.h
Normal file
1
arch/hexagon/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/ia64/include/asm/fncpy.h
Normal file
1
arch/ia64/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/m32r/include/asm/fncpy.h
Normal file
1
arch/m32r/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/m68k/include/asm/fncpy.h
Normal file
1
arch/m68k/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/metag/include/asm/fncpy.h
Normal file
1
arch/metag/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/microblaze/include/asm/fncpy.h
Normal file
1
arch/microblaze/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/mips/include/asm/fncpy.h
Normal file
1
arch/mips/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/mn10300/include/asm/fncpy.h
Normal file
1
arch/mn10300/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/openrisc/include/asm/fncpy.h
Normal file
1
arch/openrisc/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/parisc/include/asm/fncpy.h
Normal file
1
arch/parisc/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/powerpc/include/asm/fncpy.h
Normal file
1
arch/powerpc/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/s390/include/asm/fncpy.h
Normal file
1
arch/s390/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/score/include/asm/fncpy.h
Normal file
1
arch/score/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/sh/include/asm/fncpy.h
Normal file
1
arch/sh/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/sparc/include/asm/fncpy.h
Normal file
1
arch/sparc/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/tile/include/asm/fncpy.h
Normal file
1
arch/tile/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/um/include/asm/fncpy.h
Normal file
1
arch/um/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/unicore32/include/asm/fncpy.h
Normal file
1
arch/unicore32/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/x86/include/asm/fncpy.h
Normal file
1
arch/x86/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
1
arch/xtensa/include/asm/fncpy.h
Normal file
1
arch/xtensa/include/asm/fncpy.h
Normal file
|
|
@ -0,0 +1 @@
|
|||
#include <asm-generic/fncpy.h>
|
||||
104
include/asm-generic/fncpy.h
Normal file
104
include/asm-generic/fncpy.h
Normal 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 */
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue