ia32_binfmt.c 8.76 KB
Newer Older
1
/* 
2
 * Written 2000,2002 by Andi Kleen. 
3
 * 
Andi Kleen's avatar
Andi Kleen committed
4
 * Loosely based on the sparc64 and IA64 32bit emulation loaders.
5 6
 * This tricks binfmt_elf.c into loading 32bit binaries using lots 
 * of ugly preprocessor tricks. Talk about very very poor man's inheritance.
7 8
 */ 
#include <linux/types.h>
Andi Kleen's avatar
Andi Kleen committed
9
#include <linux/compat.h>
10 11 12
#include <linux/config.h> 
#include <linux/stddef.h>
#include <linux/rwsem.h>
13 14
#include <linux/sched.h>
#include <linux/string.h>
Andi Kleen's avatar
Andi Kleen committed
15
#include <linux/binfmts.h>
16
#include <linux/mm.h>
17 18 19
#include <asm/segment.h> 
#include <asm/ptrace.h>
#include <asm/processor.h>
20 21 22 23
#include <asm/user32.h>
#include <asm/sigcontext32.h>
#include <asm/fpu32.h>
#include <asm/i387.h>
Andi Kleen's avatar
Andi Kleen committed
24 25
#include <asm/uaccess.h>
#include <asm/ia32.h>
26

Andi Kleen's avatar
Andi Kleen committed
27 28 29 30 31 32
#define ELF_NAME "elf/i386"

#define AT_SYSINFO 32

#define ARCH_DLINFO NEW_AUX_ENT(AT_SYSINFO, 0xffffe000)

Andi Kleen's avatar
Andi Kleen committed
33 34 35
struct file;
struct elf_phdr; 

36 37 38 39 40 41 42 43 44 45 46
#define IA32_EMULATOR 1

#define ELF_ET_DYN_BASE		(IA32_PAGE_OFFSET/3 + 0x1000000)

#undef ELF_ARCH
#define ELF_ARCH EM_386

#undef ELF_CLASS
#define ELF_CLASS ELFCLASS32

#define ELF_DATA	ELFDATA2LSB
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

#define USE_ELF_CORE_DUMP 1

/* Overwrite elfcore.h */ 
#define _LINUX_ELFCORE_H 1
typedef unsigned int elf_greg_t;

#define ELF_NGREG (sizeof (struct user_regs_struct32) / sizeof(elf_greg_t))
typedef elf_greg_t elf_gregset_t[ELF_NGREG];

struct elf_siginfo
{
	int	si_signo;			/* signal number */
	int	si_code;			/* extra code */
	int	si_errno;			/* errno */
};

64 65
#define jiffies_to_timeval(a,b) do { (b)->tv_usec = 0; (b)->tv_sec = (a)/HZ; }while(0)

66 67 68 69 70 71 72 73 74 75
struct elf_prstatus
{
	struct elf_siginfo pr_info;	/* Info associated with signal */
	short	pr_cursig;		/* Current signal */
	unsigned int pr_sigpend;	/* Set of pending signals */
	unsigned int pr_sighold;	/* Set of held signals */
	pid_t	pr_pid;
	pid_t	pr_ppid;
	pid_t	pr_pgrp;
	pid_t	pr_sid;
Andi Kleen's avatar
Andi Kleen committed
76 77 78 79
	struct compat_timeval pr_utime;	/* User time */
	struct compat_timeval pr_stime;	/* System time */
	struct compat_timeval pr_cutime;	/* Cumulative user time */
	struct compat_timeval pr_cstime;	/* Cumulative system time */
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	elf_gregset_t pr_reg;	/* GP registers */
	int pr_fpvalid;		/* True if math co-processor being used.  */
};

#define ELF_PRARGSZ	(80)	/* Number of chars for args */

struct elf_prpsinfo
{
	char	pr_state;	/* numeric process state */
	char	pr_sname;	/* char for pr_state */
	char	pr_zomb;	/* zombie */
	char	pr_nice;	/* nice val */
	unsigned int pr_flag;	/* flags */
	__u16	pr_uid;
	__u16	pr_gid;
	pid_t	pr_pid, pr_ppid, pr_pgrp, pr_sid;
	/* Lots missing */
	char	pr_fname[16];	/* filename of executable */
	char	pr_psargs[ELF_PRARGSZ];	/* initial part of arg list */
};

#define __STR(x) #x
#define STR(x) __STR(x)

#define _GET_SEG(x) \
	({ __u32 seg; asm("movl %%" STR(x) ",%0" : "=r"(seg)); seg; })

