binfmt_elfo32.c 4.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 * Support for o32 Linux/MIPS ELF binaries.
 *
 * Copyright (C) 1999, 2001 Ralf Baechle
 * Copyright (C) 1999, 2001 Silicon Graphics, Inc.
 *
 * Heavily inspired by the 32-bit Sparc compat code which is
 * Copyright (C) 1995, 1996, 1997, 1998 David S. Miller (davem@redhat.com)
R
Ralf Baechle 已提交
9
 * Copyright (C) 1995, 1996, 1997, 1998 Jakub Jelinek	(jj@ultra.linux.cz)
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 */

#define ELF_ARCH		EM_MIPS
#define ELF_CLASS		ELFCLASS32
#ifdef __MIPSEB__
#define ELF_DATA		ELFDATA2MSB;
#else /* __MIPSEL__ */
#define ELF_DATA		ELFDATA2LSB;
#endif

/* ELF register definitions */
#define ELF_NGREG	45
#define ELF_NFPREG	33

typedef unsigned int elf_greg_t;
typedef elf_greg_t elf_gregset_t[ELF_NGREG];

typedef double elf_fpreg_t;
typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];

/*
 * This is used to ensure we don't load something for the wrong architecture.
 */
#define elf_check_arch(hdr)						\
({									\
	int __res = 1;							\
	struct elfhdr *__h = (hdr);					\
									\
	if (__h->e_machine != EM_MIPS)					\
		__res = 0;						\
	if (__h->e_ident[EI_CLASS] != ELFCLASS32)			\
		__res = 0;						\
	if ((__h->e_flags & EF_MIPS_ABI2) != 0)				\
		__res = 0;						\
	if (((__h->e_flags & EF_MIPS_ABI) != 0) &&			\
	    ((__h->e_flags & EF_MIPS_ABI) != EF_MIPS_ABI_O32))		\
		__res = 0;						\
									\
	__res;								\
})

51 52 53
#ifdef CONFIG_KVM_GUEST
#define TASK32_SIZE		0x3fff8000UL
#else
L
Linus Torvalds 已提交
54
#define TASK32_SIZE		0x7fff8000UL
55
#endif
L
Linus Torvalds 已提交
56
#undef ELF_ET_DYN_BASE
R
Ralf Baechle 已提交
57
#define ELF_ET_DYN_BASE		(TASK32_SIZE / 3 * 2)
L
Linus Torvalds 已提交
58 59

#include <asm/processor.h>
Y
Yong Zhang 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

/*
 * When this file is selected, we are definitely running a 64bit kernel.
 * So using the right regs define in asm/reg.h
 */
#define WANT_COMPAT_REG_H

/* These MUST be defined before elf.h gets included */
extern void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs);
#define ELF_CORE_COPY_REGS(_dest, _regs) elf32_core_copy_regs(_dest, _regs);
#define ELF_CORE_COPY_TASK_REGS(_tsk, _dest)				\
({									\
	int __res = 1;							\
	elf32_core_copy_regs(*(_dest), task_pt_regs(_tsk));		\
	__res;								\
})

L
Linus Torvalds 已提交
77 78 79
#include <linux/module.h>
#include <linux/elfcore.h>
#include <linux/compat.h>
R
Roman Zippel 已提交
80
#include <linux/math64.h>
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91 92

#define elf_prstatus elf_prstatus32
struct elf_prstatus32
{
	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;
R
Ralf Baechle 已提交
93 94
	struct compat_timeval pr_utime; /* User time */
	struct compat_timeval pr_stime; /* System time */
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	struct compat_timeval pr_cutime;/* Cumulative user time */
	struct compat_timeval pr_cstime;/* Cumulative system time */
	elf_gregset_t pr_reg;	/* GP registers */
	int pr_fpvalid;		/* True if math co-processor being used.  */
};

#define elf_prpsinfo elf_prpsinfo32
struct elf_prpsinfo32
{
	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 */
	__kernel_uid_t	pr_uid;
	__kernel_gid_t	pr_gid;
	pid_t	pr_pid, pr_ppid, pr_pgrp, pr_sid;
	/* Lots missing */
	char	pr_fname[16];	/* filename of executable */
R
Ralf Baechle 已提交
114
	char	pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
L
Linus Torvalds 已提交
115 116 117 118 119 120
};

#define elf_caddr_t	u32
#define init_elf_binfmt init_elf32_binfmt

#define jiffies_to_timeval jiffies_to_compat_timeval
121
static inline void
L
Linus Torvalds 已提交
122 123 124
jiffies_to_compat_timeval(unsigned long jiffies, struct compat_timeval *value)
{
	/*
J
Joe Perches 已提交
125
	 * Convert jiffies to nanoseconds and separate with
L
Linus Torvalds 已提交
126 127
	 * one divide.
	 */
128
	u64 nsec = (u64)jiffies * TICK_NSEC;
R
Roman Zippel 已提交
129 130
	u32 rem;
	value->tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem);
131
	value->tv_usec = rem / NSEC_PER_USEC;
L
Linus Torvalds 已提交
132 133
}

134
void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs)
L
Linus Torvalds 已提交
135 136 137
{
	int i;

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	for (i = 0; i < EF_R0; i++)
		grp[i] = 0;
	grp[EF_R0] = 0;
	for (i = 1; i <= 31; i++)
		grp[EF_R0 + i] = (elf_greg_t) regs->regs[i];
	grp[EF_R26] = 0;
	grp[EF_R27] = 0;
	grp[EF_LO] = (elf_greg_t) regs->lo;
	grp[EF_HI] = (elf_greg_t) regs->hi;
	grp[EF_CP0_EPC] = (elf_greg_t) regs->cp0_epc;
	grp[EF_CP0_BADVADDR] = (elf_greg_t) regs->cp0_badvaddr;
	grp[EF_CP0_STATUS] = (elf_greg_t) regs->cp0_status;
	grp[EF_CP0_CAUSE] = (elf_greg_t) regs->cp0_cause;
#ifdef EF_UNUSED0
	grp[EF_UNUSED0] = 0;
#endif
L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161
}

MODULE_DESCRIPTION("Binary format loader for compatibility with o32 Linux/MIPS binaries");
MODULE_AUTHOR("Ralf Baechle (ralf@linux-mips.org)");

#undef MODULE_DESCRIPTION
#undef MODULE_AUTHOR

162 163 164
#undef TASK_SIZE
#define TASK_SIZE TASK_SIZE32

165 166 167 168 169 170 171 172 173 174 175
#undef cputime_to_timeval
#define cputime_to_timeval cputime_to_compat_timeval
static __inline__ void
cputime_to_compat_timeval(const cputime_t cputime, struct compat_timeval *value)
{
	unsigned long jiffies = cputime_to_jiffies(cputime);

	value->tv_usec = (jiffies % HZ) * (1000000L / HZ);
	value->tv_sec = jiffies / HZ;
}

L
Linus Torvalds 已提交
176
#include "../../../fs/binfmt_elf.c"