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