OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [binfmt_som.c] - Diff between revs 1275 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1275 Rev 1765
/*
/*
 * linux/fs/binfmt_som.c
 * linux/fs/binfmt_som.c
 *
 *
 * These are the functions used to load SOM format executables as used
 * These are the functions used to load SOM format executables as used
 * by HP-UX.
 * by HP-UX.
 *
 *
 * Copyright 1999 Matthew Wilcox <willy@bofh.ai>
 * Copyright 1999 Matthew Wilcox <willy@bofh.ai>
 * based on binfmt_elf which is
 * based on binfmt_elf which is
 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
 */
 */
 
 
#include <linux/module.h>
#include <linux/module.h>
 
 
#include <linux/fs.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/stat.h>
#include <linux/sched.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/mman.h>
#include <linux/errno.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/signal.h>
#include <linux/binfmts.h>
#include <linux/binfmts.h>
#include <linux/som.h>
#include <linux/som.h>
#include <linux/string.h>
#include <linux/string.h>
#include <linux/file.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/ptrace.h>
#include <linux/slab.h>
#include <linux/slab.h>
#include <linux/shm.h>
#include <linux/shm.h>
#include <linux/personality.h>
#include <linux/personality.h>
#include <linux/init.h>
#include <linux/init.h>
 
 
#include <asm/uaccess.h>
#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <asm/pgtable.h>
 
 
#include <linux/config.h>
#include <linux/config.h>
 
 
#include <linux/elf.h>
#include <linux/elf.h>
 
 
static int load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs);
static int load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs);
static int load_som_library(struct file *);
static int load_som_library(struct file *);
 
 
/*
/*
 * If we don't support core dumping, then supply a NULL so we
 * If we don't support core dumping, then supply a NULL so we
 * don't even try.
 * don't even try.
 */
 */
#if 0
#if 0
static int som_core_dump(long signr, struct pt_regs * regs);
static int som_core_dump(long signr, struct pt_regs * regs);
#else
#else
#define som_core_dump   NULL
#define som_core_dump   NULL
#endif
#endif
 
 
#define SOM_PAGESTART(_v) ((_v) & ~(unsigned long)(SOM_PAGESIZE-1))
#define SOM_PAGESTART(_v) ((_v) & ~(unsigned long)(SOM_PAGESIZE-1))
#define SOM_PAGEOFFSET(_v) ((_v) & (SOM_PAGESIZE-1))
#define SOM_PAGEOFFSET(_v) ((_v) & (SOM_PAGESIZE-1))
#define SOM_PAGEALIGN(_v) (((_v) + SOM_PAGESIZE - 1) & ~(SOM_PAGESIZE - 1))
#define SOM_PAGEALIGN(_v) (((_v) + SOM_PAGESIZE - 1) & ~(SOM_PAGESIZE - 1))
 
 
static struct linux_binfmt som_format = {
static struct linux_binfmt som_format = {
        NULL, THIS_MODULE, load_som_binary, load_som_library, som_core_dump, SOM_PAGESIZE
        NULL, THIS_MODULE, load_som_binary, load_som_library, som_core_dump, SOM_PAGESIZE
};
};
 
 
/*
/*
 * create_som_tables() parses the env- and arg-strings in new user
 * create_som_tables() parses the env- and arg-strings in new user
 * memory and creates the pointer tables from them, and puts their
 * memory and creates the pointer tables from them, and puts their
 * addresses on the "stack", returning the new stack pointer value.
 * addresses on the "stack", returning the new stack pointer value.
 */
 */
