1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/device.h>
18#include <linux/genalloc.h>
19#include <linux/mm.h>
20#include <linux/sram.h>
21
22#include <asm/fncpy.h>
23#include <asm/set_memory.h>
24
25#include "sram.h"
26
27static DEFINE_MUTEX(exec_pool_list_mutex);
28static LIST_HEAD(exec_pool_list);
29
30int sram_check_protect_exec(struct sram_dev *sram, struct sram_reserve *block,
31 struct sram_partition *part)
32{
33 unsigned long base = (unsigned long)part->base;
34 unsigned long end = base + block->size;
35
36 if (!PAGE_ALIGNED(base) || !PAGE_ALIGNED(end)) {
37 dev_err(sram->dev,
38 "SRAM pool marked with 'protect-exec' is not page aligned and will not be created.\n");
39 return -ENOMEM;
40 }
41
42 return 0;
43}
44
45int sram_add_protect_exec(struct sram_partition *part)
46{
47 mutex_lock(&exec_pool_list_mutex);
48 list_add_tail(&part->list, &exec_pool_list);
49 mutex_unlock(&exec_pool_list_mutex);
50
51 return 0;
52}
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
82 size_t size)
83{
84 struct sram_partition *part = NULL, *p;
85 unsigned long base;
86 int pages;
87 void *dst_cpy;
88 int ret;
89
90 mutex_lock(&exec_pool_list_mutex);
91 list_for_each_entry(p, &exec_pool_list, list) {
92 if (p->pool == pool)
93 part = p;
94 }
95 mutex_unlock(&exec_pool_list_mutex);
96
97 if (!part)
98 return NULL;
99
100 if (!gen_pool_has_addr(pool, (unsigned long)dst, size))
101 return NULL;
102
103 base = (unsigned long)part->base;
104 pages = PAGE_ALIGN(size) / PAGE_SIZE;
105
106 mutex_lock(&part->lock);
107
108 ret = set_memory_nx((unsigned long)base, pages);
109 if (ret)
110 goto error_out;
111 ret = set_memory_rw((unsigned long)base, pages);
112 if (ret)
113 goto error_out;
114
115 dst_cpy = fncpy(dst, src, size);
116
117 ret = set_memory_ro((unsigned long)base, pages);
118 if (ret)
119 goto error_out;
120 ret = set_memory_x((unsigned long)base, pages);
121 if (ret)
122 goto error_out;
123
124 mutex_unlock(&part->lock);
125
126 return dst_cpy;
127
128error_out:
129 mutex_unlock(&part->lock);
130 return NULL;
131}
132EXPORT_SYMBOL_GPL(sram_exec_copy);
133