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