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