1 /*
2  * Support for o32 Linux/MIPS ELF binaries.
3  *
4  * Copyright (C) 1999, 2001 Ralf Baechle
5  * Copyright (C) 1999, 2001 Silicon Graphics, Inc.
6  *
7  * Heavily inspired by the 32-bit Sparc compat code which is
8  * Copyright (C) 1995, 1996, 1997, 1998 David S. Miller (davem@redhat.com)
9  * Copyright (C) 1995, 1996, 1997, 1998 Jakub Jelinek   (jj@ultra.linux.cz)
10  */
11 
12 #define ELF_ARCH		EM_MIPS
13 #define ELF_CLASS		ELFCLASS32
14 #ifdef __MIPSEB__
15 #define ELF_DATA		ELFDATA2MSB;
16 #else /* __MIPSEL__ */
17 #define ELF_DATA		ELFDATA2LSB;
18 #endif
19 
20 /* ELF register definitions */
21 #define ELF_NGREG	45
22 #define ELF_NFPREG	33
23 
24 typedef unsigned int elf_greg_t;
25 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
26 
27 typedef double elf_fpreg_t;
28 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
29 
30 /*
31  * This is used to ensure we don't load something for the wrong architecture.
32  */
33 #define elf_check_arch(hdr)						\
34 ({									\
35 	int __res = 1;							\
36 	struct elfhdr *__h = (hdr);					\
37 									\
38 	if (__h->e_machine != EM_MIPS)					\
39 		__res = 0;						\
40 	if (__h->e_ident[EI_CLASS] != ELFCLASS32)			\
41 		__res = 0;						\
42 	if ((__h->e_flags & EF_MIPS_ABI2) != 0)				\
43 		__res = 0;						\
44 	if (((__h->e_flags & EF_MIPS_ABI) != 0) &&			\
45 	    ((__h->e_flags & EF_MIPS_ABI) != EF_MIPS_ABI_O32))		\
46 		__res = 0;						\
47 									\
48 	__res;								\
49 })
50 
51 #define TASK32_SIZE		0x7fff8000UL
52 #undef ELF_ET_DYN_BASE
53 #define ELF_ET_DYN_BASE         (TASK32_SIZE / 3 * 2)
54 
55 #include <asm/processor.h>
56 
57 /*
58  * When this file is selected, we are definitely running a 64bit kernel.
59  * So using the right regs define in asm/reg.h
60  */
61 #define WANT_COMPAT_REG_H
62 
63 /* These MUST be defined before elf.h gets included */
64 extern void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs);
65 #define ELF_CORE_COPY_REGS(_dest, _regs) elf32_core_copy_regs(_dest, _regs);
66 #define ELF_CORE_COPY_TASK_REGS(_tsk, _dest)				\
67 ({									\
68 	int __res = 1;							\
69 	elf32_core_copy_regs(*(_dest), task_pt_regs(_tsk));		\
70 	__res;								\
71 })
72 
73 #include <linux/module.h>
74 #include <linux/elfcore.h>
75 #include <linux/compat.h>
76 #include <linux/math64.h>
77 
78 #define elf_prstatus elf_prstatus32
79 struct elf_prstatus32
80 {
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.  */
95 };
96 
97 #define elf_prpsinfo elf_prpsinfo32
98 struct elf_prpsinfo32
99 {
100 	char	pr_state;	/* numeric process state */
101 	char	pr_sname;	/* char for pr_state */
102 	char	pr_zomb;	/* zombie */
103 	char	pr_nice;	/* nice val */
104 	unsigned int pr_flag;	/* flags */
105 	__kernel_uid_t	pr_uid;
106 	__kernel_gid_t	pr_gid;
107 	pid_t	pr_pid, pr_ppid, pr_pgrp, pr_sid;
108 	/* Lots missing */
109 	char	pr_fname[16];	/* filename of executable */
110 	char	pr_psargs[ELF_PRARGSZ];	/* initial part of arg list */
111 };
112 
113 #define elf_caddr_t	u32
114 #define init_elf_binfmt init_elf32_binfmt
115 
116 #define jiffies_to_timeval jiffies_to_compat_timeval
117 static inline void
jiffies_to_compat_timeval(unsigned long jiffies,struct compat_timeval * value)118 jiffies_to_compat_timeval(unsigned long jiffies, struct compat_timeval *value)
119 {
120 	/*
121 	 * Convert jiffies to nanoseconds and separate with
122 	 * one divide.
123 	 */
124 	u64 nsec = (u64)jiffies * TICK_NSEC;
125 	u32 rem;
126 	value->tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem);
127 	value->tv_usec = rem / NSEC_PER_USEC;
128 }
129 
elf32_core_copy_regs(elf_gregset_t grp,struct pt_regs * regs)130 void elf32_core_copy_regs(elf_gregset_t grp, struct pt_regs *regs)
131 {
132 	int i;
133 
134 	for (i = 0; i < EF_R0; i++)
135 		grp[i] = 0;
136 	grp[EF_R0] = 0;
137 	for (i = 1; i <= 31; i++)
138 		grp[EF_R0 + i] = (elf_greg_t) regs->regs[i];
139 	grp[EF_R26] = 0;
140 	grp[EF_R27] = 0;
141 	grp[EF_LO] = (elf_greg_t) regs->lo;
142 	grp[EF_HI] = (elf_greg_t) regs->hi;
143 	grp[EF_CP0_EPC] = (elf_greg_t) regs->cp0_epc;
144 	grp[EF_CP0_BADVADDR] = (elf_greg_t) regs->cp0_badvaddr;
145 	grp[EF_CP0_STATUS] = (elf_greg_t) regs->cp0_status;
146 	grp[EF_CP0_CAUSE] = (elf_greg_t) regs->cp0_cause;
147 #ifdef EF_UNUSED0
148 	grp[EF_UNUSED0] = 0;
149 #endif
150 }
151 
152 MODULE_DESCRIPTION("Binary format loader for compatibility with o32 Linux/MIPS binaries");
153 MODULE_AUTHOR("Ralf Baechle (ralf@linux-mips.org)");
154 
155 #undef MODULE_DESCRIPTION
156 #undef MODULE_AUTHOR
157 
158 #undef TASK_SIZE
159 #define TASK_SIZE TASK_SIZE32
160 
161 #include "../../../fs/binfmt_elf.c"
162