Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/anholt...
[pandora-kernel.git] / arch / powerpc / oprofile / cell / vma_map.c
1 /*
2  * Cell Broadband Engine OProfile Support
3  *
4  * (C) Copyright IBM Corporation 2006
5  *
6  * Author: Maynard Johnson <maynardj@us.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  */
13
14 /* The code in this source file is responsible for generating
15  * vma-to-fileOffset maps for both overlay and non-overlay SPU
16  * applications.
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
27 void 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
36 unsigned int
37 vma_map_lookup(struct vma_to_fileoffset_map *map, unsigned int vma,
38                const struct spu *aSpu, int *grd_val)
39 {
40         /*
41          * Default the offset to the physical address + a flag value.
42          * Addresses of dynamically generated code can't be found in the vma
43          * map.  For those addresses the flagged value will be sent on to
44          * the user space tools so they can be reported rather than just
45          * thrown away.
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
67 static struct vma_to_fileoffset_map *
68 vma_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 /* Parse SPE ELF header and generate a list of vma_maps.
93  * A pointer to the first vma_map in the generated list
94  * of vma_maps is returned.  */
95 struct 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         /* Get and validate ELF header.  */
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         /* Traverse program headers.  */
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         /* Traverse section table and search for overlay-related symbols.  */
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         /* If we don't have overlays, we're done.  */
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         /* The _ovly_table symbol represents a table with one entry
225          * per overlay section.  The _ovly_buf_table symbol represents
226          * a table with one entry per overlay region.
227          * The struct spu_overlay_info gives the structure of the _ovly_table
228          * entries.  The structure of _ovly_table_buf is simply one
229          * u32 word per entry.
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         /* Traverse overlay table.  */
245         for (i = 0; i < n_ovlys; i++) {
246                 if (copy_from_user(&ovly, ovly_table + i, sizeof (ovly)))
247                         goto fail;
248
249                 /* The ovly.vma/size/offset arguments are analogous to the same
250                  * arguments used above for non-overlay maps.  The final two
251                  * args are referred to as the guard pointer and the guard
252                  * value.
253                  * The guard pointer is an entry in the _ovly_buf_table,
254                  * computed using ovly.buf as the index into the table.  Since
255                  * ovly.buf values begin at '1' to reference the first (or 0th)
256                  * entry in the _ovly_buf_table, the computation subtracts 1
257                  * from ovly.buf.
258                  * The guard value is stored in the _ovly_buf_table entry and
259                  * is an index (starting at 1) back to the _ovly_table entry
260                  * that is pointing at this _ovly_buf_table entry.  So, for
261                  * example, for an overlay scenario with one overlay segment
262                  * and two overlay sections:
263                  *      - Section 1 points to the first entry of the
264                  *        _ovly_buf_table, which contains a guard value
265                  *        of '1', referencing the first (index=0) entry of
266                  *        _ovly_table.
267                  *      - Section 2 points to the second entry of the
268                  *        _ovly_buf_table, which contains a guard value
269                  *        of '2', referencing the second (index=1) entry of
270                  *        _ovly_table.
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 }