static void create_som_tables(struct linux_binprm *bprm)
static void create_som_tables(struct linux_binprm *bprm)
{
{
        char **argv, **envp;
        char **argv, **envp;
        int argc = bprm->argc;
        int argc = bprm->argc;
        int envc = bprm->envc;
        int envc = bprm->envc;
        unsigned long p;
        unsigned long p;
        unsigned long *sp;
        unsigned long *sp;
 
 
        /* Word-align the stack pointer */
        /* Word-align the stack pointer */
        sp = (unsigned long *)((bprm->p + 3) & ~3);
        sp = (unsigned long *)((bprm->p + 3) & ~3);
 
 
        envp = (char **) sp;
        envp = (char **) sp;
        sp += envc + 1;
        sp += envc + 1;
        argv = (char **) sp;
        argv = (char **) sp;
        sp += argc + 1;
        sp += argc + 1;
 
 
        __put_user((unsigned long) envp,++sp);
        __put_user((unsigned long) envp,++sp);
        __put_user((unsigned long) argv,++sp);
        __put_user((unsigned long) argv,++sp);
 
 
        __put_user(argc, ++sp);
        __put_user(argc, ++sp);
 
 
        bprm->p = (unsigned long) sp;
        bprm->p = (unsigned long) sp;
 
 
        p = current->mm->arg_start;
        p = current->mm->arg_start;
        while (argc-- > 0) {
        while (argc-- > 0) {
                __put_user((char *)p,argv++);
                __put_user((char *)p,argv++);
                p += strlen_user((char *)p);
                p += strlen_user((char *)p);
        }
        }
        __put_user(NULL, argv);
        __put_user(NULL, argv);
        current->mm->arg_end = current->mm->env_start = p;
        current->mm->arg_end = current->mm->env_start = p;
        while (envc-- > 0) {
        while (envc-- > 0) {
                __put_user((char *)p,envp++);
                __put_user((char *)p,envp++);
                p += strlen_user((char *)p);
                p += strlen_user((char *)p);
        }
        }
        __put_user(NULL, envp);
        __put_user(NULL, envp);
        current->mm->env_end = p;
        current->mm->env_end = p;
}
}
 
 
static int check_som_header(struct som_hdr *som_ex)
static int check_som_header(struct som_hdr *som_ex)
{
{
        int *buf = (int *)som_ex;
        int *buf = (int *)som_ex;
        int i, ck;
        int i, ck;
 
 
        if (som_ex->system_id != SOM_SID_PARISC_1_0 &&
        if (som_ex->system_id != SOM_SID_PARISC_1_0 &&
            som_ex->system_id != SOM_SID_PARISC_1_1 &&
            som_ex->system_id != SOM_SID_PARISC_1_1 &&
            som_ex->system_id != SOM_SID_PARISC_2_0)
            som_ex->system_id != SOM_SID_PARISC_2_0)
                return -ENOEXEC;
                return -ENOEXEC;
 
 
        if (som_ex->a_magic != SOM_EXEC_NONSHARE &&
        if (som_ex->a_magic != SOM_EXEC_NONSHARE &&
            som_ex->a_magic != SOM_EXEC_SHARE &&
            som_ex->a_magic != SOM_EXEC_SHARE &&
            som_ex->a_magic != SOM_EXEC_DEMAND)
            som_ex->a_magic != SOM_EXEC_DEMAND)
                return -ENOEXEC;
                return -ENOEXEC;
 
 
        if (som_ex->version_id != SOM_ID_OLD &&
        if (som_ex->version_id != SOM_ID_OLD &&
            som_ex->version_id != SOM_ID_NEW)
            som_ex->version_id != SOM_ID_NEW)
                return -ENOEXEC;
                return -ENOEXEC;
 
 
        ck = 0;
        ck = 0;
        for (i=0; i<32; i++)
        for (i=0; i<32; i++)
                ck ^= buf[i];
                ck ^= buf[i];
        if (ck != 0)
        if (ck != 0)
                return -ENOEXEC;
                return -ENOEXEC;
 
 
        return 0;
        return 0;
}
}
 
 
static int map_som_binary(struct file *file,
static int map_som_binary(struct file *file,
                const struct som_exec_auxhdr *hpuxhdr)
                const struct som_exec_auxhdr *hpuxhdr)
{
{
        unsigned long code_start, code_size, data_start, data_size;
        unsigned long code_start, code_size, data_start, data_size;
        unsigned long bss_start, som_brk;
        unsigned long bss_start, som_brk;
        int retval;
        int retval;
        int prot = PROT_READ | PROT_EXEC;
        int prot = PROT_READ | PROT_EXEC;
        int flags = MAP_FIXED|MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
        int flags = MAP_FIXED|MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
 
 
        mm_segment_t old_fs = get_fs();
        mm_segment_t old_fs = get_fs();
        set_fs(get_ds());
        set_fs(get_ds());
 
 
        code_start = SOM_PAGESTART(hpuxhdr->exec_tmem);
        code_start = SOM_PAGESTART(hpuxhdr->exec_tmem);
        code_size = SOM_PAGEALIGN(hpuxhdr->exec_tsize);
        code_size = SOM_PAGEALIGN(hpuxhdr->exec_tsize);
        current->mm->start_code = code_start;
        current->mm->start_code = code_start;
        current->mm->end_code = code_start + code_size;
        current->mm->end_code = code_start + code_size;
        down_write(&current->mm->mmap_sem);
        down_write(&current->mm->mmap_sem);
        retval = do_mmap(file, code_start, code_size, prot,
        retval = do_mmap(file, code_start, code_size, prot,
                        flags, SOM_PAGESTART(hpuxhdr->exec_tfile));
                        flags, SOM_PAGESTART(hpuxhdr->exec_tfile));
        up_write(&current->mm->mmap_sem);
        up_write(&current->mm->mmap_sem);
        if (retval < 0 && retval > -1024)
        if (retval < 0 && retval > -1024)
                goto out;
                goto out;
 
 
        data_start = SOM_PAGESTART(hpuxhdr->exec_dmem);
        data_start = SOM_PAGESTART(hpuxhdr->exec_dmem);
        data_size = SOM_PAGEALIGN(hpuxhdr->exec_dsize);
        data_size = SOM_PAGEALIGN(hpuxhdr->exec_dsize);
        current->mm->start_data = data_start;
        current->mm->start_data = data_start;
        current->mm->end_data = bss_start = data_start + data_size;
        current->mm->end_data = bss_start = data_start + data_size;
        down_write(&current->mm->mmap_sem);
        down_write(&current->mm->mmap_sem);
        retval = do_mmap(file, data_start, data_size,
        retval = do_mmap(file, data_start, data_size,
                        prot | PROT_WRITE, flags,
                        prot | PROT_WRITE, flags,
                        SOM_PAGESTART(hpuxhdr->exec_dfile));
                        SOM_PAGESTART(hpuxhdr->exec_dfile));
        up_write(&current->mm->mmap_sem);
        up_write(&current->mm->mmap_sem);
        if (retval < 0 && retval > -1024)
        if (retval < 0 && retval > -1024)
                goto out;
                goto out;
 
 
        som_brk = bss_start + SOM_PAGEALIGN(hpuxhdr->exec_bsize);
        som_brk = bss_start + SOM_PAGEALIGN(hpuxhdr->exec_bsize);
        current->mm->start_brk = current->mm->brk = som_brk;
        current->mm->start_brk = current->mm->brk = som_brk;
        down_write(&current->mm->mmap_sem);
        down_write(&current->mm->mmap_sem);
        retval = do_mmap(NULL, bss_start, som_brk - bss_start,
        retval = do_mmap(NULL, bss_start, som_brk - bss_start,
                        prot | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, 0);
                        prot | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, 0);
        up_write(&current->mm->mmap_sem);
        up_write(&current->mm->mmap_sem);
        if (retval > 0 || retval < -1024)
        if (retval > 0 || retval < -1024)
                retval = 0;
                retval = 0;
out:
out:
        set_fs(old_fs);
        set_fs(old_fs);
        return retval;
        return retval;
}
}
 
 
 
 
/*
/*
 * These are the functions used to load SOM executables and shared
 * These are the functions used to load SOM executables and shared
 * libraries.  There is no binary dependent code anywhere else.
 * libraries.  There is no binary dependent code anywhere else.
 */
 */
 
 
