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