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