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