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