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