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