Remove obsolete #include <linux/config.h>
[linux-3.10.git] / arch / ia64 / ia32 / binfmt_elf32.c
1 /*
2  * IA-32 ELF support.
3  *
4  * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
5  * Copyright (C) 2001 Hewlett-Packard Co
6  *      David Mosberger-Tang <davidm@hpl.hp.com>
7  *
8  * 06/16/00     A. Mallick      initialize csd/ssd/tssd/cflg for ia32_load_state
9  * 04/13/01     D. Mosberger    dropped saving tssd in ar.k1---it's not needed
10  * 09/14/01     D. Mosberger    fixed memory management for gdt/tss page
11  */
12
13 #include <linux/types.h>
14 #include <linux/mm.h>
15 #include <linux/security.h>
16
17 #include <asm/param.h>
18 #include <asm/signal.h>
19
20 #include "ia32priv.h"
21 #include "elfcore32.h"
22
23 /* Override some function names */
24 #undef start_thread
25 #define start_thread                    ia32_start_thread
26 #define elf_format                      elf32_format
27 #define init_elf_binfmt                 init_elf32_binfmt
28 #define exit_elf_binfmt                 exit_elf32_binfmt
29
30 #undef CLOCKS_PER_SEC
31 #define CLOCKS_PER_SEC  IA32_CLOCKS_PER_SEC
32
33 extern void ia64_elf32_init (struct pt_regs *regs);
34
35 static void elf32_set_personality (void);
36
37 static unsigned long __attribute ((unused))
38 randomize_stack_top(unsigned long stack_top);
39
40 #define setup_arg_pages(bprm,tos,exec)          ia32_setup_arg_pages(bprm,exec)
41 #define elf_map                         elf32_map
42
43 #undef SET_PERSONALITY
44 #define SET_PERSONALITY(ex, ibcs2)      elf32_set_personality()
45
46 #define elf_read_implies_exec(ex, have_pt_gnu_stack)    (!(have_pt_gnu_stack))
47
48 /* Ugly but avoids duplication */
49 #include "../../../fs/binfmt_elf.c"
50
51 extern struct page *ia32_shared_page[];
52 extern unsigned long *ia32_gdt;
53 extern struct page *ia32_gate_page;
54
55 struct page *
56 ia32_install_shared_page (struct vm_area_struct *vma, unsigned long address, int *type)
57 {
58         struct page *pg = ia32_shared_page[smp_processor_id()];
59         get_page(pg);
60         if (type)
61                 *type = VM_FAULT_MINOR;
62         return pg;
63 }
64
65 struct page *
66 ia32_install_gate_page (struct vm_area_struct *vma, unsigned long address, int *type)
67 {
68         struct page *pg = ia32_gate_page;
69         get_page(pg);
70         if (type)
71                 *type = VM_FAULT_MINOR;
72         return pg;
73 }
74
75
76 static struct vm_operations_struct ia32_shared_page_vm_ops = {
77         .nopage = ia32_install_shared_page
78 };
79
80 static struct vm_operations_struct ia32_gate_page_vm_ops = {
81         .nopage = ia32_install_gate_page
82 };
83
84 void
85 ia64_elf32_init (struct pt_regs *regs)
86 {
87         struct vm_area_struct *vma;
88
89         /*
90          * Map GDT below 4GB, where the processor can find it.  We need to map
91          * it with privilege level 3 because the IVE uses non-privileged accesses to these
92          * tables.  IA-32 segmentation is used to protect against IA-32 accesses to them.
93          */
94         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
95         if (vma) {
96                 memset(vma, 0, sizeof(*vma));
97                 vma->vm_mm = current->mm;
98                 vma->vm_start = IA32_GDT_OFFSET;
99                 vma->vm_end = vma->vm_start + PAGE_SIZE;
100                 vma->vm_page_prot = PAGE_SHARED;
101                 vma->vm_flags = VM_READ|VM_MAYREAD|VM_RESERVED;
102                 vma->vm_ops = &ia32_shared_page_vm_ops;
103                 down_write(&current->mm->mmap_sem);
104                 {
105                         if (insert_vm_struct(current->mm, vma)) {
106                                 kmem_cache_free(vm_area_cachep, vma);
107                                 up_write(&current->mm->mmap_sem);
108                                 BUG();
109                         }
110                 }
111                 up_write(&current->mm->mmap_sem);
112         }
113
114         /*
115          * When user stack is not executable, push sigreturn code to stack makes
116          * segmentation fault raised when returning to kernel. So now sigreturn
117          * code is locked in specific gate page, which is pointed by pretcode
118          * when setup_frame_ia32
119          */
120         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
121         if (vma) {
122                 memset(vma, 0, sizeof(*vma));
123                 vma->vm_mm = current->mm;
124                 vma->vm_start = IA32_GATE_OFFSET;
125                 vma->vm_end = vma->vm_start + PAGE_SIZE;
126                 vma->vm_page_prot = PAGE_COPY_EXEC;
127                 vma->vm_flags = VM_READ | VM_MAYREAD | VM_EXEC
128                                 | VM_MAYEXEC | VM_RESERVED;
129                 vma->vm_ops = &ia32_gate_page_vm_ops;
130                 down_write(&current->mm->mmap_sem);
131                 {
132                         if (insert_vm_struct(current->mm, vma)) {
133                                 kmem_cache_free(vm_area_cachep, vma);
134                                 up_write(&current->mm->mmap_sem);
135                                 BUG();
136                         }
137                 }
138                 up_write(&current->mm->mmap_sem);
139         }
140
141         /*
142          * Install LDT as anonymous memory.  This gives us all-zero segment descriptors
143          * until a task modifies them via modify_ldt().
144          */
145         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
146         if (vma) {
147                 memset(vma, 0, sizeof(*vma));
148                 vma->vm_mm = current->mm;
149                 vma->vm_start = IA32_LDT_OFFSET;
150                 vma->vm_end = vma->vm_start + PAGE_ALIGN(IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE);
151                 vma->vm_page_prot = PAGE_SHARED;
152                 vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE;
153                 down_write(&current->mm->mmap_sem);
154                 {
155                         if (insert_vm_struct(current->mm, vma)) {
156                                 kmem_cache_free(vm_area_cachep, vma);
157                                 up_write(&current->mm->mmap_sem);
158                                 BUG();
159                         }
160                 }
161                 up_write(&current->mm->mmap_sem);
162         }
163
164         ia64_psr(regs)->ac = 0;         /* turn off alignment checking */
165         regs->loadrs = 0;
166         /*
167          *  According to the ABI %edx points to an `atexit' handler.  Since we don't have
168          *  one we'll set it to 0 and initialize all the other registers just to make
169          *  things more deterministic, ala the i386 implementation.
170          */
171         regs->r8 = 0;   /* %eax */
172         regs->r11 = 0;  /* %ebx */
173         regs->r9 = 0;   /* %ecx */
174         regs->r10 = 0;  /* %edx */
175         regs->r13 = 0;  /* %ebp */
176         regs->r14 = 0;  /* %esi */
177         regs->r15 = 0;  /* %edi */
178
179         current->thread.eflag = IA32_EFLAG;
180         current->thread.fsr = IA32_FSR_DEFAULT;
181         current->thread.fcr = IA32_FCR_DEFAULT;
182         current->thread.fir = 0;
183         current->thread.fdr = 0;
184
185         /*
186          * Setup GDTD.  Note: GDTD is the descrambled version of the pseudo-descriptor
187          * format defined by Figure 3-11 "Pseudo-Descriptor Format" in the IA-32
188          * architecture manual. Also note that the only fields that are not ignored are
189          * `base', `limit', 'G', `P' (must be 1) and `S' (must be 0).
190          */
191         regs->r31 = IA32_SEG_UNSCRAMBLE(IA32_SEG_DESCRIPTOR(IA32_GDT_OFFSET, IA32_PAGE_SIZE - 1,
192                                                             0, 0, 0, 1, 0, 0, 0));
193         /* Setup the segment selectors */
194         regs->r16 = (__USER_DS << 16) | __USER_DS; /* ES == DS, GS, FS are zero */
195         regs->r17 = (__USER_DS << 16) | __USER_CS; /* SS, CS; ia32_load_state() sets TSS and LDT */
196
197         ia32_load_segment_descriptors(current);
198         ia32_load_state(current);
199 }
200
201 int
202 ia32_setup_arg_pages (struct linux_binprm *bprm, int executable_stack)
203 {
204         unsigned long stack_base;
205         struct vm_area_struct *mpnt;
206         struct mm_struct *mm = current->mm;
207         int i, ret;
208
209         stack_base = IA32_STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
210         mm->arg_start = bprm->p + stack_base;
211
212         bprm->p += stack_base;
213         if (bprm->loader)
214                 bprm->loader += stack_base;
215         bprm->exec += stack_base;
216
217         mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
218         if (!mpnt)
219                 return -ENOMEM;
220
221         memset(mpnt, 0, sizeof(*mpnt));
222
223         down_write(&current->mm->mmap_sem);
224         {
225                 mpnt->vm_mm = current->mm;
226                 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
227                 mpnt->vm_end = IA32_STACK_TOP;
228                 if (executable_stack == EXSTACK_ENABLE_X)
229                         mpnt->vm_flags = VM_STACK_FLAGS |  VM_EXEC;
230                 else if (executable_stack == EXSTACK_DISABLE_X)
231                         mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
232                 else
233                         mpnt->vm_flags = VM_STACK_FLAGS;
234                 mpnt->vm_page_prot = (mpnt->vm_flags & VM_EXEC)?
235                                         PAGE_COPY_EXEC: PAGE_COPY;
236                 if ((ret = insert_vm_struct(current->mm, mpnt))) {
237                         up_write(&current->mm->mmap_sem);
238                         kmem_cache_free(vm_area_cachep, mpnt);
239                         return ret;
240                 }
241                 current->mm->stack_vm = current->mm->total_vm = vma_pages(mpnt);
242         }
243
244         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
245                 struct page *page = bprm->page[i];
246                 if (page) {
247                         bprm->page[i] = NULL;
248                         install_arg_page(mpnt, page, stack_base);
249                 }
250                 stack_base += PAGE_SIZE;
251         }
252         up_write(&current->mm->mmap_sem);
253
254         /* Can't do it in ia64_elf32_init(). Needs to be done before calls to
255            elf32_map() */
256         current->thread.ppl = ia32_init_pp_list();
257
258         return 0;
259 }
260
261 static void
262 elf32_set_personality (void)
263 {
264         set_personality(PER_LINUX32);
265         current->thread.map_base  = IA32_PAGE_OFFSET/3;
266 }
267
268 static unsigned long
269 elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
270 {
271         unsigned long pgoff = (eppnt->p_vaddr) & ~IA32_PAGE_MASK;
272
273         return ia32_do_mmap(filep, (addr & IA32_PAGE_MASK), eppnt->p_filesz + pgoff, prot, type,
274                             eppnt->p_offset - pgoff);
275 }
276
277 #define cpu_uses_ia32el()       (local_cpu_data->family > 0x1f)
278
279 static int __init check_elf32_binfmt(void)
280 {
281         if (cpu_uses_ia32el()) {
282                 printk("Please use IA-32 EL for executing IA-32 binaries\n");
283                 return unregister_binfmt(&elf_format);
284         }
285         return 0;
286 }
287
288 module_init(check_elf32_binfmt)