Merge branch 'linus' into timers/hrtimers
[linux-2.6] / arch / x86 / ia32 / ia32_aout.c
1 /*
2  *  a.out loader for x86-64
3  *
4  *  Copyright (C) 1991, 1992, 1996  Linus Torvalds
5  *  Hacked together by Andi Kleen
6  */
7
8 #include <linux/module.h>
9
10 #include <linux/time.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/mman.h>
14 #include <linux/a.out.h>
15 #include <linux/errno.h>
16 #include <linux/signal.h>
17 #include <linux/string.h>
18 #include <linux/fs.h>
19 #include <linux/file.h>
20 #include <linux/stat.h>
21 #include <linux/fcntl.h>
22 #include <linux/ptrace.h>
23 #include <linux/user.h>
24 #include <linux/slab.h>
25 #include <linux/binfmts.h>
26 #include <linux/personality.h>
27 #include <linux/init.h>
28 #include <linux/jiffies.h>
29
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgalloc.h>
33 #include <asm/cacheflush.h>
34 #include <asm/user32.h>
35 #include <asm/ia32.h>
36
37 #undef WARN_OLD
38 #undef CORE_DUMP /* probably broken */
39
40 static int load_aout_binary(struct linux_binprm *, struct pt_regs *regs);
41 static int load_aout_library(struct file *);
42
43 #ifdef CORE_DUMP
44 static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file,
45                           unsigned long limit);
46
47 /*
48  * fill in the user structure for a core dump..
49  */
50 static void dump_thread32(struct pt_regs *regs, struct user32 *dump)
51 {
52         u32 fs, gs;
53
54 /* changed the size calculations - should hopefully work better. lbt */
55         dump->magic = CMAGIC;
56         dump->start_code = 0;
57         dump->start_stack = regs->sp & ~(PAGE_SIZE - 1);
58         dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
59         dump->u_dsize = ((unsigned long)
60                          (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
61         dump->u_dsize -= dump->u_tsize;
62         dump->u_ssize = 0;
63         dump->u_debugreg[0] = current->thread.debugreg0;
64         dump->u_debugreg[1] = current->thread.debugreg1;
65         dump->u_debugreg[2] = current->thread.debugreg2;
66         dump->u_debugreg[3] = current->thread.debugreg3;
67         dump->u_debugreg[4] = 0;
68         dump->u_debugreg[5] = 0;
69         dump->u_debugreg[6] = current->thread.debugreg6;
70         dump->u_debugreg[7] = current->thread.debugreg7;
71
72         if (dump->start_stack < 0xc0000000) {
73                 unsigned long tmp;
74
75                 tmp = (unsigned long) (0xc0000000 - dump->start_stack);
76                 dump->u_ssize = tmp >> PAGE_SHIFT;
77         }
78
79         dump->regs.bx = regs->bx;
80         dump->regs.cx = regs->cx;
81         dump->regs.dx = regs->dx;
82         dump->regs.si = regs->si;
83         dump->regs.di = regs->di;
84         dump->regs.bp = regs->bp;
85         dump->regs.ax = regs->ax;
86         dump->regs.ds = current->thread.ds;
87         dump->regs.es = current->thread.es;
88         savesegment(fs, fs);
89         dump->regs.fs = fs;
90         savesegment(gs, gs);
91         dump->regs.gs = gs;
92         dump->regs.orig_ax = regs->orig_ax;
93         dump->regs.ip = regs->ip;
94         dump->regs.cs = regs->cs;
95         dump->regs.flags = regs->flags;
96         dump->regs.sp = regs->sp;
97         dump->regs.ss = regs->ss;
98
99 #if 1 /* FIXME */
100         dump->u_fpvalid = 0;
101 #else
102         dump->u_fpvalid = dump_fpu(regs, &dump->i387);
103 #endif
104 }
105
106 #endif
107
108 static struct linux_binfmt aout_format = {
109         .module         = THIS_MODULE,
110         .load_binary    = load_aout_binary,
111         .load_shlib     = load_aout_library,
112 #ifdef CORE_DUMP
113         .core_dump      = aout_core_dump,
114 #endif
115         .min_coredump   = PAGE_SIZE
116 };
117
118 static void set_brk(unsigned long start, unsigned long end)
119 {
120         start = PAGE_ALIGN(start);
121         end = PAGE_ALIGN(end);
122         if (end <= start)
123                 return;
124         down_write(&current->mm->mmap_sem);
125         do_brk(start, end - start);
126         up_write(&current->mm->mmap_sem);
127 }
128
129 #ifdef CORE_DUMP
130 /*
131  * These are the only things you should do on a core-file: use only these
132  * macros to write out all the necessary info.
133  */
134
135 static int dump_write(struct file *file, const void *addr, int nr)
136 {
137         return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
138 }
139
140 #define DUMP_WRITE(addr, nr)                         \
141         if (!dump_write(file, (void *)(addr), (nr))) \
142                 goto end_coredump;
143
144 #define DUMP_SEEK(offset)                                               \
145         if (file->f_op->llseek) {                                       \
146                 if (file->f_op->llseek(file, (offset), 0) != (offset))  \
147                         goto end_coredump;                              \
148         } else                                                          \
149                 file->f_pos = (offset)
150
151 #define START_DATA()    (u.u_tsize << PAGE_SHIFT)
152 #define START_STACK(u)  (u.start_stack)
153
154 /*
155  * Routine writes a core dump image in the current directory.
156  * Currently only a stub-function.
157  *
158  * Note that setuid/setgid files won't make a core-dump if the uid/gid
159  * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
160  * field, which also makes sure the core-dumps won't be recursive if the
161  * dumping of the process results in another error..
162  */
163
164 static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file,
165                           unsigned long limit)
166 {
167         mm_segment_t fs;
168         int has_dumped = 0;
169         unsigned long dump_start, dump_size;
170         struct user32 dump;
171
172         fs = get_fs();
173         set_fs(KERNEL_DS);
174         has_dumped = 1;
175         current->flags |= PF_DUMPCORE;
176         strncpy(dump.u_comm, current->comm, sizeof(current->comm));
177         dump.u_ar0 = offsetof(struct user32, regs);
178         dump.signal = signr;
179         dump_thread32(regs, &dump);
180
181         /*
182          * If the size of the dump file exceeds the rlimit, then see
183          * what would happen if we wrote the stack, but not the data
184          * area.
185          */
186         if ((dump.u_dsize + dump.u_ssize + 1) * PAGE_SIZE > limit)
187                 dump.u_dsize = 0;
188
189         /* Make sure we have enough room to write the stack and data areas. */
190         if ((dump.u_ssize + 1) * PAGE_SIZE > limit)
191                 dump.u_ssize = 0;
192
193         /* make sure we actually have a data and stack area to dump */
194         set_fs(USER_DS);
195         if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_DATA(dump),
196                        dump.u_dsize << PAGE_SHIFT))
197                 dump.u_dsize = 0;
198         if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_STACK(dump),
199                        dump.u_ssize << PAGE_SHIFT))
200                 dump.u_ssize = 0;
201
202         set_fs(KERNEL_DS);
203         /* struct user */
204         DUMP_WRITE(&dump, sizeof(dump));
205         /* Now dump all of the user data.  Include malloced stuff as well */
206         DUMP_SEEK(PAGE_SIZE);
207         /* now we start writing out the user space info */
208         set_fs(USER_DS);
209         /* Dump the data area */
210         if (dump.u_dsize != 0) {
211                 dump_start = START_DATA(dump);
212                 dump_size = dump.u_dsize << PAGE_SHIFT;
213                 DUMP_WRITE(dump_start, dump_size);
214         }
215         /* Now prepare to dump the stack area */
216         if (dump.u_ssize != 0) {
217                 dump_start = START_STACK(dump);
218                 dump_size = dump.u_ssize << PAGE_SHIFT;
219                 DUMP_WRITE(dump_start, dump_size);
220         }
221         /*
222          * Finally dump the task struct.  Not be used by gdb, but
223          * could be useful
224          */
225         set_fs(KERNEL_DS);
226         DUMP_WRITE(current, sizeof(*current));
227 end_coredump:
228         set_fs(fs);
229         return has_dumped;
230 }
231 #endif
232
233 /*
234  * create_aout_tables() parses the env- and arg-strings in new user
235  * memory and creates the pointer tables from them, and puts their
236  * addresses on the "stack", returning the new stack pointer value.
237  */
238 static u32 __user *create_aout_tables(char __user *p, struct linux_binprm *bprm)
239 {
240         u32 __user *argv, *envp, *sp;
241         int argc = bprm->argc, envc = bprm->envc;
242
243         sp = (u32 __user *) ((-(unsigned long)sizeof(u32)) & (unsigned long) p);
244         sp -= envc+1;
245         envp = sp;
246         sp -= argc+1;
247         argv = sp;
248         put_user((unsigned long) envp, --sp);
249         put_user((unsigned long) argv, --sp);
250         put_user(argc, --sp);
251         current->mm->arg_start = (unsigned long) p;
252         while (argc-- > 0) {
253                 char c;
254
255                 put_user((u32)(unsigned long)p, argv++);
256                 do {
257                         get_user(c, p++);
258                 } while (c);
259         }
260         put_user(0, argv);
261         current->mm->arg_end = current->mm->env_start = (unsigned long) p;
262         while (envc-- > 0) {
263                 char c;
264
265                 put_user((u32)(unsigned long)p, envp++);
266                 do {
267                         get_user(c, p++);
268                 } while (c);
269         }
270         put_user(0, envp);
271         current->mm->env_end = (unsigned long) p;
272         return sp;
273 }
274
275 /*
276  * These are the functions used to load a.out style executables and shared
277  * libraries.  There is no binary dependent code anywhere else.
278  */
279 static int load_aout_binary(struct linux_binprm *bprm, struct pt_regs *regs)
280 {
281         unsigned long error, fd_offset, rlim;
282         struct exec ex;
283         int retval;
284
285         ex = *((struct exec *) bprm->buf);              /* exec-header */
286         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
287              N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
288             N_TRSIZE(ex) || N_DRSIZE(ex) ||
289             i_size_read(bprm->file->f_path.dentry->d_inode) <
290             ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
291                 return -ENOEXEC;
292         }
293
294         fd_offset = N_TXTOFF(ex);
295
296         /* Check initial limits. This avoids letting people circumvent
297          * size limits imposed on them by creating programs with large
298          * arrays in the data or bss.
299          */
300         rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
301         if (rlim >= RLIM_INFINITY)
302                 rlim = ~0;
303         if (ex.a_data + ex.a_bss > rlim)
304                 return -ENOMEM;
305
306         /* Flush all traces of the currently running executable */
307         retval = flush_old_exec(bprm);
308         if (retval)
309                 return retval;
310
311         regs->cs = __USER32_CS;
312         regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
313                 regs->r13 = regs->r14 = regs->r15 = 0;
314
315         /* OK, This is the point of no return */
316         set_personality(PER_LINUX);
317         set_thread_flag(TIF_IA32);
318         clear_thread_flag(TIF_ABI_PENDING);
319
320         current->mm->end_code = ex.a_text +
321                 (current->mm->start_code = N_TXTADDR(ex));
322         current->mm->end_data = ex.a_data +
323                 (current->mm->start_data = N_DATADDR(ex));
324         current->mm->brk = ex.a_bss +
325                 (current->mm->start_brk = N_BSSADDR(ex));
326         current->mm->free_area_cache = TASK_UNMAPPED_BASE;
327         current->mm->cached_hole_size = 0;
328
329         current->mm->mmap = NULL;
330         install_exec_creds(bprm);
331         current->flags &= ~PF_FORKNOEXEC;
332
333         if (N_MAGIC(ex) == OMAGIC) {
334                 unsigned long text_addr, map_size;
335                 loff_t pos;
336
337                 text_addr = N_TXTADDR(ex);
338
339                 pos = 32;
340                 map_size = ex.a_text+ex.a_data;
341
342                 down_write(&current->mm->mmap_sem);
343                 error = do_brk(text_addr & PAGE_MASK, map_size);
344                 up_write(&current->mm->mmap_sem);
345
346                 if (error != (text_addr & PAGE_MASK)) {
347                         send_sig(SIGKILL, current, 0);
348                         return error;
349                 }
350
351                 error = bprm->file->f_op->read(bprm->file,
352                          (char __user *)text_addr,
353                           ex.a_text+ex.a_data, &pos);
354                 if ((signed long)error < 0) {
355                         send_sig(SIGKILL, current, 0);
356                         return error;
357                 }
358
359                 flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
360         } else {
361 #ifdef WARN_OLD
362                 static unsigned long error_time, error_time2;
363                 if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
364                     (N_MAGIC(ex) != NMAGIC) &&
365                                 time_after(jiffies, error_time2 + 5*HZ)) {
366                         printk(KERN_NOTICE "executable not page aligned\n");
367                         error_time2 = jiffies;
368                 }
369
370                 if ((fd_offset & ~PAGE_MASK) != 0 &&
371                             time_after(jiffies, error_time + 5*HZ)) {
372                         printk(KERN_WARNING
373                                "fd_offset is not page aligned. Please convert "
374                                "program: %s\n",
375                                bprm->file->f_path.dentry->d_name.name);
376                         error_time = jiffies;
377                 }
378 #endif
379
380                 if (!bprm->file->f_op->mmap || (fd_offset & ~PAGE_MASK) != 0) {
381                         loff_t pos = fd_offset;
382
383                         down_write(&current->mm->mmap_sem);
384                         do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
385                         up_write(&current->mm->mmap_sem);
386                         bprm->file->f_op->read(bprm->file,
387                                         (char __user *)N_TXTADDR(ex),
388                                         ex.a_text+ex.a_data, &pos);
389                         flush_icache_range((unsigned long) N_TXTADDR(ex),
390                                            (unsigned long) N_TXTADDR(ex) +
391                                            ex.a_text+ex.a_data);
392                         goto beyond_if;
393                 }
394
395                 down_write(&current->mm->mmap_sem);
396                 error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
397                                 PROT_READ | PROT_EXEC,
398                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
399                                 MAP_EXECUTABLE | MAP_32BIT,
400                                 fd_offset);
401                 up_write(&current->mm->mmap_sem);
402
403                 if (error != N_TXTADDR(ex)) {
404                         send_sig(SIGKILL, current, 0);
405                         return error;
406                 }
407
408                 down_write(&current->mm->mmap_sem);
409                 error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
410                                 PROT_READ | PROT_WRITE | PROT_EXEC,
411                                 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
412                                 MAP_EXECUTABLE | MAP_32BIT,
413                                 fd_offset + ex.a_text);
414                 up_write(&current->mm->mmap_sem);
415                 if (error != N_DATADDR(ex)) {
416                         send_sig(SIGKILL, current, 0);
417                         return error;
418                 }
419         }
420 beyond_if:
421         set_binfmt(&aout_format);
422
423         set_brk(current->mm->start_brk, current->mm->brk);
424
425         retval = setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT);
426         if (retval < 0) {
427                 /* Someone check-me: is this error path enough? */
428                 send_sig(SIGKILL, current, 0);
429                 return retval;
430         }
431
432         current->mm->start_stack =
433                 (unsigned long)create_aout_tables((char __user *)bprm->p, bprm);
434         /* start thread */
435         loadsegment(fs, 0);
436         loadsegment(ds, __USER32_DS);
437         loadsegment(es, __USER32_DS);
438         load_gs_index(0);
439         (regs)->ip = ex.a_entry;
440         (regs)->sp = current->mm->start_stack;
441         (regs)->flags = 0x200;
442         (regs)->cs = __USER32_CS;
443         (regs)->ss = __USER32_DS;
444         regs->r8 = regs->r9 = regs->r10 = regs->r11 =
445         regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;
446         set_fs(USER_DS);
447         return 0;
448 }
449
450 static int load_aout_library(struct file *file)
451 {
452         struct inode *inode;
453         unsigned long bss, start_addr, len, error;
454         int retval;
455         struct exec ex;
456
457         inode = file->f_path.dentry->d_inode;
458
459         retval = -ENOEXEC;
460         error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
461         if (error != sizeof(ex))
462                 goto out;
463
464         /* We come in here for the regular a.out style of shared libraries */
465         if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
466             N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
467             i_size_read(inode) <
468             ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
469                 goto out;
470         }
471
472         if (N_FLAGS(ex))
473                 goto out;
474
475         /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
476            this off to get the starting address for the page */
477
478         start_addr =  ex.a_entry & 0xfffff000;
479
480         if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
481                 loff_t pos = N_TXTOFF(ex);
482
483 #ifdef WARN_OLD
484                 static unsigned long error_time;
485                 if (time_after(jiffies, error_time + 5*HZ)) {
486                         printk(KERN_WARNING
487                                "N_TXTOFF is not page aligned. Please convert "
488                                "library: %s\n",
489                                file->f_path.dentry->d_name.name);
490                         error_time = jiffies;
491                 }
492 #endif
493                 down_write(&current->mm->mmap_sem);
494                 do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
495                 up_write(&current->mm->mmap_sem);
496
497                 file->f_op->read(file, (char __user *)start_addr,
498                         ex.a_text + ex.a_data, &pos);
499                 flush_icache_range((unsigned long) start_addr,
500                                    (unsigned long) start_addr + ex.a_text +
501                                    ex.a_data);
502
503                 retval = 0;
504                 goto out;
505         }
506         /* Now use mmap to map the library into memory. */
507         down_write(&current->mm->mmap_sem);
508         error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
509                         PROT_READ | PROT_WRITE | PROT_EXEC,
510                         MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_32BIT,
511                         N_TXTOFF(ex));
512         up_write(&current->mm->mmap_sem);
513         retval = error;
514         if (error != start_addr)
515                 goto out;
516
517         len = PAGE_ALIGN(ex.a_text + ex.a_data);
518         bss = ex.a_text + ex.a_data + ex.a_bss;
519         if (bss > len) {
520                 down_write(&current->mm->mmap_sem);
521                 error = do_brk(start_addr + len, bss - len);
522                 up_write(&current->mm->mmap_sem);
523                 retval = error;
524                 if (error != start_addr + len)
525                         goto out;
526         }
527         retval = 0;
528 out:
529         return retval;
530 }
531
532 static int __init init_aout_binfmt(void)
533 {
534         return register_binfmt(&aout_format);
535 }
536
537 static void __exit exit_aout_binfmt(void)
538 {
539         unregister_binfmt(&aout_format);
540 }
541
542 module_init(init_aout_binfmt);
543 module_exit(exit_aout_binfmt);
544 MODULE_LICENSE("GPL");