fncpy.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * arch/arm/include/asm/fncpy.h - helper macros for function body copying
  3. *
  4. * Copyright (C) 2011 Linaro Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * These macros are intended for use when there is a need to copy a low-level
  21. * function body into special memory.
  22. *
  23. * For example, when reconfiguring the SDRAM controller, the code doing the
  24. * reconfiguration may need to run from SRAM.
  25. *
  26. * NOTE: that the copied function body must be entirely self-contained and
  27. * position-independent in order for this to work properly.
  28. *
  29. * NOTE: in order for embedded literals and data to get referenced correctly,
  30. * the alignment of functions must be preserved when copying. To ensure this,
  31. * the source and destination addresses for fncpy() must be aligned to a
  32. * multiple of 8 bytes: you will be get a BUG() if this condition is not met.
  33. * You will typically need a ".align 3" directive in the assembler where the
  34. * function to be copied is defined, and ensure that your allocator for the
  35. * destination buffer returns 8-byte-aligned pointers.
  36. *
  37. * Typical usage example:
  38. *
  39. * extern int f(args);
  40. * extern uint32_t size_of_f;
  41. * int (*copied_f)(args);
  42. * void *sram_buffer;
  43. *
  44. * copied_f = fncpy(sram_buffer, &f, size_of_f);
  45. *
  46. * ... later, call the function: ...
  47. *
  48. * copied_f(args);
  49. *
  50. * The size of the function to be copied can't be determined from C:
  51. * this must be determined by other means, such as adding assmbler directives
  52. * in the file where f is defined.
  53. */
  54. #ifndef __ASM_FNCPY_H
  55. #define __ASM_FNCPY_H
  56. #include <linux/types.h>
  57. #include <linux/string.h>
  58. #include <asm/bug.h>
  59. #include <asm/cacheflush.h>
  60. /*
  61. * Minimum alignment requirement for the source and destination addresses
  62. * for function copying.
  63. */
  64. #define FNCPY_ALIGN 8
  65. #define fncpy(dest_buf, funcp, size) ({ \
  66. uintptr_t __funcp_address; \
  67. typeof(funcp) __result; \
  68. \
  69. asm("" : "=r" (__funcp_address) : "0" (funcp)); \
  70. \
  71. /* \
  72. * Ensure alignment of source and destination addresses, \
  73. * disregarding the function's Thumb bit: \
  74. */ \
  75. BUG_ON((uintptr_t)(dest_buf) & (FNCPY_ALIGN - 1) || \
  76. (__funcp_address & ~(uintptr_t)1 & (FNCPY_ALIGN - 1))); \
  77. \
  78. memcpy(dest_buf, (void const *)(__funcp_address & ~1), size); \
  79. flush_icache_range((unsigned long)(dest_buf), \
  80. (unsigned long)(dest_buf) + (size)); \
  81. \
  82. asm("" : "=r" (__result) \
  83. : "0" ((uintptr_t)(dest_buf) | (__funcp_address & 1))); \
  84. \
  85. __result; \
  86. })
  87. #endif /* !__ASM_FNCPY_H */