2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
7 * Copyright (C) 2013 Imagination Technologies Ltd.
9 * VPE support module for loading a MIPS SP program into VPE1. The SP
10 * environment is rather simple since there are no TLBs. It needs
11 * to be relocatable (or partially linked). Initialize your stack in
12 * the startup-code. The loader looks for the symbol __start and sets
13 * up the execution to resume from there. To load and run, simply do
14 * a cat SP 'binary' to the /dev/vpe1 device.
16 #include <linux/kernel.h>
17 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/vmalloc.h>
23 #include <linux/elf.h>
24 #include <linux/seq_file.h>
25 #include <linux/syscalls.h>
26 #include <linux/moduleloader.h>
27 #include <linux/interrupt.h>
28 #include <linux/poll.h>
29 #include <linux/memblock.h>
30 #include <asm/mipsregs.h>
31 #include <asm/mipsmtregs.h>
32 #include <asm/cacheflush.h>
33 #include <linux/atomic.h>
34 #include <asm/mips_mt.h>
35 #include <asm/processor.h>
38 #ifndef ARCH_SHF_SMALL
39 #define ARCH_SHF_SMALL 0
42 /* If this is set, the section belongs in the init part of the module */
43 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
45 struct vpe_control vpecontrol = {
46 .vpe_list_lock = __SPIN_LOCK_UNLOCKED(vpe_list_lock),
47 .vpe_list = LIST_HEAD_INIT(vpecontrol.vpe_list),
48 .tc_list_lock = __SPIN_LOCK_UNLOCKED(tc_list_lock),
49 .tc_list = LIST_HEAD_INIT(vpecontrol.tc_list)
52 /* get the vpe associated with this minor */
53 struct vpe *get_vpe(int minor)
61 spin_lock(&vpecontrol.vpe_list_lock);
62 list_for_each_entry(v, &vpecontrol.vpe_list, list) {
63 if (v->minor == VPE_MODULE_MINOR) {
68 spin_unlock(&vpecontrol.vpe_list_lock);
73 /* get the vpe associated with this minor */
74 struct tc *get_tc(int index)
79 spin_lock(&vpecontrol.tc_list_lock);
80 list_for_each_entry(t, &vpecontrol.tc_list, list) {
81 if (t->index == index) {
86 spin_unlock(&vpecontrol.tc_list_lock);
91 /* allocate a vpe and associate it with this minor (or index) */
92 struct vpe *alloc_vpe(int minor)
96 v = kzalloc(sizeof(struct vpe), GFP_KERNEL);
100 INIT_LIST_HEAD(&v->tc);
101 spin_lock(&vpecontrol.vpe_list_lock);
102 list_add_tail(&v->list, &vpecontrol.vpe_list);
103 spin_unlock(&vpecontrol.vpe_list_lock);
105 INIT_LIST_HEAD(&v->notify);
106 v->minor = VPE_MODULE_MINOR;
112 /* allocate a tc. At startup only tc0 is running, all other can be halted. */
113 struct tc *alloc_tc(int index)
117 tc = kzalloc(sizeof(struct tc), GFP_KERNEL);
121 INIT_LIST_HEAD(&tc->tc);
124 spin_lock(&vpecontrol.tc_list_lock);
125 list_add_tail(&tc->list, &vpecontrol.tc_list);
126 spin_unlock(&vpecontrol.tc_list_lock);
132 /* clean up and free everything */
133 void release_vpe(struct vpe *v)
137 release_progmem(v->load_addr);
141 /* Find some VPE program space */
142 void *alloc_progmem(unsigned long len)
146 #ifdef CONFIG_MIPS_VPE_LOADER_TOM
148 * This means you must tell Linux to use less memory than you
149 * physically have, for example by passing a mem= boot argument.
151 addr = pfn_to_kaddr(max_low_pfn);
152 memset(addr, 0, len);
154 /* simple grab some mem for now */
155 addr = kzalloc(len, GFP_KERNEL);
161 void release_progmem(void *ptr)
163 #ifndef CONFIG_MIPS_VPE_LOADER_TOM
168 /* Update size with this section: return offset. */
169 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
173 ret = ALIGN(*size, sechdr->sh_addralign ? : 1);
174 *size = ret + sechdr->sh_size;
178 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
179 might -- code, read-only data, read-write data, small data. Tally
180 sizes, and place the offsets into sh_entsize fields: high bit means it
182 static void layout_sections(struct module *mod, const Elf_Ehdr *hdr,
183 Elf_Shdr *sechdrs, const char *secstrings)
185 static unsigned long const masks[][2] = {
186 /* NOTE: all executable code must be the first section
187 * in this array; otherwise modify the text_size
188 * finder in the two loops below */
189 {SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL},
190 {SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL},
191 {SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL},
192 {ARCH_SHF_SMALL | SHF_ALLOC, 0}
196 for (i = 0; i < hdr->e_shnum; i++)
197 sechdrs[i].sh_entsize = ~0UL;
199 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
200 for (i = 0; i < hdr->e_shnum; ++i) {
201 Elf_Shdr *s = &sechdrs[i];
202 struct module_memory *mod_mem;
204 mod_mem = &mod->mem[MOD_TEXT];
206 if ((s->sh_flags & masks[m][0]) != masks[m][0]
207 || (s->sh_flags & masks[m][1])
208 || s->sh_entsize != ~0UL)
211 get_offset((unsigned long *)&mod_mem->size, s);
216 /* from module-elf32.c, but subverted a little */
219 struct mips_hi16 *next;
224 static struct mips_hi16 *mips_hi16_list;
225 static unsigned int gp_offs, gp_addr;
227 static int apply_r_mips_none(struct module *me, uint32_t *location,
233 static int apply_r_mips_gprel16(struct module *me, uint32_t *location,
238 if (!(*location & 0xffff)) {
239 rel = (int)v - gp_addr;
241 /* .sbss + gp(relative) + offset */
243 rel = (int)(short)((int)v + gp_offs +
244 (int)(short)(*location & 0xffff) - gp_addr);
247 if ((rel > 32768) || (rel < -32768)) {
248 pr_debug("VPE loader: apply_r_mips_gprel16: relative address 0x%x out of range of gp register\n",
253 *location = (*location & 0xffff0000) | (rel & 0xffff);
258 static int apply_r_mips_pc16(struct module *me, uint32_t *location,
262 rel = (((unsigned int)v - (unsigned int)location));
263 rel >>= 2; /* because the offset is in _instructions_ not bytes. */
264 rel -= 1; /* and one instruction less due to the branch delay slot. */
266 if ((rel > 32768) || (rel < -32768)) {
267 pr_debug("VPE loader: apply_r_mips_pc16: relative address out of range 0x%x\n",
272 *location = (*location & 0xffff0000) | (rel & 0xffff);
277 static int apply_r_mips_32(struct module *me, uint32_t *location,
285 static int apply_r_mips_26(struct module *me, uint32_t *location,
289 pr_debug("VPE loader: apply_r_mips_26: unaligned relocation\n");
294 * Not desperately convinced this is a good check of an overflow condition
295 * anyway. But it gets in the way of handling undefined weak symbols which
296 * we want to set to zero.
297 * if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
299 * "module %s: relocation overflow\n",
305 *location = (*location & ~0x03ffffff) |
306 ((*location + (v >> 2)) & 0x03ffffff);
310 static int apply_r_mips_hi16(struct module *me, uint32_t *location,
316 * We cannot relocate this one now because we don't know the value of
317 * the carry we need to add. Save the information, and let LO16 do the
320 n = kmalloc(sizeof(*n), GFP_KERNEL);
326 n->next = mips_hi16_list;
332 static int apply_r_mips_lo16(struct module *me, uint32_t *location,
335 unsigned long insnlo = *location;
336 Elf32_Addr val, vallo;
337 struct mips_hi16 *l, *next;
339 /* Sign extend the addend we extract from the lo insn. */
340 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
342 if (mips_hi16_list != NULL) {
349 * The value for the HI16 had best be the same.
352 pr_debug("VPE loader: apply_r_mips_lo16/hi16: inconsistent value information\n");
357 * Do the HI16 relocation. Note that we actually don't
358 * need to know anything about the LO16 itself, except
359 * where to find the low 16 bits of the addend needed
363 val = ((insn & 0xffff) << 16) + vallo;
367 * Account for the sign extension that will happen in
370 val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
372 insn = (insn & ~0xffff) | val;
380 mips_hi16_list = NULL;
384 * Ok, we're done with the HI16 relocs. Now deal with the LO16.
387 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
398 mips_hi16_list = NULL;
403 static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
405 [R_MIPS_NONE] = apply_r_mips_none,
406 [R_MIPS_32] = apply_r_mips_32,
407 [R_MIPS_26] = apply_r_mips_26,
408 [R_MIPS_HI16] = apply_r_mips_hi16,
409 [R_MIPS_LO16] = apply_r_mips_lo16,
410 [R_MIPS_GPREL16] = apply_r_mips_gprel16,
411 [R_MIPS_PC16] = apply_r_mips_pc16
414 static char *rstrs[] = {
415 [R_MIPS_NONE] = "MIPS_NONE",
416 [R_MIPS_32] = "MIPS_32",
417 [R_MIPS_26] = "MIPS_26",
418 [R_MIPS_HI16] = "MIPS_HI16",
419 [R_MIPS_LO16] = "MIPS_LO16",
420 [R_MIPS_GPREL16] = "MIPS_GPREL16",
421 [R_MIPS_PC16] = "MIPS_PC16"
424 static int apply_relocations(Elf32_Shdr *sechdrs,
426 unsigned int symindex,
430 Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
437 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
438 Elf32_Word r_info = rel[i].r_info;
440 /* This is where to make the change */
441 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
443 /* This is the symbol it is referring to */
444 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
445 + ELF32_R_SYM(r_info);
447 if (!sym->st_value) {
448 pr_debug("%s: undefined weak symbol %s\n",
449 me->name, strtab + sym->st_name);
450 /* just print the warning, dont barf */
455 res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
457 char *r = rstrs[ELF32_R_TYPE(r_info)];
458 pr_warn("VPE loader: .text+0x%x relocation type %s for symbol \"%s\" failed\n",
459 rel[i].r_offset, r ? r : "UNKNOWN",
460 strtab + sym->st_name);
468 static inline void save_gp_address(unsigned int secbase, unsigned int rel)
470 gp_addr = secbase + rel;
471 gp_offs = gp_addr - (secbase & 0xffff0000);
473 /* end module-elf32.c */
475 /* Change all symbols so that sh_value encodes the pointer directly. */
476 static void simplify_symbols(Elf_Shdr *sechdrs,
477 unsigned int symindex,
479 const char *secstrings,
480 unsigned int nsecs, struct module *mod)
482 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
483 unsigned long secbase, bssbase = 0;
484 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
487 /* find the .bss section for COMMON symbols */
488 for (i = 0; i < nsecs; i++) {
489 if (strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) == 0) {
490 bssbase = sechdrs[i].sh_addr;
495 for (i = 1; i < n; i++) {
496 switch (sym[i].st_shndx) {
498 /* Allocate space for the symbol in the .bss section.
499 st_value is currently size.
500 We want it to have the address of the symbol. */
502 size = sym[i].st_value;
503 sym[i].st_value = bssbase;
509 /* Don't need to do anything */
516 case SHN_MIPS_SCOMMON:
517 pr_debug("simplify_symbols: ignoring SHN_MIPS_SCOMMON symbol <%s> st_shndx %d\n",
518 strtab + sym[i].st_name, sym[i].st_shndx);
523 secbase = sechdrs[sym[i].st_shndx].sh_addr;
525 if (strncmp(strtab + sym[i].st_name, "_gp", 3) == 0)
526 save_gp_address(secbase, sym[i].st_value);
528 sym[i].st_value += secbase;
534 #ifdef DEBUG_ELFLOADER
535 static void dump_elfsymbols(Elf_Shdr *sechdrs, unsigned int symindex,
536 const char *strtab, struct module *mod)
538 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
539 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
541 pr_debug("dump_elfsymbols: n %d\n", n);
542 for (i = 1; i < n; i++) {
543 pr_debug(" i %d name <%s> 0x%x\n", i, strtab + sym[i].st_name,
549 static int find_vpe_symbols(struct vpe *v, Elf_Shdr *sechdrs,
550 unsigned int symindex, const char *strtab,
553 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
554 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
556 for (i = 1; i < n; i++) {
557 if (strcmp(strtab + sym[i].st_name, "__start") == 0)
558 v->__start = sym[i].st_value;
560 if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0)
561 v->shared_ptr = (void *)sym[i].st_value;
564 if ((v->__start == 0) || (v->shared_ptr == NULL))
571 * Allocates a VPE with some program code space(the load address), copies the
572 * contents of the program (p)buffer performing relocatations/etc, free's it
575 static int vpe_elfload(struct vpe *v)
580 char *secstrings, *strtab = NULL;
581 unsigned int len, i, symindex = 0, strindex = 0, relocate = 0;
582 struct module mod; /* so we can re-use the relocations code */
584 memset(&mod, 0, sizeof(struct module));
585 strcpy(mod.name, "VPE loader");
587 hdr = (Elf_Ehdr *) v->pbuffer;
590 /* Sanity checks against insmoding binaries or wrong arch,
592 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
593 || (hdr->e_type != ET_REL && hdr->e_type != ET_EXEC)
594 || !elf_check_arch(hdr)
595 || hdr->e_shentsize != sizeof(*sechdrs)) {
596 pr_warn("VPE loader: program wrong arch or weird elf version\n");
601 if (hdr->e_type == ET_REL)
604 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
605 pr_err("VPE loader: program length %u truncated\n", len);
610 /* Convenience variables */
611 sechdrs = (void *)hdr + hdr->e_shoff;
612 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
613 sechdrs[0].sh_addr = 0;
615 /* And these should exist, but gcc whinges if we don't init them */
616 symindex = strindex = 0;
619 for (i = 1; i < hdr->e_shnum; i++) {
620 if ((sechdrs[i].sh_type != SHT_NOBITS) &&
621 (len < sechdrs[i].sh_offset + sechdrs[i].sh_size)) {
622 pr_err("VPE program length %u truncated\n",
627 /* Mark all sections sh_addr with their address in the
629 sechdrs[i].sh_addr = (size_t) hdr +
630 sechdrs[i].sh_offset;
632 /* Internal symbols and strings. */
633 if (sechdrs[i].sh_type == SHT_SYMTAB) {
635 strindex = sechdrs[i].sh_link;
636 strtab = (char *)hdr +
637 sechdrs[strindex].sh_offset;
640 layout_sections(&mod, hdr, sechdrs, secstrings);
643 v->load_addr = alloc_progmem(mod.mem[MOD_TEXT].size);
647 pr_info("VPE loader: loading to %p\n", v->load_addr);
650 for (i = 0; i < hdr->e_shnum; i++) {
653 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
656 dest = v->load_addr + sechdrs[i].sh_entsize;
658 if (sechdrs[i].sh_type != SHT_NOBITS)
659 memcpy(dest, (void *)sechdrs[i].sh_addr,
661 /* Update sh_addr to point to copy in image. */
662 sechdrs[i].sh_addr = (unsigned long)dest;
664 pr_debug(" section sh_name %s sh_addr 0x%x\n",
665 secstrings + sechdrs[i].sh_name,
669 /* Fix up syms, so that st_value is a pointer to location. */
670 simplify_symbols(sechdrs, symindex, strtab, secstrings,
673 /* Now do relocations. */
674 for (i = 1; i < hdr->e_shnum; i++) {
675 const char *strtab = (char *)sechdrs[strindex].sh_addr;
676 unsigned int info = sechdrs[i].sh_info;
678 /* Not a valid relocation section? */
679 if (info >= hdr->e_shnum)
682 /* Don't bother with non-allocated sections */
683 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
686 if (sechdrs[i].sh_type == SHT_REL)
687 err = apply_relocations(sechdrs, strtab,
689 else if (sechdrs[i].sh_type == SHT_RELA)
690 err = apply_relocate_add(sechdrs, strtab,
697 struct elf_phdr *phdr = (struct elf_phdr *)
698 ((char *)hdr + hdr->e_phoff);
700 for (i = 0; i < hdr->e_phnum; i++) {
701 if (phdr->p_type == PT_LOAD) {
702 memcpy((void *)phdr->p_paddr,
703 (char *)hdr + phdr->p_offset,
705 memset((void *)phdr->p_paddr + phdr->p_filesz,
706 0, phdr->p_memsz - phdr->p_filesz);
711 for (i = 0; i < hdr->e_shnum; i++) {
712 /* Internal symbols and strings. */
713 if (sechdrs[i].sh_type == SHT_SYMTAB) {
715 strindex = sechdrs[i].sh_link;
716 strtab = (char *)hdr +
717 sechdrs[strindex].sh_offset;
720 * mark symtab's address for when we try
721 * to find the magic symbols
723 sechdrs[i].sh_addr = (size_t) hdr +
724 sechdrs[i].sh_offset;
729 /* make sure it's physically written out */
730 flush_icache_range((unsigned long)v->load_addr,
731 (unsigned long)v->load_addr + v->len);
733 if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
734 if (v->__start == 0) {
735 pr_warn("VPE loader: program does not contain a __start symbol\n");
739 if (v->shared_ptr == NULL)
740 pr_warn("VPE loader: program does not contain vpe_shared symbol.\n"
741 " Unable to use AMVP (AP/SP) facilities.\n");
744 pr_info(" elf loaded\n");
748 /* checks VPE is unused and gets ready to load program */
749 static int vpe_open(struct inode *inode, struct file *filp)
751 enum vpe_state state;
752 struct vpe_notifications *notifier;
755 if (VPE_MODULE_MINOR != iminor(inode)) {
756 /* assume only 1 device at the moment. */
757 pr_warn("VPE loader: only vpe1 is supported\n");
762 v = get_vpe(aprp_cpu_index());
764 pr_warn("VPE loader: unable to get vpe\n");
769 state = xchg(&v->state, VPE_STATE_INUSE);
770 if (state != VPE_STATE_UNUSED) {
771 pr_debug("VPE loader: tc in use dumping regs\n");
773 list_for_each_entry(notifier, &v->notify, list)
774 notifier->stop(aprp_cpu_index());
776 release_progmem(v->load_addr);
777 cleanup_tc(get_tc(aprp_cpu_index()));
780 /* this of-course trashes what was there before... */
781 v->pbuffer = vmalloc(P_SIZE);
783 pr_warn("VPE loader: unable to allocate memory\n");
789 v->shared_ptr = NULL;
795 static int vpe_release(struct inode *inode, struct file *filp)
797 #ifdef CONFIG_MIPS_VPE_LOADER_MT
802 v = get_vpe(aprp_cpu_index());
806 hdr = (Elf_Ehdr *) v->pbuffer;
807 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) == 0) {
808 if (vpe_elfload(v) >= 0) {
811 pr_warn("VPE loader: ELF load failed.\n");
815 pr_warn("VPE loader: only elf files are supported\n");
819 /* It's good to be able to run the SP and if it chokes have a look at
820 the /dev/rt?. But if we reset the pointer to the shared struct we
821 lose what has happened. So perhaps if garbage is sent to the vpe
822 device, use it as a trigger for the reset. Hopefully a nice
823 executable will be along shortly. */
825 v->shared_ptr = NULL;
832 pr_warn("VPE loader: ELF load failed.\n");
837 static ssize_t vpe_write(struct file *file, const char __user *buffer,
838 size_t count, loff_t *ppos)
843 if (iminor(file_inode(file)) != VPE_MODULE_MINOR)
846 v = get_vpe(aprp_cpu_index());
851 if ((count + v->len) > v->plen) {
852 pr_warn("VPE loader: elf size too big. Perhaps strip unneeded symbols\n");
856 count -= copy_from_user(v->pbuffer + v->len, buffer, count);
864 const struct file_operations vpe_fops = {
865 .owner = THIS_MODULE,
867 .release = vpe_release,
869 .llseek = noop_llseek,
872 void *vpe_get_shared(int index)
874 struct vpe *v = get_vpe(index);
879 return v->shared_ptr;
881 EXPORT_SYMBOL(vpe_get_shared);
883 int vpe_notify(int index, struct vpe_notifications *notify)
885 struct vpe *v = get_vpe(index);
890 list_add(¬ify->list, &v->notify);
893 EXPORT_SYMBOL(vpe_notify);
895 module_init(vpe_module_init);
896 module_exit(vpe_module_exit);
897 MODULE_DESCRIPTION("MIPS VPE Loader");
898 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
899 MODULE_LICENSE("GPL");