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