asm-generic: fncpy: Add function copying macros
authorRuss Dill <Russ.Dill@ti.com>
Fri, 6 Sep 2013 22:03:56 +0000 (15:03 -0700)
committer黄涛 <huangtao@rock-chips.com>
Thu, 21 Nov 2013 05:39:20 +0000 (13:39 +0800)
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>
32 files changed:
arch/alpha/include/asm/fncpy.h [new file with mode: 0644]
arch/arc/include/asm/fncpy.h [new file with mode: 0644]
arch/arm/include/asm/fncpy.h
arch/arm/plat-omap/sram.c
arch/arm64/include/asm/fncpy.h [new file with mode: 0644]
arch/avr32/include/asm/fncpy.h [new file with mode: 0644]
arch/blackfin/include/asm/fncpy.h [new file with mode: 0644]
arch/c6x/include/asm/fncpy.h [new file with mode: 0644]
arch/cris/include/asm/fncpy.h [new file with mode: 0644]
arch/frv/include/asm/fncpy.h [new file with mode: 0644]
arch/h8300/include/asm/fncpy.h [new file with mode: 0644]
arch/hexagon/include/asm/fncpy.h [new file with mode: 0644]
arch/ia64/include/asm/fncpy.h [new file with mode: 0644]
arch/m32r/include/asm/fncpy.h [new file with mode: 0644]
arch/m68k/include/asm/fncpy.h [new file with mode: 0644]
arch/metag/include/asm/fncpy.h [new file with mode: 0644]
arch/microblaze/include/asm/fncpy.h [new file with mode: 0644]
arch/mips/include/asm/fncpy.h [new file with mode: 0644]
arch/mn10300/include/asm/fncpy.h [new file with mode: 0644]
arch/openrisc/include/asm/fncpy.h [new file with mode: 0644]
arch/parisc/include/asm/fncpy.h [new file with mode: 0644]
arch/powerpc/include/asm/fncpy.h [new file with mode: 0644]
arch/s390/include/asm/fncpy.h [new file with mode: 0644]
arch/score/include/asm/fncpy.h [new file with mode: 0644]
arch/sh/include/asm/fncpy.h [new file with mode: 0644]
arch/sparc/include/asm/fncpy.h [new file with mode: 0644]
arch/tile/include/asm/fncpy.h [new file with mode: 0644]
arch/um/include/asm/fncpy.h [new file with mode: 0644]
arch/unicore32/include/asm/fncpy.h [new file with mode: 0644]
arch/x86/include/asm/fncpy.h [new file with mode: 0644]
arch/xtensa/include/asm/fncpy.h [new file with mode: 0644]
include/asm-generic/fncpy.h [new file with mode: 0644]

diff --git a/arch/alpha/include/asm/fncpy.h b/arch/alpha/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/arc/include/asm/fncpy.h b/arch/arc/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
index de53547469246f063b5eaadc12ebc636041f2c28..f165f20c3e3bc505fedac141faf6a602e5b6f110 100644 (file)
  * 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
  * 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 */
index a5bc92d7e4765b81315379c41618fff7f84cbec2..90ccd7463182570cf8f980a295ee75fd2b560703 100644 (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;
diff --git a/arch/arm64/include/asm/fncpy.h b/arch/arm64/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/avr32/include/asm/fncpy.h b/arch/avr32/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/blackfin/include/asm/fncpy.h b/arch/blackfin/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/c6x/include/asm/fncpy.h b/arch/c6x/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/cris/include/asm/fncpy.h b/arch/cris/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/frv/include/asm/fncpy.h b/arch/frv/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/h8300/include/asm/fncpy.h b/arch/h8300/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/hexagon/include/asm/fncpy.h b/arch/hexagon/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/ia64/include/asm/fncpy.h b/arch/ia64/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/m32r/include/asm/fncpy.h b/arch/m32r/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/m68k/include/asm/fncpy.h b/arch/m68k/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/metag/include/asm/fncpy.h b/arch/metag/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/microblaze/include/asm/fncpy.h b/arch/microblaze/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/mips/include/asm/fncpy.h b/arch/mips/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/mn10300/include/asm/fncpy.h b/arch/mn10300/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/openrisc/include/asm/fncpy.h b/arch/openrisc/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/parisc/include/asm/fncpy.h b/arch/parisc/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/powerpc/include/asm/fncpy.h b/arch/powerpc/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/s390/include/asm/fncpy.h b/arch/s390/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/score/include/asm/fncpy.h b/arch/score/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/sh/include/asm/fncpy.h b/arch/sh/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/sparc/include/asm/fncpy.h b/arch/sparc/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/tile/include/asm/fncpy.h b/arch/tile/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/um/include/asm/fncpy.h b/arch/um/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/unicore32/include/asm/fncpy.h b/arch/unicore32/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/x86/include/asm/fncpy.h b/arch/x86/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/arch/xtensa/include/asm/fncpy.h b/arch/xtensa/include/asm/fncpy.h
new file mode 100644 (file)
index 0000000..ee4741c
--- /dev/null
@@ -0,0 +1 @@
+#include <asm-generic/fncpy.h>
diff --git a/include/asm-generic/fncpy.h b/include/asm-generic/fncpy.h
new file mode 100644 (file)
index 0000000..1a25282
--- /dev/null
@@ -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 */
+