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>
23 #include <linux/slab.h>
27 void vma_map_free(struct vma_to_fileoffset_map
*map
)
30 struct vma_to_fileoffset_map
*next
= map
->next
;
37 vma_map_lookup(struct vma_to_fileoffset_map
*map
, unsigned int vma
,
38 const struct spu
*aSpu
, int *grd_val
)
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
47 u32 offset
= 0x10000000 + vma
;
50 for (; map
; map
= map
->next
) {
51 if (vma
< map
->vma
|| vma
>= map
->vma
+ map
->size
)
55 ovly_grd
= *(u32
*)(aSpu
->local_store
+ map
->guard_ptr
);
56 if (ovly_grd
!= map
->guard_val
)
60 offset
= vma
- map
->vma
+ map
->offset
;
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
)
72 struct vma_to_fileoffset_map
*new =
73 kzalloc(sizeof(struct vma_to_fileoffset_map
), GFP_KERNEL
);
75 printk(KERN_ERR
"SPU_PROF: %s, line %d: malloc failed\n",
85 new->guard_ptr
= guard_ptr
;
86 new->guard_val
= guard_val
;
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
)
98 static const unsigned char expected
[EI_PAD
] = {
103 [EI_CLASS
] = ELFCLASS32
,
104 [EI_DATA
] = ELFDATA2MSB
,
105 [EI_VERSION
] = EV_CURRENT
,
106 [EI_OSABI
] = ELFOSABI_NONE
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
;
118 Elf32_Shdr shdr
, shdr_str
;
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
;
130 /* Get and validate ELF header. */
132 if (copy_from_user(&ehdr
, spu_elf_start
, sizeof (ehdr
)))
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",
141 if (ehdr
.e_machine
!= EM_SPU
) {
142 printk(KERN_ERR
"SPU_PROF: "
143 "%s, line %d: Unexpected e_machine parsing SPU ELF\n",
147 if (ehdr
.e_type
!= ET_EXEC
) {
148 printk(KERN_ERR
"SPU_PROF: "
149 "%s, line %d: Unexpected e_type parsing SPU ELF\n",
153 phdr_start
= spu_elf_start
+ ehdr
.e_phoff
;
154 shdr_start
= spu_elf_start
+ ehdr
.e_shoff
;
156 /* Traverse program headers. */
157 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
158 if (copy_from_user(&phdr
, phdr_start
+ i
, sizeof(phdr
)))
161 if (phdr
.p_type
!= PT_LOAD
)
163 if (phdr
.p_flags
& (1 << 27))
166 map
= vma_map_add(map
, phdr
.p_vaddr
, phdr
.p_memsz
,
167 phdr
.p_offset
, 0, 0);
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
)))
178 if (shdr
.sh_type
!= SHT_SYMTAB
)
180 if (shdr
.sh_entsize
!= sizeof (sym
))
183 if (copy_from_user(&shdr_str
,
184 shdr_start
+ shdr
.sh_link
,
188 if (shdr_str
.sh_type
!= SHT_STRTAB
)
191 for (j
= 0; j
< shdr
.sh_size
/ sizeof (sym
); j
++) {
192 if (copy_from_user(&sym
, spu_elf_start
+
198 if (copy_from_user(name
,
199 spu_elf_start
+ shdr_str
.sh_offset
+
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
;
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");
221 pr_debug("SPU_PROF: Overlay table found\n");
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.
231 overlay_tbl_offset
= vma_map_lookup(map
, ovly_table_sym
,
233 if (overlay_tbl_offset
> 0x10000000) {
234 printk(KERN_ERR
"SPU_PROF: "
235 "%s, line %d: Error finding SPU overlay table\n",
239 ovly_table
= spu_elf_start
+ overlay_tbl_offset
;
241 n_ovlys
= (ovly_table_end_sym
-
242 ovly_table_sym
) / sizeof (ovly
);
244 /* Traverse overlay table. */
245 for (i
= 0; i
< n_ovlys
; i
++) {
246 if (copy_from_user(&ovly
, ovly_table
+ i
, sizeof (ovly
)))
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
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
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
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
272 map
= vma_map_add(map
, ovly
.vma
, ovly
.size
, ovly
.offset
,
273 ovly_buf_table_sym
+ (ovly
.buf
-1) * 4, i
+1);