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