/* Assumes current==process to be dumped */
#define ELF_CORE_COPY_REGS(pr_reg, regs)       		\
	pr_reg[0] = regs->rbx;				\
	pr_reg[1] = regs->rcx;				\
	pr_reg[2] = regs->rdx;				\
	pr_reg[3] = regs->rsi;				\
	pr_reg[4] = regs->rdi;				\
	pr_reg[5] = regs->rbp;				\
	pr_reg[6] = regs->rax;				\
	pr_reg[7] = _GET_SEG(ds);   			\
	pr_reg[8] = _GET_SEG(es);			\
	pr_reg[9] = _GET_SEG(fs);			\
	pr_reg[10] = _GET_SEG(gs);			\
	pr_reg[11] = regs->orig_rax;			\
	pr_reg[12] = regs->rip;				\
	pr_reg[13] = regs->cs;				\
	pr_reg[14] = regs->eflags;			\
	pr_reg[15] = regs->rsp;				\
	pr_reg[16] = regs->ss;

#define user user32

129
#define __ASM_X86_64_ELF_H 1
Andi Kleen's avatar
Andi Kleen committed
130
//#include <asm/ia32.h>
131 132 133
#include <linux/elf.h>

typedef struct user_i387_ia32_struct elf_fpregset_t;
134
typedef struct user32_fxsr_struct elf_fpxregset_t;
135

Andi Kleen's avatar
Andi Kleen committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

static inline void elf_core_copy_regs(elf_gregset_t *elfregs, struct pt_regs *regs)
{
	ELF_CORE_COPY_REGS((*elfregs), regs)
}

static inline int elf_core_copy_task_regs(struct task_struct *t, elf_gregset_t* elfregs)
{	
	struct pt_regs *pp = (struct pt_regs *)(t->thread.rsp0);
	ELF_CORE_COPY_REGS((*elfregs), pp);
	/* fix wrong segments */ 
	(*elfregs)[7] = t->thread.ds; 
	(*elfregs)[9] = t->thread.fsindex; 
	(*elfregs)[10] = t->thread.gsindex; 
	(*elfregs)[8] = t->thread.es; 	
	return 1; 
}

static inline int 
elf_core_copy_task_fpregs(struct task_struct *tsk, elf_fpregset_t *fpu)
{
	struct _fpstate_ia32 *fpstate = (void*)fpu; 
	struct pt_regs *regs = (struct pt_regs *)(tsk->thread.rsp0); 
	mm_segment_t oldfs = get_fs();
	int ret;

	if (!tsk->used_math) 
		return 0;
	--regs;
	if (tsk == current)
		unlazy_fpu(tsk);
	set_fs(KERNEL_DS); 
	ret = save_i387_ia32(tsk, fpstate, regs, 1);
	/* Correct for i386 bug. It puts the fop into the upper 16bits of 
	   the tag word (like FXSAVE), not into the fcs*/ 
	fpstate->cssel |= fpstate->tag & 0xffff0000; 
	set_fs(oldfs); 
	return ret; 
}

#define ELF_CORE_COPY_XFPREGS 1
static inline int 
elf_core_copy_task_xfpregs(struct task_struct *t, elf_fpxregset_t *xfpu)
{
	struct pt_regs *regs = ((struct pt_regs *)(t->thread.rsp0))-1; 
	if (!t->used_math) 
		return 0;
	if (t == current)
		unlazy_fpu(t); 
	memcpy(xfpu, &t->thread.i387.fxsave, sizeof(elf_fpxregset_t));
	xfpu->fcs = regs->cs; 
	xfpu->fos = t->thread.ds; /* right? */ 
	return 1;
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
#undef elf_check_arch
#define elf_check_arch(x) \
	((x)->e_machine == EM_386)

#define ELF_EXEC_PAGESIZE PAGE_SIZE
#define ELF_HWCAP (boot_cpu_data.x86_capability[0])
#define ELF_PLATFORM  ("i686")
#define SET_PERSONALITY(ex, ibcs2)			\
do {							\
	set_personality((ibcs2)?PER_SVR4:current->personality);	\
} while (0)

/* Override some function names */
#define elf_format			elf32_format

#define init_elf_binfmt			init_elf32_binfmt
#define exit_elf_binfmt			exit_elf32_binfmt

#define load_elf_binary load_elf32_binary

#undef CONFIG_BINFMT_ELF
#ifdef CONFIG_BINFMT_ELF32
# define CONFIG_BINFMT_ELF		CONFIG_BINFMT_ELF32
#endif

#undef CONFIG_BINFMT_ELF_MODULE
#ifdef CONFIG_BINFMT_ELF32_MODULE
# define CONFIG_BINFMT_ELF_MODULE	CONFIG_BINFMT_ELF32_MODULE
#endif

221
#define ELF_PLAT_INIT(r, load_addr)	elf32_init(r)
222
#define setup_arg_pages(bprm)		ia32_setup_arg_pages(bprm)
223
int ia32_setup_arg_pages(struct linux_binprm *bprm);
224 225 226

#undef start_thread
#define start_thread(regs,new_rip,new_rsp) do { \
Andi Kleen's avatar
Andi Kleen committed
227 228 229
	asm volatile("movl %0,%%fs" :: "r" (0)); \
	asm volatile("movl %0,%%es; movl %0,%%ds": :"r" (__USER32_DS)); \
	load_gs_index(0); \
230 231 232 233 234 235 236 237 238
	(regs)->rip = (new_rip); \
	(regs)->rsp = (new_rsp); \
	(regs)->eflags = 0x200; \
	(regs)->cs = __USER32_CS; \
	(regs)->ss = __USER32_DS; \
	set_fs(USER_DS); \
} while(0) 


