blob: 118b9f9ff499c251ca94bbc6e47d45233eba2c08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Ralf Baechle9f7290e2007-05-02 19:27:08 +02008#define __ASM_X86_64_ELF_H 1
9
10#undef ELF_CLASS
11#define ELF_CLASS ELFCLASS32
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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
39int sysctl_vsyscall32 = 1;
40
Andi Kleen2aae9502007-07-21 17:10:01 +020041#undef ARCH_DLINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define ARCH_DLINFO do { \
43 if (sysctl_vsyscall32) { \
Roland McGrath2ebc3cc2007-07-26 10:41:12 -070044 current->mm->context.vdso = (void *)VSYSCALL32_BASE; \
45 NEW_AUX_ENT(AT_SYSINFO, (u32)(u64)VSYSCALL32_VSYSCALL); \
46 NEW_AUX_ENT(AT_SYSINFO_EHDR, VSYSCALL32_BASE); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 } \
48} while(0)
49
50struct file;
51struct elf_phdr;
52
53#define IA32_EMULATOR 1
54
Suresh Siddha84929802005-06-21 17:14:32 -070055#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x1000000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#undef ELF_ARCH
58#define ELF_ARCH EM_386
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define ELF_DATA ELFDATA2LSB
61
62#define USE_ELF_CORE_DUMP 1
63
Dave Jonese6fc99c2006-03-25 16:30:16 +010064/* Override elfcore.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define _LINUX_ELFCORE_H 1
66typedef unsigned int elf_greg_t;
67
68#define ELF_NGREG (sizeof (struct user_regs_struct32) / sizeof(elf_greg_t))
69typedef elf_greg_t elf_gregset_t[ELF_NGREG];
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct elf_siginfo
72{
73 int si_signo; /* signal number */
74 int si_code; /* extra code */
75 int si_errno; /* errno */
76};
77
78#define jiffies_to_timeval(a,b) do { (b)->tv_usec = 0; (b)->tv_sec = (a)/HZ; }while(0)
79
80struct elf_prstatus
81{
82 struct elf_siginfo pr_info; /* Info associated with signal */
83 short pr_cursig; /* Current signal */
84 unsigned int pr_sigpend; /* Set of pending signals */
85 unsigned int pr_sighold; /* Set of held signals */
86 pid_t pr_pid;
87 pid_t pr_ppid;
88 pid_t pr_pgrp;
89 pid_t pr_sid;
90 struct compat_timeval pr_utime; /* User time */
91 struct compat_timeval pr_stime; /* System time */
92 struct compat_timeval pr_cutime; /* Cumulative user time */
93 struct compat_timeval pr_cstime; /* Cumulative system time */
94 elf_gregset_t pr_reg; /* GP registers */
95 int pr_fpvalid; /* True if math co-processor being used. */
96};
97
98#define ELF_PRARGSZ (80) /* Number of chars for args */
99
100struct elf_prpsinfo
101{
102 char pr_state; /* numeric process state */
103 char pr_sname; /* char for pr_state */
104 char pr_zomb; /* zombie */
105 char pr_nice; /* nice val */
106 unsigned int pr_flag; /* flags */
107 __u16 pr_uid;
108 __u16 pr_gid;
109 pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
110 /* Lots missing */
111 char pr_fname[16]; /* filename of executable */
112 char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
113};
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define _GET_SEG(x) \
Glauber de Oliveira Costa92b2dc72007-10-17 18:04:38 +0200116 ({ __u32 seg; asm("movl %%" __stringify(x) ",%0" : "=r"(seg)); seg; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* Assumes current==process to be dumped */
119#define ELF_CORE_COPY_REGS(pr_reg, regs) \
120 pr_reg[0] = regs->rbx; \
121 pr_reg[1] = regs->rcx; \
122 pr_reg[2] = regs->rdx; \
123 pr_reg[3] = regs->rsi; \
124 pr_reg[4] = regs->rdi; \
125 pr_reg[5] = regs->rbp; \
126 pr_reg[6] = regs->rax; \
127 pr_reg[7] = _GET_SEG(ds); \
128 pr_reg[8] = _GET_SEG(es); \
129 pr_reg[9] = _GET_SEG(fs); \
130 pr_reg[10] = _GET_SEG(gs); \
131 pr_reg[11] = regs->orig_rax; \
132 pr_reg[12] = regs->rip; \
133 pr_reg[13] = regs->cs; \
134 pr_reg[14] = regs->eflags; \
135 pr_reg[15] = regs->rsp; \
136 pr_reg[16] = regs->ss;
137
138#define user user32
139
Ralf Baechle9f7290e2007-05-02 19:27:08 +0200140#undef elf_read_implies_exec
Markus Schoder3391c222006-07-10 17:06:06 +0200141#define elf_read_implies_exec(ex, executable_stack) (executable_stack != EXSTACK_DISABLE_X)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142//#include <asm/ia32.h>
143#include <linux/elf.h>
144
145typedef struct user_i387_ia32_struct elf_fpregset_t;
146typedef struct user32_fxsr_struct elf_fpxregset_t;
147
148
149static inline void elf_core_copy_regs(elf_gregset_t *elfregs, struct pt_regs *regs)
150{
151 ELF_CORE_COPY_REGS((*elfregs), regs)
152}
153
154static inline int elf_core_copy_task_regs(struct task_struct *t, elf_gregset_t* elfregs)
155{
Al Virobb049232006-01-12 01:05:38 -0800156 struct pt_regs *pp = task_pt_regs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ELF_CORE_COPY_REGS((*elfregs), pp);
158 /* fix wrong segments */
159 (*elfregs)[7] = t->thread.ds;
160 (*elfregs)[9] = t->thread.fsindex;
161 (*elfregs)[10] = t->thread.gsindex;
162 (*elfregs)[8] = t->thread.es;
163 return 1;
164}
165
166static inline int
167elf_core_copy_task_fpregs(struct task_struct *tsk, struct pt_regs *regs, elf_fpregset_t *fpu)
168{
169 struct _fpstate_ia32 *fpstate = (void*)fpu;
170 mm_segment_t oldfs = get_fs();
171
172 if (!tsk_used_math(tsk))
173 return 0;
174 if (!regs)
Al Virobb049232006-01-12 01:05:38 -0800175 regs = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (tsk == current)
177 unlazy_fpu(tsk);
178 set_fs(KERNEL_DS);
179 save_i387_ia32(tsk, fpstate, regs, 1);
180 /* Correct for i386 bug. It puts the fop into the upper 16bits of
181 the tag word (like FXSAVE), not into the fcs*/
182 fpstate->cssel |= fpstate->tag & 0xffff0000;
183 set_fs(oldfs);
184 return 1;
185}
186
187#define ELF_CORE_COPY_XFPREGS 1
Mark Nelson5b20cd82007-10-16 23:25:39 -0700188#define ELF_CORE_XFPREG_TYPE NT_PRXFPREG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static inline int
190elf_core_copy_task_xfpregs(struct task_struct *t, elf_fpxregset_t *xfpu)
191{
Al Virobb049232006-01-12 01:05:38 -0800192 struct pt_regs *regs = task_pt_regs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 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
207extern 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) \
213do { \
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236#undef start_thread
237#define start_thread(regs,new_rip,new_rsp) do { \
238 asm volatile("movl %0,%%fs" :: "r" (0)); \
239 asm volatile("movl %0,%%es; movl %0,%%ds": :"r" (__USER32_DS)); \
240 load_gs_index(0); \
241 (regs)->rip = (new_rip); \
242 (regs)->rsp = (new_rsp); \
243 (regs)->eflags = 0x200; \
244 (regs)->cs = __USER32_CS; \
245 (regs)->ss = __USER32_DS; \
246 set_fs(USER_DS); \
247} while(0)
248
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#include <linux/module.h>
251
252MODULE_DESCRIPTION("Binary format loader for compatibility with IA32 ELF binaries.");
253MODULE_AUTHOR("Eric Youngdale, Andi Kleen");
254
255#undef MODULE_DESCRIPTION
256#undef MODULE_AUTHOR
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static void elf32_init(struct pt_regs *);
259
Andi Kleen1e014412005-04-16 15:24:55 -0700260#define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1
261#define arch_setup_additional_pages syscall32_setup_pages
262extern int syscall32_setup_pages(struct linux_binprm *, int exstack);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#include "../../../fs/binfmt_elf.c"
265
266static void elf32_init(struct pt_regs *regs)
267{
268 struct task_struct *me = current;
269 regs->rdi = 0;
270 regs->rsi = 0;
271 regs->rdx = 0;
272 regs->rcx = 0;
273 regs->rax = 0;
274 regs->rbx = 0;
275 regs->rbp = 0;
276 regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
277 regs->r13 = regs->r14 = regs->r15 = 0;
278 me->thread.fs = 0;
279 me->thread.gs = 0;
280 me->thread.fsindex = 0;
281 me->thread.gsindex = 0;
282 me->thread.ds = __USER_DS;
283 me->thread.es = __USER_DS;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#ifdef CONFIG_SYSCTL
287/* Register vsyscall32 into the ABI table */
288#include <linux/sysctl.h>
289
290static ctl_table abi_table2[] = {
Eric W. Biederman306421f2007-02-14 00:33:49 -0800291 {
292 .ctl_name = 99,
293 .procname = "vsyscall32",
294 .data = &sysctl_vsyscall32,
295 .maxlen = sizeof(int),
296 .mode = 0644,
297 .proc_handler = proc_dointvec
298 },
299 {}
300};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Eric W. Biederman306421f2007-02-14 00:33:49 -0800302static ctl_table abi_root_table2[] = {
303 {
304 .ctl_name = CTL_ABI,
305 .procname = "abi",
306 .mode = 0555,
307 .child = abi_table2
308 },
309 {}
310};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312static __init int ia32_binfmt_init(void)
313{
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800314 register_sysctl_table(abi_root_table2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
316}
317__initcall(ia32_binfmt_init);
318#endif