Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[linux-2.6] / arch / x86_64 / ia32 / ia32_binfmt.c
1 /* 
2  * Written 2000,2002 by Andi Kleen. 
3  * 
4  * Loosely based on the sparc64 and IA64 32bit emulation loaders.
5  * This tricks binfmt_elf.c into loading 32bit binaries using lots 
6  * of ugly preprocessor tricks. Talk about very very poor man's inheritance.
7  */ 
8 #include <linux/types.h>
9 #include <linux/stddef.h>
10 #include <linux/rwsem.h>
11 #include <linux/sched.h>
12 #include <linux/compat.h>
13 #include <linux/string.h>
14 #include <linux/binfmts.h>
15 #include <linux/mm.h>
16 #include <linux/security.h>
17
18 #include <asm/segment.h> 
19 #include <asm/ptrace.h>
20 #include <asm/processor.h>
21 #include <asm/user32.h>
22 #include <asm/sigcontext32.h>
23 #include <asm/fpu32.h>
24 #include <asm/i387.h>
25 #include <asm/uaccess.h>
26 #include <asm/ia32.h>
27 #include <asm/vsyscall32.h>
28
29 #define ELF_NAME "elf/i386"
30
31 #define AT_SYSINFO 32
32 #define AT_SYSINFO_EHDR         33
33
34 int sysctl_vsyscall32 = 1;
35
36 #define ARCH_DLINFO do {  \
37         if (sysctl_vsyscall32) { \
38         NEW_AUX_ENT(AT_SYSINFO, (u32)(u64)VSYSCALL32_VSYSCALL); \
39         NEW_AUX_ENT(AT_SYSINFO_EHDR, VSYSCALL32_BASE);    \
40         }       \
41 } while(0)
42
43 struct file;
44 struct elf_phdr; 
45
46 #define IA32_EMULATOR 1
47
48 #define ELF_ET_DYN_BASE         (TASK_UNMAPPED_BASE + 0x1000000)
49
50 #undef ELF_ARCH
51 #define ELF_ARCH EM_386
52
53 #undef ELF_CLASS
54 #define ELF_CLASS ELFCLASS32
55
56 #define ELF_DATA        ELFDATA2LSB
57
58 #define USE_ELF_CORE_DUMP 1
59
60 /* Override elfcore.h */ 
61 #define _LINUX_ELFCORE_H 1
62 typedef unsigned int elf_greg_t;
63
64 #define ELF_NGREG (sizeof (struct user_regs_struct32) / sizeof(elf_greg_t))
65 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
66
67 struct elf_siginfo
68 {
69         int     si_signo;                       /* signal number */
70         int     si_code;                        /* extra code */
71         int     si_errno;                       /* errno */
72 };
73
74 #define jiffies_to_timeval(a,b) do { (b)->tv_usec = 0; (b)->tv_sec = (a)/HZ; }while(0)
75
76 struct elf_prstatus
77 {
78         struct elf_siginfo pr_info;     /* Info associated with signal */
79         short   pr_cursig;              /* Current signal */
80         unsigned int pr_sigpend;        /* Set of pending signals */
81         unsigned int pr_sighold;        /* Set of held signals */
82         pid_t   pr_pid;
83         pid_t   pr_ppid;
84         pid_t   pr_pgrp;
85         pid_t   pr_sid;
86         struct compat_timeval pr_utime; /* User time */
87         struct compat_timeval pr_stime; /* System time */
88         struct compat_timeval pr_cutime;        /* Cumulative user time */
89         struct compat_timeval pr_cstime;        /* Cumulative system time */
90         elf_gregset_t pr_reg;   /* GP registers */
91         int pr_fpvalid;         /* True if math co-processor being used.  */
92 };
93
94 #define ELF_PRARGSZ     (80)    /* Number of chars for args */
95
96 struct elf_prpsinfo
97 {
98         char    pr_state;       /* numeric process state */
99         char    pr_sname;       /* char for pr_state */
100         char    pr_zomb;        /* zombie */
101         char    pr_nice;        /* nice val */
102         unsigned int pr_flag;   /* flags */
103         __u16   pr_uid;
104         __u16   pr_gid;
105         pid_t   pr_pid, pr_ppid, pr_pgrp, pr_sid;
106         /* Lots missing */
107         char    pr_fname[16];   /* filename of executable */
108         char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
109 };
110
111 #define __STR(x) #x
112 #define STR(x) __STR(x)
113
114 #define _GET_SEG(x) \
115         ({ __u32 seg; asm("movl %%" STR(x) ",%0" : "=r"(seg)); seg; })
116
117 /* Assumes current==process to be dumped */
118 #define ELF_CORE_COPY_REGS(pr_reg, regs)                \
119         pr_reg[0] = regs->rbx;                          \
120         pr_reg[1] = regs->rcx;                          \
121         pr_reg[2] = regs->rdx;                          \
122         pr_reg[3] = regs->rsi;                          \
123         pr_reg[4] = regs->rdi;                          \
124         pr_reg[5] = regs->rbp;                          \
125         pr_reg[6] = regs->rax;                          \
126         pr_reg[7] = _GET_SEG(ds);                       \
127         pr_reg[8] = _GET_SEG(es);                       \
128         pr_reg[9] = _GET_SEG(fs);                       \
129         pr_reg[10] = _GET_SEG(gs);                      \
130         pr_reg[11] = regs->orig_rax;                    \
131         pr_reg[12] = regs->rip;                         \
132         pr_reg[13] = regs->cs;                          \
133         pr_reg[14] = regs->eflags;                      \
134         pr_reg[15] = regs->rsp;                         \
135         pr_reg[16] = regs->ss;
136
137 #define user user32
138
139 #define __ASM_X86_64_ELF_H 1
140 #define elf_read_implies_exec(ex, executable_stack)     (executable_stack != EXSTACK_DISABLE_X)
141 //#include <asm/ia32.h>
142 #include <linux/elf.h>
143
144 typedef struct user_i387_ia32_struct elf_fpregset_t;
145 typedef struct user32_fxsr_struct elf_fpxregset_t;
146
147
148 static inline void elf_core_copy_regs(elf_gregset_t *elfregs, struct pt_regs *regs)
149 {
150         ELF_CORE_COPY_REGS((*elfregs), regs)
151 }
152
153 static inline int elf_core_copy_task_regs(struct task_struct *t, elf_gregset_t* elfregs)
154 {       
155         struct pt_regs *pp = task_pt_regs(t);
156         ELF_CORE_COPY_REGS((*elfregs), pp);
157         /* fix wrong segments */ 
158         (*elfregs)[7] = t->thread.ds; 
159         (*elfregs)[9] = t->thread.fsindex; 
160         (*elfregs)[10] = t->thread.gsindex; 
161         (*elfregs)[8] = t->thread.es;   
162         return 1; 
163 }
164
165 static inline int 
166 elf_core_copy_task_fpregs(struct task_struct *tsk, struct pt_regs *regs, elf_fpregset_t *fpu)
167 {
168         struct _fpstate_ia32 *fpstate = (void*)fpu; 
169         mm_segment_t oldfs = get_fs();
170
171         if (!tsk_used_math(tsk))
172                 return 0;
173         if (!regs)
174                 regs = task_pt_regs(tsk);
175         if (tsk == current)
176                 unlazy_fpu(tsk);
177         set_fs(KERNEL_DS); 
178         save_i387_ia32(tsk, fpstate, regs, 1);
179         /* Correct for i386 bug. It puts the fop into the upper 16bits of 
180            the tag word (like FXSAVE), not into the fcs*/ 
181         fpstate->cssel |= fpstate->tag & 0xffff0000; 
182         set_fs(oldfs); 
183         return 1; 
184 }
185
186 #define ELF_CORE_COPY_XFPREGS 1
187 static inline int 
188 elf_core_copy_task_xfpregs(struct task_struct *t, elf_fpxregset_t *xfpu)
189 {
190         struct pt_regs *regs = task_pt_regs(t);
191         if (!tsk_used_math(t))
192                 return 0;
193         if (t == current)
194                 unlazy_fpu(t); 
195         memcpy(xfpu, &t->thread.i387.fxsave, sizeof(elf_fpxregset_t));
196         xfpu->fcs = regs->cs; 
197         xfpu->fos = t->thread.ds; /* right? */ 
198         return 1;
199 }
200
201 #undef elf_check_arch
202 #define elf_check_arch(x) \
203         ((x)->e_machine == EM_386)
204
205 extern int force_personality32;
206
207 #define ELF_EXEC_PAGESIZE PAGE_SIZE
208 #define ELF_HWCAP (boot_cpu_data.x86_capability[0])
209 #define ELF_PLATFORM  ("i686")
210 #define SET_PERSONALITY(ex, ibcs2)                      \
211 do {                                                    \
212         unsigned long new_flags = 0;                            \
213         if ((ex).e_ident[EI_CLASS] == ELFCLASS32)               \
214                 new_flags = _TIF_IA32;                          \
215         if ((current_thread_info()->flags & _TIF_IA32)          \
216             != new_flags)                                       \
217                 set_thread_flag(TIF_ABI_PENDING);               \
218         else                                                    \
219                 clear_thread_flag(TIF_ABI_PENDING);             \
220         /* XXX This overwrites the user set personality */      \
221         current->personality |= force_personality32;            \
222 } while (0)
223
224 /* Override some function names */
225 #define elf_format                      elf32_format
226
227 #define init_elf_binfmt                 init_elf32_binfmt
228 #define exit_elf_binfmt                 exit_elf32_binfmt
229
230 #define load_elf_binary load_elf32_binary
231
232 #define ELF_PLAT_INIT(r, load_addr)     elf32_init(r)
233 #define setup_arg_pages(bprm, stack_top, exec_stack) \
234         ia32_setup_arg_pages(bprm, stack_top, exec_stack)
235 int ia32_setup_arg_pages(struct linux_binprm *bprm, unsigned long stack_top, int executable_stack);
236
237 #undef start_thread
238 #define start_thread(regs,new_rip,new_rsp) do { \
239         asm volatile("movl %0,%%fs" :: "r" (0)); \
240         asm volatile("movl %0,%%es; movl %0,%%ds": :"r" (__USER32_DS)); \
241         load_gs_index(0); \
242         (regs)->rip = (new_rip); \
243         (regs)->rsp = (new_rsp); \
244         (regs)->eflags = 0x200; \
245         (regs)->cs = __USER32_CS; \
246         (regs)->ss = __USER32_DS; \
247         set_fs(USER_DS); \
248 } while(0) 
249
250
251 #include <linux/module.h>
252
253 MODULE_DESCRIPTION("Binary format loader for compatibility with IA32 ELF binaries."); 
254 MODULE_AUTHOR("Eric Youngdale, Andi Kleen");
255
256 #undef MODULE_DESCRIPTION
257 #undef MODULE_AUTHOR
258
259 static void elf32_init(struct pt_regs *);
260
261 #define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
262 #define arch_setup_additional_pages syscall32_setup_pages
263 extern int syscall32_setup_pages(struct linux_binprm *, int exstack);
264
265 #include "../../../fs/binfmt_elf.c" 
266
267 static void elf32_init(struct pt_regs *regs)
268 {
269         struct task_struct *me = current; 
270         regs->rdi = 0;
271         regs->rsi = 0;
272         regs->rdx = 0;
273         regs->rcx = 0;
274         regs->rax = 0;
275         regs->rbx = 0; 
276         regs->rbp = 0; 
277         regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
278                 regs->r13 = regs->r14 = regs->r15 = 0; 
279     me->thread.fs = 0; 
280         me->thread.gs = 0;
281         me->thread.fsindex = 0; 
282         me->thread.gsindex = 0;
283     me->thread.ds = __USER_DS; 
284         me->thread.es = __USER_DS;
285 }
286
287 int ia32_setup_arg_pages(struct linux_binprm *bprm, unsigned long stack_top,
288                          int executable_stack)
289 {
290         unsigned long stack_base;
291         struct vm_area_struct *mpnt;
292         struct mm_struct *mm = current->mm;
293         int i, ret;
294
295         stack_base = stack_top - MAX_ARG_PAGES * PAGE_SIZE;
296         mm->arg_start = bprm->p + stack_base;
297
298         bprm->p += stack_base;
299         if (bprm->loader)
300                 bprm->loader += stack_base;
301         bprm->exec += stack_base;
302
303         mpnt = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
304         if (!mpnt) 
305                 return -ENOMEM; 
306
307         down_write(&mm->mmap_sem);
308         {
309                 mpnt->vm_mm = mm;
310                 mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
311                 mpnt->vm_end = stack_top;
312                 if (executable_stack == EXSTACK_ENABLE_X)
313                         mpnt->vm_flags = VM_STACK_FLAGS |  VM_EXEC;
314                 else if (executable_stack == EXSTACK_DISABLE_X)
315                         mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
316                 else
317                         mpnt->vm_flags = VM_STACK_FLAGS;
318                 mpnt->vm_page_prot = (mpnt->vm_flags & VM_EXEC) ? 
319                         PAGE_COPY_EXEC : PAGE_COPY;
320                 if ((ret = insert_vm_struct(mm, mpnt))) {
321                         up_write(&mm->mmap_sem);
322                         kmem_cache_free(vm_area_cachep, mpnt);
323                         return ret;
324                 }
325                 mm->stack_vm = mm->total_vm = vma_pages(mpnt);
326         } 
327
328         for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
329                 struct page *page = bprm->page[i];
330                 if (page) {
331                         bprm->page[i] = NULL;
332                         install_arg_page(mpnt, page, stack_base);
333                 }
334                 stack_base += PAGE_SIZE;
335         }
336         up_write(&mm->mmap_sem);
337         
338         return 0;
339 }
340 EXPORT_SYMBOL(ia32_setup_arg_pages);
341
342 #ifdef CONFIG_SYSCTL
343 /* Register vsyscall32 into the ABI table */
344 #include <linux/sysctl.h>
345
346 static ctl_table abi_table2[] = {
347         {
348                 .ctl_name       = 99,
349                 .procname       = "vsyscall32",
350                 .data           = &sysctl_vsyscall32,
351                 .maxlen         = sizeof(int),
352                 .mode           = 0644,
353                 .proc_handler   = proc_dointvec
354         },
355         {}
356 };
357
358 static ctl_table abi_root_table2[] = {
359         {
360                 .ctl_name = CTL_ABI,
361                 .procname = "abi",
362                 .mode = 0555,
363                 .child = abi_table2
364         },
365         {}
366 };
367
368 static __init int ia32_binfmt_init(void)
369
370         register_sysctl_table(abi_root_table2);
371         return 0;
372 }
373 __initcall(ia32_binfmt_init);
374 #endif