ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / s390 / kernel / binfmt_elf32.c
1 /*
2  * Support for 32-bit Linux for S390 ELF binaries.
3  *
4  * Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
5  * Author(s): Gerhard Tonn (ton@de.ibm.com)
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 __ASMS390_ELF_H
13
14 #include <linux/time.h>
15
16 /*
17  * These are used to set parameters in the core dumps.
18  */
19 #define ELF_CLASS       ELFCLASS32
20 #define ELF_DATA        ELFDATA2MSB
21 #define ELF_ARCH        EM_S390
22
23 /*
24  * This is used to ensure we don't load something for the wrong architecture.
25  */
26 #define elf_check_arch(x) \
27         (((x)->e_machine == EM_S390 || (x)->e_machine == EM_S390_OLD) \
28          && (x)->e_ident[EI_CLASS] == ELF_CLASS)
29
30 /* ELF register definitions */
31 #define NUM_GPRS      16
32 #define NUM_FPRS      16
33 #define NUM_ACRS      16    
34
35 #define TASK31_SIZE             (0x80000000UL)
36 #undef TASK_SIZE
37 #define TASK_SIZE TASK31_SIZE
38
39 /* For SVR4/S390 the function pointer to be registered with `atexit` is
40    passed in R14. */
41 #define ELF_PLAT_INIT(_r, load_addr) \
42         do { \
43                 _r->gprs[14] = 0; \
44         } while(0)
45
46 #define USE_ELF_CORE_DUMP
47 #define ELF_EXEC_PAGESIZE       4096
48
49 /* This is the location that an ET_DYN program is loaded if exec'ed.  Typical
50    use of this is to invoke "./ld.so someprog" to test out a new version of
51    the loader.  We need to make sure that it is out of the way of the program
52    that it will "exec", and that there is sufficient room for the brk.  */
53
54 #define ELF_ET_DYN_BASE         (TASK31_SIZE / 3 * 2)
55
56 /* Wow, the "main" arch needs arch dependent functions too.. :) */
57
58 /* regs is struct pt_regs, pr_reg is elf_gregset_t (which is
59    now struct_user_regs, they are different) */
60
61 #define ELF_CORE_COPY_REGS(pr_reg, regs) dump_regs32(regs, &pr_reg);
62
63
64 /* This yields a mask that user programs can use to figure out what
65    instruction set this CPU supports. */
66
67 #define ELF_HWCAP (0)
68
69 /* This yields a string that ld.so will use to load implementation
70    specific libraries for optimization.  This is more specific in
71    intent than poking at uname or /proc/cpuinfo.
72
73    For the moment, we have only optimizations for the Intel generations,
74    but that could change... */
75
76 #define ELF_PLATFORM (NULL)
77
78 #define SET_PERSONALITY(ex, ibcs2)                      \
79 do {                                                    \
80         if (ibcs2)                                      \
81                 set_personality(PER_SVR4);              \
82         else if (current->personality != PER_LINUX32)   \
83                 set_personality(PER_LINUX);             \
84         set_thread_flag(TIF_31BIT);                     \
85 } while (0)
86
87 #include "compat_linux.h"
88
89 typedef _s390_fp_regs32 elf_fpregset_t;
90
91 typedef struct
92 {
93         
94         _psw_t32        psw;
95         __u32           gprs[__NUM_GPRS]; 
96         __u32           acrs[__NUM_ACRS]; 
97         __u32           orig_gpr2;
98 } s390_regs32;
99 typedef s390_regs32 elf_gregset_t;
100
101 static inline int dump_regs32(struct pt_regs *ptregs, elf_gregset_t *regs)
102 {
103         int i;
104
105         memcpy(&regs->psw.mask, &ptregs->psw.mask, 4);
106         memcpy(&regs->psw.addr, &ptregs->psw.addr, 4);
107         for (i = 0; i < NUM_GPRS; i++)
108                 regs->gprs[i] = ptregs->gprs[i];
109         regs->orig_gpr2 = ptregs->orig_gpr2;
110         return 1;
111 }
112
113 #include <asm/processor.h>
114 #include <linux/module.h>
115 #include <linux/config.h>
116 #include <linux/elfcore.h>
117 #include <linux/binfmts.h>
118 #include <linux/compat.h>
119
120 int setup_arg_pages32(struct linux_binprm *bprm, int executable_stack);
121
122 #define elf_prstatus elf_prstatus32
123 struct elf_prstatus32
124 {
125         struct elf_siginfo pr_info;     /* Info associated with signal */
126         short   pr_cursig;              /* Current signal */
127         u32     pr_sigpend;     /* Set of pending signals */
128         u32     pr_sighold;     /* Set of held signals */
129         pid_t   pr_pid;
130         pid_t   pr_ppid;
131         pid_t   pr_pgrp;
132         pid_t   pr_sid;
133         struct compat_timeval pr_utime; /* User time */
134         struct compat_timeval pr_stime; /* System time */
135         struct compat_timeval pr_cutime;        /* Cumulative user time */
136         struct compat_timeval pr_cstime;        /* Cumulative system time */
137         elf_gregset_t pr_reg;   /* GP registers */
138         int pr_fpvalid;         /* True if math co-processor being used.  */
139 };
140
141 #define elf_prpsinfo elf_prpsinfo32
142 struct elf_prpsinfo32
143 {
144         char    pr_state;       /* numeric process state */
145         char    pr_sname;       /* char for pr_state */
146         char    pr_zomb;        /* zombie */
147         char    pr_nice;        /* nice val */
148         u32     pr_flag;        /* flags */
149         u16     pr_uid;
150         u16     pr_gid;
151         pid_t   pr_pid, pr_ppid, pr_pgrp, pr_sid;
152         /* Lots missing */
153         char    pr_fname[16];   /* filename of executable */
154         char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
155 };
156
157 #include <linux/highuid.h>
158
159 #undef NEW_TO_OLD_UID
160 #undef NEW_TO_OLD_GID
161 #define NEW_TO_OLD_UID(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
162 #define NEW_TO_OLD_GID(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid) 
163
164 #define elf_addr_t      u32
165 /*
166 #define init_elf_binfmt init_elf32_binfmt
167 */
168
169 #undef start_thread
170 #define start_thread                    start_thread31 
171 #define setup_arg_pages(bprm, exec)     setup_arg_pages32(bprm, exec)
172 #define elf_map                         elf_map32
173
174 MODULE_DESCRIPTION("Binary format loader for compatibility with 32bit Linux for S390 binaries,"
175                    " Copyright 2000 IBM Corporation"); 
176 MODULE_AUTHOR("Gerhard Tonn <ton@de.ibm.com>");
177
178 #undef MODULE_DESCRIPTION
179 #undef MODULE_AUTHOR
180
181 #define jiffies_to_timeval jiffies_to_compat_timeval
182 static __inline__ void
183 jiffies_to_compat_timeval(unsigned long jiffies, struct compat_timeval *value)
184 {
185         value->tv_usec = (jiffies % HZ) * (1000000L / HZ);
186         value->tv_sec = jiffies / HZ;
187 }
188
189 #include "../../../fs/binfmt_elf.c"
190
191 static unsigned long
192 elf_map32 (struct file *filep, unsigned long addr, struct elf_phdr *eppnt, int prot, int type)
193 {
194         unsigned long map_addr;
195
196         if (!addr) 
197                 addr = TASK_UNMAPPED_BASE;
198
199         down_write(&current->mm->mmap_sem);
200         map_addr = do_mmap(filep, ELF_PAGESTART(addr),
201                            eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr),
202                            prot, type,
203                            eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
204         up_write(&current->mm->mmap_sem);
205         return(map_addr);
206 }