8b2a41592746e8624adf811e4ee207b19e69989a
[linux-flexiantxendom0-3.2.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 #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
23 #define CONFIG_BINFMT_ELF32
24
25 /* Override some function names */
26 #undef start_thread
27 #define start_thread                    ia32_start_thread
28 #define elf_format                      elf32_format
29 #define init_elf_binfmt                 init_elf32_binfmt
30 #define exit_elf_binfmt                 exit_elf32_binfmt
31
32 #undef CONFIG_BINFMT_ELF
33 #ifdef CONFIG_BINFMT_ELF32
34 # define CONFIG_BINFMT_ELF              CONFIG_BINFMT_ELF32
35 #endif
36
37 #undef CONFIG_BINFMT_ELF_MODULE
38 #ifdef CONFIG_BINFMT_ELF32_MODULE
39 # define CONFIG_BINFMT_ELF_MODULE       CONFIG_BINFMT_ELF32_MODULE
40 #endif
41
42 #undef CLOCKS_PER_SEC
43 #define CLOCKS_PER_SEC  IA32_CLOCKS_PER_SEC
44
45 extern void ia64_elf32_init (struct pt_regs *regs);
46
47 static void elf32_set_personality (void);
48
49 #define setup_arg_pages(bprm)           ia32_setup_arg_pages(bprm)
50 #define elf_map                         elf32_map
51
52 #undef SET_PERSONALITY
53 #define SET_PERSONALITY(ex, ibcs2)      elf32_set_personality()
54
55 /* Ugly but avoids duplication */
56 #include "../../../fs/binfmt_elf.c"
57
58 extern struct page *ia32_shared_page[];
59 extern unsigned long *ia32_gdt;
60
61 struct page *
62 ia32_install_shared_page (struct vm_area_struct *vma, unsigned long address, int no_share)
63 {
64         struct page *pg = ia32_shared_page[(address - vma->vm_start)/PAGE_SIZE];
65
66         get_page(pg);
67         return pg;
68 }
69
70 static struct vm_operations_struct ia32_shared_page_vm_ops = {
71         .nopage =ia32_install_shared_page
72 };
73
74 void
75 ia64_elf32_init (struct pt_regs *regs)
76 {
77         struct vm_area_struct *vma;
78
79         /*
80          * Map GDT and TSS below 4GB, where the processor can find them.  We need to map
81          * it with privilege level 3 because the IVE uses non-privileged accesses to these
82          * tables.  IA-32 segmentation is used to protect against IA-32 accesses to them.
83          */
84         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
85         if (vma) {
86                 vma->vm_mm = current->mm;
87                 vma->vm_start = IA32_GDT_OFFSET;
88                 vma->vm_end = vma->vm_start + max(PAGE_SIZE, 2*IA32_PAGE_SIZE);
89                 vma->vm_page_prot = PAGE_SHARED;
90                 vma->vm_flags = VM_READ|VM_MAYREAD;
91                 vma->vm_ops = &ia32_shared_page_vm_ops;
92                 vma->vm_pgoff = 0;
93                 vma->vm_file = NULL;
94                 vma->vm_private_data = NULL;
95                 down_write(&current->mm->mmap_sem);
96                 {
97                         insert_vm_struct(current->mm, vma);
98                 }
99                 up_write(&current->mm->mmap_sem);
100         }
101
102         /*
103          * Install LDT as anonymous memory.  This gives us all-zero segment descriptors
104          * until a task modifies them via modify_ldt().
105          */
106         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
107         if (vma) {
108                 vma->vm_mm = current->mm;
109                 vma->vm_start = IA32_LDT_OFFSET;
110                 vma->vm_end = vma->vm_start + PAGE_ALIGN(IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE);
111                 vma->vm_page_prot = PAGE_SHARED;
112                 vma->vm_flags = VM_READ|VM_WRITE|VM_MAYREAD|VM_MAYWRITE;
113                 vma->vm_ops = NULL;
114                 vma->vm_pgoff = 0;
115                 vma->vm_file = NULL;
116                 vma->vm_private_data = NULL;
117                 down_write(&current->mm->mmap_sem);
118                 {
119                         insert_vm_struct(current->mm, vma);
120                 }
121                 up_write(&current->mm->mmap_sem);
122         }
123
124         ia64_psr(regs)->ac = 0;         /* turn off alignment checking */
125         regs->loadrs = 0;
126         /*
127          *  According to the ABI %edx points to an `atexit' handler.  Since we don't have
128          *  one we'll set it to 0 and initialize all the other registers just to make
129          *  things more deterministic, ala the i386 implementation.
130          */
131         regs->r8 = 0;   /* %eax */
132         regs->r11 = 0;  /* %ebx */
133         regs->r9 = 0;   /* %ecx */
134         regs->r10 = 0;  /* %edx */
135         regs->r13 = 0;  /* %ebp */
136         regs->r14 = 0;  /* %esi */
137         regs->r15 = 0;  /* %edi */
138
139         current->thread.eflag = IA32_EFLAG;
140         current->thread.fsr = IA32_FSR_DEFAULT;
141         current->thread.fcr = IA32_FCR_DEFAULT;
142         current->thread.fir = 0;
143         current->thread.fdr = 0;
144
145         /*
146          * Setup GDTD.  Note: GDTD is the descrambled version of the pseudo-descriptor
147          * format defined by Figure 3-11 "Pseudo-Descriptor Format" in the IA-32
148          * architecture manual. Also note that the only fields that are not ignored are
149          * `base', `limit', 'G', `P' (must be 1) and `S' (must be 0).
150          */
151         regs->r31 = IA32_SEG_UNSCRAMBLE(IA32_SEG_DESCRIPTOR(IA32_GDT_OFFSET, IA32_PAGE_SIZE - 1,
152                                                             0, 0, 0, 1, 0, 0, 0));
153         /* Setup the segment selectors */
154         regs->r16 = (__USER_DS << 16) | __USER_DS; /* ES == DS, GS, FS are zero */
155         regs->r17 = (__USER_DS << 16) | __USER_CS; /* SS, CS; ia32_load_state() sets TSS and LDT */
156
157         ia32_load_segment_descriptors(current);
158         ia32_load_state(current);
159 }
160
161 int
162 ia32_setup_arg_pages (struct linux_binprm *bprm)
163 {
164         unsigned long stack_base;
165         struct vm_area_struct *mpnt;
166         struct mm_struct *mm = current->mm;
167         int i;
168
169         stack_base = IA32_STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
170         mm->arg_start = bprm->p + stack_base;
171
172         bprm->p += stack_base;
173         if (bprm->loader)
174                 bprm->loader += stack_base;
175         bprm->exec += stack_base;
176
177         mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
178         if (!mpnt)
179                 return -ENOMEM;
180
181         if (security_vm_enough_memory((IA32_STACK_TOP - (PAGE_MASK & (unsigned long) bprm->p))>>PAGE_SHIFT)) {
182                 kmem_cache_free(vm_area_cachep, mpnt);
183                 return -ENOMEM;
184         }
185
186         down_write(&current->mm->mmap_sem);
187         {
188                 mpnt->vm_mm = current->mm;
189                 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
190                 mpnt->vm_end = IA32_STACK_TOP;
191                 mpnt->vm_page_prot = PAGE_COPY;
192                 mpnt->vm_flags = VM_STACK_FLAGS;
193                 mpnt->vm_ops = NULL;
194                 mpnt->vm_pgoff = 0;
195                 mpnt->vm_file = NULL;
196                 mpnt->vm_private_data = 0;
197                 insert_vm_struct(current->mm, mpnt);
198                 current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
199         }
200
201         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
202                 struct page *page = bprm->page[i];
203                 if (page) {
204                         bprm->page[i] = NULL;
205                         put_dirty_page(current, page, stack_base, PAGE_COPY);
206                 }
207                 stack_base += PAGE_SIZE;
208         }
209         up_write(&current->mm->mmap_sem);
210
211         return 0;
212 }
213
214 static void
215 elf32_set_personality (void)
216 {
217         set_personality(PER_LINUX32);
218         current->thread.map_base  = IA32_PAGE_OFFSET/3;
219         current->thread.task_size = IA32_PAGE_OFFSET;   /* use what Linux/x86 uses... */
220         current->thread.flags |= IA64_THREAD_XSTACK;    /* data must be executable */
221         set_fs(USER_DS);                                /* set addr limit for new TASK_SIZE */
222 }
223
224 static unsigned long
225 elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
226 {
227         unsigned long pgoff = (eppnt->p_vaddr) & ~IA32_PAGE_MASK;
228
229         return ia32_do_mmap(filep, (addr & IA32_PAGE_MASK), eppnt->p_filesz + pgoff, prot, type,
230                             eppnt->p_offset - pgoff);
231 }