Path: blob/master/arch/powerpc/oprofile/cell/vma_map.c
10819 views
/*1* Cell Broadband Engine OProfile Support2*3* (C) Copyright IBM Corporation 20064*5* Author: Maynard Johnson <[email protected]>6*7* This program is free software; you can redistribute it and/or8* modify it under the terms of the GNU General Public License9* as published by the Free Software Foundation; either version10* 2 of the License, or (at your option) any later version.11*/1213/* The code in this source file is responsible for generating14* vma-to-fileOffset maps for both overlay and non-overlay SPU15* applications.16*/1718#include <linux/mm.h>19#include <linux/string.h>20#include <linux/uaccess.h>21#include <linux/elf.h>22#include <linux/slab.h>23#include "pr_util.h"242526void vma_map_free(struct vma_to_fileoffset_map *map)27{28while (map) {29struct vma_to_fileoffset_map *next = map->next;30kfree(map);31map = next;32}33}3435unsigned int36vma_map_lookup(struct vma_to_fileoffset_map *map, unsigned int vma,37const struct spu *aSpu, int *grd_val)38{39/*40* Default the offset to the physical address + a flag value.41* Addresses of dynamically generated code can't be found in the vma42* map. For those addresses the flagged value will be sent on to43* the user space tools so they can be reported rather than just44* thrown away.45*/46u32 offset = 0x10000000 + vma;47u32 ovly_grd;4849for (; map; map = map->next) {50if (vma < map->vma || vma >= map->vma + map->size)51continue;5253if (map->guard_ptr) {54ovly_grd = *(u32 *)(aSpu->local_store + map->guard_ptr);55if (ovly_grd != map->guard_val)56continue;57*grd_val = ovly_grd;58}59offset = vma - map->vma + map->offset;60break;61}6263return offset;64}6566static struct vma_to_fileoffset_map *67vma_map_add(struct vma_to_fileoffset_map *map, unsigned int vma,68unsigned int size, unsigned int offset, unsigned int guard_ptr,69unsigned int guard_val)70{71struct vma_to_fileoffset_map *new =72kzalloc(sizeof(struct vma_to_fileoffset_map), GFP_KERNEL);73if (!new) {74printk(KERN_ERR "SPU_PROF: %s, line %d: malloc failed\n",75__func__, __LINE__);76vma_map_free(map);77return NULL;78}7980new->next = map;81new->vma = vma;82new->size = size;83new->offset = offset;84new->guard_ptr = guard_ptr;85new->guard_val = guard_val;8687return new;88}899091/* Parse SPE ELF header and generate a list of vma_maps.92* A pointer to the first vma_map in the generated list93* of vma_maps is returned. */94struct vma_to_fileoffset_map *create_vma_map(const struct spu *aSpu,95unsigned long __spu_elf_start)96{97static const unsigned char expected[EI_PAD] = {98[EI_MAG0] = ELFMAG0,99[EI_MAG1] = ELFMAG1,100[EI_MAG2] = ELFMAG2,101[EI_MAG3] = ELFMAG3,102[EI_CLASS] = ELFCLASS32,103[EI_DATA] = ELFDATA2MSB,104[EI_VERSION] = EV_CURRENT,105[EI_OSABI] = ELFOSABI_NONE106};107108int grd_val;109struct vma_to_fileoffset_map *map = NULL;110void __user *spu_elf_start = (void __user *)__spu_elf_start;111struct spu_overlay_info ovly;112unsigned int overlay_tbl_offset = -1;113Elf32_Phdr __user *phdr_start;114Elf32_Shdr __user *shdr_start;115Elf32_Ehdr ehdr;116Elf32_Phdr phdr;117Elf32_Shdr shdr, shdr_str;118Elf32_Sym sym;119int i, j;120char name[32];121122unsigned int ovly_table_sym = 0;123unsigned int ovly_buf_table_sym = 0;124unsigned int ovly_table_end_sym = 0;125unsigned int ovly_buf_table_end_sym = 0;126struct spu_overlay_info __user *ovly_table;127unsigned int n_ovlys;128129/* Get and validate ELF header. */130131if (copy_from_user(&ehdr, spu_elf_start, sizeof (ehdr)))132goto fail;133134if (memcmp(ehdr.e_ident, expected, EI_PAD) != 0) {135printk(KERN_ERR "SPU_PROF: "136"%s, line %d: Unexpected e_ident parsing SPU ELF\n",137__func__, __LINE__);138goto fail;139}140if (ehdr.e_machine != EM_SPU) {141printk(KERN_ERR "SPU_PROF: "142"%s, line %d: Unexpected e_machine parsing SPU ELF\n",143__func__, __LINE__);144goto fail;145}146if (ehdr.e_type != ET_EXEC) {147printk(KERN_ERR "SPU_PROF: "148"%s, line %d: Unexpected e_type parsing SPU ELF\n",149__func__, __LINE__);150goto fail;151}152phdr_start = spu_elf_start + ehdr.e_phoff;153shdr_start = spu_elf_start + ehdr.e_shoff;154155/* Traverse program headers. */156for (i = 0; i < ehdr.e_phnum; i++) {157if (copy_from_user(&phdr, phdr_start + i, sizeof(phdr)))158goto fail;159160if (phdr.p_type != PT_LOAD)161continue;162if (phdr.p_flags & (1 << 27))163continue;164165map = vma_map_add(map, phdr.p_vaddr, phdr.p_memsz,166phdr.p_offset, 0, 0);167if (!map)168goto fail;169}170171pr_debug("SPU_PROF: Created non-overlay maps\n");172/* Traverse section table and search for overlay-related symbols. */173for (i = 0; i < ehdr.e_shnum; i++) {174if (copy_from_user(&shdr, shdr_start + i, sizeof(shdr)))175goto fail;176177if (shdr.sh_type != SHT_SYMTAB)178continue;179if (shdr.sh_entsize != sizeof (sym))180continue;181182if (copy_from_user(&shdr_str,183shdr_start + shdr.sh_link,184sizeof(shdr)))185goto fail;186187if (shdr_str.sh_type != SHT_STRTAB)188goto fail;189190for (j = 0; j < shdr.sh_size / sizeof (sym); j++) {191if (copy_from_user(&sym, spu_elf_start +192shdr.sh_offset +193j * sizeof (sym),194sizeof (sym)))195goto fail;196197if (copy_from_user(name,198spu_elf_start + shdr_str.sh_offset +199sym.st_name,20020))201goto fail;202203if (memcmp(name, "_ovly_table", 12) == 0)204ovly_table_sym = sym.st_value;205if (memcmp(name, "_ovly_buf_table", 16) == 0)206ovly_buf_table_sym = sym.st_value;207if (memcmp(name, "_ovly_table_end", 16) == 0)208ovly_table_end_sym = sym.st_value;209if (memcmp(name, "_ovly_buf_table_end", 20) == 0)210ovly_buf_table_end_sym = sym.st_value;211}212}213214/* If we don't have overlays, we're done. */215if (ovly_table_sym == 0 || ovly_buf_table_sym == 0216|| ovly_table_end_sym == 0 || ovly_buf_table_end_sym == 0) {217pr_debug("SPU_PROF: No overlay table found\n");218goto out;219} else {220pr_debug("SPU_PROF: Overlay table found\n");221}222223/* The _ovly_table symbol represents a table with one entry224* per overlay section. The _ovly_buf_table symbol represents225* a table with one entry per overlay region.226* The struct spu_overlay_info gives the structure of the _ovly_table227* entries. The structure of _ovly_table_buf is simply one228* u32 word per entry.229*/230overlay_tbl_offset = vma_map_lookup(map, ovly_table_sym,231aSpu, &grd_val);232if (overlay_tbl_offset > 0x10000000) {233printk(KERN_ERR "SPU_PROF: "234"%s, line %d: Error finding SPU overlay table\n",235__func__, __LINE__);236goto fail;237}238ovly_table = spu_elf_start + overlay_tbl_offset;239240n_ovlys = (ovly_table_end_sym -241ovly_table_sym) / sizeof (ovly);242243/* Traverse overlay table. */244for (i = 0; i < n_ovlys; i++) {245if (copy_from_user(&ovly, ovly_table + i, sizeof (ovly)))246goto fail;247248/* The ovly.vma/size/offset arguments are analogous to the same249* arguments used above for non-overlay maps. The final two250* args are referred to as the guard pointer and the guard251* value.252* The guard pointer is an entry in the _ovly_buf_table,253* computed using ovly.buf as the index into the table. Since254* ovly.buf values begin at '1' to reference the first (or 0th)255* entry in the _ovly_buf_table, the computation subtracts 1256* from ovly.buf.257* The guard value is stored in the _ovly_buf_table entry and258* is an index (starting at 1) back to the _ovly_table entry259* that is pointing at this _ovly_buf_table entry. So, for260* example, for an overlay scenario with one overlay segment261* and two overlay sections:262* - Section 1 points to the first entry of the263* _ovly_buf_table, which contains a guard value264* of '1', referencing the first (index=0) entry of265* _ovly_table.266* - Section 2 points to the second entry of the267* _ovly_buf_table, which contains a guard value268* of '2', referencing the second (index=1) entry of269* _ovly_table.270*/271map = vma_map_add(map, ovly.vma, ovly.size, ovly.offset,272ovly_buf_table_sym + (ovly.buf-1) * 4, i+1);273if (!map)274goto fail;275}276goto out;277278fail:279map = NULL;280out:281return map;282}283284285