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