2005-07-14 09:57:16 -06:00
|
|
|
/*
|
2014-01-01 08:35:32 -07:00
|
|
|
* This file is subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file "COPYING" in the main directory of this archive
|
|
|
|
* for more details.
|
2005-07-14 09:57:16 -06:00
|
|
|
*
|
2014-01-01 08:35:32 -07:00
|
|
|
* Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
|
|
|
|
* Copyright (C) 2013 Imagination Technologies Ltd.
|
2005-07-14 09:57:16 -06:00
|
|
|
*
|
2014-01-01 08:35:32 -07:00
|
|
|
* VPE spport module for loading a MIPS SP program into VPE1. The SP
|
|
|
|
* environment is rather simple since there are no TLBs. It needs
|
|
|
|
* to be relocatable (or partiall linked). Initialize your stack in
|
|
|
|
* the startup-code. The loader looks for the symbol __start and sets
|
|
|
|
* up the execution to resume from there. To load and run, simply do
|
|
|
|
* a cat SP 'binary' to the /dev/vpe1 device.
|
2005-07-14 09:57:16 -06:00
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
2007-02-07 06:48:59 -07:00
|
|
|
#include <linux/device.h>
|
2005-07-14 09:57:16 -06:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/elf.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/moduleloader.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/bootmem.h>
|
|
|
|
#include <asm/mipsregs.h>
|
2005-08-17 11:44:08 -06:00
|
|
|
#include <asm/mipsmtregs.h>
|
2005-07-14 09:57:16 -06:00
|
|
|
#include <asm/cacheflush.h>
|
2011-07-26 17:09:06 -06:00
|
|
|
#include <linux/atomic.h>
|
2007-02-07 06:48:59 -07:00
|
|
|
#include <asm/mips_mt.h>
|
2005-07-14 09:57:16 -06:00
|
|
|
#include <asm/processor.h>
|
2006-04-05 02:45:45 -06:00
|
|
|
#include <asm/vpe.h>
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
#ifndef ARCH_SHF_SMALL
|
|
|
|
#define ARCH_SHF_SMALL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If this is set, the section belongs in the init part of the module */
|
|
|
|
#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
|
|
|
|
|
2013-10-30 14:52:06 -06:00
|
|
|
struct vpe_control vpecontrol = {
|
2011-01-23 08:17:00 -07:00
|
|
|
.vpe_list_lock = __SPIN_LOCK_UNLOCKED(vpe_list_lock),
|
2009-09-28 17:52:27 -06:00
|
|
|
.vpe_list = LIST_HEAD_INIT(vpecontrol.vpe_list),
|
2011-01-23 08:17:00 -07:00
|
|
|
.tc_list_lock = __SPIN_LOCK_UNLOCKED(tc_list_lock),
|
2009-09-28 17:52:27 -06:00
|
|
|
.tc_list = LIST_HEAD_INIT(vpecontrol.tc_list)
|
2007-01-24 12:13:08 -07:00
|
|
|
};
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
/* get the vpe associated with this minor */
|
2013-10-30 14:52:06 -06:00
|
|
|
struct vpe *get_vpe(int minor)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
2009-09-28 17:52:27 -06:00
|
|
|
struct vpe *res, *v;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
if (!cpu_has_mipsmt)
|
|
|
|
return NULL;
|
|
|
|
|
2009-09-28 17:52:27 -06:00
|
|
|
res = NULL;
|
|
|
|
spin_lock(&vpecontrol.vpe_list_lock);
|
2005-07-14 09:57:16 -06:00
|
|
|
list_for_each_entry(v, &vpecontrol.vpe_list, list) {
|
2013-10-30 14:52:06 -06:00
|
|
|
if (v->minor == VPE_MODULE_MINOR) {
|
2009-09-28 17:52:27 -06:00
|
|
|
res = v;
|
|
|
|
break;
|
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
2009-09-28 17:52:27 -06:00
|
|
|
spin_unlock(&vpecontrol.vpe_list_lock);
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2009-09-28 17:52:27 -06:00
|
|
|
return res;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the vpe associated with this minor */
|
2013-10-30 14:52:06 -06:00
|
|
|
struct tc *get_tc(int index)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
2009-09-28 17:52:27 -06:00
|
|
|
struct tc *res, *t;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2009-09-28 17:52:27 -06:00
|
|
|
res = NULL;
|
|
|
|
spin_lock(&vpecontrol.tc_list_lock);
|
2005-07-14 09:57:16 -06:00
|
|
|
list_for_each_entry(t, &vpecontrol.tc_list, list) {
|
2009-09-28 17:52:27 -06:00
|
|
|
if (t->index == index) {
|
|
|
|
res = t;
|
|
|
|
break;
|
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
2009-09-28 17:52:27 -06:00
|
|
|
spin_unlock(&vpecontrol.tc_list_lock);
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2011-08-04 08:38:31 -06:00
|
|
|
return res;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a vpe and associate it with this minor (or index) */
|
2013-10-30 14:52:06 -06:00
|
|
|
struct vpe *alloc_vpe(int minor)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
|
|
|
struct vpe *v;
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
v = kzalloc(sizeof(struct vpe), GFP_KERNEL);
|
|
|
|
if (v == NULL)
|
|
|
|
goto out;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
INIT_LIST_HEAD(&v->tc);
|
2009-09-28 17:52:27 -06:00
|
|
|
spin_lock(&vpecontrol.vpe_list_lock);
|
2005-07-14 09:57:16 -06:00
|
|
|
list_add_tail(&v->list, &vpecontrol.vpe_list);
|
2009-09-28 17:52:27 -06:00
|
|
|
spin_unlock(&vpecontrol.vpe_list_lock);
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
INIT_LIST_HEAD(&v->notify);
|
2013-10-30 14:52:06 -06:00
|
|
|
v->minor = VPE_MODULE_MINOR;
|
2009-09-28 17:52:27 -06:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
out:
|
2005-07-14 09:57:16 -06:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a tc. At startup only tc0 is running, all other can be halted. */
|
2013-10-30 14:52:06 -06:00
|
|
|
struct tc *alloc_tc(int index)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
2007-07-27 12:31:10 -06:00
|
|
|
struct tc *tc;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
tc = kzalloc(sizeof(struct tc), GFP_KERNEL);
|
|
|
|
if (tc == NULL)
|
2007-07-27 12:31:10 -06:00
|
|
|
goto out;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2007-07-27 12:31:10 -06:00
|
|
|
INIT_LIST_HEAD(&tc->tc);
|
|
|
|
tc->index = index;
|
2009-09-28 17:52:27 -06:00
|
|
|
|
|
|
|
spin_lock(&vpecontrol.tc_list_lock);
|
2007-07-27 12:31:10 -06:00
|
|
|
list_add_tail(&tc->list, &vpecontrol.tc_list);
|
2009-09-28 17:52:27 -06:00
|
|
|
spin_unlock(&vpecontrol.tc_list_lock);
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2007-07-27 12:31:10 -06:00
|
|
|
out:
|
|
|
|
return tc;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up and free everything */
|
2013-10-30 14:52:06 -06:00
|
|
|
void release_vpe(struct vpe *v)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
|
|
|
list_del(&v->list);
|
|
|
|
if (v->load_addr)
|
|
|
|
release_progmem(v);
|
|
|
|
kfree(v);
|
|
|
|
}
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
/* Find some VPE program space */
|
2013-10-30 14:52:06 -06:00
|
|
|
void *alloc_progmem(unsigned long len)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
2008-03-13 09:16:53 -06:00
|
|
|
void *addr;
|
|
|
|
|
2005-07-14 09:57:16 -06:00
|
|
|
#ifdef CONFIG_MIPS_VPE_LOADER_TOM
|
2008-03-13 09:16:53 -06:00
|
|
|
/*
|
|
|
|
* This means you must tell Linux to use less memory than you
|
|
|
|
* physically have, for example by passing a mem= boot argument.
|
|
|
|
*/
|
2008-04-17 06:42:50 -06:00
|
|
|
addr = pfn_to_kaddr(max_low_pfn);
|
2008-03-13 09:16:53 -06:00
|
|
|
memset(addr, 0, len);
|
2005-07-14 09:57:16 -06:00
|
|
|
#else
|
2008-03-13 09:16:53 -06:00
|
|
|
/* simple grab some mem for now */
|
|
|
|
addr = kzalloc(len, GFP_KERNEL);
|
2005-07-14 09:57:16 -06:00
|
|
|
#endif
|
2008-03-13 09:16:53 -06:00
|
|
|
|
|
|
|
return addr;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
2013-10-30 14:52:06 -06:00
|
|
|
void release_progmem(void *ptr)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_MIPS_VPE_LOADER_TOM
|
|
|
|
kfree(ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update size with this section: return offset. */
|
2014-01-01 08:35:32 -07:00
|
|
|
static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
ret = ALIGN(*size, sechdr->sh_addralign ? : 1);
|
|
|
|
*size = ret + sechdr->sh_size;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
|
2013-01-22 04:59:30 -07:00
|
|
|
might -- code, read-only data, read-write data, small data. Tally
|
2005-07-14 09:57:16 -06:00
|
|
|
sizes, and place the offsets into sh_entsize fields: high bit means it
|
|
|
|
belongs in init. */
|
2014-01-01 08:35:32 -07:00
|
|
|
static void layout_sections(struct module *mod, const Elf_Ehdr *hdr,
|
|
|
|
Elf_Shdr *sechdrs, const char *secstrings)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
|
|
|
static unsigned long const masks[][2] = {
|
|
|
|
/* NOTE: all executable code must be the first section
|
|
|
|
* in this array; otherwise modify the text_size
|
|
|
|
* finder in the two loops below */
|
|
|
|
{SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL},
|
|
|
|
{SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL},
|
|
|
|
{SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL},
|
|
|
|
{ARCH_SHF_SMALL | SHF_ALLOC, 0}
|
|
|
|
};
|
|
|
|
unsigned int m, i;
|
|
|
|
|
|
|
|
for (i = 0; i < hdr->e_shnum; i++)
|
|
|
|
sechdrs[i].sh_entsize = ~0UL;
|
|
|
|
|
|
|
|
for (m = 0; m < ARRAY_SIZE(masks); ++m) {
|
|
|
|
for (i = 0; i < hdr->e_shnum; ++i) {
|
|
|
|
Elf_Shdr *s = &sechdrs[i];
|
|
|
|
|
|
|
|
if ((s->sh_flags & masks[m][0]) != masks[m][0]
|
|
|
|
|| (s->sh_flags & masks[m][1])
|
|
|
|
|| s->sh_entsize != ~0UL)
|
|
|
|
continue;
|
2009-07-10 03:01:32 -06:00
|
|
|
s->sh_entsize =
|
2015-11-25 16:14:08 -07:00
|
|
|
get_offset((unsigned long *)&mod->core_layout.size, s);
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m == 0)
|
2015-11-25 16:14:08 -07:00
|
|
|
mod->core_layout.text_size = mod->core_layout.size;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* from module-elf32.c, but subverted a little */
|
|
|
|
|
|
|
|
struct mips_hi16 {
|
|
|
|
struct mips_hi16 *next;
|
|
|
|
Elf32_Addr *addr;
|
|
|
|
Elf32_Addr value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mips_hi16 *mips_hi16_list;
|
|
|
|
static unsigned int gp_offs, gp_addr;
|
|
|
|
|
|
|
|
static int apply_r_mips_none(struct module *me, uint32_t *location,
|
|
|
|
Elf32_Addr v)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int apply_r_mips_gprel16(struct module *me, uint32_t *location,
|
|
|
|
Elf32_Addr v)
|
|
|
|
{
|
|
|
|
int rel;
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if (!(*location & 0xffff)) {
|
2005-07-14 09:57:16 -06:00
|
|
|
rel = (int)v - gp_addr;
|
2014-01-01 08:35:32 -07:00
|
|
|
} else {
|
2005-07-14 09:57:16 -06:00
|
|
|
/* .sbss + gp(relative) + offset */
|
|
|
|
/* kludge! */
|
|
|
|
rel = (int)(short)((int)v + gp_offs +
|
|
|
|
(int)(short)(*location & 0xffff) - gp_addr);
|
|
|
|
}
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if ((rel > 32768) || (rel < -32768)) {
|
|
|
|
pr_debug("VPE loader: apply_r_mips_gprel16: relative address 0x%x out of range of gp register\n",
|
|
|
|
rel);
|
2005-07-14 09:57:16 -06:00
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
*location = (*location & 0xffff0000) | (rel & 0xffff);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int apply_r_mips_pc16(struct module *me, uint32_t *location,
|
|
|
|
Elf32_Addr v)
|
|
|
|
{
|
|
|
|
int rel;
|
|
|
|
rel = (((unsigned int)v - (unsigned int)location));
|
2014-01-01 08:35:32 -07:00
|
|
|
rel >>= 2; /* because the offset is in _instructions_ not bytes. */
|
|
|
|
rel -= 1; /* and one instruction less due to the branch delay slot. */
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if ((rel > 32768) || (rel < -32768)) {
|
|
|
|
pr_debug("VPE loader: apply_r_mips_pc16: relative address out of range 0x%x\n",
|
|
|
|
rel);
|
2005-07-14 09:57:16 -06:00
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
*location = (*location & 0xffff0000) | (rel & 0xffff);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int apply_r_mips_32(struct module *me, uint32_t *location,
|
|
|
|
Elf32_Addr v)
|
|
|
|
{
|
|
|
|
*location += v;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int apply_r_mips_26(struct module *me, uint32_t *location,
|
|
|
|
Elf32_Addr v)
|
|
|
|
{
|
|
|
|
if (v % 4) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_debug("VPE loader: apply_r_mips_26: unaligned relocation\n");
|
2005-07-14 09:57:16 -06:00
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
2005-10-31 16:34:52 -07:00
|
|
|
/*
|
|
|
|
* Not desperately convinced this is a good check of an overflow condition
|
|
|
|
* anyway. But it gets in the way of handling undefined weak symbols which
|
|
|
|
* we want to set to zero.
|
|
|
|
* if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
|
|
|
|
* printk(KERN_ERR
|
|
|
|
* "module %s: relocation overflow\n",
|
|
|
|
* me->name);
|
|
|
|
* return -ENOEXEC;
|
|
|
|
* }
|
|
|
|
*/
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
*location = (*location & ~0x03ffffff) |
|
|
|
|
((*location + (v >> 2)) & 0x03ffffff);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int apply_r_mips_hi16(struct module *me, uint32_t *location,
|
|
|
|
Elf32_Addr v)
|
|
|
|
{
|
|
|
|
struct mips_hi16 *n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We cannot relocate this one now because we don't know the value of
|
|
|
|
* the carry we need to add. Save the information, and let LO16 do the
|
|
|
|
* actual relocation.
|
|
|
|
*/
|
2014-01-01 08:35:32 -07:00
|
|
|
n = kmalloc(sizeof(*n), GFP_KERNEL);
|
2005-07-14 09:57:16 -06:00
|
|
|
if (!n)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
n->addr = location;
|
|
|
|
n->value = v;
|
|
|
|
n->next = mips_hi16_list;
|
|
|
|
mips_hi16_list = n;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int apply_r_mips_lo16(struct module *me, uint32_t *location,
|
|
|
|
Elf32_Addr v)
|
|
|
|
{
|
|
|
|
unsigned long insnlo = *location;
|
|
|
|
Elf32_Addr val, vallo;
|
2009-08-03 05:26:40 -06:00
|
|
|
struct mips_hi16 *l, *next;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2013-01-22 04:59:30 -07:00
|
|
|
/* Sign extend the addend we extract from the lo insn. */
|
2005-07-14 09:57:16 -06:00
|
|
|
vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
|
|
|
|
|
|
|
|
if (mips_hi16_list != NULL) {
|
|
|
|
|
|
|
|
l = mips_hi16_list;
|
|
|
|
while (l != NULL) {
|
|
|
|
unsigned long insn;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The value for the HI16 had best be the same.
|
|
|
|
*/
|
2013-01-22 04:59:30 -07:00
|
|
|
if (v != l->value) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_debug("VPE loader: apply_r_mips_lo16/hi16: inconsistent value information\n");
|
2009-08-03 05:26:40 -06:00
|
|
|
goto out_free;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the HI16 relocation. Note that we actually don't
|
|
|
|
* need to know anything about the LO16 itself, except
|
|
|
|
* where to find the low 16 bits of the addend needed
|
|
|
|
* by the LO16.
|
|
|
|
*/
|
|
|
|
insn = *l->addr;
|
|
|
|
val = ((insn & 0xffff) << 16) + vallo;
|
|
|
|
val += v;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Account for the sign extension that will happen in
|
|
|
|
* the low bits.
|
|
|
|
*/
|
|
|
|
val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
|
|
|
|
|
|
|
|
insn = (insn & ~0xffff) | val;
|
|
|
|
*l->addr = insn;
|
|
|
|
|
|
|
|
next = l->next;
|
|
|
|
kfree(l);
|
|
|
|
l = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
mips_hi16_list = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-01-22 04:59:30 -07:00
|
|
|
* Ok, we're done with the HI16 relocs. Now deal with the LO16.
|
2005-07-14 09:57:16 -06:00
|
|
|
*/
|
|
|
|
val = v + vallo;
|
|
|
|
insnlo = (insnlo & ~0xffff) | (val & 0xffff);
|
|
|
|
*location = insnlo;
|
|
|
|
|
|
|
|
return 0;
|
2009-08-03 05:26:40 -06:00
|
|
|
|
|
|
|
out_free:
|
|
|
|
while (l != NULL) {
|
|
|
|
next = l->next;
|
|
|
|
kfree(l);
|
|
|
|
l = next;
|
|
|
|
}
|
|
|
|
mips_hi16_list = NULL;
|
|
|
|
|
|
|
|
return -ENOEXEC;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
|
|
|
|
Elf32_Addr v) = {
|
|
|
|
[R_MIPS_NONE] = apply_r_mips_none,
|
|
|
|
[R_MIPS_32] = apply_r_mips_32,
|
|
|
|
[R_MIPS_26] = apply_r_mips_26,
|
|
|
|
[R_MIPS_HI16] = apply_r_mips_hi16,
|
|
|
|
[R_MIPS_LO16] = apply_r_mips_lo16,
|
|
|
|
[R_MIPS_GPREL16] = apply_r_mips_gprel16,
|
|
|
|
[R_MIPS_PC16] = apply_r_mips_pc16
|
|
|
|
};
|
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
static char *rstrs[] = {
|
2007-02-04 17:10:11 -07:00
|
|
|
[R_MIPS_NONE] = "MIPS_NONE",
|
2006-04-05 02:45:45 -06:00
|
|
|
[R_MIPS_32] = "MIPS_32",
|
|
|
|
[R_MIPS_26] = "MIPS_26",
|
|
|
|
[R_MIPS_HI16] = "MIPS_HI16",
|
|
|
|
[R_MIPS_LO16] = "MIPS_LO16",
|
|
|
|
[R_MIPS_GPREL16] = "MIPS_GPREL16",
|
|
|
|
[R_MIPS_PC16] = "MIPS_PC16"
|
|
|
|
};
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2009-08-03 05:54:35 -06:00
|
|
|
static int apply_relocations(Elf32_Shdr *sechdrs,
|
2005-07-14 09:57:16 -06:00
|
|
|
const char *strtab,
|
|
|
|
unsigned int symindex,
|
|
|
|
unsigned int relsec,
|
|
|
|
struct module *me)
|
|
|
|
{
|
|
|
|
Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
|
|
|
|
Elf32_Sym *sym;
|
|
|
|
uint32_t *location;
|
|
|
|
unsigned int i;
|
|
|
|
Elf32_Addr v;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
|
|
|
|
Elf32_Word r_info = rel[i].r_info;
|
|
|
|
|
|
|
|
/* This is where to make the change */
|
|
|
|
location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
|
|
|
|
+ rel[i].r_offset;
|
|
|
|
/* This is the symbol it is referring to */
|
|
|
|
sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
|
|
|
|
+ ELF32_R_SYM(r_info);
|
|
|
|
|
|
|
|
if (!sym->st_value) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_debug("%s: undefined weak symbol %s\n",
|
|
|
|
me->name, strtab + sym->st_name);
|
2005-07-14 09:57:16 -06:00
|
|
|
/* just print the warning, dont barf */
|
|
|
|
}
|
|
|
|
|
|
|
|
v = sym->st_value;
|
|
|
|
|
|
|
|
res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
|
2014-01-01 08:35:32 -07:00
|
|
|
if (res) {
|
2006-04-05 02:45:45 -06:00
|
|
|
char *r = rstrs[ELF32_R_TYPE(r_info)];
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: .text+0x%x relocation type %s for symbol \"%s\" failed\n",
|
|
|
|
rel[i].r_offset, r ? r : "UNKNOWN",
|
|
|
|
strtab + sym->st_name);
|
2005-07-14 09:57:16 -06:00
|
|
|
return res;
|
2006-04-05 02:45:45 -06:00
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 05:54:35 -06:00
|
|
|
static inline void save_gp_address(unsigned int secbase, unsigned int rel)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
|
|
|
gp_addr = secbase + rel;
|
|
|
|
gp_offs = gp_addr - (secbase & 0xffff0000);
|
|
|
|
}
|
|
|
|
/* end module-elf32.c */
|
|
|
|
|
|
|
|
/* Change all symbols so that sh_value encodes the pointer directly. */
|
2014-01-01 08:35:32 -07:00
|
|
|
static void simplify_symbols(Elf_Shdr *sechdrs,
|
2005-07-14 09:57:16 -06:00
|
|
|
unsigned int symindex,
|
|
|
|
const char *strtab,
|
|
|
|
const char *secstrings,
|
|
|
|
unsigned int nsecs, struct module *mod)
|
|
|
|
{
|
|
|
|
Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
|
|
|
|
unsigned long secbase, bssbase = 0;
|
|
|
|
unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
|
2006-04-05 02:45:45 -06:00
|
|
|
int size;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
/* find the .bss section for COMMON symbols */
|
|
|
|
for (i = 0; i < nsecs; i++) {
|
2006-04-05 02:45:45 -06:00
|
|
|
if (strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) == 0) {
|
2005-07-14 09:57:16 -06:00
|
|
|
bssbase = sechdrs[i].sh_addr;
|
2006-04-05 02:45:45 -06:00
|
|
|
break;
|
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < n; i++) {
|
|
|
|
switch (sym[i].st_shndx) {
|
|
|
|
case SHN_COMMON:
|
2006-04-05 02:45:45 -06:00
|
|
|
/* Allocate space for the symbol in the .bss section.
|
|
|
|
st_value is currently size.
|
2005-07-14 09:57:16 -06:00
|
|
|
We want it to have the address of the symbol. */
|
|
|
|
|
|
|
|
size = sym[i].st_value;
|
|
|
|
sym[i].st_value = bssbase;
|
|
|
|
|
|
|
|
bssbase += size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHN_ABS:
|
|
|
|
/* Don't need to do anything */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHN_UNDEF:
|
|
|
|
/* ret = -ENOENT; */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHN_MIPS_SCOMMON:
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_debug("simplify_symbols: ignoring SHN_MIPS_SCOMMON symbol <%s> st_shndx %d\n",
|
|
|
|
strtab + sym[i].st_name, sym[i].st_shndx);
|
|
|
|
/* .sbss section */
|
2005-07-14 09:57:16 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
secbase = sechdrs[sym[i].st_shndx].sh_addr;
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if (strncmp(strtab + sym[i].st_name, "_gp", 3) == 0)
|
2005-07-14 09:57:16 -06:00
|
|
|
save_gp_address(secbase, sym[i].st_value);
|
|
|
|
|
|
|
|
sym[i].st_value += secbase;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ELFLOADER
|
2014-01-01 08:35:32 -07:00
|
|
|
static void dump_elfsymbols(Elf_Shdr *sechdrs, unsigned int symindex,
|
2005-07-14 09:57:16 -06:00
|
|
|
const char *strtab, struct module *mod)
|
|
|
|
{
|
|
|
|
Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
|
|
|
|
unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_debug("dump_elfsymbols: n %d\n", n);
|
2005-07-14 09:57:16 -06:00
|
|
|
for (i = 1; i < n; i++) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_debug(" i %d name <%s> 0x%x\n", i, strtab + sym[i].st_name,
|
|
|
|
sym[i].st_value);
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
static int find_vpe_symbols(struct vpe *v, Elf_Shdr *sechdrs,
|
2005-07-14 09:57:16 -06:00
|
|
|
unsigned int symindex, const char *strtab,
|
|
|
|
struct module *mod)
|
|
|
|
{
|
|
|
|
Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
|
|
|
|
unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
|
|
|
|
|
|
|
|
for (i = 1; i < n; i++) {
|
2014-01-01 08:35:32 -07:00
|
|
|
if (strcmp(strtab + sym[i].st_name, "__start") == 0)
|
2005-07-14 09:57:16 -06:00
|
|
|
v->__start = sym[i].st_value;
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0)
|
2005-07-14 09:57:16 -06:00
|
|
|
v->shared_ptr = (void *)sym[i].st_value;
|
|
|
|
}
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if ((v->__start == 0) || (v->shared_ptr == NULL))
|
2006-04-05 02:45:45 -06:00
|
|
|
return -1;
|
|
|
|
|
2005-07-14 09:57:16 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-31 16:34:52 -07:00
|
|
|
/*
|
2006-04-05 02:45:45 -06:00
|
|
|
* Allocates a VPE with some program code space(the load address), copies the
|
|
|
|
* contents of the program (p)buffer performing relocatations/etc, free's it
|
|
|
|
* when finished.
|
|
|
|
*/
|
2014-01-01 08:35:32 -07:00
|
|
|
static int vpe_elfload(struct vpe *v)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
|
|
|
Elf_Ehdr *hdr;
|
|
|
|
Elf_Shdr *sechdrs;
|
|
|
|
long err = 0;
|
|
|
|
char *secstrings, *strtab = NULL;
|
2006-04-05 02:45:45 -06:00
|
|
|
unsigned int len, i, symindex = 0, strindex = 0, relocate = 0;
|
2014-01-01 08:35:32 -07:00
|
|
|
struct module mod; /* so we can re-use the relocations code */
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
memset(&mod, 0, sizeof(struct module));
|
2006-04-05 02:45:45 -06:00
|
|
|
strcpy(mod.name, "VPE loader");
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
hdr = (Elf_Ehdr *) v->pbuffer;
|
|
|
|
len = v->plen;
|
|
|
|
|
|
|
|
/* Sanity checks against insmoding binaries or wrong arch,
|
|
|
|
weird elf version */
|
2008-05-04 10:50:02 -06:00
|
|
|
if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
|
2006-04-05 02:45:45 -06:00
|
|
|
|| (hdr->e_type != ET_REL && hdr->e_type != ET_EXEC)
|
|
|
|
|| !elf_check_arch(hdr)
|
2005-07-14 09:57:16 -06:00
|
|
|
|| hdr->e_shentsize != sizeof(*sechdrs)) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: program wrong arch or weird elf version\n");
|
2005-07-14 09:57:16 -06:00
|
|
|
|
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
if (hdr->e_type == ET_REL)
|
|
|
|
relocate = 1;
|
|
|
|
|
2005-07-14 09:57:16 -06:00
|
|
|
if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_err("VPE loader: program length %u truncated\n", len);
|
2006-04-05 02:45:45 -06:00
|
|
|
|
2005-07-14 09:57:16 -06:00
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convenience variables */
|
|
|
|
sechdrs = (void *)hdr + hdr->e_shoff;
|
|
|
|
secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
|
|
|
|
sechdrs[0].sh_addr = 0;
|
|
|
|
|
|
|
|
/* And these should exist, but gcc whinges if we don't init them */
|
|
|
|
symindex = strindex = 0;
|
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
if (relocate) {
|
|
|
|
for (i = 1; i < hdr->e_shnum; i++) {
|
2014-01-01 08:35:32 -07:00
|
|
|
if ((sechdrs[i].sh_type != SHT_NOBITS) &&
|
|
|
|
(len < sechdrs[i].sh_offset + sechdrs[i].sh_size)) {
|
|
|
|
pr_err("VPE program length %u truncated\n",
|
2006-04-05 02:45:45 -06:00
|
|
|
len);
|
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
/* Mark all sections sh_addr with their address in the
|
|
|
|
temporary image. */
|
2014-01-01 08:35:32 -07:00
|
|
|
sechdrs[i].sh_addr = (size_t) hdr +
|
|
|
|
sechdrs[i].sh_offset;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
/* Internal symbols and strings. */
|
|
|
|
if (sechdrs[i].sh_type == SHT_SYMTAB) {
|
|
|
|
symindex = i;
|
|
|
|
strindex = sechdrs[i].sh_link;
|
2014-01-01 08:35:32 -07:00
|
|
|
strtab = (char *)hdr +
|
|
|
|
sechdrs[strindex].sh_offset;
|
2006-04-05 02:45:45 -06:00
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
2006-04-05 02:45:45 -06:00
|
|
|
layout_sections(&mod, hdr, sechdrs, secstrings);
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
2015-11-25 16:14:08 -07:00
|
|
|
v->load_addr = alloc_progmem(mod.core_layout.size);
|
2008-03-13 09:16:53 -06:00
|
|
|
if (!v->load_addr)
|
|
|
|
return -ENOMEM;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2008-03-13 09:16:53 -06:00
|
|
|
pr_info("VPE loader: loading to %p\n", v->load_addr);
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
if (relocate) {
|
|
|
|
for (i = 0; i < hdr->e_shnum; i++) {
|
|
|
|
void *dest;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
if (!(sechdrs[i].sh_flags & SHF_ALLOC))
|
|
|
|
continue;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
dest = v->load_addr + sechdrs[i].sh_entsize;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
if (sechdrs[i].sh_type != SHT_NOBITS)
|
|
|
|
memcpy(dest, (void *)sechdrs[i].sh_addr,
|
|
|
|
sechdrs[i].sh_size);
|
|
|
|
/* Update sh_addr to point to copy in image. */
|
|
|
|
sechdrs[i].sh_addr = (unsigned long)dest;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_debug(" section sh_name %s sh_addr 0x%x\n",
|
|
|
|
secstrings + sechdrs[i].sh_name,
|
|
|
|
sechdrs[i].sh_addr);
|
2006-04-05 02:45:45 -06:00
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2013-01-22 04:59:30 -07:00
|
|
|
/* Fix up syms, so that st_value is a pointer to location. */
|
|
|
|
simplify_symbols(sechdrs, symindex, strtab, secstrings,
|
|
|
|
hdr->e_shnum, &mod);
|
|
|
|
|
|
|
|
/* Now do relocations. */
|
|
|
|
for (i = 1; i < hdr->e_shnum; i++) {
|
|
|
|
const char *strtab = (char *)sechdrs[strindex].sh_addr;
|
|
|
|
unsigned int info = sechdrs[i].sh_info;
|
|
|
|
|
|
|
|
/* Not a valid relocation section? */
|
|
|
|
if (info >= hdr->e_shnum)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Don't bother with non-allocated sections */
|
|
|
|
if (!(sechdrs[info].sh_flags & SHF_ALLOC))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sechdrs[i].sh_type == SHT_REL)
|
2014-01-01 08:35:32 -07:00
|
|
|
err = apply_relocations(sechdrs, strtab,
|
|
|
|
symindex, i, &mod);
|
2013-01-22 04:59:30 -07:00
|
|
|
else if (sechdrs[i].sh_type == SHT_RELA)
|
2014-01-01 08:35:32 -07:00
|
|
|
err = apply_relocate_add(sechdrs, strtab,
|
|
|
|
symindex, i, &mod);
|
2013-01-22 04:59:30 -07:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
2014-01-01 08:35:32 -07:00
|
|
|
struct elf_phdr *phdr = (struct elf_phdr *)
|
|
|
|
((char *)hdr + hdr->e_phoff);
|
2006-04-05 02:45:45 -06:00
|
|
|
|
2007-10-10 06:33:03 -06:00
|
|
|
for (i = 0; i < hdr->e_phnum; i++) {
|
2008-04-16 07:32:22 -06:00
|
|
|
if (phdr->p_type == PT_LOAD) {
|
|
|
|
memcpy((void *)phdr->p_paddr,
|
|
|
|
(char *)hdr + phdr->p_offset,
|
|
|
|
phdr->p_filesz);
|
|
|
|
memset((void *)phdr->p_paddr + phdr->p_filesz,
|
|
|
|
0, phdr->p_memsz - phdr->p_filesz);
|
|
|
|
}
|
|
|
|
phdr++;
|
2007-10-10 06:33:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < hdr->e_shnum; i++) {
|
2013-01-22 04:59:30 -07:00
|
|
|
/* Internal symbols and strings. */
|
|
|
|
if (sechdrs[i].sh_type == SHT_SYMTAB) {
|
|
|
|
symindex = i;
|
|
|
|
strindex = sechdrs[i].sh_link;
|
2014-01-01 08:35:32 -07:00
|
|
|
strtab = (char *)hdr +
|
|
|
|
sechdrs[strindex].sh_offset;
|
2013-01-22 04:59:30 -07:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
/*
|
|
|
|
* mark symtab's address for when we try
|
|
|
|
* to find the magic symbols
|
|
|
|
*/
|
|
|
|
sechdrs[i].sh_addr = (size_t) hdr +
|
|
|
|
sechdrs[i].sh_offset;
|
2013-01-22 04:59:30 -07:00
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure it's physically written out */
|
|
|
|
flush_icache_range((unsigned long)v->load_addr,
|
|
|
|
(unsigned long)v->load_addr + v->len);
|
|
|
|
|
|
|
|
if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
|
2006-04-05 02:45:45 -06:00
|
|
|
if (v->__start == 0) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: program does not contain a __start symbol\n");
|
2006-04-05 02:45:45 -06:00
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
if (v->shared_ptr == NULL)
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: program does not contain vpe_shared symbol.\n"
|
|
|
|
" Unable to use AMVP (AP/SP) facilities.\n");
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_info(" elf loaded\n");
|
2006-04-05 02:45:45 -06:00
|
|
|
return 0;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
static int getcwd(char *buff, int size)
|
|
|
|
{
|
|
|
|
mm_segment_t old_fs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
old_fs = get_fs();
|
|
|
|
set_fs(KERNEL_DS);
|
|
|
|
|
2007-10-11 16:46:15 -06:00
|
|
|
ret = sys_getcwd(buff, size);
|
2006-04-05 02:45:45 -06:00
|
|
|
|
|
|
|
set_fs(old_fs);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-22 04:59:30 -07:00
|
|
|
/* checks VPE is unused and gets ready to load program */
|
2005-07-14 09:57:16 -06:00
|
|
|
static int vpe_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2007-02-23 06:40:45 -07:00
|
|
|
enum vpe_state state;
|
2014-01-01 08:35:32 -07:00
|
|
|
struct vpe_notifications *notifier;
|
2007-07-27 12:31:10 -06:00
|
|
|
struct vpe *v;
|
2009-09-28 17:52:27 -06:00
|
|
|
int ret;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2013-10-30 14:52:06 -06:00
|
|
|
if (VPE_MODULE_MINOR != iminor(inode)) {
|
2007-07-27 12:31:10 -06:00
|
|
|
/* assume only 1 device at the moment. */
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: only vpe1 is supported\n");
|
2009-09-28 17:52:27 -06:00
|
|
|
|
|
|
|
return -ENODEV;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
v = get_vpe(aprp_cpu_index());
|
|
|
|
if (v == NULL) {
|
|
|
|
pr_warn("VPE loader: unable to get vpe\n");
|
2009-09-28 17:52:27 -06:00
|
|
|
|
|
|
|
return -ENODEV;
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
2007-02-23 06:40:45 -07:00
|
|
|
state = xchg(&v->state, VPE_STATE_INUSE);
|
|
|
|
if (state != VPE_STATE_UNUSED) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_debug("VPE loader: tc in use dumping regs\n");
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
list_for_each_entry(notifier, &v->notify, list)
|
|
|
|
notifier->stop(aprp_cpu_index());
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
release_progmem(v->load_addr);
|
2013-10-30 14:52:06 -06:00
|
|
|
cleanup_tc(get_tc(aprp_cpu_index()));
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this of-course trashes what was there before... */
|
|
|
|
v->pbuffer = vmalloc(P_SIZE);
|
2010-10-30 10:37:16 -06:00
|
|
|
if (!v->pbuffer) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: unable to allocate memory\n");
|
2010-10-30 10:37:16 -06:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2005-07-14 09:57:16 -06:00
|
|
|
v->plen = P_SIZE;
|
|
|
|
v->load_addr = NULL;
|
|
|
|
v->len = 0;
|
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
v->cwd[0] = 0;
|
|
|
|
ret = getcwd(v->cwd, VPE_PATH_MAX);
|
|
|
|
if (ret < 0)
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: open, getcwd returned %d\n", ret);
|
2006-04-05 02:45:45 -06:00
|
|
|
|
|
|
|
v->shared_ptr = NULL;
|
|
|
|
v->__start = 0;
|
2007-07-27 12:31:10 -06:00
|
|
|
|
2005-07-14 09:57:16 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vpe_release(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2015-07-12 17:11:20 -06:00
|
|
|
#if defined(CONFIG_MIPS_VPE_LOADER_MT) || defined(CONFIG_MIPS_VPE_LOADER_CMP)
|
2005-10-31 16:34:52 -07:00
|
|
|
struct vpe *v;
|
2005-07-14 09:57:16 -06:00
|
|
|
Elf_Ehdr *hdr;
|
2007-07-27 12:31:10 -06:00
|
|
|
int ret = 0;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2013-10-30 14:52:06 -06:00
|
|
|
v = get_vpe(aprp_cpu_index());
|
2007-07-27 12:31:10 -06:00
|
|
|
if (v == NULL)
|
2005-07-14 09:57:16 -06:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
hdr = (Elf_Ehdr *) v->pbuffer;
|
2008-05-04 10:50:02 -06:00
|
|
|
if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) == 0) {
|
2015-07-12 17:11:12 -06:00
|
|
|
if (vpe_elfload(v) >= 0) {
|
2005-07-14 09:57:16 -06:00
|
|
|
vpe_run(v);
|
2007-07-27 12:31:10 -06:00
|
|
|
} else {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: ELF load failed.\n");
|
2005-07-14 09:57:16 -06:00
|
|
|
ret = -ENOEXEC;
|
|
|
|
}
|
|
|
|
} else {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: only elf files are supported\n");
|
2005-07-14 09:57:16 -06:00
|
|
|
ret = -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
/* It's good to be able to run the SP and if it chokes have a look at
|
|
|
|
the /dev/rt?. But if we reset the pointer to the shared struct we
|
2008-12-04 17:36:54 -07:00
|
|
|
lose what has happened. So perhaps if garbage is sent to the vpe
|
2006-04-05 02:45:45 -06:00
|
|
|
device, use it as a trigger for the reset. Hopefully a nice
|
|
|
|
executable will be along shortly. */
|
|
|
|
if (ret < 0)
|
|
|
|
v->shared_ptr = NULL;
|
|
|
|
|
2010-10-30 10:37:16 -06:00
|
|
|
vfree(v->pbuffer);
|
2005-07-14 09:57:16 -06:00
|
|
|
v->plen = 0;
|
2010-10-30 10:37:16 -06:00
|
|
|
|
2005-07-14 09:57:16 -06:00
|
|
|
return ret;
|
2015-07-12 17:11:20 -06:00
|
|
|
#else
|
|
|
|
pr_warn("VPE loader: ELF load failed.\n");
|
|
|
|
return -ENOEXEC;
|
|
|
|
#endif
|
2005-07-14 09:57:16 -06:00
|
|
|
}
|
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
static ssize_t vpe_write(struct file *file, const char __user *buffer,
|
|
|
|
size_t count, loff_t *ppos)
|
2005-07-14 09:57:16 -06:00
|
|
|
{
|
|
|
|
size_t ret = count;
|
2005-10-31 16:34:52 -07:00
|
|
|
struct vpe *v;
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2013-10-30 14:52:06 -06:00
|
|
|
if (iminor(file_inode(file)) != VPE_MODULE_MINOR)
|
2007-07-27 12:31:10 -06:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-10-30 14:52:06 -06:00
|
|
|
v = get_vpe(aprp_cpu_index());
|
2014-01-01 08:35:32 -07:00
|
|
|
|
2007-07-27 12:31:10 -06:00
|
|
|
if (v == NULL)
|
2005-07-14 09:57:16 -06:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if ((count + v->len) > v->plen) {
|
2014-01-01 08:35:32 -07:00
|
|
|
pr_warn("VPE loader: elf size too big. Perhaps strip uneeded symbols\n");
|
2005-07-14 09:57:16 -06:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
count -= copy_from_user(v->pbuffer + v->len, buffer, count);
|
2006-04-05 02:45:45 -06:00
|
|
|
if (!count)
|
2005-07-14 09:57:16 -06:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
v->len += count;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-30 14:52:06 -06:00
|
|
|
const struct file_operations vpe_fops = {
|
2005-07-14 09:57:16 -06:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = vpe_open,
|
|
|
|
.release = vpe_release,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 10:52:59 -06:00
|
|
|
.write = vpe_write,
|
|
|
|
.llseek = noop_llseek,
|
2005-07-14 09:57:16 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
void *vpe_get_shared(int index)
|
|
|
|
{
|
2014-01-01 08:35:32 -07:00
|
|
|
struct vpe *v = get_vpe(index);
|
2005-07-14 09:57:16 -06:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if (v == NULL)
|
2005-07-14 09:57:16 -06:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return v->shared_ptr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vpe_get_shared);
|
|
|
|
|
2006-04-05 02:45:45 -06:00
|
|
|
int vpe_notify(int index, struct vpe_notifications *notify)
|
|
|
|
{
|
2014-01-01 08:35:32 -07:00
|
|
|
struct vpe *v = get_vpe(index);
|
2006-04-05 02:45:45 -06:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if (v == NULL)
|
2006-04-05 02:45:45 -06:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
list_add(¬ify->list, &v->notify);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vpe_notify);
|
|
|
|
|
|
|
|
char *vpe_getcwd(int index)
|
|
|
|
{
|
2014-01-01 08:35:32 -07:00
|
|
|
struct vpe *v = get_vpe(index);
|
2006-04-05 02:45:45 -06:00
|
|
|
|
2014-01-01 08:35:32 -07:00
|
|
|
if (v == NULL)
|
2006-04-05 02:45:45 -06:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return v->cwd;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vpe_getcwd);
|
|
|
|
|
2005-07-14 09:57:16 -06:00
|
|
|
module_init(vpe_module_init);
|
|
|
|
module_exit(vpe_module_exit);
|
|
|
|
MODULE_DESCRIPTION("MIPS VPE Loader");
|
2006-04-05 02:45:45 -06:00
|
|
|
MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
|
2005-07-14 09:57:16 -06:00
|
|
|
MODULE_LICENSE("GPL");
|