fncpy.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * arch/arm/include/asm/fncpy.h - helper macros for function body copying
  4. *
  5. * Copyright (C) 2011 Linaro Limited
  6. */
  7. /*
  8. * These macros are intended for use when there is a need to copy a low-level
  9. * function body into special memory.
  10. *
  11. * For example, when reconfiguring the SDRAM controller, the code doing the
  12. * reconfiguration may need to run from SRAM.
  13. *
  14. * NOTE: that the copied function body must be entirely self-contained and
  15. * position-independent in order for this to work properly.
  16. *
  17. * NOTE: in order for embedded literals and data to get referenced correctly,
  18. * the alignment of functions must be preserved when copying. To ensure this,
  19. * the source and destination addresses for fncpy() must be aligned to a
  20. * multiple of 8 bytes: you will be get a BUG() if this condition is not met.
  21. * You will typically need a ".align 3" directive in the assembler where the
  22. * function to be copied is defined, and ensure that your allocator for the
  23. * destination buffer returns 8-byte-aligned pointers.
  24. *
  25. * Typical usage example:
  26. *
  27. * extern int f(args);
  28. * extern uint32_t size_of_f;
  29. * int (*copied_f)(args);
  30. * void *sram_buffer;
  31. *
  32. * copied_f = fncpy(sram_buffer, &f, size_of_f);
  33. *
  34. * ... later, call the function: ...
  35. *
  36. * copied_f(args);
  37. *
  38. * The size of the function to be copied can't be determined from C:
  39. * this must be determined by other means, such as adding assmbler directives
  40. * in the file where f is defined.
  41. */
  42. #ifndef __ASM_FNCPY_H
  43. #define __ASM_FNCPY_H
  44. #include <linux/types.h>
  45. #include <linux/string.h>
  46. #include <asm/bug.h>
  47. #include <asm/cacheflush.h>
  48. /*
  49. * Minimum alignment requirement for the source and destination addresses
  50. * for function copying.
  51. */
  52. #define FNCPY_ALIGN 8
  53. #define fncpy(dest_buf, funcp, size) ({ \
  54. uintptr_t __funcp_address; \
  55. typeof(funcp) __result; \
  56. \
  57. asm("" : "=r" (__funcp_address) : "0" (funcp)); \
  58. \
  59. /* \
  60. * Ensure alignment of source and destination addresses, \
  61. * disregarding the function's Thumb bit: \
  62. */ \
  63. BUG_ON((uintptr_t)(dest_buf) & (FNCPY_ALIGN - 1) || \
  64. (__funcp_address & ~(uintptr_t)1 & (FNCPY_ALIGN - 1))); \
  65. \
  66. memcpy(dest_buf, (void const *)(__funcp_address & ~1), size); \
  67. flush_icache_range((unsigned long)(dest_buf), \
  68. (unsigned long)(dest_buf) + (size)); \
  69. \
  70. asm("" : "=r" (__result) \
  71. : "0" ((uintptr_t)(dest_buf) | (__funcp_address & 1))); \
  72. \
  73. __result; \
  74. })
  75. #endif /* !__ASM_FNCPY_H */