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