4ba5d4801baec35f5c2bdde622f62fdcb76b23fd
[linux-2.6.git] / arch / sparc64 / kernel / binfmt_aout32.c
1 /*
2  *  linux/fs/binfmt_aout.c
3  *
4  *  Copyright (C) 1991, 1992, 1996  Linus Torvalds
5  *
6  *  Hacked a bit by DaveM to make it work with 32-bit SunOS
7  *  binaries on the sparc64 port.
8  */
9
10 #include <linux/module.h>
11
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/mman.h>
16 #include <linux/a.out.h>
17 #include <linux/errno.h>
18 #include <linux/signal.h>
19 #include <linux/string.h>
20 #include <linux/fs.h>
21 #include <linux/file.h>
22 #include <linux/stat.h>
23 #include <linux/fcntl.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/slab.h>
27 #include <linux/binfmts.h>
28 #include <linux/personality.h>
29 #include <linux/init.h>
30
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgalloc.h>
34
35 static int load_aout32_binary(struct linux_binprm *, struct pt_regs * regs);
36 static int load_aout32_library(struct file*);
37 static int aout32_core_dump(long signr, struct pt_regs * regs, struct file *file);
38
39 extern void dump_thread(struct pt_regs *, struct user *);
40
41 static struct linux_binfmt aout32_format = {
42         NULL, THIS_MODULE, load_aout32_binary, load_aout32_library, aout32_core_dump,
43         PAGE_SIZE
44 };
45
46 static void set_brk(unsigned long start, unsigned long end)
47 {
48         start = PAGE_ALIGN(start);
49         end = PAGE_ALIGN(end);
50         if (end <= start)
51                 return;
52         do_brk(start, end - start);
53 }
54
55 /*
56  * These are the only things you should do on a core-file: use only these
57  * macros to write out all the necessary info.
58  */
59
60 static int dump_write(struct file *file, const void *addr, int nr)
61 {
62         return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
63 }
64
65 #define DUMP_WRITE(addr, nr)    \
66         if (!dump_write(file, (void *)(addr), (nr))) \
67                 goto end_coredump;
68
69 #define DUMP_SEEK(offset) \
70 if (file->f_op->llseek) { \
71         if (file->f_op->llseek(file,(offset),0) != (offset)) \
72                 goto end_coredump; \
73 } else file->f_pos = (offset)
74
75 /*
76  * Routine writes a core dump image in the current directory.
77  * Currently only a stub-function.
78  *
79  * Note that setuid/setgid files won't make a core-dump if the uid/gid
80  * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
81  * field, which also makes sure the core-dumps won't be recursive if the
82  * dumping of the process results in another error..
83  */
84
85 static int aout32_core_dump(long signr, struct pt_regs *regs, struct file *file)
86 {
87         mm_segment_t fs;
88         int has_dumped = 0;
89         unsigned long dump_start, dump_size;
90         struct user dump;
91 #       define START_DATA(u)    (u.u_tsize)
92 #       define START_STACK(u)   ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
93
94         fs = get_fs();
95         set_fs(KERNEL_DS);
96         has_dumped = 1;
97         current->flags |= PF_DUMPCORE;
98         strncpy(dump.u_comm, current->comm, sizeof(current->comm));
99         dump.signal = signr;
100         dump_thread(regs, &dump);
101
102 /* If the size of the dump file exceeds the rlimit, then see what would happen
103    if we wrote the stack, but not the data area.  */
104         if ((dump.u_dsize+dump.u_ssize) >
105             current->rlim[RLIMIT_CORE].rlim_cur)
106                 dump.u_dsize = 0;
107
108 /* Make sure we have enough room to write the stack and data areas. */
109         if ((dump.u_ssize) >
110             current->rlim[RLIMIT_CORE].rlim_cur)
111                 dump.u_ssize = 0;
112
113 /* make sure we actually have a data and stack area to dump */
114         set_fs(USER_DS);
115         if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize))
116                 dump.u_dsize = 0;
117         if (verify_area(VERIFY_READ, (void *) START_STACK(dump), dump.u_ssize))
118                 dump.u_ssize = 0;
119
120         set_fs(KERNEL_DS);
121 /* struct user */
122         DUMP_WRITE(&dump,sizeof(dump));
123 /* now we start writing out the user space info */
124         set_fs(USER_DS);
125 /* Dump the data area */
126         if (dump.u_dsize != 0) {
127                 dump_start = START_DATA(dump);
128                 dump_size = dump.u_dsize;
129                 DUMP_WRITE(dump_start,dump_size);
130         }
131 /* Now prepare to dump the stack area */
132         if (dump.u_ssize != 0) {
133                 dump_start = START_STACK(dump);
134                 dump_size = dump.u_ssize;
135                 DUMP_WRITE(dump_start,dump_size);
136         }
137 /* Finally dump the task struct.  Not be used by gdb, but could be useful */
138         set_fs(KERNEL_DS);
139         DUMP_WRITE(current,sizeof(*current));
140 end_coredump:
141         set_fs(fs);
142         return has_dumped;
143 }
144
145 /*
146  * create_aout32_tables() parses the env- and arg-strings in new user
147  * memory and creates the pointer tables from them, and puts their
148  * addresses on the "stack", returning the new stack pointer value.
149  */
150 #define A(__x) ((unsigned long)(__x))
151
152 static u32 *create_aout32_tables(char * p, struct linux_binprm * bprm)
153 {
154         u32 *argv, *envp;
155         u32 *sp;
156         int argc = bprm->argc;
157         int envc = bprm->envc;
158
159         sp = (u32 *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p);
160
161         /* This imposes the proper stack alignment for a new process. */
162         sp = (u32 *) (((unsigned long) sp) & ~7);
163         if ((envc+argc+3)&1)
164                 --sp;
165
166         sp -= envc+1;
167         envp = (u32 *) sp;
168         sp -= argc+1;
169         argv = (u32 *) sp;
170         put_user(argc,--sp);
171         current->mm->arg_start = (unsigned long) p;
172         while (argc-->0) {
173                 char c;
174                 put_user(((u32)A(p)),argv++);
175                 do {
176                         get_user(c,p++);
177                 } while (c);
178         }
179         put_user(NULL,argv);
180         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
181         while (envc-->0) {
182                 char c;
183                 put_user(((u32)A(p)),envp++);
184                 do {
185                         get_user(c,p++);
186                 } while (c);
187         }
188         put_user(NULL,envp);
189         current->mm->env_end = (unsigned long) p;
190         return sp;
191 }
192
193 /*
194  * These are the functions used to load a.out style executables and shared
195  * libraries.  There is no binary dependent code anywhere else.
196  */
197
198 static int load_aout32_binary(struct linux_binprm * bprm, struct pt_regs * regs)
199 {
200         struct exec ex;
201         unsigned long error;
202         unsigned long fd_offset;
203         unsigned long rlim;
204         unsigned long orig_thr_flags;
205         int retval;
206
207         ex = *((struct exec *) bprm->buf);              /* exec-header */
208         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
209              N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
210             N_TRSIZE(ex) || N_DRSIZE(ex) ||
211             bprm->file->f_dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
212                 return -ENOEXEC;
213         }
214
215         fd_offset = N_TXTOFF(ex);
216
217         /* Check initial limits. This avoids letting people circumvent
218          * size limits imposed on them by creating programs with large
219          * arrays in the data or bss.
220          */
221         rlim = current->rlim[RLIMIT_DATA].rlim_cur;
222         if (rlim >= RLIM_INFINITY)
223                 rlim = ~0;
224         if (ex.a_data + ex.a_bss > rlim)
225                 return -ENOMEM;
226
227         /* Flush all traces of the currently running executable */
228         retval = flush_old_exec(bprm);
229         if (retval)
230                 return retval;
231
232         /* OK, This is the point of no return */
233         set_personality(PER_SUNOS);
234
235         current->mm->end_code = ex.a_text +
236                 (current->mm->start_code = N_TXTADDR(ex));
237         current->mm->end_data = ex.a_data +
238                 (current->mm->start_data = N_DATADDR(ex));
239         current->mm->brk = ex.a_bss +
240                 (current->mm->start_brk = N_BSSADDR(ex));
241
242         current->mm->rss = 0;
243         current->mm->mmap = NULL;
244         compute_creds(bprm);
245         current->flags &= ~PF_FORKNOEXEC;
246         if (N_MAGIC(ex) == NMAGIC) {
247                 loff_t pos = fd_offset;
248                 /* Fuck me plenty... */
249                 error = do_brk(N_TXTADDR(ex), ex.a_text);
250                 bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
251                           ex.a_text, &pos);
252                 error = do_brk(N_DATADDR(ex), ex.a_data);
253                 bprm->file->f_op->read(bprm->file, (char *) N_DATADDR(ex),
254                           ex.a_data, &pos);
255                 goto beyond_if;
256         }
257
258         if (N_MAGIC(ex) == OMAGIC) {
259                 loff_t pos = fd_offset;
260                 do_brk(N_TXTADDR(ex) & PAGE_MASK,
261                         ex.a_text+ex.a_data + PAGE_SIZE - 1);
262                 bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
263                           ex.a_text+ex.a_data, &pos);
264         } else {
265                 static unsigned long error_time;
266                 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
267                     (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time) > 5*HZ)
268                 {
269                         printk(KERN_NOTICE "executable not page aligned\n");
270                         error_time = jiffies;
271                 }
272
273                 if (!bprm->file->f_op->mmap) {
274                         loff_t pos = fd_offset;
275                         do_brk(0, ex.a_text+ex.a_data);
276                         bprm->file->f_op->read(bprm->file,(char *)N_TXTADDR(ex),
277                                   ex.a_text+ex.a_data, &pos);
278                         goto beyond_if;
279                 }
280
281                 down_write(&current->mm->mmap_sem);
282                 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
283                         PROT_READ | PROT_EXEC,
284                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
285                         fd_offset);
286                 up_write(&current->mm->mmap_sem);
287
288                 if (error != N_TXTADDR(ex)) {
289                         send_sig(SIGKILL, current, 0);
290                         return error;
291                 }
292
293                 down_write(&current->mm->mmap_sem);
294                 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
295                                 PROT_READ | PROT_WRITE | PROT_EXEC,
296                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
297                                 fd_offset + ex.a_text);
298                 up_write(&current->mm->mmap_sem);
299                 if (error != N_DATADDR(ex)) {
300                         send_sig(SIGKILL, current, 0);
301                         return error;
302                 }
303         }
304 beyond_if:
305         set_binfmt(&aout32_format);
306
307         set_brk(current->mm->start_brk, current->mm->brk);
308
309         /* Make sure STACK_TOP returns the right thing.  */
310         orig_thr_flags = current_thread_info()->flags;
311         current_thread_info()->flags |= _TIF_32BIT;
312
313         retval = setup_arg_pages(bprm, EXSTACK_DEFAULT);
314         if (retval < 0) { 
315                 current_thread_info()->flags = orig_thr_flags;
316
317                 /* Someone check-me: is this error path enough? */ 
318                 send_sig(SIGKILL, current, 0); 
319                 return retval;
320         }
321
322         current->mm->start_stack =
323                 (unsigned long) create_aout32_tables((char *)bprm->p, bprm);
324         if (!(orig_thr_flags & _TIF_32BIT)) {
325                 unsigned long pgd_cache;
326
327                 pgd_cache = ((unsigned long)current->mm->pgd[0])<<11UL;
328                 __asm__ __volatile__("stxa\t%0, [%1] %2\n\t"
329                                      "membar #Sync"
330                                      : /* no outputs */
331                                      : "r" (pgd_cache),
332                                        "r" (TSB_REG), "i" (ASI_DMMU));
333         }
334         start_thread32(regs, ex.a_entry, current->mm->start_stack);
335         if (current->ptrace & PT_PTRACED)
336                 send_sig(SIGTRAP, current, 0);
337         return 0;
338 }
339
340 /* N.B. Move to .h file and use code in fs/binfmt_aout.c? */
341 static int load_aout32_library(struct file *file)
342 {
343         struct inode * inode;
344         unsigned long bss, start_addr, len;
345         unsigned long error;
346         int retval;
347         struct exec ex;
348
349         inode = file->f_dentry->d_inode;
350
351         retval = -ENOEXEC;
352         error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
353         if (error != sizeof(ex))
354                 goto out;
355
356         /* We come in here for the regular a.out style of shared libraries */
357         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
358             N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
359             inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
360                 goto out;
361         }
362
363         if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
364             (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
365                 printk("N_TXTOFF < BLOCK_SIZE. Please convert library\n");
366                 goto out;
367         }
368
369         if (N_FLAGS(ex))
370                 goto out;
371
372         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
373            this off to get the starting address for the page */
374
375         start_addr =  ex.a_entry & 0xfffff000;
376
377         /* Now use mmap to map the library into memory. */
378         down_write(&current->mm->mmap_sem);
379         error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
380                         PROT_READ | PROT_WRITE | PROT_EXEC,
381                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
382                         N_TXTOFF(ex));
383         up_write(&current->mm->mmap_sem);
384         retval = error;
385         if (error != start_addr)
386                 goto out;
387
388         len = PAGE_ALIGN(ex.a_text + ex.a_data);
389         bss = ex.a_text + ex.a_data + ex.a_bss;
390         if (bss > len) {
391                 error = do_brk(start_addr + len, bss - len);
392                 retval = error;
393                 if (error != start_addr + len)
394                         goto out;
395         }
396         retval = 0;
397 out:
398         return retval;
399 }
400
401 static int __init init_aout32_binfmt(void)
402 {
403         return register_binfmt(&aout32_format);
404 }
405
406 static void __exit exit_aout32_binfmt(void)
407 {
408         unregister_binfmt(&aout32_format);
409 }
410
411 module_init(init_aout32_binfmt);
412 module_exit(exit_aout32_binfmt);