1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Cell Broadband Engine OProfile Support 4 * 5 * (C) Copyright IBM Corporation 2006 6 * 7 * Author: Maynard Johnson <maynardj@us.ibm.com> 8 */ 9 10/* The code in this source file is responsible for generating 11 * vma-to-fileOffset maps for both overlay and non-overlay SPU 12 * applications. 13 */ 14 15#include <linux/mm.h> 16#include <linux/string.h> 17#include <linux/uaccess.h> 18#include <linux/elf.h> 19#include <linux/slab.h> 20#include "pr_util.h" 21 22 23void vma_map_free(struct vma_to_fileoffset_map *map) 24{ 25 while (map) { 26 struct vma_to_fileoffset_map *next = map->next; 27 kfree(map); 28 map = next; 29 } 30} 31 32unsigned int 33vma_map_lookup(struct vma_to_fileoffset_map *map, unsigned int vma, 34 const struct spu *aSpu, int *grd_val) 35{ 36 /* 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 41 * thrown away. 42 */ 43 u32 offset = 0x10000000 + vma; 44 u32 ovly_grd; 45 46 for (; map; map = map->next) { 47 if (vma < map->vma || vma >= map->vma + map->size) 48 continue; 49 50 if (map->guard_ptr) { 51 ovly_grd = *(u32 *)(aSpu->local_store + map->guard_ptr); 52 if (ovly_grd != map->guard_val) 53 continue; 54 *grd_val = ovly_grd; 55 } 56 offset = vma - map->vma + map->offset; 57 break; 58 } 59 60 return offset; 61} 62 63static struct vma_to_fileoffset_map * 64vma_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) 67{ 68 struct vma_to_fileoffset_map *new = kzalloc(sizeof(*new), GFP_KERNEL); 69 70 if (!new) { 71 printk(KERN_ERR "SPU_PROF: %s, line %d: malloc failed\n", 72 __func__, __LINE__); 73 vma_map_free(map); 74 return NULL; 75 } 76 77 new->next = map; 78 new->vma = vma; 79 new->size = size; 80 new->offset = offset; 81 new->guard_ptr = guard_ptr; 82 new->guard_val = guard_val; 83 84 return new; 85} 86 87 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. */ 91struct vma_to_fileoffset_map *create_vma_map(const struct spu *aSpu, 92 unsigned long __spu_elf_start) 93{ 94 static const unsigned char expected[EI_PAD] = { 95 [EI_MAG0] = ELFMAG0, 96 [EI_MAG1] = ELFMAG1, 97 [EI_MAG2] = ELFMAG2, 98 [EI_MAG3] = ELFMAG3, 99 [EI_CLASS] = ELFCLASS32, 100 [EI_DATA] = ELFDATA2MSB, 101 [EI_VERSION] = EV_CURRENT, 102 [EI_OSABI] = ELFOSABI_NONE 103 }; 104 105 int grd_val; 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; 112 Elf32_Ehdr ehdr; 113 Elf32_Phdr phdr; 114 Elf32_Shdr shdr, shdr_str; 115 Elf32_Sym sym; 116 int i, j; 117 char name[32]; 118 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; 125 126 /* Get and validate ELF header. */ 127 128 if (copy_from_user(&ehdr, spu_elf_start, sizeof (ehdr))) 129 goto fail; 130 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", 134 __func__, __LINE__); 135 goto fail; 136 } 137 if (ehdr.e_machine != EM_SPU) { 138 printk(KERN_ERR "SPU_PROF: " 139 "%s, line %d: Unexpected e_machine parsing SPU ELF\n", 140 __func__, __LINE__); 141 goto fail; 142 } 143 if (ehdr.e_type != ET_EXEC) { 144 printk(KERN_ERR "SPU_PROF: " 145 "%s, line %d: Unexpected e_type parsing SPU ELF\n", 146 __func__, __LINE__); 147 goto fail; 148 } 149 phdr_start = spu_elf_start + ehdr.e_phoff; 150 shdr_start = spu_elf_start + ehdr.e_shoff; 151 152 /* Traverse program headers. */ 153 for (i = 0; i < ehdr.e_phnum; i++) { 154 if (copy_from_user(&phdr, phdr_start + i, sizeof(phdr))) 155 goto fail; 156 157 if (phdr.p_type != PT_LOAD) 158 continue; 159 if (phdr.p_flags & (1 << 27)) 160 continue; 161 162 map = vma_map_add(map, phdr.p_vaddr, phdr.p_memsz, 163 phdr.p_offset, 0, 0); 164 if (!map) 165 goto fail; 166 } 167 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))) 172 goto fail; 173 174 if (shdr.sh_type != SHT_SYMTAB) 175 continue; 176 if (shdr.sh_entsize != sizeof (sym)) 177 continue; 178 179 if (copy_from_user(&shdr_str, 180 shdr_start + shdr.sh_link, 181 sizeof(shdr))) 182 goto fail; 183 184 if (shdr_str.sh_type != SHT_STRTAB) 185 goto fail; 186 187 for (j = 0; j < shdr.sh_size / sizeof (sym); j++) { 188 if (copy_from_user(&sym, spu_elf_start + 189 shdr.sh_offset + 190 j * sizeof (sym), 191 sizeof (sym))) 192 goto fail; 193 194 if (copy_from_user(name, 195 spu_elf_start + shdr_str.sh_offset + 196 sym.st_name, 197 20)) 198 goto fail; 199 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; 208 } 209 } 210 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"); 215 goto out; 216 } else { 217 pr_debug("SPU_PROF: Overlay table found\n"); 218 } 219 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. 226 */ 227 overlay_tbl_offset = vma_map_lookup(map, ovly_table_sym, 228 aSpu, &grd_val); 229 if (overlay_tbl_offset > 0x10000000) { 230 printk(KERN_ERR "SPU_PROF: " 231 "%s, line %d: Error finding SPU overlay table\n", 232 __func__, __LINE__); 233 goto fail; 234 } 235 ovly_table = spu_elf_start + overlay_tbl_offset; 236 237 n_ovlys = (ovly_table_end_sym - 238 ovly_table_sym) / sizeof (ovly); 239 240 /* Traverse overlay table. */ 241 for (i = 0; i < n_ovlys; i++) { 242 if (copy_from_user(&ovly, ovly_table + i, sizeof (ovly))) 243 goto fail; 244 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 248 * value. 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 253 * from ovly.buf. 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 262 * _ovly_table. 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 266 * _ovly_table. 267 */ 268 map = vma_map_add(map, ovly.vma, ovly.size, ovly.offset, 269 ovly_buf_table_sym + (ovly.buf-1) * 4, i+1); 270 if (!map) 271 goto fail; 272 } 273 goto out; 274 275 fail: 276 map = NULL; 277 out: 278 return map; 279} 280