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