linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / fs / binfmt_elf_fdpic.c
1 /* binfmt_elf_fdpic.c: FDPIC ELF binary format
2  *
3  * Copyright (C) 2003, 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  * Derived from binfmt_elf.c
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/module.h>
14
15 #include <linux/fs.h>
16 #include <linux/stat.h>
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/mman.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/binfmts.h>
23 #include <linux/string.h>
24 #include <linux/file.h>
25 #include <linux/fcntl.h>
26 #include <linux/slab.h>
27 #include <linux/highmem.h>
28 #include <linux/personality.h>
29 #include <linux/ptrace.h>
30 #include <linux/init.h>
31 #include <linux/smp_lock.h>
32 #include <linux/elf.h>
33 #include <linux/elf-fdpic.h>
34 #include <linux/elfcore.h>
35 #include <linux/vs_cvirt.h>
36
37 #include <asm/uaccess.h>
38 #include <asm/param.h>
39 #include <asm/pgalloc.h>
40
41 typedef char *elf_caddr_t;
42 #ifndef elf_addr_t
43 #define elf_addr_t unsigned long
44 #endif
45
46 #if 0
47 #define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
48 #else
49 #define kdebug(fmt, ...) do {} while(0)
50 #endif
51
52 MODULE_LICENSE("GPL");
53
54 static int load_elf_fdpic_binary(struct linux_binprm *bprm, struct pt_regs *regs);
55 //static int load_elf_fdpic_library(struct file *);
56 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, struct file *file);
57 static int elf_fdpic_map_file(struct elf_fdpic_params *params,
58                               struct file *file,
59                               struct mm_struct *mm,
60                               const char *what);
61
62 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
63                                    struct mm_struct *mm,
64                                    struct elf_fdpic_params *exec_params,
65                                    struct elf_fdpic_params *interp_params);
66
67 #ifndef CONFIG_MMU
68 static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp);
69 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params,
70                                                    struct file *file,
71                                                    struct mm_struct *mm);
72 #endif
73
74 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
75                                              struct file *file,
76                                              struct mm_struct *mm);
77
78 static struct linux_binfmt elf_fdpic_format = {
79         .module         = THIS_MODULE,
80         .load_binary    = load_elf_fdpic_binary,
81 //      .load_shlib     = load_elf_fdpic_library,
82 //      .core_dump      = elf_fdpic_core_dump,
83         .min_coredump   = ELF_EXEC_PAGESIZE,
84 };
85
86 static int __init init_elf_fdpic_binfmt(void)  { return register_binfmt(&elf_fdpic_format); }
87 static void __exit exit_elf_fdpic_binfmt(void) { unregister_binfmt(&elf_fdpic_format); }
88
89 module_init(init_elf_fdpic_binfmt)
90 module_exit(exit_elf_fdpic_binfmt)
91
92 static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
93 {
94         if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
95                 return 0;
96         if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
97                 return 0;
98         if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
99                 return 0;
100         if (!file->f_op || !file->f_op->mmap)
101                 return 0;
102         return 1;
103 }
104
105 /*****************************************************************************/
106 /*
107  * read the program headers table into memory
108  */
109 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, struct file *file)
110 {
111         struct elf32_phdr *phdr;
112         unsigned long size;
113         int retval, loop;
114
115         if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
116                 return -ENOMEM;
117         if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
118                 return -ENOMEM;
119
120         size = params->hdr.e_phnum * sizeof(struct elf_phdr);
121         params->phdrs = kmalloc(size, GFP_KERNEL);
122         if (!params->phdrs)
123                 return -ENOMEM;
124
125         retval = kernel_read(file, params->hdr.e_phoff, (char *) params->phdrs, size);
126         if (retval < 0)
127                 return retval;
128
129         /* determine stack size for this binary */
130         phdr = params->phdrs;
131         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
132                 if (phdr->p_type != PT_GNU_STACK)
133                         continue;
134
135                 if (phdr->p_flags & PF_X)
136                         params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
137                 else
138                         params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
139
140                 params->stack_size = phdr->p_memsz;
141                 break;
142         }
143
144         return 0;
145 } /* end elf_fdpic_fetch_phdrs() */
146
147 /*****************************************************************************/
148 /*
149  * load an fdpic binary into various bits of memory
150  */
151 static int load_elf_fdpic_binary(struct linux_binprm *bprm, struct pt_regs *regs)
152 {
153         struct elf_fdpic_params exec_params, interp_params;
154         struct elf_phdr *phdr;
155         unsigned long stack_size;
156         struct file *interpreter = NULL; /* to shut gcc up */
157         char *interpreter_name = NULL;
158         int executable_stack;
159         int retval, i;
160
161         memset(&exec_params, 0, sizeof(exec_params));
162         memset(&interp_params, 0, sizeof(interp_params));
163
164         exec_params.hdr = *(struct elfhdr *) bprm->buf;
165         exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
166
167         /* check that this is a binary we know how to deal with */
168         retval = -ENOEXEC;
169         if (!is_elf_fdpic(&exec_params.hdr, bprm->file))
170                 goto error;
171
172         /* read the program header table */
173         retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
174         if (retval < 0)
175                 goto error;
176
177         /* scan for a program header that specifies an interpreter */
178         phdr = exec_params.phdrs;
179
180         for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
181                 switch (phdr->p_type) {
182                 case PT_INTERP:
183                         retval = -ENOMEM;
184                         if (phdr->p_filesz > PATH_MAX)
185                                 goto error;
186                         retval = -ENOENT;
187                         if (phdr->p_filesz < 2)
188                                 goto error;
189
190                         /* read the name of the interpreter into memory */
191                         interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
192                         if (!interpreter_name)
193                                 goto error;
194
195                         retval = kernel_read(bprm->file,
196                                              phdr->p_offset,
197                                              interpreter_name,
198                                              phdr->p_filesz);
199                         if (retval < 0)
200                                 goto error;
201
202                         retval = -ENOENT;
203                         if (interpreter_name[phdr->p_filesz - 1] != '\0')
204                                 goto error;
205
206                         kdebug("Using ELF interpreter %s", interpreter_name);
207
208                         /* replace the program with the interpreter */
209                         interpreter = open_exec(interpreter_name);
210                         retval = PTR_ERR(interpreter);
211                         if (IS_ERR(interpreter)) {
212                                 interpreter = NULL;
213                                 goto error;
214                         }
215
216                         retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
217                         if (retval < 0)
218                                 goto error;
219
220                         interp_params.hdr = *((struct elfhdr *) bprm->buf);
221                         break;
222
223                 case PT_LOAD:
224 #ifdef CONFIG_MMU
225                         if (exec_params.load_addr == 0)
226                                 exec_params.load_addr = phdr->p_vaddr;
227 #endif
228                         break;
229                 }
230
231         }
232
233         if (elf_check_const_displacement(&exec_params.hdr))
234                 exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
235
236         /* perform insanity checks on the interpreter */
237         if (interpreter_name) {
238                 retval = -ELIBBAD;
239                 if (!is_elf_fdpic(&interp_params.hdr, interpreter))
240                         goto error;
241
242                 interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
243
244                 /* read the interpreter's program header table */
245                 retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
246                 if (retval < 0)
247                         goto error;
248         }
249
250         stack_size = exec_params.stack_size;
251         if (stack_size < interp_params.stack_size)
252                 stack_size = interp_params.stack_size;
253
254         if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
255                 executable_stack = EXSTACK_ENABLE_X;
256         else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
257                 executable_stack = EXSTACK_DISABLE_X;
258         else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
259                 executable_stack = EXSTACK_ENABLE_X;
260         else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
261                 executable_stack = EXSTACK_DISABLE_X;
262         else
263                 executable_stack = EXSTACK_DEFAULT;
264
265         retval = -ENOEXEC;
266         if (stack_size == 0)
267                 goto error;
268
269         if (elf_check_const_displacement(&interp_params.hdr))
270                 interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
271
272         /* flush all traces of the currently running executable */
273         retval = flush_old_exec(bprm);
274         if (retval)
275                 goto error;
276
277         /* there's now no turning back... the old userspace image is dead,
278          * defunct, deceased, etc. after this point we have to exit via
279          * error_kill */
280         set_personality(PER_LINUX_FDPIC);
281         set_binfmt(&elf_fdpic_format);
282
283         current->mm->start_code = 0;
284         current->mm->end_code = 0;
285         current->mm->start_stack = 0;
286         current->mm->start_data = 0;
287         current->mm->end_data = 0;
288         current->mm->context.exec_fdpic_loadmap = 0;
289         current->mm->context.interp_fdpic_loadmap = 0;
290
291         current->flags &= ~PF_FORKNOEXEC;
292
293 #ifdef CONFIG_MMU
294         elf_fdpic_arch_lay_out_mm(&exec_params,
295                                   &interp_params,
296                                   &current->mm->start_stack,
297                                   &current->mm->start_brk);
298
299         retval = setup_arg_pages(bprm, current->mm->start_stack, executable_stack);
300         if (retval < 0) {
301                 send_sig(SIGKILL, current, 0);
302                 goto error_kill;
303         }
304 #endif
305
306         /* load the executable and interpreter into memory */
307         retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm, "executable");
308         if (retval < 0)
309                 goto error_kill;
310
311         if (interpreter_name) {
312                 retval = elf_fdpic_map_file(&interp_params, interpreter,
313                                             current->mm, "interpreter");
314                 if (retval < 0) {
315                         printk(KERN_ERR "Unable to load interpreter\n");
316                         goto error_kill;
317                 }
318
319                 allow_write_access(interpreter);
320                 fput(interpreter);
321                 interpreter = NULL;
322         }
323
324 #ifdef CONFIG_MMU
325         if (!current->mm->start_brk)
326                 current->mm->start_brk = current->mm->end_data;
327
328         current->mm->brk = current->mm->start_brk = PAGE_ALIGN(current->mm->start_brk);
329
330 #else
331         /* create a stack and brk area big enough for everyone
332          * - the brk heap starts at the bottom and works up
333          * - the stack starts at the top and works down
334          */
335         stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
336         if (stack_size < PAGE_SIZE * 2)
337                 stack_size = PAGE_SIZE * 2;
338
339         down_write(&current->mm->mmap_sem);
340         current->mm->start_brk = do_mmap(NULL,
341                                          0,
342                                          stack_size,
343                                          PROT_READ | PROT_WRITE | PROT_EXEC,
344                                          MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN,
345                                          0);
346
347         if (IS_ERR((void *) current->mm->start_brk)) {
348                 up_write(&current->mm->mmap_sem);
349                 retval = current->mm->start_brk;
350                 current->mm->start_brk = 0;
351                 goto error_kill;
352         }
353
354         if (do_mremap(current->mm->start_brk,
355                       stack_size,
356                       ksize((char *) current->mm->start_brk),
357                       0, 0
358                       ) == current->mm->start_brk
359             )
360                 stack_size = ksize((char *) current->mm->start_brk);
361         up_write(&current->mm->mmap_sem);
362
363         current->mm->brk = current->mm->start_brk;
364         current->mm->context.end_brk = current->mm->start_brk;
365         current->mm->context.end_brk += (stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
366         current->mm->start_stack = current->mm->start_brk + stack_size;
367 #endif
368
369         compute_creds(bprm);
370         current->flags &= ~PF_FORKNOEXEC;
371         if (create_elf_fdpic_tables(bprm, current->mm, &exec_params, &interp_params) < 0)
372                 goto error_kill;
373
374         kdebug("- start_code  %lx",     (long) current->mm->start_code);
375         kdebug("- end_code    %lx",     (long) current->mm->end_code);
376         kdebug("- start_data  %lx",     (long) current->mm->start_data);
377         kdebug("- end_data    %lx",     (long) current->mm->end_data);
378         kdebug("- start_brk   %lx",     (long) current->mm->start_brk);
379         kdebug("- brk         %lx",     (long) current->mm->brk);
380         kdebug("- start_stack %lx",     (long) current->mm->start_stack);
381
382 #ifdef ELF_FDPIC_PLAT_INIT
383         /*
384          * The ABI may specify that certain registers be set up in special
385          * ways (on i386 %edx is the address of a DT_FINI function, for
386          * example.  This macro performs whatever initialization to
387          * the regs structure is required.
388          */
389         ELF_FDPIC_PLAT_INIT(regs,
390                             exec_params.map_addr,
391                             interp_params.map_addr,
392                             interp_params.dynamic_addr ?: exec_params.dynamic_addr
393                             );
394 #endif
395
396         /* everything is now ready... get the userspace context ready to roll */
397         start_thread(regs,
398                      interp_params.entry_addr ?: exec_params.entry_addr,
399                      current->mm->start_stack);
400
401         if (unlikely(current->ptrace & PT_PTRACED)) {
402                 if (current->ptrace & PT_TRACE_EXEC)
403                         ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
404                 else
405                         send_sig(SIGTRAP, current, 0);
406         }
407
408         retval = 0;
409
410 error:
411         if (interpreter) {
412                 allow_write_access(interpreter);
413                 fput(interpreter);
414         }
415         kfree(interpreter_name);
416         kfree(exec_params.phdrs);
417         kfree(exec_params.loadmap);
418         kfree(interp_params.phdrs);
419         kfree(interp_params.loadmap);
420         return retval;
421
422         /* unrecoverable error - kill the process */
423  error_kill:
424         send_sig(SIGSEGV, current, 0);
425         goto error;
426
427 } /* end load_elf_fdpic_binary() */
428
429 /*****************************************************************************/
430 /*
431  * present useful information to the program
432  */
433 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
434                                    struct mm_struct *mm,
435                                    struct elf_fdpic_params *exec_params,
436                                    struct elf_fdpic_params *interp_params)
437 {
438         unsigned long sp, csp, nitems;
439         elf_caddr_t *argv, *envp;
440         size_t platform_len = 0, len;
441         char *k_platform, *u_platform, *p;
442         long hwcap;
443         int loop;
444
445         /* we're going to shovel a whole load of stuff onto the stack */
446 #ifdef CONFIG_MMU
447         sp = bprm->p;
448 #else
449         sp = mm->start_stack;
450
451         /* stack the program arguments and environment */
452         if (elf_fdpic_transfer_args_to_stack(bprm, &sp) < 0)
453                 return -EFAULT;
454 #endif
455
456         /* get hold of platform and hardware capabilities masks for the machine
457          * we are running on.  In some cases (Sparc), this info is impossible
458          * to get, in others (i386) it is merely difficult.
459          */
460         hwcap = ELF_HWCAP;
461         k_platform = ELF_PLATFORM;
462
463         if (k_platform) {
464                 platform_len = strlen(k_platform) + 1;
465                 sp -= platform_len;
466                 if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
467                         return -EFAULT;
468         }
469
470         u_platform = (char *) sp;
471
472 #if defined(__i386__) && defined(CONFIG_SMP)
473         /* in some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
474          * by the processes running on the same package. One thing we can do
475          * is to shuffle the initial stack for them.
476          *
477          * the conditionals here are unneeded, but kept in to make the
478          * code behaviour the same as pre change unless we have hyperthreaded
479          * processors. This keeps Mr Marcelo Person happier but should be
480          * removed for 2.5
481          */
482         if (smp_num_siblings > 1)
483                 sp = sp - ((current->pid % 64) << 7);
484 #endif
485
486         sp &= ~7UL;
487
488         /* stack the load map(s) */
489         len = sizeof(struct elf32_fdpic_loadmap);
490         len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
491         sp = (sp - len) & ~7UL;
492         exec_params->map_addr = sp;
493
494         if (copy_to_user((void *) sp, exec_params->loadmap, len) != 0)
495                 return -EFAULT;
496
497         current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
498
499         if (interp_params->loadmap) {
500                 len = sizeof(struct elf32_fdpic_loadmap);
501                 len += sizeof(struct elf32_fdpic_loadseg) * interp_params->loadmap->nsegs;
502                 sp = (sp - len) & ~7UL;
503                 interp_params->map_addr = sp;
504
505                 if (copy_to_user((void *) sp, interp_params->loadmap, len) != 0)
506                         return -EFAULT;
507
508                 current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
509         }
510
511         /* force 16 byte _final_ alignment here for generality */
512 #define DLINFO_ITEMS 13
513
514         nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0);
515 #ifdef DLINFO_ARCH_ITEMS
516         nitems += DLINFO_ARCH_ITEMS;
517 #endif
518
519         csp = sp;
520         sp -= nitems * 2 * sizeof(unsigned long);
521         sp -= (bprm->envc + 1) * sizeof(char *);        /* envv[] */
522         sp -= (bprm->argc + 1) * sizeof(char *);        /* argv[] */
523         sp -= 1 * sizeof(unsigned long);                /* argc */
524
525         csp -= sp & 15UL;
526         sp -= sp & 15UL;
527
528         /* put the ELF interpreter info on the stack */
529 #define NEW_AUX_ENT(nr, id, val)                                                \
530         do {                                                                    \
531                 struct { unsigned long _id, _val; } *ent = (void *) csp;        \
532                 __put_user((id), &ent[nr]._id);                                 \
533                 __put_user((val), &ent[nr]._val);                               \
534         } while (0)
535
536         csp -= 2 * sizeof(unsigned long);
537         NEW_AUX_ENT(0, AT_NULL, 0);
538         if (k_platform) {
539                 csp -= 2 * sizeof(unsigned long);
540                 NEW_AUX_ENT(0, AT_PLATFORM, (elf_addr_t)(unsigned long) u_platform);
541         }
542
543         csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
544         NEW_AUX_ENT( 0, AT_HWCAP,               hwcap);
545         NEW_AUX_ENT( 1, AT_PAGESZ,              PAGE_SIZE);
546         NEW_AUX_ENT( 2, AT_CLKTCK,              CLOCKS_PER_SEC);
547         NEW_AUX_ENT( 3, AT_PHDR,                exec_params->ph_addr);
548         NEW_AUX_ENT( 4, AT_PHENT,               sizeof(struct elf_phdr));
549         NEW_AUX_ENT( 5, AT_PHNUM,               exec_params->hdr.e_phnum);
550         NEW_AUX_ENT( 6, AT_BASE,                interp_params->elfhdr_addr);
551         NEW_AUX_ENT( 7, AT_FLAGS,               0);
552         NEW_AUX_ENT( 8, AT_ENTRY,               exec_params->entry_addr);
553         NEW_AUX_ENT( 9, AT_UID,                 (elf_addr_t) current->uid);
554         NEW_AUX_ENT(10, AT_EUID,                (elf_addr_t) current->euid);
555         NEW_AUX_ENT(11, AT_GID,                 (elf_addr_t) current->gid);
556         NEW_AUX_ENT(12, AT_EGID,                (elf_addr_t) current->egid);
557
558 #ifdef ARCH_DLINFO
559         /* ARCH_DLINFO must come last so platform specific code can enforce
560          * special alignment requirements on the AUXV if necessary (eg. PPC).
561          */
562         ARCH_DLINFO;
563 #endif
564 #undef NEW_AUX_ENT
565
566         /* allocate room for argv[] and envv[] */
567         csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
568         envp = (elf_caddr_t *) csp;
569         csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
570         argv = (elf_caddr_t *) csp;
571
572         /* stack argc */
573         csp -= sizeof(unsigned long);
574         __put_user(bprm->argc, (unsigned long *) csp);
575
576         if (csp != sp)
577                 BUG();
578
579         /* fill in the argv[] array */
580 #ifdef CONFIG_MMU
581         current->mm->arg_start = bprm->p;
582 #else
583         current->mm->arg_start = current->mm->start_stack - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
584 #endif
585
586         p = (char *) current->mm->arg_start;
587         for (loop = bprm->argc; loop > 0; loop--) {
588                 __put_user((elf_caddr_t) p, argv++);
589                 len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
590                 if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
591                         return -EINVAL;
592                 p += len;
593         }
594         __put_user(NULL, argv);
595         current->mm->arg_end = (unsigned long) p;
596
597         /* fill in the envv[] array */
598         current->mm->env_start = (unsigned long) p;
599         for (loop = bprm->envc; loop > 0; loop--) {
600                 __put_user((elf_caddr_t)(unsigned long) p, envp++);
601                 len = strnlen_user(p, PAGE_SIZE * MAX_ARG_PAGES);
602                 if (!len || len > PAGE_SIZE * MAX_ARG_PAGES)
603                         return -EINVAL;
604                 p += len;
605         }
606         __put_user(NULL, envp);
607         current->mm->env_end = (unsigned long) p;
608
609         mm->start_stack = (unsigned long) sp;
610         return 0;
611 } /* end create_elf_fdpic_tables() */
612
613 /*****************************************************************************/
614 /*
615  * transfer the program arguments and environment from the holding pages onto
616  * the stack
617  */
618 #ifndef CONFIG_MMU
619 static int elf_fdpic_transfer_args_to_stack(struct linux_binprm *bprm, unsigned long *_sp)
620 {
621         unsigned long index, stop, sp;
622         char *src;
623         int ret = 0;
624
625         stop = bprm->p >> PAGE_SHIFT;
626         sp = *_sp;
627
628         for (index = MAX_ARG_PAGES - 1; index >= stop; index--) {
629                 src = kmap(bprm->page[index]);
630                 sp -= PAGE_SIZE;
631                 if (copy_to_user((void *) sp, src, PAGE_SIZE) != 0)
632                         ret = -EFAULT;
633                 kunmap(bprm->page[index]);
634                 if (ret < 0)
635                         goto out;
636         }
637
638         *_sp = (*_sp - (MAX_ARG_PAGES * PAGE_SIZE - bprm->p)) & ~15;
639
640  out:
641         return ret;
642 } /* end elf_fdpic_transfer_args_to_stack() */
643 #endif
644
645 /*****************************************************************************/
646 /*
647  * load the appropriate binary image (executable or interpreter) into memory
648  * - we assume no MMU is available
649  * - if no other PIC bits are set in params->hdr->e_flags
650  *   - we assume that the LOADable segments in the binary are independently relocatable
651  *   - we assume R/O executable segments are shareable
652  * - else
653  *   - we assume the loadable parts of the image to require fixed displacement
654  *   - the image is not shareable
655  */
656 static int elf_fdpic_map_file(struct elf_fdpic_params *params,
657                               struct file *file,
658                               struct mm_struct *mm,
659                               const char *what)
660 {
661         struct elf32_fdpic_loadmap *loadmap;
662 #ifdef CONFIG_MMU
663         struct elf32_fdpic_loadseg *mseg;
664 #endif
665         struct elf32_fdpic_loadseg *seg;
666         struct elf32_phdr *phdr;
667         unsigned long load_addr, stop;
668         unsigned nloads, tmp;
669         size_t size;
670         int loop, ret;
671
672         /* allocate a load map table */
673         nloads = 0;
674         for (loop = 0; loop < params->hdr.e_phnum; loop++)
675                 if (params->phdrs[loop].p_type == PT_LOAD)
676                         nloads++;
677
678         if (nloads == 0)
679                 return -ELIBBAD;
680
681         size = sizeof(*loadmap) + nloads * sizeof(*seg);
682         loadmap = kmalloc(size, GFP_KERNEL);
683         if (!loadmap)
684                 return -ENOMEM;
685
686         params->loadmap = loadmap;
687         memset(loadmap, 0, size);
688
689         loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
690         loadmap->nsegs = nloads;
691
692         load_addr = params->load_addr;
693         seg = loadmap->segs;
694
695         /* map the requested LOADs into the memory space */
696         switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
697         case ELF_FDPIC_FLAG_CONSTDISP:
698         case ELF_FDPIC_FLAG_CONTIGUOUS:
699 #ifndef CONFIG_MMU
700                 ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
701                 if (ret < 0)
702                         return ret;
703                 break;
704 #endif
705         default:
706                 ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
707                 if (ret < 0)
708                         return ret;
709                 break;
710         }
711
712         /* map the entry point */
713         if (params->hdr.e_entry) {
714                 seg = loadmap->segs;
715                 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
716                         if (params->hdr.e_entry >= seg->p_vaddr &&
717                             params->hdr.e_entry < seg->p_vaddr + seg->p_memsz
718                             ) {
719                                 params->entry_addr =
720                                         (params->hdr.e_entry - seg->p_vaddr) + seg->addr;
721                                 break;
722                         }
723                 }
724         }
725
726         /* determine where the program header table has wound up if mapped */
727         stop = params->hdr.e_phoff + params->hdr.e_phnum * sizeof (struct elf_phdr);
728         phdr = params->phdrs;
729
730         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
731                 if (phdr->p_type != PT_LOAD)
732                         continue;
733
734                 if (phdr->p_offset > params->hdr.e_phoff ||
735                     phdr->p_offset + phdr->p_filesz < stop)
736                         continue;
737
738                 seg = loadmap->segs;
739                 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
740                         if (phdr->p_vaddr >= seg->p_vaddr &&
741                             phdr->p_vaddr + phdr->p_filesz <= seg->p_vaddr + seg->p_memsz
742                             ) {
743                                 params->ph_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr +
744                                         params->hdr.e_phoff - phdr->p_offset;
745                                 break;
746                         }
747                 }
748                 break;
749         }
750
751         /* determine where the dynamic section has wound up if there is one */
752         phdr = params->phdrs;
753         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
754                 if (phdr->p_type != PT_DYNAMIC)
755                         continue;
756
757                 seg = loadmap->segs;
758                 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
759                         if (phdr->p_vaddr >= seg->p_vaddr &&
760                             phdr->p_vaddr + phdr->p_memsz <= seg->p_vaddr + seg->p_memsz
761                             ) {
762                                 params->dynamic_addr = (phdr->p_vaddr - seg->p_vaddr) + seg->addr;
763
764                                 /* check the dynamic section contains at least one item, and that
765                                  * the last item is a NULL entry */
766                                 if (phdr->p_memsz == 0 ||
767                                     phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
768                                         goto dynamic_error;
769
770                                 tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
771                                 if (((Elf32_Dyn *) params->dynamic_addr)[tmp - 1].d_tag != 0)
772                                         goto dynamic_error;
773                                 break;
774                         }
775                 }
776                 break;
777         }
778
779         /* now elide adjacent segments in the load map on MMU linux
780          * - on uClinux the holes between may actually be filled with system stuff or stuff from
781          *   other processes
782          */
783 #ifdef CONFIG_MMU
784         nloads = loadmap->nsegs;
785         mseg = loadmap->segs;
786         seg = mseg + 1;
787         for (loop = 1; loop < nloads; loop++) {
788                 /* see if we have a candidate for merging */
789                 if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
790                         load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
791                         if (load_addr == (seg->addr & PAGE_MASK)) {
792                                 mseg->p_memsz += load_addr - (mseg->addr + mseg->p_memsz);
793                                 mseg->p_memsz += seg->addr & ~PAGE_MASK;
794                                 mseg->p_memsz += seg->p_memsz;
795                                 loadmap->nsegs--;
796                                 continue;
797                         }
798                 }
799
800                 mseg++;
801                 if (mseg != seg)
802                         *mseg = *seg;
803         }
804 #endif
805
806         kdebug("Mapped Object [%s]:", what);
807         kdebug("- elfhdr   : %lx", params->elfhdr_addr);
808         kdebug("- entry    : %lx", params->entry_addr);
809         kdebug("- PHDR[]   : %lx", params->ph_addr);
810         kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
811         seg = loadmap->segs;
812         for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
813                 kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
814                        loop,
815                        seg->addr, seg->addr + seg->p_memsz - 1,
816                        seg->p_vaddr, seg->p_memsz);
817
818         return 0;
819
820  dynamic_error:
821         printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
822                what, file->f_dentry->d_inode->i_ino);
823         return -ELIBBAD;
824 } /* end elf_fdpic_map_file() */
825
826 /*****************************************************************************/
827 /*
828  * map a file with constant displacement under uClinux
829  */
830 #ifndef CONFIG_MMU
831 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *params,
832                                                    struct file *file,
833                                                    struct mm_struct *mm)
834 {
835         struct elf32_fdpic_loadseg *seg;
836         struct elf32_phdr *phdr;
837         unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
838         loff_t fpos;
839         int loop, ret;
840
841         load_addr = params->load_addr;
842         seg = params->loadmap->segs;
843
844         /* determine the bounds of the contiguous overall allocation we must make */
845         phdr = params->phdrs;
846         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
847                 if (params->phdrs[loop].p_type != PT_LOAD)
848                         continue;
849
850                 if (base > phdr->p_vaddr)
851                         base = phdr->p_vaddr;
852                 if (top < phdr->p_vaddr + phdr->p_memsz)
853                         top = phdr->p_vaddr + phdr->p_memsz;
854         }
855
856         /* allocate one big anon block for everything */
857         mflags = MAP_PRIVATE;
858         if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
859                 mflags |= MAP_EXECUTABLE;
860
861         down_write(&mm->mmap_sem);
862         maddr = do_mmap(NULL, load_addr, top - base,
863                         PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
864         up_write(&mm->mmap_sem);
865         if (IS_ERR((void *) maddr))
866                 return (int) maddr;
867
868         if (load_addr != 0)
869                 load_addr += PAGE_ALIGN(top - base);
870
871         /* and then load the file segments into it */
872         phdr = params->phdrs;
873         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
874                 if (params->phdrs[loop].p_type != PT_LOAD)
875                         continue;
876
877                 fpos = phdr->p_offset;
878
879                 seg->addr = maddr + (phdr->p_vaddr - base);
880                 seg->p_vaddr = phdr->p_vaddr;
881                 seg->p_memsz = phdr->p_memsz;
882
883                 ret = file->f_op->read(file, (void *) seg->addr, phdr->p_filesz, &fpos);
884                 if (ret < 0)
885                         return ret;
886
887                 /* map the ELF header address if in this segment */
888                 if (phdr->p_offset == 0)
889                         params->elfhdr_addr = seg->addr;
890
891                 /* clear any space allocated but not loaded */
892                 if (phdr->p_filesz < phdr->p_memsz)
893                         clear_user((void *) (seg->addr + phdr->p_filesz),
894                                    phdr->p_memsz - phdr->p_filesz);
895
896                 if (mm) {
897                         if (phdr->p_flags & PF_X) {
898                                 mm->start_code = seg->addr;
899                                 mm->end_code = seg->addr + phdr->p_memsz;
900                         }
901                         else if (!mm->start_data) {
902                                 mm->start_data = seg->addr;
903 #ifndef CONFIG_MMU
904                                 mm->end_data = seg->addr + phdr->p_memsz;
905 #endif
906                         }
907
908 #ifdef CONFIG_MMU
909                         if (seg->addr + phdr->p_memsz > mm->end_data)
910                                 mm->end_data = seg->addr + phdr->p_memsz;
911 #endif
912                 }
913
914                 seg++;
915         }
916
917         return 0;
918 } /* end elf_fdpic_map_file_constdisp_on_uclinux() */
919 #endif
920
921 /*****************************************************************************/
922 /*
923  * map a binary by direct mmap() of the individual PT_LOAD segments
924  */
925 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
926                                              struct file *file,
927                                              struct mm_struct *mm)
928 {
929         struct elf32_fdpic_loadseg *seg;
930         struct elf32_phdr *phdr;
931         unsigned long load_addr, delta_vaddr;
932         int loop, dvset;
933
934         load_addr = params->load_addr;
935         delta_vaddr = 0;
936         dvset = 0;
937
938         seg = params->loadmap->segs;
939
940         /* deal with each load segment separately */
941         phdr = params->phdrs;
942         for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
943                 unsigned long maddr, disp, excess, excess1;
944                 int prot = 0, flags;
945
946                 if (phdr->p_type != PT_LOAD)
947                         continue;
948
949                 kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
950                        (unsigned long) phdr->p_vaddr,
951                        (unsigned long) phdr->p_offset,
952                        (unsigned long) phdr->p_filesz,
953                        (unsigned long) phdr->p_memsz);
954
955                 /* determine the mapping parameters */
956                 if (phdr->p_flags & PF_R) prot |= PROT_READ;
957                 if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
958                 if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
959
960                 flags = MAP_PRIVATE | MAP_DENYWRITE;
961                 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
962                         flags |= MAP_EXECUTABLE;
963
964                 maddr = 0;
965
966                 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
967                 case ELF_FDPIC_FLAG_INDEPENDENT:
968                         /* PT_LOADs are independently locatable */
969                         break;
970
971                 case ELF_FDPIC_FLAG_HONOURVADDR:
972                         /* the specified virtual address must be honoured */
973                         maddr = phdr->p_vaddr;
974                         flags |= MAP_FIXED;
975                         break;
976
977                 case ELF_FDPIC_FLAG_CONSTDISP:
978                         /* constant displacement
979                          * - can be mapped anywhere, but must be mapped as a unit
980                          */
981                         if (!dvset) {
982                                 maddr = load_addr;
983                                 delta_vaddr = phdr->p_vaddr;
984                                 dvset = 1;
985                         }
986                         else {
987                                 maddr = load_addr + phdr->p_vaddr - delta_vaddr;
988                                 flags |= MAP_FIXED;
989                         }
990                         break;
991
992                 case ELF_FDPIC_FLAG_CONTIGUOUS:
993                         /* contiguity handled later */
994                         break;
995
996                 default:
997                         BUG();
998                 }
999
1000                 maddr &= PAGE_MASK;
1001
1002                 /* create the mapping */
1003                 disp = phdr->p_vaddr & ~PAGE_MASK;
1004                 down_write(&mm->mmap_sem);
1005                 maddr = do_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
1006                                 phdr->p_offset - disp);
1007                 up_write(&mm->mmap_sem);
1008
1009                 kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
1010                        loop, phdr->p_memsz + disp, prot, flags, phdr->p_offset - disp,
1011                        maddr);
1012
1013                 if (IS_ERR((void *) maddr))
1014                         return (int) maddr;
1015
1016                 if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) == ELF_FDPIC_FLAG_CONTIGUOUS)
1017                         load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
1018
1019                 seg->addr = maddr + disp;
1020                 seg->p_vaddr = phdr->p_vaddr;
1021                 seg->p_memsz = phdr->p_memsz;
1022
1023                 /* map the ELF header address if in this segment */
1024                 if (phdr->p_offset == 0)
1025                         params->elfhdr_addr = seg->addr;
1026
1027                 /* clear the bit between beginning of mapping and beginning of PT_LOAD */
1028                 if (prot & PROT_WRITE && disp > 0) {
1029                         kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
1030                         clear_user((void *) maddr, disp);
1031                         maddr += disp;
1032                 }
1033
1034                 /* clear any space allocated but not loaded
1035                  * - on uClinux we can just clear the lot
1036                  * - on MMU linux we'll get a SIGBUS beyond the last page
1037                  *   extant in the file
1038                  */
1039                 excess = phdr->p_memsz - phdr->p_filesz;
1040                 excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
1041
1042 #ifdef CONFIG_MMU
1043
1044                 if (excess > excess1) {
1045                         unsigned long xaddr = maddr + phdr->p_filesz + excess1;
1046                         unsigned long xmaddr;
1047
1048                         flags |= MAP_FIXED | MAP_ANONYMOUS;
1049                         down_write(&mm->mmap_sem);
1050                         xmaddr = do_mmap(NULL, xaddr, excess - excess1, prot, flags, 0);
1051                         up_write(&mm->mmap_sem);
1052
1053                         kdebug("mmap[%d] <anon>"
1054                                " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
1055                                loop, xaddr, excess - excess1, prot, flags, xmaddr);
1056
1057                         if (xmaddr != xaddr)
1058                                 return -ENOMEM;
1059                 }
1060
1061                 if (prot & PROT_WRITE && excess1 > 0) {
1062                         kdebug("clear[%d] ad=%lx sz=%lx",
1063                                loop, maddr + phdr->p_filesz, excess1);
1064                         clear_user((void *) maddr + phdr->p_filesz, excess1);
1065                 }
1066
1067 #else
1068                 if (excess > 0) {
1069                         kdebug("clear[%d] ad=%lx sz=%lx",
1070                                loop, maddr + phdr->p_filesz, excess);
1071                         clear_user((void *) maddr + phdr->p_filesz, excess);
1072                 }
1073 #endif
1074
1075                 if (mm) {
1076                         if (phdr->p_flags & PF_X) {
1077                                 mm->start_code = maddr;
1078                                 mm->end_code = maddr + phdr->p_memsz;
1079                         }
1080                         else if (!mm->start_data) {
1081                                 mm->start_data = maddr;
1082                                 mm->end_data = maddr + phdr->p_memsz;
1083                         }
1084                 }
1085
1086                 seg++;
1087         }
1088
1089         return 0;
1090 } /* end elf_fdpic_map_file_by_direct_mmap() */