2 * Cell Broadband Engine OProfile Support
4 * (C) Copyright IBM Corporation 2006
6 * Author: Maynard Johnson <maynardj@us.ibm.com>
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.
14 /* The code in this source file is responsible for generating
15 * vma-to-fileOffset maps for both overlay and non-overlay SPU
20 #include <linux/string.h>
21 #include <linux/uaccess.h>
22 #include <linux/elf.h>
26 void vma_map_free(struct vma_to_fileoffset_map *map)
29 struct vma_to_fileoffset_map *next = map->next;
36 vma_map_lookup(struct vma_to_fileoffset_map *map, unsigned int vma,
37 const struct spu *aSpu, int *grd_val)
40 * Default the offset to the physical address + a flag value.
41 * Addresses of dynamically generated code can't be found in the vma
42 * map. For those addresses the flagged value will be sent on to
43 * the user space tools so they can be reported rather than just
46 u32 offset = 0x10000000 + vma;
49 for (; map; map = map->next) {
50 if (vma < map->vma || vma >= map->vma + map->size)
54 ovly_grd = *(u32 *)(aSpu->local_store + map->guard_ptr);
55 if (ovly_grd != map->guard_val)
59 offset = vma - map->vma + map->offset;
66 static struct vma_to_fileoffset_map *
67 vma_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)
71 struct vma_to_fileoffset_map *new =
72 kzalloc(sizeof(struct vma_to_fileoffset_map), GFP_KERNEL);
74 printk(KERN_ERR "SPU_PROF: %s, line %d: malloc failed\n",
84 new->guard_ptr = guard_ptr;
85 new->guard_val = guard_val;
91 /* Parse SPE ELF header and generate a list of vma_maps.
92 * A pointer to the first vma_map in the generated list
93 * of vma_maps is returned. */
94 struct vma_to_fileoffset_map *create_vma_map(const struct spu *aSpu,
95 unsigned long __spu_elf_start)
97 static const unsigned char expected[EI_PAD] = {
102 [EI_CLASS] = ELFCLASS32,
103 [EI_DATA] = ELFDATA2MSB,
104 [EI_VERSION] = EV_CURRENT,
105 [EI_OSABI] = ELFOSABI_NONE
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;
117 Elf32_Shdr shdr, shdr_str;
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;
129 /* Get and validate ELF header. */
131 if (copy_from_user(&ehdr, spu_elf_start, sizeof (ehdr)))
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",
140 if (ehdr.e_machine != EM_SPU) {
141 printk(KERN_ERR "SPU_PROF: "
142 "%s, line %d: Unexpected e_machine parsing SPU ELF\n",
146 if (ehdr.e_type != ET_EXEC) {
147 printk(KERN_ERR "SPU_PROF: "
148 "%s, line %d: Unexpected e_type parsing SPU ELF\n",
152 phdr_start = spu_elf_start + ehdr.e_phoff;
153 shdr_start = spu_elf_start + ehdr.e_shoff;
155 /* Traverse program headers. */
156 for (i = 0; i < ehdr.e_phnum; i++) {
157 if (copy_from_user(&phdr, phdr_start + i, sizeof(phdr)))
160 if (phdr.p_type != PT_LOAD)
162 if (phdr.p_flags & (1 << 27))
165 map = vma_map_add(map, phdr.p_vaddr, phdr.p_memsz,
166 phdr.p_offset, 0, 0);
171 pr_debug("SPU_PROF: Created non-overlay maps\n");
172 /* Traverse section table and search for overlay-related symbols. */
173 for (i = 0; i < ehdr.e_shnum; i++) {
174 if (copy_from_user(&shdr, shdr_start + i, sizeof(shdr)))
177 if (shdr.sh_type != SHT_SYMTAB)
179 if (shdr.sh_entsize != sizeof (sym))
182 if (copy_from_user(&shdr_str,
183 shdr_start + shdr.sh_link,
187 if (shdr_str.sh_type != SHT_STRTAB)
190 for (j = 0; j < shdr.sh_size / sizeof (sym); j++) {
191 if (copy_from_user(&sym, spu_elf_start +
197 if (copy_from_user(name,
198 spu_elf_start + shdr_str.sh_offset +
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;
214 /* If we don't have overlays, we're done. */
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");
220 pr_debug("SPU_PROF: Overlay table found\n");
223 /* The _ovly_table symbol represents a table with one entry
224 * per overlay section. The _ovly_buf_table symbol represents
225 * a table with one entry per overlay region.
226 * The struct spu_overlay_info gives the structure of the _ovly_table
227 * entries. The structure of _ovly_table_buf is simply one
228 * u32 word per entry.
230 overlay_tbl_offset = vma_map_lookup(map, ovly_table_sym,
232 if (overlay_tbl_offset > 0x10000000) {
233 printk(KERN_ERR "SPU_PROF: "
234 "%s, line %d: Error finding SPU overlay table\n",
238 ovly_table = spu_elf_start + overlay_tbl_offset;
240 n_ovlys = (ovly_table_end_sym -
241 ovly_table_sym) / sizeof (ovly);
243 /* Traverse overlay table. */
244 for (i = 0; i < n_ovlys; i++) {
245 if (copy_from_user(&ovly, ovly_table + i, sizeof (ovly)))
248 /* The ovly.vma/size/offset arguments are analogous to the same
249 * arguments used above for non-overlay maps. The final two
250 * args are referred to as the guard pointer and the guard
252 * The guard pointer is an entry in the _ovly_buf_table,
253 * computed using ovly.buf as the index into the table. Since
254 * ovly.buf values begin at '1' to reference the first (or 0th)
255 * entry in the _ovly_buf_table, the computation subtracts 1
257 * The guard value is stored in the _ovly_buf_table entry and
258 * is an index (starting at 1) back to the _ovly_table entry
259 * that is pointing at this _ovly_buf_table entry. So, for
260 * example, for an overlay scenario with one overlay segment
261 * and two overlay sections:
262 * - Section 1 points to the first entry of the
263 * _ovly_buf_table, which contains a guard value
264 * of '1', referencing the first (index=0) entry of
266 * - Section 2 points to the second entry of the
267 * _ovly_buf_table, which contains a guard value
268 * of '2', referencing the second (index=1) entry of
271 map = vma_map_add(map, ovly.vma, ovly.size, ovly.offset,
272 ovly_buf_table_sym + (ovly.buf-1) * 4, i+1);