Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is subject to the terms and conditions of the GNU General Public
0003  * License.  See the file "COPYING" in the main directory of this archive
0004  * for more details.
0005  *
0006  * Copyright (C) 2004, 2005 MIPS Technologies, Inc.  All rights reserved.
0007  * Copyright (C) 2013 Imagination Technologies Ltd.
0008  *
0009  * VPE spport module for loading a MIPS SP program into VPE1. The SP
0010  * environment is rather simple since there are no TLBs. It needs
0011  * to be relocatable (or partiall linked). Initialize your stack in
0012  * the startup-code. The loader looks for the symbol __start and sets
0013  * up the execution to resume from there. To load and run, simply do
0014  * a cat SP 'binary' to the /dev/vpe1 device.
0015  */
0016 #include <linux/kernel.h>
0017 #include <linux/device.h>
0018 #include <linux/fs.h>
0019 #include <linux/init.h>
0020 #include <linux/slab.h>
0021 #include <linux/list.h>
0022 #include <linux/vmalloc.h>
0023 #include <linux/elf.h>
0024 #include <linux/seq_file.h>
0025 #include <linux/syscalls.h>
0026 #include <linux/moduleloader.h>
0027 #include <linux/interrupt.h>
0028 #include <linux/poll.h>
0029 #include <linux/memblock.h>
0030 #include <asm/mipsregs.h>
0031 #include <asm/mipsmtregs.h>
0032 #include <asm/cacheflush.h>
0033 #include <linux/atomic.h>
0034 #include <asm/mips_mt.h>
0035 #include <asm/processor.h>
0036 #include <asm/vpe.h>
0037 
0038 #ifndef ARCH_SHF_SMALL
0039 #define ARCH_SHF_SMALL 0
0040 #endif
0041 
0042 /* If this is set, the section belongs in the init part of the module */
0043 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
0044 
0045 struct vpe_control vpecontrol = {
0046     .vpe_list_lock  = __SPIN_LOCK_UNLOCKED(vpe_list_lock),
0047     .vpe_list   = LIST_HEAD_INIT(vpecontrol.vpe_list),
0048     .tc_list_lock   = __SPIN_LOCK_UNLOCKED(tc_list_lock),
0049     .tc_list    = LIST_HEAD_INIT(vpecontrol.tc_list)
0050 };
0051 
0052 /* get the vpe associated with this minor */
0053 struct vpe *get_vpe(int minor)
0054 {
0055     struct vpe *res, *v;
0056 
0057     if (!cpu_has_mipsmt)
0058         return NULL;
0059 
0060     res = NULL;
0061     spin_lock(&vpecontrol.vpe_list_lock);
0062     list_for_each_entry(v, &vpecontrol.vpe_list, list) {
0063         if (v->minor == VPE_MODULE_MINOR) {
0064             res = v;
0065             break;
0066         }
0067     }
0068     spin_unlock(&vpecontrol.vpe_list_lock);
0069 
0070     return res;
0071 }
0072 
0073 /* get the vpe associated with this minor */
0074 struct tc *get_tc(int index)
0075 {
0076     struct tc *res, *t;
0077 
0078     res = NULL;
0079     spin_lock(&vpecontrol.tc_list_lock);
0080     list_for_each_entry(t, &vpecontrol.tc_list, list) {
0081         if (t->index == index) {
0082             res = t;
0083             break;
0084         }
0085     }
0086     spin_unlock(&vpecontrol.tc_list_lock);
0087 
0088     return res;
0089 }
0090 
0091 /* allocate a vpe and associate it with this minor (or index) */
0092 struct vpe *alloc_vpe(int minor)
0093 {
0094     struct vpe *v;
0095 
0096     v = kzalloc(sizeof(struct vpe), GFP_KERNEL);
0097     if (v == NULL)
0098         goto out;
0099 
0100     INIT_LIST_HEAD(&v->tc);
0101     spin_lock(&vpecontrol.vpe_list_lock);
0102     list_add_tail(&v->list, &vpecontrol.vpe_list);
0103     spin_unlock(&vpecontrol.vpe_list_lock);
0104 
0105     INIT_LIST_HEAD(&v->notify);
0106     v->minor = VPE_MODULE_MINOR;
0107 
0108 out:
0109     return v;
0110 }
0111 
0112 /* allocate a tc. At startup only tc0 is running, all other can be halted. */
0113 struct tc *alloc_tc(int index)
0114 {
0115     struct tc *tc;
0116 
0117     tc = kzalloc(sizeof(struct tc), GFP_KERNEL);
0118     if (tc == NULL)
0119         goto out;
0120 
0121     INIT_LIST_HEAD(&tc->tc);
0122     tc->index = index;
0123 
0124     spin_lock(&vpecontrol.tc_list_lock);
0125     list_add_tail(&tc->list, &vpecontrol.tc_list);
0126     spin_unlock(&vpecontrol.tc_list_lock);
0127 
0128 out:
0129     return tc;
0130 }
0131 
0132 /* clean up and free everything */
0133 void release_vpe(struct vpe *v)
0134 {
0135     list_del(&v->list);
0136     if (v->load_addr)
0137         release_progmem(v->load_addr);
0138     kfree(v);
0139 }
0140 
0141 /* Find some VPE program space */
0142 void *alloc_progmem(unsigned long len)
0143 {
0144     void *addr;
0145 
0146 #ifdef CONFIG_MIPS_VPE_LOADER_TOM
0147     /*
0148      * This means you must tell Linux to use less memory than you
0149      * physically have, for example by passing a mem= boot argument.
0150      */
0151     addr = pfn_to_kaddr(max_low_pfn);
0152     memset(addr, 0, len);
0153 #else
0154     /* simple grab some mem for now */
0155     addr = kzalloc(len, GFP_KERNEL);
0156 #endif
0157 
0158     return addr;
0159 }
0160 
0161 void release_progmem(void *ptr)
0162 {
0163 #ifndef CONFIG_MIPS_VPE_LOADER_TOM
0164     kfree(ptr);
0165 #endif
0166 }
0167 
0168 /* Update size with this section: return offset. */
0169 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
0170 {
0171     long ret;
0172 
0173     ret = ALIGN(*size, sechdr->sh_addralign ? : 1);
0174     *size = ret + sechdr->sh_size;
0175     return ret;
0176 }
0177 
0178 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
0179    might -- code, read-only data, read-write data, small data.  Tally
0180    sizes, and place the offsets into sh_entsize fields: high bit means it
0181    belongs in init. */
0182 static void layout_sections(struct module *mod, const Elf_Ehdr *hdr,
0183                 Elf_Shdr *sechdrs, const char *secstrings)
0184 {
0185     static unsigned long const masks[][2] = {
0186         /* NOTE: all executable code must be the first section
0187          * in this array; otherwise modify the text_size
0188          * finder in the two loops below */
0189         {SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL},
0190         {SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL},
0191         {SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL},
0192         {ARCH_SHF_SMALL | SHF_ALLOC, 0}
0193     };
0194     unsigned int m, i;
0195 
0196     for (i = 0; i < hdr->e_shnum; i++)
0197         sechdrs[i].sh_entsize = ~0UL;
0198 
0199     for (m = 0; m < ARRAY_SIZE(masks); ++m) {
0200         for (i = 0; i < hdr->e_shnum; ++i) {
0201             Elf_Shdr *s = &sechdrs[i];
0202 
0203             if ((s->sh_flags & masks[m][0]) != masks[m][0]
0204                 || (s->sh_flags & masks[m][1])
0205                 || s->sh_entsize != ~0UL)
0206                 continue;
0207             s->sh_entsize =
0208                 get_offset((unsigned long *)&mod->core_layout.size, s);
0209         }
0210 
0211         if (m == 0)
0212             mod->core_layout.text_size = mod->core_layout.size;
0213 
0214     }
0215 }
0216 
0217 /* from module-elf32.c, but subverted a little */
0218 
0219 struct mips_hi16 {
0220     struct mips_hi16 *next;
0221     Elf32_Addr *addr;
0222     Elf32_Addr value;
0223 };
0224 
0225 static struct mips_hi16 *mips_hi16_list;
0226 static unsigned int gp_offs, gp_addr;
0227 
0228 static int apply_r_mips_none(struct module *me, uint32_t *location,
0229                  Elf32_Addr v)
0230 {
0231     return 0;
0232 }
0233 
0234 static int apply_r_mips_gprel16(struct module *me, uint32_t *location,
0235                 Elf32_Addr v)
0236 {
0237     int rel;
0238 
0239     if (!(*location & 0xffff)) {
0240         rel = (int)v - gp_addr;
0241     } else {
0242         /* .sbss + gp(relative) + offset */
0243         /* kludge! */
0244         rel =  (int)(short)((int)v + gp_offs +
0245                     (int)(short)(*location & 0xffff) - gp_addr);
0246     }
0247 
0248     if ((rel > 32768) || (rel < -32768)) {
0249         pr_debug("VPE loader: apply_r_mips_gprel16: relative address 0x%x out of range of gp register\n",
0250              rel);
0251         return -ENOEXEC;
0252     }
0253 
0254     *location = (*location & 0xffff0000) | (rel & 0xffff);
0255 
0256     return 0;
0257 }
0258 
0259 static int apply_r_mips_pc16(struct module *me, uint32_t *location,
0260                  Elf32_Addr v)
0261 {
0262     int rel;
0263     rel = (((unsigned int)v - (unsigned int)location));
0264     rel >>= 2; /* because the offset is in _instructions_ not bytes. */
0265     rel -= 1;  /* and one instruction less due to the branch delay slot. */
0266 
0267     if ((rel > 32768) || (rel < -32768)) {
0268         pr_debug("VPE loader: apply_r_mips_pc16: relative address out of range 0x%x\n",
0269              rel);
0270         return -ENOEXEC;
0271     }
0272 
0273     *location = (*location & 0xffff0000) | (rel & 0xffff);
0274 
0275     return 0;
0276 }
0277 
0278 static int apply_r_mips_32(struct module *me, uint32_t *location,
0279                Elf32_Addr v)
0280 {
0281     *location += v;
0282 
0283     return 0;
0284 }
0285 
0286 static int apply_r_mips_26(struct module *me, uint32_t *location,
0287                Elf32_Addr v)
0288 {
0289     if (v % 4) {
0290         pr_debug("VPE loader: apply_r_mips_26: unaligned relocation\n");
0291         return -ENOEXEC;
0292     }
0293 
0294 /*
0295  * Not desperately convinced this is a good check of an overflow condition
0296  * anyway. But it gets in the way of handling undefined weak symbols which
0297  * we want to set to zero.
0298  * if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
0299  * printk(KERN_ERR
0300  * "module %s: relocation overflow\n",
0301  * me->name);
0302  * return -ENOEXEC;
0303  * }
0304  */
0305 
0306     *location = (*location & ~0x03ffffff) |
0307         ((*location + (v >> 2)) & 0x03ffffff);
0308     return 0;
0309 }
0310 
0311 static int apply_r_mips_hi16(struct module *me, uint32_t *location,
0312                  Elf32_Addr v)
0313 {
0314     struct mips_hi16 *n;
0315 
0316     /*
0317      * We cannot relocate this one now because we don't know the value of
0318      * the carry we need to add.  Save the information, and let LO16 do the
0319      * actual relocation.
0320      */
0321     n = kmalloc(sizeof(*n), GFP_KERNEL);
0322     if (!n)
0323         return -ENOMEM;
0324 
0325     n->addr = location;
0326     n->value = v;
0327     n->next = mips_hi16_list;
0328     mips_hi16_list = n;
0329 
0330     return 0;
0331 }
0332 
0333 static int apply_r_mips_lo16(struct module *me, uint32_t *location,
0334                  Elf32_Addr v)
0335 {
0336     unsigned long insnlo = *location;
0337     Elf32_Addr val, vallo;
0338     struct mips_hi16 *l, *next;
0339 
0340     /* Sign extend the addend we extract from the lo insn.  */
0341     vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
0342 
0343     if (mips_hi16_list != NULL) {
0344 
0345         l = mips_hi16_list;
0346         while (l != NULL) {
0347             unsigned long insn;
0348 
0349             /*
0350              * The value for the HI16 had best be the same.
0351              */
0352             if (v != l->value) {
0353                 pr_debug("VPE loader: apply_r_mips_lo16/hi16: inconsistent value information\n");
0354                 goto out_free;
0355             }
0356 
0357             /*
0358              * Do the HI16 relocation.  Note that we actually don't
0359              * need to know anything about the LO16 itself, except
0360              * where to find the low 16 bits of the addend needed
0361              * by the LO16.
0362              */
0363             insn = *l->addr;
0364             val = ((insn & 0xffff) << 16) + vallo;
0365             val += v;
0366 
0367             /*
0368              * Account for the sign extension that will happen in
0369              * the low bits.
0370              */
0371             val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
0372 
0373             insn = (insn & ~0xffff) | val;
0374             *l->addr = insn;
0375 
0376             next = l->next;
0377             kfree(l);
0378             l = next;
0379         }
0380 
0381         mips_hi16_list = NULL;
0382     }
0383 
0384     /*
0385      * Ok, we're done with the HI16 relocs.  Now deal with the LO16.
0386      */
0387     val = v + vallo;
0388     insnlo = (insnlo & ~0xffff) | (val & 0xffff);
0389     *location = insnlo;
0390 
0391     return 0;
0392 
0393 out_free:
0394     while (l != NULL) {
0395         next = l->next;
0396         kfree(l);
0397         l = next;
0398     }
0399     mips_hi16_list = NULL;
0400 
0401     return -ENOEXEC;
0402 }
0403 
0404 static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
0405                 Elf32_Addr v) = {
0406     [R_MIPS_NONE]   = apply_r_mips_none,
0407     [R_MIPS_32] = apply_r_mips_32,
0408     [R_MIPS_26] = apply_r_mips_26,
0409     [R_MIPS_HI16]   = apply_r_mips_hi16,
0410     [R_MIPS_LO16]   = apply_r_mips_lo16,
0411     [R_MIPS_GPREL16] = apply_r_mips_gprel16,
0412     [R_MIPS_PC16] = apply_r_mips_pc16
0413 };
0414 
0415 static char *rstrs[] = {
0416     [R_MIPS_NONE]   = "MIPS_NONE",
0417     [R_MIPS_32] = "MIPS_32",
0418     [R_MIPS_26] = "MIPS_26",
0419     [R_MIPS_HI16]   = "MIPS_HI16",
0420     [R_MIPS_LO16]   = "MIPS_LO16",
0421     [R_MIPS_GPREL16] = "MIPS_GPREL16",
0422     [R_MIPS_PC16] = "MIPS_PC16"
0423 };
0424 
0425 static int apply_relocations(Elf32_Shdr *sechdrs,
0426               const char *strtab,
0427               unsigned int symindex,
0428               unsigned int relsec,
0429               struct module *me)
0430 {
0431     Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
0432     Elf32_Sym *sym;
0433     uint32_t *location;
0434     unsigned int i;
0435     Elf32_Addr v;
0436     int res;
0437 
0438     for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
0439         Elf32_Word r_info = rel[i].r_info;
0440 
0441         /* This is where to make the change */
0442         location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
0443             + rel[i].r_offset;
0444         /* This is the symbol it is referring to */
0445         sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
0446             + ELF32_R_SYM(r_info);
0447 
0448         if (!sym->st_value) {
0449             pr_debug("%s: undefined weak symbol %s\n",
0450                  me->name, strtab + sym->st_name);
0451             /* just print the warning, dont barf */
0452         }
0453 
0454         v = sym->st_value;
0455 
0456         res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
0457         if (res) {
0458             char *r = rstrs[ELF32_R_TYPE(r_info)];
0459             pr_warn("VPE loader: .text+0x%x relocation type %s for symbol \"%s\" failed\n",
0460                 rel[i].r_offset, r ? r : "UNKNOWN",
0461                 strtab + sym->st_name);
0462             return res;
0463         }
0464     }
0465 
0466     return 0;
0467 }
0468 
0469 static inline void save_gp_address(unsigned int secbase, unsigned int rel)
0470 {
0471     gp_addr = secbase + rel;
0472     gp_offs = gp_addr - (secbase & 0xffff0000);
0473 }
0474 /* end module-elf32.c */
0475 
0476 /* Change all symbols so that sh_value encodes the pointer directly. */
0477 static void simplify_symbols(Elf_Shdr *sechdrs,
0478                 unsigned int symindex,
0479                 const char *strtab,
0480                 const char *secstrings,
0481                 unsigned int nsecs, struct module *mod)
0482 {
0483     Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
0484     unsigned long secbase, bssbase = 0;
0485     unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
0486     int size;
0487 
0488     /* find the .bss section for COMMON symbols */
0489     for (i = 0; i < nsecs; i++) {
0490         if (strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) == 0) {
0491             bssbase = sechdrs[i].sh_addr;
0492             break;
0493         }
0494     }
0495 
0496     for (i = 1; i < n; i++) {
0497         switch (sym[i].st_shndx) {
0498         case SHN_COMMON:
0499             /* Allocate space for the symbol in the .bss section.
0500                st_value is currently size.
0501                We want it to have the address of the symbol. */
0502 
0503             size = sym[i].st_value;
0504             sym[i].st_value = bssbase;
0505 
0506             bssbase += size;
0507             break;
0508 
0509         case SHN_ABS:
0510             /* Don't need to do anything */
0511             break;
0512 
0513         case SHN_UNDEF:
0514             /* ret = -ENOENT; */
0515             break;
0516 
0517         case SHN_MIPS_SCOMMON:
0518             pr_debug("simplify_symbols: ignoring SHN_MIPS_SCOMMON symbol <%s> st_shndx %d\n",
0519                  strtab + sym[i].st_name, sym[i].st_shndx);
0520             /* .sbss section */
0521             break;
0522 
0523         default:
0524             secbase = sechdrs[sym[i].st_shndx].sh_addr;
0525 
0526             if (strncmp(strtab + sym[i].st_name, "_gp", 3) == 0)
0527                 save_gp_address(secbase, sym[i].st_value);
0528 
0529             sym[i].st_value += secbase;
0530             break;
0531         }
0532     }
0533 }
0534 
0535 #ifdef DEBUG_ELFLOADER
0536 static void dump_elfsymbols(Elf_Shdr *sechdrs, unsigned int symindex,
0537                 const char *strtab, struct module *mod)
0538 {
0539     Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
0540     unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
0541 
0542     pr_debug("dump_elfsymbols: n %d\n", n);
0543     for (i = 1; i < n; i++) {
0544         pr_debug(" i %d name <%s> 0x%x\n", i, strtab + sym[i].st_name,
0545              sym[i].st_value);
0546     }
0547 }
0548 #endif
0549 
0550 static int find_vpe_symbols(struct vpe *v, Elf_Shdr *sechdrs,
0551                       unsigned int symindex, const char *strtab,
0552                       struct module *mod)
0553 {
0554     Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
0555     unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
0556 
0557     for (i = 1; i < n; i++) {
0558         if (strcmp(strtab + sym[i].st_name, "__start") == 0)
0559             v->__start = sym[i].st_value;
0560 
0561         if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0)
0562             v->shared_ptr = (void *)sym[i].st_value;
0563     }
0564 
0565     if ((v->__start == 0) || (v->shared_ptr == NULL))
0566         return -1;
0567 
0568     return 0;
0569 }
0570 
0571 /*
0572  * Allocates a VPE with some program code space(the load address), copies the
0573  * contents of the program (p)buffer performing relocatations/etc, free's it
0574  * when finished.
0575  */
0576 static int vpe_elfload(struct vpe *v)
0577 {
0578     Elf_Ehdr *hdr;
0579     Elf_Shdr *sechdrs;
0580     long err = 0;
0581     char *secstrings, *strtab = NULL;
0582     unsigned int len, i, symindex = 0, strindex = 0, relocate = 0;
0583     struct module mod; /* so we can re-use the relocations code */
0584 
0585     memset(&mod, 0, sizeof(struct module));
0586     strcpy(mod.name, "VPE loader");
0587 
0588     hdr = (Elf_Ehdr *) v->pbuffer;
0589     len = v->plen;
0590 
0591     /* Sanity checks against insmoding binaries or wrong arch,
0592        weird elf version */
0593     if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
0594         || (hdr->e_type != ET_REL && hdr->e_type != ET_EXEC)
0595         || !elf_check_arch(hdr)
0596         || hdr->e_shentsize != sizeof(*sechdrs)) {
0597         pr_warn("VPE loader: program wrong arch or weird elf version\n");
0598 
0599         return -ENOEXEC;
0600     }
0601 
0602     if (hdr->e_type == ET_REL)
0603         relocate = 1;
0604 
0605     if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
0606         pr_err("VPE loader: program length %u truncated\n", len);
0607 
0608         return -ENOEXEC;
0609     }
0610 
0611     /* Convenience variables */
0612     sechdrs = (void *)hdr + hdr->e_shoff;
0613     secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
0614     sechdrs[0].sh_addr = 0;
0615 
0616     /* And these should exist, but gcc whinges if we don't init them */
0617     symindex = strindex = 0;
0618 
0619     if (relocate) {
0620         for (i = 1; i < hdr->e_shnum; i++) {
0621             if ((sechdrs[i].sh_type != SHT_NOBITS) &&
0622                 (len < sechdrs[i].sh_offset + sechdrs[i].sh_size)) {
0623                 pr_err("VPE program length %u truncated\n",
0624                        len);
0625                 return -ENOEXEC;
0626             }
0627 
0628             /* Mark all sections sh_addr with their address in the
0629                temporary image. */
0630             sechdrs[i].sh_addr = (size_t) hdr +
0631                 sechdrs[i].sh_offset;
0632 
0633             /* Internal symbols and strings. */
0634             if (sechdrs[i].sh_type == SHT_SYMTAB) {
0635                 symindex = i;
0636                 strindex = sechdrs[i].sh_link;
0637                 strtab = (char *)hdr +
0638                     sechdrs[strindex].sh_offset;
0639             }
0640         }
0641         layout_sections(&mod, hdr, sechdrs, secstrings);
0642     }
0643 
0644     v->load_addr = alloc_progmem(mod.core_layout.size);
0645     if (!v->load_addr)
0646         return -ENOMEM;
0647 
0648     pr_info("VPE loader: loading to %p\n", v->load_addr);
0649 
0650     if (relocate) {
0651         for (i = 0; i < hdr->e_shnum; i++) {
0652             void *dest;
0653 
0654             if (!(sechdrs[i].sh_flags & SHF_ALLOC))
0655                 continue;
0656 
0657             dest = v->load_addr + sechdrs[i].sh_entsize;
0658 
0659             if (sechdrs[i].sh_type != SHT_NOBITS)
0660                 memcpy(dest, (void *)sechdrs[i].sh_addr,
0661                        sechdrs[i].sh_size);
0662             /* Update sh_addr to point to copy in image. */
0663             sechdrs[i].sh_addr = (unsigned long)dest;
0664 
0665             pr_debug(" section sh_name %s sh_addr 0x%x\n",
0666                  secstrings + sechdrs[i].sh_name,
0667                  sechdrs[i].sh_addr);
0668         }
0669 
0670         /* Fix up syms, so that st_value is a pointer to location. */
0671         simplify_symbols(sechdrs, symindex, strtab, secstrings,
0672                  hdr->e_shnum, &mod);
0673 
0674         /* Now do relocations. */
0675         for (i = 1; i < hdr->e_shnum; i++) {
0676             const char *strtab = (char *)sechdrs[strindex].sh_addr;
0677             unsigned int info = sechdrs[i].sh_info;
0678 
0679             /* Not a valid relocation section? */
0680             if (info >= hdr->e_shnum)
0681                 continue;
0682 
0683             /* Don't bother with non-allocated sections */
0684             if (!(sechdrs[info].sh_flags & SHF_ALLOC))
0685                 continue;
0686 
0687             if (sechdrs[i].sh_type == SHT_REL)
0688                 err = apply_relocations(sechdrs, strtab,
0689                             symindex, i, &mod);
0690             else if (sechdrs[i].sh_type == SHT_RELA)
0691                 err = apply_relocate_add(sechdrs, strtab,
0692                              symindex, i, &mod);
0693             if (err < 0)
0694                 return err;
0695 
0696         }
0697     } else {
0698         struct elf_phdr *phdr = (struct elf_phdr *)
0699                         ((char *)hdr + hdr->e_phoff);
0700 
0701         for (i = 0; i < hdr->e_phnum; i++) {
0702             if (phdr->p_type == PT_LOAD) {
0703                 memcpy((void *)phdr->p_paddr,
0704                        (char *)hdr + phdr->p_offset,
0705                        phdr->p_filesz);
0706                 memset((void *)phdr->p_paddr + phdr->p_filesz,
0707                        0, phdr->p_memsz - phdr->p_filesz);
0708             }
0709             phdr++;
0710         }
0711 
0712         for (i = 0; i < hdr->e_shnum; i++) {
0713             /* Internal symbols and strings. */
0714             if (sechdrs[i].sh_type == SHT_SYMTAB) {
0715                 symindex = i;
0716                 strindex = sechdrs[i].sh_link;
0717                 strtab = (char *)hdr +
0718                     sechdrs[strindex].sh_offset;
0719 
0720                 /*
0721                  * mark symtab's address for when we try
0722                  * to find the magic symbols
0723                  */
0724                 sechdrs[i].sh_addr = (size_t) hdr +
0725                     sechdrs[i].sh_offset;
0726             }
0727         }
0728     }
0729 
0730     /* make sure it's physically written out */
0731     flush_icache_range((unsigned long)v->load_addr,
0732                (unsigned long)v->load_addr + v->len);
0733 
0734     if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
0735         if (v->__start == 0) {
0736             pr_warn("VPE loader: program does not contain a __start symbol\n");
0737             return -ENOEXEC;
0738         }
0739 
0740         if (v->shared_ptr == NULL)
0741             pr_warn("VPE loader: program does not contain vpe_shared symbol.\n"
0742                 " Unable to use AMVP (AP/SP) facilities.\n");
0743     }
0744 
0745     pr_info(" elf loaded\n");
0746     return 0;
0747 }
0748 
0749 /* checks VPE is unused and gets ready to load program  */
0750 static int vpe_open(struct inode *inode, struct file *filp)
0751 {
0752     enum vpe_state state;
0753     struct vpe_notifications *notifier;
0754     struct vpe *v;
0755 
0756     if (VPE_MODULE_MINOR != iminor(inode)) {
0757         /* assume only 1 device at the moment. */
0758         pr_warn("VPE loader: only vpe1 is supported\n");
0759 
0760         return -ENODEV;
0761     }
0762 
0763     v = get_vpe(aprp_cpu_index());
0764     if (v == NULL) {
0765         pr_warn("VPE loader: unable to get vpe\n");
0766 
0767         return -ENODEV;
0768     }
0769 
0770     state = xchg(&v->state, VPE_STATE_INUSE);
0771     if (state != VPE_STATE_UNUSED) {
0772         pr_debug("VPE loader: tc in use dumping regs\n");
0773 
0774         list_for_each_entry(notifier, &v->notify, list)
0775             notifier->stop(aprp_cpu_index());
0776 
0777         release_progmem(v->load_addr);
0778         cleanup_tc(get_tc(aprp_cpu_index()));
0779     }
0780 
0781     /* this of-course trashes what was there before... */
0782     v->pbuffer = vmalloc(P_SIZE);
0783     if (!v->pbuffer) {
0784         pr_warn("VPE loader: unable to allocate memory\n");
0785         return -ENOMEM;
0786     }
0787     v->plen = P_SIZE;
0788     v->load_addr = NULL;
0789     v->len = 0;
0790     v->shared_ptr = NULL;
0791     v->__start = 0;
0792 
0793     return 0;
0794 }
0795 
0796 static int vpe_release(struct inode *inode, struct file *filp)
0797 {
0798 #if defined(CONFIG_MIPS_VPE_LOADER_MT) || defined(CONFIG_MIPS_VPE_LOADER_CMP)
0799     struct vpe *v;
0800     Elf_Ehdr *hdr;
0801     int ret = 0;
0802 
0803     v = get_vpe(aprp_cpu_index());
0804     if (v == NULL)
0805         return -ENODEV;
0806 
0807     hdr = (Elf_Ehdr *) v->pbuffer;
0808     if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) == 0) {
0809         if (vpe_elfload(v) >= 0) {
0810             vpe_run(v);
0811         } else {
0812             pr_warn("VPE loader: ELF load failed.\n");
0813             ret = -ENOEXEC;
0814         }
0815     } else {
0816         pr_warn("VPE loader: only elf files are supported\n");
0817         ret = -ENOEXEC;
0818     }
0819 
0820     /* It's good to be able to run the SP and if it chokes have a look at
0821        the /dev/rt?. But if we reset the pointer to the shared struct we
0822        lose what has happened. So perhaps if garbage is sent to the vpe
0823        device, use it as a trigger for the reset. Hopefully a nice
0824        executable will be along shortly. */
0825     if (ret < 0)
0826         v->shared_ptr = NULL;
0827 
0828     vfree(v->pbuffer);
0829     v->plen = 0;
0830 
0831     return ret;
0832 #else
0833     pr_warn("VPE loader: ELF load failed.\n");
0834     return -ENOEXEC;
0835 #endif
0836 }
0837 
0838 static ssize_t vpe_write(struct file *file, const char __user *buffer,
0839              size_t count, loff_t *ppos)
0840 {
0841     size_t ret = count;
0842     struct vpe *v;
0843 
0844     if (iminor(file_inode(file)) != VPE_MODULE_MINOR)
0845         return -ENODEV;
0846 
0847     v = get_vpe(aprp_cpu_index());
0848 
0849     if (v == NULL)
0850         return -ENODEV;
0851 
0852     if ((count + v->len) > v->plen) {
0853         pr_warn("VPE loader: elf size too big. Perhaps strip unneeded symbols\n");
0854         return -ENOMEM;
0855     }
0856 
0857     count -= copy_from_user(v->pbuffer + v->len, buffer, count);
0858     if (!count)
0859         return -EFAULT;
0860 
0861     v->len += count;
0862     return ret;
0863 }
0864 
0865 const struct file_operations vpe_fops = {
0866     .owner = THIS_MODULE,
0867     .open = vpe_open,
0868     .release = vpe_release,
0869     .write = vpe_write,
0870     .llseek = noop_llseek,
0871 };
0872 
0873 void *vpe_get_shared(int index)
0874 {
0875     struct vpe *v = get_vpe(index);
0876 
0877     if (v == NULL)
0878         return NULL;
0879 
0880     return v->shared_ptr;
0881 }
0882 EXPORT_SYMBOL(vpe_get_shared);
0883 
0884 int vpe_notify(int index, struct vpe_notifications *notify)
0885 {
0886     struct vpe *v = get_vpe(index);
0887 
0888     if (v == NULL)
0889         return -1;
0890 
0891     list_add(&notify->list, &v->notify);
0892     return 0;
0893 }
0894 EXPORT_SYMBOL(vpe_notify);
0895 
0896 module_init(vpe_module_init);
0897 module_exit(vpe_module_exit);
0898 MODULE_DESCRIPTION("MIPS VPE Loader");
0899 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
0900 MODULE_LICENSE("GPL");