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