static inline int
static inline int
do_load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs)
do_load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs)
{
{
        int som_exec_fileno;
        int som_exec_fileno;
        int retval;
        int retval;
        unsigned int size;
        unsigned int size;
        unsigned long som_entry;
        unsigned long som_entry;
        struct som_hdr *som_ex;
        struct som_hdr *som_ex;
        struct som_exec_auxhdr *hpuxhdr;
        struct som_exec_auxhdr *hpuxhdr;
 
 
        /* Get the exec-header */
        /* Get the exec-header */
        som_ex = (struct som_hdr *) bprm->buf;
        som_ex = (struct som_hdr *) bprm->buf;
 
 
        retval = check_som_header(som_ex);
        retval = check_som_header(som_ex);
        if (retval != 0)
        if (retval != 0)
                goto out;
                goto out;
 
 
        /* Now read in the auxiliary header information */
        /* Now read in the auxiliary header information */
 
 
        retval = -ENOMEM;
        retval = -ENOMEM;
        size = som_ex->aux_header_size;
        size = som_ex->aux_header_size;
        if (size > SOM_PAGESIZE)
        if (size > SOM_PAGESIZE)
                goto out;
                goto out;
        hpuxhdr = (struct som_exec_auxhdr *) kmalloc(size, GFP_KERNEL);
        hpuxhdr = (struct som_exec_auxhdr *) kmalloc(size, GFP_KERNEL);
        if (!hpuxhdr)
        if (!hpuxhdr)
                goto out;
                goto out;
 
 
        retval = kernel_read(bprm->file, som_ex->aux_header_location,
        retval = kernel_read(bprm->file, som_ex->aux_header_location,
                        (char *) hpuxhdr, size);
                        (char *) hpuxhdr, size);
        if (retval < 0)
        if (retval < 0)
                goto out_free;
                goto out_free;
#error "Fix security hole before enabling me"
#error "Fix security hole before enabling me"
        retval = get_unused_fd();
        retval = get_unused_fd();
        if (retval < 0)
        if (retval < 0)
                goto out_free;
                goto out_free;
        get_file(bprm->file);
        get_file(bprm->file);
        fd_install(som_exec_fileno = retval, bprm->file);
        fd_install(som_exec_fileno = retval, bprm->file);
 
 
        /* Flush all traces of the currently running executable */
        /* Flush all traces of the currently running executable */
        retval = flush_old_exec(bprm);
        retval = flush_old_exec(bprm);
        if (retval)
        if (retval)
                goto out_free;
                goto out_free;
 
 
        /* OK, This is the point of no return */
        /* OK, This is the point of no return */
        current->flags &= ~PF_FORKNOEXEC;
        current->flags &= ~PF_FORKNOEXEC;
        current->personality = PER_HPUX;
        current->personality = PER_HPUX;
 
 
        /* Set the task size for HP-UX processes such that
        /* Set the task size for HP-UX processes such that
         * the gateway page is outside the address space.
         * the gateway page is outside the address space.
         * This can be fixed later, but for now, this is much
         * This can be fixed later, but for now, this is much
         * easier.
         * easier.
         */
         */
 
 
        current->thread.task_size = 0xc0000000;
        current->thread.task_size = 0xc0000000;
 
 
        /* Set map base to allow enough room for hp-ux heap growth */
        /* Set map base to allow enough room for hp-ux heap growth */
 
 
        current->thread.map_base = 0x80000000;
        current->thread.map_base = 0x80000000;
 
 
        retval = map_som_binary(bprm->file, hpuxhdr);
        retval = map_som_binary(bprm->file, hpuxhdr);
        if (retval < 0)
        if (retval < 0)
                goto out_free;
                goto out_free;
 
 
        som_entry = hpuxhdr->exec_entry;
        som_entry = hpuxhdr->exec_entry;
        kfree(hpuxhdr);
        kfree(hpuxhdr);
 
 
        set_binfmt(&som_format);
        set_binfmt(&som_format);
        compute_creds(bprm);
        compute_creds(bprm);
        setup_arg_pages(bprm);
        setup_arg_pages(bprm);
 
 
        create_som_tables(bprm);
        create_som_tables(bprm);
 
 
        current->mm->start_stack = bprm->p;
        current->mm->start_stack = bprm->p;
        current->mm->rss = 0;
        current->mm->rss = 0;
 
 
#if 0
#if 0
        printk("(start_brk) %08lx\n" , (unsigned long) current->mm->start_brk);
        printk("(start_brk) %08lx\n" , (unsigned long) current->mm->start_brk);
        printk("(end_code) %08lx\n" , (unsigned long) current->mm->end_code);
        printk("(end_code) %08lx\n" , (unsigned long) current->mm->end_code);
        printk("(start_code) %08lx\n" , (unsigned long) current->mm->start_code);
        printk("(start_code) %08lx\n" , (unsigned long) current->mm->start_code);
        printk("(end_data) %08lx\n" , (unsigned long) current->mm->end_data);
        printk("(end_data) %08lx\n" , (unsigned long) current->mm->end_data);
        printk("(start_stack) %08lx\n" , (unsigned long) current->mm->start_stack);
        printk("(start_stack) %08lx\n" , (unsigned long) current->mm->start_stack);
        printk("(brk) %08lx\n" , (unsigned long) current->mm->brk);
        printk("(brk) %08lx\n" , (unsigned long) current->mm->brk);
#endif
#endif
 
 
        map_hpux_gateway_page(current,current->mm);
        map_hpux_gateway_page(current,current->mm);
 
 
        start_thread_som(regs, som_entry, bprm->p);
        start_thread_som(regs, som_entry, bprm->p);
        if (current->ptrace & PT_PTRACED)
        if (current->ptrace & PT_PTRACED)
                send_sig(SIGTRAP, current, 0);
                send_sig(SIGTRAP, current, 0);
        return 0;
        return 0;
 
 
        /* error cleanup */
        /* error cleanup */
out_free:
out_free:
        kfree(hpuxhdr);
        kfree(hpuxhdr);
out:
out:
        return retval;
        return retval;
}
}
 
 
static int
static int
load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs)
load_som_binary(struct linux_binprm * bprm, struct pt_regs * regs)
{
{
        int retval;
        int retval;
 
 
        MOD_INC_USE_COUNT;
        MOD_INC_USE_COUNT;
        retval = do_load_som_binary(bprm, regs);
        retval = do_load_som_binary(bprm, regs);
        MOD_DEC_USE_COUNT;
        MOD_DEC_USE_COUNT;
        return retval;
        return retval;
}
}
 
 
static inline int
static inline int
do_load_som_library(struct file *f)
do_load_som_library(struct file *f)
{
{
/* No lib support in SOM yet.  gizza chance.. */
/* No lib support in SOM yet.  gizza chance.. */
        return -ENOEXEC;
        return -ENOEXEC;
}
}
 
 
static int load_som_library(struct file *f)
static int load_som_library(struct file *f)
{
{
        int retval;
        int retval;
 
 
        MOD_INC_USE_COUNT;
        MOD_INC_USE_COUNT;
        retval = do_load_som_library(f);
        retval = do_load_som_library(f);
        MOD_DEC_USE_COUNT;
        MOD_DEC_USE_COUNT;
        return retval;
        return retval;
}
}
 
 
        /* Install the SOM loader.
        /* Install the SOM loader.
         * N.B. We *rely* on the table being the right size with the
         * N.B. We *rely* on the table being the right size with the
         * right number of free slots...
         * right number of free slots...
         */
         */
 
 
static int __init init_som_binfmt(void)
static int __init init_som_binfmt(void)
{
{
        return register_binfmt(&som_format);
        return register_binfmt(&som_format);
}
}
 
 
static void __exit exit_som_binfmt(void)
static void __exit exit_som_binfmt(void)
{
{
        /* Remove the SOM loader. */
        /* Remove the SOM loader. */
        unregister_binfmt(&som_format);
        unregister_binfmt(&som_format);
}
}
 
 
module_init(init_som_binfmt);
module_init(init_som_binfmt);
module_exit(exit_som_binfmt);
module_exit(exit_som_binfmt);
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.