Andi Kleen's avatar
Andi Kleen committed
239 240
#define elf_map elf32_map

Andi Kleen's avatar
Andi Kleen committed
241 242
#include <linux/module.h>

243 244 245 246 247 248 249 250 251 252 253 254 255 256
MODULE_DESCRIPTION("Binary format loader for compatibility with IA32 ELF binaries."); 
MODULE_AUTHOR("Eric Youngdale, Andi Kleen");

#undef MODULE_DESCRIPTION
#undef MODULE_AUTHOR

#define elf_addr_t __u32

static void elf32_init(struct pt_regs *);

#include "../../../fs/binfmt_elf.c" 

static void elf32_init(struct pt_regs *regs)
{
Andi Kleen's avatar
Andi Kleen committed
257
	struct task_struct *me = current; 
258 259 260 261 262 263 264
	regs->rdi = 0;
	regs->rsi = 0;
	regs->rdx = 0;
	regs->rcx = 0;
	regs->rax = 0;
	regs->rbx = 0; 
	regs->rbp = 0; 
265 266
	regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
		regs->r13 = regs->r14 = regs->r15 = 0; 
Andi Kleen's avatar
Andi Kleen committed
267 268 269 270 271 272 273
    me->thread.fs = 0; 
	me->thread.gs = 0;
	me->thread.fsindex = 0; 
	me->thread.gsindex = 0;
    me->thread.ds = __USER_DS; 
	me->thread.es = __USER_DS;
	set_thread_flag(TIF_IA32); 
274 275
}

276
int setup_arg_pages(struct linux_binprm *bprm)
277 278 279
{
	unsigned long stack_base;
	struct vm_area_struct *mpnt;
280
	struct mm_struct *mm = current->mm;
281 282
	int i;

283 284
	stack_base = IA32_STACK_TOP - MAX_ARG_PAGES * PAGE_SIZE;
	mm->arg_start = bprm->p + stack_base;
285 286 287 288 289 290 291 292 293 294

	bprm->p += stack_base;
	if (bprm->loader)
		bprm->loader += stack_base;
	bprm->exec += stack_base;

	mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
	if (!mpnt) 
		return -ENOMEM; 
	
295 296 297 298 299 300
	if (!vm_enough_memory((IA32_STACK_TOP - (PAGE_MASK & (unsigned long) bprm->p))>>PAGE_SHIFT)) {
		kmem_cache_free(vm_area_cachep, mpnt);
		return -ENOMEM;
	}

	down_write(&mm->mmap_sem);
301
	{
302
		mpnt->vm_mm = mm;
303 304
		mpnt->vm_start = PAGE_MASK & (unsigned long) bprm->p;
		mpnt->vm_end = IA32_STACK_TOP;
Andi Kleen's avatar
Andi Kleen committed
305
		mpnt->vm_page_prot = PAGE_COPY_EXEC;
306 307 308 309
		mpnt->vm_flags = VM_STACK_FLAGS;
		mpnt->vm_ops = NULL;
		mpnt->vm_pgoff = 0;
		mpnt->vm_file = NULL;
310
		INIT_LIST_HEAD(&mpnt->shared);
311
		mpnt->vm_private_data = (void *) 0;
312 313
		insert_vm_struct(mm, mpnt);
		mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
314 315 316 317 318 319
	} 

	for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
		struct page *page = bprm->page[i];
		if (page) {
			bprm->page[i] = NULL;
320
			put_dirty_page(current,page,stack_base,PAGE_COPY_EXEC);
321 322 323
		}
		stack_base += PAGE_SIZE;
	}
324
	up_write(&mm->mmap_sem);
325 326 327
	
	return 0;
}
328

Andi Kleen's avatar
Andi Kleen committed
329 330 331 332 333 334
static unsigned long
elf32_map (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
{
	unsigned long map_addr;
	struct task_struct *me = current; 

335 336 337
	if (prot & PROT_READ) 
		prot |= PROT_EXEC; 

Andi Kleen's avatar
Andi Kleen committed
338 339
	down_write(&me->mm->mmap_sem);
	map_addr = do_mmap(filep, ELF_PAGESTART(addr),
340 341
			   eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, 
			   type|MAP_32BIT,
Andi Kleen's avatar
Andi Kleen committed
342 343 344 345
			   eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
	up_write(&me->mm->mmap_sem);
	return(map_addr);
}
346