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