sram-exec.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SRAM protect-exec region helper functions
  4. *
  5. * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
  6. * Dave Gerlach
  7. */
  8. #include <linux/device.h>
  9. #include <linux/genalloc.h>
  10. #include <linux/mm.h>
  11. #include <linux/sram.h>
  12. #include <asm/fncpy.h>
  13. #include <asm/set_memory.h>
  14. #include "sram.h"
  15. static DEFINE_MUTEX(exec_pool_list_mutex);
  16. static LIST_HEAD(exec_pool_list);
  17. int sram_check_protect_exec(struct sram_dev *sram, struct sram_reserve *block,
  18. struct sram_partition *part)
  19. {
  20. unsigned long base = (unsigned long)part->base;
  21. unsigned long end = base + block->size;
  22. if (!PAGE_ALIGNED(base) || !PAGE_ALIGNED(end)) {
  23. dev_err(sram->dev,
  24. "SRAM pool marked with 'protect-exec' is not page aligned and will not be created.\n");
  25. return -ENOMEM;
  26. }
  27. return 0;
  28. }
  29. int sram_add_protect_exec(struct sram_partition *part)
  30. {
  31. mutex_lock(&exec_pool_list_mutex);
  32. list_add_tail(&part->list, &exec_pool_list);
  33. mutex_unlock(&exec_pool_list_mutex);
  34. return 0;
  35. }
  36. /**
  37. * sram_exec_copy - copy data to a protected executable region of sram
  38. *
  39. * @pool: struct gen_pool retrieved that is part of this sram
  40. * @dst: Destination address for the copy, that must be inside pool
  41. * @src: Source address for the data to copy
  42. * @size: Size of copy to perform, which starting from dst, must reside in pool
  43. *
  44. * Return: Address for copied data that can safely be called through function
  45. * pointer, or NULL if problem.
  46. *
  47. * This helper function allows sram driver to act as central control location
  48. * of 'protect-exec' pools which are normal sram pools but are always set
  49. * read-only and executable except when copying data to them, at which point
  50. * they are set to read-write non-executable, to make sure no memory is
  51. * writeable and executable at the same time. This region must be page-aligned
  52. * and is checked during probe, otherwise page attribute manipulation would
  53. * not be possible. Care must be taken to only call the returned address as
  54. * dst address is not guaranteed to be safely callable.
  55. *
  56. * NOTE: This function uses the fncpy macro to move code to the executable
  57. * region. Some architectures have strict requirements for relocating
  58. * executable code, so fncpy is a macro that must be defined by any arch
  59. * making use of this functionality that guarantees a safe copy of exec
  60. * data and returns a safe address that can be called as a C function
  61. * pointer.
  62. */
  63. void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
  64. size_t size)
  65. {
  66. struct sram_partition *part = NULL, *p;
  67. unsigned long base;
  68. int pages;
  69. void *dst_cpy;
  70. int ret;
  71. mutex_lock(&exec_pool_list_mutex);
  72. list_for_each_entry(p, &exec_pool_list, list) {
  73. if (p->pool == pool)
  74. part = p;
  75. }
  76. mutex_unlock(&exec_pool_list_mutex);
  77. if (!part)
  78. return NULL;
  79. if (!gen_pool_has_addr(pool, (unsigned long)dst, size))
  80. return NULL;
  81. base = (unsigned long)part->base;
  82. pages = PAGE_ALIGN(size) / PAGE_SIZE;
  83. mutex_lock(&part->lock);
  84. ret = set_memory_nx((unsigned long)base, pages);
  85. if (ret)
  86. goto error_out;
  87. ret = set_memory_rw((unsigned long)base, pages);
  88. if (ret)
  89. goto error_out;
  90. dst_cpy = fncpy(dst, src, size);
  91. ret = set_memory_ro((unsigned long)base, pages);
  92. if (ret)
  93. goto error_out;
  94. ret = set_memory_x((unsigned long)base, pages);
  95. if (ret)
  96. goto error_out;
  97. mutex_unlock(&part->lock);
  98. return dst_cpy;
  99. error_out:
  100. mutex_unlock(&part->lock);
  101. return NULL;
  102. }
  103. EXPORT_SYMBOL_GPL(sram_exec_copy);