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