sram-exec.c 3.6 KB

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