x86_64: Add vDSO for x86-64 with gettimeofday/clock_gettime/getcpu
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86_64 / ia32 / ia32_binfmt.c
blobb70f3e7cf06ca7a8943b4d90a7027c6c7e8a42cf
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
10 #undef ELF_CLASS
11 #define ELF_CLASS ELFCLASS32
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>
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>
34 #define ELF_NAME "elf/i386"
36 #define AT_SYSINFO 32
37 #define AT_SYSINFO_EHDR 33
39 int sysctl_vsyscall32 = 1;
41 #undef ARCH_DLINFO
42 #define ARCH_DLINFO do { \
43 if (sysctl_vsyscall32) { \
44 NEW_AUX_ENT(AT_SYSINFO, (u32)(u64)VSYSCALL32_VSYSCALL); \
45 NEW_AUX_ENT(AT_SYSINFO_EHDR, VSYSCALL32_BASE); \
46 } \
47 } while(0)
49 struct file;
50 struct elf_phdr;
52 #define IA32_EMULATOR 1
54 #define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x1000000)
56 #undef ELF_ARCH
57 #define ELF_ARCH EM_386
59 #define ELF_DATA ELFDATA2LSB
61 #define USE_ELF_CORE_DUMP 1
63 /* Override elfcore.h */
64 #define _LINUX_ELFCORE_H 1
65 typedef unsigned int elf_greg_t;
67 #define ELF_NGREG (sizeof (struct user_regs_struct32) / sizeof(elf_greg_t))
68 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
70 struct elf_siginfo
72 int si_signo; /* signal number */
73 int si_code; /* extra code */
74 int si_errno; /* errno */
77 #define jiffies_to_timeval(a,b) do { (b)->tv_usec = 0; (b)->tv_sec = (a)/HZ; }while(0)
79 struct elf_prstatus
81 struct elf_siginfo pr_info; /* Info associated with signal */
82 short pr_cursig; /* Current signal */
83 unsigned int pr_sigpend; /* Set of pending signals */
84 unsigned int pr_sighold; /* Set of held signals */
85 pid_t pr_pid;
86 pid_t pr_ppid;
87 pid_t pr_pgrp;
88 pid_t pr_sid;
89 struct compat_timeval pr_utime; /* User time */
90 struct compat_timeval pr_stime; /* System time */
91 struct compat_timeval pr_cutime; /* Cumulative user time */
92 struct compat_timeval pr_cstime; /* Cumulative system time */
93 elf_gregset_t pr_reg; /* GP registers */
94 int pr_fpvalid; /* True if math co-processor being used. */
97 #define ELF_PRARGSZ (80) /* Number of chars for args */
99 struct elf_prpsinfo
101 char pr_state; /* numeric process state */
102 char pr_sname; /* char for pr_state */
103 char pr_zomb; /* zombie */
104 char pr_nice; /* nice val */
105 unsigned int pr_flag; /* flags */
106 __u16 pr_uid;
107 __u16 pr_gid;
108 pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
109 /* Lots missing */
110 char pr_fname[16]; /* filename of executable */
111 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
114 #define __STR(x) #x
115 #define STR(x) __STR(x)
117 #define _GET_SEG(x) \
118 ({ __u32 seg; asm("movl %%" STR(x) ",%0" : "=r"(seg)); seg; })
120 /* Assumes current==process to be dumped */
121 #define ELF_CORE_COPY_REGS(pr_reg, regs) \
122 pr_reg[0] = regs->rbx; \
123 pr_reg[1] = regs->rcx; \
124 pr_reg[2] = regs->rdx; \
125 pr_reg[3] = regs->rsi; \
126 pr_reg[4] = regs->rdi; \
127 pr_reg[5] = regs->rbp; \
128 pr_reg[6] = regs->rax; \
129 pr_reg[7] = _GET_SEG(ds); \
130 pr_reg[8] = _GET_SEG(es); \
131 pr_reg[9] = _GET_SEG(fs); \
132 pr_reg[10] = _GET_SEG(gs); \
133 pr_reg[11] = regs->orig_rax; \
134 pr_reg[12] = regs->rip; \
135 pr_reg[13] = regs->cs; \
136 pr_reg[14] = regs->eflags; \
137 pr_reg[15] = regs->rsp; \
138 pr_reg[16] = regs->ss;
140 #define user user32
142 #undef elf_read_implies_exec
143 #define elf_read_implies_exec(ex, executable_stack) (executable_stack != EXSTACK_DISABLE_X)
144 //#include <asm/ia32.h>
145 #include <linux/elf.h>
147 typedef struct user_i387_ia32_struct elf_fpregset_t;
148 typedef struct user32_fxsr_struct elf_fpxregset_t;
151 static inline void elf_core_copy_regs(elf_gregset_t *elfregs, struct pt_regs *regs)
153 ELF_CORE_COPY_REGS((*elfregs), regs)
156 static inline int elf_core_copy_task_regs(struct task_struct *t, elf_gregset_t* elfregs)
158 struct pt_regs *pp = task_pt_regs(t);
159 ELF_CORE_COPY_REGS((*elfregs), pp);
160 /* fix wrong segments */
161 (*elfregs)[7] = t->thread.ds;
162 (*elfregs)[9] = t->thread.fsindex;
163 (*elfregs)[10] = t->thread.gsindex;
164 (*elfregs)[8] = t->thread.es;
165 return 1;
168 static inline int
169 elf_core_copy_task_fpregs(struct task_struct *tsk, struct pt_regs *regs, elf_fpregset_t *fpu)
171 struct _fpstate_ia32 *fpstate = (void*)fpu;
172 mm_segment_t oldfs = get_fs();
174 if (!tsk_used_math(tsk))
175 return 0;
176 if (!regs)
177 regs = task_pt_regs(tsk);
178 if (tsk == current)
179 unlazy_fpu(tsk);
180 set_fs(KERNEL_DS);
181 save_i387_ia32(tsk, fpstate, regs, 1);
182 /* Correct for i386 bug. It puts the fop into the upper 16bits of
183 the tag word (like FXSAVE), not into the fcs*/
184 fpstate->cssel |= fpstate->tag & 0xffff0000;
185 set_fs(oldfs);
186 return 1;
189 #define ELF_CORE_COPY_XFPREGS 1
190 static inline int
191 elf_core_copy_task_xfpregs(struct task_struct *t, elf_fpxregset_t *xfpu)
193 struct pt_regs *regs = task_pt_regs(t);
194 if (!tsk_used_math(t))
195 return 0;
196 if (t == current)
197 unlazy_fpu(t);
198 memcpy(xfpu, &t->thread.i387.fxsave, sizeof(elf_fpxregset_t));
199 xfpu->fcs = regs->cs;
200 xfpu->fos = t->thread.ds; /* right? */
201 return 1;
204 #undef elf_check_arch
205 #define elf_check_arch(x) \
206 ((x)->e_machine == EM_386)
208 extern int force_personality32;
210 #define ELF_EXEC_PAGESIZE PAGE_SIZE
211 #define ELF_HWCAP (boot_cpu_data.x86_capability[0])
212 #define ELF_PLATFORM ("i686")
213 #define SET_PERSONALITY(ex, ibcs2) \
214 do { \
215 unsigned long new_flags = 0; \
216 if ((ex).e_ident[EI_CLASS] == ELFCLASS32) \
217 new_flags = _TIF_IA32; \
218 if ((current_thread_info()->flags & _TIF_IA32) \
219 != new_flags) \
220 set_thread_flag(TIF_ABI_PENDING); \
221 else \
222 clear_thread_flag(TIF_ABI_PENDING); \
223 /* XXX This overwrites the user set personality */ \
224 current->personality |= force_personality32; \
225 } while (0)
227 /* Override some function names */
228 #define elf_format elf32_format
230 #define init_elf_binfmt init_elf32_binfmt
231 #define exit_elf_binfmt exit_elf32_binfmt
233 #define load_elf_binary load_elf32_binary
235 #define ELF_PLAT_INIT(r, load_addr) elf32_init(r)
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)
251 #include <linux/module.h>
253 MODULE_DESCRIPTION("Binary format loader for compatibility with IA32 ELF binaries.");
254 MODULE_AUTHOR("Eric Youngdale, Andi Kleen");
256 #undef MODULE_DESCRIPTION
257 #undef MODULE_AUTHOR
259 static void elf32_init(struct pt_regs *);
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);
265 #include "../../../fs/binfmt_elf.c"
267 static void elf32_init(struct pt_regs *regs)
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;
287 #ifdef CONFIG_SYSCTL
288 /* Register vsyscall32 into the ABI table */
289 #include <linux/sysctl.h>
291 static ctl_table abi_table2[] = {
293 .ctl_name = 99,
294 .procname = "vsyscall32",
295 .data = &sysctl_vsyscall32,
296 .maxlen = sizeof(int),
297 .mode = 0644,
298 .proc_handler = proc_dointvec
303 static ctl_table abi_root_table2[] = {
305 .ctl_name = CTL_ABI,
306 .procname = "abi",
307 .mode = 0555,
308 .child = abi_table2
313 static __init int ia32_binfmt_init(void)
315 register_sysctl_table(abi_root_table2);
316 return 0;
318 __initcall(ia32_binfmt_init);
319 #endif