1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Cell Broadband Engine OProfile Support
5 * (C) Copyright IBM Corporation 2006
7 * Author: Maynard Johnson <maynardj@us.ibm.com>
10 /* The code in this source file is responsible for generating
11 * vma-to-fileOffset maps for both overlay and non-overlay SPU
16 #include <linux/string.h>
17 #include <linux/uaccess.h>
18 #include <linux/elf.h>
19 #include <linux/slab.h>
23 void vma_map_free(struct vma_to_fileoffset_map
*map
)
26 struct vma_to_fileoffset_map
*next
= map
->next
;
33 vma_map_lookup(struct vma_to_fileoffset_map
*map
, unsigned int vma
,
34 const struct spu
*aSpu
, int *grd_val
)
37 * Default the offset to the physical address + a flag value.
38 * Addresses of dynamically generated code can't be found in the vma
39 * map. For those addresses the flagged value will be sent on to
40 * the user space tools so they can be reported rather than just
43 u32 offset
= 0x10000000 + vma
;
46 for (; map
; map
= map
->next
) {
47 if (vma
< map
->vma
|| vma
>= map
->vma
+ map
->size
)
51 ovly_grd
= *(u32
*)(aSpu
->local_store
+ map
->guard_ptr
);
52 if (ovly_grd
!= map
->guard_val
)
56 offset
= vma
- map
->vma
+ map
->offset
;
63 static struct vma_to_fileoffset_map
*
64 vma_map_add(struct vma_to_fileoffset_map
*map
, unsigned int vma
,
65 unsigned int size
, unsigned int offset
, unsigned int guard_ptr
,
66 unsigned int guard_val
)
68 struct vma_to_fileoffset_map
*new = kzalloc(sizeof(*new), GFP_KERNEL
);
71 printk(KERN_ERR
"SPU_PROF: %s, line %d: malloc failed\n",
81 new->guard_ptr
= guard_ptr
;
82 new->guard_val
= guard_val
;
88 /* Parse SPE ELF header and generate a list of vma_maps.
89 * A pointer to the first vma_map in the generated list
90 * of vma_maps is returned. */
91 struct vma_to_fileoffset_map
*create_vma_map(const struct spu
*aSpu
,
92 unsigned long __spu_elf_start
)
94 static const unsigned char expected
[EI_PAD
] = {
99 [EI_CLASS
] = ELFCLASS32
,
100 [EI_DATA
] = ELFDATA2MSB
,
101 [EI_VERSION
] = EV_CURRENT
,
102 [EI_OSABI
] = ELFOSABI_NONE
106 struct vma_to_fileoffset_map
*map
= NULL
;
107 void __user
*spu_elf_start
= (void __user
*)__spu_elf_start
;
108 struct spu_overlay_info ovly
;
109 unsigned int overlay_tbl_offset
= -1;
110 Elf32_Phdr __user
*phdr_start
;
111 Elf32_Shdr __user
*shdr_start
;
114 Elf32_Shdr shdr
, shdr_str
;
119 unsigned int ovly_table_sym
= 0;
120 unsigned int ovly_buf_table_sym
= 0;
121 unsigned int ovly_table_end_sym
= 0;
122 unsigned int ovly_buf_table_end_sym
= 0;
123 struct spu_overlay_info __user
*ovly_table
;
124 unsigned int n_ovlys
;
126 /* Get and validate ELF header. */
128 if (copy_from_user(&ehdr
, spu_elf_start
, sizeof (ehdr
)))
131 if (memcmp(ehdr
.e_ident
, expected
, EI_PAD
) != 0) {
132 printk(KERN_ERR
"SPU_PROF: "
133 "%s, line %d: Unexpected e_ident parsing SPU ELF\n",
137 if (ehdr
.e_machine
!= EM_SPU
) {
138 printk(KERN_ERR
"SPU_PROF: "
139 "%s, line %d: Unexpected e_machine parsing SPU ELF\n",
143 if (ehdr
.e_type
!= ET_EXEC
) {
144 printk(KERN_ERR
"SPU_PROF: "
145 "%s, line %d: Unexpected e_type parsing SPU ELF\n",
149 phdr_start
= spu_elf_start
+ ehdr
.e_phoff
;
150 shdr_start
= spu_elf_start
+ ehdr
.e_shoff
;
152 /* Traverse program headers. */
153 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
154 if (copy_from_user(&phdr
, phdr_start
+ i
, sizeof(phdr
)))
157 if (phdr
.p_type
!= PT_LOAD
)
159 if (phdr
.p_flags
& (1 << 27))
162 map
= vma_map_add(map
, phdr
.p_vaddr
, phdr
.p_memsz
,
163 phdr
.p_offset
, 0, 0);
168 pr_debug("SPU_PROF: Created non-overlay maps\n");
169 /* Traverse section table and search for overlay-related symbols. */
170 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
171 if (copy_from_user(&shdr
, shdr_start
+ i
, sizeof(shdr
)))
174 if (shdr
.sh_type
!= SHT_SYMTAB
)
176 if (shdr
.sh_entsize
!= sizeof (sym
))
179 if (copy_from_user(&shdr_str
,
180 shdr_start
+ shdr
.sh_link
,
184 if (shdr_str
.sh_type
!= SHT_STRTAB
)
187 for (j
= 0; j
< shdr
.sh_size
/ sizeof (sym
); j
++) {
188 if (copy_from_user(&sym
, spu_elf_start
+
194 if (copy_from_user(name
,
195 spu_elf_start
+ shdr_str
.sh_offset
+
200 if (memcmp(name
, "_ovly_table", 12) == 0)
201 ovly_table_sym
= sym
.st_value
;
202 if (memcmp(name
, "_ovly_buf_table", 16) == 0)
203 ovly_buf_table_sym
= sym
.st_value
;
204 if (memcmp(name
, "_ovly_table_end", 16) == 0)
205 ovly_table_end_sym
= sym
.st_value
;
206 if (memcmp(name
, "_ovly_buf_table_end", 20) == 0)
207 ovly_buf_table_end_sym
= sym
.st_value
;
211 /* If we don't have overlays, we're done. */
212 if (ovly_table_sym
== 0 || ovly_buf_table_sym
== 0
213 || ovly_table_end_sym
== 0 || ovly_buf_table_end_sym
== 0) {
214 pr_debug("SPU_PROF: No overlay table found\n");
217 pr_debug("SPU_PROF: Overlay table found\n");
220 /* The _ovly_table symbol represents a table with one entry
221 * per overlay section. The _ovly_buf_table symbol represents
222 * a table with one entry per overlay region.
223 * The struct spu_overlay_info gives the structure of the _ovly_table
224 * entries. The structure of _ovly_table_buf is simply one
225 * u32 word per entry.
227 overlay_tbl_offset
= vma_map_lookup(map
, ovly_table_sym
,
229 if (overlay_tbl_offset
> 0x10000000) {
230 printk(KERN_ERR
"SPU_PROF: "
231 "%s, line %d: Error finding SPU overlay table\n",
235 ovly_table
= spu_elf_start
+ overlay_tbl_offset
;
237 n_ovlys
= (ovly_table_end_sym
-
238 ovly_table_sym
) / sizeof (ovly
);
240 /* Traverse overlay table. */
241 for (i
= 0; i
< n_ovlys
; i
++) {
242 if (copy_from_user(&ovly
, ovly_table
+ i
, sizeof (ovly
)))
245 /* The ovly.vma/size/offset arguments are analogous to the same
246 * arguments used above for non-overlay maps. The final two
247 * args are referred to as the guard pointer and the guard
249 * The guard pointer is an entry in the _ovly_buf_table,
250 * computed using ovly.buf as the index into the table. Since
251 * ovly.buf values begin at '1' to reference the first (or 0th)
252 * entry in the _ovly_buf_table, the computation subtracts 1
254 * The guard value is stored in the _ovly_buf_table entry and
255 * is an index (starting at 1) back to the _ovly_table entry
256 * that is pointing at this _ovly_buf_table entry. So, for
257 * example, for an overlay scenario with one overlay segment
258 * and two overlay sections:
259 * - Section 1 points to the first entry of the
260 * _ovly_buf_table, which contains a guard value
261 * of '1', referencing the first (index=0) entry of
263 * - Section 2 points to the second entry of the
264 * _ovly_buf_table, which contains a guard value
265 * of '2', referencing the second (index=1) entry of
268 map
= vma_map_add(map
, ovly
.vma
, ovly
.size
, ovly
.offset
,
269 ovly_buf_table_sym
+ (ovly
.buf
-1) * 4, i
+1);