3cfa98bf9125215ae2fc7ea8fdc287fd84245285
[~shefty/rdma-dev.git] / arch / tile / mm / elf.c
1 /*
2  * Copyright 2010 Tilera Corporation. All Rights Reserved.
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful, but
9  *   WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11  *   NON INFRINGEMENT.  See the GNU General Public License for
12  *   more details.
13  */
14
15 #include <linux/mm.h>
16 #include <linux/pagemap.h>
17 #include <linux/binfmts.h>
18 #include <linux/compat.h>
19 #include <linux/mman.h>
20 #include <linux/elf.h>
21 #include <asm/pgtable.h>
22 #include <asm/pgalloc.h>
23 #include <asm/sections.h>
24 #include <arch/sim_def.h>
25
26 /* Notify a running simulator, if any, that an exec just occurred. */
27 static void sim_notify_exec(const char *binary_name)
28 {
29         unsigned char c;
30         do {
31                 c = *binary_name++;
32                 __insn_mtspr(SPR_SIM_CONTROL,
33                              (SIM_CONTROL_OS_EXEC
34                               | (c << _SIM_CONTROL_OPERATOR_BITS)));
35
36         } while (c);
37 }
38
39 static int notify_exec(struct mm_struct *mm)
40 {
41         int retval = 0;  /* failure */
42
43         if (mm->exe_file) {
44                 char *buf = (char *) __get_free_page(GFP_KERNEL);
45                 if (buf) {
46                         char *path = d_path(&mm->exe_file->f_path,
47                                             buf, PAGE_SIZE);
48                         if (!IS_ERR(path)) {
49                                 sim_notify_exec(path);
50                                 retval = 1;
51                         }
52                         free_page((unsigned long)buf);
53                 }
54         }
55         return retval;
56 }
57
58 /* Notify a running simulator, if any, that we loaded an interpreter. */
59 static void sim_notify_interp(unsigned long load_addr)
60 {
61         size_t i;
62         for (i = 0; i < sizeof(load_addr); i++) {
63                 unsigned char c = load_addr >> (i * 8);
64                 __insn_mtspr(SPR_SIM_CONTROL,
65                              (SIM_CONTROL_OS_INTERP
66                               | (c << _SIM_CONTROL_OPERATOR_BITS)));
67         }
68 }
69
70
71 /* Kernel address of page used to map read-only kernel data into userspace. */
72 static void *vdso_page;
73
74 /* One-entry array used for install_special_mapping. */
75 static struct page *vdso_pages[1];
76
77 static int __init vdso_setup(void)
78 {
79         vdso_page = (void *)get_zeroed_page(GFP_ATOMIC);
80         memcpy(vdso_page, __rt_sigreturn, __rt_sigreturn_end - __rt_sigreturn);
81         vdso_pages[0] = virt_to_page(vdso_page);
82         return 0;
83 }
84 device_initcall(vdso_setup);
85
86 const char *arch_vma_name(struct vm_area_struct *vma)
87 {
88         if (vma->vm_private_data == vdso_pages)
89                 return "[vdso]";
90 #ifndef __tilegx__
91         if (vma->vm_start == MEM_USER_INTRPT)
92                 return "[intrpt]";
93 #endif
94         return NULL;
95 }
96
97 int arch_setup_additional_pages(struct linux_binprm *bprm,
98                                 int executable_stack)
99 {
100         struct mm_struct *mm = current->mm;
101         unsigned long vdso_base;
102         int retval = 0;
103
104         down_write(&mm->mmap_sem);
105
106         /*
107          * Notify the simulator that an exec just occurred.
108          * If we can't find the filename of the mapping, just use
109          * whatever was passed as the linux_binprm filename.
110          */
111         if (!notify_exec(mm))
112                 sim_notify_exec(bprm->filename);
113
114         /*
115          * MAYWRITE to allow gdb to COW and set breakpoints
116          */
117         vdso_base = VDSO_BASE;
118         retval = install_special_mapping(mm, vdso_base, PAGE_SIZE,
119                                          VM_READ|VM_EXEC|
120                                          VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
121                                          vdso_pages);
122
123 #ifndef __tilegx__
124         /*
125          * Set up a user-interrupt mapping here; the user can't
126          * create one themselves since it is above TASK_SIZE.
127          * We make it unwritable by default, so the model for adding
128          * interrupt vectors always involves an mprotect.
129          */
130         if (!retval) {
131                 unsigned long addr = MEM_USER_INTRPT;
132                 addr = mmap_region(NULL, addr, INTRPT_SIZE,
133                                    MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
134                                    VM_READ|VM_EXEC|
135                                    VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 0);
136                 if (addr > (unsigned long) -PAGE_SIZE)
137                         retval = (int) addr;
138         }
139 #endif
140
141         up_write(&mm->mmap_sem);
142
143         return retval;
144 }
145
146
147 void elf_plat_init(struct pt_regs *regs, unsigned long load_addr)
148 {
149         /* Zero all registers. */
150         memset(regs, 0, sizeof(*regs));
151
152         /* Report the interpreter's load address. */
153         sim_notify_interp(load_addr);
154 }