This source file includes following definitions.
- vma_map_free
- vma_map_lookup
- vma_map_add
- create_vma_map
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include <linux/mm.h>
16 #include <linux/string.h>
17 #include <linux/uaccess.h>
18 #include <linux/elf.h>
19 #include <linux/slab.h>
20 #include "pr_util.h"
21
22
23 void vma_map_free(struct vma_to_fileoffset_map *map)
24 {
25 while (map) {
26 struct vma_to_fileoffset_map *next = map->next;
27 kfree(map);
28 map = next;
29 }
30 }
31
32 unsigned int
33 vma_map_lookup(struct vma_to_fileoffset_map *map, unsigned int vma,
34 const struct spu *aSpu, int *grd_val)
35 {
36
37
38
39
40
41
42
43 u32 offset = 0x10000000 + vma;
44 u32 ovly_grd;
45
46 for (; map; map = map->next) {
47 if (vma < map->vma || vma >= map->vma + map->size)
48 continue;
49
50 if (map->guard_ptr) {
51 ovly_grd = *(u32 *)(aSpu->local_store + map->guard_ptr);
52 if (ovly_grd != map->guard_val)
53 continue;
54 *grd_val = ovly_grd;
55 }
56 offset = vma - map->vma + map->offset;
57 break;
58 }
59
60 return offset;
61 }
62
63 static struct vma_to_fileoffset_map *
64 vma_map_add(struct vma_to_fileoffset_map *map, unsigned int vma,
65 unsigned int size, unsigned int offset, unsigned int guard_ptr,
66 unsigned int guard_val)
67 {
68 struct vma_to_fileoffset_map *new = kzalloc(sizeof(*new), GFP_KERNEL);
69
70 if (!new) {
71 printk(KERN_ERR "SPU_PROF: %s, line %d: malloc failed\n",
72 __func__, __LINE__);
73 vma_map_free(map);
74 return NULL;
75 }
76
77 new->next = map;
78 new->vma = vma;
79 new->size = size;
80 new->offset = offset;
81 new->guard_ptr = guard_ptr;
82 new->guard_val = guard_val;
83
84 return new;
85 }
86
87
88
89
90
91 struct vma_to_fileoffset_map *create_vma_map(const struct spu *aSpu,
92 unsigned long __spu_elf_start)
93 {
94 static const unsigned char expected[EI_PAD] = {
95 [EI_MAG0] = ELFMAG0,
96 [EI_MAG1] = ELFMAG1,
97 [EI_MAG2] = ELFMAG2,
98 [EI_MAG3] = ELFMAG3,
99 [EI_CLASS] = ELFCLASS32,
100 [EI_DATA] = ELFDATA2MSB,
101 [EI_VERSION] = EV_CURRENT,
102 [EI_OSABI] = ELFOSABI_NONE
103 };
104
105 int grd_val;
106 struct vma_to_fileoffset_map *map = NULL;
107 void __user *spu_elf_start = (void __user *)__spu_elf_start;
108 struct spu_overlay_info ovly;
109 unsigned int overlay_tbl_offset = -1;
110 Elf32_Phdr __user *phdr_start;
111 Elf32_Shdr __user *shdr_start;
112 Elf32_Ehdr ehdr;
113 Elf32_Phdr phdr;
114 Elf32_Shdr shdr, shdr_str;
115 Elf32_Sym sym;
116 int i, j;
117 char name[32];
118
119 unsigned int ovly_table_sym = 0;
120 unsigned int ovly_buf_table_sym = 0;
121 unsigned int ovly_table_end_sym = 0;
122 unsigned int ovly_buf_table_end_sym = 0;
123 struct spu_overlay_info __user *ovly_table;
124 unsigned int n_ovlys;
125
126
127
128 if (copy_from_user(&ehdr, spu_elf_start, sizeof (ehdr)))
129 goto fail;
130
131 if (memcmp(ehdr.e_ident, expected, EI_PAD) != 0) {
132 printk(KERN_ERR "SPU_PROF: "
133 "%s, line %d: Unexpected e_ident parsing SPU ELF\n",
134 __func__, __LINE__);
135 goto fail;
136 }
137 if (ehdr.e_machine != EM_SPU) {
138 printk(KERN_ERR "SPU_PROF: "
139 "%s, line %d: Unexpected e_machine parsing SPU ELF\n",
140 __func__, __LINE__);
141 goto fail;
142 }
143 if (ehdr.e_type != ET_EXEC) {
144 printk(KERN_ERR "SPU_PROF: "
145 "%s, line %d: Unexpected e_type parsing SPU ELF\n",
146 __func__, __LINE__);
147 goto fail;
148 }
149 phdr_start = spu_elf_start + ehdr.e_phoff;
150 shdr_start = spu_elf_start + ehdr.e_shoff;
151
152
153 for (i = 0; i < ehdr.e_phnum; i++) {
154 if (copy_from_user(&phdr, phdr_start + i, sizeof(phdr)))
155 goto fail;
156
157 if (phdr.p_type != PT_LOAD)
158 continue;
159 if (phdr.p_flags & (1 << 27))
160 continue;
161
162 map = vma_map_add(map, phdr.p_vaddr, phdr.p_memsz,
163 phdr.p_offset, 0, 0);
164 if (!map)
165 goto fail;
166 }
167
168 pr_debug("SPU_PROF: Created non-overlay maps\n");
169
170 for (i = 0; i < ehdr.e_shnum; i++) {
171 if (copy_from_user(&shdr, shdr_start + i, sizeof(shdr)))
172 goto fail;
173
174 if (shdr.sh_type != SHT_SYMTAB)
175 continue;
176 if (shdr.sh_entsize != sizeof (sym))
177 continue;
178
179 if (copy_from_user(&shdr_str,
180 shdr_start + shdr.sh_link,
181 sizeof(shdr)))
182 goto fail;
183
184 if (shdr_str.sh_type != SHT_STRTAB)
185 goto fail;
186
187 for (j = 0; j < shdr.sh_size / sizeof (sym); j++) {
188 if (copy_from_user(&sym, spu_elf_start +
189 shdr.sh_offset +
190 j * sizeof (sym),
191 sizeof (sym)))
192 goto fail;
193
194 if (copy_from_user(name,
195 spu_elf_start + shdr_str.sh_offset +
196 sym.st_name,
197 20))
198 goto fail;
199
200 if (memcmp(name, "_ovly_table", 12) == 0)
201 ovly_table_sym = sym.st_value;
202 if (memcmp(name, "_ovly_buf_table", 16) == 0)
203 ovly_buf_table_sym = sym.st_value;
204 if (memcmp(name, "_ovly_table_end", 16) == 0)
205 ovly_table_end_sym = sym.st_value;
206 if (memcmp(name, "_ovly_buf_table_end", 20) == 0)
207 ovly_buf_table_end_sym = sym.st_value;
208 }
209 }
210
211
212 if (ovly_table_sym == 0 || ovly_buf_table_sym == 0
213 || ovly_table_end_sym == 0 || ovly_buf_table_end_sym == 0) {
214 pr_debug("SPU_PROF: No overlay table found\n");
215 goto out;
216 } else {
217 pr_debug("SPU_PROF: Overlay table found\n");
218 }
219
220
221
222
223
224
225
226
227 overlay_tbl_offset = vma_map_lookup(map, ovly_table_sym,
228 aSpu, &grd_val);
229 if (overlay_tbl_offset > 0x10000000) {
230 printk(KERN_ERR "SPU_PROF: "
231 "%s, line %d: Error finding SPU overlay table\n",
232 __func__, __LINE__);
233 goto fail;
234 }
235 ovly_table = spu_elf_start + overlay_tbl_offset;
236
237 n_ovlys = (ovly_table_end_sym -
238 ovly_table_sym) / sizeof (ovly);
239
240
241 for (i = 0; i < n_ovlys; i++) {
242 if (copy_from_user(&ovly, ovly_table + i, sizeof (ovly)))
243 goto fail;
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 map = vma_map_add(map, ovly.vma, ovly.size, ovly.offset,
269 ovly_buf_table_sym + (ovly.buf-1) * 4, i+1);
270 if (!map)
271 goto fail;
272 }
273 goto out;
274
275 fail:
276 map = NULL;
277 out:
278 return map;
279 }